* ob-vbnet.el: Org-babel functions for VB.Net evaluation
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blobb47f8b6cb95b4f923003fe19ce723295a52f1f36
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (require 'org-macs)
50 (eval-when-compile
51 (require 'cl))
53 (declare-function diary-add-to-list "diary-lib"
54 (date string specifier &optional marker globcolor literal))
55 (declare-function calendar-iso-to-absolute "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-columns-remove-overlays "org-colview" ())
73 (declare-function org-datetree-find-date-create "org-datetree"
74 (date &optional keep-restriction))
75 (declare-function org-columns-quit "org-colview" ())
76 (declare-function diary-date-display-form "diary-lib" (&optional type))
77 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
78 (declare-function org-habit-insert-consistency-graphs
79 "org-habit" (&optional line))
80 (declare-function org-is-habit-p "org-habit" (&optional pom))
81 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
82 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
83 (declare-function org-agenda-columns "org-colview" ())
84 (declare-function org-add-archive-files "org-archive" (files))
85 (declare-function org-capture "org-capture" (&optional goto keys))
87 (defvar calendar-mode-map)
88 (defvar org-clock-current-task)
89 (defvar org-current-tag-alist)
90 (defvar org-mobile-force-id-on-agenda-items)
91 (defvar org-habit-show-habits)
92 (defvar org-habit-show-habits-only-for-today)
93 (defvar org-habit-show-all-today)
95 ;; Defined somewhere in this file, but used before definition.
96 (defvar org-agenda-buffer-name "*Org Agenda*")
97 (defvar org-agenda-overriding-header nil)
98 (defvar org-agenda-title-append nil)
99 (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
100 (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
101 (defvar original-date) ; dynamically scoped, calendar.el does scope this
103 (defvar org-agenda-undo-list nil
104 "List of undoable operations in the agenda since last refresh.")
105 (defvar org-agenda-pending-undo-list nil
106 "In a series of undo commands, this is the list of remaining undo items.")
108 (defcustom org-agenda-confirm-kill 1
109 "When set, remote killing from the agenda buffer needs confirmation.
110 When t, a confirmation is always needed. When a number N, confirmation is
111 only needed when the text to be killed contains more than N non-white lines."
112 :group 'org-agenda
113 :type '(choice
114 (const :tag "Never" nil)
115 (const :tag "Always" t)
116 (integer :tag "When more than N lines")))
118 (defcustom org-agenda-compact-blocks nil
119 "Non-nil means make the block agenda more compact.
120 This is done globally by leaving out lines like the agenda span
121 name and week number or the separator lines."
122 :group 'org-agenda
123 :type 'boolean)
125 (defcustom org-agenda-block-separator ?=
126 "The separator between blocks in the agenda.
127 If this is a string, it will be used as the separator, with a newline added.
128 If it is a character, it will be repeated to fill the window width.
129 If nil the separator is disabled. In `org-agenda-custom-commands' this
130 addresses the separator between the current and the previous block."
131 :group 'org-agenda
132 :type '(choice
133 (const :tag "Disabled" nil)
134 (character)
135 (string)))
137 (defgroup org-agenda-export nil
138 "Options concerning exporting agenda views in Org-mode."
139 :tag "Org Agenda Export"
140 :group 'org-agenda)
142 (defcustom org-agenda-with-colors t
143 "Non-nil means use colors in agenda views."
144 :group 'org-agenda-export
145 :type 'boolean)
147 (defcustom org-agenda-exporter-settings nil
148 "Alist of variable/value pairs that should be active during agenda export.
149 This is a good place to set options for ps-print and for htmlize.
150 Note that the way this is implemented, the values will be evaluated
151 before assigned to the variables. So make sure to quote values you do
152 *not* want evaluated, for example
154 (setq org-agenda-exporter-settings
155 \\='((ps-print-color-p \\='black-white)))"
156 :group 'org-agenda-export
157 :type '(repeat
158 (list
159 (variable)
160 (sexp :tag "Value"))))
162 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
163 "Hook run in a temporary buffer before writing the agenda to an export file.
164 A useful function for this hook is `org-agenda-add-entry-text'."
165 :group 'org-agenda-export
166 :type 'hook
167 :options '(org-agenda-add-entry-text))
169 (defcustom org-agenda-add-entry-text-maxlines 0
170 "Maximum number of entry text lines to be added to agenda.
171 This is only relevant when `org-agenda-add-entry-text' is part of
172 `org-agenda-before-write-hook', which is the default.
173 When this is 0, nothing will happen. When it is greater than 0, it
174 specifies the maximum number of lines that will be added for each entry
175 that is listed in the agenda view.
177 Note that this variable is not used during display, only when exporting
178 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
179 and `org-agenda-entry-text-maxlines'."
180 :group 'org-agenda
181 :type 'integer)
183 (defcustom org-agenda-add-entry-text-descriptive-links t
184 "Non-nil means export org-links as descriptive links in agenda added text.
185 This variable applies to the text added to the agenda when
186 `org-agenda-add-entry-text-maxlines' is larger than 0.
187 When this variable nil, the URL will (also) be shown."
188 :group 'org-agenda
189 :type 'boolean)
191 (defcustom org-agenda-export-html-style nil
192 "The style specification for exported HTML Agenda files.
193 If this variable contains a string, it will replace the default <style>
194 section as produced by `htmlize'.
195 Since there are different ways of setting style information, this variable
196 needs to contain the full HTML structure to provide a style, including the
197 surrounding HTML tags. The style specifications should include definitions
198 the fonts used by the agenda, here is an example:
200 <style type=\"text/css\">
201 p { font-weight: normal; color: gray; }
202 .org-agenda-structure {
203 font-size: 110%;
204 color: #003399;
205 font-weight: 600;
207 .org-todo {
208 color: #cc6666;
209 font-weight: bold;
211 .org-agenda-done {
212 color: #339933;
214 .org-done {
215 color: #339933;
217 .title { text-align: center; }
218 .todo, .deadline { color: red; }
219 .done { color: green; }
220 </style>
222 or, if you want to keep the style in a file,
224 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
226 As the value of this option simply gets inserted into the HTML <head> header,
227 you can \"misuse\" it to also add other text to the header."
228 :group 'org-agenda-export
229 :group 'org-export-html
230 :type '(choice
231 (const nil)
232 (string)))
234 (defcustom org-agenda-persistent-filter nil
235 "When set, keep filters from one agenda view to the next."
236 :group 'org-agenda
237 :type 'boolean)
239 (defgroup org-agenda-custom-commands nil
240 "Options concerning agenda views in Org-mode."
241 :tag "Org Agenda Custom Commands"
242 :group 'org-agenda)
244 (defconst org-sorting-choice
245 '(choice
246 (const time-up) (const time-down)
247 (const timestamp-up) (const timestamp-down)
248 (const scheduled-up) (const scheduled-down)
249 (const deadline-up) (const deadline-down)
250 (const ts-up) (const ts-down)
251 (const tsia-up) (const tsia-down)
252 (const category-keep) (const category-up) (const category-down)
253 (const tag-down) (const tag-up)
254 (const priority-up) (const priority-down)
255 (const todo-state-up) (const todo-state-down)
256 (const effort-up) (const effort-down)
257 (const habit-up) (const habit-down)
258 (const alpha-up) (const alpha-down)
259 (const user-defined-up) (const user-defined-down))
260 "Sorting choices.")
262 ;; Keep custom values for `org-agenda-filter-preset' compatible with
263 ;; the new variable `org-agenda-tag-filter-preset'.
264 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
265 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
267 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
268 "List of types searched for when creating the daily/weekly agenda.
269 This variable is a list of symbols that controls the types of
270 items that appear in the daily/weekly agenda. Allowed symbols in this
271 list are are
273 :timestamp List items containing a date stamp or date range matching
274 the selected date. This includes sexp entries in angular
275 brackets.
277 :sexp List entries resulting from plain diary-like sexps.
279 :deadline List deadline due on that date. When the date is today,
280 also list any deadlines past due, or due within
281 `org-deadline-warning-days'. `:deadline' must appear before
282 `:scheduled' if the setting of
283 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
284 any effect.
286 :deadline* Same as above, but only include the deadline if it has an
287 hour specification as [h]h:mm.
289 :scheduled List all items which are scheduled for the given date.
290 The diary for *today* also contains items which were
291 scheduled earlier and are not yet marked DONE.
293 :scheduled* Same as above, but only include the scheduled item if it
294 has an hour specification as [h]h:mm.
296 By default, all four non-starred types are turned on.
298 When :scheduled* or :deadline* are included, :schedule or :deadline
299 will be ignored.
301 Never set this variable globally using `setq', because then it
302 will apply to all future agenda commands. Instead, bind it with
303 `let' to scope it dynamically into the agenda-constructing
304 command. A good way to set it is through options in
305 `org-agenda-custom-commands'. For a more flexible (though
306 somewhat less efficient) way of determining what is included in
307 the daily/weekly agenda, see `org-agenda-skip-function'.")
309 (defconst org-agenda-custom-commands-local-options
310 `(repeat :tag "Local settings for this command. Remember to quote values"
311 (choice :tag "Setting"
312 (list :tag "Heading for this block"
313 (const org-agenda-overriding-header)
314 (string :tag "Headline"))
315 (list :tag "Files to be searched"
316 (const org-agenda-files)
317 (list
318 (const :format "" quote)
319 (repeat (file))))
320 (list :tag "Sorting strategy"
321 (const org-agenda-sorting-strategy)
322 (list
323 (const :format "" quote)
324 (repeat
325 ,org-sorting-choice)))
326 (list :tag "Prefix format"
327 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
328 (string))
329 (list :tag "Number of days in agenda"
330 (const org-agenda-span)
331 (choice (const :tag "Day" day)
332 (const :tag "Week" week)
333 (const :tag "Fortnight" fortnight)
334 (const :tag "Month" month)
335 (const :tag "Year" year)
336 (integer :tag "Custom")))
337 (list :tag "Fixed starting date"
338 (const org-agenda-start-day)
339 (string :value "2007-11-01"))
340 (list :tag "Start on day of week"
341 (const org-agenda-start-on-weekday)
342 (choice :value 1
343 (const :tag "Today" nil)
344 (integer :tag "Weekday No.")))
345 (list :tag "Include data from diary"
346 (const org-agenda-include-diary)
347 (boolean))
348 (list :tag "Deadline Warning days"
349 (const org-deadline-warning-days)
350 (integer :value 1))
351 (list :tag "Category filter preset"
352 (const org-agenda-category-filter-preset)
353 (list
354 (const :format "" quote)
355 (repeat
356 (string :tag "+category or -category"))))
357 (list :tag "Tags filter preset"
358 (const org-agenda-tag-filter-preset)
359 (list
360 (const :format "" quote)
361 (repeat
362 (string :tag "+tag or -tag"))))
363 (list :tag "Effort filter preset"
364 (const org-agenda-effort-filter-preset)
365 (list
366 (const :format "" quote)
367 (repeat
368 (string :tag "+=10 or -=10 or +<10 or ->10"))))
369 (list :tag "Regexp filter preset"
370 (const org-agenda-regexp-filter-preset)
371 (list
372 (const :format "" quote)
373 (repeat
374 (string :tag "+regexp or -regexp"))))
375 (list :tag "Set daily/weekly entry types"
376 (const org-agenda-entry-types)
377 (list
378 (const :format "" quote)
379 (set :greedy t :value ,org-agenda-entry-types
380 (const :deadline)
381 (const :scheduled)
382 (const :deadline*)
383 (const :scheduled*)
384 (const :timestamp)
385 (const :sexp))))
386 (list :tag "Standard skipping condition"
387 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
388 (const org-agenda-skip-function)
389 (list
390 (const :format "" quote)
391 (list
392 (choice
393 :tag "Skipping range"
394 (const :tag "Skip entry" org-agenda-skip-entry-if)
395 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
396 (repeat :inline t :tag "Conditions for skipping"
397 (choice
398 :tag "Condition type"
399 (list :tag "Regexp matches" :inline t
400 (const :format "" 'regexp)
401 (regexp))
402 (list :tag "Regexp does not match" :inline t
403 (const :format "" 'notregexp)
404 (regexp))
405 (list :tag "TODO state is" :inline t
406 (const 'todo)
407 (choice
408 (const :tag "Any not-done state" 'todo)
409 (const :tag "Any done state" 'done)
410 (const :tag "Any state" 'any)
411 (list :tag "Keyword list"
412 (const :format "" quote)
413 (repeat (string :tag "Keyword")))))
414 (list :tag "TODO state is not" :inline t
415 (const 'nottodo)
416 (choice
417 (const :tag "Any not-done state" 'todo)
418 (const :tag "Any done state" 'done)
419 (const :tag "Any state" 'any)
420 (list :tag "Keyword list"
421 (const :format "" quote)
422 (repeat (string :tag "Keyword")))))
423 (const :tag "scheduled" 'scheduled)
424 (const :tag "not scheduled" 'notscheduled)
425 (const :tag "deadline" 'deadline)
426 (const :tag "no deadline" 'notdeadline)
427 (const :tag "timestamp" 'timestamp)
428 (const :tag "no timestamp" 'nottimestamp))))))
429 (list :tag "Non-standard skipping condition"
430 :value (org-agenda-skip-function)
431 (const org-agenda-skip-function)
432 (sexp :tag "Function or form (quoted!)"))
433 (list :tag "Any variable"
434 (variable :tag "Variable")
435 (sexp :tag "Value (sexp)"))))
436 "Selection of examples for agenda command settings.
437 This will be spliced into the custom type of
438 `org-agenda-custom-commands'.")
441 (defcustom org-agenda-custom-commands
442 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
443 "Custom commands for the agenda.
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 or `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 TODO keyword any of" (string))
616 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
617 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
619 (defgroup org-agenda-skip nil
620 "Options concerning skipping parts of agenda files."
621 :tag "Org Agenda Skip"
622 :group 'org-agenda)
624 (defcustom org-agenda-skip-function-global nil
625 "Function to be called at each match during agenda construction.
626 If this function returns nil, the current match should not be skipped.
627 If the function decided to skip an agenda match, is must return the
628 buffer position from which the search should be continued.
629 This may also be a Lisp form, which will be evaluated.
631 This variable will be applied to every agenda match, including
632 tags/property searches and TODO lists. So try to make the test function
633 do its checking as efficiently as possible. To implement a skipping
634 condition just for specific agenda commands, use the variable
635 `org-agenda-skip-function' which can be set in the options section
636 of custom agenda commands."
637 :group 'org-agenda-skip
638 :type 'sexp)
640 (defgroup org-agenda-daily/weekly nil
641 "Options concerning the daily/weekly agenda."
642 :tag "Org Agenda Daily/Weekly"
643 :group 'org-agenda)
644 (defgroup org-agenda-todo-list nil
645 "Options concerning the global todo list agenda view."
646 :tag "Org Agenda Todo List"
647 :group 'org-agenda)
648 (defgroup org-agenda-match-view nil
649 "Options concerning the general tags/property/todo match agenda view."
650 :tag "Org Agenda Match View"
651 :group 'org-agenda)
652 (defgroup org-agenda-search-view nil
653 "Options concerning the search agenda view."
654 :tag "Org Agenda Search View"
655 :group 'org-agenda)
657 (defvar org-agenda-archives-mode nil
658 "Non-nil means the agenda will include archived items.
659 If this is the symbol `trees', trees in the selected agenda scope
660 that are marked with the ARCHIVE tag will be included anyway. When this is
661 t, also all archive files associated with the current selection of agenda
662 files will be included.")
664 (defcustom org-agenda-restriction-lock-highlight-subtree t
665 "Non-nil means highlight the whole subtree when restriction is active.
666 Otherwise only highlight the headline. Highlighting the whole subtree is
667 useful to ensure no edits happen beyond the restricted region."
668 :group 'org-agenda
669 :type 'boolean)
671 (defcustom org-agenda-skip-comment-trees t
672 "Non-nil means skip trees that start with the COMMENT keyword.
673 When nil, these trees are also scanned by agenda commands."
674 :group 'org-agenda-skip
675 :type 'boolean)
677 (defcustom org-agenda-todo-list-sublevels t
678 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
679 When nil, the sublevels of a TODO entry are not checked, resulting in
680 potentially much shorter TODO lists."
681 :group 'org-agenda-skip
682 :group 'org-agenda-todo-list
683 :type 'boolean)
685 (defcustom org-agenda-todo-ignore-with-date nil
686 "Non-nil means don't show entries with a date in the global todo list.
687 You can use this if you prefer to mark mere appointments with a TODO keyword,
688 but don't want them to show up in the TODO list.
689 When this is set, it also covers deadlines and scheduled items, the settings
690 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
691 will be ignored.
692 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
693 :group 'org-agenda-skip
694 :group 'org-agenda-todo-list
695 :type 'boolean)
697 (defcustom org-agenda-todo-ignore-timestamp nil
698 "Non-nil means don't show entries with a timestamp.
699 This applies when creating the global todo list.
700 Valid values are:
702 past Don't show entries for today or in the past.
704 future Don't show entries with a timestamp in the future.
705 The idea behind this is that if it has a future
706 timestamp, you don't want to think about it until the
707 date.
709 all Don't show any entries with a timestamp in the global todo list.
710 The idea behind this is that by setting a timestamp, you
711 have already \"taken care\" of this item.
713 This variable can also have an integer as a value. If positive (N),
714 todos with a timestamp N or more days in the future will be ignored. If
715 negative (-N), todos with a timestamp N or more days in the past will be
716 ignored. If 0, todos with a timestamp either today or in the future will
717 be ignored. For example, a value of -1 will exclude todos with a
718 timestamp in the past (yesterday or earlier), while a value of 7 will
719 exclude todos with a timestamp a week or more in the future.
721 See also `org-agenda-todo-ignore-with-date'.
722 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
723 to make his option also apply to the tags-todo list."
724 :group 'org-agenda-skip
725 :group 'org-agenda-todo-list
726 :version "24.1"
727 :type '(choice
728 (const :tag "Ignore future timestamp todos" future)
729 (const :tag "Ignore past or present timestamp todos" past)
730 (const :tag "Ignore all timestamp todos" all)
731 (const :tag "Show timestamp todos" nil)
732 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
734 (defcustom org-agenda-todo-ignore-scheduled nil
735 "Non-nil means, ignore some scheduled TODO items when making TODO list.
736 This applies when creating the global todo list.
737 Valid values are:
739 past Don't show entries scheduled today or in the past.
741 future Don't show entries scheduled in the future.
742 The idea behind this is that by scheduling it, you don't want to
743 think about it until the scheduled date.
745 all Don't show any scheduled entries in the global todo list.
746 The idea behind this is that by scheduling it, you have already
747 \"taken care\" of this item.
749 t Same as `all', for backward compatibility.
751 This variable can also have an integer as a value. See
752 `org-agenda-todo-ignore-timestamp' for more details.
754 See also `org-agenda-todo-ignore-with-date'.
755 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
756 to make his option also apply to the tags-todo list."
757 :group 'org-agenda-skip
758 :group 'org-agenda-todo-list
759 :type '(choice
760 (const :tag "Ignore future-scheduled todos" future)
761 (const :tag "Ignore past- or present-scheduled todos" past)
762 (const :tag "Ignore all scheduled todos" all)
763 (const :tag "Ignore all scheduled todos (compatibility)" t)
764 (const :tag "Show scheduled todos" nil)
765 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
767 (defcustom org-agenda-todo-ignore-deadlines nil
768 "Non-nil means ignore some deadline TODO items when making TODO list.
769 There are different motivations for using different values, please think
770 carefully when configuring this variable.
772 This applies when creating the global todo list.
773 Valid values are:
775 near Don't show near deadline entries. A deadline is near when it is
776 closer than `org-deadline-warning-days' days. The idea behind this
777 is that such items will appear in the agenda anyway.
779 far Don't show TODO entries where a deadline has been defined, but
780 the deadline is not near. This is useful if you don't want to
781 use the todo list to figure out what to do now.
783 past Don't show entries with a deadline timestamp for today or in the past.
785 future Don't show entries with a deadline timestamp in the future, not even
786 when they become `near' ones. Use it with caution.
788 all Ignore all TODO entries that do have a deadline.
790 t Same as `near', for backward compatibility.
792 This variable can also have an integer as a value. See
793 `org-agenda-todo-ignore-timestamp' for more details.
795 See also `org-agenda-todo-ignore-with-date'.
796 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
797 to make his option also apply to the tags-todo list."
798 :group 'org-agenda-skip
799 :group 'org-agenda-todo-list
800 :type '(choice
801 (const :tag "Ignore near deadlines" near)
802 (const :tag "Ignore near deadlines (compatibility)" t)
803 (const :tag "Ignore far deadlines" far)
804 (const :tag "Ignore all TODOs with a deadlines" all)
805 (const :tag "Show all TODOs, even if they have a deadline" nil)
806 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
808 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
809 "Time unit to use when possibly ignoring an agenda item.
811 See the docstring of various `org-agenda-todo-ignore-*' options.
812 The default is to compare time stamps using days. An item is thus
813 considered to be in the future if it is at least one day after today.
814 Non-nil means to compare time stamps using seconds. An item is then
815 considered future if it has a time value later than current time."
816 :group 'org-agenda-skip
817 :group 'org-agenda-todo-list
818 :version "24.4"
819 :package-version '(Org . "8.0")
820 :type '(choice
821 (const :tag "Compare time with days" nil)
822 (const :tag "Compare time with seconds" t)))
824 (defcustom org-agenda-tags-todo-honor-ignore-options nil
825 "Non-nil means honor todo-list ignores options also in tags-todo search.
826 The variables
827 `org-agenda-todo-ignore-with-date',
828 `org-agenda-todo-ignore-timestamp',
829 `org-agenda-todo-ignore-scheduled',
830 `org-agenda-todo-ignore-deadlines'
831 make the global TODO list skip entries that have time stamps of certain
832 kinds. If this option is set, the same options will also apply for the
833 tags-todo search, which is the general tags/property matcher
834 restricted to unfinished TODO entries only."
835 :group 'org-agenda-skip
836 :group 'org-agenda-todo-list
837 :group 'org-agenda-match-view
838 :type 'boolean)
840 (defcustom org-agenda-skip-scheduled-if-done nil
841 "Non-nil means don't show scheduled items in agenda when they are done.
842 This is relevant for the daily/weekly agenda, not for the TODO list. And
843 it applies only to the actual date of the scheduling. Warnings about
844 an item with a past scheduling dates are always turned off when the item
845 is DONE."
846 :group 'org-agenda-skip
847 :group 'org-agenda-daily/weekly
848 :type 'boolean)
850 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
851 "Non-nil means skip scheduling line if same entry shows because of deadline.
853 In the agenda of today, an entry can show up multiple times
854 because it is both scheduled and has a nearby deadline, and maybe
855 a plain time stamp as well.
857 When this variable is nil, the entry will be shown several times.
859 When set to t, then only the deadline is shown and the fact that
860 the entry is scheduled today or was scheduled previously is not
861 shown.
863 When set to the symbol `not-today', skip scheduled previously,
864 but not scheduled today.
866 When set to the symbol `repeated-after-deadline', skip scheduled
867 items if they are repeated beyond the current deadline."
868 :group 'org-agenda-skip
869 :group 'org-agenda-daily/weekly
870 :type '(choice
871 (const :tag "Never" nil)
872 (const :tag "Always" t)
873 (const :tag "Not when scheduled today" not-today)
874 (const :tag "When repeated past deadline" repeated-after-deadline)))
876 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
877 "Non-nil means skip timestamp line if same entry shows because of deadline.
878 In the agenda of today, an entry can show up multiple times
879 because it has both a plain timestamp and has a nearby deadline.
880 When this variable is t, then only the deadline is shown and the
881 fact that the entry has a timestamp for or including today is not
882 shown. When this variable is nil, the entry will be shown
883 several times."
884 :group 'org-agenda-skip
885 :group 'org-agenda-daily/weekly
886 :version "24.1"
887 :type '(choice
888 (const :tag "Never" nil)
889 (const :tag "Always" t)))
891 (defcustom org-agenda-skip-deadline-if-done nil
892 "Non-nil means don't show deadlines when the corresponding item is done.
893 When nil, the deadline is still shown and should give you a happy feeling.
894 This is relevant for the daily/weekly agenda. And it applied only to the
895 actually date of the deadline. Warnings about approaching and past-due
896 deadlines are always turned off when the item is DONE."
897 :group 'org-agenda-skip
898 :group 'org-agenda-daily/weekly
899 :type 'boolean)
901 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
902 "Non-nil means skip deadline prewarning when entry is also scheduled.
903 This will apply on all days where a prewarning for the deadline would
904 be shown, but not at the day when the entry is actually due. On that day,
905 the deadline will be shown anyway.
906 This variable may be set to nil, t, the symbol `pre-scheduled',
907 or a number which will then give the number of days before the actual
908 deadline when the prewarnings should resume. The symbol `pre-scheduled'
909 eliminates the deadline prewarning only prior to the scheduled date.
910 This can be used in a workflow where the first showing of the deadline will
911 trigger you to schedule it, and then you don't want to be reminded of it
912 because you will take care of it on the day when scheduled."
913 :group 'org-agenda-skip
914 :group 'org-agenda-daily/weekly
915 :version "24.1"
916 :type '(choice
917 (const :tag "Always show prewarning" nil)
918 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
919 (const :tag "Remove prewarning if entry is scheduled" t)
920 (integer :tag "Restart prewarning N days before deadline")))
922 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
923 "Non-nil means skip scheduled delay when entry also has a deadline.
924 This variable may be set to nil, t, the symbol `post-deadline',
925 or a number which will then give the number of days after the actual
926 scheduled date when the delay should expire. The symbol `post-deadline'
927 eliminates the schedule delay when the date is posterior to the deadline."
928 :group 'org-agenda-skip
929 :group 'org-agenda-daily/weekly
930 :version "24.4"
931 :package-version '(Org . "8.0")
932 :type '(choice
933 (const :tag "Always honor delay" nil)
934 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
935 (const :tag "Ignore delay if entry has a deadline" t)
936 (integer :tag "Honor delay up until N days after the scheduled date")))
938 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
939 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
940 When non-nil, after the search for timestamps has matched once in an
941 entry, the rest of the entry will not be searched."
942 :group 'org-agenda-skip
943 :type 'boolean)
945 (defcustom org-agenda-skip-timestamp-if-done nil
946 "Non-nil means don't select item by timestamp or -range if it is DONE."
947 :group 'org-agenda-skip
948 :group 'org-agenda-daily/weekly
949 :type 'boolean)
951 (defcustom org-agenda-dim-blocked-tasks t
952 "Non-nil means dim blocked tasks in the agenda display.
953 This causes some overhead during agenda construction, but if you
954 have turned on `org-enforce-todo-dependencies',
955 `org-enforce-todo-checkbox-dependencies', or any other blocking
956 mechanism, this will create useful feedback in the agenda.
958 Instead of t, this variable can also have the value `invisible'.
959 Then blocked tasks will be invisible and only become visible when
960 they become unblocked. An exemption to this behavior is when a task is
961 blocked because of unchecked checkboxes below it. Since checkboxes do
962 not show up in the agenda views, making this task invisible you remove any
963 trace from agenda views that there is something to do. Therefore, a task
964 that is blocked because of checkboxes will never be made invisible, it
965 will only be dimmed."
966 :group 'org-agenda-daily/weekly
967 :group 'org-agenda-todo-list
968 :version "24.3"
969 :type '(choice
970 (const :tag "Do not dim" nil)
971 (const :tag "Dim to a gray face" t)
972 (const :tag "Make invisible" invisible)))
974 (defcustom org-timeline-show-empty-dates 3
975 "Non-nil means `org-timeline' also shows dates without an entry.
976 When nil, only the days which actually have entries are shown.
977 When t, all days between the first and the last date are shown.
978 When an integer, show also empty dates, but if there is a gap of more than
979 N days, just insert a special line indicating the size of the gap."
980 :group 'org-agenda-skip
981 :type '(choice
982 (const :tag "None" nil)
983 (const :tag "All" t)
984 (integer :tag "at most")))
986 (defgroup org-agenda-startup nil
987 "Options concerning initial settings in the Agenda in Org Mode."
988 :tag "Org Agenda Startup"
989 :group 'org-agenda)
991 (defcustom org-agenda-menu-show-matcher t
992 "Non-nil means show the match string in the agenda dispatcher menu.
993 When nil, the matcher string is not shown, but is put into the help-echo
994 property so than moving the mouse over the command shows it.
995 Setting it to nil is good if matcher strings are very long and/or if
996 you want to use two-columns display (see `org-agenda-menu-two-columns')."
997 :group 'org-agenda
998 :version "24.1"
999 :type 'boolean)
1001 (defcustom org-agenda-menu-two-columns nil
1002 "Non-nil means, use two columns to show custom commands in the dispatcher.
1003 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1004 to nil."
1005 :group 'org-agenda
1006 :version "24.1"
1007 :type 'boolean)
1009 (defcustom org-agenda-finalize-hook nil
1010 "Hook run just before displaying an agenda buffer.
1011 The buffer is still writable when the hook is called.
1013 You can modify some of the buffer substrings but you should be
1014 extra careful not to modify the text properties of the agenda
1015 headlines as the agenda display heavily relies on them."
1016 :group 'org-agenda-startup
1017 :type 'hook)
1019 (defcustom org-agenda-mouse-1-follows-link nil
1020 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1021 A longer mouse click will still set point. Needs to be set
1022 before org.el is loaded."
1023 :group 'org-agenda-startup
1024 :type 'boolean)
1026 (defcustom org-agenda-start-with-follow-mode nil
1027 "The initial value of follow mode in a newly created agenda window."
1028 :group 'org-agenda-startup
1029 :type 'boolean)
1031 (defcustom org-agenda-follow-indirect nil
1032 "Non-nil means `org-agenda-follow-mode' displays only the
1033 current item's tree, in an indirect buffer."
1034 :group 'org-agenda
1035 :version "24.1"
1036 :type 'boolean)
1038 (defcustom org-agenda-show-outline-path t
1039 "Non-nil means show outline path in echo area after line motion."
1040 :group 'org-agenda-startup
1041 :type 'boolean)
1043 (defcustom org-agenda-start-with-entry-text-mode nil
1044 "The initial value of entry-text-mode in a newly created agenda window."
1045 :group 'org-agenda-startup
1046 :type 'boolean)
1048 (defcustom org-agenda-entry-text-maxlines 5
1049 "Number of text lines to be added when `E' is pressed in the agenda.
1051 Note that this variable only used during agenda display. Add add entry text
1052 when exporting the agenda, configure the variable
1053 `org-agenda-add-entry-ext-maxlines'."
1054 :group 'org-agenda
1055 :type 'integer)
1057 (defcustom org-agenda-entry-text-exclude-regexps nil
1058 "List of regular expressions to clean up entry text.
1059 The complete matches of all regular expressions in this list will be
1060 removed from entry text before it is shown in the agenda."
1061 :group 'org-agenda
1062 :type '(repeat (regexp)))
1064 (defcustom org-agenda-entry-text-leaders " > "
1065 "Text prepended to the entry text in agenda buffers."
1066 :version "24.4"
1067 :package-version '(Org . "8.0")
1068 :group 'org-agenda
1069 :type 'string)
1071 (defvar org-agenda-entry-text-cleanup-hook nil
1072 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1073 This cleanup is done in a temporary buffer, so the function may inspect and
1074 change the entire buffer.
1075 Some default stuff like drawers and scheduling/deadline dates will already
1076 have been removed when this is called, as will any matches for regular
1077 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1079 (defvar org-agenda-include-inactive-timestamps nil
1080 "Non-nil means include inactive time stamps in agenda and timeline.
1081 Dynamically scoped.")
1083 (defgroup org-agenda-windows nil
1084 "Options concerning the windows used by the Agenda in Org Mode."
1085 :tag "Org Agenda Windows"
1086 :group 'org-agenda)
1088 (defcustom org-agenda-window-setup 'reorganize-frame
1089 "How the agenda buffer should be displayed.
1090 Possible values for this option are:
1092 current-window Show agenda in the current window, keeping all other windows.
1093 other-window Use `switch-to-buffer-other-window' to display agenda.
1094 only-window Show agenda, deleting all other windows.
1095 reorganize-frame Show only two windows on the current frame, the current
1096 window and the agenda.
1097 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1098 Also, when exiting the agenda, kill that frame.
1099 See also the variable `org-agenda-restore-windows-after-quit'."
1100 :group 'org-agenda-windows
1101 :type '(choice
1102 (const current-window)
1103 (const other-frame)
1104 (const other-window)
1105 (const only-window)
1106 (const reorganize-frame)))
1108 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1109 "The min and max height of the agenda window as a fraction of frame height.
1110 The value of the variable is a cons cell with two numbers between 0 and 1.
1111 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1112 :group 'org-agenda-windows
1113 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1115 (defcustom org-agenda-restore-windows-after-quit nil
1116 "Non-nil means restore window configuration upon exiting agenda.
1117 Before the window configuration is changed for displaying the agenda,
1118 the current status is recorded. When the agenda is exited with
1119 `q' or `x' and this option is set, the old state is restored. If
1120 `org-agenda-window-setup' is `other-frame', the value of this
1121 option will be ignored."
1122 :group 'org-agenda-windows
1123 :type 'boolean)
1125 (defcustom org-agenda-span 'week
1126 "Number of days to include in overview display.
1127 Can be day, week, month, year, or any number of days.
1128 Custom commands can set this variable in the options section."
1129 :group 'org-agenda-daily/weekly
1130 :type '(choice (const :tag "Day" day)
1131 (const :tag "Week" week)
1132 (const :tag "Fortnight" fortnight)
1133 (const :tag "Month" month)
1134 (const :tag "Year" year)
1135 (integer :tag "Custom")))
1137 (defcustom org-agenda-start-on-weekday 1
1138 "Non-nil means start the overview always on the specified weekday.
1139 0 denotes Sunday, 1 denotes Monday, etc.
1140 When nil, always start on the current day.
1141 Custom commands can set this variable in the options section."
1142 :group 'org-agenda-daily/weekly
1143 :type '(choice (const :tag "Today" nil)
1144 (integer :tag "Weekday No.")))
1146 (defcustom org-agenda-show-all-dates t
1147 "Non-nil means `org-agenda' shows every day in the selected range.
1148 When nil, only the days which actually have entries are shown."
1149 :group 'org-agenda-daily/weekly
1150 :type 'boolean)
1152 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1153 "Format string for displaying dates in the agenda.
1154 Used by the daily/weekly agenda and by the timeline. This should be
1155 a format string understood by `format-time-string', or a function returning
1156 the formatted date as a string. The function must take a single argument,
1157 a calendar-style date list like (month day year)."
1158 :group 'org-agenda-daily/weekly
1159 :type '(choice
1160 (string :tag "Format string")
1161 (function :tag "Function")))
1163 (defun org-agenda-format-date-aligned (date)
1164 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1165 This function makes sure that dates are aligned for easy reading."
1166 (require 'cal-iso)
1167 (let* ((dayname (calendar-day-name date))
1168 (day (cadr date))
1169 (day-of-week (calendar-day-of-week date))
1170 (month (car date))
1171 (monthname (calendar-month-name month))
1172 (year (nth 2 date))
1173 (iso-week (org-days-to-iso-week
1174 (calendar-absolute-from-gregorian date)))
1175 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1176 (1- year))
1177 ((and (= month 12) (<= iso-week 1))
1178 (1+ year))
1179 (t year)))
1180 (weekstring (if (= day-of-week 1)
1181 (format " W%02d" iso-week)
1182 "")))
1183 (format "%-10s %2d %s %4d%s"
1184 dayname day monthname year weekstring)))
1186 (defcustom org-agenda-time-leading-zero nil
1187 "Non-nil means use leading zero for military times in agenda.
1188 For example, 9:30am would become 09:30 rather than 9:30."
1189 :group 'org-agenda-daily/weekly
1190 :version "24.1"
1191 :type 'boolean)
1193 (defcustom org-agenda-timegrid-use-ampm nil
1194 "When set, show AM/PM style timestamps on the timegrid."
1195 :group 'org-agenda
1196 :version "24.1"
1197 :type 'boolean)
1199 (defun org-agenda-time-of-day-to-ampm (time)
1200 "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
1201 (let* ((hour-number (string-to-number (substring time 0 -3)))
1202 (minute (substring time -2))
1203 (ampm "am"))
1204 (cond
1205 ((equal hour-number 12)
1206 (setq ampm "pm"))
1207 ((> hour-number 12)
1208 (setq ampm "pm")
1209 (setq hour-number (- hour-number 12))))
1210 (concat
1211 (if org-agenda-time-leading-zero
1212 (format "%02d" hour-number)
1213 (format "%02s" (number-to-string hour-number)))
1214 ":" minute ampm)))
1216 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1217 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1218 (if org-agenda-timegrid-use-ampm
1219 (org-agenda-time-of-day-to-ampm time)
1220 time))
1222 (defcustom org-agenda-weekend-days '(6 0)
1223 "Which days are weekend?
1224 These days get the special face `org-agenda-date-weekend' in the agenda
1225 and timeline buffers."
1226 :group 'org-agenda-daily/weekly
1227 :type '(set :greedy t
1228 (const :tag "Monday" 1)
1229 (const :tag "Tuesday" 2)
1230 (const :tag "Wednesday" 3)
1231 (const :tag "Thursday" 4)
1232 (const :tag "Friday" 5)
1233 (const :tag "Saturday" 6)
1234 (const :tag "Sunday" 0)))
1236 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1237 "Non-nil means jump to today when moving a past date forward in time.
1238 When using S-right in the agenda to move a a date forward, and the date
1239 stamp currently points to the past, the first key press will move it
1240 to today. WHen nil, just move one day forward even if the date stays
1241 in the past."
1242 :group 'org-agenda-daily/weekly
1243 :version "24.1"
1244 :type 'boolean)
1246 (defcustom org-agenda-include-diary nil
1247 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1248 Custom commands can set this variable in the options section."
1249 :group 'org-agenda-daily/weekly
1250 :type 'boolean)
1252 (defcustom org-agenda-include-deadlines t
1253 "If non-nil, include entries within their deadline warning period.
1254 Custom commands can set this variable in the options section."
1255 :group 'org-agenda-daily/weekly
1256 :version "24.1"
1257 :type 'boolean)
1259 (defcustom org-agenda-repeating-timestamp-show-all t
1260 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1261 When set to a list of strings, only show occurrences of repeating
1262 stamps for these TODO keywords. When nil, only one occurrence is
1263 shown, either today or the nearest into the future."
1264 :group 'org-agenda-daily/weekly
1265 :type '(choice
1266 (const :tag "Show repeating stamps" t)
1267 (repeat :tag "Show repeating stamps for these TODO keywords"
1268 (string :tag "TODO Keyword"))
1269 (const :tag "Don't show repeating stamps" nil)))
1271 (defcustom org-scheduled-past-days 10000
1272 "Number of days to continue listing scheduled items not marked DONE.
1273 When an item is scheduled on a date, it shows up in the agenda on
1274 this day and will be listed until it is marked done or for the
1275 number of days given here."
1276 :group 'org-agenda-daily/weekly
1277 :type 'integer)
1279 (defcustom org-agenda-log-mode-items '(closed clock)
1280 "List of items that should be shown in agenda log mode.
1281 \\<org-agenda-mode-map>\
1282 This list may contain the following symbols:
1284 closed Show entries that have been closed on that day.
1285 clock Show entries that have received clocked time on that day.
1286 state Show all logged state changes.
1287 Note that instead of changing this variable, you can also press \
1288 `\\[universal-argument] \\[org-agenda-log-mode]' in
1289 the agenda to display all available LOG items temporarily."
1290 :group 'org-agenda-daily/weekly
1291 :type '(set :greedy t (const closed) (const clock) (const state)))
1293 (defcustom org-agenda-clock-consistency-checks
1294 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1295 :gap-ok-around ("4:00")
1296 :default-face ((:background "DarkRed") (:foreground "white"))
1297 :overlap-face nil :gap-face nil :no-end-time-face nil
1298 :long-face nil :short-face nil)
1299 "This is a property list, with the following keys:
1301 :max-duration Mark clocking chunks that are longer than this time.
1302 This is a time string like \"HH:MM\", or the number
1303 of minutes as an integer.
1305 :min-duration Mark clocking chunks that are shorter that this.
1306 This is a time string like \"HH:MM\", or the number
1307 of minutes as an integer.
1309 :max-gap Mark gaps between clocking chunks that are longer than
1310 this duration. A number of minutes, or a string
1311 like \"HH:MM\".
1313 :gap-ok-around List of times during the day which are usually not working
1314 times. When a gap is detected, but the gap contains any
1315 of these times, the gap is *not* reported. For example,
1316 if this is (\"4:00\" \"13:00\") then gaps that contain
1317 4:00 in the morning (i.e. the night) and 13:00
1318 (i.e. a typical lunch time) do not cause a warning.
1319 You should have at least one time during the night in this
1320 list, or otherwise the first task each morning will trigger
1321 a warning because it follows a long gap.
1323 Furthermore, the following properties can be used to define faces for
1324 issue display.
1326 :default-face the default face, if the specific face is undefined
1327 :overlap-face face for overlapping clocks
1328 :gap-face face for gaps between clocks
1329 :no-end-time-face face for incomplete clocks
1330 :long-face face for clock intervals that are too long
1331 :short-face face for clock intervals that are too short"
1332 :group 'org-agenda-daily/weekly
1333 :group 'org-clock
1334 :version "24.1"
1335 :type 'plist)
1337 (defcustom org-agenda-log-mode-add-notes t
1338 "Non-nil means add first line of notes to log entries in agenda views.
1339 If a log item like a state change or a clock entry is associated with
1340 notes, the first line of these notes will be added to the entry in the
1341 agenda display."
1342 :group 'org-agenda-daily/weekly
1343 :type 'boolean)
1345 (defcustom org-agenda-start-with-log-mode nil
1346 "The initial value of log-mode in a newly created agenda window.
1347 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1348 explanations on the possible values."
1349 :group 'org-agenda-startup
1350 :group 'org-agenda-daily/weekly
1351 :type '(choice (const :tag "Don't show log items" nil)
1352 (const :tag "Show only log items" only)
1353 (const :tag "Show all possible log items" clockcheck)
1354 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1355 (choice (const :tag "Show closed log items" closed)
1356 (const :tag "Show clocked log items" clock)
1357 (const :tag "Show all logged state changes" state)))))
1359 (defcustom org-agenda-start-with-clockreport-mode nil
1360 "The initial value of clockreport-mode in a newly created agenda window."
1361 :group 'org-agenda-startup
1362 :group 'org-agenda-daily/weekly
1363 :type 'boolean)
1365 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1366 "Property list with parameters for the clocktable in clockreport mode.
1367 This is the display mode that shows a clock table in the daily/weekly
1368 agenda, the properties for this dynamic block can be set here.
1369 The usual clocktable parameters are allowed here, but you cannot set
1370 the properties :name, :tstart, :tend, :block, and :scope - these will
1371 be overwritten to make sure the content accurately reflects the
1372 current display in the agenda."
1373 :group 'org-agenda-daily/weekly
1374 :type 'plist)
1376 (defcustom org-agenda-search-view-always-boolean nil
1377 "Non-nil means the search string is interpreted as individual parts.
1379 The search string for search view can either be interpreted as a phrase,
1380 or as a list of snippets that define a boolean search for a number of
1381 strings.
1383 When this is non-nil, the string will be split on whitespace, and each
1384 snippet will be searched individually, and all must match in order to
1385 select an entry. A snippet is then a single string of non-white
1386 characters, or a string in double quotes, or a regexp in {} braces.
1387 If a snippet is preceded by \"-\", the snippet must *not* match.
1388 \"+\" is syntactic sugar for positive selection. Each snippet may
1389 be found as a full word or a partial word, but see the variable
1390 `org-agenda-search-view-force-full-words'.
1392 When this is nil, search will look for the entire search phrase as one,
1393 with each space character matching any amount of whitespace, including
1394 line breaks.
1396 Even when this is nil, you can still switch to Boolean search dynamically
1397 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1398 is a regexp marked with braces like \"{abc}\", this will also switch to
1399 boolean search."
1400 :group 'org-agenda-search-view
1401 :version "24.1"
1402 :type 'boolean)
1404 (defvaralias 'org-agenda-search-view-search-words-only
1405 'org-agenda-search-view-always-boolean)
1407 (defcustom org-agenda-search-view-force-full-words nil
1408 "Non-nil means, search words must be matches as complete words.
1409 When nil, they may also match part of a word."
1410 :group 'org-agenda-search-view
1411 :version "24.1"
1412 :type 'boolean)
1414 (defcustom org-agenda-search-view-max-outline-level 0
1415 "Maximum outline level to display in search view.
1416 E.g. when this is set to 1, the search view will only
1417 show headlines of level 1. When set to 0, the default
1418 value, don't limit agenda view by outline level."
1419 :group 'org-agenda-search-view
1420 :version "24.4"
1421 :package-version '(Org . "8.3")
1422 :type 'integer)
1424 (defgroup org-agenda-time-grid nil
1425 "Options concerning the time grid in the Org-mode Agenda."
1426 :tag "Org Agenda Time Grid"
1427 :group 'org-agenda)
1429 (defcustom org-agenda-search-headline-for-time t
1430 "Non-nil means search headline for a time-of-day.
1431 If the headline contains a time-of-day in one format or another, it will
1432 be used to sort the entry into the time sequence of items for a day.
1433 Some people have time stamps in the headline that refer to the creation
1434 time or so, and then this produces an unwanted side effect. If this is
1435 the case for your, use this variable to turn off searching the headline
1436 for a time."
1437 :group 'org-agenda-time-grid
1438 :type 'boolean)
1440 (defcustom org-agenda-use-time-grid t
1441 "Non-nil means show a time grid in the agenda schedule.
1442 A time grid is a set of lines for specific times (like every two hours between
1443 8:00 and 20:00). The items scheduled for a day at specific times are
1444 sorted in between these lines.
1445 For details about when the grid will be shown, and what it will look like, see
1446 the variable `org-agenda-time-grid'."
1447 :group 'org-agenda-time-grid
1448 :type 'boolean)
1450 (defcustom org-agenda-time-grid
1451 '((daily today require-timed)
1452 "----------------"
1453 (800 1000 1200 1400 1600 1800 2000))
1455 "The settings for time grid for agenda display.
1456 This is a list of three items. The first item is again a list. It contains
1457 symbols specifying conditions when the grid should be displayed:
1459 daily if the agenda shows a single day
1460 weekly if the agenda shows an entire week
1461 today show grid on current date, independent of daily/weekly display
1462 require-timed show grid only if at least one item has a time specification
1463 remove-match skip grid times already present in an entry
1465 The second item is a string which will be placed behind the grid time.
1467 The third item is a list of integers, indicating the times that should have
1468 a grid line."
1469 :group 'org-agenda-time-grid
1470 :type
1471 '(list
1472 (set :greedy t :tag "Grid Display Options"
1473 (const :tag "Show grid in single day agenda display" daily)
1474 (const :tag "Show grid in weekly agenda display" weekly)
1475 (const :tag "Always show grid for today" today)
1476 (const :tag "Show grid only if any timed entries are present"
1477 require-timed)
1478 (const :tag "Skip grid times already present in an entry"
1479 remove-match))
1480 (string :tag "Grid String")
1481 (repeat :tag "Grid Times" (integer :tag "Time"))))
1483 (defcustom org-agenda-show-current-time-in-grid t
1484 "Non-nil means show the current time in the time grid."
1485 :group 'org-agenda-time-grid
1486 :version "24.1"
1487 :type 'boolean)
1489 (defcustom org-agenda-current-time-string
1490 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1491 "The string for the current time marker in the agenda."
1492 :group 'org-agenda-time-grid
1493 :version "24.1"
1494 :type 'string)
1496 (defgroup org-agenda-sorting nil
1497 "Options concerning sorting in the Org-mode Agenda."
1498 :tag "Org Agenda Sorting"
1499 :group 'org-agenda)
1501 (defcustom org-agenda-sorting-strategy
1502 '((agenda habit-down time-up priority-down category-keep)
1503 (todo priority-down category-keep)
1504 (tags priority-down category-keep)
1505 (search category-keep))
1506 "Sorting structure for the agenda items of a single day.
1507 This is a list of symbols which will be used in sequence to determine
1508 if an entry should be listed before another entry. The following
1509 symbols are recognized:
1511 time-up Put entries with time-of-day indications first, early first
1512 time-down Put entries with time-of-day indications first, late first
1513 timestamp-up Sort by any timestamp, early first
1514 timestamp-down Sort by any timestamp, late first
1515 scheduled-up Sort by scheduled timestamp, early first
1516 scheduled-down Sort by scheduled timestamp, late first
1517 deadline-up Sort by deadline timestamp, early first
1518 deadline-down Sort by deadline timestamp, late first
1519 ts-up Sort by active timestamp, early first
1520 ts-down Sort by active timestamp, late first
1521 tsia-up Sort by inactive timestamp, early first
1522 tsia-down Sort by inactive timestamp, late first
1523 category-keep Keep the default order of categories, corresponding to the
1524 sequence in `org-agenda-files'.
1525 category-up Sort alphabetically by category, A-Z.
1526 category-down Sort alphabetically by category, Z-A.
1527 tag-up Sort alphabetically by last tag, A-Z.
1528 tag-down Sort alphabetically by last tag, Z-A.
1529 priority-up Sort numerically by priority, high priority last.
1530 priority-down Sort numerically by priority, high priority first.
1531 todo-state-up Sort by todo state, tasks that are done last.
1532 todo-state-down Sort by todo state, tasks that are done first.
1533 effort-up Sort numerically by estimated effort, high effort last.
1534 effort-down Sort numerically by estimated effort, high effort first.
1535 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1536 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1537 habit-up Put entries that are habits first
1538 habit-down Put entries that are habits last
1539 alpha-up Sort headlines alphabetically
1540 alpha-down Sort headlines alphabetically, reversed
1542 The different possibilities will be tried in sequence, and testing stops
1543 if one comparison returns a \"not-equal\". For example, the default
1544 '(time-up category-keep priority-down)
1545 means: Pull out all entries having a specified time of day and sort them,
1546 in order to make a time schedule for the current day the first thing in the
1547 agenda listing for the day. Of the entries without a time indication, keep
1548 the grouped in categories, don't sort the categories, but keep them in
1549 the sequence given in `org-agenda-files'. Within each category sort by
1550 priority.
1552 Leaving out `category-keep' would mean that items will be sorted across
1553 categories by priority.
1555 Instead of a single list, this can also be a set of list for specific
1556 contents, with a context symbol in the car of the list, any of
1557 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1559 Custom commands can bind this variable in the options section."
1560 :group 'org-agenda-sorting
1561 :type `(choice
1562 (repeat :tag "General" ,org-sorting-choice)
1563 (list :tag "Individually"
1564 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1565 (repeat ,org-sorting-choice))
1566 (cons (const :tag "Strategy for TODO lists" todo)
1567 (repeat ,org-sorting-choice))
1568 (cons (const :tag "Strategy for Tags matches" tags)
1569 (repeat ,org-sorting-choice))
1570 (cons (const :tag "Strategy for search matches" search)
1571 (repeat ,org-sorting-choice)))))
1573 (defcustom org-agenda-cmp-user-defined nil
1574 "A function to define the comparison `user-defined'.
1575 This function must receive two arguments, agenda entry a and b.
1576 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1577 the user comparison, return nil.
1578 When this is defined, you can make `user-defined-up' and `user-defined-down'
1579 part of an agenda sorting strategy."
1580 :group 'org-agenda-sorting
1581 :type 'symbol)
1583 (defcustom org-sort-agenda-notime-is-late t
1584 "Non-nil means items without time are considered late.
1585 This is only relevant for sorting. When t, items which have no explicit
1586 time like 15:30 will be considered as 99:01, i.e. later than any items which
1587 do have a time. When nil, the default time is before 0:00. You can use this
1588 option to decide if the schedule for today should come before or after timeless
1589 agenda entries."
1590 :group 'org-agenda-sorting
1591 :type 'boolean)
1593 (defcustom org-sort-agenda-noeffort-is-high t
1594 "Non-nil means items without effort estimate are sorted as high effort.
1595 This also applies when filtering an agenda view with respect to the
1596 < or > effort operator. Then, tasks with no effort defined will be treated
1597 as tasks with high effort.
1598 When nil, such items are sorted as 0 minutes effort."
1599 :group 'org-agenda-sorting
1600 :type 'boolean)
1602 (defgroup org-agenda-line-format nil
1603 "Options concerning the entry prefix in the Org-mode agenda display."
1604 :tag "Org Agenda Line Format"
1605 :group 'org-agenda)
1607 (defcustom org-agenda-prefix-format
1608 '((agenda . " %i %-12:c%?-12t% s")
1609 (timeline . " % s")
1610 (todo . " %i %-12:c")
1611 (tags . " %i %-12:c")
1612 (search . " %i %-12:c"))
1613 "Format specifications for the prefix of items in the agenda views.
1614 An alist with five entries, each for the different agenda types. The
1615 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1616 The values are format strings.
1618 This format works similar to a printf format, with the following meaning:
1620 %c the category of the item, \"Diary\" for entries from the diary,
1621 or as given by the CATEGORY keyword or derived from the file name
1622 %e the effort required by the item
1623 %l the level of the item (insert X space(s) if item is of level X)
1624 %i the icon category of the item, see `org-agenda-category-icon-alist'
1625 %T the last tag of the item (ignore inherited tags, which come first)
1626 %t the HH:MM time-of-day specification if one applies to the entry
1627 %s Scheduling/Deadline information, a short string
1628 %b show breadcrumbs, i.e., the names of the higher levels
1629 %(expression) Eval EXPRESSION and replace the control string
1630 by the result
1632 All specifiers work basically like the standard `%s' of printf, but may
1633 contain two additional characters: a question mark just after the `%'
1634 and a whitespace/punctuation character just before the final letter.
1636 If the first character after `%' is a question mark, the entire field
1637 will only be included if the corresponding value applies to the current
1638 entry. This is useful for fields which should have fixed width when
1639 present, but zero width when absent. For example, \"%?-12t\" will
1640 result in a 12 character time field if a time of the day is specified,
1641 but will completely disappear in entries which do not contain a time.
1643 If there is punctuation or whitespace character just before the
1644 final format letter, this character will be appended to the field
1645 value if the value is not empty. For example, the format
1646 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1647 the category is empty, no additional colon is inserted.
1649 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1650 which means:
1652 - Indent the line with two space characters
1653 - Give the category a 12 chars wide field, padded with whitespace on
1654 the right (because of `-'). Append a colon if there is a category
1655 (because of `:').
1656 - If there is a time-of-day, put it into a 12 chars wide field. If no
1657 time, don't put in an empty field, just skip it (because of '?').
1658 - Finally, put the scheduling information.
1660 See also the variables `org-agenda-remove-times-when-in-prefix' and
1661 `org-agenda-remove-tags'.
1663 Custom commands can set this variable in the options section."
1664 :type '(choice
1665 (string :tag "General format")
1666 (list :greedy t :tag "View dependent"
1667 (cons (const agenda) (string :tag "Format"))
1668 (cons (const timeline) (string :tag "Format"))
1669 (cons (const todo) (string :tag "Format"))
1670 (cons (const tags) (string :tag "Format"))
1671 (cons (const search) (string :tag "Format"))))
1672 :group 'org-agenda-line-format)
1674 (defvar org-prefix-format-compiled nil
1675 "The compiled prefix format and associated variables.
1676 This is a list where first element is a list of variable bindings, and second
1677 element is the compiled format expression. See the variable
1678 `org-agenda-prefix-format'.")
1680 (defcustom org-agenda-todo-keyword-format "%-1s"
1681 "Format for the TODO keyword in agenda lines.
1682 Set this to something like \"%-12s\" if you want all TODO keywords
1683 to occupy a fixed space in the agenda display."
1684 :group 'org-agenda-line-format
1685 :type 'string)
1687 (defcustom org-agenda-diary-sexp-prefix nil
1688 "A regexp that matches part of a diary sexp entry
1689 which should be treated as scheduling/deadline information in
1690 `org-agenda'.
1692 For example, you can use this to extract the `diary-remind-message' from
1693 `diary-remind' entries."
1694 :group 'org-agenda-line-format
1695 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1697 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1698 "Text preceding timerange entries in the agenda view.
1699 This is a list with two strings. The first applies when the range
1700 is entirely on one day. The second applies if the range spans several days.
1701 The strings may have two \"%d\" format specifiers which will be filled
1702 with the sequence number of the days, and the total number of days in the
1703 range, respectively."
1704 :group 'org-agenda-line-format
1705 :type '(list
1706 (string :tag "Deadline today ")
1707 (choice :tag "Deadline relative"
1708 (string :tag "Format string")
1709 (function))))
1711 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1712 "Text preceding scheduled items in the agenda view.
1713 This is a list with two strings. The first applies when the item is
1714 scheduled on the current day. The second applies when it has been scheduled
1715 previously, it may contain a %d indicating that this is the nth time that
1716 this item is scheduled, due to automatic rescheduling of unfinished items
1717 for the following day. So this number is one larger than the number of days
1718 that passed since this item was scheduled first."
1719 :group 'org-agenda-line-format
1720 :version "24.4"
1721 :package-version '(Org . "8.0")
1722 :type '(list
1723 (string :tag "Scheduled today ")
1724 (string :tag "Scheduled previously")))
1726 (defcustom org-agenda-inactive-leader "["
1727 "Text preceding item pulled into the agenda by inactive time stamps.
1728 These entries are added to the agenda when pressing \"[\"."
1729 :group 'org-agenda-line-format
1730 :version "24.1"
1731 :type 'string)
1733 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1734 "Text preceding deadline items in the agenda view.
1735 This is a list with three strings. The first applies when the item has its
1736 deadline on the current day. The second applies when the deadline is in the
1737 future, the third one when it is in the past. The strings may contain %d
1738 to capture the number of days."
1739 :group 'org-agenda-line-format
1740 :version "24.4"
1741 :package-version '(Org . "8.0")
1742 :type '(list
1743 (string :tag "Deadline today ")
1744 (string :tag "Deadline in the future ")
1745 (string :tag "Deadline in the past ")))
1747 (defcustom org-agenda-remove-times-when-in-prefix t
1748 "Non-nil means remove duplicate time specifications in agenda items.
1749 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1750 time-of-day specification in a headline or diary entry is extracted and
1751 placed into the prefix. If this option is non-nil, the original specification
1752 \(a timestamp or -range, or just a plain time(range) specification like
1753 11:30-4pm) will be removed for agenda display. This makes the agenda less
1754 cluttered.
1755 The option can be t or nil. It may also be the symbol `beg', indicating
1756 that the time should only be removed when it is located at the beginning of
1757 the headline/diary entry."
1758 :group 'org-agenda-line-format
1759 :type '(choice
1760 (const :tag "Always" t)
1761 (const :tag "Never" nil)
1762 (const :tag "When at beginning of entry" beg)))
1764 (defcustom org-agenda-remove-timeranges-from-blocks nil
1765 "Non-nil means remove time ranges specifications in agenda
1766 items that span on several days."
1767 :group 'org-agenda-line-format
1768 :version "24.1"
1769 :type 'boolean)
1771 (defcustom org-agenda-default-appointment-duration nil
1772 "Default duration for appointments that only have a starting time.
1773 When nil, no duration is specified in such cases.
1774 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1775 :group 'org-agenda-line-format
1776 :type '(choice
1777 (integer :tag "Minutes")
1778 (const :tag "No default duration")))
1780 (defcustom org-agenda-show-inherited-tags t
1781 "Non-nil means show inherited tags in each agenda line.
1783 When this option is set to `always', it take precedences over
1784 `org-agenda-use-tag-inheritance' and inherited tags are shown
1785 in every agenda.
1787 When this option is set to t (the default), inherited tags are
1788 shown when they are available, i.e. when the value of
1789 `org-agenda-use-tag-inheritance' has been taken into account.
1791 This can be set to a list of agenda types in which the agenda
1792 must display the inherited tags. Available types are `todo',
1793 `agenda', `search' and `timeline'.
1795 When set to nil, never show inherited tags in agenda lines."
1796 :group 'org-agenda-line-format
1797 :group 'org-agenda
1798 :version "24.3"
1799 :type '(choice
1800 (const :tag "Show inherited tags when available" t)
1801 (const :tag "Always show inherited tags" always)
1802 (repeat :tag "Show inherited tags only in selected agenda types"
1803 (symbol :tag "Agenda type"))))
1805 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1806 "List of agenda view types where to use tag inheritance.
1808 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1809 controlled by `org-use-tag-inheritance'. In other agenda types,
1810 `org-use-tag-inheritance' is not used for the selection of the
1811 agenda entries. Still, you may want the agenda to be aware of
1812 the inherited tags anyway, e.g. for later tag filtering.
1814 Allowed value are `todo', `search', `timeline' and `agenda'.
1816 This variable has no effect if `org-agenda-show-inherited-tags'
1817 is set to `always'. In that case, the agenda is aware of those
1818 tags.
1820 The default value sets tags in every agenda type. Setting this
1821 option to nil will speed up non-tags agenda view a lot."
1822 :group 'org-agenda
1823 :version "24.3"
1824 :type '(choice
1825 (const :tag "Use tag inheritance in all agenda types" t)
1826 (repeat :tag "Use tag inheritance in selected agenda types"
1827 (symbol :tag "Agenda type"))))
1829 (defcustom org-agenda-hide-tags-regexp nil
1830 "Regular expression used to filter away specific tags in agenda views.
1831 This means that these tags will be present, but not be shown in the agenda
1832 line. Secondary filtering will still work on the hidden tags.
1833 Nil means don't hide any tags."
1834 :group 'org-agenda-line-format
1835 :type '(choice
1836 (const :tag "Hide none" nil)
1837 (string :tag "Regexp ")))
1839 (defcustom org-agenda-remove-tags nil
1840 "Non-nil means remove the tags from the headline copy in the agenda.
1841 When this is the symbol `prefix', only remove tags when
1842 `org-agenda-prefix-format' contains a `%T' specifier."
1843 :group 'org-agenda-line-format
1844 :type '(choice
1845 (const :tag "Always" t)
1846 (const :tag "Never" nil)
1847 (const :tag "When prefix format contains %T" prefix)))
1849 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1850 'org-agenda-remove-tags)
1852 (defcustom org-agenda-tags-column -80
1853 "Shift tags in agenda items to this column.
1854 If this number is positive, it specifies the column. If it is negative,
1855 it means that the tags should be flushright to that column. For example,
1856 -80 works well for a normal 80 character screen."
1857 :group 'org-agenda-line-format
1858 :type 'integer)
1860 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1862 (defcustom org-agenda-fontify-priorities 'cookies
1863 "Non-nil means highlight low and high priorities in agenda.
1864 When t, the highest priority entries are bold, lowest priority italic.
1865 However, settings in `org-priority-faces' will overrule these faces.
1866 When this variable is the symbol `cookies', only fontify the
1867 cookies, not the entire task.
1868 This may also be an association list of priority faces, whose
1869 keys are the character values of `org-highest-priority',
1870 `org-default-priority', and `org-lowest-priority' (the default values
1871 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1872 color as a string, or a list like `(:background \"Red\")'.
1873 If it is a color, the variable `org-faces-easy-properties'
1874 determines if it is a foreground or a background color."
1875 :group 'org-agenda-line-format
1876 :type '(choice
1877 (const :tag "Never" nil)
1878 (const :tag "Defaults" t)
1879 (const :tag "Cookies only" cookies)
1880 (repeat :tag "Specify"
1881 (list (character :tag "Priority" :value ?A)
1882 (choice :tag "Face "
1883 (string :tag "Color")
1884 (sexp :tag "Face"))))))
1886 (defcustom org-agenda-day-face-function nil
1887 "Function called to determine what face should be used to display a day.
1888 The only argument passed to that function is the day. It should
1889 returns a face, or nil if does not want to specify a face and let
1890 the normal rules apply."
1891 :group 'org-agenda-line-format
1892 :version "24.1"
1893 :type '(choice (const nil) (function)))
1895 (defcustom org-agenda-category-icon-alist nil
1896 "Alist of category icon to be displayed in agenda views.
1898 Each entry should have the following format:
1900 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1902 Where CATEGORY-REGEXP is a regexp matching the categories where
1903 the icon should be displayed.
1904 FILE-OR-DATA either a file path or a string containing image data.
1906 The other fields can be omitted safely if not needed:
1907 TYPE indicates the image type.
1908 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1909 image data.
1910 PROPS are additional image attributes to assign to the image,
1911 like, e.g. `:ascent center'.
1913 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1915 If you want to set the display properties yourself, just put a
1916 list as second element:
1918 (CATEGORY-REGEXP (MY PROPERTY LIST))
1920 For example, to display a 16px horizontal space for Emacs
1921 category, you can use:
1923 (\"Emacs\" \\='(space . (:width (16))))"
1924 :group 'org-agenda-line-format
1925 :version "24.1"
1926 :type '(alist :key-type (string :tag "Regexp matching category")
1927 :value-type (choice (list :tag "Icon"
1928 (string :tag "File or data")
1929 (symbol :tag "Type")
1930 (boolean :tag "Data?")
1931 (repeat :tag "Extra image properties" :inline t symbol))
1932 (list :tag "Display properties" sexp))))
1934 (defgroup org-agenda-column-view nil
1935 "Options concerning column view in the agenda."
1936 :tag "Org Agenda Column View"
1937 :group 'org-agenda)
1939 (defcustom org-agenda-view-columns-initially nil
1940 "When non-nil, switch to columns view right after creating the agenda."
1941 :group 'org-agenda-column-view
1942 :type 'boolean
1943 :version "25.1"
1944 :package-version '(Org . "9.0")
1945 :safe #'booleanp)
1947 (defcustom org-agenda-columns-show-summaries t
1948 "Non-nil means show summaries for columns displayed in the agenda view."
1949 :group 'org-agenda-column-view
1950 :type 'boolean)
1952 (defcustom org-agenda-columns-compute-summary-properties t
1953 "Non-nil means recompute all summary properties before column view.
1954 When column view in the agenda is listing properties that have a summary
1955 operator, it can go to all relevant buffers and recompute the summaries
1956 there. This can mean overhead for the agenda column view, but is necessary
1957 to have thing up to date.
1958 As a special case, a CLOCKSUM property also makes sure that the clock
1959 computations are current."
1960 :group 'org-agenda-column-view
1961 :type 'boolean)
1963 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1964 "Non-nil means the duration of an appointment will add to day effort.
1965 The property to which appointment durations will be added is the one given
1966 in the option `org-effort-property'. If an appointment does not have
1967 an end time, `org-agenda-default-appointment-duration' will be used. If that
1968 is not set, an appointment without end time will not contribute to the time
1969 estimate."
1970 :group 'org-agenda-column-view
1971 :type 'boolean)
1973 (defcustom org-agenda-auto-exclude-function nil
1974 "A function called with a tag to decide if it is filtered on \
1975 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1976 The sole argument to the function, which is called once for each
1977 possible tag, is a string giving the name of the tag. The
1978 function should return either nil if the tag should be included
1979 as normal, or \"-<TAG>\" to exclude the tag.
1980 Note that for the purpose of tag filtering, only the lower-case version of
1981 all tags will be considered, so that this function will only ever see
1982 the lower-case version of all tags."
1983 :group 'org-agenda
1984 :type '(choice (const nil) (function)))
1986 (defcustom org-agenda-bulk-custom-functions nil
1987 "Alist of characters and custom functions for bulk actions.
1988 For example, this value makes those two functions available:
1990 \\='((?R set-category)
1991 (?C bulk-cut))
1993 With selected entries in an agenda buffer, `B R' will call
1994 the custom function `set-category' on the selected entries.
1995 Note that functions in this alist don't need to be quoted."
1996 :type '(alist :key-type character :value-type (group function))
1997 :version "24.1"
1998 :group 'org-agenda)
2000 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2001 "Execute BODY with point at location given by `org-hd-marker' property.
2002 If STRING is non-nil, the text property will be fetched from position 0
2003 in that string. If STRING is nil, it will be fetched from the beginning
2004 of the current line."
2005 (org-with-gensyms (marker)
2006 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2007 'org-hd-marker ,string)))
2008 (with-current-buffer (marker-buffer ,marker)
2009 (save-excursion
2010 (goto-char ,marker)
2011 ,@body)))))
2012 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2014 (defun org-add-agenda-custom-command (entry)
2015 "Replace or add a command in `org-agenda-custom-commands'.
2016 This is mostly for hacking and trying a new command - once the command
2017 works you probably want to add it to `org-agenda-custom-commands' for good."
2018 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2019 (if ass
2020 (setcdr ass (cdr entry))
2021 (push entry org-agenda-custom-commands))))
2023 ;;; Define the org-agenda-mode
2025 (defvar org-agenda-mode-map (make-sparse-keymap)
2026 "Keymap for `org-agenda-mode'.")
2027 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2029 (defvar org-agenda-menu) ; defined later in this file.
2030 (defvar org-agenda-restrict nil) ; defined later in this file.
2031 (defvar org-agenda-follow-mode nil)
2032 (defvar org-agenda-entry-text-mode nil)
2033 (defvar org-agenda-clockreport-mode nil)
2034 (defvar org-agenda-show-log nil)
2035 (defvar org-agenda-redo-command nil)
2036 (defvar org-agenda-query-string nil)
2037 (defvar org-agenda-mode-hook nil
2038 "Hook run after `org-agenda-mode' is turned on.
2039 The buffer is still writable when this hook is called.")
2040 (defvar org-agenda-type nil)
2041 (defvar org-agenda-force-single-file nil)
2042 (defvar org-agenda-bulk-marked-entries nil
2043 "List of markers that refer to marked entries in the agenda.")
2044 (defvar org-agenda-current-date nil
2045 "Active date when building the agenda.")
2047 ;;; Multiple agenda buffers support
2049 (defcustom org-agenda-sticky nil
2050 "Non-nil means agenda q key will bury agenda buffers.
2051 Agenda commands will then show existing buffer instead of generating new ones.
2052 When nil, `q' will kill the single agenda buffer."
2053 :group 'org-agenda
2054 :version "24.3"
2055 :type 'boolean)
2058 ;;;###autoload
2059 (defun org-toggle-sticky-agenda (&optional arg)
2060 "Toggle `org-agenda-sticky'."
2061 (interactive "P")
2062 (let ((new-value (if arg
2063 (> (prefix-numeric-value arg) 0)
2064 (not org-agenda-sticky))))
2065 (if (equal new-value org-agenda-sticky)
2066 (and (called-interactively-p 'interactive)
2067 (message "Sticky agenda was already %s"
2068 (if org-agenda-sticky "enabled" "disabled")))
2069 (setq org-agenda-sticky new-value)
2070 (org-agenda-kill-all-agenda-buffers)
2071 (and (called-interactively-p 'interactive)
2072 (message "Sticky agenda %s"
2073 (if org-agenda-sticky "enabled" "disabled"))))))
2075 (defvar org-agenda-buffer nil
2076 "Agenda buffer currently being generated.")
2078 (defvar org-agenda-last-prefix-arg nil)
2079 (defvar org-agenda-this-buffer-name nil)
2080 (defvar org-agenda-doing-sticky-redo nil)
2081 (defvar org-agenda-this-buffer-is-sticky nil)
2082 (defvar org-agenda-last-indirect-buffer nil
2083 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2085 (defconst org-agenda-local-vars
2086 '(org-agenda-this-buffer-name
2087 org-agenda-undo-list
2088 org-agenda-pending-undo-list
2089 org-agenda-follow-mode
2090 org-agenda-entry-text-mode
2091 org-agenda-clockreport-mode
2092 org-agenda-show-log
2093 org-agenda-redo-command
2094 org-agenda-query-string
2095 org-agenda-type
2096 org-agenda-bulk-marked-entries
2097 org-agenda-undo-has-started-in
2098 org-agenda-info
2099 org-agenda-pre-window-conf
2100 org-agenda-columns-active
2101 org-agenda-tag-filter
2102 org-agenda-category-filter
2103 org-agenda-top-headline-filter
2104 org-agenda-regexp-filter
2105 org-agenda-effort-filter
2106 org-agenda-markers
2107 org-agenda-last-search-view-search-was-boolean
2108 org-agenda-last-indirect-buffer
2109 org-agenda-filtered-by-category
2110 org-agenda-filter-form
2111 org-agenda-cycle-counter
2112 org-agenda-last-prefix-arg)
2113 "Variables that must be local in agenda buffers to allow multiple buffers.")
2115 (defun org-agenda-mode ()
2116 "Mode for time-sorted view on action items in Org-mode files.
2118 The following commands are available:
2120 \\{org-agenda-mode-map}"
2121 (interactive)
2122 (cond (org-agenda-doing-sticky-redo
2123 ;; Refreshing sticky agenda-buffer
2125 ;; Preserve the value of `org-agenda-local-vars' variables,
2126 ;; while letting `kill-all-local-variables' kill the rest
2127 (let ((save (buffer-local-variables)))
2128 (kill-all-local-variables)
2129 (mapc 'make-local-variable org-agenda-local-vars)
2130 (dolist (elem save)
2131 (let ((var (car elem))
2132 (val (cdr elem)))
2133 (when (and val
2134 (member var org-agenda-local-vars))
2135 (set var val)))))
2136 (setq-local org-agenda-this-buffer-is-sticky t))
2137 (org-agenda-sticky
2138 ;; Creating a sticky Agenda buffer for the first time
2139 (kill-all-local-variables)
2140 (mapc 'make-local-variable org-agenda-local-vars)
2141 (setq-local org-agenda-this-buffer-is-sticky t))
2143 ;; Creating a non-sticky agenda buffer
2144 (kill-all-local-variables)
2145 (setq-local org-agenda-this-buffer-is-sticky nil)))
2146 (setq org-agenda-undo-list nil
2147 org-agenda-pending-undo-list nil
2148 org-agenda-bulk-marked-entries nil)
2149 (setq major-mode 'org-agenda-mode)
2150 ;; Keep global-font-lock-mode from turning on font-lock-mode
2151 (setq-local font-lock-global-modes (list 'not major-mode))
2152 (setq mode-name "Org-Agenda")
2153 (setq indent-tabs-mode nil)
2154 (use-local-map org-agenda-mode-map)
2155 (easy-menu-add org-agenda-menu)
2156 (if org-startup-truncated (setq truncate-lines t))
2157 (setq-local line-move-visual nil)
2158 (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2159 (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2160 ;; Make sure properties are removed when copying text
2161 (add-hook 'filter-buffer-substring-functions
2162 (lambda (fun start end delete)
2163 (substring-no-properties (funcall fun start end delete)))
2164 nil t)
2165 (unless org-agenda-keep-modes
2166 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2167 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2168 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2169 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2170 (add-to-invisibility-spec '(org-filtered))
2171 (add-to-invisibility-spec '(org-link))
2172 (easy-menu-change
2173 '("Agenda") "Agenda Files"
2174 (append
2175 (list
2176 (vector
2177 (if (get 'org-agenda-files 'org-restrict)
2178 "Restricted to single file"
2179 "Edit File List")
2180 '(org-edit-agenda-file-list)
2181 (not (get 'org-agenda-files 'org-restrict)))
2182 "--")
2183 (mapcar 'org-file-menu-entry (org-agenda-files))))
2184 (org-agenda-set-mode-name)
2185 (apply
2186 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2187 (list 'org-agenda-mode-hook)))
2189 (substitute-key-definition 'undo 'org-agenda-undo
2190 org-agenda-mode-map global-map)
2191 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2192 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2193 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2194 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2195 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2196 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2197 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2198 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2199 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2200 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2201 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2202 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2203 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2204 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2205 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2206 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2207 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2208 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2209 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2210 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2211 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2212 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2213 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2214 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2215 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2216 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2217 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2218 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2219 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2220 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2221 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2222 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2223 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2224 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2225 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2226 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2227 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2228 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2229 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2230 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2231 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2232 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2233 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2234 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2235 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2236 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2237 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2238 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2239 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2240 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2242 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2243 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2244 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2245 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2246 (while l (org-defkey org-agenda-mode-map
2247 (int-to-string (pop l)) 'digit-argument)))
2249 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2250 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2251 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2252 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2253 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2254 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2255 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2256 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2257 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2258 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2259 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2260 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2261 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2262 'org-clock-modify-effort-estimate)
2263 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2264 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2265 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2266 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2267 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2268 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2269 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2270 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2271 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2272 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2273 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2274 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2275 (substitute-key-definition 'next-line 'org-agenda-next-line
2276 org-agenda-mode-map global-map)
2277 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2278 org-agenda-mode-map global-map)
2279 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2280 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2281 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2282 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2283 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2284 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2285 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2286 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2287 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2288 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2289 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2290 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2291 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2292 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2293 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2294 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2295 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2296 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2297 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2298 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2299 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2300 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2301 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2302 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2303 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2304 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2305 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2306 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2307 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2308 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2310 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2311 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2312 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2313 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2314 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2315 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2316 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2317 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2318 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2319 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2320 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2321 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2322 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2323 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2324 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2326 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2327 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2329 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2330 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2332 (when org-agenda-mouse-1-follows-link
2333 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2334 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2335 '("Agenda"
2336 ("Agenda Files")
2337 "--"
2338 ("Agenda Dates"
2339 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2340 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2341 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2342 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2343 "--"
2344 ("View"
2345 ["Day View" org-agenda-day-view
2346 :active (org-agenda-check-type nil 'agenda)
2347 :style radio :selected (eq org-agenda-current-span 'day)
2348 :keys "v d (or just d)"]
2349 ["Week View" org-agenda-week-view
2350 :active (org-agenda-check-type nil 'agenda)
2351 :style radio :selected (eq org-agenda-current-span 'week)
2352 :keys "v w"]
2353 ["Fortnight View" org-agenda-fortnight-view
2354 :active (org-agenda-check-type nil 'agenda)
2355 :style radio :selected (eq org-agenda-current-span 'fortnight)
2356 :keys "v f"]
2357 ["Month View" org-agenda-month-view
2358 :active (org-agenda-check-type nil 'agenda)
2359 :style radio :selected (eq org-agenda-current-span 'month)
2360 :keys "v m"]
2361 ["Year View" org-agenda-year-view
2362 :active (org-agenda-check-type nil 'agenda)
2363 :style radio :selected (eq org-agenda-current-span 'year)
2364 :keys "v y"]
2365 "--"
2366 ["Include Diary" org-agenda-toggle-diary
2367 :style toggle :selected org-agenda-include-diary
2368 :active (org-agenda-check-type nil 'agenda)]
2369 ["Include Deadlines" org-agenda-toggle-deadlines
2370 :style toggle :selected org-agenda-include-deadlines
2371 :active (org-agenda-check-type nil 'agenda)]
2372 ["Use Time Grid" org-agenda-toggle-time-grid
2373 :style toggle :selected org-agenda-use-time-grid
2374 :active (org-agenda-check-type nil 'agenda)]
2375 "--"
2376 ["Show clock report" org-agenda-clockreport-mode
2377 :style toggle :selected org-agenda-clockreport-mode
2378 :active (org-agenda-check-type nil 'agenda)]
2379 ["Show some entry text" org-agenda-entry-text-mode
2380 :style toggle :selected org-agenda-entry-text-mode
2381 :active t]
2382 "--"
2383 ["Show Logbook entries" org-agenda-log-mode
2384 :style toggle :selected org-agenda-show-log
2385 :active (org-agenda-check-type nil 'agenda 'timeline)
2386 :keys "v l (or just l)"]
2387 ["Include archived trees" org-agenda-archives-mode
2388 :style toggle :selected org-agenda-archives-mode :active t
2389 :keys "v a"]
2390 ["Include archive files" (org-agenda-archives-mode t)
2391 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2392 :keys "v A"]
2393 "--"
2394 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2395 ["Write view to file" org-agenda-write t]
2396 ["Rebuild buffer" org-agenda-redo t]
2397 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2398 "--"
2399 ["Show original entry" org-agenda-show t]
2400 ["Go To (other window)" org-agenda-goto t]
2401 ["Go To (this window)" org-agenda-switch-to t]
2402 ["Capture with cursor date" org-agenda-capture t]
2403 ["Follow Mode" org-agenda-follow-mode
2404 :style toggle :selected org-agenda-follow-mode :active t]
2405 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2406 "--"
2407 ("TODO"
2408 ["Cycle TODO" org-agenda-todo t]
2409 ["Next TODO set" org-agenda-todo-nextset t]
2410 ["Previous TODO set" org-agenda-todo-previousset t]
2411 ["Add note" org-agenda-add-note t])
2412 ("Archive/Refile/Delete"
2413 ["Archive default" org-agenda-archive-default t]
2414 ["Archive default" org-agenda-archive-default-with-confirmation t]
2415 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2416 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2417 ["Archive subtree" org-agenda-archive t]
2418 "--"
2419 ["Refile" org-agenda-refile t]
2420 "--"
2421 ["Delete subtree" org-agenda-kill t])
2422 ("Bulk action"
2423 ["Mark entry" org-agenda-bulk-mark t]
2424 ["Mark all" org-agenda-bulk-mark-all t]
2425 ["Unmark entry" org-agenda-bulk-unmark t]
2426 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2427 ["Toggle mark" org-agenda-bulk-toggle t]
2428 ["Toggle all" org-agenda-bulk-toggle-all t]
2429 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2430 ["Act on all marked" org-agenda-bulk-action t]
2431 "--"
2432 ("Tags and Properties"
2433 ["Show all Tags" org-agenda-show-tags t]
2434 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2435 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2436 "--"
2437 ["Column View" org-columns t])
2438 ("Deadline/Schedule"
2439 ["Schedule" org-agenda-schedule t]
2440 ["Set Deadline" org-agenda-deadline t]
2441 "--"
2442 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2443 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2444 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2445 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2446 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2447 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2448 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2449 ("Clock and Effort"
2450 ["Clock in" org-agenda-clock-in t]
2451 ["Clock out" org-agenda-clock-out t]
2452 ["Clock cancel" org-agenda-clock-cancel t]
2453 ["Goto running clock" org-clock-goto t]
2454 "--"
2455 ["Set Effort" org-agenda-set-effort t]
2456 ["Change clocked effort" org-clock-modify-effort-estimate
2457 (org-clock-is-active)])
2458 ("Priority"
2459 ["Set Priority" org-agenda-priority t]
2460 ["Increase Priority" org-agenda-priority-up t]
2461 ["Decrease Priority" org-agenda-priority-down t]
2462 ["Show Priority" org-show-priority t])
2463 ("Calendar/Diary"
2464 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2465 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2466 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2467 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2468 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2469 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2470 "--"
2471 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2472 "--"
2473 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2474 "--"
2475 ("MobileOrg"
2476 ["Push Files and Views" org-mobile-push t]
2477 ["Get Captured and Flagged" org-mobile-pull t]
2478 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2479 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2480 "--"
2481 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2482 "--"
2483 ["Quit" org-agenda-quit t]
2484 ["Exit and Release Buffers" org-agenda-exit t]
2487 ;;; Agenda undo
2489 (defvar org-agenda-allow-remote-undo t
2490 "Non-nil means allow remote undo from the agenda buffer.")
2491 (defvar org-agenda-undo-has-started-in nil
2492 "Buffers that have already seen `undo-start' in the current undo sequence.")
2494 (defun org-agenda-undo ()
2495 "Undo a remote editing step in the agenda.
2496 This undoes changes both in the agenda buffer and in the remote buffer
2497 that have been changed along."
2498 (interactive)
2499 (or org-agenda-allow-remote-undo
2500 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2501 (if (not (eq this-command last-command))
2502 (setq org-agenda-undo-has-started-in nil
2503 org-agenda-pending-undo-list org-agenda-undo-list))
2504 (if (not org-agenda-pending-undo-list)
2505 (user-error "No further undo information"))
2506 (let* ((entry (pop org-agenda-pending-undo-list))
2507 buf line cmd rembuf)
2508 (setq cmd (pop entry) line (pop entry))
2509 (setq rembuf (nth 2 entry))
2510 (org-with-remote-undo rembuf
2511 (while (bufferp (setq buf (pop entry)))
2512 (if (pop entry)
2513 (with-current-buffer buf
2514 (let ((last-undo-buffer buf)
2515 (inhibit-read-only t))
2516 (unless (memq buf org-agenda-undo-has-started-in)
2517 (push buf org-agenda-undo-has-started-in)
2518 (make-local-variable 'pending-undo-list)
2519 (undo-start))
2520 (while (and pending-undo-list
2521 (listp pending-undo-list)
2522 (not (car pending-undo-list)))
2523 (pop pending-undo-list))
2524 (undo-more 1))))))
2525 (org-goto-line line)
2526 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2528 (defun org-verify-change-for-undo (l1 l2)
2529 "Verify that a real change occurred between the undo lists L1 and L2."
2530 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2531 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2532 (not (eq l1 l2)))
2534 ;;; Agenda dispatch
2536 (defvar org-agenda-restrict-begin (make-marker))
2537 (defvar org-agenda-restrict-end (make-marker))
2538 (defvar org-agenda-last-dispatch-buffer nil)
2539 (defvar org-agenda-overriding-restriction nil)
2541 (defcustom org-agenda-custom-commands-contexts nil
2542 "Alist of custom agenda keys and contextual rules.
2544 For example, if you have a custom agenda command \"p\" and you
2545 want this command to be accessible only from plain text files,
2546 use this:
2548 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2550 Here are the available contexts definitions:
2552 in-file: command displayed only in matching files
2553 in-mode: command displayed only in matching modes
2554 not-in-file: command not displayed in matching files
2555 not-in-mode: command not displayed in matching modes
2556 in-buffer: command displayed only in matching buffers
2557 not-in-buffer: command not displayed in matching buffers
2558 [function]: a custom function taking no argument
2560 If you define several checks, the agenda command will be
2561 accessible if there is at least one valid check.
2563 You can also bind a key to another agenda custom command
2564 depending on contextual rules.
2566 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2568 Here it means: in .txt files, use \"p\" as the key for the
2569 agenda command otherwise associated with \"q\". (The command
2570 originally associated with \"q\" is not displayed to avoid
2571 duplicates.)"
2572 :version "24.3"
2573 :group 'org-agenda-custom-commands
2574 :type '(repeat (list :tag "Rule"
2575 (string :tag " Agenda key")
2576 (string :tag "Replace by command")
2577 (repeat :tag "Available when"
2578 (choice
2579 (cons :tag "Condition"
2580 (choice
2581 (const :tag "In file" in-file)
2582 (const :tag "Not in file" not-in-file)
2583 (const :tag "In buffer" in-buffer)
2584 (const :tag "Not in buffer" not-in-buffer)
2585 (const :tag "In mode" in-mode)
2586 (const :tag "Not in mode" not-in-mode))
2587 (regexp))
2588 (function :tag "Custom function"))))))
2590 (defcustom org-agenda-max-entries nil
2591 "Maximum number of entries to display in an agenda.
2592 This can be nil (no limit) or an integer or an alist of agenda
2593 types with an associated number of entries to display in this
2594 type."
2595 :version "24.4"
2596 :package-version '(Org . "8.0")
2597 :group 'org-agenda-custom-commands
2598 :type '(choice (symbol :tag "No limit" nil)
2599 (integer :tag "Max number of entries")
2600 (repeat
2601 (cons (choice :tag "Agenda type"
2602 (const agenda)
2603 (const todo)
2604 (const tags)
2605 (const search)
2606 (const timeline))
2607 (integer :tag "Max number of entries")))))
2609 (defcustom org-agenda-max-todos nil
2610 "Maximum number of TODOs to display in an agenda.
2611 This can be nil (no limit) or an integer or an alist of agenda
2612 types with an associated number of entries to display in this
2613 type."
2614 :version "24.4"
2615 :package-version '(Org . "8.0")
2616 :group 'org-agenda-custom-commands
2617 :type '(choice (symbol :tag "No limit" nil)
2618 (integer :tag "Max number of TODOs")
2619 (repeat
2620 (cons (choice :tag "Agenda type"
2621 (const agenda)
2622 (const todo)
2623 (const tags)
2624 (const search)
2625 (const timeline))
2626 (integer :tag "Max number of TODOs")))))
2628 (defcustom org-agenda-max-tags nil
2629 "Maximum number of tagged entries to display in an agenda.
2630 This can be nil (no limit) or an integer or an alist of agenda
2631 types with an associated number of entries to display in this
2632 type."
2633 :version "24.4"
2634 :package-version '(Org . "8.0")
2635 :group 'org-agenda-custom-commands
2636 :type '(choice (symbol :tag "No limit" nil)
2637 (integer :tag "Max number of tagged entries")
2638 (repeat
2639 (cons (choice :tag "Agenda type"
2640 (const agenda)
2641 (const todo)
2642 (const tags)
2643 (const search)
2644 (const timeline))
2645 (integer :tag "Max number of tagged entries")))))
2647 (defcustom org-agenda-max-effort nil
2648 "Maximum cumulated effort duration for the agenda.
2649 This can be nil (no limit) or a number of minutes (as an integer)
2650 or an alist of agenda types with an associated number of minutes
2651 to limit entries to in this type."
2652 :version "24.4"
2653 :package-version '(Org . "8.0")
2654 :group 'org-agenda-custom-commands
2655 :type '(choice (symbol :tag "No limit" nil)
2656 (integer :tag "Max number of minutes")
2657 (repeat
2658 (cons (choice :tag "Agenda type"
2659 (const agenda)
2660 (const todo)
2661 (const tags)
2662 (const search)
2663 (const timeline))
2664 (integer :tag "Max number of minutes")))))
2666 (defvar org-agenda-keep-restricted-file-list nil)
2667 (defvar org-keys nil)
2668 (defvar org-match nil)
2669 ;;;###autoload
2670 (defun org-agenda (&optional arg org-keys restriction)
2671 "Dispatch agenda commands to collect entries to the agenda buffer.
2672 Prompts for a command to execute. Any prefix arg will be passed
2673 on to the selected command. The default selections are:
2675 a Call `org-agenda-list' to display the agenda for current day or week.
2676 t Call `org-todo-list' to display the global todo list.
2677 T Call `org-todo-list' to display the global todo list, select only
2678 entries with a specific TODO keyword (the user gets a prompt).
2679 m Call `org-tags-view' to display headlines with tags matching
2680 a condition (the user is prompted for the condition).
2681 M Like `m', but select only TODO entries, no ordinary headlines.
2682 L Create a timeline for the current buffer.
2683 e Export views to associated files.
2684 s Search entries for keywords.
2685 S Search entries for keywords, only with TODO keywords.
2686 / Multi occur across all agenda files and also files listed
2687 in `org-agenda-text-search-extra-files'.
2688 < Restrict agenda commands to buffer, subtree, or region.
2689 Press several times to get the desired effect.
2690 > Remove a previous restriction.
2691 # List \"stuck\" projects.
2692 ! Configure what \"stuck\" means.
2693 C Configure custom agenda commands.
2695 More commands can be added by configuring the variable
2696 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2697 searches can be pre-defined in this way.
2699 If the current buffer is in Org-mode and visiting a file, you can also
2700 first press `<' once to indicate that the agenda should be temporarily
2701 \(until the next use of \\[org-agenda]) restricted to the current file.
2702 Pressing `<' twice means to restrict to the current subtree or region
2703 \(if active)."
2704 (interactive "P")
2705 (catch 'exit
2706 (let* ((prefix-descriptions nil)
2707 (org-agenda-buffer-name org-agenda-buffer-name)
2708 (org-agenda-window-setup (if (equal (buffer-name)
2709 org-agenda-buffer-name)
2710 'current-window
2711 org-agenda-window-setup))
2712 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2713 (org-agenda-custom-commands
2714 ;; normalize different versions
2715 (delq nil
2716 (mapcar
2717 (lambda (x)
2718 (cond ((stringp (cdr x))
2719 (push x prefix-descriptions)
2720 nil)
2721 ((stringp (nth 1 x)) x)
2722 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2723 (t (cons (car x) (cons "" (cdr x))))))
2724 org-agenda-custom-commands)))
2725 (org-agenda-custom-commands
2726 (org-contextualize-keys
2727 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2728 (buf (current-buffer))
2729 (bfn (buffer-file-name (buffer-base-buffer)))
2730 entry key type org-match lprops ans)
2731 ;; Turn off restriction unless there is an overriding one,
2732 (unless org-agenda-overriding-restriction
2733 (unless org-agenda-keep-restricted-file-list
2734 ;; There is a request to keep the file list in place
2735 (put 'org-agenda-files 'org-restrict nil))
2736 (setq org-agenda-restrict nil)
2737 (move-marker org-agenda-restrict-begin nil)
2738 (move-marker org-agenda-restrict-end nil))
2739 ;; Delete old local properties
2740 (put 'org-agenda-redo-command 'org-lprops nil)
2741 ;; Delete previously set last-arguments
2742 (put 'org-agenda-redo-command 'last-args nil)
2743 ;; Remember where this call originated
2744 (setq org-agenda-last-dispatch-buffer (current-buffer))
2745 (unless org-keys
2746 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2747 org-keys (car ans)
2748 restriction (cdr ans)))
2749 ;; If we have sticky agenda buffers, set a name for the buffer,
2750 ;; depending on the invoking keys. The user may still set this
2751 ;; as a command option, which will overwrite what we do here.
2752 (if org-agenda-sticky
2753 (setq org-agenda-buffer-name
2754 (format "*Org Agenda(%s)*" org-keys)))
2755 ;; Establish the restriction, if any
2756 (when (and (not org-agenda-overriding-restriction) restriction)
2757 (put 'org-agenda-files 'org-restrict (list bfn))
2758 (cond
2759 ((eq restriction 'region)
2760 (setq org-agenda-restrict (current-buffer))
2761 (move-marker org-agenda-restrict-begin (region-beginning))
2762 (move-marker org-agenda-restrict-end (region-end)))
2763 ((eq restriction 'subtree)
2764 (save-excursion
2765 (setq org-agenda-restrict (current-buffer))
2766 (org-back-to-heading t)
2767 (move-marker org-agenda-restrict-begin (point))
2768 (move-marker org-agenda-restrict-end
2769 (progn (org-end-of-subtree t)))))))
2771 ;; For example the todo list should not need it (but does...)
2772 (cond
2773 ((setq entry (assoc org-keys org-agenda-custom-commands))
2774 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2775 (progn
2776 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2777 lprops (nth 4 entry))
2778 (if org-agenda-sticky
2779 (setq org-agenda-buffer-name
2780 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2781 (format "*Org Agenda(%s)*" org-keys))))
2782 (put 'org-agenda-redo-command 'org-lprops lprops)
2783 (cond
2784 ((eq type 'agenda)
2785 (org-let lprops '(org-agenda-list current-prefix-arg)))
2786 ((eq type 'agenda*)
2787 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2788 ((eq type 'alltodo)
2789 (org-let lprops '(org-todo-list current-prefix-arg)))
2790 ((eq type 'search)
2791 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2792 ((eq type 'stuck)
2793 (org-let lprops '(org-agenda-list-stuck-projects
2794 current-prefix-arg)))
2795 ((eq type 'tags)
2796 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2797 ((eq type 'tags-todo)
2798 (org-let lprops '(org-tags-view '(4) org-match)))
2799 ((eq type 'todo)
2800 (org-let lprops '(org-todo-list org-match)))
2801 ((eq type 'tags-tree)
2802 (org-check-for-org-mode)
2803 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2804 ((eq type 'todo-tree)
2805 (org-check-for-org-mode)
2806 (org-let lprops
2807 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2808 (regexp-quote org-match) "\\>"))))
2809 ((eq type 'occur-tree)
2810 (org-check-for-org-mode)
2811 (org-let lprops '(org-occur org-match)))
2812 ((functionp type)
2813 (org-let lprops '(funcall type org-match)))
2814 ((fboundp type)
2815 (org-let lprops '(funcall type org-match)))
2816 (t (user-error "Invalid custom agenda command type %s" type))))
2817 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2818 ((equal org-keys "C")
2819 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2820 (customize-variable 'org-agenda-custom-commands))
2821 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2822 ((equal org-keys "s") (call-interactively 'org-search-view))
2823 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2824 ((equal org-keys "t") (call-interactively 'org-todo-list))
2825 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2826 ((equal org-keys "m") (call-interactively 'org-tags-view))
2827 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2828 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2829 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2830 (add-hook
2831 'post-command-hook
2832 (lambda ()
2833 (unless (current-message)
2834 (let* ((m (org-agenda-get-any-marker))
2835 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2836 (when note
2837 (message (concat
2838 "FLAGGING-NOTE ([?] for more info): "
2839 (org-add-props
2840 (replace-regexp-in-string
2841 "\\\\n" "//"
2842 (copy-sequence note))
2843 nil 'face 'org-warning)))))))
2844 t t))
2845 ((equal org-keys "L")
2846 (unless (derived-mode-p 'org-mode)
2847 (user-error "This is not an Org-mode file"))
2848 (unless restriction
2849 (put 'org-agenda-files 'org-restrict (list bfn))
2850 (org-call-with-arg 'org-timeline arg)))
2851 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2852 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2853 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2854 (t (user-error "Invalid agenda key"))))))
2856 (defvar org-agenda-multi)
2858 (defun org-agenda-append-agenda ()
2859 "Append another agenda view to the current one.
2860 This function allows interactive building of block agendas.
2861 Agenda views are separated by `org-agenda-block-separator'."
2862 (interactive)
2863 (unless (derived-mode-p 'org-agenda-mode)
2864 (user-error "Can only append from within agenda buffer"))
2865 (let ((org-agenda-multi t))
2866 (org-agenda)
2867 (widen)
2868 (org-agenda-finalize)
2869 (setq buffer-read-only t)
2870 (org-agenda-fit-window-to-buffer)))
2872 (defun org-agenda-normalize-custom-commands (cmds)
2873 "Normalize custom commands CMDS."
2874 (delq nil
2875 (mapcar
2876 (lambda (x)
2877 (cond ((stringp (cdr x)) nil)
2878 ((stringp (nth 1 x)) x)
2879 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2880 (t (cons (car x) (cons "" (cdr x))))))
2881 cmds)))
2883 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2884 "The user interface for selecting an agenda command."
2885 (catch 'exit
2886 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2887 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2888 (region-p (org-region-active-p))
2889 (custom org-agenda-custom-commands)
2890 (selstring "")
2891 restriction second-time
2892 c entry key type match prefixes rmheader header-end custom1 desc
2893 line lines left right n n1)
2894 (save-window-excursion
2895 (delete-other-windows)
2896 (org-switch-to-buffer-other-window " *Agenda Commands*")
2897 (erase-buffer)
2898 (insert (eval-when-compile
2899 (let ((header
2900 "Press key for an agenda command: < Buffer, subtree/region restriction
2901 -------------------------------- > Remove restriction
2902 a Agenda for current week or day e Export agenda views
2903 t List of all TODO entries T Entries with special TODO kwd
2904 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2905 s Search for keywords S Like s, but only TODO entries
2906 L Timeline for current buffer # List stuck projects (!=configure)
2907 / Multi-occur C Configure custom agenda commands
2908 ? Find :FLAGGED: entries * Toggle sticky agenda views
2910 (start 0))
2911 (while (string-match
2912 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2913 header start)
2914 (setq start (match-end 0))
2915 (add-text-properties (match-beginning 2) (match-end 2)
2916 '(face bold) header))
2917 header)))
2918 (setq header-end (point-marker))
2919 (while t
2920 (setq custom1 custom)
2921 (when (eq rmheader t)
2922 (org-goto-line 1)
2923 (re-search-forward ":" nil t)
2924 (delete-region (match-end 0) (point-at-eol))
2925 (forward-char 1)
2926 (looking-at "-+")
2927 (delete-region (match-end 0) (point-at-eol))
2928 (move-marker header-end (match-end 0)))
2929 (goto-char header-end)
2930 (delete-region (point) (point-max))
2932 ;; Produce all the lines that describe custom commands and prefixes
2933 (setq lines nil)
2934 (while (setq entry (pop custom1))
2935 (setq key (car entry) desc (nth 1 entry)
2936 type (nth 2 entry)
2937 match (nth 3 entry))
2938 (if (> (length key) 1)
2939 (add-to-list 'prefixes (string-to-char key))
2940 (setq line
2941 (format
2942 "%-4s%-14s"
2943 (org-add-props (copy-sequence key)
2944 '(face bold))
2945 (cond
2946 ((string-match "\\S-" desc) desc)
2947 ((eq type 'agenda) "Agenda for current week or day")
2948 ((eq type 'agenda*) "Appointments for current week or day")
2949 ((eq type 'alltodo) "List of all TODO entries")
2950 ((eq type 'search) "Word search")
2951 ((eq type 'stuck) "List of stuck projects")
2952 ((eq type 'todo) "TODO keyword")
2953 ((eq type 'tags) "Tags query")
2954 ((eq type 'tags-todo) "Tags (TODO)")
2955 ((eq type 'tags-tree) "Tags tree")
2956 ((eq type 'todo-tree) "TODO kwd tree")
2957 ((eq type 'occur-tree) "Occur tree")
2958 ((functionp type) (if (symbolp type)
2959 (symbol-name type)
2960 "Lambda expression"))
2961 (t "???"))))
2962 (if org-agenda-menu-show-matcher
2963 (setq line
2964 (concat line ": "
2965 (cond
2966 ((stringp match)
2967 (setq match (copy-sequence match))
2968 (org-add-props match nil 'face 'org-warning))
2969 ((listp type)
2970 (format "set of %d commands" (length type))))))
2971 (if (org-string-nw-p match)
2972 (add-text-properties
2973 0 (length line) (list 'help-echo
2974 (concat "Matcher: " match)) line)))
2975 (push line lines)))
2976 (setq lines (nreverse lines))
2977 (when prefixes
2978 (mapc (lambda (x)
2979 (push
2980 (format "%s %s"
2981 (org-add-props (char-to-string x)
2982 nil 'face 'bold)
2983 (or (cdr (assoc (concat selstring
2984 (char-to-string x))
2985 prefix-descriptions))
2986 "Prefix key"))
2987 lines))
2988 prefixes))
2990 ;; Check if we should display in two columns
2991 (if org-agenda-menu-two-columns
2992 (progn
2993 (setq n (length lines)
2994 n1 (+ (/ n 2) (mod n 2))
2995 right (nthcdr n1 lines)
2996 left (copy-sequence lines))
2997 (setcdr (nthcdr (1- n1) left) nil))
2998 (setq left lines right nil))
2999 (while left
3000 (insert "\n" (pop left))
3001 (when right
3002 (if (< (current-column) 40)
3003 (move-to-column 40 t)
3004 (insert " "))
3005 (insert (pop right))))
3007 ;; Make the window the right size
3008 (goto-char (point-min))
3009 (if second-time
3010 (if (not (pos-visible-in-window-p (point-max)))
3011 (org-fit-window-to-buffer))
3012 (setq second-time t)
3013 (org-fit-window-to-buffer))
3015 ;; Ask for selection
3016 (message "Press key for agenda command%s:"
3017 (if (or restrict-ok org-agenda-overriding-restriction)
3018 (if org-agenda-overriding-restriction
3019 " (restriction lock active)"
3020 (if restriction
3021 (format " (restricted to %s)" restriction)
3022 " (unrestricted)"))
3023 ""))
3024 (setq c (read-char-exclusive))
3025 (message "")
3026 (cond
3027 ((assoc (char-to-string c) custom)
3028 (setq selstring (concat selstring (char-to-string c)))
3029 (throw 'exit (cons selstring restriction)))
3030 ((memq c prefixes)
3031 (setq selstring (concat selstring (char-to-string c))
3032 prefixes nil
3033 rmheader (or rmheader t)
3034 custom (delq nil (mapcar
3035 (lambda (x)
3036 (if (or (= (length (car x)) 1)
3037 (/= (string-to-char (car x)) c))
3039 (cons (substring (car x) 1) (cdr x))))
3040 custom))))
3041 ((eq c ?*)
3042 (call-interactively 'org-toggle-sticky-agenda)
3043 (sit-for 2))
3044 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3045 (message "Restriction is only possible in Org-mode buffers")
3046 (ding) (sit-for 1))
3047 ((eq c ?1)
3048 (org-agenda-remove-restriction-lock 'noupdate)
3049 (setq restriction 'buffer))
3050 ((eq c ?0)
3051 (org-agenda-remove-restriction-lock 'noupdate)
3052 (setq restriction (if region-p 'region 'subtree)))
3053 ((eq c ?<)
3054 (org-agenda-remove-restriction-lock 'noupdate)
3055 (setq restriction
3056 (cond
3057 ((eq restriction 'buffer)
3058 (if region-p 'region 'subtree))
3059 ((memq restriction '(subtree region))
3060 nil)
3061 (t 'buffer))))
3062 ((eq c ?>)
3063 (org-agenda-remove-restriction-lock 'noupdate)
3064 (setq restriction nil))
3065 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3066 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3067 ((and (> (length selstring) 0) (eq c ?\d))
3068 (delete-window)
3069 (org-agenda-get-restriction-and-command prefix-descriptions))
3071 ((equal c ?q) (error "Abort"))
3072 (t (user-error "Invalid key %c" c))))))))
3074 (defun org-agenda-fit-window-to-buffer ()
3075 "Fit the window to the buffer size."
3076 (and (memq org-agenda-window-setup '(reorganize-frame))
3077 (fboundp 'fit-window-to-buffer)
3078 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3079 (= (car org-agenda-window-frame-fractions) 1.0))
3080 (delete-other-windows)
3081 (org-fit-window-to-buffer
3083 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3084 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3086 (defvar org-cmd nil)
3087 (defvar org-agenda-overriding-cmd nil)
3088 (defvar org-agenda-overriding-arguments nil)
3089 (defvar org-agenda-overriding-cmd-arguments nil)
3090 (defun org-agenda-run-series (name series)
3091 "Run agenda NAME as a SERIES of agenda commands."
3092 (org-let (nth 1 series) '(org-agenda-prepare name))
3093 ;; We need to reset agenda markers here, because when constructing a
3094 ;; block agenda, the individual blocks do not do that.
3095 (org-agenda-reset-markers)
3096 (let* ((org-agenda-multi t)
3097 (redo (list 'org-agenda-run-series name (list 'quote series)))
3098 (cmds (car series))
3099 (gprops (nth 1 series))
3100 match ;; The byte compiler incorrectly complains about this. Keep it!
3101 org-cmd type lprops)
3102 (while (setq org-cmd (pop cmds))
3103 (setq type (car org-cmd))
3104 (setq match (eval (nth 1 org-cmd)))
3105 (setq lprops (nth 2 org-cmd))
3106 (let ((org-agenda-overriding-arguments
3107 (if (eq org-agenda-overriding-cmd org-cmd)
3108 (or org-agenda-overriding-arguments
3109 org-agenda-overriding-cmd-arguments))))
3110 (cond
3111 ((eq type 'agenda)
3112 (org-let2 gprops lprops
3113 '(call-interactively 'org-agenda-list)))
3114 ((eq type 'agenda*)
3115 (org-let2 gprops lprops
3116 '(funcall 'org-agenda-list nil nil t)))
3117 ((eq type 'alltodo)
3118 (org-let2 gprops lprops
3119 '(call-interactively 'org-todo-list)))
3120 ((eq type 'search)
3121 (org-let2 gprops lprops
3122 '(org-search-view current-prefix-arg match nil)))
3123 ((eq type 'stuck)
3124 (org-let2 gprops lprops
3125 '(call-interactively 'org-agenda-list-stuck-projects)))
3126 ((eq type 'tags)
3127 (org-let2 gprops lprops
3128 '(org-tags-view current-prefix-arg match)))
3129 ((eq type 'tags-todo)
3130 (org-let2 gprops lprops
3131 '(org-tags-view '(4) match)))
3132 ((eq type 'todo)
3133 (org-let2 gprops lprops
3134 '(org-todo-list match)))
3135 ((fboundp type)
3136 (org-let2 gprops lprops
3137 '(funcall type match)))
3138 (t (error "Invalid type in command series")))))
3139 (widen)
3140 (let ((inhibit-read-only t))
3141 (add-text-properties (point-min) (point-max)
3142 `(org-series t org-series-redo-cmd ,redo)))
3143 (setq org-agenda-redo-command redo)
3144 (goto-char (point-min)))
3145 (org-agenda-fit-window-to-buffer)
3146 (org-let (nth 1 series) '(org-agenda-finalize)))
3148 ;;;###autoload
3149 (defmacro org-batch-agenda (cmd-key &rest parameters)
3150 "Run an agenda command in batch mode and send the result to STDOUT.
3151 If CMD-KEY is a string of length 1, it is used as a key in
3152 `org-agenda-custom-commands' and triggers this command. If it is a
3153 longer string it is used as a tags/todo match string.
3154 Parameters are alternating variable names and values that will be bound
3155 before running the agenda command."
3156 (org-eval-in-environment (org-make-parameter-alist parameters)
3157 (let (org-agenda-sticky)
3158 (if (> (length cmd-key) 1)
3159 (org-tags-view nil cmd-key)
3160 (org-agenda nil cmd-key))))
3161 (set-buffer org-agenda-buffer-name)
3162 (princ (buffer-string)))
3164 (defvar org-agenda-info nil)
3166 ;;;###autoload
3167 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3168 "Run an agenda command in batch mode and send the result to STDOUT.
3169 If CMD-KEY is a string of length 1, it is used as a key in
3170 `org-agenda-custom-commands' and triggers this command. If it is a
3171 longer string it is used as a tags/todo match string.
3172 Parameters are alternating variable names and values that will be bound
3173 before running the agenda command.
3175 The output gives a line for each selected agenda item. Each
3176 item is a list of comma-separated values, like this:
3178 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3180 category The category of the item
3181 head The headline, without TODO kwd, TAGS and PRIORITY
3182 type The type of the agenda entry, can be
3183 todo selected in TODO match
3184 tagsmatch selected in tags match
3185 diary imported from diary
3186 deadline a deadline on given date
3187 scheduled scheduled on given date
3188 timestamp entry has timestamp on given date
3189 closed entry was closed on given date
3190 upcoming-deadline warning about deadline
3191 past-scheduled forwarded scheduled item
3192 block entry has date block including g. date
3193 todo The todo keyword, if any
3194 tags All tags including inherited ones, separated by colons
3195 date The relevant date, like 2007-2-14
3196 time The time, like 15:00-16:50
3197 extra Sting with extra planning info
3198 priority-l The priority letter if any was given
3199 priority-n The computed numerical priority
3200 agenda-day The day in the agenda where this is listed"
3201 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3202 (org-make-parameter-alist parameters))
3203 (if (> (length cmd-key) 2)
3204 (org-tags-view nil cmd-key)
3205 (org-agenda nil cmd-key)))
3206 (set-buffer org-agenda-buffer-name)
3207 (let* ((lines (org-split-string (buffer-string) "\n"))
3208 line)
3209 (while (setq line (pop lines))
3210 (catch 'next
3211 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3212 (setq org-agenda-info
3213 (org-fix-agenda-info (text-properties-at 0 line)))
3214 (princ
3215 (mapconcat 'org-agenda-export-csv-mapper
3216 '(org-category txt type todo tags date time extra
3217 priority-letter priority agenda-day)
3218 ","))
3219 (princ "\n")))))
3221 (defun org-fix-agenda-info (props)
3222 "Make sure all properties on an agenda item have a canonical form.
3223 This ensures the export commands can easily use it."
3224 (let (tmp re)
3225 (when (setq tmp (plist-get props 'tags))
3226 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3227 (when (setq tmp (plist-get props 'date))
3228 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3229 (let ((calendar-date-display-form '(year "-" month "-" day)))
3230 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3232 (setq tmp (calendar-date-string tmp)))
3233 (setq props (plist-put props 'date tmp)))
3234 (when (setq tmp (plist-get props 'day))
3235 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3236 (let ((calendar-date-display-form '(year "-" month "-" day)))
3237 (setq tmp (calendar-date-string tmp)))
3238 (setq props (plist-put props 'day tmp))
3239 (setq props (plist-put props 'agenda-day tmp)))
3240 (when (setq tmp (plist-get props 'txt))
3241 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3242 (plist-put props 'priority-letter (match-string 1 tmp))
3243 (setq tmp (replace-match "" t t tmp)))
3244 (when (and (setq re (plist-get props 'org-todo-regexp))
3245 (setq re (concat "\\`\\.*" re " ?"))
3246 (string-match re tmp))
3247 (plist-put props 'todo (match-string 1 tmp))
3248 (setq tmp (replace-match "" t t tmp)))
3249 (plist-put props 'txt tmp)))
3250 props)
3252 (defun org-agenda-export-csv-mapper (prop)
3253 (let ((res (plist-get org-agenda-info prop)))
3254 (setq res
3255 (cond
3256 ((not res) "")
3257 ((stringp res) res)
3258 (t (prin1-to-string res))))
3259 (while (string-match "," res)
3260 (setq res (replace-match ";" t t res)))
3261 (org-trim res)))
3263 ;;;###autoload
3264 (defun org-store-agenda-views (&rest parameters)
3265 "Store agenda views."
3266 (interactive)
3267 (eval (list 'org-batch-store-agenda-views)))
3269 ;;;###autoload
3270 (defmacro org-batch-store-agenda-views (&rest parameters)
3271 "Run all custom agenda commands that have a file argument."
3272 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3273 (pop-up-frames nil)
3274 (dir default-directory)
3275 (pars (org-make-parameter-alist parameters))
3276 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3277 (save-window-excursion
3278 (while cmds
3279 (setq cmd (pop cmds)
3280 thiscmdkey (car cmd)
3281 thiscmdcmd (cdr cmd)
3282 match (nth 2 thiscmdcmd)
3283 bufname (if org-agenda-sticky
3284 (or (and (stringp match)
3285 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3286 (format "*Org Agenda(%s)*" thiscmdkey))
3287 org-agenda-buffer-name)
3288 cmd-or-set (nth 2 cmd)
3289 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3290 files (nth (if (listp cmd-or-set) 4 5) cmd))
3291 (if (stringp files) (setq files (list files)))
3292 (when files
3293 (org-eval-in-environment (append org-agenda-exporter-settings
3294 opts pars)
3295 (org-agenda nil thiscmdkey))
3296 (set-buffer bufname)
3297 (while files
3298 (org-eval-in-environment (append org-agenda-exporter-settings
3299 opts pars)
3300 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3301 (and (get-buffer bufname)
3302 (kill-buffer bufname)))))))
3304 (defvar org-agenda-current-span nil
3305 "The current span used in the agenda view.") ; local variable in the agenda buffer
3306 (defun org-agenda-mark-header-line (pos)
3307 "Mark the line at POS as an agenda structure header."
3308 (save-excursion
3309 (goto-char pos)
3310 (put-text-property (point-at-bol) (point-at-eol)
3311 'org-agenda-structural-header t)
3312 (when org-agenda-title-append
3313 (put-text-property (point-at-bol) (point-at-eol)
3314 'org-agenda-title-append org-agenda-title-append))))
3316 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3317 (defvar org-agenda-write-buffer-name "Agenda View")
3318 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3319 "Write the current buffer (an agenda view) as a file.
3321 Depending on the extension of the file name, plain text (.txt),
3322 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3323 If the extension is .ics, translate visible agenda into iCalendar
3324 format. If the extension is .org, collect all subtrees
3325 corresponding to the agenda entries and add them in an .org file.
3327 With prefix argument OPEN, open the new file immediately. If
3328 NOSETTINGS is given, do not scope the settings of
3329 `org-agenda-exporter-settings' into the export commands. This is
3330 used when the settings have already been scoped and we do not
3331 wish to overrule other, higher priority settings. If
3332 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3333 the agenda to write."
3334 (interactive "FWrite agenda to file: \nP")
3335 (if (or (not (file-writable-p file))
3336 (and (file-exists-p file)
3337 (if (called-interactively-p 'any)
3338 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3339 (user-error "Cannot write agenda to file %s" file))
3340 (org-let (if nosettings nil org-agenda-exporter-settings)
3341 '(save-excursion
3342 (save-window-excursion
3343 (let ((bs (copy-sequence (buffer-string))) beg content)
3344 (with-temp-buffer
3345 (rename-buffer org-agenda-write-buffer-name t)
3346 (set-buffer-modified-p nil)
3347 (insert bs)
3348 (org-agenda-remove-marked-text 'invisible 'org-filtered)
3349 (run-hooks 'org-agenda-before-write-hook)
3350 (cond
3351 ((bound-and-true-p org-mobile-creating-agendas)
3352 (org-mobile-write-agenda-for-mobile file))
3353 ((string-match "\\.org\\'" file)
3354 (let (content p m message-log-max)
3355 (goto-char (point-min))
3356 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3357 (goto-char p)
3358 (setq m (get-text-property (point) 'org-hd-marker))
3359 (when m
3360 (push (save-excursion
3361 (set-buffer (marker-buffer m))
3362 (goto-char m)
3363 (org-copy-subtree 1 nil t t)
3364 org-subtree-clip)
3365 content)))
3366 (find-file file)
3367 (erase-buffer)
3368 (dolist (s content) (org-paste-subtree 1 s))
3369 (write-file file)
3370 (kill-buffer (current-buffer))
3371 (message "Org file written to %s" file)))
3372 ((string-match "\\.html?\\'" file)
3373 (require 'htmlize)
3374 (set-buffer (htmlize-buffer (current-buffer)))
3375 (when org-agenda-export-html-style
3376 ;; replace <style> section with org-agenda-export-html-style
3377 (goto-char (point-min))
3378 (kill-region (- (search-forward "<style") 6)
3379 (search-forward "</style>"))
3380 (insert org-agenda-export-html-style))
3381 (write-file file)
3382 (kill-buffer (current-buffer))
3383 (message "HTML written to %s" file))
3384 ((string-match "\\.ps\\'" file)
3385 (require 'ps-print)
3386 (ps-print-buffer-with-faces file)
3387 (message "Postscript written to %s" file))
3388 ((string-match "\\.pdf\\'" file)
3389 (require 'ps-print)
3390 (ps-print-buffer-with-faces
3391 (concat (file-name-sans-extension file) ".ps"))
3392 (call-process "ps2pdf" nil nil nil
3393 (expand-file-name
3394 (concat (file-name-sans-extension file) ".ps"))
3395 (expand-file-name file))
3396 (delete-file (concat (file-name-sans-extension file) ".ps"))
3397 (message "PDF written to %s" file))
3398 ((string-match "\\.ics\\'" file)
3399 (require 'ox-icalendar)
3400 (org-icalendar-export-current-agenda (expand-file-name file)))
3402 (let ((bs (buffer-string)))
3403 (find-file file)
3404 (erase-buffer)
3405 (insert bs)
3406 (save-buffer 0)
3407 (kill-buffer (current-buffer))
3408 (message "Plain text written to %s" file))))))))
3409 (set-buffer (or agenda-bufname
3410 (and (called-interactively-p 'any) (buffer-name))
3411 org-agenda-buffer-name)))
3412 (when open (org-open-file file)))
3414 (defun org-agenda-remove-marked-text (property &optional value)
3415 "Delete all text marked with VALUE of PROPERTY.
3416 VALUE defaults to t."
3417 (let (beg)
3418 (setq value (or value t))
3419 (while (setq beg (text-property-any (point-min) (point-max)
3420 property value))
3421 (delete-region
3422 beg (or (next-single-property-change beg property)
3423 (point-max))))))
3425 (defun org-agenda-add-entry-text ()
3426 "Add entry text to agenda lines.
3427 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3428 entry text following headings shown in the agenda.
3429 Drawers will be excluded, also the line with scheduling/deadline info."
3430 (when (and (> org-agenda-add-entry-text-maxlines 0)
3431 (not (bound-and-true-p org-mobile-creating-agendas)))
3432 (let (m txt)
3433 (goto-char (point-min))
3434 (while (not (eobp))
3435 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3436 (beginning-of-line 2)
3437 (setq txt (org-agenda-get-some-entry-text
3438 m org-agenda-add-entry-text-maxlines " > "))
3439 (end-of-line 1)
3440 (if (string-match "\\S-" txt)
3441 (insert "\n" txt)
3442 (or (eobp) (forward-char 1))))))))
3444 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3445 &rest keep)
3446 "Extract entry text from MARKER, at most N-LINES lines.
3447 This will ignore drawers etc, just get the text.
3448 If INDENT is given, prefix every line with this string. If KEEP is
3449 given, it is a list of symbols, defining stuff that should not be
3450 removed from the entry content. Currently only `planning' is allowed here."
3451 (let (txt drawer-re kwd-time-re ind)
3452 (save-excursion
3453 (with-current-buffer (marker-buffer marker)
3454 (if (not (derived-mode-p 'org-mode))
3455 (setq txt "")
3456 (save-excursion
3457 (save-restriction
3458 (widen)
3459 (goto-char marker)
3460 (end-of-line 1)
3461 (setq txt (buffer-substring
3462 (min (1+ (point)) (point-max))
3463 (progn (outline-next-heading) (point)))
3464 drawer-re org-drawer-regexp
3465 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3466 ".*\n?"))
3467 (with-temp-buffer
3468 (insert txt)
3469 (when org-agenda-add-entry-text-descriptive-links
3470 (goto-char (point-min))
3471 (while (org-activate-bracket-links (point-max))
3472 (add-text-properties (match-beginning 0) (match-end 0)
3473 '(face org-link))))
3474 (goto-char (point-min))
3475 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3476 (set-text-properties (match-beginning 0) (match-end 0)
3477 nil))
3478 (goto-char (point-min))
3479 (while (re-search-forward drawer-re nil t)
3480 (delete-region
3481 (match-beginning 0)
3482 (progn (re-search-forward
3483 "^[ \t]*:END:.*\n?" nil 'move)
3484 (point))))
3485 (unless (member 'planning keep)
3486 (goto-char (point-min))
3487 (while (re-search-forward kwd-time-re nil t)
3488 (replace-match "")))
3489 (goto-char (point-min))
3490 (when org-agenda-entry-text-exclude-regexps
3491 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3492 (while (setq re (pop re-list))
3493 (goto-char (point-min))
3494 (while (re-search-forward re nil t)
3495 (replace-match "")))))
3496 (goto-char (point-max))
3497 (skip-chars-backward " \t\n")
3498 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3500 ;; find and remove min common indentation
3501 (goto-char (point-min))
3502 (untabify (point-min) (point-max))
3503 (setq ind (org-get-indentation))
3504 (while (not (eobp))
3505 (unless (looking-at "[ \t]*$")
3506 (setq ind (min ind (org-get-indentation))))
3507 (beginning-of-line 2))
3508 (goto-char (point-min))
3509 (while (not (eobp))
3510 (unless (looking-at "[ \t]*$")
3511 (move-to-column ind)
3512 (delete-region (point-at-bol) (point)))
3513 (beginning-of-line 2))
3515 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3517 (goto-char (point-min))
3518 (when indent
3519 (while (and (not (eobp)) (re-search-forward "^" nil t))
3520 (replace-match indent t t)))
3521 (goto-char (point-min))
3522 (while (looking-at "[ \t]*\n") (replace-match ""))
3523 (goto-char (point-max))
3524 (when (> (org-current-line)
3525 n-lines)
3526 (org-goto-line (1+ n-lines))
3527 (backward-char 1))
3528 (setq txt (buffer-substring (point-min) (point)))))))))
3529 txt))
3531 (defun org-check-for-org-mode ()
3532 "Make sure current buffer is in org-mode. Error if not."
3533 (or (derived-mode-p 'org-mode)
3534 (error "Cannot execute org-mode agenda command on buffer in %s"
3535 major-mode)))
3537 ;;; Agenda prepare and finalize
3539 (defvar org-agenda-multi nil) ; dynamically scoped
3540 (defvar org-agenda-pre-window-conf nil)
3541 (defvar org-agenda-columns-active nil)
3542 (defvar org-agenda-name nil)
3543 (defvar org-agenda-tag-filter nil)
3544 (defvar org-agenda-category-filter nil)
3545 (defvar org-agenda-regexp-filter nil)
3546 (defvar org-agenda-effort-filter nil)
3547 (defvar org-agenda-top-headline-filter nil)
3548 (defvar org-agenda-tag-filter-preset nil
3549 "A preset of the tags filter used for secondary agenda filtering.
3550 This must be a list of strings, each string must be a single tag preceded
3551 by \"+\" or \"-\".
3552 This variable should not be set directly, but agenda custom commands can
3553 bind it in the options section. The preset filter is a global property of
3554 the entire agenda view. In a block agenda, it will not work reliably to
3555 define a filter for one of the individual blocks. You need to set it in
3556 the global options and expect it to be applied to the entire view.")
3558 (defvar org-agenda-category-filter-preset nil
3559 "A preset of the category filter used for secondary agenda filtering.
3560 This must be a list of strings, each string must be a single category
3561 preceded by \"+\" or \"-\".
3562 This variable should not be set directly, but agenda custom commands can
3563 bind it in the options section. The preset filter is a global property of
3564 the entire agenda view. In a block agenda, it will not work reliably to
3565 define a filter for one of the individual blocks. You need to set it in
3566 the global options and expect it to be applied to the entire view.")
3568 (defvar org-agenda-regexp-filter-preset nil
3569 "A preset of the regexp filter used for secondary agenda filtering.
3570 This must be a list of strings, each string must be a single regexp
3571 preceded by \"+\" or \"-\".
3572 This variable should not be set directly, but agenda custom commands can
3573 bind it in the options section. The preset filter is a global property of
3574 the entire agenda view. In a block agenda, it will not work reliably to
3575 define a filter for one of the individual blocks. You need to set it in
3576 the global options and expect it to be applied to the entire view.")
3578 (defvar org-agenda-effort-filter-preset nil
3579 "A preset of the effort condition used for secondary agenda filtering.
3580 This must be a list of strings, each string must be a single regexp
3581 preceded by \"+\" or \"-\".
3582 This variable should not be set directly, but agenda custom commands can
3583 bind it in the options section. The preset filter is a global property of
3584 the entire agenda view. In a block agenda, it will not work reliably to
3585 define a filter for one of the individual blocks. You need to set it in
3586 the global options and expect it to be applied to the entire view.")
3588 (defun org-agenda-use-sticky-p ()
3589 "Return non-nil if an agenda buffer named
3590 `org-agenda-buffer-name' exists and should be shown instead of
3591 generating a new one."
3592 (and
3593 ;; turned off by user
3594 org-agenda-sticky
3595 ;; For multi-agenda buffer already exists
3596 (not org-agenda-multi)
3597 ;; buffer found
3598 (get-buffer org-agenda-buffer-name)
3599 ;; C-u parameter is same as last call
3600 (with-current-buffer (get-buffer org-agenda-buffer-name)
3601 (and
3602 (equal current-prefix-arg
3603 org-agenda-last-prefix-arg)
3604 ;; In case user turned stickiness on, while having existing
3605 ;; Agenda buffer active, don't reuse that buffer, because it
3606 ;; does not have org variables local
3607 org-agenda-this-buffer-is-sticky))))
3609 (defun org-agenda-prepare-window (abuf filter-alist)
3610 "Setup agenda buffer in the window.
3611 ABUF is the buffer for the agenda window.
3612 FILTER-ALIST is an alist of filters we need to apply when
3613 `org-agenda-persistent-filter' is non-nil."
3614 (let* ((awin (get-buffer-window abuf)) wconf)
3615 (cond
3616 ((equal (current-buffer) abuf) nil)
3617 (awin (select-window awin))
3618 ((not (setq wconf (current-window-configuration))))
3619 ((equal org-agenda-window-setup 'current-window)
3620 (pop-to-buffer-same-window abuf))
3621 ((equal org-agenda-window-setup 'other-window)
3622 (org-switch-to-buffer-other-window abuf))
3623 ((equal org-agenda-window-setup 'other-frame)
3624 (switch-to-buffer-other-frame abuf))
3625 ((eq org-agenda-window-setup 'only-window)
3626 (delete-other-windows)
3627 (pop-to-buffer-same-window abuf))
3628 ((equal org-agenda-window-setup 'reorganize-frame)
3629 (delete-other-windows)
3630 (org-switch-to-buffer-other-window abuf)))
3631 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3632 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3633 (setq org-agenda-effort-filter (cdr (assoc 'effort filter-alist)))
3634 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3635 ;; Additional test in case agenda is invoked from within agenda
3636 ;; buffer via elisp link.
3637 (unless (equal (current-buffer) abuf)
3638 (pop-to-buffer-same-window abuf))
3639 (setq org-agenda-pre-window-conf
3640 (or wconf org-agenda-pre-window-conf))))
3642 (defun org-agenda-prepare (&optional name)
3643 (let ((filter-alist (if org-agenda-persistent-filter
3644 (with-current-buffer
3645 (get-buffer-create org-agenda-buffer-name)
3646 (list `(tag . ,org-agenda-tag-filter)
3647 `(re . ,org-agenda-regexp-filter)
3648 `(effort . ,org-agenda-effort-filter)
3649 `(cat . ,org-agenda-category-filter))))))
3650 (if (org-agenda-use-sticky-p)
3651 (progn
3652 (put 'org-agenda-tag-filter :preset-filter nil)
3653 (put 'org-agenda-category-filter :preset-filter nil)
3654 (put 'org-agenda-regexp-filter :preset-filter nil)
3655 ;; Popup existing buffer
3656 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3657 filter-alist)
3658 (message "Sticky Agenda buffer, use `r' to refresh")
3659 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3660 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3661 (setq org-todo-keywords-for-agenda nil)
3662 (put 'org-agenda-tag-filter :preset-filter
3663 org-agenda-tag-filter-preset)
3664 (put 'org-agenda-category-filter :preset-filter
3665 org-agenda-category-filter-preset)
3666 (put 'org-agenda-regexp-filter :preset-filter
3667 org-agenda-regexp-filter-preset)
3668 (put 'org-agenda-effort-filter :preset-filter
3669 org-agenda-effort-filter-preset)
3670 (if org-agenda-multi
3671 (progn
3672 (setq buffer-read-only nil)
3673 (goto-char (point-max))
3674 (unless (or (bobp) org-agenda-compact-blocks
3675 (not org-agenda-block-separator))
3676 (insert "\n"
3677 (if (stringp org-agenda-block-separator)
3678 org-agenda-block-separator
3679 (make-string (window-width) org-agenda-block-separator))
3680 "\n"))
3681 (narrow-to-region (point) (point-max)))
3682 (setq org-done-keywords-for-agenda nil)
3683 ;; Setting any org variables that are in org-agenda-local-vars
3684 ;; list need to be done after the prepare call
3685 (org-agenda-prepare-window
3686 (get-buffer-create org-agenda-buffer-name) filter-alist)
3687 (setq buffer-read-only nil)
3688 (org-agenda-reset-markers)
3689 (let ((inhibit-read-only t)) (erase-buffer))
3690 (org-agenda-mode)
3691 (setq org-agenda-buffer (current-buffer))
3692 (setq org-agenda-contributing-files nil)
3693 (setq org-agenda-columns-active nil)
3694 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3695 (setq org-todo-keywords-for-agenda
3696 (org-uniquify org-todo-keywords-for-agenda))
3697 (setq org-done-keywords-for-agenda
3698 (org-uniquify org-done-keywords-for-agenda))
3699 (setq org-agenda-last-prefix-arg current-prefix-arg)
3700 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3701 (and name (not org-agenda-name)
3702 (setq-local org-agenda-name name)))
3703 (setq buffer-read-only nil))))
3705 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3706 (defun org-agenda-finalize ()
3707 "Finishing touch for the agenda buffer, called just before displaying it."
3708 (unless org-agenda-multi
3709 (save-excursion
3710 (let ((inhibit-read-only t))
3711 (goto-char (point-min))
3712 (save-excursion
3713 (while (org-activate-bracket-links (point-max))
3714 (add-text-properties (match-beginning 0) (match-end 0)
3715 '(face org-link))))
3716 (save-excursion
3717 (while (org-activate-plain-links (point-max))
3718 (add-text-properties (match-beginning 0) (match-end 0)
3719 '(face org-link))))
3720 (unless (eq org-agenda-remove-tags t)
3721 (org-agenda-align-tags))
3722 (unless org-agenda-with-colors
3723 (remove-text-properties (point-min) (point-max) '(face nil)))
3724 (if (and (boundp 'org-agenda-overriding-columns-format)
3725 org-agenda-overriding-columns-format)
3726 (setq-local org-agenda-overriding-columns-format
3727 org-agenda-overriding-columns-format))
3728 (if (and (boundp 'org-agenda-view-columns-initially)
3729 org-agenda-view-columns-initially)
3730 (org-agenda-columns))
3731 (when org-agenda-fontify-priorities
3732 (org-agenda-fontify-priorities))
3733 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3734 (org-agenda-dim-blocked-tasks))
3735 (org-agenda-mark-clocking-task)
3736 (when org-agenda-entry-text-mode
3737 (org-agenda-entry-text-hide)
3738 (org-agenda-entry-text-show))
3739 (if (and (functionp 'org-habit-insert-consistency-graphs)
3740 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3741 (org-habit-insert-consistency-graphs))
3742 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3743 (unless (or (eq org-agenda-show-inherited-tags 'always)
3744 (and (listp org-agenda-show-inherited-tags)
3745 (memq org-agenda-type org-agenda-show-inherited-tags))
3746 (and (eq org-agenda-show-inherited-tags t)
3747 (or (eq org-agenda-use-tag-inheritance t)
3748 (and (listp org-agenda-use-tag-inheritance)
3749 (not (memq org-agenda-type
3750 org-agenda-use-tag-inheritance))))))
3751 (let (mrk)
3752 (save-excursion
3753 (goto-char (point-min))
3754 (while (equal (forward-line) 0)
3755 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3756 (put-text-property (point-at-bol) (point-at-eol)
3757 'tags (org-with-point-at mrk
3758 (delete-dups
3759 (mapcar 'downcase (org-get-tags-at))))))))))
3760 (run-hooks 'org-agenda-finalize-hook)
3761 (when org-agenda-top-headline-filter
3762 (org-agenda-filter-top-headline-apply
3763 org-agenda-top-headline-filter))
3764 (when org-agenda-tag-filter
3765 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3766 (when (get 'org-agenda-tag-filter :preset-filter)
3767 (org-agenda-filter-apply
3768 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3769 (when org-agenda-category-filter
3770 (org-agenda-filter-apply org-agenda-category-filter 'category))
3771 (when (get 'org-agenda-category-filter :preset-filter)
3772 (org-agenda-filter-apply
3773 (get 'org-agenda-category-filter :preset-filter) 'category))
3774 (when org-agenda-regexp-filter
3775 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3776 (when (get 'org-agenda-regexp-filter :preset-filter)
3777 (org-agenda-filter-apply
3778 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3779 (when org-agenda-effort-filter
3780 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3781 (when (get 'org-agenda-effort-filter :preset-filter)
3782 (org-agenda-filter-apply
3783 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3784 (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3786 (defun org-agenda-mark-clocking-task ()
3787 "Mark the current clock entry in the agenda if it is present."
3788 ;; We need to widen when `org-agenda-finalize' is called from
3789 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3790 (when (bound-and-true-p org-clock-current-task)
3791 (save-restriction
3792 (widen)
3793 (org-agenda-unmark-clocking-task)
3794 (when (marker-buffer org-clock-hd-marker)
3795 (save-excursion
3796 (goto-char (point-min))
3797 (let (s ov)
3798 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3799 (goto-char s)
3800 (when (equal (org-get-at-bol 'org-hd-marker)
3801 org-clock-hd-marker)
3802 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3803 (overlay-put ov 'type 'org-agenda-clocking)
3804 (overlay-put ov 'face 'org-agenda-clocking)
3805 (overlay-put ov 'help-echo
3806 "The clock is running in this item")))))))))
3808 (defun org-agenda-unmark-clocking-task ()
3809 "Unmark the current clocking task."
3810 (mapc (lambda (o)
3811 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3812 (delete-overlay o)))
3813 (overlays-in (point-min) (point-max))))
3815 (defun org-agenda-fontify-priorities ()
3816 "Make highest priority lines bold, and lowest italic."
3817 (interactive)
3818 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3819 (delete-overlay o)))
3820 (overlays-in (point-min) (point-max)))
3821 (save-excursion
3822 (let (b e p ov h l)
3823 (goto-char (point-min))
3824 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3825 (setq h (or (get-char-property (point) 'org-highest-priority)
3826 org-highest-priority)
3827 l (or (get-char-property (point) 'org-lowest-priority)
3828 org-lowest-priority)
3829 p (string-to-char (match-string 1))
3830 b (match-beginning 0)
3831 e (if (eq org-agenda-fontify-priorities 'cookies)
3832 (match-end 0)
3833 (point-at-eol))
3834 ov (make-overlay b e))
3835 (overlay-put
3836 ov 'face
3837 (let ((special-face
3838 (cond ((org-face-from-face-or-color
3839 'priority nil
3840 (cdr (assoc p org-priority-faces))))
3841 ((and (listp org-agenda-fontify-priorities)
3842 (org-face-from-face-or-color
3843 'priority nil
3844 (cdr (assoc p org-agenda-fontify-priorities)))))
3845 ((equal p l) 'italic)
3846 ((equal p h) 'bold))))
3847 (if special-face (list special-face 'org-priority) 'org-priority)))
3848 (overlay-put ov 'org-type 'org-priority)))))
3850 (defvar org-depend-tag-blocked)
3852 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3853 "Dim currently blocked TODOs in the agenda display.
3854 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3855 dimming them."
3856 (interactive "P")
3857 (when (called-interactively-p 'interactive)
3858 (message "Dim or hide blocked tasks..."))
3859 (dolist (o (overlays-in (point-min) (point-max)))
3860 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3861 (delete-overlay o)))
3862 (save-excursion
3863 (let ((inhibit-read-only t)
3864 (org-depend-tag-blocked nil)
3865 org-blocked-by-checkboxes)
3866 (goto-char (point-min))
3867 (while (let ((pos (text-property-not-all
3868 (point) (point-max) 'todo-state nil)))
3869 (when pos (goto-char pos)))
3870 (setq org-blocked-by-checkboxes nil)
3871 (let ((marker (org-get-at-bol 'org-hd-marker)))
3872 (when (and (markerp marker)
3873 (with-current-buffer (marker-buffer marker)
3874 (save-excursion (goto-char marker)
3875 (org-entry-blocked-p))))
3876 ;; Entries blocked by checkboxes cannot be made invisible.
3877 ;; See `org-agenda-dim-blocked-tasks' for details.
3878 (let* ((really-invisible
3879 (and (not org-blocked-by-checkboxes)
3880 (or invisible (eq org-agenda-dim-blocked-tasks
3881 'invisible))))
3882 (ov (make-overlay (if really-invisible (line-end-position 0)
3883 (line-beginning-position))
3884 (line-end-position))))
3885 (if really-invisible (overlay-put ov 'invisible t)
3886 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3887 (overlay-put ov 'org-type 'org-blocked-todo))))
3888 (forward-line))))
3889 (when (called-interactively-p 'interactive)
3890 (message "Dim or hide blocked tasks...done")))
3892 (defvar org-agenda-skip-function nil
3893 "Function to be called at each match during agenda construction.
3894 If this function returns nil, the current match should not be skipped.
3895 Otherwise, the function must return a position from where the search
3896 should be continued.
3897 This may also be a Lisp form, it will be evaluated.
3898 Never set this variable using `setq' or so, because then it will apply
3899 to all future agenda commands. If you do want a global skipping condition,
3900 use the option `org-agenda-skip-function-global' instead.
3901 The correct usage for `org-agenda-skip-function' is to bind it with
3902 `let' to scope it dynamically into the agenda-constructing command.
3903 A good way to set it is through options in `org-agenda-custom-commands'.")
3905 (defun org-agenda-skip ()
3906 "Throw to `:skip' in places that should be skipped.
3907 Also moves point to the end of the skipped region, so that search can
3908 continue from there."
3909 (let ((p (point-at-bol)) to)
3910 (when (or
3911 (save-excursion (goto-char p) (looking-at comment-start-skip))
3912 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3913 (get-text-property p :org-archived)
3914 (org-end-of-subtree t))
3915 (and org-agenda-skip-comment-trees
3916 (get-text-property p :org-comment)
3917 (org-end-of-subtree t))
3918 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3919 (org-agenda-skip-eval org-agenda-skip-function)))
3920 (goto-char to))
3921 (org-in-src-block-p t))
3922 (throw :skip t))))
3924 (defun org-agenda-skip-eval (form)
3925 "If FORM is a function or a list, call (or eval) it and return the result.
3926 `save-excursion' and `save-match-data' are wrapped around the call, so point
3927 and match data are returned to the previous state no matter what these
3928 functions do."
3929 (let (fp)
3930 (and form
3931 (or (setq fp (functionp form))
3932 (consp form))
3933 (save-excursion
3934 (save-match-data
3935 (if fp
3936 (funcall form)
3937 (eval form)))))))
3939 (defvar org-agenda-markers nil
3940 "List of all currently active markers created by `org-agenda'.")
3941 (defvar org-agenda-last-marker-time (float-time)
3942 "Creation time of the last agenda marker.")
3944 (defun org-agenda-new-marker (&optional pos)
3945 "Return a new agenda marker.
3946 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3947 these markers and resets them when they are no longer in use."
3948 (let ((m (copy-marker (or pos (point)) t)))
3949 (setq org-agenda-last-marker-time (float-time))
3950 (if org-agenda-buffer
3951 (with-current-buffer org-agenda-buffer
3952 (push m org-agenda-markers))
3953 (push m org-agenda-markers))
3956 (defun org-agenda-reset-markers ()
3957 "Reset markers created by `org-agenda'."
3958 (while org-agenda-markers
3959 (move-marker (pop org-agenda-markers) nil)))
3961 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3962 "Save relative positions of markers in region.
3963 This check for agenda markers in all agenda buffers currently active."
3964 (dolist (buf (buffer-list))
3965 (with-current-buffer buf
3966 (when (eq major-mode 'org-agenda-mode)
3967 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3968 org-agenda-markers)))))
3970 ;;; Entry text mode
3972 (defun org-agenda-entry-text-show-here ()
3973 "Add some text from the entry as context to the current line."
3974 (let (m txt o)
3975 (setq m (org-get-at-bol 'org-hd-marker))
3976 (unless (marker-buffer m)
3977 (error "No marker points to an entry here"))
3978 (setq txt (concat "\n" (org-no-properties
3979 (org-agenda-get-some-entry-text
3980 m org-agenda-entry-text-maxlines
3981 org-agenda-entry-text-leaders))))
3982 (when (string-match "\\S-" txt)
3983 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3984 (overlay-put o 'evaporate t)
3985 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3986 (overlay-put o 'after-string txt))))
3988 (defun org-agenda-entry-text-show ()
3989 "Add entry context for all agenda lines."
3990 (interactive)
3991 (save-excursion
3992 (goto-char (point-max))
3993 (beginning-of-line 1)
3994 (while (not (bobp))
3995 (when (org-get-at-bol 'org-hd-marker)
3996 (org-agenda-entry-text-show-here))
3997 (beginning-of-line 0))))
3999 (defun org-agenda-entry-text-hide ()
4000 "Remove any shown entry context."
4001 (delq nil
4002 (mapcar (lambda (o)
4003 (if (eq (overlay-get o 'org-overlay-type)
4004 'agenda-entry-content)
4005 (progn (delete-overlay o) t)))
4006 (overlays-in (point-min) (point-max)))))
4008 (defun org-agenda-get-day-face (date)
4009 "Return the face DATE should be displayed with."
4010 (cond ((and (functionp org-agenda-day-face-function)
4011 (funcall org-agenda-day-face-function date)))
4012 ((org-agenda-today-p date) 'org-agenda-date-today)
4013 ((memq (calendar-day-of-week date) org-agenda-weekend-days)
4014 'org-agenda-date-weekend)
4015 (t 'org-agenda-date)))
4017 ;;; Agenda timeline
4019 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4020 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4022 (defun org-timeline (&optional dotodo)
4023 "Show a time-sorted view of the entries in the current org file.
4024 Only entries with a time stamp of today or later will be listed. With
4025 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4026 under the current date.
4027 If the buffer contains an active region, only check the region for
4028 dates."
4029 (interactive "P")
4030 (let* ((dopast t)
4031 (org-agenda-show-log-scoped org-agenda-show-log)
4032 (org-agenda-show-log org-agenda-show-log-scoped)
4033 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4034 (current-buffer))))
4035 (date (calendar-current-date))
4036 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4037 (end (if (org-region-active-p) (region-end) (point-max)))
4038 (day-numbers (org-get-all-dates
4039 beg end 'no-ranges
4040 t org-agenda-show-log-scoped ; always include today
4041 org-timeline-show-empty-dates))
4042 (org-deadline-warning-days 0)
4043 (org-agenda-only-exact-dates t)
4044 (today (org-today))
4045 (past t)
4046 args
4047 s e rtn d emptyp)
4048 (setq org-agenda-redo-command
4049 (list 'let
4050 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4051 (list 'org-switch-to-buffer-other-window (current-buffer))
4052 (list 'org-timeline (list 'quote dotodo))))
4053 (put 'org-agenda-redo-command 'org-lprops nil)
4054 (if (not dopast)
4055 ;; Remove past dates from the list of dates.
4056 (setq day-numbers (delq nil (mapcar (lambda(x)
4057 (if (>= x today) x nil))
4058 day-numbers))))
4059 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4060 (org-compile-prefix-format 'timeline)
4061 (org-set-sorting-strategy 'timeline)
4062 (if org-agenda-show-log-scoped (push :closed args))
4063 (push :timestamp args)
4064 (push :deadline args)
4065 (push :scheduled args)
4066 (push :sexp args)
4067 (if dotodo (push :todo args))
4068 (insert "Timeline of file " entry "\n")
4069 (add-text-properties (point-min) (point)
4070 (list 'face 'org-agenda-structure))
4071 (org-agenda-mark-header-line (point-min))
4072 (while (setq d (pop day-numbers))
4073 (if (and (listp d) (eq (car d) :omitted))
4074 (progn
4075 (setq s (point))
4076 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4077 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4078 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4079 (if (and (>= d today)
4080 dopast
4081 past)
4082 (progn
4083 (setq past nil)
4084 (insert (make-string 79 ?-) "\n")))
4085 (setq date (calendar-gregorian-from-absolute d))
4086 (setq s (point))
4087 (setq rtn (and (not emptyp)
4088 (apply 'org-agenda-get-day-entries entry
4089 date args)))
4090 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4091 (progn
4092 (insert
4093 (if (stringp org-agenda-format-date)
4094 (format-time-string org-agenda-format-date
4095 (org-time-from-absolute date))
4096 (funcall org-agenda-format-date date))
4097 "\n")
4098 (put-text-property s (1- (point)) 'face
4099 (org-agenda-get-day-face date))
4100 (put-text-property s (1- (point)) 'org-date-line t)
4101 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4102 (if (equal d today)
4103 (put-text-property s (1- (point)) 'org-today t))
4104 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4105 (put-text-property s (1- (point)) 'day d)))))
4106 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4107 (point-min)))
4108 (add-text-properties
4109 (point-min) (point-max)
4110 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4111 (org-agenda-finalize)
4112 (setq buffer-read-only t)))
4114 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4115 "Return a list of all relevant day numbers from BEG to END buffer positions.
4116 If NO-RANGES is non-nil, include only the start and end dates of a range,
4117 not every single day in the range. If FORCE-TODAY is non-nil, make
4118 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4119 inactive time stamps (those in square brackets) are included.
4120 When EMPTY is non-nil, also include days without any entries."
4121 (let ((re (concat
4122 (if pre-re pre-re "")
4123 (if inactive org-ts-regexp-both org-ts-regexp)))
4124 dates dates1 date day day1 day2 ts1 ts2 pos)
4125 (if force-today
4126 (setq dates (list (org-today))))
4127 (save-excursion
4128 (goto-char beg)
4129 (while (re-search-forward re end t)
4130 (setq day (time-to-days (org-time-string-to-time
4131 (substring (match-string 1) 0 10)
4132 (current-buffer) (match-beginning 0))))
4133 (or (memq day dates) (push day dates)))
4134 (unless no-ranges
4135 (goto-char beg)
4136 (while (re-search-forward org-tr-regexp end t)
4137 (setq pos (match-beginning 0))
4138 (setq ts1 (substring (match-string 1) 0 10)
4139 ts2 (substring (match-string 2) 0 10)
4140 day1 (time-to-days (org-time-string-to-time
4141 ts1 (current-buffer) pos))
4142 day2 (time-to-days (org-time-string-to-time
4143 ts2 (current-buffer) pos)))
4144 (while (< (setq day1 (1+ day1)) day2)
4145 (or (memq day1 dates) (push day1 dates)))))
4146 (setq dates (sort dates '<))
4147 (when empty
4148 (while (setq day (pop dates))
4149 (setq day2 (car dates))
4150 (push day dates1)
4151 (when (and day2 empty)
4152 (if (or (eq empty t)
4153 (and (numberp empty) (<= (- day2 day) empty)))
4154 (while (< (setq day (1+ day)) day2)
4155 (push (list day) dates1))
4156 (push (cons :omitted (- day2 day)) dates1))))
4157 (setq dates (nreverse dates1)))
4158 dates)))
4160 ;;; Agenda Daily/Weekly
4162 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4163 "Start day for the agenda view.
4164 Custom commands can set this variable in the options section.
4165 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4166 input allowed when reading a date through the Org calendar.
4167 See the docstring of `org-read-date' for details.")
4168 (defvar org-starting-day nil) ; local variable in the agenda buffer
4169 (defvar org-arg-loc nil) ; local variable
4171 (defvar org-agenda-buffer-tmp-name nil)
4172 ;;;###autoload
4173 (defun org-agenda-list (&optional arg start-day span with-hour)
4174 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4175 The view will be for the current day or week, but from the overview buffer
4176 you will be able to go to other days/weeks.
4178 With a numeric prefix argument in an interactive call, the agenda will
4179 span ARG days. Lisp programs should instead specify SPAN to change
4180 the number of days. SPAN defaults to `org-agenda-span'.
4182 START-DAY defaults to TODAY, or to the most recent match for the weekday
4183 given in `org-agenda-start-on-weekday'.
4185 When WITH-HOUR is non-nil, only include scheduled and deadline
4186 items if they have an hour specification like [h]h:mm."
4187 (interactive "P")
4188 (if org-agenda-overriding-arguments
4189 (setq arg (car org-agenda-overriding-arguments)
4190 start-day (nth 1 org-agenda-overriding-arguments)
4191 span (nth 2 org-agenda-overriding-arguments)))
4192 (if (and (integerp arg) (> arg 0))
4193 (setq span arg arg nil))
4194 (catch 'exit
4195 (setq org-agenda-buffer-name
4196 (or org-agenda-buffer-tmp-name
4197 (if org-agenda-sticky
4198 (cond ((and org-keys (stringp org-match))
4199 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4200 (org-keys
4201 (format "*Org Agenda(%s)*" org-keys))
4202 (t "*Org Agenda(a)*")))
4203 org-agenda-buffer-name))
4204 (org-agenda-prepare "Day/Week")
4205 (setq start-day (or start-day org-agenda-start-day))
4206 (if (stringp start-day)
4207 ;; Convert to an absolute day number
4208 (setq start-day (time-to-days (org-read-date nil t start-day))))
4209 (org-compile-prefix-format 'agenda)
4210 (org-set-sorting-strategy 'agenda)
4211 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
4212 (today (org-today))
4213 (sd (or start-day today))
4214 (ndays (org-agenda-span-to-ndays span sd))
4215 (org-agenda-start-on-weekday
4216 (if (or (eq ndays 7) (eq ndays 14))
4217 org-agenda-start-on-weekday))
4218 (thefiles (org-agenda-files nil 'ifmode))
4219 (files thefiles)
4220 (start (if (or (null org-agenda-start-on-weekday)
4221 (< ndays 7))
4223 (let* ((nt (calendar-day-of-week
4224 (calendar-gregorian-from-absolute sd)))
4225 (n1 org-agenda-start-on-weekday)
4226 (d (- nt n1)))
4227 (- sd (+ (if (< d 0) 7 0) d)))))
4228 (day-numbers (list start))
4229 (day-cnt 0)
4230 (inhibit-redisplay (not debug-on-error))
4231 (org-agenda-show-log-scoped org-agenda-show-log)
4232 s e rtn rtnall file date d start-pos end-pos todayp
4233 clocktable-start clocktable-end filter)
4234 (setq org-agenda-redo-command
4235 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4236 (dotimes (n (1- ndays))
4237 (push (1+ (car day-numbers)) day-numbers))
4238 (setq day-numbers (nreverse day-numbers))
4239 (setq clocktable-start (car day-numbers)
4240 clocktable-end (1+ (or (org-last day-numbers) 0)))
4241 (setq-local org-starting-day (car day-numbers))
4242 (setq-local org-arg-loc arg)
4243 (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
4244 (unless org-agenda-compact-blocks
4245 (let* ((d1 (car day-numbers))
4246 (d2 (org-last day-numbers))
4247 (w1 (org-days-to-iso-week d1))
4248 (w2 (org-days-to-iso-week d2)))
4249 (setq s (point))
4250 (if org-agenda-overriding-header
4251 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4252 nil 'face 'org-agenda-structure) "\n")
4253 (insert (org-agenda-span-name span)
4254 "-agenda"
4255 (if (< (- d2 d1) 350)
4256 (if (= w1 w2)
4257 (format " (W%02d)" w1)
4258 (format " (W%02d-W%02d)" w1 w2))
4260 ":\n")))
4261 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4262 'org-date-line t))
4263 (org-agenda-mark-header-line s))
4264 (while (setq d (pop day-numbers))
4265 (setq date (calendar-gregorian-from-absolute d)
4266 s (point))
4267 (if (or (setq todayp (= d today))
4268 (and (not start-pos) (= d sd)))
4269 (setq start-pos (point))
4270 (if (and start-pos (not end-pos))
4271 (setq end-pos (point))))
4272 (setq files thefiles
4273 rtnall nil)
4274 (while (setq file (pop files))
4275 (catch 'nextfile
4276 (org-check-agenda-file file)
4277 (let ((org-agenda-entry-types org-agenda-entry-types))
4278 ;; Starred types override non-starred equivalents
4279 (when (member :deadline* org-agenda-entry-types)
4280 (setq org-agenda-entry-types
4281 (delq :deadline org-agenda-entry-types)))
4282 (when (member :scheduled* org-agenda-entry-types)
4283 (setq org-agenda-entry-types
4284 (delq :scheduled org-agenda-entry-types)))
4285 ;; Honor with-hour
4286 (when with-hour
4287 (when (member :deadline org-agenda-entry-types)
4288 (setq org-agenda-entry-types
4289 (delq :deadline org-agenda-entry-types))
4290 (push :deadline* org-agenda-entry-types))
4291 (when (member :scheduled org-agenda-entry-types)
4292 (setq org-agenda-entry-types
4293 (delq :scheduled org-agenda-entry-types))
4294 (push :scheduled* org-agenda-entry-types)))
4295 (unless org-agenda-include-deadlines
4296 (setq org-agenda-entry-types
4297 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4298 (cond
4299 ((memq org-agenda-show-log-scoped '(only clockcheck))
4300 (setq rtn (org-agenda-get-day-entries
4301 file date :closed)))
4302 (org-agenda-show-log-scoped
4303 (setq rtn (apply 'org-agenda-get-day-entries
4304 file date
4305 (append '(:closed) org-agenda-entry-types))))
4307 (setq rtn (apply 'org-agenda-get-day-entries
4308 file date
4309 org-agenda-entry-types)))))
4310 (setq rtnall (append rtnall rtn)))) ;; all entries
4311 (if org-agenda-include-diary
4312 (let ((org-agenda-search-headline-for-time t))
4313 (require 'diary-lib)
4314 (setq rtn (org-get-entries-from-diary date))
4315 (setq rtnall (append rtnall rtn))))
4316 (if (or rtnall org-agenda-show-all-dates)
4317 (progn
4318 (setq day-cnt (1+ day-cnt))
4319 (insert
4320 (if (stringp org-agenda-format-date)
4321 (format-time-string org-agenda-format-date
4322 (org-time-from-absolute date))
4323 (funcall org-agenda-format-date date))
4324 "\n")
4325 (put-text-property s (1- (point)) 'face
4326 (org-agenda-get-day-face date))
4327 (put-text-property s (1- (point)) 'org-date-line t)
4328 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4329 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4330 (when todayp
4331 (put-text-property s (1- (point)) 'org-today t))
4332 (setq rtnall
4333 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4334 (if rtnall (insert ;; all entries
4335 (org-agenda-finalize-entries rtnall 'agenda)
4336 "\n"))
4337 (put-text-property s (1- (point)) 'day d)
4338 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4339 (when (and org-agenda-clockreport-mode clocktable-start)
4340 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4341 ;; the above line is to ensure the restricted range!
4342 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4343 tbl)
4344 (setq p (org-plist-delete p :block))
4345 (setq p (plist-put p :tstart clocktable-start))
4346 (setq p (plist-put p :tend clocktable-end))
4347 (setq p (plist-put p :scope 'agenda))
4348 (setq tbl (apply 'org-clock-get-clocktable p))
4349 (insert tbl)))
4350 (goto-char (point-min))
4351 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4352 (unless (and (pos-visible-in-window-p (point-min))
4353 (pos-visible-in-window-p (point-max)))
4354 (goto-char (1- (point-max)))
4355 (recenter -1)
4356 (if (not (pos-visible-in-window-p (or start-pos 1)))
4357 (progn
4358 (goto-char (or start-pos 1))
4359 (recenter 1))))
4360 (goto-char (or start-pos 1))
4361 (add-text-properties (point-min) (point-max)
4362 `(org-agenda-type agenda
4363 org-last-args (,arg ,start-day ,span)
4364 org-redo-cmd ,org-agenda-redo-command
4365 org-series-cmd ,org-cmd))
4366 (if (eq org-agenda-show-log-scoped 'clockcheck)
4367 (org-agenda-show-clocking-issues))
4368 (org-agenda-finalize)
4369 (setq buffer-read-only t)
4370 (message ""))))
4372 (defun org-agenda-ndays-to-span (n)
4373 "Return a span symbol for a span of N days, or N if none matches."
4374 (cond ((symbolp n) n)
4375 ((= n 1) 'day)
4376 ((= n 7) 'week)
4377 ((= n 14) 'fortnight)
4378 (t n)))
4380 (defun org-agenda-span-to-ndays (span &optional start-day)
4381 "Return ndays from SPAN, possibly starting at START-DAY.
4382 START-DAY is an absolute time value."
4383 (cond ((numberp span) span)
4384 ((eq span 'day) 1)
4385 ((eq span 'week) 7)
4386 ((eq span 'fortnight) 14)
4387 ((eq span 'month)
4388 (let ((date (calendar-gregorian-from-absolute start-day)))
4389 (calendar-last-day-of-month (car date) (caddr date))))
4390 ((eq span 'year)
4391 (let ((date (calendar-gregorian-from-absolute start-day)))
4392 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4394 (defun org-agenda-span-name (span)
4395 "Return a SPAN name."
4396 (if (null span)
4398 (if (symbolp span)
4399 (capitalize (symbol-name span))
4400 (format "%d days" span))))
4402 ;;; Agenda word search
4404 (defvar org-agenda-search-history nil)
4406 (defvar org-search-syntax-table nil
4407 "Special syntax table for org-mode search.
4408 In this table, we have single quotes not as word constituents, to
4409 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4411 (defvar org-mode-syntax-table) ; From org.el
4412 (defun org-search-syntax-table ()
4413 (unless org-search-syntax-table
4414 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4415 (modify-syntax-entry ?' "." org-search-syntax-table)
4416 (modify-syntax-entry ?` "." org-search-syntax-table))
4417 org-search-syntax-table)
4419 (defvar org-agenda-last-search-view-search-was-boolean nil)
4421 ;;;###autoload
4422 (defun org-search-view (&optional todo-only string edit-at)
4423 "Show all entries that contain a phrase or words or regular expressions.
4425 With optional prefix argument TODO-ONLY, only consider entries that are
4426 TODO entries. The argument STRING can be used to pass a default search
4427 string into this function. If EDIT-AT is non-nil, it means that the
4428 user should get a chance to edit this string, with cursor at position
4429 EDIT-AT.
4431 The search string can be viewed either as a phrase that should be found as
4432 is, or it can be broken into a number of snippets, each of which must match
4433 in a Boolean way to select an entry. The default depends on the variable
4434 `org-agenda-search-view-always-boolean'.
4435 Even if this is turned off (the default) you can always switch to
4436 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4438 The default is a direct search of the whole phrase, where each space in
4439 the search string can expand to an arbitrary amount of whitespace,
4440 including newlines.
4442 If using a Boolean search, the search string is split on whitespace and
4443 each snippet is searched separately, with logical AND to select an entry.
4444 Words prefixed with a minus must *not* occur in the entry. Words without
4445 a prefix or prefixed with a plus must occur in the entry. Matching is
4446 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4447 match whole words, not parts of a word) if
4448 `org-agenda-search-view-force-full-words' is set (default is nil).
4450 Boolean search snippets enclosed by curly braces are interpreted as
4451 regular expressions that must or (when preceded with \"-\") must not
4452 match in the entry. Snippets enclosed into double quotes will be taken
4453 as a whole, to include whitespace.
4455 - If the search string starts with an asterisk, search only in headlines.
4456 - If (possibly after the leading star) the search string starts with an
4457 exclamation mark, this also means to look at TODO entries only, an effect
4458 that can also be achieved with a prefix argument.
4459 - If (possibly after star and exclamation mark) the search string starts
4460 with a colon, this will mean that the (non-regexp) snippets of the
4461 Boolean search must match as full words.
4463 This command searches the agenda files, and in addition the files listed
4464 in `org-agenda-text-search-extra-files'."
4465 (interactive "P")
4466 (if org-agenda-overriding-arguments
4467 (setq todo-only (car org-agenda-overriding-arguments)
4468 string (nth 1 org-agenda-overriding-arguments)
4469 edit-at (nth 2 org-agenda-overriding-arguments)))
4470 (let* ((props (list 'face nil
4471 'done-face 'org-agenda-done
4472 'org-not-done-regexp org-not-done-regexp
4473 'org-todo-regexp org-todo-regexp
4474 'org-complex-heading-regexp org-complex-heading-regexp
4475 'mouse-face 'highlight
4476 'help-echo (format "mouse-2 or RET jump to location")))
4477 (full-words org-agenda-search-view-force-full-words)
4478 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4479 regexp rtn rtnall files file pos inherited-tags
4480 marker category level tags c neg re boolean
4481 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4482 (unless (and (not edit-at)
4483 (stringp string)
4484 (string-match "\\S-" string))
4485 (setq string (read-string
4486 (if org-agenda-search-view-always-boolean
4487 "[+-]Word/{Regexp} ...: "
4488 "Phrase or [+-]Word/{Regexp} ...: ")
4489 (cond
4490 ((integerp edit-at) (cons string edit-at))
4491 (edit-at string))
4492 'org-agenda-search-history)))
4493 (catch 'exit
4494 (if org-agenda-sticky
4495 (setq org-agenda-buffer-name
4496 (if (stringp string)
4497 (format "*Org Agenda(%s:%s)*"
4498 (or org-keys (or (and todo-only "S") "s")) string)
4499 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4500 (org-agenda-prepare "SEARCH")
4501 (org-compile-prefix-format 'search)
4502 (org-set-sorting-strategy 'search)
4503 (setq org-agenda-redo-command
4504 (list 'org-search-view (if todo-only t nil)
4505 (list 'if 'current-prefix-arg nil string)))
4506 (setq org-agenda-query-string string)
4507 (if (equal (string-to-char string) ?*)
4508 (setq hdl-only t
4509 words (substring string 1))
4510 (setq words string))
4511 (when (equal (string-to-char words) ?!)
4512 (setq todo-only t
4513 words (substring words 1)))
4514 (when (equal (string-to-char words) ?:)
4515 (setq full-words t
4516 words (substring words 1)))
4517 (if (or org-agenda-search-view-always-boolean
4518 (member (string-to-char words) '(?- ?+ ?\{)))
4519 (setq boolean t))
4520 (setq words (org-split-string words))
4521 (let (www w)
4522 (while (setq w (pop words))
4523 (while (and (string-match "\\\\\\'" w) words)
4524 (setq w (concat (substring w 0 -1) " " (pop words))))
4525 (push w www))
4526 (setq words (nreverse www) www nil)
4527 (while (setq w (pop words))
4528 (when (and (string-match "\\`[-+]?{" w)
4529 (not (string-match "}\\'" w)))
4530 (while (and words (not (string-match "}\\'" (car words))))
4531 (setq w (concat w " " (pop words))))
4532 (setq w (concat w " " (pop words))))
4533 (push w www))
4534 (setq words (nreverse www)))
4535 (setq org-agenda-last-search-view-search-was-boolean boolean)
4536 (when boolean
4537 (let (wds w)
4538 (while (setq w (pop words))
4539 (if (or (equal (substring w 0 1) "\"")
4540 (and (> (length w) 1)
4541 (member (substring w 0 1) '("+" "-"))
4542 (equal (substring w 1 2) "\"")))
4543 (while (and words (not (equal (substring w -1) "\"")))
4544 (setq w (concat w " " (pop words)))))
4545 (and (string-match "\\`\\([-+]?\\)\"" w)
4546 (setq w (replace-match "\\1" nil nil w)))
4547 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4548 (push w wds))
4549 (setq words (nreverse wds))))
4550 (if boolean
4551 (mapc (lambda (w)
4552 (setq c (string-to-char w))
4553 (if (equal c ?-)
4554 (setq neg t w (substring w 1))
4555 (if (equal c ?+)
4556 (setq neg nil w (substring w 1))
4557 (setq neg nil)))
4558 (if (string-match "\\`{.*}\\'" w)
4559 (setq re (substring w 1 -1))
4560 (if full-words
4561 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4562 (setq re (regexp-quote (downcase w)))))
4563 (if neg (push re regexps-) (push re regexps+)))
4564 words)
4565 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4566 regexps+))
4567 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4568 (if (not regexps+)
4569 (setq regexp org-outline-regexp-bol)
4570 (setq regexp (pop regexps+))
4571 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4572 regexp))))
4573 (setq files (org-agenda-files nil 'ifmode))
4574 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4575 (pop org-agenda-text-search-extra-files)
4576 (setq files (org-add-archive-files files)))
4577 (setq files (append files org-agenda-text-search-extra-files)
4578 rtnall nil)
4579 (while (setq file (pop files))
4580 (setq ee nil)
4581 (catch 'nextfile
4582 (org-check-agenda-file file)
4583 (setq buffer (if (file-exists-p file)
4584 (org-get-agenda-file-buffer file)
4585 (error "No such file %s" file)))
4586 (if (not buffer)
4587 ;; If file does not exist, make sure an error message is sent
4588 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4589 file))))
4590 (with-current-buffer buffer
4591 (with-syntax-table (org-search-syntax-table)
4592 (unless (derived-mode-p 'org-mode)
4593 (error "Agenda file %s is not in `org-mode'" file))
4594 (let ((case-fold-search t))
4595 (save-excursion
4596 (save-restriction
4597 (if (eq buffer org-agenda-restrict)
4598 (narrow-to-region org-agenda-restrict-begin
4599 org-agenda-restrict-end)
4600 (widen))
4601 (goto-char (point-min))
4602 (unless (or (org-at-heading-p)
4603 (outline-next-heading))
4604 (throw 'nextfile t))
4605 (goto-char (max (point-min) (1- (point))))
4606 (while (re-search-forward regexp nil t)
4607 (org-back-to-heading t)
4608 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4609 (> (org-reduced-level (org-outline-level))
4610 org-agenda-search-view-max-outline-level)
4611 (forward-line -1)
4612 (org-back-to-heading t)))
4613 (skip-chars-forward "* ")
4614 (setq beg (point-at-bol)
4615 beg1 (point)
4616 end (progn
4617 (outline-next-heading)
4618 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4619 (> (org-reduced-level (org-outline-level))
4620 org-agenda-search-view-max-outline-level)
4621 (forward-line 1)
4622 (outline-next-heading)))
4623 (point)))
4625 (catch :skip
4626 (goto-char beg)
4627 (org-agenda-skip)
4628 (setq str (buffer-substring-no-properties
4629 (point-at-bol)
4630 (if hdl-only (point-at-eol) end)))
4631 (mapc (lambda (wr) (when (string-match wr str)
4632 (goto-char (1- end))
4633 (throw :skip t)))
4634 regexps-)
4635 (mapc (lambda (wr) (unless (string-match wr str)
4636 (goto-char (1- end))
4637 (throw :skip t)))
4638 (if todo-only
4639 (cons (concat "^\\*+[ \t]+"
4640 org-not-done-regexp)
4641 regexps+)
4642 regexps+))
4643 (goto-char beg)
4644 (setq marker (org-agenda-new-marker (point))
4645 category (org-get-category)
4646 level (make-string (org-reduced-level (org-outline-level)) ? )
4647 inherited-tags
4648 (or (eq org-agenda-show-inherited-tags 'always)
4649 (and (listp org-agenda-show-inherited-tags)
4650 (memq 'todo org-agenda-show-inherited-tags))
4651 (and (eq org-agenda-show-inherited-tags t)
4652 (or (eq org-agenda-use-tag-inheritance t)
4653 (memq 'todo org-agenda-use-tag-inheritance))))
4654 tags (org-get-tags-at nil (not inherited-tags))
4655 txt (org-agenda-format-item
4657 (buffer-substring-no-properties
4658 beg1 (point-at-eol))
4659 level category tags t))
4660 (org-add-props txt props
4661 'org-marker marker 'org-hd-marker marker
4662 'org-todo-regexp org-todo-regexp
4663 'level level
4664 'org-complex-heading-regexp org-complex-heading-regexp
4665 'priority 1000
4666 'type "search")
4667 (push txt ee)
4668 (goto-char (1- end))))))))))
4669 (setq rtn (nreverse ee))
4670 (setq rtnall (append rtnall rtn)))
4671 (if org-agenda-overriding-header
4672 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4673 nil 'face 'org-agenda-structure) "\n")
4674 (insert "Search words: ")
4675 (add-text-properties (point-min) (1- (point))
4676 (list 'face 'org-agenda-structure))
4677 (setq pos (point))
4678 (insert string "\n")
4679 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4680 (setq pos (point))
4681 (unless org-agenda-multi
4682 (insert (substitute-command-keys "\
4683 Press `\\[org-agenda-manipulate-query-add]', \
4684 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4685 `\\[org-agenda-manipulate-query-add-re]', \
4686 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4687 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4688 (add-text-properties pos (1- (point))
4689 (list 'face 'org-agenda-structure))))
4690 (org-agenda-mark-header-line (point-min))
4691 (when rtnall
4692 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4693 (goto-char (point-min))
4694 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4695 (add-text-properties (point-min) (point-max)
4696 `(org-agenda-type search
4697 org-last-args (,todo-only ,string ,edit-at)
4698 org-redo-cmd ,org-agenda-redo-command
4699 org-series-cmd ,org-cmd))
4700 (org-agenda-finalize)
4701 (setq buffer-read-only t))))
4703 ;;; Agenda TODO list
4705 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4706 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4707 (concat
4708 (if (or (equal keywords "ALL") (not keywords))
4709 (propertize "ALL" 'face 'warning)
4710 (mapconcat
4711 (lambda (kw)
4712 (propertize kw 'face (org-get-todo-face kw)))
4713 (org-split-string keywords "|")
4714 "|"))
4715 "\n"))
4717 (defvar org-select-this-todo-keyword nil)
4718 (defvar org-last-arg nil)
4720 ;;;###autoload
4721 (defun org-todo-list (&optional arg)
4722 "Show all (not done) TODO entries from all agenda file in a single list.
4723 The prefix arg can be used to select a specific TODO keyword and limit
4724 the list to these. When using \\[universal-argument], you will be prompted
4725 for a keyword. A numeric prefix directly selects the Nth keyword in
4726 `org-todo-keywords-1'."
4727 (interactive "P")
4728 (if org-agenda-overriding-arguments
4729 (setq arg org-agenda-overriding-arguments))
4730 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4731 (let* ((today (org-today))
4732 (date (calendar-gregorian-from-absolute today))
4733 (kwds org-todo-keywords-for-agenda)
4734 (completion-ignore-case t)
4735 (org-select-this-todo-keyword
4736 (if (stringp arg) arg
4737 (and arg (integerp arg) (> arg 0)
4738 (nth (1- arg) kwds))))
4739 rtn rtnall files file pos)
4740 (when (equal arg '(4))
4741 (setq org-select-this-todo-keyword
4742 (completing-read "Keyword (or KWD1|K2D2|...): "
4743 (mapcar #'list kwds) nil nil)))
4744 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4745 (catch 'exit
4746 (if org-agenda-sticky
4747 (setq org-agenda-buffer-name
4748 (if (stringp org-select-this-todo-keyword)
4749 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4750 org-select-this-todo-keyword)
4751 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4752 (org-agenda-prepare "TODO")
4753 (org-compile-prefix-format 'todo)
4754 (org-set-sorting-strategy 'todo)
4755 (setq org-agenda-redo-command
4756 `(org-todo-list (or (and (numberp current-prefix-arg)
4757 current-prefix-arg)
4758 ,org-select-this-todo-keyword
4759 current-prefix-arg ,arg)))
4760 (setq files (org-agenda-files nil 'ifmode)
4761 rtnall nil)
4762 (while (setq file (pop files))
4763 (catch 'nextfile
4764 (org-check-agenda-file file)
4765 (setq rtn (org-agenda-get-day-entries file date :todo))
4766 (setq rtnall (append rtnall rtn))))
4767 (if org-agenda-overriding-header
4768 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4769 nil 'face 'org-agenda-structure) "\n")
4770 (insert "Global list of TODO items of type: ")
4771 (add-text-properties (point-min) (1- (point))
4772 (list 'face 'org-agenda-structure
4773 'short-heading
4774 (concat "ToDo: "
4775 (or org-select-this-todo-keyword "ALL"))))
4776 (org-agenda-mark-header-line (point-min))
4777 (insert (org-agenda-propertize-selected-todo-keywords
4778 org-select-this-todo-keyword))
4779 (setq pos (point))
4780 (unless org-agenda-multi
4781 (insert (substitute-command-keys "Available with \
4782 `N \\[org-agenda-redo]': (0)[ALL]"))
4783 (let ((n 0) s)
4784 (mapc (lambda (x)
4785 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4786 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4787 (insert "\n "))
4788 (insert " " s))
4789 kwds))
4790 (insert "\n"))
4791 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4792 (org-agenda-mark-header-line (point-min))
4793 (when rtnall
4794 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4795 (goto-char (point-min))
4796 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4797 (add-text-properties (point-min) (point-max)
4798 `(org-agenda-type todo
4799 org-last-args ,arg
4800 org-redo-cmd ,org-agenda-redo-command
4801 org-series-cmd ,org-cmd))
4802 (org-agenda-finalize)
4803 (setq buffer-read-only t))))
4805 ;;; Agenda tags match
4807 ;;;###autoload
4808 (defun org-tags-view (&optional todo-only match)
4809 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4810 The prefix arg TODO-ONLY limits the search to TODO entries."
4811 (interactive "P")
4812 (if org-agenda-overriding-arguments
4813 (setq todo-only (car org-agenda-overriding-arguments)
4814 match (nth 1 org-agenda-overriding-arguments)))
4815 (let* ((org-tags-match-list-sublevels
4816 org-tags-match-list-sublevels)
4817 (completion-ignore-case t)
4818 rtn rtnall files file pos matcher
4819 buffer)
4820 (when (and (stringp match) (not (string-match "\\S-" match)))
4821 (setq match nil))
4822 (catch 'exit
4823 (if org-agenda-sticky
4824 (setq org-agenda-buffer-name
4825 (if (stringp match)
4826 (format "*Org Agenda(%s:%s)*"
4827 (or org-keys (or (and todo-only "M") "m")) match)
4828 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4829 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4830 ;; expanding tags within `org-make-tags-matcher'
4831 (org-agenda-prepare (concat "TAGS " match))
4832 (setq org--matcher-tags-todo-only todo-only
4833 matcher (org-make-tags-matcher match)
4834 match (car matcher)
4835 matcher (cdr matcher))
4836 (org-compile-prefix-format 'tags)
4837 (org-set-sorting-strategy 'tags)
4838 (setq org-agenda-query-string match)
4839 (setq org-agenda-redo-command
4840 (list 'org-tags-view
4841 `(quote ,org--matcher-tags-todo-only)
4842 `(if current-prefix-arg nil ,org-agenda-query-string)))
4843 (setq files (org-agenda-files nil 'ifmode)
4844 rtnall nil)
4845 (while (setq file (pop files))
4846 (catch 'nextfile
4847 (org-check-agenda-file file)
4848 (setq buffer (if (file-exists-p file)
4849 (org-get-agenda-file-buffer file)
4850 (error "No such file %s" file)))
4851 (if (not buffer)
4852 ;; If file does not exist, error message to agenda
4853 (setq rtn (list
4854 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4855 rtnall (append rtnall rtn))
4856 (with-current-buffer buffer
4857 (unless (derived-mode-p 'org-mode)
4858 (error "Agenda file %s is not in `org-mode'" file))
4859 (save-excursion
4860 (save-restriction
4861 (if (eq buffer org-agenda-restrict)
4862 (narrow-to-region org-agenda-restrict-begin
4863 org-agenda-restrict-end)
4864 (widen))
4865 (setq rtn (org-scan-tags 'agenda
4866 matcher
4867 org--matcher-tags-todo-only))
4868 (setq rtnall (append rtnall rtn))))))))
4869 (if org-agenda-overriding-header
4870 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4871 nil 'face 'org-agenda-structure) "\n")
4872 (insert "Headlines with TAGS match: ")
4873 (add-text-properties (point-min) (1- (point))
4874 (list 'face 'org-agenda-structure
4875 'short-heading
4876 (concat "Match: " match)))
4877 (setq pos (point))
4878 (insert match "\n")
4879 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4880 (setq pos (point))
4881 (unless org-agenda-multi
4882 (insert (substitute-command-keys
4883 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4884 to search again with new search string\n")))
4885 (add-text-properties pos (1- (point))
4886 (list 'face 'org-agenda-structure)))
4887 (org-agenda-mark-header-line (point-min))
4888 (when rtnall
4889 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4890 (goto-char (point-min))
4891 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4892 (add-text-properties
4893 (point-min) (point-max)
4894 `(org-agenda-type tags
4895 org-last-args (,org--matcher-tags-todo-only ,match)
4896 org-redo-cmd ,org-agenda-redo-command
4897 org-series-cmd ,org-cmd))
4898 (org-agenda-finalize)
4899 (setq buffer-read-only t))))
4901 ;;; Agenda Finding stuck projects
4903 (defvar org-agenda-skip-regexp nil
4904 "Regular expression used in skipping subtrees for the agenda.
4905 This is basically a temporary global variable that can be set and then
4906 used by user-defined selections using `org-agenda-skip-function'.")
4908 (defvar org-agenda-overriding-header nil
4909 "When set during agenda, todo and tags searches it replaces the header.
4910 This variable should not be set directly, but custom commands can bind it
4911 in the options section.")
4913 (defun org-agenda-skip-entry-when-regexp-matches ()
4914 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4915 If yes, it returns the end position of this entry, causing agenda commands
4916 to skip the entry but continuing the search in the subtree. This is a
4917 function that can be put into `org-agenda-skip-function' for the duration
4918 of a command."
4919 (let ((end (save-excursion (org-end-of-subtree t)))
4920 skip)
4921 (save-excursion
4922 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4923 (and skip end)))
4925 (defun org-agenda-skip-subtree-when-regexp-matches ()
4926 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4927 If yes, it returns the end position of this tree, causing agenda commands
4928 to skip this subtree. This is a function that can be put into
4929 `org-agenda-skip-function' for the duration of a command."
4930 (let ((end (save-excursion (org-end-of-subtree t)))
4931 skip)
4932 (save-excursion
4933 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4934 (and skip end)))
4936 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4937 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4938 If yes, it returns the end position of the current entry (NOT the tree),
4939 causing agenda commands to skip the entry but continuing the search in
4940 the subtree. This is a function that can be put into
4941 `org-agenda-skip-function' for the duration of a command. An important
4942 use of this function is for the stuck project list."
4943 (let ((end (save-excursion (org-end-of-subtree t)))
4944 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4945 skip)
4946 (save-excursion
4947 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4948 (and skip entry-end)))
4950 (defun org-agenda-skip-entry-if (&rest conditions)
4951 "Skip entry if any of CONDITIONS is true.
4952 See `org-agenda-skip-if' for details."
4953 (org-agenda-skip-if nil conditions))
4955 (defun org-agenda-skip-subtree-if (&rest conditions)
4956 "Skip subtree if any of CONDITIONS is true.
4957 See `org-agenda-skip-if' for details."
4958 (org-agenda-skip-if t conditions))
4960 (defun org-agenda-skip-if (subtree conditions)
4961 "Checks current entity for CONDITIONS.
4962 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4963 the entry (i.e. the text before the next heading) is checked.
4965 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4966 from different tests. Valid conditions are:
4968 scheduled Check if there is a scheduled cookie
4969 notscheduled Check if there is no scheduled cookie
4970 deadline Check if there is a deadline
4971 notdeadline Check if there is no deadline
4972 timestamp Check if there is a timestamp (also deadline or scheduled)
4973 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4974 regexp Check if regexp matches
4975 notregexp Check if regexp does not match.
4976 todo Check if TODO keyword matches
4977 nottodo Check if TODO keyword does not match
4979 The regexp is taken from the conditions list, it must come right after
4980 the `regexp' or `notregexp' element.
4982 `todo' and `nottodo' accept as an argument a list of todo
4983 keywords, which may include \"*\" to match any todo keyword.
4985 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4987 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4989 Instead of a list, a keyword class may be given. For example:
4991 (org-agenda-skip-entry-if \\='nottodo \\='done)
4993 would skip entries that haven't been marked with any of \"DONE\"
4994 keywords. Possible classes are: `todo', `done', `any'.
4996 If any of these conditions is met, this function returns the end point of
4997 the entity, causing the search to continue from there. This is a function
4998 that can be put into `org-agenda-skip-function' for the duration of a command."
4999 (let (beg end m)
5000 (org-back-to-heading t)
5001 (setq beg (point)
5002 end (if subtree
5003 (progn (org-end-of-subtree t) (point))
5004 (progn (outline-next-heading) (1- (point)))))
5005 (goto-char beg)
5006 (and
5008 (and (memq 'scheduled conditions)
5009 (re-search-forward org-scheduled-time-regexp end t))
5010 (and (memq 'notscheduled conditions)
5011 (not (re-search-forward org-scheduled-time-regexp end t)))
5012 (and (memq 'deadline conditions)
5013 (re-search-forward org-deadline-time-regexp end t))
5014 (and (memq 'notdeadline conditions)
5015 (not (re-search-forward org-deadline-time-regexp end t)))
5016 (and (memq 'timestamp conditions)
5017 (re-search-forward org-ts-regexp end t))
5018 (and (memq 'nottimestamp conditions)
5019 (not (re-search-forward org-ts-regexp end t)))
5020 (and (setq m (memq 'regexp conditions))
5021 (stringp (nth 1 m))
5022 (re-search-forward (nth 1 m) end t))
5023 (and (setq m (memq 'notregexp conditions))
5024 (stringp (nth 1 m))
5025 (not (re-search-forward (nth 1 m) end t)))
5026 (and (or
5027 (setq m (memq 'nottodo conditions))
5028 (setq m (memq 'todo-unblocked conditions))
5029 (setq m (memq 'nottodo-unblocked conditions))
5030 (setq m (memq 'todo conditions)))
5031 (org-agenda-skip-if-todo m end)))
5032 end)))
5034 (defun org-agenda-skip-if-todo (args end)
5035 "Helper function for `org-agenda-skip-if', do not use it directly.
5036 ARGS is a list with first element either `todo', `nottodo',
5037 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5038 a list of TODO keywords, or a state symbol `todo' or `done' or
5039 `any'."
5040 (let ((kw (car args))
5041 (arg (cadr args))
5042 todo-wds todo-re)
5043 (setq todo-wds
5044 (org-uniquify
5045 (cond
5046 ((listp arg) ;; list of keywords
5047 (if (member "*" arg)
5048 (mapcar 'substring-no-properties org-todo-keywords-1)
5049 arg))
5050 ((symbolp arg) ;; keyword class name
5051 (cond
5052 ((eq arg 'todo)
5053 (org-delete-all org-done-keywords
5054 (mapcar 'substring-no-properties
5055 org-todo-keywords-1)))
5056 ((eq arg 'done) org-done-keywords)
5057 ((eq arg 'any)
5058 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5059 (setq todo-re
5060 (concat "^\\*+[ \t]+\\<\\("
5061 (mapconcat 'identity todo-wds "\\|")
5062 "\\)\\>"))
5063 (cond
5064 ((eq kw 'todo) (re-search-forward todo-re end t))
5065 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5066 ((eq kw 'todo-unblocked)
5067 (catch 'unblocked
5068 (while (re-search-forward todo-re end t)
5069 (or (org-entry-blocked-p) (throw 'unblocked t)))
5070 nil))
5071 ((eq kw 'nottodo-unblocked)
5072 (catch 'unblocked
5073 (while (re-search-forward todo-re end t)
5074 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5078 ;;;###autoload
5079 (defun org-agenda-list-stuck-projects (&rest ignore)
5080 "Create agenda view for projects that are stuck.
5081 Stuck projects are project that have no next actions. For the definitions
5082 of what a project is and how to check if it stuck, customize the variable
5083 `org-stuck-projects'."
5084 (interactive)
5085 (let* ((org-agenda-skip-function
5086 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5087 ;; We could have used org-agenda-skip-if here.
5088 (org-agenda-overriding-header
5089 (or org-agenda-overriding-header "List of stuck projects: "))
5090 (matcher (nth 0 org-stuck-projects))
5091 (todo (nth 1 org-stuck-projects))
5092 (todo-wds (if (member "*" todo)
5093 (progn
5094 (org-agenda-prepare-buffers (org-agenda-files
5095 nil 'ifmode))
5096 (org-delete-all
5097 org-done-keywords-for-agenda
5098 (copy-sequence org-todo-keywords-for-agenda)))
5099 todo))
5100 (todo-re (concat "^\\*+[ \t]+\\("
5101 (mapconcat 'identity todo-wds "\\|")
5102 "\\)\\>"))
5103 (tags (nth 2 org-stuck-projects))
5104 (tags-re (if (member "*" tags)
5105 (concat org-outline-regexp-bol
5106 ".*:[[:alnum:]_@#%]+:[ \t]*$")
5107 (if tags
5108 (concat org-outline-regexp-bol
5109 ".*:\\("
5110 (mapconcat #'identity tags "\\|")
5111 "\\):[[:alnum:]_@#%:]*[ \t]*$"))))
5112 (gen-re (nth 3 org-stuck-projects))
5113 (re-list
5114 (delq nil
5115 (list
5116 (if todo todo-re)
5117 (if tags tags-re)
5118 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5119 gen-re)))))
5120 (setq org-agenda-skip-regexp
5121 (if re-list
5122 (mapconcat 'identity re-list "\\|")
5123 (error "No information how to identify unstuck projects")))
5124 (org-tags-view nil matcher)
5125 (setq org-agenda-buffer-name (buffer-name))
5126 (with-current-buffer org-agenda-buffer-name
5127 (setq org-agenda-redo-command
5128 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5130 ;;; Diary integration
5132 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5133 (defvar diary-list-entries-hook)
5134 (defvar diary-time-regexp)
5135 (defun org-get-entries-from-diary (date)
5136 "Get the (Emacs Calendar) diary entries for DATE."
5137 (require 'diary-lib)
5138 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5139 (diary-display-function 'diary-fancy-display)
5140 (pop-up-frames nil)
5141 (diary-list-entries-hook
5142 (cons 'org-diary-default-entry diary-list-entries-hook))
5143 (diary-file-name-prefix nil) ; turn this feature off
5144 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5145 entries
5146 (org-disable-agenda-to-diary t))
5147 (save-excursion
5148 (save-window-excursion
5149 (funcall (if (fboundp 'diary-list-entries)
5150 'diary-list-entries 'list-diary-entries)
5151 date 1)))
5152 (if (not (get-buffer diary-fancy-buffer))
5153 (setq entries nil)
5154 (with-current-buffer diary-fancy-buffer
5155 (setq buffer-read-only nil)
5156 (if (zerop (buffer-size))
5157 ;; No entries
5158 (setq entries nil)
5159 ;; Omit the date and other unnecessary stuff
5160 (org-agenda-cleanup-fancy-diary)
5161 ;; Add prefix to each line and extend the text properties
5162 (if (zerop (buffer-size))
5163 (setq entries nil)
5164 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5165 (setq entries
5166 (with-temp-buffer
5167 (insert entries) (goto-char (point-min))
5168 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5169 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5170 (replace-match (concat "; " (match-string 1)))))
5171 (buffer-string)))))
5172 (set-buffer-modified-p nil)
5173 (kill-buffer diary-fancy-buffer)))
5174 (when entries
5175 (setq entries (org-split-string entries "\n"))
5176 (setq entries
5177 (mapcar
5178 (lambda (x)
5179 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5180 ;; Extend the text properties to the beginning of the line
5181 (org-add-props x (text-properties-at (1- (length x)) x)
5182 'type "diary" 'date date 'face 'org-agenda-diary))
5183 entries)))))
5185 (defvar org-agenda-cleanup-fancy-diary-hook nil
5186 "Hook run when the fancy diary buffer is cleaned up.")
5188 (defun org-agenda-cleanup-fancy-diary ()
5189 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5190 This gets rid of the date, the underline under the date, and
5191 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5192 date. It also removes lines that contain only whitespace."
5193 (goto-char (point-min))
5194 (if (looking-at ".*?:[ \t]*")
5195 (progn
5196 (replace-match "")
5197 (re-search-forward "\n=+$" nil t)
5198 (replace-match "")
5199 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5200 (re-search-forward "\n=+$" nil t)
5201 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5202 (goto-char (point-min))
5203 (while (re-search-forward "^ +\n" nil t)
5204 (replace-match ""))
5205 (goto-char (point-min))
5206 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5207 (replace-match ""))
5208 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5210 ;; Make sure entries from the diary have the right text properties.
5211 (eval-after-load "diary-lib"
5212 '(if (boundp 'diary-modify-entry-list-string-function)
5213 ;; We can rely on the hook, nothing to do
5215 ;; Hook not available, must use advice to make this work
5216 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5217 "Make the position visible."
5218 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5219 (stringp string)
5220 buffer-file-name)
5221 (setq string (org-modify-diary-entry-string string))))))
5223 (defun org-modify-diary-entry-string (string)
5224 "Add text properties to string, allowing org-mode to act on it."
5225 (org-add-props string nil
5226 'mouse-face 'highlight
5227 'help-echo (if buffer-file-name
5228 (format "mouse-2 or RET jump to diary file %s"
5229 (abbreviate-file-name buffer-file-name))
5231 'org-agenda-diary-link t
5232 'org-marker (org-agenda-new-marker (point-at-bol))))
5234 (defun org-diary-default-entry ()
5235 "Add a dummy entry to the diary.
5236 Needed to avoid empty dates which mess up holiday display."
5237 ;; Catch the error if dealing with the new add-to-diary-alist
5238 (when org-disable-agenda-to-diary
5239 (condition-case nil
5240 (org-add-to-diary-list original-date "Org-mode dummy" "")
5241 (error
5242 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5244 (defun org-add-to-diary-list (&rest args)
5245 (if (fboundp 'diary-add-to-list)
5246 (apply 'diary-add-to-list args)
5247 (apply 'add-to-diary-list args)))
5249 (defvar org-diary-last-run-time nil)
5251 ;;;###autoload
5252 (defun org-diary (&rest args)
5253 "Return diary information from org files.
5254 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5255 It accesses org files and extracts information from those files to be
5256 listed in the diary. The function accepts arguments specifying what
5257 items should be listed. For a list of arguments allowed here, see the
5258 variable `org-agenda-entry-types'.
5260 The call in the diary file should look like this:
5262 &%%(org-diary) ~/path/to/some/orgfile.org
5264 Use a separate line for each org file to check. Or, if you omit the file name,
5265 all files listed in `org-agenda-files' will be checked automatically:
5267 &%%(org-diary)
5269 If you don't give any arguments (as in the example above), the default value
5270 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5271 So the example above may also be written as
5273 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5275 The function expects the lisp variables `entry' and `date' to be provided
5276 by the caller, because this is how the calendar works. Don't use this
5277 function from a program - use `org-agenda-get-day-entries' instead."
5278 (when (> (- (float-time)
5279 org-agenda-last-marker-time)
5281 ;; I am not sure if this works with sticky agendas, because the marker
5282 ;; list is then no longer a global variable.
5283 (org-agenda-reset-markers))
5284 (org-compile-prefix-format 'agenda)
5285 (org-set-sorting-strategy 'agenda)
5286 (setq args (or args org-agenda-entry-types))
5287 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5288 (list entry)
5289 (org-agenda-files t)))
5290 (time (float-time))
5291 file rtn results)
5292 (when (or (not org-diary-last-run-time)
5293 (> (- time
5294 org-diary-last-run-time)
5296 (org-agenda-prepare-buffers files))
5297 (setq org-diary-last-run-time time)
5298 ;; If this is called during org-agenda, don't return any entries to
5299 ;; the calendar. Org Agenda will list these entries itself.
5300 (if org-disable-agenda-to-diary (setq files nil))
5301 (while (setq file (pop files))
5302 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5303 (setq results (append results rtn)))
5304 (when results
5305 (setq results
5306 (mapcar (lambda (i) (replace-regexp-in-string
5307 org-bracket-link-regexp "\\3" i)) results))
5308 (concat (org-agenda-finalize-entries results) "\n"))))
5310 ;;; Agenda entry finders
5312 (defun org-agenda--timestamp-to-absolute (&rest args)
5313 "Call `org-time-string-to-absolute' with ARGS.
5314 However, throw `:skip' whenever an error is raised."
5315 (condition-case e
5316 (apply #'org-time-string-to-absolute args)
5317 (org-diary-sexp-no-match (throw :skip nil))
5318 (error
5319 (message "%s; Skipping entry" (error-message-string e))
5320 (throw :skip nil))))
5322 (defun org-agenda-get-day-entries (file date &rest args)
5323 "Does the work for `org-diary' and `org-agenda'.
5324 FILE is the path to a file to be checked for entries. DATE is date like
5325 the one returned by `calendar-current-date'. ARGS are symbols indicating
5326 which kind of entries should be extracted. For details about these, see
5327 the documentation of `org-diary'."
5328 (let* ((org-startup-folded nil)
5329 (org-startup-align-all-tables nil)
5330 (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
5331 (error "No such file %s" file))))
5332 (if (not buffer)
5333 ;; If file does not exist, signal it in diary nonetheless.
5334 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5335 (with-current-buffer buffer
5336 (unless (derived-mode-p 'org-mode)
5337 (error "Agenda file %s is not in `org-mode'" file))
5338 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5339 (setf org-agenda-current-date date)
5340 (save-excursion
5341 (save-restriction
5342 (if (eq buffer org-agenda-restrict)
5343 (narrow-to-region org-agenda-restrict-begin
5344 org-agenda-restrict-end)
5345 (widen))
5346 ;; Rationalize ARGS. Also make sure `:deadline' comes
5347 ;; first in order to populate DEADLINES before passing it.
5349 ;; We use `delq' since `org-uniquify' duplicates ARGS,
5350 ;; guarding us from modifying `org-agenda-entry-types'.
5351 (setf args (org-uniquify (or args org-agenda-entry-types)))
5352 (when (and (memq :scheduled args) (memq :scheduled* args))
5353 (setf args (delq :scheduled* args)))
5354 (cond
5355 ((memq :deadline args)
5356 (setf args (cons :deadline
5357 (delq :deadline (delq :deadline* args)))))
5358 ((memq :deadline* args)
5359 (setf args (cons :deadline* (delq :deadline* args)))))
5360 ;; Collect list of headlines. Return them flattened.
5361 (let ((case-fold-search nil) results deadlines)
5362 (dolist (arg args (apply #'nconc (nreverse results)))
5363 (pcase arg
5364 ((and :todo (guard (org-agenda-today-p date)))
5365 (push (org-agenda-get-todos) results))
5366 (:timestamp
5367 (push (org-agenda-get-blocks) results)
5368 (push (org-agenda-get-timestamps deadlines) results))
5369 (:sexp
5370 (push (org-agenda-get-sexps) results))
5371 (:scheduled
5372 (push (org-agenda-get-scheduled deadlines) results))
5373 (:scheduled*
5374 (push (org-agenda-get-scheduled deadlines t) results))
5375 (:closed
5376 (push (org-agenda-get-progress) results))
5377 (:deadline
5378 (setf deadlines (org-agenda-get-deadlines))
5379 (push deadlines results))
5380 (:deadline*
5381 (setf deadlines (org-agenda-get-deadlines t))
5382 (push deadlines results)))))))))))
5384 (defsubst org-em (x y list)
5385 "Is X or Y a member of LIST?"
5386 (or (memq x list) (memq y list)))
5388 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5389 (defvar org-agenda-sorting-strategy-selected nil)
5391 (defun org-agenda-entry-get-agenda-timestamp (pom)
5392 "Retrieve timestamp information for sorting agenda views.
5393 Given a point or marker POM, returns a cons cell of the timestamp
5394 and the timestamp type relevant for the sorting strategy in
5395 `org-agenda-sorting-strategy-selected'."
5396 (let (ts ts-date-type)
5397 (save-match-data
5398 (cond ((org-em 'scheduled-up 'scheduled-down
5399 org-agenda-sorting-strategy-selected)
5400 (setq ts (org-entry-get pom "SCHEDULED")
5401 ts-date-type " scheduled"))
5402 ((org-em 'deadline-up 'deadline-down
5403 org-agenda-sorting-strategy-selected)
5404 (setq ts (org-entry-get pom "DEADLINE")
5405 ts-date-type " deadline"))
5406 ((org-em 'ts-up 'ts-down
5407 org-agenda-sorting-strategy-selected)
5408 (setq ts (org-entry-get pom "TIMESTAMP")
5409 ts-date-type " timestamp"))
5410 ((org-em 'tsia-up 'tsia-down
5411 org-agenda-sorting-strategy-selected)
5412 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5413 ts-date-type " timestamp_ia"))
5414 ((org-em 'timestamp-up 'timestamp-down
5415 org-agenda-sorting-strategy-selected)
5416 (setq ts (or (org-entry-get pom "SCHEDULED")
5417 (org-entry-get pom "DEADLINE")
5418 (org-entry-get pom "TIMESTAMP")
5419 (org-entry-get pom "TIMESTAMP_IA"))
5420 ts-date-type ""))
5421 (t (setq ts-date-type "")))
5422 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5423 ts-date-type))))
5425 (defun org-agenda-get-todos ()
5426 "Return the TODO information for agenda display."
5427 (let* ((props (list 'face nil
5428 'done-face 'org-agenda-done
5429 'org-not-done-regexp org-not-done-regexp
5430 'org-todo-regexp org-todo-regexp
5431 'org-complex-heading-regexp org-complex-heading-regexp
5432 'mouse-face 'highlight
5433 'help-echo
5434 (format "mouse-2 or RET jump to org file %s"
5435 (abbreviate-file-name buffer-file-name))))
5436 (regexp (format org-heading-keyword-regexp-format
5437 (cond
5438 ((and org-select-this-todo-keyword
5439 (equal org-select-this-todo-keyword "*"))
5440 org-todo-regexp)
5441 (org-select-this-todo-keyword
5442 (concat "\\("
5443 (mapconcat 'identity
5444 (org-split-string
5445 org-select-this-todo-keyword
5446 "|")
5447 "\\|") "\\)"))
5448 (t org-not-done-regexp))))
5449 marker priority category level tags todo-state
5450 ts-date ts-date-type ts-date-pair
5451 ee txt beg end inherited-tags todo-state-end-pos)
5452 (goto-char (point-min))
5453 (while (re-search-forward regexp nil t)
5454 (catch :skip
5455 (save-match-data
5456 (beginning-of-line)
5457 (org-agenda-skip)
5458 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5459 (unless (and (setq todo-state (org-get-todo-state))
5460 (setq todo-state-end-pos (match-end 2)))
5461 (goto-char end)
5462 (throw :skip nil))
5463 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5464 (goto-char (1+ beg))
5465 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5466 (throw :skip nil)))
5467 (goto-char (match-beginning 2))
5468 (setq marker (org-agenda-new-marker (match-beginning 0))
5469 category (org-get-category)
5470 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5471 ts-date (car ts-date-pair)
5472 ts-date-type (cdr ts-date-pair)
5473 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5474 inherited-tags
5475 (or (eq org-agenda-show-inherited-tags 'always)
5476 (and (listp org-agenda-show-inherited-tags)
5477 (memq 'todo org-agenda-show-inherited-tags))
5478 (and (eq org-agenda-show-inherited-tags t)
5479 (or (eq org-agenda-use-tag-inheritance t)
5480 (memq 'todo org-agenda-use-tag-inheritance))))
5481 tags (org-get-tags-at nil (not inherited-tags))
5482 level (make-string (org-reduced-level (org-outline-level)) ? )
5483 txt (org-agenda-format-item "" txt level category tags t)
5484 priority (1+ (org-get-priority txt)))
5485 (org-add-props txt props
5486 'org-marker marker 'org-hd-marker marker
5487 'priority priority
5488 'level level
5489 'ts-date ts-date
5490 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5491 (push txt ee)
5492 (if org-agenda-todo-list-sublevels
5493 (goto-char todo-state-end-pos)
5494 (org-end-of-subtree 'invisible))))
5495 (nreverse ee)))
5497 (defun org-agenda-todo-custom-ignore-p (time n)
5498 "Check whether timestamp is farther away than n number of days.
5499 This function is invoked if `org-agenda-todo-ignore-deadlines',
5500 `org-agenda-todo-ignore-scheduled' or
5501 `org-agenda-todo-ignore-timestamp' is set to an integer."
5502 (let ((days (org-time-stamp-to-now
5503 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5504 (if (>= n 0)
5505 (>= days n)
5506 (<= days n))))
5508 ;;;###autoload
5509 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5510 (&optional end)
5511 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5512 (when (or org-agenda-todo-ignore-with-date
5513 org-agenda-todo-ignore-scheduled
5514 org-agenda-todo-ignore-deadlines
5515 org-agenda-todo-ignore-timestamp)
5516 (setq end (or end (save-excursion (outline-next-heading) (point))))
5517 (save-excursion
5518 (or (and org-agenda-todo-ignore-with-date
5519 (re-search-forward org-ts-regexp end t))
5520 (and org-agenda-todo-ignore-scheduled
5521 (re-search-forward org-scheduled-time-regexp end t)
5522 (cond
5523 ((eq org-agenda-todo-ignore-scheduled 'future)
5524 (> (org-time-stamp-to-now
5525 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5526 ((eq org-agenda-todo-ignore-scheduled 'past)
5527 (<= (org-time-stamp-to-now
5528 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5529 ((numberp org-agenda-todo-ignore-scheduled)
5530 (org-agenda-todo-custom-ignore-p
5531 (match-string 1) org-agenda-todo-ignore-scheduled))
5532 (t)))
5533 (and org-agenda-todo-ignore-deadlines
5534 (re-search-forward org-deadline-time-regexp end t)
5535 (cond
5536 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5537 ((eq org-agenda-todo-ignore-deadlines 'far)
5538 (not (org-deadline-close-p (match-string 1))))
5539 ((eq org-agenda-todo-ignore-deadlines 'future)
5540 (> (org-time-stamp-to-now
5541 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5542 ((eq org-agenda-todo-ignore-deadlines 'past)
5543 (<= (org-time-stamp-to-now
5544 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5545 ((numberp org-agenda-todo-ignore-deadlines)
5546 (org-agenda-todo-custom-ignore-p
5547 (match-string 1) org-agenda-todo-ignore-deadlines))
5548 (t (org-deadline-close-p (match-string 1)))))
5549 (and org-agenda-todo-ignore-timestamp
5550 (let ((buffer (current-buffer))
5551 (regexp
5552 (concat
5553 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5554 (start (point)))
5555 ;; Copy current buffer into a temporary one
5556 (with-temp-buffer
5557 (insert-buffer-substring buffer start end)
5558 (goto-char (point-min))
5559 ;; Delete SCHEDULED and DEADLINE items
5560 (while (re-search-forward regexp end t)
5561 (delete-region (match-beginning 0) (match-end 0)))
5562 (goto-char (point-min))
5563 ;; No search for timestamp left
5564 (when (re-search-forward org-ts-regexp nil t)
5565 (cond
5566 ((eq org-agenda-todo-ignore-timestamp 'future)
5567 (> (org-time-stamp-to-now
5568 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5569 ((eq org-agenda-todo-ignore-timestamp 'past)
5570 (<= (org-time-stamp-to-now
5571 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5572 ((numberp org-agenda-todo-ignore-timestamp)
5573 (org-agenda-todo-custom-ignore-p
5574 (match-string 1) org-agenda-todo-ignore-timestamp))
5575 (t))))))))))
5577 (defun org-agenda-get-timestamps (&optional deadline-results)
5578 "Return the date stamp information for agenda display."
5579 (let* ((props (list 'face 'org-agenda-calendar-event
5580 'org-not-done-regexp org-not-done-regexp
5581 'org-todo-regexp org-todo-regexp
5582 'org-complex-heading-regexp org-complex-heading-regexp
5583 'mouse-face 'highlight
5584 'help-echo
5585 (format "mouse-2 or RET jump to org file %s"
5586 (abbreviate-file-name buffer-file-name))))
5587 (d1 (calendar-absolute-from-gregorian date))
5589 (deadline-position-alist
5590 (mapcar (lambda (a) (and (setq mm (get-text-property
5591 0 'org-hd-marker a))
5592 (cons (marker-position mm) a)))
5593 deadline-results))
5594 (remove-re org-ts-regexp)
5595 (regexp
5596 (concat
5597 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5598 (regexp-quote
5599 (substring
5600 (format-time-string
5601 (car org-time-stamp-formats)
5602 (apply 'encode-time ; DATE bound by calendar
5603 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5604 1 11))
5605 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5606 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5607 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5608 donep tmp priority category level ee txt timestr tags
5609 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5610 inherited-tags ts-date)
5611 (goto-char (point-min))
5612 (while (setq end-of-match (re-search-forward regexp nil t))
5613 (setq b0 (match-beginning 0)
5614 b3 (match-beginning 3) e3 (match-end 3)
5615 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5616 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5617 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5618 (member todo-state
5619 org-agenda-repeating-timestamp-show-all)))
5620 (catch :skip
5621 (and (org-at-date-range-p) (throw :skip nil))
5622 (org-agenda-skip)
5623 (if (and (match-end 1)
5624 (not (= d1 (org-agenda--timestamp-to-absolute
5625 (match-string 1) d1 nil show-all
5626 (current-buffer) b0))))
5627 (throw :skip nil))
5628 (if (and e3
5629 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5630 (throw :skip nil))
5631 (setq tmp (buffer-substring (max (point-min)
5632 (- b0 org-ds-keyword-length))
5634 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5635 inactivep (= (char-after b0) ?\[)
5636 deadlinep (string-match org-deadline-regexp tmp)
5637 scheduledp (string-match org-scheduled-regexp tmp)
5638 closedp (and org-agenda-include-inactive-timestamps
5639 (string-match org-closed-string tmp))
5640 clockp (and org-agenda-include-inactive-timestamps
5641 (or (string-match org-clock-string tmp)
5642 (string-match "]-+\\'" tmp)))
5643 warntime (get-text-property (point) 'org-appt-warntime)
5644 donep (member todo-state org-done-keywords))
5645 (if (or scheduledp deadlinep closedp clockp
5646 (and donep org-agenda-skip-timestamp-if-done))
5647 (throw :skip t))
5648 (if (string-match ">" timestr)
5649 ;; substring should only run to end of time stamp
5650 (setq timestr (substring timestr 0 (match-end 0))))
5651 (setq marker (org-agenda-new-marker b0)
5652 category (org-get-category b0))
5653 (save-excursion
5654 (if (not (re-search-backward org-outline-regexp-bol nil t))
5655 (throw :skip nil)
5656 (goto-char (match-beginning 0))
5657 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5658 (assoc (point) deadline-position-alist))
5659 (throw :skip nil))
5660 (setq hdmarker (org-agenda-new-marker)
5661 inherited-tags
5662 (or (eq org-agenda-show-inherited-tags 'always)
5663 (and (listp org-agenda-show-inherited-tags)
5664 (memq 'agenda org-agenda-show-inherited-tags))
5665 (and (eq org-agenda-show-inherited-tags t)
5666 (or (eq org-agenda-use-tag-inheritance t)
5667 (memq 'agenda org-agenda-use-tag-inheritance))))
5668 tags (org-get-tags-at nil (not inherited-tags))
5669 level (make-string (org-reduced-level (org-outline-level)) ? ))
5670 (looking-at "\\*+[ \t]+\\(.*\\)")
5671 (setq head (match-string 1))
5672 (setq txt (org-agenda-format-item
5673 (if inactivep org-agenda-inactive-leader nil)
5674 head level category tags timestr
5675 remove-re habitp)))
5676 (setq priority (org-get-priority txt))
5677 (org-add-props txt props 'priority priority
5678 'org-marker marker 'org-hd-marker hdmarker
5679 'date date
5680 'level level
5681 'ts-date
5682 (ignore-errors (org-time-string-to-absolute timestr))
5683 'todo-state todo-state
5684 'warntime warntime
5685 'type "timestamp")
5686 (push txt ee))
5687 (if org-agenda-skip-additional-timestamps-same-entry
5688 (outline-next-heading)
5689 (goto-char end-of-match))))
5690 (nreverse ee)))
5692 (defun org-agenda-get-sexps ()
5693 "Return the sexp information for agenda display."
5694 (require 'diary-lib)
5695 (let* ((props (list 'face 'org-agenda-calendar-sexp
5696 'mouse-face 'highlight
5697 'help-echo
5698 (format "mouse-2 or RET jump to org file %s"
5699 (abbreviate-file-name buffer-file-name))))
5700 (regexp "^&?%%(")
5701 marker category extra level ee txt tags entry
5702 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5703 (goto-char (point-min))
5704 (while (re-search-forward regexp nil t)
5705 (catch :skip
5706 (org-agenda-skip)
5707 (setq beg (match-beginning 0))
5708 (goto-char (1- (match-end 0)))
5709 (setq b (point))
5710 (forward-sexp 1)
5711 (setq sexp (buffer-substring b (point)))
5712 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5713 (org-trim (match-string 1))
5714 ""))
5715 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5716 (when result
5717 (setq marker (org-agenda-new-marker beg)
5718 level (make-string (org-reduced-level (org-outline-level)) ? )
5719 category (org-get-category beg)
5720 inherited-tags
5721 (or (eq org-agenda-show-inherited-tags 'always)
5722 (and (listp org-agenda-show-inherited-tags)
5723 (memq 'agenda org-agenda-show-inherited-tags))
5724 (and (eq org-agenda-show-inherited-tags t)
5725 (or (eq org-agenda-use-tag-inheritance t)
5726 (memq 'agenda org-agenda-use-tag-inheritance))))
5727 tags (org-get-tags-at nil (not inherited-tags))
5728 todo-state (org-get-todo-state)
5729 warntime (get-text-property (point) 'org-appt-warntime)
5730 extra nil)
5732 (dolist (r (if (stringp result)
5733 (list result)
5734 result)) ;; we expect a list here
5735 (when (and org-agenda-diary-sexp-prefix
5736 (string-match org-agenda-diary-sexp-prefix r))
5737 (setq extra (match-string 0 r)
5738 r (replace-match "" nil nil r)))
5739 (if (string-match "\\S-" r)
5740 (setq txt r)
5741 (setq txt "SEXP entry returned empty string"))
5742 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5743 (org-add-props txt props 'org-marker marker
5744 'date date 'todo-state todo-state
5745 'level level 'type "sexp" 'warntime warntime)
5746 (push txt ee)))))
5747 (nreverse ee)))
5749 ;; Calendar sanity: define some functions that are independent of
5750 ;; `calendar-date-style'.
5751 (defun org-anniversary (year month day &optional mark)
5752 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5753 (with-no-warnings
5754 (let ((calendar-date-style 'iso))
5755 (diary-anniversary year month day mark))))
5756 (defun org-cyclic (N year month day &optional mark)
5757 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5758 (with-no-warnings
5759 (let ((calendar-date-style 'iso))
5760 (diary-cyclic N year month day mark))))
5761 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5762 "Like `diary-block', but with fixed (ISO) order of arguments."
5763 (with-no-warnings
5764 (let ((calendar-date-style 'iso))
5765 (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
5766 (defun org-date (year month day &optional mark)
5767 "Like `diary-date', but with fixed (ISO) order of arguments."
5768 (with-no-warnings
5769 (let ((calendar-date-style 'iso))
5770 (diary-date year month day mark))))
5772 ;; Define the `org-class' function
5773 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5774 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5775 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5776 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5777 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5778 `holidays', then any date that is known by the Emacs calendar to be a
5779 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5780 then those holidays will be skipped."
5781 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5782 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5783 (d (calendar-absolute-from-gregorian date))
5784 (h (when skip-weeks (calendar-check-holidays date))))
5785 (and
5786 (<= date1 d)
5787 (<= d date2)
5788 (= (calendar-day-of-week date) dayname)
5789 (or (not skip-weeks)
5790 (progn
5791 (require 'cal-iso)
5792 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5793 (not (or (and h (memq 'holidays skip-weeks))
5794 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5795 entry)))
5797 (defalias 'org-get-closed 'org-agenda-get-progress)
5798 (defun org-agenda-get-progress ()
5799 "Return the logged TODO entries for agenda display."
5800 (let* ((props (list 'mouse-face 'highlight
5801 'org-not-done-regexp org-not-done-regexp
5802 'org-todo-regexp org-todo-regexp
5803 'org-complex-heading-regexp org-complex-heading-regexp
5804 'help-echo
5805 (format "mouse-2 or RET jump to org file %s"
5806 (abbreviate-file-name buffer-file-name))))
5807 (items (if (consp org-agenda-show-log-scoped)
5808 org-agenda-show-log-scoped
5809 (if (eq org-agenda-show-log-scoped 'clockcheck)
5810 '(clock)
5811 org-agenda-log-mode-items)))
5812 (parts
5813 (delq nil
5814 (list
5815 (if (memq 'closed items) (concat "\\<" org-closed-string))
5816 (if (memq 'clock items) (concat "\\<" org-clock-string))
5817 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5818 (parts-re (if parts (mapconcat 'identity parts "\\|")
5819 (error "`org-agenda-log-mode-items' is empty")))
5820 (regexp (concat
5821 "\\(" parts-re "\\)"
5822 " *\\["
5823 (regexp-quote
5824 (substring
5825 (format-time-string
5826 (car org-time-stamp-formats)
5827 (apply 'encode-time ; DATE bound by calendar
5828 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5829 1 11))))
5830 (org-agenda-search-headline-for-time nil)
5831 marker hdmarker priority category level tags closedp
5832 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5833 (goto-char (point-min))
5834 (while (re-search-forward regexp nil t)
5835 (catch :skip
5836 (org-agenda-skip)
5837 (setq marker (org-agenda-new-marker (match-beginning 0))
5838 closedp (equal (match-string 1) org-closed-string)
5839 statep (equal (string-to-char (match-string 1)) ?-)
5840 clockp (not (or closedp statep))
5841 state (and statep (match-string 2))
5842 category (org-get-category (match-beginning 0))
5843 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5844 (when (string-match "\\]" timestr)
5845 ;; substring should only run to end of time stamp
5846 (setq rest (substring timestr (match-end 0))
5847 timestr (substring timestr 0 (match-end 0)))
5848 (if (and (not closedp) (not statep)
5849 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5850 rest))
5851 (progn (setq timestr (concat (substring timestr 0 -1)
5852 "-" (match-string 1 rest) "]"))
5853 (setq clocked (match-string 2 rest)))
5854 (setq clocked "-")))
5855 (save-excursion
5856 (setq extra
5857 (cond
5858 ((not org-agenda-log-mode-add-notes) nil)
5859 (statep
5860 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5861 (match-string 1)))
5862 (clockp
5863 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5864 (match-string 1)))))
5865 (if (not (re-search-backward org-outline-regexp-bol nil t))
5866 (throw :skip nil)
5867 (goto-char (match-beginning 0))
5868 (setq hdmarker (org-agenda-new-marker)
5869 inherited-tags
5870 (or (eq org-agenda-show-inherited-tags 'always)
5871 (and (listp org-agenda-show-inherited-tags)
5872 (memq 'todo org-agenda-show-inherited-tags))
5873 (and (eq org-agenda-show-inherited-tags t)
5874 (or (eq org-agenda-use-tag-inheritance t)
5875 (memq 'todo org-agenda-use-tag-inheritance))))
5876 tags (org-get-tags-at nil (not inherited-tags))
5877 level (make-string (org-reduced-level (org-outline-level)) ? ))
5878 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5879 (setq txt (match-string 1))
5880 (when extra
5881 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5882 (setq txt (concat (substring txt 0 (match-beginning 1))
5883 " - " extra " " (match-string 2 txt)))
5884 (setq txt (concat txt " - " extra))))
5885 (setq txt (org-agenda-format-item
5886 (cond
5887 (closedp "Closed: ")
5888 (statep (concat "State: (" state ")"))
5889 (t (concat "Clocked: (" clocked ")")))
5890 txt level category tags timestr)))
5891 (setq priority 100000)
5892 (org-add-props txt props
5893 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5894 'priority priority 'level level
5895 'type "closed" 'date date
5896 'undone-face 'org-warning 'done-face 'org-agenda-done)
5897 (push txt ee))
5898 (goto-char (point-at-eol))))
5899 (nreverse ee)))
5901 (defun org-agenda-show-clocking-issues ()
5902 "Add overlays, showing issues with clocking.
5903 See also the user option `org-agenda-clock-consistency-checks'."
5904 (interactive)
5905 (let* ((org-time-clocksum-use-effort-durations nil)
5906 (pl org-agenda-clock-consistency-checks)
5907 (re (concat "^[ \t]*"
5908 org-clock-string
5909 "[ \t]+"
5910 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5911 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5912 (tlstart 0.)
5913 (tlend 0.)
5914 (maxtime (org-hh:mm-string-to-minutes
5915 (or (plist-get pl :max-duration) "24:00")))
5916 (mintime (org-hh:mm-string-to-minutes
5917 (or (plist-get pl :min-duration) 0)))
5918 (maxgap (org-hh:mm-string-to-minutes
5919 ;; default 30:00 means never complain
5920 (or (plist-get pl :max-gap) "30:00")))
5921 (gapok (mapcar 'org-hh:mm-string-to-minutes
5922 (plist-get pl :gap-ok-around)))
5923 (def-face (or (plist-get pl :default-face)
5924 '((:background "DarkRed") (:foreground "white"))))
5925 issue face m te ts dt ov)
5926 (goto-char (point-min))
5927 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5928 (setq issue nil face def-face)
5929 (catch 'next
5930 (setq m (org-get-at-bol 'org-marker)
5931 te nil ts nil)
5932 (unless (and m (markerp m))
5933 (setq issue "No valid clock line") (throw 'next t))
5934 (org-with-point-at m
5935 (save-excursion
5936 (goto-char (point-at-bol))
5937 (unless (looking-at re)
5938 (error "No valid Clock line")
5939 (throw 'next t))
5940 (unless (match-end 3)
5941 (setq issue "No end time"
5942 face (or (plist-get pl :no-end-time-face) face))
5943 (throw 'next t))
5944 (setq ts (match-string 1)
5945 te (match-string 3)
5946 ts (float-time
5947 (apply #'encode-time (org-parse-time-string ts)))
5948 te (float-time
5949 (apply #'encode-time (org-parse-time-string te)))
5950 dt (- te ts))))
5951 (cond
5952 ((> dt (* 60 maxtime))
5953 ;; a very long clocking chunk
5954 (setq issue (format "Clocking interval is very long: %s"
5955 (org-minutes-to-clocksum-string
5956 (floor (/ (float dt) 60.))))
5957 face (or (plist-get pl :long-face) face)))
5958 ((< dt (* 60 mintime))
5959 ;; a very short clocking chunk
5960 (setq issue (format "Clocking interval is very short: %s"
5961 (org-minutes-to-clocksum-string
5962 (floor (/ (float dt) 60.))))
5963 face (or (plist-get pl :short-face) face)))
5964 ((and (> tlend 0) (< ts tlend))
5965 ;; Two clock entries are overlapping
5966 (setq issue (format "Clocking overlap: %d minutes"
5967 (/ (- tlend ts) 60))
5968 face (or (plist-get pl :overlap-face) face)))
5969 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5970 ;; There is a gap, lets see if we need to report it
5971 (unless (org-agenda-check-clock-gap tlend ts gapok)
5972 (setq issue (format "Clocking gap: %d minutes"
5973 (/ (- ts tlend) 60))
5974 face (or (plist-get pl :gap-face) face))))
5975 (t nil)))
5976 (setq tlend (or te tlend) tlstart (or ts tlstart))
5977 (when issue
5978 ;; OK, there was some issue, add an overlay to show the issue
5979 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5980 (overlay-put ov 'before-string
5981 (concat
5982 (org-add-props
5983 (format "%-43s" (concat " " issue))
5985 'face face)
5986 "\n"))
5987 (overlay-put ov 'evaporate t)))))
5989 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5990 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5991 (catch 'exit
5992 (unless ok-list
5993 ;; there are no OK times for gaps...
5994 (throw 'exit nil))
5995 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5996 ;; This is more than 24 hours, so it is OK.
5997 ;; because we have at least one OK time, that must be in the
5998 ;; 24 hour interval.
5999 (throw 'exit t))
6000 ;; We have a shorter gap.
6001 ;; Now we have to get the minute of the day when these times are
6002 (let* ((t1dec (decode-time (seconds-to-time t1)))
6003 (t2dec (decode-time (seconds-to-time t2)))
6004 ;; compute the minute on the day
6005 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6006 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6007 (when (< min2 min1)
6008 ;; if min2 is smaller than min1, this means it is on the next day.
6009 ;; Wrap it to after midnight.
6010 (setq min2 (+ min2 1440)))
6011 ;; Now check if any of the OK times is in the gap
6012 (mapc (lambda (x)
6013 ;; Wrap the time to after midnight if necessary
6014 (if (< x min1) (setq x (+ x 1440)))
6015 ;; Check if in interval
6016 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6017 ok-list)
6018 ;; Nope, this gap is not OK
6019 nil)))
6021 (defun org-agenda-get-deadlines (&optional with-hour)
6022 "Return the deadline information for agenda display.
6023 When WITH-HOUR is non-nil, only return deadlines with an hour
6024 specification like [h]h:mm."
6025 (let* ((props (list 'mouse-face 'highlight
6026 'org-not-done-regexp org-not-done-regexp
6027 'org-todo-regexp org-todo-regexp
6028 'org-complex-heading-regexp org-complex-heading-regexp
6029 'help-echo
6030 (format "mouse-2 or RET jump to org file %s"
6031 (abbreviate-file-name buffer-file-name))))
6032 (regexp (if with-hour
6033 org-deadline-time-hour-regexp
6034 org-deadline-time-regexp))
6035 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6036 (current (calendar-absolute-from-gregorian date))
6037 deadline-items)
6038 (goto-char (point-min))
6039 (while (re-search-forward regexp nil t)
6040 (catch :skip
6041 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6042 (org-agenda-skip)
6043 (let* ((s (match-string 1))
6044 (pos (1- (match-beginning 1)))
6045 (todo-state (save-match-data (org-get-todo-state)))
6046 (donep (member todo-state org-done-keywords))
6047 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6048 (member todo-state
6049 org-agenda-repeating-timestamp-show-all)))
6050 ;; DEADLINE is the current scheduled date. When it
6051 ;; contains a repeater and SHOW-ALL is non-nil,
6052 ;; LAST-REPEAT is the repeat closest to CURRENT.
6053 ;; Otherwise, LAST-REPEAT is equal to DEADLINE.
6054 (last-repeat (org-agenda--timestamp-to-absolute
6055 s current 'past show-all (current-buffer) pos))
6056 (deadline (org-agenda--timestamp-to-absolute s current))
6057 (diff (- last-repeat current))
6058 (suppress-prewarning
6059 (let ((scheduled
6060 (and org-agenda-skip-deadline-prewarning-if-scheduled
6061 (org-entry-get nil "SCHEDULED"))))
6062 (cond
6063 ((not scheduled) nil)
6064 ;; The current item has a scheduled date, so
6065 ;; evaluate its prewarning lead time.
6066 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6067 ;; Use global prewarning-restart lead time.
6068 org-agenda-skip-deadline-prewarning-if-scheduled)
6069 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6070 'pre-scheduled)
6071 ;; Set pre-warning to no earlier than SCHEDULED.
6072 (min (- last-repeat
6073 (org-agenda--timestamp-to-absolute
6074 scheduled current 'past show-all
6075 (current-buffer)
6076 (save-excursion
6077 (beginning-of-line)
6078 (1+ (search-forward org-deadline-string)))))
6079 org-deadline-warning-days))
6080 ;; Set pre-warning to deadline.
6081 (t 0))))
6082 (wdays (if suppress-prewarning
6083 (let ((org-deadline-warning-days suppress-prewarning))
6084 (org-get-wdays s))
6085 (org-get-wdays s))))
6086 ;; When to show a deadline in the calendar: if the
6087 ;; expiration is within WDAYS warning time. Past-due
6088 ;; deadlines are only shown on the current date
6089 (unless (or (and (<= diff wdays)
6090 (and todayp (not org-agenda-only-exact-dates)))
6091 (= diff 0))
6092 (throw :skip nil))
6093 ;; Skip done tasks if `org-agenda-skip-deadline-if-done' is
6094 ;; non-nil or if it isn't applicable to CURRENT deadline.
6095 (when (and donep
6096 (or org-agenda-skip-deadline-if-done
6097 (/= deadline current)))
6098 (throw :skip nil))
6099 (save-excursion
6100 (re-search-backward "^\\*+[ \t]+" nil t)
6101 (goto-char (match-end 0))
6102 (let* ((category (org-get-category))
6103 (level
6104 (make-string (org-reduced-level (org-outline-level)) ?\s))
6105 (head (buffer-substring (point) (line-end-position)))
6106 (inherited-tags
6107 (or (eq org-agenda-show-inherited-tags 'always)
6108 (and (listp org-agenda-show-inherited-tags)
6109 (memq 'agenda org-agenda-show-inherited-tags))
6110 (and (eq org-agenda-show-inherited-tags t)
6111 (or (eq org-agenda-use-tag-inheritance t)
6112 (memq 'agenda
6113 org-agenda-use-tag-inheritance)))))
6114 (tags (org-get-tags-at nil (not inherited-tags)))
6115 (timestr
6116 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6117 (concat (substring s (match-beginning 1)) " ")
6118 'time))
6119 (item
6120 (org-agenda-format-item
6121 ;; For past deadlines, make sure to report time
6122 ;; difference since date S, not since closest
6123 ;; repeater.
6124 (let ((diff (if (< (org-today) current) diff
6125 (- deadline current))))
6126 (if (= diff 0) (car org-agenda-deadline-leaders)
6127 (let ((future (nth 1 org-agenda-deadline-leaders))
6128 (past (nth 2 org-agenda-deadline-leaders)))
6129 (cond ((> diff 0) (format future diff))
6130 ((string= future past) (format past diff))
6131 (t (format past (abs diff)))))))
6132 head level category tags
6133 (and (= diff 0) timestr)))
6134 (face (org-agenda-deadline-face
6135 (- 1 (/ (float (- deadline current)) (max wdays 1)))))
6136 (upcomingp (and todayp (> diff 0)))
6137 (warntime (get-text-property (point) 'org-appt-warntime)))
6138 (org-add-props item props
6139 'org-marker (org-agenda-new-marker pos)
6140 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6141 'warntime warntime
6142 'level level
6143 'ts-date deadline
6144 'priority (- (org-get-priority item) diff)
6145 'todo-state todo-state
6146 'type (if upcomingp "upcoming-deadline" "deadline")
6147 'date (if upcomingp date deadline)
6148 'face (if donep 'org-agenda-done face)
6149 'undone-face face
6150 'done-face 'org-agenda-done)
6151 (push item deadline-items))))))
6152 (nreverse deadline-items)))
6154 (defun org-agenda-deadline-face (fraction)
6155 "Return the face to displaying a deadline item.
6156 FRACTION is what fraction of the head-warning time has passed."
6157 (let ((faces org-agenda-deadline-faces) f)
6158 (catch 'exit
6159 (while (setq f (pop faces))
6160 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6162 (defun org-agenda-get-scheduled (&optional deadlines with-hour)
6163 "Return the scheduled information for agenda display.
6164 Optional argument DEADLINES is a list of deadline items to be
6165 displayed in agenda view. When WITH-HOUR is non-nil, only return
6166 scheduled items with an hour specification like [h]h:mm."
6167 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6168 'org-todo-regexp org-todo-regexp
6169 'org-complex-heading-regexp org-complex-heading-regexp
6170 'done-face 'org-agenda-done
6171 'mouse-face 'highlight
6172 'help-echo
6173 (format "mouse-2 or RET jump to org file %s"
6174 (abbreviate-file-name buffer-file-name))))
6175 (regexp (if with-hour
6176 org-scheduled-time-hour-regexp
6177 org-scheduled-time-regexp))
6178 (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
6179 (current (calendar-absolute-from-gregorian date))
6180 (deadline-pos
6181 (mapcar (lambda (d)
6182 (let ((m (get-text-property 0 'org-hd-marker d)))
6183 (and m (marker-position m))))
6184 deadlines))
6185 scheduled-items)
6186 (goto-char (point-min))
6187 (while (re-search-forward regexp nil t)
6188 (catch :skip
6189 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6190 (org-agenda-skip)
6191 (let* ((s (match-string 1))
6192 (pos (1- (match-beginning 1)))
6193 (todo-state (save-match-data (org-get-todo-state)))
6194 (donep (member todo-state org-done-keywords))
6195 (show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6196 (member todo-state
6197 org-agenda-repeating-timestamp-show-all)))
6198 ;; SCHEDULE is the current scheduled date. When it
6199 ;; contains a repeater and SHOW-ALL is non-nil,
6200 ;; LAST-REPEAT is the repeat closest to CURRENT.
6201 ;; Otherwise, LAST-REPEAT is equal to SCHEDULE.
6202 (last-repeat (org-agenda--timestamp-to-absolute
6203 s current 'past show-all (current-buffer) pos))
6204 (schedule (org-agenda--timestamp-to-absolute s current))
6205 (diff (- last-repeat current))
6206 (warntime (get-text-property (point) 'org-appt-warntime))
6207 (pastschedp (< schedule (org-today)))
6208 (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
6209 (suppress-delay
6210 (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
6211 (org-entry-get nil "DEADLINE"))))
6212 (cond
6213 ((not deadline) nil)
6214 ;; The current item has a deadline date, so
6215 ;; evaluate its delay time.
6216 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6217 ;; Use global delay time.
6218 (- org-agenda-skip-scheduled-delay-if-deadline))
6219 ((eq org-agenda-skip-scheduled-delay-if-deadline
6220 'post-deadline)
6221 ;; Set delay to no later than DEADLINE. If
6222 ;; DEADLINE has a repeater, compare last schedule
6223 ;; repeat and last deadline repeat.
6224 (min (- last-repeat
6225 (org-agenda--timestamp-to-absolute
6226 deadline current 'past show-all
6227 (current-buffer)
6228 (save-excursion
6229 (beginning-of-line)
6230 (1+ (search-forward org-deadline-string)))))
6231 org-scheduled-delay-days))
6232 (t 0))))
6233 (ddays
6234 (cond
6235 ;; Nullify delay when a repeater triggered already
6236 ;; and the delay is of the form --Xd.
6237 ((and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6238 (/= schedule last-repeat))
6240 (suppress-delay
6241 (let ((org-scheduled-delay-days suppress-delay))
6242 (org-get-wdays s t t)))
6243 (t (org-get-wdays s t)))))
6244 ;; Only show a scheduled item in the calendar if it is on or
6245 ;; past the current date. Skip it if it has been displayed
6246 ;; for more than `org-scheduled-past-days'.
6247 (unless (or (and (>= ddays 0) (= diff (- ddays)))
6248 (and (< (+ diff ddays) 0)
6249 (< (abs diff) org-scheduled-past-days)
6250 (and todayp (not org-agenda-only-exact-dates)))
6251 (and todayp
6252 habitp
6253 (bound-and-true-p org-habit-show-all-today)))
6254 (throw :skip nil))
6255 ;; Skip done habits, or tasks if
6256 ;; `org-agenda-skip-deadline-if-done' is non-nil or if it
6257 ;; was scheduled in the past anyway.
6258 (when (and donep
6259 (or org-agenda-skip-scheduled-if-done
6260 (/= schedule current)
6261 habitp))
6262 (throw :skip nil))
6263 ;; Skip entry if it already appears as a deadline, per
6264 ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
6265 ;; doesn't apply to habits.
6266 (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
6267 ((guard
6268 (or (not (assq (line-beginning-position 0) deadline-pos))
6269 habitp))
6270 nil)
6271 (`repeated-after-deadline
6272 (>= last-repeat
6273 (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.
6280 (when (and habitp
6281 (or (not (bound-and-true-p org-habit-show-habits))
6282 (and (not todayp)
6283 (bound-and-true-p
6284 org-habit-show-habits-only-for-today))))
6285 (throw :skip nil))
6286 (save-excursion
6287 (re-search-backward "^\\*+[ \t]+" nil t)
6288 (goto-char (match-end 0))
6289 (let* ((category (org-get-category))
6290 (inherited-tags
6291 (or (eq org-agenda-show-inherited-tags 'always)
6292 (and (listp org-agenda-show-inherited-tags)
6293 (memq 'agenda org-agenda-show-inherited-tags))
6294 (and (eq org-agenda-show-inherited-tags t)
6295 (or (eq org-agenda-use-tag-inheritance t)
6296 (memq 'agenda
6297 org-agenda-use-tag-inheritance)))))
6298 (tags (org-get-tags-at nil (not inherited-tags)))
6299 (level
6300 (make-string (org-reduced-level (org-outline-level)) ?\s))
6301 (head (buffer-substring (point) (line-end-position)))
6302 (timestr
6303 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6304 (concat (substring s (match-beginning 1)) " ")
6305 'time))
6306 (item (org-agenda-format-item
6307 ;; For past scheduled dates, make sure to
6308 ;; report time difference since SCHEDULE,
6309 ;; not since closest repeater.
6310 (let ((diff (if (< (org-today) current) diff
6311 (- schedule current))))
6312 (if (= diff 0) (car org-agenda-scheduled-leaders)
6313 (format (nth 1 org-agenda-scheduled-leaders)
6314 (- 1 diff))))
6315 head level category tags
6316 (and (= diff 0) timestr)
6317 nil habitp))
6318 (face (cond ((and (not habitp) pastschedp)
6319 'org-scheduled-previously)
6320 (todayp 'org-scheduled-today)
6321 (t 'org-scheduled)))
6322 (habitp (and habitp (org-habit-parse-todo))))
6323 (org-add-props item props
6324 'undone-face face
6325 'face (if donep 'org-agenda-done face)
6326 'org-marker (org-agenda-new-marker pos)
6327 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
6328 'type (if pastschedp "past-scheduled" "scheduled")
6329 'date (if pastschedp schedule date)
6330 'ts-date schedule
6331 'warntime warntime
6332 'level level
6333 'priority (if habitp (org-habit-get-priority habitp)
6334 (+ 94 (- 5 diff) (org-get-priority item)))
6335 'org-habit-p habitp
6336 'todo-state todo-state)
6337 (push item scheduled-items))))))
6338 (nreverse scheduled-items)))
6340 (defun org-agenda-get-blocks ()
6341 "Return the date-range information for agenda display."
6342 (let* ((props (list 'face nil
6343 'org-not-done-regexp org-not-done-regexp
6344 'org-todo-regexp org-todo-regexp
6345 'org-complex-heading-regexp org-complex-heading-regexp
6346 'mouse-face 'highlight
6347 'help-echo
6348 (format "mouse-2 or RET jump to org file %s"
6349 (abbreviate-file-name buffer-file-name))))
6350 (regexp org-tr-regexp)
6351 (d0 (calendar-absolute-from-gregorian date))
6352 marker hdmarker ee txt d1 d2 s1 s2 category
6353 level todo-state tags pos head donep inherited-tags)
6354 (goto-char (point-min))
6355 (while (re-search-forward regexp nil t)
6356 (catch :skip
6357 (org-agenda-skip)
6358 (setq pos (point))
6359 (let ((start-time (match-string 1))
6360 (end-time (match-string 2)))
6361 (setq s1 (match-string 1)
6362 s2 (match-string 2)
6363 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6364 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6365 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6366 ;; Only allow days between the limits, because the normal
6367 ;; date stamps will catch the limits.
6368 (save-excursion
6369 (setq todo-state (org-get-todo-state))
6370 (setq donep (member todo-state org-done-keywords))
6371 (if (and donep org-agenda-skip-timestamp-if-done)
6372 (throw :skip t))
6373 (setq marker (org-agenda-new-marker (point))
6374 category (org-get-category))
6375 (if (not (re-search-backward org-outline-regexp-bol nil t))
6376 (throw :skip nil)
6377 (goto-char (match-beginning 0))
6378 (setq hdmarker (org-agenda-new-marker (point))
6379 inherited-tags
6380 (or (eq org-agenda-show-inherited-tags 'always)
6381 (and (listp org-agenda-show-inherited-tags)
6382 (memq 'agenda org-agenda-show-inherited-tags))
6383 (and (eq org-agenda-show-inherited-tags t)
6384 (or (eq org-agenda-use-tag-inheritance t)
6385 (memq 'agenda org-agenda-use-tag-inheritance))))
6387 tags (org-get-tags-at nil (not inherited-tags)))
6388 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6389 (looking-at "\\*+[ \t]+\\(.*\\)")
6390 (setq head (match-string 1))
6391 (let ((remove-re
6392 (if org-agenda-remove-timeranges-from-blocks
6393 (concat
6394 "<" (regexp-quote s1) ".*?>"
6395 "--"
6396 "<" (regexp-quote s2) ".*?>")
6397 nil)))
6398 (setq txt (org-agenda-format-item
6399 (format
6400 (nth (if (= d1 d2) 0 1)
6401 org-agenda-timerange-leaders)
6402 (1+ (- d0 d1)) (1+ (- d2 d1)))
6403 head level category tags
6404 (cond ((and (= d1 d0) (= d2 d0))
6405 (concat "<" start-time ">--<" end-time ">"))
6406 ((= d1 d0)
6407 (concat "<" start-time ">"))
6408 ((= d2 d0)
6409 (concat "<" end-time ">")))
6410 remove-re))))
6411 (org-add-props txt props
6412 'org-marker marker 'org-hd-marker hdmarker
6413 'type "block" 'date date
6414 'level level
6415 'todo-state todo-state
6416 'priority (org-get-priority txt))
6417 (push txt ee))))
6418 (goto-char pos)))
6419 ;; Sort the entries by expiration date.
6420 (nreverse ee)))
6422 ;;; Agenda presentation and sorting
6424 (defvar org-prefix-has-time nil
6425 "A flag, set by `org-compile-prefix-format'.
6426 The flag is set if the currently compiled format contains a `%t'.")
6427 (defvar org-prefix-has-tag nil
6428 "A flag, set by `org-compile-prefix-format'.
6429 The flag is set if the currently compiled format contains a `%T'.")
6430 (defvar org-prefix-has-effort nil
6431 "A flag, set by `org-compile-prefix-format'.
6432 The flag is set if the currently compiled format contains a `%e'.")
6433 (defvar org-prefix-has-breadcrumbs nil
6434 "A flag, set by `org-compile-prefix-format'.
6435 The flag is set if the currently compiled format contains a `%b'.")
6436 (defvar org-prefix-category-length nil
6437 "Used by `org-compile-prefix-format' to remember the category field width.")
6438 (defvar org-prefix-category-max-length nil
6439 "Used by `org-compile-prefix-format' to remember the category field width.")
6441 (defun org-agenda-get-category-icon (category)
6442 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6443 (dolist (entry org-agenda-category-icon-alist)
6444 (when (org-string-match-p (car entry) category)
6445 (if (listp (cadr entry))
6446 (return (cadr entry))
6447 (return (apply 'create-image (cdr entry)))))))
6449 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6450 remove-re habitp)
6451 "Format TXT to be inserted into the agenda buffer.
6452 In particular, add the prefix and corresponding text properties.
6454 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6455 LEVEL may be a string to replace the `%l' specifier.
6456 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6457 category taken from local variable or file name. It will replace the `%c'
6458 specifier in the format.
6459 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6460 TXT for sorting of this entry, and for the `%t' specifier in the format.
6461 When DOTIME is a string, this string is searched for a time before TXT is.
6462 TAGS can be the tags of the headline.
6463 Any match of REMOVE-RE will be removed from TXT."
6464 ;; We keep the org-prefix-* variable values along with a compiled
6465 ;; formatter, so that multiple agendas existing at the same time do
6466 ;; not step on each other toes.
6468 ;; It was inconvenient to make these variables buffer local in
6469 ;; Agenda buffers, because this function expects to be called with
6470 ;; the buffer where item comes from being current, and not agenda
6471 ;; buffer
6472 (let* ((bindings (car org-prefix-format-compiled))
6473 (formatter (cadr org-prefix-format-compiled)))
6474 (loop for (var value) in bindings
6475 do (set var value))
6476 (save-match-data
6477 ;; Diary entries sometimes have extra whitespace at the beginning
6478 (setq txt (org-trim txt))
6480 ;; Fix the tags part in txt
6481 (setq txt (org-agenda-fix-displayed-tags
6482 txt tags
6483 org-agenda-show-inherited-tags
6484 org-agenda-hide-tags-regexp))
6486 (let* ((category (or category
6487 (if buffer-file-name
6488 (file-name-sans-extension
6489 (file-name-nondirectory buffer-file-name))
6490 "")))
6491 (category-icon (org-agenda-get-category-icon category))
6492 (category-icon (if category-icon
6493 (propertize " " 'display category-icon)
6494 ""))
6495 (effort (and (not (string= txt ""))
6496 (get-text-property 1 'effort txt)))
6497 ;; time, tag, effort are needed for the eval of the prefix format
6498 (tag (if tags (nth (1- (length tags)) tags) ""))
6499 time
6500 (ts (if dotime (concat
6501 (if (stringp dotime) dotime "")
6502 (and org-agenda-search-headline-for-time txt))))
6503 (time-of-day (and dotime (org-get-time-of-day ts)))
6504 stamp plain s0 s1 s2 rtn srp l
6505 duration breadcrumbs)
6506 (and (derived-mode-p 'org-mode) buffer-file-name
6507 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6508 (when (and dotime time-of-day)
6509 ;; Extract starting and ending time and move them to prefix
6510 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6511 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6512 (setq s0 (match-string 0 ts)
6513 srp (and stamp (match-end 3))
6514 s1 (match-string (if plain 1 2) ts)
6515 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6517 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6518 ;; them, we might want to remove them there to avoid duplication.
6519 ;; The user can turn this off with a variable.
6520 (if (and org-prefix-has-time
6521 org-agenda-remove-times-when-in-prefix (or stamp plain)
6522 (string-match (concat (regexp-quote s0) " *") txt)
6523 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6524 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6525 (= (match-beginning 0) 0)
6527 (setq txt (replace-match "" nil nil txt))))
6528 ;; Normalize the time(s) to 24 hour
6529 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6530 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6532 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6533 (let (org-time-clocksum-use-effort-durations)
6534 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6535 (setq s2
6536 (org-minutes-to-clocksum-string
6537 (+ (org-hh:mm-string-to-minutes s1)
6538 org-agenda-default-appointment-duration)))))
6540 ;; Compute the duration
6541 (when s2
6542 (setq duration (- (org-hh:mm-string-to-minutes s2)
6543 (org-hh:mm-string-to-minutes s1)))))
6545 (when (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6546 ;; Tags are in the string
6547 (if (or (eq org-agenda-remove-tags t)
6548 (and org-agenda-remove-tags
6549 org-prefix-has-tag))
6550 (setq txt (replace-match "" t t txt))
6551 (setq txt (replace-match
6552 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6553 (match-string 2 txt))
6554 t t txt))))
6556 (when remove-re
6557 (while (string-match remove-re txt)
6558 (setq txt (replace-match "" t t txt))))
6560 ;; Set org-heading property on `txt' to mark the start of the
6561 ;; heading.
6562 (add-text-properties 0 (length txt) '(org-heading t) txt)
6564 ;; Prepare the variables needed in the eval of the compiled format
6565 (if org-prefix-has-breadcrumbs
6566 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6567 (let ((s (org-display-outline-path nil nil "->" t)))
6568 (if (eq "" s) "" (concat s "->"))))))
6569 (setq time (cond (s2 (concat
6570 (org-agenda-time-of-day-to-ampm-maybe s1)
6571 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6572 (if org-agenda-timegrid-use-ampm " ")))
6573 (s1 (concat
6574 (org-agenda-time-of-day-to-ampm-maybe s1)
6575 (if org-agenda-timegrid-use-ampm
6576 "........ "
6577 "......")))
6578 (t ""))
6579 extra (or (and (not habitp) extra) "")
6580 category (if (symbolp category) (symbol-name category) category)
6581 level (or level ""))
6582 (if (string-match org-bracket-link-regexp category)
6583 (progn
6584 (setq l (if (match-end 3)
6585 (- (match-end 3) (match-beginning 3))
6586 (- (match-end 1) (match-beginning 1))))
6587 (when (< l (or org-prefix-category-length 0))
6588 (setq category (copy-sequence category))
6589 (org-add-props category nil
6590 'extra-space (make-string
6591 (- org-prefix-category-length l 1) ?\ ))))
6592 (if (and org-prefix-category-max-length
6593 (>= (length category) org-prefix-category-max-length))
6594 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6595 ;; Evaluate the compiled format
6596 (setq rtn (concat (eval formatter) txt))
6598 ;; And finally add the text properties
6599 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6600 (org-add-props rtn nil
6601 'org-category category
6602 'tags (mapcar 'org-downcase-keep-props tags)
6603 'org-highest-priority org-highest-priority
6604 'org-lowest-priority org-lowest-priority
6605 'time-of-day time-of-day
6606 'duration duration
6607 'breadcrumbs breadcrumbs
6608 'txt txt
6609 'level level
6610 'time time
6611 'extra extra
6612 'format org-prefix-format-compiled
6613 'dotime dotime)))))
6615 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6616 "Remove tags string from TXT, and add a modified list of tags.
6617 The modified list may contain inherited tags, and tags matched by
6618 `org-agenda-hide-tags-regexp' will be removed."
6619 (when (or add-inherited hide-re)
6620 (if (string-match "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" txt)
6621 (setq txt (substring txt 0 (match-beginning 0))))
6622 (setq tags
6623 (delq nil
6624 (mapcar (lambda (tg)
6625 (if (or (and hide-re (string-match hide-re tg))
6626 (and (not add-inherited)
6627 (get-text-property 0 'inherited tg)))
6629 tg))
6630 tags)))
6631 (when tags
6632 (let ((have-i (get-text-property 0 'inherited (car tags)))
6634 (setq txt (concat txt " :"
6635 (mapconcat
6636 (lambda (x)
6637 (setq i (get-text-property 0 'inherited x))
6638 (if (and have-i (not i))
6639 (progn
6640 (setq have-i nil)
6641 (concat ":" x))
6643 tags ":")
6644 (if have-i "::" ":"))))))
6645 txt)
6647 (defun org-downcase-keep-props (s)
6648 (let ((props (text-properties-at 0 s)))
6649 (setq s (downcase s))
6650 (add-text-properties 0 (length s) props s)
6653 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6655 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6656 "Add a time-grid for agenda items which need it.
6658 LIST is the list of agenda items formatted by `org-agenda-list'.
6659 NDAYS is the span of the current agenda view.
6660 TODAYP is t when the current agenda view is on today."
6661 (catch 'exit
6662 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6663 ((and todayp (member 'today (car org-agenda-time-grid))))
6664 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6665 ((member 'weekly (car org-agenda-time-grid)))
6666 (t (throw 'exit list)))
6667 (let* ((have (delq nil (mapcar
6668 (lambda (x) (get-text-property 1 'time-of-day x))
6669 list)))
6670 (string (nth 1 org-agenda-time-grid))
6671 (gridtimes (nth 2 org-agenda-time-grid))
6672 (req (car org-agenda-time-grid))
6673 (remove (member 'remove-match req))
6674 new time)
6675 (if (and (member 'require-timed req) (not have))
6676 ;; don't show empty grid
6677 (throw 'exit list))
6678 (while (setq time (pop gridtimes))
6679 (unless (and remove (member time have))
6680 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6681 (push (org-agenda-format-item
6682 nil string nil "" nil
6683 (concat (substring time 0 -2) ":" (substring time -2)))
6684 new)
6685 (put-text-property
6686 2 (length (car new)) 'face 'org-time-grid (car new))))
6687 (when (and todayp org-agenda-show-current-time-in-grid)
6688 (push (org-agenda-format-item
6689 nil org-agenda-current-time-string nil "" nil
6690 (format-time-string "%H:%M "))
6691 new)
6692 (put-text-property
6693 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6695 (if (member 'time-up org-agenda-sorting-strategy-selected)
6696 (append new list)
6697 (append list new)))))
6699 (defun org-compile-prefix-format (key)
6700 "Compile the prefix format into a Lisp form that can be evaluated.
6701 The resulting form and associated variable bindings is returned
6702 and stored in the variable `org-prefix-format-compiled'."
6703 (setq org-prefix-has-time nil
6704 org-prefix-has-tag nil
6705 org-prefix-category-length nil
6706 org-prefix-has-effort nil
6707 org-prefix-has-breadcrumbs nil)
6708 (let ((s (cond
6709 ((stringp org-agenda-prefix-format)
6710 org-agenda-prefix-format)
6711 ((assq key org-agenda-prefix-format)
6712 (cdr (assq key org-agenda-prefix-format)))
6713 (t " %-12:c%?-12t% s")))
6714 (start 0)
6715 varform vars var e c f opt)
6716 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6717 s start)
6718 (setq var (or (cdr (assoc (match-string 4 s)
6719 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6720 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6721 'eval)
6722 c (or (match-string 3 s) "")
6723 opt (match-beginning 1)
6724 start (1+ (match-beginning 0)))
6725 (if (equal var 'time) (setq org-prefix-has-time t))
6726 (if (equal var 'tag) (setq org-prefix-has-tag t))
6727 (if (equal var 'effort) (setq org-prefix-has-effort t))
6728 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6729 (setq f (concat "%" (match-string 2 s) "s"))
6730 (when (equal var 'category)
6731 (setq org-prefix-category-length
6732 (floor (abs (string-to-number (match-string 2 s)))))
6733 (setq org-prefix-category-max-length
6734 (let ((x (match-string 2 s)))
6735 (save-match-data
6736 (if (string-match "\\.[0-9]+" x)
6737 (string-to-number (substring (match-string 0 x) 1)))))))
6738 (if (eq var 'eval)
6739 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6740 (if opt
6741 (setq varform
6742 `(if (or (equal "" ,var) (equal nil ,var))
6744 (format ,f (concat ,var ,c))))
6745 (setq varform
6746 `(format ,f (if (or (equal ,var "")
6747 (equal ,var nil)) ""
6748 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6749 (setq s (replace-match "%s" t nil s))
6750 (push varform vars))
6751 (setq vars (nreverse vars))
6752 (with-current-buffer (or org-agenda-buffer (current-buffer))
6753 (setq org-prefix-format-compiled
6754 (list
6755 `((org-prefix-has-time ,org-prefix-has-time)
6756 (org-prefix-has-tag ,org-prefix-has-tag)
6757 (org-prefix-category-length ,org-prefix-category-length)
6758 (org-prefix-has-effort ,org-prefix-has-effort)
6759 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6760 `(format ,s ,@vars))))))
6762 (defun org-set-sorting-strategy (key)
6763 (if (symbolp (car org-agenda-sorting-strategy))
6764 ;; the old format
6765 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6766 (setq org-agenda-sorting-strategy-selected
6767 (or (cdr (assq key org-agenda-sorting-strategy))
6768 (cdr (assq 'agenda org-agenda-sorting-strategy))
6769 '(time-up category-keep priority-down)))))
6771 (defun org-get-time-of-day (s &optional string mod24)
6772 "Check string S for a time of day.
6773 If found, return it as a military time number between 0 and 2400.
6774 If not found, return nil.
6775 The optional STRING argument forces conversion into a 5 character wide string
6776 HH:MM."
6777 (save-match-data
6778 (when
6779 (and
6780 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6781 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6782 (not (eq (get-text-property 1 'face s) 'org-link)))
6783 (let* ((h (string-to-number (match-string 1 s)))
6784 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6785 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6786 (am-p (equal ampm "am"))
6787 (h1 (cond ((not ampm) h)
6788 ((= h 12) (if am-p 0 12))
6789 (t (+ h (if am-p 0 12)))))
6790 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6791 (mod h1 24) h1))
6792 (t0 (+ (* 100 h2) m))
6793 (t1 (concat (if (>= h1 24) "+" " ")
6794 (if (and org-agenda-time-leading-zero
6795 (< t0 1000)) "0" "")
6796 (if (< t0 100) "0" "")
6797 (if (< t0 10) "0" "")
6798 (int-to-string t0))))
6799 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6801 (defvar org-agenda-before-sorting-filter-function nil
6802 "Function to be applied to agenda items prior to sorting.
6803 Prior to sorting also means just before they are inserted into the agenda.
6805 To aid sorting, you may revisit the original entries and add more text
6806 properties which will later be used by the sorting functions.
6808 The function should take a string argument, an agenda line.
6809 It has access to the text properties in that line, which contain among
6810 other things, the property `org-hd-marker' that points to the entry
6811 where the line comes from. Note that not all lines going into the agenda
6812 have this property, only most.
6814 The function should return the modified string. It is probably best
6815 to ONLY change text properties.
6817 You can also use this function as a filter, by returning nil for lines
6818 you don't want to have in the agenda at all. For this application, you
6819 could bind the variable in the options section of a custom command.")
6821 (defun org-agenda-finalize-entries (list &optional type)
6822 "Sort, limit and concatenate the LIST of agenda items.
6823 The optional argument TYPE tells the agenda type."
6824 (let ((max-effort (cond ((listp org-agenda-max-effort)
6825 (cdr (assoc type org-agenda-max-effort)))
6826 (t org-agenda-max-effort)))
6827 (max-todo (cond ((listp org-agenda-max-todos)
6828 (cdr (assoc type org-agenda-max-todos)))
6829 (t org-agenda-max-todos)))
6830 (max-tags (cond ((listp org-agenda-max-tags)
6831 (cdr (assoc type org-agenda-max-tags)))
6832 (t org-agenda-max-tags)))
6833 (max-entries (cond ((listp org-agenda-max-entries)
6834 (cdr (assoc type org-agenda-max-entries)))
6835 (t org-agenda-max-entries))))
6836 (when org-agenda-before-sorting-filter-function
6837 (setq list
6838 (delq nil
6839 (mapcar
6840 org-agenda-before-sorting-filter-function list))))
6841 (setq list (mapcar 'org-agenda-highlight-todo list)
6842 list (mapcar 'identity (sort list 'org-entries-lessp)))
6843 (when max-effort
6844 (setq list (org-agenda-limit-entries
6845 list 'effort-minutes max-effort
6846 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6847 32767 -1))))))
6848 (when max-todo
6849 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6850 (when max-tags
6851 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6852 (when max-entries
6853 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6854 (mapconcat 'identity list "\n")))
6856 (defun org-agenda-limit-entries (list prop limit &optional fn)
6857 "Limit the number of agenda entries."
6858 (let ((include (and limit (< limit 0))))
6859 (if limit
6860 (let ((fun (or fn (lambda (p) (if p 1))))
6861 (lim 0))
6862 (delq nil
6863 (mapcar
6864 (lambda (e)
6865 (let ((pval (funcall
6866 fun (get-text-property (1- (length e))
6867 prop e))))
6868 (if pval (setq lim (+ lim pval)))
6869 (cond ((and pval (<= lim (abs limit))) e)
6870 ((and include (not pval)) e))))
6871 list)))
6872 list)))
6874 (defun org-agenda-limit-interactively (remove)
6875 "In agenda, interactively limit entries to various maximums."
6876 (interactive "P")
6877 (if remove
6878 (progn (setq org-agenda-max-entries nil
6879 org-agenda-max-todos nil
6880 org-agenda-max-tags nil
6881 org-agenda-max-effort nil)
6882 (org-agenda-redo))
6883 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6884 (msg (cond ((= max ?E) "How many minutes? ")
6885 ((= max ?e) "How many entries? ")
6886 ((= max ?t) "How many TODO entries? ")
6887 ((= max ?T) "How many tagged entries? ")
6888 (t (user-error "Wrong input"))))
6889 (num (string-to-number (read-from-minibuffer msg))))
6890 (cond ((equal max ?e)
6891 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6892 ((equal max ?t)
6893 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6894 ((equal max ?T)
6895 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6896 ((equal max ?E)
6897 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6898 (org-agenda-fit-window-to-buffer))
6900 (defun org-agenda-highlight-todo (x)
6901 (let ((org-done-keywords org-done-keywords-for-agenda)
6902 (case-fold-search nil)
6904 (if (eq x 'line)
6905 (save-excursion
6906 (beginning-of-line 1)
6907 (setq re (org-get-at-bol 'org-todo-regexp))
6908 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6909 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6910 (add-text-properties (match-beginning 0) (match-end 1)
6911 (list 'face (org-get-todo-face 1)))
6912 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6913 (delete-region (match-beginning 1) (1- (match-end 0)))
6914 (goto-char (match-beginning 1))
6915 (insert (format org-agenda-todo-keyword-format s)))))
6916 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6917 (setq re (get-text-property 0 'org-todo-regexp x))
6918 (when (and re
6919 ;; Test `pl' because if there's no heading content,
6920 ;; there's no point matching to highlight. Note
6921 ;; that if we didn't test `pl' first, and there
6922 ;; happened to be no keyword from `org-todo-regexp'
6923 ;; on this heading line, then the `equal' comparison
6924 ;; afterwards would spuriously succeed in the case
6925 ;; where `pl' is nil -- causing an args-out-of-range
6926 ;; error when we try to add text properties to text
6927 ;; that isn't there.
6929 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6930 x pl) pl))
6931 (add-text-properties
6932 (or (match-end 1) (match-end 0)) (match-end 0)
6933 (list 'face (org-get-todo-face (match-string 2 x)))
6935 (when (match-end 1)
6936 (setq x (concat (substring x 0 (match-end 1))
6937 (format org-agenda-todo-keyword-format
6938 (match-string 2 x))
6939 (org-add-props " " (text-properties-at 0 x))
6940 (substring x (match-end 3)))))))
6941 x)))
6943 (defsubst org-cmp-values (a b property)
6944 "Compare the numeric value of text PROPERTY for string A and B."
6945 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6946 (pb (or (get-text-property (1- (length b)) property b) 0)))
6947 (cond ((> pa pb) +1)
6948 ((< pa pb) -1))))
6950 (defsubst org-cmp-effort (a b)
6951 "Compare the effort values of string A and B."
6952 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6953 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6954 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6955 (cond ((> ea eb) +1)
6956 ((< ea eb) -1))))
6958 (defsubst org-cmp-category (a b)
6959 "Compare the string values of categories of strings A and B."
6960 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6961 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6962 (cond ((string-lessp ca cb) -1)
6963 ((string-lessp cb ca) +1))))
6965 (defsubst org-cmp-todo-state (a b)
6966 "Compare the todo states of strings A and B."
6967 (let* ((ma (or (get-text-property 1 'org-marker a)
6968 (get-text-property 1 'org-hd-marker a)))
6969 (mb (or (get-text-property 1 'org-marker b)
6970 (get-text-property 1 'org-hd-marker b)))
6971 (fa (and ma (marker-buffer ma)))
6972 (fb (and mb (marker-buffer mb)))
6973 (todo-kwds
6974 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6975 (and fb (with-current-buffer fb org-todo-keywords-1))))
6976 (ta (or (get-text-property 1 'todo-state a) ""))
6977 (tb (or (get-text-property 1 'todo-state b) ""))
6978 (la (- (length (member ta todo-kwds))))
6979 (lb (- (length (member tb todo-kwds))))
6980 (donepa (member ta org-done-keywords-for-agenda))
6981 (donepb (member tb org-done-keywords-for-agenda)))
6982 (cond ((and donepa (not donepb)) -1)
6983 ((and (not donepa) donepb) +1)
6984 ((< la lb) -1)
6985 ((< lb la) +1))))
6987 (defsubst org-cmp-alpha (a b)
6988 "Compare the headlines, alphabetically."
6989 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6990 (plb (text-property-any 0 (length b) 'org-heading t b))
6991 (ta (and pla (substring a pla)))
6992 (tb (and plb (substring b plb))))
6993 (when pla
6994 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6995 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6996 (setq ta (substring ta (match-end 0))))
6997 (setq ta (downcase ta)))
6998 (when plb
6999 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
7000 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
7001 (setq tb (substring tb (match-end 0))))
7002 (setq tb (downcase tb)))
7003 (cond ((not ta) +1)
7004 ((not tb) -1)
7005 ((string-lessp ta tb) -1)
7006 ((string-lessp tb ta) +1))))
7008 (defsubst org-cmp-tag (a b)
7009 "Compare the string values of the first tags of A and B."
7010 (let ((ta (car (last (get-text-property 1 'tags a))))
7011 (tb (car (last (get-text-property 1 'tags b)))))
7012 (cond ((not ta) +1)
7013 ((not tb) -1)
7014 ((string-lessp ta tb) -1)
7015 ((string-lessp tb ta) +1))))
7017 (defsubst org-cmp-time (a b)
7018 "Compare the time-of-day values of strings A and B."
7019 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7020 (ta (or (get-text-property 1 'time-of-day a) def))
7021 (tb (or (get-text-property 1 'time-of-day b) def)))
7022 (cond ((< ta tb) -1)
7023 ((< tb ta) +1))))
7025 (defsubst org-cmp-ts (a b type)
7026 "Compare the timestamps values of entries A and B.
7027 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7028 \"timestamp_ia\", compare within each of these type. When TYPE
7029 is the empty string, compare all timestamps without respect of
7030 their type."
7031 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7032 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7033 (get-text-property 1 'ts-date a))
7034 def))
7035 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7036 (get-text-property 1 'ts-date b))
7037 def)))
7038 (cond ((< ta tb) -1)
7039 ((< tb ta) +1))))
7041 (defsubst org-cmp-habit-p (a b)
7042 "Compare the todo states of strings A and B."
7043 (let ((ha (get-text-property 1 'org-habit-p a))
7044 (hb (get-text-property 1 'org-habit-p b)))
7045 (cond ((and ha (not hb)) -1)
7046 ((and (not ha) hb) +1))))
7048 (defun org-entries-lessp (a b)
7049 "Predicate for sorting agenda entries."
7050 ;; The following variables will be used when the form is evaluated.
7051 ;; So even though the compiler complains, keep them.
7052 (let* ((ss org-agenda-sorting-strategy-selected)
7053 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7054 (org-cmp-ts a b "")))
7055 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7056 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7057 (org-cmp-ts a b "scheduled")))
7058 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7059 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7060 (org-cmp-ts a b "deadline")))
7061 (deadline-down (if deadline-up (- deadline-up) nil))
7062 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7063 (org-cmp-ts a b "timestamp_ia")))
7064 (tsia-down (if tsia-up (- tsia-up) nil))
7065 (ts-up (and (org-em 'ts-up 'ts-down ss)
7066 (org-cmp-ts a b "timestamp")))
7067 (ts-down (if ts-up (- ts-up) nil))
7068 (time-up (and (org-em 'time-up 'time-down ss)
7069 (org-cmp-time a b)))
7070 (time-down (if time-up (- time-up) nil))
7071 (stats-up (and (org-em 'stats-up 'stats-down ss)
7072 (org-cmp-values a b 'org-stats)))
7073 (stats-down (if stats-up (- stats-up) nil))
7074 (priority-up (and (org-em 'priority-up 'priority-down ss)
7075 (org-cmp-values a b 'priority)))
7076 (priority-down (if priority-up (- priority-up) nil))
7077 (effort-up (and (org-em 'effort-up 'effort-down ss)
7078 (org-cmp-effort a b)))
7079 (effort-down (if effort-up (- effort-up) nil))
7080 (category-up (and (or (org-em 'category-up 'category-down ss)
7081 (memq 'category-keep ss))
7082 (org-cmp-category a b)))
7083 (category-down (if category-up (- category-up) nil))
7084 (category-keep (if category-up +1 nil))
7085 (tag-up (and (org-em 'tag-up 'tag-down ss)
7086 (org-cmp-tag a b)))
7087 (tag-down (if tag-up (- tag-up) nil))
7088 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7089 (org-cmp-todo-state a b)))
7090 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7091 (habit-up (and (org-em 'habit-up 'habit-down ss)
7092 (org-cmp-habit-p a b)))
7093 (habit-down (if habit-up (- habit-up) nil))
7094 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7095 (org-cmp-alpha a b)))
7096 (alpha-down (if alpha-up (- alpha-up) nil))
7097 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7098 user-defined-up user-defined-down)
7099 (if (and need-user-cmp org-agenda-cmp-user-defined
7100 (functionp org-agenda-cmp-user-defined))
7101 (setq user-defined-up
7102 (funcall org-agenda-cmp-user-defined a b)
7103 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7104 (cdr (assoc
7105 (eval (cons 'or org-agenda-sorting-strategy-selected))
7106 '((-1 . t) (1 . nil) (nil . nil))))))
7108 ;;; Agenda restriction lock
7110 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7111 "Overlay to mark the headline to which agenda commands are restricted.")
7112 (overlay-put org-agenda-restriction-lock-overlay
7113 'face 'org-agenda-restriction-lock)
7114 (overlay-put org-agenda-restriction-lock-overlay
7115 'help-echo "Agendas are currently limited to this subtree.")
7116 (delete-overlay org-agenda-restriction-lock-overlay)
7118 ;;;###autoload
7119 (defun org-agenda-set-restriction-lock (&optional type)
7120 "Set restriction lock for agenda, to current subtree or file.
7121 Restriction will be the file if TYPE is `file', or if type is the
7122 universal prefix \\='(4), or if the cursor is before the first headline
7123 in the file. Otherwise, restriction will be to the current subtree."
7124 (interactive "P")
7125 (org-agenda-remove-restriction-lock 'noupdate)
7126 (and (equal type '(4)) (setq type 'file))
7127 (setq type (cond
7128 (type type)
7129 ((org-at-heading-p) 'subtree)
7130 ((condition-case nil (org-back-to-heading t) (error nil))
7131 'subtree)
7132 (t 'file)))
7133 (if (eq type 'subtree)
7134 (progn
7135 (setq org-agenda-restrict (current-buffer))
7136 (setq org-agenda-overriding-restriction 'subtree)
7137 (put 'org-agenda-files 'org-restrict
7138 (list (buffer-file-name (buffer-base-buffer))))
7139 (org-back-to-heading t)
7140 (move-overlay org-agenda-restriction-lock-overlay
7141 (point)
7142 (if org-agenda-restriction-lock-highlight-subtree
7143 (save-excursion (org-end-of-subtree t t) (point))
7144 (point-at-eol)))
7145 (move-marker org-agenda-restrict-begin (point))
7146 (move-marker org-agenda-restrict-end
7147 (save-excursion (org-end-of-subtree t t)))
7148 (message "Locking agenda restriction to subtree"))
7149 (put 'org-agenda-files 'org-restrict
7150 (list (buffer-file-name (buffer-base-buffer))))
7151 (setq org-agenda-restrict nil)
7152 (setq org-agenda-overriding-restriction 'file)
7153 (move-marker org-agenda-restrict-begin nil)
7154 (move-marker org-agenda-restrict-end nil)
7155 (message "Locking agenda restriction to file"))
7156 (setq current-prefix-arg nil)
7157 (org-agenda-maybe-redo))
7159 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7160 "Remove the agenda restriction lock."
7161 (interactive "P")
7162 (delete-overlay org-agenda-restriction-lock-overlay)
7163 (delete-overlay org-speedbar-restriction-lock-overlay)
7164 (setq org-agenda-overriding-restriction nil)
7165 (setq org-agenda-restrict nil)
7166 (put 'org-agenda-files 'org-restrict nil)
7167 (move-marker org-agenda-restrict-begin nil)
7168 (move-marker org-agenda-restrict-end nil)
7169 (setq current-prefix-arg nil)
7170 (message "Agenda restriction lock removed")
7171 (or noupdate (org-agenda-maybe-redo)))
7173 (defun org-agenda-maybe-redo ()
7174 "If there is any window showing the agenda view, update it."
7175 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7176 org-agenda-buffer-name)
7178 (w0 (selected-window)))
7179 (when w
7180 (select-window w)
7181 (org-agenda-redo)
7182 (select-window w0)
7183 (if org-agenda-overriding-restriction
7184 (message "Agenda view shifted to new %s restriction"
7185 org-agenda-overriding-restriction)
7186 (message "Agenda restriction lock removed")))))
7188 ;;; Agenda commands
7190 (defun org-agenda-check-type (error &rest types)
7191 "Check if agenda buffer is of allowed type.
7192 If ERROR is non-nil, throw an error, otherwise just return nil.
7193 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7194 (if (not org-agenda-type)
7195 (error "No Org agenda currently displayed")
7196 (if (memq org-agenda-type types)
7198 (if error
7199 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7200 nil))))
7202 (defun org-agenda-Quit ()
7203 "Exit the agenda, killing the agenda buffer.
7204 Like `org-agenda-quit', but kill the buffer even when
7205 `org-agenda-sticky' is non-nil."
7206 (interactive)
7207 (org-agenda--quit))
7209 (defun org-agenda-quit ()
7210 "Exit the agenda.
7212 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7213 instead of killing it.
7215 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7216 the pre-agenda window configuration.
7218 When column view is active, exit column view instead of the
7219 agenda."
7220 (interactive)
7221 (org-agenda--quit org-agenda-sticky))
7223 (defun org-agenda--quit (&optional bury)
7224 (if org-agenda-columns-active
7225 (org-columns-quit)
7226 (let ((wconf org-agenda-pre-window-conf)
7227 (buf (current-buffer))
7228 (org-agenda-last-indirect-window
7229 (and (eq org-indirect-buffer-display 'other-window)
7230 org-agenda-last-indirect-buffer
7231 (get-buffer-window org-agenda-last-indirect-buffer))))
7232 (cond
7233 ((eq org-agenda-window-setup 'other-frame)
7234 (delete-frame))
7235 ((and org-agenda-restore-windows-after-quit
7236 wconf)
7237 ;; Maybe restore the pre-agenda window configuration. Reset
7238 ;; `org-agenda-pre-window-conf' before running
7239 ;; `set-window-configuration', which loses the current buffer.
7240 (setq org-agenda-pre-window-conf nil)
7241 (set-window-configuration wconf))
7243 (when org-agenda-last-indirect-window
7244 (delete-window org-agenda-last-indirect-window))
7245 (and (not (eq org-agenda-window-setup 'current-window))
7246 (not (one-window-p))
7247 (delete-window))))
7248 (if bury
7249 ;; Set the agenda buffer as the current buffer instead of
7250 ;; passing it as an argument to `bury-buffer' so that
7251 ;; `bury-buffer' removes it from the window.
7252 (with-current-buffer buf
7253 (bury-buffer))
7254 (kill-buffer buf)
7255 (setq org-agenda-archives-mode nil
7256 org-agenda-buffer nil)))))
7258 (defun org-agenda-exit ()
7259 "Exit the agenda, killing Org buffers loaded by the agenda.
7260 Like `org-agenda-Quit', but kill any buffers that were created by
7261 the agenda. Org buffers visited directly by the user will not be
7262 touched. Also, exit the agenda even if it is in column view."
7263 (interactive)
7264 (when org-agenda-columns-active
7265 (org-columns-quit))
7266 (org-release-buffers org-agenda-new-buffers)
7267 (setq org-agenda-new-buffers nil)
7268 (org-agenda-Quit))
7270 (defun org-agenda-kill-all-agenda-buffers ()
7271 "Kill all buffers in `org-agenda-mode'.
7272 This is used when toggling sticky agendas."
7273 (interactive)
7274 (let (blist)
7275 (dolist (buf (buffer-list))
7276 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7277 (push buf blist)))
7278 (mapc 'kill-buffer blist)))
7280 (defun org-agenda-execute (arg)
7281 "Execute another agenda command, keeping same window.
7282 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7283 in the agenda."
7284 (interactive "P")
7285 (let ((org-agenda-window-setup 'current-window))
7286 (org-agenda arg)))
7288 (defun org-agenda-redo (&optional all)
7289 "Rebuild possibly ALL agenda view(s) in the current buffer."
7290 (interactive "P")
7291 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7292 (cpa (unless (eq all t) current-prefix-arg))
7293 (org-agenda-doing-sticky-redo org-agenda-sticky)
7294 (org-agenda-sticky nil)
7295 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7296 org-agenda-buffer-name))
7297 (org-agenda-keep-modes t)
7298 (tag-filter org-agenda-tag-filter)
7299 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7300 (top-hl-filter org-agenda-top-headline-filter)
7301 (cat-filter org-agenda-category-filter)
7302 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7303 (re-filter org-agenda-regexp-filter)
7304 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7305 (effort-filter org-agenda-effort-filter)
7306 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7307 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7308 (cols org-agenda-columns-active)
7309 (line (org-current-line))
7310 (window-line (- line (org-current-line (window-start))))
7311 (lprops (get 'org-agenda-redo-command 'org-lprops))
7312 (redo-cmd (get-text-property p 'org-redo-cmd))
7313 (last-args (get-text-property p 'org-last-args))
7314 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7315 (org-agenda-overriding-cmd-arguments
7316 (unless (eq all t)
7317 (cond ((listp last-args)
7318 (cons (or cpa (car last-args)) (cdr last-args)))
7319 ((stringp last-args)
7320 last-args))))
7321 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7322 (put 'org-agenda-tag-filter :preset-filter nil)
7323 (put 'org-agenda-category-filter :preset-filter nil)
7324 (put 'org-agenda-regexp-filter :preset-filter nil)
7325 (put 'org-agenda-effort-filter :preset-filter nil)
7326 (and cols (org-columns-quit))
7327 (message "Rebuilding agenda buffer...")
7328 (if series-redo-cmd
7329 (eval series-redo-cmd)
7330 (org-let lprops redo-cmd))
7331 (setq org-agenda-undo-list nil
7332 org-agenda-pending-undo-list nil
7333 org-agenda-tag-filter tag-filter
7334 org-agenda-category-filter cat-filter
7335 org-agenda-regexp-filter re-filter
7336 org-agenda-effort-filter effort-filter
7337 org-agenda-top-headline-filter top-hl-filter)
7338 (message "Rebuilding agenda buffer...done")
7339 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7340 (put 'org-agenda-category-filter :preset-filter cat-preset)
7341 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7342 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7343 (let ((tag (or tag-filter tag-preset))
7344 (cat (or cat-filter cat-preset))
7345 (effort (or effort-filter effort-preset))
7346 (re (or re-filter re-preset)))
7347 (when tag (org-agenda-filter-apply tag 'tag t))
7348 (when cat (org-agenda-filter-apply cat 'category))
7349 (when effort (org-agenda-filter-apply effort 'effort))
7350 (when re (org-agenda-filter-apply re 'regexp)))
7351 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7352 (and cols (called-interactively-p 'any) (org-agenda-columns))
7353 (org-goto-line line)
7354 (recenter window-line)))
7356 (defvar org-global-tags-completion-table nil)
7357 (defvar org-agenda-filter-form nil)
7358 (defvar org-agenda-filtered-by-category nil)
7360 (defun org-agenda-filter-by-category (strip)
7361 "Filter lines in the agenda buffer that have a specific category.
7362 The category is that of the current line.
7363 Without prefix argument, keep only the lines of that category.
7364 With a prefix argument, exclude the lines of that category.
7366 (interactive "P")
7367 (if (and org-agenda-filtered-by-category
7368 org-agenda-category-filter)
7369 (org-agenda-filter-show-all-cat)
7370 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7371 (cond
7372 ((and cat strip)
7373 (org-agenda-filter-apply
7374 (push (concat "-" cat) org-agenda-category-filter) 'category))
7375 (cat
7376 (org-agenda-filter-apply
7377 (setq org-agenda-category-filter
7378 (list (concat "+" cat))) 'category))
7379 (t (error "No category at point"))))))
7381 (defun org-find-top-headline (&optional pos)
7382 "Find the topmost parent headline and return it."
7383 (save-excursion
7384 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7385 (if pos (goto-char pos))
7386 ;; Skip up to the topmost parent
7387 (while (ignore-errors (outline-up-heading 1) t))
7388 (ignore-errors
7389 (nth 4 (org-heading-components))))))
7391 (defvar org-agenda-filtered-by-top-headline nil)
7392 (defun org-agenda-filter-by-top-headline (strip)
7393 "Keep only those lines that are descendants from the same top headline.
7394 The top headline is that of the current line."
7395 (interactive "P")
7396 (if org-agenda-filtered-by-top-headline
7397 (progn
7398 (setq org-agenda-filtered-by-top-headline nil
7399 org-agenda-top-headline-filter nil)
7400 (org-agenda-filter-show-all-top-filter))
7401 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7402 (if toph (org-agenda-filter-top-headline-apply toph strip)
7403 (error "No top-level headline at point")))))
7405 (defvar org-agenda-regexp-filter nil)
7406 (defun org-agenda-filter-by-regexp (strip)
7407 "Filter agenda entries by a regular expression.
7408 Regexp filters are cumulative.
7409 With no prefix argument, keep entries matching the regexp.
7410 With one prefix argument, filter out entries matching the regexp.
7411 With two prefix arguments, remove the regexp filters."
7412 (interactive "P")
7413 (if (not (equal strip '(16)))
7414 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7415 (read-from-minibuffer
7416 (if (equal strip '(4))
7417 "Filter out entries matching regexp: "
7418 "Narrow to entries matching regexp: ")))))
7419 (push flt org-agenda-regexp-filter)
7420 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7421 (org-agenda-filter-show-all-re)
7422 (message "Regexp filter removed")))
7424 (defvar org-agenda-effort-filter nil)
7425 (defun org-agenda-filter-by-effort (strip)
7426 "Filter agenda entries by effort.
7427 With no prefix argument, keep entries matching the effort condition.
7428 With one prefix argument, filter out entries matching the condition.
7429 With two prefix arguments, remove the effort filters."
7430 (interactive "P")
7431 (cond ((member strip '(nil 4))
7432 (let ((efforts (org-split-string
7433 (or (cdr (assoc (concat org-effort-property "_ALL")
7434 org-global-properties))
7435 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7436 "")))
7437 (eff -1)
7438 effort-prompt op)
7439 (while (not (member op '(?< ?> ?=)))
7440 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7441 (loop for i from 0 to 9 do
7442 (setq effort-prompt
7443 (concat
7444 effort-prompt " ["
7445 (if (= i 9) "0" (int-to-string (1+ i)))
7446 "]" (nth i efforts))))
7447 (message "Effort %s%s" (char-to-string op) effort-prompt)
7448 (while (or (< eff 0) (> eff 9))
7449 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7450 (setq org-agenda-effort-filter
7451 (list (concat (if strip "-" "+")
7452 (char-to-string op) (nth (1- eff) efforts))))
7453 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7454 (t (org-agenda-filter-show-all-effort)
7455 (message "Effort filter removed"))))
7457 (defun org-agenda-filter-remove-all ()
7458 "Remove all filters from the current agenda buffer."
7459 (interactive)
7460 (when org-agenda-tag-filter
7461 (org-agenda-filter-show-all-tag))
7462 (when org-agenda-category-filter
7463 (org-agenda-filter-show-all-cat))
7464 (when org-agenda-regexp-filter
7465 (org-agenda-filter-show-all-re))
7466 (when org-agenda-top-headline-filter
7467 (org-agenda-filter-show-all-top-filter))
7468 (when org-agenda-effort-filter
7469 (org-agenda-filter-show-all-effort))
7470 (org-agenda-finalize))
7472 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7473 "Keep only those lines in the agenda buffer that have a specific tag.
7474 The tag is selected with its fast selection letter, as
7475 configured. With a single \\[universal-argument] prefix ARG,
7476 exclude the agenda search. With a double \\[universal-argument]
7477 prefix ARG, filter the literal tag. I.e. don't filter on all its
7478 group members.
7480 A lisp caller can specify CHAR. EXCLUDE means that the new tag should be
7481 used to exclude the search - the interactive user can also press `-' or `+'
7482 to switch between filtering and excluding."
7483 (interactive "P")
7484 (let* ((alist org-tag-alist-for-agenda)
7485 (tag-chars (mapconcat
7486 (lambda (x) (if (and (not (symbolp (car x)))
7487 (cdr x))
7488 (char-to-string (cdr x))
7489 ""))
7490 org-tag-alist-for-agenda ""))
7491 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7492 (string-to-list tag-chars)))
7493 (exclude (or exclude (equal arg '(4))))
7494 (expand (not (equal arg '(16))))
7495 (inhibit-read-only t)
7496 (current org-agenda-tag-filter)
7497 a n tag)
7498 (unless char
7499 (while (not (memq char valid-char-list))
7500 (message
7501 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7502 (if exclude "Exclude" "Filter") tag-chars
7503 (if org-agenda-auto-exclude-function "[RET], " "")
7504 (if expand "" ", no grouptag expand"))
7505 (setq char (read-char-exclusive))
7506 ;; Excluding or filtering down
7507 (cond ((eq char ?-) (setq exclude t))
7508 ((eq char ?+) (setq exclude nil)))))
7509 (when (eq char ?\t)
7510 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7511 (setq-local org-global-tags-completion-table
7512 (org-global-tags-completion-table)))
7513 (let ((completion-ignore-case t))
7514 (setq tag (completing-read
7515 "Tag: " org-global-tags-completion-table))))
7516 (cond
7517 ((eq char ?\r)
7518 (org-agenda-filter-show-all-tag)
7519 (when org-agenda-auto-exclude-function
7520 (setq org-agenda-tag-filter nil)
7521 (dolist (tag (org-agenda-get-represented-tags))
7522 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7523 (if modifier
7524 (push modifier org-agenda-tag-filter))))
7525 (if (not (null org-agenda-tag-filter))
7526 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7527 ((eq char ?/)
7528 (org-agenda-filter-show-all-tag)
7529 (when (get 'org-agenda-tag-filter :preset-filter)
7530 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7531 ((eq char ?.)
7532 (setq org-agenda-tag-filter
7533 (mapcar (lambda(tag) (concat "+" tag))
7534 (org-get-at-bol 'tags)))
7535 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7536 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7537 ((or (eq char ?\s)
7538 (setq a (rassoc char alist))
7539 (and tag (setq a (cons tag nil))))
7540 (org-agenda-filter-show-all-tag)
7541 (setq tag (car a))
7542 (setq org-agenda-tag-filter
7543 (cons (concat (if exclude "-" "+") tag)
7544 current))
7545 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7546 (t (error "Invalid tag selection character %c" char)))))
7548 (defun org-agenda-get-represented-tags ()
7549 "Get a list of all tags currently represented in the agenda."
7550 (let (p tags)
7551 (save-excursion
7552 (goto-char (point-min))
7553 (while (setq p (next-single-property-change (point) 'tags))
7554 (goto-char p)
7555 (mapc (lambda (x) (add-to-list 'tags x))
7556 (get-text-property (point) 'tags))))
7557 tags))
7560 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7561 "Create the form that tests a line for agenda filter. Optional
7562 argument EXPAND can be used for the TYPE tag and will expand the
7563 tags in the FILTER if any of the tags in FILTER are grouptags."
7564 (let (f f1)
7565 (cond
7566 ;; Tag filter
7567 ((eq type 'tag)
7568 (setq filter
7569 (delete-dups
7570 (append (get 'org-agenda-tag-filter :preset-filter)
7571 filter)))
7572 (dolist (x filter)
7573 (let ((op (string-to-char x)))
7574 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7575 (setq x (list x)))
7576 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7577 (push f1 f))))
7578 ;; Category filter
7579 ((eq type 'category)
7580 (setq filter
7581 (delete-dups
7582 (append (get 'org-agenda-category-filter :preset-filter)
7583 filter)))
7584 (dolist (x filter)
7585 (if (equal "-" (substring x 0 1))
7586 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7587 (setq f1 (list 'equal (substring x 1) 'cat)))
7588 (push f1 f)))
7589 ;; Regexp filter
7590 ((eq type 'regexp)
7591 (setq filter
7592 (delete-dups
7593 (append (get 'org-agenda-regexp-filter :preset-filter)
7594 filter)))
7595 (dolist (x filter)
7596 (if (equal "-" (substring x 0 1))
7597 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7598 (setq f1 (list 'string-match (substring x 1) 'txt)))
7599 (push f1 f)))
7600 ;; Effort filter
7601 ((eq type 'effort)
7602 (setq filter
7603 (delete-dups
7604 (append (get 'org-agenda-effort-filter :preset-filter)
7605 filter)))
7606 (dolist (x filter)
7607 (push (org-agenda-filter-effort-form x) f))))
7608 (cons 'and (nreverse f))))
7610 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7611 "Return a form associated to tag-expression TAGS.
7612 Build a form testing a line for agenda filter for
7613 tag-expressions. OP is an operator of type CHAR that allows the
7614 function to set the right switches in the returned form."
7615 (let (form)
7616 ;; Any of the expressions can match if OP is +, all must match if
7617 ;; the operator is -.
7618 (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
7619 (let* ((tag (substring x 1))
7620 (f (cond
7621 ((string= "" tag) '(not tags))
7622 ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
7623 ;; TAG is a regexp.
7624 (list 'org-match-any-p (substring tag 1 -1) 'tags))
7625 (t (list 'member (downcase tag) 'tags)))))
7626 (push (if (eq op ?-) (list 'not f) f) form)))))
7628 (defun org-agenda-filter-effort-form (e)
7629 "Return the form to compare the effort of the current line with what E says.
7630 E looks like \"+<2:25\"."
7631 (let (op)
7632 (setq e (substring e 1))
7633 (setq op (string-to-char e) e (substring e 1))
7634 (setq op (cond ((equal op ?<) '<=)
7635 ((equal op ?>) '>=)
7636 ((equal op ??) op)
7637 (t '=)))
7638 (list 'org-agenda-compare-effort (list 'quote op)
7639 (org-duration-string-to-minutes e))))
7641 (defun org-agenda-compare-effort (op value)
7642 "Compare the effort of the current line with VALUE, using OP.
7643 If the line does not have an effort defined, return nil."
7644 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7645 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7646 value)))
7648 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7649 "Expand group tags in FILTER for the agenda.
7650 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7651 (if org-group-tags
7652 (let ((case-fold-search t) rtn)
7653 (mapc
7654 (lambda (f)
7655 (let (f0 dir)
7656 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7657 (setq dir (match-string 1 f) f0 (match-string 2 f))
7658 (setq dir (if no-operator "" "+") f0 f))
7659 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7660 (org-tags-expand f0 t t))
7661 rtn))))
7662 filter)
7663 (reverse rtn))
7664 filter))
7666 (defun org-agenda-filter-apply (filter type &optional expand)
7667 "Set FILTER as the new agenda filter and apply it. Optional
7668 argument EXPAND can be used for the TYPE tag and will expand the
7669 tags in the FILTER if any of the tags in FILTER are grouptags."
7670 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7671 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7672 (let (tags cat txt)
7673 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7674 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7675 ;; category is used as the filter:
7676 (setq org-agenda-filtered-by-category
7677 (and (eq type 'category)
7678 (not (equal (substring (car filter) 0 1) "-"))))
7679 (org-agenda-set-mode-name)
7680 (save-excursion
7681 (goto-char (point-min))
7682 (while (not (eobp))
7683 (if (org-get-at-bol 'org-marker)
7684 (progn
7685 (setq tags (org-get-at-bol 'tags)
7686 cat (org-get-at-eol 'org-category 1)
7687 txt (org-get-at-bol 'txt))
7688 (if (not (eval org-agenda-filter-form))
7689 (org-agenda-filter-hide-line type))
7690 (beginning-of-line 2))
7691 (beginning-of-line 2))))
7692 (if (get-char-property (point) 'invisible)
7693 (ignore-errors (org-agenda-previous-line)))))
7695 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7696 "Filter by top headline HL."
7697 (org-agenda-set-mode-name)
7698 (save-excursion
7699 (goto-char (point-min))
7700 (while (not (eobp))
7701 (let* ((pos (org-get-at-bol 'org-hd-marker))
7702 (tophl (and pos (org-find-top-headline pos))))
7703 (if (and tophl (funcall (if negative 'identity 'not)
7704 (string= hl tophl)))
7705 (org-agenda-filter-hide-line 'top-headline)))
7706 (beginning-of-line 2)))
7707 (if (get-char-property (point) 'invisible)
7708 (org-agenda-previous-line))
7709 (setq org-agenda-top-headline-filter hl
7710 org-agenda-filtered-by-top-headline t))
7712 (defun org-agenda-filter-hide-line (type)
7713 "Hide lines with TYPE in the agenda buffer."
7714 (let* ((b (max (point-min) (1- (point-at-bol))))
7715 (e (point-at-eol)))
7716 (let ((inhibit-read-only t))
7717 (add-text-properties
7718 b e `(invisible org-filtered org-filter-type ,type)))))
7720 (defun org-agenda-remove-filter (type)
7721 (interactive)
7722 "Remove filter of type TYPE from the agenda buffer."
7723 (save-excursion
7724 (goto-char (point-min))
7725 (let ((inhibit-read-only t) pos)
7726 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7727 (goto-char pos)
7728 (remove-text-properties
7729 (point) (next-single-property-change (point) 'org-filter-type)
7730 `(invisible org-filtered org-filter-type ,type))))
7731 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7732 (setq org-agenda-filter-form nil)
7733 (org-agenda-set-mode-name)
7734 (org-agenda-finalize)))
7736 (defun org-agenda-filter-show-all-tag nil
7737 (org-agenda-remove-filter 'tag))
7738 (defun org-agenda-filter-show-all-re nil
7739 (org-agenda-remove-filter 'regexp))
7740 (defun org-agenda-filter-show-all-effort nil
7741 (org-agenda-remove-filter 'effort))
7742 (defun org-agenda-filter-show-all-cat nil
7743 (org-agenda-remove-filter 'category))
7744 (defun org-agenda-filter-show-all-top-filter nil
7745 (org-agenda-remove-filter 'top-headline))
7747 (defun org-agenda-manipulate-query-add ()
7748 "Manipulate the query by adding a search term with positive selection.
7749 Positive selection means the term must be matched for selection of an entry."
7750 (interactive)
7751 (org-agenda-manipulate-query ?\[))
7752 (defun org-agenda-manipulate-query-subtract ()
7753 "Manipulate the query by adding a search term with negative selection.
7754 Negative selection means term must not be matched for selection of an entry."
7755 (interactive)
7756 (org-agenda-manipulate-query ?\]))
7757 (defun org-agenda-manipulate-query-add-re ()
7758 "Manipulate the query by adding a search regexp with positive selection.
7759 Positive selection means the regexp must match for selection of an entry."
7760 (interactive)
7761 (org-agenda-manipulate-query ?\{))
7762 (defun org-agenda-manipulate-query-subtract-re ()
7763 "Manipulate the query by adding a search regexp with negative selection.
7764 Negative selection means regexp must not match for selection of an entry."
7765 (interactive)
7766 (org-agenda-manipulate-query ?\}))
7767 (defun org-agenda-manipulate-query (char)
7768 (cond
7769 ((memq org-agenda-type '(timeline agenda))
7770 (let ((org-agenda-include-inactive-timestamps t))
7771 (org-agenda-redo))
7772 (message "Display now includes inactive timestamps as well"))
7773 ((eq org-agenda-type 'search)
7774 (org-add-to-string
7775 'org-agenda-query-string
7776 (if org-agenda-last-search-view-search-was-boolean
7777 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7778 (?\{ . " +{}") (?\} . " -{}"))))
7779 " "))
7780 (setq org-agenda-redo-command
7781 (list 'org-search-view
7782 (car (get-text-property (min (1- (point-max)) (point))
7783 'org-last-args))
7784 org-agenda-query-string
7785 (+ (length org-agenda-query-string)
7786 (if (member char '(?\{ ?\})) 0 1))))
7787 (set-register org-agenda-query-register org-agenda-query-string)
7788 (let ((org-agenda-overriding-arguments
7789 (cdr org-agenda-redo-command)))
7790 (org-agenda-redo)))
7791 (t (error "Cannot manipulate query for %s-type agenda buffers"
7792 org-agenda-type))))
7794 (defun org-add-to-string (var string)
7795 (set var (concat (symbol-value var) string)))
7797 (defun org-agenda-goto-date (span)
7798 "Jump to DATE in agenda."
7799 (interactive "P")
7800 (let* ((org-read-date-prefer-future
7801 (eval org-agenda-jump-prefer-future))
7802 (date (org-read-date))
7803 (day (time-to-days (org-time-string-to-time date)))
7804 (org-agenda-sticky-orig org-agenda-sticky)
7805 (org-agenda-buffer-tmp-name (buffer-name))
7806 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7807 (0-arg (or current-prefix-arg (car args)))
7808 (2-arg (nth 2 args))
7809 (with-hour-p (nth 4 org-agenda-redo-command))
7810 (newcmd (list 'org-agenda-list 0-arg date
7811 (org-agenda-span-to-ndays
7812 2-arg (org-time-string-to-absolute date))
7813 with-hour-p))
7814 (newargs (cdr newcmd))
7815 (inhibit-read-only t)
7816 org-agenda-sticky)
7817 (if (not (org-agenda-check-type t 'agenda))
7818 (error "Not available in non-agenda views")
7819 (add-text-properties (point-min) (point-max)
7820 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7821 (org-agenda-redo)
7822 (goto-char (point-min))
7823 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7824 (save-excursion (move-beginning-of-line 2) (eobp))))
7825 (move-beginning-of-line 2))
7826 (setq org-agenda-sticky org-agenda-sticky-orig
7827 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7829 (defun org-agenda-goto-today ()
7830 "Go to today."
7831 (interactive)
7832 (org-agenda-check-type t 'timeline 'agenda)
7833 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7834 (curspan (nth 2 args))
7835 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7836 (cond
7837 (tdpos (goto-char tdpos))
7838 ((eq org-agenda-type 'agenda)
7839 (let* ((sd (org-agenda-compute-starting-span
7840 (org-today) (or curspan org-agenda-span)))
7841 (org-agenda-overriding-arguments args))
7842 (setf (nth 1 org-agenda-overriding-arguments) sd)
7843 (org-agenda-redo)
7844 (org-agenda-find-same-or-today-or-agenda)))
7845 (t (error "Cannot find today")))))
7847 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7848 (goto-char
7849 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7850 (text-property-any (point-min) (point-max) 'org-today t)
7851 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7852 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7853 (org-agenda-backward-block))
7854 (point-min))))
7856 (defun org-agenda-backward-block ()
7857 "Move backward by one agenda block."
7858 (interactive)
7859 (org-agenda-forward-block 'backward))
7861 (defun org-agenda-forward-block (&optional backward)
7862 "Move forward by one agenda block.
7863 When optional argument BACKWARD is set, go backward"
7864 (interactive)
7865 (cond ((not (derived-mode-p 'org-agenda-mode))
7866 (user-error
7867 "Cannot execute this command outside of org-agenda-mode buffers"))
7868 ((looking-at (if backward "\\`" "\\'"))
7869 (message "Already at the %s block" (if backward "first" "last")))
7870 (t (let ((pos (prog1 (point)
7871 (ignore-errors (if backward (backward-char 1)
7872 (move-end-of-line 1)))))
7873 (f (if backward
7874 'previous-single-property-change
7875 'next-single-property-change))
7876 moved dest)
7877 (while (and (setq dest (funcall
7878 f (point) 'org-agenda-structural-header))
7879 (not (get-text-property
7880 (point) 'org-agenda-structural-header)))
7881 (setq moved t)
7882 (goto-char dest))
7883 (if moved (move-beginning-of-line 1)
7884 (goto-char (if backward (point-min) (point-max)))
7885 (move-beginning-of-line 1)
7886 (message "No %s block" (if backward "previous" "further")))))))
7888 (defun org-agenda-later (arg)
7889 "Go forward in time by the current span.
7890 With prefix ARG, go forward that many times the current span."
7891 (interactive "p")
7892 (org-agenda-check-type t 'agenda)
7893 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7894 (span (or (nth 2 args) org-agenda-current-span))
7895 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7896 (greg (calendar-gregorian-from-absolute sd))
7897 (cnt (org-get-at-bol 'org-day-cnt))
7898 greg2)
7899 (cond
7900 ((numberp span)
7901 (setq sd (+ (* span arg) sd)))
7902 ((eq span 'day)
7903 (setq sd (+ arg sd)))
7904 ((eq span 'week)
7905 (setq sd (+ (* 7 arg) sd)))
7906 ((eq span 'fortnight)
7907 (setq sd (+ (* 14 arg) sd)))
7908 ((eq span 'month)
7909 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7910 sd (calendar-absolute-from-gregorian greg2))
7911 (setcar greg2 (1+ (car greg2))))
7912 ((eq span 'year)
7913 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7914 sd (calendar-absolute-from-gregorian greg2))
7915 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7917 (setq sd (+ (* span arg) sd))))
7918 (let ((org-agenda-overriding-cmd
7919 ;; `cmd' may have been set by `org-agenda-run-series' which
7920 ;; uses `org-agenda-overriding-cmd' to decide whether
7921 ;; overriding is allowed for `cmd'
7922 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7923 (org-agenda-overriding-arguments
7924 (list (car args) sd span)))
7925 (org-agenda-redo)
7926 (org-agenda-find-same-or-today-or-agenda cnt))))
7928 (defun org-agenda-earlier (arg)
7929 "Go backward in time by the current span.
7930 With prefix ARG, go backward that many times the current span."
7931 (interactive "p")
7932 (org-agenda-later (- arg)))
7934 (defun org-agenda-view-mode-dispatch ()
7935 "Call one of the view mode commands."
7936 (interactive)
7937 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7938 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7939 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7940 (let ((a (read-char-exclusive)))
7941 (case a
7942 (?\ (call-interactively 'org-agenda-reset-view))
7943 (?d (call-interactively 'org-agenda-day-view))
7944 (?w (call-interactively 'org-agenda-week-view))
7945 (?t (call-interactively 'org-agenda-fortnight-view))
7946 (?m (call-interactively 'org-agenda-month-view))
7947 (?y (call-interactively 'org-agenda-year-view))
7948 (?l (call-interactively 'org-agenda-log-mode))
7949 (?L (org-agenda-log-mode '(4)))
7950 (?c (org-agenda-log-mode 'clockcheck))
7951 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7952 (?a (call-interactively 'org-agenda-archives-mode))
7953 (?A (org-agenda-archives-mode 'files))
7954 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7955 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7956 (?G (call-interactively 'org-agenda-toggle-time-grid))
7957 (?D (call-interactively 'org-agenda-toggle-diary))
7958 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7959 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7960 (org-agenda-check-type t 'timeline 'agenda)
7961 (org-agenda-redo))
7962 (message "Display now includes inactive timestamps as well"))
7963 (?q (message "Abort"))
7964 (otherwise (error "Invalid key" )))))
7966 (defun org-agenda-reset-view ()
7967 "Switch to default view for agenda."
7968 (interactive)
7969 (org-agenda-change-time-span org-agenda-span))
7970 (defun org-agenda-day-view (&optional day-of-month)
7971 "Switch to daily view for agenda.
7972 With argument DAY-OF-MONTH, switch to that day of the month."
7973 (interactive "P")
7974 (org-agenda-change-time-span 'day day-of-month))
7975 (defun org-agenda-week-view (&optional iso-week)
7976 "Switch to daily view for agenda.
7977 With argument ISO-WEEK, switch to the corresponding ISO week.
7978 If ISO-WEEK has more then 2 digits, only the last two encode the
7979 week. Any digits before this encode a year. So 200712 means
7980 week 12 of year 2007. Years in the range 1938-2037 can also be
7981 written as 2-digit years."
7982 (interactive "P")
7983 (org-agenda-change-time-span 'week iso-week))
7984 (defun org-agenda-fortnight-view (&optional iso-week)
7985 "Switch to daily view for agenda.
7986 With argument ISO-WEEK, switch to the corresponding ISO week.
7987 If ISO-WEEK has more then 2 digits, only the last two encode the
7988 week. Any digits before this encode a year. So 200712 means
7989 week 12 of year 2007. Years in the range 1938-2037 can also be
7990 written as 2-digit years."
7991 (interactive "P")
7992 (org-agenda-change-time-span 'fortnight iso-week))
7993 (defun org-agenda-month-view (&optional month)
7994 "Switch to monthly view for agenda.
7995 With argument MONTH, switch to that month."
7996 (interactive "P")
7997 (org-agenda-change-time-span 'month month))
7998 (defun org-agenda-year-view (&optional year)
7999 "Switch to yearly view for agenda.
8000 With argument YEAR, switch to that year.
8001 If MONTH has more then 2 digits, only the last two encode the
8002 month. Any digits before this encode a year. So 200712 means
8003 December year 2007. Years in the range 1938-2037 can also be
8004 written as 2-digit years."
8005 (interactive "P")
8006 (when year
8007 (setq year (org-small-year-to-year year)))
8008 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8009 (org-agenda-change-time-span 'year year)
8010 (error "Abort")))
8012 (defun org-agenda-change-time-span (span &optional n)
8013 "Change the agenda view to SPAN.
8014 SPAN may be `day', `week', `fortnight', `month', `year'."
8015 (org-agenda-check-type t 'agenda)
8016 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8017 (curspan (nth 2 args)))
8018 (if (and (not n) (equal curspan span))
8019 (error "Viewing span is already \"%s\"" span))
8020 (let* ((sd (or (org-get-at-bol 'day)
8021 (nth 1 args)
8022 org-starting-day))
8023 (sd (org-agenda-compute-starting-span sd span n))
8024 (org-agenda-overriding-cmd
8025 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8026 (org-agenda-overriding-arguments
8027 (list (car args) sd span)))
8028 (org-agenda-redo)
8029 (org-agenda-find-same-or-today-or-agenda))
8030 (org-agenda-set-mode-name)
8031 (message "Switched to %s view" span)))
8033 (defun org-agenda-compute-starting-span (sd span &optional n)
8034 "Compute starting date for agenda.
8035 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8036 is a cons cell with the starting date and the number of days,
8037 so that the date SD will be in that range."
8038 (let* ((greg (calendar-gregorian-from-absolute sd))
8039 (dg (nth 1 greg))
8040 (mg (car greg))
8041 (yg (nth 2 greg)))
8042 (cond
8043 ((eq span 'day)
8044 (when n
8045 (setq sd (+ (calendar-absolute-from-gregorian
8046 (list mg 1 yg))
8047 n -1))))
8048 ((or (eq span 'week) (eq span 'fortnight))
8049 (let* ((nt (calendar-day-of-week
8050 (calendar-gregorian-from-absolute sd)))
8051 (d (if org-agenda-start-on-weekday
8052 (- nt org-agenda-start-on-weekday)
8055 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8056 (when n
8057 (require 'cal-iso)
8058 (when (> n 99)
8059 (setq y1 (org-small-year-to-year (/ n 100))
8060 n (mod n 100)))
8061 (setq sd
8062 (calendar-iso-to-absolute
8063 (list n 1
8064 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8065 ((eq span 'month)
8066 (let (y1)
8067 (when (and n (> n 99))
8068 (setq y1 (org-small-year-to-year (/ n 100))
8069 n (mod n 100)))
8070 (setq sd (calendar-absolute-from-gregorian
8071 (list (or n mg) 1 (or y1 yg))))))
8072 ((eq span 'year)
8073 (setq sd (calendar-absolute-from-gregorian
8074 (list 1 1 (or n yg))))))
8075 sd))
8077 (defun org-agenda-next-date-line (&optional arg)
8078 "Jump to the next line indicating a date in agenda buffer."
8079 (interactive "p")
8080 (org-agenda-check-type t 'agenda 'timeline)
8081 (beginning-of-line 1)
8082 ;; This does not work if user makes date format that starts with a blank
8083 (if (looking-at "^\\S-") (forward-char 1))
8084 (if (not (re-search-forward "^\\S-" nil t arg))
8085 (progn
8086 (backward-char 1)
8087 (error "No next date after this line in this buffer")))
8088 (goto-char (match-beginning 0)))
8090 (defun org-agenda-previous-date-line (&optional arg)
8091 "Jump to the previous line indicating a date in agenda buffer."
8092 (interactive "p")
8093 (org-agenda-check-type t 'agenda 'timeline)
8094 (beginning-of-line 1)
8095 (if (not (re-search-backward "^\\S-" nil t arg))
8096 (error "No previous date before this line in this buffer")))
8098 ;; Initialize the highlight
8099 (defvar org-hl (make-overlay 1 1))
8100 (overlay-put org-hl 'face 'highlight)
8102 (defun org-highlight (begin end &optional buffer)
8103 "Highlight a region with overlay."
8104 (move-overlay org-hl begin end (or buffer (current-buffer))))
8106 (defun org-unhighlight ()
8107 "Detach overlay INDEX."
8108 (delete-overlay org-hl))
8110 (defun org-unhighlight-once ()
8111 "Remove the highlight from its position, and this function from the hook."
8112 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8113 (org-unhighlight))
8115 (defvar org-agenda-pre-follow-window-conf nil)
8116 (defun org-agenda-follow-mode ()
8117 "Toggle follow mode in an agenda buffer."
8118 (interactive)
8119 (unless org-agenda-follow-mode
8120 (setq org-agenda-pre-follow-window-conf
8121 (current-window-configuration)))
8122 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8123 (unless org-agenda-follow-mode
8124 (set-window-configuration org-agenda-pre-follow-window-conf))
8125 (org-agenda-set-mode-name)
8126 (org-agenda-do-context-action)
8127 (message "Follow mode is %s"
8128 (if org-agenda-follow-mode "on" "off")))
8130 (defun org-agenda-entry-text-mode (&optional arg)
8131 "Toggle entry text mode in an agenda buffer."
8132 (interactive "P")
8133 (if (or org-agenda-tag-filter
8134 org-agenda-category-filter
8135 org-agenda-regexp-filter
8136 org-agenda-top-headline-filter)
8137 (user-error "Can't show entry text in filtered views")
8138 (setq org-agenda-entry-text-mode (or (integerp arg)
8139 (not org-agenda-entry-text-mode)))
8140 (org-agenda-entry-text-hide)
8141 (and org-agenda-entry-text-mode
8142 (let ((org-agenda-entry-text-maxlines
8143 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8144 (org-agenda-entry-text-show)))
8145 (org-agenda-set-mode-name)
8146 (message "Entry text mode is %s%s"
8147 (if org-agenda-entry-text-mode "on" "off")
8148 (if (not org-agenda-entry-text-mode) ""
8149 (format " (maximum number of lines is %d)"
8150 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8152 (defun org-agenda-clockreport-mode ()
8153 "Toggle clocktable mode in an agenda buffer."
8154 (interactive)
8155 (org-agenda-check-type t 'agenda)
8156 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8157 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8158 (org-agenda-set-mode-name)
8159 (org-agenda-redo)
8160 (message "Clocktable mode is %s"
8161 (if org-agenda-clockreport-mode "on" "off")))
8163 (defun org-agenda-log-mode (&optional special)
8164 "Toggle log mode in an agenda buffer.
8165 With argument SPECIAL, show all possible log items, not only the ones
8166 configured in `org-agenda-log-mode-items'.
8167 With a double \\[universal-argument] prefix arg, show *only* \
8168 log items, nothing else."
8169 (interactive "P")
8170 (org-agenda-check-type t 'agenda 'timeline)
8171 (setq org-agenda-show-log
8172 (cond
8173 ((equal special '(16)) 'only)
8174 ((eq special 'clockcheck)
8175 (if (eq org-agenda-show-log 'clockcheck)
8176 nil 'clockcheck))
8177 (special '(closed clock state))
8178 (t (not org-agenda-show-log))))
8179 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8180 (org-agenda-set-mode-name)
8181 (org-agenda-redo)
8182 (message "Log mode is %s"
8183 (if org-agenda-show-log "on" "off")))
8185 (defun org-agenda-archives-mode (&optional with-files)
8186 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8187 When called with a prefix argument, include all archive files as well."
8188 (interactive "P")
8189 (setq org-agenda-archives-mode
8190 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8191 (org-agenda-set-mode-name)
8192 (org-agenda-redo)
8193 (message
8194 "%s"
8195 (cond
8196 ((eq org-agenda-archives-mode nil)
8197 "No archives are included")
8198 ((eq org-agenda-archives-mode 'trees)
8199 (format "Trees with :%s: tag are included" org-archive-tag))
8200 ((eq org-agenda-archives-mode t)
8201 (format "Trees with :%s: tag and all active archive files are included"
8202 org-archive-tag)))))
8204 (defun org-agenda-toggle-diary ()
8205 "Toggle diary inclusion in an agenda buffer."
8206 (interactive)
8207 (org-agenda-check-type t 'agenda)
8208 (setq org-agenda-include-diary (not org-agenda-include-diary))
8209 (org-agenda-redo)
8210 (org-agenda-set-mode-name)
8211 (message "Diary inclusion turned %s"
8212 (if org-agenda-include-diary "on" "off")))
8214 (defun org-agenda-toggle-deadlines ()
8215 "Toggle inclusion of entries with a deadline in an agenda buffer."
8216 (interactive)
8217 (org-agenda-check-type t 'agenda)
8218 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8219 (org-agenda-redo)
8220 (org-agenda-set-mode-name)
8221 (message "Deadlines inclusion turned %s"
8222 (if org-agenda-include-deadlines "on" "off")))
8224 (defun org-agenda-toggle-time-grid ()
8225 "Toggle time grid in an agenda buffer."
8226 (interactive)
8227 (org-agenda-check-type t 'agenda)
8228 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8229 (org-agenda-redo)
8230 (org-agenda-set-mode-name)
8231 (message "Time-grid turned %s"
8232 (if org-agenda-use-time-grid "on" "off")))
8234 (defun org-agenda-set-mode-name ()
8235 "Set the mode name to indicate all the small mode settings."
8236 (setq mode-name
8237 (list "Org-Agenda"
8238 (if (get 'org-agenda-files 'org-restrict) " []" "")
8240 '(:eval (org-agenda-span-name org-agenda-current-span))
8241 (if org-agenda-follow-mode " Follow" "")
8242 (if org-agenda-entry-text-mode " ETxt" "")
8243 (if org-agenda-include-diary " Diary" "")
8244 (if org-agenda-include-deadlines " Ddl" "")
8245 (if org-agenda-use-time-grid " Grid" "")
8246 (if (and (boundp 'org-habit-show-habits)
8247 org-habit-show-habits) " Habit" "")
8248 (cond
8249 ((consp org-agenda-show-log) " LogAll")
8250 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8251 (org-agenda-show-log " Log")
8252 (t ""))
8253 (if (or org-agenda-category-filter
8254 (get 'org-agenda-category-filter :preset-filter))
8255 '(:eval (propertize
8256 (concat " <"
8257 (mapconcat
8258 'identity
8259 (append
8260 (get 'org-agenda-category-filter :preset-filter)
8261 org-agenda-category-filter)
8263 ">")
8264 'face 'org-agenda-filter-category
8265 'help-echo "Category used in filtering")) "")
8266 (if (or org-agenda-tag-filter
8267 (get 'org-agenda-tag-filter :preset-filter))
8268 '(:eval (propertize
8269 (concat " {"
8270 (mapconcat
8271 'identity
8272 (append
8273 (get 'org-agenda-tag-filter :preset-filter)
8274 org-agenda-tag-filter)
8276 "}")
8277 'face 'org-agenda-filter-tags
8278 'help-echo "Tags used in filtering")) "")
8279 (if (or org-agenda-effort-filter
8280 (get 'org-agenda-effort-filter :preset-filter))
8281 '(:eval (propertize
8282 (concat " {"
8283 (mapconcat
8284 'identity
8285 (append
8286 (get 'org-agenda-effort-filter :preset-filter)
8287 org-agenda-effort-filter)
8289 "}")
8290 'face 'org-agenda-filter-effort
8291 'help-echo "Effort conditions used in filtering")) "")
8292 (if (or org-agenda-regexp-filter
8293 (get 'org-agenda-regexp-filter :preset-filter))
8294 '(:eval (propertize
8295 (concat " ["
8296 (mapconcat
8297 'identity
8298 (append
8299 (get 'org-agenda-regexp-filter :preset-filter)
8300 org-agenda-regexp-filter)
8302 "]")
8303 'face 'org-agenda-filter-regexp
8304 'help-echo "Regexp used in filtering")) "")
8305 (if org-agenda-archives-mode
8306 (if (eq org-agenda-archives-mode t)
8307 " Archives"
8308 (format " :%s:" org-archive-tag))
8310 (if org-agenda-clockreport-mode " Clock" "")))
8311 (force-mode-line-update))
8313 (defun org-agenda-update-agenda-type ()
8314 "Update the agenda type after each command."
8315 (setq org-agenda-type
8316 (or (get-text-property (point) 'org-agenda-type)
8317 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8319 (defun org-agenda-next-line ()
8320 "Move cursor to the next line, and show if follow mode is active."
8321 (interactive)
8322 (call-interactively 'next-line)
8323 (org-agenda-do-context-action))
8325 (defun org-agenda-previous-line ()
8326 "Move cursor to the previous line, and show if follow-mode is active."
8327 (interactive)
8328 (call-interactively 'previous-line)
8329 (org-agenda-do-context-action))
8331 (defun org-agenda-next-item (n)
8332 "Move cursor to next agenda item."
8333 (interactive "p")
8334 (let ((col (current-column)))
8335 (dotimes (c n)
8336 (when (next-single-property-change (point-at-eol) 'org-marker)
8337 (move-end-of-line 1)
8338 (goto-char (next-single-property-change (point) 'org-marker))))
8339 (org-move-to-column col))
8340 (org-agenda-do-context-action))
8342 (defun org-agenda-previous-item (n)
8343 "Move cursor to next agenda item."
8344 (interactive "p")
8345 (dotimes (c n)
8346 (let ((col (current-column))
8347 (goto (save-excursion
8348 (move-end-of-line 0)
8349 (previous-single-property-change (point) 'org-marker))))
8350 (if goto (goto-char goto))
8351 (org-move-to-column col)))
8352 (org-agenda-do-context-action))
8354 (defun org-agenda-do-context-action ()
8355 "Show outline path and, maybe, follow mode window."
8356 (let ((m (org-get-at-bol 'org-marker)))
8357 (when (and (markerp m) (marker-buffer m))
8358 (and org-agenda-follow-mode
8359 (if org-agenda-follow-indirect
8360 (org-agenda-tree-to-indirect-buffer nil)
8361 (org-agenda-show)))
8362 (and org-agenda-show-outline-path
8363 (org-with-point-at m (org-display-outline-path t))))))
8365 (defun org-agenda-show-tags ()
8366 "Show the tags applicable to the current item."
8367 (interactive)
8368 (let* ((tags (org-get-at-bol 'tags)))
8369 (if tags
8370 (message "Tags are :%s:"
8371 (org-no-properties (mapconcat 'identity tags ":")))
8372 (message "No tags associated with this line"))))
8374 (defun org-agenda-goto (&optional highlight)
8375 "Go to the entry at point in the corresponding Org-mode file."
8376 (interactive)
8377 (let* ((marker (or (org-get-at-bol 'org-marker)
8378 (org-agenda-error)))
8379 (buffer (marker-buffer marker))
8380 (pos (marker-position marker)))
8381 (switch-to-buffer-other-window buffer)
8382 (widen)
8383 (push-mark)
8384 (goto-char pos)
8385 (when (derived-mode-p 'org-mode)
8386 (org-show-context 'agenda)
8387 (recenter (/ (window-height) 2))
8388 (org-back-to-heading t)
8389 (if (re-search-forward org-complex-heading-regexp nil t)
8390 (goto-char (match-beginning 4))))
8391 (run-hooks 'org-agenda-after-show-hook)
8392 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8394 (defvar org-agenda-after-show-hook nil
8395 "Normal hook run after an item has been shown from the agenda.
8396 Point is in the buffer where the item originated.")
8398 (defun org-agenda-kill ()
8399 "Kill the entry or subtree belonging to the current agenda entry."
8400 (interactive)
8401 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8402 (let* ((bufname-orig (buffer-name))
8403 (marker (or (org-get-at-bol 'org-marker)
8404 (org-agenda-error)))
8405 (buffer (marker-buffer marker))
8406 (pos (marker-position marker))
8407 (type (org-get-at-bol 'type))
8408 dbeg dend (n 0) conf)
8409 (org-with-remote-undo buffer
8410 (with-current-buffer buffer
8411 (save-excursion
8412 (goto-char pos)
8413 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8414 (setq dbeg (progn (org-back-to-heading t) (point))
8415 dend (org-end-of-subtree t t))
8416 (setq dbeg (point-at-bol)
8417 dend (min (point-max) (1+ (point-at-eol)))))
8418 (goto-char dbeg)
8419 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8420 (setq conf (or (eq t org-agenda-confirm-kill)
8421 (and (numberp org-agenda-confirm-kill)
8422 (> n org-agenda-confirm-kill))))
8423 (and conf
8424 (not (y-or-n-p
8425 (format "Delete entry with %d lines in buffer \"%s\"? "
8426 n (buffer-name buffer))))
8427 (error "Abort"))
8428 (let ((org-agenda-buffer-name bufname-orig))
8429 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8430 (with-current-buffer buffer (delete-region dbeg dend))
8431 (message "Agenda item and source killed"))))
8433 (defvar org-archive-default-command) ; defined in org-archive.el
8434 (defun org-agenda-archive-default ()
8435 "Archive the entry or subtree belonging to the current agenda entry."
8436 (interactive)
8437 (require 'org-archive)
8438 (org-agenda-archive-with org-archive-default-command))
8440 (defun org-agenda-archive-default-with-confirmation ()
8441 "Archive the entry or subtree belonging to the current agenda entry."
8442 (interactive)
8443 (require 'org-archive)
8444 (org-agenda-archive-with org-archive-default-command 'confirm))
8446 (defun org-agenda-archive ()
8447 "Archive the entry or subtree belonging to the current agenda entry."
8448 (interactive)
8449 (org-agenda-archive-with 'org-archive-subtree))
8451 (defun org-agenda-archive-to-archive-sibling ()
8452 "Move the entry to the archive sibling."
8453 (interactive)
8454 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8456 (defun org-agenda-archive-with (cmd &optional confirm)
8457 "Move the entry to the archive sibling."
8458 (interactive)
8459 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8460 (let* ((bufname-orig (buffer-name))
8461 (marker (or (org-get-at-bol 'org-marker)
8462 (org-agenda-error)))
8463 (buffer (marker-buffer marker))
8464 (pos (marker-position marker)))
8465 (org-with-remote-undo buffer
8466 (with-current-buffer buffer
8467 (if (derived-mode-p 'org-mode)
8468 (if (and confirm
8469 (not (y-or-n-p "Archive this subtree or entry? ")))
8470 (error "Abort")
8471 (save-window-excursion
8472 (goto-char pos)
8473 (let ((org-agenda-buffer-name bufname-orig))
8474 (org-remove-subtree-entries-from-agenda))
8475 (org-back-to-heading t)
8476 (funcall cmd)))
8477 (error "Archiving works only in Org-mode files"))))))
8479 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8480 "Remove all lines in the agenda that correspond to a given subtree.
8481 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8482 If this information is not given, the function uses the tree at point."
8483 (let ((buf (or buf (current-buffer))) m p)
8484 (save-excursion
8485 (unless (and beg end)
8486 (org-back-to-heading t)
8487 (setq beg (point))
8488 (org-end-of-subtree t)
8489 (setq end (point)))
8490 (set-buffer (get-buffer org-agenda-buffer-name))
8491 (save-excursion
8492 (goto-char (point-max))
8493 (beginning-of-line 1)
8494 (while (not (bobp))
8495 (when (and (setq m (org-get-at-bol 'org-marker))
8496 (equal buf (marker-buffer m))
8497 (setq p (marker-position m))
8498 (>= p beg)
8499 (< p end))
8500 (let ((inhibit-read-only t))
8501 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8502 (beginning-of-line 0))))))
8504 (defun org-agenda-refile (&optional goto rfloc no-update)
8505 "Refile the item at point.
8507 When GOTO is 0 or \\='(64) or a triple \\[universal-argument] prefix argument,
8508 clear the refile cache.
8509 When GOTO is \\='(16) or a double \\[universal-argument] prefix argument,
8510 go to the location of the last refiled item.
8511 RFLOC can be a refile location obtained in a different way.
8512 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8513 (interactive "P")
8514 (cond
8515 ((member goto '(0 (64)))
8516 (org-refile-cache-clear))
8517 ((equal goto '(16))
8518 (org-refile-goto-last-stored))
8520 (let* ((buffer-orig (buffer-name))
8521 (marker (or (org-get-at-bol 'org-hd-marker)
8522 (org-agenda-error)))
8523 (buffer (marker-buffer marker))
8524 (pos (marker-position marker))
8525 (rfloc (or rfloc
8526 (org-refile-get-location
8527 (if goto "Goto" "Refile to") buffer
8528 org-refile-allow-creating-parent-nodes))))
8529 (with-current-buffer buffer
8530 (save-excursion
8531 (save-restriction
8532 (widen)
8533 (goto-char marker)
8534 (let ((org-agenda-buffer-name buffer-orig))
8535 (org-remove-subtree-entries-from-agenda))
8536 (org-refile goto buffer rfloc)))))
8537 (unless no-update (org-agenda-redo)))))
8539 (defun org-agenda-open-link (&optional arg)
8540 "Open the link(s) in the current entry, if any.
8541 This looks for a link in the displayed line in the agenda.
8542 It also looks at the text of the entry itself."
8543 (interactive "P")
8544 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8545 (org-get-at-bol 'org-marker)))
8546 (buffer (and marker (marker-buffer marker)))
8547 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8548 (lkall (and buffer (org-offer-links-in-entry
8549 buffer marker arg prefix)))
8550 (lk0 (car lkall))
8551 (lk (if (stringp lk0) (list lk0) lk0))
8552 (lkend (cdr lkall))
8553 trg)
8554 (cond
8555 ((and buffer lk)
8556 (mapcar (lambda(l)
8557 (with-current-buffer buffer
8558 (setq trg (and (string-match org-bracket-link-regexp l)
8559 (match-string 1 l)))
8560 (if (or (not trg) (string-match org-any-link-re trg))
8561 (save-excursion
8562 (save-restriction
8563 (widen)
8564 (goto-char marker)
8565 (when (search-forward l nil lkend)
8566 (goto-char (match-beginning 0))
8567 (org-open-at-point))))
8568 ;; This is an internal link, widen the buffer
8569 (switch-to-buffer-other-window buffer)
8570 (widen)
8571 (goto-char marker)
8572 (when (search-forward l nil lkend)
8573 (goto-char (match-beginning 0))
8574 (org-open-at-point)))))
8575 lk))
8576 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8577 (save-excursion
8578 (beginning-of-line 1)
8579 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8580 (org-open-link-from-string (match-string 1)))
8581 (t (message "No link to open here")))))
8583 (defun org-agenda-copy-local-variable (var)
8584 "Get a variable from a referenced buffer and install it here."
8585 (let ((m (org-get-at-bol 'org-marker)))
8586 (when (and m (buffer-live-p (marker-buffer m)))
8587 (set (make-local-variable var)
8588 (with-current-buffer (marker-buffer m)
8589 (symbol-value var))))))
8591 (defun org-agenda-switch-to (&optional delete-other-windows)
8592 "Go to the Org mode file which contains the item at point.
8593 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8594 displayed Org file fills the frame."
8595 (interactive)
8596 (if (and org-return-follows-link
8597 (not (org-get-at-bol 'org-marker))
8598 (org-in-regexp org-bracket-link-regexp))
8599 (org-open-link-from-string (match-string 0))
8600 (let* ((marker (or (org-get-at-bol 'org-marker)
8601 (org-agenda-error)))
8602 (buffer (marker-buffer marker))
8603 (pos (marker-position marker)))
8604 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8605 (pop-to-buffer-same-window buffer)
8606 (when delete-other-windows (delete-other-windows))
8607 (widen)
8608 (goto-char pos)
8609 (when (derived-mode-p 'org-mode)
8610 (org-show-context 'agenda)
8611 (run-hooks 'org-agenda-after-show-hook)))))
8613 (defun org-agenda-goto-mouse (ev)
8614 "Go to the Org-mode file which contains the item at the mouse click."
8615 (interactive "e")
8616 (mouse-set-point ev)
8617 (org-agenda-goto))
8619 (defun org-agenda-show (&optional full-entry)
8620 "Display the Org-mode file which contains the item at point.
8621 With prefix argument FULL-ENTRY, make the entire entry visible
8622 if it was hidden in the outline."
8623 (interactive "P")
8624 (let ((win (selected-window)))
8625 (org-agenda-goto t)
8626 (when full-entry (org-show-entry))
8627 (select-window win)))
8629 (defvar org-agenda-show-window nil)
8630 (defun org-agenda-show-and-scroll-up (&optional arg)
8631 "Display the Org-mode file which contains the item at point.
8632 When called repeatedly, scroll the window that is displaying the buffer.
8633 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8634 `show-subtree' to display the item, so that drawers and logbooks stay
8635 folded."
8636 (interactive "P")
8637 (let ((win (selected-window)))
8638 (if (and (window-live-p org-agenda-show-window)
8639 (eq this-command last-command))
8640 (progn
8641 (select-window org-agenda-show-window)
8642 (ignore-errors (scroll-up)))
8643 (org-agenda-goto t)
8644 (if arg (org-show-entry) (outline-show-subtree))
8645 (setq org-agenda-show-window (selected-window)))
8646 (select-window win)))
8648 (defun org-agenda-show-scroll-down ()
8649 "Scroll down the window showing the agenda."
8650 (interactive)
8651 (let ((win (selected-window)))
8652 (when (window-live-p org-agenda-show-window)
8653 (select-window org-agenda-show-window)
8654 (ignore-errors (scroll-down))
8655 (select-window win))))
8657 (defun org-agenda-show-1 (&optional more)
8658 "Display the Org-mode file which contains the item at point.
8659 The prefix arg selects the amount of information to display:
8661 0 hide the subtree
8662 1 just show the entry according to defaults.
8663 2 show the children view
8664 3 show the subtree view
8665 4 show the entire subtree and any LOGBOOK drawers
8666 5 show the entire subtree and any drawers
8667 With prefix argument FULL-ENTRY, make the entire entry visible
8668 if it was hidden in the outline."
8669 (interactive "p")
8670 (let ((win (selected-window)))
8671 (org-agenda-goto t)
8672 (org-back-to-heading)
8673 (set-window-start (selected-window) (point-at-bol))
8674 (cond
8675 ((= more 0)
8676 (outline-hide-subtree)
8677 (save-excursion
8678 (org-back-to-heading)
8679 (run-hook-with-args 'org-cycle-hook 'folded))
8680 (message "Remote: FOLDED"))
8681 ((and (called-interactively-p 'any) (= more 1))
8682 (message "Remote: show with default settings"))
8683 ((= more 2)
8684 (outline-show-entry)
8685 (org-show-children)
8686 (save-excursion
8687 (org-back-to-heading)
8688 (run-hook-with-args 'org-cycle-hook 'children))
8689 (message "Remote: CHILDREN"))
8690 ((= more 3)
8691 (outline-show-subtree)
8692 (save-excursion
8693 (org-back-to-heading)
8694 (run-hook-with-args 'org-cycle-hook 'subtree))
8695 (message "Remote: SUBTREE"))
8696 ((= more 4)
8697 (outline-show-subtree)
8698 (save-excursion
8699 (org-back-to-heading)
8700 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8701 (message "Remote: SUBTREE AND LOGBOOK"))
8702 ((> more 4)
8703 (outline-show-subtree)
8704 (message "Remote: SUBTREE AND ALL DRAWERS")))
8705 (select-window win)))
8707 (defvar org-agenda-cycle-counter nil)
8708 (defun org-agenda-cycle-show (&optional n)
8709 "Show the current entry in another window, with default settings.
8711 Default settings are taken from `org-show-context-detail'. When
8712 use repeatedly in immediate succession, the remote entry will
8713 cycle through visibility
8715 children -> subtree -> folded
8717 When called with a numeric prefix arg, that arg will be passed through to
8718 `org-agenda-show-1'. For the interpretation of that argument, see the
8719 docstring of `org-agenda-show-1'."
8720 (interactive "P")
8721 (if (integerp n)
8722 (setq org-agenda-cycle-counter n)
8723 (if (not (eq last-command this-command))
8724 (setq org-agenda-cycle-counter 1)
8725 (if (equal org-agenda-cycle-counter 0)
8726 (setq org-agenda-cycle-counter 2)
8727 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8728 (if (> org-agenda-cycle-counter 3)
8729 (setq org-agenda-cycle-counter 0)))))
8730 (org-agenda-show-1 org-agenda-cycle-counter))
8732 (defun org-agenda-recenter (arg)
8733 "Display the Org-mode file which contains the item at point and recenter."
8734 (interactive "P")
8735 (let ((win (selected-window)))
8736 (org-agenda-goto t)
8737 (recenter arg)
8738 (select-window win)))
8740 (defun org-agenda-show-mouse (ev)
8741 "Display the Org-mode file which contains the item at the mouse click."
8742 (interactive "e")
8743 (mouse-set-point ev)
8744 (org-agenda-show))
8746 (defun org-agenda-check-no-diary ()
8747 "Check if the entry is a diary link and abort if yes."
8748 (if (org-get-at-bol 'org-agenda-diary-link)
8749 (org-agenda-error)))
8751 (defun org-agenda-error ()
8752 "Throw an error when a command is not allowed in the agenda."
8753 (user-error "Command not allowed in this line"))
8755 (defun org-agenda-tree-to-indirect-buffer (arg)
8756 "Show the subtree corresponding to the current entry in an indirect buffer.
8757 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8759 With a numerical prefix ARG, go up to this level and then take that tree.
8760 With a negative numeric ARG, go up by this number of levels.
8761 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8762 use the dedicated frame)."
8763 (interactive "P")
8764 (if current-prefix-arg
8765 (org-agenda-do-tree-to-indirect-buffer arg)
8766 (let ((agenda-buffer (buffer-name))
8767 (agenda-window (selected-window))
8768 (indirect-window
8769 (and org-last-indirect-buffer
8770 (get-buffer-window org-last-indirect-buffer))))
8771 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8772 (unless (or (eq org-indirect-buffer-display 'new-frame)
8773 (eq org-indirect-buffer-display 'dedicated-frame))
8774 (unwind-protect
8775 (unless (and indirect-window (window-live-p indirect-window))
8776 (setq indirect-window (split-window agenda-window)))
8777 (and indirect-window (select-window indirect-window))
8778 (switch-to-buffer org-last-indirect-buffer :norecord)
8779 (fit-window-to-buffer indirect-window)))
8780 (select-window (get-buffer-window agenda-buffer))
8781 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8783 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8784 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8785 (org-agenda-check-no-diary)
8786 (let* ((marker (or (org-get-at-bol 'org-marker)
8787 (org-agenda-error)))
8788 (buffer (marker-buffer marker))
8789 (pos (marker-position marker)))
8790 (with-current-buffer buffer
8791 (save-excursion
8792 (goto-char pos)
8793 (funcall 'org-tree-to-indirect-buffer arg)))))
8795 (defvar org-last-heading-marker (make-marker)
8796 "Marker pointing to the headline that last changed its TODO state
8797 by a remote command from the agenda.")
8799 (defun org-agenda-todo-nextset ()
8800 "Switch TODO entry to next sequence."
8801 (interactive)
8802 (org-agenda-todo 'nextset))
8804 (defun org-agenda-todo-previousset ()
8805 "Switch TODO entry to previous sequence."
8806 (interactive)
8807 (org-agenda-todo 'previousset))
8809 (defun org-agenda-todo (&optional arg)
8810 "Cycle TODO state of line at point, also in Org-mode file.
8811 This changes the line at point, all other lines in the agenda referring to
8812 the same tree node, and the headline of the tree node in the Org-mode file."
8813 (interactive "P")
8814 (org-agenda-check-no-diary)
8815 (let* ((col (current-column))
8816 (marker (or (org-get-at-bol 'org-marker)
8817 (org-agenda-error)))
8818 (buffer (marker-buffer marker))
8819 (pos (marker-position marker))
8820 (hdmarker (org-get-at-bol 'org-hd-marker))
8821 (todayp (org-agenda-today-p (org-get-at-bol 'day)))
8822 (inhibit-read-only t)
8823 org-agenda-headline-snapshot-before-repeat newhead just-one)
8824 (org-with-remote-undo buffer
8825 (with-current-buffer buffer
8826 (widen)
8827 (goto-char pos)
8828 (org-show-context 'agenda)
8829 (let ((current-prefix-arg arg))
8830 (call-interactively 'org-todo))
8831 (and (bolp) (forward-char 1))
8832 (setq newhead (org-get-heading))
8833 (when (and (bound-and-true-p
8834 org-agenda-headline-snapshot-before-repeat)
8835 (not (equal org-agenda-headline-snapshot-before-repeat
8836 newhead))
8837 todayp)
8838 (setq newhead org-agenda-headline-snapshot-before-repeat
8839 just-one t))
8840 (save-excursion
8841 (org-back-to-heading)
8842 (move-marker org-last-heading-marker (point))))
8843 (beginning-of-line 1)
8844 (save-window-excursion
8845 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8846 (when (bound-and-true-p org-clock-out-when-done)
8847 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8848 newhead)
8849 (org-agenda-unmark-clocking-task))
8850 (org-move-to-column col)
8851 (org-agenda-mark-clocking-task))))
8853 (defun org-agenda-add-note (&optional arg)
8854 "Add a time-stamped note to the entry at point."
8855 (interactive "P")
8856 (org-agenda-check-no-diary)
8857 (let* ((marker (or (org-get-at-bol 'org-marker)
8858 (org-agenda-error)))
8859 (buffer (marker-buffer marker))
8860 (pos (marker-position marker))
8861 (hdmarker (org-get-at-bol 'org-hd-marker))
8862 (inhibit-read-only t))
8863 (with-current-buffer buffer
8864 (widen)
8865 (goto-char pos)
8866 (org-show-context 'agenda)
8867 (org-add-note))))
8869 (defun org-agenda-change-all-lines (newhead hdmarker
8870 &optional fixface just-this)
8871 "Change all lines in the agenda buffer which match HDMARKER.
8872 The new content of the line will be NEWHEAD (as modified by
8873 `org-agenda-format-item'). HDMARKER is checked with
8874 `equal' against all `org-hd-marker' text properties in the file.
8875 If FIXFACE is non-nil, the face of each item is modified according to
8876 the new TODO state.
8877 If JUST-THIS is non-nil, change just the current line, not all.
8878 If FORCE-TAGS is non nil, the car of it returns the new tags."
8879 (let* ((inhibit-read-only t)
8880 (line (org-current-line))
8881 (org-agenda-buffer (current-buffer))
8882 (thetags (with-current-buffer (marker-buffer hdmarker)
8883 (save-excursion (save-restriction (widen)
8884 (goto-char hdmarker)
8885 (org-get-tags-at)))))
8886 props m pl undone-face done-face finish new dotime level cat tags)
8887 (save-excursion
8888 (goto-char (point-max))
8889 (beginning-of-line 1)
8890 (while (not finish)
8891 (setq finish (bobp))
8892 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8893 (or (not just-this) (= (org-current-line) line))
8894 (equal m hdmarker))
8895 (setq props (text-properties-at (point))
8896 dotime (org-get-at-bol 'dotime)
8897 cat (org-get-at-eol 'org-category 1)
8898 level (org-get-at-bol 'level)
8899 tags thetags
8901 (let ((org-prefix-format-compiled
8902 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8903 org-prefix-format-compiled))
8904 (extra (org-get-at-bol 'extra)))
8905 (with-current-buffer (marker-buffer hdmarker)
8906 (save-excursion
8907 (save-restriction
8908 (widen)
8909 (org-agenda-format-item extra newhead level cat tags dotime)))))
8910 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8911 undone-face (org-get-at-bol 'undone-face)
8912 done-face (org-get-at-bol 'done-face))
8913 (beginning-of-line 1)
8914 (cond
8915 ((equal new "") (delete-region (point) (line-beginning-position 2)))
8916 ((looking-at ".*")
8917 ;; When replacing the whole line, preserve bulk mark
8918 ;; overlay, if any.
8919 (let ((mark (catch :overlay
8920 (dolist (o (overlays-in (point) (+ 2 (point))))
8921 (when (eq (overlay-get o 'type)
8922 'org-marked-entry-overlay)
8923 (throw :overlay o))))))
8924 (replace-match new t t)
8925 (beginning-of-line)
8926 (when mark (move-overlay mark (point) (+ 2 (point)))))
8927 (add-text-properties (point-at-bol) (point-at-eol) props)
8928 (when fixface
8929 (add-text-properties
8930 (point-at-bol) (point-at-eol)
8931 (list 'face
8932 (if org-last-todo-state-is-todo
8933 undone-face done-face))))
8934 (org-agenda-highlight-todo 'line)
8935 (beginning-of-line 1))
8936 (t (error "Line update did not work")))
8937 (save-restriction
8938 (narrow-to-region (point-at-bol) (point-at-eol))
8939 (org-agenda-finalize)))
8940 (beginning-of-line 0)))))
8942 (defun org-agenda-align-tags (&optional line)
8943 "Align all tags in agenda items to `org-agenda-tags-column'."
8944 (let ((inhibit-read-only t) l c)
8945 (save-excursion
8946 (goto-char (if line (point-at-bol) (point-min)))
8947 (while (re-search-forward "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"
8948 (if line (point-at-eol) nil) t)
8949 (add-text-properties
8950 (match-beginning 2) (match-end 2)
8951 (list 'face (delq nil (let ((prop (get-text-property
8952 (match-beginning 2) 'face)))
8953 (or (listp prop) (setq prop (list prop)))
8954 (if (memq 'org-tag prop)
8955 prop
8956 (cons 'org-tag prop))))))
8957 (setq l (- (match-end 2) (match-beginning 2))
8958 c (if (< org-agenda-tags-column 0)
8959 (- (abs org-agenda-tags-column) l)
8960 org-agenda-tags-column))
8961 (delete-region (match-beginning 1) (match-end 1))
8962 (goto-char (match-beginning 1))
8963 (insert (org-add-props
8964 (make-string (max 1 (- c (current-column))) ?\ )
8965 (plist-put (copy-sequence (text-properties-at (point)))
8966 'face nil))))
8967 (goto-char (point-min))
8968 (org-font-lock-add-tag-faces (point-max)))))
8970 (defun org-agenda-priority-up ()
8971 "Increase the priority of line at point, also in Org-mode file."
8972 (interactive)
8973 (org-agenda-priority 'up))
8975 (defun org-agenda-priority-down ()
8976 "Decrease the priority of line at point, also in Org-mode file."
8977 (interactive)
8978 (org-agenda-priority 'down))
8980 (defun org-agenda-priority (&optional force-direction)
8981 "Set the priority of line at point, also in Org-mode file.
8982 This changes the line at point, all other lines in the agenda referring to
8983 the same tree node, and the headline of the tree node in the Org-mode file.
8984 Called with a universal prefix arg, show the priority instead of setting it."
8985 (interactive "P")
8986 (if (equal force-direction '(4))
8987 (org-show-priority)
8988 (unless org-enable-priority-commands
8989 (error "Priority commands are disabled"))
8990 (org-agenda-check-no-diary)
8991 (let* ((col (current-column))
8992 (marker (or (org-get-at-bol 'org-marker)
8993 (org-agenda-error)))
8994 (hdmarker (org-get-at-bol 'org-hd-marker))
8995 (buffer (marker-buffer hdmarker))
8996 (pos (marker-position hdmarker))
8997 (inhibit-read-only t)
8998 newhead)
8999 (org-with-remote-undo buffer
9000 (with-current-buffer buffer
9001 (widen)
9002 (goto-char pos)
9003 (org-show-context 'agenda)
9004 (funcall 'org-priority force-direction)
9005 (end-of-line 1)
9006 (setq newhead (org-get-heading)))
9007 (org-agenda-change-all-lines newhead hdmarker)
9008 (org-move-to-column col)))))
9010 ;; FIXME: should fix the tags property of the agenda line.
9011 (defun org-agenda-set-tags (&optional tag onoff)
9012 "Set tags for the current headline."
9013 (interactive)
9014 (org-agenda-check-no-diary)
9015 (if (and (org-region-active-p) (called-interactively-p 'any))
9016 (call-interactively 'org-change-tag-in-region)
9017 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9018 (org-agenda-error)))
9019 (buffer (marker-buffer hdmarker))
9020 (pos (marker-position hdmarker))
9021 (inhibit-read-only t)
9022 newhead)
9023 (org-with-remote-undo buffer
9024 (with-current-buffer buffer
9025 (widen)
9026 (goto-char pos)
9027 (org-show-context 'agenda)
9028 (if tag
9029 (org-toggle-tag tag onoff)
9030 (call-interactively 'org-set-tags))
9031 (end-of-line 1)
9032 (setq newhead (org-get-heading)))
9033 (org-agenda-change-all-lines newhead hdmarker)
9034 (beginning-of-line 1)))))
9036 (defun org-agenda-set-property ()
9037 "Set a property for the current headline."
9038 (interactive)
9039 (org-agenda-check-no-diary)
9040 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9041 (org-agenda-error)))
9042 (buffer (marker-buffer hdmarker))
9043 (pos (marker-position hdmarker))
9044 (inhibit-read-only t)
9045 newhead)
9046 (org-with-remote-undo buffer
9047 (with-current-buffer buffer
9048 (widen)
9049 (goto-char pos)
9050 (org-show-context 'agenda)
9051 (call-interactively 'org-set-property)))))
9053 (defun org-agenda-set-effort ()
9054 "Set the effort property for the current headline."
9055 (interactive)
9056 (org-agenda-check-no-diary)
9057 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9058 (org-agenda-error)))
9059 (buffer (marker-buffer hdmarker))
9060 (pos (marker-position hdmarker))
9061 (inhibit-read-only t)
9062 newhead)
9063 (org-with-remote-undo buffer
9064 (with-current-buffer buffer
9065 (widen)
9066 (goto-char pos)
9067 (org-show-context 'agenda)
9068 (call-interactively 'org-set-effort)
9069 (end-of-line 1)
9070 (setq newhead (org-get-heading)))
9071 (org-agenda-change-all-lines newhead hdmarker))))
9073 (defun org-agenda-toggle-archive-tag ()
9074 "Toggle the archive tag for the current entry."
9075 (interactive)
9076 (org-agenda-check-no-diary)
9077 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9078 (org-agenda-error)))
9079 (buffer (marker-buffer hdmarker))
9080 (pos (marker-position hdmarker))
9081 (inhibit-read-only t)
9082 newhead)
9083 (org-with-remote-undo buffer
9084 (with-current-buffer buffer
9085 (widen)
9086 (goto-char pos)
9087 (org-show-context 'agenda)
9088 (call-interactively 'org-toggle-archive-tag)
9089 (end-of-line 1)
9090 (setq newhead (org-get-heading)))
9091 (org-agenda-change-all-lines newhead hdmarker)
9092 (beginning-of-line 1))))
9094 (defun org-agenda-do-date-later (arg)
9095 (interactive "P")
9096 (cond
9097 ((or (equal arg '(16))
9098 (memq last-command
9099 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9100 (setq this-command 'org-agenda-date-later-minutes)
9101 (org-agenda-date-later-minutes 1))
9102 ((or (equal arg '(4))
9103 (memq last-command
9104 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9105 (setq this-command 'org-agenda-date-later-hours)
9106 (org-agenda-date-later-hours 1))
9108 (org-agenda-date-later (prefix-numeric-value arg)))))
9110 (defun org-agenda-do-date-earlier (arg)
9111 (interactive "P")
9112 (cond
9113 ((or (equal arg '(16))
9114 (memq last-command
9115 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9116 (setq this-command 'org-agenda-date-earlier-minutes)
9117 (org-agenda-date-earlier-minutes 1))
9118 ((or (equal arg '(4))
9119 (memq last-command
9120 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9121 (setq this-command 'org-agenda-date-earlier-hours)
9122 (org-agenda-date-earlier-hours 1))
9124 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9126 (defun org-agenda-date-later (arg &optional what)
9127 "Change the date of this item to ARG day(s) later."
9128 (interactive "p")
9129 (org-agenda-check-type t 'agenda 'timeline)
9130 (org-agenda-check-no-diary)
9131 (let* ((marker (or (org-get-at-bol 'org-marker)
9132 (org-agenda-error)))
9133 (buffer (marker-buffer marker))
9134 (pos (marker-position marker))
9135 cdate today)
9136 (org-with-remote-undo buffer
9137 (with-current-buffer buffer
9138 (widen)
9139 (goto-char pos)
9140 (if (not (org-at-timestamp-p))
9141 (error "Cannot find time stamp"))
9142 (when (and org-agenda-move-date-from-past-immediately-to-today
9143 (equal arg 1)
9144 (or (not what) (eq what 'day))
9145 (not (save-match-data (org-at-date-range-p))))
9146 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9147 cdate (calendar-absolute-from-gregorian
9148 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9149 today (org-today))
9150 (if (> today cdate)
9151 ;; immediately shift to today
9152 (setq arg (- today cdate))))
9153 (org-timestamp-change arg (or what 'day))
9154 (when (and (org-at-date-range-p)
9155 (re-search-backward org-tr-regexp-both (point-at-bol)))
9156 (let ((end org-last-changed-timestamp))
9157 (org-timestamp-change arg (or what 'day))
9158 (setq org-last-changed-timestamp
9159 (concat org-last-changed-timestamp "--" end)))))
9160 (org-agenda-show-new-time marker org-last-changed-timestamp))
9161 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9163 (defun org-agenda-date-earlier (arg &optional what)
9164 "Change the date of this item to ARG day(s) earlier."
9165 (interactive "p")
9166 (org-agenda-date-later (- arg) what))
9168 (defun org-agenda-date-later-minutes (arg)
9169 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9170 (interactive "p")
9171 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9172 (org-agenda-date-later arg 'minute))
9174 (defun org-agenda-date-earlier-minutes (arg)
9175 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9176 (interactive "p")
9177 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9178 (org-agenda-date-earlier arg 'minute))
9180 (defun org-agenda-date-later-hours (arg)
9181 "Change the time of this item, in hour steps."
9182 (interactive "p")
9183 (org-agenda-date-later arg 'hour))
9185 (defun org-agenda-date-earlier-hours (arg)
9186 "Change the time of this item, in hour steps."
9187 (interactive "p")
9188 (org-agenda-date-earlier arg 'hour))
9190 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9191 "Show new date stamp via text properties."
9192 ;; We use text properties to make this undoable
9193 (let ((inhibit-read-only t))
9194 (setq stamp (concat prefix " => " stamp " "))
9195 (save-excursion
9196 (goto-char (point-max))
9197 (while (not (bobp))
9198 (when (equal marker (org-get-at-bol 'org-marker))
9199 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9200 (org-move-to-column (- (window-width) (length stamp)) t)
9201 (add-text-properties
9202 (1- (point)) (point-at-eol)
9203 (list 'display (org-add-props stamp nil
9204 'face '(secondary-selection default))))
9205 (beginning-of-line 1))
9206 (beginning-of-line 0)))))
9208 (defun org-agenda-date-prompt (arg)
9209 "Change the date of this item. Date is prompted for, with default today.
9210 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9211 be used to request time specification in the time stamp."
9212 (interactive "P")
9213 (org-agenda-check-type t 'agenda 'timeline)
9214 (org-agenda-check-no-diary)
9215 (let* ((marker (or (org-get-at-bol 'org-marker)
9216 (org-agenda-error)))
9217 (buffer (marker-buffer marker))
9218 (pos (marker-position marker)))
9219 (org-with-remote-undo buffer
9220 (with-current-buffer buffer
9221 (widen)
9222 (goto-char pos)
9223 (if (not (org-at-timestamp-p t))
9224 (error "Cannot find time stamp"))
9225 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9226 (org-agenda-show-new-time marker org-last-changed-timestamp))
9227 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9229 (defun org-agenda-schedule (arg &optional time)
9230 "Schedule the item at point.
9231 ARG is passed through to `org-schedule'."
9232 (interactive "P")
9233 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9234 (org-agenda-check-no-diary)
9235 (let* ((marker (or (org-get-at-bol 'org-marker)
9236 (org-agenda-error)))
9237 (type (marker-insertion-type marker))
9238 (buffer (marker-buffer marker))
9239 (pos (marker-position marker))
9241 (set-marker-insertion-type marker t)
9242 (org-with-remote-undo buffer
9243 (with-current-buffer buffer
9244 (widen)
9245 (goto-char pos)
9246 (setq ts (org-schedule arg time)))
9247 (org-agenda-show-new-time marker ts " S"))
9248 (message "%s" ts)))
9250 (defun org-agenda-deadline (arg &optional time)
9251 "Schedule the item at point.
9252 ARG is passed through to `org-deadline'."
9253 (interactive "P")
9254 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9255 (org-agenda-check-no-diary)
9256 (let* ((marker (or (org-get-at-bol 'org-marker)
9257 (org-agenda-error)))
9258 (buffer (marker-buffer marker))
9259 (pos (marker-position marker))
9261 (org-with-remote-undo buffer
9262 (with-current-buffer buffer
9263 (widen)
9264 (goto-char pos)
9265 (setq ts (org-deadline arg time)))
9266 (org-agenda-show-new-time marker ts " D"))
9267 (message "%s" ts)))
9269 (defun org-agenda-clock-in (&optional arg)
9270 "Start the clock on the currently selected item."
9271 (interactive "P")
9272 (org-agenda-check-no-diary)
9273 (if (equal arg '(4))
9274 (org-clock-in arg)
9275 (let* ((marker (or (org-get-at-bol 'org-marker)
9276 (org-agenda-error)))
9277 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9278 (pos (marker-position marker))
9279 (col (current-column))
9280 newhead)
9281 (org-with-remote-undo (marker-buffer marker)
9282 (with-current-buffer (marker-buffer marker)
9283 (widen)
9284 (goto-char pos)
9285 (org-show-context 'agenda)
9286 (org-cycle-hide-drawers 'children)
9287 (org-clock-in arg)
9288 (setq newhead (org-get-heading)))
9289 (org-agenda-change-all-lines newhead hdmarker))
9290 (org-move-to-column col))))
9292 (defun org-agenda-clock-out ()
9293 "Stop the currently running clock."
9294 (interactive)
9295 (unless (marker-buffer org-clock-marker)
9296 (error "No running clock"))
9297 (let ((marker (make-marker)) (col (current-column)) newhead)
9298 (org-with-remote-undo (marker-buffer org-clock-marker)
9299 (with-current-buffer (marker-buffer org-clock-marker)
9300 (save-excursion
9301 (save-restriction
9302 (widen)
9303 (goto-char org-clock-marker)
9304 (org-back-to-heading t)
9305 (move-marker marker (point))
9306 (org-clock-out)
9307 (setq newhead (org-get-heading))))))
9308 (org-agenda-change-all-lines newhead marker)
9309 (move-marker marker nil)
9310 (org-move-to-column col)
9311 (org-agenda-unmark-clocking-task)))
9313 (defun org-agenda-clock-cancel (&optional arg)
9314 "Cancel the currently running clock."
9315 (interactive "P")
9316 (unless (marker-buffer org-clock-marker)
9317 (user-error "No running clock"))
9318 (org-with-remote-undo (marker-buffer org-clock-marker)
9319 (org-clock-cancel)))
9321 (defun org-agenda-clock-goto ()
9322 "Jump to the currently clocked in task within the agenda.
9323 If the currently clocked in task is not listed in the agenda
9324 buffer, display it in another window."
9325 (interactive)
9326 (let (pos)
9327 (mapc (lambda (o)
9328 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9329 (setq pos (overlay-start o))))
9330 (overlays-in (point-min) (point-max)))
9331 (cond (pos (goto-char pos))
9332 ;; If the currently clocked entry is not in the agenda
9333 ;; buffer, we visit it in another window:
9334 ((bound-and-true-p org-clock-current-task)
9335 (org-switch-to-buffer-other-window (org-clock-goto)))
9336 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9338 (defun org-agenda-diary-entry-in-org-file ()
9339 "Make a diary entry in the file `org-agenda-diary-file'."
9340 (let (d1 d2 char (text "") dp1 dp2)
9341 (if (equal (buffer-name) "*Calendar*")
9342 (setq d1 (calendar-cursor-to-date t)
9343 d2 (car calendar-mark-ring))
9344 (setq dp1 (get-text-property (point-at-bol) 'day))
9345 (unless dp1 (user-error "No date defined in current line"))
9346 (setq d1 (calendar-gregorian-from-absolute dp1)
9347 d2 (and (ignore-errors (mark))
9348 (save-excursion
9349 (goto-char (mark))
9350 (setq dp2 (get-text-property (point-at-bol) 'day)))
9351 (calendar-gregorian-from-absolute dp2))))
9352 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9353 (setq char (read-char-exclusive))
9354 (cond
9355 ((equal char ?d)
9356 (setq text (read-string "Day entry: "))
9357 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9358 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9359 ((equal char ?a)
9360 (setq d1 (list (car d1) (nth 1 d1)
9361 (read-number (format "Reference year [%d]: " (nth 2 d1))
9362 (nth 2 d1))))
9363 (setq text (read-string "Anniversary (use %d to show years): "))
9364 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9365 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9366 ((equal char ?b)
9367 (setq text (read-string "Block entry: "))
9368 (unless (and d1 d2 (not (equal d1 d2)))
9369 (user-error "No block of days selected"))
9370 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9371 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9372 ((equal char ?j)
9373 (org-switch-to-buffer-other-window
9374 (find-file-noselect org-agenda-diary-file))
9375 (require 'org-datetree)
9376 (org-datetree-find-date-create d1)
9377 (org-reveal t))
9378 (t (user-error "Invalid selection character `%c'" char)))))
9380 (defcustom org-agenda-insert-diary-strategy 'date-tree
9381 "Where in `org-agenda-diary-file' should new entries be added?
9382 Valid values:
9384 date-tree in the date tree, as first child of the date
9385 date-tree-last in the date tree, as last child of the date
9386 top-level as top-level entries at the end of the file."
9387 :group 'org-agenda
9388 :type '(choice
9389 (const :tag "first in a date tree" date-tree)
9390 (const :tag "last in a date tree" date-tree-last)
9391 (const :tag "as top level at end of file" top-level)))
9393 (defcustom org-agenda-insert-diary-extract-time nil
9394 "Non-nil means extract any time specification from the diary entry."
9395 :group 'org-agenda
9396 :version "24.1"
9397 :type 'boolean)
9399 (defcustom org-agenda-bulk-mark-char ">"
9400 "A single-character string to be used as the bulk mark."
9401 :group 'org-agenda
9402 :version "24.1"
9403 :type 'string)
9405 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9406 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9407 If TEXT is not empty, it will become the headline of the new entry, and
9408 the resulting entry will not be shown. When TEXT is empty, switch to
9409 `org-agenda-diary-file' and let the user finish the entry there."
9410 (let ((cw (current-window-configuration)))
9411 (org-switch-to-buffer-other-window
9412 (find-file-noselect org-agenda-diary-file))
9413 (widen)
9414 (goto-char (point-min))
9415 (cond
9416 ((eq type 'anniversary)
9417 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9418 (progn
9419 (or (org-at-heading-p t)
9420 (progn
9421 (outline-next-heading)
9422 (insert "* Anniversaries\n\n")
9423 (beginning-of-line -1)))))
9424 (outline-next-heading)
9425 (org-back-over-empty-lines)
9426 (backward-char 1)
9427 (insert "\n")
9428 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9429 (nth 2 d1) (car d1) (nth 1 d1) text)))
9430 ((eq type 'day)
9431 (let ((org-prefix-has-time t)
9432 (org-agenda-time-leading-zero t)
9433 fmt time time2)
9434 (if org-agenda-insert-diary-extract-time
9435 ;; Use org-agenda-format-item to parse text for a time-range and
9436 ;; remove it. FIXME: This is a hack, we should refactor
9437 ;; that function to make time extraction available separately
9438 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9439 time (get-text-property 0 'time fmt)
9440 time2 (if (> (length time) 0)
9441 ;; split-string removes trailing ...... if
9442 ;; no end time given. First space
9443 ;; separates time from date.
9444 (concat " " (car (split-string time "\\.")))
9445 nil)
9446 text (get-text-property 0 'txt fmt)))
9447 (if (eq org-agenda-insert-diary-strategy 'top-level)
9448 (org-agenda-insert-diary-as-top-level text)
9449 (require 'org-datetree)
9450 (org-datetree-find-date-create d1)
9451 (org-agenda-insert-diary-make-new-entry text))
9452 (org-insert-time-stamp (org-time-from-absolute
9453 (calendar-absolute-from-gregorian d1))
9454 nil nil nil nil time2))
9455 (end-of-line 0))
9456 ((eq type 'block)
9457 (if (> (calendar-absolute-from-gregorian d1)
9458 (calendar-absolute-from-gregorian d2))
9459 (setq d1 (prog1 d2 (setq d2 d1))))
9460 (if (eq org-agenda-insert-diary-strategy 'top-level)
9461 (org-agenda-insert-diary-as-top-level text)
9462 (require 'org-datetree)
9463 (org-datetree-find-date-create d1)
9464 (org-agenda-insert-diary-make-new-entry text))
9465 (org-insert-time-stamp (org-time-from-absolute
9466 (calendar-absolute-from-gregorian d1)))
9467 (insert "--")
9468 (org-insert-time-stamp (org-time-from-absolute
9469 (calendar-absolute-from-gregorian d2)))
9470 (end-of-line 0)))
9471 (if (string-match "\\S-" text)
9472 (progn
9473 (set-window-configuration cw)
9474 (message "%s entry added to %s"
9475 (capitalize (symbol-name type))
9476 (abbreviate-file-name org-agenda-diary-file)))
9477 (org-reveal t)
9478 (message "Please finish entry here"))))
9480 (defun org-agenda-insert-diary-as-top-level (text)
9481 "Make new entry as a top-level entry at the end of the file.
9482 Add TEXT as headline, and position the cursor in the second line so that
9483 a timestamp can be added there."
9484 (widen)
9485 (goto-char (point-max))
9486 (unless (bolp) (insert "\n"))
9487 (org-insert-heading nil t t)
9488 (insert text)
9489 (org-end-of-meta-data)
9490 (unless (bolp) (insert "\n"))
9491 (when org-adapt-indentation (indent-to-column 2)))
9493 (defun org-agenda-insert-diary-make-new-entry (text)
9494 "Make a new entry with TEXT as a child of the current subtree.
9495 Position the point in the heading's first body line so that
9496 a timestamp can be added there."
9497 (cond
9498 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9499 (end-of-line)
9500 (org-insert-heading '(4) t)
9501 (org-do-demote))
9503 (outline-next-heading)
9504 (org-back-over-empty-lines)
9505 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9506 (org-insert-heading nil t)
9507 (org-do-demote)))
9508 (let ((col (current-column)))
9509 (insert text)
9510 (org-end-of-meta-data)
9511 ;; Ensure point is left on a blank line, at proper indentation.
9512 (unless (bolp) (insert "\n"))
9513 (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9514 (when org-adapt-indentation (indent-to-column col)))
9515 (org-show-set-visibility 'lineage))
9517 (defun org-agenda-diary-entry ()
9518 "Make a diary entry, like the `i' command from the calendar.
9519 All the standard commands work: block, weekly etc.
9520 When `org-agenda-diary-file' points to a file,
9521 `org-agenda-diary-entry-in-org-file' is called instead to create
9522 entries in that Org-mode file."
9523 (interactive)
9524 (if (not (eq org-agenda-diary-file 'diary-file))
9525 (org-agenda-diary-entry-in-org-file)
9526 (require 'diary-lib)
9527 (let* ((char (progn
9528 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9529 (read-char-exclusive)))
9530 (cmd (cdr (assoc char
9531 '((?d . diary-insert-entry)
9532 (?w . diary-insert-weekly-entry)
9533 (?m . diary-insert-monthly-entry)
9534 (?y . diary-insert-yearly-entry)
9535 (?a . diary-insert-anniversary-entry)
9536 (?b . diary-insert-block-entry)
9537 (?c . diary-insert-cyclic-entry)))))
9538 (oldf (symbol-function 'calendar-cursor-to-date))
9539 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9540 (point (point))
9541 (mark (or (mark t) (point))))
9542 (unless cmd
9543 (user-error "No command associated with <%c>" char))
9544 (unless (and (get-text-property point 'day)
9545 (or (not (equal ?b char))
9546 (get-text-property mark 'day)))
9547 (user-error "Don't know which date to use for diary entry"))
9548 ;; We implement this by hacking the `calendar-cursor-to-date' function
9549 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9550 (let ((calendar-mark-ring
9551 (list (calendar-gregorian-from-absolute
9552 (or (get-text-property mark 'day)
9553 (get-text-property point 'day))))))
9554 (unwind-protect
9555 (progn
9556 (fset 'calendar-cursor-to-date
9557 (lambda (&optional error dummy)
9558 (calendar-gregorian-from-absolute
9559 (get-text-property point 'day))))
9560 (call-interactively cmd))
9561 (fset 'calendar-cursor-to-date oldf))))))
9563 (defun org-agenda-execute-calendar-command (cmd)
9564 "Execute a calendar command from the agenda with date from cursor."
9565 (org-agenda-check-type t 'agenda 'timeline)
9566 (require 'diary-lib)
9567 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9568 (user-error "Don't know which date to use for the calendar command"))
9569 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9570 (point (point))
9571 (date (calendar-gregorian-from-absolute
9572 (get-text-property point 'day)))
9573 ;; the following 2 vars are needed in the calendar
9574 (displayed-month (car date))
9575 (displayed-year (nth 2 date)))
9576 (unwind-protect
9577 (progn
9578 (fset 'calendar-cursor-to-date
9579 (lambda (&optional error dummy)
9580 (calendar-gregorian-from-absolute
9581 (get-text-property point 'day))))
9582 (call-interactively cmd))
9583 (fset 'calendar-cursor-to-date oldf))))
9585 (defun org-agenda-phases-of-moon ()
9586 "Display the phases of the moon for the 3 months around the cursor date."
9587 (interactive)
9588 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9590 (defun org-agenda-holidays ()
9591 "Display the holidays for the 3 months around the cursor date."
9592 (interactive)
9593 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9595 (defvar calendar-longitude) ; defined in calendar.el
9596 (defvar calendar-latitude) ; defined in calendar.el
9597 (defvar calendar-location-name) ; defined in calendar.el
9599 (defun org-agenda-sunrise-sunset (arg)
9600 "Display sunrise and sunset for the cursor date.
9601 Latitude and longitude can be specified with the variables
9602 `calendar-latitude' and `calendar-longitude'. When called with prefix
9603 argument, latitude and longitude will be prompted for."
9604 (interactive "P")
9605 (require 'solar)
9606 (let ((calendar-longitude (if arg nil calendar-longitude))
9607 (calendar-latitude (if arg nil calendar-latitude))
9608 (calendar-location-name
9609 (if arg "the given coordinates" calendar-location-name)))
9610 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9612 (defun org-agenda-goto-calendar ()
9613 "Open the Emacs calendar with the date at the cursor."
9614 (interactive)
9615 (org-agenda-check-type t 'agenda 'timeline)
9616 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9617 (user-error "Don't know which date to open in calendar")))
9618 (date (calendar-gregorian-from-absolute day))
9619 (calendar-move-hook nil)
9620 (calendar-view-holidays-initially-flag nil)
9621 (calendar-view-diary-initially-flag nil))
9622 (calendar)
9623 (calendar-goto-date date)))
9625 ;;;###autoload
9626 (defun org-calendar-goto-agenda ()
9627 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9628 This is a command that has to be installed in `calendar-mode-map'."
9629 (interactive)
9630 ;; Temporarily disable sticky agenda since user clearly wants to
9631 ;; refresh view anyway.
9632 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9633 (org-agenda-sticky nil))
9634 (org-agenda-list nil (calendar-absolute-from-gregorian
9635 (calendar-cursor-to-date))
9636 nil)))
9638 (defun org-agenda-convert-date ()
9639 (interactive)
9640 (org-agenda-check-type t 'agenda 'timeline)
9641 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9642 date s)
9643 (unless day
9644 (user-error "Don't know which date to convert"))
9645 (setq date (calendar-gregorian-from-absolute day))
9646 (setq s (concat
9647 "Gregorian: " (calendar-date-string date) "\n"
9648 "ISO: " (calendar-iso-date-string date) "\n"
9649 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9650 "Julian: " (calendar-julian-date-string date) "\n"
9651 "Astron. JD: " (calendar-astro-date-string date)
9652 " (Julian date number at noon UTC)\n"
9653 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9654 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9655 "French: " (calendar-french-date-string date) "\n"
9656 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9657 "Mayan: " (calendar-mayan-date-string date) "\n"
9658 "Coptic: " (calendar-coptic-date-string date) "\n"
9659 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9660 "Persian: " (calendar-persian-date-string date) "\n"
9661 "Chinese: " (calendar-chinese-date-string date) "\n"))
9662 (with-output-to-temp-buffer "*Dates*"
9663 (princ s))
9664 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9666 ;;; Bulk commands
9668 (defun org-agenda-bulk-marked-p ()
9669 "Non-nil when current entry is marked for bulk action."
9670 (eq (get-char-property (point-at-bol) 'type)
9671 'org-marked-entry-overlay))
9673 (defun org-agenda-bulk-mark (&optional arg)
9674 "Mark the entry at point for future bulk action."
9675 (interactive "p")
9676 (dotimes (i (or arg 1))
9677 (unless (org-get-at-bol 'org-agenda-diary-link)
9678 (let* ((m (org-get-at-bol 'org-hd-marker))
9680 (unless (org-agenda-bulk-marked-p)
9681 (unless m (user-error "Nothing to mark at point"))
9682 (push m org-agenda-bulk-marked-entries)
9683 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9684 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9685 (org-get-todo-face "TODO")
9686 'evaporate)
9687 (overlay-put ov 'type 'org-marked-entry-overlay))
9688 (end-of-line 1)
9689 (or (ignore-errors
9690 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9691 (beginning-of-line 2))
9692 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9693 (beginning-of-line 2))
9694 (message "%d entries marked for bulk action"
9695 (length org-agenda-bulk-marked-entries))))))
9697 (defun org-agenda-bulk-mark-all ()
9698 "Mark all entries for future agenda bulk action."
9699 (interactive)
9700 (org-agenda-bulk-mark-regexp "."))
9702 (defun org-agenda-bulk-mark-regexp (regexp)
9703 "Mark entries matching REGEXP for future agenda bulk action."
9704 (interactive "sMark entries matching regexp: ")
9705 (let ((entries-marked 0) txt-at-point)
9706 (save-excursion
9707 (goto-char (point-min))
9708 (goto-char (next-single-property-change (point) 'org-hd-marker))
9709 (while (and (re-search-forward regexp nil t)
9710 (setq txt-at-point (get-text-property (point) 'txt)))
9711 (when (string-match regexp txt-at-point)
9712 (setq entries-marked (1+ entries-marked))
9713 (call-interactively 'org-agenda-bulk-mark))))
9714 (if (not entries-marked)
9715 (message "No entry matching this regexp."))))
9717 (defun org-agenda-bulk-unmark (&optional arg)
9718 "Unmark the entry at point for future bulk action."
9719 (interactive "P")
9720 (if arg
9721 (org-agenda-bulk-unmark-all)
9722 (cond ((org-agenda-bulk-marked-p)
9723 (org-agenda-bulk-remove-overlays
9724 (point-at-bol) (+ 2 (point-at-bol)))
9725 (setq org-agenda-bulk-marked-entries
9726 (delete (org-get-at-bol 'org-hd-marker)
9727 org-agenda-bulk-marked-entries))
9728 (end-of-line 1)
9729 (or (ignore-errors
9730 (goto-char (next-single-property-change (point) 'txt)))
9731 (beginning-of-line 2))
9732 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9733 (beginning-of-line 2))
9734 (message "%d entries left marked for bulk action"
9735 (length org-agenda-bulk-marked-entries)))
9736 (t (message "No entry to unmark here")))))
9738 (defun org-agenda-bulk-toggle-all ()
9739 "Toggle all marks for bulk action."
9740 (interactive)
9741 (save-excursion
9742 (goto-char (point-min))
9743 (while (ignore-errors
9744 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9745 (org-agenda-bulk-toggle))))
9747 (defun org-agenda-bulk-toggle ()
9748 "Toggle the mark at point for bulk action."
9749 (interactive)
9750 (if (org-agenda-bulk-marked-p)
9751 (org-agenda-bulk-unmark)
9752 (org-agenda-bulk-mark)))
9754 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9755 "Remove the mark overlays between BEG and END in the agenda buffer.
9756 BEG and END default to the buffer limits.
9758 This only removes the overlays, it does not remove the markers
9759 from the list in `org-agenda-bulk-marked-entries'."
9760 (interactive)
9761 (mapc (lambda (ov)
9762 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9763 (delete-overlay ov)))
9764 (overlays-in (or beg (point-min)) (or end (point-max)))))
9766 (defun org-agenda-bulk-unmark-all ()
9767 "Remove all marks in the agenda buffer.
9768 This will remove the markers and the overlays."
9769 (interactive)
9770 (if (null org-agenda-bulk-marked-entries)
9771 (message "No entry to unmark")
9772 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9773 (setq org-agenda-bulk-marked-entries nil)
9774 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9776 (defcustom org-agenda-persistent-marks nil
9777 "Non-nil means marked items will stay marked after a bulk action.
9778 You can toggle this interactively by typing `p' when prompted for a
9779 bulk action."
9780 :group 'org-agenda
9781 :version "24.1"
9782 :type 'boolean)
9784 (defun org-agenda-bulk-action (&optional arg)
9785 "Execute an remote-editing action on all marked entries.
9786 The prefix arg is passed through to the command if possible."
9787 (interactive "P")
9788 ;; Make sure we have markers, and only valid ones
9789 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9790 (mapc
9791 (lambda (m)
9792 (unless (and (markerp m)
9793 (marker-buffer m)
9794 (buffer-live-p (marker-buffer m))
9795 (marker-position m))
9796 (user-error "Marker %s for bulk command is invalid" m)))
9797 org-agenda-bulk-marked-entries)
9799 ;; Prompt for the bulk command
9800 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9801 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9802 "[S]catter [f]unction "
9803 (when org-agenda-bulk-custom-functions
9804 (concat " Custom: ["
9805 (mapconcat (lambda(f) (char-to-string (car f)))
9806 org-agenda-bulk-custom-functions "")
9807 "]"))))
9808 (catch 'exit
9809 (let* ((action (read-char-exclusive))
9810 (org-log-refile (if org-log-refile 'time nil))
9811 (entries (reverse org-agenda-bulk-marked-entries))
9812 (org-overriding-default-time
9813 (if (get-text-property (point) 'org-agenda-date-header)
9814 (org-get-cursor-date)))
9815 redo-at-end
9816 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9817 (cond
9818 ((equal action ?p)
9819 (let ((org-agenda-persistent-marks
9820 (not org-agenda-persistent-marks)))
9821 (org-agenda-bulk-action)
9822 (throw 'exit nil)))
9824 ((equal action ?$)
9825 (setq cmd '(org-agenda-archive)))
9827 ((equal action ?A)
9828 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9830 ((member action '(?r ?w))
9831 (setq rfloc (org-refile-get-location
9832 "Refile to"
9833 (marker-buffer (car entries))
9834 org-refile-allow-creating-parent-nodes))
9835 (if (nth 3 rfloc)
9836 (setcar (nthcdr 3 rfloc)
9837 (move-marker (make-marker) (nth 3 rfloc)
9838 (or (get-file-buffer (nth 1 rfloc))
9839 (find-buffer-visiting (nth 1 rfloc))
9840 (error "This should not happen")))))
9842 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9843 redo-at-end t))
9845 ((equal action ?t)
9846 (setq state (completing-read
9847 "Todo state: "
9848 (with-current-buffer (marker-buffer (car entries))
9849 (mapcar #'list org-todo-keywords-1))))
9850 (setq cmd `(let ((org-inhibit-blocking t)
9851 (org-inhibit-logging 'note))
9852 (org-agenda-todo ,state))))
9854 ((memq action '(?- ?+))
9855 (setq tag (completing-read
9856 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9857 (with-current-buffer (marker-buffer (car entries))
9858 (delq nil
9859 (mapcar (lambda (x) (and (stringp (car x)) x))
9860 org-current-tag-alist)))))
9861 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9863 ((memq action '(?s ?d))
9864 (let* ((time
9865 (unless arg
9866 (org-read-date
9867 nil nil nil
9868 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9869 org-overriding-default-time)))
9870 (c1 (if (eq action ?s) 'org-agenda-schedule
9871 'org-agenda-deadline)))
9872 ;; Make sure to not prompt for a note when bulk
9873 ;; rescheduling as Org cannot cope with simultaneous Org.
9874 ;; Besides, it could be annoying depending on the number
9875 ;; of items re-scheduled.
9876 (setq cmd `(eval '(let ((org-log-reschedule
9877 (and org-log-reschedule 'time)))
9878 (,c1 arg ,time))))))
9880 ((equal action ?S)
9881 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9882 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9883 (let ((days (read-number
9884 (format "Scatter tasks across how many %sdays: "
9885 (if arg "week" "")) 7)))
9886 (setq cmd
9887 `(let ((distance (1+ (random ,days))))
9888 (if arg
9889 (let ((dist distance)
9890 (day-of-week
9891 (calendar-day-of-week
9892 (calendar-gregorian-from-absolute (org-today)))))
9893 (dotimes (i (1+ dist))
9894 (while (member day-of-week org-agenda-weekend-days)
9895 (incf distance)
9896 (incf day-of-week)
9897 (if (= day-of-week 7)
9898 (setq day-of-week 0)))
9899 (incf day-of-week)
9900 (if (= day-of-week 7)
9901 (setq day-of-week 0)))))
9902 ;; silently fail when try to replan a sexp entry
9903 (condition-case nil
9904 (let* ((date (calendar-gregorian-from-absolute
9905 (+ (org-today) distance)))
9906 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9907 (nth 2 date))))
9908 (org-agenda-schedule nil time))
9909 (error nil)))))))
9911 ((assoc action org-agenda-bulk-custom-functions)
9912 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9913 redo-at-end t))
9915 ((equal action ?f)
9916 (setq cmd (list (intern
9917 (completing-read "Function: "
9918 obarray 'fboundp t nil nil)))))
9920 (t (user-error "Invalid bulk action")))
9922 ;; Sort the markers, to make sure that parents are handled before children
9923 (setq entries (sort entries
9924 (lambda (a b)
9925 (cond
9926 ((equal (marker-buffer a) (marker-buffer b))
9927 (< (marker-position a) (marker-position b)))
9929 (string< (buffer-name (marker-buffer a))
9930 (buffer-name (marker-buffer b))))))))
9932 ;; Now loop over all markers and apply cmd
9933 (while (setq e (pop entries))
9934 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9935 (if (not pos)
9936 (progn (message "Skipping removed entry at %s" e)
9937 (setq cntskip (1+ cntskip)))
9938 (goto-char pos)
9939 (let (org-loop-over-headlines-in-active-region)
9940 (eval cmd))
9941 ;; `post-command-hook' is not run yet. We make sure any
9942 ;; pending log note is processed.
9943 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9944 (memq 'org-add-log-note post-command-hook))
9945 (org-add-log-note))
9946 (setq cnt (1+ cnt))))
9947 (when redo-at-end (org-agenda-redo))
9948 (unless org-agenda-persistent-marks
9949 (org-agenda-bulk-unmark-all))
9950 (message "Acted on %d entries%s%s"
9952 (if (= cntskip 0)
9954 (format ", skipped %d (disappeared before their turn)"
9955 cntskip))
9956 (if (not org-agenda-persistent-marks)
9957 "" " (kept marked)"))))))
9959 (defun org-agenda-capture (&optional with-time)
9960 "Call `org-capture' with the date at point.
9961 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9962 current HH:MM time."
9963 (interactive "P")
9964 (if (not (eq major-mode 'org-agenda-mode))
9965 (user-error "You cannot do this outside of agenda buffers")
9966 (let ((org-overriding-default-time
9967 (org-get-cursor-date (equal with-time 1))))
9968 (call-interactively 'org-capture))))
9970 ;;; Dragging agenda lines forward/backward
9972 (defun org-agenda-reapply-filters ()
9973 "Re-apply all agenda filters."
9974 (mapcar
9975 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
9976 `((,org-agenda-tag-filter tag)
9977 (,org-agenda-category-filter category)
9978 (,org-agenda-regexp-filter regexp)
9979 (,org-agenda-effort-filter effort)
9980 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9981 (,(get 'org-agenda-category-filter :preset-filter) category)
9982 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9983 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9985 (defun org-agenda-drag-line-forward (arg &optional backward)
9986 "Drag an agenda line forward by ARG lines.
9987 When the optional argument `backward' is non-nil, move backward."
9988 (interactive "p")
9989 (let ((inhibit-read-only t) lst line)
9990 (if (or (not (get-text-property (point) 'txt))
9991 (save-excursion
9992 (dotimes (n arg)
9993 (move-beginning-of-line (if backward 0 2))
9994 (push (not (get-text-property (point) 'txt)) lst))
9995 (delq nil lst)))
9996 (message "Cannot move line forward")
9997 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9998 (move-beginning-of-line 1)
9999 (setq line (buffer-substring (point) end))
10000 (delete-region (point) end)
10001 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10002 (insert line)
10003 (org-agenda-reapply-filters)
10004 (org-agenda-mark-clocking-task)
10005 (move-beginning-of-line 0)))))
10007 (defun org-agenda-drag-line-backward (arg)
10008 "Drag an agenda line backward by ARG lines."
10009 (interactive "p")
10010 (org-agenda-drag-line-forward arg t))
10012 ;;; Flagging notes
10014 (defun org-agenda-show-the-flagging-note ()
10015 "Display the flagging note in the other window.
10016 When called a second time in direct sequence, offer to remove the FLAGGING
10017 tag and (if present) the flagging note."
10018 (interactive)
10019 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10020 (win (selected-window))
10021 note heading newhead)
10022 (unless hdmarker
10023 (user-error "No linked entry at point"))
10024 (if (and (eq this-command last-command)
10025 (y-or-n-p "Unflag and remove any flagging note? "))
10026 (progn
10027 (org-agenda-remove-flag hdmarker)
10028 (let ((win (get-buffer-window "*Flagging Note*")))
10029 (and win (delete-window win)))
10030 (message "Entry unflagged"))
10031 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10032 (unless note
10033 (user-error "No flagging note"))
10034 (org-kill-new note)
10035 (org-switch-to-buffer-other-window "*Flagging Note*")
10036 (erase-buffer)
10037 (insert note)
10038 (goto-char (point-min))
10039 (while (re-search-forward "\\\\n" nil t)
10040 (replace-match "\n" t t))
10041 (goto-char (point-min))
10042 (select-window win)
10043 (message "%s" (substitute-command-keys "Flagging note pushed to \
10044 kill ring. Press \\[org-agenda-show-the-flagging-note] again to remove \
10045 tag and note")))))
10047 (defun org-agenda-remove-flag (marker)
10048 "Remove the FLAGGED tag and any flagging note in the entry."
10049 (let (newhead)
10050 (org-with-point-at marker
10051 (org-toggle-tag "FLAGGED" 'off)
10052 (org-entry-delete nil "THEFLAGGINGNOTE")
10053 (setq newhead (org-get-heading)))
10054 (org-agenda-change-all-lines newhead marker)
10055 (message "Entry unflagged")))
10057 (defun org-agenda-get-any-marker (&optional pos)
10058 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10059 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10061 ;;; Appointment reminders
10063 (defvar appt-time-msg-list) ; defined in appt.el
10065 ;;;###autoload
10066 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10067 "Activate appointments found in `org-agenda-files'.
10068 With a \\[universal-argument] prefix, refresh the list of
10069 appointments.
10071 If FILTER is t, interactively prompt the user for a regular
10072 expression, and filter out entries that don't match it.
10074 If FILTER is a string, use this string as a regular expression
10075 for filtering entries out.
10077 If FILTER is a function, filter out entries against which
10078 calling the function returns nil. This function takes one
10079 argument: an entry from `org-agenda-get-day-entries'.
10081 FILTER can also be an alist with the car of each cell being
10082 either `headline' or `category'. For example:
10084 \\='((headline \"IMPORTANT\")
10085 (category \"Work\"))
10087 will only add headlines containing IMPORTANT or headlines
10088 belonging to the \"Work\" category.
10090 ARGS are symbols indicating what kind of entries to consider.
10091 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10092 \(i.e., deadlines and scheduled items with a hh:mm specification)
10093 and :timestamp entries. See the docstring of `org-diary' for
10094 details and examples.
10096 If an entry has a APPT_WARNTIME property, its value will be used
10097 to override `appt-message-warning-time'."
10098 (interactive "P")
10099 (if refresh (setq appt-time-msg-list nil))
10100 (if (eq filter t)
10101 (setq filter (read-from-minibuffer "Regexp filter: ")))
10102 (let* ((cnt 0) ; count added events
10103 (scope (or args '(:deadline* :scheduled* :timestamp)))
10104 (org-agenda-new-buffers nil)
10105 (org-deadline-warning-days 0)
10106 ;; Do not use `org-today' here because appt only takes
10107 ;; time and without date as argument, so it may pass wrong
10108 ;; information otherwise
10109 (today (org-date-to-gregorian
10110 (time-to-days (current-time))))
10111 (org-agenda-restrict nil)
10112 (files (org-agenda-files 'unrestricted)) entries file
10113 (org-agenda-buffer nil))
10114 ;; Get all entries which may contain an appt
10115 (org-agenda-prepare-buffers files)
10116 (while (setq file (pop files))
10117 (setq entries
10118 (delq nil
10119 (append entries
10120 (apply 'org-agenda-get-day-entries
10121 file today scope)))))
10122 ;; Map thru entries and find if we should filter them out
10123 (mapc
10124 (lambda (x)
10125 (let* ((evt (org-trim
10126 (replace-regexp-in-string
10127 org-bracket-link-regexp "\\3"
10128 (or (get-text-property 1 'txt x) ""))))
10129 (cat (get-text-property (1- (length x)) 'org-category x))
10130 (tod (get-text-property 1 'time-of-day x))
10131 (ok (or (null filter)
10132 (and (stringp filter) (string-match filter evt))
10133 (and (functionp filter) (funcall filter x))
10134 (and (listp filter)
10135 (let ((cat-filter (cadr (assoc 'category filter)))
10136 (evt-filter (cadr (assoc 'headline filter))))
10137 (or (and (stringp cat-filter)
10138 (string-match cat-filter cat))
10139 (and (stringp evt-filter)
10140 (string-match evt-filter evt)))))))
10141 (wrn (get-text-property 1 'warntime x)))
10142 ;; FIXME: Shall we remove text-properties for the appt text?
10143 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10144 (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
10145 (setq tod (concat "00" (number-to-string tod)))
10146 (setq tod (when (string-match
10147 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10148 (concat (match-string 1 tod) ":"
10149 (match-string 2 tod))))
10150 (when (if (version< emacs-version "23.3")
10151 (appt-add tod evt)
10152 (appt-add tod evt wrn))
10153 (setq cnt (1+ cnt))))))
10154 entries)
10155 (org-release-buffers org-agenda-new-buffers)
10156 (if (eq cnt 0)
10157 (message "No event to add")
10158 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10160 (defun org-agenda-today-p (date)
10161 "Non nil when DATE means today.
10162 DATE is either a list of the form (month day year) or a number of
10163 days as returned by `calendar-absolute-from-gregorian' or
10164 `org-today'. This function considers `org-extend-today-until'
10165 when defining today."
10166 (eq (org-today)
10167 (if (consp date) (calendar-absolute-from-gregorian date) date)))
10169 (defun org-agenda-todo-yesterday (&optional arg)
10170 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10171 (interactive "P")
10172 (let* ((org-use-effective-time t)
10173 (hour (third (decode-time
10174 (org-current-time))))
10175 (org-extend-today-until (1+ hour)))
10176 (org-agenda-todo arg)))
10178 (provide 'org-agenda)
10180 ;;; org-agenda.el ends here