org-agenda: Fix "no-catch --cl-block-nil--"
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob40db218f5425a76904207d4ff2d707b7d06106e1
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.
773 There are different motivations for using different values, please think
774 carefully when configuring this variable.
776 This applies when creating the global TODO list.
778 Valid values are:
780 near Don't show near deadline entries. A deadline is near when it is
781 closer than `org-deadline-warning-days' days. The idea behind this
782 is that such items will appear in the agenda anyway.
784 far Don't show TODO entries where a deadline has been defined, but
785 is not going to happen anytime soon. This is useful if you want to use
786 the TODO list to figure out what to do now.
788 past Don't show entries with a deadline timestamp for today or in the past.
790 future Don't show entries with a deadline timestamp in the future, not even
791 when they become `near' ones. Use it with caution.
793 all Ignore all TODO entries that do have a deadline.
795 t Same as `near', for backward compatibility.
797 This variable can also have an integer as a value. See
798 `org-agenda-todo-ignore-timestamp' for more details.
800 See also `org-agenda-todo-ignore-with-date'.
801 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
802 to make his option also apply to the tags-todo list."
803 :group 'org-agenda-skip
804 :group 'org-agenda-todo-list
805 :type '(choice
806 (const :tag "Ignore near deadlines" near)
807 (const :tag "Ignore near deadlines (compatibility)" t)
808 (const :tag "Ignore far deadlines" far)
809 (const :tag "Ignore all TODOs with a deadlines" all)
810 (const :tag "Show all TODOs, even if they have a deadline" nil)
811 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
813 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
814 "Time unit to use when possibly ignoring an agenda item.
816 See the docstring of various `org-agenda-todo-ignore-*' options.
817 The default is to compare time stamps using days. An item is thus
818 considered to be in the future if it is at least one day after today.
819 Non-nil means to compare time stamps using seconds. An item is then
820 considered future if it has a time value later than current time."
821 :group 'org-agenda-skip
822 :group 'org-agenda-todo-list
823 :version "24.4"
824 :package-version '(Org . "8.0")
825 :type '(choice
826 (const :tag "Compare time with days" nil)
827 (const :tag "Compare time with seconds" t)))
829 (defcustom org-agenda-tags-todo-honor-ignore-options nil
830 "Non-nil means honor todo-list ignores options also in tags-todo search.
831 The variables
832 `org-agenda-todo-ignore-with-date',
833 `org-agenda-todo-ignore-timestamp',
834 `org-agenda-todo-ignore-scheduled',
835 `org-agenda-todo-ignore-deadlines'
836 make the global TODO list skip entries that have time stamps of certain
837 kinds. If this option is set, the same options will also apply for the
838 tags-todo search, which is the general tags/property matcher
839 restricted to unfinished TODO entries only."
840 :group 'org-agenda-skip
841 :group 'org-agenda-todo-list
842 :group 'org-agenda-match-view
843 :type 'boolean)
845 (defcustom org-agenda-skip-scheduled-if-done nil
846 "Non-nil means don't show scheduled items in agenda when they are done.
847 This is relevant for the daily/weekly agenda, not for the TODO list. And
848 it applies only to the actual date of the scheduling. Warnings about
849 an item with a past scheduling dates are always turned off when the item
850 is DONE."
851 :group 'org-agenda-skip
852 :group 'org-agenda-daily/weekly
853 :type 'boolean)
855 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
856 "Non-nil means skip scheduling line if same entry shows because of deadline.
858 In the agenda of today, an entry can show up multiple times
859 because it is both scheduled and has a nearby deadline, and maybe
860 a plain time stamp as well.
862 When this variable is nil, the entry will be shown several times.
864 When set to t, then only the deadline is shown and the fact that
865 the entry is scheduled today or was scheduled previously is not
866 shown.
868 When set to the symbol `not-today', skip scheduled previously,
869 but not scheduled today.
871 When set to the symbol `repeated-after-deadline', skip scheduled
872 items if they are repeated beyond the current deadline."
873 :group 'org-agenda-skip
874 :group 'org-agenda-daily/weekly
875 :type '(choice
876 (const :tag "Never" nil)
877 (const :tag "Always" t)
878 (const :tag "Not when scheduled today" not-today)
879 (const :tag "When repeated past deadline" repeated-after-deadline)))
881 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
882 "Non-nil means skip timestamp line if same entry shows because of deadline.
883 In the agenda of today, an entry can show up multiple times
884 because it has both a plain timestamp and has a nearby deadline.
885 When this variable is t, then only the deadline is shown and the
886 fact that the entry has a timestamp for or including today is not
887 shown. When this variable is nil, the entry will be shown
888 several times."
889 :group 'org-agenda-skip
890 :group 'org-agenda-daily/weekly
891 :version "24.1"
892 :type '(choice
893 (const :tag "Never" nil)
894 (const :tag "Always" t)))
896 (defcustom org-agenda-skip-deadline-if-done nil
897 "Non-nil means don't show deadlines when the corresponding item is done.
898 When nil, the deadline is still shown and should give you a happy feeling.
899 This is relevant for the daily/weekly agenda. And it applied only to the
900 actually date of the deadline. Warnings about approaching and past-due
901 deadlines are always turned off when the item is DONE."
902 :group 'org-agenda-skip
903 :group 'org-agenda-daily/weekly
904 :type 'boolean)
906 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
907 "Non-nil means skip deadline prewarning when entry is also scheduled.
908 This will apply on all days where a prewarning for the deadline would
909 be shown, but not at the day when the entry is actually due. On that day,
910 the deadline will be shown anyway.
911 This variable may be set to nil, t, the symbol `pre-scheduled',
912 or a number which will then give the number of days before the actual
913 deadline when the prewarnings should resume. The symbol `pre-scheduled'
914 eliminates the deadline prewarning only prior to the scheduled date.
915 This can be used in a workflow where the first showing of the deadline will
916 trigger you to schedule it, and then you don't want to be reminded of it
917 because you will take care of it on the day when scheduled."
918 :group 'org-agenda-skip
919 :group 'org-agenda-daily/weekly
920 :version "24.1"
921 :type '(choice
922 (const :tag "Always show prewarning" nil)
923 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
924 (const :tag "Remove prewarning if entry is scheduled" t)
925 (integer :tag "Restart prewarning N days before deadline")))
927 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
928 "Non-nil means skip scheduled delay when entry also has a deadline.
929 This variable may be set to nil, t, the symbol `post-deadline',
930 or a number which will then give the number of days after the actual
931 scheduled date when the delay should expire. The symbol `post-deadline'
932 eliminates the schedule delay when the date is posterior to the deadline."
933 :group 'org-agenda-skip
934 :group 'org-agenda-daily/weekly
935 :version "24.4"
936 :package-version '(Org . "8.0")
937 :type '(choice
938 (const :tag "Always honor delay" nil)
939 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
940 (const :tag "Ignore delay if entry has a deadline" t)
941 (integer :tag "Honor delay up until N days after the scheduled date")))
943 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
944 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
945 When non-nil, after the search for timestamps has matched once in an
946 entry, the rest of the entry will not be searched."
947 :group 'org-agenda-skip
948 :type 'boolean)
950 (defcustom org-agenda-skip-timestamp-if-done nil
951 "Non-nil means don't select item by timestamp or -range if it is DONE."
952 :group 'org-agenda-skip
953 :group 'org-agenda-daily/weekly
954 :type 'boolean)
956 (defcustom org-agenda-dim-blocked-tasks t
957 "Non-nil means dim blocked tasks in the agenda display.
958 This causes some overhead during agenda construction, but if you
959 have turned on `org-enforce-todo-dependencies',
960 `org-enforce-todo-checkbox-dependencies', or any other blocking
961 mechanism, this will create useful feedback in the agenda.
963 Instead of t, this variable can also have the value `invisible'.
964 Then blocked tasks will be invisible and only become visible when
965 they become unblocked. An exemption to this behavior is when a task is
966 blocked because of unchecked checkboxes below it. Since checkboxes do
967 not show up in the agenda views, making this task invisible you remove any
968 trace from agenda views that there is something to do. Therefore, a task
969 that is blocked because of checkboxes will never be made invisible, it
970 will only be dimmed."
971 :group 'org-agenda-daily/weekly
972 :group 'org-agenda-todo-list
973 :version "24.3"
974 :type '(choice
975 (const :tag "Do not dim" nil)
976 (const :tag "Dim to a gray face" t)
977 (const :tag "Make invisible" invisible)))
979 (defcustom org-timeline-show-empty-dates 3
980 "Non-nil means `org-timeline' also shows dates without an entry.
981 When nil, only the days which actually have entries are shown.
982 When t, all days between the first and the last date are shown.
983 When an integer, show also empty dates, but if there is a gap of more than
984 N days, just insert a special line indicating the size of the gap."
985 :group 'org-agenda-skip
986 :type '(choice
987 (const :tag "None" nil)
988 (const :tag "All" t)
989 (integer :tag "at most")))
991 (defgroup org-agenda-startup nil
992 "Options concerning initial settings in the Agenda in Org Mode."
993 :tag "Org Agenda Startup"
994 :group 'org-agenda)
996 (defcustom org-agenda-menu-show-matcher t
997 "Non-nil means show the match string in the agenda dispatcher menu.
998 When nil, the matcher string is not shown, but is put into the help-echo
999 property so than moving the mouse over the command shows it.
1000 Setting it to nil is good if matcher strings are very long and/or if
1001 you want to use two-columns display (see `org-agenda-menu-two-columns')."
1002 :group 'org-agenda
1003 :version "24.1"
1004 :type 'boolean)
1006 (defcustom org-agenda-menu-two-columns nil
1007 "Non-nil means, use two columns to show custom commands in the dispatcher.
1008 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1009 to nil."
1010 :group 'org-agenda
1011 :version "24.1"
1012 :type 'boolean)
1014 (defcustom org-agenda-finalize-hook nil
1015 "Hook run just before displaying an agenda buffer.
1016 The buffer is still writable when the hook is called.
1018 You can modify some of the buffer substrings but you should be
1019 extra careful not to modify the text properties of the agenda
1020 headlines as the agenda display heavily relies on them."
1021 :group 'org-agenda-startup
1022 :type 'hook)
1024 (defcustom org-agenda-mouse-1-follows-link nil
1025 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1026 A longer mouse click will still set point. Needs to be set
1027 before org.el is loaded."
1028 :group 'org-agenda-startup
1029 :type 'boolean)
1031 (defcustom org-agenda-start-with-follow-mode nil
1032 "The initial value of follow mode in a newly created agenda window."
1033 :group 'org-agenda-startup
1034 :type 'boolean)
1036 (defcustom org-agenda-follow-indirect nil
1037 "Non-nil means `org-agenda-follow-mode' displays only the
1038 current item's tree, in an indirect buffer."
1039 :group 'org-agenda
1040 :version "24.1"
1041 :type 'boolean)
1043 (defcustom org-agenda-show-outline-path t
1044 "Non-nil means show outline path in echo area after line motion."
1045 :group 'org-agenda-startup
1046 :type 'boolean)
1048 (defcustom org-agenda-start-with-entry-text-mode nil
1049 "The initial value of entry-text-mode in a newly created agenda window."
1050 :group 'org-agenda-startup
1051 :type 'boolean)
1053 (defcustom org-agenda-entry-text-maxlines 5
1054 "Number of text lines to be added when `E' is pressed in the agenda.
1056 Note that this variable only used during agenda display. To add entry text
1057 when exporting the agenda, configure the variable
1058 `org-agenda-add-entry-text-maxlines'."
1059 :group 'org-agenda
1060 :type 'integer)
1062 (defcustom org-agenda-entry-text-exclude-regexps nil
1063 "List of regular expressions to clean up entry text.
1064 The complete matches of all regular expressions in this list will be
1065 removed from entry text before it is shown in the agenda."
1066 :group 'org-agenda
1067 :type '(repeat (regexp)))
1069 (defcustom org-agenda-entry-text-leaders " > "
1070 "Text prepended to the entry text in agenda buffers."
1071 :version "24.4"
1072 :package-version '(Org . "8.0")
1073 :group 'org-agenda
1074 :type 'string)
1076 (defvar org-agenda-entry-text-cleanup-hook nil
1077 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1078 This cleanup is done in a temporary buffer, so the function may inspect and
1079 change the entire buffer.
1080 Some default stuff like drawers and scheduling/deadline dates will already
1081 have been removed when this is called, as will any matches for regular
1082 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1084 (defvar org-agenda-include-inactive-timestamps nil
1085 "Non-nil means include inactive time stamps in agenda and timeline.
1086 Dynamically scoped.")
1088 (defgroup org-agenda-windows nil
1089 "Options concerning the windows used by the Agenda in Org Mode."
1090 :tag "Org Agenda Windows"
1091 :group 'org-agenda)
1093 (defcustom org-agenda-window-setup 'reorganize-frame
1094 "How the agenda buffer should be displayed.
1095 Possible values for this option are:
1097 current-window Show agenda in the current window, keeping all other windows.
1098 other-window Use `switch-to-buffer-other-window' to display agenda.
1099 only-window Show agenda, deleting all other windows.
1100 reorganize-frame Show only two windows on the current frame, the current
1101 window and the agenda.
1102 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1103 Also, when exiting the agenda, kill that frame.
1104 See also the variable `org-agenda-restore-windows-after-quit'."
1105 :group 'org-agenda-windows
1106 :type '(choice
1107 (const current-window)
1108 (const other-frame)
1109 (const other-window)
1110 (const only-window)
1111 (const reorganize-frame)))
1113 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1114 "The min and max height of the agenda window as a fraction of frame height.
1115 The value of the variable is a cons cell with two numbers between 0 and 1.
1116 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1117 :group 'org-agenda-windows
1118 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1120 (defcustom org-agenda-restore-windows-after-quit nil
1121 "Non-nil means restore window configuration upon exiting agenda.
1122 Before the window configuration is changed for displaying the agenda,
1123 the current status is recorded. When the agenda is exited with
1124 `q' or `x' and this option is set, the old state is restored. If
1125 `org-agenda-window-setup' is `other-frame', the value of this
1126 option will be ignored."
1127 :group 'org-agenda-windows
1128 :type 'boolean)
1130 (defcustom org-agenda-span 'week
1131 "Number of days to include in overview display.
1132 Can be day, week, month, year, or any number of days.
1133 Custom commands can set this variable in the options section."
1134 :group 'org-agenda-daily/weekly
1135 :type '(choice (const :tag "Day" day)
1136 (const :tag "Week" week)
1137 (const :tag "Fortnight" fortnight)
1138 (const :tag "Month" month)
1139 (const :tag "Year" year)
1140 (integer :tag "Custom")))
1142 (defcustom org-agenda-start-on-weekday 1
1143 "Non-nil means start the overview always on the specified weekday.
1144 0 denotes Sunday, 1 denotes Monday, etc.
1145 When nil, always start on the current day.
1146 Custom commands can set this variable in the options section."
1147 :group 'org-agenda-daily/weekly
1148 :type '(choice (const :tag "Today" nil)
1149 (integer :tag "Weekday No.")))
1151 (defcustom org-agenda-show-all-dates t
1152 "Non-nil means `org-agenda' shows every day in the selected range.
1153 When nil, only the days which actually have entries are shown."
1154 :group 'org-agenda-daily/weekly
1155 :type 'boolean)
1157 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1158 "Format string for displaying dates in the agenda.
1159 Used by the daily/weekly agenda and by the timeline. This should be
1160 a format string understood by `format-time-string', or a function returning
1161 the formatted date as a string. The function must take a single argument,
1162 a calendar-style date list like (month day year)."
1163 :group 'org-agenda-daily/weekly
1164 :type '(choice
1165 (string :tag "Format string")
1166 (function :tag "Function")))
1168 (defun org-agenda-format-date-aligned (date)
1169 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1170 This function makes sure that dates are aligned for easy reading."
1171 (require 'cal-iso)
1172 (let* ((dayname (calendar-day-name date))
1173 (day (cadr date))
1174 (day-of-week (calendar-day-of-week date))
1175 (month (car date))
1176 (monthname (calendar-month-name month))
1177 (year (nth 2 date))
1178 (iso-week (org-days-to-iso-week
1179 (calendar-absolute-from-gregorian date)))
1180 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1181 (1- year))
1182 ((and (= month 12) (<= iso-week 1))
1183 (1+ year))
1184 (t year)))
1185 (weekstring (if (= day-of-week 1)
1186 (format " W%02d" iso-week)
1187 "")))
1188 (format "%-10s %2d %s %4d%s"
1189 dayname day monthname year weekstring)))
1191 (defcustom org-agenda-time-leading-zero nil
1192 "Non-nil means use leading zero for military times in agenda.
1193 For example, 9:30am would become 09:30 rather than 9:30."
1194 :group 'org-agenda-daily/weekly
1195 :version "24.1"
1196 :type 'boolean)
1198 (defcustom org-agenda-timegrid-use-ampm nil
1199 "When set, show AM/PM style timestamps on the timegrid."
1200 :group 'org-agenda
1201 :version "24.1"
1202 :type 'boolean)
1204 (defun org-agenda-time-of-day-to-ampm (time)
1205 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1206 (let* ((hour-number (string-to-number (substring time 0 -3)))
1207 (minute (substring time -2))
1208 (ampm "am"))
1209 (cond
1210 ((equal hour-number 12)
1211 (setq ampm "pm"))
1212 ((> hour-number 12)
1213 (setq ampm "pm")
1214 (setq hour-number (- hour-number 12))))
1215 (concat
1216 (if org-agenda-time-leading-zero
1217 (format "%02d" hour-number)
1218 (format "%02s" (number-to-string hour-number)))
1219 ":" minute ampm)))
1221 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1222 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1223 (if org-agenda-timegrid-use-ampm
1224 (org-agenda-time-of-day-to-ampm time)
1225 time))
1227 (defcustom org-agenda-weekend-days '(6 0)
1228 "Which days are weekend?
1229 These days get the special face `org-agenda-date-weekend' in the agenda
1230 and timeline buffers."
1231 :group 'org-agenda-daily/weekly
1232 :type '(set :greedy t
1233 (const :tag "Monday" 1)
1234 (const :tag "Tuesday" 2)
1235 (const :tag "Wednesday" 3)
1236 (const :tag "Thursday" 4)
1237 (const :tag "Friday" 5)
1238 (const :tag "Saturday" 6)
1239 (const :tag "Sunday" 0)))
1241 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1242 "Non-nil means jump to today when moving a past date forward in time.
1243 When using S-right in the agenda to move a a date forward, and the date
1244 stamp currently points to the past, the first key press will move it
1245 to today. WHen nil, just move one day forward even if the date stays
1246 in the past."
1247 :group 'org-agenda-daily/weekly
1248 :version "24.1"
1249 :type 'boolean)
1251 (defcustom org-agenda-include-diary nil
1252 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1253 Custom commands can set this variable in the options section."
1254 :group 'org-agenda-daily/weekly
1255 :type 'boolean)
1257 (defcustom org-agenda-include-deadlines t
1258 "If non-nil, include entries within their deadline warning period.
1259 Custom commands can set this variable in the options section."
1260 :group 'org-agenda-daily/weekly
1261 :version "24.1"
1262 :type 'boolean)
1264 (defcustom org-agenda-repeating-timestamp-show-all t
1265 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1266 When set to a list of strings, only show occurrences of repeating
1267 stamps for these TODO keywords. When nil, only one occurrence is
1268 shown, either today or the nearest into the future."
1269 :group 'org-agenda-daily/weekly
1270 :type '(choice
1271 (const :tag "Show repeating stamps" t)
1272 (repeat :tag "Show repeating stamps for these TODO keywords"
1273 (string :tag "TODO Keyword"))
1274 (const :tag "Don't show repeating stamps" nil)))
1276 (defcustom org-scheduled-past-days 10000
1277 "Number of days to continue listing scheduled items not marked DONE.
1278 When an item is scheduled on a date, it shows up in the agenda on
1279 this day and will be listed until it is marked done or for the
1280 number of days given here."
1281 :group 'org-agenda-daily/weekly
1282 :type 'integer)
1284 (defcustom org-agenda-log-mode-items '(closed clock)
1285 "List of items that should be shown in agenda log mode.
1286 \\<org-agenda-mode-map>\
1287 This list may contain the following symbols:
1289 closed Show entries that have been closed on that day.
1290 clock Show entries that have received clocked time on that day.
1291 state Show all logged state changes.
1292 Note that instead of changing this variable, you can also press \
1293 `\\[universal-argument] \\[org-agenda-log-mode]' in
1294 the agenda to display all available LOG items temporarily."
1295 :group 'org-agenda-daily/weekly
1296 :type '(set :greedy t (const closed) (const clock) (const state)))
1298 (defcustom org-agenda-clock-consistency-checks
1299 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1300 :gap-ok-around ("4:00")
1301 :default-face ((:background "DarkRed") (:foreground "white"))
1302 :overlap-face nil :gap-face nil :no-end-time-face nil
1303 :long-face nil :short-face nil)
1304 "This is a property list, with the following keys:
1306 :max-duration Mark clocking chunks that are longer than this time.
1307 This is a time string like \"HH:MM\", or the number
1308 of minutes as an integer.
1310 :min-duration Mark clocking chunks that are shorter that this.
1311 This is a time string like \"HH:MM\", or the number
1312 of minutes as an integer.
1314 :max-gap Mark gaps between clocking chunks that are longer than
1315 this duration. A number of minutes, or a string
1316 like \"HH:MM\".
1318 :gap-ok-around List of times during the day which are usually not working
1319 times. When a gap is detected, but the gap contains any
1320 of these times, the gap is *not* reported. For example,
1321 if this is (\"4:00\" \"13:00\") then gaps that contain
1322 4:00 in the morning (i.e. the night) and 13:00
1323 (i.e. a typical lunch time) do not cause a warning.
1324 You should have at least one time during the night in this
1325 list, or otherwise the first task each morning will trigger
1326 a warning because it follows a long gap.
1328 Furthermore, the following properties can be used to define faces for
1329 issue display.
1331 :default-face the default face, if the specific face is undefined
1332 :overlap-face face for overlapping clocks
1333 :gap-face face for gaps between clocks
1334 :no-end-time-face face for incomplete clocks
1335 :long-face face for clock intervals that are too long
1336 :short-face face for clock intervals that are too short"
1337 :group 'org-agenda-daily/weekly
1338 :group 'org-clock
1339 :version "24.1"
1340 :type 'plist)
1342 (defcustom org-agenda-log-mode-add-notes t
1343 "Non-nil means add first line of notes to log entries in agenda views.
1344 If a log item like a state change or a clock entry is associated with
1345 notes, the first line of these notes will be added to the entry in the
1346 agenda display."
1347 :group 'org-agenda-daily/weekly
1348 :type 'boolean)
1350 (defcustom org-agenda-start-with-log-mode nil
1351 "The initial value of log-mode in a newly created agenda window.
1352 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1353 explanations on the possible values."
1354 :group 'org-agenda-startup
1355 :group 'org-agenda-daily/weekly
1356 :type '(choice (const :tag "Don't show log items" nil)
1357 (const :tag "Show only log items" only)
1358 (const :tag "Show all possible log items" clockcheck)
1359 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1360 (choice (const :tag "Show closed log items" closed)
1361 (const :tag "Show clocked log items" clock)
1362 (const :tag "Show all logged state changes" state)))))
1364 (defcustom org-agenda-start-with-clockreport-mode nil
1365 "The initial value of clockreport-mode in a newly created agenda window."
1366 :group 'org-agenda-startup
1367 :group 'org-agenda-daily/weekly
1368 :type 'boolean)
1370 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1371 "Property list with parameters for the clocktable in clockreport mode.
1372 This is the display mode that shows a clock table in the daily/weekly
1373 agenda, the properties for this dynamic block can be set here.
1374 The usual clocktable parameters are allowed here, but you cannot set
1375 the properties :name, :tstart, :tend, :block, and :scope - these will
1376 be overwritten to make sure the content accurately reflects the
1377 current display in the agenda."
1378 :group 'org-agenda-daily/weekly
1379 :type 'plist)
1381 (defcustom org-agenda-search-view-always-boolean nil
1382 "Non-nil means the search string is interpreted as individual parts.
1384 The search string for search view can either be interpreted as a phrase,
1385 or as a list of snippets that define a boolean search for a number of
1386 strings.
1388 When this is non-nil, the string will be split on whitespace, and each
1389 snippet will be searched individually, and all must match in order to
1390 select an entry. A snippet is then a single string of non-white
1391 characters, or a string in double quotes, or a regexp in {} braces.
1392 If a snippet is preceded by \"-\", the snippet must *not* match.
1393 \"+\" is syntactic sugar for positive selection. Each snippet may
1394 be found as a full word or a partial word, but see the variable
1395 `org-agenda-search-view-force-full-words'.
1397 When this is nil, search will look for the entire search phrase as one,
1398 with each space character matching any amount of whitespace, including
1399 line breaks.
1401 Even when this is nil, you can still switch to Boolean search dynamically
1402 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1403 is a regexp marked with braces like \"{abc}\", this will also switch to
1404 boolean search."
1405 :group 'org-agenda-search-view
1406 :version "24.1"
1407 :type 'boolean)
1409 (defvaralias 'org-agenda-search-view-search-words-only
1410 'org-agenda-search-view-always-boolean)
1412 (defcustom org-agenda-search-view-force-full-words nil
1413 "Non-nil means, search words must be matches as complete words.
1414 When nil, they may also match part of a word."
1415 :group 'org-agenda-search-view
1416 :version "24.1"
1417 :type 'boolean)
1419 (defcustom org-agenda-search-view-max-outline-level 0
1420 "Maximum outline level to display in search view.
1421 E.g. when this is set to 1, the search view will only
1422 show headlines of level 1. When set to 0, the default
1423 value, don't limit agenda view by outline level."
1424 :group 'org-agenda-search-view
1425 :version "24.4"
1426 :package-version '(Org . "8.3")
1427 :type 'integer)
1429 (defgroup org-agenda-time-grid nil
1430 "Options concerning the time grid in the Org Agenda."
1431 :tag "Org Agenda Time Grid"
1432 :group 'org-agenda)
1434 (defcustom org-agenda-search-headline-for-time t
1435 "Non-nil means search headline for a time-of-day.
1436 If the headline contains a time-of-day in one format or another, it will
1437 be used to sort the entry into the time sequence of items for a day.
1438 Some people have time stamps in the headline that refer to the creation
1439 time or so, and then this produces an unwanted side effect. If this is
1440 the case for your, use this variable to turn off searching the headline
1441 for a time."
1442 :group 'org-agenda-time-grid
1443 :type 'boolean)
1445 (defcustom org-agenda-use-time-grid t
1446 "Non-nil means show a time grid in the agenda schedule.
1447 A time grid is a set of lines for specific times (like every two hours between
1448 8:00 and 20:00). The items scheduled for a day at specific times are
1449 sorted in between these lines.
1450 For details about when the grid will be shown, and what it will look like, see
1451 the variable `org-agenda-time-grid'."
1452 :group 'org-agenda-time-grid
1453 :type 'boolean)
1455 (defcustom org-agenda-time-grid
1456 '((daily today require-timed)
1457 "----------------"
1458 (800 1000 1200 1400 1600 1800 2000))
1460 "The settings for time grid for agenda display.
1461 This is a list of three items. The first item is again a list. It contains
1462 symbols specifying conditions when the grid should be displayed:
1464 daily if the agenda shows a single day
1465 weekly if the agenda shows an entire week
1466 today show grid on current date, independent of daily/weekly display
1467 require-timed show grid only if at least one item has a time specification
1468 remove-match skip grid times already present in an entry
1470 The second item is a string which will be placed behind the grid time.
1472 The third item is a list of integers, indicating the times that should have
1473 a grid line."
1474 :group 'org-agenda-time-grid
1475 :type
1476 '(list
1477 (set :greedy t :tag "Grid Display Options"
1478 (const :tag "Show grid in single day agenda display" daily)
1479 (const :tag "Show grid in weekly agenda display" weekly)
1480 (const :tag "Always show grid for today" today)
1481 (const :tag "Show grid only if any timed entries are present"
1482 require-timed)
1483 (const :tag "Skip grid times already present in an entry"
1484 remove-match))
1485 (string :tag "Grid String")
1486 (repeat :tag "Grid Times" (integer :tag "Time"))))
1488 (defcustom org-agenda-show-current-time-in-grid t
1489 "Non-nil means show the current time in the time grid."
1490 :group 'org-agenda-time-grid
1491 :version "24.1"
1492 :type 'boolean)
1494 (defcustom org-agenda-current-time-string
1495 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1496 "The string for the current time marker in the agenda."
1497 :group 'org-agenda-time-grid
1498 :version "24.1"
1499 :type 'string)
1501 (defgroup org-agenda-sorting nil
1502 "Options concerning sorting in the Org Agenda."
1503 :tag "Org Agenda Sorting"
1504 :group 'org-agenda)
1506 (defcustom org-agenda-sorting-strategy
1507 '((agenda habit-down time-up priority-down category-keep)
1508 (todo priority-down category-keep)
1509 (tags priority-down category-keep)
1510 (search category-keep))
1511 "Sorting structure for the agenda items of a single day.
1512 This is a list of symbols which will be used in sequence to determine
1513 if an entry should be listed before another entry. The following
1514 symbols are recognized:
1516 time-up Put entries with time-of-day indications first, early first
1517 time-down Put entries with time-of-day indications first, late first
1518 timestamp-up Sort by any timestamp, early first
1519 timestamp-down Sort by any timestamp, late first
1520 scheduled-up Sort by scheduled timestamp, early first
1521 scheduled-down Sort by scheduled timestamp, late first
1522 deadline-up Sort by deadline timestamp, early first
1523 deadline-down Sort by deadline timestamp, late first
1524 ts-up Sort by active timestamp, early first
1525 ts-down Sort by active timestamp, late first
1526 tsia-up Sort by inactive timestamp, early first
1527 tsia-down Sort by inactive timestamp, late first
1528 category-keep Keep the default order of categories, corresponding to the
1529 sequence in `org-agenda-files'.
1530 category-up Sort alphabetically by category, A-Z.
1531 category-down Sort alphabetically by category, Z-A.
1532 tag-up Sort alphabetically by last tag, A-Z.
1533 tag-down Sort alphabetically by last tag, Z-A.
1534 priority-up Sort numerically by priority, high priority last.
1535 priority-down Sort numerically by priority, high priority first.
1536 todo-state-up Sort by todo state, tasks that are done last.
1537 todo-state-down Sort by todo state, tasks that are done first.
1538 effort-up Sort numerically by estimated effort, high effort last.
1539 effort-down Sort numerically by estimated effort, high effort first.
1540 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1541 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1542 habit-up Put entries that are habits first
1543 habit-down Put entries that are habits last
1544 alpha-up Sort headlines alphabetically
1545 alpha-down Sort headlines alphabetically, reversed
1547 The different possibilities will be tried in sequence, and testing stops
1548 if one comparison returns a \"not-equal\". For example, the default
1549 '(time-up category-keep priority-down)
1550 means: Pull out all entries having a specified time of day and sort them,
1551 in order to make a time schedule for the current day the first thing in the
1552 agenda listing for the day. Of the entries without a time indication, keep
1553 the grouped in categories, don't sort the categories, but keep them in
1554 the sequence given in `org-agenda-files'. Within each category sort by
1555 priority.
1557 Leaving out `category-keep' would mean that items will be sorted across
1558 categories by priority.
1560 Instead of a single list, this can also be a set of list for specific
1561 contents, with a context symbol in the car of the list, any of
1562 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1564 Custom commands can bind this variable in the options section."
1565 :group 'org-agenda-sorting
1566 :type `(choice
1567 (repeat :tag "General" ,org-sorting-choice)
1568 (list :tag "Individually"
1569 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1570 (repeat ,org-sorting-choice))
1571 (cons (const :tag "Strategy for TODO lists" todo)
1572 (repeat ,org-sorting-choice))
1573 (cons (const :tag "Strategy for Tags matches" tags)
1574 (repeat ,org-sorting-choice))
1575 (cons (const :tag "Strategy for search matches" search)
1576 (repeat ,org-sorting-choice)))))
1578 (defcustom org-agenda-cmp-user-defined nil
1579 "A function to define the comparison `user-defined'.
1580 This function must receive two arguments, agenda entry a and b.
1581 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1582 the user comparison, return nil.
1583 When this is defined, you can make `user-defined-up' and `user-defined-down'
1584 part of an agenda sorting strategy."
1585 :group 'org-agenda-sorting
1586 :type 'symbol)
1588 (defcustom org-sort-agenda-notime-is-late t
1589 "Non-nil means items without time are considered late.
1590 This is only relevant for sorting. When t, items which have no explicit
1591 time like 15:30 will be considered as 99:01, i.e. later than any items which
1592 do have a time. When nil, the default time is before 0:00. You can use this
1593 option to decide if the schedule for today should come before or after timeless
1594 agenda entries."
1595 :group 'org-agenda-sorting
1596 :type 'boolean)
1598 (defcustom org-sort-agenda-noeffort-is-high t
1599 "Non-nil means items without effort estimate are sorted as high effort.
1600 This also applies when filtering an agenda view with respect to the
1601 < or > effort operator. Then, tasks with no effort defined will be treated
1602 as tasks with high effort.
1603 When nil, such items are sorted as 0 minutes effort."
1604 :group 'org-agenda-sorting
1605 :type 'boolean)
1607 (defgroup org-agenda-line-format nil
1608 "Options concerning the entry prefix in the Org agenda display."
1609 :tag "Org Agenda Line Format"
1610 :group 'org-agenda)
1612 (defcustom org-agenda-prefix-format
1613 '((agenda . " %i %-12:c%?-12t% s")
1614 (timeline . " % s")
1615 (todo . " %i %-12:c")
1616 (tags . " %i %-12:c")
1617 (search . " %i %-12:c"))
1618 "Format specifications for the prefix of items in the agenda views.
1619 An alist with five entries, each for the different agenda types. The
1620 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1621 The values are format strings.
1623 This format works similar to a printf format, with the following meaning:
1625 %c the category of the item, \"Diary\" for entries from the diary,
1626 or as given by the CATEGORY keyword or derived from the file name
1627 %e the effort required by the item
1628 %l the level of the item (insert X space(s) if item is of level X)
1629 %i the icon category of the item, see `org-agenda-category-icon-alist'
1630 %T the last tag of the item (ignore inherited tags, which come first)
1631 %t the HH:MM time-of-day specification if one applies to the entry
1632 %s Scheduling/Deadline information, a short string
1633 %b show breadcrumbs, i.e., the names of the higher levels
1634 %(expression) Eval EXPRESSION and replace the control string
1635 by the result
1637 All specifiers work basically like the standard `%s' of printf, but may
1638 contain two additional characters: a question mark just after the `%'
1639 and a whitespace/punctuation character just before the final letter.
1641 If the first character after `%' is a question mark, the entire field
1642 will only be included if the corresponding value applies to the current
1643 entry. This is useful for fields which should have fixed width when
1644 present, but zero width when absent. For example, \"%?-12t\" will
1645 result in a 12 character time field if a time of the day is specified,
1646 but will completely disappear in entries which do not contain a time.
1648 If there is punctuation or whitespace character just before the
1649 final format letter, this character will be appended to the field
1650 value if the value is not empty. For example, the format
1651 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1652 the category is empty, no additional colon is inserted.
1654 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1655 which means:
1657 - Indent the line with two space characters
1658 - Give the category a 12 chars wide field, padded with whitespace on
1659 the right (because of `-'). Append a colon if there is a category
1660 (because of `:').
1661 - If there is a time-of-day, put it into a 12 chars wide field. If no
1662 time, don't put in an empty field, just skip it (because of '?').
1663 - Finally, put the scheduling information.
1665 See also the variables `org-agenda-remove-times-when-in-prefix' and
1666 `org-agenda-remove-tags'.
1668 Custom commands can set this variable in the options section."
1669 :type '(choice
1670 (string :tag "General format")
1671 (list :greedy t :tag "View dependent"
1672 (cons (const agenda) (string :tag "Format"))
1673 (cons (const timeline) (string :tag "Format"))
1674 (cons (const todo) (string :tag "Format"))
1675 (cons (const tags) (string :tag "Format"))
1676 (cons (const search) (string :tag "Format"))))
1677 :group 'org-agenda-line-format)
1679 (defvar org-prefix-format-compiled nil
1680 "The compiled prefix format and associated variables.
1681 This is a list where first element is a list of variable bindings, and second
1682 element is the compiled format expression. See the variable
1683 `org-agenda-prefix-format'.")
1685 (defcustom org-agenda-todo-keyword-format "%-1s"
1686 "Format for the TODO keyword in agenda lines.
1687 Set this to something like \"%-12s\" if you want all TODO keywords
1688 to occupy a fixed space in the agenda display."
1689 :group 'org-agenda-line-format
1690 :type 'string)
1692 (defcustom org-agenda-diary-sexp-prefix nil
1693 "A regexp that matches part of a diary sexp entry
1694 which should be treated as scheduling/deadline information in
1695 `org-agenda'.
1697 For example, you can use this to extract the `diary-remind-message' from
1698 `diary-remind' entries."
1699 :group 'org-agenda-line-format
1700 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1702 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1703 "Text preceding timerange entries in the agenda view.
1704 This is a list with two strings. The first applies when the range
1705 is entirely on one day. The second applies if the range spans several days.
1706 The strings may have two \"%d\" format specifiers which will be filled
1707 with the sequence number of the days, and the total number of days in the
1708 range, respectively."
1709 :group 'org-agenda-line-format
1710 :type '(list
1711 (string :tag "Deadline today ")
1712 (choice :tag "Deadline relative"
1713 (string :tag "Format string")
1714 (function))))
1716 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1717 "Text preceding scheduled items in the agenda view.
1718 This is a list with two strings. The first applies when the item is
1719 scheduled on the current day. The second applies when it has been scheduled
1720 previously, it may contain a %d indicating that this is the nth time that
1721 this item is scheduled, due to automatic rescheduling of unfinished items
1722 for the following day. So this number is one larger than the number of days
1723 that passed since this item was scheduled first."
1724 :group 'org-agenda-line-format
1725 :version "24.4"
1726 :package-version '(Org . "8.0")
1727 :type '(list
1728 (string :tag "Scheduled today ")
1729 (string :tag "Scheduled previously")))
1731 (defcustom org-agenda-inactive-leader "["
1732 "Text preceding item pulled into the agenda by inactive time stamps.
1733 These entries are added to the agenda when pressing \"[\"."
1734 :group 'org-agenda-line-format
1735 :version "24.1"
1736 :type 'string)
1738 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1739 "Text preceding deadline items in the agenda view.
1740 This is a list with three strings. The first applies when the item has its
1741 deadline on the current day. The second applies when the deadline is in the
1742 future, the third one when it is in the past. The strings may contain %d
1743 to capture the number of days."
1744 :group 'org-agenda-line-format
1745 :version "24.4"
1746 :package-version '(Org . "8.0")
1747 :type '(list
1748 (string :tag "Deadline today ")
1749 (string :tag "Deadline in the future ")
1750 (string :tag "Deadline in the past ")))
1752 (defcustom org-agenda-remove-times-when-in-prefix t
1753 "Non-nil means remove duplicate time specifications in agenda items.
1754 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1755 time-of-day specification in a headline or diary entry is extracted and
1756 placed into the prefix. If this option is non-nil, the original specification
1757 \(a timestamp or -range, or just a plain time(range) specification like
1758 11:30-4pm) will be removed for agenda display. This makes the agenda less
1759 cluttered.
1760 The option can be t or nil. It may also be the symbol `beg', indicating
1761 that the time should only be removed when it is located at the beginning of
1762 the headline/diary entry."
1763 :group 'org-agenda-line-format
1764 :type '(choice
1765 (const :tag "Always" t)
1766 (const :tag "Never" nil)
1767 (const :tag "When at beginning of entry" beg)))
1769 (defcustom org-agenda-remove-timeranges-from-blocks nil
1770 "Non-nil means remove time ranges specifications in agenda
1771 items that span on several days."
1772 :group 'org-agenda-line-format
1773 :version "24.1"
1774 :type 'boolean)
1776 (defcustom org-agenda-default-appointment-duration nil
1777 "Default duration for appointments that only have a starting time.
1778 When nil, no duration is specified in such cases.
1779 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1780 :group 'org-agenda-line-format
1781 :type '(choice
1782 (integer :tag "Minutes")
1783 (const :tag "No default duration")))
1785 (defcustom org-agenda-show-inherited-tags t
1786 "Non-nil means show inherited tags in each agenda line.
1788 When this option is set to `always', it take precedences over
1789 `org-agenda-use-tag-inheritance' and inherited tags are shown
1790 in every agenda.
1792 When this option is set to t (the default), inherited tags are
1793 shown when they are available, i.e. when the value of
1794 `org-agenda-use-tag-inheritance' enables tag inheritance for the
1795 given agenda type.
1797 This can be set to a list of agenda types in which the agenda
1798 must display the inherited tags. Available types are `todo',
1799 `agenda', `search' and `timeline'.
1801 When set to nil, never show inherited tags in agenda lines."
1802 :group 'org-agenda-line-format
1803 :group 'org-agenda
1804 :version "24.3"
1805 :type '(choice
1806 (const :tag "Show inherited tags when available" t)
1807 (const :tag "Always show inherited tags" always)
1808 (repeat :tag "Show inherited tags only in selected agenda types"
1809 (symbol :tag "Agenda type"))))
1811 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1812 "List of agenda view types where to use tag inheritance.
1814 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1815 controlled by `org-use-tag-inheritance'. In other agenda types,
1816 `org-use-tag-inheritance' is not used for the selection of the
1817 agenda entries. Still, you may want the agenda to be aware of
1818 the inherited tags anyway, e.g. for later tag filtering.
1820 Allowed value are `todo', `search', `timeline' and `agenda'.
1822 This variable has no effect if `org-agenda-show-inherited-tags'
1823 is set to `always'. In that case, the agenda is aware of those
1824 tags.
1826 The default value sets tags in every agenda type. Setting this
1827 option to nil will speed up non-tags agenda view a lot."
1828 :group 'org-agenda
1829 :version "24.3"
1830 :type '(choice
1831 (const :tag "Use tag inheritance in all agenda types" t)
1832 (repeat :tag "Use tag inheritance in selected agenda types"
1833 (symbol :tag "Agenda type"))))
1835 (defcustom org-agenda-hide-tags-regexp nil
1836 "Regular expression used to filter away specific tags in agenda views.
1837 This means that these tags will be present, but not be shown in the agenda
1838 line. Secondary filtering will still work on the hidden tags.
1839 Nil means don't hide any tags."
1840 :group 'org-agenda-line-format
1841 :type '(choice
1842 (const :tag "Hide none" nil)
1843 (string :tag "Regexp ")))
1845 (defcustom org-agenda-remove-tags nil
1846 "Non-nil means remove the tags from the headline copy in the agenda.
1847 When this is the symbol `prefix', only remove tags when
1848 `org-agenda-prefix-format' contains a `%T' specifier."
1849 :group 'org-agenda-line-format
1850 :type '(choice
1851 (const :tag "Always" t)
1852 (const :tag "Never" nil)
1853 (const :tag "When prefix format contains %T" prefix)))
1855 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1856 'org-agenda-remove-tags)
1858 (defcustom org-agenda-tags-column -80
1859 "Shift tags in agenda items to this column.
1860 If this number is positive, it specifies the column. If it is negative,
1861 it means that the tags should be flushright to that column. For example,
1862 -80 works well for a normal 80 character screen."
1863 :group 'org-agenda-line-format
1864 :type 'integer)
1866 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1868 (defcustom org-agenda-fontify-priorities 'cookies
1869 "Non-nil means highlight low and high priorities in agenda.
1870 When t, the highest priority entries are bold, lowest priority italic.
1871 However, settings in `org-priority-faces' will overrule these faces.
1872 When this variable is the symbol `cookies', only fontify the
1873 cookies, not the entire task.
1874 This may also be an association list of priority faces, whose
1875 keys are the character values of `org-highest-priority',
1876 `org-default-priority', and `org-lowest-priority' (the default values
1877 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1878 color as a string, or a list like `(:background \"Red\")'.
1879 If it is a color, the variable `org-faces-easy-properties'
1880 determines if it is a foreground or a background color."
1881 :group 'org-agenda-line-format
1882 :type '(choice
1883 (const :tag "Never" nil)
1884 (const :tag "Defaults" t)
1885 (const :tag "Cookies only" cookies)
1886 (repeat :tag "Specify"
1887 (list (character :tag "Priority" :value ?A)
1888 (choice :tag "Face "
1889 (string :tag "Color")
1890 (sexp :tag "Face"))))))
1892 (defcustom org-agenda-day-face-function nil
1893 "Function called to determine what face should be used to display a day.
1894 The only argument passed to that function is the day. It should
1895 returns a face, or nil if does not want to specify a face and let
1896 the normal rules apply."
1897 :group 'org-agenda-line-format
1898 :version "24.1"
1899 :type '(choice (const nil) (function)))
1901 (defcustom org-agenda-category-icon-alist nil
1902 "Alist of category icon to be displayed in agenda views.
1904 Each entry should have the following format:
1906 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1908 Where CATEGORY-REGEXP is a regexp matching the categories where
1909 the icon should be displayed.
1910 FILE-OR-DATA either a file path or a string containing image data.
1912 The other fields can be omitted safely if not needed:
1913 TYPE indicates the image type.
1914 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1915 image data.
1916 PROPS are additional image attributes to assign to the image,
1917 like, e.g. `:ascent center'.
1919 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1921 If you want to set the display properties yourself, just put a
1922 list as second element:
1924 (CATEGORY-REGEXP (MY PROPERTY LIST))
1926 For example, to display a 16px horizontal space for Emacs
1927 category, you can use:
1929 (\"Emacs\" \\='(space . (:width (16))))"
1930 :group 'org-agenda-line-format
1931 :version "24.1"
1932 :type '(alist :key-type (string :tag "Regexp matching category")
1933 :value-type (choice (list :tag "Icon"
1934 (string :tag "File or data")
1935 (symbol :tag "Type")
1936 (boolean :tag "Data?")
1937 (repeat :tag "Extra image properties" :inline t symbol))
1938 (list :tag "Display properties" sexp))))
1940 (defgroup org-agenda-column-view nil
1941 "Options concerning column view in the agenda."
1942 :tag "Org Agenda Column View"
1943 :group 'org-agenda)
1945 (defcustom org-agenda-view-columns-initially nil
1946 "When non-nil, switch to columns view right after creating the agenda."
1947 :group 'org-agenda-column-view
1948 :type 'boolean
1949 :version "25.2"
1950 :package-version '(Org . "9.0")
1951 :safe #'booleanp)
1953 (defcustom org-agenda-columns-show-summaries t
1954 "Non-nil means show summaries for columns displayed in the agenda view."
1955 :group 'org-agenda-column-view
1956 :type 'boolean)
1958 (defcustom org-agenda-columns-compute-summary-properties t
1959 "Non-nil means recompute all summary properties before column view.
1960 When column view in the agenda is listing properties that have a summary
1961 operator, it can go to all relevant buffers and recompute the summaries
1962 there. This can mean overhead for the agenda column view, but is necessary
1963 to have thing up to date.
1964 As a special case, a CLOCKSUM property also makes sure that the clock
1965 computations are current."
1966 :group 'org-agenda-column-view
1967 :type 'boolean)
1969 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1970 "Non-nil means the duration of an appointment will add to day effort.
1971 The property to which appointment durations will be added is the one given
1972 in the option `org-effort-property'. If an appointment does not have
1973 an end time, `org-agenda-default-appointment-duration' will be used. If that
1974 is not set, an appointment without end time will not contribute to the time
1975 estimate."
1976 :group 'org-agenda-column-view
1977 :type 'boolean)
1979 (defcustom org-agenda-auto-exclude-function nil
1980 "A function called with a tag to decide if it is filtered on \
1981 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1982 The sole argument to the function, which is called once for each
1983 possible tag, is a string giving the name of the tag. The
1984 function should return either nil if the tag should be included
1985 as normal, or \"-<TAG>\" to exclude the tag.
1986 Note that for the purpose of tag filtering, only the lower-case version of
1987 all tags will be considered, so that this function will only ever see
1988 the lower-case version of all tags."
1989 :group 'org-agenda
1990 :type '(choice (const nil) (function)))
1992 (defcustom org-agenda-bulk-custom-functions nil
1993 "Alist of characters and custom functions for bulk actions.
1994 For example, this value makes those two functions available:
1996 \\='((?R set-category)
1997 (?C bulk-cut))
1999 With selected entries in an agenda buffer, `B R' will call
2000 the custom function `set-category' on the selected entries.
2001 Note that functions in this alist don't need to be quoted."
2002 :type '(alist :key-type character :value-type (group function))
2003 :version "24.1"
2004 :group 'org-agenda)
2006 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2007 "Execute BODY with point at location given by `org-hd-marker' property.
2008 If STRING is non-nil, the text property will be fetched from position 0
2009 in that string. If STRING is nil, it will be fetched from the beginning
2010 of the current line."
2011 (org-with-gensyms (marker)
2012 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2013 'org-hd-marker ,string)))
2014 (with-current-buffer (marker-buffer ,marker)
2015 (save-excursion
2016 (goto-char ,marker)
2017 ,@body)))))
2018 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2020 (defun org-add-agenda-custom-command (entry)
2021 "Replace or add a command in `org-agenda-custom-commands'.
2022 This is mostly for hacking and trying a new command - once the command
2023 works you probably want to add it to `org-agenda-custom-commands' for good."
2024 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2025 (if ass
2026 (setcdr ass (cdr entry))
2027 (push entry org-agenda-custom-commands))))
2029 ;;; Define the org-agenda-mode
2031 (defvar org-agenda-mode-map (make-sparse-keymap)
2032 "Keymap for `org-agenda-mode'.")
2033 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2035 (defvar org-agenda-menu) ; defined later in this file.
2036 (defvar org-agenda-restrict nil) ; defined later in this file.
2037 (defvar org-agenda-follow-mode nil)
2038 (defvar org-agenda-entry-text-mode nil)
2039 (defvar org-agenda-clockreport-mode nil)
2040 (defvar org-agenda-show-log nil)
2041 (defvar org-agenda-redo-command nil)
2042 (defvar org-agenda-query-string nil)
2043 (defvar org-agenda-mode-hook nil
2044 "Hook run after `org-agenda-mode' is turned on.
2045 The buffer is still writable when this hook is called.")
2046 (defvar org-agenda-type nil)
2047 (defvar org-agenda-force-single-file nil)
2048 (defvar org-agenda-bulk-marked-entries nil
2049 "List of markers that refer to marked entries in the agenda.")
2050 (defvar org-agenda-current-date nil
2051 "Active date when building the agenda.")
2053 ;;; Multiple agenda buffers support
2055 (defcustom org-agenda-sticky nil
2056 "Non-nil means agenda q key will bury agenda buffers.
2057 Agenda commands will then show existing buffer instead of generating new ones.
2058 When nil, `q' will kill the single agenda buffer."
2059 :group 'org-agenda
2060 :version "24.3"
2061 :type 'boolean)
2064 ;;;###autoload
2065 (defun org-toggle-sticky-agenda (&optional arg)
2066 "Toggle `org-agenda-sticky'."
2067 (interactive "P")
2068 (let ((new-value (if arg
2069 (> (prefix-numeric-value arg) 0)
2070 (not org-agenda-sticky))))
2071 (if (equal new-value org-agenda-sticky)
2072 (and (called-interactively-p 'interactive)
2073 (message "Sticky agenda was already %s"
2074 (if org-agenda-sticky "enabled" "disabled")))
2075 (setq org-agenda-sticky new-value)
2076 (org-agenda-kill-all-agenda-buffers)
2077 (and (called-interactively-p 'interactive)
2078 (message "Sticky agenda %s"
2079 (if org-agenda-sticky "enabled" "disabled"))))))
2081 (defvar org-agenda-buffer nil
2082 "Agenda buffer currently being generated.")
2084 (defvar org-agenda-last-prefix-arg nil)
2085 (defvar org-agenda-this-buffer-name nil)
2086 (defvar org-agenda-doing-sticky-redo nil)
2087 (defvar org-agenda-this-buffer-is-sticky nil)
2088 (defvar org-agenda-last-indirect-buffer nil
2089 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2091 (defconst org-agenda-local-vars
2092 '(org-agenda-this-buffer-name
2093 org-agenda-undo-list
2094 org-agenda-pending-undo-list
2095 org-agenda-follow-mode
2096 org-agenda-entry-text-mode
2097 org-agenda-clockreport-mode
2098 org-agenda-show-log
2099 org-agenda-redo-command
2100 org-agenda-query-string
2101 org-agenda-type
2102 org-agenda-bulk-marked-entries
2103 org-agenda-undo-has-started-in
2104 org-agenda-info
2105 org-agenda-pre-window-conf
2106 org-agenda-columns-active
2107 org-agenda-tag-filter
2108 org-agenda-category-filter
2109 org-agenda-top-headline-filter
2110 org-agenda-regexp-filter
2111 org-agenda-effort-filter
2112 org-agenda-markers
2113 org-agenda-last-search-view-search-was-boolean
2114 org-agenda-last-indirect-buffer
2115 org-agenda-filtered-by-category
2116 org-agenda-filter-form
2117 org-agenda-cycle-counter
2118 org-agenda-last-prefix-arg)
2119 "Variables that must be local in agenda buffers to allow multiple buffers.")
2121 (defun org-agenda-mode ()
2122 "Mode for time-sorted view on action items in Org files.
2124 The following commands are available:
2126 \\{org-agenda-mode-map}"
2127 (interactive)
2128 (cond (org-agenda-doing-sticky-redo
2129 ;; Refreshing sticky agenda-buffer
2131 ;; Preserve the value of `org-agenda-local-vars' variables,
2132 ;; while letting `kill-all-local-variables' kill the rest
2133 (let ((save (buffer-local-variables)))
2134 (kill-all-local-variables)
2135 (mapc 'make-local-variable org-agenda-local-vars)
2136 (dolist (elem save)
2137 (let ((var (car elem))
2138 (val (cdr elem)))
2139 (when (and val
2140 (member var org-agenda-local-vars))
2141 (set var val)))))
2142 (setq-local org-agenda-this-buffer-is-sticky t))
2143 (org-agenda-sticky
2144 ;; Creating a sticky Agenda buffer for the first time
2145 (kill-all-local-variables)
2146 (mapc 'make-local-variable org-agenda-local-vars)
2147 (setq-local org-agenda-this-buffer-is-sticky t))
2149 ;; Creating a non-sticky agenda buffer
2150 (kill-all-local-variables)
2151 (setq-local org-agenda-this-buffer-is-sticky nil)))
2152 (setq org-agenda-undo-list nil
2153 org-agenda-pending-undo-list nil
2154 org-agenda-bulk-marked-entries nil)
2155 (setq major-mode 'org-agenda-mode)
2156 ;; Keep global-font-lock-mode from turning on font-lock-mode
2157 (setq-local font-lock-global-modes (list 'not major-mode))
2158 (setq mode-name "Org-Agenda")
2159 (setq indent-tabs-mode nil)
2160 (use-local-map org-agenda-mode-map)
2161 (easy-menu-add org-agenda-menu)
2162 (if org-startup-truncated (setq truncate-lines t))
2163 (setq-local line-move-visual nil)
2164 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2165 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2166 ;; Make sure properties are removed when copying text
2167 (add-hook 'filter-buffer-substring-functions
2168 (lambda (fun start end delete)
2169 (substring-no-properties (funcall fun start end delete)))
2170 nil t)
2171 (unless org-agenda-keep-modes
2172 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2173 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2174 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2175 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2176 (add-to-invisibility-spec '(org-filtered))
2177 (add-to-invisibility-spec '(org-link))
2178 (easy-menu-change
2179 '("Agenda") "Agenda Files"
2180 (append
2181 (list
2182 (vector
2183 (if (get 'org-agenda-files 'org-restrict)
2184 "Restricted to single file"
2185 "Edit File List")
2186 '(org-edit-agenda-file-list)
2187 (not (get 'org-agenda-files 'org-restrict)))
2188 "--")
2189 (mapcar 'org-file-menu-entry (org-agenda-files))))
2190 (org-agenda-set-mode-name)
2191 (apply
2192 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2193 (list 'org-agenda-mode-hook)))
2195 (substitute-key-definition 'undo 'org-agenda-undo
2196 org-agenda-mode-map global-map)
2197 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2198 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2199 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2200 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2201 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2202 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2203 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2204 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2205 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2206 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2207 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2208 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2209 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2210 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2211 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2212 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2213 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2214 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2215 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2216 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2217 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2218 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2219 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2220 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2221 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2222 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2223 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2224 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2225 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2226 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2227 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2228 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2229 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2230 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2231 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2232 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2233 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2234 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2235 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2236 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2237 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2238 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2239 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2240 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2241 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2242 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2243 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2244 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2245 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2246 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2248 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2249 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2250 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2251 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2252 (while l (org-defkey org-agenda-mode-map
2253 (int-to-string (pop l)) 'digit-argument)))
2255 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2256 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2257 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2258 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2259 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2260 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2261 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2262 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2263 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2264 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2265 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2266 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2267 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2268 'org-clock-modify-effort-estimate)
2269 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2270 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2271 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2272 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2273 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2274 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2275 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2276 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2277 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2278 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2279 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2280 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2281 (substitute-key-definition 'next-line 'org-agenda-next-line
2282 org-agenda-mode-map global-map)
2283 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2284 org-agenda-mode-map global-map)
2285 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2286 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2287 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2288 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2289 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2290 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2291 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2292 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2293 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2294 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2295 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2296 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2297 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2298 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2299 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2300 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2301 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2302 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2303 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2304 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2305 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2306 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2307 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2308 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2309 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2310 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2311 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2312 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2313 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2314 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2316 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2317 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2318 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2319 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2320 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2321 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2322 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2323 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2324 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2325 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2326 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2327 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2328 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2329 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2330 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2332 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2333 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2335 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2336 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2338 (when org-agenda-mouse-1-follows-link
2339 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2340 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2341 '("Agenda"
2342 ("Agenda Files")
2343 "--"
2344 ("Agenda Dates"
2345 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2346 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2347 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2348 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2349 "--"
2350 ("View"
2351 ["Day View" org-agenda-day-view
2352 :active (org-agenda-check-type nil 'agenda)
2353 :style radio :selected (eq org-agenda-current-span 'day)
2354 :keys "v d (or just d)"]
2355 ["Week View" org-agenda-week-view
2356 :active (org-agenda-check-type nil 'agenda)
2357 :style radio :selected (eq org-agenda-current-span 'week)
2358 :keys "v w"]
2359 ["Fortnight View" org-agenda-fortnight-view
2360 :active (org-agenda-check-type nil 'agenda)
2361 :style radio :selected (eq org-agenda-current-span 'fortnight)
2362 :keys "v f"]
2363 ["Month View" org-agenda-month-view
2364 :active (org-agenda-check-type nil 'agenda)
2365 :style radio :selected (eq org-agenda-current-span 'month)
2366 :keys "v m"]
2367 ["Year View" org-agenda-year-view
2368 :active (org-agenda-check-type nil 'agenda)
2369 :style radio :selected (eq org-agenda-current-span 'year)
2370 :keys "v y"]
2371 "--"
2372 ["Include Diary" org-agenda-toggle-diary
2373 :style toggle :selected org-agenda-include-diary
2374 :active (org-agenda-check-type nil 'agenda)]
2375 ["Include Deadlines" org-agenda-toggle-deadlines
2376 :style toggle :selected org-agenda-include-deadlines
2377 :active (org-agenda-check-type nil 'agenda)]
2378 ["Use Time Grid" org-agenda-toggle-time-grid
2379 :style toggle :selected org-agenda-use-time-grid
2380 :active (org-agenda-check-type nil 'agenda)]
2381 "--"
2382 ["Show clock report" org-agenda-clockreport-mode
2383 :style toggle :selected org-agenda-clockreport-mode
2384 :active (org-agenda-check-type nil 'agenda)]
2385 ["Show some entry text" org-agenda-entry-text-mode
2386 :style toggle :selected org-agenda-entry-text-mode
2387 :active t]
2388 "--"
2389 ["Show Logbook entries" org-agenda-log-mode
2390 :style toggle :selected org-agenda-show-log
2391 :active (org-agenda-check-type nil 'agenda 'timeline)
2392 :keys "v l (or just l)"]
2393 ["Include archived trees" org-agenda-archives-mode
2394 :style toggle :selected org-agenda-archives-mode :active t
2395 :keys "v a"]
2396 ["Include archive files" (org-agenda-archives-mode t)
2397 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2398 :keys "v A"]
2399 "--"
2400 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2401 ["Write view to file" org-agenda-write t]
2402 ["Rebuild buffer" org-agenda-redo t]
2403 ["Save all Org buffers" org-save-all-org-buffers t]
2404 "--"
2405 ["Show original entry" org-agenda-show t]
2406 ["Go To (other window)" org-agenda-goto t]
2407 ["Go To (this window)" org-agenda-switch-to t]
2408 ["Capture with cursor date" org-agenda-capture t]
2409 ["Follow Mode" org-agenda-follow-mode
2410 :style toggle :selected org-agenda-follow-mode :active t]
2411 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2412 "--"
2413 ("TODO"
2414 ["Cycle TODO" org-agenda-todo t]
2415 ["Next TODO set" org-agenda-todo-nextset t]
2416 ["Previous TODO set" org-agenda-todo-previousset t]
2417 ["Add note" org-agenda-add-note t])
2418 ("Archive/Refile/Delete"
2419 ["Archive default" org-agenda-archive-default t]
2420 ["Archive default" org-agenda-archive-default-with-confirmation t]
2421 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2422 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2423 ["Archive subtree" org-agenda-archive t]
2424 "--"
2425 ["Refile" org-agenda-refile t]
2426 "--"
2427 ["Delete subtree" org-agenda-kill t])
2428 ("Bulk action"
2429 ["Mark entry" org-agenda-bulk-mark t]
2430 ["Mark all" org-agenda-bulk-mark-all t]
2431 ["Unmark entry" org-agenda-bulk-unmark t]
2432 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2433 ["Toggle mark" org-agenda-bulk-toggle t]
2434 ["Toggle all" org-agenda-bulk-toggle-all t]
2435 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2436 ["Act on all marked" org-agenda-bulk-action t]
2437 "--"
2438 ("Tags and Properties"
2439 ["Show all Tags" org-agenda-show-tags t]
2440 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2441 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2442 "--"
2443 ["Column View" org-columns t])
2444 ("Deadline/Schedule"
2445 ["Schedule" org-agenda-schedule t]
2446 ["Set Deadline" org-agenda-deadline t]
2447 "--"
2448 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2449 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2450 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2451 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2452 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2453 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2454 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2455 ("Clock and Effort"
2456 ["Clock in" org-agenda-clock-in t]
2457 ["Clock out" org-agenda-clock-out t]
2458 ["Clock cancel" org-agenda-clock-cancel t]
2459 ["Goto running clock" org-clock-goto t]
2460 "--"
2461 ["Set Effort" org-agenda-set-effort t]
2462 ["Change clocked effort" org-clock-modify-effort-estimate
2463 (org-clock-is-active)])
2464 ("Priority"
2465 ["Set Priority" org-agenda-priority t]
2466 ["Increase Priority" org-agenda-priority-up t]
2467 ["Decrease Priority" org-agenda-priority-down t]
2468 ["Show Priority" org-show-priority t])
2469 ("Calendar/Diary"
2470 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2471 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2472 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2473 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2474 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2475 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2476 "--"
2477 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2478 "--"
2479 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2480 "--"
2481 ("MobileOrg"
2482 ["Push Files and Views" org-mobile-push t]
2483 ["Get Captured and Flagged" org-mobile-pull t]
2484 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2485 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2486 "--"
2487 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2488 "--"
2489 ["Quit" org-agenda-quit t]
2490 ["Exit and Release Buffers" org-agenda-exit t]
2493 ;;; Agenda undo
2495 (defvar org-agenda-allow-remote-undo t
2496 "Non-nil means allow remote undo from the agenda buffer.")
2497 (defvar org-agenda-undo-has-started-in nil
2498 "Buffers that have already seen `undo-start' in the current undo sequence.")
2500 (defun org-agenda-undo ()
2501 "Undo a remote editing step in the agenda.
2502 This undoes changes both in the agenda buffer and in the remote buffer
2503 that have been changed along."
2504 (interactive)
2505 (or org-agenda-allow-remote-undo
2506 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2507 (if (not (eq this-command last-command))
2508 (setq org-agenda-undo-has-started-in nil
2509 org-agenda-pending-undo-list org-agenda-undo-list))
2510 (if (not org-agenda-pending-undo-list)
2511 (user-error "No further undo information"))
2512 (let* ((entry (pop org-agenda-pending-undo-list))
2513 buf line cmd rembuf)
2514 (setq cmd (pop entry) line (pop entry))
2515 (setq rembuf (nth 2 entry))
2516 (org-with-remote-undo rembuf
2517 (while (bufferp (setq buf (pop entry)))
2518 (if (pop entry)
2519 (with-current-buffer buf
2520 (let ((last-undo-buffer buf)
2521 (inhibit-read-only t))
2522 (unless (memq buf org-agenda-undo-has-started-in)
2523 (push buf org-agenda-undo-has-started-in)
2524 (make-local-variable 'pending-undo-list)
2525 (undo-start))
2526 (while (and pending-undo-list
2527 (listp pending-undo-list)
2528 (not (car pending-undo-list)))
2529 (pop pending-undo-list))
2530 (undo-more 1))))))
2531 (org-goto-line line)
2532 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2534 (defun org-verify-change-for-undo (l1 l2)
2535 "Verify that a real change occurred between the undo lists L1 and L2."
2536 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2537 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2538 (not (eq l1 l2)))
2540 ;;; Agenda dispatch
2542 (defvar org-agenda-restrict-begin (make-marker))
2543 (defvar org-agenda-restrict-end (make-marker))
2544 (defvar org-agenda-last-dispatch-buffer nil)
2545 (defvar org-agenda-overriding-restriction nil)
2547 (defcustom org-agenda-custom-commands-contexts nil
2548 "Alist of custom agenda keys and contextual rules.
2550 For example, if you have a custom agenda command \"p\" and you
2551 want this command to be accessible only from plain text files,
2552 use this:
2554 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2556 Here are the available contexts definitions:
2558 in-file: command displayed only in matching files
2559 in-mode: command displayed only in matching modes
2560 not-in-file: command not displayed in matching files
2561 not-in-mode: command not displayed in matching modes
2562 in-buffer: command displayed only in matching buffers
2563 not-in-buffer: command not displayed in matching buffers
2564 [function]: a custom function taking no argument
2566 If you define several checks, the agenda command will be
2567 accessible if there is at least one valid check.
2569 You can also bind a key to another agenda custom command
2570 depending on contextual rules.
2572 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2574 Here it means: in .txt files, use \"p\" as the key for the
2575 agenda command otherwise associated with \"q\". (The command
2576 originally associated with \"q\" is not displayed to avoid
2577 duplicates.)"
2578 :version "24.3"
2579 :group 'org-agenda-custom-commands
2580 :type '(repeat (list :tag "Rule"
2581 (string :tag " Agenda key")
2582 (string :tag "Replace by command")
2583 (repeat :tag "Available when"
2584 (choice
2585 (cons :tag "Condition"
2586 (choice
2587 (const :tag "In file" in-file)
2588 (const :tag "Not in file" not-in-file)
2589 (const :tag "In buffer" in-buffer)
2590 (const :tag "Not in buffer" not-in-buffer)
2591 (const :tag "In mode" in-mode)
2592 (const :tag "Not in mode" not-in-mode))
2593 (regexp))
2594 (function :tag "Custom function"))))))
2596 (defcustom org-agenda-max-entries nil
2597 "Maximum number of entries to display in an agenda.
2598 This can be nil (no limit) or an integer or an alist of agenda
2599 types with an associated number of entries to display in this
2600 type."
2601 :version "24.4"
2602 :package-version '(Org . "8.0")
2603 :group 'org-agenda-custom-commands
2604 :type '(choice (symbol :tag "No limit" nil)
2605 (integer :tag "Max number of entries")
2606 (repeat
2607 (cons (choice :tag "Agenda type"
2608 (const agenda)
2609 (const todo)
2610 (const tags)
2611 (const search)
2612 (const timeline))
2613 (integer :tag "Max number of entries")))))
2615 (defcustom org-agenda-max-todos nil
2616 "Maximum number of TODOs to display in an agenda.
2617 This can be nil (no limit) or an integer or an alist of agenda
2618 types with an associated number of entries to display in this
2619 type."
2620 :version "24.4"
2621 :package-version '(Org . "8.0")
2622 :group 'org-agenda-custom-commands
2623 :type '(choice (symbol :tag "No limit" nil)
2624 (integer :tag "Max number of TODOs")
2625 (repeat
2626 (cons (choice :tag "Agenda type"
2627 (const agenda)
2628 (const todo)
2629 (const tags)
2630 (const search)
2631 (const timeline))
2632 (integer :tag "Max number of TODOs")))))
2634 (defcustom org-agenda-max-tags nil
2635 "Maximum number of tagged entries to display in an agenda.
2636 This can be nil (no limit) or an integer or an alist of agenda
2637 types with an associated number of entries to display in this
2638 type."
2639 :version "24.4"
2640 :package-version '(Org . "8.0")
2641 :group 'org-agenda-custom-commands
2642 :type '(choice (symbol :tag "No limit" nil)
2643 (integer :tag "Max number of tagged entries")
2644 (repeat
2645 (cons (choice :tag "Agenda type"
2646 (const agenda)
2647 (const todo)
2648 (const tags)
2649 (const search)
2650 (const timeline))
2651 (integer :tag "Max number of tagged entries")))))
2653 (defcustom org-agenda-max-effort nil
2654 "Maximum cumulated effort duration for the agenda.
2655 This can be nil (no limit) or a number of minutes (as an integer)
2656 or an alist of agenda types with an associated number of minutes
2657 to limit entries to in this type."
2658 :version "24.4"
2659 :package-version '(Org . "8.0")
2660 :group 'org-agenda-custom-commands
2661 :type '(choice (symbol :tag "No limit" nil)
2662 (integer :tag "Max number of minutes")
2663 (repeat
2664 (cons (choice :tag "Agenda type"
2665 (const agenda)
2666 (const todo)
2667 (const tags)
2668 (const search)
2669 (const timeline))
2670 (integer :tag "Max number of minutes")))))
2672 (defvar org-agenda-keep-restricted-file-list nil)
2673 (defvar org-keys nil)
2674 (defvar org-match nil)
2675 ;;;###autoload
2676 (defun org-agenda (&optional arg org-keys restriction)
2677 "Dispatch agenda commands to collect entries to the agenda buffer.
2678 Prompts for a command to execute. Any prefix arg will be passed
2679 on to the selected command. The default selections are:
2681 a Call `org-agenda-list' to display the agenda for current day or week.
2682 t Call `org-todo-list' to display the global todo list.
2683 T Call `org-todo-list' to display the global todo list, select only
2684 entries with a specific TODO keyword (the user gets a prompt).
2685 m Call `org-tags-view' to display headlines with tags matching
2686 a condition (the user is prompted for the condition).
2687 M Like `m', but select only TODO entries, no ordinary headlines.
2688 L Create a timeline for the current buffer.
2689 e Export views to associated files.
2690 s Search entries for keywords.
2691 S Search entries for keywords, only with TODO keywords.
2692 / Multi occur across all agenda files and also files listed
2693 in `org-agenda-text-search-extra-files'.
2694 < Restrict agenda commands to buffer, subtree, or region.
2695 Press several times to get the desired effect.
2696 > Remove a previous restriction.
2697 # List \"stuck\" projects.
2698 ! Configure what \"stuck\" means.
2699 C Configure custom agenda commands.
2701 More commands can be added by configuring the variable
2702 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2703 searches can be pre-defined in this way.
2705 If the current buffer is in Org mode and visiting a file, you can also
2706 first press `<' once to indicate that the agenda should be temporarily
2707 \(until the next use of `\\[org-agenda]') restricted to the current file.
2708 Pressing `<' twice means to restrict to the current subtree or region
2709 \(if active)."
2710 (interactive "P")
2711 (catch 'exit
2712 (let* ((prefix-descriptions nil)
2713 (org-agenda-buffer-name org-agenda-buffer-name)
2714 (org-agenda-window-setup (if (equal (buffer-name)
2715 org-agenda-buffer-name)
2716 'current-window
2717 org-agenda-window-setup))
2718 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2719 (org-agenda-custom-commands
2720 ;; normalize different versions
2721 (delq nil
2722 (mapcar
2723 (lambda (x)
2724 (cond ((stringp (cdr x))
2725 (push x prefix-descriptions)
2726 nil)
2727 ((stringp (nth 1 x)) x)
2728 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2729 (t (cons (car x) (cons "" (cdr x))))))
2730 org-agenda-custom-commands)))
2731 (org-agenda-custom-commands
2732 (org-contextualize-keys
2733 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2734 (buf (current-buffer))
2735 (bfn (buffer-file-name (buffer-base-buffer)))
2736 entry key type org-match lprops ans)
2737 ;; Turn off restriction unless there is an overriding one,
2738 (unless org-agenda-overriding-restriction
2739 (unless org-agenda-keep-restricted-file-list
2740 ;; There is a request to keep the file list in place
2741 (put 'org-agenda-files 'org-restrict nil))
2742 (setq org-agenda-restrict nil)
2743 (move-marker org-agenda-restrict-begin nil)
2744 (move-marker org-agenda-restrict-end nil))
2745 ;; Delete old local properties
2746 (put 'org-agenda-redo-command 'org-lprops nil)
2747 ;; Delete previously set last-arguments
2748 (put 'org-agenda-redo-command 'last-args nil)
2749 ;; Remember where this call originated
2750 (setq org-agenda-last-dispatch-buffer (current-buffer))
2751 (unless org-keys
2752 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2753 org-keys (car ans)
2754 restriction (cdr ans)))
2755 ;; If we have sticky agenda buffers, set a name for the buffer,
2756 ;; depending on the invoking keys. The user may still set this
2757 ;; as a command option, which will overwrite what we do here.
2758 (if org-agenda-sticky
2759 (setq org-agenda-buffer-name
2760 (format "*Org Agenda(%s)*" org-keys)))
2761 ;; Establish the restriction, if any
2762 (when (and (not org-agenda-overriding-restriction) restriction)
2763 (put 'org-agenda-files 'org-restrict (list bfn))
2764 (cond
2765 ((eq restriction 'region)
2766 (setq org-agenda-restrict (current-buffer))
2767 (move-marker org-agenda-restrict-begin (region-beginning))
2768 (move-marker org-agenda-restrict-end (region-end)))
2769 ((eq restriction 'subtree)
2770 (save-excursion
2771 (setq org-agenda-restrict (current-buffer))
2772 (org-back-to-heading t)
2773 (move-marker org-agenda-restrict-begin (point))
2774 (move-marker org-agenda-restrict-end
2775 (progn (org-end-of-subtree t)))))))
2777 ;; For example the todo list should not need it (but does...)
2778 (cond
2779 ((setq entry (assoc org-keys org-agenda-custom-commands))
2780 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2781 (progn
2782 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2783 lprops (nth 4 entry))
2784 (if org-agenda-sticky
2785 (setq org-agenda-buffer-name
2786 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2787 (format "*Org Agenda(%s)*" org-keys))))
2788 (put 'org-agenda-redo-command 'org-lprops lprops)
2789 (cond
2790 ((eq type 'agenda)
2791 (org-let lprops '(org-agenda-list current-prefix-arg)))
2792 ((eq type 'agenda*)
2793 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2794 ((eq type 'alltodo)
2795 (org-let lprops '(org-todo-list current-prefix-arg)))
2796 ((eq type 'search)
2797 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2798 ((eq type 'stuck)
2799 (org-let lprops '(org-agenda-list-stuck-projects
2800 current-prefix-arg)))
2801 ((eq type 'tags)
2802 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2803 ((eq type 'tags-todo)
2804 (org-let lprops '(org-tags-view '(4) org-match)))
2805 ((eq type 'todo)
2806 (org-let lprops '(org-todo-list org-match)))
2807 ((eq type 'tags-tree)
2808 (org-check-for-org-mode)
2809 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2810 ((eq type 'todo-tree)
2811 (org-check-for-org-mode)
2812 (org-let lprops
2813 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2814 (regexp-quote org-match) "\\>"))))
2815 ((eq type 'occur-tree)
2816 (org-check-for-org-mode)
2817 (org-let lprops '(org-occur org-match)))
2818 ((functionp type)
2819 (org-let lprops '(funcall type org-match)))
2820 ((fboundp type)
2821 (org-let lprops '(funcall type org-match)))
2822 (t (user-error "Invalid custom agenda command type %s" type))))
2823 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2824 ((equal org-keys "C")
2825 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2826 (customize-variable 'org-agenda-custom-commands))
2827 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2828 ((equal org-keys "s") (call-interactively 'org-search-view))
2829 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2830 ((equal org-keys "t") (call-interactively 'org-todo-list))
2831 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2832 ((equal org-keys "m") (call-interactively 'org-tags-view))
2833 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2834 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2835 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2836 (add-hook
2837 'post-command-hook
2838 (lambda ()
2839 (unless (current-message)
2840 (let* ((m (org-agenda-get-any-marker))
2841 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2842 (when note
2843 (message (concat
2844 "FLAGGING-NOTE ([?] for more info): "
2845 (org-add-props
2846 (replace-regexp-in-string
2847 "\\\\n" "//"
2848 (copy-sequence note))
2849 nil 'face 'org-warning)))))))
2850 t t))
2851 ((equal org-keys "L")
2852 (unless (derived-mode-p 'org-mode)
2853 (user-error "This is not an Org file"))
2854 (unless restriction
2855 (put 'org-agenda-files 'org-restrict (list bfn))
2856 (org-call-with-arg 'org-timeline arg)))
2857 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2858 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2859 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2860 (t (user-error "Invalid agenda key"))))))
2862 (defvar org-agenda-multi)
2864 (defun org-agenda-append-agenda ()
2865 "Append another agenda view to the current one.
2866 This function allows interactive building of block agendas.
2867 Agenda views are separated by `org-agenda-block-separator'."
2868 (interactive)
2869 (unless (derived-mode-p 'org-agenda-mode)
2870 (user-error "Can only append from within agenda buffer"))
2871 (let ((org-agenda-multi t))
2872 (org-agenda)
2873 (widen)
2874 (org-agenda-finalize)
2875 (setq buffer-read-only t)
2876 (org-agenda-fit-window-to-buffer)))
2878 (defun org-agenda-normalize-custom-commands (cmds)
2879 "Normalize custom commands CMDS."
2880 (delq nil
2881 (mapcar
2882 (lambda (x)
2883 (cond ((stringp (cdr x)) nil)
2884 ((stringp (nth 1 x)) x)
2885 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2886 (t (cons (car x) (cons "" (cdr x))))))
2887 cmds)))
2889 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2890 "The user interface for selecting an agenda command."
2891 (catch 'exit
2892 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2893 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2894 (region-p (org-region-active-p))
2895 (custom org-agenda-custom-commands)
2896 (selstring "")
2897 restriction second-time
2898 c entry key type match prefixes rmheader header-end custom1 desc
2899 line lines left right n n1)
2900 (save-window-excursion
2901 (delete-other-windows)
2902 (org-switch-to-buffer-other-window " *Agenda Commands*")
2903 (erase-buffer)
2904 (insert (eval-when-compile
2905 (let ((header
2906 "Press key for an agenda command: < Buffer, subtree/region restriction
2907 -------------------------------- > Remove restriction
2908 a Agenda for current week or day e Export agenda views
2909 t List of all TODO entries T Entries with special TODO kwd
2910 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2911 s Search for keywords S Like s, but only TODO entries
2912 L Timeline for current buffer # List stuck projects (!=configure)
2913 / Multi-occur C Configure custom agenda commands
2914 ? Find :FLAGGED: entries * Toggle sticky agenda views
2916 (start 0))
2917 (while (string-match
2918 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2919 header start)
2920 (setq start (match-end 0))
2921 (add-text-properties (match-beginning 2) (match-end 2)
2922 '(face bold) header))
2923 header)))
2924 (setq header-end (point-marker))
2925 (while t
2926 (setq custom1 custom)
2927 (when (eq rmheader t)
2928 (org-goto-line 1)
2929 (re-search-forward ":" nil t)
2930 (delete-region (match-end 0) (point-at-eol))
2931 (forward-char 1)
2932 (looking-at "-+")
2933 (delete-region (match-end 0) (point-at-eol))
2934 (move-marker header-end (match-end 0)))
2935 (goto-char header-end)
2936 (delete-region (point) (point-max))
2938 ;; Produce all the lines that describe custom commands and prefixes
2939 (setq lines nil)
2940 (while (setq entry (pop custom1))
2941 (setq key (car entry) desc (nth 1 entry)
2942 type (nth 2 entry)
2943 match (nth 3 entry))
2944 (if (> (length key) 1)
2945 (add-to-list 'prefixes (string-to-char key))
2946 (setq line
2947 (format
2948 "%-4s%-14s"
2949 (org-add-props (copy-sequence key)
2950 '(face bold))
2951 (cond
2952 ((string-match "\\S-" desc) desc)
2953 ((eq type 'agenda) "Agenda for current week or day")
2954 ((eq type 'agenda*) "Appointments for current week or day")
2955 ((eq type 'alltodo) "List of all TODO entries")
2956 ((eq type 'search) "Word search")
2957 ((eq type 'stuck) "List of stuck projects")
2958 ((eq type 'todo) "TODO keyword")
2959 ((eq type 'tags) "Tags query")
2960 ((eq type 'tags-todo) "Tags (TODO)")
2961 ((eq type 'tags-tree) "Tags tree")
2962 ((eq type 'todo-tree) "TODO kwd tree")
2963 ((eq type 'occur-tree) "Occur tree")
2964 ((functionp type) (if (symbolp type)
2965 (symbol-name type)
2966 "Lambda expression"))
2967 (t "???"))))
2968 (if org-agenda-menu-show-matcher
2969 (setq line
2970 (concat line ": "
2971 (cond
2972 ((stringp match)
2973 (setq match (copy-sequence match))
2974 (org-add-props match nil 'face 'org-warning))
2975 ((listp type)
2976 (format "set of %d commands" (length type))))))
2977 (if (org-string-nw-p match)
2978 (add-text-properties
2979 0 (length line) (list 'help-echo
2980 (concat "Matcher: " match)) line)))
2981 (push line lines)))
2982 (setq lines (nreverse lines))
2983 (when prefixes
2984 (mapc (lambda (x)
2985 (push
2986 (format "%s %s"
2987 (org-add-props (char-to-string x)
2988 nil 'face 'bold)
2989 (or (cdr (assoc (concat selstring
2990 (char-to-string x))
2991 prefix-descriptions))
2992 "Prefix key"))
2993 lines))
2994 prefixes))
2996 ;; Check if we should display in two columns
2997 (if org-agenda-menu-two-columns
2998 (progn
2999 (setq n (length lines)
3000 n1 (+ (/ n 2) (mod n 2))
3001 right (nthcdr n1 lines)
3002 left (copy-sequence lines))
3003 (setcdr (nthcdr (1- n1) left) nil))
3004 (setq left lines right nil))
3005 (while left
3006 (insert "\n" (pop left))
3007 (when right
3008 (if (< (current-column) 40)
3009 (move-to-column 40 t)
3010 (insert " "))
3011 (insert (pop right))))
3013 ;; Make the window the right size
3014 (goto-char (point-min))
3015 (if second-time
3016 (if (not (pos-visible-in-window-p (point-max)))
3017 (org-fit-window-to-buffer))
3018 (setq second-time t)
3019 (org-fit-window-to-buffer))
3021 ;; Ask for selection
3022 (message "Press key for agenda command%s:"
3023 (if (or restrict-ok org-agenda-overriding-restriction)
3024 (if org-agenda-overriding-restriction
3025 " (restriction lock active)"
3026 (if restriction
3027 (format " (restricted to %s)" restriction)
3028 " (unrestricted)"))
3029 ""))
3030 (setq c (read-char-exclusive))
3031 (message "")
3032 (cond
3033 ((assoc (char-to-string c) custom)
3034 (setq selstring (concat selstring (char-to-string c)))
3035 (throw 'exit (cons selstring restriction)))
3036 ((memq c prefixes)
3037 (setq selstring (concat selstring (char-to-string c))
3038 prefixes nil
3039 rmheader (or rmheader t)
3040 custom (delq nil (mapcar
3041 (lambda (x)
3042 (if (or (= (length (car x)) 1)
3043 (/= (string-to-char (car x)) c))
3045 (cons (substring (car x) 1) (cdr x))))
3046 custom))))
3047 ((eq c ?*)
3048 (call-interactively 'org-toggle-sticky-agenda)
3049 (sit-for 2))
3050 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3051 (message "Restriction is only possible in Org buffers")
3052 (ding) (sit-for 1))
3053 ((eq c ?1)
3054 (org-agenda-remove-restriction-lock 'noupdate)
3055 (setq restriction 'buffer))
3056 ((eq c ?0)
3057 (org-agenda-remove-restriction-lock 'noupdate)
3058 (setq restriction (if region-p 'region 'subtree)))
3059 ((eq c ?<)
3060 (org-agenda-remove-restriction-lock 'noupdate)
3061 (setq restriction
3062 (cond
3063 ((eq restriction 'buffer)
3064 (if region-p 'region 'subtree))
3065 ((memq restriction '(subtree region))
3066 nil)
3067 (t 'buffer))))
3068 ((eq c ?>)
3069 (org-agenda-remove-restriction-lock 'noupdate)
3070 (setq restriction nil))
3071 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3072 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3073 ((and (> (length selstring) 0) (eq c ?\d))
3074 (delete-window)
3075 (org-agenda-get-restriction-and-command prefix-descriptions))
3077 ((equal c ?q) (error "Abort"))
3078 (t (user-error "Invalid key %c" c))))))))
3080 (defun org-agenda-fit-window-to-buffer ()
3081 "Fit the window to the buffer size."
3082 (and (memq org-agenda-window-setup '(reorganize-frame))
3083 (fboundp 'fit-window-to-buffer)
3084 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3085 (= (car org-agenda-window-frame-fractions) 1.0))
3086 (delete-other-windows)
3087 (org-fit-window-to-buffer
3089 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3090 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3092 (defvar org-cmd nil)
3093 (defvar org-agenda-overriding-cmd nil)
3094 (defvar org-agenda-overriding-arguments nil)
3095 (defvar org-agenda-overriding-cmd-arguments nil)
3096 (defun org-agenda-run-series (name series)
3097 "Run agenda NAME as a SERIES of agenda commands."
3098 (org-let (nth 1 series) '(org-agenda-prepare name))
3099 ;; We need to reset agenda markers here, because when constructing a
3100 ;; block agenda, the individual blocks do not do that.
3101 (org-agenda-reset-markers)
3102 (let* ((org-agenda-multi t)
3103 (redo (list 'org-agenda-run-series name (list 'quote series)))
3104 (cmds (car series))
3105 (gprops (nth 1 series))
3106 match ;; The byte compiler incorrectly complains about this. Keep it!
3107 org-cmd type lprops)
3108 (while (setq org-cmd (pop cmds))
3109 (setq type (car org-cmd))
3110 (setq match (eval (nth 1 org-cmd)))
3111 (setq lprops (nth 2 org-cmd))
3112 (let ((org-agenda-overriding-arguments
3113 (if (eq org-agenda-overriding-cmd org-cmd)
3114 (or org-agenda-overriding-arguments
3115 org-agenda-overriding-cmd-arguments))))
3116 (cond
3117 ((eq type 'agenda)
3118 (org-let2 gprops lprops
3119 '(call-interactively 'org-agenda-list)))
3120 ((eq type 'agenda*)
3121 (org-let2 gprops lprops
3122 '(funcall 'org-agenda-list nil nil t)))
3123 ((eq type 'alltodo)
3124 (org-let2 gprops lprops
3125 '(call-interactively 'org-todo-list)))
3126 ((eq type 'search)
3127 (org-let2 gprops lprops
3128 '(org-search-view current-prefix-arg match nil)))
3129 ((eq type 'stuck)
3130 (org-let2 gprops lprops
3131 '(call-interactively 'org-agenda-list-stuck-projects)))
3132 ((eq type 'tags)
3133 (org-let2 gprops lprops
3134 '(org-tags-view current-prefix-arg match)))
3135 ((eq type 'tags-todo)
3136 (org-let2 gprops lprops
3137 '(org-tags-view '(4) match)))
3138 ((eq type 'todo)
3139 (org-let2 gprops lprops
3140 '(org-todo-list match)))
3141 ((fboundp type)
3142 (org-let2 gprops lprops
3143 '(funcall type match)))
3144 (t (error "Invalid type in command series")))))
3145 (widen)
3146 (let ((inhibit-read-only t))
3147 (add-text-properties (point-min) (point-max)
3148 `(org-series t org-series-redo-cmd ,redo)))
3149 (setq org-agenda-redo-command redo)
3150 (goto-char (point-min)))
3151 (org-agenda-fit-window-to-buffer)
3152 (org-let (nth 1 series) '(org-agenda-finalize)))
3154 ;;;###autoload
3155 (defmacro org-batch-agenda (cmd-key &rest parameters)
3156 "Run an agenda command in batch mode and send the result to STDOUT.
3157 If CMD-KEY is a string of length 1, it is used as a key in
3158 `org-agenda-custom-commands' and triggers this command. If it is a
3159 longer string it is used as a tags/todo match string.
3160 Parameters are alternating variable names and values that will be bound
3161 before running the agenda command."
3162 (org-eval-in-environment (org-make-parameter-alist parameters)
3163 (let (org-agenda-sticky)
3164 (if (> (length cmd-key) 1)
3165 (org-tags-view nil cmd-key)
3166 (org-agenda nil cmd-key))))
3167 (set-buffer org-agenda-buffer-name)
3168 (princ (buffer-string)))
3170 (defvar org-agenda-info nil)
3172 ;;;###autoload
3173 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3174 "Run an agenda command in batch mode and send the result to STDOUT.
3175 If CMD-KEY is a string of length 1, it is used as a key in
3176 `org-agenda-custom-commands' and triggers this command. If it is a
3177 longer string it is used as a tags/todo match string.
3178 Parameters are alternating variable names and values that will be bound
3179 before running the agenda command.
3181 The output gives a line for each selected agenda item. Each
3182 item is a list of comma-separated values, like this:
3184 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3186 category The category of the item
3187 head The headline, without TODO kwd, TAGS and PRIORITY
3188 type The type of the agenda entry, can be
3189 todo selected in TODO match
3190 tagsmatch selected in tags match
3191 diary imported from diary
3192 deadline a deadline on given date
3193 scheduled scheduled on given date
3194 timestamp entry has timestamp on given date
3195 closed entry was closed on given date
3196 upcoming-deadline warning about deadline
3197 past-scheduled forwarded scheduled item
3198 block entry has date block including g. date
3199 todo The todo keyword, if any
3200 tags All tags including inherited ones, separated by colons
3201 date The relevant date, like 2007-2-14
3202 time The time, like 15:00-16:50
3203 extra Sting with extra planning info
3204 priority-l The priority letter if any was given
3205 priority-n The computed numerical priority
3206 agenda-day The day in the agenda where this is listed"
3207 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3208 (org-make-parameter-alist parameters))
3209 (if (> (length cmd-key) 2)
3210 (org-tags-view nil cmd-key)
3211 (org-agenda nil cmd-key)))
3212 (set-buffer org-agenda-buffer-name)
3213 (let* ((lines (org-split-string (buffer-string) "\n"))
3214 line)
3215 (while (setq line (pop lines))
3216 (catch 'next
3217 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3218 (setq org-agenda-info
3219 (org-fix-agenda-info (text-properties-at 0 line)))
3220 (princ
3221 (mapconcat 'org-agenda-export-csv-mapper
3222 '(org-category txt type todo tags date time extra
3223 priority-letter priority agenda-day)
3224 ","))
3225 (princ "\n")))))
3227 (defun org-fix-agenda-info (props)
3228 "Make sure all properties on an agenda item have a canonical form.
3229 This ensures the export commands can easily use it."
3230 (let (tmp re)
3231 (when (setq tmp (plist-get props 'tags))
3232 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3233 (when (setq tmp (plist-get props 'date))
3234 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3235 (let ((calendar-date-display-form '(year "-" month "-" day)))
3236 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3238 (setq tmp (calendar-date-string tmp)))
3239 (setq props (plist-put props 'date tmp)))
3240 (when (setq tmp (plist-get props 'day))
3241 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3242 (let ((calendar-date-display-form '(year "-" month "-" day)))
3243 (setq tmp (calendar-date-string tmp)))
3244 (setq props (plist-put props 'day tmp))
3245 (setq props (plist-put props 'agenda-day tmp)))
3246 (when (setq tmp (plist-get props 'txt))
3247 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3248 (plist-put props 'priority-letter (match-string 1 tmp))
3249 (setq tmp (replace-match "" t t tmp)))
3250 (when (and (setq re (plist-get props 'org-todo-regexp))
3251 (setq re (concat "\\`\\.*" re " ?"))
3252 (let ((case-fold-search nil)) (string-match re tmp)))
3253 (plist-put props 'todo (match-string 1 tmp))
3254 (setq tmp (replace-match "" t t tmp)))
3255 (plist-put props 'txt tmp)))
3256 props)
3258 (defun org-agenda-export-csv-mapper (prop)
3259 (let ((res (plist-get org-agenda-info prop)))
3260 (setq res
3261 (cond
3262 ((not res) "")
3263 ((stringp res) res)
3264 (t (prin1-to-string res))))
3265 (org-trim (replace-regexp-in-string "," ";" res nil t))))
3267 ;;;###autoload
3268 (defun org-store-agenda-views (&rest parameters)
3269 "Store agenda views."
3270 (interactive)
3271 (eval (list 'org-batch-store-agenda-views)))
3273 ;;;###autoload
3274 (defmacro org-batch-store-agenda-views (&rest parameters)
3275 "Run all custom agenda commands that have a file argument."
3276 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3277 (pop-up-frames nil)
3278 (dir default-directory)
3279 (pars (org-make-parameter-alist parameters))
3280 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3281 (save-window-excursion
3282 (while cmds
3283 (setq cmd (pop cmds)
3284 thiscmdkey (car cmd)
3285 thiscmdcmd (cdr cmd)
3286 match (nth 2 thiscmdcmd)
3287 bufname (if org-agenda-sticky
3288 (or (and (stringp match)
3289 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3290 (format "*Org Agenda(%s)*" thiscmdkey))
3291 org-agenda-buffer-name)
3292 cmd-or-set (nth 2 cmd)
3293 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3294 files (nth (if (listp cmd-or-set) 4 5) cmd))
3295 (if (stringp files) (setq files (list files)))
3296 (when files
3297 (org-eval-in-environment (append org-agenda-exporter-settings
3298 opts pars)
3299 (org-agenda nil thiscmdkey))
3300 (set-buffer bufname)
3301 (while files
3302 (org-eval-in-environment (append org-agenda-exporter-settings
3303 opts pars)
3304 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3305 (and (get-buffer bufname)
3306 (kill-buffer bufname)))))))
3308 (defvar org-agenda-current-span nil
3309 "The current span used in the agenda view.") ; local variable in the agenda buffer
3310 (defun org-agenda-mark-header-line (pos)
3311 "Mark the line at POS as an agenda structure header."
3312 (save-excursion
3313 (goto-char pos)
3314 (put-text-property (point-at-bol) (point-at-eol)
3315 'org-agenda-structural-header t)
3316 (when org-agenda-title-append
3317 (put-text-property (point-at-bol) (point-at-eol)
3318 'org-agenda-title-append org-agenda-title-append))))
3320 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3321 (defvar org-agenda-write-buffer-name "Agenda View")
3322 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3323 "Write the current buffer (an agenda view) as a file.
3325 Depending on the extension of the file name, plain text (.txt),
3326 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3327 If the extension is .ics, translate visible agenda into iCalendar
3328 format. If the extension is .org, collect all subtrees
3329 corresponding to the agenda entries and add them in an .org file.
3331 With prefix argument OPEN, open the new file immediately. If
3332 NOSETTINGS is given, do not scope the settings of
3333 `org-agenda-exporter-settings' into the export commands. This is
3334 used when the settings have already been scoped and we do not
3335 wish to overrule other, higher priority settings. If
3336 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3337 the agenda to write."
3338 (interactive "FWrite agenda to file: \nP")
3339 (if (or (not (file-writable-p file))
3340 (and (file-exists-p file)
3341 (if (called-interactively-p 'any)
3342 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3343 (user-error "Cannot write agenda to file %s" file))
3344 (org-let (if nosettings nil org-agenda-exporter-settings)
3345 '(save-excursion
3346 (save-window-excursion
3347 (let ((bs (copy-sequence (buffer-string)))
3348 (extension (file-name-extension file))
3349 beg content)
3350 (with-temp-buffer
3351 (rename-buffer org-agenda-write-buffer-name t)
3352 (set-buffer-modified-p nil)
3353 (insert bs)
3354 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3355 (run-hooks 'org-agenda-before-write-hook)
3356 (cond
3357 ((bound-and-true-p org-mobile-creating-agendas)
3358 (org-mobile-write-agenda-for-mobile file))
3359 ((string= "org" extension)
3360 (let (content p m message-log-max)
3361 (goto-char (point-min))
3362 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3363 (goto-char p)
3364 (setq m (get-text-property (point) 'org-hd-marker))
3365 (when m
3366 (push (save-excursion
3367 (set-buffer (marker-buffer m))
3368 (goto-char m)
3369 (org-copy-subtree 1 nil t t)
3370 org-subtree-clip)
3371 content)))
3372 (find-file file)
3373 (erase-buffer)
3374 (dolist (s content) (org-paste-subtree 1 s))
3375 (write-file file)
3376 (kill-buffer (current-buffer))
3377 (message "Org file written to %s" file)))
3378 ((member extension '("html" "htm"))
3379 (require 'htmlize)
3380 (set-buffer (htmlize-buffer (current-buffer)))
3381 (when org-agenda-export-html-style
3382 ;; replace <style> section with org-agenda-export-html-style
3383 (goto-char (point-min))
3384 (kill-region (- (search-forward "<style") 6)
3385 (search-forward "</style>"))
3386 (insert org-agenda-export-html-style))
3387 (write-file file)
3388 (kill-buffer (current-buffer))
3389 (message "HTML written to %s" file))
3390 ((string= "ps" extension)
3391 (require 'ps-print)
3392 (ps-print-buffer-with-faces file)
3393 (message "Postscript written to %s" file))
3394 ((string= "pdf" extension)
3395 (require 'ps-print)
3396 (ps-print-buffer-with-faces
3397 (concat (file-name-sans-extension file) ".ps"))
3398 (call-process "ps2pdf" nil nil nil
3399 (expand-file-name
3400 (concat (file-name-sans-extension file) ".ps"))
3401 (expand-file-name file))
3402 (delete-file (concat (file-name-sans-extension file) ".ps"))
3403 (message "PDF written to %s" file))
3404 ((string= "ics" extension)
3405 (require 'ox-icalendar)
3406 (org-icalendar-export-current-agenda (expand-file-name file)))
3408 (let ((bs (buffer-string)))
3409 (find-file file)
3410 (erase-buffer)
3411 (insert bs)
3412 (save-buffer 0)
3413 (kill-buffer (current-buffer))
3414 (message "Plain text written to %s" file))))))))
3415 (set-buffer (or agenda-bufname
3416 (and (called-interactively-p 'any) (buffer-name))
3417 org-agenda-buffer-name)))
3418 (when open (org-open-file file)))
3420 (defun org-agenda-remove-marked-text (property &optional value)
3421 "Delete all text marked with VALUE of PROPERTY.
3422 VALUE defaults to t."
3423 (let (beg)
3424 (setq value (or value t))
3425 (while (setq beg (text-property-any (point-min) (point-max)
3426 property value))
3427 (delete-region
3428 beg (or (next-single-property-change beg property)
3429 (point-max))))))
3431 (defun org-agenda-add-entry-text ()
3432 "Add entry text to agenda lines.
3433 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3434 entry text following headings shown in the agenda.
3435 Drawers will be excluded, also the line with scheduling/deadline info."
3436 (when (and (> org-agenda-add-entry-text-maxlines 0)
3437 (not (bound-and-true-p org-mobile-creating-agendas)))
3438 (let (m txt)
3439 (goto-char (point-min))
3440 (while (not (eobp))
3441 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3442 (beginning-of-line 2)
3443 (setq txt (org-agenda-get-some-entry-text
3444 m org-agenda-add-entry-text-maxlines " > "))
3445 (end-of-line 1)
3446 (if (string-match "\\S-" txt)
3447 (insert "\n" txt)
3448 (or (eobp) (forward-char 1))))))))
3450 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3451 &rest keep)
3452 "Extract entry text from MARKER, at most N-LINES lines.
3453 This will ignore drawers etc, just get the text.
3454 If INDENT is given, prefix every line with this string. If KEEP is
3455 given, it is a list of symbols, defining stuff that should not be
3456 removed from the entry content. Currently only `planning' is allowed here."
3457 (let (txt drawer-re kwd-time-re ind)
3458 (save-excursion
3459 (with-current-buffer (marker-buffer marker)
3460 (if (not (derived-mode-p 'org-mode))
3461 (setq txt "")
3462 (org-with-wide-buffer
3463 (goto-char marker)
3464 (end-of-line 1)
3465 (setq txt (buffer-substring
3466 (min (1+ (point)) (point-max))
3467 (progn (outline-next-heading) (point)))
3468 drawer-re org-drawer-regexp
3469 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3470 ".*\n?"))
3471 (with-temp-buffer
3472 (insert txt)
3473 (when org-agenda-add-entry-text-descriptive-links
3474 (goto-char (point-min))
3475 (while (org-activate-bracket-links (point-max))
3476 (add-text-properties (match-beginning 0) (match-end 0)
3477 '(face org-link))))
3478 (goto-char (point-min))
3479 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3480 (set-text-properties (match-beginning 0) (match-end 0)
3481 nil))
3482 (goto-char (point-min))
3483 (while (re-search-forward drawer-re nil t)
3484 (delete-region
3485 (match-beginning 0)
3486 (progn (re-search-forward
3487 "^[ \t]*:END:.*\n?" nil 'move)
3488 (point))))
3489 (unless (member 'planning keep)
3490 (goto-char (point-min))
3491 (while (re-search-forward kwd-time-re nil t)
3492 (replace-match "")))
3493 (goto-char (point-min))
3494 (when org-agenda-entry-text-exclude-regexps
3495 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3496 (while (setq re (pop re-list))
3497 (goto-char (point-min))
3498 (while (re-search-forward re nil t)
3499 (replace-match "")))))
3500 (goto-char (point-max))
3501 (skip-chars-backward " \t\n")
3502 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3504 ;; find and remove min common indentation
3505 (goto-char (point-min))
3506 (untabify (point-min) (point-max))
3507 (setq ind (org-get-indentation))
3508 (while (not (eobp))
3509 (unless (looking-at "[ \t]*$")
3510 (setq ind (min ind (org-get-indentation))))
3511 (beginning-of-line 2))
3512 (goto-char (point-min))
3513 (while (not (eobp))
3514 (unless (looking-at "[ \t]*$")
3515 (move-to-column ind)
3516 (delete-region (point-at-bol) (point)))
3517 (beginning-of-line 2))
3519 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3521 (goto-char (point-min))
3522 (when indent
3523 (while (and (not (eobp)) (re-search-forward "^" nil t))
3524 (replace-match indent t t)))
3525 (goto-char (point-min))
3526 (while (looking-at "[ \t]*\n") (replace-match ""))
3527 (goto-char (point-max))
3528 (when (> (org-current-line)
3529 n-lines)
3530 (org-goto-line (1+ n-lines))
3531 (backward-char 1))
3532 (setq txt (buffer-substring (point-min) (point))))))))
3533 txt))
3535 (defun org-check-for-org-mode ()
3536 "Make sure current buffer is in org-mode. Error if not."
3537 (or (derived-mode-p 'org-mode)
3538 (error "Cannot execute Org agenda command on buffer in %s"
3539 major-mode)))
3541 ;;; Agenda prepare and finalize
3543 (defvar org-agenda-multi nil) ; dynamically scoped
3544 (defvar org-agenda-pre-window-conf nil)
3545 (defvar org-agenda-columns-active nil)
3546 (defvar org-agenda-name nil)
3547 (defvar org-agenda-tag-filter nil)
3548 (defvar org-agenda-category-filter nil)
3549 (defvar org-agenda-regexp-filter nil)
3550 (defvar org-agenda-effort-filter nil)
3551 (defvar org-agenda-top-headline-filter nil)
3552 (defvar org-agenda-tag-filter-preset nil
3553 "A preset of the tags filter used for secondary agenda filtering.
3554 This must be a list of strings, each string must be a single tag preceded
3555 by \"+\" or \"-\".
3556 This variable should not be set directly, but agenda custom commands can
3557 bind it in the options section. The preset filter is a global property of
3558 the entire agenda view. In a block agenda, it will not work reliably to
3559 define a filter for one of the individual blocks. You need to set it in
3560 the global options and expect it to be applied to the entire view.")
3562 (defvar org-agenda-category-filter-preset nil
3563 "A preset of the category filter used for secondary agenda filtering.
3564 This must be a list of strings, each string must be a single category
3565 preceded by \"+\" or \"-\".
3566 This variable should not be set directly, but agenda custom commands can
3567 bind it in the options section. The preset filter is a global property of
3568 the entire agenda view. In a block agenda, it will not work reliably to
3569 define a filter for one of the individual blocks. You need to set it in
3570 the global options and expect it to be applied to the entire view.")
3572 (defvar org-agenda-regexp-filter-preset nil
3573 "A preset of the regexp filter used for secondary agenda filtering.
3574 This must be a list of strings, each string must be a single regexp
3575 preceded by \"+\" or \"-\".
3576 This variable should not be set directly, but agenda custom commands can
3577 bind it in the options section. The preset filter is a global property of
3578 the entire agenda view. In a block agenda, it will not work reliably to
3579 define a filter for one of the individual blocks. You need to set it in
3580 the global options and expect it to be applied to the entire view.")
3582 (defvar org-agenda-effort-filter-preset nil
3583 "A preset of the effort condition used for secondary agenda filtering.
3584 This must be a list of strings, each string must be a single regexp
3585 preceded by \"+\" or \"-\".
3586 This variable should not be set directly, but agenda custom commands can
3587 bind it in the options section. The preset filter is a global property of
3588 the entire agenda view. In a block agenda, it will not work reliably to
3589 define a filter for one of the individual blocks. You need to set it in
3590 the global options and expect it to be applied to the entire view.")
3592 (defun org-agenda-use-sticky-p ()
3593 "Return non-nil if an agenda buffer named
3594 `org-agenda-buffer-name' exists and should be shown instead of
3595 generating a new one."
3596 (and
3597 ;; turned off by user
3598 org-agenda-sticky
3599 ;; For multi-agenda buffer already exists
3600 (not org-agenda-multi)
3601 ;; buffer found
3602 (get-buffer org-agenda-buffer-name)
3603 ;; C-u parameter is same as last call
3604 (with-current-buffer (get-buffer org-agenda-buffer-name)
3605 (and
3606 (equal current-prefix-arg
3607 org-agenda-last-prefix-arg)
3608 ;; In case user turned stickiness on, while having existing
3609 ;; Agenda buffer active, don't reuse that buffer, because it
3610 ;; does not have org variables local
3611 org-agenda-this-buffer-is-sticky))))
3613 (defun org-agenda-prepare-window (abuf filter-alist)
3614 "Setup agenda buffer in the window.
3615 ABUF is the buffer for the agenda window.
3616 FILTER-ALIST is an alist of filters we need to apply when
3617 `org-agenda-persistent-filter' is non-nil."
3618 (let* ((awin (get-buffer-window abuf)) wconf)
3619 (cond
3620 ((equal (current-buffer) abuf) nil)
3621 (awin (select-window awin))
3622 ((not (setq wconf (current-window-configuration))))
3623 ((eq org-agenda-window-setup 'current-window)
3624 (pop-to-buffer-same-window abuf))
3625 ((eq org-agenda-window-setup 'other-window)
3626 (org-switch-to-buffer-other-window abuf))
3627 ((eq org-agenda-window-setup 'other-frame)
3628 (switch-to-buffer-other-frame abuf))
3629 ((eq org-agenda-window-setup 'only-window)
3630 (delete-other-windows)
3631 (pop-to-buffer-same-window abuf))
3632 ((eq org-agenda-window-setup 'reorganize-frame)
3633 (delete-other-windows)
3634 (org-switch-to-buffer-other-window abuf)))
3635 (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
3636 (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
3637 (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
3638 (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
3639 ;; Additional test in case agenda is invoked from within agenda
3640 ;; buffer via elisp link.
3641 (unless (equal (current-buffer) abuf)
3642 (pop-to-buffer-same-window abuf))
3643 (setq org-agenda-pre-window-conf
3644 (or wconf org-agenda-pre-window-conf))))
3646 (defun org-agenda-prepare (&optional name)
3647 (let ((filter-alist (if org-agenda-persistent-filter
3648 (with-current-buffer
3649 (get-buffer-create org-agenda-buffer-name)
3650 (list `(tag . ,org-agenda-tag-filter)
3651 `(re . ,org-agenda-regexp-filter)
3652 `(effort . ,org-agenda-effort-filter)
3653 `(cat . ,org-agenda-category-filter))))))
3654 (if (org-agenda-use-sticky-p)
3655 (progn
3656 (put 'org-agenda-tag-filter :preset-filter nil)
3657 (put 'org-agenda-category-filter :preset-filter nil)
3658 (put 'org-agenda-regexp-filter :preset-filter nil)
3659 ;; Popup existing buffer
3660 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3661 filter-alist)
3662 (message "Sticky Agenda buffer, use `r' to refresh")
3663 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3664 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3665 (setq org-todo-keywords-for-agenda nil)
3666 (put 'org-agenda-tag-filter :preset-filter
3667 org-agenda-tag-filter-preset)
3668 (put 'org-agenda-category-filter :preset-filter
3669 org-agenda-category-filter-preset)
3670 (put 'org-agenda-regexp-filter :preset-filter
3671 org-agenda-regexp-filter-preset)
3672 (put 'org-agenda-effort-filter :preset-filter
3673 org-agenda-effort-filter-preset)
3674 (if org-agenda-multi
3675 (progn
3676 (setq buffer-read-only nil)
3677 (goto-char (point-max))
3678 (unless (or (bobp) org-agenda-compact-blocks
3679 (not org-agenda-block-separator))
3680 (insert "\n"
3681 (if (stringp org-agenda-block-separator)
3682 org-agenda-block-separator
3683 (make-string (window-width) org-agenda-block-separator))
3684 "\n"))
3685 (narrow-to-region (point) (point-max)))
3686 (setq org-done-keywords-for-agenda nil)
3687 ;; Setting any org variables that are in org-agenda-local-vars
3688 ;; list need to be done after the prepare call
3689 (org-agenda-prepare-window
3690 (get-buffer-create org-agenda-buffer-name) filter-alist)
3691 (setq buffer-read-only nil)
3692 (org-agenda-reset-markers)
3693 (let ((inhibit-read-only t)) (erase-buffer))
3694 (org-agenda-mode)
3695 (setq org-agenda-buffer (current-buffer))
3696 (setq org-agenda-contributing-files nil)
3697 (setq org-agenda-columns-active nil)
3698 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3699 (setq org-todo-keywords-for-agenda
3700 (org-uniquify org-todo-keywords-for-agenda))
3701 (setq org-done-keywords-for-agenda
3702 (org-uniquify org-done-keywords-for-agenda))
3703 (setq org-agenda-last-prefix-arg current-prefix-arg)
3704 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3705 (and name (not org-agenda-name)
3706 (setq-local org-agenda-name name)))
3707 (setq buffer-read-only nil))))
3709 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3710 (defun org-agenda-finalize ()
3711 "Finishing touch for the agenda buffer, called just before displaying it."
3712 (unless org-agenda-multi
3713 (save-excursion
3714 (let ((inhibit-read-only t))
3715 (goto-char (point-min))
3716 (save-excursion
3717 (while (org-activate-bracket-links (point-max))
3718 (add-text-properties (match-beginning 0) (match-end 0)
3719 '(face org-link))))
3720 (save-excursion
3721 (while (org-activate-plain-links (point-max))
3722 (add-text-properties (match-beginning 0) (match-end 0)
3723 '(face org-link))))
3724 (unless (eq org-agenda-remove-tags t)
3725 (org-agenda-align-tags))
3726 (unless org-agenda-with-colors
3727 (remove-text-properties (point-min) (point-max) '(face nil)))
3728 (if (and (boundp 'org-agenda-overriding-columns-format)
3729 org-agenda-overriding-columns-format)
3730 (setq-local org-agenda-overriding-columns-format
3731 org-agenda-overriding-columns-format))
3732 (if (and (boundp 'org-agenda-view-columns-initially)
3733 org-agenda-view-columns-initially)
3734 (org-agenda-columns))
3735 (when org-agenda-fontify-priorities
3736 (org-agenda-fontify-priorities))
3737 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3738 (org-agenda-dim-blocked-tasks))
3739 (org-agenda-mark-clocking-task)
3740 (when org-agenda-entry-text-mode
3741 (org-agenda-entry-text-hide)
3742 (org-agenda-entry-text-show))
3743 (if (and (functionp 'org-habit-insert-consistency-graphs)
3744 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3745 (org-habit-insert-consistency-graphs))
3746 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3747 (unless (or (eq org-agenda-show-inherited-tags 'always)
3748 (and (listp org-agenda-show-inherited-tags)
3749 (memq org-agenda-type org-agenda-show-inherited-tags))
3750 (and (eq org-agenda-show-inherited-tags t)
3751 (or (eq org-agenda-use-tag-inheritance t)
3752 (and (listp org-agenda-use-tag-inheritance)
3753 (not (memq org-agenda-type
3754 org-agenda-use-tag-inheritance))))))
3755 (let (mrk)
3756 (save-excursion
3757 (goto-char (point-min))
3758 (while (equal (forward-line) 0)
3759 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3760 (put-text-property (point-at-bol) (point-at-eol)
3761 'tags (org-with-point-at mrk
3762 (delete-dups
3763 (mapcar 'downcase (org-get-tags-at))))))))))
3764 (run-hooks 'org-agenda-finalize-hook)
3765 (when org-agenda-top-headline-filter
3766 (org-agenda-filter-top-headline-apply
3767 org-agenda-top-headline-filter))
3768 (when org-agenda-tag-filter
3769 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3770 (when (get 'org-agenda-tag-filter :preset-filter)
3771 (org-agenda-filter-apply
3772 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3773 (when org-agenda-category-filter
3774 (org-agenda-filter-apply org-agenda-category-filter 'category))
3775 (when (get 'org-agenda-category-filter :preset-filter)
3776 (org-agenda-filter-apply
3777 (get 'org-agenda-category-filter :preset-filter) 'category))
3778 (when org-agenda-regexp-filter
3779 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3780 (when (get 'org-agenda-regexp-filter :preset-filter)
3781 (org-agenda-filter-apply
3782 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3783 (when org-agenda-effort-filter
3784 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3785 (when (get 'org-agenda-effort-filter :preset-filter)
3786 (org-agenda-filter-apply
3787 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3788 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3790 (defun org-agenda-mark-clocking-task ()
3791 "Mark the current clock entry in the agenda if it is present."
3792 ;; We need to widen when `org-agenda-finalize' is called from
3793 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3794 (when (bound-and-true-p org-clock-current-task)
3795 (save-restriction
3796 (widen)
3797 (org-agenda-unmark-clocking-task)
3798 (when (marker-buffer org-clock-hd-marker)
3799 (save-excursion
3800 (goto-char (point-min))
3801 (let (s ov)
3802 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3803 (goto-char s)
3804 (when (equal (org-get-at-bol 'org-hd-marker)
3805 org-clock-hd-marker)
3806 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3807 (overlay-put ov 'type 'org-agenda-clocking)
3808 (overlay-put ov 'face 'org-agenda-clocking)
3809 (overlay-put ov 'help-echo
3810 "The clock is running in this item")))))))))
3812 (defun org-agenda-unmark-clocking-task ()
3813 "Unmark the current clocking task."
3814 (mapc (lambda (o)
3815 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3816 (delete-overlay o)))
3817 (overlays-in (point-min) (point-max))))
3819 (defun org-agenda-fontify-priorities ()
3820 "Make highest priority lines bold, and lowest italic."
3821 (interactive)
3822 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3823 (delete-overlay o)))
3824 (overlays-in (point-min) (point-max)))
3825 (save-excursion
3826 (let (b e p ov h l)
3827 (goto-char (point-min))
3828 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3829 (setq h (or (get-char-property (point) 'org-highest-priority)
3830 org-highest-priority)
3831 l (or (get-char-property (point) 'org-lowest-priority)
3832 org-lowest-priority)
3833 p (string-to-char (match-string 1))
3834 b (match-beginning 0)
3835 e (if (eq org-agenda-fontify-priorities 'cookies)
3836 (match-end 0)
3837 (point-at-eol))
3838 ov (make-overlay b e))
3839 (overlay-put
3840 ov 'face
3841 (let ((special-face
3842 (cond ((org-face-from-face-or-color
3843 'priority 'org-priority
3844 (cdr (assoc p org-priority-faces))))
3845 ((and (listp org-agenda-fontify-priorities)
3846 (org-face-from-face-or-color
3847 'priority 'org-priority
3848 (cdr (assoc p org-agenda-fontify-priorities)))))
3849 ((equal p l) 'italic)
3850 ((equal p h) 'bold))))
3851 (if special-face (list special-face 'org-priority) 'org-priority)))
3852 (overlay-put ov 'org-type 'org-priority)))))
3854 (defvar org-depend-tag-blocked)
3856 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3857 "Dim currently blocked TODOs in the agenda display.
3858 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3859 dimming them."
3860 (interactive "P")
3861 (when (called-interactively-p 'interactive)
3862 (message "Dim or hide blocked tasks..."))
3863 (dolist (o (overlays-in (point-min) (point-max)))
3864 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3865 (delete-overlay o)))
3866 (save-excursion
3867 (let ((inhibit-read-only t)
3868 (org-depend-tag-blocked nil)
3869 org-blocked-by-checkboxes)
3870 (goto-char (point-min))
3871 (while (let ((pos (text-property-not-all
3872 (point) (point-max) 'todo-state nil)))
3873 (when pos (goto-char pos)))
3874 (setq org-blocked-by-checkboxes nil)
3875 (let ((marker (org-get-at-bol 'org-hd-marker)))
3876 (when (and (markerp marker)
3877 (with-current-buffer (marker-buffer marker)
3878 (save-excursion (goto-char marker)
3879 (org-entry-blocked-p))))
3880 ;; Entries blocked by checkboxes cannot be made invisible.
3881 ;; See `org-agenda-dim-blocked-tasks' for details.
3882 (let* ((really-invisible
3883 (and (not org-blocked-by-checkboxes)
3884 (or invisible (eq org-agenda-dim-blocked-tasks
3885 'invisible))))
3886 (ov (make-overlay (if really-invisible (line-end-position 0)
3887 (line-beginning-position))
3888 (line-end-position))))
3889 (if really-invisible (overlay-put ov 'invisible t)
3890 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3891 (overlay-put ov 'org-type 'org-blocked-todo))))
3892 (forward-line))))
3893 (when (called-interactively-p 'interactive)
3894 (message "Dim or hide blocked tasks...done")))
3896 (defvar org-agenda-skip-function nil
3897 "Function to be called at each match during agenda construction.
3898 If this function returns nil, the current match should not be skipped.
3899 Otherwise, the function must return a position from where the search
3900 should be continued.
3901 This may also be a Lisp form, it will be evaluated.
3902 Never set this variable using `setq' or so, because then it will apply
3903 to all future agenda commands. If you do want a global skipping condition,
3904 use the option `org-agenda-skip-function-global' instead.
3905 The correct usage for `org-agenda-skip-function' is to bind it with
3906 `let' to scope it dynamically into the agenda-constructing command.
3907 A good way to set it is through options in `org-agenda-custom-commands'.")
3909 (defun org-agenda-skip ()
3910 "Throw to `:skip' in places that should be skipped.
3911 Also moves point to the end of the skipped region, so that search can
3912 continue from there."
3913 (let ((p (point-at-bol)) to)
3914 (when (or
3915 (save-excursion (goto-char p) (looking-at comment-start-skip))
3916 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3917 (get-text-property p :org-archived)
3918 (org-end-of-subtree t))
3919 (and org-agenda-skip-comment-trees
3920 (get-text-property p :org-comment)
3921 (org-end-of-subtree t))
3922 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3923 (org-agenda-skip-eval org-agenda-skip-function)))
3924 (goto-char to))
3925 (org-in-src-block-p t))
3926 (throw :skip t))))
3928 (defun org-agenda-skip-eval (form)
3929 "If FORM is a function or a list, call (or eval) it and return the result.
3930 `save-excursion' and `save-match-data' are wrapped around the call, so point
3931 and match data are returned to the previous state no matter what these
3932 functions do."
3933 (let (fp)
3934 (and form
3935 (or (setq fp (functionp form))
3936 (consp form))
3937 (save-excursion
3938 (save-match-data
3939 (if fp
3940 (funcall form)
3941 (eval form)))))))
3943 (defvar org-agenda-markers nil
3944 "List of all currently active markers created by `org-agenda'.")
3945 (defvar org-agenda-last-marker-time (float-time)
3946 "Creation time of the last agenda marker.")
3948 (defun org-agenda-new-marker (&optional pos)
3949 "Return a new agenda marker.
3950 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3951 these markers and resets them when they are no longer in use."
3952 (let ((m (copy-marker (or pos (point)) t)))
3953 (setq org-agenda-last-marker-time (float-time))
3954 (if org-agenda-buffer
3955 (with-current-buffer org-agenda-buffer
3956 (push m org-agenda-markers))
3957 (push m org-agenda-markers))
3960 (defun org-agenda-reset-markers ()
3961 "Reset markers created by `org-agenda'."
3962 (while org-agenda-markers
3963 (move-marker (pop org-agenda-markers) nil)))
3965 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3966 "Save relative positions of markers in region.
3967 This check for agenda markers in all agenda buffers currently active."
3968 (dolist (buf (buffer-list))
3969 (with-current-buffer buf
3970 (when (eq major-mode 'org-agenda-mode)
3971 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3972 org-agenda-markers)))))
3974 ;;; Entry text mode
3976 (defun org-agenda-entry-text-show-here ()
3977 "Add some text from the entry as context to the current line."
3978 (let (m txt o)
3979 (setq m (org-get-at-bol 'org-hd-marker))
3980 (unless (marker-buffer m)
3981 (error "No marker points to an entry here"))
3982 (setq txt (concat "\n" (org-no-properties
3983 (org-agenda-get-some-entry-text
3984 m org-agenda-entry-text-maxlines
3985 org-agenda-entry-text-leaders))))
3986 (when (string-match "\\S-" txt)
3987 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3988 (overlay-put o 'evaporate t)
3989 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3990 (overlay-put o 'after-string txt))))
3992 (defun org-agenda-entry-text-show ()
3993 "Add entry context for all agenda lines."
3994 (interactive)
3995 (save-excursion
3996 (goto-char (point-max))
3997 (beginning-of-line 1)
3998 (while (not (bobp))
3999 (when (org-get-at-bol 'org-hd-marker)
4000 (org-agenda-entry-text-show-here))
4001 (beginning-of-line 0))))
4003 (defun org-agenda-entry-text-hide ()
4004 "Remove any shown entry context."
4005 (delq nil
4006 (mapcar (lambda (o)
4007 (if (eq (overlay-get o 'org-overlay-type)
4008 'agenda-entry-content)
4009 (progn (delete-overlay o) t)))
4010 (overlays-in (point-min) (point-max)))))
4012 (defun org-agenda-get-day-face (date)
4013 "Return the face DATE should be displayed with."
4014 (cond ((and (functionp org-agenda-day-face-function)
4015 (funcall org-agenda-day-face-function date)))
4016 ((org-agenda-today-p date) 'org-agenda-date-today)
4017 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4018 'org-agenda-date-weekend)
4019 (t 'org-agenda-date)))
4021 ;;; Agenda timeline
4023 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4024 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4026 (defun org-timeline (&optional dotodo)
4027 "Show a time-sorted view of the entries in the current Org file.
4029 Only entries with a time stamp of today or later will be listed.
4031 With `\\[universal-argument]' prefix, all unfinished TODO items will also be \
4032 shown,
4033 under the current date.
4035 If the buffer contains an active region, only check the region
4036 for dates."
4037 (interactive "P")
4038 (let* ((dopast t)
4039 (org-agenda-show-log-scoped org-agenda-show-log)
4040 (org-agenda-show-log org-agenda-show-log-scoped)
4041 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4042 (current-buffer))))
4043 (date (calendar-current-date))
4044 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4045 (end (if (org-region-active-p) (region-end) (point-max)))
4046 (day-numbers (org-get-all-dates
4047 beg end 'no-ranges
4048 t org-agenda-show-log-scoped ; always include today
4049 org-timeline-show-empty-dates))
4050 (org-deadline-warning-days 0)
4051 (org-agenda-only-exact-dates t)
4052 (today (org-today))
4053 (past t)
4054 args
4055 s e rtn d emptyp)
4056 (setq org-agenda-redo-command
4057 (list 'let
4058 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4059 (list 'org-switch-to-buffer-other-window (current-buffer))
4060 (list 'org-timeline (list 'quote dotodo))))
4061 (put 'org-agenda-redo-command 'org-lprops nil)
4062 (if (not dopast)
4063 ;; Remove past dates from the list of dates.
4064 (setq day-numbers (delq nil (mapcar (lambda(x)
4065 (if (>= x today) x nil))
4066 day-numbers))))
4067 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4068 (org-compile-prefix-format 'timeline)
4069 (org-set-sorting-strategy 'timeline)
4070 (if org-agenda-show-log-scoped (push :closed args))
4071 (push :timestamp args)
4072 (push :deadline args)
4073 (push :scheduled args)
4074 (push :sexp args)
4075 (if dotodo (push :todo args))
4076 (insert "Timeline of file " entry "\n")
4077 (add-text-properties (point-min) (point)
4078 (list 'face 'org-agenda-structure))
4079 (org-agenda-mark-header-line (point-min))
4080 (while (setq d (pop day-numbers))
4081 (if (and (listp d) (eq (car d) :omitted))
4082 (progn
4083 (setq s (point))
4084 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4085 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4086 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4087 (if (and (>= d today)
4088 dopast
4089 past)
4090 (progn
4091 (setq past nil)
4092 (insert (make-string 79 ?-) "\n")))
4093 (setq date (calendar-gregorian-from-absolute d))
4094 (setq s (point))
4095 (setq rtn (and (not emptyp)
4096 (apply 'org-agenda-get-day-entries entry
4097 date args)))
4098 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4099 (progn
4100 (insert
4101 (if (stringp org-agenda-format-date)
4102 (format-time-string org-agenda-format-date
4103 (org-time-from-absolute date))
4104 (funcall org-agenda-format-date date))
4105 "\n")
4106 (put-text-property s (1- (point)) 'face
4107 (org-agenda-get-day-face date))
4108 (put-text-property s (1- (point)) 'org-date-line t)
4109 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4110 (if (equal d today)
4111 (put-text-property s (1- (point)) 'org-today t))
4112 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4113 (put-text-property s (1- (point)) 'day d)))))
4114 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4115 (point-min)))
4116 (add-text-properties
4117 (point-min) (point-max)
4118 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4119 (org-agenda-finalize)
4120 (setq buffer-read-only t)))
4122 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4123 "Return a list of all relevant day numbers from BEG to END buffer positions.
4124 If NO-RANGES is non-nil, include only the start and end dates of a range,
4125 not every single day in the range. If FORCE-TODAY is non-nil, make
4126 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4127 inactive time stamps (those in square brackets) are included.
4128 When EMPTY is non-nil, also include days without any entries."
4129 (let ((re (concat
4130 (if pre-re pre-re "")
4131 (if inactive org-ts-regexp-both org-ts-regexp)))
4132 dates dates1 date day day1 day2 ts1 ts2 pos)
4133 (if force-today
4134 (setq dates (list (org-today))))
4135 (save-excursion
4136 (goto-char beg)
4137 (while (re-search-forward re end t)
4138 (setq day (time-to-days (org-time-string-to-time
4139 (substring (match-string 1) 0 10)
4140 (current-buffer) (match-beginning 0))))
4141 (or (memq day dates) (push day dates)))
4142 (unless no-ranges
4143 (goto-char beg)
4144 (while (re-search-forward org-tr-regexp end t)
4145 (setq pos (match-beginning 0))
4146 (setq ts1 (substring (match-string 1) 0 10)
4147 ts2 (substring (match-string 2) 0 10)
4148 day1 (time-to-days (org-time-string-to-time
4149 ts1 (current-buffer) pos))
4150 day2 (time-to-days (org-time-string-to-time
4151 ts2 (current-buffer) pos)))
4152 (while (< (setq day1 (1+ day1)) day2)
4153 (or (memq day1 dates) (push day1 dates)))))
4154 (setq dates (sort dates '<))
4155 (when empty
4156 (while (setq day (pop dates))
4157 (setq day2 (car dates))
4158 (push day dates1)
4159 (when (and day2 empty)
4160 (if (or (eq empty t)
4161 (and (numberp empty) (<= (- day2 day) empty)))
4162 (while (< (setq day (1+ day)) day2)
4163 (push (list day) dates1))
4164 (push (cons :omitted (- day2 day)) dates1))))
4165 (setq dates (nreverse dates1)))
4166 dates)))
4168 ;;; Agenda Daily/Weekly
4170 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4171 "Start day for the agenda view.
4172 Custom commands can set this variable in the options section.
4173 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4174 input allowed when reading a date through the Org calendar.
4175 See the docstring of `org-read-date' for details.")
4176 (defvar org-starting-day nil) ; local variable in the agenda buffer
4177 (defvar org-arg-loc nil) ; local variable
4179 (defvar org-agenda-buffer-tmp-name nil)
4180 ;;;###autoload
4181 (defun org-agenda-list (&optional arg start-day span with-hour)
4182 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4183 The view will be for the current day or week, but from the overview buffer
4184 you will be able to go to other days/weeks.
4186 With a numeric prefix argument in an interactive call, the agenda will
4187 span ARG days. Lisp programs should instead specify SPAN to change
4188 the number of days. SPAN defaults to `org-agenda-span'.
4190 START-DAY defaults to TODAY, or to the most recent match for the weekday
4191 given in `org-agenda-start-on-weekday'.
4193 When WITH-HOUR is non-nil, only include scheduled and deadline
4194 items if they have an hour specification like [h]h:mm."
4195 (interactive "P")
4196 (if org-agenda-overriding-arguments
4197 (setq arg (car org-agenda-overriding-arguments)
4198 start-day (nth 1 org-agenda-overriding-arguments)
4199 span (nth 2 org-agenda-overriding-arguments)))
4200 (if (and (integerp arg) (> arg 0))
4201 (setq span arg arg nil))
4202 (catch 'exit
4203 (setq org-agenda-buffer-name
4204 (or org-agenda-buffer-tmp-name
4205 (if org-agenda-sticky
4206 (cond ((and org-keys (stringp org-match))
4207 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4208 (org-keys
4209 (format "*Org Agenda(%s)*" org-keys))
4210 (t "*Org Agenda(a)*")))
4211 org-agenda-buffer-name))
4212 (org-agenda-prepare "Day/Week")
4213 (setq start-day (or start-day org-agenda-start-day))
4214 (if (stringp start-day)
4215 ;; Convert to an absolute day number
4216 (setq start-day (time-to-days (org-read-date nil t start-day))))
4217 (org-compile-prefix-format 'agenda)
4218 (org-set-sorting-strategy 'agenda)
4219 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4220 (today (org-today))
4221 (sd (or start-day today))
4222 (ndays (org-agenda-span-to-ndays span sd))
4223 (org-agenda-start-on-weekday
4224 (if (or (eq ndays 7) (eq ndays 14))
4225 org-agenda-start-on-weekday))
4226 (thefiles (org-agenda-files nil 'ifmode))
4227 (files thefiles)
4228 (start (if (or (null org-agenda-start-on-weekday)
4229 (< ndays 7))
4231 (let* ((nt (calendar-day-of-week
4232 (calendar-gregorian-from-absolute sd)))
4233 (n1 org-agenda-start-on-weekday)
4234 (d (- nt n1)))
4235 (- sd (+ (if (< d 0) 7 0) d)))))
4236 (day-numbers (list start))
4237 (day-cnt 0)
4238 (inhibit-redisplay (not debug-on-error))
4239 (org-agenda-show-log-scoped org-agenda-show-log)
4240 s e rtn rtnall file date d start-pos end-pos todayp
4241 clocktable-start clocktable-end filter)
4242 (setq org-agenda-redo-command
4243 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4244 (dotimes (n (1- ndays))
4245 (push (1+ (car day-numbers)) day-numbers))
4246 (setq day-numbers (nreverse day-numbers))
4247 (setq clocktable-start (car day-numbers)
4248 clocktable-end (1+ (or (org-last day-numbers) 0)))
4249 (setq-local org-starting-day (car day-numbers))
4250 (setq-local org-arg-loc arg)
4251 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4252 (unless org-agenda-compact-blocks
4253 (let* ((d1 (car day-numbers))
4254 (d2 (org-last day-numbers))
4255 (w1 (org-days-to-iso-week d1))
4256 (w2 (org-days-to-iso-week d2)))
4257 (setq s (point))
4258 (if org-agenda-overriding-header
4259 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4260 nil 'face 'org-agenda-structure) "\n")
4261 (insert (org-agenda-span-name span)
4262 "-agenda"
4263 (if (< (- d2 d1) 350)
4264 (if (= w1 w2)
4265 (format " (W%02d)" w1)
4266 (format " (W%02d-W%02d)" w1 w2))
4268 ":\n")))
4269 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4270 'org-date-line t))
4271 (org-agenda-mark-header-line s))
4272 (while (setq d (pop day-numbers))
4273 (setq date (calendar-gregorian-from-absolute d)
4274 s (point))
4275 (if (or (setq todayp (= d today))
4276 (and (not start-pos) (= d sd)))
4277 (setq start-pos (point))
4278 (if (and start-pos (not end-pos))
4279 (setq end-pos (point))))
4280 (setq files thefiles
4281 rtnall nil)
4282 (while (setq file (pop files))
4283 (catch 'nextfile
4284 (org-check-agenda-file file)
4285 (let ((org-agenda-entry-types org-agenda-entry-types))
4286 ;; Starred types override non-starred equivalents
4287 (when (member :deadline* org-agenda-entry-types)
4288 (setq org-agenda-entry-types
4289 (delq :deadline org-agenda-entry-types)))
4290 (when (member :scheduled* org-agenda-entry-types)
4291 (setq org-agenda-entry-types
4292 (delq :scheduled org-agenda-entry-types)))
4293 ;; Honor with-hour
4294 (when with-hour
4295 (when (member :deadline org-agenda-entry-types)
4296 (setq org-agenda-entry-types
4297 (delq :deadline org-agenda-entry-types))
4298 (push :deadline* org-agenda-entry-types))
4299 (when (member :scheduled org-agenda-entry-types)
4300 (setq org-agenda-entry-types
4301 (delq :scheduled org-agenda-entry-types))
4302 (push :scheduled* org-agenda-entry-types)))
4303 (unless org-agenda-include-deadlines
4304 (setq org-agenda-entry-types
4305 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4306 (cond
4307 ((memq org-agenda-show-log-scoped '(only clockcheck))
4308 (setq rtn (org-agenda-get-day-entries
4309 file date :closed)))
4310 (org-agenda-show-log-scoped
4311 (setq rtn (apply 'org-agenda-get-day-entries
4312 file date
4313 (append '(:closed) org-agenda-entry-types))))
4315 (setq rtn (apply 'org-agenda-get-day-entries
4316 file date
4317 org-agenda-entry-types)))))
4318 (setq rtnall (append rtnall rtn)))) ;; all entries
4319 (if org-agenda-include-diary
4320 (let ((org-agenda-search-headline-for-time t))
4321 (require 'diary-lib)
4322 (setq rtn (org-get-entries-from-diary date))
4323 (setq rtnall (append rtnall rtn))))
4324 (if (or rtnall org-agenda-show-all-dates)
4325 (progn
4326 (setq day-cnt (1+ day-cnt))
4327 (insert
4328 (if (stringp org-agenda-format-date)
4329 (format-time-string org-agenda-format-date
4330 (org-time-from-absolute date))
4331 (funcall org-agenda-format-date date))
4332 "\n")
4333 (put-text-property s (1- (point)) 'face
4334 (org-agenda-get-day-face date))
4335 (put-text-property s (1- (point)) 'org-date-line t)
4336 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4337 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4338 (when todayp
4339 (put-text-property s (1- (point)) 'org-today t))
4340 (setq rtnall
4341 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4342 (if rtnall (insert ;; all entries
4343 (org-agenda-finalize-entries rtnall 'agenda)
4344 "\n"))
4345 (put-text-property s (1- (point)) 'day d)
4346 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4347 (when (and org-agenda-clockreport-mode clocktable-start)
4348 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4349 ;; the above line is to ensure the restricted range!
4350 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4351 tbl)
4352 (setq p (org-plist-delete p :block))
4353 (setq p (plist-put p :tstart clocktable-start))
4354 (setq p (plist-put p :tend clocktable-end))
4355 (setq p (plist-put p :scope 'agenda))
4356 (setq tbl (apply 'org-clock-get-clocktable p))
4357 (insert tbl)))
4358 (goto-char (point-min))
4359 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4360 (unless (and (pos-visible-in-window-p (point-min))
4361 (pos-visible-in-window-p (point-max)))
4362 (goto-char (1- (point-max)))
4363 (recenter -1)
4364 (if (not (pos-visible-in-window-p (or start-pos 1)))
4365 (progn
4366 (goto-char (or start-pos 1))
4367 (recenter 1))))
4368 (goto-char (or start-pos 1))
4369 (add-text-properties (point-min) (point-max)
4370 `(org-agenda-type agenda
4371 org-last-args (,arg ,start-day ,span)
4372 org-redo-cmd ,org-agenda-redo-command
4373 org-series-cmd ,org-cmd))
4374 (if (eq org-agenda-show-log-scoped 'clockcheck)
4375 (org-agenda-show-clocking-issues))
4376 (org-agenda-finalize)
4377 (setq buffer-read-only t)
4378 (message ""))))
4380 (defun org-agenda-ndays-to-span (n)
4381 "Return a span symbol for a span of N days, or N if none matches."
4382 (cond ((symbolp n) n)
4383 ((= n 1) 'day)
4384 ((= n 7) 'week)
4385 ((= n 14) 'fortnight)
4386 (t n)))
4388 (defun org-agenda-span-to-ndays (span &optional start-day)
4389 "Return ndays from SPAN, possibly starting at START-DAY.
4390 START-DAY is an absolute time value."
4391 (cond ((numberp span) span)
4392 ((eq span 'day) 1)
4393 ((eq span 'week) 7)
4394 ((eq span 'fortnight) 14)
4395 ((eq span 'month)
4396 (let ((date (calendar-gregorian-from-absolute start-day)))
4397 (calendar-last-day-of-month (car date) (cl-caddr date))))
4398 ((eq span 'year)
4399 (let ((date (calendar-gregorian-from-absolute start-day)))
4400 (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
4402 (defun org-agenda-span-name (span)
4403 "Return a SPAN name."
4404 (if (null span)
4406 (if (symbolp span)
4407 (capitalize (symbol-name span))
4408 (format "%d days" span))))
4410 ;;; Agenda word search
4412 (defvar org-agenda-search-history nil)
4414 (defvar org-search-syntax-table nil
4415 "Special syntax table for Org search.
4416 In this table, we have single quotes not as word constituents, to
4417 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4419 (defvar org-mode-syntax-table) ; From org.el
4420 (defun org-search-syntax-table ()
4421 (unless org-search-syntax-table
4422 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4423 (modify-syntax-entry ?' "." org-search-syntax-table)
4424 (modify-syntax-entry ?` "." org-search-syntax-table))
4425 org-search-syntax-table)
4427 (defvar org-agenda-last-search-view-search-was-boolean nil)
4429 ;;;###autoload
4430 (defun org-search-view (&optional todo-only string edit-at)
4431 "Show all entries that contain a phrase or words or regular expressions.
4433 With optional prefix argument TODO-ONLY, only consider entries that are
4434 TODO entries. The argument STRING can be used to pass a default search
4435 string into this function. If EDIT-AT is non-nil, it means that the
4436 user should get a chance to edit this string, with cursor at position
4437 EDIT-AT.
4439 The search string can be viewed either as a phrase that should be found as
4440 is, or it can be broken into a number of snippets, each of which must match
4441 in a Boolean way to select an entry. The default depends on the variable
4442 `org-agenda-search-view-always-boolean'.
4443 Even if this is turned off (the default) you can always switch to
4444 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4446 The default is a direct search of the whole phrase, where each space in
4447 the search string can expand to an arbitrary amount of whitespace,
4448 including newlines.
4450 If using a Boolean search, the search string is split on whitespace and
4451 each snippet is searched separately, with logical AND to select an entry.
4452 Words prefixed with a minus must *not* occur in the entry. Words without
4453 a prefix or prefixed with a plus must occur in the entry. Matching is
4454 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4455 match whole words, not parts of a word) if
4456 `org-agenda-search-view-force-full-words' is set (default is nil).
4458 Boolean search snippets enclosed by curly braces are interpreted as
4459 regular expressions that must or (when preceded with \"-\") must not
4460 match in the entry. Snippets enclosed into double quotes will be taken
4461 as a whole, to include whitespace.
4463 - If the search string starts with an asterisk, search only in headlines.
4464 - If (possibly after the leading star) the search string starts with an
4465 exclamation mark, this also means to look at TODO entries only, an effect
4466 that can also be achieved with a prefix argument.
4467 - If (possibly after star and exclamation mark) the search string starts
4468 with a colon, this will mean that the (non-regexp) snippets of the
4469 Boolean search must match as full words.
4471 This command searches the agenda files, and in addition the files listed
4472 in `org-agenda-text-search-extra-files'."
4473 (interactive "P")
4474 (if org-agenda-overriding-arguments
4475 (setq todo-only (car org-agenda-overriding-arguments)
4476 string (nth 1 org-agenda-overriding-arguments)
4477 edit-at (nth 2 org-agenda-overriding-arguments)))
4478 (let* ((props (list 'face nil
4479 'done-face 'org-agenda-done
4480 'org-not-done-regexp org-not-done-regexp
4481 'org-todo-regexp org-todo-regexp
4482 'org-complex-heading-regexp org-complex-heading-regexp
4483 'mouse-face 'highlight
4484 'help-echo (format "mouse-2 or RET jump to location")))
4485 (full-words org-agenda-search-view-force-full-words)
4486 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4487 regexp rtn rtnall files file pos inherited-tags
4488 marker category level tags c neg re boolean
4489 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4490 (unless (and (not edit-at)
4491 (stringp string)
4492 (string-match "\\S-" string))
4493 (setq string (read-string
4494 (if org-agenda-search-view-always-boolean
4495 "[+-]Word/{Regexp} ...: "
4496 "Phrase or [+-]Word/{Regexp} ...: ")
4497 (cond
4498 ((integerp edit-at) (cons string edit-at))
4499 (edit-at string))
4500 'org-agenda-search-history)))
4501 (catch 'exit
4502 (if org-agenda-sticky
4503 (setq org-agenda-buffer-name
4504 (if (stringp string)
4505 (format "*Org Agenda(%s:%s)*"
4506 (or org-keys (or (and todo-only "S") "s")) string)
4507 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4508 (org-agenda-prepare "SEARCH")
4509 (org-compile-prefix-format 'search)
4510 (org-set-sorting-strategy 'search)
4511 (setq org-agenda-redo-command
4512 (list 'org-search-view (if todo-only t nil)
4513 (list 'if 'current-prefix-arg nil string)))
4514 (setq org-agenda-query-string string)
4515 (if (equal (string-to-char string) ?*)
4516 (setq hdl-only t
4517 words (substring string 1))
4518 (setq words string))
4519 (when (equal (string-to-char words) ?!)
4520 (setq todo-only t
4521 words (substring words 1)))
4522 (when (equal (string-to-char words) ?:)
4523 (setq full-words t
4524 words (substring words 1)))
4525 (if (or org-agenda-search-view-always-boolean
4526 (member (string-to-char words) '(?- ?+ ?\{)))
4527 (setq boolean t))
4528 (setq words (org-split-string words))
4529 (let (www w)
4530 (while (setq w (pop words))
4531 (while (and (string-match "\\\\\\'" w) words)
4532 (setq w (concat (substring w 0 -1) " " (pop words))))
4533 (push w www))
4534 (setq words (nreverse www) www nil)
4535 (while (setq w (pop words))
4536 (when (and (string-match "\\`[-+]?{" w)
4537 (not (string-match "}\\'" w)))
4538 (while (and words (not (string-match "}\\'" (car words))))
4539 (setq w (concat w " " (pop words))))
4540 (setq w (concat w " " (pop words))))
4541 (push w www))
4542 (setq words (nreverse www)))
4543 (setq org-agenda-last-search-view-search-was-boolean boolean)
4544 (when boolean
4545 (let (wds w)
4546 (while (setq w (pop words))
4547 (if (or (equal (substring w 0 1) "\"")
4548 (and (> (length w) 1)
4549 (member (substring w 0 1) '("+" "-"))
4550 (equal (substring w 1 2) "\"")))
4551 (while (and words (not (equal (substring w -1) "\"")))
4552 (setq w (concat w " " (pop words)))))
4553 (and (string-match "\\`\\([-+]?\\)\"" w)
4554 (setq w (replace-match "\\1" nil nil w)))
4555 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4556 (push w wds))
4557 (setq words (nreverse wds))))
4558 (if boolean
4559 (mapc (lambda (w)
4560 (setq c (string-to-char w))
4561 (if (equal c ?-)
4562 (setq neg t w (substring w 1))
4563 (if (equal c ?+)
4564 (setq neg nil w (substring w 1))
4565 (setq neg nil)))
4566 (if (string-match "\\`{.*}\\'" w)
4567 (setq re (substring w 1 -1))
4568 (if full-words
4569 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4570 (setq re (regexp-quote (downcase w)))))
4571 (if neg (push re regexps-) (push re regexps+)))
4572 words)
4573 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4574 regexps+))
4575 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4576 (if (not regexps+)
4577 (setq regexp org-outline-regexp-bol)
4578 (setq regexp (pop regexps+))
4579 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4580 regexp))))
4581 (setq files (org-agenda-files nil 'ifmode))
4582 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4583 (pop org-agenda-text-search-extra-files)
4584 (setq files (org-add-archive-files files)))
4585 (setq files (append files org-agenda-text-search-extra-files)
4586 rtnall nil)
4587 (while (setq file (pop files))
4588 (setq ee nil)
4589 (catch 'nextfile
4590 (org-check-agenda-file file)
4591 (setq buffer (if (file-exists-p file)
4592 (org-get-agenda-file-buffer file)
4593 (error "No such file %s" file)))
4594 (if (not buffer)
4595 ;; If file does not exist, make sure an error message is sent
4596 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4597 file))))
4598 (with-current-buffer buffer
4599 (with-syntax-table (org-search-syntax-table)
4600 (unless (derived-mode-p 'org-mode)
4601 (error "Agenda file %s is not in `org-mode'" file))
4602 (let ((case-fold-search t))
4603 (save-excursion
4604 (save-restriction
4605 (if (eq buffer org-agenda-restrict)
4606 (narrow-to-region org-agenda-restrict-begin
4607 org-agenda-restrict-end)
4608 (widen))
4609 (goto-char (point-min))
4610 (unless (or (org-at-heading-p)
4611 (outline-next-heading))
4612 (throw 'nextfile t))
4613 (goto-char (max (point-min) (1- (point))))
4614 (while (re-search-forward regexp nil t)
4615 (org-back-to-heading t)
4616 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4617 (> (org-reduced-level (org-outline-level))
4618 org-agenda-search-view-max-outline-level)
4619 (forward-line -1)
4620 (org-back-to-heading t)))
4621 (skip-chars-forward "* ")
4622 (setq beg (point-at-bol)
4623 beg1 (point)
4624 end (progn
4625 (outline-next-heading)
4626 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4627 (> (org-reduced-level (org-outline-level))
4628 org-agenda-search-view-max-outline-level)
4629 (forward-line 1)
4630 (outline-next-heading)))
4631 (point)))
4633 (catch :skip
4634 (goto-char beg)
4635 (org-agenda-skip)
4636 (setq str (buffer-substring-no-properties
4637 (point-at-bol)
4638 (if hdl-only (point-at-eol) end)))
4639 (mapc (lambda (wr) (when (string-match wr str)
4640 (goto-char (1- end))
4641 (throw :skip t)))
4642 regexps-)
4643 (mapc (lambda (wr) (unless (string-match wr str)
4644 (goto-char (1- end))
4645 (throw :skip t)))
4646 (if todo-only
4647 (cons (concat "^\\*+[ \t]+"
4648 org-not-done-regexp)
4649 regexps+)
4650 regexps+))
4651 (goto-char beg)
4652 (setq marker (org-agenda-new-marker (point))
4653 category (org-get-category)
4654 level (make-string (org-reduced-level (org-outline-level)) ? )
4655 inherited-tags
4656 (or (eq org-agenda-show-inherited-tags 'always)
4657 (and (listp org-agenda-show-inherited-tags)
4658 (memq 'todo org-agenda-show-inherited-tags))
4659 (and (eq org-agenda-show-inherited-tags t)
4660 (or (eq org-agenda-use-tag-inheritance t)
4661 (memq 'todo org-agenda-use-tag-inheritance))))
4662 tags (org-get-tags-at nil (not inherited-tags))
4663 txt (org-agenda-format-item
4665 (buffer-substring-no-properties
4666 beg1 (point-at-eol))
4667 level category tags t))
4668 (org-add-props txt props
4669 'org-marker marker 'org-hd-marker marker
4670 'org-todo-regexp org-todo-regexp
4671 'level level
4672 'org-complex-heading-regexp org-complex-heading-regexp
4673 'priority 1000
4674 'type "search")
4675 (push txt ee)
4676 (goto-char (1- end))))))))))
4677 (setq rtn (nreverse ee))
4678 (setq rtnall (append rtnall rtn)))
4679 (if org-agenda-overriding-header
4680 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4681 nil 'face 'org-agenda-structure) "\n")
4682 (insert "Search words: ")
4683 (add-text-properties (point-min) (1- (point))
4684 (list 'face 'org-agenda-structure))
4685 (setq pos (point))
4686 (insert string "\n")
4687 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4688 (setq pos (point))
4689 (unless org-agenda-multi
4690 (insert (substitute-command-keys "\
4691 Press `\\[org-agenda-manipulate-query-add]', \
4692 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4693 `\\[org-agenda-manipulate-query-add-re]', \
4694 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4695 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4696 (add-text-properties pos (1- (point))
4697 (list 'face 'org-agenda-structure))))
4698 (org-agenda-mark-header-line (point-min))
4699 (when rtnall
4700 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4701 (goto-char (point-min))
4702 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4703 (add-text-properties (point-min) (point-max)
4704 `(org-agenda-type search
4705 org-last-args (,todo-only ,string ,edit-at)
4706 org-redo-cmd ,org-agenda-redo-command
4707 org-series-cmd ,org-cmd))
4708 (org-agenda-finalize)
4709 (setq buffer-read-only t))))
4711 ;;; Agenda TODO list
4713 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4714 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4715 (concat
4716 (if (or (equal keywords "ALL") (not keywords))
4717 (propertize "ALL" 'face 'warning)
4718 (mapconcat
4719 (lambda (kw)
4720 (propertize kw 'face (org-get-todo-face kw)))
4721 (org-split-string keywords "|")
4722 "|"))
4723 "\n"))
4725 (defvar org-select-this-todo-keyword nil)
4726 (defvar org-last-arg nil)
4728 ;;;###autoload
4729 (defun org-todo-list (&optional arg)
4730 "Show all (not done) TODO entries from all agenda file in a single list.
4731 The prefix arg can be used to select a specific TODO keyword and limit
4732 the list to these. When using `\\[universal-argument]', you will be prompted
4733 for a keyword. A numeric prefix directly selects the Nth keyword in
4734 `org-todo-keywords-1'."
4735 (interactive "P")
4736 (if org-agenda-overriding-arguments
4737 (setq arg org-agenda-overriding-arguments))
4738 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4739 (let* ((today (org-today))
4740 (date (calendar-gregorian-from-absolute today))
4741 (kwds org-todo-keywords-for-agenda)
4742 (completion-ignore-case t)
4743 (org-select-this-todo-keyword
4744 (if (stringp arg) arg
4745 (and arg (integerp arg) (> arg 0)
4746 (nth (1- arg) kwds))))
4747 rtn rtnall files file pos)
4748 (when (equal arg '(4))
4749 (setq org-select-this-todo-keyword
4750 (completing-read "Keyword (or KWD1|K2D2|...): "
4751 (mapcar #'list kwds) nil nil)))
4752 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4753 (catch 'exit
4754 (if org-agenda-sticky
4755 (setq org-agenda-buffer-name
4756 (if (stringp org-select-this-todo-keyword)
4757 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4758 org-select-this-todo-keyword)
4759 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4760 (org-agenda-prepare "TODO")
4761 (org-compile-prefix-format 'todo)
4762 (org-set-sorting-strategy 'todo)
4763 (setq org-agenda-redo-command
4764 `(org-todo-list (or (and (numberp current-prefix-arg)
4765 current-prefix-arg)
4766 ,org-select-this-todo-keyword
4767 current-prefix-arg ,arg)))
4768 (setq files (org-agenda-files nil 'ifmode)
4769 rtnall nil)
4770 (while (setq file (pop files))
4771 (catch 'nextfile
4772 (org-check-agenda-file file)
4773 (setq rtn (org-agenda-get-day-entries file date :todo))
4774 (setq rtnall (append rtnall rtn))))
4775 (if org-agenda-overriding-header
4776 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4777 nil 'face 'org-agenda-structure) "\n")
4778 (insert "Global list of TODO items of type: ")
4779 (add-text-properties (point-min) (1- (point))
4780 (list 'face 'org-agenda-structure
4781 'short-heading
4782 (concat "ToDo: "
4783 (or org-select-this-todo-keyword "ALL"))))
4784 (org-agenda-mark-header-line (point-min))
4785 (insert (org-agenda-propertize-selected-todo-keywords
4786 org-select-this-todo-keyword))
4787 (setq pos (point))
4788 (unless org-agenda-multi
4789 (insert (substitute-command-keys "Available with \
4790 `N \\[org-agenda-redo]': (0)[ALL]"))
4791 (let ((n 0) s)
4792 (mapc (lambda (x)
4793 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4794 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4795 (insert "\n "))
4796 (insert " " s))
4797 kwds))
4798 (insert "\n"))
4799 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4800 (org-agenda-mark-header-line (point-min))
4801 (when rtnall
4802 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4803 (goto-char (point-min))
4804 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4805 (add-text-properties (point-min) (point-max)
4806 `(org-agenda-type todo
4807 org-last-args ,arg
4808 org-redo-cmd ,org-agenda-redo-command
4809 org-series-cmd ,org-cmd))
4810 (org-agenda-finalize)
4811 (setq buffer-read-only t))))
4813 ;;; Agenda tags match
4815 ;;;###autoload
4816 (defun org-tags-view (&optional todo-only match)
4817 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4818 The prefix arg TODO-ONLY limits the search to TODO entries."
4819 (interactive "P")
4820 (if org-agenda-overriding-arguments
4821 (setq todo-only (car org-agenda-overriding-arguments)
4822 match (nth 1 org-agenda-overriding-arguments)))
4823 (let* ((org-tags-match-list-sublevels
4824 org-tags-match-list-sublevels)
4825 (completion-ignore-case t)
4826 rtn rtnall files file pos matcher
4827 buffer)
4828 (when (and (stringp match) (not (string-match "\\S-" match)))
4829 (setq match nil))
4830 (catch 'exit
4831 (if org-agenda-sticky
4832 (setq org-agenda-buffer-name
4833 (if (stringp match)
4834 (format "*Org Agenda(%s:%s)*"
4835 (or org-keys (or (and todo-only "M") "m")) match)
4836 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4837 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4838 ;; expanding tags within `org-make-tags-matcher'
4839 (org-agenda-prepare (concat "TAGS " match))
4840 (setq org--matcher-tags-todo-only todo-only
4841 matcher (org-make-tags-matcher match)
4842 match (car matcher)
4843 matcher (cdr matcher))
4844 (org-compile-prefix-format 'tags)
4845 (org-set-sorting-strategy 'tags)
4846 (setq org-agenda-query-string match)
4847 (setq org-agenda-redo-command
4848 (list 'org-tags-view
4849 `(quote ,org--matcher-tags-todo-only)
4850 `(if current-prefix-arg nil ,org-agenda-query-string)))
4851 (setq files (org-agenda-files nil 'ifmode)
4852 rtnall nil)
4853 (while (setq file (pop files))
4854 (catch 'nextfile
4855 (org-check-agenda-file file)
4856 (setq buffer (if (file-exists-p file)
4857 (org-get-agenda-file-buffer file)
4858 (error "No such file %s" file)))
4859 (if (not buffer)
4860 ;; If file does not exist, error message to agenda
4861 (setq rtn (list
4862 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4863 rtnall (append rtnall rtn))
4864 (with-current-buffer buffer
4865 (unless (derived-mode-p 'org-mode)
4866 (error "Agenda file %s is not in `org-mode'" file))
4867 (save-excursion
4868 (save-restriction
4869 (if (eq buffer org-agenda-restrict)
4870 (narrow-to-region org-agenda-restrict-begin
4871 org-agenda-restrict-end)
4872 (widen))
4873 (setq rtn (org-scan-tags 'agenda
4874 matcher
4875 org--matcher-tags-todo-only))
4876 (setq rtnall (append rtnall rtn))))))))
4877 (if org-agenda-overriding-header
4878 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4879 nil 'face 'org-agenda-structure) "\n")
4880 (insert "Headlines with TAGS match: ")
4881 (add-text-properties (point-min) (1- (point))
4882 (list 'face 'org-agenda-structure
4883 'short-heading
4884 (concat "Match: " match)))
4885 (setq pos (point))
4886 (insert match "\n")
4887 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4888 (setq pos (point))
4889 (unless org-agenda-multi
4890 (insert (substitute-command-keys
4891 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4892 to search again with new search string\n")))
4893 (add-text-properties pos (1- (point))
4894 (list 'face 'org-agenda-structure)))
4895 (org-agenda-mark-header-line (point-min))
4896 (when rtnall
4897 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4898 (goto-char (point-min))
4899 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4900 (add-text-properties
4901 (point-min) (point-max)
4902 `(org-agenda-type tags
4903 org-last-args (,org--matcher-tags-todo-only ,match)
4904 org-redo-cmd ,org-agenda-redo-command
4905 org-series-cmd ,org-cmd))
4906 (org-agenda-finalize)
4907 (setq buffer-read-only t))))
4909 ;;; Agenda Finding stuck projects
4911 (defvar org-agenda-skip-regexp nil
4912 "Regular expression used in skipping subtrees for the agenda.
4913 This is basically a temporary global variable that can be set and then
4914 used by user-defined selections using `org-agenda-skip-function'.")
4916 (defvar org-agenda-overriding-header nil
4917 "When set during agenda, todo and tags searches it replaces the header.
4918 This variable should not be set directly, but custom commands can bind it
4919 in the options section.")
4921 (defun org-agenda-skip-entry-when-regexp-matches ()
4922 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4923 If yes, it returns the end position of this entry, causing agenda commands
4924 to skip the entry but continuing the search in the subtree. This is a
4925 function that can be put into `org-agenda-skip-function' for the duration
4926 of a command."
4927 (let ((end (save-excursion (org-end-of-subtree t)))
4928 skip)
4929 (save-excursion
4930 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4931 (and skip end)))
4933 (defun org-agenda-skip-subtree-when-regexp-matches ()
4934 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4935 If yes, it returns the end position of this tree, causing agenda commands
4936 to skip this subtree. This is a function that can be put into
4937 `org-agenda-skip-function' for the duration of a command."
4938 (let ((end (save-excursion (org-end-of-subtree t)))
4939 skip)
4940 (save-excursion
4941 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4942 (and skip end)))
4944 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4945 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4946 If yes, it returns the end position of the current entry (NOT the tree),
4947 causing agenda commands to skip the entry but continuing the search in
4948 the subtree. This is a function that can be put into
4949 `org-agenda-skip-function' for the duration of a command. An important
4950 use of this function is for the stuck project list."
4951 (let ((end (save-excursion (org-end-of-subtree t)))
4952 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4953 skip)
4954 (save-excursion
4955 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4956 (and skip entry-end)))
4958 (defun org-agenda-skip-entry-if (&rest conditions)
4959 "Skip entry if any of CONDITIONS is true.
4960 See `org-agenda-skip-if' for details."
4961 (org-agenda-skip-if nil conditions))
4963 (defun org-agenda-skip-subtree-if (&rest conditions)
4964 "Skip subtree if any of CONDITIONS is true.
4965 See `org-agenda-skip-if' for details."
4966 (org-agenda-skip-if t conditions))
4968 (defun org-agenda-skip-if (subtree conditions)
4969 "Checks current entity for CONDITIONS.
4970 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4971 the entry (i.e. the text before the next heading) is checked.
4973 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4974 from different tests. Valid conditions are:
4976 scheduled Check if there is a scheduled cookie
4977 notscheduled Check if there is no scheduled cookie
4978 deadline Check if there is a deadline
4979 notdeadline Check if there is no deadline
4980 timestamp Check if there is a timestamp (also deadline or scheduled)
4981 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4982 regexp Check if regexp matches
4983 notregexp Check if regexp does not match.
4984 todo Check if TODO keyword matches
4985 nottodo Check if TODO keyword does not match
4987 The regexp is taken from the conditions list, it must come right after
4988 the `regexp' or `notregexp' element.
4990 `todo' and `nottodo' accept as an argument a list of todo
4991 keywords, which may include \"*\" to match any todo keyword.
4993 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4995 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4997 Instead of a list, a keyword class may be given. For example:
4999 (org-agenda-skip-entry-if \\='nottodo \\='done)
5001 would skip entries that haven't been marked with any of \"DONE\"
5002 keywords. Possible classes are: `todo', `done', `any'.
5004 If any of these conditions is met, this function returns the end point of
5005 the entity, causing the search to continue from there. This is a function
5006 that can be put into `org-agenda-skip-function' for the duration of a command."
5007 (let (beg end m)
5008 (org-back-to-heading t)
5009 (setq beg (point)
5010 end (if subtree
5011 (progn (org-end-of-subtree t) (point))
5012 (progn (outline-next-heading) (1- (point)))))
5013 (goto-char beg)
5014 (and
5016 (and (memq 'scheduled conditions)
5017 (re-search-forward org-scheduled-time-regexp end t))
5018 (and (memq 'notscheduled conditions)
5019 (not (re-search-forward org-scheduled-time-regexp end t)))
5020 (and (memq 'deadline conditions)
5021 (re-search-forward org-deadline-time-regexp end t))
5022 (and (memq 'notdeadline conditions)
5023 (not (re-search-forward org-deadline-time-regexp end t)))
5024 (and (memq 'timestamp conditions)
5025 (re-search-forward org-ts-regexp end t))
5026 (and (memq 'nottimestamp conditions)
5027 (not (re-search-forward org-ts-regexp end t)))
5028 (and (setq m (memq 'regexp conditions))
5029 (stringp (nth 1 m))
5030 (re-search-forward (nth 1 m) end t))
5031 (and (setq m (memq 'notregexp conditions))
5032 (stringp (nth 1 m))
5033 (not (re-search-forward (nth 1 m) end t)))
5034 (and (or
5035 (setq m (memq 'nottodo conditions))
5036 (setq m (memq 'todo-unblocked conditions))
5037 (setq m (memq 'nottodo-unblocked conditions))
5038 (setq m (memq 'todo conditions)))
5039 (org-agenda-skip-if-todo m end)))
5040 end)))
5042 (defun org-agenda-skip-if-todo (args end)
5043 "Helper function for `org-agenda-skip-if', do not use it directly.
5044 ARGS is a list with first element either `todo', `nottodo',
5045 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5046 a list of TODO keywords, or a state symbol `todo' or `done' or
5047 `any'."
5048 (let ((kw (car args))
5049 (arg (cadr args))
5050 todo-wds todo-re)
5051 (setq todo-wds
5052 (org-uniquify
5053 (cond
5054 ((listp arg) ;; list of keywords
5055 (if (member "*" arg)
5056 (mapcar 'substring-no-properties org-todo-keywords-1)
5057 arg))
5058 ((symbolp arg) ;; keyword class name
5059 (cond
5060 ((eq arg 'todo)
5061 (org-delete-all org-done-keywords
5062 (mapcar 'substring-no-properties
5063 org-todo-keywords-1)))
5064 ((eq arg 'done) org-done-keywords)
5065 ((eq arg 'any)
5066 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5067 (setq todo-re
5068 (concat "^\\*+[ \t]+\\<\\("
5069 (mapconcat 'identity todo-wds "\\|")
5070 "\\)\\>"))
5071 (cond
5072 ((eq kw 'todo) (re-search-forward todo-re end t))
5073 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5074 ((eq kw 'todo-unblocked)
5075 (catch 'unblocked
5076 (while (re-search-forward todo-re end t)
5077 (or (org-entry-blocked-p) (throw 'unblocked t)))
5078 nil))
5079 ((eq kw 'nottodo-unblocked)
5080 (catch 'unblocked
5081 (while (re-search-forward todo-re end t)
5082 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5086 ;;;###autoload
5087 (defun org-agenda-list-stuck-projects (&rest ignore)
5088 "Create agenda view for projects that are stuck.
5089 Stuck projects are project that have no next actions. For the definitions
5090 of what a project is and how to check if it stuck, customize the variable
5091 `org-stuck-projects'."
5092 (interactive)
5093 (let* ((org-agenda-skip-function
5094 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5095 ;; We could have used org-agenda-skip-if here.
5096 (org-agenda-overriding-header
5097 (or org-agenda-overriding-header "List of stuck projects: "))
5098 (matcher (nth 0 org-stuck-projects))
5099 (todo (nth 1 org-stuck-projects))
5100 (todo-wds (if (member "*" todo)
5101 (progn
5102 (org-agenda-prepare-buffers (org-agenda-files
5103 nil 'ifmode))
5104 (org-delete-all
5105 org-done-keywords-for-agenda
5106 (copy-sequence org-todo-keywords-for-agenda)))
5107 todo))
5108 (todo-re (concat "^\\*+[ \t]+\\("
5109 (mapconcat 'identity todo-wds "\\|")
5110 "\\)\\>"))
5111 (tags (nth 2 org-stuck-projects))
5112 (tags-re (if (member "*" tags)
5113 (concat org-outline-regexp-bol
5114 ".*:[[:alnum:]_@#%]+:[ \t]*$")
5115 (if tags
5116 (concat org-outline-regexp-bol
5117 ".*:\\("
5118 (mapconcat #'identity tags "\\|")
5119 "\\):[[:alnum:]_@#%:]*[ \t]*$"))))
5120 (gen-re (nth 3 org-stuck-projects))
5121 (re-list
5122 (delq nil
5123 (list
5124 (if todo todo-re)
5125 (if tags tags-re)
5126 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5127 gen-re)))))
5128 (setq org-agenda-skip-regexp
5129 (if re-list
5130 (mapconcat 'identity re-list "\\|")
5131 (error "No information how to identify unstuck projects")))
5132 (org-tags-view nil matcher)
5133 (setq org-agenda-buffer-name (buffer-name))
5134 (with-current-buffer org-agenda-buffer-name
5135 (setq org-agenda-redo-command
5136 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5138 ;;; Diary integration
5140 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5141 (defvar diary-list-entries-hook)
5142 (defvar diary-time-regexp)
5143 (defun org-get-entries-from-diary (date)
5144 "Get the (Emacs Calendar) diary entries for DATE."
5145 (require 'diary-lib)
5146 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5147 (diary-display-function 'diary-fancy-display)
5148 (pop-up-frames nil)
5149 (diary-list-entries-hook
5150 (cons 'org-diary-default-entry diary-list-entries-hook))
5151 (diary-file-name-prefix nil) ; turn this feature off
5152 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5153 entries
5154 (org-disable-agenda-to-diary t))
5155 (save-excursion
5156 (save-window-excursion
5157 (funcall (if (fboundp 'diary-list-entries)
5158 'diary-list-entries 'list-diary-entries)
5159 date 1)))
5160 (if (not (get-buffer diary-fancy-buffer))
5161 (setq entries nil)
5162 (with-current-buffer diary-fancy-buffer
5163 (setq buffer-read-only nil)
5164 (if (zerop (buffer-size))
5165 ;; No entries
5166 (setq entries nil)
5167 ;; Omit the date and other unnecessary stuff
5168 (org-agenda-cleanup-fancy-diary)
5169 ;; Add prefix to each line and extend the text properties
5170 (if (zerop (buffer-size))
5171 (setq entries nil)
5172 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5173 (setq entries
5174 (with-temp-buffer
5175 (insert entries) (goto-char (point-min))
5176 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5177 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5178 (replace-match (concat "; " (match-string 1)))))
5179 (buffer-string)))))
5180 (set-buffer-modified-p nil)
5181 (kill-buffer diary-fancy-buffer)))
5182 (when entries
5183 (setq entries (org-split-string entries "\n"))
5184 (setq entries
5185 (mapcar
5186 (lambda (x)
5187 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5188 ;; Extend the text properties to the beginning of the line
5189 (org-add-props x (text-properties-at (1- (length x)) x)
5190 'type "diary" 'date date 'face 'org-agenda-diary))
5191 entries)))))
5193 (defvar org-agenda-cleanup-fancy-diary-hook nil
5194 "Hook run when the fancy diary buffer is cleaned up.")
5196 (defun org-agenda-cleanup-fancy-diary ()
5197 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5198 This gets rid of the date, the underline under the date, and
5199 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5200 date. It also removes lines that contain only whitespace."
5201 (goto-char (point-min))
5202 (if (looking-at ".*?:[ \t]*")
5203 (progn
5204 (replace-match "")
5205 (re-search-forward "\n=+$" nil t)
5206 (replace-match "")
5207 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5208 (re-search-forward "\n=+$" nil t)
5209 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5210 (goto-char (point-min))
5211 (while (re-search-forward "^ +\n" nil t)
5212 (replace-match ""))
5213 (goto-char (point-min))
5214 (if (re-search-forward "^Org mode dummy\n?" nil t)
5215 (replace-match ""))
5216 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5218 ;; Make sure entries from the diary have the right text properties.
5219 (eval-after-load "diary-lib"
5220 '(if (boundp 'diary-modify-entry-list-string-function)
5221 ;; We can rely on the hook, nothing to do
5223 ;; Hook not available, must use advice to make this work
5224 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5225 "Make the position visible."
5226 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5227 (stringp string)
5228 buffer-file-name)
5229 (setq string (org-modify-diary-entry-string string))))))
5231 (defun org-modify-diary-entry-string (string)
5232 "Add text properties to string, allowing Org to act on it."
5233 (org-add-props string nil
5234 'mouse-face 'highlight
5235 'help-echo (if buffer-file-name
5236 (format "mouse-2 or RET jump to diary file %s"
5237 (abbreviate-file-name buffer-file-name))
5239 'org-agenda-diary-link t
5240 'org-marker (org-agenda-new-marker (point-at-bol))))
5242 (defun org-diary-default-entry ()
5243 "Add a dummy entry to the diary.
5244 Needed to avoid empty dates which mess up holiday display."
5245 ;; Catch the error if dealing with the new add-to-diary-alist
5246 (when org-disable-agenda-to-diary
5247 (condition-case nil
5248 (org-add-to-diary-list original-date "Org mode dummy" "")
5249 (error
5250 (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
5252 (defun org-add-to-diary-list (&rest args)
5253 (if (fboundp 'diary-add-to-list)
5254 (apply 'diary-add-to-list args)
5255 (apply 'add-to-diary-list args)))
5257 (defvar org-diary-last-run-time nil)
5259 ;;;###autoload
5260 (defun org-diary (&rest args)
5261 "Return diary information from org files.
5262 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5263 It accesses org files and extracts information from those files to be
5264 listed in the diary. The function accepts arguments specifying what
5265 items should be listed. For a list of arguments allowed here, see the
5266 variable `org-agenda-entry-types'.
5268 The call in the diary file should look like this:
5270 &%%(org-diary) ~/path/to/some/orgfile.org
5272 Use a separate line for each org file to check. Or, if you omit the file name,
5273 all files listed in `org-agenda-files' will be checked automatically:
5275 &%%(org-diary)
5277 If you don't give any arguments (as in the example above), the default value
5278 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5279 So the example above may also be written as
5281 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5283 The function expects the lisp variables `entry' and `date' to be provided
5284 by the caller, because this is how the calendar works. Don't use this
5285 function from a program - use `org-agenda-get-day-entries' instead."
5286 (when (> (- (float-time)
5287 org-agenda-last-marker-time)
5289 ;; I am not sure if this works with sticky agendas, because the marker
5290 ;; list is then no longer a global variable.
5291 (org-agenda-reset-markers))
5292 (org-compile-prefix-format 'agenda)
5293 (org-set-sorting-strategy 'agenda)
5294 (setq args (or args org-agenda-entry-types))
5295 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5296 (list entry)
5297 (org-agenda-files t)))
5298 (time (float-time))
5299 file rtn results)
5300 (when (or (not org-diary-last-run-time)
5301 (> (- time
5302 org-diary-last-run-time)
5304 (org-agenda-prepare-buffers files))
5305 (setq org-diary-last-run-time time)
5306 ;; If this is called during org-agenda, don't return any entries to
5307 ;; the calendar. Org Agenda will list these entries itself.
5308 (if org-disable-agenda-to-diary (setq files nil))
5309 (while (setq file (pop files))
5310 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5311 (setq results (append results rtn)))
5312 (when results
5313 (setq results
5314 (mapcar (lambda (i) (replace-regexp-in-string
5315 org-bracket-link-regexp "\\3" i)) results))
5316 (concat (org-agenda-finalize-entries results) "\n"))))
5318 ;;; Agenda entry finders
5320 (defun org-agenda--timestamp-to-absolute (&rest args)
5321 "Call `org-time-string-to-absolute' with ARGS.
5322 However, throw `:skip' whenever an error is raised."
5323 (condition-case e
5324 (apply #'org-time-string-to-absolute args)
5325 (org-diary-sexp-no-match (throw :skip nil))
5326 (error
5327 (message "%s; Skipping entry" (error-message-string e))
5328 (throw :skip nil))))
5330 (defun org-agenda-get-day-entries (file date &rest args)
5331 "Does the work for `org-diary' and `org-agenda'.
5332 FILE is the path to a file to be checked for entries. DATE is date like
5333 the one returned by `calendar-current-date'. ARGS are symbols indicating
5334 which kind of entries should be extracted. For details about these, see
5335 the documentation of `org-diary'."
5336 (let* ((org-startup-folded nil)
5337 (org-startup-align-all-tables nil)
5338 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5339 (error "No such file %s" file))))
5340 (if (not buffer)
5341 ;; If file does not exist, signal it in diary nonetheless.
5342 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5343 (with-current-buffer buffer
5344 (unless (derived-mode-p 'org-mode)
5345 (error "Agenda file %s is not in `org-mode'" file))
5346 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5347 (setf org-agenda-current-date date)
5348 (save-excursion
5349 (save-restriction
5350 (if (eq buffer org-agenda-restrict)
5351 (narrow-to-region org-agenda-restrict-begin
5352 org-agenda-restrict-end)
5353 (widen))
5354 ;; Rationalize ARGS. Also make sure `:deadline' comes
5355 ;; first in order to populate DEADLINES before passing it.
5357 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5358 ;; guarding us from modifying `org-agenda-entry-types'.
5359 (setf args (org-uniquify (or args org-agenda-entry-types)))
5360 (when (and (memq :scheduled args) (memq :scheduled* args))
5361 (setf args (delq :scheduled* args)))
5362 (cond
5363 ((memq :deadline args)
5364 (setf args (cons :deadline
5365 (delq :deadline (delq :deadline* args)))))
5366 ((memq :deadline* args)
5367 (setf args (cons :deadline* (delq :deadline* args)))))
5368 ;; Collect list of headlines. Return them flattened.
5369 (let ((case-fold-search nil) results deadlines)
5370 (dolist (arg args (apply #'nconc (nreverse results)))
5371 (pcase arg
5372 ((and :todo (guard (org-agenda-today-p date)))
5373 (push (org-agenda-get-todos) results))
5374 (:timestamp
5375 (push (org-agenda-get-blocks) results)
5376 (push (org-agenda-get-timestamps deadlines) results))
5377 (:sexp
5378 (push (org-agenda-get-sexps) results))
5379 (:scheduled
5380 (push (org-agenda-get-scheduled deadlines) results))
5381 (:scheduled*
5382 (push (org-agenda-get-scheduled deadlines t) results))
5383 (:closed
5384 (push (org-agenda-get-progress) results))
5385 (:deadline
5386 (setf deadlines (org-agenda-get-deadlines))
5387 (push deadlines results))
5388 (:deadline*
5389 (setf deadlines (org-agenda-get-deadlines t))
5390 (push deadlines results)))))))))))
5392 (defsubst org-em (x y list)
5393 "Is X or Y a member of LIST?"
5394 (or (memq x list) (memq y list)))
5396 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5397 (defvar org-agenda-sorting-strategy-selected nil)
5399 (defun org-agenda-entry-get-agenda-timestamp (pom)
5400 "Retrieve timestamp information for sorting agenda views.
5401 Given a point or marker POM, returns a cons cell of the timestamp
5402 and the timestamp type relevant for the sorting strategy in
5403 `org-agenda-sorting-strategy-selected'."
5404 (let (ts ts-date-type)
5405 (save-match-data
5406 (cond ((org-em 'scheduled-up 'scheduled-down
5407 org-agenda-sorting-strategy-selected)
5408 (setq ts (org-entry-get pom "SCHEDULED")
5409 ts-date-type " scheduled"))
5410 ((org-em 'deadline-up 'deadline-down
5411 org-agenda-sorting-strategy-selected)
5412 (setq ts (org-entry-get pom "DEADLINE")
5413 ts-date-type " deadline"))
5414 ((org-em 'ts-up 'ts-down
5415 org-agenda-sorting-strategy-selected)
5416 (setq ts (org-entry-get pom "TIMESTAMP")
5417 ts-date-type " timestamp"))
5418 ((org-em 'tsia-up 'tsia-down
5419 org-agenda-sorting-strategy-selected)
5420 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5421 ts-date-type " timestamp_ia"))
5422 ((org-em 'timestamp-up 'timestamp-down
5423 org-agenda-sorting-strategy-selected)
5424 (setq ts (or (org-entry-get pom "SCHEDULED")
5425 (org-entry-get pom "DEADLINE")
5426 (org-entry-get pom "TIMESTAMP")
5427 (org-entry-get pom "TIMESTAMP_IA"))
5428 ts-date-type ""))
5429 (t (setq ts-date-type "")))
5430 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5431 ts-date-type))))
5433 (defun org-agenda-get-todos ()
5434 "Return the TODO information for agenda display."
5435 (let* ((props (list 'face nil
5436 'done-face 'org-agenda-done
5437 'org-not-done-regexp org-not-done-regexp
5438 'org-todo-regexp org-todo-regexp
5439 'org-complex-heading-regexp org-complex-heading-regexp
5440 'mouse-face 'highlight
5441 'help-echo
5442 (format "mouse-2 or RET jump to org file %s"
5443 (abbreviate-file-name buffer-file-name))))
5444 (case-fold-search nil)
5445 (regexp (format org-heading-keyword-regexp-format
5446 (cond
5447 ((and org-select-this-todo-keyword
5448 (equal org-select-this-todo-keyword "*"))
5449 org-todo-regexp)
5450 (org-select-this-todo-keyword
5451 (concat "\\("
5452 (mapconcat 'identity
5453 (org-split-string
5454 org-select-this-todo-keyword
5455 "|")
5456 "\\|") "\\)"))
5457 (t org-not-done-regexp))))
5458 marker priority category level tags todo-state
5459 ts-date ts-date-type ts-date-pair
5460 ee txt beg end inherited-tags todo-state-end-pos)
5461 (goto-char (point-min))
5462 (while (re-search-forward regexp nil t)
5463 (catch :skip
5464 (save-match-data
5465 (beginning-of-line)
5466 (org-agenda-skip)
5467 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5468 (unless (and (setq todo-state (org-get-todo-state))
5469 (setq todo-state-end-pos (match-end 2)))
5470 (goto-char end)
5471 (throw :skip nil))
5472 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5473 (goto-char (1+ beg))
5474 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5475 (throw :skip nil)))
5476 (goto-char (match-beginning 2))
5477 (setq marker (org-agenda-new-marker (match-beginning 0))
5478 category (org-get-category)
5479 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5480 ts-date (car ts-date-pair)
5481 ts-date-type (cdr ts-date-pair)
5482 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5483 inherited-tags
5484 (or (eq org-agenda-show-inherited-tags 'always)
5485 (and (listp org-agenda-show-inherited-tags)
5486 (memq 'todo org-agenda-show-inherited-tags))
5487 (and (eq org-agenda-show-inherited-tags t)
5488 (or (eq org-agenda-use-tag-inheritance t)
5489 (memq 'todo org-agenda-use-tag-inheritance))))
5490 tags (org-get-tags-at nil (not inherited-tags))
5491 level (make-string (org-reduced-level (org-outline-level)) ? )
5492 txt (org-agenda-format-item "" txt level category tags t)
5493 priority (1+ (org-get-priority txt)))
5494 (org-add-props txt props
5495 'org-marker marker 'org-hd-marker marker
5496 'priority priority
5497 'level level
5498 'ts-date ts-date
5499 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5500 (push txt ee)
5501 (if org-agenda-todo-list-sublevels
5502 (goto-char todo-state-end-pos)
5503 (org-end-of-subtree 'invisible))))
5504 (nreverse ee)))
5506 (defun org-agenda-todo-custom-ignore-p (time n)
5507 "Check whether timestamp is farther away than n number of days.
5508 This function is invoked if `org-agenda-todo-ignore-deadlines',
5509 `org-agenda-todo-ignore-scheduled' or
5510 `org-agenda-todo-ignore-timestamp' is set to an integer."
5511 (let ((days (org-time-stamp-to-now
5512 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5513 (if (>= n 0)
5514 (>= days n)
5515 (<= days n))))
5517 ;;;###autoload
5518 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5519 (&optional end)
5520 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5521 (when (or org-agenda-todo-ignore-with-date
5522 org-agenda-todo-ignore-scheduled
5523 org-agenda-todo-ignore-deadlines
5524 org-agenda-todo-ignore-timestamp)
5525 (setq end (or end (save-excursion (outline-next-heading) (point))))
5526 (save-excursion
5527 (or (and org-agenda-todo-ignore-with-date
5528 (re-search-forward org-ts-regexp end t))
5529 (and org-agenda-todo-ignore-scheduled
5530 (re-search-forward org-scheduled-time-regexp end t)
5531 (cond
5532 ((eq org-agenda-todo-ignore-scheduled 'future)
5533 (> (org-time-stamp-to-now
5534 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5535 ((eq org-agenda-todo-ignore-scheduled 'past)
5536 (<= (org-time-stamp-to-now
5537 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5538 ((numberp org-agenda-todo-ignore-scheduled)
5539 (org-agenda-todo-custom-ignore-p
5540 (match-string 1) org-agenda-todo-ignore-scheduled))
5541 (t)))
5542 (and org-agenda-todo-ignore-deadlines
5543 (re-search-forward org-deadline-time-regexp end t)
5544 (cond
5545 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5546 ((eq org-agenda-todo-ignore-deadlines 'far)
5547 (not (org-deadline-close-p (match-string 1))))
5548 ((eq org-agenda-todo-ignore-deadlines 'future)
5549 (> (org-time-stamp-to-now
5550 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5551 ((eq org-agenda-todo-ignore-deadlines 'past)
5552 (<= (org-time-stamp-to-now
5553 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5554 ((numberp org-agenda-todo-ignore-deadlines)
5555 (org-agenda-todo-custom-ignore-p
5556 (match-string 1) org-agenda-todo-ignore-deadlines))
5557 (t (org-deadline-close-p (match-string 1)))))
5558 (and org-agenda-todo-ignore-timestamp
5559 (let ((buffer (current-buffer))
5560 (regexp
5561 (concat
5562 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5563 (start (point)))
5564 ;; Copy current buffer into a temporary one
5565 (with-temp-buffer
5566 (insert-buffer-substring buffer start end)
5567 (goto-char (point-min))
5568 ;; Delete SCHEDULED and DEADLINE items
5569 (while (re-search-forward regexp end t)
5570 (delete-region (match-beginning 0) (match-end 0)))
5571 (goto-char (point-min))
5572 ;; No search for timestamp left
5573 (when (re-search-forward org-ts-regexp nil t)
5574 (cond
5575 ((eq org-agenda-todo-ignore-timestamp 'future)
5576 (> (org-time-stamp-to-now
5577 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5578 ((eq org-agenda-todo-ignore-timestamp 'past)
5579 (<= (org-time-stamp-to-now
5580 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5581 ((numberp org-agenda-todo-ignore-timestamp)
5582 (org-agenda-todo-custom-ignore-p
5583 (match-string 1) org-agenda-todo-ignore-timestamp))
5584 (t))))))))))
5586 (defun org-agenda-get-timestamps (&optional deadline-results)
5587 "Return the date stamp information for agenda display."
5588 (let* ((props (list 'face 'org-agenda-calendar-event
5589 'org-not-done-regexp org-not-done-regexp
5590 'org-todo-regexp org-todo-regexp
5591 'org-complex-heading-regexp org-complex-heading-regexp
5592 'mouse-face 'highlight
5593 'help-echo
5594 (format "mouse-2 or RET jump to org file %s"
5595 (abbreviate-file-name buffer-file-name))))
5596 (d1 (calendar-absolute-from-gregorian date))
5598 (deadline-position-alist
5599 (mapcar (lambda (a) (and (setq mm (get-text-property
5600 0 'org-hd-marker a))
5601 (cons (marker-position mm) a)))
5602 deadline-results))
5603 (remove-re org-ts-regexp)
5604 (regexp
5605 (concat
5606 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5607 (regexp-quote
5608 (substring
5609 (format-time-string
5610 (car org-time-stamp-formats)
5611 (apply 'encode-time ; DATE bound by calendar
5612 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5613 1 11))
5614 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5615 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5616 marker hdmarker clockp inactivep donep tmp priority category level ee
5617 txt timestr tags b0 b3 e3 head todo-state end-of-match show-all
5618 warntime habitp inherited-tags ts-date)
5619 (goto-char (point-min))
5620 (while (setq end-of-match (re-search-forward regexp nil t))
5621 (setq b0 (match-beginning 0)
5622 b3 (match-beginning 3) e3 (match-end 3)
5623 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5624 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5625 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5626 (member todo-state
5627 org-agenda-repeating-timestamp-show-all)))
5628 (catch :skip
5629 (when (org-at-date-range-p) (throw :skip nil))
5630 (when (org-at-planning-p) (throw :skip nil))
5631 (org-agenda-skip)
5632 (if (and (match-end 1)
5633 (not (= d1 (org-agenda--timestamp-to-absolute
5634 (match-string 1) d1 nil (current-buffer) b0))))
5635 (throw :skip nil))
5636 (if (and e3
5637 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5638 (throw :skip nil))
5639 (setq tmp (buffer-substring (max (point-min)
5640 (- b0 org-ds-keyword-length))
5642 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5643 inactivep (= (char-after b0) ?\[)
5644 clockp (and org-agenda-include-inactive-timestamps
5645 (or (string-match org-clock-string tmp)
5646 (string-match "]-+\\'" tmp)))
5647 warntime (get-text-property (point) 'org-appt-warntime)
5648 donep (member todo-state org-done-keywords))
5649 (when (or clockp (and donep org-agenda-skip-timestamp-if-done))
5650 (throw :skip t))
5651 (if (string-match ">" timestr)
5652 ;; substring should only run to end of time stamp
5653 (setq timestr (substring timestr 0 (match-end 0))))
5654 (setq marker (org-agenda-new-marker b0)
5655 category (org-get-category b0))
5656 (save-excursion
5657 (if (not (re-search-backward org-outline-regexp-bol nil t))
5658 (throw :skip nil)
5659 (goto-char (match-beginning 0))
5660 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5661 (assoc (point) deadline-position-alist))
5662 (throw :skip nil))
5663 (setq hdmarker (org-agenda-new-marker)
5664 inherited-tags
5665 (or (eq org-agenda-show-inherited-tags 'always)
5666 (and (listp org-agenda-show-inherited-tags)
5667 (memq 'agenda org-agenda-show-inherited-tags))
5668 (and (eq org-agenda-show-inherited-tags t)
5669 (or (eq org-agenda-use-tag-inheritance t)
5670 (memq 'agenda org-agenda-use-tag-inheritance))))
5671 tags (org-get-tags-at nil (not inherited-tags))
5672 level (make-string (org-reduced-level (org-outline-level)) ? ))
5673 (looking-at "\\*+[ \t]+\\(.*\\)")
5674 (setq head (match-string 1))
5675 (setq txt (org-agenda-format-item
5676 (if inactivep org-agenda-inactive-leader nil)
5677 head level category tags timestr
5678 remove-re habitp)))
5679 (setq priority (org-get-priority txt))
5680 (org-add-props txt props 'priority priority
5681 'org-marker marker 'org-hd-marker hdmarker
5682 'date date
5683 'level level
5684 'ts-date
5685 (ignore-errors (org-time-string-to-absolute timestr))
5686 'todo-state todo-state
5687 'warntime warntime
5688 'type "timestamp")
5689 (push txt ee))
5690 (if org-agenda-skip-additional-timestamps-same-entry
5691 (outline-next-heading)
5692 (goto-char end-of-match))))
5693 (nreverse ee)))
5695 (defun org-agenda-get-sexps ()
5696 "Return the sexp information for agenda display."
5697 (require 'diary-lib)
5698 (let* ((props (list 'face 'org-agenda-calendar-sexp
5699 'mouse-face 'highlight
5700 'help-echo
5701 (format "mouse-2 or RET jump to org file %s"
5702 (abbreviate-file-name buffer-file-name))))
5703 (regexp "^&?%%(")
5704 marker category extra level ee txt tags entry
5705 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5706 (goto-char (point-min))
5707 (while (re-search-forward regexp nil t)
5708 (catch :skip
5709 (org-agenda-skip)
5710 (setq beg (match-beginning 0))
5711 (goto-char (1- (match-end 0)))
5712 (setq b (point))
5713 (forward-sexp 1)
5714 (setq sexp (buffer-substring b (point)))
5715 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5716 (org-trim (match-string 1))
5717 ""))
5718 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5719 (when result
5720 (setq marker (org-agenda-new-marker beg)
5721 level (make-string (org-reduced-level (org-outline-level)) ? )
5722 category (org-get-category beg)
5723 inherited-tags
5724 (or (eq org-agenda-show-inherited-tags 'always)
5725 (and (listp org-agenda-show-inherited-tags)
5726 (memq 'agenda org-agenda-show-inherited-tags))
5727 (and (eq org-agenda-show-inherited-tags t)
5728 (or (eq org-agenda-use-tag-inheritance t)
5729 (memq 'agenda org-agenda-use-tag-inheritance))))
5730 tags (org-get-tags-at nil (not inherited-tags))
5731 todo-state (org-get-todo-state)
5732 warntime (get-text-property (point) 'org-appt-warntime)
5733 extra nil)
5735 (dolist (r (if (stringp result)
5736 (list result)
5737 result)) ;; we expect a list here
5738 (when (and org-agenda-diary-sexp-prefix
5739 (string-match org-agenda-diary-sexp-prefix r))
5740 (setq extra (match-string 0 r)
5741 r (replace-match "" nil nil r)))
5742 (if (string-match "\\S-" r)
5743 (setq txt r)
5744 (setq txt "SEXP entry returned empty string"))
5745 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5746 (org-add-props txt props 'org-marker marker
5747 'date date 'todo-state todo-state
5748 'level level 'type "sexp" 'warntime warntime)
5749 (push txt ee)))))
5750 (nreverse ee)))
5752 ;; Calendar sanity: define some functions that are independent of
5753 ;; `calendar-date-style'.
5754 (defun org-anniversary (year month day &optional mark)
5755 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5756 (with-no-warnings
5757 (let ((calendar-date-style 'iso))
5758 (diary-anniversary year month day mark))))
5759 (defun org-cyclic (N year month day &optional mark)
5760 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5761 (with-no-warnings
5762 (let ((calendar-date-style 'iso))
5763 (diary-cyclic N year month day mark))))
5764 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5765 "Like `diary-block', but with fixed (ISO) order of arguments."
5766 (with-no-warnings
5767 (let ((calendar-date-style 'iso))
5768 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5769 (defun org-date (year month day &optional mark)
5770 "Like `diary-date', but with fixed (ISO) order of arguments."
5771 (with-no-warnings
5772 (let ((calendar-date-style 'iso))
5773 (diary-date year month day mark))))
5775 ;; Define the `org-class' function
5776 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5777 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5778 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5779 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5780 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5781 `holidays', then any date that is known by the Emacs calendar to be a
5782 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5783 then those holidays will be skipped."
5784 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5785 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5786 (d (calendar-absolute-from-gregorian date))
5787 (h (when skip-weeks (calendar-check-holidays date))))
5788 (and
5789 (<= date1 d)
5790 (<= d date2)
5791 (= (calendar-day-of-week date) dayname)
5792 (or (not skip-weeks)
5793 (progn
5794 (require 'cal-iso)
5795 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5796 (not (or (and h (memq 'holidays skip-weeks))
5797 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5798 entry)))
5800 (defalias 'org-get-closed 'org-agenda-get-progress)
5801 (defun org-agenda-get-progress ()
5802 "Return the logged TODO entries for agenda display."
5803 (let* ((props (list 'mouse-face 'highlight
5804 'org-not-done-regexp org-not-done-regexp
5805 'org-todo-regexp org-todo-regexp
5806 'org-complex-heading-regexp org-complex-heading-regexp
5807 'help-echo
5808 (format "mouse-2 or RET jump to org file %s"
5809 (abbreviate-file-name buffer-file-name))))
5810 (items (if (consp org-agenda-show-log-scoped)
5811 org-agenda-show-log-scoped
5812 (if (eq org-agenda-show-log-scoped 'clockcheck)
5813 '(clock)
5814 org-agenda-log-mode-items)))
5815 (parts
5816 (delq nil
5817 (list
5818 (if (memq 'closed items) (concat "\\<" org-closed-string))
5819 (if (memq 'clock items) (concat "\\<" org-clock-string))
5820 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5821 (parts-re (if parts (mapconcat 'identity parts "\\|")
5822 (error "`org-agenda-log-mode-items' is empty")))
5823 (regexp (concat
5824 "\\(" parts-re "\\)"
5825 " *\\["
5826 (regexp-quote
5827 (substring
5828 (format-time-string
5829 (car org-time-stamp-formats)
5830 (apply 'encode-time ; DATE bound by calendar
5831 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5832 1 11))))
5833 (org-agenda-search-headline-for-time nil)
5834 marker hdmarker priority category level tags closedp
5835 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5836 (goto-char (point-min))
5837 (while (re-search-forward regexp nil t)
5838 (catch :skip
5839 (org-agenda-skip)
5840 (setq marker (org-agenda-new-marker (match-beginning 0))
5841 closedp (equal (match-string 1) org-closed-string)
5842 statep (equal (string-to-char (match-string 1)) ?-)
5843 clockp (not (or closedp statep))
5844 state (and statep (match-string 2))
5845 category (org-get-category (match-beginning 0))
5846 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5847 (when (string-match "\\]" timestr)
5848 ;; substring should only run to end of time stamp
5849 (setq rest (substring timestr (match-end 0))
5850 timestr (substring timestr 0 (match-end 0)))
5851 (if (and (not closedp) (not statep)
5852 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5853 rest))
5854 (progn (setq timestr (concat (substring timestr 0 -1)
5855 "-" (match-string 1 rest) "]"))
5856 (setq clocked (match-string 2 rest)))
5857 (setq clocked "-")))
5858 (save-excursion
5859 (setq extra
5860 (cond
5861 ((not org-agenda-log-mode-add-notes) nil)
5862 (statep
5863 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5864 (match-string 1)))
5865 (clockp
5866 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5867 (match-string 1)))))
5868 (if (not (re-search-backward org-outline-regexp-bol nil t))
5869 (throw :skip nil)
5870 (goto-char (match-beginning 0))
5871 (setq hdmarker (org-agenda-new-marker)
5872 inherited-tags
5873 (or (eq org-agenda-show-inherited-tags 'always)
5874 (and (listp org-agenda-show-inherited-tags)
5875 (memq 'todo org-agenda-show-inherited-tags))
5876 (and (eq org-agenda-show-inherited-tags t)
5877 (or (eq org-agenda-use-tag-inheritance t)
5878 (memq 'todo org-agenda-use-tag-inheritance))))
5879 tags (org-get-tags-at nil (not inherited-tags))
5880 level (make-string (org-reduced-level (org-outline-level)) ? ))
5881 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5882 (setq txt (match-string 1))
5883 (when extra
5884 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5885 (setq txt (concat (substring txt 0 (match-beginning 1))
5886 " - " extra " " (match-string 2 txt)))
5887 (setq txt (concat txt " - " extra))))
5888 (setq txt (org-agenda-format-item
5889 (cond
5890 (closedp "Closed: ")
5891 (statep (concat "State: (" state ")"))
5892 (t (concat "Clocked: (" clocked ")")))
5893 txt level category tags timestr)))
5894 (setq priority 100000)
5895 (org-add-props txt props
5896 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5897 'priority priority 'level level
5898 'type "closed" 'date date
5899 'undone-face 'org-warning 'done-face 'org-agenda-done)
5900 (push txt ee))
5901 (goto-char (point-at-eol))))
5902 (nreverse ee)))
5904 (defun org-agenda-show-clocking-issues ()
5905 "Add overlays, showing issues with clocking.
5906 See also the user option `org-agenda-clock-consistency-checks'."
5907 (interactive)
5908 (let* ((org-time-clocksum-use-effort-durations nil)
5909 (pl org-agenda-clock-consistency-checks)
5910 (re (concat "^[ \t]*"
5911 org-clock-string
5912 "[ \t]+"
5913 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5914 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5915 (tlstart 0.)
5916 (tlend 0.)
5917 (maxtime (org-hh:mm-string-to-minutes
5918 (or (plist-get pl :max-duration) "24:00")))
5919 (mintime (org-hh:mm-string-to-minutes
5920 (or (plist-get pl :min-duration) 0)))
5921 (maxgap (org-hh:mm-string-to-minutes
5922 ;; default 30:00 means never complain
5923 (or (plist-get pl :max-gap) "30:00")))
5924 (gapok (mapcar 'org-hh:mm-string-to-minutes
5925 (plist-get pl :gap-ok-around)))
5926 (def-face (or (plist-get pl :default-face)
5927 '((:background "DarkRed") (:foreground "white"))))
5928 issue face m te ts dt ov)
5929 (goto-char (point-min))
5930 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5931 (setq issue nil face def-face)
5932 (catch 'next
5933 (setq m (org-get-at-bol 'org-marker)
5934 te nil ts nil)
5935 (unless (and m (markerp m))
5936 (setq issue "No valid clock line") (throw 'next t))
5937 (org-with-point-at m
5938 (save-excursion
5939 (goto-char (point-at-bol))
5940 (unless (looking-at re)
5941 (error "No valid Clock line")
5942 (throw 'next t))
5943 (unless (match-end 3)
5944 (setq issue "No end time"
5945 face (or (plist-get pl :no-end-time-face) face))
5946 (throw 'next t))
5947 (setq ts (match-string 1)
5948 te (match-string 3)
5949 ts (float-time
5950 (apply #'encode-time (org-parse-time-string ts)))
5951 te (float-time
5952 (apply #'encode-time (org-parse-time-string te)))
5953 dt (- te ts))))
5954 (cond
5955 ((> dt (* 60 maxtime))
5956 ;; a very long clocking chunk
5957 (setq issue (format "Clocking interval is very long: %s"
5958 (org-minutes-to-clocksum-string
5959 (floor (/ (float dt) 60.))))
5960 face (or (plist-get pl :long-face) face)))
5961 ((< dt (* 60 mintime))
5962 ;; a very short clocking chunk
5963 (setq issue (format "Clocking interval is very short: %s"
5964 (org-minutes-to-clocksum-string
5965 (floor (/ (float dt) 60.))))
5966 face (or (plist-get pl :short-face) face)))
5967 ((and (> tlend 0) (< ts tlend))
5968 ;; Two clock entries are overlapping
5969 (setq issue (format "Clocking overlap: %d minutes"
5970 (/ (- tlend ts) 60))
5971 face (or (plist-get pl :overlap-face) face)))
5972 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5973 ;; There is a gap, lets see if we need to report it
5974 (unless (org-agenda-check-clock-gap tlend ts gapok)
5975 (setq issue (format "Clocking gap: %d minutes"
5976 (/ (- ts tlend) 60))
5977 face (or (plist-get pl :gap-face) face))))
5978 (t nil)))
5979 (setq tlend (or te tlend) tlstart (or ts tlstart))
5980 (when issue
5981 ;; OK, there was some issue, add an overlay to show the issue
5982 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5983 (overlay-put ov 'before-string
5984 (concat
5985 (org-add-props
5986 (format "%-43s" (concat " " issue))
5988 'face face)
5989 "\n"))
5990 (overlay-put ov 'evaporate t)))))
5992 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5993 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5994 (catch 'exit
5995 (unless ok-list
5996 ;; there are no OK times for gaps...
5997 (throw 'exit nil))
5998 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5999 ;; This is more than 24 hours, so it is OK.
6000 ;; because we have at least one OK time, that must be in the
6001 ;; 24 hour interval.
6002 (throw 'exit t))
6003 ;; We have a shorter gap.
6004 ;; Now we have to get the minute of the day when these times are
6005 (let* ((t1dec (decode-time (seconds-to-time t1)))
6006 (t2dec (decode-time (seconds-to-time t2)))
6007 ;; compute the minute on the day
6008 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6009 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6010 (when (< min2 min1)
6011 ;; if min2 is smaller than min1, this means it is on the next day.
6012 ;; Wrap it to after midnight.
6013 (setq min2 (+ min2 1440)))
6014 ;; Now check if any of the OK times is in the gap
6015 (mapc (lambda (x)
6016 ;; Wrap the time to after midnight if necessary
6017 (if (< x min1) (setq x (+ x 1440)))
6018 ;; Check if in interval
6019 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6020 ok-list)
6021 ;; Nope, this gap is not OK
6022 nil)))
6024 (defun org-agenda-get-deadlines (&optional with-hour)
6025 "Return the deadline information for agenda display.
6026 When WITH-HOUR is non-nil, only return deadlines with an hour
6027 specification like [h]h:mm."
6028 (let* ((props (list 'mouse-face 'highlight
6029 'org-not-done-regexp org-not-done-regexp
6030 'org-todo-regexp org-todo-regexp
6031 'org-complex-heading-regexp org-complex-heading-regexp
6032 'help-echo
6033 (format "mouse-2 or RET jump to org file %s"
6034 (abbreviate-file-name buffer-file-name))))
6035 (regexp (if with-hour
6036 org-deadline-time-hour-regexp
6037 org-deadline-time-regexp))
6038 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6039 (current (calendar-absolute-from-gregorian date))
6040 deadline-items)
6041 (goto-char (point-min))
6042 (while (re-search-forward regexp nil t)
6043 (catch :skip
6044 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6045 (org-agenda-skip)
6046 (let* ((s (match-string 1))
6047 (pos (1- (match-beginning 1)))
6048 (todo-state (save-match-data (org-get-todo-state)))
6049 (donep (member todo-state org-done-keywords))
6050 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6051 (member todo-state
6052 org-agenda-repeating-timestamp-show-all)))
6053 ;; DEADLINE is the current scheduled date. When it
6054 ;; contains a repeater and SHOW-ALL is non-nil,
6055 ;; LAST-REPEAT is the repeat closest to CURRENT.
6056 ;; Otherwise, LAST-REPEAT is equal to DEADLINE.
6057 (last-repeat (org-agenda--timestamp-to-absolute
6058 s current 'past (current-buffer) pos))
6059 (deadline (org-agenda--timestamp-to-absolute s current))
6060 (diff (- last-repeat current))
6061 (suppress-prewarning
6062 (let ((scheduled
6063 (and org-agenda-skip-deadline-prewarning-if-scheduled
6064 (org-entry-get nil "SCHEDULED"))))
6065 (cond
6066 ((not scheduled) nil)
6067 ;; The current item has a scheduled date, so
6068 ;; evaluate its prewarning lead time.
6069 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6070 ;; Use global prewarning-restart lead time.
6071 org-agenda-skip-deadline-prewarning-if-scheduled)
6072 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6073 'pre-scheduled)
6074 ;; Set pre-warning to no earlier than SCHEDULED.
6075 (min (- last-repeat
6076 (org-agenda--timestamp-to-absolute
6077 scheduled current 'past
6078 (current-buffer)
6079 (save-excursion
6080 (beginning-of-line)
6081 (1+ (search-forward org-deadline-string)))))
6082 org-deadline-warning-days))
6083 ;; Set pre-warning to deadline.
6084 (t 0))))
6085 (wdays (if suppress-prewarning
6086 (let ((org-deadline-warning-days suppress-prewarning))
6087 (org-get-wdays s))
6088 (org-get-wdays s))))
6089 ;; When to show a deadline in the calendar: if the
6090 ;; expiration is within WDAYS warning time. Past-due
6091 ;; deadlines are only shown on the current date
6092 (unless (or (and (<= diff wdays)
6093 (and todayp (not org-agenda-only-exact-dates)))
6094 (= diff 0))
6095 (throw :skip nil))
6096 ;; Skip done tasks if `org-agenda-skip-deadline-if-done' is
6097 ;; non-nil or if it isn't applicable to CURRENT deadline.
6098 (when (and donep
6099 (or org-agenda-skip-deadline-if-done
6100 (/= deadline current)))
6101 (throw :skip nil))
6102 (save-excursion
6103 (re-search-backward "^\\*+[ \t]+" nil t)
6104 (goto-char (match-end 0))
6105 (let* ((category (org-get-category))
6106 (level
6107 (make-string (org-reduced-level (org-outline-level)) ?\s))
6108 (head (buffer-substring (point) (line-end-position)))
6109 (inherited-tags
6110 (or (eq org-agenda-show-inherited-tags 'always)
6111 (and (listp org-agenda-show-inherited-tags)
6112 (memq 'agenda org-agenda-show-inherited-tags))
6113 (and (eq org-agenda-show-inherited-tags t)
6114 (or (eq org-agenda-use-tag-inheritance t)
6115 (memq 'agenda
6116 org-agenda-use-tag-inheritance)))))
6117 (tags (org-get-tags-at nil (not inherited-tags)))
6118 (timestr
6119 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6120 (concat (substring s (match-beginning 1)) " ")
6121 'time))
6122 (item
6123 (org-agenda-format-item
6124 ;; For past deadlines, make sure to report time
6125 ;; difference since date S, not since closest
6126 ;; repeater.
6127 (let ((diff (if (< (org-today) current) diff
6128 (- deadline current))))
6129 (if (= diff 0) (car org-agenda-deadline-leaders)
6130 (let ((future (nth 1 org-agenda-deadline-leaders))
6131 (past (nth 2 org-agenda-deadline-leaders)))
6132 (cond ((> diff 0) (format future diff))
6133 ((string= future past) (format past diff))
6134 (t (format past (abs diff)))))))
6135 head level category tags
6136 (and (= diff 0) timestr)))
6137 (face (org-agenda-deadline-face
6138 (- 1 (/ (float (- deadline current)) (max wdays 1)))))
6139 (upcomingp (and todayp (> diff 0)))
6140 (warntime (get-text-property (point) 'org-appt-warntime)))
6141 (org-add-props item props
6142 'org-marker (org-agenda-new-marker pos)
6143 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6144 'warntime warntime
6145 'level level
6146 'ts-date deadline
6147 'priority (- (org-get-priority item) diff)
6148 'todo-state todo-state
6149 'type (if upcomingp "upcoming-deadline" "deadline")
6150 'date (if upcomingp date deadline)
6151 'face (if donep 'org-agenda-done face)
6152 'undone-face face
6153 'done-face 'org-agenda-done)
6154 (push item deadline-items))))))
6155 (nreverse deadline-items)))
6157 (defun org-agenda-deadline-face (fraction)
6158 "Return the face to displaying a deadline item.
6159 FRACTION is what fraction of the head-warning time has passed."
6160 (let ((faces org-agenda-deadline-faces) f)
6161 (catch 'exit
6162 (while (setq f (pop faces))
6163 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6165 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6166 "Return the scheduled information for agenda display.
6167 Optional argument DEADLINES is a list of deadline items to be
6168 displayed in agenda view. When WITH-HOUR is non-nil, only return
6169 scheduled items with an hour specification like [h]h:mm."
6170 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6171 'org-todo-regexp org-todo-regexp
6172 'org-complex-heading-regexp org-complex-heading-regexp
6173 'done-face 'org-agenda-done
6174 'mouse-face 'highlight
6175 'help-echo
6176 (format "mouse-2 or RET jump to Org file %s"
6177 (abbreviate-file-name buffer-file-name))))
6178 (regexp (if with-hour
6179 org-scheduled-time-hour-regexp
6180 org-scheduled-time-regexp))
6181 (today (org-today))
6182 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6183 (current (calendar-absolute-from-gregorian date))
6184 (deadline-pos
6185 (mapcar (lambda (d)
6186 (let ((m (get-text-property 0 'org-hd-marker d)))
6187 (and m (marker-position m))))
6188 deadlines))
6189 scheduled-items)
6190 (goto-char (point-min))
6191 (while (re-search-forward regexp nil t)
6192 (catch :skip
6193 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6194 (org-agenda-skip)
6195 (let* ((s (match-string 1))
6196 (pos (1- (match-beginning 1)))
6197 (todo-state (save-match-data (org-get-todo-state)))
6198 (donep (member todo-state org-done-keywords))
6199 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6200 (member todo-state
6201 org-agenda-repeating-timestamp-show-all)))
6202 ;; SCHEDULE is the bare scheduled date, i.e., without
6203 ;; any repeater. REPEAT is the closest repeat after
6204 ;; CURRENT, if all repeated time stamps are to be
6205 ;; shown, or after TODAY otherwise. REPEAT only
6206 ;; applies to future dates.
6207 (schedule (org-agenda--timestamp-to-absolute s))
6208 (repeat (cond ((< current today) schedule)
6209 (show-all
6210 (org-agenda--timestamp-to-absolute
6211 s current 'future (current-buffer) pos))
6213 (org-agenda--timestamp-to-absolute
6214 s today 'future (current-buffer) pos))))
6215 (diff (- current schedule))
6216 (warntime (get-text-property (point) 'org-appt-warntime))
6217 (pastschedp (< schedule today))
6218 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6219 (suppress-delay
6220 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6221 (org-entry-get nil "DEADLINE"))))
6222 (cond
6223 ((not deadline) nil)
6224 ;; The current item has a deadline date, so
6225 ;; evaluate its delay time.
6226 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6227 ;; Use global delay time.
6228 (- org-agenda-skip-scheduled-delay-if-deadline))
6229 ((eq org-agenda-skip-scheduled-delay-if-deadline
6230 'post-deadline)
6231 ;; Set delay to no later than DEADLINE. If
6232 ;; DEADLINE has a repeater, compare last schedule
6233 ;; repeat and last deadline repeat.
6234 (min (- schedule deadline) org-scheduled-delay-days))
6235 (t 0))))
6236 (ddays
6237 (cond
6238 ;; Nullify delay when a repeater triggered already
6239 ;; and the delay is of the form --Xd.
6240 ((and (string-match-p "--[0-9]+[hdwmy]" s)
6241 (> current schedule))
6243 (suppress-delay
6244 (let ((org-scheduled-delay-days suppress-delay))
6245 (org-get-wdays s t t)))
6246 (t (org-get-wdays s t)))))
6247 ;; Display scheduled items at base date (SCHEDULE), today if
6248 ;; scheduled before the current date, and at any repeat past
6249 ;; today. However, skip delayed items and items that have
6250 ;; been displayed for more than `org-scheduled-past-days'.
6251 (unless (and todayp
6252 habitp
6253 (bound-and-true-p org-habit-show-all-today))
6254 (when (or (and (> ddays 0) (< diff ddays))
6255 (> diff org-scheduled-past-days)
6256 (> schedule current)
6257 (and (< schedule current)
6258 (not todayp)
6259 (/= repeat current)))
6260 (throw :skip nil)))
6261 ;; Possibly skip done tasks.
6262 (when (and donep org-agenda-skip-scheduled-if-done)
6263 (throw :skip nil))
6264 ;; Skip entry if it already appears as a deadline, per
6265 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6266 ;; doesn't apply to habits.
6267 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6268 ((guard
6269 (or (not (memq (line-beginning-position 0) deadline-pos))
6270 habitp))
6271 nil)
6272 (`repeated-after-deadline
6273 (>= repeat (time-to-days (org-get-deadline-time (point)))))
6274 (`not-today pastschedp)
6275 (`t t)
6276 (_ nil))
6277 (throw :skip nil))
6278 ;; Skip habits if `org-habit-show-habits' is nil, or if we
6279 ;; only show them for today. Also skip done habits.
6280 (when (and habitp
6281 (or donep
6282 (not (bound-and-true-p org-habit-show-habits))
6283 (and (not todayp)
6284 (bound-and-true-p
6285 org-habit-show-habits-only-for-today))))
6286 (throw :skip nil))
6287 (save-excursion
6288 (re-search-backward "^\\*+[ \t]+" nil t)
6289 (goto-char (match-end 0))
6290 (let* ((category (org-get-category))
6291 (inherited-tags
6292 (or (eq org-agenda-show-inherited-tags 'always)
6293 (and (listp org-agenda-show-inherited-tags)
6294 (memq 'agenda org-agenda-show-inherited-tags))
6295 (and (eq org-agenda-show-inherited-tags t)
6296 (or (eq org-agenda-use-tag-inheritance t)
6297 (memq 'agenda
6298 org-agenda-use-tag-inheritance)))))
6299 (tags (org-get-tags-at nil (not inherited-tags)))
6300 (level
6301 (make-string (org-reduced-level (org-outline-level)) ?\s))
6302 (head (buffer-substring (point) (line-end-position)))
6303 (timestr
6304 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6305 (concat (substring s (match-beginning 1)) " ")
6306 'time))
6307 (item
6308 (org-agenda-format-item
6309 (pcase-let ((`(,first ,next) org-agenda-scheduled-leaders))
6310 (cond
6311 ;; If CURRENT is in the future, don't use past
6312 ;; scheduled prefix.
6313 ((> current today) first)
6314 ;; SHOW-ALL focuses on future repeats. If one
6315 ;; such repeat happens today, ignore late
6316 ;; schedule reminder. However, still report
6317 ;; such reminders when repeat happens later.
6318 ((and (not show-all) (= repeat today)) first)
6319 ;; Initial report.
6320 ((= schedule current) first)
6321 ;; Subsequent reminders. Count from base
6322 ;; schedule.
6323 (t (format next (1+ diff)))))
6324 head level category tags timestr nil habitp))
6325 (face (cond ((and (not habitp) (< current today))
6326 'org-scheduled-previously)
6327 (todayp 'org-scheduled-today)
6328 (t 'org-scheduled)))
6329 (habitp (and habitp (org-habit-parse-todo))))
6330 (org-add-props item props
6331 'undone-face face
6332 'face (if donep 'org-agenda-done face)
6333 'org-marker (org-agenda-new-marker pos)
6334 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6335 'type (if pastschedp "past-scheduled" "scheduled")
6336 'date (if pastschedp schedule date)
6337 'ts-date schedule
6338 'warntime warntime
6339 'level level
6340 'priority (if habitp (org-habit-get-priority habitp)
6341 (+ 99 diff (org-get-priority item)))
6342 'org-habit-p habitp
6343 'todo-state todo-state)
6344 (push item scheduled-items))))))
6345 (nreverse scheduled-items)))
6347 (defun org-agenda-get-blocks ()
6348 "Return the date-range information for agenda display."
6349 (let* ((props (list 'face nil
6350 'org-not-done-regexp org-not-done-regexp
6351 'org-todo-regexp org-todo-regexp
6352 'org-complex-heading-regexp org-complex-heading-regexp
6353 'mouse-face 'highlight
6354 'help-echo
6355 (format "mouse-2 or RET jump to org file %s"
6356 (abbreviate-file-name buffer-file-name))))
6357 (regexp org-tr-regexp)
6358 (d0 (calendar-absolute-from-gregorian date))
6359 marker hdmarker ee txt d1 d2 s1 s2 category
6360 level todo-state tags pos head donep inherited-tags)
6361 (goto-char (point-min))
6362 (while (re-search-forward regexp nil t)
6363 (catch :skip
6364 (org-agenda-skip)
6365 (setq pos (point))
6366 (let ((start-time (match-string 1))
6367 (end-time (match-string 2)))
6368 (setq s1 (match-string 1)
6369 s2 (match-string 2)
6370 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6371 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6372 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6373 ;; Only allow days between the limits, because the normal
6374 ;; date stamps will catch the limits.
6375 (save-excursion
6376 (setq todo-state (org-get-todo-state))
6377 (setq donep (member todo-state org-done-keywords))
6378 (if (and donep org-agenda-skip-timestamp-if-done)
6379 (throw :skip t))
6380 (setq marker (org-agenda-new-marker (point))
6381 category (org-get-category))
6382 (if (not (re-search-backward org-outline-regexp-bol nil t))
6383 (throw :skip nil)
6384 (goto-char (match-beginning 0))
6385 (setq hdmarker (org-agenda-new-marker (point))
6386 inherited-tags
6387 (or (eq org-agenda-show-inherited-tags 'always)
6388 (and (listp org-agenda-show-inherited-tags)
6389 (memq 'agenda org-agenda-show-inherited-tags))
6390 (and (eq org-agenda-show-inherited-tags t)
6391 (or (eq org-agenda-use-tag-inheritance t)
6392 (memq 'agenda org-agenda-use-tag-inheritance))))
6394 tags (org-get-tags-at nil (not inherited-tags)))
6395 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6396 (looking-at "\\*+[ \t]+\\(.*\\)")
6397 (setq head (match-string 1))
6398 (let ((remove-re
6399 (if org-agenda-remove-timeranges-from-blocks
6400 (concat
6401 "<" (regexp-quote s1) ".*?>"
6402 "--"
6403 "<" (regexp-quote s2) ".*?>")
6404 nil)))
6405 (setq txt (org-agenda-format-item
6406 (format
6407 (nth (if (= d1 d2) 0 1)
6408 org-agenda-timerange-leaders)
6409 (1+ (- d0 d1)) (1+ (- d2 d1)))
6410 head level category tags
6411 (cond ((and (= d1 d0) (= d2 d0))
6412 (concat "<" start-time ">--<" end-time ">"))
6413 ((= d1 d0)
6414 (concat "<" start-time ">"))
6415 ((= d2 d0)
6416 (concat "<" end-time ">")))
6417 remove-re))))
6418 (org-add-props txt props
6419 'org-marker marker 'org-hd-marker hdmarker
6420 'type "block" 'date date
6421 'level level
6422 'todo-state todo-state
6423 'priority (org-get-priority txt))
6424 (push txt ee))))
6425 (goto-char pos)))
6426 ;; Sort the entries by expiration date.
6427 (nreverse ee)))
6429 ;;; Agenda presentation and sorting
6431 (defvar org-prefix-has-time nil
6432 "A flag, set by `org-compile-prefix-format'.
6433 The flag is set if the currently compiled format contains a `%t'.")
6434 (defvar org-prefix-has-tag nil
6435 "A flag, set by `org-compile-prefix-format'.
6436 The flag is set if the currently compiled format contains a `%T'.")
6437 (defvar org-prefix-has-effort nil
6438 "A flag, set by `org-compile-prefix-format'.
6439 The flag is set if the currently compiled format contains a `%e'.")
6440 (defvar org-prefix-has-breadcrumbs nil
6441 "A flag, set by `org-compile-prefix-format'.
6442 The flag is set if the currently compiled format contains a `%b'.")
6443 (defvar org-prefix-category-length nil
6444 "Used by `org-compile-prefix-format' to remember the category field width.")
6445 (defvar org-prefix-category-max-length nil
6446 "Used by `org-compile-prefix-format' to remember the category field width.")
6448 (defun org-agenda-get-category-icon (category)
6449 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6450 (cl-dolist (entry org-agenda-category-icon-alist)
6451 (when (string-match-p (car entry) category)
6452 (if (listp (cadr entry))
6453 (cl-return (cadr entry))
6454 (cl-return (apply #'create-image (cdr entry)))))))
6456 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6457 remove-re habitp)
6458 "Format TXT to be inserted into the agenda buffer.
6459 In particular, add the prefix and corresponding text properties.
6461 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6462 LEVEL may be a string to replace the `%l' specifier.
6463 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6464 category taken from local variable or file name. It will replace the `%c'
6465 specifier in the format.
6466 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6467 TXT for sorting of this entry, and for the `%t' specifier in the format.
6468 When DOTIME is a string, this string is searched for a time before TXT is.
6469 TAGS can be the tags of the headline.
6470 Any match of REMOVE-RE will be removed from TXT."
6471 ;; We keep the org-prefix-* variable values along with a compiled
6472 ;; formatter, so that multiple agendas existing at the same time do
6473 ;; not step on each other toes.
6475 ;; It was inconvenient to make these variables buffer local in
6476 ;; Agenda buffers, because this function expects to be called with
6477 ;; the buffer where item comes from being current, and not agenda
6478 ;; buffer
6479 (let* ((bindings (car org-prefix-format-compiled))
6480 (formatter (cadr org-prefix-format-compiled)))
6481 (cl-loop for (var value) in bindings
6482 do (set var value))
6483 (save-match-data
6484 ;; Diary entries sometimes have extra whitespace at the beginning
6485 (setq txt (org-trim txt))
6487 ;; Fix the tags part in txt
6488 (setq txt (org-agenda-fix-displayed-tags
6489 txt tags
6490 org-agenda-show-inherited-tags
6491 org-agenda-hide-tags-regexp))
6493 (let* ((category (or category
6494 (if buffer-file-name
6495 (file-name-sans-extension
6496 (file-name-nondirectory buffer-file-name))
6497 "")))
6498 (category-icon (org-agenda-get-category-icon category))
6499 (category-icon (if category-icon
6500 (propertize " " 'display category-icon)
6501 ""))
6502 (effort (and (not (string= txt ""))
6503 (get-text-property 1 'effort txt)))
6504 ;; time, tag, effort are needed for the eval of the prefix format
6505 (tag (if tags (nth (1- (length tags)) tags) ""))
6506 time
6507 (ts (if dotime (concat
6508 (if (stringp dotime) dotime "")
6509 (and org-agenda-search-headline-for-time txt))))
6510 (time-of-day (and dotime (org-get-time-of-day ts)))
6511 stamp plain s0 s1 s2 rtn srp l
6512 duration breadcrumbs)
6513 (and (derived-mode-p 'org-mode) buffer-file-name
6514 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6515 (when (and dotime time-of-day)
6516 ;; Extract starting and ending time and move them to prefix
6517 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6518 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6519 (setq s0 (match-string 0 ts)
6520 srp (and stamp (match-end 3))
6521 s1 (match-string (if plain 1 2) ts)
6522 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6524 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6525 ;; them, we might want to remove them there to avoid duplication.
6526 ;; The user can turn this off with a variable.
6527 (if (and org-prefix-has-time
6528 org-agenda-remove-times-when-in-prefix (or stamp plain)
6529 (string-match (concat (regexp-quote s0) " *") txt)
6530 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6531 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6532 (= (match-beginning 0) 0)
6534 (setq txt (replace-match "" nil nil txt))))
6535 ;; Normalize the time(s) to 24 hour
6536 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6537 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6539 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6540 (let (org-time-clocksum-use-effort-durations)
6541 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6542 (setq s2
6543 (org-minutes-to-clocksum-string
6544 (+ (org-hh:mm-string-to-minutes s1)
6545 org-agenda-default-appointment-duration)))))
6547 ;; Compute the duration
6548 (when s2
6549 (setq duration (- (org-hh:mm-string-to-minutes s2)
6550 (org-hh:mm-string-to-minutes s1)))))
6552 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6553 ;; Tags are in the string
6554 (if (or (eq org-agenda-remove-tags t)
6555 (and org-agenda-remove-tags
6556 org-prefix-has-tag))
6557 (setq txt (replace-match "" t t txt))
6558 (setq txt (replace-match
6559 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6560 (match-string 2 txt))
6561 t t txt))))
6563 (when remove-re
6564 (while (string-match remove-re txt)
6565 (setq txt (replace-match "" t t txt))))
6567 ;; Set org-heading property on `txt' to mark the start of the
6568 ;; heading.
6569 (add-text-properties 0 (length txt) '(org-heading t) txt)
6571 ;; Prepare the variables needed in the eval of the compiled format
6572 (if org-prefix-has-breadcrumbs
6573 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6574 (let ((s (org-display-outline-path nil nil "->" t)))
6575 (if (eq "" s) "" (concat s "->"))))))
6576 (setq time (cond (s2 (concat
6577 (org-agenda-time-of-day-to-ampm-maybe s1)
6578 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6579 (if org-agenda-timegrid-use-ampm " ")))
6580 (s1 (concat
6581 (org-agenda-time-of-day-to-ampm-maybe s1)
6582 (if org-agenda-timegrid-use-ampm
6583 "........ "
6584 "......")))
6585 (t ""))
6586 extra (or (and (not habitp) extra) "")
6587 category (if (symbolp category) (symbol-name category) category)
6588 level (or level ""))
6589 (if (string-match org-bracket-link-regexp category)
6590 (progn
6591 (setq l (if (match-end 3)
6592 (- (match-end 3) (match-beginning 3))
6593 (- (match-end 1) (match-beginning 1))))
6594 (when (< l (or org-prefix-category-length 0))
6595 (setq category (copy-sequence category))
6596 (org-add-props category nil
6597 'extra-space (make-string
6598 (- org-prefix-category-length l 1) ?\ ))))
6599 (if (and org-prefix-category-max-length
6600 (>= (length category) org-prefix-category-max-length))
6601 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6602 ;; Evaluate the compiled format
6603 (setq rtn (concat (eval formatter) txt))
6605 ;; And finally add the text properties
6606 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6607 (org-add-props rtn nil
6608 'org-category category
6609 'tags (mapcar 'org-downcase-keep-props tags)
6610 'org-highest-priority org-highest-priority
6611 'org-lowest-priority org-lowest-priority
6612 'time-of-day time-of-day
6613 'duration duration
6614 'breadcrumbs breadcrumbs
6615 'txt txt
6616 'level level
6617 'time time
6618 'extra extra
6619 'format org-prefix-format-compiled
6620 'dotime dotime)))))
6622 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6623 "Remove tags string from TXT, and add a modified list of tags.
6624 The modified list may contain inherited tags, and tags matched by
6625 `org-agenda-hide-tags-regexp' will be removed."
6626 (when (or add-inherited hide-re)
6627 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6628 (setq txt (substring txt 0 (match-beginning 0))))
6629 (setq tags
6630 (delq nil
6631 (mapcar (lambda (tg)
6632 (if (or (and hide-re (string-match hide-re tg))
6633 (and (not add-inherited)
6634 (get-text-property 0 'inherited tg)))
6636 tg))
6637 tags)))
6638 (when tags
6639 (let ((have-i (get-text-property 0 'inherited (car tags)))
6641 (setq txt (concat txt " :"
6642 (mapconcat
6643 (lambda (x)
6644 (setq i (get-text-property 0 'inherited x))
6645 (if (and have-i (not i))
6646 (progn
6647 (setq have-i nil)
6648 (concat ":" x))
6650 tags ":")
6651 (if have-i "::" ":"))))))
6652 txt)
6654 (defun org-downcase-keep-props (s)
6655 (let ((props (text-properties-at 0 s)))
6656 (setq s (downcase s))
6657 (add-text-properties 0 (length s) props s)
6660 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6662 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6663 "Add a time-grid for agenda items which need it.
6665 LIST is the list of agenda items formatted by `org-agenda-list'.
6666 NDAYS is the span of the current agenda view.
6667 TODAYP is t when the current agenda view is on today."
6668 (catch 'exit
6669 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6670 ((and todayp (member 'today (car org-agenda-time-grid))))
6671 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6672 ((member 'weekly (car org-agenda-time-grid)))
6673 (t (throw 'exit list)))
6674 (let* ((have (delq nil (mapcar
6675 (lambda (x) (get-text-property 1 'time-of-day x))
6676 list)))
6677 (string (nth 1 org-agenda-time-grid))
6678 (gridtimes (nth 2 org-agenda-time-grid))
6679 (req (car org-agenda-time-grid))
6680 (remove (member 'remove-match req))
6681 new time)
6682 (if (and (member 'require-timed req) (not have))
6683 ;; don't show empty grid
6684 (throw 'exit list))
6685 (while (setq time (pop gridtimes))
6686 (unless (and remove (member time have))
6687 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6688 (push (org-agenda-format-item
6689 nil string nil "" nil
6690 (concat (substring time 0 -2) ":" (substring time -2)))
6691 new)
6692 (put-text-property
6693 2 (length (car new)) 'face 'org-time-grid (car new))))
6694 (when (and todayp org-agenda-show-current-time-in-grid)
6695 (push (org-agenda-format-item
6696 nil org-agenda-current-time-string nil "" nil
6697 (format-time-string "%H:%M "))
6698 new)
6699 (put-text-property
6700 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6702 (if (member 'time-up org-agenda-sorting-strategy-selected)
6703 (append new list)
6704 (append list new)))))
6706 (defun org-compile-prefix-format (key)
6707 "Compile the prefix format into a Lisp form that can be evaluated.
6708 The resulting form and associated variable bindings is returned
6709 and stored in the variable `org-prefix-format-compiled'."
6710 (setq org-prefix-has-time nil
6711 org-prefix-has-tag nil
6712 org-prefix-category-length nil
6713 org-prefix-has-effort nil
6714 org-prefix-has-breadcrumbs nil)
6715 (let ((s (cond
6716 ((stringp org-agenda-prefix-format)
6717 org-agenda-prefix-format)
6718 ((assq key org-agenda-prefix-format)
6719 (cdr (assq key org-agenda-prefix-format)))
6720 (t " %-12:c%?-12t% s")))
6721 (start 0)
6722 varform vars var e c f opt)
6723 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6724 s start)
6725 (setq var (or (cdr (assoc (match-string 4 s)
6726 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6727 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6728 'eval)
6729 c (or (match-string 3 s) "")
6730 opt (match-beginning 1)
6731 start (1+ (match-beginning 0)))
6732 (if (eq var 'time) (setq org-prefix-has-time t))
6733 (if (eq var 'tag) (setq org-prefix-has-tag t))
6734 (if (eq var 'effort) (setq org-prefix-has-effort t))
6735 (if (eq var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6736 (setq f (concat "%" (match-string 2 s) "s"))
6737 (when (eq var 'category)
6738 (setq org-prefix-category-length
6739 (floor (abs (string-to-number (match-string 2 s)))))
6740 (setq org-prefix-category-max-length
6741 (let ((x (match-string 2 s)))
6742 (save-match-data
6743 (if (string-match "\\.[0-9]+" x)
6744 (string-to-number (substring (match-string 0 x) 1)))))))
6745 (if (eq var 'eval)
6746 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6747 (if opt
6748 (setq varform
6749 `(if (or (equal "" ,var) (equal nil ,var))
6751 (format ,f (concat ,var ,c))))
6752 (setq varform
6753 `(format ,f (if (or (equal ,var "")
6754 (equal ,var nil)) ""
6755 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6756 (setq s (replace-match "%s" t nil s))
6757 (push varform vars))
6758 (setq vars (nreverse vars))
6759 (with-current-buffer (or org-agenda-buffer (current-buffer))
6760 (setq org-prefix-format-compiled
6761 (list
6762 `((org-prefix-has-time ,org-prefix-has-time)
6763 (org-prefix-has-tag ,org-prefix-has-tag)
6764 (org-prefix-category-length ,org-prefix-category-length)
6765 (org-prefix-has-effort ,org-prefix-has-effort)
6766 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6767 `(format ,s ,@vars))))))
6769 (defun org-set-sorting-strategy (key)
6770 (if (symbolp (car org-agenda-sorting-strategy))
6771 ;; the old format
6772 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6773 (setq org-agenda-sorting-strategy-selected
6774 (or (cdr (assq key org-agenda-sorting-strategy))
6775 (cdr (assq 'agenda org-agenda-sorting-strategy))
6776 '(time-up category-keep priority-down)))))
6778 (defun org-get-time-of-day (s &optional string mod24)
6779 "Check string S for a time of day.
6780 If found, return it as a military time number between 0 and 2400.
6781 If not found, return nil.
6782 The optional STRING argument forces conversion into a 5 character wide string
6783 HH:MM."
6784 (save-match-data
6785 (when
6786 (and
6787 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6788 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6789 (not (eq (get-text-property 1 'face s) 'org-link)))
6790 (let* ((h (string-to-number (match-string 1 s)))
6791 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6792 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6793 (am-p (equal ampm "am"))
6794 (h1 (cond ((not ampm) h)
6795 ((= h 12) (if am-p 0 12))
6796 (t (+ h (if am-p 0 12)))))
6797 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6798 (mod h1 24) h1))
6799 (t0 (+ (* 100 h2) m))
6800 (t1 (concat (if (>= h1 24) "+" " ")
6801 (if (and org-agenda-time-leading-zero
6802 (< t0 1000)) "0" "")
6803 (if (< t0 100) "0" "")
6804 (if (< t0 10) "0" "")
6805 (int-to-string t0))))
6806 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6808 (defvar org-agenda-before-sorting-filter-function nil
6809 "Function to be applied to agenda items prior to sorting.
6810 Prior to sorting also means just before they are inserted into the agenda.
6812 To aid sorting, you may revisit the original entries and add more text
6813 properties which will later be used by the sorting functions.
6815 The function should take a string argument, an agenda line.
6816 It has access to the text properties in that line, which contain among
6817 other things, the property `org-hd-marker' that points to the entry
6818 where the line comes from. Note that not all lines going into the agenda
6819 have this property, only most.
6821 The function should return the modified string. It is probably best
6822 to ONLY change text properties.
6824 You can also use this function as a filter, by returning nil for lines
6825 you don't want to have in the agenda at all. For this application, you
6826 could bind the variable in the options section of a custom command.")
6828 (defun org-agenda-finalize-entries (list &optional type)
6829 "Sort, limit and concatenate the LIST of agenda items.
6830 The optional argument TYPE tells the agenda type."
6831 (let ((max-effort (cond ((listp org-agenda-max-effort)
6832 (cdr (assoc type org-agenda-max-effort)))
6833 (t org-agenda-max-effort)))
6834 (max-todo (cond ((listp org-agenda-max-todos)
6835 (cdr (assoc type org-agenda-max-todos)))
6836 (t org-agenda-max-todos)))
6837 (max-tags (cond ((listp org-agenda-max-tags)
6838 (cdr (assoc type org-agenda-max-tags)))
6839 (t org-agenda-max-tags)))
6840 (max-entries (cond ((listp org-agenda-max-entries)
6841 (cdr (assoc type org-agenda-max-entries)))
6842 (t org-agenda-max-entries))))
6843 (when org-agenda-before-sorting-filter-function
6844 (setq list
6845 (delq nil
6846 (mapcar
6847 org-agenda-before-sorting-filter-function list))))
6848 (setq list (mapcar 'org-agenda-highlight-todo list)
6849 list (mapcar 'identity (sort list 'org-entries-lessp)))
6850 (when max-effort
6851 (setq list (org-agenda-limit-entries
6852 list 'effort-minutes max-effort
6853 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6854 32767 -1))))))
6855 (when max-todo
6856 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6857 (when max-tags
6858 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6859 (when max-entries
6860 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6861 (mapconcat 'identity list "\n")))
6863 (defun org-agenda-limit-entries (list prop limit &optional fn)
6864 "Limit the number of agenda entries."
6865 (let ((include (and limit (< limit 0))))
6866 (if limit
6867 (let ((fun (or fn (lambda (p) (if p 1))))
6868 (lim 0))
6869 (delq nil
6870 (mapcar
6871 (lambda (e)
6872 (let ((pval (funcall
6873 fun (get-text-property (1- (length e))
6874 prop e))))
6875 (if pval (setq lim (+ lim pval)))
6876 (cond ((and pval (<= lim (abs limit))) e)
6877 ((and include (not pval)) e))))
6878 list)))
6879 list)))
6881 (defun org-agenda-limit-interactively (remove)
6882 "In agenda, interactively limit entries to various maximums."
6883 (interactive "P")
6884 (if remove
6885 (progn (setq org-agenda-max-entries nil
6886 org-agenda-max-todos nil
6887 org-agenda-max-tags nil
6888 org-agenda-max-effort nil)
6889 (org-agenda-redo))
6890 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6891 (msg (cond ((= max ?E) "How many minutes? ")
6892 ((= max ?e) "How many entries? ")
6893 ((= max ?t) "How many TODO entries? ")
6894 ((= max ?T) "How many tagged entries? ")
6895 (t (user-error "Wrong input"))))
6896 (num (string-to-number (read-from-minibuffer msg))))
6897 (cond ((equal max ?e)
6898 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6899 ((equal max ?t)
6900 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6901 ((equal max ?T)
6902 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6903 ((equal max ?E)
6904 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6905 (org-agenda-fit-window-to-buffer))
6907 (defun org-agenda-highlight-todo (x)
6908 (let ((org-done-keywords org-done-keywords-for-agenda)
6909 (case-fold-search nil)
6911 (if (eq x 'line)
6912 (save-excursion
6913 (beginning-of-line 1)
6914 (setq re (org-get-at-bol 'org-todo-regexp))
6915 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6916 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6917 (add-text-properties (match-beginning 0) (match-end 1)
6918 (list 'face (org-get-todo-face 1)))
6919 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6920 (delete-region (match-beginning 1) (1- (match-end 0)))
6921 (goto-char (match-beginning 1))
6922 (insert (format org-agenda-todo-keyword-format s)))))
6923 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6924 (setq re (get-text-property 0 'org-todo-regexp x))
6925 (when (and re
6926 ;; Test `pl' because if there's no heading content,
6927 ;; there's no point matching to highlight. Note
6928 ;; that if we didn't test `pl' first, and there
6929 ;; happened to be no keyword from `org-todo-regexp'
6930 ;; on this heading line, then the `equal' comparison
6931 ;; afterwards would spuriously succeed in the case
6932 ;; where `pl' is nil -- causing an args-out-of-range
6933 ;; error when we try to add text properties to text
6934 ;; that isn't there.
6936 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6937 x pl) pl))
6938 (add-text-properties
6939 (or (match-end 1) (match-end 0)) (match-end 0)
6940 (list 'face (org-get-todo-face (match-string 2 x)))
6942 (when (match-end 1)
6943 (setq x (concat (substring x 0 (match-end 1))
6944 (format org-agenda-todo-keyword-format
6945 (match-string 2 x))
6946 (org-add-props " " (text-properties-at 0 x))
6947 (substring x (match-end 3)))))))
6948 x)))
6950 (defsubst org-cmp-values (a b property)
6951 "Compare the numeric value of text PROPERTY for string A and B."
6952 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6953 (pb (or (get-text-property (1- (length b)) property b) 0)))
6954 (cond ((> pa pb) +1)
6955 ((< pa pb) -1))))
6957 (defsubst org-cmp-effort (a b)
6958 "Compare the effort values of string A and B."
6959 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6960 ;; `effort-minutes' property is not directly accessible from
6961 ;; the strings, but is stored as a property in `txt'.
6962 (ea (or (get-text-property
6963 0 'effort-minutes (get-text-property 0 'txt a))
6964 def))
6965 (eb (or (get-text-property
6966 0 'effort-minutes (get-text-property 0 'txt b))
6967 def)))
6968 (cond ((> ea eb) +1)
6969 ((< ea eb) -1))))
6971 (defsubst org-cmp-category (a b)
6972 "Compare the string values of categories of strings A and B."
6973 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6974 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6975 (cond ((string-lessp ca cb) -1)
6976 ((string-lessp cb ca) +1))))
6978 (defsubst org-cmp-todo-state (a b)
6979 "Compare the todo states of strings A and B."
6980 (let* ((ma (or (get-text-property 1 'org-marker a)
6981 (get-text-property 1 'org-hd-marker a)))
6982 (mb (or (get-text-property 1 'org-marker b)
6983 (get-text-property 1 'org-hd-marker b)))
6984 (fa (and ma (marker-buffer ma)))
6985 (fb (and mb (marker-buffer mb)))
6986 (todo-kwds
6987 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6988 (and fb (with-current-buffer fb org-todo-keywords-1))))
6989 (ta (or (get-text-property 1 'todo-state a) ""))
6990 (tb (or (get-text-property 1 'todo-state b) ""))
6991 (la (- (length (member ta todo-kwds))))
6992 (lb (- (length (member tb todo-kwds))))
6993 (donepa (member ta org-done-keywords-for-agenda))
6994 (donepb (member tb org-done-keywords-for-agenda)))
6995 (cond ((and donepa (not donepb)) -1)
6996 ((and (not donepa) donepb) +1)
6997 ((< la lb) -1)
6998 ((< lb la) +1))))
7000 (defsubst org-cmp-alpha (a b)
7001 "Compare the headlines, alphabetically."
7002 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
7003 (plb (text-property-any 0 (length b) 'org-heading t b))
7004 (ta (and pla (substring a pla)))
7005 (tb (and plb (substring b plb)))
7006 (case-fold-search nil))
7007 (when pla
7008 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
7009 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
7010 (setq ta (substring ta (match-end 0))))
7011 (setq ta (downcase ta)))
7012 (when plb
7013 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
7014 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
7015 (setq tb (substring tb (match-end 0))))
7016 (setq tb (downcase tb)))
7017 (cond ((not ta) +1)
7018 ((not tb) -1)
7019 ((string-lessp ta tb) -1)
7020 ((string-lessp tb ta) +1))))
7022 (defsubst org-cmp-tag (a b)
7023 "Compare the string values of the first tags of A and B."
7024 (let ((ta (car (last (get-text-property 1 'tags a))))
7025 (tb (car (last (get-text-property 1 'tags b)))))
7026 (cond ((not ta) +1)
7027 ((not tb) -1)
7028 ((string-lessp ta tb) -1)
7029 ((string-lessp tb ta) +1))))
7031 (defsubst org-cmp-time (a b)
7032 "Compare the time-of-day values of strings A and B."
7033 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7034 (ta (or (get-text-property 1 'time-of-day a) def))
7035 (tb (or (get-text-property 1 'time-of-day b) def)))
7036 (cond ((< ta tb) -1)
7037 ((< tb ta) +1))))
7039 (defsubst org-cmp-ts (a b type)
7040 "Compare the timestamps values of entries A and B.
7041 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7042 \"timestamp_ia\", compare within each of these type. When TYPE
7043 is the empty string, compare all timestamps without respect of
7044 their type."
7045 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7046 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7047 (get-text-property 1 'ts-date a))
7048 def))
7049 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7050 (get-text-property 1 'ts-date b))
7051 def)))
7052 (cond ((< ta tb) -1)
7053 ((< tb ta) +1))))
7055 (defsubst org-cmp-habit-p (a b)
7056 "Compare the todo states of strings A and B."
7057 (let ((ha (get-text-property 1 'org-habit-p a))
7058 (hb (get-text-property 1 'org-habit-p b)))
7059 (cond ((and ha (not hb)) -1)
7060 ((and (not ha) hb) +1))))
7062 (defun org-entries-lessp (a b)
7063 "Predicate for sorting agenda entries."
7064 ;; The following variables will be used when the form is evaluated.
7065 ;; So even though the compiler complains, keep them.
7066 (let* ((ss org-agenda-sorting-strategy-selected)
7067 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7068 (org-cmp-ts a b "")))
7069 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7070 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7071 (org-cmp-ts a b "scheduled")))
7072 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7073 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7074 (org-cmp-ts a b "deadline")))
7075 (deadline-down (if deadline-up (- deadline-up) nil))
7076 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7077 (org-cmp-ts a b "timestamp_ia")))
7078 (tsia-down (if tsia-up (- tsia-up) nil))
7079 (ts-up (and (org-em 'ts-up 'ts-down ss)
7080 (org-cmp-ts a b "timestamp")))
7081 (ts-down (if ts-up (- ts-up) nil))
7082 (time-up (and (org-em 'time-up 'time-down ss)
7083 (org-cmp-time a b)))
7084 (time-down (if time-up (- time-up) nil))
7085 (stats-up (and (org-em 'stats-up 'stats-down ss)
7086 (org-cmp-values a b 'org-stats)))
7087 (stats-down (if stats-up (- stats-up) nil))
7088 (priority-up (and (org-em 'priority-up 'priority-down ss)
7089 (org-cmp-values a b 'priority)))
7090 (priority-down (if priority-up (- priority-up) nil))
7091 (effort-up (and (org-em 'effort-up 'effort-down ss)
7092 (org-cmp-effort a b)))
7093 (effort-down (if effort-up (- effort-up) nil))
7094 (category-up (and (or (org-em 'category-up 'category-down ss)
7095 (memq 'category-keep ss))
7096 (org-cmp-category a b)))
7097 (category-down (if category-up (- category-up) nil))
7098 (category-keep (if category-up +1 nil))
7099 (tag-up (and (org-em 'tag-up 'tag-down ss)
7100 (org-cmp-tag a b)))
7101 (tag-down (if tag-up (- tag-up) nil))
7102 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7103 (org-cmp-todo-state a b)))
7104 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7105 (habit-up (and (org-em 'habit-up 'habit-down ss)
7106 (org-cmp-habit-p a b)))
7107 (habit-down (if habit-up (- habit-up) nil))
7108 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7109 (org-cmp-alpha a b)))
7110 (alpha-down (if alpha-up (- alpha-up) nil))
7111 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7112 user-defined-up user-defined-down)
7113 (if (and need-user-cmp org-agenda-cmp-user-defined
7114 (functionp org-agenda-cmp-user-defined))
7115 (setq user-defined-up
7116 (funcall org-agenda-cmp-user-defined a b)
7117 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7118 (cdr (assoc
7119 (eval (cons 'or org-agenda-sorting-strategy-selected))
7120 '((-1 . t) (1 . nil) (nil . nil))))))
7122 ;;; Agenda restriction lock
7124 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7125 "Overlay to mark the headline to which agenda commands are restricted.")
7126 (overlay-put org-agenda-restriction-lock-overlay
7127 'face 'org-agenda-restriction-lock)
7128 (overlay-put org-agenda-restriction-lock-overlay
7129 'help-echo "Agendas are currently limited to this subtree.")
7130 (delete-overlay org-agenda-restriction-lock-overlay)
7132 ;;;###autoload
7133 (defun org-agenda-set-restriction-lock (&optional type)
7134 "Set restriction lock for agenda, to current subtree or file.
7135 Restriction will be the file if TYPE is `file', or if type is the
7136 universal prefix \\='(4), or if the cursor is before the first headline
7137 in the file. Otherwise, restriction will be to the current subtree."
7138 (interactive "P")
7139 (org-agenda-remove-restriction-lock 'noupdate)
7140 (and (equal type '(4)) (setq type 'file))
7141 (setq type (cond
7142 (type type)
7143 ((org-at-heading-p) 'subtree)
7144 ((condition-case nil (org-back-to-heading t) (error nil))
7145 'subtree)
7146 (t 'file)))
7147 (if (eq type 'subtree)
7148 (progn
7149 (setq org-agenda-restrict (current-buffer))
7150 (setq org-agenda-overriding-restriction 'subtree)
7151 (put 'org-agenda-files 'org-restrict
7152 (list (buffer-file-name (buffer-base-buffer))))
7153 (org-back-to-heading t)
7154 (move-overlay org-agenda-restriction-lock-overlay
7155 (point)
7156 (if org-agenda-restriction-lock-highlight-subtree
7157 (save-excursion (org-end-of-subtree t t) (point))
7158 (point-at-eol)))
7159 (move-marker org-agenda-restrict-begin (point))
7160 (move-marker org-agenda-restrict-end
7161 (save-excursion (org-end-of-subtree t t)))
7162 (message "Locking agenda restriction to subtree"))
7163 (put 'org-agenda-files 'org-restrict
7164 (list (buffer-file-name (buffer-base-buffer))))
7165 (setq org-agenda-restrict nil)
7166 (setq org-agenda-overriding-restriction 'file)
7167 (move-marker org-agenda-restrict-begin nil)
7168 (move-marker org-agenda-restrict-end nil)
7169 (message "Locking agenda restriction to file"))
7170 (setq current-prefix-arg nil)
7171 (org-agenda-maybe-redo))
7173 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7174 "Remove the agenda restriction lock."
7175 (interactive "P")
7176 (delete-overlay org-agenda-restriction-lock-overlay)
7177 (delete-overlay org-speedbar-restriction-lock-overlay)
7178 (setq org-agenda-overriding-restriction nil)
7179 (setq org-agenda-restrict nil)
7180 (put 'org-agenda-files 'org-restrict nil)
7181 (move-marker org-agenda-restrict-begin nil)
7182 (move-marker org-agenda-restrict-end nil)
7183 (setq current-prefix-arg nil)
7184 (message "Agenda restriction lock removed")
7185 (or noupdate (org-agenda-maybe-redo)))
7187 (defun org-agenda-maybe-redo ()
7188 "If there is any window showing the agenda view, update it."
7189 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7190 org-agenda-buffer-name)
7192 (w0 (selected-window)))
7193 (when w
7194 (select-window w)
7195 (org-agenda-redo)
7196 (select-window w0)
7197 (if org-agenda-overriding-restriction
7198 (message "Agenda view shifted to new %s restriction"
7199 org-agenda-overriding-restriction)
7200 (message "Agenda restriction lock removed")))))
7202 ;;; Agenda commands
7204 (defun org-agenda-check-type (error &rest types)
7205 "Check if agenda buffer is of allowed type.
7206 If ERROR is non-nil, throw an error, otherwise just return nil.
7207 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7208 (if (not org-agenda-type)
7209 (error "No Org agenda currently displayed")
7210 (if (memq org-agenda-type types)
7212 (if error
7213 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7214 nil))))
7216 (defun org-agenda-Quit ()
7217 "Exit the agenda, killing the agenda buffer.
7218 Like `org-agenda-quit', but kill the buffer even when
7219 `org-agenda-sticky' is non-nil."
7220 (interactive)
7221 (org-agenda--quit))
7223 (defun org-agenda-quit ()
7224 "Exit the agenda.
7226 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7227 instead of killing it.
7229 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7230 the pre-agenda window configuration.
7232 When column view is active, exit column view instead of the
7233 agenda."
7234 (interactive)
7235 (org-agenda--quit org-agenda-sticky))
7237 (defun org-agenda--quit (&optional bury)
7238 (if org-agenda-columns-active
7239 (org-columns-quit)
7240 (let ((wconf org-agenda-pre-window-conf)
7241 (buf (current-buffer))
7242 (org-agenda-last-indirect-window
7243 (and (eq org-indirect-buffer-display 'other-window)
7244 org-agenda-last-indirect-buffer
7245 (get-buffer-window org-agenda-last-indirect-buffer))))
7246 (cond
7247 ((eq org-agenda-window-setup 'other-frame)
7248 (delete-frame))
7249 ((and org-agenda-restore-windows-after-quit
7250 wconf)
7251 ;; Maybe restore the pre-agenda window configuration. Reset
7252 ;; `org-agenda-pre-window-conf' before running
7253 ;; `set-window-configuration', which loses the current buffer.
7254 (setq org-agenda-pre-window-conf nil)
7255 (set-window-configuration wconf))
7257 (when org-agenda-last-indirect-window
7258 (delete-window org-agenda-last-indirect-window))
7259 (and (not (eq org-agenda-window-setup 'current-window))
7260 (not (one-window-p))
7261 (delete-window))))
7262 (if bury
7263 ;; Set the agenda buffer as the current buffer instead of
7264 ;; passing it as an argument to `bury-buffer' so that
7265 ;; `bury-buffer' removes it from the window.
7266 (with-current-buffer buf
7267 (bury-buffer))
7268 (kill-buffer buf)
7269 (setq org-agenda-archives-mode nil
7270 org-agenda-buffer nil)))))
7272 (defun org-agenda-exit ()
7273 "Exit the agenda, killing Org buffers loaded by the agenda.
7274 Like `org-agenda-Quit', but kill any buffers that were created by
7275 the agenda. Org buffers visited directly by the user will not be
7276 touched. Also, exit the agenda even if it is in column view."
7277 (interactive)
7278 (when org-agenda-columns-active
7279 (org-columns-quit))
7280 (org-release-buffers org-agenda-new-buffers)
7281 (setq org-agenda-new-buffers nil)
7282 (org-agenda-Quit))
7284 (defun org-agenda-kill-all-agenda-buffers ()
7285 "Kill all buffers in `org-agenda-mode'.
7286 This is used when toggling sticky agendas."
7287 (interactive)
7288 (let (blist)
7289 (dolist (buf (buffer-list))
7290 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7291 (push buf blist)))
7292 (mapc 'kill-buffer blist)))
7294 (defun org-agenda-execute (arg)
7295 "Execute another agenda command, keeping same window.
7296 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7297 in the agenda."
7298 (interactive "P")
7299 (let ((org-agenda-window-setup 'current-window))
7300 (org-agenda arg)))
7302 (defun org-agenda-redo (&optional all)
7303 "Rebuild possibly ALL agenda view(s) in the current buffer."
7304 (interactive "P")
7305 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7306 (cpa (unless (eq all t) current-prefix-arg))
7307 (org-agenda-doing-sticky-redo org-agenda-sticky)
7308 (org-agenda-sticky nil)
7309 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7310 org-agenda-buffer-name))
7311 (org-agenda-keep-modes t)
7312 (tag-filter org-agenda-tag-filter)
7313 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7314 (top-hl-filter org-agenda-top-headline-filter)
7315 (cat-filter org-agenda-category-filter)
7316 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7317 (re-filter org-agenda-regexp-filter)
7318 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7319 (effort-filter org-agenda-effort-filter)
7320 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7321 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7322 (cols org-agenda-columns-active)
7323 (line (org-current-line))
7324 (window-line (- line (org-current-line (window-start))))
7325 (lprops (get 'org-agenda-redo-command 'org-lprops))
7326 (redo-cmd (get-text-property p 'org-redo-cmd))
7327 (last-args (get-text-property p 'org-last-args))
7328 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7329 (org-agenda-overriding-cmd-arguments
7330 (unless (eq all t)
7331 (cond ((listp last-args)
7332 (cons (or cpa (car last-args)) (cdr last-args)))
7333 ((stringp last-args)
7334 last-args))))
7335 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7336 (put 'org-agenda-tag-filter :preset-filter nil)
7337 (put 'org-agenda-category-filter :preset-filter nil)
7338 (put 'org-agenda-regexp-filter :preset-filter nil)
7339 (put 'org-agenda-effort-filter :preset-filter nil)
7340 (and cols (org-columns-quit))
7341 (message "Rebuilding agenda buffer...")
7342 (if series-redo-cmd
7343 (eval series-redo-cmd)
7344 (org-let lprops redo-cmd))
7345 (setq org-agenda-undo-list nil
7346 org-agenda-pending-undo-list nil
7347 org-agenda-tag-filter tag-filter
7348 org-agenda-category-filter cat-filter
7349 org-agenda-regexp-filter re-filter
7350 org-agenda-effort-filter effort-filter
7351 org-agenda-top-headline-filter top-hl-filter)
7352 (message "Rebuilding agenda buffer...done")
7353 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7354 (put 'org-agenda-category-filter :preset-filter cat-preset)
7355 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7356 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7357 (let ((tag (or tag-filter tag-preset))
7358 (cat (or cat-filter cat-preset))
7359 (effort (or effort-filter effort-preset))
7360 (re (or re-filter re-preset)))
7361 (when tag (org-agenda-filter-apply tag 'tag t))
7362 (when cat (org-agenda-filter-apply cat 'category))
7363 (when effort (org-agenda-filter-apply effort 'effort))
7364 (when re (org-agenda-filter-apply re 'regexp)))
7365 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7366 (and cols (called-interactively-p 'any) (org-agenda-columns))
7367 (org-goto-line line)
7368 (recenter window-line)))
7370 (defvar org-global-tags-completion-table nil)
7371 (defvar org-agenda-filter-form nil)
7372 (defvar org-agenda-filtered-by-category nil)
7374 (defun org-agenda-filter-by-category (strip)
7375 "Filter lines in the agenda buffer that have a specific category.
7376 The category is that of the current line.
7377 Without prefix argument, keep only the lines of that category.
7378 With a prefix argument, exclude the lines of that category.
7380 (interactive "P")
7381 (if (and org-agenda-filtered-by-category
7382 org-agenda-category-filter)
7383 (org-agenda-filter-show-all-cat)
7384 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7385 (cond
7386 ((and cat strip)
7387 (org-agenda-filter-apply
7388 (push (concat "-" cat) org-agenda-category-filter) 'category))
7389 (cat
7390 (org-agenda-filter-apply
7391 (setq org-agenda-category-filter
7392 (list (concat "+" cat))) 'category))
7393 (t (error "No category at point"))))))
7395 (defun org-find-top-headline (&optional pos)
7396 "Find the topmost parent headline and return it."
7397 (save-excursion
7398 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7399 (if pos (goto-char pos))
7400 ;; Skip up to the topmost parent
7401 (while (ignore-errors (outline-up-heading 1) t))
7402 (ignore-errors
7403 (nth 4 (org-heading-components))))))
7405 (defvar org-agenda-filtered-by-top-headline nil)
7406 (defun org-agenda-filter-by-top-headline (strip)
7407 "Keep only those lines that are descendants from the same top headline.
7408 The top headline is that of the current line."
7409 (interactive "P")
7410 (if org-agenda-filtered-by-top-headline
7411 (progn
7412 (setq org-agenda-filtered-by-top-headline nil
7413 org-agenda-top-headline-filter nil)
7414 (org-agenda-filter-show-all-top-filter))
7415 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7416 (if toph (org-agenda-filter-top-headline-apply toph strip)
7417 (error "No top-level headline at point")))))
7419 (defvar org-agenda-regexp-filter nil)
7420 (defun org-agenda-filter-by-regexp (strip)
7421 "Filter agenda entries by a regular expression.
7422 Regexp filters are cumulative.
7423 With no prefix argument, keep entries matching the regexp.
7424 With one prefix argument, filter out entries matching the regexp.
7425 With two prefix arguments, remove the regexp filters."
7426 (interactive "P")
7427 (if (not (equal strip '(16)))
7428 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7429 (read-from-minibuffer
7430 (if (equal strip '(4))
7431 "Filter out entries matching regexp: "
7432 "Narrow to entries matching regexp: ")))))
7433 (push flt org-agenda-regexp-filter)
7434 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7435 (org-agenda-filter-show-all-re)
7436 (message "Regexp filter removed")))
7438 (defvar org-agenda-effort-filter nil)
7439 (defun org-agenda-filter-by-effort (strip)
7440 "Filter agenda entries by effort.
7441 With no prefix argument, keep entries matching the effort condition.
7442 With one prefix argument, filter out entries matching the condition.
7443 With two prefix arguments, remove the effort filters."
7444 (interactive "P")
7445 (cond ((member strip '(nil 4))
7446 (let ((efforts (org-split-string
7447 (or (cdr (assoc (concat org-effort-property "_ALL")
7448 org-global-properties))
7449 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7450 "")))
7451 (eff -1)
7452 effort-prompt op)
7453 (while (not (member op '(?< ?> ?=)))
7454 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7455 (cl-loop for i from 0 to 9 do
7456 (setq effort-prompt
7457 (concat
7458 effort-prompt " ["
7459 (if (= i 9) "0" (int-to-string (1+ i)))
7460 "]" (nth i efforts))))
7461 (message "Effort %s%s" (char-to-string op) effort-prompt)
7462 (while (or (< eff 0) (> eff 9))
7463 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7464 (setq org-agenda-effort-filter
7465 (list (concat (if strip "-" "+")
7466 (char-to-string op) (nth (1- eff) efforts))))
7467 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7468 (t (org-agenda-filter-show-all-effort)
7469 (message "Effort filter removed"))))
7471 (defun org-agenda-filter-remove-all ()
7472 "Remove all filters from the current agenda buffer."
7473 (interactive)
7474 (when org-agenda-tag-filter
7475 (org-agenda-filter-show-all-tag))
7476 (when org-agenda-category-filter
7477 (org-agenda-filter-show-all-cat))
7478 (when org-agenda-regexp-filter
7479 (org-agenda-filter-show-all-re))
7480 (when org-agenda-top-headline-filter
7481 (org-agenda-filter-show-all-top-filter))
7482 (when org-agenda-effort-filter
7483 (org-agenda-filter-show-all-effort))
7484 (org-agenda-finalize))
7486 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7487 "Keep only those lines in the agenda buffer that have a specific tag.
7489 The tag is selected with its fast selection letter, as configured.
7491 With a `\\[universal-argument]' prefix, exclude the agenda search.
7493 With a `\\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
7494 i.e. don't
7495 filter on all its group members.
7497 A lisp caller can specify CHAR. EXCLUDE means that the new tag
7498 should be used to exclude the search - the interactive user can
7499 also press `-' or `+' to switch between filtering and excluding."
7500 (interactive "P")
7501 (let* ((alist org-tag-alist-for-agenda)
7502 (tag-chars (mapconcat
7503 (lambda (x) (if (and (not (symbolp (car x)))
7504 (cdr x))
7505 (char-to-string (cdr x))
7506 ""))
7507 org-tag-alist-for-agenda ""))
7508 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7509 (string-to-list tag-chars)))
7510 (exclude (or exclude (equal arg '(4))))
7511 (expand (not (equal arg '(16))))
7512 (inhibit-read-only t)
7513 (current org-agenda-tag-filter)
7514 a n tag)
7515 (unless char
7516 (while (not (memq char valid-char-list))
7517 (message
7518 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7519 (if exclude "Exclude" "Filter") tag-chars
7520 (if org-agenda-auto-exclude-function "[RET], " "")
7521 (if expand "" ", no grouptag expand"))
7522 (setq char (read-char-exclusive))
7523 ;; Excluding or filtering down
7524 (cond ((eq char ?-) (setq exclude t))
7525 ((eq char ?+) (setq exclude nil)))))
7526 (when (eq char ?\t)
7527 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7528 (setq-local org-global-tags-completion-table
7529 (org-global-tags-completion-table)))
7530 (let ((completion-ignore-case t))
7531 (setq tag (completing-read
7532 "Tag: " org-global-tags-completion-table))))
7533 (cond
7534 ((eq char ?\r)
7535 (org-agenda-filter-show-all-tag)
7536 (when org-agenda-auto-exclude-function
7537 (setq org-agenda-tag-filter nil)
7538 (dolist (tag (org-agenda-get-represented-tags))
7539 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7540 (if modifier
7541 (push modifier org-agenda-tag-filter))))
7542 (if (not (null org-agenda-tag-filter))
7543 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7544 ((eq char ?/)
7545 (org-agenda-filter-show-all-tag)
7546 (when (get 'org-agenda-tag-filter :preset-filter)
7547 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7548 ((eq char ?.)
7549 (setq org-agenda-tag-filter
7550 (mapcar (lambda(tag) (concat "+" tag))
7551 (org-get-at-bol 'tags)))
7552 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7553 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7554 ((or (eq char ?\s)
7555 (setq a (rassoc char alist))
7556 (and tag (setq a (cons tag nil))))
7557 (org-agenda-filter-show-all-tag)
7558 (setq tag (car a))
7559 (setq org-agenda-tag-filter
7560 (cons (concat (if exclude "-" "+") tag)
7561 current))
7562 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7563 (t (error "Invalid tag selection character %c" char)))))
7565 (defun org-agenda-get-represented-tags ()
7566 "Get a list of all tags currently represented in the agenda."
7567 (let (p tags)
7568 (save-excursion
7569 (goto-char (point-min))
7570 (while (setq p (next-single-property-change (point) 'tags))
7571 (goto-char p)
7572 (mapc (lambda (x) (add-to-list 'tags x))
7573 (get-text-property (point) 'tags))))
7574 tags))
7577 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7578 "Create the form that tests a line for agenda filter. Optional
7579 argument EXPAND can be used for the TYPE tag and will expand the
7580 tags in the FILTER if any of the tags in FILTER are grouptags."
7581 (let (f f1)
7582 (cond
7583 ;; Tag filter
7584 ((eq type 'tag)
7585 (setq filter
7586 (delete-dups
7587 (append (get 'org-agenda-tag-filter :preset-filter)
7588 filter)))
7589 (dolist (x filter)
7590 (let ((op (string-to-char x)))
7591 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7592 (setq x (list x)))
7593 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7594 (push f1 f))))
7595 ;; Category filter
7596 ((eq type 'category)
7597 (setq filter
7598 (delete-dups
7599 (append (get 'org-agenda-category-filter :preset-filter)
7600 filter)))
7601 (dolist (x filter)
7602 (if (equal "-" (substring x 0 1))
7603 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7604 (setq f1 (list 'equal (substring x 1) 'cat)))
7605 (push f1 f)))
7606 ;; Regexp filter
7607 ((eq type 'regexp)
7608 (setq filter
7609 (delete-dups
7610 (append (get 'org-agenda-regexp-filter :preset-filter)
7611 filter)))
7612 (dolist (x filter)
7613 (if (equal "-" (substring x 0 1))
7614 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7615 (setq f1 (list 'string-match (substring x 1) 'txt)))
7616 (push f1 f)))
7617 ;; Effort filter
7618 ((eq type 'effort)
7619 (setq filter
7620 (delete-dups
7621 (append (get 'org-agenda-effort-filter :preset-filter)
7622 filter)))
7623 (dolist (x filter)
7624 (push (org-agenda-filter-effort-form x) f))))
7625 (cons 'and (nreverse f))))
7627 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7628 "Return a form associated to tag-expression TAGS.
7629 Build a form testing a line for agenda filter for
7630 tag-expressions. OP is an operator of type CHAR that allows the
7631 function to set the right switches in the returned form."
7632 (let (form)
7633 ;; Any of the expressions can match if OP is +, all must match if
7634 ;; the operator is -.
7635 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7636 (let* ((tag (substring x 1))
7637 (f (cond
7638 ((string= "" tag) '(not tags))
7639 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7640 ;; TAG is a regexp.
7641 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7642 (t (list 'member (downcase tag) 'tags)))))
7643 (push (if (eq op ?-) (list 'not f) f) form)))))
7645 (defun org-agenda-filter-effort-form (e)
7646 "Return the form to compare the effort of the current line with what E says.
7647 E looks like \"+<2:25\"."
7648 (let (op)
7649 (setq e (substring e 1))
7650 (setq op (string-to-char e) e (substring e 1))
7651 (setq op (cond ((equal op ?<) '<=)
7652 ((equal op ?>) '>=)
7653 ((equal op ??) op)
7654 (t '=)))
7655 (list 'org-agenda-compare-effort (list 'quote op)
7656 (org-duration-string-to-minutes e))))
7658 (defun org-agenda-compare-effort (op value)
7659 "Compare the effort of the current line with VALUE, using OP.
7660 If the line does not have an effort defined, return nil."
7661 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7662 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7663 value)))
7665 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7666 "Expand group tags in FILTER for the agenda.
7667 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7668 (if org-group-tags
7669 (let ((case-fold-search t) rtn)
7670 (mapc
7671 (lambda (f)
7672 (let (f0 dir)
7673 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7674 (setq dir (match-string 1 f) f0 (match-string 2 f))
7675 (setq dir (if no-operator "" "+") f0 f))
7676 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7677 (org-tags-expand f0 t t))
7678 rtn))))
7679 filter)
7680 (reverse rtn))
7681 filter))
7683 (defun org-agenda-filter-apply (filter type &optional expand)
7684 "Set FILTER as the new agenda filter and apply it. Optional
7685 argument EXPAND can be used for the TYPE tag and will expand the
7686 tags in the FILTER if any of the tags in FILTER are grouptags."
7687 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7688 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7689 (let (tags cat txt)
7690 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7691 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7692 ;; category is used as the filter:
7693 (setq org-agenda-filtered-by-category
7694 (and (eq type 'category)
7695 (not (equal (substring (car filter) 0 1) "-"))))
7696 (org-agenda-set-mode-name)
7697 (save-excursion
7698 (goto-char (point-min))
7699 (while (not (eobp))
7700 (if (org-get-at-bol 'org-marker)
7701 (progn
7702 (setq tags (org-get-at-bol 'tags)
7703 cat (org-get-at-eol 'org-category 1)
7704 txt (org-get-at-bol 'txt))
7705 (if (not (eval org-agenda-filter-form))
7706 (org-agenda-filter-hide-line type))
7707 (beginning-of-line 2))
7708 (beginning-of-line 2))))
7709 (if (get-char-property (point) 'invisible)
7710 (ignore-errors (org-agenda-previous-line)))))
7712 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7713 "Filter by top headline HL."
7714 (org-agenda-set-mode-name)
7715 (save-excursion
7716 (goto-char (point-min))
7717 (while (not (eobp))
7718 (let* ((pos (org-get-at-bol 'org-hd-marker))
7719 (tophl (and pos (org-find-top-headline pos))))
7720 (if (and tophl (funcall (if negative 'identity 'not)
7721 (string= hl tophl)))
7722 (org-agenda-filter-hide-line 'top-headline)))
7723 (beginning-of-line 2)))
7724 (if (get-char-property (point) 'invisible)
7725 (org-agenda-previous-line))
7726 (setq org-agenda-top-headline-filter hl
7727 org-agenda-filtered-by-top-headline t))
7729 (defun org-agenda-filter-hide-line (type)
7730 "Hide lines with TYPE in the agenda buffer."
7731 (let* ((b (max (point-min) (1- (point-at-bol))))
7732 (e (point-at-eol)))
7733 (let ((inhibit-read-only t))
7734 (add-text-properties
7735 b e `(invisible org-filtered org-filter-type ,type)))))
7737 (defun org-agenda-remove-filter (type)
7738 (interactive)
7739 "Remove filter of type TYPE from the agenda buffer."
7740 (save-excursion
7741 (goto-char (point-min))
7742 (let ((inhibit-read-only t) pos)
7743 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7744 (goto-char pos)
7745 (remove-text-properties
7746 (point) (next-single-property-change (point) 'org-filter-type)
7747 `(invisible org-filtered org-filter-type ,type))))
7748 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7749 (setq org-agenda-filter-form nil)
7750 (org-agenda-set-mode-name)
7751 (org-agenda-finalize)))
7753 (defun org-agenda-filter-show-all-tag nil
7754 (org-agenda-remove-filter 'tag))
7755 (defun org-agenda-filter-show-all-re nil
7756 (org-agenda-remove-filter 'regexp))
7757 (defun org-agenda-filter-show-all-effort nil
7758 (org-agenda-remove-filter 'effort))
7759 (defun org-agenda-filter-show-all-cat nil
7760 (org-agenda-remove-filter 'category))
7761 (defun org-agenda-filter-show-all-top-filter nil
7762 (org-agenda-remove-filter 'top-headline))
7764 (defun org-agenda-manipulate-query-add ()
7765 "Manipulate the query by adding a search term with positive selection.
7766 Positive selection means the term must be matched for selection of an entry."
7767 (interactive)
7768 (org-agenda-manipulate-query ?\[))
7769 (defun org-agenda-manipulate-query-subtract ()
7770 "Manipulate the query by adding a search term with negative selection.
7771 Negative selection means term must not be matched for selection of an entry."
7772 (interactive)
7773 (org-agenda-manipulate-query ?\]))
7774 (defun org-agenda-manipulate-query-add-re ()
7775 "Manipulate the query by adding a search regexp with positive selection.
7776 Positive selection means the regexp must match for selection of an entry."
7777 (interactive)
7778 (org-agenda-manipulate-query ?\{))
7779 (defun org-agenda-manipulate-query-subtract-re ()
7780 "Manipulate the query by adding a search regexp with negative selection.
7781 Negative selection means regexp must not match for selection of an entry."
7782 (interactive)
7783 (org-agenda-manipulate-query ?\}))
7784 (defun org-agenda-manipulate-query (char)
7785 (cond
7786 ((memq org-agenda-type '(timeline agenda))
7787 (let ((org-agenda-include-inactive-timestamps t))
7788 (org-agenda-redo))
7789 (message "Display now includes inactive timestamps as well"))
7790 ((eq org-agenda-type 'search)
7791 (org-add-to-string
7792 'org-agenda-query-string
7793 (if org-agenda-last-search-view-search-was-boolean
7794 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7795 (?\{ . " +{}") (?\} . " -{}"))))
7796 " "))
7797 (setq org-agenda-redo-command
7798 (list 'org-search-view
7799 (car (get-text-property (min (1- (point-max)) (point))
7800 'org-last-args))
7801 org-agenda-query-string
7802 (+ (length org-agenda-query-string)
7803 (if (member char '(?\{ ?\})) 0 1))))
7804 (set-register org-agenda-query-register org-agenda-query-string)
7805 (let ((org-agenda-overriding-arguments
7806 (cdr org-agenda-redo-command)))
7807 (org-agenda-redo)))
7808 (t (error "Cannot manipulate query for %s-type agenda buffers"
7809 org-agenda-type))))
7811 (defun org-add-to-string (var string)
7812 (set var (concat (symbol-value var) string)))
7814 (defun org-agenda-goto-date (span)
7815 "Jump to DATE in agenda."
7816 (interactive "P")
7817 (let* ((org-read-date-prefer-future
7818 (eval org-agenda-jump-prefer-future))
7819 (date (org-read-date))
7820 (day (time-to-days (org-time-string-to-time date)))
7821 (org-agenda-sticky-orig org-agenda-sticky)
7822 (org-agenda-buffer-tmp-name (buffer-name))
7823 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7824 (0-arg (or current-prefix-arg (car args)))
7825 (2-arg (nth 2 args))
7826 (with-hour-p (nth 4 org-agenda-redo-command))
7827 (newcmd (list 'org-agenda-list 0-arg date
7828 (org-agenda-span-to-ndays
7829 2-arg (org-time-string-to-absolute date))
7830 with-hour-p))
7831 (newargs (cdr newcmd))
7832 (inhibit-read-only t)
7833 org-agenda-sticky)
7834 (if (not (org-agenda-check-type t 'agenda))
7835 (error "Not available in non-agenda views")
7836 (add-text-properties (point-min) (point-max)
7837 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7838 (org-agenda-redo)
7839 (goto-char (point-min))
7840 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7841 (save-excursion (move-beginning-of-line 2) (eobp))))
7842 (move-beginning-of-line 2))
7843 (setq org-agenda-sticky org-agenda-sticky-orig
7844 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7846 (defun org-agenda-goto-today ()
7847 "Go to today."
7848 (interactive)
7849 (org-agenda-check-type t 'timeline 'agenda)
7850 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7851 (curspan (nth 2 args))
7852 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7853 (cond
7854 (tdpos (goto-char tdpos))
7855 ((eq org-agenda-type 'agenda)
7856 (let* ((sd (org-agenda-compute-starting-span
7857 (org-today) (or curspan org-agenda-span)))
7858 (org-agenda-overriding-arguments args))
7859 (setf (nth 1 org-agenda-overriding-arguments) sd)
7860 (org-agenda-redo)
7861 (org-agenda-find-same-or-today-or-agenda)))
7862 (t (error "Cannot find today")))))
7864 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7865 (goto-char
7866 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7867 (text-property-any (point-min) (point-max) 'org-today t)
7868 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7869 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7870 (org-agenda-backward-block))
7871 (point-min))))
7873 (defun org-agenda-backward-block ()
7874 "Move backward by one agenda block."
7875 (interactive)
7876 (org-agenda-forward-block 'backward))
7878 (defun org-agenda-forward-block (&optional backward)
7879 "Move forward by one agenda block.
7880 When optional argument BACKWARD is set, go backward"
7881 (interactive)
7882 (cond ((not (derived-mode-p 'org-agenda-mode))
7883 (user-error
7884 "Cannot execute this command outside of org-agenda-mode buffers"))
7885 ((looking-at (if backward "\\`" "\\'"))
7886 (message "Already at the %s block" (if backward "first" "last")))
7887 (t (let ((pos (prog1 (point)
7888 (ignore-errors (if backward (backward-char 1)
7889 (move-end-of-line 1)))))
7890 (f (if backward
7891 'previous-single-property-change
7892 'next-single-property-change))
7893 moved dest)
7894 (while (and (setq dest (funcall
7895 f (point) 'org-agenda-structural-header))
7896 (not (get-text-property
7897 (point) 'org-agenda-structural-header)))
7898 (setq moved t)
7899 (goto-char dest))
7900 (if moved (move-beginning-of-line 1)
7901 (goto-char (if backward (point-min) (point-max)))
7902 (move-beginning-of-line 1)
7903 (message "No %s block" (if backward "previous" "further")))))))
7905 (defun org-agenda-later (arg)
7906 "Go forward in time by the current span.
7907 With prefix ARG, go forward that many times the current span."
7908 (interactive "p")
7909 (org-agenda-check-type t 'agenda)
7910 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7911 (span (or (nth 2 args) org-agenda-current-span))
7912 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7913 (greg (calendar-gregorian-from-absolute sd))
7914 (cnt (org-get-at-bol 'org-day-cnt))
7915 greg2)
7916 (cond
7917 ((numberp span)
7918 (setq sd (+ (* span arg) sd)))
7919 ((eq span 'day)
7920 (setq sd (+ arg sd)))
7921 ((eq span 'week)
7922 (setq sd (+ (* 7 arg) sd)))
7923 ((eq span 'fortnight)
7924 (setq sd (+ (* 14 arg) sd)))
7925 ((eq span 'month)
7926 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7927 sd (calendar-absolute-from-gregorian greg2))
7928 (setcar greg2 (1+ (car greg2))))
7929 ((eq span 'year)
7930 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7931 sd (calendar-absolute-from-gregorian greg2))
7932 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7934 (setq sd (+ (* span arg) sd))))
7935 (let ((org-agenda-overriding-cmd
7936 ;; `cmd' may have been set by `org-agenda-run-series' which
7937 ;; uses `org-agenda-overriding-cmd' to decide whether
7938 ;; overriding is allowed for `cmd'
7939 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7940 (org-agenda-overriding-arguments
7941 (list (car args) sd span)))
7942 (org-agenda-redo)
7943 (org-agenda-find-same-or-today-or-agenda cnt))))
7945 (defun org-agenda-earlier (arg)
7946 "Go backward in time by the current span.
7947 With prefix ARG, go backward that many times the current span."
7948 (interactive "p")
7949 (org-agenda-later (- arg)))
7951 (defun org-agenda-view-mode-dispatch ()
7952 "Call one of the view mode commands."
7953 (interactive)
7954 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7955 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7956 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7957 (pcase (read-char-exclusive)
7958 (?\ (call-interactively 'org-agenda-reset-view))
7959 (?d (call-interactively 'org-agenda-day-view))
7960 (?w (call-interactively 'org-agenda-week-view))
7961 (?t (call-interactively 'org-agenda-fortnight-view))
7962 (?m (call-interactively 'org-agenda-month-view))
7963 (?y (call-interactively 'org-agenda-year-view))
7964 (?l (call-interactively 'org-agenda-log-mode))
7965 (?L (org-agenda-log-mode '(4)))
7966 (?c (org-agenda-log-mode 'clockcheck))
7967 ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
7968 (?a (call-interactively 'org-agenda-archives-mode))
7969 (?A (org-agenda-archives-mode 'files))
7970 ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7971 ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7972 (?G (call-interactively 'org-agenda-toggle-time-grid))
7973 (?D (call-interactively 'org-agenda-toggle-diary))
7974 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7975 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7976 (org-agenda-check-type t 'timeline 'agenda)
7977 (org-agenda-redo))
7978 (message "Display now includes inactive timestamps as well"))
7979 (?q (message "Abort"))
7980 (key (user-error "Invalid key: %s" key))))
7982 (defun org-agenda-reset-view ()
7983 "Switch to default view for agenda."
7984 (interactive)
7985 (org-agenda-change-time-span org-agenda-span))
7987 (defun org-agenda-day-view (&optional day-of-month)
7988 "Switch to daily view for agenda.
7989 With argument DAY-OF-MONTH, switch to that day of the month."
7990 (interactive "P")
7991 (org-agenda-change-time-span 'day day-of-month))
7993 (defun org-agenda-week-view (&optional iso-week)
7994 "Switch to weekly view for agenda.
7995 With argument ISO-WEEK, switch to the corresponding ISO week.
7996 If ISO-WEEK has more then 2 digits, only the last two encode
7997 the week. Any digits before this encode a year. So 200712
7998 means week 12 of year 2007. Years ranging from 70 years ago
7999 to 30 years in the future can also be written as 2-digit years."
8000 (interactive "P")
8001 (org-agenda-change-time-span 'week iso-week))
8003 (defun org-agenda-fortnight-view (&optional iso-week)
8004 "Switch to fortnightly view for agenda.
8005 With argument ISO-WEEK, switch to the corresponding ISO week.
8006 If ISO-WEEK has more then 2 digits, only the last two encode
8007 the week. Any digits before this encode a year. So 200712
8008 means week 12 of year 2007. Years ranging from 70 years ago
8009 to 30 years in the future can also be written as 2-digit years."
8010 (interactive "P")
8011 (org-agenda-change-time-span 'fortnight iso-week))
8013 (defun org-agenda-month-view (&optional month)
8014 "Switch to monthly view for agenda.
8015 With argument MONTH, switch to that month. If MONTH has more
8016 then 2 digits, only the last two encode the month. Any digits
8017 before this encode a year. So 200712 means December year 2007.
8018 Years ranging from 70 years ago to 30 years in the future can
8019 also be written as 2-digit years."
8020 (interactive "P")
8021 (org-agenda-change-time-span 'month month))
8023 (defun org-agenda-year-view (&optional year)
8024 "Switch to yearly view for agenda.
8025 With argument YEAR, switch to that year. Years ranging from 70
8026 years ago to 30 years in the future can also be written as
8027 2-digit years."
8028 (interactive "P")
8029 (when year
8030 (setq year (org-small-year-to-year year)))
8031 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8032 (org-agenda-change-time-span 'year year)
8033 (error "Abort")))
8035 (defun org-agenda-change-time-span (span &optional n)
8036 "Change the agenda view to SPAN.
8037 SPAN may be `day', `week', `fortnight', `month', `year'."
8038 (org-agenda-check-type t 'agenda)
8039 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8040 (curspan (nth 2 args)))
8041 (if (and (not n) (equal curspan span))
8042 (error "Viewing span is already \"%s\"" span))
8043 (let* ((sd (or (org-get-at-bol 'day)
8044 (nth 1 args)
8045 org-starting-day))
8046 (sd (org-agenda-compute-starting-span sd span n))
8047 (org-agenda-overriding-cmd
8048 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8049 (org-agenda-overriding-arguments
8050 (list (car args) sd span)))
8051 (org-agenda-redo)
8052 (org-agenda-find-same-or-today-or-agenda))
8053 (org-agenda-set-mode-name)
8054 (message "Switched to %s view" span)))
8056 (defun org-agenda-compute-starting-span (sd span &optional n)
8057 "Compute starting date for agenda.
8058 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8059 is a cons cell with the starting date and the number of days,
8060 so that the date SD will be in that range."
8061 (let* ((greg (calendar-gregorian-from-absolute sd))
8062 (dg (nth 1 greg))
8063 (mg (car greg))
8064 (yg (nth 2 greg)))
8065 (cond
8066 ((eq span 'day)
8067 (when n
8068 (setq sd (+ (calendar-absolute-from-gregorian
8069 (list mg 1 yg))
8070 n -1))))
8071 ((or (eq span 'week) (eq span 'fortnight))
8072 (let* ((nt (calendar-day-of-week
8073 (calendar-gregorian-from-absolute sd)))
8074 (d (if org-agenda-start-on-weekday
8075 (- nt org-agenda-start-on-weekday)
8078 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8079 (when n
8080 (require 'cal-iso)
8081 (when (> n 99)
8082 (setq y1 (org-small-year-to-year (/ n 100))
8083 n (mod n 100)))
8084 (setq sd
8085 (calendar-iso-to-absolute
8086 (list n 1
8087 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8088 ((eq span 'month)
8089 (let (y1)
8090 (when (and n (> n 99))
8091 (setq y1 (org-small-year-to-year (/ n 100))
8092 n (mod n 100)))
8093 (setq sd (calendar-absolute-from-gregorian
8094 (list (or n mg) 1 (or y1 yg))))))
8095 ((eq span 'year)
8096 (setq sd (calendar-absolute-from-gregorian
8097 (list 1 1 (or n yg))))))
8098 sd))
8100 (defun org-agenda-next-date-line (&optional arg)
8101 "Jump to the next line indicating a date in agenda buffer."
8102 (interactive "p")
8103 (org-agenda-check-type t 'agenda 'timeline)
8104 (beginning-of-line 1)
8105 ;; This does not work if user makes date format that starts with a blank
8106 (if (looking-at "^\\S-") (forward-char 1))
8107 (if (not (re-search-forward "^\\S-" nil t arg))
8108 (progn
8109 (backward-char 1)
8110 (error "No next date after this line in this buffer")))
8111 (goto-char (match-beginning 0)))
8113 (defun org-agenda-previous-date-line (&optional arg)
8114 "Jump to the previous line indicating a date in agenda buffer."
8115 (interactive "p")
8116 (org-agenda-check-type t 'agenda 'timeline)
8117 (beginning-of-line 1)
8118 (if (not (re-search-backward "^\\S-" nil t arg))
8119 (error "No previous date before this line in this buffer")))
8121 ;; Initialize the highlight
8122 (defvar org-hl (make-overlay 1 1))
8123 (overlay-put org-hl 'face 'highlight)
8125 (defun org-highlight (begin end &optional buffer)
8126 "Highlight a region with overlay."
8127 (move-overlay org-hl begin end (or buffer (current-buffer))))
8129 (defun org-unhighlight ()
8130 "Detach overlay INDEX."
8131 (delete-overlay org-hl))
8133 (defun org-unhighlight-once ()
8134 "Remove the highlight from its position, and this function from the hook."
8135 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8136 (org-unhighlight))
8138 (defvar org-agenda-pre-follow-window-conf nil)
8139 (defun org-agenda-follow-mode ()
8140 "Toggle follow mode in an agenda buffer."
8141 (interactive)
8142 (unless org-agenda-follow-mode
8143 (setq org-agenda-pre-follow-window-conf
8144 (current-window-configuration)))
8145 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8146 (unless org-agenda-follow-mode
8147 (set-window-configuration org-agenda-pre-follow-window-conf))
8148 (org-agenda-set-mode-name)
8149 (org-agenda-do-context-action)
8150 (message "Follow mode is %s"
8151 (if org-agenda-follow-mode "on" "off")))
8153 (defun org-agenda-entry-text-mode (&optional arg)
8154 "Toggle entry text mode in an agenda buffer."
8155 (interactive "P")
8156 (if (or org-agenda-tag-filter
8157 org-agenda-category-filter
8158 org-agenda-regexp-filter
8159 org-agenda-top-headline-filter)
8160 (user-error "Can't show entry text in filtered views")
8161 (setq org-agenda-entry-text-mode (or (integerp arg)
8162 (not org-agenda-entry-text-mode)))
8163 (org-agenda-entry-text-hide)
8164 (and org-agenda-entry-text-mode
8165 (let ((org-agenda-entry-text-maxlines
8166 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8167 (org-agenda-entry-text-show)))
8168 (org-agenda-set-mode-name)
8169 (message "Entry text mode is %s%s"
8170 (if org-agenda-entry-text-mode "on" "off")
8171 (if (not org-agenda-entry-text-mode) ""
8172 (format " (maximum number of lines is %d)"
8173 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8175 (defun org-agenda-clockreport-mode ()
8176 "Toggle clocktable mode in an agenda buffer."
8177 (interactive)
8178 (org-agenda-check-type t 'agenda)
8179 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8180 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8181 (org-agenda-set-mode-name)
8182 (org-agenda-redo)
8183 (message "Clocktable mode is %s"
8184 (if org-agenda-clockreport-mode "on" "off")))
8186 (defun org-agenda-log-mode (&optional special)
8187 "Toggle log mode in an agenda buffer.
8189 With argument SPECIAL, show all possible log items, not only the ones
8190 configured in `org-agenda-log-mode-items'.
8192 With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
8193 log items, nothing else."
8194 (interactive "P")
8195 (org-agenda-check-type t 'agenda 'timeline)
8196 (setq org-agenda-show-log
8197 (cond
8198 ((equal special '(16)) 'only)
8199 ((eq special 'clockcheck)
8200 (if (eq org-agenda-show-log 'clockcheck)
8201 nil 'clockcheck))
8202 (special '(closed clock state))
8203 (t (not org-agenda-show-log))))
8204 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8205 (org-agenda-set-mode-name)
8206 (org-agenda-redo)
8207 (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
8209 (defun org-agenda-archives-mode (&optional with-files)
8210 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8211 When called with a prefix argument, include all archive files as well."
8212 (interactive "P")
8213 (setq org-agenda-archives-mode
8214 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8215 (org-agenda-set-mode-name)
8216 (org-agenda-redo)
8217 (message
8218 "%s"
8219 (cond
8220 ((eq org-agenda-archives-mode nil)
8221 "No archives are included")
8222 ((eq org-agenda-archives-mode 'trees)
8223 (format "Trees with :%s: tag are included" org-archive-tag))
8224 ((eq org-agenda-archives-mode t)
8225 (format "Trees with :%s: tag and all active archive files are included"
8226 org-archive-tag)))))
8228 (defun org-agenda-toggle-diary ()
8229 "Toggle diary inclusion in an agenda buffer."
8230 (interactive)
8231 (org-agenda-check-type t 'agenda)
8232 (setq org-agenda-include-diary (not org-agenda-include-diary))
8233 (org-agenda-redo)
8234 (org-agenda-set-mode-name)
8235 (message "Diary inclusion turned %s"
8236 (if org-agenda-include-diary "on" "off")))
8238 (defun org-agenda-toggle-deadlines ()
8239 "Toggle inclusion of entries with a deadline in an agenda buffer."
8240 (interactive)
8241 (org-agenda-check-type t 'agenda)
8242 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8243 (org-agenda-redo)
8244 (org-agenda-set-mode-name)
8245 (message "Deadlines inclusion turned %s"
8246 (if org-agenda-include-deadlines "on" "off")))
8248 (defun org-agenda-toggle-time-grid ()
8249 "Toggle time grid in an agenda buffer."
8250 (interactive)
8251 (org-agenda-check-type t 'agenda)
8252 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8253 (org-agenda-redo)
8254 (org-agenda-set-mode-name)
8255 (message "Time-grid turned %s"
8256 (if org-agenda-use-time-grid "on" "off")))
8258 (defun org-agenda-set-mode-name ()
8259 "Set the mode name to indicate all the small mode settings."
8260 (setq mode-name
8261 (list "Org-Agenda"
8262 (if (get 'org-agenda-files 'org-restrict) " []" "")
8264 '(:eval (org-agenda-span-name org-agenda-current-span))
8265 (if org-agenda-follow-mode " Follow" "")
8266 (if org-agenda-entry-text-mode " ETxt" "")
8267 (if org-agenda-include-diary " Diary" "")
8268 (if org-agenda-include-deadlines " Ddl" "")
8269 (if org-agenda-use-time-grid " Grid" "")
8270 (if (and (boundp 'org-habit-show-habits)
8271 org-habit-show-habits) " Habit" "")
8272 (cond
8273 ((consp org-agenda-show-log) " LogAll")
8274 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8275 (org-agenda-show-log " Log")
8276 (t ""))
8277 (if (or org-agenda-category-filter
8278 (get 'org-agenda-category-filter :preset-filter))
8279 '(:eval (propertize
8280 (concat " <"
8281 (mapconcat
8282 'identity
8283 (append
8284 (get 'org-agenda-category-filter :preset-filter)
8285 org-agenda-category-filter)
8287 ">")
8288 'face 'org-agenda-filter-category
8289 'help-echo "Category used in filtering")) "")
8290 (if (or org-agenda-tag-filter
8291 (get 'org-agenda-tag-filter :preset-filter))
8292 '(:eval (propertize
8293 (concat " {"
8294 (mapconcat
8295 'identity
8296 (append
8297 (get 'org-agenda-tag-filter :preset-filter)
8298 org-agenda-tag-filter)
8300 "}")
8301 'face 'org-agenda-filter-tags
8302 'help-echo "Tags used in filtering")) "")
8303 (if (or org-agenda-effort-filter
8304 (get 'org-agenda-effort-filter :preset-filter))
8305 '(:eval (propertize
8306 (concat " {"
8307 (mapconcat
8308 'identity
8309 (append
8310 (get 'org-agenda-effort-filter :preset-filter)
8311 org-agenda-effort-filter)
8313 "}")
8314 'face 'org-agenda-filter-effort
8315 'help-echo "Effort conditions used in filtering")) "")
8316 (if (or org-agenda-regexp-filter
8317 (get 'org-agenda-regexp-filter :preset-filter))
8318 '(:eval (propertize
8319 (concat " ["
8320 (mapconcat
8321 'identity
8322 (append
8323 (get 'org-agenda-regexp-filter :preset-filter)
8324 org-agenda-regexp-filter)
8326 "]")
8327 'face 'org-agenda-filter-regexp
8328 'help-echo "Regexp used in filtering")) "")
8329 (if org-agenda-archives-mode
8330 (if (eq org-agenda-archives-mode t)
8331 " Archives"
8332 (format " :%s:" org-archive-tag))
8334 (if org-agenda-clockreport-mode " Clock" "")))
8335 (force-mode-line-update))
8337 (defun org-agenda-update-agenda-type ()
8338 "Update the agenda type after each command."
8339 (setq org-agenda-type
8340 (or (get-text-property (point) 'org-agenda-type)
8341 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8343 (defun org-agenda-next-line ()
8344 "Move cursor to the next line, and show if follow mode is active."
8345 (interactive)
8346 (call-interactively 'next-line)
8347 (org-agenda-do-context-action))
8349 (defun org-agenda-previous-line ()
8350 "Move cursor to the previous line, and show if follow-mode is active."
8351 (interactive)
8352 (call-interactively 'previous-line)
8353 (org-agenda-do-context-action))
8355 (defun org-agenda-next-item (n)
8356 "Move cursor to next agenda item."
8357 (interactive "p")
8358 (let ((col (current-column)))
8359 (dotimes (c n)
8360 (when (next-single-property-change (point-at-eol) 'org-marker)
8361 (move-end-of-line 1)
8362 (goto-char (next-single-property-change (point) 'org-marker))))
8363 (org-move-to-column col))
8364 (org-agenda-do-context-action))
8366 (defun org-agenda-previous-item (n)
8367 "Move cursor to next agenda item."
8368 (interactive "p")
8369 (dotimes (c n)
8370 (let ((col (current-column))
8371 (goto (save-excursion
8372 (move-end-of-line 0)
8373 (previous-single-property-change (point) 'org-marker))))
8374 (if goto (goto-char goto))
8375 (org-move-to-column col)))
8376 (org-agenda-do-context-action))
8378 (defun org-agenda-do-context-action ()
8379 "Show outline path and, maybe, follow mode window."
8380 (let ((m (org-get-at-bol 'org-marker)))
8381 (when (and (markerp m) (marker-buffer m))
8382 (and org-agenda-follow-mode
8383 (if org-agenda-follow-indirect
8384 (org-agenda-tree-to-indirect-buffer nil)
8385 (org-agenda-show)))
8386 (and org-agenda-show-outline-path
8387 (org-with-point-at m (org-display-outline-path t))))))
8389 (defun org-agenda-show-tags ()
8390 "Show the tags applicable to the current item."
8391 (interactive)
8392 (let* ((tags (org-get-at-bol 'tags)))
8393 (if tags
8394 (message "Tags are :%s:"
8395 (org-no-properties (mapconcat 'identity tags ":")))
8396 (message "No tags associated with this line"))))
8398 (defun org-agenda-goto (&optional highlight)
8399 "Go to the entry at point in the corresponding Org file."
8400 (interactive)
8401 (let* ((marker (or (org-get-at-bol 'org-marker)
8402 (org-agenda-error)))
8403 (buffer (marker-buffer marker))
8404 (pos (marker-position marker)))
8405 (switch-to-buffer-other-window buffer)
8406 (widen)
8407 (push-mark)
8408 (goto-char pos)
8409 (when (derived-mode-p 'org-mode)
8410 (org-show-context 'agenda)
8411 (recenter (/ (window-height) 2))
8412 (org-back-to-heading t)
8413 (let ((case-fold-search nil))
8414 (when (re-search-forward org-complex-heading-regexp nil t)
8415 (goto-char (match-beginning 4)))))
8416 (run-hooks 'org-agenda-after-show-hook)
8417 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8419 (defvar org-agenda-after-show-hook nil
8420 "Normal hook run after an item has been shown from the agenda.
8421 Point is in the buffer where the item originated.")
8423 (defun org-agenda-kill ()
8424 "Kill the entry or subtree belonging to the current agenda entry."
8425 (interactive)
8426 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8427 (let* ((bufname-orig (buffer-name))
8428 (marker (or (org-get-at-bol 'org-marker)
8429 (org-agenda-error)))
8430 (buffer (marker-buffer marker))
8431 (pos (marker-position marker))
8432 (type (org-get-at-bol 'type))
8433 dbeg dend (n 0) conf)
8434 (org-with-remote-undo buffer
8435 (with-current-buffer buffer
8436 (save-excursion
8437 (goto-char pos)
8438 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8439 (setq dbeg (progn (org-back-to-heading t) (point))
8440 dend (org-end-of-subtree t t))
8441 (setq dbeg (point-at-bol)
8442 dend (min (point-max) (1+ (point-at-eol)))))
8443 (goto-char dbeg)
8444 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8445 (setq conf (or (eq t org-agenda-confirm-kill)
8446 (and (numberp org-agenda-confirm-kill)
8447 (> n org-agenda-confirm-kill))))
8448 (and conf
8449 (not (y-or-n-p
8450 (format "Delete entry with %d lines in buffer \"%s\"? "
8451 n (buffer-name buffer))))
8452 (error "Abort"))
8453 (let ((org-agenda-buffer-name bufname-orig))
8454 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8455 (with-current-buffer buffer (delete-region dbeg dend))
8456 (message "Agenda item and source killed"))))
8458 (defvar org-archive-default-command) ; defined in org-archive.el
8459 (defun org-agenda-archive-default ()
8460 "Archive the entry or subtree belonging to the current agenda entry."
8461 (interactive)
8462 (require 'org-archive)
8463 (org-agenda-archive-with org-archive-default-command))
8465 (defun org-agenda-archive-default-with-confirmation ()
8466 "Archive the entry or subtree belonging to the current agenda entry."
8467 (interactive)
8468 (require 'org-archive)
8469 (org-agenda-archive-with org-archive-default-command 'confirm))
8471 (defun org-agenda-archive ()
8472 "Archive the entry or subtree belonging to the current agenda entry."
8473 (interactive)
8474 (org-agenda-archive-with 'org-archive-subtree))
8476 (defun org-agenda-archive-to-archive-sibling ()
8477 "Move the entry to the archive sibling."
8478 (interactive)
8479 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8481 (defun org-agenda-archive-with (cmd &optional confirm)
8482 "Move the entry to the archive sibling."
8483 (interactive)
8484 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8485 (let* ((bufname-orig (buffer-name))
8486 (marker (or (org-get-at-bol 'org-marker)
8487 (org-agenda-error)))
8488 (buffer (marker-buffer marker))
8489 (pos (marker-position marker)))
8490 (org-with-remote-undo buffer
8491 (with-current-buffer buffer
8492 (if (derived-mode-p 'org-mode)
8493 (if (and confirm
8494 (not (y-or-n-p "Archive this subtree or entry? ")))
8495 (error "Abort")
8496 (save-window-excursion
8497 (goto-char pos)
8498 (let ((org-agenda-buffer-name bufname-orig))
8499 (org-remove-subtree-entries-from-agenda))
8500 (org-back-to-heading t)
8501 (funcall cmd)))
8502 (error "Archiving works only in Org files"))))))
8504 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8505 "Remove all lines in the agenda that correspond to a given subtree.
8506 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8507 If this information is not given, the function uses the tree at point."
8508 (let ((buf (or buf (current-buffer))) m p)
8509 (save-excursion
8510 (unless (and beg end)
8511 (org-back-to-heading t)
8512 (setq beg (point))
8513 (org-end-of-subtree t)
8514 (setq end (point)))
8515 (set-buffer (get-buffer org-agenda-buffer-name))
8516 (save-excursion
8517 (goto-char (point-max))
8518 (beginning-of-line 1)
8519 (while (not (bobp))
8520 (when (and (setq m (org-get-at-bol 'org-marker))
8521 (equal buf (marker-buffer m))
8522 (setq p (marker-position m))
8523 (>= p beg)
8524 (< p end))
8525 (let ((inhibit-read-only t))
8526 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8527 (beginning-of-line 0))))))
8529 (defun org-agenda-refile (&optional goto rfloc no-update)
8530 "Refile the item at point.
8532 When called with `\\[universal-argument] \\[universal-argument]', \
8533 go to the location of the last
8534 refiled item.
8536 When called with `\\[universal-argument] \\[universal-argument] \
8537 \\[universal-argument]' prefix or when GOTO is 0, clear
8538 the refile cache.
8540 RFLOC can be a refile location obtained in a different way.
8542 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8543 (interactive "P")
8544 (cond
8545 ((member goto '(0 (64)))
8546 (org-refile-cache-clear))
8547 ((equal goto '(16))
8548 (org-refile-goto-last-stored))
8550 (let* ((buffer-orig (buffer-name))
8551 (marker (or (org-get-at-bol 'org-hd-marker)
8552 (org-agenda-error)))
8553 (buffer (marker-buffer marker))
8554 (pos (marker-position marker))
8555 (rfloc (or rfloc
8556 (org-refile-get-location
8557 (if goto "Goto" "Refile to") buffer
8558 org-refile-allow-creating-parent-nodes))))
8559 (with-current-buffer buffer
8560 (org-with-wide-buffer
8561 (goto-char marker)
8562 (let ((org-agenda-buffer-name buffer-orig))
8563 (org-remove-subtree-entries-from-agenda))
8564 (org-refile goto buffer rfloc))))
8565 (unless no-update (org-agenda-redo)))))
8567 (defun org-agenda-open-link (&optional arg)
8568 "Open the link(s) in the current entry, if any.
8569 This looks for a link in the displayed line in the agenda.
8570 It also looks at the text of the entry itself."
8571 (interactive "P")
8572 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8573 (org-get-at-bol 'org-marker)))
8574 (buffer (and marker (marker-buffer marker)))
8575 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8576 (lkall (and buffer (org-offer-links-in-entry
8577 buffer marker arg prefix)))
8578 (lk0 (car lkall))
8579 (lk (if (stringp lk0) (list lk0) lk0))
8580 (lkend (cdr lkall))
8581 trg)
8582 (cond
8583 ((and buffer lk)
8584 (mapcar (lambda(l)
8585 (with-current-buffer buffer
8586 (setq trg (and (string-match org-bracket-link-regexp l)
8587 (match-string 1 l)))
8588 (if (or (not trg) (string-match org-any-link-re trg))
8589 (org-with-wide-buffer
8590 (goto-char marker)
8591 (when (search-forward l nil lkend)
8592 (goto-char (match-beginning 0))
8593 (org-open-at-point)))
8594 ;; This is an internal link, widen the buffer
8595 (switch-to-buffer-other-window buffer)
8596 (widen)
8597 (goto-char marker)
8598 (when (search-forward l nil lkend)
8599 (goto-char (match-beginning 0))
8600 (org-open-at-point)))))
8601 lk))
8602 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8603 (save-excursion
8604 (beginning-of-line 1)
8605 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8606 (org-open-link-from-string (match-string 1)))
8607 (t (message "No link to open here")))))
8609 (defun org-agenda-copy-local-variable (var)
8610 "Get a variable from a referenced buffer and install it here."
8611 (let ((m (org-get-at-bol 'org-marker)))
8612 (when (and m (buffer-live-p (marker-buffer m)))
8613 (set (make-local-variable var)
8614 (with-current-buffer (marker-buffer m)
8615 (symbol-value var))))))
8617 (defun org-agenda-switch-to (&optional delete-other-windows)
8618 "Go to the Org mode file which contains the item at point.
8619 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8620 displayed Org file fills the frame."
8621 (interactive)
8622 (if (and org-return-follows-link
8623 (not (org-get-at-bol 'org-marker))
8624 (org-in-regexp org-bracket-link-regexp))
8625 (org-open-link-from-string (match-string 0))
8626 (let* ((marker (or (org-get-at-bol 'org-marker)
8627 (org-agenda-error)))
8628 (buffer (marker-buffer marker))
8629 (pos (marker-position marker)))
8630 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8631 (pop-to-buffer-same-window buffer)
8632 (when delete-other-windows (delete-other-windows))
8633 (widen)
8634 (goto-char pos)
8635 (when (derived-mode-p 'org-mode)
8636 (org-show-context 'agenda)
8637 (run-hooks 'org-agenda-after-show-hook)))))
8639 (defun org-agenda-goto-mouse (ev)
8640 "Go to the Org file which contains the item at the mouse click."
8641 (interactive "e")
8642 (mouse-set-point ev)
8643 (org-agenda-goto))
8645 (defun org-agenda-show (&optional full-entry)
8646 "Display the Org file which contains the item at point.
8647 With prefix argument FULL-ENTRY, make the entire entry visible
8648 if it was hidden in the outline."
8649 (interactive "P")
8650 (let ((win (selected-window)))
8651 (org-agenda-goto t)
8652 (when full-entry (org-show-entry))
8653 (select-window win)))
8655 (defvar org-agenda-show-window nil)
8656 (defun org-agenda-show-and-scroll-up (&optional arg)
8657 "Display the Org file which contains the item at point.
8659 When called repeatedly, scroll the window that is displaying the buffer.
8661 With a `\\[universal-argument]' prefix, use `org-show-entry' instead of \
8662 `outline-show-subtree'
8663 to display the item, so that drawers and logbooks stay folded."
8664 (interactive "P")
8665 (let ((win (selected-window)))
8666 (if (and (window-live-p org-agenda-show-window)
8667 (eq this-command last-command))
8668 (progn
8669 (select-window org-agenda-show-window)
8670 (ignore-errors (scroll-up)))
8671 (org-agenda-goto t)
8672 (if arg (org-show-entry) (outline-show-subtree))
8673 (setq org-agenda-show-window (selected-window)))
8674 (select-window win)))
8676 (defun org-agenda-show-scroll-down ()
8677 "Scroll down the window showing the agenda."
8678 (interactive)
8679 (let ((win (selected-window)))
8680 (when (window-live-p org-agenda-show-window)
8681 (select-window org-agenda-show-window)
8682 (ignore-errors (scroll-down))
8683 (select-window win))))
8685 (defun org-agenda-show-1 (&optional more)
8686 "Display the Org file which contains the item at point.
8687 The prefix arg selects the amount of information to display:
8689 0 hide the subtree
8690 1 just show the entry according to defaults.
8691 2 show the children view
8692 3 show the subtree view
8693 4 show the entire subtree and any LOGBOOK drawers
8694 5 show the entire subtree and any drawers
8695 With prefix argument FULL-ENTRY, make the entire entry visible
8696 if it was hidden in the outline."
8697 (interactive "p")
8698 (let ((win (selected-window)))
8699 (org-agenda-goto t)
8700 (org-back-to-heading)
8701 (set-window-start (selected-window) (point-at-bol))
8702 (cond
8703 ((= more 0)
8704 (outline-hide-subtree)
8705 (save-excursion
8706 (org-back-to-heading)
8707 (run-hook-with-args 'org-cycle-hook 'folded))
8708 (message "Remote: FOLDED"))
8709 ((and (called-interactively-p 'any) (= more 1))
8710 (message "Remote: show with default settings"))
8711 ((= more 2)
8712 (outline-show-entry)
8713 (org-show-children)
8714 (save-excursion
8715 (org-back-to-heading)
8716 (run-hook-with-args 'org-cycle-hook 'children))
8717 (message "Remote: CHILDREN"))
8718 ((= more 3)
8719 (outline-show-subtree)
8720 (save-excursion
8721 (org-back-to-heading)
8722 (run-hook-with-args 'org-cycle-hook 'subtree))
8723 (message "Remote: SUBTREE"))
8724 ((= more 4)
8725 (outline-show-subtree)
8726 (save-excursion
8727 (org-back-to-heading)
8728 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8729 (message "Remote: SUBTREE AND LOGBOOK"))
8730 ((> more 4)
8731 (outline-show-subtree)
8732 (message "Remote: SUBTREE AND ALL DRAWERS")))
8733 (select-window win)))
8735 (defvar org-agenda-cycle-counter nil)
8736 (defun org-agenda-cycle-show (&optional n)
8737 "Show the current entry in another window, with default settings.
8739 Default settings are taken from `org-show-context-detail'. When
8740 use repeatedly in immediate succession, the remote entry will
8741 cycle through visibility
8743 children -> subtree -> folded
8745 When called with a numeric prefix arg, that arg will be passed through to
8746 `org-agenda-show-1'. For the interpretation of that argument, see the
8747 docstring of `org-agenda-show-1'."
8748 (interactive "P")
8749 (if (integerp n)
8750 (setq org-agenda-cycle-counter n)
8751 (if (not (eq last-command this-command))
8752 (setq org-agenda-cycle-counter 1)
8753 (if (equal org-agenda-cycle-counter 0)
8754 (setq org-agenda-cycle-counter 2)
8755 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8756 (if (> org-agenda-cycle-counter 3)
8757 (setq org-agenda-cycle-counter 0)))))
8758 (org-agenda-show-1 org-agenda-cycle-counter))
8760 (defun org-agenda-recenter (arg)
8761 "Display the Org file which contains the item at point and recenter."
8762 (interactive "P")
8763 (let ((win (selected-window)))
8764 (org-agenda-goto t)
8765 (recenter arg)
8766 (select-window win)))
8768 (defun org-agenda-show-mouse (ev)
8769 "Display the Org file which contains the item at the mouse click."
8770 (interactive "e")
8771 (mouse-set-point ev)
8772 (org-agenda-show))
8774 (defun org-agenda-check-no-diary ()
8775 "Check if the entry is a diary link and abort if yes."
8776 (if (org-get-at-bol 'org-agenda-diary-link)
8777 (org-agenda-error)))
8779 (defun org-agenda-error ()
8780 "Throw an error when a command is not allowed in the agenda."
8781 (user-error "Command not allowed in this line"))
8783 (defun org-agenda-tree-to-indirect-buffer (arg)
8784 "Show the subtree corresponding to the current entry in an indirect buffer.
8785 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8787 With a numerical prefix ARG, go up to this level and then take that tree.
8788 With a negative numeric ARG, go up by this number of levels.
8790 With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
8791 i.e. don't use
8792 the dedicated frame."
8793 (interactive "P")
8794 (if current-prefix-arg
8795 (org-agenda-do-tree-to-indirect-buffer arg)
8796 (let ((agenda-buffer (buffer-name))
8797 (agenda-window (selected-window))
8798 (indirect-window
8799 (and org-last-indirect-buffer
8800 (get-buffer-window org-last-indirect-buffer))))
8801 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8802 (unless (or (eq org-indirect-buffer-display 'new-frame)
8803 (eq org-indirect-buffer-display 'dedicated-frame))
8804 (unwind-protect
8805 (unless (and indirect-window (window-live-p indirect-window))
8806 (setq indirect-window (split-window agenda-window)))
8807 (and indirect-window (select-window indirect-window))
8808 (switch-to-buffer org-last-indirect-buffer :norecord)
8809 (fit-window-to-buffer indirect-window)))
8810 (select-window (get-buffer-window agenda-buffer))
8811 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8813 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8814 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8815 (org-agenda-check-no-diary)
8816 (let* ((marker (or (org-get-at-bol 'org-marker)
8817 (org-agenda-error)))
8818 (buffer (marker-buffer marker))
8819 (pos (marker-position marker)))
8820 (with-current-buffer buffer
8821 (save-excursion
8822 (goto-char pos)
8823 (funcall 'org-tree-to-indirect-buffer arg)))))
8825 (defvar org-last-heading-marker (make-marker)
8826 "Marker pointing to the headline that last changed its TODO state
8827 by a remote command from the agenda.")
8829 (defun org-agenda-todo-nextset ()
8830 "Switch TODO entry to next sequence."
8831 (interactive)
8832 (org-agenda-todo 'nextset))
8834 (defun org-agenda-todo-previousset ()
8835 "Switch TODO entry to previous sequence."
8836 (interactive)
8837 (org-agenda-todo 'previousset))
8839 (defun org-agenda-todo (&optional arg)
8840 "Cycle TODO state of line at point, also in Org file.
8841 This changes the line at point, all other lines in the agenda referring to
8842 the same tree node, and the headline of the tree node in the Org file."
8843 (interactive "P")
8844 (org-agenda-check-no-diary)
8845 (let* ((col (current-column))
8846 (marker (or (org-get-at-bol 'org-marker)
8847 (org-agenda-error)))
8848 (buffer (marker-buffer marker))
8849 (pos (marker-position marker))
8850 (hdmarker (org-get-at-bol 'org-hd-marker))
8851 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8852 (inhibit-read-only t)
8853 org-agenda-headline-snapshot-before-repeat newhead just-one)
8854 (org-with-remote-undo buffer
8855 (with-current-buffer buffer
8856 (widen)
8857 (goto-char pos)
8858 (org-show-context 'agenda)
8859 (let ((current-prefix-arg arg))
8860 (call-interactively 'org-todo))
8861 (and (bolp) (forward-char 1))
8862 (setq newhead (org-get-heading))
8863 (when (and (bound-and-true-p
8864 org-agenda-headline-snapshot-before-repeat)
8865 (not (equal org-agenda-headline-snapshot-before-repeat
8866 newhead))
8867 todayp)
8868 (setq newhead org-agenda-headline-snapshot-before-repeat
8869 just-one t))
8870 (save-excursion
8871 (org-back-to-heading)
8872 (move-marker org-last-heading-marker (point))))
8873 (beginning-of-line 1)
8874 (save-window-excursion
8875 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8876 (when (bound-and-true-p org-clock-out-when-done)
8877 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8878 newhead)
8879 (org-agenda-unmark-clocking-task))
8880 (org-move-to-column col)
8881 (org-agenda-mark-clocking-task))))
8883 (defun org-agenda-add-note (&optional arg)
8884 "Add a time-stamped note to the entry at point."
8885 (interactive "P")
8886 (org-agenda-check-no-diary)
8887 (let* ((marker (or (org-get-at-bol 'org-marker)
8888 (org-agenda-error)))
8889 (buffer (marker-buffer marker))
8890 (pos (marker-position marker))
8891 (hdmarker (org-get-at-bol 'org-hd-marker))
8892 (inhibit-read-only t))
8893 (with-current-buffer buffer
8894 (widen)
8895 (goto-char pos)
8896 (org-show-context 'agenda)
8897 (org-add-note))))
8899 (defun org-agenda-change-all-lines (newhead hdmarker
8900 &optional fixface just-this)
8901 "Change all lines in the agenda buffer which match HDMARKER.
8902 The new content of the line will be NEWHEAD (as modified by
8903 `org-agenda-format-item'). HDMARKER is checked with
8904 `equal' against all `org-hd-marker' text properties in the file.
8905 If FIXFACE is non-nil, the face of each item is modified according to
8906 the new TODO state.
8907 If JUST-THIS is non-nil, change just the current line, not all.
8908 If FORCE-TAGS is non nil, the car of it returns the new tags."
8909 (let* ((inhibit-read-only t)
8910 (line (org-current-line))
8911 (org-agenda-buffer (current-buffer))
8912 (thetags (with-current-buffer (marker-buffer hdmarker)
8913 (org-with-wide-buffer
8914 (goto-char hdmarker)
8915 (org-get-tags-at))))
8916 props m pl undone-face done-face finish new dotime level cat tags)
8917 (save-excursion
8918 (goto-char (point-max))
8919 (beginning-of-line 1)
8920 (while (not finish)
8921 (setq finish (bobp))
8922 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8923 (or (not just-this) (= (org-current-line) line))
8924 (equal m hdmarker))
8925 (setq props (text-properties-at (point))
8926 dotime (org-get-at-bol 'dotime)
8927 cat (org-get-at-eol 'org-category 1)
8928 level (org-get-at-bol 'level)
8929 tags thetags
8931 (let ((org-prefix-format-compiled
8932 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8933 org-prefix-format-compiled))
8934 (extra (org-get-at-bol 'extra)))
8935 (with-current-buffer (marker-buffer hdmarker)
8936 (org-with-wide-buffer
8937 (org-agenda-format-item extra newhead level cat tags dotime))))
8938 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8939 undone-face (org-get-at-bol 'undone-face)
8940 done-face (org-get-at-bol 'done-face))
8941 (beginning-of-line 1)
8942 (cond
8943 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8944 ((looking-at ".*")
8945 ;; When replacing the whole line, preserve bulk mark
8946 ;; overlay, if any.
8947 (let ((mark (catch :overlay
8948 (dolist (o (overlays-in (point) (+ 2 (point))))
8949 (when (eq (overlay-get o 'type)
8950 'org-marked-entry-overlay)
8951 (throw :overlay o))))))
8952 (replace-match new t t)
8953 (beginning-of-line)
8954 (when mark (move-overlay mark (point) (+ 2 (point)))))
8955 (add-text-properties (point-at-bol) (point-at-eol) props)
8956 (when fixface
8957 (add-text-properties
8958 (point-at-bol) (point-at-eol)
8959 (list 'face
8960 (if org-last-todo-state-is-todo
8961 undone-face done-face))))
8962 (org-agenda-highlight-todo 'line)
8963 (beginning-of-line 1))
8964 (t (error "Line update did not work")))
8965 (save-restriction
8966 (narrow-to-region (point-at-bol) (point-at-eol))
8967 (org-agenda-finalize)))
8968 (beginning-of-line 0)))))
8970 (defun org-agenda-align-tags (&optional line)
8971 "Align all tags in agenda items to `org-agenda-tags-column'."
8972 (let ((inhibit-read-only t) l c)
8973 (save-excursion
8974 (goto-char (if line (point-at-bol) (point-min)))
8975 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8976 (if line (point-at-eol) nil) t)
8977 (add-text-properties
8978 (match-beginning 2) (match-end 2)
8979 (list 'face (delq nil (let ((prop (get-text-property
8980 (match-beginning 2) 'face)))
8981 (or (listp prop) (setq prop (list prop)))
8982 (if (memq 'org-tag prop)
8983 prop
8984 (cons 'org-tag prop))))))
8985 (setq l (- (match-end 2) (match-beginning 2))
8986 c (if (< org-agenda-tags-column 0)
8987 (- (abs org-agenda-tags-column) l)
8988 org-agenda-tags-column))
8989 (delete-region (match-beginning 1) (match-end 1))
8990 (goto-char (match-beginning 1))
8991 (insert (org-add-props
8992 (make-string (max 1 (- c (current-column))) ?\ )
8993 (plist-put (copy-sequence (text-properties-at (point)))
8994 'face nil))))
8995 (goto-char (point-min))
8996 (org-font-lock-add-tag-faces (point-max)))))
8998 (defun org-agenda-priority-up ()
8999 "Increase the priority of line at point, also in Org file."
9000 (interactive)
9001 (org-agenda-priority 'up))
9003 (defun org-agenda-priority-down ()
9004 "Decrease the priority of line at point, also in Org file."
9005 (interactive)
9006 (org-agenda-priority 'down))
9008 (defun org-agenda-priority (&optional force-direction)
9009 "Set the priority of line at point, also in Org file.
9010 This changes the line at point, all other lines in the agenda referring to
9011 the same tree node, and the headline of the tree node in the Org file.
9012 Called with a universal prefix arg, show the priority instead of setting it."
9013 (interactive "P")
9014 (if (equal force-direction '(4))
9015 (org-show-priority)
9016 (unless org-enable-priority-commands
9017 (error "Priority commands are disabled"))
9018 (org-agenda-check-no-diary)
9019 (let* ((col (current-column))
9020 (marker (or (org-get-at-bol 'org-marker)
9021 (org-agenda-error)))
9022 (hdmarker (org-get-at-bol 'org-hd-marker))
9023 (buffer (marker-buffer hdmarker))
9024 (pos (marker-position hdmarker))
9025 (inhibit-read-only t)
9026 newhead)
9027 (org-with-remote-undo buffer
9028 (with-current-buffer buffer
9029 (widen)
9030 (goto-char pos)
9031 (org-show-context 'agenda)
9032 (funcall 'org-priority force-direction)
9033 (end-of-line 1)
9034 (setq newhead (org-get-heading)))
9035 (org-agenda-change-all-lines newhead hdmarker)
9036 (org-move-to-column col)))))
9038 ;; FIXME: should fix the tags property of the agenda line.
9039 (defun org-agenda-set-tags (&optional tag onoff)
9040 "Set tags for the current headline."
9041 (interactive)
9042 (org-agenda-check-no-diary)
9043 (if (and (org-region-active-p) (called-interactively-p 'any))
9044 (call-interactively 'org-change-tag-in-region)
9045 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9046 (org-agenda-error)))
9047 (buffer (marker-buffer hdmarker))
9048 (pos (marker-position hdmarker))
9049 (inhibit-read-only t)
9050 newhead)
9051 (org-with-remote-undo buffer
9052 (with-current-buffer buffer
9053 (widen)
9054 (goto-char pos)
9055 (org-show-context 'agenda)
9056 (if tag
9057 (org-toggle-tag tag onoff)
9058 (call-interactively 'org-set-tags))
9059 (end-of-line 1)
9060 (setq newhead (org-get-heading)))
9061 (org-agenda-change-all-lines newhead hdmarker)
9062 (beginning-of-line 1)))))
9064 (defun org-agenda-set-property ()
9065 "Set a property for the current headline."
9066 (interactive)
9067 (org-agenda-check-no-diary)
9068 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9069 (org-agenda-error)))
9070 (buffer (marker-buffer hdmarker))
9071 (pos (marker-position hdmarker))
9072 (inhibit-read-only t)
9073 newhead)
9074 (org-with-remote-undo buffer
9075 (with-current-buffer buffer
9076 (widen)
9077 (goto-char pos)
9078 (org-show-context 'agenda)
9079 (call-interactively 'org-set-property)))))
9081 (defun org-agenda-set-effort ()
9082 "Set the effort property for the current headline."
9083 (interactive)
9084 (org-agenda-check-no-diary)
9085 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9086 (org-agenda-error)))
9087 (buffer (marker-buffer hdmarker))
9088 (pos (marker-position hdmarker))
9089 (inhibit-read-only t)
9090 newhead)
9091 (org-with-remote-undo buffer
9092 (with-current-buffer buffer
9093 (widen)
9094 (goto-char pos)
9095 (org-show-context 'agenda)
9096 (call-interactively 'org-set-effort)
9097 (end-of-line 1)
9098 (setq newhead (org-get-heading)))
9099 (org-agenda-change-all-lines newhead hdmarker))))
9101 (defun org-agenda-toggle-archive-tag ()
9102 "Toggle the archive tag for the current entry."
9103 (interactive)
9104 (org-agenda-check-no-diary)
9105 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9106 (org-agenda-error)))
9107 (buffer (marker-buffer hdmarker))
9108 (pos (marker-position hdmarker))
9109 (inhibit-read-only t)
9110 newhead)
9111 (org-with-remote-undo buffer
9112 (with-current-buffer buffer
9113 (widen)
9114 (goto-char pos)
9115 (org-show-context 'agenda)
9116 (call-interactively 'org-toggle-archive-tag)
9117 (end-of-line 1)
9118 (setq newhead (org-get-heading)))
9119 (org-agenda-change-all-lines newhead hdmarker)
9120 (beginning-of-line 1))))
9122 (defun org-agenda-do-date-later (arg)
9123 (interactive "P")
9124 (cond
9125 ((or (equal arg '(16))
9126 (memq last-command
9127 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9128 (setq this-command 'org-agenda-date-later-minutes)
9129 (org-agenda-date-later-minutes 1))
9130 ((or (equal arg '(4))
9131 (memq last-command
9132 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9133 (setq this-command 'org-agenda-date-later-hours)
9134 (org-agenda-date-later-hours 1))
9136 (org-agenda-date-later (prefix-numeric-value arg)))))
9138 (defun org-agenda-do-date-earlier (arg)
9139 (interactive "P")
9140 (cond
9141 ((or (equal arg '(16))
9142 (memq last-command
9143 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9144 (setq this-command 'org-agenda-date-earlier-minutes)
9145 (org-agenda-date-earlier-minutes 1))
9146 ((or (equal arg '(4))
9147 (memq last-command
9148 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9149 (setq this-command 'org-agenda-date-earlier-hours)
9150 (org-agenda-date-earlier-hours 1))
9152 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9154 (defun org-agenda-date-later (arg &optional what)
9155 "Change the date of this item to ARG day(s) later."
9156 (interactive "p")
9157 (org-agenda-check-type t 'agenda 'timeline)
9158 (org-agenda-check-no-diary)
9159 (let* ((marker (or (org-get-at-bol 'org-marker)
9160 (org-agenda-error)))
9161 (buffer (marker-buffer marker))
9162 (pos (marker-position marker))
9163 cdate today)
9164 (org-with-remote-undo buffer
9165 (with-current-buffer buffer
9166 (widen)
9167 (goto-char pos)
9168 (if (not (org-at-timestamp-p))
9169 (error "Cannot find time stamp"))
9170 (when (and org-agenda-move-date-from-past-immediately-to-today
9171 (equal arg 1)
9172 (or (not what) (eq what 'day))
9173 (not (save-match-data (org-at-date-range-p))))
9174 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9175 cdate (calendar-absolute-from-gregorian
9176 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9177 today (org-today))
9178 (if (> today cdate)
9179 ;; immediately shift to today
9180 (setq arg (- today cdate))))
9181 (org-timestamp-change arg (or what 'day))
9182 (when (and (org-at-date-range-p)
9183 (re-search-backward org-tr-regexp-both (point-at-bol)))
9184 (let ((end org-last-changed-timestamp))
9185 (org-timestamp-change arg (or what 'day))
9186 (setq org-last-changed-timestamp
9187 (concat org-last-changed-timestamp "--" end)))))
9188 (org-agenda-show-new-time marker org-last-changed-timestamp))
9189 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9191 (defun org-agenda-date-earlier (arg &optional what)
9192 "Change the date of this item to ARG day(s) earlier."
9193 (interactive "p")
9194 (org-agenda-date-later (- arg) what))
9196 (defun org-agenda-date-later-minutes (arg)
9197 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9198 (interactive "p")
9199 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9200 (org-agenda-date-later arg 'minute))
9202 (defun org-agenda-date-earlier-minutes (arg)
9203 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9204 (interactive "p")
9205 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9206 (org-agenda-date-earlier arg 'minute))
9208 (defun org-agenda-date-later-hours (arg)
9209 "Change the time of this item, in hour steps."
9210 (interactive "p")
9211 (org-agenda-date-later arg 'hour))
9213 (defun org-agenda-date-earlier-hours (arg)
9214 "Change the time of this item, in hour steps."
9215 (interactive "p")
9216 (org-agenda-date-earlier arg 'hour))
9218 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9219 "Show new date stamp via text properties."
9220 ;; We use text properties to make this undoable
9221 (let ((inhibit-read-only t))
9222 (setq stamp (concat prefix " => " stamp " "))
9223 (save-excursion
9224 (goto-char (point-max))
9225 (while (not (bobp))
9226 (when (equal marker (org-get-at-bol 'org-marker))
9227 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9228 (org-move-to-column (- (window-width) (length stamp)) t)
9229 (add-text-properties
9230 (1- (point)) (point-at-eol)
9231 (list 'display (org-add-props stamp nil
9232 'face '(secondary-selection default))))
9233 (beginning-of-line 1))
9234 (beginning-of-line 0)))))
9236 (defun org-agenda-date-prompt (arg)
9237 "Change the date of this item. Date is prompted for, with default today.
9238 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9239 be used to request time specification in the time stamp."
9240 (interactive "P")
9241 (org-agenda-check-type t 'agenda 'timeline)
9242 (org-agenda-check-no-diary)
9243 (let* ((marker (or (org-get-at-bol 'org-marker)
9244 (org-agenda-error)))
9245 (buffer (marker-buffer marker))
9246 (pos (marker-position marker)))
9247 (org-with-remote-undo buffer
9248 (with-current-buffer buffer
9249 (widen)
9250 (goto-char pos)
9251 (if (not (org-at-timestamp-p t))
9252 (error "Cannot find time stamp"))
9253 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9254 (org-agenda-show-new-time marker org-last-changed-timestamp))
9255 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9257 (defun org-agenda-schedule (arg &optional time)
9258 "Schedule the item at point.
9259 ARG is passed through to `org-schedule'."
9260 (interactive "P")
9261 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9262 (org-agenda-check-no-diary)
9263 (let* ((marker (or (org-get-at-bol 'org-marker)
9264 (org-agenda-error)))
9265 (type (marker-insertion-type marker))
9266 (buffer (marker-buffer marker))
9267 (pos (marker-position marker))
9269 (set-marker-insertion-type marker t)
9270 (org-with-remote-undo buffer
9271 (with-current-buffer buffer
9272 (widen)
9273 (goto-char pos)
9274 (setq ts (org-schedule arg time)))
9275 (org-agenda-show-new-time marker ts " S"))
9276 (message "%s" ts)))
9278 (defun org-agenda-deadline (arg &optional time)
9279 "Schedule the item at point.
9280 ARG is passed through to `org-deadline'."
9281 (interactive "P")
9282 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9283 (org-agenda-check-no-diary)
9284 (let* ((marker (or (org-get-at-bol 'org-marker)
9285 (org-agenda-error)))
9286 (buffer (marker-buffer marker))
9287 (pos (marker-position marker))
9289 (org-with-remote-undo buffer
9290 (with-current-buffer buffer
9291 (widen)
9292 (goto-char pos)
9293 (setq ts (org-deadline arg time)))
9294 (org-agenda-show-new-time marker ts " D"))
9295 (message "%s" ts)))
9297 (defun org-agenda-clock-in (&optional arg)
9298 "Start the clock on the currently selected item."
9299 (interactive "P")
9300 (org-agenda-check-no-diary)
9301 (if (equal arg '(4))
9302 (org-clock-in arg)
9303 (let* ((marker (or (org-get-at-bol 'org-marker)
9304 (org-agenda-error)))
9305 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9306 (pos (marker-position marker))
9307 (col (current-column))
9308 newhead)
9309 (org-with-remote-undo (marker-buffer marker)
9310 (with-current-buffer (marker-buffer marker)
9311 (widen)
9312 (goto-char pos)
9313 (org-show-context 'agenda)
9314 (org-cycle-hide-drawers 'children)
9315 (org-clock-in arg)
9316 (setq newhead (org-get-heading)))
9317 (org-agenda-change-all-lines newhead hdmarker))
9318 (org-move-to-column col))))
9320 (defun org-agenda-clock-out ()
9321 "Stop the currently running clock."
9322 (interactive)
9323 (unless (marker-buffer org-clock-marker)
9324 (error "No running clock"))
9325 (let ((marker (make-marker)) (col (current-column)) newhead)
9326 (org-with-remote-undo (marker-buffer org-clock-marker)
9327 (with-current-buffer (marker-buffer org-clock-marker)
9328 (org-with-wide-buffer
9329 (goto-char org-clock-marker)
9330 (org-back-to-heading t)
9331 (move-marker marker (point))
9332 (org-clock-out)
9333 (setq newhead (org-get-heading)))))
9334 (org-agenda-change-all-lines newhead marker)
9335 (move-marker marker nil)
9336 (org-move-to-column col)
9337 (org-agenda-unmark-clocking-task)))
9339 (defun org-agenda-clock-cancel (&optional arg)
9340 "Cancel the currently running clock."
9341 (interactive "P")
9342 (unless (marker-buffer org-clock-marker)
9343 (user-error "No running clock"))
9344 (org-with-remote-undo (marker-buffer org-clock-marker)
9345 (org-clock-cancel)))
9347 (defun org-agenda-clock-goto ()
9348 "Jump to the currently clocked in task within the agenda.
9349 If the currently clocked in task is not listed in the agenda
9350 buffer, display it in another window."
9351 (interactive)
9352 (let (pos)
9353 (mapc (lambda (o)
9354 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9355 (setq pos (overlay-start o))))
9356 (overlays-in (point-min) (point-max)))
9357 (cond (pos (goto-char pos))
9358 ;; If the currently clocked entry is not in the agenda
9359 ;; buffer, we visit it in another window:
9360 ((bound-and-true-p org-clock-current-task)
9361 (org-switch-to-buffer-other-window (org-clock-goto)))
9362 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9364 (defun org-agenda-diary-entry-in-org-file ()
9365 "Make a diary entry in the file `org-agenda-diary-file'."
9366 (let (d1 d2 char (text "") dp1 dp2)
9367 (if (equal (buffer-name) "*Calendar*")
9368 (setq d1 (calendar-cursor-to-date t)
9369 d2 (car calendar-mark-ring))
9370 (setq dp1 (get-text-property (point-at-bol) 'day))
9371 (unless dp1 (user-error "No date defined in current line"))
9372 (setq d1 (calendar-gregorian-from-absolute dp1)
9373 d2 (and (ignore-errors (mark))
9374 (save-excursion
9375 (goto-char (mark))
9376 (setq dp2 (get-text-property (point-at-bol) 'day)))
9377 (calendar-gregorian-from-absolute dp2))))
9378 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9379 (setq char (read-char-exclusive))
9380 (cond
9381 ((equal char ?d)
9382 (setq text (read-string "Day entry: "))
9383 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9384 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9385 ((equal char ?a)
9386 (setq d1 (list (car d1) (nth 1 d1)
9387 (read-number (format "Reference year [%d]: " (nth 2 d1))
9388 (nth 2 d1))))
9389 (setq text (read-string "Anniversary (use %d to show years): "))
9390 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9391 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9392 ((equal char ?b)
9393 (setq text (read-string "Block entry: "))
9394 (unless (and d1 d2 (not (equal d1 d2)))
9395 (user-error "No block of days selected"))
9396 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9397 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9398 ((equal char ?j)
9399 (org-switch-to-buffer-other-window
9400 (find-file-noselect org-agenda-diary-file))
9401 (require 'org-datetree)
9402 (org-datetree-find-date-create d1)
9403 (org-reveal t))
9404 (t (user-error "Invalid selection character `%c'" char)))))
9406 (defcustom org-agenda-insert-diary-strategy 'date-tree
9407 "Where in `org-agenda-diary-file' should new entries be added?
9408 Valid values:
9410 date-tree in the date tree, as first child of the date
9411 date-tree-last in the date tree, as last child of the date
9412 top-level as top-level entries at the end of the file."
9413 :group 'org-agenda
9414 :type '(choice
9415 (const :tag "first in a date tree" date-tree)
9416 (const :tag "last in a date tree" date-tree-last)
9417 (const :tag "as top level at end of file" top-level)))
9419 (defcustom org-agenda-insert-diary-extract-time nil
9420 "Non-nil means extract any time specification from the diary entry."
9421 :group 'org-agenda
9422 :version "24.1"
9423 :type 'boolean)
9425 (defcustom org-agenda-bulk-mark-char ">"
9426 "A single-character string to be used as the bulk mark."
9427 :group 'org-agenda
9428 :version "24.1"
9429 :type 'string)
9431 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9432 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9433 If TEXT is not empty, it will become the headline of the new entry, and
9434 the resulting entry will not be shown. When TEXT is empty, switch to
9435 `org-agenda-diary-file' and let the user finish the entry there."
9436 (let ((cw (current-window-configuration)))
9437 (org-switch-to-buffer-other-window
9438 (find-file-noselect org-agenda-diary-file))
9439 (widen)
9440 (goto-char (point-min))
9441 (cond
9442 ((eq type 'anniversary)
9443 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9444 (progn
9445 (or (org-at-heading-p t)
9446 (progn
9447 (outline-next-heading)
9448 (insert "* Anniversaries\n\n")
9449 (beginning-of-line -1)))))
9450 (outline-next-heading)
9451 (org-back-over-empty-lines)
9452 (backward-char 1)
9453 (insert "\n")
9454 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9455 (nth 2 d1) (car d1) (nth 1 d1) text)))
9456 ((eq type 'day)
9457 (let ((org-prefix-has-time t)
9458 (org-agenda-time-leading-zero t)
9459 fmt time time2)
9460 (if org-agenda-insert-diary-extract-time
9461 ;; Use org-agenda-format-item to parse text for a time-range and
9462 ;; remove it. FIXME: This is a hack, we should refactor
9463 ;; that function to make time extraction available separately
9464 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9465 time (get-text-property 0 'time fmt)
9466 time2 (if (> (length time) 0)
9467 ;; split-string removes trailing ...... if
9468 ;; no end time given. First space
9469 ;; separates time from date.
9470 (concat " " (car (split-string time "\\.")))
9471 nil)
9472 text (get-text-property 0 'txt fmt)))
9473 (if (eq org-agenda-insert-diary-strategy 'top-level)
9474 (org-agenda-insert-diary-as-top-level text)
9475 (require 'org-datetree)
9476 (org-datetree-find-date-create d1)
9477 (org-agenda-insert-diary-make-new-entry text))
9478 (org-insert-time-stamp (org-time-from-absolute
9479 (calendar-absolute-from-gregorian d1))
9480 nil nil nil nil time2))
9481 (end-of-line 0))
9482 ((eq type 'block)
9483 (if (> (calendar-absolute-from-gregorian d1)
9484 (calendar-absolute-from-gregorian d2))
9485 (setq d1 (prog1 d2 (setq d2 d1))))
9486 (if (eq org-agenda-insert-diary-strategy 'top-level)
9487 (org-agenda-insert-diary-as-top-level text)
9488 (require 'org-datetree)
9489 (org-datetree-find-date-create d1)
9490 (org-agenda-insert-diary-make-new-entry text))
9491 (org-insert-time-stamp (org-time-from-absolute
9492 (calendar-absolute-from-gregorian d1)))
9493 (insert "--")
9494 (org-insert-time-stamp (org-time-from-absolute
9495 (calendar-absolute-from-gregorian d2)))
9496 (end-of-line 0)))
9497 (if (string-match "\\S-" text)
9498 (progn
9499 (set-window-configuration cw)
9500 (message "%s entry added to %s"
9501 (capitalize (symbol-name type))
9502 (abbreviate-file-name org-agenda-diary-file)))
9503 (org-reveal t)
9504 (message "Please finish entry here"))))
9506 (defun org-agenda-insert-diary-as-top-level (text)
9507 "Make new entry as a top-level entry at the end of the file.
9508 Add TEXT as headline, and position the cursor in the second line so that
9509 a timestamp can be added there."
9510 (widen)
9511 (goto-char (point-max))
9512 (unless (bolp) (insert "\n"))
9513 (org-insert-heading nil t t)
9514 (insert text)
9515 (org-end-of-meta-data)
9516 (unless (bolp) (insert "\n"))
9517 (when org-adapt-indentation (indent-to-column 2)))
9519 (defun org-agenda-insert-diary-make-new-entry (text)
9520 "Make a new entry with TEXT as a child of the current subtree.
9521 Position the point in the heading's first body line so that
9522 a timestamp can be added there."
9523 (cond
9524 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9525 (end-of-line)
9526 (org-insert-heading '(4) t)
9527 (org-do-demote))
9529 (outline-next-heading)
9530 (org-back-over-empty-lines)
9531 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9532 (org-insert-heading nil t)
9533 (org-do-demote)))
9534 (let ((col (current-column)))
9535 (insert text)
9536 (org-end-of-meta-data)
9537 ;; Ensure point is left on a blank line, at proper indentation.
9538 (unless (bolp) (insert "\n"))
9539 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9540 (when org-adapt-indentation (indent-to-column col)))
9541 (org-show-set-visibility 'lineage))
9543 (defun org-agenda-diary-entry ()
9544 "Make a diary entry, like the `i' command from the calendar.
9545 All the standard commands work: block, weekly etc.
9546 When `org-agenda-diary-file' points to a file,
9547 `org-agenda-diary-entry-in-org-file' is called instead to create
9548 entries in that Org file."
9549 (interactive)
9550 (if (not (eq org-agenda-diary-file 'diary-file))
9551 (org-agenda-diary-entry-in-org-file)
9552 (require 'diary-lib)
9553 (let* ((char (progn
9554 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9555 (read-char-exclusive)))
9556 (cmd (cdr (assoc char
9557 '((?d . diary-insert-entry)
9558 (?w . diary-insert-weekly-entry)
9559 (?m . diary-insert-monthly-entry)
9560 (?y . diary-insert-yearly-entry)
9561 (?a . diary-insert-anniversary-entry)
9562 (?b . diary-insert-block-entry)
9563 (?c . diary-insert-cyclic-entry)))))
9564 (oldf (symbol-function 'calendar-cursor-to-date))
9565 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9566 (point (point))
9567 (mark (or (mark t) (point))))
9568 (unless cmd
9569 (user-error "No command associated with <%c>" char))
9570 (unless (and (get-text-property point 'day)
9571 (or (not (equal ?b char))
9572 (get-text-property mark 'day)))
9573 (user-error "Don't know which date to use for diary entry"))
9574 ;; We implement this by hacking the `calendar-cursor-to-date' function
9575 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9576 (let ((calendar-mark-ring
9577 (list (calendar-gregorian-from-absolute
9578 (or (get-text-property mark 'day)
9579 (get-text-property point 'day))))))
9580 (unwind-protect
9581 (progn
9582 (fset 'calendar-cursor-to-date
9583 (lambda (&optional error dummy)
9584 (calendar-gregorian-from-absolute
9585 (get-text-property point 'day))))
9586 (call-interactively cmd))
9587 (fset 'calendar-cursor-to-date oldf))))))
9589 (defun org-agenda-execute-calendar-command (cmd)
9590 "Execute a calendar command from the agenda with date from cursor."
9591 (org-agenda-check-type t 'agenda 'timeline)
9592 (require 'diary-lib)
9593 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9594 (user-error "Don't know which date to use for the calendar command"))
9595 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9596 (point (point))
9597 (date (calendar-gregorian-from-absolute
9598 (get-text-property point 'day)))
9599 ;; the following 2 vars are needed in the calendar
9600 (displayed-month (car date))
9601 (displayed-year (nth 2 date)))
9602 (unwind-protect
9603 (progn
9604 (fset 'calendar-cursor-to-date
9605 (lambda (&optional error dummy)
9606 (calendar-gregorian-from-absolute
9607 (get-text-property point 'day))))
9608 (call-interactively cmd))
9609 (fset 'calendar-cursor-to-date oldf))))
9611 (defun org-agenda-phases-of-moon ()
9612 "Display the phases of the moon for the 3 months around the cursor date."
9613 (interactive)
9614 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9616 (defun org-agenda-holidays ()
9617 "Display the holidays for the 3 months around the cursor date."
9618 (interactive)
9619 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9621 (defvar calendar-longitude) ; defined in calendar.el
9622 (defvar calendar-latitude) ; defined in calendar.el
9623 (defvar calendar-location-name) ; defined in calendar.el
9625 (defun org-agenda-sunrise-sunset (arg)
9626 "Display sunrise and sunset for the cursor date.
9627 Latitude and longitude can be specified with the variables
9628 `calendar-latitude' and `calendar-longitude'. When called with prefix
9629 argument, latitude and longitude will be prompted for."
9630 (interactive "P")
9631 (require 'solar)
9632 (let ((calendar-longitude (if arg nil calendar-longitude))
9633 (calendar-latitude (if arg nil calendar-latitude))
9634 (calendar-location-name
9635 (if arg "the given coordinates" calendar-location-name)))
9636 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9638 (defun org-agenda-goto-calendar ()
9639 "Open the Emacs calendar with the date at the cursor."
9640 (interactive)
9641 (org-agenda-check-type t 'agenda 'timeline)
9642 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9643 (user-error "Don't know which date to open in calendar")))
9644 (date (calendar-gregorian-from-absolute day))
9645 (calendar-move-hook nil)
9646 (calendar-view-holidays-initially-flag nil)
9647 (calendar-view-diary-initially-flag nil))
9648 (calendar)
9649 (calendar-goto-date date)))
9651 ;;;###autoload
9652 (defun org-calendar-goto-agenda ()
9653 "Compute the Org agenda for the calendar date displayed at the cursor.
9654 This is a command that has to be installed in `calendar-mode-map'."
9655 (interactive)
9656 ;; Temporarily disable sticky agenda since user clearly wants to
9657 ;; refresh view anyway.
9658 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9659 (org-agenda-sticky nil))
9660 (org-agenda-list nil (calendar-absolute-from-gregorian
9661 (calendar-cursor-to-date))
9662 nil)))
9664 (defun org-agenda-convert-date ()
9665 (interactive)
9666 (org-agenda-check-type t 'agenda 'timeline)
9667 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9668 date s)
9669 (unless day
9670 (user-error "Don't know which date to convert"))
9671 (setq date (calendar-gregorian-from-absolute day))
9672 (setq s (concat
9673 "Gregorian: " (calendar-date-string date) "\n"
9674 "ISO: " (calendar-iso-date-string date) "\n"
9675 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9676 "Julian: " (calendar-julian-date-string date) "\n"
9677 "Astron. JD: " (calendar-astro-date-string date)
9678 " (Julian date number at noon UTC)\n"
9679 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9680 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9681 "French: " (calendar-french-date-string date) "\n"
9682 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9683 "Mayan: " (calendar-mayan-date-string date) "\n"
9684 "Coptic: " (calendar-coptic-date-string date) "\n"
9685 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9686 "Persian: " (calendar-persian-date-string date) "\n"
9687 "Chinese: " (calendar-chinese-date-string date) "\n"))
9688 (with-output-to-temp-buffer "*Dates*"
9689 (princ s))
9690 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9692 ;;; Bulk commands
9694 (defun org-agenda-bulk-marked-p ()
9695 "Non-nil when current entry is marked for bulk action."
9696 (eq (get-char-property (point-at-bol) 'type)
9697 'org-marked-entry-overlay))
9699 (defun org-agenda-bulk-mark (&optional arg)
9700 "Mark the entry at point for future bulk action."
9701 (interactive "p")
9702 (dotimes (i (or arg 1))
9703 (unless (org-get-at-bol 'org-agenda-diary-link)
9704 (let* ((m (org-get-at-bol 'org-hd-marker))
9706 (unless (org-agenda-bulk-marked-p)
9707 (unless m (user-error "Nothing to mark at point"))
9708 (push m org-agenda-bulk-marked-entries)
9709 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9710 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9711 (org-get-todo-face "TODO")
9712 'evaporate)
9713 (overlay-put ov 'type 'org-marked-entry-overlay))
9714 (end-of-line 1)
9715 (or (ignore-errors
9716 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9717 (beginning-of-line 2))
9718 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9719 (beginning-of-line 2))
9720 (message "%d entries marked for bulk action"
9721 (length org-agenda-bulk-marked-entries))))))
9723 (defun org-agenda-bulk-mark-all ()
9724 "Mark all entries for future agenda bulk action."
9725 (interactive)
9726 (org-agenda-bulk-mark-regexp "."))
9728 (defun org-agenda-bulk-mark-regexp (regexp)
9729 "Mark entries matching REGEXP for future agenda bulk action."
9730 (interactive "sMark entries matching regexp: ")
9731 (let ((entries-marked 0) txt-at-point)
9732 (save-excursion
9733 (goto-char (point-min))
9734 (goto-char (next-single-property-change (point) 'org-hd-marker))
9735 (while (and (re-search-forward regexp nil t)
9736 (setq txt-at-point (get-text-property (point) 'txt)))
9737 (if (get-char-property (point) 'invisible)
9738 (beginning-of-line 2)
9739 (when (string-match regexp txt-at-point)
9740 (setq entries-marked (1+ entries-marked))
9741 (call-interactively 'org-agenda-bulk-mark)))))
9743 (if (not entries-marked)
9744 (message "No entry matching this regexp."))))
9746 (defun org-agenda-bulk-unmark (&optional arg)
9747 "Unmark the entry at point for future bulk action."
9748 (interactive "P")
9749 (if arg
9750 (org-agenda-bulk-unmark-all)
9751 (cond ((org-agenda-bulk-marked-p)
9752 (org-agenda-bulk-remove-overlays
9753 (point-at-bol) (+ 2 (point-at-bol)))
9754 (setq org-agenda-bulk-marked-entries
9755 (delete (org-get-at-bol 'org-hd-marker)
9756 org-agenda-bulk-marked-entries))
9757 (end-of-line 1)
9758 (or (ignore-errors
9759 (goto-char (next-single-property-change (point) 'txt)))
9760 (beginning-of-line 2))
9761 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9762 (beginning-of-line 2))
9763 (message "%d entries left marked for bulk action"
9764 (length org-agenda-bulk-marked-entries)))
9765 (t (message "No entry to unmark here")))))
9767 (defun org-agenda-bulk-toggle-all ()
9768 "Toggle all marks for bulk action."
9769 (interactive)
9770 (save-excursion
9771 (goto-char (point-min))
9772 (while (ignore-errors
9773 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9774 (org-agenda-bulk-toggle))))
9776 (defun org-agenda-bulk-toggle ()
9777 "Toggle the mark at point for bulk action."
9778 (interactive)
9779 (if (org-agenda-bulk-marked-p)
9780 (org-agenda-bulk-unmark)
9781 (org-agenda-bulk-mark)))
9783 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9784 "Remove the mark overlays between BEG and END in the agenda buffer.
9785 BEG and END default to the buffer limits.
9787 This only removes the overlays, it does not remove the markers
9788 from the list in `org-agenda-bulk-marked-entries'."
9789 (interactive)
9790 (mapc (lambda (ov)
9791 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9792 (delete-overlay ov)))
9793 (overlays-in (or beg (point-min)) (or end (point-max)))))
9795 (defun org-agenda-bulk-unmark-all ()
9796 "Remove all marks in the agenda buffer.
9797 This will remove the markers and the overlays."
9798 (interactive)
9799 (if (null org-agenda-bulk-marked-entries)
9800 (message "No entry to unmark")
9801 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9802 (setq org-agenda-bulk-marked-entries nil)
9803 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9805 (defcustom org-agenda-persistent-marks nil
9806 "Non-nil means marked items will stay marked after a bulk action.
9807 You can toggle this interactively by typing `p' when prompted for a
9808 bulk action."
9809 :group 'org-agenda
9810 :version "24.1"
9811 :type 'boolean)
9813 (defun org-agenda-bulk-action (&optional arg)
9814 "Execute an remote-editing action on all marked entries.
9815 The prefix arg is passed through to the command if possible."
9816 (interactive "P")
9817 ;; Make sure we have markers, and only valid ones
9818 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9819 (mapc
9820 (lambda (m)
9821 (unless (and (markerp m)
9822 (marker-buffer m)
9823 (buffer-live-p (marker-buffer m))
9824 (marker-position m))
9825 (user-error "Marker %s for bulk command is invalid" m)))
9826 org-agenda-bulk-marked-entries)
9828 ;; Prompt for the bulk command
9829 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9830 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9831 "[S]catter [f]unction "
9832 (when org-agenda-bulk-custom-functions
9833 (concat " Custom: ["
9834 (mapconcat (lambda(f) (char-to-string (car f)))
9835 org-agenda-bulk-custom-functions "")
9836 "]"))))
9837 (catch 'exit
9838 (let* ((action (read-char-exclusive))
9839 (org-log-refile (if org-log-refile 'time nil))
9840 (entries (reverse org-agenda-bulk-marked-entries))
9841 (org-overriding-default-time
9842 (if (get-text-property (point) 'org-agenda-date-header)
9843 (org-get-cursor-date)))
9844 redo-at-end
9845 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9846 (cond
9847 ((equal action ?p)
9848 (let ((org-agenda-persistent-marks
9849 (not org-agenda-persistent-marks)))
9850 (org-agenda-bulk-action)
9851 (throw 'exit nil)))
9853 ((equal action ?$)
9854 (setq cmd '(org-agenda-archive)))
9856 ((equal action ?A)
9857 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9859 ((member action '(?r ?w))
9860 (setq rfloc (org-refile-get-location
9861 "Refile to"
9862 (marker-buffer (car entries))
9863 org-refile-allow-creating-parent-nodes))
9864 (if (nth 3 rfloc)
9865 (setcar (nthcdr 3 rfloc)
9866 (move-marker (make-marker) (nth 3 rfloc)
9867 (or (get-file-buffer (nth 1 rfloc))
9868 (find-buffer-visiting (nth 1 rfloc))
9869 (error "This should not happen")))))
9871 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9872 redo-at-end t))
9874 ((equal action ?t)
9875 (setq state (completing-read
9876 "Todo state: "
9877 (with-current-buffer (marker-buffer (car entries))
9878 (mapcar #'list org-todo-keywords-1))))
9879 (setq cmd `(let ((org-inhibit-blocking t)
9880 (org-inhibit-logging 'note))
9881 (org-agenda-todo ,state))))
9883 ((memq action '(?- ?+))
9884 (setq tag (completing-read
9885 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9886 (with-current-buffer (marker-buffer (car entries))
9887 (delq nil
9888 (mapcar (lambda (x) (and (stringp (car x)) x))
9889 org-current-tag-alist)))))
9890 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9892 ((memq action '(?s ?d))
9893 (let* ((time
9894 (unless arg
9895 (org-read-date
9896 nil nil nil
9897 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9898 org-overriding-default-time)))
9899 (c1 (if (eq action ?s) 'org-agenda-schedule
9900 'org-agenda-deadline)))
9901 ;; Make sure to not prompt for a note when bulk
9902 ;; rescheduling as Org cannot cope with simultaneous Org.
9903 ;; Besides, it could be annoying depending on the number
9904 ;; of items re-scheduled.
9905 (setq cmd `(eval '(let ((org-log-reschedule
9906 (and org-log-reschedule 'time)))
9907 (,c1 arg ,time))))))
9909 ((equal action ?S)
9910 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9911 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9912 (let ((days (read-number
9913 (format "Scatter tasks across how many %sdays: "
9914 (if arg "week" "")) 7)))
9915 (setq cmd
9916 `(let ((distance (1+ (random ,days))))
9917 (if arg
9918 (let ((dist distance)
9919 (day-of-week
9920 (calendar-day-of-week
9921 (calendar-gregorian-from-absolute (org-today)))))
9922 (dotimes (i (1+ dist))
9923 (while (member day-of-week org-agenda-weekend-days)
9924 (cl-incf distance)
9925 (cl-incf day-of-week)
9926 (when (= day-of-week 7)
9927 (setq day-of-week 0)))
9928 (cl-incf day-of-week)
9929 (when (= day-of-week 7)
9930 (setq day-of-week 0)))))
9931 ;; silently fail when try to replan a sexp entry
9932 (condition-case nil
9933 (let* ((date (calendar-gregorian-from-absolute
9934 (+ (org-today) distance)))
9935 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9936 (nth 2 date))))
9937 (org-agenda-schedule nil time))
9938 (error nil)))))))
9940 ((assoc action org-agenda-bulk-custom-functions)
9941 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9942 redo-at-end t))
9944 ((equal action ?f)
9945 (setq cmd (list (intern
9946 (completing-read "Function: "
9947 obarray 'fboundp t nil nil)))))
9949 (t (user-error "Invalid bulk action")))
9951 ;; Sort the markers, to make sure that parents are handled before children
9952 (setq entries (sort entries
9953 (lambda (a b)
9954 (cond
9955 ((equal (marker-buffer a) (marker-buffer b))
9956 (< (marker-position a) (marker-position b)))
9958 (string< (buffer-name (marker-buffer a))
9959 (buffer-name (marker-buffer b))))))))
9961 ;; Now loop over all markers and apply cmd
9962 (while (setq e (pop entries))
9963 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9964 (if (not pos)
9965 (progn (message "Skipping removed entry at %s" e)
9966 (setq cntskip (1+ cntskip)))
9967 (goto-char pos)
9968 (let (org-loop-over-headlines-in-active-region)
9969 (eval cmd))
9970 ;; `post-command-hook' is not run yet. We make sure any
9971 ;; pending log note is processed.
9972 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9973 (memq 'org-add-log-note post-command-hook))
9974 (org-add-log-note))
9975 (setq cnt (1+ cnt))))
9976 (when redo-at-end (org-agenda-redo))
9977 (unless org-agenda-persistent-marks
9978 (org-agenda-bulk-unmark-all))
9979 (message "Acted on %d entries%s%s"
9981 (if (= cntskip 0)
9983 (format ", skipped %d (disappeared before their turn)"
9984 cntskip))
9985 (if (not org-agenda-persistent-marks)
9986 "" " (kept marked)"))))))
9988 (defun org-agenda-capture (&optional with-time)
9989 "Call `org-capture' with the date at point.
9990 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9991 current HH:MM time."
9992 (interactive "P")
9993 (if (not (eq major-mode 'org-agenda-mode))
9994 (user-error "You cannot do this outside of agenda buffers")
9995 (let ((org-overriding-default-time
9996 (org-get-cursor-date (equal with-time 1))))
9997 (call-interactively 'org-capture))))
9999 ;;; Dragging agenda lines forward/backward
10001 (defun org-agenda-reapply-filters ()
10002 "Re-apply all agenda filters."
10003 (mapcar
10004 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10005 `((,org-agenda-tag-filter tag)
10006 (,org-agenda-category-filter category)
10007 (,org-agenda-regexp-filter regexp)
10008 (,org-agenda-effort-filter effort)
10009 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10010 (,(get 'org-agenda-category-filter :preset-filter) category)
10011 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10012 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10014 (defun org-agenda-drag-line-forward (arg &optional backward)
10015 "Drag an agenda line forward by ARG lines.
10016 When the optional argument `backward' is non-nil, move backward."
10017 (interactive "p")
10018 (let ((inhibit-read-only t) lst line)
10019 (if (or (not (get-text-property (point) 'txt))
10020 (save-excursion
10021 (dotimes (n arg)
10022 (move-beginning-of-line (if backward 0 2))
10023 (push (not (get-text-property (point) 'txt)) lst))
10024 (delq nil lst)))
10025 (message "Cannot move line forward")
10026 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10027 (move-beginning-of-line 1)
10028 (setq line (buffer-substring (point) end))
10029 (delete-region (point) end)
10030 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10031 (insert line)
10032 (org-agenda-reapply-filters)
10033 (org-agenda-mark-clocking-task)
10034 (move-beginning-of-line 0)))))
10036 (defun org-agenda-drag-line-backward (arg)
10037 "Drag an agenda line backward by ARG lines."
10038 (interactive "p")
10039 (org-agenda-drag-line-forward arg t))
10041 ;;; Flagging notes
10043 (defun org-agenda-show-the-flagging-note ()
10044 "Display the flagging note in the other window.
10045 When called a second time in direct sequence, offer to remove the FLAGGING
10046 tag and (if present) the flagging note."
10047 (interactive)
10048 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10049 (win (selected-window))
10050 note heading newhead)
10051 (unless hdmarker
10052 (user-error "No linked entry at point"))
10053 (if (and (eq this-command last-command)
10054 (y-or-n-p "Unflag and remove any flagging note? "))
10055 (progn
10056 (org-agenda-remove-flag hdmarker)
10057 (let ((win (get-buffer-window "*Flagging Note*")))
10058 (and win (delete-window win)))
10059 (message "Entry unflagged"))
10060 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10061 (unless note
10062 (user-error "No flagging note"))
10063 (org-kill-new note)
10064 (org-switch-to-buffer-other-window "*Flagging Note*")
10065 (erase-buffer)
10066 (insert note)
10067 (goto-char (point-min))
10068 (while (re-search-forward "\\\\n" nil t)
10069 (replace-match "\n" t t))
10070 (goto-char (point-min))
10071 (select-window win)
10072 (message "%s" (substitute-command-keys "Flagging note pushed to \
10073 kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
10074 tag and note")))))
10076 (defun org-agenda-remove-flag (marker)
10077 "Remove the FLAGGED tag and any flagging note in the entry."
10078 (let (newhead)
10079 (org-with-point-at marker
10080 (org-toggle-tag "FLAGGED" 'off)
10081 (org-entry-delete nil "THEFLAGGINGNOTE")
10082 (setq newhead (org-get-heading)))
10083 (org-agenda-change-all-lines newhead marker)
10084 (message "Entry unflagged")))
10086 (defun org-agenda-get-any-marker (&optional pos)
10087 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10088 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10090 ;;; Appointment reminders
10092 (defvar appt-time-msg-list) ; defined in appt.el
10094 ;;;###autoload
10095 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10096 "Activate appointments found in `org-agenda-files'.
10098 With a `\\[universal-argument]' prefix, refresh the list of \
10099 appointments.
10101 If FILTER is t, interactively prompt the user for a regular
10102 expression, and filter out entries that don't match it.
10104 If FILTER is a string, use this string as a regular expression
10105 for filtering entries out.
10107 If FILTER is a function, filter out entries against which
10108 calling the function returns nil. This function takes one
10109 argument: an entry from `org-agenda-get-day-entries'.
10111 FILTER can also be an alist with the car of each cell being
10112 either `headline' or `category'. For example:
10114 \\='((headline \"IMPORTANT\")
10115 (category \"Work\"))
10117 will only add headlines containing IMPORTANT or headlines
10118 belonging to the \"Work\" category.
10120 ARGS are symbols indicating what kind of entries to consider.
10121 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10122 \(i.e., deadlines and scheduled items with a hh:mm specification)
10123 and :timestamp entries. See the docstring of `org-diary' for
10124 details and examples.
10126 If an entry has a APPT_WARNTIME property, its value will be used
10127 to override `appt-message-warning-time'."
10128 (interactive "P")
10129 (if refresh (setq appt-time-msg-list nil))
10130 (if (eq filter t)
10131 (setq filter (read-from-minibuffer "Regexp filter: ")))
10132 (let* ((cnt 0) ; count added events
10133 (scope (or args '(:deadline* :scheduled* :timestamp)))
10134 (org-agenda-new-buffers nil)
10135 (org-deadline-warning-days 0)
10136 ;; Do not use `org-today' here because appt only takes
10137 ;; time and without date as argument, so it may pass wrong
10138 ;; information otherwise
10139 (today (org-date-to-gregorian
10140 (time-to-days (current-time))))
10141 (org-agenda-restrict nil)
10142 (files (org-agenda-files 'unrestricted)) entries file
10143 (org-agenda-buffer nil))
10144 ;; Get all entries which may contain an appt
10145 (org-agenda-prepare-buffers files)
10146 (while (setq file (pop files))
10147 (setq entries
10148 (delq nil
10149 (append entries
10150 (apply 'org-agenda-get-day-entries
10151 file today scope)))))
10152 ;; Map thru entries and find if we should filter them out
10153 (mapc
10154 (lambda (x)
10155 (let* ((evt (org-trim
10156 (replace-regexp-in-string
10157 org-bracket-link-regexp "\\3"
10158 (or (get-text-property 1 'txt x) ""))))
10159 (cat (get-text-property (1- (length x)) 'org-category x))
10160 (tod (get-text-property 1 'time-of-day x))
10161 (ok (or (null filter)
10162 (and (stringp filter) (string-match filter evt))
10163 (and (functionp filter) (funcall filter x))
10164 (and (listp filter)
10165 (let ((cat-filter (cadr (assq 'category filter)))
10166 (evt-filter (cadr (assq 'headline filter))))
10167 (or (and (stringp cat-filter)
10168 (string-match cat-filter cat))
10169 (and (stringp evt-filter)
10170 (string-match evt-filter evt)))))))
10171 (wrn (get-text-property 1 'warntime x)))
10172 ;; FIXME: Shall we remove text-properties for the appt text?
10173 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10174 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10175 (setq tod (concat "00" (number-to-string tod)))
10176 (setq tod (when (string-match
10177 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10178 (concat (match-string 1 tod) ":"
10179 (match-string 2 tod))))
10180 (when (if (version< emacs-version "23.3")
10181 (appt-add tod evt)
10182 (appt-add tod evt wrn))
10183 (setq cnt (1+ cnt))))))
10184 entries)
10185 (org-release-buffers org-agenda-new-buffers)
10186 (if (eq cnt 0)
10187 (message "No event to add")
10188 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10190 (defun org-agenda-today-p (date)
10191 "Non nil when DATE means today.
10192 DATE is either a list of the form (month day year) or a number of
10193 days as returned by `calendar-absolute-from-gregorian' or
10194 `org-today'. This function considers `org-extend-today-until'
10195 when defining today."
10196 (eq (org-today)
10197 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10199 (defun org-agenda-todo-yesterday (&optional arg)
10200 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10201 (interactive "P")
10202 (let* ((org-use-effective-time t)
10203 (hour (nth 2 (decode-time (org-current-time))))
10204 (org-extend-today-until (1+ hour)))
10205 (org-agenda-todo arg)))
10207 (provide 'org-agenda)
10209 ;;; org-agenda.el ends here