Add test for table alignment
[org-mode.git] / lisp / org-agenda.el
blob4559f901300cc9ac25fa4987f559342eeca847c6
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.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'cl-lib)
49 (require 'org)
50 (require 'org-macs)
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-iso-to-absolute "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-columns-remove-overlays "org-colview" ())
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-agenda-columns "org-colview" ())
83 (declare-function org-add-archive-files "org-archive" (files))
84 (declare-function org-capture "org-capture" (&optional goto keys))
86 (defvar calendar-mode-map)
87 (defvar org-clock-current-task)
88 (defvar org-current-tag-alist)
89 (defvar org-mobile-force-id-on-agenda-items)
90 (defvar org-habit-show-habits)
91 (defvar org-habit-show-habits-only-for-today)
92 (defvar org-habit-show-all-today)
94 ;; Defined somewhere in this file, but used before definition.
95 (defvar org-agenda-buffer-name "*Org Agenda*")
96 (defvar org-agenda-overriding-header nil)
97 (defvar org-agenda-title-append nil)
98 (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
99 (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
100 (defvar original-date) ; dynamically scoped, calendar.el does scope this
102 (defvar org-agenda-undo-list nil
103 "List of undoable operations in the agenda since last refresh.")
104 (defvar org-agenda-pending-undo-list nil
105 "In a series of undo commands, this is the list of remaining undo items.")
107 (defcustom org-agenda-confirm-kill 1
108 "When set, remote killing from the agenda buffer needs confirmation.
109 When t, a confirmation is always needed. When a number N, confirmation is
110 only needed when the text to be killed contains more than N non-white lines."
111 :group 'org-agenda
112 :type '(choice
113 (const :tag "Never" nil)
114 (const :tag "Always" t)
115 (integer :tag "When more than N lines")))
117 (defcustom org-agenda-compact-blocks nil
118 "Non-nil means make the block agenda more compact.
119 This is done globally by leaving out lines like the agenda span
120 name and week number or the separator lines."
121 :group 'org-agenda
122 :type 'boolean)
124 (defcustom org-agenda-block-separator ?=
125 "The separator between blocks in the agenda.
126 If this is a string, it will be used as the separator, with a newline added.
127 If it is a character, it will be repeated to fill the window width.
128 If nil the separator is disabled. In `org-agenda-custom-commands' this
129 addresses the separator between the current and the previous block."
130 :group 'org-agenda
131 :type '(choice
132 (const :tag "Disabled" nil)
133 (character)
134 (string)))
136 (defgroup org-agenda-export nil
137 "Options concerning exporting agenda views in Org mode."
138 :tag "Org Agenda Export"
139 :group 'org-agenda)
141 (defcustom org-agenda-with-colors t
142 "Non-nil means use colors in agenda views."
143 :group 'org-agenda-export
144 :type 'boolean)
146 (defcustom org-agenda-exporter-settings nil
147 "Alist of variable/value pairs that should be active during agenda export.
148 This is a good place to set options for ps-print and for htmlize.
149 Note that the way this is implemented, the values will be evaluated
150 before assigned to the variables. So make sure to quote values you do
151 *not* want evaluated, for example
153 (setq org-agenda-exporter-settings
154 \\='((ps-print-color-p \\='black-white)))"
155 :group 'org-agenda-export
156 :type '(repeat
157 (list
158 (variable)
159 (sexp :tag "Value"))))
161 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
162 "Hook run in a temporary buffer before writing the agenda to an export file.
163 A useful function for this hook is `org-agenda-add-entry-text'."
164 :group 'org-agenda-export
165 :type 'hook
166 :options '(org-agenda-add-entry-text))
168 (defcustom org-agenda-add-entry-text-maxlines 0
169 "Maximum number of entry text lines to be added to agenda.
170 This is only relevant when `org-agenda-add-entry-text' is part of
171 `org-agenda-before-write-hook', which is the default.
172 When this is 0, nothing will happen. When it is greater than 0, it
173 specifies the maximum number of lines that will be added for each entry
174 that is listed in the agenda view.
176 Note that this variable is not used during display, only when exporting
177 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
178 and `org-agenda-entry-text-maxlines'."
179 :group 'org-agenda
180 :type 'integer)
182 (defcustom org-agenda-add-entry-text-descriptive-links t
183 "Non-nil means export org-links as descriptive links in agenda added text.
184 This variable applies to the text added to the agenda when
185 `org-agenda-add-entry-text-maxlines' is larger than 0.
186 When this variable nil, the URL will (also) be shown."
187 :group 'org-agenda
188 :type 'boolean)
190 (defcustom org-agenda-export-html-style nil
191 "The style specification for exported HTML Agenda files.
192 If this variable contains a string, it will replace the default <style>
193 section as produced by `htmlize'.
194 Since there are different ways of setting style information, this variable
195 needs to contain the full HTML structure to provide a style, including the
196 surrounding HTML tags. The style specifications should include definitions
197 the fonts used by the agenda, here is an example:
199 <style type=\"text/css\">
200 p { font-weight: normal; color: gray; }
201 .org-agenda-structure {
202 font-size: 110%;
203 color: #003399;
204 font-weight: 600;
206 .org-todo {
207 color: #cc6666;
208 font-weight: bold;
210 .org-agenda-done {
211 color: #339933;
213 .org-done {
214 color: #339933;
216 .title { text-align: center; }
217 .todo, .deadline { color: red; }
218 .done { color: green; }
219 </style>
221 or, if you want to keep the style in a file,
223 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
225 As the value of this option simply gets inserted into the HTML <head> header,
226 you can \"misuse\" it to also add other text to the header."
227 :group 'org-agenda-export
228 :group 'org-export-html
229 :type '(choice
230 (const nil)
231 (string)))
233 (defcustom org-agenda-persistent-filter nil
234 "When set, keep filters from one agenda view to the next."
235 :group 'org-agenda
236 :type 'boolean)
238 (defgroup org-agenda-custom-commands nil
239 "Options concerning agenda views in Org mode."
240 :tag "Org Agenda Custom Commands"
241 :group 'org-agenda)
243 (defconst org-sorting-choice
244 '(choice
245 (const time-up) (const time-down)
246 (const timestamp-up) (const timestamp-down)
247 (const scheduled-up) (const scheduled-down)
248 (const deadline-up) (const deadline-down)
249 (const ts-up) (const ts-down)
250 (const tsia-up) (const tsia-down)
251 (const category-keep) (const category-up) (const category-down)
252 (const tag-down) (const tag-up)
253 (const priority-up) (const priority-down)
254 (const todo-state-up) (const todo-state-down)
255 (const effort-up) (const effort-down)
256 (const habit-up) (const habit-down)
257 (const alpha-up) (const alpha-down)
258 (const user-defined-up) (const user-defined-down))
259 "Sorting choices.")
261 ;; Keep custom values for `org-agenda-filter-preset' compatible with
262 ;; the new variable `org-agenda-tag-filter-preset'.
263 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
264 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
266 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
267 "List of types searched for when creating the daily/weekly agenda.
268 This variable is a list of symbols that controls the types of
269 items that appear in the daily/weekly agenda. Allowed symbols in this
270 list are are
272 :timestamp List items containing a date stamp or date range matching
273 the selected date. This includes sexp entries in angular
274 brackets.
276 :sexp List entries resulting from plain diary-like sexps.
278 :deadline List deadline due on that date. When the date is today,
279 also list any deadlines past due, or due within
280 `org-deadline-warning-days'. `:deadline' must appear before
281 `:scheduled' if the setting of
282 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
283 any effect.
285 :deadline* Same as above, but only include the deadline if it has an
286 hour specification as [h]h:mm.
288 :scheduled List all items which are scheduled for the given date.
289 The diary for *today* also contains items which were
290 scheduled earlier and are not yet marked DONE.
292 :scheduled* Same as above, but only include the scheduled item if it
293 has an hour specification as [h]h:mm.
295 By default, all four non-starred types are turned on.
297 When :scheduled* or :deadline* are included, :schedule or :deadline
298 will be ignored.
300 Never set this variable globally using `setq', because then it
301 will apply to all future agenda commands. Instead, bind it with
302 `let' to scope it dynamically into the agenda-constructing
303 command. A good way to set it is through options in
304 `org-agenda-custom-commands'. For a more flexible (though
305 somewhat less efficient) way of determining what is included in
306 the daily/weekly agenda, see `org-agenda-skip-function'.")
308 (defconst org-agenda-custom-commands-local-options
309 `(repeat :tag "Local settings for this command. Remember to quote values"
310 (choice :tag "Setting"
311 (list :tag "Heading for this block"
312 (const org-agenda-overriding-header)
313 (string :tag "Headline"))
314 (list :tag "Files to be searched"
315 (const org-agenda-files)
316 (list
317 (const :format "" quote)
318 (repeat (file))))
319 (list :tag "Sorting strategy"
320 (const org-agenda-sorting-strategy)
321 (list
322 (const :format "" quote)
323 (repeat
324 ,org-sorting-choice)))
325 (list :tag "Prefix format"
326 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
327 (string))
328 (list :tag "Number of days in agenda"
329 (const org-agenda-span)
330 (choice (const :tag "Day" day)
331 (const :tag "Week" week)
332 (const :tag "Fortnight" fortnight)
333 (const :tag "Month" month)
334 (const :tag "Year" year)
335 (integer :tag "Custom")))
336 (list :tag "Fixed starting date"
337 (const org-agenda-start-day)
338 (string :value "2007-11-01"))
339 (list :tag "Start on day of week"
340 (const org-agenda-start-on-weekday)
341 (choice :value 1
342 (const :tag "Today" nil)
343 (integer :tag "Weekday No.")))
344 (list :tag "Include data from diary"
345 (const org-agenda-include-diary)
346 (boolean))
347 (list :tag "Deadline Warning days"
348 (const org-deadline-warning-days)
349 (integer :value 1))
350 (list :tag "Category filter preset"
351 (const org-agenda-category-filter-preset)
352 (list
353 (const :format "" quote)
354 (repeat
355 (string :tag "+category or -category"))))
356 (list :tag "Tags filter preset"
357 (const org-agenda-tag-filter-preset)
358 (list
359 (const :format "" quote)
360 (repeat
361 (string :tag "+tag or -tag"))))
362 (list :tag "Effort filter preset"
363 (const org-agenda-effort-filter-preset)
364 (list
365 (const :format "" quote)
366 (repeat
367 (string :tag "+=10 or -=10 or +<10 or ->10"))))
368 (list :tag "Regexp filter preset"
369 (const org-agenda-regexp-filter-preset)
370 (list
371 (const :format "" quote)
372 (repeat
373 (string :tag "+regexp or -regexp"))))
374 (list :tag "Set daily/weekly entry types"
375 (const org-agenda-entry-types)
376 (list
377 (const :format "" quote)
378 (set :greedy t :value ,org-agenda-entry-types
379 (const :deadline)
380 (const :scheduled)
381 (const :deadline*)
382 (const :scheduled*)
383 (const :timestamp)
384 (const :sexp))))
385 (list :tag "Standard skipping condition"
386 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
387 (const org-agenda-skip-function)
388 (list
389 (const :format "" quote)
390 (list
391 (choice
392 :tag "Skipping range"
393 (const :tag "Skip entry" org-agenda-skip-entry-if)
394 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
395 (repeat :inline t :tag "Conditions for skipping"
396 (choice
397 :tag "Condition type"
398 (list :tag "Regexp matches" :inline t
399 (const :format "" 'regexp)
400 (regexp))
401 (list :tag "Regexp does not match" :inline t
402 (const :format "" 'notregexp)
403 (regexp))
404 (list :tag "TODO state is" :inline t
405 (const 'todo)
406 (choice
407 (const :tag "Any not-done state" 'todo)
408 (const :tag "Any done state" 'done)
409 (const :tag "Any state" 'any)
410 (list :tag "Keyword list"
411 (const :format "" quote)
412 (repeat (string :tag "Keyword")))))
413 (list :tag "TODO state is not" :inline t
414 (const 'nottodo)
415 (choice
416 (const :tag "Any not-done state" 'todo)
417 (const :tag "Any done state" 'done)
418 (const :tag "Any state" 'any)
419 (list :tag "Keyword list"
420 (const :format "" quote)
421 (repeat (string :tag "Keyword")))))
422 (const :tag "scheduled" 'scheduled)
423 (const :tag "not scheduled" 'notscheduled)
424 (const :tag "deadline" 'deadline)
425 (const :tag "no deadline" 'notdeadline)
426 (const :tag "timestamp" 'timestamp)
427 (const :tag "no timestamp" 'nottimestamp))))))
428 (list :tag "Non-standard skipping condition"
429 :value (org-agenda-skip-function)
430 (const org-agenda-skip-function)
431 (sexp :tag "Function or form (quoted!)"))
432 (list :tag "Any variable"
433 (variable :tag "Variable")
434 (sexp :tag "Value (sexp)"))))
435 "Selection of examples for agenda command settings.
436 This will be spliced into the custom type of
437 `org-agenda-custom-commands'.")
440 (defcustom org-agenda-custom-commands
441 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
442 "Custom commands for the agenda.
443 \\<org-mode-map>
444 These commands will be offered on the splash screen displayed by the
445 agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
447 (key desc type match settings files)
449 key The key (one or more characters as a string) to be associated
450 with the command.
451 desc A description of the command, when omitted or nil, a default
452 description is built using MATCH.
453 type The command type, any of the following symbols:
454 agenda The daily/weekly agenda.
455 todo Entries with a specific TODO keyword, in all agenda files.
456 search Entries containing search words entry or headline.
457 tags Tags/Property/TODO match in all agenda files.
458 tags-todo Tags/P/T match in all agenda files, TODO entries only.
459 todo-tree Sparse tree of specific TODO keyword in *current* file.
460 tags-tree Sparse tree with all tags matches in *current* file.
461 occur-tree Occur sparse tree for *current* file.
462 ... A user-defined function.
463 match What to search for:
464 - a single keyword for TODO keyword searches
465 - a tags match expression for tags searches
466 - a word search expression for text searches.
467 - a regular expression for occur searches
468 For all other commands, this should be the empty string.
469 settings A list of option settings, similar to that in a let form, so like
470 this: ((opt1 val1) (opt2 val2) ...). The values will be
471 evaluated at the moment of execution, so quote them when needed.
472 files A list of files file to write the produced agenda buffer to
473 with the command `org-store-agenda-views'.
474 If a file name ends in \".html\", an HTML version of the buffer
475 is written out. If it ends in \".ps\", a postscript version is
476 produced. Otherwise, only the plain text is written to the file.
478 You can also define a set of commands, to create a composite agenda buffer.
479 In this case, an entry looks like this:
481 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
483 where
485 desc A description string to be displayed in the dispatcher menu.
486 cmd An agenda command, similar to the above. However, tree commands
487 are not allowed, but instead you can get agenda and global todo list.
488 So valid commands for a set are:
489 (agenda \"\" settings)
490 (alltodo \"\" settings)
491 (stuck \"\" settings)
492 (todo \"match\" settings files)
493 (search \"match\" settings files)
494 (tags \"match\" settings files)
495 (tags-todo \"match\" settings files)
497 Each command can carry a list of options, and another set of options can be
498 given for the whole set of commands. Individual command options take
499 precedence over the general options.
501 When using several characters as key to a command, the first characters
502 are prefix commands. For the dispatcher to display useful information, you
503 should provide a description for the prefix, like
505 (setq org-agenda-custom-commands
506 \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
507 (\"hl\" tags \"+HOME+Lisa\")
508 (\"hp\" tags \"+HOME+Peter\")
509 (\"hk\" tags \"+HOME+Kim\")))"
510 :group 'org-agenda-custom-commands
511 :type `(repeat
512 (choice :value ("x" "Describe command here" tags "" nil)
513 (list :tag "Single command"
514 (string :tag "Access Key(s) ")
515 (option (string :tag "Description"))
516 (choice
517 (const :tag "Agenda" agenda)
518 (const :tag "TODO list" alltodo)
519 (const :tag "Search words" search)
520 (const :tag "Stuck projects" stuck)
521 (const :tag "Tags/Property match (all agenda files)" tags)
522 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
523 (const :tag "TODO keyword search (all agenda files)" todo)
524 (const :tag "Tags sparse tree (current buffer)" tags-tree)
525 (const :tag "TODO keyword tree (current buffer)" todo-tree)
526 (const :tag "Occur tree (current buffer)" occur-tree)
527 (sexp :tag "Other, user-defined function"))
528 (string :tag "Match (only for some commands)")
529 ,org-agenda-custom-commands-local-options
530 (option (repeat :tag "Export" (file :tag "Export to"))))
531 (list :tag "Command series, all agenda files"
532 (string :tag "Access Key(s)")
533 (string :tag "Description ")
534 (repeat :tag "Component"
535 (choice
536 (list :tag "Agenda"
537 (const :format "" agenda)
538 (const :tag "" :format "" "")
539 ,org-agenda-custom-commands-local-options)
540 (list :tag "TODO list (all keywords)"
541 (const :format "" alltodo)
542 (const :tag "" :format "" "")
543 ,org-agenda-custom-commands-local-options)
544 (list :tag "Search words"
545 (const :format "" search)
546 (string :tag "Match")
547 ,org-agenda-custom-commands-local-options)
548 (list :tag "Stuck projects"
549 (const :format "" stuck)
550 (const :tag "" :format "" "")
551 ,org-agenda-custom-commands-local-options)
552 (list :tag "Tags search"
553 (const :format "" tags)
554 (string :tag "Match")
555 ,org-agenda-custom-commands-local-options)
556 (list :tag "Tags search, TODO entries only"
557 (const :format "" tags-todo)
558 (string :tag "Match")
559 ,org-agenda-custom-commands-local-options)
560 (list :tag "TODO keyword search"
561 (const :format "" todo)
562 (string :tag "Match")
563 ,org-agenda-custom-commands-local-options)
564 (list :tag "Other, user-defined function"
565 (symbol :tag "function")
566 (string :tag "Match")
567 ,org-agenda-custom-commands-local-options)))
569 (repeat :tag "Settings for entire command set"
570 (list (variable :tag "Any variable")
571 (sexp :tag "Value")))
572 (option (repeat :tag "Export" (file :tag "Export to"))))
573 (cons :tag "Prefix key documentation"
574 (string :tag "Access Key(s)")
575 (string :tag "Description ")))))
577 (defcustom org-agenda-query-register ?o
578 "The register holding the current query string.
579 The purpose of this is that if you construct a query string interactively,
580 you can then use it to define a custom command."
581 :group 'org-agenda-custom-commands
582 :type 'character)
584 (defcustom org-stuck-projects
585 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
586 "How to identify stuck projects.
587 This is a list of four items:
588 1. A tags/todo/property matcher string that is used to identify a project.
589 See the manual for a description of tag and property searches.
590 The entire tree below a headline matched by this is considered one project.
591 2. A list of TODO keywords identifying non-stuck projects.
592 If the project subtree contains any headline with one of these todo
593 keywords, the project is considered to be not stuck. If you specify
594 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
595 3. A list of tags identifying non-stuck projects.
596 If the project subtree contains any headline with one of these tags,
597 the project is considered to be not stuck. If you specify \"*\" as
598 a tag, any tag will mark the project unstuck. Note that this is about
599 the explicit presence of a tag somewhere in the subtree, inherited
600 tags do not count here. If inherited tags make a project not stuck,
601 use \"-TAG\" in the tags part of the matcher under (1.) above.
602 4. An arbitrary regular expression matching non-stuck projects.
604 If the project turns out to be not stuck, search continues also in the
605 subtree to see if any of the subtasks have project status.
607 See also the variable `org-tags-match-list-sublevels' which applies
608 to projects matched by this search as well.
610 After defining this variable, you may use `\\[org-agenda-list-stuck-projects]'
611 \(bound to `C-c a #') to produce the list."
612 :group 'org-agenda-custom-commands
613 :type '(list
614 (string :tag "Tags/TODO match to identify a project")
615 (repeat :tag "Projects are *not* stuck if they have an entry with \
616 TODO keyword any of" (string))
617 (repeat :tag "Projects are *not* stuck if they have an entry with \
618 TAG being any of" (string))
619 (regexp :tag "Projects are *not* stuck if this regexp matches inside \
620 the subtree")))
622 (defgroup org-agenda-skip nil
623 "Options concerning skipping parts of agenda files."
624 :tag "Org Agenda Skip"
625 :group 'org-agenda)
627 (defcustom org-agenda-skip-function-global nil
628 "Function to be called at each match during agenda construction.
629 If this function returns nil, the current match should not be skipped.
630 If the function decided to skip an agenda match, is must return the
631 buffer position from which the search should be continued.
632 This may also be a Lisp form, which will be evaluated.
634 This variable will be applied to every agenda match, including
635 tags/property searches and TODO lists. So try to make the test function
636 do its checking as efficiently as possible. To implement a skipping
637 condition just for specific agenda commands, use the variable
638 `org-agenda-skip-function' which can be set in the options section
639 of custom agenda commands."
640 :group 'org-agenda-skip
641 :type 'sexp)
643 (defgroup org-agenda-daily/weekly nil
644 "Options concerning the daily/weekly agenda."
645 :tag "Org Agenda Daily/Weekly"
646 :group 'org-agenda)
647 (defgroup org-agenda-todo-list nil
648 "Options concerning the global todo list agenda view."
649 :tag "Org Agenda Todo List"
650 :group 'org-agenda)
651 (defgroup org-agenda-match-view nil
652 "Options concerning the general tags/property/todo match agenda view."
653 :tag "Org Agenda Match View"
654 :group 'org-agenda)
655 (defgroup org-agenda-search-view nil
656 "Options concerning the search agenda view."
657 :tag "Org Agenda Search View"
658 :group 'org-agenda)
660 (defvar org-agenda-archives-mode nil
661 "Non-nil means the agenda will include archived items.
662 If this is the symbol `trees', trees in the selected agenda scope
663 that are marked with the ARCHIVE tag will be included anyway. When this is
664 t, also all archive files associated with the current selection of agenda
665 files will be included.")
667 (defcustom org-agenda-restriction-lock-highlight-subtree t
668 "Non-nil means highlight the whole subtree when restriction is active.
669 Otherwise only highlight the headline. Highlighting the whole subtree is
670 useful to ensure no edits happen beyond the restricted region."
671 :group 'org-agenda
672 :type 'boolean)
674 (defcustom org-agenda-skip-comment-trees t
675 "Non-nil means skip trees that start with the COMMENT keyword.
676 When nil, these trees are also scanned by agenda commands."
677 :group 'org-agenda-skip
678 :type 'boolean)
680 (defcustom org-agenda-todo-list-sublevels t
681 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
682 When nil, the sublevels of a TODO entry are not checked, resulting in
683 potentially much shorter TODO lists."
684 :group 'org-agenda-skip
685 :group 'org-agenda-todo-list
686 :type 'boolean)
688 (defcustom org-agenda-todo-ignore-with-date nil
689 "Non-nil means don't show entries with a date in the global todo list.
690 You can use this if you prefer to mark mere appointments with a TODO keyword,
691 but don't want them to show up in the TODO list.
692 When this is set, it also covers deadlines and scheduled items, the settings
693 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
694 will be ignored.
695 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
696 :group 'org-agenda-skip
697 :group 'org-agenda-todo-list
698 :type 'boolean)
700 (defcustom org-agenda-todo-ignore-timestamp nil
701 "Non-nil means don't show entries with a timestamp.
702 This applies when creating the global todo list.
703 Valid values are:
705 past Don't show entries for today or in the past.
707 future Don't show entries with a timestamp in the future.
708 The idea behind this is that if it has a future
709 timestamp, you don't want to think about it until the
710 date.
712 all Don't show any entries with a timestamp in the global todo list.
713 The idea behind this is that by setting a timestamp, you
714 have already \"taken care\" of this item.
716 This variable can also have an integer as a value. If positive (N),
717 todos with a timestamp N or more days in the future will be ignored. If
718 negative (-N), todos with a timestamp N or more days in the past will be
719 ignored. If 0, todos with a timestamp either today or in the future will
720 be ignored. For example, a value of -1 will exclude todos with a
721 timestamp in the past (yesterday or earlier), while a value of 7 will
722 exclude todos with a timestamp a week or more in the future.
724 See also `org-agenda-todo-ignore-with-date'.
725 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
726 to make his option also apply to the tags-todo list."
727 :group 'org-agenda-skip
728 :group 'org-agenda-todo-list
729 :version "24.1"
730 :type '(choice
731 (const :tag "Ignore future timestamp todos" future)
732 (const :tag "Ignore past or present timestamp todos" past)
733 (const :tag "Ignore all timestamp todos" all)
734 (const :tag "Show timestamp todos" nil)
735 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
737 (defcustom org-agenda-todo-ignore-scheduled nil
738 "Non-nil means, ignore some scheduled TODO items when making TODO list.
739 This applies when creating the global todo list.
740 Valid values are:
742 past Don't show entries scheduled today or in the past.
744 future Don't show entries scheduled in the future.
745 The idea behind this is that by scheduling it, you don't want to
746 think about it until the scheduled date.
748 all Don't show any scheduled entries in the global todo list.
749 The idea behind this is that by scheduling it, you have already
750 \"taken care\" of this item.
752 t Same as `all', for backward compatibility.
754 This variable can also have an integer as a value. See
755 `org-agenda-todo-ignore-timestamp' for more details.
757 See also `org-agenda-todo-ignore-with-date'.
758 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
759 to make his option also apply to the tags-todo list."
760 :group 'org-agenda-skip
761 :group 'org-agenda-todo-list
762 :type '(choice
763 (const :tag "Ignore future-scheduled todos" future)
764 (const :tag "Ignore past- or present-scheduled todos" past)
765 (const :tag "Ignore all scheduled todos" all)
766 (const :tag "Ignore all scheduled todos (compatibility)" t)
767 (const :tag "Show scheduled todos" nil)
768 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
770 (defcustom org-agenda-todo-ignore-deadlines nil
771 "Non-nil means ignore some deadline TODO items when making TODO list.
772 There are different motivations for using different values, please think
773 carefully when configuring this variable.
775 This applies when creating the global todo list.
776 Valid values are:
778 near Don't show near deadline entries. A deadline is near when it is
779 closer than `org-deadline-warning-days' days. The idea behind this
780 is that such items will appear in the agenda anyway.
782 far Don't show TODO entries where a deadline has been defined, but
783 the deadline is not near. This is useful if you don't want to
784 use the todo list to figure out what to do now.
786 past Don't show entries with a deadline timestamp for today or in the past.
788 future Don't show entries with a deadline timestamp in the future, not even
789 when they become `near' ones. Use it with caution.
791 all Ignore all TODO entries that do have a deadline.
793 t Same as `near', for backward compatibility.
795 This variable can also have an integer as a value. See
796 `org-agenda-todo-ignore-timestamp' for more details.
798 See also `org-agenda-todo-ignore-with-date'.
799 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
800 to make his option also apply to the tags-todo list."
801 :group 'org-agenda-skip
802 :group 'org-agenda-todo-list
803 :type '(choice
804 (const :tag "Ignore near deadlines" near)
805 (const :tag "Ignore near deadlines (compatibility)" t)
806 (const :tag "Ignore far deadlines" far)
807 (const :tag "Ignore all TODOs with a deadlines" all)
808 (const :tag "Show all TODOs, even if they have a deadline" nil)
809 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
811 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
812 "Time unit to use when possibly ignoring an agenda item.
814 See the docstring of various `org-agenda-todo-ignore-*' options.
815 The default is to compare time stamps using days. An item is thus
816 considered to be in the future if it is at least one day after today.
817 Non-nil means to compare time stamps using seconds. An item is then
818 considered future if it has a time value later than current time."
819 :group 'org-agenda-skip
820 :group 'org-agenda-todo-list
821 :version "24.4"
822 :package-version '(Org . "8.0")
823 :type '(choice
824 (const :tag "Compare time with days" nil)
825 (const :tag "Compare time with seconds" t)))
827 (defcustom org-agenda-tags-todo-honor-ignore-options nil
828 "Non-nil means honor todo-list ignores options also in tags-todo search.
829 The variables
830 `org-agenda-todo-ignore-with-date',
831 `org-agenda-todo-ignore-timestamp',
832 `org-agenda-todo-ignore-scheduled',
833 `org-agenda-todo-ignore-deadlines'
834 make the global TODO list skip entries that have time stamps of certain
835 kinds. If this option is set, the same options will also apply for the
836 tags-todo search, which is the general tags/property matcher
837 restricted to unfinished TODO entries only."
838 :group 'org-agenda-skip
839 :group 'org-agenda-todo-list
840 :group 'org-agenda-match-view
841 :type 'boolean)
843 (defcustom org-agenda-skip-scheduled-if-done nil
844 "Non-nil means don't show scheduled items in agenda when they are done.
845 This is relevant for the daily/weekly agenda, not for the TODO list. And
846 it applies only to the actual date of the scheduling. Warnings about
847 an item with a past scheduling dates are always turned off when the item
848 is DONE."
849 :group 'org-agenda-skip
850 :group 'org-agenda-daily/weekly
851 :type 'boolean)
853 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
854 "Non-nil means skip scheduling line if same entry shows because of deadline.
856 In the agenda of today, an entry can show up multiple times
857 because it is both scheduled and has a nearby deadline, and maybe
858 a plain time stamp as well.
860 When this variable is nil, the entry will be shown several times.
862 When set to t, then only the deadline is shown and the fact that
863 the entry is scheduled today or was scheduled previously is not
864 shown.
866 When set to the symbol `not-today', skip scheduled previously,
867 but not scheduled today.
869 When set to the symbol `repeated-after-deadline', skip scheduled
870 items if they are repeated beyond the current deadline."
871 :group 'org-agenda-skip
872 :group 'org-agenda-daily/weekly
873 :type '(choice
874 (const :tag "Never" nil)
875 (const :tag "Always" t)
876 (const :tag "Not when scheduled today" not-today)
877 (const :tag "When repeated past deadline" repeated-after-deadline)))
879 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
880 "Non-nil means skip timestamp line if same entry shows because of deadline.
881 In the agenda of today, an entry can show up multiple times
882 because it has both a plain timestamp and has a nearby deadline.
883 When this variable is t, then only the deadline is shown and the
884 fact that the entry has a timestamp for or including today is not
885 shown. When this variable is nil, the entry will be shown
886 several times."
887 :group 'org-agenda-skip
888 :group 'org-agenda-daily/weekly
889 :version "24.1"
890 :type '(choice
891 (const :tag "Never" nil)
892 (const :tag "Always" t)))
894 (defcustom org-agenda-skip-deadline-if-done nil
895 "Non-nil means don't show deadlines when the corresponding item is done.
896 When nil, the deadline is still shown and should give you a happy feeling.
897 This is relevant for the daily/weekly agenda. And it applied only to the
898 actually date of the deadline. Warnings about approaching and past-due
899 deadlines are always turned off when the item is DONE."
900 :group 'org-agenda-skip
901 :group 'org-agenda-daily/weekly
902 :type 'boolean)
904 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
905 "Non-nil means skip deadline prewarning when entry is also scheduled.
906 This will apply on all days where a prewarning for the deadline would
907 be shown, but not at the day when the entry is actually due. On that day,
908 the deadline will be shown anyway.
909 This variable may be set to nil, t, the symbol `pre-scheduled',
910 or a number which will then give the number of days before the actual
911 deadline when the prewarnings should resume. The symbol `pre-scheduled'
912 eliminates the deadline prewarning only prior to the scheduled date.
913 This can be used in a workflow where the first showing of the deadline will
914 trigger you to schedule it, and then you don't want to be reminded of it
915 because you will take care of it on the day when scheduled."
916 :group 'org-agenda-skip
917 :group 'org-agenda-daily/weekly
918 :version "24.1"
919 :type '(choice
920 (const :tag "Always show prewarning" nil)
921 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
922 (const :tag "Remove prewarning if entry is scheduled" t)
923 (integer :tag "Restart prewarning N days before deadline")))
925 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
926 "Non-nil means skip scheduled delay when entry also has a deadline.
927 This variable may be set to nil, t, the symbol `post-deadline',
928 or a number which will then give the number of days after the actual
929 scheduled date when the delay should expire. The symbol `post-deadline'
930 eliminates the schedule delay when the date is posterior to the deadline."
931 :group 'org-agenda-skip
932 :group 'org-agenda-daily/weekly
933 :version "24.4"
934 :package-version '(Org . "8.0")
935 :type '(choice
936 (const :tag "Always honor delay" nil)
937 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
938 (const :tag "Ignore delay if entry has a deadline" t)
939 (integer :tag "Honor delay up until N days after the scheduled date")))
941 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
942 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
943 When non-nil, after the search for timestamps has matched once in an
944 entry, the rest of the entry will not be searched."
945 :group 'org-agenda-skip
946 :type 'boolean)
948 (defcustom org-agenda-skip-timestamp-if-done nil
949 "Non-nil means don't select item by timestamp or -range if it is DONE."
950 :group 'org-agenda-skip
951 :group 'org-agenda-daily/weekly
952 :type 'boolean)
954 (defcustom org-agenda-dim-blocked-tasks t
955 "Non-nil means dim blocked tasks in the agenda display.
956 This causes some overhead during agenda construction, but if you
957 have turned on `org-enforce-todo-dependencies',
958 `org-enforce-todo-checkbox-dependencies', or any other blocking
959 mechanism, this will create useful feedback in the agenda.
961 Instead of t, this variable can also have the value `invisible'.
962 Then blocked tasks will be invisible and only become visible when
963 they become unblocked. An exemption to this behavior is when a task is
964 blocked because of unchecked checkboxes below it. Since checkboxes do
965 not show up in the agenda views, making this task invisible you remove any
966 trace from agenda views that there is something to do. Therefore, a task
967 that is blocked because of checkboxes will never be made invisible, it
968 will only be dimmed."
969 :group 'org-agenda-daily/weekly
970 :group 'org-agenda-todo-list
971 :version "24.3"
972 :type '(choice
973 (const :tag "Do not dim" nil)
974 (const :tag "Dim to a gray face" t)
975 (const :tag "Make invisible" invisible)))
977 (defcustom org-timeline-show-empty-dates 3
978 "Non-nil means `org-timeline' also shows dates without an entry.
979 When nil, only the days which actually have entries are shown.
980 When t, all days between the first and the last date are shown.
981 When an integer, show also empty dates, but if there is a gap of more than
982 N days, just insert a special line indicating the size of the gap."
983 :group 'org-agenda-skip
984 :type '(choice
985 (const :tag "None" nil)
986 (const :tag "All" t)
987 (integer :tag "at most")))
989 (defgroup org-agenda-startup nil
990 "Options concerning initial settings in the Agenda in Org Mode."
991 :tag "Org Agenda Startup"
992 :group 'org-agenda)
994 (defcustom org-agenda-menu-show-matcher t
995 "Non-nil means show the match string in the agenda dispatcher menu.
996 When nil, the matcher string is not shown, but is put into the help-echo
997 property so than moving the mouse over the command shows it.
998 Setting it to nil is good if matcher strings are very long and/or if
999 you want to use two-columns display (see `org-agenda-menu-two-columns')."
1000 :group 'org-agenda
1001 :version "24.1"
1002 :type 'boolean)
1004 (defcustom org-agenda-menu-two-columns nil
1005 "Non-nil means, use two columns to show custom commands in the dispatcher.
1006 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1007 to nil."
1008 :group 'org-agenda
1009 :version "24.1"
1010 :type 'boolean)
1012 (defcustom org-agenda-finalize-hook nil
1013 "Hook run just before displaying an agenda buffer.
1014 The buffer is still writable when the hook is called.
1016 You can modify some of the buffer substrings but you should be
1017 extra careful not to modify the text properties of the agenda
1018 headlines as the agenda display heavily relies on them."
1019 :group 'org-agenda-startup
1020 :type 'hook)
1022 (defcustom org-agenda-mouse-1-follows-link nil
1023 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1024 A longer mouse click will still set point. Needs to be set
1025 before org.el is loaded."
1026 :group 'org-agenda-startup
1027 :type 'boolean)
1029 (defcustom org-agenda-start-with-follow-mode nil
1030 "The initial value of follow mode in a newly created agenda window."
1031 :group 'org-agenda-startup
1032 :type 'boolean)
1034 (defcustom org-agenda-follow-indirect nil
1035 "Non-nil means `org-agenda-follow-mode' displays only the
1036 current item's tree, in an indirect buffer."
1037 :group 'org-agenda
1038 :version "24.1"
1039 :type 'boolean)
1041 (defcustom org-agenda-show-outline-path t
1042 "Non-nil means show outline path in echo area after line motion."
1043 :group 'org-agenda-startup
1044 :type 'boolean)
1046 (defcustom org-agenda-start-with-entry-text-mode nil
1047 "The initial value of entry-text-mode in a newly created agenda window."
1048 :group 'org-agenda-startup
1049 :type 'boolean)
1051 (defcustom org-agenda-entry-text-maxlines 5
1052 "Number of text lines to be added when `E' is pressed in the agenda.
1054 Note that this variable only used during agenda display. To add entry text
1055 when exporting the agenda, configure the variable
1056 `org-agenda-add-entry-text-maxlines'."
1057 :group 'org-agenda
1058 :type 'integer)
1060 (defcustom org-agenda-entry-text-exclude-regexps nil
1061 "List of regular expressions to clean up entry text.
1062 The complete matches of all regular expressions in this list will be
1063 removed from entry text before it is shown in the agenda."
1064 :group 'org-agenda
1065 :type '(repeat (regexp)))
1067 (defcustom org-agenda-entry-text-leaders " > "
1068 "Text prepended to the entry text in agenda buffers."
1069 :version "24.4"
1070 :package-version '(Org . "8.0")
1071 :group 'org-agenda
1072 :type 'string)
1074 (defvar org-agenda-entry-text-cleanup-hook nil
1075 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1076 This cleanup is done in a temporary buffer, so the function may inspect and
1077 change the entire buffer.
1078 Some default stuff like drawers and scheduling/deadline dates will already
1079 have been removed when this is called, as will any matches for regular
1080 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1082 (defvar org-agenda-include-inactive-timestamps nil
1083 "Non-nil means include inactive time stamps in agenda and timeline.
1084 Dynamically scoped.")
1086 (defgroup org-agenda-windows nil
1087 "Options concerning the windows used by the Agenda in Org Mode."
1088 :tag "Org Agenda Windows"
1089 :group 'org-agenda)
1091 (defcustom org-agenda-window-setup 'reorganize-frame
1092 "How the agenda buffer should be displayed.
1093 Possible values for this option are:
1095 current-window Show agenda in the current window, keeping all other windows.
1096 other-window Use `switch-to-buffer-other-window' to display agenda.
1097 only-window Show agenda, deleting all other windows.
1098 reorganize-frame Show only two windows on the current frame, the current
1099 window and the agenda.
1100 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1101 Also, when exiting the agenda, kill that frame.
1102 See also the variable `org-agenda-restore-windows-after-quit'."
1103 :group 'org-agenda-windows
1104 :type '(choice
1105 (const current-window)
1106 (const other-frame)
1107 (const other-window)
1108 (const only-window)
1109 (const reorganize-frame)))
1111 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1112 "The min and max height of the agenda window as a fraction of frame height.
1113 The value of the variable is a cons cell with two numbers between 0 and 1.
1114 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1115 :group 'org-agenda-windows
1116 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1118 (defcustom org-agenda-restore-windows-after-quit nil
1119 "Non-nil means restore window configuration upon exiting agenda.
1120 Before the window configuration is changed for displaying the agenda,
1121 the current status is recorded. When the agenda is exited with
1122 `q' or `x' and this option is set, the old state is restored. If
1123 `org-agenda-window-setup' is `other-frame', the value of this
1124 option will be ignored."
1125 :group 'org-agenda-windows
1126 :type 'boolean)
1128 (defcustom org-agenda-span 'week
1129 "Number of days to include in overview display.
1130 Can be day, week, month, year, or any number of days.
1131 Custom commands can set this variable in the options section."
1132 :group 'org-agenda-daily/weekly
1133 :type '(choice (const :tag "Day" day)
1134 (const :tag "Week" week)
1135 (const :tag "Fortnight" fortnight)
1136 (const :tag "Month" month)
1137 (const :tag "Year" year)
1138 (integer :tag "Custom")))
1140 (defcustom org-agenda-start-on-weekday 1
1141 "Non-nil means start the overview always on the specified weekday.
1142 0 denotes Sunday, 1 denotes Monday, etc.
1143 When nil, always start on the current day.
1144 Custom commands can set this variable in the options section."
1145 :group 'org-agenda-daily/weekly
1146 :type '(choice (const :tag "Today" nil)
1147 (integer :tag "Weekday No.")))
1149 (defcustom org-agenda-show-all-dates t
1150 "Non-nil means `org-agenda' shows every day in the selected range.
1151 When nil, only the days which actually have entries are shown."
1152 :group 'org-agenda-daily/weekly
1153 :type 'boolean)
1155 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1156 "Format string for displaying dates in the agenda.
1157 Used by the daily/weekly agenda and by the timeline. This should be
1158 a format string understood by `format-time-string', or a function returning
1159 the formatted date as a string. The function must take a single argument,
1160 a calendar-style date list like (month day year)."
1161 :group 'org-agenda-daily/weekly
1162 :type '(choice
1163 (string :tag "Format string")
1164 (function :tag "Function")))
1166 (defun org-agenda-format-date-aligned (date)
1167 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1168 This function makes sure that dates are aligned for easy reading."
1169 (require 'cal-iso)
1170 (let* ((dayname (calendar-day-name date))
1171 (day (cadr date))
1172 (day-of-week (calendar-day-of-week date))
1173 (month (car date))
1174 (monthname (calendar-month-name month))
1175 (year (nth 2 date))
1176 (iso-week (org-days-to-iso-week
1177 (calendar-absolute-from-gregorian date)))
1178 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1179 (1- year))
1180 ((and (= month 12) (<= iso-week 1))
1181 (1+ year))
1182 (t year)))
1183 (weekstring (if (= day-of-week 1)
1184 (format " W%02d" iso-week)
1185 "")))
1186 (format "%-10s %2d %s %4d%s"
1187 dayname day monthname year weekstring)))
1189 (defcustom org-agenda-time-leading-zero nil
1190 "Non-nil means use leading zero for military times in agenda.
1191 For example, 9:30am would become 09:30 rather than 9:30."
1192 :group 'org-agenda-daily/weekly
1193 :version "24.1"
1194 :type 'boolean)
1196 (defcustom org-agenda-timegrid-use-ampm nil
1197 "When set, show AM/PM style timestamps on the timegrid."
1198 :group 'org-agenda
1199 :version "24.1"
1200 :type 'boolean)
1202 (defun org-agenda-time-of-day-to-ampm (time)
1203 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1204 (let* ((hour-number (string-to-number (substring time 0 -3)))
1205 (minute (substring time -2))
1206 (ampm "am"))
1207 (cond
1208 ((equal hour-number 12)
1209 (setq ampm "pm"))
1210 ((> hour-number 12)
1211 (setq ampm "pm")
1212 (setq hour-number (- hour-number 12))))
1213 (concat
1214 (if org-agenda-time-leading-zero
1215 (format "%02d" hour-number)
1216 (format "%02s" (number-to-string hour-number)))
1217 ":" minute ampm)))
1219 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1220 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1221 (if org-agenda-timegrid-use-ampm
1222 (org-agenda-time-of-day-to-ampm time)
1223 time))
1225 (defcustom org-agenda-weekend-days '(6 0)
1226 "Which days are weekend?
1227 These days get the special face `org-agenda-date-weekend' in the agenda
1228 and timeline buffers."
1229 :group 'org-agenda-daily/weekly
1230 :type '(set :greedy t
1231 (const :tag "Monday" 1)
1232 (const :tag "Tuesday" 2)
1233 (const :tag "Wednesday" 3)
1234 (const :tag "Thursday" 4)
1235 (const :tag "Friday" 5)
1236 (const :tag "Saturday" 6)
1237 (const :tag "Sunday" 0)))
1239 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1240 "Non-nil means jump to today when moving a past date forward in time.
1241 When using S-right in the agenda to move a a date forward, and the date
1242 stamp currently points to the past, the first key press will move it
1243 to today. WHen nil, just move one day forward even if the date stays
1244 in the past."
1245 :group 'org-agenda-daily/weekly
1246 :version "24.1"
1247 :type 'boolean)
1249 (defcustom org-agenda-include-diary nil
1250 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1251 Custom commands can set this variable in the options section."
1252 :group 'org-agenda-daily/weekly
1253 :type 'boolean)
1255 (defcustom org-agenda-include-deadlines t
1256 "If non-nil, include entries within their deadline warning period.
1257 Custom commands can set this variable in the options section."
1258 :group 'org-agenda-daily/weekly
1259 :version "24.1"
1260 :type 'boolean)
1262 (defcustom org-agenda-repeating-timestamp-show-all t
1263 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1264 When set to a list of strings, only show occurrences of repeating
1265 stamps for these TODO keywords. When nil, only one occurrence is
1266 shown, either today or the nearest into the future."
1267 :group 'org-agenda-daily/weekly
1268 :type '(choice
1269 (const :tag "Show repeating stamps" t)
1270 (repeat :tag "Show repeating stamps for these TODO keywords"
1271 (string :tag "TODO Keyword"))
1272 (const :tag "Don't show repeating stamps" nil)))
1274 (defcustom org-scheduled-past-days 10000
1275 "Number of days to continue listing scheduled items not marked DONE.
1276 When an item is scheduled on a date, it shows up in the agenda on
1277 this day and will be listed until it is marked done or for the
1278 number of days given here."
1279 :group 'org-agenda-daily/weekly
1280 :type 'integer)
1282 (defcustom org-agenda-log-mode-items '(closed clock)
1283 "List of items that should be shown in agenda log mode.
1284 \\<org-agenda-mode-map>\
1285 This list may contain the following symbols:
1287 closed Show entries that have been closed on that day.
1288 clock Show entries that have received clocked time on that day.
1289 state Show all logged state changes.
1290 Note that instead of changing this variable, you can also press \
1291 `\\[universal-argument] \\[org-agenda-log-mode]' in
1292 the agenda to display all available LOG items temporarily."
1293 :group 'org-agenda-daily/weekly
1294 :type '(set :greedy t (const closed) (const clock) (const state)))
1296 (defcustom org-agenda-clock-consistency-checks
1297 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1298 :gap-ok-around ("4:00")
1299 :default-face ((:background "DarkRed") (:foreground "white"))
1300 :overlap-face nil :gap-face nil :no-end-time-face nil
1301 :long-face nil :short-face nil)
1302 "This is a property list, with the following keys:
1304 :max-duration Mark clocking chunks that are longer than this time.
1305 This is a time string like \"HH:MM\", or the number
1306 of minutes as an integer.
1308 :min-duration Mark clocking chunks that are shorter that this.
1309 This is a time string like \"HH:MM\", or the number
1310 of minutes as an integer.
1312 :max-gap Mark gaps between clocking chunks that are longer than
1313 this duration. A number of minutes, or a string
1314 like \"HH:MM\".
1316 :gap-ok-around List of times during the day which are usually not working
1317 times. When a gap is detected, but the gap contains any
1318 of these times, the gap is *not* reported. For example,
1319 if this is (\"4:00\" \"13:00\") then gaps that contain
1320 4:00 in the morning (i.e. the night) and 13:00
1321 (i.e. a typical lunch time) do not cause a warning.
1322 You should have at least one time during the night in this
1323 list, or otherwise the first task each morning will trigger
1324 a warning because it follows a long gap.
1326 Furthermore, the following properties can be used to define faces for
1327 issue display.
1329 :default-face the default face, if the specific face is undefined
1330 :overlap-face face for overlapping clocks
1331 :gap-face face for gaps between clocks
1332 :no-end-time-face face for incomplete clocks
1333 :long-face face for clock intervals that are too long
1334 :short-face face for clock intervals that are too short"
1335 :group 'org-agenda-daily/weekly
1336 :group 'org-clock
1337 :version "24.1"
1338 :type 'plist)
1340 (defcustom org-agenda-log-mode-add-notes t
1341 "Non-nil means add first line of notes to log entries in agenda views.
1342 If a log item like a state change or a clock entry is associated with
1343 notes, the first line of these notes will be added to the entry in the
1344 agenda display."
1345 :group 'org-agenda-daily/weekly
1346 :type 'boolean)
1348 (defcustom org-agenda-start-with-log-mode nil
1349 "The initial value of log-mode in a newly created agenda window.
1350 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1351 explanations on the possible values."
1352 :group 'org-agenda-startup
1353 :group 'org-agenda-daily/weekly
1354 :type '(choice (const :tag "Don't show log items" nil)
1355 (const :tag "Show only log items" only)
1356 (const :tag "Show all possible log items" clockcheck)
1357 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1358 (choice (const :tag "Show closed log items" closed)
1359 (const :tag "Show clocked log items" clock)
1360 (const :tag "Show all logged state changes" state)))))
1362 (defcustom org-agenda-start-with-clockreport-mode nil
1363 "The initial value of clockreport-mode in a newly created agenda window."
1364 :group 'org-agenda-startup
1365 :group 'org-agenda-daily/weekly
1366 :type 'boolean)
1368 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1369 "Property list with parameters for the clocktable in clockreport mode.
1370 This is the display mode that shows a clock table in the daily/weekly
1371 agenda, the properties for this dynamic block can be set here.
1372 The usual clocktable parameters are allowed here, but you cannot set
1373 the properties :name, :tstart, :tend, :block, and :scope - these will
1374 be overwritten to make sure the content accurately reflects the
1375 current display in the agenda."
1376 :group 'org-agenda-daily/weekly
1377 :type 'plist)
1379 (defcustom org-agenda-search-view-always-boolean nil
1380 "Non-nil means the search string is interpreted as individual parts.
1382 The search string for search view can either be interpreted as a phrase,
1383 or as a list of snippets that define a boolean search for a number of
1384 strings.
1386 When this is non-nil, the string will be split on whitespace, and each
1387 snippet will be searched individually, and all must match in order to
1388 select an entry. A snippet is then a single string of non-white
1389 characters, or a string in double quotes, or a regexp in {} braces.
1390 If a snippet is preceded by \"-\", the snippet must *not* match.
1391 \"+\" is syntactic sugar for positive selection. Each snippet may
1392 be found as a full word or a partial word, but see the variable
1393 `org-agenda-search-view-force-full-words'.
1395 When this is nil, search will look for the entire search phrase as one,
1396 with each space character matching any amount of whitespace, including
1397 line breaks.
1399 Even when this is nil, you can still switch to Boolean search dynamically
1400 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1401 is a regexp marked with braces like \"{abc}\", this will also switch to
1402 boolean search."
1403 :group 'org-agenda-search-view
1404 :version "24.1"
1405 :type 'boolean)
1407 (defvaralias 'org-agenda-search-view-search-words-only
1408 'org-agenda-search-view-always-boolean)
1410 (defcustom org-agenda-search-view-force-full-words nil
1411 "Non-nil means, search words must be matches as complete words.
1412 When nil, they may also match part of a word."
1413 :group 'org-agenda-search-view
1414 :version "24.1"
1415 :type 'boolean)
1417 (defcustom org-agenda-search-view-max-outline-level 0
1418 "Maximum outline level to display in search view.
1419 E.g. when this is set to 1, the search view will only
1420 show headlines of level 1. When set to 0, the default
1421 value, don't limit agenda view by outline level."
1422 :group 'org-agenda-search-view
1423 :version "24.4"
1424 :package-version '(Org . "8.3")
1425 :type 'integer)
1427 (defgroup org-agenda-time-grid nil
1428 "Options concerning the time grid in the Org Agenda."
1429 :tag "Org Agenda Time Grid"
1430 :group 'org-agenda)
1432 (defcustom org-agenda-search-headline-for-time t
1433 "Non-nil means search headline for a time-of-day.
1434 If the headline contains a time-of-day in one format or another, it will
1435 be used to sort the entry into the time sequence of items for a day.
1436 Some people have time stamps in the headline that refer to the creation
1437 time or so, and then this produces an unwanted side effect. If this is
1438 the case for your, use this variable to turn off searching the headline
1439 for a time."
1440 :group 'org-agenda-time-grid
1441 :type 'boolean)
1443 (defcustom org-agenda-use-time-grid t
1444 "Non-nil means show a time grid in the agenda schedule.
1445 A time grid is a set of lines for specific times (like every two hours between
1446 8:00 and 20:00). The items scheduled for a day at specific times are
1447 sorted in between these lines.
1448 For details about when the grid will be shown, and what it will look like, see
1449 the variable `org-agenda-time-grid'."
1450 :group 'org-agenda-time-grid
1451 :type 'boolean)
1453 (defcustom org-agenda-time-grid
1454 '((daily today require-timed)
1455 "----------------"
1456 (800 1000 1200 1400 1600 1800 2000))
1458 "The settings for time grid for agenda display.
1459 This is a list of three items. The first item is again a list. It contains
1460 symbols specifying conditions when the grid should be displayed:
1462 daily if the agenda shows a single day
1463 weekly if the agenda shows an entire week
1464 today show grid on current date, independent of daily/weekly display
1465 require-timed show grid only if at least one item has a time specification
1466 remove-match skip grid times already present in an entry
1468 The second item is a string which will be placed behind the grid time.
1470 The third item is a list of integers, indicating the times that should have
1471 a grid line."
1472 :group 'org-agenda-time-grid
1473 :type
1474 '(list
1475 (set :greedy t :tag "Grid Display Options"
1476 (const :tag "Show grid in single day agenda display" daily)
1477 (const :tag "Show grid in weekly agenda display" weekly)
1478 (const :tag "Always show grid for today" today)
1479 (const :tag "Show grid only if any timed entries are present"
1480 require-timed)
1481 (const :tag "Skip grid times already present in an entry"
1482 remove-match))
1483 (string :tag "Grid String")
1484 (repeat :tag "Grid Times" (integer :tag "Time"))))
1486 (defcustom org-agenda-show-current-time-in-grid t
1487 "Non-nil means show the current time in the time grid."
1488 :group 'org-agenda-time-grid
1489 :version "24.1"
1490 :type 'boolean)
1492 (defcustom org-agenda-current-time-string
1493 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1494 "The string for the current time marker in the agenda."
1495 :group 'org-agenda-time-grid
1496 :version "24.1"
1497 :type 'string)
1499 (defgroup org-agenda-sorting nil
1500 "Options concerning sorting in the Org Agenda."
1501 :tag "Org Agenda Sorting"
1502 :group 'org-agenda)
1504 (defcustom org-agenda-sorting-strategy
1505 '((agenda habit-down time-up priority-down category-keep)
1506 (todo priority-down category-keep)
1507 (tags priority-down category-keep)
1508 (search category-keep))
1509 "Sorting structure for the agenda items of a single day.
1510 This is a list of symbols which will be used in sequence to determine
1511 if an entry should be listed before another entry. The following
1512 symbols are recognized:
1514 time-up Put entries with time-of-day indications first, early first
1515 time-down Put entries with time-of-day indications first, late first
1516 timestamp-up Sort by any timestamp, early first
1517 timestamp-down Sort by any timestamp, late first
1518 scheduled-up Sort by scheduled timestamp, early first
1519 scheduled-down Sort by scheduled timestamp, late first
1520 deadline-up Sort by deadline timestamp, early first
1521 deadline-down Sort by deadline timestamp, late first
1522 ts-up Sort by active timestamp, early first
1523 ts-down Sort by active timestamp, late first
1524 tsia-up Sort by inactive timestamp, early first
1525 tsia-down Sort by inactive timestamp, late first
1526 category-keep Keep the default order of categories, corresponding to the
1527 sequence in `org-agenda-files'.
1528 category-up Sort alphabetically by category, A-Z.
1529 category-down Sort alphabetically by category, Z-A.
1530 tag-up Sort alphabetically by last tag, A-Z.
1531 tag-down Sort alphabetically by last tag, Z-A.
1532 priority-up Sort numerically by priority, high priority last.
1533 priority-down Sort numerically by priority, high priority first.
1534 todo-state-up Sort by todo state, tasks that are done last.
1535 todo-state-down Sort by todo state, tasks that are done first.
1536 effort-up Sort numerically by estimated effort, high effort last.
1537 effort-down Sort numerically by estimated effort, high effort first.
1538 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1539 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1540 habit-up Put entries that are habits first
1541 habit-down Put entries that are habits last
1542 alpha-up Sort headlines alphabetically
1543 alpha-down Sort headlines alphabetically, reversed
1545 The different possibilities will be tried in sequence, and testing stops
1546 if one comparison returns a \"not-equal\". For example, the default
1547 '(time-up category-keep priority-down)
1548 means: Pull out all entries having a specified time of day and sort them,
1549 in order to make a time schedule for the current day the first thing in the
1550 agenda listing for the day. Of the entries without a time indication, keep
1551 the grouped in categories, don't sort the categories, but keep them in
1552 the sequence given in `org-agenda-files'. Within each category sort by
1553 priority.
1555 Leaving out `category-keep' would mean that items will be sorted across
1556 categories by priority.
1558 Instead of a single list, this can also be a set of list for specific
1559 contents, with a context symbol in the car of the list, any of
1560 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1562 Custom commands can bind this variable in the options section."
1563 :group 'org-agenda-sorting
1564 :type `(choice
1565 (repeat :tag "General" ,org-sorting-choice)
1566 (list :tag "Individually"
1567 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1568 (repeat ,org-sorting-choice))
1569 (cons (const :tag "Strategy for TODO lists" todo)
1570 (repeat ,org-sorting-choice))
1571 (cons (const :tag "Strategy for Tags matches" tags)
1572 (repeat ,org-sorting-choice))
1573 (cons (const :tag "Strategy for search matches" search)
1574 (repeat ,org-sorting-choice)))))
1576 (defcustom org-agenda-cmp-user-defined nil
1577 "A function to define the comparison `user-defined'.
1578 This function must receive two arguments, agenda entry a and b.
1579 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1580 the user comparison, return nil.
1581 When this is defined, you can make `user-defined-up' and `user-defined-down'
1582 part of an agenda sorting strategy."
1583 :group 'org-agenda-sorting
1584 :type 'symbol)
1586 (defcustom org-sort-agenda-notime-is-late t
1587 "Non-nil means items without time are considered late.
1588 This is only relevant for sorting. When t, items which have no explicit
1589 time like 15:30 will be considered as 99:01, i.e. later than any items which
1590 do have a time. When nil, the default time is before 0:00. You can use this
1591 option to decide if the schedule for today should come before or after timeless
1592 agenda entries."
1593 :group 'org-agenda-sorting
1594 :type 'boolean)
1596 (defcustom org-sort-agenda-noeffort-is-high t
1597 "Non-nil means items without effort estimate are sorted as high effort.
1598 This also applies when filtering an agenda view with respect to the
1599 < or > effort operator. Then, tasks with no effort defined will be treated
1600 as tasks with high effort.
1601 When nil, such items are sorted as 0 minutes effort."
1602 :group 'org-agenda-sorting
1603 :type 'boolean)
1605 (defgroup org-agenda-line-format nil
1606 "Options concerning the entry prefix in the Org agenda display."
1607 :tag "Org Agenda Line Format"
1608 :group 'org-agenda)
1610 (defcustom org-agenda-prefix-format
1611 '((agenda . " %i %-12:c%?-12t% s")
1612 (timeline . " % s")
1613 (todo . " %i %-12:c")
1614 (tags . " %i %-12:c")
1615 (search . " %i %-12:c"))
1616 "Format specifications for the prefix of items in the agenda views.
1617 An alist with five entries, each for the different agenda types. The
1618 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1619 The values are format strings.
1621 This format works similar to a printf format, with the following meaning:
1623 %c the category of the item, \"Diary\" for entries from the diary,
1624 or as given by the CATEGORY keyword or derived from the file name
1625 %e the effort required by the item
1626 %l the level of the item (insert X space(s) if item is of level X)
1627 %i the icon category of the item, see `org-agenda-category-icon-alist'
1628 %T the last tag of the item (ignore inherited tags, which come first)
1629 %t the HH:MM time-of-day specification if one applies to the entry
1630 %s Scheduling/Deadline information, a short string
1631 %b show breadcrumbs, i.e., the names of the higher levels
1632 %(expression) Eval EXPRESSION and replace the control string
1633 by the result
1635 All specifiers work basically like the standard `%s' of printf, but may
1636 contain two additional characters: a question mark just after the `%'
1637 and a whitespace/punctuation character just before the final letter.
1639 If the first character after `%' is a question mark, the entire field
1640 will only be included if the corresponding value applies to the current
1641 entry. This is useful for fields which should have fixed width when
1642 present, but zero width when absent. For example, \"%?-12t\" will
1643 result in a 12 character time field if a time of the day is specified,
1644 but will completely disappear in entries which do not contain a time.
1646 If there is punctuation or whitespace character just before the
1647 final format letter, this character will be appended to the field
1648 value if the value is not empty. For example, the format
1649 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1650 the category is empty, no additional colon is inserted.
1652 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1653 which means:
1655 - Indent the line with two space characters
1656 - Give the category a 12 chars wide field, padded with whitespace on
1657 the right (because of `-'). Append a colon if there is a category
1658 (because of `:').
1659 - If there is a time-of-day, put it into a 12 chars wide field. If no
1660 time, don't put in an empty field, just skip it (because of '?').
1661 - Finally, put the scheduling information.
1663 See also the variables `org-agenda-remove-times-when-in-prefix' and
1664 `org-agenda-remove-tags'.
1666 Custom commands can set this variable in the options section."
1667 :type '(choice
1668 (string :tag "General format")
1669 (list :greedy t :tag "View dependent"
1670 (cons (const agenda) (string :tag "Format"))
1671 (cons (const timeline) (string :tag "Format"))
1672 (cons (const todo) (string :tag "Format"))
1673 (cons (const tags) (string :tag "Format"))
1674 (cons (const search) (string :tag "Format"))))
1675 :group 'org-agenda-line-format)
1677 (defvar org-prefix-format-compiled nil
1678 "The compiled prefix format and associated variables.
1679 This is a list where first element is a list of variable bindings, and second
1680 element is the compiled format expression. See the variable
1681 `org-agenda-prefix-format'.")
1683 (defcustom org-agenda-todo-keyword-format "%-1s"
1684 "Format for the TODO keyword in agenda lines.
1685 Set this to something like \"%-12s\" if you want all TODO keywords
1686 to occupy a fixed space in the agenda display."
1687 :group 'org-agenda-line-format
1688 :type 'string)
1690 (defcustom org-agenda-diary-sexp-prefix nil
1691 "A regexp that matches part of a diary sexp entry
1692 which should be treated as scheduling/deadline information in
1693 `org-agenda'.
1695 For example, you can use this to extract the `diary-remind-message' from
1696 `diary-remind' entries."
1697 :group 'org-agenda-line-format
1698 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1700 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1701 "Text preceding timerange entries in the agenda view.
1702 This is a list with two strings. The first applies when the range
1703 is entirely on one day. The second applies if the range spans several days.
1704 The strings may have two \"%d\" format specifiers which will be filled
1705 with the sequence number of the days, and the total number of days in the
1706 range, respectively."
1707 :group 'org-agenda-line-format
1708 :type '(list
1709 (string :tag "Deadline today ")
1710 (choice :tag "Deadline relative"
1711 (string :tag "Format string")
1712 (function))))
1714 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1715 "Text preceding scheduled items in the agenda view.
1716 This is a list with two strings. The first applies when the item is
1717 scheduled on the current day. The second applies when it has been scheduled
1718 previously, it may contain a %d indicating that this is the nth time that
1719 this item is scheduled, due to automatic rescheduling of unfinished items
1720 for the following day. So this number is one larger than the number of days
1721 that passed since this item was scheduled first."
1722 :group 'org-agenda-line-format
1723 :version "24.4"
1724 :package-version '(Org . "8.0")
1725 :type '(list
1726 (string :tag "Scheduled today ")
1727 (string :tag "Scheduled previously")))
1729 (defcustom org-agenda-inactive-leader "["
1730 "Text preceding item pulled into the agenda by inactive time stamps.
1731 These entries are added to the agenda when pressing \"[\"."
1732 :group 'org-agenda-line-format
1733 :version "24.1"
1734 :type 'string)
1736 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1737 "Text preceding deadline items in the agenda view.
1738 This is a list with three strings. The first applies when the item has its
1739 deadline on the current day. The second applies when the deadline is in the
1740 future, the third one when it is in the past. The strings may contain %d
1741 to capture the number of days."
1742 :group 'org-agenda-line-format
1743 :version "24.4"
1744 :package-version '(Org . "8.0")
1745 :type '(list
1746 (string :tag "Deadline today ")
1747 (string :tag "Deadline in the future ")
1748 (string :tag "Deadline in the past ")))
1750 (defcustom org-agenda-remove-times-when-in-prefix t
1751 "Non-nil means remove duplicate time specifications in agenda items.
1752 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1753 time-of-day specification in a headline or diary entry is extracted and
1754 placed into the prefix. If this option is non-nil, the original specification
1755 \(a timestamp or -range, or just a plain time(range) specification like
1756 11:30-4pm) will be removed for agenda display. This makes the agenda less
1757 cluttered.
1758 The option can be t or nil. It may also be the symbol `beg', indicating
1759 that the time should only be removed when it is located at the beginning of
1760 the headline/diary entry."
1761 :group 'org-agenda-line-format
1762 :type '(choice
1763 (const :tag "Always" t)
1764 (const :tag "Never" nil)
1765 (const :tag "When at beginning of entry" beg)))
1767 (defcustom org-agenda-remove-timeranges-from-blocks nil
1768 "Non-nil means remove time ranges specifications in agenda
1769 items that span on several days."
1770 :group 'org-agenda-line-format
1771 :version "24.1"
1772 :type 'boolean)
1774 (defcustom org-agenda-default-appointment-duration nil
1775 "Default duration for appointments that only have a starting time.
1776 When nil, no duration is specified in such cases.
1777 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1778 :group 'org-agenda-line-format
1779 :type '(choice
1780 (integer :tag "Minutes")
1781 (const :tag "No default duration")))
1783 (defcustom org-agenda-show-inherited-tags t
1784 "Non-nil means show inherited tags in each agenda line.
1786 When this option is set to `always', it take precedences over
1787 `org-agenda-use-tag-inheritance' and inherited tags are shown
1788 in every agenda.
1790 When this option is set to t (the default), inherited tags are
1791 shown when they are available, i.e. when the value of
1792 `org-agenda-use-tag-inheritance' has been taken into account.
1794 This can be set to a list of agenda types in which the agenda
1795 must display the inherited tags. Available types are `todo',
1796 `agenda', `search' and `timeline'.
1798 When set to nil, never show inherited tags in agenda lines."
1799 :group 'org-agenda-line-format
1800 :group 'org-agenda
1801 :version "24.3"
1802 :type '(choice
1803 (const :tag "Show inherited tags when available" t)
1804 (const :tag "Always show inherited tags" always)
1805 (repeat :tag "Show inherited tags only in selected agenda types"
1806 (symbol :tag "Agenda type"))))
1808 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1809 "List of agenda view types where to use tag inheritance.
1811 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1812 controlled by `org-use-tag-inheritance'. In other agenda types,
1813 `org-use-tag-inheritance' is not used for the selection of the
1814 agenda entries. Still, you may want the agenda to be aware of
1815 the inherited tags anyway, e.g. for later tag filtering.
1817 Allowed value are `todo', `search', `timeline' and `agenda'.
1819 This variable has no effect if `org-agenda-show-inherited-tags'
1820 is set to `always'. In that case, the agenda is aware of those
1821 tags.
1823 The default value sets tags in every agenda type. Setting this
1824 option to nil will speed up non-tags agenda view a lot."
1825 :group 'org-agenda
1826 :version "24.3"
1827 :type '(choice
1828 (const :tag "Use tag inheritance in all agenda types" t)
1829 (repeat :tag "Use tag inheritance in selected agenda types"
1830 (symbol :tag "Agenda type"))))
1832 (defcustom org-agenda-hide-tags-regexp nil
1833 "Regular expression used to filter away specific tags in agenda views.
1834 This means that these tags will be present, but not be shown in the agenda
1835 line. Secondary filtering will still work on the hidden tags.
1836 Nil means don't hide any tags."
1837 :group 'org-agenda-line-format
1838 :type '(choice
1839 (const :tag "Hide none" nil)
1840 (string :tag "Regexp ")))
1842 (defcustom org-agenda-remove-tags nil
1843 "Non-nil means remove the tags from the headline copy in the agenda.
1844 When this is the symbol `prefix', only remove tags when
1845 `org-agenda-prefix-format' contains a `%T' specifier."
1846 :group 'org-agenda-line-format
1847 :type '(choice
1848 (const :tag "Always" t)
1849 (const :tag "Never" nil)
1850 (const :tag "When prefix format contains %T" prefix)))
1852 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1853 'org-agenda-remove-tags)
1855 (defcustom org-agenda-tags-column -80
1856 "Shift tags in agenda items to this column.
1857 If this number is positive, it specifies the column. If it is negative,
1858 it means that the tags should be flushright to that column. For example,
1859 -80 works well for a normal 80 character screen."
1860 :group 'org-agenda-line-format
1861 :type 'integer)
1863 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1865 (defcustom org-agenda-fontify-priorities 'cookies
1866 "Non-nil means highlight low and high priorities in agenda.
1867 When t, the highest priority entries are bold, lowest priority italic.
1868 However, settings in `org-priority-faces' will overrule these faces.
1869 When this variable is the symbol `cookies', only fontify the
1870 cookies, not the entire task.
1871 This may also be an association list of priority faces, whose
1872 keys are the character values of `org-highest-priority',
1873 `org-default-priority', and `org-lowest-priority' (the default values
1874 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1875 color as a string, or a list like `(:background \"Red\")'.
1876 If it is a color, the variable `org-faces-easy-properties'
1877 determines if it is a foreground or a background color."
1878 :group 'org-agenda-line-format
1879 :type '(choice
1880 (const :tag "Never" nil)
1881 (const :tag "Defaults" t)
1882 (const :tag "Cookies only" cookies)
1883 (repeat :tag "Specify"
1884 (list (character :tag "Priority" :value ?A)
1885 (choice :tag "Face "
1886 (string :tag "Color")
1887 (sexp :tag "Face"))))))
1889 (defcustom org-agenda-day-face-function nil
1890 "Function called to determine what face should be used to display a day.
1891 The only argument passed to that function is the day. It should
1892 returns a face, or nil if does not want to specify a face and let
1893 the normal rules apply."
1894 :group 'org-agenda-line-format
1895 :version "24.1"
1896 :type '(choice (const nil) (function)))
1898 (defcustom org-agenda-category-icon-alist nil
1899 "Alist of category icon to be displayed in agenda views.
1901 Each entry should have the following format:
1903 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1905 Where CATEGORY-REGEXP is a regexp matching the categories where
1906 the icon should be displayed.
1907 FILE-OR-DATA either a file path or a string containing image data.
1909 The other fields can be omitted safely if not needed:
1910 TYPE indicates the image type.
1911 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1912 image data.
1913 PROPS are additional image attributes to assign to the image,
1914 like, e.g. `:ascent center'.
1916 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1918 If you want to set the display properties yourself, just put a
1919 list as second element:
1921 (CATEGORY-REGEXP (MY PROPERTY LIST))
1923 For example, to display a 16px horizontal space for Emacs
1924 category, you can use:
1926 (\"Emacs\" \\='(space . (:width (16))))"
1927 :group 'org-agenda-line-format
1928 :version "24.1"
1929 :type '(alist :key-type (string :tag "Regexp matching category")
1930 :value-type (choice (list :tag "Icon"
1931 (string :tag "File or data")
1932 (symbol :tag "Type")
1933 (boolean :tag "Data?")
1934 (repeat :tag "Extra image properties" :inline t symbol))
1935 (list :tag "Display properties" sexp))))
1937 (defgroup org-agenda-column-view nil
1938 "Options concerning column view in the agenda."
1939 :tag "Org Agenda Column View"
1940 :group 'org-agenda)
1942 (defcustom org-agenda-view-columns-initially nil
1943 "When non-nil, switch to columns view right after creating the agenda."
1944 :group 'org-agenda-column-view
1945 :type 'boolean
1946 :version "25.2"
1947 :package-version '(Org . "9.0")
1948 :safe #'booleanp)
1950 (defcustom org-agenda-columns-show-summaries t
1951 "Non-nil means show summaries for columns displayed in the agenda view."
1952 :group 'org-agenda-column-view
1953 :type 'boolean)
1955 (defcustom org-agenda-columns-compute-summary-properties t
1956 "Non-nil means recompute all summary properties before column view.
1957 When column view in the agenda is listing properties that have a summary
1958 operator, it can go to all relevant buffers and recompute the summaries
1959 there. This can mean overhead for the agenda column view, but is necessary
1960 to have thing up to date.
1961 As a special case, a CLOCKSUM property also makes sure that the clock
1962 computations are current."
1963 :group 'org-agenda-column-view
1964 :type 'boolean)
1966 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1967 "Non-nil means the duration of an appointment will add to day effort.
1968 The property to which appointment durations will be added is the one given
1969 in the option `org-effort-property'. If an appointment does not have
1970 an end time, `org-agenda-default-appointment-duration' will be used. If that
1971 is not set, an appointment without end time will not contribute to the time
1972 estimate."
1973 :group 'org-agenda-column-view
1974 :type 'boolean)
1976 (defcustom org-agenda-auto-exclude-function nil
1977 "A function called with a tag to decide if it is filtered on \
1978 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1979 The sole argument to the function, which is called once for each
1980 possible tag, is a string giving the name of the tag. The
1981 function should return either nil if the tag should be included
1982 as normal, or \"-<TAG>\" to exclude the tag.
1983 Note that for the purpose of tag filtering, only the lower-case version of
1984 all tags will be considered, so that this function will only ever see
1985 the lower-case version of all tags."
1986 :group 'org-agenda
1987 :type '(choice (const nil) (function)))
1989 (defcustom org-agenda-bulk-custom-functions nil
1990 "Alist of characters and custom functions for bulk actions.
1991 For example, this value makes those two functions available:
1993 \\='((?R set-category)
1994 (?C bulk-cut))
1996 With selected entries in an agenda buffer, `B R' will call
1997 the custom function `set-category' on the selected entries.
1998 Note that functions in this alist don't need to be quoted."
1999 :type '(alist :key-type character :value-type (group function))
2000 :version "24.1"
2001 :group 'org-agenda)
2003 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2004 "Execute BODY with point at location given by `org-hd-marker' property.
2005 If STRING is non-nil, the text property will be fetched from position 0
2006 in that string. If STRING is nil, it will be fetched from the beginning
2007 of the current line."
2008 (org-with-gensyms (marker)
2009 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2010 'org-hd-marker ,string)))
2011 (with-current-buffer (marker-buffer ,marker)
2012 (save-excursion
2013 (goto-char ,marker)
2014 ,@body)))))
2015 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2017 (defun org-add-agenda-custom-command (entry)
2018 "Replace or add a command in `org-agenda-custom-commands'.
2019 This is mostly for hacking and trying a new command - once the command
2020 works you probably want to add it to `org-agenda-custom-commands' for good."
2021 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2022 (if ass
2023 (setcdr ass (cdr entry))
2024 (push entry org-agenda-custom-commands))))
2026 ;;; Define the org-agenda-mode
2028 (defvar org-agenda-mode-map (make-sparse-keymap)
2029 "Keymap for `org-agenda-mode'.")
2030 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2032 (defvar org-agenda-menu) ; defined later in this file.
2033 (defvar org-agenda-restrict nil) ; defined later in this file.
2034 (defvar org-agenda-follow-mode nil)
2035 (defvar org-agenda-entry-text-mode nil)
2036 (defvar org-agenda-clockreport-mode nil)
2037 (defvar org-agenda-show-log nil)
2038 (defvar org-agenda-redo-command nil)
2039 (defvar org-agenda-query-string nil)
2040 (defvar org-agenda-mode-hook nil
2041 "Hook run after `org-agenda-mode' is turned on.
2042 The buffer is still writable when this hook is called.")
2043 (defvar org-agenda-type nil)
2044 (defvar org-agenda-force-single-file nil)
2045 (defvar org-agenda-bulk-marked-entries nil
2046 "List of markers that refer to marked entries in the agenda.")
2047 (defvar org-agenda-current-date nil
2048 "Active date when building the agenda.")
2050 ;;; Multiple agenda buffers support
2052 (defcustom org-agenda-sticky nil
2053 "Non-nil means agenda q key will bury agenda buffers.
2054 Agenda commands will then show existing buffer instead of generating new ones.
2055 When nil, `q' will kill the single agenda buffer."
2056 :group 'org-agenda
2057 :version "24.3"
2058 :type 'boolean)
2061 ;;;###autoload
2062 (defun org-toggle-sticky-agenda (&optional arg)
2063 "Toggle `org-agenda-sticky'."
2064 (interactive "P")
2065 (let ((new-value (if arg
2066 (> (prefix-numeric-value arg) 0)
2067 (not org-agenda-sticky))))
2068 (if (equal new-value org-agenda-sticky)
2069 (and (called-interactively-p 'interactive)
2070 (message "Sticky agenda was already %s"
2071 (if org-agenda-sticky "enabled" "disabled")))
2072 (setq org-agenda-sticky new-value)
2073 (org-agenda-kill-all-agenda-buffers)
2074 (and (called-interactively-p 'interactive)
2075 (message "Sticky agenda %s"
2076 (if org-agenda-sticky "enabled" "disabled"))))))
2078 (defvar org-agenda-buffer nil
2079 "Agenda buffer currently being generated.")
2081 (defvar org-agenda-last-prefix-arg nil)
2082 (defvar org-agenda-this-buffer-name nil)
2083 (defvar org-agenda-doing-sticky-redo nil)
2084 (defvar org-agenda-this-buffer-is-sticky nil)
2085 (defvar org-agenda-last-indirect-buffer nil
2086 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2088 (defconst org-agenda-local-vars
2089 '(org-agenda-this-buffer-name
2090 org-agenda-undo-list
2091 org-agenda-pending-undo-list
2092 org-agenda-follow-mode
2093 org-agenda-entry-text-mode
2094 org-agenda-clockreport-mode
2095 org-agenda-show-log
2096 org-agenda-redo-command
2097 org-agenda-query-string
2098 org-agenda-type
2099 org-agenda-bulk-marked-entries
2100 org-agenda-undo-has-started-in
2101 org-agenda-info
2102 org-agenda-pre-window-conf
2103 org-agenda-columns-active
2104 org-agenda-tag-filter
2105 org-agenda-category-filter
2106 org-agenda-top-headline-filter
2107 org-agenda-regexp-filter
2108 org-agenda-effort-filter
2109 org-agenda-markers
2110 org-agenda-last-search-view-search-was-boolean
2111 org-agenda-last-indirect-buffer
2112 org-agenda-filtered-by-category
2113 org-agenda-filter-form
2114 org-agenda-cycle-counter
2115 org-agenda-last-prefix-arg)
2116 "Variables that must be local in agenda buffers to allow multiple buffers.")
2118 (defun org-agenda-mode ()
2119 "Mode for time-sorted view on action items in Org files.
2121 The following commands are available:
2123 \\{org-agenda-mode-map}"
2124 (interactive)
2125 (cond (org-agenda-doing-sticky-redo
2126 ;; Refreshing sticky agenda-buffer
2128 ;; Preserve the value of `org-agenda-local-vars' variables,
2129 ;; while letting `kill-all-local-variables' kill the rest
2130 (let ((save (buffer-local-variables)))
2131 (kill-all-local-variables)
2132 (mapc 'make-local-variable org-agenda-local-vars)
2133 (dolist (elem save)
2134 (let ((var (car elem))
2135 (val (cdr elem)))
2136 (when (and val
2137 (member var org-agenda-local-vars))
2138 (set var val)))))
2139 (setq-local org-agenda-this-buffer-is-sticky t))
2140 (org-agenda-sticky
2141 ;; Creating a sticky Agenda buffer for the first time
2142 (kill-all-local-variables)
2143 (mapc 'make-local-variable org-agenda-local-vars)
2144 (setq-local org-agenda-this-buffer-is-sticky t))
2146 ;; Creating a non-sticky agenda buffer
2147 (kill-all-local-variables)
2148 (setq-local org-agenda-this-buffer-is-sticky nil)))
2149 (setq org-agenda-undo-list nil
2150 org-agenda-pending-undo-list nil
2151 org-agenda-bulk-marked-entries nil)
2152 (setq major-mode 'org-agenda-mode)
2153 ;; Keep global-font-lock-mode from turning on font-lock-mode
2154 (setq-local font-lock-global-modes (list 'not major-mode))
2155 (setq mode-name "Org-Agenda")
2156 (setq indent-tabs-mode nil)
2157 (use-local-map org-agenda-mode-map)
2158 (easy-menu-add org-agenda-menu)
2159 (if org-startup-truncated (setq truncate-lines t))
2160 (setq-local line-move-visual nil)
2161 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2162 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2163 ;; Make sure properties are removed when copying text
2164 (add-hook 'filter-buffer-substring-functions
2165 (lambda (fun start end delete)
2166 (substring-no-properties (funcall fun start end delete)))
2167 nil t)
2168 (unless org-agenda-keep-modes
2169 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2170 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2171 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2172 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2173 (add-to-invisibility-spec '(org-filtered))
2174 (add-to-invisibility-spec '(org-link))
2175 (easy-menu-change
2176 '("Agenda") "Agenda Files"
2177 (append
2178 (list
2179 (vector
2180 (if (get 'org-agenda-files 'org-restrict)
2181 "Restricted to single file"
2182 "Edit File List")
2183 '(org-edit-agenda-file-list)
2184 (not (get 'org-agenda-files 'org-restrict)))
2185 "--")
2186 (mapcar 'org-file-menu-entry (org-agenda-files))))
2187 (org-agenda-set-mode-name)
2188 (apply
2189 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2190 (list 'org-agenda-mode-hook)))
2192 (substitute-key-definition 'undo 'org-agenda-undo
2193 org-agenda-mode-map global-map)
2194 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2195 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2196 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2197 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2198 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2199 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2200 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2201 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2202 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2203 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2204 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2205 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2206 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2207 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2208 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2209 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2210 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2211 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2212 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2213 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2214 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2215 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2216 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2217 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2218 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2219 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2220 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2221 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2222 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2223 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2224 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2225 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2226 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2227 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2228 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2229 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2230 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2231 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2232 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2233 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2234 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2235 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2236 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2237 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2238 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2239 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2240 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2241 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2242 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2243 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2245 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2246 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2247 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2248 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2249 (while l (org-defkey org-agenda-mode-map
2250 (int-to-string (pop l)) 'digit-argument)))
2252 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2253 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2254 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2255 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2256 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2257 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2258 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2259 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2260 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2261 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2262 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2263 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2264 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2265 'org-clock-modify-effort-estimate)
2266 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2267 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2268 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2269 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2270 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2271 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2272 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2273 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2274 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2275 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2276 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2277 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2278 (substitute-key-definition 'next-line 'org-agenda-next-line
2279 org-agenda-mode-map global-map)
2280 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2281 org-agenda-mode-map global-map)
2282 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2283 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2284 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2285 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2286 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2287 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2288 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2289 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2290 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2291 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2292 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2293 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2294 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2295 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2296 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2297 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2298 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2299 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2300 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2301 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2302 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2303 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2304 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2305 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2306 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2307 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2308 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2309 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2310 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2311 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2313 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2314 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2315 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2316 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2317 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2318 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2319 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2320 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2321 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2322 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2323 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2324 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2325 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2326 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2327 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2329 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2330 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2332 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2333 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2335 (when org-agenda-mouse-1-follows-link
2336 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2337 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2338 '("Agenda"
2339 ("Agenda Files")
2340 "--"
2341 ("Agenda Dates"
2342 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2343 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2344 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2345 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2346 "--"
2347 ("View"
2348 ["Day View" org-agenda-day-view
2349 :active (org-agenda-check-type nil 'agenda)
2350 :style radio :selected (eq org-agenda-current-span 'day)
2351 :keys "v d (or just d)"]
2352 ["Week View" org-agenda-week-view
2353 :active (org-agenda-check-type nil 'agenda)
2354 :style radio :selected (eq org-agenda-current-span 'week)
2355 :keys "v w"]
2356 ["Fortnight View" org-agenda-fortnight-view
2357 :active (org-agenda-check-type nil 'agenda)
2358 :style radio :selected (eq org-agenda-current-span 'fortnight)
2359 :keys "v f"]
2360 ["Month View" org-agenda-month-view
2361 :active (org-agenda-check-type nil 'agenda)
2362 :style radio :selected (eq org-agenda-current-span 'month)
2363 :keys "v m"]
2364 ["Year View" org-agenda-year-view
2365 :active (org-agenda-check-type nil 'agenda)
2366 :style radio :selected (eq org-agenda-current-span 'year)
2367 :keys "v y"]
2368 "--"
2369 ["Include Diary" org-agenda-toggle-diary
2370 :style toggle :selected org-agenda-include-diary
2371 :active (org-agenda-check-type nil 'agenda)]
2372 ["Include Deadlines" org-agenda-toggle-deadlines
2373 :style toggle :selected org-agenda-include-deadlines
2374 :active (org-agenda-check-type nil 'agenda)]
2375 ["Use Time Grid" org-agenda-toggle-time-grid
2376 :style toggle :selected org-agenda-use-time-grid
2377 :active (org-agenda-check-type nil 'agenda)]
2378 "--"
2379 ["Show clock report" org-agenda-clockreport-mode
2380 :style toggle :selected org-agenda-clockreport-mode
2381 :active (org-agenda-check-type nil 'agenda)]
2382 ["Show some entry text" org-agenda-entry-text-mode
2383 :style toggle :selected org-agenda-entry-text-mode
2384 :active t]
2385 "--"
2386 ["Show Logbook entries" org-agenda-log-mode
2387 :style toggle :selected org-agenda-show-log
2388 :active (org-agenda-check-type nil 'agenda 'timeline)
2389 :keys "v l (or just l)"]
2390 ["Include archived trees" org-agenda-archives-mode
2391 :style toggle :selected org-agenda-archives-mode :active t
2392 :keys "v a"]
2393 ["Include archive files" (org-agenda-archives-mode t)
2394 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2395 :keys "v A"]
2396 "--"
2397 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2398 ["Write view to file" org-agenda-write t]
2399 ["Rebuild buffer" org-agenda-redo t]
2400 ["Save all Org buffers" org-save-all-org-buffers t]
2401 "--"
2402 ["Show original entry" org-agenda-show t]
2403 ["Go To (other window)" org-agenda-goto t]
2404 ["Go To (this window)" org-agenda-switch-to t]
2405 ["Capture with cursor date" org-agenda-capture t]
2406 ["Follow Mode" org-agenda-follow-mode
2407 :style toggle :selected org-agenda-follow-mode :active t]
2408 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2409 "--"
2410 ("TODO"
2411 ["Cycle TODO" org-agenda-todo t]
2412 ["Next TODO set" org-agenda-todo-nextset t]
2413 ["Previous TODO set" org-agenda-todo-previousset t]
2414 ["Add note" org-agenda-add-note t])
2415 ("Archive/Refile/Delete"
2416 ["Archive default" org-agenda-archive-default t]
2417 ["Archive default" org-agenda-archive-default-with-confirmation t]
2418 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2419 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2420 ["Archive subtree" org-agenda-archive t]
2421 "--"
2422 ["Refile" org-agenda-refile t]
2423 "--"
2424 ["Delete subtree" org-agenda-kill t])
2425 ("Bulk action"
2426 ["Mark entry" org-agenda-bulk-mark t]
2427 ["Mark all" org-agenda-bulk-mark-all t]
2428 ["Unmark entry" org-agenda-bulk-unmark t]
2429 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2430 ["Toggle mark" org-agenda-bulk-toggle t]
2431 ["Toggle all" org-agenda-bulk-toggle-all t]
2432 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2433 ["Act on all marked" org-agenda-bulk-action t]
2434 "--"
2435 ("Tags and Properties"
2436 ["Show all Tags" org-agenda-show-tags t]
2437 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2438 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2439 "--"
2440 ["Column View" org-columns t])
2441 ("Deadline/Schedule"
2442 ["Schedule" org-agenda-schedule t]
2443 ["Set Deadline" org-agenda-deadline t]
2444 "--"
2445 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2446 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2447 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2448 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2449 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2450 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2451 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2452 ("Clock and Effort"
2453 ["Clock in" org-agenda-clock-in t]
2454 ["Clock out" org-agenda-clock-out t]
2455 ["Clock cancel" org-agenda-clock-cancel t]
2456 ["Goto running clock" org-clock-goto t]
2457 "--"
2458 ["Set Effort" org-agenda-set-effort t]
2459 ["Change clocked effort" org-clock-modify-effort-estimate
2460 (org-clock-is-active)])
2461 ("Priority"
2462 ["Set Priority" org-agenda-priority t]
2463 ["Increase Priority" org-agenda-priority-up t]
2464 ["Decrease Priority" org-agenda-priority-down t]
2465 ["Show Priority" org-show-priority t])
2466 ("Calendar/Diary"
2467 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2468 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2469 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2470 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2471 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2472 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2473 "--"
2474 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2475 "--"
2476 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2477 "--"
2478 ("MobileOrg"
2479 ["Push Files and Views" org-mobile-push t]
2480 ["Get Captured and Flagged" org-mobile-pull t]
2481 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2482 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2483 "--"
2484 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2485 "--"
2486 ["Quit" org-agenda-quit t]
2487 ["Exit and Release Buffers" org-agenda-exit t]
2490 ;;; Agenda undo
2492 (defvar org-agenda-allow-remote-undo t
2493 "Non-nil means allow remote undo from the agenda buffer.")
2494 (defvar org-agenda-undo-has-started-in nil
2495 "Buffers that have already seen `undo-start' in the current undo sequence.")
2497 (defun org-agenda-undo ()
2498 "Undo a remote editing step in the agenda.
2499 This undoes changes both in the agenda buffer and in the remote buffer
2500 that have been changed along."
2501 (interactive)
2502 (or org-agenda-allow-remote-undo
2503 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2504 (if (not (eq this-command last-command))
2505 (setq org-agenda-undo-has-started-in nil
2506 org-agenda-pending-undo-list org-agenda-undo-list))
2507 (if (not org-agenda-pending-undo-list)
2508 (user-error "No further undo information"))
2509 (let* ((entry (pop org-agenda-pending-undo-list))
2510 buf line cmd rembuf)
2511 (setq cmd (pop entry) line (pop entry))
2512 (setq rembuf (nth 2 entry))
2513 (org-with-remote-undo rembuf
2514 (while (bufferp (setq buf (pop entry)))
2515 (if (pop entry)
2516 (with-current-buffer buf
2517 (let ((last-undo-buffer buf)
2518 (inhibit-read-only t))
2519 (unless (memq buf org-agenda-undo-has-started-in)
2520 (push buf org-agenda-undo-has-started-in)
2521 (make-local-variable 'pending-undo-list)
2522 (undo-start))
2523 (while (and pending-undo-list
2524 (listp pending-undo-list)
2525 (not (car pending-undo-list)))
2526 (pop pending-undo-list))
2527 (undo-more 1))))))
2528 (org-goto-line line)
2529 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2531 (defun org-verify-change-for-undo (l1 l2)
2532 "Verify that a real change occurred between the undo lists L1 and L2."
2533 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2534 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2535 (not (eq l1 l2)))
2537 ;;; Agenda dispatch
2539 (defvar org-agenda-restrict-begin (make-marker))
2540 (defvar org-agenda-restrict-end (make-marker))
2541 (defvar org-agenda-last-dispatch-buffer nil)
2542 (defvar org-agenda-overriding-restriction nil)
2544 (defcustom org-agenda-custom-commands-contexts nil
2545 "Alist of custom agenda keys and contextual rules.
2547 For example, if you have a custom agenda command \"p\" and you
2548 want this command to be accessible only from plain text files,
2549 use this:
2551 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2553 Here are the available contexts definitions:
2555 in-file: command displayed only in matching files
2556 in-mode: command displayed only in matching modes
2557 not-in-file: command not displayed in matching files
2558 not-in-mode: command not displayed in matching modes
2559 in-buffer: command displayed only in matching buffers
2560 not-in-buffer: command not displayed in matching buffers
2561 [function]: a custom function taking no argument
2563 If you define several checks, the agenda command will be
2564 accessible if there is at least one valid check.
2566 You can also bind a key to another agenda custom command
2567 depending on contextual rules.
2569 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2571 Here it means: in .txt files, use \"p\" as the key for the
2572 agenda command otherwise associated with \"q\". (The command
2573 originally associated with \"q\" is not displayed to avoid
2574 duplicates.)"
2575 :version "24.3"
2576 :group 'org-agenda-custom-commands
2577 :type '(repeat (list :tag "Rule"
2578 (string :tag " Agenda key")
2579 (string :tag "Replace by command")
2580 (repeat :tag "Available when"
2581 (choice
2582 (cons :tag "Condition"
2583 (choice
2584 (const :tag "In file" in-file)
2585 (const :tag "Not in file" not-in-file)
2586 (const :tag "In buffer" in-buffer)
2587 (const :tag "Not in buffer" not-in-buffer)
2588 (const :tag "In mode" in-mode)
2589 (const :tag "Not in mode" not-in-mode))
2590 (regexp))
2591 (function :tag "Custom function"))))))
2593 (defcustom org-agenda-max-entries nil
2594 "Maximum number of entries to display in an agenda.
2595 This can be nil (no limit) or an integer or an alist of agenda
2596 types with an associated number of entries to display in this
2597 type."
2598 :version "24.4"
2599 :package-version '(Org . "8.0")
2600 :group 'org-agenda-custom-commands
2601 :type '(choice (symbol :tag "No limit" nil)
2602 (integer :tag "Max number of entries")
2603 (repeat
2604 (cons (choice :tag "Agenda type"
2605 (const agenda)
2606 (const todo)
2607 (const tags)
2608 (const search)
2609 (const timeline))
2610 (integer :tag "Max number of entries")))))
2612 (defcustom org-agenda-max-todos nil
2613 "Maximum number of TODOs to display in an agenda.
2614 This can be nil (no limit) or an integer or an alist of agenda
2615 types with an associated number of entries to display in this
2616 type."
2617 :version "24.4"
2618 :package-version '(Org . "8.0")
2619 :group 'org-agenda-custom-commands
2620 :type '(choice (symbol :tag "No limit" nil)
2621 (integer :tag "Max number of TODOs")
2622 (repeat
2623 (cons (choice :tag "Agenda type"
2624 (const agenda)
2625 (const todo)
2626 (const tags)
2627 (const search)
2628 (const timeline))
2629 (integer :tag "Max number of TODOs")))))
2631 (defcustom org-agenda-max-tags nil
2632 "Maximum number of tagged entries to display in an agenda.
2633 This can be nil (no limit) or an integer or an alist of agenda
2634 types with an associated number of entries to display in this
2635 type."
2636 :version "24.4"
2637 :package-version '(Org . "8.0")
2638 :group 'org-agenda-custom-commands
2639 :type '(choice (symbol :tag "No limit" nil)
2640 (integer :tag "Max number of tagged entries")
2641 (repeat
2642 (cons (choice :tag "Agenda type"
2643 (const agenda)
2644 (const todo)
2645 (const tags)
2646 (const search)
2647 (const timeline))
2648 (integer :tag "Max number of tagged entries")))))
2650 (defcustom org-agenda-max-effort nil
2651 "Maximum cumulated effort duration for the agenda.
2652 This can be nil (no limit) or a number of minutes (as an integer)
2653 or an alist of agenda types with an associated number of minutes
2654 to limit entries to in this type."
2655 :version "24.4"
2656 :package-version '(Org . "8.0")
2657 :group 'org-agenda-custom-commands
2658 :type '(choice (symbol :tag "No limit" nil)
2659 (integer :tag "Max number of minutes")
2660 (repeat
2661 (cons (choice :tag "Agenda type"
2662 (const agenda)
2663 (const todo)
2664 (const tags)
2665 (const search)
2666 (const timeline))
2667 (integer :tag "Max number of minutes")))))
2669 (defvar org-agenda-keep-restricted-file-list nil)
2670 (defvar org-keys nil)
2671 (defvar org-match nil)
2672 ;;;###autoload
2673 (defun org-agenda (&optional arg org-keys restriction)
2674 "Dispatch agenda commands to collect entries to the agenda buffer.
2675 Prompts for a command to execute. Any prefix arg will be passed
2676 on to the selected command. The default selections are:
2678 a Call `org-agenda-list' to display the agenda for current day or week.
2679 t Call `org-todo-list' to display the global todo list.
2680 T Call `org-todo-list' to display the global todo list, select only
2681 entries with a specific TODO keyword (the user gets a prompt).
2682 m Call `org-tags-view' to display headlines with tags matching
2683 a condition (the user is prompted for the condition).
2684 M Like `m', but select only TODO entries, no ordinary headlines.
2685 L Create a timeline for the current buffer.
2686 e Export views to associated files.
2687 s Search entries for keywords.
2688 S Search entries for keywords, only with TODO keywords.
2689 / Multi occur across all agenda files and also files listed
2690 in `org-agenda-text-search-extra-files'.
2691 < Restrict agenda commands to buffer, subtree, or region.
2692 Press several times to get the desired effect.
2693 > Remove a previous restriction.
2694 # List \"stuck\" projects.
2695 ! Configure what \"stuck\" means.
2696 C Configure custom agenda commands.
2698 More commands can be added by configuring the variable
2699 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2700 searches can be pre-defined in this way.
2702 If the current buffer is in Org mode and visiting a file, you can also
2703 first press `<' once to indicate that the agenda should be temporarily
2704 \(until the next use of `\\[org-agenda]') restricted to the current file.
2705 Pressing `<' twice means to restrict to the current subtree or region
2706 \(if active)."
2707 (interactive "P")
2708 (catch 'exit
2709 (let* ((prefix-descriptions nil)
2710 (org-agenda-buffer-name org-agenda-buffer-name)
2711 (org-agenda-window-setup (if (equal (buffer-name)
2712 org-agenda-buffer-name)
2713 'current-window
2714 org-agenda-window-setup))
2715 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2716 (org-agenda-custom-commands
2717 ;; normalize different versions
2718 (delq nil
2719 (mapcar
2720 (lambda (x)
2721 (cond ((stringp (cdr x))
2722 (push x prefix-descriptions)
2723 nil)
2724 ((stringp (nth 1 x)) x)
2725 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2726 (t (cons (car x) (cons "" (cdr x))))))
2727 org-agenda-custom-commands)))
2728 (org-agenda-custom-commands
2729 (org-contextualize-keys
2730 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2731 (buf (current-buffer))
2732 (bfn (buffer-file-name (buffer-base-buffer)))
2733 entry key type org-match lprops ans)
2734 ;; Turn off restriction unless there is an overriding one,
2735 (unless org-agenda-overriding-restriction
2736 (unless org-agenda-keep-restricted-file-list
2737 ;; There is a request to keep the file list in place
2738 (put 'org-agenda-files 'org-restrict nil))
2739 (setq org-agenda-restrict nil)
2740 (move-marker org-agenda-restrict-begin nil)
2741 (move-marker org-agenda-restrict-end nil))
2742 ;; Delete old local properties
2743 (put 'org-agenda-redo-command 'org-lprops nil)
2744 ;; Delete previously set last-arguments
2745 (put 'org-agenda-redo-command 'last-args nil)
2746 ;; Remember where this call originated
2747 (setq org-agenda-last-dispatch-buffer (current-buffer))
2748 (unless org-keys
2749 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2750 org-keys (car ans)
2751 restriction (cdr ans)))
2752 ;; If we have sticky agenda buffers, set a name for the buffer,
2753 ;; depending on the invoking keys. The user may still set this
2754 ;; as a command option, which will overwrite what we do here.
2755 (if org-agenda-sticky
2756 (setq org-agenda-buffer-name
2757 (format "*Org Agenda(%s)*" org-keys)))
2758 ;; Establish the restriction, if any
2759 (when (and (not org-agenda-overriding-restriction) restriction)
2760 (put 'org-agenda-files 'org-restrict (list bfn))
2761 (cond
2762 ((eq restriction 'region)
2763 (setq org-agenda-restrict (current-buffer))
2764 (move-marker org-agenda-restrict-begin (region-beginning))
2765 (move-marker org-agenda-restrict-end (region-end)))
2766 ((eq restriction 'subtree)
2767 (save-excursion
2768 (setq org-agenda-restrict (current-buffer))
2769 (org-back-to-heading t)
2770 (move-marker org-agenda-restrict-begin (point))
2771 (move-marker org-agenda-restrict-end
2772 (progn (org-end-of-subtree t)))))))
2774 ;; For example the todo list should not need it (but does...)
2775 (cond
2776 ((setq entry (assoc org-keys org-agenda-custom-commands))
2777 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2778 (progn
2779 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2780 lprops (nth 4 entry))
2781 (if org-agenda-sticky
2782 (setq org-agenda-buffer-name
2783 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2784 (format "*Org Agenda(%s)*" org-keys))))
2785 (put 'org-agenda-redo-command 'org-lprops lprops)
2786 (cond
2787 ((eq type 'agenda)
2788 (org-let lprops '(org-agenda-list current-prefix-arg)))
2789 ((eq type 'agenda*)
2790 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2791 ((eq type 'alltodo)
2792 (org-let lprops '(org-todo-list current-prefix-arg)))
2793 ((eq type 'search)
2794 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2795 ((eq type 'stuck)
2796 (org-let lprops '(org-agenda-list-stuck-projects
2797 current-prefix-arg)))
2798 ((eq type 'tags)
2799 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2800 ((eq type 'tags-todo)
2801 (org-let lprops '(org-tags-view '(4) org-match)))
2802 ((eq type 'todo)
2803 (org-let lprops '(org-todo-list org-match)))
2804 ((eq type 'tags-tree)
2805 (org-check-for-org-mode)
2806 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2807 ((eq type 'todo-tree)
2808 (org-check-for-org-mode)
2809 (org-let lprops
2810 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2811 (regexp-quote org-match) "\\>"))))
2812 ((eq type 'occur-tree)
2813 (org-check-for-org-mode)
2814 (org-let lprops '(org-occur org-match)))
2815 ((functionp type)
2816 (org-let lprops '(funcall type org-match)))
2817 ((fboundp type)
2818 (org-let lprops '(funcall type org-match)))
2819 (t (user-error "Invalid custom agenda command type %s" type))))
2820 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2821 ((equal org-keys "C")
2822 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2823 (customize-variable 'org-agenda-custom-commands))
2824 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2825 ((equal org-keys "s") (call-interactively 'org-search-view))
2826 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2827 ((equal org-keys "t") (call-interactively 'org-todo-list))
2828 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2829 ((equal org-keys "m") (call-interactively 'org-tags-view))
2830 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2831 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2832 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2833 (add-hook
2834 'post-command-hook
2835 (lambda ()
2836 (unless (current-message)
2837 (let* ((m (org-agenda-get-any-marker))
2838 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2839 (when note
2840 (message (concat
2841 "FLAGGING-NOTE ([?] for more info): "
2842 (org-add-props
2843 (replace-regexp-in-string
2844 "\\\\n" "//"
2845 (copy-sequence note))
2846 nil 'face 'org-warning)))))))
2847 t t))
2848 ((equal org-keys "L")
2849 (unless (derived-mode-p 'org-mode)
2850 (user-error "This is not an Org file"))
2851 (unless restriction
2852 (put 'org-agenda-files 'org-restrict (list bfn))
2853 (org-call-with-arg 'org-timeline arg)))
2854 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2855 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2856 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2857 (t (user-error "Invalid agenda key"))))))
2859 (defvar org-agenda-multi)
2861 (defun org-agenda-append-agenda ()
2862 "Append another agenda view to the current one.
2863 This function allows interactive building of block agendas.
2864 Agenda views are separated by `org-agenda-block-separator'."
2865 (interactive)
2866 (unless (derived-mode-p 'org-agenda-mode)
2867 (user-error "Can only append from within agenda buffer"))
2868 (let ((org-agenda-multi t))
2869 (org-agenda)
2870 (widen)
2871 (org-agenda-finalize)
2872 (setq buffer-read-only t)
2873 (org-agenda-fit-window-to-buffer)))
2875 (defun org-agenda-normalize-custom-commands (cmds)
2876 "Normalize custom commands CMDS."
2877 (delq nil
2878 (mapcar
2879 (lambda (x)
2880 (cond ((stringp (cdr x)) nil)
2881 ((stringp (nth 1 x)) x)
2882 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2883 (t (cons (car x) (cons "" (cdr x))))))
2884 cmds)))
2886 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2887 "The user interface for selecting an agenda command."
2888 (catch 'exit
2889 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2890 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2891 (region-p (org-region-active-p))
2892 (custom org-agenda-custom-commands)
2893 (selstring "")
2894 restriction second-time
2895 c entry key type match prefixes rmheader header-end custom1 desc
2896 line lines left right n n1)
2897 (save-window-excursion
2898 (delete-other-windows)
2899 (org-switch-to-buffer-other-window " *Agenda Commands*")
2900 (erase-buffer)
2901 (insert (eval-when-compile
2902 (let ((header
2903 "Press key for an agenda command: < Buffer, subtree/region restriction
2904 -------------------------------- > Remove restriction
2905 a Agenda for current week or day e Export agenda views
2906 t List of all TODO entries T Entries with special TODO kwd
2907 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2908 s Search for keywords S Like s, but only TODO entries
2909 L Timeline for current buffer # List stuck projects (!=configure)
2910 / Multi-occur C Configure custom agenda commands
2911 ? Find :FLAGGED: entries * Toggle sticky agenda views
2913 (start 0))
2914 (while (string-match
2915 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2916 header start)
2917 (setq start (match-end 0))
2918 (add-text-properties (match-beginning 2) (match-end 2)
2919 '(face bold) header))
2920 header)))
2921 (setq header-end (point-marker))
2922 (while t
2923 (setq custom1 custom)
2924 (when (eq rmheader t)
2925 (org-goto-line 1)
2926 (re-search-forward ":" nil t)
2927 (delete-region (match-end 0) (point-at-eol))
2928 (forward-char 1)
2929 (looking-at "-+")
2930 (delete-region (match-end 0) (point-at-eol))
2931 (move-marker header-end (match-end 0)))
2932 (goto-char header-end)
2933 (delete-region (point) (point-max))
2935 ;; Produce all the lines that describe custom commands and prefixes
2936 (setq lines nil)
2937 (while (setq entry (pop custom1))
2938 (setq key (car entry) desc (nth 1 entry)
2939 type (nth 2 entry)
2940 match (nth 3 entry))
2941 (if (> (length key) 1)
2942 (add-to-list 'prefixes (string-to-char key))
2943 (setq line
2944 (format
2945 "%-4s%-14s"
2946 (org-add-props (copy-sequence key)
2947 '(face bold))
2948 (cond
2949 ((string-match "\\S-" desc) desc)
2950 ((eq type 'agenda) "Agenda for current week or day")
2951 ((eq type 'agenda*) "Appointments for current week or day")
2952 ((eq type 'alltodo) "List of all TODO entries")
2953 ((eq type 'search) "Word search")
2954 ((eq type 'stuck) "List of stuck projects")
2955 ((eq type 'todo) "TODO keyword")
2956 ((eq type 'tags) "Tags query")
2957 ((eq type 'tags-todo) "Tags (TODO)")
2958 ((eq type 'tags-tree) "Tags tree")
2959 ((eq type 'todo-tree) "TODO kwd tree")
2960 ((eq type 'occur-tree) "Occur tree")
2961 ((functionp type) (if (symbolp type)
2962 (symbol-name type)
2963 "Lambda expression"))
2964 (t "???"))))
2965 (if org-agenda-menu-show-matcher
2966 (setq line
2967 (concat line ": "
2968 (cond
2969 ((stringp match)
2970 (setq match (copy-sequence match))
2971 (org-add-props match nil 'face 'org-warning))
2972 ((listp type)
2973 (format "set of %d commands" (length type))))))
2974 (if (org-string-nw-p match)
2975 (add-text-properties
2976 0 (length line) (list 'help-echo
2977 (concat "Matcher: " match)) line)))
2978 (push line lines)))
2979 (setq lines (nreverse lines))
2980 (when prefixes
2981 (mapc (lambda (x)
2982 (push
2983 (format "%s %s"
2984 (org-add-props (char-to-string x)
2985 nil 'face 'bold)
2986 (or (cdr (assoc (concat selstring
2987 (char-to-string x))
2988 prefix-descriptions))
2989 "Prefix key"))
2990 lines))
2991 prefixes))
2993 ;; Check if we should display in two columns
2994 (if org-agenda-menu-two-columns
2995 (progn
2996 (setq n (length lines)
2997 n1 (+ (/ n 2) (mod n 2))
2998 right (nthcdr n1 lines)
2999 left (copy-sequence lines))
3000 (setcdr (nthcdr (1- n1) left) nil))
3001 (setq left lines right nil))
3002 (while left
3003 (insert "\n" (pop left))
3004 (when right
3005 (if (< (current-column) 40)
3006 (move-to-column 40 t)
3007 (insert " "))
3008 (insert (pop right))))
3010 ;; Make the window the right size
3011 (goto-char (point-min))
3012 (if second-time
3013 (if (not (pos-visible-in-window-p (point-max)))
3014 (org-fit-window-to-buffer))
3015 (setq second-time t)
3016 (org-fit-window-to-buffer))
3018 ;; Ask for selection
3019 (message "Press key for agenda command%s:"
3020 (if (or restrict-ok org-agenda-overriding-restriction)
3021 (if org-agenda-overriding-restriction
3022 " (restriction lock active)"
3023 (if restriction
3024 (format " (restricted to %s)" restriction)
3025 " (unrestricted)"))
3026 ""))
3027 (setq c (read-char-exclusive))
3028 (message "")
3029 (cond
3030 ((assoc (char-to-string c) custom)
3031 (setq selstring (concat selstring (char-to-string c)))
3032 (throw 'exit (cons selstring restriction)))
3033 ((memq c prefixes)
3034 (setq selstring (concat selstring (char-to-string c))
3035 prefixes nil
3036 rmheader (or rmheader t)
3037 custom (delq nil (mapcar
3038 (lambda (x)
3039 (if (or (= (length (car x)) 1)
3040 (/= (string-to-char (car x)) c))
3042 (cons (substring (car x) 1) (cdr x))))
3043 custom))))
3044 ((eq c ?*)
3045 (call-interactively 'org-toggle-sticky-agenda)
3046 (sit-for 2))
3047 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3048 (message "Restriction is only possible in Org buffers")
3049 (ding) (sit-for 1))
3050 ((eq c ?1)
3051 (org-agenda-remove-restriction-lock 'noupdate)
3052 (setq restriction 'buffer))
3053 ((eq c ?0)
3054 (org-agenda-remove-restriction-lock 'noupdate)
3055 (setq restriction (if region-p 'region 'subtree)))
3056 ((eq c ?<)
3057 (org-agenda-remove-restriction-lock 'noupdate)
3058 (setq restriction
3059 (cond
3060 ((eq restriction 'buffer)
3061 (if region-p 'region 'subtree))
3062 ((memq restriction '(subtree region))
3063 nil)
3064 (t 'buffer))))
3065 ((eq c ?>)
3066 (org-agenda-remove-restriction-lock 'noupdate)
3067 (setq restriction nil))
3068 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3069 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3070 ((and (> (length selstring) 0) (eq c ?\d))
3071 (delete-window)
3072 (org-agenda-get-restriction-and-command prefix-descriptions))
3074 ((equal c ?q) (error "Abort"))
3075 (t (user-error "Invalid key %c" c))))))))
3077 (defun org-agenda-fit-window-to-buffer ()
3078 "Fit the window to the buffer size."
3079 (and (memq org-agenda-window-setup '(reorganize-frame))
3080 (fboundp 'fit-window-to-buffer)
3081 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3082 (= (car org-agenda-window-frame-fractions) 1.0))
3083 (delete-other-windows)
3084 (org-fit-window-to-buffer
3086 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3087 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3089 (defvar org-cmd nil)
3090 (defvar org-agenda-overriding-cmd nil)
3091 (defvar org-agenda-overriding-arguments nil)
3092 (defvar org-agenda-overriding-cmd-arguments nil)
3093 (defun org-agenda-run-series (name series)
3094 "Run agenda NAME as a SERIES of agenda commands."
3095 (org-let (nth 1 series) '(org-agenda-prepare name))
3096 ;; We need to reset agenda markers here, because when constructing a
3097 ;; block agenda, the individual blocks do not do that.
3098 (org-agenda-reset-markers)
3099 (let* ((org-agenda-multi t)
3100 (redo (list 'org-agenda-run-series name (list 'quote series)))
3101 (cmds (car series))
3102 (gprops (nth 1 series))
3103 match ;; The byte compiler incorrectly complains about this. Keep it!
3104 org-cmd type lprops)
3105 (while (setq org-cmd (pop cmds))
3106 (setq type (car org-cmd))
3107 (setq match (eval (nth 1 org-cmd)))
3108 (setq lprops (nth 2 org-cmd))
3109 (let ((org-agenda-overriding-arguments
3110 (if (eq org-agenda-overriding-cmd org-cmd)
3111 (or org-agenda-overriding-arguments
3112 org-agenda-overriding-cmd-arguments))))
3113 (cond
3114 ((eq type 'agenda)
3115 (org-let2 gprops lprops
3116 '(call-interactively 'org-agenda-list)))
3117 ((eq type 'agenda*)
3118 (org-let2 gprops lprops
3119 '(funcall 'org-agenda-list nil nil t)))
3120 ((eq type 'alltodo)
3121 (org-let2 gprops lprops
3122 '(call-interactively 'org-todo-list)))
3123 ((eq type 'search)
3124 (org-let2 gprops lprops
3125 '(org-search-view current-prefix-arg match nil)))
3126 ((eq type 'stuck)
3127 (org-let2 gprops lprops
3128 '(call-interactively 'org-agenda-list-stuck-projects)))
3129 ((eq type 'tags)
3130 (org-let2 gprops lprops
3131 '(org-tags-view current-prefix-arg match)))
3132 ((eq type 'tags-todo)
3133 (org-let2 gprops lprops
3134 '(org-tags-view '(4) match)))
3135 ((eq type 'todo)
3136 (org-let2 gprops lprops
3137 '(org-todo-list match)))
3138 ((fboundp type)
3139 (org-let2 gprops lprops
3140 '(funcall type match)))
3141 (t (error "Invalid type in command series")))))
3142 (widen)
3143 (let ((inhibit-read-only t))
3144 (add-text-properties (point-min) (point-max)
3145 `(org-series t org-series-redo-cmd ,redo)))
3146 (setq org-agenda-redo-command redo)
3147 (goto-char (point-min)))
3148 (org-agenda-fit-window-to-buffer)
3149 (org-let (nth 1 series) '(org-agenda-finalize)))
3151 ;;;###autoload
3152 (defmacro org-batch-agenda (cmd-key &rest parameters)
3153 "Run an agenda command in batch mode and send the result to STDOUT.
3154 If CMD-KEY is a string of length 1, it is used as a key in
3155 `org-agenda-custom-commands' and triggers this command. If it is a
3156 longer string it is used as a tags/todo match string.
3157 Parameters are alternating variable names and values that will be bound
3158 before running the agenda command."
3159 (org-eval-in-environment (org-make-parameter-alist parameters)
3160 (let (org-agenda-sticky)
3161 (if (> (length cmd-key) 1)
3162 (org-tags-view nil cmd-key)
3163 (org-agenda nil cmd-key))))
3164 (set-buffer org-agenda-buffer-name)
3165 (princ (buffer-string)))
3167 (defvar org-agenda-info nil)
3169 ;;;###autoload
3170 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3171 "Run an agenda command in batch mode and send the result to STDOUT.
3172 If CMD-KEY is a string of length 1, it is used as a key in
3173 `org-agenda-custom-commands' and triggers this command. If it is a
3174 longer string it is used as a tags/todo match string.
3175 Parameters are alternating variable names and values that will be bound
3176 before running the agenda command.
3178 The output gives a line for each selected agenda item. Each
3179 item is a list of comma-separated values, like this:
3181 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3183 category The category of the item
3184 head The headline, without TODO kwd, TAGS and PRIORITY
3185 type The type of the agenda entry, can be
3186 todo selected in TODO match
3187 tagsmatch selected in tags match
3188 diary imported from diary
3189 deadline a deadline on given date
3190 scheduled scheduled on given date
3191 timestamp entry has timestamp on given date
3192 closed entry was closed on given date
3193 upcoming-deadline warning about deadline
3194 past-scheduled forwarded scheduled item
3195 block entry has date block including g. date
3196 todo The todo keyword, if any
3197 tags All tags including inherited ones, separated by colons
3198 date The relevant date, like 2007-2-14
3199 time The time, like 15:00-16:50
3200 extra Sting with extra planning info
3201 priority-l The priority letter if any was given
3202 priority-n The computed numerical priority
3203 agenda-day The day in the agenda where this is listed"
3204 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3205 (org-make-parameter-alist parameters))
3206 (if (> (length cmd-key) 2)
3207 (org-tags-view nil cmd-key)
3208 (org-agenda nil cmd-key)))
3209 (set-buffer org-agenda-buffer-name)
3210 (let* ((lines (org-split-string (buffer-string) "\n"))
3211 line)
3212 (while (setq line (pop lines))
3213 (catch 'next
3214 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3215 (setq org-agenda-info
3216 (org-fix-agenda-info (text-properties-at 0 line)))
3217 (princ
3218 (mapconcat 'org-agenda-export-csv-mapper
3219 '(org-category txt type todo tags date time extra
3220 priority-letter priority agenda-day)
3221 ","))
3222 (princ "\n")))))
3224 (defun org-fix-agenda-info (props)
3225 "Make sure all properties on an agenda item have a canonical form.
3226 This ensures the export commands can easily use it."
3227 (let (tmp re)
3228 (when (setq tmp (plist-get props 'tags))
3229 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3230 (when (setq tmp (plist-get props 'date))
3231 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3232 (let ((calendar-date-display-form '(year "-" month "-" day)))
3233 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3235 (setq tmp (calendar-date-string tmp)))
3236 (setq props (plist-put props 'date tmp)))
3237 (when (setq tmp (plist-get props 'day))
3238 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3239 (let ((calendar-date-display-form '(year "-" month "-" day)))
3240 (setq tmp (calendar-date-string tmp)))
3241 (setq props (plist-put props 'day tmp))
3242 (setq props (plist-put props 'agenda-day tmp)))
3243 (when (setq tmp (plist-get props 'txt))
3244 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3245 (plist-put props 'priority-letter (match-string 1 tmp))
3246 (setq tmp (replace-match "" t t tmp)))
3247 (when (and (setq re (plist-get props 'org-todo-regexp))
3248 (setq re (concat "\\`\\.*" re " ?"))
3249 (string-match re tmp))
3250 (plist-put props 'todo (match-string 1 tmp))
3251 (setq tmp (replace-match "" t t tmp)))
3252 (plist-put props 'txt tmp)))
3253 props)
3255 (defun org-agenda-export-csv-mapper (prop)
3256 (let ((res (plist-get org-agenda-info prop)))
3257 (setq res
3258 (cond
3259 ((not res) "")
3260 ((stringp res) res)
3261 (t (prin1-to-string res))))
3262 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3264 ;;;###autoload
3265 (defun org-store-agenda-views (&rest parameters)
3266 "Store agenda views."
3267 (interactive)
3268 (eval (list 'org-batch-store-agenda-views)))
3270 ;;;###autoload
3271 (defmacro org-batch-store-agenda-views (&rest parameters)
3272 "Run all custom agenda commands that have a file argument."
3273 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3274 (pop-up-frames nil)
3275 (dir default-directory)
3276 (pars (org-make-parameter-alist parameters))
3277 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3278 (save-window-excursion
3279 (while cmds
3280 (setq cmd (pop cmds)
3281 thiscmdkey (car cmd)
3282 thiscmdcmd (cdr cmd)
3283 match (nth 2 thiscmdcmd)
3284 bufname (if org-agenda-sticky
3285 (or (and (stringp match)
3286 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3287 (format "*Org Agenda(%s)*" thiscmdkey))
3288 org-agenda-buffer-name)
3289 cmd-or-set (nth 2 cmd)
3290 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3291 files (nth (if (listp cmd-or-set) 4 5) cmd))
3292 (if (stringp files) (setq files (list files)))
3293 (when files
3294 (org-eval-in-environment (append org-agenda-exporter-settings
3295 opts pars)
3296 (org-agenda nil thiscmdkey))
3297 (set-buffer bufname)
3298 (while files
3299 (org-eval-in-environment (append org-agenda-exporter-settings
3300 opts pars)
3301 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3302 (and (get-buffer bufname)
3303 (kill-buffer bufname)))))))
3305 (defvar org-agenda-current-span nil
3306 "The current span used in the agenda view.") ; local variable in the agenda buffer
3307 (defun org-agenda-mark-header-line (pos)
3308 "Mark the line at POS as an agenda structure header."
3309 (save-excursion
3310 (goto-char pos)
3311 (put-text-property (point-at-bol) (point-at-eol)
3312 'org-agenda-structural-header t)
3313 (when org-agenda-title-append
3314 (put-text-property (point-at-bol) (point-at-eol)
3315 'org-agenda-title-append org-agenda-title-append))))
3317 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3318 (defvar org-agenda-write-buffer-name "Agenda View")
3319 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3320 "Write the current buffer (an agenda view) as a file.
3322 Depending on the extension of the file name, plain text (.txt),
3323 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3324 If the extension is .ics, translate visible agenda into iCalendar
3325 format. If the extension is .org, collect all subtrees
3326 corresponding to the agenda entries and add them in an .org file.
3328 With prefix argument OPEN, open the new file immediately. If
3329 NOSETTINGS is given, do not scope the settings of
3330 `org-agenda-exporter-settings' into the export commands. This is
3331 used when the settings have already been scoped and we do not
3332 wish to overrule other, higher priority settings. If
3333 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3334 the agenda to write."
3335 (interactive "FWrite agenda to file: \nP")
3336 (if (or (not (file-writable-p file))
3337 (and (file-exists-p file)
3338 (if (called-interactively-p 'any)
3339 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3340 (user-error "Cannot write agenda to file %s" file))
3341 (org-let (if nosettings nil org-agenda-exporter-settings)
3342 '(save-excursion
3343 (save-window-excursion
3344 (let ((bs (copy-sequence (buffer-string)))
3345 (extension (file-name-extension file))
3346 beg content)
3347 (with-temp-buffer
3348 (rename-buffer org-agenda-write-buffer-name t)
3349 (set-buffer-modified-p nil)
3350 (insert bs)
3351 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3352 (run-hooks 'org-agenda-before-write-hook)
3353 (cond
3354 ((bound-and-true-p org-mobile-creating-agendas)
3355 (org-mobile-write-agenda-for-mobile file))
3356 ((string= "org" extension)
3357 (let (content p m message-log-max)
3358 (goto-char (point-min))
3359 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3360 (goto-char p)
3361 (setq m (get-text-property (point) 'org-hd-marker))
3362 (when m
3363 (push (save-excursion
3364 (set-buffer (marker-buffer m))
3365 (goto-char m)
3366 (org-copy-subtree 1 nil t t)
3367 org-subtree-clip)
3368 content)))
3369 (find-file file)
3370 (erase-buffer)
3371 (dolist (s content) (org-paste-subtree 1 s))
3372 (write-file file)
3373 (kill-buffer (current-buffer))
3374 (message "Org file written to %s" file)))
3375 ((member extension '("html" "htm"))
3376 (require 'htmlize)
3377 (set-buffer (htmlize-buffer (current-buffer)))
3378 (when org-agenda-export-html-style
3379 ;; replace <style> section with org-agenda-export-html-style
3380 (goto-char (point-min))
3381 (kill-region (- (search-forward "<style") 6)
3382 (search-forward "</style>"))
3383 (insert org-agenda-export-html-style))
3384 (write-file file)
3385 (kill-buffer (current-buffer))
3386 (message "HTML written to %s" file))
3387 ((string= "ps" extension)
3388 (require 'ps-print)
3389 (ps-print-buffer-with-faces file)
3390 (message "Postscript written to %s" file))
3391 ((string= "pdf" extension)
3392 (require 'ps-print)
3393 (ps-print-buffer-with-faces
3394 (concat (file-name-sans-extension file) ".ps"))
3395 (call-process "ps2pdf" nil nil nil
3396 (expand-file-name
3397 (concat (file-name-sans-extension file) ".ps"))
3398 (expand-file-name file))
3399 (delete-file (concat (file-name-sans-extension file) ".ps"))
3400 (message "PDF written to %s" file))
3401 ((string= "ics" extension)
3402 (require 'ox-icalendar)
3403 (org-icalendar-export-current-agenda (expand-file-name file)))
3405 (let ((bs (buffer-string)))
3406 (find-file file)
3407 (erase-buffer)
3408 (insert bs)
3409 (save-buffer 0)
3410 (kill-buffer (current-buffer))
3411 (message "Plain text written to %s" file))))))))
3412 (set-buffer (or agenda-bufname
3413 (and (called-interactively-p 'any) (buffer-name))
3414 org-agenda-buffer-name)))
3415 (when open (org-open-file file)))
3417 (defun org-agenda-remove-marked-text (property &optional value)
3418 "Delete all text marked with VALUE of PROPERTY.
3419 VALUE defaults to t."
3420 (let (beg)
3421 (setq value (or value t))
3422 (while (setq beg (text-property-any (point-min) (point-max)
3423 property value))
3424 (delete-region
3425 beg (or (next-single-property-change beg property)
3426 (point-max))))))
3428 (defun org-agenda-add-entry-text ()
3429 "Add entry text to agenda lines.
3430 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3431 entry text following headings shown in the agenda.
3432 Drawers will be excluded, also the line with scheduling/deadline info."
3433 (when (and (> org-agenda-add-entry-text-maxlines 0)
3434 (not (bound-and-true-p org-mobile-creating-agendas)))
3435 (let (m txt)
3436 (goto-char (point-min))
3437 (while (not (eobp))
3438 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3439 (beginning-of-line 2)
3440 (setq txt (org-agenda-get-some-entry-text
3441 m org-agenda-add-entry-text-maxlines " > "))
3442 (end-of-line 1)
3443 (if (string-match "\\S-" txt)
3444 (insert "\n" txt)
3445 (or (eobp) (forward-char 1))))))))
3447 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3448 &rest keep)
3449 "Extract entry text from MARKER, at most N-LINES lines.
3450 This will ignore drawers etc, just get the text.
3451 If INDENT is given, prefix every line with this string. If KEEP is
3452 given, it is a list of symbols, defining stuff that should not be
3453 removed from the entry content. Currently only `planning' is allowed here."
3454 (let (txt drawer-re kwd-time-re ind)
3455 (save-excursion
3456 (with-current-buffer (marker-buffer marker)
3457 (if (not (derived-mode-p 'org-mode))
3458 (setq txt "")
3459 (org-with-wide-buffer
3460 (goto-char marker)
3461 (end-of-line 1)
3462 (setq txt (buffer-substring
3463 (min (1+ (point)) (point-max))
3464 (progn (outline-next-heading) (point)))
3465 drawer-re org-drawer-regexp
3466 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3467 ".*\n?"))
3468 (with-temp-buffer
3469 (insert txt)
3470 (when org-agenda-add-entry-text-descriptive-links
3471 (goto-char (point-min))
3472 (while (org-activate-bracket-links (point-max))
3473 (add-text-properties (match-beginning 0) (match-end 0)
3474 '(face org-link))))
3475 (goto-char (point-min))
3476 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3477 (set-text-properties (match-beginning 0) (match-end 0)
3478 nil))
3479 (goto-char (point-min))
3480 (while (re-search-forward drawer-re nil t)
3481 (delete-region
3482 (match-beginning 0)
3483 (progn (re-search-forward
3484 "^[ \t]*:END:.*\n?" nil 'move)
3485 (point))))
3486 (unless (member 'planning keep)
3487 (goto-char (point-min))
3488 (while (re-search-forward kwd-time-re nil t)
3489 (replace-match "")))
3490 (goto-char (point-min))
3491 (when org-agenda-entry-text-exclude-regexps
3492 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3493 (while (setq re (pop re-list))
3494 (goto-char (point-min))
3495 (while (re-search-forward re nil t)
3496 (replace-match "")))))
3497 (goto-char (point-max))
3498 (skip-chars-backward " \t\n")
3499 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3501 ;; find and remove min common indentation
3502 (goto-char (point-min))
3503 (untabify (point-min) (point-max))
3504 (setq ind (org-get-indentation))
3505 (while (not (eobp))
3506 (unless (looking-at "[ \t]*$")
3507 (setq ind (min ind (org-get-indentation))))
3508 (beginning-of-line 2))
3509 (goto-char (point-min))
3510 (while (not (eobp))
3511 (unless (looking-at "[ \t]*$")
3512 (move-to-column ind)
3513 (delete-region (point-at-bol) (point)))
3514 (beginning-of-line 2))
3516 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3518 (goto-char (point-min))
3519 (when indent
3520 (while (and (not (eobp)) (re-search-forward "^" nil t))
3521 (replace-match indent t t)))
3522 (goto-char (point-min))
3523 (while (looking-at "[ \t]*\n") (replace-match ""))
3524 (goto-char (point-max))
3525 (when (> (org-current-line)
3526 n-lines)
3527 (org-goto-line (1+ n-lines))
3528 (backward-char 1))
3529 (setq txt (buffer-substring (point-min) (point))))))))
3530 txt))
3532 (defun org-check-for-org-mode ()
3533 "Make sure current buffer is in org-mode. Error if not."
3534 (or (derived-mode-p 'org-mode)
3535 (error "Cannot execute Org agenda command on buffer in %s"
3536 major-mode)))
3538 ;;; Agenda prepare and finalize
3540 (defvar org-agenda-multi nil) ; dynamically scoped
3541 (defvar org-agenda-pre-window-conf nil)
3542 (defvar org-agenda-columns-active nil)
3543 (defvar org-agenda-name nil)
3544 (defvar org-agenda-tag-filter nil)
3545 (defvar org-agenda-category-filter nil)
3546 (defvar org-agenda-regexp-filter nil)
3547 (defvar org-agenda-effort-filter nil)
3548 (defvar org-agenda-top-headline-filter nil)
3549 (defvar org-agenda-tag-filter-preset nil
3550 "A preset of the tags filter used for secondary agenda filtering.
3551 This must be a list of strings, each string must be a single tag preceded
3552 by \"+\" or \"-\".
3553 This variable should not be set directly, but agenda custom commands can
3554 bind it in the options section. The preset filter is a global property of
3555 the entire agenda view. In a block agenda, it will not work reliably to
3556 define a filter for one of the individual blocks. You need to set it in
3557 the global options and expect it to be applied to the entire view.")
3559 (defvar org-agenda-category-filter-preset nil
3560 "A preset of the category filter used for secondary agenda filtering.
3561 This must be a list of strings, each string must be a single category
3562 preceded by \"+\" or \"-\".
3563 This variable should not be set directly, but agenda custom commands can
3564 bind it in the options section. The preset filter is a global property of
3565 the entire agenda view. In a block agenda, it will not work reliably to
3566 define a filter for one of the individual blocks. You need to set it in
3567 the global options and expect it to be applied to the entire view.")
3569 (defvar org-agenda-regexp-filter-preset nil
3570 "A preset of the regexp filter used for secondary agenda filtering.
3571 This must be a list of strings, each string must be a single regexp
3572 preceded by \"+\" or \"-\".
3573 This variable should not be set directly, but agenda custom commands can
3574 bind it in the options section. The preset filter is a global property of
3575 the entire agenda view. In a block agenda, it will not work reliably to
3576 define a filter for one of the individual blocks. You need to set it in
3577 the global options and expect it to be applied to the entire view.")
3579 (defvar org-agenda-effort-filter-preset nil
3580 "A preset of the effort condition used for secondary agenda filtering.
3581 This must be a list of strings, each string must be a single regexp
3582 preceded by \"+\" or \"-\".
3583 This variable should not be set directly, but agenda custom commands can
3584 bind it in the options section. The preset filter is a global property of
3585 the entire agenda view. In a block agenda, it will not work reliably to
3586 define a filter for one of the individual blocks. You need to set it in
3587 the global options and expect it to be applied to the entire view.")
3589 (defun org-agenda-use-sticky-p ()
3590 "Return non-nil if an agenda buffer named
3591 `org-agenda-buffer-name' exists and should be shown instead of
3592 generating a new one."
3593 (and
3594 ;; turned off by user
3595 org-agenda-sticky
3596 ;; For multi-agenda buffer already exists
3597 (not org-agenda-multi)
3598 ;; buffer found
3599 (get-buffer org-agenda-buffer-name)
3600 ;; C-u parameter is same as last call
3601 (with-current-buffer (get-buffer org-agenda-buffer-name)
3602 (and
3603 (equal current-prefix-arg
3604 org-agenda-last-prefix-arg)
3605 ;; In case user turned stickiness on, while having existing
3606 ;; Agenda buffer active, don't reuse that buffer, because it
3607 ;; does not have org variables local
3608 org-agenda-this-buffer-is-sticky))))
3610 (defun org-agenda-prepare-window (abuf filter-alist)
3611 "Setup agenda buffer in the window.
3612 ABUF is the buffer for the agenda window.
3613 FILTER-ALIST is an alist of filters we need to apply when
3614 `org-agenda-persistent-filter' is non-nil."
3615 (let* ((awin (get-buffer-window abuf)) wconf)
3616 (cond
3617 ((equal (current-buffer) abuf) nil)
3618 (awin (select-window awin))
3619 ((not (setq wconf (current-window-configuration))))
3620 ((eq org-agenda-window-setup 'current-window)
3621 (pop-to-buffer-same-window abuf))
3622 ((eq org-agenda-window-setup 'other-window)
3623 (org-switch-to-buffer-other-window abuf))
3624 ((eq org-agenda-window-setup 'other-frame)
3625 (switch-to-buffer-other-frame abuf))
3626 ((eq org-agenda-window-setup 'only-window)
3627 (delete-other-windows)
3628 (pop-to-buffer-same-window abuf))
3629 ((eq org-agenda-window-setup 'reorganize-frame)
3630 (delete-other-windows)
3631 (org-switch-to-buffer-other-window abuf)))
3632 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3633 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3634 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3635 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3636 ;; Additional test in case agenda is invoked from within agenda
3637 ;; buffer via elisp link.
3638 (unless (equal (current-buffer) abuf)
3639 (pop-to-buffer-same-window abuf))
3640 (setq org-agenda-pre-window-conf
3641 (or wconf org-agenda-pre-window-conf))))
3643 (defun org-agenda-prepare (&optional name)
3644 (let ((filter-alist (if org-agenda-persistent-filter
3645 (with-current-buffer
3646 (get-buffer-create org-agenda-buffer-name)
3647 (list `(tag . ,org-agenda-tag-filter)
3648 `(re . ,org-agenda-regexp-filter)
3649 `(effort . ,org-agenda-effort-filter)
3650 `(cat . ,org-agenda-category-filter))))))
3651 (if (org-agenda-use-sticky-p)
3652 (progn
3653 (put 'org-agenda-tag-filter :preset-filter nil)
3654 (put 'org-agenda-category-filter :preset-filter nil)
3655 (put 'org-agenda-regexp-filter :preset-filter nil)
3656 ;; Popup existing buffer
3657 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3658 filter-alist)
3659 (message "Sticky Agenda buffer, use `r' to refresh")
3660 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3661 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3662 (setq org-todo-keywords-for-agenda nil)
3663 (put 'org-agenda-tag-filter :preset-filter
3664 org-agenda-tag-filter-preset)
3665 (put 'org-agenda-category-filter :preset-filter
3666 org-agenda-category-filter-preset)
3667 (put 'org-agenda-regexp-filter :preset-filter
3668 org-agenda-regexp-filter-preset)
3669 (put 'org-agenda-effort-filter :preset-filter
3670 org-agenda-effort-filter-preset)
3671 (if org-agenda-multi
3672 (progn
3673 (setq buffer-read-only nil)
3674 (goto-char (point-max))
3675 (unless (or (bobp) org-agenda-compact-blocks
3676 (not org-agenda-block-separator))
3677 (insert "\n"
3678 (if (stringp org-agenda-block-separator)
3679 org-agenda-block-separator
3680 (make-string (window-width) org-agenda-block-separator))
3681 "\n"))
3682 (narrow-to-region (point) (point-max)))
3683 (setq org-done-keywords-for-agenda nil)
3684 ;; Setting any org variables that are in org-agenda-local-vars
3685 ;; list need to be done after the prepare call
3686 (org-agenda-prepare-window
3687 (get-buffer-create org-agenda-buffer-name) filter-alist)
3688 (setq buffer-read-only nil)
3689 (org-agenda-reset-markers)
3690 (let ((inhibit-read-only t)) (erase-buffer))
3691 (org-agenda-mode)
3692 (setq org-agenda-buffer (current-buffer))
3693 (setq org-agenda-contributing-files nil)
3694 (setq org-agenda-columns-active nil)
3695 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3696 (setq org-todo-keywords-for-agenda
3697 (org-uniquify org-todo-keywords-for-agenda))
3698 (setq org-done-keywords-for-agenda
3699 (org-uniquify org-done-keywords-for-agenda))
3700 (setq org-agenda-last-prefix-arg current-prefix-arg)
3701 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3702 (and name (not org-agenda-name)
3703 (setq-local org-agenda-name name)))
3704 (setq buffer-read-only nil))))
3706 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3707 (defun org-agenda-finalize ()
3708 "Finishing touch for the agenda buffer, called just before displaying it."
3709 (unless org-agenda-multi
3710 (save-excursion
3711 (let ((inhibit-read-only t))
3712 (goto-char (point-min))
3713 (save-excursion
3714 (while (org-activate-bracket-links (point-max))
3715 (add-text-properties (match-beginning 0) (match-end 0)
3716 '(face org-link))))
3717 (save-excursion
3718 (while (org-activate-plain-links (point-max))
3719 (add-text-properties (match-beginning 0) (match-end 0)
3720 '(face org-link))))
3721 (unless (eq org-agenda-remove-tags t)
3722 (org-agenda-align-tags))
3723 (unless org-agenda-with-colors
3724 (remove-text-properties (point-min) (point-max) '(face nil)))
3725 (if (and (boundp 'org-agenda-overriding-columns-format)
3726 org-agenda-overriding-columns-format)
3727 (setq-local org-agenda-overriding-columns-format
3728 org-agenda-overriding-columns-format))
3729 (if (and (boundp 'org-agenda-view-columns-initially)
3730 org-agenda-view-columns-initially)
3731 (org-agenda-columns))
3732 (when org-agenda-fontify-priorities
3733 (org-agenda-fontify-priorities))
3734 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3735 (org-agenda-dim-blocked-tasks))
3736 (org-agenda-mark-clocking-task)
3737 (when org-agenda-entry-text-mode
3738 (org-agenda-entry-text-hide)
3739 (org-agenda-entry-text-show))
3740 (if (and (functionp 'org-habit-insert-consistency-graphs)
3741 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3742 (org-habit-insert-consistency-graphs))
3743 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3744 (unless (or (eq org-agenda-show-inherited-tags 'always)
3745 (and (listp org-agenda-show-inherited-tags)
3746 (memq org-agenda-type org-agenda-show-inherited-tags))
3747 (and (eq org-agenda-show-inherited-tags t)
3748 (or (eq org-agenda-use-tag-inheritance t)
3749 (and (listp org-agenda-use-tag-inheritance)
3750 (not (memq org-agenda-type
3751 org-agenda-use-tag-inheritance))))))
3752 (let (mrk)
3753 (save-excursion
3754 (goto-char (point-min))
3755 (while (equal (forward-line) 0)
3756 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3757 (put-text-property (point-at-bol) (point-at-eol)
3758 'tags (org-with-point-at mrk
3759 (delete-dups
3760 (mapcar 'downcase (org-get-tags-at))))))))))
3761 (run-hooks 'org-agenda-finalize-hook)
3762 (when org-agenda-top-headline-filter
3763 (org-agenda-filter-top-headline-apply
3764 org-agenda-top-headline-filter))
3765 (when org-agenda-tag-filter
3766 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3767 (when (get 'org-agenda-tag-filter :preset-filter)
3768 (org-agenda-filter-apply
3769 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3770 (when org-agenda-category-filter
3771 (org-agenda-filter-apply org-agenda-category-filter 'category))
3772 (when (get 'org-agenda-category-filter :preset-filter)
3773 (org-agenda-filter-apply
3774 (get 'org-agenda-category-filter :preset-filter) 'category))
3775 (when org-agenda-regexp-filter
3776 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3777 (when (get 'org-agenda-regexp-filter :preset-filter)
3778 (org-agenda-filter-apply
3779 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3780 (when org-agenda-effort-filter
3781 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3782 (when (get 'org-agenda-effort-filter :preset-filter)
3783 (org-agenda-filter-apply
3784 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3785 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3787 (defun org-agenda-mark-clocking-task ()
3788 "Mark the current clock entry in the agenda if it is present."
3789 ;; We need to widen when `org-agenda-finalize' is called from
3790 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3791 (when (bound-and-true-p org-clock-current-task)
3792 (save-restriction
3793 (widen)
3794 (org-agenda-unmark-clocking-task)
3795 (when (marker-buffer org-clock-hd-marker)
3796 (save-excursion
3797 (goto-char (point-min))
3798 (let (s ov)
3799 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3800 (goto-char s)
3801 (when (equal (org-get-at-bol 'org-hd-marker)
3802 org-clock-hd-marker)
3803 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3804 (overlay-put ov 'type 'org-agenda-clocking)
3805 (overlay-put ov 'face 'org-agenda-clocking)
3806 (overlay-put ov 'help-echo
3807 "The clock is running in this item")))))))))
3809 (defun org-agenda-unmark-clocking-task ()
3810 "Unmark the current clocking task."
3811 (mapc (lambda (o)
3812 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3813 (delete-overlay o)))
3814 (overlays-in (point-min) (point-max))))
3816 (defun org-agenda-fontify-priorities ()
3817 "Make highest priority lines bold, and lowest italic."
3818 (interactive)
3819 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3820 (delete-overlay o)))
3821 (overlays-in (point-min) (point-max)))
3822 (save-excursion
3823 (let (b e p ov h l)
3824 (goto-char (point-min))
3825 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3826 (setq h (or (get-char-property (point) 'org-highest-priority)
3827 org-highest-priority)
3828 l (or (get-char-property (point) 'org-lowest-priority)
3829 org-lowest-priority)
3830 p (string-to-char (match-string 1))
3831 b (match-beginning 0)
3832 e (if (eq org-agenda-fontify-priorities 'cookies)
3833 (match-end 0)
3834 (point-at-eol))
3835 ov (make-overlay b e))
3836 (overlay-put
3837 ov 'face
3838 (let ((special-face
3839 (cond ((org-face-from-face-or-color
3840 'priority nil
3841 (cdr (assoc p org-priority-faces))))
3842 ((and (listp org-agenda-fontify-priorities)
3843 (org-face-from-face-or-color
3844 'priority nil
3845 (cdr (assoc p org-agenda-fontify-priorities)))))
3846 ((equal p l) 'italic)
3847 ((equal p h) 'bold))))
3848 (if special-face (list special-face 'org-priority) 'org-priority)))
3849 (overlay-put ov 'org-type 'org-priority)))))
3851 (defvar org-depend-tag-blocked)
3853 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3854 "Dim currently blocked TODOs in the agenda display.
3855 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3856 dimming them."
3857 (interactive "P")
3858 (when (called-interactively-p 'interactive)
3859 (message "Dim or hide blocked tasks..."))
3860 (dolist (o (overlays-in (point-min) (point-max)))
3861 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3862 (delete-overlay o)))
3863 (save-excursion
3864 (let ((inhibit-read-only t)
3865 (org-depend-tag-blocked nil)
3866 org-blocked-by-checkboxes)
3867 (goto-char (point-min))
3868 (while (let ((pos (text-property-not-all
3869 (point) (point-max) 'todo-state nil)))
3870 (when pos (goto-char pos)))
3871 (setq org-blocked-by-checkboxes nil)
3872 (let ((marker (org-get-at-bol 'org-hd-marker)))
3873 (when (and (markerp marker)
3874 (with-current-buffer (marker-buffer marker)
3875 (save-excursion (goto-char marker)
3876 (org-entry-blocked-p))))
3877 ;; Entries blocked by checkboxes cannot be made invisible.
3878 ;; See `org-agenda-dim-blocked-tasks' for details.
3879 (let* ((really-invisible
3880 (and (not org-blocked-by-checkboxes)
3881 (or invisible (eq org-agenda-dim-blocked-tasks
3882 'invisible))))
3883 (ov (make-overlay (if really-invisible (line-end-position 0)
3884 (line-beginning-position))
3885 (line-end-position))))
3886 (if really-invisible (overlay-put ov 'invisible t)
3887 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3888 (overlay-put ov 'org-type 'org-blocked-todo))))
3889 (forward-line))))
3890 (when (called-interactively-p 'interactive)
3891 (message "Dim or hide blocked tasks...done")))
3893 (defvar org-agenda-skip-function nil
3894 "Function to be called at each match during agenda construction.
3895 If this function returns nil, the current match should not be skipped.
3896 Otherwise, the function must return a position from where the search
3897 should be continued.
3898 This may also be a Lisp form, it will be evaluated.
3899 Never set this variable using `setq' or so, because then it will apply
3900 to all future agenda commands. If you do want a global skipping condition,
3901 use the option `org-agenda-skip-function-global' instead.
3902 The correct usage for `org-agenda-skip-function' is to bind it with
3903 `let' to scope it dynamically into the agenda-constructing command.
3904 A good way to set it is through options in `org-agenda-custom-commands'.")
3906 (defun org-agenda-skip ()
3907 "Throw to `:skip' in places that should be skipped.
3908 Also moves point to the end of the skipped region, so that search can
3909 continue from there."
3910 (let ((p (point-at-bol)) to)
3911 (when (or
3912 (save-excursion (goto-char p) (looking-at comment-start-skip))
3913 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3914 (get-text-property p :org-archived)
3915 (org-end-of-subtree t))
3916 (and org-agenda-skip-comment-trees
3917 (get-text-property p :org-comment)
3918 (org-end-of-subtree t))
3919 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3920 (org-agenda-skip-eval org-agenda-skip-function)))
3921 (goto-char to))
3922 (org-in-src-block-p t))
3923 (throw :skip t))))
3925 (defun org-agenda-skip-eval (form)
3926 "If FORM is a function or a list, call (or eval) it and return the result.
3927 `save-excursion' and `save-match-data' are wrapped around the call, so point
3928 and match data are returned to the previous state no matter what these
3929 functions do."
3930 (let (fp)
3931 (and form
3932 (or (setq fp (functionp form))
3933 (consp form))
3934 (save-excursion
3935 (save-match-data
3936 (if fp
3937 (funcall form)
3938 (eval form)))))))
3940 (defvar org-agenda-markers nil
3941 "List of all currently active markers created by `org-agenda'.")
3942 (defvar org-agenda-last-marker-time (float-time)
3943 "Creation time of the last agenda marker.")
3945 (defun org-agenda-new-marker (&optional pos)
3946 "Return a new agenda marker.
3947 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3948 these markers and resets them when they are no longer in use."
3949 (let ((m (copy-marker (or pos (point)) t)))
3950 (setq org-agenda-last-marker-time (float-time))
3951 (if org-agenda-buffer
3952 (with-current-buffer org-agenda-buffer
3953 (push m org-agenda-markers))
3954 (push m org-agenda-markers))
3957 (defun org-agenda-reset-markers ()
3958 "Reset markers created by `org-agenda'."
3959 (while org-agenda-markers
3960 (move-marker (pop org-agenda-markers) nil)))
3962 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3963 "Save relative positions of markers in region.
3964 This check for agenda markers in all agenda buffers currently active."
3965 (dolist (buf (buffer-list))
3966 (with-current-buffer buf
3967 (when (eq major-mode 'org-agenda-mode)
3968 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3969 org-agenda-markers)))))
3971 ;;; Entry text mode
3973 (defun org-agenda-entry-text-show-here ()
3974 "Add some text from the entry as context to the current line."
3975 (let (m txt o)
3976 (setq m (org-get-at-bol 'org-hd-marker))
3977 (unless (marker-buffer m)
3978 (error "No marker points to an entry here"))
3979 (setq txt (concat "\n" (org-no-properties
3980 (org-agenda-get-some-entry-text
3981 m org-agenda-entry-text-maxlines
3982 org-agenda-entry-text-leaders))))
3983 (when (string-match "\\S-" txt)
3984 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3985 (overlay-put o 'evaporate t)
3986 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3987 (overlay-put o 'after-string txt))))
3989 (defun org-agenda-entry-text-show ()
3990 "Add entry context for all agenda lines."
3991 (interactive)
3992 (save-excursion
3993 (goto-char (point-max))
3994 (beginning-of-line 1)
3995 (while (not (bobp))
3996 (when (org-get-at-bol 'org-hd-marker)
3997 (org-agenda-entry-text-show-here))
3998 (beginning-of-line 0))))
4000 (defun org-agenda-entry-text-hide ()
4001 "Remove any shown entry context."
4002 (delq nil
4003 (mapcar (lambda (o)
4004 (if (eq (overlay-get o 'org-overlay-type)
4005 'agenda-entry-content)
4006 (progn (delete-overlay o) t)))
4007 (overlays-in (point-min) (point-max)))))
4009 (defun org-agenda-get-day-face (date)
4010 "Return the face DATE should be displayed with."
4011 (cond ((and (functionp org-agenda-day-face-function)
4012 (funcall org-agenda-day-face-function date)))
4013 ((org-agenda-today-p date) 'org-agenda-date-today)
4014 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4015 'org-agenda-date-weekend)
4016 (t 'org-agenda-date)))
4018 ;;; Agenda timeline
4020 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4021 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4023 (defun org-timeline (&optional dotodo)
4024 "Show a time-sorted view of the entries in the current Org file.
4026 Only entries with a time stamp of today or later will be listed.
4028 With `\\[universal-argument]' prefix, all unfinished TODO items will also be \
4029 shown,
4030 under the current date.
4032 If the buffer contains an active region, only check the region
4033 for dates."
4034 (interactive "P")
4035 (let* ((dopast t)
4036 (org-agenda-show-log-scoped org-agenda-show-log)
4037 (org-agenda-show-log org-agenda-show-log-scoped)
4038 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4039 (current-buffer))))
4040 (date (calendar-current-date))
4041 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4042 (end (if (org-region-active-p) (region-end) (point-max)))
4043 (day-numbers (org-get-all-dates
4044 beg end 'no-ranges
4045 t org-agenda-show-log-scoped ; always include today
4046 org-timeline-show-empty-dates))
4047 (org-deadline-warning-days 0)
4048 (org-agenda-only-exact-dates t)
4049 (today (org-today))
4050 (past t)
4051 args
4052 s e rtn d emptyp)
4053 (setq org-agenda-redo-command
4054 (list 'let
4055 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4056 (list 'org-switch-to-buffer-other-window (current-buffer))
4057 (list 'org-timeline (list 'quote dotodo))))
4058 (put 'org-agenda-redo-command 'org-lprops nil)
4059 (if (not dopast)
4060 ;; Remove past dates from the list of dates.
4061 (setq day-numbers (delq nil (mapcar (lambda(x)
4062 (if (>= x today) x nil))
4063 day-numbers))))
4064 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4065 (org-compile-prefix-format 'timeline)
4066 (org-set-sorting-strategy 'timeline)
4067 (if org-agenda-show-log-scoped (push :closed args))
4068 (push :timestamp args)
4069 (push :deadline args)
4070 (push :scheduled args)
4071 (push :sexp args)
4072 (if dotodo (push :todo args))
4073 (insert "Timeline of file " entry "\n")
4074 (add-text-properties (point-min) (point)
4075 (list 'face 'org-agenda-structure))
4076 (org-agenda-mark-header-line (point-min))
4077 (while (setq d (pop day-numbers))
4078 (if (and (listp d) (eq (car d) :omitted))
4079 (progn
4080 (setq s (point))
4081 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4082 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4083 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4084 (if (and (>= d today)
4085 dopast
4086 past)
4087 (progn
4088 (setq past nil)
4089 (insert (make-string 79 ?-) "\n")))
4090 (setq date (calendar-gregorian-from-absolute d))
4091 (setq s (point))
4092 (setq rtn (and (not emptyp)
4093 (apply 'org-agenda-get-day-entries entry
4094 date args)))
4095 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4096 (progn
4097 (insert
4098 (if (stringp org-agenda-format-date)
4099 (format-time-string org-agenda-format-date
4100 (org-time-from-absolute date))
4101 (funcall org-agenda-format-date date))
4102 "\n")
4103 (put-text-property s (1- (point)) 'face
4104 (org-agenda-get-day-face date))
4105 (put-text-property s (1- (point)) 'org-date-line t)
4106 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4107 (if (equal d today)
4108 (put-text-property s (1- (point)) 'org-today t))
4109 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4110 (put-text-property s (1- (point)) 'day d)))))
4111 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4112 (point-min)))
4113 (add-text-properties
4114 (point-min) (point-max)
4115 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4116 (org-agenda-finalize)
4117 (setq buffer-read-only t)))
4119 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4120 "Return a list of all relevant day numbers from BEG to END buffer positions.
4121 If NO-RANGES is non-nil, include only the start and end dates of a range,
4122 not every single day in the range. If FORCE-TODAY is non-nil, make
4123 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4124 inactive time stamps (those in square brackets) are included.
4125 When EMPTY is non-nil, also include days without any entries."
4126 (let ((re (concat
4127 (if pre-re pre-re "")
4128 (if inactive org-ts-regexp-both org-ts-regexp)))
4129 dates dates1 date day day1 day2 ts1 ts2 pos)
4130 (if force-today
4131 (setq dates (list (org-today))))
4132 (save-excursion
4133 (goto-char beg)
4134 (while (re-search-forward re end t)
4135 (setq day (time-to-days (org-time-string-to-time
4136 (substring (match-string 1) 0 10)
4137 (current-buffer) (match-beginning 0))))
4138 (or (memq day dates) (push day dates)))
4139 (unless no-ranges
4140 (goto-char beg)
4141 (while (re-search-forward org-tr-regexp end t)
4142 (setq pos (match-beginning 0))
4143 (setq ts1 (substring (match-string 1) 0 10)
4144 ts2 (substring (match-string 2) 0 10)
4145 day1 (time-to-days (org-time-string-to-time
4146 ts1 (current-buffer) pos))
4147 day2 (time-to-days (org-time-string-to-time
4148 ts2 (current-buffer) pos)))
4149 (while (< (setq day1 (1+ day1)) day2)
4150 (or (memq day1 dates) (push day1 dates)))))
4151 (setq dates (sort dates '<))
4152 (when empty
4153 (while (setq day (pop dates))
4154 (setq day2 (car dates))
4155 (push day dates1)
4156 (when (and day2 empty)
4157 (if (or (eq empty t)
4158 (and (numberp empty) (<= (- day2 day) empty)))
4159 (while (< (setq day (1+ day)) day2)
4160 (push (list day) dates1))
4161 (push (cons :omitted (- day2 day)) dates1))))
4162 (setq dates (nreverse dates1)))
4163 dates)))
4165 ;;; Agenda Daily/Weekly
4167 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4168 "Start day for the agenda view.
4169 Custom commands can set this variable in the options section.
4170 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4171 input allowed when reading a date through the Org calendar.
4172 See the docstring of `org-read-date' for details.")
4173 (defvar org-starting-day nil) ; local variable in the agenda buffer
4174 (defvar org-arg-loc nil) ; local variable
4176 (defvar org-agenda-buffer-tmp-name nil)
4177 ;;;###autoload
4178 (defun org-agenda-list (&optional arg start-day span with-hour)
4179 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4180 The view will be for the current day or week, but from the overview buffer
4181 you will be able to go to other days/weeks.
4183 With a numeric prefix argument in an interactive call, the agenda will
4184 span ARG days. Lisp programs should instead specify SPAN to change
4185 the number of days. SPAN defaults to `org-agenda-span'.
4187 START-DAY defaults to TODAY, or to the most recent match for the weekday
4188 given in `org-agenda-start-on-weekday'.
4190 When WITH-HOUR is non-nil, only include scheduled and deadline
4191 items if they have an hour specification like [h]h:mm."
4192 (interactive "P")
4193 (if org-agenda-overriding-arguments
4194 (setq arg (car org-agenda-overriding-arguments)
4195 start-day (nth 1 org-agenda-overriding-arguments)
4196 span (nth 2 org-agenda-overriding-arguments)))
4197 (if (and (integerp arg) (> arg 0))
4198 (setq span arg arg nil))
4199 (catch 'exit
4200 (setq org-agenda-buffer-name
4201 (or org-agenda-buffer-tmp-name
4202 (if org-agenda-sticky
4203 (cond ((and org-keys (stringp org-match))
4204 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4205 (org-keys
4206 (format "*Org Agenda(%s)*" org-keys))
4207 (t "*Org Agenda(a)*")))
4208 org-agenda-buffer-name))
4209 (org-agenda-prepare "Day/Week")
4210 (setq start-day (or start-day org-agenda-start-day))
4211 (if (stringp start-day)
4212 ;; Convert to an absolute day number
4213 (setq start-day (time-to-days (org-read-date nil t start-day))))
4214 (org-compile-prefix-format 'agenda)
4215 (org-set-sorting-strategy 'agenda)
4216 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4217 (today (org-today))
4218 (sd (or start-day today))
4219 (ndays (org-agenda-span-to-ndays span sd))
4220 (org-agenda-start-on-weekday
4221 (if (or (eq ndays 7) (eq ndays 14))
4222 org-agenda-start-on-weekday))
4223 (thefiles (org-agenda-files nil 'ifmode))
4224 (files thefiles)
4225 (start (if (or (null org-agenda-start-on-weekday)
4226 (< ndays 7))
4228 (let* ((nt (calendar-day-of-week
4229 (calendar-gregorian-from-absolute sd)))
4230 (n1 org-agenda-start-on-weekday)
4231 (d (- nt n1)))
4232 (- sd (+ (if (< d 0) 7 0) d)))))
4233 (day-numbers (list start))
4234 (day-cnt 0)
4235 (inhibit-redisplay (not debug-on-error))
4236 (org-agenda-show-log-scoped org-agenda-show-log)
4237 s e rtn rtnall file date d start-pos end-pos todayp
4238 clocktable-start clocktable-end filter)
4239 (setq org-agenda-redo-command
4240 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4241 (dotimes (n (1- ndays))
4242 (push (1+ (car day-numbers)) day-numbers))
4243 (setq day-numbers (nreverse day-numbers))
4244 (setq clocktable-start (car day-numbers)
4245 clocktable-end (1+ (or (org-last day-numbers) 0)))
4246 (setq-local org-starting-day (car day-numbers))
4247 (setq-local org-arg-loc arg)
4248 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4249 (unless org-agenda-compact-blocks
4250 (let* ((d1 (car day-numbers))
4251 (d2 (org-last day-numbers))
4252 (w1 (org-days-to-iso-week d1))
4253 (w2 (org-days-to-iso-week d2)))
4254 (setq s (point))
4255 (if org-agenda-overriding-header
4256 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4257 nil 'face 'org-agenda-structure) "\n")
4258 (insert (org-agenda-span-name span)
4259 "-agenda"
4260 (if (< (- d2 d1) 350)
4261 (if (= w1 w2)
4262 (format " (W%02d)" w1)
4263 (format " (W%02d-W%02d)" w1 w2))
4265 ":\n")))
4266 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4267 'org-date-line t))
4268 (org-agenda-mark-header-line s))
4269 (while (setq d (pop day-numbers))
4270 (setq date (calendar-gregorian-from-absolute d)
4271 s (point))
4272 (if (or (setq todayp (= d today))
4273 (and (not start-pos) (= d sd)))
4274 (setq start-pos (point))
4275 (if (and start-pos (not end-pos))
4276 (setq end-pos (point))))
4277 (setq files thefiles
4278 rtnall nil)
4279 (while (setq file (pop files))
4280 (catch 'nextfile
4281 (org-check-agenda-file file)
4282 (let ((org-agenda-entry-types org-agenda-entry-types))
4283 ;; Starred types override non-starred equivalents
4284 (when (member :deadline* org-agenda-entry-types)
4285 (setq org-agenda-entry-types
4286 (delq :deadline org-agenda-entry-types)))
4287 (when (member :scheduled* org-agenda-entry-types)
4288 (setq org-agenda-entry-types
4289 (delq :scheduled org-agenda-entry-types)))
4290 ;; Honor with-hour
4291 (when with-hour
4292 (when (member :deadline org-agenda-entry-types)
4293 (setq org-agenda-entry-types
4294 (delq :deadline org-agenda-entry-types))
4295 (push :deadline* org-agenda-entry-types))
4296 (when (member :scheduled org-agenda-entry-types)
4297 (setq org-agenda-entry-types
4298 (delq :scheduled org-agenda-entry-types))
4299 (push :scheduled* org-agenda-entry-types)))
4300 (unless org-agenda-include-deadlines
4301 (setq org-agenda-entry-types
4302 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4303 (cond
4304 ((memq org-agenda-show-log-scoped '(only clockcheck))
4305 (setq rtn (org-agenda-get-day-entries
4306 file date :closed)))
4307 (org-agenda-show-log-scoped
4308 (setq rtn (apply 'org-agenda-get-day-entries
4309 file date
4310 (append '(:closed) org-agenda-entry-types))))
4312 (setq rtn (apply 'org-agenda-get-day-entries
4313 file date
4314 org-agenda-entry-types)))))
4315 (setq rtnall (append rtnall rtn)))) ;; all entries
4316 (if org-agenda-include-diary
4317 (let ((org-agenda-search-headline-for-time t))
4318 (require 'diary-lib)
4319 (setq rtn (org-get-entries-from-diary date))
4320 (setq rtnall (append rtnall rtn))))
4321 (if (or rtnall org-agenda-show-all-dates)
4322 (progn
4323 (setq day-cnt (1+ day-cnt))
4324 (insert
4325 (if (stringp org-agenda-format-date)
4326 (format-time-string org-agenda-format-date
4327 (org-time-from-absolute date))
4328 (funcall org-agenda-format-date date))
4329 "\n")
4330 (put-text-property s (1- (point)) 'face
4331 (org-agenda-get-day-face date))
4332 (put-text-property s (1- (point)) 'org-date-line t)
4333 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4334 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4335 (when todayp
4336 (put-text-property s (1- (point)) 'org-today t))
4337 (setq rtnall
4338 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4339 (if rtnall (insert ;; all entries
4340 (org-agenda-finalize-entries rtnall 'agenda)
4341 "\n"))
4342 (put-text-property s (1- (point)) 'day d)
4343 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4344 (when (and org-agenda-clockreport-mode clocktable-start)
4345 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4346 ;; the above line is to ensure the restricted range!
4347 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4348 tbl)
4349 (setq p (org-plist-delete p :block))
4350 (setq p (plist-put p :tstart clocktable-start))
4351 (setq p (plist-put p :tend clocktable-end))
4352 (setq p (plist-put p :scope 'agenda))
4353 (setq tbl (apply 'org-clock-get-clocktable p))
4354 (insert tbl)))
4355 (goto-char (point-min))
4356 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4357 (unless (and (pos-visible-in-window-p (point-min))
4358 (pos-visible-in-window-p (point-max)))
4359 (goto-char (1- (point-max)))
4360 (recenter -1)
4361 (if (not (pos-visible-in-window-p (or start-pos 1)))
4362 (progn
4363 (goto-char (or start-pos 1))
4364 (recenter 1))))
4365 (goto-char (or start-pos 1))
4366 (add-text-properties (point-min) (point-max)
4367 `(org-agenda-type agenda
4368 org-last-args (,arg ,start-day ,span)
4369 org-redo-cmd ,org-agenda-redo-command
4370 org-series-cmd ,org-cmd))
4371 (if (eq org-agenda-show-log-scoped 'clockcheck)
4372 (org-agenda-show-clocking-issues))
4373 (org-agenda-finalize)
4374 (setq buffer-read-only t)
4375 (message ""))))
4377 (defun org-agenda-ndays-to-span (n)
4378 "Return a span symbol for a span of N days, or N if none matches."
4379 (cond ((symbolp n) n)
4380 ((= n 1) 'day)
4381 ((= n 7) 'week)
4382 ((= n 14) 'fortnight)
4383 (t n)))
4385 (defun org-agenda-span-to-ndays (span &optional start-day)
4386 "Return ndays from SPAN, possibly starting at START-DAY.
4387 START-DAY is an absolute time value."
4388 (cond ((numberp span) span)
4389 ((eq span 'day) 1)
4390 ((eq span 'week) 7)
4391 ((eq span 'fortnight) 14)
4392 ((eq span 'month)
4393 (let ((date (calendar-gregorian-from-absolute start-day)))
4394 (calendar-last-day-of-month (car date) (cl-caddr date))))
4395 ((eq span 'year)
4396 (let ((date (calendar-gregorian-from-absolute start-day)))
4397 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4399 (defun org-agenda-span-name (span)
4400 "Return a SPAN name."
4401 (if (null span)
4403 (if (symbolp span)
4404 (capitalize (symbol-name span))
4405 (format "%d days" span))))
4407 ;;; Agenda word search
4409 (defvar org-agenda-search-history nil)
4411 (defvar org-search-syntax-table nil
4412 "Special syntax table for Org search.
4413 In this table, we have single quotes not as word constituents, to
4414 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4416 (defvar org-mode-syntax-table) ; From org.el
4417 (defun org-search-syntax-table ()
4418 (unless org-search-syntax-table
4419 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4420 (modify-syntax-entry ?' "." org-search-syntax-table)
4421 (modify-syntax-entry ?` "." org-search-syntax-table))
4422 org-search-syntax-table)
4424 (defvar org-agenda-last-search-view-search-was-boolean nil)
4426 ;;;###autoload
4427 (defun org-search-view (&optional todo-only string edit-at)
4428 "Show all entries that contain a phrase or words or regular expressions.
4430 With optional prefix argument TODO-ONLY, only consider entries that are
4431 TODO entries. The argument STRING can be used to pass a default search
4432 string into this function. If EDIT-AT is non-nil, it means that the
4433 user should get a chance to edit this string, with cursor at position
4434 EDIT-AT.
4436 The search string can be viewed either as a phrase that should be found as
4437 is, or it can be broken into a number of snippets, each of which must match
4438 in a Boolean way to select an entry. The default depends on the variable
4439 `org-agenda-search-view-always-boolean'.
4440 Even if this is turned off (the default) you can always switch to
4441 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4443 The default is a direct search of the whole phrase, where each space in
4444 the search string can expand to an arbitrary amount of whitespace,
4445 including newlines.
4447 If using a Boolean search, the search string is split on whitespace and
4448 each snippet is searched separately, with logical AND to select an entry.
4449 Words prefixed with a minus must *not* occur in the entry. Words without
4450 a prefix or prefixed with a plus must occur in the entry. Matching is
4451 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4452 match whole words, not parts of a word) if
4453 `org-agenda-search-view-force-full-words' is set (default is nil).
4455 Boolean search snippets enclosed by curly braces are interpreted as
4456 regular expressions that must or (when preceded with \"-\") must not
4457 match in the entry. Snippets enclosed into double quotes will be taken
4458 as a whole, to include whitespace.
4460 - If the search string starts with an asterisk, search only in headlines.
4461 - If (possibly after the leading star) the search string starts with an
4462 exclamation mark, this also means to look at TODO entries only, an effect
4463 that can also be achieved with a prefix argument.
4464 - If (possibly after star and exclamation mark) the search string starts
4465 with a colon, this will mean that the (non-regexp) snippets of the
4466 Boolean search must match as full words.
4468 This command searches the agenda files, and in addition the files listed
4469 in `org-agenda-text-search-extra-files'."
4470 (interactive "P")
4471 (if org-agenda-overriding-arguments
4472 (setq todo-only (car org-agenda-overriding-arguments)
4473 string (nth 1 org-agenda-overriding-arguments)
4474 edit-at (nth 2 org-agenda-overriding-arguments)))
4475 (let* ((props (list 'face nil
4476 'done-face 'org-agenda-done
4477 'org-not-done-regexp org-not-done-regexp
4478 'org-todo-regexp org-todo-regexp
4479 'org-complex-heading-regexp org-complex-heading-regexp
4480 'mouse-face 'highlight
4481 'help-echo (format "mouse-2 or RET jump to location")))
4482 (full-words org-agenda-search-view-force-full-words)
4483 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4484 regexp rtn rtnall files file pos inherited-tags
4485 marker category level tags c neg re boolean
4486 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4487 (unless (and (not edit-at)
4488 (stringp string)
4489 (string-match "\\S-" string))
4490 (setq string (read-string
4491 (if org-agenda-search-view-always-boolean
4492 "[+-]Word/{Regexp} ...: "
4493 "Phrase or [+-]Word/{Regexp} ...: ")
4494 (cond
4495 ((integerp edit-at) (cons string edit-at))
4496 (edit-at string))
4497 'org-agenda-search-history)))
4498 (catch 'exit
4499 (if org-agenda-sticky
4500 (setq org-agenda-buffer-name
4501 (if (stringp string)
4502 (format "*Org Agenda(%s:%s)*"
4503 (or org-keys (or (and todo-only "S") "s")) string)
4504 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4505 (org-agenda-prepare "SEARCH")
4506 (org-compile-prefix-format 'search)
4507 (org-set-sorting-strategy 'search)
4508 (setq org-agenda-redo-command
4509 (list 'org-search-view (if todo-only t nil)
4510 (list 'if 'current-prefix-arg nil string)))
4511 (setq org-agenda-query-string string)
4512 (if (equal (string-to-char string) ?*)
4513 (setq hdl-only t
4514 words (substring string 1))
4515 (setq words string))
4516 (when (equal (string-to-char words) ?!)
4517 (setq todo-only t
4518 words (substring words 1)))
4519 (when (equal (string-to-char words) ?:)
4520 (setq full-words t
4521 words (substring words 1)))
4522 (if (or org-agenda-search-view-always-boolean
4523 (member (string-to-char words) '(?- ?+ ?\{)))
4524 (setq boolean t))
4525 (setq words (org-split-string words))
4526 (let (www w)
4527 (while (setq w (pop words))
4528 (while (and (string-match "\\\\\\'" w) words)
4529 (setq w (concat (substring w 0 -1) " " (pop words))))
4530 (push w www))
4531 (setq words (nreverse www) www nil)
4532 (while (setq w (pop words))
4533 (when (and (string-match "\\`[-+]?{" w)
4534 (not (string-match "}\\'" w)))
4535 (while (and words (not (string-match "}\\'" (car words))))
4536 (setq w (concat w " " (pop words))))
4537 (setq w (concat w " " (pop words))))
4538 (push w www))
4539 (setq words (nreverse www)))
4540 (setq org-agenda-last-search-view-search-was-boolean boolean)
4541 (when boolean
4542 (let (wds w)
4543 (while (setq w (pop words))
4544 (if (or (equal (substring w 0 1) "\"")
4545 (and (> (length w) 1)
4546 (member (substring w 0 1) '("+" "-"))
4547 (equal (substring w 1 2) "\"")))
4548 (while (and words (not (equal (substring w -1) "\"")))
4549 (setq w (concat w " " (pop words)))))
4550 (and (string-match "\\`\\([-+]?\\)\"" w)
4551 (setq w (replace-match "\\1" nil nil w)))
4552 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4553 (push w wds))
4554 (setq words (nreverse wds))))
4555 (if boolean
4556 (mapc (lambda (w)
4557 (setq c (string-to-char w))
4558 (if (equal c ?-)
4559 (setq neg t w (substring w 1))
4560 (if (equal c ?+)
4561 (setq neg nil w (substring w 1))
4562 (setq neg nil)))
4563 (if (string-match "\\`{.*}\\'" w)
4564 (setq re (substring w 1 -1))
4565 (if full-words
4566 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4567 (setq re (regexp-quote (downcase w)))))
4568 (if neg (push re regexps-) (push re regexps+)))
4569 words)
4570 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4571 regexps+))
4572 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4573 (if (not regexps+)
4574 (setq regexp org-outline-regexp-bol)
4575 (setq regexp (pop regexps+))
4576 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4577 regexp))))
4578 (setq files (org-agenda-files nil 'ifmode))
4579 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4580 (pop org-agenda-text-search-extra-files)
4581 (setq files (org-add-archive-files files)))
4582 (setq files (append files org-agenda-text-search-extra-files)
4583 rtnall nil)
4584 (while (setq file (pop files))
4585 (setq ee nil)
4586 (catch 'nextfile
4587 (org-check-agenda-file file)
4588 (setq buffer (if (file-exists-p file)
4589 (org-get-agenda-file-buffer file)
4590 (error "No such file %s" file)))
4591 (if (not buffer)
4592 ;; If file does not exist, make sure an error message is sent
4593 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4594 file))))
4595 (with-current-buffer buffer
4596 (with-syntax-table (org-search-syntax-table)
4597 (unless (derived-mode-p 'org-mode)
4598 (error "Agenda file %s is not in `org-mode'" file))
4599 (let ((case-fold-search t))
4600 (save-excursion
4601 (save-restriction
4602 (if (eq buffer org-agenda-restrict)
4603 (narrow-to-region org-agenda-restrict-begin
4604 org-agenda-restrict-end)
4605 (widen))
4606 (goto-char (point-min))
4607 (unless (or (org-at-heading-p)
4608 (outline-next-heading))
4609 (throw 'nextfile t))
4610 (goto-char (max (point-min) (1- (point))))
4611 (while (re-search-forward regexp nil t)
4612 (org-back-to-heading t)
4613 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4614 (> (org-reduced-level (org-outline-level))
4615 org-agenda-search-view-max-outline-level)
4616 (forward-line -1)
4617 (org-back-to-heading t)))
4618 (skip-chars-forward "* ")
4619 (setq beg (point-at-bol)
4620 beg1 (point)
4621 end (progn
4622 (outline-next-heading)
4623 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4624 (> (org-reduced-level (org-outline-level))
4625 org-agenda-search-view-max-outline-level)
4626 (forward-line 1)
4627 (outline-next-heading)))
4628 (point)))
4630 (catch :skip
4631 (goto-char beg)
4632 (org-agenda-skip)
4633 (setq str (buffer-substring-no-properties
4634 (point-at-bol)
4635 (if hdl-only (point-at-eol) end)))
4636 (mapc (lambda (wr) (when (string-match wr str)
4637 (goto-char (1- end))
4638 (throw :skip t)))
4639 regexps-)
4640 (mapc (lambda (wr) (unless (string-match wr str)
4641 (goto-char (1- end))
4642 (throw :skip t)))
4643 (if todo-only
4644 (cons (concat "^\\*+[ \t]+"
4645 org-not-done-regexp)
4646 regexps+)
4647 regexps+))
4648 (goto-char beg)
4649 (setq marker (org-agenda-new-marker (point))
4650 category (org-get-category)
4651 level (make-string (org-reduced-level (org-outline-level)) ? )
4652 inherited-tags
4653 (or (eq org-agenda-show-inherited-tags 'always)
4654 (and (listp org-agenda-show-inherited-tags)
4655 (memq 'todo org-agenda-show-inherited-tags))
4656 (and (eq org-agenda-show-inherited-tags t)
4657 (or (eq org-agenda-use-tag-inheritance t)
4658 (memq 'todo org-agenda-use-tag-inheritance))))
4659 tags (org-get-tags-at nil (not inherited-tags))
4660 txt (org-agenda-format-item
4662 (buffer-substring-no-properties
4663 beg1 (point-at-eol))
4664 level category tags t))
4665 (org-add-props txt props
4666 'org-marker marker 'org-hd-marker marker
4667 'org-todo-regexp org-todo-regexp
4668 'level level
4669 'org-complex-heading-regexp org-complex-heading-regexp
4670 'priority 1000
4671 'type "search")
4672 (push txt ee)
4673 (goto-char (1- end))))))))))
4674 (setq rtn (nreverse ee))
4675 (setq rtnall (append rtnall rtn)))
4676 (if org-agenda-overriding-header
4677 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4678 nil 'face 'org-agenda-structure) "\n")
4679 (insert "Search words: ")
4680 (add-text-properties (point-min) (1- (point))
4681 (list 'face 'org-agenda-structure))
4682 (setq pos (point))
4683 (insert string "\n")
4684 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4685 (setq pos (point))
4686 (unless org-agenda-multi
4687 (insert (substitute-command-keys "\
4688 Press `\\[org-agenda-manipulate-query-add]', \
4689 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4690 `\\[org-agenda-manipulate-query-add-re]', \
4691 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4692 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4693 (add-text-properties pos (1- (point))
4694 (list 'face 'org-agenda-structure))))
4695 (org-agenda-mark-header-line (point-min))
4696 (when rtnall
4697 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4698 (goto-char (point-min))
4699 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4700 (add-text-properties (point-min) (point-max)
4701 `(org-agenda-type search
4702 org-last-args (,todo-only ,string ,edit-at)
4703 org-redo-cmd ,org-agenda-redo-command
4704 org-series-cmd ,org-cmd))
4705 (org-agenda-finalize)
4706 (setq buffer-read-only t))))
4708 ;;; Agenda TODO list
4710 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4711 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4712 (concat
4713 (if (or (equal keywords "ALL") (not keywords))
4714 (propertize "ALL" 'face 'warning)
4715 (mapconcat
4716 (lambda (kw)
4717 (propertize kw 'face (org-get-todo-face kw)))
4718 (org-split-string keywords "|")
4719 "|"))
4720 "\n"))
4722 (defvar org-select-this-todo-keyword nil)
4723 (defvar org-last-arg nil)
4725 ;;;###autoload
4726 (defun org-todo-list (&optional arg)
4727 "Show all (not done) TODO entries from all agenda file in a single list.
4728 The prefix arg can be used to select a specific TODO keyword and limit
4729 the list to these. When using `\\[universal-argument]', you will be prompted
4730 for a keyword. A numeric prefix directly selects the Nth keyword in
4731 `org-todo-keywords-1'."
4732 (interactive "P")
4733 (if org-agenda-overriding-arguments
4734 (setq arg org-agenda-overriding-arguments))
4735 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4736 (let* ((today (org-today))
4737 (date (calendar-gregorian-from-absolute today))
4738 (kwds org-todo-keywords-for-agenda)
4739 (completion-ignore-case t)
4740 (org-select-this-todo-keyword
4741 (if (stringp arg) arg
4742 (and arg (integerp arg) (> arg 0)
4743 (nth (1- arg) kwds))))
4744 rtn rtnall files file pos)
4745 (when (equal arg '(4))
4746 (setq org-select-this-todo-keyword
4747 (completing-read "Keyword (or KWD1|K2D2|...): "
4748 (mapcar #'list kwds) nil nil)))
4749 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4750 (catch 'exit
4751 (if org-agenda-sticky
4752 (setq org-agenda-buffer-name
4753 (if (stringp org-select-this-todo-keyword)
4754 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4755 org-select-this-todo-keyword)
4756 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4757 (org-agenda-prepare "TODO")
4758 (org-compile-prefix-format 'todo)
4759 (org-set-sorting-strategy 'todo)
4760 (setq org-agenda-redo-command
4761 `(org-todo-list (or (and (numberp current-prefix-arg)
4762 current-prefix-arg)
4763 ,org-select-this-todo-keyword
4764 current-prefix-arg ,arg)))
4765 (setq files (org-agenda-files nil 'ifmode)
4766 rtnall nil)
4767 (while (setq file (pop files))
4768 (catch 'nextfile
4769 (org-check-agenda-file file)
4770 (setq rtn (org-agenda-get-day-entries file date :todo))
4771 (setq rtnall (append rtnall rtn))))
4772 (if org-agenda-overriding-header
4773 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4774 nil 'face 'org-agenda-structure) "\n")
4775 (insert "Global list of TODO items of type: ")
4776 (add-text-properties (point-min) (1- (point))
4777 (list 'face 'org-agenda-structure
4778 'short-heading
4779 (concat "ToDo: "
4780 (or org-select-this-todo-keyword "ALL"))))
4781 (org-agenda-mark-header-line (point-min))
4782 (insert (org-agenda-propertize-selected-todo-keywords
4783 org-select-this-todo-keyword))
4784 (setq pos (point))
4785 (unless org-agenda-multi
4786 (insert (substitute-command-keys "Available with \
4787 `N \\[org-agenda-redo]': (0)[ALL]"))
4788 (let ((n 0) s)
4789 (mapc (lambda (x)
4790 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4791 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4792 (insert "\n "))
4793 (insert " " s))
4794 kwds))
4795 (insert "\n"))
4796 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4797 (org-agenda-mark-header-line (point-min))
4798 (when rtnall
4799 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4800 (goto-char (point-min))
4801 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4802 (add-text-properties (point-min) (point-max)
4803 `(org-agenda-type todo
4804 org-last-args ,arg
4805 org-redo-cmd ,org-agenda-redo-command
4806 org-series-cmd ,org-cmd))
4807 (org-agenda-finalize)
4808 (setq buffer-read-only t))))
4810 ;;; Agenda tags match
4812 ;;;###autoload
4813 (defun org-tags-view (&optional todo-only match)
4814 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4815 The prefix arg TODO-ONLY limits the search to TODO entries."
4816 (interactive "P")
4817 (if org-agenda-overriding-arguments
4818 (setq todo-only (car org-agenda-overriding-arguments)
4819 match (nth 1 org-agenda-overriding-arguments)))
4820 (let* ((org-tags-match-list-sublevels
4821 org-tags-match-list-sublevels)
4822 (completion-ignore-case t)
4823 rtn rtnall files file pos matcher
4824 buffer)
4825 (when (and (stringp match) (not (string-match "\\S-" match)))
4826 (setq match nil))
4827 (catch 'exit
4828 (if org-agenda-sticky
4829 (setq org-agenda-buffer-name
4830 (if (stringp match)
4831 (format "*Org Agenda(%s:%s)*"
4832 (or org-keys (or (and todo-only "M") "m")) match)
4833 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4834 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4835 ;; expanding tags within `org-make-tags-matcher'
4836 (org-agenda-prepare (concat "TAGS " match))
4837 (setq org--matcher-tags-todo-only todo-only
4838 matcher (org-make-tags-matcher match)
4839 match (car matcher)
4840 matcher (cdr matcher))
4841 (org-compile-prefix-format 'tags)
4842 (org-set-sorting-strategy 'tags)
4843 (setq org-agenda-query-string match)
4844 (setq org-agenda-redo-command
4845 (list 'org-tags-view
4846 `(quote ,org--matcher-tags-todo-only)
4847 `(if current-prefix-arg nil ,org-agenda-query-string)))
4848 (setq files (org-agenda-files nil 'ifmode)
4849 rtnall nil)
4850 (while (setq file (pop files))
4851 (catch 'nextfile
4852 (org-check-agenda-file file)
4853 (setq buffer (if (file-exists-p file)
4854 (org-get-agenda-file-buffer file)
4855 (error "No such file %s" file)))
4856 (if (not buffer)
4857 ;; If file does not exist, error message to agenda
4858 (setq rtn (list
4859 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4860 rtnall (append rtnall rtn))
4861 (with-current-buffer buffer
4862 (unless (derived-mode-p 'org-mode)
4863 (error "Agenda file %s is not in `org-mode'" file))
4864 (save-excursion
4865 (save-restriction
4866 (if (eq buffer org-agenda-restrict)
4867 (narrow-to-region org-agenda-restrict-begin
4868 org-agenda-restrict-end)
4869 (widen))
4870 (setq rtn (org-scan-tags 'agenda
4871 matcher
4872 org--matcher-tags-todo-only))
4873 (setq rtnall (append rtnall rtn))))))))
4874 (if org-agenda-overriding-header
4875 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4876 nil 'face 'org-agenda-structure) "\n")
4877 (insert "Headlines with TAGS match: ")
4878 (add-text-properties (point-min) (1- (point))
4879 (list 'face 'org-agenda-structure
4880 'short-heading
4881 (concat "Match: " match)))
4882 (setq pos (point))
4883 (insert match "\n")
4884 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4885 (setq pos (point))
4886 (unless org-agenda-multi
4887 (insert (substitute-command-keys
4888 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4889 to search again with new search string\n")))
4890 (add-text-properties pos (1- (point))
4891 (list 'face 'org-agenda-structure)))
4892 (org-agenda-mark-header-line (point-min))
4893 (when rtnall
4894 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4895 (goto-char (point-min))
4896 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4897 (add-text-properties
4898 (point-min) (point-max)
4899 `(org-agenda-type tags
4900 org-last-args (,org--matcher-tags-todo-only ,match)
4901 org-redo-cmd ,org-agenda-redo-command
4902 org-series-cmd ,org-cmd))
4903 (org-agenda-finalize)
4904 (setq buffer-read-only t))))
4906 ;;; Agenda Finding stuck projects
4908 (defvar org-agenda-skip-regexp nil
4909 "Regular expression used in skipping subtrees for the agenda.
4910 This is basically a temporary global variable that can be set and then
4911 used by user-defined selections using `org-agenda-skip-function'.")
4913 (defvar org-agenda-overriding-header nil
4914 "When set during agenda, todo and tags searches it replaces the header.
4915 This variable should not be set directly, but custom commands can bind it
4916 in the options section.")
4918 (defun org-agenda-skip-entry-when-regexp-matches ()
4919 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4920 If yes, it returns the end position of this entry, causing agenda commands
4921 to skip the entry but continuing the search in the subtree. This is a
4922 function that can be put into `org-agenda-skip-function' for the duration
4923 of a command."
4924 (let ((end (save-excursion (org-end-of-subtree t)))
4925 skip)
4926 (save-excursion
4927 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4928 (and skip end)))
4930 (defun org-agenda-skip-subtree-when-regexp-matches ()
4931 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4932 If yes, it returns the end position of this tree, causing agenda commands
4933 to skip this subtree. This is a function that can be put into
4934 `org-agenda-skip-function' for the duration of a command."
4935 (let ((end (save-excursion (org-end-of-subtree t)))
4936 skip)
4937 (save-excursion
4938 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4939 (and skip end)))
4941 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4942 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4943 If yes, it returns the end position of the current entry (NOT the tree),
4944 causing agenda commands to skip the entry but continuing the search in
4945 the subtree. This is a function that can be put into
4946 `org-agenda-skip-function' for the duration of a command. An important
4947 use of this function is for the stuck project list."
4948 (let ((end (save-excursion (org-end-of-subtree t)))
4949 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4950 skip)
4951 (save-excursion
4952 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4953 (and skip entry-end)))
4955 (defun org-agenda-skip-entry-if (&rest conditions)
4956 "Skip entry if any of CONDITIONS is true.
4957 See `org-agenda-skip-if' for details."
4958 (org-agenda-skip-if nil conditions))
4960 (defun org-agenda-skip-subtree-if (&rest conditions)
4961 "Skip subtree if any of CONDITIONS is true.
4962 See `org-agenda-skip-if' for details."
4963 (org-agenda-skip-if t conditions))
4965 (defun org-agenda-skip-if (subtree conditions)
4966 "Checks current entity for CONDITIONS.
4967 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4968 the entry (i.e. the text before the next heading) is checked.
4970 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4971 from different tests. Valid conditions are:
4973 scheduled Check if there is a scheduled cookie
4974 notscheduled Check if there is no scheduled cookie
4975 deadline Check if there is a deadline
4976 notdeadline Check if there is no deadline
4977 timestamp Check if there is a timestamp (also deadline or scheduled)
4978 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4979 regexp Check if regexp matches
4980 notregexp Check if regexp does not match.
4981 todo Check if TODO keyword matches
4982 nottodo Check if TODO keyword does not match
4984 The regexp is taken from the conditions list, it must come right after
4985 the `regexp' or `notregexp' element.
4987 `todo' and `nottodo' accept as an argument a list of todo
4988 keywords, which may include \"*\" to match any todo keyword.
4990 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4992 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4994 Instead of a list, a keyword class may be given. For example:
4996 (org-agenda-skip-entry-if \\='nottodo \\='done)
4998 would skip entries that haven't been marked with any of \"DONE\"
4999 keywords. Possible classes are: `todo', `done', `any'.
5001 If any of these conditions is met, this function returns the end point of
5002 the entity, causing the search to continue from there. This is a function
5003 that can be put into `org-agenda-skip-function' for the duration of a command."
5004 (let (beg end m)
5005 (org-back-to-heading t)
5006 (setq beg (point)
5007 end (if subtree
5008 (progn (org-end-of-subtree t) (point))
5009 (progn (outline-next-heading) (1- (point)))))
5010 (goto-char beg)
5011 (and
5013 (and (memq 'scheduled conditions)
5014 (re-search-forward org-scheduled-time-regexp end t))
5015 (and (memq 'notscheduled conditions)
5016 (not (re-search-forward org-scheduled-time-regexp end t)))
5017 (and (memq 'deadline conditions)
5018 (re-search-forward org-deadline-time-regexp end t))
5019 (and (memq 'notdeadline conditions)
5020 (not (re-search-forward org-deadline-time-regexp end t)))
5021 (and (memq 'timestamp conditions)
5022 (re-search-forward org-ts-regexp end t))
5023 (and (memq 'nottimestamp conditions)
5024 (not (re-search-forward org-ts-regexp end t)))
5025 (and (setq m (memq 'regexp conditions))
5026 (stringp (nth 1 m))
5027 (re-search-forward (nth 1 m) end t))
5028 (and (setq m (memq 'notregexp conditions))
5029 (stringp (nth 1 m))
5030 (not (re-search-forward (nth 1 m) end t)))
5031 (and (or
5032 (setq m (memq 'nottodo conditions))
5033 (setq m (memq 'todo-unblocked conditions))
5034 (setq m (memq 'nottodo-unblocked conditions))
5035 (setq m (memq 'todo conditions)))
5036 (org-agenda-skip-if-todo m end)))
5037 end)))
5039 (defun org-agenda-skip-if-todo (args end)
5040 "Helper function for `org-agenda-skip-if', do not use it directly.
5041 ARGS is a list with first element either `todo', `nottodo',
5042 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5043 a list of TODO keywords, or a state symbol `todo' or `done' or
5044 `any'."
5045 (let ((kw (car args))
5046 (arg (cadr args))
5047 todo-wds todo-re)
5048 (setq todo-wds
5049 (org-uniquify
5050 (cond
5051 ((listp arg) ;; list of keywords
5052 (if (member "*" arg)
5053 (mapcar 'substring-no-properties org-todo-keywords-1)
5054 arg))
5055 ((symbolp arg) ;; keyword class name
5056 (cond
5057 ((eq arg 'todo)
5058 (org-delete-all org-done-keywords
5059 (mapcar 'substring-no-properties
5060 org-todo-keywords-1)))
5061 ((eq arg 'done) org-done-keywords)
5062 ((eq arg 'any)
5063 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5064 (setq todo-re
5065 (concat "^\\*+[ \t]+\\<\\("
5066 (mapconcat 'identity todo-wds "\\|")
5067 "\\)\\>"))
5068 (cond
5069 ((eq kw 'todo) (re-search-forward todo-re end t))
5070 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5071 ((eq kw 'todo-unblocked)
5072 (catch 'unblocked
5073 (while (re-search-forward todo-re end t)
5074 (or (org-entry-blocked-p) (throw 'unblocked t)))
5075 nil))
5076 ((eq kw 'nottodo-unblocked)
5077 (catch 'unblocked
5078 (while (re-search-forward todo-re end t)
5079 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5083 ;;;###autoload
5084 (defun org-agenda-list-stuck-projects (&rest ignore)
5085 "Create agenda view for projects that are stuck.
5086 Stuck projects are project that have no next actions. For the definitions
5087 of what a project is and how to check if it stuck, customize the variable
5088 `org-stuck-projects'."
5089 (interactive)
5090 (let* ((org-agenda-skip-function
5091 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5092 ;; We could have used org-agenda-skip-if here.
5093 (org-agenda-overriding-header
5094 (or org-agenda-overriding-header "List of stuck projects: "))
5095 (matcher (nth 0 org-stuck-projects))
5096 (todo (nth 1 org-stuck-projects))
5097 (todo-wds (if (member "*" todo)
5098 (progn
5099 (org-agenda-prepare-buffers (org-agenda-files
5100 nil 'ifmode))
5101 (org-delete-all
5102 org-done-keywords-for-agenda
5103 (copy-sequence org-todo-keywords-for-agenda)))
5104 todo))
5105 (todo-re (concat "^\\*+[ \t]+\\("
5106 (mapconcat 'identity todo-wds "\\|")
5107 "\\)\\>"))
5108 (tags (nth 2 org-stuck-projects))
5109 (tags-re (if (member "*" tags)
5110 (concat org-outline-regexp-bol
5111 ".*:[[:alnum:]_@#%]+:[ \t]*$")
5112 (if tags
5113 (concat org-outline-regexp-bol
5114 ".*:\\("
5115 (mapconcat #'identity tags "\\|")
5116 "\\):[[:alnum:]_@#%:]*[ \t]*$"))))
5117 (gen-re (nth 3 org-stuck-projects))
5118 (re-list
5119 (delq nil
5120 (list
5121 (if todo todo-re)
5122 (if tags tags-re)
5123 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5124 gen-re)))))
5125 (setq org-agenda-skip-regexp
5126 (if re-list
5127 (mapconcat 'identity re-list "\\|")
5128 (error "No information how to identify unstuck projects")))
5129 (org-tags-view nil matcher)
5130 (setq org-agenda-buffer-name (buffer-name))
5131 (with-current-buffer org-agenda-buffer-name
5132 (setq org-agenda-redo-command
5133 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5135 ;;; Diary integration
5137 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5138 (defvar diary-list-entries-hook)
5139 (defvar diary-time-regexp)
5140 (defun org-get-entries-from-diary (date)
5141 "Get the (Emacs Calendar) diary entries for DATE."
5142 (require 'diary-lib)
5143 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5144 (diary-display-function 'diary-fancy-display)
5145 (pop-up-frames nil)
5146 (diary-list-entries-hook
5147 (cons 'org-diary-default-entry diary-list-entries-hook))
5148 (diary-file-name-prefix nil) ; turn this feature off
5149 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5150 entries
5151 (org-disable-agenda-to-diary t))
5152 (save-excursion
5153 (save-window-excursion
5154 (funcall (if (fboundp 'diary-list-entries)
5155 'diary-list-entries 'list-diary-entries)
5156 date 1)))
5157 (if (not (get-buffer diary-fancy-buffer))
5158 (setq entries nil)
5159 (with-current-buffer diary-fancy-buffer
5160 (setq buffer-read-only nil)
5161 (if (zerop (buffer-size))
5162 ;; No entries
5163 (setq entries nil)
5164 ;; Omit the date and other unnecessary stuff
5165 (org-agenda-cleanup-fancy-diary)
5166 ;; Add prefix to each line and extend the text properties
5167 (if (zerop (buffer-size))
5168 (setq entries nil)
5169 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5170 (setq entries
5171 (with-temp-buffer
5172 (insert entries) (goto-char (point-min))
5173 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5174 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5175 (replace-match (concat "; " (match-string 1)))))
5176 (buffer-string)))))
5177 (set-buffer-modified-p nil)
5178 (kill-buffer diary-fancy-buffer)))
5179 (when entries
5180 (setq entries (org-split-string entries "\n"))
5181 (setq entries
5182 (mapcar
5183 (lambda (x)
5184 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5185 ;; Extend the text properties to the beginning of the line
5186 (org-add-props x (text-properties-at (1- (length x)) x)
5187 'type "diary" 'date date 'face 'org-agenda-diary))
5188 entries)))))
5190 (defvar org-agenda-cleanup-fancy-diary-hook nil
5191 "Hook run when the fancy diary buffer is cleaned up.")
5193 (defun org-agenda-cleanup-fancy-diary ()
5194 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5195 This gets rid of the date, the underline under the date, and
5196 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5197 date. It also removes lines that contain only whitespace."
5198 (goto-char (point-min))
5199 (if (looking-at ".*?:[ \t]*")
5200 (progn
5201 (replace-match "")
5202 (re-search-forward "\n=+$" nil t)
5203 (replace-match "")
5204 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5205 (re-search-forward "\n=+$" nil t)
5206 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5207 (goto-char (point-min))
5208 (while (re-search-forward "^ +\n" nil t)
5209 (replace-match ""))
5210 (goto-char (point-min))
5211 (if (re-search-forward "^Org mode dummy\n?" nil t)
5212 (replace-match ""))
5213 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5215 ;; Make sure entries from the diary have the right text properties.
5216 (eval-after-load "diary-lib"
5217 '(if (boundp 'diary-modify-entry-list-string-function)
5218 ;; We can rely on the hook, nothing to do
5220 ;; Hook not available, must use advice to make this work
5221 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5222 "Make the position visible."
5223 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5224 (stringp string)
5225 buffer-file-name)
5226 (setq string (org-modify-diary-entry-string string))))))
5228 (defun org-modify-diary-entry-string (string)
5229 "Add text properties to string, allowing Org to act on it."
5230 (org-add-props string nil
5231 'mouse-face 'highlight
5232 'help-echo (if buffer-file-name
5233 (format "mouse-2 or RET jump to diary file %s"
5234 (abbreviate-file-name buffer-file-name))
5236 'org-agenda-diary-link t
5237 'org-marker (org-agenda-new-marker (point-at-bol))))
5239 (defun org-diary-default-entry ()
5240 "Add a dummy entry to the diary.
5241 Needed to avoid empty dates which mess up holiday display."
5242 ;; Catch the error if dealing with the new add-to-diary-alist
5243 (when org-disable-agenda-to-diary
5244 (condition-case nil
5245 (org-add-to-diary-list original-date "Org mode dummy" "")
5246 (error
5247 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5249 (defun org-add-to-diary-list (&rest args)
5250 (if (fboundp 'diary-add-to-list)
5251 (apply 'diary-add-to-list args)
5252 (apply 'add-to-diary-list args)))
5254 (defvar org-diary-last-run-time nil)
5256 ;;;###autoload
5257 (defun org-diary (&rest args)
5258 "Return diary information from org files.
5259 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5260 It accesses org files and extracts information from those files to be
5261 listed in the diary. The function accepts arguments specifying what
5262 items should be listed. For a list of arguments allowed here, see the
5263 variable `org-agenda-entry-types'.
5265 The call in the diary file should look like this:
5267 &%%(org-diary) ~/path/to/some/orgfile.org
5269 Use a separate line for each org file to check. Or, if you omit the file name,
5270 all files listed in `org-agenda-files' will be checked automatically:
5272 &%%(org-diary)
5274 If you don't give any arguments (as in the example above), the default value
5275 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5276 So the example above may also be written as
5278 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5280 The function expects the lisp variables `entry' and `date' to be provided
5281 by the caller, because this is how the calendar works. Don't use this
5282 function from a program - use `org-agenda-get-day-entries' instead."
5283 (when (> (- (float-time)
5284 org-agenda-last-marker-time)
5286 ;; I am not sure if this works with sticky agendas, because the marker
5287 ;; list is then no longer a global variable.
5288 (org-agenda-reset-markers))
5289 (org-compile-prefix-format 'agenda)
5290 (org-set-sorting-strategy 'agenda)
5291 (setq args (or args org-agenda-entry-types))
5292 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5293 (list entry)
5294 (org-agenda-files t)))
5295 (time (float-time))
5296 file rtn results)
5297 (when (or (not org-diary-last-run-time)
5298 (> (- time
5299 org-diary-last-run-time)
5301 (org-agenda-prepare-buffers files))
5302 (setq org-diary-last-run-time time)
5303 ;; If this is called during org-agenda, don't return any entries to
5304 ;; the calendar. Org Agenda will list these entries itself.
5305 (if org-disable-agenda-to-diary (setq files nil))
5306 (while (setq file (pop files))
5307 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5308 (setq results (append results rtn)))
5309 (when results
5310 (setq results
5311 (mapcar (lambda (i) (replace-regexp-in-string
5312 org-bracket-link-regexp "\\3" i)) results))
5313 (concat (org-agenda-finalize-entries results) "\n"))))
5315 ;;; Agenda entry finders
5317 (defun org-agenda--timestamp-to-absolute (&rest args)
5318 "Call `org-time-string-to-absolute' with ARGS.
5319 However, throw `:skip' whenever an error is raised."
5320 (condition-case e
5321 (apply #'org-time-string-to-absolute args)
5322 (org-diary-sexp-no-match (throw :skip nil))
5323 (error
5324 (message "%s; Skipping entry" (error-message-string e))
5325 (throw :skip nil))))
5327 (defun org-agenda-get-day-entries (file date &rest args)
5328 "Does the work for `org-diary' and `org-agenda'.
5329 FILE is the path to a file to be checked for entries. DATE is date like
5330 the one returned by `calendar-current-date'. ARGS are symbols indicating
5331 which kind of entries should be extracted. For details about these, see
5332 the documentation of `org-diary'."
5333 (let* ((org-startup-folded nil)
5334 (org-startup-align-all-tables nil)
5335 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5336 (error "No such file %s" file))))
5337 (if (not buffer)
5338 ;; If file does not exist, signal it in diary nonetheless.
5339 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5340 (with-current-buffer buffer
5341 (unless (derived-mode-p 'org-mode)
5342 (error "Agenda file %s is not in `org-mode'" file))
5343 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5344 (setf org-agenda-current-date date)
5345 (save-excursion
5346 (save-restriction
5347 (if (eq buffer org-agenda-restrict)
5348 (narrow-to-region org-agenda-restrict-begin
5349 org-agenda-restrict-end)
5350 (widen))
5351 ;; Rationalize ARGS. Also make sure `:deadline' comes
5352 ;; first in order to populate DEADLINES before passing it.
5354 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5355 ;; guarding us from modifying `org-agenda-entry-types'.
5356 (setf args (org-uniquify (or args org-agenda-entry-types)))
5357 (when (and (memq :scheduled args) (memq :scheduled* args))
5358 (setf args (delq :scheduled* args)))
5359 (cond
5360 ((memq :deadline args)
5361 (setf args (cons :deadline
5362 (delq :deadline (delq :deadline* args)))))
5363 ((memq :deadline* args)
5364 (setf args (cons :deadline* (delq :deadline* args)))))
5365 ;; Collect list of headlines. Return them flattened.
5366 (let ((case-fold-search nil) results deadlines)
5367 (dolist (arg args (apply #'nconc (nreverse results)))
5368 (pcase arg
5369 ((and :todo (guard (org-agenda-today-p date)))
5370 (push (org-agenda-get-todos) results))
5371 (:timestamp
5372 (push (org-agenda-get-blocks) results)
5373 (push (org-agenda-get-timestamps deadlines) results))
5374 (:sexp
5375 (push (org-agenda-get-sexps) results))
5376 (:scheduled
5377 (push (org-agenda-get-scheduled deadlines) results))
5378 (:scheduled*
5379 (push (org-agenda-get-scheduled deadlines t) results))
5380 (:closed
5381 (push (org-agenda-get-progress) results))
5382 (:deadline
5383 (setf deadlines (org-agenda-get-deadlines))
5384 (push deadlines results))
5385 (:deadline*
5386 (setf deadlines (org-agenda-get-deadlines t))
5387 (push deadlines results)))))))))))
5389 (defsubst org-em (x y list)
5390 "Is X or Y a member of LIST?"
5391 (or (memq x list) (memq y list)))
5393 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5394 (defvar org-agenda-sorting-strategy-selected nil)
5396 (defun org-agenda-entry-get-agenda-timestamp (pom)
5397 "Retrieve timestamp information for sorting agenda views.
5398 Given a point or marker POM, returns a cons cell of the timestamp
5399 and the timestamp type relevant for the sorting strategy in
5400 `org-agenda-sorting-strategy-selected'."
5401 (let (ts ts-date-type)
5402 (save-match-data
5403 (cond ((org-em 'scheduled-up 'scheduled-down
5404 org-agenda-sorting-strategy-selected)
5405 (setq ts (org-entry-get pom "SCHEDULED")
5406 ts-date-type " scheduled"))
5407 ((org-em 'deadline-up 'deadline-down
5408 org-agenda-sorting-strategy-selected)
5409 (setq ts (org-entry-get pom "DEADLINE")
5410 ts-date-type " deadline"))
5411 ((org-em 'ts-up 'ts-down
5412 org-agenda-sorting-strategy-selected)
5413 (setq ts (org-entry-get pom "TIMESTAMP")
5414 ts-date-type " timestamp"))
5415 ((org-em 'tsia-up 'tsia-down
5416 org-agenda-sorting-strategy-selected)
5417 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5418 ts-date-type " timestamp_ia"))
5419 ((org-em 'timestamp-up 'timestamp-down
5420 org-agenda-sorting-strategy-selected)
5421 (setq ts (or (org-entry-get pom "SCHEDULED")
5422 (org-entry-get pom "DEADLINE")
5423 (org-entry-get pom "TIMESTAMP")
5424 (org-entry-get pom "TIMESTAMP_IA"))
5425 ts-date-type ""))
5426 (t (setq ts-date-type "")))
5427 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5428 ts-date-type))))
5430 (defun org-agenda-get-todos ()
5431 "Return the TODO information for agenda display."
5432 (let* ((props (list 'face nil
5433 'done-face 'org-agenda-done
5434 'org-not-done-regexp org-not-done-regexp
5435 'org-todo-regexp org-todo-regexp
5436 'org-complex-heading-regexp org-complex-heading-regexp
5437 'mouse-face 'highlight
5438 'help-echo
5439 (format "mouse-2 or RET jump to org file %s"
5440 (abbreviate-file-name buffer-file-name))))
5441 (regexp (format org-heading-keyword-regexp-format
5442 (cond
5443 ((and org-select-this-todo-keyword
5444 (equal org-select-this-todo-keyword "*"))
5445 org-todo-regexp)
5446 (org-select-this-todo-keyword
5447 (concat "\\("
5448 (mapconcat 'identity
5449 (org-split-string
5450 org-select-this-todo-keyword
5451 "|")
5452 "\\|") "\\)"))
5453 (t org-not-done-regexp))))
5454 marker priority category level tags todo-state
5455 ts-date ts-date-type ts-date-pair
5456 ee txt beg end inherited-tags todo-state-end-pos)
5457 (goto-char (point-min))
5458 (while (re-search-forward regexp nil t)
5459 (catch :skip
5460 (save-match-data
5461 (beginning-of-line)
5462 (org-agenda-skip)
5463 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5464 (unless (and (setq todo-state (org-get-todo-state))
5465 (setq todo-state-end-pos (match-end 2)))
5466 (goto-char end)
5467 (throw :skip nil))
5468 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5469 (goto-char (1+ beg))
5470 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5471 (throw :skip nil)))
5472 (goto-char (match-beginning 2))
5473 (setq marker (org-agenda-new-marker (match-beginning 0))
5474 category (org-get-category)
5475 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5476 ts-date (car ts-date-pair)
5477 ts-date-type (cdr ts-date-pair)
5478 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5479 inherited-tags
5480 (or (eq org-agenda-show-inherited-tags 'always)
5481 (and (listp org-agenda-show-inherited-tags)
5482 (memq 'todo org-agenda-show-inherited-tags))
5483 (and (eq org-agenda-show-inherited-tags t)
5484 (or (eq org-agenda-use-tag-inheritance t)
5485 (memq 'todo org-agenda-use-tag-inheritance))))
5486 tags (org-get-tags-at nil (not inherited-tags))
5487 level (make-string (org-reduced-level (org-outline-level)) ? )
5488 txt (org-agenda-format-item "" txt level category tags t)
5489 priority (1+ (org-get-priority txt)))
5490 (org-add-props txt props
5491 'org-marker marker 'org-hd-marker marker
5492 'priority priority
5493 'level level
5494 'ts-date ts-date
5495 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5496 (push txt ee)
5497 (if org-agenda-todo-list-sublevels
5498 (goto-char todo-state-end-pos)
5499 (org-end-of-subtree 'invisible))))
5500 (nreverse ee)))
5502 (defun org-agenda-todo-custom-ignore-p (time n)
5503 "Check whether timestamp is farther away than n number of days.
5504 This function is invoked if `org-agenda-todo-ignore-deadlines',
5505 `org-agenda-todo-ignore-scheduled' or
5506 `org-agenda-todo-ignore-timestamp' is set to an integer."
5507 (let ((days (org-time-stamp-to-now
5508 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5509 (if (>= n 0)
5510 (>= days n)
5511 (<= days n))))
5513 ;;;###autoload
5514 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5515 (&optional end)
5516 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5517 (when (or org-agenda-todo-ignore-with-date
5518 org-agenda-todo-ignore-scheduled
5519 org-agenda-todo-ignore-deadlines
5520 org-agenda-todo-ignore-timestamp)
5521 (setq end (or end (save-excursion (outline-next-heading) (point))))
5522 (save-excursion
5523 (or (and org-agenda-todo-ignore-with-date
5524 (re-search-forward org-ts-regexp end t))
5525 (and org-agenda-todo-ignore-scheduled
5526 (re-search-forward org-scheduled-time-regexp end t)
5527 (cond
5528 ((eq org-agenda-todo-ignore-scheduled 'future)
5529 (> (org-time-stamp-to-now
5530 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5531 ((eq org-agenda-todo-ignore-scheduled 'past)
5532 (<= (org-time-stamp-to-now
5533 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5534 ((numberp org-agenda-todo-ignore-scheduled)
5535 (org-agenda-todo-custom-ignore-p
5536 (match-string 1) org-agenda-todo-ignore-scheduled))
5537 (t)))
5538 (and org-agenda-todo-ignore-deadlines
5539 (re-search-forward org-deadline-time-regexp end t)
5540 (cond
5541 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5542 ((eq org-agenda-todo-ignore-deadlines 'far)
5543 (not (org-deadline-close-p (match-string 1))))
5544 ((eq org-agenda-todo-ignore-deadlines 'future)
5545 (> (org-time-stamp-to-now
5546 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5547 ((eq org-agenda-todo-ignore-deadlines 'past)
5548 (<= (org-time-stamp-to-now
5549 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5550 ((numberp org-agenda-todo-ignore-deadlines)
5551 (org-agenda-todo-custom-ignore-p
5552 (match-string 1) org-agenda-todo-ignore-deadlines))
5553 (t (org-deadline-close-p (match-string 1)))))
5554 (and org-agenda-todo-ignore-timestamp
5555 (let ((buffer (current-buffer))
5556 (regexp
5557 (concat
5558 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5559 (start (point)))
5560 ;; Copy current buffer into a temporary one
5561 (with-temp-buffer
5562 (insert-buffer-substring buffer start end)
5563 (goto-char (point-min))
5564 ;; Delete SCHEDULED and DEADLINE items
5565 (while (re-search-forward regexp end t)
5566 (delete-region (match-beginning 0) (match-end 0)))
5567 (goto-char (point-min))
5568 ;; No search for timestamp left
5569 (when (re-search-forward org-ts-regexp nil t)
5570 (cond
5571 ((eq org-agenda-todo-ignore-timestamp 'future)
5572 (> (org-time-stamp-to-now
5573 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5574 ((eq org-agenda-todo-ignore-timestamp 'past)
5575 (<= (org-time-stamp-to-now
5576 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5577 ((numberp org-agenda-todo-ignore-timestamp)
5578 (org-agenda-todo-custom-ignore-p
5579 (match-string 1) org-agenda-todo-ignore-timestamp))
5580 (t))))))))))
5582 (defun org-agenda-get-timestamps (&optional deadline-results)
5583 "Return the date stamp information for agenda display."
5584 (let* ((props (list 'face 'org-agenda-calendar-event
5585 'org-not-done-regexp org-not-done-regexp
5586 'org-todo-regexp org-todo-regexp
5587 'org-complex-heading-regexp org-complex-heading-regexp
5588 'mouse-face 'highlight
5589 'help-echo
5590 (format "mouse-2 or RET jump to org file %s"
5591 (abbreviate-file-name buffer-file-name))))
5592 (d1 (calendar-absolute-from-gregorian date))
5594 (deadline-position-alist
5595 (mapcar (lambda (a) (and (setq mm (get-text-property
5596 0 'org-hd-marker a))
5597 (cons (marker-position mm) a)))
5598 deadline-results))
5599 (remove-re org-ts-regexp)
5600 (regexp
5601 (concat
5602 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5603 (regexp-quote
5604 (substring
5605 (format-time-string
5606 (car org-time-stamp-formats)
5607 (apply 'encode-time ; DATE bound by calendar
5608 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5609 1 11))
5610 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5611 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5612 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5613 donep tmp priority category level ee txt timestr tags
5614 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5615 inherited-tags ts-date)
5616 (goto-char (point-min))
5617 (while (setq end-of-match (re-search-forward regexp nil t))
5618 (setq b0 (match-beginning 0)
5619 b3 (match-beginning 3) e3 (match-end 3)
5620 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5621 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5622 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5623 (member todo-state
5624 org-agenda-repeating-timestamp-show-all)))
5625 (catch :skip
5626 (and (org-at-date-range-p) (throw :skip nil))
5627 (org-agenda-skip)
5628 (if (and (match-end 1)
5629 (not (= d1 (org-agenda--timestamp-to-absolute
5630 (match-string 1) d1 nil show-all
5631 (current-buffer) b0))))
5632 (throw :skip nil))
5633 (if (and e3
5634 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5635 (throw :skip nil))
5636 (setq tmp (buffer-substring (max (point-min)
5637 (- b0 org-ds-keyword-length))
5639 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5640 inactivep (= (char-after b0) ?\[)
5641 deadlinep (string-match org-deadline-regexp tmp)
5642 scheduledp (string-match org-scheduled-regexp tmp)
5643 closedp (and org-agenda-include-inactive-timestamps
5644 (string-match org-closed-string tmp))
5645 clockp (and org-agenda-include-inactive-timestamps
5646 (or (string-match org-clock-string tmp)
5647 (string-match "]-+\\'" tmp)))
5648 warntime (get-text-property (point) 'org-appt-warntime)
5649 donep (member todo-state org-done-keywords))
5650 (if (or scheduledp deadlinep closedp clockp
5651 (and donep org-agenda-skip-timestamp-if-done))
5652 (throw :skip t))
5653 (if (string-match ">" timestr)
5654 ;; substring should only run to end of time stamp
5655 (setq timestr (substring timestr 0 (match-end 0))))
5656 (setq marker (org-agenda-new-marker b0)
5657 category (org-get-category b0))
5658 (save-excursion
5659 (if (not (re-search-backward org-outline-regexp-bol nil t))
5660 (throw :skip nil)
5661 (goto-char (match-beginning 0))
5662 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5663 (assoc (point) deadline-position-alist))
5664 (throw :skip nil))
5665 (setq hdmarker (org-agenda-new-marker)
5666 inherited-tags
5667 (or (eq org-agenda-show-inherited-tags 'always)
5668 (and (listp org-agenda-show-inherited-tags)
5669 (memq 'agenda org-agenda-show-inherited-tags))
5670 (and (eq org-agenda-show-inherited-tags t)
5671 (or (eq org-agenda-use-tag-inheritance t)
5672 (memq 'agenda org-agenda-use-tag-inheritance))))
5673 tags (org-get-tags-at nil (not inherited-tags))
5674 level (make-string (org-reduced-level (org-outline-level)) ? ))
5675 (looking-at "\\*+[ \t]+\\(.*\\)")
5676 (setq head (match-string 1))
5677 (setq txt (org-agenda-format-item
5678 (if inactivep org-agenda-inactive-leader nil)
5679 head level category tags timestr
5680 remove-re habitp)))
5681 (setq priority (org-get-priority txt))
5682 (org-add-props txt props 'priority priority
5683 'org-marker marker 'org-hd-marker hdmarker
5684 'date date
5685 'level level
5686 'ts-date
5687 (ignore-errors (org-time-string-to-absolute timestr))
5688 'todo-state todo-state
5689 'warntime warntime
5690 'type "timestamp")
5691 (push txt ee))
5692 (if org-agenda-skip-additional-timestamps-same-entry
5693 (outline-next-heading)
5694 (goto-char end-of-match))))
5695 (nreverse ee)))
5697 (defun org-agenda-get-sexps ()
5698 "Return the sexp information for agenda display."
5699 (require 'diary-lib)
5700 (let* ((props (list 'face 'org-agenda-calendar-sexp
5701 'mouse-face 'highlight
5702 'help-echo
5703 (format "mouse-2 or RET jump to org file %s"
5704 (abbreviate-file-name buffer-file-name))))
5705 (regexp "^&?%%(")
5706 marker category extra level ee txt tags entry
5707 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5708 (goto-char (point-min))
5709 (while (re-search-forward regexp nil t)
5710 (catch :skip
5711 (org-agenda-skip)
5712 (setq beg (match-beginning 0))
5713 (goto-char (1- (match-end 0)))
5714 (setq b (point))
5715 (forward-sexp 1)
5716 (setq sexp (buffer-substring b (point)))
5717 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5718 (org-trim (match-string 1))
5719 ""))
5720 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5721 (when result
5722 (setq marker (org-agenda-new-marker beg)
5723 level (make-string (org-reduced-level (org-outline-level)) ? )
5724 category (org-get-category beg)
5725 inherited-tags
5726 (or (eq org-agenda-show-inherited-tags 'always)
5727 (and (listp org-agenda-show-inherited-tags)
5728 (memq 'agenda org-agenda-show-inherited-tags))
5729 (and (eq org-agenda-show-inherited-tags t)
5730 (or (eq org-agenda-use-tag-inheritance t)
5731 (memq 'agenda org-agenda-use-tag-inheritance))))
5732 tags (org-get-tags-at nil (not inherited-tags))
5733 todo-state (org-get-todo-state)
5734 warntime (get-text-property (point) 'org-appt-warntime)
5735 extra nil)
5737 (dolist (r (if (stringp result)
5738 (list result)
5739 result)) ;; we expect a list here
5740 (when (and org-agenda-diary-sexp-prefix
5741 (string-match org-agenda-diary-sexp-prefix r))
5742 (setq extra (match-string 0 r)
5743 r (replace-match "" nil nil r)))
5744 (if (string-match "\\S-" r)
5745 (setq txt r)
5746 (setq txt "SEXP entry returned empty string"))
5747 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5748 (org-add-props txt props 'org-marker marker
5749 'date date 'todo-state todo-state
5750 'level level 'type "sexp" 'warntime warntime)
5751 (push txt ee)))))
5752 (nreverse ee)))
5754 ;; Calendar sanity: define some functions that are independent of
5755 ;; `calendar-date-style'.
5756 (defun org-anniversary (year month day &optional mark)
5757 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5758 (with-no-warnings
5759 (let ((calendar-date-style 'iso))
5760 (diary-anniversary year month day mark))))
5761 (defun org-cyclic (N year month day &optional mark)
5762 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5763 (with-no-warnings
5764 (let ((calendar-date-style 'iso))
5765 (diary-cyclic N year month day mark))))
5766 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5767 "Like `diary-block', but with fixed (ISO) order of arguments."
5768 (with-no-warnings
5769 (let ((calendar-date-style 'iso))
5770 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5771 (defun org-date (year month day &optional mark)
5772 "Like `diary-date', but with fixed (ISO) order of arguments."
5773 (with-no-warnings
5774 (let ((calendar-date-style 'iso))
5775 (diary-date year month day mark))))
5777 ;; Define the `org-class' function
5778 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5779 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5780 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5781 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5782 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5783 `holidays', then any date that is known by the Emacs calendar to be a
5784 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5785 then those holidays will be skipped."
5786 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5787 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5788 (d (calendar-absolute-from-gregorian date))
5789 (h (when skip-weeks (calendar-check-holidays date))))
5790 (and
5791 (<= date1 d)
5792 (<= d date2)
5793 (= (calendar-day-of-week date) dayname)
5794 (or (not skip-weeks)
5795 (progn
5796 (require 'cal-iso)
5797 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5798 (not (or (and h (memq 'holidays skip-weeks))
5799 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5800 entry)))
5802 (defalias 'org-get-closed 'org-agenda-get-progress)
5803 (defun org-agenda-get-progress ()
5804 "Return the logged TODO entries for agenda display."
5805 (let* ((props (list 'mouse-face 'highlight
5806 'org-not-done-regexp org-not-done-regexp
5807 'org-todo-regexp org-todo-regexp
5808 'org-complex-heading-regexp org-complex-heading-regexp
5809 'help-echo
5810 (format "mouse-2 or RET jump to org file %s"
5811 (abbreviate-file-name buffer-file-name))))
5812 (items (if (consp org-agenda-show-log-scoped)
5813 org-agenda-show-log-scoped
5814 (if (eq org-agenda-show-log-scoped 'clockcheck)
5815 '(clock)
5816 org-agenda-log-mode-items)))
5817 (parts
5818 (delq nil
5819 (list
5820 (if (memq 'closed items) (concat "\\<" org-closed-string))
5821 (if (memq 'clock items) (concat "\\<" org-clock-string))
5822 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5823 (parts-re (if parts (mapconcat 'identity parts "\\|")
5824 (error "`org-agenda-log-mode-items' is empty")))
5825 (regexp (concat
5826 "\\(" parts-re "\\)"
5827 " *\\["
5828 (regexp-quote
5829 (substring
5830 (format-time-string
5831 (car org-time-stamp-formats)
5832 (apply 'encode-time ; DATE bound by calendar
5833 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5834 1 11))))
5835 (org-agenda-search-headline-for-time nil)
5836 marker hdmarker priority category level tags closedp
5837 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5838 (goto-char (point-min))
5839 (while (re-search-forward regexp nil t)
5840 (catch :skip
5841 (org-agenda-skip)
5842 (setq marker (org-agenda-new-marker (match-beginning 0))
5843 closedp (equal (match-string 1) org-closed-string)
5844 statep (equal (string-to-char (match-string 1)) ?-)
5845 clockp (not (or closedp statep))
5846 state (and statep (match-string 2))
5847 category (org-get-category (match-beginning 0))
5848 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5849 (when (string-match "\\]" timestr)
5850 ;; substring should only run to end of time stamp
5851 (setq rest (substring timestr (match-end 0))
5852 timestr (substring timestr 0 (match-end 0)))
5853 (if (and (not closedp) (not statep)
5854 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5855 rest))
5856 (progn (setq timestr (concat (substring timestr 0 -1)
5857 "-" (match-string 1 rest) "]"))
5858 (setq clocked (match-string 2 rest)))
5859 (setq clocked "-")))
5860 (save-excursion
5861 (setq extra
5862 (cond
5863 ((not org-agenda-log-mode-add-notes) nil)
5864 (statep
5865 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5866 (match-string 1)))
5867 (clockp
5868 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5869 (match-string 1)))))
5870 (if (not (re-search-backward org-outline-regexp-bol nil t))
5871 (throw :skip nil)
5872 (goto-char (match-beginning 0))
5873 (setq hdmarker (org-agenda-new-marker)
5874 inherited-tags
5875 (or (eq org-agenda-show-inherited-tags 'always)
5876 (and (listp org-agenda-show-inherited-tags)
5877 (memq 'todo org-agenda-show-inherited-tags))
5878 (and (eq org-agenda-show-inherited-tags t)
5879 (or (eq org-agenda-use-tag-inheritance t)
5880 (memq 'todo org-agenda-use-tag-inheritance))))
5881 tags (org-get-tags-at nil (not inherited-tags))
5882 level (make-string (org-reduced-level (org-outline-level)) ? ))
5883 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5884 (setq txt (match-string 1))
5885 (when extra
5886 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5887 (setq txt (concat (substring txt 0 (match-beginning 1))
5888 " - " extra " " (match-string 2 txt)))
5889 (setq txt (concat txt " - " extra))))
5890 (setq txt (org-agenda-format-item
5891 (cond
5892 (closedp "Closed: ")
5893 (statep (concat "State: (" state ")"))
5894 (t (concat "Clocked: (" clocked ")")))
5895 txt level category tags timestr)))
5896 (setq priority 100000)
5897 (org-add-props txt props
5898 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5899 'priority priority 'level level
5900 'type "closed" 'date date
5901 'undone-face 'org-warning 'done-face 'org-agenda-done)
5902 (push txt ee))
5903 (goto-char (point-at-eol))))
5904 (nreverse ee)))
5906 (defun org-agenda-show-clocking-issues ()
5907 "Add overlays, showing issues with clocking.
5908 See also the user option `org-agenda-clock-consistency-checks'."
5909 (interactive)
5910 (let* ((org-time-clocksum-use-effort-durations nil)
5911 (pl org-agenda-clock-consistency-checks)
5912 (re (concat "^[ \t]*"
5913 org-clock-string
5914 "[ \t]+"
5915 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5916 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5917 (tlstart 0.)
5918 (tlend 0.)
5919 (maxtime (org-hh:mm-string-to-minutes
5920 (or (plist-get pl :max-duration) "24:00")))
5921 (mintime (org-hh:mm-string-to-minutes
5922 (or (plist-get pl :min-duration) 0)))
5923 (maxgap (org-hh:mm-string-to-minutes
5924 ;; default 30:00 means never complain
5925 (or (plist-get pl :max-gap) "30:00")))
5926 (gapok (mapcar 'org-hh:mm-string-to-minutes
5927 (plist-get pl :gap-ok-around)))
5928 (def-face (or (plist-get pl :default-face)
5929 '((:background "DarkRed") (:foreground "white"))))
5930 issue face m te ts dt ov)
5931 (goto-char (point-min))
5932 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5933 (setq issue nil face def-face)
5934 (catch 'next
5935 (setq m (org-get-at-bol 'org-marker)
5936 te nil ts nil)
5937 (unless (and m (markerp m))
5938 (setq issue "No valid clock line") (throw 'next t))
5939 (org-with-point-at m
5940 (save-excursion
5941 (goto-char (point-at-bol))
5942 (unless (looking-at re)
5943 (error "No valid Clock line")
5944 (throw 'next t))
5945 (unless (match-end 3)
5946 (setq issue "No end time"
5947 face (or (plist-get pl :no-end-time-face) face))
5948 (throw 'next t))
5949 (setq ts (match-string 1)
5950 te (match-string 3)
5951 ts (float-time
5952 (apply #'encode-time (org-parse-time-string ts)))
5953 te (float-time
5954 (apply #'encode-time (org-parse-time-string te)))
5955 dt (- te ts))))
5956 (cond
5957 ((> dt (* 60 maxtime))
5958 ;; a very long clocking chunk
5959 (setq issue (format "Clocking interval is very long: %s"
5960 (org-minutes-to-clocksum-string
5961 (floor (/ (float dt) 60.))))
5962 face (or (plist-get pl :long-face) face)))
5963 ((< dt (* 60 mintime))
5964 ;; a very short clocking chunk
5965 (setq issue (format "Clocking interval is very short: %s"
5966 (org-minutes-to-clocksum-string
5967 (floor (/ (float dt) 60.))))
5968 face (or (plist-get pl :short-face) face)))
5969 ((and (> tlend 0) (< ts tlend))
5970 ;; Two clock entries are overlapping
5971 (setq issue (format "Clocking overlap: %d minutes"
5972 (/ (- tlend ts) 60))
5973 face (or (plist-get pl :overlap-face) face)))
5974 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5975 ;; There is a gap, lets see if we need to report it
5976 (unless (org-agenda-check-clock-gap tlend ts gapok)
5977 (setq issue (format "Clocking gap: %d minutes"
5978 (/ (- ts tlend) 60))
5979 face (or (plist-get pl :gap-face) face))))
5980 (t nil)))
5981 (setq tlend (or te tlend) tlstart (or ts tlstart))
5982 (when issue
5983 ;; OK, there was some issue, add an overlay to show the issue
5984 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5985 (overlay-put ov 'before-string
5986 (concat
5987 (org-add-props
5988 (format "%-43s" (concat " " issue))
5990 'face face)
5991 "\n"))
5992 (overlay-put ov 'evaporate t)))))
5994 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5995 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5996 (catch 'exit
5997 (unless ok-list
5998 ;; there are no OK times for gaps...
5999 (throw 'exit nil))
6000 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
6001 ;; This is more than 24 hours, so it is OK.
6002 ;; because we have at least one OK time, that must be in the
6003 ;; 24 hour interval.
6004 (throw 'exit t))
6005 ;; We have a shorter gap.
6006 ;; Now we have to get the minute of the day when these times are
6007 (let* ((t1dec (decode-time (seconds-to-time t1)))
6008 (t2dec (decode-time (seconds-to-time t2)))
6009 ;; compute the minute on the day
6010 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6011 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6012 (when (< min2 min1)
6013 ;; if min2 is smaller than min1, this means it is on the next day.
6014 ;; Wrap it to after midnight.
6015 (setq min2 (+ min2 1440)))
6016 ;; Now check if any of the OK times is in the gap
6017 (mapc (lambda (x)
6018 ;; Wrap the time to after midnight if necessary
6019 (if (< x min1) (setq x (+ x 1440)))
6020 ;; Check if in interval
6021 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6022 ok-list)
6023 ;; Nope, this gap is not OK
6024 nil)))
6026 (defun org-agenda-get-deadlines (&optional with-hour)
6027 "Return the deadline information for agenda display.
6028 When WITH-HOUR is non-nil, only return deadlines with an hour
6029 specification like [h]h:mm."
6030 (let* ((props (list 'mouse-face 'highlight
6031 'org-not-done-regexp org-not-done-regexp
6032 'org-todo-regexp org-todo-regexp
6033 'org-complex-heading-regexp org-complex-heading-regexp
6034 'help-echo
6035 (format "mouse-2 or RET jump to org file %s"
6036 (abbreviate-file-name buffer-file-name))))
6037 (regexp (if with-hour
6038 org-deadline-time-hour-regexp
6039 org-deadline-time-regexp))
6040 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6041 (current (calendar-absolute-from-gregorian date))
6042 deadline-items)
6043 (goto-char (point-min))
6044 (while (re-search-forward regexp nil t)
6045 (catch :skip
6046 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6047 (org-agenda-skip)
6048 (let* ((s (match-string 1))
6049 (pos (1- (match-beginning 1)))
6050 (todo-state (save-match-data (org-get-todo-state)))
6051 (donep (member todo-state org-done-keywords))
6052 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6053 (member todo-state
6054 org-agenda-repeating-timestamp-show-all)))
6055 ;; DEADLINE is the current scheduled date. When it
6056 ;; contains a repeater and SHOW-ALL is non-nil,
6057 ;; LAST-REPEAT is the repeat closest to CURRENT.
6058 ;; Otherwise, LAST-REPEAT is equal to DEADLINE.
6059 (last-repeat (org-agenda--timestamp-to-absolute
6060 s current 'past show-all (current-buffer) pos))
6061 (deadline (org-agenda--timestamp-to-absolute s current))
6062 (diff (- last-repeat current))
6063 (suppress-prewarning
6064 (let ((scheduled
6065 (and org-agenda-skip-deadline-prewarning-if-scheduled
6066 (org-entry-get nil "SCHEDULED"))))
6067 (cond
6068 ((not scheduled) nil)
6069 ;; The current item has a scheduled date, so
6070 ;; evaluate its prewarning lead time.
6071 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6072 ;; Use global prewarning-restart lead time.
6073 org-agenda-skip-deadline-prewarning-if-scheduled)
6074 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6075 'pre-scheduled)
6076 ;; Set pre-warning to no earlier than SCHEDULED.
6077 (min (- last-repeat
6078 (org-agenda--timestamp-to-absolute
6079 scheduled current 'past show-all
6080 (current-buffer)
6081 (save-excursion
6082 (beginning-of-line)
6083 (1+ (search-forward org-deadline-string)))))
6084 org-deadline-warning-days))
6085 ;; Set pre-warning to deadline.
6086 (t 0))))
6087 (wdays (if suppress-prewarning
6088 (let ((org-deadline-warning-days suppress-prewarning))
6089 (org-get-wdays s))
6090 (org-get-wdays s))))
6091 ;; When to show a deadline in the calendar: if the
6092 ;; expiration is within WDAYS warning time. Past-due
6093 ;; deadlines are only shown on the current date
6094 (unless (or (and (<= diff wdays)
6095 (and todayp (not org-agenda-only-exact-dates)))
6096 (= diff 0))
6097 (throw :skip nil))
6098 ;; Skip done tasks if `org-agenda-skip-deadline-if-done' is
6099 ;; non-nil or if it isn't applicable to CURRENT deadline.
6100 (when (and donep
6101 (or org-agenda-skip-deadline-if-done
6102 (/= deadline current)))
6103 (throw :skip nil))
6104 (save-excursion
6105 (re-search-backward "^\\*+[ \t]+" nil t)
6106 (goto-char (match-end 0))
6107 (let* ((category (org-get-category))
6108 (level
6109 (make-string (org-reduced-level (org-outline-level)) ?\s))
6110 (head (buffer-substring (point) (line-end-position)))
6111 (inherited-tags
6112 (or (eq org-agenda-show-inherited-tags 'always)
6113 (and (listp org-agenda-show-inherited-tags)
6114 (memq 'agenda org-agenda-show-inherited-tags))
6115 (and (eq org-agenda-show-inherited-tags t)
6116 (or (eq org-agenda-use-tag-inheritance t)
6117 (memq 'agenda
6118 org-agenda-use-tag-inheritance)))))
6119 (tags (org-get-tags-at nil (not inherited-tags)))
6120 (timestr
6121 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6122 (concat (substring s (match-beginning 1)) " ")
6123 'time))
6124 (item
6125 (org-agenda-format-item
6126 ;; For past deadlines, make sure to report time
6127 ;; difference since date S, not since closest
6128 ;; repeater.
6129 (let ((diff (if (< (org-today) current) diff
6130 (- deadline current))))
6131 (if (= diff 0) (car org-agenda-deadline-leaders)
6132 (let ((future (nth 1 org-agenda-deadline-leaders))
6133 (past (nth 2 org-agenda-deadline-leaders)))
6134 (cond ((> diff 0) (format future diff))
6135 ((string= future past) (format past diff))
6136 (t (format past (abs diff)))))))
6137 head level category tags
6138 (and (= diff 0) timestr)))
6139 (face (org-agenda-deadline-face
6140 (- 1 (/ (float (- deadline current)) (max wdays 1)))))
6141 (upcomingp (and todayp (> diff 0)))
6142 (warntime (get-text-property (point) 'org-appt-warntime)))
6143 (org-add-props item props
6144 'org-marker (org-agenda-new-marker pos)
6145 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6146 'warntime warntime
6147 'level level
6148 'ts-date deadline
6149 'priority (- (org-get-priority item) diff)
6150 'todo-state todo-state
6151 'type (if upcomingp "upcoming-deadline" "deadline")
6152 'date (if upcomingp date deadline)
6153 'face (if donep 'org-agenda-done face)
6154 'undone-face face
6155 'done-face 'org-agenda-done)
6156 (push item deadline-items))))))
6157 (nreverse deadline-items)))
6159 (defun org-agenda-deadline-face (fraction)
6160 "Return the face to displaying a deadline item.
6161 FRACTION is what fraction of the head-warning time has passed."
6162 (let ((faces org-agenda-deadline-faces) f)
6163 (catch 'exit
6164 (while (setq f (pop faces))
6165 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6167 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6168 "Return the scheduled information for agenda display.
6169 Optional argument DEADLINES is a list of deadline items to be
6170 displayed in agenda view. When WITH-HOUR is non-nil, only return
6171 scheduled items with an hour specification like [h]h:mm."
6172 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6173 'org-todo-regexp org-todo-regexp
6174 'org-complex-heading-regexp org-complex-heading-regexp
6175 'done-face 'org-agenda-done
6176 'mouse-face 'highlight
6177 'help-echo
6178 (format "mouse-2 or RET jump to org file %s"
6179 (abbreviate-file-name buffer-file-name))))
6180 (regexp (if with-hour
6181 org-scheduled-time-hour-regexp
6182 org-scheduled-time-regexp))
6183 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6184 (current (calendar-absolute-from-gregorian date))
6185 (deadline-pos
6186 (mapcar (lambda (d)
6187 (let ((m (get-text-property 0 'org-hd-marker d)))
6188 (and m (marker-position m))))
6189 deadlines))
6190 scheduled-items)
6191 (goto-char (point-min))
6192 (while (re-search-forward regexp nil t)
6193 (catch :skip
6194 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6195 (org-agenda-skip)
6196 (let* ((s (match-string 1))
6197 (pos (1- (match-beginning 1)))
6198 (todo-state (save-match-data (org-get-todo-state)))
6199 (donep (member todo-state org-done-keywords))
6200 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6201 (member todo-state
6202 org-agenda-repeating-timestamp-show-all)))
6203 ;; SCHEDULE is the current scheduled date. When it
6204 ;; contains a repeater and SHOW-ALL is non-nil,
6205 ;; LAST-REPEAT is the repeat closest to CURRENT.
6206 ;; Otherwise, LAST-REPEAT is equal to SCHEDULE.
6207 (last-repeat (org-agenda--timestamp-to-absolute
6208 s current 'past show-all (current-buffer) pos))
6209 (schedule (org-agenda--timestamp-to-absolute s current))
6210 (diff (- last-repeat current))
6211 (warntime (get-text-property (point) 'org-appt-warntime))
6212 (pastschedp (< schedule (org-today)))
6213 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6214 (suppress-delay
6215 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6216 (org-entry-get nil "DEADLINE"))))
6217 (cond
6218 ((not deadline) nil)
6219 ;; The current item has a deadline date, so
6220 ;; evaluate its delay time.
6221 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6222 ;; Use global delay time.
6223 (- org-agenda-skip-scheduled-delay-if-deadline))
6224 ((eq org-agenda-skip-scheduled-delay-if-deadline
6225 'post-deadline)
6226 ;; Set delay to no later than DEADLINE. If
6227 ;; DEADLINE has a repeater, compare last schedule
6228 ;; repeat and last deadline repeat.
6229 (min (- last-repeat
6230 (org-agenda--timestamp-to-absolute
6231 deadline current 'past show-all
6232 (current-buffer)
6233 (save-excursion
6234 (beginning-of-line)
6235 (1+ (search-forward org-deadline-string)))))
6236 org-scheduled-delay-days))
6237 (t 0))))
6238 (ddays
6239 (cond
6240 ;; Nullify delay when a repeater triggered already
6241 ;; and the delay is of the form --Xd.
6242 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6243 (/= schedule last-repeat))
6245 (suppress-delay
6246 (let ((org-scheduled-delay-days suppress-delay))
6247 (org-get-wdays s t t)))
6248 (t (org-get-wdays s t)))))
6249 ;; Only show a scheduled item in the calendar if it is on or
6250 ;; past the current date. Skip it if it has been displayed
6251 ;; for more than `org-scheduled-past-days'.
6252 (unless (or (and (>= ddays 0) (= diff (- ddays)))
6253 (and (< (+ diff ddays) 0)
6254 (< (abs diff) org-scheduled-past-days)
6255 (and todayp (not org-agenda-only-exact-dates)))
6256 (and todayp
6257 habitp
6258 (bound-and-true-p org-habit-show-all-today)))
6259 (throw :skip nil))
6260 ;; Skip done habits, or tasks if
6261 ;; `org-agenda-skip-deadline-if-done' is non-nil or if it
6262 ;; was scheduled in the past anyway.
6263 (when (and donep
6264 (or org-agenda-skip-scheduled-if-done
6265 (/= schedule current)
6266 habitp))
6267 (throw :skip nil))
6268 ;; Skip entry if it already appears as a deadline, per
6269 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6270 ;; doesn't apply to habits.
6271 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6272 ((guard
6273 (or (not (memq (line-beginning-position 0) deadline-pos))
6274 habitp))
6275 nil)
6276 (`repeated-after-deadline
6277 (>= last-repeat
6278 (time-to-days (org-get-deadline-time (point)))))
6279 (`not-today pastschedp)
6280 (`t t)
6281 (_ nil))
6282 (throw :skip nil))
6283 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6284 ;; only show them for today.
6285 (when (and habitp
6286 (or (not (bound-and-true-p org-habit-show-habits))
6287 (and (not todayp)
6288 (bound-and-true-p
6289 org-habit-show-habits-only-for-today))))
6290 (throw :skip nil))
6291 (save-excursion
6292 (re-search-backward "^\\*+[ \t]+" nil t)
6293 (goto-char (match-end 0))
6294 (let* ((category (org-get-category))
6295 (inherited-tags
6296 (or (eq org-agenda-show-inherited-tags 'always)
6297 (and (listp org-agenda-show-inherited-tags)
6298 (memq 'agenda org-agenda-show-inherited-tags))
6299 (and (eq org-agenda-show-inherited-tags t)
6300 (or (eq org-agenda-use-tag-inheritance t)
6301 (memq 'agenda
6302 org-agenda-use-tag-inheritance)))))
6303 (tags (org-get-tags-at nil (not inherited-tags)))
6304 (level
6305 (make-string (org-reduced-level (org-outline-level)) ?\s))
6306 (head (buffer-substring (point) (line-end-position)))
6307 (timestr
6308 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6309 (concat (substring s (match-beginning 1)) " ")
6310 'time))
6311 (item (org-agenda-format-item
6312 ;; For past scheduled dates, make sure to
6313 ;; report time difference since SCHEDULE,
6314 ;; not since closest repeater.
6315 (let ((diff (if (< (org-today) current) diff
6316 (- schedule current))))
6317 (if (= diff 0) (car org-agenda-scheduled-leaders)
6318 (format (nth 1 org-agenda-scheduled-leaders)
6319 (- 1 diff))))
6320 head level category tags
6321 (and (= diff 0) timestr)
6322 nil habitp))
6323 (face (cond ((and (not habitp) pastschedp)
6324 'org-scheduled-previously)
6325 (todayp 'org-scheduled-today)
6326 (t 'org-scheduled)))
6327 (habitp (and habitp (org-habit-parse-todo))))
6328 (org-add-props item props
6329 'undone-face face
6330 'face (if donep 'org-agenda-done face)
6331 'org-marker (org-agenda-new-marker pos)
6332 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6333 'type (if pastschedp "past-scheduled" "scheduled")
6334 'date (if pastschedp schedule date)
6335 'ts-date schedule
6336 'warntime warntime
6337 'level level
6338 'priority (if habitp (org-habit-get-priority habitp)
6339 (+ 94 (- 5 diff) (org-get-priority item)))
6340 'org-habit-p habitp
6341 'todo-state todo-state)
6342 (push item scheduled-items))))))
6343 (nreverse scheduled-items)))
6345 (defun org-agenda-get-blocks ()
6346 "Return the date-range information for agenda display."
6347 (let* ((props (list 'face nil
6348 'org-not-done-regexp org-not-done-regexp
6349 'org-todo-regexp org-todo-regexp
6350 'org-complex-heading-regexp org-complex-heading-regexp
6351 'mouse-face 'highlight
6352 'help-echo
6353 (format "mouse-2 or RET jump to org file %s"
6354 (abbreviate-file-name buffer-file-name))))
6355 (regexp org-tr-regexp)
6356 (d0 (calendar-absolute-from-gregorian date))
6357 marker hdmarker ee txt d1 d2 s1 s2 category
6358 level todo-state tags pos head donep inherited-tags)
6359 (goto-char (point-min))
6360 (while (re-search-forward regexp nil t)
6361 (catch :skip
6362 (org-agenda-skip)
6363 (setq pos (point))
6364 (let ((start-time (match-string 1))
6365 (end-time (match-string 2)))
6366 (setq s1 (match-string 1)
6367 s2 (match-string 2)
6368 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6369 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6370 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6371 ;; Only allow days between the limits, because the normal
6372 ;; date stamps will catch the limits.
6373 (save-excursion
6374 (setq todo-state (org-get-todo-state))
6375 (setq donep (member todo-state org-done-keywords))
6376 (if (and donep org-agenda-skip-timestamp-if-done)
6377 (throw :skip t))
6378 (setq marker (org-agenda-new-marker (point))
6379 category (org-get-category))
6380 (if (not (re-search-backward org-outline-regexp-bol nil t))
6381 (throw :skip nil)
6382 (goto-char (match-beginning 0))
6383 (setq hdmarker (org-agenda-new-marker (point))
6384 inherited-tags
6385 (or (eq org-agenda-show-inherited-tags 'always)
6386 (and (listp org-agenda-show-inherited-tags)
6387 (memq 'agenda org-agenda-show-inherited-tags))
6388 (and (eq org-agenda-show-inherited-tags t)
6389 (or (eq org-agenda-use-tag-inheritance t)
6390 (memq 'agenda org-agenda-use-tag-inheritance))))
6392 tags (org-get-tags-at nil (not inherited-tags)))
6393 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6394 (looking-at "\\*+[ \t]+\\(.*\\)")
6395 (setq head (match-string 1))
6396 (let ((remove-re
6397 (if org-agenda-remove-timeranges-from-blocks
6398 (concat
6399 "<" (regexp-quote s1) ".*?>"
6400 "--"
6401 "<" (regexp-quote s2) ".*?>")
6402 nil)))
6403 (setq txt (org-agenda-format-item
6404 (format
6405 (nth (if (= d1 d2) 0 1)
6406 org-agenda-timerange-leaders)
6407 (1+ (- d0 d1)) (1+ (- d2 d1)))
6408 head level category tags
6409 (cond ((and (= d1 d0) (= d2 d0))
6410 (concat "<" start-time ">--<" end-time ">"))
6411 ((= d1 d0)
6412 (concat "<" start-time ">"))
6413 ((= d2 d0)
6414 (concat "<" end-time ">")))
6415 remove-re))))
6416 (org-add-props txt props
6417 'org-marker marker 'org-hd-marker hdmarker
6418 'type "block" 'date date
6419 'level level
6420 'todo-state todo-state
6421 'priority (org-get-priority txt))
6422 (push txt ee))))
6423 (goto-char pos)))
6424 ;; Sort the entries by expiration date.
6425 (nreverse ee)))
6427 ;;; Agenda presentation and sorting
6429 (defvar org-prefix-has-time nil
6430 "A flag, set by `org-compile-prefix-format'.
6431 The flag is set if the currently compiled format contains a `%t'.")
6432 (defvar org-prefix-has-tag nil
6433 "A flag, set by `org-compile-prefix-format'.
6434 The flag is set if the currently compiled format contains a `%T'.")
6435 (defvar org-prefix-has-effort nil
6436 "A flag, set by `org-compile-prefix-format'.
6437 The flag is set if the currently compiled format contains a `%e'.")
6438 (defvar org-prefix-has-breadcrumbs nil
6439 "A flag, set by `org-compile-prefix-format'.
6440 The flag is set if the currently compiled format contains a `%b'.")
6441 (defvar org-prefix-category-length nil
6442 "Used by `org-compile-prefix-format' to remember the category field width.")
6443 (defvar org-prefix-category-max-length nil
6444 "Used by `org-compile-prefix-format' to remember the category field width.")
6446 (defun org-agenda-get-category-icon (category)
6447 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6448 (dolist (entry org-agenda-category-icon-alist)
6449 (when (string-match-p (car entry) category)
6450 (if (listp (cadr entry))
6451 (cl-return (cadr entry))
6452 (cl-return (apply #'create-image (cdr entry)))))))
6454 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6455 remove-re habitp)
6456 "Format TXT to be inserted into the agenda buffer.
6457 In particular, add the prefix and corresponding text properties.
6459 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6460 LEVEL may be a string to replace the `%l' specifier.
6461 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6462 category taken from local variable or file name. It will replace the `%c'
6463 specifier in the format.
6464 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6465 TXT for sorting of this entry, and for the `%t' specifier in the format.
6466 When DOTIME is a string, this string is searched for a time before TXT is.
6467 TAGS can be the tags of the headline.
6468 Any match of REMOVE-RE will be removed from TXT."
6469 ;; We keep the org-prefix-* variable values along with a compiled
6470 ;; formatter, so that multiple agendas existing at the same time do
6471 ;; not step on each other toes.
6473 ;; It was inconvenient to make these variables buffer local in
6474 ;; Agenda buffers, because this function expects to be called with
6475 ;; the buffer where item comes from being current, and not agenda
6476 ;; buffer
6477 (let* ((bindings (car org-prefix-format-compiled))
6478 (formatter (cadr org-prefix-format-compiled)))
6479 (cl-loop for (var value) in bindings
6480 do (set var value))
6481 (save-match-data
6482 ;; Diary entries sometimes have extra whitespace at the beginning
6483 (setq txt (org-trim txt))
6485 ;; Fix the tags part in txt
6486 (setq txt (org-agenda-fix-displayed-tags
6487 txt tags
6488 org-agenda-show-inherited-tags
6489 org-agenda-hide-tags-regexp))
6491 (let* ((category (or category
6492 (if buffer-file-name
6493 (file-name-sans-extension
6494 (file-name-nondirectory buffer-file-name))
6495 "")))
6496 (category-icon (org-agenda-get-category-icon category))
6497 (category-icon (if category-icon
6498 (propertize " " 'display category-icon)
6499 ""))
6500 (effort (and (not (string= txt ""))
6501 (get-text-property 1 'effort txt)))
6502 ;; time, tag, effort are needed for the eval of the prefix format
6503 (tag (if tags (nth (1- (length tags)) tags) ""))
6504 time
6505 (ts (if dotime (concat
6506 (if (stringp dotime) dotime "")
6507 (and org-agenda-search-headline-for-time txt))))
6508 (time-of-day (and dotime (org-get-time-of-day ts)))
6509 stamp plain s0 s1 s2 rtn srp l
6510 duration breadcrumbs)
6511 (and (derived-mode-p 'org-mode) buffer-file-name
6512 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6513 (when (and dotime time-of-day)
6514 ;; Extract starting and ending time and move them to prefix
6515 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6516 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6517 (setq s0 (match-string 0 ts)
6518 srp (and stamp (match-end 3))
6519 s1 (match-string (if plain 1 2) ts)
6520 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6522 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6523 ;; them, we might want to remove them there to avoid duplication.
6524 ;; The user can turn this off with a variable.
6525 (if (and org-prefix-has-time
6526 org-agenda-remove-times-when-in-prefix (or stamp plain)
6527 (string-match (concat (regexp-quote s0) " *") txt)
6528 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6529 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6530 (= (match-beginning 0) 0)
6532 (setq txt (replace-match "" nil nil txt))))
6533 ;; Normalize the time(s) to 24 hour
6534 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6535 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6537 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6538 (let (org-time-clocksum-use-effort-durations)
6539 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6540 (setq s2
6541 (org-minutes-to-clocksum-string
6542 (+ (org-hh:mm-string-to-minutes s1)
6543 org-agenda-default-appointment-duration)))))
6545 ;; Compute the duration
6546 (when s2
6547 (setq duration (- (org-hh:mm-string-to-minutes s2)
6548 (org-hh:mm-string-to-minutes s1)))))
6550 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6551 ;; Tags are in the string
6552 (if (or (eq org-agenda-remove-tags t)
6553 (and org-agenda-remove-tags
6554 org-prefix-has-tag))
6555 (setq txt (replace-match "" t t txt))
6556 (setq txt (replace-match
6557 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6558 (match-string 2 txt))
6559 t t txt))))
6561 (when remove-re
6562 (while (string-match remove-re txt)
6563 (setq txt (replace-match "" t t txt))))
6565 ;; Set org-heading property on `txt' to mark the start of the
6566 ;; heading.
6567 (add-text-properties 0 (length txt) '(org-heading t) txt)
6569 ;; Prepare the variables needed in the eval of the compiled format
6570 (if org-prefix-has-breadcrumbs
6571 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6572 (let ((s (org-display-outline-path nil nil "->" t)))
6573 (if (eq "" s) "" (concat s "->"))))))
6574 (setq time (cond (s2 (concat
6575 (org-agenda-time-of-day-to-ampm-maybe s1)
6576 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6577 (if org-agenda-timegrid-use-ampm " ")))
6578 (s1 (concat
6579 (org-agenda-time-of-day-to-ampm-maybe s1)
6580 (if org-agenda-timegrid-use-ampm
6581 "........ "
6582 "......")))
6583 (t ""))
6584 extra (or (and (not habitp) extra) "")
6585 category (if (symbolp category) (symbol-name category) category)
6586 level (or level ""))
6587 (if (string-match org-bracket-link-regexp category)
6588 (progn
6589 (setq l (if (match-end 3)
6590 (- (match-end 3) (match-beginning 3))
6591 (- (match-end 1) (match-beginning 1))))
6592 (when (< l (or org-prefix-category-length 0))
6593 (setq category (copy-sequence category))
6594 (org-add-props category nil
6595 'extra-space (make-string
6596 (- org-prefix-category-length l 1) ?\ ))))
6597 (if (and org-prefix-category-max-length
6598 (>= (length category) org-prefix-category-max-length))
6599 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6600 ;; Evaluate the compiled format
6601 (setq rtn (concat (eval formatter) txt))
6603 ;; And finally add the text properties
6604 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6605 (org-add-props rtn nil
6606 'org-category category
6607 'tags (mapcar 'org-downcase-keep-props tags)
6608 'org-highest-priority org-highest-priority
6609 'org-lowest-priority org-lowest-priority
6610 'time-of-day time-of-day
6611 'duration duration
6612 'breadcrumbs breadcrumbs
6613 'txt txt
6614 'level level
6615 'time time
6616 'extra extra
6617 'format org-prefix-format-compiled
6618 'dotime dotime)))))
6620 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6621 "Remove tags string from TXT, and add a modified list of tags.
6622 The modified list may contain inherited tags, and tags matched by
6623 `org-agenda-hide-tags-regexp' will be removed."
6624 (when (or add-inherited hide-re)
6625 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6626 (setq txt (substring txt 0 (match-beginning 0))))
6627 (setq tags
6628 (delq nil
6629 (mapcar (lambda (tg)
6630 (if (or (and hide-re (string-match hide-re tg))
6631 (and (not add-inherited)
6632 (get-text-property 0 'inherited tg)))
6634 tg))
6635 tags)))
6636 (when tags
6637 (let ((have-i (get-text-property 0 'inherited (car tags)))
6639 (setq txt (concat txt " :"
6640 (mapconcat
6641 (lambda (x)
6642 (setq i (get-text-property 0 'inherited x))
6643 (if (and have-i (not i))
6644 (progn
6645 (setq have-i nil)
6646 (concat ":" x))
6648 tags ":")
6649 (if have-i "::" ":"))))))
6650 txt)
6652 (defun org-downcase-keep-props (s)
6653 (let ((props (text-properties-at 0 s)))
6654 (setq s (downcase s))
6655 (add-text-properties 0 (length s) props s)
6658 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6660 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6661 "Add a time-grid for agenda items which need it.
6663 LIST is the list of agenda items formatted by `org-agenda-list'.
6664 NDAYS is the span of the current agenda view.
6665 TODAYP is t when the current agenda view is on today."
6666 (catch 'exit
6667 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6668 ((and todayp (member 'today (car org-agenda-time-grid))))
6669 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6670 ((member 'weekly (car org-agenda-time-grid)))
6671 (t (throw 'exit list)))
6672 (let* ((have (delq nil (mapcar
6673 (lambda (x) (get-text-property 1 'time-of-day x))
6674 list)))
6675 (string (nth 1 org-agenda-time-grid))
6676 (gridtimes (nth 2 org-agenda-time-grid))
6677 (req (car org-agenda-time-grid))
6678 (remove (member 'remove-match req))
6679 new time)
6680 (if (and (member 'require-timed req) (not have))
6681 ;; don't show empty grid
6682 (throw 'exit list))
6683 (while (setq time (pop gridtimes))
6684 (unless (and remove (member time have))
6685 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6686 (push (org-agenda-format-item
6687 nil string nil "" nil
6688 (concat (substring time 0 -2) ":" (substring time -2)))
6689 new)
6690 (put-text-property
6691 2 (length (car new)) 'face 'org-time-grid (car new))))
6692 (when (and todayp org-agenda-show-current-time-in-grid)
6693 (push (org-agenda-format-item
6694 nil org-agenda-current-time-string nil "" nil
6695 (format-time-string "%H:%M "))
6696 new)
6697 (put-text-property
6698 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6700 (if (member 'time-up org-agenda-sorting-strategy-selected)
6701 (append new list)
6702 (append list new)))))
6704 (defun org-compile-prefix-format (key)
6705 "Compile the prefix format into a Lisp form that can be evaluated.
6706 The resulting form and associated variable bindings is returned
6707 and stored in the variable `org-prefix-format-compiled'."
6708 (setq org-prefix-has-time nil
6709 org-prefix-has-tag nil
6710 org-prefix-category-length nil
6711 org-prefix-has-effort nil
6712 org-prefix-has-breadcrumbs nil)
6713 (let ((s (cond
6714 ((stringp org-agenda-prefix-format)
6715 org-agenda-prefix-format)
6716 ((assq key org-agenda-prefix-format)
6717 (cdr (assq key org-agenda-prefix-format)))
6718 (t " %-12:c%?-12t% s")))
6719 (start 0)
6720 varform vars var e c f opt)
6721 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6722 s start)
6723 (setq var (or (cdr (assoc (match-string 4 s)
6724 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6725 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6726 'eval)
6727 c (or (match-string 3 s) "")
6728 opt (match-beginning 1)
6729 start (1+ (match-beginning 0)))
6730 (if (eq var 'time) (setq org-prefix-has-time t))
6731 (if (eq var 'tag) (setq org-prefix-has-tag t))
6732 (if (eq var 'effort) (setq org-prefix-has-effort t))
6733 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6734 (setq f (concat "%" (match-string 2 s) "s"))
6735 (when (eq var 'category)
6736 (setq org-prefix-category-length
6737 (floor (abs (string-to-number (match-string 2 s)))))
6738 (setq org-prefix-category-max-length
6739 (let ((x (match-string 2 s)))
6740 (save-match-data
6741 (if (string-match "\\.[0-9]+" x)
6742 (string-to-number (substring (match-string 0 x) 1)))))))
6743 (if (eq var 'eval)
6744 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6745 (if opt
6746 (setq varform
6747 `(if (or (equal "" ,var) (equal nil ,var))
6749 (format ,f (concat ,var ,c))))
6750 (setq varform
6751 `(format ,f (if (or (equal ,var "")
6752 (equal ,var nil)) ""
6753 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6754 (setq s (replace-match "%s" t nil s))
6755 (push varform vars))
6756 (setq vars (nreverse vars))
6757 (with-current-buffer (or org-agenda-buffer (current-buffer))
6758 (setq org-prefix-format-compiled
6759 (list
6760 `((org-prefix-has-time ,org-prefix-has-time)
6761 (org-prefix-has-tag ,org-prefix-has-tag)
6762 (org-prefix-category-length ,org-prefix-category-length)
6763 (org-prefix-has-effort ,org-prefix-has-effort)
6764 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6765 `(format ,s ,@vars))))))
6767 (defun org-set-sorting-strategy (key)
6768 (if (symbolp (car org-agenda-sorting-strategy))
6769 ;; the old format
6770 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6771 (setq org-agenda-sorting-strategy-selected
6772 (or (cdr (assq key org-agenda-sorting-strategy))
6773 (cdr (assq 'agenda org-agenda-sorting-strategy))
6774 '(time-up category-keep priority-down)))))
6776 (defun org-get-time-of-day (s &optional string mod24)
6777 "Check string S for a time of day.
6778 If found, return it as a military time number between 0 and 2400.
6779 If not found, return nil.
6780 The optional STRING argument forces conversion into a 5 character wide string
6781 HH:MM."
6782 (save-match-data
6783 (when
6784 (and
6785 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6786 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6787 (not (eq (get-text-property 1 'face s) 'org-link)))
6788 (let* ((h (string-to-number (match-string 1 s)))
6789 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6790 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6791 (am-p (equal ampm "am"))
6792 (h1 (cond ((not ampm) h)
6793 ((= h 12) (if am-p 0 12))
6794 (t (+ h (if am-p 0 12)))))
6795 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6796 (mod h1 24) h1))
6797 (t0 (+ (* 100 h2) m))
6798 (t1 (concat (if (>= h1 24) "+" " ")
6799 (if (and org-agenda-time-leading-zero
6800 (< t0 1000)) "0" "")
6801 (if (< t0 100) "0" "")
6802 (if (< t0 10) "0" "")
6803 (int-to-string t0))))
6804 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6806 (defvar org-agenda-before-sorting-filter-function nil
6807 "Function to be applied to agenda items prior to sorting.
6808 Prior to sorting also means just before they are inserted into the agenda.
6810 To aid sorting, you may revisit the original entries and add more text
6811 properties which will later be used by the sorting functions.
6813 The function should take a string argument, an agenda line.
6814 It has access to the text properties in that line, which contain among
6815 other things, the property `org-hd-marker' that points to the entry
6816 where the line comes from. Note that not all lines going into the agenda
6817 have this property, only most.
6819 The function should return the modified string. It is probably best
6820 to ONLY change text properties.
6822 You can also use this function as a filter, by returning nil for lines
6823 you don't want to have in the agenda at all. For this application, you
6824 could bind the variable in the options section of a custom command.")
6826 (defun org-agenda-finalize-entries (list &optional type)
6827 "Sort, limit and concatenate the LIST of agenda items.
6828 The optional argument TYPE tells the agenda type."
6829 (let ((max-effort (cond ((listp org-agenda-max-effort)
6830 (cdr (assoc type org-agenda-max-effort)))
6831 (t org-agenda-max-effort)))
6832 (max-todo (cond ((listp org-agenda-max-todos)
6833 (cdr (assoc type org-agenda-max-todos)))
6834 (t org-agenda-max-todos)))
6835 (max-tags (cond ((listp org-agenda-max-tags)
6836 (cdr (assoc type org-agenda-max-tags)))
6837 (t org-agenda-max-tags)))
6838 (max-entries (cond ((listp org-agenda-max-entries)
6839 (cdr (assoc type org-agenda-max-entries)))
6840 (t org-agenda-max-entries))))
6841 (when org-agenda-before-sorting-filter-function
6842 (setq list
6843 (delq nil
6844 (mapcar
6845 org-agenda-before-sorting-filter-function list))))
6846 (setq list (mapcar 'org-agenda-highlight-todo list)
6847 list (mapcar 'identity (sort list 'org-entries-lessp)))
6848 (when max-effort
6849 (setq list (org-agenda-limit-entries
6850 list 'effort-minutes max-effort
6851 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6852 32767 -1))))))
6853 (when max-todo
6854 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6855 (when max-tags
6856 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6857 (when max-entries
6858 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6859 (mapconcat 'identity list "\n")))
6861 (defun org-agenda-limit-entries (list prop limit &optional fn)
6862 "Limit the number of agenda entries."
6863 (let ((include (and limit (< limit 0))))
6864 (if limit
6865 (let ((fun (or fn (lambda (p) (if p 1))))
6866 (lim 0))
6867 (delq nil
6868 (mapcar
6869 (lambda (e)
6870 (let ((pval (funcall
6871 fun (get-text-property (1- (length e))
6872 prop e))))
6873 (if pval (setq lim (+ lim pval)))
6874 (cond ((and pval (<= lim (abs limit))) e)
6875 ((and include (not pval)) e))))
6876 list)))
6877 list)))
6879 (defun org-agenda-limit-interactively (remove)
6880 "In agenda, interactively limit entries to various maximums."
6881 (interactive "P")
6882 (if remove
6883 (progn (setq org-agenda-max-entries nil
6884 org-agenda-max-todos nil
6885 org-agenda-max-tags nil
6886 org-agenda-max-effort nil)
6887 (org-agenda-redo))
6888 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6889 (msg (cond ((= max ?E) "How many minutes? ")
6890 ((= max ?e) "How many entries? ")
6891 ((= max ?t) "How many TODO entries? ")
6892 ((= max ?T) "How many tagged entries? ")
6893 (t (user-error "Wrong input"))))
6894 (num (string-to-number (read-from-minibuffer msg))))
6895 (cond ((equal max ?e)
6896 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6897 ((equal max ?t)
6898 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6899 ((equal max ?T)
6900 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6901 ((equal max ?E)
6902 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6903 (org-agenda-fit-window-to-buffer))
6905 (defun org-agenda-highlight-todo (x)
6906 (let ((org-done-keywords org-done-keywords-for-agenda)
6907 (case-fold-search nil)
6909 (if (eq x 'line)
6910 (save-excursion
6911 (beginning-of-line 1)
6912 (setq re (org-get-at-bol 'org-todo-regexp))
6913 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6914 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6915 (add-text-properties (match-beginning 0) (match-end 1)
6916 (list 'face (org-get-todo-face 1)))
6917 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6918 (delete-region (match-beginning 1) (1- (match-end 0)))
6919 (goto-char (match-beginning 1))
6920 (insert (format org-agenda-todo-keyword-format s)))))
6921 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6922 (setq re (get-text-property 0 'org-todo-regexp x))
6923 (when (and re
6924 ;; Test `pl' because if there's no heading content,
6925 ;; there's no point matching to highlight. Note
6926 ;; that if we didn't test `pl' first, and there
6927 ;; happened to be no keyword from `org-todo-regexp'
6928 ;; on this heading line, then the `equal' comparison
6929 ;; afterwards would spuriously succeed in the case
6930 ;; where `pl' is nil -- causing an args-out-of-range
6931 ;; error when we try to add text properties to text
6932 ;; that isn't there.
6934 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6935 x pl) pl))
6936 (add-text-properties
6937 (or (match-end 1) (match-end 0)) (match-end 0)
6938 (list 'face (org-get-todo-face (match-string 2 x)))
6940 (when (match-end 1)
6941 (setq x (concat (substring x 0 (match-end 1))
6942 (format org-agenda-todo-keyword-format
6943 (match-string 2 x))
6944 (org-add-props " " (text-properties-at 0 x))
6945 (substring x (match-end 3)))))))
6946 x)))
6948 (defsubst org-cmp-values (a b property)
6949 "Compare the numeric value of text PROPERTY for string A and B."
6950 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6951 (pb (or (get-text-property (1- (length b)) property b) 0)))
6952 (cond ((> pa pb) +1)
6953 ((< pa pb) -1))))
6955 (defsubst org-cmp-effort (a b)
6956 "Compare the effort values of string A and B."
6957 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6958 ;; `effort-minutes' property is not directly accessible from
6959 ;; the strings, but is stored as a property in `txt'.
6960 (ea (or (get-text-property
6961 0 'effort-minutes (get-text-property 0 'txt a))
6962 def))
6963 (eb (or (get-text-property
6964 0 'effort-minutes (get-text-property 0 'txt b))
6965 def)))
6966 (cond ((> ea eb) +1)
6967 ((< ea eb) -1))))
6969 (defsubst org-cmp-category (a b)
6970 "Compare the string values of categories of strings A and B."
6971 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6972 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6973 (cond ((string-lessp ca cb) -1)
6974 ((string-lessp cb ca) +1))))
6976 (defsubst org-cmp-todo-state (a b)
6977 "Compare the todo states of strings A and B."
6978 (let* ((ma (or (get-text-property 1 'org-marker a)
6979 (get-text-property 1 'org-hd-marker a)))
6980 (mb (or (get-text-property 1 'org-marker b)
6981 (get-text-property 1 'org-hd-marker b)))
6982 (fa (and ma (marker-buffer ma)))
6983 (fb (and mb (marker-buffer mb)))
6984 (todo-kwds
6985 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6986 (and fb (with-current-buffer fb org-todo-keywords-1))))
6987 (ta (or (get-text-property 1 'todo-state a) ""))
6988 (tb (or (get-text-property 1 'todo-state b) ""))
6989 (la (- (length (member ta todo-kwds))))
6990 (lb (- (length (member tb todo-kwds))))
6991 (donepa (member ta org-done-keywords-for-agenda))
6992 (donepb (member tb org-done-keywords-for-agenda)))
6993 (cond ((and donepa (not donepb)) -1)
6994 ((and (not donepa) donepb) +1)
6995 ((< la lb) -1)
6996 ((< lb la) +1))))
6998 (defsubst org-cmp-alpha (a b)
6999 "Compare the headlines, alphabetically."
7000 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
7001 (plb (text-property-any 0 (length b) 'org-heading t b))
7002 (ta (and pla (substring a pla)))
7003 (tb (and plb (substring b plb))))
7004 (when pla
7005 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
7006 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
7007 (setq ta (substring ta (match-end 0))))
7008 (setq ta (downcase ta)))
7009 (when plb
7010 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
7011 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
7012 (setq tb (substring tb (match-end 0))))
7013 (setq tb (downcase tb)))
7014 (cond ((not ta) +1)
7015 ((not tb) -1)
7016 ((string-lessp ta tb) -1)
7017 ((string-lessp tb ta) +1))))
7019 (defsubst org-cmp-tag (a b)
7020 "Compare the string values of the first tags of A and B."
7021 (let ((ta (car (last (get-text-property 1 'tags a))))
7022 (tb (car (last (get-text-property 1 'tags b)))))
7023 (cond ((not ta) +1)
7024 ((not tb) -1)
7025 ((string-lessp ta tb) -1)
7026 ((string-lessp tb ta) +1))))
7028 (defsubst org-cmp-time (a b)
7029 "Compare the time-of-day values of strings A and B."
7030 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7031 (ta (or (get-text-property 1 'time-of-day a) def))
7032 (tb (or (get-text-property 1 'time-of-day b) def)))
7033 (cond ((< ta tb) -1)
7034 ((< tb ta) +1))))
7036 (defsubst org-cmp-ts (a b type)
7037 "Compare the timestamps values of entries A and B.
7038 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7039 \"timestamp_ia\", compare within each of these type. When TYPE
7040 is the empty string, compare all timestamps without respect of
7041 their type."
7042 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7043 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7044 (get-text-property 1 'ts-date a))
7045 def))
7046 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7047 (get-text-property 1 'ts-date b))
7048 def)))
7049 (cond ((< ta tb) -1)
7050 ((< tb ta) +1))))
7052 (defsubst org-cmp-habit-p (a b)
7053 "Compare the todo states of strings A and B."
7054 (let ((ha (get-text-property 1 'org-habit-p a))
7055 (hb (get-text-property 1 'org-habit-p b)))
7056 (cond ((and ha (not hb)) -1)
7057 ((and (not ha) hb) +1))))
7059 (defun org-entries-lessp (a b)
7060 "Predicate for sorting agenda entries."
7061 ;; The following variables will be used when the form is evaluated.
7062 ;; So even though the compiler complains, keep them.
7063 (let* ((ss org-agenda-sorting-strategy-selected)
7064 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7065 (org-cmp-ts a b "")))
7066 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7067 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7068 (org-cmp-ts a b "scheduled")))
7069 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7070 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7071 (org-cmp-ts a b "deadline")))
7072 (deadline-down (if deadline-up (- deadline-up) nil))
7073 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7074 (org-cmp-ts a b "timestamp_ia")))
7075 (tsia-down (if tsia-up (- tsia-up) nil))
7076 (ts-up (and (org-em 'ts-up 'ts-down ss)
7077 (org-cmp-ts a b "timestamp")))
7078 (ts-down (if ts-up (- ts-up) nil))
7079 (time-up (and (org-em 'time-up 'time-down ss)
7080 (org-cmp-time a b)))
7081 (time-down (if time-up (- time-up) nil))
7082 (stats-up (and (org-em 'stats-up 'stats-down ss)
7083 (org-cmp-values a b 'org-stats)))
7084 (stats-down (if stats-up (- stats-up) nil))
7085 (priority-up (and (org-em 'priority-up 'priority-down ss)
7086 (org-cmp-values a b 'priority)))
7087 (priority-down (if priority-up (- priority-up) nil))
7088 (effort-up (and (org-em 'effort-up 'effort-down ss)
7089 (org-cmp-effort a b)))
7090 (effort-down (if effort-up (- effort-up) nil))
7091 (category-up (and (or (org-em 'category-up 'category-down ss)
7092 (memq 'category-keep ss))
7093 (org-cmp-category a b)))
7094 (category-down (if category-up (- category-up) nil))
7095 (category-keep (if category-up +1 nil))
7096 (tag-up (and (org-em 'tag-up 'tag-down ss)
7097 (org-cmp-tag a b)))
7098 (tag-down (if tag-up (- tag-up) nil))
7099 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7100 (org-cmp-todo-state a b)))
7101 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7102 (habit-up (and (org-em 'habit-up 'habit-down ss)
7103 (org-cmp-habit-p a b)))
7104 (habit-down (if habit-up (- habit-up) nil))
7105 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7106 (org-cmp-alpha a b)))
7107 (alpha-down (if alpha-up (- alpha-up) nil))
7108 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7109 user-defined-up user-defined-down)
7110 (if (and need-user-cmp org-agenda-cmp-user-defined
7111 (functionp org-agenda-cmp-user-defined))
7112 (setq user-defined-up
7113 (funcall org-agenda-cmp-user-defined a b)
7114 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7115 (cdr (assoc
7116 (eval (cons 'or org-agenda-sorting-strategy-selected))
7117 '((-1 . t) (1 . nil) (nil . nil))))))
7119 ;;; Agenda restriction lock
7121 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7122 "Overlay to mark the headline to which agenda commands are restricted.")
7123 (overlay-put org-agenda-restriction-lock-overlay
7124 'face 'org-agenda-restriction-lock)
7125 (overlay-put org-agenda-restriction-lock-overlay
7126 'help-echo "Agendas are currently limited to this subtree.")
7127 (delete-overlay org-agenda-restriction-lock-overlay)
7129 ;;;###autoload
7130 (defun org-agenda-set-restriction-lock (&optional type)
7131 "Set restriction lock for agenda, to current subtree or file.
7132 Restriction will be the file if TYPE is `file', or if type is the
7133 universal prefix \\='(4), or if the cursor is before the first headline
7134 in the file. Otherwise, restriction will be to the current subtree."
7135 (interactive "P")
7136 (org-agenda-remove-restriction-lock 'noupdate)
7137 (and (equal type '(4)) (setq type 'file))
7138 (setq type (cond
7139 (type type)
7140 ((org-at-heading-p) 'subtree)
7141 ((condition-case nil (org-back-to-heading t) (error nil))
7142 'subtree)
7143 (t 'file)))
7144 (if (eq type 'subtree)
7145 (progn
7146 (setq org-agenda-restrict (current-buffer))
7147 (setq org-agenda-overriding-restriction 'subtree)
7148 (put 'org-agenda-files 'org-restrict
7149 (list (buffer-file-name (buffer-base-buffer))))
7150 (org-back-to-heading t)
7151 (move-overlay org-agenda-restriction-lock-overlay
7152 (point)
7153 (if org-agenda-restriction-lock-highlight-subtree
7154 (save-excursion (org-end-of-subtree t t) (point))
7155 (point-at-eol)))
7156 (move-marker org-agenda-restrict-begin (point))
7157 (move-marker org-agenda-restrict-end
7158 (save-excursion (org-end-of-subtree t t)))
7159 (message "Locking agenda restriction to subtree"))
7160 (put 'org-agenda-files 'org-restrict
7161 (list (buffer-file-name (buffer-base-buffer))))
7162 (setq org-agenda-restrict nil)
7163 (setq org-agenda-overriding-restriction 'file)
7164 (move-marker org-agenda-restrict-begin nil)
7165 (move-marker org-agenda-restrict-end nil)
7166 (message "Locking agenda restriction to file"))
7167 (setq current-prefix-arg nil)
7168 (org-agenda-maybe-redo))
7170 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7171 "Remove the agenda restriction lock."
7172 (interactive "P")
7173 (delete-overlay org-agenda-restriction-lock-overlay)
7174 (delete-overlay org-speedbar-restriction-lock-overlay)
7175 (setq org-agenda-overriding-restriction nil)
7176 (setq org-agenda-restrict nil)
7177 (put 'org-agenda-files 'org-restrict nil)
7178 (move-marker org-agenda-restrict-begin nil)
7179 (move-marker org-agenda-restrict-end nil)
7180 (setq current-prefix-arg nil)
7181 (message "Agenda restriction lock removed")
7182 (or noupdate (org-agenda-maybe-redo)))
7184 (defun org-agenda-maybe-redo ()
7185 "If there is any window showing the agenda view, update it."
7186 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7187 org-agenda-buffer-name)
7189 (w0 (selected-window)))
7190 (when w
7191 (select-window w)
7192 (org-agenda-redo)
7193 (select-window w0)
7194 (if org-agenda-overriding-restriction
7195 (message "Agenda view shifted to new %s restriction"
7196 org-agenda-overriding-restriction)
7197 (message "Agenda restriction lock removed")))))
7199 ;;; Agenda commands
7201 (defun org-agenda-check-type (error &rest types)
7202 "Check if agenda buffer is of allowed type.
7203 If ERROR is non-nil, throw an error, otherwise just return nil.
7204 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7205 (if (not org-agenda-type)
7206 (error "No Org agenda currently displayed")
7207 (if (memq org-agenda-type types)
7209 (if error
7210 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7211 nil))))
7213 (defun org-agenda-Quit ()
7214 "Exit the agenda, killing the agenda buffer.
7215 Like `org-agenda-quit', but kill the buffer even when
7216 `org-agenda-sticky' is non-nil."
7217 (interactive)
7218 (org-agenda--quit))
7220 (defun org-agenda-quit ()
7221 "Exit the agenda.
7223 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7224 instead of killing it.
7226 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7227 the pre-agenda window configuration.
7229 When column view is active, exit column view instead of the
7230 agenda."
7231 (interactive)
7232 (org-agenda--quit org-agenda-sticky))
7234 (defun org-agenda--quit (&optional bury)
7235 (if org-agenda-columns-active
7236 (org-columns-quit)
7237 (let ((wconf org-agenda-pre-window-conf)
7238 (buf (current-buffer))
7239 (org-agenda-last-indirect-window
7240 (and (eq org-indirect-buffer-display 'other-window)
7241 org-agenda-last-indirect-buffer
7242 (get-buffer-window org-agenda-last-indirect-buffer))))
7243 (cond
7244 ((eq org-agenda-window-setup 'other-frame)
7245 (delete-frame))
7246 ((and org-agenda-restore-windows-after-quit
7247 wconf)
7248 ;; Maybe restore the pre-agenda window configuration. Reset
7249 ;; `org-agenda-pre-window-conf' before running
7250 ;; `set-window-configuration', which loses the current buffer.
7251 (setq org-agenda-pre-window-conf nil)
7252 (set-window-configuration wconf))
7254 (when org-agenda-last-indirect-window
7255 (delete-window org-agenda-last-indirect-window))
7256 (and (not (eq org-agenda-window-setup 'current-window))
7257 (not (one-window-p))
7258 (delete-window))))
7259 (if bury
7260 ;; Set the agenda buffer as the current buffer instead of
7261 ;; passing it as an argument to `bury-buffer' so that
7262 ;; `bury-buffer' removes it from the window.
7263 (with-current-buffer buf
7264 (bury-buffer))
7265 (kill-buffer buf)
7266 (setq org-agenda-archives-mode nil
7267 org-agenda-buffer nil)))))
7269 (defun org-agenda-exit ()
7270 "Exit the agenda, killing Org buffers loaded by the agenda.
7271 Like `org-agenda-Quit', but kill any buffers that were created by
7272 the agenda. Org buffers visited directly by the user will not be
7273 touched. Also, exit the agenda even if it is in column view."
7274 (interactive)
7275 (when org-agenda-columns-active
7276 (org-columns-quit))
7277 (org-release-buffers org-agenda-new-buffers)
7278 (setq org-agenda-new-buffers nil)
7279 (org-agenda-Quit))
7281 (defun org-agenda-kill-all-agenda-buffers ()
7282 "Kill all buffers in `org-agenda-mode'.
7283 This is used when toggling sticky agendas."
7284 (interactive)
7285 (let (blist)
7286 (dolist (buf (buffer-list))
7287 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7288 (push buf blist)))
7289 (mapc 'kill-buffer blist)))
7291 (defun org-agenda-execute (arg)
7292 "Execute another agenda command, keeping same window.
7293 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7294 in the agenda."
7295 (interactive "P")
7296 (let ((org-agenda-window-setup 'current-window))
7297 (org-agenda arg)))
7299 (defun org-agenda-redo (&optional all)
7300 "Rebuild possibly ALL agenda view(s) in the current buffer."
7301 (interactive "P")
7302 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7303 (cpa (unless (eq all t) current-prefix-arg))
7304 (org-agenda-doing-sticky-redo org-agenda-sticky)
7305 (org-agenda-sticky nil)
7306 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7307 org-agenda-buffer-name))
7308 (org-agenda-keep-modes t)
7309 (tag-filter org-agenda-tag-filter)
7310 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7311 (top-hl-filter org-agenda-top-headline-filter)
7312 (cat-filter org-agenda-category-filter)
7313 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7314 (re-filter org-agenda-regexp-filter)
7315 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7316 (effort-filter org-agenda-effort-filter)
7317 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7318 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7319 (cols org-agenda-columns-active)
7320 (line (org-current-line))
7321 (window-line (- line (org-current-line (window-start))))
7322 (lprops (get 'org-agenda-redo-command 'org-lprops))
7323 (redo-cmd (get-text-property p 'org-redo-cmd))
7324 (last-args (get-text-property p 'org-last-args))
7325 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7326 (org-agenda-overriding-cmd-arguments
7327 (unless (eq all t)
7328 (cond ((listp last-args)
7329 (cons (or cpa (car last-args)) (cdr last-args)))
7330 ((stringp last-args)
7331 last-args))))
7332 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7333 (put 'org-agenda-tag-filter :preset-filter nil)
7334 (put 'org-agenda-category-filter :preset-filter nil)
7335 (put 'org-agenda-regexp-filter :preset-filter nil)
7336 (put 'org-agenda-effort-filter :preset-filter nil)
7337 (and cols (org-columns-quit))
7338 (message "Rebuilding agenda buffer...")
7339 (if series-redo-cmd
7340 (eval series-redo-cmd)
7341 (org-let lprops redo-cmd))
7342 (setq org-agenda-undo-list nil
7343 org-agenda-pending-undo-list nil
7344 org-agenda-tag-filter tag-filter
7345 org-agenda-category-filter cat-filter
7346 org-agenda-regexp-filter re-filter
7347 org-agenda-effort-filter effort-filter
7348 org-agenda-top-headline-filter top-hl-filter)
7349 (message "Rebuilding agenda buffer...done")
7350 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7351 (put 'org-agenda-category-filter :preset-filter cat-preset)
7352 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7353 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7354 (let ((tag (or tag-filter tag-preset))
7355 (cat (or cat-filter cat-preset))
7356 (effort (or effort-filter effort-preset))
7357 (re (or re-filter re-preset)))
7358 (when tag (org-agenda-filter-apply tag 'tag t))
7359 (when cat (org-agenda-filter-apply cat 'category))
7360 (when effort (org-agenda-filter-apply effort 'effort))
7361 (when re (org-agenda-filter-apply re 'regexp)))
7362 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7363 (and cols (called-interactively-p 'any) (org-agenda-columns))
7364 (org-goto-line line)
7365 (recenter window-line)))
7367 (defvar org-global-tags-completion-table nil)
7368 (defvar org-agenda-filter-form nil)
7369 (defvar org-agenda-filtered-by-category nil)
7371 (defun org-agenda-filter-by-category (strip)
7372 "Filter lines in the agenda buffer that have a specific category.
7373 The category is that of the current line.
7374 Without prefix argument, keep only the lines of that category.
7375 With a prefix argument, exclude the lines of that category.
7377 (interactive "P")
7378 (if (and org-agenda-filtered-by-category
7379 org-agenda-category-filter)
7380 (org-agenda-filter-show-all-cat)
7381 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7382 (cond
7383 ((and cat strip)
7384 (org-agenda-filter-apply
7385 (push (concat "-" cat) org-agenda-category-filter) 'category))
7386 (cat
7387 (org-agenda-filter-apply
7388 (setq org-agenda-category-filter
7389 (list (concat "+" cat))) 'category))
7390 (t (error "No category at point"))))))
7392 (defun org-find-top-headline (&optional pos)
7393 "Find the topmost parent headline and return it."
7394 (save-excursion
7395 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7396 (if pos (goto-char pos))
7397 ;; Skip up to the topmost parent
7398 (while (ignore-errors (outline-up-heading 1) t))
7399 (ignore-errors
7400 (nth 4 (org-heading-components))))))
7402 (defvar org-agenda-filtered-by-top-headline nil)
7403 (defun org-agenda-filter-by-top-headline (strip)
7404 "Keep only those lines that are descendants from the same top headline.
7405 The top headline is that of the current line."
7406 (interactive "P")
7407 (if org-agenda-filtered-by-top-headline
7408 (progn
7409 (setq org-agenda-filtered-by-top-headline nil
7410 org-agenda-top-headline-filter nil)
7411 (org-agenda-filter-show-all-top-filter))
7412 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7413 (if toph (org-agenda-filter-top-headline-apply toph strip)
7414 (error "No top-level headline at point")))))
7416 (defvar org-agenda-regexp-filter nil)
7417 (defun org-agenda-filter-by-regexp (strip)
7418 "Filter agenda entries by a regular expression.
7419 Regexp filters are cumulative.
7420 With no prefix argument, keep entries matching the regexp.
7421 With one prefix argument, filter out entries matching the regexp.
7422 With two prefix arguments, remove the regexp filters."
7423 (interactive "P")
7424 (if (not (equal strip '(16)))
7425 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7426 (read-from-minibuffer
7427 (if (equal strip '(4))
7428 "Filter out entries matching regexp: "
7429 "Narrow to entries matching regexp: ")))))
7430 (push flt org-agenda-regexp-filter)
7431 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7432 (org-agenda-filter-show-all-re)
7433 (message "Regexp filter removed")))
7435 (defvar org-agenda-effort-filter nil)
7436 (defun org-agenda-filter-by-effort (strip)
7437 "Filter agenda entries by effort.
7438 With no prefix argument, keep entries matching the effort condition.
7439 With one prefix argument, filter out entries matching the condition.
7440 With two prefix arguments, remove the effort filters."
7441 (interactive "P")
7442 (cond ((member strip '(nil 4))
7443 (let ((efforts (org-split-string
7444 (or (cdr (assoc (concat org-effort-property "_ALL")
7445 org-global-properties))
7446 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7447 "")))
7448 (eff -1)
7449 effort-prompt op)
7450 (while (not (member op '(?< ?> ?=)))
7451 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7452 (cl-loop for i from 0 to 9 do
7453 (setq effort-prompt
7454 (concat
7455 effort-prompt " ["
7456 (if (= i 9) "0" (int-to-string (1+ i)))
7457 "]" (nth i efforts))))
7458 (message "Effort %s%s" (char-to-string op) effort-prompt)
7459 (while (or (< eff 0) (> eff 9))
7460 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7461 (setq org-agenda-effort-filter
7462 (list (concat (if strip "-" "+")
7463 (char-to-string op) (nth (1- eff) efforts))))
7464 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7465 (t (org-agenda-filter-show-all-effort)
7466 (message "Effort filter removed"))))
7468 (defun org-agenda-filter-remove-all ()
7469 "Remove all filters from the current agenda buffer."
7470 (interactive)
7471 (when org-agenda-tag-filter
7472 (org-agenda-filter-show-all-tag))
7473 (when org-agenda-category-filter
7474 (org-agenda-filter-show-all-cat))
7475 (when org-agenda-regexp-filter
7476 (org-agenda-filter-show-all-re))
7477 (when org-agenda-top-headline-filter
7478 (org-agenda-filter-show-all-top-filter))
7479 (when org-agenda-effort-filter
7480 (org-agenda-filter-show-all-effort))
7481 (org-agenda-finalize))
7483 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7484 "Keep only those lines in the agenda buffer that have a specific tag.
7486 The tag is selected with its fast selection letter, as configured.
7488 With a `\\[universal-argument]' prefix, exclude the agenda search.
7490 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7491 i.e. don't
7492 filter on all its group members.
7494 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7495 should be used to exclude the search - the interactive user can
7496 also press `-' or `+' to switch between filtering and excluding."
7497 (interactive "P")
7498 (let* ((alist org-tag-alist-for-agenda)
7499 (tag-chars (mapconcat
7500 (lambda (x) (if (and (not (symbolp (car x)))
7501 (cdr x))
7502 (char-to-string (cdr x))
7503 ""))
7504 org-tag-alist-for-agenda ""))
7505 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7506 (string-to-list tag-chars)))
7507 (exclude (or exclude (equal arg '(4))))
7508 (expand (not (equal arg '(16))))
7509 (inhibit-read-only t)
7510 (current org-agenda-tag-filter)
7511 a n tag)
7512 (unless char
7513 (while (not (memq char valid-char-list))
7514 (message
7515 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7516 (if exclude "Exclude" "Filter") tag-chars
7517 (if org-agenda-auto-exclude-function "[RET], " "")
7518 (if expand "" ", no grouptag expand"))
7519 (setq char (read-char-exclusive))
7520 ;; Excluding or filtering down
7521 (cond ((eq char ?-) (setq exclude t))
7522 ((eq char ?+) (setq exclude nil)))))
7523 (when (eq char ?\t)
7524 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7525 (setq-local org-global-tags-completion-table
7526 (org-global-tags-completion-table)))
7527 (let ((completion-ignore-case t))
7528 (setq tag (completing-read
7529 "Tag: " org-global-tags-completion-table))))
7530 (cond
7531 ((eq char ?\r)
7532 (org-agenda-filter-show-all-tag)
7533 (when org-agenda-auto-exclude-function
7534 (setq org-agenda-tag-filter nil)
7535 (dolist (tag (org-agenda-get-represented-tags))
7536 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7537 (if modifier
7538 (push modifier org-agenda-tag-filter))))
7539 (if (not (null org-agenda-tag-filter))
7540 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7541 ((eq char ?/)
7542 (org-agenda-filter-show-all-tag)
7543 (when (get 'org-agenda-tag-filter :preset-filter)
7544 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7545 ((eq char ?.)
7546 (setq org-agenda-tag-filter
7547 (mapcar (lambda(tag) (concat "+" tag))
7548 (org-get-at-bol 'tags)))
7549 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7550 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7551 ((or (eq char ?\s)
7552 (setq a (rassoc char alist))
7553 (and tag (setq a (cons tag nil))))
7554 (org-agenda-filter-show-all-tag)
7555 (setq tag (car a))
7556 (setq org-agenda-tag-filter
7557 (cons (concat (if exclude "-" "+") tag)
7558 current))
7559 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7560 (t (error "Invalid tag selection character %c" char)))))
7562 (defun org-agenda-get-represented-tags ()
7563 "Get a list of all tags currently represented in the agenda."
7564 (let (p tags)
7565 (save-excursion
7566 (goto-char (point-min))
7567 (while (setq p (next-single-property-change (point) 'tags))
7568 (goto-char p)
7569 (mapc (lambda (x) (add-to-list 'tags x))
7570 (get-text-property (point) 'tags))))
7571 tags))
7574 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7575 "Create the form that tests a line for agenda filter. Optional
7576 argument EXPAND can be used for the TYPE tag and will expand the
7577 tags in the FILTER if any of the tags in FILTER are grouptags."
7578 (let (f f1)
7579 (cond
7580 ;; Tag filter
7581 ((eq type 'tag)
7582 (setq filter
7583 (delete-dups
7584 (append (get 'org-agenda-tag-filter :preset-filter)
7585 filter)))
7586 (dolist (x filter)
7587 (let ((op (string-to-char x)))
7588 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7589 (setq x (list x)))
7590 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7591 (push f1 f))))
7592 ;; Category filter
7593 ((eq type 'category)
7594 (setq filter
7595 (delete-dups
7596 (append (get 'org-agenda-category-filter :preset-filter)
7597 filter)))
7598 (dolist (x filter)
7599 (if (equal "-" (substring x 0 1))
7600 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7601 (setq f1 (list 'equal (substring x 1) 'cat)))
7602 (push f1 f)))
7603 ;; Regexp filter
7604 ((eq type 'regexp)
7605 (setq filter
7606 (delete-dups
7607 (append (get 'org-agenda-regexp-filter :preset-filter)
7608 filter)))
7609 (dolist (x filter)
7610 (if (equal "-" (substring x 0 1))
7611 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7612 (setq f1 (list 'string-match (substring x 1) 'txt)))
7613 (push f1 f)))
7614 ;; Effort filter
7615 ((eq type 'effort)
7616 (setq filter
7617 (delete-dups
7618 (append (get 'org-agenda-effort-filter :preset-filter)
7619 filter)))
7620 (dolist (x filter)
7621 (push (org-agenda-filter-effort-form x) f))))
7622 (cons 'and (nreverse f))))
7624 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7625 "Return a form associated to tag-expression TAGS.
7626 Build a form testing a line for agenda filter for
7627 tag-expressions. OP is an operator of type CHAR that allows the
7628 function to set the right switches in the returned form."
7629 (let (form)
7630 ;; Any of the expressions can match if OP is +, all must match if
7631 ;; the operator is -.
7632 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7633 (let* ((tag (substring x 1))
7634 (f (cond
7635 ((string= "" tag) '(not tags))
7636 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7637 ;; TAG is a regexp.
7638 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7639 (t (list 'member (downcase tag) 'tags)))))
7640 (push (if (eq op ?-) (list 'not f) f) form)))))
7642 (defun org-agenda-filter-effort-form (e)
7643 "Return the form to compare the effort of the current line with what E says.
7644 E looks like \"+<2:25\"."
7645 (let (op)
7646 (setq e (substring e 1))
7647 (setq op (string-to-char e) e (substring e 1))
7648 (setq op (cond ((equal op ?<) '<=)
7649 ((equal op ?>) '>=)
7650 ((equal op ??) op)
7651 (t '=)))
7652 (list 'org-agenda-compare-effort (list 'quote op)
7653 (org-duration-string-to-minutes e))))
7655 (defun org-agenda-compare-effort (op value)
7656 "Compare the effort of the current line with VALUE, using OP.
7657 If the line does not have an effort defined, return nil."
7658 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7659 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7660 value)))
7662 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7663 "Expand group tags in FILTER for the agenda.
7664 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7665 (if org-group-tags
7666 (let ((case-fold-search t) rtn)
7667 (mapc
7668 (lambda (f)
7669 (let (f0 dir)
7670 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7671 (setq dir (match-string 1 f) f0 (match-string 2 f))
7672 (setq dir (if no-operator "" "+") f0 f))
7673 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7674 (org-tags-expand f0 t t))
7675 rtn))))
7676 filter)
7677 (reverse rtn))
7678 filter))
7680 (defun org-agenda-filter-apply (filter type &optional expand)
7681 "Set FILTER as the new agenda filter and apply it. Optional
7682 argument EXPAND can be used for the TYPE tag and will expand the
7683 tags in the FILTER if any of the tags in FILTER are grouptags."
7684 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7685 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7686 (let (tags cat txt)
7687 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7688 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7689 ;; category is used as the filter:
7690 (setq org-agenda-filtered-by-category
7691 (and (eq type 'category)
7692 (not (equal (substring (car filter) 0 1) "-"))))
7693 (org-agenda-set-mode-name)
7694 (save-excursion
7695 (goto-char (point-min))
7696 (while (not (eobp))
7697 (if (org-get-at-bol 'org-marker)
7698 (progn
7699 (setq tags (org-get-at-bol 'tags)
7700 cat (org-get-at-eol 'org-category 1)
7701 txt (org-get-at-bol 'txt))
7702 (if (not (eval org-agenda-filter-form))
7703 (org-agenda-filter-hide-line type))
7704 (beginning-of-line 2))
7705 (beginning-of-line 2))))
7706 (if (get-char-property (point) 'invisible)
7707 (ignore-errors (org-agenda-previous-line)))))
7709 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7710 "Filter by top headline HL."
7711 (org-agenda-set-mode-name)
7712 (save-excursion
7713 (goto-char (point-min))
7714 (while (not (eobp))
7715 (let* ((pos (org-get-at-bol 'org-hd-marker))
7716 (tophl (and pos (org-find-top-headline pos))))
7717 (if (and tophl (funcall (if negative 'identity 'not)
7718 (string= hl tophl)))
7719 (org-agenda-filter-hide-line 'top-headline)))
7720 (beginning-of-line 2)))
7721 (if (get-char-property (point) 'invisible)
7722 (org-agenda-previous-line))
7723 (setq org-agenda-top-headline-filter hl
7724 org-agenda-filtered-by-top-headline t))
7726 (defun org-agenda-filter-hide-line (type)
7727 "Hide lines with TYPE in the agenda buffer."
7728 (let* ((b (max (point-min) (1- (point-at-bol))))
7729 (e (point-at-eol)))
7730 (let ((inhibit-read-only t))
7731 (add-text-properties
7732 b e `(invisible org-filtered org-filter-type ,type)))))
7734 (defun org-agenda-remove-filter (type)
7735 (interactive)
7736 "Remove filter of type TYPE from the agenda buffer."
7737 (save-excursion
7738 (goto-char (point-min))
7739 (let ((inhibit-read-only t) pos)
7740 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7741 (goto-char pos)
7742 (remove-text-properties
7743 (point) (next-single-property-change (point) 'org-filter-type)
7744 `(invisible org-filtered org-filter-type ,type))))
7745 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7746 (setq org-agenda-filter-form nil)
7747 (org-agenda-set-mode-name)
7748 (org-agenda-finalize)))
7750 (defun org-agenda-filter-show-all-tag nil
7751 (org-agenda-remove-filter 'tag))
7752 (defun org-agenda-filter-show-all-re nil
7753 (org-agenda-remove-filter 'regexp))
7754 (defun org-agenda-filter-show-all-effort nil
7755 (org-agenda-remove-filter 'effort))
7756 (defun org-agenda-filter-show-all-cat nil
7757 (org-agenda-remove-filter 'category))
7758 (defun org-agenda-filter-show-all-top-filter nil
7759 (org-agenda-remove-filter 'top-headline))
7761 (defun org-agenda-manipulate-query-add ()
7762 "Manipulate the query by adding a search term with positive selection.
7763 Positive selection means the term must be matched for selection of an entry."
7764 (interactive)
7765 (org-agenda-manipulate-query ?\[))
7766 (defun org-agenda-manipulate-query-subtract ()
7767 "Manipulate the query by adding a search term with negative selection.
7768 Negative selection means term must not be matched for selection of an entry."
7769 (interactive)
7770 (org-agenda-manipulate-query ?\]))
7771 (defun org-agenda-manipulate-query-add-re ()
7772 "Manipulate the query by adding a search regexp with positive selection.
7773 Positive selection means the regexp must match for selection of an entry."
7774 (interactive)
7775 (org-agenda-manipulate-query ?\{))
7776 (defun org-agenda-manipulate-query-subtract-re ()
7777 "Manipulate the query by adding a search regexp with negative selection.
7778 Negative selection means regexp must not match for selection of an entry."
7779 (interactive)
7780 (org-agenda-manipulate-query ?\}))
7781 (defun org-agenda-manipulate-query (char)
7782 (cond
7783 ((memq org-agenda-type '(timeline agenda))
7784 (let ((org-agenda-include-inactive-timestamps t))
7785 (org-agenda-redo))
7786 (message "Display now includes inactive timestamps as well"))
7787 ((eq org-agenda-type 'search)
7788 (org-add-to-string
7789 'org-agenda-query-string
7790 (if org-agenda-last-search-view-search-was-boolean
7791 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7792 (?\{ . " +{}") (?\} . " -{}"))))
7793 " "))
7794 (setq org-agenda-redo-command
7795 (list 'org-search-view
7796 (car (get-text-property (min (1- (point-max)) (point))
7797 'org-last-args))
7798 org-agenda-query-string
7799 (+ (length org-agenda-query-string)
7800 (if (member char '(?\{ ?\})) 0 1))))
7801 (set-register org-agenda-query-register org-agenda-query-string)
7802 (let ((org-agenda-overriding-arguments
7803 (cdr org-agenda-redo-command)))
7804 (org-agenda-redo)))
7805 (t (error "Cannot manipulate query for %s-type agenda buffers"
7806 org-agenda-type))))
7808 (defun org-add-to-string (var string)
7809 (set var (concat (symbol-value var) string)))
7811 (defun org-agenda-goto-date (span)
7812 "Jump to DATE in agenda."
7813 (interactive "P")
7814 (let* ((org-read-date-prefer-future
7815 (eval org-agenda-jump-prefer-future))
7816 (date (org-read-date))
7817 (day (time-to-days (org-time-string-to-time date)))
7818 (org-agenda-sticky-orig org-agenda-sticky)
7819 (org-agenda-buffer-tmp-name (buffer-name))
7820 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7821 (0-arg (or current-prefix-arg (car args)))
7822 (2-arg (nth 2 args))
7823 (with-hour-p (nth 4 org-agenda-redo-command))
7824 (newcmd (list 'org-agenda-list 0-arg date
7825 (org-agenda-span-to-ndays
7826 2-arg (org-time-string-to-absolute date))
7827 with-hour-p))
7828 (newargs (cdr newcmd))
7829 (inhibit-read-only t)
7830 org-agenda-sticky)
7831 (if (not (org-agenda-check-type t 'agenda))
7832 (error "Not available in non-agenda views")
7833 (add-text-properties (point-min) (point-max)
7834 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7835 (org-agenda-redo)
7836 (goto-char (point-min))
7837 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7838 (save-excursion (move-beginning-of-line 2) (eobp))))
7839 (move-beginning-of-line 2))
7840 (setq org-agenda-sticky org-agenda-sticky-orig
7841 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7843 (defun org-agenda-goto-today ()
7844 "Go to today."
7845 (interactive)
7846 (org-agenda-check-type t 'timeline 'agenda)
7847 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7848 (curspan (nth 2 args))
7849 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7850 (cond
7851 (tdpos (goto-char tdpos))
7852 ((eq org-agenda-type 'agenda)
7853 (let* ((sd (org-agenda-compute-starting-span
7854 (org-today) (or curspan org-agenda-span)))
7855 (org-agenda-overriding-arguments args))
7856 (setf (nth 1 org-agenda-overriding-arguments) sd)
7857 (org-agenda-redo)
7858 (org-agenda-find-same-or-today-or-agenda)))
7859 (t (error "Cannot find today")))))
7861 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7862 (goto-char
7863 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7864 (text-property-any (point-min) (point-max) 'org-today t)
7865 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7866 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7867 (org-agenda-backward-block))
7868 (point-min))))
7870 (defun org-agenda-backward-block ()
7871 "Move backward by one agenda block."
7872 (interactive)
7873 (org-agenda-forward-block 'backward))
7875 (defun org-agenda-forward-block (&optional backward)
7876 "Move forward by one agenda block.
7877 When optional argument BACKWARD is set, go backward"
7878 (interactive)
7879 (cond ((not (derived-mode-p 'org-agenda-mode))
7880 (user-error
7881 "Cannot execute this command outside of org-agenda-mode buffers"))
7882 ((looking-at (if backward "\\`" "\\'"))
7883 (message "Already at the %s block" (if backward "first" "last")))
7884 (t (let ((pos (prog1 (point)
7885 (ignore-errors (if backward (backward-char 1)
7886 (move-end-of-line 1)))))
7887 (f (if backward
7888 'previous-single-property-change
7889 'next-single-property-change))
7890 moved dest)
7891 (while (and (setq dest (funcall
7892 f (point) 'org-agenda-structural-header))
7893 (not (get-text-property
7894 (point) 'org-agenda-structural-header)))
7895 (setq moved t)
7896 (goto-char dest))
7897 (if moved (move-beginning-of-line 1)
7898 (goto-char (if backward (point-min) (point-max)))
7899 (move-beginning-of-line 1)
7900 (message "No %s block" (if backward "previous" "further")))))))
7902 (defun org-agenda-later (arg)
7903 "Go forward in time by the current span.
7904 With prefix ARG, go forward that many times the current span."
7905 (interactive "p")
7906 (org-agenda-check-type t 'agenda)
7907 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7908 (span (or (nth 2 args) org-agenda-current-span))
7909 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7910 (greg (calendar-gregorian-from-absolute sd))
7911 (cnt (org-get-at-bol 'org-day-cnt))
7912 greg2)
7913 (cond
7914 ((numberp span)
7915 (setq sd (+ (* span arg) sd)))
7916 ((eq span 'day)
7917 (setq sd (+ arg sd)))
7918 ((eq span 'week)
7919 (setq sd (+ (* 7 arg) sd)))
7920 ((eq span 'fortnight)
7921 (setq sd (+ (* 14 arg) sd)))
7922 ((eq span 'month)
7923 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7924 sd (calendar-absolute-from-gregorian greg2))
7925 (setcar greg2 (1+ (car greg2))))
7926 ((eq span 'year)
7927 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7928 sd (calendar-absolute-from-gregorian greg2))
7929 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7931 (setq sd (+ (* span arg) sd))))
7932 (let ((org-agenda-overriding-cmd
7933 ;; `cmd' may have been set by `org-agenda-run-series' which
7934 ;; uses `org-agenda-overriding-cmd' to decide whether
7935 ;; overriding is allowed for `cmd'
7936 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7937 (org-agenda-overriding-arguments
7938 (list (car args) sd span)))
7939 (org-agenda-redo)
7940 (org-agenda-find-same-or-today-or-agenda cnt))))
7942 (defun org-agenda-earlier (arg)
7943 "Go backward in time by the current span.
7944 With prefix ARG, go backward that many times the current span."
7945 (interactive "p")
7946 (org-agenda-later (- arg)))
7948 (defun org-agenda-view-mode-dispatch ()
7949 "Call one of the view mode commands."
7950 (interactive)
7951 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7952 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7953 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7954 (pcase (read-char-exclusive)
7955 (?\ (call-interactively 'org-agenda-reset-view))
7956 (?d (call-interactively 'org-agenda-day-view))
7957 (?w (call-interactively 'org-agenda-week-view))
7958 (?t (call-interactively 'org-agenda-fortnight-view))
7959 (?m (call-interactively 'org-agenda-month-view))
7960 (?y (call-interactively 'org-agenda-year-view))
7961 (?l (call-interactively 'org-agenda-log-mode))
7962 (?L (org-agenda-log-mode '(4)))
7963 (?c (org-agenda-log-mode 'clockcheck))
7964 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7965 (?a (call-interactively 'org-agenda-archives-mode))
7966 (?A (org-agenda-archives-mode 'files))
7967 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7968 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7969 (?G (call-interactively 'org-agenda-toggle-time-grid))
7970 (?D (call-interactively 'org-agenda-toggle-diary))
7971 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7972 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7973 (org-agenda-check-type t 'timeline 'agenda)
7974 (org-agenda-redo))
7975 (message "Display now includes inactive timestamps as well"))
7976 (?q (message "Abort"))
7977 (key (user-error "Invalid key: %s" key))))
7979 (defun org-agenda-reset-view ()
7980 "Switch to default view for agenda."
7981 (interactive)
7982 (org-agenda-change-time-span org-agenda-span))
7983 (defun org-agenda-day-view (&optional day-of-month)
7984 "Switch to daily view for agenda.
7985 With argument DAY-OF-MONTH, switch to that day of the month."
7986 (interactive "P")
7987 (org-agenda-change-time-span 'day day-of-month))
7988 (defun org-agenda-week-view (&optional iso-week)
7989 "Switch to daily view for agenda.
7990 With argument ISO-WEEK, switch to the corresponding ISO week.
7991 If ISO-WEEK has more then 2 digits, only the last two encode the
7992 week. Any digits before this encode a year. So 200712 means
7993 week 12 of year 2007. Years in the range 1938-2037 can also be
7994 written as 2-digit years."
7995 (interactive "P")
7996 (org-agenda-change-time-span 'week iso-week))
7997 (defun org-agenda-fortnight-view (&optional iso-week)
7998 "Switch to daily view for agenda.
7999 With argument ISO-WEEK, switch to the corresponding ISO week.
8000 If ISO-WEEK has more then 2 digits, only the last two encode the
8001 week. Any digits before this encode a year. So 200712 means
8002 week 12 of year 2007. Years in the range 1938-2037 can also be
8003 written as 2-digit years."
8004 (interactive "P")
8005 (org-agenda-change-time-span 'fortnight iso-week))
8006 (defun org-agenda-month-view (&optional month)
8007 "Switch to monthly view for agenda.
8008 With argument MONTH, switch to that month."
8009 (interactive "P")
8010 (org-agenda-change-time-span 'month month))
8011 (defun org-agenda-year-view (&optional year)
8012 "Switch to yearly view for agenda.
8013 With argument YEAR, switch to that year.
8014 If MONTH has more then 2 digits, only the last two encode the
8015 month. Any digits before this encode a year. So 200712 means
8016 December year 2007. Years in the range 1938-2037 can also be
8017 written as 2-digit years."
8018 (interactive "P")
8019 (when year
8020 (setq year (org-small-year-to-year year)))
8021 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8022 (org-agenda-change-time-span 'year year)
8023 (error "Abort")))
8025 (defun org-agenda-change-time-span (span &optional n)
8026 "Change the agenda view to SPAN.
8027 SPAN may be `day', `week', `fortnight', `month', `year'."
8028 (org-agenda-check-type t 'agenda)
8029 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8030 (curspan (nth 2 args)))
8031 (if (and (not n) (equal curspan span))
8032 (error "Viewing span is already \"%s\"" span))
8033 (let* ((sd (or (org-get-at-bol 'day)
8034 (nth 1 args)
8035 org-starting-day))
8036 (sd (org-agenda-compute-starting-span sd span n))
8037 (org-agenda-overriding-cmd
8038 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8039 (org-agenda-overriding-arguments
8040 (list (car args) sd span)))
8041 (org-agenda-redo)
8042 (org-agenda-find-same-or-today-or-agenda))
8043 (org-agenda-set-mode-name)
8044 (message "Switched to %s view" span)))
8046 (defun org-agenda-compute-starting-span (sd span &optional n)
8047 "Compute starting date for agenda.
8048 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8049 is a cons cell with the starting date and the number of days,
8050 so that the date SD will be in that range."
8051 (let* ((greg (calendar-gregorian-from-absolute sd))
8052 (dg (nth 1 greg))
8053 (mg (car greg))
8054 (yg (nth 2 greg)))
8055 (cond
8056 ((eq span 'day)
8057 (when n
8058 (setq sd (+ (calendar-absolute-from-gregorian
8059 (list mg 1 yg))
8060 n -1))))
8061 ((or (eq span 'week) (eq span 'fortnight))
8062 (let* ((nt (calendar-day-of-week
8063 (calendar-gregorian-from-absolute sd)))
8064 (d (if org-agenda-start-on-weekday
8065 (- nt org-agenda-start-on-weekday)
8068 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8069 (when n
8070 (require 'cal-iso)
8071 (when (> n 99)
8072 (setq y1 (org-small-year-to-year (/ n 100))
8073 n (mod n 100)))
8074 (setq sd
8075 (calendar-iso-to-absolute
8076 (list n 1
8077 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8078 ((eq span 'month)
8079 (let (y1)
8080 (when (and n (> n 99))
8081 (setq y1 (org-small-year-to-year (/ n 100))
8082 n (mod n 100)))
8083 (setq sd (calendar-absolute-from-gregorian
8084 (list (or n mg) 1 (or y1 yg))))))
8085 ((eq span 'year)
8086 (setq sd (calendar-absolute-from-gregorian
8087 (list 1 1 (or n yg))))))
8088 sd))
8090 (defun org-agenda-next-date-line (&optional arg)
8091 "Jump to the next line indicating a date in agenda buffer."
8092 (interactive "p")
8093 (org-agenda-check-type t 'agenda 'timeline)
8094 (beginning-of-line 1)
8095 ;; This does not work if user makes date format that starts with a blank
8096 (if (looking-at "^\\S-") (forward-char 1))
8097 (if (not (re-search-forward "^\\S-" nil t arg))
8098 (progn
8099 (backward-char 1)
8100 (error "No next date after this line in this buffer")))
8101 (goto-char (match-beginning 0)))
8103 (defun org-agenda-previous-date-line (&optional arg)
8104 "Jump to the previous line indicating a date in agenda buffer."
8105 (interactive "p")
8106 (org-agenda-check-type t 'agenda 'timeline)
8107 (beginning-of-line 1)
8108 (if (not (re-search-backward "^\\S-" nil t arg))
8109 (error "No previous date before this line in this buffer")))
8111 ;; Initialize the highlight
8112 (defvar org-hl (make-overlay 1 1))
8113 (overlay-put org-hl 'face 'highlight)
8115 (defun org-highlight (begin end &optional buffer)
8116 "Highlight a region with overlay."
8117 (move-overlay org-hl begin end (or buffer (current-buffer))))
8119 (defun org-unhighlight ()
8120 "Detach overlay INDEX."
8121 (delete-overlay org-hl))
8123 (defun org-unhighlight-once ()
8124 "Remove the highlight from its position, and this function from the hook."
8125 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8126 (org-unhighlight))
8128 (defvar org-agenda-pre-follow-window-conf nil)
8129 (defun org-agenda-follow-mode ()
8130 "Toggle follow mode in an agenda buffer."
8131 (interactive)
8132 (unless org-agenda-follow-mode
8133 (setq org-agenda-pre-follow-window-conf
8134 (current-window-configuration)))
8135 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8136 (unless org-agenda-follow-mode
8137 (set-window-configuration org-agenda-pre-follow-window-conf))
8138 (org-agenda-set-mode-name)
8139 (org-agenda-do-context-action)
8140 (message "Follow mode is %s"
8141 (if org-agenda-follow-mode "on" "off")))
8143 (defun org-agenda-entry-text-mode (&optional arg)
8144 "Toggle entry text mode in an agenda buffer."
8145 (interactive "P")
8146 (if (or org-agenda-tag-filter
8147 org-agenda-category-filter
8148 org-agenda-regexp-filter
8149 org-agenda-top-headline-filter)
8150 (user-error "Can't show entry text in filtered views")
8151 (setq org-agenda-entry-text-mode (or (integerp arg)
8152 (not org-agenda-entry-text-mode)))
8153 (org-agenda-entry-text-hide)
8154 (and org-agenda-entry-text-mode
8155 (let ((org-agenda-entry-text-maxlines
8156 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8157 (org-agenda-entry-text-show)))
8158 (org-agenda-set-mode-name)
8159 (message "Entry text mode is %s%s"
8160 (if org-agenda-entry-text-mode "on" "off")
8161 (if (not org-agenda-entry-text-mode) ""
8162 (format " (maximum number of lines is %d)"
8163 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8165 (defun org-agenda-clockreport-mode ()
8166 "Toggle clocktable mode in an agenda buffer."
8167 (interactive)
8168 (org-agenda-check-type t 'agenda)
8169 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8170 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8171 (org-agenda-set-mode-name)
8172 (org-agenda-redo)
8173 (message "Clocktable mode is %s"
8174 (if org-agenda-clockreport-mode "on" "off")))
8176 (defun org-agenda-log-mode (&optional special)
8177 "Toggle log mode in an agenda buffer.
8179 With argument SPECIAL, show all possible log items, not only the ones
8180 configured in `org-agenda-log-mode-items'.
8182 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8183 log items, nothing else."
8184 (interactive "P")
8185 (org-agenda-check-type t 'agenda 'timeline)
8186 (setq org-agenda-show-log
8187 (cond
8188 ((equal special '(16)) 'only)
8189 ((eq special 'clockcheck)
8190 (if (eq org-agenda-show-log 'clockcheck)
8191 nil 'clockcheck))
8192 (special '(closed clock state))
8193 (t (not org-agenda-show-log))))
8194 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8195 (org-agenda-set-mode-name)
8196 (org-agenda-redo)
8197 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8199 (defun org-agenda-archives-mode (&optional with-files)
8200 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8201 When called with a prefix argument, include all archive files as well."
8202 (interactive "P")
8203 (setq org-agenda-archives-mode
8204 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8205 (org-agenda-set-mode-name)
8206 (org-agenda-redo)
8207 (message
8208 "%s"
8209 (cond
8210 ((eq org-agenda-archives-mode nil)
8211 "No archives are included")
8212 ((eq org-agenda-archives-mode 'trees)
8213 (format "Trees with :%s: tag are included" org-archive-tag))
8214 ((eq org-agenda-archives-mode t)
8215 (format "Trees with :%s: tag and all active archive files are included"
8216 org-archive-tag)))))
8218 (defun org-agenda-toggle-diary ()
8219 "Toggle diary inclusion in an agenda buffer."
8220 (interactive)
8221 (org-agenda-check-type t 'agenda)
8222 (setq org-agenda-include-diary (not org-agenda-include-diary))
8223 (org-agenda-redo)
8224 (org-agenda-set-mode-name)
8225 (message "Diary inclusion turned %s"
8226 (if org-agenda-include-diary "on" "off")))
8228 (defun org-agenda-toggle-deadlines ()
8229 "Toggle inclusion of entries with a deadline in an agenda buffer."
8230 (interactive)
8231 (org-agenda-check-type t 'agenda)
8232 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8233 (org-agenda-redo)
8234 (org-agenda-set-mode-name)
8235 (message "Deadlines inclusion turned %s"
8236 (if org-agenda-include-deadlines "on" "off")))
8238 (defun org-agenda-toggle-time-grid ()
8239 "Toggle time grid in an agenda buffer."
8240 (interactive)
8241 (org-agenda-check-type t 'agenda)
8242 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8243 (org-agenda-redo)
8244 (org-agenda-set-mode-name)
8245 (message "Time-grid turned %s"
8246 (if org-agenda-use-time-grid "on" "off")))
8248 (defun org-agenda-set-mode-name ()
8249 "Set the mode name to indicate all the small mode settings."
8250 (setq mode-name
8251 (list "Org-Agenda"
8252 (if (get 'org-agenda-files 'org-restrict) " []" "")
8254 '(:eval (org-agenda-span-name org-agenda-current-span))
8255 (if org-agenda-follow-mode " Follow" "")
8256 (if org-agenda-entry-text-mode " ETxt" "")
8257 (if org-agenda-include-diary " Diary" "")
8258 (if org-agenda-include-deadlines " Ddl" "")
8259 (if org-agenda-use-time-grid " Grid" "")
8260 (if (and (boundp 'org-habit-show-habits)
8261 org-habit-show-habits) " Habit" "")
8262 (cond
8263 ((consp org-agenda-show-log) " LogAll")
8264 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8265 (org-agenda-show-log " Log")
8266 (t ""))
8267 (if (or org-agenda-category-filter
8268 (get 'org-agenda-category-filter :preset-filter))
8269 '(:eval (propertize
8270 (concat " <"
8271 (mapconcat
8272 'identity
8273 (append
8274 (get 'org-agenda-category-filter :preset-filter)
8275 org-agenda-category-filter)
8277 ">")
8278 'face 'org-agenda-filter-category
8279 'help-echo "Category used in filtering")) "")
8280 (if (or org-agenda-tag-filter
8281 (get 'org-agenda-tag-filter :preset-filter))
8282 '(:eval (propertize
8283 (concat " {"
8284 (mapconcat
8285 'identity
8286 (append
8287 (get 'org-agenda-tag-filter :preset-filter)
8288 org-agenda-tag-filter)
8290 "}")
8291 'face 'org-agenda-filter-tags
8292 'help-echo "Tags used in filtering")) "")
8293 (if (or org-agenda-effort-filter
8294 (get 'org-agenda-effort-filter :preset-filter))
8295 '(:eval (propertize
8296 (concat " {"
8297 (mapconcat
8298 'identity
8299 (append
8300 (get 'org-agenda-effort-filter :preset-filter)
8301 org-agenda-effort-filter)
8303 "}")
8304 'face 'org-agenda-filter-effort
8305 'help-echo "Effort conditions used in filtering")) "")
8306 (if (or org-agenda-regexp-filter
8307 (get 'org-agenda-regexp-filter :preset-filter))
8308 '(:eval (propertize
8309 (concat " ["
8310 (mapconcat
8311 'identity
8312 (append
8313 (get 'org-agenda-regexp-filter :preset-filter)
8314 org-agenda-regexp-filter)
8316 "]")
8317 'face 'org-agenda-filter-regexp
8318 'help-echo "Regexp used in filtering")) "")
8319 (if org-agenda-archives-mode
8320 (if (eq org-agenda-archives-mode t)
8321 " Archives"
8322 (format " :%s:" org-archive-tag))
8324 (if org-agenda-clockreport-mode " Clock" "")))
8325 (force-mode-line-update))
8327 (defun org-agenda-update-agenda-type ()
8328 "Update the agenda type after each command."
8329 (setq org-agenda-type
8330 (or (get-text-property (point) 'org-agenda-type)
8331 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8333 (defun org-agenda-next-line ()
8334 "Move cursor to the next line, and show if follow mode is active."
8335 (interactive)
8336 (call-interactively 'next-line)
8337 (org-agenda-do-context-action))
8339 (defun org-agenda-previous-line ()
8340 "Move cursor to the previous line, and show if follow-mode is active."
8341 (interactive)
8342 (call-interactively 'previous-line)
8343 (org-agenda-do-context-action))
8345 (defun org-agenda-next-item (n)
8346 "Move cursor to next agenda item."
8347 (interactive "p")
8348 (let ((col (current-column)))
8349 (dotimes (c n)
8350 (when (next-single-property-change (point-at-eol) 'org-marker)
8351 (move-end-of-line 1)
8352 (goto-char (next-single-property-change (point) 'org-marker))))
8353 (org-move-to-column col))
8354 (org-agenda-do-context-action))
8356 (defun org-agenda-previous-item (n)
8357 "Move cursor to next agenda item."
8358 (interactive "p")
8359 (dotimes (c n)
8360 (let ((col (current-column))
8361 (goto (save-excursion
8362 (move-end-of-line 0)
8363 (previous-single-property-change (point) 'org-marker))))
8364 (if goto (goto-char goto))
8365 (org-move-to-column col)))
8366 (org-agenda-do-context-action))
8368 (defun org-agenda-do-context-action ()
8369 "Show outline path and, maybe, follow mode window."
8370 (let ((m (org-get-at-bol 'org-marker)))
8371 (when (and (markerp m) (marker-buffer m))
8372 (and org-agenda-follow-mode
8373 (if org-agenda-follow-indirect
8374 (org-agenda-tree-to-indirect-buffer nil)
8375 (org-agenda-show)))
8376 (and org-agenda-show-outline-path
8377 (org-with-point-at m (org-display-outline-path t))))))
8379 (defun org-agenda-show-tags ()
8380 "Show the tags applicable to the current item."
8381 (interactive)
8382 (let* ((tags (org-get-at-bol 'tags)))
8383 (if tags
8384 (message "Tags are :%s:"
8385 (org-no-properties (mapconcat 'identity tags ":")))
8386 (message "No tags associated with this line"))))
8388 (defun org-agenda-goto (&optional highlight)
8389 "Go to the entry at point in the corresponding Org file."
8390 (interactive)
8391 (let* ((marker (or (org-get-at-bol 'org-marker)
8392 (org-agenda-error)))
8393 (buffer (marker-buffer marker))
8394 (pos (marker-position marker)))
8395 (switch-to-buffer-other-window buffer)
8396 (widen)
8397 (push-mark)
8398 (goto-char pos)
8399 (when (derived-mode-p 'org-mode)
8400 (org-show-context 'agenda)
8401 (recenter (/ (window-height) 2))
8402 (org-back-to-heading t)
8403 (if (re-search-forward org-complex-heading-regexp nil t)
8404 (goto-char (match-beginning 4))))
8405 (run-hooks 'org-agenda-after-show-hook)
8406 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8408 (defvar org-agenda-after-show-hook nil
8409 "Normal hook run after an item has been shown from the agenda.
8410 Point is in the buffer where the item originated.")
8412 (defun org-agenda-kill ()
8413 "Kill the entry or subtree belonging to the current agenda entry."
8414 (interactive)
8415 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8416 (let* ((bufname-orig (buffer-name))
8417 (marker (or (org-get-at-bol 'org-marker)
8418 (org-agenda-error)))
8419 (buffer (marker-buffer marker))
8420 (pos (marker-position marker))
8421 (type (org-get-at-bol 'type))
8422 dbeg dend (n 0) conf)
8423 (org-with-remote-undo buffer
8424 (with-current-buffer buffer
8425 (save-excursion
8426 (goto-char pos)
8427 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8428 (setq dbeg (progn (org-back-to-heading t) (point))
8429 dend (org-end-of-subtree t t))
8430 (setq dbeg (point-at-bol)
8431 dend (min (point-max) (1+ (point-at-eol)))))
8432 (goto-char dbeg)
8433 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8434 (setq conf (or (eq t org-agenda-confirm-kill)
8435 (and (numberp org-agenda-confirm-kill)
8436 (> n org-agenda-confirm-kill))))
8437 (and conf
8438 (not (y-or-n-p
8439 (format "Delete entry with %d lines in buffer \"%s\"? "
8440 n (buffer-name buffer))))
8441 (error "Abort"))
8442 (let ((org-agenda-buffer-name bufname-orig))
8443 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8444 (with-current-buffer buffer (delete-region dbeg dend))
8445 (message "Agenda item and source killed"))))
8447 (defvar org-archive-default-command) ; defined in org-archive.el
8448 (defun org-agenda-archive-default ()
8449 "Archive the entry or subtree belonging to the current agenda entry."
8450 (interactive)
8451 (require 'org-archive)
8452 (org-agenda-archive-with org-archive-default-command))
8454 (defun org-agenda-archive-default-with-confirmation ()
8455 "Archive the entry or subtree belonging to the current agenda entry."
8456 (interactive)
8457 (require 'org-archive)
8458 (org-agenda-archive-with org-archive-default-command 'confirm))
8460 (defun org-agenda-archive ()
8461 "Archive the entry or subtree belonging to the current agenda entry."
8462 (interactive)
8463 (org-agenda-archive-with 'org-archive-subtree))
8465 (defun org-agenda-archive-to-archive-sibling ()
8466 "Move the entry to the archive sibling."
8467 (interactive)
8468 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8470 (defun org-agenda-archive-with (cmd &optional confirm)
8471 "Move the entry to the archive sibling."
8472 (interactive)
8473 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8474 (let* ((bufname-orig (buffer-name))
8475 (marker (or (org-get-at-bol 'org-marker)
8476 (org-agenda-error)))
8477 (buffer (marker-buffer marker))
8478 (pos (marker-position marker)))
8479 (org-with-remote-undo buffer
8480 (with-current-buffer buffer
8481 (if (derived-mode-p 'org-mode)
8482 (if (and confirm
8483 (not (y-or-n-p "Archive this subtree or entry? ")))
8484 (error "Abort")
8485 (save-window-excursion
8486 (goto-char pos)
8487 (let ((org-agenda-buffer-name bufname-orig))
8488 (org-remove-subtree-entries-from-agenda))
8489 (org-back-to-heading t)
8490 (funcall cmd)))
8491 (error "Archiving works only in Org files"))))))
8493 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8494 "Remove all lines in the agenda that correspond to a given subtree.
8495 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8496 If this information is not given, the function uses the tree at point."
8497 (let ((buf (or buf (current-buffer))) m p)
8498 (save-excursion
8499 (unless (and beg end)
8500 (org-back-to-heading t)
8501 (setq beg (point))
8502 (org-end-of-subtree t)
8503 (setq end (point)))
8504 (set-buffer (get-buffer org-agenda-buffer-name))
8505 (save-excursion
8506 (goto-char (point-max))
8507 (beginning-of-line 1)
8508 (while (not (bobp))
8509 (when (and (setq m (org-get-at-bol 'org-marker))
8510 (equal buf (marker-buffer m))
8511 (setq p (marker-position m))
8512 (>= p beg)
8513 (< p end))
8514 (let ((inhibit-read-only t))
8515 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8516 (beginning-of-line 0))))))
8518 (defun org-agenda-refile (&optional goto rfloc no-update)
8519 "Refile the item at point.
8521 When called with `\\[universal-argument] \\[universal-argument]', \
8522 go to the location of the last
8523 refiled item.
8525 When called with `\\[universal-argument] \\[universal-argument] \
8526 \\[universal-argument]' prefix or when GOTO is 0, clear
8527 the refile cache.
8529 RFLOC can be a refile location obtained in a different way.
8531 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8532 (interactive "P")
8533 (cond
8534 ((member goto '(0 (64)))
8535 (org-refile-cache-clear))
8536 ((equal goto '(16))
8537 (org-refile-goto-last-stored))
8539 (let* ((buffer-orig (buffer-name))
8540 (marker (or (org-get-at-bol 'org-hd-marker)
8541 (org-agenda-error)))
8542 (buffer (marker-buffer marker))
8543 (pos (marker-position marker))
8544 (rfloc (or rfloc
8545 (org-refile-get-location
8546 (if goto "Goto" "Refile to") buffer
8547 org-refile-allow-creating-parent-nodes))))
8548 (with-current-buffer buffer
8549 (org-with-wide-buffer
8550 (goto-char marker)
8551 (let ((org-agenda-buffer-name buffer-orig))
8552 (org-remove-subtree-entries-from-agenda))
8553 (org-refile goto buffer rfloc))))
8554 (unless no-update (org-agenda-redo)))))
8556 (defun org-agenda-open-link (&optional arg)
8557 "Open the link(s) in the current entry, if any.
8558 This looks for a link in the displayed line in the agenda.
8559 It also looks at the text of the entry itself."
8560 (interactive "P")
8561 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8562 (org-get-at-bol 'org-marker)))
8563 (buffer (and marker (marker-buffer marker)))
8564 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8565 (lkall (and buffer (org-offer-links-in-entry
8566 buffer marker arg prefix)))
8567 (lk0 (car lkall))
8568 (lk (if (stringp lk0) (list lk0) lk0))
8569 (lkend (cdr lkall))
8570 trg)
8571 (cond
8572 ((and buffer lk)
8573 (mapcar (lambda(l)
8574 (with-current-buffer buffer
8575 (setq trg (and (string-match org-bracket-link-regexp l)
8576 (match-string 1 l)))
8577 (if (or (not trg) (string-match org-any-link-re trg))
8578 (org-with-wide-buffer
8579 (goto-char marker)
8580 (when (search-forward l nil lkend)
8581 (goto-char (match-beginning 0))
8582 (org-open-at-point)))
8583 ;; This is an internal link, widen the buffer
8584 (switch-to-buffer-other-window buffer)
8585 (widen)
8586 (goto-char marker)
8587 (when (search-forward l nil lkend)
8588 (goto-char (match-beginning 0))
8589 (org-open-at-point)))))
8590 lk))
8591 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8592 (save-excursion
8593 (beginning-of-line 1)
8594 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8595 (org-open-link-from-string (match-string 1)))
8596 (t (message "No link to open here")))))
8598 (defun org-agenda-copy-local-variable (var)
8599 "Get a variable from a referenced buffer and install it here."
8600 (let ((m (org-get-at-bol 'org-marker)))
8601 (when (and m (buffer-live-p (marker-buffer m)))
8602 (set (make-local-variable var)
8603 (with-current-buffer (marker-buffer m)
8604 (symbol-value var))))))
8606 (defun org-agenda-switch-to (&optional delete-other-windows)
8607 "Go to the Org mode file which contains the item at point.
8608 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8609 displayed Org file fills the frame."
8610 (interactive)
8611 (if (and org-return-follows-link
8612 (not (org-get-at-bol 'org-marker))
8613 (org-in-regexp org-bracket-link-regexp))
8614 (org-open-link-from-string (match-string 0))
8615 (let* ((marker (or (org-get-at-bol 'org-marker)
8616 (org-agenda-error)))
8617 (buffer (marker-buffer marker))
8618 (pos (marker-position marker)))
8619 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8620 (pop-to-buffer-same-window buffer)
8621 (when delete-other-windows (delete-other-windows))
8622 (widen)
8623 (goto-char pos)
8624 (when (derived-mode-p 'org-mode)
8625 (org-show-context 'agenda)
8626 (run-hooks 'org-agenda-after-show-hook)))))
8628 (defun org-agenda-goto-mouse (ev)
8629 "Go to the Org file which contains the item at the mouse click."
8630 (interactive "e")
8631 (mouse-set-point ev)
8632 (org-agenda-goto))
8634 (defun org-agenda-show (&optional full-entry)
8635 "Display the Org file which contains the item at point.
8636 With prefix argument FULL-ENTRY, make the entire entry visible
8637 if it was hidden in the outline."
8638 (interactive "P")
8639 (let ((win (selected-window)))
8640 (org-agenda-goto t)
8641 (when full-entry (org-show-entry))
8642 (select-window win)))
8644 (defvar org-agenda-show-window nil)
8645 (defun org-agenda-show-and-scroll-up (&optional arg)
8646 "Display the Org file which contains the item at point.
8648 When called repeatedly, scroll the window that is displaying the buffer.
8650 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8651 `outline-show-subtree'
8652 to display the item, so that drawers and logbooks stay folded."
8653 (interactive "P")
8654 (let ((win (selected-window)))
8655 (if (and (window-live-p org-agenda-show-window)
8656 (eq this-command last-command))
8657 (progn
8658 (select-window org-agenda-show-window)
8659 (ignore-errors (scroll-up)))
8660 (org-agenda-goto t)
8661 (if arg (org-show-entry) (outline-show-subtree))
8662 (setq org-agenda-show-window (selected-window)))
8663 (select-window win)))
8665 (defun org-agenda-show-scroll-down ()
8666 "Scroll down the window showing the agenda."
8667 (interactive)
8668 (let ((win (selected-window)))
8669 (when (window-live-p org-agenda-show-window)
8670 (select-window org-agenda-show-window)
8671 (ignore-errors (scroll-down))
8672 (select-window win))))
8674 (defun org-agenda-show-1 (&optional more)
8675 "Display the Org file which contains the item at point.
8676 The prefix arg selects the amount of information to display:
8678 0 hide the subtree
8679 1 just show the entry according to defaults.
8680 2 show the children view
8681 3 show the subtree view
8682 4 show the entire subtree and any LOGBOOK drawers
8683 5 show the entire subtree and any drawers
8684 With prefix argument FULL-ENTRY, make the entire entry visible
8685 if it was hidden in the outline."
8686 (interactive "p")
8687 (let ((win (selected-window)))
8688 (org-agenda-goto t)
8689 (org-back-to-heading)
8690 (set-window-start (selected-window) (point-at-bol))
8691 (cond
8692 ((= more 0)
8693 (outline-hide-subtree)
8694 (save-excursion
8695 (org-back-to-heading)
8696 (run-hook-with-args 'org-cycle-hook 'folded))
8697 (message "Remote: FOLDED"))
8698 ((and (called-interactively-p 'any) (= more 1))
8699 (message "Remote: show with default settings"))
8700 ((= more 2)
8701 (outline-show-entry)
8702 (org-show-children)
8703 (save-excursion
8704 (org-back-to-heading)
8705 (run-hook-with-args 'org-cycle-hook 'children))
8706 (message "Remote: CHILDREN"))
8707 ((= more 3)
8708 (outline-show-subtree)
8709 (save-excursion
8710 (org-back-to-heading)
8711 (run-hook-with-args 'org-cycle-hook 'subtree))
8712 (message "Remote: SUBTREE"))
8713 ((= more 4)
8714 (outline-show-subtree)
8715 (save-excursion
8716 (org-back-to-heading)
8717 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8718 (message "Remote: SUBTREE AND LOGBOOK"))
8719 ((> more 4)
8720 (outline-show-subtree)
8721 (message "Remote: SUBTREE AND ALL DRAWERS")))
8722 (select-window win)))
8724 (defvar org-agenda-cycle-counter nil)
8725 (defun org-agenda-cycle-show (&optional n)
8726 "Show the current entry in another window, with default settings.
8728 Default settings are taken from `org-show-context-detail'. When
8729 use repeatedly in immediate succession, the remote entry will
8730 cycle through visibility
8732 children -> subtree -> folded
8734 When called with a numeric prefix arg, that arg will be passed through to
8735 `org-agenda-show-1'. For the interpretation of that argument, see the
8736 docstring of `org-agenda-show-1'."
8737 (interactive "P")
8738 (if (integerp n)
8739 (setq org-agenda-cycle-counter n)
8740 (if (not (eq last-command this-command))
8741 (setq org-agenda-cycle-counter 1)
8742 (if (equal org-agenda-cycle-counter 0)
8743 (setq org-agenda-cycle-counter 2)
8744 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8745 (if (> org-agenda-cycle-counter 3)
8746 (setq org-agenda-cycle-counter 0)))))
8747 (org-agenda-show-1 org-agenda-cycle-counter))
8749 (defun org-agenda-recenter (arg)
8750 "Display the Org file which contains the item at point and recenter."
8751 (interactive "P")
8752 (let ((win (selected-window)))
8753 (org-agenda-goto t)
8754 (recenter arg)
8755 (select-window win)))
8757 (defun org-agenda-show-mouse (ev)
8758 "Display the Org file which contains the item at the mouse click."
8759 (interactive "e")
8760 (mouse-set-point ev)
8761 (org-agenda-show))
8763 (defun org-agenda-check-no-diary ()
8764 "Check if the entry is a diary link and abort if yes."
8765 (if (org-get-at-bol 'org-agenda-diary-link)
8766 (org-agenda-error)))
8768 (defun org-agenda-error ()
8769 "Throw an error when a command is not allowed in the agenda."
8770 (user-error "Command not allowed in this line"))
8772 (defun org-agenda-tree-to-indirect-buffer (arg)
8773 "Show the subtree corresponding to the current entry in an indirect buffer.
8774 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8776 With a numerical prefix ARG, go up to this level and then take that tree.
8777 With a negative numeric ARG, go up by this number of levels.
8779 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8780 i.e. don't use
8781 the dedicated frame."
8782 (interactive "P")
8783 (if current-prefix-arg
8784 (org-agenda-do-tree-to-indirect-buffer arg)
8785 (let ((agenda-buffer (buffer-name))
8786 (agenda-window (selected-window))
8787 (indirect-window
8788 (and org-last-indirect-buffer
8789 (get-buffer-window org-last-indirect-buffer))))
8790 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8791 (unless (or (eq org-indirect-buffer-display 'new-frame)
8792 (eq org-indirect-buffer-display 'dedicated-frame))
8793 (unwind-protect
8794 (unless (and indirect-window (window-live-p indirect-window))
8795 (setq indirect-window (split-window agenda-window)))
8796 (and indirect-window (select-window indirect-window))
8797 (switch-to-buffer org-last-indirect-buffer :norecord)
8798 (fit-window-to-buffer indirect-window)))
8799 (select-window (get-buffer-window agenda-buffer))
8800 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8802 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8803 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8804 (org-agenda-check-no-diary)
8805 (let* ((marker (or (org-get-at-bol 'org-marker)
8806 (org-agenda-error)))
8807 (buffer (marker-buffer marker))
8808 (pos (marker-position marker)))
8809 (with-current-buffer buffer
8810 (save-excursion
8811 (goto-char pos)
8812 (funcall 'org-tree-to-indirect-buffer arg)))))
8814 (defvar org-last-heading-marker (make-marker)
8815 "Marker pointing to the headline that last changed its TODO state
8816 by a remote command from the agenda.")
8818 (defun org-agenda-todo-nextset ()
8819 "Switch TODO entry to next sequence."
8820 (interactive)
8821 (org-agenda-todo 'nextset))
8823 (defun org-agenda-todo-previousset ()
8824 "Switch TODO entry to previous sequence."
8825 (interactive)
8826 (org-agenda-todo 'previousset))
8828 (defun org-agenda-todo (&optional arg)
8829 "Cycle TODO state of line at point, also in Org file.
8830 This changes the line at point, all other lines in the agenda referring to
8831 the same tree node, and the headline of the tree node in the Org file."
8832 (interactive "P")
8833 (org-agenda-check-no-diary)
8834 (let* ((col (current-column))
8835 (marker (or (org-get-at-bol 'org-marker)
8836 (org-agenda-error)))
8837 (buffer (marker-buffer marker))
8838 (pos (marker-position marker))
8839 (hdmarker (org-get-at-bol 'org-hd-marker))
8840 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8841 (inhibit-read-only t)
8842 org-agenda-headline-snapshot-before-repeat newhead just-one)
8843 (org-with-remote-undo buffer
8844 (with-current-buffer buffer
8845 (widen)
8846 (goto-char pos)
8847 (org-show-context 'agenda)
8848 (let ((current-prefix-arg arg))
8849 (call-interactively 'org-todo))
8850 (and (bolp) (forward-char 1))
8851 (setq newhead (org-get-heading))
8852 (when (and (bound-and-true-p
8853 org-agenda-headline-snapshot-before-repeat)
8854 (not (equal org-agenda-headline-snapshot-before-repeat
8855 newhead))
8856 todayp)
8857 (setq newhead org-agenda-headline-snapshot-before-repeat
8858 just-one t))
8859 (save-excursion
8860 (org-back-to-heading)
8861 (move-marker org-last-heading-marker (point))))
8862 (beginning-of-line 1)
8863 (save-window-excursion
8864 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8865 (when (bound-and-true-p org-clock-out-when-done)
8866 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8867 newhead)
8868 (org-agenda-unmark-clocking-task))
8869 (org-move-to-column col)
8870 (org-agenda-mark-clocking-task))))
8872 (defun org-agenda-add-note (&optional arg)
8873 "Add a time-stamped note to the entry at point."
8874 (interactive "P")
8875 (org-agenda-check-no-diary)
8876 (let* ((marker (or (org-get-at-bol 'org-marker)
8877 (org-agenda-error)))
8878 (buffer (marker-buffer marker))
8879 (pos (marker-position marker))
8880 (hdmarker (org-get-at-bol 'org-hd-marker))
8881 (inhibit-read-only t))
8882 (with-current-buffer buffer
8883 (widen)
8884 (goto-char pos)
8885 (org-show-context 'agenda)
8886 (org-add-note))))
8888 (defun org-agenda-change-all-lines (newhead hdmarker
8889 &optional fixface just-this)
8890 "Change all lines in the agenda buffer which match HDMARKER.
8891 The new content of the line will be NEWHEAD (as modified by
8892 `org-agenda-format-item'). HDMARKER is checked with
8893 `equal' against all `org-hd-marker' text properties in the file.
8894 If FIXFACE is non-nil, the face of each item is modified according to
8895 the new TODO state.
8896 If JUST-THIS is non-nil, change just the current line, not all.
8897 If FORCE-TAGS is non nil, the car of it returns the new tags."
8898 (let* ((inhibit-read-only t)
8899 (line (org-current-line))
8900 (org-agenda-buffer (current-buffer))
8901 (thetags (with-current-buffer (marker-buffer hdmarker)
8902 (org-with-wide-buffer
8903 (goto-char hdmarker)
8904 (org-get-tags-at))))
8905 props m pl undone-face done-face finish new dotime level cat tags)
8906 (save-excursion
8907 (goto-char (point-max))
8908 (beginning-of-line 1)
8909 (while (not finish)
8910 (setq finish (bobp))
8911 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8912 (or (not just-this) (= (org-current-line) line))
8913 (equal m hdmarker))
8914 (setq props (text-properties-at (point))
8915 dotime (org-get-at-bol 'dotime)
8916 cat (org-get-at-eol 'org-category 1)
8917 level (org-get-at-bol 'level)
8918 tags thetags
8920 (let ((org-prefix-format-compiled
8921 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8922 org-prefix-format-compiled))
8923 (extra (org-get-at-bol 'extra)))
8924 (with-current-buffer (marker-buffer hdmarker)
8925 (org-with-wide-buffer
8926 (org-agenda-format-item extra newhead level cat tags dotime))))
8927 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8928 undone-face (org-get-at-bol 'undone-face)
8929 done-face (org-get-at-bol 'done-face))
8930 (beginning-of-line 1)
8931 (cond
8932 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8933 ((looking-at ".*")
8934 ;; When replacing the whole line, preserve bulk mark
8935 ;; overlay, if any.
8936 (let ((mark (catch :overlay
8937 (dolist (o (overlays-in (point) (+ 2 (point))))
8938 (when (eq (overlay-get o 'type)
8939 'org-marked-entry-overlay)
8940 (throw :overlay o))))))
8941 (replace-match new t t)
8942 (beginning-of-line)
8943 (when mark (move-overlay mark (point) (+ 2 (point)))))
8944 (add-text-properties (point-at-bol) (point-at-eol) props)
8945 (when fixface
8946 (add-text-properties
8947 (point-at-bol) (point-at-eol)
8948 (list 'face
8949 (if org-last-todo-state-is-todo
8950 undone-face done-face))))
8951 (org-agenda-highlight-todo 'line)
8952 (beginning-of-line 1))
8953 (t (error "Line update did not work")))
8954 (save-restriction
8955 (narrow-to-region (point-at-bol) (point-at-eol))
8956 (org-agenda-finalize)))
8957 (beginning-of-line 0)))))
8959 (defun org-agenda-align-tags (&optional line)
8960 "Align all tags in agenda items to `org-agenda-tags-column'."
8961 (let ((inhibit-read-only t) l c)
8962 (save-excursion
8963 (goto-char (if line (point-at-bol) (point-min)))
8964 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8965 (if line (point-at-eol) nil) t)
8966 (add-text-properties
8967 (match-beginning 2) (match-end 2)
8968 (list 'face (delq nil (let ((prop (get-text-property
8969 (match-beginning 2) 'face)))
8970 (or (listp prop) (setq prop (list prop)))
8971 (if (memq 'org-tag prop)
8972 prop
8973 (cons 'org-tag prop))))))
8974 (setq l (- (match-end 2) (match-beginning 2))
8975 c (if (< org-agenda-tags-column 0)
8976 (- (abs org-agenda-tags-column) l)
8977 org-agenda-tags-column))
8978 (delete-region (match-beginning 1) (match-end 1))
8979 (goto-char (match-beginning 1))
8980 (insert (org-add-props
8981 (make-string (max 1 (- c (current-column))) ?\ )
8982 (plist-put (copy-sequence (text-properties-at (point)))
8983 'face nil))))
8984 (goto-char (point-min))
8985 (org-font-lock-add-tag-faces (point-max)))))
8987 (defun org-agenda-priority-up ()
8988 "Increase the priority of line at point, also in Org file."
8989 (interactive)
8990 (org-agenda-priority 'up))
8992 (defun org-agenda-priority-down ()
8993 "Decrease the priority of line at point, also in Org file."
8994 (interactive)
8995 (org-agenda-priority 'down))
8997 (defun org-agenda-priority (&optional force-direction)
8998 "Set the priority of line at point, also in Org file.
8999 This changes the line at point, all other lines in the agenda referring to
9000 the same tree node, and the headline of the tree node in the Org file.
9001 Called with a universal prefix arg, show the priority instead of setting it."
9002 (interactive "P")
9003 (if (equal force-direction '(4))
9004 (org-show-priority)
9005 (unless org-enable-priority-commands
9006 (error "Priority commands are disabled"))
9007 (org-agenda-check-no-diary)
9008 (let* ((col (current-column))
9009 (marker (or (org-get-at-bol 'org-marker)
9010 (org-agenda-error)))
9011 (hdmarker (org-get-at-bol 'org-hd-marker))
9012 (buffer (marker-buffer hdmarker))
9013 (pos (marker-position hdmarker))
9014 (inhibit-read-only t)
9015 newhead)
9016 (org-with-remote-undo buffer
9017 (with-current-buffer buffer
9018 (widen)
9019 (goto-char pos)
9020 (org-show-context 'agenda)
9021 (funcall 'org-priority force-direction)
9022 (end-of-line 1)
9023 (setq newhead (org-get-heading)))
9024 (org-agenda-change-all-lines newhead hdmarker)
9025 (org-move-to-column col)))))
9027 ;; FIXME: should fix the tags property of the agenda line.
9028 (defun org-agenda-set-tags (&optional tag onoff)
9029 "Set tags for the current headline."
9030 (interactive)
9031 (org-agenda-check-no-diary)
9032 (if (and (org-region-active-p) (called-interactively-p 'any))
9033 (call-interactively 'org-change-tag-in-region)
9034 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9035 (org-agenda-error)))
9036 (buffer (marker-buffer hdmarker))
9037 (pos (marker-position hdmarker))
9038 (inhibit-read-only t)
9039 newhead)
9040 (org-with-remote-undo buffer
9041 (with-current-buffer buffer
9042 (widen)
9043 (goto-char pos)
9044 (org-show-context 'agenda)
9045 (if tag
9046 (org-toggle-tag tag onoff)
9047 (call-interactively 'org-set-tags))
9048 (end-of-line 1)
9049 (setq newhead (org-get-heading)))
9050 (org-agenda-change-all-lines newhead hdmarker)
9051 (beginning-of-line 1)))))
9053 (defun org-agenda-set-property ()
9054 "Set a property for the current headline."
9055 (interactive)
9056 (org-agenda-check-no-diary)
9057 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9058 (org-agenda-error)))
9059 (buffer (marker-buffer hdmarker))
9060 (pos (marker-position hdmarker))
9061 (inhibit-read-only t)
9062 newhead)
9063 (org-with-remote-undo buffer
9064 (with-current-buffer buffer
9065 (widen)
9066 (goto-char pos)
9067 (org-show-context 'agenda)
9068 (call-interactively 'org-set-property)))))
9070 (defun org-agenda-set-effort ()
9071 "Set the effort property for the current headline."
9072 (interactive)
9073 (org-agenda-check-no-diary)
9074 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9075 (org-agenda-error)))
9076 (buffer (marker-buffer hdmarker))
9077 (pos (marker-position hdmarker))
9078 (inhibit-read-only t)
9079 newhead)
9080 (org-with-remote-undo buffer
9081 (with-current-buffer buffer
9082 (widen)
9083 (goto-char pos)
9084 (org-show-context 'agenda)
9085 (call-interactively 'org-set-effort)
9086 (end-of-line 1)
9087 (setq newhead (org-get-heading)))
9088 (org-agenda-change-all-lines newhead hdmarker))))
9090 (defun org-agenda-toggle-archive-tag ()
9091 "Toggle the archive tag for the current entry."
9092 (interactive)
9093 (org-agenda-check-no-diary)
9094 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9095 (org-agenda-error)))
9096 (buffer (marker-buffer hdmarker))
9097 (pos (marker-position hdmarker))
9098 (inhibit-read-only t)
9099 newhead)
9100 (org-with-remote-undo buffer
9101 (with-current-buffer buffer
9102 (widen)
9103 (goto-char pos)
9104 (org-show-context 'agenda)
9105 (call-interactively 'org-toggle-archive-tag)
9106 (end-of-line 1)
9107 (setq newhead (org-get-heading)))
9108 (org-agenda-change-all-lines newhead hdmarker)
9109 (beginning-of-line 1))))
9111 (defun org-agenda-do-date-later (arg)
9112 (interactive "P")
9113 (cond
9114 ((or (equal arg '(16))
9115 (memq last-command
9116 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9117 (setq this-command 'org-agenda-date-later-minutes)
9118 (org-agenda-date-later-minutes 1))
9119 ((or (equal arg '(4))
9120 (memq last-command
9121 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9122 (setq this-command 'org-agenda-date-later-hours)
9123 (org-agenda-date-later-hours 1))
9125 (org-agenda-date-later (prefix-numeric-value arg)))))
9127 (defun org-agenda-do-date-earlier (arg)
9128 (interactive "P")
9129 (cond
9130 ((or (equal arg '(16))
9131 (memq last-command
9132 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9133 (setq this-command 'org-agenda-date-earlier-minutes)
9134 (org-agenda-date-earlier-minutes 1))
9135 ((or (equal arg '(4))
9136 (memq last-command
9137 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9138 (setq this-command 'org-agenda-date-earlier-hours)
9139 (org-agenda-date-earlier-hours 1))
9141 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9143 (defun org-agenda-date-later (arg &optional what)
9144 "Change the date of this item to ARG day(s) later."
9145 (interactive "p")
9146 (org-agenda-check-type t 'agenda 'timeline)
9147 (org-agenda-check-no-diary)
9148 (let* ((marker (or (org-get-at-bol 'org-marker)
9149 (org-agenda-error)))
9150 (buffer (marker-buffer marker))
9151 (pos (marker-position marker))
9152 cdate today)
9153 (org-with-remote-undo buffer
9154 (with-current-buffer buffer
9155 (widen)
9156 (goto-char pos)
9157 (if (not (org-at-timestamp-p))
9158 (error "Cannot find time stamp"))
9159 (when (and org-agenda-move-date-from-past-immediately-to-today
9160 (equal arg 1)
9161 (or (not what) (eq what 'day))
9162 (not (save-match-data (org-at-date-range-p))))
9163 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9164 cdate (calendar-absolute-from-gregorian
9165 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9166 today (org-today))
9167 (if (> today cdate)
9168 ;; immediately shift to today
9169 (setq arg (- today cdate))))
9170 (org-timestamp-change arg (or what 'day))
9171 (when (and (org-at-date-range-p)
9172 (re-search-backward org-tr-regexp-both (point-at-bol)))
9173 (let ((end org-last-changed-timestamp))
9174 (org-timestamp-change arg (or what 'day))
9175 (setq org-last-changed-timestamp
9176 (concat org-last-changed-timestamp "--" end)))))
9177 (org-agenda-show-new-time marker org-last-changed-timestamp))
9178 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9180 (defun org-agenda-date-earlier (arg &optional what)
9181 "Change the date of this item to ARG day(s) earlier."
9182 (interactive "p")
9183 (org-agenda-date-later (- arg) what))
9185 (defun org-agenda-date-later-minutes (arg)
9186 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9187 (interactive "p")
9188 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9189 (org-agenda-date-later arg 'minute))
9191 (defun org-agenda-date-earlier-minutes (arg)
9192 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9193 (interactive "p")
9194 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9195 (org-agenda-date-earlier arg 'minute))
9197 (defun org-agenda-date-later-hours (arg)
9198 "Change the time of this item, in hour steps."
9199 (interactive "p")
9200 (org-agenda-date-later arg 'hour))
9202 (defun org-agenda-date-earlier-hours (arg)
9203 "Change the time of this item, in hour steps."
9204 (interactive "p")
9205 (org-agenda-date-earlier arg 'hour))
9207 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9208 "Show new date stamp via text properties."
9209 ;; We use text properties to make this undoable
9210 (let ((inhibit-read-only t))
9211 (setq stamp (concat prefix " => " stamp " "))
9212 (save-excursion
9213 (goto-char (point-max))
9214 (while (not (bobp))
9215 (when (equal marker (org-get-at-bol 'org-marker))
9216 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9217 (org-move-to-column (- (window-width) (length stamp)) t)
9218 (add-text-properties
9219 (1- (point)) (point-at-eol)
9220 (list 'display (org-add-props stamp nil
9221 'face '(secondary-selection default))))
9222 (beginning-of-line 1))
9223 (beginning-of-line 0)))))
9225 (defun org-agenda-date-prompt (arg)
9226 "Change the date of this item. Date is prompted for, with default today.
9227 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9228 be used to request time specification in the time stamp."
9229 (interactive "P")
9230 (org-agenda-check-type t 'agenda 'timeline)
9231 (org-agenda-check-no-diary)
9232 (let* ((marker (or (org-get-at-bol 'org-marker)
9233 (org-agenda-error)))
9234 (buffer (marker-buffer marker))
9235 (pos (marker-position marker)))
9236 (org-with-remote-undo buffer
9237 (with-current-buffer buffer
9238 (widen)
9239 (goto-char pos)
9240 (if (not (org-at-timestamp-p t))
9241 (error "Cannot find time stamp"))
9242 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9243 (org-agenda-show-new-time marker org-last-changed-timestamp))
9244 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9246 (defun org-agenda-schedule (arg &optional time)
9247 "Schedule the item at point.
9248 ARG is passed through to `org-schedule'."
9249 (interactive "P")
9250 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9251 (org-agenda-check-no-diary)
9252 (let* ((marker (or (org-get-at-bol 'org-marker)
9253 (org-agenda-error)))
9254 (type (marker-insertion-type marker))
9255 (buffer (marker-buffer marker))
9256 (pos (marker-position marker))
9258 (set-marker-insertion-type marker t)
9259 (org-with-remote-undo buffer
9260 (with-current-buffer buffer
9261 (widen)
9262 (goto-char pos)
9263 (setq ts (org-schedule arg time)))
9264 (org-agenda-show-new-time marker ts " S"))
9265 (message "%s" ts)))
9267 (defun org-agenda-deadline (arg &optional time)
9268 "Schedule the item at point.
9269 ARG is passed through to `org-deadline'."
9270 (interactive "P")
9271 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9272 (org-agenda-check-no-diary)
9273 (let* ((marker (or (org-get-at-bol 'org-marker)
9274 (org-agenda-error)))
9275 (buffer (marker-buffer marker))
9276 (pos (marker-position marker))
9278 (org-with-remote-undo buffer
9279 (with-current-buffer buffer
9280 (widen)
9281 (goto-char pos)
9282 (setq ts (org-deadline arg time)))
9283 (org-agenda-show-new-time marker ts " D"))
9284 (message "%s" ts)))
9286 (defun org-agenda-clock-in (&optional arg)
9287 "Start the clock on the currently selected item."
9288 (interactive "P")
9289 (org-agenda-check-no-diary)
9290 (if (equal arg '(4))
9291 (org-clock-in arg)
9292 (let* ((marker (or (org-get-at-bol 'org-marker)
9293 (org-agenda-error)))
9294 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9295 (pos (marker-position marker))
9296 (col (current-column))
9297 newhead)
9298 (org-with-remote-undo (marker-buffer marker)
9299 (with-current-buffer (marker-buffer marker)
9300 (widen)
9301 (goto-char pos)
9302 (org-show-context 'agenda)
9303 (org-cycle-hide-drawers 'children)
9304 (org-clock-in arg)
9305 (setq newhead (org-get-heading)))
9306 (org-agenda-change-all-lines newhead hdmarker))
9307 (org-move-to-column col))))
9309 (defun org-agenda-clock-out ()
9310 "Stop the currently running clock."
9311 (interactive)
9312 (unless (marker-buffer org-clock-marker)
9313 (error "No running clock"))
9314 (let ((marker (make-marker)) (col (current-column)) newhead)
9315 (org-with-remote-undo (marker-buffer org-clock-marker)
9316 (with-current-buffer (marker-buffer org-clock-marker)
9317 (org-with-wide-buffer
9318 (goto-char org-clock-marker)
9319 (org-back-to-heading t)
9320 (move-marker marker (point))
9321 (org-clock-out)
9322 (setq newhead (org-get-heading)))))
9323 (org-agenda-change-all-lines newhead marker)
9324 (move-marker marker nil)
9325 (org-move-to-column col)
9326 (org-agenda-unmark-clocking-task)))
9328 (defun org-agenda-clock-cancel (&optional arg)
9329 "Cancel the currently running clock."
9330 (interactive "P")
9331 (unless (marker-buffer org-clock-marker)
9332 (user-error "No running clock"))
9333 (org-with-remote-undo (marker-buffer org-clock-marker)
9334 (org-clock-cancel)))
9336 (defun org-agenda-clock-goto ()
9337 "Jump to the currently clocked in task within the agenda.
9338 If the currently clocked in task is not listed in the agenda
9339 buffer, display it in another window."
9340 (interactive)
9341 (let (pos)
9342 (mapc (lambda (o)
9343 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9344 (setq pos (overlay-start o))))
9345 (overlays-in (point-min) (point-max)))
9346 (cond (pos (goto-char pos))
9347 ;; If the currently clocked entry is not in the agenda
9348 ;; buffer, we visit it in another window:
9349 ((bound-and-true-p org-clock-current-task)
9350 (org-switch-to-buffer-other-window (org-clock-goto)))
9351 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9353 (defun org-agenda-diary-entry-in-org-file ()
9354 "Make a diary entry in the file `org-agenda-diary-file'."
9355 (let (d1 d2 char (text "") dp1 dp2)
9356 (if (equal (buffer-name) "*Calendar*")
9357 (setq d1 (calendar-cursor-to-date t)
9358 d2 (car calendar-mark-ring))
9359 (setq dp1 (get-text-property (point-at-bol) 'day))
9360 (unless dp1 (user-error "No date defined in current line"))
9361 (setq d1 (calendar-gregorian-from-absolute dp1)
9362 d2 (and (ignore-errors (mark))
9363 (save-excursion
9364 (goto-char (mark))
9365 (setq dp2 (get-text-property (point-at-bol) 'day)))
9366 (calendar-gregorian-from-absolute dp2))))
9367 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9368 (setq char (read-char-exclusive))
9369 (cond
9370 ((equal char ?d)
9371 (setq text (read-string "Day entry: "))
9372 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9373 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9374 ((equal char ?a)
9375 (setq d1 (list (car d1) (nth 1 d1)
9376 (read-number (format "Reference year [%d]: " (nth 2 d1))
9377 (nth 2 d1))))
9378 (setq text (read-string "Anniversary (use %d to show years): "))
9379 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9380 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9381 ((equal char ?b)
9382 (setq text (read-string "Block entry: "))
9383 (unless (and d1 d2 (not (equal d1 d2)))
9384 (user-error "No block of days selected"))
9385 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9386 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9387 ((equal char ?j)
9388 (org-switch-to-buffer-other-window
9389 (find-file-noselect org-agenda-diary-file))
9390 (require 'org-datetree)
9391 (org-datetree-find-date-create d1)
9392 (org-reveal t))
9393 (t (user-error "Invalid selection character `%c'" char)))))
9395 (defcustom org-agenda-insert-diary-strategy 'date-tree
9396 "Where in `org-agenda-diary-file' should new entries be added?
9397 Valid values:
9399 date-tree in the date tree, as first child of the date
9400 date-tree-last in the date tree, as last child of the date
9401 top-level as top-level entries at the end of the file."
9402 :group 'org-agenda
9403 :type '(choice
9404 (const :tag "first in a date tree" date-tree)
9405 (const :tag "last in a date tree" date-tree-last)
9406 (const :tag "as top level at end of file" top-level)))
9408 (defcustom org-agenda-insert-diary-extract-time nil
9409 "Non-nil means extract any time specification from the diary entry."
9410 :group 'org-agenda
9411 :version "24.1"
9412 :type 'boolean)
9414 (defcustom org-agenda-bulk-mark-char ">"
9415 "A single-character string to be used as the bulk mark."
9416 :group 'org-agenda
9417 :version "24.1"
9418 :type 'string)
9420 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9421 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9422 If TEXT is not empty, it will become the headline of the new entry, and
9423 the resulting entry will not be shown. When TEXT is empty, switch to
9424 `org-agenda-diary-file' and let the user finish the entry there."
9425 (let ((cw (current-window-configuration)))
9426 (org-switch-to-buffer-other-window
9427 (find-file-noselect org-agenda-diary-file))
9428 (widen)
9429 (goto-char (point-min))
9430 (cond
9431 ((eq type 'anniversary)
9432 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9433 (progn
9434 (or (org-at-heading-p t)
9435 (progn
9436 (outline-next-heading)
9437 (insert "* Anniversaries\n\n")
9438 (beginning-of-line -1)))))
9439 (outline-next-heading)
9440 (org-back-over-empty-lines)
9441 (backward-char 1)
9442 (insert "\n")
9443 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9444 (nth 2 d1) (car d1) (nth 1 d1) text)))
9445 ((eq type 'day)
9446 (let ((org-prefix-has-time t)
9447 (org-agenda-time-leading-zero t)
9448 fmt time time2)
9449 (if org-agenda-insert-diary-extract-time
9450 ;; Use org-agenda-format-item to parse text for a time-range and
9451 ;; remove it. FIXME: This is a hack, we should refactor
9452 ;; that function to make time extraction available separately
9453 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9454 time (get-text-property 0 'time fmt)
9455 time2 (if (> (length time) 0)
9456 ;; split-string removes trailing ...... if
9457 ;; no end time given. First space
9458 ;; separates time from date.
9459 (concat " " (car (split-string time "\\.")))
9460 nil)
9461 text (get-text-property 0 'txt fmt)))
9462 (if (eq org-agenda-insert-diary-strategy 'top-level)
9463 (org-agenda-insert-diary-as-top-level text)
9464 (require 'org-datetree)
9465 (org-datetree-find-date-create d1)
9466 (org-agenda-insert-diary-make-new-entry text))
9467 (org-insert-time-stamp (org-time-from-absolute
9468 (calendar-absolute-from-gregorian d1))
9469 nil nil nil nil time2))
9470 (end-of-line 0))
9471 ((eq type 'block)
9472 (if (> (calendar-absolute-from-gregorian d1)
9473 (calendar-absolute-from-gregorian d2))
9474 (setq d1 (prog1 d2 (setq d2 d1))))
9475 (if (eq org-agenda-insert-diary-strategy 'top-level)
9476 (org-agenda-insert-diary-as-top-level text)
9477 (require 'org-datetree)
9478 (org-datetree-find-date-create d1)
9479 (org-agenda-insert-diary-make-new-entry text))
9480 (org-insert-time-stamp (org-time-from-absolute
9481 (calendar-absolute-from-gregorian d1)))
9482 (insert "--")
9483 (org-insert-time-stamp (org-time-from-absolute
9484 (calendar-absolute-from-gregorian d2)))
9485 (end-of-line 0)))
9486 (if (string-match "\\S-" text)
9487 (progn
9488 (set-window-configuration cw)
9489 (message "%s entry added to %s"
9490 (capitalize (symbol-name type))
9491 (abbreviate-file-name org-agenda-diary-file)))
9492 (org-reveal t)
9493 (message "Please finish entry here"))))
9495 (defun org-agenda-insert-diary-as-top-level (text)
9496 "Make new entry as a top-level entry at the end of the file.
9497 Add TEXT as headline, and position the cursor in the second line so that
9498 a timestamp can be added there."
9499 (widen)
9500 (goto-char (point-max))
9501 (unless (bolp) (insert "\n"))
9502 (org-insert-heading nil t t)
9503 (insert text)
9504 (org-end-of-meta-data)
9505 (unless (bolp) (insert "\n"))
9506 (when org-adapt-indentation (indent-to-column 2)))
9508 (defun org-agenda-insert-diary-make-new-entry (text)
9509 "Make a new entry with TEXT as a child of the current subtree.
9510 Position the point in the heading's first body line so that
9511 a timestamp can be added there."
9512 (cond
9513 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9514 (end-of-line)
9515 (org-insert-heading '(4) t)
9516 (org-do-demote))
9518 (outline-next-heading)
9519 (org-back-over-empty-lines)
9520 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9521 (org-insert-heading nil t)
9522 (org-do-demote)))
9523 (let ((col (current-column)))
9524 (insert text)
9525 (org-end-of-meta-data)
9526 ;; Ensure point is left on a blank line, at proper indentation.
9527 (unless (bolp) (insert "\n"))
9528 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9529 (when org-adapt-indentation (indent-to-column col)))
9530 (org-show-set-visibility 'lineage))
9532 (defun org-agenda-diary-entry ()
9533 "Make a diary entry, like the `i' command from the calendar.
9534 All the standard commands work: block, weekly etc.
9535 When `org-agenda-diary-file' points to a file,
9536 `org-agenda-diary-entry-in-org-file' is called instead to create
9537 entries in that Org file."
9538 (interactive)
9539 (if (not (eq org-agenda-diary-file 'diary-file))
9540 (org-agenda-diary-entry-in-org-file)
9541 (require 'diary-lib)
9542 (let* ((char (progn
9543 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9544 (read-char-exclusive)))
9545 (cmd (cdr (assoc char
9546 '((?d . diary-insert-entry)
9547 (?w . diary-insert-weekly-entry)
9548 (?m . diary-insert-monthly-entry)
9549 (?y . diary-insert-yearly-entry)
9550 (?a . diary-insert-anniversary-entry)
9551 (?b . diary-insert-block-entry)
9552 (?c . diary-insert-cyclic-entry)))))
9553 (oldf (symbol-function 'calendar-cursor-to-date))
9554 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9555 (point (point))
9556 (mark (or (mark t) (point))))
9557 (unless cmd
9558 (user-error "No command associated with <%c>" char))
9559 (unless (and (get-text-property point 'day)
9560 (or (not (equal ?b char))
9561 (get-text-property mark 'day)))
9562 (user-error "Don't know which date to use for diary entry"))
9563 ;; We implement this by hacking the `calendar-cursor-to-date' function
9564 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9565 (let ((calendar-mark-ring
9566 (list (calendar-gregorian-from-absolute
9567 (or (get-text-property mark 'day)
9568 (get-text-property point 'day))))))
9569 (unwind-protect
9570 (progn
9571 (fset 'calendar-cursor-to-date
9572 (lambda (&optional error dummy)
9573 (calendar-gregorian-from-absolute
9574 (get-text-property point 'day))))
9575 (call-interactively cmd))
9576 (fset 'calendar-cursor-to-date oldf))))))
9578 (defun org-agenda-execute-calendar-command (cmd)
9579 "Execute a calendar command from the agenda with date from cursor."
9580 (org-agenda-check-type t 'agenda 'timeline)
9581 (require 'diary-lib)
9582 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9583 (user-error "Don't know which date to use for the calendar command"))
9584 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9585 (point (point))
9586 (date (calendar-gregorian-from-absolute
9587 (get-text-property point 'day)))
9588 ;; the following 2 vars are needed in the calendar
9589 (displayed-month (car date))
9590 (displayed-year (nth 2 date)))
9591 (unwind-protect
9592 (progn
9593 (fset 'calendar-cursor-to-date
9594 (lambda (&optional error dummy)
9595 (calendar-gregorian-from-absolute
9596 (get-text-property point 'day))))
9597 (call-interactively cmd))
9598 (fset 'calendar-cursor-to-date oldf))))
9600 (defun org-agenda-phases-of-moon ()
9601 "Display the phases of the moon for the 3 months around the cursor date."
9602 (interactive)
9603 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9605 (defun org-agenda-holidays ()
9606 "Display the holidays for the 3 months around the cursor date."
9607 (interactive)
9608 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9610 (defvar calendar-longitude) ; defined in calendar.el
9611 (defvar calendar-latitude) ; defined in calendar.el
9612 (defvar calendar-location-name) ; defined in calendar.el
9614 (defun org-agenda-sunrise-sunset (arg)
9615 "Display sunrise and sunset for the cursor date.
9616 Latitude and longitude can be specified with the variables
9617 `calendar-latitude' and `calendar-longitude'. When called with prefix
9618 argument, latitude and longitude will be prompted for."
9619 (interactive "P")
9620 (require 'solar)
9621 (let ((calendar-longitude (if arg nil calendar-longitude))
9622 (calendar-latitude (if arg nil calendar-latitude))
9623 (calendar-location-name
9624 (if arg "the given coordinates" calendar-location-name)))
9625 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9627 (defun org-agenda-goto-calendar ()
9628 "Open the Emacs calendar with the date at the cursor."
9629 (interactive)
9630 (org-agenda-check-type t 'agenda 'timeline)
9631 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9632 (user-error "Don't know which date to open in calendar")))
9633 (date (calendar-gregorian-from-absolute day))
9634 (calendar-move-hook nil)
9635 (calendar-view-holidays-initially-flag nil)
9636 (calendar-view-diary-initially-flag nil))
9637 (calendar)
9638 (calendar-goto-date date)))
9640 ;;;###autoload
9641 (defun org-calendar-goto-agenda ()
9642 "Compute the Org agenda for the calendar date displayed at the cursor.
9643 This is a command that has to be installed in `calendar-mode-map'."
9644 (interactive)
9645 ;; Temporarily disable sticky agenda since user clearly wants to
9646 ;; refresh view anyway.
9647 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9648 (org-agenda-sticky nil))
9649 (org-agenda-list nil (calendar-absolute-from-gregorian
9650 (calendar-cursor-to-date))
9651 nil)))
9653 (defun org-agenda-convert-date ()
9654 (interactive)
9655 (org-agenda-check-type t 'agenda 'timeline)
9656 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9657 date s)
9658 (unless day
9659 (user-error "Don't know which date to convert"))
9660 (setq date (calendar-gregorian-from-absolute day))
9661 (setq s (concat
9662 "Gregorian: " (calendar-date-string date) "\n"
9663 "ISO: " (calendar-iso-date-string date) "\n"
9664 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9665 "Julian: " (calendar-julian-date-string date) "\n"
9666 "Astron. JD: " (calendar-astro-date-string date)
9667 " (Julian date number at noon UTC)\n"
9668 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9669 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9670 "French: " (calendar-french-date-string date) "\n"
9671 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9672 "Mayan: " (calendar-mayan-date-string date) "\n"
9673 "Coptic: " (calendar-coptic-date-string date) "\n"
9674 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9675 "Persian: " (calendar-persian-date-string date) "\n"
9676 "Chinese: " (calendar-chinese-date-string date) "\n"))
9677 (with-output-to-temp-buffer "*Dates*"
9678 (princ s))
9679 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9681 ;;; Bulk commands
9683 (defun org-agenda-bulk-marked-p ()
9684 "Non-nil when current entry is marked for bulk action."
9685 (eq (get-char-property (point-at-bol) 'type)
9686 'org-marked-entry-overlay))
9688 (defun org-agenda-bulk-mark (&optional arg)
9689 "Mark the entry at point for future bulk action."
9690 (interactive "p")
9691 (dotimes (i (or arg 1))
9692 (unless (org-get-at-bol 'org-agenda-diary-link)
9693 (let* ((m (org-get-at-bol 'org-hd-marker))
9695 (unless (org-agenda-bulk-marked-p)
9696 (unless m (user-error "Nothing to mark at point"))
9697 (push m org-agenda-bulk-marked-entries)
9698 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9699 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9700 (org-get-todo-face "TODO")
9701 'evaporate)
9702 (overlay-put ov 'type 'org-marked-entry-overlay))
9703 (end-of-line 1)
9704 (or (ignore-errors
9705 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9706 (beginning-of-line 2))
9707 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9708 (beginning-of-line 2))
9709 (message "%d entries marked for bulk action"
9710 (length org-agenda-bulk-marked-entries))))))
9712 (defun org-agenda-bulk-mark-all ()
9713 "Mark all entries for future agenda bulk action."
9714 (interactive)
9715 (org-agenda-bulk-mark-regexp "."))
9717 (defun org-agenda-bulk-mark-regexp (regexp)
9718 "Mark entries matching REGEXP for future agenda bulk action."
9719 (interactive "sMark entries matching regexp: ")
9720 (let ((entries-marked 0) txt-at-point)
9721 (save-excursion
9722 (goto-char (point-min))
9723 (goto-char (next-single-property-change (point) 'org-hd-marker))
9724 (while (and (re-search-forward regexp nil t)
9725 (setq txt-at-point (get-text-property (point) 'txt)))
9726 (if (get-char-property (point) 'invisible)
9727 (beginning-of-line 2)
9728 (when (string-match regexp txt-at-point)
9729 (setq entries-marked (1+ entries-marked))
9730 (call-interactively 'org-agenda-bulk-mark)))))
9732 (if (not entries-marked)
9733 (message "No entry matching this regexp."))))
9735 (defun org-agenda-bulk-unmark (&optional arg)
9736 "Unmark the entry at point for future bulk action."
9737 (interactive "P")
9738 (if arg
9739 (org-agenda-bulk-unmark-all)
9740 (cond ((org-agenda-bulk-marked-p)
9741 (org-agenda-bulk-remove-overlays
9742 (point-at-bol) (+ 2 (point-at-bol)))
9743 (setq org-agenda-bulk-marked-entries
9744 (delete (org-get-at-bol 'org-hd-marker)
9745 org-agenda-bulk-marked-entries))
9746 (end-of-line 1)
9747 (or (ignore-errors
9748 (goto-char (next-single-property-change (point) 'txt)))
9749 (beginning-of-line 2))
9750 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9751 (beginning-of-line 2))
9752 (message "%d entries left marked for bulk action"
9753 (length org-agenda-bulk-marked-entries)))
9754 (t (message "No entry to unmark here")))))
9756 (defun org-agenda-bulk-toggle-all ()
9757 "Toggle all marks for bulk action."
9758 (interactive)
9759 (save-excursion
9760 (goto-char (point-min))
9761 (while (ignore-errors
9762 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9763 (org-agenda-bulk-toggle))))
9765 (defun org-agenda-bulk-toggle ()
9766 "Toggle the mark at point for bulk action."
9767 (interactive)
9768 (if (org-agenda-bulk-marked-p)
9769 (org-agenda-bulk-unmark)
9770 (org-agenda-bulk-mark)))
9772 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9773 "Remove the mark overlays between BEG and END in the agenda buffer.
9774 BEG and END default to the buffer limits.
9776 This only removes the overlays, it does not remove the markers
9777 from the list in `org-agenda-bulk-marked-entries'."
9778 (interactive)
9779 (mapc (lambda (ov)
9780 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9781 (delete-overlay ov)))
9782 (overlays-in (or beg (point-min)) (or end (point-max)))))
9784 (defun org-agenda-bulk-unmark-all ()
9785 "Remove all marks in the agenda buffer.
9786 This will remove the markers and the overlays."
9787 (interactive)
9788 (if (null org-agenda-bulk-marked-entries)
9789 (message "No entry to unmark")
9790 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9791 (setq org-agenda-bulk-marked-entries nil)
9792 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9794 (defcustom org-agenda-persistent-marks nil
9795 "Non-nil means marked items will stay marked after a bulk action.
9796 You can toggle this interactively by typing `p' when prompted for a
9797 bulk action."
9798 :group 'org-agenda
9799 :version "24.1"
9800 :type 'boolean)
9802 (defun org-agenda-bulk-action (&optional arg)
9803 "Execute an remote-editing action on all marked entries.
9804 The prefix arg is passed through to the command if possible."
9805 (interactive "P")
9806 ;; Make sure we have markers, and only valid ones
9807 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9808 (mapc
9809 (lambda (m)
9810 (unless (and (markerp m)
9811 (marker-buffer m)
9812 (buffer-live-p (marker-buffer m))
9813 (marker-position m))
9814 (user-error "Marker %s for bulk command is invalid" m)))
9815 org-agenda-bulk-marked-entries)
9817 ;; Prompt for the bulk command
9818 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9819 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9820 "[S]catter [f]unction "
9821 (when org-agenda-bulk-custom-functions
9822 (concat " Custom: ["
9823 (mapconcat (lambda(f) (char-to-string (car f)))
9824 org-agenda-bulk-custom-functions "")
9825 "]"))))
9826 (catch 'exit
9827 (let* ((action (read-char-exclusive))
9828 (org-log-refile (if org-log-refile 'time nil))
9829 (entries (reverse org-agenda-bulk-marked-entries))
9830 (org-overriding-default-time
9831 (if (get-text-property (point) 'org-agenda-date-header)
9832 (org-get-cursor-date)))
9833 redo-at-end
9834 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9835 (cond
9836 ((equal action ?p)
9837 (let ((org-agenda-persistent-marks
9838 (not org-agenda-persistent-marks)))
9839 (org-agenda-bulk-action)
9840 (throw 'exit nil)))
9842 ((equal action ?$)
9843 (setq cmd '(org-agenda-archive)))
9845 ((equal action ?A)
9846 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9848 ((member action '(?r ?w))
9849 (setq rfloc (org-refile-get-location
9850 "Refile to"
9851 (marker-buffer (car entries))
9852 org-refile-allow-creating-parent-nodes))
9853 (if (nth 3 rfloc)
9854 (setcar (nthcdr 3 rfloc)
9855 (move-marker (make-marker) (nth 3 rfloc)
9856 (or (get-file-buffer (nth 1 rfloc))
9857 (find-buffer-visiting (nth 1 rfloc))
9858 (error "This should not happen")))))
9860 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9861 redo-at-end t))
9863 ((equal action ?t)
9864 (setq state (completing-read
9865 "Todo state: "
9866 (with-current-buffer (marker-buffer (car entries))
9867 (mapcar #'list org-todo-keywords-1))))
9868 (setq cmd `(let ((org-inhibit-blocking t)
9869 (org-inhibit-logging 'note))
9870 (org-agenda-todo ,state))))
9872 ((memq action '(?- ?+))
9873 (setq tag (completing-read
9874 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9875 (with-current-buffer (marker-buffer (car entries))
9876 (delq nil
9877 (mapcar (lambda (x) (and (stringp (car x)) x))
9878 org-current-tag-alist)))))
9879 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9881 ((memq action '(?s ?d))
9882 (let* ((time
9883 (unless arg
9884 (org-read-date
9885 nil nil nil
9886 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9887 org-overriding-default-time)))
9888 (c1 (if (eq action ?s) 'org-agenda-schedule
9889 'org-agenda-deadline)))
9890 ;; Make sure to not prompt for a note when bulk
9891 ;; rescheduling as Org cannot cope with simultaneous Org.
9892 ;; Besides, it could be annoying depending on the number
9893 ;; of items re-scheduled.
9894 (setq cmd `(eval '(let ((org-log-reschedule
9895 (and org-log-reschedule 'time)))
9896 (,c1 arg ,time))))))
9898 ((equal action ?S)
9899 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9900 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9901 (let ((days (read-number
9902 (format "Scatter tasks across how many %sdays: "
9903 (if arg "week" "")) 7)))
9904 (setq cmd
9905 `(let ((distance (1+ (random ,days))))
9906 (if arg
9907 (let ((dist distance)
9908 (day-of-week
9909 (calendar-day-of-week
9910 (calendar-gregorian-from-absolute (org-today)))))
9911 (dotimes (i (1+ dist))
9912 (while (member day-of-week org-agenda-weekend-days)
9913 (cl-incf distance)
9914 (cl-incf day-of-week)
9915 (when (= day-of-week 7)
9916 (setq day-of-week 0)))
9917 (cl-incf day-of-week)
9918 (when (= day-of-week 7)
9919 (setq day-of-week 0)))))
9920 ;; silently fail when try to replan a sexp entry
9921 (condition-case nil
9922 (let* ((date (calendar-gregorian-from-absolute
9923 (+ (org-today) distance)))
9924 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9925 (nth 2 date))))
9926 (org-agenda-schedule nil time))
9927 (error nil)))))))
9929 ((assoc action org-agenda-bulk-custom-functions)
9930 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9931 redo-at-end t))
9933 ((equal action ?f)
9934 (setq cmd (list (intern
9935 (completing-read "Function: "
9936 obarray 'fboundp t nil nil)))))
9938 (t (user-error "Invalid bulk action")))
9940 ;; Sort the markers, to make sure that parents are handled before children
9941 (setq entries (sort entries
9942 (lambda (a b)
9943 (cond
9944 ((equal (marker-buffer a) (marker-buffer b))
9945 (< (marker-position a) (marker-position b)))
9947 (string< (buffer-name (marker-buffer a))
9948 (buffer-name (marker-buffer b))))))))
9950 ;; Now loop over all markers and apply cmd
9951 (while (setq e (pop entries))
9952 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9953 (if (not pos)
9954 (progn (message "Skipping removed entry at %s" e)
9955 (setq cntskip (1+ cntskip)))
9956 (goto-char pos)
9957 (let (org-loop-over-headlines-in-active-region)
9958 (eval cmd))
9959 ;; `post-command-hook' is not run yet. We make sure any
9960 ;; pending log note is processed.
9961 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9962 (memq 'org-add-log-note post-command-hook))
9963 (org-add-log-note))
9964 (setq cnt (1+ cnt))))
9965 (when redo-at-end (org-agenda-redo))
9966 (unless org-agenda-persistent-marks
9967 (org-agenda-bulk-unmark-all))
9968 (message "Acted on %d entries%s%s"
9970 (if (= cntskip 0)
9972 (format ", skipped %d (disappeared before their turn)"
9973 cntskip))
9974 (if (not org-agenda-persistent-marks)
9975 "" " (kept marked)"))))))
9977 (defun org-agenda-capture (&optional with-time)
9978 "Call `org-capture' with the date at point.
9979 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9980 current HH:MM time."
9981 (interactive "P")
9982 (if (not (eq major-mode 'org-agenda-mode))
9983 (user-error "You cannot do this outside of agenda buffers")
9984 (let ((org-overriding-default-time
9985 (org-get-cursor-date (equal with-time 1))))
9986 (call-interactively 'org-capture))))
9988 ;;; Dragging agenda lines forward/backward
9990 (defun org-agenda-reapply-filters ()
9991 "Re-apply all agenda filters."
9992 (mapcar
9993 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
9994 `((,org-agenda-tag-filter tag)
9995 (,org-agenda-category-filter category)
9996 (,org-agenda-regexp-filter regexp)
9997 (,org-agenda-effort-filter effort)
9998 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9999 (,(get 'org-agenda-category-filter :preset-filter) category)
10000 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10001 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10003 (defun org-agenda-drag-line-forward (arg &optional backward)
10004 "Drag an agenda line forward by ARG lines.
10005 When the optional argument `backward' is non-nil, move backward."
10006 (interactive "p")
10007 (let ((inhibit-read-only t) lst line)
10008 (if (or (not (get-text-property (point) 'txt))
10009 (save-excursion
10010 (dotimes (n arg)
10011 (move-beginning-of-line (if backward 0 2))
10012 (push (not (get-text-property (point) 'txt)) lst))
10013 (delq nil lst)))
10014 (message "Cannot move line forward")
10015 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10016 (move-beginning-of-line 1)
10017 (setq line (buffer-substring (point) end))
10018 (delete-region (point) end)
10019 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10020 (insert line)
10021 (org-agenda-reapply-filters)
10022 (org-agenda-mark-clocking-task)
10023 (move-beginning-of-line 0)))))
10025 (defun org-agenda-drag-line-backward (arg)
10026 "Drag an agenda line backward by ARG lines."
10027 (interactive "p")
10028 (org-agenda-drag-line-forward arg t))
10030 ;;; Flagging notes
10032 (defun org-agenda-show-the-flagging-note ()
10033 "Display the flagging note in the other window.
10034 When called a second time in direct sequence, offer to remove the FLAGGING
10035 tag and (if present) the flagging note."
10036 (interactive)
10037 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10038 (win (selected-window))
10039 note heading newhead)
10040 (unless hdmarker
10041 (user-error "No linked entry at point"))
10042 (if (and (eq this-command last-command)
10043 (y-or-n-p "Unflag and remove any flagging note? "))
10044 (progn
10045 (org-agenda-remove-flag hdmarker)
10046 (let ((win (get-buffer-window "*Flagging Note*")))
10047 (and win (delete-window win)))
10048 (message "Entry unflagged"))
10049 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10050 (unless note
10051 (user-error "No flagging note"))
10052 (org-kill-new note)
10053 (org-switch-to-buffer-other-window "*Flagging Note*")
10054 (erase-buffer)
10055 (insert note)
10056 (goto-char (point-min))
10057 (while (re-search-forward "\\\\n" nil t)
10058 (replace-match "\n" t t))
10059 (goto-char (point-min))
10060 (select-window win)
10061 (message "%s" (substitute-command-keys "Flagging note pushed to \
10062 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10063 tag and note")))))
10065 (defun org-agenda-remove-flag (marker)
10066 "Remove the FLAGGED tag and any flagging note in the entry."
10067 (let (newhead)
10068 (org-with-point-at marker
10069 (org-toggle-tag "FLAGGED" 'off)
10070 (org-entry-delete nil "THEFLAGGINGNOTE")
10071 (setq newhead (org-get-heading)))
10072 (org-agenda-change-all-lines newhead marker)
10073 (message "Entry unflagged")))
10075 (defun org-agenda-get-any-marker (&optional pos)
10076 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10077 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10079 ;;; Appointment reminders
10081 (defvar appt-time-msg-list) ; defined in appt.el
10083 ;;;###autoload
10084 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10085 "Activate appointments found in `org-agenda-files'.
10087 With a `\\[universal-argument]' prefix, refresh the list of \
10088 appointments.
10090 If FILTER is t, interactively prompt the user for a regular
10091 expression, and filter out entries that don't match it.
10093 If FILTER is a string, use this string as a regular expression
10094 for filtering entries out.
10096 If FILTER is a function, filter out entries against which
10097 calling the function returns nil. This function takes one
10098 argument: an entry from `org-agenda-get-day-entries'.
10100 FILTER can also be an alist with the car of each cell being
10101 either `headline' or `category'. For example:
10103 \\='((headline \"IMPORTANT\")
10104 (category \"Work\"))
10106 will only add headlines containing IMPORTANT or headlines
10107 belonging to the \"Work\" category.
10109 ARGS are symbols indicating what kind of entries to consider.
10110 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10111 \(i.e., deadlines and scheduled items with a hh:mm specification)
10112 and :timestamp entries. See the docstring of `org-diary' for
10113 details and examples.
10115 If an entry has a APPT_WARNTIME property, its value will be used
10116 to override `appt-message-warning-time'."
10117 (interactive "P")
10118 (if refresh (setq appt-time-msg-list nil))
10119 (if (eq filter t)
10120 (setq filter (read-from-minibuffer "Regexp filter: ")))
10121 (let* ((cnt 0) ; count added events
10122 (scope (or args '(:deadline* :scheduled* :timestamp)))
10123 (org-agenda-new-buffers nil)
10124 (org-deadline-warning-days 0)
10125 ;; Do not use `org-today' here because appt only takes
10126 ;; time and without date as argument, so it may pass wrong
10127 ;; information otherwise
10128 (today (org-date-to-gregorian
10129 (time-to-days (current-time))))
10130 (org-agenda-restrict nil)
10131 (files (org-agenda-files 'unrestricted)) entries file
10132 (org-agenda-buffer nil))
10133 ;; Get all entries which may contain an appt
10134 (org-agenda-prepare-buffers files)
10135 (while (setq file (pop files))
10136 (setq entries
10137 (delq nil
10138 (append entries
10139 (apply 'org-agenda-get-day-entries
10140 file today scope)))))
10141 ;; Map thru entries and find if we should filter them out
10142 (mapc
10143 (lambda (x)
10144 (let* ((evt (org-trim
10145 (replace-regexp-in-string
10146 org-bracket-link-regexp "\\3"
10147 (or (get-text-property 1 'txt x) ""))))
10148 (cat (get-text-property (1- (length x)) 'org-category x))
10149 (tod (get-text-property 1 'time-of-day x))
10150 (ok (or (null filter)
10151 (and (stringp filter) (string-match filter evt))
10152 (and (functionp filter) (funcall filter x))
10153 (and (listp filter)
10154 (let ((cat-filter (cadr (assq 'category filter)))
10155 (evt-filter (cadr (assq 'headline filter))))
10156 (or (and (stringp cat-filter)
10157 (string-match cat-filter cat))
10158 (and (stringp evt-filter)
10159 (string-match evt-filter evt)))))))
10160 (wrn (get-text-property 1 'warntime x)))
10161 ;; FIXME: Shall we remove text-properties for the appt text?
10162 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10163 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10164 (setq tod (concat "00" (number-to-string tod)))
10165 (setq tod (when (string-match
10166 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10167 (concat (match-string 1 tod) ":"
10168 (match-string 2 tod))))
10169 (when (if (version< emacs-version "23.3")
10170 (appt-add tod evt)
10171 (appt-add tod evt wrn))
10172 (setq cnt (1+ cnt))))))
10173 entries)
10174 (org-release-buffers org-agenda-new-buffers)
10175 (if (eq cnt 0)
10176 (message "No event to add")
10177 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10179 (defun org-agenda-today-p (date)
10180 "Non nil when DATE means today.
10181 DATE is either a list of the form (month day year) or a number of
10182 days as returned by `calendar-absolute-from-gregorian' or
10183 `org-today'. This function considers `org-extend-today-until'
10184 when defining today."
10185 (eq (org-today)
10186 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10188 (defun org-agenda-todo-yesterday (&optional arg)
10189 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10190 (interactive "P")
10191 (let* ((org-use-effective-time t)
10192 (hour (nth 2 (decode-time (org-current-time))))
10193 (org-extend-today-until (1+ hour)))
10194 (org-agenda-todo arg)))
10196 (provide 'org-agenda)
10198 ;;; org-agenda.el ends here