ORG-NEWS: Backport commit 62803a2ef from Emacs
[org-mode.git] / lisp / org-agenda.el
blobf90dd53bb050ead55d80f0b480af3a47ca1f40bb
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'cl-lib)
49 (require 'org)
50 (require 'org-macs)
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-iso-to-absolute "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-columns-remove-overlays "org-colview" ())
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-agenda-columns "org-colview" ())
83 (declare-function org-add-archive-files "org-archive" (files))
84 (declare-function org-capture "org-capture" (&optional goto keys))
86 (defvar calendar-mode-map)
87 (defvar org-clock-current-task)
88 (defvar org-current-tag-alist)
89 (defvar org-mobile-force-id-on-agenda-items)
90 (defvar org-habit-show-habits)
91 (defvar org-habit-show-habits-only-for-today)
92 (defvar org-habit-show-all-today)
94 ;; Defined somewhere in this file, but used before definition.
95 (defvar org-agenda-buffer-name "*Org Agenda*")
96 (defvar org-agenda-overriding-header nil)
97 (defvar org-agenda-title-append nil)
98 (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
99 (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
100 (defvar original-date) ; dynamically scoped, calendar.el does scope this
102 (defvar org-agenda-undo-list nil
103 "List of undoable operations in the agenda since last refresh.")
104 (defvar org-agenda-pending-undo-list nil
105 "In a series of undo commands, this is the list of remaining undo items.")
107 (defcustom org-agenda-confirm-kill 1
108 "When set, remote killing from the agenda buffer needs confirmation.
109 When t, a confirmation is always needed. When a number N, confirmation is
110 only needed when the text to be killed contains more than N non-white lines."
111 :group 'org-agenda
112 :type '(choice
113 (const :tag "Never" nil)
114 (const :tag "Always" t)
115 (integer :tag "When more than N lines")))
117 (defcustom org-agenda-compact-blocks nil
118 "Non-nil means make the block agenda more compact.
119 This is done globally by leaving out lines like the agenda span
120 name and week number or the separator lines."
121 :group 'org-agenda
122 :type 'boolean)
124 (defcustom org-agenda-block-separator ?=
125 "The separator between blocks in the agenda.
126 If this is a string, it will be used as the separator, with a newline added.
127 If it is a character, it will be repeated to fill the window width.
128 If nil the separator is disabled. In `org-agenda-custom-commands' this
129 addresses the separator between the current and the previous block."
130 :group 'org-agenda
131 :type '(choice
132 (const :tag "Disabled" nil)
133 (character)
134 (string)))
136 (defgroup org-agenda-export nil
137 "Options concerning exporting agenda views in Org mode."
138 :tag "Org Agenda Export"
139 :group 'org-agenda)
141 (defcustom org-agenda-with-colors t
142 "Non-nil means use colors in agenda views."
143 :group 'org-agenda-export
144 :type 'boolean)
146 (defcustom org-agenda-exporter-settings nil
147 "Alist of variable/value pairs that should be active during agenda export.
148 This is a good place to set options for ps-print and for htmlize.
149 Note that the way this is implemented, the values will be evaluated
150 before assigned to the variables. So make sure to quote values you do
151 *not* want evaluated, for example
153 (setq org-agenda-exporter-settings
154 \\='((ps-print-color-p \\='black-white)))"
155 :group 'org-agenda-export
156 :type '(repeat
157 (list
158 (variable)
159 (sexp :tag "Value"))))
161 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
162 "Hook run in a temporary buffer before writing the agenda to an export file.
163 A useful function for this hook is `org-agenda-add-entry-text'."
164 :group 'org-agenda-export
165 :type 'hook
166 :options '(org-agenda-add-entry-text))
168 (defcustom org-agenda-add-entry-text-maxlines 0
169 "Maximum number of entry text lines to be added to agenda.
170 This is only relevant when `org-agenda-add-entry-text' is part of
171 `org-agenda-before-write-hook', which is the default.
172 When this is 0, nothing will happen. When it is greater than 0, it
173 specifies the maximum number of lines that will be added for each entry
174 that is listed in the agenda view.
176 Note that this variable is not used during display, only when exporting
177 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
178 and `org-agenda-entry-text-maxlines'."
179 :group 'org-agenda
180 :type 'integer)
182 (defcustom org-agenda-add-entry-text-descriptive-links t
183 "Non-nil means export org-links as descriptive links in agenda added text.
184 This variable applies to the text added to the agenda when
185 `org-agenda-add-entry-text-maxlines' is larger than 0.
186 When this variable nil, the URL will (also) be shown."
187 :group 'org-agenda
188 :type 'boolean)
190 (defcustom org-agenda-export-html-style nil
191 "The style specification for exported HTML Agenda files.
192 If this variable contains a string, it will replace the default <style>
193 section as produced by `htmlize'.
194 Since there are different ways of setting style information, this variable
195 needs to contain the full HTML structure to provide a style, including the
196 surrounding HTML tags. The style specifications should include definitions
197 the fonts used by the agenda, here is an example:
199 <style type=\"text/css\">
200 p { font-weight: normal; color: gray; }
201 .org-agenda-structure {
202 font-size: 110%;
203 color: #003399;
204 font-weight: 600;
206 .org-todo {
207 color: #cc6666;
208 font-weight: bold;
210 .org-agenda-done {
211 color: #339933;
213 .org-done {
214 color: #339933;
216 .title { text-align: center; }
217 .todo, .deadline { color: red; }
218 .done { color: green; }
219 </style>
221 or, if you want to keep the style in a file,
223 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
225 As the value of this option simply gets inserted into the HTML <head> header,
226 you can \"misuse\" it to also add other text to the header."
227 :group 'org-agenda-export
228 :group 'org-export-html
229 :type '(choice
230 (const nil)
231 (string)))
233 (defcustom org-agenda-persistent-filter nil
234 "When set, keep filters from one agenda view to the next."
235 :group 'org-agenda
236 :type 'boolean)
238 (defgroup org-agenda-custom-commands nil
239 "Options concerning agenda views in Org mode."
240 :tag "Org Agenda Custom Commands"
241 :group 'org-agenda)
243 (defconst org-sorting-choice
244 '(choice
245 (const time-up) (const time-down)
246 (const timestamp-up) (const timestamp-down)
247 (const scheduled-up) (const scheduled-down)
248 (const deadline-up) (const deadline-down)
249 (const ts-up) (const ts-down)
250 (const tsia-up) (const tsia-down)
251 (const category-keep) (const category-up) (const category-down)
252 (const tag-down) (const tag-up)
253 (const priority-up) (const priority-down)
254 (const todo-state-up) (const todo-state-down)
255 (const effort-up) (const effort-down)
256 (const habit-up) (const habit-down)
257 (const alpha-up) (const alpha-down)
258 (const user-defined-up) (const user-defined-down))
259 "Sorting choices.")
261 ;; Keep custom values for `org-agenda-filter-preset' compatible with
262 ;; the new variable `org-agenda-tag-filter-preset'.
263 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
264 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
266 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
267 "List of types searched for when creating the daily/weekly agenda.
268 This variable is a list of symbols that controls the types of
269 items that appear in the daily/weekly agenda. Allowed symbols in this
270 list are are
272 :timestamp List items containing a date stamp or date range matching
273 the selected date. This includes sexp entries in angular
274 brackets.
276 :sexp List entries resulting from plain diary-like sexps.
278 :deadline List deadline due on that date. When the date is today,
279 also list any deadlines past due, or due within
280 `org-deadline-warning-days'. `:deadline' must appear before
281 `:scheduled' if the setting of
282 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
283 any effect.
285 :deadline* Same as above, but only include the deadline if it has an
286 hour specification as [h]h:mm.
288 :scheduled List all items which are scheduled for the given date.
289 The diary for *today* also contains items which were
290 scheduled earlier and are not yet marked DONE.
292 :scheduled* Same as above, but only include the scheduled item if it
293 has an hour specification as [h]h:mm.
295 By default, all four non-starred types are turned on.
297 When :scheduled* or :deadline* are included, :schedule or :deadline
298 will be ignored.
300 Never set this variable globally using `setq', because then it
301 will apply to all future agenda commands. Instead, bind it with
302 `let' to scope it dynamically into the agenda-constructing
303 command. A good way to set it is through options in
304 `org-agenda-custom-commands'. For a more flexible (though
305 somewhat less efficient) way of determining what is included in
306 the daily/weekly agenda, see `org-agenda-skip-function'.")
308 (defconst org-agenda-custom-commands-local-options
309 `(repeat :tag "Local settings for this command. Remember to quote values"
310 (choice :tag "Setting"
311 (list :tag "Heading for this block"
312 (const org-agenda-overriding-header)
313 (string :tag "Headline"))
314 (list :tag "Files to be searched"
315 (const org-agenda-files)
316 (list
317 (const :format "" quote)
318 (repeat (file))))
319 (list :tag "Sorting strategy"
320 (const org-agenda-sorting-strategy)
321 (list
322 (const :format "" quote)
323 (repeat
324 ,org-sorting-choice)))
325 (list :tag "Prefix format"
326 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
327 (string))
328 (list :tag "Number of days in agenda"
329 (const org-agenda-span)
330 (choice (const :tag "Day" day)
331 (const :tag "Week" week)
332 (const :tag "Fortnight" fortnight)
333 (const :tag "Month" month)
334 (const :tag "Year" year)
335 (integer :tag "Custom")))
336 (list :tag "Fixed starting date"
337 (const org-agenda-start-day)
338 (string :value "2007-11-01"))
339 (list :tag "Start on day of week"
340 (const org-agenda-start-on-weekday)
341 (choice :value 1
342 (const :tag "Today" nil)
343 (integer :tag "Weekday No.")))
344 (list :tag "Include data from diary"
345 (const org-agenda-include-diary)
346 (boolean))
347 (list :tag "Deadline Warning days"
348 (const org-deadline-warning-days)
349 (integer :value 1))
350 (list :tag "Category filter preset"
351 (const org-agenda-category-filter-preset)
352 (list
353 (const :format "" quote)
354 (repeat
355 (string :tag "+category or -category"))))
356 (list :tag "Tags filter preset"
357 (const org-agenda-tag-filter-preset)
358 (list
359 (const :format "" quote)
360 (repeat
361 (string :tag "+tag or -tag"))))
362 (list :tag "Effort filter preset"
363 (const org-agenda-effort-filter-preset)
364 (list
365 (const :format "" quote)
366 (repeat
367 (string :tag "+=10 or -=10 or +<10 or ->10"))))
368 (list :tag "Regexp filter preset"
369 (const org-agenda-regexp-filter-preset)
370 (list
371 (const :format "" quote)
372 (repeat
373 (string :tag "+regexp or -regexp"))))
374 (list :tag "Set daily/weekly entry types"
375 (const org-agenda-entry-types)
376 (list
377 (const :format "" quote)
378 (set :greedy t :value ,org-agenda-entry-types
379 (const :deadline)
380 (const :scheduled)
381 (const :deadline*)
382 (const :scheduled*)
383 (const :timestamp)
384 (const :sexp))))
385 (list :tag "Standard skipping condition"
386 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
387 (const org-agenda-skip-function)
388 (list
389 (const :format "" quote)
390 (list
391 (choice
392 :tag "Skipping range"
393 (const :tag "Skip entry" org-agenda-skip-entry-if)
394 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
395 (repeat :inline t :tag "Conditions for skipping"
396 (choice
397 :tag "Condition type"
398 (list :tag "Regexp matches" :inline t
399 (const :format "" 'regexp)
400 (regexp))
401 (list :tag "Regexp does not match" :inline t
402 (const :format "" 'notregexp)
403 (regexp))
404 (list :tag "TODO state is" :inline t
405 (const 'todo)
406 (choice
407 (const :tag "Any not-done state" 'todo)
408 (const :tag "Any done state" 'done)
409 (const :tag "Any state" 'any)
410 (list :tag "Keyword list"
411 (const :format "" quote)
412 (repeat (string :tag "Keyword")))))
413 (list :tag "TODO state is not" :inline t
414 (const 'nottodo)
415 (choice
416 (const :tag "Any not-done state" 'todo)
417 (const :tag "Any done state" 'done)
418 (const :tag "Any state" 'any)
419 (list :tag "Keyword list"
420 (const :format "" quote)
421 (repeat (string :tag "Keyword")))))
422 (const :tag "scheduled" 'scheduled)
423 (const :tag "not scheduled" 'notscheduled)
424 (const :tag "deadline" 'deadline)
425 (const :tag "no deadline" 'notdeadline)
426 (const :tag "timestamp" 'timestamp)
427 (const :tag "no timestamp" 'nottimestamp))))))
428 (list :tag "Non-standard skipping condition"
429 :value (org-agenda-skip-function)
430 (const org-agenda-skip-function)
431 (sexp :tag "Function or form (quoted!)"))
432 (list :tag "Any variable"
433 (variable :tag "Variable")
434 (sexp :tag "Value (sexp)"))))
435 "Selection of examples for agenda command settings.
436 This will be spliced into the custom type of
437 `org-agenda-custom-commands'.")
440 (defcustom org-agenda-custom-commands
441 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
442 "Custom commands for the agenda.
443 \\<org-mode-map>
444 These commands will be offered on the splash screen displayed by the
445 agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
447 (key desc type match settings files)
449 key The key (one or more characters as a string) to be associated
450 with the command.
451 desc A description of the command, when omitted or nil, a default
452 description is built using MATCH.
453 type The command type, any of the following symbols:
454 agenda The daily/weekly agenda.
455 todo Entries with a specific TODO keyword, in all agenda files.
456 search Entries containing search words entry or headline.
457 tags Tags/Property/TODO match in all agenda files.
458 tags-todo Tags/P/T match in all agenda files, TODO entries only.
459 todo-tree Sparse tree of specific TODO keyword in *current* file.
460 tags-tree Sparse tree with all tags matches in *current* file.
461 occur-tree Occur sparse tree for *current* file.
462 ... A user-defined function.
463 match What to search for:
464 - a single keyword for TODO keyword searches
465 - a tags match expression for tags searches
466 - a word search expression for text searches.
467 - a regular expression for occur searches
468 For all other commands, this should be the empty string.
469 settings A list of option settings, similar to that in a let form, so like
470 this: ((opt1 val1) (opt2 val2) ...). The values will be
471 evaluated at the moment of execution, so quote them when needed.
472 files A list of files to write the produced agenda buffer to with
473 the command `org-store-agenda-views'.
474 If a file name ends in \".html\", an HTML version of the buffer
475 is written out. If it ends in \".ps\", a postscript version is
476 produced. Otherwise, only the plain text is written to the file.
478 You can also define a set of commands, to create a composite agenda buffer.
479 In this case, an entry looks like this:
481 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
483 where
485 desc A description string to be displayed in the dispatcher menu.
486 cmd An agenda command, similar to the above. However, tree commands
487 are not allowed, but instead you can get agenda and global todo list.
488 So valid commands for a set are:
489 (agenda \"\" settings)
490 (alltodo \"\" settings)
491 (stuck \"\" settings)
492 (todo \"match\" settings files)
493 (search \"match\" settings files)
494 (tags \"match\" settings files)
495 (tags-todo \"match\" settings files)
497 Each command can carry a list of options, and another set of options can be
498 given for the whole set of commands. Individual command options take
499 precedence over the general options.
501 When using several characters as key to a command, the first characters
502 are prefix commands. For the dispatcher to display useful information, you
503 should provide a description for the prefix, like
505 (setq org-agenda-custom-commands
506 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
507 (\"hl\" tags \"+HOME+Lisa\")
508 (\"hp\" tags \"+HOME+Peter\")
509 (\"hk\" tags \"+HOME+Kim\")))"
510 :group 'org-agenda-custom-commands
511 :type `(repeat
512 (choice :value ("x" "Describe command here" tags "" nil)
513 (list :tag "Single command"
514 (string :tag "Access Key(s) ")
515 (option (string :tag "Description"))
516 (choice
517 (const :tag "Agenda" agenda)
518 (const :tag "TODO list" alltodo)
519 (const :tag "Search words" search)
520 (const :tag "Stuck projects" stuck)
521 (const :tag "Tags/Property match (all agenda files)" tags)
522 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
523 (const :tag "TODO keyword search (all agenda files)" todo)
524 (const :tag "Tags sparse tree (current buffer)" tags-tree)
525 (const :tag "TODO keyword tree (current buffer)" todo-tree)
526 (const :tag "Occur tree (current buffer)" occur-tree)
527 (sexp :tag "Other, user-defined function"))
528 (string :tag "Match (only for some commands)")
529 ,org-agenda-custom-commands-local-options
530 (option (repeat :tag "Export" (file :tag "Export to"))))
531 (list :tag "Command series, all agenda files"
532 (string :tag "Access Key(s)")
533 (string :tag "Description ")
534 (repeat :tag "Component"
535 (choice
536 (list :tag "Agenda"
537 (const :format "" agenda)
538 (const :tag "" :format "" "")
539 ,org-agenda-custom-commands-local-options)
540 (list :tag "TODO list (all keywords)"
541 (const :format "" alltodo)
542 (const :tag "" :format "" "")
543 ,org-agenda-custom-commands-local-options)
544 (list :tag "Search words"
545 (const :format "" search)
546 (string :tag "Match")
547 ,org-agenda-custom-commands-local-options)
548 (list :tag "Stuck projects"
549 (const :format "" stuck)
550 (const :tag "" :format "" "")
551 ,org-agenda-custom-commands-local-options)
552 (list :tag "Tags search"
553 (const :format "" tags)
554 (string :tag "Match")
555 ,org-agenda-custom-commands-local-options)
556 (list :tag "Tags search, TODO entries only"
557 (const :format "" tags-todo)
558 (string :tag "Match")
559 ,org-agenda-custom-commands-local-options)
560 (list :tag "TODO keyword search"
561 (const :format "" todo)
562 (string :tag "Match")
563 ,org-agenda-custom-commands-local-options)
564 (list :tag "Other, user-defined function"
565 (symbol :tag "function")
566 (string :tag "Match")
567 ,org-agenda-custom-commands-local-options)))
569 (repeat :tag "Settings for entire command set"
570 (list (variable :tag "Any variable")
571 (sexp :tag "Value")))
572 (option (repeat :tag "Export" (file :tag "Export to"))))
573 (cons :tag "Prefix key documentation"
574 (string :tag "Access Key(s)")
575 (string :tag "Description ")))))
577 (defcustom org-agenda-query-register ?o
578 "The register holding the current query string.
579 The purpose of this is that if you construct a query string interactively,
580 you can then use it to define a custom command."
581 :group 'org-agenda-custom-commands
582 :type 'character)
584 (defcustom org-stuck-projects
585 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
586 "How to identify stuck projects.
587 This is a list of four items:
588 1. A tags/todo/property matcher string that is used to identify a project.
589 See the manual for a description of tag and property searches.
590 The entire tree below a headline matched by this is considered one project.
591 2. A list of TODO keywords identifying non-stuck projects.
592 If the project subtree contains any headline with one of these todo
593 keywords, the project is considered to be not stuck. If you specify
594 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
595 3. A list of tags identifying non-stuck projects.
596 If the project subtree contains any headline with one of these tags,
597 the project is considered to be not stuck. If you specify \"*\" as
598 a tag, any tag will mark the project unstuck. Note that this is about
599 the explicit presence of a tag somewhere in the subtree, inherited
600 tags do not count here. If inherited tags make a project not stuck,
601 use \"-TAG\" in the tags part of the matcher under (1.) above.
602 4. An arbitrary regular expression matching non-stuck projects.
604 If the project turns out to be not stuck, search continues also in the
605 subtree to see if any of the subtasks have project status.
607 See also the variable `org-tags-match-list-sublevels' which applies
608 to projects matched by this search as well.
610 After defining this variable, you may use `\\[org-agenda-list-stuck-projects]'
611 \(bound to `C-c a #') to produce the list."
612 :group 'org-agenda-custom-commands
613 :type '(list
614 (string :tag "Tags/TODO match to identify a project")
615 (repeat :tag "Projects are *not* stuck if they have an entry with \
616 TODO keyword any of" (string))
617 (repeat :tag "Projects are *not* stuck if they have an entry with \
618 TAG being any of" (string))
619 (regexp :tag "Projects are *not* stuck if this regexp matches inside \
620 the subtree")))
622 (defgroup org-agenda-skip nil
623 "Options concerning skipping parts of agenda files."
624 :tag "Org Agenda Skip"
625 :group 'org-agenda)
627 (defcustom org-agenda-skip-function-global nil
628 "Function to be called at each match during agenda construction.
629 If this function returns nil, the current match should not be skipped.
630 If the function decided to skip an agenda match, is must return the
631 buffer position from which the search should be continued.
632 This may also be a Lisp form, which will be evaluated.
634 This variable will be applied to every agenda match, including
635 tags/property searches and TODO lists. So try to make the test function
636 do its checking as efficiently as possible. To implement a skipping
637 condition just for specific agenda commands, use the variable
638 `org-agenda-skip-function' which can be set in the options section
639 of custom agenda commands."
640 :group 'org-agenda-skip
641 :type 'sexp)
643 (defgroup org-agenda-daily/weekly nil
644 "Options concerning the daily/weekly agenda."
645 :tag "Org Agenda Daily/Weekly"
646 :group 'org-agenda)
647 (defgroup org-agenda-todo-list nil
648 "Options concerning the global todo list agenda view."
649 :tag "Org Agenda Todo List"
650 :group 'org-agenda)
651 (defgroup org-agenda-match-view nil
652 "Options concerning the general tags/property/todo match agenda view."
653 :tag "Org Agenda Match View"
654 :group 'org-agenda)
655 (defgroup org-agenda-search-view nil
656 "Options concerning the search agenda view."
657 :tag "Org Agenda Search View"
658 :group 'org-agenda)
660 (defvar org-agenda-archives-mode nil
661 "Non-nil means the agenda will include archived items.
662 If this is the symbol `trees', trees in the selected agenda scope
663 that are marked with the ARCHIVE tag will be included anyway. When this is
664 t, also all archive files associated with the current selection of agenda
665 files will be included.")
667 (defcustom org-agenda-restriction-lock-highlight-subtree t
668 "Non-nil means highlight the whole subtree when restriction is active.
669 Otherwise only highlight the headline. Highlighting the whole subtree is
670 useful to ensure no edits happen beyond the restricted region."
671 :group 'org-agenda
672 :type 'boolean)
674 (defcustom org-agenda-skip-comment-trees t
675 "Non-nil means skip trees that start with the COMMENT keyword.
676 When nil, these trees are also scanned by agenda commands."
677 :group 'org-agenda-skip
678 :type 'boolean)
680 (defcustom org-agenda-todo-list-sublevels t
681 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
682 When nil, the sublevels of a TODO entry are not checked, resulting in
683 potentially much shorter TODO lists."
684 :group 'org-agenda-skip
685 :group 'org-agenda-todo-list
686 :type 'boolean)
688 (defcustom org-agenda-todo-ignore-with-date nil
689 "Non-nil means don't show entries with a date in the global todo list.
690 You can use this if you prefer to mark mere appointments with a TODO keyword,
691 but don't want them to show up in the TODO list.
692 When this is set, it also covers deadlines and scheduled items, the settings
693 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
694 will be ignored.
695 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
696 :group 'org-agenda-skip
697 :group 'org-agenda-todo-list
698 :type 'boolean)
700 (defcustom org-agenda-todo-ignore-timestamp nil
701 "Non-nil means don't show entries with a timestamp.
702 This applies when creating the global todo list.
703 Valid values are:
705 past Don't show entries for today or in the past.
707 future Don't show entries with a timestamp in the future.
708 The idea behind this is that if it has a future
709 timestamp, you don't want to think about it until the
710 date.
712 all Don't show any entries with a timestamp in the global todo list.
713 The idea behind this is that by setting a timestamp, you
714 have already \"taken care\" of this item.
716 This variable can also have an integer as a value. If positive (N),
717 todos with a timestamp N or more days in the future will be ignored. If
718 negative (-N), todos with a timestamp N or more days in the past will be
719 ignored. If 0, todos with a timestamp either today or in the future will
720 be ignored. For example, a value of -1 will exclude todos with a
721 timestamp in the past (yesterday or earlier), while a value of 7 will
722 exclude todos with a timestamp a week or more in the future.
724 See also `org-agenda-todo-ignore-with-date'.
725 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
726 to make his option also apply to the tags-todo list."
727 :group 'org-agenda-skip
728 :group 'org-agenda-todo-list
729 :version "24.1"
730 :type '(choice
731 (const :tag "Ignore future timestamp todos" future)
732 (const :tag "Ignore past or present timestamp todos" past)
733 (const :tag "Ignore all timestamp todos" all)
734 (const :tag "Show timestamp todos" nil)
735 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
737 (defcustom org-agenda-todo-ignore-scheduled nil
738 "Non-nil means, ignore some scheduled TODO items when making TODO list.
739 This applies when creating the global todo list.
740 Valid values are:
742 past Don't show entries scheduled today or in the past.
744 future Don't show entries scheduled in the future.
745 The idea behind this is that by scheduling it, you don't want to
746 think about it until the scheduled date.
748 all Don't show any scheduled entries in the global todo list.
749 The idea behind this is that by scheduling it, you have already
750 \"taken care\" of this item.
752 t Same as `all', for backward compatibility.
754 This variable can also have an integer as a value. See
755 `org-agenda-todo-ignore-timestamp' for more details.
757 See also `org-agenda-todo-ignore-with-date'.
758 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
759 to make his option also apply to the tags-todo list."
760 :group 'org-agenda-skip
761 :group 'org-agenda-todo-list
762 :type '(choice
763 (const :tag "Ignore future-scheduled todos" future)
764 (const :tag "Ignore past- or present-scheduled todos" past)
765 (const :tag "Ignore all scheduled todos" all)
766 (const :tag "Ignore all scheduled todos (compatibility)" t)
767 (const :tag "Show scheduled todos" nil)
768 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
770 (defcustom org-agenda-todo-ignore-deadlines nil
771 "Non-nil means ignore some deadline TODO items when making TODO list.
773 There are different motivations for using different values, please think
774 carefully when configuring this variable.
776 This applies when creating the global TODO list.
778 Valid values are:
780 near Don't show near deadline entries. A deadline is near when it is
781 closer than `org-deadline-warning-days' days. The idea behind this
782 is that such items will appear in the agenda anyway.
784 far Don't show TODO entries where a deadline has been defined, but
785 is not going to happen anytime soon. This is useful if you want to use
786 the TODO list to figure out what to do now.
788 past Don't show entries with a deadline timestamp for today or in the past.
790 future Don't show entries with a deadline timestamp in the future, not even
791 when they become `near' ones. Use it with caution.
793 all Ignore all TODO entries that do have a deadline.
795 t Same as `near', for backward compatibility.
797 This variable can also have an integer as a value. See
798 `org-agenda-todo-ignore-timestamp' for more details.
800 See also `org-agenda-todo-ignore-with-date'.
801 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
802 to make his option also apply to the tags-todo list."
803 :group 'org-agenda-skip
804 :group 'org-agenda-todo-list
805 :type '(choice
806 (const :tag "Ignore near deadlines" near)
807 (const :tag "Ignore near deadlines (compatibility)" t)
808 (const :tag "Ignore far deadlines" far)
809 (const :tag "Ignore all TODOs with a deadlines" all)
810 (const :tag "Show all TODOs, even if they have a deadline" nil)
811 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
813 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
814 "Time unit to use when possibly ignoring an agenda item.
816 See the docstring of various `org-agenda-todo-ignore-*' options.
817 The default is to compare time stamps using days. An item is thus
818 considered to be in the future if it is at least one day after today.
819 Non-nil means to compare time stamps using seconds. An item is then
820 considered future if it has a time value later than current time."
821 :group 'org-agenda-skip
822 :group 'org-agenda-todo-list
823 :version "24.4"
824 :package-version '(Org . "8.0")
825 :type '(choice
826 (const :tag "Compare time with days" nil)
827 (const :tag "Compare time with seconds" t)))
829 (defcustom org-agenda-tags-todo-honor-ignore-options nil
830 "Non-nil means honor todo-list ignores options also in tags-todo search.
831 The variables
832 `org-agenda-todo-ignore-with-date',
833 `org-agenda-todo-ignore-timestamp',
834 `org-agenda-todo-ignore-scheduled',
835 `org-agenda-todo-ignore-deadlines'
836 make the global TODO list skip entries that have time stamps of certain
837 kinds. If this option is set, the same options will also apply for the
838 tags-todo search, which is the general tags/property matcher
839 restricted to unfinished TODO entries only."
840 :group 'org-agenda-skip
841 :group 'org-agenda-todo-list
842 :group 'org-agenda-match-view
843 :type 'boolean)
845 (defcustom org-agenda-skip-scheduled-if-done nil
846 "Non-nil means don't show scheduled items in agenda when they are done.
847 This is relevant for the daily/weekly agenda, not for the TODO list. It
848 applies only to the actual date of the scheduling. Warnings about an item
849 with a past scheduling dates are always turned off when the item is DONE."
850 :group 'org-agenda-skip
851 :group 'org-agenda-daily/weekly
852 :type 'boolean)
854 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
855 "Non-nil means skip scheduling line if same entry shows because of deadline.
857 In the agenda of today, an entry can show up multiple times
858 because it is both scheduled and has a nearby deadline, and maybe
859 a plain time stamp as well.
861 When this variable is nil, the entry will be shown several times.
863 When set to t, then only the deadline is shown and the fact that
864 the entry is scheduled today or was scheduled previously is not
865 shown.
867 When set to the symbol `not-today', skip scheduled previously,
868 but not scheduled today.
870 When set to the symbol `repeated-after-deadline', skip scheduled
871 items if they are repeated beyond the current deadline."
872 :group 'org-agenda-skip
873 :group 'org-agenda-daily/weekly
874 :type '(choice
875 (const :tag "Never" nil)
876 (const :tag "Always" t)
877 (const :tag "Not when scheduled today" not-today)
878 (const :tag "When repeated past deadline" repeated-after-deadline)))
880 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
881 "Non-nil means skip timestamp line if same entry shows because of deadline.
882 In the agenda of today, an entry can show up multiple times
883 because it has both a plain timestamp and has a nearby deadline.
884 When this variable is t, then only the deadline is shown and the
885 fact that the entry has a timestamp for or including today is not
886 shown. When this variable is nil, the entry will be shown
887 several times."
888 :group 'org-agenda-skip
889 :group 'org-agenda-daily/weekly
890 :version "24.1"
891 :type '(choice
892 (const :tag "Never" nil)
893 (const :tag "Always" t)))
895 (defcustom org-agenda-skip-deadline-if-done nil
896 "Non-nil means don't show deadlines when the corresponding item is done.
897 When nil, the deadline is still shown and should give you a happy feeling.
898 This is relevant for the daily/weekly agenda. It applies only to the
899 actual date of the deadline. Warnings about approaching and past-due
900 deadlines are always turned off when the item is DONE."
901 :group 'org-agenda-skip
902 :group 'org-agenda-daily/weekly
903 :type 'boolean)
905 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
906 "Non-nil means skip deadline prewarning when entry is also scheduled.
907 This will apply on all days where a prewarning for the deadline would
908 be shown, but not at the day when the entry is actually due. On that day,
909 the deadline will be shown anyway.
910 This variable may be set to nil, t, the symbol `pre-scheduled',
911 or a number which will then give the number of days before the actual
912 deadline when the prewarnings should resume. The symbol `pre-scheduled'
913 eliminates the deadline prewarning only prior to the scheduled date.
914 This can be used in a workflow where the first showing of the deadline will
915 trigger you to schedule it, and then you don't want to be reminded of it
916 because you will take care of it on the day when scheduled."
917 :group 'org-agenda-skip
918 :group 'org-agenda-daily/weekly
919 :version "24.1"
920 :type '(choice
921 (const :tag "Always show prewarning" nil)
922 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
923 (const :tag "Remove prewarning if entry is scheduled" t)
924 (integer :tag "Restart prewarning N days before deadline")))
926 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
927 "Non-nil means skip scheduled delay when entry also has a deadline.
928 This variable may be set to nil, t, the symbol `post-deadline',
929 or a number which will then give the number of days after the actual
930 scheduled date when the delay should expire. The symbol `post-deadline'
931 eliminates the schedule delay when the date is posterior to the deadline."
932 :group 'org-agenda-skip
933 :group 'org-agenda-daily/weekly
934 :version "24.4"
935 :package-version '(Org . "8.0")
936 :type '(choice
937 (const :tag "Always honor delay" nil)
938 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
939 (const :tag "Ignore delay if entry has a deadline" t)
940 (integer :tag "Honor delay up until N days after the scheduled date")))
942 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
943 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
944 When non-nil, after the search for timestamps has matched once in an
945 entry, the rest of the entry will not be searched."
946 :group 'org-agenda-skip
947 :type 'boolean)
949 (defcustom org-agenda-skip-timestamp-if-done nil
950 "Non-nil means don't select item by timestamp or -range if it is DONE."
951 :group 'org-agenda-skip
952 :group 'org-agenda-daily/weekly
953 :type 'boolean)
955 (defcustom org-agenda-dim-blocked-tasks t
956 "Non-nil means dim blocked tasks in the agenda display.
957 This causes some overhead during agenda construction, but if you
958 have turned on `org-enforce-todo-dependencies',
959 `org-enforce-todo-checkbox-dependencies', or any other blocking
960 mechanism, this will create useful feedback in the agenda.
962 Instead of t, this variable can also have the value `invisible'.
963 Then blocked tasks will be invisible and only become visible when
964 they become unblocked. An exemption to this behavior is when a task is
965 blocked because of unchecked checkboxes below it. Since checkboxes do
966 not show up in the agenda views, making this task invisible you remove any
967 trace from agenda views that there is something to do. Therefore, a task
968 that is blocked because of checkboxes will never be made invisible, it
969 will only be dimmed."
970 :group 'org-agenda-daily/weekly
971 :group 'org-agenda-todo-list
972 :version "24.3"
973 :type '(choice
974 (const :tag "Do not dim" nil)
975 (const :tag "Dim to a gray face" t)
976 (const :tag "Make invisible" invisible)))
978 (defcustom org-timeline-show-empty-dates 3
979 "Non-nil means `org-timeline' also shows dates without an entry.
980 When nil, only the days which actually have entries are shown.
981 When t, all days between the first and the last date are shown.
982 When an integer, show also empty dates, but if there is a gap of more than
983 N days, just insert a special line indicating the size of the gap."
984 :group 'org-agenda-skip
985 :type '(choice
986 (const :tag "None" nil)
987 (const :tag "All" t)
988 (integer :tag "at most")))
990 (defgroup org-agenda-startup nil
991 "Options concerning initial settings in the Agenda in Org Mode."
992 :tag "Org Agenda Startup"
993 :group 'org-agenda)
995 (defcustom org-agenda-menu-show-matcher t
996 "Non-nil means show the match string in the agenda dispatcher menu.
997 When nil, the matcher string is not shown, but is put into the help-echo
998 property so than moving the mouse over the command shows it.
999 Setting it to nil is good if matcher strings are very long and/or if
1000 you want to use two-columns display (see `org-agenda-menu-two-columns')."
1001 :group 'org-agenda
1002 :version "24.1"
1003 :type 'boolean)
1005 (defcustom org-agenda-menu-two-columns nil
1006 "Non-nil means, use two columns to show custom commands in the dispatcher.
1007 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1008 to nil."
1009 :group 'org-agenda
1010 :version "24.1"
1011 :type 'boolean)
1013 (defcustom org-agenda-finalize-hook nil
1014 "Hook run just before displaying an agenda buffer.
1015 The buffer is still writable when the hook is called.
1017 You can modify some of the buffer substrings but you should be
1018 extra careful not to modify the text properties of the agenda
1019 headlines as the agenda display heavily relies on them."
1020 :group 'org-agenda-startup
1021 :type 'hook)
1023 (defcustom org-agenda-mouse-1-follows-link nil
1024 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1025 A longer mouse click will still set point. Needs to be set
1026 before org.el is loaded."
1027 :group 'org-agenda-startup
1028 :type 'boolean)
1030 (defcustom org-agenda-start-with-follow-mode nil
1031 "The initial value of follow mode in a newly created agenda window."
1032 :group 'org-agenda-startup
1033 :type 'boolean)
1035 (defcustom org-agenda-follow-indirect nil
1036 "Non-nil means `org-agenda-follow-mode' displays only the
1037 current item's tree, in an indirect buffer."
1038 :group 'org-agenda
1039 :version "24.1"
1040 :type 'boolean)
1042 (defcustom org-agenda-show-outline-path t
1043 "Non-nil means show outline path in echo area after line motion."
1044 :group 'org-agenda-startup
1045 :type 'boolean)
1047 (defcustom org-agenda-start-with-entry-text-mode nil
1048 "The initial value of entry-text-mode in a newly created agenda window."
1049 :group 'org-agenda-startup
1050 :type 'boolean)
1052 (defcustom org-agenda-entry-text-maxlines 5
1053 "Number of text lines to be added when `E' is pressed in the agenda.
1055 Note that this variable only used during agenda display. To add entry text
1056 when exporting the agenda, configure the variable
1057 `org-agenda-add-entry-text-maxlines'."
1058 :group 'org-agenda
1059 :type 'integer)
1061 (defcustom org-agenda-entry-text-exclude-regexps nil
1062 "List of regular expressions to clean up entry text.
1063 The complete matches of all regular expressions in this list will be
1064 removed from entry text before it is shown in the agenda."
1065 :group 'org-agenda
1066 :type '(repeat (regexp)))
1068 (defcustom org-agenda-entry-text-leaders " > "
1069 "Text prepended to the entry text in agenda buffers."
1070 :version "24.4"
1071 :package-version '(Org . "8.0")
1072 :group 'org-agenda
1073 :type 'string)
1075 (defvar org-agenda-entry-text-cleanup-hook nil
1076 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1077 This cleanup is done in a temporary buffer, so the function may inspect and
1078 change the entire buffer.
1079 Some default stuff like drawers and scheduling/deadline dates will already
1080 have been removed when this is called, as will any matches for regular
1081 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1083 (defvar org-agenda-include-inactive-timestamps nil
1084 "Non-nil means include inactive time stamps in agenda and timeline.
1085 Dynamically scoped.")
1087 (defgroup org-agenda-windows nil
1088 "Options concerning the windows used by the Agenda in Org Mode."
1089 :tag "Org Agenda Windows"
1090 :group 'org-agenda)
1092 (defcustom org-agenda-window-setup 'reorganize-frame
1093 "How the agenda buffer should be displayed.
1094 Possible values for this option are:
1096 current-window Show agenda in the current window, keeping all other windows.
1097 other-window Use `switch-to-buffer-other-window' to display agenda.
1098 only-window Show agenda, deleting all other windows.
1099 reorganize-frame Show only two windows on the current frame, the current
1100 window and the agenda.
1101 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1102 Also, when exiting the agenda, kill that frame.
1103 See also the variable `org-agenda-restore-windows-after-quit'."
1104 :group 'org-agenda-windows
1105 :type '(choice
1106 (const current-window)
1107 (const other-frame)
1108 (const other-window)
1109 (const only-window)
1110 (const reorganize-frame)))
1112 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1113 "The min and max height of the agenda window as a fraction of frame height.
1114 The value of the variable is a cons cell with two numbers between 0 and 1.
1115 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1116 :group 'org-agenda-windows
1117 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1119 (defcustom org-agenda-restore-windows-after-quit nil
1120 "Non-nil means restore window configuration upon exiting agenda.
1121 Before the window configuration is changed for displaying the agenda,
1122 the current status is recorded. When the agenda is exited with
1123 `q' or `x' and this option is set, the old state is restored. If
1124 `org-agenda-window-setup' is `other-frame', the value of this
1125 option will be ignored."
1126 :group 'org-agenda-windows
1127 :type 'boolean)
1129 (defcustom org-agenda-span 'week
1130 "Number of days to include in overview display.
1131 Can be day, week, month, year, or any number of days.
1132 Custom commands can set this variable in the options section."
1133 :group 'org-agenda-daily/weekly
1134 :type '(choice (const :tag "Day" day)
1135 (const :tag "Week" week)
1136 (const :tag "Fortnight" fortnight)
1137 (const :tag "Month" month)
1138 (const :tag "Year" year)
1139 (integer :tag "Custom")))
1141 (defcustom org-agenda-start-on-weekday 1
1142 "Non-nil means start the overview always on the specified weekday.
1143 0 denotes Sunday, 1 denotes Monday, etc.
1144 When nil, always start on the current day.
1145 Custom commands can set this variable in the options section."
1146 :group 'org-agenda-daily/weekly
1147 :type '(choice (const :tag "Today" nil)
1148 (integer :tag "Weekday No.")))
1150 (defcustom org-agenda-show-all-dates t
1151 "Non-nil means `org-agenda' shows every day in the selected range.
1152 When nil, only the days which actually have entries are shown."
1153 :group 'org-agenda-daily/weekly
1154 :type 'boolean)
1156 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1157 "Format string for displaying dates in the agenda.
1158 Used by the daily/weekly agenda and by the timeline. This should be
1159 a format string understood by `format-time-string', or a function returning
1160 the formatted date as a string. The function must take a single argument,
1161 a calendar-style date list like (month day year)."
1162 :group 'org-agenda-daily/weekly
1163 :type '(choice
1164 (string :tag "Format string")
1165 (function :tag "Function")))
1167 (defun org-agenda-format-date-aligned (date)
1168 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1169 This function makes sure that dates are aligned for easy reading."
1170 (require 'cal-iso)
1171 (let* ((dayname (calendar-day-name date))
1172 (day (cadr date))
1173 (day-of-week (calendar-day-of-week date))
1174 (month (car date))
1175 (monthname (calendar-month-name month))
1176 (year (nth 2 date))
1177 (iso-week (org-days-to-iso-week
1178 (calendar-absolute-from-gregorian date)))
1179 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1180 (1- year))
1181 ((and (= month 12) (<= iso-week 1))
1182 (1+ year))
1183 (t year)))
1184 (weekstring (if (= day-of-week 1)
1185 (format " W%02d" iso-week)
1186 "")))
1187 (format "%-10s %2d %s %4d%s"
1188 dayname day monthname year weekstring)))
1190 (defcustom org-agenda-time-leading-zero nil
1191 "Non-nil means use leading zero for military times in agenda.
1192 For example, 9:30am would become 09:30 rather than 9:30."
1193 :group 'org-agenda-daily/weekly
1194 :version "24.1"
1195 :type 'boolean)
1197 (defcustom org-agenda-timegrid-use-ampm nil
1198 "When set, show AM/PM style timestamps on the timegrid."
1199 :group 'org-agenda
1200 :version "24.1"
1201 :type 'boolean)
1203 (defun org-agenda-time-of-day-to-ampm (time)
1204 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1205 (let* ((hour-number (string-to-number (substring time 0 -3)))
1206 (minute (substring time -2))
1207 (ampm "am"))
1208 (cond
1209 ((equal hour-number 12)
1210 (setq ampm "pm"))
1211 ((> hour-number 12)
1212 (setq ampm "pm")
1213 (setq hour-number (- hour-number 12))))
1214 (concat
1215 (if org-agenda-time-leading-zero
1216 (format "%02d" hour-number)
1217 (format "%02s" (number-to-string hour-number)))
1218 ":" minute ampm)))
1220 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1221 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1222 (if org-agenda-timegrid-use-ampm
1223 (org-agenda-time-of-day-to-ampm time)
1224 time))
1226 (defcustom org-agenda-weekend-days '(6 0)
1227 "Which days are weekend?
1228 These days get the special face `org-agenda-date-weekend' in the agenda
1229 and timeline buffers."
1230 :group 'org-agenda-daily/weekly
1231 :type '(set :greedy t
1232 (const :tag "Monday" 1)
1233 (const :tag "Tuesday" 2)
1234 (const :tag "Wednesday" 3)
1235 (const :tag "Thursday" 4)
1236 (const :tag "Friday" 5)
1237 (const :tag "Saturday" 6)
1238 (const :tag "Sunday" 0)))
1240 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1241 "Non-nil means jump to today when moving a past date forward in time.
1242 When using S-right in the agenda to move a a date forward, and the date
1243 stamp currently points to the past, the first key press will move it
1244 to today. WHen nil, just move one day forward even if the date stays
1245 in the past."
1246 :group 'org-agenda-daily/weekly
1247 :version "24.1"
1248 :type 'boolean)
1250 (defcustom org-agenda-include-diary nil
1251 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1252 Custom commands can set this variable in the options section."
1253 :group 'org-agenda-daily/weekly
1254 :type 'boolean)
1256 (defcustom org-agenda-include-deadlines t
1257 "If non-nil, include entries within their deadline warning period.
1258 Custom commands can set this variable in the options section."
1259 :group 'org-agenda-daily/weekly
1260 :version "24.1"
1261 :type 'boolean)
1263 (defcustom org-agenda-repeating-timestamp-show-all t
1264 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1265 When set to a list of strings, only show occurrences of repeating
1266 stamps for these TODO keywords. When nil, only one occurrence is
1267 shown, either today or the nearest into the future."
1268 :group 'org-agenda-daily/weekly
1269 :type '(choice
1270 (const :tag "Show repeating stamps" t)
1271 (repeat :tag "Show repeating stamps for these TODO keywords"
1272 (string :tag "TODO Keyword"))
1273 (const :tag "Don't show repeating stamps" nil)))
1275 (defcustom org-scheduled-past-days 10000
1276 "Number of days to continue listing scheduled items not marked DONE.
1277 When an item is scheduled on a date, it shows up in the agenda on
1278 this day and will be listed until it is marked done or for the
1279 number of days given here."
1280 :group 'org-agenda-daily/weekly
1281 :type 'integer)
1283 (defcustom org-agenda-log-mode-items '(closed clock)
1284 "List of items that should be shown in agenda log mode.
1285 \\<org-agenda-mode-map>\
1286 This list may contain the following symbols:
1288 closed Show entries that have been closed on that day.
1289 clock Show entries that have received clocked time on that day.
1290 state Show all logged state changes.
1291 Note that instead of changing this variable, you can also press \
1292 `\\[universal-argument] \\[org-agenda-log-mode]' in
1293 the agenda to display all available LOG items temporarily."
1294 :group 'org-agenda-daily/weekly
1295 :type '(set :greedy t (const closed) (const clock) (const state)))
1297 (defcustom org-agenda-clock-consistency-checks
1298 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1299 :gap-ok-around ("4:00")
1300 :default-face ((:background "DarkRed") (:foreground "white"))
1301 :overlap-face nil :gap-face nil :no-end-time-face nil
1302 :long-face nil :short-face nil)
1303 "This is a property list, with the following keys:
1305 :max-duration Mark clocking chunks that are longer than this time.
1306 This is a time string like \"HH:MM\", or the number
1307 of minutes as an integer.
1309 :min-duration Mark clocking chunks that are shorter that this.
1310 This is a time string like \"HH:MM\", or the number
1311 of minutes as an integer.
1313 :max-gap Mark gaps between clocking chunks that are longer than
1314 this duration. A number of minutes, or a string
1315 like \"HH:MM\".
1317 :gap-ok-around List of times during the day which are usually not working
1318 times. When a gap is detected, but the gap contains any
1319 of these times, the gap is *not* reported. For example,
1320 if this is (\"4:00\" \"13:00\") then gaps that contain
1321 4:00 in the morning (i.e. the night) and 13:00
1322 (i.e. a typical lunch time) do not cause a warning.
1323 You should have at least one time during the night in this
1324 list, or otherwise the first task each morning will trigger
1325 a warning because it follows a long gap.
1327 Furthermore, the following properties can be used to define faces for
1328 issue display.
1330 :default-face the default face, if the specific face is undefined
1331 :overlap-face face for overlapping clocks
1332 :gap-face face for gaps between clocks
1333 :no-end-time-face face for incomplete clocks
1334 :long-face face for clock intervals that are too long
1335 :short-face face for clock intervals that are too short"
1336 :group 'org-agenda-daily/weekly
1337 :group 'org-clock
1338 :version "24.1"
1339 :type 'plist)
1341 (defcustom org-agenda-log-mode-add-notes t
1342 "Non-nil means add first line of notes to log entries in agenda views.
1343 If a log item like a state change or a clock entry is associated with
1344 notes, the first line of these notes will be added to the entry in the
1345 agenda display."
1346 :group 'org-agenda-daily/weekly
1347 :type 'boolean)
1349 (defcustom org-agenda-start-with-log-mode nil
1350 "The initial value of log-mode in a newly created agenda window.
1351 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1352 explanations on the possible values."
1353 :group 'org-agenda-startup
1354 :group 'org-agenda-daily/weekly
1355 :type '(choice (const :tag "Don't show log items" nil)
1356 (const :tag "Show only log items" only)
1357 (const :tag "Show all possible log items" clockcheck)
1358 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1359 (choice (const :tag "Show closed log items" closed)
1360 (const :tag "Show clocked log items" clock)
1361 (const :tag "Show all logged state changes" state)))))
1363 (defcustom org-agenda-start-with-clockreport-mode nil
1364 "The initial value of clockreport-mode in a newly created agenda window."
1365 :group 'org-agenda-startup
1366 :group 'org-agenda-daily/weekly
1367 :type 'boolean)
1369 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1370 "Property list with parameters for the clocktable in clockreport mode.
1371 This is the display mode that shows a clock table in the daily/weekly
1372 agenda, the properties for this dynamic block can be set here.
1373 The usual clocktable parameters are allowed here, but you cannot set
1374 the properties :name, :tstart, :tend, :block, and :scope - these will
1375 be overwritten to make sure the content accurately reflects the
1376 current display in the agenda."
1377 :group 'org-agenda-daily/weekly
1378 :type 'plist)
1380 (defcustom org-agenda-search-view-always-boolean nil
1381 "Non-nil means the search string is interpreted as individual parts.
1383 The search string for search view can either be interpreted as a phrase,
1384 or as a list of snippets that define a boolean search for a number of
1385 strings.
1387 When this is non-nil, the string will be split on whitespace, and each
1388 snippet will be searched individually, and all must match in order to
1389 select an entry. A snippet is then a single string of non-white
1390 characters, or a string in double quotes, or a regexp in {} braces.
1391 If a snippet is preceded by \"-\", the snippet must *not* match.
1392 \"+\" is syntactic sugar for positive selection. Each snippet may
1393 be found as a full word or a partial word, but see the variable
1394 `org-agenda-search-view-force-full-words'.
1396 When this is nil, search will look for the entire search phrase as one,
1397 with each space character matching any amount of whitespace, including
1398 line breaks.
1400 Even when this is nil, you can still switch to Boolean search dynamically
1401 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1402 is a regexp marked with braces like \"{abc}\", this will also switch to
1403 boolean search."
1404 :group 'org-agenda-search-view
1405 :version "24.1"
1406 :type 'boolean)
1408 (defvaralias 'org-agenda-search-view-search-words-only
1409 'org-agenda-search-view-always-boolean)
1411 (defcustom org-agenda-search-view-force-full-words nil
1412 "Non-nil means, search words must be matches as complete words.
1413 When nil, they may also match part of a word."
1414 :group 'org-agenda-search-view
1415 :version "24.1"
1416 :type 'boolean)
1418 (defcustom org-agenda-search-view-max-outline-level 0
1419 "Maximum outline level to display in search view.
1420 E.g. when this is set to 1, the search view will only
1421 show headlines of level 1. When set to 0, the default
1422 value, don't limit agenda view by outline level."
1423 :group 'org-agenda-search-view
1424 :version "24.4"
1425 :package-version '(Org . "8.3")
1426 :type 'integer)
1428 (defgroup org-agenda-time-grid nil
1429 "Options concerning the time grid in the Org Agenda."
1430 :tag "Org Agenda Time Grid"
1431 :group 'org-agenda)
1433 (defcustom org-agenda-search-headline-for-time t
1434 "Non-nil means search headline for a time-of-day.
1435 If the headline contains a time-of-day in one format or another, it will
1436 be used to sort the entry into the time sequence of items for a day.
1437 Some people have time stamps in the headline that refer to the creation
1438 time or so, and then this produces an unwanted side effect. If this is
1439 the case for your, use this variable to turn off searching the headline
1440 for a time."
1441 :group 'org-agenda-time-grid
1442 :type 'boolean)
1444 (defcustom org-agenda-use-time-grid t
1445 "Non-nil means show a time grid in the agenda schedule.
1446 A time grid is a set of lines for specific times (like every two hours between
1447 8:00 and 20:00). The items scheduled for a day at specific times are
1448 sorted in between these lines.
1449 For details about when the grid will be shown, and what it will look like, see
1450 the variable `org-agenda-time-grid'."
1451 :group 'org-agenda-time-grid
1452 :type 'boolean)
1454 (defcustom org-agenda-time-grid
1455 '((daily today require-timed)
1456 "----------------"
1457 (800 1000 1200 1400 1600 1800 2000))
1459 "The settings for time grid for agenda display.
1460 This is a list of three items. The first item is again a list. It contains
1461 symbols specifying conditions when the grid should be displayed:
1463 daily if the agenda shows a single day
1464 weekly if the agenda shows an entire week
1465 today show grid on current date, independent of daily/weekly display
1466 require-timed show grid only if at least one item has a time specification
1467 remove-match skip grid times already present in an entry
1469 The second item is a string which will be placed behind the grid time.
1471 The third item is a list of integers, indicating the times that should have
1472 a grid line."
1473 :group 'org-agenda-time-grid
1474 :type
1475 '(list
1476 (set :greedy t :tag "Grid Display Options"
1477 (const :tag "Show grid in single day agenda display" daily)
1478 (const :tag "Show grid in weekly agenda display" weekly)
1479 (const :tag "Always show grid for today" today)
1480 (const :tag "Show grid only if any timed entries are present"
1481 require-timed)
1482 (const :tag "Skip grid times already present in an entry"
1483 remove-match))
1484 (string :tag "Grid String")
1485 (repeat :tag "Grid Times" (integer :tag "Time"))))
1487 (defcustom org-agenda-show-current-time-in-grid t
1488 "Non-nil means show the current time in the time grid."
1489 :group 'org-agenda-time-grid
1490 :version "24.1"
1491 :type 'boolean)
1493 (defcustom org-agenda-current-time-string
1494 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1495 "The string for the current time marker in the agenda."
1496 :group 'org-agenda-time-grid
1497 :version "24.1"
1498 :type 'string)
1500 (defgroup org-agenda-sorting nil
1501 "Options concerning sorting in the Org Agenda."
1502 :tag "Org Agenda Sorting"
1503 :group 'org-agenda)
1505 (defcustom org-agenda-sorting-strategy
1506 '((agenda habit-down time-up priority-down category-keep)
1507 (todo priority-down category-keep)
1508 (tags priority-down category-keep)
1509 (search category-keep))
1510 "Sorting structure for the agenda items of a single day.
1511 This is a list of symbols which will be used in sequence to determine
1512 if an entry should be listed before another entry. The following
1513 symbols are recognized:
1515 time-up Put entries with time-of-day indications first, early first
1516 time-down Put entries with time-of-day indications first, late first
1517 timestamp-up Sort by any timestamp, early first
1518 timestamp-down Sort by any timestamp, late first
1519 scheduled-up Sort by scheduled timestamp, early first
1520 scheduled-down Sort by scheduled timestamp, late first
1521 deadline-up Sort by deadline timestamp, early first
1522 deadline-down Sort by deadline timestamp, late first
1523 ts-up Sort by active timestamp, early first
1524 ts-down Sort by active timestamp, late first
1525 tsia-up Sort by inactive timestamp, early first
1526 tsia-down Sort by inactive timestamp, late first
1527 category-keep Keep the default order of categories, corresponding to the
1528 sequence in `org-agenda-files'.
1529 category-up Sort alphabetically by category, A-Z.
1530 category-down Sort alphabetically by category, Z-A.
1531 tag-up Sort alphabetically by last tag, A-Z.
1532 tag-down Sort alphabetically by last tag, Z-A.
1533 priority-up Sort numerically by priority, high priority last.
1534 priority-down Sort numerically by priority, high priority first.
1535 todo-state-up Sort by todo state, tasks that are done last.
1536 todo-state-down Sort by todo state, tasks that are done first.
1537 effort-up Sort numerically by estimated effort, high effort last.
1538 effort-down Sort numerically by estimated effort, high effort first.
1539 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1540 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1541 habit-up Put entries that are habits first
1542 habit-down Put entries that are habits last
1543 alpha-up Sort headlines alphabetically
1544 alpha-down Sort headlines alphabetically, reversed
1546 The different possibilities will be tried in sequence, and testing stops
1547 if one comparison returns a \"not-equal\". For example, the default
1548 '(time-up category-keep priority-down)
1549 means: Pull out all entries having a specified time of day and sort them,
1550 in order to make a time schedule for the current day the first thing in the
1551 agenda listing for the day. Of the entries without a time indication, keep
1552 the grouped in categories, don't sort the categories, but keep them in
1553 the sequence given in `org-agenda-files'. Within each category sort by
1554 priority.
1556 Leaving out `category-keep' would mean that items will be sorted across
1557 categories by priority.
1559 Instead of a single list, this can also be a set of list for specific
1560 contents, with a context symbol in the car of the list, any of
1561 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1563 Custom commands can bind this variable in the options section."
1564 :group 'org-agenda-sorting
1565 :type `(choice
1566 (repeat :tag "General" ,org-sorting-choice)
1567 (list :tag "Individually"
1568 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1569 (repeat ,org-sorting-choice))
1570 (cons (const :tag "Strategy for TODO lists" todo)
1571 (repeat ,org-sorting-choice))
1572 (cons (const :tag "Strategy for Tags matches" tags)
1573 (repeat ,org-sorting-choice))
1574 (cons (const :tag "Strategy for search matches" search)
1575 (repeat ,org-sorting-choice)))))
1577 (defcustom org-agenda-cmp-user-defined nil
1578 "A function to define the comparison `user-defined'.
1579 This function must receive two arguments, agenda entry a and b.
1580 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1581 the user comparison, return nil.
1582 When this is defined, you can make `user-defined-up' and `user-defined-down'
1583 part of an agenda sorting strategy."
1584 :group 'org-agenda-sorting
1585 :type 'symbol)
1587 (defcustom org-sort-agenda-notime-is-late t
1588 "Non-nil means items without time are considered late.
1589 This is only relevant for sorting. When t, items which have no explicit
1590 time like 15:30 will be considered as 99:01, i.e. later than any items which
1591 do have a time. When nil, the default time is before 0:00. You can use this
1592 option to decide if the schedule for today should come before or after timeless
1593 agenda entries."
1594 :group 'org-agenda-sorting
1595 :type 'boolean)
1597 (defcustom org-sort-agenda-noeffort-is-high t
1598 "Non-nil means items without effort estimate are sorted as high effort.
1599 This also applies when filtering an agenda view with respect to the
1600 < or > effort operator. Then, tasks with no effort defined will be treated
1601 as tasks with high effort.
1602 When nil, such items are sorted as 0 minutes effort."
1603 :group 'org-agenda-sorting
1604 :type 'boolean)
1606 (defgroup org-agenda-line-format nil
1607 "Options concerning the entry prefix in the Org agenda display."
1608 :tag "Org Agenda Line Format"
1609 :group 'org-agenda)
1611 (defcustom org-agenda-prefix-format
1612 '((agenda . " %i %-12:c%?-12t% s")
1613 (timeline . " % s")
1614 (todo . " %i %-12:c")
1615 (tags . " %i %-12:c")
1616 (search . " %i %-12:c"))
1617 "Format specifications for the prefix of items in the agenda views.
1618 An alist with five entries, each for the different agenda types. The
1619 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1620 The values are format strings.
1622 This format works similar to a printf format, with the following meaning:
1624 %c the category of the item, \"Diary\" for entries from the diary,
1625 or as given by the CATEGORY keyword or derived from the file name
1626 %e the effort required by the item
1627 %l the level of the item (insert X space(s) if item is of level X)
1628 %i the icon category of the item, see `org-agenda-category-icon-alist'
1629 %T the last tag of the item (ignore inherited tags, which come first)
1630 %t the HH:MM time-of-day specification if one applies to the entry
1631 %s Scheduling/Deadline information, a short string
1632 %b show breadcrumbs, i.e., the names of the higher levels
1633 %(expression) Eval EXPRESSION and replace the control string
1634 by the result
1636 All specifiers work basically like the standard `%s' of printf, but may
1637 contain two additional characters: a question mark just after the `%'
1638 and a whitespace/punctuation character just before the final letter.
1640 If the first character after `%' is a question mark, the entire field
1641 will only be included if the corresponding value applies to the current
1642 entry. This is useful for fields which should have fixed width when
1643 present, but zero width when absent. For example, \"%?-12t\" will
1644 result in a 12 character time field if a time of the day is specified,
1645 but will completely disappear in entries which do not contain a time.
1647 If there is punctuation or whitespace character just before the
1648 final format letter, this character will be appended to the field
1649 value if the value is not empty. For example, the format
1650 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1651 the category is empty, no additional colon is inserted.
1653 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1654 which means:
1656 - Indent the line with two space characters
1657 - Give the category a 12 chars wide field, padded with whitespace on
1658 the right (because of `-'). Append a colon if there is a category
1659 (because of `:').
1660 - If there is a time-of-day, put it into a 12 chars wide field. If no
1661 time, don't put in an empty field, just skip it (because of '?').
1662 - Finally, put the scheduling information.
1664 See also the variables `org-agenda-remove-times-when-in-prefix' and
1665 `org-agenda-remove-tags'.
1667 Custom commands can set this variable in the options section."
1668 :type '(choice
1669 (string :tag "General format")
1670 (list :greedy t :tag "View dependent"
1671 (cons (const agenda) (string :tag "Format"))
1672 (cons (const timeline) (string :tag "Format"))
1673 (cons (const todo) (string :tag "Format"))
1674 (cons (const tags) (string :tag "Format"))
1675 (cons (const search) (string :tag "Format"))))
1676 :group 'org-agenda-line-format)
1678 (defvar org-prefix-format-compiled nil
1679 "The compiled prefix format and associated variables.
1680 This is a list where first element is a list of variable bindings, and second
1681 element is the compiled format expression. See the variable
1682 `org-agenda-prefix-format'.")
1684 (defcustom org-agenda-todo-keyword-format "%-1s"
1685 "Format for the TODO keyword in agenda lines.
1686 Set this to something like \"%-12s\" if you want all TODO keywords
1687 to occupy a fixed space in the agenda display."
1688 :group 'org-agenda-line-format
1689 :type 'string)
1691 (defcustom org-agenda-diary-sexp-prefix nil
1692 "A regexp that matches part of a diary sexp entry
1693 which should be treated as scheduling/deadline information in
1694 `org-agenda'.
1696 For example, you can use this to extract the `diary-remind-message' from
1697 `diary-remind' entries."
1698 :group 'org-agenda-line-format
1699 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1701 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1702 "Text preceding timerange entries in the agenda view.
1703 This is a list with two strings. The first applies when the range
1704 is entirely on one day. The second applies if the range spans several days.
1705 The strings may have two \"%d\" format specifiers which will be filled
1706 with the sequence number of the days, and the total number of days in the
1707 range, respectively."
1708 :group 'org-agenda-line-format
1709 :type '(list
1710 (string :tag "Deadline today ")
1711 (choice :tag "Deadline relative"
1712 (string :tag "Format string")
1713 (function))))
1715 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1716 "Text preceding scheduled items in the agenda view.
1717 This is a list with two strings. The first applies when the item is
1718 scheduled on the current day. The second applies when it has been scheduled
1719 previously, it may contain a %d indicating that this is the nth time that
1720 this item is scheduled, due to automatic rescheduling of unfinished items
1721 for the following day. So this number is one larger than the number of days
1722 that passed since this item was scheduled first."
1723 :group 'org-agenda-line-format
1724 :version "24.4"
1725 :package-version '(Org . "8.0")
1726 :type '(list
1727 (string :tag "Scheduled today ")
1728 (string :tag "Scheduled previously")))
1730 (defcustom org-agenda-inactive-leader "["
1731 "Text preceding item pulled into the agenda by inactive time stamps.
1732 These entries are added to the agenda when pressing \"[\"."
1733 :group 'org-agenda-line-format
1734 :version "24.1"
1735 :type 'string)
1737 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1738 "Text preceding deadline items in the agenda view.
1739 This is a list with three strings. The first applies when the item has its
1740 deadline on the current day. The second applies when the deadline is in the
1741 future, the third one when it is in the past. The strings may contain %d
1742 to capture the number of days."
1743 :group 'org-agenda-line-format
1744 :version "24.4"
1745 :package-version '(Org . "8.0")
1746 :type '(list
1747 (string :tag "Deadline today ")
1748 (string :tag "Deadline in the future ")
1749 (string :tag "Deadline in the past ")))
1751 (defcustom org-agenda-remove-times-when-in-prefix t
1752 "Non-nil means remove duplicate time specifications in agenda items.
1753 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1754 time-of-day specification in a headline or diary entry is extracted and
1755 placed into the prefix. If this option is non-nil, the original specification
1756 \(a timestamp or -range, or just a plain time(range) specification like
1757 11:30-4pm) will be removed for agenda display. This makes the agenda less
1758 cluttered.
1759 The option can be t or nil. It may also be the symbol `beg', indicating
1760 that the time should only be removed when it is located at the beginning of
1761 the headline/diary entry."
1762 :group 'org-agenda-line-format
1763 :type '(choice
1764 (const :tag "Always" t)
1765 (const :tag "Never" nil)
1766 (const :tag "When at beginning of entry" beg)))
1768 (defcustom org-agenda-remove-timeranges-from-blocks nil
1769 "Non-nil means remove time ranges specifications in agenda
1770 items that span on several days."
1771 :group 'org-agenda-line-format
1772 :version "24.1"
1773 :type 'boolean)
1775 (defcustom org-agenda-default-appointment-duration nil
1776 "Default duration for appointments that only have a starting time.
1777 When nil, no duration is specified in such cases.
1778 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1779 :group 'org-agenda-line-format
1780 :type '(choice
1781 (integer :tag "Minutes")
1782 (const :tag "No default duration")))
1784 (defcustom org-agenda-show-inherited-tags t
1785 "Non-nil means show inherited tags in each agenda line.
1787 When this option is set to `always', it takes precedence over
1788 `org-agenda-use-tag-inheritance' and inherited tags are shown
1789 in every agenda.
1791 When this option is set to t (the default), inherited tags are
1792 shown when they are available, i.e. when the value of
1793 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1794 given agenda type.
1796 This can be set to a list of agenda types in which the agenda
1797 must display the inherited tags. Available types are `todo',
1798 `agenda', `search' and `timeline'.
1800 When set to nil, never show inherited tags in agenda lines."
1801 :group 'org-agenda-line-format
1802 :group 'org-agenda
1803 :version "24.3"
1804 :type '(choice
1805 (const :tag "Show inherited tags when available" t)
1806 (const :tag "Always show inherited tags" always)
1807 (repeat :tag "Show inherited tags only in selected agenda types"
1808 (symbol :tag "Agenda type"))))
1810 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1811 "List of agenda view types where to use tag inheritance.
1813 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1814 controlled by `org-use-tag-inheritance'. In other agenda types,
1815 `org-use-tag-inheritance' is not used for the selection of the
1816 agenda entries. Still, you may want the agenda to be aware of
1817 the inherited tags anyway, e.g. for later tag filtering.
1819 Allowed value are `todo', `search', `timeline' and `agenda'.
1821 This variable has no effect if `org-agenda-show-inherited-tags'
1822 is set to `always'. In that case, the agenda is aware of those
1823 tags.
1825 The default value sets tags in every agenda type. Setting this
1826 option to nil will speed up non-tags agenda view a lot."
1827 :group 'org-agenda
1828 :version "24.3"
1829 :type '(choice
1830 (const :tag "Use tag inheritance in all agenda types" t)
1831 (repeat :tag "Use tag inheritance in selected agenda types"
1832 (symbol :tag "Agenda type"))))
1834 (defcustom org-agenda-hide-tags-regexp nil
1835 "Regular expression used to filter away specific tags in agenda views.
1836 This means that these tags will be present, but not be shown in the agenda
1837 line. Secondary filtering will still work on the hidden tags.
1838 Nil means don't hide any tags."
1839 :group 'org-agenda-line-format
1840 :type '(choice
1841 (const :tag "Hide none" nil)
1842 (string :tag "Regexp ")))
1844 (defcustom org-agenda-remove-tags nil
1845 "Non-nil means remove the tags from the headline copy in the agenda.
1846 When this is the symbol `prefix', only remove tags when
1847 `org-agenda-prefix-format' contains a `%T' specifier."
1848 :group 'org-agenda-line-format
1849 :type '(choice
1850 (const :tag "Always" t)
1851 (const :tag "Never" nil)
1852 (const :tag "When prefix format contains %T" prefix)))
1854 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1855 'org-agenda-remove-tags)
1857 (defcustom org-agenda-tags-column -80
1858 "Shift tags in agenda items to this column.
1859 If this number is positive, it specifies the column. If it is negative,
1860 it means that the tags should be flushright to that column. For example,
1861 -80 works well for a normal 80 character screen."
1862 :group 'org-agenda-line-format
1863 :type 'integer)
1865 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1867 (defcustom org-agenda-fontify-priorities 'cookies
1868 "Non-nil means highlight low and high priorities in agenda.
1869 When t, the highest priority entries are bold, lowest priority italic.
1870 However, settings in `org-priority-faces' will overrule these faces.
1871 When this variable is the symbol `cookies', only fontify the
1872 cookies, not the entire task.
1873 This may also be an association list of priority faces, whose
1874 keys are the character values of `org-highest-priority',
1875 `org-default-priority', and `org-lowest-priority' (the default values
1876 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1877 color as a string, or a list like `(:background \"Red\")'.
1878 If it is a color, the variable `org-faces-easy-properties'
1879 determines if it is a foreground or a background color."
1880 :group 'org-agenda-line-format
1881 :type '(choice
1882 (const :tag "Never" nil)
1883 (const :tag "Defaults" t)
1884 (const :tag "Cookies only" cookies)
1885 (repeat :tag "Specify"
1886 (list (character :tag "Priority" :value ?A)
1887 (choice :tag "Face "
1888 (string :tag "Color")
1889 (sexp :tag "Face"))))))
1891 (defcustom org-agenda-day-face-function nil
1892 "Function called to determine what face should be used to display a day.
1893 The only argument passed to that function is the day. It should
1894 returns a face, or nil if does not want to specify a face and let
1895 the normal rules apply."
1896 :group 'org-agenda-line-format
1897 :version "24.1"
1898 :type '(choice (const nil) (function)))
1900 (defcustom org-agenda-category-icon-alist nil
1901 "Alist of category icon to be displayed in agenda views.
1903 Each entry should have the following format:
1905 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1907 Where CATEGORY-REGEXP is a regexp matching the categories where
1908 the icon should be displayed.
1909 FILE-OR-DATA either a file path or a string containing image data.
1911 The other fields can be omitted safely if not needed:
1912 TYPE indicates the image type.
1913 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1914 image data.
1915 PROPS are additional image attributes to assign to the image,
1916 like, e.g. `:ascent center'.
1918 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1920 If you want to set the display properties yourself, just put a
1921 list as second element:
1923 (CATEGORY-REGEXP (MY PROPERTY LIST))
1925 For example, to display a 16px horizontal space for Emacs
1926 category, you can use:
1928 (\"Emacs\" \\='(space . (:width (16))))"
1929 :group 'org-agenda-line-format
1930 :version "24.1"
1931 :type '(alist :key-type (string :tag "Regexp matching category")
1932 :value-type (choice (list :tag "Icon"
1933 (string :tag "File or data")
1934 (symbol :tag "Type")
1935 (boolean :tag "Data?")
1936 (repeat :tag "Extra image properties" :inline t symbol))
1937 (list :tag "Display properties" sexp))))
1939 (defgroup org-agenda-column-view nil
1940 "Options concerning column view in the agenda."
1941 :tag "Org Agenda Column View"
1942 :group 'org-agenda)
1944 (defcustom org-agenda-view-columns-initially nil
1945 "When non-nil, switch to columns view right after creating the agenda."
1946 :group 'org-agenda-column-view
1947 :type 'boolean
1948 :version "26.1"
1949 :package-version '(Org . "9.0")
1950 :safe #'booleanp)
1952 (defcustom org-agenda-columns-show-summaries t
1953 "Non-nil means show summaries for columns displayed in the agenda view."
1954 :group 'org-agenda-column-view
1955 :type 'boolean)
1957 (defcustom org-agenda-columns-compute-summary-properties t
1958 "Non-nil means recompute all summary properties before column view.
1959 When column view in the agenda is listing properties that have a summary
1960 operator, it can go to all relevant buffers and recompute the summaries
1961 there. This can mean overhead for the agenda column view, but is necessary
1962 to have thing up to date.
1963 As a special case, a CLOCKSUM property also makes sure that the clock
1964 computations are current."
1965 :group 'org-agenda-column-view
1966 :type 'boolean)
1968 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1969 "Non-nil means the duration of an appointment will add to day effort.
1970 The property to which appointment durations will be added is the one given
1971 in the option `org-effort-property'. If an appointment does not have
1972 an end time, `org-agenda-default-appointment-duration' will be used. If that
1973 is not set, an appointment without end time will not contribute to the time
1974 estimate."
1975 :group 'org-agenda-column-view
1976 :type 'boolean)
1978 (defcustom org-agenda-auto-exclude-function nil
1979 "A function called with a tag to decide if it is filtered on \
1980 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1981 The sole argument to the function, which is called once for each
1982 possible tag, is a string giving the name of the tag. The
1983 function should return either nil if the tag should be included
1984 as normal, or \"-<TAG>\" to exclude the tag.
1985 Note that for the purpose of tag filtering, only the lower-case version of
1986 all tags will be considered, so that this function will only ever see
1987 the lower-case version of all tags."
1988 :group 'org-agenda
1989 :type '(choice (const nil) (function)))
1991 (defcustom org-agenda-bulk-custom-functions nil
1992 "Alist of characters and custom functions for bulk actions.
1993 For example, this value makes those two functions available:
1995 \\='((?R set-category)
1996 (?C bulk-cut))
1998 With selected entries in an agenda buffer, `B R' will call
1999 the custom function `set-category' on the selected entries.
2000 Note that functions in this alist don't need to be quoted."
2001 :type '(alist :key-type character :value-type (group function))
2002 :version "24.1"
2003 :group 'org-agenda)
2005 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2006 "Execute BODY with point at location given by `org-hd-marker' property.
2007 If STRING is non-nil, the text property will be fetched from position 0
2008 in that string. If STRING is nil, it will be fetched from the beginning
2009 of the current line."
2010 (org-with-gensyms (marker)
2011 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2012 'org-hd-marker ,string)))
2013 (with-current-buffer (marker-buffer ,marker)
2014 (save-excursion
2015 (goto-char ,marker)
2016 ,@body)))))
2017 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2019 (defun org-add-agenda-custom-command (entry)
2020 "Replace or add a command in `org-agenda-custom-commands'.
2021 This is mostly for hacking and trying a new command - once the command
2022 works you probably want to add it to `org-agenda-custom-commands' for good."
2023 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2024 (if ass
2025 (setcdr ass (cdr entry))
2026 (push entry org-agenda-custom-commands))))
2028 ;;; Define the org-agenda-mode
2030 (defvar org-agenda-mode-map (make-sparse-keymap)
2031 "Keymap for `org-agenda-mode'.")
2032 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2034 (defvar org-agenda-menu) ; defined later in this file.
2035 (defvar org-agenda-restrict nil) ; defined later in this file.
2036 (defvar org-agenda-follow-mode nil)
2037 (defvar org-agenda-entry-text-mode nil)
2038 (defvar org-agenda-clockreport-mode nil)
2039 (defvar org-agenda-show-log nil)
2040 (defvar org-agenda-redo-command nil)
2041 (defvar org-agenda-query-string nil)
2042 (defvar org-agenda-mode-hook nil
2043 "Hook run after `org-agenda-mode' is turned on.
2044 The buffer is still writable when this hook is called.")
2045 (defvar org-agenda-type nil)
2046 (defvar org-agenda-force-single-file nil)
2047 (defvar org-agenda-bulk-marked-entries nil
2048 "List of markers that refer to marked entries in the agenda.")
2049 (defvar org-agenda-current-date nil
2050 "Active date when building the agenda.")
2052 ;;; Multiple agenda buffers support
2054 (defcustom org-agenda-sticky nil
2055 "Non-nil means agenda q key will bury agenda buffers.
2056 Agenda commands will then show existing buffer instead of generating new ones.
2057 When nil, `q' will kill the single agenda buffer."
2058 :group 'org-agenda
2059 :version "24.3"
2060 :type 'boolean)
2063 ;;;###autoload
2064 (defun org-toggle-sticky-agenda (&optional arg)
2065 "Toggle `org-agenda-sticky'."
2066 (interactive "P")
2067 (let ((new-value (if arg
2068 (> (prefix-numeric-value arg) 0)
2069 (not org-agenda-sticky))))
2070 (if (equal new-value org-agenda-sticky)
2071 (and (called-interactively-p 'interactive)
2072 (message "Sticky agenda was already %s"
2073 (if org-agenda-sticky "enabled" "disabled")))
2074 (setq org-agenda-sticky new-value)
2075 (org-agenda-kill-all-agenda-buffers)
2076 (and (called-interactively-p 'interactive)
2077 (message "Sticky agenda %s"
2078 (if org-agenda-sticky "enabled" "disabled"))))))
2080 (defvar org-agenda-buffer nil
2081 "Agenda buffer currently being generated.")
2083 (defvar org-agenda-last-prefix-arg nil)
2084 (defvar org-agenda-this-buffer-name nil)
2085 (defvar org-agenda-doing-sticky-redo nil)
2086 (defvar org-agenda-this-buffer-is-sticky nil)
2087 (defvar org-agenda-last-indirect-buffer nil
2088 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2090 (defconst org-agenda-local-vars
2091 '(org-agenda-this-buffer-name
2092 org-agenda-undo-list
2093 org-agenda-pending-undo-list
2094 org-agenda-follow-mode
2095 org-agenda-entry-text-mode
2096 org-agenda-clockreport-mode
2097 org-agenda-show-log
2098 org-agenda-redo-command
2099 org-agenda-query-string
2100 org-agenda-type
2101 org-agenda-bulk-marked-entries
2102 org-agenda-undo-has-started-in
2103 org-agenda-info
2104 org-agenda-pre-window-conf
2105 org-agenda-columns-active
2106 org-agenda-tag-filter
2107 org-agenda-category-filter
2108 org-agenda-top-headline-filter
2109 org-agenda-regexp-filter
2110 org-agenda-effort-filter
2111 org-agenda-markers
2112 org-agenda-last-search-view-search-was-boolean
2113 org-agenda-last-indirect-buffer
2114 org-agenda-filtered-by-category
2115 org-agenda-filter-form
2116 org-agenda-cycle-counter
2117 org-agenda-last-prefix-arg)
2118 "Variables that must be local in agenda buffers to allow multiple buffers.")
2120 (defun org-agenda-mode ()
2121 "Mode for time-sorted view on action items in Org files.
2123 The following commands are available:
2125 \\{org-agenda-mode-map}"
2126 (interactive)
2127 (cond (org-agenda-doing-sticky-redo
2128 ;; Refreshing sticky agenda-buffer
2130 ;; Preserve the value of `org-agenda-local-vars' variables,
2131 ;; while letting `kill-all-local-variables' kill the rest
2132 (let ((save (buffer-local-variables)))
2133 (kill-all-local-variables)
2134 (mapc #'make-local-variable org-agenda-local-vars)
2135 (dolist (elem save)
2136 (pcase elem
2137 (`(,var . ,val) ;ignore unbound variables
2138 (when (and val (memq var org-agenda-local-vars))
2139 (set var val))))))
2140 (setq-local org-agenda-this-buffer-is-sticky t))
2141 (org-agenda-sticky
2142 ;; Creating a sticky Agenda buffer for the first time
2143 (kill-all-local-variables)
2144 (mapc 'make-local-variable org-agenda-local-vars)
2145 (setq-local org-agenda-this-buffer-is-sticky t))
2147 ;; Creating a non-sticky agenda buffer
2148 (kill-all-local-variables)
2149 (setq-local org-agenda-this-buffer-is-sticky nil)))
2150 (setq org-agenda-undo-list nil
2151 org-agenda-pending-undo-list nil
2152 org-agenda-bulk-marked-entries nil)
2153 (setq major-mode 'org-agenda-mode)
2154 ;; Keep global-font-lock-mode from turning on font-lock-mode
2155 (setq-local font-lock-global-modes (list 'not major-mode))
2156 (setq mode-name "Org-Agenda")
2157 (setq indent-tabs-mode nil)
2158 (use-local-map org-agenda-mode-map)
2159 (easy-menu-add org-agenda-menu)
2160 (if org-startup-truncated (setq truncate-lines t))
2161 (setq-local line-move-visual nil)
2162 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2163 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2164 ;; Make sure properties are removed when copying text
2165 (add-hook 'filter-buffer-substring-functions
2166 (lambda (fun start end delete)
2167 (substring-no-properties (funcall fun start end delete)))
2168 nil t)
2169 (unless org-agenda-keep-modes
2170 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2171 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2172 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2173 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2174 (add-to-invisibility-spec '(org-filtered))
2175 (add-to-invisibility-spec '(org-link))
2176 (easy-menu-change
2177 '("Agenda") "Agenda Files"
2178 (append
2179 (list
2180 (vector
2181 (if (get 'org-agenda-files 'org-restrict)
2182 "Restricted to single file"
2183 "Edit File List")
2184 '(org-edit-agenda-file-list)
2185 (not (get 'org-agenda-files 'org-restrict)))
2186 "--")
2187 (mapcar 'org-file-menu-entry (org-agenda-files))))
2188 (org-agenda-set-mode-name)
2189 (apply
2190 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2191 (list 'org-agenda-mode-hook)))
2193 (substitute-key-definition 'undo 'org-agenda-undo
2194 org-agenda-mode-map global-map)
2195 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2196 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2197 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2198 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2199 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2200 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2201 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2202 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2203 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2204 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2205 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2206 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2207 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2208 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2209 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2210 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2211 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2212 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2213 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2214 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2215 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2216 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2217 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2218 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2219 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2220 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2221 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2222 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2223 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2224 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2225 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2226 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2227 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2228 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2229 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2230 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2231 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2232 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2233 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2234 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2235 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2236 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2237 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2238 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2239 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2240 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2241 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2242 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2243 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2244 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2246 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2247 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2248 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2249 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2250 (while l (org-defkey org-agenda-mode-map
2251 (int-to-string (pop l)) 'digit-argument)))
2253 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2254 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2255 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2256 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2257 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2258 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2259 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2260 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2261 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2262 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2263 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2264 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2265 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2266 'org-clock-modify-effort-estimate)
2267 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2268 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2269 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2270 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2271 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2272 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2273 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2274 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2275 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2276 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2277 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2278 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2279 (substitute-key-definition 'next-line 'org-agenda-next-line
2280 org-agenda-mode-map global-map)
2281 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2282 org-agenda-mode-map global-map)
2283 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2284 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2285 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2286 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2287 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2288 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2289 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2290 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2291 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2292 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2293 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2294 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2295 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2296 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2297 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2298 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2299 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2300 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2301 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2302 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2303 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2304 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2305 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2306 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2307 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2308 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2309 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2310 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2311 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2312 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2314 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2315 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2316 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2317 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2318 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2319 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2320 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2321 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2322 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2323 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2324 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2325 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2326 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2327 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2328 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2330 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2331 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2333 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2334 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2336 (when org-agenda-mouse-1-follows-link
2337 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2338 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2339 '("Agenda"
2340 ("Agenda Files")
2341 "--"
2342 ("Agenda Dates"
2343 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2344 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2345 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2346 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2347 "--"
2348 ("View"
2349 ["Day View" org-agenda-day-view
2350 :active (org-agenda-check-type nil 'agenda)
2351 :style radio :selected (eq org-agenda-current-span 'day)
2352 :keys "v d (or just d)"]
2353 ["Week View" org-agenda-week-view
2354 :active (org-agenda-check-type nil 'agenda)
2355 :style radio :selected (eq org-agenda-current-span 'week)
2356 :keys "v w"]
2357 ["Fortnight View" org-agenda-fortnight-view
2358 :active (org-agenda-check-type nil 'agenda)
2359 :style radio :selected (eq org-agenda-current-span 'fortnight)
2360 :keys "v t"]
2361 ["Month View" org-agenda-month-view
2362 :active (org-agenda-check-type nil 'agenda)
2363 :style radio :selected (eq org-agenda-current-span 'month)
2364 :keys "v m"]
2365 ["Year View" org-agenda-year-view
2366 :active (org-agenda-check-type nil 'agenda)
2367 :style radio :selected (eq org-agenda-current-span 'year)
2368 :keys "v y"]
2369 "--"
2370 ["Include Diary" org-agenda-toggle-diary
2371 :style toggle :selected org-agenda-include-diary
2372 :active (org-agenda-check-type nil 'agenda)]
2373 ["Include Deadlines" org-agenda-toggle-deadlines
2374 :style toggle :selected org-agenda-include-deadlines
2375 :active (org-agenda-check-type nil 'agenda)]
2376 ["Use Time Grid" org-agenda-toggle-time-grid
2377 :style toggle :selected org-agenda-use-time-grid
2378 :active (org-agenda-check-type nil 'agenda)]
2379 "--"
2380 ["Show clock report" org-agenda-clockreport-mode
2381 :style toggle :selected org-agenda-clockreport-mode
2382 :active (org-agenda-check-type nil 'agenda)]
2383 ["Show some entry text" org-agenda-entry-text-mode
2384 :style toggle :selected org-agenda-entry-text-mode
2385 :active t]
2386 "--"
2387 ["Show Logbook entries" org-agenda-log-mode
2388 :style toggle :selected org-agenda-show-log
2389 :active (org-agenda-check-type nil 'agenda 'timeline)
2390 :keys "v l (or just l)"]
2391 ["Include archived trees" org-agenda-archives-mode
2392 :style toggle :selected org-agenda-archives-mode :active t
2393 :keys "v a"]
2394 ["Include archive files" (org-agenda-archives-mode t)
2395 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2396 :keys "v A"]
2397 "--"
2398 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2399 ["Write view to file" org-agenda-write t]
2400 ["Rebuild buffer" org-agenda-redo t]
2401 ["Save all Org buffers" org-save-all-org-buffers t]
2402 "--"
2403 ["Show original entry" org-agenda-show t]
2404 ["Go To (other window)" org-agenda-goto t]
2405 ["Go To (this window)" org-agenda-switch-to t]
2406 ["Capture with cursor date" org-agenda-capture t]
2407 ["Follow Mode" org-agenda-follow-mode
2408 :style toggle :selected org-agenda-follow-mode :active t]
2409 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2410 "--"
2411 ("TODO"
2412 ["Cycle TODO" org-agenda-todo t]
2413 ["Next TODO set" org-agenda-todo-nextset t]
2414 ["Previous TODO set" org-agenda-todo-previousset t]
2415 ["Add note" org-agenda-add-note t])
2416 ("Archive/Refile/Delete"
2417 ["Archive default" org-agenda-archive-default t]
2418 ["Archive default" org-agenda-archive-default-with-confirmation t]
2419 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2420 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2421 ["Archive subtree" org-agenda-archive t]
2422 "--"
2423 ["Refile" org-agenda-refile t]
2424 "--"
2425 ["Delete subtree" org-agenda-kill t])
2426 ("Bulk action"
2427 ["Mark entry" org-agenda-bulk-mark t]
2428 ["Mark all" org-agenda-bulk-mark-all t]
2429 ["Unmark entry" org-agenda-bulk-unmark t]
2430 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2431 ["Toggle mark" org-agenda-bulk-toggle t]
2432 ["Toggle all" org-agenda-bulk-toggle-all t]
2433 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2434 ["Act on all marked" org-agenda-bulk-action t]
2435 "--"
2436 ("Tags and Properties"
2437 ["Show all Tags" org-agenda-show-tags t]
2438 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2439 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2440 "--"
2441 ["Column View" org-columns t])
2442 ("Deadline/Schedule"
2443 ["Schedule" org-agenda-schedule t]
2444 ["Set Deadline" org-agenda-deadline t]
2445 "--"
2446 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2447 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2448 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2449 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2450 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2451 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2452 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2453 ("Clock and Effort"
2454 ["Clock in" org-agenda-clock-in t]
2455 ["Clock out" org-agenda-clock-out t]
2456 ["Clock cancel" org-agenda-clock-cancel t]
2457 ["Goto running clock" org-clock-goto t]
2458 "--"
2459 ["Set Effort" org-agenda-set-effort t]
2460 ["Change clocked effort" org-clock-modify-effort-estimate
2461 (org-clock-is-active)])
2462 ("Priority"
2463 ["Set Priority" org-agenda-priority t]
2464 ["Increase Priority" org-agenda-priority-up t]
2465 ["Decrease Priority" org-agenda-priority-down t]
2466 ["Show Priority" org-show-priority t])
2467 ("Calendar/Diary"
2468 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2469 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2470 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2471 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2472 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2473 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2474 "--"
2475 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2476 "--"
2477 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2478 "--"
2479 ("MobileOrg"
2480 ["Push Files and Views" org-mobile-push t]
2481 ["Get Captured and Flagged" org-mobile-pull t]
2482 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2483 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2484 "--"
2485 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2486 "--"
2487 ["Quit" org-agenda-quit t]
2488 ["Exit and Release Buffers" org-agenda-exit t]
2491 ;;; Agenda undo
2493 (defvar org-agenda-allow-remote-undo t
2494 "Non-nil means allow remote undo from the agenda buffer.")
2495 (defvar org-agenda-undo-has-started-in nil
2496 "Buffers that have already seen `undo-start' in the current undo sequence.")
2498 (defun org-agenda-undo ()
2499 "Undo a remote editing step in the agenda.
2500 This undoes changes both in the agenda buffer and in the remote buffer
2501 that have been changed along."
2502 (interactive)
2503 (or org-agenda-allow-remote-undo
2504 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2505 (if (not (eq this-command last-command))
2506 (setq org-agenda-undo-has-started-in nil
2507 org-agenda-pending-undo-list org-agenda-undo-list))
2508 (if (not org-agenda-pending-undo-list)
2509 (user-error "No further undo information"))
2510 (let* ((entry (pop org-agenda-pending-undo-list))
2511 buf line cmd rembuf)
2512 (setq cmd (pop entry) line (pop entry))
2513 (setq rembuf (nth 2 entry))
2514 (org-with-remote-undo rembuf
2515 (while (bufferp (setq buf (pop entry)))
2516 (if (pop entry)
2517 (with-current-buffer buf
2518 (let ((last-undo-buffer buf)
2519 (inhibit-read-only t))
2520 (unless (memq buf org-agenda-undo-has-started-in)
2521 (push buf org-agenda-undo-has-started-in)
2522 (make-local-variable 'pending-undo-list)
2523 (undo-start))
2524 (while (and pending-undo-list
2525 (listp pending-undo-list)
2526 (not (car pending-undo-list)))
2527 (pop pending-undo-list))
2528 (undo-more 1))))))
2529 (org-goto-line line)
2530 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2532 (defun org-verify-change-for-undo (l1 l2)
2533 "Verify that a real change occurred between the undo lists L1 and L2."
2534 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2535 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2536 (not (eq l1 l2)))
2538 ;;; Agenda dispatch
2540 (defvar org-agenda-restrict-begin (make-marker))
2541 (defvar org-agenda-restrict-end (make-marker))
2542 (defvar org-agenda-last-dispatch-buffer nil)
2543 (defvar org-agenda-overriding-restriction nil)
2545 (defcustom org-agenda-custom-commands-contexts nil
2546 "Alist of custom agenda keys and contextual rules.
2548 For example, if you have a custom agenda command \"p\" and you
2549 want this command to be accessible only from plain text files,
2550 use this:
2552 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2554 Here are the available contexts definitions:
2556 in-file: command displayed only in matching files
2557 in-mode: command displayed only in matching modes
2558 not-in-file: command not displayed in matching files
2559 not-in-mode: command not displayed in matching modes
2560 in-buffer: command displayed only in matching buffers
2561 not-in-buffer: command not displayed in matching buffers
2562 [function]: a custom function taking no argument
2564 If you define several checks, the agenda command will be
2565 accessible if there is at least one valid check.
2567 You can also bind a key to another agenda custom command
2568 depending on contextual rules.
2570 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2572 Here it means: in .txt files, use \"p\" as the key for the
2573 agenda command otherwise associated with \"q\". (The command
2574 originally associated with \"q\" is not displayed to avoid
2575 duplicates.)"
2576 :version "24.3"
2577 :group 'org-agenda-custom-commands
2578 :type '(repeat (list :tag "Rule"
2579 (string :tag " Agenda key")
2580 (string :tag "Replace by command")
2581 (repeat :tag "Available when"
2582 (choice
2583 (cons :tag "Condition"
2584 (choice
2585 (const :tag "In file" in-file)
2586 (const :tag "Not in file" not-in-file)
2587 (const :tag "In buffer" in-buffer)
2588 (const :tag "Not in buffer" not-in-buffer)
2589 (const :tag "In mode" in-mode)
2590 (const :tag "Not in mode" not-in-mode))
2591 (regexp))
2592 (function :tag "Custom function"))))))
2594 (defcustom org-agenda-max-entries nil
2595 "Maximum number of entries to display in an agenda.
2596 This can be nil (no limit) or an integer or an alist of agenda
2597 types with an associated number of entries to display in this
2598 type."
2599 :version "24.4"
2600 :package-version '(Org . "8.0")
2601 :group 'org-agenda-custom-commands
2602 :type '(choice (symbol :tag "No limit" nil)
2603 (integer :tag "Max number of entries")
2604 (repeat
2605 (cons (choice :tag "Agenda type"
2606 (const agenda)
2607 (const todo)
2608 (const tags)
2609 (const search)
2610 (const timeline))
2611 (integer :tag "Max number of entries")))))
2613 (defcustom org-agenda-max-todos nil
2614 "Maximum number of TODOs to display in an agenda.
2615 This can be nil (no limit) or an integer or an alist of agenda
2616 types with an associated number of entries to display in this
2617 type."
2618 :version "24.4"
2619 :package-version '(Org . "8.0")
2620 :group 'org-agenda-custom-commands
2621 :type '(choice (symbol :tag "No limit" nil)
2622 (integer :tag "Max number of TODOs")
2623 (repeat
2624 (cons (choice :tag "Agenda type"
2625 (const agenda)
2626 (const todo)
2627 (const tags)
2628 (const search)
2629 (const timeline))
2630 (integer :tag "Max number of TODOs")))))
2632 (defcustom org-agenda-max-tags nil
2633 "Maximum number of tagged entries to display in an agenda.
2634 This can be nil (no limit) or an integer or an alist of agenda
2635 types with an associated number of entries to display in this
2636 type."
2637 :version "24.4"
2638 :package-version '(Org . "8.0")
2639 :group 'org-agenda-custom-commands
2640 :type '(choice (symbol :tag "No limit" nil)
2641 (integer :tag "Max number of tagged entries")
2642 (repeat
2643 (cons (choice :tag "Agenda type"
2644 (const agenda)
2645 (const todo)
2646 (const tags)
2647 (const search)
2648 (const timeline))
2649 (integer :tag "Max number of tagged entries")))))
2651 (defcustom org-agenda-max-effort nil
2652 "Maximum cumulated effort duration for the agenda.
2653 This can be nil (no limit) or a number of minutes (as an integer)
2654 or an alist of agenda types with an associated number of minutes
2655 to limit entries to in this type."
2656 :version "24.4"
2657 :package-version '(Org . "8.0")
2658 :group 'org-agenda-custom-commands
2659 :type '(choice (symbol :tag "No limit" nil)
2660 (integer :tag "Max number of minutes")
2661 (repeat
2662 (cons (choice :tag "Agenda type"
2663 (const agenda)
2664 (const todo)
2665 (const tags)
2666 (const search)
2667 (const timeline))
2668 (integer :tag "Max number of minutes")))))
2670 (defvar org-agenda-keep-restricted-file-list nil)
2671 (defvar org-keys nil)
2672 (defvar org-match nil)
2673 ;;;###autoload
2674 (defun org-agenda (&optional arg org-keys restriction)
2675 "Dispatch agenda commands to collect entries to the agenda buffer.
2676 Prompts for a command to execute. Any prefix arg will be passed
2677 on to the selected command. The default selections are:
2679 a Call `org-agenda-list' to display the agenda for current day or week.
2680 t Call `org-todo-list' to display the global todo list.
2681 T Call `org-todo-list' to display the global todo list, select only
2682 entries with a specific TODO keyword (the user gets a prompt).
2683 m Call `org-tags-view' to display headlines with tags matching
2684 a condition (the user is prompted for the condition).
2685 M Like `m', but select only TODO entries, no ordinary headlines.
2686 L Create a timeline for the current buffer.
2687 e Export views to associated files.
2688 s Search entries for keywords.
2689 S Search entries for keywords, only with TODO keywords.
2690 / Multi occur across all agenda files and also files listed
2691 in `org-agenda-text-search-extra-files'.
2692 < Restrict agenda commands to buffer, subtree, or region.
2693 Press several times to get the desired effect.
2694 > Remove a previous restriction.
2695 # List \"stuck\" projects.
2696 ! Configure what \"stuck\" means.
2697 C Configure custom agenda commands.
2699 More commands can be added by configuring the variable
2700 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2701 searches can be pre-defined in this way.
2703 If the current buffer is in Org mode and visiting a file, you can also
2704 first press `<' once to indicate that the agenda should be temporarily
2705 \(until the next use of `\\[org-agenda]') restricted to the current file.
2706 Pressing `<' twice means to restrict to the current subtree or region
2707 \(if active)."
2708 (interactive "P")
2709 (catch 'exit
2710 (let* ((prefix-descriptions nil)
2711 (org-agenda-buffer-name org-agenda-buffer-name)
2712 (org-agenda-window-setup (if (equal (buffer-name)
2713 org-agenda-buffer-name)
2714 'current-window
2715 org-agenda-window-setup))
2716 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2717 (org-agenda-custom-commands
2718 ;; normalize different versions
2719 (delq nil
2720 (mapcar
2721 (lambda (x)
2722 (cond ((stringp (cdr x))
2723 (push x prefix-descriptions)
2724 nil)
2725 ((stringp (nth 1 x)) x)
2726 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2727 (t (cons (car x) (cons "" (cdr x))))))
2728 org-agenda-custom-commands)))
2729 (org-agenda-custom-commands
2730 (org-contextualize-keys
2731 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2732 (buf (current-buffer))
2733 (bfn (buffer-file-name (buffer-base-buffer)))
2734 entry key type org-match lprops ans)
2735 ;; Turn off restriction unless there is an overriding one,
2736 (unless org-agenda-overriding-restriction
2737 (unless org-agenda-keep-restricted-file-list
2738 ;; There is a request to keep the file list in place
2739 (put 'org-agenda-files 'org-restrict nil))
2740 (setq org-agenda-restrict nil)
2741 (move-marker org-agenda-restrict-begin nil)
2742 (move-marker org-agenda-restrict-end nil))
2743 ;; Delete old local properties
2744 (put 'org-agenda-redo-command 'org-lprops nil)
2745 ;; Delete previously set last-arguments
2746 (put 'org-agenda-redo-command 'last-args nil)
2747 ;; Remember where this call originated
2748 (setq org-agenda-last-dispatch-buffer (current-buffer))
2749 (unless org-keys
2750 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2751 org-keys (car ans)
2752 restriction (cdr ans)))
2753 ;; If we have sticky agenda buffers, set a name for the buffer,
2754 ;; depending on the invoking keys. The user may still set this
2755 ;; as a command option, which will overwrite what we do here.
2756 (if org-agenda-sticky
2757 (setq org-agenda-buffer-name
2758 (format "*Org Agenda(%s)*" org-keys)))
2759 ;; Establish the restriction, if any
2760 (when (and (not org-agenda-overriding-restriction) restriction)
2761 (put 'org-agenda-files 'org-restrict (list bfn))
2762 (cond
2763 ((eq restriction 'region)
2764 (setq org-agenda-restrict (current-buffer))
2765 (move-marker org-agenda-restrict-begin (region-beginning))
2766 (move-marker org-agenda-restrict-end (region-end)))
2767 ((eq restriction 'subtree)
2768 (save-excursion
2769 (setq org-agenda-restrict (current-buffer))
2770 (org-back-to-heading t)
2771 (move-marker org-agenda-restrict-begin (point))
2772 (move-marker org-agenda-restrict-end
2773 (progn (org-end-of-subtree t)))))))
2775 ;; For example the todo list should not need it (but does...)
2776 (cond
2777 ((setq entry (assoc org-keys org-agenda-custom-commands))
2778 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2779 (progn
2780 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2781 lprops (nth 4 entry))
2782 (if org-agenda-sticky
2783 (setq org-agenda-buffer-name
2784 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2785 (format "*Org Agenda(%s)*" org-keys))))
2786 (put 'org-agenda-redo-command 'org-lprops lprops)
2787 (cond
2788 ((eq type 'agenda)
2789 (org-let lprops '(org-agenda-list current-prefix-arg)))
2790 ((eq type 'agenda*)
2791 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2792 ((eq type 'alltodo)
2793 (org-let lprops '(org-todo-list current-prefix-arg)))
2794 ((eq type 'search)
2795 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2796 ((eq type 'stuck)
2797 (org-let lprops '(org-agenda-list-stuck-projects
2798 current-prefix-arg)))
2799 ((eq type 'tags)
2800 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2801 ((eq type 'tags-todo)
2802 (org-let lprops '(org-tags-view '(4) org-match)))
2803 ((eq type 'todo)
2804 (org-let lprops '(org-todo-list org-match)))
2805 ((eq type 'tags-tree)
2806 (org-check-for-org-mode)
2807 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2808 ((eq type 'todo-tree)
2809 (org-check-for-org-mode)
2810 (org-let lprops
2811 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2812 (regexp-quote org-match) "\\>"))))
2813 ((eq type 'occur-tree)
2814 (org-check-for-org-mode)
2815 (org-let lprops '(org-occur org-match)))
2816 ((functionp type)
2817 (org-let lprops '(funcall type org-match)))
2818 ((fboundp type)
2819 (org-let lprops '(funcall type org-match)))
2820 (t (user-error "Invalid custom agenda command type %s" type))))
2821 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2822 ((equal org-keys "C")
2823 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2824 (customize-variable 'org-agenda-custom-commands))
2825 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2826 ((equal org-keys "s") (call-interactively 'org-search-view))
2827 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2828 ((equal org-keys "t") (call-interactively 'org-todo-list))
2829 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2830 ((equal org-keys "m") (call-interactively 'org-tags-view))
2831 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2832 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2833 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2834 (add-hook
2835 'post-command-hook
2836 (lambda ()
2837 (unless (current-message)
2838 (let* ((m (org-agenda-get-any-marker))
2839 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2840 (when note
2841 (message (concat
2842 "FLAGGING-NOTE ([?] for more info): "
2843 (org-add-props
2844 (replace-regexp-in-string
2845 "\\\\n" "//"
2846 (copy-sequence note))
2847 nil 'face 'org-warning)))))))
2848 t t))
2849 ((equal org-keys "L")
2850 (unless (derived-mode-p 'org-mode)
2851 (user-error "This is not an Org file"))
2852 (unless restriction
2853 (put 'org-agenda-files 'org-restrict (list bfn))
2854 (org-call-with-arg 'org-timeline arg)))
2855 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2856 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2857 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2858 (t (user-error "Invalid agenda key"))))))
2860 (defvar org-agenda-multi)
2862 (defun org-agenda-append-agenda ()
2863 "Append another agenda view to the current one.
2864 This function allows interactive building of block agendas.
2865 Agenda views are separated by `org-agenda-block-separator'."
2866 (interactive)
2867 (unless (derived-mode-p 'org-agenda-mode)
2868 (user-error "Can only append from within agenda buffer"))
2869 (let ((org-agenda-multi t))
2870 (org-agenda)
2871 (widen)
2872 (org-agenda-finalize)
2873 (setq buffer-read-only t)
2874 (org-agenda-fit-window-to-buffer)))
2876 (defun org-agenda-normalize-custom-commands (cmds)
2877 "Normalize custom commands CMDS."
2878 (delq nil
2879 (mapcar
2880 (lambda (x)
2881 (cond ((stringp (cdr x)) nil)
2882 ((stringp (nth 1 x)) x)
2883 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2884 (t (cons (car x) (cons "" (cdr x))))))
2885 cmds)))
2887 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2888 "The user interface for selecting an agenda command."
2889 (catch 'exit
2890 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2891 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2892 (region-p (org-region-active-p))
2893 (custom org-agenda-custom-commands)
2894 (selstring "")
2895 restriction second-time
2896 c entry key type match prefixes rmheader header-end custom1 desc
2897 line lines left right n n1)
2898 (save-window-excursion
2899 (delete-other-windows)
2900 (org-switch-to-buffer-other-window " *Agenda Commands*")
2901 (erase-buffer)
2902 (insert (eval-when-compile
2903 (let ((header
2904 "Press key for an agenda command: < Buffer, subtree/region restriction
2905 -------------------------------- > Remove restriction
2906 a Agenda for current week or day e Export agenda views
2907 t List of all TODO entries T Entries with special TODO kwd
2908 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2909 s Search for keywords S Like s, but only TODO entries
2910 L Timeline for current buffer # List stuck projects (!=configure)
2911 / Multi-occur C Configure custom agenda commands
2912 ? Find :FLAGGED: entries * Toggle sticky agenda views
2914 (start 0))
2915 (while (string-match
2916 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2917 header start)
2918 (setq start (match-end 0))
2919 (add-text-properties (match-beginning 2) (match-end 2)
2920 '(face bold) header))
2921 header)))
2922 (setq header-end (point-marker))
2923 (while t
2924 (setq custom1 custom)
2925 (when (eq rmheader t)
2926 (org-goto-line 1)
2927 (re-search-forward ":" nil t)
2928 (delete-region (match-end 0) (point-at-eol))
2929 (forward-char 1)
2930 (looking-at "-+")
2931 (delete-region (match-end 0) (point-at-eol))
2932 (move-marker header-end (match-end 0)))
2933 (goto-char header-end)
2934 (delete-region (point) (point-max))
2936 ;; Produce all the lines that describe custom commands and prefixes
2937 (setq lines nil)
2938 (while (setq entry (pop custom1))
2939 (setq key (car entry) desc (nth 1 entry)
2940 type (nth 2 entry)
2941 match (nth 3 entry))
2942 (if (> (length key) 1)
2943 (cl-pushnew (string-to-char key) prefixes :test #'equal)
2944 (setq line
2945 (format
2946 "%-4s%-14s"
2947 (org-add-props (copy-sequence key)
2948 '(face bold))
2949 (cond
2950 ((string-match "\\S-" desc) desc)
2951 ((eq type 'agenda) "Agenda for current week or day")
2952 ((eq type 'agenda*) "Appointments for current week or day")
2953 ((eq type 'alltodo) "List of all TODO entries")
2954 ((eq type 'search) "Word search")
2955 ((eq type 'stuck) "List of stuck projects")
2956 ((eq type 'todo) "TODO keyword")
2957 ((eq type 'tags) "Tags query")
2958 ((eq type 'tags-todo) "Tags (TODO)")
2959 ((eq type 'tags-tree) "Tags tree")
2960 ((eq type 'todo-tree) "TODO kwd tree")
2961 ((eq type 'occur-tree) "Occur tree")
2962 ((functionp type) (if (symbolp type)
2963 (symbol-name type)
2964 "Lambda expression"))
2965 (t "???"))))
2966 (if org-agenda-menu-show-matcher
2967 (setq line
2968 (concat line ": "
2969 (cond
2970 ((stringp match)
2971 (setq match (copy-sequence match))
2972 (org-add-props match nil 'face 'org-warning))
2973 ((listp type)
2974 (format "set of %d commands" (length type))))))
2975 (if (org-string-nw-p match)
2976 (add-text-properties
2977 0 (length line) (list 'help-echo
2978 (concat "Matcher: " match)) line)))
2979 (push line lines)))
2980 (setq lines (nreverse lines))
2981 (when prefixes
2982 (mapc (lambda (x)
2983 (push
2984 (format "%s %s"
2985 (org-add-props (char-to-string x)
2986 nil 'face 'bold)
2987 (or (cdr (assoc (concat selstring
2988 (char-to-string x))
2989 prefix-descriptions))
2990 "Prefix key"))
2991 lines))
2992 prefixes))
2994 ;; Check if we should display in two columns
2995 (if org-agenda-menu-two-columns
2996 (progn
2997 (setq n (length lines)
2998 n1 (+ (/ n 2) (mod n 2))
2999 right (nthcdr n1 lines)
3000 left (copy-sequence lines))
3001 (setcdr (nthcdr (1- n1) left) nil))
3002 (setq left lines right nil))
3003 (while left
3004 (insert "\n" (pop left))
3005 (when right
3006 (if (< (current-column) 40)
3007 (move-to-column 40 t)
3008 (insert " "))
3009 (insert (pop right))))
3011 ;; Make the window the right size
3012 (goto-char (point-min))
3013 (if second-time
3014 (if (not (pos-visible-in-window-p (point-max)))
3015 (org-fit-window-to-buffer))
3016 (setq second-time t)
3017 (org-fit-window-to-buffer))
3019 ;; Ask for selection
3020 (message "Press key for agenda command%s:"
3021 (if (or restrict-ok org-agenda-overriding-restriction)
3022 (if org-agenda-overriding-restriction
3023 " (restriction lock active)"
3024 (if restriction
3025 (format " (restricted to %s)" restriction)
3026 " (unrestricted)"))
3027 ""))
3028 (setq c (read-char-exclusive))
3029 (message "")
3030 (cond
3031 ((assoc (char-to-string c) custom)
3032 (setq selstring (concat selstring (char-to-string c)))
3033 (throw 'exit (cons selstring restriction)))
3034 ((memq c prefixes)
3035 (setq selstring (concat selstring (char-to-string c))
3036 prefixes nil
3037 rmheader (or rmheader t)
3038 custom (delq nil (mapcar
3039 (lambda (x)
3040 (if (or (= (length (car x)) 1)
3041 (/= (string-to-char (car x)) c))
3043 (cons (substring (car x) 1) (cdr x))))
3044 custom))))
3045 ((eq c ?*)
3046 (call-interactively 'org-toggle-sticky-agenda)
3047 (sit-for 2))
3048 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3049 (message "Restriction is only possible in Org buffers")
3050 (ding) (sit-for 1))
3051 ((eq c ?1)
3052 (org-agenda-remove-restriction-lock 'noupdate)
3053 (setq restriction 'buffer))
3054 ((eq c ?0)
3055 (org-agenda-remove-restriction-lock 'noupdate)
3056 (setq restriction (if region-p 'region 'subtree)))
3057 ((eq c ?<)
3058 (org-agenda-remove-restriction-lock 'noupdate)
3059 (setq restriction
3060 (cond
3061 ((eq restriction 'buffer)
3062 (if region-p 'region 'subtree))
3063 ((memq restriction '(subtree region))
3064 nil)
3065 (t 'buffer))))
3066 ((eq c ?>)
3067 (org-agenda-remove-restriction-lock 'noupdate)
3068 (setq restriction nil))
3069 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3070 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3071 ((and (> (length selstring) 0) (eq c ?\d))
3072 (delete-window)
3073 (org-agenda-get-restriction-and-command prefix-descriptions))
3075 ((equal c ?q) (error "Abort"))
3076 (t (user-error "Invalid key %c" c))))))))
3078 (defun org-agenda-fit-window-to-buffer ()
3079 "Fit the window to the buffer size."
3080 (and (memq org-agenda-window-setup '(reorganize-frame))
3081 (fboundp 'fit-window-to-buffer)
3082 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3083 (= (car org-agenda-window-frame-fractions) 1.0))
3084 (delete-other-windows)
3085 (org-fit-window-to-buffer
3087 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3088 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3090 (defvar org-cmd nil)
3091 (defvar org-agenda-overriding-cmd nil)
3092 (defvar org-agenda-overriding-arguments nil)
3093 (defvar org-agenda-overriding-cmd-arguments nil)
3094 (defun org-agenda-run-series (name series)
3095 "Run agenda NAME as a SERIES of agenda commands."
3096 (org-let (nth 1 series) '(org-agenda-prepare name))
3097 ;; We need to reset agenda markers here, because when constructing a
3098 ;; block agenda, the individual blocks do not do that.
3099 (org-agenda-reset-markers)
3100 (let* ((org-agenda-multi t)
3101 (redo (list 'org-agenda-run-series name (list 'quote series)))
3102 (cmds (car series))
3103 (gprops (nth 1 series))
3104 match ;; The byte compiler incorrectly complains about this. Keep it!
3105 org-cmd type lprops)
3106 (while (setq org-cmd (pop cmds))
3107 (setq type (car org-cmd))
3108 (setq match (eval (nth 1 org-cmd)))
3109 (setq lprops (nth 2 org-cmd))
3110 (let ((org-agenda-overriding-arguments
3111 (if (eq org-agenda-overriding-cmd org-cmd)
3112 (or org-agenda-overriding-arguments
3113 org-agenda-overriding-cmd-arguments))))
3114 (cond
3115 ((eq type 'agenda)
3116 (org-let2 gprops lprops
3117 '(call-interactively 'org-agenda-list)))
3118 ((eq type 'agenda*)
3119 (org-let2 gprops lprops
3120 '(funcall 'org-agenda-list nil nil t)))
3121 ((eq type 'alltodo)
3122 (org-let2 gprops lprops
3123 '(call-interactively 'org-todo-list)))
3124 ((eq type 'search)
3125 (org-let2 gprops lprops
3126 '(org-search-view current-prefix-arg match nil)))
3127 ((eq type 'stuck)
3128 (org-let2 gprops lprops
3129 '(call-interactively 'org-agenda-list-stuck-projects)))
3130 ((eq type 'tags)
3131 (org-let2 gprops lprops
3132 '(org-tags-view current-prefix-arg match)))
3133 ((eq type 'tags-todo)
3134 (org-let2 gprops lprops
3135 '(org-tags-view '(4) match)))
3136 ((eq type 'todo)
3137 (org-let2 gprops lprops
3138 '(org-todo-list match)))
3139 ((fboundp type)
3140 (org-let2 gprops lprops
3141 '(funcall type match)))
3142 (t (error "Invalid type in command series")))))
3143 (widen)
3144 (let ((inhibit-read-only t))
3145 (add-text-properties (point-min) (point-max)
3146 `(org-series t org-series-redo-cmd ,redo)))
3147 (setq org-agenda-redo-command redo)
3148 (goto-char (point-min)))
3149 (org-agenda-fit-window-to-buffer)
3150 (org-let (nth 1 series) '(org-agenda-finalize)))
3152 ;;;###autoload
3153 (defmacro org-batch-agenda (cmd-key &rest parameters)
3154 "Run an agenda command in batch mode and send the result to STDOUT.
3155 If CMD-KEY is a string of length 1, it is used as a key in
3156 `org-agenda-custom-commands' and triggers this command. If it is a
3157 longer string it is used as a tags/todo match string.
3158 Parameters are alternating variable names and values that will be bound
3159 before running the agenda command."
3160 (org-eval-in-environment (org-make-parameter-alist parameters)
3161 (let (org-agenda-sticky)
3162 (if (> (length cmd-key) 1)
3163 (org-tags-view nil cmd-key)
3164 (org-agenda nil cmd-key))))
3165 (set-buffer org-agenda-buffer-name)
3166 (princ (buffer-string)))
3168 (defvar org-agenda-info nil)
3170 ;;;###autoload
3171 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3172 "Run an agenda command in batch mode and send the result to STDOUT.
3173 If CMD-KEY is a string of length 1, it is used as a key in
3174 `org-agenda-custom-commands' and triggers this command. If it is a
3175 longer string it is used as a tags/todo match string.
3176 Parameters are alternating variable names and values that will be bound
3177 before running the agenda command.
3179 The output gives a line for each selected agenda item. Each
3180 item is a list of comma-separated values, like this:
3182 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3184 category The category of the item
3185 head The headline, without TODO kwd, TAGS and PRIORITY
3186 type The type of the agenda entry, can be
3187 todo selected in TODO match
3188 tagsmatch selected in tags match
3189 diary imported from diary
3190 deadline a deadline on given date
3191 scheduled scheduled on given date
3192 timestamp entry has timestamp on given date
3193 closed entry was closed on given date
3194 upcoming-deadline warning about deadline
3195 past-scheduled forwarded scheduled item
3196 block entry has date block including g. date
3197 todo The todo keyword, if any
3198 tags All tags including inherited ones, separated by colons
3199 date The relevant date, like 2007-2-14
3200 time The time, like 15:00-16:50
3201 extra Sting with extra planning info
3202 priority-l The priority letter if any was given
3203 priority-n The computed numerical priority
3204 agenda-day The day in the agenda where this is listed"
3205 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3206 (org-make-parameter-alist parameters))
3207 (if (> (length cmd-key) 2)
3208 (org-tags-view nil cmd-key)
3209 (org-agenda nil cmd-key)))
3210 (set-buffer org-agenda-buffer-name)
3211 (let* ((lines (org-split-string (buffer-string) "\n"))
3212 line)
3213 (while (setq line (pop lines))
3214 (catch 'next
3215 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3216 (setq org-agenda-info
3217 (org-fix-agenda-info (text-properties-at 0 line)))
3218 (princ
3219 (mapconcat 'org-agenda-export-csv-mapper
3220 '(org-category txt type todo tags date time extra
3221 priority-letter priority agenda-day)
3222 ","))
3223 (princ "\n")))))
3225 (defun org-fix-agenda-info (props)
3226 "Make sure all properties on an agenda item have a canonical form.
3227 This ensures the export commands can easily use it."
3228 (let (tmp re)
3229 (when (setq tmp (plist-get props 'tags))
3230 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3231 (when (setq tmp (plist-get props 'date))
3232 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3233 (let ((calendar-date-display-form '(year "-" month "-" day)))
3234 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3236 (setq tmp (calendar-date-string tmp)))
3237 (setq props (plist-put props 'date tmp)))
3238 (when (setq tmp (plist-get props 'day))
3239 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3240 (let ((calendar-date-display-form '(year "-" month "-" day)))
3241 (setq tmp (calendar-date-string tmp)))
3242 (setq props (plist-put props 'day tmp))
3243 (setq props (plist-put props 'agenda-day tmp)))
3244 (when (setq tmp (plist-get props 'txt))
3245 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3246 (plist-put props 'priority-letter (match-string 1 tmp))
3247 (setq tmp (replace-match "" t t tmp)))
3248 (when (and (setq re (plist-get props 'org-todo-regexp))
3249 (setq re (concat "\\`\\.*" re " ?"))
3250 (let ((case-fold-search nil)) (string-match re tmp)))
3251 (plist-put props 'todo (match-string 1 tmp))
3252 (setq tmp (replace-match "" t t tmp)))
3253 (plist-put props 'txt tmp)))
3254 props)
3256 (defun org-agenda-export-csv-mapper (prop)
3257 (let ((res (plist-get org-agenda-info prop)))
3258 (setq res
3259 (cond
3260 ((not res) "")
3261 ((stringp res) res)
3262 (t (prin1-to-string res))))
3263 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3265 ;;;###autoload
3266 (defun org-store-agenda-views (&rest parameters)
3267 "Store agenda views."
3268 (interactive)
3269 (eval (list 'org-batch-store-agenda-views)))
3271 ;;;###autoload
3272 (defmacro org-batch-store-agenda-views (&rest parameters)
3273 "Run all custom agenda commands that have a file argument."
3274 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3275 (pop-up-frames nil)
3276 (dir default-directory)
3277 (pars (org-make-parameter-alist parameters))
3278 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3279 (save-window-excursion
3280 (while cmds
3281 (setq cmd (pop cmds)
3282 thiscmdkey (car cmd)
3283 thiscmdcmd (cdr cmd)
3284 match (nth 2 thiscmdcmd)
3285 bufname (if org-agenda-sticky
3286 (or (and (stringp match)
3287 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3288 (format "*Org Agenda(%s)*" thiscmdkey))
3289 org-agenda-buffer-name)
3290 cmd-or-set (nth 2 cmd)
3291 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3292 files (nth (if (listp cmd-or-set) 4 5) cmd))
3293 (if (stringp files) (setq files (list files)))
3294 (when files
3295 (org-eval-in-environment (append org-agenda-exporter-settings
3296 opts pars)
3297 (org-agenda nil thiscmdkey))
3298 (set-buffer bufname)
3299 (while files
3300 (org-eval-in-environment (append org-agenda-exporter-settings
3301 opts pars)
3302 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3303 (and (get-buffer bufname)
3304 (kill-buffer bufname)))))))
3306 (defvar org-agenda-current-span nil
3307 "The current span used in the agenda view.") ; local variable in the agenda buffer
3308 (defun org-agenda-mark-header-line (pos)
3309 "Mark the line at POS as an agenda structure header."
3310 (save-excursion
3311 (goto-char pos)
3312 (put-text-property (point-at-bol) (point-at-eol)
3313 'org-agenda-structural-header t)
3314 (when org-agenda-title-append
3315 (put-text-property (point-at-bol) (point-at-eol)
3316 'org-agenda-title-append org-agenda-title-append))))
3318 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3319 (defvar org-agenda-write-buffer-name "Agenda View")
3320 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3321 "Write the current buffer (an agenda view) as a file.
3323 Depending on the extension of the file name, plain text (.txt),
3324 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3325 If the extension is .ics, translate visible agenda into iCalendar
3326 format. If the extension is .org, collect all subtrees
3327 corresponding to the agenda entries and add them in an .org file.
3329 With prefix argument OPEN, open the new file immediately. If
3330 NOSETTINGS is given, do not scope the settings of
3331 `org-agenda-exporter-settings' into the export commands. This is
3332 used when the settings have already been scoped and we do not
3333 wish to overrule other, higher priority settings. If
3334 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3335 the agenda to write."
3336 (interactive "FWrite agenda to file: \nP")
3337 (if (or (not (file-writable-p file))
3338 (and (file-exists-p file)
3339 (if (called-interactively-p 'any)
3340 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3341 (user-error "Cannot write agenda to file %s" file))
3342 (org-let (if nosettings nil org-agenda-exporter-settings)
3343 '(save-excursion
3344 (save-window-excursion
3345 (let ((bs (copy-sequence (buffer-string)))
3346 (extension (file-name-extension file))
3347 beg content)
3348 (with-temp-buffer
3349 (rename-buffer org-agenda-write-buffer-name t)
3350 (set-buffer-modified-p nil)
3351 (insert bs)
3352 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3353 (run-hooks 'org-agenda-before-write-hook)
3354 (cond
3355 ((bound-and-true-p org-mobile-creating-agendas)
3356 (org-mobile-write-agenda-for-mobile file))
3357 ((string= "org" extension)
3358 (let (content p m message-log-max)
3359 (goto-char (point-min))
3360 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3361 (goto-char p)
3362 (setq m (get-text-property (point) 'org-hd-marker))
3363 (when m
3364 (push (save-excursion
3365 (set-buffer (marker-buffer m))
3366 (goto-char m)
3367 (org-copy-subtree 1 nil t t)
3368 org-subtree-clip)
3369 content)))
3370 (find-file file)
3371 (erase-buffer)
3372 (dolist (s content) (org-paste-subtree 1 s))
3373 (write-file file)
3374 (kill-buffer (current-buffer))
3375 (message "Org file written to %s" file)))
3376 ((member extension '("html" "htm"))
3377 (require 'htmlize)
3378 (set-buffer (htmlize-buffer (current-buffer)))
3379 (when org-agenda-export-html-style
3380 ;; replace <style> section with org-agenda-export-html-style
3381 (goto-char (point-min))
3382 (kill-region (- (search-forward "<style") 6)
3383 (search-forward "</style>"))
3384 (insert org-agenda-export-html-style))
3385 (write-file file)
3386 (kill-buffer (current-buffer))
3387 (message "HTML written to %s" file))
3388 ((string= "ps" extension)
3389 (require 'ps-print)
3390 (ps-print-buffer-with-faces file)
3391 (message "Postscript written to %s" file))
3392 ((string= "pdf" extension)
3393 (require 'ps-print)
3394 (ps-print-buffer-with-faces
3395 (concat (file-name-sans-extension file) ".ps"))
3396 (call-process "ps2pdf" nil nil nil
3397 (expand-file-name
3398 (concat (file-name-sans-extension file) ".ps"))
3399 (expand-file-name file))
3400 (delete-file (concat (file-name-sans-extension file) ".ps"))
3401 (message "PDF written to %s" file))
3402 ((string= "ics" extension)
3403 (require 'ox-icalendar)
3404 (org-icalendar-export-current-agenda (expand-file-name file)))
3406 (let ((bs (buffer-string)))
3407 (find-file file)
3408 (erase-buffer)
3409 (insert bs)
3410 (save-buffer 0)
3411 (kill-buffer (current-buffer))
3412 (message "Plain text written to %s" file))))))))
3413 (set-buffer (or agenda-bufname
3414 (and (called-interactively-p 'any) (buffer-name))
3415 org-agenda-buffer-name)))
3416 (when open (org-open-file file)))
3418 (defun org-agenda-remove-marked-text (property &optional value)
3419 "Delete all text marked with VALUE of PROPERTY.
3420 VALUE defaults to t."
3421 (let (beg)
3422 (setq value (or value t))
3423 (while (setq beg (text-property-any (point-min) (point-max)
3424 property value))
3425 (delete-region
3426 beg (or (next-single-property-change beg property)
3427 (point-max))))))
3429 (defun org-agenda-add-entry-text ()
3430 "Add entry text to agenda lines.
3431 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3432 entry text following headings shown in the agenda.
3433 Drawers will be excluded, also the line with scheduling/deadline info."
3434 (when (and (> org-agenda-add-entry-text-maxlines 0)
3435 (not (bound-and-true-p org-mobile-creating-agendas)))
3436 (let (m txt)
3437 (goto-char (point-min))
3438 (while (not (eobp))
3439 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3440 (beginning-of-line 2)
3441 (setq txt (org-agenda-get-some-entry-text
3442 m org-agenda-add-entry-text-maxlines " > "))
3443 (end-of-line 1)
3444 (if (string-match "\\S-" txt)
3445 (insert "\n" txt)
3446 (or (eobp) (forward-char 1))))))))
3448 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3449 &rest keep)
3450 "Extract entry text from MARKER, at most N-LINES lines.
3451 This will ignore drawers etc, just get the text.
3452 If INDENT is given, prefix every line with this string. If KEEP is
3453 given, it is a list of symbols, defining stuff that should not be
3454 removed from the entry content. Currently only `planning' is allowed here."
3455 (let (txt drawer-re kwd-time-re ind)
3456 (save-excursion
3457 (with-current-buffer (marker-buffer marker)
3458 (if (not (derived-mode-p 'org-mode))
3459 (setq txt "")
3460 (org-with-wide-buffer
3461 (goto-char marker)
3462 (end-of-line 1)
3463 (setq txt (buffer-substring
3464 (min (1+ (point)) (point-max))
3465 (progn (outline-next-heading) (point)))
3466 drawer-re org-drawer-regexp
3467 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3468 ".*\n?"))
3469 (with-temp-buffer
3470 (insert txt)
3471 (when org-agenda-add-entry-text-descriptive-links
3472 (goto-char (point-min))
3473 (while (org-activate-links (point-max))
3474 (add-text-properties (match-beginning 0) (match-end 0)
3475 '(face org-link))))
3476 (goto-char (point-min))
3477 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3478 (set-text-properties (match-beginning 0) (match-end 0)
3479 nil))
3480 (goto-char (point-min))
3481 (while (re-search-forward drawer-re nil t)
3482 (delete-region
3483 (match-beginning 0)
3484 (progn (re-search-forward
3485 "^[ \t]*:END:.*\n?" nil 'move)
3486 (point))))
3487 (unless (member 'planning keep)
3488 (goto-char (point-min))
3489 (while (re-search-forward kwd-time-re nil t)
3490 (replace-match "")))
3491 (goto-char (point-min))
3492 (when org-agenda-entry-text-exclude-regexps
3493 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3494 (while (setq re (pop re-list))
3495 (goto-char (point-min))
3496 (while (re-search-forward re nil t)
3497 (replace-match "")))))
3498 (goto-char (point-max))
3499 (skip-chars-backward " \t\n")
3500 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3502 ;; find and remove min common indentation
3503 (goto-char (point-min))
3504 (untabify (point-min) (point-max))
3505 (setq ind (org-get-indentation))
3506 (while (not (eobp))
3507 (unless (looking-at "[ \t]*$")
3508 (setq ind (min ind (org-get-indentation))))
3509 (beginning-of-line 2))
3510 (goto-char (point-min))
3511 (while (not (eobp))
3512 (unless (looking-at "[ \t]*$")
3513 (move-to-column ind)
3514 (delete-region (point-at-bol) (point)))
3515 (beginning-of-line 2))
3517 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3519 (goto-char (point-min))
3520 (when indent
3521 (while (and (not (eobp)) (re-search-forward "^" nil t))
3522 (replace-match indent t t)))
3523 (goto-char (point-min))
3524 (while (looking-at "[ \t]*\n") (replace-match ""))
3525 (goto-char (point-max))
3526 (when (> (org-current-line)
3527 n-lines)
3528 (org-goto-line (1+ n-lines))
3529 (backward-char 1))
3530 (setq txt (buffer-substring (point-min) (point))))))))
3531 txt))
3533 (defun org-check-for-org-mode ()
3534 "Make sure current buffer is in org-mode. Error if not."
3535 (or (derived-mode-p 'org-mode)
3536 (error "Cannot execute Org agenda command on buffer in %s"
3537 major-mode)))
3539 ;;; Agenda prepare and finalize
3541 (defvar org-agenda-multi nil) ; dynamically scoped
3542 (defvar org-agenda-pre-window-conf nil)
3543 (defvar org-agenda-columns-active nil)
3544 (defvar org-agenda-name nil)
3545 (defvar org-agenda-tag-filter nil)
3546 (defvar org-agenda-category-filter nil)
3547 (defvar org-agenda-regexp-filter nil)
3548 (defvar org-agenda-effort-filter nil)
3549 (defvar org-agenda-top-headline-filter nil)
3550 (defvar org-agenda-tag-filter-preset nil
3551 "A preset of the tags filter used for secondary agenda filtering.
3552 This must be a list of strings, each string must be a single tag preceded
3553 by \"+\" or \"-\".
3554 This variable should not be set directly, but agenda custom commands can
3555 bind it in the options section. The preset filter is a global property of
3556 the entire agenda view. In a block agenda, it will not work reliably to
3557 define a filter for one of the individual blocks. You need to set it in
3558 the global options and expect it to be applied to the entire view.")
3560 (defvar org-agenda-category-filter-preset nil
3561 "A preset of the category filter used for secondary agenda filtering.
3562 This must be a list of strings, each string must be a single category
3563 preceded by \"+\" or \"-\".
3564 This variable should not be set directly, but agenda custom commands can
3565 bind it in the options section. The preset filter is a global property of
3566 the entire agenda view. In a block agenda, it will not work reliably to
3567 define a filter for one of the individual blocks. You need to set it in
3568 the global options and expect it to be applied to the entire view.")
3570 (defvar org-agenda-regexp-filter-preset nil
3571 "A preset of the regexp filter used for secondary agenda filtering.
3572 This must be a list of strings, each string must be a single regexp
3573 preceded by \"+\" or \"-\".
3574 This variable should not be set directly, but agenda custom commands can
3575 bind it in the options section. The preset filter is a global property of
3576 the entire agenda view. In a block agenda, it will not work reliably to
3577 define a filter for one of the individual blocks. You need to set it in
3578 the global options and expect it to be applied to the entire view.")
3580 (defvar org-agenda-effort-filter-preset nil
3581 "A preset of the effort condition used for secondary agenda filtering.
3582 This must be a list of strings, each string must be a single regexp
3583 preceded by \"+\" or \"-\".
3584 This variable should not be set directly, but agenda custom commands can
3585 bind it in the options section. The preset filter is a global property of
3586 the entire agenda view. In a block agenda, it will not work reliably to
3587 define a filter for one of the individual blocks. You need to set it in
3588 the global options and expect it to be applied to the entire view.")
3590 (defun org-agenda-use-sticky-p ()
3591 "Return non-nil if an agenda buffer named
3592 `org-agenda-buffer-name' exists and should be shown instead of
3593 generating a new one."
3594 (and
3595 ;; turned off by user
3596 org-agenda-sticky
3597 ;; For multi-agenda buffer already exists
3598 (not org-agenda-multi)
3599 ;; buffer found
3600 (get-buffer org-agenda-buffer-name)
3601 ;; C-u parameter is same as last call
3602 (with-current-buffer (get-buffer org-agenda-buffer-name)
3603 (and
3604 (equal current-prefix-arg
3605 org-agenda-last-prefix-arg)
3606 ;; In case user turned stickiness on, while having existing
3607 ;; Agenda buffer active, don't reuse that buffer, because it
3608 ;; does not have org variables local
3609 org-agenda-this-buffer-is-sticky))))
3611 (defun org-agenda-prepare-window (abuf filter-alist)
3612 "Setup agenda buffer in the window.
3613 ABUF is the buffer for the agenda window.
3614 FILTER-ALIST is an alist of filters we need to apply when
3615 `org-agenda-persistent-filter' is non-nil."
3616 (let* ((awin (get-buffer-window abuf)) wconf)
3617 (cond
3618 ((equal (current-buffer) abuf) nil)
3619 (awin (select-window awin))
3620 ((not (setq wconf (current-window-configuration))))
3621 ((eq org-agenda-window-setup 'current-window)
3622 (pop-to-buffer-same-window abuf))
3623 ((eq org-agenda-window-setup 'other-window)
3624 (org-switch-to-buffer-other-window abuf))
3625 ((eq org-agenda-window-setup 'other-frame)
3626 (switch-to-buffer-other-frame abuf))
3627 ((eq org-agenda-window-setup 'only-window)
3628 (delete-other-windows)
3629 (pop-to-buffer-same-window abuf))
3630 ((eq org-agenda-window-setup 'reorganize-frame)
3631 (delete-other-windows)
3632 (org-switch-to-buffer-other-window abuf)))
3633 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3634 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3635 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3636 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3637 ;; Additional test in case agenda is invoked from within agenda
3638 ;; buffer via elisp link.
3639 (unless (equal (current-buffer) abuf)
3640 (pop-to-buffer-same-window abuf))
3641 (setq org-agenda-pre-window-conf
3642 (or wconf org-agenda-pre-window-conf))))
3644 (defun org-agenda-prepare (&optional name)
3645 (let ((filter-alist (if org-agenda-persistent-filter
3646 (with-current-buffer
3647 (get-buffer-create org-agenda-buffer-name)
3648 (list `(tag . ,org-agenda-tag-filter)
3649 `(re . ,org-agenda-regexp-filter)
3650 `(effort . ,org-agenda-effort-filter)
3651 `(cat . ,org-agenda-category-filter))))))
3652 (if (org-agenda-use-sticky-p)
3653 (progn
3654 (put 'org-agenda-tag-filter :preset-filter nil)
3655 (put 'org-agenda-category-filter :preset-filter nil)
3656 (put 'org-agenda-regexp-filter :preset-filter nil)
3657 ;; Popup existing buffer
3658 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3659 filter-alist)
3660 (message "Sticky Agenda buffer, use `r' to refresh")
3661 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3662 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3663 (setq org-todo-keywords-for-agenda nil)
3664 (put 'org-agenda-tag-filter :preset-filter
3665 org-agenda-tag-filter-preset)
3666 (put 'org-agenda-category-filter :preset-filter
3667 org-agenda-category-filter-preset)
3668 (put 'org-agenda-regexp-filter :preset-filter
3669 org-agenda-regexp-filter-preset)
3670 (put 'org-agenda-effort-filter :preset-filter
3671 org-agenda-effort-filter-preset)
3672 (if org-agenda-multi
3673 (progn
3674 (setq buffer-read-only nil)
3675 (goto-char (point-max))
3676 (unless (or (bobp) org-agenda-compact-blocks
3677 (not org-agenda-block-separator))
3678 (insert "\n"
3679 (if (stringp org-agenda-block-separator)
3680 org-agenda-block-separator
3681 (make-string (window-width) org-agenda-block-separator))
3682 "\n"))
3683 (narrow-to-region (point) (point-max)))
3684 (setq org-done-keywords-for-agenda nil)
3685 ;; Setting any org variables that are in org-agenda-local-vars
3686 ;; list need to be done after the prepare call
3687 (org-agenda-prepare-window
3688 (get-buffer-create org-agenda-buffer-name) filter-alist)
3689 (setq buffer-read-only nil)
3690 (org-agenda-reset-markers)
3691 (let ((inhibit-read-only t)) (erase-buffer))
3692 (org-agenda-mode)
3693 (setq org-agenda-buffer (current-buffer))
3694 (setq org-agenda-contributing-files nil)
3695 (setq org-agenda-columns-active nil)
3696 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3697 (setq org-todo-keywords-for-agenda
3698 (org-uniquify org-todo-keywords-for-agenda))
3699 (setq org-done-keywords-for-agenda
3700 (org-uniquify org-done-keywords-for-agenda))
3701 (setq org-agenda-last-prefix-arg current-prefix-arg)
3702 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3703 (and name (not org-agenda-name)
3704 (setq-local org-agenda-name name)))
3705 (setq buffer-read-only nil))))
3707 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3708 (defun org-agenda-finalize ()
3709 "Finishing touch for the agenda buffer, called just before displaying it."
3710 (unless org-agenda-multi
3711 (save-excursion
3712 (let ((inhibit-read-only t))
3713 (goto-char (point-min))
3714 (save-excursion
3715 (while (org-activate-links (point-max))
3716 (add-text-properties (match-beginning 0) (match-end 0)
3717 '(face org-link))))
3718 (unless (eq org-agenda-remove-tags t)
3719 (org-agenda-align-tags))
3720 (unless org-agenda-with-colors
3721 (remove-text-properties (point-min) (point-max) '(face nil)))
3722 (if (and (boundp 'org-agenda-overriding-columns-format)
3723 org-agenda-overriding-columns-format)
3724 (setq-local org-agenda-overriding-columns-format
3725 org-agenda-overriding-columns-format))
3726 (if (and (boundp 'org-agenda-view-columns-initially)
3727 org-agenda-view-columns-initially)
3728 (org-agenda-columns))
3729 (when org-agenda-fontify-priorities
3730 (org-agenda-fontify-priorities))
3731 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3732 (org-agenda-dim-blocked-tasks))
3733 (org-agenda-mark-clocking-task)
3734 (when org-agenda-entry-text-mode
3735 (org-agenda-entry-text-hide)
3736 (org-agenda-entry-text-show))
3737 (if (and (functionp 'org-habit-insert-consistency-graphs)
3738 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3739 (org-habit-insert-consistency-graphs))
3740 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3741 (unless (or (eq org-agenda-show-inherited-tags 'always)
3742 (and (listp org-agenda-show-inherited-tags)
3743 (memq org-agenda-type org-agenda-show-inherited-tags))
3744 (and (eq org-agenda-show-inherited-tags t)
3745 (or (eq org-agenda-use-tag-inheritance t)
3746 (and (listp org-agenda-use-tag-inheritance)
3747 (not (memq org-agenda-type
3748 org-agenda-use-tag-inheritance))))))
3749 (let (mrk)
3750 (save-excursion
3751 (goto-char (point-min))
3752 (while (equal (forward-line) 0)
3753 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3754 (put-text-property (point-at-bol) (point-at-eol)
3755 'tags (org-with-point-at mrk
3756 (delete-dups
3757 (mapcar 'downcase (org-get-tags-at))))))))))
3758 (run-hooks 'org-agenda-finalize-hook)
3759 (when org-agenda-top-headline-filter
3760 (org-agenda-filter-top-headline-apply
3761 org-agenda-top-headline-filter))
3762 (when org-agenda-tag-filter
3763 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3764 (when (get 'org-agenda-tag-filter :preset-filter)
3765 (org-agenda-filter-apply
3766 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3767 (when org-agenda-category-filter
3768 (org-agenda-filter-apply org-agenda-category-filter 'category))
3769 (when (get 'org-agenda-category-filter :preset-filter)
3770 (org-agenda-filter-apply
3771 (get 'org-agenda-category-filter :preset-filter) 'category))
3772 (when org-agenda-regexp-filter
3773 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3774 (when (get 'org-agenda-regexp-filter :preset-filter)
3775 (org-agenda-filter-apply
3776 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3777 (when org-agenda-effort-filter
3778 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3779 (when (get 'org-agenda-effort-filter :preset-filter)
3780 (org-agenda-filter-apply
3781 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3782 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3784 (defun org-agenda-mark-clocking-task ()
3785 "Mark the current clock entry in the agenda if it is present."
3786 ;; We need to widen when `org-agenda-finalize' is called from
3787 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3788 (when (bound-and-true-p org-clock-current-task)
3789 (save-restriction
3790 (widen)
3791 (org-agenda-unmark-clocking-task)
3792 (when (marker-buffer org-clock-hd-marker)
3793 (save-excursion
3794 (goto-char (point-min))
3795 (let (s ov)
3796 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3797 (goto-char s)
3798 (when (equal (org-get-at-bol 'org-hd-marker)
3799 org-clock-hd-marker)
3800 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3801 (overlay-put ov 'type 'org-agenda-clocking)
3802 (overlay-put ov 'face 'org-agenda-clocking)
3803 (overlay-put ov 'help-echo
3804 "The clock is running in this item")))))))))
3806 (defun org-agenda-unmark-clocking-task ()
3807 "Unmark the current clocking task."
3808 (mapc (lambda (o)
3809 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3810 (delete-overlay o)))
3811 (overlays-in (point-min) (point-max))))
3813 (defun org-agenda-fontify-priorities ()
3814 "Make highest priority lines bold, and lowest italic."
3815 (interactive)
3816 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3817 (delete-overlay o)))
3818 (overlays-in (point-min) (point-max)))
3819 (save-excursion
3820 (let (b e p ov h l)
3821 (goto-char (point-min))
3822 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3823 (setq h (or (get-char-property (point) 'org-highest-priority)
3824 org-highest-priority)
3825 l (or (get-char-property (point) 'org-lowest-priority)
3826 org-lowest-priority)
3827 p (string-to-char (match-string 1))
3828 b (match-beginning 0)
3829 e (if (eq org-agenda-fontify-priorities 'cookies)
3830 (match-end 0)
3831 (point-at-eol))
3832 ov (make-overlay b e))
3833 (overlay-put
3834 ov 'face
3835 (let ((special-face
3836 (cond ((org-face-from-face-or-color
3837 'priority 'org-priority
3838 (cdr (assoc p org-priority-faces))))
3839 ((and (listp org-agenda-fontify-priorities)
3840 (org-face-from-face-or-color
3841 'priority 'org-priority
3842 (cdr (assoc p org-agenda-fontify-priorities)))))
3843 ((equal p l) 'italic)
3844 ((equal p h) 'bold))))
3845 (if special-face (list special-face 'org-priority) 'org-priority)))
3846 (overlay-put ov 'org-type 'org-priority)))))
3848 (defvar org-depend-tag-blocked)
3850 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3851 "Dim currently blocked TODOs in the agenda display.
3852 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3853 dimming them."
3854 (interactive "P")
3855 (when (called-interactively-p 'interactive)
3856 (message "Dim or hide blocked tasks..."))
3857 (dolist (o (overlays-in (point-min) (point-max)))
3858 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3859 (delete-overlay o)))
3860 (save-excursion
3861 (let ((inhibit-read-only t)
3862 (org-depend-tag-blocked nil)
3863 org-blocked-by-checkboxes)
3864 (goto-char (point-min))
3865 (while (let ((pos (text-property-not-all
3866 (point) (point-max) 'todo-state nil)))
3867 (when pos (goto-char pos)))
3868 (setq org-blocked-by-checkboxes nil)
3869 (let ((marker (org-get-at-bol 'org-hd-marker)))
3870 (when (and (markerp marker)
3871 (with-current-buffer (marker-buffer marker)
3872 (save-excursion (goto-char marker)
3873 (org-entry-blocked-p))))
3874 ;; Entries blocked by checkboxes cannot be made invisible.
3875 ;; See `org-agenda-dim-blocked-tasks' for details.
3876 (let* ((really-invisible
3877 (and (not org-blocked-by-checkboxes)
3878 (or invisible (eq org-agenda-dim-blocked-tasks
3879 'invisible))))
3880 (ov (make-overlay (if really-invisible (line-end-position 0)
3881 (line-beginning-position))
3882 (line-end-position))))
3883 (if really-invisible (overlay-put ov 'invisible t)
3884 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3885 (overlay-put ov 'org-type 'org-blocked-todo))))
3886 (forward-line))))
3887 (when (called-interactively-p 'interactive)
3888 (message "Dim or hide blocked tasks...done")))
3890 (defvar org-agenda-skip-function nil
3891 "Function to be called at each match during agenda construction.
3892 If this function returns nil, the current match should not be skipped.
3893 Otherwise, the function must return a position from where the search
3894 should be continued.
3895 This may also be a Lisp form, it will be evaluated.
3896 Never set this variable using `setq' or so, because then it will apply
3897 to all future agenda commands. If you do want a global skipping condition,
3898 use the option `org-agenda-skip-function-global' instead.
3899 The correct usage for `org-agenda-skip-function' is to bind it with
3900 `let' to scope it dynamically into the agenda-constructing command.
3901 A good way to set it is through options in `org-agenda-custom-commands'.")
3903 (defun org-agenda-skip ()
3904 "Throw to `:skip' in places that should be skipped.
3905 Also moves point to the end of the skipped region, so that search can
3906 continue from there."
3907 (let ((p (point-at-bol)) to)
3908 (when (or
3909 (save-excursion (goto-char p) (looking-at comment-start-skip))
3910 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3911 (get-text-property p :org-archived)
3912 (org-end-of-subtree t))
3913 (and org-agenda-skip-comment-trees
3914 (get-text-property p :org-comment)
3915 (org-end-of-subtree t))
3916 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3917 (org-agenda-skip-eval org-agenda-skip-function)))
3918 (goto-char to))
3919 (org-in-src-block-p t))
3920 (throw :skip t))))
3922 (defun org-agenda-skip-eval (form)
3923 "If FORM is a function or a list, call (or eval) it and return the result.
3924 `save-excursion' and `save-match-data' are wrapped around the call, so point
3925 and match data are returned to the previous state no matter what these
3926 functions do."
3927 (let (fp)
3928 (and form
3929 (or (setq fp (functionp form))
3930 (consp form))
3931 (save-excursion
3932 (save-match-data
3933 (if fp
3934 (funcall form)
3935 (eval form)))))))
3937 (defvar org-agenda-markers nil
3938 "List of all currently active markers created by `org-agenda'.")
3939 (defvar org-agenda-last-marker-time (float-time)
3940 "Creation time of the last agenda marker.")
3942 (defun org-agenda-new-marker (&optional pos)
3943 "Return a new agenda marker.
3944 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3945 these markers and resets them when they are no longer in use."
3946 (let ((m (copy-marker (or pos (point)) t)))
3947 (setq org-agenda-last-marker-time (float-time))
3948 (if org-agenda-buffer
3949 (with-current-buffer org-agenda-buffer
3950 (push m org-agenda-markers))
3951 (push m org-agenda-markers))
3954 (defun org-agenda-reset-markers ()
3955 "Reset markers created by `org-agenda'."
3956 (while org-agenda-markers
3957 (move-marker (pop org-agenda-markers) nil)))
3959 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3960 "Save relative positions of markers in region.
3961 This check for agenda markers in all agenda buffers currently active."
3962 (dolist (buf (buffer-list))
3963 (with-current-buffer buf
3964 (when (eq major-mode 'org-agenda-mode)
3965 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3966 org-agenda-markers)))))
3968 ;;; Entry text mode
3970 (defun org-agenda-entry-text-show-here ()
3971 "Add some text from the entry as context to the current line."
3972 (let (m txt o)
3973 (setq m (org-get-at-bol 'org-hd-marker))
3974 (unless (marker-buffer m)
3975 (error "No marker points to an entry here"))
3976 (setq txt (concat "\n" (org-no-properties
3977 (org-agenda-get-some-entry-text
3978 m org-agenda-entry-text-maxlines
3979 org-agenda-entry-text-leaders))))
3980 (when (string-match "\\S-" txt)
3981 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3982 (overlay-put o 'evaporate t)
3983 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3984 (overlay-put o 'after-string txt))))
3986 (defun org-agenda-entry-text-show ()
3987 "Add entry context for all agenda lines."
3988 (interactive)
3989 (save-excursion
3990 (goto-char (point-max))
3991 (beginning-of-line 1)
3992 (while (not (bobp))
3993 (when (org-get-at-bol 'org-hd-marker)
3994 (org-agenda-entry-text-show-here))
3995 (beginning-of-line 0))))
3997 (defun org-agenda-entry-text-hide ()
3998 "Remove any shown entry context."
3999 (delq nil
4000 (mapcar (lambda (o)
4001 (if (eq (overlay-get o 'org-overlay-type)
4002 'agenda-entry-content)
4003 (progn (delete-overlay o) t)))
4004 (overlays-in (point-min) (point-max)))))
4006 (defun org-agenda-get-day-face (date)
4007 "Return the face DATE should be displayed with."
4008 (cond ((and (functionp org-agenda-day-face-function)
4009 (funcall org-agenda-day-face-function date)))
4010 ((org-agenda-today-p date) 'org-agenda-date-today)
4011 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4012 'org-agenda-date-weekend)
4013 (t 'org-agenda-date)))
4015 ;;; Agenda timeline
4017 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4018 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4020 (defun org-timeline (&optional dotodo)
4021 "Show a time-sorted view of the entries in the current Org file.
4023 Only entries with a time stamp of today or later will be listed.
4025 With `\\[universal-argument]' prefix, all unfinished TODO items will also be \
4026 shown,
4027 under the current date.
4029 If the buffer contains an active region, only check the region
4030 for dates."
4031 (interactive "P")
4032 (let* ((dopast t)
4033 (org-agenda-show-log-scoped org-agenda-show-log)
4034 (org-agenda-show-log org-agenda-show-log-scoped)
4035 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4036 (current-buffer))))
4037 (date (calendar-current-date))
4038 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4039 (end (if (org-region-active-p) (region-end) (point-max)))
4040 (day-numbers (org-get-all-dates
4041 beg end 'no-ranges
4042 t org-agenda-show-log-scoped ; always include today
4043 org-timeline-show-empty-dates))
4044 (org-deadline-warning-days 0)
4045 (org-agenda-only-exact-dates t)
4046 (today (org-today))
4047 (past t)
4048 args
4049 s e rtn d emptyp)
4050 (setq org-agenda-redo-command
4051 (list 'let
4052 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4053 (list 'org-switch-to-buffer-other-window (current-buffer))
4054 (list 'org-timeline (list 'quote dotodo))))
4055 (put 'org-agenda-redo-command 'org-lprops nil)
4056 (if (not dopast)
4057 ;; Remove past dates from the list of dates.
4058 (setq day-numbers (delq nil (mapcar (lambda(x)
4059 (if (>= x today) x nil))
4060 day-numbers))))
4061 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4062 (org-compile-prefix-format 'timeline)
4063 (org-set-sorting-strategy 'timeline)
4064 (if org-agenda-show-log-scoped (push :closed args))
4065 (push :timestamp args)
4066 (push :deadline args)
4067 (push :scheduled args)
4068 (push :sexp args)
4069 (if dotodo (push :todo args))
4070 (insert "Timeline of file " entry "\n")
4071 (add-text-properties (point-min) (point)
4072 (list 'face 'org-agenda-structure))
4073 (org-agenda-mark-header-line (point-min))
4074 (while (setq d (pop day-numbers))
4075 (if (and (listp d) (eq (car d) :omitted))
4076 (progn
4077 (setq s (point))
4078 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4079 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4080 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4081 (if (and (>= d today)
4082 dopast
4083 past)
4084 (progn
4085 (setq past nil)
4086 (insert (make-string 79 ?-) "\n")))
4087 (setq date (calendar-gregorian-from-absolute d))
4088 (setq s (point))
4089 (setq rtn (and (not emptyp)
4090 (apply 'org-agenda-get-day-entries entry
4091 date args)))
4092 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4093 (progn
4094 (insert
4095 (if (stringp org-agenda-format-date)
4096 (format-time-string org-agenda-format-date
4097 (org-time-from-absolute date))
4098 (funcall org-agenda-format-date date))
4099 "\n")
4100 (put-text-property s (1- (point)) 'face
4101 (org-agenda-get-day-face date))
4102 (put-text-property s (1- (point)) 'org-date-line t)
4103 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4104 (if (equal d today)
4105 (put-text-property s (1- (point)) 'org-today t))
4106 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4107 (put-text-property s (1- (point)) 'day d)))))
4108 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4109 (point-min)))
4110 (add-text-properties
4111 (point-min) (point-max)
4112 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4113 (org-agenda-finalize)
4114 (setq buffer-read-only t)))
4116 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4117 "Return a list of all relevant day numbers from BEG to END buffer positions.
4118 If NO-RANGES is non-nil, include only the start and end dates of a range,
4119 not every single day in the range. If FORCE-TODAY is non-nil, make
4120 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4121 inactive time stamps (those in square brackets) are included.
4122 When EMPTY is non-nil, also include days without any entries."
4123 (let ((re (concat
4124 (if pre-re pre-re "")
4125 (if inactive org-ts-regexp-both org-ts-regexp)))
4126 dates dates1 date day day1 day2 ts1 ts2 pos)
4127 (if force-today
4128 (setq dates (list (org-today))))
4129 (save-excursion
4130 (goto-char beg)
4131 (while (re-search-forward re end t)
4132 (setq day (time-to-days (org-time-string-to-time
4133 (substring (match-string 1) 0 10)
4134 (current-buffer) (match-beginning 0))))
4135 (or (memq day dates) (push day dates)))
4136 (unless no-ranges
4137 (goto-char beg)
4138 (while (re-search-forward org-tr-regexp end t)
4139 (setq pos (match-beginning 0))
4140 (setq ts1 (substring (match-string 1) 0 10)
4141 ts2 (substring (match-string 2) 0 10)
4142 day1 (time-to-days (org-time-string-to-time
4143 ts1 (current-buffer) pos))
4144 day2 (time-to-days (org-time-string-to-time
4145 ts2 (current-buffer) pos)))
4146 (while (< (setq day1 (1+ day1)) day2)
4147 (or (memq day1 dates) (push day1 dates)))))
4148 (setq dates (sort dates '<))
4149 (when empty
4150 (while (setq day (pop dates))
4151 (setq day2 (car dates))
4152 (push day dates1)
4153 (when (and day2 empty)
4154 (if (or (eq empty t)
4155 (and (numberp empty) (<= (- day2 day) empty)))
4156 (while (< (setq day (1+ day)) day2)
4157 (push (list day) dates1))
4158 (push (cons :omitted (- day2 day)) dates1))))
4159 (setq dates (nreverse dates1)))
4160 dates)))
4162 ;;; Agenda Daily/Weekly
4164 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4165 "Start day for the agenda view.
4166 Custom commands can set this variable in the options section.
4167 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4168 input allowed when reading a date through the Org calendar.
4169 See the docstring of `org-read-date' for details.")
4170 (defvar org-starting-day nil) ; local variable in the agenda buffer
4171 (defvar org-arg-loc nil) ; local variable
4173 (defvar org-agenda-buffer-tmp-name nil)
4174 ;;;###autoload
4175 (defun org-agenda-list (&optional arg start-day span with-hour)
4176 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4177 The view will be for the current day or week, but from the overview buffer
4178 you will be able to go to other days/weeks.
4180 With a numeric prefix argument in an interactive call, the agenda will
4181 span ARG days. Lisp programs should instead specify SPAN to change
4182 the number of days. SPAN defaults to `org-agenda-span'.
4184 START-DAY defaults to TODAY, or to the most recent match for the weekday
4185 given in `org-agenda-start-on-weekday'.
4187 When WITH-HOUR is non-nil, only include scheduled and deadline
4188 items if they have an hour specification like [h]h:mm."
4189 (interactive "P")
4190 (if org-agenda-overriding-arguments
4191 (setq arg (car org-agenda-overriding-arguments)
4192 start-day (nth 1 org-agenda-overriding-arguments)
4193 span (nth 2 org-agenda-overriding-arguments)))
4194 (if (and (integerp arg) (> arg 0))
4195 (setq span arg arg nil))
4196 (catch 'exit
4197 (setq org-agenda-buffer-name
4198 (or org-agenda-buffer-tmp-name
4199 (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
4200 (if org-agenda-sticky
4201 (cond ((and org-keys (stringp org-match))
4202 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4203 (org-keys
4204 (format "*Org Agenda(%s)*" org-keys))
4205 (t "*Org Agenda(a)*")))
4206 "*Org Agenda*"))
4207 (org-agenda-prepare "Day/Week")
4208 (setq start-day (or start-day org-agenda-start-day))
4209 (if (stringp start-day)
4210 ;; Convert to an absolute day number
4211 (setq start-day (time-to-days (org-read-date nil t start-day))))
4212 (org-compile-prefix-format 'agenda)
4213 (org-set-sorting-strategy 'agenda)
4214 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4215 (today (org-today))
4216 (sd (or start-day today))
4217 (ndays (org-agenda-span-to-ndays span sd))
4218 (org-agenda-start-on-weekday
4219 (if (or (eq ndays 7) (eq ndays 14))
4220 org-agenda-start-on-weekday))
4221 (thefiles (org-agenda-files nil 'ifmode))
4222 (files thefiles)
4223 (start (if (or (null org-agenda-start-on-weekday)
4224 (< ndays 7))
4226 (let* ((nt (calendar-day-of-week
4227 (calendar-gregorian-from-absolute sd)))
4228 (n1 org-agenda-start-on-weekday)
4229 (d (- nt n1)))
4230 (- sd (+ (if (< d 0) 7 0) d)))))
4231 (day-numbers (list start))
4232 (day-cnt 0)
4233 (inhibit-redisplay (not debug-on-error))
4234 (org-agenda-show-log-scoped org-agenda-show-log)
4235 s e rtn rtnall file date d start-pos end-pos todayp
4236 clocktable-start clocktable-end filter)
4237 (setq org-agenda-redo-command
4238 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4239 (dotimes (n (1- ndays))
4240 (push (1+ (car day-numbers)) day-numbers))
4241 (setq day-numbers (nreverse day-numbers))
4242 (setq clocktable-start (car day-numbers)
4243 clocktable-end (1+ (or (org-last day-numbers) 0)))
4244 (setq-local org-starting-day (car day-numbers))
4245 (setq-local org-arg-loc arg)
4246 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4247 (unless org-agenda-compact-blocks
4248 (let* ((d1 (car day-numbers))
4249 (d2 (org-last day-numbers))
4250 (w1 (org-days-to-iso-week d1))
4251 (w2 (org-days-to-iso-week d2)))
4252 (setq s (point))
4253 (if org-agenda-overriding-header
4254 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4255 nil 'face 'org-agenda-structure) "\n")
4256 (insert (org-agenda-span-name span)
4257 "-agenda"
4258 (if (< (- d2 d1) 350)
4259 (if (= w1 w2)
4260 (format " (W%02d)" w1)
4261 (format " (W%02d-W%02d)" w1 w2))
4263 ":\n")))
4264 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4265 'org-date-line t))
4266 (org-agenda-mark-header-line s))
4267 (while (setq d (pop day-numbers))
4268 (setq date (calendar-gregorian-from-absolute d)
4269 s (point))
4270 (if (or (setq todayp (= d today))
4271 (and (not start-pos) (= d sd)))
4272 (setq start-pos (point))
4273 (if (and start-pos (not end-pos))
4274 (setq end-pos (point))))
4275 (setq files thefiles
4276 rtnall nil)
4277 (while (setq file (pop files))
4278 (catch 'nextfile
4279 (org-check-agenda-file file)
4280 (let ((org-agenda-entry-types org-agenda-entry-types))
4281 ;; Starred types override non-starred equivalents
4282 (when (member :deadline* org-agenda-entry-types)
4283 (setq org-agenda-entry-types
4284 (delq :deadline org-agenda-entry-types)))
4285 (when (member :scheduled* org-agenda-entry-types)
4286 (setq org-agenda-entry-types
4287 (delq :scheduled org-agenda-entry-types)))
4288 ;; Honor with-hour
4289 (when with-hour
4290 (when (member :deadline org-agenda-entry-types)
4291 (setq org-agenda-entry-types
4292 (delq :deadline org-agenda-entry-types))
4293 (push :deadline* org-agenda-entry-types))
4294 (when (member :scheduled org-agenda-entry-types)
4295 (setq org-agenda-entry-types
4296 (delq :scheduled org-agenda-entry-types))
4297 (push :scheduled* org-agenda-entry-types)))
4298 (unless org-agenda-include-deadlines
4299 (setq org-agenda-entry-types
4300 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4301 (cond
4302 ((memq org-agenda-show-log-scoped '(only clockcheck))
4303 (setq rtn (org-agenda-get-day-entries
4304 file date :closed)))
4305 (org-agenda-show-log-scoped
4306 (setq rtn (apply 'org-agenda-get-day-entries
4307 file date
4308 (append '(:closed) org-agenda-entry-types))))
4310 (setq rtn (apply 'org-agenda-get-day-entries
4311 file date
4312 org-agenda-entry-types)))))
4313 (setq rtnall (append rtnall rtn)))) ;; all entries
4314 (if org-agenda-include-diary
4315 (let ((org-agenda-search-headline-for-time t))
4316 (require 'diary-lib)
4317 (setq rtn (org-get-entries-from-diary date))
4318 (setq rtnall (append rtnall rtn))))
4319 (if (or rtnall org-agenda-show-all-dates)
4320 (progn
4321 (setq day-cnt (1+ day-cnt))
4322 (insert
4323 (if (stringp org-agenda-format-date)
4324 (format-time-string org-agenda-format-date
4325 (org-time-from-absolute date))
4326 (funcall org-agenda-format-date date))
4327 "\n")
4328 (put-text-property s (1- (point)) 'face
4329 (org-agenda-get-day-face date))
4330 (put-text-property s (1- (point)) 'org-date-line t)
4331 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4332 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4333 (when todayp
4334 (put-text-property s (1- (point)) 'org-today t))
4335 (setq rtnall
4336 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4337 (if rtnall (insert ;; all entries
4338 (org-agenda-finalize-entries rtnall 'agenda)
4339 "\n"))
4340 (put-text-property s (1- (point)) 'day d)
4341 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4342 (when (and org-agenda-clockreport-mode clocktable-start)
4343 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4344 ;; the above line is to ensure the restricted range!
4345 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4346 tbl)
4347 (setq p (org-plist-delete p :block))
4348 (setq p (plist-put p :tstart clocktable-start))
4349 (setq p (plist-put p :tend clocktable-end))
4350 (setq p (plist-put p :scope 'agenda))
4351 (setq tbl (apply 'org-clock-get-clocktable p))
4352 (insert tbl)))
4353 (goto-char (point-min))
4354 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4355 (unless (and (pos-visible-in-window-p (point-min))
4356 (pos-visible-in-window-p (point-max)))
4357 (goto-char (1- (point-max)))
4358 (recenter -1)
4359 (if (not (pos-visible-in-window-p (or start-pos 1)))
4360 (progn
4361 (goto-char (or start-pos 1))
4362 (recenter 1))))
4363 (goto-char (or start-pos 1))
4364 (add-text-properties (point-min) (point-max)
4365 `(org-agenda-type agenda
4366 org-last-args (,arg ,start-day ,span)
4367 org-redo-cmd ,org-agenda-redo-command
4368 org-series-cmd ,org-cmd))
4369 (if (eq org-agenda-show-log-scoped 'clockcheck)
4370 (org-agenda-show-clocking-issues))
4371 (org-agenda-finalize)
4372 (setq buffer-read-only t)
4373 (message ""))))
4375 (defun org-agenda-ndays-to-span (n)
4376 "Return a span symbol for a span of N days, or N if none matches."
4377 (cond ((symbolp n) n)
4378 ((= n 1) 'day)
4379 ((= n 7) 'week)
4380 ((= n 14) 'fortnight)
4381 (t n)))
4383 (defun org-agenda-span-to-ndays (span &optional start-day)
4384 "Return ndays from SPAN, possibly starting at START-DAY.
4385 START-DAY is an absolute time value."
4386 (cond ((numberp span) span)
4387 ((eq span 'day) 1)
4388 ((eq span 'week) 7)
4389 ((eq span 'fortnight) 14)
4390 ((eq span 'month)
4391 (let ((date (calendar-gregorian-from-absolute start-day)))
4392 (calendar-last-day-of-month (car date) (cl-caddr date))))
4393 ((eq span 'year)
4394 (let ((date (calendar-gregorian-from-absolute start-day)))
4395 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4397 (defun org-agenda-span-name (span)
4398 "Return a SPAN name."
4399 (if (null span)
4401 (if (symbolp span)
4402 (capitalize (symbol-name span))
4403 (format "%d days" span))))
4405 ;;; Agenda word search
4407 (defvar org-agenda-search-history nil)
4409 (defvar org-search-syntax-table nil
4410 "Special syntax table for Org search.
4411 In this table, we have single quotes not as word constituents, to
4412 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4414 (defvar org-mode-syntax-table) ; From org.el
4415 (defun org-search-syntax-table ()
4416 (unless org-search-syntax-table
4417 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4418 (modify-syntax-entry ?' "." org-search-syntax-table)
4419 (modify-syntax-entry ?` "." org-search-syntax-table))
4420 org-search-syntax-table)
4422 (defvar org-agenda-last-search-view-search-was-boolean nil)
4424 ;;;###autoload
4425 (defun org-search-view (&optional todo-only string edit-at)
4426 "Show all entries that contain a phrase or words or regular expressions.
4428 With optional prefix argument TODO-ONLY, only consider entries that are
4429 TODO entries. The argument STRING can be used to pass a default search
4430 string into this function. If EDIT-AT is non-nil, it means that the
4431 user should get a chance to edit this string, with cursor at position
4432 EDIT-AT.
4434 The search string can be viewed either as a phrase that should be found as
4435 is, or it can be broken into a number of snippets, each of which must match
4436 in a Boolean way to select an entry. The default depends on the variable
4437 `org-agenda-search-view-always-boolean'.
4438 Even if this is turned off (the default) you can always switch to
4439 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4441 The default is a direct search of the whole phrase, where each space in
4442 the search string can expand to an arbitrary amount of whitespace,
4443 including newlines.
4445 If using a Boolean search, the search string is split on whitespace and
4446 each snippet is searched separately, with logical AND to select an entry.
4447 Words prefixed with a minus must *not* occur in the entry. Words without
4448 a prefix or prefixed with a plus must occur in the entry. Matching is
4449 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4450 match whole words, not parts of a word) if
4451 `org-agenda-search-view-force-full-words' is set (default is nil).
4453 Boolean search snippets enclosed by curly braces are interpreted as
4454 regular expressions that must or (when preceded with \"-\") must not
4455 match in the entry. Snippets enclosed into double quotes will be taken
4456 as a whole, to include whitespace.
4458 - If the search string starts with an asterisk, search only in headlines.
4459 - If (possibly after the leading star) the search string starts with an
4460 exclamation mark, this also means to look at TODO entries only, an effect
4461 that can also be achieved with a prefix argument.
4462 - If (possibly after star and exclamation mark) the search string starts
4463 with a colon, this will mean that the (non-regexp) snippets of the
4464 Boolean search must match as full words.
4466 This command searches the agenda files, and in addition the files listed
4467 in `org-agenda-text-search-extra-files'."
4468 (interactive "P")
4469 (if org-agenda-overriding-arguments
4470 (setq todo-only (car org-agenda-overriding-arguments)
4471 string (nth 1 org-agenda-overriding-arguments)
4472 edit-at (nth 2 org-agenda-overriding-arguments)))
4473 (let* ((props (list 'face nil
4474 'done-face 'org-agenda-done
4475 'org-not-done-regexp org-not-done-regexp
4476 'org-todo-regexp org-todo-regexp
4477 'org-complex-heading-regexp org-complex-heading-regexp
4478 'mouse-face 'highlight
4479 'help-echo (format "mouse-2 or RET jump to location")))
4480 (full-words org-agenda-search-view-force-full-words)
4481 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4482 regexp rtn rtnall files file pos inherited-tags
4483 marker category level tags c neg re boolean
4484 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4485 (unless (and (not edit-at)
4486 (stringp string)
4487 (string-match "\\S-" string))
4488 (setq string (read-string
4489 (if org-agenda-search-view-always-boolean
4490 "[+-]Word/{Regexp} ...: "
4491 "Phrase or [+-]Word/{Regexp} ...: ")
4492 (cond
4493 ((integerp edit-at) (cons string edit-at))
4494 (edit-at string))
4495 'org-agenda-search-history)))
4496 (catch 'exit
4497 (if org-agenda-sticky
4498 (setq org-agenda-buffer-name
4499 (if (stringp string)
4500 (format "*Org Agenda(%s:%s)*"
4501 (or org-keys (or (and todo-only "S") "s")) string)
4502 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4503 (org-agenda-prepare "SEARCH")
4504 (org-compile-prefix-format 'search)
4505 (org-set-sorting-strategy 'search)
4506 (setq org-agenda-redo-command
4507 (list 'org-search-view (if todo-only t nil)
4508 (list 'if 'current-prefix-arg nil string)))
4509 (setq org-agenda-query-string string)
4510 (if (equal (string-to-char string) ?*)
4511 (setq hdl-only t
4512 words (substring string 1))
4513 (setq words string))
4514 (when (equal (string-to-char words) ?!)
4515 (setq todo-only t
4516 words (substring words 1)))
4517 (when (equal (string-to-char words) ?:)
4518 (setq full-words t
4519 words (substring words 1)))
4520 (if (or org-agenda-search-view-always-boolean
4521 (member (string-to-char words) '(?- ?+ ?\{)))
4522 (setq boolean t))
4523 (setq words (org-split-string words))
4524 (let (www w)
4525 (while (setq w (pop words))
4526 (while (and (string-match "\\\\\\'" w) words)
4527 (setq w (concat (substring w 0 -1) " " (pop words))))
4528 (push w www))
4529 (setq words (nreverse www) www nil)
4530 (while (setq w (pop words))
4531 (when (and (string-match "\\`[-+]?{" w)
4532 (not (string-match "}\\'" w)))
4533 (while (and words (not (string-match "}\\'" (car words))))
4534 (setq w (concat w " " (pop words))))
4535 (setq w (concat w " " (pop words))))
4536 (push w www))
4537 (setq words (nreverse www)))
4538 (setq org-agenda-last-search-view-search-was-boolean boolean)
4539 (when boolean
4540 (let (wds w)
4541 (while (setq w (pop words))
4542 (if (or (equal (substring w 0 1) "\"")
4543 (and (> (length w) 1)
4544 (member (substring w 0 1) '("+" "-"))
4545 (equal (substring w 1 2) "\"")))
4546 (while (and words (not (equal (substring w -1) "\"")))
4547 (setq w (concat w " " (pop words)))))
4548 (and (string-match "\\`\\([-+]?\\)\"" w)
4549 (setq w (replace-match "\\1" nil nil w)))
4550 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4551 (push w wds))
4552 (setq words (nreverse wds))))
4553 (if boolean
4554 (mapc (lambda (w)
4555 (setq c (string-to-char w))
4556 (if (equal c ?-)
4557 (setq neg t w (substring w 1))
4558 (if (equal c ?+)
4559 (setq neg nil w (substring w 1))
4560 (setq neg nil)))
4561 (if (string-match "\\`{.*}\\'" w)
4562 (setq re (substring w 1 -1))
4563 (if full-words
4564 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4565 (setq re (regexp-quote (downcase w)))))
4566 (if neg (push re regexps-) (push re regexps+)))
4567 words)
4568 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4569 regexps+))
4570 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4571 (if (not regexps+)
4572 (setq regexp org-outline-regexp-bol)
4573 (setq regexp (pop regexps+))
4574 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4575 regexp))))
4576 (setq files (org-agenda-files nil 'ifmode))
4577 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4578 (pop org-agenda-text-search-extra-files)
4579 (setq files (org-add-archive-files files)))
4580 (setq files (append files org-agenda-text-search-extra-files)
4581 rtnall nil)
4582 (while (setq file (pop files))
4583 (setq ee nil)
4584 (catch 'nextfile
4585 (org-check-agenda-file file)
4586 (setq buffer (if (file-exists-p file)
4587 (org-get-agenda-file-buffer file)
4588 (error "No such file %s" file)))
4589 (if (not buffer)
4590 ;; If file does not exist, make sure an error message is sent
4591 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4592 file))))
4593 (with-current-buffer buffer
4594 (with-syntax-table (org-search-syntax-table)
4595 (unless (derived-mode-p 'org-mode)
4596 (error "Agenda file %s is not in `org-mode'" file))
4597 (let ((case-fold-search t))
4598 (save-excursion
4599 (save-restriction
4600 (if (eq buffer org-agenda-restrict)
4601 (narrow-to-region org-agenda-restrict-begin
4602 org-agenda-restrict-end)
4603 (widen))
4604 (goto-char (point-min))
4605 (unless (or (org-at-heading-p)
4606 (outline-next-heading))
4607 (throw 'nextfile t))
4608 (goto-char (max (point-min) (1- (point))))
4609 (while (re-search-forward regexp nil t)
4610 (org-back-to-heading t)
4611 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4612 (> (org-reduced-level (org-outline-level))
4613 org-agenda-search-view-max-outline-level)
4614 (forward-line -1)
4615 (org-back-to-heading t)))
4616 (skip-chars-forward "* ")
4617 (setq beg (point-at-bol)
4618 beg1 (point)
4619 end (progn
4620 (outline-next-heading)
4621 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4622 (> (org-reduced-level (org-outline-level))
4623 org-agenda-search-view-max-outline-level)
4624 (forward-line 1)
4625 (outline-next-heading)))
4626 (point)))
4628 (catch :skip
4629 (goto-char beg)
4630 (org-agenda-skip)
4631 (setq str (buffer-substring-no-properties
4632 (point-at-bol)
4633 (if hdl-only (point-at-eol) end)))
4634 (mapc (lambda (wr) (when (string-match wr str)
4635 (goto-char (1- end))
4636 (throw :skip t)))
4637 regexps-)
4638 (mapc (lambda (wr) (unless (string-match wr str)
4639 (goto-char (1- end))
4640 (throw :skip t)))
4641 (if todo-only
4642 (cons (concat "^\\*+[ \t]+"
4643 org-not-done-regexp)
4644 regexps+)
4645 regexps+))
4646 (goto-char beg)
4647 (setq marker (org-agenda-new-marker (point))
4648 category (org-get-category)
4649 level (make-string (org-reduced-level (org-outline-level)) ? )
4650 inherited-tags
4651 (or (eq org-agenda-show-inherited-tags 'always)
4652 (and (listp org-agenda-show-inherited-tags)
4653 (memq 'todo org-agenda-show-inherited-tags))
4654 (and (eq org-agenda-show-inherited-tags t)
4655 (or (eq org-agenda-use-tag-inheritance t)
4656 (memq 'todo org-agenda-use-tag-inheritance))))
4657 tags (org-get-tags-at nil (not inherited-tags))
4658 txt (org-agenda-format-item
4660 (buffer-substring-no-properties
4661 beg1 (point-at-eol))
4662 level category tags t))
4663 (org-add-props txt props
4664 'org-marker marker 'org-hd-marker marker
4665 'org-todo-regexp org-todo-regexp
4666 'level level
4667 'org-complex-heading-regexp org-complex-heading-regexp
4668 'priority 1000
4669 'type "search")
4670 (push txt ee)
4671 (goto-char (1- end))))))))))
4672 (setq rtn (nreverse ee))
4673 (setq rtnall (append rtnall rtn)))
4674 (if org-agenda-overriding-header
4675 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4676 nil 'face 'org-agenda-structure) "\n")
4677 (insert "Search words: ")
4678 (add-text-properties (point-min) (1- (point))
4679 (list 'face 'org-agenda-structure))
4680 (setq pos (point))
4681 (insert string "\n")
4682 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4683 (setq pos (point))
4684 (unless org-agenda-multi
4685 (insert (substitute-command-keys "\
4686 Press `\\[org-agenda-manipulate-query-add]', \
4687 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4688 `\\[org-agenda-manipulate-query-add-re]', \
4689 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4690 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4691 (add-text-properties pos (1- (point))
4692 (list 'face 'org-agenda-structure))))
4693 (org-agenda-mark-header-line (point-min))
4694 (when rtnall
4695 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4696 (goto-char (point-min))
4697 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4698 (add-text-properties (point-min) (point-max)
4699 `(org-agenda-type search
4700 org-last-args (,todo-only ,string ,edit-at)
4701 org-redo-cmd ,org-agenda-redo-command
4702 org-series-cmd ,org-cmd))
4703 (org-agenda-finalize)
4704 (setq buffer-read-only t))))
4706 ;;; Agenda TODO list
4708 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4709 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4710 (concat
4711 (if (or (equal keywords "ALL") (not keywords))
4712 (propertize "ALL" 'face 'warning)
4713 (mapconcat
4714 (lambda (kw)
4715 (propertize kw 'face (org-get-todo-face kw)))
4716 (org-split-string keywords "|")
4717 "|"))
4718 "\n"))
4720 (defvar org-select-this-todo-keyword nil)
4721 (defvar org-last-arg nil)
4723 ;;;###autoload
4724 (defun org-todo-list (&optional arg)
4725 "Show all (not done) TODO entries from all agenda file in a single list.
4726 The prefix arg can be used to select a specific TODO keyword and limit
4727 the list to these. When using `\\[universal-argument]', you will be prompted
4728 for a keyword. A numeric prefix directly selects the Nth keyword in
4729 `org-todo-keywords-1'."
4730 (interactive "P")
4731 (if org-agenda-overriding-arguments
4732 (setq arg org-agenda-overriding-arguments))
4733 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4734 (let* ((today (org-today))
4735 (date (calendar-gregorian-from-absolute today))
4736 (kwds org-todo-keywords-for-agenda)
4737 (completion-ignore-case t)
4738 (org-select-this-todo-keyword
4739 (if (stringp arg) arg
4740 (and arg (integerp arg) (> arg 0)
4741 (nth (1- arg) kwds))))
4742 rtn rtnall files file pos)
4743 (when (equal arg '(4))
4744 (setq org-select-this-todo-keyword
4745 (completing-read "Keyword (or KWD1|K2D2|...): "
4746 (mapcar #'list kwds) nil nil)))
4747 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4748 (catch 'exit
4749 (if org-agenda-sticky
4750 (setq org-agenda-buffer-name
4751 (if (stringp org-select-this-todo-keyword)
4752 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4753 org-select-this-todo-keyword)
4754 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4755 (org-agenda-prepare "TODO")
4756 (org-compile-prefix-format 'todo)
4757 (org-set-sorting-strategy 'todo)
4758 (setq org-agenda-redo-command
4759 `(org-todo-list (or (and (numberp current-prefix-arg)
4760 current-prefix-arg)
4761 ,org-select-this-todo-keyword
4762 current-prefix-arg ,arg)))
4763 (setq files (org-agenda-files nil 'ifmode)
4764 rtnall nil)
4765 (while (setq file (pop files))
4766 (catch 'nextfile
4767 (org-check-agenda-file file)
4768 (setq rtn (org-agenda-get-day-entries file date :todo))
4769 (setq rtnall (append rtnall rtn))))
4770 (if org-agenda-overriding-header
4771 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4772 nil 'face 'org-agenda-structure) "\n")
4773 (insert "Global list of TODO items of type: ")
4774 (add-text-properties (point-min) (1- (point))
4775 (list 'face 'org-agenda-structure
4776 'short-heading
4777 (concat "ToDo: "
4778 (or org-select-this-todo-keyword "ALL"))))
4779 (org-agenda-mark-header-line (point-min))
4780 (insert (org-agenda-propertize-selected-todo-keywords
4781 org-select-this-todo-keyword))
4782 (setq pos (point))
4783 (unless org-agenda-multi
4784 (insert (substitute-command-keys "Available with \
4785 `N \\[org-agenda-redo]': (0)[ALL]"))
4786 (let ((n 0) s)
4787 (mapc (lambda (x)
4788 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4789 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4790 (insert "\n "))
4791 (insert " " s))
4792 kwds))
4793 (insert "\n"))
4794 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4795 (org-agenda-mark-header-line (point-min))
4796 (when rtnall
4797 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4798 (goto-char (point-min))
4799 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4800 (add-text-properties (point-min) (point-max)
4801 `(org-agenda-type todo
4802 org-last-args ,arg
4803 org-redo-cmd ,org-agenda-redo-command
4804 org-series-cmd ,org-cmd))
4805 (org-agenda-finalize)
4806 (setq buffer-read-only t))))
4808 ;;; Agenda tags match
4810 ;;;###autoload
4811 (defun org-tags-view (&optional todo-only match)
4812 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4813 The prefix arg TODO-ONLY limits the search to TODO entries."
4814 (interactive "P")
4815 (if org-agenda-overriding-arguments
4816 (setq todo-only (car org-agenda-overriding-arguments)
4817 match (nth 1 org-agenda-overriding-arguments)))
4818 (let* ((org-tags-match-list-sublevels
4819 org-tags-match-list-sublevels)
4820 (completion-ignore-case t)
4821 (org--matcher-tags-todo-only todo-only)
4822 rtn rtnall files file pos matcher
4823 buffer)
4824 (when (and (stringp match) (not (string-match "\\S-" match)))
4825 (setq match nil))
4826 (catch 'exit
4827 (if org-agenda-sticky
4828 (setq org-agenda-buffer-name
4829 (if (stringp match)
4830 (format "*Org Agenda(%s:%s)*"
4831 (or org-keys (or (and todo-only "M") "m")) match)
4832 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4833 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4834 ;; expanding tags within `org-make-tags-matcher'
4835 (org-agenda-prepare (concat "TAGS " match))
4836 (setq matcher (org-make-tags-matcher match)
4837 match (car matcher)
4838 matcher (cdr matcher))
4839 (org-compile-prefix-format 'tags)
4840 (org-set-sorting-strategy 'tags)
4841 (setq org-agenda-query-string match)
4842 (setq org-agenda-redo-command
4843 (list 'org-tags-view
4844 `(quote ,org--matcher-tags-todo-only)
4845 `(if current-prefix-arg nil ,org-agenda-query-string)))
4846 (setq files (org-agenda-files nil 'ifmode)
4847 rtnall nil)
4848 (while (setq file (pop files))
4849 (catch 'nextfile
4850 (org-check-agenda-file file)
4851 (setq buffer (if (file-exists-p file)
4852 (org-get-agenda-file-buffer file)
4853 (error "No such file %s" file)))
4854 (if (not buffer)
4855 ;; If file does not exist, error message to agenda
4856 (setq rtn (list
4857 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4858 rtnall (append rtnall rtn))
4859 (with-current-buffer buffer
4860 (unless (derived-mode-p 'org-mode)
4861 (error "Agenda file %s is not in `org-mode'" file))
4862 (save-excursion
4863 (save-restriction
4864 (if (eq buffer org-agenda-restrict)
4865 (narrow-to-region org-agenda-restrict-begin
4866 org-agenda-restrict-end)
4867 (widen))
4868 (setq rtn (org-scan-tags 'agenda
4869 matcher
4870 org--matcher-tags-todo-only))
4871 (setq rtnall (append rtnall rtn))))))))
4872 (if org-agenda-overriding-header
4873 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4874 nil 'face 'org-agenda-structure) "\n")
4875 (insert "Headlines with TAGS match: ")
4876 (add-text-properties (point-min) (1- (point))
4877 (list 'face 'org-agenda-structure
4878 'short-heading
4879 (concat "Match: " match)))
4880 (setq pos (point))
4881 (insert match "\n")
4882 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4883 (setq pos (point))
4884 (unless org-agenda-multi
4885 (insert (substitute-command-keys
4886 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4887 to search again with new search string\n")))
4888 (add-text-properties pos (1- (point))
4889 (list 'face 'org-agenda-structure)))
4890 (org-agenda-mark-header-line (point-min))
4891 (when rtnall
4892 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4893 (goto-char (point-min))
4894 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4895 (add-text-properties
4896 (point-min) (point-max)
4897 `(org-agenda-type tags
4898 org-last-args (,org--matcher-tags-todo-only ,match)
4899 org-redo-cmd ,org-agenda-redo-command
4900 org-series-cmd ,org-cmd))
4901 (org-agenda-finalize)
4902 (setq buffer-read-only t))))
4904 ;;; Agenda Finding stuck projects
4906 (defvar org-agenda-skip-regexp nil
4907 "Regular expression used in skipping subtrees for the agenda.
4908 This is basically a temporary global variable that can be set and then
4909 used by user-defined selections using `org-agenda-skip-function'.")
4911 (defvar org-agenda-overriding-header nil
4912 "When set during agenda, todo and tags searches it replaces the header.
4913 This variable should not be set directly, but custom commands can bind it
4914 in the options section.")
4916 (defun org-agenda-skip-entry-when-regexp-matches ()
4917 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4918 If yes, it returns the end position of this entry, causing agenda commands
4919 to skip the entry but continuing the search in the subtree. This is a
4920 function that can be put into `org-agenda-skip-function' for the duration
4921 of a command."
4922 (let ((end (save-excursion (org-end-of-subtree t)))
4923 skip)
4924 (save-excursion
4925 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4926 (and skip end)))
4928 (defun org-agenda-skip-subtree-when-regexp-matches ()
4929 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4930 If yes, it returns the end position of this tree, causing agenda commands
4931 to skip this subtree. This is a function that can be put into
4932 `org-agenda-skip-function' for the duration of a command."
4933 (let ((end (save-excursion (org-end-of-subtree t)))
4934 skip)
4935 (save-excursion
4936 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4937 (and skip end)))
4939 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4940 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4941 If yes, it returns the end position of the current entry (NOT the tree),
4942 causing agenda commands to skip the entry but continuing the search in
4943 the subtree. This is a function that can be put into
4944 `org-agenda-skip-function' for the duration of a command. An important
4945 use of this function is for the stuck project list."
4946 (let ((end (save-excursion (org-end-of-subtree t)))
4947 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4948 skip)
4949 (save-excursion
4950 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4951 (and skip entry-end)))
4953 (defun org-agenda-skip-entry-if (&rest conditions)
4954 "Skip entry if any of CONDITIONS is true.
4955 See `org-agenda-skip-if' for details."
4956 (org-agenda-skip-if nil conditions))
4958 (defun org-agenda-skip-subtree-if (&rest conditions)
4959 "Skip subtree if any of CONDITIONS is true.
4960 See `org-agenda-skip-if' for details."
4961 (org-agenda-skip-if t conditions))
4963 (defun org-agenda-skip-if (subtree conditions)
4964 "Checks current entity for CONDITIONS.
4965 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4966 the entry (i.e. the text before the next heading) is checked.
4968 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4969 from different tests. Valid conditions are:
4971 scheduled Check if there is a scheduled cookie
4972 notscheduled Check if there is no scheduled cookie
4973 deadline Check if there is a deadline
4974 notdeadline Check if there is no deadline
4975 timestamp Check if there is a timestamp (also deadline or scheduled)
4976 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4977 regexp Check if regexp matches
4978 notregexp Check if regexp does not match.
4979 todo Check if TODO keyword matches
4980 nottodo Check if TODO keyword does not match
4982 The regexp is taken from the conditions list, it must come right after
4983 the `regexp' or `notregexp' element.
4985 `todo' and `nottodo' accept as an argument a list of todo
4986 keywords, which may include \"*\" to match any todo keyword.
4988 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4990 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4992 Instead of a list, a keyword class may be given. For example:
4994 (org-agenda-skip-entry-if \\='nottodo \\='done)
4996 would skip entries that haven't been marked with any of \"DONE\"
4997 keywords. Possible classes are: `todo', `done', `any'.
4999 If any of these conditions is met, this function returns the end point of
5000 the entity, causing the search to continue from there. This is a function
5001 that can be put into `org-agenda-skip-function' for the duration of a command."
5002 (let (beg end m)
5003 (org-back-to-heading t)
5004 (setq beg (point)
5005 end (if subtree
5006 (progn (org-end-of-subtree t) (point))
5007 (progn (outline-next-heading) (1- (point)))))
5008 (goto-char beg)
5009 (and
5011 (and (memq 'scheduled conditions)
5012 (re-search-forward org-scheduled-time-regexp end t))
5013 (and (memq 'notscheduled conditions)
5014 (not (re-search-forward org-scheduled-time-regexp end t)))
5015 (and (memq 'deadline conditions)
5016 (re-search-forward org-deadline-time-regexp end t))
5017 (and (memq 'notdeadline conditions)
5018 (not (re-search-forward org-deadline-time-regexp end t)))
5019 (and (memq 'timestamp conditions)
5020 (re-search-forward org-ts-regexp end t))
5021 (and (memq 'nottimestamp conditions)
5022 (not (re-search-forward org-ts-regexp end t)))
5023 (and (setq m (memq 'regexp conditions))
5024 (stringp (nth 1 m))
5025 (re-search-forward (nth 1 m) end t))
5026 (and (setq m (memq 'notregexp conditions))
5027 (stringp (nth 1 m))
5028 (not (re-search-forward (nth 1 m) end t)))
5029 (and (or
5030 (setq m (memq 'nottodo conditions))
5031 (setq m (memq 'todo-unblocked conditions))
5032 (setq m (memq 'nottodo-unblocked conditions))
5033 (setq m (memq 'todo conditions)))
5034 (org-agenda-skip-if-todo m end)))
5035 end)))
5037 (defun org-agenda-skip-if-todo (args end)
5038 "Helper function for `org-agenda-skip-if', do not use it directly.
5039 ARGS is a list with first element either `todo', `nottodo',
5040 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5041 a list of TODO keywords, or a state symbol `todo' or `done' or
5042 `any'."
5043 (let ((kw (car args))
5044 (arg (cadr args))
5045 todo-wds todo-re)
5046 (setq todo-wds
5047 (org-uniquify
5048 (cond
5049 ((listp arg) ;; list of keywords
5050 (if (member "*" arg)
5051 (mapcar 'substring-no-properties org-todo-keywords-1)
5052 arg))
5053 ((symbolp arg) ;; keyword class name
5054 (cond
5055 ((eq arg 'todo)
5056 (org-delete-all org-done-keywords
5057 (mapcar 'substring-no-properties
5058 org-todo-keywords-1)))
5059 ((eq arg 'done) org-done-keywords)
5060 ((eq arg 'any)
5061 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5062 (setq todo-re
5063 (concat "^\\*+[ \t]+\\<\\("
5064 (mapconcat 'identity todo-wds "\\|")
5065 "\\)\\>"))
5066 (cond
5067 ((eq kw 'todo) (re-search-forward todo-re end t))
5068 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5069 ((eq kw 'todo-unblocked)
5070 (catch 'unblocked
5071 (while (re-search-forward todo-re end t)
5072 (or (org-entry-blocked-p) (throw 'unblocked t)))
5073 nil))
5074 ((eq kw 'nottodo-unblocked)
5075 (catch 'unblocked
5076 (while (re-search-forward todo-re end t)
5077 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5081 ;;;###autoload
5082 (defun org-agenda-list-stuck-projects (&rest ignore)
5083 "Create agenda view for projects that are stuck.
5084 Stuck projects are project that have no next actions. For the definitions
5085 of what a project is and how to check if it stuck, customize the variable
5086 `org-stuck-projects'."
5087 (interactive)
5088 (let* ((org-agenda-overriding-header
5089 (or org-agenda-overriding-header "List of stuck projects: "))
5090 (matcher (nth 0 org-stuck-projects))
5091 (todo (nth 1 org-stuck-projects))
5092 (tags (nth 2 org-stuck-projects))
5093 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
5094 (todo-wds
5095 (if (not (member "*" todo)) todo
5096 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
5097 (org-delete-all org-done-keywords-for-agenda
5098 (copy-sequence org-todo-keywords-for-agenda))))
5099 (todo-re (and todo
5100 (format "^\\*+[ \t]+\\(%s\\)\\>"
5101 (mapconcat #'identity todo-wds "\\|"))))
5102 (tags-re (cond ((null tags) nil)
5103 ((member "*" tags)
5104 (eval-when-compile
5105 (concat org-outline-regexp-bol
5106 ".*:[[:alnum:]_@#%]+:[ \t]*$")))
5107 (tags (concat org-outline-regexp-bol
5108 ".*:\\("
5109 (mapconcat #'identity tags "\\|")
5110 "\\):[[:alnum:]_@#%:]*[ \t]*$"))
5111 (t nil)))
5112 (re-list (delq nil (list todo-re tags-re gen-re)))
5113 (skip-re
5114 (if (null re-list)
5115 (error "Missing information to identify unstuck projects")
5116 (mapconcat #'identity re-list "\\|")))
5117 (org-agenda-skip-function
5118 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
5119 ;; in the subtree.
5120 `(lambda ()
5121 (and (save-excursion
5122 (let ((case-fold-search nil))
5123 (re-search-forward
5124 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
5125 (progn (outline-next-heading) (point))))))
5126 (org-tags-view nil matcher)
5127 (setq org-agenda-buffer-name (buffer-name))
5128 (with-current-buffer org-agenda-buffer-name
5129 (setq org-agenda-redo-command
5130 `(org-agenda-list-stuck-projects ,current-prefix-arg))
5131 (let ((inhibit-read-only t))
5132 (add-text-properties
5133 (point-min) (point-max)
5134 `(org-redo-cmd ,org-agenda-redo-command))))))
5136 ;;; Diary integration
5138 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5139 (defvar diary-list-entries-hook)
5140 (defvar diary-time-regexp)
5141 (defun org-get-entries-from-diary (date)
5142 "Get the (Emacs Calendar) diary entries for DATE."
5143 (require 'diary-lib)
5144 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5145 (diary-display-function 'diary-fancy-display)
5146 (pop-up-frames nil)
5147 (diary-list-entries-hook
5148 (cons 'org-diary-default-entry diary-list-entries-hook))
5149 (diary-file-name-prefix nil) ; turn this feature off
5150 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5151 entries
5152 (org-disable-agenda-to-diary t))
5153 (save-excursion
5154 (save-window-excursion
5155 (funcall (if (fboundp 'diary-list-entries)
5156 'diary-list-entries 'list-diary-entries)
5157 date 1)))
5158 (if (not (get-buffer diary-fancy-buffer))
5159 (setq entries nil)
5160 (with-current-buffer diary-fancy-buffer
5161 (setq buffer-read-only nil)
5162 (if (zerop (buffer-size))
5163 ;; No entries
5164 (setq entries nil)
5165 ;; Omit the date and other unnecessary stuff
5166 (org-agenda-cleanup-fancy-diary)
5167 ;; Add prefix to each line and extend the text properties
5168 (if (zerop (buffer-size))
5169 (setq entries nil)
5170 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5171 (setq entries
5172 (with-temp-buffer
5173 (insert entries) (goto-char (point-min))
5174 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5175 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5176 (replace-match (concat "; " (match-string 1)))))
5177 (buffer-string)))))
5178 (set-buffer-modified-p nil)
5179 (kill-buffer diary-fancy-buffer)))
5180 (when entries
5181 (setq entries (org-split-string entries "\n"))
5182 (setq entries
5183 (mapcar
5184 (lambda (x)
5185 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5186 ;; Extend the text properties to the beginning of the line
5187 (org-add-props x (text-properties-at (1- (length x)) x)
5188 'type "diary" 'date date 'face 'org-agenda-diary))
5189 entries)))))
5191 (defvar org-agenda-cleanup-fancy-diary-hook nil
5192 "Hook run when the fancy diary buffer is cleaned up.")
5194 (defun org-agenda-cleanup-fancy-diary ()
5195 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5196 This gets rid of the date, the underline under the date, and
5197 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5198 date. It also removes lines that contain only whitespace."
5199 (goto-char (point-min))
5200 (if (looking-at ".*?:[ \t]*")
5201 (progn
5202 (replace-match "")
5203 (re-search-forward "\n=+$" nil t)
5204 (replace-match "")
5205 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5206 (re-search-forward "\n=+$" nil t)
5207 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5208 (goto-char (point-min))
5209 (while (re-search-forward "^ +\n" nil t)
5210 (replace-match ""))
5211 (goto-char (point-min))
5212 (if (re-search-forward "^Org mode dummy\n?" nil t)
5213 (replace-match ""))
5214 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5216 ;; Make sure entries from the diary have the right text properties.
5217 (eval-after-load "diary-lib"
5218 '(if (boundp 'diary-modify-entry-list-string-function)
5219 ;; We can rely on the hook, nothing to do
5221 ;; Hook not available, must use advice to make this work
5222 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5223 "Make the position visible."
5224 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5225 (stringp string)
5226 buffer-file-name)
5227 (setq string (org-modify-diary-entry-string string))))))
5229 (defun org-modify-diary-entry-string (string)
5230 "Add text properties to string, allowing Org to act on it."
5231 (org-add-props string nil
5232 'mouse-face 'highlight
5233 'help-echo (if buffer-file-name
5234 (format "mouse-2 or RET jump to diary file %s"
5235 (abbreviate-file-name buffer-file-name))
5237 'org-agenda-diary-link t
5238 'org-marker (org-agenda-new-marker (point-at-bol))))
5240 (defun org-diary-default-entry ()
5241 "Add a dummy entry to the diary.
5242 Needed to avoid empty dates which mess up holiday display."
5243 ;; Catch the error if dealing with the new add-to-diary-alist
5244 (when org-disable-agenda-to-diary
5245 (condition-case nil
5246 (org-add-to-diary-list original-date "Org mode dummy" "")
5247 (error
5248 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5250 (defun org-add-to-diary-list (&rest args)
5251 (if (fboundp 'diary-add-to-list)
5252 (apply 'diary-add-to-list args)
5253 (apply 'add-to-diary-list args)))
5255 (defvar org-diary-last-run-time nil)
5257 ;;;###autoload
5258 (defun org-diary (&rest args)
5259 "Return diary information from org files.
5260 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5261 It accesses org files and extracts information from those files to be
5262 listed in the diary. The function accepts arguments specifying what
5263 items should be listed. For a list of arguments allowed here, see the
5264 variable `org-agenda-entry-types'.
5266 The call in the diary file should look like this:
5268 &%%(org-diary) ~/path/to/some/orgfile.org
5270 Use a separate line for each org file to check. Or, if you omit the file name,
5271 all files listed in `org-agenda-files' will be checked automatically:
5273 &%%(org-diary)
5275 If you don't give any arguments (as in the example above), the default value
5276 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5277 So the example above may also be written as
5279 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5281 The function expects the lisp variables `entry' and `date' to be provided
5282 by the caller, because this is how the calendar works. Don't use this
5283 function from a program - use `org-agenda-get-day-entries' instead."
5284 (when (> (- (float-time)
5285 org-agenda-last-marker-time)
5287 ;; I am not sure if this works with sticky agendas, because the marker
5288 ;; list is then no longer a global variable.
5289 (org-agenda-reset-markers))
5290 (org-compile-prefix-format 'agenda)
5291 (org-set-sorting-strategy 'agenda)
5292 (setq args (or args org-agenda-entry-types))
5293 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5294 (list entry)
5295 (org-agenda-files t)))
5296 (time (float-time))
5297 file rtn results)
5298 (when (or (not org-diary-last-run-time)
5299 (> (- time
5300 org-diary-last-run-time)
5302 (org-agenda-prepare-buffers files))
5303 (setq org-diary-last-run-time time)
5304 ;; If this is called during org-agenda, don't return any entries to
5305 ;; the calendar. Org Agenda will list these entries itself.
5306 (if org-disable-agenda-to-diary (setq files nil))
5307 (while (setq file (pop files))
5308 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5309 (setq results (append results rtn)))
5310 (when results
5311 (setq results
5312 (mapcar (lambda (i) (replace-regexp-in-string
5313 org-bracket-link-regexp "\\3" i)) results))
5314 (concat (org-agenda-finalize-entries results) "\n"))))
5316 ;;; Agenda entry finders
5318 (defun org-agenda--timestamp-to-absolute (&rest args)
5319 "Call `org-time-string-to-absolute' with ARGS.
5320 However, throw `:skip' whenever an error is raised."
5321 (condition-case e
5322 (apply #'org-time-string-to-absolute args)
5323 (org-diary-sexp-no-match (throw :skip nil))
5324 (error
5325 (message "%s; Skipping entry" (error-message-string e))
5326 (throw :skip nil))))
5328 (defun org-agenda-get-day-entries (file date &rest args)
5329 "Does the work for `org-diary' and `org-agenda'.
5330 FILE is the path to a file to be checked for entries. DATE is date like
5331 the one returned by `calendar-current-date'. ARGS are symbols indicating
5332 which kind of entries should be extracted. For details about these, see
5333 the documentation of `org-diary'."
5334 (let* ((org-startup-folded nil)
5335 (org-startup-align-all-tables nil)
5336 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5337 (error "No such file %s" file))))
5338 (if (not buffer)
5339 ;; If file does not exist, signal it in diary nonetheless.
5340 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5341 (with-current-buffer buffer
5342 (unless (derived-mode-p 'org-mode)
5343 (error "Agenda file %s is not in `org-mode'" file))
5344 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5345 (setf org-agenda-current-date date)
5346 (save-excursion
5347 (save-restriction
5348 (if (eq buffer org-agenda-restrict)
5349 (narrow-to-region org-agenda-restrict-begin
5350 org-agenda-restrict-end)
5351 (widen))
5352 ;; Rationalize ARGS. Also make sure `:deadline' comes
5353 ;; first in order to populate DEADLINES before passing it.
5355 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5356 ;; guarding us from modifying `org-agenda-entry-types'.
5357 (setf args (org-uniquify (or args org-agenda-entry-types)))
5358 (when (and (memq :scheduled args) (memq :scheduled* args))
5359 (setf args (delq :scheduled* args)))
5360 (cond
5361 ((memq :deadline args)
5362 (setf args (cons :deadline
5363 (delq :deadline (delq :deadline* args)))))
5364 ((memq :deadline* args)
5365 (setf args (cons :deadline* (delq :deadline* args)))))
5366 ;; Collect list of headlines. Return them flattened.
5367 (let ((case-fold-search nil) results deadlines)
5368 (dolist (arg args (apply #'nconc (nreverse results)))
5369 (pcase arg
5370 ((and :todo (guard (org-agenda-today-p date)))
5371 (push (org-agenda-get-todos) results))
5372 (:timestamp
5373 (push (org-agenda-get-blocks) results)
5374 (push (org-agenda-get-timestamps deadlines) results))
5375 (:sexp
5376 (push (org-agenda-get-sexps) results))
5377 (:scheduled
5378 (push (org-agenda-get-scheduled deadlines) results))
5379 (:scheduled*
5380 (push (org-agenda-get-scheduled deadlines t) results))
5381 (:closed
5382 (push (org-agenda-get-progress) results))
5383 (:deadline
5384 (setf deadlines (org-agenda-get-deadlines))
5385 (push deadlines results))
5386 (:deadline*
5387 (setf deadlines (org-agenda-get-deadlines t))
5388 (push deadlines results)))))))))))
5390 (defsubst org-em (x y list)
5391 "Is X or Y a member of LIST?"
5392 (or (memq x list) (memq y list)))
5394 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5395 (defvar org-agenda-sorting-strategy-selected nil)
5397 (defun org-agenda-entry-get-agenda-timestamp (pom)
5398 "Retrieve timestamp information for sorting agenda views.
5399 Given a point or marker POM, returns a cons cell of the timestamp
5400 and the timestamp type relevant for the sorting strategy in
5401 `org-agenda-sorting-strategy-selected'."
5402 (let (ts ts-date-type)
5403 (save-match-data
5404 (cond ((org-em 'scheduled-up 'scheduled-down
5405 org-agenda-sorting-strategy-selected)
5406 (setq ts (org-entry-get pom "SCHEDULED")
5407 ts-date-type " scheduled"))
5408 ((org-em 'deadline-up 'deadline-down
5409 org-agenda-sorting-strategy-selected)
5410 (setq ts (org-entry-get pom "DEADLINE")
5411 ts-date-type " deadline"))
5412 ((org-em 'ts-up 'ts-down
5413 org-agenda-sorting-strategy-selected)
5414 (setq ts (org-entry-get pom "TIMESTAMP")
5415 ts-date-type " timestamp"))
5416 ((org-em 'tsia-up 'tsia-down
5417 org-agenda-sorting-strategy-selected)
5418 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5419 ts-date-type " timestamp_ia"))
5420 ((org-em 'timestamp-up 'timestamp-down
5421 org-agenda-sorting-strategy-selected)
5422 (setq ts (or (org-entry-get pom "SCHEDULED")
5423 (org-entry-get pom "DEADLINE")
5424 (org-entry-get pom "TIMESTAMP")
5425 (org-entry-get pom "TIMESTAMP_IA"))
5426 ts-date-type ""))
5427 (t (setq ts-date-type "")))
5428 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5429 ts-date-type))))
5431 (defun org-agenda-get-todos ()
5432 "Return the TODO information for agenda display."
5433 (let* ((props (list 'face nil
5434 'done-face 'org-agenda-done
5435 'org-not-done-regexp org-not-done-regexp
5436 'org-todo-regexp org-todo-regexp
5437 'org-complex-heading-regexp org-complex-heading-regexp
5438 'mouse-face 'highlight
5439 'help-echo
5440 (format "mouse-2 or RET jump to org file %s"
5441 (abbreviate-file-name buffer-file-name))))
5442 (case-fold-search nil)
5443 (regexp (format org-heading-keyword-regexp-format
5444 (cond
5445 ((and org-select-this-todo-keyword
5446 (equal org-select-this-todo-keyword "*"))
5447 org-todo-regexp)
5448 (org-select-this-todo-keyword
5449 (concat "\\("
5450 (mapconcat 'identity
5451 (org-split-string
5452 org-select-this-todo-keyword
5453 "|")
5454 "\\|") "\\)"))
5455 (t org-not-done-regexp))))
5456 marker priority category level tags todo-state
5457 ts-date ts-date-type ts-date-pair
5458 ee txt beg end inherited-tags todo-state-end-pos)
5459 (goto-char (point-min))
5460 (while (re-search-forward regexp nil t)
5461 (catch :skip
5462 (save-match-data
5463 (beginning-of-line)
5464 (org-agenda-skip)
5465 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5466 (unless (and (setq todo-state (org-get-todo-state))
5467 (setq todo-state-end-pos (match-end 2)))
5468 (goto-char end)
5469 (throw :skip nil))
5470 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5471 (goto-char (1+ beg))
5472 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5473 (throw :skip nil)))
5474 (goto-char (match-beginning 2))
5475 (setq marker (org-agenda-new-marker (match-beginning 0))
5476 category (org-get-category)
5477 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5478 ts-date (car ts-date-pair)
5479 ts-date-type (cdr ts-date-pair)
5480 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5481 inherited-tags
5482 (or (eq org-agenda-show-inherited-tags 'always)
5483 (and (listp org-agenda-show-inherited-tags)
5484 (memq 'todo org-agenda-show-inherited-tags))
5485 (and (eq org-agenda-show-inherited-tags t)
5486 (or (eq org-agenda-use-tag-inheritance t)
5487 (memq 'todo org-agenda-use-tag-inheritance))))
5488 tags (org-get-tags-at nil (not inherited-tags))
5489 level (make-string (org-reduced-level (org-outline-level)) ? )
5490 txt (org-agenda-format-item "" txt level category tags t)
5491 priority (1+ (org-get-priority txt)))
5492 (org-add-props txt props
5493 'org-marker marker 'org-hd-marker marker
5494 'priority priority
5495 'level level
5496 'ts-date ts-date
5497 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5498 (push txt ee)
5499 (if org-agenda-todo-list-sublevels
5500 (goto-char todo-state-end-pos)
5501 (org-end-of-subtree 'invisible))))
5502 (nreverse ee)))
5504 (defun org-agenda-todo-custom-ignore-p (time n)
5505 "Check whether timestamp is farther away than n number of days.
5506 This function is invoked if `org-agenda-todo-ignore-deadlines',
5507 `org-agenda-todo-ignore-scheduled' or
5508 `org-agenda-todo-ignore-timestamp' is set to an integer."
5509 (let ((days (org-time-stamp-to-now
5510 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5511 (if (>= n 0)
5512 (>= days n)
5513 (<= days n))))
5515 ;;;###autoload
5516 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5517 (&optional end)
5518 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5519 (when (or org-agenda-todo-ignore-with-date
5520 org-agenda-todo-ignore-scheduled
5521 org-agenda-todo-ignore-deadlines
5522 org-agenda-todo-ignore-timestamp)
5523 (setq end (or end (save-excursion (outline-next-heading) (point))))
5524 (save-excursion
5525 (or (and org-agenda-todo-ignore-with-date
5526 (re-search-forward org-ts-regexp end t))
5527 (and org-agenda-todo-ignore-scheduled
5528 (re-search-forward org-scheduled-time-regexp end t)
5529 (cond
5530 ((eq org-agenda-todo-ignore-scheduled 'future)
5531 (> (org-time-stamp-to-now
5532 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5533 ((eq org-agenda-todo-ignore-scheduled 'past)
5534 (<= (org-time-stamp-to-now
5535 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5536 ((numberp org-agenda-todo-ignore-scheduled)
5537 (org-agenda-todo-custom-ignore-p
5538 (match-string 1) org-agenda-todo-ignore-scheduled))
5539 (t)))
5540 (and org-agenda-todo-ignore-deadlines
5541 (re-search-forward org-deadline-time-regexp end t)
5542 (cond
5543 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5544 ((eq org-agenda-todo-ignore-deadlines 'far)
5545 (not (org-deadline-close-p (match-string 1))))
5546 ((eq org-agenda-todo-ignore-deadlines 'future)
5547 (> (org-time-stamp-to-now
5548 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5549 ((eq org-agenda-todo-ignore-deadlines 'past)
5550 (<= (org-time-stamp-to-now
5551 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5552 ((numberp org-agenda-todo-ignore-deadlines)
5553 (org-agenda-todo-custom-ignore-p
5554 (match-string 1) org-agenda-todo-ignore-deadlines))
5555 (t (org-deadline-close-p (match-string 1)))))
5556 (and org-agenda-todo-ignore-timestamp
5557 (let ((buffer (current-buffer))
5558 (regexp
5559 (concat
5560 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5561 (start (point)))
5562 ;; Copy current buffer into a temporary one
5563 (with-temp-buffer
5564 (insert-buffer-substring buffer start end)
5565 (goto-char (point-min))
5566 ;; Delete SCHEDULED and DEADLINE items
5567 (while (re-search-forward regexp end t)
5568 (delete-region (match-beginning 0) (match-end 0)))
5569 (goto-char (point-min))
5570 ;; No search for timestamp left
5571 (when (re-search-forward org-ts-regexp nil t)
5572 (cond
5573 ((eq org-agenda-todo-ignore-timestamp 'future)
5574 (> (org-time-stamp-to-now
5575 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5576 ((eq org-agenda-todo-ignore-timestamp 'past)
5577 (<= (org-time-stamp-to-now
5578 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5579 ((numberp org-agenda-todo-ignore-timestamp)
5580 (org-agenda-todo-custom-ignore-p
5581 (match-string 1) org-agenda-todo-ignore-timestamp))
5582 (t))))))))))
5584 (defun org-agenda-get-timestamps (&optional deadlines)
5585 "Return the date stamp information for agenda display.
5586 Optional argument DEADLINES is a list of deadline items to be
5587 displayed in agenda view."
5588 (let* ((props (list 'face 'org-agenda-calendar-event
5589 'org-not-done-regexp org-not-done-regexp
5590 'org-todo-regexp org-todo-regexp
5591 'org-complex-heading-regexp org-complex-heading-regexp
5592 'mouse-face 'highlight
5593 'help-echo
5594 (format "mouse-2 or RET jump to Org file %s"
5595 (abbreviate-file-name buffer-file-name))))
5596 (current (calendar-absolute-from-gregorian date))
5597 (today (org-today))
5598 (deadline-position-alist
5599 (mapcar (lambda (d)
5600 (let ((m (get-text-property 0 'org-hd-marker d)))
5601 (and m (marker-position m))))
5602 deadlines))
5603 ;; Match time-stamps set to current date, time-stamps with
5604 ;; a repeater, and S-exp time-stamps.
5605 (regexp
5606 (concat
5607 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5608 (regexp-quote
5609 (substring
5610 (format-time-string
5611 (car org-time-stamp-formats)
5612 (apply #'encode-time ; DATE bound by calendar
5613 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5614 1 11))
5615 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5616 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5617 timestamp-items)
5618 (goto-char (point-min))
5619 (while (re-search-forward regexp nil t)
5620 ;; Skip date ranges, scheduled and deadlines, which are handled
5621 ;; specially. Also skip time-stamps before first headline as
5622 ;; there would be no entry to add to the agenda. Eventually,
5623 ;; ignore clock entries.
5624 (catch :skip
5625 (save-match-data
5626 (when (or (org-at-date-range-p)
5627 (org-at-planning-p)
5628 (org-before-first-heading-p)
5629 (and org-agenda-include-inactive-timestamps
5630 (org-at-clock-log-p)))
5631 (throw :skip nil))
5632 (org-agenda-skip))
5633 (let* ((pos (match-beginning 0))
5634 (repeat (match-string 1))
5635 (sexp-entry (match-string 3))
5636 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5637 (save-excursion
5638 (goto-char pos)
5639 (looking-at org-ts-regexp-both)
5640 (match-string 0))))
5641 (todo-state (org-get-todo-state))
5642 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5643 (member todo-state
5644 org-agenda-repeating-timestamp-show-all)))
5645 (warntime (get-text-property (point) 'org-appt-warntime))
5646 (done? (member todo-state org-done-keywords)))
5647 ;; Possibly skip done tasks.
5648 (when (and done? org-agenda-skip-timestamp-if-done)
5649 (throw :skip t))
5650 ;; S-exp entry doesn't match current day: skip it.
5651 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5652 (throw :skip nil))
5653 ;; When time-stamp doesn't match CURRENT but has a repeater,
5654 ;; make sure it repeats on CURRENT. Furthermore, if
5655 ;; SHOW-ALL is nil, ensure that repeats are only the first
5656 ;; before and the first after today.
5657 (when (and repeat
5658 (if show-all
5659 (/= current
5660 (org-agenda--timestamp-to-absolute
5661 repeat current 'future (current-buffer) pos))
5662 (and (/= current
5663 (org-agenda--timestamp-to-absolute
5664 repeat today 'past (current-buffer) pos))
5665 (/= current
5666 (org-agenda--timestamp-to-absolute
5667 repeat today 'future (current-buffer) pos)))))
5668 (throw :skip nil))
5669 (save-excursion
5670 (re-search-backward org-outline-regexp-bol nil t)
5671 ;; Possibly skip time-stamp when a deadline is set.
5672 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5673 (assq (point) deadline-position-alist))
5674 (throw :skip nil))
5675 (let* ((category (org-get-category pos))
5676 (inherited-tags
5677 (or (eq org-agenda-show-inherited-tags 'always)
5678 (and (consp org-agenda-show-inherited-tags)
5679 (memq 'agenda org-agenda-show-inherited-tags))
5680 (and (eq org-agenda-show-inherited-tags t)
5681 (or (eq org-agenda-use-tag-inheritance t)
5682 (memq 'agenda
5683 org-agenda-use-tag-inheritance)))))
5684 (tags (org-get-tags-at nil (not inherited-tags)))
5685 (level (make-string (org-reduced-level (org-outline-level))
5686 ?\s))
5687 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5688 (match-string 1)))
5689 (inactive? (= (char-after pos) ?\[))
5690 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5691 (item
5692 (org-agenda-format-item
5693 (and inactive? org-agenda-inactive-leader)
5694 head level category tags time-stamp org-ts-regexp habit?)))
5695 (org-add-props item props
5696 'priority (if habit?
5697 (org-habit-get-priority (org-habit-parse-todo))
5698 (org-get-priority item))
5699 'org-marker (org-agenda-new-marker pos)
5700 'org-hd-marker (org-agenda-new-marker)
5701 'date date
5702 'level level
5703 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5704 current)
5705 'todo-state todo-state
5706 'warntime warntime
5707 'type "timestamp")
5708 (push item timestamp-items))))
5709 (when org-agenda-skip-additional-timestamps-same-entry
5710 (outline-next-heading))))
5711 (nreverse timestamp-items)))
5713 (defun org-agenda-get-sexps ()
5714 "Return the sexp information for agenda display."
5715 (require 'diary-lib)
5716 (let* ((props (list 'face 'org-agenda-calendar-sexp
5717 'mouse-face 'highlight
5718 'help-echo
5719 (format "mouse-2 or RET jump to org file %s"
5720 (abbreviate-file-name buffer-file-name))))
5721 (regexp "^&?%%(")
5722 marker category extra level ee txt tags entry
5723 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5724 (goto-char (point-min))
5725 (while (re-search-forward regexp nil t)
5726 (catch :skip
5727 (org-agenda-skip)
5728 (setq beg (match-beginning 0))
5729 (goto-char (1- (match-end 0)))
5730 (setq b (point))
5731 (forward-sexp 1)
5732 (setq sexp (buffer-substring b (point)))
5733 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5734 (org-trim (match-string 1))
5735 ""))
5736 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5737 (when result
5738 (setq marker (org-agenda-new-marker beg)
5739 level (make-string (org-reduced-level (org-outline-level)) ? )
5740 category (org-get-category beg)
5741 inherited-tags
5742 (or (eq org-agenda-show-inherited-tags 'always)
5743 (and (listp org-agenda-show-inherited-tags)
5744 (memq 'agenda org-agenda-show-inherited-tags))
5745 (and (eq org-agenda-show-inherited-tags t)
5746 (or (eq org-agenda-use-tag-inheritance t)
5747 (memq 'agenda org-agenda-use-tag-inheritance))))
5748 tags (org-get-tags-at nil (not inherited-tags))
5749 todo-state (org-get-todo-state)
5750 warntime (get-text-property (point) 'org-appt-warntime)
5751 extra nil)
5753 (dolist (r (if (stringp result)
5754 (list result)
5755 result)) ;; we expect a list here
5756 (when (and org-agenda-diary-sexp-prefix
5757 (string-match org-agenda-diary-sexp-prefix r))
5758 (setq extra (match-string 0 r)
5759 r (replace-match "" nil nil r)))
5760 (if (string-match "\\S-" r)
5761 (setq txt r)
5762 (setq txt "SEXP entry returned empty string"))
5763 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5764 (org-add-props txt props 'org-marker marker
5765 'date date 'todo-state todo-state
5766 'level level 'type "sexp" 'warntime warntime)
5767 (push txt ee)))))
5768 (nreverse ee)))
5770 ;; Calendar sanity: define some functions that are independent of
5771 ;; `calendar-date-style'.
5772 (defun org-anniversary (year month day &optional mark)
5773 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5774 (with-no-warnings
5775 (let ((calendar-date-style 'iso))
5776 (diary-anniversary year month day mark))))
5777 (defun org-cyclic (N year month day &optional mark)
5778 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5779 (with-no-warnings
5780 (let ((calendar-date-style 'iso))
5781 (diary-cyclic N year month day mark))))
5782 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5783 "Like `diary-block', but with fixed (ISO) order of arguments."
5784 (with-no-warnings
5785 (let ((calendar-date-style 'iso))
5786 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5787 (defun org-date (year month day &optional mark)
5788 "Like `diary-date', but with fixed (ISO) order of arguments."
5789 (with-no-warnings
5790 (let ((calendar-date-style 'iso))
5791 (diary-date year month day mark))))
5793 ;; Define the `org-class' function
5794 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5795 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5796 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5797 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5798 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5799 `holidays', then any date that is known by the Emacs calendar to be a
5800 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5801 then those holidays will be skipped."
5802 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5803 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5804 (d (calendar-absolute-from-gregorian date))
5805 (h (when skip-weeks (calendar-check-holidays date))))
5806 (and
5807 (<= date1 d)
5808 (<= d date2)
5809 (= (calendar-day-of-week date) dayname)
5810 (or (not skip-weeks)
5811 (progn
5812 (require 'cal-iso)
5813 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5814 (not (or (and h (memq 'holidays skip-weeks))
5815 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5816 entry)))
5818 (defalias 'org-get-closed 'org-agenda-get-progress)
5819 (defun org-agenda-get-progress ()
5820 "Return the logged TODO entries for agenda display."
5821 (let* ((props (list 'mouse-face 'highlight
5822 'org-not-done-regexp org-not-done-regexp
5823 'org-todo-regexp org-todo-regexp
5824 'org-complex-heading-regexp org-complex-heading-regexp
5825 'help-echo
5826 (format "mouse-2 or RET jump to org file %s"
5827 (abbreviate-file-name buffer-file-name))))
5828 (items (if (consp org-agenda-show-log-scoped)
5829 org-agenda-show-log-scoped
5830 (if (eq org-agenda-show-log-scoped 'clockcheck)
5831 '(clock)
5832 org-agenda-log-mode-items)))
5833 (parts
5834 (delq nil
5835 (list
5836 (if (memq 'closed items) (concat "\\<" org-closed-string))
5837 (if (memq 'clock items) (concat "\\<" org-clock-string))
5838 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5839 (parts-re (if parts (mapconcat 'identity parts "\\|")
5840 (error "`org-agenda-log-mode-items' is empty")))
5841 (regexp (concat
5842 "\\(" parts-re "\\)"
5843 " *\\["
5844 (regexp-quote
5845 (substring
5846 (format-time-string
5847 (car org-time-stamp-formats)
5848 (apply 'encode-time ; DATE bound by calendar
5849 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5850 1 11))))
5851 (org-agenda-search-headline-for-time nil)
5852 marker hdmarker priority category level tags closedp
5853 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5854 (goto-char (point-min))
5855 (while (re-search-forward regexp nil t)
5856 (catch :skip
5857 (org-agenda-skip)
5858 (setq marker (org-agenda-new-marker (match-beginning 0))
5859 closedp (equal (match-string 1) org-closed-string)
5860 statep (equal (string-to-char (match-string 1)) ?-)
5861 clockp (not (or closedp statep))
5862 state (and statep (match-string 2))
5863 category (org-get-category (match-beginning 0))
5864 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5865 (when (string-match "\\]" timestr)
5866 ;; substring should only run to end of time stamp
5867 (setq rest (substring timestr (match-end 0))
5868 timestr (substring timestr 0 (match-end 0)))
5869 (if (and (not closedp) (not statep)
5870 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5871 rest))
5872 (progn (setq timestr (concat (substring timestr 0 -1)
5873 "-" (match-string 1 rest) "]"))
5874 (setq clocked (match-string 2 rest)))
5875 (setq clocked "-")))
5876 (save-excursion
5877 (setq extra
5878 (cond
5879 ((not org-agenda-log-mode-add-notes) nil)
5880 (statep
5881 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5882 (match-string 1)))
5883 (clockp
5884 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5885 (match-string 1)))))
5886 (if (not (re-search-backward org-outline-regexp-bol nil t))
5887 (throw :skip nil)
5888 (goto-char (match-beginning 0))
5889 (setq hdmarker (org-agenda-new-marker)
5890 inherited-tags
5891 (or (eq org-agenda-show-inherited-tags 'always)
5892 (and (listp org-agenda-show-inherited-tags)
5893 (memq 'todo org-agenda-show-inherited-tags))
5894 (and (eq org-agenda-show-inherited-tags t)
5895 (or (eq org-agenda-use-tag-inheritance t)
5896 (memq 'todo org-agenda-use-tag-inheritance))))
5897 tags (org-get-tags-at nil (not inherited-tags))
5898 level (make-string (org-reduced-level (org-outline-level)) ? ))
5899 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5900 (setq txt (match-string 1))
5901 (when extra
5902 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5903 (setq txt (concat (substring txt 0 (match-beginning 1))
5904 " - " extra " " (match-string 2 txt)))
5905 (setq txt (concat txt " - " extra))))
5906 (setq txt (org-agenda-format-item
5907 (cond
5908 (closedp "Closed: ")
5909 (statep (concat "State: (" state ")"))
5910 (t (concat "Clocked: (" clocked ")")))
5911 txt level category tags timestr)))
5912 (setq priority 100000)
5913 (org-add-props txt props
5914 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5915 'priority priority 'level level
5916 'type "closed" 'date date
5917 'undone-face 'org-warning 'done-face 'org-agenda-done)
5918 (push txt ee))
5919 (goto-char (point-at-eol))))
5920 (nreverse ee)))
5922 (defun org-agenda-show-clocking-issues ()
5923 "Add overlays, showing issues with clocking.
5924 See also the user option `org-agenda-clock-consistency-checks'."
5925 (interactive)
5926 (let* ((org-time-clocksum-use-effort-durations nil)
5927 (pl org-agenda-clock-consistency-checks)
5928 (re (concat "^[ \t]*"
5929 org-clock-string
5930 "[ \t]+"
5931 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5932 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5933 (tlstart 0.)
5934 (tlend 0.)
5935 (maxtime (org-hh:mm-string-to-minutes
5936 (or (plist-get pl :max-duration) "24:00")))
5937 (mintime (org-hh:mm-string-to-minutes
5938 (or (plist-get pl :min-duration) 0)))
5939 (maxgap (org-hh:mm-string-to-minutes
5940 ;; default 30:00 means never complain
5941 (or (plist-get pl :max-gap) "30:00")))
5942 (gapok (mapcar 'org-hh:mm-string-to-minutes
5943 (plist-get pl :gap-ok-around)))
5944 (def-face (or (plist-get pl :default-face)
5945 '((:background "DarkRed") (:foreground "white"))))
5946 issue face m te ts dt ov)
5947 (goto-char (point-min))
5948 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5949 (setq issue nil face def-face)
5950 (catch 'next
5951 (setq m (org-get-at-bol 'org-marker)
5952 te nil ts nil)
5953 (unless (and m (markerp m))
5954 (setq issue "No valid clock line") (throw 'next t))
5955 (org-with-point-at m
5956 (save-excursion
5957 (goto-char (point-at-bol))
5958 (unless (looking-at re)
5959 (error "No valid Clock line")
5960 (throw 'next t))
5961 (unless (match-end 3)
5962 (setq issue "No end time"
5963 face (or (plist-get pl :no-end-time-face) face))
5964 (throw 'next t))
5965 (setq ts (match-string 1)
5966 te (match-string 3)
5967 ts (float-time
5968 (apply #'encode-time (org-parse-time-string ts)))
5969 te (float-time
5970 (apply #'encode-time (org-parse-time-string te)))
5971 dt (- te ts))))
5972 (cond
5973 ((> dt (* 60 maxtime))
5974 ;; a very long clocking chunk
5975 (setq issue (format "Clocking interval is very long: %s"
5976 (org-minutes-to-clocksum-string
5977 (floor (/ (float dt) 60.))))
5978 face (or (plist-get pl :long-face) face)))
5979 ((< dt (* 60 mintime))
5980 ;; a very short clocking chunk
5981 (setq issue (format "Clocking interval is very short: %s"
5982 (org-minutes-to-clocksum-string
5983 (floor (/ (float dt) 60.))))
5984 face (or (plist-get pl :short-face) face)))
5985 ((and (> tlend 0) (< ts tlend))
5986 ;; Two clock entries are overlapping
5987 (setq issue (format "Clocking overlap: %d minutes"
5988 (/ (- tlend ts) 60))
5989 face (or (plist-get pl :overlap-face) face)))
5990 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5991 ;; There is a gap, lets see if we need to report it
5992 (unless (org-agenda-check-clock-gap tlend ts gapok)
5993 (setq issue (format "Clocking gap: %d minutes"
5994 (/ (- ts tlend) 60))
5995 face (or (plist-get pl :gap-face) face))))
5996 (t nil)))
5997 (setq tlend (or te tlend) tlstart (or ts tlstart))
5998 (when issue
5999 ;; OK, there was some issue, add an overlay to show the issue
6000 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
6001 (overlay-put ov 'before-string
6002 (concat
6003 (org-add-props
6004 (format "%-43s" (concat " " issue))
6006 'face face)
6007 "\n"))
6008 (overlay-put ov 'evaporate t)))))
6010 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
6011 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
6012 (catch 'exit
6013 (unless ok-list
6014 ;; there are no OK times for gaps...
6015 (throw 'exit nil))
6016 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
6017 ;; This is more than 24 hours, so it is OK.
6018 ;; because we have at least one OK time, that must be in the
6019 ;; 24 hour interval.
6020 (throw 'exit t))
6021 ;; We have a shorter gap.
6022 ;; Now we have to get the minute of the day when these times are
6023 (let* ((t1dec (decode-time (seconds-to-time t1)))
6024 (t2dec (decode-time (seconds-to-time t2)))
6025 ;; compute the minute on the day
6026 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6027 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6028 (when (< min2 min1)
6029 ;; if min2 is smaller than min1, this means it is on the next day.
6030 ;; Wrap it to after midnight.
6031 (setq min2 (+ min2 1440)))
6032 ;; Now check if any of the OK times is in the gap
6033 (mapc (lambda (x)
6034 ;; Wrap the time to after midnight if necessary
6035 (if (< x min1) (setq x (+ x 1440)))
6036 ;; Check if in interval
6037 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6038 ok-list)
6039 ;; Nope, this gap is not OK
6040 nil)))
6042 (defun org-agenda-get-deadlines (&optional with-hour)
6043 "Return the deadline information for agenda display.
6044 When WITH-HOUR is non-nil, only return deadlines with an hour
6045 specification like [h]h:mm."
6046 (let* ((props (list 'mouse-face 'highlight
6047 'org-not-done-regexp org-not-done-regexp
6048 'org-todo-regexp org-todo-regexp
6049 'org-complex-heading-regexp org-complex-heading-regexp
6050 'help-echo
6051 (format "mouse-2 or RET jump to org file %s"
6052 (abbreviate-file-name buffer-file-name))))
6053 (regexp (if with-hour
6054 org-deadline-time-hour-regexp
6055 org-deadline-time-regexp))
6056 (today (org-today))
6057 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
6058 (current (calendar-absolute-from-gregorian date))
6059 deadline-items)
6060 (goto-char (point-min))
6061 (while (re-search-forward regexp nil t)
6062 (catch :skip
6063 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6064 (org-agenda-skip)
6065 (let* ((s (match-string 1))
6066 (pos (1- (match-beginning 1)))
6067 (todo-state (save-match-data (org-get-todo-state)))
6068 (done? (member todo-state org-done-keywords))
6069 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6070 (member todo-state
6071 org-agenda-repeating-timestamp-show-all)))
6072 (sexp? (string-prefix-p "%%" s))
6073 ;; DEADLINE is the bare deadline date, i.e., without
6074 ;; any repeater, or the last repeat if SHOW-ALL is
6075 ;; non-nil. REPEAT is closest repeat after CURRENT, if
6076 ;; all repeated time stamps are to be shown, or after
6077 ;; TODAY otherwise. REPEAT only applies to future
6078 ;; dates.
6079 (deadline (cond
6080 (sexp? (org-agenda--timestamp-to-absolute s current))
6081 (show-all (org-agenda--timestamp-to-absolute s))
6082 (t (org-agenda--timestamp-to-absolute
6083 s today 'past (current-buffer) pos))))
6084 (repeat (cond (sexp? deadline)
6085 ((< current today) deadline)
6087 (org-agenda--timestamp-to-absolute
6088 s (if show-all current today) 'future
6089 (current-buffer) pos))))
6090 (diff (- deadline current))
6091 (suppress-prewarning
6092 (let ((scheduled
6093 (and org-agenda-skip-deadline-prewarning-if-scheduled
6094 (org-entry-get nil "SCHEDULED"))))
6095 (cond
6096 ((not scheduled) nil)
6097 ;; The current item has a scheduled date, so
6098 ;; evaluate its prewarning lead time.
6099 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6100 ;; Use global prewarning-restart lead time.
6101 org-agenda-skip-deadline-prewarning-if-scheduled)
6102 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6103 'pre-scheduled)
6104 ;; Set pre-warning to no earlier than SCHEDULED.
6105 (min (- deadline
6106 (org-agenda--timestamp-to-absolute scheduled))
6107 org-deadline-warning-days))
6108 ;; Set pre-warning to deadline.
6109 (t 0))))
6110 (wdays (if suppress-prewarning
6111 (let ((org-deadline-warning-days suppress-prewarning))
6112 (org-get-wdays s))
6113 (org-get-wdays s))))
6114 ;; When to show a deadline in the calendar: if the
6115 ;; expiration is within WDAYS warning time. Past-due
6116 ;; deadlines are only shown on today agenda.
6117 (when (cond ((= current deadline) nil)
6118 ((< deadline today)
6119 (and (not today?)
6120 (or (< current today) (/= repeat current))))
6121 ((> deadline current)
6122 (or (not today?) (> diff wdays)))
6123 (t (/= repeat current)))
6124 (throw :skip nil))
6125 ;; Possibly skip done tasks.
6126 (when (and done?
6127 (or org-agenda-skip-deadline-if-done
6128 (/= deadline current)))
6129 (throw :skip nil))
6130 (save-excursion
6131 (re-search-backward "^\\*+[ \t]+" nil t)
6132 (goto-char (match-end 0))
6133 (let* ((category (org-get-category))
6134 (level
6135 (make-string (org-reduced-level (org-outline-level)) ?\s))
6136 (head (buffer-substring (point) (line-end-position)))
6137 (inherited-tags
6138 (or (eq org-agenda-show-inherited-tags 'always)
6139 (and (listp org-agenda-show-inherited-tags)
6140 (memq 'agenda org-agenda-show-inherited-tags))
6141 (and (eq org-agenda-show-inherited-tags t)
6142 (or (eq org-agenda-use-tag-inheritance t)
6143 (memq 'agenda
6144 org-agenda-use-tag-inheritance)))))
6145 (tags (org-get-tags-at nil (not inherited-tags)))
6146 (time
6147 (cond
6148 ;; No time of day designation if it is only
6149 ;; a reminder.
6150 ((and (/= current deadline) (/= current repeat)) nil)
6151 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6152 (concat (substring s (match-beginning 1)) " "))
6153 (t 'time)))
6154 (item
6155 (org-agenda-format-item
6156 ;; Insert appropriate suffixes before deadlines.
6157 (pcase-let ((`(,now ,future ,past)
6158 org-agenda-deadline-leaders))
6159 (cond
6160 ;; Future (i.e., repeated) deadlines are
6161 ;; displayed as new headlines.
6162 ((> current today) now)
6163 ;; When SHOW-ALL is nil, prefer repeated
6164 ;; deadlines over reminders of past deadlines.
6165 ((and (not show-all) (= repeat today)) now)
6166 ((= deadline current) now)
6167 ((< deadline current) (format past (- diff)))
6168 (t (format future diff))))
6169 head level category tags
6170 (and (or (= repeat current) (= deadline current))
6171 time)))
6172 (face (org-agenda-deadline-face
6173 (- 1 (/ (float (- deadline current)) (max wdays 1)))))
6174 (upcoming? (and today? (> deadline today)))
6175 (warntime (get-text-property (point) 'org-appt-warntime)))
6176 (org-add-props item props
6177 'org-marker (org-agenda-new-marker pos)
6178 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6179 'warntime warntime
6180 'level level
6181 'ts-date deadline
6182 'priority
6183 ;; Adjust priority to today reminders about deadlines.
6184 ;; Overdue deadlines get the highest priority
6185 ;; increase, then imminent deadlines and eventually
6186 ;; more distant deadlines.
6187 (let ((adjust (cond ((not today?) 0)
6188 ((and (not show-all) (= repeat current)) 0)
6189 (t (- diff)))))
6190 (+ adjust (org-get-priority item)))
6191 'todo-state todo-state
6192 'type (if upcoming? "upcoming-deadline" "deadline")
6193 'date (if upcoming? date deadline)
6194 'face (if done? 'org-agenda-done face)
6195 'undone-face face
6196 'done-face 'org-agenda-done)
6197 (push item deadline-items))))))
6198 (nreverse deadline-items)))
6200 (defun org-agenda-deadline-face (fraction)
6201 "Return the face to displaying a deadline item.
6202 FRACTION is what fraction of the head-warning time has passed."
6203 (assoc-default fraction org-agenda-deadline-faces #'<=))
6205 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6206 "Return the scheduled information for agenda display.
6207 Optional argument DEADLINES is a list of deadline items to be
6208 displayed in agenda view. When WITH-HOUR is non-nil, only return
6209 scheduled items with an hour specification like [h]h:mm."
6210 (let* ((props (list '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 'done-face 'org-agenda-done
6214 'mouse-face 'highlight
6215 'help-echo
6216 (format "mouse-2 or RET jump to Org file %s"
6217 (abbreviate-file-name buffer-file-name))))
6218 (regexp (if with-hour
6219 org-scheduled-time-hour-regexp
6220 org-scheduled-time-regexp))
6221 (today (org-today))
6222 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6223 (current (calendar-absolute-from-gregorian date))
6224 (deadline-pos
6225 (mapcar (lambda (d)
6226 (let ((m (get-text-property 0 'org-hd-marker d)))
6227 (and m (marker-position m))))
6228 deadlines))
6229 scheduled-items)
6230 (goto-char (point-min))
6231 (while (re-search-forward regexp nil t)
6232 (catch :skip
6233 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6234 (org-agenda-skip)
6235 (let* ((s (match-string 1))
6236 (pos (1- (match-beginning 1)))
6237 (todo-state (save-match-data (org-get-todo-state)))
6238 (donep (member todo-state org-done-keywords))
6239 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6240 (member todo-state
6241 org-agenda-repeating-timestamp-show-all)))
6242 (sexp? (string-prefix-p "%%" s))
6243 ;; SCHEDULE is the bare scheduled date, i.e., without
6244 ;; any repeater if non-nil, or last repeat if SHOW-ALL
6245 ;; is nil. REPEAT is the closest repeat after CURRENT,
6246 ;; if all repeated time stamps are to be shown, or
6247 ;; after TODAY otherwise. REPEAT only applies to
6248 ;; future dates.
6249 (schedule (cond
6250 (sexp? (org-agenda--timestamp-to-absolute s current))
6251 (show-all (org-agenda--timestamp-to-absolute s))
6252 (t (org-agenda--timestamp-to-absolute
6253 s today 'past (current-buffer) pos))))
6254 (repeat (cond
6255 (sexp? schedule)
6256 ((< current today) schedule)
6258 (org-agenda--timestamp-to-absolute
6259 s (if show-all current today) 'future
6260 (current-buffer) pos))))
6261 (diff (- current schedule))
6262 (warntime (get-text-property (point) 'org-appt-warntime))
6263 (pastschedp (< schedule today))
6264 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6265 (suppress-delay
6266 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6267 (org-entry-get nil "DEADLINE"))))
6268 (cond
6269 ((not deadline) nil)
6270 ;; The current item has a deadline date, so
6271 ;; evaluate its delay time.
6272 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6273 ;; Use global delay time.
6274 (- org-agenda-skip-scheduled-delay-if-deadline))
6275 ((eq org-agenda-skip-scheduled-delay-if-deadline
6276 'post-deadline)
6277 ;; Set delay to no later than DEADLINE.
6278 (min (- schedule
6279 (org-agenda--timestamp-to-absolute deadline))
6280 org-scheduled-delay-days))
6281 (t 0))))
6282 (ddays
6283 (cond
6284 ;; Nullify delay when a repeater triggered already
6285 ;; and the delay is of the form --Xd.
6286 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6287 (> current schedule))
6289 (suppress-delay
6290 (let ((org-scheduled-delay-days suppress-delay))
6291 (org-get-wdays s t t)))
6292 (t (org-get-wdays s t)))))
6293 ;; Display scheduled items at base date (SCHEDULE), today if
6294 ;; scheduled before the current date, and at any repeat past
6295 ;; today. However, skip delayed items and items that have
6296 ;; been displayed for more than `org-scheduled-past-days'.
6297 (unless (and todayp
6298 habitp
6299 (bound-and-true-p org-habit-show-all-today))
6300 (when (or (and (> ddays 0) (< diff ddays))
6301 (> diff org-scheduled-past-days)
6302 (> schedule current)
6303 (and (< schedule current)
6304 (not todayp)
6305 (/= repeat current)))
6306 (throw :skip nil)))
6307 ;; Possibly skip done tasks.
6308 (when (and donep
6309 (or org-agenda-skip-scheduled-if-done
6310 (/= schedule current)))
6311 (throw :skip nil))
6312 ;; Skip entry if it already appears as a deadline, per
6313 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6314 ;; doesn't apply to habits.
6315 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6316 ((guard
6317 (or (not (memq (line-beginning-position 0) deadline-pos))
6318 habitp))
6319 nil)
6320 (`repeated-after-deadline
6321 (>= repeat (time-to-days (org-get-deadline-time (point)))))
6322 (`not-today pastschedp)
6323 (`t t)
6324 (_ nil))
6325 (throw :skip nil))
6326 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6327 ;; only show them for today. Also skip done habits.
6328 (when (and habitp
6329 (or donep
6330 (not (bound-and-true-p org-habit-show-habits))
6331 (and (not todayp)
6332 (bound-and-true-p
6333 org-habit-show-habits-only-for-today))))
6334 (throw :skip nil))
6335 (save-excursion
6336 (re-search-backward "^\\*+[ \t]+" nil t)
6337 (goto-char (match-end 0))
6338 (let* ((category (org-get-category))
6339 (inherited-tags
6340 (or (eq org-agenda-show-inherited-tags 'always)
6341 (and (listp org-agenda-show-inherited-tags)
6342 (memq 'agenda org-agenda-show-inherited-tags))
6343 (and (eq org-agenda-show-inherited-tags t)
6344 (or (eq org-agenda-use-tag-inheritance t)
6345 (memq 'agenda
6346 org-agenda-use-tag-inheritance)))))
6347 (tags (org-get-tags-at nil (not inherited-tags)))
6348 (level
6349 (make-string (org-reduced-level (org-outline-level)) ?\s))
6350 (head (buffer-substring (point) (line-end-position)))
6351 (time
6352 (cond
6353 ;; No time of day designation if it is only
6354 ;; a reminder.
6355 ((and (/= current schedule) (/= current repeat)) nil)
6356 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6357 (concat (substring s (match-beginning 1)) " "))
6358 (t 'time)))
6359 (item
6360 (org-agenda-format-item
6361 (pcase-let ((`(,first ,next) org-agenda-scheduled-leaders))
6362 (cond
6363 ;; If CURRENT is in the future, don't use past
6364 ;; scheduled prefix.
6365 ((> current today) first)
6366 ;; SHOW-ALL focuses on future repeats. If one
6367 ;; such repeat happens today, ignore late
6368 ;; schedule reminder. However, still report
6369 ;; such reminders when repeat happens later.
6370 ((and (not show-all) (= repeat today)) first)
6371 ;; Initial report.
6372 ((= schedule current) first)
6373 ;; Subsequent reminders. Count from base
6374 ;; schedule.
6375 (t (format next diff))))
6376 head level category tags time nil habitp))
6377 (face (cond ((and (not habitp) pastschedp)
6378 'org-scheduled-previously)
6379 (todayp 'org-scheduled-today)
6380 (t 'org-scheduled)))
6381 (habitp (and habitp (org-habit-parse-todo))))
6382 (org-add-props item props
6383 'undone-face face
6384 'face (if donep 'org-agenda-done face)
6385 'org-marker (org-agenda-new-marker pos)
6386 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6387 'type (if pastschedp "past-scheduled" "scheduled")
6388 'date (if pastschedp schedule date)
6389 'ts-date schedule
6390 'warntime warntime
6391 'level level
6392 'priority (if habitp (org-habit-get-priority habitp)
6393 (+ 99 diff (org-get-priority item)))
6394 'org-habit-p habitp
6395 'todo-state todo-state)
6396 (push item scheduled-items))))))
6397 (nreverse scheduled-items)))
6399 (defun org-agenda-get-blocks ()
6400 "Return the date-range information for agenda display."
6401 (let* ((props (list 'face nil
6402 'org-not-done-regexp org-not-done-regexp
6403 'org-todo-regexp org-todo-regexp
6404 'org-complex-heading-regexp org-complex-heading-regexp
6405 'mouse-face 'highlight
6406 'help-echo
6407 (format "mouse-2 or RET jump to org file %s"
6408 (abbreviate-file-name buffer-file-name))))
6409 (regexp org-tr-regexp)
6410 (d0 (calendar-absolute-from-gregorian date))
6411 marker hdmarker ee txt d1 d2 s1 s2 category
6412 level todo-state tags pos head donep inherited-tags)
6413 (goto-char (point-min))
6414 (while (re-search-forward regexp nil t)
6415 (catch :skip
6416 (org-agenda-skip)
6417 (setq pos (point))
6418 (let ((start-time (match-string 1))
6419 (end-time (match-string 2)))
6420 (setq s1 (match-string 1)
6421 s2 (match-string 2)
6422 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6423 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6424 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6425 ;; Only allow days between the limits, because the normal
6426 ;; date stamps will catch the limits.
6427 (save-excursion
6428 (setq todo-state (org-get-todo-state))
6429 (setq donep (member todo-state org-done-keywords))
6430 (if (and donep org-agenda-skip-timestamp-if-done)
6431 (throw :skip t))
6432 (setq marker (org-agenda-new-marker (point))
6433 category (org-get-category))
6434 (if (not (re-search-backward org-outline-regexp-bol nil t))
6435 (throw :skip nil)
6436 (goto-char (match-beginning 0))
6437 (setq hdmarker (org-agenda-new-marker (point))
6438 inherited-tags
6439 (or (eq org-agenda-show-inherited-tags 'always)
6440 (and (listp org-agenda-show-inherited-tags)
6441 (memq 'agenda org-agenda-show-inherited-tags))
6442 (and (eq org-agenda-show-inherited-tags t)
6443 (or (eq org-agenda-use-tag-inheritance t)
6444 (memq 'agenda org-agenda-use-tag-inheritance))))
6446 tags (org-get-tags-at nil (not inherited-tags)))
6447 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6448 (looking-at "\\*+[ \t]+\\(.*\\)")
6449 (setq head (match-string 1))
6450 (let ((remove-re
6451 (if org-agenda-remove-timeranges-from-blocks
6452 (concat
6453 "<" (regexp-quote s1) ".*?>"
6454 "--"
6455 "<" (regexp-quote s2) ".*?>")
6456 nil)))
6457 (setq txt (org-agenda-format-item
6458 (format
6459 (nth (if (= d1 d2) 0 1)
6460 org-agenda-timerange-leaders)
6461 (1+ (- d0 d1)) (1+ (- d2 d1)))
6462 head level category tags
6463 (cond ((and (= d1 d0) (= d2 d0))
6464 (concat "<" start-time ">--<" end-time ">"))
6465 ((= d1 d0)
6466 (concat "<" start-time ">"))
6467 ((= d2 d0)
6468 (concat "<" end-time ">")))
6469 remove-re))))
6470 (org-add-props txt props
6471 'org-marker marker 'org-hd-marker hdmarker
6472 'type "block" 'date date
6473 'level level
6474 'todo-state todo-state
6475 'priority (org-get-priority txt))
6476 (push txt ee))))
6477 (goto-char pos)))
6478 ;; Sort the entries by expiration date.
6479 (nreverse ee)))
6481 ;;; Agenda presentation and sorting
6483 (defvar org-prefix-has-time nil
6484 "A flag, set by `org-compile-prefix-format'.
6485 The flag is set if the currently compiled format contains a `%t'.")
6486 (defvar org-prefix-has-tag nil
6487 "A flag, set by `org-compile-prefix-format'.
6488 The flag is set if the currently compiled format contains a `%T'.")
6489 (defvar org-prefix-has-effort nil
6490 "A flag, set by `org-compile-prefix-format'.
6491 The flag is set if the currently compiled format contains a `%e'.")
6492 (defvar org-prefix-has-breadcrumbs nil
6493 "A flag, set by `org-compile-prefix-format'.
6494 The flag is set if the currently compiled format contains a `%b'.")
6495 (defvar org-prefix-category-length nil
6496 "Used by `org-compile-prefix-format' to remember the category field width.")
6497 (defvar org-prefix-category-max-length nil
6498 "Used by `org-compile-prefix-format' to remember the category field width.")
6500 (defun org-agenda-get-category-icon (category)
6501 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6502 (cl-dolist (entry org-agenda-category-icon-alist)
6503 (when (string-match-p (car entry) category)
6504 (if (listp (cadr entry))
6505 (cl-return (cadr entry))
6506 (cl-return (apply #'create-image (cdr entry)))))))
6508 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6509 remove-re habitp)
6510 "Format TXT to be inserted into the agenda buffer.
6511 In particular, add the prefix and corresponding text properties.
6513 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6514 LEVEL may be a string to replace the `%l' specifier.
6515 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6516 category taken from local variable or file name. It will replace the `%c'
6517 specifier in the format.
6518 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6519 TXT for sorting of this entry, and for the `%t' specifier in the format.
6520 When DOTIME is a string, this string is searched for a time before TXT is.
6521 TAGS can be the tags of the headline.
6522 Any match of REMOVE-RE will be removed from TXT."
6523 ;; We keep the org-prefix-* variable values along with a compiled
6524 ;; formatter, so that multiple agendas existing at the same time do
6525 ;; not step on each other toes.
6527 ;; It was inconvenient to make these variables buffer local in
6528 ;; Agenda buffers, because this function expects to be called with
6529 ;; the buffer where item comes from being current, and not agenda
6530 ;; buffer
6531 (let* ((bindings (car org-prefix-format-compiled))
6532 (formatter (cadr org-prefix-format-compiled)))
6533 (cl-loop for (var value) in bindings
6534 do (set var value))
6535 (save-match-data
6536 ;; Diary entries sometimes have extra whitespace at the beginning
6537 (setq txt (org-trim txt))
6539 ;; Fix the tags part in txt
6540 (setq txt (org-agenda-fix-displayed-tags
6541 txt tags
6542 org-agenda-show-inherited-tags
6543 org-agenda-hide-tags-regexp))
6545 (let* ((category (or category
6546 (if buffer-file-name
6547 (file-name-sans-extension
6548 (file-name-nondirectory buffer-file-name))
6549 "")))
6550 (category-icon (org-agenda-get-category-icon category))
6551 (category-icon (if category-icon
6552 (propertize " " 'display category-icon)
6553 ""))
6554 (effort (and (not (string= txt ""))
6555 (get-text-property 1 'effort txt)))
6556 ;; time, tag, effort are needed for the eval of the prefix format
6557 (tag (if tags (nth (1- (length tags)) tags) ""))
6558 time
6559 (ts (if dotime (concat
6560 (if (stringp dotime) dotime "")
6561 (and org-agenda-search-headline-for-time txt))))
6562 (time-of-day (and dotime (org-get-time-of-day ts)))
6563 stamp plain s0 s1 s2 rtn srp l
6564 duration breadcrumbs)
6565 (and (derived-mode-p 'org-mode) buffer-file-name
6566 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6567 (when (and dotime time-of-day)
6568 ;; Extract starting and ending time and move them to prefix
6569 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6570 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6571 (setq s0 (match-string 0 ts)
6572 srp (and stamp (match-end 3))
6573 s1 (match-string (if plain 1 2) ts)
6574 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6576 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6577 ;; them, we might want to remove them there to avoid duplication.
6578 ;; The user can turn this off with a variable.
6579 (if (and org-prefix-has-time
6580 org-agenda-remove-times-when-in-prefix (or stamp plain)
6581 (string-match (concat (regexp-quote s0) " *") txt)
6582 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6583 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6584 (= (match-beginning 0) 0)
6586 (setq txt (replace-match "" nil nil txt))))
6587 ;; Normalize the time(s) to 24 hour
6588 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6589 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6591 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6592 (let (org-time-clocksum-use-effort-durations)
6593 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6594 (setq s2
6595 (org-minutes-to-clocksum-string
6596 (+ (org-hh:mm-string-to-minutes s1)
6597 org-agenda-default-appointment-duration)))))
6599 ;; Compute the duration
6600 (when s2
6601 (setq duration (- (org-hh:mm-string-to-minutes s2)
6602 (org-hh:mm-string-to-minutes s1)))))
6604 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6605 ;; Tags are in the string
6606 (if (or (eq org-agenda-remove-tags t)
6607 (and org-agenda-remove-tags
6608 org-prefix-has-tag))
6609 (setq txt (replace-match "" t t txt))
6610 (setq txt (replace-match
6611 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6612 (match-string 2 txt))
6613 t t txt))))
6615 (when remove-re
6616 (while (string-match remove-re txt)
6617 (setq txt (replace-match "" t t txt))))
6619 ;; Set org-heading property on `txt' to mark the start of the
6620 ;; heading.
6621 (add-text-properties 0 (length txt) '(org-heading t) txt)
6623 ;; Prepare the variables needed in the eval of the compiled format
6624 (if org-prefix-has-breadcrumbs
6625 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6626 (let ((s (org-display-outline-path nil nil "->" t)))
6627 (if (eq "" s) "" (concat s "->"))))))
6628 (setq time (cond (s2 (concat
6629 (org-agenda-time-of-day-to-ampm-maybe s1)
6630 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6631 (if org-agenda-timegrid-use-ampm " ")))
6632 (s1 (concat
6633 (org-agenda-time-of-day-to-ampm-maybe s1)
6634 (if org-agenda-timegrid-use-ampm
6635 "........ "
6636 "......")))
6637 (t ""))
6638 extra (or (and (not habitp) extra) "")
6639 category (if (symbolp category) (symbol-name category) category)
6640 level (or level ""))
6641 (if (string-match org-bracket-link-regexp category)
6642 (progn
6643 (setq l (if (match-end 3)
6644 (- (match-end 3) (match-beginning 3))
6645 (- (match-end 1) (match-beginning 1))))
6646 (when (< l (or org-prefix-category-length 0))
6647 (setq category (copy-sequence category))
6648 (org-add-props category nil
6649 'extra-space (make-string
6650 (- org-prefix-category-length l 1) ?\ ))))
6651 (if (and org-prefix-category-max-length
6652 (>= (length category) org-prefix-category-max-length))
6653 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6654 ;; Evaluate the compiled format
6655 (setq rtn (concat (eval formatter) txt))
6657 ;; And finally add the text properties
6658 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6659 (org-add-props rtn nil
6660 'org-category category
6661 'tags (mapcar 'org-downcase-keep-props tags)
6662 'org-highest-priority org-highest-priority
6663 'org-lowest-priority org-lowest-priority
6664 'time-of-day time-of-day
6665 'duration duration
6666 'breadcrumbs breadcrumbs
6667 'txt txt
6668 'level level
6669 'time time
6670 'extra extra
6671 'format org-prefix-format-compiled
6672 'dotime dotime)))))
6674 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6675 "Remove tags string from TXT, and add a modified list of tags.
6676 The modified list may contain inherited tags, and tags matched by
6677 `org-agenda-hide-tags-regexp' will be removed."
6678 (when (or add-inherited hide-re)
6679 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6680 (setq txt (substring txt 0 (match-beginning 0))))
6681 (setq tags
6682 (delq nil
6683 (mapcar (lambda (tg)
6684 (if (or (and hide-re (string-match hide-re tg))
6685 (and (not add-inherited)
6686 (get-text-property 0 'inherited tg)))
6688 tg))
6689 tags)))
6690 (when tags
6691 (let ((have-i (get-text-property 0 'inherited (car tags)))
6693 (setq txt (concat txt " :"
6694 (mapconcat
6695 (lambda (x)
6696 (setq i (get-text-property 0 'inherited x))
6697 (if (and have-i (not i))
6698 (progn
6699 (setq have-i nil)
6700 (concat ":" x))
6702 tags ":")
6703 (if have-i "::" ":"))))))
6704 txt)
6706 (defun org-downcase-keep-props (s)
6707 (let ((props (text-properties-at 0 s)))
6708 (setq s (downcase s))
6709 (add-text-properties 0 (length s) props s)
6712 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6714 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6715 "Add a time-grid for agenda items which need it.
6717 LIST is the list of agenda items formatted by `org-agenda-list'.
6718 NDAYS is the span of the current agenda view.
6719 TODAYP is t when the current agenda view is on today."
6720 (catch 'exit
6721 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6722 ((and todayp (member 'today (car org-agenda-time-grid))))
6723 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6724 ((member 'weekly (car org-agenda-time-grid)))
6725 (t (throw 'exit list)))
6726 (let* ((have (delq nil (mapcar
6727 (lambda (x) (get-text-property 1 'time-of-day x))
6728 list)))
6729 (string (nth 1 org-agenda-time-grid))
6730 (gridtimes (nth 2 org-agenda-time-grid))
6731 (req (car org-agenda-time-grid))
6732 (remove (member 'remove-match req))
6733 new time)
6734 (if (and (member 'require-timed req) (not have))
6735 ;; don't show empty grid
6736 (throw 'exit list))
6737 (while (setq time (pop gridtimes))
6738 (unless (and remove (member time have))
6739 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6740 (push (org-agenda-format-item
6741 nil string nil "" nil
6742 (concat (substring time 0 -2) ":" (substring time -2)))
6743 new)
6744 (put-text-property
6745 2 (length (car new)) 'face 'org-time-grid (car new))))
6746 (when (and todayp org-agenda-show-current-time-in-grid)
6747 (push (org-agenda-format-item
6748 nil org-agenda-current-time-string nil "" nil
6749 (format-time-string "%H:%M "))
6750 new)
6751 (put-text-property
6752 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6754 (if (member 'time-up org-agenda-sorting-strategy-selected)
6755 (append new list)
6756 (append list new)))))
6758 (defun org-compile-prefix-format (key)
6759 "Compile the prefix format into a Lisp form that can be evaluated.
6760 The resulting form and associated variable bindings is returned
6761 and stored in the variable `org-prefix-format-compiled'."
6762 (setq org-prefix-has-time nil
6763 org-prefix-has-tag nil
6764 org-prefix-category-length nil
6765 org-prefix-has-effort nil
6766 org-prefix-has-breadcrumbs nil)
6767 (let ((s (cond
6768 ((stringp org-agenda-prefix-format)
6769 org-agenda-prefix-format)
6770 ((assq key org-agenda-prefix-format)
6771 (cdr (assq key org-agenda-prefix-format)))
6772 (t " %-12:c%?-12t% s")))
6773 (start 0)
6774 varform vars var e c f opt)
6775 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6776 s start)
6777 (setq var (or (cdr (assoc (match-string 4 s)
6778 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6779 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6780 'eval)
6781 c (or (match-string 3 s) "")
6782 opt (match-beginning 1)
6783 start (1+ (match-beginning 0)))
6784 (if (eq var 'time) (setq org-prefix-has-time t))
6785 (if (eq var 'tag) (setq org-prefix-has-tag t))
6786 (if (eq var 'effort) (setq org-prefix-has-effort t))
6787 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6788 (setq f (concat "%" (match-string 2 s) "s"))
6789 (when (eq var 'category)
6790 (setq org-prefix-category-length
6791 (floor (abs (string-to-number (match-string 2 s)))))
6792 (setq org-prefix-category-max-length
6793 (let ((x (match-string 2 s)))
6794 (save-match-data
6795 (if (string-match "\\.[0-9]+" x)
6796 (string-to-number (substring (match-string 0 x) 1)))))))
6797 (if (eq var 'eval)
6798 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6799 (if opt
6800 (setq varform
6801 `(if (or (equal "" ,var) (equal nil ,var))
6803 (format ,f (concat ,var ,c))))
6804 (setq varform
6805 `(format ,f (if (or (equal ,var "")
6806 (equal ,var nil)) ""
6807 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6808 (setq s (replace-match "%s" t nil s))
6809 (push varform vars))
6810 (setq vars (nreverse vars))
6811 (with-current-buffer (or org-agenda-buffer (current-buffer))
6812 (setq org-prefix-format-compiled
6813 (list
6814 `((org-prefix-has-time ,org-prefix-has-time)
6815 (org-prefix-has-tag ,org-prefix-has-tag)
6816 (org-prefix-category-length ,org-prefix-category-length)
6817 (org-prefix-has-effort ,org-prefix-has-effort)
6818 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6819 `(format ,s ,@vars))))))
6821 (defun org-set-sorting-strategy (key)
6822 (if (symbolp (car org-agenda-sorting-strategy))
6823 ;; the old format
6824 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6825 (setq org-agenda-sorting-strategy-selected
6826 (or (cdr (assq key org-agenda-sorting-strategy))
6827 (cdr (assq 'agenda org-agenda-sorting-strategy))
6828 '(time-up category-keep priority-down)))))
6830 (defun org-get-time-of-day (s &optional string mod24)
6831 "Check string S for a time of day.
6832 If found, return it as a military time number between 0 and 2400.
6833 If not found, return nil.
6834 The optional STRING argument forces conversion into a 5 character wide string
6835 HH:MM."
6836 (save-match-data
6837 (when
6838 (and
6839 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6840 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6841 (not (eq (get-text-property 1 'face s) 'org-link)))
6842 (let* ((h (string-to-number (match-string 1 s)))
6843 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6844 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6845 (am-p (equal ampm "am"))
6846 (h1 (cond ((not ampm) h)
6847 ((= h 12) (if am-p 0 12))
6848 (t (+ h (if am-p 0 12)))))
6849 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6850 (mod h1 24) h1))
6851 (t0 (+ (* 100 h2) m))
6852 (t1 (concat (if (>= h1 24) "+" " ")
6853 (if (and org-agenda-time-leading-zero
6854 (< t0 1000)) "0" "")
6855 (if (< t0 100) "0" "")
6856 (if (< t0 10) "0" "")
6857 (int-to-string t0))))
6858 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6860 (defvar org-agenda-before-sorting-filter-function nil
6861 "Function to be applied to agenda items prior to sorting.
6862 Prior to sorting also means just before they are inserted into the agenda.
6864 To aid sorting, you may revisit the original entries and add more text
6865 properties which will later be used by the sorting functions.
6867 The function should take a string argument, an agenda line.
6868 It has access to the text properties in that line, which contain among
6869 other things, the property `org-hd-marker' that points to the entry
6870 where the line comes from. Note that not all lines going into the agenda
6871 have this property, only most.
6873 The function should return the modified string. It is probably best
6874 to ONLY change text properties.
6876 You can also use this function as a filter, by returning nil for lines
6877 you don't want to have in the agenda at all. For this application, you
6878 could bind the variable in the options section of a custom command.")
6880 (defun org-agenda-finalize-entries (list &optional type)
6881 "Sort, limit and concatenate the LIST of agenda items.
6882 The optional argument TYPE tells the agenda type."
6883 (let ((max-effort (cond ((listp org-agenda-max-effort)
6884 (cdr (assoc type org-agenda-max-effort)))
6885 (t org-agenda-max-effort)))
6886 (max-todo (cond ((listp org-agenda-max-todos)
6887 (cdr (assoc type org-agenda-max-todos)))
6888 (t org-agenda-max-todos)))
6889 (max-tags (cond ((listp org-agenda-max-tags)
6890 (cdr (assoc type org-agenda-max-tags)))
6891 (t org-agenda-max-tags)))
6892 (max-entries (cond ((listp org-agenda-max-entries)
6893 (cdr (assoc type org-agenda-max-entries)))
6894 (t org-agenda-max-entries))))
6895 (when org-agenda-before-sorting-filter-function
6896 (setq list
6897 (delq nil
6898 (mapcar
6899 org-agenda-before-sorting-filter-function list))))
6900 (setq list (mapcar 'org-agenda-highlight-todo list)
6901 list (mapcar 'identity (sort list 'org-entries-lessp)))
6902 (when max-effort
6903 (setq list (org-agenda-limit-entries
6904 list 'effort-minutes max-effort
6905 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6906 32767 -1))))))
6907 (when max-todo
6908 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6909 (when max-tags
6910 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6911 (when max-entries
6912 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6913 (mapconcat 'identity list "\n")))
6915 (defun org-agenda-limit-entries (list prop limit &optional fn)
6916 "Limit the number of agenda entries."
6917 (let ((include (and limit (< limit 0))))
6918 (if limit
6919 (let ((fun (or fn (lambda (p) (if p 1))))
6920 (lim 0))
6921 (delq nil
6922 (mapcar
6923 (lambda (e)
6924 (let ((pval (funcall
6925 fun (get-text-property (1- (length e))
6926 prop e))))
6927 (if pval (setq lim (+ lim pval)))
6928 (cond ((and pval (<= lim (abs limit))) e)
6929 ((and include (not pval)) e))))
6930 list)))
6931 list)))
6933 (defun org-agenda-limit-interactively (remove)
6934 "In agenda, interactively limit entries to various maximums."
6935 (interactive "P")
6936 (if remove
6937 (progn (setq org-agenda-max-entries nil
6938 org-agenda-max-todos nil
6939 org-agenda-max-tags nil
6940 org-agenda-max-effort nil)
6941 (org-agenda-redo))
6942 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6943 (msg (cond ((= max ?E) "How many minutes? ")
6944 ((= max ?e) "How many entries? ")
6945 ((= max ?t) "How many TODO entries? ")
6946 ((= max ?T) "How many tagged entries? ")
6947 (t (user-error "Wrong input"))))
6948 (num (string-to-number (read-from-minibuffer msg))))
6949 (cond ((equal max ?e)
6950 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6951 ((equal max ?t)
6952 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6953 ((equal max ?T)
6954 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6955 ((equal max ?E)
6956 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6957 (org-agenda-fit-window-to-buffer))
6959 (defun org-agenda-highlight-todo (x)
6960 (let ((org-done-keywords org-done-keywords-for-agenda)
6961 (case-fold-search nil)
6963 (if (eq x 'line)
6964 (save-excursion
6965 (beginning-of-line 1)
6966 (setq re (org-get-at-bol 'org-todo-regexp))
6967 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6968 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6969 (add-text-properties (match-beginning 0) (match-end 1)
6970 (list 'face (org-get-todo-face 1)))
6971 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6972 (delete-region (match-beginning 1) (1- (match-end 0)))
6973 (goto-char (match-beginning 1))
6974 (insert (format org-agenda-todo-keyword-format s)))))
6975 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6976 (setq re (get-text-property 0 'org-todo-regexp x))
6977 (when (and re
6978 ;; Test `pl' because if there's no heading content,
6979 ;; there's no point matching to highlight. Note
6980 ;; that if we didn't test `pl' first, and there
6981 ;; happened to be no keyword from `org-todo-regexp'
6982 ;; on this heading line, then the `equal' comparison
6983 ;; afterwards would spuriously succeed in the case
6984 ;; where `pl' is nil -- causing an args-out-of-range
6985 ;; error when we try to add text properties to text
6986 ;; that isn't there.
6988 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6989 x pl) pl))
6990 (add-text-properties
6991 (or (match-end 1) (match-end 0)) (match-end 0)
6992 (list 'face (org-get-todo-face (match-string 2 x)))
6994 (when (match-end 1)
6995 (setq x (concat (substring x 0 (match-end 1))
6996 (format org-agenda-todo-keyword-format
6997 (match-string 2 x))
6998 (org-add-props " " (text-properties-at 0 x))
6999 (substring x (match-end 3)))))))
7000 x)))
7002 (defsubst org-cmp-values (a b property)
7003 "Compare the numeric value of text PROPERTY for string A and B."
7004 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
7005 (pb (or (get-text-property (1- (length b)) property b) 0)))
7006 (cond ((> pa pb) +1)
7007 ((< pa pb) -1))))
7009 (defsubst org-cmp-effort (a b)
7010 "Compare the effort values of string A and B."
7011 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
7012 ;; `effort-minutes' property is not directly accessible from
7013 ;; the strings, but is stored as a property in `txt'.
7014 (ea (or (get-text-property
7015 0 'effort-minutes (get-text-property 0 'txt a))
7016 def))
7017 (eb (or (get-text-property
7018 0 'effort-minutes (get-text-property 0 'txt b))
7019 def)))
7020 (cond ((> ea eb) +1)
7021 ((< ea eb) -1))))
7023 (defsubst org-cmp-category (a b)
7024 "Compare the string values of categories of strings A and B."
7025 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
7026 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
7027 (cond ((string-lessp ca cb) -1)
7028 ((string-lessp cb ca) +1))))
7030 (defsubst org-cmp-todo-state (a b)
7031 "Compare the todo states of strings A and B."
7032 (let* ((ma (or (get-text-property 1 'org-marker a)
7033 (get-text-property 1 'org-hd-marker a)))
7034 (mb (or (get-text-property 1 'org-marker b)
7035 (get-text-property 1 'org-hd-marker b)))
7036 (fa (and ma (marker-buffer ma)))
7037 (fb (and mb (marker-buffer mb)))
7038 (todo-kwds
7039 (or (and fa (with-current-buffer fa org-todo-keywords-1))
7040 (and fb (with-current-buffer fb org-todo-keywords-1))))
7041 (ta (or (get-text-property 1 'todo-state a) ""))
7042 (tb (or (get-text-property 1 'todo-state b) ""))
7043 (la (- (length (member ta todo-kwds))))
7044 (lb (- (length (member tb todo-kwds))))
7045 (donepa (member ta org-done-keywords-for-agenda))
7046 (donepb (member tb org-done-keywords-for-agenda)))
7047 (cond ((and donepa (not donepb)) -1)
7048 ((and (not donepa) donepb) +1)
7049 ((< la lb) -1)
7050 ((< lb la) +1))))
7052 (defsubst org-cmp-alpha (a b)
7053 "Compare the headlines, alphabetically."
7054 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
7055 (plb (text-property-any 0 (length b) 'org-heading t b))
7056 (ta (and pla (substring a pla)))
7057 (tb (and plb (substring b plb)))
7058 (case-fold-search nil))
7059 (when pla
7060 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
7061 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
7062 (setq ta (substring ta (match-end 0))))
7063 (setq ta (downcase ta)))
7064 (when plb
7065 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
7066 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
7067 (setq tb (substring tb (match-end 0))))
7068 (setq tb (downcase tb)))
7069 (cond ((not ta) +1)
7070 ((not tb) -1)
7071 ((string-lessp ta tb) -1)
7072 ((string-lessp tb ta) +1))))
7074 (defsubst org-cmp-tag (a b)
7075 "Compare the string values of the first tags of A and B."
7076 (let ((ta (car (last (get-text-property 1 'tags a))))
7077 (tb (car (last (get-text-property 1 'tags b)))))
7078 (cond ((not ta) +1)
7079 ((not tb) -1)
7080 ((string-lessp ta tb) -1)
7081 ((string-lessp tb ta) +1))))
7083 (defsubst org-cmp-time (a b)
7084 "Compare the time-of-day values of strings A and B."
7085 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7086 (ta (or (get-text-property 1 'time-of-day a) def))
7087 (tb (or (get-text-property 1 'time-of-day b) def)))
7088 (cond ((< ta tb) -1)
7089 ((< tb ta) +1))))
7091 (defsubst org-cmp-ts (a b type)
7092 "Compare the timestamps values of entries A and B.
7093 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7094 \"timestamp_ia\", compare within each of these type. When TYPE
7095 is the empty string, compare all timestamps without respect of
7096 their type."
7097 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7098 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7099 (get-text-property 1 'ts-date a))
7100 def))
7101 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7102 (get-text-property 1 'ts-date b))
7103 def)))
7104 (cond ((< ta tb) -1)
7105 ((< tb ta) +1))))
7107 (defsubst org-cmp-habit-p (a b)
7108 "Compare the todo states of strings A and B."
7109 (let ((ha (get-text-property 1 'org-habit-p a))
7110 (hb (get-text-property 1 'org-habit-p b)))
7111 (cond ((and ha (not hb)) -1)
7112 ((and (not ha) hb) +1))))
7114 (defun org-entries-lessp (a b)
7115 "Predicate for sorting agenda entries."
7116 ;; The following variables will be used when the form is evaluated.
7117 ;; So even though the compiler complains, keep them.
7118 (let* ((ss org-agenda-sorting-strategy-selected)
7119 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7120 (org-cmp-ts a b "")))
7121 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7122 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7123 (org-cmp-ts a b "scheduled")))
7124 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7125 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7126 (org-cmp-ts a b "deadline")))
7127 (deadline-down (if deadline-up (- deadline-up) nil))
7128 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7129 (org-cmp-ts a b "timestamp_ia")))
7130 (tsia-down (if tsia-up (- tsia-up) nil))
7131 (ts-up (and (org-em 'ts-up 'ts-down ss)
7132 (org-cmp-ts a b "timestamp")))
7133 (ts-down (if ts-up (- ts-up) nil))
7134 (time-up (and (org-em 'time-up 'time-down ss)
7135 (org-cmp-time a b)))
7136 (time-down (if time-up (- time-up) nil))
7137 (stats-up (and (org-em 'stats-up 'stats-down ss)
7138 (org-cmp-values a b 'org-stats)))
7139 (stats-down (if stats-up (- stats-up) nil))
7140 (priority-up (and (org-em 'priority-up 'priority-down ss)
7141 (org-cmp-values a b 'priority)))
7142 (priority-down (if priority-up (- priority-up) nil))
7143 (effort-up (and (org-em 'effort-up 'effort-down ss)
7144 (org-cmp-effort a b)))
7145 (effort-down (if effort-up (- effort-up) nil))
7146 (category-up (and (or (org-em 'category-up 'category-down ss)
7147 (memq 'category-keep ss))
7148 (org-cmp-category a b)))
7149 (category-down (if category-up (- category-up) nil))
7150 (category-keep (if category-up +1 nil))
7151 (tag-up (and (org-em 'tag-up 'tag-down ss)
7152 (org-cmp-tag a b)))
7153 (tag-down (if tag-up (- tag-up) nil))
7154 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7155 (org-cmp-todo-state a b)))
7156 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7157 (habit-up (and (org-em 'habit-up 'habit-down ss)
7158 (org-cmp-habit-p a b)))
7159 (habit-down (if habit-up (- habit-up) nil))
7160 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7161 (org-cmp-alpha a b)))
7162 (alpha-down (if alpha-up (- alpha-up) nil))
7163 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7164 user-defined-up user-defined-down)
7165 (if (and need-user-cmp org-agenda-cmp-user-defined
7166 (functionp org-agenda-cmp-user-defined))
7167 (setq user-defined-up
7168 (funcall org-agenda-cmp-user-defined a b)
7169 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7170 (cdr (assoc
7171 (eval (cons 'or org-agenda-sorting-strategy-selected))
7172 '((-1 . t) (1 . nil) (nil . nil))))))
7174 ;;; Agenda restriction lock
7176 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7177 "Overlay to mark the headline to which agenda commands are restricted.")
7178 (overlay-put org-agenda-restriction-lock-overlay
7179 'face 'org-agenda-restriction-lock)
7180 (overlay-put org-agenda-restriction-lock-overlay
7181 'help-echo "Agendas are currently limited to this subtree.")
7182 (delete-overlay org-agenda-restriction-lock-overlay)
7184 ;;;###autoload
7185 (defun org-agenda-set-restriction-lock (&optional type)
7186 "Set restriction lock for agenda, to current subtree or file.
7187 Restriction will be the file if TYPE is `file', or if type is the
7188 universal prefix \\='(4), or if the cursor is before the first headline
7189 in the file. Otherwise, restriction will be to the current subtree."
7190 (interactive "P")
7191 (org-agenda-remove-restriction-lock 'noupdate)
7192 (and (equal type '(4)) (setq type 'file))
7193 (setq type (cond
7194 (type type)
7195 ((org-at-heading-p) 'subtree)
7196 ((condition-case nil (org-back-to-heading t) (error nil))
7197 'subtree)
7198 (t 'file)))
7199 (if (eq type 'subtree)
7200 (progn
7201 (setq org-agenda-restrict (current-buffer))
7202 (setq org-agenda-overriding-restriction 'subtree)
7203 (put 'org-agenda-files 'org-restrict
7204 (list (buffer-file-name (buffer-base-buffer))))
7205 (org-back-to-heading t)
7206 (move-overlay org-agenda-restriction-lock-overlay
7207 (point)
7208 (if org-agenda-restriction-lock-highlight-subtree
7209 (save-excursion (org-end-of-subtree t t) (point))
7210 (point-at-eol)))
7211 (move-marker org-agenda-restrict-begin (point))
7212 (move-marker org-agenda-restrict-end
7213 (save-excursion (org-end-of-subtree t t)))
7214 (message "Locking agenda restriction to subtree"))
7215 (put 'org-agenda-files 'org-restrict
7216 (list (buffer-file-name (buffer-base-buffer))))
7217 (setq org-agenda-restrict nil)
7218 (setq org-agenda-overriding-restriction 'file)
7219 (move-marker org-agenda-restrict-begin nil)
7220 (move-marker org-agenda-restrict-end nil)
7221 (message "Locking agenda restriction to file"))
7222 (setq current-prefix-arg nil)
7223 (org-agenda-maybe-redo))
7225 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7226 "Remove the agenda restriction lock."
7227 (interactive "P")
7228 (delete-overlay org-agenda-restriction-lock-overlay)
7229 (delete-overlay org-speedbar-restriction-lock-overlay)
7230 (setq org-agenda-overriding-restriction nil)
7231 (setq org-agenda-restrict nil)
7232 (put 'org-agenda-files 'org-restrict nil)
7233 (move-marker org-agenda-restrict-begin nil)
7234 (move-marker org-agenda-restrict-end nil)
7235 (setq current-prefix-arg nil)
7236 (message "Agenda restriction lock removed")
7237 (or noupdate (org-agenda-maybe-redo)))
7239 (defun org-agenda-maybe-redo ()
7240 "If there is any window showing the agenda view, update it."
7241 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7242 org-agenda-buffer-name)
7244 (w0 (selected-window)))
7245 (when w
7246 (select-window w)
7247 (org-agenda-redo)
7248 (select-window w0)
7249 (if org-agenda-overriding-restriction
7250 (message "Agenda view shifted to new %s restriction"
7251 org-agenda-overriding-restriction)
7252 (message "Agenda restriction lock removed")))))
7254 ;;; Agenda commands
7256 (defun org-agenda-check-type (error &rest types)
7257 "Check if agenda buffer is of allowed type.
7258 If ERROR is non-nil, throw an error, otherwise just return nil.
7259 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7260 (if (not org-agenda-type)
7261 (error "No Org agenda currently displayed")
7262 (if (memq org-agenda-type types)
7264 (if error
7265 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7266 nil))))
7268 (defun org-agenda-Quit ()
7269 "Exit the agenda, killing the agenda buffer.
7270 Like `org-agenda-quit', but kill the buffer even when
7271 `org-agenda-sticky' is non-nil."
7272 (interactive)
7273 (org-agenda--quit))
7275 (defun org-agenda-quit ()
7276 "Exit the agenda.
7278 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7279 instead of killing it.
7281 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7282 the pre-agenda window configuration.
7284 When column view is active, exit column view instead of the
7285 agenda."
7286 (interactive)
7287 (org-agenda--quit org-agenda-sticky))
7289 (defun org-agenda--quit (&optional bury)
7290 (if org-agenda-columns-active
7291 (org-columns-quit)
7292 (let ((wconf org-agenda-pre-window-conf)
7293 (buf (current-buffer))
7294 (org-agenda-last-indirect-window
7295 (and (eq org-indirect-buffer-display 'other-window)
7296 org-agenda-last-indirect-buffer
7297 (get-buffer-window org-agenda-last-indirect-buffer))))
7298 (cond
7299 ((eq org-agenda-window-setup 'other-frame)
7300 (delete-frame))
7301 ((and org-agenda-restore-windows-after-quit
7302 wconf)
7303 ;; Maybe restore the pre-agenda window configuration. Reset
7304 ;; `org-agenda-pre-window-conf' before running
7305 ;; `set-window-configuration', which loses the current buffer.
7306 (setq org-agenda-pre-window-conf nil)
7307 (set-window-configuration wconf))
7309 (when org-agenda-last-indirect-window
7310 (delete-window org-agenda-last-indirect-window))
7311 (and (not (eq org-agenda-window-setup 'current-window))
7312 (not (one-window-p))
7313 (delete-window))))
7314 (if bury
7315 ;; Set the agenda buffer as the current buffer instead of
7316 ;; passing it as an argument to `bury-buffer' so that
7317 ;; `bury-buffer' removes it from the window.
7318 (with-current-buffer buf
7319 (bury-buffer))
7320 (kill-buffer buf)
7321 (setq org-agenda-archives-mode nil
7322 org-agenda-buffer nil)))))
7324 (defun org-agenda-exit ()
7325 "Exit the agenda, killing Org buffers loaded by the agenda.
7326 Like `org-agenda-Quit', but kill any buffers that were created by
7327 the agenda. Org buffers visited directly by the user will not be
7328 touched. Also, exit the agenda even if it is in column view."
7329 (interactive)
7330 (when org-agenda-columns-active
7331 (org-columns-quit))
7332 (org-release-buffers org-agenda-new-buffers)
7333 (setq org-agenda-new-buffers nil)
7334 (org-agenda-Quit))
7336 (defun org-agenda-kill-all-agenda-buffers ()
7337 "Kill all buffers in `org-agenda-mode'.
7338 This is used when toggling sticky agendas."
7339 (interactive)
7340 (let (blist)
7341 (dolist (buf (buffer-list))
7342 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7343 (push buf blist)))
7344 (mapc 'kill-buffer blist)))
7346 (defun org-agenda-execute (arg)
7347 "Execute another agenda command, keeping same window.
7348 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7349 in the agenda."
7350 (interactive "P")
7351 (let ((org-agenda-window-setup 'current-window))
7352 (org-agenda arg)))
7354 (defun org-agenda-redo (&optional all)
7355 "Rebuild possibly ALL agenda view(s) in the current buffer."
7356 (interactive "P")
7357 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7358 (cpa (unless (eq all t) current-prefix-arg))
7359 (org-agenda-doing-sticky-redo org-agenda-sticky)
7360 (org-agenda-sticky nil)
7361 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7362 org-agenda-buffer-name))
7363 (org-agenda-keep-modes t)
7364 (tag-filter org-agenda-tag-filter)
7365 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7366 (top-hl-filter org-agenda-top-headline-filter)
7367 (cat-filter org-agenda-category-filter)
7368 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7369 (re-filter org-agenda-regexp-filter)
7370 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7371 (effort-filter org-agenda-effort-filter)
7372 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7373 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7374 (cols org-agenda-columns-active)
7375 (line (org-current-line))
7376 (window-line (- line (org-current-line (window-start))))
7377 (lprops (get 'org-agenda-redo-command 'org-lprops))
7378 (redo-cmd (get-text-property p 'org-redo-cmd))
7379 (last-args (get-text-property p 'org-last-args))
7380 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7381 (org-agenda-overriding-cmd-arguments
7382 (unless (eq all t)
7383 (cond ((listp last-args)
7384 (cons (or cpa (car last-args)) (cdr last-args)))
7385 ((stringp last-args)
7386 last-args))))
7387 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7388 (put 'org-agenda-tag-filter :preset-filter nil)
7389 (put 'org-agenda-category-filter :preset-filter nil)
7390 (put 'org-agenda-regexp-filter :preset-filter nil)
7391 (put 'org-agenda-effort-filter :preset-filter nil)
7392 (and cols (org-columns-quit))
7393 (message "Rebuilding agenda buffer...")
7394 (if series-redo-cmd
7395 (eval series-redo-cmd)
7396 (org-let lprops redo-cmd))
7397 (setq org-agenda-undo-list nil
7398 org-agenda-pending-undo-list nil
7399 org-agenda-tag-filter tag-filter
7400 org-agenda-category-filter cat-filter
7401 org-agenda-regexp-filter re-filter
7402 org-agenda-effort-filter effort-filter
7403 org-agenda-top-headline-filter top-hl-filter)
7404 (message "Rebuilding agenda buffer...done")
7405 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7406 (put 'org-agenda-category-filter :preset-filter cat-preset)
7407 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7408 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7409 (let ((tag (or tag-filter tag-preset))
7410 (cat (or cat-filter cat-preset))
7411 (effort (or effort-filter effort-preset))
7412 (re (or re-filter re-preset)))
7413 (when tag (org-agenda-filter-apply tag 'tag t))
7414 (when cat (org-agenda-filter-apply cat 'category))
7415 (when effort (org-agenda-filter-apply effort 'effort))
7416 (when re (org-agenda-filter-apply re 'regexp)))
7417 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7418 (and cols (called-interactively-p 'any) (org-agenda-columns))
7419 (org-goto-line line)
7420 (recenter window-line)))
7422 (defvar org-global-tags-completion-table nil)
7423 (defvar org-agenda-filter-form nil)
7424 (defvar org-agenda-filtered-by-category nil)
7426 (defun org-agenda-filter-by-category (strip)
7427 "Filter lines in the agenda buffer that have a specific category.
7428 The category is that of the current line.
7429 Without prefix argument, keep only the lines of that category.
7430 With a prefix argument, exclude the lines of that category.
7432 (interactive "P")
7433 (if (and org-agenda-filtered-by-category
7434 org-agenda-category-filter)
7435 (org-agenda-filter-show-all-cat)
7436 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7437 (cond
7438 ((and cat strip)
7439 (org-agenda-filter-apply
7440 (push (concat "-" cat) org-agenda-category-filter) 'category))
7441 (cat
7442 (org-agenda-filter-apply
7443 (setq org-agenda-category-filter
7444 (list (concat "+" cat))) 'category))
7445 (t (error "No category at point"))))))
7447 (defun org-find-top-headline (&optional pos)
7448 "Find the topmost parent headline and return it.
7449 POS when non-nil is the marker or buffer position to start the
7450 search from."
7451 (save-excursion
7452 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
7453 (when pos (goto-char pos))
7454 ;; Skip up to the topmost parent.
7455 (while (org-up-heading-safe))
7456 (ignore-errors (nth 4 (org-heading-components))))))
7458 (defvar org-agenda-filtered-by-top-headline nil)
7459 (defun org-agenda-filter-by-top-headline (strip)
7460 "Keep only those lines that are descendants from the same top headline.
7461 The top headline is that of the current line."
7462 (interactive "P")
7463 (if org-agenda-filtered-by-top-headline
7464 (progn
7465 (setq org-agenda-filtered-by-top-headline nil
7466 org-agenda-top-headline-filter nil)
7467 (org-agenda-filter-show-all-top-filter))
7468 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7469 (if toph (org-agenda-filter-top-headline-apply toph strip)
7470 (error "No top-level headline at point")))))
7472 (defvar org-agenda-regexp-filter nil)
7473 (defun org-agenda-filter-by-regexp (strip)
7474 "Filter agenda entries by a regular expression.
7475 Regexp filters are cumulative.
7476 With no prefix argument, keep entries matching the regexp.
7477 With one prefix argument, filter out entries matching the regexp.
7478 With two prefix arguments, remove the regexp filters."
7479 (interactive "P")
7480 (if (not (equal strip '(16)))
7481 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7482 (read-from-minibuffer
7483 (if (equal strip '(4))
7484 "Filter out entries matching regexp: "
7485 "Narrow to entries matching regexp: ")))))
7486 (push flt org-agenda-regexp-filter)
7487 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7488 (org-agenda-filter-show-all-re)
7489 (message "Regexp filter removed")))
7491 (defvar org-agenda-effort-filter nil)
7492 (defun org-agenda-filter-by-effort (strip)
7493 "Filter agenda entries by effort.
7494 With no prefix argument, keep entries matching the effort condition.
7495 With one prefix argument, filter out entries matching the condition.
7496 With two prefix arguments, remove the effort filters."
7497 (interactive "P")
7498 (cond
7499 ((member strip '(nil 4))
7500 (let* ((efforts (split-string
7501 (or (cdr (assoc (concat org-effort-property "_ALL")
7502 org-global-properties))
7503 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7504 ;; XXX: the following handles only up to 10 different
7505 ;; effort values.
7506 (allowed-keys (if (null efforts) nil
7507 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7508 (number-sequence 1 (length efforts)))))
7509 (op nil))
7510 (while (not (memq op '(?< ?> ?=)))
7511 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7512 ;; Select appropriate duration. Ignore non-digit characters.
7513 (let ((prompt
7514 (apply #'format
7515 (concat "Effort %c "
7516 (mapconcat (lambda (s) (concat "[%d]" s))
7517 efforts
7518 " "))
7519 op allowed-keys))
7520 (eff -1))
7521 (while (not (memq eff allowed-keys))
7522 (message prompt)
7523 (setq eff (- (read-char-exclusive) 48)))
7524 (setq org-agenda-effort-filter
7525 (list (concat (if strip "-" "+")
7526 (char-to-string op)
7527 ;; Numbering is 1 2 3 ... 9 0, but we want
7528 ;; 0 1 2 ... 8 9.
7529 (nth (mod (1- eff) 10) efforts)))))
7530 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7531 (t (org-agenda-filter-show-all-effort)
7532 (message "Effort filter removed"))))
7534 (defun org-agenda-filter-remove-all ()
7535 "Remove all filters from the current agenda buffer."
7536 (interactive)
7537 (when org-agenda-tag-filter
7538 (org-agenda-filter-show-all-tag))
7539 (when org-agenda-category-filter
7540 (org-agenda-filter-show-all-cat))
7541 (when org-agenda-regexp-filter
7542 (org-agenda-filter-show-all-re))
7543 (when org-agenda-top-headline-filter
7544 (org-agenda-filter-show-all-top-filter))
7545 (when org-agenda-effort-filter
7546 (org-agenda-filter-show-all-effort))
7547 (org-agenda-finalize))
7549 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7550 "Keep only those lines in the agenda buffer that have a specific tag.
7552 The tag is selected with its fast selection letter, as configured.
7554 With a `\\[universal-argument]' prefix, exclude the agenda search.
7556 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7557 i.e. don't
7558 filter on all its group members.
7560 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7561 should be used to exclude the search - the interactive user can
7562 also press `-' or `+' to switch between filtering and excluding."
7563 (interactive "P")
7564 (let* ((alist org-tag-alist-for-agenda)
7565 (tag-chars (mapconcat
7566 (lambda (x) (if (and (not (symbolp (car x)))
7567 (cdr x))
7568 (char-to-string (cdr x))
7569 ""))
7570 org-tag-alist-for-agenda ""))
7571 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7572 (string-to-list tag-chars)))
7573 (exclude (or exclude (equal arg '(4))))
7574 (expand (not (equal arg '(16))))
7575 (inhibit-read-only t)
7576 (current org-agenda-tag-filter)
7577 a n tag)
7578 (unless char
7579 (while (not (memq char valid-char-list))
7580 (message
7581 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7582 (if exclude "Exclude" "Filter") tag-chars
7583 (if org-agenda-auto-exclude-function "[RET], " "")
7584 (if expand "" ", no grouptag expand"))
7585 (setq char (read-char-exclusive))
7586 ;; Excluding or filtering down
7587 (cond ((eq char ?-) (setq exclude t))
7588 ((eq char ?+) (setq exclude nil)))))
7589 (when (eq char ?\t)
7590 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7591 (setq-local org-global-tags-completion-table
7592 (org-global-tags-completion-table)))
7593 (let ((completion-ignore-case t))
7594 (setq tag (completing-read
7595 "Tag: " org-global-tags-completion-table))))
7596 (cond
7597 ((eq char ?\r)
7598 (org-agenda-filter-show-all-tag)
7599 (when org-agenda-auto-exclude-function
7600 (setq org-agenda-tag-filter nil)
7601 (dolist (tag (org-agenda-get-represented-tags))
7602 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7603 (if modifier
7604 (push modifier org-agenda-tag-filter))))
7605 (if (not (null org-agenda-tag-filter))
7606 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7607 ((eq char ?/)
7608 (org-agenda-filter-show-all-tag)
7609 (when (get 'org-agenda-tag-filter :preset-filter)
7610 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7611 ((eq char ?.)
7612 (setq org-agenda-tag-filter
7613 (mapcar (lambda(tag) (concat "+" tag))
7614 (org-get-at-bol 'tags)))
7615 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7616 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7617 ((or (eq char ?\s)
7618 (setq a (rassoc char alist))
7619 (and tag (setq a (cons tag nil))))
7620 (org-agenda-filter-show-all-tag)
7621 (setq tag (car a))
7622 (setq org-agenda-tag-filter
7623 (cons (concat (if exclude "-" "+") tag)
7624 current))
7625 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7626 (t (error "Invalid tag selection character %c" char)))))
7628 (defun org-agenda-get-represented-tags ()
7629 "Get a list of all tags currently represented in the agenda."
7630 (let (p tags)
7631 (save-excursion
7632 (goto-char (point-min))
7633 (while (setq p (next-single-property-change (point) 'tags))
7634 (goto-char p)
7635 (mapc (lambda (x) (add-to-list 'tags x))
7636 (get-text-property (point) 'tags))))
7637 tags))
7640 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7641 "Create the form that tests a line for agenda filter. Optional
7642 argument EXPAND can be used for the TYPE tag and will expand the
7643 tags in the FILTER if any of the tags in FILTER are grouptags."
7644 (let (f f1)
7645 (cond
7646 ;; Tag filter
7647 ((eq type 'tag)
7648 (setq filter
7649 (delete-dups
7650 (append (get 'org-agenda-tag-filter :preset-filter)
7651 filter)))
7652 (dolist (x filter)
7653 (let ((op (string-to-char x)))
7654 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7655 (setq x (list x)))
7656 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7657 (push f1 f))))
7658 ;; Category filter
7659 ((eq type 'category)
7660 (setq filter
7661 (delete-dups
7662 (append (get 'org-agenda-category-filter :preset-filter)
7663 filter)))
7664 (dolist (x filter)
7665 (if (equal "-" (substring x 0 1))
7666 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7667 (setq f1 (list 'equal (substring x 1) 'cat)))
7668 (push f1 f)))
7669 ;; Regexp filter
7670 ((eq type 'regexp)
7671 (setq filter
7672 (delete-dups
7673 (append (get 'org-agenda-regexp-filter :preset-filter)
7674 filter)))
7675 (dolist (x filter)
7676 (if (equal "-" (substring x 0 1))
7677 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7678 (setq f1 (list 'string-match (substring x 1) 'txt)))
7679 (push f1 f)))
7680 ;; Effort filter
7681 ((eq type 'effort)
7682 (setq filter
7683 (delete-dups
7684 (append (get 'org-agenda-effort-filter :preset-filter)
7685 filter)))
7686 (dolist (x filter)
7687 (push (org-agenda-filter-effort-form x) f))))
7688 (cons 'and (nreverse f))))
7690 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7691 "Return a form associated to tag-expression TAGS.
7692 Build a form testing a line for agenda filter for
7693 tag-expressions. OP is an operator of type CHAR that allows the
7694 function to set the right switches in the returned form."
7695 (let (form)
7696 ;; Any of the expressions can match if OP is +, all must match if
7697 ;; the operator is -.
7698 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7699 (let* ((tag (substring x 1))
7700 (f (cond
7701 ((string= "" tag) '(not tags))
7702 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7703 ;; TAG is a regexp.
7704 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7705 (t (list 'member (downcase tag) 'tags)))))
7706 (push (if (eq op ?-) (list 'not f) f) form)))))
7708 (defun org-agenda-filter-effort-form (e)
7709 "Return the form to compare the effort of the current line with what E says.
7710 E looks like \"+<2:25\"."
7711 (let (op)
7712 (setq e (substring e 1))
7713 (setq op (string-to-char e) e (substring e 1))
7714 (setq op (cond ((equal op ?<) '<=)
7715 ((equal op ?>) '>=)
7716 ((equal op ??) op)
7717 (t '=)))
7718 (list 'org-agenda-compare-effort (list 'quote op)
7719 (org-duration-string-to-minutes e))))
7721 (defun org-agenda-compare-effort (op value)
7722 "Compare the effort of the current line with VALUE, using OP.
7723 If the line does not have an effort defined, return nil."
7724 ;; `effort-minutes' property cannot be extracted directly from
7725 ;; current line but is stored as a property in `txt'.
7726 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7727 (funcall op
7728 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7729 value)))
7731 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7732 "Expand group tags in FILTER for the agenda.
7733 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7734 (if org-group-tags
7735 (let ((case-fold-search t) rtn)
7736 (mapc
7737 (lambda (f)
7738 (let (f0 dir)
7739 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7740 (setq dir (match-string 1 f) f0 (match-string 2 f))
7741 (setq dir (if no-operator "" "+") f0 f))
7742 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7743 (org-tags-expand f0 t t))
7744 rtn))))
7745 filter)
7746 (reverse rtn))
7747 filter))
7749 (defun org-agenda-filter-apply (filter type &optional expand)
7750 "Set FILTER as the new agenda filter and apply it. Optional
7751 argument EXPAND can be used for the TYPE tag and will expand the
7752 tags in the FILTER if any of the tags in FILTER are grouptags."
7753 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7754 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7755 (let (tags cat txt)
7756 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7757 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7758 ;; category is used as the filter:
7759 (setq org-agenda-filtered-by-category
7760 (and (eq type 'category)
7761 (not (equal (substring (car filter) 0 1) "-"))))
7762 (org-agenda-set-mode-name)
7763 (save-excursion
7764 (goto-char (point-min))
7765 (while (not (eobp))
7766 (if (org-get-at-bol 'org-marker)
7767 (progn
7768 (setq tags (org-get-at-bol 'tags)
7769 cat (org-get-at-eol 'org-category 1)
7770 txt (org-get-at-bol 'txt))
7771 (if (not (eval org-agenda-filter-form))
7772 (org-agenda-filter-hide-line type))
7773 (beginning-of-line 2))
7774 (beginning-of-line 2))))
7775 (if (get-char-property (point) 'invisible)
7776 (ignore-errors (org-agenda-previous-line)))))
7778 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7779 "Filter by top headline HL."
7780 (org-agenda-set-mode-name)
7781 (save-excursion
7782 (goto-char (point-min))
7783 (while (not (eobp))
7784 (let* ((pos (org-get-at-bol 'org-hd-marker))
7785 (tophl (and pos (org-find-top-headline pos))))
7786 (if (and tophl (funcall (if negative 'identity 'not)
7787 (string= hl tophl)))
7788 (org-agenda-filter-hide-line 'top-headline)))
7789 (beginning-of-line 2)))
7790 (if (get-char-property (point) 'invisible)
7791 (org-agenda-previous-line))
7792 (setq org-agenda-top-headline-filter hl
7793 org-agenda-filtered-by-top-headline t))
7795 (defun org-agenda-filter-hide-line (type)
7796 "Hide lines with TYPE in the agenda buffer."
7797 (let* ((b (max (point-min) (1- (point-at-bol))))
7798 (e (point-at-eol)))
7799 (let ((inhibit-read-only t))
7800 (add-text-properties
7801 b e `(invisible org-filtered org-filter-type ,type)))))
7803 (defun org-agenda-remove-filter (type)
7804 (interactive)
7805 "Remove filter of type TYPE from the agenda buffer."
7806 (save-excursion
7807 (goto-char (point-min))
7808 (let ((inhibit-read-only t) pos)
7809 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7810 (goto-char pos)
7811 (remove-text-properties
7812 (point) (next-single-property-change (point) 'org-filter-type)
7813 `(invisible org-filtered org-filter-type ,type))))
7814 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7815 (setq org-agenda-filter-form nil)
7816 (org-agenda-set-mode-name)
7817 (org-agenda-finalize)))
7819 (defun org-agenda-filter-show-all-tag nil
7820 (org-agenda-remove-filter 'tag))
7821 (defun org-agenda-filter-show-all-re nil
7822 (org-agenda-remove-filter 'regexp))
7823 (defun org-agenda-filter-show-all-effort nil
7824 (org-agenda-remove-filter 'effort))
7825 (defun org-agenda-filter-show-all-cat nil
7826 (org-agenda-remove-filter 'category))
7827 (defun org-agenda-filter-show-all-top-filter nil
7828 (org-agenda-remove-filter 'top-headline))
7830 (defun org-agenda-manipulate-query-add ()
7831 "Manipulate the query by adding a search term with positive selection.
7832 Positive selection means the term must be matched for selection of an entry."
7833 (interactive)
7834 (org-agenda-manipulate-query ?\[))
7835 (defun org-agenda-manipulate-query-subtract ()
7836 "Manipulate the query by adding a search term with negative selection.
7837 Negative selection means term must not be matched for selection of an entry."
7838 (interactive)
7839 (org-agenda-manipulate-query ?\]))
7840 (defun org-agenda-manipulate-query-add-re ()
7841 "Manipulate the query by adding a search regexp with positive selection.
7842 Positive selection means the regexp must match for selection of an entry."
7843 (interactive)
7844 (org-agenda-manipulate-query ?\{))
7845 (defun org-agenda-manipulate-query-subtract-re ()
7846 "Manipulate the query by adding a search regexp with negative selection.
7847 Negative selection means regexp must not match for selection of an entry."
7848 (interactive)
7849 (org-agenda-manipulate-query ?\}))
7850 (defun org-agenda-manipulate-query (char)
7851 (cond
7852 ((memq org-agenda-type '(timeline agenda))
7853 (let ((org-agenda-include-inactive-timestamps t))
7854 (org-agenda-redo))
7855 (message "Display now includes inactive timestamps as well"))
7856 ((eq org-agenda-type 'search)
7857 (org-add-to-string
7858 'org-agenda-query-string
7859 (if org-agenda-last-search-view-search-was-boolean
7860 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7861 (?\{ . " +{}") (?\} . " -{}"))))
7862 " "))
7863 (setq org-agenda-redo-command
7864 (list 'org-search-view
7865 (car (get-text-property (min (1- (point-max)) (point))
7866 'org-last-args))
7867 org-agenda-query-string
7868 (+ (length org-agenda-query-string)
7869 (if (member char '(?\{ ?\})) 0 1))))
7870 (set-register org-agenda-query-register org-agenda-query-string)
7871 (let ((org-agenda-overriding-arguments
7872 (cdr org-agenda-redo-command)))
7873 (org-agenda-redo)))
7874 (t (error "Cannot manipulate query for %s-type agenda buffers"
7875 org-agenda-type))))
7877 (defun org-add-to-string (var string)
7878 (set var (concat (symbol-value var) string)))
7880 (defun org-agenda-goto-date (span)
7881 "Jump to DATE in agenda."
7882 (interactive "P")
7883 (let* ((org-read-date-prefer-future
7884 (eval org-agenda-jump-prefer-future))
7885 (date (org-read-date))
7886 (day (time-to-days (org-time-string-to-time date)))
7887 (org-agenda-sticky-orig org-agenda-sticky)
7888 (org-agenda-buffer-tmp-name (buffer-name))
7889 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7890 (0-arg (or current-prefix-arg (car args)))
7891 (2-arg (nth 2 args))
7892 (with-hour-p (nth 4 org-agenda-redo-command))
7893 (newcmd (list 'org-agenda-list 0-arg date
7894 (org-agenda-span-to-ndays
7895 2-arg (org-time-string-to-absolute date))
7896 with-hour-p))
7897 (newargs (cdr newcmd))
7898 (inhibit-read-only t)
7899 org-agenda-sticky)
7900 (if (not (org-agenda-check-type t 'agenda))
7901 (error "Not available in non-agenda views")
7902 (add-text-properties (point-min) (point-max)
7903 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7904 (org-agenda-redo)
7905 (goto-char (point-min))
7906 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7907 (save-excursion (move-beginning-of-line 2) (eobp))))
7908 (move-beginning-of-line 2))
7909 (setq org-agenda-sticky org-agenda-sticky-orig
7910 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7912 (defun org-agenda-goto-today ()
7913 "Go to today."
7914 (interactive)
7915 (org-agenda-check-type t 'timeline 'agenda)
7916 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7917 (curspan (nth 2 args))
7918 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7919 (cond
7920 (tdpos (goto-char tdpos))
7921 ((eq org-agenda-type 'agenda)
7922 (let* ((sd (org-agenda-compute-starting-span
7923 (org-today) (or curspan org-agenda-span)))
7924 (org-agenda-overriding-arguments args))
7925 (setf (nth 1 org-agenda-overriding-arguments) sd)
7926 (org-agenda-redo)
7927 (org-agenda-find-same-or-today-or-agenda)))
7928 (t (error "Cannot find today")))))
7930 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7931 (goto-char
7932 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7933 (text-property-any (point-min) (point-max) 'org-today t)
7934 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7935 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7936 (org-agenda-backward-block))
7937 (point-min))))
7939 (defun org-agenda-backward-block ()
7940 "Move backward by one agenda block."
7941 (interactive)
7942 (org-agenda-forward-block 'backward))
7944 (defun org-agenda-forward-block (&optional backward)
7945 "Move forward by one agenda block.
7946 When optional argument BACKWARD is set, go backward"
7947 (interactive)
7948 (cond ((not (derived-mode-p 'org-agenda-mode))
7949 (user-error
7950 "Cannot execute this command outside of org-agenda-mode buffers"))
7951 ((looking-at (if backward "\\`" "\\'"))
7952 (message "Already at the %s block" (if backward "first" "last")))
7953 (t (let ((pos (prog1 (point)
7954 (ignore-errors (if backward (backward-char 1)
7955 (move-end-of-line 1)))))
7956 (f (if backward
7957 'previous-single-property-change
7958 'next-single-property-change))
7959 moved dest)
7960 (while (and (setq dest (funcall
7961 f (point) 'org-agenda-structural-header))
7962 (not (get-text-property
7963 (point) 'org-agenda-structural-header)))
7964 (setq moved t)
7965 (goto-char dest))
7966 (if moved (move-beginning-of-line 1)
7967 (goto-char (if backward (point-min) (point-max)))
7968 (move-beginning-of-line 1)
7969 (message "No %s block" (if backward "previous" "further")))))))
7971 (defun org-agenda-later (arg)
7972 "Go forward in time by the current span.
7973 With prefix ARG, go forward that many times the current span."
7974 (interactive "p")
7975 (org-agenda-check-type t 'agenda)
7976 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7977 (span (or (nth 2 args) org-agenda-current-span))
7978 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7979 (greg (calendar-gregorian-from-absolute sd))
7980 (cnt (org-get-at-bol 'org-day-cnt))
7981 greg2)
7982 (cond
7983 ((numberp span)
7984 (setq sd (+ (* span arg) sd)))
7985 ((eq span 'day)
7986 (setq sd (+ arg sd)))
7987 ((eq span 'week)
7988 (setq sd (+ (* 7 arg) sd)))
7989 ((eq span 'fortnight)
7990 (setq sd (+ (* 14 arg) sd)))
7991 ((eq span 'month)
7992 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7993 sd (calendar-absolute-from-gregorian greg2))
7994 (setcar greg2 (1+ (car greg2))))
7995 ((eq span 'year)
7996 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7997 sd (calendar-absolute-from-gregorian greg2))
7998 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
8000 (setq sd (+ (* span arg) sd))))
8001 (let ((org-agenda-overriding-cmd
8002 ;; `cmd' may have been set by `org-agenda-run-series' which
8003 ;; uses `org-agenda-overriding-cmd' to decide whether
8004 ;; overriding is allowed for `cmd'
8005 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8006 (org-agenda-overriding-arguments
8007 (list (car args) sd span)))
8008 (org-agenda-redo)
8009 (org-agenda-find-same-or-today-or-agenda cnt))))
8011 (defun org-agenda-earlier (arg)
8012 "Go backward in time by the current span.
8013 With prefix ARG, go backward that many times the current span."
8014 (interactive "p")
8015 (org-agenda-later (- arg)))
8017 (defun org-agenda-view-mode-dispatch ()
8018 "Call one of the view mode commands."
8019 (interactive)
8020 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
8021 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
8022 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
8023 (pcase (read-char-exclusive)
8024 (?\ (call-interactively 'org-agenda-reset-view))
8025 (?d (call-interactively 'org-agenda-day-view))
8026 (?w (call-interactively 'org-agenda-week-view))
8027 (?t (call-interactively 'org-agenda-fortnight-view))
8028 (?m (call-interactively 'org-agenda-month-view))
8029 (?y (call-interactively 'org-agenda-year-view))
8030 (?l (call-interactively 'org-agenda-log-mode))
8031 (?L (org-agenda-log-mode '(4)))
8032 (?c (org-agenda-log-mode 'clockcheck))
8033 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
8034 (?a (call-interactively 'org-agenda-archives-mode))
8035 (?A (org-agenda-archives-mode 'files))
8036 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
8037 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
8038 (?G (call-interactively 'org-agenda-toggle-time-grid))
8039 (?D (call-interactively 'org-agenda-toggle-diary))
8040 (?\! (call-interactively 'org-agenda-toggle-deadlines))
8041 (?\[ (let ((org-agenda-include-inactive-timestamps t))
8042 (org-agenda-check-type t 'timeline 'agenda)
8043 (org-agenda-redo))
8044 (message "Display now includes inactive timestamps as well"))
8045 (?q (message "Abort"))
8046 (key (user-error "Invalid key: %s" key))))
8048 (defun org-agenda-reset-view ()
8049 "Switch to default view for agenda."
8050 (interactive)
8051 (org-agenda-change-time-span org-agenda-span))
8053 (defun org-agenda-day-view (&optional day-of-month)
8054 "Switch to daily view for agenda.
8055 With argument DAY-OF-MONTH, switch to that day of the month."
8056 (interactive "P")
8057 (org-agenda-change-time-span 'day day-of-month))
8059 (defun org-agenda-week-view (&optional iso-week)
8060 "Switch to weekly view for agenda.
8061 With argument ISO-WEEK, switch to the corresponding ISO week.
8062 If ISO-WEEK has more then 2 digits, only the last two encode
8063 the week. Any digits before this encode a year. So 200712
8064 means week 12 of year 2007. Years ranging from 70 years ago
8065 to 30 years in the future can also be written as 2-digit years."
8066 (interactive "P")
8067 (org-agenda-change-time-span 'week iso-week))
8069 (defun org-agenda-fortnight-view (&optional iso-week)
8070 "Switch to fortnightly view for agenda.
8071 With argument ISO-WEEK, switch to the corresponding ISO week.
8072 If ISO-WEEK has more then 2 digits, only the last two encode
8073 the week. Any digits before this encode a year. So 200712
8074 means week 12 of year 2007. Years ranging from 70 years ago
8075 to 30 years in the future can also be written as 2-digit years."
8076 (interactive "P")
8077 (org-agenda-change-time-span 'fortnight iso-week))
8079 (defun org-agenda-month-view (&optional month)
8080 "Switch to monthly view for agenda.
8081 With argument MONTH, switch to that month. If MONTH has more
8082 then 2 digits, only the last two encode the month. Any digits
8083 before this encode a year. So 200712 means December year 2007.
8084 Years ranging from 70 years ago to 30 years in the future can
8085 also be written as 2-digit years."
8086 (interactive "P")
8087 (org-agenda-change-time-span 'month month))
8089 (defun org-agenda-year-view (&optional year)
8090 "Switch to yearly view for agenda.
8091 With argument YEAR, switch to that year. Years ranging from 70
8092 years ago to 30 years in the future can also be written as
8093 2-digit years."
8094 (interactive "P")
8095 (when year
8096 (setq year (org-small-year-to-year year)))
8097 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8098 (org-agenda-change-time-span 'year year)
8099 (error "Abort")))
8101 (defun org-agenda-change-time-span (span &optional n)
8102 "Change the agenda view to SPAN.
8103 SPAN may be `day', `week', `fortnight', `month', `year'."
8104 (org-agenda-check-type t 'agenda)
8105 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8106 (curspan (nth 2 args)))
8107 (if (and (not n) (equal curspan span))
8108 (error "Viewing span is already \"%s\"" span))
8109 (let* ((sd (or (org-get-at-bol 'day)
8110 (nth 1 args)
8111 org-starting-day))
8112 (sd (org-agenda-compute-starting-span sd span n))
8113 (org-agenda-overriding-cmd
8114 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8115 (org-agenda-overriding-arguments
8116 (list (car args) sd span)))
8117 (org-agenda-redo)
8118 (org-agenda-find-same-or-today-or-agenda))
8119 (org-agenda-set-mode-name)
8120 (message "Switched to %s view" span)))
8122 (defun org-agenda-compute-starting-span (sd span &optional n)
8123 "Compute starting date for agenda.
8124 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8125 is a cons cell with the starting date and the number of days,
8126 so that the date SD will be in that range."
8127 (let* ((greg (calendar-gregorian-from-absolute sd))
8128 (dg (nth 1 greg))
8129 (mg (car greg))
8130 (yg (nth 2 greg)))
8131 (cond
8132 ((eq span 'day)
8133 (when n
8134 (setq sd (+ (calendar-absolute-from-gregorian
8135 (list mg 1 yg))
8136 n -1))))
8137 ((or (eq span 'week) (eq span 'fortnight))
8138 (let* ((nt (calendar-day-of-week
8139 (calendar-gregorian-from-absolute sd)))
8140 (d (if org-agenda-start-on-weekday
8141 (- nt org-agenda-start-on-weekday)
8144 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8145 (when n
8146 (require 'cal-iso)
8147 (when (> n 99)
8148 (setq y1 (org-small-year-to-year (/ n 100))
8149 n (mod n 100)))
8150 (setq sd
8151 (calendar-iso-to-absolute
8152 (list n 1
8153 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8154 ((eq span 'month)
8155 (let (y1)
8156 (when (and n (> n 99))
8157 (setq y1 (org-small-year-to-year (/ n 100))
8158 n (mod n 100)))
8159 (setq sd (calendar-absolute-from-gregorian
8160 (list (or n mg) 1 (or y1 yg))))))
8161 ((eq span 'year)
8162 (setq sd (calendar-absolute-from-gregorian
8163 (list 1 1 (or n yg))))))
8164 sd))
8166 (defun org-agenda-next-date-line (&optional arg)
8167 "Jump to the next line indicating a date in agenda buffer."
8168 (interactive "p")
8169 (org-agenda-check-type t 'agenda 'timeline)
8170 (beginning-of-line 1)
8171 ;; This does not work if user makes date format that starts with a blank
8172 (if (looking-at "^\\S-") (forward-char 1))
8173 (if (not (re-search-forward "^\\S-" nil t arg))
8174 (progn
8175 (backward-char 1)
8176 (error "No next date after this line in this buffer")))
8177 (goto-char (match-beginning 0)))
8179 (defun org-agenda-previous-date-line (&optional arg)
8180 "Jump to the previous line indicating a date in agenda buffer."
8181 (interactive "p")
8182 (org-agenda-check-type t 'agenda 'timeline)
8183 (beginning-of-line 1)
8184 (if (not (re-search-backward "^\\S-" nil t arg))
8185 (error "No previous date before this line in this buffer")))
8187 ;; Initialize the highlight
8188 (defvar org-hl (make-overlay 1 1))
8189 (overlay-put org-hl 'face 'highlight)
8191 (defun org-highlight (begin end &optional buffer)
8192 "Highlight a region with overlay."
8193 (move-overlay org-hl begin end (or buffer (current-buffer))))
8195 (defun org-unhighlight ()
8196 "Detach overlay INDEX."
8197 (delete-overlay org-hl))
8199 (defun org-unhighlight-once ()
8200 "Remove the highlight from its position, and this function from the hook."
8201 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8202 (org-unhighlight))
8204 (defvar org-agenda-pre-follow-window-conf nil)
8205 (defun org-agenda-follow-mode ()
8206 "Toggle follow mode in an agenda buffer."
8207 (interactive)
8208 (unless org-agenda-follow-mode
8209 (setq org-agenda-pre-follow-window-conf
8210 (current-window-configuration)))
8211 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8212 (unless org-agenda-follow-mode
8213 (set-window-configuration org-agenda-pre-follow-window-conf))
8214 (org-agenda-set-mode-name)
8215 (org-agenda-do-context-action)
8216 (message "Follow mode is %s"
8217 (if org-agenda-follow-mode "on" "off")))
8219 (defun org-agenda-entry-text-mode (&optional arg)
8220 "Toggle entry text mode in an agenda buffer."
8221 (interactive "P")
8222 (if (or org-agenda-tag-filter
8223 org-agenda-category-filter
8224 org-agenda-regexp-filter
8225 org-agenda-top-headline-filter)
8226 (user-error "Can't show entry text in filtered views")
8227 (setq org-agenda-entry-text-mode (or (integerp arg)
8228 (not org-agenda-entry-text-mode)))
8229 (org-agenda-entry-text-hide)
8230 (and org-agenda-entry-text-mode
8231 (let ((org-agenda-entry-text-maxlines
8232 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8233 (org-agenda-entry-text-show)))
8234 (org-agenda-set-mode-name)
8235 (message "Entry text mode is %s%s"
8236 (if org-agenda-entry-text-mode "on" "off")
8237 (if (not org-agenda-entry-text-mode) ""
8238 (format " (maximum number of lines is %d)"
8239 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8241 (defun org-agenda-clockreport-mode ()
8242 "Toggle clocktable mode in an agenda buffer."
8243 (interactive)
8244 (org-agenda-check-type t 'agenda)
8245 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8246 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8247 (org-agenda-set-mode-name)
8248 (org-agenda-redo)
8249 (message "Clocktable mode is %s"
8250 (if org-agenda-clockreport-mode "on" "off")))
8252 (defun org-agenda-log-mode (&optional special)
8253 "Toggle log mode in an agenda buffer.
8255 With argument SPECIAL, show all possible log items, not only the ones
8256 configured in `org-agenda-log-mode-items'.
8258 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8259 log items, nothing else."
8260 (interactive "P")
8261 (org-agenda-check-type t 'agenda 'timeline)
8262 (setq org-agenda-show-log
8263 (cond
8264 ((equal special '(16)) 'only)
8265 ((eq special 'clockcheck)
8266 (if (eq org-agenda-show-log 'clockcheck)
8267 nil 'clockcheck))
8268 (special '(closed clock state))
8269 (t (not org-agenda-show-log))))
8270 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8271 (org-agenda-set-mode-name)
8272 (org-agenda-redo)
8273 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8275 (defun org-agenda-archives-mode (&optional with-files)
8276 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8277 When called with a prefix argument, include all archive files as well."
8278 (interactive "P")
8279 (setq org-agenda-archives-mode
8280 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8281 (org-agenda-set-mode-name)
8282 (org-agenda-redo)
8283 (message
8284 "%s"
8285 (cond
8286 ((eq org-agenda-archives-mode nil)
8287 "No archives are included")
8288 ((eq org-agenda-archives-mode 'trees)
8289 (format "Trees with :%s: tag are included" org-archive-tag))
8290 ((eq org-agenda-archives-mode t)
8291 (format "Trees with :%s: tag and all active archive files are included"
8292 org-archive-tag)))))
8294 (defun org-agenda-toggle-diary ()
8295 "Toggle diary inclusion in an agenda buffer."
8296 (interactive)
8297 (org-agenda-check-type t 'agenda)
8298 (setq org-agenda-include-diary (not org-agenda-include-diary))
8299 (org-agenda-redo)
8300 (org-agenda-set-mode-name)
8301 (message "Diary inclusion turned %s"
8302 (if org-agenda-include-diary "on" "off")))
8304 (defun org-agenda-toggle-deadlines ()
8305 "Toggle inclusion of entries with a deadline in an agenda buffer."
8306 (interactive)
8307 (org-agenda-check-type t 'agenda)
8308 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8309 (org-agenda-redo)
8310 (org-agenda-set-mode-name)
8311 (message "Deadlines inclusion turned %s"
8312 (if org-agenda-include-deadlines "on" "off")))
8314 (defun org-agenda-toggle-time-grid ()
8315 "Toggle time grid in an agenda buffer."
8316 (interactive)
8317 (org-agenda-check-type t 'agenda)
8318 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8319 (org-agenda-redo)
8320 (org-agenda-set-mode-name)
8321 (message "Time-grid turned %s"
8322 (if org-agenda-use-time-grid "on" "off")))
8324 (defun org-agenda-set-mode-name ()
8325 "Set the mode name to indicate all the small mode settings."
8326 (setq mode-name
8327 (list "Org-Agenda"
8328 (if (get 'org-agenda-files 'org-restrict) " []" "")
8330 '(:eval (org-agenda-span-name org-agenda-current-span))
8331 (if org-agenda-follow-mode " Follow" "")
8332 (if org-agenda-entry-text-mode " ETxt" "")
8333 (if org-agenda-include-diary " Diary" "")
8334 (if org-agenda-include-deadlines " Ddl" "")
8335 (if org-agenda-use-time-grid " Grid" "")
8336 (if (and (boundp 'org-habit-show-habits)
8337 org-habit-show-habits) " Habit" "")
8338 (cond
8339 ((consp org-agenda-show-log) " LogAll")
8340 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8341 (org-agenda-show-log " Log")
8342 (t ""))
8343 (if (or org-agenda-category-filter
8344 (get 'org-agenda-category-filter :preset-filter))
8345 '(:eval (propertize
8346 (concat " <"
8347 (mapconcat
8348 'identity
8349 (append
8350 (get 'org-agenda-category-filter :preset-filter)
8351 org-agenda-category-filter)
8353 ">")
8354 'face 'org-agenda-filter-category
8355 'help-echo "Category used in filtering")) "")
8356 (if (or org-agenda-tag-filter
8357 (get 'org-agenda-tag-filter :preset-filter))
8358 '(:eval (propertize
8359 (concat " {"
8360 (mapconcat
8361 'identity
8362 (append
8363 (get 'org-agenda-tag-filter :preset-filter)
8364 org-agenda-tag-filter)
8366 "}")
8367 'face 'org-agenda-filter-tags
8368 'help-echo "Tags used in filtering")) "")
8369 (if (or org-agenda-effort-filter
8370 (get 'org-agenda-effort-filter :preset-filter))
8371 '(:eval (propertize
8372 (concat " {"
8373 (mapconcat
8374 'identity
8375 (append
8376 (get 'org-agenda-effort-filter :preset-filter)
8377 org-agenda-effort-filter)
8379 "}")
8380 'face 'org-agenda-filter-effort
8381 'help-echo "Effort conditions used in filtering")) "")
8382 (if (or org-agenda-regexp-filter
8383 (get 'org-agenda-regexp-filter :preset-filter))
8384 '(:eval (propertize
8385 (concat " ["
8386 (mapconcat
8387 'identity
8388 (append
8389 (get 'org-agenda-regexp-filter :preset-filter)
8390 org-agenda-regexp-filter)
8392 "]")
8393 'face 'org-agenda-filter-regexp
8394 'help-echo "Regexp used in filtering")) "")
8395 (if org-agenda-archives-mode
8396 (if (eq org-agenda-archives-mode t)
8397 " Archives"
8398 (format " :%s:" org-archive-tag))
8400 (if org-agenda-clockreport-mode " Clock" "")))
8401 (force-mode-line-update))
8403 (defun org-agenda-update-agenda-type ()
8404 "Update the agenda type after each command."
8405 (setq org-agenda-type
8406 (or (get-text-property (point) 'org-agenda-type)
8407 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8409 (defun org-agenda-next-line ()
8410 "Move cursor to the next line, and show if follow mode is active."
8411 (interactive)
8412 (call-interactively 'next-line)
8413 (org-agenda-do-context-action))
8415 (defun org-agenda-previous-line ()
8416 "Move cursor to the previous line, and show if follow-mode is active."
8417 (interactive)
8418 (call-interactively 'previous-line)
8419 (org-agenda-do-context-action))
8421 (defun org-agenda-next-item (n)
8422 "Move cursor to next agenda item."
8423 (interactive "p")
8424 (let ((col (current-column)))
8425 (dotimes (c n)
8426 (when (next-single-property-change (point-at-eol) 'org-marker)
8427 (move-end-of-line 1)
8428 (goto-char (next-single-property-change (point) 'org-marker))))
8429 (org-move-to-column col))
8430 (org-agenda-do-context-action))
8432 (defun org-agenda-previous-item (n)
8433 "Move cursor to next agenda item."
8434 (interactive "p")
8435 (dotimes (c n)
8436 (let ((col (current-column))
8437 (goto (save-excursion
8438 (move-end-of-line 0)
8439 (previous-single-property-change (point) 'org-marker))))
8440 (if goto (goto-char goto))
8441 (org-move-to-column col)))
8442 (org-agenda-do-context-action))
8444 (defun org-agenda-do-context-action ()
8445 "Show outline path and, maybe, follow mode window."
8446 (let ((m (org-get-at-bol 'org-marker)))
8447 (when (and (markerp m) (marker-buffer m))
8448 (and org-agenda-follow-mode
8449 (if org-agenda-follow-indirect
8450 (org-agenda-tree-to-indirect-buffer nil)
8451 (org-agenda-show)))
8452 (and org-agenda-show-outline-path
8453 (org-with-point-at m (org-display-outline-path t))))))
8455 (defun org-agenda-show-tags ()
8456 "Show the tags applicable to the current item."
8457 (interactive)
8458 (let* ((tags (org-get-at-bol 'tags)))
8459 (if tags
8460 (message "Tags are :%s:"
8461 (org-no-properties (mapconcat 'identity tags ":")))
8462 (message "No tags associated with this line"))))
8464 (defun org-agenda-goto (&optional highlight)
8465 "Go to the entry at point in the corresponding Org file."
8466 (interactive)
8467 (let* ((marker (or (org-get-at-bol 'org-marker)
8468 (org-agenda-error)))
8469 (buffer (marker-buffer marker))
8470 (pos (marker-position marker)))
8471 (switch-to-buffer-other-window buffer)
8472 (widen)
8473 (push-mark)
8474 (goto-char pos)
8475 (when (derived-mode-p 'org-mode)
8476 (org-show-context 'agenda)
8477 (recenter (/ (window-height) 2))
8478 (org-back-to-heading t)
8479 (let ((case-fold-search nil))
8480 (when (re-search-forward org-complex-heading-regexp nil t)
8481 (goto-char (match-beginning 4)))))
8482 (run-hooks 'org-agenda-after-show-hook)
8483 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8485 (defvar org-agenda-after-show-hook nil
8486 "Normal hook run after an item has been shown from the agenda.
8487 Point is in the buffer where the item originated.")
8489 (defun org-agenda-kill ()
8490 "Kill the entry or subtree belonging to the current agenda entry."
8491 (interactive)
8492 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8493 (let* ((bufname-orig (buffer-name))
8494 (marker (or (org-get-at-bol 'org-marker)
8495 (org-agenda-error)))
8496 (buffer (marker-buffer marker))
8497 (pos (marker-position marker))
8498 (type (org-get-at-bol 'type))
8499 dbeg dend (n 0) conf)
8500 (org-with-remote-undo buffer
8501 (with-current-buffer buffer
8502 (save-excursion
8503 (goto-char pos)
8504 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8505 (setq dbeg (progn (org-back-to-heading t) (point))
8506 dend (org-end-of-subtree t t))
8507 (setq dbeg (point-at-bol)
8508 dend (min (point-max) (1+ (point-at-eol)))))
8509 (goto-char dbeg)
8510 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8511 (setq conf (or (eq t org-agenda-confirm-kill)
8512 (and (numberp org-agenda-confirm-kill)
8513 (> n org-agenda-confirm-kill))))
8514 (and conf
8515 (not (y-or-n-p
8516 (format "Delete entry with %d lines in buffer \"%s\"? "
8517 n (buffer-name buffer))))
8518 (error "Abort"))
8519 (let ((org-agenda-buffer-name bufname-orig))
8520 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8521 (with-current-buffer buffer (delete-region dbeg dend))
8522 (message "Agenda item and source killed"))))
8524 (defvar org-archive-default-command) ; defined in org-archive.el
8525 (defun org-agenda-archive-default ()
8526 "Archive the entry or subtree belonging to the current agenda entry."
8527 (interactive)
8528 (require 'org-archive)
8529 (org-agenda-archive-with org-archive-default-command))
8531 (defun org-agenda-archive-default-with-confirmation ()
8532 "Archive the entry or subtree belonging to the current agenda entry."
8533 (interactive)
8534 (require 'org-archive)
8535 (org-agenda-archive-with org-archive-default-command 'confirm))
8537 (defun org-agenda-archive ()
8538 "Archive the entry or subtree belonging to the current agenda entry."
8539 (interactive)
8540 (org-agenda-archive-with 'org-archive-subtree))
8542 (defun org-agenda-archive-to-archive-sibling ()
8543 "Move the entry to the archive sibling."
8544 (interactive)
8545 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8547 (defun org-agenda-archive-with (cmd &optional confirm)
8548 "Move the entry to the archive sibling."
8549 (interactive)
8550 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8551 (let* ((bufname-orig (buffer-name))
8552 (marker (or (org-get-at-bol 'org-marker)
8553 (org-agenda-error)))
8554 (buffer (marker-buffer marker))
8555 (pos (marker-position marker)))
8556 (org-with-remote-undo buffer
8557 (with-current-buffer buffer
8558 (if (derived-mode-p 'org-mode)
8559 (if (and confirm
8560 (not (y-or-n-p "Archive this subtree or entry? ")))
8561 (error "Abort")
8562 (save-window-excursion
8563 (goto-char pos)
8564 (let ((org-agenda-buffer-name bufname-orig))
8565 (org-remove-subtree-entries-from-agenda))
8566 (org-back-to-heading t)
8567 (funcall cmd)))
8568 (error "Archiving works only in Org files"))))))
8570 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8571 "Remove all lines in the agenda that correspond to a given subtree.
8572 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8573 If this information is not given, the function uses the tree at point."
8574 (let ((buf (or buf (current-buffer))) m p)
8575 (save-excursion
8576 (unless (and beg end)
8577 (org-back-to-heading t)
8578 (setq beg (point))
8579 (org-end-of-subtree t)
8580 (setq end (point)))
8581 (set-buffer (get-buffer org-agenda-buffer-name))
8582 (save-excursion
8583 (goto-char (point-max))
8584 (beginning-of-line 1)
8585 (while (not (bobp))
8586 (when (and (setq m (org-get-at-bol 'org-marker))
8587 (equal buf (marker-buffer m))
8588 (setq p (marker-position m))
8589 (>= p beg)
8590 (< p end))
8591 (let ((inhibit-read-only t))
8592 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8593 (beginning-of-line 0))))))
8595 (defun org-agenda-refile (&optional goto rfloc no-update)
8596 "Refile the item at point.
8598 When called with `\\[universal-argument] \\[universal-argument]', \
8599 go to the location of the last
8600 refiled item.
8602 When called with `\\[universal-argument] \\[universal-argument] \
8603 \\[universal-argument]' prefix or when GOTO is 0, clear
8604 the refile cache.
8606 RFLOC can be a refile location obtained in a different way.
8608 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8609 (interactive "P")
8610 (cond
8611 ((member goto '(0 (64)))
8612 (org-refile-cache-clear))
8613 ((equal goto '(16))
8614 (org-refile-goto-last-stored))
8616 (let* ((buffer-orig (buffer-name))
8617 (marker (or (org-get-at-bol 'org-hd-marker)
8618 (org-agenda-error)))
8619 (buffer (marker-buffer marker))
8620 (pos (marker-position marker))
8621 (rfloc (or rfloc
8622 (org-refile-get-location
8623 (if goto "Goto" "Refile to") buffer
8624 org-refile-allow-creating-parent-nodes))))
8625 (with-current-buffer buffer
8626 (org-with-wide-buffer
8627 (goto-char marker)
8628 (let ((org-agenda-buffer-name buffer-orig))
8629 (org-remove-subtree-entries-from-agenda))
8630 (org-refile goto buffer rfloc))))
8631 (unless no-update (org-agenda-redo)))))
8633 (defun org-agenda-open-link (&optional arg)
8634 "Open the link(s) in the current entry, if any.
8635 This looks for a link in the displayed line in the agenda.
8636 It also looks at the text of the entry itself."
8637 (interactive "P")
8638 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8639 (org-get-at-bol 'org-marker)))
8640 (buffer (and marker (marker-buffer marker)))
8641 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8642 (lkall (and buffer (org-offer-links-in-entry
8643 buffer marker arg prefix)))
8644 (lk0 (car lkall))
8645 (lk (if (stringp lk0) (list lk0) lk0))
8646 (lkend (cdr lkall))
8647 trg)
8648 (cond
8649 ((and buffer lk)
8650 (mapcar (lambda(l)
8651 (with-current-buffer buffer
8652 (setq trg (and (string-match org-bracket-link-regexp l)
8653 (match-string 1 l)))
8654 (if (or (not trg) (string-match org-any-link-re trg))
8655 (org-with-wide-buffer
8656 (goto-char marker)
8657 (when (search-forward l nil lkend)
8658 (goto-char (match-beginning 0))
8659 (org-open-at-point)))
8660 ;; This is an internal link, widen the buffer
8661 (switch-to-buffer-other-window buffer)
8662 (widen)
8663 (goto-char marker)
8664 (when (search-forward l nil lkend)
8665 (goto-char (match-beginning 0))
8666 (org-open-at-point)))))
8667 lk))
8668 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8669 (save-excursion
8670 (beginning-of-line 1)
8671 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8672 (org-open-link-from-string (match-string 1)))
8673 (t (message "No link to open here")))))
8675 (defun org-agenda-copy-local-variable (var)
8676 "Get a variable from a referenced buffer and install it here."
8677 (let ((m (org-get-at-bol 'org-marker)))
8678 (when (and m (buffer-live-p (marker-buffer m)))
8679 (set (make-local-variable var)
8680 (with-current-buffer (marker-buffer m)
8681 (symbol-value var))))))
8683 (defun org-agenda-switch-to (&optional delete-other-windows)
8684 "Go to the Org mode file which contains the item at point.
8685 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8686 displayed Org file fills the frame."
8687 (interactive)
8688 (if (and org-return-follows-link
8689 (not (org-get-at-bol 'org-marker))
8690 (org-in-regexp org-bracket-link-regexp))
8691 (org-open-link-from-string (match-string 0))
8692 (let* ((marker (or (org-get-at-bol 'org-marker)
8693 (org-agenda-error)))
8694 (buffer (marker-buffer marker))
8695 (pos (marker-position marker)))
8696 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8697 (pop-to-buffer-same-window buffer)
8698 (when delete-other-windows (delete-other-windows))
8699 (widen)
8700 (goto-char pos)
8701 (when (derived-mode-p 'org-mode)
8702 (org-show-context 'agenda)
8703 (run-hooks 'org-agenda-after-show-hook)))))
8705 (defun org-agenda-goto-mouse (ev)
8706 "Go to the Org file which contains the item at the mouse click."
8707 (interactive "e")
8708 (mouse-set-point ev)
8709 (org-agenda-goto))
8711 (defun org-agenda-show (&optional full-entry)
8712 "Display the Org file which contains the item at point.
8713 With prefix argument FULL-ENTRY, make the entire entry visible
8714 if it was hidden in the outline."
8715 (interactive "P")
8716 (let ((win (selected-window)))
8717 (org-agenda-goto t)
8718 (when full-entry (org-show-entry))
8719 (select-window win)))
8721 (defvar org-agenda-show-window nil)
8722 (defun org-agenda-show-and-scroll-up (&optional arg)
8723 "Display the Org file which contains the item at point.
8725 When called repeatedly, scroll the window that is displaying the buffer.
8727 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8728 `outline-show-subtree'
8729 to display the item, so that drawers and logbooks stay folded."
8730 (interactive "P")
8731 (let ((win (selected-window)))
8732 (if (and (window-live-p org-agenda-show-window)
8733 (eq this-command last-command))
8734 (progn
8735 (select-window org-agenda-show-window)
8736 (ignore-errors (scroll-up)))
8737 (org-agenda-goto t)
8738 (if arg (org-show-entry) (outline-show-subtree))
8739 (setq org-agenda-show-window (selected-window)))
8740 (select-window win)))
8742 (defun org-agenda-show-scroll-down ()
8743 "Scroll down the window showing the agenda."
8744 (interactive)
8745 (let ((win (selected-window)))
8746 (when (window-live-p org-agenda-show-window)
8747 (select-window org-agenda-show-window)
8748 (ignore-errors (scroll-down))
8749 (select-window win))))
8751 (defun org-agenda-show-1 (&optional more)
8752 "Display the Org file which contains the item at point.
8753 The prefix arg selects the amount of information to display:
8755 0 hide the subtree
8756 1 just show the entry according to defaults.
8757 2 show the children view
8758 3 show the subtree view
8759 4 show the entire subtree and any LOGBOOK drawers
8760 5 show the entire subtree and any drawers
8761 With prefix argument FULL-ENTRY, make the entire entry visible
8762 if it was hidden in the outline."
8763 (interactive "p")
8764 (let ((win (selected-window)))
8765 (org-agenda-goto t)
8766 (org-back-to-heading)
8767 (set-window-start (selected-window) (point-at-bol))
8768 (cond
8769 ((= more 0)
8770 (outline-hide-subtree)
8771 (save-excursion
8772 (org-back-to-heading)
8773 (run-hook-with-args 'org-cycle-hook 'folded))
8774 (message "Remote: FOLDED"))
8775 ((and (called-interactively-p 'any) (= more 1))
8776 (message "Remote: show with default settings"))
8777 ((= more 2)
8778 (outline-show-entry)
8779 (org-show-children)
8780 (save-excursion
8781 (org-back-to-heading)
8782 (run-hook-with-args 'org-cycle-hook 'children))
8783 (message "Remote: CHILDREN"))
8784 ((= more 3)
8785 (outline-show-subtree)
8786 (save-excursion
8787 (org-back-to-heading)
8788 (run-hook-with-args 'org-cycle-hook 'subtree))
8789 (message "Remote: SUBTREE"))
8790 ((= more 4)
8791 (outline-show-subtree)
8792 (save-excursion
8793 (org-back-to-heading)
8794 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8795 (message "Remote: SUBTREE AND LOGBOOK"))
8796 ((> more 4)
8797 (outline-show-subtree)
8798 (message "Remote: SUBTREE AND ALL DRAWERS")))
8799 (select-window win)))
8801 (defvar org-agenda-cycle-counter nil)
8802 (defun org-agenda-cycle-show (&optional n)
8803 "Show the current entry in another window, with default settings.
8805 Default settings are taken from `org-show-context-detail'. When
8806 use repeatedly in immediate succession, the remote entry will
8807 cycle through visibility
8809 children -> subtree -> folded
8811 When called with a numeric prefix arg, that arg will be passed through to
8812 `org-agenda-show-1'. For the interpretation of that argument, see the
8813 docstring of `org-agenda-show-1'."
8814 (interactive "P")
8815 (if (integerp n)
8816 (setq org-agenda-cycle-counter n)
8817 (if (not (eq last-command this-command))
8818 (setq org-agenda-cycle-counter 1)
8819 (if (equal org-agenda-cycle-counter 0)
8820 (setq org-agenda-cycle-counter 2)
8821 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8822 (if (> org-agenda-cycle-counter 3)
8823 (setq org-agenda-cycle-counter 0)))))
8824 (org-agenda-show-1 org-agenda-cycle-counter))
8826 (defun org-agenda-recenter (arg)
8827 "Display the Org file which contains the item at point and recenter."
8828 (interactive "P")
8829 (let ((win (selected-window)))
8830 (org-agenda-goto t)
8831 (recenter arg)
8832 (select-window win)))
8834 (defun org-agenda-show-mouse (ev)
8835 "Display the Org file which contains the item at the mouse click."
8836 (interactive "e")
8837 (mouse-set-point ev)
8838 (org-agenda-show))
8840 (defun org-agenda-check-no-diary ()
8841 "Check if the entry is a diary link and abort if yes."
8842 (if (org-get-at-bol 'org-agenda-diary-link)
8843 (org-agenda-error)))
8845 (defun org-agenda-error ()
8846 "Throw an error when a command is not allowed in the agenda."
8847 (user-error "Command not allowed in this line"))
8849 (defun org-agenda-tree-to-indirect-buffer (arg)
8850 "Show the subtree corresponding to the current entry in an indirect buffer.
8851 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8853 With a numerical prefix ARG, go up to this level and then take that tree.
8854 With a negative numeric ARG, go up by this number of levels.
8856 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8857 i.e. don't use
8858 the dedicated frame."
8859 (interactive "P")
8860 (if current-prefix-arg
8861 (org-agenda-do-tree-to-indirect-buffer arg)
8862 (let ((agenda-buffer (buffer-name))
8863 (agenda-window (selected-window))
8864 (indirect-window
8865 (and org-last-indirect-buffer
8866 (get-buffer-window org-last-indirect-buffer))))
8867 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8868 (unless (or (eq org-indirect-buffer-display 'new-frame)
8869 (eq org-indirect-buffer-display 'dedicated-frame))
8870 (unwind-protect
8871 (unless (and indirect-window (window-live-p indirect-window))
8872 (setq indirect-window (split-window agenda-window)))
8873 (and indirect-window (select-window indirect-window))
8874 (switch-to-buffer org-last-indirect-buffer :norecord)
8875 (fit-window-to-buffer indirect-window)))
8876 (select-window (get-buffer-window agenda-buffer))
8877 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8879 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8880 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8881 (org-agenda-check-no-diary)
8882 (let* ((marker (or (org-get-at-bol 'org-marker)
8883 (org-agenda-error)))
8884 (buffer (marker-buffer marker))
8885 (pos (marker-position marker)))
8886 (with-current-buffer buffer
8887 (save-excursion
8888 (goto-char pos)
8889 (funcall 'org-tree-to-indirect-buffer arg)))))
8891 (defvar org-last-heading-marker (make-marker)
8892 "Marker pointing to the headline that last changed its TODO state
8893 by a remote command from the agenda.")
8895 (defun org-agenda-todo-nextset ()
8896 "Switch TODO entry to next sequence."
8897 (interactive)
8898 (org-agenda-todo 'nextset))
8900 (defun org-agenda-todo-previousset ()
8901 "Switch TODO entry to previous sequence."
8902 (interactive)
8903 (org-agenda-todo 'previousset))
8905 (defun org-agenda-todo (&optional arg)
8906 "Cycle TODO state of line at point, also in Org file.
8907 This changes the line at point, all other lines in the agenda referring to
8908 the same tree node, and the headline of the tree node in the Org file."
8909 (interactive "P")
8910 (org-agenda-check-no-diary)
8911 (let* ((col (current-column))
8912 (marker (or (org-get-at-bol 'org-marker)
8913 (org-agenda-error)))
8914 (buffer (marker-buffer marker))
8915 (pos (marker-position marker))
8916 (hdmarker (org-get-at-bol 'org-hd-marker))
8917 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8918 (inhibit-read-only t)
8919 org-agenda-headline-snapshot-before-repeat newhead just-one)
8920 (org-with-remote-undo buffer
8921 (with-current-buffer buffer
8922 (widen)
8923 (goto-char pos)
8924 (org-show-context 'agenda)
8925 (let ((current-prefix-arg arg))
8926 (call-interactively 'org-todo))
8927 (and (bolp) (forward-char 1))
8928 (setq newhead (org-get-heading))
8929 (when (and (bound-and-true-p
8930 org-agenda-headline-snapshot-before-repeat)
8931 (not (equal org-agenda-headline-snapshot-before-repeat
8932 newhead))
8933 todayp)
8934 (setq newhead org-agenda-headline-snapshot-before-repeat
8935 just-one t))
8936 (save-excursion
8937 (org-back-to-heading)
8938 (move-marker org-last-heading-marker (point))))
8939 (beginning-of-line 1)
8940 (save-window-excursion
8941 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8942 (when (bound-and-true-p org-clock-out-when-done)
8943 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8944 newhead)
8945 (org-agenda-unmark-clocking-task))
8946 (org-move-to-column col)
8947 (org-agenda-mark-clocking-task))))
8949 (defun org-agenda-add-note (&optional arg)
8950 "Add a time-stamped note to the entry at point."
8951 (interactive "P")
8952 (org-agenda-check-no-diary)
8953 (let* ((marker (or (org-get-at-bol 'org-marker)
8954 (org-agenda-error)))
8955 (buffer (marker-buffer marker))
8956 (pos (marker-position marker))
8957 (hdmarker (org-get-at-bol 'org-hd-marker))
8958 (inhibit-read-only t))
8959 (with-current-buffer buffer
8960 (widen)
8961 (goto-char pos)
8962 (org-show-context 'agenda)
8963 (org-add-note))))
8965 (defun org-agenda-change-all-lines (newhead hdmarker
8966 &optional fixface just-this)
8967 "Change all lines in the agenda buffer which match HDMARKER.
8968 The new content of the line will be NEWHEAD (as modified by
8969 `org-agenda-format-item'). HDMARKER is checked with
8970 `equal' against all `org-hd-marker' text properties in the file.
8971 If FIXFACE is non-nil, the face of each item is modified according to
8972 the new TODO state.
8973 If JUST-THIS is non-nil, change just the current line, not all.
8974 If FORCE-TAGS is non nil, the car of it returns the new tags."
8975 (let* ((inhibit-read-only t)
8976 (line (org-current-line))
8977 (org-agenda-buffer (current-buffer))
8978 (thetags (with-current-buffer (marker-buffer hdmarker)
8979 (org-with-wide-buffer
8980 (goto-char hdmarker)
8981 (org-get-tags-at))))
8982 props m pl undone-face done-face finish new dotime level cat tags)
8983 (save-excursion
8984 (goto-char (point-max))
8985 (beginning-of-line 1)
8986 (while (not finish)
8987 (setq finish (bobp))
8988 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8989 (or (not just-this) (= (org-current-line) line))
8990 (equal m hdmarker))
8991 (setq props (text-properties-at (point))
8992 dotime (org-get-at-bol 'dotime)
8993 cat (org-get-at-eol 'org-category 1)
8994 level (org-get-at-bol 'level)
8995 tags thetags
8997 (let ((org-prefix-format-compiled
8998 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8999 org-prefix-format-compiled))
9000 (extra (org-get-at-bol 'extra)))
9001 (with-current-buffer (marker-buffer hdmarker)
9002 (org-with-wide-buffer
9003 (org-agenda-format-item extra newhead level cat tags dotime))))
9004 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
9005 undone-face (org-get-at-bol 'undone-face)
9006 done-face (org-get-at-bol 'done-face))
9007 (beginning-of-line 1)
9008 (cond
9009 ((equal new "") (delete-region (point) (line-beginning-position 2)))
9010 ((looking-at ".*")
9011 ;; When replacing the whole line, preserve bulk mark
9012 ;; overlay, if any.
9013 (let ((mark (catch :overlay
9014 (dolist (o (overlays-in (point) (+ 2 (point))))
9015 (when (eq (overlay-get o 'type)
9016 'org-marked-entry-overlay)
9017 (throw :overlay o))))))
9018 (replace-match new t t)
9019 (beginning-of-line)
9020 (when mark (move-overlay mark (point) (+ 2 (point)))))
9021 (add-text-properties (point-at-bol) (point-at-eol) props)
9022 (when fixface
9023 (add-text-properties
9024 (point-at-bol) (point-at-eol)
9025 (list 'face
9026 (if org-last-todo-state-is-todo
9027 undone-face done-face))))
9028 (org-agenda-highlight-todo 'line)
9029 (beginning-of-line 1))
9030 (t (error "Line update did not work")))
9031 (save-restriction
9032 (narrow-to-region (point-at-bol) (point-at-eol))
9033 (org-agenda-finalize)))
9034 (beginning-of-line 0)))))
9036 (defun org-agenda-align-tags (&optional line)
9037 "Align all tags in agenda items to `org-agenda-tags-column'."
9038 (let ((inhibit-read-only t) l c)
9039 (save-excursion
9040 (goto-char (if line (point-at-bol) (point-min)))
9041 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
9042 (if line (point-at-eol) nil) t)
9043 (add-text-properties
9044 (match-beginning 2) (match-end 2)
9045 (list 'face (delq nil (let ((prop (get-text-property
9046 (match-beginning 2) 'face)))
9047 (or (listp prop) (setq prop (list prop)))
9048 (if (memq 'org-tag prop)
9049 prop
9050 (cons 'org-tag prop))))))
9051 (setq l (- (match-end 2) (match-beginning 2))
9052 c (if (< org-agenda-tags-column 0)
9053 (- (abs org-agenda-tags-column) l)
9054 org-agenda-tags-column))
9055 (delete-region (match-beginning 1) (match-end 1))
9056 (goto-char (match-beginning 1))
9057 (insert (org-add-props
9058 (make-string (max 1 (- c (current-column))) ?\ )
9059 (plist-put (copy-sequence (text-properties-at (point)))
9060 'face nil))))
9061 (goto-char (point-min))
9062 (org-font-lock-add-tag-faces (point-max)))))
9064 (defun org-agenda-priority-up ()
9065 "Increase the priority of line at point, also in Org file."
9066 (interactive)
9067 (org-agenda-priority 'up))
9069 (defun org-agenda-priority-down ()
9070 "Decrease the priority of line at point, also in Org file."
9071 (interactive)
9072 (org-agenda-priority 'down))
9074 (defun org-agenda-priority (&optional force-direction)
9075 "Set the priority of line at point, also in Org file.
9076 This changes the line at point, all other lines in the agenda referring to
9077 the same tree node, and the headline of the tree node in the Org file.
9078 Called with a universal prefix arg, show the priority instead of setting it."
9079 (interactive "P")
9080 (if (equal force-direction '(4))
9081 (org-show-priority)
9082 (unless org-enable-priority-commands
9083 (error "Priority commands are disabled"))
9084 (org-agenda-check-no-diary)
9085 (let* ((col (current-column))
9086 (marker (or (org-get-at-bol 'org-marker)
9087 (org-agenda-error)))
9088 (hdmarker (org-get-at-bol 'org-hd-marker))
9089 (buffer (marker-buffer hdmarker))
9090 (pos (marker-position hdmarker))
9091 (inhibit-read-only t)
9092 newhead)
9093 (org-with-remote-undo buffer
9094 (with-current-buffer buffer
9095 (widen)
9096 (goto-char pos)
9097 (org-show-context 'agenda)
9098 (funcall 'org-priority force-direction)
9099 (end-of-line 1)
9100 (setq newhead (org-get-heading)))
9101 (org-agenda-change-all-lines newhead hdmarker)
9102 (org-move-to-column col)))))
9104 ;; FIXME: should fix the tags property of the agenda line.
9105 (defun org-agenda-set-tags (&optional tag onoff)
9106 "Set tags for the current headline."
9107 (interactive)
9108 (org-agenda-check-no-diary)
9109 (if (and (org-region-active-p) (called-interactively-p 'any))
9110 (call-interactively 'org-change-tag-in-region)
9111 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9112 (org-agenda-error)))
9113 (buffer (marker-buffer hdmarker))
9114 (pos (marker-position hdmarker))
9115 (inhibit-read-only t)
9116 newhead)
9117 (org-with-remote-undo buffer
9118 (with-current-buffer buffer
9119 (widen)
9120 (goto-char pos)
9121 (org-show-context 'agenda)
9122 (if tag
9123 (org-toggle-tag tag onoff)
9124 (call-interactively 'org-set-tags))
9125 (end-of-line 1)
9126 (setq newhead (org-get-heading)))
9127 (org-agenda-change-all-lines newhead hdmarker)
9128 (beginning-of-line 1)))))
9130 (defun org-agenda-set-property ()
9131 "Set a property for the current headline."
9132 (interactive)
9133 (org-agenda-check-no-diary)
9134 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9135 (org-agenda-error)))
9136 (buffer (marker-buffer hdmarker))
9137 (pos (marker-position hdmarker))
9138 (inhibit-read-only t)
9139 newhead)
9140 (org-with-remote-undo buffer
9141 (with-current-buffer buffer
9142 (widen)
9143 (goto-char pos)
9144 (org-show-context 'agenda)
9145 (call-interactively 'org-set-property)))))
9147 (defun org-agenda-set-effort ()
9148 "Set the effort property for the current headline."
9149 (interactive)
9150 (org-agenda-check-no-diary)
9151 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9152 (org-agenda-error)))
9153 (buffer (marker-buffer hdmarker))
9154 (pos (marker-position hdmarker))
9155 (inhibit-read-only t)
9156 newhead)
9157 (org-with-remote-undo buffer
9158 (with-current-buffer buffer
9159 (widen)
9160 (goto-char pos)
9161 (org-show-context 'agenda)
9162 (call-interactively 'org-set-effort)
9163 (end-of-line 1)
9164 (setq newhead (org-get-heading)))
9165 (org-agenda-change-all-lines newhead hdmarker))))
9167 (defun org-agenda-toggle-archive-tag ()
9168 "Toggle the archive tag for the current entry."
9169 (interactive)
9170 (org-agenda-check-no-diary)
9171 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9172 (org-agenda-error)))
9173 (buffer (marker-buffer hdmarker))
9174 (pos (marker-position hdmarker))
9175 (inhibit-read-only t)
9176 newhead)
9177 (org-with-remote-undo buffer
9178 (with-current-buffer buffer
9179 (widen)
9180 (goto-char pos)
9181 (org-show-context 'agenda)
9182 (call-interactively 'org-toggle-archive-tag)
9183 (end-of-line 1)
9184 (setq newhead (org-get-heading)))
9185 (org-agenda-change-all-lines newhead hdmarker)
9186 (beginning-of-line 1))))
9188 (defun org-agenda-do-date-later (arg)
9189 (interactive "P")
9190 (cond
9191 ((or (equal arg '(16))
9192 (memq last-command
9193 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9194 (setq this-command 'org-agenda-date-later-minutes)
9195 (org-agenda-date-later-minutes 1))
9196 ((or (equal arg '(4))
9197 (memq last-command
9198 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9199 (setq this-command 'org-agenda-date-later-hours)
9200 (org-agenda-date-later-hours 1))
9202 (org-agenda-date-later (prefix-numeric-value arg)))))
9204 (defun org-agenda-do-date-earlier (arg)
9205 (interactive "P")
9206 (cond
9207 ((or (equal arg '(16))
9208 (memq last-command
9209 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9210 (setq this-command 'org-agenda-date-earlier-minutes)
9211 (org-agenda-date-earlier-minutes 1))
9212 ((or (equal arg '(4))
9213 (memq last-command
9214 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9215 (setq this-command 'org-agenda-date-earlier-hours)
9216 (org-agenda-date-earlier-hours 1))
9218 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9220 (defun org-agenda-date-later (arg &optional what)
9221 "Change the date of this item to ARG day(s) later."
9222 (interactive "p")
9223 (org-agenda-check-type t 'agenda 'timeline)
9224 (org-agenda-check-no-diary)
9225 (let* ((marker (or (org-get-at-bol 'org-marker)
9226 (org-agenda-error)))
9227 (buffer (marker-buffer marker))
9228 (pos (marker-position marker))
9229 cdate today)
9230 (org-with-remote-undo buffer
9231 (with-current-buffer buffer
9232 (widen)
9233 (goto-char pos)
9234 (if (not (org-at-timestamp-p))
9235 (error "Cannot find time stamp"))
9236 (when (and org-agenda-move-date-from-past-immediately-to-today
9237 (equal arg 1)
9238 (or (not what) (eq what 'day))
9239 (not (save-match-data (org-at-date-range-p))))
9240 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9241 cdate (calendar-absolute-from-gregorian
9242 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9243 today (org-today))
9244 (if (> today cdate)
9245 ;; immediately shift to today
9246 (setq arg (- today cdate))))
9247 (org-timestamp-change arg (or what 'day))
9248 (when (and (org-at-date-range-p)
9249 (re-search-backward org-tr-regexp-both (point-at-bol)))
9250 (let ((end org-last-changed-timestamp))
9251 (org-timestamp-change arg (or what 'day))
9252 (setq org-last-changed-timestamp
9253 (concat org-last-changed-timestamp "--" end)))))
9254 (org-agenda-show-new-time marker org-last-changed-timestamp))
9255 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9257 (defun org-agenda-date-earlier (arg &optional what)
9258 "Change the date of this item to ARG day(s) earlier."
9259 (interactive "p")
9260 (org-agenda-date-later (- arg) what))
9262 (defun org-agenda-date-later-minutes (arg)
9263 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9264 (interactive "p")
9265 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9266 (org-agenda-date-later arg 'minute))
9268 (defun org-agenda-date-earlier-minutes (arg)
9269 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9270 (interactive "p")
9271 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9272 (org-agenda-date-earlier arg 'minute))
9274 (defun org-agenda-date-later-hours (arg)
9275 "Change the time of this item, in hour steps."
9276 (interactive "p")
9277 (org-agenda-date-later arg 'hour))
9279 (defun org-agenda-date-earlier-hours (arg)
9280 "Change the time of this item, in hour steps."
9281 (interactive "p")
9282 (org-agenda-date-earlier arg 'hour))
9284 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9285 "Show new date stamp via text properties."
9286 ;; We use text properties to make this undoable
9287 (let ((inhibit-read-only t))
9288 (setq stamp (concat prefix " => " stamp " "))
9289 (save-excursion
9290 (goto-char (point-max))
9291 (while (not (bobp))
9292 (when (equal marker (org-get-at-bol 'org-marker))
9293 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9294 (org-move-to-column (- (window-width) (length stamp)) t)
9295 (add-text-properties
9296 (1- (point)) (point-at-eol)
9297 (list 'display (org-add-props stamp nil
9298 'face '(secondary-selection default))))
9299 (beginning-of-line 1))
9300 (beginning-of-line 0)))))
9302 (defun org-agenda-date-prompt (arg)
9303 "Change the date of this item. Date is prompted for, with default today.
9304 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9305 be used to request time specification in the time stamp."
9306 (interactive "P")
9307 (org-agenda-check-type t 'agenda 'timeline)
9308 (org-agenda-check-no-diary)
9309 (let* ((marker (or (org-get-at-bol 'org-marker)
9310 (org-agenda-error)))
9311 (buffer (marker-buffer marker))
9312 (pos (marker-position marker)))
9313 (org-with-remote-undo buffer
9314 (with-current-buffer buffer
9315 (widen)
9316 (goto-char pos)
9317 (if (not (org-at-timestamp-p t))
9318 (error "Cannot find time stamp"))
9319 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9320 (org-agenda-show-new-time marker org-last-changed-timestamp))
9321 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9323 (defun org-agenda-schedule (arg &optional time)
9324 "Schedule the item at point.
9325 ARG is passed through to `org-schedule'."
9326 (interactive "P")
9327 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9328 (org-agenda-check-no-diary)
9329 (let* ((marker (or (org-get-at-bol 'org-marker)
9330 (org-agenda-error)))
9331 (type (marker-insertion-type marker))
9332 (buffer (marker-buffer marker))
9333 (pos (marker-position marker))
9335 (set-marker-insertion-type marker t)
9336 (org-with-remote-undo buffer
9337 (with-current-buffer buffer
9338 (widen)
9339 (goto-char pos)
9340 (setq ts (org-schedule arg time)))
9341 (org-agenda-show-new-time marker ts " S"))
9342 (message "%s" ts)))
9344 (defun org-agenda-deadline (arg &optional time)
9345 "Schedule the item at point.
9346 ARG is passed through to `org-deadline'."
9347 (interactive "P")
9348 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9349 (org-agenda-check-no-diary)
9350 (let* ((marker (or (org-get-at-bol 'org-marker)
9351 (org-agenda-error)))
9352 (buffer (marker-buffer marker))
9353 (pos (marker-position marker))
9355 (org-with-remote-undo buffer
9356 (with-current-buffer buffer
9357 (widen)
9358 (goto-char pos)
9359 (setq ts (org-deadline arg time)))
9360 (org-agenda-show-new-time marker ts " D"))
9361 (message "%s" ts)))
9363 (defun org-agenda-clock-in (&optional arg)
9364 "Start the clock on the currently selected item."
9365 (interactive "P")
9366 (org-agenda-check-no-diary)
9367 (if (equal arg '(4))
9368 (org-clock-in arg)
9369 (let* ((marker (or (org-get-at-bol 'org-marker)
9370 (org-agenda-error)))
9371 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9372 (pos (marker-position marker))
9373 (col (current-column))
9374 newhead)
9375 (org-with-remote-undo (marker-buffer marker)
9376 (with-current-buffer (marker-buffer marker)
9377 (widen)
9378 (goto-char pos)
9379 (org-show-context 'agenda)
9380 (org-cycle-hide-drawers 'children)
9381 (org-clock-in arg)
9382 (setq newhead (org-get-heading)))
9383 (org-agenda-change-all-lines newhead hdmarker))
9384 (org-move-to-column col))))
9386 (defun org-agenda-clock-out ()
9387 "Stop the currently running clock."
9388 (interactive)
9389 (unless (marker-buffer org-clock-marker)
9390 (error "No running clock"))
9391 (let ((marker (make-marker)) (col (current-column)) newhead)
9392 (org-with-remote-undo (marker-buffer org-clock-marker)
9393 (with-current-buffer (marker-buffer org-clock-marker)
9394 (org-with-wide-buffer
9395 (goto-char org-clock-marker)
9396 (org-back-to-heading t)
9397 (move-marker marker (point))
9398 (org-clock-out)
9399 (setq newhead (org-get-heading)))))
9400 (org-agenda-change-all-lines newhead marker)
9401 (move-marker marker nil)
9402 (org-move-to-column col)
9403 (org-agenda-unmark-clocking-task)))
9405 (defun org-agenda-clock-cancel (&optional arg)
9406 "Cancel the currently running clock."
9407 (interactive "P")
9408 (unless (marker-buffer org-clock-marker)
9409 (user-error "No running clock"))
9410 (org-with-remote-undo (marker-buffer org-clock-marker)
9411 (org-clock-cancel)))
9413 (defun org-agenda-clock-goto ()
9414 "Jump to the currently clocked in task within the agenda.
9415 If the currently clocked in task is not listed in the agenda
9416 buffer, display it in another window."
9417 (interactive)
9418 (let (pos)
9419 (mapc (lambda (o)
9420 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9421 (setq pos (overlay-start o))))
9422 (overlays-in (point-min) (point-max)))
9423 (cond (pos (goto-char pos))
9424 ;; If the currently clocked entry is not in the agenda
9425 ;; buffer, we visit it in another window:
9426 ((bound-and-true-p org-clock-current-task)
9427 (org-switch-to-buffer-other-window (org-clock-goto)))
9428 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9430 (defun org-agenda-diary-entry-in-org-file ()
9431 "Make a diary entry in the file `org-agenda-diary-file'."
9432 (let (d1 d2 char (text "") dp1 dp2)
9433 (if (equal (buffer-name) "*Calendar*")
9434 (setq d1 (calendar-cursor-to-date t)
9435 d2 (car calendar-mark-ring))
9436 (setq dp1 (get-text-property (point-at-bol) 'day))
9437 (unless dp1 (user-error "No date defined in current line"))
9438 (setq d1 (calendar-gregorian-from-absolute dp1)
9439 d2 (and (ignore-errors (mark))
9440 (save-excursion
9441 (goto-char (mark))
9442 (setq dp2 (get-text-property (point-at-bol) 'day)))
9443 (calendar-gregorian-from-absolute dp2))))
9444 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9445 (setq char (read-char-exclusive))
9446 (cond
9447 ((equal char ?d)
9448 (setq text (read-string "Day entry: "))
9449 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9450 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9451 ((equal char ?a)
9452 (setq d1 (list (car d1) (nth 1 d1)
9453 (read-number (format "Reference year [%d]: " (nth 2 d1))
9454 (nth 2 d1))))
9455 (setq text (read-string "Anniversary (use %d to show years): "))
9456 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9457 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9458 ((equal char ?b)
9459 (setq text (read-string "Block entry: "))
9460 (unless (and d1 d2 (not (equal d1 d2)))
9461 (user-error "No block of days selected"))
9462 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9463 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9464 ((equal char ?j)
9465 (org-switch-to-buffer-other-window
9466 (find-file-noselect org-agenda-diary-file))
9467 (require 'org-datetree)
9468 (org-datetree-find-date-create d1)
9469 (org-reveal t))
9470 (t (user-error "Invalid selection character `%c'" char)))))
9472 (defcustom org-agenda-insert-diary-strategy 'date-tree
9473 "Where in `org-agenda-diary-file' should new entries be added?
9474 Valid values:
9476 date-tree in the date tree, as first child of the date
9477 date-tree-last in the date tree, as last child of the date
9478 top-level as top-level entries at the end of the file."
9479 :group 'org-agenda
9480 :type '(choice
9481 (const :tag "first in a date tree" date-tree)
9482 (const :tag "last in a date tree" date-tree-last)
9483 (const :tag "as top level at end of file" top-level)))
9485 (defcustom org-agenda-insert-diary-extract-time nil
9486 "Non-nil means extract any time specification from the diary entry."
9487 :group 'org-agenda
9488 :version "24.1"
9489 :type 'boolean)
9491 (defcustom org-agenda-bulk-mark-char ">"
9492 "A single-character string to be used as the bulk mark."
9493 :group 'org-agenda
9494 :version "24.1"
9495 :type 'string)
9497 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9498 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9499 If TEXT is not empty, it will become the headline of the new entry, and
9500 the resulting entry will not be shown. When TEXT is empty, switch to
9501 `org-agenda-diary-file' and let the user finish the entry there."
9502 (let ((cw (current-window-configuration)))
9503 (org-switch-to-buffer-other-window
9504 (find-file-noselect org-agenda-diary-file))
9505 (widen)
9506 (goto-char (point-min))
9507 (cond
9508 ((eq type 'anniversary)
9509 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9510 (progn
9511 (or (org-at-heading-p t)
9512 (progn
9513 (outline-next-heading)
9514 (insert "* Anniversaries\n\n")
9515 (beginning-of-line -1)))))
9516 (outline-next-heading)
9517 (org-back-over-empty-lines)
9518 (backward-char 1)
9519 (insert "\n")
9520 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9521 (nth 2 d1) (car d1) (nth 1 d1) text)))
9522 ((eq type 'day)
9523 (let ((org-prefix-has-time t)
9524 (org-agenda-time-leading-zero t)
9525 fmt time time2)
9526 (if org-agenda-insert-diary-extract-time
9527 ;; Use org-agenda-format-item to parse text for a time-range and
9528 ;; remove it. FIXME: This is a hack, we should refactor
9529 ;; that function to make time extraction available separately
9530 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9531 time (get-text-property 0 'time fmt)
9532 time2 (if (> (length time) 0)
9533 ;; split-string removes trailing ...... if
9534 ;; no end time given. First space
9535 ;; separates time from date.
9536 (concat " " (car (split-string time "\\.")))
9537 nil)
9538 text (get-text-property 0 'txt fmt)))
9539 (if (eq org-agenda-insert-diary-strategy 'top-level)
9540 (org-agenda-insert-diary-as-top-level text)
9541 (require 'org-datetree)
9542 (org-datetree-find-date-create d1)
9543 (org-agenda-insert-diary-make-new-entry text))
9544 (org-insert-time-stamp (org-time-from-absolute
9545 (calendar-absolute-from-gregorian d1))
9546 nil nil nil nil time2))
9547 (end-of-line 0))
9548 ((eq type 'block)
9549 (if (> (calendar-absolute-from-gregorian d1)
9550 (calendar-absolute-from-gregorian d2))
9551 (setq d1 (prog1 d2 (setq d2 d1))))
9552 (if (eq org-agenda-insert-diary-strategy 'top-level)
9553 (org-agenda-insert-diary-as-top-level text)
9554 (require 'org-datetree)
9555 (org-datetree-find-date-create d1)
9556 (org-agenda-insert-diary-make-new-entry text))
9557 (org-insert-time-stamp (org-time-from-absolute
9558 (calendar-absolute-from-gregorian d1)))
9559 (insert "--")
9560 (org-insert-time-stamp (org-time-from-absolute
9561 (calendar-absolute-from-gregorian d2)))
9562 (end-of-line 0)))
9563 (if (string-match "\\S-" text)
9564 (progn
9565 (set-window-configuration cw)
9566 (message "%s entry added to %s"
9567 (capitalize (symbol-name type))
9568 (abbreviate-file-name org-agenda-diary-file)))
9569 (org-reveal t)
9570 (message "Please finish entry here"))))
9572 (defun org-agenda-insert-diary-as-top-level (text)
9573 "Make new entry as a top-level entry at the end of the file.
9574 Add TEXT as headline, and position the cursor in the second line so that
9575 a timestamp can be added there."
9576 (widen)
9577 (goto-char (point-max))
9578 (unless (bolp) (insert "\n"))
9579 (org-insert-heading nil t t)
9580 (insert text)
9581 (org-end-of-meta-data)
9582 (unless (bolp) (insert "\n"))
9583 (when org-adapt-indentation (indent-to-column 2)))
9585 (defun org-agenda-insert-diary-make-new-entry (text)
9586 "Make a new entry with TEXT as a child of the current subtree.
9587 Position the point in the heading's first body line so that
9588 a timestamp can be added there."
9589 (cond
9590 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9591 (end-of-line)
9592 (org-insert-heading '(4) t)
9593 (org-do-demote))
9595 (outline-next-heading)
9596 (org-back-over-empty-lines)
9597 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9598 (org-insert-heading nil t)
9599 (org-do-demote)))
9600 (let ((col (current-column)))
9601 (insert text)
9602 (org-end-of-meta-data)
9603 ;; Ensure point is left on a blank line, at proper indentation.
9604 (unless (bolp) (insert "\n"))
9605 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9606 (when org-adapt-indentation (indent-to-column col)))
9607 (org-show-set-visibility 'lineage))
9609 (defun org-agenda-diary-entry ()
9610 "Make a diary entry, like the `i' command from the calendar.
9611 All the standard commands work: block, weekly etc.
9612 When `org-agenda-diary-file' points to a file,
9613 `org-agenda-diary-entry-in-org-file' is called instead to create
9614 entries in that Org file."
9615 (interactive)
9616 (if (not (eq org-agenda-diary-file 'diary-file))
9617 (org-agenda-diary-entry-in-org-file)
9618 (require 'diary-lib)
9619 (let* ((char (progn
9620 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9621 (read-char-exclusive)))
9622 (cmd (cdr (assoc char
9623 '((?d . diary-insert-entry)
9624 (?w . diary-insert-weekly-entry)
9625 (?m . diary-insert-monthly-entry)
9626 (?y . diary-insert-yearly-entry)
9627 (?a . diary-insert-anniversary-entry)
9628 (?b . diary-insert-block-entry)
9629 (?c . diary-insert-cyclic-entry)))))
9630 (oldf (symbol-function 'calendar-cursor-to-date))
9631 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9632 (point (point))
9633 (mark (or (mark t) (point))))
9634 (unless cmd
9635 (user-error "No command associated with <%c>" char))
9636 (unless (and (get-text-property point 'day)
9637 (or (not (equal ?b char))
9638 (get-text-property mark 'day)))
9639 (user-error "Don't know which date to use for diary entry"))
9640 ;; We implement this by hacking the `calendar-cursor-to-date' function
9641 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9642 (let ((calendar-mark-ring
9643 (list (calendar-gregorian-from-absolute
9644 (or (get-text-property mark 'day)
9645 (get-text-property point 'day))))))
9646 (unwind-protect
9647 (progn
9648 (fset 'calendar-cursor-to-date
9649 (lambda (&optional error dummy)
9650 (calendar-gregorian-from-absolute
9651 (get-text-property point 'day))))
9652 (call-interactively cmd))
9653 (fset 'calendar-cursor-to-date oldf))))))
9655 (defun org-agenda-execute-calendar-command (cmd)
9656 "Execute a calendar command from the agenda with date from cursor."
9657 (org-agenda-check-type t 'agenda 'timeline)
9658 (require 'diary-lib)
9659 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9660 (user-error "Don't know which date to use for the calendar command"))
9661 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9662 (point (point))
9663 (date (calendar-gregorian-from-absolute
9664 (get-text-property point 'day)))
9665 ;; the following 2 vars are needed in the calendar
9666 (displayed-month (car date))
9667 (displayed-year (nth 2 date)))
9668 (unwind-protect
9669 (progn
9670 (fset 'calendar-cursor-to-date
9671 (lambda (&optional error dummy)
9672 (calendar-gregorian-from-absolute
9673 (get-text-property point 'day))))
9674 (call-interactively cmd))
9675 (fset 'calendar-cursor-to-date oldf))))
9677 (defun org-agenda-phases-of-moon ()
9678 "Display the phases of the moon for the 3 months around the cursor date."
9679 (interactive)
9680 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9682 (defun org-agenda-holidays ()
9683 "Display the holidays for the 3 months around the cursor date."
9684 (interactive)
9685 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9687 (defvar calendar-longitude) ; defined in calendar.el
9688 (defvar calendar-latitude) ; defined in calendar.el
9689 (defvar calendar-location-name) ; defined in calendar.el
9691 (defun org-agenda-sunrise-sunset (arg)
9692 "Display sunrise and sunset for the cursor date.
9693 Latitude and longitude can be specified with the variables
9694 `calendar-latitude' and `calendar-longitude'. When called with prefix
9695 argument, latitude and longitude will be prompted for."
9696 (interactive "P")
9697 (require 'solar)
9698 (let ((calendar-longitude (if arg nil calendar-longitude))
9699 (calendar-latitude (if arg nil calendar-latitude))
9700 (calendar-location-name
9701 (if arg "the given coordinates" calendar-location-name)))
9702 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9704 (defun org-agenda-goto-calendar ()
9705 "Open the Emacs calendar with the date at the cursor."
9706 (interactive)
9707 (org-agenda-check-type t 'agenda 'timeline)
9708 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9709 (user-error "Don't know which date to open in calendar")))
9710 (date (calendar-gregorian-from-absolute day))
9711 (calendar-move-hook nil)
9712 (calendar-view-holidays-initially-flag nil)
9713 (calendar-view-diary-initially-flag nil))
9714 (calendar)
9715 (calendar-goto-date date)))
9717 ;;;###autoload
9718 (defun org-calendar-goto-agenda ()
9719 "Compute the Org agenda for the calendar date displayed at the cursor.
9720 This is a command that has to be installed in `calendar-mode-map'."
9721 (interactive)
9722 ;; Temporarily disable sticky agenda since user clearly wants to
9723 ;; refresh view anyway.
9724 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9725 (org-agenda-sticky nil))
9726 (org-agenda-list nil (calendar-absolute-from-gregorian
9727 (calendar-cursor-to-date))
9728 nil)))
9730 (defun org-agenda-convert-date ()
9731 (interactive)
9732 (org-agenda-check-type t 'agenda 'timeline)
9733 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9734 date s)
9735 (unless day
9736 (user-error "Don't know which date to convert"))
9737 (setq date (calendar-gregorian-from-absolute day))
9738 (setq s (concat
9739 "Gregorian: " (calendar-date-string date) "\n"
9740 "ISO: " (calendar-iso-date-string date) "\n"
9741 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9742 "Julian: " (calendar-julian-date-string date) "\n"
9743 "Astron. JD: " (calendar-astro-date-string date)
9744 " (Julian date number at noon UTC)\n"
9745 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9746 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9747 "French: " (calendar-french-date-string date) "\n"
9748 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9749 "Mayan: " (calendar-mayan-date-string date) "\n"
9750 "Coptic: " (calendar-coptic-date-string date) "\n"
9751 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9752 "Persian: " (calendar-persian-date-string date) "\n"
9753 "Chinese: " (calendar-chinese-date-string date) "\n"))
9754 (with-output-to-temp-buffer "*Dates*"
9755 (princ s))
9756 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9758 ;;; Bulk commands
9760 (defun org-agenda-bulk-marked-p ()
9761 "Non-nil when current entry is marked for bulk action."
9762 (eq (get-char-property (point-at-bol) 'type)
9763 'org-marked-entry-overlay))
9765 (defun org-agenda-bulk-mark (&optional arg)
9766 "Mark the entry at point for future bulk action."
9767 (interactive "p")
9768 (dotimes (i (or arg 1))
9769 (unless (org-get-at-bol 'org-agenda-diary-link)
9770 (let* ((m (org-get-at-bol 'org-hd-marker))
9772 (unless (org-agenda-bulk-marked-p)
9773 (unless m (user-error "Nothing to mark at point"))
9774 (push m org-agenda-bulk-marked-entries)
9775 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9776 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9777 (org-get-todo-face "TODO")
9778 'evaporate)
9779 (overlay-put ov 'type 'org-marked-entry-overlay))
9780 (end-of-line 1)
9781 (or (ignore-errors
9782 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9783 (beginning-of-line 2))
9784 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9785 (beginning-of-line 2))
9786 (message "%d entries marked for bulk action"
9787 (length org-agenda-bulk-marked-entries))))))
9789 (defun org-agenda-bulk-mark-all ()
9790 "Mark all entries for future agenda bulk action."
9791 (interactive)
9792 (org-agenda-bulk-mark-regexp "."))
9794 (defun org-agenda-bulk-mark-regexp (regexp)
9795 "Mark entries matching REGEXP for future agenda bulk action."
9796 (interactive "sMark entries matching regexp: ")
9797 (let ((entries-marked 0) txt-at-point)
9798 (save-excursion
9799 (goto-char (point-min))
9800 (goto-char (next-single-property-change (point) 'org-hd-marker))
9801 (while (and (re-search-forward regexp nil t)
9802 (setq txt-at-point (get-text-property (point) 'txt)))
9803 (if (get-char-property (point) 'invisible)
9804 (beginning-of-line 2)
9805 (when (string-match regexp txt-at-point)
9806 (setq entries-marked (1+ entries-marked))
9807 (call-interactively 'org-agenda-bulk-mark)))))
9809 (if (not entries-marked)
9810 (message "No entry matching this regexp."))))
9812 (defun org-agenda-bulk-unmark (&optional arg)
9813 "Unmark the entry at point for future bulk action."
9814 (interactive "P")
9815 (if arg
9816 (org-agenda-bulk-unmark-all)
9817 (cond ((org-agenda-bulk-marked-p)
9818 (org-agenda-bulk-remove-overlays
9819 (point-at-bol) (+ 2 (point-at-bol)))
9820 (setq org-agenda-bulk-marked-entries
9821 (delete (org-get-at-bol 'org-hd-marker)
9822 org-agenda-bulk-marked-entries))
9823 (end-of-line 1)
9824 (or (ignore-errors
9825 (goto-char (next-single-property-change (point) 'txt)))
9826 (beginning-of-line 2))
9827 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9828 (beginning-of-line 2))
9829 (message "%d entries left marked for bulk action"
9830 (length org-agenda-bulk-marked-entries)))
9831 (t (message "No entry to unmark here")))))
9833 (defun org-agenda-bulk-toggle-all ()
9834 "Toggle all marks for bulk action."
9835 (interactive)
9836 (save-excursion
9837 (goto-char (point-min))
9838 (while (ignore-errors
9839 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9840 (org-agenda-bulk-toggle))))
9842 (defun org-agenda-bulk-toggle ()
9843 "Toggle the mark at point for bulk action."
9844 (interactive)
9845 (if (org-agenda-bulk-marked-p)
9846 (org-agenda-bulk-unmark)
9847 (org-agenda-bulk-mark)))
9849 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9850 "Remove the mark overlays between BEG and END in the agenda buffer.
9851 BEG and END default to the buffer limits.
9853 This only removes the overlays, it does not remove the markers
9854 from the list in `org-agenda-bulk-marked-entries'."
9855 (interactive)
9856 (mapc (lambda (ov)
9857 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9858 (delete-overlay ov)))
9859 (overlays-in (or beg (point-min)) (or end (point-max)))))
9861 (defun org-agenda-bulk-unmark-all ()
9862 "Remove all marks in the agenda buffer.
9863 This will remove the markers and the overlays."
9864 (interactive)
9865 (if (null org-agenda-bulk-marked-entries)
9866 (message "No entry to unmark")
9867 (setq org-agenda-bulk-marked-entries nil)
9868 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9870 (defcustom org-agenda-persistent-marks nil
9871 "Non-nil means marked items will stay marked after a bulk action.
9872 You can toggle this interactively by typing `p' when prompted for a
9873 bulk action."
9874 :group 'org-agenda
9875 :version "24.1"
9876 :type 'boolean)
9878 (defun org-agenda-bulk-action (&optional arg)
9879 "Execute an remote-editing action on all marked entries.
9880 The prefix arg is passed through to the command if possible."
9881 (interactive "P")
9882 ;; Make sure we have markers, and only valid ones
9883 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9884 (mapc
9885 (lambda (m)
9886 (unless (and (markerp m)
9887 (marker-buffer m)
9888 (buffer-live-p (marker-buffer m))
9889 (marker-position m))
9890 (user-error "Marker %s for bulk command is invalid" m)))
9891 org-agenda-bulk-marked-entries)
9893 ;; Prompt for the bulk command
9894 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9895 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9896 "[S]catter [f]unction "
9897 (when org-agenda-bulk-custom-functions
9898 (concat " Custom: ["
9899 (mapconcat (lambda(f) (char-to-string (car f)))
9900 org-agenda-bulk-custom-functions "")
9901 "]"))))
9902 (catch 'exit
9903 (let* ((action (read-char-exclusive))
9904 (org-log-refile (if org-log-refile 'time nil))
9905 (entries (reverse org-agenda-bulk-marked-entries))
9906 (org-overriding-default-time
9907 (if (get-text-property (point) 'org-agenda-date-header)
9908 (org-get-cursor-date)))
9909 redo-at-end
9910 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9911 (cond
9912 ((equal action ?p)
9913 (let ((org-agenda-persistent-marks
9914 (not org-agenda-persistent-marks)))
9915 (org-agenda-bulk-action)
9916 (throw 'exit nil)))
9918 ((equal action ?$)
9919 (setq cmd '(org-agenda-archive)))
9921 ((equal action ?A)
9922 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9924 ((member action '(?r ?w))
9925 (setq rfloc (org-refile-get-location
9926 "Refile to"
9927 (marker-buffer (car entries))
9928 org-refile-allow-creating-parent-nodes))
9929 (if (nth 3 rfloc)
9930 (setcar (nthcdr 3 rfloc)
9931 (move-marker (make-marker) (nth 3 rfloc)
9932 (or (get-file-buffer (nth 1 rfloc))
9933 (find-buffer-visiting (nth 1 rfloc))
9934 (error "This should not happen")))))
9936 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9937 redo-at-end t))
9939 ((equal action ?t)
9940 (setq state (completing-read
9941 "Todo state: "
9942 (with-current-buffer (marker-buffer (car entries))
9943 (mapcar #'list org-todo-keywords-1))))
9944 (setq cmd `(let ((org-inhibit-blocking t)
9945 (org-inhibit-logging 'note))
9946 (org-agenda-todo ,state))))
9948 ((memq action '(?- ?+))
9949 (setq tag (completing-read
9950 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9951 (with-current-buffer (marker-buffer (car entries))
9952 (delq nil
9953 (mapcar (lambda (x) (and (stringp (car x)) x))
9954 org-current-tag-alist)))))
9955 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9957 ((memq action '(?s ?d))
9958 (let* ((time
9959 (unless arg
9960 (org-read-date
9961 nil nil nil
9962 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9963 org-overriding-default-time)))
9964 (c1 (if (eq action ?s) 'org-agenda-schedule
9965 'org-agenda-deadline)))
9966 ;; Make sure to not prompt for a note when bulk
9967 ;; rescheduling as Org cannot cope with simultaneous
9968 ;; notes. Besides, it could be annoying depending on the
9969 ;; number of items re-scheduled.
9970 (setq cmd `(eval '(let ((org-log-reschedule
9971 (and org-log-reschedule 'time))
9972 (org-log-redeadline
9973 (and org-log-redeadline 'time)))
9974 (,c1 arg ,time))))))
9976 ((equal action ?S)
9977 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9978 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9979 (let ((days (read-number
9980 (format "Scatter tasks across how many %sdays: "
9981 (if arg "week" "")) 7)))
9982 (setq cmd
9983 `(let ((distance (1+ (random ,days))))
9984 (if arg
9985 (let ((dist distance)
9986 (day-of-week
9987 (calendar-day-of-week
9988 (calendar-gregorian-from-absolute (org-today)))))
9989 (dotimes (i (1+ dist))
9990 (while (member day-of-week org-agenda-weekend-days)
9991 (cl-incf distance)
9992 (cl-incf day-of-week)
9993 (when (= day-of-week 7)
9994 (setq day-of-week 0)))
9995 (cl-incf day-of-week)
9996 (when (= day-of-week 7)
9997 (setq day-of-week 0)))))
9998 ;; silently fail when try to replan a sexp entry
9999 (condition-case nil
10000 (let* ((date (calendar-gregorian-from-absolute
10001 (+ (org-today) distance)))
10002 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
10003 (nth 2 date))))
10004 (org-agenda-schedule nil time))
10005 (error nil)))))))
10007 ((assoc action org-agenda-bulk-custom-functions)
10008 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
10009 redo-at-end t))
10011 ((equal action ?f)
10012 (setq cmd (list (intern
10013 (completing-read "Function: "
10014 obarray 'fboundp t nil nil)))))
10016 (t (user-error "Invalid bulk action")))
10018 ;; Sort the markers, to make sure that parents are handled before children
10019 (setq entries (sort entries
10020 (lambda (a b)
10021 (cond
10022 ((equal (marker-buffer a) (marker-buffer b))
10023 (< (marker-position a) (marker-position b)))
10025 (string< (buffer-name (marker-buffer a))
10026 (buffer-name (marker-buffer b))))))))
10028 ;; Now loop over all markers and apply cmd
10029 (while (setq e (pop entries))
10030 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
10031 (if (not pos)
10032 (progn (message "Skipping removed entry at %s" e)
10033 (setq cntskip (1+ cntskip)))
10034 (goto-char pos)
10035 (let (org-loop-over-headlines-in-active-region)
10036 (eval cmd))
10037 ;; `post-command-hook' is not run yet. We make sure any
10038 ;; pending log note is processed.
10039 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
10040 (memq 'org-add-log-note post-command-hook))
10041 (org-add-log-note))
10042 (setq cnt (1+ cnt))))
10043 (when redo-at-end (org-agenda-redo))
10044 (unless org-agenda-persistent-marks
10045 (org-agenda-bulk-unmark-all))
10046 (message "Acted on %d entries%s%s"
10048 (if (= cntskip 0)
10050 (format ", skipped %d (disappeared before their turn)"
10051 cntskip))
10052 (if (not org-agenda-persistent-marks)
10053 "" " (kept marked)"))))))
10055 (defun org-agenda-capture (&optional with-time)
10056 "Call `org-capture' with the date at point.
10057 With a `C-1' prefix, use the HH:MM value at point (if any) or the
10058 current HH:MM time."
10059 (interactive "P")
10060 (if (not (eq major-mode 'org-agenda-mode))
10061 (user-error "You cannot do this outside of agenda buffers")
10062 (let ((org-overriding-default-time
10063 (org-get-cursor-date (equal with-time 1))))
10064 (call-interactively 'org-capture))))
10066 ;;; Dragging agenda lines forward/backward
10068 (defun org-agenda-reapply-filters ()
10069 "Re-apply all agenda filters."
10070 (mapcar
10071 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10072 `((,org-agenda-tag-filter tag)
10073 (,org-agenda-category-filter category)
10074 (,org-agenda-regexp-filter regexp)
10075 (,org-agenda-effort-filter effort)
10076 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10077 (,(get 'org-agenda-category-filter :preset-filter) category)
10078 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10079 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10081 (defun org-agenda-drag-line-forward (arg &optional backward)
10082 "Drag an agenda line forward by ARG lines.
10083 When the optional argument `backward' is non-nil, move backward."
10084 (interactive "p")
10085 (let ((inhibit-read-only t) lst line)
10086 (if (or (not (get-text-property (point) 'txt))
10087 (save-excursion
10088 (dotimes (n arg)
10089 (move-beginning-of-line (if backward 0 2))
10090 (push (not (get-text-property (point) 'txt)) lst))
10091 (delq nil lst)))
10092 (message "Cannot move line forward")
10093 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10094 (move-beginning-of-line 1)
10095 (setq line (buffer-substring (point) end))
10096 (delete-region (point) end)
10097 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10098 (insert line)
10099 (org-agenda-reapply-filters)
10100 (org-agenda-mark-clocking-task)
10101 (move-beginning-of-line 0)))))
10103 (defun org-agenda-drag-line-backward (arg)
10104 "Drag an agenda line backward by ARG lines."
10105 (interactive "p")
10106 (org-agenda-drag-line-forward arg t))
10108 ;;; Flagging notes
10110 (defun org-agenda-show-the-flagging-note ()
10111 "Display the flagging note in the other window.
10112 When called a second time in direct sequence, offer to remove the FLAGGING
10113 tag and (if present) the flagging note."
10114 (interactive)
10115 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10116 (win (selected-window))
10117 note heading newhead)
10118 (unless hdmarker
10119 (user-error "No linked entry at point"))
10120 (if (and (eq this-command last-command)
10121 (y-or-n-p "Unflag and remove any flagging note? "))
10122 (progn
10123 (org-agenda-remove-flag hdmarker)
10124 (let ((win (get-buffer-window "*Flagging Note*")))
10125 (and win (delete-window win)))
10126 (message "Entry unflagged"))
10127 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10128 (unless note
10129 (user-error "No flagging note"))
10130 (org-kill-new note)
10131 (org-switch-to-buffer-other-window "*Flagging Note*")
10132 (erase-buffer)
10133 (insert note)
10134 (goto-char (point-min))
10135 (while (re-search-forward "\\\\n" nil t)
10136 (replace-match "\n" t t))
10137 (goto-char (point-min))
10138 (select-window win)
10139 (message "%s" (substitute-command-keys "Flagging note pushed to \
10140 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10141 tag and note")))))
10143 (defun org-agenda-remove-flag (marker)
10144 "Remove the FLAGGED tag and any flagging note in the entry."
10145 (let (newhead)
10146 (org-with-point-at marker
10147 (org-toggle-tag "FLAGGED" 'off)
10148 (org-entry-delete nil "THEFLAGGINGNOTE")
10149 (setq newhead (org-get-heading)))
10150 (org-agenda-change-all-lines newhead marker)
10151 (message "Entry unflagged")))
10153 (defun org-agenda-get-any-marker (&optional pos)
10154 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10155 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10157 ;;; Appointment reminders
10159 (defvar appt-time-msg-list) ; defined in appt.el
10161 ;;;###autoload
10162 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10163 "Activate appointments found in `org-agenda-files'.
10165 With a `\\[universal-argument]' prefix, refresh the list of \
10166 appointments.
10168 If FILTER is t, interactively prompt the user for a regular
10169 expression, and filter out entries that don't match it.
10171 If FILTER is a string, use this string as a regular expression
10172 for filtering entries out.
10174 If FILTER is a function, filter out entries against which
10175 calling the function returns nil. This function takes one
10176 argument: an entry from `org-agenda-get-day-entries'.
10178 FILTER can also be an alist with the car of each cell being
10179 either `headline' or `category'. For example:
10181 \\='((headline \"IMPORTANT\")
10182 (category \"Work\"))
10184 will only add headlines containing IMPORTANT or headlines
10185 belonging to the \"Work\" category.
10187 ARGS are symbols indicating what kind of entries to consider.
10188 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10189 \(i.e., deadlines and scheduled items with a hh:mm specification)
10190 and :timestamp entries. See the docstring of `org-diary' for
10191 details and examples.
10193 If an entry has a APPT_WARNTIME property, its value will be used
10194 to override `appt-message-warning-time'."
10195 (interactive "P")
10196 (if refresh (setq appt-time-msg-list nil))
10197 (if (eq filter t)
10198 (setq filter (read-from-minibuffer "Regexp filter: ")))
10199 (let* ((cnt 0) ; count added events
10200 (scope (or args '(:deadline* :scheduled* :timestamp)))
10201 (org-agenda-new-buffers nil)
10202 (org-deadline-warning-days 0)
10203 ;; Do not use `org-today' here because appt only takes
10204 ;; time and without date as argument, so it may pass wrong
10205 ;; information otherwise
10206 (today (org-date-to-gregorian
10207 (time-to-days (current-time))))
10208 (org-agenda-restrict nil)
10209 (files (org-agenda-files 'unrestricted)) entries file
10210 (org-agenda-buffer nil))
10211 ;; Get all entries which may contain an appt
10212 (org-agenda-prepare-buffers files)
10213 (while (setq file (pop files))
10214 (setq entries
10215 (delq nil
10216 (append entries
10217 (apply 'org-agenda-get-day-entries
10218 file today scope)))))
10219 ;; Map thru entries and find if we should filter them out
10220 (mapc
10221 (lambda (x)
10222 (let* ((evt (org-trim
10223 (replace-regexp-in-string
10224 org-bracket-link-regexp "\\3"
10225 (or (get-text-property 1 'txt x) ""))))
10226 (cat (get-text-property (1- (length x)) 'org-category x))
10227 (tod (get-text-property 1 'time-of-day x))
10228 (ok (or (null filter)
10229 (and (stringp filter) (string-match filter evt))
10230 (and (functionp filter) (funcall filter x))
10231 (and (listp filter)
10232 (let ((cat-filter (cadr (assq 'category filter)))
10233 (evt-filter (cadr (assq 'headline filter))))
10234 (or (and (stringp cat-filter)
10235 (string-match cat-filter cat))
10236 (and (stringp evt-filter)
10237 (string-match evt-filter evt)))))))
10238 (wrn (get-text-property 1 'warntime x)))
10239 ;; FIXME: Shall we remove text-properties for the appt text?
10240 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10241 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10242 (setq tod (concat "00" (number-to-string tod)))
10243 (setq tod (when (string-match
10244 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10245 (concat (match-string 1 tod) ":"
10246 (match-string 2 tod))))
10247 (when (if (version< emacs-version "23.3")
10248 (appt-add tod evt)
10249 (appt-add tod evt wrn))
10250 (setq cnt (1+ cnt))))))
10251 entries)
10252 (org-release-buffers org-agenda-new-buffers)
10253 (if (eq cnt 0)
10254 (message "No event to add")
10255 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10257 (defun org-agenda-today-p (date)
10258 "Non nil when DATE means today.
10259 DATE is either a list of the form (month day year) or a number of
10260 days as returned by `calendar-absolute-from-gregorian' or
10261 `org-today'. This function considers `org-extend-today-until'
10262 when defining today."
10263 (eq (org-today)
10264 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10266 (defun org-agenda-todo-yesterday (&optional arg)
10267 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10268 (interactive "P")
10269 (let* ((org-use-effective-time t)
10270 (hour (nth 2 (decode-time (org-current-time))))
10271 (org-extend-today-until (1+ hour)))
10272 (org-agenda-todo arg)))
10274 (provide 'org-agenda)
10276 ;;; org-agenda.el ends here