Fix bug when update timestamps on filtered agenda entries
[org-mode.git] / lisp / org-agenda.el
blobf183728885da440ce3fc7ed7933d699b09bdbaf5
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2014 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-absolute-from-iso "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-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-pop-to-buffer-same-window "org-compat"
83 (&optional buffer-or-name norecord label))
84 (declare-function org-agenda-columns "org-colview" ())
85 (declare-function org-add-archive-files "org-archive" (files))
86 (declare-function org-capture "org-capture" (&optional goto keys))
88 (defvar calendar-mode-map) ; defined in calendar.el
89 (defvar org-clock-current-task nil) ; defined in org-clock.el
90 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
91 (defvar org-habit-show-habits) ; defined in org-habit.el
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 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
100 (org-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 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
265 (org-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 "Regexp filter preset"
364 (const org-agenda-regexp-filter-preset)
365 (list
366 (const :format "" quote)
367 (repeat
368 (string :tag "+regexp or -regexp"))))
369 (list :tag "Set daily/weekly entry types"
370 (const org-agenda-entry-types)
371 (list
372 (const :format "" quote)
373 (set :greedy t :value ,org-agenda-entry-types
374 (const :deadline)
375 (const :scheduled)
376 (const :deadline*)
377 (const :scheduled*)
378 (const :timestamp)
379 (const :sexp))))
380 (list :tag "Standard skipping condition"
381 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
382 (const org-agenda-skip-function)
383 (list
384 (const :format "" quote)
385 (list
386 (choice
387 :tag "Skipping range"
388 (const :tag "Skip entry" org-agenda-skip-entry-if)
389 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
390 (repeat :inline t :tag "Conditions for skipping"
391 (choice
392 :tag "Condition type"
393 (list :tag "Regexp matches" :inline t (const :format "" regexp) (regexp))
394 (list :tag "Regexp does not match" :inline t (const :format "" notregexp) (regexp))
395 (list :tag "TODO state is" :inline t
396 (const todo)
397 (choice
398 (const :tag "any not-done state" todo)
399 (const :tag "any done state" done)
400 (const :tag "any state" any)
401 (list :tag "Keyword list"
402 (const :format "" quote)
403 (repeat (string :tag "Keyword")))))
404 (list :tag "TODO state is not" :inline t
405 (const nottodo)
406 (choice
407 (const :tag "any not-done state" todo)
408 (const :tag "any done state" done)
409 (const :tag "any state" any)
410 (list :tag "Keyword list"
411 (const :format "" quote)
412 (repeat (string :tag "Keyword")))))
413 (const :tag "scheduled" scheduled)
414 (const :tag "not scheduled" notscheduled)
415 (const :tag "deadline" deadline)
416 (const :tag "no deadline" notdeadline)
417 (const :tag "timestamp" timestamp)
418 (const :tag "no timestamp" nottimestamp))))))
419 (list :tag "Non-standard skipping condition"
420 :value (org-agenda-skip-function)
421 (const org-agenda-skip-function)
422 (sexp :tag "Function or form (quoted!)"))
423 (list :tag "Any variable"
424 (variable :tag "Variable")
425 (sexp :tag "Value (sexp)"))))
426 "Selection of examples for agenda command settings.
427 This will be spliced into the custom type of
428 `org-agenda-custom-commands'.")
431 (defcustom org-agenda-custom-commands
432 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
433 "Custom commands for the agenda.
434 These commands will be offered on the splash screen displayed by the
435 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
437 (key desc type match settings files)
439 key The key (one or more characters as a string) to be associated
440 with the command.
441 desc A description of the command, when omitted or nil, a default
442 description is built using MATCH.
443 type The command type, any of the following symbols:
444 agenda The daily/weekly agenda.
445 todo Entries with a specific TODO keyword, in all agenda files.
446 search Entries containing search words entry or headline.
447 tags Tags/Property/TODO match in all agenda files.
448 tags-todo Tags/P/T match in all agenda files, TODO entries only.
449 todo-tree Sparse tree of specific TODO keyword in *current* file.
450 tags-tree Sparse tree with all tags matches in *current* file.
451 occur-tree Occur sparse tree for *current* file.
452 ... A user-defined function.
453 match What to search for:
454 - a single keyword for TODO keyword searches
455 - a tags match expression for tags searches
456 - a word search expression for text searches.
457 - a regular expression for occur searches
458 For all other commands, this should be the empty string.
459 settings A list of option settings, similar to that in a let form, so like
460 this: ((opt1 val1) (opt2 val2) ...). The values will be
461 evaluated at the moment of execution, so quote them when needed.
462 files A list of files file to write the produced agenda buffer to
463 with the command `org-store-agenda-views'.
464 If a file name ends in \".html\", an HTML version of the buffer
465 is written out. If it ends in \".ps\", a postscript version is
466 produced. Otherwise, only the plain text is written to the file.
468 You can also define a set of commands, to create a composite agenda buffer.
469 In this case, an entry looks like this:
471 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
473 where
475 desc A description string to be displayed in the dispatcher menu.
476 cmd An agenda command, similar to the above. However, tree commands
477 are not allowed, but instead you can get agenda and global todo list.
478 So valid commands for a set are:
479 (agenda \"\" settings)
480 (alltodo \"\" settings)
481 (stuck \"\" settings)
482 (todo \"match\" settings files)
483 (search \"match\" settings files)
484 (tags \"match\" settings files)
485 (tags-todo \"match\" settings files)
487 Each command can carry a list of options, and another set of options can be
488 given for the whole set of commands. Individual command options take
489 precedence over the general options.
491 When using several characters as key to a command, the first characters
492 are prefix commands. For the dispatcher to display useful information, you
493 should provide a description for the prefix, like
495 (setq org-agenda-custom-commands
496 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
497 (\"hl\" tags \"+HOME+Lisa\")
498 (\"hp\" tags \"+HOME+Peter\")
499 (\"hk\" tags \"+HOME+Kim\")))"
500 :group 'org-agenda-custom-commands
501 :type `(repeat
502 (choice :value ("x" "Describe command here" tags "" nil)
503 (list :tag "Single command"
504 (string :tag "Access Key(s) ")
505 (option (string :tag "Description"))
506 (choice
507 (const :tag "Agenda" agenda)
508 (const :tag "TODO list" alltodo)
509 (const :tag "Search words" search)
510 (const :tag "Stuck projects" stuck)
511 (const :tag "Tags/Property match (all agenda files)" tags)
512 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
513 (const :tag "TODO keyword search (all agenda files)" todo)
514 (const :tag "Tags sparse tree (current buffer)" tags-tree)
515 (const :tag "TODO keyword tree (current buffer)" todo-tree)
516 (const :tag "Occur tree (current buffer)" occur-tree)
517 (sexp :tag "Other, user-defined function"))
518 (string :tag "Match (only for some commands)")
519 ,org-agenda-custom-commands-local-options
520 (option (repeat :tag "Export" (file :tag "Export to"))))
521 (list :tag "Command series, all agenda files"
522 (string :tag "Access Key(s)")
523 (string :tag "Description ")
524 (repeat :tag "Component"
525 (choice
526 (list :tag "Agenda"
527 (const :format "" agenda)
528 (const :tag "" :format "" "")
529 ,org-agenda-custom-commands-local-options)
530 (list :tag "TODO list (all keywords)"
531 (const :format "" alltodo)
532 (const :tag "" :format "" "")
533 ,org-agenda-custom-commands-local-options)
534 (list :tag "Search words"
535 (const :format "" search)
536 (string :tag "Match")
537 ,org-agenda-custom-commands-local-options)
538 (list :tag "Stuck projects"
539 (const :format "" stuck)
540 (const :tag "" :format "" "")
541 ,org-agenda-custom-commands-local-options)
542 (list :tag "Tags search"
543 (const :format "" tags)
544 (string :tag "Match")
545 ,org-agenda-custom-commands-local-options)
546 (list :tag "Tags search, TODO entries only"
547 (const :format "" tags-todo)
548 (string :tag "Match")
549 ,org-agenda-custom-commands-local-options)
550 (list :tag "TODO keyword search"
551 (const :format "" todo)
552 (string :tag "Match")
553 ,org-agenda-custom-commands-local-options)
554 (list :tag "Other, user-defined function"
555 (symbol :tag "function")
556 (string :tag "Match")
557 ,org-agenda-custom-commands-local-options)))
559 (repeat :tag "Settings for entire command set"
560 (list (variable :tag "Any variable")
561 (sexp :tag "Value")))
562 (option (repeat :tag "Export" (file :tag "Export to"))))
563 (cons :tag "Prefix key documentation"
564 (string :tag "Access Key(s)")
565 (string :tag "Description ")))))
567 (defcustom org-agenda-query-register ?o
568 "The register holding the current query string.
569 The purpose of this is that if you construct a query string interactively,
570 you can then use it to define a custom command."
571 :group 'org-agenda-custom-commands
572 :type 'character)
574 (defcustom org-stuck-projects
575 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
576 "How to identify stuck projects.
577 This is a list of four items:
578 1. A tags/todo/property matcher string that is used to identify a project.
579 See the manual for a description of tag and property searches.
580 The entire tree below a headline matched by this is considered one project.
581 2. A list of TODO keywords identifying non-stuck projects.
582 If the project subtree contains any headline with one of these todo
583 keywords, the project is considered to be not stuck. If you specify
584 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
585 3. A list of tags identifying non-stuck projects.
586 If the project subtree contains any headline with one of these tags,
587 the project is considered to be not stuck. If you specify \"*\" as
588 a tag, any tag will mark the project unstuck. Note that this is about
589 the explicit presence of a tag somewhere in the subtree, inherited
590 tags do not count here. If inherited tags make a project not stuck,
591 use \"-TAG\" in the tags part of the matcher under (1.) above.
592 4. An arbitrary regular expression matching non-stuck projects.
594 If the project turns out to be not stuck, search continues also in the
595 subtree to see if any of the subtasks have project status.
597 See also the variable `org-tags-match-list-sublevels' which applies
598 to projects matched by this search as well.
600 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
601 or `C-c a #' to produce the list."
602 :group 'org-agenda-custom-commands
603 :type '(list
604 (string :tag "Tags/TODO match to identify a project")
605 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
606 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
607 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
609 (defcustom org-agenda-filter-effort-default-operator "<"
610 "The default operator for effort estimate filtering.
611 If you select an effort estimate limit without first pressing an operator,
612 this one will be used."
613 :group 'org-agenda-custom-commands
614 :type '(choice (const :tag "less or equal" "<")
615 (const :tag "greater or equal"">")
616 (const :tag "equal" "=")))
618 (defgroup org-agenda-skip nil
619 "Options concerning skipping parts of agenda files."
620 :tag "Org Agenda Skip"
621 :group 'org-agenda)
623 (defcustom org-agenda-skip-function-global nil
624 "Function to be called at each match during agenda construction.
625 If this function returns nil, the current match should not be skipped.
626 If the function decided to skip an agenda match, is must return the
627 buffer position from which the search should be continued.
628 This may also be a Lisp form, which will be evaluated.
630 This variable will be applied to every agenda match, including
631 tags/property searches and TODO lists. So try to make the test function
632 do its checking as efficiently as possible. To implement a skipping
633 condition just for specific agenda commands, use the variable
634 `org-agenda-skip-function' which can be set in the options section
635 of custom agenda commands."
636 :group 'org-agenda-skip
637 :type 'sexp)
639 (defgroup org-agenda-daily/weekly nil
640 "Options concerning the daily/weekly agenda."
641 :tag "Org Agenda Daily/Weekly"
642 :group 'org-agenda)
643 (defgroup org-agenda-todo-list nil
644 "Options concerning the global todo list agenda view."
645 :tag "Org Agenda Todo List"
646 :group 'org-agenda)
647 (defgroup org-agenda-match-view nil
648 "Options concerning the general tags/property/todo match agenda view."
649 :tag "Org Agenda Match View"
650 :group 'org-agenda)
651 (defgroup org-agenda-search-view nil
652 "Options concerning the search agenda view."
653 :tag "Org Agenda Search View"
654 :group 'org-agenda)
656 (defvar org-agenda-archives-mode nil
657 "Non-nil means the agenda will include archived items.
658 If this is the symbol `trees', trees in the selected agenda scope
659 that are marked with the ARCHIVE tag will be included anyway. When this is
660 t, also all archive files associated with the current selection of agenda
661 files will be included.")
663 (defcustom org-agenda-restriction-lock-highlight-subtree t
664 "Non-nil means highlight the whole subtree when restriction is active.
665 Otherwise only highlight the headline. Highlighting the whole subtree is
666 useful to ensure no edits happen beyond the restricted region."
667 :group 'org-agenda
668 :type 'boolean)
670 (defcustom org-agenda-skip-comment-trees t
671 "Non-nil means skip trees that start with the COMMENT keyword.
672 When nil, these trees are also scanned by agenda commands."
673 :group 'org-agenda-skip
674 :type 'boolean)
676 (defcustom org-agenda-todo-list-sublevels t
677 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
678 When nil, the sublevels of a TODO entry are not checked, resulting in
679 potentially much shorter TODO lists."
680 :group 'org-agenda-skip
681 :group 'org-agenda-todo-list
682 :type 'boolean)
684 (defcustom org-agenda-todo-ignore-with-date nil
685 "Non-nil means don't show entries with a date in the global todo list.
686 You can use this if you prefer to mark mere appointments with a TODO keyword,
687 but don't want them to show up in the TODO list.
688 When this is set, it also covers deadlines and scheduled items, the settings
689 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
690 will be ignored.
691 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
692 :group 'org-agenda-skip
693 :group 'org-agenda-todo-list
694 :type 'boolean)
696 (defcustom org-agenda-todo-ignore-timestamp nil
697 "Non-nil means don't show entries with a timestamp.
698 This applies when creating the global todo list.
699 Valid values are:
701 past Don't show entries for today or in the past.
703 future Don't show entries with a timestamp in the future.
704 The idea behind this is that if it has a future
705 timestamp, you don't want to think about it until the
706 date.
708 all Don't show any entries with a timestamp in the global todo list.
709 The idea behind this is that by setting a timestamp, you
710 have already \"taken care\" of this item.
712 This variable can also have an integer as a value. If positive (N),
713 todos with a timestamp N or more days in the future will be ignored. If
714 negative (-N), todos with a timestamp N or more days in the past will be
715 ignored. If 0, todos with a timestamp either today or in the future will
716 be ignored. For example, a value of -1 will exclude todos with a
717 timestamp in the past (yesterday or earlier), while a value of 7 will
718 exclude todos with a timestamp a week or more in the future.
720 See also `org-agenda-todo-ignore-with-date'.
721 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
722 to make his option also apply to the tags-todo list."
723 :group 'org-agenda-skip
724 :group 'org-agenda-todo-list
725 :version "24.1"
726 :type '(choice
727 (const :tag "Ignore future timestamp todos" future)
728 (const :tag "Ignore past or present timestamp todos" past)
729 (const :tag "Ignore all timestamp todos" all)
730 (const :tag "Show timestamp todos" nil)
731 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
733 (defcustom org-agenda-todo-ignore-scheduled nil
734 "Non-nil means, ignore some scheduled TODO items when making TODO list.
735 This applies when creating the global todo list.
736 Valid values are:
738 past Don't show entries scheduled today or in the past.
740 future Don't show entries scheduled in the future.
741 The idea behind this is that by scheduling it, you don't want to
742 think about it until the scheduled date.
744 all Don't show any scheduled entries in the global todo list.
745 The idea behind this is that by scheduling it, you have already
746 \"taken care\" of this item.
748 t Same as `all', for backward compatibility.
750 This variable can also have an integer as a value. See
751 `org-agenda-todo-ignore-timestamp' for more details.
753 See also `org-agenda-todo-ignore-with-date'.
754 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
755 to make his option also apply to the tags-todo list."
756 :group 'org-agenda-skip
757 :group 'org-agenda-todo-list
758 :type '(choice
759 (const :tag "Ignore future-scheduled todos" future)
760 (const :tag "Ignore past- or present-scheduled todos" past)
761 (const :tag "Ignore all scheduled todos" all)
762 (const :tag "Ignore all scheduled todos (compatibility)" t)
763 (const :tag "Show scheduled todos" nil)
764 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
766 (defcustom org-agenda-todo-ignore-deadlines nil
767 "Non-nil means ignore some deadline TODO items when making TODO list.
768 There are different motivations for using different values, please think
769 carefully when configuring this variable.
771 This applies when creating the global todo list.
772 Valid values are:
774 near Don't show near deadline entries. A deadline is near when it is
775 closer than `org-deadline-warning-days' days. The idea behind this
776 is that such items will appear in the agenda anyway.
778 far Don't show TODO entries where a deadline has been defined, but
779 the deadline is not near. This is useful if you don't want to
780 use the todo list to figure out what to do now.
782 past Don't show entries with a deadline timestamp for today or in the past.
784 future Don't show entries with a deadline timestamp in the future, not even
785 when they become `near' ones. Use it with caution.
787 all Ignore all TODO entries that do have a deadline.
789 t Same as `near', for backward compatibility.
791 This variable can also have an integer as a value. See
792 `org-agenda-todo-ignore-timestamp' for more details.
794 See also `org-agenda-todo-ignore-with-date'.
795 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
796 to make his option also apply to the tags-todo list."
797 :group 'org-agenda-skip
798 :group 'org-agenda-todo-list
799 :type '(choice
800 (const :tag "Ignore near deadlines" near)
801 (const :tag "Ignore near deadlines (compatibility)" t)
802 (const :tag "Ignore far deadlines" far)
803 (const :tag "Ignore all TODOs with a deadlines" all)
804 (const :tag "Show all TODOs, even if they have a deadline" nil)
805 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
807 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
808 "Time unit to use when possibly ignoring an agenda item.
810 See the docstring of various `org-agenda-todo-ignore-*' options.
811 The default is to compare time stamps using days. An item is thus
812 considered to be in the future if it is at least one day after today.
813 Non-nil means to compare time stamps using seconds. An item is then
814 considered future if it has a time value later than current time."
815 :group 'org-agenda-skip
816 :group 'org-agenda-todo-list
817 :version "24.4"
818 :package-version '(Org . "8.0")
819 :type '(choice
820 (const :tag "Compare time with days" nil)
821 (const :tag "Compare time with seconds" t)))
823 (defcustom org-agenda-tags-todo-honor-ignore-options nil
824 "Non-nil means honor todo-list ignores options also in tags-todo search.
825 The variables
826 `org-agenda-todo-ignore-with-date',
827 `org-agenda-todo-ignore-timestamp',
828 `org-agenda-todo-ignore-scheduled',
829 `org-agenda-todo-ignore-deadlines'
830 make the global TODO list skip entries that have time stamps of certain
831 kinds. If this option is set, the same options will also apply for the
832 tags-todo search, which is the general tags/property matcher
833 restricted to unfinished TODO entries only."
834 :group 'org-agenda-skip
835 :group 'org-agenda-todo-list
836 :group 'org-agenda-match-view
837 :type 'boolean)
839 (defcustom org-agenda-skip-scheduled-if-done nil
840 "Non-nil means don't show scheduled items in agenda when they are done.
841 This is relevant for the daily/weekly agenda, not for the TODO list. And
842 it applies only to the actual date of the scheduling. Warnings about
843 an item with a past scheduling dates are always turned off when the item
844 is DONE."
845 :group 'org-agenda-skip
846 :group 'org-agenda-daily/weekly
847 :type 'boolean)
849 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
850 "Non-nil means skip scheduling line if same entry shows because of deadline.
852 In the agenda of today, an entry can show up multiple times
853 because it is both scheduled and has a nearby deadline, and maybe
854 a plain time stamp as well.
856 When this variable is nil, the entry will be shown several times.
858 When set to t, then only the deadline is shown and the fact that
859 the entry is scheduled today or was scheduled previously is not
860 shown.
862 When set to the symbol `not-today', skip scheduled previously,
863 but not scheduled today.
865 When set to the symbol `repeated-after-deadline', skip scheduled
866 items if they are repeated beyond the current deadline."
867 :group 'org-agenda-skip
868 :group 'org-agenda-daily/weekly
869 :type '(choice
870 (const :tag "Never" nil)
871 (const :tag "Always" t)
872 (const :tag "Not when scheduled today" not-today)
873 (const :tag "When repeated past deadline" repeated-after-deadline)))
875 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
876 "Non-nil means skip timestamp line if same entry shows because of deadline.
877 In the agenda of today, an entry can show up multiple times
878 because it has both a plain timestamp and has a nearby deadline.
879 When this variable is t, then only the deadline is shown and the
880 fact that the entry has a timestamp for or including today is not
881 shown. When this variable is nil, the entry will be shown
882 several times."
883 :group 'org-agenda-skip
884 :group 'org-agenda-daily/weekly
885 :version "24.1"
886 :type '(choice
887 (const :tag "Never" nil)
888 (const :tag "Always" t)))
890 (defcustom org-agenda-skip-deadline-if-done nil
891 "Non-nil means don't show deadlines when the corresponding item is done.
892 When nil, the deadline is still shown and should give you a happy feeling.
893 This is relevant for the daily/weekly agenda. And it applied only to the
894 actually date of the deadline. Warnings about approaching and past-due
895 deadlines are always turned off when the item is DONE."
896 :group 'org-agenda-skip
897 :group 'org-agenda-daily/weekly
898 :type 'boolean)
900 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
901 "Non-nil means skip deadline prewarning when entry is also scheduled.
902 This will apply on all days where a prewarning for the deadline would
903 be shown, but not at the day when the entry is actually due. On that day,
904 the deadline will be shown anyway.
905 This variable may be set to nil, t, the symbol `pre-scheduled',
906 or a number which will then give the number of days before the actual
907 deadline when the prewarnings should resume. The symbol `pre-scheduled'
908 eliminates the deadline prewarning only prior to the scheduled date.
909 This can be used in a workflow where the first showing of the deadline will
910 trigger you to schedule it, and then you don't want to be reminded of it
911 because you will take care of it on the day when scheduled."
912 :group 'org-agenda-skip
913 :group 'org-agenda-daily/weekly
914 :version "24.1"
915 :type '(choice
916 (const :tag "Always show prewarning" nil)
917 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
918 (const :tag "Remove prewarning if entry is scheduled" t)
919 (integer :tag "Restart prewarning N days before deadline")))
921 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
922 "Non-nil means skip scheduled delay when entry also has a deadline.
923 This variable may be set to nil, t, the symbol `post-deadline',
924 or a number which will then give the number of days after the actual
925 scheduled date when the delay should expire. The symbol `post-deadline'
926 eliminates the schedule delay when the date is posterior to the deadline."
927 :group 'org-agenda-skip
928 :group 'org-agenda-daily/weekly
929 :version "24.4"
930 :package-version '(Org . "8.0")
931 :type '(choice
932 (const :tag "Always honor delay" nil)
933 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
934 (const :tag "Ignore delay if entry has a deadline" t)
935 (integer :tag "Honor delay up until N days after the scheduled date")))
937 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
938 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
939 When non-nil, after the search for timestamps has matched once in an
940 entry, the rest of the entry will not be searched."
941 :group 'org-agenda-skip
942 :type 'boolean)
944 (defcustom org-agenda-skip-timestamp-if-done nil
945 "Non-nil means don't select item by timestamp or -range if it is DONE."
946 :group 'org-agenda-skip
947 :group 'org-agenda-daily/weekly
948 :type 'boolean)
950 (defcustom org-agenda-dim-blocked-tasks t
951 "Non-nil means dim blocked tasks in the agenda display.
952 This causes some overhead during agenda construction, but if you
953 have turned on `org-enforce-todo-dependencies',
954 `org-enforce-todo-checkbox-dependencies', or any other blocking
955 mechanism, this will create useful feedback in the agenda.
957 Instead of t, this variable can also have the value `invisible'.
958 Then blocked tasks will be invisible and only become visible when
959 they become unblocked. An exemption to this behavior is when a task is
960 blocked because of unchecked checkboxes below it. Since checkboxes do
961 not show up in the agenda views, making this task invisible you remove any
962 trace from agenda views that there is something to do. Therefore, a task
963 that is blocked because of checkboxes will never be made invisible, it
964 will only be dimmed."
965 :group 'org-agenda-daily/weekly
966 :group 'org-agenda-todo-list
967 :version "24.3"
968 :type '(choice
969 (const :tag "Do not dim" nil)
970 (const :tag "Dim to a gray face" t)
971 (const :tag "Make invisible" invisible)))
973 (defcustom org-timeline-show-empty-dates 3
974 "Non-nil means `org-timeline' also shows dates without an entry.
975 When nil, only the days which actually have entries are shown.
976 When t, all days between the first and the last date are shown.
977 When an integer, show also empty dates, but if there is a gap of more than
978 N days, just insert a special line indicating the size of the gap."
979 :group 'org-agenda-skip
980 :type '(choice
981 (const :tag "None" nil)
982 (const :tag "All" t)
983 (integer :tag "at most")))
985 (defgroup org-agenda-startup nil
986 "Options concerning initial settings in the Agenda in Org Mode."
987 :tag "Org Agenda Startup"
988 :group 'org-agenda)
990 (defcustom org-agenda-menu-show-matcher t
991 "Non-nil means show the match string in the agenda dispatcher menu.
992 When nil, the matcher string is not shown, but is put into the help-echo
993 property so than moving the mouse over the command shows it.
994 Setting it to nil is good if matcher strings are very long and/or if
995 you want to use two-columns display (see `org-agenda-menu-two-columns')."
996 :group 'org-agenda
997 :version "24.1"
998 :type 'boolean)
1000 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1002 (defcustom org-agenda-menu-two-columns nil
1003 "Non-nil means, use two columns to show custom commands in the dispatcher.
1004 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1005 to nil."
1006 :group 'org-agenda
1007 :version "24.1"
1008 :type 'boolean)
1010 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1011 (defcustom org-agenda-finalize-hook nil
1012 "Hook run just before displaying an agenda buffer.
1013 The buffer is still writable when the hook is called.
1015 You can modify some of the buffer substrings but you should be
1016 extra careful not to modify the text properties of the agenda
1017 headlines as the agenda display heavily relies on them."
1018 :group 'org-agenda-startup
1019 :type 'hook)
1021 (defcustom org-agenda-mouse-1-follows-link nil
1022 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1023 A longer mouse click will still set point. Does not work on XEmacs.
1024 Needs to be set before org.el is loaded."
1025 :group 'org-agenda-startup
1026 :type 'boolean)
1028 (defcustom org-agenda-start-with-follow-mode nil
1029 "The initial value of follow mode in a newly created agenda window."
1030 :group 'org-agenda-startup
1031 :type 'boolean)
1033 (defcustom org-agenda-follow-indirect nil
1034 "Non-nil means `org-agenda-follow-mode' displays only the
1035 current item's tree, in an indirect buffer."
1036 :group 'org-agenda
1037 :version "24.1"
1038 :type 'boolean)
1040 (defcustom org-agenda-show-outline-path t
1041 "Non-nil means show outline path in echo area after line motion."
1042 :group 'org-agenda-startup
1043 :type 'boolean)
1045 (defcustom org-agenda-start-with-entry-text-mode nil
1046 "The initial value of entry-text-mode in a newly created agenda window."
1047 :group 'org-agenda-startup
1048 :type 'boolean)
1050 (defcustom org-agenda-entry-text-maxlines 5
1051 "Number of text lines to be added when `E' is pressed in the agenda.
1053 Note that this variable only used during agenda display. Add add entry text
1054 when exporting the agenda, configure the variable
1055 `org-agenda-add-entry-ext-maxlines'."
1056 :group 'org-agenda
1057 :type 'integer)
1059 (defcustom org-agenda-entry-text-exclude-regexps nil
1060 "List of regular expressions to clean up entry text.
1061 The complete matches of all regular expressions in this list will be
1062 removed from entry text before it is shown in the agenda."
1063 :group 'org-agenda
1064 :type '(repeat (regexp)))
1066 (defcustom org-agenda-entry-text-leaders " > "
1067 "Text prepended to the entry text in agenda buffers."
1068 :version "24.4"
1069 :package-version '(Org . "8.0")
1070 :group 'org-agenda
1071 :type 'string)
1073 (defvar org-agenda-entry-text-cleanup-hook nil
1074 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1075 This cleanup is done in a temporary buffer, so the function may inspect and
1076 change the entire buffer.
1077 Some default stuff like drawers and scheduling/deadline dates will already
1078 have been removed when this is called, as will any matches for regular
1079 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1081 (defvar org-agenda-include-inactive-timestamps nil
1082 "Non-nil means include inactive time stamps in agenda and timeline.
1083 Dynamically scoped.")
1085 (defgroup org-agenda-windows nil
1086 "Options concerning the windows used by the Agenda in Org Mode."
1087 :tag "Org Agenda Windows"
1088 :group 'org-agenda)
1090 (defcustom org-agenda-window-setup 'reorganize-frame
1091 "How the agenda buffer should be displayed.
1092 Possible values for this option are:
1094 current-window Show agenda in the current window, keeping all other windows.
1095 other-window Use `switch-to-buffer-other-window' to display agenda.
1096 reorganize-frame Show only two windows on the current frame, the current
1097 window and the agenda.
1098 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1099 Also, when exiting the agenda, kill that frame.
1100 See also the variable `org-agenda-restore-windows-after-quit'."
1101 :group 'org-agenda-windows
1102 :type '(choice
1103 (const current-window)
1104 (const other-frame)
1105 (const other-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-ndays nil
1126 "Number of days to include in overview display.
1127 Should be 1 or 7.
1128 Obsolete, see `org-agenda-span'."
1129 :group 'org-agenda-daily/weekly
1130 :type '(choice (const nil)
1131 (integer)))
1133 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1135 (defcustom org-agenda-span 'week
1136 "Number of days to include in overview display.
1137 Can be day, week, month, year, or any number of days.
1138 Custom commands can set this variable in the options section."
1139 :group 'org-agenda-daily/weekly
1140 :type '(choice (const :tag "Day" day)
1141 (const :tag "Week" week)
1142 (const :tag "Fortnight" fortnight)
1143 (const :tag "Month" month)
1144 (const :tag "Year" year)
1145 (integer :tag "Custom")))
1147 (defcustom org-agenda-start-on-weekday 1
1148 "Non-nil means start the overview always on the specified weekday.
1149 0 denotes Sunday, 1 denotes Monday, etc.
1150 When nil, always start on the current day.
1151 Custom commands can set this variable in the options section."
1152 :group 'org-agenda-daily/weekly
1153 :type '(choice (const :tag "Today" nil)
1154 (integer :tag "Weekday No.")))
1156 (defcustom org-agenda-show-all-dates t
1157 "Non-nil means `org-agenda' shows every day in the selected range.
1158 When nil, only the days which actually have entries are shown."
1159 :group 'org-agenda-daily/weekly
1160 :type 'boolean)
1162 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1163 "Format string for displaying dates in the agenda.
1164 Used by the daily/weekly agenda and by the timeline. This should be
1165 a format string understood by `format-time-string', or a function returning
1166 the formatted date as a string. The function must take a single argument,
1167 a calendar-style date list like (month day year)."
1168 :group 'org-agenda-daily/weekly
1169 :type '(choice
1170 (string :tag "Format string")
1171 (function :tag "Function")))
1173 (defun org-agenda-format-date-aligned (date)
1174 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1175 This function makes sure that dates are aligned for easy reading."
1176 (require 'cal-iso)
1177 (let* ((dayname (calendar-day-name date))
1178 (day (cadr date))
1179 (day-of-week (calendar-day-of-week date))
1180 (month (car date))
1181 (monthname (calendar-month-name month))
1182 (year (nth 2 date))
1183 (iso-week (org-days-to-iso-week
1184 (calendar-absolute-from-gregorian date)))
1185 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1186 (1- year))
1187 ((and (= month 12) (<= iso-week 1))
1188 (1+ year))
1189 (t year)))
1190 (weekstring (if (= day-of-week 1)
1191 (format " W%02d" iso-week)
1192 "")))
1193 (format "%-10s %2d %s %4d%s"
1194 dayname day monthname year weekstring)))
1196 (defcustom org-agenda-time-leading-zero nil
1197 "Non-nil means use leading zero for military times in agenda.
1198 For example, 9:30am would become 09:30 rather than 9:30."
1199 :group 'org-agenda-daily/weekly
1200 :version "24.1"
1201 :type 'boolean)
1203 (defcustom org-agenda-timegrid-use-ampm nil
1204 "When set, show AM/PM style timestamps on the timegrid."
1205 :group 'org-agenda
1206 :version "24.1"
1207 :type 'boolean)
1209 (defun org-agenda-time-of-day-to-ampm (time)
1210 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1211 (let* ((hour-number (string-to-number (substring time 0 -3)))
1212 (minute (substring time -2))
1213 (ampm "am"))
1214 (cond
1215 ((equal hour-number 12)
1216 (setq ampm "pm"))
1217 ((> hour-number 12)
1218 (setq ampm "pm")
1219 (setq hour-number (- hour-number 12))))
1220 (concat
1221 (if org-agenda-time-leading-zero
1222 (format "%02d" hour-number)
1223 (format "%02s" (number-to-string hour-number)))
1224 ":" minute ampm)))
1226 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1227 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1228 (if org-agenda-timegrid-use-ampm
1229 (org-agenda-time-of-day-to-ampm time)
1230 time))
1232 (defcustom org-agenda-weekend-days '(6 0)
1233 "Which days are weekend?
1234 These days get the special face `org-agenda-date-weekend' in the agenda
1235 and timeline buffers."
1236 :group 'org-agenda-daily/weekly
1237 :type '(set :greedy t
1238 (const :tag "Monday" 1)
1239 (const :tag "Tuesday" 2)
1240 (const :tag "Wednesday" 3)
1241 (const :tag "Thursday" 4)
1242 (const :tag "Friday" 5)
1243 (const :tag "Saturday" 6)
1244 (const :tag "Sunday" 0)))
1246 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1247 "Non-nil means jump to today when moving a past date forward in time.
1248 When using S-right in the agenda to move a a date forward, and the date
1249 stamp currently points to the past, the first key press will move it
1250 to today. WHen nil, just move one day forward even if the date stays
1251 in the past."
1252 :group 'org-agenda-daily/weekly
1253 :version "24.1"
1254 :type 'boolean)
1256 (defcustom org-agenda-include-diary nil
1257 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1258 Custom commands can set this variable in the options section."
1259 :group 'org-agenda-daily/weekly
1260 :type 'boolean)
1262 (defcustom org-agenda-include-deadlines t
1263 "If non-nil, include entries within their deadline warning period.
1264 Custom commands can set this variable in the options section."
1265 :group 'org-agenda-daily/weekly
1266 :version "24.1"
1267 :type 'boolean)
1269 (defcustom org-agenda-repeating-timestamp-show-all t
1270 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1271 When set to a list of strings, only show occurrences of repeating
1272 stamps for these TODO keywords. When nil, only one occurrence is
1273 shown, either today or the nearest into the future."
1274 :group 'org-agenda-daily/weekly
1275 :type '(choice
1276 (const :tag "Show repeating stamps" t)
1277 (repeat :tag "Show repeating stamps for these TODO keywords"
1278 (string :tag "TODO Keyword"))
1279 (const :tag "Don't show repeating stamps" nil)))
1281 (defcustom org-scheduled-past-days 10000
1282 "Number of days to continue listing scheduled items not marked DONE.
1283 When an item is scheduled on a date, it shows up in the agenda on this
1284 day and will be listed until it is marked done for the number of days
1285 given here."
1286 :group 'org-agenda-daily/weekly
1287 :type 'integer)
1289 (defcustom org-agenda-log-mode-items '(closed clock)
1290 "List of items that should be shown in agenda log mode.
1291 This list may contain the following symbols:
1293 closed Show entries that have been closed on that day.
1294 clock Show entries that have received clocked time on that day.
1295 state Show all logged state changes.
1296 Note that instead of changing this variable, you can also press `C-u l' in
1297 the agenda to display all available LOG items temporarily."
1298 :group 'org-agenda-daily/weekly
1299 :type '(set :greedy t (const closed) (const clock) (const state)))
1301 (defcustom org-agenda-clock-consistency-checks
1302 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1303 :gap-ok-around ("4:00")
1304 :default-face ((:background "DarkRed") (:foreground "white"))
1305 :overlap-face nil :gap-face nil :no-end-time-face nil
1306 :long-face nil :short-face nil)
1307 "This is a property list, with the following keys:
1309 :max-duration Mark clocking chunks that are longer than this time.
1310 This is a time string like \"HH:MM\", or the number
1311 of minutes as an integer.
1313 :min-duration Mark clocking chunks that are shorter that this.
1314 This is a time string like \"HH:MM\", or the number
1315 of minutes as an integer.
1317 :max-gap Mark gaps between clocking chunks that are longer than
1318 this duration. A number of minutes, or a string
1319 like \"HH:MM\".
1321 :gap-ok-around List of times during the day which are usually not working
1322 times. When a gap is detected, but the gap contains any
1323 of these times, the gap is *not* reported. For example,
1324 if this is (\"4:00\" \"13:00\") then gaps that contain
1325 4:00 in the morning (i.e. the night) and 13:00
1326 (i.e. a typical lunch time) do not cause a warning.
1327 You should have at least one time during the night in this
1328 list, or otherwise the first task each morning will trigger
1329 a warning because it follows a long gap.
1331 Furthermore, the following properties can be used to define faces for
1332 issue display.
1334 :default-face the default face, if the specific face is undefined
1335 :overlap-face face for overlapping clocks
1336 :gap-face face for gaps between clocks
1337 :no-end-time-face face for incomplete clocks
1338 :long-face face for clock intervals that are too long
1339 :short-face face for clock intervals that are too short"
1340 :group 'org-agenda-daily/weekly
1341 :group 'org-clock
1342 :version "24.1"
1343 :type 'plist)
1345 (defcustom org-agenda-log-mode-add-notes t
1346 "Non-nil means add first line of notes to log entries in agenda views.
1347 If a log item like a state change or a clock entry is associated with
1348 notes, the first line of these notes will be added to the entry in the
1349 agenda display."
1350 :group 'org-agenda-daily/weekly
1351 :type 'boolean)
1353 (defcustom org-agenda-start-with-log-mode nil
1354 "The initial value of log-mode in a newly created agenda window.
1355 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1356 explanations on the possible values."
1357 :group 'org-agenda-startup
1358 :group 'org-agenda-daily/weekly
1359 :type '(choice (const :tag "Don't show log items" nil)
1360 (const :tag "Show only log items" only)
1361 (const :tag "Show all possible log items" clockcheck)
1362 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1363 (choice (const :tag "Show closed log items" closed)
1364 (const :tag "Show clocked log items" clock)
1365 (const :tag "Show all logged state changes" state)))))
1367 (defcustom org-agenda-start-with-clockreport-mode nil
1368 "The initial value of clockreport-mode in a newly created agenda window."
1369 :group 'org-agenda-startup
1370 :group 'org-agenda-daily/weekly
1371 :type 'boolean)
1373 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1374 "Property list with parameters for the clocktable in clockreport mode.
1375 This is the display mode that shows a clock table in the daily/weekly
1376 agenda, the properties for this dynamic block can be set here.
1377 The usual clocktable parameters are allowed here, but you cannot set
1378 the properties :name, :tstart, :tend, :block, and :scope - these will
1379 be overwritten to make sure the content accurately reflects the
1380 current display in the agenda."
1381 :group 'org-agenda-daily/weekly
1382 :type 'plist)
1384 (defcustom org-agenda-search-view-always-boolean nil
1385 "Non-nil means the search string is interpreted as individual parts.
1387 The search string for search view can either be interpreted as a phrase,
1388 or as a list of snippets that define a boolean search for a number of
1389 strings.
1391 When this is non-nil, the string will be split on whitespace, and each
1392 snippet will be searched individually, and all must match in order to
1393 select an entry. A snippet is then a single string of non-white
1394 characters, or a string in double quotes, or a regexp in {} braces.
1395 If a snippet is preceded by \"-\", the snippet must *not* match.
1396 \"+\" is syntactic sugar for positive selection. Each snippet may
1397 be found as a full word or a partial word, but see the variable
1398 `org-agenda-search-view-force-full-words'.
1400 When this is nil, search will look for the entire search phrase as one,
1401 with each space character matching any amount of whitespace, including
1402 line breaks.
1404 Even when this is nil, you can still switch to Boolean search dynamically
1405 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1406 is a regexp marked with braces like \"{abc}\", this will also switch to
1407 boolean search."
1408 :group 'org-agenda-search-view
1409 :version "24.1"
1410 :type 'boolean)
1412 (org-defvaralias 'org-agenda-search-view-search-words-only
1413 'org-agenda-search-view-always-boolean)
1415 (defcustom org-agenda-search-view-force-full-words nil
1416 "Non-nil means, search words must be matches as complete words.
1417 When nil, they may also match part of a word."
1418 :group 'org-agenda-search-view
1419 :version "24.1"
1420 :type 'boolean)
1422 (defcustom org-agenda-search-view-max-outline-level 0
1423 "Maximum outline level to display in search view.
1424 E.g. when this is set to 1, the search view will only
1425 show headlines of level 1. When set to 0, the default
1426 value, don't limit agenda view by outline level."
1427 :group 'org-agenda-search-view
1428 :version "24.4"
1429 :package-version '(Org . "8.3")
1430 :type 'integer)
1432 (defgroup org-agenda-time-grid nil
1433 "Options concerning the time grid in the Org-mode Agenda."
1434 :tag "Org Agenda Time Grid"
1435 :group 'org-agenda)
1437 (defcustom org-agenda-search-headline-for-time t
1438 "Non-nil means search headline for a time-of-day.
1439 If the headline contains a time-of-day in one format or another, it will
1440 be used to sort the entry into the time sequence of items for a day.
1441 Some people have time stamps in the headline that refer to the creation
1442 time or so, and then this produces an unwanted side effect. If this is
1443 the case for your, use this variable to turn off searching the headline
1444 for a time."
1445 :group 'org-agenda-time-grid
1446 :type 'boolean)
1448 (defcustom org-agenda-use-time-grid t
1449 "Non-nil means show a time grid in the agenda schedule.
1450 A time grid is a set of lines for specific times (like every two hours between
1451 8:00 and 20:00). The items scheduled for a day at specific times are
1452 sorted in between these lines.
1453 For details about when the grid will be shown, and what it will look like, see
1454 the variable `org-agenda-time-grid'."
1455 :group 'org-agenda-time-grid
1456 :type 'boolean)
1458 (defcustom org-agenda-time-grid
1459 '((daily today require-timed)
1460 "----------------"
1461 (800 1000 1200 1400 1600 1800 2000))
1463 "The settings for time grid for agenda display.
1464 This is a list of three items. The first item is again a list. It contains
1465 symbols specifying conditions when the grid should be displayed:
1467 daily if the agenda shows a single day
1468 weekly if the agenda shows an entire week
1469 today show grid on current date, independent of daily/weekly display
1470 require-timed show grid only if at least one item has a time specification
1472 The second item is a string which will be placed behind the grid time.
1474 The third item is a list of integers, indicating the times that should have
1475 a grid line."
1476 :group 'org-agenda-time-grid
1477 :type
1478 '(list
1479 (set :greedy t :tag "Grid Display Options"
1480 (const :tag "Show grid in single day agenda display" daily)
1481 (const :tag "Show grid in weekly agenda display" weekly)
1482 (const :tag "Always show grid for today" today)
1483 (const :tag "Show grid only if any timed entries are present"
1484 require-timed)
1485 (const :tag "Skip grid times already present in an entry"
1486 remove-match))
1487 (string :tag "Grid String")
1488 (repeat :tag "Grid Times" (integer :tag "Time"))))
1490 (defcustom org-agenda-show-current-time-in-grid t
1491 "Non-nil means show the current time in the time grid."
1492 :group 'org-agenda-time-grid
1493 :version "24.1"
1494 :type 'boolean)
1496 (defcustom org-agenda-current-time-string
1497 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1498 "The string for the current time marker in the agenda."
1499 :group 'org-agenda-time-grid
1500 :version "24.1"
1501 :type 'string)
1503 (defgroup org-agenda-sorting nil
1504 "Options concerning sorting in the Org-mode Agenda."
1505 :tag "Org Agenda Sorting"
1506 :group 'org-agenda)
1508 (defcustom org-agenda-sorting-strategy
1509 '((agenda habit-down time-up priority-down category-keep)
1510 (todo priority-down category-keep)
1511 (tags priority-down category-keep)
1512 (search category-keep))
1513 "Sorting structure for the agenda items of a single day.
1514 This is a list of symbols which will be used in sequence to determine
1515 if an entry should be listed before another entry. The following
1516 symbols are recognized:
1518 time-up Put entries with time-of-day indications first, early first
1519 time-down Put entries with time-of-day indications first, late first
1520 timestamp-up Sort by any timestamp, early first
1521 timestamp-down Sort by any timestamp, late first
1522 scheduled-up Sort by scheduled timestamp, early first
1523 scheduled-down Sort by scheduled timestamp, late first
1524 deadline-up Sort by deadline timestamp, early first
1525 deadline-down Sort by deadline timestamp, late first
1526 ts-up Sort by active timestamp, early first
1527 ts-down Sort by active timestamp, late first
1528 tsia-up Sort by inactive timestamp, early first
1529 tsia-down Sort by inactive timestamp, late first
1530 category-keep Keep the default order of categories, corresponding to the
1531 sequence in `org-agenda-files'.
1532 category-up Sort alphabetically by category, A-Z.
1533 category-down Sort alphabetically by category, Z-A.
1534 tag-up Sort alphabetically by last tag, A-Z.
1535 tag-down Sort alphabetically by last tag, Z-A.
1536 priority-up Sort numerically by priority, high priority last.
1537 priority-down Sort numerically by priority, high priority first.
1538 todo-state-up Sort by todo state, tasks that are done last.
1539 todo-state-down Sort by todo state, tasks that are done first.
1540 effort-up Sort numerically by estimated effort, high effort last.
1541 effort-down Sort numerically by estimated effort, high effort first.
1542 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1543 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1544 habit-up Put entries that are habits first
1545 habit-down Put entries that are habits last
1546 alpha-up Sort headlines alphabetically
1547 alpha-down Sort headlines alphabetically, reversed
1549 The different possibilities will be tried in sequence, and testing stops
1550 if one comparison returns a \"not-equal\". For example, the default
1551 '(time-up category-keep priority-down)
1552 means: Pull out all entries having a specified time of day and sort them,
1553 in order to make a time schedule for the current day the first thing in the
1554 agenda listing for the day. Of the entries without a time indication, keep
1555 the grouped in categories, don't sort the categories, but keep them in
1556 the sequence given in `org-agenda-files'. Within each category sort by
1557 priority.
1559 Leaving out `category-keep' would mean that items will be sorted across
1560 categories by priority.
1562 Instead of a single list, this can also be a set of list for specific
1563 contents, with a context symbol in the car of the list, any of
1564 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1566 Custom commands can bind this variable in the options section."
1567 :group 'org-agenda-sorting
1568 :type `(choice
1569 (repeat :tag "General" ,org-sorting-choice)
1570 (list :tag "Individually"
1571 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1572 (repeat ,org-sorting-choice))
1573 (cons (const :tag "Strategy for TODO lists" todo)
1574 (repeat ,org-sorting-choice))
1575 (cons (const :tag "Strategy for Tags matches" tags)
1576 (repeat ,org-sorting-choice))
1577 (cons (const :tag "Strategy for search matches" search)
1578 (repeat ,org-sorting-choice)))))
1580 (defcustom org-agenda-cmp-user-defined nil
1581 "A function to define the comparison `user-defined'.
1582 This function must receive two arguments, agenda entry a and b.
1583 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1584 the user comparison, return nil.
1585 When this is defined, you can make `user-defined-up' and `user-defined-down'
1586 part of an agenda sorting strategy."
1587 :group 'org-agenda-sorting
1588 :type 'symbol)
1590 (defcustom org-sort-agenda-notime-is-late t
1591 "Non-nil means items without time are considered late.
1592 This is only relevant for sorting. When t, items which have no explicit
1593 time like 15:30 will be considered as 99:01, i.e. later than any items which
1594 do have a time. When nil, the default time is before 0:00. You can use this
1595 option to decide if the schedule for today should come before or after timeless
1596 agenda entries."
1597 :group 'org-agenda-sorting
1598 :type 'boolean)
1600 (defcustom org-sort-agenda-noeffort-is-high t
1601 "Non-nil means items without effort estimate are sorted as high effort.
1602 This also applies when filtering an agenda view with respect to the
1603 < or > effort operator. Then, tasks with no effort defined will be treated
1604 as tasks with high effort.
1605 When nil, such items are sorted as 0 minutes effort."
1606 :group 'org-agenda-sorting
1607 :type 'boolean)
1609 (defgroup org-agenda-line-format nil
1610 "Options concerning the entry prefix in the Org-mode agenda display."
1611 :tag "Org Agenda Line Format"
1612 :group 'org-agenda)
1614 (defcustom org-agenda-prefix-format
1615 '((agenda . " %i %-12:c%?-12t% s")
1616 (timeline . " % s")
1617 (todo . " %i %-12:c")
1618 (tags . " %i %-12:c")
1619 (search . " %i %-12:c"))
1620 "Format specifications for the prefix of items in the agenda views.
1621 An alist with five entries, each for the different agenda types. The
1622 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1623 The values are format strings.
1625 This format works similar to a printf format, with the following meaning:
1627 %c the category of the item, \"Diary\" for entries from the diary,
1628 or as given by the CATEGORY keyword or derived from the file name
1629 %e the effort required by the item
1630 %l the level of the item (insert X space(s) if item is of level X)
1631 %i the icon category of the item, see `org-agenda-category-icon-alist'
1632 %T the last tag of the item (ignore inherited tags, which come first)
1633 %t the HH:MM time-of-day specification if one applies to the entry
1634 %s Scheduling/Deadline information, a short string
1635 %b show breadcrumbs, i.e., the names of the higher levels
1636 %(expression) Eval EXPRESSION and replace the control string
1637 by the result
1639 All specifiers work basically like the standard `%s' of printf, but may
1640 contain two additional characters: a question mark just after the `%'
1641 and a whitespace/punctuation character just before the final letter.
1643 If the first character after `%' is a question mark, the entire field
1644 will only be included if the corresponding value applies to the current
1645 entry. This is useful for fields which should have fixed width when
1646 present, but zero width when absent. For example, \"%?-12t\" will
1647 result in a 12 character time field if a time of the day is specified,
1648 but will completely disappear in entries which do not contain a time.
1650 If there is punctuation or whitespace character just before the
1651 final format letter, this character will be appended to the field
1652 value if the value is not empty. For example, the format
1653 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1654 the category is empty, no additional colon is inserted.
1656 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1657 which means:
1659 - Indent the line with two space characters
1660 - Give the category a 12 chars wide field, padded with whitespace on
1661 the right (because of `-'). Append a colon if there is a category
1662 (because of `:').
1663 - If there is a time-of-day, put it into a 12 chars wide field. If no
1664 time, don't put in an empty field, just skip it (because of '?').
1665 - Finally, put the scheduling information.
1667 See also the variables `org-agenda-remove-times-when-in-prefix' and
1668 `org-agenda-remove-tags'.
1670 Custom commands can set this variable in the options section."
1671 :type '(choice
1672 (string :tag "General format")
1673 (list :greedy t :tag "View dependent"
1674 (cons (const agenda) (string :tag "Format"))
1675 (cons (const timeline) (string :tag "Format"))
1676 (cons (const todo) (string :tag "Format"))
1677 (cons (const tags) (string :tag "Format"))
1678 (cons (const search) (string :tag "Format"))))
1679 :group 'org-agenda-line-format)
1681 (defvar org-prefix-format-compiled nil
1682 "The compiled prefix format and associated variables.
1683 This is a list where first element is a list of variable bindings, and second
1684 element is the compiled format expression. See the variable
1685 `org-agenda-prefix-format'.")
1687 (defcustom org-agenda-todo-keyword-format "%-1s"
1688 "Format for the TODO keyword in agenda lines.
1689 Set this to something like \"%-12s\" if you want all TODO keywords
1690 to occupy a fixed space in the agenda display."
1691 :group 'org-agenda-line-format
1692 :type 'string)
1694 (defcustom org-agenda-diary-sexp-prefix nil
1695 "A regexp that matches part of a diary sexp entry
1696 which should be treated as scheduling/deadline information in
1697 `org-agenda'.
1699 For example, you can use this to extract the `diary-remind-message' from
1700 `diary-remind' entries."
1701 :group 'org-agenda-line-format
1702 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1704 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1705 "Text preceding timerange entries in the agenda view.
1706 This is a list with two strings. The first applies when the range
1707 is entirely on one day. The second applies if the range spans several days.
1708 The strings may have two \"%d\" format specifiers which will be filled
1709 with the sequence number of the days, and the total number of days in the
1710 range, respectively."
1711 :group 'org-agenda-line-format
1712 :type '(list
1713 (string :tag "Deadline today ")
1714 (choice :tag "Deadline relative"
1715 (string :tag "Format string")
1716 (function))))
1718 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1719 "Text preceding scheduled items in the agenda view.
1720 This is a list with two strings. The first applies when the item is
1721 scheduled on the current day. The second applies when it has been scheduled
1722 previously, it may contain a %d indicating that this is the nth time that
1723 this item is scheduled, due to automatic rescheduling of unfinished items
1724 for the following day. So this number is one larger than the number of days
1725 that passed since this item was scheduled first."
1726 :group 'org-agenda-line-format
1727 :version "24.4"
1728 :package-version '(Org . "8.0")
1729 :type '(list
1730 (string :tag "Scheduled today ")
1731 (string :tag "Scheduled previously")))
1733 (defcustom org-agenda-inactive-leader "["
1734 "Text preceding item pulled into the agenda by inactive time stamps.
1735 These entries are added to the agenda when pressing \"[\"."
1736 :group 'org-agenda-line-format
1737 :version "24.1"
1738 :type 'string)
1740 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1741 "Text preceding deadline items in the agenda view.
1742 This is a list with three strings. The first applies when the item has its
1743 deadline on the current day. The second applies when the deadline is in the
1744 future, the third one when it is in the past. The strings may contain %d
1745 to capture the number of days."
1746 :group 'org-agenda-line-format
1747 :version "24.4"
1748 :package-version '(Org . "8.0")
1749 :type '(list
1750 (string :tag "Deadline today ")
1751 (string :tag "Deadline in the future ")
1752 (string :tag "Deadline in the past ")))
1754 (defcustom org-agenda-remove-times-when-in-prefix t
1755 "Non-nil means remove duplicate time specifications in agenda items.
1756 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1757 time-of-day specification in a headline or diary entry is extracted and
1758 placed into the prefix. If this option is non-nil, the original specification
1759 \(a timestamp or -range, or just a plain time(range) specification like
1760 11:30-4pm) will be removed for agenda display. This makes the agenda less
1761 cluttered.
1762 The option can be t or nil. It may also be the symbol `beg', indicating
1763 that the time should only be removed when it is located at the beginning of
1764 the headline/diary entry."
1765 :group 'org-agenda-line-format
1766 :type '(choice
1767 (const :tag "Always" t)
1768 (const :tag "Never" nil)
1769 (const :tag "When at beginning of entry" beg)))
1771 (defcustom org-agenda-remove-timeranges-from-blocks nil
1772 "Non-nil means remove time ranges specifications in agenda
1773 items that span on several days."
1774 :group 'org-agenda-line-format
1775 :version "24.1"
1776 :type 'boolean)
1778 (defcustom org-agenda-default-appointment-duration nil
1779 "Default duration for appointments that only have a starting time.
1780 When nil, no duration is specified in such cases.
1781 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1782 :group 'org-agenda-line-format
1783 :type '(choice
1784 (integer :tag "Minutes")
1785 (const :tag "No default duration")))
1787 (defcustom org-agenda-show-inherited-tags t
1788 "Non-nil means show inherited tags in each agenda line.
1790 When this option is set to 'always, it take precedences over
1791 `org-agenda-use-tag-inheritance' and inherited tags are shown
1792 in every agenda.
1794 When this option is set to t (the default), inherited tags are
1795 shown when they are available, i.e. when the value of
1796 `org-agenda-use-tag-inheritance' has been taken into account.
1798 This can be set to a list of agenda types in which the agenda
1799 must display the inherited tags. Available types are 'todo,
1800 'agenda, 'search and 'timeline.
1802 When set to nil, never show inherited tags in agenda lines."
1803 :group 'org-agenda-line-format
1804 :group 'org-agenda
1805 :version "24.3"
1806 :type '(choice
1807 (const :tag "Show inherited tags when available" t)
1808 (const :tag "Always show inherited tags" always)
1809 (repeat :tag "Show inherited tags only in selected agenda types"
1810 (symbol :tag "Agenda type"))))
1812 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1813 "List of agenda view types where to use tag inheritance.
1815 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1816 controlled by `org-use-tag-inheritance'. In other agenda types,
1817 `org-use-tag-inheritance' is not used for the selection of the
1818 agenda entries. Still, you may want the agenda to be aware of
1819 the inherited tags anyway, e.g. for later tag filtering.
1821 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1823 This variable has no effect if `org-agenda-show-inherited-tags'
1824 is set to 'always. In that case, the agenda is aware of those
1825 tags.
1827 The default value sets tags in every agenda type. Setting this
1828 option to nil will speed up non-tags agenda view a lot."
1829 :group 'org-agenda
1830 :version "24.3"
1831 :type '(choice
1832 (const :tag "Use tag inheritance in all agenda types" t)
1833 (repeat :tag "Use tag inheritance in selected agenda types"
1834 (symbol :tag "Agenda type"))))
1836 (defcustom org-agenda-hide-tags-regexp nil
1837 "Regular expression used to filter away specific tags in agenda views.
1838 This means that these tags will be present, but not be shown in the agenda
1839 line. Secondary filtering will still work on the hidden tags.
1840 Nil means don't hide any tags."
1841 :group 'org-agenda-line-format
1842 :type '(choice
1843 (const :tag "Hide none" nil)
1844 (string :tag "Regexp ")))
1846 (defcustom org-agenda-remove-tags nil
1847 "Non-nil means remove the tags from the headline copy in the agenda.
1848 When this is the symbol `prefix', only remove tags when
1849 `org-agenda-prefix-format' contains a `%T' specifier."
1850 :group 'org-agenda-line-format
1851 :type '(choice
1852 (const :tag "Always" t)
1853 (const :tag "Never" nil)
1854 (const :tag "When prefix format contains %T" prefix)))
1856 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1857 'org-agenda-remove-tags)
1859 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1860 "Shift tags in agenda items to this column.
1861 If this number is positive, it specifies the column. If it is negative,
1862 it means that the tags should be flushright to that column. For example,
1863 -80 works well for a normal 80 character screen."
1864 :group 'org-agenda-line-format
1865 :type 'integer)
1867 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1869 (defcustom org-agenda-fontify-priorities 'cookies
1870 "Non-nil means highlight low and high priorities in agenda.
1871 When t, the highest priority entries are bold, lowest priority italic.
1872 However, settings in `org-priority-faces' will overrule these faces.
1873 When this variable is the symbol `cookies', only fontify the
1874 cookies, not the entire task.
1875 This may also be an association list of priority faces, whose
1876 keys are the character values of `org-highest-priority',
1877 `org-default-priority', and `org-lowest-priority' (the default values
1878 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1879 color as a string, or a list like `(:background \"Red\")'.
1880 If it is a color, the variable `org-faces-easy-properties'
1881 determines if it is a foreground or a background color."
1882 :group 'org-agenda-line-format
1883 :type '(choice
1884 (const :tag "Never" nil)
1885 (const :tag "Defaults" t)
1886 (const :tag "Cookies only" cookies)
1887 (repeat :tag "Specify"
1888 (list (character :tag "Priority" :value ?A)
1889 (choice :tag "Face "
1890 (string :tag "Color")
1891 (sexp :tag "Face"))))))
1893 (defcustom org-agenda-day-face-function nil
1894 "Function called to determine what face should be used to display a day.
1895 The only argument passed to that function is the day. It should
1896 returns a face, or nil if does not want to specify a face and let
1897 the normal rules apply."
1898 :group 'org-agenda-line-format
1899 :version "24.1"
1900 :type '(choice (const nil) (function)))
1902 (defcustom org-agenda-category-icon-alist nil
1903 "Alist of category icon to be displayed in agenda views.
1905 Each entry should have the following format:
1907 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1909 Where CATEGORY-REGEXP is a regexp matching the categories where
1910 the icon should be displayed.
1911 FILE-OR-DATA either a file path or a string containing image data.
1913 The other fields can be omitted safely if not needed:
1914 TYPE indicates the image type.
1915 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1916 image data.
1917 PROPS are additional image attributes to assign to the image,
1918 like, e.g. `:ascent center'.
1920 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1922 If you want to set the display properties yourself, just put a
1923 list as second element:
1925 (CATEGORY-REGEXP (MY PROPERTY LIST))
1927 For example, to display a 16px horizontal space for Emacs
1928 category, you can use:
1930 (\"Emacs\" '(space . (:width (16))))"
1931 :group 'org-agenda-line-format
1932 :version "24.1"
1933 :type '(alist :key-type (string :tag "Regexp matching category")
1934 :value-type (choice (list :tag "Icon"
1935 (string :tag "File or data")
1936 (symbol :tag "Type")
1937 (boolean :tag "Data?")
1938 (repeat :tag "Extra image properties" :inline t symbol))
1939 (list :tag "Display properties" sexp))))
1941 (defgroup org-agenda-column-view nil
1942 "Options concerning column view in the agenda."
1943 :tag "Org Agenda Column View"
1944 :group 'org-agenda)
1946 (defcustom org-agenda-columns-show-summaries t
1947 "Non-nil means show summaries for columns displayed in the agenda view."
1948 :group 'org-agenda-column-view
1949 :type 'boolean)
1951 (defcustom org-agenda-columns-compute-summary-properties t
1952 "Non-nil means recompute all summary properties before column view.
1953 When column view in the agenda is listing properties that have a summary
1954 operator, it can go to all relevant buffers and recompute the summaries
1955 there. This can mean overhead for the agenda column view, but is necessary
1956 to have thing up to date.
1957 As a special case, a CLOCKSUM property also makes sure that the clock
1958 computations are current."
1959 :group 'org-agenda-column-view
1960 :type 'boolean)
1962 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1963 "Non-nil means the duration of an appointment will add to day effort.
1964 The property to which appointment durations will be added is the one given
1965 in the option `org-effort-property'. If an appointment does not have
1966 an end time, `org-agenda-default-appointment-duration' will be used. If that
1967 is not set, an appointment without end time will not contribute to the time
1968 estimate."
1969 :group 'org-agenda-column-view
1970 :type 'boolean)
1972 (defcustom org-agenda-auto-exclude-function nil
1973 "A function called with a tag to decide if it is filtered on '/ RET'.
1974 The sole argument to the function, which is called once for each
1975 possible tag, is a string giving the name of the tag. The
1976 function should return either nil if the tag should be included
1977 as normal, or \"-<TAG>\" to exclude the tag.
1978 Note that for the purpose of tag filtering, only the lower-case version of
1979 all tags will be considered, so that this function will only ever see
1980 the lower-case version of all tags."
1981 :group 'org-agenda
1982 :type '(choice (const nil) (function)))
1984 (defcustom org-agenda-bulk-custom-functions nil
1985 "Alist of characters and custom functions for bulk actions.
1986 For example, this value makes those two functions available:
1988 '((?R set-category)
1989 (?C bulk-cut))
1991 With selected entries in an agenda buffer, `B R' will call
1992 the custom function `set-category' on the selected entries.
1993 Note that functions in this alist don't need to be quoted."
1994 :type 'alist
1995 :version "24.1"
1996 :group 'org-agenda)
1998 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1999 "Execute BODY with point at location given by `org-hd-marker' property.
2000 If STRING is non-nil, the text property will be fetched from position 0
2001 in that string. If STRING is nil, it will be fetched from the beginning
2002 of the current line."
2003 (org-with-gensyms (marker)
2004 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2005 'org-hd-marker ,string)))
2006 (with-current-buffer (marker-buffer ,marker)
2007 (save-excursion
2008 (goto-char ,marker)
2009 ,@body)))))
2010 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2012 (defun org-add-agenda-custom-command (entry)
2013 "Replace or add a command in `org-agenda-custom-commands'.
2014 This is mostly for hacking and trying a new command - once the command
2015 works you probably want to add it to `org-agenda-custom-commands' for good."
2016 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2017 (if ass
2018 (setcdr ass (cdr entry))
2019 (push entry org-agenda-custom-commands))))
2021 ;;; Define the org-agenda-mode
2023 (defvar org-agenda-mode-map (make-sparse-keymap)
2024 "Keymap for `org-agenda-mode'.")
2025 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2027 (defvar org-agenda-menu) ; defined later in this file.
2028 (defvar org-agenda-restrict nil) ; defined later in this file.
2029 (defvar org-agenda-follow-mode nil)
2030 (defvar org-agenda-entry-text-mode nil)
2031 (defvar org-agenda-clockreport-mode nil)
2032 (defvar org-agenda-show-log nil)
2033 (defvar org-agenda-redo-command nil)
2034 (defvar org-agenda-query-string nil)
2035 (defvar org-agenda-mode-hook nil
2036 "Hook run after `org-agenda-mode' is turned on.
2037 The buffer is still writable when this hook is called.")
2038 (defvar org-agenda-type nil)
2039 (defvar org-agenda-force-single-file nil)
2040 (defvar org-agenda-bulk-marked-entries nil
2041 "List of markers that refer to marked entries in the agenda.")
2043 ;;; Multiple agenda buffers support
2045 (defcustom org-agenda-sticky nil
2046 "Non-nil means agenda q key will bury agenda buffers.
2047 Agenda commands will then show existing buffer instead of generating new ones.
2048 When nil, `q' will kill the single agenda buffer."
2049 :group 'org-agenda
2050 :version "24.3"
2051 :type 'boolean)
2054 ;;;###autoload
2055 (defun org-toggle-sticky-agenda (&optional arg)
2056 "Toggle `org-agenda-sticky'."
2057 (interactive "P")
2058 (let ((new-value (if arg
2059 (> (prefix-numeric-value arg) 0)
2060 (not org-agenda-sticky))))
2061 (if (equal new-value org-agenda-sticky)
2062 (and (org-called-interactively-p 'interactive)
2063 (message "Sticky agenda was already %s"
2064 (if org-agenda-sticky "enabled" "disabled")))
2065 (setq org-agenda-sticky new-value)
2066 (org-agenda-kill-all-agenda-buffers)
2067 (and (org-called-interactively-p 'interactive)
2068 (message "Sticky agenda was %s"
2069 (if org-agenda-sticky "enabled" "disabled"))))))
2071 (defvar org-agenda-buffer nil
2072 "Agenda buffer currently being generated.")
2074 (defvar org-agenda-last-prefix-arg nil)
2075 (defvar org-agenda-this-buffer-name nil)
2076 (defvar org-agenda-doing-sticky-redo nil)
2077 (defvar org-agenda-this-buffer-is-sticky nil)
2079 (defconst org-agenda-local-vars
2080 '(org-agenda-this-buffer-name
2081 org-agenda-undo-list
2082 org-agenda-pending-undo-list
2083 org-agenda-follow-mode
2084 org-agenda-entry-text-mode
2085 org-agenda-clockreport-mode
2086 org-agenda-show-log
2087 org-agenda-redo-command
2088 org-agenda-query-string
2089 org-agenda-type
2090 org-agenda-bulk-marked-entries
2091 org-agenda-undo-has-started-in
2092 org-agenda-info
2093 org-agenda-pre-window-conf
2094 org-agenda-columns-active
2095 org-agenda-tag-filter
2096 org-agenda-category-filter
2097 org-agenda-top-headline-filter
2098 org-agenda-regexp-filter
2099 org-agenda-markers
2100 org-agenda-last-search-view-search-was-boolean
2101 org-agenda-filtered-by-category
2102 org-agenda-filter-form
2103 org-agenda-cycle-counter
2104 org-agenda-last-prefix-arg)
2105 "Variables that must be local in agenda buffers to allow multiple buffers.")
2107 (defun org-agenda-mode ()
2108 "Mode for time-sorted view on action items in Org-mode files.
2110 The following commands are available:
2112 \\{org-agenda-mode-map}"
2113 (interactive)
2114 (cond (org-agenda-doing-sticky-redo
2115 ;; Refreshing sticky agenda-buffer
2117 ;; Preserve the value of `org-agenda-local-vars' variables,
2118 ;; while letting `kill-all-local-variables' kill the rest
2119 (let ((save (buffer-local-variables)))
2120 (kill-all-local-variables)
2121 (mapc 'make-local-variable org-agenda-local-vars)
2122 (dolist (elem save)
2123 (let ((var (car elem))
2124 (val (cdr elem)))
2125 (when (and val
2126 (member var org-agenda-local-vars))
2127 (set var val)))))
2128 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2129 (org-agenda-sticky
2130 ;; Creating a sticky Agenda buffer for the first time
2131 (kill-all-local-variables)
2132 (mapc 'make-local-variable org-agenda-local-vars)
2133 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2135 ;; Creating a non-sticky agenda buffer
2136 (kill-all-local-variables)
2137 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2138 (setq org-agenda-undo-list nil
2139 org-agenda-pending-undo-list nil
2140 org-agenda-bulk-marked-entries nil)
2141 (setq major-mode 'org-agenda-mode)
2142 ;; Keep global-font-lock-mode from turning on font-lock-mode
2143 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2144 (setq mode-name "Org-Agenda")
2145 (setq indent-tabs-mode nil)
2146 (use-local-map org-agenda-mode-map)
2147 (easy-menu-add org-agenda-menu)
2148 (if org-startup-truncated (setq truncate-lines t))
2149 (org-set-local 'line-move-visual nil)
2150 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2151 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2152 ;; Make sure properties are removed when copying text
2153 (org-add-hook 'filter-buffer-substring-functions
2154 (lambda (fun start end delete)
2155 (substring-no-properties (funcall fun start end delete)))
2156 nil t)
2157 (unless org-agenda-keep-modes
2158 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2159 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2160 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2161 org-agenda-show-log org-agenda-start-with-log-mode))
2162 (add-to-invisibility-spec '(org-filtered))
2163 (add-to-invisibility-spec '(org-link))
2164 (easy-menu-change
2165 '("Agenda") "Agenda Files"
2166 (append
2167 (list
2168 (vector
2169 (if (get 'org-agenda-files 'org-restrict)
2170 "Restricted to single file"
2171 "Edit File List")
2172 '(org-edit-agenda-file-list)
2173 (not (get 'org-agenda-files 'org-restrict)))
2174 "--")
2175 (mapcar 'org-file-menu-entry (org-agenda-files))))
2176 (org-agenda-set-mode-name)
2177 (apply
2178 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2179 (list 'org-agenda-mode-hook)))
2181 (substitute-key-definition 'undo 'org-agenda-undo
2182 org-agenda-mode-map global-map)
2183 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2184 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2185 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2186 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2187 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2188 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2189 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2190 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2191 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2192 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2193 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2194 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2195 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2196 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2197 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2198 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2199 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2200 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2201 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2202 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2203 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2204 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2205 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2206 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2207 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2208 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2209 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2210 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2211 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2212 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2213 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2214 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2215 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2216 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2217 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2218 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2219 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2220 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2221 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2222 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2223 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2224 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2225 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2226 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2227 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2228 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2229 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2230 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2231 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2232 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2234 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2235 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2236 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2237 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2238 (while l (org-defkey org-agenda-mode-map
2239 (int-to-string (pop l)) 'digit-argument)))
2241 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2242 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2243 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2244 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2245 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2246 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2247 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2248 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2249 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2250 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2251 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2252 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2253 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2254 'org-clock-modify-effort-estimate)
2255 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2256 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2257 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2258 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2259 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2260 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2261 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2262 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2263 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2264 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2265 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2266 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2267 (substitute-key-definition 'next-line 'org-agenda-next-line
2268 org-agenda-mode-map global-map)
2269 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2270 org-agenda-mode-map global-map)
2271 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2272 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2273 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2274 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2275 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2276 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2277 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2278 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2279 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2280 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2281 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2282 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2283 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2284 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2285 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2286 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2287 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2288 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2289 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2290 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2291 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2292 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2293 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2294 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2295 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2296 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2297 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2298 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2299 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2300 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2302 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2303 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2304 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2305 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2306 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2307 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2308 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2309 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2310 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2311 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2312 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2313 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2314 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2315 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2316 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2318 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2319 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2320 (when org-agenda-mouse-1-follows-link
2321 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2322 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2323 '("Agenda"
2324 ("Agenda Files")
2325 "--"
2326 ("Agenda Dates"
2327 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2328 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2329 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2330 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2331 "--"
2332 ("View"
2333 ["Day View" org-agenda-day-view
2334 :active (org-agenda-check-type nil 'agenda)
2335 :style radio :selected (eq org-agenda-current-span 'day)
2336 :keys "v d (or just d)"]
2337 ["Week View" org-agenda-week-view
2338 :active (org-agenda-check-type nil 'agenda)
2339 :style radio :selected (eq org-agenda-current-span 'week)
2340 :keys "v w"]
2341 ["Fortnight View" org-agenda-fortnight-view
2342 :active (org-agenda-check-type nil 'agenda)
2343 :style radio :selected (eq org-agenda-current-span 'fortnight)
2344 :keys "v f"]
2345 ["Month View" org-agenda-month-view
2346 :active (org-agenda-check-type nil 'agenda)
2347 :style radio :selected (eq org-agenda-current-span 'month)
2348 :keys "v m"]
2349 ["Year View" org-agenda-year-view
2350 :active (org-agenda-check-type nil 'agenda)
2351 :style radio :selected (eq org-agenda-current-span 'year)
2352 :keys "v y"]
2353 "--"
2354 ["Include Diary" org-agenda-toggle-diary
2355 :style toggle :selected org-agenda-include-diary
2356 :active (org-agenda-check-type nil 'agenda)]
2357 ["Include Deadlines" org-agenda-toggle-deadlines
2358 :style toggle :selected org-agenda-include-deadlines
2359 :active (org-agenda-check-type nil 'agenda)]
2360 ["Use Time Grid" org-agenda-toggle-time-grid
2361 :style toggle :selected org-agenda-use-time-grid
2362 :active (org-agenda-check-type nil 'agenda)]
2363 "--"
2364 ["Show clock report" org-agenda-clockreport-mode
2365 :style toggle :selected org-agenda-clockreport-mode
2366 :active (org-agenda-check-type nil 'agenda)]
2367 ["Show some entry text" org-agenda-entry-text-mode
2368 :style toggle :selected org-agenda-entry-text-mode
2369 :active t]
2370 "--"
2371 ["Show Logbook entries" org-agenda-log-mode
2372 :style toggle :selected org-agenda-show-log
2373 :active (org-agenda-check-type nil 'agenda 'timeline)
2374 :keys "v l (or just l)"]
2375 ["Include archived trees" org-agenda-archives-mode
2376 :style toggle :selected org-agenda-archives-mode :active t
2377 :keys "v a"]
2378 ["Include archive files" (org-agenda-archives-mode t)
2379 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2380 :keys "v A"]
2381 "--"
2382 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2383 ["Write view to file" org-agenda-write t]
2384 ["Rebuild buffer" org-agenda-redo t]
2385 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2386 "--"
2387 ["Show original entry" org-agenda-show t]
2388 ["Go To (other window)" org-agenda-goto t]
2389 ["Go To (this window)" org-agenda-switch-to t]
2390 ["Capture with cursor date" org-agenda-capture t]
2391 ["Follow Mode" org-agenda-follow-mode
2392 :style toggle :selected org-agenda-follow-mode :active t]
2393 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2394 "--"
2395 ("TODO"
2396 ["Cycle TODO" org-agenda-todo t]
2397 ["Next TODO set" org-agenda-todo-nextset t]
2398 ["Previous TODO set" org-agenda-todo-previousset t]
2399 ["Add note" org-agenda-add-note t])
2400 ("Archive/Refile/Delete"
2401 ["Archive default" org-agenda-archive-default t]
2402 ["Archive default" org-agenda-archive-default-with-confirmation t]
2403 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2404 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2405 ["Archive subtree" org-agenda-archive t]
2406 "--"
2407 ["Refile" org-agenda-refile t]
2408 "--"
2409 ["Delete subtree" org-agenda-kill t])
2410 ("Bulk action"
2411 ["Mark entry" org-agenda-bulk-mark t]
2412 ["Mark all" org-agenda-bulk-mark-all t]
2413 ["Unmark entry" org-agenda-bulk-unmark t]
2414 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2415 ["Toggle mark" org-agenda-bulk-toggle t]
2416 ["Toggle all" org-agenda-bulk-toggle-all t]
2417 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2418 ["Act on all marked" org-agenda-bulk-action t]
2419 "--"
2420 ("Tags and Properties"
2421 ["Show all Tags" org-agenda-show-tags t]
2422 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2423 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2424 "--"
2425 ["Column View" org-columns t])
2426 ("Deadline/Schedule"
2427 ["Schedule" org-agenda-schedule t]
2428 ["Set Deadline" org-agenda-deadline t]
2429 "--"
2430 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2431 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2432 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2433 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2434 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2435 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2436 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2437 ("Clock and Effort"
2438 ["Clock in" org-agenda-clock-in t]
2439 ["Clock out" org-agenda-clock-out t]
2440 ["Clock cancel" org-agenda-clock-cancel t]
2441 ["Goto running clock" org-clock-goto t]
2442 "--"
2443 ["Set Effort" org-agenda-set-effort t]
2444 ["Change clocked effort" org-clock-modify-effort-estimate
2445 (org-clock-is-active)])
2446 ("Priority"
2447 ["Set Priority" org-agenda-priority t]
2448 ["Increase Priority" org-agenda-priority-up t]
2449 ["Decrease Priority" org-agenda-priority-down t]
2450 ["Show Priority" org-show-priority t])
2451 ("Calendar/Diary"
2452 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2453 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2454 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2455 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2456 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2457 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2458 "--"
2459 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2460 "--"
2461 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2462 "--"
2463 ("MobileOrg"
2464 ["Push Files and Views" org-mobile-push t]
2465 ["Get Captured and Flagged" org-mobile-pull t]
2466 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2467 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2468 "--"
2469 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2470 "--"
2471 ["Quit" org-agenda-quit t]
2472 ["Exit and Release Buffers" org-agenda-exit t]
2475 ;;; Agenda undo
2477 (defvar org-agenda-allow-remote-undo t
2478 "Non-nil means allow remote undo from the agenda buffer.")
2479 (defvar org-agenda-undo-has-started-in nil
2480 "Buffers that have already seen `undo-start' in the current undo sequence.")
2482 (defun org-agenda-undo ()
2483 "Undo a remote editing step in the agenda.
2484 This undoes changes both in the agenda buffer and in the remote buffer
2485 that have been changed along."
2486 (interactive)
2487 (or org-agenda-allow-remote-undo
2488 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2489 (if (not (eq this-command last-command))
2490 (setq org-agenda-undo-has-started-in nil
2491 org-agenda-pending-undo-list org-agenda-undo-list))
2492 (if (not org-agenda-pending-undo-list)
2493 (user-error "No further undo information"))
2494 (let* ((entry (pop org-agenda-pending-undo-list))
2495 buf line cmd rembuf)
2496 (setq cmd (pop entry) line (pop entry))
2497 (setq rembuf (nth 2 entry))
2498 (org-with-remote-undo rembuf
2499 (while (bufferp (setq buf (pop entry)))
2500 (if (pop entry)
2501 (with-current-buffer buf
2502 (let ((last-undo-buffer buf)
2503 (inhibit-read-only t))
2504 (unless (memq buf org-agenda-undo-has-started-in)
2505 (push buf org-agenda-undo-has-started-in)
2506 (make-local-variable 'pending-undo-list)
2507 (undo-start))
2508 (while (and pending-undo-list
2509 (listp pending-undo-list)
2510 (not (car pending-undo-list)))
2511 (pop pending-undo-list))
2512 (undo-more 1))))))
2513 (org-goto-line line)
2514 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2516 (defun org-verify-change-for-undo (l1 l2)
2517 "Verify that a real change occurred between the undo lists L1 and L2."
2518 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2519 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2520 (not (eq l1 l2)))
2522 ;;; Agenda dispatch
2524 (defvar org-agenda-restrict-begin (make-marker))
2525 (defvar org-agenda-restrict-end (make-marker))
2526 (defvar org-agenda-last-dispatch-buffer nil)
2527 (defvar org-agenda-overriding-restriction nil)
2529 (defcustom org-agenda-custom-commands-contexts nil
2530 "Alist of custom agenda keys and contextual rules.
2532 For example, if you have a custom agenda command \"p\" and you
2533 want this command to be accessible only from plain text files,
2534 use this:
2536 '((\"p\" ((in-file . \"\\.txt\"))))
2538 Here are the available contexts definitions:
2540 in-file: command displayed only in matching files
2541 in-mode: command displayed only in matching modes
2542 not-in-file: command not displayed in matching files
2543 not-in-mode: command not displayed in matching modes
2544 in-buffer: command displayed only in matching buffers
2545 not-in-buffer: command not displayed in matching buffers
2546 [function]: a custom function taking no argument
2548 If you define several checks, the agenda command will be
2549 accessible if there is at least one valid check.
2551 You can also bind a key to another agenda custom command
2552 depending on contextual rules.
2554 '((\"p\" \"q\" ((in-file . \"\\.txt\"))))
2556 Here it means: in .txt files, use \"p\" as the key for the
2557 agenda command otherwise associated with \"q\". (The command
2558 originally associated with \"q\" is not displayed to avoid
2559 duplicates.)"
2560 :version "24.3"
2561 :group 'org-agenda-custom-commands
2562 :type '(repeat (list :tag "Rule"
2563 (string :tag " Agenda key")
2564 (string :tag "Replace by command")
2565 (repeat :tag "Available when"
2566 (choice
2567 (cons :tag "Condition"
2568 (choice
2569 (const :tag "In file" in-file)
2570 (const :tag "Not in file" not-in-file)
2571 (const :tag "In buffer" in-buffer)
2572 (const :tag "Not in buffer" not-in-buffer)
2573 (const :tag "In mode" in-mode)
2574 (const :tag "Not in mode" not-in-mode))
2575 (regexp))
2576 (function :tag "Custom function"))))))
2578 (defcustom org-agenda-max-entries nil
2579 "Maximum number of entries to display in an agenda.
2580 This can be nil (no limit) or an integer or an alist of agenda
2581 types with an associated number of entries to display in this
2582 type."
2583 :version "24.4"
2584 :package-version '(Org . "8.0")
2585 :group 'org-agenda-custom-commands
2586 :type '(choice (symbol :tag "No limit" nil)
2587 (integer :tag "Max number of entries")
2588 (repeat
2589 (cons (choice :tag "Agenda type"
2590 (const agenda)
2591 (const todo)
2592 (const tags)
2593 (const search)
2594 (const timeline))
2595 (integer :tag "Max number of entries")))))
2597 (defcustom org-agenda-max-todos nil
2598 "Maximum number of TODOs to display in an agenda.
2599 This can be nil (no limit) or an integer or an alist of agenda
2600 types with an associated number of entries to display in this
2601 type."
2602 :version "24.4"
2603 :package-version '(Org . "8.0")
2604 :group 'org-agenda-custom-commands
2605 :type '(choice (symbol :tag "No limit" nil)
2606 (integer :tag "Max number of TODOs")
2607 (repeat
2608 (cons (choice :tag "Agenda type"
2609 (const agenda)
2610 (const todo)
2611 (const tags)
2612 (const search)
2613 (const timeline))
2614 (integer :tag "Max number of TODOs")))))
2616 (defcustom org-agenda-max-tags nil
2617 "Maximum number of tagged entries to display in an agenda.
2618 This can be nil (no limit) or an integer or an alist of agenda
2619 types with an associated number of entries to display in this
2620 type."
2621 :version "24.4"
2622 :package-version '(Org . "8.0")
2623 :group 'org-agenda-custom-commands
2624 :type '(choice (symbol :tag "No limit" nil)
2625 (integer :tag "Max number of tagged entries")
2626 (repeat
2627 (cons (choice :tag "Agenda type"
2628 (const agenda)
2629 (const todo)
2630 (const tags)
2631 (const search)
2632 (const timeline))
2633 (integer :tag "Max number of tagged entries")))))
2635 (defcustom org-agenda-max-effort nil
2636 "Maximum cumulated effort duration for the agenda.
2637 This can be nil (no limit) or a number of minutes (as an integer)
2638 or an alist of agenda types with an associated number of minutes
2639 to limit entries to in this type."
2640 :version "24.4"
2641 :package-version '(Org . "8.0")
2642 :group 'org-agenda-custom-commands
2643 :type '(choice (symbol :tag "No limit" nil)
2644 (integer :tag "Max number of minutes")
2645 (repeat
2646 (cons (choice :tag "Agenda type"
2647 (const agenda)
2648 (const todo)
2649 (const tags)
2650 (const search)
2651 (const timeline))
2652 (integer :tag "Max number of minutes")))))
2654 (defvar org-keys nil)
2655 (defvar org-match nil)
2656 ;;;###autoload
2657 (defun org-agenda (&optional arg org-keys restriction)
2658 "Dispatch agenda commands to collect entries to the agenda buffer.
2659 Prompts for a command to execute. Any prefix arg will be passed
2660 on to the selected command. The default selections are:
2662 a Call `org-agenda-list' to display the agenda for current day or week.
2663 t Call `org-todo-list' to display the global todo list.
2664 T Call `org-todo-list' to display the global todo list, select only
2665 entries with a specific TODO keyword (the user gets a prompt).
2666 m Call `org-tags-view' to display headlines with tags matching
2667 a condition (the user is prompted for the condition).
2668 M Like `m', but select only TODO entries, no ordinary headlines.
2669 L Create a timeline for the current buffer.
2670 e Export views to associated files.
2671 s Search entries for keywords.
2672 S Search entries for keywords, only with TODO keywords.
2673 / Multi occur across all agenda files and also files listed
2674 in `org-agenda-text-search-extra-files'.
2675 < Restrict agenda commands to buffer, subtree, or region.
2676 Press several times to get the desired effect.
2677 > Remove a previous restriction.
2678 # List \"stuck\" projects.
2679 ! Configure what \"stuck\" means.
2680 C Configure custom agenda commands.
2682 More commands can be added by configuring the variable
2683 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2684 searches can be pre-defined in this way.
2686 If the current buffer is in Org-mode and visiting a file, you can also
2687 first press `<' once to indicate that the agenda should be temporarily
2688 \(until the next use of \\[org-agenda]) restricted to the current file.
2689 Pressing `<' twice means to restrict to the current subtree or region
2690 \(if active)."
2691 (interactive "P")
2692 (catch 'exit
2693 (let* ((prefix-descriptions nil)
2694 (org-agenda-buffer-name org-agenda-buffer-name)
2695 (org-agenda-window-setup (if (equal (buffer-name)
2696 org-agenda-buffer-name)
2697 'current-window
2698 org-agenda-window-setup))
2699 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2700 (org-agenda-custom-commands
2701 ;; normalize different versions
2702 (delq nil
2703 (mapcar
2704 (lambda (x)
2705 (cond ((stringp (cdr x))
2706 (push x prefix-descriptions)
2707 nil)
2708 ((stringp (nth 1 x)) x)
2709 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2710 (t (cons (car x) (cons "" (cdr x))))))
2711 org-agenda-custom-commands)))
2712 (org-agenda-custom-commands
2713 (org-contextualize-keys
2714 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2715 (buf (current-buffer))
2716 (bfn (buffer-file-name (buffer-base-buffer)))
2717 entry key type org-match lprops ans)
2718 ;; Turn off restriction unless there is an overriding one,
2719 (unless org-agenda-overriding-restriction
2720 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2721 ;; There is a request to keep the file list in place
2722 (put 'org-agenda-files 'org-restrict nil))
2723 (setq org-agenda-restrict nil)
2724 (move-marker org-agenda-restrict-begin nil)
2725 (move-marker org-agenda-restrict-end nil))
2726 ;; Delete old local properties
2727 (put 'org-agenda-redo-command 'org-lprops nil)
2728 ;; Delete previously set last-arguments
2729 (put 'org-agenda-redo-command 'last-args nil)
2730 ;; Remember where this call originated
2731 (setq org-agenda-last-dispatch-buffer (current-buffer))
2732 (unless org-keys
2733 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2734 org-keys (car ans)
2735 restriction (cdr ans)))
2736 ;; If we have sticky agenda buffers, set a name for the buffer,
2737 ;; depending on the invoking keys. The user may still set this
2738 ;; as a command option, which will overwrite what we do here.
2739 (if org-agenda-sticky
2740 (setq org-agenda-buffer-name
2741 (format "*Org Agenda(%s)*" org-keys)))
2742 ;; Establish the restriction, if any
2743 (when (and (not org-agenda-overriding-restriction) restriction)
2744 (put 'org-agenda-files 'org-restrict (list bfn))
2745 (cond
2746 ((eq restriction 'region)
2747 (setq org-agenda-restrict (current-buffer))
2748 (move-marker org-agenda-restrict-begin (region-beginning))
2749 (move-marker org-agenda-restrict-end (region-end)))
2750 ((eq restriction 'subtree)
2751 (save-excursion
2752 (setq org-agenda-restrict (current-buffer))
2753 (org-back-to-heading t)
2754 (move-marker org-agenda-restrict-begin (point))
2755 (move-marker org-agenda-restrict-end
2756 (progn (org-end-of-subtree t)))))))
2758 ;; For example the todo list should not need it (but does...)
2759 (cond
2760 ((setq entry (assoc org-keys org-agenda-custom-commands))
2761 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2762 (progn
2763 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2764 lprops (nth 4 entry))
2765 (if org-agenda-sticky
2766 (setq org-agenda-buffer-name
2767 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2768 (format "*Org Agenda(%s)*" org-keys))))
2769 (put 'org-agenda-redo-command 'org-lprops lprops)
2770 (cond
2771 ((eq type 'agenda)
2772 (org-let lprops '(org-agenda-list current-prefix-arg)))
2773 ((eq type 'agenda*)
2774 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2775 ((eq type 'alltodo)
2776 (org-let lprops '(org-todo-list current-prefix-arg)))
2777 ((eq type 'search)
2778 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2779 ((eq type 'stuck)
2780 (org-let lprops '(org-agenda-list-stuck-projects
2781 current-prefix-arg)))
2782 ((eq type 'tags)
2783 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2784 ((eq type 'tags-todo)
2785 (org-let lprops '(org-tags-view '(4) org-match)))
2786 ((eq type 'todo)
2787 (org-let lprops '(org-todo-list org-match)))
2788 ((eq type 'tags-tree)
2789 (org-check-for-org-mode)
2790 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2791 ((eq type 'todo-tree)
2792 (org-check-for-org-mode)
2793 (org-let lprops
2794 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2795 (regexp-quote org-match) "\\>"))))
2796 ((eq type 'occur-tree)
2797 (org-check-for-org-mode)
2798 (org-let lprops '(org-occur org-match)))
2799 ((functionp type)
2800 (org-let lprops '(funcall type org-match)))
2801 ((fboundp type)
2802 (org-let lprops '(funcall type org-match)))
2803 (t (user-error "Invalid custom agenda command type %s" type))))
2804 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2805 ((equal org-keys "C")
2806 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2807 (customize-variable 'org-agenda-custom-commands))
2808 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2809 ((equal org-keys "s") (call-interactively 'org-search-view))
2810 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2811 ((equal org-keys "t") (call-interactively 'org-todo-list))
2812 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2813 ((equal org-keys "m") (call-interactively 'org-tags-view))
2814 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2815 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2816 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2817 (org-add-hook
2818 'post-command-hook
2819 (lambda ()
2820 (unless (current-message)
2821 (let* ((m (org-agenda-get-any-marker))
2822 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2823 (when note
2824 (message (concat
2825 "FLAGGING-NOTE ([?] for more info): "
2826 (org-add-props
2827 (replace-regexp-in-string
2828 "\\\\n" "//"
2829 (copy-sequence note))
2830 nil 'face 'org-warning)))))))
2831 t t))
2832 ((equal org-keys "L")
2833 (unless (derived-mode-p 'org-mode)
2834 (user-error "This is not an Org-mode file"))
2835 (unless restriction
2836 (put 'org-agenda-files 'org-restrict (list bfn))
2837 (org-call-with-arg 'org-timeline arg)))
2838 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2839 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2840 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2841 (t (user-error "Invalid agenda key"))))))
2843 (defvar org-agenda-multi)
2845 (defun org-agenda-append-agenda ()
2846 "Append another agenda view to the current one.
2847 This function allows interactive building of block agendas.
2848 Agenda views are separated by `org-agenda-block-separator'."
2849 (interactive)
2850 (unless (derived-mode-p 'org-agenda-mode)
2851 (user-error "Can only append from within agenda buffer"))
2852 (let ((org-agenda-multi t))
2853 (org-agenda)
2854 (widen)
2855 (org-agenda-finalize)
2856 (setq buffer-read-only t)
2857 (org-agenda-fit-window-to-buffer)))
2859 (defun org-agenda-normalize-custom-commands (cmds)
2860 "Normalize custom commands CMDS."
2861 (delq nil
2862 (mapcar
2863 (lambda (x)
2864 (cond ((stringp (cdr x)) nil)
2865 ((stringp (nth 1 x)) x)
2866 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2867 (t (cons (car x) (cons "" (cdr x))))))
2868 cmds)))
2870 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2871 "The user interface for selecting an agenda command."
2872 (catch 'exit
2873 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2874 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2875 (region-p (org-region-active-p))
2876 (custom org-agenda-custom-commands)
2877 (selstring "")
2878 restriction second-time
2879 c entry key type match prefixes rmheader header-end custom1 desc
2880 line lines left right n n1)
2881 (save-window-excursion
2882 (delete-other-windows)
2883 (org-switch-to-buffer-other-window " *Agenda Commands*")
2884 (erase-buffer)
2885 (insert (eval-when-compile
2886 (let ((header
2887 "Press key for an agenda command: < Buffer, subtree/region restriction
2888 -------------------------------- > Remove restriction
2889 a Agenda for current week or day e Export agenda views
2890 t List of all TODO entries T Entries with special TODO kwd
2891 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2892 s Search for keywords S Like s, but only TODO entries
2893 L Timeline for current buffer # List stuck projects (!=configure)
2894 / Multi-occur C Configure custom agenda commands
2895 ? Find :FLAGGED: entries * Toggle sticky agenda views
2897 (start 0))
2898 (while (string-match
2899 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2900 header start)
2901 (setq start (match-end 0))
2902 (add-text-properties (match-beginning 2) (match-end 2)
2903 '(face bold) header))
2904 header)))
2905 (setq header-end (point-marker))
2906 (while t
2907 (setq custom1 custom)
2908 (when (eq rmheader t)
2909 (org-goto-line 1)
2910 (re-search-forward ":" nil t)
2911 (delete-region (match-end 0) (point-at-eol))
2912 (forward-char 1)
2913 (looking-at "-+")
2914 (delete-region (match-end 0) (point-at-eol))
2915 (move-marker header-end (match-end 0)))
2916 (goto-char header-end)
2917 (delete-region (point) (point-max))
2919 ;; Produce all the lines that describe custom commands and prefixes
2920 (setq lines nil)
2921 (while (setq entry (pop custom1))
2922 (setq key (car entry) desc (nth 1 entry)
2923 type (nth 2 entry)
2924 match (nth 3 entry))
2925 (if (> (length key) 1)
2926 (add-to-list 'prefixes (string-to-char key))
2927 (setq line
2928 (format
2929 "%-4s%-14s"
2930 (org-add-props (copy-sequence key)
2931 '(face bold))
2932 (cond
2933 ((string-match "\\S-" desc) desc)
2934 ((eq type 'agenda) "Agenda for current week or day")
2935 ((eq type 'agenda*) "Appointments for current week or day")
2936 ((eq type 'alltodo) "List of all TODO entries")
2937 ((eq type 'search) "Word search")
2938 ((eq type 'stuck) "List of stuck projects")
2939 ((eq type 'todo) "TODO keyword")
2940 ((eq type 'tags) "Tags query")
2941 ((eq type 'tags-todo) "Tags (TODO)")
2942 ((eq type 'tags-tree) "Tags tree")
2943 ((eq type 'todo-tree) "TODO kwd tree")
2944 ((eq type 'occur-tree) "Occur tree")
2945 ((functionp type) (if (symbolp type)
2946 (symbol-name type)
2947 "Lambda expression"))
2948 (t "???"))))
2949 (if org-agenda-menu-show-matcher
2950 (setq line
2951 (concat line ": "
2952 (cond
2953 ((stringp match)
2954 (setq match (copy-sequence match))
2955 (org-add-props match nil 'face 'org-warning))
2956 ((listp type)
2957 (format "set of %d commands" (length type))))))
2958 (if (org-string-nw-p match)
2959 (add-text-properties
2960 0 (length line) (list 'help-echo
2961 (concat "Matcher: " match)) line)))
2962 (push line lines)))
2963 (setq lines (nreverse lines))
2964 (when prefixes
2965 (mapc (lambda (x)
2966 (push
2967 (format "%s %s"
2968 (org-add-props (char-to-string x)
2969 nil 'face 'bold)
2970 (or (cdr (assoc (concat selstring
2971 (char-to-string x))
2972 prefix-descriptions))
2973 "Prefix key"))
2974 lines))
2975 prefixes))
2977 ;; Check if we should display in two columns
2978 (if org-agenda-menu-two-columns
2979 (progn
2980 (setq n (length lines)
2981 n1 (+ (/ n 2) (mod n 2))
2982 right (nthcdr n1 lines)
2983 left (copy-sequence lines))
2984 (setcdr (nthcdr (1- n1) left) nil))
2985 (setq left lines right nil))
2986 (while left
2987 (insert "\n" (pop left))
2988 (when right
2989 (if (< (current-column) 40)
2990 (move-to-column 40 t)
2991 (insert " "))
2992 (insert (pop right))))
2994 ;; Make the window the right size
2995 (goto-char (point-min))
2996 (if second-time
2997 (if (not (pos-visible-in-window-p (point-max)))
2998 (org-fit-window-to-buffer))
2999 (setq second-time t)
3000 (org-fit-window-to-buffer))
3002 ;; Ask for selection
3003 (message "Press key for agenda command%s:"
3004 (if (or restrict-ok org-agenda-overriding-restriction)
3005 (if org-agenda-overriding-restriction
3006 " (restriction lock active)"
3007 (if restriction
3008 (format " (restricted to %s)" restriction)
3009 " (unrestricted)"))
3010 ""))
3011 (setq c (read-char-exclusive))
3012 (message "")
3013 (cond
3014 ((assoc (char-to-string c) custom)
3015 (setq selstring (concat selstring (char-to-string c)))
3016 (throw 'exit (cons selstring restriction)))
3017 ((memq c prefixes)
3018 (setq selstring (concat selstring (char-to-string c))
3019 prefixes nil
3020 rmheader (or rmheader t)
3021 custom (delq nil (mapcar
3022 (lambda (x)
3023 (if (or (= (length (car x)) 1)
3024 (/= (string-to-char (car x)) c))
3026 (cons (substring (car x) 1) (cdr x))))
3027 custom))))
3028 ((eq c ?*)
3029 (call-interactively 'org-toggle-sticky-agenda)
3030 (sit-for 2))
3031 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3032 (message "Restriction is only possible in Org-mode buffers")
3033 (ding) (sit-for 1))
3034 ((eq c ?1)
3035 (org-agenda-remove-restriction-lock 'noupdate)
3036 (setq restriction 'buffer))
3037 ((eq c ?0)
3038 (org-agenda-remove-restriction-lock 'noupdate)
3039 (setq restriction (if region-p 'region 'subtree)))
3040 ((eq c ?<)
3041 (org-agenda-remove-restriction-lock 'noupdate)
3042 (setq restriction
3043 (cond
3044 ((eq restriction 'buffer)
3045 (if region-p 'region 'subtree))
3046 ((memq restriction '(subtree region))
3047 nil)
3048 (t 'buffer))))
3049 ((eq c ?>)
3050 (org-agenda-remove-restriction-lock 'noupdate)
3051 (setq restriction nil))
3052 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3053 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3054 ((and (> (length selstring) 0) (eq c ?\d))
3055 (delete-window)
3056 (org-agenda-get-restriction-and-command prefix-descriptions))
3058 ((equal c ?q) (error "Abort"))
3059 (t (user-error "Invalid key %c" c))))))))
3061 (defun org-agenda-fit-window-to-buffer ()
3062 "Fit the window to the buffer size."
3063 (and (memq org-agenda-window-setup '(reorganize-frame))
3064 (fboundp 'fit-window-to-buffer)
3065 (org-fit-window-to-buffer
3067 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3068 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3070 (defvar org-cmd nil)
3071 (defvar org-agenda-overriding-cmd nil)
3072 (defvar org-agenda-overriding-arguments nil)
3073 (defvar org-agenda-overriding-cmd-arguments nil)
3074 (defun org-agenda-run-series (name series)
3075 "Run agenda NAME as a SERIES of agenda commands."
3076 (org-let (nth 1 series) '(org-agenda-prepare name))
3077 ;; We need to reset agenda markers here, because when constructing a
3078 ;; block agenda, the individual blocks do not do that.
3079 (org-agenda-reset-markers)
3080 (let* ((org-agenda-multi t)
3081 (redo (list 'org-agenda-run-series name (list 'quote series)))
3082 (cmds (car series))
3083 (gprops (nth 1 series))
3084 match ;; The byte compiler incorrectly complains about this. Keep it!
3085 org-cmd type lprops)
3086 (while (setq org-cmd (pop cmds))
3087 (setq type (car org-cmd)
3088 match (eval (nth 1 org-cmd))
3089 lprops (nth 2 org-cmd))
3090 (let ((org-agenda-overriding-arguments
3091 (if (eq org-agenda-overriding-cmd org-cmd)
3092 (or org-agenda-overriding-arguments
3093 org-agenda-overriding-cmd-arguments))))
3094 (cond
3095 ((eq type 'agenda)
3096 (org-let2 gprops lprops
3097 '(call-interactively 'org-agenda-list)))
3098 ((eq type 'agenda*)
3099 (org-let2 gprops lprops
3100 '(funcall 'org-agenda-list nil nil t)))
3101 ((eq type 'alltodo)
3102 (org-let2 gprops lprops
3103 '(call-interactively 'org-todo-list)))
3104 ((eq type 'search)
3105 (org-let2 gprops lprops
3106 '(org-search-view current-prefix-arg match nil)))
3107 ((eq type 'stuck)
3108 (org-let2 gprops lprops
3109 '(call-interactively 'org-agenda-list-stuck-projects)))
3110 ((eq type 'tags)
3111 (org-let2 gprops lprops
3112 '(org-tags-view current-prefix-arg match)))
3113 ((eq type 'tags-todo)
3114 (org-let2 gprops lprops
3115 '(org-tags-view '(4) match)))
3116 ((eq type 'todo)
3117 (org-let2 gprops lprops
3118 '(org-todo-list match)))
3119 ((fboundp type)
3120 (org-let2 gprops lprops
3121 '(funcall type match)))
3122 (t (error "Invalid type in command series")))))
3123 (widen)
3124 (let ((inhibit-read-only t))
3125 (add-text-properties (point-min) (point-max)
3126 `(org-series t org-series-redo-cmd ,redo)))
3127 (setq org-agenda-redo-command redo)
3128 (goto-char (point-min)))
3129 (org-agenda-fit-window-to-buffer)
3130 (org-let (nth 1 series) '(org-agenda-finalize)))
3132 ;;;###autoload
3133 (defmacro org-batch-agenda (cmd-key &rest parameters)
3134 "Run an agenda command in batch mode and send the result to STDOUT.
3135 If CMD-KEY is a string of length 1, it is used as a key in
3136 `org-agenda-custom-commands' and triggers this command. If it is a
3137 longer string it is used as a tags/todo match string.
3138 Parameters are alternating variable names and values that will be bound
3139 before running the agenda command."
3140 (org-eval-in-environment (org-make-parameter-alist parameters)
3141 (let (org-agenda-sticky)
3142 (if (> (length cmd-key) 2)
3143 (org-tags-view nil cmd-key)
3144 (org-agenda nil cmd-key))))
3145 (set-buffer org-agenda-buffer-name)
3146 (princ (buffer-string)))
3148 (defvar org-agenda-info nil)
3150 ;;;###autoload
3151 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3152 "Run an agenda command in batch mode and send the result to STDOUT.
3153 If CMD-KEY is a string of length 1, it is used as a key in
3154 `org-agenda-custom-commands' and triggers this command. If it is a
3155 longer string it is used as a tags/todo match string.
3156 Parameters are alternating variable names and values that will be bound
3157 before running the agenda command.
3159 The output gives a line for each selected agenda item. Each
3160 item is a list of comma-separated values, like this:
3162 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3164 category The category of the item
3165 head The headline, without TODO kwd, TAGS and PRIORITY
3166 type The type of the agenda entry, can be
3167 todo selected in TODO match
3168 tagsmatch selected in tags match
3169 diary imported from diary
3170 deadline a deadline on given date
3171 scheduled scheduled on given date
3172 timestamp entry has timestamp on given date
3173 closed entry was closed on given date
3174 upcoming-deadline warning about deadline
3175 past-scheduled forwarded scheduled item
3176 block entry has date block including g. date
3177 todo The todo keyword, if any
3178 tags All tags including inherited ones, separated by colons
3179 date The relevant date, like 2007-2-14
3180 time The time, like 15:00-16:50
3181 extra Sting with extra planning info
3182 priority-l The priority letter if any was given
3183 priority-n The computed numerical priority
3184 agenda-day The day in the agenda where this is listed"
3185 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3186 (org-make-parameter-alist parameters))
3187 (if (> (length cmd-key) 2)
3188 (org-tags-view nil cmd-key)
3189 (org-agenda nil cmd-key)))
3190 (set-buffer org-agenda-buffer-name)
3191 (let* ((lines (org-split-string (buffer-string) "\n"))
3192 line)
3193 (while (setq line (pop lines))
3194 (catch 'next
3195 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3196 (setq org-agenda-info
3197 (org-fix-agenda-info (text-properties-at 0 line)))
3198 (princ
3199 (mapconcat 'org-agenda-export-csv-mapper
3200 '(org-category txt type todo tags date time extra
3201 priority-letter priority agenda-day)
3202 ","))
3203 (princ "\n")))))
3205 (defun org-fix-agenda-info (props)
3206 "Make sure all properties on an agenda item have a canonical form.
3207 This ensures the export commands can easily use it."
3208 (let (tmp re)
3209 (when (setq tmp (plist-get props 'tags))
3210 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3211 (when (setq tmp (plist-get props 'date))
3212 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3213 (let ((calendar-date-display-form '(year "-" month "-" day)))
3214 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3216 (setq tmp (calendar-date-string tmp)))
3217 (setq props (plist-put props 'date tmp)))
3218 (when (setq tmp (plist-get props 'day))
3219 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3220 (let ((calendar-date-display-form '(year "-" month "-" day)))
3221 (setq tmp (calendar-date-string tmp)))
3222 (setq props (plist-put props 'day tmp))
3223 (setq props (plist-put props 'agenda-day tmp)))
3224 (when (setq tmp (plist-get props 'txt))
3225 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3226 (plist-put props 'priority-letter (match-string 1 tmp))
3227 (setq tmp (replace-match "" t t tmp)))
3228 (when (and (setq re (plist-get props 'org-todo-regexp))
3229 (setq re (concat "\\`\\.*" re " ?"))
3230 (string-match re tmp))
3231 (plist-put props 'todo (match-string 1 tmp))
3232 (setq tmp (replace-match "" t t tmp)))
3233 (plist-put props 'txt tmp)))
3234 props)
3236 (defun org-agenda-export-csv-mapper (prop)
3237 (let ((res (plist-get org-agenda-info prop)))
3238 (setq res
3239 (cond
3240 ((not res) "")
3241 ((stringp res) res)
3242 (t (prin1-to-string res))))
3243 (while (string-match "," res)
3244 (setq res (replace-match ";" t t res)))
3245 (org-trim res)))
3247 ;;;###autoload
3248 (defun org-store-agenda-views (&rest parameters)
3249 "Store agenda views."
3250 (interactive)
3251 (eval (list 'org-batch-store-agenda-views)))
3253 ;;;###autoload
3254 (defmacro org-batch-store-agenda-views (&rest parameters)
3255 "Run all custom agenda commands that have a file argument."
3256 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3257 (pop-up-frames nil)
3258 (dir default-directory)
3259 (pars (org-make-parameter-alist parameters))
3260 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3261 (save-window-excursion
3262 (while cmds
3263 (setq cmd (pop cmds)
3264 thiscmdkey (car cmd)
3265 thiscmdcmd (cdr cmd)
3266 match (nth 2 thiscmdcmd)
3267 bufname (if org-agenda-sticky
3268 (or (and (stringp match)
3269 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3270 (format "*Org Agenda(%s)*" thiscmdkey))
3271 org-agenda-buffer-name)
3272 cmd-or-set (nth 2 cmd)
3273 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3274 files (nth (if (listp cmd-or-set) 4 5) cmd))
3275 (if (stringp files) (setq files (list files)))
3276 (when files
3277 (org-eval-in-environment (append org-agenda-exporter-settings
3278 opts pars)
3279 (org-agenda nil thiscmdkey))
3280 (set-buffer bufname)
3281 (while files
3282 (org-eval-in-environment (append org-agenda-exporter-settings
3283 opts pars)
3284 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3285 (and (get-buffer bufname)
3286 (kill-buffer bufname)))))))
3288 (defvar org-agenda-current-span nil
3289 "The current span used in the agenda view.") ; local variable in the agenda buffer
3290 (defun org-agenda-mark-header-line (pos)
3291 "Mark the line at POS as an agenda structure header."
3292 (save-excursion
3293 (goto-char pos)
3294 (put-text-property (point-at-bol) (point-at-eol)
3295 'org-agenda-structural-header t)
3296 (when org-agenda-title-append
3297 (put-text-property (point-at-bol) (point-at-eol)
3298 'org-agenda-title-append org-agenda-title-append))))
3300 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3301 (defvar org-agenda-write-buffer-name "Agenda View")
3302 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3303 "Write the current buffer (an agenda view) as a file.
3304 Depending on the extension of the file name, plain text (.txt),
3305 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3306 If the extension is .ics, run icalendar export over all files used
3307 to construct the agenda and limit the export to entries listed in the
3308 agenda now.
3309 If the extension is .org, collect all subtrees corresponding to the
3310 agenda entries and add them in an .org file.
3311 With prefix argument OPEN, open the new file immediately.
3312 If NOSETTINGS is given, do not scope the settings of
3313 `org-agenda-exporter-settings' into the export commands. This is used when
3314 the settings have already been scoped and we do not wish to overrule other,
3315 higher priority settings.
3316 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3317 (interactive "FWrite agenda to file: \nP")
3318 (if (or (not (file-writable-p file))
3319 (and (file-exists-p file)
3320 (if (org-called-interactively-p 'any)
3321 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3322 (user-error "Cannot write agenda to file %s" file))
3323 (org-let (if nosettings nil org-agenda-exporter-settings)
3324 '(save-excursion
3325 (save-window-excursion
3326 (let ((bs (copy-sequence (buffer-string))) beg content)
3327 (with-temp-buffer
3328 (rename-buffer org-agenda-write-buffer-name t)
3329 (set-buffer-modified-p nil)
3330 (insert bs)
3331 (org-agenda-remove-marked-text 'org-filtered)
3332 (run-hooks 'org-agenda-before-write-hook)
3333 (cond
3334 ((org-bound-and-true-p org-mobile-creating-agendas)
3335 (org-mobile-write-agenda-for-mobile file))
3336 ((string-match "\\.org\\'" file)
3337 (let (content p m message-log-max)
3338 (goto-char (point-min))
3339 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3340 (goto-char p)
3341 (setq m (get-text-property (point) 'org-hd-marker))
3342 (when m
3343 (push (save-excursion
3344 (set-buffer (marker-buffer m))
3345 (goto-char m)
3346 (org-copy-subtree 1 nil t t)
3347 org-subtree-clip)
3348 content)))
3349 (find-file file)
3350 (erase-buffer)
3351 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3352 (write-file file)
3353 (kill-buffer (current-buffer))
3354 (message "Org file written to %s" file)))
3355 ((string-match "\\.html?\\'" file)
3356 (require 'htmlize)
3357 (set-buffer (htmlize-buffer (current-buffer)))
3358 (when org-agenda-export-html-style
3359 ;; replace <style> section with org-agenda-export-html-style
3360 (goto-char (point-min))
3361 (kill-region (- (search-forward "<style") 6)
3362 (search-forward "</style>"))
3363 (insert org-agenda-export-html-style))
3364 (write-file file)
3365 (kill-buffer (current-buffer))
3366 (message "HTML written to %s" file))
3367 ((string-match "\\.ps\\'" file)
3368 (require 'ps-print)
3369 (ps-print-buffer-with-faces file)
3370 (message "Postscript written to %s" file))
3371 ((string-match "\\.pdf\\'" file)
3372 (require 'ps-print)
3373 (ps-print-buffer-with-faces
3374 (concat (file-name-sans-extension file) ".ps"))
3375 (call-process "ps2pdf" nil nil nil
3376 (expand-file-name
3377 (concat (file-name-sans-extension file) ".ps"))
3378 (expand-file-name file))
3379 (delete-file (concat (file-name-sans-extension file) ".ps"))
3380 (message "PDF written to %s" file))
3381 ((string-match "\\.ics\\'" file)
3382 (require 'ox-icalendar)
3383 (org-icalendar-export-current-agenda (expand-file-name file)))
3385 (let ((bs (buffer-string)))
3386 (find-file file)
3387 (erase-buffer)
3388 (insert bs)
3389 (save-buffer 0)
3390 (kill-buffer (current-buffer))
3391 (message "Plain text written to %s" file))))))))
3392 (set-buffer (or agenda-bufname
3393 (and (org-called-interactively-p 'any) (buffer-name))
3394 org-agenda-buffer-name)))
3395 (when open (org-open-file file)))
3397 (defun org-agenda-remove-marked-text (property &optional value)
3398 "Delete all text marked with VALUE of PROPERTY.
3399 VALUE defaults to t."
3400 (let (beg)
3401 (setq value (or value t))
3402 (while (setq beg (text-property-any (point-min) (point-max)
3403 property value))
3404 (delete-region
3405 beg (or (next-single-property-change beg property)
3406 (point-max))))))
3408 (defun org-agenda-add-entry-text ()
3409 "Add entry text to agenda lines.
3410 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3411 entry text following headings shown in the agenda.
3412 Drawers will be excluded, also the line with scheduling/deadline info."
3413 (when (and (> org-agenda-add-entry-text-maxlines 0)
3414 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3415 (let (m txt)
3416 (goto-char (point-min))
3417 (while (not (eobp))
3418 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3419 (beginning-of-line 2)
3420 (setq txt (org-agenda-get-some-entry-text
3421 m org-agenda-add-entry-text-maxlines " > "))
3422 (end-of-line 1)
3423 (if (string-match "\\S-" txt)
3424 (insert "\n" txt)
3425 (or (eobp) (forward-char 1))))))))
3427 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3428 &rest keep)
3429 "Extract entry text from MARKER, at most N-LINES lines.
3430 This will ignore drawers etc, just get the text.
3431 If INDENT is given, prefix every line with this string. If KEEP is
3432 given, it is a list of symbols, defining stuff that should not be
3433 removed from the entry content. Currently only `planning' is allowed here."
3434 (let (txt drawer-re kwd-time-re ind)
3435 (save-excursion
3436 (with-current-buffer (marker-buffer marker)
3437 (if (not (derived-mode-p 'org-mode))
3438 (setq txt "")
3439 (save-excursion
3440 (save-restriction
3441 (widen)
3442 (goto-char marker)
3443 (end-of-line 1)
3444 (setq txt (buffer-substring
3445 (min (1+ (point)) (point-max))
3446 (progn (outline-next-heading) (point)))
3447 drawer-re org-drawer-regexp
3448 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3449 ".*\n?"))
3450 (with-temp-buffer
3451 (insert txt)
3452 (when org-agenda-add-entry-text-descriptive-links
3453 (goto-char (point-min))
3454 (while (org-activate-bracket-links (point-max))
3455 (add-text-properties (match-beginning 0) (match-end 0)
3456 '(face org-link))))
3457 (goto-char (point-min))
3458 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3459 (set-text-properties (match-beginning 0) (match-end 0)
3460 nil))
3461 (goto-char (point-min))
3462 (while (re-search-forward drawer-re nil t)
3463 (delete-region
3464 (match-beginning 0)
3465 (progn (re-search-forward
3466 "^[ \t]*:END:.*\n?" nil 'move)
3467 (point))))
3468 (unless (member 'planning keep)
3469 (goto-char (point-min))
3470 (while (re-search-forward kwd-time-re nil t)
3471 (replace-match "")))
3472 (goto-char (point-min))
3473 (when org-agenda-entry-text-exclude-regexps
3474 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3475 (while (setq re (pop re-list))
3476 (goto-char (point-min))
3477 (while (re-search-forward re nil t)
3478 (replace-match "")))))
3479 (goto-char (point-max))
3480 (skip-chars-backward " \t\n")
3481 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3483 ;; find and remove min common indentation
3484 (goto-char (point-min))
3485 (untabify (point-min) (point-max))
3486 (setq ind (org-get-indentation))
3487 (while (not (eobp))
3488 (unless (looking-at "[ \t]*$")
3489 (setq ind (min ind (org-get-indentation))))
3490 (beginning-of-line 2))
3491 (goto-char (point-min))
3492 (while (not (eobp))
3493 (unless (looking-at "[ \t]*$")
3494 (move-to-column ind)
3495 (delete-region (point-at-bol) (point)))
3496 (beginning-of-line 2))
3498 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3500 (goto-char (point-min))
3501 (when indent
3502 (while (and (not (eobp)) (re-search-forward "^" nil t))
3503 (replace-match indent t t)))
3504 (goto-char (point-min))
3505 (while (looking-at "[ \t]*\n") (replace-match ""))
3506 (goto-char (point-max))
3507 (when (> (org-current-line)
3508 n-lines)
3509 (org-goto-line (1+ n-lines))
3510 (backward-char 1))
3511 (setq txt (buffer-substring (point-min) (point)))))))))
3512 txt))
3514 (defun org-check-for-org-mode ()
3515 "Make sure current buffer is in org-mode. Error if not."
3516 (or (derived-mode-p 'org-mode)
3517 (error "Cannot execute org-mode agenda command on buffer in %s"
3518 major-mode)))
3520 ;;; Agenda prepare and finalize
3522 (defvar org-agenda-multi nil) ; dynamically scoped
3523 (defvar org-agenda-pre-window-conf nil)
3524 (defvar org-agenda-columns-active nil)
3525 (defvar org-agenda-name nil)
3526 (defvar org-agenda-tag-filter nil)
3527 (defvar org-agenda-category-filter nil)
3528 (defvar org-agenda-regexp-filter nil)
3529 (defvar org-agenda-top-headline-filter nil)
3530 (defvar org-agenda-tag-filter-while-redo nil)
3531 (defvar org-agenda-tag-filter-preset nil
3532 "A preset of the tags filter used for secondary agenda filtering.
3533 This must be a list of strings, each string must be a single tag preceded
3534 by \"+\" or \"-\".
3535 This variable should not be set directly, but agenda custom commands can
3536 bind it in the options section. The preset filter is a global property of
3537 the entire agenda view. In a block agenda, it will not work reliably to
3538 define a filter for one of the individual blocks. You need to set it in
3539 the global options and expect it to be applied to the entire view.")
3541 (defvar org-agenda-category-filter-preset nil
3542 "A preset of the category filter used for secondary agenda filtering.
3543 This must be a list of strings, each string must be a single category
3544 preceded by \"+\" or \"-\".
3545 This variable should not be set directly, but agenda custom commands can
3546 bind it in the options section. The preset filter is a global property of
3547 the entire agenda view. In a block agenda, it will not work reliably to
3548 define a filter for one of the individual blocks. You need to set it in
3549 the global options and expect it to be applied to the entire view.")
3551 (defvar org-agenda-regexp-filter-preset nil
3552 "A preset of the regexp filter used for secondary agenda filtering.
3553 This must be a list of strings, each string must be a single regexp
3554 preceded by \"+\" or \"-\".
3555 This variable should not be set directly, but agenda custom commands can
3556 bind it in the options section. The preset filter is a global property of
3557 the entire agenda view. In a block agenda, it will not work reliably to
3558 define a filter for one of the individual blocks. You need to set it in
3559 the global options and expect it to be applied to the entire view.")
3561 (defun org-agenda-use-sticky-p ()
3562 "Return non-nil if an agenda buffer named
3563 `org-agenda-buffer-name' exists and should be shown instead of
3564 generating a new one."
3565 (and
3566 ;; turned off by user
3567 org-agenda-sticky
3568 ;; For multi-agenda buffer already exists
3569 (not org-agenda-multi)
3570 ;; buffer found
3571 (get-buffer org-agenda-buffer-name)
3572 ;; C-u parameter is same as last call
3573 (with-current-buffer (get-buffer org-agenda-buffer-name)
3574 (and
3575 (equal current-prefix-arg
3576 org-agenda-last-prefix-arg)
3577 ;; In case user turned stickiness on, while having existing
3578 ;; Agenda buffer active, don't reuse that buffer, because it
3579 ;; does not have org variables local
3580 org-agenda-this-buffer-is-sticky))))
3582 (defun org-agenda-prepare-window (abuf)
3583 "Setup agenda buffer in the window."
3584 (let* ((awin (get-buffer-window abuf))
3585 wconf)
3586 (cond
3587 ((equal (current-buffer) abuf) nil)
3588 (awin (select-window awin))
3589 ((not (setq wconf (current-window-configuration))))
3590 ((equal org-agenda-window-setup 'current-window)
3591 (org-pop-to-buffer-same-window abuf))
3592 ((equal org-agenda-window-setup 'other-window)
3593 (org-switch-to-buffer-other-window abuf))
3594 ((equal org-agenda-window-setup 'other-frame)
3595 (switch-to-buffer-other-frame abuf))
3596 ((equal org-agenda-window-setup 'reorganize-frame)
3597 (delete-other-windows)
3598 (org-switch-to-buffer-other-window abuf)))
3599 ;; additional test in case agenda is invoked from within agenda
3600 ;; buffer via elisp link
3601 (unless (equal (current-buffer) abuf)
3602 (org-pop-to-buffer-same-window abuf))
3603 (setq org-agenda-pre-window-conf
3604 (or org-agenda-pre-window-conf wconf))))
3606 (defun org-agenda-prepare (&optional name)
3607 (if (org-agenda-use-sticky-p)
3608 (progn
3609 ;; Popup existing buffer
3610 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3611 (message "Sticky Agenda buffer, use `r' to refresh")
3612 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3613 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3614 (setq org-todo-keywords-for-agenda nil)
3615 (setq org-drawers-for-agenda nil)
3616 (unless org-agenda-persistent-filter
3617 (setq org-agenda-tag-filter nil
3618 org-agenda-category-filter nil
3619 org-agenda-regexp-filter nil))
3620 (put 'org-agenda-tag-filter :preset-filter
3621 org-agenda-tag-filter-preset)
3622 (put 'org-agenda-category-filter :preset-filter
3623 org-agenda-category-filter-preset)
3624 (put 'org-agenda-regexp-filter :preset-filter
3625 org-agenda-regexp-filter-preset)
3626 (if org-agenda-multi
3627 (progn
3628 (setq buffer-read-only nil)
3629 (goto-char (point-max))
3630 (unless (or (bobp) org-agenda-compact-blocks
3631 (not org-agenda-block-separator))
3632 (insert "\n"
3633 (if (stringp org-agenda-block-separator)
3634 org-agenda-block-separator
3635 (make-string (window-width) org-agenda-block-separator))
3636 "\n"))
3637 (narrow-to-region (point) (point-max)))
3638 (setq org-done-keywords-for-agenda nil)
3640 ;; Setting any org variables that are in org-agenda-local-vars
3641 ;; list need to be done after the prepare call
3642 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3643 (setq buffer-read-only nil)
3644 (org-agenda-reset-markers)
3645 (let ((inhibit-read-only t)) (erase-buffer))
3646 (org-agenda-mode)
3647 (setq org-agenda-buffer (current-buffer))
3648 (setq org-agenda-contributing-files nil)
3649 (setq org-agenda-columns-active nil)
3650 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3651 (setq org-todo-keywords-for-agenda
3652 (org-uniquify org-todo-keywords-for-agenda))
3653 (setq org-done-keywords-for-agenda
3654 (org-uniquify org-done-keywords-for-agenda))
3655 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3656 (setq org-agenda-last-prefix-arg current-prefix-arg)
3657 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3658 (and name (not org-agenda-name)
3659 (org-set-local 'org-agenda-name name)))
3660 (setq buffer-read-only nil)))
3662 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3663 (defun org-agenda-finalize ()
3664 "Finishing touch for the agenda buffer, called just before displaying it."
3665 (unless org-agenda-multi
3666 (save-excursion
3667 (let ((inhibit-read-only t))
3668 (goto-char (point-min))
3669 (save-excursion
3670 (while (org-activate-bracket-links (point-max))
3671 (add-text-properties (match-beginning 0) (match-end 0)
3672 '(face org-link))))
3673 (save-excursion
3674 (while (org-activate-plain-links (point-max))
3675 (add-text-properties (match-beginning 0) (match-end 0)
3676 '(face org-link))))
3677 (unless (eq org-agenda-remove-tags t)
3678 (org-agenda-align-tags))
3679 (unless org-agenda-with-colors
3680 (remove-text-properties (point-min) (point-max) '(face nil)))
3681 (if (and (boundp 'org-agenda-overriding-columns-format)
3682 org-agenda-overriding-columns-format)
3683 (org-set-local 'org-agenda-overriding-columns-format
3684 org-agenda-overriding-columns-format))
3685 (if (and (boundp 'org-agenda-view-columns-initially)
3686 org-agenda-view-columns-initially)
3687 (org-agenda-columns))
3688 (when org-agenda-fontify-priorities
3689 (org-agenda-fontify-priorities))
3690 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3691 (org-agenda-dim-blocked-tasks))
3692 (org-agenda-mark-clocking-task)
3693 (when org-agenda-entry-text-mode
3694 (org-agenda-entry-text-hide)
3695 (org-agenda-entry-text-show))
3696 (if (and (functionp 'org-habit-insert-consistency-graphs)
3697 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3698 (org-habit-insert-consistency-graphs))
3699 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3700 (unless (or (eq org-agenda-show-inherited-tags 'always)
3701 (and (listp org-agenda-show-inherited-tags)
3702 (memq org-agenda-type org-agenda-show-inherited-tags))
3703 (and (eq org-agenda-show-inherited-tags t)
3704 (or (eq org-agenda-use-tag-inheritance t)
3705 (and (listp org-agenda-use-tag-inheritance)
3706 (not (memq org-agenda-type
3707 org-agenda-use-tag-inheritance))))))
3708 (let (mrk)
3709 (save-excursion
3710 (goto-char (point-min))
3711 (while (equal (forward-line) 0)
3712 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3713 (get-text-property (point) 'org-hd-marker)))
3714 (put-text-property (point-at-bol) (point-at-eol)
3715 'tags (org-with-point-at mrk
3716 (delete-dups
3717 (mapcar 'downcase (org-get-tags-at))))))))))
3718 (run-hooks 'org-agenda-finalize-hook)
3719 (when org-agenda-top-headline-filter
3720 (org-agenda-filter-top-headline-apply
3721 org-agenda-top-headline-filter))
3722 (when org-agenda-tag-filter
3723 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3724 (when (get 'org-agenda-tag-filter :preset-filter)
3725 (org-agenda-filter-apply
3726 (get 'org-agenda-tag-filter :preset-filter) 'tag))
3727 (when org-agenda-category-filter
3728 (org-agenda-filter-apply org-agenda-category-filter 'category))
3729 (when (get 'org-agenda-category-filter :preset-filter)
3730 (org-agenda-filter-apply
3731 (get 'org-agenda-category-filter :preset-filter) 'category))
3732 (when org-agenda-regexp-filter
3733 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3734 (when (get 'org-agenda-regexp-filter :preset-filter)
3735 (org-agenda-filter-apply
3736 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3737 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3739 (defun org-agenda-mark-clocking-task ()
3740 "Mark the current clock entry in the agenda if it is present."
3741 ;; We need to widen when `org-agenda-finalize' is called from
3742 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3743 (when org-clock-current-task
3744 (save-restriction
3745 (widen)
3746 (org-agenda-unmark-clocking-task)
3747 (when (marker-buffer org-clock-hd-marker)
3748 (save-excursion
3749 (goto-char (point-min))
3750 (let (s ov)
3751 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3752 (goto-char s)
3753 (when (equal (org-get-at-bol 'org-hd-marker)
3754 org-clock-hd-marker)
3755 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3756 (overlay-put ov 'type 'org-agenda-clocking)
3757 (overlay-put ov 'face 'org-agenda-clocking)
3758 (overlay-put ov 'help-echo
3759 "The clock is running in this item")))))))))
3761 (defun org-agenda-unmark-clocking-task ()
3762 "Unmark the current clocking task."
3763 (mapc (lambda (o)
3764 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3765 (delete-overlay o)))
3766 (overlays-in (point-min) (point-max))))
3768 (defun org-agenda-fontify-priorities ()
3769 "Make highest priority lines bold, and lowest italic."
3770 (interactive)
3771 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3772 (delete-overlay o)))
3773 (overlays-in (point-min) (point-max)))
3774 (save-excursion
3775 (let (b e p ov h l)
3776 (goto-char (point-min))
3777 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3778 (setq h (or (get-char-property (point) 'org-highest-priority)
3779 org-highest-priority)
3780 l (or (get-char-property (point) 'org-lowest-priority)
3781 org-lowest-priority)
3782 p (string-to-char (match-string 1))
3783 b (match-beginning 0)
3784 e (if (eq org-agenda-fontify-priorities 'cookies)
3785 (match-end 0)
3786 (point-at-eol))
3787 ov (make-overlay b e))
3788 (overlay-put
3789 ov 'face
3790 (cons (cond ((org-face-from-face-or-color
3791 'priority nil
3792 (cdr (assoc p org-priority-faces))))
3793 ((and (listp org-agenda-fontify-priorities)
3794 (org-face-from-face-or-color
3795 'priority nil
3796 (cdr (assoc p org-agenda-fontify-priorities)))))
3797 ((equal p l) 'italic)
3798 ((equal p h) 'bold))
3799 'org-priority))
3800 (overlay-put ov 'org-type 'org-priority)))))
3802 (defvar org-depend-tag-blocked)
3804 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3805 "Dim currently blocked TODO's in the agenda display.
3806 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3807 dimming them."
3808 (interactive "P")
3809 (when (org-called-interactively-p 'interactive)
3810 (message "Dim or hide blocked tasks..."))
3811 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3812 (delete-overlay o)))
3813 (overlays-in (point-min) (point-max)))
3814 (save-excursion
3815 (let ((inhibit-read-only t)
3816 (org-depend-tag-blocked nil)
3817 (invis (or (not (null invisible))
3818 (eq org-agenda-dim-blocked-tasks 'invisible)))
3819 org-blocked-by-checkboxes
3820 invis1 b e p ov h l)
3821 (goto-char (point-min))
3822 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3823 (and pos (goto-char (1+ pos))))
3824 (setq org-blocked-by-checkboxes nil invis1 invis)
3825 (let ((marker (org-get-at-bol 'org-hd-marker)))
3826 (when (and marker
3827 (with-current-buffer (marker-buffer marker)
3828 (save-excursion (goto-char marker)
3829 (org-entry-blocked-p))))
3830 (if org-blocked-by-checkboxes (setq invis1 nil))
3831 (setq b (if invis1
3832 (max (point-min) (1- (point-at-bol)))
3833 (point-at-bol))
3834 e (point-at-eol)
3835 ov (make-overlay b e))
3836 (if invis1
3837 (progn (overlay-put ov 'invisible t)
3838 (overlay-put ov 'intangible t))
3839 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3840 (overlay-put ov 'org-type 'org-blocked-todo))))))
3841 (when (org-called-interactively-p 'interactive)
3842 (message "Dim or hide blocked tasks...done")))
3844 (defvar org-agenda-skip-function nil
3845 "Function to be called at each match during agenda construction.
3846 If this function returns nil, the current match should not be skipped.
3847 Otherwise, the function must return a position from where the search
3848 should be continued.
3849 This may also be a Lisp form, it will be evaluated.
3850 Never set this variable using `setq' or so, because then it will apply
3851 to all future agenda commands. If you do want a global skipping condition,
3852 use the option `org-agenda-skip-function-global' instead.
3853 The correct usage for `org-agenda-skip-function' is to bind it with
3854 `let' to scope it dynamically into the agenda-constructing command.
3855 A good way to set it is through options in `org-agenda-custom-commands'.")
3857 (defun org-agenda-skip ()
3858 "Throw to `:skip' in places that should be skipped.
3859 Also moves point to the end of the skipped region, so that search can
3860 continue from there."
3861 (let ((p (point-at-bol)) to)
3862 (when (or
3863 (save-excursion (goto-char p) (looking-at comment-start-skip))
3864 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3865 (get-text-property p :org-archived)
3866 (org-end-of-subtree t))
3867 (and org-agenda-skip-comment-trees
3868 (get-text-property p :org-comment)
3869 (org-end-of-subtree t))
3870 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3871 (org-agenda-skip-eval org-agenda-skip-function)))
3872 (goto-char to))
3873 (org-in-src-block-p t))
3874 (throw :skip t))))
3876 (defun org-agenda-skip-eval (form)
3877 "If FORM is a function or a list, call (or eval) it and return the result.
3878 `save-excursion' and `save-match-data' are wrapped around the call, so point
3879 and match data are returned to the previous state no matter what these
3880 functions do."
3881 (let (fp)
3882 (and form
3883 (or (setq fp (functionp form))
3884 (consp form))
3885 (save-excursion
3886 (save-match-data
3887 (if fp
3888 (funcall form)
3889 (eval form)))))))
3891 (defvar org-agenda-markers nil
3892 "List of all currently active markers created by `org-agenda'.")
3893 (defvar org-agenda-last-marker-time (org-float-time)
3894 "Creation time of the last agenda marker.")
3896 (defun org-agenda-new-marker (&optional pos)
3897 "Return a new agenda marker.
3898 Org-mode keeps a list of these markers and resets them when they are
3899 no longer in use."
3900 (let ((m (copy-marker (or pos (point)))))
3901 (setq org-agenda-last-marker-time (org-float-time))
3902 (if org-agenda-buffer
3903 (with-current-buffer org-agenda-buffer
3904 (push m org-agenda-markers))
3905 (push m org-agenda-markers))
3908 (defun org-agenda-reset-markers ()
3909 "Reset markers created by `org-agenda'."
3910 (while org-agenda-markers
3911 (move-marker (pop org-agenda-markers) nil)))
3913 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3914 "Save relative positions of markers in region.
3915 This check for agenda markers in all agenda buffers currently active."
3916 (dolist (buf (buffer-list))
3917 (with-current-buffer buf
3918 (when (eq major-mode 'org-agenda-mode)
3919 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3920 org-agenda-markers)))))
3922 ;;; Entry text mode
3924 (defun org-agenda-entry-text-show-here ()
3925 "Add some text from the entry as context to the current line."
3926 (let (m txt o)
3927 (setq m (org-get-at-bol 'org-hd-marker))
3928 (unless (marker-buffer m)
3929 (error "No marker points to an entry here"))
3930 (setq txt (concat "\n" (org-no-properties
3931 (org-agenda-get-some-entry-text
3932 m org-agenda-entry-text-maxlines
3933 org-agenda-entry-text-leaders))))
3934 (when (string-match "\\S-" txt)
3935 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3936 (overlay-put o 'evaporate t)
3937 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3938 (overlay-put o 'after-string txt))))
3940 (defun org-agenda-entry-text-show ()
3941 "Add entry context for all agenda lines."
3942 (interactive)
3943 (save-excursion
3944 (goto-char (point-max))
3945 (beginning-of-line 1)
3946 (while (not (bobp))
3947 (when (org-get-at-bol 'org-hd-marker)
3948 (org-agenda-entry-text-show-here))
3949 (beginning-of-line 0))))
3951 (defun org-agenda-entry-text-hide ()
3952 "Remove any shown entry context."
3953 (delq nil
3954 (mapcar (lambda (o)
3955 (if (eq (overlay-get o 'org-overlay-type)
3956 'agenda-entry-content)
3957 (progn (delete-overlay o) t)))
3958 (overlays-in (point-min) (point-max)))))
3960 (defun org-agenda-get-day-face (date)
3961 "Return the face DATE should be displayed with."
3962 (or (and (functionp org-agenda-day-face-function)
3963 (funcall org-agenda-day-face-function date))
3964 (cond ((org-agenda-todayp date)
3965 'org-agenda-date-today)
3966 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3967 'org-agenda-date-weekend)
3968 (t 'org-agenda-date))))
3970 ;;; Agenda timeline
3972 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3973 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
3975 (defun org-timeline (&optional dotodo)
3976 "Show a time-sorted view of the entries in the current org file.
3977 Only entries with a time stamp of today or later will be listed. With
3978 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3979 under the current date.
3980 If the buffer contains an active region, only check the region for
3981 dates."
3982 (interactive "P")
3983 (let* ((dopast t)
3984 (org-agenda-show-log-scoped org-agenda-show-log)
3985 (org-agenda-show-log org-agenda-show-log-scoped)
3986 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3987 (current-buffer))))
3988 (date (calendar-current-date))
3989 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3990 (end (if (org-region-active-p) (region-end) (point-max)))
3991 (day-numbers (org-get-all-dates
3992 beg end 'no-ranges
3993 t org-agenda-show-log-scoped ; always include today
3994 org-timeline-show-empty-dates))
3995 (org-deadline-warning-days 0)
3996 (org-agenda-only-exact-dates t)
3997 (today (org-today))
3998 (past t)
3999 args
4000 s e rtn d emptyp)
4001 (setq org-agenda-redo-command
4002 (list 'let
4003 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4004 (list 'org-switch-to-buffer-other-window (current-buffer))
4005 (list 'org-timeline (list 'quote dotodo))))
4006 (put 'org-agenda-redo-command 'org-lprops nil)
4007 (if (not dopast)
4008 ;; Remove past dates from the list of dates.
4009 (setq day-numbers (delq nil (mapcar (lambda(x)
4010 (if (>= x today) x nil))
4011 day-numbers))))
4012 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4013 (org-compile-prefix-format 'timeline)
4014 (org-set-sorting-strategy 'timeline)
4015 (if org-agenda-show-log-scoped (push :closed args))
4016 (push :timestamp args)
4017 (push :deadline args)
4018 (push :scheduled args)
4019 (push :sexp args)
4020 (if dotodo (push :todo args))
4021 (insert "Timeline of file " entry "\n")
4022 (add-text-properties (point-min) (point)
4023 (list 'face 'org-agenda-structure))
4024 (org-agenda-mark-header-line (point-min))
4025 (while (setq d (pop day-numbers))
4026 (if (and (listp d) (eq (car d) :omitted))
4027 (progn
4028 (setq s (point))
4029 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4030 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4031 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4032 (if (and (>= d today)
4033 dopast
4034 past)
4035 (progn
4036 (setq past nil)
4037 (insert (make-string 79 ?-) "\n")))
4038 (setq date (calendar-gregorian-from-absolute d))
4039 (setq s (point))
4040 (setq rtn (and (not emptyp)
4041 (apply 'org-agenda-get-day-entries entry
4042 date args)))
4043 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4044 (progn
4045 (insert
4046 (if (stringp org-agenda-format-date)
4047 (format-time-string org-agenda-format-date
4048 (org-time-from-absolute date))
4049 (funcall org-agenda-format-date date))
4050 "\n")
4051 (put-text-property s (1- (point)) 'face
4052 (org-agenda-get-day-face date))
4053 (put-text-property s (1- (point)) 'org-date-line t)
4054 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4055 (if (equal d today)
4056 (put-text-property s (1- (point)) 'org-today t))
4057 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4058 (put-text-property s (1- (point)) 'day d)))))
4059 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4060 (point-min)))
4061 (add-text-properties
4062 (point-min) (point-max)
4063 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4064 (org-agenda-finalize)
4065 (setq buffer-read-only t)))
4067 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4068 "Return a list of all relevant day numbers from BEG to END buffer positions.
4069 If NO-RANGES is non-nil, include only the start and end dates of a range,
4070 not every single day in the range. If FORCE-TODAY is non-nil, make
4071 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4072 inactive time stamps (those in square brackets) are included.
4073 When EMPTY is non-nil, also include days without any entries."
4074 (let ((re (concat
4075 (if pre-re pre-re "")
4076 (if inactive org-ts-regexp-both org-ts-regexp)))
4077 dates dates1 date day day1 day2 ts1 ts2 pos)
4078 (if force-today
4079 (setq dates (list (org-today))))
4080 (save-excursion
4081 (goto-char beg)
4082 (while (re-search-forward re end t)
4083 (setq day (time-to-days (org-time-string-to-time
4084 (substring (match-string 1) 0 10)
4085 (current-buffer) (match-beginning 0))))
4086 (or (memq day dates) (push day dates)))
4087 (unless no-ranges
4088 (goto-char beg)
4089 (while (re-search-forward org-tr-regexp end t)
4090 (setq pos (match-beginning 0))
4091 (setq ts1 (substring (match-string 1) 0 10)
4092 ts2 (substring (match-string 2) 0 10)
4093 day1 (time-to-days (org-time-string-to-time
4094 ts1 (current-buffer) pos))
4095 day2 (time-to-days (org-time-string-to-time
4096 ts2 (current-buffer) pos)))
4097 (while (< (setq day1 (1+ day1)) day2)
4098 (or (memq day1 dates) (push day1 dates)))))
4099 (setq dates (sort dates '<))
4100 (when empty
4101 (while (setq day (pop dates))
4102 (setq day2 (car dates))
4103 (push day dates1)
4104 (when (and day2 empty)
4105 (if (or (eq empty t)
4106 (and (numberp empty) (<= (- day2 day) empty)))
4107 (while (< (setq day (1+ day)) day2)
4108 (push (list day) dates1))
4109 (push (cons :omitted (- day2 day)) dates1))))
4110 (setq dates (nreverse dates1)))
4111 dates)))
4113 ;;; Agenda Daily/Weekly
4115 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4116 "Start day for the agenda view.
4117 Custom commands can set this variable in the options section.
4118 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4119 input allowed when reading a date through the Org calendar.
4120 See the docstring of `org-read-date' for details.")
4121 (defvar org-starting-day nil) ; local variable in the agenda buffer
4122 (defvar org-arg-loc nil) ; local variable
4124 (defvar org-agenda-buffer-tmp-name nil)
4125 ;;;###autoload
4126 (defun org-agenda-list (&optional arg start-day span with-hour)
4127 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4128 The view will be for the current day or week, but from the overview buffer
4129 you will be able to go to other days/weeks.
4131 With a numeric prefix argument in an interactive call, the agenda will
4132 span ARG days. Lisp programs should instead specify SPAN to change
4133 the number of days. SPAN defaults to `org-agenda-span'.
4135 START-DAY defaults to TODAY, or to the most recent match for the weekday
4136 given in `org-agenda-start-on-weekday'.
4138 When WITH-HOUR is non-nil, only include scheduled and deadline
4139 items if they have an hour specification like [h]h:mm."
4140 (interactive "P")
4141 (if org-agenda-overriding-arguments
4142 (setq arg (car org-agenda-overriding-arguments)
4143 start-day (nth 1 org-agenda-overriding-arguments)
4144 span (nth 2 org-agenda-overriding-arguments)))
4145 (if (and (integerp arg) (> arg 0))
4146 (setq span arg arg nil))
4147 (catch 'exit
4148 (setq org-agenda-buffer-name
4149 (or org-agenda-buffer-tmp-name
4150 (if org-agenda-sticky
4151 (cond ((and org-keys (stringp org-match))
4152 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4153 (org-keys
4154 (format "*Org Agenda(%s)*" org-keys))
4155 (t "*Org Agenda(a)*")))
4156 org-agenda-buffer-name))
4157 (org-agenda-prepare "Day/Week")
4158 (setq start-day (or start-day org-agenda-start-day))
4159 (if (stringp start-day)
4160 ;; Convert to an absolute day number
4161 (setq start-day (time-to-days (org-read-date nil t start-day))))
4162 (org-compile-prefix-format 'agenda)
4163 (org-set-sorting-strategy 'agenda)
4164 (let* ((span (org-agenda-ndays-to-span
4165 (or span org-agenda-ndays org-agenda-span)))
4166 (today (org-today))
4167 (sd (or start-day today))
4168 (ndays (org-agenda-span-to-ndays span sd))
4169 (org-agenda-start-on-weekday
4170 (if (or (eq ndays 7) (eq ndays 14))
4171 org-agenda-start-on-weekday))
4172 (thefiles (org-agenda-files nil 'ifmode))
4173 (files thefiles)
4174 (start (if (or (null org-agenda-start-on-weekday)
4175 (< ndays 7))
4177 (let* ((nt (calendar-day-of-week
4178 (calendar-gregorian-from-absolute sd)))
4179 (n1 org-agenda-start-on-weekday)
4180 (d (- nt n1)))
4181 (- sd (+ (if (< d 0) 7 0) d)))))
4182 (day-numbers (list start))
4183 (day-cnt 0)
4184 (inhibit-redisplay (not debug-on-error))
4185 (org-agenda-show-log-scoped org-agenda-show-log)
4186 s e rtn rtnall file date d start-pos end-pos todayp
4187 clocktable-start clocktable-end filter)
4188 (setq org-agenda-redo-command
4189 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4190 (dotimes (n (1- ndays))
4191 (push (1+ (car day-numbers)) day-numbers))
4192 (setq day-numbers (nreverse day-numbers))
4193 (setq clocktable-start (car day-numbers)
4194 clocktable-end (1+ (or (org-last day-numbers) 0)))
4195 (org-set-local 'org-starting-day (car day-numbers))
4196 (org-set-local 'org-arg-loc arg)
4197 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4198 (unless org-agenda-compact-blocks
4199 (let* ((d1 (car day-numbers))
4200 (d2 (org-last day-numbers))
4201 (w1 (org-days-to-iso-week d1))
4202 (w2 (org-days-to-iso-week d2)))
4203 (setq s (point))
4204 (if org-agenda-overriding-header
4205 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4206 nil 'face 'org-agenda-structure) "\n")
4207 (insert (org-agenda-span-name span)
4208 "-agenda"
4209 (if (< (- d2 d1) 350)
4210 (if (= w1 w2)
4211 (format " (W%02d)" w1)
4212 (format " (W%02d-W%02d)" w1 w2))
4214 ":\n")))
4215 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4216 'org-date-line t))
4217 (org-agenda-mark-header-line s))
4218 (while (setq d (pop day-numbers))
4219 (setq date (calendar-gregorian-from-absolute d)
4220 s (point))
4221 (if (or (setq todayp (= d today))
4222 (and (not start-pos) (= d sd)))
4223 (setq start-pos (point))
4224 (if (and start-pos (not end-pos))
4225 (setq end-pos (point))))
4226 (setq files thefiles
4227 rtnall nil)
4228 (while (setq file (pop files))
4229 (catch 'nextfile
4230 (org-check-agenda-file file)
4231 (let ((org-agenda-entry-types org-agenda-entry-types))
4232 ;; Starred types override non-starred equivalents
4233 (when (member :deadline* org-agenda-entry-types)
4234 (setq org-agenda-entry-types
4235 (delq :deadline org-agenda-entry-types)))
4236 (when (member :scheduled* org-agenda-entry-types)
4237 (setq org-agenda-entry-types
4238 (delq :scheduled org-agenda-entry-types)))
4239 ;; Honor with-hour
4240 (when with-hour
4241 (when (member :deadline org-agenda-entry-types)
4242 (setq org-agenda-entry-types
4243 (delq :deadline org-agenda-entry-types))
4244 (push :deadline* org-agenda-entry-types))
4245 (when (member :scheduled org-agenda-entry-types)
4246 (setq org-agenda-entry-types
4247 (delq :scheduled org-agenda-entry-types))
4248 (push :scheduled* org-agenda-entry-types)))
4249 (unless org-agenda-include-deadlines
4250 (setq org-agenda-entry-types
4251 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4252 (cond
4253 ((memq org-agenda-show-log-scoped '(only clockcheck))
4254 (setq rtn (org-agenda-get-day-entries
4255 file date :closed)))
4256 (org-agenda-show-log-scoped
4257 (setq rtn (apply 'org-agenda-get-day-entries
4258 file date
4259 (append '(:closed) org-agenda-entry-types))))
4261 (setq rtn (apply 'org-agenda-get-day-entries
4262 file date
4263 org-agenda-entry-types)))))
4264 (setq rtnall (append rtnall rtn)))) ;; all entries
4265 (if org-agenda-include-diary
4266 (let ((org-agenda-search-headline-for-time t))
4267 (require 'diary-lib)
4268 (setq rtn (org-get-entries-from-diary date))
4269 (setq rtnall (append rtnall rtn))))
4270 (if (or rtnall org-agenda-show-all-dates)
4271 (progn
4272 (setq day-cnt (1+ day-cnt))
4273 (insert
4274 (if (stringp org-agenda-format-date)
4275 (format-time-string org-agenda-format-date
4276 (org-time-from-absolute date))
4277 (funcall org-agenda-format-date date))
4278 "\n")
4279 (put-text-property s (1- (point)) 'face
4280 (org-agenda-get-day-face date))
4281 (put-text-property s (1- (point)) 'org-date-line t)
4282 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4283 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4284 (when todayp
4285 (put-text-property s (1- (point)) 'org-today t))
4286 (setq rtnall
4287 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4288 (if rtnall (insert ;; all entries
4289 (org-agenda-finalize-entries rtnall 'agenda)
4290 "\n"))
4291 (put-text-property s (1- (point)) 'day d)
4292 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4293 (when (and org-agenda-clockreport-mode clocktable-start)
4294 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4295 ;; the above line is to ensure the restricted range!
4296 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4297 tbl)
4298 (setq p (org-plist-delete p :block))
4299 (setq p (plist-put p :tstart clocktable-start))
4300 (setq p (plist-put p :tend clocktable-end))
4301 (setq p (plist-put p :scope 'agenda))
4302 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4303 (setq filter (or org-agenda-tag-filter-while-redo
4304 (get 'org-agenda-tag-filter :preset-filter))))
4305 (setq p (plist-put p :tags (mapconcat (lambda (x)
4306 (if (string-match "[<>=]" x)
4309 filter ""))))
4310 (setq tbl (apply 'org-clock-get-clocktable p))
4311 (insert tbl)))
4312 (goto-char (point-min))
4313 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4314 (unless (and (pos-visible-in-window-p (point-min))
4315 (pos-visible-in-window-p (point-max)))
4316 (goto-char (1- (point-max)))
4317 (recenter -1)
4318 (if (not (pos-visible-in-window-p (or start-pos 1)))
4319 (progn
4320 (goto-char (or start-pos 1))
4321 (recenter 1))))
4322 (goto-char (or start-pos 1))
4323 (add-text-properties (point-min) (point-max)
4324 `(org-agenda-type agenda
4325 org-last-args (,arg ,start-day ,span)
4326 org-redo-cmd ,org-agenda-redo-command
4327 org-series-cmd ,org-cmd))
4328 (if (eq org-agenda-show-log-scoped 'clockcheck)
4329 (org-agenda-show-clocking-issues))
4330 (org-agenda-finalize)
4331 (setq buffer-read-only t)
4332 (message ""))))
4334 (defun org-agenda-ndays-to-span (n)
4335 "Return a span symbol for a span of N days, or N if none matches."
4336 (cond ((symbolp n) n)
4337 ((= n 1) 'day)
4338 ((= n 7) 'week)
4339 ((= n 14) 'fortnight)
4340 (t n)))
4342 (defun org-agenda-span-to-ndays (span &optional start-day)
4343 "Return ndays from SPAN, possibly starting at START-DAY.
4344 START-DAY is an absolute time value."
4345 (cond ((numberp span) span)
4346 ((eq span 'day) 1)
4347 ((eq span 'week) 7)
4348 ((eq span 'fortnight) 14)
4349 ((eq span 'month)
4350 (let ((date (calendar-gregorian-from-absolute start-day)))
4351 (calendar-last-day-of-month (car date) (caddr date))))
4352 ((eq span 'year)
4353 (let ((date (calendar-gregorian-from-absolute start-day)))
4354 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4356 (defun org-agenda-span-name (span)
4357 "Return a SPAN name."
4358 (if (null span)
4360 (if (symbolp span)
4361 (capitalize (symbol-name span))
4362 (format "%d days" span))))
4364 ;;; Agenda word search
4366 (defvar org-agenda-search-history nil)
4368 (defvar org-search-syntax-table nil
4369 "Special syntax table for org-mode search.
4370 In this table, we have single quotes not as word constituents, to
4371 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4373 (defvar org-mode-syntax-table) ; From org.el
4374 (defun org-search-syntax-table ()
4375 (unless org-search-syntax-table
4376 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4377 (modify-syntax-entry ?' "." org-search-syntax-table)
4378 (modify-syntax-entry ?` "." org-search-syntax-table))
4379 org-search-syntax-table)
4381 (defvar org-agenda-last-search-view-search-was-boolean nil)
4383 ;;;###autoload
4384 (defun org-search-view (&optional todo-only string edit-at)
4385 "Show all entries that contain a phrase or words or regular expressions.
4387 With optional prefix argument TODO-ONLY, only consider entries that are
4388 TODO entries. The argument STRING can be used to pass a default search
4389 string into this function. If EDIT-AT is non-nil, it means that the
4390 user should get a chance to edit this string, with cursor at position
4391 EDIT-AT.
4393 The search string can be viewed either as a phrase that should be found as
4394 is, or it can be broken into a number of snippets, each of which must match
4395 in a Boolean way to select an entry. The default depends on the variable
4396 `org-agenda-search-view-always-boolean'.
4397 Even if this is turned off (the default) you can always switch to
4398 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4400 The default is a direct search of the whole phrase, where each space in
4401 the search string can expand to an arbitrary amount of whitespace,
4402 including newlines.
4404 If using a Boolean search, the search string is split on whitespace and
4405 each snippet is searched separately, with logical AND to select an entry.
4406 Words prefixed with a minus must *not* occur in the entry. Words without
4407 a prefix or prefixed with a plus must occur in the entry. Matching is
4408 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4409 match whole words, not parts of a word) if
4410 `org-agenda-search-view-force-full-words' is set (default is nil).
4412 Boolean search snippets enclosed by curly braces are interpreted as
4413 regular expressions that must or (when preceded with \"-\") must not
4414 match in the entry. Snippets enclosed into double quotes will be taken
4415 as a whole, to include whitespace.
4417 - If the search string starts with an asterisk, search only in headlines.
4418 - If (possibly after the leading star) the search string starts with an
4419 exclamation mark, this also means to look at TODO entries only, an effect
4420 that can also be achieved with a prefix argument.
4421 - If (possibly after star and exclamation mark) the search string starts
4422 with a colon, this will mean that the (non-regexp) snippets of the
4423 Boolean search must match as full words.
4425 This command searches the agenda files, and in addition the files listed
4426 in `org-agenda-text-search-extra-files'."
4427 (interactive "P")
4428 (if org-agenda-overriding-arguments
4429 (setq todo-only (car org-agenda-overriding-arguments)
4430 string (nth 1 org-agenda-overriding-arguments)
4431 edit-at (nth 2 org-agenda-overriding-arguments)))
4432 (let* ((props (list 'face nil
4433 'done-face 'org-agenda-done
4434 'org-not-done-regexp org-not-done-regexp
4435 'org-todo-regexp org-todo-regexp
4436 'org-complex-heading-regexp org-complex-heading-regexp
4437 'mouse-face 'highlight
4438 'help-echo (format "mouse-2 or RET jump to location")))
4439 (full-words org-agenda-search-view-force-full-words)
4440 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4441 regexp rtn rtnall files file pos inherited-tags
4442 marker category category-pos level tags c neg re boolean
4443 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4444 (unless (and (not edit-at)
4445 (stringp string)
4446 (string-match "\\S-" string))
4447 (setq string (read-string
4448 (if org-agenda-search-view-always-boolean
4449 "[+-]Word/{Regexp} ...: "
4450 "Phrase or [+-]Word/{Regexp} ...: ")
4451 (cond
4452 ((integerp edit-at) (cons string edit-at))
4453 (edit-at string))
4454 'org-agenda-search-history)))
4455 (catch 'exit
4456 (if org-agenda-sticky
4457 (setq org-agenda-buffer-name
4458 (if (stringp string)
4459 (format "*Org Agenda(%s:%s)*"
4460 (or org-keys (or (and todo-only "S") "s")) string)
4461 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4462 (org-agenda-prepare "SEARCH")
4463 (org-compile-prefix-format 'search)
4464 (org-set-sorting-strategy 'search)
4465 (setq org-agenda-redo-command
4466 (list 'org-search-view (if todo-only t nil)
4467 (list 'if 'current-prefix-arg nil string)))
4468 (setq org-agenda-query-string string)
4469 (if (equal (string-to-char string) ?*)
4470 (setq hdl-only t
4471 words (substring string 1))
4472 (setq words string))
4473 (when (equal (string-to-char words) ?!)
4474 (setq todo-only t
4475 words (substring words 1)))
4476 (when (equal (string-to-char words) ?:)
4477 (setq full-words t
4478 words (substring words 1)))
4479 (if (or org-agenda-search-view-always-boolean
4480 (member (string-to-char words) '(?- ?+ ?\{)))
4481 (setq boolean t))
4482 (setq words (org-split-string words))
4483 (let (www w)
4484 (while (setq w (pop words))
4485 (while (and (string-match "\\\\\\'" w) words)
4486 (setq w (concat (substring w 0 -1) " " (pop words))))
4487 (push w www))
4488 (setq words (nreverse www) www nil)
4489 (while (setq w (pop words))
4490 (when (and (string-match "\\`[-+]?{" w)
4491 (not (string-match "}\\'" w)))
4492 (while (and words (not (string-match "}\\'" (car words))))
4493 (setq w (concat w " " (pop words))))
4494 (setq w (concat w " " (pop words))))
4495 (push w www))
4496 (setq words (nreverse www)))
4497 (setq org-agenda-last-search-view-search-was-boolean boolean)
4498 (when boolean
4499 (let (wds w)
4500 (while (setq w (pop words))
4501 (if (or (equal (substring w 0 1) "\"")
4502 (and (> (length w) 1)
4503 (member (substring w 0 1) '("+" "-"))
4504 (equal (substring w 1 2) "\"")))
4505 (while (and words (not (equal (substring w -1) "\"")))
4506 (setq w (concat w " " (pop words)))))
4507 (and (string-match "\\`\\([-+]?\\)\"" w)
4508 (setq w (replace-match "\\1" nil nil w)))
4509 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4510 (push w wds))
4511 (setq words (nreverse wds))))
4512 (if boolean
4513 (mapc (lambda (w)
4514 (setq c (string-to-char w))
4515 (if (equal c ?-)
4516 (setq neg t w (substring w 1))
4517 (if (equal c ?+)
4518 (setq neg nil w (substring w 1))
4519 (setq neg nil)))
4520 (if (string-match "\\`{.*}\\'" w)
4521 (setq re (substring w 1 -1))
4522 (if full-words
4523 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4524 (setq re (regexp-quote (downcase w)))))
4525 (if neg (push re regexps-) (push re regexps+)))
4526 words)
4527 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4528 regexps+))
4529 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4530 (if (not regexps+)
4531 (setq regexp org-outline-regexp-bol)
4532 (setq regexp (pop regexps+))
4533 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4534 regexp))))
4535 (setq files (org-agenda-files nil 'ifmode))
4536 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4537 (pop org-agenda-text-search-extra-files)
4538 (setq files (org-add-archive-files files)))
4539 (setq files (append files org-agenda-text-search-extra-files)
4540 rtnall nil)
4541 (while (setq file (pop files))
4542 (setq ee nil)
4543 (catch 'nextfile
4544 (org-check-agenda-file file)
4545 (setq buffer (if (file-exists-p file)
4546 (org-get-agenda-file-buffer file)
4547 (error "No such file %s" file)))
4548 (if (not buffer)
4549 ;; If file does not exist, make sure an error message is sent
4550 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4551 file))))
4552 (with-current-buffer buffer
4553 (with-syntax-table (org-search-syntax-table)
4554 (unless (derived-mode-p 'org-mode)
4555 (error "Agenda file %s is not in `org-mode'" file))
4556 (let ((case-fold-search t))
4557 (save-excursion
4558 (save-restriction
4559 (if (eq buffer org-agenda-restrict)
4560 (narrow-to-region org-agenda-restrict-begin
4561 org-agenda-restrict-end)
4562 (widen))
4563 (goto-char (point-min))
4564 (unless (or (org-at-heading-p)
4565 (outline-next-heading))
4566 (throw 'nextfile t))
4567 (goto-char (max (point-min) (1- (point))))
4568 (while (re-search-forward regexp nil t)
4569 (org-back-to-heading t)
4570 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4571 (> (org-reduced-level (org-outline-level))
4572 org-agenda-search-view-max-outline-level)
4573 (forward-line -1)
4574 (outline-back-to-heading t)))
4575 (skip-chars-forward "* ")
4576 (setq beg (point-at-bol)
4577 beg1 (point)
4578 end (progn
4579 (outline-next-heading)
4580 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4581 (> (org-reduced-level (org-outline-level))
4582 org-agenda-search-view-max-outline-level)
4583 (forward-line 1)
4584 (outline-next-heading)))
4585 (point)))
4587 (catch :skip
4588 (goto-char beg)
4589 (org-agenda-skip)
4590 (setq str (buffer-substring-no-properties
4591 (point-at-bol)
4592 (if hdl-only (point-at-eol) end)))
4593 (mapc (lambda (wr) (when (string-match wr str)
4594 (goto-char (1- end))
4595 (throw :skip t)))
4596 regexps-)
4597 (mapc (lambda (wr) (unless (string-match wr str)
4598 (goto-char (1- end))
4599 (throw :skip t)))
4600 (if todo-only
4601 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4602 regexps+)
4603 regexps+))
4604 (goto-char beg)
4605 (setq marker (org-agenda-new-marker (point))
4606 category (org-get-category)
4607 level (make-string (org-reduced-level (org-outline-level)) ? )
4608 category-pos (get-text-property (point) 'org-category-position)
4609 inherited-tags
4610 (or (eq org-agenda-show-inherited-tags 'always)
4611 (and (listp org-agenda-show-inherited-tags)
4612 (memq 'todo org-agenda-show-inherited-tags))
4613 (and (eq org-agenda-show-inherited-tags t)
4614 (or (eq org-agenda-use-tag-inheritance t)
4615 (memq 'todo org-agenda-use-tag-inheritance))))
4616 tags (org-get-tags-at nil (not inherited-tags))
4617 txt (org-agenda-format-item
4619 (buffer-substring-no-properties
4620 beg1 (point-at-eol))
4621 level category tags t))
4622 (org-add-props txt props
4623 'org-marker marker 'org-hd-marker marker
4624 'org-todo-regexp org-todo-regexp
4625 'level level
4626 'org-complex-heading-regexp org-complex-heading-regexp
4627 'priority 1000 'org-category category
4628 'org-category-position category-pos
4629 'type "search")
4630 (push txt ee)
4631 (goto-char (1- end))))))))))
4632 (setq rtn (nreverse ee))
4633 (setq rtnall (append rtnall rtn)))
4634 (if org-agenda-overriding-header
4635 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4636 nil 'face 'org-agenda-structure) "\n")
4637 (insert "Search words: ")
4638 (add-text-properties (point-min) (1- (point))
4639 (list 'face 'org-agenda-structure))
4640 (setq pos (point))
4641 (insert string "\n")
4642 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4643 (setq pos (point))
4644 (unless org-agenda-multi
4645 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4646 (add-text-properties pos (1- (point))
4647 (list 'face 'org-agenda-structure))))
4648 (org-agenda-mark-header-line (point-min))
4649 (when rtnall
4650 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4651 (goto-char (point-min))
4652 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4653 (add-text-properties (point-min) (point-max)
4654 `(org-agenda-type search
4655 org-last-args (,todo-only ,string ,edit-at)
4656 org-redo-cmd ,org-agenda-redo-command
4657 org-series-cmd ,org-cmd))
4658 (org-agenda-finalize)
4659 (setq buffer-read-only t))))
4661 ;;; Agenda TODO list
4663 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4664 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4665 (concat
4666 (if (or (equal keywords "ALL") (not keywords))
4667 (propertize "ALL" 'face 'warning)
4668 (mapconcat
4669 (lambda (kw)
4670 (propertize kw 'face (org-get-todo-face kw)))
4671 (org-split-string keywords "|")
4672 "|"))
4673 "\n"))
4675 (defvar org-select-this-todo-keyword nil)
4676 (defvar org-last-arg nil)
4678 ;;;###autoload
4679 (defun org-todo-list (&optional arg)
4680 "Show all (not done) TODO entries from all agenda file in a single list.
4681 The prefix arg can be used to select a specific TODO keyword and limit
4682 the list to these. When using \\[universal-argument], you will be prompted
4683 for a keyword. A numeric prefix directly selects the Nth keyword in
4684 `org-todo-keywords-1'."
4685 (interactive "P")
4686 (if org-agenda-overriding-arguments
4687 (setq arg org-agenda-overriding-arguments))
4688 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4689 (let* ((today (org-today))
4690 (date (calendar-gregorian-from-absolute today))
4691 (kwds org-todo-keywords-for-agenda)
4692 (completion-ignore-case t)
4693 (org-select-this-todo-keyword
4694 (if (stringp arg) arg
4695 (and arg (integerp arg) (> arg 0)
4696 (nth (1- arg) kwds))))
4697 rtn rtnall files file pos)
4698 (when (equal arg '(4))
4699 (setq org-select-this-todo-keyword
4700 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4701 (mapcar 'list kwds) nil nil)))
4702 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4703 (catch 'exit
4704 (if org-agenda-sticky
4705 (setq org-agenda-buffer-name
4706 (if (stringp org-select-this-todo-keyword)
4707 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4708 org-select-this-todo-keyword)
4709 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4710 (org-agenda-prepare "TODO")
4711 (org-compile-prefix-format 'todo)
4712 (org-set-sorting-strategy 'todo)
4713 (setq org-agenda-redo-command
4714 `(org-todo-list (or (and (numberp current-prefix-arg)
4715 current-prefix-arg)
4716 ,org-select-this-todo-keyword
4717 current-prefix-arg ,arg)))
4718 (setq files (org-agenda-files nil 'ifmode)
4719 rtnall nil)
4720 (while (setq file (pop files))
4721 (catch 'nextfile
4722 (org-check-agenda-file file)
4723 (setq rtn (org-agenda-get-day-entries file date :todo))
4724 (setq rtnall (append rtnall rtn))))
4725 (if org-agenda-overriding-header
4726 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4727 nil 'face 'org-agenda-structure) "\n")
4728 (insert "Global list of TODO items of type: ")
4729 (add-text-properties (point-min) (1- (point))
4730 (list 'face 'org-agenda-structure
4731 'short-heading
4732 (concat "ToDo: "
4733 (or org-select-this-todo-keyword "ALL"))))
4734 (org-agenda-mark-header-line (point-min))
4735 (insert (org-agenda-propertize-selected-todo-keywords
4736 org-select-this-todo-keyword))
4737 (setq pos (point))
4738 (unless org-agenda-multi
4739 (insert "Available with `N r': (0)[ALL]")
4740 (let ((n 0) s)
4741 (mapc (lambda (x)
4742 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4743 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4744 (insert "\n "))
4745 (insert " " s))
4746 kwds))
4747 (insert "\n"))
4748 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4749 (org-agenda-mark-header-line (point-min))
4750 (when rtnall
4751 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4752 (goto-char (point-min))
4753 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4754 (add-text-properties (point-min) (point-max)
4755 `(org-agenda-type todo
4756 org-last-args ,arg
4757 org-redo-cmd ,org-agenda-redo-command
4758 org-series-cmd ,org-cmd))
4759 (org-agenda-finalize)
4760 (setq buffer-read-only t))))
4762 ;;; Agenda tags match
4764 ;;;###autoload
4765 (defun org-tags-view (&optional todo-only match)
4766 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4767 The prefix arg TODO-ONLY limits the search to TODO entries."
4768 (interactive "P")
4769 (if org-agenda-overriding-arguments
4770 (setq todo-only (car org-agenda-overriding-arguments)
4771 match (nth 1 org-agenda-overriding-arguments)))
4772 (let* ((org-tags-match-list-sublevels
4773 org-tags-match-list-sublevels)
4774 (completion-ignore-case t)
4775 rtn rtnall files file pos matcher
4776 buffer)
4777 (when (and (stringp match) (not (string-match "\\S-" match)))
4778 (setq match nil))
4779 (catch 'exit
4780 (if org-agenda-sticky
4781 (setq org-agenda-buffer-name
4782 (if (stringp match)
4783 (format "*Org Agenda(%s:%s)*"
4784 (or org-keys (or (and todo-only "M") "m")) match)
4785 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4786 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4787 ;; expanding tags within `org-make-tags-matcher'
4788 (org-agenda-prepare (concat "TAGS " match))
4789 (setq matcher (org-make-tags-matcher match)
4790 match (car matcher) matcher (cdr matcher))
4791 (org-compile-prefix-format 'tags)
4792 (org-set-sorting-strategy 'tags)
4793 (setq org-agenda-query-string match)
4794 (setq org-agenda-redo-command
4795 (list 'org-tags-view `(quote ,todo-only)
4796 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4797 (setq files (org-agenda-files nil 'ifmode)
4798 rtnall nil)
4799 (while (setq file (pop files))
4800 (catch 'nextfile
4801 (org-check-agenda-file file)
4802 (setq buffer (if (file-exists-p file)
4803 (org-get-agenda-file-buffer file)
4804 (error "No such file %s" file)))
4805 (if (not buffer)
4806 ;; If file does not exist, error message to agenda
4807 (setq rtn (list
4808 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4809 rtnall (append rtnall rtn))
4810 (with-current-buffer buffer
4811 (unless (derived-mode-p 'org-mode)
4812 (error "Agenda file %s is not in `org-mode'" file))
4813 (save-excursion
4814 (save-restriction
4815 (if (eq buffer org-agenda-restrict)
4816 (narrow-to-region org-agenda-restrict-begin
4817 org-agenda-restrict-end)
4818 (widen))
4819 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4820 (setq rtnall (append rtnall rtn))))))))
4821 (if org-agenda-overriding-header
4822 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4823 nil 'face 'org-agenda-structure) "\n")
4824 (insert "Headlines with TAGS match: ")
4825 (add-text-properties (point-min) (1- (point))
4826 (list 'face 'org-agenda-structure
4827 'short-heading
4828 (concat "Match: " match)))
4829 (setq pos (point))
4830 (insert match "\n")
4831 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4832 (setq pos (point))
4833 (unless org-agenda-multi
4834 (insert "Press `C-u r' to search again with new search string\n"))
4835 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4836 (org-agenda-mark-header-line (point-min))
4837 (when rtnall
4838 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4839 (goto-char (point-min))
4840 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4841 (add-text-properties (point-min) (point-max)
4842 `(org-agenda-type tags
4843 org-last-args (,todo-only ,match)
4844 org-redo-cmd ,org-agenda-redo-command
4845 org-series-cmd ,org-cmd))
4846 (org-agenda-finalize)
4847 (setq buffer-read-only t))))
4849 ;;; Agenda Finding stuck projects
4851 (defvar org-agenda-skip-regexp nil
4852 "Regular expression used in skipping subtrees for the agenda.
4853 This is basically a temporary global variable that can be set and then
4854 used by user-defined selections using `org-agenda-skip-function'.")
4856 (defvar org-agenda-overriding-header nil
4857 "When set during agenda, todo and tags searches it replaces the header.
4858 This variable should not be set directly, but custom commands can bind it
4859 in the options section.")
4861 (defun org-agenda-skip-entry-when-regexp-matches ()
4862 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4863 If yes, it returns the end position of this entry, causing agenda commands
4864 to skip the entry but continuing the search in the subtree. This is a
4865 function that can be put into `org-agenda-skip-function' for the duration
4866 of a command."
4867 (let ((end (save-excursion (org-end-of-subtree t)))
4868 skip)
4869 (save-excursion
4870 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4871 (and skip end)))
4873 (defun org-agenda-skip-subtree-when-regexp-matches ()
4874 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4875 If yes, it returns the end position of this tree, causing agenda commands
4876 to skip this subtree. This is a function that can be put into
4877 `org-agenda-skip-function' for the duration of a command."
4878 (let ((end (save-excursion (org-end-of-subtree t)))
4879 skip)
4880 (save-excursion
4881 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4882 (and skip end)))
4884 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4885 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4886 If yes, it returns the end position of the current entry (NOT the tree),
4887 causing agenda commands to skip the entry but continuing the search in
4888 the subtree. This is a function that can be put into
4889 `org-agenda-skip-function' for the duration of a command. An important
4890 use of this function is for the stuck project list."
4891 (let ((end (save-excursion (org-end-of-subtree t)))
4892 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4893 skip)
4894 (save-excursion
4895 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4896 (and skip entry-end)))
4898 (defun org-agenda-skip-entry-if (&rest conditions)
4899 "Skip entry if any of CONDITIONS is true.
4900 See `org-agenda-skip-if' for details."
4901 (org-agenda-skip-if nil conditions))
4903 (defun org-agenda-skip-subtree-if (&rest conditions)
4904 "Skip subtree if any of CONDITIONS is true.
4905 See `org-agenda-skip-if' for details."
4906 (org-agenda-skip-if t conditions))
4908 (defun org-agenda-skip-if (subtree conditions)
4909 "Checks current entity for CONDITIONS.
4910 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4911 the entry (i.e. the text before the next heading) is checked.
4913 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4914 from different tests. Valid conditions are:
4916 scheduled Check if there is a scheduled cookie
4917 notscheduled Check if there is no scheduled cookie
4918 deadline Check if there is a deadline
4919 notdeadline Check if there is no deadline
4920 timestamp Check if there is a timestamp (also deadline or scheduled)
4921 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4922 regexp Check if regexp matches
4923 notregexp Check if regexp does not match.
4924 todo Check if TODO keyword matches
4925 nottodo Check if TODO keyword does not match
4927 The regexp is taken from the conditions list, it must come right after
4928 the `regexp' or `notregexp' element.
4930 `todo' and `nottodo' accept as an argument a list of todo
4931 keywords, which may include \"*\" to match any todo keyword.
4933 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4935 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4937 Instead of a list, a keyword class may be given. For example:
4939 (org-agenda-skip-entry-if 'nottodo 'done)
4941 would skip entries that haven't been marked with any of \"DONE\"
4942 keywords. Possible classes are: `todo', `done', `any'.
4944 If any of these conditions is met, this function returns the end point of
4945 the entity, causing the search to continue from there. This is a function
4946 that can be put into `org-agenda-skip-function' for the duration of a command."
4947 (let (beg end m)
4948 (org-back-to-heading t)
4949 (setq beg (point)
4950 end (if subtree
4951 (progn (org-end-of-subtree t) (point))
4952 (progn (outline-next-heading) (1- (point)))))
4953 (goto-char beg)
4954 (and
4956 (and (memq 'scheduled conditions)
4957 (re-search-forward org-scheduled-time-regexp end t))
4958 (and (memq 'notscheduled conditions)
4959 (not (re-search-forward org-scheduled-time-regexp end t)))
4960 (and (memq 'deadline conditions)
4961 (re-search-forward org-deadline-time-regexp end t))
4962 (and (memq 'notdeadline conditions)
4963 (not (re-search-forward org-deadline-time-regexp end t)))
4964 (and (memq 'timestamp conditions)
4965 (re-search-forward org-ts-regexp end t))
4966 (and (memq 'nottimestamp conditions)
4967 (not (re-search-forward org-ts-regexp end t)))
4968 (and (setq m (memq 'regexp conditions))
4969 (stringp (nth 1 m))
4970 (re-search-forward (nth 1 m) end t))
4971 (and (setq m (memq 'notregexp conditions))
4972 (stringp (nth 1 m))
4973 (not (re-search-forward (nth 1 m) end t)))
4974 (and (or
4975 (setq m (memq 'nottodo conditions))
4976 (setq m (memq 'todo-unblocked conditions))
4977 (setq m (memq 'nottodo-unblocked conditions))
4978 (setq m (memq 'todo conditions)))
4979 (org-agenda-skip-if-todo m end)))
4980 end)))
4982 (defun org-agenda-skip-if-todo (args end)
4983 "Helper function for `org-agenda-skip-if', do not use it directly.
4984 ARGS is a list with first element either `todo', `nottodo',
4985 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4986 a list of TODO keywords, or a state symbol `todo' or `done' or
4987 `any'."
4988 (let ((kw (car args))
4989 (arg (cadr args))
4990 todo-wds todo-re)
4991 (setq todo-wds
4992 (org-uniquify
4993 (cond
4994 ((listp arg) ;; list of keywords
4995 (if (member "*" arg)
4996 (mapcar 'substring-no-properties org-todo-keywords-1)
4997 arg))
4998 ((symbolp arg) ;; keyword class name
4999 (cond
5000 ((eq arg 'todo)
5001 (org-delete-all org-done-keywords
5002 (mapcar 'substring-no-properties
5003 org-todo-keywords-1)))
5004 ((eq arg 'done) org-done-keywords)
5005 ((eq arg 'any)
5006 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5007 (setq todo-re
5008 (concat "^\\*+[ \t]+\\<\\("
5009 (mapconcat 'identity todo-wds "\\|")
5010 "\\)\\>"))
5011 (cond
5012 ((eq kw 'todo) (re-search-forward todo-re end t))
5013 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5014 ((eq kw 'todo-unblocked)
5015 (catch 'unblocked
5016 (while (re-search-forward todo-re end t)
5017 (or (org-entry-blocked-p) (throw 'unblocked t)))
5018 nil))
5019 ((eq kw 'nottodo-unblocked)
5020 (catch 'unblocked
5021 (while (re-search-forward todo-re end t)
5022 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5026 ;;;###autoload
5027 (defun org-agenda-list-stuck-projects (&rest ignore)
5028 "Create agenda view for projects that are stuck.
5029 Stuck projects are project that have no next actions. For the definitions
5030 of what a project is and how to check if it stuck, customize the variable
5031 `org-stuck-projects'."
5032 (interactive)
5033 (let* ((org-agenda-skip-function
5034 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5035 ;; We could have used org-agenda-skip-if here.
5036 (org-agenda-overriding-header
5037 (or org-agenda-overriding-header "List of stuck projects: "))
5038 (matcher (nth 0 org-stuck-projects))
5039 (todo (nth 1 org-stuck-projects))
5040 (todo-wds (if (member "*" todo)
5041 (progn
5042 (org-agenda-prepare-buffers (org-agenda-files
5043 nil 'ifmode))
5044 (org-delete-all
5045 org-done-keywords-for-agenda
5046 (copy-sequence org-todo-keywords-for-agenda)))
5047 todo))
5048 (todo-re (concat "^\\*+[ \t]+\\("
5049 (mapconcat 'identity todo-wds "\\|")
5050 "\\)\\>"))
5051 (tags (nth 2 org-stuck-projects))
5052 (tags-re (if (member "*" tags)
5053 (concat org-outline-regexp-bol
5054 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5055 (if tags
5056 (concat org-outline-regexp-bol
5057 ".*:\\("
5058 (mapconcat 'identity tags "\\|")
5059 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5060 (gen-re (nth 3 org-stuck-projects))
5061 (re-list
5062 (delq nil
5063 (list
5064 (if todo todo-re)
5065 (if tags tags-re)
5066 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5067 gen-re)))))
5068 (setq org-agenda-skip-regexp
5069 (if re-list
5070 (mapconcat 'identity re-list "\\|")
5071 (error "No information how to identify unstuck projects")))
5072 (org-tags-view nil matcher)
5073 (setq org-agenda-buffer-name (buffer-name))
5074 (with-current-buffer org-agenda-buffer-name
5075 (setq org-agenda-redo-command
5076 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5078 ;;; Diary integration
5080 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5081 (defvar diary-list-entries-hook)
5082 (defvar diary-time-regexp)
5083 (defun org-get-entries-from-diary (date)
5084 "Get the (Emacs Calendar) diary entries for DATE."
5085 (require 'diary-lib)
5086 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5087 (diary-display-hook '(fancy-diary-display))
5088 (diary-display-function 'fancy-diary-display)
5089 (pop-up-frames nil)
5090 (diary-list-entries-hook
5091 (cons 'org-diary-default-entry diary-list-entries-hook))
5092 (diary-file-name-prefix nil) ; turn this feature off
5093 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5094 entries
5095 (org-disable-agenda-to-diary t))
5096 (save-excursion
5097 (save-window-excursion
5098 (funcall (if (fboundp 'diary-list-entries)
5099 'diary-list-entries 'list-diary-entries)
5100 date 1)))
5101 (if (not (get-buffer diary-fancy-buffer))
5102 (setq entries nil)
5103 (with-current-buffer diary-fancy-buffer
5104 (setq buffer-read-only nil)
5105 (if (zerop (buffer-size))
5106 ;; No entries
5107 (setq entries nil)
5108 ;; Omit the date and other unnecessary stuff
5109 (org-agenda-cleanup-fancy-diary)
5110 ;; Add prefix to each line and extend the text properties
5111 (if (zerop (buffer-size))
5112 (setq entries nil)
5113 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5114 (setq entries
5115 (with-temp-buffer
5116 (insert entries) (goto-char (point-min))
5117 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5118 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5119 (replace-match (concat "; " (match-string 1)))))
5120 (buffer-string)))))
5121 (set-buffer-modified-p nil)
5122 (kill-buffer diary-fancy-buffer)))
5123 (when entries
5124 (setq entries (org-split-string entries "\n"))
5125 (setq entries
5126 (mapcar
5127 (lambda (x)
5128 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5129 ;; Extend the text properties to the beginning of the line
5130 (org-add-props x (text-properties-at (1- (length x)) x)
5131 'type "diary" 'date date 'face 'org-agenda-diary))
5132 entries)))))
5134 (defvar org-agenda-cleanup-fancy-diary-hook nil
5135 "Hook run when the fancy diary buffer is cleaned up.")
5137 (defun org-agenda-cleanup-fancy-diary ()
5138 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5139 This gets rid of the date, the underline under the date, and
5140 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5141 date. It also removes lines that contain only whitespace."
5142 (goto-char (point-min))
5143 (if (looking-at ".*?:[ \t]*")
5144 (progn
5145 (replace-match "")
5146 (re-search-forward "\n=+$" nil t)
5147 (replace-match "")
5148 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5149 (re-search-forward "\n=+$" nil t)
5150 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5151 (goto-char (point-min))
5152 (while (re-search-forward "^ +\n" nil t)
5153 (replace-match ""))
5154 (goto-char (point-min))
5155 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5156 (replace-match ""))
5157 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5159 ;; Make sure entries from the diary have the right text properties.
5160 (eval-after-load "diary-lib"
5161 '(if (boundp 'diary-modify-entry-list-string-function)
5162 ;; We can rely on the hook, nothing to do
5164 ;; Hook not available, must use advice to make this work
5165 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5166 "Make the position visible."
5167 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5168 (stringp string)
5169 buffer-file-name)
5170 (setq string (org-modify-diary-entry-string string))))))
5172 (defun org-modify-diary-entry-string (string)
5173 "Add text properties to string, allowing org-mode to act on it."
5174 (org-add-props string nil
5175 'mouse-face 'highlight
5176 'help-echo (if buffer-file-name
5177 (format "mouse-2 or RET jump to diary file %s"
5178 (abbreviate-file-name buffer-file-name))
5180 'org-agenda-diary-link t
5181 'org-marker (org-agenda-new-marker (point-at-bol))))
5183 (defun org-diary-default-entry ()
5184 "Add a dummy entry to the diary.
5185 Needed to avoid empty dates which mess up holiday display."
5186 ;; Catch the error if dealing with the new add-to-diary-alist
5187 (when org-disable-agenda-to-diary
5188 (condition-case nil
5189 (org-add-to-diary-list original-date "Org-mode dummy" "")
5190 (error
5191 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5193 (defun org-add-to-diary-list (&rest args)
5194 (if (fboundp 'diary-add-to-list)
5195 (apply 'diary-add-to-list args)
5196 (apply 'add-to-diary-list args)))
5198 (defvar org-diary-last-run-time nil)
5200 ;;;###autoload
5201 (defun org-diary (&rest args)
5202 "Return diary information from org files.
5203 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5204 It accesses org files and extracts information from those files to be
5205 listed in the diary. The function accepts arguments specifying what
5206 items should be listed. For a list of arguments allowed here, see the
5207 variable `org-agenda-entry-types'.
5209 The call in the diary file should look like this:
5211 &%%(org-diary) ~/path/to/some/orgfile.org
5213 Use a separate line for each org file to check. Or, if you omit the file name,
5214 all files listed in `org-agenda-files' will be checked automatically:
5216 &%%(org-diary)
5218 If you don't give any arguments (as in the example above), the default value
5219 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5220 So the example above may also be written as
5222 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5224 The function expects the lisp variables `entry' and `date' to be provided
5225 by the caller, because this is how the calendar works. Don't use this
5226 function from a program - use `org-agenda-get-day-entries' instead."
5227 (when (> (- (org-float-time)
5228 org-agenda-last-marker-time)
5230 ;; I am not sure if this works with sticky agendas, because the marker
5231 ;; list is then no longer a global variable.
5232 (org-agenda-reset-markers))
5233 (org-compile-prefix-format 'agenda)
5234 (org-set-sorting-strategy 'agenda)
5235 (setq args (or args org-agenda-entry-types))
5236 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5237 (list entry)
5238 (org-agenda-files t)))
5239 (time (org-float-time))
5240 file rtn results)
5241 (when (or (not org-diary-last-run-time)
5242 (> (- time
5243 org-diary-last-run-time)
5245 (org-agenda-prepare-buffers files))
5246 (setq org-diary-last-run-time time)
5247 ;; If this is called during org-agenda, don't return any entries to
5248 ;; the calendar. Org Agenda will list these entries itself.
5249 (if org-disable-agenda-to-diary (setq files nil))
5250 (while (setq file (pop files))
5251 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5252 (setq results (append results rtn)))
5253 (when results
5254 (setq results
5255 (mapcar (lambda (i) (replace-regexp-in-string
5256 org-bracket-link-regexp "\\3" i)) results))
5257 (concat (org-agenda-finalize-entries results) "\n"))))
5259 ;;; Agenda entry finders
5261 (defun org-agenda-get-day-entries (file date &rest args)
5262 "Does the work for `org-diary' and `org-agenda'.
5263 FILE is the path to a file to be checked for entries. DATE is date like
5264 the one returned by `calendar-current-date'. ARGS are symbols indicating
5265 which kind of entries should be extracted. For details about these, see
5266 the documentation of `org-diary'."
5267 (setq args (or args org-agenda-entry-types))
5268 (let* ((org-startup-folded nil)
5269 (org-startup-align-all-tables nil)
5270 (buffer (if (file-exists-p file)
5271 (org-get-agenda-file-buffer file)
5272 (error "No such file %s" file)))
5273 arg results rtn deadline-results)
5274 (if (not buffer)
5275 ;; If file does not exist, make sure an error message ends up in diary
5276 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5277 (with-current-buffer buffer
5278 (unless (derived-mode-p 'org-mode)
5279 (error "Agenda file %s is not in `org-mode'" file))
5280 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5281 (let ((case-fold-search nil))
5282 (save-excursion
5283 (save-restriction
5284 (if (eq buffer org-agenda-restrict)
5285 (narrow-to-region org-agenda-restrict-begin
5286 org-agenda-restrict-end)
5287 (widen))
5288 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5289 (while (setq arg (pop args))
5290 (cond
5291 ((and (eq arg :todo)
5292 (equal date (calendar-gregorian-from-absolute
5293 (org-today))))
5294 (setq rtn (org-agenda-get-todos))
5295 (setq results (append results rtn)))
5296 ((eq arg :timestamp)
5297 (setq rtn (org-agenda-get-blocks))
5298 (setq results (append results rtn))
5299 (setq rtn (org-agenda-get-timestamps deadline-results))
5300 (setq results (append results rtn)))
5301 ((eq arg :sexp)
5302 (setq rtn (org-agenda-get-sexps))
5303 (setq results (append results rtn)))
5304 ((eq arg :scheduled)
5305 (setq rtn (org-agenda-get-scheduled deadline-results))
5306 (setq results (append results rtn)))
5307 ((eq arg :scheduled*)
5308 (setq rtn (org-agenda-get-scheduled deadline-results t))
5309 (setq results (append results rtn)))
5310 ((eq arg :closed)
5311 (setq rtn (org-agenda-get-progress))
5312 (setq results (append results rtn)))
5313 ((eq arg :deadline)
5314 (setq rtn (org-agenda-get-deadlines))
5315 (setq deadline-results (copy-sequence rtn))
5316 (setq results (append results rtn)))
5317 ((eq arg :deadline*)
5318 (setq rtn (org-agenda-get-deadlines t))
5319 (setq deadline-results (copy-sequence rtn))
5320 (setq results (append results rtn))))))))
5321 results))))
5323 (defsubst org-em (x y list)
5324 "Is X or Y a member of LIST?"
5325 (or (memq x list) (memq y list)))
5327 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5328 (defvar org-agenda-sorting-strategy-selected nil)
5330 (defun org-agenda-get-todos ()
5331 "Return the TODO information for agenda display."
5332 (let* ((props (list 'face nil
5333 'done-face 'org-agenda-done
5334 'org-not-done-regexp org-not-done-regexp
5335 'org-todo-regexp org-todo-regexp
5336 'org-complex-heading-regexp org-complex-heading-regexp
5337 'mouse-face 'highlight
5338 'help-echo
5339 (format "mouse-2 or RET jump to org file %s"
5340 (abbreviate-file-name buffer-file-name))))
5341 (regexp (format org-heading-keyword-regexp-format
5342 (cond
5343 ((and org-select-this-todo-keyword
5344 (equal org-select-this-todo-keyword "*"))
5345 org-todo-regexp)
5346 (org-select-this-todo-keyword
5347 (concat "\\("
5348 (mapconcat 'identity
5349 (org-split-string
5350 org-select-this-todo-keyword
5351 "|")
5352 "\\|") "\\)"))
5353 (t org-not-done-regexp))))
5354 marker priority category category-pos level tags todo-state ts-date ts-date-type
5355 ee txt beg end inherited-tags todo-state-end-pos)
5356 (goto-char (point-min))
5357 (while (re-search-forward regexp nil t)
5358 (catch :skip
5359 (save-match-data
5360 (beginning-of-line)
5361 (org-agenda-skip)
5362 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5363 (unless (and (setq todo-state (org-get-todo-state))
5364 (setq todo-state-end-pos (match-end 2)))
5365 (goto-char end)
5366 (throw :skip nil))
5367 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5368 (goto-char (1+ beg))
5369 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5370 (throw :skip nil)))
5371 (goto-char (match-beginning 2))
5372 (setq marker (org-agenda-new-marker (match-beginning 0))
5373 category (org-get-category)
5374 ts-date (let (ts)
5375 (save-match-data
5376 (cond ((org-em 'scheduled-up 'scheduled-down
5377 org-agenda-sorting-strategy-selected)
5378 (setq ts (org-entry-get (point) "SCHEDULED")
5379 ts-date-type " scheduled"))
5380 ((org-em 'deadline-up 'deadline-down
5381 org-agenda-sorting-strategy-selected)
5382 (setq ts (org-entry-get (point) "DEADLINE")
5383 ts-date-type " deadline"))
5384 ((org-em 'ts-up 'ts-down
5385 org-agenda-sorting-strategy-selected)
5386 (setq ts (org-entry-get (point) "TIMESTAMP")
5387 ts-date-type " timestamp"))
5388 ((org-em 'tsia-up 'tsia-down
5389 org-agenda-sorting-strategy-selected)
5390 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5391 ts-date-type " timestamp_ia"))
5392 ((org-em 'timestamp-up 'timestamp-down
5393 org-agenda-sorting-strategy-selected)
5394 (setq ts (or (org-entry-get (point) "SCHEDULED")
5395 (org-entry-get (point) "DEADLINE")
5396 (org-entry-get (point) "TIMESTAMP")
5397 (org-entry-get (point) "TIMESTAMP_IA"))
5398 ts-date-type ""))
5399 (t (setq ts-date-type "")))
5400 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5401 category-pos (get-text-property (point) 'org-category-position)
5402 txt (org-trim
5403 (buffer-substring (match-beginning 2) (match-end 0)))
5404 inherited-tags
5405 (or (eq org-agenda-show-inherited-tags 'always)
5406 (and (listp org-agenda-show-inherited-tags)
5407 (memq 'todo org-agenda-show-inherited-tags))
5408 (and (eq org-agenda-show-inherited-tags t)
5409 (or (eq org-agenda-use-tag-inheritance t)
5410 (memq 'todo org-agenda-use-tag-inheritance))))
5411 tags (org-get-tags-at nil (not inherited-tags))
5412 level (make-string (org-reduced-level (org-outline-level)) ? )
5413 txt (org-agenda-format-item "" txt level category tags t)
5414 priority (1+ (org-get-priority txt)))
5415 (org-add-props txt props
5416 'org-marker marker 'org-hd-marker marker
5417 'priority priority 'org-category category
5418 'level level
5419 'ts-date ts-date
5420 'org-category-position category-pos
5421 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5422 (push txt ee)
5423 (if org-agenda-todo-list-sublevels
5424 (goto-char todo-state-end-pos)
5425 (org-end-of-subtree 'invisible))))
5426 (nreverse ee)))
5428 (defun org-agenda-todo-custom-ignore-p (time n)
5429 "Check whether timestamp is farther away than n number of days.
5430 This function is invoked if `org-agenda-todo-ignore-deadlines',
5431 `org-agenda-todo-ignore-scheduled' or
5432 `org-agenda-todo-ignore-timestamp' is set to an integer."
5433 (let ((days (org-time-stamp-to-now
5434 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5435 (if (>= n 0)
5436 (>= days n)
5437 (<= days n))))
5439 ;;;###autoload
5440 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5441 (&optional end)
5442 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5443 (when (or org-agenda-todo-ignore-with-date
5444 org-agenda-todo-ignore-scheduled
5445 org-agenda-todo-ignore-deadlines
5446 org-agenda-todo-ignore-timestamp)
5447 (setq end (or end (save-excursion (outline-next-heading) (point))))
5448 (save-excursion
5449 (or (and org-agenda-todo-ignore-with-date
5450 (re-search-forward org-ts-regexp end t))
5451 (and org-agenda-todo-ignore-scheduled
5452 (re-search-forward org-scheduled-time-regexp end t)
5453 (cond
5454 ((eq org-agenda-todo-ignore-scheduled 'future)
5455 (> (org-time-stamp-to-now
5456 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5457 ((eq org-agenda-todo-ignore-scheduled 'past)
5458 (<= (org-time-stamp-to-now
5459 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5460 ((numberp org-agenda-todo-ignore-scheduled)
5461 (org-agenda-todo-custom-ignore-p
5462 (match-string 1) org-agenda-todo-ignore-scheduled))
5463 (t)))
5464 (and org-agenda-todo-ignore-deadlines
5465 (re-search-forward org-deadline-time-regexp end t)
5466 (cond
5467 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5468 ((eq org-agenda-todo-ignore-deadlines 'far)
5469 (not (org-deadline-close (match-string 1))))
5470 ((eq org-agenda-todo-ignore-deadlines 'future)
5471 (> (org-time-stamp-to-now
5472 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5473 ((eq org-agenda-todo-ignore-deadlines 'past)
5474 (<= (org-time-stamp-to-now
5475 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5476 ((numberp org-agenda-todo-ignore-deadlines)
5477 (org-agenda-todo-custom-ignore-p
5478 (match-string 1) org-agenda-todo-ignore-deadlines))
5479 (t (org-deadline-close (match-string 1)))))
5480 (and org-agenda-todo-ignore-timestamp
5481 (let ((buffer (current-buffer))
5482 (regexp
5483 (concat
5484 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5485 (start (point)))
5486 ;; Copy current buffer into a temporary one
5487 (with-temp-buffer
5488 (insert-buffer-substring buffer start end)
5489 (goto-char (point-min))
5490 ;; Delete SCHEDULED and DEADLINE items
5491 (while (re-search-forward regexp end t)
5492 (delete-region (match-beginning 0) (match-end 0)))
5493 (goto-char (point-min))
5494 ;; No search for timestamp left
5495 (when (re-search-forward org-ts-regexp nil t)
5496 (cond
5497 ((eq org-agenda-todo-ignore-timestamp 'future)
5498 (> (org-time-stamp-to-now
5499 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5500 ((eq org-agenda-todo-ignore-timestamp 'past)
5501 (<= (org-time-stamp-to-now
5502 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5503 ((numberp org-agenda-todo-ignore-timestamp)
5504 (org-agenda-todo-custom-ignore-p
5505 (match-string 1) org-agenda-todo-ignore-timestamp))
5506 (t))))))))))
5508 (defun org-agenda-get-timestamps (&optional deadline-results)
5509 "Return the date stamp information for agenda display."
5510 (let* ((props (list 'face 'org-agenda-calendar-event
5511 'org-not-done-regexp org-not-done-regexp
5512 'org-todo-regexp org-todo-regexp
5513 'org-complex-heading-regexp org-complex-heading-regexp
5514 'mouse-face 'highlight
5515 'help-echo
5516 (format "mouse-2 or RET jump to org file %s"
5517 (abbreviate-file-name buffer-file-name))))
5518 (d1 (calendar-absolute-from-gregorian date))
5520 (deadline-position-alist
5521 (mapcar (lambda (a) (and (setq mm (get-text-property
5522 0 'org-hd-marker a))
5523 (cons (marker-position mm) a)))
5524 deadline-results))
5525 (remove-re org-ts-regexp)
5526 (regexp
5527 (concat
5528 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5529 (regexp-quote
5530 (substring
5531 (format-time-string
5532 (car org-time-stamp-formats)
5533 (apply 'encode-time ; DATE bound by calendar
5534 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5535 1 11))
5536 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5537 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5538 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5539 donep tmp priority category category-pos level ee txt timestr tags
5540 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5541 inherited-tags ts-date)
5542 (goto-char (point-min))
5543 (while (setq end-of-match (re-search-forward regexp nil t))
5544 (setq b0 (match-beginning 0)
5545 b3 (match-beginning 3) e3 (match-end 3)
5546 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5547 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5548 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5549 (member todo-state
5550 org-agenda-repeating-timestamp-show-all)))
5551 (catch :skip
5552 (and (org-at-date-range-p) (throw :skip nil))
5553 (org-agenda-skip)
5554 (if (and (match-end 1)
5555 (not (= d1 (org-time-string-to-absolute
5556 (match-string 1) d1 nil show-all
5557 (current-buffer) b0))))
5558 (throw :skip nil))
5559 (if (and e3
5560 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5561 (throw :skip nil))
5562 (setq tmp (buffer-substring (max (point-min)
5563 (- b0 org-ds-keyword-length))
5565 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5566 inactivep (= (char-after b0) ?\[)
5567 deadlinep (string-match org-deadline-regexp tmp)
5568 scheduledp (string-match org-scheduled-regexp tmp)
5569 closedp (and org-agenda-include-inactive-timestamps
5570 (string-match org-closed-string tmp))
5571 clockp (and org-agenda-include-inactive-timestamps
5572 (or (string-match org-clock-string tmp)
5573 (string-match "]-+\\'" tmp)))
5574 warntime (get-text-property (point) 'org-appt-warntime)
5575 donep (member todo-state org-done-keywords))
5576 (if (or scheduledp deadlinep closedp clockp
5577 (and donep org-agenda-skip-timestamp-if-done))
5578 (throw :skip t))
5579 (if (string-match ">" timestr)
5580 ;; substring should only run to end of time stamp
5581 (setq timestr (substring timestr 0 (match-end 0))))
5582 (setq marker (org-agenda-new-marker b0)
5583 category (org-get-category b0)
5584 category-pos (get-text-property b0 'org-category-position))
5585 (save-excursion
5586 (if (not (re-search-backward org-outline-regexp-bol nil t))
5587 (throw :skip nil)
5588 (goto-char (match-beginning 0))
5589 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5590 (assoc (point) deadline-position-alist))
5591 (throw :skip nil))
5592 (setq hdmarker (org-agenda-new-marker)
5593 inherited-tags
5594 (or (eq org-agenda-show-inherited-tags 'always)
5595 (and (listp org-agenda-show-inherited-tags)
5596 (memq 'agenda org-agenda-show-inherited-tags))
5597 (and (eq org-agenda-show-inherited-tags t)
5598 (or (eq org-agenda-use-tag-inheritance t)
5599 (memq 'agenda org-agenda-use-tag-inheritance))))
5600 tags (org-get-tags-at nil (not inherited-tags))
5601 level (make-string (org-reduced-level (org-outline-level)) ? ))
5602 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5603 (setq head (or (match-string 1) ""))
5604 (setq txt (org-agenda-format-item
5605 (if inactivep org-agenda-inactive-leader nil)
5606 head level category tags timestr
5607 remove-re habitp)))
5608 (setq priority (org-get-priority txt))
5609 (org-add-props txt props 'priority priority
5610 'org-marker marker 'org-hd-marker hdmarker
5611 'org-category category 'date date
5612 'level level
5613 'ts-date
5614 (ignore-errors (org-time-string-to-absolute timestr))
5615 'org-category-position category-pos
5616 'todo-state todo-state
5617 'warntime warntime
5618 'type "timestamp")
5619 (push txt ee))
5620 (if org-agenda-skip-additional-timestamps-same-entry
5621 (outline-next-heading)
5622 (goto-char end-of-match))))
5623 (nreverse ee)))
5625 (defun org-agenda-get-sexps ()
5626 "Return the sexp information for agenda display."
5627 (require 'diary-lib)
5628 (let* ((props (list 'face 'org-agenda-calendar-sexp
5629 'mouse-face 'highlight
5630 'help-echo
5631 (format "mouse-2 or RET jump to org file %s"
5632 (abbreviate-file-name buffer-file-name))))
5633 (regexp "^&?%%(")
5634 marker category extra category-pos level ee txt tags entry
5635 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5636 (goto-char (point-min))
5637 (while (re-search-forward regexp nil t)
5638 (catch :skip
5639 (org-agenda-skip)
5640 (setq beg (match-beginning 0))
5641 (goto-char (1- (match-end 0)))
5642 (setq b (point))
5643 (forward-sexp 1)
5644 (setq sexp (buffer-substring b (point)))
5645 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5646 (org-trim (match-string 1))
5647 ""))
5648 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5649 (when result
5650 (setq marker (org-agenda-new-marker beg)
5651 level (make-string (org-reduced-level (org-outline-level)) ? )
5652 category (org-get-category beg)
5653 category-pos (get-text-property beg 'org-category-position)
5654 inherited-tags
5655 (or (eq org-agenda-show-inherited-tags 'always)
5656 (and (listp org-agenda-show-inherited-tags)
5657 (memq 'agenda org-agenda-show-inherited-tags))
5658 (and (eq org-agenda-show-inherited-tags t)
5659 (or (eq org-agenda-use-tag-inheritance t)
5660 (memq 'agenda org-agenda-use-tag-inheritance))))
5661 tags (org-get-tags-at nil (not inherited-tags))
5662 todo-state (org-get-todo-state)
5663 warntime (get-text-property (point) 'org-appt-warntime)
5664 extra nil)
5666 (dolist (r (if (stringp result)
5667 (list result)
5668 result)) ;; we expect a list here
5669 (when (and org-agenda-diary-sexp-prefix
5670 (string-match org-agenda-diary-sexp-prefix r))
5671 (setq extra (match-string 0 r)
5672 r (replace-match "" nil nil r)))
5673 (if (string-match "\\S-" r)
5674 (setq txt r)
5675 (setq txt "SEXP entry returned empty string"))
5676 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5677 (org-add-props txt props 'org-marker marker
5678 'org-category category 'date date 'todo-state todo-state
5679 'org-category-position category-pos 'tags tags
5680 'level level
5681 'type "sexp" 'warntime warntime)
5682 (push txt ee)))))
5683 (nreverse ee)))
5685 ;; Calendar sanity: define some functions that are independent of
5686 ;; `calendar-date-style'.
5687 ;; Normally I would like to use ISO format when calling the diary functions,
5688 ;; but to make sure we still have Emacs 22 compatibility we bind
5689 ;; also `european-calendar-style' and use european format
5690 (defun org-anniversary (year month day &optional mark)
5691 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5692 (org-no-warnings
5693 (let ((calendar-date-style 'european) (european-calendar-style t))
5694 (diary-anniversary day month year mark))))
5695 (defun org-cyclic (N year month day &optional mark)
5696 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5697 (org-no-warnings
5698 (let ((calendar-date-style 'european) (european-calendar-style t))
5699 (diary-cyclic N day month year mark))))
5700 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5701 "Like `diary-block', but with fixed (ISO) order of arguments."
5702 (org-no-warnings
5703 (let ((calendar-date-style 'european) (european-calendar-style t))
5704 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5705 (defun org-date (year month day &optional mark)
5706 "Like `diary-date', but with fixed (ISO) order of arguments."
5707 (org-no-warnings
5708 (let ((calendar-date-style 'european) (european-calendar-style t))
5709 (diary-date day month year mark))))
5711 ;; Define the` org-class' function
5712 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5713 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5714 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5715 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5716 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5717 `holidays', then any date that is known by the Emacs calendar to be a
5718 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5719 then those holidays will be skipped."
5720 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5721 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5722 (d (calendar-absolute-from-gregorian date))
5723 (h (when skip-weeks (calendar-check-holidays date))))
5724 (and
5725 (<= date1 d)
5726 (<= d date2)
5727 (= (calendar-day-of-week date) dayname)
5728 (or (not skip-weeks)
5729 (progn
5730 (require 'cal-iso)
5731 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5732 (not (or (and h (memq 'holidays skip-weeks))
5733 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5734 entry)))
5736 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5737 "Like `org-class', but honor `calendar-date-style'.
5738 The order of the first 2 times 3 arguments depends on the variable
5739 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5740 So for American calendars, give this as MONTH DAY YEAR, for European as
5741 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5742 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5743 is any number of ISO weeks in the block period for which the item should
5744 be skipped.
5746 This function is here only for backward compatibility and it is deprecated,
5747 please use `org-class' instead."
5748 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5749 (date2 (org-order-calendar-date-args m2 d2 y2)))
5750 (org-class
5751 (nth 2 date1) (car date1) (nth 1 date1)
5752 (nth 2 date2) (car date2) (nth 1 date2)
5753 dayname skip-weeks)))
5754 (make-obsolete 'org-diary-class 'org-class "")
5756 (defalias 'org-get-closed 'org-agenda-get-progress)
5757 (defun org-agenda-get-progress ()
5758 "Return the logged TODO entries for agenda display."
5759 (let* ((props (list 'mouse-face 'highlight
5760 'org-not-done-regexp org-not-done-regexp
5761 'org-todo-regexp org-todo-regexp
5762 'org-complex-heading-regexp org-complex-heading-regexp
5763 'help-echo
5764 (format "mouse-2 or RET jump to org file %s"
5765 (abbreviate-file-name buffer-file-name))))
5766 (items (if (consp org-agenda-show-log-scoped)
5767 org-agenda-show-log-scoped
5768 (if (eq org-agenda-show-log-scoped 'clockcheck)
5769 '(clock)
5770 org-agenda-log-mode-items)))
5771 (parts
5772 (delq nil
5773 (list
5774 (if (memq 'closed items) (concat "\\<" org-closed-string))
5775 (if (memq 'clock items) (concat "\\<" org-clock-string))
5776 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5777 (parts-re (if parts (mapconcat 'identity parts "\\|")
5778 (error "`org-agenda-log-mode-items' is empty")))
5779 (regexp (concat
5780 "\\(" parts-re "\\)"
5781 " *\\["
5782 (regexp-quote
5783 (substring
5784 (format-time-string
5785 (car org-time-stamp-formats)
5786 (apply 'encode-time ; DATE bound by calendar
5787 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5788 1 11))))
5789 (org-agenda-search-headline-for-time nil)
5790 marker hdmarker priority category category-pos level tags closedp
5791 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5792 (goto-char (point-min))
5793 (while (re-search-forward regexp nil t)
5794 (catch :skip
5795 (org-agenda-skip)
5796 (setq marker (org-agenda-new-marker (match-beginning 0))
5797 closedp (equal (match-string 1) org-closed-string)
5798 statep (equal (string-to-char (match-string 1)) ?-)
5799 clockp (not (or closedp statep))
5800 state (and statep (match-string 2))
5801 category (org-get-category (match-beginning 0))
5802 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5803 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5804 (when (string-match "\\]" timestr)
5805 ;; substring should only run to end of time stamp
5806 (setq rest (substring timestr (match-end 0))
5807 timestr (substring timestr 0 (match-end 0)))
5808 (if (and (not closedp) (not statep)
5809 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5810 rest))
5811 (progn (setq timestr (concat (substring timestr 0 -1)
5812 "-" (match-string 1 rest) "]"))
5813 (setq clocked (match-string 2 rest)))
5814 (setq clocked "-")))
5815 (save-excursion
5816 (setq extra
5817 (cond
5818 ((not org-agenda-log-mode-add-notes) nil)
5819 (statep
5820 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5821 (match-string 1)))
5822 (clockp
5823 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5824 (match-string 1)))))
5825 (if (not (re-search-backward org-outline-regexp-bol nil t))
5826 (throw :skip nil)
5827 (goto-char (match-beginning 0))
5828 (setq hdmarker (org-agenda-new-marker)
5829 inherited-tags
5830 (or (eq org-agenda-show-inherited-tags 'always)
5831 (and (listp org-agenda-show-inherited-tags)
5832 (memq 'todo org-agenda-show-inherited-tags))
5833 (and (eq org-agenda-show-inherited-tags t)
5834 (or (eq org-agenda-use-tag-inheritance t)
5835 (memq 'todo org-agenda-use-tag-inheritance))))
5836 tags (org-get-tags-at nil (not inherited-tags))
5837 level (make-string (org-reduced-level (org-outline-level)) ? ))
5838 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5839 (setq txt (match-string 1))
5840 (when extra
5841 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5842 (setq txt (concat (substring txt 0 (match-beginning 1))
5843 " - " extra " " (match-string 2 txt)))
5844 (setq txt (concat txt " - " extra))))
5845 (setq txt (org-agenda-format-item
5846 (cond
5847 (closedp "Closed: ")
5848 (statep (concat "State: (" state ")"))
5849 (t (concat "Clocked: (" clocked ")")))
5850 txt level category tags timestr)))
5851 (setq priority 100000)
5852 (org-add-props txt props
5853 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5854 'priority priority 'org-category category
5855 'org-category-position category-pos
5856 'level level
5857 'type "closed" 'date date
5858 'undone-face 'org-warning 'done-face 'org-agenda-done)
5859 (push txt ee))
5860 (goto-char (point-at-eol))))
5861 (nreverse ee)))
5863 (defun org-agenda-show-clocking-issues ()
5864 "Add overlays, showing issues with clocking.
5865 See also the user option `org-agenda-clock-consistency-checks'."
5866 (interactive)
5867 (let* ((org-time-clocksum-use-effort-durations nil)
5868 (pl org-agenda-clock-consistency-checks)
5869 (re (concat "^[ \t]*"
5870 org-clock-string
5871 "[ \t]+"
5872 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5873 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5874 (tlstart 0.)
5875 (tlend 0.)
5876 (maxtime (org-hh:mm-string-to-minutes
5877 (or (plist-get pl :max-duration) "24:00")))
5878 (mintime (org-hh:mm-string-to-minutes
5879 (or (plist-get pl :min-duration) 0)))
5880 (maxgap (org-hh:mm-string-to-minutes
5881 ;; default 30:00 means never complain
5882 (or (plist-get pl :max-gap) "30:00")))
5883 (gapok (mapcar 'org-hh:mm-string-to-minutes
5884 (plist-get pl :gap-ok-around)))
5885 (def-face (or (plist-get pl :default-face)
5886 '((:background "DarkRed") (:foreground "white"))))
5887 issue face m te ts dt ov)
5888 (goto-char (point-min))
5889 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5890 (setq issue nil face def-face)
5891 (catch 'next
5892 (setq m (org-get-at-bol 'org-marker)
5893 te nil ts nil)
5894 (unless (and m (markerp m))
5895 (setq issue "No valid clock line") (throw 'next t))
5896 (org-with-point-at m
5897 (save-excursion
5898 (goto-char (point-at-bol))
5899 (unless (looking-at re)
5900 (error "No valid Clock line")
5901 (throw 'next t))
5902 (unless (match-end 3)
5903 (setq issue "No end time"
5904 face (or (plist-get pl :no-end-time-face) face))
5905 (throw 'next t))
5906 (setq ts (match-string 1)
5907 te (match-string 3)
5908 ts (org-float-time
5909 (apply 'encode-time (org-parse-time-string ts)))
5910 te (org-float-time
5911 (apply 'encode-time (org-parse-time-string te)))
5912 dt (- te ts))))
5913 (cond
5914 ((> dt (* 60 maxtime))
5915 ;; a very long clocking chunk
5916 (setq issue (format "Clocking interval is very long: %s"
5917 (org-minutes-to-clocksum-string
5918 (floor (/ (float dt) 60.))))
5919 face (or (plist-get pl :long-face) face)))
5920 ((< dt (* 60 mintime))
5921 ;; a very short clocking chunk
5922 (setq issue (format "Clocking interval is very short: %s"
5923 (org-minutes-to-clocksum-string
5924 (floor (/ (float dt) 60.))))
5925 face (or (plist-get pl :short-face) face)))
5926 ((and (> tlend 0) (< ts tlend))
5927 ;; Two clock entries are overlapping
5928 (setq issue (format "Clocking overlap: %d minutes"
5929 (/ (- tlend ts) 60))
5930 face (or (plist-get pl :overlap-face) face)))
5931 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5932 ;; There is a gap, lets see if we need to report it
5933 (unless (org-agenda-check-clock-gap tlend ts gapok)
5934 (setq issue (format "Clocking gap: %d minutes"
5935 (/ (- ts tlend) 60))
5936 face (or (plist-get pl :gap-face) face))))
5937 (t nil)))
5938 (setq tlend (or te tlend) tlstart (or ts tlstart))
5939 (when issue
5940 ;; OK, there was some issue, add an overlay to show the issue
5941 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5942 (overlay-put ov 'before-string
5943 (concat
5944 (org-add-props
5945 (format "%-43s" (concat " " issue))
5947 'face face)
5948 "\n"))
5949 (overlay-put ov 'evaporate t)))))
5951 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5952 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5953 (catch 'exit
5954 (unless ok-list
5955 ;; there are no OK times for gaps...
5956 (throw 'exit nil))
5957 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5958 ;; This is more than 24 hours, so it is OK.
5959 ;; because we have at least one OK time, that must be in the
5960 ;; 24 hour interval.
5961 (throw 'exit t))
5962 ;; We have a shorter gap.
5963 ;; Now we have to get the minute of the day when these times are
5964 (let* ((t1dec (decode-time (seconds-to-time t1)))
5965 (t2dec (decode-time (seconds-to-time t2)))
5966 ;; compute the minute on the day
5967 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5968 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5969 (when (< min2 min1)
5970 ;; if min2 is smaller than min1, this means it is on the next day.
5971 ;; Wrap it to after midnight.
5972 (setq min2 (+ min2 1440)))
5973 ;; Now check if any of the OK times is in the gap
5974 (mapc (lambda (x)
5975 ;; Wrap the time to after midnight if necessary
5976 (if (< x min1) (setq x (+ x 1440)))
5977 ;; Check if in interval
5978 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5979 ok-list)
5980 ;; Nope, this gap is not OK
5981 nil)))
5983 (defun org-agenda-get-deadlines (&optional with-hour)
5984 "Return the deadline information for agenda display.
5985 When WITH-HOUR is non-nil, only return deadlines with an hour
5986 specification like [h]h:mm."
5987 (let* ((props (list 'mouse-face 'highlight
5988 'org-not-done-regexp org-not-done-regexp
5989 'org-todo-regexp org-todo-regexp
5990 'org-complex-heading-regexp org-complex-heading-regexp
5991 'help-echo
5992 (format "mouse-2 or RET jump to org file %s"
5993 (abbreviate-file-name buffer-file-name))))
5994 (regexp (if with-hour
5995 org-deadline-time-hour-regexp
5996 org-deadline-time-regexp))
5997 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5998 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5999 (dl0 (car org-agenda-deadline-leaders))
6000 (dl1 (nth 1 org-agenda-deadline-leaders))
6001 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6002 d2 diff dfrac wdays pos pos1 category category-pos level
6003 tags suppress-prewarning ee txt head face s todo-state
6004 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6005 (goto-char (point-min))
6006 (while (re-search-forward regexp nil t)
6007 (catch :skip
6008 (org-agenda-skip)
6009 (setq s (match-string 1)
6010 txt nil
6011 pos (1- (match-beginning 1))
6012 todo-state (save-match-data (org-get-todo-state))
6013 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6014 (member todo-state
6015 org-agenda-repeating-timestamp-show-all))
6016 d2 (org-time-string-to-absolute
6017 s d1 'past show-all (current-buffer) pos)
6018 diff (- d2 d1))
6019 (setq suppress-prewarning
6020 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6021 (let ((item (buffer-substring (point-at-bol)
6022 (point-at-eol))))
6023 (save-match-data
6024 (and (string-match
6025 org-scheduled-time-regexp item)
6026 (match-string 1 item)))))))
6027 (cond
6028 ((not ds) nil)
6029 ;; The current item has a scheduled date (in ds), so
6030 ;; evaluate its prewarning lead time.
6031 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6032 ;; Use global prewarning-restart lead time.
6033 org-agenda-skip-deadline-prewarning-if-scheduled)
6034 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6035 'pre-scheduled)
6036 ;; Set prewarning to no earlier than scheduled.
6037 (min (- d2 (org-time-string-to-absolute
6038 ds d1 'past show-all (current-buffer) pos))
6039 org-deadline-warning-days))
6040 ;; Set prewarning to deadline.
6041 (t 0))))
6042 (setq wdays (if suppress-prewarning
6043 (let ((org-deadline-warning-days suppress-prewarning))
6044 (org-get-wdays s))
6045 (org-get-wdays s))
6046 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6047 upcomingp (and todayp (> diff 0)))
6048 ;; When to show a deadline in the calendar:
6049 ;; If the expiration is within wdays warning time.
6050 ;; Past-due deadlines are only shown on the current date
6051 (if (and (or (and (<= diff wdays)
6052 (and todayp (not org-agenda-only-exact-dates)))
6053 (= diff 0)))
6054 (save-excursion
6055 ;; (setq todo-state (org-get-todo-state))
6056 (setq donep (member todo-state org-done-keywords))
6057 (if (and donep
6058 (or org-agenda-skip-deadline-if-done
6059 (not (= diff 0))))
6060 (setq txt nil)
6061 (setq category (org-get-category)
6062 warntime (get-text-property (point) 'org-appt-warntime)
6063 category-pos (get-text-property (point) 'org-category-position))
6064 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6065 (throw :skip nil)
6066 (goto-char (match-end 0))
6067 (setq pos1 (match-beginning 0))
6068 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6069 (setq inherited-tags
6070 (or (eq org-agenda-show-inherited-tags 'always)
6071 (and (listp org-agenda-show-inherited-tags)
6072 (memq 'agenda org-agenda-show-inherited-tags))
6073 (and (eq org-agenda-show-inherited-tags t)
6074 (or (eq org-agenda-use-tag-inheritance t)
6075 (memq 'agenda org-agenda-use-tag-inheritance))))
6076 tags (org-get-tags-at pos1 (not inherited-tags)))
6077 (setq head (buffer-substring
6078 (point)
6079 (progn (skip-chars-forward "^\r\n")
6080 (point))))
6081 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6082 (setq timestr
6083 (concat (substring s (match-beginning 1)) " "))
6084 (setq timestr 'time))
6085 (setq txt (org-agenda-format-item
6086 (cond ((= diff 0) dl0)
6087 ((> diff 0)
6088 (if (functionp dl1)
6089 (funcall dl1 diff date)
6090 (format dl1 diff)))
6092 (if (functionp dl2)
6093 (funcall dl2 diff date)
6094 (format dl2 (if (string= dl2 dl1)
6095 diff (abs diff))))))
6096 head level category tags
6097 (if (not (= diff 0)) nil timestr)))))
6098 (when txt
6099 (setq face (org-agenda-deadline-face dfrac))
6100 (org-add-props txt props
6101 'org-marker (org-agenda-new-marker pos)
6102 'warntime warntime
6103 'level level
6104 'ts-date d2
6105 'org-hd-marker (org-agenda-new-marker pos1)
6106 'priority (+ (- diff)
6107 (org-get-priority txt))
6108 'org-category category
6109 'org-category-position category-pos
6110 'todo-state todo-state
6111 'type (if upcomingp "upcoming-deadline" "deadline")
6112 'date (if upcomingp date d2)
6113 'face (if donep 'org-agenda-done face)
6114 'undone-face face 'done-face 'org-agenda-done)
6115 (push txt ee))))))
6116 (nreverse ee)))
6118 (defun org-agenda-deadline-face (fraction)
6119 "Return the face to displaying a deadline item.
6120 FRACTION is what fraction of the head-warning time has passed."
6121 (let ((faces org-agenda-deadline-faces) f)
6122 (catch 'exit
6123 (while (setq f (pop faces))
6124 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6126 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6127 "Return the scheduled information for agenda display.
6128 When WITH-HOUR is non-nil, only return scheduled items with
6129 an hour specification like [h]h:mm."
6130 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6131 'org-todo-regexp org-todo-regexp
6132 'org-complex-heading-regexp org-complex-heading-regexp
6133 'done-face 'org-agenda-done
6134 'mouse-face 'highlight
6135 'help-echo
6136 (format "mouse-2 or RET jump to org file %s"
6137 (abbreviate-file-name buffer-file-name))))
6138 (regexp (if with-hour
6139 org-scheduled-time-hour-regexp
6140 org-scheduled-time-regexp))
6141 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6142 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6144 (deadline-position-alist
6145 (mapcar (lambda (a) (and (setq mm (get-text-property
6146 0 'org-hd-marker a))
6147 (cons (marker-position mm) a)))
6148 deadline-results))
6149 d2 diff pos pos1 category category-pos level tags donep
6150 ee txt head pastschedp todo-state face timestr s habitp show-all
6151 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6152 ddays)
6153 (goto-char (point-min))
6154 (while (re-search-forward regexp nil t)
6155 (catch :skip
6156 (org-agenda-skip)
6157 (setq s (match-string 1)
6158 txt nil
6159 pos (1- (match-beginning 1))
6160 todo-state (save-match-data (org-get-todo-state))
6161 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6162 (member todo-state
6163 org-agenda-repeating-timestamp-show-all))
6164 d2 (org-time-string-to-absolute
6165 s d1 'past show-all (current-buffer) pos)
6166 diff (- d2 d1)
6167 warntime (get-text-property (point) 'org-appt-warntime))
6168 (setq pastschedp (and todayp (< diff 0)))
6169 (setq did-habit-check-p nil)
6170 (setq suppress-delay
6171 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6172 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6173 (save-match-data
6174 (and (string-match
6175 org-deadline-time-regexp item)
6176 (match-string 1 item)))))))
6177 (cond
6178 ((not ds) nil)
6179 ;; The current item has a deadline date (in ds), so
6180 ;; evaluate its delay time.
6181 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6182 ;; Use global delay time.
6183 (- org-agenda-skip-scheduled-delay-if-deadline))
6184 ((eq org-agenda-skip-scheduled-delay-if-deadline
6185 'post-deadline)
6186 ;; Set delay to no later than deadline.
6187 (min (- d2 (org-time-string-to-absolute
6188 ds d1 'past show-all (current-buffer) pos))
6189 org-scheduled-delay-days))
6190 (t 0))))
6191 (setq ddays (if suppress-delay
6192 (let ((org-scheduled-delay-days suppress-delay))
6193 (org-get-wdays s t t))
6194 (org-get-wdays s t)))
6195 ;; Use a delay of 0 when there is a repeater and the delay is
6196 ;; of the form --3d
6197 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6198 (< (org-time-string-to-absolute s)
6199 (org-time-string-to-absolute
6200 s d2 'past nil (current-buffer) pos)))
6201 (setq ddays 0))
6202 ;; When to show a scheduled item in the calendar:
6203 ;; If it is on or past the date.
6204 (when (or (and (> ddays 0) (= diff (- ddays)))
6205 (and (zerop ddays) (= diff 0))
6206 (and (< (+ diff ddays) 0)
6207 (< (abs diff) org-scheduled-past-days)
6208 (and todayp (not org-agenda-only-exact-dates)))
6209 ;; org-is-habit-p uses org-entry-get, which is expansive
6210 ;; so we go extra mile to only call it once
6211 (and todayp
6212 (boundp 'org-habit-show-all-today)
6213 org-habit-show-all-today
6214 (setq did-habit-check-p t)
6215 (setq habitp (and (functionp 'org-is-habit-p)
6216 (org-is-habit-p)))))
6217 (save-excursion
6218 (setq donep (member todo-state org-done-keywords))
6219 (if (and donep
6220 (or org-agenda-skip-scheduled-if-done
6221 (not (= diff 0))
6222 (and (functionp 'org-is-habit-p)
6223 (org-is-habit-p))))
6224 (setq txt nil)
6225 (setq habitp (if did-habit-check-p habitp
6226 (and (functionp 'org-is-habit-p)
6227 (org-is-habit-p))))
6228 (setq category (org-get-category)
6229 category-pos (get-text-property (point) 'org-category-position))
6230 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6231 'repeated-after-deadline)
6232 (org-get-deadline-time (point))
6233 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6234 (throw :skip nil))
6235 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6236 (throw :skip nil)
6237 (goto-char (match-end 0))
6238 (setq pos1 (match-beginning 0))
6239 (if habitp
6240 (if (or (not org-habit-show-habits)
6241 (and (not todayp)
6242 (boundp 'org-habit-show-habits-only-for-today)
6243 org-habit-show-habits-only-for-today))
6244 (throw :skip nil))
6245 (if (and
6246 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6247 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6248 pastschedp))
6249 (setq mm (assoc pos1 deadline-position-alist)))
6250 (throw :skip nil)))
6251 (setq inherited-tags
6252 (or (eq org-agenda-show-inherited-tags 'always)
6253 (and (listp org-agenda-show-inherited-tags)
6254 (memq 'agenda org-agenda-show-inherited-tags))
6255 (and (eq org-agenda-show-inherited-tags t)
6256 (or (eq org-agenda-use-tag-inheritance t)
6257 (memq 'agenda org-agenda-use-tag-inheritance))))
6259 tags (org-get-tags-at nil (not inherited-tags)))
6260 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6261 (setq head (buffer-substring
6262 (point)
6263 (progn (skip-chars-forward "^\r\n") (point))))
6264 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6265 (setq timestr
6266 (concat (substring s (match-beginning 1)) " "))
6267 (setq timestr 'time))
6268 (setq txt (org-agenda-format-item
6269 (if (= diff 0)
6270 (car org-agenda-scheduled-leaders)
6271 (format (nth 1 org-agenda-scheduled-leaders)
6272 (- 1 diff)))
6273 head level category tags
6274 (if (not (= diff 0)) nil timestr)
6275 nil habitp))))
6276 (when txt
6277 (setq face
6278 (cond
6279 ((and (not habitp) pastschedp)
6280 'org-scheduled-previously)
6281 (todayp 'org-scheduled-today)
6282 (t 'org-scheduled))
6283 habitp (and habitp (org-habit-parse-todo)))
6284 (org-add-props txt props
6285 'undone-face face
6286 'face (if donep 'org-agenda-done face)
6287 'org-marker (org-agenda-new-marker pos)
6288 'org-hd-marker (org-agenda-new-marker pos1)
6289 'type (if pastschedp "past-scheduled" "scheduled")
6290 'date (if pastschedp d2 date)
6291 'ts-date d2
6292 'warntime warntime
6293 'level level
6294 'priority (if habitp
6295 (org-habit-get-priority habitp)
6296 (+ 94 (- 5 diff) (org-get-priority txt)))
6297 'org-category category
6298 'category-position category-pos
6299 'org-habit-p habitp
6300 'todo-state todo-state)
6301 (push txt ee))))))
6302 (nreverse ee)))
6304 (defun org-agenda-get-blocks ()
6305 "Return the date-range information for agenda display."
6306 (let* ((props (list 'face nil
6307 'org-not-done-regexp org-not-done-regexp
6308 'org-todo-regexp org-todo-regexp
6309 'org-complex-heading-regexp org-complex-heading-regexp
6310 'mouse-face 'highlight
6311 'help-echo
6312 (format "mouse-2 or RET jump to org file %s"
6313 (abbreviate-file-name buffer-file-name))))
6314 (regexp org-tr-regexp)
6315 (d0 (calendar-absolute-from-gregorian date))
6316 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6317 level todo-state tags pos head donep inherited-tags)
6318 (goto-char (point-min))
6319 (while (re-search-forward regexp nil t)
6320 (catch :skip
6321 (org-agenda-skip)
6322 (setq pos (point))
6323 (let ((start-time (match-string 1))
6324 (end-time (match-string 2)))
6325 (setq s1 (match-string 1)
6326 s2 (match-string 2)
6327 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6328 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6329 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6330 ;; Only allow days between the limits, because the normal
6331 ;; date stamps will catch the limits.
6332 (save-excursion
6333 (setq todo-state (org-get-todo-state))
6334 (setq donep (member todo-state org-done-keywords))
6335 (if (and donep org-agenda-skip-timestamp-if-done)
6336 (throw :skip t))
6337 (setq marker (org-agenda-new-marker (point)))
6338 (setq category (org-get-category)
6339 category-pos (get-text-property (point) 'org-category-position))
6340 (if (not (re-search-backward org-outline-regexp-bol nil t))
6341 (throw :skip nil)
6342 (goto-char (match-beginning 0))
6343 (setq hdmarker (org-agenda-new-marker (point))
6344 inherited-tags
6345 (or (eq org-agenda-show-inherited-tags 'always)
6346 (and (listp org-agenda-show-inherited-tags)
6347 (memq 'agenda org-agenda-show-inherited-tags))
6348 (and (eq org-agenda-show-inherited-tags t)
6349 (or (eq org-agenda-use-tag-inheritance t)
6350 (memq 'agenda org-agenda-use-tag-inheritance))))
6352 tags (org-get-tags-at nil (not inherited-tags)))
6353 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6354 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6355 (setq head (match-string 1))
6356 (let ((remove-re
6357 (if org-agenda-remove-timeranges-from-blocks
6358 (concat
6359 "<" (regexp-quote s1) ".*?>"
6360 "--"
6361 "<" (regexp-quote s2) ".*?>")
6362 nil)))
6363 (setq txt (org-agenda-format-item
6364 (format
6365 (nth (if (= d1 d2) 0 1)
6366 org-agenda-timerange-leaders)
6367 (1+ (- d0 d1)) (1+ (- d2 d1)))
6368 head level category tags
6369 (cond ((and (= d1 d0) (= d2 d0))
6370 (concat "<" start-time ">--<" end-time ">"))
6371 ((= d1 d0)
6372 (concat "<" start-time ">"))
6373 ((= d2 d0)
6374 (concat "<" end-time ">")))
6375 remove-re))))
6376 (org-add-props txt props
6377 'org-marker marker 'org-hd-marker hdmarker
6378 'type "block" 'date date
6379 'level level
6380 'todo-state todo-state
6381 'priority (org-get-priority txt) 'org-category category
6382 'org-category-position category-pos)
6383 (push txt ee))))
6384 (goto-char pos)))
6385 ;; Sort the entries by expiration date.
6386 (nreverse ee)))
6388 ;;; Agenda presentation and sorting
6390 (defvar org-prefix-has-time nil
6391 "A flag, set by `org-compile-prefix-format'.
6392 The flag is set if the currently compiled format contains a `%t'.")
6393 (defvar org-prefix-has-tag nil
6394 "A flag, set by `org-compile-prefix-format'.
6395 The flag is set if the currently compiled format contains a `%T'.")
6396 (defvar org-prefix-has-effort nil
6397 "A flag, set by `org-compile-prefix-format'.
6398 The flag is set if the currently compiled format contains a `%e'.")
6399 (defvar org-prefix-has-breadcrumbs nil
6400 "A flag, set by `org-compile-prefix-format'.
6401 The flag is set if the currently compiled format contains a `%b'.")
6402 (defvar org-prefix-category-length nil
6403 "Used by `org-compile-prefix-format' to remember the category field width.")
6404 (defvar org-prefix-category-max-length nil
6405 "Used by `org-compile-prefix-format' to remember the category field width.")
6407 (defun org-agenda-get-category-icon (category)
6408 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6409 (dolist (entry org-agenda-category-icon-alist)
6410 (when (org-string-match-p (car entry) category)
6411 (if (listp (cadr entry))
6412 (return (cadr entry))
6413 (return (apply 'create-image (cdr entry)))))))
6415 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6416 remove-re habitp)
6417 "Format TXT to be inserted into the agenda buffer.
6418 In particular, add the prefix and corresponding text properties.
6420 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6421 LEVEL may be a string to replace the `%l' specifier.
6422 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6423 category taken from local variable or file name. It will replace the `%c'
6424 specifier in the format.
6425 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6426 TXT for sorting of this entry, and for the `%t' specifier in the format.
6427 When DOTIME is a string, this string is searched for a time before TXT is.
6428 TAGS can be the tags of the headline.
6429 Any match of REMOVE-RE will be removed from TXT."
6430 ;; We keep the org-prefix-* variable values along with a compiled
6431 ;; formatter, so that multiple agendas existing at the same time do
6432 ;; not step on each other toes.
6434 ;; It was inconvenient to make these variables buffer local in
6435 ;; Agenda buffers, because this function expects to be called with
6436 ;; the buffer where item comes from being current, and not agenda
6437 ;; buffer
6438 (let* ((bindings (car org-prefix-format-compiled))
6439 (formatter (cadr org-prefix-format-compiled)))
6440 (loop for (var value) in bindings
6441 do (set var value))
6442 (save-match-data
6443 ;; Diary entries sometimes have extra whitespace at the beginning
6444 (setq txt (org-trim txt))
6446 ;; Fix the tags part in txt
6447 (setq txt (org-agenda-fix-displayed-tags
6448 txt tags
6449 org-agenda-show-inherited-tags
6450 org-agenda-hide-tags-regexp))
6452 (let* ((category (or category
6453 (if (stringp org-category)
6454 org-category
6455 (and org-category (symbol-name org-category)))
6456 (if buffer-file-name
6457 (file-name-sans-extension
6458 (file-name-nondirectory buffer-file-name))
6459 "")))
6460 (category-icon (org-agenda-get-category-icon category))
6461 (category-icon (if category-icon
6462 (propertize " " 'display category-icon)
6463 ""))
6464 ;; time, tag, effort are needed for the eval of the prefix format
6465 (tag (if tags (nth (1- (length tags)) tags) ""))
6466 time effort neffort
6467 (ts (if dotime (concat
6468 (if (stringp dotime) dotime "")
6469 (and org-agenda-search-headline-for-time txt))))
6470 (time-of-day (and dotime (org-get-time-of-day ts)))
6471 stamp plain s0 s1 s2 rtn srp l
6472 duration thecategory breadcrumbs)
6473 (and (derived-mode-p 'org-mode) buffer-file-name
6474 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6475 (when (and dotime time-of-day)
6476 ;; Extract starting and ending time and move them to prefix
6477 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6478 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6479 (setq s0 (match-string 0 ts)
6480 srp (and stamp (match-end 3))
6481 s1 (match-string (if plain 1 2) ts)
6482 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6484 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6485 ;; them, we might want to remove them there to avoid duplication.
6486 ;; The user can turn this off with a variable.
6487 (if (and org-prefix-has-time
6488 org-agenda-remove-times-when-in-prefix (or stamp plain)
6489 (string-match (concat (regexp-quote s0) " *") txt)
6490 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6491 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6492 (= (match-beginning 0) 0)
6494 (setq txt (replace-match "" nil nil txt))))
6495 ;; Normalize the time(s) to 24 hour
6496 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6497 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6499 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6500 (let (org-time-clocksum-use-effort-durations)
6501 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6502 (setq s2
6503 (org-minutes-to-clocksum-string
6504 (+ (org-hh:mm-string-to-minutes s1)
6505 org-agenda-default-appointment-duration)))))
6507 ;; Compute the duration
6508 (when s2
6509 (setq duration (- (org-hh:mm-string-to-minutes s2)
6510 (org-hh:mm-string-to-minutes s1)))))
6512 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6513 txt)
6514 ;; Tags are in the string
6515 (if (or (eq org-agenda-remove-tags t)
6516 (and org-agenda-remove-tags
6517 org-prefix-has-tag))
6518 (setq txt (replace-match "" t t txt))
6519 (setq txt (replace-match
6520 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6521 (match-string 2 txt))
6522 t t txt))))
6523 (when (derived-mode-p 'org-mode)
6524 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6526 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6527 ;; current buffer, so move this check outside of above
6528 (if effort
6529 (setq neffort (org-duration-string-to-minutes effort)
6530 effort (setq effort (concat "[" effort "]")))
6531 ;; prevent erroring out with %e format when there is no effort
6532 (setq effort ""))
6534 (when remove-re
6535 (while (string-match remove-re txt)
6536 (setq txt (replace-match "" t t txt))))
6538 ;; Set org-heading property on `txt' to mark the start of the
6539 ;; heading.
6540 (add-text-properties 0 (length txt) '(org-heading t) txt)
6542 ;; Prepare the variables needed in the eval of the compiled format
6543 (if org-prefix-has-breadcrumbs
6544 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6545 (let ((s (org-display-outline-path nil nil "->" t)))
6546 (if (eq "" s) "" (concat s "->"))))))
6547 (setq time (cond (s2 (concat
6548 (org-agenda-time-of-day-to-ampm-maybe s1)
6549 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6550 (if org-agenda-timegrid-use-ampm " ")))
6551 (s1 (concat
6552 (org-agenda-time-of-day-to-ampm-maybe s1)
6553 (if org-agenda-timegrid-use-ampm
6554 "........ "
6555 "......")))
6556 (t ""))
6557 extra (or (and (not habitp) extra) "")
6558 category (if (symbolp category) (symbol-name category) category)
6559 thecategory (copy-sequence category)
6560 level (or level ""))
6561 (if (string-match org-bracket-link-regexp category)
6562 (progn
6563 (setq l (if (match-end 3)
6564 (- (match-end 3) (match-beginning 3))
6565 (- (match-end 1) (match-beginning 1))))
6566 (when (< l (or org-prefix-category-length 0))
6567 (setq category (copy-sequence category))
6568 (org-add-props category nil
6569 'extra-space (make-string
6570 (- org-prefix-category-length l 1) ?\ ))))
6571 (if (and org-prefix-category-max-length
6572 (>= (length category) org-prefix-category-max-length))
6573 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6574 ;; Evaluate the compiled format
6575 (setq rtn (concat (eval formatter) txt))
6577 ;; And finally add the text properties
6578 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6579 (org-add-props rtn nil
6580 'org-category (if thecategory (downcase thecategory) category)
6581 'tags (mapcar 'org-downcase-keep-props tags)
6582 'org-highest-priority org-highest-priority
6583 'org-lowest-priority org-lowest-priority
6584 'time-of-day time-of-day
6585 'duration duration
6586 'effort effort
6587 'effort-minutes neffort
6588 'breadcrumbs breadcrumbs
6589 'txt txt
6590 'level level
6591 'time time
6592 'extra extra
6593 'format org-prefix-format-compiled
6594 'dotime dotime)))))
6596 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6597 "Remove tags string from TXT, and add a modified list of tags.
6598 The modified list may contain inherited tags, and tags matched by
6599 `org-agenda-hide-tags-regexp' will be removed."
6600 (when (or add-inherited hide-re)
6601 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6602 (setq txt (substring txt 0 (match-beginning 0))))
6603 (setq tags
6604 (delq nil
6605 (mapcar (lambda (tg)
6606 (if (or (and hide-re (string-match hide-re tg))
6607 (and (not add-inherited)
6608 (get-text-property 0 'inherited tg)))
6610 tg))
6611 tags)))
6612 (when tags
6613 (let ((have-i (get-text-property 0 'inherited (car tags)))
6615 (setq txt (concat txt " :"
6616 (mapconcat
6617 (lambda (x)
6618 (setq i (get-text-property 0 'inherited x))
6619 (if (and have-i (not i))
6620 (progn
6621 (setq have-i nil)
6622 (concat ":" x))
6624 tags ":")
6625 (if have-i "::" ":"))))))
6626 txt)
6628 (defun org-downcase-keep-props (s)
6629 (let ((props (text-properties-at 0 s)))
6630 (setq s (downcase s))
6631 (add-text-properties 0 (length s) props s)
6634 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6636 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6637 "Add a time-grid for agenda items which need it.
6639 LIST is the list of agenda items formatted by `org-agenda-list'.
6640 NDAYS is the span of the current agenda view.
6641 TODAYP is `t' when the current agenda view is on today."
6642 (catch 'exit
6643 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6644 ((and todayp (member 'today (car org-agenda-time-grid))))
6645 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6646 ((member 'weekly (car org-agenda-time-grid)))
6647 (t (throw 'exit list)))
6648 (let* ((have (delq nil (mapcar
6649 (lambda (x) (get-text-property 1 'time-of-day x))
6650 list)))
6651 (string (nth 1 org-agenda-time-grid))
6652 (gridtimes (nth 2 org-agenda-time-grid))
6653 (req (car org-agenda-time-grid))
6654 (remove (member 'remove-match req))
6655 new time)
6656 (if (and (member 'require-timed req) (not have))
6657 ;; don't show empty grid
6658 (throw 'exit list))
6659 (while (setq time (pop gridtimes))
6660 (unless (and remove (member time have))
6661 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6662 (push (org-agenda-format-item
6663 nil string nil "" nil
6664 (concat (substring time 0 -2) ":" (substring time -2)))
6665 new)
6666 (put-text-property
6667 2 (length (car new)) 'face 'org-time-grid (car new))))
6668 (when (and todayp org-agenda-show-current-time-in-grid)
6669 (push (org-agenda-format-item
6670 nil org-agenda-current-time-string nil "" nil
6671 (format-time-string "%H:%M "))
6672 new)
6673 (put-text-property
6674 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6676 (if (member 'time-up org-agenda-sorting-strategy-selected)
6677 (append new list)
6678 (append list new)))))
6680 (defun org-compile-prefix-format (key)
6681 "Compile the prefix format into a Lisp form that can be evaluated.
6682 The resulting form and associated variable bindings is returned
6683 and stored in the variable `org-prefix-format-compiled'."
6684 (setq org-prefix-has-time nil
6685 org-prefix-has-tag nil
6686 org-prefix-category-length nil
6687 org-prefix-has-effort nil
6688 org-prefix-has-breadcrumbs nil)
6689 (let ((s (cond
6690 ((stringp org-agenda-prefix-format)
6691 org-agenda-prefix-format)
6692 ((assq key org-agenda-prefix-format)
6693 (cdr (assq key org-agenda-prefix-format)))
6694 (t " %-12:c%?-12t% s")))
6695 (start 0)
6696 varform vars var e c f opt)
6697 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6698 s start)
6699 (setq var (or (cdr (assoc (match-string 4 s)
6700 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6701 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6702 'eval)
6703 c (or (match-string 3 s) "")
6704 opt (match-beginning 1)
6705 start (1+ (match-beginning 0)))
6706 (if (equal var 'time) (setq org-prefix-has-time t))
6707 (if (equal var 'tag) (setq org-prefix-has-tag t))
6708 (if (equal var 'effort) (setq org-prefix-has-effort t))
6709 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6710 (setq f (concat "%" (match-string 2 s) "s"))
6711 (when (equal var 'category)
6712 (setq org-prefix-category-length
6713 (floor (abs (string-to-number (match-string 2 s)))))
6714 (setq org-prefix-category-max-length
6715 (let ((x (match-string 2 s)))
6716 (save-match-data
6717 (if (string-match "\\.[0-9]+" x)
6718 (string-to-number (substring (match-string 0 x) 1)))))))
6719 (if (eq var 'eval)
6720 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6721 (if opt
6722 (setq varform
6723 `(if (equal "" ,var)
6725 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6726 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6727 (setq s (replace-match "%s" t nil s))
6728 (push varform vars))
6729 (setq vars (nreverse vars))
6730 (with-current-buffer (or org-agenda-buffer (current-buffer))
6731 (setq org-prefix-format-compiled
6732 (list
6733 `((org-prefix-has-time ,org-prefix-has-time)
6734 (org-prefix-has-tag ,org-prefix-has-tag)
6735 (org-prefix-category-length ,org-prefix-category-length)
6736 (org-prefix-has-effort ,org-prefix-has-effort)
6737 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6738 `(format ,s ,@vars))))))
6740 (defun org-set-sorting-strategy (key)
6741 (if (symbolp (car org-agenda-sorting-strategy))
6742 ;; the old format
6743 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6744 (setq org-agenda-sorting-strategy-selected
6745 (or (cdr (assq key org-agenda-sorting-strategy))
6746 (cdr (assq 'agenda org-agenda-sorting-strategy))
6747 '(time-up category-keep priority-down)))))
6749 (defun org-get-time-of-day (s &optional string mod24)
6750 "Check string S for a time of day.
6751 If found, return it as a military time number between 0 and 2400.
6752 If not found, return nil.
6753 The optional STRING argument forces conversion into a 5 character wide string
6754 HH:MM."
6755 (save-match-data
6756 (when
6757 (and
6758 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6759 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6760 (not (eq (get-text-property 1 'face s) 'org-link)))
6761 (let* ((h (string-to-number (match-string 1 s)))
6762 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6763 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6764 (am-p (equal ampm "am"))
6765 (h1 (cond ((not ampm) h)
6766 ((= h 12) (if am-p 0 12))
6767 (t (+ h (if am-p 0 12)))))
6768 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6769 (mod h1 24) h1))
6770 (t0 (+ (* 100 h2) m))
6771 (t1 (concat (if (>= h1 24) "+" " ")
6772 (if (and org-agenda-time-leading-zero
6773 (< t0 1000)) "0" "")
6774 (if (< t0 100) "0" "")
6775 (if (< t0 10) "0" "")
6776 (int-to-string t0))))
6777 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6779 (defvar org-agenda-before-sorting-filter-function nil
6780 "Function to be applied to agenda items prior to sorting.
6781 Prior to sorting also means just before they are inserted into the agenda.
6783 To aid sorting, you may revisit the original entries and add more text
6784 properties which will later be used by the sorting functions.
6786 The function should take a string argument, an agenda line.
6787 It has access to the text properties in that line, which contain among
6788 other things, the property `org-hd-marker' that points to the entry
6789 where the line comes from. Note that not all lines going into the agenda
6790 have this property, only most.
6792 The function should return the modified string. It is probably best
6793 to ONLY change text properties.
6795 You can also use this function as a filter, by returning nil for lines
6796 you don't want to have in the agenda at all. For this application, you
6797 could bind the variable in the options section of a custom command.")
6799 (defun org-agenda-finalize-entries (list &optional type)
6800 "Sort, limit and concatenate the LIST of agenda items.
6801 The optional argument TYPE tells the agenda type."
6802 (let ((max-effort (cond ((listp org-agenda-max-effort)
6803 (cdr (assoc type org-agenda-max-effort)))
6804 (t org-agenda-max-effort)))
6805 (max-todo (cond ((listp org-agenda-max-todos)
6806 (cdr (assoc type org-agenda-max-todos)))
6807 (t org-agenda-max-todos)))
6808 (max-tags (cond ((listp org-agenda-max-tags)
6809 (cdr (assoc type org-agenda-max-tags)))
6810 (t org-agenda-max-tags)))
6811 (max-entries (cond ((listp org-agenda-max-entries)
6812 (cdr (assoc type org-agenda-max-entries)))
6813 (t org-agenda-max-entries))) l)
6814 (when org-agenda-before-sorting-filter-function
6815 (setq list
6816 (delq nil
6817 (mapcar
6818 org-agenda-before-sorting-filter-function list))))
6819 (setq list (mapcar 'org-agenda-highlight-todo list)
6820 list (mapcar 'identity (sort list 'org-entries-lessp)))
6821 (when max-effort
6822 (setq list (org-agenda-limit-entries
6823 list 'effort-minutes max-effort 'identity)))
6824 (when max-todo
6825 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6826 (when max-tags
6827 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6828 (when max-entries
6829 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6830 (mapconcat 'identity list "\n")))
6832 (defun org-agenda-limit-entries (list prop limit &optional fn)
6833 "Limit the number of agenda entries."
6834 (let ((include (and limit (< limit 0))))
6835 (if limit
6836 (let ((fun (or fn (lambda (p) (if p 1))))
6837 (lim 0))
6838 (delq nil
6839 (mapcar
6840 (lambda (e)
6841 (let ((pval (funcall fun (get-text-property 1 prop e))))
6842 (if pval (setq lim (+ lim pval)))
6843 (cond ((and pval (<= lim (abs limit))) e)
6844 ((and include (not pval)) e))))
6845 list)))
6846 list)))
6848 (defun org-agenda-limit-interactively ()
6849 "In agenda, interactively limit entries to various maximums."
6850 (interactive)
6851 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6852 (num (string-to-number (read-from-minibuffer "How many? "))))
6853 (cond ((equal max ?e)
6854 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6855 ((equal max ?t)
6856 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6857 ((equal max ?T)
6858 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6859 ((equal max ?E)
6860 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6861 (org-agenda-fit-window-to-buffer))
6863 (defun org-agenda-highlight-todo (x)
6864 (let ((org-done-keywords org-done-keywords-for-agenda)
6865 (case-fold-search nil)
6867 (if (eq x 'line)
6868 (save-excursion
6869 (beginning-of-line 1)
6870 (setq re (org-get-at-bol 'org-todo-regexp))
6871 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6872 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6873 (add-text-properties (match-beginning 0) (match-end 1)
6874 (list 'face (org-get-todo-face 1)))
6875 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6876 (delete-region (match-beginning 1) (1- (match-end 0)))
6877 (goto-char (match-beginning 1))
6878 (insert (format org-agenda-todo-keyword-format s)))))
6879 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6880 (setq re (get-text-property 0 'org-todo-regexp x))
6881 (when (and re
6882 ;; Test `pl' because if there's no heading content,
6883 ;; there's no point matching to highlight. Note
6884 ;; that if we didn't test `pl' first, and there
6885 ;; happened to be no keyword from `org-todo-regexp'
6886 ;; on this heading line, then the `equal' comparison
6887 ;; afterwards would spuriously succeed in the case
6888 ;; where `pl' is nil -- causing an args-out-of-range
6889 ;; error when we try to add text properties to text
6890 ;; that isn't there.
6892 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6893 x pl) pl))
6894 (add-text-properties
6895 (or (match-end 1) (match-end 0)) (match-end 0)
6896 (list 'face (org-get-todo-face (match-string 2 x)))
6898 (when (match-end 1)
6899 (setq x (concat (substring x 0 (match-end 1))
6900 (format org-agenda-todo-keyword-format
6901 (match-string 2 x))
6902 (org-add-props " " (text-properties-at 0 x))
6903 (substring x (match-end 3)))))))
6904 x)))
6906 (defsubst org-cmp-priority (a b)
6907 "Compare the priorities of string A and B."
6908 (let ((pa (or (get-text-property 1 'priority a) 0))
6909 (pb (or (get-text-property 1 'priority b) 0)))
6910 (cond ((> pa pb) +1)
6911 ((< pa pb) -1))))
6913 (defsubst org-cmp-effort (a b)
6914 "Compare the effort values of string A and B."
6915 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6916 (ea (or (get-text-property 1 'effort-minutes a) def))
6917 (eb (or (get-text-property 1 'effort-minutes b) def)))
6918 (cond ((> ea eb) +1)
6919 ((< ea eb) -1))))
6921 (defsubst org-cmp-category (a b)
6922 "Compare the string values of categories of strings A and B."
6923 (let ((ca (or (get-text-property 1 'org-category a) ""))
6924 (cb (or (get-text-property 1 'org-category b) "")))
6925 (cond ((string-lessp ca cb) -1)
6926 ((string-lessp cb ca) +1))))
6928 (defsubst org-cmp-todo-state (a b)
6929 "Compare the todo states of strings A and B."
6930 (let* ((ma (or (get-text-property 1 'org-marker a)
6931 (get-text-property 1 'org-hd-marker a)))
6932 (mb (or (get-text-property 1 'org-marker b)
6933 (get-text-property 1 'org-hd-marker b)))
6934 (fa (and ma (marker-buffer ma)))
6935 (fb (and mb (marker-buffer mb)))
6936 (todo-kwds
6937 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6938 (and fb (with-current-buffer fb org-todo-keywords-1))))
6939 (ta (or (get-text-property 1 'todo-state a) ""))
6940 (tb (or (get-text-property 1 'todo-state b) ""))
6941 (la (- (length (member ta todo-kwds))))
6942 (lb (- (length (member tb todo-kwds))))
6943 (donepa (member ta org-done-keywords-for-agenda))
6944 (donepb (member tb org-done-keywords-for-agenda)))
6945 (cond ((and donepa (not donepb)) -1)
6946 ((and (not donepa) donepb) +1)
6947 ((< la lb) -1)
6948 ((< lb la) +1))))
6950 (defsubst org-cmp-alpha (a b)
6951 "Compare the headlines, alphabetically."
6952 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6953 (plb (text-property-any 0 (length b) 'org-heading t b))
6954 (ta (and pla (substring a pla)))
6955 (tb (and plb (substring b plb))))
6956 (when pla
6957 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6958 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6959 (setq ta (substring ta (match-end 0))))
6960 (setq ta (downcase ta)))
6961 (when plb
6962 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6963 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6964 (setq tb (substring tb (match-end 0))))
6965 (setq tb (downcase tb)))
6966 (cond ((not ta) +1)
6967 ((not tb) -1)
6968 ((string-lessp ta tb) -1)
6969 ((string-lessp tb ta) +1))))
6971 (defsubst org-cmp-tag (a b)
6972 "Compare the string values of the first tags of A and B."
6973 (let ((ta (car (last (get-text-property 1 'tags a))))
6974 (tb (car (last (get-text-property 1 'tags b)))))
6975 (cond ((not ta) +1)
6976 ((not tb) -1)
6977 ((string-lessp ta tb) -1)
6978 ((string-lessp tb ta) +1))))
6980 (defsubst org-cmp-time (a b)
6981 "Compare the time-of-day values of strings A and B."
6982 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6983 (ta (or (get-text-property 1 'time-of-day a) def))
6984 (tb (or (get-text-property 1 'time-of-day b) def)))
6985 (cond ((< ta tb) -1)
6986 ((< tb ta) +1))))
6988 (defsubst org-cmp-ts (a b type)
6989 "Compare the timestamps values of entries A and B.
6990 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
6991 \"timestamp_ia\", compare within each of these type. When TYPE
6992 is the empty string, compare all timestamps without respect of
6993 their type."
6994 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
6995 (ta (or (and (string-match type (or (get-text-property 1 type a) ""))
6996 (get-text-property 1 'ts-date a)) def))
6997 (tb (or (and (string-match type (or (get-text-property 1 type b) ""))
6998 (get-text-property 1 'ts-date b)) def)))
6999 (cond ((< ta tb) -1)
7000 ((< tb ta) +1))))
7002 (defsubst org-cmp-habit-p (a b)
7003 "Compare the todo states of strings A and B."
7004 (let ((ha (get-text-property 1 'org-habit-p a))
7005 (hb (get-text-property 1 'org-habit-p b)))
7006 (cond ((and ha (not hb)) -1)
7007 ((and (not ha) hb) +1))))
7009 (defun org-entries-lessp (a b)
7010 "Predicate for sorting agenda entries."
7011 ;; The following variables will be used when the form is evaluated.
7012 ;; So even though the compiler complains, keep them.
7013 (let* ((ss org-agenda-sorting-strategy-selected)
7014 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7015 (org-cmp-ts a b "")))
7016 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7017 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7018 (org-cmp-ts a b "scheduled")))
7019 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7020 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7021 (org-cmp-ts a b "deadline")))
7022 (deadline-down (if deadline-up (- deadline-up) nil))
7023 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7024 (org-cmp-ts a b "iatimestamp_ia")))
7025 (tsia-down (if tsia-up (- tsia-up) nil))
7026 (ts-up (and (org-em 'ts-up 'ts-down ss)
7027 (org-cmp-ts a b "timestamp")))
7028 (ts-down (if ts-up (- ts-up) nil))
7029 (time-up (and (org-em 'time-up 'time-down ss)
7030 (org-cmp-time a b)))
7031 (time-down (if time-up (- time-up) nil))
7032 (priority-up (and (org-em 'priority-up 'priority-down ss)
7033 (org-cmp-priority a b)))
7034 (priority-down (if priority-up (- priority-up) nil))
7035 (effort-up (and (org-em 'effort-up 'effort-down ss)
7036 (org-cmp-effort a b)))
7037 (effort-down (if effort-up (- effort-up) nil))
7038 (category-up (and (or (org-em 'category-up 'category-down ss)
7039 (memq 'category-keep ss))
7040 (org-cmp-category a b)))
7041 (category-down (if category-up (- category-up) nil))
7042 (category-keep (if category-up +1 nil))
7043 (tag-up (and (org-em 'tag-up 'tag-down ss)
7044 (org-cmp-tag a b)))
7045 (tag-down (if tag-up (- tag-up) nil))
7046 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7047 (org-cmp-todo-state a b)))
7048 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7049 (habit-up (and (org-em 'habit-up 'habit-down ss)
7050 (org-cmp-habit-p a b)))
7051 (habit-down (if habit-up (- habit-up) nil))
7052 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7053 (org-cmp-alpha a b)))
7054 (alpha-down (if alpha-up (- alpha-up) nil))
7055 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7056 user-defined-up user-defined-down)
7057 (if (and need-user-cmp org-agenda-cmp-user-defined
7058 (functionp org-agenda-cmp-user-defined))
7059 (setq user-defined-up
7060 (funcall org-agenda-cmp-user-defined a b)
7061 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7062 (cdr (assoc
7063 (eval (cons 'or org-agenda-sorting-strategy-selected))
7064 '((-1 . t) (1 . nil) (nil . nil))))))
7066 ;;; Agenda restriction lock
7068 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7069 "Overlay to mark the headline to which agenda commands are restricted.")
7070 (overlay-put org-agenda-restriction-lock-overlay
7071 'face 'org-agenda-restriction-lock)
7072 (overlay-put org-agenda-restriction-lock-overlay
7073 'help-echo "Agendas are currently limited to this subtree.")
7074 (org-detach-overlay org-agenda-restriction-lock-overlay)
7076 ;;;###autoload
7077 (defun org-agenda-set-restriction-lock (&optional type)
7078 "Set restriction lock for agenda, to current subtree or file.
7079 Restriction will be the file if TYPE is `file', or if type is the
7080 universal prefix '(4), or if the cursor is before the first headline
7081 in the file. Otherwise, restriction will be to the current subtree."
7082 (interactive "P")
7083 (and (equal type '(4)) (setq type 'file))
7084 (setq type (cond
7085 (type type)
7086 ((org-at-heading-p) 'subtree)
7087 ((condition-case nil (org-back-to-heading t) (error nil))
7088 'subtree)
7089 (t 'file)))
7090 (if (eq type 'subtree)
7091 (progn
7092 (setq org-agenda-restrict (current-buffer))
7093 (setq org-agenda-overriding-restriction 'subtree)
7094 (put 'org-agenda-files 'org-restrict
7095 (list (buffer-file-name (buffer-base-buffer))))
7096 (org-back-to-heading t)
7097 (move-overlay org-agenda-restriction-lock-overlay
7098 (point)
7099 (if org-agenda-restriction-lock-highlight-subtree
7100 (save-excursion (org-end-of-subtree t t) (point))
7101 (point-at-eol)))
7102 (move-marker org-agenda-restrict-begin (point))
7103 (move-marker org-agenda-restrict-end
7104 (save-excursion (org-end-of-subtree t t)))
7105 (message "Locking agenda restriction to subtree"))
7106 (put 'org-agenda-files 'org-restrict
7107 (list (buffer-file-name (buffer-base-buffer))))
7108 (setq org-agenda-restrict nil)
7109 (setq org-agenda-overriding-restriction 'file)
7110 (move-marker org-agenda-restrict-begin nil)
7111 (move-marker org-agenda-restrict-end nil)
7112 (message "Locking agenda restriction to file"))
7113 (setq current-prefix-arg nil)
7114 (org-agenda-maybe-redo))
7116 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7117 "Remove the agenda restriction lock."
7118 (interactive "P")
7119 (org-detach-overlay org-agenda-restriction-lock-overlay)
7120 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7121 (setq org-agenda-overriding-restriction nil)
7122 (setq org-agenda-restrict nil)
7123 (put 'org-agenda-files 'org-restrict nil)
7124 (move-marker org-agenda-restrict-begin nil)
7125 (move-marker org-agenda-restrict-end nil)
7126 (setq current-prefix-arg nil)
7127 (message "Agenda restriction lock removed")
7128 (or noupdate (org-agenda-maybe-redo)))
7130 (defun org-agenda-maybe-redo ()
7131 "If there is any window showing the agenda view, update it."
7132 (let ((w (get-buffer-window org-agenda-buffer-name t))
7133 (w0 (selected-window)))
7134 (when w
7135 (select-window w)
7136 (org-agenda-redo)
7137 (select-window w0)
7138 (if org-agenda-overriding-restriction
7139 (message "Agenda view shifted to new %s restriction"
7140 org-agenda-overriding-restriction)
7141 (message "Agenda restriction lock removed")))))
7143 ;;; Agenda commands
7145 (defun org-agenda-check-type (error &rest types)
7146 "Check if agenda buffer is of allowed type.
7147 If ERROR is non-nil, throw an error, otherwise just return nil.
7148 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7149 (if (not org-agenda-type)
7150 (error "No Org agenda currently displayed")
7151 (if (memq org-agenda-type types)
7153 (if error
7154 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7155 nil))))
7157 (defun org-agenda-Quit ()
7158 "Exit the agenda and kill buffers loaded by `org-agenda'.
7159 Also restore the window configuration."
7160 (interactive)
7161 (if org-agenda-columns-active
7162 (org-columns-quit)
7163 (let ((buf (current-buffer)))
7164 (if (eq org-agenda-window-setup 'other-frame)
7165 (progn
7166 (org-agenda-reset-markers)
7167 (kill-buffer buf)
7168 (org-columns-remove-overlays)
7169 (setq org-agenda-archives-mode nil)
7170 (delete-frame))
7171 (and (not (eq org-agenda-window-setup 'current-window))
7172 (not (one-window-p))
7173 (delete-window))
7174 (org-agenda-reset-markers)
7175 (kill-buffer buf)
7176 (org-columns-remove-overlays)
7177 (setq org-agenda-archives-mode nil)))
7178 (setq org-agenda-buffer nil)
7179 ;; Maybe restore the pre-agenda window configuration.
7180 (and org-agenda-restore-windows-after-quit
7181 (not (eq org-agenda-window-setup 'other-frame))
7182 org-agenda-pre-window-conf
7183 (set-window-configuration org-agenda-pre-window-conf)
7184 (setq org-agenda-pre-window-conf nil))))
7186 (defun org-agenda-quit ()
7187 "Exit the agenda and restore the window configuration.
7188 When `org-agenda-sticky' is non-nil, only bury the agenda."
7189 (interactive)
7190 (if (and (eq org-indirect-buffer-display 'other-window)
7191 org-last-indirect-buffer)
7192 (let ((org-last-indirect-window
7193 (get-buffer-window org-last-indirect-buffer)))
7194 (if org-last-indirect-window
7195 (delete-window org-last-indirect-window))))
7196 (if org-agenda-columns-active
7197 (org-columns-quit)
7198 (if org-agenda-sticky
7199 (let ((buf (current-buffer)))
7200 (if (eq org-agenda-window-setup 'other-frame)
7201 (progn
7202 (delete-frame))
7203 (and (not (eq org-agenda-window-setup 'current-window))
7204 (not (one-window-p))
7205 (delete-window)))
7206 (with-current-buffer buf
7207 (bury-buffer)
7208 ;; Maybe restore the pre-agenda window configuration.
7209 (and org-agenda-restore-windows-after-quit
7210 (not (eq org-agenda-window-setup 'other-frame))
7211 org-agenda-pre-window-conf
7212 (set-window-configuration org-agenda-pre-window-conf)
7213 (setq org-agenda-pre-window-conf nil))))
7214 (org-agenda-Quit))))
7216 (defun org-agenda-exit ()
7217 "Exit the agenda and restore the window configuration.
7218 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7219 buffers visited directly by the user will not be touched."
7220 (interactive)
7221 (org-release-buffers org-agenda-new-buffers)
7222 (setq org-agenda-new-buffers nil)
7223 (org-agenda-Quit))
7225 (defun org-agenda-kill-all-agenda-buffers ()
7226 "Kill all buffers in `org-agenda-mode'.
7227 This is used when toggling sticky agendas.
7228 You can also explicitly invoke it with `C-c a C-k'."
7229 (interactive)
7230 (let (blist)
7231 (dolist (buf (buffer-list))
7232 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7233 (push buf blist)))
7234 (mapc 'kill-buffer blist)))
7236 (defun org-agenda-execute (arg)
7237 "Execute another agenda command, keeping same window.
7238 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7239 in the agenda."
7240 (interactive "P")
7241 (let ((org-agenda-window-setup 'current-window))
7242 (org-agenda arg)))
7244 (defun org-agenda-redo (&optional all)
7245 "Rebuild possibly ALL agenda view(s) in the current buffer."
7246 (interactive "P")
7247 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7248 (cpa (unless (eq all t) current-prefix-arg))
7249 (org-agenda-doing-sticky-redo org-agenda-sticky)
7250 (org-agenda-sticky nil)
7251 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7252 org-agenda-buffer-name))
7253 (org-agenda-keep-modes t)
7254 (tag-filter org-agenda-tag-filter)
7255 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7256 (top-hl-filter org-agenda-top-headline-filter)
7257 (cat-filter org-agenda-category-filter)
7258 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7259 (re-filter org-agenda-regexp-filter)
7260 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7261 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7262 (cols org-agenda-columns-active)
7263 (line (org-current-line))
7264 (window-line (- line (org-current-line (window-start))))
7265 (lprops (get 'org-agenda-redo-command 'org-lprops))
7266 (redo-cmd (get-text-property p 'org-redo-cmd))
7267 (last-args (get-text-property p 'org-last-args))
7268 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7269 (org-agenda-overriding-cmd-arguments
7270 (unless (eq all t)
7271 (cond ((listp last-args)
7272 (cons (or cpa (car last-args)) (cdr last-args)))
7273 ((stringp last-args)
7274 last-args))))
7275 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7276 (put 'org-agenda-tag-filter :preset-filter nil)
7277 (put 'org-agenda-category-filter :preset-filter nil)
7278 (put 'org-agenda-regexp-filter :preset-filter nil)
7279 (and cols (org-columns-quit))
7280 (message "Rebuilding agenda buffer...")
7281 (if series-redo-cmd
7282 (eval series-redo-cmd)
7283 (org-let lprops redo-cmd))
7284 (setq org-agenda-undo-list nil
7285 org-agenda-pending-undo-list nil
7286 org-agenda-tag-filter tag-filter
7287 org-agenda-category-filter cat-filter
7288 org-agenda-regexp-filter re-filter
7289 org-agenda-top-headline-filter top-hl-filter)
7290 (message "Rebuilding agenda buffer...done")
7291 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7292 (put 'org-agenda-category-filter :preset-filter cat-preset)
7293 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7294 (let ((tag (or tag-filter tag-preset))
7295 (cat (or cat-filter cat-preset))
7296 (re (or re-filter re-preset)))
7297 (when tag (org-agenda-filter-apply tag 'tag))
7298 (when cat (org-agenda-filter-apply cat 'category))
7299 (when re (org-agenda-filter-apply re 'regexp)))
7300 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7301 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7302 (org-goto-line line)
7303 (recenter window-line)))
7305 (defvar org-global-tags-completion-table nil)
7306 (defvar org-agenda-filter-form nil)
7307 (defvar org-agenda-filtered-by-category nil)
7309 (defun org-agenda-filter-by-category (strip)
7310 "Keep only those lines in the agenda buffer that have a specific category.
7311 The category is that of the current line."
7312 (interactive "P")
7313 (if (and org-agenda-filtered-by-category
7314 org-agenda-category-filter)
7315 (org-agenda-filter-show-all-cat)
7316 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7317 (cond
7318 ((and cat strip)
7319 (org-agenda-filter-apply
7320 (push (concat "-" cat) org-agenda-category-filter) 'category))
7321 ((and cat)
7322 (org-agenda-filter-apply
7323 (setq org-agenda-category-filter
7324 (list (concat "+" cat))) 'category))
7325 (t (error "No category at point"))))))
7327 (defun org-find-top-headline (&optional pos)
7328 "Find the topmost parent headline and return it."
7329 (save-excursion
7330 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7331 (if pos (goto-char pos))
7332 ;; Skip up to the topmost parent
7333 (while (ignore-errors (outline-up-heading 1) t))
7334 (ignore-errors
7335 (nth 4 (org-heading-components))))))
7337 (defvar org-agenda-filtered-by-top-headline nil)
7338 (defun org-agenda-filter-by-top-headline (strip)
7339 "Keep only those lines that are descendants from the same top headline.
7340 The top headline is that of the current line."
7341 (interactive "P")
7342 (if org-agenda-filtered-by-top-headline
7343 (progn
7344 (setq org-agenda-filtered-by-top-headline nil
7345 org-agenda-top-headline-filter nil)
7346 (org-agenda-filter-show-all-top-filter))
7347 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7348 (if toph (org-agenda-filter-top-headline-apply toph strip)
7349 (error "No top-level headline at point")))))
7351 (defvar org-agenda-regexp-filter nil)
7352 (defun org-agenda-filter-by-regexp (strip)
7353 "Filter agenda entries by a regular expression.
7354 Regexp filters are cumulative.
7355 With no prefix argument, keep entries matching the regexp.
7356 With one prefix argument, filter out entries matching the regexp.
7357 With two prefix arguments, remove the regexp filters."
7358 (interactive "P")
7359 (if (not (equal strip '(16)))
7360 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7361 (read-from-minibuffer
7362 (if (equal strip '(4))
7363 "Filter out entries matching regexp: "
7364 "Narrow to entries matching regexp: ")))))
7365 (push flt org-agenda-regexp-filter)
7366 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7367 (org-agenda-filter-show-all-re)
7368 (message "Regexp filter removed")))
7370 (defun org-agenda-filter-remove-all ()
7371 "Remove all filters from the current agenda buffer."
7372 (interactive)
7373 (when org-agenda-tag-filter
7374 (org-agenda-filter-show-all-tag))
7375 (when org-agenda-category-filter
7376 (org-agenda-filter-show-all-cat))
7377 (when org-agenda-regexp-filter
7378 (org-agenda-filter-show-all-re))
7379 (when org-agenda-top-headline-filter
7380 (org-agenda-filter-show-all-top-filter))
7381 (org-agenda-finalize))
7383 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7384 "Keep only those lines in the agenda buffer that have a specific tag.
7385 The tag is selected with its fast selection letter, as configured.
7386 With prefix argument STRIP, remove all lines that do have the tag.
7387 A lisp caller can specify CHAR. NARROW means that the new tag should be
7388 used to narrow the search - the interactive user can also press `-' or `+'
7389 to switch to narrowing."
7390 (interactive "P")
7391 (let* ((alist org-tag-alist-for-agenda)
7392 (tag-chars (mapconcat
7393 (lambda (x) (if (and (not (symbolp (car x)))
7394 (cdr x))
7395 (char-to-string (cdr x))
7396 ""))
7397 alist ""))
7398 (efforts (org-split-string
7399 (or (cdr (assoc (concat org-effort-property "_ALL")
7400 org-global-properties))
7401 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7402 "")))
7403 (effort-op org-agenda-filter-effort-default-operator)
7404 (effort-prompt "")
7405 (inhibit-read-only t)
7406 (current org-agenda-tag-filter)
7407 maybe-refresh a n tag)
7408 (unless char
7409 (message
7410 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7411 (if narrow "Narrow" "Filter") tag-chars
7412 (if org-agenda-auto-exclude-function "[RET], " ""))
7413 (setq char (read-char-exclusive)))
7414 (when (member char '(?+ ?-))
7415 ;; Narrowing down
7416 (cond ((equal char ?-) (setq strip t narrow t))
7417 ((equal char ?+) (setq strip nil narrow t)))
7418 (message
7419 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7420 (setq char (read-char-exclusive)))
7421 (when (member char '(?< ?> ?= ??))
7422 ;; An effort operator
7423 (setq effort-op (char-to-string char))
7424 (setq alist nil) ; to make sure it will be interpreted as effort.
7425 (unless (equal char ??)
7426 (loop for i from 0 to 9 do
7427 (setq effort-prompt
7428 (concat
7429 effort-prompt " ["
7430 (if (= i 9) "0" (int-to-string (1+ i)))
7431 "]" (nth i efforts))))
7432 (message "Effort%s: %s " effort-op effort-prompt)
7433 (setq char (read-char-exclusive))
7434 (when (or (< char ?0) (> char ?9))
7435 (error "Need 1-9,0 to select effort"))))
7436 (when (equal char ?\t)
7437 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7438 (org-set-local 'org-global-tags-completion-table
7439 (org-global-tags-completion-table)))
7440 (let ((completion-ignore-case t))
7441 (setq tag (org-icompleting-read
7442 "Tag: " org-global-tags-completion-table))))
7443 (cond
7444 ((equal char ?\r)
7445 (org-agenda-filter-show-all-tag)
7446 (when org-agenda-auto-exclude-function
7447 (setq org-agenda-tag-filter nil)
7448 (dolist (tag (org-agenda-get-represented-tags))
7449 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7450 (if modifier
7451 (push modifier org-agenda-tag-filter))))
7452 (if (not (null org-agenda-tag-filter))
7453 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7454 (setq maybe-refresh t))
7455 ((equal char ?/)
7456 (org-agenda-filter-show-all-tag)
7457 (when (get 'org-agenda-tag-filter :preset-filter)
7458 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7459 (setq maybe-refresh t))
7460 ((equal char ?. )
7461 (setq org-agenda-tag-filter
7462 (mapcar (lambda(tag) (concat "+" tag))
7463 (org-get-at-bol 'tags)))
7464 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7465 (setq maybe-refresh t))
7466 ((or (equal char ?\ )
7467 (setq a (rassoc char alist))
7468 (and (>= char ?0) (<= char ?9)
7469 (setq n (if (= char ?0) 9 (- char ?0 1))
7470 tag (concat effort-op (nth n efforts))
7471 a (cons tag nil)))
7472 (and (= char ??)
7473 (setq tag "?eff")
7474 a (cons tag nil))
7475 (and tag (setq a (cons tag nil))))
7476 (org-agenda-filter-show-all-tag)
7477 (setq tag (car a))
7478 (setq org-agenda-tag-filter
7479 (cons (concat (if strip "-" "+") tag)
7480 (if narrow current nil)))
7481 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7482 (setq maybe-refresh t))
7483 (t (error "Invalid tag selection character %c" char)))
7484 (when (and maybe-refresh
7485 (eq org-agenda-clockreport-mode 'with-filter))
7486 (org-agenda-redo))))
7488 (defun org-agenda-get-represented-tags ()
7489 "Get a list of all tags currently represented in the agenda."
7490 (let (p tags)
7491 (save-excursion
7492 (goto-char (point-min))
7493 (while (setq p (next-single-property-change (point) 'tags))
7494 (goto-char p)
7495 (mapc (lambda (x) (add-to-list 'tags x))
7496 (get-text-property (point) 'tags))))
7497 tags))
7499 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7500 "Refine the current filter. See `org-agenda-filter-by-tag'."
7501 (interactive "P")
7502 (org-agenda-filter-by-tag strip char 'refine))
7504 (defun org-agenda-filter-make-matcher (filter type)
7505 "Create the form that tests a line for agenda filter."
7506 (let (f f1)
7507 (cond
7508 ;; Tag filter
7509 ((eq type 'tag)
7510 (setq filter
7511 (delete-dups
7512 (append (get 'org-agenda-tag-filter :preset-filter)
7513 filter)))
7514 (dolist (x filter)
7515 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7516 (ffunc
7517 (lambda (nf0 nf01 fltr notgroup op)
7518 (dolist (x fltr)
7519 (if (member x '("-" "+"))
7520 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7521 (if (string-match "[<=>?]" x)
7522 (setq nf01 (org-agenda-filter-effort-form x))
7523 (setq nf01 (list 'member (downcase (substring x 1))
7524 'tags)))
7525 (when (equal (string-to-char x) ?-)
7526 (setq nf01 (list 'not nf01))
7527 (when (not notgroup) (setq op 'and))))
7528 (push nf01 nf0))
7529 (if notgroup
7530 (push (cons 'and nf0) f)
7531 (push (cons (or op 'or) nf0) f)))))
7532 (cond ((equal filter '("+"))
7533 (setq f (list (list 'not 'tags))))
7534 ((equal nfilter filter)
7535 (funcall ffunc f1 f filter t nil))
7536 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7537 ;; Category filter
7538 ((eq type 'category)
7539 (setq filter
7540 (delete-dups
7541 (append (get 'org-agenda-category-filter :preset-filter)
7542 filter)))
7543 (dolist (x filter)
7544 (if (equal "-" (substring x 0 1))
7545 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7546 (setq f1 (list 'equal (substring x 1) 'cat)))
7547 (push f1 f)))
7548 ;; Regexp filter
7549 ((eq type 'regexp)
7550 (setq filter
7551 (delete-dups
7552 (append (get 'org-agenda-regexp-filter :preset-filter)
7553 filter)))
7554 (dolist (x filter)
7555 (if (equal "-" (substring x 0 1))
7556 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7557 (setq f1 (list 'string-match (substring x 1) 'txt)))
7558 (push f1 f))))
7559 (cons 'and (nreverse f))))
7561 (defun org-agenda-filter-effort-form (e)
7562 "Return the form to compare the effort of the current line with what E says.
7563 E looks like \"+<2:25\"."
7564 (let (op)
7565 (setq e (substring e 1))
7566 (setq op (string-to-char e) e (substring e 1))
7567 (setq op (cond ((equal op ?<) '<=)
7568 ((equal op ?>) '>=)
7569 ((equal op ??) op)
7570 (t '=)))
7571 (list 'org-agenda-compare-effort (list 'quote op)
7572 (org-duration-string-to-minutes e))))
7574 (defun org-agenda-compare-effort (op value)
7575 "Compare the effort of the current line with VALUE, using OP.
7576 If the line does not have an effort defined, return nil."
7577 (let ((eff (org-get-at-bol 'effort-minutes)))
7578 (if (equal op ??)
7579 (not eff)
7580 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7581 value))))
7583 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7584 "Expand group tags in FILTER for the agenda.
7585 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7586 (if org-group-tags
7587 (let ((case-fold-search t) rtn)
7588 (mapc
7589 (lambda (f)
7590 (let (f0 dir)
7591 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7592 (setq dir (match-string 1 f) f0 (match-string 2 f))
7593 (setq dir (if no-operator "" "+") f0 f))
7594 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7595 (org-tags-expand f0 t t))
7596 rtn))))
7597 filter)
7598 (reverse rtn))
7599 filter))
7601 (defun org-agenda-filter-apply (filter type)
7602 "Set FILTER as the new agenda filter and apply it."
7603 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7604 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7605 (let (tags cat txt)
7606 (setq org-agenda-filter-form
7607 (org-agenda-filter-make-matcher filter type))
7608 (if (and (eq type 'category)
7609 (not (equal (substring (car filter) 0 1) "-")))
7610 ;; Only set `org-agenda-filtered-by-category' to t
7611 ;; when a unique category is used as the filter
7612 (setq org-agenda-filtered-by-category t))
7613 (org-agenda-set-mode-name)
7614 (save-excursion
7615 (goto-char (point-min))
7616 (while (not (eobp))
7617 (if (org-get-at-bol 'org-marker)
7618 (progn
7619 (setq tags ; used in eval
7620 (apply 'append
7621 (mapcar (lambda (f)
7622 (org-agenda-filter-expand-tags (list f) t))
7623 (org-get-at-bol 'tags)))
7624 cat (get-text-property (point) 'org-category)
7625 txt (get-text-property (point) 'txt))
7626 (if (not (eval org-agenda-filter-form))
7627 (org-agenda-filter-hide-line type))
7628 (beginning-of-line 2))
7629 (beginning-of-line 2))))
7630 (if (get-char-property (point) 'invisible)
7631 (ignore-errors (org-agenda-previous-line)))))
7633 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7634 "Filter by top headline HL."
7635 (org-agenda-set-mode-name)
7636 (save-excursion
7637 (goto-char (point-min))
7638 (while (not (eobp))
7639 (let* ((pos (org-get-at-bol 'org-hd-marker))
7640 (tophl (and pos (org-find-top-headline pos))))
7641 (if (and tophl (funcall (if negative 'identity 'not)
7642 (string= hl tophl)))
7643 (org-agenda-filter-hide-line 'top-headline)))
7644 (beginning-of-line 2)))
7645 (if (get-char-property (point) 'invisible)
7646 (org-agenda-previous-line))
7647 (setq org-agenda-top-headline-filter hl
7648 org-agenda-filtered-by-top-headline t))
7650 (defun org-agenda-filter-hide-line (type)
7651 "Hide lines with TYPE in the agenda buffer."
7652 (let* ((b (max (point-min) (1- (point-at-bol))))
7653 (e (point-at-eol)))
7654 (let ((inhibit-read-only t))
7655 (add-text-properties
7656 b e `(invisible org-filtered org-filter-type ,type)))))
7658 (defun org-agenda-remove-filter (type)
7659 (interactive)
7660 "Remove filter of type TYPE from the agenda buffer."
7661 (save-excursion
7662 (goto-char (point-min))
7663 (let ((inhibit-read-only t) pos)
7664 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7665 (goto-char pos)
7666 (remove-text-properties
7667 (point) (next-single-property-change (point) 'org-filter-type)
7668 `(invisible org-filtered org-filter-type ,type))))
7669 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7670 (setq org-agenda-filter-form nil)
7671 (org-agenda-set-mode-name)
7672 (org-agenda-finalize)))
7674 (defun org-agenda-filter-show-all-tag nil
7675 (org-agenda-remove-filter 'tag))
7676 (defun org-agenda-filter-show-all-re nil
7677 (org-agenda-remove-filter 'regexp))
7678 (defun org-agenda-filter-show-all-cat nil
7679 (org-agenda-remove-filter 'category))
7680 (defun org-agenda-filter-show-all-top-filter nil
7681 (org-agenda-remove-filter 'top-headline))
7683 (defun org-agenda-manipulate-query-add ()
7684 "Manipulate the query by adding a search term with positive selection.
7685 Positive selection means the term must be matched for selection of an entry."
7686 (interactive)
7687 (org-agenda-manipulate-query ?\[))
7688 (defun org-agenda-manipulate-query-subtract ()
7689 "Manipulate the query by adding a search term with negative selection.
7690 Negative selection means term must not be matched for selection of an entry."
7691 (interactive)
7692 (org-agenda-manipulate-query ?\]))
7693 (defun org-agenda-manipulate-query-add-re ()
7694 "Manipulate the query by adding a search regexp with positive selection.
7695 Positive selection means the regexp must match for selection of an entry."
7696 (interactive)
7697 (org-agenda-manipulate-query ?\{))
7698 (defun org-agenda-manipulate-query-subtract-re ()
7699 "Manipulate the query by adding a search regexp with negative selection.
7700 Negative selection means regexp must not match for selection of an entry."
7701 (interactive)
7702 (org-agenda-manipulate-query ?\}))
7703 (defun org-agenda-manipulate-query (char)
7704 (cond
7705 ((memq org-agenda-type '(timeline agenda))
7706 (let ((org-agenda-include-inactive-timestamps t))
7707 (org-agenda-redo))
7708 (message "Display now includes inactive timestamps as well"))
7709 ((eq org-agenda-type 'search)
7710 (org-add-to-string
7711 'org-agenda-query-string
7712 (if org-agenda-last-search-view-search-was-boolean
7713 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7714 (?\{ . " +{}") (?\} . " -{}"))))
7715 " "))
7716 (setq org-agenda-redo-command
7717 (list 'org-search-view
7718 (car (get-text-property (min (1- (point-max)) (point))
7719 'org-last-args))
7720 org-agenda-query-string
7721 (+ (length org-agenda-query-string)
7722 (if (member char '(?\{ ?\})) 0 1))))
7723 (set-register org-agenda-query-register org-agenda-query-string)
7724 (let ((org-agenda-overriding-arguments
7725 (cdr org-agenda-redo-command)))
7726 (org-agenda-redo)))
7727 (t (error "Cannot manipulate query for %s-type agenda buffers"
7728 org-agenda-type))))
7730 (defun org-add-to-string (var string)
7731 (set var (concat (symbol-value var) string)))
7733 (defun org-agenda-goto-date (span)
7734 "Jump to DATE in agenda."
7735 (interactive "P")
7736 (let* ((org-read-date-prefer-future
7737 (eval org-agenda-jump-prefer-future))
7738 (date (org-read-date))
7739 (day (time-to-days (org-time-string-to-time date)))
7740 (org-agenda-sticky-orig org-agenda-sticky)
7741 (org-agenda-buffer-tmp-name (buffer-name))
7742 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7743 (0-arg (or current-prefix-arg (car args)))
7744 (2-arg (nth 2 args))
7745 (with-hour-p (nth 4 org-agenda-redo-command))
7746 (newcmd (list 'org-agenda-list 0-arg date
7747 (org-agenda-span-to-ndays
7748 2-arg (org-time-string-to-absolute date))
7749 with-hour-p))
7750 (newargs (cdr newcmd))
7751 (inhibit-read-only t)
7752 org-agenda-sticky)
7753 (if (not (org-agenda-check-type t 'agenda))
7754 (error "Not available in non-agenda views")
7755 (add-text-properties (point-min) (point-max)
7756 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7757 (org-agenda-redo)
7758 (goto-char (point-min))
7759 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7760 (save-excursion (move-beginning-of-line 2) (eobp))))
7761 (move-beginning-of-line 2))
7762 (setq org-agenda-sticky org-agenda-sticky-orig
7763 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7765 (defun org-agenda-goto-today ()
7766 "Go to today."
7767 (interactive)
7768 (org-agenda-check-type t 'timeline 'agenda)
7769 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7770 (curspan (nth 2 args))
7771 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7772 (cond
7773 (tdpos (goto-char tdpos))
7774 ((eq org-agenda-type 'agenda)
7775 (let* ((sd (org-agenda-compute-starting-span
7776 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7777 (org-agenda-overriding-arguments args))
7778 (setf (nth 1 org-agenda-overriding-arguments) sd)
7779 (org-agenda-redo)
7780 (org-agenda-find-same-or-today-or-agenda)))
7781 (t (error "Cannot find today")))))
7783 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7784 (goto-char
7785 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7786 (text-property-any (point-min) (point-max) 'org-today t)
7787 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7788 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7789 (org-agenda-goto-block-beginning))
7790 (point-min))))
7792 (defun org-agenda-goto-block-beginning ()
7793 "Go the agenda block beginning."
7794 (interactive)
7795 (if (not (derived-mode-p 'org-agenda-mode))
7796 (error "Cannot execute this command outside of org-agenda-mode buffers")
7797 (let (dest)
7798 (save-excursion
7799 (unless (looking-at "\\'")
7800 (forward-char))
7801 (let* ((prop 'org-agenda-structural-header)
7802 (p (previous-single-property-change (point) prop))
7803 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7804 (1- (point))) prop)))
7805 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7806 (if (not dest)
7807 (error "Cannot find the beginning of the blog")
7808 (goto-char dest)
7809 (move-beginning-of-line 1)))))
7811 (defun org-agenda-later (arg)
7812 "Go forward in time by the current span.
7813 With prefix ARG, go forward that many times the current span."
7814 (interactive "p")
7815 (org-agenda-check-type t 'agenda)
7816 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7817 (span (or (nth 2 args) org-agenda-current-span))
7818 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7819 (greg (calendar-gregorian-from-absolute sd))
7820 (cnt (org-get-at-bol 'org-day-cnt))
7821 greg2)
7822 (cond
7823 ((numberp span)
7824 (setq sd (+ (* span arg) sd)))
7825 ((eq span 'day)
7826 (setq sd (+ arg sd)))
7827 ((eq span 'week)
7828 (setq sd (+ (* 7 arg) sd)))
7829 ((eq span 'fortnight)
7830 (setq sd (+ (* 14 arg) sd)))
7831 ((eq span 'month)
7832 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7833 sd (calendar-absolute-from-gregorian greg2))
7834 (setcar greg2 (1+ (car greg2))))
7835 ((eq span 'year)
7836 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7837 sd (calendar-absolute-from-gregorian greg2))
7838 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7840 (setq sd (+ (* span arg) sd))))
7841 (let ((org-agenda-overriding-cmd
7842 ;; `cmd' may have been set by `org-agenda-run-series' which
7843 ;; uses `org-agenda-overriding-cmd' to decide whether
7844 ;; overriding is allowed for `cmd'
7845 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7846 (org-agenda-overriding-arguments
7847 (list (car args) sd span)))
7848 (org-agenda-redo)
7849 (org-agenda-find-same-or-today-or-agenda cnt))))
7851 (defun org-agenda-earlier (arg)
7852 "Go backward in time by the current span.
7853 With prefix ARG, go backward that many times the current span."
7854 (interactive "p")
7855 (org-agenda-later (- arg)))
7857 (defun org-agenda-view-mode-dispatch ()
7858 "Call one of the view mode commands."
7859 (interactive)
7860 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7861 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7862 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7863 (let ((a (read-char-exclusive)))
7864 (case a
7865 (?\ (call-interactively 'org-agenda-reset-view))
7866 (?d (call-interactively 'org-agenda-day-view))
7867 (?w (call-interactively 'org-agenda-week-view))
7868 (?t (call-interactively 'org-agenda-fortnight-view))
7869 (?m (call-interactively 'org-agenda-month-view))
7870 (?y (call-interactively 'org-agenda-year-view))
7871 (?l (call-interactively 'org-agenda-log-mode))
7872 (?L (org-agenda-log-mode '(4)))
7873 (?c (org-agenda-log-mode 'clockcheck))
7874 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7875 (?a (call-interactively 'org-agenda-archives-mode))
7876 (?A (org-agenda-archives-mode 'files))
7877 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7878 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7879 (?G (call-interactively 'org-agenda-toggle-time-grid))
7880 (?D (call-interactively 'org-agenda-toggle-diary))
7881 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7882 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7883 (org-agenda-check-type t 'timeline 'agenda)
7884 (org-agenda-redo))
7885 (message "Display now includes inactive timestamps as well"))
7886 (?q (message "Abort"))
7887 (otherwise (error "Invalid key" )))))
7889 (defun org-agenda-reset-view ()
7890 "Switch to default view for agenda."
7891 (interactive)
7892 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7893 (defun org-agenda-day-view (&optional day-of-month)
7894 "Switch to daily view for agenda.
7895 With argument DAY-OF-MONTH, switch to that day of the month."
7896 (interactive "P")
7897 (org-agenda-change-time-span 'day day-of-month))
7898 (defun org-agenda-week-view (&optional iso-week)
7899 "Switch to daily view for agenda.
7900 With argument ISO-WEEK, switch to the corresponding ISO week.
7901 If ISO-WEEK has more then 2 digits, only the last two encode the
7902 week. Any digits before this encode a year. So 200712 means
7903 week 12 of year 2007. Years in the range 1938-2037 can also be
7904 written as 2-digit years."
7905 (interactive "P")
7906 (org-agenda-change-time-span 'week iso-week))
7907 (defun org-agenda-fortnight-view (&optional iso-week)
7908 "Switch to daily view for agenda.
7909 With argument ISO-WEEK, switch to the corresponding ISO week.
7910 If ISO-WEEK has more then 2 digits, only the last two encode the
7911 week. Any digits before this encode a year. So 200712 means
7912 week 12 of year 2007. Years in the range 1938-2037 can also be
7913 written as 2-digit years."
7914 (interactive "P")
7915 (org-agenda-change-time-span 'fortnight iso-week))
7916 (defun org-agenda-month-view (&optional month)
7917 "Switch to monthly view for agenda.
7918 With argument MONTH, switch to that month."
7919 (interactive "P")
7920 (org-agenda-change-time-span 'month month))
7921 (defun org-agenda-year-view (&optional year)
7922 "Switch to yearly view for agenda.
7923 With argument YEAR, switch to that year.
7924 If MONTH has more then 2 digits, only the last two encode the
7925 month. Any digits before this encode a year. So 200712 means
7926 December year 2007. Years in the range 1938-2037 can also be
7927 written as 2-digit years."
7928 (interactive "P")
7929 (when year
7930 (setq year (org-small-year-to-year year)))
7931 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7932 (org-agenda-change-time-span 'year year)
7933 (error "Abort")))
7935 (defun org-agenda-change-time-span (span &optional n)
7936 "Change the agenda view to SPAN.
7937 SPAN may be `day', `week', `fortnight', `month', `year'."
7938 (org-agenda-check-type t 'agenda)
7939 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7940 (curspan (nth 2 args)))
7941 (if (and (not n) (equal curspan span))
7942 (error "Viewing span is already \"%s\"" span))
7943 (let* ((sd (or (org-get-at-bol 'day)
7944 (nth 1 args)
7945 org-starting-day))
7946 (sd (org-agenda-compute-starting-span sd span n))
7947 (org-agenda-overriding-cmd
7948 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7949 (org-agenda-overriding-arguments
7950 (list (car args) sd span)))
7951 (org-agenda-redo)
7952 (org-agenda-find-same-or-today-or-agenda))
7953 (org-agenda-set-mode-name)
7954 (message "Switched to %s view" span)))
7956 (defun org-agenda-compute-starting-span (sd span &optional n)
7957 "Compute starting date for agenda.
7958 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7959 is a cons cell with the starting date and the number of days,
7960 so that the date SD will be in that range."
7961 (let* ((greg (calendar-gregorian-from-absolute sd))
7962 (dg (nth 1 greg))
7963 (mg (car greg))
7964 (yg (nth 2 greg)))
7965 (cond
7966 ((eq span 'day)
7967 (when n
7968 (setq sd (+ (calendar-absolute-from-gregorian
7969 (list mg 1 yg))
7970 n -1))))
7971 ((or (eq span 'week) (eq span 'fortnight))
7972 (let* ((nt (calendar-day-of-week
7973 (calendar-gregorian-from-absolute sd)))
7974 (d (if org-agenda-start-on-weekday
7975 (- nt org-agenda-start-on-weekday)
7978 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7979 (when n
7980 (require 'cal-iso)
7981 (when (> n 99)
7982 (setq y1 (org-small-year-to-year (/ n 100))
7983 n (mod n 100)))
7984 (setq sd
7985 (calendar-absolute-from-iso
7986 (list n 1
7987 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7988 ((eq span 'month)
7989 (let (y1)
7990 (when (and n (> n 99))
7991 (setq y1 (org-small-year-to-year (/ n 100))
7992 n (mod n 100)))
7993 (setq sd (calendar-absolute-from-gregorian
7994 (list (or n mg) 1 (or y1 yg))))))
7995 ((eq span 'year)
7996 (setq sd (calendar-absolute-from-gregorian
7997 (list 1 1 (or n yg))))))
7998 sd))
8000 (defun org-agenda-next-date-line (&optional arg)
8001 "Jump to the next line indicating a date in agenda buffer."
8002 (interactive "p")
8003 (org-agenda-check-type t 'agenda 'timeline)
8004 (beginning-of-line 1)
8005 ;; This does not work if user makes date format that starts with a blank
8006 (if (looking-at "^\\S-") (forward-char 1))
8007 (if (not (re-search-forward "^\\S-" nil t arg))
8008 (progn
8009 (backward-char 1)
8010 (error "No next date after this line in this buffer")))
8011 (goto-char (match-beginning 0)))
8013 (defun org-agenda-previous-date-line (&optional arg)
8014 "Jump to the previous line indicating a date in agenda buffer."
8015 (interactive "p")
8016 (org-agenda-check-type t 'agenda 'timeline)
8017 (beginning-of-line 1)
8018 (if (not (re-search-backward "^\\S-" nil t arg))
8019 (error "No previous date before this line in this buffer")))
8021 ;; Initialize the highlight
8022 (defvar org-hl (make-overlay 1 1))
8023 (overlay-put org-hl 'face 'highlight)
8025 (defun org-highlight (begin end &optional buffer)
8026 "Highlight a region with overlay."
8027 (move-overlay org-hl begin end (or buffer (current-buffer))))
8029 (defun org-unhighlight ()
8030 "Detach overlay INDEX."
8031 (org-detach-overlay org-hl))
8033 (defun org-unhighlight-once ()
8034 "Remove the highlight from its position, and this function from the hook."
8035 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8036 (org-unhighlight))
8038 (defvar org-agenda-pre-follow-window-conf nil)
8039 (defun org-agenda-follow-mode ()
8040 "Toggle follow mode in an agenda buffer."
8041 (interactive)
8042 (unless org-agenda-follow-mode
8043 (setq org-agenda-pre-follow-window-conf
8044 (current-window-configuration)))
8045 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8046 (unless org-agenda-follow-mode
8047 (set-window-configuration org-agenda-pre-follow-window-conf))
8048 (org-agenda-set-mode-name)
8049 (org-agenda-do-context-action)
8050 (message "Follow mode is %s"
8051 (if org-agenda-follow-mode "on" "off")))
8053 (defun org-agenda-entry-text-mode (&optional arg)
8054 "Toggle entry text mode in an agenda buffer."
8055 (interactive "P")
8056 (if (or org-agenda-tag-filter
8057 org-agenda-category-filter
8058 org-agenda-regexp-filter
8059 org-agenda-top-headline-filter)
8060 (user-error "Can't show entry text in filtered views")
8061 (setq org-agenda-entry-text-mode (or (integerp arg)
8062 (not org-agenda-entry-text-mode)))
8063 (org-agenda-entry-text-hide)
8064 (and org-agenda-entry-text-mode
8065 (let ((org-agenda-entry-text-maxlines
8066 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8067 (org-agenda-entry-text-show)))
8068 (org-agenda-set-mode-name)
8069 (message "Entry text mode is %s%s"
8070 (if org-agenda-entry-text-mode "on" "off")
8071 (if (not org-agenda-entry-text-mode) ""
8072 (format " (maximum number of lines is %d)"
8073 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8075 (defun org-agenda-clockreport-mode (&optional with-filter)
8076 "Toggle clocktable mode in an agenda buffer.
8077 With prefix arg WITH-FILTER, make the clocktable respect the current
8078 agenda filter."
8079 (interactive "P")
8080 (org-agenda-check-type t 'agenda)
8081 (if with-filter
8082 (setq org-agenda-clockreport-mode 'with-filter)
8083 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8084 (org-agenda-set-mode-name)
8085 (org-agenda-redo)
8086 (message "Clocktable mode is %s"
8087 (if org-agenda-clockreport-mode "on" "off")))
8089 (defun org-agenda-log-mode (&optional special)
8090 "Toggle log mode in an agenda buffer.
8091 With argument SPECIAL, show all possible log items, not only the ones
8092 configured in `org-agenda-log-mode-items'.
8093 With a double `C-u' prefix arg, show *only* log items, nothing else."
8094 (interactive "P")
8095 (org-agenda-check-type t 'agenda 'timeline)
8096 (setq org-agenda-show-log
8097 (cond
8098 ((equal special '(16)) 'only)
8099 ((eq special 'clockcheck)
8100 (if (eq org-agenda-show-log 'clockcheck)
8101 nil 'clockcheck))
8102 (special '(closed clock state))
8103 (t (not org-agenda-show-log))))
8104 (org-agenda-set-mode-name)
8105 (org-agenda-redo)
8106 (message "Log mode is %s"
8107 (if org-agenda-show-log "on" "off")))
8109 (defun org-agenda-archives-mode (&optional with-files)
8110 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8111 When called with a prefix argument, include all archive files as well."
8112 (interactive "P")
8113 (setq org-agenda-archives-mode
8114 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8115 (org-agenda-set-mode-name)
8116 (org-agenda-redo)
8117 (message
8118 "%s"
8119 (cond
8120 ((eq org-agenda-archives-mode nil)
8121 "No archives are included")
8122 ((eq org-agenda-archives-mode 'trees)
8123 (format "Trees with :%s: tag are included" org-archive-tag))
8124 ((eq org-agenda-archives-mode t)
8125 (format "Trees with :%s: tag and all active archive files are included"
8126 org-archive-tag)))))
8128 (defun org-agenda-toggle-diary ()
8129 "Toggle diary inclusion in an agenda buffer."
8130 (interactive)
8131 (org-agenda-check-type t 'agenda)
8132 (setq org-agenda-include-diary (not org-agenda-include-diary))
8133 (org-agenda-redo)
8134 (org-agenda-set-mode-name)
8135 (message "Diary inclusion turned %s"
8136 (if org-agenda-include-diary "on" "off")))
8138 (defun org-agenda-toggle-deadlines ()
8139 "Toggle inclusion of entries with a deadline in an agenda buffer."
8140 (interactive)
8141 (org-agenda-check-type t 'agenda)
8142 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8143 (org-agenda-redo)
8144 (org-agenda-set-mode-name)
8145 (message "Deadlines inclusion turned %s"
8146 (if org-agenda-include-deadlines "on" "off")))
8148 (defun org-agenda-toggle-time-grid ()
8149 "Toggle time grid in an agenda buffer."
8150 (interactive)
8151 (org-agenda-check-type t 'agenda)
8152 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8153 (org-agenda-redo)
8154 (org-agenda-set-mode-name)
8155 (message "Time-grid turned %s"
8156 (if org-agenda-use-time-grid "on" "off")))
8158 (defun org-agenda-set-mode-name ()
8159 "Set the mode name to indicate all the small mode settings."
8160 (setq mode-name
8161 (list "Org-Agenda"
8162 (if (get 'org-agenda-files 'org-restrict) " []" "")
8164 '(:eval (org-agenda-span-name org-agenda-current-span))
8165 (if org-agenda-follow-mode " Follow" "")
8166 (if org-agenda-entry-text-mode " ETxt" "")
8167 (if org-agenda-include-diary " Diary" "")
8168 (if org-agenda-include-deadlines " Ddl" "")
8169 (if org-agenda-use-time-grid " Grid" "")
8170 (if (and (boundp 'org-habit-show-habits)
8171 org-habit-show-habits) " Habit" "")
8172 (cond
8173 ((consp org-agenda-show-log) " LogAll")
8174 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8175 (org-agenda-show-log " Log")
8176 (t ""))
8177 (if (or org-agenda-category-filter
8178 (get 'org-agenda-category-filter :preset-filter))
8179 '(:eval (org-propertize
8180 (concat " <"
8181 (mapconcat
8182 'identity
8183 (append
8184 (get 'org-agenda-category-filter :preset-filter)
8185 org-agenda-category-filter)
8187 ">")
8188 'face 'org-agenda-filter-category
8189 'help-echo "Category used in filtering")) "")
8190 (if (or org-agenda-tag-filter
8191 (get 'org-agenda-tag-filter :preset-filter))
8192 '(:eval (org-propertize
8193 (concat " {"
8194 (mapconcat
8195 'identity
8196 (append
8197 (get 'org-agenda-tag-filter :preset-filter)
8198 org-agenda-tag-filter)
8200 "}")
8201 'face 'org-agenda-filter-tags
8202 'help-echo "Tags used in filtering")) "")
8203 (if (or org-agenda-regexp-filter
8204 (get 'org-agenda-regexp-filter :preset-filter))
8205 '(:eval (org-propertize
8206 (concat " ["
8207 (mapconcat
8208 'identity
8209 (append
8210 (get 'org-agenda-regexp-filter :preset-filter)
8211 org-agenda-regexp-filter)
8213 "]")
8214 'face 'org-agenda-filter-regexp
8215 'help-echo "Regexp used in filtering")) "")
8216 (if org-agenda-archives-mode
8217 (if (eq org-agenda-archives-mode t)
8218 " Archives"
8219 (format " :%s:" org-archive-tag))
8221 (if org-agenda-clockreport-mode
8222 (if (eq org-agenda-clockreport-mode 'with-filter)
8223 " Clock{}" " Clock")
8224 "")))
8225 (force-mode-line-update))
8227 (define-obsolete-function-alias
8228 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8230 (defun org-agenda-update-agenda-type ()
8231 "Update the agenda type after each command."
8232 (setq org-agenda-type
8233 (or (get-text-property (point) 'org-agenda-type)
8234 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8236 (defun org-agenda-next-line ()
8237 "Move cursor to the next line, and show if follow mode is active."
8238 (interactive)
8239 (call-interactively 'next-line)
8240 (org-agenda-do-context-action))
8242 (defun org-agenda-previous-line ()
8243 "Move cursor to the previous line, and show if follow-mode is active."
8244 (interactive)
8245 (call-interactively 'previous-line)
8246 (org-agenda-do-context-action))
8248 (defun org-agenda-next-item (n)
8249 "Move cursor to next agenda item."
8250 (interactive "p")
8251 (let ((col (current-column)))
8252 (dotimes (c n)
8253 (when (next-single-property-change (point-at-eol) 'org-marker)
8254 (move-end-of-line 1)
8255 (goto-char (next-single-property-change (point) 'org-marker))))
8256 (org-move-to-column col))
8257 (org-agenda-do-context-action))
8259 (defun org-agenda-previous-item (n)
8260 "Move cursor to next agenda item."
8261 (interactive "p")
8262 (dotimes (c n)
8263 (let ((col (current-column))
8264 (goto (save-excursion
8265 (move-end-of-line 0)
8266 (previous-single-property-change (point) 'org-marker))))
8267 (if goto (goto-char goto))
8268 (org-move-to-column col)))
8269 (org-agenda-do-context-action))
8271 (defun org-agenda-do-context-action ()
8272 "Show outline path and, maybe, follow mode window."
8273 (let ((m (org-get-at-bol 'org-marker)))
8274 (when (and (markerp m) (marker-buffer m))
8275 (and org-agenda-follow-mode
8276 (if org-agenda-follow-indirect
8277 (org-agenda-tree-to-indirect-buffer nil)
8278 (org-agenda-show)))
8279 (and org-agenda-show-outline-path
8280 (org-with-point-at m (org-display-outline-path t))))))
8282 (defun org-agenda-show-tags ()
8283 "Show the tags applicable to the current item."
8284 (interactive)
8285 (let* ((tags (org-get-at-bol 'tags)))
8286 (if tags
8287 (message "Tags are :%s:"
8288 (org-no-properties (mapconcat 'identity tags ":")))
8289 (message "No tags associated with this line"))))
8291 (defun org-agenda-goto (&optional highlight)
8292 "Go to the Org-mode file which contains the item at point."
8293 (interactive)
8294 (let* ((marker (or (org-get-at-bol 'org-marker)
8295 (org-agenda-error)))
8296 (buffer (marker-buffer marker))
8297 (pos (marker-position marker)))
8298 (switch-to-buffer-other-window buffer)
8299 (widen)
8300 (push-mark)
8301 (goto-char pos)
8302 (when (derived-mode-p 'org-mode)
8303 (org-show-context 'agenda)
8304 (save-excursion
8305 (and (outline-next-heading)
8306 (org-flag-heading nil)))) ; show the next heading
8307 (when (outline-invisible-p)
8308 (show-entry)) ; display invisible text
8309 (recenter (/ (window-height) 2))
8310 (run-hooks 'org-agenda-after-show-hook)
8311 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8313 (defvar org-agenda-after-show-hook nil
8314 "Normal hook run after an item has been shown from the agenda.
8315 Point is in the buffer where the item originated.")
8317 (defun org-agenda-kill ()
8318 "Kill the entry or subtree belonging to the current agenda entry."
8319 (interactive)
8320 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8321 (let* ((bufname-orig (buffer-name))
8322 (marker (or (org-get-at-bol 'org-marker)
8323 (org-agenda-error)))
8324 (buffer (marker-buffer marker))
8325 (pos (marker-position marker))
8326 (type (org-get-at-bol 'type))
8327 dbeg dend (n 0) conf)
8328 (org-with-remote-undo buffer
8329 (with-current-buffer buffer
8330 (save-excursion
8331 (goto-char pos)
8332 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8333 (setq dbeg (progn (org-back-to-heading t) (point))
8334 dend (org-end-of-subtree t t))
8335 (setq dbeg (point-at-bol)
8336 dend (min (point-max) (1+ (point-at-eol)))))
8337 (goto-char dbeg)
8338 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8339 (setq conf (or (eq t org-agenda-confirm-kill)
8340 (and (numberp org-agenda-confirm-kill)
8341 (> n org-agenda-confirm-kill))))
8342 (and conf
8343 (not (y-or-n-p
8344 (format "Delete entry with %d lines in buffer \"%s\"? "
8345 n (buffer-name buffer))))
8346 (error "Abort"))
8347 (let ((org-agenda-buffer-name bufname-orig))
8348 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8349 (with-current-buffer buffer (delete-region dbeg dend))
8350 (message "Agenda item and source killed"))))
8352 (defvar org-archive-default-command) ; defined in org-archive.el
8353 (defun org-agenda-archive-default ()
8354 "Archive the entry or subtree belonging to the current agenda entry."
8355 (interactive)
8356 (require 'org-archive)
8357 (org-agenda-archive-with org-archive-default-command))
8359 (defun org-agenda-archive-default-with-confirmation ()
8360 "Archive the entry or subtree belonging to the current agenda entry."
8361 (interactive)
8362 (require 'org-archive)
8363 (org-agenda-archive-with org-archive-default-command 'confirm))
8365 (defun org-agenda-archive ()
8366 "Archive the entry or subtree belonging to the current agenda entry."
8367 (interactive)
8368 (org-agenda-archive-with 'org-archive-subtree))
8370 (defun org-agenda-archive-to-archive-sibling ()
8371 "Move the entry to the archive sibling."
8372 (interactive)
8373 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8375 (defun org-agenda-archive-with (cmd &optional confirm)
8376 "Move the entry to the archive sibling."
8377 (interactive)
8378 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8379 (let* ((bufname-orig (buffer-name))
8380 (marker (or (org-get-at-bol 'org-marker)
8381 (org-agenda-error)))
8382 (buffer (marker-buffer marker))
8383 (pos (marker-position marker)))
8384 (org-with-remote-undo buffer
8385 (with-current-buffer buffer
8386 (if (derived-mode-p 'org-mode)
8387 (if (and confirm
8388 (not (y-or-n-p "Archive this subtree or entry? ")))
8389 (error "Abort")
8390 (save-window-excursion
8391 (goto-char pos)
8392 (let ((org-agenda-buffer-name bufname-orig))
8393 (org-remove-subtree-entries-from-agenda))
8394 (org-back-to-heading t)
8395 (funcall cmd)))
8396 (error "Archiving works only in Org-mode files"))))))
8398 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8399 "Remove all lines in the agenda that correspond to a given subtree.
8400 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8401 If this information is not given, the function uses the tree at point."
8402 (let ((buf (or buf (current-buffer))) m p)
8403 (save-excursion
8404 (unless (and beg end)
8405 (org-back-to-heading t)
8406 (setq beg (point))
8407 (org-end-of-subtree t)
8408 (setq end (point)))
8409 (set-buffer (get-buffer org-agenda-buffer-name))
8410 (save-excursion
8411 (goto-char (point-max))
8412 (beginning-of-line 1)
8413 (while (not (bobp))
8414 (when (and (setq m (org-get-at-bol 'org-marker))
8415 (equal buf (marker-buffer m))
8416 (setq p (marker-position m))
8417 (>= p beg)
8418 (< p end))
8419 (let ((inhibit-read-only t))
8420 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8421 (beginning-of-line 0))))))
8423 (defun org-agenda-refile (&optional goto rfloc no-update)
8424 "Refile the item at point.
8426 When GOTO is 0 or '(64), clear the refile cache.
8427 When GOTO is '(16), go to the location of the last refiled item.
8428 RFLOC can be a refile location obtained in a different way.
8429 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8430 (interactive "P")
8431 (cond
8432 ((member goto '(0 (64)))
8433 (org-refile-cache-clear))
8434 ((equal goto '(16))
8435 (org-refile-goto-last-stored))
8437 (let* ((buffer-orig (buffer-name))
8438 (marker (or (org-get-at-bol 'org-hd-marker)
8439 (org-agenda-error)))
8440 (buffer (marker-buffer marker))
8441 (pos (marker-position marker))
8442 (rfloc (or rfloc
8443 (org-refile-get-location
8444 (if goto "Goto" "Refile to") buffer
8445 org-refile-allow-creating-parent-nodes))))
8446 (with-current-buffer buffer
8447 (save-excursion
8448 (save-restriction
8449 (widen)
8450 (goto-char marker)
8451 (let ((org-agenda-buffer-name buffer-orig))
8452 (org-remove-subtree-entries-from-agenda))
8453 (org-refile goto buffer rfloc)))))
8454 (unless no-update (org-agenda-redo)))))
8456 (defun org-agenda-open-link (&optional arg)
8457 "Open the link(s) in the current entry, if any.
8458 This looks for a link in the displayed line in the agenda.
8459 It also looks at the text of the entry itself."
8460 (interactive "P")
8461 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8462 (org-get-at-bol 'org-marker)))
8463 (buffer (and marker (marker-buffer marker)))
8464 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8465 (lkall (and buffer (org-offer-links-in-entry
8466 buffer marker arg prefix)))
8467 (lk0 (car lkall))
8468 (lk (if (stringp lk0) (list lk0) lk0))
8469 (lkend (cdr lkall))
8470 trg)
8471 (cond
8472 ((and buffer lk)
8473 (mapcar (lambda(l)
8474 (with-current-buffer buffer
8475 (setq trg (and (string-match org-bracket-link-regexp l)
8476 (match-string 1 l)))
8477 (if (or (not trg) (string-match org-any-link-re trg))
8478 (save-excursion
8479 (save-restriction
8480 (widen)
8481 (goto-char marker)
8482 (when (search-forward l nil lkend)
8483 (goto-char (match-beginning 0))
8484 (org-open-at-point))))
8485 ;; This is an internal link, widen the buffer
8486 (switch-to-buffer-other-window buffer)
8487 (widen)
8488 (goto-char marker)
8489 (when (search-forward l nil lkend)
8490 (goto-char (match-beginning 0))
8491 (org-open-at-point)))))
8492 lk))
8493 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8494 (save-excursion
8495 (beginning-of-line 1)
8496 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8497 (org-open-link-from-string (match-string 1)))
8498 (t (message "No link to open here")))))
8500 (defun org-agenda-copy-local-variable (var)
8501 "Get a variable from a referenced buffer and install it here."
8502 (let ((m (org-get-at-bol 'org-marker)))
8503 (when (and m (buffer-live-p (marker-buffer m)))
8504 (org-set-local var (with-current-buffer (marker-buffer m)
8505 (symbol-value var))))))
8507 (defun org-agenda-switch-to (&optional delete-other-windows)
8508 "Go to the Org-mode file which contains the item at point."
8509 (interactive)
8510 (if (and org-return-follows-link
8511 (not (org-get-at-bol 'org-marker))
8512 (org-in-regexp org-bracket-link-regexp))
8513 (org-open-link-from-string (match-string 0))
8514 (let* ((marker (or (org-get-at-bol 'org-marker)
8515 (org-agenda-error)))
8516 (buffer (marker-buffer marker))
8517 (pos (marker-position marker)))
8518 (org-pop-to-buffer-same-window buffer)
8519 (and delete-other-windows (delete-other-windows))
8520 (widen)
8521 (goto-char pos)
8522 (when (derived-mode-p 'org-mode)
8523 (org-show-context 'agenda)
8524 (save-excursion
8525 (and (outline-next-heading)
8526 (org-flag-heading nil))) ; show the next heading
8527 (when (outline-invisible-p)
8528 (show-entry)) ; display invisible text
8529 (run-hooks 'org-agenda-after-show-hook)))))
8531 (defun org-agenda-goto-mouse (ev)
8532 "Go to the Org-mode file which contains the item at the mouse click."
8533 (interactive "e")
8534 (mouse-set-point ev)
8535 (org-agenda-goto))
8537 (defun org-agenda-show (&optional full-entry)
8538 "Display the Org-mode file which contains the item at point.
8539 With prefix argument FULL-ENTRY, make the entire entry visible
8540 if it was hidden in the outline."
8541 (interactive "P")
8542 (let ((win (selected-window)))
8543 (if full-entry
8544 (let ((org-show-entry-below t))
8545 (org-agenda-goto t))
8546 (org-agenda-goto t))
8547 (select-window win)))
8549 (defvar org-agenda-show-window nil)
8550 (defun org-agenda-show-and-scroll-up (&optional arg)
8551 "Display the Org-mode file which contains the item at point.
8552 When called repeatedly, scroll the window that is displaying the buffer.
8553 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8554 `show-subtree' to display the item, so that drawers and logbooks stay
8555 folded."
8556 (interactive "P")
8557 (let ((win (selected-window)))
8558 (if (and (window-live-p org-agenda-show-window)
8559 (eq this-command last-command))
8560 (progn
8561 (select-window org-agenda-show-window)
8562 (ignore-errors (scroll-up)))
8563 (org-agenda-goto t)
8564 (if arg (org-show-entry) (show-subtree))
8565 (setq org-agenda-show-window (selected-window)))
8566 (select-window win)))
8568 (defun org-agenda-show-scroll-down ()
8569 "Scroll down the window showing the agenda."
8570 (interactive)
8571 (let ((win (selected-window)))
8572 (when (window-live-p org-agenda-show-window)
8573 (select-window org-agenda-show-window)
8574 (ignore-errors (scroll-down))
8575 (select-window win))))
8577 (defun org-agenda-show-1 (&optional more)
8578 "Display the Org-mode file which contains the item at point.
8579 The prefix arg selects the amount of information to display:
8581 0 hide the subtree
8582 1 just show the entry according to defaults.
8583 2 show the children view
8584 3 show the subtree view
8585 4 show the entire subtree and any LOGBOOK drawers
8586 5 show the entire subtree and any drawers
8587 With prefix argument FULL-ENTRY, make the entire entry visible
8588 if it was hidden in the outline."
8589 (interactive "p")
8590 (let ((win (selected-window)))
8591 (org-agenda-goto t)
8592 (org-recenter-heading 1)
8593 (cond
8594 ((= more 0)
8595 (hide-subtree)
8596 (save-excursion
8597 (org-back-to-heading)
8598 (run-hook-with-args 'org-cycle-hook 'folded))
8599 (message "Remote: FOLDED"))
8600 ((and (org-called-interactively-p 'any) (= more 1))
8601 (message "Remote: show with default settings"))
8602 ((= more 2)
8603 (show-entry)
8604 (show-children)
8605 (save-excursion
8606 (org-back-to-heading)
8607 (run-hook-with-args 'org-cycle-hook 'children))
8608 (message "Remote: CHILDREN"))
8609 ((= more 3)
8610 (show-subtree)
8611 (save-excursion
8612 (org-back-to-heading)
8613 (run-hook-with-args 'org-cycle-hook 'subtree))
8614 (message "Remote: SUBTREE"))
8615 ((= more 4)
8616 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8617 (org-drawer-regexp
8618 (concat "^[ \t]*:\\("
8619 (mapconcat 'regexp-quote org-drawers "\\|")
8620 "\\):[ \t]*$")))
8621 (show-subtree)
8622 (save-excursion
8623 (org-back-to-heading)
8624 (org-cycle-hide-drawers 'subtree)))
8625 (message "Remote: SUBTREE AND LOGBOOK"))
8626 ((> more 4)
8627 (show-subtree)
8628 (message "Remote: SUBTREE AND ALL DRAWERS")))
8629 (select-window win)))
8631 (defun org-recenter-heading (n)
8632 (save-excursion
8633 (org-back-to-heading)
8634 (recenter n)))
8636 (defvar org-agenda-cycle-counter nil)
8637 (defun org-agenda-cycle-show (&optional n)
8638 "Show the current entry in another window, with default settings.
8639 Default settings are taken from `org-show-hierarchy-above' and siblings.
8640 When use repeatedly in immediate succession, the remote entry will cycle
8641 through visibility
8643 children -> subtree -> folded
8645 When called with a numeric prefix arg, that arg will be passed through to
8646 `org-agenda-show-1'. For the interpretation of that argument, see the
8647 docstring of `org-agenda-show-1'."
8648 (interactive "P")
8649 (if (integerp n)
8650 (setq org-agenda-cycle-counter n)
8651 (if (not (eq last-command this-command))
8652 (setq org-agenda-cycle-counter 1)
8653 (if (equal org-agenda-cycle-counter 0)
8654 (setq org-agenda-cycle-counter 2)
8655 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8656 (if (> org-agenda-cycle-counter 3)
8657 (setq org-agenda-cycle-counter 0)))))
8658 (org-agenda-show-1 org-agenda-cycle-counter))
8660 (defun org-agenda-recenter (arg)
8661 "Display the Org-mode file which contains the item at point and recenter."
8662 (interactive "P")
8663 (let ((win (selected-window)))
8664 (org-agenda-goto t)
8665 (recenter arg)
8666 (select-window win)))
8668 (defun org-agenda-show-mouse (ev)
8669 "Display the Org-mode file which contains the item at the mouse click."
8670 (interactive "e")
8671 (mouse-set-point ev)
8672 (org-agenda-show))
8674 (defun org-agenda-check-no-diary ()
8675 "Check if the entry is a diary link and abort if yes."
8676 (if (org-get-at-bol 'org-agenda-diary-link)
8677 (org-agenda-error)))
8679 (defun org-agenda-error ()
8680 (error "Command not allowed in this line"))
8682 (defun org-agenda-tree-to-indirect-buffer (arg)
8683 "Show the subtree corresponding to the current entry in an indirect buffer.
8684 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8686 With a numerical prefix ARG, go up to this level and then take that tree.
8687 With a negative numeric ARG, go up by this number of levels.
8688 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8689 use the dedicated frame)."
8690 (interactive "P")
8691 (if current-prefix-arg
8692 (org-agenda-do-tree-to-indirect-buffer arg)
8693 (let ((agenda-buffer (buffer-name))
8694 (agenda-window (selected-window))
8695 (indirect-window
8696 (and org-last-indirect-buffer
8697 (get-buffer-window org-last-indirect-buffer))))
8698 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8699 (unless (or (eq org-indirect-buffer-display 'new-frame)
8700 (eq org-indirect-buffer-display 'dedicated-frame))
8701 (unwind-protect
8702 (unless (and indirect-window (window-live-p indirect-window))
8703 (setq indirect-window (split-window agenda-window)))
8704 (and indirect-window (select-window indirect-window))
8705 (switch-to-buffer org-last-indirect-buffer :norecord)
8706 (fit-window-to-buffer indirect-window)))
8707 (select-window (get-buffer-window agenda-buffer)))))
8709 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8710 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8711 (org-agenda-check-no-diary)
8712 (let* ((marker (or (org-get-at-bol 'org-marker)
8713 (org-agenda-error)))
8714 (buffer (marker-buffer marker))
8715 (pos (marker-position marker)))
8716 (with-current-buffer buffer
8717 (save-excursion
8718 (goto-char pos)
8719 (funcall 'org-tree-to-indirect-buffer arg)))))
8721 (defvar org-last-heading-marker (make-marker)
8722 "Marker pointing to the headline that last changed its TODO state
8723 by a remote command from the agenda.")
8725 (defun org-agenda-todo-nextset ()
8726 "Switch TODO entry to next sequence."
8727 (interactive)
8728 (org-agenda-todo 'nextset))
8730 (defun org-agenda-todo-previousset ()
8731 "Switch TODO entry to previous sequence."
8732 (interactive)
8733 (org-agenda-todo 'previousset))
8735 (defun org-agenda-todo (&optional arg)
8736 "Cycle TODO state of line at point, also in Org-mode file.
8737 This changes the line at point, all other lines in the agenda referring to
8738 the same tree node, and the headline of the tree node in the Org-mode file."
8739 (interactive "P")
8740 (org-agenda-check-no-diary)
8741 (let* ((col (current-column))
8742 (marker (or (org-get-at-bol 'org-marker)
8743 (org-agenda-error)))
8744 (buffer (marker-buffer marker))
8745 (pos (marker-position marker))
8746 (hdmarker (org-get-at-bol 'org-hd-marker))
8747 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8748 (inhibit-read-only t)
8749 org-agenda-headline-snapshot-before-repeat newhead just-one)
8750 (org-with-remote-undo buffer
8751 (with-current-buffer buffer
8752 (widen)
8753 (goto-char pos)
8754 (org-show-context 'agenda)
8755 (save-excursion
8756 (and (outline-next-heading)
8757 (org-flag-heading nil))) ; show the next heading
8758 (let ((current-prefix-arg arg))
8759 (call-interactively 'org-todo))
8760 (and (bolp) (forward-char 1))
8761 (setq newhead (org-get-heading))
8762 (when (and (org-bound-and-true-p
8763 org-agenda-headline-snapshot-before-repeat)
8764 (not (equal org-agenda-headline-snapshot-before-repeat
8765 newhead))
8766 todayp)
8767 (setq newhead org-agenda-headline-snapshot-before-repeat
8768 just-one t))
8769 (save-excursion
8770 (org-back-to-heading)
8771 (move-marker org-last-heading-marker (point))))
8772 (beginning-of-line 1)
8773 (save-window-excursion
8774 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8775 (when (org-bound-and-true-p org-clock-out-when-done)
8776 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8777 newhead)
8778 (org-agenda-unmark-clocking-task))
8779 (org-move-to-column col))))
8781 (defun org-agenda-add-note (&optional arg)
8782 "Add a time-stamped note to the entry at point."
8783 (interactive "P")
8784 (org-agenda-check-no-diary)
8785 (let* ((marker (or (org-get-at-bol 'org-marker)
8786 (org-agenda-error)))
8787 (buffer (marker-buffer marker))
8788 (pos (marker-position marker))
8789 (hdmarker (org-get-at-bol 'org-hd-marker))
8790 (inhibit-read-only t))
8791 (with-current-buffer buffer
8792 (widen)
8793 (goto-char pos)
8794 (org-show-context 'agenda)
8795 (save-excursion
8796 (and (outline-next-heading)
8797 (org-flag-heading nil))) ; show the next heading
8798 (org-add-note))))
8800 (defun org-agenda-change-all-lines (newhead hdmarker
8801 &optional fixface just-this)
8802 "Change all lines in the agenda buffer which match HDMARKER.
8803 The new content of the line will be NEWHEAD (as modified by
8804 `org-agenda-format-item'). HDMARKER is checked with
8805 `equal' against all `org-hd-marker' text properties in the file.
8806 If FIXFACE is non-nil, the face of each item is modified according to
8807 the new TODO state.
8808 If JUST-THIS is non-nil, change just the current line, not all.
8809 If FORCE-TAGS is non nil, the car of it returns the new tags."
8810 (let* ((inhibit-read-only t)
8811 (line (org-current-line))
8812 (org-agenda-buffer (current-buffer))
8813 (thetags (with-current-buffer (marker-buffer hdmarker)
8814 (save-excursion (save-restriction (widen)
8815 (goto-char hdmarker)
8816 (org-get-tags-at)))))
8817 props m pl undone-face done-face finish new dotime level cat tags)
8818 (save-excursion
8819 (goto-char (point-max))
8820 (beginning-of-line 1)
8821 (while (not finish)
8822 (setq finish (bobp))
8823 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8824 (or (not just-this) (= (org-current-line) line))
8825 (equal m hdmarker))
8826 (setq props (text-properties-at (point))
8827 dotime (org-get-at-bol 'dotime)
8828 cat (org-get-at-bol 'org-category)
8829 level (org-get-at-bol 'level)
8830 tags thetags
8832 (let ((org-prefix-format-compiled
8833 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8834 org-prefix-format-compiled))
8835 (extra (org-get-at-bol 'extra)))
8836 (with-current-buffer (marker-buffer hdmarker)
8837 (save-excursion
8838 (save-restriction
8839 (widen)
8840 (org-agenda-format-item extra newhead level cat tags dotime)))))
8841 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8842 undone-face (org-get-at-bol 'undone-face)
8843 done-face (org-get-at-bol 'done-face))
8844 (beginning-of-line 1)
8845 (cond
8846 ((equal new "")
8847 (and (looking-at ".*\n?") (replace-match "")))
8848 ((looking-at ".*")
8849 (replace-match new t t)
8850 (beginning-of-line 1)
8851 (add-text-properties (point-at-bol) (point-at-eol) props)
8852 (when fixface
8853 (add-text-properties
8854 (point-at-bol) (point-at-eol)
8855 (list 'face
8856 (if org-last-todo-state-is-todo
8857 undone-face done-face))))
8858 (org-agenda-highlight-todo 'line)
8859 (beginning-of-line 1))
8860 (t (error "Line update did not work")))
8861 (save-restriction
8862 (narrow-to-region (point-at-bol) (point-at-eol))
8863 (org-agenda-finalize)))
8864 (beginning-of-line 0)))))
8866 (defun org-agenda-align-tags (&optional line)
8867 "Align all tags in agenda items to `org-agenda-tags-column'."
8868 (let ((inhibit-read-only t) l c)
8869 (save-excursion
8870 (goto-char (if line (point-at-bol) (point-min)))
8871 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8872 (if line (point-at-eol) nil) t)
8873 (add-text-properties
8874 (match-beginning 2) (match-end 2)
8875 (list 'face (delq nil (let ((prop (get-text-property
8876 (match-beginning 2) 'face)))
8877 (or (listp prop) (setq prop (list prop)))
8878 (if (memq 'org-tag prop)
8879 prop
8880 (cons 'org-tag prop))))))
8881 (setq l (- (match-end 2) (match-beginning 2))
8882 c (if (< org-agenda-tags-column 0)
8883 (- (abs org-agenda-tags-column) l)
8884 org-agenda-tags-column))
8885 (delete-region (match-beginning 1) (match-end 1))
8886 (goto-char (match-beginning 1))
8887 (insert (org-add-props
8888 (make-string (max 1 (- c (current-column))) ?\ )
8889 (plist-put (copy-sequence (text-properties-at (point)))
8890 'face nil))))
8891 (goto-char (point-min))
8892 (org-font-lock-add-tag-faces (point-max)))))
8894 (defun org-agenda-priority-up ()
8895 "Increase the priority of line at point, also in Org-mode file."
8896 (interactive)
8897 (org-agenda-priority 'up))
8899 (defun org-agenda-priority-down ()
8900 "Decrease the priority of line at point, also in Org-mode file."
8901 (interactive)
8902 (org-agenda-priority 'down))
8904 (defun org-agenda-priority (&optional force-direction)
8905 "Set the priority of line at point, also in Org-mode file.
8906 This changes the line at point, all other lines in the agenda referring to
8907 the same tree node, and the headline of the tree node in the Org-mode file.
8908 Called with a universal prefix arg, show the priority instead of setting it."
8909 (interactive "P")
8910 (if (equal force-direction '(4))
8911 (org-show-priority)
8912 (unless org-enable-priority-commands
8913 (error "Priority commands are disabled"))
8914 (org-agenda-check-no-diary)
8915 (let* ((col (current-column))
8916 (marker (or (org-get-at-bol 'org-marker)
8917 (org-agenda-error)))
8918 (hdmarker (org-get-at-bol 'org-hd-marker))
8919 (buffer (marker-buffer hdmarker))
8920 (pos (marker-position hdmarker))
8921 (inhibit-read-only t)
8922 newhead)
8923 (org-with-remote-undo buffer
8924 (with-current-buffer buffer
8925 (widen)
8926 (goto-char pos)
8927 (org-show-context 'agenda)
8928 (save-excursion
8929 (and (outline-next-heading)
8930 (org-flag-heading nil))) ; show the next heading
8931 (funcall 'org-priority force-direction)
8932 (end-of-line 1)
8933 (setq newhead (org-get-heading)))
8934 (org-agenda-change-all-lines newhead hdmarker)
8935 (org-move-to-column col)))))
8937 ;; FIXME: should fix the tags property of the agenda line.
8938 (defun org-agenda-set-tags (&optional tag onoff)
8939 "Set tags for the current headline."
8940 (interactive)
8941 (org-agenda-check-no-diary)
8942 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8943 (call-interactively 'org-change-tag-in-region)
8944 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8945 (org-agenda-error)))
8946 (buffer (marker-buffer hdmarker))
8947 (pos (marker-position hdmarker))
8948 (inhibit-read-only t)
8949 newhead)
8950 (org-with-remote-undo buffer
8951 (with-current-buffer buffer
8952 (widen)
8953 (goto-char pos)
8954 (save-excursion
8955 (org-show-context 'agenda))
8956 (save-excursion
8957 (and (outline-next-heading)
8958 (org-flag-heading nil))) ; show the next heading
8959 (goto-char pos)
8960 (if tag
8961 (org-toggle-tag tag onoff)
8962 (call-interactively 'org-set-tags))
8963 (end-of-line 1)
8964 (setq newhead (org-get-heading)))
8965 (org-agenda-change-all-lines newhead hdmarker)
8966 (beginning-of-line 1)))))
8968 (defun org-agenda-set-property ()
8969 "Set a property for the current headline."
8970 (interactive)
8971 (org-agenda-check-no-diary)
8972 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8973 (org-agenda-error)))
8974 (buffer (marker-buffer hdmarker))
8975 (pos (marker-position hdmarker))
8976 (inhibit-read-only t)
8977 newhead)
8978 (org-with-remote-undo buffer
8979 (with-current-buffer buffer
8980 (widen)
8981 (goto-char pos)
8982 (save-excursion
8983 (org-show-context 'agenda))
8984 (save-excursion
8985 (and (outline-next-heading)
8986 (org-flag-heading nil))) ; show the next heading
8987 (goto-char pos)
8988 (call-interactively 'org-set-property)))))
8990 (defun org-agenda-set-effort ()
8991 "Set the effort property for the current headline."
8992 (interactive)
8993 (org-agenda-check-no-diary)
8994 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8995 (org-agenda-error)))
8996 (buffer (marker-buffer hdmarker))
8997 (pos (marker-position hdmarker))
8998 (inhibit-read-only t)
8999 newhead)
9000 (org-with-remote-undo buffer
9001 (with-current-buffer buffer
9002 (widen)
9003 (goto-char pos)
9004 (save-excursion
9005 (org-show-context 'agenda))
9006 (save-excursion
9007 (and (outline-next-heading)
9008 (org-flag-heading nil))) ; show the next heading
9009 (goto-char pos)
9010 (call-interactively 'org-set-effort)
9011 (end-of-line 1)
9012 (setq newhead (org-get-heading)))
9013 (org-agenda-change-all-lines newhead hdmarker))))
9015 (defun org-agenda-toggle-archive-tag ()
9016 "Toggle the archive tag for the current entry."
9017 (interactive)
9018 (org-agenda-check-no-diary)
9019 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9020 (org-agenda-error)))
9021 (buffer (marker-buffer hdmarker))
9022 (pos (marker-position hdmarker))
9023 (inhibit-read-only t)
9024 newhead)
9025 (org-with-remote-undo buffer
9026 (with-current-buffer buffer
9027 (widen)
9028 (goto-char pos)
9029 (org-show-context 'agenda)
9030 (save-excursion
9031 (and (outline-next-heading)
9032 (org-flag-heading nil))) ; show the next heading
9033 (call-interactively 'org-toggle-archive-tag)
9034 (end-of-line 1)
9035 (setq newhead (org-get-heading)))
9036 (org-agenda-change-all-lines newhead hdmarker)
9037 (beginning-of-line 1))))
9039 (defun org-agenda-do-date-later (arg)
9040 (interactive "P")
9041 (cond
9042 ((or (equal arg '(16))
9043 (memq last-command
9044 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9045 (setq this-command 'org-agenda-date-later-minutes)
9046 (org-agenda-date-later-minutes 1))
9047 ((or (equal arg '(4))
9048 (memq last-command
9049 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9050 (setq this-command 'org-agenda-date-later-hours)
9051 (org-agenda-date-later-hours 1))
9053 (org-agenda-date-later (prefix-numeric-value arg)))))
9055 (defun org-agenda-do-date-earlier (arg)
9056 (interactive "P")
9057 (cond
9058 ((or (equal arg '(16))
9059 (memq last-command
9060 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9061 (setq this-command 'org-agenda-date-earlier-minutes)
9062 (org-agenda-date-earlier-minutes 1))
9063 ((or (equal arg '(4))
9064 (memq last-command
9065 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9066 (setq this-command 'org-agenda-date-earlier-hours)
9067 (org-agenda-date-earlier-hours 1))
9069 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9071 (defun org-agenda-date-later (arg &optional what)
9072 "Change the date of this item to ARG day(s) later."
9073 (interactive "p")
9074 (org-agenda-check-type t 'agenda 'timeline)
9075 (org-agenda-check-no-diary)
9076 (let* ((marker (or (org-get-at-bol 'org-marker)
9077 (org-agenda-error)))
9078 (buffer (marker-buffer marker))
9079 (pos (marker-position marker))
9080 cdate today)
9081 (org-with-remote-undo buffer
9082 (with-current-buffer buffer
9083 (widen)
9084 (goto-char pos)
9085 (if (not (org-at-timestamp-p))
9086 (error "Cannot find time stamp"))
9087 (when (and org-agenda-move-date-from-past-immediately-to-today
9088 (equal arg 1)
9089 (or (not what) (eq what 'day))
9090 (not (save-match-data (org-at-date-range-p))))
9091 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9092 cdate (calendar-absolute-from-gregorian
9093 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9094 today (org-today))
9095 (if (> today cdate)
9096 ;; immediately shift to today
9097 (setq arg (- today cdate))))
9098 (org-timestamp-change arg (or what 'day))
9099 (when (and (org-at-date-range-p)
9100 (re-search-backward org-tr-regexp-both (point-at-bol)))
9101 (let ((end org-last-changed-timestamp))
9102 (org-timestamp-change arg (or what 'day))
9103 (setq org-last-changed-timestamp
9104 (concat org-last-changed-timestamp "--" end)))))
9105 (org-agenda-show-new-time marker org-last-changed-timestamp))
9106 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9108 (defun org-agenda-date-earlier (arg &optional what)
9109 "Change the date of this item to ARG day(s) earlier."
9110 (interactive "p")
9111 (org-agenda-date-later (- arg) what))
9113 (defun org-agenda-date-later-minutes (arg)
9114 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9115 (interactive "p")
9116 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9117 (org-agenda-date-later arg 'minute))
9119 (defun org-agenda-date-earlier-minutes (arg)
9120 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9121 (interactive "p")
9122 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9123 (org-agenda-date-earlier arg 'minute))
9125 (defun org-agenda-date-later-hours (arg)
9126 "Change the time of this item, in hour steps."
9127 (interactive "p")
9128 (org-agenda-date-later arg 'hour))
9130 (defun org-agenda-date-earlier-hours (arg)
9131 "Change the time of this item, in hour steps."
9132 (interactive "p")
9133 (org-agenda-date-earlier arg 'hour))
9135 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9136 "Show new date stamp via text properties."
9137 ;; We use text properties to make this undoable
9138 (let ((inhibit-read-only t))
9139 (setq stamp (concat prefix " => " stamp " "))
9140 (save-excursion
9141 (goto-char (point-max))
9142 (while (not (bobp))
9143 (when (equal marker (org-get-at-bol 'org-marker))
9144 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9145 (org-move-to-column (- (window-width) (length stamp)) t)
9146 (if (featurep 'xemacs)
9147 ;; Use `duplicable' property to trigger undo recording
9148 (let ((ex (make-extent nil nil))
9149 (gl (make-glyph stamp)))
9150 (set-glyph-face gl 'secondary-selection)
9151 (set-extent-properties
9152 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9153 (insert-extent ex (1- (point)) (point-at-eol)))
9154 (add-text-properties
9155 (1- (point)) (point-at-eol)
9156 (list 'display (org-add-props stamp nil
9157 'face '(secondary-selection default)))))
9158 (beginning-of-line 1))
9159 (beginning-of-line 0)))))
9161 (defun org-agenda-date-prompt (arg)
9162 "Change the date of this item. Date is prompted for, with default today.
9163 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9164 be used to request time specification in the time stamp."
9165 (interactive "P")
9166 (org-agenda-check-type t 'agenda 'timeline)
9167 (org-agenda-check-no-diary)
9168 (let* ((marker (or (org-get-at-bol 'org-marker)
9169 (org-agenda-error)))
9170 (buffer (marker-buffer marker))
9171 (pos (marker-position marker)))
9172 (org-with-remote-undo buffer
9173 (with-current-buffer buffer
9174 (widen)
9175 (goto-char pos)
9176 (if (not (org-at-timestamp-p t))
9177 (error "Cannot find time stamp"))
9178 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9179 (org-agenda-show-new-time marker org-last-changed-timestamp))
9180 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9182 (defun org-agenda-schedule (arg &optional time)
9183 "Schedule the item at point.
9184 ARG is passed through to `org-schedule'."
9185 (interactive "P")
9186 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9187 (org-agenda-check-no-diary)
9188 (let* ((marker (or (org-get-at-bol 'org-marker)
9189 (org-agenda-error)))
9190 (type (marker-insertion-type marker))
9191 (buffer (marker-buffer marker))
9192 (pos (marker-position marker))
9193 (org-insert-labeled-timestamps-at-point nil)
9195 (set-marker-insertion-type marker t)
9196 (org-with-remote-undo buffer
9197 (with-current-buffer buffer
9198 (widen)
9199 (goto-char pos)
9200 (setq ts (org-schedule arg time)))
9201 (org-agenda-show-new-time marker ts " S"))
9202 (message "%s" ts)))
9204 (defun org-agenda-deadline (arg &optional time)
9205 "Schedule the item at point.
9206 ARG is passed through to `org-deadline'."
9207 (interactive "P")
9208 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9209 (org-agenda-check-no-diary)
9210 (let* ((marker (or (org-get-at-bol 'org-marker)
9211 (org-agenda-error)))
9212 (buffer (marker-buffer marker))
9213 (pos (marker-position marker))
9214 (org-insert-labeled-timestamps-at-point nil)
9216 (org-with-remote-undo buffer
9217 (with-current-buffer buffer
9218 (widen)
9219 (goto-char pos)
9220 (setq ts (org-deadline arg time)))
9221 (org-agenda-show-new-time marker ts " D"))
9222 (message "%s" ts)))
9224 (defun org-agenda-clock-in (&optional arg)
9225 "Start the clock on the currently selected item."
9226 (interactive "P")
9227 (org-agenda-check-no-diary)
9228 (if (equal arg '(4))
9229 (org-clock-in arg)
9230 (let* ((marker (or (org-get-at-bol 'org-marker)
9231 (org-agenda-error)))
9232 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9233 (pos (marker-position marker))
9234 (col (current-column))
9235 newhead)
9236 (org-with-remote-undo (marker-buffer marker)
9237 (with-current-buffer (marker-buffer marker)
9238 (widen)
9239 (goto-char pos)
9240 (org-show-context 'agenda)
9241 (org-show-entry)
9242 (org-cycle-hide-drawers 'children)
9243 (org-clock-in arg)
9244 (setq newhead (org-get-heading)))
9245 (org-agenda-change-all-lines newhead hdmarker))
9246 (org-move-to-column col))))
9248 (defun org-agenda-clock-out ()
9249 "Stop the currently running clock."
9250 (interactive)
9251 (unless (marker-buffer org-clock-marker)
9252 (error "No running clock"))
9253 (let ((marker (make-marker)) (col (current-column)) newhead)
9254 (org-with-remote-undo (marker-buffer org-clock-marker)
9255 (with-current-buffer (marker-buffer org-clock-marker)
9256 (save-excursion
9257 (save-restriction
9258 (widen)
9259 (goto-char org-clock-marker)
9260 (org-back-to-heading t)
9261 (move-marker marker (point))
9262 (org-clock-out)
9263 (setq newhead (org-get-heading))))))
9264 (org-agenda-change-all-lines newhead marker)
9265 (move-marker marker nil)
9266 (org-move-to-column col)
9267 (org-agenda-unmark-clocking-task)))
9269 (defun org-agenda-clock-cancel (&optional arg)
9270 "Cancel the currently running clock."
9271 (interactive "P")
9272 (unless (marker-buffer org-clock-marker)
9273 (user-error "No running clock"))
9274 (org-with-remote-undo (marker-buffer org-clock-marker)
9275 (org-clock-cancel)))
9277 (defun org-agenda-clock-goto ()
9278 "Jump to the currently clocked in task within the agenda.
9279 If the currently clocked in task is not listed in the agenda
9280 buffer, display it in another window."
9281 (interactive)
9282 (let (pos)
9283 (mapc (lambda (o)
9284 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9285 (setq pos (overlay-start o))))
9286 (overlays-in (point-min) (point-max)))
9287 (cond (pos (goto-char pos))
9288 ;; If the currently clocked entry is not in the agenda
9289 ;; buffer, we visit it in another window:
9290 (org-clock-current-task
9291 (org-switch-to-buffer-other-window (org-clock-goto)))
9292 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9294 (defun org-agenda-diary-entry-in-org-file ()
9295 "Make a diary entry in the file `org-agenda-diary-file'."
9296 (let (d1 d2 char (text "") dp1 dp2)
9297 (if (equal (buffer-name) "*Calendar*")
9298 (setq d1 (calendar-cursor-to-date t)
9299 d2 (car calendar-mark-ring))
9300 (setq dp1 (get-text-property (point-at-bol) 'day))
9301 (unless dp1 (user-error "No date defined in current line"))
9302 (setq d1 (calendar-gregorian-from-absolute dp1)
9303 d2 (and (ignore-errors (mark))
9304 (save-excursion
9305 (goto-char (mark))
9306 (setq dp2 (get-text-property (point-at-bol) 'day)))
9307 (calendar-gregorian-from-absolute dp2))))
9308 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9309 (setq char (read-char-exclusive))
9310 (cond
9311 ((equal char ?d)
9312 (setq text (read-string "Day entry: "))
9313 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9314 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9315 ((equal char ?a)
9316 (setq d1 (list (car d1) (nth 1 d1)
9317 (read-number (format "Reference year [%d]: " (nth 2 d1))
9318 (nth 2 d1))))
9319 (setq text (read-string "Anniversary (use %d to show years): "))
9320 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9321 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9322 ((equal char ?b)
9323 (setq text (read-string "Block entry: "))
9324 (unless (and d1 d2 (not (equal d1 d2)))
9325 (user-error "No block of days selected"))
9326 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9327 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9328 ((equal char ?j)
9329 (org-switch-to-buffer-other-window
9330 (find-file-noselect org-agenda-diary-file))
9331 (require 'org-datetree)
9332 (org-datetree-find-date-create d1)
9333 (org-reveal t))
9334 (t (user-error "Invalid selection character `%c'" char)))))
9336 (defcustom org-agenda-insert-diary-strategy 'date-tree
9337 "Where in `org-agenda-diary-file' should new entries be added?
9338 Valid values:
9340 date-tree in the date tree, as child of the date
9341 top-level as top-level entries at the end of the file."
9342 :group 'org-agenda
9343 :type '(choice
9344 (const :tag "in a date tree" date-tree)
9345 (const :tag "as top level at end of file" top-level)))
9347 (defcustom org-agenda-insert-diary-extract-time nil
9348 "Non-nil means extract any time specification from the diary entry."
9349 :group 'org-agenda
9350 :version "24.1"
9351 :type 'boolean)
9353 (defcustom org-agenda-bulk-mark-char ">"
9354 "A single-character string to be used as the bulk mark."
9355 :group 'org-agenda
9356 :version "24.1"
9357 :type 'string)
9359 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9360 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9361 If TEXT is not empty, it will become the headline of the new entry, and
9362 the resulting entry will not be shown. When TEXT is empty, switch to
9363 `org-agenda-diary-file' and let the user finish the entry there."
9364 (let ((cw (current-window-configuration)))
9365 (org-switch-to-buffer-other-window
9366 (find-file-noselect org-agenda-diary-file))
9367 (widen)
9368 (goto-char (point-min))
9369 (cond
9370 ((eq type 'anniversary)
9371 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9372 (progn
9373 (or (org-at-heading-p t)
9374 (progn
9375 (outline-next-heading)
9376 (insert "* Anniversaries\n\n")
9377 (beginning-of-line -1)))))
9378 (outline-next-heading)
9379 (org-back-over-empty-lines)
9380 (backward-char 1)
9381 (insert "\n")
9382 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9383 (nth 2 d1) (car d1) (nth 1 d1) text)))
9384 ((eq type 'day)
9385 (let ((org-prefix-has-time t)
9386 (org-agenda-time-leading-zero t)
9387 fmt time time2)
9388 (if org-agenda-insert-diary-extract-time
9389 ;; Use org-agenda-format-item to parse text for a time-range and
9390 ;; remove it. FIXME: This is a hack, we should refactor
9391 ;; that function to make time extraction available separately
9392 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9393 time (get-text-property 0 'time fmt)
9394 time2 (if (> (length time) 0)
9395 ;; split-string removes trailing ...... if
9396 ;; no end time given. First space
9397 ;; separates time from date.
9398 (concat " " (car (split-string time "\\.")))
9399 nil)
9400 text (get-text-property 0 'txt fmt)))
9401 (if (eq org-agenda-insert-diary-strategy 'top-level)
9402 (org-agenda-insert-diary-as-top-level text)
9403 (require 'org-datetree)
9404 (org-datetree-find-date-create d1)
9405 (org-agenda-insert-diary-make-new-entry text))
9406 (org-insert-time-stamp (org-time-from-absolute
9407 (calendar-absolute-from-gregorian d1))
9408 nil nil nil nil time2))
9409 (end-of-line 0))
9410 ((eq type 'block)
9411 (if (> (calendar-absolute-from-gregorian d1)
9412 (calendar-absolute-from-gregorian d2))
9413 (setq d1 (prog1 d2 (setq d2 d1))))
9414 (if (eq org-agenda-insert-diary-strategy 'top-level)
9415 (org-agenda-insert-diary-as-top-level text)
9416 (require 'org-datetree)
9417 (org-datetree-find-date-create d1)
9418 (org-agenda-insert-diary-make-new-entry text))
9419 (org-insert-time-stamp (org-time-from-absolute
9420 (calendar-absolute-from-gregorian d1)))
9421 (insert "--")
9422 (org-insert-time-stamp (org-time-from-absolute
9423 (calendar-absolute-from-gregorian d2)))
9424 (end-of-line 0)))
9425 (if (string-match "\\S-" text)
9426 (progn
9427 (set-window-configuration cw)
9428 (message "%s entry added to %s"
9429 (capitalize (symbol-name type))
9430 (abbreviate-file-name org-agenda-diary-file)))
9431 (org-reveal t)
9432 (message "Please finish entry here"))))
9434 (defun org-agenda-insert-diary-as-top-level (text)
9435 "Make new entry as a top-level entry at the end of the file.
9436 Add TEXT as headline, and position the cursor in the second line so that
9437 a timestamp can be added there."
9438 (widen)
9439 (goto-char (point-max))
9440 (or (bolp) (insert "\n"))
9441 (insert "* " text "\n")
9442 (if org-adapt-indentation (org-indent-to-column 2)))
9444 (defun org-agenda-insert-diary-make-new-entry (text)
9445 "Make new entry as last child of current entry.
9446 Add TEXT as headline, and position the cursor in the second line so that
9447 a timestamp can be added there."
9448 (let ((org-show-following-heading t)
9449 (org-show-siblings t)
9450 (org-show-hierarchy-above t)
9451 (org-show-entry-below t)
9452 col)
9453 (outline-next-heading)
9454 (org-back-over-empty-lines)
9455 (or (looking-at "[ \t]*$")
9456 (progn (insert "\n") (backward-char 1)))
9457 (org-insert-heading nil t)
9458 (org-do-demote)
9459 (setq col (current-column))
9460 (insert text "\n")
9461 (if org-adapt-indentation (org-indent-to-column col))
9462 (let ((org-show-following-heading t)
9463 (org-show-siblings t)
9464 (org-show-hierarchy-above t)
9465 (org-show-entry-below t))
9466 (org-show-context))))
9468 (defun org-agenda-diary-entry ()
9469 "Make a diary entry, like the `i' command from the calendar.
9470 All the standard commands work: block, weekly etc.
9471 When `org-agenda-diary-file' points to a file,
9472 `org-agenda-diary-entry-in-org-file' is called instead to create
9473 entries in that Org-mode file."
9474 (interactive)
9475 (if (not (eq org-agenda-diary-file 'diary-file))
9476 (org-agenda-diary-entry-in-org-file)
9477 (require 'diary-lib)
9478 (let* ((char (progn
9479 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9480 (read-char-exclusive)))
9481 (cmd (cdr (assoc char
9482 '((?d . insert-diary-entry)
9483 (?w . insert-weekly-diary-entry)
9484 (?m . insert-monthly-diary-entry)
9485 (?y . insert-yearly-diary-entry)
9486 (?a . insert-anniversary-diary-entry)
9487 (?b . insert-block-diary-entry)
9488 (?c . insert-cyclic-diary-entry)))))
9489 (oldf (symbol-function 'calendar-cursor-to-date))
9490 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9491 (point (point))
9492 (mark (or (mark t) (point))))
9493 (unless cmd
9494 (user-error "No command associated with <%c>" char))
9495 (unless (and (get-text-property point 'day)
9496 (or (not (equal ?b char))
9497 (get-text-property mark 'day)))
9498 (user-error "Don't know which date to use for diary entry"))
9499 ;; We implement this by hacking the `calendar-cursor-to-date' function
9500 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9501 (let ((calendar-mark-ring
9502 (list (calendar-gregorian-from-absolute
9503 (or (get-text-property mark 'day)
9504 (get-text-property point 'day))))))
9505 (unwind-protect
9506 (progn
9507 (fset 'calendar-cursor-to-date
9508 (lambda (&optional error dummy)
9509 (calendar-gregorian-from-absolute
9510 (get-text-property point 'day))))
9511 (call-interactively cmd))
9512 (fset 'calendar-cursor-to-date oldf))))))
9514 (defun org-agenda-execute-calendar-command (cmd)
9515 "Execute a calendar command from the agenda with date from cursor."
9516 (org-agenda-check-type t 'agenda 'timeline)
9517 (require 'diary-lib)
9518 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9519 (user-error "Don't know which date to use for the calendar command"))
9520 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9521 (point (point))
9522 (date (calendar-gregorian-from-absolute
9523 (get-text-property point 'day)))
9524 ;; the following 2 vars are needed in the calendar
9525 (displayed-month (car date))
9526 (displayed-year (nth 2 date)))
9527 (unwind-protect
9528 (progn
9529 (fset 'calendar-cursor-to-date
9530 (lambda (&optional error dummy)
9531 (calendar-gregorian-from-absolute
9532 (get-text-property point 'day))))
9533 (call-interactively cmd))
9534 (fset 'calendar-cursor-to-date oldf))))
9536 (defun org-agenda-phases-of-moon ()
9537 "Display the phases of the moon for the 3 months around the cursor date."
9538 (interactive)
9539 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9541 (defun org-agenda-holidays ()
9542 "Display the holidays for the 3 months around the cursor date."
9543 (interactive)
9544 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9546 (defvar calendar-longitude) ; defined in calendar.el
9547 (defvar calendar-latitude) ; defined in calendar.el
9548 (defvar calendar-location-name) ; defined in calendar.el
9550 (defun org-agenda-sunrise-sunset (arg)
9551 "Display sunrise and sunset for the cursor date.
9552 Latitude and longitude can be specified with the variables
9553 `calendar-latitude' and `calendar-longitude'. When called with prefix
9554 argument, latitude and longitude will be prompted for."
9555 (interactive "P")
9556 (require 'solar)
9557 (let ((calendar-longitude (if arg nil calendar-longitude))
9558 (calendar-latitude (if arg nil calendar-latitude))
9559 (calendar-location-name
9560 (if arg "the given coordinates" calendar-location-name)))
9561 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9563 (defun org-agenda-goto-calendar ()
9564 "Open the Emacs calendar with the date at the cursor."
9565 (interactive)
9566 (org-agenda-check-type t 'agenda 'timeline)
9567 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9568 (user-error "Don't know which date to open in calendar")))
9569 (date (calendar-gregorian-from-absolute day))
9570 (calendar-move-hook nil)
9571 (calendar-view-holidays-initially-flag nil)
9572 (calendar-view-diary-initially-flag nil))
9573 (calendar)
9574 (calendar-goto-date date)))
9576 ;;;###autoload
9577 (defun org-calendar-goto-agenda ()
9578 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9579 This is a command that has to be installed in `calendar-mode-map'."
9580 (interactive)
9581 (org-agenda-list nil (calendar-absolute-from-gregorian
9582 (calendar-cursor-to-date))
9583 nil))
9585 (defun org-agenda-convert-date ()
9586 (interactive)
9587 (org-agenda-check-type t 'agenda 'timeline)
9588 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9589 date s)
9590 (unless day
9591 (user-error "Don't know which date to convert"))
9592 (setq date (calendar-gregorian-from-absolute day))
9593 (setq s (concat
9594 "Gregorian: " (calendar-date-string date) "\n"
9595 "ISO: " (calendar-iso-date-string date) "\n"
9596 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9597 "Julian: " (calendar-julian-date-string date) "\n"
9598 "Astron. JD: " (calendar-astro-date-string date)
9599 " (Julian date number at noon UTC)\n"
9600 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9601 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9602 "French: " (calendar-french-date-string date) "\n"
9603 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9604 "Mayan: " (calendar-mayan-date-string date) "\n"
9605 "Coptic: " (calendar-coptic-date-string date) "\n"
9606 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9607 "Persian: " (calendar-persian-date-string date) "\n"
9608 "Chinese: " (calendar-chinese-date-string date) "\n"))
9609 (with-output-to-temp-buffer "*Dates*"
9610 (princ s))
9611 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9613 ;;; Bulk commands
9615 (defun org-agenda-bulk-marked-p ()
9616 (eq (get-char-property (point-at-bol) 'type)
9617 'org-marked-entry-overlay))
9619 (defun org-agenda-bulk-mark (&optional arg)
9620 "Mark the entry at point for future bulk action."
9621 (interactive "p")
9622 (dotimes (i (or arg 1))
9623 (unless (org-get-at-bol 'org-agenda-diary-link)
9624 (let* ((m (org-get-at-bol 'org-hd-marker))
9626 (unless (org-agenda-bulk-marked-p)
9627 (unless m (user-error "Nothing to mark at point"))
9628 (push m org-agenda-bulk-marked-entries)
9629 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9630 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9631 (org-get-todo-face "TODO")
9632 'evaporate)
9633 (overlay-put ov 'type 'org-marked-entry-overlay))
9634 (end-of-line 1)
9635 (or (ignore-errors
9636 (goto-char (next-single-property-change (point) 'txt)))
9637 (beginning-of-line 2))
9638 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9639 (beginning-of-line 2))
9640 (message "%d entries marked for bulk action"
9641 (length org-agenda-bulk-marked-entries))))))
9643 (defun org-agenda-bulk-mark-all ()
9644 "Mark all entries for future agenda bulk action."
9645 (interactive)
9646 (org-agenda-bulk-mark-regexp "."))
9648 (defun org-agenda-bulk-mark-regexp (regexp)
9649 "Mark entries matching REGEXP for future agenda bulk action."
9650 (interactive "sMark entries matching regexp: ")
9651 (let ((entries-marked 0) txt-at-point)
9652 (save-excursion
9653 (goto-char (point-min))
9654 (goto-char (next-single-property-change (point) 'txt))
9655 (while (and (re-search-forward regexp nil t)
9656 (setq txt-at-point (get-text-property (point) 'txt)))
9657 (when (string-match regexp txt-at-point)
9658 (setq entries-marked (1+ entries-marked))
9659 (call-interactively 'org-agenda-bulk-mark))))
9660 (if (not entries-marked)
9661 (message "No entry matching this regexp."))))
9663 (defun org-agenda-bulk-unmark (&optional arg)
9664 "Unmark the entry at point for future bulk action."
9665 (interactive "P")
9666 (if arg
9667 (org-agenda-bulk-unmark-all)
9668 (cond ((org-agenda-bulk-marked-p)
9669 (org-agenda-bulk-remove-overlays
9670 (point-at-bol) (+ 2 (point-at-bol)))
9671 (setq org-agenda-bulk-marked-entries
9672 (delete (org-get-at-bol 'org-hd-marker)
9673 org-agenda-bulk-marked-entries))
9674 (end-of-line 1)
9675 (or (ignore-errors
9676 (goto-char (next-single-property-change (point) 'txt)))
9677 (beginning-of-line 2))
9678 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9679 (beginning-of-line 2))
9680 (message "%d entries left marked for bulk action"
9681 (length org-agenda-bulk-marked-entries)))
9682 (t (message "No entry to unmark here")))))
9684 (defun org-agenda-bulk-toggle-all ()
9685 "Toggle all marks for bulk action."
9686 (interactive)
9687 (save-excursion
9688 (goto-char (point-min))
9689 (while (ignore-errors
9690 (goto-char (next-single-property-change (point) 'txt)))
9691 (org-agenda-bulk-toggle))))
9693 (defun org-agenda-bulk-toggle ()
9694 "Toggle the mark at point for bulk action."
9695 (interactive)
9696 (if (org-agenda-bulk-marked-p)
9697 (org-agenda-bulk-unmark)
9698 (org-agenda-bulk-mark)))
9700 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9701 "Remove the mark overlays between BEG and END in the agenda buffer.
9702 BEG and END default to the buffer limits.
9704 This only removes the overlays, it does not remove the markers
9705 from the list in `org-agenda-bulk-marked-entries'."
9706 (interactive)
9707 (mapc (lambda (ov)
9708 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9709 (delete-overlay ov)))
9710 (overlays-in (or beg (point-min)) (or end (point-max)))))
9712 (defun org-agenda-bulk-unmark-all ()
9713 "Remove all marks in the agenda buffer.
9714 This will remove the markers and the overlays."
9715 (interactive)
9716 (if (null org-agenda-bulk-marked-entries)
9717 (message "No entry to unmark")
9718 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9719 (setq org-agenda-bulk-marked-entries nil)
9720 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9722 (defcustom org-agenda-persistent-marks nil
9723 "Non-nil means marked items will stay marked after a bulk action.
9724 You can toggle this interactively by typing `p' when prompted for a
9725 bulk action."
9726 :group 'org-agenda
9727 :version "24.1"
9728 :type 'boolean)
9730 (defun org-agenda-bulk-action (&optional arg)
9731 "Execute an remote-editing action on all marked entries.
9732 The prefix arg is passed through to the command if possible."
9733 (interactive "P")
9734 ;; Make sure we have markers, and only valid ones
9735 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9736 (mapc
9737 (lambda (m)
9738 (unless (and (markerp m)
9739 (marker-buffer m)
9740 (buffer-live-p (marker-buffer m))
9741 (marker-position m))
9742 (user-error "Marker %s for bulk command is invalid" m)))
9743 org-agenda-bulk-marked-entries)
9745 ;; Prompt for the bulk command
9746 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9747 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9748 "[S]catter [f]unction "
9749 (when org-agenda-bulk-custom-functions
9750 (concat " Custom: ["
9751 (mapconcat (lambda(f) (char-to-string (car f)))
9752 org-agenda-bulk-custom-functions "")
9753 "]"))))
9754 (catch 'exit
9755 (let* ((action (read-char-exclusive))
9756 (org-log-refile (if org-log-refile 'time nil))
9757 (entries (reverse org-agenda-bulk-marked-entries))
9758 (org-overriding-default-time
9759 (if (get-text-property (point) 'org-agenda-date-header)
9760 (org-get-cursor-date)))
9761 redo-at-end
9762 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9763 (cond
9764 ((equal action ?p)
9765 (let ((org-agenda-persistent-marks
9766 (not org-agenda-persistent-marks)))
9767 (org-agenda-bulk-action)
9768 (throw 'exit nil)))
9770 ((equal action ?$)
9771 (setq cmd '(org-agenda-archive)))
9773 ((equal action ?A)
9774 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9776 ((member action '(?r ?w))
9777 (setq rfloc (org-refile-get-location
9778 "Refile to"
9779 (marker-buffer (car entries))
9780 org-refile-allow-creating-parent-nodes))
9781 (if (nth 3 rfloc)
9782 (setcar (nthcdr 3 rfloc)
9783 (move-marker (make-marker) (nth 3 rfloc)
9784 (or (get-file-buffer (nth 1 rfloc))
9785 (find-buffer-visiting (nth 1 rfloc))
9786 (error "This should not happen")))))
9788 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9789 redo-at-end t))
9791 ((equal action ?t)
9792 (setq state (org-icompleting-read
9793 "Todo state: "
9794 (with-current-buffer (marker-buffer (car entries))
9795 (mapcar 'list org-todo-keywords-1))))
9796 (setq cmd `(let ((org-inhibit-blocking t)
9797 (org-inhibit-logging 'note))
9798 (org-agenda-todo ,state))))
9800 ((memq action '(?- ?+))
9801 (setq tag (org-icompleting-read
9802 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9803 (with-current-buffer (marker-buffer (car entries))
9804 (delq nil
9805 (mapcar (lambda (x)
9806 (if (stringp (car x)) x)) org-tag-alist)))))
9807 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9809 ((memq action '(?s ?d))
9810 (let* ((time
9811 (unless arg
9812 (org-read-date
9813 nil nil nil
9814 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9815 org-overriding-default-time)))
9816 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9817 (setq cmd `(eval '(,c1 arg ,time)))))
9819 ((equal action ?S)
9820 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9821 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9822 (let ((days (read-number
9823 (format "Scatter tasks across how many %sdays: "
9824 (if arg "week" "")) 7)))
9825 (setq cmd
9826 `(let ((distance (1+ (random ,days))))
9827 (if arg
9828 (let ((dist distance)
9829 (day-of-week
9830 (calendar-day-of-week
9831 (calendar-gregorian-from-absolute (org-today)))))
9832 (dotimes (i (1+ dist))
9833 (while (member day-of-week org-agenda-weekend-days)
9834 (incf distance)
9835 (incf day-of-week)
9836 (if (= day-of-week 7)
9837 (setq day-of-week 0)))
9838 (incf day-of-week)
9839 (if (= day-of-week 7)
9840 (setq day-of-week 0)))))
9841 ;; silently fail when try to replan a sexp entry
9842 (condition-case nil
9843 (let* ((date (calendar-gregorian-from-absolute
9844 (+ (org-today) distance)))
9845 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9846 (nth 2 date))))
9847 (org-agenda-schedule nil time))
9848 (error nil)))))))
9850 ((assoc action org-agenda-bulk-custom-functions)
9851 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9852 redo-at-end t))
9854 ((equal action ?f)
9855 (setq cmd (list (intern
9856 (org-icompleting-read "Function: "
9857 obarray 'fboundp t nil nil)))))
9859 (t (user-error "Invalid bulk action")))
9861 ;; Sort the markers, to make sure that parents are handled before children
9862 (setq entries (sort entries
9863 (lambda (a b)
9864 (cond
9865 ((equal (marker-buffer a) (marker-buffer b))
9866 (< (marker-position a) (marker-position b)))
9868 (string< (buffer-name (marker-buffer a))
9869 (buffer-name (marker-buffer b))))))))
9871 ;; Now loop over all markers and apply cmd
9872 (while (setq e (pop entries))
9873 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9874 (if (not pos)
9875 (progn (message "Skipping removed entry at %s" e)
9876 (setq cntskip (1+ cntskip)))
9877 (goto-char pos)
9878 (let (org-loop-over-headlines-in-active-region)
9879 (eval cmd))
9880 (setq cnt (1+ cnt))))
9881 (when redo-at-end (org-agenda-redo))
9882 (unless org-agenda-persistent-marks
9883 (org-agenda-bulk-unmark-all))
9884 (message "Acted on %d entries%s%s"
9886 (if (= cntskip 0)
9888 (format ", skipped %d (disappeared before their turn)"
9889 cntskip))
9890 (if (not org-agenda-persistent-marks)
9891 "" " (kept marked)"))))))
9893 (defun org-agenda-capture (&optional with-time)
9894 "Call `org-capture' with the date at point.
9895 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9896 current HH:MM time."
9897 (interactive "P")
9898 (if (not (eq major-mode 'org-agenda-mode))
9899 (user-error "You cannot do this outside of agenda buffers")
9900 (let ((org-overriding-default-time
9901 (org-get-cursor-date (equal with-time 1))))
9902 (call-interactively 'org-capture))))
9904 ;;; Dragging agenda lines forward/backward
9906 (defun org-agenda-reapply-filters ()
9907 "Re-apply all agenda filters."
9908 (mapcar
9909 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f))))
9910 `((,org-agenda-tag-filter tag)
9911 (,org-agenda-category-filter category)
9912 (,org-agenda-regexp-filter regexp)
9913 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9914 (,(get 'org-agenda-category-filter :preset-filter) category)
9915 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9917 (defun org-agenda-drag-line-forward (arg &optional backward)
9918 "Drag an agenda line forward by ARG lines.
9919 When the optional argument `backward' is non-nil, move backward."
9920 (interactive "p")
9921 (let ((inhibit-read-only t) lst line)
9922 (if (or (not (get-text-property (point) 'txt))
9923 (save-excursion
9924 (dotimes (n arg)
9925 (move-beginning-of-line (if backward 0 2))
9926 (push (not (get-text-property (point) 'txt)) lst))
9927 (delq nil lst)))
9928 (message "Cannot move line forward")
9929 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9930 (move-beginning-of-line 1)
9931 (setq line (buffer-substring (point) end))
9932 (delete-region (point) end)
9933 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9934 (insert line)
9935 (org-agenda-reapply-filters)
9936 (org-agenda-mark-clocking-task)
9937 (move-beginning-of-line 0)))))
9939 (defun org-agenda-drag-line-backward (arg)
9940 "Drag an agenda line backward by ARG lines."
9941 (interactive "p")
9942 (org-agenda-drag-line-forward arg t))
9944 ;;; Flagging notes
9946 (defun org-agenda-show-the-flagging-note ()
9947 "Display the flagging note in the other window.
9948 When called a second time in direct sequence, offer to remove the FLAGGING
9949 tag and (if present) the flagging note."
9950 (interactive)
9951 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9952 (win (selected-window))
9953 note heading newhead)
9954 (unless hdmarker
9955 (user-error "No linked entry at point"))
9956 (if (and (eq this-command last-command)
9957 (y-or-n-p "Unflag and remove any flagging note? "))
9958 (progn
9959 (org-agenda-remove-flag hdmarker)
9960 (let ((win (get-buffer-window "*Flagging Note*")))
9961 (and win (delete-window win)))
9962 (message "Entry unflagged"))
9963 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9964 (unless note
9965 (user-error "No flagging note"))
9966 (org-kill-new note)
9967 (org-switch-to-buffer-other-window "*Flagging Note*")
9968 (erase-buffer)
9969 (insert note)
9970 (goto-char (point-min))
9971 (while (re-search-forward "\\\\n" nil t)
9972 (replace-match "\n" t t))
9973 (goto-char (point-min))
9974 (select-window win)
9975 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9977 (defun org-agenda-remove-flag (marker)
9978 "Remove the FLAGGED tag and any flagging note in the entry."
9979 (let (newhead)
9980 (org-with-point-at marker
9981 (org-toggle-tag "FLAGGED" 'off)
9982 (org-entry-delete nil "THEFLAGGINGNOTE")
9983 (setq newhead (org-get-heading)))
9984 (org-agenda-change-all-lines newhead marker)
9985 (message "Entry unflagged")))
9987 (defun org-agenda-get-any-marker (&optional pos)
9988 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9989 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9991 ;;; Appointment reminders
9993 (defvar appt-time-msg-list) ; defined in appt.el
9995 ;;;###autoload
9996 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9997 "Activate appointments found in `org-agenda-files'.
9998 With a \\[universal-argument] prefix, refresh the list of
9999 appointments.
10001 If FILTER is t, interactively prompt the user for a regular
10002 expression, and filter out entries that don't match it.
10004 If FILTER is a string, use this string as a regular expression
10005 for filtering entries out.
10007 If FILTER is a function, filter out entries against which
10008 calling the function returns nil. This function takes one
10009 argument: an entry from `org-agenda-get-day-entries'.
10011 FILTER can also be an alist with the car of each cell being
10012 either 'headline or 'category. For example:
10014 '((headline \"IMPORTANT\")
10015 (category \"Work\"))
10017 will only add headlines containing IMPORTANT or headlines
10018 belonging to the \"Work\" category.
10020 ARGS are symbols indicating what kind of entries to consider.
10021 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10022 \(i.e., deadlines and scheduled items with a hh:mm specification)
10023 and :timestamp entries. See the docstring of `org-diary' for
10024 details and examples.
10026 If an entry has a APPT_WARNTIME property, its value will be used
10027 to override `appt-message-warning-time'."
10028 (interactive "P")
10029 (if refresh (setq appt-time-msg-list nil))
10030 (if (eq filter t)
10031 (setq filter (read-from-minibuffer "Regexp filter: ")))
10032 (let* ((cnt 0) ; count added events
10033 (scope (or args '(:deadline* :scheduled* :timestamp)))
10034 (org-agenda-new-buffers nil)
10035 (org-deadline-warning-days 0)
10036 ;; Do not use `org-today' here because appt only takes
10037 ;; time and without date as argument, so it may pass wrong
10038 ;; information otherwise
10039 (today (org-date-to-gregorian
10040 (time-to-days (current-time))))
10041 (org-agenda-restrict nil)
10042 (files (org-agenda-files 'unrestricted)) entries file
10043 (org-agenda-buffer nil))
10044 ;; Get all entries which may contain an appt
10045 (org-agenda-prepare-buffers files)
10046 (while (setq file (pop files))
10047 (setq entries
10048 (delq nil
10049 (append entries
10050 (apply 'org-agenda-get-day-entries
10051 file today scope)))))
10052 ;; Map thru entries and find if we should filter them out
10053 (mapc
10054 (lambda(x)
10055 (let* ((evt (org-trim
10056 (replace-regexp-in-string
10057 org-bracket-link-regexp "\\3"
10058 (or (get-text-property 1 'txt x) ""))))
10059 (cat (get-text-property 1 'org-category x))
10060 (tod (get-text-property 1 'time-of-day x))
10061 (ok (or (null filter)
10062 (and (stringp filter) (string-match filter evt))
10063 (and (functionp filter) (funcall filter x))
10064 (and (listp filter)
10065 (let ((cat-filter (cadr (assoc 'category filter)))
10066 (evt-filter (cadr (assoc 'headline filter))))
10067 (or (and (stringp cat-filter)
10068 (string-match cat-filter cat))
10069 (and (stringp evt-filter)
10070 (string-match evt-filter evt)))))))
10071 (wrn (get-text-property 1 'warntime x)))
10072 ;; FIXME: Shall we remove text-properties for the appt text?
10073 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10074 (when (and ok tod)
10075 (setq tod (concat "00" (number-to-string tod))
10076 tod (when (string-match
10077 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10078 (concat (match-string 1 tod) ":"
10079 (match-string 2 tod))))
10080 (if (version< emacs-version "23.3")
10081 (appt-add tod evt)
10082 (appt-add tod evt wrn))
10083 (setq cnt (1+ cnt))))) entries)
10084 (org-release-buffers org-agenda-new-buffers)
10085 (if (eq cnt 0)
10086 (message "No event to add")
10087 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10089 (defun org-agenda-todayp (date)
10090 "Does DATE mean today, when considering `org-extend-today-until'?"
10091 (let ((today (org-today))
10092 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10093 date)))
10094 (eq date today)))
10096 (defun org-agenda-todo-yesterday (&optional arg)
10097 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10098 (interactive "P")
10099 (let* ((hour (third (decode-time
10100 (org-current-time))))
10101 (org-extend-today-until (1+ hour)))
10102 (org-agenda-todo arg)))
10104 (provide 'org-agenda)
10106 ;;; org-agenda.el ends here