org-eww: Fix docstrings
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob6581895a42146afb2b86179d4d0ae79b36775ac8
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2016 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-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'cl-lib)
49 (require 'org)
50 (require 'org-macs)
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-iso-to-absolute "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-columns-remove-overlays "org-colview" ())
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-agenda-columns "org-colview" ())
83 (declare-function org-add-archive-files "org-archive" (files))
84 (declare-function org-capture "org-capture" (&optional goto keys))
86 (defvar calendar-mode-map)
87 (defvar org-clock-current-task)
88 (defvar org-current-tag-alist)
89 (defvar org-mobile-force-id-on-agenda-items)
90 (defvar org-habit-show-habits)
91 (defvar org-habit-show-habits-only-for-today)
92 (defvar org-habit-show-all-today)
94 ;; Defined somewhere in this file, but used before definition.
95 (defvar org-agenda-buffer-name "*Org Agenda*")
96 (defvar org-agenda-overriding-header nil)
97 (defvar org-agenda-title-append nil)
98 (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
99 (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
100 (defvar original-date) ; dynamically scoped, calendar.el does scope this
102 (defvar org-agenda-undo-list nil
103 "List of undoable operations in the agenda since last refresh.")
104 (defvar org-agenda-pending-undo-list nil
105 "In a series of undo commands, this is the list of remaining undo items.")
107 (defcustom org-agenda-confirm-kill 1
108 "When set, remote killing from the agenda buffer needs confirmation.
109 When t, a confirmation is always needed. When a number N, confirmation is
110 only needed when the text to be killed contains more than N non-white lines."
111 :group 'org-agenda
112 :type '(choice
113 (const :tag "Never" nil)
114 (const :tag "Always" t)
115 (integer :tag "When more than N lines")))
117 (defcustom org-agenda-compact-blocks nil
118 "Non-nil means make the block agenda more compact.
119 This is done globally by leaving out lines like the agenda span
120 name and week number or the separator lines."
121 :group 'org-agenda
122 :type 'boolean)
124 (defcustom org-agenda-block-separator ?=
125 "The separator between blocks in the agenda.
126 If this is a string, it will be used as the separator, with a newline added.
127 If it is a character, it will be repeated to fill the window width.
128 If nil the separator is disabled. In `org-agenda-custom-commands' this
129 addresses the separator between the current and the previous block."
130 :group 'org-agenda
131 :type '(choice
132 (const :tag "Disabled" nil)
133 (character)
134 (string)))
136 (defgroup org-agenda-export nil
137 "Options concerning exporting agenda views in Org-mode."
138 :tag "Org Agenda Export"
139 :group 'org-agenda)
141 (defcustom org-agenda-with-colors t
142 "Non-nil means use colors in agenda views."
143 :group 'org-agenda-export
144 :type 'boolean)
146 (defcustom org-agenda-exporter-settings nil
147 "Alist of variable/value pairs that should be active during agenda export.
148 This is a good place to set options for ps-print and for htmlize.
149 Note that the way this is implemented, the values will be evaluated
150 before assigned to the variables. So make sure to quote values you do
151 *not* want evaluated, for example
153 (setq org-agenda-exporter-settings
154 \\='((ps-print-color-p \\='black-white)))"
155 :group 'org-agenda-export
156 :type '(repeat
157 (list
158 (variable)
159 (sexp :tag "Value"))))
161 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
162 "Hook run in a temporary buffer before writing the agenda to an export file.
163 A useful function for this hook is `org-agenda-add-entry-text'."
164 :group 'org-agenda-export
165 :type 'hook
166 :options '(org-agenda-add-entry-text))
168 (defcustom org-agenda-add-entry-text-maxlines 0
169 "Maximum number of entry text lines to be added to agenda.
170 This is only relevant when `org-agenda-add-entry-text' is part of
171 `org-agenda-before-write-hook', which is the default.
172 When this is 0, nothing will happen. When it is greater than 0, it
173 specifies the maximum number of lines that will be added for each entry
174 that is listed in the agenda view.
176 Note that this variable is not used during display, only when exporting
177 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
178 and `org-agenda-entry-text-maxlines'."
179 :group 'org-agenda
180 :type 'integer)
182 (defcustom org-agenda-add-entry-text-descriptive-links t
183 "Non-nil means export org-links as descriptive links in agenda added text.
184 This variable applies to the text added to the agenda when
185 `org-agenda-add-entry-text-maxlines' is larger than 0.
186 When this variable nil, the URL will (also) be shown."
187 :group 'org-agenda
188 :type 'boolean)
190 (defcustom org-agenda-export-html-style nil
191 "The style specification for exported HTML Agenda files.
192 If this variable contains a string, it will replace the default <style>
193 section as produced by `htmlize'.
194 Since there are different ways of setting style information, this variable
195 needs to contain the full HTML structure to provide a style, including the
196 surrounding HTML tags. The style specifications should include definitions
197 the fonts used by the agenda, here is an example:
199 <style type=\"text/css\">
200 p { font-weight: normal; color: gray; }
201 .org-agenda-structure {
202 font-size: 110%;
203 color: #003399;
204 font-weight: 600;
206 .org-todo {
207 color: #cc6666;
208 font-weight: bold;
210 .org-agenda-done {
211 color: #339933;
213 .org-done {
214 color: #339933;
216 .title { text-align: center; }
217 .todo, .deadline { color: red; }
218 .done { color: green; }
219 </style>
221 or, if you want to keep the style in a file,
223 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
225 As the value of this option simply gets inserted into the HTML <head> header,
226 you can \"misuse\" it to also add other text to the header."
227 :group 'org-agenda-export
228 :group 'org-export-html
229 :type '(choice
230 (const nil)
231 (string)))
233 (defcustom org-agenda-persistent-filter nil
234 "When set, keep filters from one agenda view to the next."
235 :group 'org-agenda
236 :type 'boolean)
238 (defgroup org-agenda-custom-commands nil
239 "Options concerning agenda views in Org-mode."
240 :tag "Org Agenda Custom Commands"
241 :group 'org-agenda)
243 (defconst org-sorting-choice
244 '(choice
245 (const time-up) (const time-down)
246 (const timestamp-up) (const timestamp-down)
247 (const scheduled-up) (const scheduled-down)
248 (const deadline-up) (const deadline-down)
249 (const ts-up) (const ts-down)
250 (const tsia-up) (const tsia-down)
251 (const category-keep) (const category-up) (const category-down)
252 (const tag-down) (const tag-up)
253 (const priority-up) (const priority-down)
254 (const todo-state-up) (const todo-state-down)
255 (const effort-up) (const effort-down)
256 (const habit-up) (const habit-down)
257 (const alpha-up) (const alpha-down)
258 (const user-defined-up) (const user-defined-down))
259 "Sorting choices.")
261 ;; Keep custom values for `org-agenda-filter-preset' compatible with
262 ;; the new variable `org-agenda-tag-filter-preset'.
263 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
264 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
266 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
267 "List of types searched for when creating the daily/weekly agenda.
268 This variable is a list of symbols that controls the types of
269 items that appear in the daily/weekly agenda. Allowed symbols in this
270 list are are
272 :timestamp List items containing a date stamp or date range matching
273 the selected date. This includes sexp entries in angular
274 brackets.
276 :sexp List entries resulting from plain diary-like sexps.
278 :deadline List deadline due on that date. When the date is today,
279 also list any deadlines past due, or due within
280 `org-deadline-warning-days'. `:deadline' must appear before
281 `:scheduled' if the setting of
282 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
283 any effect.
285 :deadline* Same as above, but only include the deadline if it has an
286 hour specification as [h]h:mm.
288 :scheduled List all items which are scheduled for the given date.
289 The diary for *today* also contains items which were
290 scheduled earlier and are not yet marked DONE.
292 :scheduled* Same as above, but only include the scheduled item if it
293 has an hour specification as [h]h:mm.
295 By default, all four non-starred types are turned on.
297 When :scheduled* or :deadline* are included, :schedule or :deadline
298 will be ignored.
300 Never set this variable globally using `setq', because then it
301 will apply to all future agenda commands. Instead, bind it with
302 `let' to scope it dynamically into the agenda-constructing
303 command. A good way to set it is through options in
304 `org-agenda-custom-commands'. For a more flexible (though
305 somewhat less efficient) way of determining what is included in
306 the daily/weekly agenda, see `org-agenda-skip-function'.")
308 (defconst org-agenda-custom-commands-local-options
309 `(repeat :tag "Local settings for this command. Remember to quote values"
310 (choice :tag "Setting"
311 (list :tag "Heading for this block"
312 (const org-agenda-overriding-header)
313 (string :tag "Headline"))
314 (list :tag "Files to be searched"
315 (const org-agenda-files)
316 (list
317 (const :format "" quote)
318 (repeat (file))))
319 (list :tag "Sorting strategy"
320 (const org-agenda-sorting-strategy)
321 (list
322 (const :format "" quote)
323 (repeat
324 ,org-sorting-choice)))
325 (list :tag "Prefix format"
326 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
327 (string))
328 (list :tag "Number of days in agenda"
329 (const org-agenda-span)
330 (choice (const :tag "Day" day)
331 (const :tag "Week" week)
332 (const :tag "Fortnight" fortnight)
333 (const :tag "Month" month)
334 (const :tag "Year" year)
335 (integer :tag "Custom")))
336 (list :tag "Fixed starting date"
337 (const org-agenda-start-day)
338 (string :value "2007-11-01"))
339 (list :tag "Start on day of week"
340 (const org-agenda-start-on-weekday)
341 (choice :value 1
342 (const :tag "Today" nil)
343 (integer :tag "Weekday No.")))
344 (list :tag "Include data from diary"
345 (const org-agenda-include-diary)
346 (boolean))
347 (list :tag "Deadline Warning days"
348 (const org-deadline-warning-days)
349 (integer :value 1))
350 (list :tag "Category filter preset"
351 (const org-agenda-category-filter-preset)
352 (list
353 (const :format "" quote)
354 (repeat
355 (string :tag "+category or -category"))))
356 (list :tag "Tags filter preset"
357 (const org-agenda-tag-filter-preset)
358 (list
359 (const :format "" quote)
360 (repeat
361 (string :tag "+tag or -tag"))))
362 (list :tag "Effort filter preset"
363 (const org-agenda-effort-filter-preset)
364 (list
365 (const :format "" quote)
366 (repeat
367 (string :tag "+=10 or -=10 or +<10 or ->10"))))
368 (list :tag "Regexp filter preset"
369 (const org-agenda-regexp-filter-preset)
370 (list
371 (const :format "" quote)
372 (repeat
373 (string :tag "+regexp or -regexp"))))
374 (list :tag "Set daily/weekly entry types"
375 (const org-agenda-entry-types)
376 (list
377 (const :format "" quote)
378 (set :greedy t :value ,org-agenda-entry-types
379 (const :deadline)
380 (const :scheduled)
381 (const :deadline*)
382 (const :scheduled*)
383 (const :timestamp)
384 (const :sexp))))
385 (list :tag "Standard skipping condition"
386 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
387 (const org-agenda-skip-function)
388 (list
389 (const :format "" quote)
390 (list
391 (choice
392 :tag "Skipping range"
393 (const :tag "Skip entry" org-agenda-skip-entry-if)
394 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
395 (repeat :inline t :tag "Conditions for skipping"
396 (choice
397 :tag "Condition type"
398 (list :tag "Regexp matches" :inline t
399 (const :format "" 'regexp)
400 (regexp))
401 (list :tag "Regexp does not match" :inline t
402 (const :format "" 'notregexp)
403 (regexp))
404 (list :tag "TODO state is" :inline t
405 (const 'todo)
406 (choice
407 (const :tag "Any not-done state" 'todo)
408 (const :tag "Any done state" 'done)
409 (const :tag "Any state" 'any)
410 (list :tag "Keyword list"
411 (const :format "" quote)
412 (repeat (string :tag "Keyword")))))
413 (list :tag "TODO state is not" :inline t
414 (const 'nottodo)
415 (choice
416 (const :tag "Any not-done state" 'todo)
417 (const :tag "Any done state" 'done)
418 (const :tag "Any state" 'any)
419 (list :tag "Keyword list"
420 (const :format "" quote)
421 (repeat (string :tag "Keyword")))))
422 (const :tag "scheduled" 'scheduled)
423 (const :tag "not scheduled" 'notscheduled)
424 (const :tag "deadline" 'deadline)
425 (const :tag "no deadline" 'notdeadline)
426 (const :tag "timestamp" 'timestamp)
427 (const :tag "no timestamp" 'nottimestamp))))))
428 (list :tag "Non-standard skipping condition"
429 :value (org-agenda-skip-function)
430 (const org-agenda-skip-function)
431 (sexp :tag "Function or form (quoted!)"))
432 (list :tag "Any variable"
433 (variable :tag "Variable")
434 (sexp :tag "Value (sexp)"))))
435 "Selection of examples for agenda command settings.
436 This will be spliced into the custom type of
437 `org-agenda-custom-commands'.")
440 (defcustom org-agenda-custom-commands
441 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
442 "Custom commands for the agenda.
443 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 file to write the produced agenda buffer to
472 with 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 or `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 TODO keyword any of" (string))
615 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
616 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
618 (defgroup org-agenda-skip nil
619 "Options concerning skipping parts of agenda files."
620 :tag "Org Agenda Skip"
621 :group 'org-agenda)
623 (defcustom org-agenda-skip-function-global nil
624 "Function to be called at each match during agenda construction.
625 If this function returns nil, the current match should not be skipped.
626 If the function decided to skip an agenda match, is must return the
627 buffer position from which the search should be continued.
628 This may also be a Lisp form, which will be evaluated.
630 This variable will be applied to every agenda match, including
631 tags/property searches and TODO lists. So try to make the test function
632 do its checking as efficiently as possible. To implement a skipping
633 condition just for specific agenda commands, use the variable
634 `org-agenda-skip-function' which can be set in the options section
635 of custom agenda commands."
636 :group 'org-agenda-skip
637 :type 'sexp)
639 (defgroup org-agenda-daily/weekly nil
640 "Options concerning the daily/weekly agenda."
641 :tag "Org Agenda Daily/Weekly"
642 :group 'org-agenda)
643 (defgroup org-agenda-todo-list nil
644 "Options concerning the global todo list agenda view."
645 :tag "Org Agenda Todo List"
646 :group 'org-agenda)
647 (defgroup org-agenda-match-view nil
648 "Options concerning the general tags/property/todo match agenda view."
649 :tag "Org Agenda Match View"
650 :group 'org-agenda)
651 (defgroup org-agenda-search-view nil
652 "Options concerning the search agenda view."
653 :tag "Org Agenda Search View"
654 :group 'org-agenda)
656 (defvar org-agenda-archives-mode nil
657 "Non-nil means the agenda will include archived items.
658 If this is the symbol `trees', trees in the selected agenda scope
659 that are marked with the ARCHIVE tag will be included anyway. When this is
660 t, also all archive files associated with the current selection of agenda
661 files will be included.")
663 (defcustom org-agenda-restriction-lock-highlight-subtree t
664 "Non-nil means highlight the whole subtree when restriction is active.
665 Otherwise only highlight the headline. Highlighting the whole subtree is
666 useful to ensure no edits happen beyond the restricted region."
667 :group 'org-agenda
668 :type 'boolean)
670 (defcustom org-agenda-skip-comment-trees t
671 "Non-nil means skip trees that start with the COMMENT keyword.
672 When nil, these trees are also scanned by agenda commands."
673 :group 'org-agenda-skip
674 :type 'boolean)
676 (defcustom org-agenda-todo-list-sublevels t
677 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
678 When nil, the sublevels of a TODO entry are not checked, resulting in
679 potentially much shorter TODO lists."
680 :group 'org-agenda-skip
681 :group 'org-agenda-todo-list
682 :type 'boolean)
684 (defcustom org-agenda-todo-ignore-with-date nil
685 "Non-nil means don't show entries with a date in the global todo list.
686 You can use this if you prefer to mark mere appointments with a TODO keyword,
687 but don't want them to show up in the TODO list.
688 When this is set, it also covers deadlines and scheduled items, the settings
689 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
690 will be ignored.
691 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
692 :group 'org-agenda-skip
693 :group 'org-agenda-todo-list
694 :type 'boolean)
696 (defcustom org-agenda-todo-ignore-timestamp nil
697 "Non-nil means don't show entries with a timestamp.
698 This applies when creating the global todo list.
699 Valid values are:
701 past Don't show entries for today or in the past.
703 future Don't show entries with a timestamp in the future.
704 The idea behind this is that if it has a future
705 timestamp, you don't want to think about it until the
706 date.
708 all Don't show any entries with a timestamp in the global todo list.
709 The idea behind this is that by setting a timestamp, you
710 have already \"taken care\" of this item.
712 This variable can also have an integer as a value. If positive (N),
713 todos with a timestamp N or more days in the future will be ignored. If
714 negative (-N), todos with a timestamp N or more days in the past will be
715 ignored. If 0, todos with a timestamp either today or in the future will
716 be ignored. For example, a value of -1 will exclude todos with a
717 timestamp in the past (yesterday or earlier), while a value of 7 will
718 exclude todos with a timestamp a week or more in the future.
720 See also `org-agenda-todo-ignore-with-date'.
721 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
722 to make his option also apply to the tags-todo list."
723 :group 'org-agenda-skip
724 :group 'org-agenda-todo-list
725 :version "24.1"
726 :type '(choice
727 (const :tag "Ignore future timestamp todos" future)
728 (const :tag "Ignore past or present timestamp todos" past)
729 (const :tag "Ignore all timestamp todos" all)
730 (const :tag "Show timestamp todos" nil)
731 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
733 (defcustom org-agenda-todo-ignore-scheduled nil
734 "Non-nil means, ignore some scheduled TODO items when making TODO list.
735 This applies when creating the global todo list.
736 Valid values are:
738 past Don't show entries scheduled today or in the past.
740 future Don't show entries scheduled in the future.
741 The idea behind this is that by scheduling it, you don't want to
742 think about it until the scheduled date.
744 all Don't show any scheduled entries in the global todo list.
745 The idea behind this is that by scheduling it, you have already
746 \"taken care\" of this item.
748 t Same as `all', for backward compatibility.
750 This variable can also have an integer as a value. See
751 `org-agenda-todo-ignore-timestamp' for more details.
753 See also `org-agenda-todo-ignore-with-date'.
754 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
755 to make his option also apply to the tags-todo list."
756 :group 'org-agenda-skip
757 :group 'org-agenda-todo-list
758 :type '(choice
759 (const :tag "Ignore future-scheduled todos" future)
760 (const :tag "Ignore past- or present-scheduled todos" past)
761 (const :tag "Ignore all scheduled todos" all)
762 (const :tag "Ignore all scheduled todos (compatibility)" t)
763 (const :tag "Show scheduled todos" nil)
764 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
766 (defcustom org-agenda-todo-ignore-deadlines nil
767 "Non-nil means ignore some deadline TODO items when making TODO list.
768 There are different motivations for using different values, please think
769 carefully when configuring this variable.
771 This applies when creating the global todo list.
772 Valid values are:
774 near Don't show near deadline entries. A deadline is near when it is
775 closer than `org-deadline-warning-days' days. The idea behind this
776 is that such items will appear in the agenda anyway.
778 far Don't show TODO entries where a deadline has been defined, but
779 the deadline is not near. This is useful if you don't want to
780 use the todo list to figure out what to do now.
782 past Don't show entries with a deadline timestamp for today or in the past.
784 future Don't show entries with a deadline timestamp in the future, not even
785 when they become `near' ones. Use it with caution.
787 all Ignore all TODO entries that do have a deadline.
789 t Same as `near', for backward compatibility.
791 This variable can also have an integer as a value. See
792 `org-agenda-todo-ignore-timestamp' for more details.
794 See also `org-agenda-todo-ignore-with-date'.
795 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
796 to make his option also apply to the tags-todo list."
797 :group 'org-agenda-skip
798 :group 'org-agenda-todo-list
799 :type '(choice
800 (const :tag "Ignore near deadlines" near)
801 (const :tag "Ignore near deadlines (compatibility)" t)
802 (const :tag "Ignore far deadlines" far)
803 (const :tag "Ignore all TODOs with a deadlines" all)
804 (const :tag "Show all TODOs, even if they have a deadline" nil)
805 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
807 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
808 "Time unit to use when possibly ignoring an agenda item.
810 See the docstring of various `org-agenda-todo-ignore-*' options.
811 The default is to compare time stamps using days. An item is thus
812 considered to be in the future if it is at least one day after today.
813 Non-nil means to compare time stamps using seconds. An item is then
814 considered future if it has a time value later than current time."
815 :group 'org-agenda-skip
816 :group 'org-agenda-todo-list
817 :version "24.4"
818 :package-version '(Org . "8.0")
819 :type '(choice
820 (const :tag "Compare time with days" nil)
821 (const :tag "Compare time with seconds" t)))
823 (defcustom org-agenda-tags-todo-honor-ignore-options nil
824 "Non-nil means honor todo-list ignores options also in tags-todo search.
825 The variables
826 `org-agenda-todo-ignore-with-date',
827 `org-agenda-todo-ignore-timestamp',
828 `org-agenda-todo-ignore-scheduled',
829 `org-agenda-todo-ignore-deadlines'
830 make the global TODO list skip entries that have time stamps of certain
831 kinds. If this option is set, the same options will also apply for the
832 tags-todo search, which is the general tags/property matcher
833 restricted to unfinished TODO entries only."
834 :group 'org-agenda-skip
835 :group 'org-agenda-todo-list
836 :group 'org-agenda-match-view
837 :type 'boolean)
839 (defcustom org-agenda-skip-scheduled-if-done nil
840 "Non-nil means don't show scheduled items in agenda when they are done.
841 This is relevant for the daily/weekly agenda, not for the TODO list. And
842 it applies only to the actual date of the scheduling. Warnings about
843 an item with a past scheduling dates are always turned off when the item
844 is DONE."
845 :group 'org-agenda-skip
846 :group 'org-agenda-daily/weekly
847 :type 'boolean)
849 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
850 "Non-nil means skip scheduling line if same entry shows because of deadline.
852 In the agenda of today, an entry can show up multiple times
853 because it is both scheduled and has a nearby deadline, and maybe
854 a plain time stamp as well.
856 When this variable is nil, the entry will be shown several times.
858 When set to t, then only the deadline is shown and the fact that
859 the entry is scheduled today or was scheduled previously is not
860 shown.
862 When set to the symbol `not-today', skip scheduled previously,
863 but not scheduled today.
865 When set to the symbol `repeated-after-deadline', skip scheduled
866 items if they are repeated beyond the current deadline."
867 :group 'org-agenda-skip
868 :group 'org-agenda-daily/weekly
869 :type '(choice
870 (const :tag "Never" nil)
871 (const :tag "Always" t)
872 (const :tag "Not when scheduled today" not-today)
873 (const :tag "When repeated past deadline" repeated-after-deadline)))
875 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
876 "Non-nil means skip timestamp line if same entry shows because of deadline.
877 In the agenda of today, an entry can show up multiple times
878 because it has both a plain timestamp and has a nearby deadline.
879 When this variable is t, then only the deadline is shown and the
880 fact that the entry has a timestamp for or including today is not
881 shown. When this variable is nil, the entry will be shown
882 several times."
883 :group 'org-agenda-skip
884 :group 'org-agenda-daily/weekly
885 :version "24.1"
886 :type '(choice
887 (const :tag "Never" nil)
888 (const :tag "Always" t)))
890 (defcustom org-agenda-skip-deadline-if-done nil
891 "Non-nil means don't show deadlines when the corresponding item is done.
892 When nil, the deadline is still shown and should give you a happy feeling.
893 This is relevant for the daily/weekly agenda. And it applied only to the
894 actually date of the deadline. Warnings about approaching and past-due
895 deadlines are always turned off when the item is DONE."
896 :group 'org-agenda-skip
897 :group 'org-agenda-daily/weekly
898 :type 'boolean)
900 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
901 "Non-nil means skip deadline prewarning when entry is also scheduled.
902 This will apply on all days where a prewarning for the deadline would
903 be shown, but not at the day when the entry is actually due. On that day,
904 the deadline will be shown anyway.
905 This variable may be set to nil, t, the symbol `pre-scheduled',
906 or a number which will then give the number of days before the actual
907 deadline when the prewarnings should resume. The symbol `pre-scheduled'
908 eliminates the deadline prewarning only prior to the scheduled date.
909 This can be used in a workflow where the first showing of the deadline will
910 trigger you to schedule it, and then you don't want to be reminded of it
911 because you will take care of it on the day when scheduled."
912 :group 'org-agenda-skip
913 :group 'org-agenda-daily/weekly
914 :version "24.1"
915 :type '(choice
916 (const :tag "Always show prewarning" nil)
917 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
918 (const :tag "Remove prewarning if entry is scheduled" t)
919 (integer :tag "Restart prewarning N days before deadline")))
921 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
922 "Non-nil means skip scheduled delay when entry also has a deadline.
923 This variable may be set to nil, t, the symbol `post-deadline',
924 or a number which will then give the number of days after the actual
925 scheduled date when the delay should expire. The symbol `post-deadline'
926 eliminates the schedule delay when the date is posterior to the deadline."
927 :group 'org-agenda-skip
928 :group 'org-agenda-daily/weekly
929 :version "24.4"
930 :package-version '(Org . "8.0")
931 :type '(choice
932 (const :tag "Always honor delay" nil)
933 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
934 (const :tag "Ignore delay if entry has a deadline" t)
935 (integer :tag "Honor delay up until N days after the scheduled date")))
937 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
938 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
939 When non-nil, after the search for timestamps has matched once in an
940 entry, the rest of the entry will not be searched."
941 :group 'org-agenda-skip
942 :type 'boolean)
944 (defcustom org-agenda-skip-timestamp-if-done nil
945 "Non-nil means don't select item by timestamp or -range if it is DONE."
946 :group 'org-agenda-skip
947 :group 'org-agenda-daily/weekly
948 :type 'boolean)
950 (defcustom org-agenda-dim-blocked-tasks t
951 "Non-nil means dim blocked tasks in the agenda display.
952 This causes some overhead during agenda construction, but if you
953 have turned on `org-enforce-todo-dependencies',
954 `org-enforce-todo-checkbox-dependencies', or any other blocking
955 mechanism, this will create useful feedback in the agenda.
957 Instead of t, this variable can also have the value `invisible'.
958 Then blocked tasks will be invisible and only become visible when
959 they become unblocked. An exemption to this behavior is when a task is
960 blocked because of unchecked checkboxes below it. Since checkboxes do
961 not show up in the agenda views, making this task invisible you remove any
962 trace from agenda views that there is something to do. Therefore, a task
963 that is blocked because of checkboxes will never be made invisible, it
964 will only be dimmed."
965 :group 'org-agenda-daily/weekly
966 :group 'org-agenda-todo-list
967 :version "24.3"
968 :type '(choice
969 (const :tag "Do not dim" nil)
970 (const :tag "Dim to a gray face" t)
971 (const :tag "Make invisible" invisible)))
973 (defcustom org-timeline-show-empty-dates 3
974 "Non-nil means `org-timeline' also shows dates without an entry.
975 When nil, only the days which actually have entries are shown.
976 When t, all days between the first and the last date are shown.
977 When an integer, show also empty dates, but if there is a gap of more than
978 N days, just insert a special line indicating the size of the gap."
979 :group 'org-agenda-skip
980 :type '(choice
981 (const :tag "None" nil)
982 (const :tag "All" t)
983 (integer :tag "at most")))
985 (defgroup org-agenda-startup nil
986 "Options concerning initial settings in the Agenda in Org Mode."
987 :tag "Org Agenda Startup"
988 :group 'org-agenda)
990 (defcustom org-agenda-menu-show-matcher t
991 "Non-nil means show the match string in the agenda dispatcher menu.
992 When nil, the matcher string is not shown, but is put into the help-echo
993 property so than moving the mouse over the command shows it.
994 Setting it to nil is good if matcher strings are very long and/or if
995 you want to use two-columns display (see `org-agenda-menu-two-columns')."
996 :group 'org-agenda
997 :version "24.1"
998 :type 'boolean)
1000 (defcustom org-agenda-menu-two-columns nil
1001 "Non-nil means, use two columns to show custom commands in the dispatcher.
1002 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1003 to nil."
1004 :group 'org-agenda
1005 :version "24.1"
1006 :type 'boolean)
1008 (defcustom org-agenda-finalize-hook nil
1009 "Hook run just before displaying an agenda buffer.
1010 The buffer is still writable when the hook is called.
1012 You can modify some of the buffer substrings but you should be
1013 extra careful not to modify the text properties of the agenda
1014 headlines as the agenda display heavily relies on them."
1015 :group 'org-agenda-startup
1016 :type 'hook)
1018 (defcustom org-agenda-mouse-1-follows-link nil
1019 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1020 A longer mouse click will still set point. Needs to be set
1021 before org.el is loaded."
1022 :group 'org-agenda-startup
1023 :type 'boolean)
1025 (defcustom org-agenda-start-with-follow-mode nil
1026 "The initial value of follow mode in a newly created agenda window."
1027 :group 'org-agenda-startup
1028 :type 'boolean)
1030 (defcustom org-agenda-follow-indirect nil
1031 "Non-nil means `org-agenda-follow-mode' displays only the
1032 current item's tree, in an indirect buffer."
1033 :group 'org-agenda
1034 :version "24.1"
1035 :type 'boolean)
1037 (defcustom org-agenda-show-outline-path t
1038 "Non-nil means show outline path in echo area after line motion."
1039 :group 'org-agenda-startup
1040 :type 'boolean)
1042 (defcustom org-agenda-start-with-entry-text-mode nil
1043 "The initial value of entry-text-mode in a newly created agenda window."
1044 :group 'org-agenda-startup
1045 :type 'boolean)
1047 (defcustom org-agenda-entry-text-maxlines 5
1048 "Number of text lines to be added when `E' is pressed in the agenda.
1050 Note that this variable only used during agenda display. Add add entry text
1051 when exporting the agenda, configure the variable
1052 `org-agenda-add-entry-ext-maxlines'."
1053 :group 'org-agenda
1054 :type 'integer)
1056 (defcustom org-agenda-entry-text-exclude-regexps nil
1057 "List of regular expressions to clean up entry text.
1058 The complete matches of all regular expressions in this list will be
1059 removed from entry text before it is shown in the agenda."
1060 :group 'org-agenda
1061 :type '(repeat (regexp)))
1063 (defcustom org-agenda-entry-text-leaders " > "
1064 "Text prepended to the entry text in agenda buffers."
1065 :version "24.4"
1066 :package-version '(Org . "8.0")
1067 :group 'org-agenda
1068 :type 'string)
1070 (defvar org-agenda-entry-text-cleanup-hook nil
1071 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1072 This cleanup is done in a temporary buffer, so the function may inspect and
1073 change the entire buffer.
1074 Some default stuff like drawers and scheduling/deadline dates will already
1075 have been removed when this is called, as will any matches for regular
1076 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1078 (defvar org-agenda-include-inactive-timestamps nil
1079 "Non-nil means include inactive time stamps in agenda and timeline.
1080 Dynamically scoped.")
1082 (defgroup org-agenda-windows nil
1083 "Options concerning the windows used by the Agenda in Org Mode."
1084 :tag "Org Agenda Windows"
1085 :group 'org-agenda)
1087 (defcustom org-agenda-window-setup 'reorganize-frame
1088 "How the agenda buffer should be displayed.
1089 Possible values for this option are:
1091 current-window Show agenda in the current window, keeping all other windows.
1092 other-window Use `switch-to-buffer-other-window' to display agenda.
1093 only-window Show agenda, deleting all other windows.
1094 reorganize-frame Show only two windows on the current frame, the current
1095 window and the agenda.
1096 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1097 Also, when exiting the agenda, kill that frame.
1098 See also the variable `org-agenda-restore-windows-after-quit'."
1099 :group 'org-agenda-windows
1100 :type '(choice
1101 (const current-window)
1102 (const other-frame)
1103 (const other-window)
1104 (const only-window)
1105 (const reorganize-frame)))
1107 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1108 "The min and max height of the agenda window as a fraction of frame height.
1109 The value of the variable is a cons cell with two numbers between 0 and 1.
1110 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1111 :group 'org-agenda-windows
1112 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1114 (defcustom org-agenda-restore-windows-after-quit nil
1115 "Non-nil means restore window configuration upon exiting agenda.
1116 Before the window configuration is changed for displaying the agenda,
1117 the current status is recorded. When the agenda is exited with
1118 `q' or `x' and this option is set, the old state is restored. If
1119 `org-agenda-window-setup' is `other-frame', the value of this
1120 option will be ignored."
1121 :group 'org-agenda-windows
1122 :type 'boolean)
1124 (defcustom org-agenda-span 'week
1125 "Number of days to include in overview display.
1126 Can be day, week, month, year, or any number of days.
1127 Custom commands can set this variable in the options section."
1128 :group 'org-agenda-daily/weekly
1129 :type '(choice (const :tag "Day" day)
1130 (const :tag "Week" week)
1131 (const :tag "Fortnight" fortnight)
1132 (const :tag "Month" month)
1133 (const :tag "Year" year)
1134 (integer :tag "Custom")))
1136 (defcustom org-agenda-start-on-weekday 1
1137 "Non-nil means start the overview always on the specified weekday.
1138 0 denotes Sunday, 1 denotes Monday, etc.
1139 When nil, always start on the current day.
1140 Custom commands can set this variable in the options section."
1141 :group 'org-agenda-daily/weekly
1142 :type '(choice (const :tag "Today" nil)
1143 (integer :tag "Weekday No.")))
1145 (defcustom org-agenda-show-all-dates t
1146 "Non-nil means `org-agenda' shows every day in the selected range.
1147 When nil, only the days which actually have entries are shown."
1148 :group 'org-agenda-daily/weekly
1149 :type 'boolean)
1151 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1152 "Format string for displaying dates in the agenda.
1153 Used by the daily/weekly agenda and by the timeline. This should be
1154 a format string understood by `format-time-string', or a function returning
1155 the formatted date as a string. The function must take a single argument,
1156 a calendar-style date list like (month day year)."
1157 :group 'org-agenda-daily/weekly
1158 :type '(choice
1159 (string :tag "Format string")
1160 (function :tag "Function")))
1162 (defun org-agenda-format-date-aligned (date)
1163 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1164 This function makes sure that dates are aligned for easy reading."
1165 (require 'cal-iso)
1166 (let* ((dayname (calendar-day-name date))
1167 (day (cadr date))
1168 (day-of-week (calendar-day-of-week date))
1169 (month (car date))
1170 (monthname (calendar-month-name month))
1171 (year (nth 2 date))
1172 (iso-week (org-days-to-iso-week
1173 (calendar-absolute-from-gregorian date)))
1174 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1175 (1- year))
1176 ((and (= month 12) (<= iso-week 1))
1177 (1+ year))
1178 (t year)))
1179 (weekstring (if (= day-of-week 1)
1180 (format " W%02d" iso-week)
1181 "")))
1182 (format "%-10s %2d %s %4d%s"
1183 dayname day monthname year weekstring)))
1185 (defcustom org-agenda-time-leading-zero nil
1186 "Non-nil means use leading zero for military times in agenda.
1187 For example, 9:30am would become 09:30 rather than 9:30."
1188 :group 'org-agenda-daily/weekly
1189 :version "24.1"
1190 :type 'boolean)
1192 (defcustom org-agenda-timegrid-use-ampm nil
1193 "When set, show AM/PM style timestamps on the timegrid."
1194 :group 'org-agenda
1195 :version "24.1"
1196 :type 'boolean)
1198 (defun org-agenda-time-of-day-to-ampm (time)
1199 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1200 (let* ((hour-number (string-to-number (substring time 0 -3)))
1201 (minute (substring time -2))
1202 (ampm "am"))
1203 (cond
1204 ((equal hour-number 12)
1205 (setq ampm "pm"))
1206 ((> hour-number 12)
1207 (setq ampm "pm")
1208 (setq hour-number (- hour-number 12))))
1209 (concat
1210 (if org-agenda-time-leading-zero
1211 (format "%02d" hour-number)
1212 (format "%02s" (number-to-string hour-number)))
1213 ":" minute ampm)))
1215 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1216 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1217 (if org-agenda-timegrid-use-ampm
1218 (org-agenda-time-of-day-to-ampm time)
1219 time))
1221 (defcustom org-agenda-weekend-days '(6 0)
1222 "Which days are weekend?
1223 These days get the special face `org-agenda-date-weekend' in the agenda
1224 and timeline buffers."
1225 :group 'org-agenda-daily/weekly
1226 :type '(set :greedy t
1227 (const :tag "Monday" 1)
1228 (const :tag "Tuesday" 2)
1229 (const :tag "Wednesday" 3)
1230 (const :tag "Thursday" 4)
1231 (const :tag "Friday" 5)
1232 (const :tag "Saturday" 6)
1233 (const :tag "Sunday" 0)))
1235 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1236 "Non-nil means jump to today when moving a past date forward in time.
1237 When using S-right in the agenda to move a a date forward, and the date
1238 stamp currently points to the past, the first key press will move it
1239 to today. WHen nil, just move one day forward even if the date stays
1240 in the past."
1241 :group 'org-agenda-daily/weekly
1242 :version "24.1"
1243 :type 'boolean)
1245 (defcustom org-agenda-include-diary nil
1246 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1247 Custom commands can set this variable in the options section."
1248 :group 'org-agenda-daily/weekly
1249 :type 'boolean)
1251 (defcustom org-agenda-include-deadlines t
1252 "If non-nil, include entries within their deadline warning period.
1253 Custom commands can set this variable in the options section."
1254 :group 'org-agenda-daily/weekly
1255 :version "24.1"
1256 :type 'boolean)
1258 (defcustom org-agenda-repeating-timestamp-show-all t
1259 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1260 When set to a list of strings, only show occurrences of repeating
1261 stamps for these TODO keywords. When nil, only one occurrence is
1262 shown, either today or the nearest into the future."
1263 :group 'org-agenda-daily/weekly
1264 :type '(choice
1265 (const :tag "Show repeating stamps" t)
1266 (repeat :tag "Show repeating stamps for these TODO keywords"
1267 (string :tag "TODO Keyword"))
1268 (const :tag "Don't show repeating stamps" nil)))
1270 (defcustom org-scheduled-past-days 10000
1271 "Number of days to continue listing scheduled items not marked DONE.
1272 When an item is scheduled on a date, it shows up in the agenda on
1273 this day and will be listed until it is marked done or for the
1274 number of days given here."
1275 :group 'org-agenda-daily/weekly
1276 :type 'integer)
1278 (defcustom org-agenda-log-mode-items '(closed clock)
1279 "List of items that should be shown in agenda log mode.
1280 \\<org-agenda-mode-map>\
1281 This list may contain the following symbols:
1283 closed Show entries that have been closed on that day.
1284 clock Show entries that have received clocked time on that day.
1285 state Show all logged state changes.
1286 Note that instead of changing this variable, you can also press \
1287 `\\[universal-argument] \\[org-agenda-log-mode]' in
1288 the agenda to display all available LOG items temporarily."
1289 :group 'org-agenda-daily/weekly
1290 :type '(set :greedy t (const closed) (const clock) (const state)))
1292 (defcustom org-agenda-clock-consistency-checks
1293 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1294 :gap-ok-around ("4:00")
1295 :default-face ((:background "DarkRed") (:foreground "white"))
1296 :overlap-face nil :gap-face nil :no-end-time-face nil
1297 :long-face nil :short-face nil)
1298 "This is a property list, with the following keys:
1300 :max-duration Mark clocking chunks that are longer than this time.
1301 This is a time string like \"HH:MM\", or the number
1302 of minutes as an integer.
1304 :min-duration Mark clocking chunks that are shorter that this.
1305 This is a time string like \"HH:MM\", or the number
1306 of minutes as an integer.
1308 :max-gap Mark gaps between clocking chunks that are longer than
1309 this duration. A number of minutes, or a string
1310 like \"HH:MM\".
1312 :gap-ok-around List of times during the day which are usually not working
1313 times. When a gap is detected, but the gap contains any
1314 of these times, the gap is *not* reported. For example,
1315 if this is (\"4:00\" \"13:00\") then gaps that contain
1316 4:00 in the morning (i.e. the night) and 13:00
1317 (i.e. a typical lunch time) do not cause a warning.
1318 You should have at least one time during the night in this
1319 list, or otherwise the first task each morning will trigger
1320 a warning because it follows a long gap.
1322 Furthermore, the following properties can be used to define faces for
1323 issue display.
1325 :default-face the default face, if the specific face is undefined
1326 :overlap-face face for overlapping clocks
1327 :gap-face face for gaps between clocks
1328 :no-end-time-face face for incomplete clocks
1329 :long-face face for clock intervals that are too long
1330 :short-face face for clock intervals that are too short"
1331 :group 'org-agenda-daily/weekly
1332 :group 'org-clock
1333 :version "24.1"
1334 :type 'plist)
1336 (defcustom org-agenda-log-mode-add-notes t
1337 "Non-nil means add first line of notes to log entries in agenda views.
1338 If a log item like a state change or a clock entry is associated with
1339 notes, the first line of these notes will be added to the entry in the
1340 agenda display."
1341 :group 'org-agenda-daily/weekly
1342 :type 'boolean)
1344 (defcustom org-agenda-start-with-log-mode nil
1345 "The initial value of log-mode in a newly created agenda window.
1346 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1347 explanations on the possible values."
1348 :group 'org-agenda-startup
1349 :group 'org-agenda-daily/weekly
1350 :type '(choice (const :tag "Don't show log items" nil)
1351 (const :tag "Show only log items" only)
1352 (const :tag "Show all possible log items" clockcheck)
1353 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1354 (choice (const :tag "Show closed log items" closed)
1355 (const :tag "Show clocked log items" clock)
1356 (const :tag "Show all logged state changes" state)))))
1358 (defcustom org-agenda-start-with-clockreport-mode nil
1359 "The initial value of clockreport-mode in a newly created agenda window."
1360 :group 'org-agenda-startup
1361 :group 'org-agenda-daily/weekly
1362 :type 'boolean)
1364 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1365 "Property list with parameters for the clocktable in clockreport mode.
1366 This is the display mode that shows a clock table in the daily/weekly
1367 agenda, the properties for this dynamic block can be set here.
1368 The usual clocktable parameters are allowed here, but you cannot set
1369 the properties :name, :tstart, :tend, :block, and :scope - these will
1370 be overwritten to make sure the content accurately reflects the
1371 current display in the agenda."
1372 :group 'org-agenda-daily/weekly
1373 :type 'plist)
1375 (defcustom org-agenda-search-view-always-boolean nil
1376 "Non-nil means the search string is interpreted as individual parts.
1378 The search string for search view can either be interpreted as a phrase,
1379 or as a list of snippets that define a boolean search for a number of
1380 strings.
1382 When this is non-nil, the string will be split on whitespace, and each
1383 snippet will be searched individually, and all must match in order to
1384 select an entry. A snippet is then a single string of non-white
1385 characters, or a string in double quotes, or a regexp in {} braces.
1386 If a snippet is preceded by \"-\", the snippet must *not* match.
1387 \"+\" is syntactic sugar for positive selection. Each snippet may
1388 be found as a full word or a partial word, but see the variable
1389 `org-agenda-search-view-force-full-words'.
1391 When this is nil, search will look for the entire search phrase as one,
1392 with each space character matching any amount of whitespace, including
1393 line breaks.
1395 Even when this is nil, you can still switch to Boolean search dynamically
1396 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1397 is a regexp marked with braces like \"{abc}\", this will also switch to
1398 boolean search."
1399 :group 'org-agenda-search-view
1400 :version "24.1"
1401 :type 'boolean)
1403 (defvaralias 'org-agenda-search-view-search-words-only
1404 'org-agenda-search-view-always-boolean)
1406 (defcustom org-agenda-search-view-force-full-words nil
1407 "Non-nil means, search words must be matches as complete words.
1408 When nil, they may also match part of a word."
1409 :group 'org-agenda-search-view
1410 :version "24.1"
1411 :type 'boolean)
1413 (defcustom org-agenda-search-view-max-outline-level 0
1414 "Maximum outline level to display in search view.
1415 E.g. when this is set to 1, the search view will only
1416 show headlines of level 1. When set to 0, the default
1417 value, don't limit agenda view by outline level."
1418 :group 'org-agenda-search-view
1419 :version "24.4"
1420 :package-version '(Org . "8.3")
1421 :type 'integer)
1423 (defgroup org-agenda-time-grid nil
1424 "Options concerning the time grid in the Org-mode Agenda."
1425 :tag "Org Agenda Time Grid"
1426 :group 'org-agenda)
1428 (defcustom org-agenda-search-headline-for-time t
1429 "Non-nil means search headline for a time-of-day.
1430 If the headline contains a time-of-day in one format or another, it will
1431 be used to sort the entry into the time sequence of items for a day.
1432 Some people have time stamps in the headline that refer to the creation
1433 time or so, and then this produces an unwanted side effect. If this is
1434 the case for your, use this variable to turn off searching the headline
1435 for a time."
1436 :group 'org-agenda-time-grid
1437 :type 'boolean)
1439 (defcustom org-agenda-use-time-grid t
1440 "Non-nil means show a time grid in the agenda schedule.
1441 A time grid is a set of lines for specific times (like every two hours between
1442 8:00 and 20:00). The items scheduled for a day at specific times are
1443 sorted in between these lines.
1444 For details about when the grid will be shown, and what it will look like, see
1445 the variable `org-agenda-time-grid'."
1446 :group 'org-agenda-time-grid
1447 :type 'boolean)
1449 (defcustom org-agenda-time-grid
1450 '((daily today require-timed)
1451 "----------------"
1452 (800 1000 1200 1400 1600 1800 2000))
1454 "The settings for time grid for agenda display.
1455 This is a list of three items. The first item is again a list. It contains
1456 symbols specifying conditions when the grid should be displayed:
1458 daily if the agenda shows a single day
1459 weekly if the agenda shows an entire week
1460 today show grid on current date, independent of daily/weekly display
1461 require-timed show grid only if at least one item has a time specification
1462 remove-match skip grid times already present in an entry
1464 The second item is a string which will be placed behind the grid time.
1466 The third item is a list of integers, indicating the times that should have
1467 a grid line."
1468 :group 'org-agenda-time-grid
1469 :type
1470 '(list
1471 (set :greedy t :tag "Grid Display Options"
1472 (const :tag "Show grid in single day agenda display" daily)
1473 (const :tag "Show grid in weekly agenda display" weekly)
1474 (const :tag "Always show grid for today" today)
1475 (const :tag "Show grid only if any timed entries are present"
1476 require-timed)
1477 (const :tag "Skip grid times already present in an entry"
1478 remove-match))
1479 (string :tag "Grid String")
1480 (repeat :tag "Grid Times" (integer :tag "Time"))))
1482 (defcustom org-agenda-show-current-time-in-grid t
1483 "Non-nil means show the current time in the time grid."
1484 :group 'org-agenda-time-grid
1485 :version "24.1"
1486 :type 'boolean)
1488 (defcustom org-agenda-current-time-string
1489 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1490 "The string for the current time marker in the agenda."
1491 :group 'org-agenda-time-grid
1492 :version "24.1"
1493 :type 'string)
1495 (defgroup org-agenda-sorting nil
1496 "Options concerning sorting in the Org-mode Agenda."
1497 :tag "Org Agenda Sorting"
1498 :group 'org-agenda)
1500 (defcustom org-agenda-sorting-strategy
1501 '((agenda habit-down time-up priority-down category-keep)
1502 (todo priority-down category-keep)
1503 (tags priority-down category-keep)
1504 (search category-keep))
1505 "Sorting structure for the agenda items of a single day.
1506 This is a list of symbols which will be used in sequence to determine
1507 if an entry should be listed before another entry. The following
1508 symbols are recognized:
1510 time-up Put entries with time-of-day indications first, early first
1511 time-down Put entries with time-of-day indications first, late first
1512 timestamp-up Sort by any timestamp, early first
1513 timestamp-down Sort by any timestamp, late first
1514 scheduled-up Sort by scheduled timestamp, early first
1515 scheduled-down Sort by scheduled timestamp, late first
1516 deadline-up Sort by deadline timestamp, early first
1517 deadline-down Sort by deadline timestamp, late first
1518 ts-up Sort by active timestamp, early first
1519 ts-down Sort by active timestamp, late first
1520 tsia-up Sort by inactive timestamp, early first
1521 tsia-down Sort by inactive timestamp, late first
1522 category-keep Keep the default order of categories, corresponding to the
1523 sequence in `org-agenda-files'.
1524 category-up Sort alphabetically by category, A-Z.
1525 category-down Sort alphabetically by category, Z-A.
1526 tag-up Sort alphabetically by last tag, A-Z.
1527 tag-down Sort alphabetically by last tag, Z-A.
1528 priority-up Sort numerically by priority, high priority last.
1529 priority-down Sort numerically by priority, high priority first.
1530 todo-state-up Sort by todo state, tasks that are done last.
1531 todo-state-down Sort by todo state, tasks that are done first.
1532 effort-up Sort numerically by estimated effort, high effort last.
1533 effort-down Sort numerically by estimated effort, high effort first.
1534 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1535 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1536 habit-up Put entries that are habits first
1537 habit-down Put entries that are habits last
1538 alpha-up Sort headlines alphabetically
1539 alpha-down Sort headlines alphabetically, reversed
1541 The different possibilities will be tried in sequence, and testing stops
1542 if one comparison returns a \"not-equal\". For example, the default
1543 '(time-up category-keep priority-down)
1544 means: Pull out all entries having a specified time of day and sort them,
1545 in order to make a time schedule for the current day the first thing in the
1546 agenda listing for the day. Of the entries without a time indication, keep
1547 the grouped in categories, don't sort the categories, but keep them in
1548 the sequence given in `org-agenda-files'. Within each category sort by
1549 priority.
1551 Leaving out `category-keep' would mean that items will be sorted across
1552 categories by priority.
1554 Instead of a single list, this can also be a set of list for specific
1555 contents, with a context symbol in the car of the list, any of
1556 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1558 Custom commands can bind this variable in the options section."
1559 :group 'org-agenda-sorting
1560 :type `(choice
1561 (repeat :tag "General" ,org-sorting-choice)
1562 (list :tag "Individually"
1563 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1564 (repeat ,org-sorting-choice))
1565 (cons (const :tag "Strategy for TODO lists" todo)
1566 (repeat ,org-sorting-choice))
1567 (cons (const :tag "Strategy for Tags matches" tags)
1568 (repeat ,org-sorting-choice))
1569 (cons (const :tag "Strategy for search matches" search)
1570 (repeat ,org-sorting-choice)))))
1572 (defcustom org-agenda-cmp-user-defined nil
1573 "A function to define the comparison `user-defined'.
1574 This function must receive two arguments, agenda entry a and b.
1575 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1576 the user comparison, return nil.
1577 When this is defined, you can make `user-defined-up' and `user-defined-down'
1578 part of an agenda sorting strategy."
1579 :group 'org-agenda-sorting
1580 :type 'symbol)
1582 (defcustom org-sort-agenda-notime-is-late t
1583 "Non-nil means items without time are considered late.
1584 This is only relevant for sorting. When t, items which have no explicit
1585 time like 15:30 will be considered as 99:01, i.e. later than any items which
1586 do have a time. When nil, the default time is before 0:00. You can use this
1587 option to decide if the schedule for today should come before or after timeless
1588 agenda entries."
1589 :group 'org-agenda-sorting
1590 :type 'boolean)
1592 (defcustom org-sort-agenda-noeffort-is-high t
1593 "Non-nil means items without effort estimate are sorted as high effort.
1594 This also applies when filtering an agenda view with respect to the
1595 < or > effort operator. Then, tasks with no effort defined will be treated
1596 as tasks with high effort.
1597 When nil, such items are sorted as 0 minutes effort."
1598 :group 'org-agenda-sorting
1599 :type 'boolean)
1601 (defgroup org-agenda-line-format nil
1602 "Options concerning the entry prefix in the Org-mode agenda display."
1603 :tag "Org Agenda Line Format"
1604 :group 'org-agenda)
1606 (defcustom org-agenda-prefix-format
1607 '((agenda . " %i %-12:c%?-12t% s")
1608 (timeline . " % s")
1609 (todo . " %i %-12:c")
1610 (tags . " %i %-12:c")
1611 (search . " %i %-12:c"))
1612 "Format specifications for the prefix of items in the agenda views.
1613 An alist with five entries, each for the different agenda types. The
1614 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1615 The values are format strings.
1617 This format works similar to a printf format, with the following meaning:
1619 %c the category of the item, \"Diary\" for entries from the diary,
1620 or as given by the CATEGORY keyword or derived from the file name
1621 %e the effort required by the item
1622 %l the level of the item (insert X space(s) if item is of level X)
1623 %i the icon category of the item, see `org-agenda-category-icon-alist'
1624 %T the last tag of the item (ignore inherited tags, which come first)
1625 %t the HH:MM time-of-day specification if one applies to the entry
1626 %s Scheduling/Deadline information, a short string
1627 %b show breadcrumbs, i.e., the names of the higher levels
1628 %(expression) Eval EXPRESSION and replace the control string
1629 by the result
1631 All specifiers work basically like the standard `%s' of printf, but may
1632 contain two additional characters: a question mark just after the `%'
1633 and a whitespace/punctuation character just before the final letter.
1635 If the first character after `%' is a question mark, the entire field
1636 will only be included if the corresponding value applies to the current
1637 entry. This is useful for fields which should have fixed width when
1638 present, but zero width when absent. For example, \"%?-12t\" will
1639 result in a 12 character time field if a time of the day is specified,
1640 but will completely disappear in entries which do not contain a time.
1642 If there is punctuation or whitespace character just before the
1643 final format letter, this character will be appended to the field
1644 value if the value is not empty. For example, the format
1645 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1646 the category is empty, no additional colon is inserted.
1648 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1649 which means:
1651 - Indent the line with two space characters
1652 - Give the category a 12 chars wide field, padded with whitespace on
1653 the right (because of `-'). Append a colon if there is a category
1654 (because of `:').
1655 - If there is a time-of-day, put it into a 12 chars wide field. If no
1656 time, don't put in an empty field, just skip it (because of '?').
1657 - Finally, put the scheduling information.
1659 See also the variables `org-agenda-remove-times-when-in-prefix' and
1660 `org-agenda-remove-tags'.
1662 Custom commands can set this variable in the options section."
1663 :type '(choice
1664 (string :tag "General format")
1665 (list :greedy t :tag "View dependent"
1666 (cons (const agenda) (string :tag "Format"))
1667 (cons (const timeline) (string :tag "Format"))
1668 (cons (const todo) (string :tag "Format"))
1669 (cons (const tags) (string :tag "Format"))
1670 (cons (const search) (string :tag "Format"))))
1671 :group 'org-agenda-line-format)
1673 (defvar org-prefix-format-compiled nil
1674 "The compiled prefix format and associated variables.
1675 This is a list where first element is a list of variable bindings, and second
1676 element is the compiled format expression. See the variable
1677 `org-agenda-prefix-format'.")
1679 (defcustom org-agenda-todo-keyword-format "%-1s"
1680 "Format for the TODO keyword in agenda lines.
1681 Set this to something like \"%-12s\" if you want all TODO keywords
1682 to occupy a fixed space in the agenda display."
1683 :group 'org-agenda-line-format
1684 :type 'string)
1686 (defcustom org-agenda-diary-sexp-prefix nil
1687 "A regexp that matches part of a diary sexp entry
1688 which should be treated as scheduling/deadline information in
1689 `org-agenda'.
1691 For example, you can use this to extract the `diary-remind-message' from
1692 `diary-remind' entries."
1693 :group 'org-agenda-line-format
1694 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1696 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1697 "Text preceding timerange entries in the agenda view.
1698 This is a list with two strings. The first applies when the range
1699 is entirely on one day. The second applies if the range spans several days.
1700 The strings may have two \"%d\" format specifiers which will be filled
1701 with the sequence number of the days, and the total number of days in the
1702 range, respectively."
1703 :group 'org-agenda-line-format
1704 :type '(list
1705 (string :tag "Deadline today ")
1706 (choice :tag "Deadline relative"
1707 (string :tag "Format string")
1708 (function))))
1710 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1711 "Text preceding scheduled items in the agenda view.
1712 This is a list with two strings. The first applies when the item is
1713 scheduled on the current day. The second applies when it has been scheduled
1714 previously, it may contain a %d indicating that this is the nth time that
1715 this item is scheduled, due to automatic rescheduling of unfinished items
1716 for the following day. So this number is one larger than the number of days
1717 that passed since this item was scheduled first."
1718 :group 'org-agenda-line-format
1719 :version "24.4"
1720 :package-version '(Org . "8.0")
1721 :type '(list
1722 (string :tag "Scheduled today ")
1723 (string :tag "Scheduled previously")))
1725 (defcustom org-agenda-inactive-leader "["
1726 "Text preceding item pulled into the agenda by inactive time stamps.
1727 These entries are added to the agenda when pressing \"[\"."
1728 :group 'org-agenda-line-format
1729 :version "24.1"
1730 :type 'string)
1732 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1733 "Text preceding deadline items in the agenda view.
1734 This is a list with three strings. The first applies when the item has its
1735 deadline on the current day. The second applies when the deadline is in the
1736 future, the third one when it is in the past. The strings may contain %d
1737 to capture the number of days."
1738 :group 'org-agenda-line-format
1739 :version "24.4"
1740 :package-version '(Org . "8.0")
1741 :type '(list
1742 (string :tag "Deadline today ")
1743 (string :tag "Deadline in the future ")
1744 (string :tag "Deadline in the past ")))
1746 (defcustom org-agenda-remove-times-when-in-prefix t
1747 "Non-nil means remove duplicate time specifications in agenda items.
1748 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1749 time-of-day specification in a headline or diary entry is extracted and
1750 placed into the prefix. If this option is non-nil, the original specification
1751 \(a timestamp or -range, or just a plain time(range) specification like
1752 11:30-4pm) will be removed for agenda display. This makes the agenda less
1753 cluttered.
1754 The option can be t or nil. It may also be the symbol `beg', indicating
1755 that the time should only be removed when it is located at the beginning of
1756 the headline/diary entry."
1757 :group 'org-agenda-line-format
1758 :type '(choice
1759 (const :tag "Always" t)
1760 (const :tag "Never" nil)
1761 (const :tag "When at beginning of entry" beg)))
1763 (defcustom org-agenda-remove-timeranges-from-blocks nil
1764 "Non-nil means remove time ranges specifications in agenda
1765 items that span on several days."
1766 :group 'org-agenda-line-format
1767 :version "24.1"
1768 :type 'boolean)
1770 (defcustom org-agenda-default-appointment-duration nil
1771 "Default duration for appointments that only have a starting time.
1772 When nil, no duration is specified in such cases.
1773 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1774 :group 'org-agenda-line-format
1775 :type '(choice
1776 (integer :tag "Minutes")
1777 (const :tag "No default duration")))
1779 (defcustom org-agenda-show-inherited-tags t
1780 "Non-nil means show inherited tags in each agenda line.
1782 When this option is set to `always', it take precedences over
1783 `org-agenda-use-tag-inheritance' and inherited tags are shown
1784 in every agenda.
1786 When this option is set to t (the default), inherited tags are
1787 shown when they are available, i.e. when the value of
1788 `org-agenda-use-tag-inheritance' has been taken into account.
1790 This can be set to a list of agenda types in which the agenda
1791 must display the inherited tags. Available types are `todo',
1792 `agenda', `search' and `timeline'.
1794 When set to nil, never show inherited tags in agenda lines."
1795 :group 'org-agenda-line-format
1796 :group 'org-agenda
1797 :version "24.3"
1798 :type '(choice
1799 (const :tag "Show inherited tags when available" t)
1800 (const :tag "Always show inherited tags" always)
1801 (repeat :tag "Show inherited tags only in selected agenda types"
1802 (symbol :tag "Agenda type"))))
1804 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1805 "List of agenda view types where to use tag inheritance.
1807 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1808 controlled by `org-use-tag-inheritance'. In other agenda types,
1809 `org-use-tag-inheritance' is not used for the selection of the
1810 agenda entries. Still, you may want the agenda to be aware of
1811 the inherited tags anyway, e.g. for later tag filtering.
1813 Allowed value are `todo', `search', `timeline' and `agenda'.
1815 This variable has no effect if `org-agenda-show-inherited-tags'
1816 is set to `always'. In that case, the agenda is aware of those
1817 tags.
1819 The default value sets tags in every agenda type. Setting this
1820 option to nil will speed up non-tags agenda view a lot."
1821 :group 'org-agenda
1822 :version "24.3"
1823 :type '(choice
1824 (const :tag "Use tag inheritance in all agenda types" t)
1825 (repeat :tag "Use tag inheritance in selected agenda types"
1826 (symbol :tag "Agenda type"))))
1828 (defcustom org-agenda-hide-tags-regexp nil
1829 "Regular expression used to filter away specific tags in agenda views.
1830 This means that these tags will be present, but not be shown in the agenda
1831 line. Secondary filtering will still work on the hidden tags.
1832 Nil means don't hide any tags."
1833 :group 'org-agenda-line-format
1834 :type '(choice
1835 (const :tag "Hide none" nil)
1836 (string :tag "Regexp ")))
1838 (defcustom org-agenda-remove-tags nil
1839 "Non-nil means remove the tags from the headline copy in the agenda.
1840 When this is the symbol `prefix', only remove tags when
1841 `org-agenda-prefix-format' contains a `%T' specifier."
1842 :group 'org-agenda-line-format
1843 :type '(choice
1844 (const :tag "Always" t)
1845 (const :tag "Never" nil)
1846 (const :tag "When prefix format contains %T" prefix)))
1848 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1849 'org-agenda-remove-tags)
1851 (defcustom org-agenda-tags-column -80
1852 "Shift tags in agenda items to this column.
1853 If this number is positive, it specifies the column. If it is negative,
1854 it means that the tags should be flushright to that column. For example,
1855 -80 works well for a normal 80 character screen."
1856 :group 'org-agenda-line-format
1857 :type 'integer)
1859 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1861 (defcustom org-agenda-fontify-priorities 'cookies
1862 "Non-nil means highlight low and high priorities in agenda.
1863 When t, the highest priority entries are bold, lowest priority italic.
1864 However, settings in `org-priority-faces' will overrule these faces.
1865 When this variable is the symbol `cookies', only fontify the
1866 cookies, not the entire task.
1867 This may also be an association list of priority faces, whose
1868 keys are the character values of `org-highest-priority',
1869 `org-default-priority', and `org-lowest-priority' (the default values
1870 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1871 color as a string, or a list like `(:background \"Red\")'.
1872 If it is a color, the variable `org-faces-easy-properties'
1873 determines if it is a foreground or a background color."
1874 :group 'org-agenda-line-format
1875 :type '(choice
1876 (const :tag "Never" nil)
1877 (const :tag "Defaults" t)
1878 (const :tag "Cookies only" cookies)
1879 (repeat :tag "Specify"
1880 (list (character :tag "Priority" :value ?A)
1881 (choice :tag "Face "
1882 (string :tag "Color")
1883 (sexp :tag "Face"))))))
1885 (defcustom org-agenda-day-face-function nil
1886 "Function called to determine what face should be used to display a day.
1887 The only argument passed to that function is the day. It should
1888 returns a face, or nil if does not want to specify a face and let
1889 the normal rules apply."
1890 :group 'org-agenda-line-format
1891 :version "24.1"
1892 :type '(choice (const nil) (function)))
1894 (defcustom org-agenda-category-icon-alist nil
1895 "Alist of category icon to be displayed in agenda views.
1897 Each entry should have the following format:
1899 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1901 Where CATEGORY-REGEXP is a regexp matching the categories where
1902 the icon should be displayed.
1903 FILE-OR-DATA either a file path or a string containing image data.
1905 The other fields can be omitted safely if not needed:
1906 TYPE indicates the image type.
1907 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1908 image data.
1909 PROPS are additional image attributes to assign to the image,
1910 like, e.g. `:ascent center'.
1912 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1914 If you want to set the display properties yourself, just put a
1915 list as second element:
1917 (CATEGORY-REGEXP (MY PROPERTY LIST))
1919 For example, to display a 16px horizontal space for Emacs
1920 category, you can use:
1922 (\"Emacs\" \\='(space . (:width (16))))"
1923 :group 'org-agenda-line-format
1924 :version "24.1"
1925 :type '(alist :key-type (string :tag "Regexp matching category")
1926 :value-type (choice (list :tag "Icon"
1927 (string :tag "File or data")
1928 (symbol :tag "Type")
1929 (boolean :tag "Data?")
1930 (repeat :tag "Extra image properties" :inline t symbol))
1931 (list :tag "Display properties" sexp))))
1933 (defgroup org-agenda-column-view nil
1934 "Options concerning column view in the agenda."
1935 :tag "Org Agenda Column View"
1936 :group 'org-agenda)
1938 (defcustom org-agenda-view-columns-initially nil
1939 "When non-nil, switch to columns view right after creating the agenda."
1940 :group 'org-agenda-column-view
1941 :type 'boolean
1942 :version "25.1"
1943 :package-version '(Org . "9.0")
1944 :safe #'booleanp)
1946 (defcustom org-agenda-columns-show-summaries t
1947 "Non-nil means show summaries for columns displayed in the agenda view."
1948 :group 'org-agenda-column-view
1949 :type 'boolean)
1951 (defcustom org-agenda-columns-compute-summary-properties t
1952 "Non-nil means recompute all summary properties before column view.
1953 When column view in the agenda is listing properties that have a summary
1954 operator, it can go to all relevant buffers and recompute the summaries
1955 there. This can mean overhead for the agenda column view, but is necessary
1956 to have thing up to date.
1957 As a special case, a CLOCKSUM property also makes sure that the clock
1958 computations are current."
1959 :group 'org-agenda-column-view
1960 :type 'boolean)
1962 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1963 "Non-nil means the duration of an appointment will add to day effort.
1964 The property to which appointment durations will be added is the one given
1965 in the option `org-effort-property'. If an appointment does not have
1966 an end time, `org-agenda-default-appointment-duration' will be used. If that
1967 is not set, an appointment without end time will not contribute to the time
1968 estimate."
1969 :group 'org-agenda-column-view
1970 :type 'boolean)
1972 (defcustom org-agenda-auto-exclude-function nil
1973 "A function called with a tag to decide if it is filtered on \
1974 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1975 The sole argument to the function, which is called once for each
1976 possible tag, is a string giving the name of the tag. The
1977 function should return either nil if the tag should be included
1978 as normal, or \"-<TAG>\" to exclude the tag.
1979 Note that for the purpose of tag filtering, only the lower-case version of
1980 all tags will be considered, so that this function will only ever see
1981 the lower-case version of all tags."
1982 :group 'org-agenda
1983 :type '(choice (const nil) (function)))
1985 (defcustom org-agenda-bulk-custom-functions nil
1986 "Alist of characters and custom functions for bulk actions.
1987 For example, this value makes those two functions available:
1989 \\='((?R set-category)
1990 (?C bulk-cut))
1992 With selected entries in an agenda buffer, `B R' will call
1993 the custom function `set-category' on the selected entries.
1994 Note that functions in this alist don't need to be quoted."
1995 :type '(alist :key-type character :value-type (group function))
1996 :version "24.1"
1997 :group 'org-agenda)
1999 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2000 "Execute BODY with point at location given by `org-hd-marker' property.
2001 If STRING is non-nil, the text property will be fetched from position 0
2002 in that string. If STRING is nil, it will be fetched from the beginning
2003 of the current line."
2004 (org-with-gensyms (marker)
2005 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2006 'org-hd-marker ,string)))
2007 (with-current-buffer (marker-buffer ,marker)
2008 (save-excursion
2009 (goto-char ,marker)
2010 ,@body)))))
2011 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2013 (defun org-add-agenda-custom-command (entry)
2014 "Replace or add a command in `org-agenda-custom-commands'.
2015 This is mostly for hacking and trying a new command - once the command
2016 works you probably want to add it to `org-agenda-custom-commands' for good."
2017 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2018 (if ass
2019 (setcdr ass (cdr entry))
2020 (push entry org-agenda-custom-commands))))
2022 ;;; Define the org-agenda-mode
2024 (defvar org-agenda-mode-map (make-sparse-keymap)
2025 "Keymap for `org-agenda-mode'.")
2026 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2028 (defvar org-agenda-menu) ; defined later in this file.
2029 (defvar org-agenda-restrict nil) ; defined later in this file.
2030 (defvar org-agenda-follow-mode nil)
2031 (defvar org-agenda-entry-text-mode nil)
2032 (defvar org-agenda-clockreport-mode nil)
2033 (defvar org-agenda-show-log nil)
2034 (defvar org-agenda-redo-command nil)
2035 (defvar org-agenda-query-string nil)
2036 (defvar org-agenda-mode-hook nil
2037 "Hook run after `org-agenda-mode' is turned on.
2038 The buffer is still writable when this hook is called.")
2039 (defvar org-agenda-type nil)
2040 (defvar org-agenda-force-single-file nil)
2041 (defvar org-agenda-bulk-marked-entries nil
2042 "List of markers that refer to marked entries in the agenda.")
2043 (defvar org-agenda-current-date nil
2044 "Active date when building the agenda.")
2046 ;;; Multiple agenda buffers support
2048 (defcustom org-agenda-sticky nil
2049 "Non-nil means agenda q key will bury agenda buffers.
2050 Agenda commands will then show existing buffer instead of generating new ones.
2051 When nil, `q' will kill the single agenda buffer."
2052 :group 'org-agenda
2053 :version "24.3"
2054 :type 'boolean)
2057 ;;;###autoload
2058 (defun org-toggle-sticky-agenda (&optional arg)
2059 "Toggle `org-agenda-sticky'."
2060 (interactive "P")
2061 (let ((new-value (if arg
2062 (> (prefix-numeric-value arg) 0)
2063 (not org-agenda-sticky))))
2064 (if (equal new-value org-agenda-sticky)
2065 (and (called-interactively-p 'interactive)
2066 (message "Sticky agenda was already %s"
2067 (if org-agenda-sticky "enabled" "disabled")))
2068 (setq org-agenda-sticky new-value)
2069 (org-agenda-kill-all-agenda-buffers)
2070 (and (called-interactively-p 'interactive)
2071 (message "Sticky agenda %s"
2072 (if org-agenda-sticky "enabled" "disabled"))))))
2074 (defvar org-agenda-buffer nil
2075 "Agenda buffer currently being generated.")
2077 (defvar org-agenda-last-prefix-arg nil)
2078 (defvar org-agenda-this-buffer-name nil)
2079 (defvar org-agenda-doing-sticky-redo nil)
2080 (defvar org-agenda-this-buffer-is-sticky nil)
2081 (defvar org-agenda-last-indirect-buffer nil
2082 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2084 (defconst org-agenda-local-vars
2085 '(org-agenda-this-buffer-name
2086 org-agenda-undo-list
2087 org-agenda-pending-undo-list
2088 org-agenda-follow-mode
2089 org-agenda-entry-text-mode
2090 org-agenda-clockreport-mode
2091 org-agenda-show-log
2092 org-agenda-redo-command
2093 org-agenda-query-string
2094 org-agenda-type
2095 org-agenda-bulk-marked-entries
2096 org-agenda-undo-has-started-in
2097 org-agenda-info
2098 org-agenda-pre-window-conf
2099 org-agenda-columns-active
2100 org-agenda-tag-filter
2101 org-agenda-category-filter
2102 org-agenda-top-headline-filter
2103 org-agenda-regexp-filter
2104 org-agenda-effort-filter
2105 org-agenda-markers
2106 org-agenda-last-search-view-search-was-boolean
2107 org-agenda-last-indirect-buffer
2108 org-agenda-filtered-by-category
2109 org-agenda-filter-form
2110 org-agenda-cycle-counter
2111 org-agenda-last-prefix-arg)
2112 "Variables that must be local in agenda buffers to allow multiple buffers.")
2114 (defun org-agenda-mode ()
2115 "Mode for time-sorted view on action items in Org-mode files.
2117 The following commands are available:
2119 \\{org-agenda-mode-map}"
2120 (interactive)
2121 (cond (org-agenda-doing-sticky-redo
2122 ;; Refreshing sticky agenda-buffer
2124 ;; Preserve the value of `org-agenda-local-vars' variables,
2125 ;; while letting `kill-all-local-variables' kill the rest
2126 (let ((save (buffer-local-variables)))
2127 (kill-all-local-variables)
2128 (mapc 'make-local-variable org-agenda-local-vars)
2129 (dolist (elem save)
2130 (let ((var (car elem))
2131 (val (cdr elem)))
2132 (when (and val
2133 (member var org-agenda-local-vars))
2134 (set var val)))))
2135 (setq-local org-agenda-this-buffer-is-sticky t))
2136 (org-agenda-sticky
2137 ;; Creating a sticky Agenda buffer for the first time
2138 (kill-all-local-variables)
2139 (mapc 'make-local-variable org-agenda-local-vars)
2140 (setq-local org-agenda-this-buffer-is-sticky t))
2142 ;; Creating a non-sticky agenda buffer
2143 (kill-all-local-variables)
2144 (setq-local org-agenda-this-buffer-is-sticky nil)))
2145 (setq org-agenda-undo-list nil
2146 org-agenda-pending-undo-list nil
2147 org-agenda-bulk-marked-entries nil)
2148 (setq major-mode 'org-agenda-mode)
2149 ;; Keep global-font-lock-mode from turning on font-lock-mode
2150 (setq-local font-lock-global-modes (list 'not major-mode))
2151 (setq mode-name "Org-Agenda")
2152 (setq indent-tabs-mode nil)
2153 (use-local-map org-agenda-mode-map)
2154 (easy-menu-add org-agenda-menu)
2155 (if org-startup-truncated (setq truncate-lines t))
2156 (setq-local line-move-visual nil)
2157 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2158 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2159 ;; Make sure properties are removed when copying text
2160 (add-hook 'filter-buffer-substring-functions
2161 (lambda (fun start end delete)
2162 (substring-no-properties (funcall fun start end delete)))
2163 nil t)
2164 (unless org-agenda-keep-modes
2165 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2166 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2167 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2168 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2169 (add-to-invisibility-spec '(org-filtered))
2170 (add-to-invisibility-spec '(org-link))
2171 (easy-menu-change
2172 '("Agenda") "Agenda Files"
2173 (append
2174 (list
2175 (vector
2176 (if (get 'org-agenda-files 'org-restrict)
2177 "Restricted to single file"
2178 "Edit File List")
2179 '(org-edit-agenda-file-list)
2180 (not (get 'org-agenda-files 'org-restrict)))
2181 "--")
2182 (mapcar 'org-file-menu-entry (org-agenda-files))))
2183 (org-agenda-set-mode-name)
2184 (apply
2185 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2186 (list 'org-agenda-mode-hook)))
2188 (substitute-key-definition 'undo 'org-agenda-undo
2189 org-agenda-mode-map global-map)
2190 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2191 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2192 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2193 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2194 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2195 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2196 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2197 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2198 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2199 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2200 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2201 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2202 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2203 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2204 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2205 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2206 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2207 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2208 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2209 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2210 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2211 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2212 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2213 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2214 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2215 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2216 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2217 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2218 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2219 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2220 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2221 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2222 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2223 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2224 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2225 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2226 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2227 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2228 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2229 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2230 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2231 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2232 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2233 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2234 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2235 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2236 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2237 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2238 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2239 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2241 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2242 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2243 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2244 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2245 (while l (org-defkey org-agenda-mode-map
2246 (int-to-string (pop l)) 'digit-argument)))
2248 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2249 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2250 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2251 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2252 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2253 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2254 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2255 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2256 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2257 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2258 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2259 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2260 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2261 'org-clock-modify-effort-estimate)
2262 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2263 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2264 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2265 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2266 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2267 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2268 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2269 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2270 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2271 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2272 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2273 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2274 (substitute-key-definition 'next-line 'org-agenda-next-line
2275 org-agenda-mode-map global-map)
2276 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2277 org-agenda-mode-map global-map)
2278 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2279 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2280 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2281 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2282 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2283 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2284 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2285 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2286 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2287 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2288 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2289 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2290 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2291 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2292 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2293 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2294 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2295 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2296 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2297 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2298 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2299 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2300 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2301 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2302 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2303 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2304 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2305 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2306 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2307 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2309 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2310 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2311 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2312 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2313 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2314 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2315 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2316 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2317 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2318 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2319 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2320 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2321 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2322 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2323 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2325 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2326 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2328 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2329 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2331 (when org-agenda-mouse-1-follows-link
2332 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2333 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2334 '("Agenda"
2335 ("Agenda Files")
2336 "--"
2337 ("Agenda Dates"
2338 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2339 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2340 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2341 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2342 "--"
2343 ("View"
2344 ["Day View" org-agenda-day-view
2345 :active (org-agenda-check-type nil 'agenda)
2346 :style radio :selected (eq org-agenda-current-span 'day)
2347 :keys "v d (or just d)"]
2348 ["Week View" org-agenda-week-view
2349 :active (org-agenda-check-type nil 'agenda)
2350 :style radio :selected (eq org-agenda-current-span 'week)
2351 :keys "v w"]
2352 ["Fortnight View" org-agenda-fortnight-view
2353 :active (org-agenda-check-type nil 'agenda)
2354 :style radio :selected (eq org-agenda-current-span 'fortnight)
2355 :keys "v f"]
2356 ["Month View" org-agenda-month-view
2357 :active (org-agenda-check-type nil 'agenda)
2358 :style radio :selected (eq org-agenda-current-span 'month)
2359 :keys "v m"]
2360 ["Year View" org-agenda-year-view
2361 :active (org-agenda-check-type nil 'agenda)
2362 :style radio :selected (eq org-agenda-current-span 'year)
2363 :keys "v y"]
2364 "--"
2365 ["Include Diary" org-agenda-toggle-diary
2366 :style toggle :selected org-agenda-include-diary
2367 :active (org-agenda-check-type nil 'agenda)]
2368 ["Include Deadlines" org-agenda-toggle-deadlines
2369 :style toggle :selected org-agenda-include-deadlines
2370 :active (org-agenda-check-type nil 'agenda)]
2371 ["Use Time Grid" org-agenda-toggle-time-grid
2372 :style toggle :selected org-agenda-use-time-grid
2373 :active (org-agenda-check-type nil 'agenda)]
2374 "--"
2375 ["Show clock report" org-agenda-clockreport-mode
2376 :style toggle :selected org-agenda-clockreport-mode
2377 :active (org-agenda-check-type nil 'agenda)]
2378 ["Show some entry text" org-agenda-entry-text-mode
2379 :style toggle :selected org-agenda-entry-text-mode
2380 :active t]
2381 "--"
2382 ["Show Logbook entries" org-agenda-log-mode
2383 :style toggle :selected org-agenda-show-log
2384 :active (org-agenda-check-type nil 'agenda 'timeline)
2385 :keys "v l (or just l)"]
2386 ["Include archived trees" org-agenda-archives-mode
2387 :style toggle :selected org-agenda-archives-mode :active t
2388 :keys "v a"]
2389 ["Include archive files" (org-agenda-archives-mode t)
2390 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2391 :keys "v A"]
2392 "--"
2393 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2394 ["Write view to file" org-agenda-write t]
2395 ["Rebuild buffer" org-agenda-redo t]
2396 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2397 "--"
2398 ["Show original entry" org-agenda-show t]
2399 ["Go To (other window)" org-agenda-goto t]
2400 ["Go To (this window)" org-agenda-switch-to t]
2401 ["Capture with cursor date" org-agenda-capture t]
2402 ["Follow Mode" org-agenda-follow-mode
2403 :style toggle :selected org-agenda-follow-mode :active t]
2404 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2405 "--"
2406 ("TODO"
2407 ["Cycle TODO" org-agenda-todo t]
2408 ["Next TODO set" org-agenda-todo-nextset t]
2409 ["Previous TODO set" org-agenda-todo-previousset t]
2410 ["Add note" org-agenda-add-note t])
2411 ("Archive/Refile/Delete"
2412 ["Archive default" org-agenda-archive-default t]
2413 ["Archive default" org-agenda-archive-default-with-confirmation t]
2414 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2415 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2416 ["Archive subtree" org-agenda-archive t]
2417 "--"
2418 ["Refile" org-agenda-refile t]
2419 "--"
2420 ["Delete subtree" org-agenda-kill t])
2421 ("Bulk action"
2422 ["Mark entry" org-agenda-bulk-mark t]
2423 ["Mark all" org-agenda-bulk-mark-all t]
2424 ["Unmark entry" org-agenda-bulk-unmark t]
2425 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2426 ["Toggle mark" org-agenda-bulk-toggle t]
2427 ["Toggle all" org-agenda-bulk-toggle-all t]
2428 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2429 ["Act on all marked" org-agenda-bulk-action t]
2430 "--"
2431 ("Tags and Properties"
2432 ["Show all Tags" org-agenda-show-tags t]
2433 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2434 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2435 "--"
2436 ["Column View" org-columns t])
2437 ("Deadline/Schedule"
2438 ["Schedule" org-agenda-schedule t]
2439 ["Set Deadline" org-agenda-deadline t]
2440 "--"
2441 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2442 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2443 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2444 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2445 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2446 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2447 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2448 ("Clock and Effort"
2449 ["Clock in" org-agenda-clock-in t]
2450 ["Clock out" org-agenda-clock-out t]
2451 ["Clock cancel" org-agenda-clock-cancel t]
2452 ["Goto running clock" org-clock-goto t]
2453 "--"
2454 ["Set Effort" org-agenda-set-effort t]
2455 ["Change clocked effort" org-clock-modify-effort-estimate
2456 (org-clock-is-active)])
2457 ("Priority"
2458 ["Set Priority" org-agenda-priority t]
2459 ["Increase Priority" org-agenda-priority-up t]
2460 ["Decrease Priority" org-agenda-priority-down t]
2461 ["Show Priority" org-show-priority t])
2462 ("Calendar/Diary"
2463 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2464 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2465 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2466 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2467 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2468 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2469 "--"
2470 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2471 "--"
2472 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2473 "--"
2474 ("MobileOrg"
2475 ["Push Files and Views" org-mobile-push t]
2476 ["Get Captured and Flagged" org-mobile-pull t]
2477 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2478 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2479 "--"
2480 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2481 "--"
2482 ["Quit" org-agenda-quit t]
2483 ["Exit and Release Buffers" org-agenda-exit t]
2486 ;;; Agenda undo
2488 (defvar org-agenda-allow-remote-undo t
2489 "Non-nil means allow remote undo from the agenda buffer.")
2490 (defvar org-agenda-undo-has-started-in nil
2491 "Buffers that have already seen `undo-start' in the current undo sequence.")
2493 (defun org-agenda-undo ()
2494 "Undo a remote editing step in the agenda.
2495 This undoes changes both in the agenda buffer and in the remote buffer
2496 that have been changed along."
2497 (interactive)
2498 (or org-agenda-allow-remote-undo
2499 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2500 (if (not (eq this-command last-command))
2501 (setq org-agenda-undo-has-started-in nil
2502 org-agenda-pending-undo-list org-agenda-undo-list))
2503 (if (not org-agenda-pending-undo-list)
2504 (user-error "No further undo information"))
2505 (let* ((entry (pop org-agenda-pending-undo-list))
2506 buf line cmd rembuf)
2507 (setq cmd (pop entry) line (pop entry))
2508 (setq rembuf (nth 2 entry))
2509 (org-with-remote-undo rembuf
2510 (while (bufferp (setq buf (pop entry)))
2511 (if (pop entry)
2512 (with-current-buffer buf
2513 (let ((last-undo-buffer buf)
2514 (inhibit-read-only t))
2515 (unless (memq buf org-agenda-undo-has-started-in)
2516 (push buf org-agenda-undo-has-started-in)
2517 (make-local-variable 'pending-undo-list)
2518 (undo-start))
2519 (while (and pending-undo-list
2520 (listp pending-undo-list)
2521 (not (car pending-undo-list)))
2522 (pop pending-undo-list))
2523 (undo-more 1))))))
2524 (org-goto-line line)
2525 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2527 (defun org-verify-change-for-undo (l1 l2)
2528 "Verify that a real change occurred between the undo lists L1 and L2."
2529 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2530 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2531 (not (eq l1 l2)))
2533 ;;; Agenda dispatch
2535 (defvar org-agenda-restrict-begin (make-marker))
2536 (defvar org-agenda-restrict-end (make-marker))
2537 (defvar org-agenda-last-dispatch-buffer nil)
2538 (defvar org-agenda-overriding-restriction nil)
2540 (defcustom org-agenda-custom-commands-contexts nil
2541 "Alist of custom agenda keys and contextual rules.
2543 For example, if you have a custom agenda command \"p\" and you
2544 want this command to be accessible only from plain text files,
2545 use this:
2547 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2549 Here are the available contexts definitions:
2551 in-file: command displayed only in matching files
2552 in-mode: command displayed only in matching modes
2553 not-in-file: command not displayed in matching files
2554 not-in-mode: command not displayed in matching modes
2555 in-buffer: command displayed only in matching buffers
2556 not-in-buffer: command not displayed in matching buffers
2557 [function]: a custom function taking no argument
2559 If you define several checks, the agenda command will be
2560 accessible if there is at least one valid check.
2562 You can also bind a key to another agenda custom command
2563 depending on contextual rules.
2565 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2567 Here it means: in .txt files, use \"p\" as the key for the
2568 agenda command otherwise associated with \"q\". (The command
2569 originally associated with \"q\" is not displayed to avoid
2570 duplicates.)"
2571 :version "24.3"
2572 :group 'org-agenda-custom-commands
2573 :type '(repeat (list :tag "Rule"
2574 (string :tag " Agenda key")
2575 (string :tag "Replace by command")
2576 (repeat :tag "Available when"
2577 (choice
2578 (cons :tag "Condition"
2579 (choice
2580 (const :tag "In file" in-file)
2581 (const :tag "Not in file" not-in-file)
2582 (const :tag "In buffer" in-buffer)
2583 (const :tag "Not in buffer" not-in-buffer)
2584 (const :tag "In mode" in-mode)
2585 (const :tag "Not in mode" not-in-mode))
2586 (regexp))
2587 (function :tag "Custom function"))))))
2589 (defcustom org-agenda-max-entries nil
2590 "Maximum number of entries to display in an agenda.
2591 This can be nil (no limit) or an integer or an alist of agenda
2592 types with an associated number of entries to display in this
2593 type."
2594 :version "24.4"
2595 :package-version '(Org . "8.0")
2596 :group 'org-agenda-custom-commands
2597 :type '(choice (symbol :tag "No limit" nil)
2598 (integer :tag "Max number of entries")
2599 (repeat
2600 (cons (choice :tag "Agenda type"
2601 (const agenda)
2602 (const todo)
2603 (const tags)
2604 (const search)
2605 (const timeline))
2606 (integer :tag "Max number of entries")))))
2608 (defcustom org-agenda-max-todos nil
2609 "Maximum number of TODOs to display in an agenda.
2610 This can be nil (no limit) or an integer or an alist of agenda
2611 types with an associated number of entries to display in this
2612 type."
2613 :version "24.4"
2614 :package-version '(Org . "8.0")
2615 :group 'org-agenda-custom-commands
2616 :type '(choice (symbol :tag "No limit" nil)
2617 (integer :tag "Max number of TODOs")
2618 (repeat
2619 (cons (choice :tag "Agenda type"
2620 (const agenda)
2621 (const todo)
2622 (const tags)
2623 (const search)
2624 (const timeline))
2625 (integer :tag "Max number of TODOs")))))
2627 (defcustom org-agenda-max-tags nil
2628 "Maximum number of tagged entries to display in an agenda.
2629 This can be nil (no limit) or an integer or an alist of agenda
2630 types with an associated number of entries to display in this
2631 type."
2632 :version "24.4"
2633 :package-version '(Org . "8.0")
2634 :group 'org-agenda-custom-commands
2635 :type '(choice (symbol :tag "No limit" nil)
2636 (integer :tag "Max number of tagged entries")
2637 (repeat
2638 (cons (choice :tag "Agenda type"
2639 (const agenda)
2640 (const todo)
2641 (const tags)
2642 (const search)
2643 (const timeline))
2644 (integer :tag "Max number of tagged entries")))))
2646 (defcustom org-agenda-max-effort nil
2647 "Maximum cumulated effort duration for the agenda.
2648 This can be nil (no limit) or a number of minutes (as an integer)
2649 or an alist of agenda types with an associated number of minutes
2650 to limit entries to in this type."
2651 :version "24.4"
2652 :package-version '(Org . "8.0")
2653 :group 'org-agenda-custom-commands
2654 :type '(choice (symbol :tag "No limit" nil)
2655 (integer :tag "Max number of minutes")
2656 (repeat
2657 (cons (choice :tag "Agenda type"
2658 (const agenda)
2659 (const todo)
2660 (const tags)
2661 (const search)
2662 (const timeline))
2663 (integer :tag "Max number of minutes")))))
2665 (defvar org-agenda-keep-restricted-file-list nil)
2666 (defvar org-keys nil)
2667 (defvar org-match nil)
2668 ;;;###autoload
2669 (defun org-agenda (&optional arg org-keys restriction)
2670 "Dispatch agenda commands to collect entries to the agenda buffer.
2671 Prompts for a command to execute. Any prefix arg will be passed
2672 on to the selected command. The default selections are:
2674 a Call `org-agenda-list' to display the agenda for current day or week.
2675 t Call `org-todo-list' to display the global todo list.
2676 T Call `org-todo-list' to display the global todo list, select only
2677 entries with a specific TODO keyword (the user gets a prompt).
2678 m Call `org-tags-view' to display headlines with tags matching
2679 a condition (the user is prompted for the condition).
2680 M Like `m', but select only TODO entries, no ordinary headlines.
2681 L Create a timeline for the current buffer.
2682 e Export views to associated files.
2683 s Search entries for keywords.
2684 S Search entries for keywords, only with TODO keywords.
2685 / Multi occur across all agenda files and also files listed
2686 in `org-agenda-text-search-extra-files'.
2687 < Restrict agenda commands to buffer, subtree, or region.
2688 Press several times to get the desired effect.
2689 > Remove a previous restriction.
2690 # List \"stuck\" projects.
2691 ! Configure what \"stuck\" means.
2692 C Configure custom agenda commands.
2694 More commands can be added by configuring the variable
2695 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2696 searches can be pre-defined in this way.
2698 If the current buffer is in Org-mode and visiting a file, you can also
2699 first press `<' once to indicate that the agenda should be temporarily
2700 \(until the next use of \\[org-agenda]) restricted to the current file.
2701 Pressing `<' twice means to restrict to the current subtree or region
2702 \(if active)."
2703 (interactive "P")
2704 (catch 'exit
2705 (let* ((prefix-descriptions nil)
2706 (org-agenda-buffer-name org-agenda-buffer-name)
2707 (org-agenda-window-setup (if (equal (buffer-name)
2708 org-agenda-buffer-name)
2709 'current-window
2710 org-agenda-window-setup))
2711 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2712 (org-agenda-custom-commands
2713 ;; normalize different versions
2714 (delq nil
2715 (mapcar
2716 (lambda (x)
2717 (cond ((stringp (cdr x))
2718 (push x prefix-descriptions)
2719 nil)
2720 ((stringp (nth 1 x)) x)
2721 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2722 (t (cons (car x) (cons "" (cdr x))))))
2723 org-agenda-custom-commands)))
2724 (org-agenda-custom-commands
2725 (org-contextualize-keys
2726 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2727 (buf (current-buffer))
2728 (bfn (buffer-file-name (buffer-base-buffer)))
2729 entry key type org-match lprops ans)
2730 ;; Turn off restriction unless there is an overriding one,
2731 (unless org-agenda-overriding-restriction
2732 (unless org-agenda-keep-restricted-file-list
2733 ;; There is a request to keep the file list in place
2734 (put 'org-agenda-files 'org-restrict nil))
2735 (setq org-agenda-restrict nil)
2736 (move-marker org-agenda-restrict-begin nil)
2737 (move-marker org-agenda-restrict-end nil))
2738 ;; Delete old local properties
2739 (put 'org-agenda-redo-command 'org-lprops nil)
2740 ;; Delete previously set last-arguments
2741 (put 'org-agenda-redo-command 'last-args nil)
2742 ;; Remember where this call originated
2743 (setq org-agenda-last-dispatch-buffer (current-buffer))
2744 (unless org-keys
2745 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2746 org-keys (car ans)
2747 restriction (cdr ans)))
2748 ;; If we have sticky agenda buffers, set a name for the buffer,
2749 ;; depending on the invoking keys. The user may still set this
2750 ;; as a command option, which will overwrite what we do here.
2751 (if org-agenda-sticky
2752 (setq org-agenda-buffer-name
2753 (format "*Org Agenda(%s)*" org-keys)))
2754 ;; Establish the restriction, if any
2755 (when (and (not org-agenda-overriding-restriction) restriction)
2756 (put 'org-agenda-files 'org-restrict (list bfn))
2757 (cond
2758 ((eq restriction 'region)
2759 (setq org-agenda-restrict (current-buffer))
2760 (move-marker org-agenda-restrict-begin (region-beginning))
2761 (move-marker org-agenda-restrict-end (region-end)))
2762 ((eq restriction 'subtree)
2763 (save-excursion
2764 (setq org-agenda-restrict (current-buffer))
2765 (org-back-to-heading t)
2766 (move-marker org-agenda-restrict-begin (point))
2767 (move-marker org-agenda-restrict-end
2768 (progn (org-end-of-subtree t)))))))
2770 ;; For example the todo list should not need it (but does...)
2771 (cond
2772 ((setq entry (assoc org-keys org-agenda-custom-commands))
2773 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2774 (progn
2775 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2776 lprops (nth 4 entry))
2777 (if org-agenda-sticky
2778 (setq org-agenda-buffer-name
2779 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2780 (format "*Org Agenda(%s)*" org-keys))))
2781 (put 'org-agenda-redo-command 'org-lprops lprops)
2782 (cond
2783 ((eq type 'agenda)
2784 (org-let lprops '(org-agenda-list current-prefix-arg)))
2785 ((eq type 'agenda*)
2786 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2787 ((eq type 'alltodo)
2788 (org-let lprops '(org-todo-list current-prefix-arg)))
2789 ((eq type 'search)
2790 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2791 ((eq type 'stuck)
2792 (org-let lprops '(org-agenda-list-stuck-projects
2793 current-prefix-arg)))
2794 ((eq type 'tags)
2795 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2796 ((eq type 'tags-todo)
2797 (org-let lprops '(org-tags-view '(4) org-match)))
2798 ((eq type 'todo)
2799 (org-let lprops '(org-todo-list org-match)))
2800 ((eq type 'tags-tree)
2801 (org-check-for-org-mode)
2802 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2803 ((eq type 'todo-tree)
2804 (org-check-for-org-mode)
2805 (org-let lprops
2806 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2807 (regexp-quote org-match) "\\>"))))
2808 ((eq type 'occur-tree)
2809 (org-check-for-org-mode)
2810 (org-let lprops '(org-occur org-match)))
2811 ((functionp type)
2812 (org-let lprops '(funcall type org-match)))
2813 ((fboundp type)
2814 (org-let lprops '(funcall type org-match)))
2815 (t (user-error "Invalid custom agenda command type %s" type))))
2816 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2817 ((equal org-keys "C")
2818 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2819 (customize-variable 'org-agenda-custom-commands))
2820 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2821 ((equal org-keys "s") (call-interactively 'org-search-view))
2822 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2823 ((equal org-keys "t") (call-interactively 'org-todo-list))
2824 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2825 ((equal org-keys "m") (call-interactively 'org-tags-view))
2826 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2827 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2828 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2829 (add-hook
2830 'post-command-hook
2831 (lambda ()
2832 (unless (current-message)
2833 (let* ((m (org-agenda-get-any-marker))
2834 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2835 (when note
2836 (message (concat
2837 "FLAGGING-NOTE ([?] for more info): "
2838 (org-add-props
2839 (replace-regexp-in-string
2840 "\\\\n" "//"
2841 (copy-sequence note))
2842 nil 'face 'org-warning)))))))
2843 t t))
2844 ((equal org-keys "L")
2845 (unless (derived-mode-p 'org-mode)
2846 (user-error "This is not an Org-mode file"))
2847 (unless restriction
2848 (put 'org-agenda-files 'org-restrict (list bfn))
2849 (org-call-with-arg 'org-timeline arg)))
2850 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2851 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2852 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2853 (t (user-error "Invalid agenda key"))))))
2855 (defvar org-agenda-multi)
2857 (defun org-agenda-append-agenda ()
2858 "Append another agenda view to the current one.
2859 This function allows interactive building of block agendas.
2860 Agenda views are separated by `org-agenda-block-separator'."
2861 (interactive)
2862 (unless (derived-mode-p 'org-agenda-mode)
2863 (user-error "Can only append from within agenda buffer"))
2864 (let ((org-agenda-multi t))
2865 (org-agenda)
2866 (widen)
2867 (org-agenda-finalize)
2868 (setq buffer-read-only t)
2869 (org-agenda-fit-window-to-buffer)))
2871 (defun org-agenda-normalize-custom-commands (cmds)
2872 "Normalize custom commands CMDS."
2873 (delq nil
2874 (mapcar
2875 (lambda (x)
2876 (cond ((stringp (cdr x)) nil)
2877 ((stringp (nth 1 x)) x)
2878 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2879 (t (cons (car x) (cons "" (cdr x))))))
2880 cmds)))
2882 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2883 "The user interface for selecting an agenda command."
2884 (catch 'exit
2885 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2886 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2887 (region-p (org-region-active-p))
2888 (custom org-agenda-custom-commands)
2889 (selstring "")
2890 restriction second-time
2891 c entry key type match prefixes rmheader header-end custom1 desc
2892 line lines left right n n1)
2893 (save-window-excursion
2894 (delete-other-windows)
2895 (org-switch-to-buffer-other-window " *Agenda Commands*")
2896 (erase-buffer)
2897 (insert (eval-when-compile
2898 (let ((header
2899 "Press key for an agenda command: < Buffer, subtree/region restriction
2900 -------------------------------- > Remove restriction
2901 a Agenda for current week or day e Export agenda views
2902 t List of all TODO entries T Entries with special TODO kwd
2903 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2904 s Search for keywords S Like s, but only TODO entries
2905 L Timeline for current buffer # List stuck projects (!=configure)
2906 / Multi-occur C Configure custom agenda commands
2907 ? Find :FLAGGED: entries * Toggle sticky agenda views
2909 (start 0))
2910 (while (string-match
2911 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2912 header start)
2913 (setq start (match-end 0))
2914 (add-text-properties (match-beginning 2) (match-end 2)
2915 '(face bold) header))
2916 header)))
2917 (setq header-end (point-marker))
2918 (while t
2919 (setq custom1 custom)
2920 (when (eq rmheader t)
2921 (org-goto-line 1)
2922 (re-search-forward ":" nil t)
2923 (delete-region (match-end 0) (point-at-eol))
2924 (forward-char 1)
2925 (looking-at "-+")
2926 (delete-region (match-end 0) (point-at-eol))
2927 (move-marker header-end (match-end 0)))
2928 (goto-char header-end)
2929 (delete-region (point) (point-max))
2931 ;; Produce all the lines that describe custom commands and prefixes
2932 (setq lines nil)
2933 (while (setq entry (pop custom1))
2934 (setq key (car entry) desc (nth 1 entry)
2935 type (nth 2 entry)
2936 match (nth 3 entry))
2937 (if (> (length key) 1)
2938 (add-to-list 'prefixes (string-to-char key))
2939 (setq line
2940 (format
2941 "%-4s%-14s"
2942 (org-add-props (copy-sequence key)
2943 '(face bold))
2944 (cond
2945 ((string-match "\\S-" desc) desc)
2946 ((eq type 'agenda) "Agenda for current week or day")
2947 ((eq type 'agenda*) "Appointments for current week or day")
2948 ((eq type 'alltodo) "List of all TODO entries")
2949 ((eq type 'search) "Word search")
2950 ((eq type 'stuck) "List of stuck projects")
2951 ((eq type 'todo) "TODO keyword")
2952 ((eq type 'tags) "Tags query")
2953 ((eq type 'tags-todo) "Tags (TODO)")
2954 ((eq type 'tags-tree) "Tags tree")
2955 ((eq type 'todo-tree) "TODO kwd tree")
2956 ((eq type 'occur-tree) "Occur tree")
2957 ((functionp type) (if (symbolp type)
2958 (symbol-name type)
2959 "Lambda expression"))
2960 (t "???"))))
2961 (if org-agenda-menu-show-matcher
2962 (setq line
2963 (concat line ": "
2964 (cond
2965 ((stringp match)
2966 (setq match (copy-sequence match))
2967 (org-add-props match nil 'face 'org-warning))
2968 ((listp type)
2969 (format "set of %d commands" (length type))))))
2970 (if (org-string-nw-p match)
2971 (add-text-properties
2972 0 (length line) (list 'help-echo
2973 (concat "Matcher: " match)) line)))
2974 (push line lines)))
2975 (setq lines (nreverse lines))
2976 (when prefixes
2977 (mapc (lambda (x)
2978 (push
2979 (format "%s %s"
2980 (org-add-props (char-to-string x)
2981 nil 'face 'bold)
2982 (or (cdr (assoc (concat selstring
2983 (char-to-string x))
2984 prefix-descriptions))
2985 "Prefix key"))
2986 lines))
2987 prefixes))
2989 ;; Check if we should display in two columns
2990 (if org-agenda-menu-two-columns
2991 (progn
2992 (setq n (length lines)
2993 n1 (+ (/ n 2) (mod n 2))
2994 right (nthcdr n1 lines)
2995 left (copy-sequence lines))
2996 (setcdr (nthcdr (1- n1) left) nil))
2997 (setq left lines right nil))
2998 (while left
2999 (insert "\n" (pop left))
3000 (when right
3001 (if (< (current-column) 40)
3002 (move-to-column 40 t)
3003 (insert " "))
3004 (insert (pop right))))
3006 ;; Make the window the right size
3007 (goto-char (point-min))
3008 (if second-time
3009 (if (not (pos-visible-in-window-p (point-max)))
3010 (org-fit-window-to-buffer))
3011 (setq second-time t)
3012 (org-fit-window-to-buffer))
3014 ;; Ask for selection
3015 (message "Press key for agenda command%s:"
3016 (if (or restrict-ok org-agenda-overriding-restriction)
3017 (if org-agenda-overriding-restriction
3018 " (restriction lock active)"
3019 (if restriction
3020 (format " (restricted to %s)" restriction)
3021 " (unrestricted)"))
3022 ""))
3023 (setq c (read-char-exclusive))
3024 (message "")
3025 (cond
3026 ((assoc (char-to-string c) custom)
3027 (setq selstring (concat selstring (char-to-string c)))
3028 (throw 'exit (cons selstring restriction)))
3029 ((memq c prefixes)
3030 (setq selstring (concat selstring (char-to-string c))
3031 prefixes nil
3032 rmheader (or rmheader t)
3033 custom (delq nil (mapcar
3034 (lambda (x)
3035 (if (or (= (length (car x)) 1)
3036 (/= (string-to-char (car x)) c))
3038 (cons (substring (car x) 1) (cdr x))))
3039 custom))))
3040 ((eq c ?*)
3041 (call-interactively 'org-toggle-sticky-agenda)
3042 (sit-for 2))
3043 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3044 (message "Restriction is only possible in Org-mode buffers")
3045 (ding) (sit-for 1))
3046 ((eq c ?1)
3047 (org-agenda-remove-restriction-lock 'noupdate)
3048 (setq restriction 'buffer))
3049 ((eq c ?0)
3050 (org-agenda-remove-restriction-lock 'noupdate)
3051 (setq restriction (if region-p 'region 'subtree)))
3052 ((eq c ?<)
3053 (org-agenda-remove-restriction-lock 'noupdate)
3054 (setq restriction
3055 (cond
3056 ((eq restriction 'buffer)
3057 (if region-p 'region 'subtree))
3058 ((memq restriction '(subtree region))
3059 nil)
3060 (t 'buffer))))
3061 ((eq c ?>)
3062 (org-agenda-remove-restriction-lock 'noupdate)
3063 (setq restriction nil))
3064 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3065 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3066 ((and (> (length selstring) 0) (eq c ?\d))
3067 (delete-window)
3068 (org-agenda-get-restriction-and-command prefix-descriptions))
3070 ((equal c ?q) (error "Abort"))
3071 (t (user-error "Invalid key %c" c))))))))
3073 (defun org-agenda-fit-window-to-buffer ()
3074 "Fit the window to the buffer size."
3075 (and (memq org-agenda-window-setup '(reorganize-frame))
3076 (fboundp 'fit-window-to-buffer)
3077 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3078 (= (car org-agenda-window-frame-fractions) 1.0))
3079 (delete-other-windows)
3080 (org-fit-window-to-buffer
3082 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3083 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3085 (defvar org-cmd nil)
3086 (defvar org-agenda-overriding-cmd nil)
3087 (defvar org-agenda-overriding-arguments nil)
3088 (defvar org-agenda-overriding-cmd-arguments nil)
3089 (defun org-agenda-run-series (name series)
3090 "Run agenda NAME as a SERIES of agenda commands."
3091 (org-let (nth 1 series) '(org-agenda-prepare name))
3092 ;; We need to reset agenda markers here, because when constructing a
3093 ;; block agenda, the individual blocks do not do that.
3094 (org-agenda-reset-markers)
3095 (let* ((org-agenda-multi t)
3096 (redo (list 'org-agenda-run-series name (list 'quote series)))
3097 (cmds (car series))
3098 (gprops (nth 1 series))
3099 match ;; The byte compiler incorrectly complains about this. Keep it!
3100 org-cmd type lprops)
3101 (while (setq org-cmd (pop cmds))
3102 (setq type (car org-cmd))
3103 (setq match (eval (nth 1 org-cmd)))
3104 (setq lprops (nth 2 org-cmd))
3105 (let ((org-agenda-overriding-arguments
3106 (if (eq org-agenda-overriding-cmd org-cmd)
3107 (or org-agenda-overriding-arguments
3108 org-agenda-overriding-cmd-arguments))))
3109 (cond
3110 ((eq type 'agenda)
3111 (org-let2 gprops lprops
3112 '(call-interactively 'org-agenda-list)))
3113 ((eq type 'agenda*)
3114 (org-let2 gprops lprops
3115 '(funcall 'org-agenda-list nil nil t)))
3116 ((eq type 'alltodo)
3117 (org-let2 gprops lprops
3118 '(call-interactively 'org-todo-list)))
3119 ((eq type 'search)
3120 (org-let2 gprops lprops
3121 '(org-search-view current-prefix-arg match nil)))
3122 ((eq type 'stuck)
3123 (org-let2 gprops lprops
3124 '(call-interactively 'org-agenda-list-stuck-projects)))
3125 ((eq type 'tags)
3126 (org-let2 gprops lprops
3127 '(org-tags-view current-prefix-arg match)))
3128 ((eq type 'tags-todo)
3129 (org-let2 gprops lprops
3130 '(org-tags-view '(4) match)))
3131 ((eq type 'todo)
3132 (org-let2 gprops lprops
3133 '(org-todo-list match)))
3134 ((fboundp type)
3135 (org-let2 gprops lprops
3136 '(funcall type match)))
3137 (t (error "Invalid type in command series")))))
3138 (widen)
3139 (let ((inhibit-read-only t))
3140 (add-text-properties (point-min) (point-max)
3141 `(org-series t org-series-redo-cmd ,redo)))
3142 (setq org-agenda-redo-command redo)
3143 (goto-char (point-min)))
3144 (org-agenda-fit-window-to-buffer)
3145 (org-let (nth 1 series) '(org-agenda-finalize)))
3147 ;;;###autoload
3148 (defmacro org-batch-agenda (cmd-key &rest parameters)
3149 "Run an agenda command in batch mode and send the result to STDOUT.
3150 If CMD-KEY is a string of length 1, it is used as a key in
3151 `org-agenda-custom-commands' and triggers this command. If it is a
3152 longer string it is used as a tags/todo match string.
3153 Parameters are alternating variable names and values that will be bound
3154 before running the agenda command."
3155 (org-eval-in-environment (org-make-parameter-alist parameters)
3156 (let (org-agenda-sticky)
3157 (if (> (length cmd-key) 1)
3158 (org-tags-view nil cmd-key)
3159 (org-agenda nil cmd-key))))
3160 (set-buffer org-agenda-buffer-name)
3161 (princ (buffer-string)))
3163 (defvar org-agenda-info nil)
3165 ;;;###autoload
3166 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3167 "Run an agenda command in batch mode and send the result to STDOUT.
3168 If CMD-KEY is a string of length 1, it is used as a key in
3169 `org-agenda-custom-commands' and triggers this command. If it is a
3170 longer string it is used as a tags/todo match string.
3171 Parameters are alternating variable names and values that will be bound
3172 before running the agenda command.
3174 The output gives a line for each selected agenda item. Each
3175 item is a list of comma-separated values, like this:
3177 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3179 category The category of the item
3180 head The headline, without TODO kwd, TAGS and PRIORITY
3181 type The type of the agenda entry, can be
3182 todo selected in TODO match
3183 tagsmatch selected in tags match
3184 diary imported from diary
3185 deadline a deadline on given date
3186 scheduled scheduled on given date
3187 timestamp entry has timestamp on given date
3188 closed entry was closed on given date
3189 upcoming-deadline warning about deadline
3190 past-scheduled forwarded scheduled item
3191 block entry has date block including g. date
3192 todo The todo keyword, if any
3193 tags All tags including inherited ones, separated by colons
3194 date The relevant date, like 2007-2-14
3195 time The time, like 15:00-16:50
3196 extra Sting with extra planning info
3197 priority-l The priority letter if any was given
3198 priority-n The computed numerical priority
3199 agenda-day The day in the agenda where this is listed"
3200 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3201 (org-make-parameter-alist parameters))
3202 (if (> (length cmd-key) 2)
3203 (org-tags-view nil cmd-key)
3204 (org-agenda nil cmd-key)))
3205 (set-buffer org-agenda-buffer-name)
3206 (let* ((lines (org-split-string (buffer-string) "\n"))
3207 line)
3208 (while (setq line (pop lines))
3209 (catch 'next
3210 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3211 (setq org-agenda-info
3212 (org-fix-agenda-info (text-properties-at 0 line)))
3213 (princ
3214 (mapconcat 'org-agenda-export-csv-mapper
3215 '(org-category txt type todo tags date time extra
3216 priority-letter priority agenda-day)
3217 ","))
3218 (princ "\n")))))
3220 (defun org-fix-agenda-info (props)
3221 "Make sure all properties on an agenda item have a canonical form.
3222 This ensures the export commands can easily use it."
3223 (let (tmp re)
3224 (when (setq tmp (plist-get props 'tags))
3225 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3226 (when (setq tmp (plist-get props 'date))
3227 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3228 (let ((calendar-date-display-form '(year "-" month "-" day)))
3229 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3231 (setq tmp (calendar-date-string tmp)))
3232 (setq props (plist-put props 'date tmp)))
3233 (when (setq tmp (plist-get props 'day))
3234 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3235 (let ((calendar-date-display-form '(year "-" month "-" day)))
3236 (setq tmp (calendar-date-string tmp)))
3237 (setq props (plist-put props 'day tmp))
3238 (setq props (plist-put props 'agenda-day tmp)))
3239 (when (setq tmp (plist-get props 'txt))
3240 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3241 (plist-put props 'priority-letter (match-string 1 tmp))
3242 (setq tmp (replace-match "" t t tmp)))
3243 (when (and (setq re (plist-get props 'org-todo-regexp))
3244 (setq re (concat "\\`\\.*" re " ?"))
3245 (string-match re tmp))
3246 (plist-put props 'todo (match-string 1 tmp))
3247 (setq tmp (replace-match "" t t tmp)))
3248 (plist-put props 'txt tmp)))
3249 props)
3251 (defun org-agenda-export-csv-mapper (prop)
3252 (let ((res (plist-get org-agenda-info prop)))
3253 (setq res
3254 (cond
3255 ((not res) "")
3256 ((stringp res) res)
3257 (t (prin1-to-string res))))
3258 (while (string-match "," res)
3259 (setq res (replace-match ";" t t res)))
3260 (org-trim res)))
3262 ;;;###autoload
3263 (defun org-store-agenda-views (&rest parameters)
3264 "Store agenda views."
3265 (interactive)
3266 (eval (list 'org-batch-store-agenda-views)))
3268 ;;;###autoload
3269 (defmacro org-batch-store-agenda-views (&rest parameters)
3270 "Run all custom agenda commands that have a file argument."
3271 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3272 (pop-up-frames nil)
3273 (dir default-directory)
3274 (pars (org-make-parameter-alist parameters))
3275 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3276 (save-window-excursion
3277 (while cmds
3278 (setq cmd (pop cmds)
3279 thiscmdkey (car cmd)
3280 thiscmdcmd (cdr cmd)
3281 match (nth 2 thiscmdcmd)
3282 bufname (if org-agenda-sticky
3283 (or (and (stringp match)
3284 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3285 (format "*Org Agenda(%s)*" thiscmdkey))
3286 org-agenda-buffer-name)
3287 cmd-or-set (nth 2 cmd)
3288 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3289 files (nth (if (listp cmd-or-set) 4 5) cmd))
3290 (if (stringp files) (setq files (list files)))
3291 (when files
3292 (org-eval-in-environment (append org-agenda-exporter-settings
3293 opts pars)
3294 (org-agenda nil thiscmdkey))
3295 (set-buffer bufname)
3296 (while files
3297 (org-eval-in-environment (append org-agenda-exporter-settings
3298 opts pars)
3299 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3300 (and (get-buffer bufname)
3301 (kill-buffer bufname)))))))
3303 (defvar org-agenda-current-span nil
3304 "The current span used in the agenda view.") ; local variable in the agenda buffer
3305 (defun org-agenda-mark-header-line (pos)
3306 "Mark the line at POS as an agenda structure header."
3307 (save-excursion
3308 (goto-char pos)
3309 (put-text-property (point-at-bol) (point-at-eol)
3310 'org-agenda-structural-header t)
3311 (when org-agenda-title-append
3312 (put-text-property (point-at-bol) (point-at-eol)
3313 'org-agenda-title-append org-agenda-title-append))))
3315 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3316 (defvar org-agenda-write-buffer-name "Agenda View")
3317 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3318 "Write the current buffer (an agenda view) as a file.
3320 Depending on the extension of the file name, plain text (.txt),
3321 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3322 If the extension is .ics, translate visible agenda into iCalendar
3323 format. If the extension is .org, collect all subtrees
3324 corresponding to the agenda entries and add them in an .org file.
3326 With prefix argument OPEN, open the new file immediately. If
3327 NOSETTINGS is given, do not scope the settings of
3328 `org-agenda-exporter-settings' into the export commands. This is
3329 used when the settings have already been scoped and we do not
3330 wish to overrule other, higher priority settings. If
3331 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3332 the agenda to write."
3333 (interactive "FWrite agenda to file: \nP")
3334 (if (or (not (file-writable-p file))
3335 (and (file-exists-p file)
3336 (if (called-interactively-p 'any)
3337 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3338 (user-error "Cannot write agenda to file %s" file))
3339 (org-let (if nosettings nil org-agenda-exporter-settings)
3340 '(save-excursion
3341 (save-window-excursion
3342 (let ((bs (copy-sequence (buffer-string))) beg content)
3343 (with-temp-buffer
3344 (rename-buffer org-agenda-write-buffer-name t)
3345 (set-buffer-modified-p nil)
3346 (insert bs)
3347 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3348 (run-hooks 'org-agenda-before-write-hook)
3349 (cond
3350 ((bound-and-true-p org-mobile-creating-agendas)
3351 (org-mobile-write-agenda-for-mobile file))
3352 ((string-match "\\.org\\'" file)
3353 (let (content p m message-log-max)
3354 (goto-char (point-min))
3355 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3356 (goto-char p)
3357 (setq m (get-text-property (point) 'org-hd-marker))
3358 (when m
3359 (push (save-excursion
3360 (set-buffer (marker-buffer m))
3361 (goto-char m)
3362 (org-copy-subtree 1 nil t t)
3363 org-subtree-clip)
3364 content)))
3365 (find-file file)
3366 (erase-buffer)
3367 (dolist (s content) (org-paste-subtree 1 s))
3368 (write-file file)
3369 (kill-buffer (current-buffer))
3370 (message "Org file written to %s" file)))
3371 ((string-match "\\.html?\\'" file)
3372 (require 'htmlize)
3373 (set-buffer (htmlize-buffer (current-buffer)))
3374 (when org-agenda-export-html-style
3375 ;; replace <style> section with org-agenda-export-html-style
3376 (goto-char (point-min))
3377 (kill-region (- (search-forward "<style") 6)
3378 (search-forward "</style>"))
3379 (insert org-agenda-export-html-style))
3380 (write-file file)
3381 (kill-buffer (current-buffer))
3382 (message "HTML written to %s" file))
3383 ((string-match "\\.ps\\'" file)
3384 (require 'ps-print)
3385 (ps-print-buffer-with-faces file)
3386 (message "Postscript written to %s" file))
3387 ((string-match "\\.pdf\\'" file)
3388 (require 'ps-print)
3389 (ps-print-buffer-with-faces
3390 (concat (file-name-sans-extension file) ".ps"))
3391 (call-process "ps2pdf" nil nil nil
3392 (expand-file-name
3393 (concat (file-name-sans-extension file) ".ps"))
3394 (expand-file-name file))
3395 (delete-file (concat (file-name-sans-extension file) ".ps"))
3396 (message "PDF written to %s" file))
3397 ((string-match "\\.ics\\'" file)
3398 (require 'ox-icalendar)
3399 (org-icalendar-export-current-agenda (expand-file-name file)))
3401 (let ((bs (buffer-string)))
3402 (find-file file)
3403 (erase-buffer)
3404 (insert bs)
3405 (save-buffer 0)
3406 (kill-buffer (current-buffer))
3407 (message "Plain text written to %s" file))))))))
3408 (set-buffer (or agenda-bufname
3409 (and (called-interactively-p 'any) (buffer-name))
3410 org-agenda-buffer-name)))
3411 (when open (org-open-file file)))
3413 (defun org-agenda-remove-marked-text (property &optional value)
3414 "Delete all text marked with VALUE of PROPERTY.
3415 VALUE defaults to t."
3416 (let (beg)
3417 (setq value (or value t))
3418 (while (setq beg (text-property-any (point-min) (point-max)
3419 property value))
3420 (delete-region
3421 beg (or (next-single-property-change beg property)
3422 (point-max))))))
3424 (defun org-agenda-add-entry-text ()
3425 "Add entry text to agenda lines.
3426 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3427 entry text following headings shown in the agenda.
3428 Drawers will be excluded, also the line with scheduling/deadline info."
3429 (when (and (> org-agenda-add-entry-text-maxlines 0)
3430 (not (bound-and-true-p org-mobile-creating-agendas)))
3431 (let (m txt)
3432 (goto-char (point-min))
3433 (while (not (eobp))
3434 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3435 (beginning-of-line 2)
3436 (setq txt (org-agenda-get-some-entry-text
3437 m org-agenda-add-entry-text-maxlines " > "))
3438 (end-of-line 1)
3439 (if (string-match "\\S-" txt)
3440 (insert "\n" txt)
3441 (or (eobp) (forward-char 1))))))))
3443 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3444 &rest keep)
3445 "Extract entry text from MARKER, at most N-LINES lines.
3446 This will ignore drawers etc, just get the text.
3447 If INDENT is given, prefix every line with this string. If KEEP is
3448 given, it is a list of symbols, defining stuff that should not be
3449 removed from the entry content. Currently only `planning' is allowed here."
3450 (let (txt drawer-re kwd-time-re ind)
3451 (save-excursion
3452 (with-current-buffer (marker-buffer marker)
3453 (if (not (derived-mode-p 'org-mode))
3454 (setq txt "")
3455 (save-excursion
3456 (save-restriction
3457 (widen)
3458 (goto-char marker)
3459 (end-of-line 1)
3460 (setq txt (buffer-substring
3461 (min (1+ (point)) (point-max))
3462 (progn (outline-next-heading) (point)))
3463 drawer-re org-drawer-regexp
3464 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3465 ".*\n?"))
3466 (with-temp-buffer
3467 (insert txt)
3468 (when org-agenda-add-entry-text-descriptive-links
3469 (goto-char (point-min))
3470 (while (org-activate-bracket-links (point-max))
3471 (add-text-properties (match-beginning 0) (match-end 0)
3472 '(face org-link))))
3473 (goto-char (point-min))
3474 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3475 (set-text-properties (match-beginning 0) (match-end 0)
3476 nil))
3477 (goto-char (point-min))
3478 (while (re-search-forward drawer-re nil t)
3479 (delete-region
3480 (match-beginning 0)
3481 (progn (re-search-forward
3482 "^[ \t]*:END:.*\n?" nil 'move)
3483 (point))))
3484 (unless (member 'planning keep)
3485 (goto-char (point-min))
3486 (while (re-search-forward kwd-time-re nil t)
3487 (replace-match "")))
3488 (goto-char (point-min))
3489 (when org-agenda-entry-text-exclude-regexps
3490 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3491 (while (setq re (pop re-list))
3492 (goto-char (point-min))
3493 (while (re-search-forward re nil t)
3494 (replace-match "")))))
3495 (goto-char (point-max))
3496 (skip-chars-backward " \t\n")
3497 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3499 ;; find and remove min common indentation
3500 (goto-char (point-min))
3501 (untabify (point-min) (point-max))
3502 (setq ind (org-get-indentation))
3503 (while (not (eobp))
3504 (unless (looking-at "[ \t]*$")
3505 (setq ind (min ind (org-get-indentation))))
3506 (beginning-of-line 2))
3507 (goto-char (point-min))
3508 (while (not (eobp))
3509 (unless (looking-at "[ \t]*$")
3510 (move-to-column ind)
3511 (delete-region (point-at-bol) (point)))
3512 (beginning-of-line 2))
3514 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3516 (goto-char (point-min))
3517 (when indent
3518 (while (and (not (eobp)) (re-search-forward "^" nil t))
3519 (replace-match indent t t)))
3520 (goto-char (point-min))
3521 (while (looking-at "[ \t]*\n") (replace-match ""))
3522 (goto-char (point-max))
3523 (when (> (org-current-line)
3524 n-lines)
3525 (org-goto-line (1+ n-lines))
3526 (backward-char 1))
3527 (setq txt (buffer-substring (point-min) (point)))))))))
3528 txt))
3530 (defun org-check-for-org-mode ()
3531 "Make sure current buffer is in org-mode. Error if not."
3532 (or (derived-mode-p 'org-mode)
3533 (error "Cannot execute org-mode agenda command on buffer in %s"
3534 major-mode)))
3536 ;;; Agenda prepare and finalize
3538 (defvar org-agenda-multi nil) ; dynamically scoped
3539 (defvar org-agenda-pre-window-conf nil)
3540 (defvar org-agenda-columns-active nil)
3541 (defvar org-agenda-name nil)
3542 (defvar org-agenda-tag-filter nil)
3543 (defvar org-agenda-category-filter nil)
3544 (defvar org-agenda-regexp-filter nil)
3545 (defvar org-agenda-effort-filter nil)
3546 (defvar org-agenda-top-headline-filter nil)
3547 (defvar org-agenda-tag-filter-preset nil
3548 "A preset of the tags filter used for secondary agenda filtering.
3549 This must be a list of strings, each string must be a single tag preceded
3550 by \"+\" or \"-\".
3551 This variable should not be set directly, but agenda custom commands can
3552 bind it in the options section. The preset filter is a global property of
3553 the entire agenda view. In a block agenda, it will not work reliably to
3554 define a filter for one of the individual blocks. You need to set it in
3555 the global options and expect it to be applied to the entire view.")
3557 (defvar org-agenda-category-filter-preset nil
3558 "A preset of the category filter used for secondary agenda filtering.
3559 This must be a list of strings, each string must be a single category
3560 preceded by \"+\" or \"-\".
3561 This variable should not be set directly, but agenda custom commands can
3562 bind it in the options section. The preset filter is a global property of
3563 the entire agenda view. In a block agenda, it will not work reliably to
3564 define a filter for one of the individual blocks. You need to set it in
3565 the global options and expect it to be applied to the entire view.")
3567 (defvar org-agenda-regexp-filter-preset nil
3568 "A preset of the regexp filter used for secondary agenda filtering.
3569 This must be a list of strings, each string must be a single regexp
3570 preceded by \"+\" or \"-\".
3571 This variable should not be set directly, but agenda custom commands can
3572 bind it in the options section. The preset filter is a global property of
3573 the entire agenda view. In a block agenda, it will not work reliably to
3574 define a filter for one of the individual blocks. You need to set it in
3575 the global options and expect it to be applied to the entire view.")
3577 (defvar org-agenda-effort-filter-preset nil
3578 "A preset of the effort condition used for secondary agenda filtering.
3579 This must be a list of strings, each string must be a single regexp
3580 preceded by \"+\" or \"-\".
3581 This variable should not be set directly, but agenda custom commands can
3582 bind it in the options section. The preset filter is a global property of
3583 the entire agenda view. In a block agenda, it will not work reliably to
3584 define a filter for one of the individual blocks. You need to set it in
3585 the global options and expect it to be applied to the entire view.")
3587 (defun org-agenda-use-sticky-p ()
3588 "Return non-nil if an agenda buffer named
3589 `org-agenda-buffer-name' exists and should be shown instead of
3590 generating a new one."
3591 (and
3592 ;; turned off by user
3593 org-agenda-sticky
3594 ;; For multi-agenda buffer already exists
3595 (not org-agenda-multi)
3596 ;; buffer found
3597 (get-buffer org-agenda-buffer-name)
3598 ;; C-u parameter is same as last call
3599 (with-current-buffer (get-buffer org-agenda-buffer-name)
3600 (and
3601 (equal current-prefix-arg
3602 org-agenda-last-prefix-arg)
3603 ;; In case user turned stickiness on, while having existing
3604 ;; Agenda buffer active, don't reuse that buffer, because it
3605 ;; does not have org variables local
3606 org-agenda-this-buffer-is-sticky))))
3608 (defun org-agenda-prepare-window (abuf filter-alist)
3609 "Setup agenda buffer in the window.
3610 ABUF is the buffer for the agenda window.
3611 FILTER-ALIST is an alist of filters we need to apply when
3612 `org-agenda-persistent-filter' is non-nil."
3613 (let* ((awin (get-buffer-window abuf)) wconf)
3614 (cond
3615 ((equal (current-buffer) abuf) nil)
3616 (awin (select-window awin))
3617 ((not (setq wconf (current-window-configuration))))
3618 ((equal org-agenda-window-setup 'current-window)
3619 (pop-to-buffer-same-window abuf))
3620 ((equal org-agenda-window-setup 'other-window)
3621 (org-switch-to-buffer-other-window abuf))
3622 ((equal org-agenda-window-setup 'other-frame)
3623 (switch-to-buffer-other-frame abuf))
3624 ((eq org-agenda-window-setup 'only-window)
3625 (delete-other-windows)
3626 (pop-to-buffer-same-window abuf))
3627 ((equal org-agenda-window-setup 'reorganize-frame)
3628 (delete-other-windows)
3629 (org-switch-to-buffer-other-window abuf)))
3630 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3631 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3632 (setq org-agenda-effort-filter (cdr (assoc 'effort filter-alist)))
3633 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3634 ;; Additional test in case agenda is invoked from within agenda
3635 ;; buffer via elisp link.
3636 (unless (equal (current-buffer) abuf)
3637 (pop-to-buffer-same-window abuf))
3638 (setq org-agenda-pre-window-conf
3639 (or wconf org-agenda-pre-window-conf))))
3641 (defun org-agenda-prepare (&optional name)
3642 (let ((filter-alist (if org-agenda-persistent-filter
3643 (with-current-buffer
3644 (get-buffer-create org-agenda-buffer-name)
3645 (list `(tag . ,org-agenda-tag-filter)
3646 `(re . ,org-agenda-regexp-filter)
3647 `(effort . ,org-agenda-effort-filter)
3648 `(cat . ,org-agenda-category-filter))))))
3649 (if (org-agenda-use-sticky-p)
3650 (progn
3651 (put 'org-agenda-tag-filter :preset-filter nil)
3652 (put 'org-agenda-category-filter :preset-filter nil)
3653 (put 'org-agenda-regexp-filter :preset-filter nil)
3654 ;; Popup existing buffer
3655 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3656 filter-alist)
3657 (message "Sticky Agenda buffer, use `r' to refresh")
3658 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3659 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3660 (setq org-todo-keywords-for-agenda nil)
3661 (put 'org-agenda-tag-filter :preset-filter
3662 org-agenda-tag-filter-preset)
3663 (put 'org-agenda-category-filter :preset-filter
3664 org-agenda-category-filter-preset)
3665 (put 'org-agenda-regexp-filter :preset-filter
3666 org-agenda-regexp-filter-preset)
3667 (put 'org-agenda-effort-filter :preset-filter
3668 org-agenda-effort-filter-preset)
3669 (if org-agenda-multi
3670 (progn
3671 (setq buffer-read-only nil)
3672 (goto-char (point-max))
3673 (unless (or (bobp) org-agenda-compact-blocks
3674 (not org-agenda-block-separator))
3675 (insert "\n"
3676 (if (stringp org-agenda-block-separator)
3677 org-agenda-block-separator
3678 (make-string (window-width) org-agenda-block-separator))
3679 "\n"))
3680 (narrow-to-region (point) (point-max)))
3681 (setq org-done-keywords-for-agenda nil)
3682 ;; Setting any org variables that are in org-agenda-local-vars
3683 ;; list need to be done after the prepare call
3684 (org-agenda-prepare-window
3685 (get-buffer-create org-agenda-buffer-name) filter-alist)
3686 (setq buffer-read-only nil)
3687 (org-agenda-reset-markers)
3688 (let ((inhibit-read-only t)) (erase-buffer))
3689 (org-agenda-mode)
3690 (setq org-agenda-buffer (current-buffer))
3691 (setq org-agenda-contributing-files nil)
3692 (setq org-agenda-columns-active nil)
3693 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3694 (setq org-todo-keywords-for-agenda
3695 (org-uniquify org-todo-keywords-for-agenda))
3696 (setq org-done-keywords-for-agenda
3697 (org-uniquify org-done-keywords-for-agenda))
3698 (setq org-agenda-last-prefix-arg current-prefix-arg)
3699 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3700 (and name (not org-agenda-name)
3701 (setq-local org-agenda-name name)))
3702 (setq buffer-read-only nil))))
3704 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3705 (defun org-agenda-finalize ()
3706 "Finishing touch for the agenda buffer, called just before displaying it."
3707 (unless org-agenda-multi
3708 (save-excursion
3709 (let ((inhibit-read-only t))
3710 (goto-char (point-min))
3711 (save-excursion
3712 (while (org-activate-bracket-links (point-max))
3713 (add-text-properties (match-beginning 0) (match-end 0)
3714 '(face org-link))))
3715 (save-excursion
3716 (while (org-activate-plain-links (point-max))
3717 (add-text-properties (match-beginning 0) (match-end 0)
3718 '(face org-link))))
3719 (unless (eq org-agenda-remove-tags t)
3720 (org-agenda-align-tags))
3721 (unless org-agenda-with-colors
3722 (remove-text-properties (point-min) (point-max) '(face nil)))
3723 (if (and (boundp 'org-agenda-overriding-columns-format)
3724 org-agenda-overriding-columns-format)
3725 (setq-local org-agenda-overriding-columns-format
3726 org-agenda-overriding-columns-format))
3727 (if (and (boundp 'org-agenda-view-columns-initially)
3728 org-agenda-view-columns-initially)
3729 (org-agenda-columns))
3730 (when org-agenda-fontify-priorities
3731 (org-agenda-fontify-priorities))
3732 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3733 (org-agenda-dim-blocked-tasks))
3734 (org-agenda-mark-clocking-task)
3735 (when org-agenda-entry-text-mode
3736 (org-agenda-entry-text-hide)
3737 (org-agenda-entry-text-show))
3738 (if (and (functionp 'org-habit-insert-consistency-graphs)
3739 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3740 (org-habit-insert-consistency-graphs))
3741 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3742 (unless (or (eq org-agenda-show-inherited-tags 'always)
3743 (and (listp org-agenda-show-inherited-tags)
3744 (memq org-agenda-type org-agenda-show-inherited-tags))
3745 (and (eq org-agenda-show-inherited-tags t)
3746 (or (eq org-agenda-use-tag-inheritance t)
3747 (and (listp org-agenda-use-tag-inheritance)
3748 (not (memq org-agenda-type
3749 org-agenda-use-tag-inheritance))))))
3750 (let (mrk)
3751 (save-excursion
3752 (goto-char (point-min))
3753 (while (equal (forward-line) 0)
3754 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3755 (put-text-property (point-at-bol) (point-at-eol)
3756 'tags (org-with-point-at mrk
3757 (delete-dups
3758 (mapcar 'downcase (org-get-tags-at))))))))))
3759 (run-hooks 'org-agenda-finalize-hook)
3760 (when org-agenda-top-headline-filter
3761 (org-agenda-filter-top-headline-apply
3762 org-agenda-top-headline-filter))
3763 (when org-agenda-tag-filter
3764 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3765 (when (get 'org-agenda-tag-filter :preset-filter)
3766 (org-agenda-filter-apply
3767 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3768 (when org-agenda-category-filter
3769 (org-agenda-filter-apply org-agenda-category-filter 'category))
3770 (when (get 'org-agenda-category-filter :preset-filter)
3771 (org-agenda-filter-apply
3772 (get 'org-agenda-category-filter :preset-filter) 'category))
3773 (when org-agenda-regexp-filter
3774 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3775 (when (get 'org-agenda-regexp-filter :preset-filter)
3776 (org-agenda-filter-apply
3777 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3778 (when org-agenda-effort-filter
3779 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3780 (when (get 'org-agenda-effort-filter :preset-filter)
3781 (org-agenda-filter-apply
3782 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3783 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3785 (defun org-agenda-mark-clocking-task ()
3786 "Mark the current clock entry in the agenda if it is present."
3787 ;; We need to widen when `org-agenda-finalize' is called from
3788 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3789 (when (bound-and-true-p org-clock-current-task)
3790 (save-restriction
3791 (widen)
3792 (org-agenda-unmark-clocking-task)
3793 (when (marker-buffer org-clock-hd-marker)
3794 (save-excursion
3795 (goto-char (point-min))
3796 (let (s ov)
3797 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3798 (goto-char s)
3799 (when (equal (org-get-at-bol 'org-hd-marker)
3800 org-clock-hd-marker)
3801 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3802 (overlay-put ov 'type 'org-agenda-clocking)
3803 (overlay-put ov 'face 'org-agenda-clocking)
3804 (overlay-put ov 'help-echo
3805 "The clock is running in this item")))))))))
3807 (defun org-agenda-unmark-clocking-task ()
3808 "Unmark the current clocking task."
3809 (mapc (lambda (o)
3810 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3811 (delete-overlay o)))
3812 (overlays-in (point-min) (point-max))))
3814 (defun org-agenda-fontify-priorities ()
3815 "Make highest priority lines bold, and lowest italic."
3816 (interactive)
3817 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3818 (delete-overlay o)))
3819 (overlays-in (point-min) (point-max)))
3820 (save-excursion
3821 (let (b e p ov h l)
3822 (goto-char (point-min))
3823 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3824 (setq h (or (get-char-property (point) 'org-highest-priority)
3825 org-highest-priority)
3826 l (or (get-char-property (point) 'org-lowest-priority)
3827 org-lowest-priority)
3828 p (string-to-char (match-string 1))
3829 b (match-beginning 0)
3830 e (if (eq org-agenda-fontify-priorities 'cookies)
3831 (match-end 0)
3832 (point-at-eol))
3833 ov (make-overlay b e))
3834 (overlay-put
3835 ov 'face
3836 (let ((special-face
3837 (cond ((org-face-from-face-or-color
3838 'priority nil
3839 (cdr (assoc p org-priority-faces))))
3840 ((and (listp org-agenda-fontify-priorities)
3841 (org-face-from-face-or-color
3842 'priority nil
3843 (cdr (assoc p org-agenda-fontify-priorities)))))
3844 ((equal p l) 'italic)
3845 ((equal p h) 'bold))))
3846 (if special-face (list special-face 'org-priority) 'org-priority)))
3847 (overlay-put ov 'org-type 'org-priority)))))
3849 (defvar org-depend-tag-blocked)
3851 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3852 "Dim currently blocked TODOs in the agenda display.
3853 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3854 dimming them."
3855 (interactive "P")
3856 (when (called-interactively-p 'interactive)
3857 (message "Dim or hide blocked tasks..."))
3858 (dolist (o (overlays-in (point-min) (point-max)))
3859 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3860 (delete-overlay o)))
3861 (save-excursion
3862 (let ((inhibit-read-only t)
3863 (org-depend-tag-blocked nil)
3864 org-blocked-by-checkboxes)
3865 (goto-char (point-min))
3866 (while (let ((pos (text-property-not-all
3867 (point) (point-max) 'todo-state nil)))
3868 (when pos (goto-char pos)))
3869 (setq org-blocked-by-checkboxes nil)
3870 (let ((marker (org-get-at-bol 'org-hd-marker)))
3871 (when (and (markerp marker)
3872 (with-current-buffer (marker-buffer marker)
3873 (save-excursion (goto-char marker)
3874 (org-entry-blocked-p))))
3875 ;; Entries blocked by checkboxes cannot be made invisible.
3876 ;; See `org-agenda-dim-blocked-tasks' for details.
3877 (let* ((really-invisible
3878 (and (not org-blocked-by-checkboxes)
3879 (or invisible (eq org-agenda-dim-blocked-tasks
3880 'invisible))))
3881 (ov (make-overlay (if really-invisible (line-end-position 0)
3882 (line-beginning-position))
3883 (line-end-position))))
3884 (if really-invisible (overlay-put ov 'invisible t)
3885 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3886 (overlay-put ov 'org-type 'org-blocked-todo))))
3887 (forward-line))))
3888 (when (called-interactively-p 'interactive)
3889 (message "Dim or hide blocked tasks...done")))
3891 (defvar org-agenda-skip-function nil
3892 "Function to be called at each match during agenda construction.
3893 If this function returns nil, the current match should not be skipped.
3894 Otherwise, the function must return a position from where the search
3895 should be continued.
3896 This may also be a Lisp form, it will be evaluated.
3897 Never set this variable using `setq' or so, because then it will apply
3898 to all future agenda commands. If you do want a global skipping condition,
3899 use the option `org-agenda-skip-function-global' instead.
3900 The correct usage for `org-agenda-skip-function' is to bind it with
3901 `let' to scope it dynamically into the agenda-constructing command.
3902 A good way to set it is through options in `org-agenda-custom-commands'.")
3904 (defun org-agenda-skip ()
3905 "Throw to `:skip' in places that should be skipped.
3906 Also moves point to the end of the skipped region, so that search can
3907 continue from there."
3908 (let ((p (point-at-bol)) to)
3909 (when (or
3910 (save-excursion (goto-char p) (looking-at comment-start-skip))
3911 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3912 (get-text-property p :org-archived)
3913 (org-end-of-subtree t))
3914 (and org-agenda-skip-comment-trees
3915 (get-text-property p :org-comment)
3916 (org-end-of-subtree t))
3917 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3918 (org-agenda-skip-eval org-agenda-skip-function)))
3919 (goto-char to))
3920 (org-in-src-block-p t))
3921 (throw :skip t))))
3923 (defun org-agenda-skip-eval (form)
3924 "If FORM is a function or a list, call (or eval) it and return the result.
3925 `save-excursion' and `save-match-data' are wrapped around the call, so point
3926 and match data are returned to the previous state no matter what these
3927 functions do."
3928 (let (fp)
3929 (and form
3930 (or (setq fp (functionp form))
3931 (consp form))
3932 (save-excursion
3933 (save-match-data
3934 (if fp
3935 (funcall form)
3936 (eval form)))))))
3938 (defvar org-agenda-markers nil
3939 "List of all currently active markers created by `org-agenda'.")
3940 (defvar org-agenda-last-marker-time (float-time)
3941 "Creation time of the last agenda marker.")
3943 (defun org-agenda-new-marker (&optional pos)
3944 "Return a new agenda marker.
3945 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3946 these markers and resets them when they are no longer in use."
3947 (let ((m (copy-marker (or pos (point)) t)))
3948 (setq org-agenda-last-marker-time (float-time))
3949 (if org-agenda-buffer
3950 (with-current-buffer org-agenda-buffer
3951 (push m org-agenda-markers))
3952 (push m org-agenda-markers))
3955 (defun org-agenda-reset-markers ()
3956 "Reset markers created by `org-agenda'."
3957 (while org-agenda-markers
3958 (move-marker (pop org-agenda-markers) nil)))
3960 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3961 "Save relative positions of markers in region.
3962 This check for agenda markers in all agenda buffers currently active."
3963 (dolist (buf (buffer-list))
3964 (with-current-buffer buf
3965 (when (eq major-mode 'org-agenda-mode)
3966 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3967 org-agenda-markers)))))
3969 ;;; Entry text mode
3971 (defun org-agenda-entry-text-show-here ()
3972 "Add some text from the entry as context to the current line."
3973 (let (m txt o)
3974 (setq m (org-get-at-bol 'org-hd-marker))
3975 (unless (marker-buffer m)
3976 (error "No marker points to an entry here"))
3977 (setq txt (concat "\n" (org-no-properties
3978 (org-agenda-get-some-entry-text
3979 m org-agenda-entry-text-maxlines
3980 org-agenda-entry-text-leaders))))
3981 (when (string-match "\\S-" txt)
3982 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3983 (overlay-put o 'evaporate t)
3984 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3985 (overlay-put o 'after-string txt))))
3987 (defun org-agenda-entry-text-show ()
3988 "Add entry context for all agenda lines."
3989 (interactive)
3990 (save-excursion
3991 (goto-char (point-max))
3992 (beginning-of-line 1)
3993 (while (not (bobp))
3994 (when (org-get-at-bol 'org-hd-marker)
3995 (org-agenda-entry-text-show-here))
3996 (beginning-of-line 0))))
3998 (defun org-agenda-entry-text-hide ()
3999 "Remove any shown entry context."
4000 (delq nil
4001 (mapcar (lambda (o)
4002 (if (eq (overlay-get o 'org-overlay-type)
4003 'agenda-entry-content)
4004 (progn (delete-overlay o) t)))
4005 (overlays-in (point-min) (point-max)))))
4007 (defun org-agenda-get-day-face (date)
4008 "Return the face DATE should be displayed with."
4009 (cond ((and (functionp org-agenda-day-face-function)
4010 (funcall org-agenda-day-face-function date)))
4011 ((org-agenda-today-p date) 'org-agenda-date-today)
4012 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4013 'org-agenda-date-weekend)
4014 (t 'org-agenda-date)))
4016 ;;; Agenda timeline
4018 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4019 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4021 (defun org-timeline (&optional dotodo)
4022 "Show a time-sorted view of the entries in the current org file.
4023 Only entries with a time stamp of today or later will be listed. With
4024 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4025 under the current date.
4026 If the buffer contains an active region, only check the region for
4027 dates."
4028 (interactive "P")
4029 (let* ((dopast t)
4030 (org-agenda-show-log-scoped org-agenda-show-log)
4031 (org-agenda-show-log org-agenda-show-log-scoped)
4032 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4033 (current-buffer))))
4034 (date (calendar-current-date))
4035 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4036 (end (if (org-region-active-p) (region-end) (point-max)))
4037 (day-numbers (org-get-all-dates
4038 beg end 'no-ranges
4039 t org-agenda-show-log-scoped ; always include today
4040 org-timeline-show-empty-dates))
4041 (org-deadline-warning-days 0)
4042 (org-agenda-only-exact-dates t)
4043 (today (org-today))
4044 (past t)
4045 args
4046 s e rtn d emptyp)
4047 (setq org-agenda-redo-command
4048 (list 'let
4049 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4050 (list 'org-switch-to-buffer-other-window (current-buffer))
4051 (list 'org-timeline (list 'quote dotodo))))
4052 (put 'org-agenda-redo-command 'org-lprops nil)
4053 (if (not dopast)
4054 ;; Remove past dates from the list of dates.
4055 (setq day-numbers (delq nil (mapcar (lambda(x)
4056 (if (>= x today) x nil))
4057 day-numbers))))
4058 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4059 (org-compile-prefix-format 'timeline)
4060 (org-set-sorting-strategy 'timeline)
4061 (if org-agenda-show-log-scoped (push :closed args))
4062 (push :timestamp args)
4063 (push :deadline args)
4064 (push :scheduled args)
4065 (push :sexp args)
4066 (if dotodo (push :todo args))
4067 (insert "Timeline of file " entry "\n")
4068 (add-text-properties (point-min) (point)
4069 (list 'face 'org-agenda-structure))
4070 (org-agenda-mark-header-line (point-min))
4071 (while (setq d (pop day-numbers))
4072 (if (and (listp d) (eq (car d) :omitted))
4073 (progn
4074 (setq s (point))
4075 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4076 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4077 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4078 (if (and (>= d today)
4079 dopast
4080 past)
4081 (progn
4082 (setq past nil)
4083 (insert (make-string 79 ?-) "\n")))
4084 (setq date (calendar-gregorian-from-absolute d))
4085 (setq s (point))
4086 (setq rtn (and (not emptyp)
4087 (apply 'org-agenda-get-day-entries entry
4088 date args)))
4089 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4090 (progn
4091 (insert
4092 (if (stringp org-agenda-format-date)
4093 (format-time-string org-agenda-format-date
4094 (org-time-from-absolute date))
4095 (funcall org-agenda-format-date date))
4096 "\n")
4097 (put-text-property s (1- (point)) 'face
4098 (org-agenda-get-day-face date))
4099 (put-text-property s (1- (point)) 'org-date-line t)
4100 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4101 (if (equal d today)
4102 (put-text-property s (1- (point)) 'org-today t))
4103 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4104 (put-text-property s (1- (point)) 'day d)))))
4105 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4106 (point-min)))
4107 (add-text-properties
4108 (point-min) (point-max)
4109 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4110 (org-agenda-finalize)
4111 (setq buffer-read-only t)))
4113 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4114 "Return a list of all relevant day numbers from BEG to END buffer positions.
4115 If NO-RANGES is non-nil, include only the start and end dates of a range,
4116 not every single day in the range. If FORCE-TODAY is non-nil, make
4117 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4118 inactive time stamps (those in square brackets) are included.
4119 When EMPTY is non-nil, also include days without any entries."
4120 (let ((re (concat
4121 (if pre-re pre-re "")
4122 (if inactive org-ts-regexp-both org-ts-regexp)))
4123 dates dates1 date day day1 day2 ts1 ts2 pos)
4124 (if force-today
4125 (setq dates (list (org-today))))
4126 (save-excursion
4127 (goto-char beg)
4128 (while (re-search-forward re end t)
4129 (setq day (time-to-days (org-time-string-to-time
4130 (substring (match-string 1) 0 10)
4131 (current-buffer) (match-beginning 0))))
4132 (or (memq day dates) (push day dates)))
4133 (unless no-ranges
4134 (goto-char beg)
4135 (while (re-search-forward org-tr-regexp end t)
4136 (setq pos (match-beginning 0))
4137 (setq ts1 (substring (match-string 1) 0 10)
4138 ts2 (substring (match-string 2) 0 10)
4139 day1 (time-to-days (org-time-string-to-time
4140 ts1 (current-buffer) pos))
4141 day2 (time-to-days (org-time-string-to-time
4142 ts2 (current-buffer) pos)))
4143 (while (< (setq day1 (1+ day1)) day2)
4144 (or (memq day1 dates) (push day1 dates)))))
4145 (setq dates (sort dates '<))
4146 (when empty
4147 (while (setq day (pop dates))
4148 (setq day2 (car dates))
4149 (push day dates1)
4150 (when (and day2 empty)
4151 (if (or (eq empty t)
4152 (and (numberp empty) (<= (- day2 day) empty)))
4153 (while (< (setq day (1+ day)) day2)
4154 (push (list day) dates1))
4155 (push (cons :omitted (- day2 day)) dates1))))
4156 (setq dates (nreverse dates1)))
4157 dates)))
4159 ;;; Agenda Daily/Weekly
4161 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4162 "Start day for the agenda view.
4163 Custom commands can set this variable in the options section.
4164 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4165 input allowed when reading a date through the Org calendar.
4166 See the docstring of `org-read-date' for details.")
4167 (defvar org-starting-day nil) ; local variable in the agenda buffer
4168 (defvar org-arg-loc nil) ; local variable
4170 (defvar org-agenda-buffer-tmp-name nil)
4171 ;;;###autoload
4172 (defun org-agenda-list (&optional arg start-day span with-hour)
4173 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4174 The view will be for the current day or week, but from the overview buffer
4175 you will be able to go to other days/weeks.
4177 With a numeric prefix argument in an interactive call, the agenda will
4178 span ARG days. Lisp programs should instead specify SPAN to change
4179 the number of days. SPAN defaults to `org-agenda-span'.
4181 START-DAY defaults to TODAY, or to the most recent match for the weekday
4182 given in `org-agenda-start-on-weekday'.
4184 When WITH-HOUR is non-nil, only include scheduled and deadline
4185 items if they have an hour specification like [h]h:mm."
4186 (interactive "P")
4187 (if org-agenda-overriding-arguments
4188 (setq arg (car org-agenda-overriding-arguments)
4189 start-day (nth 1 org-agenda-overriding-arguments)
4190 span (nth 2 org-agenda-overriding-arguments)))
4191 (if (and (integerp arg) (> arg 0))
4192 (setq span arg arg nil))
4193 (catch 'exit
4194 (setq org-agenda-buffer-name
4195 (or org-agenda-buffer-tmp-name
4196 (if org-agenda-sticky
4197 (cond ((and org-keys (stringp org-match))
4198 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4199 (org-keys
4200 (format "*Org Agenda(%s)*" org-keys))
4201 (t "*Org Agenda(a)*")))
4202 org-agenda-buffer-name))
4203 (org-agenda-prepare "Day/Week")
4204 (setq start-day (or start-day org-agenda-start-day))
4205 (if (stringp start-day)
4206 ;; Convert to an absolute day number
4207 (setq start-day (time-to-days (org-read-date nil t start-day))))
4208 (org-compile-prefix-format 'agenda)
4209 (org-set-sorting-strategy 'agenda)
4210 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4211 (today (org-today))
4212 (sd (or start-day today))
4213 (ndays (org-agenda-span-to-ndays span sd))
4214 (org-agenda-start-on-weekday
4215 (if (or (eq ndays 7) (eq ndays 14))
4216 org-agenda-start-on-weekday))
4217 (thefiles (org-agenda-files nil 'ifmode))
4218 (files thefiles)
4219 (start (if (or (null org-agenda-start-on-weekday)
4220 (< ndays 7))
4222 (let* ((nt (calendar-day-of-week
4223 (calendar-gregorian-from-absolute sd)))
4224 (n1 org-agenda-start-on-weekday)
4225 (d (- nt n1)))
4226 (- sd (+ (if (< d 0) 7 0) d)))))
4227 (day-numbers (list start))
4228 (day-cnt 0)
4229 (inhibit-redisplay (not debug-on-error))
4230 (org-agenda-show-log-scoped org-agenda-show-log)
4231 s e rtn rtnall file date d start-pos end-pos todayp
4232 clocktable-start clocktable-end filter)
4233 (setq org-agenda-redo-command
4234 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4235 (dotimes (n (1- ndays))
4236 (push (1+ (car day-numbers)) day-numbers))
4237 (setq day-numbers (nreverse day-numbers))
4238 (setq clocktable-start (car day-numbers)
4239 clocktable-end (1+ (or (org-last day-numbers) 0)))
4240 (setq-local org-starting-day (car day-numbers))
4241 (setq-local org-arg-loc arg)
4242 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4243 (unless org-agenda-compact-blocks
4244 (let* ((d1 (car day-numbers))
4245 (d2 (org-last day-numbers))
4246 (w1 (org-days-to-iso-week d1))
4247 (w2 (org-days-to-iso-week d2)))
4248 (setq s (point))
4249 (if org-agenda-overriding-header
4250 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4251 nil 'face 'org-agenda-structure) "\n")
4252 (insert (org-agenda-span-name span)
4253 "-agenda"
4254 (if (< (- d2 d1) 350)
4255 (if (= w1 w2)
4256 (format " (W%02d)" w1)
4257 (format " (W%02d-W%02d)" w1 w2))
4259 ":\n")))
4260 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4261 'org-date-line t))
4262 (org-agenda-mark-header-line s))
4263 (while (setq d (pop day-numbers))
4264 (setq date (calendar-gregorian-from-absolute d)
4265 s (point))
4266 (if (or (setq todayp (= d today))
4267 (and (not start-pos) (= d sd)))
4268 (setq start-pos (point))
4269 (if (and start-pos (not end-pos))
4270 (setq end-pos (point))))
4271 (setq files thefiles
4272 rtnall nil)
4273 (while (setq file (pop files))
4274 (catch 'nextfile
4275 (org-check-agenda-file file)
4276 (let ((org-agenda-entry-types org-agenda-entry-types))
4277 ;; Starred types override non-starred equivalents
4278 (when (member :deadline* org-agenda-entry-types)
4279 (setq org-agenda-entry-types
4280 (delq :deadline org-agenda-entry-types)))
4281 (when (member :scheduled* org-agenda-entry-types)
4282 (setq org-agenda-entry-types
4283 (delq :scheduled org-agenda-entry-types)))
4284 ;; Honor with-hour
4285 (when with-hour
4286 (when (member :deadline org-agenda-entry-types)
4287 (setq org-agenda-entry-types
4288 (delq :deadline org-agenda-entry-types))
4289 (push :deadline* org-agenda-entry-types))
4290 (when (member :scheduled org-agenda-entry-types)
4291 (setq org-agenda-entry-types
4292 (delq :scheduled org-agenda-entry-types))
4293 (push :scheduled* org-agenda-entry-types)))
4294 (unless org-agenda-include-deadlines
4295 (setq org-agenda-entry-types
4296 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4297 (cond
4298 ((memq org-agenda-show-log-scoped '(only clockcheck))
4299 (setq rtn (org-agenda-get-day-entries
4300 file date :closed)))
4301 (org-agenda-show-log-scoped
4302 (setq rtn (apply 'org-agenda-get-day-entries
4303 file date
4304 (append '(:closed) org-agenda-entry-types))))
4306 (setq rtn (apply 'org-agenda-get-day-entries
4307 file date
4308 org-agenda-entry-types)))))
4309 (setq rtnall (append rtnall rtn)))) ;; all entries
4310 (if org-agenda-include-diary
4311 (let ((org-agenda-search-headline-for-time t))
4312 (require 'diary-lib)
4313 (setq rtn (org-get-entries-from-diary date))
4314 (setq rtnall (append rtnall rtn))))
4315 (if (or rtnall org-agenda-show-all-dates)
4316 (progn
4317 (setq day-cnt (1+ day-cnt))
4318 (insert
4319 (if (stringp org-agenda-format-date)
4320 (format-time-string org-agenda-format-date
4321 (org-time-from-absolute date))
4322 (funcall org-agenda-format-date date))
4323 "\n")
4324 (put-text-property s (1- (point)) 'face
4325 (org-agenda-get-day-face date))
4326 (put-text-property s (1- (point)) 'org-date-line t)
4327 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4328 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4329 (when todayp
4330 (put-text-property s (1- (point)) 'org-today t))
4331 (setq rtnall
4332 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4333 (if rtnall (insert ;; all entries
4334 (org-agenda-finalize-entries rtnall 'agenda)
4335 "\n"))
4336 (put-text-property s (1- (point)) 'day d)
4337 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4338 (when (and org-agenda-clockreport-mode clocktable-start)
4339 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4340 ;; the above line is to ensure the restricted range!
4341 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4342 tbl)
4343 (setq p (org-plist-delete p :block))
4344 (setq p (plist-put p :tstart clocktable-start))
4345 (setq p (plist-put p :tend clocktable-end))
4346 (setq p (plist-put p :scope 'agenda))
4347 (setq tbl (apply 'org-clock-get-clocktable p))
4348 (insert tbl)))
4349 (goto-char (point-min))
4350 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4351 (unless (and (pos-visible-in-window-p (point-min))
4352 (pos-visible-in-window-p (point-max)))
4353 (goto-char (1- (point-max)))
4354 (recenter -1)
4355 (if (not (pos-visible-in-window-p (or start-pos 1)))
4356 (progn
4357 (goto-char (or start-pos 1))
4358 (recenter 1))))
4359 (goto-char (or start-pos 1))
4360 (add-text-properties (point-min) (point-max)
4361 `(org-agenda-type agenda
4362 org-last-args (,arg ,start-day ,span)
4363 org-redo-cmd ,org-agenda-redo-command
4364 org-series-cmd ,org-cmd))
4365 (if (eq org-agenda-show-log-scoped 'clockcheck)
4366 (org-agenda-show-clocking-issues))
4367 (org-agenda-finalize)
4368 (setq buffer-read-only t)
4369 (message ""))))
4371 (defun org-agenda-ndays-to-span (n)
4372 "Return a span symbol for a span of N days, or N if none matches."
4373 (cond ((symbolp n) n)
4374 ((= n 1) 'day)
4375 ((= n 7) 'week)
4376 ((= n 14) 'fortnight)
4377 (t n)))
4379 (defun org-agenda-span-to-ndays (span &optional start-day)
4380 "Return ndays from SPAN, possibly starting at START-DAY.
4381 START-DAY is an absolute time value."
4382 (cond ((numberp span) span)
4383 ((eq span 'day) 1)
4384 ((eq span 'week) 7)
4385 ((eq span 'fortnight) 14)
4386 ((eq span 'month)
4387 (let ((date (calendar-gregorian-from-absolute start-day)))
4388 (calendar-last-day-of-month (car date) (cl-caddr date))))
4389 ((eq span 'year)
4390 (let ((date (calendar-gregorian-from-absolute start-day)))
4391 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4393 (defun org-agenda-span-name (span)
4394 "Return a SPAN name."
4395 (if (null span)
4397 (if (symbolp span)
4398 (capitalize (symbol-name span))
4399 (format "%d days" span))))
4401 ;;; Agenda word search
4403 (defvar org-agenda-search-history nil)
4405 (defvar org-search-syntax-table nil
4406 "Special syntax table for org-mode search.
4407 In this table, we have single quotes not as word constituents, to
4408 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4410 (defvar org-mode-syntax-table) ; From org.el
4411 (defun org-search-syntax-table ()
4412 (unless org-search-syntax-table
4413 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4414 (modify-syntax-entry ?' "." org-search-syntax-table)
4415 (modify-syntax-entry ?` "." org-search-syntax-table))
4416 org-search-syntax-table)
4418 (defvar org-agenda-last-search-view-search-was-boolean nil)
4420 ;;;###autoload
4421 (defun org-search-view (&optional todo-only string edit-at)
4422 "Show all entries that contain a phrase or words or regular expressions.
4424 With optional prefix argument TODO-ONLY, only consider entries that are
4425 TODO entries. The argument STRING can be used to pass a default search
4426 string into this function. If EDIT-AT is non-nil, it means that the
4427 user should get a chance to edit this string, with cursor at position
4428 EDIT-AT.
4430 The search string can be viewed either as a phrase that should be found as
4431 is, or it can be broken into a number of snippets, each of which must match
4432 in a Boolean way to select an entry. The default depends on the variable
4433 `org-agenda-search-view-always-boolean'.
4434 Even if this is turned off (the default) you can always switch to
4435 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4437 The default is a direct search of the whole phrase, where each space in
4438 the search string can expand to an arbitrary amount of whitespace,
4439 including newlines.
4441 If using a Boolean search, the search string is split on whitespace and
4442 each snippet is searched separately, with logical AND to select an entry.
4443 Words prefixed with a minus must *not* occur in the entry. Words without
4444 a prefix or prefixed with a plus must occur in the entry. Matching is
4445 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4446 match whole words, not parts of a word) if
4447 `org-agenda-search-view-force-full-words' is set (default is nil).
4449 Boolean search snippets enclosed by curly braces are interpreted as
4450 regular expressions that must or (when preceded with \"-\") must not
4451 match in the entry. Snippets enclosed into double quotes will be taken
4452 as a whole, to include whitespace.
4454 - If the search string starts with an asterisk, search only in headlines.
4455 - If (possibly after the leading star) the search string starts with an
4456 exclamation mark, this also means to look at TODO entries only, an effect
4457 that can also be achieved with a prefix argument.
4458 - If (possibly after star and exclamation mark) the search string starts
4459 with a colon, this will mean that the (non-regexp) snippets of the
4460 Boolean search must match as full words.
4462 This command searches the agenda files, and in addition the files listed
4463 in `org-agenda-text-search-extra-files'."
4464 (interactive "P")
4465 (if org-agenda-overriding-arguments
4466 (setq todo-only (car org-agenda-overriding-arguments)
4467 string (nth 1 org-agenda-overriding-arguments)
4468 edit-at (nth 2 org-agenda-overriding-arguments)))
4469 (let* ((props (list 'face nil
4470 'done-face 'org-agenda-done
4471 'org-not-done-regexp org-not-done-regexp
4472 'org-todo-regexp org-todo-regexp
4473 'org-complex-heading-regexp org-complex-heading-regexp
4474 'mouse-face 'highlight
4475 'help-echo (format "mouse-2 or RET jump to location")))
4476 (full-words org-agenda-search-view-force-full-words)
4477 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4478 regexp rtn rtnall files file pos inherited-tags
4479 marker category level tags c neg re boolean
4480 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4481 (unless (and (not edit-at)
4482 (stringp string)
4483 (string-match "\\S-" string))
4484 (setq string (read-string
4485 (if org-agenda-search-view-always-boolean
4486 "[+-]Word/{Regexp} ...: "
4487 "Phrase or [+-]Word/{Regexp} ...: ")
4488 (cond
4489 ((integerp edit-at) (cons string edit-at))
4490 (edit-at string))
4491 'org-agenda-search-history)))
4492 (catch 'exit
4493 (if org-agenda-sticky
4494 (setq org-agenda-buffer-name
4495 (if (stringp string)
4496 (format "*Org Agenda(%s:%s)*"
4497 (or org-keys (or (and todo-only "S") "s")) string)
4498 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4499 (org-agenda-prepare "SEARCH")
4500 (org-compile-prefix-format 'search)
4501 (org-set-sorting-strategy 'search)
4502 (setq org-agenda-redo-command
4503 (list 'org-search-view (if todo-only t nil)
4504 (list 'if 'current-prefix-arg nil string)))
4505 (setq org-agenda-query-string string)
4506 (if (equal (string-to-char string) ?*)
4507 (setq hdl-only t
4508 words (substring string 1))
4509 (setq words string))
4510 (when (equal (string-to-char words) ?!)
4511 (setq todo-only t
4512 words (substring words 1)))
4513 (when (equal (string-to-char words) ?:)
4514 (setq full-words t
4515 words (substring words 1)))
4516 (if (or org-agenda-search-view-always-boolean
4517 (member (string-to-char words) '(?- ?+ ?\{)))
4518 (setq boolean t))
4519 (setq words (org-split-string words))
4520 (let (www w)
4521 (while (setq w (pop words))
4522 (while (and (string-match "\\\\\\'" w) words)
4523 (setq w (concat (substring w 0 -1) " " (pop words))))
4524 (push w www))
4525 (setq words (nreverse www) www nil)
4526 (while (setq w (pop words))
4527 (when (and (string-match "\\`[-+]?{" w)
4528 (not (string-match "}\\'" w)))
4529 (while (and words (not (string-match "}\\'" (car words))))
4530 (setq w (concat w " " (pop words))))
4531 (setq w (concat w " " (pop words))))
4532 (push w www))
4533 (setq words (nreverse www)))
4534 (setq org-agenda-last-search-view-search-was-boolean boolean)
4535 (when boolean
4536 (let (wds w)
4537 (while (setq w (pop words))
4538 (if (or (equal (substring w 0 1) "\"")
4539 (and (> (length w) 1)
4540 (member (substring w 0 1) '("+" "-"))
4541 (equal (substring w 1 2) "\"")))
4542 (while (and words (not (equal (substring w -1) "\"")))
4543 (setq w (concat w " " (pop words)))))
4544 (and (string-match "\\`\\([-+]?\\)\"" w)
4545 (setq w (replace-match "\\1" nil nil w)))
4546 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4547 (push w wds))
4548 (setq words (nreverse wds))))
4549 (if boolean
4550 (mapc (lambda (w)
4551 (setq c (string-to-char w))
4552 (if (equal c ?-)
4553 (setq neg t w (substring w 1))
4554 (if (equal c ?+)
4555 (setq neg nil w (substring w 1))
4556 (setq neg nil)))
4557 (if (string-match "\\`{.*}\\'" w)
4558 (setq re (substring w 1 -1))
4559 (if full-words
4560 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4561 (setq re (regexp-quote (downcase w)))))
4562 (if neg (push re regexps-) (push re regexps+)))
4563 words)
4564 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4565 regexps+))
4566 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4567 (if (not regexps+)
4568 (setq regexp org-outline-regexp-bol)
4569 (setq regexp (pop regexps+))
4570 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4571 regexp))))
4572 (setq files (org-agenda-files nil 'ifmode))
4573 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4574 (pop org-agenda-text-search-extra-files)
4575 (setq files (org-add-archive-files files)))
4576 (setq files (append files org-agenda-text-search-extra-files)
4577 rtnall nil)
4578 (while (setq file (pop files))
4579 (setq ee nil)
4580 (catch 'nextfile
4581 (org-check-agenda-file file)
4582 (setq buffer (if (file-exists-p file)
4583 (org-get-agenda-file-buffer file)
4584 (error "No such file %s" file)))
4585 (if (not buffer)
4586 ;; If file does not exist, make sure an error message is sent
4587 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4588 file))))
4589 (with-current-buffer buffer
4590 (with-syntax-table (org-search-syntax-table)
4591 (unless (derived-mode-p 'org-mode)
4592 (error "Agenda file %s is not in `org-mode'" file))
4593 (let ((case-fold-search t))
4594 (save-excursion
4595 (save-restriction
4596 (if (eq buffer org-agenda-restrict)
4597 (narrow-to-region org-agenda-restrict-begin
4598 org-agenda-restrict-end)
4599 (widen))
4600 (goto-char (point-min))
4601 (unless (or (org-at-heading-p)
4602 (outline-next-heading))
4603 (throw 'nextfile t))
4604 (goto-char (max (point-min) (1- (point))))
4605 (while (re-search-forward regexp nil t)
4606 (org-back-to-heading t)
4607 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4608 (> (org-reduced-level (org-outline-level))
4609 org-agenda-search-view-max-outline-level)
4610 (forward-line -1)
4611 (org-back-to-heading t)))
4612 (skip-chars-forward "* ")
4613 (setq beg (point-at-bol)
4614 beg1 (point)
4615 end (progn
4616 (outline-next-heading)
4617 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4618 (> (org-reduced-level (org-outline-level))
4619 org-agenda-search-view-max-outline-level)
4620 (forward-line 1)
4621 (outline-next-heading)))
4622 (point)))
4624 (catch :skip
4625 (goto-char beg)
4626 (org-agenda-skip)
4627 (setq str (buffer-substring-no-properties
4628 (point-at-bol)
4629 (if hdl-only (point-at-eol) end)))
4630 (mapc (lambda (wr) (when (string-match wr str)
4631 (goto-char (1- end))
4632 (throw :skip t)))
4633 regexps-)
4634 (mapc (lambda (wr) (unless (string-match wr str)
4635 (goto-char (1- end))
4636 (throw :skip t)))
4637 (if todo-only
4638 (cons (concat "^\\*+[ \t]+"
4639 org-not-done-regexp)
4640 regexps+)
4641 regexps+))
4642 (goto-char beg)
4643 (setq marker (org-agenda-new-marker (point))
4644 category (org-get-category)
4645 level (make-string (org-reduced-level (org-outline-level)) ? )
4646 inherited-tags
4647 (or (eq org-agenda-show-inherited-tags 'always)
4648 (and (listp org-agenda-show-inherited-tags)
4649 (memq 'todo org-agenda-show-inherited-tags))
4650 (and (eq org-agenda-show-inherited-tags t)
4651 (or (eq org-agenda-use-tag-inheritance t)
4652 (memq 'todo org-agenda-use-tag-inheritance))))
4653 tags (org-get-tags-at nil (not inherited-tags))
4654 txt (org-agenda-format-item
4656 (buffer-substring-no-properties
4657 beg1 (point-at-eol))
4658 level category tags t))
4659 (org-add-props txt props
4660 'org-marker marker 'org-hd-marker marker
4661 'org-todo-regexp org-todo-regexp
4662 'level level
4663 'org-complex-heading-regexp org-complex-heading-regexp
4664 'priority 1000
4665 'type "search")
4666 (push txt ee)
4667 (goto-char (1- end))))))))))
4668 (setq rtn (nreverse ee))
4669 (setq rtnall (append rtnall rtn)))
4670 (if org-agenda-overriding-header
4671 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4672 nil 'face 'org-agenda-structure) "\n")
4673 (insert "Search words: ")
4674 (add-text-properties (point-min) (1- (point))
4675 (list 'face 'org-agenda-structure))
4676 (setq pos (point))
4677 (insert string "\n")
4678 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4679 (setq pos (point))
4680 (unless org-agenda-multi
4681 (insert (substitute-command-keys "\
4682 Press `\\[org-agenda-manipulate-query-add]', \
4683 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4684 `\\[org-agenda-manipulate-query-add-re]', \
4685 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4686 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4687 (add-text-properties pos (1- (point))
4688 (list 'face 'org-agenda-structure))))
4689 (org-agenda-mark-header-line (point-min))
4690 (when rtnall
4691 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4692 (goto-char (point-min))
4693 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4694 (add-text-properties (point-min) (point-max)
4695 `(org-agenda-type search
4696 org-last-args (,todo-only ,string ,edit-at)
4697 org-redo-cmd ,org-agenda-redo-command
4698 org-series-cmd ,org-cmd))
4699 (org-agenda-finalize)
4700 (setq buffer-read-only t))))
4702 ;;; Agenda TODO list
4704 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4705 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4706 (concat
4707 (if (or (equal keywords "ALL") (not keywords))
4708 (propertize "ALL" 'face 'warning)
4709 (mapconcat
4710 (lambda (kw)
4711 (propertize kw 'face (org-get-todo-face kw)))
4712 (org-split-string keywords "|")
4713 "|"))
4714 "\n"))
4716 (defvar org-select-this-todo-keyword nil)
4717 (defvar org-last-arg nil)
4719 ;;;###autoload
4720 (defun org-todo-list (&optional arg)
4721 "Show all (not done) TODO entries from all agenda file in a single list.
4722 The prefix arg can be used to select a specific TODO keyword and limit
4723 the list to these. When using \\[universal-argument], you will be prompted
4724 for a keyword. A numeric prefix directly selects the Nth keyword in
4725 `org-todo-keywords-1'."
4726 (interactive "P")
4727 (if org-agenda-overriding-arguments
4728 (setq arg org-agenda-overriding-arguments))
4729 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4730 (let* ((today (org-today))
4731 (date (calendar-gregorian-from-absolute today))
4732 (kwds org-todo-keywords-for-agenda)
4733 (completion-ignore-case t)
4734 (org-select-this-todo-keyword
4735 (if (stringp arg) arg
4736 (and arg (integerp arg) (> arg 0)
4737 (nth (1- arg) kwds))))
4738 rtn rtnall files file pos)
4739 (when (equal arg '(4))
4740 (setq org-select-this-todo-keyword
4741 (completing-read "Keyword (or KWD1|K2D2|...): "
4742 (mapcar #'list kwds) nil nil)))
4743 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4744 (catch 'exit
4745 (if org-agenda-sticky
4746 (setq org-agenda-buffer-name
4747 (if (stringp org-select-this-todo-keyword)
4748 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4749 org-select-this-todo-keyword)
4750 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4751 (org-agenda-prepare "TODO")
4752 (org-compile-prefix-format 'todo)
4753 (org-set-sorting-strategy 'todo)
4754 (setq org-agenda-redo-command
4755 `(org-todo-list (or (and (numberp current-prefix-arg)
4756 current-prefix-arg)
4757 ,org-select-this-todo-keyword
4758 current-prefix-arg ,arg)))
4759 (setq files (org-agenda-files nil 'ifmode)
4760 rtnall nil)
4761 (while (setq file (pop files))
4762 (catch 'nextfile
4763 (org-check-agenda-file file)
4764 (setq rtn (org-agenda-get-day-entries file date :todo))
4765 (setq rtnall (append rtnall rtn))))
4766 (if org-agenda-overriding-header
4767 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4768 nil 'face 'org-agenda-structure) "\n")
4769 (insert "Global list of TODO items of type: ")
4770 (add-text-properties (point-min) (1- (point))
4771 (list 'face 'org-agenda-structure
4772 'short-heading
4773 (concat "ToDo: "
4774 (or org-select-this-todo-keyword "ALL"))))
4775 (org-agenda-mark-header-line (point-min))
4776 (insert (org-agenda-propertize-selected-todo-keywords
4777 org-select-this-todo-keyword))
4778 (setq pos (point))
4779 (unless org-agenda-multi
4780 (insert (substitute-command-keys "Available with \
4781 `N \\[org-agenda-redo]': (0)[ALL]"))
4782 (let ((n 0) s)
4783 (mapc (lambda (x)
4784 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4785 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4786 (insert "\n "))
4787 (insert " " s))
4788 kwds))
4789 (insert "\n"))
4790 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4791 (org-agenda-mark-header-line (point-min))
4792 (when rtnall
4793 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4794 (goto-char (point-min))
4795 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4796 (add-text-properties (point-min) (point-max)
4797 `(org-agenda-type todo
4798 org-last-args ,arg
4799 org-redo-cmd ,org-agenda-redo-command
4800 org-series-cmd ,org-cmd))
4801 (org-agenda-finalize)
4802 (setq buffer-read-only t))))
4804 ;;; Agenda tags match
4806 ;;;###autoload
4807 (defun org-tags-view (&optional todo-only match)
4808 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4809 The prefix arg TODO-ONLY limits the search to TODO entries."
4810 (interactive "P")
4811 (if org-agenda-overriding-arguments
4812 (setq todo-only (car org-agenda-overriding-arguments)
4813 match (nth 1 org-agenda-overriding-arguments)))
4814 (let* ((org-tags-match-list-sublevels
4815 org-tags-match-list-sublevels)
4816 (completion-ignore-case t)
4817 rtn rtnall files file pos matcher
4818 buffer)
4819 (when (and (stringp match) (not (string-match "\\S-" match)))
4820 (setq match nil))
4821 (catch 'exit
4822 (if org-agenda-sticky
4823 (setq org-agenda-buffer-name
4824 (if (stringp match)
4825 (format "*Org Agenda(%s:%s)*"
4826 (or org-keys (or (and todo-only "M") "m")) match)
4827 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4828 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4829 ;; expanding tags within `org-make-tags-matcher'
4830 (org-agenda-prepare (concat "TAGS " match))
4831 (setq org--matcher-tags-todo-only todo-only
4832 matcher (org-make-tags-matcher match)
4833 match (car matcher)
4834 matcher (cdr matcher))
4835 (org-compile-prefix-format 'tags)
4836 (org-set-sorting-strategy 'tags)
4837 (setq org-agenda-query-string match)
4838 (setq org-agenda-redo-command
4839 (list 'org-tags-view
4840 `(quote ,org--matcher-tags-todo-only)
4841 `(if current-prefix-arg nil ,org-agenda-query-string)))
4842 (setq files (org-agenda-files nil 'ifmode)
4843 rtnall nil)
4844 (while (setq file (pop files))
4845 (catch 'nextfile
4846 (org-check-agenda-file file)
4847 (setq buffer (if (file-exists-p file)
4848 (org-get-agenda-file-buffer file)
4849 (error "No such file %s" file)))
4850 (if (not buffer)
4851 ;; If file does not exist, error message to agenda
4852 (setq rtn (list
4853 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4854 rtnall (append rtnall rtn))
4855 (with-current-buffer buffer
4856 (unless (derived-mode-p 'org-mode)
4857 (error "Agenda file %s is not in `org-mode'" file))
4858 (save-excursion
4859 (save-restriction
4860 (if (eq buffer org-agenda-restrict)
4861 (narrow-to-region org-agenda-restrict-begin
4862 org-agenda-restrict-end)
4863 (widen))
4864 (setq rtn (org-scan-tags 'agenda
4865 matcher
4866 org--matcher-tags-todo-only))
4867 (setq rtnall (append rtnall rtn))))))))
4868 (if org-agenda-overriding-header
4869 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4870 nil 'face 'org-agenda-structure) "\n")
4871 (insert "Headlines with TAGS match: ")
4872 (add-text-properties (point-min) (1- (point))
4873 (list 'face 'org-agenda-structure
4874 'short-heading
4875 (concat "Match: " match)))
4876 (setq pos (point))
4877 (insert match "\n")
4878 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4879 (setq pos (point))
4880 (unless org-agenda-multi
4881 (insert (substitute-command-keys
4882 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4883 to search again with new search string\n")))
4884 (add-text-properties pos (1- (point))
4885 (list 'face 'org-agenda-structure)))
4886 (org-agenda-mark-header-line (point-min))
4887 (when rtnall
4888 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4889 (goto-char (point-min))
4890 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4891 (add-text-properties
4892 (point-min) (point-max)
4893 `(org-agenda-type tags
4894 org-last-args (,org--matcher-tags-todo-only ,match)
4895 org-redo-cmd ,org-agenda-redo-command
4896 org-series-cmd ,org-cmd))
4897 (org-agenda-finalize)
4898 (setq buffer-read-only t))))
4900 ;;; Agenda Finding stuck projects
4902 (defvar org-agenda-skip-regexp nil
4903 "Regular expression used in skipping subtrees for the agenda.
4904 This is basically a temporary global variable that can be set and then
4905 used by user-defined selections using `org-agenda-skip-function'.")
4907 (defvar org-agenda-overriding-header nil
4908 "When set during agenda, todo and tags searches it replaces the header.
4909 This variable should not be set directly, but custom commands can bind it
4910 in the options section.")
4912 (defun org-agenda-skip-entry-when-regexp-matches ()
4913 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4914 If yes, it returns the end position of this entry, causing agenda commands
4915 to skip the entry but continuing the search in the subtree. This is a
4916 function that can be put into `org-agenda-skip-function' for the duration
4917 of a command."
4918 (let ((end (save-excursion (org-end-of-subtree t)))
4919 skip)
4920 (save-excursion
4921 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4922 (and skip end)))
4924 (defun org-agenda-skip-subtree-when-regexp-matches ()
4925 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4926 If yes, it returns the end position of this tree, causing agenda commands
4927 to skip this subtree. This is a function that can be put into
4928 `org-agenda-skip-function' for the duration of a command."
4929 (let ((end (save-excursion (org-end-of-subtree t)))
4930 skip)
4931 (save-excursion
4932 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4933 (and skip end)))
4935 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4936 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4937 If yes, it returns the end position of the current entry (NOT the tree),
4938 causing agenda commands to skip the entry but continuing the search in
4939 the subtree. This is a function that can be put into
4940 `org-agenda-skip-function' for the duration of a command. An important
4941 use of this function is for the stuck project list."
4942 (let ((end (save-excursion (org-end-of-subtree t)))
4943 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4944 skip)
4945 (save-excursion
4946 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4947 (and skip entry-end)))
4949 (defun org-agenda-skip-entry-if (&rest conditions)
4950 "Skip entry if any of CONDITIONS is true.
4951 See `org-agenda-skip-if' for details."
4952 (org-agenda-skip-if nil conditions))
4954 (defun org-agenda-skip-subtree-if (&rest conditions)
4955 "Skip subtree if any of CONDITIONS is true.
4956 See `org-agenda-skip-if' for details."
4957 (org-agenda-skip-if t conditions))
4959 (defun org-agenda-skip-if (subtree conditions)
4960 "Checks current entity for CONDITIONS.
4961 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4962 the entry (i.e. the text before the next heading) is checked.
4964 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4965 from different tests. Valid conditions are:
4967 scheduled Check if there is a scheduled cookie
4968 notscheduled Check if there is no scheduled cookie
4969 deadline Check if there is a deadline
4970 notdeadline Check if there is no deadline
4971 timestamp Check if there is a timestamp (also deadline or scheduled)
4972 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4973 regexp Check if regexp matches
4974 notregexp Check if regexp does not match.
4975 todo Check if TODO keyword matches
4976 nottodo Check if TODO keyword does not match
4978 The regexp is taken from the conditions list, it must come right after
4979 the `regexp' or `notregexp' element.
4981 `todo' and `nottodo' accept as an argument a list of todo
4982 keywords, which may include \"*\" to match any todo keyword.
4984 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4986 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4988 Instead of a list, a keyword class may be given. For example:
4990 (org-agenda-skip-entry-if \\='nottodo \\='done)
4992 would skip entries that haven't been marked with any of \"DONE\"
4993 keywords. Possible classes are: `todo', `done', `any'.
4995 If any of these conditions is met, this function returns the end point of
4996 the entity, causing the search to continue from there. This is a function
4997 that can be put into `org-agenda-skip-function' for the duration of a command."
4998 (let (beg end m)
4999 (org-back-to-heading t)
5000 (setq beg (point)
5001 end (if subtree
5002 (progn (org-end-of-subtree t) (point))
5003 (progn (outline-next-heading) (1- (point)))))
5004 (goto-char beg)
5005 (and
5007 (and (memq 'scheduled conditions)
5008 (re-search-forward org-scheduled-time-regexp end t))
5009 (and (memq 'notscheduled conditions)
5010 (not (re-search-forward org-scheduled-time-regexp end t)))
5011 (and (memq 'deadline conditions)
5012 (re-search-forward org-deadline-time-regexp end t))
5013 (and (memq 'notdeadline conditions)
5014 (not (re-search-forward org-deadline-time-regexp end t)))
5015 (and (memq 'timestamp conditions)
5016 (re-search-forward org-ts-regexp end t))
5017 (and (memq 'nottimestamp conditions)
5018 (not (re-search-forward org-ts-regexp end t)))
5019 (and (setq m (memq 'regexp conditions))
5020 (stringp (nth 1 m))
5021 (re-search-forward (nth 1 m) end t))
5022 (and (setq m (memq 'notregexp conditions))
5023 (stringp (nth 1 m))
5024 (not (re-search-forward (nth 1 m) end t)))
5025 (and (or
5026 (setq m (memq 'nottodo conditions))
5027 (setq m (memq 'todo-unblocked conditions))
5028 (setq m (memq 'nottodo-unblocked conditions))
5029 (setq m (memq 'todo conditions)))
5030 (org-agenda-skip-if-todo m end)))
5031 end)))
5033 (defun org-agenda-skip-if-todo (args end)
5034 "Helper function for `org-agenda-skip-if', do not use it directly.
5035 ARGS is a list with first element either `todo', `nottodo',
5036 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5037 a list of TODO keywords, or a state symbol `todo' or `done' or
5038 `any'."
5039 (let ((kw (car args))
5040 (arg (cadr args))
5041 todo-wds todo-re)
5042 (setq todo-wds
5043 (org-uniquify
5044 (cond
5045 ((listp arg) ;; list of keywords
5046 (if (member "*" arg)
5047 (mapcar 'substring-no-properties org-todo-keywords-1)
5048 arg))
5049 ((symbolp arg) ;; keyword class name
5050 (cond
5051 ((eq arg 'todo)
5052 (org-delete-all org-done-keywords
5053 (mapcar 'substring-no-properties
5054 org-todo-keywords-1)))
5055 ((eq arg 'done) org-done-keywords)
5056 ((eq arg 'any)
5057 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5058 (setq todo-re
5059 (concat "^\\*+[ \t]+\\<\\("
5060 (mapconcat 'identity todo-wds "\\|")
5061 "\\)\\>"))
5062 (cond
5063 ((eq kw 'todo) (re-search-forward todo-re end t))
5064 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5065 ((eq kw 'todo-unblocked)
5066 (catch 'unblocked
5067 (while (re-search-forward todo-re end t)
5068 (or (org-entry-blocked-p) (throw 'unblocked t)))
5069 nil))
5070 ((eq kw 'nottodo-unblocked)
5071 (catch 'unblocked
5072 (while (re-search-forward todo-re end t)
5073 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5077 ;;;###autoload
5078 (defun org-agenda-list-stuck-projects (&rest ignore)
5079 "Create agenda view for projects that are stuck.
5080 Stuck projects are project that have no next actions. For the definitions
5081 of what a project is and how to check if it stuck, customize the variable
5082 `org-stuck-projects'."
5083 (interactive)
5084 (let* ((org-agenda-skip-function
5085 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5086 ;; We could have used org-agenda-skip-if here.
5087 (org-agenda-overriding-header
5088 (or org-agenda-overriding-header "List of stuck projects: "))
5089 (matcher (nth 0 org-stuck-projects))
5090 (todo (nth 1 org-stuck-projects))
5091 (todo-wds (if (member "*" todo)
5092 (progn
5093 (org-agenda-prepare-buffers (org-agenda-files
5094 nil 'ifmode))
5095 (org-delete-all
5096 org-done-keywords-for-agenda
5097 (copy-sequence org-todo-keywords-for-agenda)))
5098 todo))
5099 (todo-re (concat "^\\*+[ \t]+\\("
5100 (mapconcat 'identity todo-wds "\\|")
5101 "\\)\\>"))
5102 (tags (nth 2 org-stuck-projects))
5103 (tags-re (if (member "*" tags)
5104 (concat org-outline-regexp-bol
5105 ".*:[[:alnum:]_@#%]+:[ \t]*$")
5106 (if tags
5107 (concat org-outline-regexp-bol
5108 ".*:\\("
5109 (mapconcat #'identity tags "\\|")
5110 "\\):[[:alnum:]_@#%:]*[ \t]*$"))))
5111 (gen-re (nth 3 org-stuck-projects))
5112 (re-list
5113 (delq nil
5114 (list
5115 (if todo todo-re)
5116 (if tags tags-re)
5117 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5118 gen-re)))))
5119 (setq org-agenda-skip-regexp
5120 (if re-list
5121 (mapconcat 'identity re-list "\\|")
5122 (error "No information how to identify unstuck projects")))
5123 (org-tags-view nil matcher)
5124 (setq org-agenda-buffer-name (buffer-name))
5125 (with-current-buffer org-agenda-buffer-name
5126 (setq org-agenda-redo-command
5127 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5129 ;;; Diary integration
5131 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5132 (defvar diary-list-entries-hook)
5133 (defvar diary-time-regexp)
5134 (defun org-get-entries-from-diary (date)
5135 "Get the (Emacs Calendar) diary entries for DATE."
5136 (require 'diary-lib)
5137 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5138 (diary-display-function 'diary-fancy-display)
5139 (pop-up-frames nil)
5140 (diary-list-entries-hook
5141 (cons 'org-diary-default-entry diary-list-entries-hook))
5142 (diary-file-name-prefix nil) ; turn this feature off
5143 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5144 entries
5145 (org-disable-agenda-to-diary t))
5146 (save-excursion
5147 (save-window-excursion
5148 (funcall (if (fboundp 'diary-list-entries)
5149 'diary-list-entries 'list-diary-entries)
5150 date 1)))
5151 (if (not (get-buffer diary-fancy-buffer))
5152 (setq entries nil)
5153 (with-current-buffer diary-fancy-buffer
5154 (setq buffer-read-only nil)
5155 (if (zerop (buffer-size))
5156 ;; No entries
5157 (setq entries nil)
5158 ;; Omit the date and other unnecessary stuff
5159 (org-agenda-cleanup-fancy-diary)
5160 ;; Add prefix to each line and extend the text properties
5161 (if (zerop (buffer-size))
5162 (setq entries nil)
5163 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5164 (setq entries
5165 (with-temp-buffer
5166 (insert entries) (goto-char (point-min))
5167 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5168 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5169 (replace-match (concat "; " (match-string 1)))))
5170 (buffer-string)))))
5171 (set-buffer-modified-p nil)
5172 (kill-buffer diary-fancy-buffer)))
5173 (when entries
5174 (setq entries (org-split-string entries "\n"))
5175 (setq entries
5176 (mapcar
5177 (lambda (x)
5178 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5179 ;; Extend the text properties to the beginning of the line
5180 (org-add-props x (text-properties-at (1- (length x)) x)
5181 'type "diary" 'date date 'face 'org-agenda-diary))
5182 entries)))))
5184 (defvar org-agenda-cleanup-fancy-diary-hook nil
5185 "Hook run when the fancy diary buffer is cleaned up.")
5187 (defun org-agenda-cleanup-fancy-diary ()
5188 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5189 This gets rid of the date, the underline under the date, and
5190 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5191 date. It also removes lines that contain only whitespace."
5192 (goto-char (point-min))
5193 (if (looking-at ".*?:[ \t]*")
5194 (progn
5195 (replace-match "")
5196 (re-search-forward "\n=+$" nil t)
5197 (replace-match "")
5198 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5199 (re-search-forward "\n=+$" nil t)
5200 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5201 (goto-char (point-min))
5202 (while (re-search-forward "^ +\n" nil t)
5203 (replace-match ""))
5204 (goto-char (point-min))
5205 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5206 (replace-match ""))
5207 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5209 ;; Make sure entries from the diary have the right text properties.
5210 (eval-after-load "diary-lib"
5211 '(if (boundp 'diary-modify-entry-list-string-function)
5212 ;; We can rely on the hook, nothing to do
5214 ;; Hook not available, must use advice to make this work
5215 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5216 "Make the position visible."
5217 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5218 (stringp string)
5219 buffer-file-name)
5220 (setq string (org-modify-diary-entry-string string))))))
5222 (defun org-modify-diary-entry-string (string)
5223 "Add text properties to string, allowing org-mode to act on it."
5224 (org-add-props string nil
5225 'mouse-face 'highlight
5226 'help-echo (if buffer-file-name
5227 (format "mouse-2 or RET jump to diary file %s"
5228 (abbreviate-file-name buffer-file-name))
5230 'org-agenda-diary-link t
5231 'org-marker (org-agenda-new-marker (point-at-bol))))
5233 (defun org-diary-default-entry ()
5234 "Add a dummy entry to the diary.
5235 Needed to avoid empty dates which mess up holiday display."
5236 ;; Catch the error if dealing with the new add-to-diary-alist
5237 (when org-disable-agenda-to-diary
5238 (condition-case nil
5239 (org-add-to-diary-list original-date "Org-mode dummy" "")
5240 (error
5241 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5243 (defun org-add-to-diary-list (&rest args)
5244 (if (fboundp 'diary-add-to-list)
5245 (apply 'diary-add-to-list args)
5246 (apply 'add-to-diary-list args)))
5248 (defvar org-diary-last-run-time nil)
5250 ;;;###autoload
5251 (defun org-diary (&rest args)
5252 "Return diary information from org files.
5253 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5254 It accesses org files and extracts information from those files to be
5255 listed in the diary. The function accepts arguments specifying what
5256 items should be listed. For a list of arguments allowed here, see the
5257 variable `org-agenda-entry-types'.
5259 The call in the diary file should look like this:
5261 &%%(org-diary) ~/path/to/some/orgfile.org
5263 Use a separate line for each org file to check. Or, if you omit the file name,
5264 all files listed in `org-agenda-files' will be checked automatically:
5266 &%%(org-diary)
5268 If you don't give any arguments (as in the example above), the default value
5269 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5270 So the example above may also be written as
5272 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5274 The function expects the lisp variables `entry' and `date' to be provided
5275 by the caller, because this is how the calendar works. Don't use this
5276 function from a program - use `org-agenda-get-day-entries' instead."
5277 (when (> (- (float-time)
5278 org-agenda-last-marker-time)
5280 ;; I am not sure if this works with sticky agendas, because the marker
5281 ;; list is then no longer a global variable.
5282 (org-agenda-reset-markers))
5283 (org-compile-prefix-format 'agenda)
5284 (org-set-sorting-strategy 'agenda)
5285 (setq args (or args org-agenda-entry-types))
5286 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5287 (list entry)
5288 (org-agenda-files t)))
5289 (time (float-time))
5290 file rtn results)
5291 (when (or (not org-diary-last-run-time)
5292 (> (- time
5293 org-diary-last-run-time)
5295 (org-agenda-prepare-buffers files))
5296 (setq org-diary-last-run-time time)
5297 ;; If this is called during org-agenda, don't return any entries to
5298 ;; the calendar. Org Agenda will list these entries itself.
5299 (if org-disable-agenda-to-diary (setq files nil))
5300 (while (setq file (pop files))
5301 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5302 (setq results (append results rtn)))
5303 (when results
5304 (setq results
5305 (mapcar (lambda (i) (replace-regexp-in-string
5306 org-bracket-link-regexp "\\3" i)) results))
5307 (concat (org-agenda-finalize-entries results) "\n"))))
5309 ;;; Agenda entry finders
5311 (defun org-agenda--timestamp-to-absolute (&rest args)
5312 "Call `org-time-string-to-absolute' with ARGS.
5313 However, throw `:skip' whenever an error is raised."
5314 (condition-case e
5315 (apply #'org-time-string-to-absolute args)
5316 (org-diary-sexp-no-match (throw :skip nil))
5317 (error
5318 (message "%s; Skipping entry" (error-message-string e))
5319 (throw :skip nil))))
5321 (defun org-agenda-get-day-entries (file date &rest args)
5322 "Does the work for `org-diary' and `org-agenda'.
5323 FILE is the path to a file to be checked for entries. DATE is date like
5324 the one returned by `calendar-current-date'. ARGS are symbols indicating
5325 which kind of entries should be extracted. For details about these, see
5326 the documentation of `org-diary'."
5327 (let* ((org-startup-folded nil)
5328 (org-startup-align-all-tables nil)
5329 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5330 (error "No such file %s" file))))
5331 (if (not buffer)
5332 ;; If file does not exist, signal it in diary nonetheless.
5333 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5334 (with-current-buffer buffer
5335 (unless (derived-mode-p 'org-mode)
5336 (error "Agenda file %s is not in `org-mode'" file))
5337 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5338 (setf org-agenda-current-date date)
5339 (save-excursion
5340 (save-restriction
5341 (if (eq buffer org-agenda-restrict)
5342 (narrow-to-region org-agenda-restrict-begin
5343 org-agenda-restrict-end)
5344 (widen))
5345 ;; Rationalize ARGS. Also make sure `:deadline' comes
5346 ;; first in order to populate DEADLINES before passing it.
5348 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5349 ;; guarding us from modifying `org-agenda-entry-types'.
5350 (setf args (org-uniquify (or args org-agenda-entry-types)))
5351 (when (and (memq :scheduled args) (memq :scheduled* args))
5352 (setf args (delq :scheduled* args)))
5353 (cond
5354 ((memq :deadline args)
5355 (setf args (cons :deadline
5356 (delq :deadline (delq :deadline* args)))))
5357 ((memq :deadline* args)
5358 (setf args (cons :deadline* (delq :deadline* args)))))
5359 ;; Collect list of headlines. Return them flattened.
5360 (let ((case-fold-search nil) results deadlines)
5361 (dolist (arg args (apply #'nconc (nreverse results)))
5362 (pcase arg
5363 ((and :todo (guard (org-agenda-today-p date)))
5364 (push (org-agenda-get-todos) results))
5365 (:timestamp
5366 (push (org-agenda-get-blocks) results)
5367 (push (org-agenda-get-timestamps deadlines) results))
5368 (:sexp
5369 (push (org-agenda-get-sexps) results))
5370 (:scheduled
5371 (push (org-agenda-get-scheduled deadlines) results))
5372 (:scheduled*
5373 (push (org-agenda-get-scheduled deadlines t) results))
5374 (:closed
5375 (push (org-agenda-get-progress) results))
5376 (:deadline
5377 (setf deadlines (org-agenda-get-deadlines))
5378 (push deadlines results))
5379 (:deadline*
5380 (setf deadlines (org-agenda-get-deadlines t))
5381 (push deadlines results)))))))))))
5383 (defsubst org-em (x y list)
5384 "Is X or Y a member of LIST?"
5385 (or (memq x list) (memq y list)))
5387 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5388 (defvar org-agenda-sorting-strategy-selected nil)
5390 (defun org-agenda-entry-get-agenda-timestamp (pom)
5391 "Retrieve timestamp information for sorting agenda views.
5392 Given a point or marker POM, returns a cons cell of the timestamp
5393 and the timestamp type relevant for the sorting strategy in
5394 `org-agenda-sorting-strategy-selected'."
5395 (let (ts ts-date-type)
5396 (save-match-data
5397 (cond ((org-em 'scheduled-up 'scheduled-down
5398 org-agenda-sorting-strategy-selected)
5399 (setq ts (org-entry-get pom "SCHEDULED")
5400 ts-date-type " scheduled"))
5401 ((org-em 'deadline-up 'deadline-down
5402 org-agenda-sorting-strategy-selected)
5403 (setq ts (org-entry-get pom "DEADLINE")
5404 ts-date-type " deadline"))
5405 ((org-em 'ts-up 'ts-down
5406 org-agenda-sorting-strategy-selected)
5407 (setq ts (org-entry-get pom "TIMESTAMP")
5408 ts-date-type " timestamp"))
5409 ((org-em 'tsia-up 'tsia-down
5410 org-agenda-sorting-strategy-selected)
5411 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5412 ts-date-type " timestamp_ia"))
5413 ((org-em 'timestamp-up 'timestamp-down
5414 org-agenda-sorting-strategy-selected)
5415 (setq ts (or (org-entry-get pom "SCHEDULED")
5416 (org-entry-get pom "DEADLINE")
5417 (org-entry-get pom "TIMESTAMP")
5418 (org-entry-get pom "TIMESTAMP_IA"))
5419 ts-date-type ""))
5420 (t (setq ts-date-type "")))
5421 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5422 ts-date-type))))
5424 (defun org-agenda-get-todos ()
5425 "Return the TODO information for agenda display."
5426 (let* ((props (list 'face nil
5427 'done-face 'org-agenda-done
5428 'org-not-done-regexp org-not-done-regexp
5429 'org-todo-regexp org-todo-regexp
5430 'org-complex-heading-regexp org-complex-heading-regexp
5431 'mouse-face 'highlight
5432 'help-echo
5433 (format "mouse-2 or RET jump to org file %s"
5434 (abbreviate-file-name buffer-file-name))))
5435 (regexp (format org-heading-keyword-regexp-format
5436 (cond
5437 ((and org-select-this-todo-keyword
5438 (equal org-select-this-todo-keyword "*"))
5439 org-todo-regexp)
5440 (org-select-this-todo-keyword
5441 (concat "\\("
5442 (mapconcat 'identity
5443 (org-split-string
5444 org-select-this-todo-keyword
5445 "|")
5446 "\\|") "\\)"))
5447 (t org-not-done-regexp))))
5448 marker priority category level tags todo-state
5449 ts-date ts-date-type ts-date-pair
5450 ee txt beg end inherited-tags todo-state-end-pos)
5451 (goto-char (point-min))
5452 (while (re-search-forward regexp nil t)
5453 (catch :skip
5454 (save-match-data
5455 (beginning-of-line)
5456 (org-agenda-skip)
5457 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5458 (unless (and (setq todo-state (org-get-todo-state))
5459 (setq todo-state-end-pos (match-end 2)))
5460 (goto-char end)
5461 (throw :skip nil))
5462 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5463 (goto-char (1+ beg))
5464 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5465 (throw :skip nil)))
5466 (goto-char (match-beginning 2))
5467 (setq marker (org-agenda-new-marker (match-beginning 0))
5468 category (org-get-category)
5469 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5470 ts-date (car ts-date-pair)
5471 ts-date-type (cdr ts-date-pair)
5472 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5473 inherited-tags
5474 (or (eq org-agenda-show-inherited-tags 'always)
5475 (and (listp org-agenda-show-inherited-tags)
5476 (memq 'todo org-agenda-show-inherited-tags))
5477 (and (eq org-agenda-show-inherited-tags t)
5478 (or (eq org-agenda-use-tag-inheritance t)
5479 (memq 'todo org-agenda-use-tag-inheritance))))
5480 tags (org-get-tags-at nil (not inherited-tags))
5481 level (make-string (org-reduced-level (org-outline-level)) ? )
5482 txt (org-agenda-format-item "" txt level category tags t)
5483 priority (1+ (org-get-priority txt)))
5484 (org-add-props txt props
5485 'org-marker marker 'org-hd-marker marker
5486 'priority priority
5487 'level level
5488 'ts-date ts-date
5489 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5490 (push txt ee)
5491 (if org-agenda-todo-list-sublevels
5492 (goto-char todo-state-end-pos)
5493 (org-end-of-subtree 'invisible))))
5494 (nreverse ee)))
5496 (defun org-agenda-todo-custom-ignore-p (time n)
5497 "Check whether timestamp is farther away than n number of days.
5498 This function is invoked if `org-agenda-todo-ignore-deadlines',
5499 `org-agenda-todo-ignore-scheduled' or
5500 `org-agenda-todo-ignore-timestamp' is set to an integer."
5501 (let ((days (org-time-stamp-to-now
5502 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5503 (if (>= n 0)
5504 (>= days n)
5505 (<= days n))))
5507 ;;;###autoload
5508 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5509 (&optional end)
5510 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5511 (when (or org-agenda-todo-ignore-with-date
5512 org-agenda-todo-ignore-scheduled
5513 org-agenda-todo-ignore-deadlines
5514 org-agenda-todo-ignore-timestamp)
5515 (setq end (or end (save-excursion (outline-next-heading) (point))))
5516 (save-excursion
5517 (or (and org-agenda-todo-ignore-with-date
5518 (re-search-forward org-ts-regexp end t))
5519 (and org-agenda-todo-ignore-scheduled
5520 (re-search-forward org-scheduled-time-regexp end t)
5521 (cond
5522 ((eq org-agenda-todo-ignore-scheduled 'future)
5523 (> (org-time-stamp-to-now
5524 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5525 ((eq org-agenda-todo-ignore-scheduled 'past)
5526 (<= (org-time-stamp-to-now
5527 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5528 ((numberp org-agenda-todo-ignore-scheduled)
5529 (org-agenda-todo-custom-ignore-p
5530 (match-string 1) org-agenda-todo-ignore-scheduled))
5531 (t)))
5532 (and org-agenda-todo-ignore-deadlines
5533 (re-search-forward org-deadline-time-regexp end t)
5534 (cond
5535 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5536 ((eq org-agenda-todo-ignore-deadlines 'far)
5537 (not (org-deadline-close-p (match-string 1))))
5538 ((eq org-agenda-todo-ignore-deadlines 'future)
5539 (> (org-time-stamp-to-now
5540 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5541 ((eq org-agenda-todo-ignore-deadlines 'past)
5542 (<= (org-time-stamp-to-now
5543 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5544 ((numberp org-agenda-todo-ignore-deadlines)
5545 (org-agenda-todo-custom-ignore-p
5546 (match-string 1) org-agenda-todo-ignore-deadlines))
5547 (t (org-deadline-close-p (match-string 1)))))
5548 (and org-agenda-todo-ignore-timestamp
5549 (let ((buffer (current-buffer))
5550 (regexp
5551 (concat
5552 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5553 (start (point)))
5554 ;; Copy current buffer into a temporary one
5555 (with-temp-buffer
5556 (insert-buffer-substring buffer start end)
5557 (goto-char (point-min))
5558 ;; Delete SCHEDULED and DEADLINE items
5559 (while (re-search-forward regexp end t)
5560 (delete-region (match-beginning 0) (match-end 0)))
5561 (goto-char (point-min))
5562 ;; No search for timestamp left
5563 (when (re-search-forward org-ts-regexp nil t)
5564 (cond
5565 ((eq org-agenda-todo-ignore-timestamp 'future)
5566 (> (org-time-stamp-to-now
5567 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5568 ((eq org-agenda-todo-ignore-timestamp 'past)
5569 (<= (org-time-stamp-to-now
5570 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5571 ((numberp org-agenda-todo-ignore-timestamp)
5572 (org-agenda-todo-custom-ignore-p
5573 (match-string 1) org-agenda-todo-ignore-timestamp))
5574 (t))))))))))
5576 (defun org-agenda-get-timestamps (&optional deadline-results)
5577 "Return the date stamp information for agenda display."
5578 (let* ((props (list 'face 'org-agenda-calendar-event
5579 'org-not-done-regexp org-not-done-regexp
5580 'org-todo-regexp org-todo-regexp
5581 'org-complex-heading-regexp org-complex-heading-regexp
5582 'mouse-face 'highlight
5583 'help-echo
5584 (format "mouse-2 or RET jump to org file %s"
5585 (abbreviate-file-name buffer-file-name))))
5586 (d1 (calendar-absolute-from-gregorian date))
5588 (deadline-position-alist
5589 (mapcar (lambda (a) (and (setq mm (get-text-property
5590 0 'org-hd-marker a))
5591 (cons (marker-position mm) a)))
5592 deadline-results))
5593 (remove-re org-ts-regexp)
5594 (regexp
5595 (concat
5596 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5597 (regexp-quote
5598 (substring
5599 (format-time-string
5600 (car org-time-stamp-formats)
5601 (apply 'encode-time ; DATE bound by calendar
5602 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5603 1 11))
5604 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5605 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5606 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5607 donep tmp priority category level ee txt timestr tags
5608 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5609 inherited-tags ts-date)
5610 (goto-char (point-min))
5611 (while (setq end-of-match (re-search-forward regexp nil t))
5612 (setq b0 (match-beginning 0)
5613 b3 (match-beginning 3) e3 (match-end 3)
5614 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5615 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5616 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5617 (member todo-state
5618 org-agenda-repeating-timestamp-show-all)))
5619 (catch :skip
5620 (and (org-at-date-range-p) (throw :skip nil))
5621 (org-agenda-skip)
5622 (if (and (match-end 1)
5623 (not (= d1 (org-agenda--timestamp-to-absolute
5624 (match-string 1) d1 nil show-all
5625 (current-buffer) b0))))
5626 (throw :skip nil))
5627 (if (and e3
5628 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5629 (throw :skip nil))
5630 (setq tmp (buffer-substring (max (point-min)
5631 (- b0 org-ds-keyword-length))
5633 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5634 inactivep (= (char-after b0) ?\[)
5635 deadlinep (string-match org-deadline-regexp tmp)
5636 scheduledp (string-match org-scheduled-regexp tmp)
5637 closedp (and org-agenda-include-inactive-timestamps
5638 (string-match org-closed-string tmp))
5639 clockp (and org-agenda-include-inactive-timestamps
5640 (or (string-match org-clock-string tmp)
5641 (string-match "]-+\\'" tmp)))
5642 warntime (get-text-property (point) 'org-appt-warntime)
5643 donep (member todo-state org-done-keywords))
5644 (if (or scheduledp deadlinep closedp clockp
5645 (and donep org-agenda-skip-timestamp-if-done))
5646 (throw :skip t))
5647 (if (string-match ">" timestr)
5648 ;; substring should only run to end of time stamp
5649 (setq timestr (substring timestr 0 (match-end 0))))
5650 (setq marker (org-agenda-new-marker b0)
5651 category (org-get-category b0))
5652 (save-excursion
5653 (if (not (re-search-backward org-outline-regexp-bol nil t))
5654 (throw :skip nil)
5655 (goto-char (match-beginning 0))
5656 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5657 (assoc (point) deadline-position-alist))
5658 (throw :skip nil))
5659 (setq hdmarker (org-agenda-new-marker)
5660 inherited-tags
5661 (or (eq org-agenda-show-inherited-tags 'always)
5662 (and (listp org-agenda-show-inherited-tags)
5663 (memq 'agenda org-agenda-show-inherited-tags))
5664 (and (eq org-agenda-show-inherited-tags t)
5665 (or (eq org-agenda-use-tag-inheritance t)
5666 (memq 'agenda org-agenda-use-tag-inheritance))))
5667 tags (org-get-tags-at nil (not inherited-tags))
5668 level (make-string (org-reduced-level (org-outline-level)) ? ))
5669 (looking-at "\\*+[ \t]+\\(.*\\)")
5670 (setq head (match-string 1))
5671 (setq txt (org-agenda-format-item
5672 (if inactivep org-agenda-inactive-leader nil)
5673 head level category tags timestr
5674 remove-re habitp)))
5675 (setq priority (org-get-priority txt))
5676 (org-add-props txt props 'priority priority
5677 'org-marker marker 'org-hd-marker hdmarker
5678 'date date
5679 'level level
5680 'ts-date
5681 (ignore-errors (org-time-string-to-absolute timestr))
5682 'todo-state todo-state
5683 'warntime warntime
5684 'type "timestamp")
5685 (push txt ee))
5686 (if org-agenda-skip-additional-timestamps-same-entry
5687 (outline-next-heading)
5688 (goto-char end-of-match))))
5689 (nreverse ee)))
5691 (defun org-agenda-get-sexps ()
5692 "Return the sexp information for agenda display."
5693 (require 'diary-lib)
5694 (let* ((props (list 'face 'org-agenda-calendar-sexp
5695 'mouse-face 'highlight
5696 'help-echo
5697 (format "mouse-2 or RET jump to org file %s"
5698 (abbreviate-file-name buffer-file-name))))
5699 (regexp "^&?%%(")
5700 marker category extra level ee txt tags entry
5701 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5702 (goto-char (point-min))
5703 (while (re-search-forward regexp nil t)
5704 (catch :skip
5705 (org-agenda-skip)
5706 (setq beg (match-beginning 0))
5707 (goto-char (1- (match-end 0)))
5708 (setq b (point))
5709 (forward-sexp 1)
5710 (setq sexp (buffer-substring b (point)))
5711 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5712 (org-trim (match-string 1))
5713 ""))
5714 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5715 (when result
5716 (setq marker (org-agenda-new-marker beg)
5717 level (make-string (org-reduced-level (org-outline-level)) ? )
5718 category (org-get-category beg)
5719 inherited-tags
5720 (or (eq org-agenda-show-inherited-tags 'always)
5721 (and (listp org-agenda-show-inherited-tags)
5722 (memq 'agenda org-agenda-show-inherited-tags))
5723 (and (eq org-agenda-show-inherited-tags t)
5724 (or (eq org-agenda-use-tag-inheritance t)
5725 (memq 'agenda org-agenda-use-tag-inheritance))))
5726 tags (org-get-tags-at nil (not inherited-tags))
5727 todo-state (org-get-todo-state)
5728 warntime (get-text-property (point) 'org-appt-warntime)
5729 extra nil)
5731 (dolist (r (if (stringp result)
5732 (list result)
5733 result)) ;; we expect a list here
5734 (when (and org-agenda-diary-sexp-prefix
5735 (string-match org-agenda-diary-sexp-prefix r))
5736 (setq extra (match-string 0 r)
5737 r (replace-match "" nil nil r)))
5738 (if (string-match "\\S-" r)
5739 (setq txt r)
5740 (setq txt "SEXP entry returned empty string"))
5741 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5742 (org-add-props txt props 'org-marker marker
5743 'date date 'todo-state todo-state
5744 'level level 'type "sexp" 'warntime warntime)
5745 (push txt ee)))))
5746 (nreverse ee)))
5748 ;; Calendar sanity: define some functions that are independent of
5749 ;; `calendar-date-style'.
5750 (defun org-anniversary (year month day &optional mark)
5751 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5752 (with-no-warnings
5753 (let ((calendar-date-style 'iso))
5754 (diary-anniversary year month day mark))))
5755 (defun org-cyclic (N year month day &optional mark)
5756 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5757 (with-no-warnings
5758 (let ((calendar-date-style 'iso))
5759 (diary-cyclic N year month day mark))))
5760 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5761 "Like `diary-block', but with fixed (ISO) order of arguments."
5762 (with-no-warnings
5763 (let ((calendar-date-style 'iso))
5764 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5765 (defun org-date (year month day &optional mark)
5766 "Like `diary-date', but with fixed (ISO) order of arguments."
5767 (with-no-warnings
5768 (let ((calendar-date-style 'iso))
5769 (diary-date year month day mark))))
5771 ;; Define the `org-class' function
5772 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5773 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5774 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5775 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5776 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5777 `holidays', then any date that is known by the Emacs calendar to be a
5778 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5779 then those holidays will be skipped."
5780 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5781 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5782 (d (calendar-absolute-from-gregorian date))
5783 (h (when skip-weeks (calendar-check-holidays date))))
5784 (and
5785 (<= date1 d)
5786 (<= d date2)
5787 (= (calendar-day-of-week date) dayname)
5788 (or (not skip-weeks)
5789 (progn
5790 (require 'cal-iso)
5791 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5792 (not (or (and h (memq 'holidays skip-weeks))
5793 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5794 entry)))
5796 (defalias 'org-get-closed 'org-agenda-get-progress)
5797 (defun org-agenda-get-progress ()
5798 "Return the logged TODO entries for agenda display."
5799 (let* ((props (list 'mouse-face 'highlight
5800 'org-not-done-regexp org-not-done-regexp
5801 'org-todo-regexp org-todo-regexp
5802 'org-complex-heading-regexp org-complex-heading-regexp
5803 'help-echo
5804 (format "mouse-2 or RET jump to org file %s"
5805 (abbreviate-file-name buffer-file-name))))
5806 (items (if (consp org-agenda-show-log-scoped)
5807 org-agenda-show-log-scoped
5808 (if (eq org-agenda-show-log-scoped 'clockcheck)
5809 '(clock)
5810 org-agenda-log-mode-items)))
5811 (parts
5812 (delq nil
5813 (list
5814 (if (memq 'closed items) (concat "\\<" org-closed-string))
5815 (if (memq 'clock items) (concat "\\<" org-clock-string))
5816 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5817 (parts-re (if parts (mapconcat 'identity parts "\\|")
5818 (error "`org-agenda-log-mode-items' is empty")))
5819 (regexp (concat
5820 "\\(" parts-re "\\)"
5821 " *\\["
5822 (regexp-quote
5823 (substring
5824 (format-time-string
5825 (car org-time-stamp-formats)
5826 (apply 'encode-time ; DATE bound by calendar
5827 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5828 1 11))))
5829 (org-agenda-search-headline-for-time nil)
5830 marker hdmarker priority category level tags closedp
5831 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5832 (goto-char (point-min))
5833 (while (re-search-forward regexp nil t)
5834 (catch :skip
5835 (org-agenda-skip)
5836 (setq marker (org-agenda-new-marker (match-beginning 0))
5837 closedp (equal (match-string 1) org-closed-string)
5838 statep (equal (string-to-char (match-string 1)) ?-)
5839 clockp (not (or closedp statep))
5840 state (and statep (match-string 2))
5841 category (org-get-category (match-beginning 0))
5842 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5843 (when (string-match "\\]" timestr)
5844 ;; substring should only run to end of time stamp
5845 (setq rest (substring timestr (match-end 0))
5846 timestr (substring timestr 0 (match-end 0)))
5847 (if (and (not closedp) (not statep)
5848 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5849 rest))
5850 (progn (setq timestr (concat (substring timestr 0 -1)
5851 "-" (match-string 1 rest) "]"))
5852 (setq clocked (match-string 2 rest)))
5853 (setq clocked "-")))
5854 (save-excursion
5855 (setq extra
5856 (cond
5857 ((not org-agenda-log-mode-add-notes) nil)
5858 (statep
5859 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5860 (match-string 1)))
5861 (clockp
5862 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5863 (match-string 1)))))
5864 (if (not (re-search-backward org-outline-regexp-bol nil t))
5865 (throw :skip nil)
5866 (goto-char (match-beginning 0))
5867 (setq hdmarker (org-agenda-new-marker)
5868 inherited-tags
5869 (or (eq org-agenda-show-inherited-tags 'always)
5870 (and (listp org-agenda-show-inherited-tags)
5871 (memq 'todo org-agenda-show-inherited-tags))
5872 (and (eq org-agenda-show-inherited-tags t)
5873 (or (eq org-agenda-use-tag-inheritance t)
5874 (memq 'todo org-agenda-use-tag-inheritance))))
5875 tags (org-get-tags-at nil (not inherited-tags))
5876 level (make-string (org-reduced-level (org-outline-level)) ? ))
5877 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5878 (setq txt (match-string 1))
5879 (when extra
5880 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5881 (setq txt (concat (substring txt 0 (match-beginning 1))
5882 " - " extra " " (match-string 2 txt)))
5883 (setq txt (concat txt " - " extra))))
5884 (setq txt (org-agenda-format-item
5885 (cond
5886 (closedp "Closed: ")
5887 (statep (concat "State: (" state ")"))
5888 (t (concat "Clocked: (" clocked ")")))
5889 txt level category tags timestr)))
5890 (setq priority 100000)
5891 (org-add-props txt props
5892 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5893 'priority priority 'level level
5894 'type "closed" 'date date
5895 'undone-face 'org-warning 'done-face 'org-agenda-done)
5896 (push txt ee))
5897 (goto-char (point-at-eol))))
5898 (nreverse ee)))
5900 (defun org-agenda-show-clocking-issues ()
5901 "Add overlays, showing issues with clocking.
5902 See also the user option `org-agenda-clock-consistency-checks'."
5903 (interactive)
5904 (let* ((org-time-clocksum-use-effort-durations nil)
5905 (pl org-agenda-clock-consistency-checks)
5906 (re (concat "^[ \t]*"
5907 org-clock-string
5908 "[ \t]+"
5909 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5910 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5911 (tlstart 0.)
5912 (tlend 0.)
5913 (maxtime (org-hh:mm-string-to-minutes
5914 (or (plist-get pl :max-duration) "24:00")))
5915 (mintime (org-hh:mm-string-to-minutes
5916 (or (plist-get pl :min-duration) 0)))
5917 (maxgap (org-hh:mm-string-to-minutes
5918 ;; default 30:00 means never complain
5919 (or (plist-get pl :max-gap) "30:00")))
5920 (gapok (mapcar 'org-hh:mm-string-to-minutes
5921 (plist-get pl :gap-ok-around)))
5922 (def-face (or (plist-get pl :default-face)
5923 '((:background "DarkRed") (:foreground "white"))))
5924 issue face m te ts dt ov)
5925 (goto-char (point-min))
5926 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5927 (setq issue nil face def-face)
5928 (catch 'next
5929 (setq m (org-get-at-bol 'org-marker)
5930 te nil ts nil)
5931 (unless (and m (markerp m))
5932 (setq issue "No valid clock line") (throw 'next t))
5933 (org-with-point-at m
5934 (save-excursion
5935 (goto-char (point-at-bol))
5936 (unless (looking-at re)
5937 (error "No valid Clock line")
5938 (throw 'next t))
5939 (unless (match-end 3)
5940 (setq issue "No end time"
5941 face (or (plist-get pl :no-end-time-face) face))
5942 (throw 'next t))
5943 (setq ts (match-string 1)
5944 te (match-string 3)
5945 ts (float-time
5946 (apply #'encode-time (org-parse-time-string ts)))
5947 te (float-time
5948 (apply #'encode-time (org-parse-time-string te)))
5949 dt (- te ts))))
5950 (cond
5951 ((> dt (* 60 maxtime))
5952 ;; a very long clocking chunk
5953 (setq issue (format "Clocking interval is very long: %s"
5954 (org-minutes-to-clocksum-string
5955 (floor (/ (float dt) 60.))))
5956 face (or (plist-get pl :long-face) face)))
5957 ((< dt (* 60 mintime))
5958 ;; a very short clocking chunk
5959 (setq issue (format "Clocking interval is very short: %s"
5960 (org-minutes-to-clocksum-string
5961 (floor (/ (float dt) 60.))))
5962 face (or (plist-get pl :short-face) face)))
5963 ((and (> tlend 0) (< ts tlend))
5964 ;; Two clock entries are overlapping
5965 (setq issue (format "Clocking overlap: %d minutes"
5966 (/ (- tlend ts) 60))
5967 face (or (plist-get pl :overlap-face) face)))
5968 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5969 ;; There is a gap, lets see if we need to report it
5970 (unless (org-agenda-check-clock-gap tlend ts gapok)
5971 (setq issue (format "Clocking gap: %d minutes"
5972 (/ (- ts tlend) 60))
5973 face (or (plist-get pl :gap-face) face))))
5974 (t nil)))
5975 (setq tlend (or te tlend) tlstart (or ts tlstart))
5976 (when issue
5977 ;; OK, there was some issue, add an overlay to show the issue
5978 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5979 (overlay-put ov 'before-string
5980 (concat
5981 (org-add-props
5982 (format "%-43s" (concat " " issue))
5984 'face face)
5985 "\n"))
5986 (overlay-put ov 'evaporate t)))))
5988 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5989 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5990 (catch 'exit
5991 (unless ok-list
5992 ;; there are no OK times for gaps...
5993 (throw 'exit nil))
5994 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5995 ;; This is more than 24 hours, so it is OK.
5996 ;; because we have at least one OK time, that must be in the
5997 ;; 24 hour interval.
5998 (throw 'exit t))
5999 ;; We have a shorter gap.
6000 ;; Now we have to get the minute of the day when these times are
6001 (let* ((t1dec (decode-time (seconds-to-time t1)))
6002 (t2dec (decode-time (seconds-to-time t2)))
6003 ;; compute the minute on the day
6004 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6005 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6006 (when (< min2 min1)
6007 ;; if min2 is smaller than min1, this means it is on the next day.
6008 ;; Wrap it to after midnight.
6009 (setq min2 (+ min2 1440)))
6010 ;; Now check if any of the OK times is in the gap
6011 (mapc (lambda (x)
6012 ;; Wrap the time to after midnight if necessary
6013 (if (< x min1) (setq x (+ x 1440)))
6014 ;; Check if in interval
6015 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6016 ok-list)
6017 ;; Nope, this gap is not OK
6018 nil)))
6020 (defun org-agenda-get-deadlines (&optional with-hour)
6021 "Return the deadline information for agenda display.
6022 When WITH-HOUR is non-nil, only return deadlines with an hour
6023 specification like [h]h:mm."
6024 (let* ((props (list 'mouse-face 'highlight
6025 'org-not-done-regexp org-not-done-regexp
6026 'org-todo-regexp org-todo-regexp
6027 'org-complex-heading-regexp org-complex-heading-regexp
6028 'help-echo
6029 (format "mouse-2 or RET jump to org file %s"
6030 (abbreviate-file-name buffer-file-name))))
6031 (regexp (if with-hour
6032 org-deadline-time-hour-regexp
6033 org-deadline-time-regexp))
6034 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6035 (current (calendar-absolute-from-gregorian date))
6036 deadline-items)
6037 (goto-char (point-min))
6038 (while (re-search-forward regexp nil t)
6039 (catch :skip
6040 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6041 (org-agenda-skip)
6042 (let* ((s (match-string 1))
6043 (pos (1- (match-beginning 1)))
6044 (todo-state (save-match-data (org-get-todo-state)))
6045 (donep (member todo-state org-done-keywords))
6046 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6047 (member todo-state
6048 org-agenda-repeating-timestamp-show-all)))
6049 ;; DEADLINE is the current scheduled date. When it
6050 ;; contains a repeater and SHOW-ALL is non-nil,
6051 ;; LAST-REPEAT is the repeat closest to CURRENT.
6052 ;; Otherwise, LAST-REPEAT is equal to DEADLINE.
6053 (last-repeat (org-agenda--timestamp-to-absolute
6054 s current 'past show-all (current-buffer) pos))
6055 (deadline (org-agenda--timestamp-to-absolute s current))
6056 (diff (- last-repeat current))
6057 (suppress-prewarning
6058 (let ((scheduled
6059 (and org-agenda-skip-deadline-prewarning-if-scheduled
6060 (org-entry-get nil "SCHEDULED"))))
6061 (cond
6062 ((not scheduled) nil)
6063 ;; The current item has a scheduled date, so
6064 ;; evaluate its prewarning lead time.
6065 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6066 ;; Use global prewarning-restart lead time.
6067 org-agenda-skip-deadline-prewarning-if-scheduled)
6068 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6069 'pre-scheduled)
6070 ;; Set pre-warning to no earlier than SCHEDULED.
6071 (min (- last-repeat
6072 (org-agenda--timestamp-to-absolute
6073 scheduled current 'past show-all
6074 (current-buffer)
6075 (save-excursion
6076 (beginning-of-line)
6077 (1+ (search-forward org-deadline-string)))))
6078 org-deadline-warning-days))
6079 ;; Set pre-warning to deadline.
6080 (t 0))))
6081 (wdays (if suppress-prewarning
6082 (let ((org-deadline-warning-days suppress-prewarning))
6083 (org-get-wdays s))
6084 (org-get-wdays s))))
6085 ;; When to show a deadline in the calendar: if the
6086 ;; expiration is within WDAYS warning time. Past-due
6087 ;; deadlines are only shown on the current date
6088 (unless (or (and (<= diff wdays)
6089 (and todayp (not org-agenda-only-exact-dates)))
6090 (= diff 0))
6091 (throw :skip nil))
6092 ;; Skip done tasks if `org-agenda-skip-deadline-if-done' is
6093 ;; non-nil or if it isn't applicable to CURRENT deadline.
6094 (when (and donep
6095 (or org-agenda-skip-deadline-if-done
6096 (/= deadline current)))
6097 (throw :skip nil))
6098 (save-excursion
6099 (re-search-backward "^\\*+[ \t]+" nil t)
6100 (goto-char (match-end 0))
6101 (let* ((category (org-get-category))
6102 (level
6103 (make-string (org-reduced-level (org-outline-level)) ?\s))
6104 (head (buffer-substring (point) (line-end-position)))
6105 (inherited-tags
6106 (or (eq org-agenda-show-inherited-tags 'always)
6107 (and (listp org-agenda-show-inherited-tags)
6108 (memq 'agenda org-agenda-show-inherited-tags))
6109 (and (eq org-agenda-show-inherited-tags t)
6110 (or (eq org-agenda-use-tag-inheritance t)
6111 (memq 'agenda
6112 org-agenda-use-tag-inheritance)))))
6113 (tags (org-get-tags-at nil (not inherited-tags)))
6114 (timestr
6115 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6116 (concat (substring s (match-beginning 1)) " ")
6117 'time))
6118 (item
6119 (org-agenda-format-item
6120 ;; For past deadlines, make sure to report time
6121 ;; difference since date S, not since closest
6122 ;; repeater.
6123 (let ((diff (if (< (org-today) current) diff
6124 (- deadline current))))
6125 (if (= diff 0) (car org-agenda-deadline-leaders)
6126 (let ((future (nth 1 org-agenda-deadline-leaders))
6127 (past (nth 2 org-agenda-deadline-leaders)))
6128 (cond ((> diff 0) (format future diff))
6129 ((string= future past) (format past diff))
6130 (t (format past (abs diff)))))))
6131 head level category tags
6132 (and (= diff 0) timestr)))
6133 (face (org-agenda-deadline-face
6134 (- 1 (/ (float (- deadline current)) (max wdays 1)))))
6135 (upcomingp (and todayp (> diff 0)))
6136 (warntime (get-text-property (point) 'org-appt-warntime)))
6137 (org-add-props item props
6138 'org-marker (org-agenda-new-marker pos)
6139 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6140 'warntime warntime
6141 'level level
6142 'ts-date deadline
6143 'priority (- (org-get-priority item) diff)
6144 'todo-state todo-state
6145 'type (if upcomingp "upcoming-deadline" "deadline")
6146 'date (if upcomingp date deadline)
6147 'face (if donep 'org-agenda-done face)
6148 'undone-face face
6149 'done-face 'org-agenda-done)
6150 (push item deadline-items))))))
6151 (nreverse deadline-items)))
6153 (defun org-agenda-deadline-face (fraction)
6154 "Return the face to displaying a deadline item.
6155 FRACTION is what fraction of the head-warning time has passed."
6156 (let ((faces org-agenda-deadline-faces) f)
6157 (catch 'exit
6158 (while (setq f (pop faces))
6159 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6161 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6162 "Return the scheduled information for agenda display.
6163 Optional argument DEADLINES is a list of deadline items to be
6164 displayed in agenda view. When WITH-HOUR is non-nil, only return
6165 scheduled items with an hour specification like [h]h:mm."
6166 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6167 'org-todo-regexp org-todo-regexp
6168 'org-complex-heading-regexp org-complex-heading-regexp
6169 'done-face 'org-agenda-done
6170 'mouse-face 'highlight
6171 'help-echo
6172 (format "mouse-2 or RET jump to org file %s"
6173 (abbreviate-file-name buffer-file-name))))
6174 (regexp (if with-hour
6175 org-scheduled-time-hour-regexp
6176 org-scheduled-time-regexp))
6177 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6178 (current (calendar-absolute-from-gregorian date))
6179 (deadline-pos
6180 (mapcar (lambda (d)
6181 (let ((m (get-text-property 0 'org-hd-marker d)))
6182 (and m (marker-position m))))
6183 deadlines))
6184 scheduled-items)
6185 (goto-char (point-min))
6186 (while (re-search-forward regexp nil t)
6187 (catch :skip
6188 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6189 (org-agenda-skip)
6190 (let* ((s (match-string 1))
6191 (pos (1- (match-beginning 1)))
6192 (todo-state (save-match-data (org-get-todo-state)))
6193 (donep (member todo-state org-done-keywords))
6194 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6195 (member todo-state
6196 org-agenda-repeating-timestamp-show-all)))
6197 ;; SCHEDULE is the current scheduled date. When it
6198 ;; contains a repeater and SHOW-ALL is non-nil,
6199 ;; LAST-REPEAT is the repeat closest to CURRENT.
6200 ;; Otherwise, LAST-REPEAT is equal to SCHEDULE.
6201 (last-repeat (org-agenda--timestamp-to-absolute
6202 s current 'past show-all (current-buffer) pos))
6203 (schedule (org-agenda--timestamp-to-absolute s current))
6204 (diff (- last-repeat current))
6205 (warntime (get-text-property (point) 'org-appt-warntime))
6206 (pastschedp (< schedule (org-today)))
6207 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6208 (suppress-delay
6209 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6210 (org-entry-get nil "DEADLINE"))))
6211 (cond
6212 ((not deadline) nil)
6213 ;; The current item has a deadline date, so
6214 ;; evaluate its delay time.
6215 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6216 ;; Use global delay time.
6217 (- org-agenda-skip-scheduled-delay-if-deadline))
6218 ((eq org-agenda-skip-scheduled-delay-if-deadline
6219 'post-deadline)
6220 ;; Set delay to no later than DEADLINE. If
6221 ;; DEADLINE has a repeater, compare last schedule
6222 ;; repeat and last deadline repeat.
6223 (min (- last-repeat
6224 (org-agenda--timestamp-to-absolute
6225 deadline current 'past show-all
6226 (current-buffer)
6227 (save-excursion
6228 (beginning-of-line)
6229 (1+ (search-forward org-deadline-string)))))
6230 org-scheduled-delay-days))
6231 (t 0))))
6232 (ddays
6233 (cond
6234 ;; Nullify delay when a repeater triggered already
6235 ;; and the delay is of the form --Xd.
6236 ((and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6237 (/= schedule last-repeat))
6239 (suppress-delay
6240 (let ((org-scheduled-delay-days suppress-delay))
6241 (org-get-wdays s t t)))
6242 (t (org-get-wdays s t)))))
6243 ;; Only show a scheduled item in the calendar if it is on or
6244 ;; past the current date. Skip it if it has been displayed
6245 ;; for more than `org-scheduled-past-days'.
6246 (unless (or (and (>= ddays 0) (= diff (- ddays)))
6247 (and (< (+ diff ddays) 0)
6248 (< (abs diff) org-scheduled-past-days)
6249 (and todayp (not org-agenda-only-exact-dates)))
6250 (and todayp
6251 habitp
6252 (bound-and-true-p org-habit-show-all-today)))
6253 (throw :skip nil))
6254 ;; Skip done habits, or tasks if
6255 ;; `org-agenda-skip-deadline-if-done' is non-nil or if it
6256 ;; was scheduled in the past anyway.
6257 (when (and donep
6258 (or org-agenda-skip-scheduled-if-done
6259 (/= schedule current)
6260 habitp))
6261 (throw :skip nil))
6262 ;; Skip entry if it already appears as a deadline, per
6263 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6264 ;; doesn't apply to habits.
6265 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6266 ((guard
6267 (or (not (assq (line-beginning-position 0) deadline-pos))
6268 habitp))
6269 nil)
6270 (`repeated-after-deadline
6271 (>= last-repeat
6272 (time-to-days (org-get-deadline-time (point)))))
6273 (`not-today pastschedp)
6274 (`t t)
6275 (_ nil))
6276 (throw :skip nil))
6277 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6278 ;; only show them for today.
6279 (when (and habitp
6280 (or (not (bound-and-true-p org-habit-show-habits))
6281 (and (not todayp)
6282 (bound-and-true-p
6283 org-habit-show-habits-only-for-today))))
6284 (throw :skip nil))
6285 (save-excursion
6286 (re-search-backward "^\\*+[ \t]+" nil t)
6287 (goto-char (match-end 0))
6288 (let* ((category (org-get-category))
6289 (inherited-tags
6290 (or (eq org-agenda-show-inherited-tags 'always)
6291 (and (listp org-agenda-show-inherited-tags)
6292 (memq 'agenda org-agenda-show-inherited-tags))
6293 (and (eq org-agenda-show-inherited-tags t)
6294 (or (eq org-agenda-use-tag-inheritance t)
6295 (memq 'agenda
6296 org-agenda-use-tag-inheritance)))))
6297 (tags (org-get-tags-at nil (not inherited-tags)))
6298 (level
6299 (make-string (org-reduced-level (org-outline-level)) ?\s))
6300 (head (buffer-substring (point) (line-end-position)))
6301 (timestr
6302 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6303 (concat (substring s (match-beginning 1)) " ")
6304 'time))
6305 (item (org-agenda-format-item
6306 ;; For past scheduled dates, make sure to
6307 ;; report time difference since SCHEDULE,
6308 ;; not since closest repeater.
6309 (let ((diff (if (< (org-today) current) diff
6310 (- schedule current))))
6311 (if (= diff 0) (car org-agenda-scheduled-leaders)
6312 (format (nth 1 org-agenda-scheduled-leaders)
6313 (- 1 diff))))
6314 head level category tags
6315 (and (= diff 0) timestr)
6316 nil habitp))
6317 (face (cond ((and (not habitp) pastschedp)
6318 'org-scheduled-previously)
6319 (todayp 'org-scheduled-today)
6320 (t 'org-scheduled)))
6321 (habitp (and habitp (org-habit-parse-todo))))
6322 (org-add-props item props
6323 'undone-face face
6324 'face (if donep 'org-agenda-done face)
6325 'org-marker (org-agenda-new-marker pos)
6326 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6327 'type (if pastschedp "past-scheduled" "scheduled")
6328 'date (if pastschedp schedule date)
6329 'ts-date schedule
6330 'warntime warntime
6331 'level level
6332 'priority (if habitp (org-habit-get-priority habitp)
6333 (+ 94 (- 5 diff) (org-get-priority item)))
6334 'org-habit-p habitp
6335 'todo-state todo-state)
6336 (push item scheduled-items))))))
6337 (nreverse scheduled-items)))
6339 (defun org-agenda-get-blocks ()
6340 "Return the date-range information for agenda display."
6341 (let* ((props (list 'face nil
6342 'org-not-done-regexp org-not-done-regexp
6343 'org-todo-regexp org-todo-regexp
6344 'org-complex-heading-regexp org-complex-heading-regexp
6345 'mouse-face 'highlight
6346 'help-echo
6347 (format "mouse-2 or RET jump to org file %s"
6348 (abbreviate-file-name buffer-file-name))))
6349 (regexp org-tr-regexp)
6350 (d0 (calendar-absolute-from-gregorian date))
6351 marker hdmarker ee txt d1 d2 s1 s2 category
6352 level todo-state tags pos head donep inherited-tags)
6353 (goto-char (point-min))
6354 (while (re-search-forward regexp nil t)
6355 (catch :skip
6356 (org-agenda-skip)
6357 (setq pos (point))
6358 (let ((start-time (match-string 1))
6359 (end-time (match-string 2)))
6360 (setq s1 (match-string 1)
6361 s2 (match-string 2)
6362 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6363 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6364 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6365 ;; Only allow days between the limits, because the normal
6366 ;; date stamps will catch the limits.
6367 (save-excursion
6368 (setq todo-state (org-get-todo-state))
6369 (setq donep (member todo-state org-done-keywords))
6370 (if (and donep org-agenda-skip-timestamp-if-done)
6371 (throw :skip t))
6372 (setq marker (org-agenda-new-marker (point))
6373 category (org-get-category))
6374 (if (not (re-search-backward org-outline-regexp-bol nil t))
6375 (throw :skip nil)
6376 (goto-char (match-beginning 0))
6377 (setq hdmarker (org-agenda-new-marker (point))
6378 inherited-tags
6379 (or (eq org-agenda-show-inherited-tags 'always)
6380 (and (listp org-agenda-show-inherited-tags)
6381 (memq 'agenda org-agenda-show-inherited-tags))
6382 (and (eq org-agenda-show-inherited-tags t)
6383 (or (eq org-agenda-use-tag-inheritance t)
6384 (memq 'agenda org-agenda-use-tag-inheritance))))
6386 tags (org-get-tags-at nil (not inherited-tags)))
6387 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6388 (looking-at "\\*+[ \t]+\\(.*\\)")
6389 (setq head (match-string 1))
6390 (let ((remove-re
6391 (if org-agenda-remove-timeranges-from-blocks
6392 (concat
6393 "<" (regexp-quote s1) ".*?>"
6394 "--"
6395 "<" (regexp-quote s2) ".*?>")
6396 nil)))
6397 (setq txt (org-agenda-format-item
6398 (format
6399 (nth (if (= d1 d2) 0 1)
6400 org-agenda-timerange-leaders)
6401 (1+ (- d0 d1)) (1+ (- d2 d1)))
6402 head level category tags
6403 (cond ((and (= d1 d0) (= d2 d0))
6404 (concat "<" start-time ">--<" end-time ">"))
6405 ((= d1 d0)
6406 (concat "<" start-time ">"))
6407 ((= d2 d0)
6408 (concat "<" end-time ">")))
6409 remove-re))))
6410 (org-add-props txt props
6411 'org-marker marker 'org-hd-marker hdmarker
6412 'type "block" 'date date
6413 'level level
6414 'todo-state todo-state
6415 'priority (org-get-priority txt))
6416 (push txt ee))))
6417 (goto-char pos)))
6418 ;; Sort the entries by expiration date.
6419 (nreverse ee)))
6421 ;;; Agenda presentation and sorting
6423 (defvar org-prefix-has-time nil
6424 "A flag, set by `org-compile-prefix-format'.
6425 The flag is set if the currently compiled format contains a `%t'.")
6426 (defvar org-prefix-has-tag nil
6427 "A flag, set by `org-compile-prefix-format'.
6428 The flag is set if the currently compiled format contains a `%T'.")
6429 (defvar org-prefix-has-effort nil
6430 "A flag, set by `org-compile-prefix-format'.
6431 The flag is set if the currently compiled format contains a `%e'.")
6432 (defvar org-prefix-has-breadcrumbs nil
6433 "A flag, set by `org-compile-prefix-format'.
6434 The flag is set if the currently compiled format contains a `%b'.")
6435 (defvar org-prefix-category-length nil
6436 "Used by `org-compile-prefix-format' to remember the category field width.")
6437 (defvar org-prefix-category-max-length nil
6438 "Used by `org-compile-prefix-format' to remember the category field width.")
6440 (defun org-agenda-get-category-icon (category)
6441 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6442 (dolist (entry org-agenda-category-icon-alist)
6443 (when (string-match-p (car entry) category)
6444 (if (listp (cadr entry))
6445 (cl-return (cadr entry))
6446 (cl-return (apply #'create-image (cdr entry)))))))
6448 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6449 remove-re habitp)
6450 "Format TXT to be inserted into the agenda buffer.
6451 In particular, add the prefix and corresponding text properties.
6453 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6454 LEVEL may be a string to replace the `%l' specifier.
6455 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6456 category taken from local variable or file name. It will replace the `%c'
6457 specifier in the format.
6458 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6459 TXT for sorting of this entry, and for the `%t' specifier in the format.
6460 When DOTIME is a string, this string is searched for a time before TXT is.
6461 TAGS can be the tags of the headline.
6462 Any match of REMOVE-RE will be removed from TXT."
6463 ;; We keep the org-prefix-* variable values along with a compiled
6464 ;; formatter, so that multiple agendas existing at the same time do
6465 ;; not step on each other toes.
6467 ;; It was inconvenient to make these variables buffer local in
6468 ;; Agenda buffers, because this function expects to be called with
6469 ;; the buffer where item comes from being current, and not agenda
6470 ;; buffer
6471 (let* ((bindings (car org-prefix-format-compiled))
6472 (formatter (cadr org-prefix-format-compiled)))
6473 (cl-loop for (var value) in bindings
6474 do (set var value))
6475 (save-match-data
6476 ;; Diary entries sometimes have extra whitespace at the beginning
6477 (setq txt (org-trim txt))
6479 ;; Fix the tags part in txt
6480 (setq txt (org-agenda-fix-displayed-tags
6481 txt tags
6482 org-agenda-show-inherited-tags
6483 org-agenda-hide-tags-regexp))
6485 (let* ((category (or category
6486 (if buffer-file-name
6487 (file-name-sans-extension
6488 (file-name-nondirectory buffer-file-name))
6489 "")))
6490 (category-icon (org-agenda-get-category-icon category))
6491 (category-icon (if category-icon
6492 (propertize " " 'display category-icon)
6493 ""))
6494 (effort (and (not (string= txt ""))
6495 (get-text-property 1 'effort txt)))
6496 ;; time, tag, effort are needed for the eval of the prefix format
6497 (tag (if tags (nth (1- (length tags)) tags) ""))
6498 time
6499 (ts (if dotime (concat
6500 (if (stringp dotime) dotime "")
6501 (and org-agenda-search-headline-for-time txt))))
6502 (time-of-day (and dotime (org-get-time-of-day ts)))
6503 stamp plain s0 s1 s2 rtn srp l
6504 duration breadcrumbs)
6505 (and (derived-mode-p 'org-mode) buffer-file-name
6506 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6507 (when (and dotime time-of-day)
6508 ;; Extract starting and ending time and move them to prefix
6509 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6510 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6511 (setq s0 (match-string 0 ts)
6512 srp (and stamp (match-end 3))
6513 s1 (match-string (if plain 1 2) ts)
6514 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6516 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6517 ;; them, we might want to remove them there to avoid duplication.
6518 ;; The user can turn this off with a variable.
6519 (if (and org-prefix-has-time
6520 org-agenda-remove-times-when-in-prefix (or stamp plain)
6521 (string-match (concat (regexp-quote s0) " *") txt)
6522 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6523 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6524 (= (match-beginning 0) 0)
6526 (setq txt (replace-match "" nil nil txt))))
6527 ;; Normalize the time(s) to 24 hour
6528 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6529 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6531 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6532 (let (org-time-clocksum-use-effort-durations)
6533 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6534 (setq s2
6535 (org-minutes-to-clocksum-string
6536 (+ (org-hh:mm-string-to-minutes s1)
6537 org-agenda-default-appointment-duration)))))
6539 ;; Compute the duration
6540 (when s2
6541 (setq duration (- (org-hh:mm-string-to-minutes s2)
6542 (org-hh:mm-string-to-minutes s1)))))
6544 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6545 ;; Tags are in the string
6546 (if (or (eq org-agenda-remove-tags t)
6547 (and org-agenda-remove-tags
6548 org-prefix-has-tag))
6549 (setq txt (replace-match "" t t txt))
6550 (setq txt (replace-match
6551 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6552 (match-string 2 txt))
6553 t t txt))))
6555 (when remove-re
6556 (while (string-match remove-re txt)
6557 (setq txt (replace-match "" t t txt))))
6559 ;; Set org-heading property on `txt' to mark the start of the
6560 ;; heading.
6561 (add-text-properties 0 (length txt) '(org-heading t) txt)
6563 ;; Prepare the variables needed in the eval of the compiled format
6564 (if org-prefix-has-breadcrumbs
6565 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6566 (let ((s (org-display-outline-path nil nil "->" t)))
6567 (if (eq "" s) "" (concat s "->"))))))
6568 (setq time (cond (s2 (concat
6569 (org-agenda-time-of-day-to-ampm-maybe s1)
6570 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6571 (if org-agenda-timegrid-use-ampm " ")))
6572 (s1 (concat
6573 (org-agenda-time-of-day-to-ampm-maybe s1)
6574 (if org-agenda-timegrid-use-ampm
6575 "........ "
6576 "......")))
6577 (t ""))
6578 extra (or (and (not habitp) extra) "")
6579 category (if (symbolp category) (symbol-name category) category)
6580 level (or level ""))
6581 (if (string-match org-bracket-link-regexp category)
6582 (progn
6583 (setq l (if (match-end 3)
6584 (- (match-end 3) (match-beginning 3))
6585 (- (match-end 1) (match-beginning 1))))
6586 (when (< l (or org-prefix-category-length 0))
6587 (setq category (copy-sequence category))
6588 (org-add-props category nil
6589 'extra-space (make-string
6590 (- org-prefix-category-length l 1) ?\ ))))
6591 (if (and org-prefix-category-max-length
6592 (>= (length category) org-prefix-category-max-length))
6593 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6594 ;; Evaluate the compiled format
6595 (setq rtn (concat (eval formatter) txt))
6597 ;; And finally add the text properties
6598 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6599 (org-add-props rtn nil
6600 'org-category category
6601 'tags (mapcar 'org-downcase-keep-props tags)
6602 'org-highest-priority org-highest-priority
6603 'org-lowest-priority org-lowest-priority
6604 'time-of-day time-of-day
6605 'duration duration
6606 'breadcrumbs breadcrumbs
6607 'txt txt
6608 'level level
6609 'time time
6610 'extra extra
6611 'format org-prefix-format-compiled
6612 'dotime dotime)))))
6614 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6615 "Remove tags string from TXT, and add a modified list of tags.
6616 The modified list may contain inherited tags, and tags matched by
6617 `org-agenda-hide-tags-regexp' will be removed."
6618 (when (or add-inherited hide-re)
6619 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6620 (setq txt (substring txt 0 (match-beginning 0))))
6621 (setq tags
6622 (delq nil
6623 (mapcar (lambda (tg)
6624 (if (or (and hide-re (string-match hide-re tg))
6625 (and (not add-inherited)
6626 (get-text-property 0 'inherited tg)))
6628 tg))
6629 tags)))
6630 (when tags
6631 (let ((have-i (get-text-property 0 'inherited (car tags)))
6633 (setq txt (concat txt " :"
6634 (mapconcat
6635 (lambda (x)
6636 (setq i (get-text-property 0 'inherited x))
6637 (if (and have-i (not i))
6638 (progn
6639 (setq have-i nil)
6640 (concat ":" x))
6642 tags ":")
6643 (if have-i "::" ":"))))))
6644 txt)
6646 (defun org-downcase-keep-props (s)
6647 (let ((props (text-properties-at 0 s)))
6648 (setq s (downcase s))
6649 (add-text-properties 0 (length s) props s)
6652 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6654 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6655 "Add a time-grid for agenda items which need it.
6657 LIST is the list of agenda items formatted by `org-agenda-list'.
6658 NDAYS is the span of the current agenda view.
6659 TODAYP is t when the current agenda view is on today."
6660 (catch 'exit
6661 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6662 ((and todayp (member 'today (car org-agenda-time-grid))))
6663 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6664 ((member 'weekly (car org-agenda-time-grid)))
6665 (t (throw 'exit list)))
6666 (let* ((have (delq nil (mapcar
6667 (lambda (x) (get-text-property 1 'time-of-day x))
6668 list)))
6669 (string (nth 1 org-agenda-time-grid))
6670 (gridtimes (nth 2 org-agenda-time-grid))
6671 (req (car org-agenda-time-grid))
6672 (remove (member 'remove-match req))
6673 new time)
6674 (if (and (member 'require-timed req) (not have))
6675 ;; don't show empty grid
6676 (throw 'exit list))
6677 (while (setq time (pop gridtimes))
6678 (unless (and remove (member time have))
6679 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6680 (push (org-agenda-format-item
6681 nil string nil "" nil
6682 (concat (substring time 0 -2) ":" (substring time -2)))
6683 new)
6684 (put-text-property
6685 2 (length (car new)) 'face 'org-time-grid (car new))))
6686 (when (and todayp org-agenda-show-current-time-in-grid)
6687 (push (org-agenda-format-item
6688 nil org-agenda-current-time-string nil "" nil
6689 (format-time-string "%H:%M "))
6690 new)
6691 (put-text-property
6692 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6694 (if (member 'time-up org-agenda-sorting-strategy-selected)
6695 (append new list)
6696 (append list new)))))
6698 (defun org-compile-prefix-format (key)
6699 "Compile the prefix format into a Lisp form that can be evaluated.
6700 The resulting form and associated variable bindings is returned
6701 and stored in the variable `org-prefix-format-compiled'."
6702 (setq org-prefix-has-time nil
6703 org-prefix-has-tag nil
6704 org-prefix-category-length nil
6705 org-prefix-has-effort nil
6706 org-prefix-has-breadcrumbs nil)
6707 (let ((s (cond
6708 ((stringp org-agenda-prefix-format)
6709 org-agenda-prefix-format)
6710 ((assq key org-agenda-prefix-format)
6711 (cdr (assq key org-agenda-prefix-format)))
6712 (t " %-12:c%?-12t% s")))
6713 (start 0)
6714 varform vars var e c f opt)
6715 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6716 s start)
6717 (setq var (or (cdr (assoc (match-string 4 s)
6718 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6719 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6720 'eval)
6721 c (or (match-string 3 s) "")
6722 opt (match-beginning 1)
6723 start (1+ (match-beginning 0)))
6724 (if (equal var 'time) (setq org-prefix-has-time t))
6725 (if (equal var 'tag) (setq org-prefix-has-tag t))
6726 (if (equal var 'effort) (setq org-prefix-has-effort t))
6727 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6728 (setq f (concat "%" (match-string 2 s) "s"))
6729 (when (equal var 'category)
6730 (setq org-prefix-category-length
6731 (floor (abs (string-to-number (match-string 2 s)))))
6732 (setq org-prefix-category-max-length
6733 (let ((x (match-string 2 s)))
6734 (save-match-data
6735 (if (string-match "\\.[0-9]+" x)
6736 (string-to-number (substring (match-string 0 x) 1)))))))
6737 (if (eq var 'eval)
6738 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6739 (if opt
6740 (setq varform
6741 `(if (or (equal "" ,var) (equal nil ,var))
6743 (format ,f (concat ,var ,c))))
6744 (setq varform
6745 `(format ,f (if (or (equal ,var "")
6746 (equal ,var nil)) ""
6747 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6748 (setq s (replace-match "%s" t nil s))
6749 (push varform vars))
6750 (setq vars (nreverse vars))
6751 (with-current-buffer (or org-agenda-buffer (current-buffer))
6752 (setq org-prefix-format-compiled
6753 (list
6754 `((org-prefix-has-time ,org-prefix-has-time)
6755 (org-prefix-has-tag ,org-prefix-has-tag)
6756 (org-prefix-category-length ,org-prefix-category-length)
6757 (org-prefix-has-effort ,org-prefix-has-effort)
6758 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6759 `(format ,s ,@vars))))))
6761 (defun org-set-sorting-strategy (key)
6762 (if (symbolp (car org-agenda-sorting-strategy))
6763 ;; the old format
6764 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6765 (setq org-agenda-sorting-strategy-selected
6766 (or (cdr (assq key org-agenda-sorting-strategy))
6767 (cdr (assq 'agenda org-agenda-sorting-strategy))
6768 '(time-up category-keep priority-down)))))
6770 (defun org-get-time-of-day (s &optional string mod24)
6771 "Check string S for a time of day.
6772 If found, return it as a military time number between 0 and 2400.
6773 If not found, return nil.
6774 The optional STRING argument forces conversion into a 5 character wide string
6775 HH:MM."
6776 (save-match-data
6777 (when
6778 (and
6779 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6780 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6781 (not (eq (get-text-property 1 'face s) 'org-link)))
6782 (let* ((h (string-to-number (match-string 1 s)))
6783 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6784 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6785 (am-p (equal ampm "am"))
6786 (h1 (cond ((not ampm) h)
6787 ((= h 12) (if am-p 0 12))
6788 (t (+ h (if am-p 0 12)))))
6789 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6790 (mod h1 24) h1))
6791 (t0 (+ (* 100 h2) m))
6792 (t1 (concat (if (>= h1 24) "+" " ")
6793 (if (and org-agenda-time-leading-zero
6794 (< t0 1000)) "0" "")
6795 (if (< t0 100) "0" "")
6796 (if (< t0 10) "0" "")
6797 (int-to-string t0))))
6798 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6800 (defvar org-agenda-before-sorting-filter-function nil
6801 "Function to be applied to agenda items prior to sorting.
6802 Prior to sorting also means just before they are inserted into the agenda.
6804 To aid sorting, you may revisit the original entries and add more text
6805 properties which will later be used by the sorting functions.
6807 The function should take a string argument, an agenda line.
6808 It has access to the text properties in that line, which contain among
6809 other things, the property `org-hd-marker' that points to the entry
6810 where the line comes from. Note that not all lines going into the agenda
6811 have this property, only most.
6813 The function should return the modified string. It is probably best
6814 to ONLY change text properties.
6816 You can also use this function as a filter, by returning nil for lines
6817 you don't want to have in the agenda at all. For this application, you
6818 could bind the variable in the options section of a custom command.")
6820 (defun org-agenda-finalize-entries (list &optional type)
6821 "Sort, limit and concatenate the LIST of agenda items.
6822 The optional argument TYPE tells the agenda type."
6823 (let ((max-effort (cond ((listp org-agenda-max-effort)
6824 (cdr (assoc type org-agenda-max-effort)))
6825 (t org-agenda-max-effort)))
6826 (max-todo (cond ((listp org-agenda-max-todos)
6827 (cdr (assoc type org-agenda-max-todos)))
6828 (t org-agenda-max-todos)))
6829 (max-tags (cond ((listp org-agenda-max-tags)
6830 (cdr (assoc type org-agenda-max-tags)))
6831 (t org-agenda-max-tags)))
6832 (max-entries (cond ((listp org-agenda-max-entries)
6833 (cdr (assoc type org-agenda-max-entries)))
6834 (t org-agenda-max-entries))))
6835 (when org-agenda-before-sorting-filter-function
6836 (setq list
6837 (delq nil
6838 (mapcar
6839 org-agenda-before-sorting-filter-function list))))
6840 (setq list (mapcar 'org-agenda-highlight-todo list)
6841 list (mapcar 'identity (sort list 'org-entries-lessp)))
6842 (when max-effort
6843 (setq list (org-agenda-limit-entries
6844 list 'effort-minutes max-effort
6845 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6846 32767 -1))))))
6847 (when max-todo
6848 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6849 (when max-tags
6850 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6851 (when max-entries
6852 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6853 (mapconcat 'identity list "\n")))
6855 (defun org-agenda-limit-entries (list prop limit &optional fn)
6856 "Limit the number of agenda entries."
6857 (let ((include (and limit (< limit 0))))
6858 (if limit
6859 (let ((fun (or fn (lambda (p) (if p 1))))
6860 (lim 0))
6861 (delq nil
6862 (mapcar
6863 (lambda (e)
6864 (let ((pval (funcall
6865 fun (get-text-property (1- (length e))
6866 prop e))))
6867 (if pval (setq lim (+ lim pval)))
6868 (cond ((and pval (<= lim (abs limit))) e)
6869 ((and include (not pval)) e))))
6870 list)))
6871 list)))
6873 (defun org-agenda-limit-interactively (remove)
6874 "In agenda, interactively limit entries to various maximums."
6875 (interactive "P")
6876 (if remove
6877 (progn (setq org-agenda-max-entries nil
6878 org-agenda-max-todos nil
6879 org-agenda-max-tags nil
6880 org-agenda-max-effort nil)
6881 (org-agenda-redo))
6882 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6883 (msg (cond ((= max ?E) "How many minutes? ")
6884 ((= max ?e) "How many entries? ")
6885 ((= max ?t) "How many TODO entries? ")
6886 ((= max ?T) "How many tagged entries? ")
6887 (t (user-error "Wrong input"))))
6888 (num (string-to-number (read-from-minibuffer msg))))
6889 (cond ((equal max ?e)
6890 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6891 ((equal max ?t)
6892 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6893 ((equal max ?T)
6894 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6895 ((equal max ?E)
6896 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6897 (org-agenda-fit-window-to-buffer))
6899 (defun org-agenda-highlight-todo (x)
6900 (let ((org-done-keywords org-done-keywords-for-agenda)
6901 (case-fold-search nil)
6903 (if (eq x 'line)
6904 (save-excursion
6905 (beginning-of-line 1)
6906 (setq re (org-get-at-bol 'org-todo-regexp))
6907 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6908 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6909 (add-text-properties (match-beginning 0) (match-end 1)
6910 (list 'face (org-get-todo-face 1)))
6911 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6912 (delete-region (match-beginning 1) (1- (match-end 0)))
6913 (goto-char (match-beginning 1))
6914 (insert (format org-agenda-todo-keyword-format s)))))
6915 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6916 (setq re (get-text-property 0 'org-todo-regexp x))
6917 (when (and re
6918 ;; Test `pl' because if there's no heading content,
6919 ;; there's no point matching to highlight. Note
6920 ;; that if we didn't test `pl' first, and there
6921 ;; happened to be no keyword from `org-todo-regexp'
6922 ;; on this heading line, then the `equal' comparison
6923 ;; afterwards would spuriously succeed in the case
6924 ;; where `pl' is nil -- causing an args-out-of-range
6925 ;; error when we try to add text properties to text
6926 ;; that isn't there.
6928 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6929 x pl) pl))
6930 (add-text-properties
6931 (or (match-end 1) (match-end 0)) (match-end 0)
6932 (list 'face (org-get-todo-face (match-string 2 x)))
6934 (when (match-end 1)
6935 (setq x (concat (substring x 0 (match-end 1))
6936 (format org-agenda-todo-keyword-format
6937 (match-string 2 x))
6938 (org-add-props " " (text-properties-at 0 x))
6939 (substring x (match-end 3)))))))
6940 x)))
6942 (defsubst org-cmp-values (a b property)
6943 "Compare the numeric value of text PROPERTY for string A and B."
6944 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6945 (pb (or (get-text-property (1- (length b)) property b) 0)))
6946 (cond ((> pa pb) +1)
6947 ((< pa pb) -1))))
6949 (defsubst org-cmp-effort (a b)
6950 "Compare the effort values of string A and B."
6951 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6952 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6953 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6954 (cond ((> ea eb) +1)
6955 ((< ea eb) -1))))
6957 (defsubst org-cmp-category (a b)
6958 "Compare the string values of categories of strings A and B."
6959 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6960 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6961 (cond ((string-lessp ca cb) -1)
6962 ((string-lessp cb ca) +1))))
6964 (defsubst org-cmp-todo-state (a b)
6965 "Compare the todo states of strings A and B."
6966 (let* ((ma (or (get-text-property 1 'org-marker a)
6967 (get-text-property 1 'org-hd-marker a)))
6968 (mb (or (get-text-property 1 'org-marker b)
6969 (get-text-property 1 'org-hd-marker b)))
6970 (fa (and ma (marker-buffer ma)))
6971 (fb (and mb (marker-buffer mb)))
6972 (todo-kwds
6973 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6974 (and fb (with-current-buffer fb org-todo-keywords-1))))
6975 (ta (or (get-text-property 1 'todo-state a) ""))
6976 (tb (or (get-text-property 1 'todo-state b) ""))
6977 (la (- (length (member ta todo-kwds))))
6978 (lb (- (length (member tb todo-kwds))))
6979 (donepa (member ta org-done-keywords-for-agenda))
6980 (donepb (member tb org-done-keywords-for-agenda)))
6981 (cond ((and donepa (not donepb)) -1)
6982 ((and (not donepa) donepb) +1)
6983 ((< la lb) -1)
6984 ((< lb la) +1))))
6986 (defsubst org-cmp-alpha (a b)
6987 "Compare the headlines, alphabetically."
6988 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6989 (plb (text-property-any 0 (length b) 'org-heading t b))
6990 (ta (and pla (substring a pla)))
6991 (tb (and plb (substring b plb))))
6992 (when pla
6993 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6994 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6995 (setq ta (substring ta (match-end 0))))
6996 (setq ta (downcase ta)))
6997 (when plb
6998 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6999 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
7000 (setq tb (substring tb (match-end 0))))
7001 (setq tb (downcase tb)))
7002 (cond ((not ta) +1)
7003 ((not tb) -1)
7004 ((string-lessp ta tb) -1)
7005 ((string-lessp tb ta) +1))))
7007 (defsubst org-cmp-tag (a b)
7008 "Compare the string values of the first tags of A and B."
7009 (let ((ta (car (last (get-text-property 1 'tags a))))
7010 (tb (car (last (get-text-property 1 'tags b)))))
7011 (cond ((not ta) +1)
7012 ((not tb) -1)
7013 ((string-lessp ta tb) -1)
7014 ((string-lessp tb ta) +1))))
7016 (defsubst org-cmp-time (a b)
7017 "Compare the time-of-day values of strings A and B."
7018 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7019 (ta (or (get-text-property 1 'time-of-day a) def))
7020 (tb (or (get-text-property 1 'time-of-day b) def)))
7021 (cond ((< ta tb) -1)
7022 ((< tb ta) +1))))
7024 (defsubst org-cmp-ts (a b type)
7025 "Compare the timestamps values of entries A and B.
7026 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7027 \"timestamp_ia\", compare within each of these type. When TYPE
7028 is the empty string, compare all timestamps without respect of
7029 their type."
7030 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7031 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7032 (get-text-property 1 'ts-date a))
7033 def))
7034 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7035 (get-text-property 1 'ts-date b))
7036 def)))
7037 (cond ((< ta tb) -1)
7038 ((< tb ta) +1))))
7040 (defsubst org-cmp-habit-p (a b)
7041 "Compare the todo states of strings A and B."
7042 (let ((ha (get-text-property 1 'org-habit-p a))
7043 (hb (get-text-property 1 'org-habit-p b)))
7044 (cond ((and ha (not hb)) -1)
7045 ((and (not ha) hb) +1))))
7047 (defun org-entries-lessp (a b)
7048 "Predicate for sorting agenda entries."
7049 ;; The following variables will be used when the form is evaluated.
7050 ;; So even though the compiler complains, keep them.
7051 (let* ((ss org-agenda-sorting-strategy-selected)
7052 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7053 (org-cmp-ts a b "")))
7054 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7055 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7056 (org-cmp-ts a b "scheduled")))
7057 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7058 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7059 (org-cmp-ts a b "deadline")))
7060 (deadline-down (if deadline-up (- deadline-up) nil))
7061 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7062 (org-cmp-ts a b "timestamp_ia")))
7063 (tsia-down (if tsia-up (- tsia-up) nil))
7064 (ts-up (and (org-em 'ts-up 'ts-down ss)
7065 (org-cmp-ts a b "timestamp")))
7066 (ts-down (if ts-up (- ts-up) nil))
7067 (time-up (and (org-em 'time-up 'time-down ss)
7068 (org-cmp-time a b)))
7069 (time-down (if time-up (- time-up) nil))
7070 (stats-up (and (org-em 'stats-up 'stats-down ss)
7071 (org-cmp-values a b 'org-stats)))
7072 (stats-down (if stats-up (- stats-up) nil))
7073 (priority-up (and (org-em 'priority-up 'priority-down ss)
7074 (org-cmp-values a b 'priority)))
7075 (priority-down (if priority-up (- priority-up) nil))
7076 (effort-up (and (org-em 'effort-up 'effort-down ss)
7077 (org-cmp-effort a b)))
7078 (effort-down (if effort-up (- effort-up) nil))
7079 (category-up (and (or (org-em 'category-up 'category-down ss)
7080 (memq 'category-keep ss))
7081 (org-cmp-category a b)))
7082 (category-down (if category-up (- category-up) nil))
7083 (category-keep (if category-up +1 nil))
7084 (tag-up (and (org-em 'tag-up 'tag-down ss)
7085 (org-cmp-tag a b)))
7086 (tag-down (if tag-up (- tag-up) nil))
7087 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7088 (org-cmp-todo-state a b)))
7089 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7090 (habit-up (and (org-em 'habit-up 'habit-down ss)
7091 (org-cmp-habit-p a b)))
7092 (habit-down (if habit-up (- habit-up) nil))
7093 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7094 (org-cmp-alpha a b)))
7095 (alpha-down (if alpha-up (- alpha-up) nil))
7096 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7097 user-defined-up user-defined-down)
7098 (if (and need-user-cmp org-agenda-cmp-user-defined
7099 (functionp org-agenda-cmp-user-defined))
7100 (setq user-defined-up
7101 (funcall org-agenda-cmp-user-defined a b)
7102 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7103 (cdr (assoc
7104 (eval (cons 'or org-agenda-sorting-strategy-selected))
7105 '((-1 . t) (1 . nil) (nil . nil))))))
7107 ;;; Agenda restriction lock
7109 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7110 "Overlay to mark the headline to which agenda commands are restricted.")
7111 (overlay-put org-agenda-restriction-lock-overlay
7112 'face 'org-agenda-restriction-lock)
7113 (overlay-put org-agenda-restriction-lock-overlay
7114 'help-echo "Agendas are currently limited to this subtree.")
7115 (delete-overlay org-agenda-restriction-lock-overlay)
7117 ;;;###autoload
7118 (defun org-agenda-set-restriction-lock (&optional type)
7119 "Set restriction lock for agenda, to current subtree or file.
7120 Restriction will be the file if TYPE is `file', or if type is the
7121 universal prefix \\='(4), or if the cursor is before the first headline
7122 in the file. Otherwise, restriction will be to the current subtree."
7123 (interactive "P")
7124 (org-agenda-remove-restriction-lock 'noupdate)
7125 (and (equal type '(4)) (setq type 'file))
7126 (setq type (cond
7127 (type type)
7128 ((org-at-heading-p) 'subtree)
7129 ((condition-case nil (org-back-to-heading t) (error nil))
7130 'subtree)
7131 (t 'file)))
7132 (if (eq type 'subtree)
7133 (progn
7134 (setq org-agenda-restrict (current-buffer))
7135 (setq org-agenda-overriding-restriction 'subtree)
7136 (put 'org-agenda-files 'org-restrict
7137 (list (buffer-file-name (buffer-base-buffer))))
7138 (org-back-to-heading t)
7139 (move-overlay org-agenda-restriction-lock-overlay
7140 (point)
7141 (if org-agenda-restriction-lock-highlight-subtree
7142 (save-excursion (org-end-of-subtree t t) (point))
7143 (point-at-eol)))
7144 (move-marker org-agenda-restrict-begin (point))
7145 (move-marker org-agenda-restrict-end
7146 (save-excursion (org-end-of-subtree t t)))
7147 (message "Locking agenda restriction to subtree"))
7148 (put 'org-agenda-files 'org-restrict
7149 (list (buffer-file-name (buffer-base-buffer))))
7150 (setq org-agenda-restrict nil)
7151 (setq org-agenda-overriding-restriction 'file)
7152 (move-marker org-agenda-restrict-begin nil)
7153 (move-marker org-agenda-restrict-end nil)
7154 (message "Locking agenda restriction to file"))
7155 (setq current-prefix-arg nil)
7156 (org-agenda-maybe-redo))
7158 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7159 "Remove the agenda restriction lock."
7160 (interactive "P")
7161 (delete-overlay org-agenda-restriction-lock-overlay)
7162 (delete-overlay org-speedbar-restriction-lock-overlay)
7163 (setq org-agenda-overriding-restriction nil)
7164 (setq org-agenda-restrict nil)
7165 (put 'org-agenda-files 'org-restrict nil)
7166 (move-marker org-agenda-restrict-begin nil)
7167 (move-marker org-agenda-restrict-end nil)
7168 (setq current-prefix-arg nil)
7169 (message "Agenda restriction lock removed")
7170 (or noupdate (org-agenda-maybe-redo)))
7172 (defun org-agenda-maybe-redo ()
7173 "If there is any window showing the agenda view, update it."
7174 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7175 org-agenda-buffer-name)
7177 (w0 (selected-window)))
7178 (when w
7179 (select-window w)
7180 (org-agenda-redo)
7181 (select-window w0)
7182 (if org-agenda-overriding-restriction
7183 (message "Agenda view shifted to new %s restriction"
7184 org-agenda-overriding-restriction)
7185 (message "Agenda restriction lock removed")))))
7187 ;;; Agenda commands
7189 (defun org-agenda-check-type (error &rest types)
7190 "Check if agenda buffer is of allowed type.
7191 If ERROR is non-nil, throw an error, otherwise just return nil.
7192 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7193 (if (not org-agenda-type)
7194 (error "No Org agenda currently displayed")
7195 (if (memq org-agenda-type types)
7197 (if error
7198 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7199 nil))))
7201 (defun org-agenda-Quit ()
7202 "Exit the agenda, killing the agenda buffer.
7203 Like `org-agenda-quit', but kill the buffer even when
7204 `org-agenda-sticky' is non-nil."
7205 (interactive)
7206 (org-agenda--quit))
7208 (defun org-agenda-quit ()
7209 "Exit the agenda.
7211 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7212 instead of killing it.
7214 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7215 the pre-agenda window configuration.
7217 When column view is active, exit column view instead of the
7218 agenda."
7219 (interactive)
7220 (org-agenda--quit org-agenda-sticky))
7222 (defun org-agenda--quit (&optional bury)
7223 (if org-agenda-columns-active
7224 (org-columns-quit)
7225 (let ((wconf org-agenda-pre-window-conf)
7226 (buf (current-buffer))
7227 (org-agenda-last-indirect-window
7228 (and (eq org-indirect-buffer-display 'other-window)
7229 org-agenda-last-indirect-buffer
7230 (get-buffer-window org-agenda-last-indirect-buffer))))
7231 (cond
7232 ((eq org-agenda-window-setup 'other-frame)
7233 (delete-frame))
7234 ((and org-agenda-restore-windows-after-quit
7235 wconf)
7236 ;; Maybe restore the pre-agenda window configuration. Reset
7237 ;; `org-agenda-pre-window-conf' before running
7238 ;; `set-window-configuration', which loses the current buffer.
7239 (setq org-agenda-pre-window-conf nil)
7240 (set-window-configuration wconf))
7242 (when org-agenda-last-indirect-window
7243 (delete-window org-agenda-last-indirect-window))
7244 (and (not (eq org-agenda-window-setup 'current-window))
7245 (not (one-window-p))
7246 (delete-window))))
7247 (if bury
7248 ;; Set the agenda buffer as the current buffer instead of
7249 ;; passing it as an argument to `bury-buffer' so that
7250 ;; `bury-buffer' removes it from the window.
7251 (with-current-buffer buf
7252 (bury-buffer))
7253 (kill-buffer buf)
7254 (setq org-agenda-archives-mode nil
7255 org-agenda-buffer nil)))))
7257 (defun org-agenda-exit ()
7258 "Exit the agenda, killing Org buffers loaded by the agenda.
7259 Like `org-agenda-Quit', but kill any buffers that were created by
7260 the agenda. Org buffers visited directly by the user will not be
7261 touched. Also, exit the agenda even if it is in column view."
7262 (interactive)
7263 (when org-agenda-columns-active
7264 (org-columns-quit))
7265 (org-release-buffers org-agenda-new-buffers)
7266 (setq org-agenda-new-buffers nil)
7267 (org-agenda-Quit))
7269 (defun org-agenda-kill-all-agenda-buffers ()
7270 "Kill all buffers in `org-agenda-mode'.
7271 This is used when toggling sticky agendas."
7272 (interactive)
7273 (let (blist)
7274 (dolist (buf (buffer-list))
7275 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7276 (push buf blist)))
7277 (mapc 'kill-buffer blist)))
7279 (defun org-agenda-execute (arg)
7280 "Execute another agenda command, keeping same window.
7281 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7282 in the agenda."
7283 (interactive "P")
7284 (let ((org-agenda-window-setup 'current-window))
7285 (org-agenda arg)))
7287 (defun org-agenda-redo (&optional all)
7288 "Rebuild possibly ALL agenda view(s) in the current buffer."
7289 (interactive "P")
7290 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7291 (cpa (unless (eq all t) current-prefix-arg))
7292 (org-agenda-doing-sticky-redo org-agenda-sticky)
7293 (org-agenda-sticky nil)
7294 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7295 org-agenda-buffer-name))
7296 (org-agenda-keep-modes t)
7297 (tag-filter org-agenda-tag-filter)
7298 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7299 (top-hl-filter org-agenda-top-headline-filter)
7300 (cat-filter org-agenda-category-filter)
7301 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7302 (re-filter org-agenda-regexp-filter)
7303 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7304 (effort-filter org-agenda-effort-filter)
7305 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7306 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7307 (cols org-agenda-columns-active)
7308 (line (org-current-line))
7309 (window-line (- line (org-current-line (window-start))))
7310 (lprops (get 'org-agenda-redo-command 'org-lprops))
7311 (redo-cmd (get-text-property p 'org-redo-cmd))
7312 (last-args (get-text-property p 'org-last-args))
7313 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7314 (org-agenda-overriding-cmd-arguments
7315 (unless (eq all t)
7316 (cond ((listp last-args)
7317 (cons (or cpa (car last-args)) (cdr last-args)))
7318 ((stringp last-args)
7319 last-args))))
7320 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7321 (put 'org-agenda-tag-filter :preset-filter nil)
7322 (put 'org-agenda-category-filter :preset-filter nil)
7323 (put 'org-agenda-regexp-filter :preset-filter nil)
7324 (put 'org-agenda-effort-filter :preset-filter nil)
7325 (and cols (org-columns-quit))
7326 (message "Rebuilding agenda buffer...")
7327 (if series-redo-cmd
7328 (eval series-redo-cmd)
7329 (org-let lprops redo-cmd))
7330 (setq org-agenda-undo-list nil
7331 org-agenda-pending-undo-list nil
7332 org-agenda-tag-filter tag-filter
7333 org-agenda-category-filter cat-filter
7334 org-agenda-regexp-filter re-filter
7335 org-agenda-effort-filter effort-filter
7336 org-agenda-top-headline-filter top-hl-filter)
7337 (message "Rebuilding agenda buffer...done")
7338 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7339 (put 'org-agenda-category-filter :preset-filter cat-preset)
7340 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7341 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7342 (let ((tag (or tag-filter tag-preset))
7343 (cat (or cat-filter cat-preset))
7344 (effort (or effort-filter effort-preset))
7345 (re (or re-filter re-preset)))
7346 (when tag (org-agenda-filter-apply tag 'tag t))
7347 (when cat (org-agenda-filter-apply cat 'category))
7348 (when effort (org-agenda-filter-apply effort 'effort))
7349 (when re (org-agenda-filter-apply re 'regexp)))
7350 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7351 (and cols (called-interactively-p 'any) (org-agenda-columns))
7352 (org-goto-line line)
7353 (recenter window-line)))
7355 (defvar org-global-tags-completion-table nil)
7356 (defvar org-agenda-filter-form nil)
7357 (defvar org-agenda-filtered-by-category nil)
7359 (defun org-agenda-filter-by-category (strip)
7360 "Filter lines in the agenda buffer that have a specific category.
7361 The category is that of the current line.
7362 Without prefix argument, keep only the lines of that category.
7363 With a prefix argument, exclude the lines of that category.
7365 (interactive "P")
7366 (if (and org-agenda-filtered-by-category
7367 org-agenda-category-filter)
7368 (org-agenda-filter-show-all-cat)
7369 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7370 (cond
7371 ((and cat strip)
7372 (org-agenda-filter-apply
7373 (push (concat "-" cat) org-agenda-category-filter) 'category))
7374 (cat
7375 (org-agenda-filter-apply
7376 (setq org-agenda-category-filter
7377 (list (concat "+" cat))) 'category))
7378 (t (error "No category at point"))))))
7380 (defun org-find-top-headline (&optional pos)
7381 "Find the topmost parent headline and return it."
7382 (save-excursion
7383 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7384 (if pos (goto-char pos))
7385 ;; Skip up to the topmost parent
7386 (while (ignore-errors (outline-up-heading 1) t))
7387 (ignore-errors
7388 (nth 4 (org-heading-components))))))
7390 (defvar org-agenda-filtered-by-top-headline nil)
7391 (defun org-agenda-filter-by-top-headline (strip)
7392 "Keep only those lines that are descendants from the same top headline.
7393 The top headline is that of the current line."
7394 (interactive "P")
7395 (if org-agenda-filtered-by-top-headline
7396 (progn
7397 (setq org-agenda-filtered-by-top-headline nil
7398 org-agenda-top-headline-filter nil)
7399 (org-agenda-filter-show-all-top-filter))
7400 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7401 (if toph (org-agenda-filter-top-headline-apply toph strip)
7402 (error "No top-level headline at point")))))
7404 (defvar org-agenda-regexp-filter nil)
7405 (defun org-agenda-filter-by-regexp (strip)
7406 "Filter agenda entries by a regular expression.
7407 Regexp filters are cumulative.
7408 With no prefix argument, keep entries matching the regexp.
7409 With one prefix argument, filter out entries matching the regexp.
7410 With two prefix arguments, remove the regexp filters."
7411 (interactive "P")
7412 (if (not (equal strip '(16)))
7413 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7414 (read-from-minibuffer
7415 (if (equal strip '(4))
7416 "Filter out entries matching regexp: "
7417 "Narrow to entries matching regexp: ")))))
7418 (push flt org-agenda-regexp-filter)
7419 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7420 (org-agenda-filter-show-all-re)
7421 (message "Regexp filter removed")))
7423 (defvar org-agenda-effort-filter nil)
7424 (defun org-agenda-filter-by-effort (strip)
7425 "Filter agenda entries by effort.
7426 With no prefix argument, keep entries matching the effort condition.
7427 With one prefix argument, filter out entries matching the condition.
7428 With two prefix arguments, remove the effort filters."
7429 (interactive "P")
7430 (cond ((member strip '(nil 4))
7431 (let ((efforts (org-split-string
7432 (or (cdr (assoc (concat org-effort-property "_ALL")
7433 org-global-properties))
7434 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7435 "")))
7436 (eff -1)
7437 effort-prompt op)
7438 (while (not (member op '(?< ?> ?=)))
7439 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7440 (cl-loop for i from 0 to 9 do
7441 (setq effort-prompt
7442 (concat
7443 effort-prompt " ["
7444 (if (= i 9) "0" (int-to-string (1+ i)))
7445 "]" (nth i efforts))))
7446 (message "Effort %s%s" (char-to-string op) effort-prompt)
7447 (while (or (< eff 0) (> eff 9))
7448 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7449 (setq org-agenda-effort-filter
7450 (list (concat (if strip "-" "+")
7451 (char-to-string op) (nth (1- eff) efforts))))
7452 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7453 (t (org-agenda-filter-show-all-effort)
7454 (message "Effort filter removed"))))
7456 (defun org-agenda-filter-remove-all ()
7457 "Remove all filters from the current agenda buffer."
7458 (interactive)
7459 (when org-agenda-tag-filter
7460 (org-agenda-filter-show-all-tag))
7461 (when org-agenda-category-filter
7462 (org-agenda-filter-show-all-cat))
7463 (when org-agenda-regexp-filter
7464 (org-agenda-filter-show-all-re))
7465 (when org-agenda-top-headline-filter
7466 (org-agenda-filter-show-all-top-filter))
7467 (when org-agenda-effort-filter
7468 (org-agenda-filter-show-all-effort))
7469 (org-agenda-finalize))
7471 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7472 "Keep only those lines in the agenda buffer that have a specific tag.
7473 The tag is selected with its fast selection letter, as
7474 configured. With a single \\[universal-argument] prefix ARG,
7475 exclude the agenda search. With a double \\[universal-argument]
7476 prefix ARG, filter the literal tag. I.e. don't filter on all its
7477 group members.
7479 A lisp caller can specify CHAR. EXCLUDE means that the new tag should be
7480 used to exclude the search - the interactive user can also press `-' or `+'
7481 to switch between filtering and excluding."
7482 (interactive "P")
7483 (let* ((alist org-tag-alist-for-agenda)
7484 (tag-chars (mapconcat
7485 (lambda (x) (if (and (not (symbolp (car x)))
7486 (cdr x))
7487 (char-to-string (cdr x))
7488 ""))
7489 org-tag-alist-for-agenda ""))
7490 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7491 (string-to-list tag-chars)))
7492 (exclude (or exclude (equal arg '(4))))
7493 (expand (not (equal arg '(16))))
7494 (inhibit-read-only t)
7495 (current org-agenda-tag-filter)
7496 a n tag)
7497 (unless char
7498 (while (not (memq char valid-char-list))
7499 (message
7500 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7501 (if exclude "Exclude" "Filter") tag-chars
7502 (if org-agenda-auto-exclude-function "[RET], " "")
7503 (if expand "" ", no grouptag expand"))
7504 (setq char (read-char-exclusive))
7505 ;; Excluding or filtering down
7506 (cond ((eq char ?-) (setq exclude t))
7507 ((eq char ?+) (setq exclude nil)))))
7508 (when (eq char ?\t)
7509 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7510 (setq-local org-global-tags-completion-table
7511 (org-global-tags-completion-table)))
7512 (let ((completion-ignore-case t))
7513 (setq tag (completing-read
7514 "Tag: " org-global-tags-completion-table))))
7515 (cond
7516 ((eq char ?\r)
7517 (org-agenda-filter-show-all-tag)
7518 (when org-agenda-auto-exclude-function
7519 (setq org-agenda-tag-filter nil)
7520 (dolist (tag (org-agenda-get-represented-tags))
7521 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7522 (if modifier
7523 (push modifier org-agenda-tag-filter))))
7524 (if (not (null org-agenda-tag-filter))
7525 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7526 ((eq char ?/)
7527 (org-agenda-filter-show-all-tag)
7528 (when (get 'org-agenda-tag-filter :preset-filter)
7529 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7530 ((eq char ?.)
7531 (setq org-agenda-tag-filter
7532 (mapcar (lambda(tag) (concat "+" tag))
7533 (org-get-at-bol 'tags)))
7534 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7535 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7536 ((or (eq char ?\s)
7537 (setq a (rassoc char alist))
7538 (and tag (setq a (cons tag nil))))
7539 (org-agenda-filter-show-all-tag)
7540 (setq tag (car a))
7541 (setq org-agenda-tag-filter
7542 (cons (concat (if exclude "-" "+") tag)
7543 current))
7544 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7545 (t (error "Invalid tag selection character %c" char)))))
7547 (defun org-agenda-get-represented-tags ()
7548 "Get a list of all tags currently represented in the agenda."
7549 (let (p tags)
7550 (save-excursion
7551 (goto-char (point-min))
7552 (while (setq p (next-single-property-change (point) 'tags))
7553 (goto-char p)
7554 (mapc (lambda (x) (add-to-list 'tags x))
7555 (get-text-property (point) 'tags))))
7556 tags))
7559 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7560 "Create the form that tests a line for agenda filter. Optional
7561 argument EXPAND can be used for the TYPE tag and will expand the
7562 tags in the FILTER if any of the tags in FILTER are grouptags."
7563 (let (f f1)
7564 (cond
7565 ;; Tag filter
7566 ((eq type 'tag)
7567 (setq filter
7568 (delete-dups
7569 (append (get 'org-agenda-tag-filter :preset-filter)
7570 filter)))
7571 (dolist (x filter)
7572 (let ((op (string-to-char x)))
7573 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7574 (setq x (list x)))
7575 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7576 (push f1 f))))
7577 ;; Category filter
7578 ((eq type 'category)
7579 (setq filter
7580 (delete-dups
7581 (append (get 'org-agenda-category-filter :preset-filter)
7582 filter)))
7583 (dolist (x filter)
7584 (if (equal "-" (substring x 0 1))
7585 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7586 (setq f1 (list 'equal (substring x 1) 'cat)))
7587 (push f1 f)))
7588 ;; Regexp filter
7589 ((eq type 'regexp)
7590 (setq filter
7591 (delete-dups
7592 (append (get 'org-agenda-regexp-filter :preset-filter)
7593 filter)))
7594 (dolist (x filter)
7595 (if (equal "-" (substring x 0 1))
7596 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7597 (setq f1 (list 'string-match (substring x 1) 'txt)))
7598 (push f1 f)))
7599 ;; Effort filter
7600 ((eq type 'effort)
7601 (setq filter
7602 (delete-dups
7603 (append (get 'org-agenda-effort-filter :preset-filter)
7604 filter)))
7605 (dolist (x filter)
7606 (push (org-agenda-filter-effort-form x) f))))
7607 (cons 'and (nreverse f))))
7609 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7610 "Return a form associated to tag-expression TAGS.
7611 Build a form testing a line for agenda filter for
7612 tag-expressions. OP is an operator of type CHAR that allows the
7613 function to set the right switches in the returned form."
7614 (let (form)
7615 ;; Any of the expressions can match if OP is +, all must match if
7616 ;; the operator is -.
7617 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7618 (let* ((tag (substring x 1))
7619 (f (cond
7620 ((string= "" tag) '(not tags))
7621 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7622 ;; TAG is a regexp.
7623 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7624 (t (list 'member (downcase tag) 'tags)))))
7625 (push (if (eq op ?-) (list 'not f) f) form)))))
7627 (defun org-agenda-filter-effort-form (e)
7628 "Return the form to compare the effort of the current line with what E says.
7629 E looks like \"+<2:25\"."
7630 (let (op)
7631 (setq e (substring e 1))
7632 (setq op (string-to-char e) e (substring e 1))
7633 (setq op (cond ((equal op ?<) '<=)
7634 ((equal op ?>) '>=)
7635 ((equal op ??) op)
7636 (t '=)))
7637 (list 'org-agenda-compare-effort (list 'quote op)
7638 (org-duration-string-to-minutes e))))
7640 (defun org-agenda-compare-effort (op value)
7641 "Compare the effort of the current line with VALUE, using OP.
7642 If the line does not have an effort defined, return nil."
7643 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7644 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7645 value)))
7647 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7648 "Expand group tags in FILTER for the agenda.
7649 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7650 (if org-group-tags
7651 (let ((case-fold-search t) rtn)
7652 (mapc
7653 (lambda (f)
7654 (let (f0 dir)
7655 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7656 (setq dir (match-string 1 f) f0 (match-string 2 f))
7657 (setq dir (if no-operator "" "+") f0 f))
7658 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7659 (org-tags-expand f0 t t))
7660 rtn))))
7661 filter)
7662 (reverse rtn))
7663 filter))
7665 (defun org-agenda-filter-apply (filter type &optional expand)
7666 "Set FILTER as the new agenda filter and apply it. Optional
7667 argument EXPAND can be used for the TYPE tag and will expand the
7668 tags in the FILTER if any of the tags in FILTER are grouptags."
7669 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7670 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7671 (let (tags cat txt)
7672 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7673 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7674 ;; category is used as the filter:
7675 (setq org-agenda-filtered-by-category
7676 (and (eq type 'category)
7677 (not (equal (substring (car filter) 0 1) "-"))))
7678 (org-agenda-set-mode-name)
7679 (save-excursion
7680 (goto-char (point-min))
7681 (while (not (eobp))
7682 (if (org-get-at-bol 'org-marker)
7683 (progn
7684 (setq tags (org-get-at-bol 'tags)
7685 cat (org-get-at-eol 'org-category 1)
7686 txt (org-get-at-bol 'txt))
7687 (if (not (eval org-agenda-filter-form))
7688 (org-agenda-filter-hide-line type))
7689 (beginning-of-line 2))
7690 (beginning-of-line 2))))
7691 (if (get-char-property (point) 'invisible)
7692 (ignore-errors (org-agenda-previous-line)))))
7694 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7695 "Filter by top headline HL."
7696 (org-agenda-set-mode-name)
7697 (save-excursion
7698 (goto-char (point-min))
7699 (while (not (eobp))
7700 (let* ((pos (org-get-at-bol 'org-hd-marker))
7701 (tophl (and pos (org-find-top-headline pos))))
7702 (if (and tophl (funcall (if negative 'identity 'not)
7703 (string= hl tophl)))
7704 (org-agenda-filter-hide-line 'top-headline)))
7705 (beginning-of-line 2)))
7706 (if (get-char-property (point) 'invisible)
7707 (org-agenda-previous-line))
7708 (setq org-agenda-top-headline-filter hl
7709 org-agenda-filtered-by-top-headline t))
7711 (defun org-agenda-filter-hide-line (type)
7712 "Hide lines with TYPE in the agenda buffer."
7713 (let* ((b (max (point-min) (1- (point-at-bol))))
7714 (e (point-at-eol)))
7715 (let ((inhibit-read-only t))
7716 (add-text-properties
7717 b e `(invisible org-filtered org-filter-type ,type)))))
7719 (defun org-agenda-remove-filter (type)
7720 (interactive)
7721 "Remove filter of type TYPE from the agenda buffer."
7722 (save-excursion
7723 (goto-char (point-min))
7724 (let ((inhibit-read-only t) pos)
7725 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7726 (goto-char pos)
7727 (remove-text-properties
7728 (point) (next-single-property-change (point) 'org-filter-type)
7729 `(invisible org-filtered org-filter-type ,type))))
7730 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7731 (setq org-agenda-filter-form nil)
7732 (org-agenda-set-mode-name)
7733 (org-agenda-finalize)))
7735 (defun org-agenda-filter-show-all-tag nil
7736 (org-agenda-remove-filter 'tag))
7737 (defun org-agenda-filter-show-all-re nil
7738 (org-agenda-remove-filter 'regexp))
7739 (defun org-agenda-filter-show-all-effort nil
7740 (org-agenda-remove-filter 'effort))
7741 (defun org-agenda-filter-show-all-cat nil
7742 (org-agenda-remove-filter 'category))
7743 (defun org-agenda-filter-show-all-top-filter nil
7744 (org-agenda-remove-filter 'top-headline))
7746 (defun org-agenda-manipulate-query-add ()
7747 "Manipulate the query by adding a search term with positive selection.
7748 Positive selection means the term must be matched for selection of an entry."
7749 (interactive)
7750 (org-agenda-manipulate-query ?\[))
7751 (defun org-agenda-manipulate-query-subtract ()
7752 "Manipulate the query by adding a search term with negative selection.
7753 Negative selection means term must not be matched for selection of an entry."
7754 (interactive)
7755 (org-agenda-manipulate-query ?\]))
7756 (defun org-agenda-manipulate-query-add-re ()
7757 "Manipulate the query by adding a search regexp with positive selection.
7758 Positive selection means the regexp must match for selection of an entry."
7759 (interactive)
7760 (org-agenda-manipulate-query ?\{))
7761 (defun org-agenda-manipulate-query-subtract-re ()
7762 "Manipulate the query by adding a search regexp with negative selection.
7763 Negative selection means regexp must not match for selection of an entry."
7764 (interactive)
7765 (org-agenda-manipulate-query ?\}))
7766 (defun org-agenda-manipulate-query (char)
7767 (cond
7768 ((memq org-agenda-type '(timeline agenda))
7769 (let ((org-agenda-include-inactive-timestamps t))
7770 (org-agenda-redo))
7771 (message "Display now includes inactive timestamps as well"))
7772 ((eq org-agenda-type 'search)
7773 (org-add-to-string
7774 'org-agenda-query-string
7775 (if org-agenda-last-search-view-search-was-boolean
7776 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7777 (?\{ . " +{}") (?\} . " -{}"))))
7778 " "))
7779 (setq org-agenda-redo-command
7780 (list 'org-search-view
7781 (car (get-text-property (min (1- (point-max)) (point))
7782 'org-last-args))
7783 org-agenda-query-string
7784 (+ (length org-agenda-query-string)
7785 (if (member char '(?\{ ?\})) 0 1))))
7786 (set-register org-agenda-query-register org-agenda-query-string)
7787 (let ((org-agenda-overriding-arguments
7788 (cdr org-agenda-redo-command)))
7789 (org-agenda-redo)))
7790 (t (error "Cannot manipulate query for %s-type agenda buffers"
7791 org-agenda-type))))
7793 (defun org-add-to-string (var string)
7794 (set var (concat (symbol-value var) string)))
7796 (defun org-agenda-goto-date (span)
7797 "Jump to DATE in agenda."
7798 (interactive "P")
7799 (let* ((org-read-date-prefer-future
7800 (eval org-agenda-jump-prefer-future))
7801 (date (org-read-date))
7802 (day (time-to-days (org-time-string-to-time date)))
7803 (org-agenda-sticky-orig org-agenda-sticky)
7804 (org-agenda-buffer-tmp-name (buffer-name))
7805 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7806 (0-arg (or current-prefix-arg (car args)))
7807 (2-arg (nth 2 args))
7808 (with-hour-p (nth 4 org-agenda-redo-command))
7809 (newcmd (list 'org-agenda-list 0-arg date
7810 (org-agenda-span-to-ndays
7811 2-arg (org-time-string-to-absolute date))
7812 with-hour-p))
7813 (newargs (cdr newcmd))
7814 (inhibit-read-only t)
7815 org-agenda-sticky)
7816 (if (not (org-agenda-check-type t 'agenda))
7817 (error "Not available in non-agenda views")
7818 (add-text-properties (point-min) (point-max)
7819 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7820 (org-agenda-redo)
7821 (goto-char (point-min))
7822 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7823 (save-excursion (move-beginning-of-line 2) (eobp))))
7824 (move-beginning-of-line 2))
7825 (setq org-agenda-sticky org-agenda-sticky-orig
7826 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7828 (defun org-agenda-goto-today ()
7829 "Go to today."
7830 (interactive)
7831 (org-agenda-check-type t 'timeline 'agenda)
7832 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7833 (curspan (nth 2 args))
7834 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7835 (cond
7836 (tdpos (goto-char tdpos))
7837 ((eq org-agenda-type 'agenda)
7838 (let* ((sd (org-agenda-compute-starting-span
7839 (org-today) (or curspan org-agenda-span)))
7840 (org-agenda-overriding-arguments args))
7841 (setf (nth 1 org-agenda-overriding-arguments) sd)
7842 (org-agenda-redo)
7843 (org-agenda-find-same-or-today-or-agenda)))
7844 (t (error "Cannot find today")))))
7846 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7847 (goto-char
7848 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7849 (text-property-any (point-min) (point-max) 'org-today t)
7850 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7851 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7852 (org-agenda-backward-block))
7853 (point-min))))
7855 (defun org-agenda-backward-block ()
7856 "Move backward by one agenda block."
7857 (interactive)
7858 (org-agenda-forward-block 'backward))
7860 (defun org-agenda-forward-block (&optional backward)
7861 "Move forward by one agenda block.
7862 When optional argument BACKWARD is set, go backward"
7863 (interactive)
7864 (cond ((not (derived-mode-p 'org-agenda-mode))
7865 (user-error
7866 "Cannot execute this command outside of org-agenda-mode buffers"))
7867 ((looking-at (if backward "\\`" "\\'"))
7868 (message "Already at the %s block" (if backward "first" "last")))
7869 (t (let ((pos (prog1 (point)
7870 (ignore-errors (if backward (backward-char 1)
7871 (move-end-of-line 1)))))
7872 (f (if backward
7873 'previous-single-property-change
7874 'next-single-property-change))
7875 moved dest)
7876 (while (and (setq dest (funcall
7877 f (point) 'org-agenda-structural-header))
7878 (not (get-text-property
7879 (point) 'org-agenda-structural-header)))
7880 (setq moved t)
7881 (goto-char dest))
7882 (if moved (move-beginning-of-line 1)
7883 (goto-char (if backward (point-min) (point-max)))
7884 (move-beginning-of-line 1)
7885 (message "No %s block" (if backward "previous" "further")))))))
7887 (defun org-agenda-later (arg)
7888 "Go forward in time by the current span.
7889 With prefix ARG, go forward that many times the current span."
7890 (interactive "p")
7891 (org-agenda-check-type t 'agenda)
7892 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7893 (span (or (nth 2 args) org-agenda-current-span))
7894 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7895 (greg (calendar-gregorian-from-absolute sd))
7896 (cnt (org-get-at-bol 'org-day-cnt))
7897 greg2)
7898 (cond
7899 ((numberp span)
7900 (setq sd (+ (* span arg) sd)))
7901 ((eq span 'day)
7902 (setq sd (+ arg sd)))
7903 ((eq span 'week)
7904 (setq sd (+ (* 7 arg) sd)))
7905 ((eq span 'fortnight)
7906 (setq sd (+ (* 14 arg) sd)))
7907 ((eq span 'month)
7908 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7909 sd (calendar-absolute-from-gregorian greg2))
7910 (setcar greg2 (1+ (car greg2))))
7911 ((eq span 'year)
7912 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7913 sd (calendar-absolute-from-gregorian greg2))
7914 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7916 (setq sd (+ (* span arg) sd))))
7917 (let ((org-agenda-overriding-cmd
7918 ;; `cmd' may have been set by `org-agenda-run-series' which
7919 ;; uses `org-agenda-overriding-cmd' to decide whether
7920 ;; overriding is allowed for `cmd'
7921 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7922 (org-agenda-overriding-arguments
7923 (list (car args) sd span)))
7924 (org-agenda-redo)
7925 (org-agenda-find-same-or-today-or-agenda cnt))))
7927 (defun org-agenda-earlier (arg)
7928 "Go backward in time by the current span.
7929 With prefix ARG, go backward that many times the current span."
7930 (interactive "p")
7931 (org-agenda-later (- arg)))
7933 (defun org-agenda-view-mode-dispatch ()
7934 "Call one of the view mode commands."
7935 (interactive)
7936 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7937 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7938 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7939 (pcase (read-char-exclusive)
7940 (?\ (call-interactively 'org-agenda-reset-view))
7941 (?d (call-interactively 'org-agenda-day-view))
7942 (?w (call-interactively 'org-agenda-week-view))
7943 (?t (call-interactively 'org-agenda-fortnight-view))
7944 (?m (call-interactively 'org-agenda-month-view))
7945 (?y (call-interactively 'org-agenda-year-view))
7946 (?l (call-interactively 'org-agenda-log-mode))
7947 (?L (org-agenda-log-mode '(4)))
7948 (?c (org-agenda-log-mode 'clockcheck))
7949 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7950 (?a (call-interactively 'org-agenda-archives-mode))
7951 (?A (org-agenda-archives-mode 'files))
7952 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7953 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7954 (?G (call-interactively 'org-agenda-toggle-time-grid))
7955 (?D (call-interactively 'org-agenda-toggle-diary))
7956 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7957 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7958 (org-agenda-check-type t 'timeline 'agenda)
7959 (org-agenda-redo))
7960 (message "Display now includes inactive timestamps as well"))
7961 (?q (message "Abort"))
7962 (key (user-error "Invalid key: %s" key))))
7964 (defun org-agenda-reset-view ()
7965 "Switch to default view for agenda."
7966 (interactive)
7967 (org-agenda-change-time-span org-agenda-span))
7968 (defun org-agenda-day-view (&optional day-of-month)
7969 "Switch to daily view for agenda.
7970 With argument DAY-OF-MONTH, switch to that day of the month."
7971 (interactive "P")
7972 (org-agenda-change-time-span 'day day-of-month))
7973 (defun org-agenda-week-view (&optional iso-week)
7974 "Switch to daily view for agenda.
7975 With argument ISO-WEEK, switch to the corresponding ISO week.
7976 If ISO-WEEK has more then 2 digits, only the last two encode the
7977 week. Any digits before this encode a year. So 200712 means
7978 week 12 of year 2007. Years in the range 1938-2037 can also be
7979 written as 2-digit years."
7980 (interactive "P")
7981 (org-agenda-change-time-span 'week iso-week))
7982 (defun org-agenda-fortnight-view (&optional iso-week)
7983 "Switch to daily view for agenda.
7984 With argument ISO-WEEK, switch to the corresponding ISO week.
7985 If ISO-WEEK has more then 2 digits, only the last two encode the
7986 week. Any digits before this encode a year. So 200712 means
7987 week 12 of year 2007. Years in the range 1938-2037 can also be
7988 written as 2-digit years."
7989 (interactive "P")
7990 (org-agenda-change-time-span 'fortnight iso-week))
7991 (defun org-agenda-month-view (&optional month)
7992 "Switch to monthly view for agenda.
7993 With argument MONTH, switch to that month."
7994 (interactive "P")
7995 (org-agenda-change-time-span 'month month))
7996 (defun org-agenda-year-view (&optional year)
7997 "Switch to yearly view for agenda.
7998 With argument YEAR, switch to that year.
7999 If MONTH has more then 2 digits, only the last two encode the
8000 month. Any digits before this encode a year. So 200712 means
8001 December year 2007. Years in the range 1938-2037 can also be
8002 written as 2-digit years."
8003 (interactive "P")
8004 (when year
8005 (setq year (org-small-year-to-year year)))
8006 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8007 (org-agenda-change-time-span 'year year)
8008 (error "Abort")))
8010 (defun org-agenda-change-time-span (span &optional n)
8011 "Change the agenda view to SPAN.
8012 SPAN may be `day', `week', `fortnight', `month', `year'."
8013 (org-agenda-check-type t 'agenda)
8014 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8015 (curspan (nth 2 args)))
8016 (if (and (not n) (equal curspan span))
8017 (error "Viewing span is already \"%s\"" span))
8018 (let* ((sd (or (org-get-at-bol 'day)
8019 (nth 1 args)
8020 org-starting-day))
8021 (sd (org-agenda-compute-starting-span sd span n))
8022 (org-agenda-overriding-cmd
8023 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8024 (org-agenda-overriding-arguments
8025 (list (car args) sd span)))
8026 (org-agenda-redo)
8027 (org-agenda-find-same-or-today-or-agenda))
8028 (org-agenda-set-mode-name)
8029 (message "Switched to %s view" span)))
8031 (defun org-agenda-compute-starting-span (sd span &optional n)
8032 "Compute starting date for agenda.
8033 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8034 is a cons cell with the starting date and the number of days,
8035 so that the date SD will be in that range."
8036 (let* ((greg (calendar-gregorian-from-absolute sd))
8037 (dg (nth 1 greg))
8038 (mg (car greg))
8039 (yg (nth 2 greg)))
8040 (cond
8041 ((eq span 'day)
8042 (when n
8043 (setq sd (+ (calendar-absolute-from-gregorian
8044 (list mg 1 yg))
8045 n -1))))
8046 ((or (eq span 'week) (eq span 'fortnight))
8047 (let* ((nt (calendar-day-of-week
8048 (calendar-gregorian-from-absolute sd)))
8049 (d (if org-agenda-start-on-weekday
8050 (- nt org-agenda-start-on-weekday)
8053 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8054 (when n
8055 (require 'cal-iso)
8056 (when (> n 99)
8057 (setq y1 (org-small-year-to-year (/ n 100))
8058 n (mod n 100)))
8059 (setq sd
8060 (calendar-iso-to-absolute
8061 (list n 1
8062 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8063 ((eq span 'month)
8064 (let (y1)
8065 (when (and n (> n 99))
8066 (setq y1 (org-small-year-to-year (/ n 100))
8067 n (mod n 100)))
8068 (setq sd (calendar-absolute-from-gregorian
8069 (list (or n mg) 1 (or y1 yg))))))
8070 ((eq span 'year)
8071 (setq sd (calendar-absolute-from-gregorian
8072 (list 1 1 (or n yg))))))
8073 sd))
8075 (defun org-agenda-next-date-line (&optional arg)
8076 "Jump to the next line indicating a date in agenda buffer."
8077 (interactive "p")
8078 (org-agenda-check-type t 'agenda 'timeline)
8079 (beginning-of-line 1)
8080 ;; This does not work if user makes date format that starts with a blank
8081 (if (looking-at "^\\S-") (forward-char 1))
8082 (if (not (re-search-forward "^\\S-" nil t arg))
8083 (progn
8084 (backward-char 1)
8085 (error "No next date after this line in this buffer")))
8086 (goto-char (match-beginning 0)))
8088 (defun org-agenda-previous-date-line (&optional arg)
8089 "Jump to the previous line indicating a date in agenda buffer."
8090 (interactive "p")
8091 (org-agenda-check-type t 'agenda 'timeline)
8092 (beginning-of-line 1)
8093 (if (not (re-search-backward "^\\S-" nil t arg))
8094 (error "No previous date before this line in this buffer")))
8096 ;; Initialize the highlight
8097 (defvar org-hl (make-overlay 1 1))
8098 (overlay-put org-hl 'face 'highlight)
8100 (defun org-highlight (begin end &optional buffer)
8101 "Highlight a region with overlay."
8102 (move-overlay org-hl begin end (or buffer (current-buffer))))
8104 (defun org-unhighlight ()
8105 "Detach overlay INDEX."
8106 (delete-overlay org-hl))
8108 (defun org-unhighlight-once ()
8109 "Remove the highlight from its position, and this function from the hook."
8110 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8111 (org-unhighlight))
8113 (defvar org-agenda-pre-follow-window-conf nil)
8114 (defun org-agenda-follow-mode ()
8115 "Toggle follow mode in an agenda buffer."
8116 (interactive)
8117 (unless org-agenda-follow-mode
8118 (setq org-agenda-pre-follow-window-conf
8119 (current-window-configuration)))
8120 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8121 (unless org-agenda-follow-mode
8122 (set-window-configuration org-agenda-pre-follow-window-conf))
8123 (org-agenda-set-mode-name)
8124 (org-agenda-do-context-action)
8125 (message "Follow mode is %s"
8126 (if org-agenda-follow-mode "on" "off")))
8128 (defun org-agenda-entry-text-mode (&optional arg)
8129 "Toggle entry text mode in an agenda buffer."
8130 (interactive "P")
8131 (if (or org-agenda-tag-filter
8132 org-agenda-category-filter
8133 org-agenda-regexp-filter
8134 org-agenda-top-headline-filter)
8135 (user-error "Can't show entry text in filtered views")
8136 (setq org-agenda-entry-text-mode (or (integerp arg)
8137 (not org-agenda-entry-text-mode)))
8138 (org-agenda-entry-text-hide)
8139 (and org-agenda-entry-text-mode
8140 (let ((org-agenda-entry-text-maxlines
8141 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8142 (org-agenda-entry-text-show)))
8143 (org-agenda-set-mode-name)
8144 (message "Entry text mode is %s%s"
8145 (if org-agenda-entry-text-mode "on" "off")
8146 (if (not org-agenda-entry-text-mode) ""
8147 (format " (maximum number of lines is %d)"
8148 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8150 (defun org-agenda-clockreport-mode ()
8151 "Toggle clocktable mode in an agenda buffer."
8152 (interactive)
8153 (org-agenda-check-type t 'agenda)
8154 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8155 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8156 (org-agenda-set-mode-name)
8157 (org-agenda-redo)
8158 (message "Clocktable mode is %s"
8159 (if org-agenda-clockreport-mode "on" "off")))
8161 (defun org-agenda-log-mode (&optional special)
8162 "Toggle log mode in an agenda buffer.
8163 With argument SPECIAL, show all possible log items, not only the ones
8164 configured in `org-agenda-log-mode-items'.
8165 With a double \\[universal-argument] prefix arg, show *only* \
8166 log items, nothing else."
8167 (interactive "P")
8168 (org-agenda-check-type t 'agenda 'timeline)
8169 (setq org-agenda-show-log
8170 (cond
8171 ((equal special '(16)) 'only)
8172 ((eq special 'clockcheck)
8173 (if (eq org-agenda-show-log 'clockcheck)
8174 nil 'clockcheck))
8175 (special '(closed clock state))
8176 (t (not org-agenda-show-log))))
8177 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8178 (org-agenda-set-mode-name)
8179 (org-agenda-redo)
8180 (message "Log mode is %s"
8181 (if org-agenda-show-log "on" "off")))
8183 (defun org-agenda-archives-mode (&optional with-files)
8184 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8185 When called with a prefix argument, include all archive files as well."
8186 (interactive "P")
8187 (setq org-agenda-archives-mode
8188 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8189 (org-agenda-set-mode-name)
8190 (org-agenda-redo)
8191 (message
8192 "%s"
8193 (cond
8194 ((eq org-agenda-archives-mode nil)
8195 "No archives are included")
8196 ((eq org-agenda-archives-mode 'trees)
8197 (format "Trees with :%s: tag are included" org-archive-tag))
8198 ((eq org-agenda-archives-mode t)
8199 (format "Trees with :%s: tag and all active archive files are included"
8200 org-archive-tag)))))
8202 (defun org-agenda-toggle-diary ()
8203 "Toggle diary inclusion in an agenda buffer."
8204 (interactive)
8205 (org-agenda-check-type t 'agenda)
8206 (setq org-agenda-include-diary (not org-agenda-include-diary))
8207 (org-agenda-redo)
8208 (org-agenda-set-mode-name)
8209 (message "Diary inclusion turned %s"
8210 (if org-agenda-include-diary "on" "off")))
8212 (defun org-agenda-toggle-deadlines ()
8213 "Toggle inclusion of entries with a deadline in an agenda buffer."
8214 (interactive)
8215 (org-agenda-check-type t 'agenda)
8216 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8217 (org-agenda-redo)
8218 (org-agenda-set-mode-name)
8219 (message "Deadlines inclusion turned %s"
8220 (if org-agenda-include-deadlines "on" "off")))
8222 (defun org-agenda-toggle-time-grid ()
8223 "Toggle time grid in an agenda buffer."
8224 (interactive)
8225 (org-agenda-check-type t 'agenda)
8226 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8227 (org-agenda-redo)
8228 (org-agenda-set-mode-name)
8229 (message "Time-grid turned %s"
8230 (if org-agenda-use-time-grid "on" "off")))
8232 (defun org-agenda-set-mode-name ()
8233 "Set the mode name to indicate all the small mode settings."
8234 (setq mode-name
8235 (list "Org-Agenda"
8236 (if (get 'org-agenda-files 'org-restrict) " []" "")
8238 '(:eval (org-agenda-span-name org-agenda-current-span))
8239 (if org-agenda-follow-mode " Follow" "")
8240 (if org-agenda-entry-text-mode " ETxt" "")
8241 (if org-agenda-include-diary " Diary" "")
8242 (if org-agenda-include-deadlines " Ddl" "")
8243 (if org-agenda-use-time-grid " Grid" "")
8244 (if (and (boundp 'org-habit-show-habits)
8245 org-habit-show-habits) " Habit" "")
8246 (cond
8247 ((consp org-agenda-show-log) " LogAll")
8248 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8249 (org-agenda-show-log " Log")
8250 (t ""))
8251 (if (or org-agenda-category-filter
8252 (get 'org-agenda-category-filter :preset-filter))
8253 '(:eval (propertize
8254 (concat " <"
8255 (mapconcat
8256 'identity
8257 (append
8258 (get 'org-agenda-category-filter :preset-filter)
8259 org-agenda-category-filter)
8261 ">")
8262 'face 'org-agenda-filter-category
8263 'help-echo "Category used in filtering")) "")
8264 (if (or org-agenda-tag-filter
8265 (get 'org-agenda-tag-filter :preset-filter))
8266 '(:eval (propertize
8267 (concat " {"
8268 (mapconcat
8269 'identity
8270 (append
8271 (get 'org-agenda-tag-filter :preset-filter)
8272 org-agenda-tag-filter)
8274 "}")
8275 'face 'org-agenda-filter-tags
8276 'help-echo "Tags used in filtering")) "")
8277 (if (or org-agenda-effort-filter
8278 (get 'org-agenda-effort-filter :preset-filter))
8279 '(:eval (propertize
8280 (concat " {"
8281 (mapconcat
8282 'identity
8283 (append
8284 (get 'org-agenda-effort-filter :preset-filter)
8285 org-agenda-effort-filter)
8287 "}")
8288 'face 'org-agenda-filter-effort
8289 'help-echo "Effort conditions used in filtering")) "")
8290 (if (or org-agenda-regexp-filter
8291 (get 'org-agenda-regexp-filter :preset-filter))
8292 '(:eval (propertize
8293 (concat " ["
8294 (mapconcat
8295 'identity
8296 (append
8297 (get 'org-agenda-regexp-filter :preset-filter)
8298 org-agenda-regexp-filter)
8300 "]")
8301 'face 'org-agenda-filter-regexp
8302 'help-echo "Regexp used in filtering")) "")
8303 (if org-agenda-archives-mode
8304 (if (eq org-agenda-archives-mode t)
8305 " Archives"
8306 (format " :%s:" org-archive-tag))
8308 (if org-agenda-clockreport-mode " Clock" "")))
8309 (force-mode-line-update))
8311 (defun org-agenda-update-agenda-type ()
8312 "Update the agenda type after each command."
8313 (setq org-agenda-type
8314 (or (get-text-property (point) 'org-agenda-type)
8315 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8317 (defun org-agenda-next-line ()
8318 "Move cursor to the next line, and show if follow mode is active."
8319 (interactive)
8320 (call-interactively 'next-line)
8321 (org-agenda-do-context-action))
8323 (defun org-agenda-previous-line ()
8324 "Move cursor to the previous line, and show if follow-mode is active."
8325 (interactive)
8326 (call-interactively 'previous-line)
8327 (org-agenda-do-context-action))
8329 (defun org-agenda-next-item (n)
8330 "Move cursor to next agenda item."
8331 (interactive "p")
8332 (let ((col (current-column)))
8333 (dotimes (c n)
8334 (when (next-single-property-change (point-at-eol) 'org-marker)
8335 (move-end-of-line 1)
8336 (goto-char (next-single-property-change (point) 'org-marker))))
8337 (org-move-to-column col))
8338 (org-agenda-do-context-action))
8340 (defun org-agenda-previous-item (n)
8341 "Move cursor to next agenda item."
8342 (interactive "p")
8343 (dotimes (c n)
8344 (let ((col (current-column))
8345 (goto (save-excursion
8346 (move-end-of-line 0)
8347 (previous-single-property-change (point) 'org-marker))))
8348 (if goto (goto-char goto))
8349 (org-move-to-column col)))
8350 (org-agenda-do-context-action))
8352 (defun org-agenda-do-context-action ()
8353 "Show outline path and, maybe, follow mode window."
8354 (let ((m (org-get-at-bol 'org-marker)))
8355 (when (and (markerp m) (marker-buffer m))
8356 (and org-agenda-follow-mode
8357 (if org-agenda-follow-indirect
8358 (org-agenda-tree-to-indirect-buffer nil)
8359 (org-agenda-show)))
8360 (and org-agenda-show-outline-path
8361 (org-with-point-at m (org-display-outline-path t))))))
8363 (defun org-agenda-show-tags ()
8364 "Show the tags applicable to the current item."
8365 (interactive)
8366 (let* ((tags (org-get-at-bol 'tags)))
8367 (if tags
8368 (message "Tags are :%s:"
8369 (org-no-properties (mapconcat 'identity tags ":")))
8370 (message "No tags associated with this line"))))
8372 (defun org-agenda-goto (&optional highlight)
8373 "Go to the entry at point in the corresponding Org-mode file."
8374 (interactive)
8375 (let* ((marker (or (org-get-at-bol 'org-marker)
8376 (org-agenda-error)))
8377 (buffer (marker-buffer marker))
8378 (pos (marker-position marker)))
8379 (switch-to-buffer-other-window buffer)
8380 (widen)
8381 (push-mark)
8382 (goto-char pos)
8383 (when (derived-mode-p 'org-mode)
8384 (org-show-context 'agenda)
8385 (recenter (/ (window-height) 2))
8386 (org-back-to-heading t)
8387 (if (re-search-forward org-complex-heading-regexp nil t)
8388 (goto-char (match-beginning 4))))
8389 (run-hooks 'org-agenda-after-show-hook)
8390 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8392 (defvar org-agenda-after-show-hook nil
8393 "Normal hook run after an item has been shown from the agenda.
8394 Point is in the buffer where the item originated.")
8396 (defun org-agenda-kill ()
8397 "Kill the entry or subtree belonging to the current agenda entry."
8398 (interactive)
8399 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8400 (let* ((bufname-orig (buffer-name))
8401 (marker (or (org-get-at-bol 'org-marker)
8402 (org-agenda-error)))
8403 (buffer (marker-buffer marker))
8404 (pos (marker-position marker))
8405 (type (org-get-at-bol 'type))
8406 dbeg dend (n 0) conf)
8407 (org-with-remote-undo buffer
8408 (with-current-buffer buffer
8409 (save-excursion
8410 (goto-char pos)
8411 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8412 (setq dbeg (progn (org-back-to-heading t) (point))
8413 dend (org-end-of-subtree t t))
8414 (setq dbeg (point-at-bol)
8415 dend (min (point-max) (1+ (point-at-eol)))))
8416 (goto-char dbeg)
8417 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8418 (setq conf (or (eq t org-agenda-confirm-kill)
8419 (and (numberp org-agenda-confirm-kill)
8420 (> n org-agenda-confirm-kill))))
8421 (and conf
8422 (not (y-or-n-p
8423 (format "Delete entry with %d lines in buffer \"%s\"? "
8424 n (buffer-name buffer))))
8425 (error "Abort"))
8426 (let ((org-agenda-buffer-name bufname-orig))
8427 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8428 (with-current-buffer buffer (delete-region dbeg dend))
8429 (message "Agenda item and source killed"))))
8431 (defvar org-archive-default-command) ; defined in org-archive.el
8432 (defun org-agenda-archive-default ()
8433 "Archive the entry or subtree belonging to the current agenda entry."
8434 (interactive)
8435 (require 'org-archive)
8436 (org-agenda-archive-with org-archive-default-command))
8438 (defun org-agenda-archive-default-with-confirmation ()
8439 "Archive the entry or subtree belonging to the current agenda entry."
8440 (interactive)
8441 (require 'org-archive)
8442 (org-agenda-archive-with org-archive-default-command 'confirm))
8444 (defun org-agenda-archive ()
8445 "Archive the entry or subtree belonging to the current agenda entry."
8446 (interactive)
8447 (org-agenda-archive-with 'org-archive-subtree))
8449 (defun org-agenda-archive-to-archive-sibling ()
8450 "Move the entry to the archive sibling."
8451 (interactive)
8452 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8454 (defun org-agenda-archive-with (cmd &optional confirm)
8455 "Move the entry to the archive sibling."
8456 (interactive)
8457 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8458 (let* ((bufname-orig (buffer-name))
8459 (marker (or (org-get-at-bol 'org-marker)
8460 (org-agenda-error)))
8461 (buffer (marker-buffer marker))
8462 (pos (marker-position marker)))
8463 (org-with-remote-undo buffer
8464 (with-current-buffer buffer
8465 (if (derived-mode-p 'org-mode)
8466 (if (and confirm
8467 (not (y-or-n-p "Archive this subtree or entry? ")))
8468 (error "Abort")
8469 (save-window-excursion
8470 (goto-char pos)
8471 (let ((org-agenda-buffer-name bufname-orig))
8472 (org-remove-subtree-entries-from-agenda))
8473 (org-back-to-heading t)
8474 (funcall cmd)))
8475 (error "Archiving works only in Org-mode files"))))))
8477 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8478 "Remove all lines in the agenda that correspond to a given subtree.
8479 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8480 If this information is not given, the function uses the tree at point."
8481 (let ((buf (or buf (current-buffer))) m p)
8482 (save-excursion
8483 (unless (and beg end)
8484 (org-back-to-heading t)
8485 (setq beg (point))
8486 (org-end-of-subtree t)
8487 (setq end (point)))
8488 (set-buffer (get-buffer org-agenda-buffer-name))
8489 (save-excursion
8490 (goto-char (point-max))
8491 (beginning-of-line 1)
8492 (while (not (bobp))
8493 (when (and (setq m (org-get-at-bol 'org-marker))
8494 (equal buf (marker-buffer m))
8495 (setq p (marker-position m))
8496 (>= p beg)
8497 (< p end))
8498 (let ((inhibit-read-only t))
8499 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8500 (beginning-of-line 0))))))
8502 (defun org-agenda-refile (&optional goto rfloc no-update)
8503 "Refile the item at point.
8505 When GOTO is 0 or \\='(64) or a triple \\[universal-argument] prefix argument,
8506 clear the refile cache.
8507 When GOTO is \\='(16) or a double \\[universal-argument] prefix argument,
8508 go to the location of the last refiled item.
8509 RFLOC can be a refile location obtained in a different way.
8510 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8511 (interactive "P")
8512 (cond
8513 ((member goto '(0 (64)))
8514 (org-refile-cache-clear))
8515 ((equal goto '(16))
8516 (org-refile-goto-last-stored))
8518 (let* ((buffer-orig (buffer-name))
8519 (marker (or (org-get-at-bol 'org-hd-marker)
8520 (org-agenda-error)))
8521 (buffer (marker-buffer marker))
8522 (pos (marker-position marker))
8523 (rfloc (or rfloc
8524 (org-refile-get-location
8525 (if goto "Goto" "Refile to") buffer
8526 org-refile-allow-creating-parent-nodes))))
8527 (with-current-buffer buffer
8528 (save-excursion
8529 (save-restriction
8530 (widen)
8531 (goto-char marker)
8532 (let ((org-agenda-buffer-name buffer-orig))
8533 (org-remove-subtree-entries-from-agenda))
8534 (org-refile goto buffer rfloc)))))
8535 (unless no-update (org-agenda-redo)))))
8537 (defun org-agenda-open-link (&optional arg)
8538 "Open the link(s) in the current entry, if any.
8539 This looks for a link in the displayed line in the agenda.
8540 It also looks at the text of the entry itself."
8541 (interactive "P")
8542 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8543 (org-get-at-bol 'org-marker)))
8544 (buffer (and marker (marker-buffer marker)))
8545 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8546 (lkall (and buffer (org-offer-links-in-entry
8547 buffer marker arg prefix)))
8548 (lk0 (car lkall))
8549 (lk (if (stringp lk0) (list lk0) lk0))
8550 (lkend (cdr lkall))
8551 trg)
8552 (cond
8553 ((and buffer lk)
8554 (mapcar (lambda(l)
8555 (with-current-buffer buffer
8556 (setq trg (and (string-match org-bracket-link-regexp l)
8557 (match-string 1 l)))
8558 (if (or (not trg) (string-match org-any-link-re trg))
8559 (save-excursion
8560 (save-restriction
8561 (widen)
8562 (goto-char marker)
8563 (when (search-forward l nil lkend)
8564 (goto-char (match-beginning 0))
8565 (org-open-at-point))))
8566 ;; This is an internal link, widen the buffer
8567 (switch-to-buffer-other-window buffer)
8568 (widen)
8569 (goto-char marker)
8570 (when (search-forward l nil lkend)
8571 (goto-char (match-beginning 0))
8572 (org-open-at-point)))))
8573 lk))
8574 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8575 (save-excursion
8576 (beginning-of-line 1)
8577 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8578 (org-open-link-from-string (match-string 1)))
8579 (t (message "No link to open here")))))
8581 (defun org-agenda-copy-local-variable (var)
8582 "Get a variable from a referenced buffer and install it here."
8583 (let ((m (org-get-at-bol 'org-marker)))
8584 (when (and m (buffer-live-p (marker-buffer m)))
8585 (set (make-local-variable var)
8586 (with-current-buffer (marker-buffer m)
8587 (symbol-value var))))))
8589 (defun org-agenda-switch-to (&optional delete-other-windows)
8590 "Go to the Org mode file which contains the item at point.
8591 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8592 displayed Org file fills the frame."
8593 (interactive)
8594 (if (and org-return-follows-link
8595 (not (org-get-at-bol 'org-marker))
8596 (org-in-regexp org-bracket-link-regexp))
8597 (org-open-link-from-string (match-string 0))
8598 (let* ((marker (or (org-get-at-bol 'org-marker)
8599 (org-agenda-error)))
8600 (buffer (marker-buffer marker))
8601 (pos (marker-position marker)))
8602 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8603 (pop-to-buffer-same-window buffer)
8604 (when delete-other-windows (delete-other-windows))
8605 (widen)
8606 (goto-char pos)
8607 (when (derived-mode-p 'org-mode)
8608 (org-show-context 'agenda)
8609 (run-hooks 'org-agenda-after-show-hook)))))
8611 (defun org-agenda-goto-mouse (ev)
8612 "Go to the Org-mode file which contains the item at the mouse click."
8613 (interactive "e")
8614 (mouse-set-point ev)
8615 (org-agenda-goto))
8617 (defun org-agenda-show (&optional full-entry)
8618 "Display the Org-mode file which contains the item at point.
8619 With prefix argument FULL-ENTRY, make the entire entry visible
8620 if it was hidden in the outline."
8621 (interactive "P")
8622 (let ((win (selected-window)))
8623 (org-agenda-goto t)
8624 (when full-entry (org-show-entry))
8625 (select-window win)))
8627 (defvar org-agenda-show-window nil)
8628 (defun org-agenda-show-and-scroll-up (&optional arg)
8629 "Display the Org-mode file which contains the item at point.
8630 When called repeatedly, scroll the window that is displaying the buffer.
8631 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8632 `show-subtree' to display the item, so that drawers and logbooks stay
8633 folded."
8634 (interactive "P")
8635 (let ((win (selected-window)))
8636 (if (and (window-live-p org-agenda-show-window)
8637 (eq this-command last-command))
8638 (progn
8639 (select-window org-agenda-show-window)
8640 (ignore-errors (scroll-up)))
8641 (org-agenda-goto t)
8642 (if arg (org-show-entry) (outline-show-subtree))
8643 (setq org-agenda-show-window (selected-window)))
8644 (select-window win)))
8646 (defun org-agenda-show-scroll-down ()
8647 "Scroll down the window showing the agenda."
8648 (interactive)
8649 (let ((win (selected-window)))
8650 (when (window-live-p org-agenda-show-window)
8651 (select-window org-agenda-show-window)
8652 (ignore-errors (scroll-down))
8653 (select-window win))))
8655 (defun org-agenda-show-1 (&optional more)
8656 "Display the Org-mode file which contains the item at point.
8657 The prefix arg selects the amount of information to display:
8659 0 hide the subtree
8660 1 just show the entry according to defaults.
8661 2 show the children view
8662 3 show the subtree view
8663 4 show the entire subtree and any LOGBOOK drawers
8664 5 show the entire subtree and any drawers
8665 With prefix argument FULL-ENTRY, make the entire entry visible
8666 if it was hidden in the outline."
8667 (interactive "p")
8668 (let ((win (selected-window)))
8669 (org-agenda-goto t)
8670 (org-back-to-heading)
8671 (set-window-start (selected-window) (point-at-bol))
8672 (cond
8673 ((= more 0)
8674 (outline-hide-subtree)
8675 (save-excursion
8676 (org-back-to-heading)
8677 (run-hook-with-args 'org-cycle-hook 'folded))
8678 (message "Remote: FOLDED"))
8679 ((and (called-interactively-p 'any) (= more 1))
8680 (message "Remote: show with default settings"))
8681 ((= more 2)
8682 (outline-show-entry)
8683 (org-show-children)
8684 (save-excursion
8685 (org-back-to-heading)
8686 (run-hook-with-args 'org-cycle-hook 'children))
8687 (message "Remote: CHILDREN"))
8688 ((= more 3)
8689 (outline-show-subtree)
8690 (save-excursion
8691 (org-back-to-heading)
8692 (run-hook-with-args 'org-cycle-hook 'subtree))
8693 (message "Remote: SUBTREE"))
8694 ((= more 4)
8695 (outline-show-subtree)
8696 (save-excursion
8697 (org-back-to-heading)
8698 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8699 (message "Remote: SUBTREE AND LOGBOOK"))
8700 ((> more 4)
8701 (outline-show-subtree)
8702 (message "Remote: SUBTREE AND ALL DRAWERS")))
8703 (select-window win)))
8705 (defvar org-agenda-cycle-counter nil)
8706 (defun org-agenda-cycle-show (&optional n)
8707 "Show the current entry in another window, with default settings.
8709 Default settings are taken from `org-show-context-detail'. When
8710 use repeatedly in immediate succession, the remote entry will
8711 cycle through visibility
8713 children -> subtree -> folded
8715 When called with a numeric prefix arg, that arg will be passed through to
8716 `org-agenda-show-1'. For the interpretation of that argument, see the
8717 docstring of `org-agenda-show-1'."
8718 (interactive "P")
8719 (if (integerp n)
8720 (setq org-agenda-cycle-counter n)
8721 (if (not (eq last-command this-command))
8722 (setq org-agenda-cycle-counter 1)
8723 (if (equal org-agenda-cycle-counter 0)
8724 (setq org-agenda-cycle-counter 2)
8725 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8726 (if (> org-agenda-cycle-counter 3)
8727 (setq org-agenda-cycle-counter 0)))))
8728 (org-agenda-show-1 org-agenda-cycle-counter))
8730 (defun org-agenda-recenter (arg)
8731 "Display the Org-mode file which contains the item at point and recenter."
8732 (interactive "P")
8733 (let ((win (selected-window)))
8734 (org-agenda-goto t)
8735 (recenter arg)
8736 (select-window win)))
8738 (defun org-agenda-show-mouse (ev)
8739 "Display the Org-mode file which contains the item at the mouse click."
8740 (interactive "e")
8741 (mouse-set-point ev)
8742 (org-agenda-show))
8744 (defun org-agenda-check-no-diary ()
8745 "Check if the entry is a diary link and abort if yes."
8746 (if (org-get-at-bol 'org-agenda-diary-link)
8747 (org-agenda-error)))
8749 (defun org-agenda-error ()
8750 "Throw an error when a command is not allowed in the agenda."
8751 (user-error "Command not allowed in this line"))
8753 (defun org-agenda-tree-to-indirect-buffer (arg)
8754 "Show the subtree corresponding to the current entry in an indirect buffer.
8755 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8757 With a numerical prefix ARG, go up to this level and then take that tree.
8758 With a negative numeric ARG, go up by this number of levels.
8759 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8760 use the dedicated frame)."
8761 (interactive "P")
8762 (if current-prefix-arg
8763 (org-agenda-do-tree-to-indirect-buffer arg)
8764 (let ((agenda-buffer (buffer-name))
8765 (agenda-window (selected-window))
8766 (indirect-window
8767 (and org-last-indirect-buffer
8768 (get-buffer-window org-last-indirect-buffer))))
8769 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8770 (unless (or (eq org-indirect-buffer-display 'new-frame)
8771 (eq org-indirect-buffer-display 'dedicated-frame))
8772 (unwind-protect
8773 (unless (and indirect-window (window-live-p indirect-window))
8774 (setq indirect-window (split-window agenda-window)))
8775 (and indirect-window (select-window indirect-window))
8776 (switch-to-buffer org-last-indirect-buffer :norecord)
8777 (fit-window-to-buffer indirect-window)))
8778 (select-window (get-buffer-window agenda-buffer))
8779 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8781 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8782 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8783 (org-agenda-check-no-diary)
8784 (let* ((marker (or (org-get-at-bol 'org-marker)
8785 (org-agenda-error)))
8786 (buffer (marker-buffer marker))
8787 (pos (marker-position marker)))
8788 (with-current-buffer buffer
8789 (save-excursion
8790 (goto-char pos)
8791 (funcall 'org-tree-to-indirect-buffer arg)))))
8793 (defvar org-last-heading-marker (make-marker)
8794 "Marker pointing to the headline that last changed its TODO state
8795 by a remote command from the agenda.")
8797 (defun org-agenda-todo-nextset ()
8798 "Switch TODO entry to next sequence."
8799 (interactive)
8800 (org-agenda-todo 'nextset))
8802 (defun org-agenda-todo-previousset ()
8803 "Switch TODO entry to previous sequence."
8804 (interactive)
8805 (org-agenda-todo 'previousset))
8807 (defun org-agenda-todo (&optional arg)
8808 "Cycle TODO state of line at point, also in Org-mode file.
8809 This changes the line at point, all other lines in the agenda referring to
8810 the same tree node, and the headline of the tree node in the Org-mode file."
8811 (interactive "P")
8812 (org-agenda-check-no-diary)
8813 (let* ((col (current-column))
8814 (marker (or (org-get-at-bol 'org-marker)
8815 (org-agenda-error)))
8816 (buffer (marker-buffer marker))
8817 (pos (marker-position marker))
8818 (hdmarker (org-get-at-bol 'org-hd-marker))
8819 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8820 (inhibit-read-only t)
8821 org-agenda-headline-snapshot-before-repeat newhead just-one)
8822 (org-with-remote-undo buffer
8823 (with-current-buffer buffer
8824 (widen)
8825 (goto-char pos)
8826 (org-show-context 'agenda)
8827 (let ((current-prefix-arg arg))
8828 (call-interactively 'org-todo))
8829 (and (bolp) (forward-char 1))
8830 (setq newhead (org-get-heading))
8831 (when (and (bound-and-true-p
8832 org-agenda-headline-snapshot-before-repeat)
8833 (not (equal org-agenda-headline-snapshot-before-repeat
8834 newhead))
8835 todayp)
8836 (setq newhead org-agenda-headline-snapshot-before-repeat
8837 just-one t))
8838 (save-excursion
8839 (org-back-to-heading)
8840 (move-marker org-last-heading-marker (point))))
8841 (beginning-of-line 1)
8842 (save-window-excursion
8843 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8844 (when (bound-and-true-p org-clock-out-when-done)
8845 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8846 newhead)
8847 (org-agenda-unmark-clocking-task))
8848 (org-move-to-column col)
8849 (org-agenda-mark-clocking-task))))
8851 (defun org-agenda-add-note (&optional arg)
8852 "Add a time-stamped note to the entry at point."
8853 (interactive "P")
8854 (org-agenda-check-no-diary)
8855 (let* ((marker (or (org-get-at-bol 'org-marker)
8856 (org-agenda-error)))
8857 (buffer (marker-buffer marker))
8858 (pos (marker-position marker))
8859 (hdmarker (org-get-at-bol 'org-hd-marker))
8860 (inhibit-read-only t))
8861 (with-current-buffer buffer
8862 (widen)
8863 (goto-char pos)
8864 (org-show-context 'agenda)
8865 (org-add-note))))
8867 (defun org-agenda-change-all-lines (newhead hdmarker
8868 &optional fixface just-this)
8869 "Change all lines in the agenda buffer which match HDMARKER.
8870 The new content of the line will be NEWHEAD (as modified by
8871 `org-agenda-format-item'). HDMARKER is checked with
8872 `equal' against all `org-hd-marker' text properties in the file.
8873 If FIXFACE is non-nil, the face of each item is modified according to
8874 the new TODO state.
8875 If JUST-THIS is non-nil, change just the current line, not all.
8876 If FORCE-TAGS is non nil, the car of it returns the new tags."
8877 (let* ((inhibit-read-only t)
8878 (line (org-current-line))
8879 (org-agenda-buffer (current-buffer))
8880 (thetags (with-current-buffer (marker-buffer hdmarker)
8881 (save-excursion (save-restriction (widen)
8882 (goto-char hdmarker)
8883 (org-get-tags-at)))))
8884 props m pl undone-face done-face finish new dotime level cat tags)
8885 (save-excursion
8886 (goto-char (point-max))
8887 (beginning-of-line 1)
8888 (while (not finish)
8889 (setq finish (bobp))
8890 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8891 (or (not just-this) (= (org-current-line) line))
8892 (equal m hdmarker))
8893 (setq props (text-properties-at (point))
8894 dotime (org-get-at-bol 'dotime)
8895 cat (org-get-at-eol 'org-category 1)
8896 level (org-get-at-bol 'level)
8897 tags thetags
8899 (let ((org-prefix-format-compiled
8900 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8901 org-prefix-format-compiled))
8902 (extra (org-get-at-bol 'extra)))
8903 (with-current-buffer (marker-buffer hdmarker)
8904 (save-excursion
8905 (save-restriction
8906 (widen)
8907 (org-agenda-format-item extra newhead level cat tags dotime)))))
8908 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8909 undone-face (org-get-at-bol 'undone-face)
8910 done-face (org-get-at-bol 'done-face))
8911 (beginning-of-line 1)
8912 (cond
8913 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8914 ((looking-at ".*")
8915 ;; When replacing the whole line, preserve bulk mark
8916 ;; overlay, if any.
8917 (let ((mark (catch :overlay
8918 (dolist (o (overlays-in (point) (+ 2 (point))))
8919 (when (eq (overlay-get o 'type)
8920 'org-marked-entry-overlay)
8921 (throw :overlay o))))))
8922 (replace-match new t t)
8923 (beginning-of-line)
8924 (when mark (move-overlay mark (point) (+ 2 (point)))))
8925 (add-text-properties (point-at-bol) (point-at-eol) props)
8926 (when fixface
8927 (add-text-properties
8928 (point-at-bol) (point-at-eol)
8929 (list 'face
8930 (if org-last-todo-state-is-todo
8931 undone-face done-face))))
8932 (org-agenda-highlight-todo 'line)
8933 (beginning-of-line 1))
8934 (t (error "Line update did not work")))
8935 (save-restriction
8936 (narrow-to-region (point-at-bol) (point-at-eol))
8937 (org-agenda-finalize)))
8938 (beginning-of-line 0)))))
8940 (defun org-agenda-align-tags (&optional line)
8941 "Align all tags in agenda items to `org-agenda-tags-column'."
8942 (let ((inhibit-read-only t) l c)
8943 (save-excursion
8944 (goto-char (if line (point-at-bol) (point-min)))
8945 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8946 (if line (point-at-eol) nil) t)
8947 (add-text-properties
8948 (match-beginning 2) (match-end 2)
8949 (list 'face (delq nil (let ((prop (get-text-property
8950 (match-beginning 2) 'face)))
8951 (or (listp prop) (setq prop (list prop)))
8952 (if (memq 'org-tag prop)
8953 prop
8954 (cons 'org-tag prop))))))
8955 (setq l (- (match-end 2) (match-beginning 2))
8956 c (if (< org-agenda-tags-column 0)
8957 (- (abs org-agenda-tags-column) l)
8958 org-agenda-tags-column))
8959 (delete-region (match-beginning 1) (match-end 1))
8960 (goto-char (match-beginning 1))
8961 (insert (org-add-props
8962 (make-string (max 1 (- c (current-column))) ?\ )
8963 (plist-put (copy-sequence (text-properties-at (point)))
8964 'face nil))))
8965 (goto-char (point-min))
8966 (org-font-lock-add-tag-faces (point-max)))))
8968 (defun org-agenda-priority-up ()
8969 "Increase the priority of line at point, also in Org-mode file."
8970 (interactive)
8971 (org-agenda-priority 'up))
8973 (defun org-agenda-priority-down ()
8974 "Decrease the priority of line at point, also in Org-mode file."
8975 (interactive)
8976 (org-agenda-priority 'down))
8978 (defun org-agenda-priority (&optional force-direction)
8979 "Set the priority of line at point, also in Org-mode file.
8980 This changes the line at point, all other lines in the agenda referring to
8981 the same tree node, and the headline of the tree node in the Org-mode file.
8982 Called with a universal prefix arg, show the priority instead of setting it."
8983 (interactive "P")
8984 (if (equal force-direction '(4))
8985 (org-show-priority)
8986 (unless org-enable-priority-commands
8987 (error "Priority commands are disabled"))
8988 (org-agenda-check-no-diary)
8989 (let* ((col (current-column))
8990 (marker (or (org-get-at-bol 'org-marker)
8991 (org-agenda-error)))
8992 (hdmarker (org-get-at-bol 'org-hd-marker))
8993 (buffer (marker-buffer hdmarker))
8994 (pos (marker-position hdmarker))
8995 (inhibit-read-only t)
8996 newhead)
8997 (org-with-remote-undo buffer
8998 (with-current-buffer buffer
8999 (widen)
9000 (goto-char pos)
9001 (org-show-context 'agenda)
9002 (funcall 'org-priority force-direction)
9003 (end-of-line 1)
9004 (setq newhead (org-get-heading)))
9005 (org-agenda-change-all-lines newhead hdmarker)
9006 (org-move-to-column col)))))
9008 ;; FIXME: should fix the tags property of the agenda line.
9009 (defun org-agenda-set-tags (&optional tag onoff)
9010 "Set tags for the current headline."
9011 (interactive)
9012 (org-agenda-check-no-diary)
9013 (if (and (org-region-active-p) (called-interactively-p 'any))
9014 (call-interactively 'org-change-tag-in-region)
9015 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9016 (org-agenda-error)))
9017 (buffer (marker-buffer hdmarker))
9018 (pos (marker-position hdmarker))
9019 (inhibit-read-only t)
9020 newhead)
9021 (org-with-remote-undo buffer
9022 (with-current-buffer buffer
9023 (widen)
9024 (goto-char pos)
9025 (org-show-context 'agenda)
9026 (if tag
9027 (org-toggle-tag tag onoff)
9028 (call-interactively 'org-set-tags))
9029 (end-of-line 1)
9030 (setq newhead (org-get-heading)))
9031 (org-agenda-change-all-lines newhead hdmarker)
9032 (beginning-of-line 1)))))
9034 (defun org-agenda-set-property ()
9035 "Set a property for the current headline."
9036 (interactive)
9037 (org-agenda-check-no-diary)
9038 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9039 (org-agenda-error)))
9040 (buffer (marker-buffer hdmarker))
9041 (pos (marker-position hdmarker))
9042 (inhibit-read-only t)
9043 newhead)
9044 (org-with-remote-undo buffer
9045 (with-current-buffer buffer
9046 (widen)
9047 (goto-char pos)
9048 (org-show-context 'agenda)
9049 (call-interactively 'org-set-property)))))
9051 (defun org-agenda-set-effort ()
9052 "Set the effort property for the current headline."
9053 (interactive)
9054 (org-agenda-check-no-diary)
9055 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9056 (org-agenda-error)))
9057 (buffer (marker-buffer hdmarker))
9058 (pos (marker-position hdmarker))
9059 (inhibit-read-only t)
9060 newhead)
9061 (org-with-remote-undo buffer
9062 (with-current-buffer buffer
9063 (widen)
9064 (goto-char pos)
9065 (org-show-context 'agenda)
9066 (call-interactively 'org-set-effort)
9067 (end-of-line 1)
9068 (setq newhead (org-get-heading)))
9069 (org-agenda-change-all-lines newhead hdmarker))))
9071 (defun org-agenda-toggle-archive-tag ()
9072 "Toggle the archive tag for the current entry."
9073 (interactive)
9074 (org-agenda-check-no-diary)
9075 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9076 (org-agenda-error)))
9077 (buffer (marker-buffer hdmarker))
9078 (pos (marker-position hdmarker))
9079 (inhibit-read-only t)
9080 newhead)
9081 (org-with-remote-undo buffer
9082 (with-current-buffer buffer
9083 (widen)
9084 (goto-char pos)
9085 (org-show-context 'agenda)
9086 (call-interactively 'org-toggle-archive-tag)
9087 (end-of-line 1)
9088 (setq newhead (org-get-heading)))
9089 (org-agenda-change-all-lines newhead hdmarker)
9090 (beginning-of-line 1))))
9092 (defun org-agenda-do-date-later (arg)
9093 (interactive "P")
9094 (cond
9095 ((or (equal arg '(16))
9096 (memq last-command
9097 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9098 (setq this-command 'org-agenda-date-later-minutes)
9099 (org-agenda-date-later-minutes 1))
9100 ((or (equal arg '(4))
9101 (memq last-command
9102 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9103 (setq this-command 'org-agenda-date-later-hours)
9104 (org-agenda-date-later-hours 1))
9106 (org-agenda-date-later (prefix-numeric-value arg)))))
9108 (defun org-agenda-do-date-earlier (arg)
9109 (interactive "P")
9110 (cond
9111 ((or (equal arg '(16))
9112 (memq last-command
9113 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9114 (setq this-command 'org-agenda-date-earlier-minutes)
9115 (org-agenda-date-earlier-minutes 1))
9116 ((or (equal arg '(4))
9117 (memq last-command
9118 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9119 (setq this-command 'org-agenda-date-earlier-hours)
9120 (org-agenda-date-earlier-hours 1))
9122 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9124 (defun org-agenda-date-later (arg &optional what)
9125 "Change the date of this item to ARG day(s) later."
9126 (interactive "p")
9127 (org-agenda-check-type t 'agenda 'timeline)
9128 (org-agenda-check-no-diary)
9129 (let* ((marker (or (org-get-at-bol 'org-marker)
9130 (org-agenda-error)))
9131 (buffer (marker-buffer marker))
9132 (pos (marker-position marker))
9133 cdate today)
9134 (org-with-remote-undo buffer
9135 (with-current-buffer buffer
9136 (widen)
9137 (goto-char pos)
9138 (if (not (org-at-timestamp-p))
9139 (error "Cannot find time stamp"))
9140 (when (and org-agenda-move-date-from-past-immediately-to-today
9141 (equal arg 1)
9142 (or (not what) (eq what 'day))
9143 (not (save-match-data (org-at-date-range-p))))
9144 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9145 cdate (calendar-absolute-from-gregorian
9146 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9147 today (org-today))
9148 (if (> today cdate)
9149 ;; immediately shift to today
9150 (setq arg (- today cdate))))
9151 (org-timestamp-change arg (or what 'day))
9152 (when (and (org-at-date-range-p)
9153 (re-search-backward org-tr-regexp-both (point-at-bol)))
9154 (let ((end org-last-changed-timestamp))
9155 (org-timestamp-change arg (or what 'day))
9156 (setq org-last-changed-timestamp
9157 (concat org-last-changed-timestamp "--" end)))))
9158 (org-agenda-show-new-time marker org-last-changed-timestamp))
9159 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9161 (defun org-agenda-date-earlier (arg &optional what)
9162 "Change the date of this item to ARG day(s) earlier."
9163 (interactive "p")
9164 (org-agenda-date-later (- arg) what))
9166 (defun org-agenda-date-later-minutes (arg)
9167 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9168 (interactive "p")
9169 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9170 (org-agenda-date-later arg 'minute))
9172 (defun org-agenda-date-earlier-minutes (arg)
9173 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9174 (interactive "p")
9175 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9176 (org-agenda-date-earlier arg 'minute))
9178 (defun org-agenda-date-later-hours (arg)
9179 "Change the time of this item, in hour steps."
9180 (interactive "p")
9181 (org-agenda-date-later arg 'hour))
9183 (defun org-agenda-date-earlier-hours (arg)
9184 "Change the time of this item, in hour steps."
9185 (interactive "p")
9186 (org-agenda-date-earlier arg 'hour))
9188 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9189 "Show new date stamp via text properties."
9190 ;; We use text properties to make this undoable
9191 (let ((inhibit-read-only t))
9192 (setq stamp (concat prefix " => " stamp " "))
9193 (save-excursion
9194 (goto-char (point-max))
9195 (while (not (bobp))
9196 (when (equal marker (org-get-at-bol 'org-marker))
9197 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9198 (org-move-to-column (- (window-width) (length stamp)) t)
9199 (add-text-properties
9200 (1- (point)) (point-at-eol)
9201 (list 'display (org-add-props stamp nil
9202 'face '(secondary-selection default))))
9203 (beginning-of-line 1))
9204 (beginning-of-line 0)))))
9206 (defun org-agenda-date-prompt (arg)
9207 "Change the date of this item. Date is prompted for, with default today.
9208 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9209 be used to request time specification in the time stamp."
9210 (interactive "P")
9211 (org-agenda-check-type t 'agenda 'timeline)
9212 (org-agenda-check-no-diary)
9213 (let* ((marker (or (org-get-at-bol 'org-marker)
9214 (org-agenda-error)))
9215 (buffer (marker-buffer marker))
9216 (pos (marker-position marker)))
9217 (org-with-remote-undo buffer
9218 (with-current-buffer buffer
9219 (widen)
9220 (goto-char pos)
9221 (if (not (org-at-timestamp-p t))
9222 (error "Cannot find time stamp"))
9223 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9224 (org-agenda-show-new-time marker org-last-changed-timestamp))
9225 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9227 (defun org-agenda-schedule (arg &optional time)
9228 "Schedule the item at point.
9229 ARG is passed through to `org-schedule'."
9230 (interactive "P")
9231 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9232 (org-agenda-check-no-diary)
9233 (let* ((marker (or (org-get-at-bol 'org-marker)
9234 (org-agenda-error)))
9235 (type (marker-insertion-type marker))
9236 (buffer (marker-buffer marker))
9237 (pos (marker-position marker))
9239 (set-marker-insertion-type marker t)
9240 (org-with-remote-undo buffer
9241 (with-current-buffer buffer
9242 (widen)
9243 (goto-char pos)
9244 (setq ts (org-schedule arg time)))
9245 (org-agenda-show-new-time marker ts " S"))
9246 (message "%s" ts)))
9248 (defun org-agenda-deadline (arg &optional time)
9249 "Schedule the item at point.
9250 ARG is passed through to `org-deadline'."
9251 (interactive "P")
9252 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9253 (org-agenda-check-no-diary)
9254 (let* ((marker (or (org-get-at-bol 'org-marker)
9255 (org-agenda-error)))
9256 (buffer (marker-buffer marker))
9257 (pos (marker-position marker))
9259 (org-with-remote-undo buffer
9260 (with-current-buffer buffer
9261 (widen)
9262 (goto-char pos)
9263 (setq ts (org-deadline arg time)))
9264 (org-agenda-show-new-time marker ts " D"))
9265 (message "%s" ts)))
9267 (defun org-agenda-clock-in (&optional arg)
9268 "Start the clock on the currently selected item."
9269 (interactive "P")
9270 (org-agenda-check-no-diary)
9271 (if (equal arg '(4))
9272 (org-clock-in arg)
9273 (let* ((marker (or (org-get-at-bol 'org-marker)
9274 (org-agenda-error)))
9275 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9276 (pos (marker-position marker))
9277 (col (current-column))
9278 newhead)
9279 (org-with-remote-undo (marker-buffer marker)
9280 (with-current-buffer (marker-buffer marker)
9281 (widen)
9282 (goto-char pos)
9283 (org-show-context 'agenda)
9284 (org-cycle-hide-drawers 'children)
9285 (org-clock-in arg)
9286 (setq newhead (org-get-heading)))
9287 (org-agenda-change-all-lines newhead hdmarker))
9288 (org-move-to-column col))))
9290 (defun org-agenda-clock-out ()
9291 "Stop the currently running clock."
9292 (interactive)
9293 (unless (marker-buffer org-clock-marker)
9294 (error "No running clock"))
9295 (let ((marker (make-marker)) (col (current-column)) newhead)
9296 (org-with-remote-undo (marker-buffer org-clock-marker)
9297 (with-current-buffer (marker-buffer org-clock-marker)
9298 (save-excursion
9299 (save-restriction
9300 (widen)
9301 (goto-char org-clock-marker)
9302 (org-back-to-heading t)
9303 (move-marker marker (point))
9304 (org-clock-out)
9305 (setq newhead (org-get-heading))))))
9306 (org-agenda-change-all-lines newhead marker)
9307 (move-marker marker nil)
9308 (org-move-to-column col)
9309 (org-agenda-unmark-clocking-task)))
9311 (defun org-agenda-clock-cancel (&optional arg)
9312 "Cancel the currently running clock."
9313 (interactive "P")
9314 (unless (marker-buffer org-clock-marker)
9315 (user-error "No running clock"))
9316 (org-with-remote-undo (marker-buffer org-clock-marker)
9317 (org-clock-cancel)))
9319 (defun org-agenda-clock-goto ()
9320 "Jump to the currently clocked in task within the agenda.
9321 If the currently clocked in task is not listed in the agenda
9322 buffer, display it in another window."
9323 (interactive)
9324 (let (pos)
9325 (mapc (lambda (o)
9326 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9327 (setq pos (overlay-start o))))
9328 (overlays-in (point-min) (point-max)))
9329 (cond (pos (goto-char pos))
9330 ;; If the currently clocked entry is not in the agenda
9331 ;; buffer, we visit it in another window:
9332 ((bound-and-true-p org-clock-current-task)
9333 (org-switch-to-buffer-other-window (org-clock-goto)))
9334 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9336 (defun org-agenda-diary-entry-in-org-file ()
9337 "Make a diary entry in the file `org-agenda-diary-file'."
9338 (let (d1 d2 char (text "") dp1 dp2)
9339 (if (equal (buffer-name) "*Calendar*")
9340 (setq d1 (calendar-cursor-to-date t)
9341 d2 (car calendar-mark-ring))
9342 (setq dp1 (get-text-property (point-at-bol) 'day))
9343 (unless dp1 (user-error "No date defined in current line"))
9344 (setq d1 (calendar-gregorian-from-absolute dp1)
9345 d2 (and (ignore-errors (mark))
9346 (save-excursion
9347 (goto-char (mark))
9348 (setq dp2 (get-text-property (point-at-bol) 'day)))
9349 (calendar-gregorian-from-absolute dp2))))
9350 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9351 (setq char (read-char-exclusive))
9352 (cond
9353 ((equal char ?d)
9354 (setq text (read-string "Day entry: "))
9355 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9356 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9357 ((equal char ?a)
9358 (setq d1 (list (car d1) (nth 1 d1)
9359 (read-number (format "Reference year [%d]: " (nth 2 d1))
9360 (nth 2 d1))))
9361 (setq text (read-string "Anniversary (use %d to show years): "))
9362 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9363 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9364 ((equal char ?b)
9365 (setq text (read-string "Block entry: "))
9366 (unless (and d1 d2 (not (equal d1 d2)))
9367 (user-error "No block of days selected"))
9368 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9369 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9370 ((equal char ?j)
9371 (org-switch-to-buffer-other-window
9372 (find-file-noselect org-agenda-diary-file))
9373 (require 'org-datetree)
9374 (org-datetree-find-date-create d1)
9375 (org-reveal t))
9376 (t (user-error "Invalid selection character `%c'" char)))))
9378 (defcustom org-agenda-insert-diary-strategy 'date-tree
9379 "Where in `org-agenda-diary-file' should new entries be added?
9380 Valid values:
9382 date-tree in the date tree, as first child of the date
9383 date-tree-last in the date tree, as last child of the date
9384 top-level as top-level entries at the end of the file."
9385 :group 'org-agenda
9386 :type '(choice
9387 (const :tag "first in a date tree" date-tree)
9388 (const :tag "last in a date tree" date-tree-last)
9389 (const :tag "as top level at end of file" top-level)))
9391 (defcustom org-agenda-insert-diary-extract-time nil
9392 "Non-nil means extract any time specification from the diary entry."
9393 :group 'org-agenda
9394 :version "24.1"
9395 :type 'boolean)
9397 (defcustom org-agenda-bulk-mark-char ">"
9398 "A single-character string to be used as the bulk mark."
9399 :group 'org-agenda
9400 :version "24.1"
9401 :type 'string)
9403 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9404 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9405 If TEXT is not empty, it will become the headline of the new entry, and
9406 the resulting entry will not be shown. When TEXT is empty, switch to
9407 `org-agenda-diary-file' and let the user finish the entry there."
9408 (let ((cw (current-window-configuration)))
9409 (org-switch-to-buffer-other-window
9410 (find-file-noselect org-agenda-diary-file))
9411 (widen)
9412 (goto-char (point-min))
9413 (cond
9414 ((eq type 'anniversary)
9415 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9416 (progn
9417 (or (org-at-heading-p t)
9418 (progn
9419 (outline-next-heading)
9420 (insert "* Anniversaries\n\n")
9421 (beginning-of-line -1)))))
9422 (outline-next-heading)
9423 (org-back-over-empty-lines)
9424 (backward-char 1)
9425 (insert "\n")
9426 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9427 (nth 2 d1) (car d1) (nth 1 d1) text)))
9428 ((eq type 'day)
9429 (let ((org-prefix-has-time t)
9430 (org-agenda-time-leading-zero t)
9431 fmt time time2)
9432 (if org-agenda-insert-diary-extract-time
9433 ;; Use org-agenda-format-item to parse text for a time-range and
9434 ;; remove it. FIXME: This is a hack, we should refactor
9435 ;; that function to make time extraction available separately
9436 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9437 time (get-text-property 0 'time fmt)
9438 time2 (if (> (length time) 0)
9439 ;; split-string removes trailing ...... if
9440 ;; no end time given. First space
9441 ;; separates time from date.
9442 (concat " " (car (split-string time "\\.")))
9443 nil)
9444 text (get-text-property 0 'txt fmt)))
9445 (if (eq org-agenda-insert-diary-strategy 'top-level)
9446 (org-agenda-insert-diary-as-top-level text)
9447 (require 'org-datetree)
9448 (org-datetree-find-date-create d1)
9449 (org-agenda-insert-diary-make-new-entry text))
9450 (org-insert-time-stamp (org-time-from-absolute
9451 (calendar-absolute-from-gregorian d1))
9452 nil nil nil nil time2))
9453 (end-of-line 0))
9454 ((eq type 'block)
9455 (if (> (calendar-absolute-from-gregorian d1)
9456 (calendar-absolute-from-gregorian d2))
9457 (setq d1 (prog1 d2 (setq d2 d1))))
9458 (if (eq org-agenda-insert-diary-strategy 'top-level)
9459 (org-agenda-insert-diary-as-top-level text)
9460 (require 'org-datetree)
9461 (org-datetree-find-date-create d1)
9462 (org-agenda-insert-diary-make-new-entry text))
9463 (org-insert-time-stamp (org-time-from-absolute
9464 (calendar-absolute-from-gregorian d1)))
9465 (insert "--")
9466 (org-insert-time-stamp (org-time-from-absolute
9467 (calendar-absolute-from-gregorian d2)))
9468 (end-of-line 0)))
9469 (if (string-match "\\S-" text)
9470 (progn
9471 (set-window-configuration cw)
9472 (message "%s entry added to %s"
9473 (capitalize (symbol-name type))
9474 (abbreviate-file-name org-agenda-diary-file)))
9475 (org-reveal t)
9476 (message "Please finish entry here"))))
9478 (defun org-agenda-insert-diary-as-top-level (text)
9479 "Make new entry as a top-level entry at the end of the file.
9480 Add TEXT as headline, and position the cursor in the second line so that
9481 a timestamp can be added there."
9482 (widen)
9483 (goto-char (point-max))
9484 (unless (bolp) (insert "\n"))
9485 (org-insert-heading nil t t)
9486 (insert text)
9487 (org-end-of-meta-data)
9488 (unless (bolp) (insert "\n"))
9489 (when org-adapt-indentation (indent-to-column 2)))
9491 (defun org-agenda-insert-diary-make-new-entry (text)
9492 "Make a new entry with TEXT as a child of the current subtree.
9493 Position the point in the heading's first body line so that
9494 a timestamp can be added there."
9495 (cond
9496 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9497 (end-of-line)
9498 (org-insert-heading '(4) t)
9499 (org-do-demote))
9501 (outline-next-heading)
9502 (org-back-over-empty-lines)
9503 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9504 (org-insert-heading nil t)
9505 (org-do-demote)))
9506 (let ((col (current-column)))
9507 (insert text)
9508 (org-end-of-meta-data)
9509 ;; Ensure point is left on a blank line, at proper indentation.
9510 (unless (bolp) (insert "\n"))
9511 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9512 (when org-adapt-indentation (indent-to-column col)))
9513 (org-show-set-visibility 'lineage))
9515 (defun org-agenda-diary-entry ()
9516 "Make a diary entry, like the `i' command from the calendar.
9517 All the standard commands work: block, weekly etc.
9518 When `org-agenda-diary-file' points to a file,
9519 `org-agenda-diary-entry-in-org-file' is called instead to create
9520 entries in that Org-mode file."
9521 (interactive)
9522 (if (not (eq org-agenda-diary-file 'diary-file))
9523 (org-agenda-diary-entry-in-org-file)
9524 (require 'diary-lib)
9525 (let* ((char (progn
9526 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9527 (read-char-exclusive)))
9528 (cmd (cdr (assoc char
9529 '((?d . diary-insert-entry)
9530 (?w . diary-insert-weekly-entry)
9531 (?m . diary-insert-monthly-entry)
9532 (?y . diary-insert-yearly-entry)
9533 (?a . diary-insert-anniversary-entry)
9534 (?b . diary-insert-block-entry)
9535 (?c . diary-insert-cyclic-entry)))))
9536 (oldf (symbol-function 'calendar-cursor-to-date))
9537 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9538 (point (point))
9539 (mark (or (mark t) (point))))
9540 (unless cmd
9541 (user-error "No command associated with <%c>" char))
9542 (unless (and (get-text-property point 'day)
9543 (or (not (equal ?b char))
9544 (get-text-property mark 'day)))
9545 (user-error "Don't know which date to use for diary entry"))
9546 ;; We implement this by hacking the `calendar-cursor-to-date' function
9547 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9548 (let ((calendar-mark-ring
9549 (list (calendar-gregorian-from-absolute
9550 (or (get-text-property mark 'day)
9551 (get-text-property point 'day))))))
9552 (unwind-protect
9553 (progn
9554 (fset 'calendar-cursor-to-date
9555 (lambda (&optional error dummy)
9556 (calendar-gregorian-from-absolute
9557 (get-text-property point 'day))))
9558 (call-interactively cmd))
9559 (fset 'calendar-cursor-to-date oldf))))))
9561 (defun org-agenda-execute-calendar-command (cmd)
9562 "Execute a calendar command from the agenda with date from cursor."
9563 (org-agenda-check-type t 'agenda 'timeline)
9564 (require 'diary-lib)
9565 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9566 (user-error "Don't know which date to use for the calendar command"))
9567 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9568 (point (point))
9569 (date (calendar-gregorian-from-absolute
9570 (get-text-property point 'day)))
9571 ;; the following 2 vars are needed in the calendar
9572 (displayed-month (car date))
9573 (displayed-year (nth 2 date)))
9574 (unwind-protect
9575 (progn
9576 (fset 'calendar-cursor-to-date
9577 (lambda (&optional error dummy)
9578 (calendar-gregorian-from-absolute
9579 (get-text-property point 'day))))
9580 (call-interactively cmd))
9581 (fset 'calendar-cursor-to-date oldf))))
9583 (defun org-agenda-phases-of-moon ()
9584 "Display the phases of the moon for the 3 months around the cursor date."
9585 (interactive)
9586 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9588 (defun org-agenda-holidays ()
9589 "Display the holidays for the 3 months around the cursor date."
9590 (interactive)
9591 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9593 (defvar calendar-longitude) ; defined in calendar.el
9594 (defvar calendar-latitude) ; defined in calendar.el
9595 (defvar calendar-location-name) ; defined in calendar.el
9597 (defun org-agenda-sunrise-sunset (arg)
9598 "Display sunrise and sunset for the cursor date.
9599 Latitude and longitude can be specified with the variables
9600 `calendar-latitude' and `calendar-longitude'. When called with prefix
9601 argument, latitude and longitude will be prompted for."
9602 (interactive "P")
9603 (require 'solar)
9604 (let ((calendar-longitude (if arg nil calendar-longitude))
9605 (calendar-latitude (if arg nil calendar-latitude))
9606 (calendar-location-name
9607 (if arg "the given coordinates" calendar-location-name)))
9608 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9610 (defun org-agenda-goto-calendar ()
9611 "Open the Emacs calendar with the date at the cursor."
9612 (interactive)
9613 (org-agenda-check-type t 'agenda 'timeline)
9614 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9615 (user-error "Don't know which date to open in calendar")))
9616 (date (calendar-gregorian-from-absolute day))
9617 (calendar-move-hook nil)
9618 (calendar-view-holidays-initially-flag nil)
9619 (calendar-view-diary-initially-flag nil))
9620 (calendar)
9621 (calendar-goto-date date)))
9623 ;;;###autoload
9624 (defun org-calendar-goto-agenda ()
9625 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9626 This is a command that has to be installed in `calendar-mode-map'."
9627 (interactive)
9628 ;; Temporarily disable sticky agenda since user clearly wants to
9629 ;; refresh view anyway.
9630 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9631 (org-agenda-sticky nil))
9632 (org-agenda-list nil (calendar-absolute-from-gregorian
9633 (calendar-cursor-to-date))
9634 nil)))
9636 (defun org-agenda-convert-date ()
9637 (interactive)
9638 (org-agenda-check-type t 'agenda 'timeline)
9639 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9640 date s)
9641 (unless day
9642 (user-error "Don't know which date to convert"))
9643 (setq date (calendar-gregorian-from-absolute day))
9644 (setq s (concat
9645 "Gregorian: " (calendar-date-string date) "\n"
9646 "ISO: " (calendar-iso-date-string date) "\n"
9647 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9648 "Julian: " (calendar-julian-date-string date) "\n"
9649 "Astron. JD: " (calendar-astro-date-string date)
9650 " (Julian date number at noon UTC)\n"
9651 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9652 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9653 "French: " (calendar-french-date-string date) "\n"
9654 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9655 "Mayan: " (calendar-mayan-date-string date) "\n"
9656 "Coptic: " (calendar-coptic-date-string date) "\n"
9657 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9658 "Persian: " (calendar-persian-date-string date) "\n"
9659 "Chinese: " (calendar-chinese-date-string date) "\n"))
9660 (with-output-to-temp-buffer "*Dates*"
9661 (princ s))
9662 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9664 ;;; Bulk commands
9666 (defun org-agenda-bulk-marked-p ()
9667 "Non-nil when current entry is marked for bulk action."
9668 (eq (get-char-property (point-at-bol) 'type)
9669 'org-marked-entry-overlay))
9671 (defun org-agenda-bulk-mark (&optional arg)
9672 "Mark the entry at point for future bulk action."
9673 (interactive "p")
9674 (dotimes (i (or arg 1))
9675 (unless (org-get-at-bol 'org-agenda-diary-link)
9676 (let* ((m (org-get-at-bol 'org-hd-marker))
9678 (unless (org-agenda-bulk-marked-p)
9679 (unless m (user-error "Nothing to mark at point"))
9680 (push m org-agenda-bulk-marked-entries)
9681 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9682 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9683 (org-get-todo-face "TODO")
9684 'evaporate)
9685 (overlay-put ov 'type 'org-marked-entry-overlay))
9686 (end-of-line 1)
9687 (or (ignore-errors
9688 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9689 (beginning-of-line 2))
9690 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9691 (beginning-of-line 2))
9692 (message "%d entries marked for bulk action"
9693 (length org-agenda-bulk-marked-entries))))))
9695 (defun org-agenda-bulk-mark-all ()
9696 "Mark all entries for future agenda bulk action."
9697 (interactive)
9698 (org-agenda-bulk-mark-regexp "."))
9700 (defun org-agenda-bulk-mark-regexp (regexp)
9701 "Mark entries matching REGEXP for future agenda bulk action."
9702 (interactive "sMark entries matching regexp: ")
9703 (let ((entries-marked 0) txt-at-point)
9704 (save-excursion
9705 (goto-char (point-min))
9706 (goto-char (next-single-property-change (point) 'org-hd-marker))
9707 (while (and (re-search-forward regexp nil t)
9708 (setq txt-at-point (get-text-property (point) 'txt)))
9709 (when (string-match regexp txt-at-point)
9710 (setq entries-marked (1+ entries-marked))
9711 (call-interactively 'org-agenda-bulk-mark))))
9712 (if (not entries-marked)
9713 (message "No entry matching this regexp."))))
9715 (defun org-agenda-bulk-unmark (&optional arg)
9716 "Unmark the entry at point for future bulk action."
9717 (interactive "P")
9718 (if arg
9719 (org-agenda-bulk-unmark-all)
9720 (cond ((org-agenda-bulk-marked-p)
9721 (org-agenda-bulk-remove-overlays
9722 (point-at-bol) (+ 2 (point-at-bol)))
9723 (setq org-agenda-bulk-marked-entries
9724 (delete (org-get-at-bol 'org-hd-marker)
9725 org-agenda-bulk-marked-entries))
9726 (end-of-line 1)
9727 (or (ignore-errors
9728 (goto-char (next-single-property-change (point) 'txt)))
9729 (beginning-of-line 2))
9730 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9731 (beginning-of-line 2))
9732 (message "%d entries left marked for bulk action"
9733 (length org-agenda-bulk-marked-entries)))
9734 (t (message "No entry to unmark here")))))
9736 (defun org-agenda-bulk-toggle-all ()
9737 "Toggle all marks for bulk action."
9738 (interactive)
9739 (save-excursion
9740 (goto-char (point-min))
9741 (while (ignore-errors
9742 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9743 (org-agenda-bulk-toggle))))
9745 (defun org-agenda-bulk-toggle ()
9746 "Toggle the mark at point for bulk action."
9747 (interactive)
9748 (if (org-agenda-bulk-marked-p)
9749 (org-agenda-bulk-unmark)
9750 (org-agenda-bulk-mark)))
9752 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9753 "Remove the mark overlays between BEG and END in the agenda buffer.
9754 BEG and END default to the buffer limits.
9756 This only removes the overlays, it does not remove the markers
9757 from the list in `org-agenda-bulk-marked-entries'."
9758 (interactive)
9759 (mapc (lambda (ov)
9760 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9761 (delete-overlay ov)))
9762 (overlays-in (or beg (point-min)) (or end (point-max)))))
9764 (defun org-agenda-bulk-unmark-all ()
9765 "Remove all marks in the agenda buffer.
9766 This will remove the markers and the overlays."
9767 (interactive)
9768 (if (null org-agenda-bulk-marked-entries)
9769 (message "No entry to unmark")
9770 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9771 (setq org-agenda-bulk-marked-entries nil)
9772 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9774 (defcustom org-agenda-persistent-marks nil
9775 "Non-nil means marked items will stay marked after a bulk action.
9776 You can toggle this interactively by typing `p' when prompted for a
9777 bulk action."
9778 :group 'org-agenda
9779 :version "24.1"
9780 :type 'boolean)
9782 (defun org-agenda-bulk-action (&optional arg)
9783 "Execute an remote-editing action on all marked entries.
9784 The prefix arg is passed through to the command if possible."
9785 (interactive "P")
9786 ;; Make sure we have markers, and only valid ones
9787 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9788 (mapc
9789 (lambda (m)
9790 (unless (and (markerp m)
9791 (marker-buffer m)
9792 (buffer-live-p (marker-buffer m))
9793 (marker-position m))
9794 (user-error "Marker %s for bulk command is invalid" m)))
9795 org-agenda-bulk-marked-entries)
9797 ;; Prompt for the bulk command
9798 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9799 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9800 "[S]catter [f]unction "
9801 (when org-agenda-bulk-custom-functions
9802 (concat " Custom: ["
9803 (mapconcat (lambda(f) (char-to-string (car f)))
9804 org-agenda-bulk-custom-functions "")
9805 "]"))))
9806 (catch 'exit
9807 (let* ((action (read-char-exclusive))
9808 (org-log-refile (if org-log-refile 'time nil))
9809 (entries (reverse org-agenda-bulk-marked-entries))
9810 (org-overriding-default-time
9811 (if (get-text-property (point) 'org-agenda-date-header)
9812 (org-get-cursor-date)))
9813 redo-at-end
9814 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9815 (cond
9816 ((equal action ?p)
9817 (let ((org-agenda-persistent-marks
9818 (not org-agenda-persistent-marks)))
9819 (org-agenda-bulk-action)
9820 (throw 'exit nil)))
9822 ((equal action ?$)
9823 (setq cmd '(org-agenda-archive)))
9825 ((equal action ?A)
9826 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9828 ((member action '(?r ?w))
9829 (setq rfloc (org-refile-get-location
9830 "Refile to"
9831 (marker-buffer (car entries))
9832 org-refile-allow-creating-parent-nodes))
9833 (if (nth 3 rfloc)
9834 (setcar (nthcdr 3 rfloc)
9835 (move-marker (make-marker) (nth 3 rfloc)
9836 (or (get-file-buffer (nth 1 rfloc))
9837 (find-buffer-visiting (nth 1 rfloc))
9838 (error "This should not happen")))))
9840 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9841 redo-at-end t))
9843 ((equal action ?t)
9844 (setq state (completing-read
9845 "Todo state: "
9846 (with-current-buffer (marker-buffer (car entries))
9847 (mapcar #'list org-todo-keywords-1))))
9848 (setq cmd `(let ((org-inhibit-blocking t)
9849 (org-inhibit-logging 'note))
9850 (org-agenda-todo ,state))))
9852 ((memq action '(?- ?+))
9853 (setq tag (completing-read
9854 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9855 (with-current-buffer (marker-buffer (car entries))
9856 (delq nil
9857 (mapcar (lambda (x) (and (stringp (car x)) x))
9858 org-current-tag-alist)))))
9859 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9861 ((memq action '(?s ?d))
9862 (let* ((time
9863 (unless arg
9864 (org-read-date
9865 nil nil nil
9866 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9867 org-overriding-default-time)))
9868 (c1 (if (eq action ?s) 'org-agenda-schedule
9869 'org-agenda-deadline)))
9870 ;; Make sure to not prompt for a note when bulk
9871 ;; rescheduling as Org cannot cope with simultaneous Org.
9872 ;; Besides, it could be annoying depending on the number
9873 ;; of items re-scheduled.
9874 (setq cmd `(eval '(let ((org-log-reschedule
9875 (and org-log-reschedule 'time)))
9876 (,c1 arg ,time))))))
9878 ((equal action ?S)
9879 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9880 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9881 (let ((days (read-number
9882 (format "Scatter tasks across how many %sdays: "
9883 (if arg "week" "")) 7)))
9884 (setq cmd
9885 `(let ((distance (1+ (random ,days))))
9886 (if arg
9887 (let ((dist distance)
9888 (day-of-week
9889 (calendar-day-of-week
9890 (calendar-gregorian-from-absolute (org-today)))))
9891 (dotimes (i (1+ dist))
9892 (while (member day-of-week org-agenda-weekend-days)
9893 (cl-incf distance)
9894 (cl-incf day-of-week)
9895 (when (= day-of-week 7)
9896 (setq day-of-week 0)))
9897 (cl-incf day-of-week)
9898 (when (= day-of-week 7)
9899 (setq day-of-week 0)))))
9900 ;; silently fail when try to replan a sexp entry
9901 (condition-case nil
9902 (let* ((date (calendar-gregorian-from-absolute
9903 (+ (org-today) distance)))
9904 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9905 (nth 2 date))))
9906 (org-agenda-schedule nil time))
9907 (error nil)))))))
9909 ((assoc action org-agenda-bulk-custom-functions)
9910 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9911 redo-at-end t))
9913 ((equal action ?f)
9914 (setq cmd (list (intern
9915 (completing-read "Function: "
9916 obarray 'fboundp t nil nil)))))
9918 (t (user-error "Invalid bulk action")))
9920 ;; Sort the markers, to make sure that parents are handled before children
9921 (setq entries (sort entries
9922 (lambda (a b)
9923 (cond
9924 ((equal (marker-buffer a) (marker-buffer b))
9925 (< (marker-position a) (marker-position b)))
9927 (string< (buffer-name (marker-buffer a))
9928 (buffer-name (marker-buffer b))))))))
9930 ;; Now loop over all markers and apply cmd
9931 (while (setq e (pop entries))
9932 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9933 (if (not pos)
9934 (progn (message "Skipping removed entry at %s" e)
9935 (setq cntskip (1+ cntskip)))
9936 (goto-char pos)
9937 (let (org-loop-over-headlines-in-active-region)
9938 (eval cmd))
9939 ;; `post-command-hook' is not run yet. We make sure any
9940 ;; pending log note is processed.
9941 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9942 (memq 'org-add-log-note post-command-hook))
9943 (org-add-log-note))
9944 (setq cnt (1+ cnt))))
9945 (when redo-at-end (org-agenda-redo))
9946 (unless org-agenda-persistent-marks
9947 (org-agenda-bulk-unmark-all))
9948 (message "Acted on %d entries%s%s"
9950 (if (= cntskip 0)
9952 (format ", skipped %d (disappeared before their turn)"
9953 cntskip))
9954 (if (not org-agenda-persistent-marks)
9955 "" " (kept marked)"))))))
9957 (defun org-agenda-capture (&optional with-time)
9958 "Call `org-capture' with the date at point.
9959 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9960 current HH:MM time."
9961 (interactive "P")
9962 (if (not (eq major-mode 'org-agenda-mode))
9963 (user-error "You cannot do this outside of agenda buffers")
9964 (let ((org-overriding-default-time
9965 (org-get-cursor-date (equal with-time 1))))
9966 (call-interactively 'org-capture))))
9968 ;;; Dragging agenda lines forward/backward
9970 (defun org-agenda-reapply-filters ()
9971 "Re-apply all agenda filters."
9972 (mapcar
9973 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
9974 `((,org-agenda-tag-filter tag)
9975 (,org-agenda-category-filter category)
9976 (,org-agenda-regexp-filter regexp)
9977 (,org-agenda-effort-filter effort)
9978 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9979 (,(get 'org-agenda-category-filter :preset-filter) category)
9980 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9981 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9983 (defun org-agenda-drag-line-forward (arg &optional backward)
9984 "Drag an agenda line forward by ARG lines.
9985 When the optional argument `backward' is non-nil, move backward."
9986 (interactive "p")
9987 (let ((inhibit-read-only t) lst line)
9988 (if (or (not (get-text-property (point) 'txt))
9989 (save-excursion
9990 (dotimes (n arg)
9991 (move-beginning-of-line (if backward 0 2))
9992 (push (not (get-text-property (point) 'txt)) lst))
9993 (delq nil lst)))
9994 (message "Cannot move line forward")
9995 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9996 (move-beginning-of-line 1)
9997 (setq line (buffer-substring (point) end))
9998 (delete-region (point) end)
9999 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10000 (insert line)
10001 (org-agenda-reapply-filters)
10002 (org-agenda-mark-clocking-task)
10003 (move-beginning-of-line 0)))))
10005 (defun org-agenda-drag-line-backward (arg)
10006 "Drag an agenda line backward by ARG lines."
10007 (interactive "p")
10008 (org-agenda-drag-line-forward arg t))
10010 ;;; Flagging notes
10012 (defun org-agenda-show-the-flagging-note ()
10013 "Display the flagging note in the other window.
10014 When called a second time in direct sequence, offer to remove the FLAGGING
10015 tag and (if present) the flagging note."
10016 (interactive)
10017 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10018 (win (selected-window))
10019 note heading newhead)
10020 (unless hdmarker
10021 (user-error "No linked entry at point"))
10022 (if (and (eq this-command last-command)
10023 (y-or-n-p "Unflag and remove any flagging note? "))
10024 (progn
10025 (org-agenda-remove-flag hdmarker)
10026 (let ((win (get-buffer-window "*Flagging Note*")))
10027 (and win (delete-window win)))
10028 (message "Entry unflagged"))
10029 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10030 (unless note
10031 (user-error "No flagging note"))
10032 (org-kill-new note)
10033 (org-switch-to-buffer-other-window "*Flagging Note*")
10034 (erase-buffer)
10035 (insert note)
10036 (goto-char (point-min))
10037 (while (re-search-forward "\\\\n" nil t)
10038 (replace-match "\n" t t))
10039 (goto-char (point-min))
10040 (select-window win)
10041 (message "%s" (substitute-command-keys "Flagging note pushed to \
10042 kill ring. Press \\[org-agenda-show-the-flagging-note] again to remove \
10043 tag and note")))))
10045 (defun org-agenda-remove-flag (marker)
10046 "Remove the FLAGGED tag and any flagging note in the entry."
10047 (let (newhead)
10048 (org-with-point-at marker
10049 (org-toggle-tag "FLAGGED" 'off)
10050 (org-entry-delete nil "THEFLAGGINGNOTE")
10051 (setq newhead (org-get-heading)))
10052 (org-agenda-change-all-lines newhead marker)
10053 (message "Entry unflagged")))
10055 (defun org-agenda-get-any-marker (&optional pos)
10056 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10057 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10059 ;;; Appointment reminders
10061 (defvar appt-time-msg-list) ; defined in appt.el
10063 ;;;###autoload
10064 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10065 "Activate appointments found in `org-agenda-files'.
10066 With a \\[universal-argument] prefix, refresh the list of
10067 appointments.
10069 If FILTER is t, interactively prompt the user for a regular
10070 expression, and filter out entries that don't match it.
10072 If FILTER is a string, use this string as a regular expression
10073 for filtering entries out.
10075 If FILTER is a function, filter out entries against which
10076 calling the function returns nil. This function takes one
10077 argument: an entry from `org-agenda-get-day-entries'.
10079 FILTER can also be an alist with the car of each cell being
10080 either `headline' or `category'. For example:
10082 \\='((headline \"IMPORTANT\")
10083 (category \"Work\"))
10085 will only add headlines containing IMPORTANT or headlines
10086 belonging to the \"Work\" category.
10088 ARGS are symbols indicating what kind of entries to consider.
10089 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10090 \(i.e., deadlines and scheduled items with a hh:mm specification)
10091 and :timestamp entries. See the docstring of `org-diary' for
10092 details and examples.
10094 If an entry has a APPT_WARNTIME property, its value will be used
10095 to override `appt-message-warning-time'."
10096 (interactive "P")
10097 (if refresh (setq appt-time-msg-list nil))
10098 (if (eq filter t)
10099 (setq filter (read-from-minibuffer "Regexp filter: ")))
10100 (let* ((cnt 0) ; count added events
10101 (scope (or args '(:deadline* :scheduled* :timestamp)))
10102 (org-agenda-new-buffers nil)
10103 (org-deadline-warning-days 0)
10104 ;; Do not use `org-today' here because appt only takes
10105 ;; time and without date as argument, so it may pass wrong
10106 ;; information otherwise
10107 (today (org-date-to-gregorian
10108 (time-to-days (current-time))))
10109 (org-agenda-restrict nil)
10110 (files (org-agenda-files 'unrestricted)) entries file
10111 (org-agenda-buffer nil))
10112 ;; Get all entries which may contain an appt
10113 (org-agenda-prepare-buffers files)
10114 (while (setq file (pop files))
10115 (setq entries
10116 (delq nil
10117 (append entries
10118 (apply 'org-agenda-get-day-entries
10119 file today scope)))))
10120 ;; Map thru entries and find if we should filter them out
10121 (mapc
10122 (lambda (x)
10123 (let* ((evt (org-trim
10124 (replace-regexp-in-string
10125 org-bracket-link-regexp "\\3"
10126 (or (get-text-property 1 'txt x) ""))))
10127 (cat (get-text-property (1- (length x)) 'org-category x))
10128 (tod (get-text-property 1 'time-of-day x))
10129 (ok (or (null filter)
10130 (and (stringp filter) (string-match filter evt))
10131 (and (functionp filter) (funcall filter x))
10132 (and (listp filter)
10133 (let ((cat-filter (cadr (assoc 'category filter)))
10134 (evt-filter (cadr (assoc 'headline filter))))
10135 (or (and (stringp cat-filter)
10136 (string-match cat-filter cat))
10137 (and (stringp evt-filter)
10138 (string-match evt-filter evt)))))))
10139 (wrn (get-text-property 1 'warntime x)))
10140 ;; FIXME: Shall we remove text-properties for the appt text?
10141 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10142 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10143 (setq tod (concat "00" (number-to-string tod)))
10144 (setq tod (when (string-match
10145 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10146 (concat (match-string 1 tod) ":"
10147 (match-string 2 tod))))
10148 (when (if (version< emacs-version "23.3")
10149 (appt-add tod evt)
10150 (appt-add tod evt wrn))
10151 (setq cnt (1+ cnt))))))
10152 entries)
10153 (org-release-buffers org-agenda-new-buffers)
10154 (if (eq cnt 0)
10155 (message "No event to add")
10156 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10158 (defun org-agenda-today-p (date)
10159 "Non nil when DATE means today.
10160 DATE is either a list of the form (month day year) or a number of
10161 days as returned by `calendar-absolute-from-gregorian' or
10162 `org-today'. This function considers `org-extend-today-until'
10163 when defining today."
10164 (eq (org-today)
10165 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10167 (defun org-agenda-todo-yesterday (&optional arg)
10168 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10169 (interactive "P")
10170 (let* ((org-use-effective-time t)
10171 (hour (nth 2 (decode-time (org-current-time))))
10172 (org-extend-today-until (1+ hour)))
10173 (org-agenda-todo arg)))
10175 (provide 'org-agenda)
10177 ;;; org-agenda.el ends here