org-agenda: Remove duplicate files when building a search view
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob76bfa90d18e3d952169779707a59bab771269b36
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'.
282 :deadline* Same as above, but only include the deadline if it has an
283 hour specification as [h]h:mm.
285 :scheduled List all items which are scheduled for the given date.
286 The diary for *today* also contains items which were
287 scheduled earlier and are not yet marked DONE.
289 :scheduled* Same as above, but only include the scheduled item if it
290 has an hour specification as [h]h:mm.
292 By default, all four non-starred types are turned on.
294 When :scheduled* or :deadline* are included, :schedule or :deadline
295 will be ignored.
297 Never set this variable globally using `setq', because then it
298 will apply to all future agenda commands. Instead, bind it with
299 `let' to scope it dynamically into the agenda-constructing
300 command. A good way to set it is through options in
301 `org-agenda-custom-commands'. For a more flexible (though
302 somewhat less efficient) way of determining what is included in
303 the daily/weekly agenda, see `org-agenda-skip-function'.")
305 (defconst org-agenda-custom-commands-local-options
306 `(repeat :tag "Local settings for this command. Remember to quote values"
307 (choice :tag "Setting"
308 (list :tag "Heading for this block"
309 (const org-agenda-overriding-header)
310 (string :tag "Headline"))
311 (list :tag "Files to be searched"
312 (const org-agenda-files)
313 (list
314 (const :format "" quote)
315 (repeat (file))))
316 (list :tag "Sorting strategy"
317 (const org-agenda-sorting-strategy)
318 (list
319 (const :format "" quote)
320 (repeat
321 ,org-sorting-choice)))
322 (list :tag "Prefix format"
323 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
324 (string))
325 (list :tag "Number of days in agenda"
326 (const org-agenda-span)
327 (list
328 (const :format "" quote)
329 (choice (const :tag "Day" day)
330 (const :tag "Week" week)
331 (const :tag "Fortnight" fortnight)
332 (const :tag "Month" month)
333 (const :tag "Year" year)
334 (integer :tag "Custom"))))
335 (list :tag "Fixed starting date"
336 (const org-agenda-start-day)
337 (string :value "2007-11-01"))
338 (list :tag "Start on day of week"
339 (const org-agenda-start-on-weekday)
340 (choice :value 1
341 (const :tag "Today" nil)
342 (integer :tag "Weekday No.")))
343 (list :tag "Include data from diary"
344 (const org-agenda-include-diary)
345 (boolean))
346 (list :tag "Deadline Warning days"
347 (const org-deadline-warning-days)
348 (integer :value 1))
349 (list :tag "Category filter preset"
350 (const org-agenda-category-filter-preset)
351 (list
352 (const :format "" quote)
353 (repeat
354 (string :tag "+category or -category"))))
355 (list :tag "Tags filter preset"
356 (const org-agenda-tag-filter-preset)
357 (list
358 (const :format "" quote)
359 (repeat
360 (string :tag "+tag or -tag"))))
361 (list :tag "Effort filter preset"
362 (const org-agenda-effort-filter-preset)
363 (list
364 (const :format "" quote)
365 (repeat
366 (string :tag "+=10 or -=10 or +<10 or ->10"))))
367 (list :tag "Regexp filter preset"
368 (const org-agenda-regexp-filter-preset)
369 (list
370 (const :format "" quote)
371 (repeat
372 (string :tag "+regexp or -regexp"))))
373 (list :tag "Set daily/weekly entry types"
374 (const org-agenda-entry-types)
375 (list
376 (const :format "" quote)
377 (set :greedy t :value ,org-agenda-entry-types
378 (const :deadline)
379 (const :scheduled)
380 (const :deadline*)
381 (const :scheduled*)
382 (const :timestamp)
383 (const :sexp))))
384 (list :tag "Standard skipping condition"
385 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
386 (const org-agenda-skip-function)
387 (list
388 (const :format "" quote)
389 (list
390 (choice
391 :tag "Skipping range"
392 (const :tag "Skip entry" org-agenda-skip-entry-if)
393 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
394 (repeat :inline t :tag "Conditions for skipping"
395 (choice
396 :tag "Condition type"
397 (list :tag "Regexp matches" :inline t
398 (const :format "" 'regexp)
399 (regexp))
400 (list :tag "Regexp does not match" :inline t
401 (const :format "" 'notregexp)
402 (regexp))
403 (list :tag "TODO state is" :inline t
404 (const 'todo)
405 (choice
406 (const :tag "Any not-done state" 'todo)
407 (const :tag "Any done state" 'done)
408 (const :tag "Any state" 'any)
409 (list :tag "Keyword list"
410 (const :format "" quote)
411 (repeat (string :tag "Keyword")))))
412 (list :tag "TODO state is not" :inline t
413 (const 'nottodo)
414 (choice
415 (const :tag "Any not-done state" 'todo)
416 (const :tag "Any done state" 'done)
417 (const :tag "Any state" 'any)
418 (list :tag "Keyword list"
419 (const :format "" quote)
420 (repeat (string :tag "Keyword")))))
421 (const :tag "scheduled" 'scheduled)
422 (const :tag "not scheduled" 'notscheduled)
423 (const :tag "deadline" 'deadline)
424 (const :tag "no deadline" 'notdeadline)
425 (const :tag "timestamp" 'timestamp)
426 (const :tag "no timestamp" 'nottimestamp))))))
427 (list :tag "Non-standard skipping condition"
428 :value (org-agenda-skip-function)
429 (const org-agenda-skip-function)
430 (sexp :tag "Function or form (quoted!)"))
431 (list :tag "Any variable"
432 (variable :tag "Variable")
433 (sexp :tag "Value (sexp)"))))
434 "Selection of examples for agenda command settings.
435 This will be spliced into the custom type of
436 `org-agenda-custom-commands'.")
439 (defcustom org-agenda-custom-commands
440 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
441 "Custom commands for the agenda.
442 \\<org-mode-map>
443 These commands will be offered on the splash screen displayed by the
444 agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
446 (key desc type match settings files)
448 key The key (one or more characters as a string) to be associated
449 with the command.
450 desc A description of the command, when omitted or nil, a default
451 description is built using MATCH.
452 type The command type, any of the following symbols:
453 agenda The daily/weekly agenda.
454 todo Entries with a specific TODO keyword, in all agenda files.
455 search Entries containing search words entry or headline.
456 tags Tags/Property/TODO match in all agenda files.
457 tags-todo Tags/P/T match in all agenda files, TODO entries only.
458 todo-tree Sparse tree of specific TODO keyword in *current* file.
459 tags-tree Sparse tree with all tags matches in *current* file.
460 occur-tree Occur sparse tree for *current* file.
461 ... A user-defined function.
462 match What to search for:
463 - a single keyword for TODO keyword searches
464 - a tags match expression for tags searches
465 - a word search expression for text searches.
466 - a regular expression for occur searches
467 For all other commands, this should be the empty string.
468 settings A list of option settings, similar to that in a let form, so like
469 this: ((opt1 val1) (opt2 val2) ...). The values will be
470 evaluated at the moment of execution, so quote them when needed.
471 files A list of files to write the produced agenda buffer to with
472 the command `org-store-agenda-views'.
473 If a file name ends in \".html\", an HTML version of the buffer
474 is written out. If it ends in \".ps\", a postscript version is
475 produced. Otherwise, only the plain text is written to the file.
477 You can also define a set of commands, to create a composite agenda buffer.
478 In this case, an entry looks like this:
480 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
482 where
484 desc A description string to be displayed in the dispatcher menu.
485 cmd An agenda command, similar to the above. However, tree commands
486 are not allowed, but instead you can get agenda and global todo list.
487 So valid commands for a set are:
488 (agenda \"\" settings)
489 (alltodo \"\" settings)
490 (stuck \"\" settings)
491 (todo \"match\" settings files)
492 (search \"match\" settings files)
493 (tags \"match\" settings files)
494 (tags-todo \"match\" settings files)
496 Each command can carry a list of options, and another set of options can be
497 given for the whole set of commands. Individual command options take
498 precedence over the general options.
500 When using several characters as key to a command, the first characters
501 are prefix commands. For the dispatcher to display useful information, you
502 should provide a description for the prefix, like
504 (setq org-agenda-custom-commands
505 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
506 (\"hl\" tags \"+HOME+Lisa\")
507 (\"hp\" tags \"+HOME+Peter\")
508 (\"hk\" tags \"+HOME+Kim\")))"
509 :group 'org-agenda-custom-commands
510 :type `(repeat
511 (choice :value ("x" "Describe command here" tags "" nil)
512 (list :tag "Single command"
513 (string :tag "Access Key(s) ")
514 (option (string :tag "Description"))
515 (choice
516 (const :tag "Agenda" agenda)
517 (const :tag "TODO list" alltodo)
518 (const :tag "Search words" search)
519 (const :tag "Stuck projects" stuck)
520 (const :tag "Tags/Property match (all agenda files)" tags)
521 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
522 (const :tag "TODO keyword search (all agenda files)" todo)
523 (const :tag "Tags sparse tree (current buffer)" tags-tree)
524 (const :tag "TODO keyword tree (current buffer)" todo-tree)
525 (const :tag "Occur tree (current buffer)" occur-tree)
526 (sexp :tag "Other, user-defined function"))
527 (string :tag "Match (only for some commands)")
528 ,org-agenda-custom-commands-local-options
529 (option (repeat :tag "Export" (file :tag "Export to"))))
530 (list :tag "Command series, all agenda files"
531 (string :tag "Access Key(s)")
532 (string :tag "Description ")
533 (repeat :tag "Component"
534 (choice
535 (list :tag "Agenda"
536 (const :format "" agenda)
537 (const :tag "" :format "" "")
538 ,org-agenda-custom-commands-local-options)
539 (list :tag "TODO list (all keywords)"
540 (const :format "" alltodo)
541 (const :tag "" :format "" "")
542 ,org-agenda-custom-commands-local-options)
543 (list :tag "Search words"
544 (const :format "" search)
545 (string :tag "Match")
546 ,org-agenda-custom-commands-local-options)
547 (list :tag "Stuck projects"
548 (const :format "" stuck)
549 (const :tag "" :format "" "")
550 ,org-agenda-custom-commands-local-options)
551 (list :tag "Tags search"
552 (const :format "" tags)
553 (string :tag "Match")
554 ,org-agenda-custom-commands-local-options)
555 (list :tag "Tags search, TODO entries only"
556 (const :format "" tags-todo)
557 (string :tag "Match")
558 ,org-agenda-custom-commands-local-options)
559 (list :tag "TODO keyword search"
560 (const :format "" todo)
561 (string :tag "Match")
562 ,org-agenda-custom-commands-local-options)
563 (list :tag "Other, user-defined function"
564 (symbol :tag "function")
565 (string :tag "Match")
566 ,org-agenda-custom-commands-local-options)))
568 (repeat :tag "Settings for entire command set"
569 (list (variable :tag "Any variable")
570 (sexp :tag "Value")))
571 (option (repeat :tag "Export" (file :tag "Export to"))))
572 (cons :tag "Prefix key documentation"
573 (string :tag "Access Key(s)")
574 (string :tag "Description ")))))
576 (defcustom org-agenda-query-register ?o
577 "The register holding the current query string.
578 The purpose of this is that if you construct a query string interactively,
579 you can then use it to define a custom command."
580 :group 'org-agenda-custom-commands
581 :type 'character)
583 (defcustom org-stuck-projects
584 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
585 "How to identify stuck projects.
586 This is a list of four items:
587 1. A tags/todo/property matcher string that is used to identify a project.
588 See the manual for a description of tag and property searches.
589 The entire tree below a headline matched by this is considered one project.
590 2. A list of TODO keywords identifying non-stuck projects.
591 If the project subtree contains any headline with one of these todo
592 keywords, the project is considered to be not stuck. If you specify
593 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
594 3. A list of tags identifying non-stuck projects.
595 If the project subtree contains any headline with one of these tags,
596 the project is considered to be not stuck. If you specify \"*\" as
597 a tag, any tag will mark the project unstuck. Note that this is about
598 the explicit presence of a tag somewhere in the subtree, inherited
599 tags do not count here. If inherited tags make a project not stuck,
600 use \"-TAG\" in the tags part of the matcher under (1.) above.
601 4. An arbitrary regular expression matching non-stuck projects.
603 If the project turns out to be not stuck, search continues also in the
604 subtree to see if any of the subtasks have project status.
606 See also the variable `org-tags-match-list-sublevels' which applies
607 to projects matched by this search as well.
609 After defining this variable, you may use `\\[org-agenda-list-stuck-projects]'
610 \(bound to `C-c a #') to produce the list."
611 :group 'org-agenda-custom-commands
612 :type '(list
613 (string :tag "Tags/TODO match to identify a project")
614 (repeat :tag "Projects are *not* stuck if they have an entry with \
615 TODO keyword any of" (string))
616 (repeat :tag "Projects are *not* stuck if they have an entry with \
617 TAG being any of" (string))
618 (regexp :tag "Projects are *not* stuck if this regexp matches inside \
619 the subtree")))
621 (defgroup org-agenda-skip nil
622 "Options concerning skipping parts of agenda files."
623 :tag "Org Agenda Skip"
624 :group 'org-agenda)
626 (defcustom org-agenda-skip-function-global nil
627 "Function to be called at each match during agenda construction.
628 If this function returns nil, the current match should not be skipped.
629 If the function decided to skip an agenda match, is must return the
630 buffer position from which the search should be continued.
631 This may also be a Lisp form, which will be evaluated.
633 This variable will be applied to every agenda match, including
634 tags/property searches and TODO lists. So try to make the test function
635 do its checking as efficiently as possible. To implement a skipping
636 condition just for specific agenda commands, use the variable
637 `org-agenda-skip-function' which can be set in the options section
638 of custom agenda commands."
639 :group 'org-agenda-skip
640 :type 'sexp)
642 (defgroup org-agenda-daily/weekly nil
643 "Options concerning the daily/weekly agenda."
644 :tag "Org Agenda Daily/Weekly"
645 :group 'org-agenda)
646 (defgroup org-agenda-todo-list nil
647 "Options concerning the global todo list agenda view."
648 :tag "Org Agenda Todo List"
649 :group 'org-agenda)
650 (defgroup org-agenda-match-view nil
651 "Options concerning the general tags/property/todo match agenda view."
652 :tag "Org Agenda Match View"
653 :group 'org-agenda)
654 (defgroup org-agenda-search-view nil
655 "Options concerning the search agenda view."
656 :tag "Org Agenda Search View"
657 :group 'org-agenda)
659 (defvar org-agenda-archives-mode nil
660 "Non-nil means the agenda will include archived items.
661 If this is the symbol `trees', trees in the selected agenda scope
662 that are marked with the ARCHIVE tag will be included anyway. When this is
663 t, also all archive files associated with the current selection of agenda
664 files will be included.")
666 (defcustom org-agenda-restriction-lock-highlight-subtree t
667 "Non-nil means highlight the whole subtree when restriction is active.
668 Otherwise only highlight the headline. Highlighting the whole subtree is
669 useful to ensure no edits happen beyond the restricted region."
670 :group 'org-agenda
671 :type 'boolean)
673 (defcustom org-agenda-skip-comment-trees t
674 "Non-nil means skip trees that start with the COMMENT keyword.
675 When nil, these trees are also scanned by agenda commands."
676 :group 'org-agenda-skip
677 :type 'boolean)
679 (defcustom org-agenda-todo-list-sublevels t
680 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
681 When nil, the sublevels of a TODO entry are not checked, resulting in
682 potentially much shorter TODO lists."
683 :group 'org-agenda-skip
684 :group 'org-agenda-todo-list
685 :type 'boolean)
687 (defcustom org-agenda-todo-ignore-with-date nil
688 "Non-nil means don't show entries with a date in the global todo list.
689 You can use this if you prefer to mark mere appointments with a TODO keyword,
690 but don't want them to show up in the TODO list.
691 When this is set, it also covers deadlines and scheduled items, the settings
692 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
693 will be ignored.
694 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
695 :group 'org-agenda-skip
696 :group 'org-agenda-todo-list
697 :type 'boolean)
699 (defcustom org-agenda-todo-ignore-timestamp nil
700 "Non-nil means don't show entries with a timestamp.
701 This applies when creating the global todo list.
702 Valid values are:
704 past Don't show entries for today or in the past.
706 future Don't show entries with a timestamp in the future.
707 The idea behind this is that if it has a future
708 timestamp, you don't want to think about it until the
709 date.
711 all Don't show any entries with a timestamp in the global todo list.
712 The idea behind this is that by setting a timestamp, you
713 have already \"taken care\" of this item.
715 This variable can also have an integer as a value. If positive (N),
716 todos with a timestamp N or more days in the future will be ignored. If
717 negative (-N), todos with a timestamp N or more days in the past will be
718 ignored. If 0, todos with a timestamp either today or in the future will
719 be ignored. For example, a value of -1 will exclude todos with a
720 timestamp in the past (yesterday or earlier), while a value of 7 will
721 exclude todos with a timestamp a week or more in the future.
723 See also `org-agenda-todo-ignore-with-date'.
724 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
725 to make his option also apply to the tags-todo list."
726 :group 'org-agenda-skip
727 :group 'org-agenda-todo-list
728 :version "24.1"
729 :type '(choice
730 (const :tag "Ignore future timestamp todos" future)
731 (const :tag "Ignore past or present timestamp todos" past)
732 (const :tag "Ignore all timestamp todos" all)
733 (const :tag "Show timestamp todos" nil)
734 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
736 (defcustom org-agenda-todo-ignore-scheduled nil
737 "Non-nil means, ignore some scheduled TODO items when making TODO list.
738 This applies when creating the global todo list.
739 Valid values are:
741 past Don't show entries scheduled today or in the past.
743 future Don't show entries scheduled in the future.
744 The idea behind this is that by scheduling it, you don't want to
745 think about it until the scheduled date.
747 all Don't show any scheduled entries in the global todo list.
748 The idea behind this is that by scheduling it, you have already
749 \"taken care\" of this item.
751 t Same as `all', for backward compatibility.
753 This variable can also have an integer as a value. See
754 `org-agenda-todo-ignore-timestamp' for more details.
756 See also `org-agenda-todo-ignore-with-date'.
757 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
758 to make his option also apply to the tags-todo list."
759 :group 'org-agenda-skip
760 :group 'org-agenda-todo-list
761 :type '(choice
762 (const :tag "Ignore future-scheduled todos" future)
763 (const :tag "Ignore past- or present-scheduled todos" past)
764 (const :tag "Ignore all scheduled todos" all)
765 (const :tag "Ignore all scheduled todos (compatibility)" t)
766 (const :tag "Show scheduled todos" nil)
767 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
769 (defcustom org-agenda-todo-ignore-deadlines nil
770 "Non-nil means ignore some deadline TODO items when making TODO list.
772 There are different motivations for using different values, please think
773 carefully when configuring this variable.
775 This applies when creating the global TODO list.
777 Valid values are:
779 near Don't show near deadline entries. A deadline is near when it is
780 closer than `org-deadline-warning-days' days. The idea behind this
781 is that such items will appear in the agenda anyway.
783 far Don't show TODO entries where a deadline has been defined, but
784 is not going to happen anytime soon. This is useful if you want to use
785 the TODO list to figure out what to do now.
787 past Don't show entries with a deadline timestamp for today or in the past.
789 future Don't show entries with a deadline timestamp in the future, not even
790 when they become `near' ones. Use it with caution.
792 all Ignore all TODO entries that do have a deadline.
794 t Same as `near', for backward compatibility.
796 This variable can also have an integer as a value. See
797 `org-agenda-todo-ignore-timestamp' for more details.
799 See also `org-agenda-todo-ignore-with-date'.
800 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
801 to make his option also apply to the tags-todo list."
802 :group 'org-agenda-skip
803 :group 'org-agenda-todo-list
804 :type '(choice
805 (const :tag "Ignore near deadlines" near)
806 (const :tag "Ignore near deadlines (compatibility)" t)
807 (const :tag "Ignore far deadlines" far)
808 (const :tag "Ignore all TODOs with a deadlines" all)
809 (const :tag "Show all TODOs, even if they have a deadline" nil)
810 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
812 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
813 "Time unit to use when possibly ignoring an agenda item.
815 See the docstring of various `org-agenda-todo-ignore-*' options.
816 The default is to compare time stamps using days. An item is thus
817 considered to be in the future if it is at least one day after today.
818 Non-nil means to compare time stamps using seconds. An item is then
819 considered future if it has a time value later than current time."
820 :group 'org-agenda-skip
821 :group 'org-agenda-todo-list
822 :version "24.4"
823 :package-version '(Org . "8.0")
824 :type '(choice
825 (const :tag "Compare time with days" nil)
826 (const :tag "Compare time with seconds" t)))
828 (defcustom org-agenda-tags-todo-honor-ignore-options nil
829 "Non-nil means honor todo-list ignores options also in tags-todo search.
830 The variables
831 `org-agenda-todo-ignore-with-date',
832 `org-agenda-todo-ignore-timestamp',
833 `org-agenda-todo-ignore-scheduled',
834 `org-agenda-todo-ignore-deadlines'
835 make the global TODO list skip entries that have time stamps of certain
836 kinds. If this option is set, the same options will also apply for the
837 tags-todo search, which is the general tags/property matcher
838 restricted to unfinished TODO entries only."
839 :group 'org-agenda-skip
840 :group 'org-agenda-todo-list
841 :group 'org-agenda-match-view
842 :type 'boolean)
844 (defcustom org-agenda-skip-scheduled-if-done nil
845 "Non-nil means don't show scheduled items in agenda when they are done.
846 This is relevant for the daily/weekly agenda, not for the TODO list. It
847 applies only to the actual date of the scheduling. Warnings about an item
848 with a past scheduling dates are always turned off when the item is DONE."
849 :group 'org-agenda-skip
850 :group 'org-agenda-daily/weekly
851 :type 'boolean)
853 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
854 "Non-nil means skip scheduling line if same entry shows because of deadline.
856 In the agenda of today, an entry can show up multiple times
857 because it is both scheduled and has a nearby deadline, and maybe
858 a plain time stamp as well.
860 When this variable is nil, the entry will be shown several times.
862 When set to t, then only the deadline is shown and the fact that
863 the entry is scheduled today or was scheduled previously is not
864 shown.
866 When set to the symbol `not-today', skip scheduled previously,
867 but not scheduled today.
869 When set to the symbol `repeated-after-deadline', skip scheduled
870 items if they are repeated beyond the current deadline."
871 :group 'org-agenda-skip
872 :group 'org-agenda-daily/weekly
873 :type '(choice
874 (const :tag "Never" nil)
875 (const :tag "Always" t)
876 (const :tag "Not when scheduled today" not-today)
877 (const :tag "When repeated past deadline" repeated-after-deadline)))
879 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
880 "Non-nil means skip timestamp line if same entry shows because of deadline.
881 In the agenda of today, an entry can show up multiple times
882 because it has both a plain timestamp and has a nearby deadline.
883 When this variable is t, then only the deadline is shown and the
884 fact that the entry has a timestamp for or including today is not
885 shown. When this variable is nil, the entry will be shown
886 several times."
887 :group 'org-agenda-skip
888 :group 'org-agenda-daily/weekly
889 :version "24.1"
890 :type '(choice
891 (const :tag "Never" nil)
892 (const :tag "Always" t)))
894 (defcustom org-agenda-skip-deadline-if-done nil
895 "Non-nil means don't show deadlines when the corresponding item is done.
896 When nil, the deadline is still shown and should give you a happy feeling.
897 This is relevant for the daily/weekly agenda. It applies only to the
898 actual date of the deadline. Warnings about approaching and past-due
899 deadlines are always turned off when the item is DONE."
900 :group 'org-agenda-skip
901 :group 'org-agenda-daily/weekly
902 :type 'boolean)
904 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
905 "Non-nil means skip deadline prewarning when entry is also scheduled.
906 This will apply on all days where a prewarning for the deadline would
907 be shown, but not at the day when the entry is actually due. On that day,
908 the deadline will be shown anyway.
909 This variable may be set to nil, t, the symbol `pre-scheduled',
910 or a number which will then give the number of days before the actual
911 deadline when the prewarnings should resume. The symbol `pre-scheduled'
912 eliminates the deadline prewarning only prior to the scheduled date.
913 This can be used in a workflow where the first showing of the deadline will
914 trigger you to schedule it, and then you don't want to be reminded of it
915 because you will take care of it on the day when scheduled."
916 :group 'org-agenda-skip
917 :group 'org-agenda-daily/weekly
918 :version "24.1"
919 :type '(choice
920 (const :tag "Always show prewarning" nil)
921 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
922 (const :tag "Remove prewarning if entry is scheduled" t)
923 (integer :tag "Restart prewarning N days before deadline")))
925 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
926 "Non-nil means skip scheduled delay when entry also has a deadline.
927 This variable may be set to nil, t, the symbol `post-deadline',
928 or a number which will then give the number of days after the actual
929 scheduled date when the delay should expire. The symbol `post-deadline'
930 eliminates the schedule delay when the date is posterior to the deadline."
931 :group 'org-agenda-skip
932 :group 'org-agenda-daily/weekly
933 :version "24.4"
934 :package-version '(Org . "8.0")
935 :type '(choice
936 (const :tag "Always honor delay" nil)
937 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
938 (const :tag "Ignore delay if entry has a deadline" t)
939 (integer :tag "Honor delay up until N days after the scheduled date")))
941 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
942 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
943 When non-nil, after the search for timestamps has matched once in an
944 entry, the rest of the entry will not be searched."
945 :group 'org-agenda-skip
946 :type 'boolean)
948 (defcustom org-agenda-skip-timestamp-if-done nil
949 "Non-nil means don't select item by timestamp or -range if it is DONE."
950 :group 'org-agenda-skip
951 :group 'org-agenda-daily/weekly
952 :type 'boolean)
954 (defcustom org-agenda-dim-blocked-tasks t
955 "Non-nil means dim blocked tasks in the agenda display.
956 This causes some overhead during agenda construction, but if you
957 have turned on `org-enforce-todo-dependencies',
958 `org-enforce-todo-checkbox-dependencies', or any other blocking
959 mechanism, this will create useful feedback in the agenda.
961 Instead of t, this variable can also have the value `invisible'.
962 Then blocked tasks will be invisible and only become visible when
963 they become unblocked. An exemption to this behavior is when a task is
964 blocked because of unchecked checkboxes below it. Since checkboxes do
965 not show up in the agenda views, making this task invisible you remove any
966 trace from agenda views that there is something to do. Therefore, a task
967 that is blocked because of checkboxes will never be made invisible, it
968 will only be dimmed."
969 :group 'org-agenda-daily/weekly
970 :group 'org-agenda-todo-list
971 :version "24.3"
972 :type '(choice
973 (const :tag "Do not dim" nil)
974 (const :tag "Dim to a gray face" t)
975 (const :tag "Make invisible" invisible)))
977 (defgroup org-agenda-startup nil
978 "Options concerning initial settings in the Agenda in Org Mode."
979 :tag "Org Agenda Startup"
980 :group 'org-agenda)
982 (defcustom org-agenda-menu-show-matcher t
983 "Non-nil means show the match string in the agenda dispatcher menu.
984 When nil, the matcher string is not shown, but is put into the help-echo
985 property so than moving the mouse over the command shows it.
986 Setting it to nil is good if matcher strings are very long and/or if
987 you want to use two-columns display (see `org-agenda-menu-two-columns')."
988 :group 'org-agenda
989 :version "24.1"
990 :type 'boolean)
992 (defcustom org-agenda-menu-two-columns nil
993 "Non-nil means, use two columns to show custom commands in the dispatcher.
994 If you use this, you probably want to set `org-agenda-menu-show-matcher'
995 to nil."
996 :group 'org-agenda
997 :version "24.1"
998 :type 'boolean)
1000 (defcustom org-agenda-finalize-hook nil
1001 "Hook run just before displaying an agenda buffer.
1002 The buffer is still writable when the hook is called.
1004 You can modify some of the buffer substrings but you should be
1005 extra careful not to modify the text properties of the agenda
1006 headlines as the agenda display heavily relies on them."
1007 :group 'org-agenda-startup
1008 :type 'hook)
1010 (defcustom org-agenda-mouse-1-follows-link nil
1011 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1012 A longer mouse click will still set point. Needs to be set
1013 before org.el is loaded."
1014 :group 'org-agenda-startup
1015 :type 'boolean)
1017 (defcustom org-agenda-start-with-follow-mode nil
1018 "The initial value of follow mode in a newly created agenda window."
1019 :group 'org-agenda-startup
1020 :type 'boolean)
1022 (defcustom org-agenda-follow-indirect nil
1023 "Non-nil means `org-agenda-follow-mode' displays only the
1024 current item's tree, in an indirect buffer."
1025 :group 'org-agenda
1026 :version "24.1"
1027 :type 'boolean)
1029 (defcustom org-agenda-show-outline-path t
1030 "Non-nil means show outline path in echo area after line motion."
1031 :group 'org-agenda-startup
1032 :type 'boolean)
1034 (defcustom org-agenda-start-with-entry-text-mode nil
1035 "The initial value of entry-text-mode in a newly created agenda window."
1036 :group 'org-agenda-startup
1037 :type 'boolean)
1039 (defcustom org-agenda-entry-text-maxlines 5
1040 "Number of text lines to be added when `E' is pressed in the agenda.
1042 Note that this variable only used during agenda display. To add entry text
1043 when exporting the agenda, configure the variable
1044 `org-agenda-add-entry-text-maxlines'."
1045 :group 'org-agenda
1046 :type 'integer)
1048 (defcustom org-agenda-entry-text-exclude-regexps nil
1049 "List of regular expressions to clean up entry text.
1050 The complete matches of all regular expressions in this list will be
1051 removed from entry text before it is shown in the agenda."
1052 :group 'org-agenda
1053 :type '(repeat (regexp)))
1055 (defcustom org-agenda-entry-text-leaders " > "
1056 "Text prepended to the entry text in agenda buffers."
1057 :version "24.4"
1058 :package-version '(Org . "8.0")
1059 :group 'org-agenda
1060 :type 'string)
1062 (defvar org-agenda-entry-text-cleanup-hook nil
1063 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1064 This cleanup is done in a temporary buffer, so the function may inspect and
1065 change the entire buffer.
1066 Some default stuff like drawers and scheduling/deadline dates will already
1067 have been removed when this is called, as will any matches for regular
1068 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1070 (defvar org-agenda-include-inactive-timestamps nil
1071 "Non-nil means include inactive time stamps in agenda.
1072 Dynamically scoped.")
1074 (defgroup org-agenda-windows nil
1075 "Options concerning the windows used by the Agenda in Org Mode."
1076 :tag "Org Agenda Windows"
1077 :group 'org-agenda)
1079 (defcustom org-agenda-window-setup 'reorganize-frame
1080 "How the agenda buffer should be displayed.
1081 Possible values for this option are:
1083 current-window Show agenda in the current window, keeping all other windows.
1084 other-window Use `switch-to-buffer-other-window' to display agenda.
1085 only-window Show agenda, deleting all other windows.
1086 reorganize-frame Show only two windows on the current frame, the current
1087 window and the agenda.
1088 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1089 Also, when exiting the agenda, kill that frame.
1090 See also the variable `org-agenda-restore-windows-after-quit'."
1091 :group 'org-agenda-windows
1092 :type '(choice
1093 (const current-window)
1094 (const other-frame)
1095 (const other-window)
1096 (const only-window)
1097 (const reorganize-frame)))
1099 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1100 "The min and max height of the agenda window as a fraction of frame height.
1101 The value of the variable is a cons cell with two numbers between 0 and 1.
1102 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1103 :group 'org-agenda-windows
1104 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1106 (defcustom org-agenda-restore-windows-after-quit nil
1107 "Non-nil means restore window configuration upon exiting agenda.
1108 Before the window configuration is changed for displaying the agenda,
1109 the current status is recorded. When the agenda is exited with
1110 `q' or `x' and this option is set, the old state is restored. If
1111 `org-agenda-window-setup' is `other-frame', the value of this
1112 option will be ignored."
1113 :group 'org-agenda-windows
1114 :type 'boolean)
1116 (defcustom org-agenda-span 'week
1117 "Number of days to include in overview display.
1118 Can be day, week, month, year, or any number of days.
1119 Custom commands can set this variable in the options section."
1120 :group 'org-agenda-daily/weekly
1121 :type '(choice (const :tag "Day" day)
1122 (const :tag "Week" week)
1123 (const :tag "Fortnight" fortnight)
1124 (const :tag "Month" month)
1125 (const :tag "Year" year)
1126 (integer :tag "Custom")))
1128 (defcustom org-agenda-start-on-weekday 1
1129 "Non-nil means start the overview always on the specified weekday.
1130 0 denotes Sunday, 1 denotes Monday, etc.
1131 When nil, always start on the current day.
1132 Custom commands can set this variable in the options section."
1133 :group 'org-agenda-daily/weekly
1134 :type '(choice (const :tag "Today" nil)
1135 (integer :tag "Weekday No.")))
1137 (defcustom org-agenda-show-all-dates t
1138 "Non-nil means `org-agenda' shows every day in the selected range.
1139 When nil, only the days which actually have entries are shown."
1140 :group 'org-agenda-daily/weekly
1141 :type 'boolean)
1143 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1144 "Format string for displaying dates in the agenda.
1145 Used by the daily/weekly agenda. This should be a format string
1146 understood by `format-time-string', or a function returning the
1147 formatted date as a string. The function must take a single
1148 argument, a calendar-style date list like (month day year)."
1149 :group 'org-agenda-daily/weekly
1150 :type '(choice
1151 (string :tag "Format string")
1152 (function :tag "Function")))
1154 (defun org-agenda-format-date-aligned (date)
1155 "Format a DATE string for display in the daily/weekly agenda.
1156 This function makes sure that dates are aligned for easy reading."
1157 (require 'cal-iso)
1158 (let* ((dayname (calendar-day-name date))
1159 (day (cadr date))
1160 (day-of-week (calendar-day-of-week date))
1161 (month (car date))
1162 (monthname (calendar-month-name month))
1163 (year (nth 2 date))
1164 (iso-week (org-days-to-iso-week
1165 (calendar-absolute-from-gregorian date)))
1166 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1167 (1- year))
1168 ((and (= month 12) (<= iso-week 1))
1169 (1+ year))
1170 (t year)))
1171 (weekstring (if (= day-of-week 1)
1172 (format " W%02d" iso-week)
1173 "")))
1174 (format "%-10s %2d %s %4d%s"
1175 dayname day monthname year weekstring)))
1177 (defcustom org-agenda-time-leading-zero nil
1178 "Non-nil means use leading zero for military times in agenda.
1179 For example, 9:30am would become 09:30 rather than 9:30."
1180 :group 'org-agenda-daily/weekly
1181 :version "24.1"
1182 :type 'boolean)
1184 (defcustom org-agenda-timegrid-use-ampm nil
1185 "When set, show AM/PM style timestamps on the timegrid."
1186 :group 'org-agenda
1187 :version "24.1"
1188 :type 'boolean)
1190 (defun org-agenda-time-of-day-to-ampm (time)
1191 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1192 (let* ((hour-number (string-to-number (substring time 0 -3)))
1193 (minute (substring time -2))
1194 (ampm "am"))
1195 (cond
1196 ((equal hour-number 12)
1197 (setq ampm "pm"))
1198 ((> hour-number 12)
1199 (setq ampm "pm")
1200 (setq hour-number (- hour-number 12))))
1201 (concat
1202 (if org-agenda-time-leading-zero
1203 (format "%02d" hour-number)
1204 (format "%02s" (number-to-string hour-number)))
1205 ":" minute ampm)))
1207 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1208 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1209 (if org-agenda-timegrid-use-ampm
1210 (org-agenda-time-of-day-to-ampm time)
1211 time))
1213 (defcustom org-agenda-weekend-days '(6 0)
1214 "Which days are weekend?
1215 These days get the special face `org-agenda-date-weekend' in the agenda."
1216 :group 'org-agenda-daily/weekly
1217 :type '(set :greedy t
1218 (const :tag "Monday" 1)
1219 (const :tag "Tuesday" 2)
1220 (const :tag "Wednesday" 3)
1221 (const :tag "Thursday" 4)
1222 (const :tag "Friday" 5)
1223 (const :tag "Saturday" 6)
1224 (const :tag "Sunday" 0)))
1226 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1227 "Non-nil means jump to today when moving a past date forward in time.
1228 When using S-right in the agenda to move a a date forward, and the date
1229 stamp currently points to the past, the first key press will move it
1230 to today. WHen nil, just move one day forward even if the date stays
1231 in the past."
1232 :group 'org-agenda-daily/weekly
1233 :version "24.1"
1234 :type 'boolean)
1236 (defcustom org-agenda-include-diary nil
1237 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1238 Custom commands can set this variable in the options section."
1239 :group 'org-agenda-daily/weekly
1240 :type 'boolean)
1242 (defcustom org-agenda-include-deadlines t
1243 "If non-nil, include entries within their deadline warning period.
1244 Custom commands can set this variable in the options section."
1245 :group 'org-agenda-daily/weekly
1246 :version "24.1"
1247 :type 'boolean)
1249 (defcustom org-agenda-show-future-repeats t
1250 "Non-nil shows repeated entries in the future part of the agenda.
1251 When set to the symbol `next' only the first future repeat is shown."
1252 :group 'org-agenda-daily/weekly
1253 :type '(choice
1254 (const :tag "Show all repeated entries" t)
1255 (const :tag "Show next repeated entry" next)
1256 (const :tag "Do not show repeated entries" nil))
1257 :version "26.1"
1258 :package-version '(Org . "9.1")
1259 :safe #'symbolp)
1261 (defcustom org-agenda-prefer-last-repeat nil
1262 "Non-nil sets date for repeated entries to their last repeat.
1264 When nil, display SCHEDULED and DEADLINE dates at their base
1265 date, and in today's agenda, as a reminder. Display plain
1266 time-stamps, on the other hand, at every repeat date in the past
1267 in addition to the base date.
1269 When non-nil, show a repeated entry at its latest repeat date,
1270 possibly being today even if it wasn't marked as done. This
1271 setting is useful if you do not always mark repeated entries as
1272 done and, yet, consider that reaching repeat date starts the task
1273 anew.
1275 When set to a list of strings, prefer last repeats only for
1276 entries with these TODO keywords."
1277 :group 'org-agenda-daily/weekly
1278 :type '(choice
1279 (const :tag "Prefer last repeat" t)
1280 (const :tag "Prefer base date" nil)
1281 (repeat :tag "Prefer last repeat for entries with these TODO keywords"
1282 (string :tag "TODO keyword")))
1283 :version "26.1"
1284 :package-version '(Org . "9.1")
1285 :safe (lambda (x) (or (booleanp x) (consp x))))
1287 (defcustom org-scheduled-past-days 10000
1288 "Number of days to continue listing scheduled items not marked DONE.
1289 When an item is scheduled on a date, it shows up in the agenda on
1290 this day and will be listed until it is marked done or for the
1291 number of days given here."
1292 :group 'org-agenda-daily/weekly
1293 :type 'integer
1294 :safe 'integerp)
1296 (defcustom org-deadline-past-days 10000
1297 "Number of days to warn about missed deadlines.
1298 When an item has deadline on a date, it shows up in the agenda on
1299 this day and will appear as a reminder until it is marked DONE or
1300 for the number of days given here."
1301 :group 'org-agenda-daily/weekly
1302 :type 'integer
1303 :version "26.1"
1304 :package-version '(Org . "9.1")
1305 :safe 'integerp)
1307 (defcustom org-agenda-log-mode-items '(closed clock)
1308 "List of items that should be shown in agenda log mode.
1309 \\<org-agenda-mode-map>\
1310 This list may contain the following symbols:
1312 closed Show entries that have been closed on that day.
1313 clock Show entries that have received clocked time on that day.
1314 state Show all logged state changes.
1315 Note that instead of changing this variable, you can also press \
1316 `\\[universal-argument] \\[org-agenda-log-mode]' in
1317 the agenda to display all available LOG items temporarily."
1318 :group 'org-agenda-daily/weekly
1319 :type '(set :greedy t (const closed) (const clock) (const state)))
1321 (defcustom org-agenda-clock-consistency-checks
1322 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1323 :gap-ok-around ("4:00")
1324 :default-face ((:background "DarkRed") (:foreground "white"))
1325 :overlap-face nil :gap-face nil :no-end-time-face nil
1326 :long-face nil :short-face nil)
1327 "This is a property list, with the following keys:
1329 :max-duration Mark clocking chunks that are longer than this time.
1330 This is a time string like \"HH:MM\", or the number
1331 of minutes as an integer.
1333 :min-duration Mark clocking chunks that are shorter that this.
1334 This is a time string like \"HH:MM\", or the number
1335 of minutes as an integer.
1337 :max-gap Mark gaps between clocking chunks that are longer than
1338 this duration. A number of minutes, or a string
1339 like \"HH:MM\".
1341 :gap-ok-around List of times during the day which are usually not working
1342 times. When a gap is detected, but the gap contains any
1343 of these times, the gap is *not* reported. For example,
1344 if this is (\"4:00\" \"13:00\") then gaps that contain
1345 4:00 in the morning (i.e. the night) and 13:00
1346 (i.e. a typical lunch time) do not cause a warning.
1347 You should have at least one time during the night in this
1348 list, or otherwise the first task each morning will trigger
1349 a warning because it follows a long gap.
1351 Furthermore, the following properties can be used to define faces for
1352 issue display.
1354 :default-face the default face, if the specific face is undefined
1355 :overlap-face face for overlapping clocks
1356 :gap-face face for gaps between clocks
1357 :no-end-time-face face for incomplete clocks
1358 :long-face face for clock intervals that are too long
1359 :short-face face for clock intervals that are too short"
1360 :group 'org-agenda-daily/weekly
1361 :group 'org-clock
1362 :version "24.1"
1363 :type 'plist)
1365 (defcustom org-agenda-log-mode-add-notes t
1366 "Non-nil means add first line of notes to log entries in agenda views.
1367 If a log item like a state change or a clock entry is associated with
1368 notes, the first line of these notes will be added to the entry in the
1369 agenda display."
1370 :group 'org-agenda-daily/weekly
1371 :type 'boolean)
1373 (defcustom org-agenda-start-with-log-mode nil
1374 "The initial value of log-mode in a newly created agenda window.
1375 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1376 explanations on the possible values."
1377 :group 'org-agenda-startup
1378 :group 'org-agenda-daily/weekly
1379 :type '(choice (const :tag "Don't show log items" nil)
1380 (const :tag "Show only log items" only)
1381 (const :tag "Show all possible log items" clockcheck)
1382 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1383 (choice (const :tag "Show closed log items" closed)
1384 (const :tag "Show clocked log items" clock)
1385 (const :tag "Show all logged state changes" state)))))
1387 (defcustom org-agenda-start-with-clockreport-mode nil
1388 "The initial value of clockreport-mode in a newly created agenda window."
1389 :group 'org-agenda-startup
1390 :group 'org-agenda-daily/weekly
1391 :type 'boolean)
1393 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1394 "Property list with parameters for the clocktable in clockreport mode.
1395 This is the display mode that shows a clock table in the daily/weekly
1396 agenda, the properties for this dynamic block can be set here.
1397 The usual clocktable parameters are allowed here, but you cannot set
1398 the properties :name, :tstart, :tend, :block, and :scope - these will
1399 be overwritten to make sure the content accurately reflects the
1400 current display in the agenda."
1401 :group 'org-agenda-daily/weekly
1402 :type 'plist)
1404 (defcustom org-agenda-search-view-always-boolean nil
1405 "Non-nil means the search string is interpreted as individual parts.
1407 The search string for search view can either be interpreted as a phrase,
1408 or as a list of snippets that define a boolean search for a number of
1409 strings.
1411 When this is non-nil, the string will be split on whitespace, and each
1412 snippet will be searched individually, and all must match in order to
1413 select an entry. A snippet is then a single string of non-white
1414 characters, or a string in double quotes, or a regexp in {} braces.
1415 If a snippet is preceded by \"-\", the snippet must *not* match.
1416 \"+\" is syntactic sugar for positive selection. Each snippet may
1417 be found as a full word or a partial word, but see the variable
1418 `org-agenda-search-view-force-full-words'.
1420 When this is nil, search will look for the entire search phrase as one,
1421 with each space character matching any amount of whitespace, including
1422 line breaks.
1424 Even when this is nil, you can still switch to Boolean search dynamically
1425 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1426 is a regexp marked with braces like \"{abc}\", this will also switch to
1427 boolean search."
1428 :group 'org-agenda-search-view
1429 :version "24.1"
1430 :type 'boolean)
1432 (defvaralias 'org-agenda-search-view-search-words-only
1433 'org-agenda-search-view-always-boolean)
1435 (defcustom org-agenda-search-view-force-full-words nil
1436 "Non-nil means, search words must be matches as complete words.
1437 When nil, they may also match part of a word."
1438 :group 'org-agenda-search-view
1439 :version "24.1"
1440 :type 'boolean)
1442 (defcustom org-agenda-search-view-max-outline-level 0
1443 "Maximum outline level to display in search view.
1444 E.g. when this is set to 1, the search view will only
1445 show headlines of level 1. When set to 0, the default
1446 value, don't limit agenda view by outline level."
1447 :group 'org-agenda-search-view
1448 :version "24.4"
1449 :package-version '(Org . "8.3")
1450 :type 'integer)
1452 (defgroup org-agenda-time-grid nil
1453 "Options concerning the time grid in the Org Agenda."
1454 :tag "Org Agenda Time Grid"
1455 :group 'org-agenda)
1457 (defcustom org-agenda-search-headline-for-time t
1458 "Non-nil means search headline for a time-of-day.
1459 If the headline contains a time-of-day in one format or another, it will
1460 be used to sort the entry into the time sequence of items for a day.
1461 Some people have time stamps in the headline that refer to the creation
1462 time or so, and then this produces an unwanted side effect. If this is
1463 the case for your, use this variable to turn off searching the headline
1464 for a time."
1465 :group 'org-agenda-time-grid
1466 :type 'boolean)
1468 (defcustom org-agenda-use-time-grid t
1469 "Non-nil means show a time grid in the agenda schedule.
1470 A time grid is a set of lines for specific times (like every two hours between
1471 8:00 and 20:00). The items scheduled for a day at specific times are
1472 sorted in between these lines.
1473 For details about when the grid will be shown, and what it will look like, see
1474 the variable `org-agenda-time-grid'."
1475 :group 'org-agenda-time-grid
1476 :type 'boolean)
1478 (defcustom org-agenda-time-grid
1479 '((daily today require-timed)
1480 (800 1000 1200 1400 1600 1800 2000)
1481 "......"
1482 "----------------")
1484 "The settings for time grid for agenda display.
1485 This is a list of four items. The first item is again a list. It contains
1486 symbols specifying conditions when the grid should be displayed:
1488 daily if the agenda shows a single day
1489 weekly if the agenda shows an entire week
1490 today show grid on current date, independent of daily/weekly display
1491 require-timed show grid only if at least one item has a time specification
1492 remove-match skip grid times already present in an entry
1494 The second item is a list of integers, indicating the times that
1495 should have a grid line.
1497 The third item is a string which will be placed right after the
1498 times that have a grid line.
1500 The fourth item is a string placed after the grid times. This
1501 will align with agenda items"
1502 :group 'org-agenda-time-grid
1503 :type
1504 '(list
1505 (set :greedy t :tag "Grid Display Options"
1506 (const :tag "Show grid in single day agenda display" daily)
1507 (const :tag "Show grid in weekly agenda display" weekly)
1508 (const :tag "Always show grid for today" today)
1509 (const :tag "Show grid only if any timed entries are present"
1510 require-timed)
1511 (const :tag "Skip grid times already present in an entry"
1512 remove-match))
1513 (repeat :tag "Grid Times" (integer :tag "Time"))
1514 (string :tag "Grid String (after agenda times)")
1515 (string :tag "Grid String (aligns with agenda items)")))
1517 (defcustom org-agenda-show-current-time-in-grid t
1518 "Non-nil means show the current time in the time grid."
1519 :group 'org-agenda-time-grid
1520 :version "24.1"
1521 :type 'boolean)
1523 (defcustom org-agenda-current-time-string
1524 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1525 "The string for the current time marker in the agenda."
1526 :group 'org-agenda-time-grid
1527 :version "24.1"
1528 :type 'string)
1530 (defgroup org-agenda-sorting nil
1531 "Options concerning sorting in the Org Agenda."
1532 :tag "Org Agenda Sorting"
1533 :group 'org-agenda)
1535 (defcustom org-agenda-sorting-strategy
1536 '((agenda habit-down time-up priority-down category-keep)
1537 (todo priority-down category-keep)
1538 (tags priority-down category-keep)
1539 (search category-keep))
1540 "Sorting structure for the agenda items of a single day.
1541 This is a list of symbols which will be used in sequence to determine
1542 if an entry should be listed before another entry. The following
1543 symbols are recognized:
1545 time-up Put entries with time-of-day indications first, early first
1546 time-down Put entries with time-of-day indications first, late first
1547 timestamp-up Sort by any timestamp, early first
1548 timestamp-down Sort by any timestamp, late first
1549 scheduled-up Sort by scheduled timestamp, early first
1550 scheduled-down Sort by scheduled timestamp, late first
1551 deadline-up Sort by deadline timestamp, early first
1552 deadline-down Sort by deadline timestamp, late first
1553 ts-up Sort by active timestamp, early first
1554 ts-down Sort by active timestamp, late first
1555 tsia-up Sort by inactive timestamp, early first
1556 tsia-down Sort by inactive timestamp, late first
1557 category-keep Keep the default order of categories, corresponding to the
1558 sequence in `org-agenda-files'.
1559 category-up Sort alphabetically by category, A-Z.
1560 category-down Sort alphabetically by category, Z-A.
1561 tag-up Sort alphabetically by last tag, A-Z.
1562 tag-down Sort alphabetically by last tag, Z-A.
1563 priority-up Sort numerically by priority, high priority last.
1564 priority-down Sort numerically by priority, high priority first.
1565 todo-state-up Sort by todo state, tasks that are done last.
1566 todo-state-down Sort by todo state, tasks that are done first.
1567 effort-up Sort numerically by estimated effort, high effort last.
1568 effort-down Sort numerically by estimated effort, high effort first.
1569 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1570 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1571 habit-up Put entries that are habits first
1572 habit-down Put entries that are habits last
1573 alpha-up Sort headlines alphabetically
1574 alpha-down Sort headlines alphabetically, reversed
1576 The different possibilities will be tried in sequence, and testing stops
1577 if one comparison returns a \"not-equal\". For example, the default
1578 '(time-up category-keep priority-down)
1579 means: Pull out all entries having a specified time of day and sort them,
1580 in order to make a time schedule for the current day the first thing in the
1581 agenda listing for the day. Of the entries without a time indication, keep
1582 the grouped in categories, don't sort the categories, but keep them in
1583 the sequence given in `org-agenda-files'. Within each category sort by
1584 priority.
1586 Leaving out `category-keep' would mean that items will be sorted across
1587 categories by priority.
1589 Instead of a single list, this can also be a set of list for specific
1590 contents, with a context symbol in the car of the list, any of
1591 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1593 Custom commands can bind this variable in the options section."
1594 :group 'org-agenda-sorting
1595 :type `(choice
1596 (repeat :tag "General" ,org-sorting-choice)
1597 (list :tag "Individually"
1598 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1599 (repeat ,org-sorting-choice))
1600 (cons (const :tag "Strategy for TODO lists" todo)
1601 (repeat ,org-sorting-choice))
1602 (cons (const :tag "Strategy for Tags matches" tags)
1603 (repeat ,org-sorting-choice))
1604 (cons (const :tag "Strategy for search matches" search)
1605 (repeat ,org-sorting-choice)))))
1607 (defcustom org-agenda-cmp-user-defined nil
1608 "A function to define the comparison `user-defined'.
1609 This function must receive two arguments, agenda entry a and b.
1610 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1611 the user comparison, return nil.
1612 When this is defined, you can make `user-defined-up' and `user-defined-down'
1613 part of an agenda sorting strategy."
1614 :group 'org-agenda-sorting
1615 :type 'symbol)
1617 (defcustom org-sort-agenda-notime-is-late t
1618 "Non-nil means items without time are considered late.
1619 This is only relevant for sorting. When t, items which have no explicit
1620 time like 15:30 will be considered as 99:01, i.e. later than any items which
1621 do have a time. When nil, the default time is before 0:00. You can use this
1622 option to decide if the schedule for today should come before or after timeless
1623 agenda entries."
1624 :group 'org-agenda-sorting
1625 :type 'boolean)
1627 (defcustom org-sort-agenda-noeffort-is-high t
1628 "Non-nil means items without effort estimate are sorted as high effort.
1629 This also applies when filtering an agenda view with respect to the
1630 < or > effort operator. Then, tasks with no effort defined will be treated
1631 as tasks with high effort.
1632 When nil, such items are sorted as 0 minutes effort."
1633 :group 'org-agenda-sorting
1634 :type 'boolean)
1636 (defgroup org-agenda-line-format nil
1637 "Options concerning the entry prefix in the Org agenda display."
1638 :tag "Org Agenda Line Format"
1639 :group 'org-agenda)
1641 (defcustom org-agenda-prefix-format
1642 '((agenda . " %i %-12:c%?-12t% s")
1643 (todo . " %i %-12:c")
1644 (tags . " %i %-12:c")
1645 (search . " %i %-12:c"))
1646 "Format specifications for the prefix of items in the agenda views.
1647 An alist with five entries, each for the different agenda types. The
1648 keys of the sublists are `agenda', `todo', `search' and `tags'.
1649 The values are format strings.
1651 This format works similar to a printf format, with the following meaning:
1653 %c the category of the item, \"Diary\" for entries from the diary,
1654 or as given by the CATEGORY keyword or derived from the file name
1655 %e the effort required by the item
1656 %l the level of the item (insert X space(s) if item is of level X)
1657 %i the icon category of the item, see `org-agenda-category-icon-alist'
1658 %T the last tag of the item (ignore inherited tags, which come first)
1659 %t the HH:MM time-of-day specification if one applies to the entry
1660 %s Scheduling/Deadline information, a short string
1661 %b show breadcrumbs, i.e., the names of the higher levels
1662 %(expression) Eval EXPRESSION and replace the control string
1663 by the result
1665 All specifiers work basically like the standard `%s' of printf, but may
1666 contain two additional characters: a question mark just after the `%'
1667 and a whitespace/punctuation character just before the final letter.
1669 If the first character after `%' is a question mark, the entire field
1670 will only be included if the corresponding value applies to the current
1671 entry. This is useful for fields which should have fixed width when
1672 present, but zero width when absent. For example, \"%?-12t\" will
1673 result in a 12 character time field if a time of the day is specified,
1674 but will completely disappear in entries which do not contain a time.
1676 If there is punctuation or whitespace character just before the
1677 final format letter, this character will be appended to the field
1678 value if the value is not empty. For example, the format
1679 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1680 the category is empty, no additional colon is inserted.
1682 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1683 which means:
1685 - Indent the line with two space characters
1686 - Give the category a 12 chars wide field, padded with whitespace on
1687 the right (because of `-'). Append a colon if there is a category
1688 (because of `:').
1689 - If there is a time-of-day, put it into a 12 chars wide field. If no
1690 time, don't put in an empty field, just skip it (because of '?').
1691 - Finally, put the scheduling information.
1693 See also the variables `org-agenda-remove-times-when-in-prefix' and
1694 `org-agenda-remove-tags'.
1696 Custom commands can set this variable in the options section."
1697 :type '(choice
1698 (string :tag "General format")
1699 (list :greedy t :tag "View dependent"
1700 (cons (const agenda) (string :tag "Format"))
1701 (cons (const todo) (string :tag "Format"))
1702 (cons (const tags) (string :tag "Format"))
1703 (cons (const search) (string :tag "Format"))))
1704 :group 'org-agenda-line-format
1705 :version "26.1"
1706 :package-version '(Org . "9.1"))
1708 (defvar org-prefix-format-compiled nil
1709 "The compiled prefix format and associated variables.
1710 This is a list where first element is a list of variable bindings, and second
1711 element is the compiled format expression. See the variable
1712 `org-agenda-prefix-format'.")
1714 (defcustom org-agenda-todo-keyword-format "%-1s"
1715 "Format for the TODO keyword in agenda lines.
1716 Set this to something like \"%-12s\" if you want all TODO keywords
1717 to occupy a fixed space in the agenda display."
1718 :group 'org-agenda-line-format
1719 :type 'string)
1721 (defcustom org-agenda-diary-sexp-prefix nil
1722 "A regexp that matches part of a diary sexp entry
1723 which should be treated as scheduling/deadline information in
1724 `org-agenda'.
1726 For example, you can use this to extract the `diary-remind-message' from
1727 `diary-remind' entries."
1728 :group 'org-agenda-line-format
1729 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1731 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1732 "Text preceding timerange entries in the agenda view.
1733 This is a list with two strings. The first applies when the range
1734 is entirely on one day. The second applies if the range spans several days.
1735 The strings may have two \"%d\" format specifiers which will be filled
1736 with the sequence number of the days, and the total number of days in the
1737 range, respectively."
1738 :group 'org-agenda-line-format
1739 :type '(list
1740 (string :tag "Deadline today ")
1741 (choice :tag "Deadline relative"
1742 (string :tag "Format string")
1743 (function))))
1745 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1746 "Text preceding scheduled items in the agenda view.
1747 This is a list with two strings. The first applies when the item is
1748 scheduled on the current day. The second applies when it has been scheduled
1749 previously, it may contain a %d indicating that this is the nth time that
1750 this item is scheduled, due to automatic rescheduling of unfinished items
1751 for the following day. So this number is one larger than the number of days
1752 that passed since this item was scheduled first."
1753 :group 'org-agenda-line-format
1754 :version "24.4"
1755 :package-version '(Org . "8.0")
1756 :type '(list
1757 (string :tag "Scheduled today ")
1758 (string :tag "Scheduled previously")))
1760 (defcustom org-agenda-inactive-leader "["
1761 "Text preceding item pulled into the agenda by inactive time stamps.
1762 These entries are added to the agenda when pressing \"[\"."
1763 :group 'org-agenda-line-format
1764 :version "24.1"
1765 :type 'string)
1767 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1768 "Text preceding deadline items in the agenda view.
1769 This is a list with three strings. The first applies when the item has its
1770 deadline on the current day. The second applies when the deadline is in the
1771 future, the third one when it is in the past. The strings may contain %d
1772 to capture the number of days."
1773 :group 'org-agenda-line-format
1774 :version "24.4"
1775 :package-version '(Org . "8.0")
1776 :type '(list
1777 (string :tag "Deadline today ")
1778 (string :tag "Deadline in the future ")
1779 (string :tag "Deadline in the past ")))
1781 (defcustom org-agenda-remove-times-when-in-prefix t
1782 "Non-nil means remove duplicate time specifications in agenda items.
1783 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1784 time-of-day specification in a headline or diary entry is extracted and
1785 placed into the prefix. If this option is non-nil, the original specification
1786 \(a timestamp or -range, or just a plain time(range) specification like
1787 11:30-4pm) will be removed for agenda display. This makes the agenda less
1788 cluttered.
1789 The option can be t or nil. It may also be the symbol `beg', indicating
1790 that the time should only be removed when it is located at the beginning of
1791 the headline/diary entry."
1792 :group 'org-agenda-line-format
1793 :type '(choice
1794 (const :tag "Always" t)
1795 (const :tag "Never" nil)
1796 (const :tag "When at beginning of entry" beg)))
1798 (defcustom org-agenda-remove-timeranges-from-blocks nil
1799 "Non-nil means remove time ranges specifications in agenda
1800 items that span on several days."
1801 :group 'org-agenda-line-format
1802 :version "24.1"
1803 :type 'boolean)
1805 (defcustom org-agenda-default-appointment-duration nil
1806 "Default duration for appointments that only have a starting time.
1807 When nil, no duration is specified in such cases.
1808 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1809 :group 'org-agenda-line-format
1810 :type '(choice
1811 (integer :tag "Minutes")
1812 (const :tag "No default duration")))
1814 (defcustom org-agenda-show-inherited-tags t
1815 "Non-nil means show inherited tags in each agenda line.
1817 When this option is set to `always', it takes precedence over
1818 `org-agenda-use-tag-inheritance' and inherited tags are shown
1819 in every agenda.
1821 When this option is set to t (the default), inherited tags are
1822 shown when they are available, i.e. when the value of
1823 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1824 given agenda type.
1826 This can be set to a list of agenda types in which the agenda
1827 must display the inherited tags. Available types are `todo',
1828 `agenda' and `search'.
1830 When set to nil, never show inherited tags in agenda lines."
1831 :group 'org-agenda-line-format
1832 :group 'org-agenda
1833 :version "24.3"
1834 :type '(choice
1835 (const :tag "Show inherited tags when available" t)
1836 (const :tag "Always show inherited tags" always)
1837 (repeat :tag "Show inherited tags only in selected agenda types"
1838 (symbol :tag "Agenda type"))))
1840 (defcustom org-agenda-use-tag-inheritance '(todo search agenda)
1841 "List of agenda view types where to use tag inheritance.
1843 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1844 controlled by `org-use-tag-inheritance'. In other agenda types,
1845 `org-use-tag-inheritance' is not used for the selection of the
1846 agenda entries. Still, you may want the agenda to be aware of
1847 the inherited tags anyway, e.g. for later tag filtering.
1849 Allowed value are `todo', `search' and `agenda'.
1851 This variable has no effect if `org-agenda-show-inherited-tags'
1852 is set to `always'. In that case, the agenda is aware of those
1853 tags.
1855 The default value sets tags in every agenda type. Setting this
1856 option to nil will speed up non-tags agenda view a lot."
1857 :group 'org-agenda
1858 :version "26.1"
1859 :package-version '(Org . "9.1")
1860 :type '(choice
1861 (const :tag "Use tag inheritance in all agenda types" t)
1862 (repeat :tag "Use tag inheritance in selected agenda types"
1863 (symbol :tag "Agenda type"))))
1865 (defcustom org-agenda-hide-tags-regexp nil
1866 "Regular expression used to filter away specific tags in agenda views.
1867 This means that these tags will be present, but not be shown in the agenda
1868 line. Secondary filtering will still work on the hidden tags.
1869 Nil means don't hide any tags."
1870 :group 'org-agenda-line-format
1871 :type '(choice
1872 (const :tag "Hide none" nil)
1873 (string :tag "Regexp ")))
1875 (defcustom org-agenda-remove-tags nil
1876 "Non-nil means remove the tags from the headline copy in the agenda.
1877 When this is the symbol `prefix', only remove tags when
1878 `org-agenda-prefix-format' contains a `%T' specifier."
1879 :group 'org-agenda-line-format
1880 :type '(choice
1881 (const :tag "Always" t)
1882 (const :tag "Never" nil)
1883 (const :tag "When prefix format contains %T" prefix)))
1885 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1886 'org-agenda-remove-tags)
1888 (defcustom org-agenda-tags-column 'auto
1889 "Shift tags in agenda items to this column.
1890 If set to `auto', tags will be automatically aligned to the right
1891 edge of the window.
1893 If set to a positive number, tags will be left-aligned to that
1894 column. If set to a negative number, tags will be right-aligned
1895 to that column. For example, -80 works well for a normal 80
1896 character screen."
1897 :group 'org-agenda-line-format
1898 :type '(choice
1899 (const :tag "Automatically align to right edge of window" auto)
1900 (integer :tag "Specific column" -80))
1901 :package-version '(Org . "9.1")
1902 :version "26.1")
1904 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1906 (defcustom org-agenda-fontify-priorities 'cookies
1907 "Non-nil means highlight low and high priorities in agenda.
1908 When t, the highest priority entries are bold, lowest priority italic.
1909 However, settings in `org-priority-faces' will overrule these faces.
1910 When this variable is the symbol `cookies', only fontify the
1911 cookies, not the entire task.
1912 This may also be an association list of priority faces, whose
1913 keys are the character values of `org-highest-priority',
1914 `org-default-priority', and `org-lowest-priority' (the default values
1915 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1916 color as a string, or a list like `(:background \"Red\")'.
1917 If it is a color, the variable `org-faces-easy-properties'
1918 determines if it is a foreground or a background color."
1919 :group 'org-agenda-line-format
1920 :type '(choice
1921 (const :tag "Never" nil)
1922 (const :tag "Defaults" t)
1923 (const :tag "Cookies only" cookies)
1924 (repeat :tag "Specify"
1925 (list (character :tag "Priority" :value ?A)
1926 (choice :tag "Face "
1927 (string :tag "Color")
1928 (sexp :tag "Face"))))))
1930 (defcustom org-agenda-day-face-function nil
1931 "Function called to determine what face should be used to display a day.
1932 The only argument passed to that function is the day. It should
1933 returns a face, or nil if does not want to specify a face and let
1934 the normal rules apply."
1935 :group 'org-agenda-line-format
1936 :version "24.1"
1937 :type '(choice (const nil) (function)))
1939 (defcustom org-agenda-category-icon-alist nil
1940 "Alist of category icon to be displayed in agenda views.
1942 Each entry should have the following format:
1944 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1946 Where CATEGORY-REGEXP is a regexp matching the categories where
1947 the icon should be displayed.
1948 FILE-OR-DATA either a file path or a string containing image data.
1950 The other fields can be omitted safely if not needed:
1951 TYPE indicates the image type.
1952 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1953 image data.
1954 PROPS are additional image attributes to assign to the image,
1955 like, e.g. `:ascent center'.
1957 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1959 If you want to set the display properties yourself, just put a
1960 list as second element:
1962 (CATEGORY-REGEXP (MY PROPERTY LIST))
1964 For example, to display a 16px horizontal space for Emacs
1965 category, you can use:
1967 (\"Emacs\" \\='(space . (:width (16))))"
1968 :group 'org-agenda-line-format
1969 :version "24.1"
1970 :type '(alist :key-type (string :tag "Regexp matching category")
1971 :value-type (choice (list :tag "Icon"
1972 (string :tag "File or data")
1973 (symbol :tag "Type")
1974 (boolean :tag "Data?")
1975 (repeat :tag "Extra image properties" :inline t symbol))
1976 (list :tag "Display properties" sexp))))
1978 (defgroup org-agenda-column-view nil
1979 "Options concerning column view in the agenda."
1980 :tag "Org Agenda Column View"
1981 :group 'org-agenda)
1983 (defcustom org-agenda-view-columns-initially nil
1984 "When non-nil, switch to columns view right after creating the agenda."
1985 :group 'org-agenda-column-view
1986 :type 'boolean
1987 :version "26.1"
1988 :package-version '(Org . "9.0")
1989 :safe #'booleanp)
1991 (defcustom org-agenda-columns-show-summaries t
1992 "Non-nil means show summaries for columns displayed in the agenda view."
1993 :group 'org-agenda-column-view
1994 :type 'boolean)
1996 (defcustom org-agenda-columns-compute-summary-properties t
1997 "Non-nil means recompute all summary properties before column view.
1998 When column view in the agenda is listing properties that have a summary
1999 operator, it can go to all relevant buffers and recompute the summaries
2000 there. This can mean overhead for the agenda column view, but is necessary
2001 to have thing up to date.
2002 As a special case, a CLOCKSUM property also makes sure that the clock
2003 computations are current."
2004 :group 'org-agenda-column-view
2005 :type 'boolean)
2007 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
2008 "Non-nil means the duration of an appointment will add to day effort.
2009 The property to which appointment durations will be added is the one given
2010 in the option `org-effort-property'. If an appointment does not have
2011 an end time, `org-agenda-default-appointment-duration' will be used. If that
2012 is not set, an appointment without end time will not contribute to the time
2013 estimate."
2014 :group 'org-agenda-column-view
2015 :type 'boolean)
2017 (defcustom org-agenda-auto-exclude-function nil
2018 "A function called with a tag to decide if it is filtered on \
2019 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
2020 The sole argument to the function, which is called once for each
2021 possible tag, is a string giving the name of the tag. The
2022 function should return either nil if the tag should be included
2023 as normal, or \"-<TAG>\" to exclude the tag.
2024 Note that for the purpose of tag filtering, only the lower-case version of
2025 all tags will be considered, so that this function will only ever see
2026 the lower-case version of all tags."
2027 :group 'org-agenda
2028 :type '(choice (const nil) (function)))
2030 (defcustom org-agenda-bulk-custom-functions nil
2031 "Alist of characters and custom functions for bulk actions.
2032 For example, this value makes those two functions available:
2034 \\='((?R set-category)
2035 (?C bulk-cut))
2037 With selected entries in an agenda buffer, `B R' will call
2038 the custom function `set-category' on the selected entries.
2039 Note that functions in this alist don't need to be quoted."
2040 :type '(alist :key-type character :value-type (group function))
2041 :version "24.1"
2042 :group 'org-agenda)
2044 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2045 "Execute BODY with point at location given by `org-hd-marker' property.
2046 If STRING is non-nil, the text property will be fetched from position 0
2047 in that string. If STRING is nil, it will be fetched from the beginning
2048 of the current line."
2049 (org-with-gensyms (marker)
2050 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2051 'org-hd-marker ,string)))
2052 (with-current-buffer (marker-buffer ,marker)
2053 (save-excursion
2054 (goto-char ,marker)
2055 ,@body)))))
2056 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2058 (defun org-add-agenda-custom-command (entry)
2059 "Replace or add a command in `org-agenda-custom-commands'.
2060 This is mostly for hacking and trying a new command - once the command
2061 works you probably want to add it to `org-agenda-custom-commands' for good."
2062 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2063 (if ass
2064 (setcdr ass (cdr entry))
2065 (push entry org-agenda-custom-commands))))
2067 ;;; Define the org-agenda-mode
2069 (defvar org-agenda-mode-map (make-sparse-keymap)
2070 "Keymap for `org-agenda-mode'.")
2071 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2073 (defvar org-agenda-menu) ; defined later in this file.
2074 (defvar org-agenda-restrict nil) ; defined later in this file.
2075 (defvar org-agenda-follow-mode nil)
2076 (defvar org-agenda-entry-text-mode nil)
2077 (defvar org-agenda-clockreport-mode nil)
2078 (defvar org-agenda-show-log nil)
2079 (defvar org-agenda-redo-command nil)
2080 (defvar org-agenda-query-string nil)
2081 (defvar org-agenda-mode-hook nil
2082 "Hook run after `org-agenda-mode' is turned on.
2083 The buffer is still writable when this hook is called.")
2084 (defvar org-agenda-type nil)
2085 (defvar org-agenda-force-single-file nil)
2086 (defvar org-agenda-bulk-marked-entries nil
2087 "List of markers that refer to marked entries in the agenda.")
2088 (defvar org-agenda-current-date nil
2089 "Active date when building the agenda.")
2091 ;;; Multiple agenda buffers support
2093 (defcustom org-agenda-sticky nil
2094 "Non-nil means agenda q key will bury agenda buffers.
2095 Agenda commands will then show existing buffer instead of generating new ones.
2096 When nil, `q' will kill the single agenda buffer."
2097 :group 'org-agenda
2098 :version "24.3"
2099 :type 'boolean)
2102 ;;;###autoload
2103 (defun org-toggle-sticky-agenda (&optional arg)
2104 "Toggle `org-agenda-sticky'."
2105 (interactive "P")
2106 (let ((new-value (if arg
2107 (> (prefix-numeric-value arg) 0)
2108 (not org-agenda-sticky))))
2109 (if (equal new-value org-agenda-sticky)
2110 (and (called-interactively-p 'interactive)
2111 (message "Sticky agenda was already %s"
2112 (if org-agenda-sticky "enabled" "disabled")))
2113 (setq org-agenda-sticky new-value)
2114 (org-agenda-kill-all-agenda-buffers)
2115 (and (called-interactively-p 'interactive)
2116 (message "Sticky agenda %s"
2117 (if org-agenda-sticky "enabled" "disabled"))))))
2119 (defvar org-agenda-buffer nil
2120 "Agenda buffer currently being generated.")
2122 (defvar org-agenda-last-prefix-arg nil)
2123 (defvar org-agenda-this-buffer-name nil)
2124 (defvar org-agenda-doing-sticky-redo nil)
2125 (defvar org-agenda-this-buffer-is-sticky nil)
2126 (defvar org-agenda-last-indirect-buffer nil
2127 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2129 (defconst org-agenda-local-vars
2130 '(org-agenda-this-buffer-name
2131 org-agenda-undo-list
2132 org-agenda-pending-undo-list
2133 org-agenda-follow-mode
2134 org-agenda-entry-text-mode
2135 org-agenda-clockreport-mode
2136 org-agenda-show-log
2137 org-agenda-redo-command
2138 org-agenda-query-string
2139 org-agenda-type
2140 org-agenda-bulk-marked-entries
2141 org-agenda-undo-has-started-in
2142 org-agenda-info
2143 org-agenda-pre-window-conf
2144 org-agenda-columns-active
2145 org-agenda-tag-filter
2146 org-agenda-category-filter
2147 org-agenda-top-headline-filter
2148 org-agenda-regexp-filter
2149 org-agenda-effort-filter
2150 org-agenda-markers
2151 org-agenda-last-search-view-search-was-boolean
2152 org-agenda-last-indirect-buffer
2153 org-agenda-filtered-by-category
2154 org-agenda-filter-form
2155 org-agenda-cycle-counter
2156 org-agenda-last-prefix-arg)
2157 "Variables that must be local in agenda buffers to allow multiple buffers.")
2159 (defun org-agenda-mode ()
2160 "Mode for time-sorted view on action items in Org files.
2162 The following commands are available:
2164 \\{org-agenda-mode-map}"
2165 (interactive)
2166 (cond (org-agenda-doing-sticky-redo
2167 ;; Refreshing sticky agenda-buffer
2169 ;; Preserve the value of `org-agenda-local-vars' variables,
2170 ;; while letting `kill-all-local-variables' kill the rest
2171 (let ((save (buffer-local-variables)))
2172 (kill-all-local-variables)
2173 (mapc #'make-local-variable org-agenda-local-vars)
2174 (dolist (elem save)
2175 (pcase elem
2176 (`(,var . ,val) ;ignore unbound variables
2177 (when (and val (memq var org-agenda-local-vars))
2178 (set var val))))))
2179 (setq-local org-agenda-this-buffer-is-sticky t))
2180 (org-agenda-sticky
2181 ;; Creating a sticky Agenda buffer for the first time
2182 (kill-all-local-variables)
2183 (mapc 'make-local-variable org-agenda-local-vars)
2184 (setq-local org-agenda-this-buffer-is-sticky t))
2186 ;; Creating a non-sticky agenda buffer
2187 (kill-all-local-variables)
2188 (setq-local org-agenda-this-buffer-is-sticky nil)))
2189 (setq org-agenda-undo-list nil
2190 org-agenda-pending-undo-list nil
2191 org-agenda-bulk-marked-entries nil)
2192 (setq major-mode 'org-agenda-mode)
2193 ;; Keep global-font-lock-mode from turning on font-lock-mode
2194 (setq-local font-lock-global-modes (list 'not major-mode))
2195 (setq mode-name "Org-Agenda")
2196 (setq indent-tabs-mode nil)
2197 (use-local-map org-agenda-mode-map)
2198 (easy-menu-add org-agenda-menu)
2199 (if org-startup-truncated (setq truncate-lines t))
2200 (setq-local line-move-visual nil)
2201 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2202 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2203 ;; Make sure properties are removed when copying text
2204 (add-hook 'filter-buffer-substring-functions
2205 (lambda (fun start end delete)
2206 (substring-no-properties (funcall fun start end delete)))
2207 nil t)
2208 (unless org-agenda-keep-modes
2209 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2210 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2211 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2212 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2213 (add-to-invisibility-spec '(org-filtered))
2214 (add-to-invisibility-spec '(org-link))
2215 (easy-menu-change
2216 '("Agenda") "Agenda Files"
2217 (append
2218 (list
2219 (vector
2220 (if (get 'org-agenda-files 'org-restrict)
2221 "Restricted to single file"
2222 "Edit File List")
2223 '(org-edit-agenda-file-list)
2224 (not (get 'org-agenda-files 'org-restrict)))
2225 "--")
2226 (mapcar 'org-file-menu-entry (org-agenda-files))))
2227 (org-agenda-set-mode-name)
2228 (apply
2229 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2230 (list 'org-agenda-mode-hook)))
2232 (substitute-key-definition 'undo 'org-agenda-undo
2233 org-agenda-mode-map global-map)
2234 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2235 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2236 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2237 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2238 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2239 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2240 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2241 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2242 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2243 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2244 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2245 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2246 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2247 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2248 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2249 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2250 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2251 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2252 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2253 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2254 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2255 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2256 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2257 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2258 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2259 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2260 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2261 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2262 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2263 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2264 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2265 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2266 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2267 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2268 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2269 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2270 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2271 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2272 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2273 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2274 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2275 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2276 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2277 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2278 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2279 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2280 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2281 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2282 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2283 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2285 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2286 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2287 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2288 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2289 (while l (org-defkey org-agenda-mode-map
2290 (int-to-string (pop l)) 'digit-argument)))
2292 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2293 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2294 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2295 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2296 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2297 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2298 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2299 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2300 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2301 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo-all)
2302 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2303 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2304 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2305 'org-clock-modify-effort-estimate)
2306 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2307 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2308 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2309 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2310 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2311 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2312 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2313 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2314 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2315 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2316 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2317 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2318 (substitute-key-definition 'next-line 'org-agenda-next-line
2319 org-agenda-mode-map global-map)
2320 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2321 org-agenda-mode-map global-map)
2322 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2323 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2324 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2325 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2326 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2327 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2328 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2329 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2330 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2331 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2332 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2333 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2334 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2335 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2336 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2337 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2338 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2339 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2340 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2341 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2342 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2343 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2344 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2345 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2346 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2347 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2348 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2349 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2350 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2351 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2352 (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
2354 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2355 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2356 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2357 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2358 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2359 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2360 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2361 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2362 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2363 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2364 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2365 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2366 (org-defkey org-agenda-mode-map "\C-c\C-x_" 'org-timer-stop)
2367 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2368 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2369 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2371 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2372 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2374 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2375 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2377 (when org-agenda-mouse-1-follows-link
2378 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2379 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2380 '("Agenda"
2381 ("Agenda Files")
2382 "--"
2383 ("Agenda Dates"
2384 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda)]
2385 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2386 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2387 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2388 "--"
2389 ("View"
2390 ["Day View" org-agenda-day-view
2391 :active (org-agenda-check-type nil 'agenda)
2392 :style radio :selected (eq org-agenda-current-span 'day)
2393 :keys "v d (or just d)"]
2394 ["Week View" org-agenda-week-view
2395 :active (org-agenda-check-type nil 'agenda)
2396 :style radio :selected (eq org-agenda-current-span 'week)
2397 :keys "v w"]
2398 ["Fortnight View" org-agenda-fortnight-view
2399 :active (org-agenda-check-type nil 'agenda)
2400 :style radio :selected (eq org-agenda-current-span 'fortnight)
2401 :keys "v t"]
2402 ["Month View" org-agenda-month-view
2403 :active (org-agenda-check-type nil 'agenda)
2404 :style radio :selected (eq org-agenda-current-span 'month)
2405 :keys "v m"]
2406 ["Year View" org-agenda-year-view
2407 :active (org-agenda-check-type nil 'agenda)
2408 :style radio :selected (eq org-agenda-current-span 'year)
2409 :keys "v y"]
2410 "--"
2411 ["Include Diary" org-agenda-toggle-diary
2412 :style toggle :selected org-agenda-include-diary
2413 :active (org-agenda-check-type nil 'agenda)]
2414 ["Include Deadlines" org-agenda-toggle-deadlines
2415 :style toggle :selected org-agenda-include-deadlines
2416 :active (org-agenda-check-type nil 'agenda)]
2417 ["Use Time Grid" org-agenda-toggle-time-grid
2418 :style toggle :selected org-agenda-use-time-grid
2419 :active (org-agenda-check-type nil 'agenda)]
2420 "--"
2421 ["Show clock report" org-agenda-clockreport-mode
2422 :style toggle :selected org-agenda-clockreport-mode
2423 :active (org-agenda-check-type nil 'agenda)]
2424 ["Show some entry text" org-agenda-entry-text-mode
2425 :style toggle :selected org-agenda-entry-text-mode
2426 :active t]
2427 "--"
2428 ["Show Logbook entries" org-agenda-log-mode
2429 :style toggle :selected org-agenda-show-log
2430 :active (org-agenda-check-type nil 'agenda)
2431 :keys "v l (or just l)"]
2432 ["Include archived trees" org-agenda-archives-mode
2433 :style toggle :selected org-agenda-archives-mode :active t
2434 :keys "v a"]
2435 ["Include archive files" (org-agenda-archives-mode t)
2436 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2437 :keys "v A"]
2438 "--"
2439 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2440 ["Write view to file" org-agenda-write t]
2441 ["Rebuild buffer" org-agenda-redo t]
2442 ["Save all Org buffers" org-save-all-org-buffers t]
2443 "--"
2444 ["Show original entry" org-agenda-show t]
2445 ["Go To (other window)" org-agenda-goto t]
2446 ["Go To (this window)" org-agenda-switch-to t]
2447 ["Capture with cursor date" org-agenda-capture t]
2448 ["Follow Mode" org-agenda-follow-mode
2449 :style toggle :selected org-agenda-follow-mode :active t]
2450 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2451 "--"
2452 ("TODO"
2453 ["Cycle TODO" org-agenda-todo t]
2454 ["Next TODO set" org-agenda-todo-nextset t]
2455 ["Previous TODO set" org-agenda-todo-previousset t]
2456 ["Add note" org-agenda-add-note t])
2457 ("Archive/Refile/Delete"
2458 ["Archive default" org-agenda-archive-default t]
2459 ["Archive default" org-agenda-archive-default-with-confirmation t]
2460 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2461 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2462 ["Archive subtree" org-agenda-archive t]
2463 "--"
2464 ["Refile" org-agenda-refile t]
2465 "--"
2466 ["Delete subtree" org-agenda-kill t])
2467 ("Bulk action"
2468 ["Mark entry" org-agenda-bulk-mark t]
2469 ["Mark all" org-agenda-bulk-mark-all t]
2470 ["Unmark entry" org-agenda-bulk-unmark t]
2471 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2472 ["Toggle mark" org-agenda-bulk-toggle t]
2473 ["Toggle all" org-agenda-bulk-toggle-all t]
2474 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2475 ["Act on all marked" org-agenda-bulk-action t]
2476 "--"
2477 ("Tags and Properties"
2478 ["Show all Tags" org-agenda-show-tags t]
2479 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2480 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2481 "--"
2482 ["Column View" org-columns t])
2483 ("Deadline/Schedule"
2484 ["Schedule" org-agenda-schedule t]
2485 ["Set Deadline" org-agenda-deadline t]
2486 "--"
2487 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda)]
2488 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda)]
2489 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u S-right"]
2490 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u S-left"]
2491 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-right"]
2492 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-left"]
2493 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda)])
2494 ("Clock and Effort"
2495 ["Clock in" org-agenda-clock-in t]
2496 ["Clock out" org-agenda-clock-out t]
2497 ["Clock cancel" org-agenda-clock-cancel t]
2498 ["Goto running clock" org-clock-goto t]
2499 "--"
2500 ["Set Effort" org-agenda-set-effort t]
2501 ["Change clocked effort" org-clock-modify-effort-estimate
2502 (org-clock-is-active)])
2503 ("Priority"
2504 ["Set Priority" org-agenda-priority t]
2505 ["Increase Priority" org-agenda-priority-up t]
2506 ["Decrease Priority" org-agenda-priority-down t]
2507 ["Show Priority" org-show-priority t])
2508 ("Calendar/Diary"
2509 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda)]
2510 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda)]
2511 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda)]
2512 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda)]
2513 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda)]
2514 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda)]
2515 "--"
2516 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2517 "--"
2518 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2519 "--"
2520 ("MobileOrg"
2521 ["Push Files and Views" org-mobile-push t]
2522 ["Get Captured and Flagged" org-mobile-pull t]
2523 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2524 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2525 "--"
2526 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2527 "--"
2528 ["Quit" org-agenda-quit t]
2529 ["Exit and Release Buffers" org-agenda-exit t]
2532 ;;; Agenda undo
2534 (defvar org-agenda-allow-remote-undo t
2535 "Non-nil means allow remote undo from the agenda buffer.")
2536 (defvar org-agenda-undo-has-started-in nil
2537 "Buffers that have already seen `undo-start' in the current undo sequence.")
2539 (defun org-agenda-undo ()
2540 "Undo a remote editing step in the agenda.
2541 This undoes changes both in the agenda buffer and in the remote buffer
2542 that have been changed along."
2543 (interactive)
2544 (or org-agenda-allow-remote-undo
2545 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2546 (if (not (eq this-command last-command))
2547 (setq org-agenda-undo-has-started-in nil
2548 org-agenda-pending-undo-list org-agenda-undo-list))
2549 (if (not org-agenda-pending-undo-list)
2550 (user-error "No further undo information"))
2551 (let* ((entry (pop org-agenda-pending-undo-list))
2552 buf line cmd rembuf)
2553 (setq cmd (pop entry) line (pop entry))
2554 (setq rembuf (nth 2 entry))
2555 (org-with-remote-undo rembuf
2556 (while (bufferp (setq buf (pop entry)))
2557 (if (pop entry)
2558 (with-current-buffer buf
2559 (let ((last-undo-buffer buf)
2560 (inhibit-read-only t))
2561 (unless (memq buf org-agenda-undo-has-started-in)
2562 (push buf org-agenda-undo-has-started-in)
2563 (make-local-variable 'pending-undo-list)
2564 (undo-start))
2565 (while (and pending-undo-list
2566 (listp pending-undo-list)
2567 (not (car pending-undo-list)))
2568 (pop pending-undo-list))
2569 (undo-more 1))))))
2570 (org-goto-line line)
2571 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2573 (defun org-verify-change-for-undo (l1 l2)
2574 "Verify that a real change occurred between the undo lists L1 and L2."
2575 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2576 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2577 (not (eq l1 l2)))
2579 ;;; Agenda dispatch
2581 (defvar org-agenda-restrict-begin (make-marker))
2582 (defvar org-agenda-restrict-end (make-marker))
2583 (defvar org-agenda-last-dispatch-buffer nil)
2584 (defvar org-agenda-overriding-restriction nil)
2586 (defcustom org-agenda-custom-commands-contexts nil
2587 "Alist of custom agenda keys and contextual rules.
2589 For example, if you have a custom agenda command \"p\" and you
2590 want this command to be accessible only from plain text files,
2591 use this:
2593 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2595 Here are the available contexts definitions:
2597 in-file: command displayed only in matching files
2598 in-mode: command displayed only in matching modes
2599 not-in-file: command not displayed in matching files
2600 not-in-mode: command not displayed in matching modes
2601 in-buffer: command displayed only in matching buffers
2602 not-in-buffer: command not displayed in matching buffers
2603 [function]: a custom function taking no argument
2605 If you define several checks, the agenda command will be
2606 accessible if there is at least one valid check.
2608 You can also bind a key to another agenda custom command
2609 depending on contextual rules.
2611 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2613 Here it means: in .txt files, use \"p\" as the key for the
2614 agenda command otherwise associated with \"q\". (The command
2615 originally associated with \"q\" is not displayed to avoid
2616 duplicates.)"
2617 :version "24.3"
2618 :group 'org-agenda-custom-commands
2619 :type '(repeat (list :tag "Rule"
2620 (string :tag " Agenda key")
2621 (string :tag "Replace by command")
2622 (repeat :tag "Available when"
2623 (choice
2624 (cons :tag "Condition"
2625 (choice
2626 (const :tag "In file" in-file)
2627 (const :tag "Not in file" not-in-file)
2628 (const :tag "In buffer" in-buffer)
2629 (const :tag "Not in buffer" not-in-buffer)
2630 (const :tag "In mode" in-mode)
2631 (const :tag "Not in mode" not-in-mode))
2632 (regexp))
2633 (function :tag "Custom function"))))))
2635 (defcustom org-agenda-max-entries nil
2636 "Maximum number of entries to display in an agenda.
2637 This can be nil (no limit) or an integer or an alist of agenda
2638 types with an associated number of entries to display in this
2639 type."
2640 :version "24.4"
2641 :package-version '(Org . "8.0")
2642 :group 'org-agenda-custom-commands
2643 :type '(choice (symbol :tag "No limit" nil)
2644 (integer :tag "Max number of entries")
2645 (repeat
2646 (cons (choice :tag "Agenda type"
2647 (const agenda)
2648 (const todo)
2649 (const tags)
2650 (const search))
2651 (integer :tag "Max number of entries")))))
2653 (defcustom org-agenda-max-todos nil
2654 "Maximum number of TODOs to display in an agenda.
2655 This can be nil (no limit) or an integer or an alist of agenda
2656 types with an associated number of entries to display in this
2657 type."
2658 :version "24.4"
2659 :package-version '(Org . "8.0")
2660 :group 'org-agenda-custom-commands
2661 :type '(choice (symbol :tag "No limit" nil)
2662 (integer :tag "Max number of TODOs")
2663 (repeat
2664 (cons (choice :tag "Agenda type"
2665 (const agenda)
2666 (const todo)
2667 (const tags)
2668 (const search))
2669 (integer :tag "Max number of TODOs")))))
2671 (defcustom org-agenda-max-tags nil
2672 "Maximum number of tagged entries to display in an agenda.
2673 This can be nil (no limit) or an integer or an alist of agenda
2674 types with an associated number of entries to display in this
2675 type."
2676 :version "24.4"
2677 :package-version '(Org . "8.0")
2678 :group 'org-agenda-custom-commands
2679 :type '(choice (symbol :tag "No limit" nil)
2680 (integer :tag "Max number of tagged entries")
2681 (repeat
2682 (cons (choice :tag "Agenda type"
2683 (const agenda)
2684 (const todo)
2685 (const tags)
2686 (const search))
2687 (integer :tag "Max number of tagged entries")))))
2689 (defcustom org-agenda-max-effort nil
2690 "Maximum cumulated effort duration for the agenda.
2691 This can be nil (no limit) or a number of minutes (as an integer)
2692 or an alist of agenda types with an associated number of minutes
2693 to limit entries to in this type."
2694 :version "24.4"
2695 :package-version '(Org . "8.0")
2696 :group 'org-agenda-custom-commands
2697 :type '(choice (symbol :tag "No limit" nil)
2698 (integer :tag "Max number of minutes")
2699 (repeat
2700 (cons (choice :tag "Agenda type"
2701 (const agenda)
2702 (const todo)
2703 (const tags)
2704 (const search))
2705 (integer :tag "Max number of minutes")))))
2707 (defvar org-agenda-keep-restricted-file-list nil)
2708 (defvar org-keys nil)
2709 (defvar org-match nil)
2710 ;;;###autoload
2711 (defun org-agenda (&optional arg org-keys restriction)
2712 "Dispatch agenda commands to collect entries to the agenda buffer.
2713 Prompts for a command to execute. Any prefix arg will be passed
2714 on to the selected command. The default selections are:
2716 a Call `org-agenda-list' to display the agenda for current day or week.
2717 t Call `org-todo-list' to display the global todo list.
2718 T Call `org-todo-list' to display the global todo list, select only
2719 entries with a specific TODO keyword (the user gets a prompt).
2720 m Call `org-tags-view' to display headlines with tags matching
2721 a condition (the user is prompted for the condition).
2722 M Like `m', but select only TODO entries, no ordinary headlines.
2723 e Export views to associated files.
2724 s Search entries for keywords.
2725 S Search entries for keywords, only with TODO keywords.
2726 / Multi occur across all agenda files and also files listed
2727 in `org-agenda-text-search-extra-files'.
2728 < Restrict agenda commands to buffer, subtree, or region.
2729 Press several times to get the desired effect.
2730 > Remove a previous restriction.
2731 # List \"stuck\" projects.
2732 ! Configure what \"stuck\" means.
2733 C Configure custom agenda commands.
2735 More commands can be added by configuring the variable
2736 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2737 searches can be pre-defined in this way.
2739 If the current buffer is in Org mode and visiting a file, you can also
2740 first press `<' once to indicate that the agenda should be temporarily
2741 \(until the next use of `\\[org-agenda]') restricted to the current file.
2742 Pressing `<' twice means to restrict to the current subtree or region
2743 \(if active)."
2744 (interactive "P")
2745 (catch 'exit
2746 (let* ((prefix-descriptions nil)
2747 (org-agenda-buffer-name org-agenda-buffer-name)
2748 (org-agenda-window-setup (if (equal (buffer-name)
2749 org-agenda-buffer-name)
2750 'current-window
2751 org-agenda-window-setup))
2752 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2753 (org-agenda-custom-commands
2754 ;; normalize different versions
2755 (delq nil
2756 (mapcar
2757 (lambda (x)
2758 (cond ((stringp (cdr x))
2759 (push x prefix-descriptions)
2760 nil)
2761 ((stringp (nth 1 x)) x)
2762 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2763 (t (cons (car x) (cons "" (cdr x))))))
2764 org-agenda-custom-commands)))
2765 (org-agenda-custom-commands
2766 (org-contextualize-keys
2767 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2768 (buf (current-buffer))
2769 (bfn (buffer-file-name (buffer-base-buffer)))
2770 entry key type org-match lprops ans)
2771 ;; Turn off restriction unless there is an overriding one,
2772 (unless org-agenda-overriding-restriction
2773 (unless org-agenda-keep-restricted-file-list
2774 ;; There is a request to keep the file list in place
2775 (put 'org-agenda-files 'org-restrict nil))
2776 (setq org-agenda-restrict nil)
2777 (move-marker org-agenda-restrict-begin nil)
2778 (move-marker org-agenda-restrict-end nil))
2779 ;; Delete old local properties
2780 (put 'org-agenda-redo-command 'org-lprops nil)
2781 ;; Delete previously set last-arguments
2782 (put 'org-agenda-redo-command 'last-args nil)
2783 ;; Remember where this call originated
2784 (setq org-agenda-last-dispatch-buffer (current-buffer))
2785 (unless org-keys
2786 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2787 org-keys (car ans)
2788 restriction (cdr ans)))
2789 ;; If we have sticky agenda buffers, set a name for the buffer,
2790 ;; depending on the invoking keys. The user may still set this
2791 ;; as a command option, which will overwrite what we do here.
2792 (if org-agenda-sticky
2793 (setq org-agenda-buffer-name
2794 (format "*Org Agenda(%s)*" org-keys)))
2795 ;; Establish the restriction, if any
2796 (when (and (not org-agenda-overriding-restriction) restriction)
2797 (put 'org-agenda-files 'org-restrict (list bfn))
2798 (cond
2799 ((eq restriction 'region)
2800 (setq org-agenda-restrict (current-buffer))
2801 (move-marker org-agenda-restrict-begin (region-beginning))
2802 (move-marker org-agenda-restrict-end (region-end)))
2803 ((eq restriction 'subtree)
2804 (save-excursion
2805 (setq org-agenda-restrict (current-buffer))
2806 (org-back-to-heading t)
2807 (move-marker org-agenda-restrict-begin (point))
2808 (move-marker org-agenda-restrict-end
2809 (progn (org-end-of-subtree t)))))))
2811 ;; For example the todo list should not need it (but does...)
2812 (cond
2813 ((setq entry (assoc org-keys org-agenda-custom-commands))
2814 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2815 (progn
2816 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2817 lprops (nth 4 entry))
2818 (if org-agenda-sticky
2819 (setq org-agenda-buffer-name
2820 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2821 (format "*Org Agenda(%s)*" org-keys))))
2822 (put 'org-agenda-redo-command 'org-lprops lprops)
2823 (cond
2824 ((eq type 'agenda)
2825 (org-let lprops '(org-agenda-list current-prefix-arg)))
2826 ((eq type 'agenda*)
2827 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2828 ((eq type 'alltodo)
2829 (org-let lprops '(org-todo-list current-prefix-arg)))
2830 ((eq type 'search)
2831 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2832 ((eq type 'stuck)
2833 (org-let lprops '(org-agenda-list-stuck-projects
2834 current-prefix-arg)))
2835 ((eq type 'tags)
2836 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2837 ((eq type 'tags-todo)
2838 (org-let lprops '(org-tags-view '(4) org-match)))
2839 ((eq type 'todo)
2840 (org-let lprops '(org-todo-list org-match)))
2841 ((eq type 'tags-tree)
2842 (org-check-for-org-mode)
2843 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2844 ((eq type 'todo-tree)
2845 (org-check-for-org-mode)
2846 (org-let lprops
2847 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2848 (regexp-quote org-match) "\\>"))))
2849 ((eq type 'occur-tree)
2850 (org-check-for-org-mode)
2851 (org-let lprops '(org-occur org-match)))
2852 ((functionp type)
2853 (org-let lprops '(funcall type org-match)))
2854 ((fboundp type)
2855 (org-let lprops '(funcall type org-match)))
2856 (t (user-error "Invalid custom agenda command type %s" type))))
2857 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2858 ((equal org-keys "C")
2859 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2860 (customize-variable 'org-agenda-custom-commands))
2861 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2862 ((equal org-keys "s") (call-interactively 'org-search-view))
2863 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2864 ((equal org-keys "t") (call-interactively 'org-todo-list))
2865 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2866 ((equal org-keys "m") (call-interactively 'org-tags-view))
2867 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2868 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2869 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2870 (add-hook
2871 'post-command-hook
2872 (lambda ()
2873 (unless (current-message)
2874 (let* ((m (org-agenda-get-any-marker))
2875 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2876 (when note
2877 (message (concat
2878 "FLAGGING-NOTE ([?] for more info): "
2879 (org-add-props
2880 (replace-regexp-in-string
2881 "\\\\n" "//"
2882 (copy-sequence note))
2883 nil 'face 'org-warning)))))))
2884 t t))
2885 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2886 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2887 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2888 (t (user-error "Invalid agenda key"))))))
2890 (defvar org-agenda-multi)
2892 (defun org-agenda-append-agenda ()
2893 "Append another agenda view to the current one.
2894 This function allows interactive building of block agendas.
2895 Agenda views are separated by `org-agenda-block-separator'."
2896 (interactive)
2897 (unless (derived-mode-p 'org-agenda-mode)
2898 (user-error "Can only append from within agenda buffer"))
2899 (let ((org-agenda-multi t))
2900 (org-agenda)
2901 (widen)
2902 (org-agenda-finalize)
2903 (setq buffer-read-only t)
2904 (org-agenda-fit-window-to-buffer)))
2906 (defun org-agenda-normalize-custom-commands (cmds)
2907 "Normalize custom commands CMDS."
2908 (delq nil
2909 (mapcar
2910 (lambda (x)
2911 (cond ((stringp (cdr x)) nil)
2912 ((stringp (nth 1 x)) x)
2913 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2914 (t (cons (car x) (cons "" (cdr x))))))
2915 cmds)))
2917 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2918 "The user interface for selecting an agenda command."
2919 (catch 'exit
2920 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2921 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2922 (region-p (org-region-active-p))
2923 (custom org-agenda-custom-commands)
2924 (selstring "")
2925 restriction second-time
2926 c entry key type match prefixes rmheader header-end custom1 desc
2927 line lines left right n n1)
2928 (save-window-excursion
2929 (delete-other-windows)
2930 (org-switch-to-buffer-other-window " *Agenda Commands*")
2931 (erase-buffer)
2932 (insert (eval-when-compile
2933 (let ((header
2934 "Press key for an agenda command:
2935 -------------------------------- < Buffer, subtree/region restriction
2936 a Agenda for current week or day > Remove restriction
2937 t List of all TODO entries e Export agenda views
2938 m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
2939 s Search for keywords M Like m, but only TODO entries
2940 / Multi-occur S Like s, but only TODO entries
2941 ? Find :FLAGGED: entries C Configure custom agenda commands
2942 * Toggle sticky agenda views # List stuck projects (!=configure)
2944 (start 0))
2945 (while (string-match
2946 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2947 header start)
2948 (setq start (match-end 0))
2949 (add-text-properties (match-beginning 2) (match-end 2)
2950 '(face bold) header))
2951 header)))
2952 (setq header-end (point-marker))
2953 (while t
2954 (setq custom1 custom)
2955 (when (eq rmheader t)
2956 (org-goto-line 1)
2957 (re-search-forward ":" nil t)
2958 (delete-region (match-end 0) (point-at-eol))
2959 (forward-char 1)
2960 (looking-at "-+")
2961 (delete-region (match-end 0) (point-at-eol))
2962 (move-marker header-end (match-end 0)))
2963 (goto-char header-end)
2964 (delete-region (point) (point-max))
2966 ;; Produce all the lines that describe custom commands and prefixes
2967 (setq lines nil)
2968 (while (setq entry (pop custom1))
2969 (setq key (car entry) desc (nth 1 entry)
2970 type (nth 2 entry)
2971 match (nth 3 entry))
2972 (if (> (length key) 1)
2973 (cl-pushnew (string-to-char key) prefixes :test #'equal)
2974 (setq line
2975 (format
2976 "%-4s%-14s"
2977 (org-add-props (copy-sequence key)
2978 '(face bold))
2979 (cond
2980 ((string-match "\\S-" desc) desc)
2981 ((eq type 'agenda) "Agenda for current week or day")
2982 ((eq type 'agenda*) "Appointments for current week or day")
2983 ((eq type 'alltodo) "List of all TODO entries")
2984 ((eq type 'search) "Word search")
2985 ((eq type 'stuck) "List of stuck projects")
2986 ((eq type 'todo) "TODO keyword")
2987 ((eq type 'tags) "Tags query")
2988 ((eq type 'tags-todo) "Tags (TODO)")
2989 ((eq type 'tags-tree) "Tags tree")
2990 ((eq type 'todo-tree) "TODO kwd tree")
2991 ((eq type 'occur-tree) "Occur tree")
2992 ((functionp type) (if (symbolp type)
2993 (symbol-name type)
2994 "Lambda expression"))
2995 (t "???"))))
2996 (if org-agenda-menu-show-matcher
2997 (setq line
2998 (concat line ": "
2999 (cond
3000 ((stringp match)
3001 (setq match (copy-sequence match))
3002 (org-add-props match nil 'face 'org-warning))
3003 ((listp type)
3004 (format "set of %d commands" (length type))))))
3005 (if (org-string-nw-p match)
3006 (add-text-properties
3007 0 (length line) (list 'help-echo
3008 (concat "Matcher: " match)) line)))
3009 (push line lines)))
3010 (setq lines (nreverse lines))
3011 (when prefixes
3012 (mapc (lambda (x)
3013 (push
3014 (format "%s %s"
3015 (org-add-props (char-to-string x)
3016 nil 'face 'bold)
3017 (or (cdr (assoc (concat selstring
3018 (char-to-string x))
3019 prefix-descriptions))
3020 "Prefix key"))
3021 lines))
3022 prefixes))
3024 ;; Check if we should display in two columns
3025 (if org-agenda-menu-two-columns
3026 (progn
3027 (setq n (length lines)
3028 n1 (+ (/ n 2) (mod n 2))
3029 right (nthcdr n1 lines)
3030 left (copy-sequence lines))
3031 (setcdr (nthcdr (1- n1) left) nil))
3032 (setq left lines right nil))
3033 (while left
3034 (insert "\n" (pop left))
3035 (when right
3036 (if (< (current-column) 40)
3037 (move-to-column 40 t)
3038 (insert " "))
3039 (insert (pop right))))
3041 ;; Make the window the right size
3042 (goto-char (point-min))
3043 (if second-time
3044 (if (not (pos-visible-in-window-p (point-max)))
3045 (org-fit-window-to-buffer))
3046 (setq second-time t)
3047 (org-fit-window-to-buffer))
3049 ;; Ask for selection
3050 (message "Press key for agenda command%s:"
3051 (if (or restrict-ok org-agenda-overriding-restriction)
3052 (if org-agenda-overriding-restriction
3053 " (restriction lock active)"
3054 (if restriction
3055 (format " (restricted to %s)" restriction)
3056 " (unrestricted)"))
3057 ""))
3058 (setq c (read-char-exclusive))
3059 (message "")
3060 (cond
3061 ((assoc (char-to-string c) custom)
3062 (setq selstring (concat selstring (char-to-string c)))
3063 (throw 'exit (cons selstring restriction)))
3064 ((memq c prefixes)
3065 (setq selstring (concat selstring (char-to-string c))
3066 prefixes nil
3067 rmheader (or rmheader t)
3068 custom (delq nil (mapcar
3069 (lambda (x)
3070 (if (or (= (length (car x)) 1)
3071 (/= (string-to-char (car x)) c))
3073 (cons (substring (car x) 1) (cdr x))))
3074 custom))))
3075 ((eq c ?*)
3076 (call-interactively 'org-toggle-sticky-agenda)
3077 (sit-for 2))
3078 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3079 (message "Restriction is only possible in Org buffers")
3080 (ding) (sit-for 1))
3081 ((eq c ?1)
3082 (org-agenda-remove-restriction-lock 'noupdate)
3083 (setq restriction 'buffer))
3084 ((eq c ?0)
3085 (org-agenda-remove-restriction-lock 'noupdate)
3086 (setq restriction (if region-p 'region 'subtree)))
3087 ((eq c ?<)
3088 (org-agenda-remove-restriction-lock 'noupdate)
3089 (setq restriction
3090 (cond
3091 ((eq restriction 'buffer)
3092 (if region-p 'region 'subtree))
3093 ((memq restriction '(subtree region))
3094 nil)
3095 (t 'buffer))))
3096 ((eq c ?>)
3097 (org-agenda-remove-restriction-lock 'noupdate)
3098 (setq restriction nil))
3099 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3100 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3101 ((and (> (length selstring) 0) (eq c ?\d))
3102 (delete-window)
3103 (org-agenda-get-restriction-and-command prefix-descriptions))
3105 ((equal c ?q) (error "Abort"))
3106 (t (user-error "Invalid key %c" c))))))))
3108 (defun org-agenda-fit-window-to-buffer ()
3109 "Fit the window to the buffer size."
3110 (and (memq org-agenda-window-setup '(reorganize-frame))
3111 (fboundp 'fit-window-to-buffer)
3112 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3113 (= (car org-agenda-window-frame-fractions) 1.0))
3114 (delete-other-windows)
3115 (org-fit-window-to-buffer
3117 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3118 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3120 (defvar org-cmd nil)
3121 (defvar org-agenda-overriding-cmd nil)
3122 (defvar org-agenda-overriding-arguments nil)
3123 (defvar org-agenda-overriding-cmd-arguments nil)
3124 (defun org-agenda-run-series (name series)
3125 "Run agenda NAME as a SERIES of agenda commands."
3126 (org-let (nth 1 series) '(org-agenda-prepare name))
3127 ;; We need to reset agenda markers here, because when constructing a
3128 ;; block agenda, the individual blocks do not do that.
3129 (org-agenda-reset-markers)
3130 (let* ((org-agenda-multi t)
3131 (redo (list 'org-agenda-run-series name (list 'quote series)))
3132 (cmds (car series))
3133 (gprops (nth 1 series))
3134 match ;; The byte compiler incorrectly complains about this. Keep it!
3135 org-cmd type lprops)
3136 (while (setq org-cmd (pop cmds))
3137 (setq type (car org-cmd))
3138 (setq match (eval (nth 1 org-cmd)))
3139 (setq lprops (nth 2 org-cmd))
3140 (let ((org-agenda-overriding-arguments
3141 (if (eq org-agenda-overriding-cmd org-cmd)
3142 (or org-agenda-overriding-arguments
3143 org-agenda-overriding-cmd-arguments))))
3144 (cond
3145 ((eq type 'agenda)
3146 (org-let2 gprops lprops
3147 '(call-interactively 'org-agenda-list)))
3148 ((eq type 'agenda*)
3149 (org-let2 gprops lprops
3150 '(funcall 'org-agenda-list nil nil t)))
3151 ((eq type 'alltodo)
3152 (org-let2 gprops lprops
3153 '(call-interactively 'org-todo-list)))
3154 ((eq type 'search)
3155 (org-let2 gprops lprops
3156 '(org-search-view current-prefix-arg match nil)))
3157 ((eq type 'stuck)
3158 (org-let2 gprops lprops
3159 '(call-interactively 'org-agenda-list-stuck-projects)))
3160 ((eq type 'tags)
3161 (org-let2 gprops lprops
3162 '(org-tags-view current-prefix-arg match)))
3163 ((eq type 'tags-todo)
3164 (org-let2 gprops lprops
3165 '(org-tags-view '(4) match)))
3166 ((eq type 'todo)
3167 (org-let2 gprops lprops
3168 '(org-todo-list match)))
3169 ((fboundp type)
3170 (org-let2 gprops lprops
3171 '(funcall type match)))
3172 (t (error "Invalid type in command series")))))
3173 (widen)
3174 (let ((inhibit-read-only t))
3175 (add-text-properties (point-min) (point-max)
3176 `(org-series t org-series-redo-cmd ,redo)))
3177 (setq org-agenda-redo-command redo)
3178 (goto-char (point-min)))
3179 (org-agenda-fit-window-to-buffer)
3180 (org-let (nth 1 series) '(org-agenda-finalize)))
3182 ;;;###autoload
3183 (defmacro org-batch-agenda (cmd-key &rest parameters)
3184 "Run an agenda command in batch mode and send the result to STDOUT.
3185 If CMD-KEY is a string of length 1, it is used as a key in
3186 `org-agenda-custom-commands' and triggers this command. If it is a
3187 longer string it is used as a tags/todo match string.
3188 Parameters are alternating variable names and values that will be bound
3189 before running the agenda command."
3190 (org-eval-in-environment (org-make-parameter-alist parameters)
3191 (let (org-agenda-sticky)
3192 (if (> (length cmd-key) 1)
3193 (org-tags-view nil cmd-key)
3194 (org-agenda nil cmd-key))))
3195 (set-buffer org-agenda-buffer-name)
3196 (princ (buffer-string)))
3198 (defvar org-agenda-info nil)
3200 ;;;###autoload
3201 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3202 "Run an agenda command in batch mode and send the result to STDOUT.
3203 If CMD-KEY is a string of length 1, it is used as a key in
3204 `org-agenda-custom-commands' and triggers this command. If it is a
3205 longer string it is used as a tags/todo match string.
3206 Parameters are alternating variable names and values that will be bound
3207 before running the agenda command.
3209 The output gives a line for each selected agenda item. Each
3210 item is a list of comma-separated values, like this:
3212 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3214 category The category of the item
3215 head The headline, without TODO kwd, TAGS and PRIORITY
3216 type The type of the agenda entry, can be
3217 todo selected in TODO match
3218 tagsmatch selected in tags match
3219 diary imported from diary
3220 deadline a deadline on given date
3221 scheduled scheduled on given date
3222 timestamp entry has timestamp on given date
3223 closed entry was closed on given date
3224 upcoming-deadline warning about deadline
3225 past-scheduled forwarded scheduled item
3226 block entry has date block including g. date
3227 todo The todo keyword, if any
3228 tags All tags including inherited ones, separated by colons
3229 date The relevant date, like 2007-2-14
3230 time The time, like 15:00-16:50
3231 extra Sting with extra planning info
3232 priority-l The priority letter if any was given
3233 priority-n The computed numerical priority
3234 agenda-day The day in the agenda where this is listed"
3235 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3236 (org-make-parameter-alist parameters))
3237 (if (> (length cmd-key) 2)
3238 (org-tags-view nil cmd-key)
3239 (org-agenda nil cmd-key)))
3240 (set-buffer org-agenda-buffer-name)
3241 (let* ((lines (org-split-string (buffer-string) "\n"))
3242 line)
3243 (while (setq line (pop lines))
3244 (catch 'next
3245 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3246 (setq org-agenda-info
3247 (org-fix-agenda-info (text-properties-at 0 line)))
3248 (princ
3249 (mapconcat 'org-agenda-export-csv-mapper
3250 '(org-category txt type todo tags date time extra
3251 priority-letter priority agenda-day)
3252 ","))
3253 (princ "\n")))))
3255 (defun org-fix-agenda-info (props)
3256 "Make sure all properties on an agenda item have a canonical form.
3257 This ensures the export commands can easily use it."
3258 (let (tmp re)
3259 (when (setq tmp (plist-get props 'tags))
3260 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3261 (when (setq tmp (plist-get props 'date))
3262 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3263 (let ((calendar-date-display-form '(year "-" month "-" day)))
3264 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3266 (setq tmp (calendar-date-string tmp)))
3267 (setq props (plist-put props 'date tmp)))
3268 (when (setq tmp (plist-get props 'day))
3269 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3270 (let ((calendar-date-display-form '(year "-" month "-" day)))
3271 (setq tmp (calendar-date-string tmp)))
3272 (setq props (plist-put props 'day tmp))
3273 (setq props (plist-put props 'agenda-day tmp)))
3274 (when (setq tmp (plist-get props 'txt))
3275 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3276 (plist-put props 'priority-letter (match-string 1 tmp))
3277 (setq tmp (replace-match "" t t tmp)))
3278 (when (and (setq re (plist-get props 'org-todo-regexp))
3279 (setq re (concat "\\`\\.*" re " ?"))
3280 (let ((case-fold-search nil)) (string-match re tmp)))
3281 (plist-put props 'todo (match-string 1 tmp))
3282 (setq tmp (replace-match "" t t tmp)))
3283 (plist-put props 'txt tmp)))
3284 props)
3286 (defun org-agenda-export-csv-mapper (prop)
3287 (let ((res (plist-get org-agenda-info prop)))
3288 (setq res
3289 (cond
3290 ((not res) "")
3291 ((stringp res) res)
3292 (t (prin1-to-string res))))
3293 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3295 ;;;###autoload
3296 (defun org-store-agenda-views (&rest parameters)
3297 "Store agenda views."
3298 (interactive)
3299 (eval (list 'org-batch-store-agenda-views)))
3301 ;;;###autoload
3302 (defmacro org-batch-store-agenda-views (&rest parameters)
3303 "Run all custom agenda commands that have a file argument."
3304 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3305 (pop-up-frames nil)
3306 (dir default-directory)
3307 (pars (org-make-parameter-alist parameters))
3308 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3309 (save-window-excursion
3310 (while cmds
3311 (setq cmd (pop cmds)
3312 thiscmdkey (car cmd)
3313 thiscmdcmd (cdr cmd)
3314 match (nth 2 thiscmdcmd)
3315 bufname (if org-agenda-sticky
3316 (or (and (stringp match)
3317 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3318 (format "*Org Agenda(%s)*" thiscmdkey))
3319 org-agenda-buffer-name)
3320 cmd-or-set (nth 2 cmd)
3321 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3322 files (nth (if (listp cmd-or-set) 4 5) cmd))
3323 (if (stringp files) (setq files (list files)))
3324 (when files
3325 (org-eval-in-environment (append org-agenda-exporter-settings
3326 opts pars)
3327 (org-agenda nil thiscmdkey))
3328 (set-buffer bufname)
3329 (while files
3330 (org-eval-in-environment (append org-agenda-exporter-settings
3331 opts pars)
3332 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3333 (and (get-buffer bufname)
3334 (kill-buffer bufname)))))))
3336 (defvar org-agenda-current-span nil
3337 "The current span used in the agenda view.") ; local variable in the agenda buffer
3338 (defun org-agenda-mark-header-line (pos)
3339 "Mark the line at POS as an agenda structure header."
3340 (save-excursion
3341 (goto-char pos)
3342 (put-text-property (point-at-bol) (point-at-eol)
3343 'org-agenda-structural-header t)
3344 (when org-agenda-title-append
3345 (put-text-property (point-at-bol) (point-at-eol)
3346 'org-agenda-title-append org-agenda-title-append))))
3348 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3349 (defvar org-agenda-write-buffer-name "Agenda View")
3350 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3351 "Write the current buffer (an agenda view) as a file.
3353 Depending on the extension of the file name, plain text (.txt),
3354 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3355 If the extension is .ics, translate visible agenda into iCalendar
3356 format. If the extension is .org, collect all subtrees
3357 corresponding to the agenda entries and add them in an .org file.
3359 With prefix argument OPEN, open the new file immediately. If
3360 NOSETTINGS is given, do not scope the settings of
3361 `org-agenda-exporter-settings' into the export commands. This is
3362 used when the settings have already been scoped and we do not
3363 wish to overrule other, higher priority settings. If
3364 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3365 the agenda to write."
3366 (interactive "FWrite agenda to file: \nP")
3367 (if (or (not (file-writable-p file))
3368 (and (file-exists-p file)
3369 (if (called-interactively-p 'any)
3370 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3371 (user-error "Cannot write agenda to file %s" file))
3372 (org-let (if nosettings nil org-agenda-exporter-settings)
3373 '(save-excursion
3374 (save-window-excursion
3375 (let ((bs (copy-sequence (buffer-string)))
3376 (extension (file-name-extension file))
3377 (default-directory (file-name-directory file))
3378 beg content)
3379 (with-temp-buffer
3380 (rename-buffer org-agenda-write-buffer-name t)
3381 (set-buffer-modified-p nil)
3382 (insert bs)
3383 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3384 (run-hooks 'org-agenda-before-write-hook)
3385 (cond
3386 ((bound-and-true-p org-mobile-creating-agendas)
3387 (org-mobile-write-agenda-for-mobile file))
3388 ((string= "org" extension)
3389 (let (content p m message-log-max)
3390 (goto-char (point-min))
3391 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3392 (goto-char p)
3393 (setq m (get-text-property (point) 'org-hd-marker))
3394 (when m
3395 (push (save-excursion
3396 (set-buffer (marker-buffer m))
3397 (goto-char m)
3398 (org-copy-subtree 1 nil t t)
3399 org-subtree-clip)
3400 content)))
3401 (find-file file)
3402 (erase-buffer)
3403 (dolist (s content) (org-paste-subtree 1 s))
3404 (write-file file)
3405 (kill-buffer (current-buffer))
3406 (message "Org file written to %s" file)))
3407 ((member extension '("html" "htm"))
3408 (or (require 'htmlize nil t)
3409 (error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
3410 (set-buffer (htmlize-buffer (current-buffer)))
3411 (when org-agenda-export-html-style
3412 ;; replace <style> section with org-agenda-export-html-style
3413 (goto-char (point-min))
3414 (kill-region (- (search-forward "<style") 6)
3415 (search-forward "</style>"))
3416 (insert org-agenda-export-html-style))
3417 (write-file file)
3418 (kill-buffer (current-buffer))
3419 (message "HTML written to %s" file))
3420 ((string= "ps" extension)
3421 (require 'ps-print)
3422 (ps-print-buffer-with-faces file)
3423 (message "Postscript written to %s" file))
3424 ((string= "pdf" extension)
3425 (require 'ps-print)
3426 (ps-print-buffer-with-faces
3427 (concat (file-name-sans-extension file) ".ps"))
3428 (call-process "ps2pdf" nil nil nil
3429 (expand-file-name
3430 (concat (file-name-sans-extension file) ".ps"))
3431 (expand-file-name file))
3432 (delete-file (concat (file-name-sans-extension file) ".ps"))
3433 (message "PDF written to %s" file))
3434 ((string= "ics" extension)
3435 (require 'ox-icalendar)
3436 (org-icalendar-export-current-agenda (expand-file-name file)))
3438 (let ((bs (buffer-string)))
3439 (find-file file)
3440 (erase-buffer)
3441 (insert bs)
3442 (save-buffer 0)
3443 (kill-buffer (current-buffer))
3444 (message "Plain text written to %s" file))))))))
3445 (set-buffer (or agenda-bufname
3446 (and (called-interactively-p 'any) (buffer-name))
3447 org-agenda-buffer-name)))
3448 (when open (org-open-file file)))
3450 (defun org-agenda-remove-marked-text (property &optional value)
3451 "Delete all text marked with VALUE of PROPERTY.
3452 VALUE defaults to t."
3453 (let (beg)
3454 (setq value (or value t))
3455 (while (setq beg (text-property-any (point-min) (point-max)
3456 property value))
3457 (delete-region
3458 beg (or (next-single-property-change beg property)
3459 (point-max))))))
3461 (defun org-agenda-add-entry-text ()
3462 "Add entry text to agenda lines.
3463 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3464 entry text following headings shown in the agenda.
3465 Drawers will be excluded, also the line with scheduling/deadline info."
3466 (when (and (> org-agenda-add-entry-text-maxlines 0)
3467 (not (bound-and-true-p org-mobile-creating-agendas)))
3468 (let (m txt)
3469 (goto-char (point-min))
3470 (while (not (eobp))
3471 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3472 (beginning-of-line 2)
3473 (setq txt (org-agenda-get-some-entry-text
3474 m org-agenda-add-entry-text-maxlines " > "))
3475 (end-of-line 1)
3476 (if (string-match "\\S-" txt)
3477 (insert "\n" txt)
3478 (or (eobp) (forward-char 1))))))))
3480 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3481 &rest keep)
3482 "Extract entry text from MARKER, at most N-LINES lines.
3483 This will ignore drawers etc, just get the text.
3484 If INDENT is given, prefix every line with this string. If KEEP is
3485 given, it is a list of symbols, defining stuff that should not be
3486 removed from the entry content. Currently only `planning' is allowed here."
3487 (let (txt drawer-re kwd-time-re ind)
3488 (save-excursion
3489 (with-current-buffer (marker-buffer marker)
3490 (if (not (derived-mode-p 'org-mode))
3491 (setq txt "")
3492 (org-with-wide-buffer
3493 (goto-char marker)
3494 (end-of-line 1)
3495 (setq txt (buffer-substring
3496 (min (1+ (point)) (point-max))
3497 (progn (outline-next-heading) (point)))
3498 drawer-re org-drawer-regexp
3499 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3500 ".*\n?"))
3501 (with-temp-buffer
3502 (insert txt)
3503 (when org-agenda-add-entry-text-descriptive-links
3504 (goto-char (point-min))
3505 (while (org-activate-links (point-max))
3506 (add-text-properties (match-beginning 0) (match-end 0)
3507 '(face org-link))))
3508 (goto-char (point-min))
3509 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3510 (set-text-properties (match-beginning 0) (match-end 0)
3511 nil))
3512 (goto-char (point-min))
3513 (while (re-search-forward drawer-re nil t)
3514 (delete-region
3515 (match-beginning 0)
3516 (progn (re-search-forward
3517 "^[ \t]*:END:.*\n?" nil 'move)
3518 (point))))
3519 (unless (member 'planning keep)
3520 (goto-char (point-min))
3521 (while (re-search-forward kwd-time-re nil t)
3522 (replace-match "")))
3523 (goto-char (point-min))
3524 (when org-agenda-entry-text-exclude-regexps
3525 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3526 (while (setq re (pop re-list))
3527 (goto-char (point-min))
3528 (while (re-search-forward re nil t)
3529 (replace-match "")))))
3530 (goto-char (point-max))
3531 (skip-chars-backward " \t\n")
3532 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3534 ;; find and remove min common indentation
3535 (goto-char (point-min))
3536 (untabify (point-min) (point-max))
3537 (setq ind (org-get-indentation))
3538 (while (not (eobp))
3539 (unless (looking-at "[ \t]*$")
3540 (setq ind (min ind (org-get-indentation))))
3541 (beginning-of-line 2))
3542 (goto-char (point-min))
3543 (while (not (eobp))
3544 (unless (looking-at "[ \t]*$")
3545 (move-to-column ind)
3546 (delete-region (point-at-bol) (point)))
3547 (beginning-of-line 2))
3549 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3551 (goto-char (point-min))
3552 (when indent
3553 (while (and (not (eobp)) (re-search-forward "^" nil t))
3554 (replace-match indent t t)))
3555 (goto-char (point-min))
3556 (while (looking-at "[ \t]*\n") (replace-match ""))
3557 (goto-char (point-max))
3558 (when (> (org-current-line)
3559 n-lines)
3560 (org-goto-line (1+ n-lines))
3561 (backward-char 1))
3562 (setq txt (buffer-substring (point-min) (point))))))))
3563 txt))
3565 (defun org-check-for-org-mode ()
3566 "Make sure current buffer is in org-mode. Error if not."
3567 (or (derived-mode-p 'org-mode)
3568 (error "Cannot execute Org agenda command on buffer in %s"
3569 major-mode)))
3571 ;;; Agenda prepare and finalize
3573 (defvar org-agenda-multi nil) ; dynamically scoped
3574 (defvar org-agenda-pre-window-conf nil)
3575 (defvar org-agenda-columns-active nil)
3576 (defvar org-agenda-name nil)
3577 (defvar org-agenda-tag-filter nil)
3578 (defvar org-agenda-category-filter nil)
3579 (defvar org-agenda-regexp-filter nil)
3580 (defvar org-agenda-effort-filter nil)
3581 (defvar org-agenda-top-headline-filter nil)
3582 (defvar org-agenda-tag-filter-preset nil
3583 "A preset of the tags filter used for secondary agenda filtering.
3584 This must be a list of strings, each string must be a single tag preceded
3585 by \"+\" or \"-\".
3586 This variable should not be set directly, but agenda custom commands can
3587 bind it in the options section. The preset filter is a global property of
3588 the entire agenda view. In a block agenda, it will not work reliably to
3589 define a filter for one of the individual blocks. You need to set it in
3590 the global options and expect it to be applied to the entire view.")
3592 (defvar org-agenda-category-filter-preset nil
3593 "A preset of the category filter used for secondary agenda filtering.
3594 This must be a list of strings, each string must be a single category
3595 preceded by \"+\" or \"-\".
3596 This variable should not be set directly, but agenda custom commands can
3597 bind it in the options section. The preset filter is a global property of
3598 the entire agenda view. In a block agenda, it will not work reliably to
3599 define a filter for one of the individual blocks. You need to set it in
3600 the global options and expect it to be applied to the entire view.")
3602 (defvar org-agenda-regexp-filter-preset nil
3603 "A preset of the regexp filter used for secondary agenda filtering.
3604 This must be a list of strings, each string must be a single regexp
3605 preceded by \"+\" or \"-\".
3606 This variable should not be set directly, but agenda custom commands can
3607 bind it in the options section. The preset filter is a global property of
3608 the entire agenda view. In a block agenda, it will not work reliably to
3609 define a filter for one of the individual blocks. You need to set it in
3610 the global options and expect it to be applied to the entire view.")
3612 (defvar org-agenda-effort-filter-preset nil
3613 "A preset of the effort condition used for secondary agenda filtering.
3614 This must be a list of strings, each string must be a single regexp
3615 preceded by \"+\" or \"-\".
3616 This variable should not be set directly, but agenda custom commands can
3617 bind it in the options section. The preset filter is a global property of
3618 the entire agenda view. In a block agenda, it will not work reliably to
3619 define a filter for one of the individual blocks. You need to set it in
3620 the global options and expect it to be applied to the entire view.")
3622 (defun org-agenda-use-sticky-p ()
3623 "Return non-nil if an agenda buffer named
3624 `org-agenda-buffer-name' exists and should be shown instead of
3625 generating a new one."
3626 (and
3627 ;; turned off by user
3628 org-agenda-sticky
3629 ;; For multi-agenda buffer already exists
3630 (not org-agenda-multi)
3631 ;; buffer found
3632 (get-buffer org-agenda-buffer-name)
3633 ;; C-u parameter is same as last call
3634 (with-current-buffer (get-buffer org-agenda-buffer-name)
3635 (and
3636 (equal current-prefix-arg
3637 org-agenda-last-prefix-arg)
3638 ;; In case user turned stickiness on, while having existing
3639 ;; Agenda buffer active, don't reuse that buffer, because it
3640 ;; does not have org variables local
3641 org-agenda-this-buffer-is-sticky))))
3643 (defun org-agenda-prepare-window (abuf filter-alist)
3644 "Setup agenda buffer in the window.
3645 ABUF is the buffer for the agenda window.
3646 FILTER-ALIST is an alist of filters we need to apply when
3647 `org-agenda-persistent-filter' is non-nil."
3648 (let* ((awin (get-buffer-window abuf)) wconf)
3649 (cond
3650 ((equal (current-buffer) abuf) nil)
3651 (awin (select-window awin))
3652 ((not (setq wconf (current-window-configuration))))
3653 ((eq org-agenda-window-setup 'current-window)
3654 (pop-to-buffer-same-window abuf))
3655 ((eq org-agenda-window-setup 'other-window)
3656 (org-switch-to-buffer-other-window abuf))
3657 ((eq org-agenda-window-setup 'other-frame)
3658 (switch-to-buffer-other-frame abuf))
3659 ((eq org-agenda-window-setup 'only-window)
3660 (delete-other-windows)
3661 (pop-to-buffer-same-window abuf))
3662 ((eq org-agenda-window-setup 'reorganize-frame)
3663 (delete-other-windows)
3664 (org-switch-to-buffer-other-window abuf)))
3665 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3666 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3667 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3668 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3669 ;; Additional test in case agenda is invoked from within agenda
3670 ;; buffer via elisp link.
3671 (unless (equal (current-buffer) abuf)
3672 (pop-to-buffer-same-window abuf))
3673 (setq org-agenda-pre-window-conf
3674 (or wconf org-agenda-pre-window-conf))))
3676 (defun org-agenda-prepare (&optional name)
3677 (let ((filter-alist (if org-agenda-persistent-filter
3678 (with-current-buffer
3679 (get-buffer-create org-agenda-buffer-name)
3680 (list `(tag . ,org-agenda-tag-filter)
3681 `(re . ,org-agenda-regexp-filter)
3682 `(effort . ,org-agenda-effort-filter)
3683 `(cat . ,org-agenda-category-filter))))))
3684 (if (org-agenda-use-sticky-p)
3685 (progn
3686 (put 'org-agenda-tag-filter :preset-filter nil)
3687 (put 'org-agenda-category-filter :preset-filter nil)
3688 (put 'org-agenda-regexp-filter :preset-filter nil)
3689 ;; Popup existing buffer
3690 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3691 filter-alist)
3692 (message "Sticky Agenda buffer, use `r' to refresh")
3693 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3694 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3695 (setq org-todo-keywords-for-agenda nil)
3696 (put 'org-agenda-tag-filter :preset-filter
3697 org-agenda-tag-filter-preset)
3698 (put 'org-agenda-category-filter :preset-filter
3699 org-agenda-category-filter-preset)
3700 (put 'org-agenda-regexp-filter :preset-filter
3701 org-agenda-regexp-filter-preset)
3702 (put 'org-agenda-effort-filter :preset-filter
3703 org-agenda-effort-filter-preset)
3704 (if org-agenda-multi
3705 (progn
3706 (setq buffer-read-only nil)
3707 (goto-char (point-max))
3708 (unless (or (bobp) org-agenda-compact-blocks
3709 (not org-agenda-block-separator))
3710 (insert "\n"
3711 (if (stringp org-agenda-block-separator)
3712 org-agenda-block-separator
3713 (make-string (window-width) org-agenda-block-separator))
3714 "\n"))
3715 (narrow-to-region (point) (point-max)))
3716 (setq org-done-keywords-for-agenda nil)
3717 ;; Setting any org variables that are in org-agenda-local-vars
3718 ;; list need to be done after the prepare call
3719 (org-agenda-prepare-window
3720 (get-buffer-create org-agenda-buffer-name) filter-alist)
3721 (setq buffer-read-only nil)
3722 (org-agenda-reset-markers)
3723 (let ((inhibit-read-only t)) (erase-buffer))
3724 (org-agenda-mode)
3725 (setq org-agenda-buffer (current-buffer))
3726 (setq org-agenda-contributing-files nil)
3727 (setq org-agenda-columns-active nil)
3728 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3729 (setq org-todo-keywords-for-agenda
3730 (org-uniquify org-todo-keywords-for-agenda))
3731 (setq org-done-keywords-for-agenda
3732 (org-uniquify org-done-keywords-for-agenda))
3733 (setq org-agenda-last-prefix-arg current-prefix-arg)
3734 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3735 (and name (not org-agenda-name)
3736 (setq-local org-agenda-name name)))
3737 (setq buffer-read-only nil))))
3739 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3740 (defun org-agenda-finalize ()
3741 "Finishing touch for the agenda buffer, called just before displaying it."
3742 (unless org-agenda-multi
3743 (save-excursion
3744 (let ((inhibit-read-only t))
3745 (goto-char (point-min))
3746 (save-excursion
3747 (while (org-activate-links (point-max))
3748 (add-text-properties (match-beginning 0) (match-end 0)
3749 '(face org-link))))
3750 (unless (eq org-agenda-remove-tags t)
3751 (org-agenda-align-tags))
3752 (unless org-agenda-with-colors
3753 (remove-text-properties (point-min) (point-max) '(face nil)))
3754 (if (and (boundp 'org-agenda-overriding-columns-format)
3755 org-agenda-overriding-columns-format)
3756 (setq-local org-agenda-overriding-columns-format
3757 org-agenda-overriding-columns-format))
3758 (if (and (boundp 'org-agenda-view-columns-initially)
3759 org-agenda-view-columns-initially)
3760 (org-agenda-columns))
3761 (when org-agenda-fontify-priorities
3762 (org-agenda-fontify-priorities))
3763 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3764 (org-agenda-dim-blocked-tasks))
3765 (org-agenda-mark-clocking-task)
3766 (when org-agenda-entry-text-mode
3767 (org-agenda-entry-text-hide)
3768 (org-agenda-entry-text-show))
3769 (if (and (functionp 'org-habit-insert-consistency-graphs)
3770 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3771 (org-habit-insert-consistency-graphs))
3772 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3773 (unless (or (eq org-agenda-show-inherited-tags 'always)
3774 (and (listp org-agenda-show-inherited-tags)
3775 (memq org-agenda-type org-agenda-show-inherited-tags))
3776 (and (eq org-agenda-show-inherited-tags t)
3777 (or (eq org-agenda-use-tag-inheritance t)
3778 (and (listp org-agenda-use-tag-inheritance)
3779 (not (memq org-agenda-type
3780 org-agenda-use-tag-inheritance))))))
3781 (let (mrk)
3782 (save-excursion
3783 (goto-char (point-min))
3784 (while (equal (forward-line) 0)
3785 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3786 (put-text-property (point-at-bol) (point-at-eol)
3787 'tags (org-with-point-at mrk
3788 (delete-dups
3789 (mapcar 'downcase (org-get-tags-at))))))))))
3790 (run-hooks 'org-agenda-finalize-hook)
3791 (when org-agenda-top-headline-filter
3792 (org-agenda-filter-top-headline-apply
3793 org-agenda-top-headline-filter))
3794 (when org-agenda-tag-filter
3795 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3796 (when (get 'org-agenda-tag-filter :preset-filter)
3797 (org-agenda-filter-apply
3798 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3799 (when org-agenda-category-filter
3800 (org-agenda-filter-apply org-agenda-category-filter 'category))
3801 (when (get 'org-agenda-category-filter :preset-filter)
3802 (org-agenda-filter-apply
3803 (get 'org-agenda-category-filter :preset-filter) 'category))
3804 (when org-agenda-regexp-filter
3805 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3806 (when (get 'org-agenda-regexp-filter :preset-filter)
3807 (org-agenda-filter-apply
3808 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3809 (when org-agenda-effort-filter
3810 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3811 (when (get 'org-agenda-effort-filter :preset-filter)
3812 (org-agenda-filter-apply
3813 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3814 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3816 (defun org-agenda-mark-clocking-task ()
3817 "Mark the current clock entry in the agenda if it is present."
3818 ;; We need to widen when `org-agenda-finalize' is called from
3819 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3820 (when (bound-and-true-p org-clock-current-task)
3821 (save-restriction
3822 (widen)
3823 (org-agenda-unmark-clocking-task)
3824 (when (marker-buffer org-clock-hd-marker)
3825 (save-excursion
3826 (goto-char (point-min))
3827 (let (s ov)
3828 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3829 (goto-char s)
3830 (when (equal (org-get-at-bol 'org-hd-marker)
3831 org-clock-hd-marker)
3832 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3833 (overlay-put ov 'type 'org-agenda-clocking)
3834 (overlay-put ov 'face 'org-agenda-clocking)
3835 (overlay-put ov 'help-echo
3836 "The clock is running in this item")))))))))
3838 (defun org-agenda-unmark-clocking-task ()
3839 "Unmark the current clocking task."
3840 (mapc (lambda (o)
3841 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3842 (delete-overlay o)))
3843 (overlays-in (point-min) (point-max))))
3845 (defun org-agenda-fontify-priorities ()
3846 "Make highest priority lines bold, and lowest italic."
3847 (interactive)
3848 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3849 (delete-overlay o)))
3850 (overlays-in (point-min) (point-max)))
3851 (save-excursion
3852 (let (b e p ov h l)
3853 (goto-char (point-min))
3854 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3855 (setq h (or (get-char-property (point) 'org-highest-priority)
3856 org-highest-priority)
3857 l (or (get-char-property (point) 'org-lowest-priority)
3858 org-lowest-priority)
3859 p (string-to-char (match-string 1))
3860 b (match-beginning 0)
3861 e (if (eq org-agenda-fontify-priorities 'cookies)
3862 (match-end 0)
3863 (point-at-eol))
3864 ov (make-overlay b e))
3865 (overlay-put
3866 ov 'face
3867 (let ((special-face
3868 (cond ((org-face-from-face-or-color
3869 'priority 'org-priority
3870 (cdr (assoc p org-priority-faces))))
3871 ((and (listp org-agenda-fontify-priorities)
3872 (org-face-from-face-or-color
3873 'priority 'org-priority
3874 (cdr (assoc p org-agenda-fontify-priorities)))))
3875 ((equal p l) 'italic)
3876 ((equal p h) 'bold))))
3877 (if special-face (list special-face 'org-priority) 'org-priority)))
3878 (overlay-put ov 'org-type 'org-priority)))))
3880 (defvar org-depend-tag-blocked)
3882 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3883 "Dim currently blocked TODOs in the agenda display.
3884 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3885 dimming them."
3886 (interactive "P")
3887 (when (called-interactively-p 'interactive)
3888 (message "Dim or hide blocked tasks..."))
3889 (dolist (o (overlays-in (point-min) (point-max)))
3890 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3891 (delete-overlay o)))
3892 (save-excursion
3893 (let ((inhibit-read-only t))
3894 (goto-char (point-min))
3895 (while (let ((pos (text-property-not-all
3896 (point) (point-max) 'org-todo-blocked nil)))
3897 (when pos (goto-char pos)))
3898 (let* ((invisible (eq (org-get-at-bol 'org-todo-blocked) 'invisible))
3899 (ov (make-overlay (if invisible
3900 (line-end-position 0)
3901 (line-beginning-position))
3902 (line-end-position))))
3903 (if invisible
3904 (overlay-put ov 'invisible t)
3905 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3906 (overlay-put ov 'org-type 'org-blocked-todo))
3907 (forward-line))))
3908 (when (called-interactively-p 'interactive)
3909 (message "Dim or hide blocked tasks...done")))
3911 (defun org-agenda--mark-blocked-entry (entry)
3912 "For ENTRY a string with the text property `org-hd-marker', if
3913 the header at `org-hd-marker' is blocked according to
3914 `org-entry-blocked-p', then if `org-agenda-dim-blocked-tasks' is
3915 'invisible and the header is not blocked by checkboxes, set the
3916 text property `org-todo-blocked' to 'invisible, otherwise set it
3917 to t."
3918 (when (get-text-property 0 'todo-state entry)
3919 (let ((entry-marker (get-text-property 0 'org-hd-marker entry))
3920 (org-blocked-by-checkboxes nil)
3921 ;; Necessary so that `org-entry-blocked-p' does not change
3922 ;; the buffer.
3923 (org-depend-tag-blocked nil))
3924 (when entry-marker
3925 (let ((blocked
3926 (with-current-buffer (marker-buffer entry-marker)
3927 (save-excursion
3928 (goto-char entry-marker)
3929 (org-entry-blocked-p)))))
3930 (when blocked
3931 (let ((really-invisible
3932 (and (not org-blocked-by-checkboxes)
3933 (eq org-agenda-dim-blocked-tasks 'invisible))))
3934 (put-text-property
3935 0 (length entry) 'org-todo-blocked
3936 (if really-invisible 'invisible t)
3937 entry)))))))
3938 entry)
3940 (defvar org-agenda-skip-function nil
3941 "Function to be called at each match during agenda construction.
3942 If this function returns nil, the current match should not be skipped.
3943 Otherwise, the function must return a position from where the search
3944 should be continued.
3945 This may also be a Lisp form, it will be evaluated.
3946 Never set this variable using `setq' or so, because then it will apply
3947 to all future agenda commands. If you do want a global skipping condition,
3948 use the option `org-agenda-skip-function-global' instead.
3949 The correct usage for `org-agenda-skip-function' is to bind it with
3950 `let' to scope it dynamically into the agenda-constructing command.
3951 A good way to set it is through options in `org-agenda-custom-commands'.")
3953 (defun org-agenda-skip ()
3954 "Throw to `:skip' in places that should be skipped.
3955 Also moves point to the end of the skipped region, so that search can
3956 continue from there."
3957 (let ((p (point-at-bol)) to)
3958 (when (or
3959 (save-excursion (goto-char p) (looking-at comment-start-skip))
3960 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3961 (get-text-property p :org-archived)
3962 (org-end-of-subtree t))
3963 (and org-agenda-skip-comment-trees
3964 (get-text-property p :org-comment)
3965 (org-end-of-subtree t))
3966 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3967 (org-agenda-skip-eval org-agenda-skip-function)))
3968 (goto-char to))
3969 (org-in-src-block-p t))
3970 (throw :skip t))))
3972 (defun org-agenda-skip-eval (form)
3973 "If FORM is a function or a list, call (or eval) it and return the result.
3974 `save-excursion' and `save-match-data' are wrapped around the call, so point
3975 and match data are returned to the previous state no matter what these
3976 functions do."
3977 (let (fp)
3978 (and form
3979 (or (setq fp (functionp form))
3980 (consp form))
3981 (save-excursion
3982 (save-match-data
3983 (if fp
3984 (funcall form)
3985 (eval form)))))))
3987 (defvar org-agenda-markers nil
3988 "List of all currently active markers created by `org-agenda'.")
3989 (defvar org-agenda-last-marker-time (float-time)
3990 "Creation time of the last agenda marker.")
3992 (defun org-agenda-new-marker (&optional pos)
3993 "Return a new agenda marker.
3994 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3995 these markers and resets them when they are no longer in use."
3996 (let ((m (copy-marker (or pos (point)) t)))
3997 (setq org-agenda-last-marker-time (float-time))
3998 (if org-agenda-buffer
3999 (with-current-buffer org-agenda-buffer
4000 (push m org-agenda-markers))
4001 (push m org-agenda-markers))
4004 (defun org-agenda-reset-markers ()
4005 "Reset markers created by `org-agenda'."
4006 (while org-agenda-markers
4007 (move-marker (pop org-agenda-markers) nil)))
4009 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
4010 "Save relative positions of markers in region.
4011 This check for agenda markers in all agenda buffers currently active."
4012 (dolist (buf (buffer-list))
4013 (with-current-buffer buf
4014 (when (eq major-mode 'org-agenda-mode)
4015 (mapc (lambda (m) (org-check-and-save-marker m beg end))
4016 org-agenda-markers)))))
4018 ;;; Entry text mode
4020 (defun org-agenda-entry-text-show-here ()
4021 "Add some text from the entry as context to the current line."
4022 (let (m txt o)
4023 (setq m (org-get-at-bol 'org-hd-marker))
4024 (unless (marker-buffer m)
4025 (error "No marker points to an entry here"))
4026 (setq txt (concat "\n" (org-no-properties
4027 (org-agenda-get-some-entry-text
4028 m org-agenda-entry-text-maxlines
4029 org-agenda-entry-text-leaders))))
4030 (when (string-match "\\S-" txt)
4031 (setq o (make-overlay (point-at-bol) (point-at-eol)))
4032 (overlay-put o 'evaporate t)
4033 (overlay-put o 'org-overlay-type 'agenda-entry-content)
4034 (overlay-put o 'after-string txt))))
4036 (defun org-agenda-entry-text-show ()
4037 "Add entry context for all agenda lines."
4038 (interactive)
4039 (save-excursion
4040 (goto-char (point-max))
4041 (beginning-of-line 1)
4042 (while (not (bobp))
4043 (when (org-get-at-bol 'org-hd-marker)
4044 (org-agenda-entry-text-show-here))
4045 (beginning-of-line 0))))
4047 (defun org-agenda-entry-text-hide ()
4048 "Remove any shown entry context."
4049 (delq nil
4050 (mapcar (lambda (o)
4051 (if (eq (overlay-get o 'org-overlay-type)
4052 'agenda-entry-content)
4053 (progn (delete-overlay o) t)))
4054 (overlays-in (point-min) (point-max)))))
4056 (defun org-agenda-get-day-face (date)
4057 "Return the face DATE should be displayed with."
4058 (cond ((and (functionp org-agenda-day-face-function)
4059 (funcall org-agenda-day-face-function date)))
4060 ((org-agenda-today-p date) 'org-agenda-date-today)
4061 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4062 'org-agenda-date-weekend)
4063 (t 'org-agenda-date)))
4065 (defvar org-agenda-show-log-scoped)
4067 ;;; Agenda Daily/Weekly
4069 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4070 "Start day for the agenda view.
4071 Custom commands can set this variable in the options section.
4072 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4073 input allowed when reading a date through the Org calendar.
4074 See the docstring of `org-read-date' for details.")
4075 (defvar org-starting-day nil) ; local variable in the agenda buffer
4076 (defvar org-arg-loc nil) ; local variable
4078 (defvar org-agenda-buffer-tmp-name nil)
4079 ;;;###autoload
4080 (defun org-agenda-list (&optional arg start-day span with-hour)
4081 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4082 The view will be for the current day or week, but from the overview buffer
4083 you will be able to go to other days/weeks.
4085 With a numeric prefix argument in an interactive call, the agenda will
4086 span ARG days. Lisp programs should instead specify SPAN to change
4087 the number of days. SPAN defaults to `org-agenda-span'.
4089 START-DAY defaults to TODAY, or to the most recent match for the weekday
4090 given in `org-agenda-start-on-weekday'.
4092 When WITH-HOUR is non-nil, only include scheduled and deadline
4093 items if they have an hour specification like [h]h:mm."
4094 (interactive "P")
4095 (if org-agenda-overriding-arguments
4096 (setq arg (car org-agenda-overriding-arguments)
4097 start-day (nth 1 org-agenda-overriding-arguments)
4098 span (nth 2 org-agenda-overriding-arguments)))
4099 (if (and (integerp arg) (> arg 0))
4100 (setq span arg arg nil))
4101 (catch 'exit
4102 (setq org-agenda-buffer-name
4103 (or org-agenda-buffer-tmp-name
4104 (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
4105 (if org-agenda-sticky
4106 (cond ((and org-keys (stringp org-match))
4107 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4108 (org-keys
4109 (format "*Org Agenda(%s)*" org-keys))
4110 (t "*Org Agenda(a)*")))
4111 "*Org Agenda*"))
4112 (org-agenda-prepare "Day/Week")
4113 (setq start-day (or start-day org-agenda-start-day))
4114 (if (stringp start-day)
4115 ;; Convert to an absolute day number
4116 (setq start-day (time-to-days (org-read-date nil t start-day))))
4117 (org-compile-prefix-format 'agenda)
4118 (org-set-sorting-strategy 'agenda)
4119 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4120 (today (org-today))
4121 (sd (or start-day today))
4122 (ndays (org-agenda-span-to-ndays span sd))
4123 (org-agenda-start-on-weekday
4124 (if (or (eq ndays 7) (eq ndays 14))
4125 org-agenda-start-on-weekday))
4126 (thefiles (org-agenda-files nil 'ifmode))
4127 (files thefiles)
4128 (start (if (or (null org-agenda-start-on-weekday)
4129 (< ndays 7))
4131 (let* ((nt (calendar-day-of-week
4132 (calendar-gregorian-from-absolute sd)))
4133 (n1 org-agenda-start-on-weekday)
4134 (d (- nt n1)))
4135 (- sd (+ (if (< d 0) 7 0) d)))))
4136 (day-numbers (list start))
4137 (day-cnt 0)
4138 (inhibit-redisplay (not debug-on-error))
4139 (org-agenda-show-log-scoped org-agenda-show-log)
4140 s e rtn rtnall file date d start-pos end-pos todayp
4141 clocktable-start clocktable-end filter)
4142 (setq org-agenda-redo-command
4143 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4144 (dotimes (n (1- ndays))
4145 (push (1+ (car day-numbers)) day-numbers))
4146 (setq day-numbers (nreverse day-numbers))
4147 (setq clocktable-start (car day-numbers)
4148 clocktable-end (1+ (or (org-last day-numbers) 0)))
4149 (setq-local org-starting-day (car day-numbers))
4150 (setq-local org-arg-loc arg)
4151 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4152 (unless org-agenda-compact-blocks
4153 (let* ((d1 (car day-numbers))
4154 (d2 (org-last day-numbers))
4155 (w1 (org-days-to-iso-week d1))
4156 (w2 (org-days-to-iso-week d2)))
4157 (setq s (point))
4158 (if org-agenda-overriding-header
4159 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4160 nil 'face 'org-agenda-structure) "\n")
4161 (insert (org-agenda-span-name span)
4162 "-agenda"
4163 (if (< (- d2 d1) 350)
4164 (if (= w1 w2)
4165 (format " (W%02d)" w1)
4166 (format " (W%02d-W%02d)" w1 w2))
4168 ":\n")))
4169 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4170 'org-date-line t))
4171 (org-agenda-mark-header-line s))
4172 (while (setq d (pop day-numbers))
4173 (setq date (calendar-gregorian-from-absolute d)
4174 s (point))
4175 (if (or (setq todayp (= d today))
4176 (and (not start-pos) (= d sd)))
4177 (setq start-pos (point))
4178 (if (and start-pos (not end-pos))
4179 (setq end-pos (point))))
4180 (setq files thefiles
4181 rtnall nil)
4182 (while (setq file (pop files))
4183 (catch 'nextfile
4184 (org-check-agenda-file file)
4185 (let ((org-agenda-entry-types org-agenda-entry-types))
4186 ;; Starred types override non-starred equivalents
4187 (when (member :deadline* org-agenda-entry-types)
4188 (setq org-agenda-entry-types
4189 (delq :deadline org-agenda-entry-types)))
4190 (when (member :scheduled* org-agenda-entry-types)
4191 (setq org-agenda-entry-types
4192 (delq :scheduled org-agenda-entry-types)))
4193 ;; Honor with-hour
4194 (when with-hour
4195 (when (member :deadline org-agenda-entry-types)
4196 (setq org-agenda-entry-types
4197 (delq :deadline org-agenda-entry-types))
4198 (push :deadline* org-agenda-entry-types))
4199 (when (member :scheduled org-agenda-entry-types)
4200 (setq org-agenda-entry-types
4201 (delq :scheduled org-agenda-entry-types))
4202 (push :scheduled* org-agenda-entry-types)))
4203 (unless org-agenda-include-deadlines
4204 (setq org-agenda-entry-types
4205 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4206 (cond
4207 ((memq org-agenda-show-log-scoped '(only clockcheck))
4208 (setq rtn (org-agenda-get-day-entries
4209 file date :closed)))
4210 (org-agenda-show-log-scoped
4211 (setq rtn (apply 'org-agenda-get-day-entries
4212 file date
4213 (append '(:closed) org-agenda-entry-types))))
4215 (setq rtn (apply 'org-agenda-get-day-entries
4216 file date
4217 org-agenda-entry-types)))))
4218 (setq rtnall (append rtnall rtn)))) ;; all entries
4219 (if org-agenda-include-diary
4220 (let ((org-agenda-search-headline-for-time t))
4221 (require 'diary-lib)
4222 (setq rtn (org-get-entries-from-diary date))
4223 (setq rtnall (append rtnall rtn))))
4224 (if (or rtnall org-agenda-show-all-dates)
4225 (progn
4226 (setq day-cnt (1+ day-cnt))
4227 (insert
4228 (if (stringp org-agenda-format-date)
4229 (format-time-string org-agenda-format-date
4230 (org-time-from-absolute date))
4231 (funcall org-agenda-format-date date))
4232 "\n")
4233 (put-text-property s (1- (point)) 'face
4234 (org-agenda-get-day-face date))
4235 (put-text-property s (1- (point)) 'org-date-line t)
4236 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4237 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4238 (when todayp
4239 (put-text-property s (1- (point)) 'org-today t))
4240 (setq rtnall
4241 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4242 (if rtnall (insert ;; all entries
4243 (org-agenda-finalize-entries rtnall 'agenda)
4244 "\n"))
4245 (put-text-property s (1- (point)) 'day d)
4246 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4247 (when (and org-agenda-clockreport-mode clocktable-start)
4248 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4249 ;; the above line is to ensure the restricted range!
4250 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4251 tbl)
4252 (setq p (org-plist-delete p :block))
4253 (setq p (plist-put p :tstart clocktable-start))
4254 (setq p (plist-put p :tend clocktable-end))
4255 (setq p (plist-put p :scope 'agenda))
4256 (setq tbl (apply 'org-clock-get-clocktable p))
4257 (insert tbl)))
4258 (goto-char (point-min))
4259 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4260 (unless (and (pos-visible-in-window-p (point-min))
4261 (pos-visible-in-window-p (point-max)))
4262 (goto-char (1- (point-max)))
4263 (recenter -1)
4264 (if (not (pos-visible-in-window-p (or start-pos 1)))
4265 (progn
4266 (goto-char (or start-pos 1))
4267 (recenter 1))))
4268 (goto-char (or start-pos 1))
4269 (add-text-properties (point-min) (point-max)
4270 `(org-agenda-type agenda
4271 org-last-args (,arg ,start-day ,span)
4272 org-redo-cmd ,org-agenda-redo-command
4273 org-series-cmd ,org-cmd))
4274 (if (eq org-agenda-show-log-scoped 'clockcheck)
4275 (org-agenda-show-clocking-issues))
4276 (org-agenda-finalize)
4277 (setq buffer-read-only t)
4278 (message ""))))
4280 (defun org-agenda-ndays-to-span (n)
4281 "Return a span symbol for a span of N days, or N if none matches."
4282 (cond ((symbolp n) n)
4283 ((= n 1) 'day)
4284 ((= n 7) 'week)
4285 ((= n 14) 'fortnight)
4286 (t n)))
4288 (defun org-agenda-span-to-ndays (span &optional start-day)
4289 "Return ndays from SPAN, possibly starting at START-DAY.
4290 START-DAY is an absolute time value."
4291 (cond ((numberp span) span)
4292 ((eq span 'day) 1)
4293 ((eq span 'week) 7)
4294 ((eq span 'fortnight) 14)
4295 ((eq span 'month)
4296 (let ((date (calendar-gregorian-from-absolute start-day)))
4297 (calendar-last-day-of-month (car date) (cl-caddr date))))
4298 ((eq span 'year)
4299 (let ((date (calendar-gregorian-from-absolute start-day)))
4300 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4302 (defun org-agenda-span-name (span)
4303 "Return a SPAN name."
4304 (if (null span)
4306 (if (symbolp span)
4307 (capitalize (symbol-name span))
4308 (format "%d days" span))))
4310 ;;; Agenda word search
4312 (defvar org-agenda-search-history nil)
4314 (defvar org-search-syntax-table nil
4315 "Special syntax table for Org search.
4316 In this table, we have single quotes not as word constituents, to
4317 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4319 (defvar org-mode-syntax-table) ; From org.el
4320 (defun org-search-syntax-table ()
4321 (unless org-search-syntax-table
4322 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4323 (modify-syntax-entry ?' "." org-search-syntax-table)
4324 (modify-syntax-entry ?` "." org-search-syntax-table))
4325 org-search-syntax-table)
4327 (defvar org-agenda-last-search-view-search-was-boolean nil)
4329 ;;;###autoload
4330 (defun org-search-view (&optional todo-only string edit-at)
4331 "Show all entries that contain a phrase or words or regular expressions.
4333 With optional prefix argument TODO-ONLY, only consider entries that are
4334 TODO entries. The argument STRING can be used to pass a default search
4335 string into this function. If EDIT-AT is non-nil, it means that the
4336 user should get a chance to edit this string, with cursor at position
4337 EDIT-AT.
4339 The search string can be viewed either as a phrase that should be found as
4340 is, or it can be broken into a number of snippets, each of which must match
4341 in a Boolean way to select an entry. The default depends on the variable
4342 `org-agenda-search-view-always-boolean'.
4343 Even if this is turned off (the default) you can always switch to
4344 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4346 The default is a direct search of the whole phrase, where each space in
4347 the search string can expand to an arbitrary amount of whitespace,
4348 including newlines.
4350 If using a Boolean search, the search string is split on whitespace and
4351 each snippet is searched separately, with logical AND to select an entry.
4352 Words prefixed with a minus must *not* occur in the entry. Words without
4353 a prefix or prefixed with a plus must occur in the entry. Matching is
4354 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4355 match whole words, not parts of a word) if
4356 `org-agenda-search-view-force-full-words' is set (default is nil).
4358 Boolean search snippets enclosed by curly braces are interpreted as
4359 regular expressions that must or (when preceded with \"-\") must not
4360 match in the entry. Snippets enclosed into double quotes will be taken
4361 as a whole, to include whitespace.
4363 - If the search string starts with an asterisk, search only in headlines.
4364 - If (possibly after the leading star) the search string starts with an
4365 exclamation mark, this also means to look at TODO entries only, an effect
4366 that can also be achieved with a prefix argument.
4367 - If (possibly after star and exclamation mark) the search string starts
4368 with a colon, this will mean that the (non-regexp) snippets of the
4369 Boolean search must match as full words.
4371 This command searches the agenda files, and in addition the files
4372 listed in `org-agenda-text-search-extra-files' unless a restriction lock
4373 is active."
4374 (interactive "P")
4375 (if org-agenda-overriding-arguments
4376 (setq todo-only (car org-agenda-overriding-arguments)
4377 string (nth 1 org-agenda-overriding-arguments)
4378 edit-at (nth 2 org-agenda-overriding-arguments)))
4379 (let* ((props (list 'face nil
4380 'done-face 'org-agenda-done
4381 'org-not-done-regexp org-not-done-regexp
4382 'org-todo-regexp org-todo-regexp
4383 'org-complex-heading-regexp org-complex-heading-regexp
4384 'mouse-face 'highlight
4385 'help-echo (format "mouse-2 or RET jump to location")))
4386 (full-words org-agenda-search-view-force-full-words)
4387 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4388 regexp rtn rtnall files file pos inherited-tags
4389 marker category level tags c neg re boolean
4390 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4391 (unless (and (not edit-at)
4392 (stringp string)
4393 (string-match "\\S-" string))
4394 (setq string (read-string
4395 (if org-agenda-search-view-always-boolean
4396 "[+-]Word/{Regexp} ...: "
4397 "Phrase or [+-]Word/{Regexp} ...: ")
4398 (cond
4399 ((integerp edit-at) (cons string edit-at))
4400 (edit-at string))
4401 'org-agenda-search-history)))
4402 (catch 'exit
4403 (if org-agenda-sticky
4404 (setq org-agenda-buffer-name
4405 (if (stringp string)
4406 (format "*Org Agenda(%s:%s)*"
4407 (or org-keys (or (and todo-only "S") "s")) string)
4408 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4409 (org-agenda-prepare "SEARCH")
4410 (org-compile-prefix-format 'search)
4411 (org-set-sorting-strategy 'search)
4412 (setq org-agenda-redo-command
4413 (list 'org-search-view (if todo-only t nil)
4414 (list 'if 'current-prefix-arg nil string)))
4415 (setq org-agenda-query-string string)
4416 (if (equal (string-to-char string) ?*)
4417 (setq hdl-only t
4418 words (substring string 1))
4419 (setq words string))
4420 (when (equal (string-to-char words) ?!)
4421 (setq todo-only t
4422 words (substring words 1)))
4423 (when (equal (string-to-char words) ?:)
4424 (setq full-words t
4425 words (substring words 1)))
4426 (if (or org-agenda-search-view-always-boolean
4427 (member (string-to-char words) '(?- ?+ ?\{)))
4428 (setq boolean t))
4429 (setq words (split-string words))
4430 (let (www w)
4431 (while (setq w (pop words))
4432 (while (and (string-match "\\\\\\'" w) words)
4433 (setq w (concat (substring w 0 -1) " " (pop words))))
4434 (push w www))
4435 (setq words (nreverse www) www nil)
4436 (while (setq w (pop words))
4437 (when (and (string-match "\\`[-+]?{" w)
4438 (not (string-match "}\\'" w)))
4439 (while (and words (not (string-match "}\\'" (car words))))
4440 (setq w (concat w " " (pop words))))
4441 (setq w (concat w " " (pop words))))
4442 (push w www))
4443 (setq words (nreverse www)))
4444 (setq org-agenda-last-search-view-search-was-boolean boolean)
4445 (when boolean
4446 (let (wds w)
4447 (while (setq w (pop words))
4448 (if (or (equal (substring w 0 1) "\"")
4449 (and (> (length w) 1)
4450 (member (substring w 0 1) '("+" "-"))
4451 (equal (substring w 1 2) "\"")))
4452 (while (and words (not (equal (substring w -1) "\"")))
4453 (setq w (concat w " " (pop words)))))
4454 (and (string-match "\\`\\([-+]?\\)\"" w)
4455 (setq w (replace-match "\\1" nil nil w)))
4456 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4457 (push w wds))
4458 (setq words (nreverse wds))))
4459 (if boolean
4460 (mapc (lambda (w)
4461 (setq c (string-to-char w))
4462 (if (equal c ?-)
4463 (setq neg t w (substring w 1))
4464 (if (equal c ?+)
4465 (setq neg nil w (substring w 1))
4466 (setq neg nil)))
4467 (if (string-match "\\`{.*}\\'" w)
4468 (setq re (substring w 1 -1))
4469 (if full-words
4470 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4471 (setq re (regexp-quote (downcase w)))))
4472 (if neg (push re regexps-) (push re regexps+)))
4473 words)
4474 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4475 regexps+))
4476 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4477 (if (not regexps+)
4478 (setq regexp org-outline-regexp-bol)
4479 (setq regexp (pop regexps+))
4480 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4481 regexp))))
4482 (setq files (org-agenda-files nil 'ifmode))
4483 ;; Add `org-agenda-text-search-extra-files' unless there is some
4484 ;; restriction.
4485 (unless (get 'org-agenda-files 'org-restrict)
4486 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4487 (pop org-agenda-text-search-extra-files)
4488 (setq files (org-add-archive-files files))))
4489 ;; Uniquify files. However, let `org-check-agenda-file' handle
4490 ;; non-existent ones.
4491 (setq files (cl-remove-duplicates
4492 (append files org-agenda-text-search-extra-files)
4493 (lambda (a b)
4494 (and (file-exists-p a)
4495 (file-exists-p b)
4496 (file-equal-p a b))))
4497 rtnall nil)
4498 (while (setq file (pop files))
4499 (setq ee nil)
4500 (catch 'nextfile
4501 (org-check-agenda-file file)
4502 (setq buffer (if (file-exists-p file)
4503 (org-get-agenda-file-buffer file)
4504 (error "No such file %s" file)))
4505 (if (not buffer)
4506 ;; If file does not exist, make sure an error message is sent
4507 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4508 file))))
4509 (with-current-buffer buffer
4510 (with-syntax-table (org-search-syntax-table)
4511 (unless (derived-mode-p 'org-mode)
4512 (error "Agenda file %s is not in `org-mode'" file))
4513 (let ((case-fold-search t))
4514 (save-excursion
4515 (save-restriction
4516 (if (eq buffer org-agenda-restrict)
4517 (narrow-to-region org-agenda-restrict-begin
4518 org-agenda-restrict-end)
4519 (widen))
4520 (goto-char (point-min))
4521 (unless (or (org-at-heading-p)
4522 (outline-next-heading))
4523 (throw 'nextfile t))
4524 (goto-char (max (point-min) (1- (point))))
4525 (while (re-search-forward regexp nil t)
4526 (org-back-to-heading t)
4527 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4528 (> (org-reduced-level (org-outline-level))
4529 org-agenda-search-view-max-outline-level)
4530 (forward-line -1)
4531 (org-back-to-heading t)))
4532 (skip-chars-forward "* ")
4533 (setq beg (point-at-bol)
4534 beg1 (point)
4535 end (progn
4536 (outline-next-heading)
4537 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4538 (> (org-reduced-level (org-outline-level))
4539 org-agenda-search-view-max-outline-level)
4540 (forward-line 1)
4541 (outline-next-heading)))
4542 (point)))
4544 (catch :skip
4545 (goto-char beg)
4546 (org-agenda-skip)
4547 (setq str (buffer-substring-no-properties
4548 (point-at-bol)
4549 (if hdl-only (point-at-eol) end)))
4550 (mapc (lambda (wr) (when (string-match wr str)
4551 (goto-char (1- end))
4552 (throw :skip t)))
4553 regexps-)
4554 (mapc (lambda (wr) (unless (string-match wr str)
4555 (goto-char (1- end))
4556 (throw :skip t)))
4557 (if todo-only
4558 (cons (concat "^\\*+[ \t]+"
4559 org-not-done-regexp)
4560 regexps+)
4561 regexps+))
4562 (goto-char beg)
4563 (setq marker (org-agenda-new-marker (point))
4564 category (org-get-category)
4565 level (make-string (org-reduced-level (org-outline-level)) ? )
4566 inherited-tags
4567 (or (eq org-agenda-show-inherited-tags 'always)
4568 (and (listp org-agenda-show-inherited-tags)
4569 (memq 'todo org-agenda-show-inherited-tags))
4570 (and (eq org-agenda-show-inherited-tags t)
4571 (or (eq org-agenda-use-tag-inheritance t)
4572 (memq 'todo org-agenda-use-tag-inheritance))))
4573 tags (org-get-tags-at nil (not inherited-tags))
4574 txt (org-agenda-format-item
4576 (buffer-substring-no-properties
4577 beg1 (point-at-eol))
4578 level category tags t))
4579 (org-add-props txt props
4580 'org-marker marker 'org-hd-marker marker
4581 'org-todo-regexp org-todo-regexp
4582 'level level
4583 'org-complex-heading-regexp org-complex-heading-regexp
4584 'priority 1000
4585 'type "search")
4586 (push txt ee)
4587 (goto-char (1- end))))))))))
4588 (setq rtn (nreverse ee))
4589 (setq rtnall (append rtnall rtn)))
4590 (if org-agenda-overriding-header
4591 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4592 nil 'face 'org-agenda-structure) "\n")
4593 (insert "Search words: ")
4594 (add-text-properties (point-min) (1- (point))
4595 (list 'face 'org-agenda-structure))
4596 (setq pos (point))
4597 (insert string "\n")
4598 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4599 (setq pos (point))
4600 (unless org-agenda-multi
4601 (insert (substitute-command-keys "\
4602 Press `\\[org-agenda-manipulate-query-add]', \
4603 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4604 `\\[org-agenda-manipulate-query-add-re]', \
4605 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4606 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4607 (add-text-properties pos (1- (point))
4608 (list 'face 'org-agenda-structure))))
4609 (org-agenda-mark-header-line (point-min))
4610 (when rtnall
4611 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4612 (goto-char (point-min))
4613 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4614 (add-text-properties (point-min) (point-max)
4615 `(org-agenda-type search
4616 org-last-args (,todo-only ,string ,edit-at)
4617 org-redo-cmd ,org-agenda-redo-command
4618 org-series-cmd ,org-cmd))
4619 (org-agenda-finalize)
4620 (setq buffer-read-only t))))
4622 ;;; Agenda TODO list
4624 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4625 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4626 (concat
4627 (if (or (equal keywords "ALL") (not keywords))
4628 (propertize "ALL" 'face 'warning)
4629 (mapconcat
4630 (lambda (kw)
4631 (propertize kw 'face (org-get-todo-face kw)))
4632 (org-split-string keywords "|")
4633 "|"))
4634 "\n"))
4636 (defvar org-select-this-todo-keyword nil)
4637 (defvar org-last-arg nil)
4639 ;;;###autoload
4640 (defun org-todo-list (&optional arg)
4641 "Show all (not done) TODO entries from all agenda file in a single list.
4642 The prefix arg can be used to select a specific TODO keyword and limit
4643 the list to these. When using `\\[universal-argument]', you will be prompted
4644 for a keyword. A numeric prefix directly selects the Nth keyword in
4645 `org-todo-keywords-1'."
4646 (interactive "P")
4647 (if org-agenda-overriding-arguments
4648 (setq arg org-agenda-overriding-arguments))
4649 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4650 (let* ((today (org-today))
4651 (date (calendar-gregorian-from-absolute today))
4652 (kwds org-todo-keywords-for-agenda)
4653 (completion-ignore-case t)
4654 (org-select-this-todo-keyword
4655 (if (stringp arg) arg
4656 (and arg (integerp arg) (> arg 0)
4657 (nth (1- arg) kwds))))
4658 rtn rtnall files file pos)
4659 (when (equal arg '(4))
4660 (setq org-select-this-todo-keyword
4661 (completing-read "Keyword (or KWD1|K2D2|...): "
4662 (mapcar #'list kwds) nil nil)))
4663 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4664 (catch 'exit
4665 (if org-agenda-sticky
4666 (setq org-agenda-buffer-name
4667 (if (stringp org-select-this-todo-keyword)
4668 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4669 org-select-this-todo-keyword)
4670 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4671 (org-agenda-prepare "TODO")
4672 (org-compile-prefix-format 'todo)
4673 (org-set-sorting-strategy 'todo)
4674 (setq org-agenda-redo-command
4675 `(org-todo-list (or (and (numberp current-prefix-arg)
4676 current-prefix-arg)
4677 ,org-select-this-todo-keyword
4678 current-prefix-arg ,arg)))
4679 (setq files (org-agenda-files nil 'ifmode)
4680 rtnall nil)
4681 (while (setq file (pop files))
4682 (catch 'nextfile
4683 (org-check-agenda-file file)
4684 (setq rtn (org-agenda-get-day-entries file date :todo))
4685 (setq rtnall (append rtnall rtn))))
4686 (if org-agenda-overriding-header
4687 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4688 nil 'face 'org-agenda-structure) "\n")
4689 (insert "Global list of TODO items of type: ")
4690 (add-text-properties (point-min) (1- (point))
4691 (list 'face 'org-agenda-structure
4692 'short-heading
4693 (concat "ToDo: "
4694 (or org-select-this-todo-keyword "ALL"))))
4695 (org-agenda-mark-header-line (point-min))
4696 (insert (org-agenda-propertize-selected-todo-keywords
4697 org-select-this-todo-keyword))
4698 (setq pos (point))
4699 (unless org-agenda-multi
4700 (insert (substitute-command-keys "Available with \
4701 `N \\[org-agenda-redo]': (0)[ALL]"))
4702 (let ((n 0) s)
4703 (mapc (lambda (x)
4704 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4705 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4706 (insert "\n "))
4707 (insert " " s))
4708 kwds))
4709 (insert "\n"))
4710 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4711 (org-agenda-mark-header-line (point-min))
4712 (when rtnall
4713 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4714 (goto-char (point-min))
4715 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4716 (add-text-properties (point-min) (point-max)
4717 `(org-agenda-type todo
4718 org-last-args ,arg
4719 org-redo-cmd ,org-agenda-redo-command
4720 org-series-cmd ,org-cmd))
4721 (org-agenda-finalize)
4722 (setq buffer-read-only t))))
4724 ;;; Agenda tags match
4726 ;;;###autoload
4727 (defun org-tags-view (&optional todo-only match)
4728 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4729 The prefix arg TODO-ONLY limits the search to TODO entries."
4730 (interactive "P")
4731 (if org-agenda-overriding-arguments
4732 (setq todo-only (car org-agenda-overriding-arguments)
4733 match (nth 1 org-agenda-overriding-arguments)))
4734 (let* ((org-tags-match-list-sublevels
4735 org-tags-match-list-sublevels)
4736 (completion-ignore-case t)
4737 (org--matcher-tags-todo-only todo-only)
4738 rtn rtnall files file pos matcher
4739 buffer)
4740 (when (and (stringp match) (not (string-match "\\S-" match)))
4741 (setq match nil))
4742 (catch 'exit
4743 (if org-agenda-sticky
4744 (setq org-agenda-buffer-name
4745 (if (stringp match)
4746 (format "*Org Agenda(%s:%s)*"
4747 (or org-keys (or (and todo-only "M") "m")) match)
4748 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4749 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4750 ;; expanding tags within `org-make-tags-matcher'
4751 (org-agenda-prepare (concat "TAGS " match))
4752 (setq matcher (org-make-tags-matcher match)
4753 match (car matcher)
4754 matcher (cdr matcher))
4755 (org-compile-prefix-format 'tags)
4756 (org-set-sorting-strategy 'tags)
4757 (setq org-agenda-query-string match)
4758 (setq org-agenda-redo-command
4759 (list 'org-tags-view
4760 `(quote ,org--matcher-tags-todo-only)
4761 `(if current-prefix-arg nil ,org-agenda-query-string)))
4762 (setq files (org-agenda-files nil 'ifmode)
4763 rtnall nil)
4764 (while (setq file (pop files))
4765 (catch 'nextfile
4766 (org-check-agenda-file file)
4767 (setq buffer (if (file-exists-p file)
4768 (org-get-agenda-file-buffer file)
4769 (error "No such file %s" file)))
4770 (if (not buffer)
4771 ;; If file does not exist, error message to agenda
4772 (setq rtn (list
4773 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4774 rtnall (append rtnall rtn))
4775 (with-current-buffer buffer
4776 (unless (derived-mode-p 'org-mode)
4777 (error "Agenda file %s is not in `org-mode'" file))
4778 (save-excursion
4779 (save-restriction
4780 (if (eq buffer org-agenda-restrict)
4781 (narrow-to-region org-agenda-restrict-begin
4782 org-agenda-restrict-end)
4783 (widen))
4784 (setq rtn (org-scan-tags 'agenda
4785 matcher
4786 org--matcher-tags-todo-only))
4787 (setq rtnall (append rtnall rtn))))))))
4788 (if org-agenda-overriding-header
4789 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4790 nil 'face 'org-agenda-structure) "\n")
4791 (insert "Headlines with TAGS match: ")
4792 (add-text-properties (point-min) (1- (point))
4793 (list 'face 'org-agenda-structure
4794 'short-heading
4795 (concat "Match: " match)))
4796 (setq pos (point))
4797 (insert match "\n")
4798 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4799 (setq pos (point))
4800 (unless org-agenda-multi
4801 (insert (substitute-command-keys
4802 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4803 to search again with new search string\n")))
4804 (add-text-properties pos (1- (point))
4805 (list 'face 'org-agenda-structure)))
4806 (org-agenda-mark-header-line (point-min))
4807 (when rtnall
4808 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4809 (goto-char (point-min))
4810 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4811 (add-text-properties
4812 (point-min) (point-max)
4813 `(org-agenda-type tags
4814 org-last-args (,org--matcher-tags-todo-only ,match)
4815 org-redo-cmd ,org-agenda-redo-command
4816 org-series-cmd ,org-cmd))
4817 (org-agenda-finalize)
4818 (setq buffer-read-only t))))
4820 ;;; Agenda Finding stuck projects
4822 (defvar org-agenda-skip-regexp nil
4823 "Regular expression used in skipping subtrees for the agenda.
4824 This is basically a temporary global variable that can be set and then
4825 used by user-defined selections using `org-agenda-skip-function'.")
4827 (defvar org-agenda-overriding-header nil
4828 "When set during agenda, todo and tags searches it replaces the header.
4829 This variable should not be set directly, but custom commands can bind it
4830 in the options section.")
4832 (defun org-agenda-skip-entry-if (&rest conditions)
4833 "Skip entry if any of CONDITIONS is true.
4834 See `org-agenda-skip-if' for details."
4835 (org-agenda-skip-if nil conditions))
4837 (defun org-agenda-skip-subtree-if (&rest conditions)
4838 "Skip subtree if any of CONDITIONS is true.
4839 See `org-agenda-skip-if' for details."
4840 (org-agenda-skip-if t conditions))
4842 (defun org-agenda-skip-if (subtree conditions)
4843 "Checks current entity for CONDITIONS.
4844 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4845 the entry (i.e. the text before the next heading) is checked.
4847 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4848 from different tests. Valid conditions are:
4850 scheduled Check if there is a scheduled cookie
4851 notscheduled Check if there is no scheduled cookie
4852 deadline Check if there is a deadline
4853 notdeadline Check if there is no deadline
4854 timestamp Check if there is a timestamp (also deadline or scheduled)
4855 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4856 regexp Check if regexp matches
4857 notregexp Check if regexp does not match.
4858 todo Check if TODO keyword matches
4859 nottodo Check if TODO keyword does not match
4861 The regexp is taken from the conditions list, it must come right after
4862 the `regexp' or `notregexp' element.
4864 `todo' and `nottodo' accept as an argument a list of todo
4865 keywords, which may include \"*\" to match any todo keyword.
4867 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4869 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4871 Instead of a list, a keyword class may be given. For example:
4873 (org-agenda-skip-entry-if \\='nottodo \\='done)
4875 would skip entries that haven't been marked with any of \"DONE\"
4876 keywords. Possible classes are: `todo', `done', `any'.
4878 If any of these conditions is met, this function returns the end point of
4879 the entity, causing the search to continue from there. This is a function
4880 that can be put into `org-agenda-skip-function' for the duration of a command."
4881 (let (beg end m)
4882 (org-back-to-heading t)
4883 (setq beg (point)
4884 end (if subtree
4885 (progn (org-end-of-subtree t) (point))
4886 (progn (outline-next-heading) (1- (point)))))
4887 (goto-char beg)
4888 (and
4890 (and (memq 'scheduled conditions)
4891 (re-search-forward org-scheduled-time-regexp end t))
4892 (and (memq 'notscheduled conditions)
4893 (not (re-search-forward org-scheduled-time-regexp end t)))
4894 (and (memq 'deadline conditions)
4895 (re-search-forward org-deadline-time-regexp end t))
4896 (and (memq 'notdeadline conditions)
4897 (not (re-search-forward org-deadline-time-regexp end t)))
4898 (and (memq 'timestamp conditions)
4899 (re-search-forward org-ts-regexp end t))
4900 (and (memq 'nottimestamp conditions)
4901 (not (re-search-forward org-ts-regexp end t)))
4902 (and (setq m (memq 'regexp conditions))
4903 (stringp (nth 1 m))
4904 (re-search-forward (nth 1 m) end t))
4905 (and (setq m (memq 'notregexp conditions))
4906 (stringp (nth 1 m))
4907 (not (re-search-forward (nth 1 m) end t)))
4908 (and (or
4909 (setq m (memq 'nottodo conditions))
4910 (setq m (memq 'todo-unblocked conditions))
4911 (setq m (memq 'nottodo-unblocked conditions))
4912 (setq m (memq 'todo conditions)))
4913 (org-agenda-skip-if-todo m end)))
4914 end)))
4916 (defun org-agenda-skip-if-todo (args end)
4917 "Helper function for `org-agenda-skip-if', do not use it directly.
4918 ARGS is a list with first element either `todo', `nottodo',
4919 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4920 a list of TODO keywords, or a state symbol `todo' or `done' or
4921 `any'."
4922 (let ((kw (car args))
4923 (arg (cadr args))
4924 todo-wds todo-re)
4925 (setq todo-wds
4926 (org-uniquify
4927 (cond
4928 ((listp arg) ;; list of keywords
4929 (if (member "*" arg)
4930 (mapcar 'substring-no-properties org-todo-keywords-1)
4931 arg))
4932 ((symbolp arg) ;; keyword class name
4933 (cond
4934 ((eq arg 'todo)
4935 (org-delete-all org-done-keywords
4936 (mapcar 'substring-no-properties
4937 org-todo-keywords-1)))
4938 ((eq arg 'done) org-done-keywords)
4939 ((eq arg 'any)
4940 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4941 (setq todo-re
4942 (concat "^\\*+[ \t]+\\<\\("
4943 (mapconcat 'identity todo-wds "\\|")
4944 "\\)\\>"))
4945 (cond
4946 ((eq kw 'todo) (re-search-forward todo-re end t))
4947 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4948 ((eq kw 'todo-unblocked)
4949 (catch 'unblocked
4950 (while (re-search-forward todo-re end t)
4951 (or (org-entry-blocked-p) (throw 'unblocked t)))
4952 nil))
4953 ((eq kw 'nottodo-unblocked)
4954 (catch 'unblocked
4955 (while (re-search-forward todo-re end t)
4956 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4960 ;;;###autoload
4961 (defun org-agenda-list-stuck-projects (&rest ignore)
4962 "Create agenda view for projects that are stuck.
4963 Stuck projects are project that have no next actions. For the definitions
4964 of what a project is and how to check if it stuck, customize the variable
4965 `org-stuck-projects'."
4966 (interactive)
4967 (let* ((org-agenda-overriding-header
4968 (or org-agenda-overriding-header "List of stuck projects: "))
4969 (matcher (nth 0 org-stuck-projects))
4970 (todo (nth 1 org-stuck-projects))
4971 (tags (nth 2 org-stuck-projects))
4972 (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
4973 (todo-wds
4974 (if (not (member "*" todo)) todo
4975 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
4976 (org-delete-all org-done-keywords-for-agenda
4977 (copy-sequence org-todo-keywords-for-agenda))))
4978 (todo-re (and todo
4979 (format "^\\*+[ \t]+\\(%s\\)\\>"
4980 (mapconcat #'identity todo-wds "\\|"))))
4981 (tags-re (cond ((null tags) nil)
4982 ((member "*" tags)
4983 (eval-when-compile
4984 (concat org-outline-regexp-bol
4985 ".*:[[:alnum:]_@#%]+:[ \t]*$")))
4986 (tags (concat org-outline-regexp-bol
4987 ".*:\\("
4988 (mapconcat #'identity tags "\\|")
4989 "\\):[[:alnum:]_@#%:]*[ \t]*$"))
4990 (t nil)))
4991 (re-list (delq nil (list todo-re tags-re gen-re)))
4992 (skip-re
4993 (if (null re-list)
4994 (error "Missing information to identify unstuck projects")
4995 (mapconcat #'identity re-list "\\|")))
4996 (org-agenda-skip-function
4997 ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
4998 ;; in the subtree.
4999 `(lambda ()
5000 (and (save-excursion
5001 (let ((case-fold-search nil))
5002 (re-search-forward
5003 ,skip-re (save-excursion (org-end-of-subtree t)) t)))
5004 (progn (outline-next-heading) (point))))))
5005 (org-tags-view nil matcher)
5006 (setq org-agenda-buffer-name (buffer-name))
5007 (with-current-buffer org-agenda-buffer-name
5008 (setq org-agenda-redo-command
5009 `(org-agenda-list-stuck-projects ,current-prefix-arg))
5010 (let ((inhibit-read-only t))
5011 (add-text-properties
5012 (point-min) (point-max)
5013 `(org-redo-cmd ,org-agenda-redo-command))))))
5015 ;;; Diary integration
5017 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5018 (defvar diary-list-entries-hook)
5019 (defvar diary-time-regexp)
5020 (defun org-get-entries-from-diary (date)
5021 "Get the (Emacs Calendar) diary entries for DATE."
5022 (require 'diary-lib)
5023 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5024 (diary-display-function 'diary-fancy-display)
5025 (pop-up-frames nil)
5026 (diary-list-entries-hook
5027 (cons 'org-diary-default-entry diary-list-entries-hook))
5028 (diary-file-name-prefix nil) ; turn this feature off
5029 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5030 entries
5031 (org-disable-agenda-to-diary t))
5032 (save-excursion
5033 (save-window-excursion
5034 (funcall (if (fboundp 'diary-list-entries)
5035 'diary-list-entries 'list-diary-entries)
5036 date 1)))
5037 (if (not (get-buffer diary-fancy-buffer))
5038 (setq entries nil)
5039 (with-current-buffer diary-fancy-buffer
5040 (setq buffer-read-only nil)
5041 (if (zerop (buffer-size))
5042 ;; No entries
5043 (setq entries nil)
5044 ;; Omit the date and other unnecessary stuff
5045 (org-agenda-cleanup-fancy-diary)
5046 ;; Add prefix to each line and extend the text properties
5047 (if (zerop (buffer-size))
5048 (setq entries nil)
5049 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5050 (setq entries
5051 (with-temp-buffer
5052 (insert entries) (goto-char (point-min))
5053 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5054 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5055 (replace-match (concat "; " (match-string 1)))))
5056 (buffer-string)))))
5057 (set-buffer-modified-p nil)
5058 (kill-buffer diary-fancy-buffer)))
5059 (when entries
5060 (setq entries (org-split-string entries "\n"))
5061 (setq entries
5062 (mapcar
5063 (lambda (x)
5064 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5065 ;; Extend the text properties to the beginning of the line
5066 (org-add-props x (text-properties-at (1- (length x)) x)
5067 'type "diary" 'date date 'face 'org-agenda-diary))
5068 entries)))))
5070 (defvar org-agenda-cleanup-fancy-diary-hook nil
5071 "Hook run when the fancy diary buffer is cleaned up.")
5073 (defun org-agenda-cleanup-fancy-diary ()
5074 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5075 This gets rid of the date, the underline under the date, and
5076 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5077 date. It also removes lines that contain only whitespace."
5078 (goto-char (point-min))
5079 (if (looking-at ".*?:[ \t]*")
5080 (progn
5081 (replace-match "")
5082 (re-search-forward "\n=+$" nil t)
5083 (replace-match "")
5084 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5085 (re-search-forward "\n=+$" nil t)
5086 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5087 (goto-char (point-min))
5088 (while (re-search-forward "^ +\n" nil t)
5089 (replace-match ""))
5090 (goto-char (point-min))
5091 (if (re-search-forward "^Org mode dummy\n?" nil t)
5092 (replace-match ""))
5093 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5095 ;; Make sure entries from the diary have the right text properties.
5096 (eval-after-load "diary-lib"
5097 '(if (boundp 'diary-modify-entry-list-string-function)
5098 ;; We can rely on the hook, nothing to do
5100 ;; Hook not available, must use advice to make this work
5101 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5102 "Make the position visible."
5103 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5104 (stringp string)
5105 buffer-file-name)
5106 (setq string (org-modify-diary-entry-string string))))))
5108 (defun org-modify-diary-entry-string (string)
5109 "Add text properties to string, allowing Org to act on it."
5110 (org-add-props string nil
5111 'mouse-face 'highlight
5112 'help-echo (if buffer-file-name
5113 (format "mouse-2 or RET jump to diary file %s"
5114 (abbreviate-file-name buffer-file-name))
5116 'org-agenda-diary-link t
5117 'org-marker (org-agenda-new-marker (point-at-bol))))
5119 (defun org-diary-default-entry ()
5120 "Add a dummy entry to the diary.
5121 Needed to avoid empty dates which mess up holiday display."
5122 ;; Catch the error if dealing with the new add-to-diary-alist
5123 (when org-disable-agenda-to-diary
5124 (condition-case nil
5125 (org-add-to-diary-list original-date "Org mode dummy" "")
5126 (error
5127 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5129 (defun org-add-to-diary-list (&rest args)
5130 (if (fboundp 'diary-add-to-list)
5131 (apply 'diary-add-to-list args)
5132 (apply 'add-to-diary-list args)))
5134 (defvar org-diary-last-run-time nil)
5136 ;;;###autoload
5137 (defun org-diary (&rest args)
5138 "Return diary information from org files.
5139 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5140 It accesses org files and extracts information from those files to be
5141 listed in the diary. The function accepts arguments specifying what
5142 items should be listed. For a list of arguments allowed here, see the
5143 variable `org-agenda-entry-types'.
5145 The call in the diary file should look like this:
5147 &%%(org-diary) ~/path/to/some/orgfile.org
5149 Use a separate line for each org file to check. Or, if you omit the file name,
5150 all files listed in `org-agenda-files' will be checked automatically:
5152 &%%(org-diary)
5154 If you don't give any arguments (as in the example above), the default value
5155 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5156 So the example above may also be written as
5158 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5160 The function expects the lisp variables `entry' and `date' to be provided
5161 by the caller, because this is how the calendar works. Don't use this
5162 function from a program - use `org-agenda-get-day-entries' instead."
5163 (when (> (- (float-time)
5164 org-agenda-last-marker-time)
5166 ;; I am not sure if this works with sticky agendas, because the marker
5167 ;; list is then no longer a global variable.
5168 (org-agenda-reset-markers))
5169 (org-compile-prefix-format 'agenda)
5170 (org-set-sorting-strategy 'agenda)
5171 (setq args (or args org-agenda-entry-types))
5172 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5173 (list entry)
5174 (org-agenda-files t)))
5175 (time (float-time))
5176 file rtn results)
5177 (when (or (not org-diary-last-run-time)
5178 (> (- time
5179 org-diary-last-run-time)
5181 (org-agenda-prepare-buffers files))
5182 (setq org-diary-last-run-time time)
5183 ;; If this is called during org-agenda, don't return any entries to
5184 ;; the calendar. Org Agenda will list these entries itself.
5185 (if org-disable-agenda-to-diary (setq files nil))
5186 (while (setq file (pop files))
5187 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5188 (setq results (append results rtn)))
5189 (when results
5190 (setq results
5191 (mapcar (lambda (i) (replace-regexp-in-string
5192 org-bracket-link-regexp "\\3" i)) results))
5193 (concat (org-agenda-finalize-entries results) "\n"))))
5195 ;;; Agenda entry finders
5197 (defun org-agenda--timestamp-to-absolute (&rest args)
5198 "Call `org-time-string-to-absolute' with ARGS.
5199 However, throw `:skip' whenever an error is raised."
5200 (condition-case e
5201 (apply #'org-time-string-to-absolute args)
5202 (org-diary-sexp-no-match (throw :skip nil))
5203 (error
5204 (message "%s; Skipping entry" (error-message-string e))
5205 (throw :skip nil))))
5207 (defun org-agenda-get-day-entries (file date &rest args)
5208 "Does the work for `org-diary' and `org-agenda'.
5209 FILE is the path to a file to be checked for entries. DATE is date like
5210 the one returned by `calendar-current-date'. ARGS are symbols indicating
5211 which kind of entries should be extracted. For details about these, see
5212 the documentation of `org-diary'."
5213 (let* ((org-startup-folded nil)
5214 (org-startup-align-all-tables nil)
5215 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5216 (error "No such file %s" file))))
5217 (if (not buffer)
5218 ;; If file does not exist, signal it in diary nonetheless.
5219 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5220 (with-current-buffer buffer
5221 (unless (derived-mode-p 'org-mode)
5222 (error "Agenda file %s is not in `org-mode'" file))
5223 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5224 (setf org-agenda-current-date date)
5225 (save-excursion
5226 (save-restriction
5227 (if (eq buffer org-agenda-restrict)
5228 (narrow-to-region org-agenda-restrict-begin
5229 org-agenda-restrict-end)
5230 (widen))
5231 ;; Rationalize ARGS. Also make sure `:deadline' comes
5232 ;; first in order to populate DEADLINES before passing it.
5234 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5235 ;; guarding us from modifying `org-agenda-entry-types'.
5236 (setf args (org-uniquify (or args org-agenda-entry-types)))
5237 (when (and (memq :scheduled args) (memq :scheduled* args))
5238 (setf args (delq :scheduled* args)))
5239 (cond
5240 ((memq :deadline args)
5241 (setf args (cons :deadline
5242 (delq :deadline (delq :deadline* args)))))
5243 ((memq :deadline* args)
5244 (setf args (cons :deadline* (delq :deadline* args)))))
5245 ;; Collect list of headlines. Return them flattened.
5246 (let ((case-fold-search nil) results deadlines)
5247 (dolist (arg args (apply #'nconc (nreverse results)))
5248 (pcase arg
5249 ((and :todo (guard (org-agenda-today-p date)))
5250 (push (org-agenda-get-todos) results))
5251 (:timestamp
5252 (push (org-agenda-get-blocks) results)
5253 (push (org-agenda-get-timestamps deadlines) results))
5254 (:sexp
5255 (push (org-agenda-get-sexps) results))
5256 (:scheduled
5257 (push (org-agenda-get-scheduled deadlines) results))
5258 (:scheduled*
5259 (push (org-agenda-get-scheduled deadlines t) results))
5260 (:closed
5261 (push (org-agenda-get-progress) results))
5262 (:deadline
5263 (setf deadlines (org-agenda-get-deadlines))
5264 (push deadlines results))
5265 (:deadline*
5266 (setf deadlines (org-agenda-get-deadlines t))
5267 (push deadlines results)))))))))))
5269 (defsubst org-em (x y list)
5270 "Is X or Y a member of LIST?"
5271 (or (memq x list) (memq y list)))
5273 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5274 (defvar org-agenda-sorting-strategy-selected nil)
5276 (defun org-agenda-entry-get-agenda-timestamp (pom)
5277 "Retrieve timestamp information for sorting agenda views.
5278 Given a point or marker POM, returns a cons cell of the timestamp
5279 and the timestamp type relevant for the sorting strategy in
5280 `org-agenda-sorting-strategy-selected'."
5281 (let (ts ts-date-type)
5282 (save-match-data
5283 (cond ((org-em 'scheduled-up 'scheduled-down
5284 org-agenda-sorting-strategy-selected)
5285 (setq ts (org-entry-get pom "SCHEDULED")
5286 ts-date-type " scheduled"))
5287 ((org-em 'deadline-up 'deadline-down
5288 org-agenda-sorting-strategy-selected)
5289 (setq ts (org-entry-get pom "DEADLINE")
5290 ts-date-type " deadline"))
5291 ((org-em 'ts-up 'ts-down
5292 org-agenda-sorting-strategy-selected)
5293 (setq ts (org-entry-get pom "TIMESTAMP")
5294 ts-date-type " timestamp"))
5295 ((org-em 'tsia-up 'tsia-down
5296 org-agenda-sorting-strategy-selected)
5297 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5298 ts-date-type " timestamp_ia"))
5299 ((org-em 'timestamp-up 'timestamp-down
5300 org-agenda-sorting-strategy-selected)
5301 (setq ts (or (org-entry-get pom "SCHEDULED")
5302 (org-entry-get pom "DEADLINE")
5303 (org-entry-get pom "TIMESTAMP")
5304 (org-entry-get pom "TIMESTAMP_IA"))
5305 ts-date-type ""))
5306 (t (setq ts-date-type "")))
5307 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5308 ts-date-type))))
5310 (defun org-agenda-get-todos ()
5311 "Return the TODO information for agenda display."
5312 (let* ((props (list 'face nil
5313 'done-face 'org-agenda-done
5314 'org-not-done-regexp org-not-done-regexp
5315 'org-todo-regexp org-todo-regexp
5316 'org-complex-heading-regexp org-complex-heading-regexp
5317 'mouse-face 'highlight
5318 'help-echo
5319 (format "mouse-2 or RET jump to org file %s"
5320 (abbreviate-file-name buffer-file-name))))
5321 (case-fold-search nil)
5322 (regexp (format org-heading-keyword-regexp-format
5323 (cond
5324 ((and org-select-this-todo-keyword
5325 (equal org-select-this-todo-keyword "*"))
5326 org-todo-regexp)
5327 (org-select-this-todo-keyword
5328 (concat "\\("
5329 (mapconcat 'identity
5330 (org-split-string
5331 org-select-this-todo-keyword
5332 "|")
5333 "\\|") "\\)"))
5334 (t org-not-done-regexp))))
5335 marker priority category level tags todo-state
5336 ts-date ts-date-type ts-date-pair
5337 ee txt beg end inherited-tags todo-state-end-pos)
5338 (goto-char (point-min))
5339 (while (re-search-forward regexp nil t)
5340 (catch :skip
5341 (save-match-data
5342 (beginning-of-line)
5343 (org-agenda-skip)
5344 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5345 (unless (and (setq todo-state (org-get-todo-state))
5346 (setq todo-state-end-pos (match-end 2)))
5347 (goto-char end)
5348 (throw :skip nil))
5349 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5350 (goto-char (1+ beg))
5351 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5352 (throw :skip nil)))
5353 (goto-char (match-beginning 2))
5354 (setq marker (org-agenda-new-marker (match-beginning 0))
5355 category (org-get-category)
5356 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5357 ts-date (car ts-date-pair)
5358 ts-date-type (cdr ts-date-pair)
5359 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5360 inherited-tags
5361 (or (eq org-agenda-show-inherited-tags 'always)
5362 (and (listp org-agenda-show-inherited-tags)
5363 (memq 'todo org-agenda-show-inherited-tags))
5364 (and (eq org-agenda-show-inherited-tags t)
5365 (or (eq org-agenda-use-tag-inheritance t)
5366 (memq 'todo org-agenda-use-tag-inheritance))))
5367 tags (org-get-tags-at nil (not inherited-tags))
5368 level (make-string (org-reduced-level (org-outline-level)) ? )
5369 txt (org-agenda-format-item "" txt level category tags t)
5370 priority (1+ (org-get-priority txt)))
5371 (org-add-props txt props
5372 'org-marker marker 'org-hd-marker marker
5373 'priority priority
5374 'level level
5375 'ts-date ts-date
5376 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5377 (push txt ee)
5378 (if org-agenda-todo-list-sublevels
5379 (goto-char todo-state-end-pos)
5380 (org-end-of-subtree 'invisible))))
5381 (nreverse ee)))
5383 (defun org-agenda-todo-custom-ignore-p (time n)
5384 "Check whether timestamp is farther away than n number of days.
5385 This function is invoked if `org-agenda-todo-ignore-deadlines',
5386 `org-agenda-todo-ignore-scheduled' or
5387 `org-agenda-todo-ignore-timestamp' is set to an integer."
5388 (let ((days (org-time-stamp-to-now
5389 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5390 (if (>= n 0)
5391 (>= days n)
5392 (<= days n))))
5394 ;;;###autoload
5395 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5396 (&optional end)
5397 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5398 (when (or org-agenda-todo-ignore-with-date
5399 org-agenda-todo-ignore-scheduled
5400 org-agenda-todo-ignore-deadlines
5401 org-agenda-todo-ignore-timestamp)
5402 (setq end (or end (save-excursion (outline-next-heading) (point))))
5403 (save-excursion
5404 (or (and org-agenda-todo-ignore-with-date
5405 (re-search-forward org-ts-regexp end t))
5406 (and org-agenda-todo-ignore-scheduled
5407 (re-search-forward org-scheduled-time-regexp end t)
5408 (cond
5409 ((eq org-agenda-todo-ignore-scheduled 'future)
5410 (> (org-time-stamp-to-now
5411 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5412 ((eq org-agenda-todo-ignore-scheduled 'past)
5413 (<= (org-time-stamp-to-now
5414 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5415 ((numberp org-agenda-todo-ignore-scheduled)
5416 (org-agenda-todo-custom-ignore-p
5417 (match-string 1) org-agenda-todo-ignore-scheduled))
5418 (t)))
5419 (and org-agenda-todo-ignore-deadlines
5420 (re-search-forward org-deadline-time-regexp end t)
5421 (cond
5422 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5423 ((eq org-agenda-todo-ignore-deadlines 'far)
5424 (not (org-deadline-close-p (match-string 1))))
5425 ((eq org-agenda-todo-ignore-deadlines 'future)
5426 (> (org-time-stamp-to-now
5427 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5428 ((eq org-agenda-todo-ignore-deadlines 'past)
5429 (<= (org-time-stamp-to-now
5430 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5431 ((numberp org-agenda-todo-ignore-deadlines)
5432 (org-agenda-todo-custom-ignore-p
5433 (match-string 1) org-agenda-todo-ignore-deadlines))
5434 (t (org-deadline-close-p (match-string 1)))))
5435 (and org-agenda-todo-ignore-timestamp
5436 (let ((buffer (current-buffer))
5437 (regexp
5438 (concat
5439 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5440 (start (point)))
5441 ;; Copy current buffer into a temporary one
5442 (with-temp-buffer
5443 (insert-buffer-substring buffer start end)
5444 (goto-char (point-min))
5445 ;; Delete SCHEDULED and DEADLINE items
5446 (while (re-search-forward regexp end t)
5447 (delete-region (match-beginning 0) (match-end 0)))
5448 (goto-char (point-min))
5449 ;; No search for timestamp left
5450 (when (re-search-forward org-ts-regexp nil t)
5451 (cond
5452 ((eq org-agenda-todo-ignore-timestamp 'future)
5453 (> (org-time-stamp-to-now
5454 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5455 ((eq org-agenda-todo-ignore-timestamp 'past)
5456 (<= (org-time-stamp-to-now
5457 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5458 ((numberp org-agenda-todo-ignore-timestamp)
5459 (org-agenda-todo-custom-ignore-p
5460 (match-string 1) org-agenda-todo-ignore-timestamp))
5461 (t))))))))))
5463 (defun org-agenda-get-timestamps (&optional deadlines)
5464 "Return the date stamp information for agenda display.
5465 Optional argument DEADLINES is a list of deadline items to be
5466 displayed in agenda view."
5467 (let* ((props (list 'face 'org-agenda-calendar-event
5468 'org-not-done-regexp org-not-done-regexp
5469 'org-todo-regexp org-todo-regexp
5470 'org-complex-heading-regexp org-complex-heading-regexp
5471 'mouse-face 'highlight
5472 'help-echo
5473 (format "mouse-2 or RET jump to Org file %s"
5474 (abbreviate-file-name buffer-file-name))))
5475 (current (calendar-absolute-from-gregorian date))
5476 (today (org-today))
5477 (deadline-position-alist
5478 (mapcar (lambda (d)
5479 (let ((m (get-text-property 0 'org-hd-marker d)))
5480 (and m (marker-position m))))
5481 deadlines))
5482 ;; Match time-stamps set to current date, time-stamps with
5483 ;; a repeater, and S-exp time-stamps.
5484 (regexp
5485 (concat
5486 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5487 (regexp-quote
5488 (substring
5489 (format-time-string
5490 (car org-time-stamp-formats)
5491 (apply #'encode-time ; DATE bound by calendar
5492 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5493 1 11))
5494 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5495 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5496 timestamp-items)
5497 (goto-char (point-min))
5498 (while (re-search-forward regexp nil t)
5499 ;; Skip date ranges, scheduled and deadlines, which are handled
5500 ;; specially. Also skip time-stamps before first headline as
5501 ;; there would be no entry to add to the agenda. Eventually,
5502 ;; ignore clock entries.
5503 (catch :skip
5504 (save-match-data
5505 (when (or (org-at-date-range-p)
5506 (org-at-planning-p)
5507 (org-before-first-heading-p)
5508 (and org-agenda-include-inactive-timestamps
5509 (org-at-clock-log-p)))
5510 (throw :skip nil))
5511 (org-agenda-skip))
5512 (let* ((pos (match-beginning 0))
5513 (repeat (match-string 1))
5514 (sexp-entry (match-string 3))
5515 (time-stamp (if (or repeat sexp-entry) (match-string 0)
5516 (save-excursion
5517 (goto-char pos)
5518 (looking-at org-ts-regexp-both)
5519 (match-string 0))))
5520 (todo-state (org-get-todo-state))
5521 (warntime (get-text-property (point) 'org-appt-warntime))
5522 (done? (member todo-state org-done-keywords)))
5523 ;; Possibly skip done tasks.
5524 (when (and done? org-agenda-skip-timestamp-if-done)
5525 (throw :skip t))
5526 ;; S-exp entry doesn't match current day: skip it.
5527 (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
5528 (throw :skip nil))
5529 (when repeat
5530 (let* ((past
5531 ;; A repeating time stamp is shown at its base
5532 ;; date and every repeated date up to TODAY. If
5533 ;; `org-agenda-prefer-last-repeat' is non-nil,
5534 ;; however, only the last repeat before today
5535 ;; (inclusive) is shown.
5536 (org-agenda--timestamp-to-absolute
5537 repeat
5538 (if (or (> current today)
5539 (eq org-agenda-prefer-last-repeat t)
5540 (member todo-state org-agenda-prefer-last-repeat))
5541 today
5542 current)
5543 'past (current-buffer) pos))
5544 (future
5545 ;; Display every repeated date past TODAY
5546 ;; (exclusive) unless
5547 ;; `org-agenda-show-future-repeats' is nil. If
5548 ;; this variable is set to `next', only display
5549 ;; the first repeated date after TODAY
5550 ;; (exclusive).
5551 (cond
5552 ((<= current today) past)
5553 ((not org-agenda-show-future-repeats) past)
5555 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5556 (1+ today)
5557 current)))
5558 (org-agenda--timestamp-to-absolute
5559 repeat base 'future (current-buffer) pos))))))
5560 (when (and (/= current past) (/= current future))
5561 (throw :skip nil))))
5562 (save-excursion
5563 (re-search-backward org-outline-regexp-bol nil t)
5564 ;; Possibly skip time-stamp when a deadline is set.
5565 (when (and org-agenda-skip-timestamp-if-deadline-is-shown
5566 (assq (point) deadline-position-alist))
5567 (throw :skip nil))
5568 (let* ((category (org-get-category pos))
5569 (inherited-tags
5570 (or (eq org-agenda-show-inherited-tags 'always)
5571 (and (consp org-agenda-show-inherited-tags)
5572 (memq 'agenda org-agenda-show-inherited-tags))
5573 (and (eq org-agenda-show-inherited-tags t)
5574 (or (eq org-agenda-use-tag-inheritance t)
5575 (memq 'agenda
5576 org-agenda-use-tag-inheritance)))))
5577 (tags (org-get-tags-at nil (not inherited-tags)))
5578 (level (make-string (org-reduced-level (org-outline-level))
5579 ?\s))
5580 (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
5581 (match-string 1)))
5582 (inactive? (= (char-after pos) ?\[))
5583 (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
5584 (item
5585 (org-agenda-format-item
5586 (and inactive? org-agenda-inactive-leader)
5587 head level category tags time-stamp org-ts-regexp habit?)))
5588 (org-add-props item props
5589 'priority (if habit?
5590 (org-habit-get-priority (org-habit-parse-todo))
5591 (org-get-priority item))
5592 'org-marker (org-agenda-new-marker pos)
5593 'org-hd-marker (org-agenda-new-marker)
5594 'date date
5595 'level level
5596 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
5597 current)
5598 'todo-state todo-state
5599 'warntime warntime
5600 'type "timestamp")
5601 (push item timestamp-items))))
5602 (when org-agenda-skip-additional-timestamps-same-entry
5603 (outline-next-heading))))
5604 (nreverse timestamp-items)))
5606 (defun org-agenda-get-sexps ()
5607 "Return the sexp information for agenda display."
5608 (require 'diary-lib)
5609 (let* ((props (list 'face 'org-agenda-calendar-sexp
5610 'mouse-face 'highlight
5611 'help-echo
5612 (format "mouse-2 or RET jump to org file %s"
5613 (abbreviate-file-name buffer-file-name))))
5614 (regexp "^&?%%(")
5615 marker category extra level ee txt tags entry
5616 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5617 (goto-char (point-min))
5618 (while (re-search-forward regexp nil t)
5619 (catch :skip
5620 (org-agenda-skip)
5621 (setq beg (match-beginning 0))
5622 (goto-char (1- (match-end 0)))
5623 (setq b (point))
5624 (forward-sexp 1)
5625 (setq sexp (buffer-substring b (point)))
5626 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5627 (org-trim (match-string 1))
5628 ""))
5629 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5630 (when result
5631 (setq marker (org-agenda-new-marker beg)
5632 level (make-string (org-reduced-level (org-outline-level)) ? )
5633 category (org-get-category beg)
5634 inherited-tags
5635 (or (eq org-agenda-show-inherited-tags 'always)
5636 (and (listp org-agenda-show-inherited-tags)
5637 (memq 'agenda org-agenda-show-inherited-tags))
5638 (and (eq org-agenda-show-inherited-tags t)
5639 (or (eq org-agenda-use-tag-inheritance t)
5640 (memq 'agenda org-agenda-use-tag-inheritance))))
5641 tags (org-get-tags-at nil (not inherited-tags))
5642 todo-state (org-get-todo-state)
5643 warntime (get-text-property (point) 'org-appt-warntime)
5644 extra nil)
5646 (dolist (r (if (stringp result)
5647 (list result)
5648 result)) ;; we expect a list here
5649 (when (and org-agenda-diary-sexp-prefix
5650 (string-match org-agenda-diary-sexp-prefix r))
5651 (setq extra (match-string 0 r)
5652 r (replace-match "" nil nil r)))
5653 (if (string-match "\\S-" r)
5654 (setq txt r)
5655 (setq txt "SEXP entry returned empty string"))
5656 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5657 (org-add-props txt props 'org-marker marker
5658 'date date 'todo-state todo-state
5659 'level level 'type "sexp" 'warntime warntime)
5660 (push txt ee)))))
5661 (nreverse ee)))
5663 ;; Calendar sanity: define some functions that are independent of
5664 ;; `calendar-date-style'.
5665 (defun org-anniversary (year month day &optional mark)
5666 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5667 (with-no-warnings
5668 (let ((calendar-date-style 'iso))
5669 (diary-anniversary year month day mark))))
5670 (defun org-cyclic (N year month day &optional mark)
5671 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5672 (with-no-warnings
5673 (let ((calendar-date-style 'iso))
5674 (diary-cyclic N year month day mark))))
5675 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5676 "Like `diary-block', but with fixed (ISO) order of arguments."
5677 (with-no-warnings
5678 (let ((calendar-date-style 'iso))
5679 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5680 (defun org-date (year month day &optional mark)
5681 "Like `diary-date', but with fixed (ISO) order of arguments."
5682 (with-no-warnings
5683 (let ((calendar-date-style 'iso))
5684 (diary-date year month day mark))))
5686 ;; Define the `org-class' function
5687 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5688 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5689 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5690 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5691 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5692 `holidays', then any date that is known by the Emacs calendar to be a
5693 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5694 then those holidays will be skipped."
5695 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5696 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5697 (d (calendar-absolute-from-gregorian date))
5698 (h (when skip-weeks (calendar-check-holidays date))))
5699 (and
5700 (<= date1 d)
5701 (<= d date2)
5702 (= (calendar-day-of-week date) dayname)
5703 (or (not skip-weeks)
5704 (progn
5705 (require 'cal-iso)
5706 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5707 (not (or (and h (memq 'holidays skip-weeks))
5708 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5709 entry)))
5711 (defalias 'org-get-closed 'org-agenda-get-progress)
5712 (defun org-agenda-get-progress ()
5713 "Return the logged TODO entries for agenda display."
5714 (let* ((props (list 'mouse-face 'highlight
5715 'org-not-done-regexp org-not-done-regexp
5716 'org-todo-regexp org-todo-regexp
5717 'org-complex-heading-regexp org-complex-heading-regexp
5718 'help-echo
5719 (format "mouse-2 or RET jump to org file %s"
5720 (abbreviate-file-name buffer-file-name))))
5721 (items (if (consp org-agenda-show-log-scoped)
5722 org-agenda-show-log-scoped
5723 (if (eq org-agenda-show-log-scoped 'clockcheck)
5724 '(clock)
5725 org-agenda-log-mode-items)))
5726 (parts
5727 (delq nil
5728 (list
5729 (if (memq 'closed items) (concat "\\<" org-closed-string))
5730 (if (memq 'clock items) (concat "\\<" org-clock-string))
5731 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5732 (parts-re (if parts (mapconcat 'identity parts "\\|")
5733 (error "`org-agenda-log-mode-items' is empty")))
5734 (regexp (concat
5735 "\\(" parts-re "\\)"
5736 " *\\["
5737 (regexp-quote
5738 (substring
5739 (format-time-string
5740 (car org-time-stamp-formats)
5741 (apply 'encode-time ; DATE bound by calendar
5742 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5743 1 11))))
5744 (org-agenda-search-headline-for-time nil)
5745 marker hdmarker priority category level tags closedp
5746 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5747 (goto-char (point-min))
5748 (while (re-search-forward regexp nil t)
5749 (catch :skip
5750 (org-agenda-skip)
5751 (setq marker (org-agenda-new-marker (match-beginning 0))
5752 closedp (equal (match-string 1) org-closed-string)
5753 statep (equal (string-to-char (match-string 1)) ?-)
5754 clockp (not (or closedp statep))
5755 state (and statep (match-string 2))
5756 category (org-get-category (match-beginning 0))
5757 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5758 (when (string-match "\\]" timestr)
5759 ;; substring should only run to end of time stamp
5760 (setq rest (substring timestr (match-end 0))
5761 timestr (substring timestr 0 (match-end 0)))
5762 (if (and (not closedp) (not statep)
5763 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5764 rest))
5765 (progn (setq timestr (concat (substring timestr 0 -1)
5766 "-" (match-string 1 rest) "]"))
5767 (setq clocked (match-string 2 rest)))
5768 (setq clocked "-")))
5769 (save-excursion
5770 (setq extra
5771 (cond
5772 ((not org-agenda-log-mode-add-notes) nil)
5773 (statep
5774 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5775 (match-string 1)))
5776 (clockp
5777 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5778 (match-string 1)))))
5779 (if (not (re-search-backward org-outline-regexp-bol nil t))
5780 (throw :skip nil)
5781 (goto-char (match-beginning 0))
5782 (setq hdmarker (org-agenda-new-marker)
5783 inherited-tags
5784 (or (eq org-agenda-show-inherited-tags 'always)
5785 (and (listp org-agenda-show-inherited-tags)
5786 (memq 'todo org-agenda-show-inherited-tags))
5787 (and (eq org-agenda-show-inherited-tags t)
5788 (or (eq org-agenda-use-tag-inheritance t)
5789 (memq 'todo org-agenda-use-tag-inheritance))))
5790 tags (org-get-tags-at nil (not inherited-tags))
5791 level (make-string (org-reduced-level (org-outline-level)) ? ))
5792 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5793 (setq txt (match-string 1))
5794 (when extra
5795 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5796 (setq txt (concat (substring txt 0 (match-beginning 1))
5797 " - " extra " " (match-string 2 txt)))
5798 (setq txt (concat txt " - " extra))))
5799 (setq txt (org-agenda-format-item
5800 (cond
5801 (closedp "Closed: ")
5802 (statep (concat "State: (" state ")"))
5803 (t (concat "Clocked: (" clocked ")")))
5804 txt level category tags timestr)))
5805 (setq priority 100000)
5806 (org-add-props txt props
5807 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5808 'priority priority 'level level
5809 'type "closed" 'date date
5810 'undone-face 'org-warning 'done-face 'org-agenda-done)
5811 (push txt ee))
5812 (goto-char (point-at-eol))))
5813 (nreverse ee)))
5815 (defun org-agenda-show-clocking-issues ()
5816 "Add overlays, showing issues with clocking.
5817 See also the user option `org-agenda-clock-consistency-checks'."
5818 (interactive)
5819 (let* ((pl org-agenda-clock-consistency-checks)
5820 (re (concat "^[ \t]*"
5821 org-clock-string
5822 "[ \t]+"
5823 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5824 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5825 (tlstart 0.)
5826 (tlend 0.)
5827 (maxtime (org-duration-to-minutes
5828 (or (plist-get pl :max-duration) "24:00")))
5829 (mintime (org-duration-to-minutes
5830 (or (plist-get pl :min-duration) 0)))
5831 (maxgap (org-duration-to-minutes
5832 ;; default 30:00 means never complain
5833 (or (plist-get pl :max-gap) "30:00")))
5834 (gapok (mapcar #'org-duration-to-minutes
5835 (plist-get pl :gap-ok-around)))
5836 (def-face (or (plist-get pl :default-face)
5837 '((:background "DarkRed") (:foreground "white"))))
5838 issue face m te ts dt ov)
5839 (goto-char (point-min))
5840 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5841 (setq issue nil face def-face)
5842 (catch 'next
5843 (setq m (org-get-at-bol 'org-marker)
5844 te nil ts nil)
5845 (unless (and m (markerp m))
5846 (setq issue "No valid clock line") (throw 'next t))
5847 (org-with-point-at m
5848 (save-excursion
5849 (goto-char (point-at-bol))
5850 (unless (looking-at re)
5851 (error "No valid Clock line")
5852 (throw 'next t))
5853 (unless (match-end 3)
5854 (setq issue "No end time"
5855 face (or (plist-get pl :no-end-time-face) face))
5856 (throw 'next t))
5857 (setq ts (match-string 1)
5858 te (match-string 3)
5859 ts (float-time
5860 (apply #'encode-time (org-parse-time-string ts)))
5861 te (float-time
5862 (apply #'encode-time (org-parse-time-string te)))
5863 dt (- te ts))))
5864 (cond
5865 ((> dt (* 60 maxtime))
5866 ;; a very long clocking chunk
5867 (setq issue (format "Clocking interval is very long: %s"
5868 (org-duration-from-minutes (floor (/ dt 60.))))
5869 face (or (plist-get pl :long-face) face)))
5870 ((< dt (* 60 mintime))
5871 ;; a very short clocking chunk
5872 (setq issue (format "Clocking interval is very short: %s"
5873 (org-duration-from-minutes (floor (/ dt 60.))))
5874 face (or (plist-get pl :short-face) face)))
5875 ((and (> tlend 0) (< ts tlend))
5876 ;; Two clock entries are overlapping
5877 (setq issue (format "Clocking overlap: %d minutes"
5878 (/ (- tlend ts) 60))
5879 face (or (plist-get pl :overlap-face) face)))
5880 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5881 ;; There is a gap, lets see if we need to report it
5882 (unless (org-agenda-check-clock-gap tlend ts gapok)
5883 (setq issue (format "Clocking gap: %d minutes"
5884 (/ (- ts tlend) 60))
5885 face (or (plist-get pl :gap-face) face))))
5886 (t nil)))
5887 (setq tlend (or te tlend) tlstart (or ts tlstart))
5888 (when issue
5889 ;; OK, there was some issue, add an overlay to show the issue
5890 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5891 (overlay-put ov 'before-string
5892 (concat
5893 (org-add-props
5894 (format "%-43s" (concat " " issue))
5896 'face face)
5897 "\n"))
5898 (overlay-put ov 'evaporate t)))))
5900 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5901 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5902 (catch 'exit
5903 (unless ok-list
5904 ;; there are no OK times for gaps...
5905 (throw 'exit nil))
5906 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5907 ;; This is more than 24 hours, so it is OK.
5908 ;; because we have at least one OK time, that must be in the
5909 ;; 24 hour interval.
5910 (throw 'exit t))
5911 ;; We have a shorter gap.
5912 ;; Now we have to get the minute of the day when these times are
5913 (let* ((t1dec (decode-time (seconds-to-time t1)))
5914 (t2dec (decode-time (seconds-to-time t2)))
5915 ;; compute the minute on the day
5916 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5917 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5918 (when (< min2 min1)
5919 ;; if min2 is smaller than min1, this means it is on the next day.
5920 ;; Wrap it to after midnight.
5921 (setq min2 (+ min2 1440)))
5922 ;; Now check if any of the OK times is in the gap
5923 (mapc (lambda (x)
5924 ;; Wrap the time to after midnight if necessary
5925 (if (< x min1) (setq x (+ x 1440)))
5926 ;; Check if in interval
5927 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5928 ok-list)
5929 ;; Nope, this gap is not OK
5930 nil)))
5932 (defun org-agenda-get-deadlines (&optional with-hour)
5933 "Return the deadline information for agenda display.
5934 When WITH-HOUR is non-nil, only return deadlines with an hour
5935 specification like [h]h:mm."
5936 (let* ((props (list 'mouse-face 'highlight
5937 'org-not-done-regexp org-not-done-regexp
5938 'org-todo-regexp org-todo-regexp
5939 'org-complex-heading-regexp org-complex-heading-regexp
5940 'help-echo
5941 (format "mouse-2 or RET jump to org file %s"
5942 (abbreviate-file-name buffer-file-name))))
5943 (regexp (if with-hour
5944 org-deadline-time-hour-regexp
5945 org-deadline-time-regexp))
5946 (today (org-today))
5947 (today? (org-agenda-today-p date)) ; DATE bound by calendar.
5948 (current (calendar-absolute-from-gregorian date))
5949 deadline-items)
5950 (goto-char (point-min))
5951 (while (re-search-forward regexp nil t)
5952 (catch :skip
5953 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
5954 (org-agenda-skip)
5955 (let* ((s (match-string 1))
5956 (pos (1- (match-beginning 1)))
5957 (todo-state (save-match-data (org-get-todo-state)))
5958 (done? (member todo-state org-done-keywords))
5959 (sexp? (string-prefix-p "%%" s))
5960 ;; DEADLINE is the deadline date for the entry. It is
5961 ;; either the base date or the last repeat, according
5962 ;; to `org-agenda-prefer-last-repeat'.
5963 (deadline
5964 (cond
5965 (sexp? (org-agenda--timestamp-to-absolute s current))
5966 ((or (eq org-agenda-prefer-last-repeat t)
5967 (member todo-state org-agenda-prefer-last-repeat))
5968 (org-agenda--timestamp-to-absolute
5969 s today 'past (current-buffer) pos))
5970 (t (org-agenda--timestamp-to-absolute s))))
5971 ;; REPEAT is the future repeat closest from CURRENT,
5972 ;; according to `org-agenda-show-future-repeats'. If
5973 ;; the latter is nil, or if the time stamp has no
5974 ;; repeat part, default to DEADLINE.
5975 (repeat
5976 (cond
5977 (sexp? deadline)
5978 ((<= current today) deadline)
5979 ((not org-agenda-show-future-repeats) deadline)
5981 (let ((base (if (eq org-agenda-show-future-repeats 'next)
5982 (1+ today)
5983 current)))
5984 (org-agenda--timestamp-to-absolute
5985 s base 'future (current-buffer) pos)))))
5986 (diff (- deadline current))
5987 (suppress-prewarning
5988 (let ((scheduled
5989 (and org-agenda-skip-deadline-prewarning-if-scheduled
5990 (org-entry-get nil "SCHEDULED"))))
5991 (cond
5992 ((not scheduled) nil)
5993 ;; The current item has a scheduled date, so
5994 ;; evaluate its prewarning lead time.
5995 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5996 ;; Use global prewarning-restart lead time.
5997 org-agenda-skip-deadline-prewarning-if-scheduled)
5998 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
5999 'pre-scheduled)
6000 ;; Set pre-warning to no earlier than SCHEDULED.
6001 (min (- deadline
6002 (org-agenda--timestamp-to-absolute scheduled))
6003 org-deadline-warning-days))
6004 ;; Set pre-warning to deadline.
6005 (t 0))))
6006 (wdays (if suppress-prewarning
6007 (let ((org-deadline-warning-days suppress-prewarning))
6008 (org-get-wdays s))
6009 (org-get-wdays s))))
6010 (cond
6011 ;; Only display deadlines at their base date, at future
6012 ;; repeat occurrences or in today agenda.
6013 ((= current deadline) nil)
6014 ((= current repeat) nil)
6015 ((not today?) (throw :skip nil))
6016 ;; Upcoming deadline: display within warning period WDAYS.
6017 ((> deadline current) (when (> diff wdays) (throw :skip nil)))
6018 ;; Overdue deadline: warn about it for
6019 ;; `org-deadline-past-days' duration.
6020 (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
6021 ;; Possibly skip done tasks.
6022 (when (and done?
6023 (or org-agenda-skip-deadline-if-done
6024 (/= deadline current)))
6025 (throw :skip nil))
6026 (save-excursion
6027 (re-search-backward "^\\*+[ \t]+" nil t)
6028 (goto-char (match-end 0))
6029 (let* ((category (org-get-category))
6030 (level (make-string (org-reduced-level (org-outline-level))
6031 ?\s))
6032 (head (buffer-substring (point) (line-end-position)))
6033 (inherited-tags
6034 (or (eq org-agenda-show-inherited-tags 'always)
6035 (and (listp org-agenda-show-inherited-tags)
6036 (memq 'agenda org-agenda-show-inherited-tags))
6037 (and (eq org-agenda-show-inherited-tags t)
6038 (or (eq org-agenda-use-tag-inheritance t)
6039 (memq 'agenda
6040 org-agenda-use-tag-inheritance)))))
6041 (tags (org-get-tags-at nil (not inherited-tags)))
6042 (time
6043 (cond
6044 ;; No time of day designation if it is only
6045 ;; a reminder.
6046 ((and (/= current deadline) (/= current repeat)) nil)
6047 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6048 (concat (substring s (match-beginning 1)) " "))
6049 (t 'time)))
6050 (item
6051 (org-agenda-format-item
6052 ;; Insert appropriate suffixes before deadlines.
6053 ;; Those only apply to today agenda.
6054 (pcase-let ((`(,now ,future ,past)
6055 org-agenda-deadline-leaders))
6056 (cond
6057 ((and today? (< deadline today)) (format past (- diff)))
6058 ((and today? (> deadline today)) (format future diff))
6059 (t now)))
6060 head level category tags time))
6061 (face (org-agenda-deadline-face
6062 (- 1 (/ (float diff) (max wdays 1)))))
6063 (upcoming? (and today? (> deadline today)))
6064 (warntime (get-text-property (point) 'org-appt-warntime)))
6065 (org-add-props item props
6066 'org-marker (org-agenda-new-marker pos)
6067 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6068 'warntime warntime
6069 'level level
6070 'ts-date deadline
6071 'priority
6072 ;; Adjust priority to today reminders about deadlines.
6073 ;; Overdue deadlines get the highest priority
6074 ;; increase, then imminent deadlines and eventually
6075 ;; more distant deadlines.
6076 (let ((adjust (if today? (- diff) 0)))
6077 (+ adjust (org-get-priority item)))
6078 'todo-state todo-state
6079 'type (if upcoming? "upcoming-deadline" "deadline")
6080 'date (if upcoming? date deadline)
6081 'face (if done? 'org-agenda-done face)
6082 'undone-face face
6083 'done-face 'org-agenda-done)
6084 (push item deadline-items))))))
6085 (nreverse deadline-items)))
6087 (defun org-agenda-deadline-face (fraction)
6088 "Return the face to displaying a deadline item.
6089 FRACTION is what fraction of the head-warning time has passed."
6090 (assoc-default fraction org-agenda-deadline-faces #'<=))
6092 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6093 "Return the scheduled information for agenda display.
6094 Optional argument DEADLINES is a list of deadline items to be
6095 displayed in agenda view. When WITH-HOUR is non-nil, only return
6096 scheduled items with an hour specification like [h]h:mm."
6097 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6098 'org-todo-regexp org-todo-regexp
6099 'org-complex-heading-regexp org-complex-heading-regexp
6100 'done-face 'org-agenda-done
6101 'mouse-face 'highlight
6102 'help-echo
6103 (format "mouse-2 or RET jump to Org file %s"
6104 (abbreviate-file-name buffer-file-name))))
6105 (regexp (if with-hour
6106 org-scheduled-time-hour-regexp
6107 org-scheduled-time-regexp))
6108 (today (org-today))
6109 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6110 (current (calendar-absolute-from-gregorian date))
6111 (deadline-pos
6112 (mapcar (lambda (d)
6113 (let ((m (get-text-property 0 'org-hd-marker d)))
6114 (and m (marker-position m))))
6115 deadlines))
6116 scheduled-items)
6117 (goto-char (point-min))
6118 (while (re-search-forward regexp nil t)
6119 (catch :skip
6120 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6121 (org-agenda-skip)
6122 (let* ((s (match-string 1))
6123 (pos (1- (match-beginning 1)))
6124 (todo-state (save-match-data (org-get-todo-state)))
6125 (donep (member todo-state org-done-keywords))
6126 (sexp? (string-prefix-p "%%" s))
6127 ;; SCHEDULE is the scheduled date for the entry. It is
6128 ;; either the bare date or the last repeat, according
6129 ;; to `org-agenda-prefer-last-repeat'.
6130 (schedule
6131 (cond
6132 (sexp? (org-agenda--timestamp-to-absolute s current))
6133 ((or (eq org-agenda-prefer-last-repeat t)
6134 (member todo-state org-agenda-prefer-last-repeat))
6135 (org-agenda--timestamp-to-absolute
6136 s today 'past (current-buffer) pos))
6137 (t (org-agenda--timestamp-to-absolute s))))
6138 ;; REPEAT is the future repeat closest from CURRENT,
6139 ;; according to `org-agenda-show-future-repeats'. If
6140 ;; the latter is nil, or if the time stamp has no
6141 ;; repeat part, default to SCHEDULE.
6142 (repeat
6143 (cond
6144 (sexp? schedule)
6145 ((<= current today) schedule)
6146 ((not org-agenda-show-future-repeats) schedule)
6148 (let ((base (if (eq org-agenda-show-future-repeats 'next)
6149 (1+ today)
6150 current)))
6151 (org-agenda--timestamp-to-absolute
6152 s base 'future (current-buffer) pos)))))
6153 (diff (- current schedule))
6154 (warntime (get-text-property (point) 'org-appt-warntime))
6155 (pastschedp (< schedule today))
6156 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6157 (suppress-delay
6158 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6159 (org-entry-get nil "DEADLINE"))))
6160 (cond
6161 ((not deadline) nil)
6162 ;; The current item has a deadline date, so
6163 ;; evaluate its delay time.
6164 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6165 ;; Use global delay time.
6166 (- org-agenda-skip-scheduled-delay-if-deadline))
6167 ((eq org-agenda-skip-scheduled-delay-if-deadline
6168 'post-deadline)
6169 ;; Set delay to no later than DEADLINE.
6170 (min (- schedule
6171 (org-agenda--timestamp-to-absolute deadline))
6172 org-scheduled-delay-days))
6173 (t 0))))
6174 (ddays
6175 (cond
6176 ;; Nullify delay when a repeater triggered already
6177 ;; and the delay is of the form --Xd.
6178 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6179 (> current schedule))
6181 (suppress-delay
6182 (let ((org-scheduled-delay-days suppress-delay))
6183 (org-get-wdays s t t)))
6184 (t (org-get-wdays s t)))))
6185 ;; Display scheduled items at base date (SCHEDULE), today if
6186 ;; scheduled before the current date, and at any repeat past
6187 ;; today. However, skip delayed items and items that have
6188 ;; been displayed for more than `org-scheduled-past-days'.
6189 (unless (and todayp
6190 habitp
6191 (bound-and-true-p org-habit-show-all-today))
6192 (when (or (and (> ddays 0) (< diff ddays))
6193 (> diff org-scheduled-past-days)
6194 (> schedule current)
6195 (and (/= current schedule)
6196 (/= current today)
6197 (/= current repeat)))
6198 (throw :skip nil)))
6199 ;; Possibly skip done tasks.
6200 (when (and donep
6201 (or org-agenda-skip-scheduled-if-done
6202 (/= schedule current)))
6203 (throw :skip nil))
6204 ;; Skip entry if it already appears as a deadline, per
6205 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6206 ;; doesn't apply to habits.
6207 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6208 ((guard
6209 (or (not (memq (line-beginning-position 0) deadline-pos))
6210 habitp))
6211 nil)
6212 (`repeated-after-deadline
6213 (let ((deadline (time-to-days
6214 (org-get-deadline-time (point)))))
6215 (and (<= schedule deadline) (> current deadline))))
6216 (`not-today pastschedp)
6217 (`t t)
6218 (_ nil))
6219 (throw :skip nil))
6220 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6221 ;; only show them for today. Also skip done habits.
6222 (when (and habitp
6223 (or donep
6224 (not (bound-and-true-p org-habit-show-habits))
6225 (and (not todayp)
6226 (bound-and-true-p
6227 org-habit-show-habits-only-for-today))))
6228 (throw :skip nil))
6229 (save-excursion
6230 (re-search-backward "^\\*+[ \t]+" nil t)
6231 (goto-char (match-end 0))
6232 (let* ((category (org-get-category))
6233 (inherited-tags
6234 (or (eq org-agenda-show-inherited-tags 'always)
6235 (and (listp org-agenda-show-inherited-tags)
6236 (memq 'agenda org-agenda-show-inherited-tags))
6237 (and (eq org-agenda-show-inherited-tags t)
6238 (or (eq org-agenda-use-tag-inheritance t)
6239 (memq 'agenda
6240 org-agenda-use-tag-inheritance)))))
6241 (tags (org-get-tags-at nil (not inherited-tags)))
6242 (level (make-string (org-reduced-level (org-outline-level))
6243 ?\s))
6244 (head (buffer-substring (point) (line-end-position)))
6245 (time
6246 (cond
6247 ;; No time of day designation if it is only
6248 ;; a reminder.
6249 ((and (/= current schedule) (/= current repeat)) nil)
6250 ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6251 (concat (substring s (match-beginning 1)) " "))
6252 (t 'time)))
6253 (item
6254 (org-agenda-format-item
6255 (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
6256 ;; Show a reminder of a past scheduled today.
6257 (if (and todayp pastschedp)
6258 (format past diff)
6259 first))
6260 head level category tags time nil habitp))
6261 (face (cond ((and (not habitp) pastschedp)
6262 'org-scheduled-previously)
6263 (todayp 'org-scheduled-today)
6264 (t 'org-scheduled)))
6265 (habitp (and habitp (org-habit-parse-todo))))
6266 (org-add-props item props
6267 'undone-face face
6268 'face (if donep 'org-agenda-done face)
6269 'org-marker (org-agenda-new-marker pos)
6270 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6271 'type (if pastschedp "past-scheduled" "scheduled")
6272 'date (if pastschedp schedule date)
6273 'ts-date schedule
6274 'warntime warntime
6275 'level level
6276 'priority (if habitp (org-habit-get-priority habitp)
6277 (+ 99 diff (org-get-priority item)))
6278 'org-habit-p habitp
6279 'todo-state todo-state)
6280 (push item scheduled-items))))))
6281 (nreverse scheduled-items)))
6283 (defun org-agenda-get-blocks ()
6284 "Return the date-range information for agenda display."
6285 (let* ((props (list 'face nil
6286 'org-not-done-regexp org-not-done-regexp
6287 'org-todo-regexp org-todo-regexp
6288 'org-complex-heading-regexp org-complex-heading-regexp
6289 'mouse-face 'highlight
6290 'help-echo
6291 (format "mouse-2 or RET jump to org file %s"
6292 (abbreviate-file-name buffer-file-name))))
6293 (regexp org-tr-regexp)
6294 (d0 (calendar-absolute-from-gregorian date))
6295 marker hdmarker ee txt d1 d2 s1 s2 category
6296 level todo-state tags pos head donep inherited-tags)
6297 (goto-char (point-min))
6298 (while (re-search-forward regexp nil t)
6299 (catch :skip
6300 (org-agenda-skip)
6301 (setq pos (point))
6302 (let ((start-time (match-string 1))
6303 (end-time (match-string 2)))
6304 (setq s1 (match-string 1)
6305 s2 (match-string 2)
6306 d1 (time-to-days
6307 (condition-case err
6308 (org-time-string-to-time s1)
6309 (error
6310 (error
6311 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6314 (current-buffer)
6315 (error-message-string err)))))
6316 d2 (time-to-days
6317 (condition-case err
6318 (org-time-string-to-time s2)
6319 (error
6320 (error
6321 "Bad timestamp %S at %d in buffer %S\nError was: %s"
6324 (current-buffer)
6325 (error-message-string err))))))
6326 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6327 ;; Only allow days between the limits, because the normal
6328 ;; date stamps will catch the limits.
6329 (save-excursion
6330 (setq todo-state (org-get-todo-state))
6331 (setq donep (member todo-state org-done-keywords))
6332 (if (and donep org-agenda-skip-timestamp-if-done)
6333 (throw :skip t))
6334 (setq marker (org-agenda-new-marker (point))
6335 category (org-get-category))
6336 (if (not (re-search-backward org-outline-regexp-bol nil t))
6337 (throw :skip nil)
6338 (goto-char (match-beginning 0))
6339 (setq hdmarker (org-agenda-new-marker (point))
6340 inherited-tags
6341 (or (eq org-agenda-show-inherited-tags 'always)
6342 (and (listp org-agenda-show-inherited-tags)
6343 (memq 'agenda org-agenda-show-inherited-tags))
6344 (and (eq org-agenda-show-inherited-tags t)
6345 (or (eq org-agenda-use-tag-inheritance t)
6346 (memq 'agenda org-agenda-use-tag-inheritance))))
6348 tags (org-get-tags-at nil (not inherited-tags)))
6349 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6350 (looking-at "\\*+[ \t]+\\(.*\\)")
6351 (setq head (match-string 1))
6352 (let ((remove-re
6353 (if org-agenda-remove-timeranges-from-blocks
6354 (concat
6355 "<" (regexp-quote s1) ".*?>"
6356 "--"
6357 "<" (regexp-quote s2) ".*?>")
6358 nil)))
6359 (setq txt (org-agenda-format-item
6360 (format
6361 (nth (if (= d1 d2) 0 1)
6362 org-agenda-timerange-leaders)
6363 (1+ (- d0 d1)) (1+ (- d2 d1)))
6364 head level category tags
6365 (cond ((and (= d1 d0) (= d2 d0))
6366 (concat "<" start-time ">--<" end-time ">"))
6367 ((= d1 d0)
6368 (concat "<" start-time ">"))
6369 ((= d2 d0)
6370 (concat "<" end-time ">")))
6371 remove-re))))
6372 (org-add-props txt props
6373 'org-marker marker 'org-hd-marker hdmarker
6374 'type "block" 'date date
6375 'level level
6376 'todo-state todo-state
6377 'priority (org-get-priority txt))
6378 (push txt ee))))
6379 (goto-char pos)))
6380 ;; Sort the entries by expiration date.
6381 (nreverse ee)))
6383 ;;; Agenda presentation and sorting
6385 (defvar org-prefix-has-time nil
6386 "A flag, set by `org-compile-prefix-format'.
6387 The flag is set if the currently compiled format contains a `%t'.")
6388 (defvar org-prefix-has-tag nil
6389 "A flag, set by `org-compile-prefix-format'.
6390 The flag is set if the currently compiled format contains a `%T'.")
6391 (defvar org-prefix-has-effort nil
6392 "A flag, set by `org-compile-prefix-format'.
6393 The flag is set if the currently compiled format contains a `%e'.")
6394 (defvar org-prefix-has-breadcrumbs nil
6395 "A flag, set by `org-compile-prefix-format'.
6396 The flag is set if the currently compiled format contains a `%b'.")
6397 (defvar org-prefix-category-length nil
6398 "Used by `org-compile-prefix-format' to remember the category field width.")
6399 (defvar org-prefix-category-max-length nil
6400 "Used by `org-compile-prefix-format' to remember the category field width.")
6402 (defun org-agenda-get-category-icon (category)
6403 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6404 (cl-dolist (entry org-agenda-category-icon-alist)
6405 (when (string-match-p (car entry) category)
6406 (if (listp (cadr entry))
6407 (cl-return (cadr entry))
6408 (cl-return (apply #'create-image (cdr entry)))))))
6410 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6411 remove-re habitp)
6412 "Format TXT to be inserted into the agenda buffer.
6413 In particular, add the prefix and corresponding text properties.
6415 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6416 LEVEL may be a string to replace the `%l' specifier.
6417 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6418 category taken from local variable or file name. It will replace the `%c'
6419 specifier in the format.
6420 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6421 TXT for sorting of this entry, and for the `%t' specifier in the format.
6422 When DOTIME is a string, this string is searched for a time before TXT is.
6423 TAGS can be the tags of the headline.
6424 Any match of REMOVE-RE will be removed from TXT."
6425 ;; We keep the org-prefix-* variable values along with a compiled
6426 ;; formatter, so that multiple agendas existing at the same time do
6427 ;; not step on each other toes.
6429 ;; It was inconvenient to make these variables buffer local in
6430 ;; Agenda buffers, because this function expects to be called with
6431 ;; the buffer where item comes from being current, and not agenda
6432 ;; buffer
6433 (let* ((bindings (car org-prefix-format-compiled))
6434 (formatter (cadr org-prefix-format-compiled)))
6435 (cl-loop for (var value) in bindings
6436 do (set var value))
6437 (save-match-data
6438 ;; Diary entries sometimes have extra whitespace at the beginning
6439 (setq txt (org-trim txt))
6441 ;; Fix the tags part in txt
6442 (setq txt (org-agenda-fix-displayed-tags
6443 txt tags
6444 org-agenda-show-inherited-tags
6445 org-agenda-hide-tags-regexp))
6447 (let* ((category (or category
6448 (if buffer-file-name
6449 (file-name-sans-extension
6450 (file-name-nondirectory buffer-file-name))
6451 "")))
6452 (category-icon (org-agenda-get-category-icon category))
6453 (category-icon (if category-icon
6454 (propertize " " 'display category-icon)
6455 ""))
6456 (effort (and (not (string= txt ""))
6457 (get-text-property 1 'effort txt)))
6458 ;; time, tag, effort are needed for the eval of the prefix format
6459 (tag (if tags (nth (1- (length tags)) tags) ""))
6460 (time-grid-trailing-characters (nth 2 org-agenda-time-grid))
6461 time
6462 (ts (if dotime (concat
6463 (if (stringp dotime) dotime "")
6464 (and org-agenda-search-headline-for-time txt))))
6465 (time-of-day (and dotime (org-get-time-of-day ts)))
6466 stamp plain s0 s1 s2 rtn srp l
6467 duration breadcrumbs)
6468 (and (derived-mode-p 'org-mode) buffer-file-name
6469 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6470 (when (and dotime time-of-day)
6471 ;; Extract starting and ending time and move them to prefix
6472 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6473 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6474 (setq s0 (match-string 0 ts)
6475 srp (and stamp (match-end 3))
6476 s1 (match-string (if plain 1 2) ts)
6477 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6479 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6480 ;; them, we might want to remove them there to avoid duplication.
6481 ;; The user can turn this off with a variable.
6482 (if (and org-prefix-has-time
6483 org-agenda-remove-times-when-in-prefix (or stamp plain)
6484 (string-match (concat (regexp-quote s0) " *") txt)
6485 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6486 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6487 (= (match-beginning 0) 0)
6489 (setq txt (replace-match "" nil nil txt))))
6490 ;; Normalize the time(s) to 24 hour
6491 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6492 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6494 ;; Try to set s2 if s1 and
6495 ;; `org-agenda-default-appointment-duration' are set
6496 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6497 (setq s2
6498 (org-duration-from-minutes
6499 (+ (org-duration-to-minutes s1 t)
6500 org-agenda-default-appointment-duration)
6501 nil t)))
6503 ;; Compute the duration
6504 (when s2
6505 (setq duration (- (org-duration-to-minutes s2)
6506 (org-duration-to-minutes s1)))))
6508 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6509 ;; Tags are in the string
6510 (if (or (eq org-agenda-remove-tags t)
6511 (and org-agenda-remove-tags
6512 org-prefix-has-tag))
6513 (setq txt (replace-match "" t t txt))
6514 (setq txt (replace-match
6515 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6516 (match-string 2 txt))
6517 t t txt))))
6519 (when remove-re
6520 (while (string-match remove-re txt)
6521 (setq txt (replace-match "" t t txt))))
6523 ;; Set org-heading property on `txt' to mark the start of the
6524 ;; heading.
6525 (add-text-properties 0 (length txt) '(org-heading t) txt)
6527 ;; Prepare the variables needed in the eval of the compiled format
6528 (if org-prefix-has-breadcrumbs
6529 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6530 (let ((s (org-display-outline-path nil nil "->" t)))
6531 (if (eq "" s) "" (concat s "->"))))))
6532 (setq time (cond (s2 (concat
6533 (org-agenda-time-of-day-to-ampm-maybe s1)
6534 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6535 (if org-agenda-timegrid-use-ampm " ")))
6536 (s1 (concat
6537 (org-agenda-time-of-day-to-ampm-maybe s1)
6538 (if org-agenda-timegrid-use-ampm
6539 (concat time-grid-trailing-characters " ")
6540 time-grid-trailing-characters)))
6541 (t ""))
6542 extra (or (and (not habitp) extra) "")
6543 category (if (symbolp category) (symbol-name category) category)
6544 level (or level ""))
6545 (if (string-match org-bracket-link-regexp category)
6546 (progn
6547 (setq l (if (match-end 3)
6548 (- (match-end 3) (match-beginning 3))
6549 (- (match-end 1) (match-beginning 1))))
6550 (when (< l (or org-prefix-category-length 0))
6551 (setq category (copy-sequence category))
6552 (org-add-props category nil
6553 'extra-space (make-string
6554 (- org-prefix-category-length l 1) ?\ ))))
6555 (if (and org-prefix-category-max-length
6556 (>= (length category) org-prefix-category-max-length))
6557 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6558 ;; Evaluate the compiled format
6559 (setq rtn (concat (eval formatter) txt))
6561 ;; And finally add the text properties
6562 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6563 (org-add-props rtn nil
6564 'org-category category
6565 'tags (mapcar 'org-downcase-keep-props tags)
6566 'org-highest-priority org-highest-priority
6567 'org-lowest-priority org-lowest-priority
6568 'time-of-day time-of-day
6569 'duration duration
6570 'breadcrumbs breadcrumbs
6571 'txt txt
6572 'level level
6573 'time time
6574 'extra extra
6575 'format org-prefix-format-compiled
6576 'dotime dotime)))))
6578 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6579 "Remove tags string from TXT, and add a modified list of tags.
6580 The modified list may contain inherited tags, and tags matched by
6581 `org-agenda-hide-tags-regexp' will be removed."
6582 (when (or add-inherited hide-re)
6583 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6584 (setq txt (substring txt 0 (match-beginning 0))))
6585 (setq tags
6586 (delq nil
6587 (mapcar (lambda (tg)
6588 (if (or (and hide-re (string-match hide-re tg))
6589 (and (not add-inherited)
6590 (get-text-property 0 'inherited tg)))
6592 tg))
6593 tags)))
6594 (when tags
6595 (let ((have-i (get-text-property 0 'inherited (car tags)))
6597 (setq txt (concat txt " :"
6598 (mapconcat
6599 (lambda (x)
6600 (setq i (get-text-property 0 'inherited x))
6601 (if (and have-i (not i))
6602 (progn
6603 (setq have-i nil)
6604 (concat ":" x))
6606 tags ":")
6607 (if have-i "::" ":"))))))
6608 txt)
6610 (defun org-downcase-keep-props (s)
6611 (let ((props (text-properties-at 0 s)))
6612 (setq s (downcase s))
6613 (add-text-properties 0 (length s) props s)
6616 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6618 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6619 "Add a time-grid for agenda items which need it.
6621 LIST is the list of agenda items formatted by `org-agenda-list'.
6622 NDAYS is the span of the current agenda view.
6623 TODAYP is t when the current agenda view is on today."
6624 (catch 'exit
6625 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6626 ((and todayp (member 'today (car org-agenda-time-grid))))
6627 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6628 ((member 'weekly (car org-agenda-time-grid)))
6629 (t (throw 'exit list)))
6630 (let* ((have (delq nil (mapcar
6631 (lambda (x) (get-text-property 1 'time-of-day x))
6632 list)))
6633 (string (nth 3 org-agenda-time-grid))
6634 (gridtimes (nth 1 org-agenda-time-grid))
6635 (req (car org-agenda-time-grid))
6636 (remove (member 'remove-match req))
6637 new time)
6638 (if (and (member 'require-timed req) (not have))
6639 ;; don't show empty grid
6640 (throw 'exit list))
6641 (while (setq time (pop gridtimes))
6642 (unless (and remove (member time have))
6643 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6644 (push (org-agenda-format-item
6645 nil string nil "" nil
6646 (concat (substring time 0 -2) ":" (substring time -2)))
6647 new)
6648 (put-text-property
6649 2 (length (car new)) 'face 'org-time-grid (car new))))
6650 (when (and todayp org-agenda-show-current-time-in-grid)
6651 (push (org-agenda-format-item
6652 nil org-agenda-current-time-string nil "" nil
6653 (format-time-string "%H:%M "))
6654 new)
6655 (put-text-property
6656 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6658 (if (member 'time-up org-agenda-sorting-strategy-selected)
6659 (append new list)
6660 (append list new)))))
6662 (defun org-compile-prefix-format (key)
6663 "Compile the prefix format into a Lisp form that can be evaluated.
6664 The resulting form and associated variable bindings is returned
6665 and stored in the variable `org-prefix-format-compiled'."
6666 (setq org-prefix-has-time nil
6667 org-prefix-has-tag nil
6668 org-prefix-category-length nil
6669 org-prefix-has-effort nil
6670 org-prefix-has-breadcrumbs nil)
6671 (let ((s (cond
6672 ((stringp org-agenda-prefix-format)
6673 org-agenda-prefix-format)
6674 ((assq key org-agenda-prefix-format)
6675 (cdr (assq key org-agenda-prefix-format)))
6676 (t " %-12:c%?-12t% s")))
6677 (start 0)
6678 varform vars var e c f opt)
6679 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6680 s start)
6681 (setq var (or (cdr (assoc (match-string 4 s)
6682 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6683 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6684 'eval)
6685 c (or (match-string 3 s) "")
6686 opt (match-beginning 1)
6687 start (1+ (match-beginning 0)))
6688 (if (eq var 'time) (setq org-prefix-has-time t))
6689 (if (eq var 'tag) (setq org-prefix-has-tag t))
6690 (if (eq var 'effort) (setq org-prefix-has-effort t))
6691 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6692 (setq f (concat "%" (match-string 2 s) "s"))
6693 (when (eq var 'category)
6694 (setq org-prefix-category-length
6695 (floor (abs (string-to-number (match-string 2 s)))))
6696 (setq org-prefix-category-max-length
6697 (let ((x (match-string 2 s)))
6698 (save-match-data
6699 (if (string-match "\\.[0-9]+" x)
6700 (string-to-number (substring (match-string 0 x) 1)))))))
6701 (if (eq var 'eval)
6702 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6703 (if opt
6704 (setq varform
6705 `(if (or (equal "" ,var) (equal nil ,var))
6707 (format ,f (concat ,var ,c))))
6708 (setq varform
6709 `(format ,f (if (or (equal ,var "")
6710 (equal ,var nil)) ""
6711 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6712 (setq s (replace-match "%s" t nil s))
6713 (push varform vars))
6714 (setq vars (nreverse vars))
6715 (with-current-buffer (or org-agenda-buffer (current-buffer))
6716 (setq org-prefix-format-compiled
6717 (list
6718 `((org-prefix-has-time ,org-prefix-has-time)
6719 (org-prefix-has-tag ,org-prefix-has-tag)
6720 (org-prefix-category-length ,org-prefix-category-length)
6721 (org-prefix-has-effort ,org-prefix-has-effort)
6722 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6723 `(format ,s ,@vars))))))
6725 (defun org-set-sorting-strategy (key)
6726 (if (symbolp (car org-agenda-sorting-strategy))
6727 ;; the old format
6728 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6729 (setq org-agenda-sorting-strategy-selected
6730 (or (cdr (assq key org-agenda-sorting-strategy))
6731 (cdr (assq 'agenda org-agenda-sorting-strategy))
6732 '(time-up category-keep priority-down)))))
6734 (defun org-get-time-of-day (s &optional string mod24)
6735 "Check string S for a time of day.
6736 If found, return it as a military time number between 0 and 2400.
6737 If not found, return nil.
6738 The optional STRING argument forces conversion into a 5 character wide string
6739 HH:MM."
6740 (save-match-data
6741 (when
6742 (and
6743 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6744 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6745 (not (eq (get-text-property 1 'face s) 'org-link)))
6746 (let* ((h (string-to-number (match-string 1 s)))
6747 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6748 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6749 (am-p (equal ampm "am"))
6750 (h1 (cond ((not ampm) h)
6751 ((= h 12) (if am-p 0 12))
6752 (t (+ h (if am-p 0 12)))))
6753 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6754 (mod h1 24) h1))
6755 (t0 (+ (* 100 h2) m))
6756 (t1 (concat (if (>= h1 24) "+" " ")
6757 (if (and org-agenda-time-leading-zero
6758 (< t0 1000)) "0" "")
6759 (if (< t0 100) "0" "")
6760 (if (< t0 10) "0" "")
6761 (int-to-string t0))))
6762 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6764 (defvar org-agenda-before-sorting-filter-function nil
6765 "Function to be applied to agenda items prior to sorting.
6766 Prior to sorting also means just before they are inserted into the agenda.
6768 To aid sorting, you may revisit the original entries and add more text
6769 properties which will later be used by the sorting functions.
6771 The function should take a string argument, an agenda line.
6772 It has access to the text properties in that line, which contain among
6773 other things, the property `org-hd-marker' that points to the entry
6774 where the line comes from. Note that not all lines going into the agenda
6775 have this property, only most.
6777 The function should return the modified string. It is probably best
6778 to ONLY change text properties.
6780 You can also use this function as a filter, by returning nil for lines
6781 you don't want to have in the agenda at all. For this application, you
6782 could bind the variable in the options section of a custom command.")
6784 (defun org-agenda-finalize-entries (list &optional type)
6785 "Sort, limit and concatenate the LIST of agenda items.
6786 The optional argument TYPE tells the agenda type."
6787 (let ((max-effort (cond ((listp org-agenda-max-effort)
6788 (cdr (assoc type org-agenda-max-effort)))
6789 (t org-agenda-max-effort)))
6790 (max-todo (cond ((listp org-agenda-max-todos)
6791 (cdr (assoc type org-agenda-max-todos)))
6792 (t org-agenda-max-todos)))
6793 (max-tags (cond ((listp org-agenda-max-tags)
6794 (cdr (assoc type org-agenda-max-tags)))
6795 (t org-agenda-max-tags)))
6796 (max-entries (cond ((listp org-agenda-max-entries)
6797 (cdr (assoc type org-agenda-max-entries)))
6798 (t org-agenda-max-entries))))
6799 (when org-agenda-before-sorting-filter-function
6800 (setq list
6801 (delq nil
6802 (mapcar
6803 org-agenda-before-sorting-filter-function list))))
6804 (setq list (mapcar 'org-agenda-highlight-todo list)
6805 list (mapcar 'identity (sort list 'org-entries-lessp)))
6806 (when max-effort
6807 (setq list (org-agenda-limit-entries
6808 list 'effort-minutes max-effort
6809 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6810 32767 -1))))))
6811 (when max-todo
6812 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6813 (when max-tags
6814 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6815 (when max-entries
6816 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6817 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
6818 (setq list (mapcar #'org-agenda--mark-blocked-entry list)))
6819 (mapconcat 'identity list "\n")))
6821 (defun org-agenda-limit-entries (list prop limit &optional fn)
6822 "Limit the number of agenda entries."
6823 (let ((include (and limit (< limit 0))))
6824 (if limit
6825 (let ((fun (or fn (lambda (p) (if p 1))))
6826 (lim 0))
6827 (delq nil
6828 (mapcar
6829 (lambda (e)
6830 (let ((pval (funcall
6831 fun (get-text-property (1- (length e))
6832 prop e))))
6833 (if pval (setq lim (+ lim pval)))
6834 (cond ((and pval (<= lim (abs limit))) e)
6835 ((and include (not pval)) e))))
6836 list)))
6837 list)))
6839 (defun org-agenda-limit-interactively (remove)
6840 "In agenda, interactively limit entries to various maximums."
6841 (interactive "P")
6842 (if remove
6843 (progn (setq org-agenda-max-entries nil
6844 org-agenda-max-todos nil
6845 org-agenda-max-tags nil
6846 org-agenda-max-effort nil)
6847 (org-agenda-redo))
6848 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6849 (msg (cond ((= max ?E) "How many minutes? ")
6850 ((= max ?e) "How many entries? ")
6851 ((= max ?t) "How many TODO entries? ")
6852 ((= max ?T) "How many tagged entries? ")
6853 (t (user-error "Wrong input"))))
6854 (num (string-to-number (read-from-minibuffer msg))))
6855 (cond ((equal max ?e)
6856 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6857 ((equal max ?t)
6858 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6859 ((equal max ?T)
6860 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6861 ((equal max ?E)
6862 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6863 (org-agenda-fit-window-to-buffer))
6865 (defun org-agenda-highlight-todo (x)
6866 (let ((org-done-keywords org-done-keywords-for-agenda)
6867 (case-fold-search nil)
6869 (if (eq x 'line)
6870 (save-excursion
6871 (beginning-of-line 1)
6872 (setq re (org-get-at-bol 'org-todo-regexp))
6873 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6874 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6875 (add-text-properties (match-beginning 0) (match-end 1)
6876 (list 'face (org-get-todo-face 1)))
6877 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6878 (delete-region (match-beginning 1) (1- (match-end 0)))
6879 (goto-char (match-beginning 1))
6880 (insert (format org-agenda-todo-keyword-format s)))))
6881 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6882 (setq re (get-text-property 0 'org-todo-regexp x))
6883 (when (and re
6884 ;; Test `pl' because if there's no heading content,
6885 ;; there's no point matching to highlight. Note
6886 ;; that if we didn't test `pl' first, and there
6887 ;; happened to be no keyword from `org-todo-regexp'
6888 ;; on this heading line, then the `equal' comparison
6889 ;; afterwards would spuriously succeed in the case
6890 ;; where `pl' is nil -- causing an args-out-of-range
6891 ;; error when we try to add text properties to text
6892 ;; that isn't there.
6894 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6895 x pl) pl))
6896 (add-text-properties
6897 (or (match-end 1) (match-end 0)) (match-end 0)
6898 (list 'face (org-get-todo-face (match-string 2 x)))
6900 (when (match-end 1)
6901 (setq x
6902 (concat
6903 (substring x 0 (match-end 1))
6904 (format org-agenda-todo-keyword-format
6905 (match-string 2 x))
6906 ;; Remove `display' property as the icon could leak
6907 ;; on the white space.
6908 (org-add-props " " (org-plist-delete (text-properties-at 0 x)
6909 'display))
6910 (substring x (match-end 3)))))))
6911 x)))
6913 (defsubst org-cmp-values (a b property)
6914 "Compare the numeric value of text PROPERTY for string A and B."
6915 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6916 (pb (or (get-text-property (1- (length b)) property b) 0)))
6917 (cond ((> pa pb) +1)
6918 ((< pa pb) -1))))
6920 (defsubst org-cmp-effort (a b)
6921 "Compare the effort values of string A and B."
6922 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6923 ;; `effort-minutes' property is not directly accessible from
6924 ;; the strings, but is stored as a property in `txt'.
6925 (ea (or (get-text-property
6926 0 'effort-minutes (get-text-property 0 'txt a))
6927 def))
6928 (eb (or (get-text-property
6929 0 'effort-minutes (get-text-property 0 'txt b))
6930 def)))
6931 (cond ((> ea eb) +1)
6932 ((< ea eb) -1))))
6934 (defsubst org-cmp-category (a b)
6935 "Compare the string values of categories of strings A and B."
6936 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6937 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6938 (cond ((string-lessp ca cb) -1)
6939 ((string-lessp cb ca) +1))))
6941 (defsubst org-cmp-todo-state (a b)
6942 "Compare the todo states of strings A and B."
6943 (let* ((ma (or (get-text-property 1 'org-marker a)
6944 (get-text-property 1 'org-hd-marker a)))
6945 (mb (or (get-text-property 1 'org-marker b)
6946 (get-text-property 1 'org-hd-marker b)))
6947 (fa (and ma (marker-buffer ma)))
6948 (fb (and mb (marker-buffer mb)))
6949 (todo-kwds
6950 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6951 (and fb (with-current-buffer fb org-todo-keywords-1))))
6952 (ta (or (get-text-property 1 'todo-state a) ""))
6953 (tb (or (get-text-property 1 'todo-state b) ""))
6954 (la (- (length (member ta todo-kwds))))
6955 (lb (- (length (member tb todo-kwds))))
6956 (donepa (member ta org-done-keywords-for-agenda))
6957 (donepb (member tb org-done-keywords-for-agenda)))
6958 (cond ((and donepa (not donepb)) -1)
6959 ((and (not donepa) donepb) +1)
6960 ((< la lb) -1)
6961 ((< lb la) +1))))
6963 (defsubst org-cmp-alpha (a b)
6964 "Compare the headlines, alphabetically."
6965 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6966 (plb (text-property-any 0 (length b) 'org-heading t b))
6967 (ta (and pla (substring a pla)))
6968 (tb (and plb (substring b plb)))
6969 (case-fold-search nil))
6970 (when pla
6971 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6972 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6973 (setq ta (substring ta (match-end 0))))
6974 (setq ta (downcase ta)))
6975 (when plb
6976 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6977 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6978 (setq tb (substring tb (match-end 0))))
6979 (setq tb (downcase tb)))
6980 (cond ((not ta) +1)
6981 ((not tb) -1)
6982 ((string-lessp ta tb) -1)
6983 ((string-lessp tb ta) +1))))
6985 (defsubst org-cmp-tag (a b)
6986 "Compare the string values of the first tags of A and B."
6987 (let ((ta (car (last (get-text-property 1 'tags a))))
6988 (tb (car (last (get-text-property 1 'tags b)))))
6989 (cond ((not ta) +1)
6990 ((not tb) -1)
6991 ((string-lessp ta tb) -1)
6992 ((string-lessp tb ta) +1))))
6994 (defsubst org-cmp-time (a b)
6995 "Compare the time-of-day values of strings A and B."
6996 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6997 (ta (or (get-text-property 1 'time-of-day a) def))
6998 (tb (or (get-text-property 1 'time-of-day b) def)))
6999 (cond ((< ta tb) -1)
7000 ((< tb ta) +1))))
7002 (defsubst org-cmp-ts (a b type)
7003 "Compare the timestamps values of entries A and B.
7004 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7005 \"timestamp_ia\", compare within each of these type. When TYPE
7006 is the empty string, compare all timestamps without respect of
7007 their type."
7008 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7009 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7010 (get-text-property 1 'ts-date a))
7011 def))
7012 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7013 (get-text-property 1 'ts-date b))
7014 def)))
7015 (cond ((< ta tb) -1)
7016 ((< tb ta) +1))))
7018 (defsubst org-cmp-habit-p (a b)
7019 "Compare the todo states of strings A and B."
7020 (let ((ha (get-text-property 1 'org-habit-p a))
7021 (hb (get-text-property 1 'org-habit-p b)))
7022 (cond ((and ha (not hb)) -1)
7023 ((and (not ha) hb) +1))))
7025 (defun org-entries-lessp (a b)
7026 "Predicate for sorting agenda entries."
7027 ;; The following variables will be used when the form is evaluated.
7028 ;; So even though the compiler complains, keep them.
7029 (let* ((ss org-agenda-sorting-strategy-selected)
7030 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7031 (org-cmp-ts a b "")))
7032 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7033 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7034 (org-cmp-ts a b "scheduled")))
7035 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7036 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7037 (org-cmp-ts a b "deadline")))
7038 (deadline-down (if deadline-up (- deadline-up) nil))
7039 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7040 (org-cmp-ts a b "timestamp_ia")))
7041 (tsia-down (if tsia-up (- tsia-up) nil))
7042 (ts-up (and (org-em 'ts-up 'ts-down ss)
7043 (org-cmp-ts a b "timestamp")))
7044 (ts-down (if ts-up (- ts-up) nil))
7045 (time-up (and (org-em 'time-up 'time-down ss)
7046 (org-cmp-time a b)))
7047 (time-down (if time-up (- time-up) nil))
7048 (stats-up (and (org-em 'stats-up 'stats-down ss)
7049 (org-cmp-values a b 'org-stats)))
7050 (stats-down (if stats-up (- stats-up) nil))
7051 (priority-up (and (org-em 'priority-up 'priority-down ss)
7052 (org-cmp-values a b 'priority)))
7053 (priority-down (if priority-up (- priority-up) nil))
7054 (effort-up (and (org-em 'effort-up 'effort-down ss)
7055 (org-cmp-effort a b)))
7056 (effort-down (if effort-up (- effort-up) nil))
7057 (category-up (and (or (org-em 'category-up 'category-down ss)
7058 (memq 'category-keep ss))
7059 (org-cmp-category a b)))
7060 (category-down (if category-up (- category-up) nil))
7061 (category-keep (if category-up +1 nil))
7062 (tag-up (and (org-em 'tag-up 'tag-down ss)
7063 (org-cmp-tag a b)))
7064 (tag-down (if tag-up (- tag-up) nil))
7065 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7066 (org-cmp-todo-state a b)))
7067 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7068 (habit-up (and (org-em 'habit-up 'habit-down ss)
7069 (org-cmp-habit-p a b)))
7070 (habit-down (if habit-up (- habit-up) nil))
7071 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7072 (org-cmp-alpha a b)))
7073 (alpha-down (if alpha-up (- alpha-up) nil))
7074 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7075 user-defined-up user-defined-down)
7076 (if (and need-user-cmp org-agenda-cmp-user-defined
7077 (functionp org-agenda-cmp-user-defined))
7078 (setq user-defined-up
7079 (funcall org-agenda-cmp-user-defined a b)
7080 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7081 (cdr (assoc
7082 (eval (cons 'or org-agenda-sorting-strategy-selected))
7083 '((-1 . t) (1 . nil) (nil . nil))))))
7085 ;;; Agenda restriction lock
7087 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7088 "Overlay to mark the headline to which agenda commands are restricted.")
7089 (overlay-put org-agenda-restriction-lock-overlay
7090 'face 'org-agenda-restriction-lock)
7091 (overlay-put org-agenda-restriction-lock-overlay
7092 'help-echo "Agendas are currently limited to this subtree.")
7093 (delete-overlay org-agenda-restriction-lock-overlay)
7095 (defun org-agenda-set-restriction-lock-from-agenda (arg)
7096 "Set the restriction lock to the agenda item at point from within the agenda.
7097 When called with a `\\[universal-argument]' prefix, restrict to
7098 the file which contains the item.
7099 Argument ARG is the prefix argument."
7100 (interactive "P")
7101 (unless (derived-mode-p 'org-agenda-mode)
7102 (user-error "Not in an Org agenda buffer"))
7103 (let* ((marker (or (org-get-at-bol 'org-marker)
7104 (org-agenda-error)))
7105 (buffer (marker-buffer marker))
7106 (pos (marker-position marker)))
7107 (with-current-buffer buffer
7108 (goto-char pos)
7109 (org-agenda-set-restriction-lock arg))))
7111 ;;;###autoload
7112 (defun org-agenda-set-restriction-lock (&optional type)
7113 "Set restriction lock for agenda, to current subtree or file.
7114 Restriction will be the file if TYPE is `file', or if type is the
7115 universal prefix \\='(4), or if the cursor is before the first headline
7116 in the file. Otherwise, restriction will be to the current subtree."
7117 (interactive "P")
7118 (org-agenda-remove-restriction-lock 'noupdate)
7119 (and (equal type '(4)) (setq type 'file))
7120 (setq type (cond
7121 (type type)
7122 ((org-at-heading-p) 'subtree)
7123 ((condition-case nil (org-back-to-heading t) (error nil))
7124 'subtree)
7125 (t 'file)))
7126 (if (eq type 'subtree)
7127 (progn
7128 (setq org-agenda-restrict (current-buffer))
7129 (setq org-agenda-overriding-restriction 'subtree)
7130 (put 'org-agenda-files 'org-restrict
7131 (list (buffer-file-name (buffer-base-buffer))))
7132 (org-back-to-heading t)
7133 (move-overlay org-agenda-restriction-lock-overlay
7134 (point)
7135 (if org-agenda-restriction-lock-highlight-subtree
7136 (save-excursion (org-end-of-subtree t t) (point))
7137 (point-at-eol)))
7138 (move-marker org-agenda-restrict-begin (point))
7139 (move-marker org-agenda-restrict-end
7140 (save-excursion (org-end-of-subtree t t)))
7141 (message "Locking agenda restriction to subtree"))
7142 (put 'org-agenda-files 'org-restrict
7143 (list (buffer-file-name (buffer-base-buffer))))
7144 (setq org-agenda-restrict nil)
7145 (setq org-agenda-overriding-restriction 'file)
7146 (move-marker org-agenda-restrict-begin nil)
7147 (move-marker org-agenda-restrict-end nil)
7148 (message "Locking agenda restriction to file"))
7149 (setq current-prefix-arg nil)
7150 (org-agenda-maybe-redo))
7152 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7153 "Remove the agenda restriction lock."
7154 (interactive "P")
7155 (delete-overlay org-agenda-restriction-lock-overlay)
7156 (delete-overlay org-speedbar-restriction-lock-overlay)
7157 (setq org-agenda-overriding-restriction nil)
7158 (setq org-agenda-restrict nil)
7159 (put 'org-agenda-files 'org-restrict nil)
7160 (move-marker org-agenda-restrict-begin nil)
7161 (move-marker org-agenda-restrict-end nil)
7162 (setq current-prefix-arg nil)
7163 (message "Agenda restriction lock removed")
7164 (or noupdate (org-agenda-maybe-redo)))
7166 (defun org-agenda-maybe-redo ()
7167 "If there is any window showing the agenda view, update it."
7168 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7169 org-agenda-buffer-name)
7171 (w0 (selected-window)))
7172 (when w
7173 (select-window w)
7174 (org-agenda-redo)
7175 (select-window w0)
7176 (if org-agenda-overriding-restriction
7177 (message "Agenda view shifted to new %s restriction"
7178 org-agenda-overriding-restriction)
7179 (message "Agenda restriction lock removed")))))
7181 ;;; Agenda commands
7183 (defun org-agenda-check-type (error &rest types)
7184 "Check if agenda buffer is of allowed type.
7185 If ERROR is non-nil, throw an error, otherwise just return nil.
7186 Allowed types are `agenda' `todo' `tags' `search'."
7187 (cond ((not org-agenda-type)
7188 (error "No Org agenda currently displayed"))
7189 ((memq org-agenda-type types) t)
7190 (error
7191 (error "Not allowed in %s-type agenda buffers" org-agenda-type))
7192 (t nil)))
7194 (defun org-agenda-Quit ()
7195 "Exit the agenda, killing the agenda buffer.
7196 Like `org-agenda-quit', but kill the buffer even when
7197 `org-agenda-sticky' is non-nil."
7198 (interactive)
7199 (org-agenda--quit))
7201 (defun org-agenda-quit ()
7202 "Exit the agenda.
7204 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7205 instead of killing it.
7207 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7208 the pre-agenda window configuration.
7210 When column view is active, exit column view instead of the
7211 agenda."
7212 (interactive)
7213 (org-agenda--quit org-agenda-sticky))
7215 (defun org-agenda--quit (&optional bury)
7216 (if org-agenda-columns-active
7217 (org-columns-quit)
7218 (let ((wconf org-agenda-pre-window-conf)
7219 (buf (current-buffer))
7220 (org-agenda-last-indirect-window
7221 (and (eq org-indirect-buffer-display 'other-window)
7222 org-agenda-last-indirect-buffer
7223 (get-buffer-window org-agenda-last-indirect-buffer))))
7224 (cond
7225 ((eq org-agenda-window-setup 'other-frame)
7226 (delete-frame))
7227 ((and org-agenda-restore-windows-after-quit
7228 wconf)
7229 ;; Maybe restore the pre-agenda window configuration. Reset
7230 ;; `org-agenda-pre-window-conf' before running
7231 ;; `set-window-configuration', which loses the current buffer.
7232 (setq org-agenda-pre-window-conf nil)
7233 (set-window-configuration wconf))
7235 (when org-agenda-last-indirect-window
7236 (delete-window org-agenda-last-indirect-window))
7237 (and (not (eq org-agenda-window-setup 'current-window))
7238 (not (one-window-p))
7239 (delete-window))))
7240 (if bury
7241 ;; Set the agenda buffer as the current buffer instead of
7242 ;; passing it as an argument to `bury-buffer' so that
7243 ;; `bury-buffer' removes it from the window.
7244 (with-current-buffer buf
7245 (bury-buffer))
7246 (kill-buffer buf)
7247 (setq org-agenda-archives-mode nil
7248 org-agenda-buffer nil)))))
7250 (defun org-agenda-exit ()
7251 "Exit the agenda, killing Org buffers loaded by the agenda.
7252 Like `org-agenda-Quit', but kill any buffers that were created by
7253 the agenda. Org buffers visited directly by the user will not be
7254 touched. Also, exit the agenda even if it is in column view."
7255 (interactive)
7256 (when org-agenda-columns-active
7257 (org-columns-quit))
7258 (org-release-buffers org-agenda-new-buffers)
7259 (setq org-agenda-new-buffers nil)
7260 (org-agenda-Quit))
7262 (defun org-agenda-kill-all-agenda-buffers ()
7263 "Kill all buffers in `org-agenda-mode'.
7264 This is used when toggling sticky agendas."
7265 (interactive)
7266 (let (blist)
7267 (dolist (buf (buffer-list))
7268 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7269 (push buf blist)))
7270 (mapc 'kill-buffer blist)))
7272 (defun org-agenda-execute (arg)
7273 "Execute another agenda command, keeping same window.
7274 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7275 in the agenda."
7276 (interactive "P")
7277 (let ((org-agenda-window-setup 'current-window))
7278 (org-agenda arg)))
7280 (defun org-agenda-redo (&optional all)
7281 "Rebuild possibly ALL agenda view(s) in the current buffer."
7282 (interactive "P")
7283 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7284 (cpa (unless (eq all t) current-prefix-arg))
7285 (org-agenda-doing-sticky-redo org-agenda-sticky)
7286 (org-agenda-sticky nil)
7287 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7288 org-agenda-buffer-name))
7289 (org-agenda-keep-modes t)
7290 (tag-filter org-agenda-tag-filter)
7291 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7292 (top-hl-filter org-agenda-top-headline-filter)
7293 (cat-filter org-agenda-category-filter)
7294 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7295 (re-filter org-agenda-regexp-filter)
7296 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7297 (effort-filter org-agenda-effort-filter)
7298 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7299 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7300 (cols org-agenda-columns-active)
7301 (line (org-current-line))
7302 (window-line (- line (org-current-line (window-start))))
7303 (lprops (get 'org-agenda-redo-command 'org-lprops))
7304 (redo-cmd (get-text-property p 'org-redo-cmd))
7305 (last-args (get-text-property p 'org-last-args))
7306 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7307 (org-agenda-overriding-cmd-arguments
7308 (unless (eq all t)
7309 (cond ((listp last-args)
7310 (cons (or cpa (car last-args)) (cdr last-args)))
7311 ((stringp last-args)
7312 last-args))))
7313 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7314 (put 'org-agenda-tag-filter :preset-filter nil)
7315 (put 'org-agenda-category-filter :preset-filter nil)
7316 (put 'org-agenda-regexp-filter :preset-filter nil)
7317 (put 'org-agenda-effort-filter :preset-filter nil)
7318 (and cols (org-columns-quit))
7319 (message "Rebuilding agenda buffer...")
7320 (if series-redo-cmd
7321 (eval series-redo-cmd)
7322 (org-let lprops redo-cmd))
7323 (setq org-agenda-undo-list nil
7324 org-agenda-pending-undo-list nil
7325 org-agenda-tag-filter tag-filter
7326 org-agenda-category-filter cat-filter
7327 org-agenda-regexp-filter re-filter
7328 org-agenda-effort-filter effort-filter
7329 org-agenda-top-headline-filter top-hl-filter)
7330 (message "Rebuilding agenda buffer...done")
7331 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7332 (put 'org-agenda-category-filter :preset-filter cat-preset)
7333 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7334 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7335 (let ((tag (or tag-filter tag-preset))
7336 (cat (or cat-filter cat-preset))
7337 (effort (or effort-filter effort-preset))
7338 (re (or re-filter re-preset)))
7339 (when tag (org-agenda-filter-apply tag 'tag t))
7340 (when cat (org-agenda-filter-apply cat 'category))
7341 (when effort (org-agenda-filter-apply effort 'effort))
7342 (when re (org-agenda-filter-apply re 'regexp)))
7343 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7344 (and cols (called-interactively-p 'any) (org-agenda-columns))
7345 (org-goto-line line)
7346 (recenter window-line)))
7348 (defun org-agenda-redo-all (&optional exhaustive)
7349 "Rebuild all agenda views in the current buffer.
7350 With a prefix argument, do so in all agenda buffers."
7351 (interactive "P")
7352 (if exhaustive
7353 (dolist (buffer (buffer-list))
7354 (with-current-buffer buffer
7355 (when (derived-mode-p 'org-agenda-mode)
7356 (org-agenda-redo t))))
7357 (org-agenda-redo t)))
7359 (defvar org-global-tags-completion-table nil)
7360 (defvar org-agenda-filter-form nil)
7361 (defvar org-agenda-filtered-by-category nil)
7363 (defun org-agenda-filter-by-category (strip)
7364 "Filter lines in the agenda buffer that have a specific category.
7365 The category is that of the current line.
7366 Without prefix argument, keep only the lines of that category.
7367 With a prefix argument, exclude the lines of that category.
7369 (interactive "P")
7370 (if (and org-agenda-filtered-by-category
7371 org-agenda-category-filter)
7372 (org-agenda-filter-show-all-cat)
7373 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7374 (cond
7375 ((and cat strip)
7376 (org-agenda-filter-apply
7377 (push (concat "-" cat) org-agenda-category-filter) 'category))
7378 (cat
7379 (org-agenda-filter-apply
7380 (setq org-agenda-category-filter
7381 (list (concat "+" cat))) 'category))
7382 (t (error "No category at point"))))))
7384 (defun org-find-top-headline (&optional pos)
7385 "Find the topmost parent headline and return it.
7386 POS when non-nil is the marker or buffer position to start the
7387 search from."
7388 (save-excursion
7389 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
7390 (when pos (goto-char pos))
7391 ;; Skip up to the topmost parent.
7392 (while (org-up-heading-safe))
7393 (ignore-errors (nth 4 (org-heading-components))))))
7395 (defvar org-agenda-filtered-by-top-headline nil)
7396 (defun org-agenda-filter-by-top-headline (strip)
7397 "Keep only those lines that are descendants from the same top headline.
7398 The top headline is that of the current line."
7399 (interactive "P")
7400 (if org-agenda-filtered-by-top-headline
7401 (progn
7402 (setq org-agenda-filtered-by-top-headline nil
7403 org-agenda-top-headline-filter nil)
7404 (org-agenda-filter-show-all-top-filter))
7405 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7406 (if toph (org-agenda-filter-top-headline-apply toph strip)
7407 (error "No top-level headline at point")))))
7409 (defvar org-agenda-regexp-filter nil)
7410 (defun org-agenda-filter-by-regexp (strip)
7411 "Filter agenda entries by a regular expression.
7412 Regexp filters are cumulative.
7413 With no prefix argument, keep entries matching the regexp.
7414 With one prefix argument, filter out entries matching the regexp.
7415 With two prefix arguments, remove the regexp filters."
7416 (interactive "P")
7417 (if (not (equal strip '(16)))
7418 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7419 (read-from-minibuffer
7420 (if (equal strip '(4))
7421 "Filter out entries matching regexp: "
7422 "Narrow to entries matching regexp: ")))))
7423 (push flt org-agenda-regexp-filter)
7424 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7425 (org-agenda-filter-show-all-re)
7426 (message "Regexp filter removed")))
7428 (defvar org-agenda-effort-filter nil)
7429 (defun org-agenda-filter-by-effort (strip)
7430 "Filter agenda entries by effort.
7431 With no prefix argument, keep entries matching the effort condition.
7432 With one prefix argument, filter out entries matching the condition.
7433 With two prefix arguments, remove the effort filters."
7434 (interactive "P")
7435 (cond
7436 ((member strip '(nil 4))
7437 (let* ((efforts (split-string
7438 (or (cdr (assoc (concat org-effort-property "_ALL")
7439 org-global-properties))
7440 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
7441 ;; XXX: the following handles only up to 10 different
7442 ;; effort values.
7443 (allowed-keys (if (null efforts) nil
7444 (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
7445 (number-sequence 1 (length efforts)))))
7446 (op nil))
7447 (while (not (memq op '(?< ?> ?=)))
7448 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7449 ;; Select appropriate duration. Ignore non-digit characters.
7450 (let ((prompt
7451 (apply #'format
7452 (concat "Effort %c "
7453 (mapconcat (lambda (s) (concat "[%d]" s))
7454 efforts
7455 " "))
7456 op allowed-keys))
7457 (eff -1))
7458 (while (not (memq eff allowed-keys))
7459 (message prompt)
7460 (setq eff (- (read-char-exclusive) 48)))
7461 (setq org-agenda-effort-filter
7462 (list (concat (if strip "-" "+")
7463 (char-to-string op)
7464 ;; Numbering is 1 2 3 ... 9 0, but we want
7465 ;; 0 1 2 ... 8 9.
7466 (nth (mod (1- eff) 10) efforts)))))
7467 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7468 (t (org-agenda-filter-show-all-effort)
7469 (message "Effort filter removed"))))
7471 (defun org-agenda-filter-remove-all ()
7472 "Remove all filters from the current agenda buffer."
7473 (interactive)
7474 (when org-agenda-tag-filter
7475 (org-agenda-filter-show-all-tag))
7476 (when org-agenda-category-filter
7477 (org-agenda-filter-show-all-cat))
7478 (when org-agenda-regexp-filter
7479 (org-agenda-filter-show-all-re))
7480 (when org-agenda-top-headline-filter
7481 (org-agenda-filter-show-all-top-filter))
7482 (when org-agenda-effort-filter
7483 (org-agenda-filter-show-all-effort))
7484 (org-agenda-finalize))
7486 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7487 "Keep only those lines in the agenda buffer that have a specific tag.
7489 The tag is selected with its fast selection letter, as configured.
7491 With a `\\[universal-argument]' prefix, exclude the agenda search.
7493 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7494 i.e. don't
7495 filter on all its group members.
7497 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7498 should be used to exclude the search - the interactive user can
7499 also press `-' or `+' to switch between filtering and excluding."
7500 (interactive "P")
7501 (let* ((alist org-tag-alist-for-agenda)
7502 (tag-chars (mapconcat
7503 (lambda (x) (if (and (not (symbolp (car x)))
7504 (cdr x))
7505 (char-to-string (cdr x))
7506 ""))
7507 org-tag-alist-for-agenda ""))
7508 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7509 (string-to-list tag-chars)))
7510 (exclude (or exclude (equal arg '(4))))
7511 (expand (not (equal arg '(16))))
7512 (inhibit-read-only t)
7513 (current org-agenda-tag-filter)
7514 a n tag)
7515 (unless char
7516 (while (not (memq char valid-char-list))
7517 (message
7518 "%s by tag [%s ]:tag-char, [TAB]:tag, %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7519 (if exclude "Exclude" "Filter")
7520 tag-chars
7521 (if org-agenda-auto-exclude-function "[RET], " "")
7522 (if expand "" ", no grouptag expand"))
7523 (setq char (read-char-exclusive))
7524 ;; Excluding or filtering down
7525 (cond ((eq char ?-) (setq exclude t))
7526 ((eq char ?+) (setq exclude nil)))))
7527 (when (eq char ?\t)
7528 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7529 (setq-local org-global-tags-completion-table
7530 (org-global-tags-completion-table)))
7531 (let ((completion-ignore-case t))
7532 (setq tag (completing-read
7533 "Tag: " org-global-tags-completion-table nil t))))
7534 (cond
7535 ((eq char ?\r)
7536 (org-agenda-filter-show-all-tag)
7537 (when org-agenda-auto-exclude-function
7538 (setq org-agenda-tag-filter nil)
7539 (dolist (tag (org-agenda-get-represented-tags))
7540 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7541 (if modifier
7542 (push modifier org-agenda-tag-filter))))
7543 (if (not (null org-agenda-tag-filter))
7544 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7545 ((eq char ?/)
7546 (org-agenda-filter-show-all-tag)
7547 (when (get 'org-agenda-tag-filter :preset-filter)
7548 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7549 ((eq char ?.)
7550 (setq org-agenda-tag-filter
7551 (mapcar (lambda(tag) (concat "+" tag))
7552 (org-get-at-bol 'tags)))
7553 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7554 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7555 ((or (eq char ?\s)
7556 (setq a (rassoc char alist))
7557 (and tag (setq a (cons tag nil))))
7558 (org-agenda-filter-show-all-tag)
7559 (setq tag (car a))
7560 (setq org-agenda-tag-filter
7561 (cons (concat (if exclude "-" "+") tag)
7562 current))
7563 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7564 (t (error "Invalid tag selection character %c" char)))))
7566 (defun org-agenda-get-represented-tags ()
7567 "Get a list of all tags currently represented in the agenda."
7568 (let (p tags)
7569 (save-excursion
7570 (goto-char (point-min))
7571 (while (setq p (next-single-property-change (point) 'tags))
7572 (goto-char p)
7573 (mapc (lambda (x) (add-to-list 'tags x))
7574 (get-text-property (point) 'tags))))
7575 tags))
7578 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7579 "Create the form that tests a line for agenda filter. Optional
7580 argument EXPAND can be used for the TYPE tag and will expand the
7581 tags in the FILTER if any of the tags in FILTER are grouptags."
7582 (let (f f1)
7583 (cond
7584 ;; Tag filter
7585 ((eq type 'tag)
7586 (setq filter
7587 (delete-dups
7588 (append (get 'org-agenda-tag-filter :preset-filter)
7589 filter)))
7590 (dolist (x filter)
7591 (let ((op (string-to-char x)))
7592 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7593 (setq x (list x)))
7594 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7595 (push f1 f))))
7596 ;; Category filter
7597 ((eq type 'category)
7598 (setq filter
7599 (delete-dups
7600 (append (get 'org-agenda-category-filter :preset-filter)
7601 filter)))
7602 (dolist (x filter)
7603 (if (equal "-" (substring x 0 1))
7604 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7605 (setq f1 (list 'equal (substring x 1) 'cat)))
7606 (push f1 f)))
7607 ;; Regexp filter
7608 ((eq type 'regexp)
7609 (setq filter
7610 (delete-dups
7611 (append (get 'org-agenda-regexp-filter :preset-filter)
7612 filter)))
7613 (dolist (x filter)
7614 (if (equal "-" (substring x 0 1))
7615 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7616 (setq f1 (list 'string-match (substring x 1) 'txt)))
7617 (push f1 f)))
7618 ;; Effort filter
7619 ((eq type 'effort)
7620 (setq filter
7621 (delete-dups
7622 (append (get 'org-agenda-effort-filter :preset-filter)
7623 filter)))
7624 (dolist (x filter)
7625 (push (org-agenda-filter-effort-form x) f))))
7626 (cons 'and (nreverse f))))
7628 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7629 "Return a form associated to tag-expression TAGS.
7630 Build a form testing a line for agenda filter for
7631 tag-expressions. OP is an operator of type CHAR that allows the
7632 function to set the right switches in the returned form."
7633 (let (form)
7634 ;; Any of the expressions can match if OP is +, all must match if
7635 ;; the operator is -.
7636 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7637 (let* ((tag (substring x 1))
7638 (f (cond
7639 ((string= "" tag) '(not tags))
7640 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7641 ;; TAG is a regexp.
7642 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7643 (t (list 'member (downcase tag) 'tags)))))
7644 (push (if (eq op ?-) (list 'not f) f) form)))))
7646 (defun org-agenda-filter-effort-form (e)
7647 "Return the form to compare the effort of the current line with what E says.
7648 E looks like \"+<2:25\"."
7649 (let (op)
7650 (setq e (substring e 1))
7651 (setq op (string-to-char e) e (substring e 1))
7652 (setq op (cond ((equal op ?<) '<=)
7653 ((equal op ?>) '>=)
7654 ((equal op ??) op)
7655 (t '=)))
7656 (list 'org-agenda-compare-effort (list 'quote op)
7657 (org-duration-to-minutes e))))
7659 (defun org-agenda-compare-effort (op value)
7660 "Compare the effort of the current line with VALUE, using OP.
7661 If the line does not have an effort defined, return nil."
7662 ;; `effort-minutes' property cannot be extracted directly from
7663 ;; current line but is stored as a property in `txt'.
7664 (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
7665 (funcall op
7666 (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
7667 value)))
7669 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7670 "Expand group tags in FILTER for the agenda.
7671 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7672 (if org-group-tags
7673 (let ((case-fold-search t) rtn)
7674 (mapc
7675 (lambda (f)
7676 (let (f0 dir)
7677 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7678 (setq dir (match-string 1 f) f0 (match-string 2 f))
7679 (setq dir (if no-operator "" "+") f0 f))
7680 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7681 (org-tags-expand f0 t t))
7682 rtn))))
7683 filter)
7684 (reverse rtn))
7685 filter))
7687 (defun org-agenda-filter-apply (filter type &optional expand)
7688 "Set FILTER as the new agenda filter and apply it. Optional
7689 argument EXPAND can be used for the TYPE tag and will expand the
7690 tags in the FILTER if any of the tags in FILTER are grouptags."
7691 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7692 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7693 (let (tags cat txt)
7694 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7695 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7696 ;; category is used as the filter:
7697 (setq org-agenda-filtered-by-category
7698 (and (eq type 'category)
7699 (not (equal (substring (car filter) 0 1) "-"))))
7700 (org-agenda-set-mode-name)
7701 (save-excursion
7702 (goto-char (point-min))
7703 (while (not (eobp))
7704 (if (org-get-at-bol 'org-marker)
7705 (progn
7706 (setq tags (org-get-at-bol 'tags)
7707 cat (org-get-at-eol 'org-category 1)
7708 txt (org-get-at-bol 'txt))
7709 (if (not (eval org-agenda-filter-form))
7710 (org-agenda-filter-hide-line type))
7711 (beginning-of-line 2))
7712 (beginning-of-line 2))))
7713 (if (get-char-property (point) 'invisible)
7714 (ignore-errors (org-agenda-previous-line)))))
7716 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7717 "Filter by top headline HL."
7718 (org-agenda-set-mode-name)
7719 (save-excursion
7720 (goto-char (point-min))
7721 (while (not (eobp))
7722 (let* ((pos (org-get-at-bol 'org-hd-marker))
7723 (tophl (and pos (org-find-top-headline pos))))
7724 (if (and tophl (funcall (if negative 'identity 'not)
7725 (string= hl tophl)))
7726 (org-agenda-filter-hide-line 'top-headline)))
7727 (beginning-of-line 2)))
7728 (if (get-char-property (point) 'invisible)
7729 (org-agenda-previous-line))
7730 (setq org-agenda-top-headline-filter hl
7731 org-agenda-filtered-by-top-headline t))
7733 (defun org-agenda-filter-hide-line (type)
7734 "Hide lines with TYPE in the agenda buffer."
7735 (let* ((b (max (point-min) (1- (point-at-bol))))
7736 (e (point-at-eol)))
7737 (let ((inhibit-read-only t))
7738 (add-text-properties
7739 b e `(invisible org-filtered org-filter-type ,type)))))
7741 (defun org-agenda-remove-filter (type)
7742 (interactive)
7743 "Remove filter of type TYPE from the agenda buffer."
7744 (save-excursion
7745 (goto-char (point-min))
7746 (let ((inhibit-read-only t) pos)
7747 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7748 (goto-char pos)
7749 (remove-text-properties
7750 (point) (next-single-property-change (point) 'org-filter-type)
7751 `(invisible org-filtered org-filter-type ,type))))
7752 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7753 (setq org-agenda-filter-form nil)
7754 (org-agenda-set-mode-name)
7755 (org-agenda-finalize)))
7757 (defun org-agenda-filter-show-all-tag nil
7758 (org-agenda-remove-filter 'tag))
7759 (defun org-agenda-filter-show-all-re nil
7760 (org-agenda-remove-filter 'regexp))
7761 (defun org-agenda-filter-show-all-effort nil
7762 (org-agenda-remove-filter 'effort))
7763 (defun org-agenda-filter-show-all-cat nil
7764 (org-agenda-remove-filter 'category))
7765 (defun org-agenda-filter-show-all-top-filter nil
7766 (org-agenda-remove-filter 'top-headline))
7768 (defun org-agenda-manipulate-query-add ()
7769 "Manipulate the query by adding a search term with positive selection.
7770 Positive selection means the term must be matched for selection of an entry."
7771 (interactive)
7772 (org-agenda-manipulate-query ?\[))
7773 (defun org-agenda-manipulate-query-subtract ()
7774 "Manipulate the query by adding a search term with negative selection.
7775 Negative selection means term must not be matched for selection of an entry."
7776 (interactive)
7777 (org-agenda-manipulate-query ?\]))
7778 (defun org-agenda-manipulate-query-add-re ()
7779 "Manipulate the query by adding a search regexp with positive selection.
7780 Positive selection means the regexp must match for selection of an entry."
7781 (interactive)
7782 (org-agenda-manipulate-query ?\{))
7783 (defun org-agenda-manipulate-query-subtract-re ()
7784 "Manipulate the query by adding a search regexp with negative selection.
7785 Negative selection means regexp must not match for selection of an entry."
7786 (interactive)
7787 (org-agenda-manipulate-query ?\}))
7788 (defun org-agenda-manipulate-query (char)
7789 (cond
7790 ((eq org-agenda-type 'agenda)
7791 (let ((org-agenda-include-inactive-timestamps t))
7792 (org-agenda-redo))
7793 (message "Display now includes inactive timestamps as well"))
7794 ((eq org-agenda-type 'search)
7795 (org-add-to-string
7796 'org-agenda-query-string
7797 (if org-agenda-last-search-view-search-was-boolean
7798 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7799 (?\{ . " +{}") (?\} . " -{}"))))
7800 " "))
7801 (setq org-agenda-redo-command
7802 (list 'org-search-view
7803 (car (get-text-property (min (1- (point-max)) (point))
7804 'org-last-args))
7805 org-agenda-query-string
7806 (+ (length org-agenda-query-string)
7807 (if (member char '(?\{ ?\})) 0 1))))
7808 (set-register org-agenda-query-register org-agenda-query-string)
7809 (let ((org-agenda-overriding-arguments
7810 (cdr org-agenda-redo-command)))
7811 (org-agenda-redo)))
7812 (t (error "Cannot manipulate query for %s-type agenda buffers"
7813 org-agenda-type))))
7815 (defun org-add-to-string (var string)
7816 (set var (concat (symbol-value var) string)))
7818 (defun org-agenda-goto-date (span)
7819 "Jump to DATE in agenda."
7820 (interactive "P")
7821 (let* ((org-read-date-prefer-future
7822 (eval org-agenda-jump-prefer-future))
7823 (date (org-read-date))
7824 (day (time-to-days (org-time-string-to-time date)))
7825 (org-agenda-sticky-orig org-agenda-sticky)
7826 (org-agenda-buffer-tmp-name (buffer-name))
7827 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7828 (0-arg (or current-prefix-arg (car args)))
7829 (2-arg (nth 2 args))
7830 (with-hour-p (nth 4 org-agenda-redo-command))
7831 (newcmd (list 'org-agenda-list 0-arg date
7832 (org-agenda-span-to-ndays
7833 2-arg (org-time-string-to-absolute date))
7834 with-hour-p))
7835 (newargs (cdr newcmd))
7836 (inhibit-read-only t)
7837 org-agenda-sticky)
7838 (if (not (org-agenda-check-type t 'agenda))
7839 (error "Not available in non-agenda views")
7840 (add-text-properties (point-min) (point-max)
7841 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7842 (org-agenda-redo)
7843 (goto-char (point-min))
7844 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7845 (save-excursion (move-beginning-of-line 2) (eobp))))
7846 (move-beginning-of-line 2))
7847 (setq org-agenda-sticky org-agenda-sticky-orig
7848 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7850 (defun org-agenda-goto-today ()
7851 "Go to today."
7852 (interactive)
7853 (org-agenda-check-type t 'agenda)
7854 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7855 (curspan (nth 2 args))
7856 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7857 (cond
7858 (tdpos (goto-char tdpos))
7859 ((eq org-agenda-type 'agenda)
7860 (let* ((sd (org-agenda-compute-starting-span
7861 (org-today) (or curspan org-agenda-span)))
7862 (org-agenda-overriding-arguments args))
7863 (setf (nth 1 org-agenda-overriding-arguments) sd)
7864 (org-agenda-redo)
7865 (org-agenda-find-same-or-today-or-agenda)))
7866 (t (error "Cannot find today")))))
7868 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7869 (goto-char
7870 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7871 (text-property-any (point-min) (point-max) 'org-today t)
7872 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7873 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7874 (org-agenda-backward-block))
7875 (point-min))))
7877 (defun org-agenda-backward-block ()
7878 "Move backward by one agenda block."
7879 (interactive)
7880 (org-agenda-forward-block 'backward))
7882 (defun org-agenda-forward-block (&optional backward)
7883 "Move forward by one agenda block.
7884 When optional argument BACKWARD is set, go backward"
7885 (interactive)
7886 (cond ((not (derived-mode-p 'org-agenda-mode))
7887 (user-error
7888 "Cannot execute this command outside of org-agenda-mode buffers"))
7889 ((looking-at (if backward "\\`" "\\'"))
7890 (message "Already at the %s block" (if backward "first" "last")))
7891 (t (let ((pos (prog1 (point)
7892 (ignore-errors (if backward (backward-char 1)
7893 (move-end-of-line 1)))))
7894 (f (if backward
7895 'previous-single-property-change
7896 'next-single-property-change))
7897 moved dest)
7898 (while (and (setq dest (funcall
7899 f (point) 'org-agenda-structural-header))
7900 (not (get-text-property
7901 (point) 'org-agenda-structural-header)))
7902 (setq moved t)
7903 (goto-char dest))
7904 (if moved (move-beginning-of-line 1)
7905 (goto-char (if backward (point-min) (point-max)))
7906 (move-beginning-of-line 1)
7907 (message "No %s block" (if backward "previous" "further")))))))
7909 (defun org-agenda-later (arg)
7910 "Go forward in time by the current span.
7911 With prefix ARG, go forward that many times the current span."
7912 (interactive "p")
7913 (org-agenda-check-type t 'agenda)
7914 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7915 (span (or (nth 2 args) org-agenda-current-span))
7916 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7917 (greg (calendar-gregorian-from-absolute sd))
7918 (cnt (org-get-at-bol 'org-day-cnt))
7919 greg2)
7920 (cond
7921 ((numberp span)
7922 (setq sd (+ (* span arg) sd)))
7923 ((eq span 'day)
7924 (setq sd (+ arg sd)))
7925 ((eq span 'week)
7926 (setq sd (+ (* 7 arg) sd)))
7927 ((eq span 'fortnight)
7928 (setq sd (+ (* 14 arg) sd)))
7929 ((eq span 'month)
7930 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7931 sd (calendar-absolute-from-gregorian greg2))
7932 (setcar greg2 (1+ (car greg2))))
7933 ((eq span 'year)
7934 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7935 sd (calendar-absolute-from-gregorian greg2))
7936 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7938 (setq sd (+ (* span arg) sd))))
7939 (let ((org-agenda-overriding-cmd
7940 ;; `cmd' may have been set by `org-agenda-run-series' which
7941 ;; uses `org-agenda-overriding-cmd' to decide whether
7942 ;; overriding is allowed for `cmd'
7943 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7944 (org-agenda-overriding-arguments
7945 (list (car args) sd span)))
7946 (org-agenda-redo)
7947 (org-agenda-find-same-or-today-or-agenda cnt))))
7949 (defun org-agenda-earlier (arg)
7950 "Go backward in time by the current span.
7951 With prefix ARG, go backward that many times the current span."
7952 (interactive "p")
7953 (org-agenda-later (- arg)))
7955 (defun org-agenda-view-mode-dispatch ()
7956 "Call one of the view mode commands."
7957 (interactive)
7958 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7959 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7960 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7961 (pcase (read-char-exclusive)
7962 (?\ (call-interactively 'org-agenda-reset-view))
7963 (?d (call-interactively 'org-agenda-day-view))
7964 (?w (call-interactively 'org-agenda-week-view))
7965 (?t (call-interactively 'org-agenda-fortnight-view))
7966 (?m (call-interactively 'org-agenda-month-view))
7967 (?y (call-interactively 'org-agenda-year-view))
7968 (?l (call-interactively 'org-agenda-log-mode))
7969 (?L (org-agenda-log-mode '(4)))
7970 (?c (org-agenda-log-mode 'clockcheck))
7971 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7972 (?a (call-interactively 'org-agenda-archives-mode))
7973 (?A (org-agenda-archives-mode 'files))
7974 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7975 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7976 (?G (call-interactively 'org-agenda-toggle-time-grid))
7977 (?D (call-interactively 'org-agenda-toggle-diary))
7978 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7979 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7980 (org-agenda-check-type t 'agenda)
7981 (org-agenda-redo))
7982 (message "Display now includes inactive timestamps as well"))
7983 (?q (message "Abort"))
7984 (key (user-error "Invalid key: %s" key))))
7986 (defun org-agenda-reset-view ()
7987 "Switch to default view for agenda."
7988 (interactive)
7989 (org-agenda-change-time-span org-agenda-span))
7991 (defun org-agenda-day-view (&optional day-of-month)
7992 "Switch to daily view for agenda.
7993 With argument DAY-OF-MONTH, switch to that day of the month."
7994 (interactive "P")
7995 (org-agenda-change-time-span 'day day-of-month))
7997 (defun org-agenda-week-view (&optional iso-week)
7998 "Switch to weekly view for agenda.
7999 With argument ISO-WEEK, switch to the corresponding ISO week.
8000 If ISO-WEEK has more then 2 digits, only the last two encode
8001 the week. Any digits before this encode a year. So 200712
8002 means week 12 of year 2007. Years ranging from 70 years ago
8003 to 30 years in the future can also be written as 2-digit years."
8004 (interactive "P")
8005 (org-agenda-change-time-span 'week iso-week))
8007 (defun org-agenda-fortnight-view (&optional iso-week)
8008 "Switch to fortnightly view for agenda.
8009 With argument ISO-WEEK, switch to the corresponding ISO week.
8010 If ISO-WEEK has more then 2 digits, only the last two encode
8011 the week. Any digits before this encode a year. So 200712
8012 means week 12 of year 2007. Years ranging from 70 years ago
8013 to 30 years in the future can also be written as 2-digit years."
8014 (interactive "P")
8015 (org-agenda-change-time-span 'fortnight iso-week))
8017 (defun org-agenda-month-view (&optional month)
8018 "Switch to monthly view for agenda.
8019 With argument MONTH, switch to that month. If MONTH has more
8020 then 2 digits, only the last two encode the month. Any digits
8021 before this encode a year. So 200712 means December year 2007.
8022 Years ranging from 70 years ago to 30 years in the future can
8023 also be written as 2-digit years."
8024 (interactive "P")
8025 (org-agenda-change-time-span 'month month))
8027 (defun org-agenda-year-view (&optional year)
8028 "Switch to yearly view for agenda.
8029 With argument YEAR, switch to that year. Years ranging from 70
8030 years ago to 30 years in the future can also be written as
8031 2-digit years."
8032 (interactive "P")
8033 (when year
8034 (setq year (org-small-year-to-year year)))
8035 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8036 (org-agenda-change-time-span 'year year)
8037 (error "Abort")))
8039 (defun org-agenda-change-time-span (span &optional n)
8040 "Change the agenda view to SPAN.
8041 SPAN may be `day', `week', `fortnight', `month', `year'."
8042 (org-agenda-check-type t 'agenda)
8043 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8044 (curspan (nth 2 args)))
8045 (if (and (not n) (equal curspan span))
8046 (error "Viewing span is already \"%s\"" span))
8047 (let* ((sd (or (org-get-at-bol 'day)
8048 (nth 1 args)
8049 org-starting-day))
8050 (sd (org-agenda-compute-starting-span sd span n))
8051 (org-agenda-overriding-cmd
8052 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8053 (org-agenda-overriding-arguments
8054 (list (car args) sd span)))
8055 (org-agenda-redo)
8056 (org-agenda-find-same-or-today-or-agenda))
8057 (org-agenda-set-mode-name)
8058 (message "Switched to %s view" span)))
8060 (defun org-agenda-compute-starting-span (sd span &optional n)
8061 "Compute starting date for agenda.
8062 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8063 is a cons cell with the starting date and the number of days,
8064 so that the date SD will be in that range."
8065 (let* ((greg (calendar-gregorian-from-absolute sd))
8066 (dg (nth 1 greg))
8067 (mg (car greg))
8068 (yg (nth 2 greg)))
8069 (cond
8070 ((eq span 'day)
8071 (when n
8072 (setq sd (+ (calendar-absolute-from-gregorian
8073 (list mg 1 yg))
8074 n -1))))
8075 ((or (eq span 'week) (eq span 'fortnight))
8076 (let* ((nt (calendar-day-of-week
8077 (calendar-gregorian-from-absolute sd)))
8078 (d (if org-agenda-start-on-weekday
8079 (- nt org-agenda-start-on-weekday)
8082 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8083 (when n
8084 (require 'cal-iso)
8085 (when (> n 99)
8086 (setq y1 (org-small-year-to-year (/ n 100))
8087 n (mod n 100)))
8088 (setq sd
8089 (calendar-iso-to-absolute
8090 (list n 1
8091 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8092 ((eq span 'month)
8093 (let (y1)
8094 (when (and n (> n 99))
8095 (setq y1 (org-small-year-to-year (/ n 100))
8096 n (mod n 100)))
8097 (setq sd (calendar-absolute-from-gregorian
8098 (list (or n mg) 1 (or y1 yg))))))
8099 ((eq span 'year)
8100 (setq sd (calendar-absolute-from-gregorian
8101 (list 1 1 (or n yg))))))
8102 sd))
8104 (defun org-agenda-next-date-line (&optional arg)
8105 "Jump to the next line indicating a date in agenda buffer."
8106 (interactive "p")
8107 (org-agenda-check-type t 'agenda)
8108 (beginning-of-line 1)
8109 ;; This does not work if user makes date format that starts with a blank
8110 (if (looking-at "^\\S-") (forward-char 1))
8111 (if (not (re-search-forward "^\\S-" nil t arg))
8112 (progn
8113 (backward-char 1)
8114 (error "No next date after this line in this buffer")))
8115 (goto-char (match-beginning 0)))
8117 (defun org-agenda-previous-date-line (&optional arg)
8118 "Jump to the previous line indicating a date in agenda buffer."
8119 (interactive "p")
8120 (org-agenda-check-type t 'agenda)
8121 (beginning-of-line 1)
8122 (if (not (re-search-backward "^\\S-" nil t arg))
8123 (error "No previous date before this line in this buffer")))
8125 ;; Initialize the highlight
8126 (defvar org-hl (make-overlay 1 1))
8127 (overlay-put org-hl 'face 'highlight)
8129 (defun org-highlight (begin end &optional buffer)
8130 "Highlight a region with overlay."
8131 (move-overlay org-hl begin end (or buffer (current-buffer))))
8133 (defun org-unhighlight ()
8134 "Detach overlay INDEX."
8135 (delete-overlay org-hl))
8137 (defun org-unhighlight-once ()
8138 "Remove the highlight from its position, and this function from the hook."
8139 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8140 (org-unhighlight))
8142 (defvar org-agenda-pre-follow-window-conf nil)
8143 (defun org-agenda-follow-mode ()
8144 "Toggle follow mode in an agenda buffer."
8145 (interactive)
8146 (unless org-agenda-follow-mode
8147 (setq org-agenda-pre-follow-window-conf
8148 (current-window-configuration)))
8149 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8150 (unless org-agenda-follow-mode
8151 (set-window-configuration org-agenda-pre-follow-window-conf))
8152 (org-agenda-set-mode-name)
8153 (org-agenda-do-context-action)
8154 (message "Follow mode is %s"
8155 (if org-agenda-follow-mode "on" "off")))
8157 (defun org-agenda-entry-text-mode (&optional arg)
8158 "Toggle entry text mode in an agenda buffer."
8159 (interactive "P")
8160 (if (or org-agenda-tag-filter
8161 org-agenda-category-filter
8162 org-agenda-regexp-filter
8163 org-agenda-top-headline-filter)
8164 (user-error "Can't show entry text in filtered views")
8165 (setq org-agenda-entry-text-mode (or (integerp arg)
8166 (not org-agenda-entry-text-mode)))
8167 (org-agenda-entry-text-hide)
8168 (and org-agenda-entry-text-mode
8169 (let ((org-agenda-entry-text-maxlines
8170 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8171 (org-agenda-entry-text-show)))
8172 (org-agenda-set-mode-name)
8173 (message "Entry text mode is %s%s"
8174 (if org-agenda-entry-text-mode "on" "off")
8175 (if (not org-agenda-entry-text-mode) ""
8176 (format " (maximum number of lines is %d)"
8177 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8179 (defun org-agenda-clockreport-mode ()
8180 "Toggle clocktable mode in an agenda buffer."
8181 (interactive)
8182 (org-agenda-check-type t 'agenda)
8183 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8184 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8185 (org-agenda-set-mode-name)
8186 (org-agenda-redo)
8187 (message "Clocktable mode is %s"
8188 (if org-agenda-clockreport-mode "on" "off")))
8190 (defun org-agenda-log-mode (&optional special)
8191 "Toggle log mode in an agenda buffer.
8193 With argument SPECIAL, show all possible log items, not only the ones
8194 configured in `org-agenda-log-mode-items'.
8196 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8197 log items, nothing else."
8198 (interactive "P")
8199 (org-agenda-check-type t 'agenda)
8200 (setq org-agenda-show-log
8201 (cond
8202 ((equal special '(16)) 'only)
8203 ((eq special 'clockcheck)
8204 (if (eq org-agenda-show-log 'clockcheck)
8205 nil 'clockcheck))
8206 (special '(closed clock state))
8207 (t (not org-agenda-show-log))))
8208 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8209 (org-agenda-set-mode-name)
8210 (org-agenda-redo)
8211 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8213 (defun org-agenda-archives-mode (&optional with-files)
8214 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8215 When called with a prefix argument, include all archive files as well."
8216 (interactive "P")
8217 (setq org-agenda-archives-mode
8218 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8219 (org-agenda-set-mode-name)
8220 (org-agenda-redo)
8221 (message
8222 "%s"
8223 (cond
8224 ((eq org-agenda-archives-mode nil)
8225 "No archives are included")
8226 ((eq org-agenda-archives-mode 'trees)
8227 (format "Trees with :%s: tag are included" org-archive-tag))
8228 ((eq org-agenda-archives-mode t)
8229 (format "Trees with :%s: tag and all active archive files are included"
8230 org-archive-tag)))))
8232 (defun org-agenda-toggle-diary ()
8233 "Toggle diary inclusion in an agenda buffer."
8234 (interactive)
8235 (org-agenda-check-type t 'agenda)
8236 (setq org-agenda-include-diary (not org-agenda-include-diary))
8237 (org-agenda-redo)
8238 (org-agenda-set-mode-name)
8239 (message "Diary inclusion turned %s"
8240 (if org-agenda-include-diary "on" "off")))
8242 (defun org-agenda-toggle-deadlines ()
8243 "Toggle inclusion of entries with a deadline in an agenda buffer."
8244 (interactive)
8245 (org-agenda-check-type t 'agenda)
8246 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8247 (org-agenda-redo)
8248 (org-agenda-set-mode-name)
8249 (message "Deadlines inclusion turned %s"
8250 (if org-agenda-include-deadlines "on" "off")))
8252 (defun org-agenda-toggle-time-grid ()
8253 "Toggle time grid in an agenda buffer."
8254 (interactive)
8255 (org-agenda-check-type t 'agenda)
8256 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8257 (org-agenda-redo)
8258 (org-agenda-set-mode-name)
8259 (message "Time-grid turned %s"
8260 (if org-agenda-use-time-grid "on" "off")))
8262 (defun org-agenda-set-mode-name ()
8263 "Set the mode name to indicate all the small mode settings."
8264 (setq mode-name
8265 (list "Org-Agenda"
8266 (if (get 'org-agenda-files 'org-restrict) " []" "")
8268 '(:eval (org-agenda-span-name org-agenda-current-span))
8269 (if org-agenda-follow-mode " Follow" "")
8270 (if org-agenda-entry-text-mode " ETxt" "")
8271 (if org-agenda-include-diary " Diary" "")
8272 (if org-agenda-include-deadlines " Ddl" "")
8273 (if org-agenda-use-time-grid " Grid" "")
8274 (if (and (boundp 'org-habit-show-habits)
8275 org-habit-show-habits) " Habit" "")
8276 (cond
8277 ((consp org-agenda-show-log) " LogAll")
8278 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8279 (org-agenda-show-log " Log")
8280 (t ""))
8281 (if (or org-agenda-category-filter
8282 (get 'org-agenda-category-filter :preset-filter))
8283 '(:eval (propertize
8284 (concat " <"
8285 (mapconcat
8286 'identity
8287 (append
8288 (get 'org-agenda-category-filter :preset-filter)
8289 org-agenda-category-filter)
8291 ">")
8292 'face 'org-agenda-filter-category
8293 'help-echo "Category used in filtering")) "")
8294 (if (or org-agenda-tag-filter
8295 (get 'org-agenda-tag-filter :preset-filter))
8296 '(:eval (propertize
8297 (concat " {"
8298 (mapconcat
8299 'identity
8300 (append
8301 (get 'org-agenda-tag-filter :preset-filter)
8302 org-agenda-tag-filter)
8304 "}")
8305 'face 'org-agenda-filter-tags
8306 'help-echo "Tags used in filtering")) "")
8307 (if (or org-agenda-effort-filter
8308 (get 'org-agenda-effort-filter :preset-filter))
8309 '(:eval (propertize
8310 (concat " {"
8311 (mapconcat
8312 'identity
8313 (append
8314 (get 'org-agenda-effort-filter :preset-filter)
8315 org-agenda-effort-filter)
8317 "}")
8318 'face 'org-agenda-filter-effort
8319 'help-echo "Effort conditions used in filtering")) "")
8320 (if (or org-agenda-regexp-filter
8321 (get 'org-agenda-regexp-filter :preset-filter))
8322 '(:eval (propertize
8323 (concat " ["
8324 (mapconcat
8325 'identity
8326 (append
8327 (get 'org-agenda-regexp-filter :preset-filter)
8328 org-agenda-regexp-filter)
8330 "]")
8331 'face 'org-agenda-filter-regexp
8332 'help-echo "Regexp used in filtering")) "")
8333 (if org-agenda-archives-mode
8334 (if (eq org-agenda-archives-mode t)
8335 " Archives"
8336 (format " :%s:" org-archive-tag))
8338 (if org-agenda-clockreport-mode " Clock" "")))
8339 (force-mode-line-update))
8341 (defun org-agenda-update-agenda-type ()
8342 "Update the agenda type after each command."
8343 (setq org-agenda-type
8344 (or (get-text-property (point) 'org-agenda-type)
8345 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8347 (defun org-agenda-next-line ()
8348 "Move cursor to the next line, and show if follow mode is active."
8349 (interactive)
8350 (call-interactively 'next-line)
8351 (org-agenda-do-context-action))
8353 (defun org-agenda-previous-line ()
8354 "Move cursor to the previous line, and show if follow-mode is active."
8355 (interactive)
8356 (call-interactively 'previous-line)
8357 (org-agenda-do-context-action))
8359 (defun org-agenda-next-item (n)
8360 "Move cursor to next agenda item."
8361 (interactive "p")
8362 (let ((col (current-column)))
8363 (dotimes (c n)
8364 (when (next-single-property-change (point-at-eol) 'org-marker)
8365 (move-end-of-line 1)
8366 (goto-char (next-single-property-change (point) 'org-marker))))
8367 (org-move-to-column col))
8368 (org-agenda-do-context-action))
8370 (defun org-agenda-previous-item (n)
8371 "Move cursor to next agenda item."
8372 (interactive "p")
8373 (dotimes (c n)
8374 (let ((col (current-column))
8375 (goto (save-excursion
8376 (move-end-of-line 0)
8377 (previous-single-property-change (point) 'org-marker))))
8378 (if goto (goto-char goto))
8379 (org-move-to-column col)))
8380 (org-agenda-do-context-action))
8382 (defun org-agenda-do-context-action ()
8383 "Show outline path and, maybe, follow mode window."
8384 (let ((m (org-get-at-bol 'org-marker)))
8385 (when (and (markerp m) (marker-buffer m))
8386 (and org-agenda-follow-mode
8387 (if org-agenda-follow-indirect
8388 (org-agenda-tree-to-indirect-buffer nil)
8389 (org-agenda-show)))
8390 (and org-agenda-show-outline-path
8391 (org-with-point-at m (org-display-outline-path t))))))
8393 (defun org-agenda-show-tags ()
8394 "Show the tags applicable to the current item."
8395 (interactive)
8396 (let* ((tags (org-get-at-bol 'tags)))
8397 (if tags
8398 (message "Tags are :%s:"
8399 (org-no-properties (mapconcat 'identity tags ":")))
8400 (message "No tags associated with this line"))))
8402 (defun org-agenda-goto (&optional highlight)
8403 "Go to the entry at point in the corresponding Org file."
8404 (interactive)
8405 (let* ((marker (or (org-get-at-bol 'org-marker)
8406 (org-agenda-error)))
8407 (buffer (marker-buffer marker))
8408 (pos (marker-position marker)))
8409 (switch-to-buffer-other-window buffer)
8410 (widen)
8411 (push-mark)
8412 (goto-char pos)
8413 (when (derived-mode-p 'org-mode)
8414 (org-show-context 'agenda)
8415 (recenter (/ (window-height) 2))
8416 (org-back-to-heading t)
8417 (let ((case-fold-search nil))
8418 (when (re-search-forward org-complex-heading-regexp nil t)
8419 (goto-char (match-beginning 4)))))
8420 (run-hooks 'org-agenda-after-show-hook)
8421 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8423 (defvar org-agenda-after-show-hook nil
8424 "Normal hook run after an item has been shown from the agenda.
8425 Point is in the buffer where the item originated.")
8427 (defun org-agenda-kill ()
8428 "Kill the entry or subtree belonging to the current agenda entry."
8429 (interactive)
8430 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8431 (let* ((bufname-orig (buffer-name))
8432 (marker (or (org-get-at-bol 'org-marker)
8433 (org-agenda-error)))
8434 (buffer (marker-buffer marker))
8435 (pos (marker-position marker))
8436 (type (org-get-at-bol 'type))
8437 dbeg dend (n 0) conf)
8438 (org-with-remote-undo buffer
8439 (with-current-buffer buffer
8440 (save-excursion
8441 (goto-char pos)
8442 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8443 (setq dbeg (progn (org-back-to-heading t) (point))
8444 dend (org-end-of-subtree t t))
8445 (setq dbeg (point-at-bol)
8446 dend (min (point-max) (1+ (point-at-eol)))))
8447 (goto-char dbeg)
8448 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8449 (setq conf (or (eq t org-agenda-confirm-kill)
8450 (and (numberp org-agenda-confirm-kill)
8451 (> n org-agenda-confirm-kill))))
8452 (and conf
8453 (not (y-or-n-p
8454 (format "Delete entry with %d lines in buffer \"%s\"? "
8455 n (buffer-name buffer))))
8456 (error "Abort"))
8457 (let ((org-agenda-buffer-name bufname-orig))
8458 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8459 (with-current-buffer buffer (delete-region dbeg dend))
8460 (message "Agenda item and source killed"))))
8462 (defvar org-archive-default-command) ; defined in org-archive.el
8463 (defun org-agenda-archive-default ()
8464 "Archive the entry or subtree belonging to the current agenda entry."
8465 (interactive)
8466 (require 'org-archive)
8467 (org-agenda-archive-with org-archive-default-command))
8469 (defun org-agenda-archive-default-with-confirmation ()
8470 "Archive the entry or subtree belonging to the current agenda entry."
8471 (interactive)
8472 (require 'org-archive)
8473 (org-agenda-archive-with org-archive-default-command 'confirm))
8475 (defun org-agenda-archive ()
8476 "Archive the entry or subtree belonging to the current agenda entry."
8477 (interactive)
8478 (org-agenda-archive-with 'org-archive-subtree))
8480 (defun org-agenda-archive-to-archive-sibling ()
8481 "Move the entry to the archive sibling."
8482 (interactive)
8483 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8485 (defun org-agenda-archive-with (cmd &optional confirm)
8486 "Move the entry to the archive sibling."
8487 (interactive)
8488 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8489 (let* ((bufname-orig (buffer-name))
8490 (marker (or (org-get-at-bol 'org-marker)
8491 (org-agenda-error)))
8492 (buffer (marker-buffer marker))
8493 (pos (marker-position marker)))
8494 (org-with-remote-undo buffer
8495 (with-current-buffer buffer
8496 (if (derived-mode-p 'org-mode)
8497 (if (and confirm
8498 (not (y-or-n-p "Archive this subtree or entry? ")))
8499 (error "Abort")
8500 (save-window-excursion
8501 (goto-char pos)
8502 (let ((org-agenda-buffer-name bufname-orig))
8503 (org-remove-subtree-entries-from-agenda))
8504 (org-back-to-heading t)
8505 (funcall cmd)))
8506 (error "Archiving works only in Org files"))))))
8508 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8509 "Remove all lines in the agenda that correspond to a given subtree.
8510 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8511 If this information is not given, the function uses the tree at point."
8512 (let ((buf (or buf (current-buffer))) m p)
8513 (save-excursion
8514 (unless (and beg end)
8515 (org-back-to-heading t)
8516 (setq beg (point))
8517 (org-end-of-subtree t)
8518 (setq end (point)))
8519 (set-buffer (get-buffer org-agenda-buffer-name))
8520 (save-excursion
8521 (goto-char (point-max))
8522 (beginning-of-line 1)
8523 (while (not (bobp))
8524 (when (and (setq m (org-get-at-bol 'org-marker))
8525 (equal buf (marker-buffer m))
8526 (setq p (marker-position m))
8527 (>= p beg)
8528 (< p end))
8529 (let ((inhibit-read-only t))
8530 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8531 (beginning-of-line 0))))))
8533 (defun org-agenda-refile (&optional goto rfloc no-update)
8534 "Refile the item at point.
8536 When called with `\\[universal-argument] \\[universal-argument]', \
8537 go to the location of the last
8538 refiled item.
8540 When called with `\\[universal-argument] \\[universal-argument] \
8541 \\[universal-argument]' prefix or when GOTO is 0, clear
8542 the refile cache.
8544 RFLOC can be a refile location obtained in a different way.
8546 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8547 (interactive "P")
8548 (cond
8549 ((member goto '(0 (64)))
8550 (org-refile-cache-clear))
8551 ((equal goto '(16))
8552 (org-refile-goto-last-stored))
8554 (let* ((buffer-orig (buffer-name))
8555 (marker (or (org-get-at-bol 'org-hd-marker)
8556 (org-agenda-error)))
8557 (buffer (marker-buffer marker))
8558 (pos (marker-position marker))
8559 (rfloc (or rfloc
8560 (org-refile-get-location
8561 (if goto "Goto" "Refile to") buffer
8562 org-refile-allow-creating-parent-nodes))))
8563 (with-current-buffer buffer
8564 (org-with-wide-buffer
8565 (goto-char marker)
8566 (let ((org-agenda-buffer-name buffer-orig))
8567 (org-remove-subtree-entries-from-agenda))
8568 (org-refile goto buffer rfloc))))
8569 (unless no-update (org-agenda-redo)))))
8571 (defun org-agenda-open-link (&optional arg)
8572 "Open the link(s) in the current entry, if any.
8573 This looks for a link in the displayed line in the agenda.
8574 It also looks at the text of the entry itself."
8575 (interactive "P")
8576 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8577 (org-get-at-bol 'org-marker)))
8578 (buffer (and marker (marker-buffer marker)))
8579 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8580 (lkall (and buffer (org-offer-links-in-entry
8581 buffer marker arg prefix)))
8582 (lk0 (car lkall))
8583 (lk (if (stringp lk0) (list lk0) lk0))
8584 (lkend (cdr lkall))
8585 trg)
8586 (cond
8587 ((and buffer lk)
8588 (mapcar (lambda(l)
8589 (with-current-buffer buffer
8590 (setq trg (and (string-match org-bracket-link-regexp l)
8591 (match-string 1 l)))
8592 (if (or (not trg) (string-match org-any-link-re trg))
8593 (org-with-wide-buffer
8594 (goto-char marker)
8595 (when (search-forward l nil lkend)
8596 (goto-char (match-beginning 0))
8597 (org-open-at-point)))
8598 ;; This is an internal link, widen the buffer
8599 (switch-to-buffer-other-window buffer)
8600 (widen)
8601 (goto-char marker)
8602 (when (search-forward l nil lkend)
8603 (goto-char (match-beginning 0))
8604 (org-open-at-point)))))
8605 lk))
8606 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8607 (save-excursion
8608 (beginning-of-line 1)
8609 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8610 (org-open-link-from-string (match-string 1)))
8611 (t (message "No link to open here")))))
8613 (defun org-agenda-copy-local-variable (var)
8614 "Get a variable from a referenced buffer and install it here."
8615 (let ((m (org-get-at-bol 'org-marker)))
8616 (when (and m (buffer-live-p (marker-buffer m)))
8617 (set (make-local-variable var)
8618 (with-current-buffer (marker-buffer m)
8619 (symbol-value var))))))
8621 (defun org-agenda-switch-to (&optional delete-other-windows)
8622 "Go to the Org mode file which contains the item at point.
8623 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8624 displayed Org file fills the frame."
8625 (interactive)
8626 (if (and org-return-follows-link
8627 (not (org-get-at-bol 'org-marker))
8628 (org-in-regexp org-bracket-link-regexp))
8629 (org-open-link-from-string (match-string 0))
8630 (let* ((marker (or (org-get-at-bol 'org-marker)
8631 (org-agenda-error)))
8632 (buffer (marker-buffer marker))
8633 (pos (marker-position marker)))
8634 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8635 (pop-to-buffer-same-window buffer)
8636 (when delete-other-windows (delete-other-windows))
8637 (widen)
8638 (goto-char pos)
8639 (when (derived-mode-p 'org-mode)
8640 (org-show-context 'agenda)
8641 (run-hooks 'org-agenda-after-show-hook)))))
8643 (defun org-agenda-goto-mouse (ev)
8644 "Go to the Org file which contains the item at the mouse click."
8645 (interactive "e")
8646 (mouse-set-point ev)
8647 (org-agenda-goto))
8649 (defun org-agenda-show (&optional full-entry)
8650 "Display the Org file which contains the item at point.
8651 With prefix argument FULL-ENTRY, make the entire entry visible
8652 if it was hidden in the outline."
8653 (interactive "P")
8654 (let ((win (selected-window)))
8655 (org-agenda-goto t)
8656 (when full-entry (org-show-entry))
8657 (select-window win)))
8659 (defvar org-agenda-show-window nil)
8660 (defun org-agenda-show-and-scroll-up (&optional arg)
8661 "Display the Org file which contains the item at point.
8663 When called repeatedly, scroll the window that is displaying the buffer.
8665 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8666 `outline-show-subtree'
8667 to display the item, so that drawers and logbooks stay folded."
8668 (interactive "P")
8669 (let ((win (selected-window)))
8670 (if (and (window-live-p org-agenda-show-window)
8671 (eq this-command last-command))
8672 (progn
8673 (select-window org-agenda-show-window)
8674 (ignore-errors (scroll-up)))
8675 (org-agenda-goto t)
8676 (if arg (org-show-entry) (outline-show-subtree))
8677 (setq org-agenda-show-window (selected-window)))
8678 (select-window win)))
8680 (defun org-agenda-show-scroll-down ()
8681 "Scroll down the window showing the agenda."
8682 (interactive)
8683 (let ((win (selected-window)))
8684 (when (window-live-p org-agenda-show-window)
8685 (select-window org-agenda-show-window)
8686 (ignore-errors (scroll-down))
8687 (select-window win))))
8689 (defun org-agenda-show-1 (&optional more)
8690 "Display the Org file which contains the item at point.
8691 The prefix arg selects the amount of information to display:
8693 0 hide the subtree
8694 1 just show the entry according to defaults.
8695 2 show the children view
8696 3 show the subtree view
8697 4 show the entire subtree and any LOGBOOK drawers
8698 5 show the entire subtree and any drawers
8699 With prefix argument FULL-ENTRY, make the entire entry visible
8700 if it was hidden in the outline."
8701 (interactive "p")
8702 (let ((win (selected-window)))
8703 (org-agenda-goto t)
8704 (org-back-to-heading)
8705 (set-window-start (selected-window) (point-at-bol))
8706 (cond
8707 ((= more 0)
8708 (outline-hide-subtree)
8709 (save-excursion
8710 (org-back-to-heading)
8711 (run-hook-with-args 'org-cycle-hook 'folded))
8712 (message "Remote: FOLDED"))
8713 ((and (called-interactively-p 'any) (= more 1))
8714 (message "Remote: show with default settings"))
8715 ((= more 2)
8716 (outline-show-entry)
8717 (org-show-children)
8718 (save-excursion
8719 (org-back-to-heading)
8720 (run-hook-with-args 'org-cycle-hook 'children))
8721 (message "Remote: CHILDREN"))
8722 ((= more 3)
8723 (outline-show-subtree)
8724 (save-excursion
8725 (org-back-to-heading)
8726 (run-hook-with-args 'org-cycle-hook 'subtree))
8727 (message "Remote: SUBTREE"))
8728 ((= more 4)
8729 (outline-show-subtree)
8730 (save-excursion
8731 (org-back-to-heading)
8732 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8733 (message "Remote: SUBTREE AND LOGBOOK"))
8734 ((> more 4)
8735 (outline-show-subtree)
8736 (message "Remote: SUBTREE AND ALL DRAWERS")))
8737 (select-window win)))
8739 (defvar org-agenda-cycle-counter nil)
8740 (defun org-agenda-cycle-show (&optional n)
8741 "Show the current entry in another window, with default settings.
8743 Default settings are taken from `org-show-context-detail'. When
8744 use repeatedly in immediate succession, the remote entry will
8745 cycle through visibility
8747 children -> subtree -> folded
8749 When called with a numeric prefix arg, that arg will be passed through to
8750 `org-agenda-show-1'. For the interpretation of that argument, see the
8751 docstring of `org-agenda-show-1'."
8752 (interactive "P")
8753 (if (integerp n)
8754 (setq org-agenda-cycle-counter n)
8755 (if (not (eq last-command this-command))
8756 (setq org-agenda-cycle-counter 1)
8757 (if (equal org-agenda-cycle-counter 0)
8758 (setq org-agenda-cycle-counter 2)
8759 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8760 (if (> org-agenda-cycle-counter 3)
8761 (setq org-agenda-cycle-counter 0)))))
8762 (org-agenda-show-1 org-agenda-cycle-counter))
8764 (defun org-agenda-recenter (arg)
8765 "Display the Org file which contains the item at point and recenter."
8766 (interactive "P")
8767 (let ((win (selected-window)))
8768 (org-agenda-goto t)
8769 (recenter arg)
8770 (select-window win)))
8772 (defun org-agenda-show-mouse (ev)
8773 "Display the Org file which contains the item at the mouse click."
8774 (interactive "e")
8775 (mouse-set-point ev)
8776 (org-agenda-show))
8778 (defun org-agenda-check-no-diary ()
8779 "Check if the entry is a diary link and abort if yes."
8780 (if (org-get-at-bol 'org-agenda-diary-link)
8781 (org-agenda-error)))
8783 (defun org-agenda-error ()
8784 "Throw an error when a command is not allowed in the agenda."
8785 (user-error "Command not allowed in this line"))
8787 (defun org-agenda-tree-to-indirect-buffer (arg)
8788 "Show the subtree corresponding to the current entry in an indirect buffer.
8789 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8791 With a numerical prefix ARG, go up to this level and then take that tree.
8792 With a negative numeric ARG, go up by this number of levels.
8794 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8795 i.e. don't use
8796 the dedicated frame."
8797 (interactive "P")
8798 (if current-prefix-arg
8799 (org-agenda-do-tree-to-indirect-buffer arg)
8800 (let ((agenda-buffer (buffer-name))
8801 (agenda-window (selected-window))
8802 (indirect-window
8803 (and org-last-indirect-buffer
8804 (get-buffer-window org-last-indirect-buffer))))
8805 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8806 (unless (or (eq org-indirect-buffer-display 'new-frame)
8807 (eq org-indirect-buffer-display 'dedicated-frame))
8808 (unwind-protect
8809 (unless (and indirect-window (window-live-p indirect-window))
8810 (setq indirect-window (split-window agenda-window)))
8811 (and indirect-window (select-window indirect-window))
8812 (switch-to-buffer org-last-indirect-buffer :norecord)
8813 (fit-window-to-buffer indirect-window)))
8814 (select-window (get-buffer-window agenda-buffer))
8815 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8817 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8818 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8819 (org-agenda-check-no-diary)
8820 (let* ((marker (or (org-get-at-bol 'org-marker)
8821 (org-agenda-error)))
8822 (buffer (marker-buffer marker))
8823 (pos (marker-position marker)))
8824 (with-current-buffer buffer
8825 (save-excursion
8826 (goto-char pos)
8827 (funcall 'org-tree-to-indirect-buffer arg)))))
8829 (defvar org-last-heading-marker (make-marker)
8830 "Marker pointing to the headline that last changed its TODO state
8831 by a remote command from the agenda.")
8833 (defun org-agenda-todo-nextset ()
8834 "Switch TODO entry to next sequence."
8835 (interactive)
8836 (org-agenda-todo 'nextset))
8838 (defun org-agenda-todo-previousset ()
8839 "Switch TODO entry to previous sequence."
8840 (interactive)
8841 (org-agenda-todo 'previousset))
8843 (defun org-agenda-todo (&optional arg)
8844 "Cycle TODO state of line at point, also in Org file.
8845 This changes the line at point, all other lines in the agenda referring to
8846 the same tree node, and the headline of the tree node in the Org file."
8847 (interactive "P")
8848 (org-agenda-check-no-diary)
8849 (let* ((col (current-column))
8850 (marker (or (org-get-at-bol 'org-marker)
8851 (org-agenda-error)))
8852 (buffer (marker-buffer marker))
8853 (pos (marker-position marker))
8854 (hdmarker (org-get-at-bol 'org-hd-marker))
8855 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8856 (inhibit-read-only t)
8857 org-agenda-headline-snapshot-before-repeat newhead just-one)
8858 (org-with-remote-undo buffer
8859 (with-current-buffer buffer
8860 (widen)
8861 (goto-char pos)
8862 (org-show-context 'agenda)
8863 (let ((current-prefix-arg arg))
8864 (call-interactively 'org-todo))
8865 (and (bolp) (forward-char 1))
8866 (setq newhead (org-get-heading))
8867 (when (and (bound-and-true-p
8868 org-agenda-headline-snapshot-before-repeat)
8869 (not (equal org-agenda-headline-snapshot-before-repeat
8870 newhead))
8871 todayp)
8872 (setq newhead org-agenda-headline-snapshot-before-repeat
8873 just-one t))
8874 (save-excursion
8875 (org-back-to-heading)
8876 (move-marker org-last-heading-marker (point))))
8877 (beginning-of-line 1)
8878 (save-window-excursion
8879 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8880 (when (bound-and-true-p org-clock-out-when-done)
8881 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8882 newhead)
8883 (org-agenda-unmark-clocking-task))
8884 (org-move-to-column col)
8885 (org-agenda-mark-clocking-task))))
8887 (defun org-agenda-add-note (&optional arg)
8888 "Add a time-stamped note to the entry at point."
8889 (interactive "P")
8890 (org-agenda-check-no-diary)
8891 (let* ((marker (or (org-get-at-bol 'org-marker)
8892 (org-agenda-error)))
8893 (buffer (marker-buffer marker))
8894 (pos (marker-position marker))
8895 (hdmarker (org-get-at-bol 'org-hd-marker))
8896 (inhibit-read-only t))
8897 (with-current-buffer buffer
8898 (widen)
8899 (goto-char pos)
8900 (org-show-context 'agenda)
8901 (org-add-note))))
8903 (defun org-agenda-change-all-lines (newhead hdmarker
8904 &optional fixface just-this)
8905 "Change all lines in the agenda buffer which match HDMARKER.
8906 The new content of the line will be NEWHEAD (as modified by
8907 `org-agenda-format-item'). HDMARKER is checked with
8908 `equal' against all `org-hd-marker' text properties in the file.
8909 If FIXFACE is non-nil, the face of each item is modified according to
8910 the new TODO state.
8911 If JUST-THIS is non-nil, change just the current line, not all.
8912 If FORCE-TAGS is non nil, the car of it returns the new tags."
8913 (let* ((inhibit-read-only t)
8914 (line (org-current-line))
8915 (org-agenda-buffer (current-buffer))
8916 (thetags (with-current-buffer (marker-buffer hdmarker)
8917 (org-with-wide-buffer
8918 (goto-char hdmarker)
8919 (org-get-tags-at))))
8920 props m pl undone-face done-face finish new dotime level cat tags)
8921 (save-excursion
8922 (goto-char (point-max))
8923 (beginning-of-line 1)
8924 (while (not finish)
8925 (setq finish (bobp))
8926 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8927 (or (not just-this) (= (org-current-line) line))
8928 (equal m hdmarker))
8929 (setq props (text-properties-at (point))
8930 dotime (org-get-at-bol 'dotime)
8931 cat (org-get-at-eol 'org-category 1)
8932 level (org-get-at-bol 'level)
8933 tags thetags
8935 (let ((org-prefix-format-compiled
8936 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8937 org-prefix-format-compiled))
8938 (extra (org-get-at-bol 'extra)))
8939 (with-current-buffer (marker-buffer hdmarker)
8940 (org-with-wide-buffer
8941 (org-agenda-format-item extra newhead level cat tags dotime))))
8942 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8943 undone-face (org-get-at-bol 'undone-face)
8944 done-face (org-get-at-bol 'done-face))
8945 (beginning-of-line 1)
8946 (cond
8947 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8948 ((looking-at ".*")
8949 ;; When replacing the whole line, preserve bulk mark
8950 ;; overlay, if any.
8951 (let ((mark (catch :overlay
8952 (dolist (o (overlays-in (point) (+ 2 (point))))
8953 (when (eq (overlay-get o 'type)
8954 'org-marked-entry-overlay)
8955 (throw :overlay o))))))
8956 (replace-match new t t)
8957 (beginning-of-line)
8958 (when mark (move-overlay mark (point) (+ 2 (point)))))
8959 (add-text-properties (point-at-bol) (point-at-eol) props)
8960 (when fixface
8961 (add-text-properties
8962 (point-at-bol) (point-at-eol)
8963 (list 'face
8964 (if org-last-todo-state-is-todo
8965 undone-face done-face))))
8966 (org-agenda-highlight-todo 'line)
8967 (beginning-of-line 1))
8968 (t (error "Line update did not work")))
8969 (save-restriction
8970 (narrow-to-region (point-at-bol) (point-at-eol))
8971 (org-agenda-finalize)))
8972 (beginning-of-line 0)))))
8974 (defun org-agenda-align-tags (&optional line)
8975 "Align all tags in agenda items to `org-agenda-tags-column'."
8976 (let ((inhibit-read-only t)
8977 (org-agenda-tags-column (if (eq 'auto org-agenda-tags-column)
8978 (- (window-text-width))
8979 org-agenda-tags-column))
8980 l c)
8981 (save-excursion
8982 (goto-char (if line (point-at-bol) (point-min)))
8983 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8984 (if line (point-at-eol) nil) t)
8985 (add-text-properties
8986 (match-beginning 2) (match-end 2)
8987 (list 'face (delq nil (let ((prop (get-text-property
8988 (match-beginning 2) 'face)))
8989 (or (listp prop) (setq prop (list prop)))
8990 (if (memq 'org-tag prop)
8991 prop
8992 (cons 'org-tag prop))))))
8993 (setq l (- (match-end 2) (match-beginning 2))
8994 c (if (< org-agenda-tags-column 0)
8995 (- (abs org-agenda-tags-column) l)
8996 org-agenda-tags-column))
8997 (delete-region (match-beginning 1) (match-end 1))
8998 (goto-char (match-beginning 1))
8999 (insert (org-add-props
9000 (make-string (max 1 (- c (current-column))) ?\ )
9001 (plist-put (copy-sequence (text-properties-at (point)))
9002 'face nil))))
9003 (goto-char (point-min))
9004 (org-font-lock-add-tag-faces (point-max)))))
9006 (defun org-agenda-priority-up ()
9007 "Increase the priority of line at point, also in Org file."
9008 (interactive)
9009 (org-agenda-priority 'up))
9011 (defun org-agenda-priority-down ()
9012 "Decrease the priority of line at point, also in Org file."
9013 (interactive)
9014 (org-agenda-priority 'down))
9016 (defun org-agenda-priority (&optional force-direction)
9017 "Set the priority of line at point, also in Org file.
9018 This changes the line at point, all other lines in the agenda referring to
9019 the same tree node, and the headline of the tree node in the Org file.
9020 Called with a universal prefix arg, show the priority instead of setting it."
9021 (interactive "P")
9022 (if (equal force-direction '(4))
9023 (org-show-priority)
9024 (unless org-enable-priority-commands
9025 (error "Priority commands are disabled"))
9026 (org-agenda-check-no-diary)
9027 (let* ((col (current-column))
9028 (marker (or (org-get-at-bol 'org-marker)
9029 (org-agenda-error)))
9030 (hdmarker (org-get-at-bol 'org-hd-marker))
9031 (buffer (marker-buffer hdmarker))
9032 (pos (marker-position hdmarker))
9033 (inhibit-read-only t)
9034 newhead)
9035 (org-with-remote-undo buffer
9036 (with-current-buffer buffer
9037 (widen)
9038 (goto-char pos)
9039 (org-show-context 'agenda)
9040 (funcall 'org-priority force-direction)
9041 (end-of-line 1)
9042 (setq newhead (org-get-heading)))
9043 (org-agenda-change-all-lines newhead hdmarker)
9044 (org-move-to-column col)))))
9046 ;; FIXME: should fix the tags property of the agenda line.
9047 (defun org-agenda-set-tags (&optional tag onoff)
9048 "Set tags for the current headline."
9049 (interactive)
9050 (org-agenda-check-no-diary)
9051 (if (and (org-region-active-p) (called-interactively-p 'any))
9052 (call-interactively 'org-change-tag-in-region)
9053 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9054 (org-agenda-error)))
9055 (buffer (marker-buffer hdmarker))
9056 (pos (marker-position hdmarker))
9057 (inhibit-read-only t)
9058 newhead)
9059 (org-with-remote-undo buffer
9060 (with-current-buffer buffer
9061 (widen)
9062 (goto-char pos)
9063 (org-show-context 'agenda)
9064 (if tag
9065 (org-toggle-tag tag onoff)
9066 (call-interactively 'org-set-tags))
9067 (end-of-line 1)
9068 (setq newhead (org-get-heading)))
9069 (org-agenda-change-all-lines newhead hdmarker)
9070 (beginning-of-line 1)))))
9072 (defun org-agenda-set-property ()
9073 "Set a property for the current headline."
9074 (interactive)
9075 (org-agenda-check-no-diary)
9076 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9077 (org-agenda-error)))
9078 (buffer (marker-buffer hdmarker))
9079 (pos (marker-position hdmarker))
9080 (inhibit-read-only t)
9081 newhead)
9082 (org-with-remote-undo buffer
9083 (with-current-buffer buffer
9084 (widen)
9085 (goto-char pos)
9086 (org-show-context 'agenda)
9087 (call-interactively 'org-set-property)))))
9089 (defun org-agenda-set-effort ()
9090 "Set the effort property for the current headline."
9091 (interactive)
9092 (org-agenda-check-no-diary)
9093 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9094 (org-agenda-error)))
9095 (buffer (marker-buffer hdmarker))
9096 (pos (marker-position hdmarker))
9097 (inhibit-read-only t)
9098 newhead)
9099 (org-with-remote-undo buffer
9100 (with-current-buffer buffer
9101 (widen)
9102 (goto-char pos)
9103 (org-show-context 'agenda)
9104 (call-interactively 'org-set-effort)
9105 (end-of-line 1)
9106 (setq newhead (org-get-heading)))
9107 (org-agenda-change-all-lines newhead hdmarker))))
9109 (defun org-agenda-toggle-archive-tag ()
9110 "Toggle the archive tag for the current entry."
9111 (interactive)
9112 (org-agenda-check-no-diary)
9113 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9114 (org-agenda-error)))
9115 (buffer (marker-buffer hdmarker))
9116 (pos (marker-position hdmarker))
9117 (inhibit-read-only t)
9118 newhead)
9119 (org-with-remote-undo buffer
9120 (with-current-buffer buffer
9121 (widen)
9122 (goto-char pos)
9123 (org-show-context 'agenda)
9124 (call-interactively 'org-toggle-archive-tag)
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-do-date-later (arg)
9131 (interactive "P")
9132 (cond
9133 ((or (equal arg '(16))
9134 (memq last-command
9135 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9136 (setq this-command 'org-agenda-date-later-minutes)
9137 (org-agenda-date-later-minutes 1))
9138 ((or (equal arg '(4))
9139 (memq last-command
9140 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9141 (setq this-command 'org-agenda-date-later-hours)
9142 (org-agenda-date-later-hours 1))
9144 (org-agenda-date-later (prefix-numeric-value arg)))))
9146 (defun org-agenda-do-date-earlier (arg)
9147 (interactive "P")
9148 (cond
9149 ((or (equal arg '(16))
9150 (memq last-command
9151 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9152 (setq this-command 'org-agenda-date-earlier-minutes)
9153 (org-agenda-date-earlier-minutes 1))
9154 ((or (equal arg '(4))
9155 (memq last-command
9156 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9157 (setq this-command 'org-agenda-date-earlier-hours)
9158 (org-agenda-date-earlier-hours 1))
9160 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9162 (defun org-agenda-date-later (arg &optional what)
9163 "Change the date of this item to ARG day(s) later."
9164 (interactive "p")
9165 (org-agenda-check-type t 'agenda)
9166 (org-agenda-check-no-diary)
9167 (let* ((marker (or (org-get-at-bol 'org-marker)
9168 (org-agenda-error)))
9169 (buffer (marker-buffer marker))
9170 (pos (marker-position marker))
9171 cdate today)
9172 (org-with-remote-undo buffer
9173 (with-current-buffer buffer
9174 (widen)
9175 (goto-char pos)
9176 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9177 (when (and org-agenda-move-date-from-past-immediately-to-today
9178 (equal arg 1)
9179 (or (not what) (eq what 'day))
9180 (not (save-match-data (org-at-date-range-p))))
9181 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9182 cdate (calendar-absolute-from-gregorian
9183 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9184 today (org-today))
9185 (if (> today cdate)
9186 ;; immediately shift to today
9187 (setq arg (- today cdate))))
9188 (org-timestamp-change arg (or what 'day))
9189 (when (and (org-at-date-range-p)
9190 (re-search-backward org-tr-regexp-both (point-at-bol)))
9191 (let ((end org-last-changed-timestamp))
9192 (org-timestamp-change arg (or what 'day))
9193 (setq org-last-changed-timestamp
9194 (concat org-last-changed-timestamp "--" end)))))
9195 (org-agenda-show-new-time marker org-last-changed-timestamp))
9196 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9198 (defun org-agenda-date-earlier (arg &optional what)
9199 "Change the date of this item to ARG day(s) earlier."
9200 (interactive "p")
9201 (org-agenda-date-later (- arg) what))
9203 (defun org-agenda-date-later-minutes (arg)
9204 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9205 (interactive "p")
9206 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9207 (org-agenda-date-later arg 'minute))
9209 (defun org-agenda-date-earlier-minutes (arg)
9210 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9211 (interactive "p")
9212 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9213 (org-agenda-date-earlier arg 'minute))
9215 (defun org-agenda-date-later-hours (arg)
9216 "Change the time of this item, in hour steps."
9217 (interactive "p")
9218 (org-agenda-date-later arg 'hour))
9220 (defun org-agenda-date-earlier-hours (arg)
9221 "Change the time of this item, in hour steps."
9222 (interactive "p")
9223 (org-agenda-date-earlier arg 'hour))
9225 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9226 "Show new date stamp via text properties."
9227 ;; We use text properties to make this undoable
9228 (let ((inhibit-read-only t))
9229 (setq stamp (concat prefix " => " stamp " "))
9230 (save-excursion
9231 (goto-char (point-max))
9232 (while (not (bobp))
9233 (when (equal marker (org-get-at-bol 'org-marker))
9234 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9235 (org-move-to-column (- (window-width) (length stamp)) t)
9236 (add-text-properties
9237 (1- (point)) (point-at-eol)
9238 (list 'display (org-add-props stamp nil
9239 'face '(secondary-selection default))))
9240 (beginning-of-line 1))
9241 (beginning-of-line 0)))))
9243 (defun org-agenda-date-prompt (arg)
9244 "Change the date of this item. Date is prompted for, with default today.
9245 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9246 be used to request time specification in the time stamp."
9247 (interactive "P")
9248 (org-agenda-check-type t 'agenda)
9249 (org-agenda-check-no-diary)
9250 (let* ((marker (or (org-get-at-bol 'org-marker)
9251 (org-agenda-error)))
9252 (buffer (marker-buffer marker))
9253 (pos (marker-position marker)))
9254 (org-with-remote-undo buffer
9255 (with-current-buffer buffer
9256 (widen)
9257 (goto-char pos)
9258 (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
9259 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9260 (org-agenda-show-new-time marker org-last-changed-timestamp))
9261 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9263 (defun org-agenda-schedule (arg &optional time)
9264 "Schedule the item at point.
9265 ARG is passed through to `org-schedule'."
9266 (interactive "P")
9267 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9268 (org-agenda-check-no-diary)
9269 (let* ((marker (or (org-get-at-bol 'org-marker)
9270 (org-agenda-error)))
9271 (type (marker-insertion-type marker))
9272 (buffer (marker-buffer marker))
9273 (pos (marker-position marker))
9275 (set-marker-insertion-type marker t)
9276 (org-with-remote-undo buffer
9277 (with-current-buffer buffer
9278 (widen)
9279 (goto-char pos)
9280 (setq ts (org-schedule arg time)))
9281 (org-agenda-show-new-time marker ts " S"))
9282 (message "%s" ts)))
9284 (defun org-agenda-deadline (arg &optional time)
9285 "Schedule the item at point.
9286 ARG is passed through to `org-deadline'."
9287 (interactive "P")
9288 (org-agenda-check-type t 'agenda 'todo 'tags 'search)
9289 (org-agenda-check-no-diary)
9290 (let* ((marker (or (org-get-at-bol 'org-marker)
9291 (org-agenda-error)))
9292 (buffer (marker-buffer marker))
9293 (pos (marker-position marker))
9295 (org-with-remote-undo buffer
9296 (with-current-buffer buffer
9297 (widen)
9298 (goto-char pos)
9299 (setq ts (org-deadline arg time)))
9300 (org-agenda-show-new-time marker ts " D"))
9301 (message "%s" ts)))
9303 (defun org-agenda-clock-in (&optional arg)
9304 "Start the clock on the currently selected item."
9305 (interactive "P")
9306 (org-agenda-check-no-diary)
9307 (if (equal arg '(4))
9308 (org-clock-in arg)
9309 (let* ((marker (or (org-get-at-bol 'org-marker)
9310 (org-agenda-error)))
9311 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9312 (pos (marker-position marker))
9313 (col (current-column))
9314 newhead)
9315 (org-with-remote-undo (marker-buffer marker)
9316 (with-current-buffer (marker-buffer marker)
9317 (widen)
9318 (goto-char pos)
9319 (org-show-context 'agenda)
9320 (org-cycle-hide-drawers 'children)
9321 (org-clock-in arg)
9322 (setq newhead (org-get-heading)))
9323 (org-agenda-change-all-lines newhead hdmarker))
9324 (org-move-to-column col))))
9326 (defun org-agenda-clock-out ()
9327 "Stop the currently running clock."
9328 (interactive)
9329 (unless (marker-buffer org-clock-marker)
9330 (error "No running clock"))
9331 (let ((marker (make-marker)) (col (current-column)) newhead)
9332 (org-with-remote-undo (marker-buffer org-clock-marker)
9333 (with-current-buffer (marker-buffer org-clock-marker)
9334 (org-with-wide-buffer
9335 (goto-char org-clock-marker)
9336 (org-back-to-heading t)
9337 (move-marker marker (point))
9338 (org-clock-out)
9339 (setq newhead (org-get-heading)))))
9340 (org-agenda-change-all-lines newhead marker)
9341 (move-marker marker nil)
9342 (org-move-to-column col)
9343 (org-agenda-unmark-clocking-task)))
9345 (defun org-agenda-clock-cancel (&optional arg)
9346 "Cancel the currently running clock."
9347 (interactive "P")
9348 (unless (marker-buffer org-clock-marker)
9349 (user-error "No running clock"))
9350 (org-with-remote-undo (marker-buffer org-clock-marker)
9351 (org-clock-cancel)))
9353 (defun org-agenda-clock-goto ()
9354 "Jump to the currently clocked in task within the agenda.
9355 If the currently clocked in task is not listed in the agenda
9356 buffer, display it in another window."
9357 (interactive)
9358 (let (pos)
9359 (mapc (lambda (o)
9360 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9361 (setq pos (overlay-start o))))
9362 (overlays-in (point-min) (point-max)))
9363 (cond (pos (goto-char pos))
9364 ;; If the currently clocked entry is not in the agenda
9365 ;; buffer, we visit it in another window:
9366 ((bound-and-true-p org-clock-current-task)
9367 (org-switch-to-buffer-other-window (org-clock-goto)))
9368 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9370 (defun org-agenda-diary-entry-in-org-file ()
9371 "Make a diary entry in the file `org-agenda-diary-file'."
9372 (let (d1 d2 char (text "") dp1 dp2)
9373 (if (equal (buffer-name) "*Calendar*")
9374 (setq d1 (calendar-cursor-to-date t)
9375 d2 (car calendar-mark-ring))
9376 (setq dp1 (get-text-property (point-at-bol) 'day))
9377 (unless dp1 (user-error "No date defined in current line"))
9378 (setq d1 (calendar-gregorian-from-absolute dp1)
9379 d2 (and (ignore-errors (mark))
9380 (save-excursion
9381 (goto-char (mark))
9382 (setq dp2 (get-text-property (point-at-bol) 'day)))
9383 (calendar-gregorian-from-absolute dp2))))
9384 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9385 (setq char (read-char-exclusive))
9386 (cond
9387 ((equal char ?d)
9388 (setq text (read-string "Day entry: "))
9389 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9390 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9391 ((equal char ?a)
9392 (setq d1 (list (car d1) (nth 1 d1)
9393 (read-number (format "Reference year [%d]: " (nth 2 d1))
9394 (nth 2 d1))))
9395 (setq text (read-string "Anniversary (use %d to show years): "))
9396 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9397 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9398 ((equal char ?b)
9399 (setq text (read-string "Block entry: "))
9400 (unless (and d1 d2 (not (equal d1 d2)))
9401 (user-error "No block of days selected"))
9402 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9403 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9404 ((equal char ?j)
9405 (org-switch-to-buffer-other-window
9406 (find-file-noselect org-agenda-diary-file))
9407 (require 'org-datetree)
9408 (org-datetree-find-date-create d1)
9409 (org-reveal t))
9410 (t (user-error "Invalid selection character `%c'" char)))))
9412 (defcustom org-agenda-insert-diary-strategy 'date-tree
9413 "Where in `org-agenda-diary-file' should new entries be added?
9414 Valid values:
9416 date-tree in the date tree, as first child of the date
9417 date-tree-last in the date tree, as last child of the date
9418 top-level as top-level entries at the end of the file."
9419 :group 'org-agenda
9420 :type '(choice
9421 (const :tag "first in a date tree" date-tree)
9422 (const :tag "last in a date tree" date-tree-last)
9423 (const :tag "as top level at end of file" top-level)))
9425 (defcustom org-agenda-insert-diary-extract-time nil
9426 "Non-nil means extract any time specification from the diary entry."
9427 :group 'org-agenda
9428 :version "24.1"
9429 :type 'boolean)
9431 (defcustom org-agenda-bulk-mark-char ">"
9432 "A single-character string to be used as the bulk mark."
9433 :group 'org-agenda
9434 :version "24.1"
9435 :type 'string)
9437 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9438 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9439 If TEXT is not empty, it will become the headline of the new entry, and
9440 the resulting entry will not be shown. When TEXT is empty, switch to
9441 `org-agenda-diary-file' and let the user finish the entry there."
9442 (let ((cw (current-window-configuration)))
9443 (org-switch-to-buffer-other-window
9444 (find-file-noselect org-agenda-diary-file))
9445 (widen)
9446 (goto-char (point-min))
9447 (cond
9448 ((eq type 'anniversary)
9449 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9450 (progn
9451 (or (org-at-heading-p t)
9452 (progn
9453 (outline-next-heading)
9454 (insert "* Anniversaries\n\n")
9455 (beginning-of-line -1)))))
9456 (outline-next-heading)
9457 (org-back-over-empty-lines)
9458 (backward-char 1)
9459 (insert "\n")
9460 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9461 (nth 2 d1) (car d1) (nth 1 d1) text)))
9462 ((eq type 'day)
9463 (let ((org-prefix-has-time t)
9464 (org-agenda-time-leading-zero t)
9465 fmt time time2)
9466 (if org-agenda-insert-diary-extract-time
9467 ;; Use org-agenda-format-item to parse text for a time-range and
9468 ;; remove it. FIXME: This is a hack, we should refactor
9469 ;; that function to make time extraction available separately
9470 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9471 time (get-text-property 0 'time fmt)
9472 time2 (if (> (length time) 0)
9473 ;; split-string removes trailing ...... if
9474 ;; no end time given. First space
9475 ;; separates time from date.
9476 (concat " " (car (split-string time "\\.")))
9477 nil)
9478 text (get-text-property 0 'txt fmt)))
9479 (if (eq org-agenda-insert-diary-strategy 'top-level)
9480 (org-agenda-insert-diary-as-top-level text)
9481 (require 'org-datetree)
9482 (org-datetree-find-date-create d1)
9483 (org-agenda-insert-diary-make-new-entry text))
9484 (org-insert-time-stamp (org-time-from-absolute
9485 (calendar-absolute-from-gregorian d1))
9486 nil nil nil nil time2))
9487 (end-of-line 0))
9488 ((eq type 'block)
9489 (if (> (calendar-absolute-from-gregorian d1)
9490 (calendar-absolute-from-gregorian d2))
9491 (setq d1 (prog1 d2 (setq d2 d1))))
9492 (if (eq org-agenda-insert-diary-strategy 'top-level)
9493 (org-agenda-insert-diary-as-top-level text)
9494 (require 'org-datetree)
9495 (org-datetree-find-date-create d1)
9496 (org-agenda-insert-diary-make-new-entry text))
9497 (org-insert-time-stamp (org-time-from-absolute
9498 (calendar-absolute-from-gregorian d1)))
9499 (insert "--")
9500 (org-insert-time-stamp (org-time-from-absolute
9501 (calendar-absolute-from-gregorian d2)))
9502 (end-of-line 0)))
9503 (if (string-match "\\S-" text)
9504 (progn
9505 (set-window-configuration cw)
9506 (message "%s entry added to %s"
9507 (capitalize (symbol-name type))
9508 (abbreviate-file-name org-agenda-diary-file)))
9509 (org-reveal t)
9510 (message "Please finish entry here"))))
9512 (defun org-agenda-insert-diary-as-top-level (text)
9513 "Make new entry as a top-level entry at the end of the file.
9514 Add TEXT as headline, and position the cursor in the second line so that
9515 a timestamp can be added there."
9516 (widen)
9517 (goto-char (point-max))
9518 (unless (bolp) (insert "\n"))
9519 (org-insert-heading nil t t)
9520 (insert text)
9521 (org-end-of-meta-data)
9522 (unless (bolp) (insert "\n"))
9523 (when org-adapt-indentation (indent-to-column 2)))
9525 (defun org-agenda-insert-diary-make-new-entry (text)
9526 "Make a new entry with TEXT as a child of the current subtree.
9527 Position the point in the heading's first body line so that
9528 a timestamp can be added there."
9529 (cond
9530 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9531 (end-of-line)
9532 (org-insert-heading '(4) t)
9533 (org-do-demote))
9535 (outline-next-heading)
9536 (org-back-over-empty-lines)
9537 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9538 (org-insert-heading nil t)
9539 (org-do-demote)))
9540 (let ((col (current-column)))
9541 (insert text)
9542 (org-end-of-meta-data)
9543 ;; Ensure point is left on a blank line, at proper indentation.
9544 (unless (bolp) (insert "\n"))
9545 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9546 (when org-adapt-indentation (indent-to-column col)))
9547 (org-show-set-visibility 'lineage))
9549 (defun org-agenda-diary-entry ()
9550 "Make a diary entry, like the `i' command from the calendar.
9551 All the standard commands work: block, weekly etc.
9552 When `org-agenda-diary-file' points to a file,
9553 `org-agenda-diary-entry-in-org-file' is called instead to create
9554 entries in that Org file."
9555 (interactive)
9556 (if (not (eq org-agenda-diary-file 'diary-file))
9557 (org-agenda-diary-entry-in-org-file)
9558 (require 'diary-lib)
9559 (let* ((char (progn
9560 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9561 (read-char-exclusive)))
9562 (cmd (cdr (assoc char
9563 '((?d . diary-insert-entry)
9564 (?w . diary-insert-weekly-entry)
9565 (?m . diary-insert-monthly-entry)
9566 (?y . diary-insert-yearly-entry)
9567 (?a . diary-insert-anniversary-entry)
9568 (?b . diary-insert-block-entry)
9569 (?c . diary-insert-cyclic-entry)))))
9570 (oldf (symbol-function 'calendar-cursor-to-date))
9571 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9572 (point (point))
9573 (mark (or (mark t) (point))))
9574 (unless cmd
9575 (user-error "No command associated with <%c>" char))
9576 (unless (and (get-text-property point 'day)
9577 (or (not (equal ?b char))
9578 (get-text-property mark 'day)))
9579 (user-error "Don't know which date to use for diary entry"))
9580 ;; We implement this by hacking the `calendar-cursor-to-date' function
9581 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9582 (let ((calendar-mark-ring
9583 (list (calendar-gregorian-from-absolute
9584 (or (get-text-property mark 'day)
9585 (get-text-property point 'day))))))
9586 (unwind-protect
9587 (progn
9588 (fset 'calendar-cursor-to-date
9589 (lambda (&optional error dummy)
9590 (calendar-gregorian-from-absolute
9591 (get-text-property point 'day))))
9592 (call-interactively cmd))
9593 (fset 'calendar-cursor-to-date oldf))))))
9595 (defun org-agenda-execute-calendar-command (cmd)
9596 "Execute a calendar command from the agenda with date from cursor."
9597 (org-agenda-check-type t 'agenda)
9598 (require 'diary-lib)
9599 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9600 (user-error "Don't know which date to use for the calendar command"))
9601 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9602 (point (point))
9603 (date (calendar-gregorian-from-absolute
9604 (get-text-property point 'day)))
9605 ;; the following 2 vars are needed in the calendar
9606 (displayed-month (car date))
9607 (displayed-year (nth 2 date)))
9608 (unwind-protect
9609 (progn
9610 (fset 'calendar-cursor-to-date
9611 (lambda (&optional error dummy)
9612 (calendar-gregorian-from-absolute
9613 (get-text-property point 'day))))
9614 (call-interactively cmd))
9615 (fset 'calendar-cursor-to-date oldf))))
9617 (defun org-agenda-phases-of-moon ()
9618 "Display the phases of the moon for the 3 months around the cursor date."
9619 (interactive)
9620 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9622 (defun org-agenda-holidays ()
9623 "Display the holidays for the 3 months around the cursor date."
9624 (interactive)
9625 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9627 (defvar calendar-longitude) ; defined in calendar.el
9628 (defvar calendar-latitude) ; defined in calendar.el
9629 (defvar calendar-location-name) ; defined in calendar.el
9631 (defun org-agenda-sunrise-sunset (arg)
9632 "Display sunrise and sunset for the cursor date.
9633 Latitude and longitude can be specified with the variables
9634 `calendar-latitude' and `calendar-longitude'. When called with prefix
9635 argument, latitude and longitude will be prompted for."
9636 (interactive "P")
9637 (require 'solar)
9638 (let ((calendar-longitude (if arg nil calendar-longitude))
9639 (calendar-latitude (if arg nil calendar-latitude))
9640 (calendar-location-name
9641 (if arg "the given coordinates" calendar-location-name)))
9642 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9644 (defun org-agenda-goto-calendar ()
9645 "Open the Emacs calendar with the date at the cursor."
9646 (interactive)
9647 (org-agenda-check-type t 'agenda)
9648 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9649 (user-error "Don't know which date to open in calendar")))
9650 (date (calendar-gregorian-from-absolute day))
9651 (calendar-move-hook nil)
9652 (calendar-view-holidays-initially-flag nil)
9653 (calendar-view-diary-initially-flag nil))
9654 (calendar)
9655 (calendar-goto-date date)))
9657 ;;;###autoload
9658 (defun org-calendar-goto-agenda ()
9659 "Compute the Org agenda for the calendar date displayed at the cursor.
9660 This is a command that has to be installed in `calendar-mode-map'."
9661 (interactive)
9662 ;; Temporarily disable sticky agenda since user clearly wants to
9663 ;; refresh view anyway.
9664 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9665 (org-agenda-sticky nil))
9666 (org-agenda-list nil (calendar-absolute-from-gregorian
9667 (calendar-cursor-to-date))
9668 nil)))
9670 (defun org-agenda-convert-date ()
9671 (interactive)
9672 (org-agenda-check-type t 'agenda)
9673 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9674 date s)
9675 (unless day
9676 (user-error "Don't know which date to convert"))
9677 (setq date (calendar-gregorian-from-absolute day))
9678 (setq s (concat
9679 "Gregorian: " (calendar-date-string date) "\n"
9680 "ISO: " (calendar-iso-date-string date) "\n"
9681 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9682 "Julian: " (calendar-julian-date-string date) "\n"
9683 "Astron. JD: " (calendar-astro-date-string date)
9684 " (Julian date number at noon UTC)\n"
9685 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9686 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9687 "French: " (calendar-french-date-string date) "\n"
9688 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9689 "Mayan: " (calendar-mayan-date-string date) "\n"
9690 "Coptic: " (calendar-coptic-date-string date) "\n"
9691 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9692 "Persian: " (calendar-persian-date-string date) "\n"
9693 "Chinese: " (calendar-chinese-date-string date) "\n"))
9694 (with-output-to-temp-buffer "*Dates*"
9695 (princ s))
9696 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9698 ;;; Bulk commands
9700 (defun org-agenda-bulk-marked-p ()
9701 "Non-nil when current entry is marked for bulk action."
9702 (eq (get-char-property (point-at-bol) 'type)
9703 'org-marked-entry-overlay))
9705 (defun org-agenda-bulk-mark (&optional arg)
9706 "Mark the entry at point for future bulk action."
9707 (interactive "p")
9708 (dotimes (i (or arg 1))
9709 (unless (org-get-at-bol 'org-agenda-diary-link)
9710 (let* ((m (org-get-at-bol 'org-hd-marker))
9712 (unless (org-agenda-bulk-marked-p)
9713 (unless m (user-error "Nothing to mark at point"))
9714 (push m org-agenda-bulk-marked-entries)
9715 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9716 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9717 (org-get-todo-face "TODO")
9718 'evaporate)
9719 (overlay-put ov 'type 'org-marked-entry-overlay))
9720 (end-of-line 1)
9721 (or (ignore-errors
9722 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9723 (beginning-of-line 2))
9724 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9725 (beginning-of-line 2))
9726 (message "%d entries marked for bulk action"
9727 (length org-agenda-bulk-marked-entries))))))
9729 (defun org-agenda-bulk-mark-all ()
9730 "Mark all entries for future agenda bulk action."
9731 (interactive)
9732 (org-agenda-bulk-mark-regexp "."))
9734 (defun org-agenda-bulk-mark-regexp (regexp)
9735 "Mark entries matching REGEXP for future agenda bulk action."
9736 (interactive "sMark entries matching regexp: ")
9737 (let ((entries-marked 0) txt-at-point)
9738 (save-excursion
9739 (goto-char (point-min))
9740 (goto-char (next-single-property-change (point) 'org-hd-marker))
9741 (while (and (re-search-forward regexp nil t)
9742 (setq txt-at-point (get-text-property (point) 'txt)))
9743 (if (get-char-property (point) 'invisible)
9744 (beginning-of-line 2)
9745 (when (string-match regexp txt-at-point)
9746 (setq entries-marked (1+ entries-marked))
9747 (call-interactively 'org-agenda-bulk-mark)))))
9749 (if (not entries-marked)
9750 (message "No entry matching this regexp."))))
9752 (defun org-agenda-bulk-unmark (&optional arg)
9753 "Unmark the entry at point for future bulk action."
9754 (interactive "P")
9755 (if arg
9756 (org-agenda-bulk-unmark-all)
9757 (cond ((org-agenda-bulk-marked-p)
9758 (org-agenda-bulk-remove-overlays
9759 (point-at-bol) (+ 2 (point-at-bol)))
9760 (setq org-agenda-bulk-marked-entries
9761 (delete (org-get-at-bol 'org-hd-marker)
9762 org-agenda-bulk-marked-entries))
9763 (end-of-line 1)
9764 (or (ignore-errors
9765 (goto-char (next-single-property-change (point) 'txt)))
9766 (beginning-of-line 2))
9767 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9768 (beginning-of-line 2))
9769 (message "%d entries left marked for bulk action"
9770 (length org-agenda-bulk-marked-entries)))
9771 (t (message "No entry to unmark here")))))
9773 (defun org-agenda-bulk-toggle-all ()
9774 "Toggle all marks for bulk action."
9775 (interactive)
9776 (save-excursion
9777 (goto-char (point-min))
9778 (while (ignore-errors
9779 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9780 (org-agenda-bulk-toggle))))
9782 (defun org-agenda-bulk-toggle ()
9783 "Toggle the mark at point for bulk action."
9784 (interactive)
9785 (if (org-agenda-bulk-marked-p)
9786 (org-agenda-bulk-unmark)
9787 (org-agenda-bulk-mark)))
9789 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9790 "Remove the mark overlays between BEG and END in the agenda buffer.
9791 BEG and END default to the buffer limits.
9793 This only removes the overlays, it does not remove the markers
9794 from the list in `org-agenda-bulk-marked-entries'."
9795 (interactive)
9796 (mapc (lambda (ov)
9797 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9798 (delete-overlay ov)))
9799 (overlays-in (or beg (point-min)) (or end (point-max)))))
9801 (defun org-agenda-bulk-unmark-all ()
9802 "Remove all marks in the agenda buffer.
9803 This will remove the markers and the overlays."
9804 (interactive)
9805 (if (null org-agenda-bulk-marked-entries)
9806 (message "No entry to unmark")
9807 (setq org-agenda-bulk-marked-entries nil)
9808 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9810 (defcustom org-agenda-persistent-marks nil
9811 "Non-nil means marked items will stay marked after a bulk action.
9812 You can toggle this interactively by typing `p' when prompted for a
9813 bulk action."
9814 :group 'org-agenda
9815 :version "24.1"
9816 :type 'boolean)
9818 (defun org-agenda-bulk-action (&optional arg)
9819 "Execute an remote-editing action on all marked entries.
9820 The prefix arg is passed through to the command if possible."
9821 (interactive "P")
9822 ;; Make sure we have markers, and only valid ones.
9823 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9824 (dolist (m org-agenda-bulk-marked-entries)
9825 (unless (and (markerp m)
9826 (marker-buffer m)
9827 (buffer-live-p (marker-buffer m))
9828 (marker-position m))
9829 (user-error "Marker %s for bulk command is invalid" m)))
9831 ;; Prompt for the bulk command.
9832 (message
9833 (concat (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")
9834 "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9835 "[S]catter [f]unction "
9836 (and org-agenda-bulk-custom-functions
9837 (format " Custom: [%s]"
9838 (mapconcat (lambda (f) (char-to-string (car f)))
9839 org-agenda-bulk-custom-functions
9840 "")))))
9841 (catch 'exit
9842 (let* ((org-log-refile (if org-log-refile 'time nil))
9843 (entries (reverse org-agenda-bulk-marked-entries))
9844 (org-overriding-default-time
9845 (and (get-text-property (point) 'org-agenda-date-header)
9846 (org-get-cursor-date)))
9847 redo-at-end
9848 cmd)
9849 (pcase (read-char-exclusive)
9851 (let ((org-agenda-persistent-marks
9852 (not org-agenda-persistent-marks)))
9853 (org-agenda-bulk-action)
9854 (throw 'exit nil)))
9857 (setq cmd #'org-agenda-archive))
9860 (setq cmd #'org-agenda-archive-to-archive-sibling))
9862 ((or ?r ?w)
9863 (let ((refile-location
9864 (org-refile-get-location
9865 "Refile to"
9866 (marker-buffer (car entries))
9867 org-refile-allow-creating-parent-nodes)))
9868 (when (nth 3 refile-location)
9869 (setcar (nthcdr 3 refile-location)
9870 (move-marker
9871 (make-marker)
9872 (nth 3 refile-location)
9873 (or (get-file-buffer (nth 1 refile-location))
9874 (find-buffer-visiting (nth 1 refile-location))
9875 (error "This should not happen")))))
9877 (setq cmd `(lambda () (org-agenda-refile nil ',refile-location t)))
9878 (setq redo-at-end t)))
9881 (let ((state (completing-read
9882 "Todo state: "
9883 (with-current-buffer (marker-buffer (car entries))
9884 (mapcar #'list org-todo-keywords-1)))))
9885 (setq cmd `(lambda ()
9886 (let ((org-inhibit-blocking t)
9887 (org-inhibit-logging 'note))
9888 (org-agenda-todo ,state))))))
9890 ((and (or ?- ?+) action)
9891 (let ((tag (completing-read
9892 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9893 (with-current-buffer (marker-buffer (car entries))
9894 (delq nil
9895 (mapcar (lambda (x) (and (stringp (car x)) x))
9896 org-current-tag-alist))))))
9897 (setq cmd
9898 `(lambda ()
9899 (org-agenda-set-tags ,tag
9900 ,(if (eq action ?+) ''on ''off))))))
9903 (let ((time
9904 (and (not arg)
9905 (org-read-date nil nil nil "(Re)Schedule to"
9906 org-overriding-default-time))))
9907 ;; Make sure to not prompt for a note when bulk
9908 ;; rescheduling as Org cannot cope with simultaneous notes.
9909 ;; Besides, it could be annoying depending on the number of
9910 ;; items re-scheduled.
9911 (setq cmd
9912 `(lambda ()
9913 (let ((org-log-reschedule (and org-log-reschedule 'time)))
9914 (org-agenda-schedule arg ,time))))))
9916 (let ((time
9917 (and (not arg)
9918 (org-read-date nil nil nil "(Re)Set Deadline to"
9919 org-overriding-default-time))))
9920 ;; Make sure to not prompt for a note when bulk
9921 ;; rescheduling as Org cannot cope with simultaneous
9922 ;; notes. Besides, it could be annoying depending on the
9923 ;; number of items re-scheduled.
9924 (setq cmd
9925 `(lambda ()
9926 (let ((org-log-redeadline (and org-log-redeadline 'time)))
9927 (org-agenda-deadline arg ,time))))))
9930 (unless (org-agenda-check-type nil 'agenda 'todo)
9931 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type))
9932 (let ((days (read-number
9933 (format "Scatter tasks across how many %sdays: "
9934 (if arg "week" ""))
9935 7)))
9936 (setq cmd
9937 `(lambda ()
9938 (let ((distance (1+ (random ,days))))
9939 (when arg
9940 (let ((dist distance)
9941 (day-of-week
9942 (calendar-day-of-week
9943 (calendar-gregorian-from-absolute (org-today)))))
9944 (dotimes (i (1+ dist))
9945 (while (member day-of-week org-agenda-weekend-days)
9946 (cl-incf distance)
9947 (cl-incf day-of-week)
9948 (when (= day-of-week 7)
9949 (setq day-of-week 0)))
9950 (cl-incf day-of-week)
9951 (when (= day-of-week 7)
9952 (setq day-of-week 0)))))
9953 ;; Silently fail when try to replan a sexp entry.
9954 (ignore-errors
9955 (let* ((date (calendar-gregorian-from-absolute
9956 (+ (org-today) distance)))
9957 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9958 (nth 2 date))))
9959 (org-agenda-schedule nil time))))))))
9962 (setq cmd
9963 (intern
9964 (completing-read "Function: " obarray #'fboundp t nil nil))))
9966 (action
9967 (pcase (assoc action org-agenda-bulk-custom-functions)
9968 (`(,_ ,f) (setq cmd f) (setq redo-at-end t))
9969 (_ (user-error "Invalid bulk action: %c" action)))))
9971 ;; Sort the markers, to make sure that parents are handled
9972 ;; before children.
9973 (setq entries (sort entries
9974 (lambda (a b)
9975 (cond
9976 ((eq (marker-buffer a) (marker-buffer b))
9977 (< (marker-position a) (marker-position b)))
9979 (string< (buffer-name (marker-buffer a))
9980 (buffer-name (marker-buffer b))))))))
9982 ;; Now loop over all markers and apply CMD.
9983 (let ((processed 0)
9984 (skipped 0))
9985 (dolist (e entries)
9986 (let ((pos (text-property-any (point-min) (point-max) 'org-hd-marker e)))
9987 (if (not pos)
9988 (progn (message "Skipping removed entry at %s" e)
9989 (cl-incf skipped))
9990 (goto-char pos)
9991 (let (org-loop-over-headlines-in-active-region) (funcall cmd))
9992 ;; `post-command-hook' is not run yet. We make sure any
9993 ;; pending log note is processed.
9994 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9995 (memq 'org-add-log-note post-command-hook))
9996 (org-add-log-note))
9997 (cl-incf processed))))
9998 (when redo-at-end (org-agenda-redo))
9999 (unless org-agenda-persistent-marks (org-agenda-bulk-unmark-all))
10000 (message "Acted on %d entries%s%s"
10001 processed
10002 (if (= skipped 0)
10004 (format ", skipped %d (disappeared before their turn)"
10005 skipped))
10006 (if (not org-agenda-persistent-marks) "" " (kept marked)"))))))
10008 (defun org-agenda-capture (&optional with-time)
10009 "Call `org-capture' with the date at point.
10010 With a `C-1' prefix, use the HH:MM value at point (if any) or the
10011 current HH:MM time."
10012 (interactive "P")
10013 (if (not (eq major-mode 'org-agenda-mode))
10014 (user-error "You cannot do this outside of agenda buffers")
10015 (let ((org-overriding-default-time
10016 (org-get-cursor-date (equal with-time 1))))
10017 (call-interactively 'org-capture))))
10019 ;;; Dragging agenda lines forward/backward
10021 (defun org-agenda-reapply-filters ()
10022 "Re-apply all agenda filters."
10023 (mapcar
10024 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10025 `((,org-agenda-tag-filter tag)
10026 (,org-agenda-category-filter category)
10027 (,org-agenda-regexp-filter regexp)
10028 (,org-agenda-effort-filter effort)
10029 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10030 (,(get 'org-agenda-category-filter :preset-filter) category)
10031 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10032 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10034 (defun org-agenda-drag-line-forward (arg &optional backward)
10035 "Drag an agenda line forward by ARG lines.
10036 When the optional argument `backward' is non-nil, move backward."
10037 (interactive "p")
10038 (let ((inhibit-read-only t) lst line)
10039 (if (or (not (get-text-property (point) 'txt))
10040 (save-excursion
10041 (dotimes (n arg)
10042 (move-beginning-of-line (if backward 0 2))
10043 (push (not (get-text-property (point) 'txt)) lst))
10044 (delq nil lst)))
10045 (message "Cannot move line forward")
10046 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10047 (move-beginning-of-line 1)
10048 (setq line (buffer-substring (point) end))
10049 (delete-region (point) end)
10050 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10051 (insert line)
10052 (org-agenda-reapply-filters)
10053 (org-agenda-mark-clocking-task)
10054 (move-beginning-of-line 0)))))
10056 (defun org-agenda-drag-line-backward (arg)
10057 "Drag an agenda line backward by ARG lines."
10058 (interactive "p")
10059 (org-agenda-drag-line-forward arg t))
10061 ;;; Flagging notes
10063 (defun org-agenda-show-the-flagging-note ()
10064 "Display the flagging note in the other window.
10065 When called a second time in direct sequence, offer to remove the FLAGGING
10066 tag and (if present) the flagging note."
10067 (interactive)
10068 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10069 (win (selected-window))
10070 note heading newhead)
10071 (unless hdmarker
10072 (user-error "No linked entry at point"))
10073 (if (and (eq this-command last-command)
10074 (y-or-n-p "Unflag and remove any flagging note? "))
10075 (progn
10076 (org-agenda-remove-flag hdmarker)
10077 (let ((win (get-buffer-window "*Flagging Note*")))
10078 (and win (delete-window win)))
10079 (message "Entry unflagged"))
10080 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10081 (unless note
10082 (user-error "No flagging note"))
10083 (org-kill-new note)
10084 (org-switch-to-buffer-other-window "*Flagging Note*")
10085 (erase-buffer)
10086 (insert note)
10087 (goto-char (point-min))
10088 (while (re-search-forward "\\\\n" nil t)
10089 (replace-match "\n" t t))
10090 (goto-char (point-min))
10091 (select-window win)
10092 (message "%s" (substitute-command-keys "Flagging note pushed to \
10093 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10094 tag and note")))))
10096 (defun org-agenda-remove-flag (marker)
10097 "Remove the FLAGGED tag and any flagging note in the entry."
10098 (let (newhead)
10099 (org-with-point-at marker
10100 (org-toggle-tag "FLAGGED" 'off)
10101 (org-entry-delete nil "THEFLAGGINGNOTE")
10102 (setq newhead (org-get-heading)))
10103 (org-agenda-change-all-lines newhead marker)
10104 (message "Entry unflagged")))
10106 (defun org-agenda-get-any-marker (&optional pos)
10107 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10108 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10110 ;;; Appointment reminders
10112 (defvar appt-time-msg-list) ; defined in appt.el
10114 ;;;###autoload
10115 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10116 "Activate appointments found in `org-agenda-files'.
10118 With a `\\[universal-argument]' prefix, refresh the list of \
10119 appointments.
10121 If FILTER is t, interactively prompt the user for a regular
10122 expression, and filter out entries that don't match it.
10124 If FILTER is a string, use this string as a regular expression
10125 for filtering entries out.
10127 If FILTER is a function, filter out entries against which
10128 calling the function returns nil. This function takes one
10129 argument: an entry from `org-agenda-get-day-entries'.
10131 FILTER can also be an alist with the car of each cell being
10132 either `headline' or `category'. For example:
10134 \\='((headline \"IMPORTANT\")
10135 (category \"Work\"))
10137 will only add headlines containing IMPORTANT or headlines
10138 belonging to the \"Work\" category.
10140 ARGS are symbols indicating what kind of entries to consider.
10141 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10142 \(i.e., deadlines and scheduled items with a hh:mm specification)
10143 and :timestamp entries. See the docstring of `org-diary' for
10144 details and examples.
10146 If an entry has a APPT_WARNTIME property, its value will be used
10147 to override `appt-message-warning-time'."
10148 (interactive "P")
10149 (if refresh (setq appt-time-msg-list nil))
10150 (if (eq filter t)
10151 (setq filter (read-from-minibuffer "Regexp filter: ")))
10152 (let* ((cnt 0) ; count added events
10153 (scope (or args '(:deadline* :scheduled* :timestamp)))
10154 (org-agenda-new-buffers nil)
10155 (org-deadline-warning-days 0)
10156 ;; Do not use `org-today' here because appt only takes
10157 ;; time and without date as argument, so it may pass wrong
10158 ;; information otherwise
10159 (today (org-date-to-gregorian
10160 (time-to-days (current-time))))
10161 (org-agenda-restrict nil)
10162 (files (org-agenda-files 'unrestricted)) entries file
10163 (org-agenda-buffer nil))
10164 ;; Get all entries which may contain an appt
10165 (org-agenda-prepare-buffers files)
10166 (while (setq file (pop files))
10167 (setq entries
10168 (delq nil
10169 (append entries
10170 (apply 'org-agenda-get-day-entries
10171 file today scope)))))
10172 ;; Map thru entries and find if we should filter them out
10173 (mapc
10174 (lambda (x)
10175 (let* ((evt (org-trim
10176 (replace-regexp-in-string
10177 org-bracket-link-regexp "\\3"
10178 (or (get-text-property 1 'txt x) ""))))
10179 (cat (get-text-property (1- (length x)) 'org-category x))
10180 (tod (get-text-property 1 'time-of-day x))
10181 (ok (or (null filter)
10182 (and (stringp filter) (string-match filter evt))
10183 (and (functionp filter) (funcall filter x))
10184 (and (listp filter)
10185 (let ((cat-filter (cadr (assq 'category filter)))
10186 (evt-filter (cadr (assq 'headline filter))))
10187 (or (and (stringp cat-filter)
10188 (string-match cat-filter cat))
10189 (and (stringp evt-filter)
10190 (string-match evt-filter evt)))))))
10191 (wrn (get-text-property 1 'warntime x)))
10192 ;; FIXME: Shall we remove text-properties for the appt text?
10193 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10194 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10195 (setq tod (concat "00" (number-to-string tod)))
10196 (setq tod (when (string-match
10197 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10198 (concat (match-string 1 tod) ":"
10199 (match-string 2 tod))))
10200 (when (appt-add tod evt wrn)
10201 (setq cnt (1+ cnt))))))
10202 entries)
10203 (org-release-buffers org-agenda-new-buffers)
10204 (if (eq cnt 0)
10205 (message "No event to add")
10206 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10208 (defun org-agenda-today-p (date)
10209 "Non nil when DATE means today.
10210 DATE is either a list of the form (month day year) or a number of
10211 days as returned by `calendar-absolute-from-gregorian' or
10212 `org-today'. This function considers `org-extend-today-until'
10213 when defining today."
10214 (eq (org-today)
10215 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10217 (defun org-agenda-todo-yesterday (&optional arg)
10218 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10219 (interactive "P")
10220 (let* ((org-use-effective-time t)
10221 (hour (nth 2 (decode-time (org-current-time))))
10222 (org-extend-today-until (1+ hour)))
10223 (org-agenda-todo arg)))
10225 (provide 'org-agenda)
10227 ;;; org-agenda.el ends here