Fix agenda generation error if agenda had time grid and agenda format
[org-mode.git] / lisp / org-agenda.el
blob72ff2a7702588568f23e5b4a19b14c4ab854375c
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2013 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 'string)
232 (defcustom org-agenda-persistent-filter nil
233 "When set, keep filters from one agenda view to the next."
234 :group 'org-agenda
235 :type 'boolean)
237 (defgroup org-agenda-custom-commands nil
238 "Options concerning agenda views in Org-mode."
239 :tag "Org Agenda Custom Commands"
240 :group 'org-agenda)
242 (defconst org-sorting-choice
243 '(choice
244 (const time-up) (const time-down)
245 (const timestamp-up) (const timestamp-down)
246 (const scheduled-up) (const scheduled-down)
247 (const deadline-up) (const deadline-down)
248 (const ts-up) (const ts-down)
249 (const tsia-up) (const tsia-down)
250 (const category-keep) (const category-up) (const category-down)
251 (const tag-down) (const tag-up)
252 (const priority-up) (const priority-down)
253 (const todo-state-up) (const todo-state-down)
254 (const effort-up) (const effort-down)
255 (const habit-up) (const habit-down)
256 (const alpha-up) (const alpha-down)
257 (const user-defined-up) (const user-defined-down))
258 "Sorting choices.")
260 ;; Keep custom values for `org-agenda-filter-preset' compatible with
261 ;; the new variable `org-agenda-tag-filter-preset'.
262 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
263 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
265 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
266 "List of types searched for when creating the daily/weekly agenda.
267 This variable is a list of symbols that controls the types of
268 items that appear in the daily/weekly agenda. Allowed symbols in this
269 list are are
271 :timestamp List items containing a date stamp or date range matching
272 the selected date. This includes sexp entries in angular
273 brackets.
275 :sexp List entries resulting from plain diary-like sexps.
277 :deadline List deadline due on that date. When the date is today,
278 also list any deadlines past due, or due within
279 `org-deadline-warning-days'. `:deadline' must appear before
280 `:scheduled' if the setting of
281 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
282 any effect.
284 :deadline* Same as above, but only include the deadline if it has an
285 hour specification as [h]h:mm.
287 :scheduled List all items which are scheduled for the given date.
288 The diary for *today* also contains items which were
289 scheduled earlier and are not yet marked DONE.
291 :scheduled* Same as above, but only include the scheduled item if it
292 has an hour specification as [h]h:mm.
294 By default, all four non-starred types are turned on.
296 When :scheduled* or :deadline* are included, :schedule or :deadline
297 will be ignored.
299 Never set this variable globally using `setq', because then it
300 will apply to all future agenda commands. Instead, bind it with
301 `let' to scope it dynamically into the agenda-constructing
302 command. A good way to set it is through options in
303 `org-agenda-custom-commands'. For a more flexible (though
304 somewhat less efficient) way of determining what is included in
305 the daily/weekly agenda, see `org-agenda-skip-function'.")
307 (defconst org-agenda-custom-commands-local-options
308 `(repeat :tag "Local settings for this command. Remember to quote values"
309 (choice :tag "Setting"
310 (list :tag "Heading for this block"
311 (const org-agenda-overriding-header)
312 (string :tag "Headline"))
313 (list :tag "Files to be searched"
314 (const org-agenda-files)
315 (list
316 (const :format "" quote)
317 (repeat (file))))
318 (list :tag "Sorting strategy"
319 (const org-agenda-sorting-strategy)
320 (list
321 (const :format "" quote)
322 (repeat
323 ,org-sorting-choice)))
324 (list :tag "Prefix format"
325 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
326 (string))
327 (list :tag "Number of days in agenda"
328 (const org-agenda-span)
329 (choice (const :tag "Day" 'day)
330 (const :tag "Week" 'week)
331 (const :tag "Month" 'month)
332 (const :tag "Year" 'year)
333 (integer :tag "Custom")))
334 (list :tag "Fixed starting date"
335 (const org-agenda-start-day)
336 (string :value "2007-11-01"))
337 (list :tag "Start on day of week"
338 (const org-agenda-start-on-weekday)
339 (choice :value 1
340 (const :tag "Today" nil)
341 (integer :tag "Weekday No.")))
342 (list :tag "Include data from diary"
343 (const org-agenda-include-diary)
344 (boolean))
345 (list :tag "Deadline Warning days"
346 (const org-deadline-warning-days)
347 (integer :value 1))
348 (list :tag "Category filter preset"
349 (const org-agenda-category-filter-preset)
350 (list
351 (const :format "" quote)
352 (repeat
353 (string :tag "+category or -category"))))
354 (list :tag "Tags filter preset"
355 (const org-agenda-tag-filter-preset)
356 (list
357 (const :format "" quote)
358 (repeat
359 (string :tag "+tag or -tag"))))
360 (list :tag "Regexp filter preset"
361 (const org-agenda-regexp-filter-preset)
362 (list
363 (const :format "" quote)
364 (repeat
365 (string :tag "+regexp or -regexp"))))
366 (list :tag "Set daily/weekly entry types"
367 (const org-agenda-entry-types)
368 (list
369 (const :format "" quote)
370 (set :greedy t :value ,org-agenda-entry-types
371 (const :deadline)
372 (const :scheduled)
373 (const :deadline*)
374 (const :scheduled*)
375 (const :timestamp)
376 (const :sexp))))
377 (list :tag "Standard skipping condition"
378 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
379 (const org-agenda-skip-function)
380 (list
381 (const :format "" quote)
382 (list
383 (choice
384 :tag "Skipping range"
385 (const :tag "Skip entry" org-agenda-skip-entry-if)
386 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
387 (repeat :inline t :tag "Conditions for skipping"
388 (choice
389 :tag "Condition type"
390 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
391 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
392 (list :tag "TODO state is" :inline t
393 (const 'todo)
394 (choice
395 (const :tag "any not-done state" 'todo)
396 (const :tag "any done state" 'done)
397 (const :tag "any state" 'any)
398 (list :tag "Keyword list"
399 (const :format "" quote)
400 (repeat (string :tag "Keyword")))))
401 (list :tag "TODO state is not" :inline t
402 (const 'nottodo)
403 (choice
404 (const :tag "any not-done state" 'todo)
405 (const :tag "any done state" 'done)
406 (const :tag "any state" 'any)
407 (list :tag "Keyword list"
408 (const :format "" quote)
409 (repeat (string :tag "Keyword")))))
410 (const :tag "scheduled" 'scheduled)
411 (const :tag "not scheduled" 'notscheduled)
412 (const :tag "deadline" 'deadline)
413 (const :tag "no deadline" 'notdeadline)
414 (const :tag "timestamp" 'timestamp)
415 (const :tag "no timestamp" 'nottimestamp))))))
416 (list :tag "Non-standard skipping condition"
417 :value (org-agenda-skip-function)
418 (const org-agenda-skip-function)
419 (sexp :tag "Function or form (quoted!)"))
420 (list :tag "Any variable"
421 (variable :tag "Variable")
422 (sexp :tag "Value (sexp)"))))
423 "Selection of examples for agenda command settings.
424 This will be spliced into the custom type of
425 `org-agenda-custom-commands'.")
428 (defcustom org-agenda-custom-commands
429 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
430 "Custom commands for the agenda.
431 These commands will be offered on the splash screen displayed by the
432 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
434 (key desc type match settings files)
436 key The key (one or more characters as a string) to be associated
437 with the command.
438 desc A description of the command, when omitted or nil, a default
439 description is built using MATCH.
440 type The command type, any of the following symbols:
441 agenda The daily/weekly agenda.
442 todo Entries with a specific TODO keyword, in all agenda files.
443 search Entries containing search words entry or headline.
444 tags Tags/Property/TODO match in all agenda files.
445 tags-todo Tags/P/T match in all agenda files, TODO entries only.
446 todo-tree Sparse tree of specific TODO keyword in *current* file.
447 tags-tree Sparse tree with all tags matches in *current* file.
448 occur-tree Occur sparse tree for *current* file.
449 ... A user-defined function.
450 match What to search for:
451 - a single keyword for TODO keyword searches
452 - a tags match expression for tags searches
453 - a word search expression for text searches.
454 - a regular expression for occur searches
455 For all other commands, this should be the empty string.
456 settings A list of option settings, similar to that in a let form, so like
457 this: ((opt1 val1) (opt2 val2) ...). The values will be
458 evaluated at the moment of execution, so quote them when needed.
459 files A list of files file to write the produced agenda buffer to
460 with the command `org-store-agenda-views'.
461 If a file name ends in \".html\", an HTML version of the buffer
462 is written out. If it ends in \".ps\", a postscript version is
463 produced. Otherwise, only the plain text is written to the file.
465 You can also define a set of commands, to create a composite agenda buffer.
466 In this case, an entry looks like this:
468 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
470 where
472 desc A description string to be displayed in the dispatcher menu.
473 cmd An agenda command, similar to the above. However, tree commands
474 are not allowed, but instead you can get agenda and global todo list.
475 So valid commands for a set are:
476 (agenda \"\" settings)
477 (alltodo \"\" settings)
478 (stuck \"\" settings)
479 (todo \"match\" settings files)
480 (search \"match\" settings files)
481 (tags \"match\" settings files)
482 (tags-todo \"match\" settings files)
484 Each command can carry a list of options, and another set of options can be
485 given for the whole set of commands. Individual command options take
486 precedence over the general options.
488 When using several characters as key to a command, the first characters
489 are prefix commands. For the dispatcher to display useful information, you
490 should provide a description for the prefix, like
492 (setq org-agenda-custom-commands
493 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
494 (\"hl\" tags \"+HOME+Lisa\")
495 (\"hp\" tags \"+HOME+Peter\")
496 (\"hk\" tags \"+HOME+Kim\")))"
497 :group 'org-agenda-custom-commands
498 :type `(repeat
499 (choice :value ("x" "Describe command here" tags "" nil)
500 (list :tag "Single command"
501 (string :tag "Access Key(s) ")
502 (option (string :tag "Description"))
503 (choice
504 (const :tag "Agenda" agenda)
505 (const :tag "TODO list" alltodo)
506 (const :tag "Search words" search)
507 (const :tag "Stuck projects" stuck)
508 (const :tag "Tags/Property match (all agenda files)" tags)
509 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
510 (const :tag "TODO keyword search (all agenda files)" todo)
511 (const :tag "Tags sparse tree (current buffer)" tags-tree)
512 (const :tag "TODO keyword tree (current buffer)" todo-tree)
513 (const :tag "Occur tree (current buffer)" occur-tree)
514 (sexp :tag "Other, user-defined function"))
515 (string :tag "Match (only for some commands)")
516 ,org-agenda-custom-commands-local-options
517 (option (repeat :tag "Export" (file :tag "Export to"))))
518 (list :tag "Command series, all agenda files"
519 (string :tag "Access Key(s)")
520 (string :tag "Description ")
521 (repeat :tag "Component"
522 (choice
523 (list :tag "Agenda"
524 (const :format "" agenda)
525 (const :tag "" :format "" "")
526 ,org-agenda-custom-commands-local-options)
527 (list :tag "TODO list (all keywords)"
528 (const :format "" alltodo)
529 (const :tag "" :format "" "")
530 ,org-agenda-custom-commands-local-options)
531 (list :tag "Search words"
532 (const :format "" search)
533 (string :tag "Match")
534 ,org-agenda-custom-commands-local-options)
535 (list :tag "Stuck projects"
536 (const :format "" stuck)
537 (const :tag "" :format "" "")
538 ,org-agenda-custom-commands-local-options)
539 (list :tag "Tags search"
540 (const :format "" tags)
541 (string :tag "Match")
542 ,org-agenda-custom-commands-local-options)
543 (list :tag "Tags search, TODO entries only"
544 (const :format "" tags-todo)
545 (string :tag "Match")
546 ,org-agenda-custom-commands-local-options)
547 (list :tag "TODO keyword search"
548 (const :format "" todo)
549 (string :tag "Match")
550 ,org-agenda-custom-commands-local-options)
551 (list :tag "Other, user-defined function"
552 (symbol :tag "function")
553 (string :tag "Match")
554 ,org-agenda-custom-commands-local-options)))
556 (repeat :tag "Settings for entire command set"
557 (list (variable :tag "Any variable")
558 (sexp :tag "Value")))
559 (option (repeat :tag "Export" (file :tag "Export to"))))
560 (cons :tag "Prefix key documentation"
561 (string :tag "Access Key(s)")
562 (string :tag "Description ")))))
564 (defcustom org-agenda-query-register ?o
565 "The register holding the current query string.
566 The purpose of this is that if you construct a query string interactively,
567 you can then use it to define a custom command."
568 :group 'org-agenda-custom-commands
569 :type 'character)
571 (defcustom org-stuck-projects
572 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
573 "How to identify stuck projects.
574 This is a list of four items:
575 1. A tags/todo/property matcher string that is used to identify a project.
576 See the manual for a description of tag and property searches.
577 The entire tree below a headline matched by this is considered one project.
578 2. A list of TODO keywords identifying non-stuck projects.
579 If the project subtree contains any headline with one of these todo
580 keywords, the project is considered to be not stuck. If you specify
581 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
582 3. A list of tags identifying non-stuck projects.
583 If the project subtree contains any headline with one of these tags,
584 the project is considered to be not stuck. If you specify \"*\" as
585 a tag, any tag will mark the project unstuck. Note that this is about
586 the explicit presence of a tag somewhere in the subtree, inherited
587 tags do not count here. If inherited tags make a project not stuck,
588 use \"-TAG\" in the tags part of the matcher under (1.) above.
589 4. An arbitrary regular expression matching non-stuck projects.
591 If the project turns out to be not stuck, search continues also in the
592 subtree to see if any of the subtasks have project status.
594 See also the variable `org-tags-match-list-sublevels' which applies
595 to projects matched by this search as well.
597 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
598 or `C-c a #' to produce the list."
599 :group 'org-agenda-custom-commands
600 :type '(list
601 (string :tag "Tags/TODO match to identify a project")
602 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
603 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
604 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
606 (defcustom org-agenda-filter-effort-default-operator "<"
607 "The default operator for effort estimate filtering.
608 If you select an effort estimate limit without first pressing an operator,
609 this one will be used."
610 :group 'org-agenda-custom-commands
611 :type '(choice (const :tag "less or equal" "<")
612 (const :tag "greater or equal"">")
613 (const :tag "equal" "=")))
615 (defgroup org-agenda-skip nil
616 "Options concerning skipping parts of agenda files."
617 :tag "Org Agenda Skip"
618 :group 'org-agenda)
620 (defcustom org-agenda-skip-function-global nil
621 "Function to be called at each match during agenda construction.
622 If this function returns nil, the current match should not be skipped.
623 If the function decided to skip an agenda match, is must return the
624 buffer position from which the search should be continued.
625 This may also be a Lisp form, which will be evaluated.
627 This variable will be applied to every agenda match, including
628 tags/property searches and TODO lists. So try to make the test function
629 do its checking as efficiently as possible. To implement a skipping
630 condition just for specific agenda commands, use the variable
631 `org-agenda-skip-function' which can be set in the options section
632 of custom agenda commands."
633 :group 'org-agenda-skip
634 :type 'sexp)
636 (defgroup org-agenda-daily/weekly nil
637 "Options concerning the daily/weekly agenda."
638 :tag "Org Agenda Daily/Weekly"
639 :group 'org-agenda)
640 (defgroup org-agenda-todo-list nil
641 "Options concerning the global todo list agenda view."
642 :tag "Org Agenda Todo List"
643 :group 'org-agenda)
644 (defgroup org-agenda-match-view nil
645 "Options concerning the general tags/property/todo match agenda view."
646 :tag "Org Agenda Match View"
647 :group 'org-agenda)
648 (defgroup org-agenda-search-view nil
649 "Options concerning the general tags/property/todo match agenda view."
650 :tag "Org Agenda Match View"
651 :group 'org-agenda)
653 (defvar org-agenda-archives-mode nil
654 "Non-nil means the agenda will include archived items.
655 If this is the symbol `trees', trees in the selected agenda scope
656 that are marked with the ARCHIVE tag will be included anyway. When this is
657 t, also all archive files associated with the current selection of agenda
658 files will be included.")
660 (defcustom org-agenda-skip-comment-trees t
661 "Non-nil means skip trees that start with the COMMENT keyword.
662 When nil, these trees are also scanned by agenda commands."
663 :group 'org-agenda-skip
664 :type 'boolean)
666 (defcustom org-agenda-todo-list-sublevels t
667 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
668 When nil, the sublevels of a TODO entry are not checked, resulting in
669 potentially much shorter TODO lists."
670 :group 'org-agenda-skip
671 :group 'org-agenda-todo-list
672 :type 'boolean)
674 (defcustom org-agenda-todo-ignore-with-date nil
675 "Non-nil means don't show entries with a date in the global todo list.
676 You can use this if you prefer to mark mere appointments with a TODO keyword,
677 but don't want them to show up in the TODO list.
678 When this is set, it also covers deadlines and scheduled items, the settings
679 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
680 will be ignored.
681 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
682 :group 'org-agenda-skip
683 :group 'org-agenda-todo-list
684 :type 'boolean)
686 (defcustom org-agenda-todo-ignore-timestamp nil
687 "Non-nil means don't show entries with a timestamp.
688 This applies when creating the global todo list.
689 Valid values are:
691 past Don't show entries for today or in the past.
693 future Don't show entries with a timestamp in the future.
694 The idea behind this is that if it has a future
695 timestamp, you don't want to think about it until the
696 date.
698 all Don't show any entries with a timestamp in the global todo list.
699 The idea behind this is that by setting a timestamp, you
700 have already \"taken care\" of this item.
702 This variable can also have an integer as a value. If positive (N),
703 todos with a timestamp N or more days in the future will be ignored. If
704 negative (-N), todos with a timestamp N or more days in the past will be
705 ignored. If 0, todos with a timestamp either today or in the future will
706 be ignored. For example, a value of -1 will exclude todos with a
707 timestamp in the past (yesterday or earlier), while a value of 7 will
708 exclude todos with a timestamp a week or more in the future.
710 See also `org-agenda-todo-ignore-with-date'.
711 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
712 to make his option also apply to the tags-todo list."
713 :group 'org-agenda-skip
714 :group 'org-agenda-todo-list
715 :version "24.1"
716 :type '(choice
717 (const :tag "Ignore future timestamp todos" future)
718 (const :tag "Ignore past or present timestamp todos" past)
719 (const :tag "Ignore all timestamp todos" all)
720 (const :tag "Show timestamp todos" nil)
721 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
723 (defcustom org-agenda-todo-ignore-scheduled nil
724 "Non-nil means, ignore some scheduled TODO items when making TODO list.
725 This applies when creating the global todo list.
726 Valid values are:
728 past Don't show entries scheduled today or in the past.
730 future Don't show entries scheduled in the future.
731 The idea behind this is that by scheduling it, you don't want to
732 think about it until the scheduled date.
734 all Don't show any scheduled entries in the global todo list.
735 The idea behind this is that by scheduling it, you have already
736 \"taken care\" of this item.
738 t Same as `all', for backward compatibility.
740 This variable can also have an integer as a value. See
741 `org-agenda-todo-ignore-timestamp' for more details.
743 See also `org-agenda-todo-ignore-with-date'.
744 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
745 to make his option also apply to the tags-todo list."
746 :group 'org-agenda-skip
747 :group 'org-agenda-todo-list
748 :type '(choice
749 (const :tag "Ignore future-scheduled todos" future)
750 (const :tag "Ignore past- or present-scheduled todos" past)
751 (const :tag "Ignore all scheduled todos" all)
752 (const :tag "Ignore all scheduled todos (compatibility)" t)
753 (const :tag "Show scheduled todos" nil)
754 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
756 (defcustom org-agenda-todo-ignore-deadlines nil
757 "Non-nil means ignore some deadlined TODO items when making TODO list.
758 There are different motivations for using different values, please think
759 carefully when configuring this variable.
761 This applies when creating the global todo list.
762 Valid values are:
764 near Don't show near deadline entries. A deadline is near when it is
765 closer than `org-deadline-warning-days' days. The idea behind this
766 is that such items will appear in the agenda anyway.
768 far Don't show TODO entries where a deadline has been defined, but
769 the deadline is not near. This is useful if you don't want to
770 use the todo list to figure out what to do now.
772 past Don't show entries with a deadline timestamp for today or in the past.
774 future Don't show entries with a deadline timestamp in the future, not even
775 when they become `near' ones. Use it with caution.
777 all Ignore all TODO entries that do have a deadline.
779 t Same as `near', for backward compatibility.
781 This variable can also have an integer as a value. See
782 `org-agenda-todo-ignore-timestamp' for more details.
784 See also `org-agenda-todo-ignore-with-date'.
785 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
786 to make his option also apply to the tags-todo list."
787 :group 'org-agenda-skip
788 :group 'org-agenda-todo-list
789 :type '(choice
790 (const :tag "Ignore near deadlines" near)
791 (const :tag "Ignore near deadlines (compatibility)" t)
792 (const :tag "Ignore far deadlines" far)
793 (const :tag "Ignore all TODOs with a deadlines" all)
794 (const :tag "Show all TODOs, even if they have a deadline" nil)
795 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
797 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
798 "Time unit to use when possibly ignoring an agenda item.
800 See the docstring of various `org-agenda-todo-ignore-*' options.
801 The default is to compare time stamps using days. An item is thus
802 considered to be in the future if it is at least one day after today.
803 Non-nil means to compare time stamps using seconds. An item is then
804 considered future if it has a time value later than current time."
805 :group 'org-agenda-skip
806 :group 'org-agenda-todo-list
807 :version "24.4"
808 :package-version '(Org . "8.0")
809 :type '(choice
810 (const :tag "Compare time with days" nil)
811 (const :tag "Compare time with seconds" t)))
813 (defcustom org-agenda-tags-todo-honor-ignore-options nil
814 "Non-nil means honor todo-list ignores options also in tags-todo search.
815 The variables
816 `org-agenda-todo-ignore-with-date',
817 `org-agenda-todo-ignore-timestamp',
818 `org-agenda-todo-ignore-scheduled',
819 `org-agenda-todo-ignore-deadlines'
820 make the global TODO list skip entries that have time stamps of certain
821 kinds. If this option is set, the same options will also apply for the
822 tags-todo search, which is the general tags/property matcher
823 restricted to unfinished TODO entries only."
824 :group 'org-agenda-skip
825 :group 'org-agenda-todo-list
826 :group 'org-agenda-match-view
827 :type 'boolean)
829 (defcustom org-agenda-skip-scheduled-if-done nil
830 "Non-nil means don't show scheduled items in agenda when they are done.
831 This is relevant for the daily/weekly agenda, not for the TODO list. And
832 it applies only to the actual date of the scheduling. Warnings about
833 an item with a past scheduling dates are always turned off when the item
834 is DONE."
835 :group 'org-agenda-skip
836 :group 'org-agenda-daily/weekly
837 :type 'boolean)
839 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
840 "Non-nil means skip scheduling line if same entry shows because of deadline.
842 In the agenda of today, an entry can show up multiple times
843 because it is both scheduled and has a nearby deadline, and maybe
844 a plain time stamp as well.
846 When this variable is nil, the entry will be shown several times.
848 When set to t, then only the deadline is shown and the fact that
849 the entry is scheduled today or was scheduled previously is not
850 shown.
852 When set to the symbol `not-today', skip scheduled previously,
853 but not scheduled today.
855 When set to the symbol `repeated-after-deadline', skip scheduled
856 items if they are repeated beyond the current dealine."
857 :group 'org-agenda-skip
858 :group 'org-agenda-daily/weekly
859 :type '(choice
860 (const :tag "Never" nil)
861 (const :tag "Always" t)
862 (const :tag "Not when scheduled today" not-today)
863 (const :tag "When repeated past deadline" repeated-after-deadline)))
865 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
866 "Non-nil means skip timestamp line if same entry shows because of deadline.
867 In the agenda of today, an entry can show up multiple times
868 because it has both a plain timestamp and has a nearby deadline.
869 When this variable is t, then only the deadline is shown and the
870 fact that the entry has a timestamp for or including today is not
871 shown. When this variable is nil, the entry will be shown
872 several times."
873 :group 'org-agenda-skip
874 :group 'org-agenda-daily/weekly
875 :version "24.1"
876 :type '(choice
877 (const :tag "Never" nil)
878 (const :tag "Always" t)))
880 (defcustom org-agenda-skip-deadline-if-done nil
881 "Non-nil means don't show deadlines when the corresponding item is done.
882 When nil, the deadline is still shown and should give you a happy feeling.
883 This is relevant for the daily/weekly agenda. And it applied only to the
884 actually date of the deadline. Warnings about approaching and past-due
885 deadlines are always turned off when the item is DONE."
886 :group 'org-agenda-skip
887 :group 'org-agenda-daily/weekly
888 :type 'boolean)
890 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
891 "Non-nil means skip deadline prewarning when entry is also scheduled.
892 This will apply on all days where a prewarning for the deadline would
893 be shown, but not at the day when the entry is actually due. On that day,
894 the deadline will be shown anyway.
895 This variable may be set to nil, t, the symbol `pre-scheduled',
896 or a number which will then give the number of days before the actual
897 deadline when the prewarnings should resume. The symbol `pre-scheduled'
898 eliminates the deadline prewarning only prior to the scheduled date.
899 This can be used in a workflow where the first showing of the deadline will
900 trigger you to schedule it, and then you don't want to be reminded of it
901 because you will take care of it on the day when scheduled."
902 :group 'org-agenda-skip
903 :group 'org-agenda-daily/weekly
904 :version "24.1"
905 :type '(choice
906 (const :tag "Always show prewarning" nil)
907 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
908 (const :tag "Remove prewarning if entry is scheduled" t)
909 (integer :tag "Restart prewarning N days before deadline")))
911 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
912 "Non-nil means skip scheduled delay when entry also has a deadline.
913 This variable may be set to nil, t, the symbol `post-deadline',
914 or a number which will then give the number of days after the actual
915 scheduled date when the delay should expire. The symbol `post-deadline'
916 eliminates the schedule delay when the date is posterior to the deadline."
917 :group 'org-agenda-skip
918 :group 'org-agenda-daily/weekly
919 :version "24.4"
920 :package-version '(Org . "8.0")
921 :type '(choice
922 (const :tag "Always honor delay" nil)
923 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
924 (const :tag "Ignore delay if entry has a deadline" t)
925 (integer :tag "Honor delay up until N days after the scheduled date")))
927 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
928 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
929 When non-nil, after the search for timestamps has matched once in an
930 entry, the rest of the entry will not be searched."
931 :group 'org-agenda-skip
932 :type 'boolean)
934 (defcustom org-agenda-skip-timestamp-if-done nil
935 "Non-nil means don't select item by timestamp or -range if it is DONE."
936 :group 'org-agenda-skip
937 :group 'org-agenda-daily/weekly
938 :type 'boolean)
940 (defcustom org-agenda-dim-blocked-tasks t
941 "Non-nil means dim blocked tasks in the agenda display.
942 This causes some overhead during agenda construction, but if you
943 have turned on `org-enforce-todo-dependencies',
944 `org-enforce-todo-checkbox-dependencies', or any other blocking
945 mechanism, this will create useful feedback in the agenda.
947 Instead of t, this variable can also have the value `invisible'.
948 Then blocked tasks will be invisible and only become visible when
949 they become unblocked. An exemption to this behavior is when a task is
950 blocked because of unchecked checkboxes below it. Since checkboxes do
951 not show up in the agenda views, making this task invisible you remove any
952 trace from agenda views that there is something to do. Therefore, a task
953 that is blocked because of checkboxes will never be made invisible, it
954 will only be dimmed."
955 :group 'org-agenda-daily/weekly
956 :group 'org-agenda-todo-list
957 :version "24.3"
958 :type '(choice
959 (const :tag "Do not dim" nil)
960 (const :tag "Dim to a gray face" t)
961 (const :tag "Make invisible" invisible)))
963 (defcustom org-timeline-show-empty-dates 3
964 "Non-nil means `org-timeline' also shows dates without an entry.
965 When nil, only the days which actually have entries are shown.
966 When t, all days between the first and the last date are shown.
967 When an integer, show also empty dates, but if there is a gap of more than
968 N days, just insert a special line indicating the size of the gap."
969 :group 'org-agenda-skip
970 :type '(choice
971 (const :tag "None" nil)
972 (const :tag "All" t)
973 (integer :tag "at most")))
975 (defgroup org-agenda-startup nil
976 "Options concerning initial settings in the Agenda in Org Mode."
977 :tag "Org Agenda Startup"
978 :group 'org-agenda)
980 (defcustom org-agenda-menu-show-matcher t
981 "Non-nil means show the match string in the agenda dispatcher menu.
982 When nil, the matcher string is not shown, but is put into the help-echo
983 property so than moving the mouse over the command shows it.
984 Setting it to nil is good if matcher strings are very long and/or if
985 you want to use two-columns display (see `org-agenda-menu-two-columns')."
986 :group 'org-agenda
987 :version "24.1"
988 :type 'boolean)
990 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
992 (defcustom org-agenda-menu-two-columns nil
993 "Non-nil means, use two columns to show custom commands in the dispatcher.
994 If you use this, you probably want to set `org-agenda-menu-show-matcher'
995 to nil."
996 :group 'org-agenda
997 :version "24.1"
998 :type 'boolean)
1000 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1001 (defcustom org-agenda-finalize-hook nil
1002 "Hook run just before displaying an agenda buffer.
1003 The buffer is still writable when the hook is called.
1005 You can modify some of the buffer substrings but you should be
1006 extra careful not to modify the text properties of the agenda
1007 headlines as the agenda display heavily relies on them."
1008 :group 'org-agenda-startup
1009 :type 'hook)
1011 (defcustom org-agenda-mouse-1-follows-link nil
1012 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1013 A longer mouse click will still set point. Does not work on XEmacs.
1014 Needs to be set before org.el is loaded."
1015 :group 'org-agenda-startup
1016 :type 'boolean)
1018 (defcustom org-agenda-start-with-follow-mode nil
1019 "The initial value of follow mode in a newly created agenda window."
1020 :group 'org-agenda-startup
1021 :type 'boolean)
1023 (defcustom org-agenda-follow-indirect nil
1024 "Non-nil means `org-agenda-follow-mode' displays only the
1025 current item's tree, in an indirect buffer."
1026 :group 'org-agenda
1027 :version "24.1"
1028 :type 'boolean)
1030 (defcustom org-agenda-show-outline-path t
1031 "Non-nil means show outline path in echo area after line motion."
1032 :group 'org-agenda-startup
1033 :type 'boolean)
1035 (defcustom org-agenda-start-with-entry-text-mode nil
1036 "The initial value of entry-text-mode in a newly created agenda window."
1037 :group 'org-agenda-startup
1038 :type 'boolean)
1040 (defcustom org-agenda-entry-text-maxlines 5
1041 "Number of text lines to be added when `E' is pressed in the agenda.
1043 Note that this variable only used during agenda display. Add add entry text
1044 when exporting the agenda, configure the variable
1045 `org-agenda-add-entry-ext-maxlines'."
1046 :group 'org-agenda
1047 :type 'integer)
1049 (defcustom org-agenda-entry-text-exclude-regexps nil
1050 "List of regular expressions to clean up entry text.
1051 The complete matches of all regular expressions in this list will be
1052 removed from entry text before it is shown in the agenda."
1053 :group 'org-agenda
1054 :type '(repeat (regexp)))
1056 (defcustom org-agenda-entry-text-leaders " > "
1057 "Text prepended to the entry text in agenda buffers."
1058 :version "24.4"
1059 :package-version '(Org . "8.0")
1060 :group 'org-agenda
1061 :type 'string)
1063 (defvar org-agenda-entry-text-cleanup-hook nil
1064 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1065 This cleanup is done in a temporary buffer, so the function may inspect and
1066 change the entire buffer.
1067 Some default stuff like drawers and scheduling/deadline dates will already
1068 have been removed when this is called, as will any matches for regular
1069 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1071 (defvar org-agenda-include-inactive-timestamps nil
1072 "Non-nil means include inactive time stamps in agenda and timeline.
1073 Dynamically scoped.")
1075 (defgroup org-agenda-windows nil
1076 "Options concerning the windows used by the Agenda in Org Mode."
1077 :tag "Org Agenda Windows"
1078 :group 'org-agenda)
1080 (defcustom org-agenda-window-setup 'reorganize-frame
1081 "How the agenda buffer should be displayed.
1082 Possible values for this option are:
1084 current-window Show agenda in the current window, keeping all other windows.
1085 other-window Use `switch-to-buffer-other-window' to display agenda.
1086 reorganize-frame Show only two windows on the current frame, the current
1087 window and the agenda.
1088 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1089 Also, when exiting the agenda, kill that frame.
1090 See also the variable `org-agenda-restore-windows-after-quit'."
1091 :group 'org-agenda-windows
1092 :type '(choice
1093 (const current-window)
1094 (const other-frame)
1095 (const other-window)
1096 (const reorganize-frame)))
1098 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1099 "The min and max height of the agenda window as a fraction of frame height.
1100 The value of the variable is a cons cell with two numbers between 0 and 1.
1101 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1102 :group 'org-agenda-windows
1103 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1105 (defcustom org-agenda-restore-windows-after-quit nil
1106 "Non-nil means restore window configuration upon exiting agenda.
1107 Before the window configuration is changed for displaying the agenda,
1108 the current status is recorded. When the agenda is exited with
1109 `q' or `x' and this option is set, the old state is restored. If
1110 `org-agenda-window-setup' is `other-frame', the value of this
1111 option will be ignored."
1112 :group 'org-agenda-windows
1113 :type 'boolean)
1115 (defcustom org-agenda-ndays nil
1116 "Number of days to include in overview display.
1117 Should be 1 or 7.
1118 Obsolete, see `org-agenda-span'."
1119 :group 'org-agenda-daily/weekly
1120 :type 'integer)
1122 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1124 (defcustom org-agenda-span 'week
1125 "Number of days to include in overview display.
1126 Can be day, week, month, year, or any number of days.
1127 Custom commands can set this variable in the options section."
1128 :group 'org-agenda-daily/weekly
1129 :type '(choice (const :tag "Day" day)
1130 (const :tag "Week" week)
1131 (const :tag "Month" month)
1132 (const :tag "Year" year)
1133 (integer :tag "Custom")))
1135 (defcustom org-agenda-start-on-weekday 1
1136 "Non-nil means start the overview always on the specified weekday.
1137 0 denotes Sunday, 1 denotes Monday etc.
1138 When nil, always start on the current day.
1139 Custom commands can set this variable in the options section."
1140 :group 'org-agenda-daily/weekly
1141 :type '(choice (const :tag "Today" nil)
1142 (integer :tag "Weekday No.")))
1144 (defcustom org-agenda-show-all-dates t
1145 "Non-nil means `org-agenda' shows every day in the selected range.
1146 When nil, only the days which actually have entries are shown."
1147 :group 'org-agenda-daily/weekly
1148 :type 'boolean)
1150 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1151 "Format string for displaying dates in the agenda.
1152 Used by the daily/weekly agenda and by the timeline. This should be
1153 a format string understood by `format-time-string', or a function returning
1154 the formatted date as a string. The function must take a single argument,
1155 a calendar-style date list like (month day year)."
1156 :group 'org-agenda-daily/weekly
1157 :type '(choice
1158 (string :tag "Format string")
1159 (function :tag "Function")))
1161 (defun org-agenda-format-date-aligned (date)
1162 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1163 This function makes sure that dates are aligned for easy reading."
1164 (require 'cal-iso)
1165 (let* ((dayname (calendar-day-name date))
1166 (day (cadr date))
1167 (day-of-week (calendar-day-of-week date))
1168 (month (car date))
1169 (monthname (calendar-month-name month))
1170 (year (nth 2 date))
1171 (iso-week (org-days-to-iso-week
1172 (calendar-absolute-from-gregorian date)))
1173 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1174 (1- year))
1175 ((and (= month 12) (<= iso-week 1))
1176 (1+ year))
1177 (t year)))
1178 (weekstring (if (= day-of-week 1)
1179 (format " W%02d" iso-week)
1180 "")))
1181 (format "%-10s %2d %s %4d%s"
1182 dayname day monthname year weekstring)))
1184 (defcustom org-agenda-time-leading-zero nil
1185 "Non-nil means use leading zero for military times in agenda.
1186 For example, 9:30am would become 09:30 rather than 9:30."
1187 :group 'org-agenda-daily/weekly
1188 :version "24.1"
1189 :type 'boolean)
1191 (defcustom org-agenda-timegrid-use-ampm nil
1192 "When set, show AM/PM style timestamps on the timegrid."
1193 :group 'org-agenda
1194 :version "24.1"
1195 :type 'boolean)
1197 (defun org-agenda-time-of-day-to-ampm (time)
1198 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1199 (let* ((hour-number (string-to-number (substring time 0 -3)))
1200 (minute (substring time -2))
1201 (ampm "am"))
1202 (cond
1203 ((equal hour-number 12)
1204 (setq ampm "pm"))
1205 ((> hour-number 12)
1206 (setq ampm "pm")
1207 (setq hour-number (- hour-number 12))))
1208 (concat
1209 (if org-agenda-time-leading-zero
1210 (format "%02d" hour-number)
1211 (format "%02s" (number-to-string hour-number)))
1212 ":" minute ampm)))
1214 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1215 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1216 (if org-agenda-timegrid-use-ampm
1217 (org-agenda-time-of-day-to-ampm time)
1218 time))
1220 (defcustom org-agenda-weekend-days '(6 0)
1221 "Which days are weekend?
1222 These days get the special face `org-agenda-date-weekend' in the agenda
1223 and timeline buffers."
1224 :group 'org-agenda-daily/weekly
1225 :type '(set :greedy t
1226 (const :tag "Monday" 1)
1227 (const :tag "Tuesday" 2)
1228 (const :tag "Wednesday" 3)
1229 (const :tag "Thursday" 4)
1230 (const :tag "Friday" 5)
1231 (const :tag "Saturday" 6)
1232 (const :tag "Sunday" 0)))
1234 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1235 "Non-nil means jump to today when moving a past date forward in time.
1236 When using S-right in the agenda to move a a date forward, and the date
1237 stamp currently points to the past, the first key press will move it
1238 to today. WHen nil, just move one day forward even if the date stays
1239 in the past."
1240 :group 'org-agenda-daily/weekly
1241 :version "24.1"
1242 :type 'boolean)
1244 (defcustom org-agenda-include-diary nil
1245 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1246 Custom commands can set this variable in the options section."
1247 :group 'org-agenda-daily/weekly
1248 :type 'boolean)
1250 (defcustom org-agenda-include-deadlines t
1251 "If non-nil, include entries within their deadline warning period.
1252 Custom commands can set this variable in the options section."
1253 :group 'org-agenda-daily/weekly
1254 :version "24.1"
1255 :type 'boolean)
1257 (defcustom org-agenda-repeating-timestamp-show-all t
1258 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1259 When set to a list of strings, only show occurrences of repeating
1260 stamps for these TODO keywords. When nil, only one occurrence is
1261 shown, either today or the nearest into the future."
1262 :group 'org-agenda-daily/weekly
1263 :type '(choice
1264 (const :tag "Show repeating stamps" t)
1265 (repeat :tag "Show repeating stamps for these TODO keywords"
1266 (string :tag "TODO Keyword"))
1267 (const :tag "Don't show repeating stamps" nil)))
1269 (defcustom org-scheduled-past-days 10000
1270 "Number of days to continue listing scheduled items not marked DONE.
1271 When an item is scheduled on a date, it shows up in the agenda on this
1272 day and will be listed until it is marked done for the number of days
1273 given here."
1274 :group 'org-agenda-daily/weekly
1275 :type 'integer)
1277 (defcustom org-agenda-log-mode-items '(closed clock)
1278 "List of items that should be shown in agenda log mode.
1279 This list may contain the following symbols:
1281 closed Show entries that have been closed on that day.
1282 clock Show entries that have received clocked time on that day.
1283 state Show all logged state changes.
1284 Note that instead of changing this variable, you can also press `C-u l' in
1285 the agenda to display all available LOG items temporarily."
1286 :group 'org-agenda-daily/weekly
1287 :type '(set :greedy t (const closed) (const clock) (const state)))
1289 (defcustom org-agenda-clock-consistency-checks
1290 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1291 :gap-ok-around ("4:00")
1292 :default-face ((:background "DarkRed") (:foreground "white"))
1293 :overlap-face nil :gap-face nil :no-end-time-face nil
1294 :long-face nil :short-face nil)
1295 "This is a property list, with the following keys:
1297 :max-duration Mark clocking chunks that are longer than this time.
1298 This is a time string like \"HH:MM\", or the number
1299 of minutes as an integer.
1301 :min-duration Mark clocking chunks that are shorter that this.
1302 This is a time string like \"HH:MM\", or the number
1303 of minutes as an integer.
1305 :max-gap Mark gaps between clocking chunks that are longer than
1306 this duration. A number of minutes, or a string
1307 like \"HH:MM\".
1309 :gap-ok-around List of times during the day which are usually not working
1310 times. When a gap is detected, but the gap contains any
1311 of these times, the gap is *not* reported. For example,
1312 if this is (\"4:00\" \"13:00\") then gaps that contain
1313 4:00 in the morning (i.e. the night) and 13:00
1314 (i.e. a typical lunch time) do not cause a warning.
1315 You should have at least one time during the night in this
1316 list, or otherwise the first task each morning will trigger
1317 a warning because it follows a long gap.
1319 Furthermore, the following properties can be used to define faces for
1320 issue display.
1322 :default-face the default face, if the specific face is undefined
1323 :overlap-face face for overlapping clocks
1324 :gap-face face for gaps between clocks
1325 :no-end-time-face face for incomplete clocks
1326 :long-face face for clock intervals that are too long
1327 :short-face face for clock intervals that are too short"
1328 :group 'org-agenda-daily/weekly
1329 :group 'org-clock
1330 :version "24.1"
1331 :type 'plist)
1333 (defcustom org-agenda-log-mode-add-notes t
1334 "Non-nil means add first line of notes to log entries in agenda views.
1335 If a log item like a state change or a clock entry is associated with
1336 notes, the first line of these notes will be added to the entry in the
1337 agenda display."
1338 :group 'org-agenda-daily/weekly
1339 :type 'boolean)
1341 (defcustom org-agenda-start-with-log-mode nil
1342 "The initial value of log-mode in a newly created agenda window.
1343 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1344 explanations on the possible values."
1345 :group 'org-agenda-startup
1346 :group 'org-agenda-daily/weekly
1347 :type '(choice (const :tag "Don't show log items" nil)
1348 (const :tag "Show only log items" 'only)
1349 (const :tag "Show all possible log items" 'clockcheck)
1350 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1351 (choice (const :tag "Show closed log items" 'closed)
1352 (const :tag "Show clocked log items" 'clock)
1353 (const :tag "Show all logged state changes" 'state)))))
1355 (defcustom org-agenda-start-with-clockreport-mode nil
1356 "The initial value of clockreport-mode in a newly created agenda window."
1357 :group 'org-agenda-startup
1358 :group 'org-agenda-daily/weekly
1359 :type 'boolean)
1361 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1362 "Property list with parameters for the clocktable in clockreport mode.
1363 This is the display mode that shows a clock table in the daily/weekly
1364 agenda, the properties for this dynamic block can be set here.
1365 The usual clocktable parameters are allowed here, but you cannot set
1366 the properties :name, :tstart, :tend, :block, and :scope - these will
1367 be overwritten to make sure the content accurately reflects the
1368 current display in the agenda."
1369 :group 'org-agenda-daily/weekly
1370 :type 'plist)
1372 (defcustom org-agenda-search-view-always-boolean nil
1373 "Non-nil means the search string is interpreted as individual parts.
1375 The search string for search view can either be interpreted as a phrase,
1376 or as a list of snippets that define a boolean search for a number of
1377 strings.
1379 When this is non-nil, the string will be split on whitespace, and each
1380 snippet will be searched individually, and all must match in order to
1381 select an entry. A snippet is then a single string of non-white
1382 characters, or a string in double quotes, or a regexp in {} braces.
1383 If a snippet is preceded by \"-\", the snippet must *not* match.
1384 \"+\" is syntactic sugar for positive selection. Each snippet may
1385 be found as a full word or a partial word, but see the variable
1386 `org-agenda-search-view-force-full-words'.
1388 When this is nil, search will look for the entire search phrase as one,
1389 with each space character matching any amount of whitespace, including
1390 line breaks.
1392 Even when this is nil, you can still switch to Boolean search dynamically
1393 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1394 is a regexp marked with braces like \"{abc}\", this will also switch to
1395 boolean search."
1396 :group 'org-agenda-search-view
1397 :version "24.1"
1398 :type 'boolean)
1400 (org-defvaralias 'org-agenda-search-view-search-words-only
1401 'org-agenda-search-view-always-boolean)
1403 (defcustom org-agenda-search-view-force-full-words nil
1404 "Non-nil means, search words must be matches as complete words.
1405 When nil, they may also match part of a word."
1406 :group 'org-agenda-search-view
1407 :version "24.1"
1408 :type 'boolean)
1410 (defcustom org-agenda-search-view-max-outline-level nil
1411 "Maximum outline level to display in search view.
1412 E.g. when this is set to 1, the search view will only
1413 show headlines of level 1."
1414 :group 'org-agenda-search-view
1415 :version "24.4"
1416 :package-version '(Org . "8.0")
1417 :type 'integer)
1419 (defgroup org-agenda-time-grid nil
1420 "Options concerning the time grid in the Org-mode Agenda."
1421 :tag "Org Agenda Time Grid"
1422 :group 'org-agenda)
1424 (defcustom org-agenda-search-headline-for-time t
1425 "Non-nil means search headline for a time-of-day.
1426 If the headline contains a time-of-day in one format or another, it will
1427 be used to sort the entry into the time sequence of items for a day.
1428 Some people have time stamps in the headline that refer to the creation
1429 time or so, and then this produces an unwanted side effect. If this is
1430 the case for your, use this variable to turn off searching the headline
1431 for a time."
1432 :group 'org-agenda-time-grid
1433 :type 'boolean)
1435 (defcustom org-agenda-use-time-grid t
1436 "Non-nil means show a time grid in the agenda schedule.
1437 A time grid is a set of lines for specific times (like every two hours between
1438 8:00 and 20:00). The items scheduled for a day at specific times are
1439 sorted in between these lines.
1440 For details about when the grid will be shown, and what it will look like, see
1441 the variable `org-agenda-time-grid'."
1442 :group 'org-agenda-time-grid
1443 :type 'boolean)
1445 (defcustom org-agenda-time-grid
1446 '((daily today require-timed)
1447 "----------------"
1448 (800 1000 1200 1400 1600 1800 2000))
1450 "The settings for time grid for agenda display.
1451 This is a list of three items. The first item is again a list. It contains
1452 symbols specifying conditions when the grid should be displayed:
1454 daily if the agenda shows a single day
1455 weekly if the agenda shows an entire week
1456 today show grid on current date, independent of daily/weekly display
1457 require-timed show grid only if at least one item has a time specification
1459 The second item is a string which will be placed behind the grid time.
1461 The third item is a list of integers, indicating the times that should have
1462 a grid line."
1463 :group 'org-agenda-time-grid
1464 :type
1465 '(list
1466 (set :greedy t :tag "Grid Display Options"
1467 (const :tag "Show grid in single day agenda display" daily)
1468 (const :tag "Show grid in weekly agenda display" weekly)
1469 (const :tag "Always show grid for today" today)
1470 (const :tag "Show grid only if any timed entries are present"
1471 require-timed)
1472 (const :tag "Skip grid times already present in an entry"
1473 remove-match))
1474 (string :tag "Grid String")
1475 (repeat :tag "Grid Times" (integer :tag "Time"))))
1477 (defcustom org-agenda-show-current-time-in-grid t
1478 "Non-nil means show the current time in the time grid."
1479 :group 'org-agenda-time-grid
1480 :version "24.1"
1481 :type 'boolean)
1483 (defcustom org-agenda-current-time-string
1484 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1485 "The string for the current time marker in the agenda."
1486 :group 'org-agenda-time-grid
1487 :version "24.1"
1488 :type 'string)
1490 (defgroup org-agenda-sorting nil
1491 "Options concerning sorting in the Org-mode Agenda."
1492 :tag "Org Agenda Sorting"
1493 :group 'org-agenda)
1495 (defcustom org-agenda-sorting-strategy
1496 '((agenda habit-down time-up priority-down category-keep)
1497 (todo priority-down category-keep)
1498 (tags priority-down category-keep)
1499 (search category-keep))
1500 "Sorting structure for the agenda items of a single day.
1501 This is a list of symbols which will be used in sequence to determine
1502 if an entry should be listed before another entry. The following
1503 symbols are recognized:
1505 time-up Put entries with time-of-day indications first, early first
1506 time-down Put entries with time-of-day indications first, late first
1507 timestamp-up Sort by any timestamp, early first
1508 timestamp-down Sort by any timestamp, late first
1509 scheduled-up Sort by scheduled timestamp, early first
1510 scheduled-down Sort by scheduled timestamp, late first
1511 deadline-up Sort by deadline timestamp, early first
1512 deadline-down Sort by deadline timestamp, late first
1513 ts-up Sort by active timestamp, early first
1514 ts-down Sort by active timestamp, late first
1515 tsia-up Sort by inactive timestamp, early first
1516 tsia-down Sort by inactive timestamp, late first
1517 category-keep Keep the default order of categories, corresponding to the
1518 sequence in `org-agenda-files'.
1519 category-up Sort alphabetically by category, A-Z.
1520 category-down Sort alphabetically by category, Z-A.
1521 tag-up Sort alphabetically by last tag, A-Z.
1522 tag-down Sort alphabetically by last tag, Z-A.
1523 priority-up Sort numerically by priority, high priority last.
1524 priority-down Sort numerically by priority, high priority first.
1525 todo-state-up Sort by todo state, tasks that are done last.
1526 todo-state-down Sort by todo state, tasks that are done first.
1527 effort-up Sort numerically by estimated effort, high effort last.
1528 effort-down Sort numerically by estimated effort, high effort first.
1529 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1530 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1531 habit-up Put entries that are habits first
1532 habit-down Put entries that are habits last
1533 alpha-up Sort headlines alphabetically
1534 alpha-down Sort headlines alphabetically, reversed
1536 The different possibilities will be tried in sequence, and testing stops
1537 if one comparison returns a \"not-equal\". For example, the default
1538 '(time-up category-keep priority-down)
1539 means: Pull out all entries having a specified time of day and sort them,
1540 in order to make a time schedule for the current day the first thing in the
1541 agenda listing for the day. Of the entries without a time indication, keep
1542 the grouped in categories, don't sort the categories, but keep them in
1543 the sequence given in `org-agenda-files'. Within each category sort by
1544 priority.
1546 Leaving out `category-keep' would mean that items will be sorted across
1547 categories by priority.
1549 Instead of a single list, this can also be a set of list for specific
1550 contents, with a context symbol in the car of the list, any of
1551 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1553 Custom commands can bind this variable in the options section."
1554 :group 'org-agenda-sorting
1555 :type `(choice
1556 (repeat :tag "General" ,org-sorting-choice)
1557 (list :tag "Individually"
1558 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1559 (repeat ,org-sorting-choice))
1560 (cons (const :tag "Strategy for TODO lists" todo)
1561 (repeat ,org-sorting-choice))
1562 (cons (const :tag "Strategy for Tags matches" tags)
1563 (repeat ,org-sorting-choice))
1564 (cons (const :tag "Strategy for search matches" search)
1565 (repeat ,org-sorting-choice)))))
1567 (defcustom org-agenda-cmp-user-defined nil
1568 "A function to define the comparison `user-defined'.
1569 This function must receive two arguments, agenda entry a and b.
1570 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1571 the user comparison, return nil.
1572 When this is defined, you can make `user-defined-up' and `user-defined-down'
1573 part of an agenda sorting strategy."
1574 :group 'org-agenda-sorting
1575 :type 'symbol)
1577 (defcustom org-sort-agenda-notime-is-late t
1578 "Non-nil means items without time are considered late.
1579 This is only relevant for sorting. When t, items which have no explicit
1580 time like 15:30 will be considered as 99:01, i.e. later than any items which
1581 do have a time. When nil, the default time is before 0:00. You can use this
1582 option to decide if the schedule for today should come before or after timeless
1583 agenda entries."
1584 :group 'org-agenda-sorting
1585 :type 'boolean)
1587 (defcustom org-sort-agenda-noeffort-is-high t
1588 "Non-nil means items without effort estimate are sorted as high effort.
1589 This also applies when filtering an agenda view with respect to the
1590 < or > effort operator. Then, tasks with no effort defined will be treated
1591 as tasks with high effort.
1592 When nil, such items are sorted as 0 minutes effort."
1593 :group 'org-agenda-sorting
1594 :type 'boolean)
1596 (defgroup org-agenda-line-format nil
1597 "Options concerning the entry prefix in the Org-mode agenda display."
1598 :tag "Org Agenda Line Format"
1599 :group 'org-agenda)
1601 (defcustom org-agenda-prefix-format
1602 '((agenda . " %i %-12:c%?-12t% s")
1603 (timeline . " % s")
1604 (todo . " %i %-12:c")
1605 (tags . " %i %-12:c")
1606 (search . " %i %-12:c"))
1607 "Format specifications for the prefix of items in the agenda views.
1608 An alist with five entries, each for the different agenda types. The
1609 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1610 The values are format strings.
1612 This format works similar to a printf format, with the following meaning:
1614 %c the category of the item, \"Diary\" for entries from the diary,
1615 or as given by the CATEGORY keyword or derived from the file name
1616 %e the effort required by the item
1617 %l the level of the item (insert X space(s) if item is of level X)
1618 %i the icon category of the item, see `org-agenda-category-icon-alist'
1619 %T the last tag of the item (ignore inherited tags, which come first)
1620 %t the HH:MM time-of-day specification if one applies to the entry
1621 %s Scheduling/Deadline information, a short string
1622 %b show breadcrumbs, i.e., the names of the higher levels
1623 %(expression) Eval EXPRESSION and replace the control string
1624 by the result
1626 All specifiers work basically like the standard `%s' of printf, but may
1627 contain two additional characters: a question mark just after the `%'
1628 and a whitespace/punctuation character just before the final letter.
1630 If the first character after `%' is a question mark, the entire field
1631 will only be included if the corresponding value applies to the current
1632 entry. This is useful for fields which should have fixed width when
1633 present, but zero width when absent. For example, \"%?-12t\" will
1634 result in a 12 character time field if a time of the day is specified,
1635 but will completely disappear in entries which do not contain a time.
1637 If there is punctuation or whitespace character just before the
1638 final format letter, this character will be appended to the field
1639 value if the value is not empty. For example, the format
1640 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1641 the category is empty, no additional colon is inserted.
1643 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1644 which means:
1646 - Indent the line with two space characters
1647 - Give the category a 12 chars wide field, padded with whitespace on
1648 the right (because of `-'). Append a colon if there is a category
1649 (because of `:').
1650 - If there is a time-of-day, put it into a 12 chars wide field. If no
1651 time, don't put in an empty field, just skip it (because of '?').
1652 - Finally, put the scheduling information.
1654 See also the variables `org-agenda-remove-times-when-in-prefix' and
1655 `org-agenda-remove-tags'.
1657 Custom commands can set this variable in the options section."
1658 :type '(choice
1659 (string :tag "General format")
1660 (list :greedy t :tag "View dependent"
1661 (cons (const agenda) (string :tag "Format"))
1662 (cons (const timeline) (string :tag "Format"))
1663 (cons (const todo) (string :tag "Format"))
1664 (cons (const tags) (string :tag "Format"))
1665 (cons (const search) (string :tag "Format"))))
1666 :group 'org-agenda-line-format)
1668 (defvar org-prefix-format-compiled nil
1669 "The compiled prefix format and associated variables.
1670 This is a list where first element is a list of variable bindings, and second
1671 element is the compiled format expression. See the variable
1672 `org-agenda-prefix-format'.")
1674 (defcustom org-agenda-todo-keyword-format "%-1s"
1675 "Format for the TODO keyword in agenda lines.
1676 Set this to something like \"%-12s\" if you want all TODO keywords
1677 to occupy a fixed space in the agenda display."
1678 :group 'org-agenda-line-format
1679 :type 'string)
1681 (defcustom org-agenda-diary-sexp-prefix nil
1682 "A regexp that matches part of a diary sexp entry
1683 which should be treated as scheduling/deadline information in
1684 `org-agenda'.
1686 For example, you can use this to extract the `diary-remind-message' from
1687 `diary-remind' entries."
1688 :group 'org-agenda-line-format
1689 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1691 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1692 "Text preceding timerange entries in the agenda view.
1693 This is a list with two strings. The first applies when the range
1694 is entirely on one day. The second applies if the range spans several days.
1695 The strings may have two \"%d\" format specifiers which will be filled
1696 with the sequence number of the days, and the total number of days in the
1697 range, respectively."
1698 :group 'org-agenda-line-format
1699 :type '(list
1700 (string :tag "Deadline today ")
1701 (choice :tag "Deadline relative"
1702 (string :tag "Format string")
1703 (function))))
1705 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1706 "Text preceding scheduled items in the agenda view.
1707 This is a list with two strings. The first applies when the item is
1708 scheduled on the current day. The second applies when it has been scheduled
1709 previously, it may contain a %d indicating that this is the nth time that
1710 this item is scheduled, due to automatic rescheduling of unfinished items
1711 for the following day. So this number is one larger than the number of days
1712 that passed since this item was scheduled first."
1713 :group 'org-agenda-line-format
1714 :version "24.4"
1715 :package-version '(Org . "8.0")
1716 :type '(list
1717 (string :tag "Scheduled today ")
1718 (string :tag "Scheduled previously")))
1720 (defcustom org-agenda-inactive-leader "["
1721 "Text preceding item pulled into the agenda by inactive time stamps.
1722 These entries are added to the agenda when pressing \"[\"."
1723 :group 'org-agenda-line-format
1724 :version "24.1"
1725 :type '(list
1726 (string :tag "Scheduled today ")
1727 (string :tag "Scheduled previously")))
1729 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1730 "Text preceding deadline items in the agenda view.
1731 This is a list with three strings. The first applies when the item has its
1732 deadline on the current day. The second applies when the deadline is in the
1733 future, the third one when it is in the past. The strings may contain %d
1734 to capture the number of days."
1735 :group 'org-agenda-line-format
1736 :version "24.4"
1737 :package-version '(Org . "8.0")
1738 :type '(list
1739 (string :tag "Deadline today ")
1740 (choice :tag "Deadline relative"
1741 (string :tag "Format string")
1742 (function))))
1744 (defcustom org-agenda-remove-times-when-in-prefix t
1745 "Non-nil means remove duplicate time specifications in agenda items.
1746 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1747 time-of-day specification in a headline or diary entry is extracted and
1748 placed into the prefix. If this option is non-nil, the original specification
1749 \(a timestamp or -range, or just a plain time(range) specification like
1750 11:30-4pm) will be removed for agenda display. This makes the agenda less
1751 cluttered.
1752 The option can be t or nil. It may also be the symbol `beg', indicating
1753 that the time should only be removed when it is located at the beginning of
1754 the headline/diary entry."
1755 :group 'org-agenda-line-format
1756 :type '(choice
1757 (const :tag "Always" t)
1758 (const :tag "Never" nil)
1759 (const :tag "When at beginning of entry" beg)))
1761 (defcustom org-agenda-remove-timeranges-from-blocks nil
1762 "Non-nil means remove time ranges specifications in agenda
1763 items that span on several days."
1764 :group 'org-agenda-line-format
1765 :version "24.1"
1766 :type 'boolean)
1768 (defcustom org-agenda-default-appointment-duration nil
1769 "Default duration for appointments that only have a starting time.
1770 When nil, no duration is specified in such cases.
1771 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1772 :group 'org-agenda-line-format
1773 :type '(choice
1774 (integer :tag "Minutes")
1775 (const :tag "No default duration")))
1777 (defcustom org-agenda-show-inherited-tags t
1778 "Non-nil means show inherited tags in each agenda line.
1780 When this option is set to 'always, it take precedences over
1781 `org-agenda-use-tag-inheritance' and inherited tags are shown
1782 in every agenda.
1784 When this option is set to t (the default), inherited tags are
1785 shown when they are available, i.e. when the value of
1786 `org-agenda-use-tag-inheritance' has been taken into account.
1788 This can be set to a list of agenda types in which the agenda
1789 must display the inherited tags. Available types are 'todo,
1790 'agenda, 'search and 'timeline.
1792 When set to nil, never show inherited tags in agenda lines."
1793 :group 'org-agenda-line-format
1794 :group 'org-agenda
1795 :version "24.3"
1796 :type '(choice
1797 (const :tag "Show inherited tags when available" t)
1798 (const :tag "Always show inherited tags" 'always)
1799 (repeat :tag "Show inherited tags only in selected agenda types"
1800 (symbol :tag "Agenda type"))))
1802 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1803 "List of agenda view types where to use tag inheritance.
1805 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1806 controlled by `org-use-tag-inheritance'. In other agenda types,
1807 `org-use-tag-inheritance' is not used for the selection of the
1808 agenda entries. Still, you may want the agenda to be aware of
1809 the inherited tags anyway, e.g. for later tag filtering.
1811 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1813 This variable has no effect if `org-agenda-show-inherited-tags'
1814 is set to 'always. In that case, the agenda is aware of those
1815 tags.
1817 The default value sets tags in every agenda type. Setting this
1818 option to nil will speed up non-tags agenda view a lot."
1819 :group 'org-agenda
1820 :version "24.3"
1821 :type '(choice
1822 (const :tag "Use tag inheritance in all agenda types" t)
1823 (repeat :tag "Use tag inheritance in selected agenda types"
1824 (symbol :tag "Agenda type"))))
1826 (defcustom org-agenda-hide-tags-regexp nil
1827 "Regular expression used to filter away specific tags in agenda views.
1828 This means that these tags will be present, but not be shown in the agenda
1829 line. Secondary filtering will still work on the hidden tags.
1830 Nil means don't hide any tags."
1831 :group 'org-agenda-line-format
1832 :type '(choice
1833 (const :tag "Hide none" nil)
1834 (string :tag "Regexp ")))
1836 (defcustom org-agenda-remove-tags nil
1837 "Non-nil means remove the tags from the headline copy in the agenda.
1838 When this is the symbol `prefix', only remove tags when
1839 `org-agenda-prefix-format' contains a `%T' specifier."
1840 :group 'org-agenda-line-format
1841 :type '(choice
1842 (const :tag "Always" t)
1843 (const :tag "Never" nil)
1844 (const :tag "When prefix format contains %T" prefix)))
1846 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1847 'org-agenda-remove-tags)
1849 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1850 "Shift tags in agenda items to this column.
1851 If this number is positive, it specifies the column. If it is negative,
1852 it means that the tags should be flushright to that column. For example,
1853 -80 works well for a normal 80 character screen."
1854 :group 'org-agenda-line-format
1855 :type 'integer)
1857 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1859 (defcustom org-agenda-fontify-priorities 'cookies
1860 "Non-nil means highlight low and high priorities in agenda.
1861 When t, the highest priority entries are bold, lowest priority italic.
1862 However, settings in `org-priority-faces' will overrule these faces.
1863 When this variable is the symbol `cookies', only fontify the
1864 cookies, not the entire task.
1865 This may also be an association list of priority faces, whose
1866 keys are the character values of `org-highest-priority',
1867 `org-default-priority', and `org-lowest-priority' (the default values
1868 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1869 color as a string, or a list like `(:background \"Red\")'.
1870 If it is a color, the variable `org-faces-easy-properties'
1871 determines if it is a foreground or a background color."
1872 :group 'org-agenda-line-format
1873 :type '(choice
1874 (const :tag "Never" nil)
1875 (const :tag "Defaults" t)
1876 (const :tag "Cookies only" cookies)
1877 (repeat :tag "Specify"
1878 (list (character :tag "Priority" :value ?A)
1879 (choice :tag "Face "
1880 (string :tag "Color")
1881 (sexp :tag "Face"))))))
1883 (defcustom org-agenda-day-face-function nil
1884 "Function called to determine what face should be used to display a day.
1885 The only argument passed to that function is the day. It should
1886 returns a face, or nil if does not want to specify a face and let
1887 the normal rules apply."
1888 :group 'org-agenda-line-format
1889 :version "24.1"
1890 :type 'function)
1892 (defcustom org-agenda-category-icon-alist nil
1893 "Alist of category icon to be displayed in agenda views.
1895 Each entry should have the following format:
1897 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1899 Where CATEGORY-REGEXP is a regexp matching the categories where
1900 the icon should be displayed.
1901 FILE-OR-DATA either a file path or a string containing image data.
1903 The other fields can be omitted safely if not needed:
1904 TYPE indicates the image type.
1905 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1906 image data.
1907 PROPS are additional image attributes to assign to the image,
1908 like, e.g. `:ascent center'.
1910 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1912 If you want to set the display properties yourself, just put a
1913 list as second element:
1915 (CATEGORY-REGEXP (MY PROPERTY LIST))
1917 For example, to display a 16px horizontal space for Emacs
1918 category, you can use:
1920 (\"Emacs\" '(space . (:width (16))))"
1921 :group 'org-agenda-line-format
1922 :version "24.1"
1923 :type '(alist :key-type (string :tag "Regexp matching category")
1924 :value-type (choice (list :tag "Icon"
1925 (string :tag "File or data")
1926 (symbol :tag "Type")
1927 (boolean :tag "Data?")
1928 (repeat :tag "Extra image properties" :inline t symbol))
1929 (list :tag "Display properties" sexp))))
1931 (defgroup org-agenda-column-view nil
1932 "Options concerning column view in the agenda."
1933 :tag "Org Agenda Column View"
1934 :group 'org-agenda)
1936 (defcustom org-agenda-columns-show-summaries t
1937 "Non-nil means show summaries for columns displayed in the agenda view."
1938 :group 'org-agenda-column-view
1939 :type 'boolean)
1941 (defcustom org-agenda-columns-compute-summary-properties t
1942 "Non-nil means recompute all summary properties before column view.
1943 When column view in the agenda is listing properties that have a summary
1944 operator, it can go to all relevant buffers and recompute the summaries
1945 there. This can mean overhead for the agenda column view, but is necessary
1946 to have thing up to date.
1947 As a special case, a CLOCKSUM property also makes sure that the clock
1948 computations are current."
1949 :group 'org-agenda-column-view
1950 :type 'boolean)
1952 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1953 "Non-nil means the duration of an appointment will add to day effort.
1954 The property to which appointment durations will be added is the one given
1955 in the option `org-effort-property'. If an appointment does not have
1956 an end time, `org-agenda-default-appointment-duration' will be used. If that
1957 is not set, an appointment without end time will not contribute to the time
1958 estimate."
1959 :group 'org-agenda-column-view
1960 :type 'boolean)
1962 (defcustom org-agenda-auto-exclude-function nil
1963 "A function called with a tag to decide if it is filtered on '/ RET'.
1964 The sole argument to the function, which is called once for each
1965 possible tag, is a string giving the name of the tag. The
1966 function should return either nil if the tag should be included
1967 as normal, or \"-<TAG>\" to exclude the tag.
1968 Note that for the purpose of tag filtering, only the lower-case version of
1969 all tags will be considered, so that this function will only ever see
1970 the lower-case version of all tags."
1971 :group 'org-agenda
1972 :type 'function)
1974 (defcustom org-agenda-bulk-custom-functions nil
1975 "Alist of characters and custom functions for bulk actions.
1976 For example, this value makes those two functions available:
1978 '((?R set-category)
1979 (?C bulk-cut))
1981 With selected entries in an agenda buffer, `B R' will call
1982 the custom function `set-category' on the selected entries.
1983 Note that functions in this alist don't need to be quoted."
1984 :type 'alist
1985 :version "24.1"
1986 :group 'org-agenda)
1988 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1989 "Execute BODY with point at location given by `org-hd-marker' property.
1990 If STRING is non-nil, the text property will be fetched from position 0
1991 in that string. If STRING is nil, it will be fetched from the beginning
1992 of the current line."
1993 (org-with-gensyms (marker)
1994 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1995 'org-hd-marker ,string)))
1996 (with-current-buffer (marker-buffer ,marker)
1997 (save-excursion
1998 (goto-char ,marker)
1999 ,@body)))))
2000 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2002 (defun org-add-agenda-custom-command (entry)
2003 "Replace or add a command in `org-agenda-custom-commands'.
2004 This is mostly for hacking and trying a new command - once the command
2005 works you probably want to add it to `org-agenda-custom-commands' for good."
2006 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2007 (if ass
2008 (setcdr ass (cdr entry))
2009 (push entry org-agenda-custom-commands))))
2011 ;;; Define the org-agenda-mode
2013 (defvar org-agenda-mode-map (make-sparse-keymap)
2014 "Keymap for `org-agenda-mode'.")
2015 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2017 (defvar org-agenda-menu) ; defined later in this file.
2018 (defvar org-agenda-restrict nil) ; defined later in this file.
2019 (defvar org-agenda-follow-mode nil)
2020 (defvar org-agenda-entry-text-mode nil)
2021 (defvar org-agenda-clockreport-mode nil)
2022 (defvar org-agenda-show-log nil)
2023 (defvar org-agenda-redo-command nil)
2024 (defvar org-agenda-query-string nil)
2025 (defvar org-agenda-mode-hook nil
2026 "Hook run after `org-agenda-mode' is turned on.
2027 The buffer is still writable when this hook is called.")
2028 (defvar org-agenda-type nil)
2029 (defvar org-agenda-force-single-file nil)
2030 (defvar org-agenda-bulk-marked-entries nil
2031 "List of markers that refer to marked entries in the agenda.")
2033 ;;; Multiple agenda buffers support
2035 (defcustom org-agenda-sticky nil
2036 "Non-nil means agenda q key will bury agenda buffers.
2037 Agenda commands will then show existing buffer instead of generating new ones.
2038 When nil, `q' will kill the single agenda buffer."
2039 :group 'org-agenda
2040 :version "24.3"
2041 :type 'boolean)
2044 ;;;###autoload
2045 (defun org-toggle-sticky-agenda (&optional arg)
2046 "Toggle `org-agenda-sticky'."
2047 (interactive "P")
2048 (let ((new-value (if arg
2049 (> (prefix-numeric-value arg) 0)
2050 (not org-agenda-sticky))))
2051 (if (equal new-value org-agenda-sticky)
2052 (and (org-called-interactively-p 'interactive)
2053 (message "Sticky agenda was already %s"
2054 (if org-agenda-sticky "enabled" "disabled")))
2055 (setq org-agenda-sticky new-value)
2056 (org-agenda-kill-all-agenda-buffers)
2057 (and (org-called-interactively-p 'interactive)
2058 (message "Sticky agenda was %s"
2059 (if org-agenda-sticky "enabled" "disabled"))))))
2061 (defvar org-agenda-buffer nil
2062 "Agenda buffer currently being generated.")
2064 (defvar org-agenda-last-prefix-arg nil)
2065 (defvar org-agenda-this-buffer-name nil)
2066 (defvar org-agenda-doing-sticky-redo nil)
2067 (defvar org-agenda-this-buffer-is-sticky nil)
2069 (defconst org-agenda-local-vars
2070 '(org-agenda-this-buffer-name
2071 org-agenda-undo-list
2072 org-agenda-pending-undo-list
2073 org-agenda-follow-mode
2074 org-agenda-entry-text-mode
2075 org-agenda-clockreport-mode
2076 org-agenda-show-log
2077 org-agenda-redo-command
2078 org-agenda-query-string
2079 org-agenda-type
2080 org-agenda-bulk-marked-entries
2081 org-agenda-undo-has-started-in
2082 org-agenda-info
2083 org-agenda-pre-window-conf
2084 org-agenda-columns-active
2085 org-agenda-tag-filter-overlays
2086 org-agenda-tag-filter
2087 org-agenda-cat-filter-overlays
2088 org-agenda-category-filter
2089 org-agenda-re-filter-overlays
2090 org-agenda-regexp-filter
2091 org-agenda-markers
2092 org-agenda-last-search-view-search-was-boolean
2093 org-agenda-filtered-by-category
2094 org-agenda-filter-form
2095 org-agenda-cycle-counter
2096 org-agenda-last-prefix-arg)
2097 "Variables that must be local in agenda buffers to allow multiple buffers.")
2099 (defun org-agenda-mode ()
2100 "Mode for time-sorted view on action items in Org-mode files.
2102 The following commands are available:
2104 \\{org-agenda-mode-map}"
2105 (interactive)
2106 (cond (org-agenda-doing-sticky-redo
2107 ;; Refreshing sticky agenda-buffer
2109 ;; Preserve the value of `org-agenda-local-vars' variables,
2110 ;; while letting `kill-all-local-variables' kill the rest
2111 (let ((save (buffer-local-variables)))
2112 (kill-all-local-variables)
2113 (mapc 'make-local-variable org-agenda-local-vars)
2114 (dolist (elem save)
2115 (let ((var (car elem))
2116 (val (cdr elem)))
2117 (when (and val
2118 (member var org-agenda-local-vars))
2119 (set var val)))))
2120 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2121 (org-agenda-sticky
2122 ;; Creating a sticky Agenda buffer for the first time
2123 (kill-all-local-variables)
2124 (mapc 'make-local-variable org-agenda-local-vars)
2125 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2127 ;; Creating a non-sticky agenda buffer
2128 (kill-all-local-variables)
2129 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2130 (setq org-agenda-undo-list nil
2131 org-agenda-pending-undo-list nil
2132 org-agenda-bulk-marked-entries nil)
2133 (setq major-mode 'org-agenda-mode)
2134 ;; Keep global-font-lock-mode from turning on font-lock-mode
2135 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2136 (setq mode-name "Org-Agenda")
2137 (use-local-map org-agenda-mode-map)
2138 (easy-menu-add org-agenda-menu)
2139 (if org-startup-truncated (setq truncate-lines t))
2140 (org-set-local 'line-move-visual nil)
2141 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2142 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2143 ;; Make sure properties are removed when copying text
2144 (make-local-variable 'filter-buffer-substring-functions)
2145 (add-hook 'filter-buffer-substring-functions
2146 (lambda (fun start end delete)
2147 (substring-no-properties (funcall fun start end delete))))
2148 (unless org-agenda-keep-modes
2149 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2150 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2151 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2152 org-agenda-show-log org-agenda-start-with-log-mode))
2154 (easy-menu-change
2155 '("Agenda") "Agenda Files"
2156 (append
2157 (list
2158 (vector
2159 (if (get 'org-agenda-files 'org-restrict)
2160 "Restricted to single file"
2161 "Edit File List")
2162 '(org-edit-agenda-file-list)
2163 (not (get 'org-agenda-files 'org-restrict)))
2164 "--")
2165 (mapcar 'org-file-menu-entry (org-agenda-files))))
2166 (org-agenda-set-mode-name)
2167 (apply
2168 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2169 (list 'org-agenda-mode-hook)))
2171 (substitute-key-definition 'undo 'org-agenda-undo
2172 org-agenda-mode-map global-map)
2173 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2174 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2175 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2176 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2177 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2178 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2179 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2180 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2181 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2182 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2183 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2184 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2185 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2186 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2187 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2188 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2189 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2190 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2191 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2192 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2193 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2194 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2195 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2196 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2197 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2198 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2199 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2200 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2201 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2202 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2203 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2204 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2205 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2206 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2207 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2208 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2209 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2210 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2211 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2212 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2213 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2214 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2215 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2216 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2217 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2218 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2220 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2221 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2222 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2223 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2224 (while l (org-defkey org-agenda-mode-map
2225 (int-to-string (pop l)) 'digit-argument)))
2227 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2228 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2229 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2230 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2231 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2232 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2233 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2234 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2235 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2236 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2237 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2238 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2239 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2240 'org-clock-modify-effort-estimate)
2241 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2242 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2243 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2244 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2245 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2246 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2247 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2248 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2249 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2250 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2251 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2252 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2253 (substitute-key-definition 'next-line 'org-agenda-next-line
2254 org-agenda-mode-map global-map)
2255 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2256 org-agenda-mode-map global-map)
2257 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2258 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2259 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2260 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2261 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2262 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2263 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2264 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2265 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2266 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2267 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2268 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2269 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2270 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2271 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2272 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2273 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2274 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2275 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2276 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2277 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2278 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2279 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2280 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2281 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2282 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2283 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2284 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2285 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2286 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2288 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2289 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2290 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2291 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2292 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2293 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2294 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2295 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2296 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2297 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-category)
2298 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2299 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2300 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2301 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2303 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2304 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2305 (when org-agenda-mouse-1-follows-link
2306 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2307 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2308 '("Agenda"
2309 ("Agenda Files")
2310 "--"
2311 ("Agenda Dates"
2312 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2313 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2314 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2315 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2316 "--"
2317 ("View"
2318 ["Day View" org-agenda-day-view
2319 :active (org-agenda-check-type nil 'agenda)
2320 :style radio :selected (eq org-agenda-current-span 'day)
2321 :keys "v d (or just d)"]
2322 ["Week View" org-agenda-week-view
2323 :active (org-agenda-check-type nil 'agenda)
2324 :style radio :selected (eq org-agenda-current-span 'week)
2325 :keys "v w (or just w)"]
2326 ["Month View" org-agenda-month-view
2327 :active (org-agenda-check-type nil 'agenda)
2328 :style radio :selected (eq org-agenda-current-span 'month)
2329 :keys "v m"]
2330 ["Year View" org-agenda-year-view
2331 :active (org-agenda-check-type nil 'agenda)
2332 :style radio :selected (eq org-agenda-current-span 'year)
2333 :keys "v y"]
2334 "--"
2335 ["Include Diary" org-agenda-toggle-diary
2336 :style toggle :selected org-agenda-include-diary
2337 :active (org-agenda-check-type nil 'agenda)]
2338 ["Include Deadlines" org-agenda-toggle-deadlines
2339 :style toggle :selected org-agenda-include-deadlines
2340 :active (org-agenda-check-type nil 'agenda)]
2341 ["Use Time Grid" org-agenda-toggle-time-grid
2342 :style toggle :selected org-agenda-use-time-grid
2343 :active (org-agenda-check-type nil 'agenda)]
2344 "--"
2345 ["Show clock report" org-agenda-clockreport-mode
2346 :style toggle :selected org-agenda-clockreport-mode
2347 :active (org-agenda-check-type nil 'agenda)]
2348 ["Show some entry text" org-agenda-entry-text-mode
2349 :style toggle :selected org-agenda-entry-text-mode
2350 :active t]
2351 "--"
2352 ["Show Logbook entries" org-agenda-log-mode
2353 :style toggle :selected org-agenda-show-log
2354 :active (org-agenda-check-type nil 'agenda 'timeline)
2355 :keys "v l (or just l)"]
2356 ["Include archived trees" org-agenda-archives-mode
2357 :style toggle :selected org-agenda-archives-mode :active t
2358 :keys "v a"]
2359 ["Include archive files" (org-agenda-archives-mode t)
2360 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2361 :keys "v A"]
2362 "--"
2363 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2364 ["Write view to file" org-agenda-write t]
2365 ["Rebuild buffer" org-agenda-redo t]
2366 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2367 "--"
2368 ["Show original entry" org-agenda-show t]
2369 ["Go To (other window)" org-agenda-goto t]
2370 ["Go To (this window)" org-agenda-switch-to t]
2371 ["Capture with cursor date" org-agenda-capture t]
2372 ["Follow Mode" org-agenda-follow-mode
2373 :style toggle :selected org-agenda-follow-mode :active t]
2374 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2375 "--"
2376 ("TODO"
2377 ["Cycle TODO" org-agenda-todo t]
2378 ["Next TODO set" org-agenda-todo-nextset t]
2379 ["Previous TODO set" org-agenda-todo-previousset t]
2380 ["Add note" org-agenda-add-note t])
2381 ("Archive/Refile/Delete"
2382 ["Archive default" org-agenda-archive-default t]
2383 ["Archive default" org-agenda-archive-default-with-confirmation t]
2384 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2385 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2386 ["Archive subtree" org-agenda-archive t]
2387 "--"
2388 ["Refile" org-agenda-refile t]
2389 "--"
2390 ["Delete subtree" org-agenda-kill t])
2391 ("Bulk action"
2392 ["Mark entry" org-agenda-bulk-mark t]
2393 ["Mark all" org-agenda-bulk-mark-all t]
2394 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2395 ["Unmark entry" org-agenda-bulk-unmark t]
2396 ["Unmark all entries" org-agenda-bulk-unmark-all :active t :keys "U"])
2397 ["Act on all marked" org-agenda-bulk-action t]
2398 "--"
2399 ("Tags and Properties"
2400 ["Show all Tags" org-agenda-show-tags t]
2401 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2402 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2403 "--"
2404 ["Column View" org-columns t])
2405 ("Deadline/Schedule"
2406 ["Schedule" org-agenda-schedule t]
2407 ["Set Deadline" org-agenda-deadline t]
2408 "--"
2409 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2410 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2411 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2412 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2413 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2414 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2415 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2416 ("Clock and Effort"
2417 ["Clock in" org-agenda-clock-in t]
2418 ["Clock out" org-agenda-clock-out t]
2419 ["Clock cancel" org-agenda-clock-cancel t]
2420 ["Goto running clock" org-clock-goto t]
2421 "--"
2422 ["Set Effort" org-agenda-set-effort t]
2423 ["Change clocked effort" org-clock-modify-effort-estimate
2424 (org-clock-is-active)])
2425 ("Priority"
2426 ["Set Priority" org-agenda-priority t]
2427 ["Increase Priority" org-agenda-priority-up t]
2428 ["Decrease Priority" org-agenda-priority-down t]
2429 ["Show Priority" org-show-priority t])
2430 ("Calendar/Diary"
2431 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2432 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2433 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2434 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2435 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2436 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2437 "--"
2438 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2439 "--"
2440 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2441 "--"
2442 ("MobileOrg"
2443 ["Push Files and Views" org-mobile-push t]
2444 ["Get Captured and Flagged" org-mobile-pull t]
2445 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2446 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2447 "--"
2448 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2449 "--"
2450 ["Quit" org-agenda-quit t]
2451 ["Exit and Release Buffers" org-agenda-exit t]
2454 ;;; Agenda undo
2456 (defvar org-agenda-allow-remote-undo t
2457 "Non-nil means allow remote undo from the agenda buffer.")
2458 (defvar org-agenda-undo-has-started-in nil
2459 "Buffers that have already seen `undo-start' in the current undo sequence.")
2461 (defun org-agenda-undo ()
2462 "Undo a remote editing step in the agenda.
2463 This undoes changes both in the agenda buffer and in the remote buffer
2464 that have been changed along."
2465 (interactive)
2466 (or org-agenda-allow-remote-undo
2467 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2468 (if (not (eq this-command last-command))
2469 (setq org-agenda-undo-has-started-in nil
2470 org-agenda-pending-undo-list org-agenda-undo-list))
2471 (if (not org-agenda-pending-undo-list)
2472 (error "No further undo information"))
2473 (let* ((entry (pop org-agenda-pending-undo-list))
2474 buf line cmd rembuf)
2475 (setq cmd (pop entry) line (pop entry))
2476 (setq rembuf (nth 2 entry))
2477 (org-with-remote-undo rembuf
2478 (while (bufferp (setq buf (pop entry)))
2479 (if (pop entry)
2480 (with-current-buffer buf
2481 (let ((last-undo-buffer buf)
2482 (inhibit-read-only t))
2483 (unless (memq buf org-agenda-undo-has-started-in)
2484 (push buf org-agenda-undo-has-started-in)
2485 (make-local-variable 'pending-undo-list)
2486 (undo-start))
2487 (while (and pending-undo-list
2488 (listp pending-undo-list)
2489 (not (car pending-undo-list)))
2490 (pop pending-undo-list))
2491 (undo-more 1))))))
2492 (org-goto-line line)
2493 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2495 (defun org-verify-change-for-undo (l1 l2)
2496 "Verify that a real change occurred between the undo lists L1 and L2."
2497 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2498 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2499 (not (eq l1 l2)))
2501 ;;; Agenda dispatch
2503 (defvar org-agenda-restrict-begin (make-marker))
2504 (defvar org-agenda-restrict-end (make-marker))
2505 (defvar org-agenda-last-dispatch-buffer nil)
2506 (defvar org-agenda-overriding-restriction nil)
2508 (defcustom org-agenda-custom-commands-contexts nil
2509 "Alist of custom agenda keys and contextual rules.
2511 For example, if you have a custom agenda command \"p\" and you
2512 want this command to be accessible only from plain text files,
2513 use this:
2515 '((\"p\" ((in-file . \"\\.txt\"))))
2517 Here are the available contexts definitions:
2519 in-file: command displayed only in matching files
2520 in-mode: command displayed only in matching modes
2521 not-in-file: command not displayed in matching files
2522 not-in-mode: command not displayed in matching modes
2523 in-buffer: command displayed only in matching buffers
2524 not-in-buffer: command not displayed in matching buffers
2525 [function]: a custom function taking no argument
2527 If you define several checks, the agenda command will be
2528 accessible if there is at least one valid check.
2530 You can also bind a key to another agenda custom command
2531 depending on contextual rules.
2533 '((\"p\" \"q\" ((in-file . \"\\.txt\"))))
2535 Here it means: in .txt files, use \"p\" as the key for the
2536 agenda command otherwise associated with \"q\". (The command
2537 originally associated with \"q\" is not displayed to avoid
2538 duplicates.)"
2539 :version "24.3"
2540 :group 'org-agenda-custom-commands
2541 :type '(repeat (list :tag "Rule"
2542 (string :tag " Agenda key")
2543 (string :tag "Replace by command")
2544 (repeat :tag "Available when"
2545 (choice
2546 (cons :tag "Condition"
2547 (choice
2548 (const :tag "In file" in-file)
2549 (const :tag "Not in file" not-in-file)
2550 (const :tag "In buffer" in-buffer)
2551 (const :tag "Not in buffer" not-in-buffer)
2552 (const :tag "In mode" in-mode)
2553 (const :tag "Not in mode" not-in-mode))
2554 (regexp))
2555 (function :tag "Custom function"))))))
2557 (defcustom org-agenda-max-entries nil
2558 "Maximum number of entries to display in an agenda.
2559 This can be nil (no limit) or an integer or an alist of agenda
2560 types with an associated number of entries to display in this
2561 type."
2562 :version "24.4"
2563 :package-version '(Org . "8.0")
2564 :group 'org-agenda-custom-commands
2565 :type '(choice (symbol :tag "No limit" nil)
2566 (integer :tag "Max number of entries")
2567 (repeat
2568 (cons (choice :tag "Agenda type"
2569 (const agenda)
2570 (const todo)
2571 (const tags)
2572 (const search)
2573 (const timeline))
2574 (integer :tag "Max number of entries")))))
2576 (defcustom org-agenda-max-todos nil
2577 "Maximum number of TODOs to display in an agenda.
2578 This can be nil (no limit) or an integer or an alist of agenda
2579 types with an associated number of entries to display in this
2580 type."
2581 :version "24.4"
2582 :package-version '(Org . "8.0")
2583 :group 'org-agenda-custom-commands
2584 :type '(choice (symbol :tag "No limit" nil)
2585 (integer :tag "Max number of entries")
2586 (repeat
2587 (cons (choice :tag "Agenda type"
2588 (const agenda)
2589 (const todo)
2590 (const tags)
2591 (const search)
2592 (const timeline))
2593 (integer :tag "Max number of entries")))))
2595 (defcustom org-agenda-max-tags nil
2596 "Maximum number of tagged entries to display in an agenda.
2597 This can be nil (no limit) or an integer or an alist of agenda
2598 types with an associated number of entries to display in this
2599 type."
2600 :version "24.4"
2601 :package-version '(Org . "8.0")
2602 :group 'org-agenda-custom-commands
2603 :type '(choice (symbol :tag "No limit" nil)
2604 (integer :tag "Max number of entries")
2605 (repeat
2606 (cons (choice :tag "Agenda type"
2607 (const agenda)
2608 (const todo)
2609 (const tags)
2610 (const search)
2611 (const timeline))
2612 (integer :tag "Max number of entries")))))
2614 (defcustom org-agenda-max-effort nil
2615 "Maximum cumulated effort duration for the agenda.
2616 This can be nil (no limit) or a number of minutes (as an integer)
2617 or an alist of agenda types with an associated number of minutes
2618 to limit entries to in this type."
2619 :version "24.4"
2620 :package-version '(Org . "8.0")
2621 :group 'org-agenda-custom-commands
2622 :type '(choice (symbol :tag "No limit" nil)
2623 (integer :tag "Max number of entries")
2624 (repeat
2625 (cons (choice :tag "Agenda type"
2626 (const agenda)
2627 (const todo)
2628 (const tags)
2629 (const search)
2630 (const timeline))
2631 (integer :tag "Max number of entries")))))
2633 (defvar org-keys nil)
2634 (defvar org-match nil)
2635 ;;;###autoload
2636 (defun org-agenda (&optional arg org-keys restriction)
2637 "Dispatch agenda commands to collect entries to the agenda buffer.
2638 Prompts for a command to execute. Any prefix arg will be passed
2639 on to the selected command. The default selections are:
2641 a Call `org-agenda-list' to display the agenda for current day or week.
2642 t Call `org-todo-list' to display the global todo list.
2643 T Call `org-todo-list' to display the global todo list, select only
2644 entries with a specific TODO keyword (the user gets a prompt).
2645 m Call `org-tags-view' to display headlines with tags matching
2646 a condition (the user is prompted for the condition).
2647 M Like `m', but select only TODO entries, no ordinary headlines.
2648 L Create a timeline for the current buffer.
2649 e Export views to associated files.
2650 s Search entries for keywords.
2651 S Search entries for keywords, only with TODO keywords.
2652 / Multi occur across all agenda files and also files listed
2653 in `org-agenda-text-search-extra-files'.
2654 < Restrict agenda commands to buffer, subtree, or region.
2655 Press several times to get the desired effect.
2656 > Remove a previous restriction.
2657 # List \"stuck\" projects.
2658 ! Configure what \"stuck\" means.
2659 C Configure custom agenda commands.
2661 More commands can be added by configuring the variable
2662 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2663 searches can be pre-defined in this way.
2665 If the current buffer is in Org-mode and visiting a file, you can also
2666 first press `<' once to indicate that the agenda should be temporarily
2667 \(until the next use of \\[org-agenda]) restricted to the current file.
2668 Pressing `<' twice means to restrict to the current subtree or region
2669 \(if active)."
2670 (interactive "P")
2671 (catch 'exit
2672 (let* ((prefix-descriptions nil)
2673 (org-agenda-buffer-name org-agenda-buffer-name)
2674 (org-agenda-window-setup (if (equal (buffer-name)
2675 org-agenda-buffer-name)
2676 'current-window
2677 org-agenda-window-setup))
2678 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2679 (org-agenda-custom-commands
2680 ;; normalize different versions
2681 (delq nil
2682 (mapcar
2683 (lambda (x)
2684 (cond ((stringp (cdr x))
2685 (push x prefix-descriptions)
2686 nil)
2687 ((stringp (nth 1 x)) x)
2688 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2689 (t (cons (car x) (cons "" (cdr x))))))
2690 org-agenda-custom-commands)))
2691 (org-agenda-custom-commands
2692 (org-contextualize-keys
2693 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2694 (buf (current-buffer))
2695 (bfn (buffer-file-name (buffer-base-buffer)))
2696 entry key type org-match lprops ans)
2697 ;; Turn off restriction unless there is an overriding one,
2698 (unless org-agenda-overriding-restriction
2699 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2700 ;; There is a request to keep the file list in place
2701 (put 'org-agenda-files 'org-restrict nil))
2702 (setq org-agenda-restrict nil)
2703 (move-marker org-agenda-restrict-begin nil)
2704 (move-marker org-agenda-restrict-end nil))
2705 ;; Delete old local properties
2706 (put 'org-agenda-redo-command 'org-lprops nil)
2707 ;; Delete previously set last-arguments
2708 (put 'org-agenda-redo-command 'last-args nil)
2709 ;; Remember where this call originated
2710 (setq org-agenda-last-dispatch-buffer (current-buffer))
2711 (unless org-keys
2712 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2713 org-keys (car ans)
2714 restriction (cdr ans)))
2715 ;; If we have sticky agenda buffers, set a name for the buffer,
2716 ;; depending on the invoking keys. The user may still set this
2717 ;; as a command option, which will overwrite what we do here.
2718 (if org-agenda-sticky
2719 (setq org-agenda-buffer-name
2720 (format "*Org Agenda(%s)*" org-keys)))
2721 ;; Establish the restriction, if any
2722 (when (and (not org-agenda-overriding-restriction) restriction)
2723 (put 'org-agenda-files 'org-restrict (list bfn))
2724 (cond
2725 ((eq restriction 'region)
2726 (setq org-agenda-restrict t)
2727 (move-marker org-agenda-restrict-begin (region-beginning))
2728 (move-marker org-agenda-restrict-end (region-end)))
2729 ((eq restriction 'subtree)
2730 (save-excursion
2731 (setq org-agenda-restrict t)
2732 (org-back-to-heading t)
2733 (move-marker org-agenda-restrict-begin (point))
2734 (move-marker org-agenda-restrict-end
2735 (progn (org-end-of-subtree t)))))))
2737 ;; For example the todo list should not need it (but does...)
2738 (cond
2739 ((setq entry (assoc org-keys org-agenda-custom-commands))
2740 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2741 (progn
2742 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2743 lprops (nth 4 entry))
2744 (if org-agenda-sticky
2745 (setq org-agenda-buffer-name
2746 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2747 (format "*Org Agenda(%s)*" org-keys))))
2748 (put 'org-agenda-redo-command 'org-lprops lprops)
2749 (cond
2750 ((eq type 'agenda)
2751 (org-let lprops '(org-agenda-list current-prefix-arg)))
2752 ((eq type 'agenda*)
2753 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2754 ((eq type 'alltodo)
2755 (org-let lprops '(org-todo-list current-prefix-arg)))
2756 ((eq type 'search)
2757 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2758 ((eq type 'stuck)
2759 (org-let lprops '(org-agenda-list-stuck-projects
2760 current-prefix-arg)))
2761 ((eq type 'tags)
2762 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2763 ((eq type 'tags-todo)
2764 (org-let lprops '(org-tags-view '(4) org-match)))
2765 ((eq type 'todo)
2766 (org-let lprops '(org-todo-list org-match)))
2767 ((eq type 'tags-tree)
2768 (org-check-for-org-mode)
2769 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2770 ((eq type 'todo-tree)
2771 (org-check-for-org-mode)
2772 (org-let lprops
2773 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2774 (regexp-quote org-match) "\\>"))))
2775 ((eq type 'occur-tree)
2776 (org-check-for-org-mode)
2777 (org-let lprops '(org-occur org-match)))
2778 ((functionp type)
2779 (org-let lprops '(funcall type org-match)))
2780 ((fboundp type)
2781 (org-let lprops '(funcall type org-match)))
2782 (t (error "Invalid custom agenda command type %s" type))))
2783 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2784 ((equal org-keys "C")
2785 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2786 (customize-variable 'org-agenda-custom-commands))
2787 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2788 ((equal org-keys "s") (call-interactively 'org-search-view))
2789 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2790 ((equal org-keys "t") (call-interactively 'org-todo-list))
2791 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2792 ((equal org-keys "m") (call-interactively 'org-tags-view))
2793 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2794 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2795 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2796 (org-add-hook
2797 'post-command-hook
2798 (lambda ()
2799 (unless (current-message)
2800 (let* ((m (org-agenda-get-any-marker))
2801 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2802 (when note
2803 (message (concat
2804 "FLAGGING-NOTE ([?] for more info): "
2805 (org-add-props
2806 (replace-regexp-in-string
2807 "\\\\n" "//"
2808 (copy-sequence note))
2809 nil 'face 'org-warning)))))))
2810 t t))
2811 ((equal org-keys "L")
2812 (unless (derived-mode-p 'org-mode)
2813 (error "This is not an Org-mode file"))
2814 (unless restriction
2815 (put 'org-agenda-files 'org-restrict (list bfn))
2816 (org-call-with-arg 'org-timeline arg)))
2817 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2818 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2819 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2820 (t (error "Invalid agenda key"))))))
2822 (defun org-agenda-append-agenda ()
2823 "Append another agenda view to the current one.
2824 This function allows interactive building of block agendas.
2825 Agenda views are separated by `org-agenda-block-separator'."
2826 (interactive)
2827 (unless (derived-mode-p 'org-agenda-mode)
2828 (error "Can only append from within agenda buffer"))
2829 (let ((org-agenda-multi t))
2830 (org-agenda)
2831 (widen)
2832 (org-agenda-finalize)
2833 (org-agenda-fit-window-to-buffer)))
2835 (defun org-agenda-normalize-custom-commands (cmds)
2836 "Normalize custom commands CMDS."
2837 (delq nil
2838 (mapcar
2839 (lambda (x)
2840 (cond ((stringp (cdr x)) nil)
2841 ((stringp (nth 1 x)) x)
2842 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2843 (t (cons (car x) (cons "" (cdr x))))))
2844 cmds)))
2846 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2847 "The user interface for selecting an agenda command."
2848 (catch 'exit
2849 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2850 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2851 (region-p (org-region-active-p))
2852 (custom org-agenda-custom-commands)
2853 (selstring "")
2854 restriction second-time
2855 c entry key type match prefixes rmheader header-end custom1 desc
2856 line lines left right n n1)
2857 (save-window-excursion
2858 (delete-other-windows)
2859 (org-switch-to-buffer-other-window " *Agenda Commands*")
2860 (erase-buffer)
2861 (insert (eval-when-compile
2862 (let ((header
2863 "Press key for an agenda command: < Buffer, subtree/region restriction
2864 -------------------------------- > Remove restriction
2865 a Agenda for current week or day e Export agenda views
2866 t List of all TODO entries T Entries with special TODO kwd
2867 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2868 s Search for keywords S Like s, but only TODO entries
2869 L Timeline for current buffer # List stuck projects (!=configure)
2870 / Multi-occur C Configure custom agenda commands
2871 ? Find :FLAGGED: entries * Toggle sticky agenda views
2873 (start 0))
2874 (while (string-match
2875 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2876 header start)
2877 (setq start (match-end 0))
2878 (add-text-properties (match-beginning 2) (match-end 2)
2879 '(face bold) header))
2880 header)))
2881 (setq header-end (point-marker))
2882 (while t
2883 (setq custom1 custom)
2884 (when (eq rmheader t)
2885 (org-goto-line 1)
2886 (re-search-forward ":" nil t)
2887 (delete-region (match-end 0) (point-at-eol))
2888 (forward-char 1)
2889 (looking-at "-+")
2890 (delete-region (match-end 0) (point-at-eol))
2891 (move-marker header-end (match-end 0)))
2892 (goto-char header-end)
2893 (delete-region (point) (point-max))
2895 ;; Produce all the lines that describe custom commands and prefixes
2896 (setq lines nil)
2897 (while (setq entry (pop custom1))
2898 (setq key (car entry) desc (nth 1 entry)
2899 type (nth 2 entry)
2900 match (nth 3 entry))
2901 (if (> (length key) 1)
2902 (add-to-list 'prefixes (string-to-char key))
2903 (setq line
2904 (format
2905 "%-4s%-14s"
2906 (org-add-props (copy-sequence key)
2907 '(face bold))
2908 (cond
2909 ((string-match "\\S-" desc) desc)
2910 ((eq type 'agenda) "Agenda for current week or day")
2911 ((eq type 'agenda*) "Appointments for current week or day")
2912 ((eq type 'alltodo) "List of all TODO entries")
2913 ((eq type 'search) "Word search")
2914 ((eq type 'stuck) "List of stuck projects")
2915 ((eq type 'todo) "TODO keyword")
2916 ((eq type 'tags) "Tags query")
2917 ((eq type 'tags-todo) "Tags (TODO)")
2918 ((eq type 'tags-tree) "Tags tree")
2919 ((eq type 'todo-tree) "TODO kwd tree")
2920 ((eq type 'occur-tree) "Occur tree")
2921 ((functionp type) (if (symbolp type)
2922 (symbol-name type)
2923 "Lambda expression"))
2924 (t "???"))))
2925 (if org-agenda-menu-show-matcher
2926 (setq line
2927 (concat line ": "
2928 (cond
2929 ((stringp match)
2930 (setq match (copy-sequence match))
2931 (org-add-props match nil 'face 'org-warning))
2932 ((listp type)
2933 (format "set of %d commands" (length type))))))
2934 (if (org-string-nw-p match)
2935 (add-text-properties
2936 0 (length line) (list 'help-echo
2937 (concat "Matcher: " match)) line)))
2938 (push line lines)))
2939 (setq lines (nreverse lines))
2940 (when prefixes
2941 (mapc (lambda (x)
2942 (push
2943 (format "%s %s"
2944 (org-add-props (char-to-string x)
2945 nil 'face 'bold)
2946 (or (cdr (assoc (concat selstring
2947 (char-to-string x))
2948 prefix-descriptions))
2949 "Prefix key"))
2950 lines))
2951 prefixes))
2953 ;; Check if we should display in two columns
2954 (if org-agenda-menu-two-columns
2955 (progn
2956 (setq n (length lines)
2957 n1 (+ (/ n 2) (mod n 2))
2958 right (nthcdr n1 lines)
2959 left (copy-sequence lines))
2960 (setcdr (nthcdr (1- n1) left) nil))
2961 (setq left lines right nil))
2962 (while left
2963 (insert "\n" (pop left))
2964 (when right
2965 (if (< (current-column) 40)
2966 (move-to-column 40 t)
2967 (insert " "))
2968 (insert (pop right))))
2970 ;; Make the window the right size
2971 (goto-char (point-min))
2972 (if second-time
2973 (if (not (pos-visible-in-window-p (point-max)))
2974 (org-fit-window-to-buffer))
2975 (setq second-time t)
2976 (org-fit-window-to-buffer))
2978 ;; Ask for selection
2979 (message "Press key for agenda command%s:"
2980 (if (or restrict-ok org-agenda-overriding-restriction)
2981 (if org-agenda-overriding-restriction
2982 " (restriction lock active)"
2983 (if restriction
2984 (format " (restricted to %s)" restriction)
2985 " (unrestricted)"))
2986 ""))
2987 (setq c (read-char-exclusive))
2988 (message "")
2989 (cond
2990 ((assoc (char-to-string c) custom)
2991 (setq selstring (concat selstring (char-to-string c)))
2992 (throw 'exit (cons selstring restriction)))
2993 ((memq c prefixes)
2994 (setq selstring (concat selstring (char-to-string c))
2995 prefixes nil
2996 rmheader (or rmheader t)
2997 custom (delq nil (mapcar
2998 (lambda (x)
2999 (if (or (= (length (car x)) 1)
3000 (/= (string-to-char (car x)) c))
3002 (cons (substring (car x) 1) (cdr x))))
3003 custom))))
3004 ((eq c ?*)
3005 (call-interactively 'org-toggle-sticky-agenda)
3006 (sit-for 2))
3007 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3008 (message "Restriction is only possible in Org-mode buffers")
3009 (ding) (sit-for 1))
3010 ((eq c ?1)
3011 (org-agenda-remove-restriction-lock 'noupdate)
3012 (setq restriction 'buffer))
3013 ((eq c ?0)
3014 (org-agenda-remove-restriction-lock 'noupdate)
3015 (setq restriction (if region-p 'region 'subtree)))
3016 ((eq c ?<)
3017 (org-agenda-remove-restriction-lock 'noupdate)
3018 (setq restriction
3019 (cond
3020 ((eq restriction 'buffer)
3021 (if region-p 'region 'subtree))
3022 ((memq restriction '(subtree region))
3023 nil)
3024 (t 'buffer))))
3025 ((eq c ?>)
3026 (org-agenda-remove-restriction-lock 'noupdate)
3027 (setq restriction nil))
3028 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3029 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3030 ((and (> (length selstring) 0) (eq c ?\d))
3031 (delete-window)
3032 (org-agenda-get-restriction-and-command prefix-descriptions))
3034 ((equal c ?q) (error "Abort"))
3035 (t (error "Invalid key %c" c))))))))
3037 (defun org-agenda-fit-window-to-buffer ()
3038 "Fit the window to the buffer size."
3039 (and (memq org-agenda-window-setup '(reorganize-frame))
3040 (fboundp 'fit-window-to-buffer)
3041 (org-fit-window-to-buffer
3043 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3044 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3046 (defvar org-cmd nil)
3047 (defvar org-agenda-overriding-cmd nil)
3048 (defvar org-agenda-overriding-arguments nil)
3049 (defvar org-agenda-overriding-cmd-arguments nil)
3050 (defun org-agenda-run-series (name series)
3051 "Run agenda NAME as a SERIES of agenda commands."
3052 (org-let (nth 1 series) '(org-agenda-prepare name))
3053 ;; We need to reset agenda markers here, because when constructing a
3054 ;; block agenda, the individual blocks do not do that.
3055 (org-agenda-reset-markers)
3056 (let* ((org-agenda-multi t)
3057 (redo (list 'org-agenda-run-series name (list 'quote series)))
3058 (cmds (car series))
3059 (gprops (nth 1 series))
3060 match ;; The byte compiler incorrectly complains about this. Keep it!
3061 org-cmd type lprops)
3062 (while (setq org-cmd (pop cmds))
3063 (setq type (car org-cmd)
3064 match (eval (nth 1 org-cmd))
3065 lprops (nth 2 org-cmd))
3066 (let ((org-agenda-overriding-arguments
3067 (if (eq org-agenda-overriding-cmd org-cmd)
3068 (or org-agenda-overriding-arguments
3069 org-agenda-overriding-cmd-arguments))))
3070 (cond
3071 ((eq type 'agenda)
3072 (org-let2 gprops lprops
3073 '(call-interactively 'org-agenda-list)))
3074 ((eq type 'agenda*)
3075 (org-let2 gprops lprops
3076 '(funcall 'org-agenda-list nil nil t)))
3077 ((eq type 'alltodo)
3078 (org-let2 gprops lprops
3079 '(call-interactively 'org-todo-list)))
3080 ((eq type 'search)
3081 (org-let2 gprops lprops
3082 '(org-search-view current-prefix-arg match nil)))
3083 ((eq type 'stuck)
3084 (org-let2 gprops lprops
3085 '(call-interactively 'org-agenda-list-stuck-projects)))
3086 ((eq type 'tags)
3087 (org-let2 gprops lprops
3088 '(org-tags-view current-prefix-arg match)))
3089 ((eq type 'tags-todo)
3090 (org-let2 gprops lprops
3091 '(org-tags-view '(4) match)))
3092 ((eq type 'todo)
3093 (org-let2 gprops lprops
3094 '(org-todo-list match)))
3095 ((fboundp type)
3096 (org-let2 gprops lprops
3097 '(funcall type match)))
3098 (t (error "Invalid type in command series")))))
3099 (widen)
3100 (let ((inhibit-read-only t))
3101 (add-text-properties (point-min) (point-max)
3102 `(org-series t org-series-redo-cmd ,redo)))
3103 (setq org-agenda-redo-command redo)
3104 (goto-char (point-min)))
3105 (org-agenda-fit-window-to-buffer)
3106 (org-let (nth 1 series) '(org-agenda-finalize)))
3108 ;;;###autoload
3109 (defmacro org-batch-agenda (cmd-key &rest parameters)
3110 "Run an agenda command in batch mode and send the result to STDOUT.
3111 If CMD-KEY is a string of length 1, it is used as a key in
3112 `org-agenda-custom-commands' and triggers this command. If it is a
3113 longer string it is used as a tags/todo match string.
3114 Parameters are alternating variable names and values that will be bound
3115 before running the agenda command."
3116 (org-eval-in-environment (org-make-parameter-alist parameters)
3117 (if (> (length cmd-key) 2)
3118 (org-tags-view nil cmd-key)
3119 (org-agenda nil cmd-key)))
3120 (set-buffer org-agenda-buffer-name)
3121 (princ (buffer-string)))
3123 (defvar org-agenda-info nil)
3125 ;;;###autoload
3126 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3127 "Run an agenda command in batch mode and send the result to STDOUT.
3128 If CMD-KEY is a string of length 1, it is used as a key in
3129 `org-agenda-custom-commands' and triggers this command. If it is a
3130 longer string it is used as a tags/todo match string.
3131 Parameters are alternating variable names and values that will be bound
3132 before running the agenda command.
3134 The output gives a line for each selected agenda item. Each
3135 item is a list of comma-separated values, like this:
3137 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3139 category The category of the item
3140 head The headline, without TODO kwd, TAGS and PRIORITY
3141 type The type of the agenda entry, can be
3142 todo selected in TODO match
3143 tagsmatch selected in tags match
3144 diary imported from diary
3145 deadline a deadline on given date
3146 scheduled scheduled on given date
3147 timestamp entry has timestamp on given date
3148 closed entry was closed on given date
3149 upcoming-deadline warning about deadline
3150 past-scheduled forwarded scheduled item
3151 block entry has date block including g. date
3152 todo The todo keyword, if any
3153 tags All tags including inherited ones, separated by colons
3154 date The relevant date, like 2007-2-14
3155 time The time, like 15:00-16:50
3156 extra Sting with extra planning info
3157 priority-l The priority letter if any was given
3158 priority-n The computed numerical priority
3159 agenda-day The day in the agenda where this is listed"
3160 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3161 (org-make-parameter-alist parameters))
3162 (if (> (length cmd-key) 2)
3163 (org-tags-view nil cmd-key)
3164 (org-agenda nil cmd-key)))
3165 (set-buffer org-agenda-buffer-name)
3166 (let* ((lines (org-split-string (buffer-string) "\n"))
3167 line)
3168 (while (setq line (pop lines))
3169 (catch 'next
3170 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3171 (setq org-agenda-info
3172 (org-fix-agenda-info (text-properties-at 0 line)))
3173 (princ
3174 (mapconcat 'org-agenda-export-csv-mapper
3175 '(org-category txt type todo tags date time extra
3176 priority-letter priority agenda-day)
3177 ","))
3178 (princ "\n")))))
3180 (defun org-fix-agenda-info (props)
3181 "Make sure all properties on an agenda item have a canonical form.
3182 This ensures the export commands can easily use it."
3183 (let (tmp re)
3184 (when (setq tmp (plist-get props 'tags))
3185 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3186 (when (setq tmp (plist-get props 'date))
3187 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3188 (let ((calendar-date-display-form '(year "-" month "-" day)))
3189 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3191 (setq tmp (calendar-date-string tmp)))
3192 (setq props (plist-put props 'date tmp)))
3193 (when (setq tmp (plist-get props 'day))
3194 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3195 (let ((calendar-date-display-form '(year "-" month "-" day)))
3196 (setq tmp (calendar-date-string tmp)))
3197 (setq props (plist-put props 'day tmp))
3198 (setq props (plist-put props 'agenda-day tmp)))
3199 (when (setq tmp (plist-get props 'txt))
3200 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3201 (plist-put props 'priority-letter (match-string 1 tmp))
3202 (setq tmp (replace-match "" t t tmp)))
3203 (when (and (setq re (plist-get props 'org-todo-regexp))
3204 (setq re (concat "\\`\\.*" re " ?"))
3205 (string-match re tmp))
3206 (plist-put props 'todo (match-string 1 tmp))
3207 (setq tmp (replace-match "" t t tmp)))
3208 (plist-put props 'txt tmp)))
3209 props)
3211 (defun org-agenda-export-csv-mapper (prop)
3212 (let ((res (plist-get org-agenda-info prop)))
3213 (setq res
3214 (cond
3215 ((not res) "")
3216 ((stringp res) res)
3217 (t (prin1-to-string res))))
3218 (while (string-match "," res)
3219 (setq res (replace-match ";" t t res)))
3220 (org-trim res)))
3222 ;;;###autoload
3223 (defun org-store-agenda-views (&rest parameters)
3224 "Store agenda views."
3225 (interactive)
3226 (eval (list 'org-batch-store-agenda-views)))
3228 ;;;###autoload
3229 (defmacro org-batch-store-agenda-views (&rest parameters)
3230 "Run all custom agenda commands that have a file argument."
3231 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3232 (pop-up-frames nil)
3233 (dir default-directory)
3234 (pars (org-make-parameter-alist parameters))
3235 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3236 (save-window-excursion
3237 (while cmds
3238 (setq cmd (pop cmds)
3239 thiscmdkey (car cmd)
3240 thiscmdcmd (cdr cmd)
3241 match (nth 2 thiscmdcmd)
3242 bufname (if org-agenda-sticky
3243 (or (and (stringp match)
3244 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3245 (format "*Org Agenda(%s)*" thiscmdkey))
3246 org-agenda-buffer-name)
3247 cmd-or-set (nth 2 cmd)
3248 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3249 files (nth (if (listp cmd-or-set) 4 5) cmd))
3250 (if (stringp files) (setq files (list files)))
3251 (when files
3252 (org-eval-in-environment (append org-agenda-exporter-settings
3253 opts pars)
3254 (org-agenda nil thiscmdkey))
3255 (set-buffer bufname)
3256 (while files
3257 (org-eval-in-environment (append org-agenda-exporter-settings
3258 opts pars)
3259 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3260 (and (get-buffer bufname)
3261 (kill-buffer bufname)))))))
3263 (defvar org-agenda-current-span nil
3264 "The current span used in the agenda view.") ; local variable in the agenda buffer
3265 (defun org-agenda-mark-header-line (pos)
3266 "Mark the line at POS as an agenda structure header."
3267 (save-excursion
3268 (goto-char pos)
3269 (put-text-property (point-at-bol) (point-at-eol)
3270 'org-agenda-structural-header t)
3271 (when org-agenda-title-append
3272 (put-text-property (point-at-bol) (point-at-eol)
3273 'org-agenda-title-append org-agenda-title-append))))
3275 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3276 (defvar org-agenda-write-buffer-name "Agenda View")
3277 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3278 "Write the current buffer (an agenda view) as a file.
3279 Depending on the extension of the file name, plain text (.txt),
3280 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3281 If the extension is .ics, run icalendar export over all files used
3282 to construct the agenda and limit the export to entries listed in the
3283 agenda now.
3284 If the extension is .org, collect all subtrees corresponding to the
3285 agenda entries and add them in an .org file.
3286 With prefix argument OPEN, open the new file immediately.
3287 If NOSETTINGS is given, do not scope the settings of
3288 `org-agenda-exporter-settings' into the export commands. This is used when
3289 the settings have already been scoped and we do not wish to overrule other,
3290 higher priority settings.
3291 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3292 (interactive "FWrite agenda to file: \nP")
3293 (if (or (not (file-writable-p file))
3294 (and (file-exists-p file)
3295 (if (called-interactively-p 'any)
3296 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3297 (error "Cannot write agenda to file %s" file))
3298 (org-let (if nosettings nil org-agenda-exporter-settings)
3299 '(save-excursion
3300 (save-window-excursion
3301 (org-agenda-mark-filtered-text)
3302 (let ((bs (copy-sequence (buffer-string))) beg content)
3303 (org-agenda-unmark-filtered-text)
3304 (with-temp-buffer
3305 (rename-buffer org-agenda-write-buffer-name t)
3306 (set-buffer-modified-p nil)
3307 (insert bs)
3308 (org-agenda-remove-marked-text 'org-filtered)
3309 (while (setq beg (text-property-any (point-min) (point-max)
3310 'org-filtered t))
3311 (delete-region
3312 beg (or (next-single-property-change beg 'org-filtered)
3313 (point-max))))
3314 (run-hooks 'org-agenda-before-write-hook)
3315 (cond
3316 ((org-bound-and-true-p org-mobile-creating-agendas)
3317 (org-mobile-write-agenda-for-mobile file))
3318 ((string-match "\\.org\\'" file)
3319 (let (content p m message-log-max)
3320 (goto-char (point-min))
3321 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3322 (goto-char p)
3323 (setq m (get-text-property (point) 'org-hd-marker))
3324 (when m
3325 (push (save-excursion
3326 (set-buffer (marker-buffer m))
3327 (goto-char m)
3328 (org-copy-subtree 1 nil t t)
3329 org-subtree-clip)
3330 content)))
3331 (find-file file)
3332 (erase-buffer)
3333 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3334 (write-file file)
3335 (kill-buffer (current-buffer))
3336 (message "Org file written to %s" file)))
3337 ((string-match "\\.html?\\'" file)
3338 (require 'htmlize)
3339 (set-buffer (htmlize-buffer (current-buffer)))
3340 (when org-agenda-export-html-style
3341 ;; replace <style> section with org-agenda-export-html-style
3342 (goto-char (point-min))
3343 (kill-region (- (search-forward "<style") 6)
3344 (search-forward "</style>"))
3345 (insert org-agenda-export-html-style))
3346 (write-file file)
3347 (kill-buffer (current-buffer))
3348 (message "HTML written to %s" file))
3349 ((string-match "\\.ps\\'" file)
3350 (require 'ps-print)
3351 (ps-print-buffer-with-faces file)
3352 (message "Postscript written to %s" file))
3353 ((string-match "\\.pdf\\'" file)
3354 (require 'ps-print)
3355 (ps-print-buffer-with-faces
3356 (concat (file-name-sans-extension file) ".ps"))
3357 (call-process "ps2pdf" nil nil nil
3358 (expand-file-name
3359 (concat (file-name-sans-extension file) ".ps"))
3360 (expand-file-name file))
3361 (delete-file (concat (file-name-sans-extension file) ".ps"))
3362 (message "PDF written to %s" file))
3363 ((string-match "\\.ics\\'" file)
3364 (require 'ox-icalendar)
3365 (org-icalendar-export-current-agenda (expand-file-name file)))
3367 (let ((bs (buffer-string)))
3368 (find-file file)
3369 (erase-buffer)
3370 (insert bs)
3371 (save-buffer 0)
3372 (kill-buffer (current-buffer))
3373 (message "Plain text written to %s" file))))))))
3374 (set-buffer (or agenda-bufname
3375 (and (org-called-interactively-p 'any) (buffer-name))
3376 org-agenda-buffer-name)))
3377 (when open (org-open-file file)))
3379 (defvar org-agenda-tag-filter-overlays nil)
3380 (defvar org-agenda-cat-filter-overlays nil)
3381 (defvar org-agenda-re-filter-overlays nil)
3383 (defun org-agenda-mark-filtered-text ()
3384 "Mark all text hidden by filtering with a text property."
3385 (let ((inhibit-read-only t))
3386 (mapc
3387 (lambda (o)
3388 (when (equal (overlay-buffer o) (current-buffer))
3389 (put-text-property
3390 (overlay-start o) (overlay-end o)
3391 'org-filtered t)))
3392 (append org-agenda-tag-filter-overlays
3393 org-agenda-cat-filter-overlays
3394 org-agenda-re-filter-overlays))))
3396 (defun org-agenda-unmark-filtered-text ()
3397 "Remove the filtering text property."
3398 (let ((inhibit-read-only t))
3399 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3401 (defun org-agenda-remove-marked-text (property &optional value)
3402 "Delete all text marked with VALUE of PROPERTY.
3403 VALUE defaults to t."
3404 (let (beg)
3405 (setq value (or value t))
3406 (while (setq beg (text-property-any (point-min) (point-max)
3407 property value))
3408 (delete-region
3409 beg (or (next-single-property-change beg 'org-filtered)
3410 (point-max))))))
3412 (defun org-agenda-add-entry-text ()
3413 "Add entry text to agenda lines.
3414 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3415 entry text following headings shown in the agenda.
3416 Drawers will be excluded, also the line with scheduling/deadline info."
3417 (when (and (> org-agenda-add-entry-text-maxlines 0)
3418 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3419 (let (m txt)
3420 (goto-char (point-min))
3421 (while (not (eobp))
3422 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3423 (beginning-of-line 2)
3424 (setq txt (org-agenda-get-some-entry-text
3425 m org-agenda-add-entry-text-maxlines " > "))
3426 (end-of-line 1)
3427 (if (string-match "\\S-" txt)
3428 (insert "\n" txt)
3429 (or (eobp) (forward-char 1))))))))
3431 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3432 &rest keep)
3433 "Extract entry text from MARKER, at most N-LINES lines.
3434 This will ignore drawers etc, just get the text.
3435 If INDENT is given, prefix every line with this string. If KEEP is
3436 given, it is a list of symbols, defining stuff that should not be
3437 removed from the entry content. Currently only `planning' is allowed here."
3438 (let (txt drawer-re kwd-time-re ind)
3439 (save-excursion
3440 (with-current-buffer (marker-buffer marker)
3441 (if (not (derived-mode-p 'org-mode))
3442 (setq txt "")
3443 (save-excursion
3444 (save-restriction
3445 (widen)
3446 (goto-char marker)
3447 (end-of-line 1)
3448 (setq txt (buffer-substring
3449 (min (1+ (point)) (point-max))
3450 (progn (outline-next-heading) (point)))
3451 drawer-re org-drawer-regexp
3452 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3453 ".*\n?"))
3454 (with-temp-buffer
3455 (insert txt)
3456 (when org-agenda-add-entry-text-descriptive-links
3457 (goto-char (point-min))
3458 (while (org-activate-bracket-links (point-max))
3459 (add-text-properties (match-beginning 0) (match-end 0)
3460 '(face org-link))))
3461 (goto-char (point-min))
3462 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3463 (set-text-properties (match-beginning 0) (match-end 0)
3464 nil))
3465 (goto-char (point-min))
3466 (while (re-search-forward drawer-re nil t)
3467 (delete-region
3468 (match-beginning 0)
3469 (progn (re-search-forward
3470 "^[ \t]*:END:.*\n?" nil 'move)
3471 (point))))
3472 (unless (member 'planning keep)
3473 (goto-char (point-min))
3474 (while (re-search-forward kwd-time-re nil t)
3475 (replace-match "")))
3476 (goto-char (point-min))
3477 (when org-agenda-entry-text-exclude-regexps
3478 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3479 (while (setq re (pop re-list))
3480 (goto-char (point-min))
3481 (while (re-search-forward re nil t)
3482 (replace-match "")))))
3483 (goto-char (point-max))
3484 (skip-chars-backward " \t\n")
3485 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3487 ;; find and remove min common indentation
3488 (goto-char (point-min))
3489 (untabify (point-min) (point-max))
3490 (setq ind (org-get-indentation))
3491 (while (not (eobp))
3492 (unless (looking-at "[ \t]*$")
3493 (setq ind (min ind (org-get-indentation))))
3494 (beginning-of-line 2))
3495 (goto-char (point-min))
3496 (while (not (eobp))
3497 (unless (looking-at "[ \t]*$")
3498 (move-to-column ind)
3499 (delete-region (point-at-bol) (point)))
3500 (beginning-of-line 2))
3502 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3504 (goto-char (point-min))
3505 (when indent
3506 (while (and (not (eobp)) (re-search-forward "^" nil t))
3507 (replace-match indent t t)))
3508 (goto-char (point-min))
3509 (while (looking-at "[ \t]*\n") (replace-match ""))
3510 (goto-char (point-max))
3511 (when (> (org-current-line)
3512 n-lines)
3513 (org-goto-line (1+ n-lines))
3514 (backward-char 1))
3515 (setq txt (buffer-substring (point-min) (point)))))))))
3516 txt))
3518 (defun org-check-for-org-mode ()
3519 "Make sure current buffer is in org-mode. Error if not."
3520 (or (derived-mode-p 'org-mode)
3521 (error "Cannot execute org-mode agenda command on buffer in %s"
3522 major-mode)))
3524 ;;; Agenda prepare and finalize
3526 (defvar org-agenda-multi nil) ; dynamically scoped
3527 (defvar org-agenda-pre-window-conf nil)
3528 (defvar org-agenda-columns-active nil)
3529 (defvar org-agenda-name nil)
3530 (defvar org-agenda-tag-filter nil)
3531 (defvar org-agenda-category-filter nil)
3532 (defvar org-agenda-regexp-filter nil)
3533 (defvar org-agenda-top-category-filter nil)
3534 (defvar org-agenda-tag-filter-while-redo nil)
3535 (defvar org-agenda-tag-filter-preset nil
3536 "A preset of the tags filter used for secondary agenda filtering.
3537 This must be a list of strings, each string must be a single tag preceded
3538 by \"+\" or \"-\".
3539 This variable should not be set directly, but agenda custom commands can
3540 bind it in the options section. The preset filter is a global property of
3541 the entire agenda view. In a block agenda, it will not work reliably to
3542 define a filter for one of the individual blocks. You need to set it in
3543 the global options and expect it to be applied to the entire view.")
3545 (defvar org-agenda-category-filter-preset nil
3546 "A preset of the category filter used for secondary agenda filtering.
3547 This must be a list of strings, each string must be a single category
3548 preceded by \"+\" or \"-\".
3549 This variable should not be set directly, but agenda custom commands can
3550 bind it in the options section. The preset filter is a global property of
3551 the entire agenda view. In a block agenda, it will not work reliably to
3552 define a filter for one of the individual blocks. You need to set it in
3553 the global options and expect it to be applied to the entire view.")
3555 (defvar org-agenda-regexp-filter-preset nil
3556 "A preset of the regexp filter used for secondary agenda filtering.
3557 This must be a list of strings, each string must be a single category
3558 preceded by \"+\" or \"-\".
3559 This variable should not be set directly, but agenda custom commands can
3560 bind it in the options section. The preset filter is a global property of
3561 the entire agenda view. In a block agenda, it will not work reliably to
3562 define a filter for one of the individual blocks. You need to set it in
3563 the global options and expect it to be applied to the entire view.")
3565 (defun org-agenda-use-sticky-p ()
3566 "Return non-nil if an agenda buffer named
3567 `org-agenda-buffer-name' exists and should be shown instead of
3568 generating a new one."
3569 (and
3570 ;; turned off by user
3571 org-agenda-sticky
3572 ;; For multi-agenda buffer already exists
3573 (not org-agenda-multi)
3574 ;; buffer found
3575 (get-buffer org-agenda-buffer-name)
3576 ;; C-u parameter is same as last call
3577 (with-current-buffer (get-buffer org-agenda-buffer-name)
3578 (and
3579 (equal current-prefix-arg
3580 org-agenda-last-prefix-arg)
3581 ;; In case user turned stickiness on, while having existing
3582 ;; Agenda buffer active, don't reuse that buffer, because it
3583 ;; does not have org variables local
3584 org-agenda-this-buffer-is-sticky))))
3586 (defun org-agenda-prepare-window (abuf)
3587 "Setup agenda buffer in the window."
3588 (let* ((awin (get-buffer-window abuf))
3589 wconf)
3590 (cond
3591 ((equal (current-buffer) abuf) nil)
3592 (awin (select-window awin))
3593 ((not (setq wconf (current-window-configuration))))
3594 ((equal org-agenda-window-setup 'current-window)
3595 (org-pop-to-buffer-same-window abuf))
3596 ((equal org-agenda-window-setup 'other-window)
3597 (org-switch-to-buffer-other-window abuf))
3598 ((equal org-agenda-window-setup 'other-frame)
3599 (switch-to-buffer-other-frame abuf))
3600 ((equal org-agenda-window-setup 'reorganize-frame)
3601 (delete-other-windows)
3602 (org-switch-to-buffer-other-window abuf)))
3603 ;; additional test in case agenda is invoked from within agenda
3604 ;; buffer via elisp link
3605 (unless (equal (current-buffer) abuf)
3606 (org-pop-to-buffer-same-window abuf))
3607 (setq org-agenda-pre-window-conf
3608 (or org-agenda-pre-window-conf wconf))))
3610 (defun org-agenda-prepare (&optional name)
3611 (if (org-agenda-use-sticky-p)
3612 (progn
3613 ;; Popup existing buffer
3614 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3615 (message "Sticky Agenda buffer, use `r' to refresh")
3616 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3617 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3618 (setq org-todo-keywords-for-agenda nil)
3619 (setq org-drawers-for-agenda nil)
3620 (unless org-agenda-persistent-filter
3621 (setq org-agenda-tag-filter nil
3622 org-agenda-category-filter nil
3623 org-agenda-regexp-filter nil))
3624 (put 'org-agenda-tag-filter :preset-filter
3625 org-agenda-tag-filter-preset)
3626 (put 'org-agenda-category-filter :preset-filter
3627 org-agenda-category-filter-preset)
3628 (put 'org-agenda-regexp-filter :preset-filter
3629 org-agenda-regexp-filter-preset)
3630 (if org-agenda-multi
3631 (progn
3632 (setq buffer-read-only nil)
3633 (goto-char (point-max))
3634 (unless (or (bobp) org-agenda-compact-blocks
3635 (not org-agenda-block-separator))
3636 (insert "\n"
3637 (if (stringp org-agenda-block-separator)
3638 org-agenda-block-separator
3639 (make-string (window-width) org-agenda-block-separator))
3640 "\n"))
3641 (narrow-to-region (point) (point-max)))
3642 (setq org-done-keywords-for-agenda nil)
3644 ;; Setting any org variables that are in org-agenda-local-vars
3645 ;; list need to be done after the prepare call
3646 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3647 (setq buffer-read-only nil)
3648 (org-agenda-reset-markers)
3649 (let ((inhibit-read-only t)) (erase-buffer))
3650 (org-agenda-mode)
3651 (setq org-agenda-buffer (current-buffer))
3652 (setq org-agenda-contributing-files nil)
3653 (setq org-agenda-columns-active nil)
3654 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3655 (setq org-todo-keywords-for-agenda
3656 (org-uniquify org-todo-keywords-for-agenda))
3657 (setq org-done-keywords-for-agenda
3658 (org-uniquify org-done-keywords-for-agenda))
3659 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3660 (setq org-agenda-last-prefix-arg current-prefix-arg)
3661 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3662 (and name (not org-agenda-name)
3663 (org-set-local 'org-agenda-name name)))
3664 (setq buffer-read-only nil)))
3666 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3667 (defun org-agenda-finalize ()
3668 "Finishing touch for the agenda buffer, called just before displaying it."
3669 (unless org-agenda-multi
3670 (save-excursion
3671 (let ((inhibit-read-only t))
3672 (goto-char (point-min))
3673 (save-excursion
3674 (while (org-activate-bracket-links (point-max))
3675 (add-text-properties (match-beginning 0) (match-end 0)
3676 '(face org-link))))
3677 (save-excursion
3678 (while (org-activate-plain-links (point-max))
3679 (add-text-properties (match-beginning 0) (match-end 0)
3680 '(face org-link))))
3681 (unless (eq org-agenda-remove-tags t)
3682 (org-agenda-align-tags))
3683 (unless org-agenda-with-colors
3684 (remove-text-properties (point-min) (point-max) '(face nil)))
3685 (if (and (boundp 'org-agenda-overriding-columns-format)
3686 org-agenda-overriding-columns-format)
3687 (org-set-local 'org-agenda-overriding-columns-format
3688 org-agenda-overriding-columns-format))
3689 (if (and (boundp 'org-agenda-view-columns-initially)
3690 org-agenda-view-columns-initially)
3691 (org-agenda-columns))
3692 (when org-agenda-fontify-priorities
3693 (org-agenda-fontify-priorities))
3694 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3695 (org-agenda-dim-blocked-tasks))
3696 ;; We need to widen when `org-agenda-finalize' is called from
3697 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3698 (when org-clock-current-task
3699 (save-restriction
3700 (widen)
3701 (org-agenda-mark-clocking-task)))
3702 (when org-agenda-entry-text-mode
3703 (org-agenda-entry-text-hide)
3704 (org-agenda-entry-text-show))
3705 (if (and (functionp 'org-habit-insert-consistency-graphs)
3706 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3707 (org-habit-insert-consistency-graphs))
3708 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3709 (unless (or (eq org-agenda-show-inherited-tags 'always)
3710 (and (listp org-agenda-show-inherited-tags)
3711 (memq org-agenda-type org-agenda-show-inherited-tags))
3712 (and (eq org-agenda-show-inherited-tags t)
3713 (or (eq org-agenda-use-tag-inheritance t)
3714 (and (listp org-agenda-use-tag-inheritance)
3715 (not (memq org-agenda-type
3716 org-agenda-use-tag-inheritance))))))
3717 (let (mrk)
3718 (save-excursion
3719 (goto-char (point-min))
3720 (while (equal (forward-line) 0)
3721 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3722 (get-text-property (point) 'org-hd-marker)))
3723 (put-text-property (point-at-bol) (point-at-eol)
3724 'tags (org-with-point-at mrk
3725 (delete-dups
3726 (mapcar 'downcase (org-get-tags-at))))))))))
3727 (run-hooks 'org-agenda-finalize-hook)
3728 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3729 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3730 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3731 (org-agenda-filter-apply org-agenda-category-filter 'category))
3732 (when (or org-agenda-regexp-filter (get 'org-agenda-regexp-filter :preset-filter))
3733 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3734 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3736 (defun org-agenda-mark-clocking-task ()
3737 "Mark the current clock entry in the agenda if it is present."
3738 (org-agenda-unmark-clocking-task)
3739 (when (marker-buffer org-clock-hd-marker)
3740 (save-excursion
3741 (goto-char (point-min))
3742 (let (s ov)
3743 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3744 (goto-char s)
3745 (when (equal (org-get-at-bol 'org-hd-marker)
3746 org-clock-hd-marker)
3747 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3748 (overlay-put ov 'type 'org-agenda-clocking)
3749 (overlay-put ov 'face 'org-agenda-clocking)
3750 (overlay-put ov 'help-echo
3751 "The clock is running in this item")))))))
3753 (defun org-agenda-unmark-clocking-task ()
3754 "Unmark the current clocking task."
3755 (mapc (lambda (o)
3756 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3757 (delete-overlay o)))
3758 (overlays-in (point-min) (point-max))))
3760 (defun org-agenda-fontify-priorities ()
3761 "Make highest priority lines bold, and lowest italic."
3762 (interactive)
3763 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3764 (delete-overlay o)))
3765 (overlays-in (point-min) (point-max)))
3766 (save-excursion
3767 (let (b e p ov h l)
3768 (goto-char (point-min))
3769 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3770 (setq h (or (get-char-property (point) 'org-highest-priority)
3771 org-highest-priority)
3772 l (or (get-char-property (point) 'org-lowest-priority)
3773 org-lowest-priority)
3774 p (string-to-char (match-string 1))
3775 b (match-beginning 0)
3776 e (if (eq org-agenda-fontify-priorities 'cookies)
3777 (match-end 0)
3778 (point-at-eol))
3779 ov (make-overlay b e))
3780 (overlay-put
3781 ov 'face
3782 (cons (cond ((org-face-from-face-or-color
3783 'priority nil
3784 (cdr (assoc p org-priority-faces))))
3785 ((and (listp org-agenda-fontify-priorities)
3786 (org-face-from-face-or-color
3787 'priority nil
3788 (cdr (assoc p org-agenda-fontify-priorities)))))
3789 ((equal p l) 'italic)
3790 ((equal p h) 'bold))
3791 'org-priority))
3792 (overlay-put ov 'org-type 'org-priority)))))
3794 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3795 "Dim currently blocked TODO's in the agenda display."
3796 (interactive "P")
3797 (when (org-called-interactively-p 'interactive)
3798 (message "Dim or hide blocked tasks..."))
3799 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3800 (delete-overlay o)))
3801 (overlays-in (point-min) (point-max)))
3802 (save-excursion
3803 (let ((inhibit-read-only t)
3804 (org-depend-tag-blocked nil)
3805 (invis (or (not (null invisible))
3806 (eq org-agenda-dim-blocked-tasks 'invisible)))
3807 org-blocked-by-checkboxes
3808 invis1 b e p ov h l)
3809 (goto-char (point-min))
3810 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3811 (and pos (goto-char (1+ pos))))
3812 (setq org-blocked-by-checkboxes nil invis1 invis)
3813 (let ((marker (org-get-at-bol 'org-hd-marker)))
3814 (when (and marker
3815 (with-current-buffer (marker-buffer marker)
3816 (save-excursion (goto-char marker)
3817 (org-entry-blocked-p))))
3818 (if org-blocked-by-checkboxes (setq invis1 nil))
3819 (setq b (if invis1
3820 (max (point-min) (1- (point-at-bol)))
3821 (point-at-bol))
3822 e (point-at-eol)
3823 ov (make-overlay b e))
3824 (if invis1
3825 (overlay-put ov 'invisible t)
3826 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3827 (overlay-put ov 'org-type 'org-blocked-todo))))))
3828 (when (org-called-interactively-p 'interactive)
3829 (message "Dim or hide blocked tasks...done")))
3831 (defvar org-agenda-skip-function nil
3832 "Function to be called at each match during agenda construction.
3833 If this function returns nil, the current match should not be skipped.
3834 Otherwise, the function must return a position from where the search
3835 should be continued.
3836 This may also be a Lisp form, it will be evaluated.
3837 Never set this variable using `setq' or so, because then it will apply
3838 to all future agenda commands. If you do want a global skipping condition,
3839 use the option `org-agenda-skip-function-global' instead.
3840 The correct usage for `org-agenda-skip-function' is to bind it with
3841 `let' to scope it dynamically into the agenda-constructing command.
3842 A good way to set it is through options in `org-agenda-custom-commands'.")
3844 (defun org-agenda-skip ()
3845 "Throw to `:skip' in places that should be skipped.
3846 Also moves point to the end of the skipped region, so that search can
3847 continue from there."
3848 (let ((p (point-at-bol)) to)
3849 (when (or
3850 (save-excursion (goto-char p) (looking-at comment-start-skip))
3851 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3852 (get-text-property p :org-archived)
3853 (org-end-of-subtree t))
3854 (and org-agenda-skip-comment-trees
3855 (get-text-property p :org-comment)
3856 (org-end-of-subtree t))
3857 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3858 (org-agenda-skip-eval org-agenda-skip-function)))
3859 (goto-char to))
3860 (org-in-src-block-p t))
3861 (throw :skip t))))
3863 (defun org-agenda-skip-eval (form)
3864 "If FORM is a function or a list, call (or eval) is and return result.
3865 `save-excursion' and `save-match-data' are wrapped around the call, so point
3866 and match data are returned to the previous state no matter what these
3867 functions do."
3868 (let (fp)
3869 (and form
3870 (or (setq fp (functionp form))
3871 (consp form))
3872 (save-excursion
3873 (save-match-data
3874 (if fp
3875 (funcall form)
3876 (eval form)))))))
3878 (defvar org-agenda-markers nil
3879 "List of all currently active markers created by `org-agenda'.")
3880 (defvar org-agenda-last-marker-time (org-float-time)
3881 "Creation time of the last agenda marker.")
3883 (defun org-agenda-new-marker (&optional pos)
3884 "Return a new agenda marker.
3885 Org-mode keeps a list of these markers and resets them when they are
3886 no longer in use."
3887 (let ((m (copy-marker (or pos (point)))))
3888 (setq org-agenda-last-marker-time (org-float-time))
3889 (if org-agenda-buffer
3890 (with-current-buffer org-agenda-buffer
3891 (push m org-agenda-markers))
3892 (push m org-agenda-markers))
3895 (defun org-agenda-reset-markers ()
3896 "Reset markers created by `org-agenda'."
3897 (while org-agenda-markers
3898 (move-marker (pop org-agenda-markers) nil)))
3900 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3901 "Save relative positions of markers in region.
3902 This check for agenda markers in all agenda buffers currently active."
3903 (dolist (buf (buffer-list))
3904 (with-current-buffer buf
3905 (when (eq major-mode 'org-agenda-mode)
3906 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3907 org-agenda-markers)))))
3909 ;;; Entry text mode
3911 (defun org-agenda-entry-text-show-here ()
3912 "Add some text from the entry as context to the current line."
3913 (let (m txt o)
3914 (setq m (org-get-at-bol 'org-hd-marker))
3915 (unless (marker-buffer m)
3916 (error "No marker points to an entry here"))
3917 (setq txt (concat "\n" (org-no-properties
3918 (org-agenda-get-some-entry-text
3919 m org-agenda-entry-text-maxlines
3920 org-agenda-entry-text-leaders))))
3921 (when (string-match "\\S-" txt)
3922 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3923 (overlay-put o 'evaporate t)
3924 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3925 (overlay-put o 'after-string txt))))
3927 (defun org-agenda-entry-text-show ()
3928 "Add entry context for all agenda lines."
3929 (interactive)
3930 (save-excursion
3931 (goto-char (point-max))
3932 (beginning-of-line 1)
3933 (while (not (bobp))
3934 (when (org-get-at-bol 'org-hd-marker)
3935 (org-agenda-entry-text-show-here))
3936 (beginning-of-line 0))))
3938 (defun org-agenda-entry-text-hide ()
3939 "Remove any shown entry context."
3940 (delq nil
3941 (mapcar (lambda (o)
3942 (if (eq (overlay-get o 'org-overlay-type)
3943 'agenda-entry-content)
3944 (progn (delete-overlay o) t)))
3945 (overlays-in (point-min) (point-max)))))
3947 (defun org-agenda-get-day-face (date)
3948 "Return the face DATE should be displayed with."
3949 (or (and (functionp org-agenda-day-face-function)
3950 (funcall org-agenda-day-face-function date))
3951 (cond ((org-agenda-todayp date)
3952 'org-agenda-date-today)
3953 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3954 'org-agenda-date-weekend)
3955 (t 'org-agenda-date))))
3957 ;;; Agenda timeline
3959 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3961 (defun org-timeline (&optional dotodo)
3962 "Show a time-sorted view of the entries in the current org file.
3963 Only entries with a time stamp of today or later will be listed. With
3964 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3965 under the current date.
3966 If the buffer contains an active region, only check the region for
3967 dates."
3968 (interactive "P")
3969 (let* ((dopast t)
3970 (org-agenda-show-log-scoped org-agenda-show-log)
3971 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3972 (current-buffer))))
3973 (date (calendar-current-date))
3974 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3975 (end (if (org-region-active-p) (region-end) (point-max)))
3976 (day-numbers (org-get-all-dates
3977 beg end 'no-ranges
3978 t org-agenda-show-log-scoped ; always include today
3979 org-timeline-show-empty-dates))
3980 (org-deadline-warning-days 0)
3981 (org-agenda-only-exact-dates t)
3982 (today (org-today))
3983 (past t)
3984 args
3985 s e rtn d emptyp)
3986 (setq org-agenda-redo-command
3987 (list 'progn
3988 (list 'org-switch-to-buffer-other-window (current-buffer))
3989 (list 'org-timeline (list 'quote dotodo))))
3990 (if (not dopast)
3991 ;; Remove past dates from the list of dates.
3992 (setq day-numbers (delq nil (mapcar (lambda(x)
3993 (if (>= x today) x nil))
3994 day-numbers))))
3995 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
3996 (org-compile-prefix-format 'timeline)
3997 (org-set-sorting-strategy 'timeline)
3998 (if org-agenda-show-log-scoped (push :closed args))
3999 (push :timestamp args)
4000 (push :deadline args)
4001 (push :scheduled args)
4002 (push :sexp args)
4003 (if dotodo (push :todo args))
4004 (insert "Timeline of file " entry "\n")
4005 (add-text-properties (point-min) (point)
4006 (list 'face 'org-agenda-structure))
4007 (org-agenda-mark-header-line (point-min))
4008 (while (setq d (pop day-numbers))
4009 (if (and (listp d) (eq (car d) :omitted))
4010 (progn
4011 (setq s (point))
4012 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4013 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4014 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4015 (if (and (>= d today)
4016 dopast
4017 past)
4018 (progn
4019 (setq past nil)
4020 (insert (make-string 79 ?-) "\n")))
4021 (setq date (calendar-gregorian-from-absolute d))
4022 (setq s (point))
4023 (setq rtn (and (not emptyp)
4024 (apply 'org-agenda-get-day-entries entry
4025 date args)))
4026 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4027 (progn
4028 (insert
4029 (if (stringp org-agenda-format-date)
4030 (format-time-string org-agenda-format-date
4031 (org-time-from-absolute date))
4032 (funcall org-agenda-format-date date))
4033 "\n")
4034 (put-text-property s (1- (point)) 'face
4035 (org-agenda-get-day-face date))
4036 (put-text-property s (1- (point)) 'org-date-line t)
4037 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4038 (if (equal d today)
4039 (put-text-property s (1- (point)) 'org-today t))
4040 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4041 (put-text-property s (1- (point)) 'day d)))))
4042 (goto-char (point-min))
4043 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4044 (point-min)))
4045 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
4046 (org-agenda-finalize)
4047 (setq buffer-read-only t)))
4049 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4050 "Return a list of all relevant day numbers from BEG to END buffer positions.
4051 If NO-RANGES is non-nil, include only the start and end dates of a range,
4052 not every single day in the range. If FORCE-TODAY is non-nil, make
4053 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4054 inactive time stamps (those in square brackets) are included.
4055 When EMPTY is non-nil, also include days without any entries."
4056 (let ((re (concat
4057 (if pre-re pre-re "")
4058 (if inactive org-ts-regexp-both org-ts-regexp)))
4059 dates dates1 date day day1 day2 ts1 ts2 pos)
4060 (if force-today
4061 (setq dates (list (org-today))))
4062 (save-excursion
4063 (goto-char beg)
4064 (while (re-search-forward re end t)
4065 (setq day (time-to-days (org-time-string-to-time
4066 (substring (match-string 1) 0 10)
4067 (current-buffer) (match-beginning 0))))
4068 (or (memq day dates) (push day dates)))
4069 (unless no-ranges
4070 (goto-char beg)
4071 (while (re-search-forward org-tr-regexp end t)
4072 (setq pos (match-beginning 0))
4073 (setq ts1 (substring (match-string 1) 0 10)
4074 ts2 (substring (match-string 2) 0 10)
4075 day1 (time-to-days (org-time-string-to-time
4076 ts1 (current-buffer) pos))
4077 day2 (time-to-days (org-time-string-to-time
4078 ts2 (current-buffer) pos)))
4079 (while (< (setq day1 (1+ day1)) day2)
4080 (or (memq day1 dates) (push day1 dates)))))
4081 (setq dates (sort dates '<))
4082 (when empty
4083 (while (setq day (pop dates))
4084 (setq day2 (car dates))
4085 (push day dates1)
4086 (when (and day2 empty)
4087 (if (or (eq empty t)
4088 (and (numberp empty) (<= (- day2 day) empty)))
4089 (while (< (setq day (1+ day)) day2)
4090 (push (list day) dates1))
4091 (push (cons :omitted (- day2 day)) dates1))))
4092 (setq dates (nreverse dates1)))
4093 dates)))
4095 ;;; Agenda Daily/Weekly
4097 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4098 "Start day for the agenda view.
4099 Custom commands can set this variable in the options section.
4100 See the docstring of `org-read-date' on allowed values.")
4101 (defvar org-starting-day nil) ; local variable in the agenda buffer
4102 (defvar org-arg-loc nil) ; local variable
4104 (defvar org-agenda-buffer-tmp-name nil)
4105 ;;;###autoload
4106 (defun org-agenda-list (&optional arg start-day span with-hour)
4107 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4108 The view will be for the current day or week, but from the overview buffer
4109 you will be able to go to other days/weeks.
4111 With a numeric prefix argument in an interactive call, the agenda will
4112 span ARG days. Lisp programs should instead specify SPAN to change
4113 the number of days. SPAN defaults to `org-agenda-span'.
4115 START-DAY defaults to TODAY, or to the most recent match for the weekday
4116 given in `org-agenda-start-on-weekday'.
4118 When WITH-HOUR is non-nil, only include scheduled and deadline
4119 items if they have an hour specification like [h]h:mm."
4120 (interactive "P")
4121 (if org-agenda-overriding-arguments
4122 (setq arg (car org-agenda-overriding-arguments)
4123 start-day (nth 1 org-agenda-overriding-arguments)
4124 span (nth 2 org-agenda-overriding-arguments)))
4125 (if (and (integerp arg) (> arg 0))
4126 (setq span arg arg nil))
4127 (catch 'exit
4128 (setq org-agenda-buffer-name
4129 (or org-agenda-buffer-tmp-name
4130 (if org-agenda-sticky
4131 (cond ((and org-keys (stringp org-match))
4132 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4133 (org-keys
4134 (format "*Org Agenda(%s)*" org-keys))
4135 (t "*Org Agenda(a)*")))
4136 org-agenda-buffer-name))
4137 (org-agenda-prepare "Day/Week")
4138 (setq start-day (or start-day org-agenda-start-day))
4139 (if (stringp start-day)
4140 ;; Convert to an absolute day number
4141 (setq start-day (time-to-days (org-read-date nil t start-day))))
4142 (org-compile-prefix-format 'agenda)
4143 (org-set-sorting-strategy 'agenda)
4144 (let* ((span (org-agenda-ndays-to-span
4145 (or span org-agenda-ndays org-agenda-span)))
4146 (today (org-today))
4147 (sd (or start-day today))
4148 (ndays (org-agenda-span-to-ndays span sd))
4149 (org-agenda-start-on-weekday
4150 (if (eq ndays 7)
4151 org-agenda-start-on-weekday))
4152 (thefiles (org-agenda-files nil 'ifmode))
4153 (files thefiles)
4154 (start (if (or (null org-agenda-start-on-weekday)
4155 (< ndays 7))
4157 (let* ((nt (calendar-day-of-week
4158 (calendar-gregorian-from-absolute sd)))
4159 (n1 org-agenda-start-on-weekday)
4160 (d (- nt n1)))
4161 (- sd (+ (if (< d 0) 7 0) d)))))
4162 (day-numbers (list start))
4163 (day-cnt 0)
4164 (inhibit-redisplay (not debug-on-error))
4165 (org-agenda-show-log-scoped org-agenda-show-log)
4166 s e rtn rtnall file date d start-pos end-pos todayp
4167 clocktable-start clocktable-end filter)
4168 (setq org-agenda-redo-command
4169 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4170 (dotimes (n (1- ndays))
4171 (push (1+ (car day-numbers)) day-numbers))
4172 (setq day-numbers (nreverse day-numbers))
4173 (setq clocktable-start (car day-numbers)
4174 clocktable-end (1+ (or (org-last day-numbers) 0)))
4175 (org-set-local 'org-starting-day (car day-numbers))
4176 (org-set-local 'org-arg-loc arg)
4177 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4178 (unless org-agenda-compact-blocks
4179 (let* ((d1 (car day-numbers))
4180 (d2 (org-last day-numbers))
4181 (w1 (org-days-to-iso-week d1))
4182 (w2 (org-days-to-iso-week d2)))
4183 (setq s (point))
4184 (if org-agenda-overriding-header
4185 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4186 nil 'face 'org-agenda-structure) "\n")
4187 (insert (org-agenda-span-name span)
4188 "-agenda"
4189 (if (< (- d2 d1) 350)
4190 (if (= w1 w2)
4191 (format " (W%02d)" w1)
4192 (format " (W%02d-W%02d)" w1 w2))
4194 ":\n")))
4195 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4196 'org-date-line t))
4197 (org-agenda-mark-header-line s))
4198 (while (setq d (pop day-numbers))
4199 (setq date (calendar-gregorian-from-absolute d)
4200 s (point))
4201 (if (or (setq todayp (= d today))
4202 (and (not start-pos) (= d sd)))
4203 (setq start-pos (point))
4204 (if (and start-pos (not end-pos))
4205 (setq end-pos (point))))
4206 (setq files thefiles
4207 rtnall nil)
4208 (while (setq file (pop files))
4209 (catch 'nextfile
4210 (org-check-agenda-file file)
4211 (let ((org-agenda-entry-types org-agenda-entry-types))
4212 ;; Starred types override non-starred equivalents
4213 (when (member :deadline* org-agenda-entry-types)
4214 (setq org-agenda-entry-types
4215 (delq :deadline org-agenda-entry-types)))
4216 (when (member :scheduled* org-agenda-entry-types)
4217 (setq org-agenda-entry-types
4218 (delq :scheduled org-agenda-entry-types)))
4219 ;; Honor with-hour
4220 (when with-hour
4221 (when (member :deadline org-agenda-entry-types)
4222 (setq org-agenda-entry-types
4223 (delq :deadline org-agenda-entry-types))
4224 (push :deadline* org-agenda-entry-types))
4225 (when (member :scheduled org-agenda-entry-types)
4226 (setq org-agenda-entry-types
4227 (delq :scheduled org-agenda-entry-types))
4228 (push :scheduled* org-agenda-entry-types)))
4229 (unless org-agenda-include-deadlines
4230 (setq org-agenda-entry-types
4231 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4232 (cond
4233 ((memq org-agenda-show-log-scoped '(only clockcheck))
4234 (setq rtn (org-agenda-get-day-entries
4235 file date :closed)))
4236 (org-agenda-show-log-scoped
4237 (setq rtn (apply 'org-agenda-get-day-entries
4238 file date
4239 (append '(:closed) org-agenda-entry-types))))
4241 (setq rtn (apply 'org-agenda-get-day-entries
4242 file date
4243 org-agenda-entry-types)))))
4244 (setq rtnall (append rtnall rtn)))) ;; all entries
4245 (if org-agenda-include-diary
4246 (let ((org-agenda-search-headline-for-time t))
4247 (require 'diary-lib)
4248 (setq rtn (org-get-entries-from-diary date))
4249 (setq rtnall (append rtnall rtn))))
4250 (if (or rtnall org-agenda-show-all-dates)
4251 (progn
4252 (setq day-cnt (1+ day-cnt))
4253 (insert
4254 (if (stringp org-agenda-format-date)
4255 (format-time-string org-agenda-format-date
4256 (org-time-from-absolute date))
4257 (funcall org-agenda-format-date date))
4258 "\n")
4259 (put-text-property s (1- (point)) 'face
4260 (org-agenda-get-day-face date))
4261 (put-text-property s (1- (point)) 'org-date-line t)
4262 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4263 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4264 (when todayp
4265 (put-text-property s (1- (point)) 'org-today t))
4266 (setq rtnall
4267 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4268 (if rtnall (insert ;; all entries
4269 (org-agenda-finalize-entries rtnall 'agenda)
4270 "\n"))
4271 (put-text-property s (1- (point)) 'day d)
4272 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4273 (when (and org-agenda-clockreport-mode clocktable-start)
4274 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4275 ;; the above line is to ensure the restricted range!
4276 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4277 tbl)
4278 (setq p (org-plist-delete p :block))
4279 (setq p (plist-put p :tstart clocktable-start))
4280 (setq p (plist-put p :tend clocktable-end))
4281 (setq p (plist-put p :scope 'agenda))
4282 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4283 (setq filter (or org-agenda-tag-filter-while-redo
4284 (get 'org-agenda-tag-filter :preset-filter))))
4285 (setq p (plist-put p :tags (mapconcat (lambda (x)
4286 (if (string-match "[<>=]" x)
4289 filter ""))))
4290 (setq tbl (apply 'org-clock-get-clocktable p))
4291 (insert tbl)))
4292 (goto-char (point-min))
4293 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4294 (unless (and (pos-visible-in-window-p (point-min))
4295 (pos-visible-in-window-p (point-max)))
4296 (goto-char (1- (point-max)))
4297 (recenter -1)
4298 (if (not (pos-visible-in-window-p (or start-pos 1)))
4299 (progn
4300 (goto-char (or start-pos 1))
4301 (recenter 1))))
4302 (goto-char (or start-pos 1))
4303 (add-text-properties (point-min) (point-max)
4304 `(org-agenda-type agenda
4305 org-last-args (,arg ,start-day ,span)
4306 org-redo-cmd ,org-agenda-redo-command
4307 org-series-cmd ,org-cmd))
4308 (if (eq org-agenda-show-log-scoped 'clockcheck)
4309 (org-agenda-show-clocking-issues))
4310 (org-agenda-finalize)
4311 (setq buffer-read-only t)
4312 (message ""))))
4314 (defun org-agenda-ndays-to-span (n)
4315 "Return a span symbol for a span of N days, or N if none matches."
4316 (cond ((symbolp n) n)
4317 ((= n 1) 'day)
4318 ((= n 7) 'week)
4319 (t n)))
4321 (defun org-agenda-span-to-ndays (span &optional start-day)
4322 "Return ndays from SPAN, possibly starting at START-DAY."
4323 (cond ((numberp span) span)
4324 ((eq span 'day) 1)
4325 ((eq span 'week) 7)
4326 ((eq span 'month)
4327 (let ((date (calendar-gregorian-from-absolute start-day)))
4328 (calendar-last-day-of-month (car date) (caddr date))))
4329 ((eq span 'year)
4330 (let ((date (calendar-gregorian-from-absolute start-day)))
4331 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4333 (defun org-agenda-span-name (span)
4334 "Return a SPAN name."
4335 (if (null span)
4337 (if (symbolp span)
4338 (capitalize (symbol-name span))
4339 (format "%d days" span))))
4341 ;;; Agenda word search
4343 (defvar org-agenda-search-history nil)
4345 (defvar org-search-syntax-table nil
4346 "Special syntax table for org-mode search.
4347 In this table, we have single quotes not as word constituents, to
4348 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4350 (defvar org-mode-syntax-table) ; From org.el
4351 (defun org-search-syntax-table ()
4352 (unless org-search-syntax-table
4353 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4354 (modify-syntax-entry ?' "." org-search-syntax-table)
4355 (modify-syntax-entry ?` "." org-search-syntax-table))
4356 org-search-syntax-table)
4358 (defvar org-agenda-last-search-view-search-was-boolean nil)
4360 ;;;###autoload
4361 (defun org-search-view (&optional todo-only string edit-at)
4362 "Show all entries that contain a phrase or words or regular expressions.
4364 With optional prefix argument TODO-ONLY, only consider entries that are
4365 TODO entries. The argument STRING can be used to pass a default search
4366 string into this function. If EDIT-AT is non-nil, it means that the
4367 user should get a chance to edit this string, with cursor at position
4368 EDIT-AT.
4370 The search string can be viewed either as a phrase that should be found as
4371 is, or it can be broken into a number of snippets, each of which must match
4372 in a Boolean way to select an entry. The default depends on the variable
4373 `org-agenda-search-view-always-boolean'.
4374 Even if this is turned off (the default) you can always switch to
4375 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4377 The default is a direct search of the whole phrase, where each space in
4378 the search string can expand to an arbitrary amount of whitespace,
4379 including newlines.
4381 If using a Boolean search, the search string is split on whitespace and
4382 each snippet is searched separately, with logical AND to select an entry.
4383 Words prefixed with a minus must *not* occur in the entry. Words without
4384 a prefix or prefixed with a plus must occur in the entry. Matching is
4385 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4386 match whole words, not parts of a word) if
4387 `org-agenda-search-view-force-full-words' is set (default is nil).
4389 Boolean search snippets enclosed by curly braces are interpreted as
4390 regular expressions that must or (when preceded with \"-\") must not
4391 match in the entry. Snippets enclosed into double quotes will be taken
4392 as a whole, to include whitespace.
4394 - If the search string starts with an asterisk, search only in headlines.
4395 - If (possibly after the leading star) the search string starts with an
4396 exclamation mark, this also means to look at TODO entries only, an effect
4397 that can also be achieved with a prefix argument.
4398 - If (possibly after star and exclamation mark) the search string starts
4399 with a colon, this will mean that the (non-regexp) snippets of the
4400 Boolean search must match as full words.
4402 This command searches the agenda files, and in addition the files listed
4403 in `org-agenda-text-search-extra-files'."
4404 (interactive "P")
4405 (if org-agenda-overriding-arguments
4406 (setq todo-only (car org-agenda-overriding-arguments)
4407 string (nth 1 org-agenda-overriding-arguments)
4408 edit-at (nth 2 org-agenda-overriding-arguments)))
4409 (let* ((props (list 'face nil
4410 'done-face 'org-agenda-done
4411 'org-not-done-regexp org-not-done-regexp
4412 'org-todo-regexp org-todo-regexp
4413 'org-complex-heading-regexp org-complex-heading-regexp
4414 'mouse-face 'highlight
4415 'help-echo (format "mouse-2 or RET jump to location")))
4416 (full-words org-agenda-search-view-force-full-words)
4417 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4418 regexp rtn rtnall files file pos inherited-tags
4419 marker category category-pos level tags c neg re boolean
4420 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4421 (unless (and (not edit-at)
4422 (stringp string)
4423 (string-match "\\S-" string))
4424 (setq string (read-string
4425 (if org-agenda-search-view-always-boolean
4426 "[+-]Word/{Regexp} ...: "
4427 "Phrase or [+-]Word/{Regexp} ...: ")
4428 (cond
4429 ((integerp edit-at) (cons string edit-at))
4430 (edit-at string))
4431 'org-agenda-search-history)))
4432 (catch 'exit
4433 (if org-agenda-sticky
4434 (setq org-agenda-buffer-name
4435 (if (stringp string)
4436 (format "*Org Agenda(%s:%s)*"
4437 (or org-keys (or (and todo-only "S") "s")) string)
4438 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4439 (org-agenda-prepare "SEARCH")
4440 (org-compile-prefix-format 'search)
4441 (org-set-sorting-strategy 'search)
4442 (setq org-agenda-redo-command
4443 (list 'org-search-view (if todo-only t nil)
4444 (list 'if 'current-prefix-arg nil string)))
4445 (setq org-agenda-query-string string)
4446 (if (equal (string-to-char string) ?*)
4447 (setq hdl-only t
4448 words (substring string 1))
4449 (setq words string))
4450 (when (equal (string-to-char words) ?!)
4451 (setq todo-only t
4452 words (substring words 1)))
4453 (when (equal (string-to-char words) ?:)
4454 (setq full-words t
4455 words (substring words 1)))
4456 (if (or org-agenda-search-view-always-boolean
4457 (member (string-to-char words) '(?- ?+ ?\{)))
4458 (setq boolean t))
4459 (setq words (org-split-string words))
4460 (let (www w)
4461 (while (setq w (pop words))
4462 (while (and (string-match "\\\\\\'" w) words)
4463 (setq w (concat (substring w 0 -1) " " (pop words))))
4464 (push w www))
4465 (setq words (nreverse www) www nil)
4466 (while (setq w (pop words))
4467 (when (and (string-match "\\`[-+]?{" w)
4468 (not (string-match "}\\'" w)))
4469 (while (and words (not (string-match "}\\'" (car words))))
4470 (setq w (concat w " " (pop words))))
4471 (setq w (concat w " " (pop words))))
4472 (push w www))
4473 (setq words (nreverse www)))
4474 (setq org-agenda-last-search-view-search-was-boolean boolean)
4475 (when boolean
4476 (let (wds w)
4477 (while (setq w (pop words))
4478 (if (or (equal (substring w 0 1) "\"")
4479 (and (> (length w) 1)
4480 (member (substring w 0 1) '("+" "-"))
4481 (equal (substring w 1 2) "\"")))
4482 (while (and words (not (equal (substring w -1) "\"")))
4483 (setq w (concat w " " (pop words)))))
4484 (and (string-match "\\`\\([-+]?\\)\"" w)
4485 (setq w (replace-match "\\1" nil nil w)))
4486 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4487 (push w wds))
4488 (setq words (nreverse wds))))
4489 (if boolean
4490 (mapc (lambda (w)
4491 (setq c (string-to-char w))
4492 (if (equal c ?-)
4493 (setq neg t w (substring w 1))
4494 (if (equal c ?+)
4495 (setq neg nil w (substring w 1))
4496 (setq neg nil)))
4497 (if (string-match "\\`{.*}\\'" w)
4498 (setq re (substring w 1 -1))
4499 (if full-words
4500 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4501 (setq re (regexp-quote (downcase w)))))
4502 (if neg (push re regexps-) (push re regexps+)))
4503 words)
4504 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4505 regexps+))
4506 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4507 (if (not regexps+)
4508 (setq regexp org-outline-regexp-bol)
4509 (setq regexp (pop regexps+))
4510 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4511 regexp))))
4512 (setq files (org-agenda-files nil 'ifmode))
4513 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4514 (pop org-agenda-text-search-extra-files)
4515 (setq files (org-add-archive-files files)))
4516 (setq files (append files org-agenda-text-search-extra-files)
4517 rtnall nil)
4518 (while (setq file (pop files))
4519 (setq ee nil)
4520 (catch 'nextfile
4521 (org-check-agenda-file file)
4522 (setq buffer (if (file-exists-p file)
4523 (org-get-agenda-file-buffer file)
4524 (error "No such file %s" file)))
4525 (if (not buffer)
4526 ;; If file does not exist, make sure an error message is sent
4527 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4528 file))))
4529 (with-current-buffer buffer
4530 (with-syntax-table (org-search-syntax-table)
4531 (unless (derived-mode-p 'org-mode)
4532 (error "Agenda file %s is not in `org-mode'" file))
4533 (let ((case-fold-search t))
4534 (save-excursion
4535 (save-restriction
4536 (if org-agenda-restrict
4537 (narrow-to-region org-agenda-restrict-begin
4538 org-agenda-restrict-end)
4539 (widen))
4540 (goto-char (point-min))
4541 (unless (or (org-at-heading-p)
4542 (outline-next-heading))
4543 (throw 'nextfile t))
4544 (goto-char (max (point-min) (1- (point))))
4545 (while (re-search-forward regexp nil t)
4546 (org-back-to-heading t)
4547 (while (and org-agenda-search-view-max-outline-level
4548 (> (org-reduced-level (org-outline-level))
4549 org-agenda-search-view-max-outline-level)
4550 (forward-line -1)
4551 (outline-back-to-heading t)))
4552 (skip-chars-forward "* ")
4553 (setq beg (point-at-bol)
4554 beg1 (point)
4555 end (progn
4556 (outline-next-heading)
4557 (while (and org-agenda-search-view-max-outline-level
4558 (> (org-reduced-level (org-outline-level))
4559 org-agenda-search-view-max-outline-level)
4560 (forward-line 1)
4561 (outline-next-heading)))
4562 (point)))
4564 (catch :skip
4565 (goto-char beg)
4566 (org-agenda-skip)
4567 (setq str (buffer-substring-no-properties
4568 (point-at-bol)
4569 (if hdl-only (point-at-eol) end)))
4570 (mapc (lambda (wr) (when (string-match wr str)
4571 (goto-char (1- end))
4572 (throw :skip t)))
4573 regexps-)
4574 (mapc (lambda (wr) (unless (string-match wr str)
4575 (goto-char (1- end))
4576 (throw :skip t)))
4577 (if todo-only
4578 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4579 regexps+)
4580 regexps+))
4581 (goto-char beg)
4582 (setq marker (org-agenda-new-marker (point))
4583 category (org-get-category)
4584 level (make-string (org-reduced-level (org-outline-level)) ? )
4585 category-pos (get-text-property (point) 'org-category-position)
4586 inherited-tags
4587 (or (eq org-agenda-show-inherited-tags 'always)
4588 (and (listp org-agenda-show-inherited-tags)
4589 (memq 'todo org-agenda-show-inherited-tags))
4590 (and (eq org-agenda-show-inherited-tags t)
4591 (or (eq org-agenda-use-tag-inheritance t)
4592 (memq 'todo org-agenda-use-tag-inheritance))))
4593 tags (org-get-tags-at nil (not inherited-tags))
4594 txt (org-agenda-format-item
4596 (buffer-substring-no-properties
4597 beg1 (point-at-eol))
4598 level category tags t))
4599 (org-add-props txt props
4600 'org-marker marker 'org-hd-marker marker
4601 'org-todo-regexp org-todo-regexp
4602 'level level
4603 'org-complex-heading-regexp org-complex-heading-regexp
4604 'priority 1000 'org-category category
4605 'org-category-position category-pos
4606 'type "search")
4607 (push txt ee)
4608 (goto-char (1- end))))))))))
4609 (setq rtn (nreverse ee))
4610 (setq rtnall (append rtnall rtn)))
4611 (if org-agenda-overriding-header
4612 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4613 nil 'face 'org-agenda-structure) "\n")
4614 (insert "Search words: ")
4615 (add-text-properties (point-min) (1- (point))
4616 (list 'face 'org-agenda-structure))
4617 (setq pos (point))
4618 (insert string "\n")
4619 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4620 (setq pos (point))
4621 (unless org-agenda-multi
4622 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4623 (add-text-properties pos (1- (point))
4624 (list 'face 'org-agenda-structure))))
4625 (org-agenda-mark-header-line (point-min))
4626 (when rtnall
4627 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4628 (goto-char (point-min))
4629 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4630 (add-text-properties (point-min) (point-max)
4631 `(org-agenda-type search
4632 org-last-args (,todo-only ,string ,edit-at)
4633 org-redo-cmd ,org-agenda-redo-command
4634 org-series-cmd ,org-cmd))
4635 (org-agenda-finalize)
4636 (setq buffer-read-only t))))
4638 ;;; Agenda TODO list
4640 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4641 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4642 (concat
4643 (if (or (equal keywords "ALL") (not keywords))
4644 (propertize "ALL" 'face 'warning)
4645 (mapconcat
4646 (lambda (kw)
4647 (propertize kw 'face (org-get-todo-face kw)))
4648 (org-split-string keywords "|")
4649 "|"))
4650 "\n"))
4652 (defvar org-select-this-todo-keyword nil)
4653 (defvar org-last-arg nil)
4655 ;;;###autoload
4656 (defun org-todo-list (&optional arg)
4657 "Show all (not done) TODO entries from all agenda file in a single list.
4658 The prefix arg can be used to select a specific TODO keyword and limit
4659 the list to these. When using \\[universal-argument], you will be prompted
4660 for a keyword. A numeric prefix directly selects the Nth keyword in
4661 `org-todo-keywords-1'."
4662 (interactive "P")
4663 (if org-agenda-overriding-arguments
4664 (setq arg org-agenda-overriding-arguments))
4665 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4666 (let* ((today (org-today))
4667 (date (calendar-gregorian-from-absolute today))
4668 (kwds org-todo-keywords-for-agenda)
4669 (completion-ignore-case t)
4670 (org-select-this-todo-keyword
4671 (if (stringp arg) arg
4672 (and arg (integerp arg) (> arg 0)
4673 (nth (1- arg) kwds))))
4674 rtn rtnall files file pos)
4675 (when (equal arg '(4))
4676 (setq org-select-this-todo-keyword
4677 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4678 (mapcar 'list kwds) nil nil)))
4679 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4680 (catch 'exit
4681 (if org-agenda-sticky
4682 (setq org-agenda-buffer-name
4683 (if (stringp org-select-this-todo-keyword)
4684 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4685 org-select-this-todo-keyword)
4686 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4687 (org-agenda-prepare "TODO")
4688 (org-compile-prefix-format 'todo)
4689 (org-set-sorting-strategy 'todo)
4690 (setq org-agenda-redo-command
4691 `(org-todo-list (or (and (numberp current-prefix-arg)
4692 current-prefix-arg)
4693 ,org-select-this-todo-keyword
4694 current-prefix-arg ,arg)))
4695 (setq files (org-agenda-files nil 'ifmode)
4696 rtnall nil)
4697 (while (setq file (pop files))
4698 (catch 'nextfile
4699 (org-check-agenda-file file)
4700 (setq rtn (org-agenda-get-day-entries file date :todo))
4701 (setq rtnall (append rtnall rtn))))
4702 (if org-agenda-overriding-header
4703 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4704 nil 'face 'org-agenda-structure) "\n")
4705 (insert "Global list of TODO items of type: ")
4706 (add-text-properties (point-min) (1- (point))
4707 (list 'face 'org-agenda-structure
4708 'short-heading
4709 (concat "ToDo: "
4710 (or org-select-this-todo-keyword "ALL"))))
4711 (org-agenda-mark-header-line (point-min))
4712 (insert (org-agenda-propertize-selected-todo-keywords
4713 org-select-this-todo-keyword))
4714 (setq pos (point))
4715 (unless org-agenda-multi
4716 (insert "Available with `N r': (0)[ALL]")
4717 (let ((n 0) s)
4718 (mapc (lambda (x)
4719 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4720 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4721 (insert "\n "))
4722 (insert " " s))
4723 kwds))
4724 (insert "\n"))
4725 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4726 (org-agenda-mark-header-line (point-min))
4727 (when rtnall
4728 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4729 (goto-char (point-min))
4730 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4731 (add-text-properties (point-min) (point-max)
4732 `(org-agenda-type todo
4733 org-last-args ,arg
4734 org-redo-cmd ,org-agenda-redo-command
4735 org-series-cmd ,org-cmd))
4736 (org-agenda-finalize)
4737 (setq buffer-read-only t))))
4739 ;;; Agenda tags match
4741 ;;;###autoload
4742 (defun org-tags-view (&optional todo-only match)
4743 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4744 The prefix arg TODO-ONLY limits the search to TODO entries."
4745 (interactive "P")
4746 (if org-agenda-overriding-arguments
4747 (setq todo-only (car org-agenda-overriding-arguments)
4748 match (nth 1 org-agenda-overriding-arguments)))
4749 (let* ((org-tags-match-list-sublevels
4750 org-tags-match-list-sublevels)
4751 (completion-ignore-case t)
4752 rtn rtnall files file pos matcher
4753 buffer)
4754 (when (and (stringp match) (not (string-match "\\S-" match)))
4755 (setq match nil))
4756 (catch 'exit
4757 (if org-agenda-sticky
4758 (setq org-agenda-buffer-name
4759 (if (stringp match)
4760 (format "*Org Agenda(%s:%s)*"
4761 (or org-keys (or (and todo-only "M") "m")) match)
4762 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4763 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4764 ;; expanding tags within `org-make-tags-matcher'
4765 (org-agenda-prepare (concat "TAGS " match))
4766 (setq matcher (org-make-tags-matcher match)
4767 match (car matcher) matcher (cdr matcher))
4768 (org-compile-prefix-format 'tags)
4769 (org-set-sorting-strategy 'tags)
4770 (setq org-agenda-query-string match)
4771 (setq org-agenda-redo-command
4772 (list 'org-tags-view `(quote ,todo-only)
4773 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4774 (setq files (org-agenda-files nil 'ifmode)
4775 rtnall nil)
4776 (while (setq file (pop files))
4777 (catch 'nextfile
4778 (org-check-agenda-file file)
4779 (setq buffer (if (file-exists-p file)
4780 (org-get-agenda-file-buffer file)
4781 (error "No such file %s" file)))
4782 (if (not buffer)
4783 ;; If file does not exist, error message to agenda
4784 (setq rtn (list
4785 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4786 rtnall (append rtnall rtn))
4787 (with-current-buffer buffer
4788 (unless (derived-mode-p 'org-mode)
4789 (error "Agenda file %s is not in `org-mode'" file))
4790 (save-excursion
4791 (save-restriction
4792 (if org-agenda-restrict
4793 (narrow-to-region org-agenda-restrict-begin
4794 org-agenda-restrict-end)
4795 (widen))
4796 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4797 (setq rtnall (append rtnall rtn))))))))
4798 (if org-agenda-overriding-header
4799 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4800 nil 'face 'org-agenda-structure) "\n")
4801 (insert "Headlines with TAGS match: ")
4802 (add-text-properties (point-min) (1- (point))
4803 (list 'face 'org-agenda-structure
4804 'short-heading
4805 (concat "Match: " match)))
4806 (setq pos (point))
4807 (insert match "\n")
4808 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4809 (setq pos (point))
4810 (unless org-agenda-multi
4811 (insert "Press `C-u r' to search again with new search string\n"))
4812 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4813 (org-agenda-mark-header-line (point-min))
4814 (when rtnall
4815 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4816 (goto-char (point-min))
4817 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4818 (add-text-properties (point-min) (point-max)
4819 `(org-agenda-type tags
4820 org-last-args (,todo-only ,match)
4821 org-redo-cmd ,org-agenda-redo-command
4822 org-series-cmd ,org-cmd))
4823 (org-agenda-finalize)
4824 (setq buffer-read-only t))))
4826 ;;; Agenda Finding stuck projects
4828 (defvar org-agenda-skip-regexp nil
4829 "Regular expression used in skipping subtrees for the agenda.
4830 This is basically a temporary global variable that can be set and then
4831 used by user-defined selections using `org-agenda-skip-function'.")
4833 (defvar org-agenda-overriding-header nil
4834 "When set during agenda, todo and tags searches it replaces the header.
4835 This variable should not be set directly, but custom commands can bind it
4836 in the options section.")
4838 (defun org-agenda-skip-entry-when-regexp-matches ()
4839 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4840 If yes, it returns the end position of this entry, causing agenda commands
4841 to skip the entry but continuing the search in the subtree. This is a
4842 function that can be put into `org-agenda-skip-function' for the duration
4843 of a command."
4844 (let ((end (save-excursion (org-end-of-subtree t)))
4845 skip)
4846 (save-excursion
4847 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4848 (and skip end)))
4850 (defun org-agenda-skip-subtree-when-regexp-matches ()
4851 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4852 If yes, it returns the end position of this tree, causing agenda commands
4853 to skip this subtree. This is a function that can be put into
4854 `org-agenda-skip-function' for the duration of a command."
4855 (let ((end (save-excursion (org-end-of-subtree t)))
4856 skip)
4857 (save-excursion
4858 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4859 (and skip end)))
4861 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4862 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4863 If yes, it returns the end position of the current entry (NOT the tree),
4864 causing agenda commands to skip the entry but continuing the search in
4865 the subtree. This is a function that can be put into
4866 `org-agenda-skip-function' for the duration of a command. An important
4867 use of this function is for the stuck project list."
4868 (let ((end (save-excursion (org-end-of-subtree t)))
4869 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4870 skip)
4871 (save-excursion
4872 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4873 (and skip entry-end)))
4875 (defun org-agenda-skip-entry-if (&rest conditions)
4876 "Skip entry if any of CONDITIONS is true.
4877 See `org-agenda-skip-if' for details."
4878 (org-agenda-skip-if nil conditions))
4880 (defun org-agenda-skip-subtree-if (&rest conditions)
4881 "Skip entry if any of CONDITIONS is true.
4882 See `org-agenda-skip-if' for details."
4883 (org-agenda-skip-if t conditions))
4885 (defun org-agenda-skip-if (subtree conditions)
4886 "Checks current entity for CONDITIONS.
4887 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4888 the entry (i.e. the text before the next heading) is checked.
4890 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4891 from different tests. Valid conditions are:
4893 scheduled Check if there is a scheduled cookie
4894 notscheduled Check if there is no scheduled cookie
4895 deadline Check if there is a deadline
4896 notdeadline Check if there is no deadline
4897 timestamp Check if there is a timestamp (also deadline or scheduled)
4898 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4899 regexp Check if regexp matches
4900 notregexp Check if regexp does not match.
4901 todo Check if TODO keyword matches
4902 nottodo Check if TODO keyword does not match
4904 The regexp is taken from the conditions list, it must come right after
4905 the `regexp' or `notregexp' element.
4907 `todo' and `nottodo' accept as an argument a list of todo
4908 keywords, which may include \"*\" to match any todo keyword.
4910 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4912 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4914 Instead of a list, a keyword class may be given. For example:
4916 (org-agenda-skip-entry-if 'nottodo 'done)
4918 would skip entries that haven't been marked with any of \"DONE\"
4919 keywords. Possible classes are: `todo', `done', `any'.
4921 If any of these conditions is met, this function returns the end point of
4922 the entity, causing the search to continue from there. This is a function
4923 that can be put into `org-agenda-skip-function' for the duration of a command."
4924 (let (beg end m)
4925 (org-back-to-heading t)
4926 (setq beg (point)
4927 end (if subtree
4928 (progn (org-end-of-subtree t) (point))
4929 (progn (outline-next-heading) (1- (point)))))
4930 (goto-char beg)
4931 (and
4933 (and (memq 'scheduled conditions)
4934 (re-search-forward org-scheduled-time-regexp end t))
4935 (and (memq 'notscheduled conditions)
4936 (not (re-search-forward org-scheduled-time-regexp end t)))
4937 (and (memq 'deadline conditions)
4938 (re-search-forward org-deadline-time-regexp end t))
4939 (and (memq 'notdeadline conditions)
4940 (not (re-search-forward org-deadline-time-regexp end t)))
4941 (and (memq 'timestamp conditions)
4942 (re-search-forward org-ts-regexp end t))
4943 (and (memq 'nottimestamp conditions)
4944 (not (re-search-forward org-ts-regexp end t)))
4945 (and (setq m (memq 'regexp conditions))
4946 (stringp (nth 1 m))
4947 (re-search-forward (nth 1 m) end t))
4948 (and (setq m (memq 'notregexp conditions))
4949 (stringp (nth 1 m))
4950 (not (re-search-forward (nth 1 m) end t)))
4951 (and (or
4952 (setq m (memq 'nottodo conditions))
4953 (setq m (memq 'todo-unblocked conditions))
4954 (setq m (memq 'nottodo-unblocked conditions))
4955 (setq m (memq 'todo conditions)))
4956 (org-agenda-skip-if-todo m end)))
4957 end)))
4959 (defun org-agenda-skip-if-todo (args end)
4960 "Helper function for `org-agenda-skip-if', do not use it directly.
4961 ARGS is a list with first element either `todo', `nottodo',
4962 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4963 a list of TODO keywords, or a state symbol `todo' or `done' or
4964 `any'."
4965 (let ((kw (car args))
4966 (arg (cadr args))
4967 todo-wds todo-re)
4968 (setq todo-wds
4969 (org-uniquify
4970 (cond
4971 ((listp arg) ;; list of keywords
4972 (if (member "*" arg)
4973 (mapcar 'substring-no-properties org-todo-keywords-1)
4974 arg))
4975 ((symbolp arg) ;; keyword class name
4976 (cond
4977 ((eq arg 'todo)
4978 (org-delete-all org-done-keywords
4979 (mapcar 'substring-no-properties
4980 org-todo-keywords-1)))
4981 ((eq arg 'done) org-done-keywords)
4982 ((eq arg 'any)
4983 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4984 (setq todo-re
4985 (concat "^\\*+[ \t]+\\<\\("
4986 (mapconcat 'identity todo-wds "\\|")
4987 "\\)\\>"))
4988 (cond
4989 ((eq kw 'todo) (re-search-forward todo-re end t))
4990 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4991 ((eq kw 'todo-unblocked)
4992 (catch 'unblocked
4993 (while (re-search-forward todo-re end t)
4994 (or (org-entry-blocked-p) (throw 'unblocked t)))
4995 nil))
4996 ((eq kw 'nottodo-unblocked)
4997 (catch 'unblocked
4998 (while (re-search-forward todo-re end t)
4999 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5003 ;;;###autoload
5004 (defun org-agenda-list-stuck-projects (&rest ignore)
5005 "Create agenda view for projects that are stuck.
5006 Stuck projects are project that have no next actions. For the definitions
5007 of what a project is and how to check if it stuck, customize the variable
5008 `org-stuck-projects'."
5009 (interactive)
5010 (let* ((org-agenda-skip-function
5011 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5012 ;; We could have used org-agenda-skip-if here.
5013 (org-agenda-overriding-header
5014 (or org-agenda-overriding-header "List of stuck projects: "))
5015 (matcher (nth 0 org-stuck-projects))
5016 (todo (nth 1 org-stuck-projects))
5017 (todo-wds (if (member "*" todo)
5018 (progn
5019 (org-agenda-prepare-buffers (org-agenda-files
5020 nil 'ifmode))
5021 (org-delete-all
5022 org-done-keywords-for-agenda
5023 (copy-sequence org-todo-keywords-for-agenda)))
5024 todo))
5025 (todo-re (concat "^\\*+[ \t]+\\("
5026 (mapconcat 'identity todo-wds "\\|")
5027 "\\)\\>"))
5028 (tags (nth 2 org-stuck-projects))
5029 (tags-re (if (member "*" tags)
5030 (concat org-outline-regexp-bol
5031 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5032 (if tags
5033 (concat org-outline-regexp-bol
5034 ".*:\\("
5035 (mapconcat 'identity tags "\\|")
5036 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5037 (gen-re (nth 3 org-stuck-projects))
5038 (re-list
5039 (delq nil
5040 (list
5041 (if todo todo-re)
5042 (if tags tags-re)
5043 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5044 gen-re)))))
5045 (setq org-agenda-skip-regexp
5046 (if re-list
5047 (mapconcat 'identity re-list "\\|")
5048 (error "No information how to identify unstuck projects")))
5049 (org-tags-view nil matcher)
5050 (with-current-buffer org-agenda-buffer-name
5051 (setq org-agenda-redo-command
5052 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5054 ;;; Diary integration
5056 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5057 (defvar diary-list-entries-hook)
5058 (defvar diary-time-regexp)
5059 (defun org-get-entries-from-diary (date)
5060 "Get the (Emacs Calendar) diary entries for DATE."
5061 (require 'diary-lib)
5062 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5063 (diary-display-hook '(fancy-diary-display))
5064 (diary-display-function 'fancy-diary-display)
5065 (pop-up-frames nil)
5066 (diary-list-entries-hook
5067 (cons 'org-diary-default-entry diary-list-entries-hook))
5068 (diary-file-name-prefix nil) ; turn this feature off
5069 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5070 entries
5071 (org-disable-agenda-to-diary t))
5072 (save-excursion
5073 (save-window-excursion
5074 (funcall (if (fboundp 'diary-list-entries)
5075 'diary-list-entries 'list-diary-entries)
5076 date 1)))
5077 (if (not (get-buffer diary-fancy-buffer))
5078 (setq entries nil)
5079 (with-current-buffer diary-fancy-buffer
5080 (setq buffer-read-only nil)
5081 (if (zerop (buffer-size))
5082 ;; No entries
5083 (setq entries nil)
5084 ;; Omit the date and other unnecessary stuff
5085 (org-agenda-cleanup-fancy-diary)
5086 ;; Add prefix to each line and extend the text properties
5087 (if (zerop (buffer-size))
5088 (setq entries nil)
5089 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5090 (setq entries
5091 (with-temp-buffer
5092 (insert entries) (goto-char (point-min))
5093 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5094 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5095 (replace-match (concat "; " (match-string 1)))))
5096 (buffer-string)))))
5097 (set-buffer-modified-p nil)
5098 (kill-buffer diary-fancy-buffer)))
5099 (when entries
5100 (setq entries (org-split-string entries "\n"))
5101 (setq entries
5102 (mapcar
5103 (lambda (x)
5104 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5105 ;; Extend the text properties to the beginning of the line
5106 (org-add-props x (text-properties-at (1- (length x)) x)
5107 'type "diary" 'date date 'face 'org-agenda-diary))
5108 entries)))))
5110 (defvar org-agenda-cleanup-fancy-diary-hook nil
5111 "Hook run when the fancy diary buffer is cleaned up.")
5113 (defun org-agenda-cleanup-fancy-diary ()
5114 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5115 This gets rid of the date, the underline under the date, and
5116 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5117 date. It also removes lines that contain only whitespace."
5118 (goto-char (point-min))
5119 (if (looking-at ".*?:[ \t]*")
5120 (progn
5121 (replace-match "")
5122 (re-search-forward "\n=+$" nil t)
5123 (replace-match "")
5124 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5125 (re-search-forward "\n=+$" nil t)
5126 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5127 (goto-char (point-min))
5128 (while (re-search-forward "^ +\n" nil t)
5129 (replace-match ""))
5130 (goto-char (point-min))
5131 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5132 (replace-match ""))
5133 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5135 ;; Make sure entries from the diary have the right text properties.
5136 (eval-after-load "diary-lib"
5137 '(if (boundp 'diary-modify-entry-list-string-function)
5138 ;; We can rely on the hook, nothing to do
5140 ;; Hook not available, must use advice to make this work
5141 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5142 "Make the position visible."
5143 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5144 (stringp string)
5145 buffer-file-name)
5146 (setq string (org-modify-diary-entry-string string))))))
5148 (defun org-modify-diary-entry-string (string)
5149 "Add text properties to string, allowing org-mode to act on it."
5150 (org-add-props string nil
5151 'mouse-face 'highlight
5152 'help-echo (if buffer-file-name
5153 (format "mouse-2 or RET jump to diary file %s"
5154 (abbreviate-file-name buffer-file-name))
5156 'org-agenda-diary-link t
5157 'org-marker (org-agenda-new-marker (point-at-bol))))
5159 (defun org-diary-default-entry ()
5160 "Add a dummy entry to the diary.
5161 Needed to avoid empty dates which mess up holiday display."
5162 ;; Catch the error if dealing with the new add-to-diary-alist
5163 (when org-disable-agenda-to-diary
5164 (condition-case nil
5165 (org-add-to-diary-list original-date "Org-mode dummy" "")
5166 (error
5167 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5169 (defun org-add-to-diary-list (&rest args)
5170 (if (fboundp 'diary-add-to-list)
5171 (apply 'diary-add-to-list args)
5172 (apply 'add-to-diary-list args)))
5174 (defvar org-diary-last-run-time nil)
5176 ;;;###autoload
5177 (defun org-diary (&rest args)
5178 "Return diary information from org files.
5179 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5180 It accesses org files and extracts information from those files to be
5181 listed in the diary. The function accepts arguments specifying what
5182 items should be listed. For a list of arguments allowed here, see the
5183 variable `org-agenda-entry-types'.
5185 The call in the diary file should look like this:
5187 &%%(org-diary) ~/path/to/some/orgfile.org
5189 Use a separate line for each org file to check. Or, if you omit the file name,
5190 all files listed in `org-agenda-files' will be checked automatically:
5192 &%%(org-diary)
5194 If you don't give any arguments (as in the example above), the default value
5195 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5196 So the example above may also be written as
5198 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5200 The function expects the lisp variables `entry' and `date' to be provided
5201 by the caller, because this is how the calendar works. Don't use this
5202 function from a program - use `org-agenda-get-day-entries' instead."
5203 (when (> (- (org-float-time)
5204 org-agenda-last-marker-time)
5206 ;; I am not sure if this works with sticky agendas, because the marker
5207 ;; list is then no longer a global variable.
5208 (org-agenda-reset-markers))
5209 (org-compile-prefix-format 'agenda)
5210 (org-set-sorting-strategy 'agenda)
5211 (setq args (or args org-agenda-entry-types))
5212 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5213 (list entry)
5214 (org-agenda-files t)))
5215 (time (org-float-time))
5216 file rtn results)
5217 (when (or (not org-diary-last-run-time)
5218 (> (- time
5219 org-diary-last-run-time)
5221 (org-agenda-prepare-buffers files))
5222 (setq org-diary-last-run-time time)
5223 ;; If this is called during org-agenda, don't return any entries to
5224 ;; the calendar. Org Agenda will list these entries itself.
5225 (if org-disable-agenda-to-diary (setq files nil))
5226 (while (setq file (pop files))
5227 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5228 (setq results (append results rtn)))
5229 (when results
5230 (setq results
5231 (mapcar (lambda (i) (replace-regexp-in-string
5232 org-bracket-link-regexp "\\3" i)) results))
5233 (concat (org-agenda-finalize-entries results) "\n"))))
5235 ;;; Agenda entry finders
5237 (defun org-agenda-get-day-entries (file date &rest args)
5238 "Does the work for `org-diary' and `org-agenda'.
5239 FILE is the path to a file to be checked for entries. DATE is date like
5240 the one returned by `calendar-current-date'. ARGS are symbols indicating
5241 which kind of entries should be extracted. For details about these, see
5242 the documentation of `org-diary'."
5243 (setq args (or args org-agenda-entry-types))
5244 (let* ((org-startup-folded nil)
5245 (org-startup-align-all-tables nil)
5246 (buffer (if (file-exists-p file)
5247 (org-get-agenda-file-buffer file)
5248 (error "No such file %s" file)))
5249 arg results rtn deadline-results)
5250 (if (not buffer)
5251 ;; If file does not exist, make sure an error message ends up in diary
5252 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5253 (with-current-buffer buffer
5254 (unless (derived-mode-p 'org-mode)
5255 (error "Agenda file %s is not in `org-mode'" file))
5256 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5257 (let ((case-fold-search nil))
5258 (save-excursion
5259 (save-restriction
5260 (if org-agenda-restrict
5261 (narrow-to-region org-agenda-restrict-begin
5262 org-agenda-restrict-end)
5263 (widen))
5264 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5265 (while (setq arg (pop args))
5266 (cond
5267 ((and (eq arg :todo)
5268 (equal date (calendar-gregorian-from-absolute
5269 (org-today))))
5270 (setq rtn (org-agenda-get-todos))
5271 (setq results (append results rtn)))
5272 ((eq arg :timestamp)
5273 (setq rtn (org-agenda-get-blocks))
5274 (setq results (append results rtn))
5275 (setq rtn (org-agenda-get-timestamps deadline-results))
5276 (setq results (append results rtn)))
5277 ((eq arg :sexp)
5278 (setq rtn (org-agenda-get-sexps))
5279 (setq results (append results rtn)))
5280 ((eq arg :scheduled)
5281 (setq rtn (org-agenda-get-scheduled deadline-results))
5282 (setq results (append results rtn)))
5283 ((eq arg :scheduled*)
5284 (setq rtn (org-agenda-get-scheduled deadline-results t))
5285 (setq results (append results rtn)))
5286 ((eq arg :closed)
5287 (setq rtn (org-agenda-get-progress))
5288 (setq results (append results rtn)))
5289 ((eq arg :deadline)
5290 (setq rtn (org-agenda-get-deadlines))
5291 (setq deadline-results (copy-sequence rtn))
5292 (setq results (append results rtn)))
5293 ((eq arg :deadline*)
5294 (setq rtn (org-agenda-get-deadlines t))
5295 (setq deadline-results (copy-sequence rtn))
5296 (setq results (append results rtn))))))))
5297 results))))
5299 (defsubst org-em (x y list)
5300 "Is X or Y a member of LIST?"
5301 (or (memq x list) (memq y list)))
5303 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5304 (defvar org-agenda-sorting-strategy-selected nil)
5306 (defun org-agenda-get-todos ()
5307 "Return the TODO information for agenda display."
5308 (let* ((props (list 'face nil
5309 'done-face 'org-agenda-done
5310 'org-not-done-regexp org-not-done-regexp
5311 'org-todo-regexp org-todo-regexp
5312 'org-complex-heading-regexp org-complex-heading-regexp
5313 'mouse-face 'highlight
5314 'help-echo
5315 (format "mouse-2 or RET jump to org file %s"
5316 (abbreviate-file-name buffer-file-name))))
5317 (regexp (format org-heading-keyword-regexp-format
5318 (cond
5319 ((and org-select-this-todo-keyword
5320 (equal org-select-this-todo-keyword "*"))
5321 org-todo-regexp)
5322 (org-select-this-todo-keyword
5323 (concat "\\("
5324 (mapconcat 'identity
5325 (org-split-string
5326 org-select-this-todo-keyword
5327 "|")
5328 "\\|") "\\)"))
5329 (t org-not-done-regexp))))
5330 marker priority category category-pos level tags todo-state ts-date ts-date-type
5331 ee txt beg end inherited-tags)
5332 (goto-char (point-min))
5333 (while (re-search-forward regexp nil t)
5334 (catch :skip
5335 (save-match-data
5336 (beginning-of-line)
5337 (org-agenda-skip)
5338 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5339 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5340 (goto-char (1+ beg))
5341 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5342 (throw :skip nil)))
5343 (goto-char (match-beginning 2))
5344 (setq marker (org-agenda-new-marker (match-beginning 0))
5345 category (org-get-category)
5346 ts-date (let (ts)
5347 (save-match-data
5348 (cond ((org-em 'scheduled-up 'scheduled-down
5349 org-agenda-sorting-strategy-selected)
5350 (setq ts (org-entry-get (point) "SCHEDULED")
5351 ts-date-type " scheduled"))
5352 ((org-em 'deadline-up 'deadline-down
5353 org-agenda-sorting-strategy-selected)
5354 (setq ts (org-entry-get (point) "DEADLINE")
5355 ts-date-type " deadline"))
5356 ((org-em 'ts-up 'ts-down
5357 org-agenda-sorting-strategy-selected)
5358 (setq ts (org-entry-get (point) "TIMESTAMP")
5359 ts-date-type " timestamp"))
5360 ((org-em 'tsia-up 'tsia-down
5361 org-agenda-sorting-strategy-selected)
5362 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5363 ts-date-type " timestamp_ia"))
5364 ((org-em 'timestamp-up 'timestamp-down
5365 org-agenda-sorting-strategy-selected)
5366 (setq ts (or (org-entry-get (point) "SCHEDULED")
5367 (org-entry-get (point) "DEADLINE")
5368 (org-entry-get (point) "TIMESTAMP")
5369 (org-entry-get (point) "TIMESTAMP_IA"))
5370 ts-date-type ""))
5371 (t (setq ts-date-type "")))
5372 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5373 category-pos (get-text-property (point) 'org-category-position)
5374 txt (org-trim
5375 (buffer-substring (match-beginning 2) (match-end 0)))
5376 inherited-tags
5377 (or (eq org-agenda-show-inherited-tags 'always)
5378 (and (listp org-agenda-show-inherited-tags)
5379 (memq 'todo org-agenda-show-inherited-tags))
5380 (and (eq org-agenda-show-inherited-tags t)
5381 (or (eq org-agenda-use-tag-inheritance t)
5382 (memq 'todo org-agenda-use-tag-inheritance))))
5383 tags (org-get-tags-at nil (not inherited-tags))
5384 level (make-string (org-reduced-level (org-outline-level)) ? )
5385 txt (org-agenda-format-item "" txt level category tags t)
5386 priority (1+ (org-get-priority txt))
5387 todo-state (org-get-todo-state))
5388 (org-add-props txt props
5389 'org-marker marker 'org-hd-marker marker
5390 'priority priority 'org-category category
5391 'level level
5392 'ts-date ts-date
5393 'org-category-position category-pos
5394 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5395 (push txt ee)
5396 (if org-agenda-todo-list-sublevels
5397 (goto-char (match-end 2))
5398 (org-end-of-subtree 'invisible))))
5399 (nreverse ee)))
5401 (defun org-agenda-todo-custom-ignore-p (time n)
5402 "Check whether timestamp is farther away than n number of days.
5403 This function is invoked if `org-agenda-todo-ignore-deadlines',
5404 `org-agenda-todo-ignore-scheduled' or
5405 `org-agenda-todo-ignore-timestamp' is set to an integer."
5406 (let ((days (org-time-stamp-to-now
5407 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5408 (if (>= n 0)
5409 (>= days n)
5410 (<= days n))))
5412 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5413 (&optional end)
5414 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5415 (when (or org-agenda-todo-ignore-with-date
5416 org-agenda-todo-ignore-scheduled
5417 org-agenda-todo-ignore-deadlines
5418 org-agenda-todo-ignore-timestamp)
5419 (setq end (or end (save-excursion (outline-next-heading) (point))))
5420 (save-excursion
5421 (or (and org-agenda-todo-ignore-with-date
5422 (re-search-forward org-ts-regexp end t))
5423 (and org-agenda-todo-ignore-scheduled
5424 (re-search-forward org-scheduled-time-regexp end t)
5425 (cond
5426 ((eq org-agenda-todo-ignore-scheduled 'future)
5427 (> (org-time-stamp-to-now
5428 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5429 ((eq org-agenda-todo-ignore-scheduled 'past)
5430 (<= (org-time-stamp-to-now
5431 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5432 ((numberp org-agenda-todo-ignore-scheduled)
5433 (org-agenda-todo-custom-ignore-p
5434 (match-string 1) org-agenda-todo-ignore-scheduled))
5435 (t)))
5436 (and org-agenda-todo-ignore-deadlines
5437 (re-search-forward org-deadline-time-regexp end t)
5438 (cond
5439 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5440 ((eq org-agenda-todo-ignore-deadlines 'far)
5441 (not (org-deadline-close (match-string 1))))
5442 ((eq org-agenda-todo-ignore-deadlines 'future)
5443 (> (org-time-stamp-to-now
5444 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5445 ((eq org-agenda-todo-ignore-deadlines 'past)
5446 (<= (org-time-stamp-to-now
5447 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5448 ((numberp org-agenda-todo-ignore-deadlines)
5449 (org-agenda-todo-custom-ignore-p
5450 (match-string 1) org-agenda-todo-ignore-deadlines))
5451 (t (org-deadline-close (match-string 1)))))
5452 (and org-agenda-todo-ignore-timestamp
5453 (let ((buffer (current-buffer))
5454 (regexp
5455 (concat
5456 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5457 (start (point)))
5458 ;; Copy current buffer into a temporary one
5459 (with-temp-buffer
5460 (insert-buffer-substring buffer start end)
5461 (goto-char (point-min))
5462 ;; Delete SCHEDULED and DEADLINE items
5463 (while (re-search-forward regexp end t)
5464 (delete-region (match-beginning 0) (match-end 0)))
5465 (goto-char (point-min))
5466 ;; No search for timestamp left
5467 (when (re-search-forward org-ts-regexp nil t)
5468 (cond
5469 ((eq org-agenda-todo-ignore-timestamp 'future)
5470 (> (org-time-stamp-to-now
5471 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5472 ((eq org-agenda-todo-ignore-timestamp 'past)
5473 (<= (org-time-stamp-to-now
5474 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5475 ((numberp org-agenda-todo-ignore-timestamp)
5476 (org-agenda-todo-custom-ignore-p
5477 (match-string 1) org-agenda-todo-ignore-timestamp))
5478 (t))))))))))
5480 (defun org-agenda-get-timestamps (&optional deadline-results)
5481 "Return the date stamp information for agenda display."
5482 (let* ((props (list 'face 'org-agenda-calendar-event
5483 'org-not-done-regexp org-not-done-regexp
5484 'org-todo-regexp org-todo-regexp
5485 'org-complex-heading-regexp org-complex-heading-regexp
5486 'mouse-face 'highlight
5487 'help-echo
5488 (format "mouse-2 or RET jump to org file %s"
5489 (abbreviate-file-name buffer-file-name))))
5490 (d1 (calendar-absolute-from-gregorian date))
5492 (deadline-position-alist
5493 (mapcar (lambda (a) (and (setq mm (get-text-property
5494 0 'org-hd-marker a))
5495 (cons (marker-position mm) a)))
5496 deadline-results))
5497 (remove-re org-ts-regexp)
5498 (regexp
5499 (concat
5500 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5501 (regexp-quote
5502 (substring
5503 (format-time-string
5504 (car org-time-stamp-formats)
5505 (apply 'encode-time ; DATE bound by calendar
5506 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5507 1 11))
5508 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5509 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5510 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5511 donep tmp priority category category-pos level ee txt timestr tags
5512 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5513 inherited-tags ts-date)
5514 (goto-char (point-min))
5515 (while (setq end-of-match (re-search-forward regexp nil t))
5516 (setq b0 (match-beginning 0)
5517 b3 (match-beginning 3) e3 (match-end 3)
5518 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5519 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5520 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5521 (member todo-state
5522 org-agenda-repeating-timestamp-show-all)))
5523 (catch :skip
5524 (and (org-at-date-range-p) (throw :skip nil))
5525 (org-agenda-skip)
5526 (if (and (match-end 1)
5527 (not (= d1 (org-time-string-to-absolute
5528 (match-string 1) d1 nil show-all
5529 (current-buffer) b0))))
5530 (throw :skip nil))
5531 (if (and e3
5532 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5533 (throw :skip nil))
5534 (setq tmp (buffer-substring (max (point-min)
5535 (- b0 org-ds-keyword-length))
5537 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5538 inactivep (= (char-after b0) ?\[)
5539 deadlinep (string-match org-deadline-regexp tmp)
5540 scheduledp (string-match org-scheduled-regexp tmp)
5541 closedp (and org-agenda-include-inactive-timestamps
5542 (string-match org-closed-string tmp))
5543 clockp (and org-agenda-include-inactive-timestamps
5544 (or (string-match org-clock-string tmp)
5545 (string-match "]-+\\'" tmp)))
5546 warntime (get-text-property (point) 'org-appt-warntime)
5547 donep (member todo-state org-done-keywords))
5548 (if (or scheduledp deadlinep closedp clockp
5549 (and donep org-agenda-skip-timestamp-if-done))
5550 (throw :skip t))
5551 (if (string-match ">" timestr)
5552 ;; substring should only run to end of time stamp
5553 (setq timestr (substring timestr 0 (match-end 0))))
5554 (setq marker (org-agenda-new-marker b0)
5555 category (org-get-category b0)
5556 category-pos (get-text-property b0 'org-category-position))
5557 (save-excursion
5558 (if (not (re-search-backward org-outline-regexp-bol nil t))
5559 (throw :skip nil)
5560 (goto-char (match-beginning 0))
5561 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5562 (assoc (point) deadline-position-alist))
5563 (throw :skip nil))
5564 (setq hdmarker (org-agenda-new-marker)
5565 inherited-tags
5566 (or (eq org-agenda-show-inherited-tags 'always)
5567 (and (listp org-agenda-show-inherited-tags)
5568 (memq 'agenda org-agenda-show-inherited-tags))
5569 (and (eq org-agenda-show-inherited-tags t)
5570 (or (eq org-agenda-use-tag-inheritance t)
5571 (memq 'agenda org-agenda-use-tag-inheritance))))
5572 tags (org-get-tags-at nil (not inherited-tags))
5573 level (make-string (org-reduced-level (org-outline-level)) ? ))
5574 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5575 (setq head (or (match-string 1) ""))
5576 (setq txt (org-agenda-format-item
5577 (if inactivep org-agenda-inactive-leader nil)
5578 head level category tags timestr
5579 remove-re habitp)))
5580 (setq priority (org-get-priority txt))
5581 (org-add-props txt props 'priority priority
5582 'org-marker marker 'org-hd-marker hdmarker
5583 'org-category category 'date date
5584 'level level
5585 'ts-date
5586 (ignore-errors (org-time-string-to-absolute timestr))
5587 'org-category-position category-pos
5588 'todo-state todo-state
5589 'warntime warntime
5590 'type "timestamp")
5591 (push txt ee))
5592 (if org-agenda-skip-additional-timestamps-same-entry
5593 (outline-next-heading)
5594 (goto-char end-of-match))))
5595 (nreverse ee)))
5597 (defun org-agenda-get-sexps ()
5598 "Return the sexp information for agenda display."
5599 (require 'diary-lib)
5600 (let* ((props (list 'face 'org-agenda-calendar-sexp
5601 'mouse-face 'highlight
5602 'help-echo
5603 (format "mouse-2 or RET jump to org file %s"
5604 (abbreviate-file-name buffer-file-name))))
5605 (regexp "^&?%%(")
5606 marker category extra category-pos level ee txt tags entry
5607 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5608 (goto-char (point-min))
5609 (while (re-search-forward regexp nil t)
5610 (catch :skip
5611 (org-agenda-skip)
5612 (setq beg (match-beginning 0))
5613 (goto-char (1- (match-end 0)))
5614 (setq b (point))
5615 (forward-sexp 1)
5616 (setq sexp (buffer-substring b (point)))
5617 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5618 (org-trim (match-string 1))
5619 ""))
5620 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5621 (when result
5622 (setq marker (org-agenda-new-marker beg)
5623 level (make-string (org-reduced-level (org-outline-level)) ? )
5624 category (org-get-category beg)
5625 category-pos (get-text-property beg 'org-category-position)
5626 inherited-tags
5627 (or (eq org-agenda-show-inherited-tags 'always)
5628 (and (listp org-agenda-show-inherited-tags)
5629 (memq 'agenda org-agenda-show-inherited-tags))
5630 (and (eq org-agenda-show-inherited-tags t)
5631 (or (eq org-agenda-use-tag-inheritance t)
5632 (memq 'agenda org-agenda-use-tag-inheritance))))
5633 tags (org-get-tags-at nil (not inherited-tags))
5634 todo-state (org-get-todo-state)
5635 warntime (get-text-property (point) 'org-appt-warntime)
5636 extra nil)
5638 (dolist (r (if (stringp result)
5639 (list result)
5640 result)) ;; we expect a list here
5641 (when (and org-agenda-diary-sexp-prefix
5642 (string-match org-agenda-diary-sexp-prefix r))
5643 (setq extra (match-string 0 r)
5644 r (replace-match "" nil nil r)))
5645 (if (string-match "\\S-" r)
5646 (setq txt r)
5647 (setq txt "SEXP entry returned empty string"))
5648 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5649 (org-add-props txt props 'org-marker marker
5650 'org-category category 'date date 'todo-state todo-state
5651 'org-category-position category-pos 'tags tags
5652 'level level
5653 'type "sexp" 'warntime warntime)
5654 (push txt ee)))))
5655 (nreverse ee)))
5657 ;; Calendar sanity: define some functions that are independent of
5658 ;; `calendar-date-style'.
5659 ;; Normally I would like to use ISO format when calling the diary functions,
5660 ;; but to make sure we still have Emacs 22 compatibility we bind
5661 ;; also `european-calendar-style' and use european format
5662 (defun org-anniversary (year month day &optional mark)
5663 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5664 (org-no-warnings
5665 (let ((calendar-date-style 'european) (european-calendar-style t))
5666 (diary-anniversary day month year mark))))
5667 (defun org-cyclic (N year month day &optional mark)
5668 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5669 (org-no-warnings
5670 (let ((calendar-date-style 'european) (european-calendar-style t))
5671 (diary-cyclic N day month year mark))))
5672 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5673 "Like `diary-block', but with fixed (ISO) order of arguments."
5674 (org-no-warnings
5675 (let ((calendar-date-style 'european) (european-calendar-style t))
5676 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5677 (defun org-date (year month day &optional mark)
5678 "Like `diary-date', but with fixed (ISO) order of arguments."
5679 (org-no-warnings
5680 (let ((calendar-date-style 'european) (european-calendar-style t))
5681 (diary-date day month year mark))))
5683 ;; Define the` org-class' function
5684 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5685 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5686 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5687 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5688 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5689 `holidays', then any date that is known by the Emacs calendar to be a
5690 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5691 then those holidays will be skipped."
5692 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5693 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5694 (d (calendar-absolute-from-gregorian date))
5695 (h (when skip-weeks (calendar-check-holidays date))))
5696 (and
5697 (<= date1 d)
5698 (<= d date2)
5699 (= (calendar-day-of-week date) dayname)
5700 (or (not skip-weeks)
5701 (progn
5702 (require 'cal-iso)
5703 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5704 (not (or (and h (memq 'holidays skip-weeks))
5705 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5706 entry)))
5708 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5709 "Like `org-class', but honor `calendar-date-style'.
5710 The order of the first 2 times 3 arguments depends on the variable
5711 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5712 So for American calendars, give this as MONTH DAY YEAR, for European as
5713 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5714 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5715 is any number of ISO weeks in the block period for which the item should
5716 be skipped.
5718 This function is here only for backward compatibility and it is deprecated,
5719 please use `org-class' instead."
5720 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5721 (date2 (org-order-calendar-date-args m2 d2 y2)))
5722 (org-class
5723 (nth 2 date1) (car date1) (nth 1 date1)
5724 (nth 2 date2) (car date2) (nth 1 date2)
5725 dayname skip-weeks)))
5726 (make-obsolete 'org-diary-class 'org-class "")
5728 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
5729 (defalias 'org-get-closed 'org-agenda-get-progress)
5730 (defun org-agenda-get-progress ()
5731 "Return the logged TODO entries for agenda display."
5732 (let* ((props (list 'mouse-face 'highlight
5733 'org-not-done-regexp org-not-done-regexp
5734 'org-todo-regexp org-todo-regexp
5735 'org-complex-heading-regexp org-complex-heading-regexp
5736 'help-echo
5737 (format "mouse-2 or RET jump to org file %s"
5738 (abbreviate-file-name buffer-file-name))))
5739 (items (if (consp org-agenda-show-log-scoped)
5740 org-agenda-show-log-scoped
5741 (if (eq org-agenda-show-log-scoped 'clockcheck)
5742 '(clock)
5743 org-agenda-log-mode-items)))
5744 (parts
5745 (delq nil
5746 (list
5747 (if (memq 'closed items) (concat "\\<" org-closed-string))
5748 (if (memq 'clock items) (concat "\\<" org-clock-string))
5749 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5750 (parts-re (if parts (mapconcat 'identity parts "\\|")
5751 (error "`org-agenda-log-mode-items' is empty")))
5752 (regexp (concat
5753 "\\(" parts-re "\\)"
5754 " *\\["
5755 (regexp-quote
5756 (substring
5757 (format-time-string
5758 (car org-time-stamp-formats)
5759 (apply 'encode-time ; DATE bound by calendar
5760 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5761 1 11))))
5762 (org-agenda-search-headline-for-time nil)
5763 marker hdmarker priority category category-pos level tags closedp
5764 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5765 (goto-char (point-min))
5766 (while (re-search-forward regexp nil t)
5767 (catch :skip
5768 (org-agenda-skip)
5769 (setq marker (org-agenda-new-marker (match-beginning 0))
5770 closedp (equal (match-string 1) org-closed-string)
5771 statep (equal (string-to-char (match-string 1)) ?-)
5772 clockp (not (or closedp statep))
5773 state (and statep (match-string 2))
5774 category (org-get-category (match-beginning 0))
5775 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5776 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5777 (when (string-match "\\]" timestr)
5778 ;; substring should only run to end of time stamp
5779 (setq rest (substring timestr (match-end 0))
5780 timestr (substring timestr 0 (match-end 0)))
5781 (if (and (not closedp) (not statep)
5782 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5783 rest))
5784 (progn (setq timestr (concat (substring timestr 0 -1)
5785 "-" (match-string 1 rest) "]"))
5786 (setq clocked (match-string 2 rest)))
5787 (setq clocked "-")))
5788 (save-excursion
5789 (setq extra
5790 (cond
5791 ((not org-agenda-log-mode-add-notes) nil)
5792 (statep
5793 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5794 (match-string 1)))
5795 (clockp
5796 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5797 (match-string 1)))))
5798 (if (not (re-search-backward org-outline-regexp-bol nil t))
5799 (throw :skip nil)
5800 (goto-char (match-beginning 0))
5801 (setq hdmarker (org-agenda-new-marker)
5802 inherited-tags
5803 (or (eq org-agenda-show-inherited-tags 'always)
5804 (and (listp org-agenda-show-inherited-tags)
5805 (memq 'todo org-agenda-show-inherited-tags))
5806 (and (eq org-agenda-show-inherited-tags t)
5807 (or (eq org-agenda-use-tag-inheritance t)
5808 (memq 'todo org-agenda-use-tag-inheritance))))
5809 tags (org-get-tags-at nil (not inherited-tags))
5810 level (make-string (org-reduced-level (org-outline-level)) ? ))
5811 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5812 (setq txt (match-string 1))
5813 (when extra
5814 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5815 (setq txt (concat (substring txt 0 (match-beginning 1))
5816 " - " extra " " (match-string 2 txt)))
5817 (setq txt (concat txt " - " extra))))
5818 (setq txt (org-agenda-format-item
5819 (cond
5820 (closedp "Closed: ")
5821 (statep (concat "State: (" state ")"))
5822 (t (concat "Clocked: (" clocked ")")))
5823 txt level category tags timestr)))
5824 (setq priority 100000)
5825 (org-add-props txt props
5826 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5827 'priority priority 'org-category category
5828 'org-category-position category-pos
5829 'level level
5830 'type "closed" 'date date
5831 'undone-face 'org-warning 'done-face 'org-agenda-done)
5832 (push txt ee))
5833 (goto-char (point-at-eol))))
5834 (nreverse ee)))
5836 (defun org-agenda-show-clocking-issues ()
5837 "Add overlays, showing issues with clocking.
5838 See also the user option `org-agenda-clock-consistency-checks'."
5839 (interactive)
5840 (let* ((org-time-clocksum-use-effort-durations nil)
5841 (pl org-agenda-clock-consistency-checks)
5842 (re (concat "^[ \t]*"
5843 org-clock-string
5844 "[ \t]+"
5845 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5846 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5847 (tlstart 0.)
5848 (tlend 0.)
5849 (maxtime (org-hh:mm-string-to-minutes
5850 (or (plist-get pl :max-duration) "24:00")))
5851 (mintime (org-hh:mm-string-to-minutes
5852 (or (plist-get pl :min-duration) 0)))
5853 (maxgap (org-hh:mm-string-to-minutes
5854 ;; default 30:00 means never complain
5855 (or (plist-get pl :max-gap) "30:00")))
5856 (gapok (mapcar 'org-hh:mm-string-to-minutes
5857 (plist-get pl :gap-ok-around)))
5858 (def-face (or (plist-get pl :default-face)
5859 '((:background "DarkRed") (:foreground "white"))))
5860 issue face m te ts dt ov)
5861 (goto-char (point-min))
5862 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5863 (setq issue nil face def-face)
5864 (catch 'next
5865 (setq m (org-get-at-bol 'org-marker)
5866 te nil ts nil)
5867 (unless (and m (markerp m))
5868 (setq issue "No valid clock line") (throw 'next t))
5869 (org-with-point-at m
5870 (save-excursion
5871 (goto-char (point-at-bol))
5872 (unless (looking-at re)
5873 (error "No valid Clock line")
5874 (throw 'next t))
5875 (unless (match-end 3)
5876 (setq issue "No end time"
5877 face (or (plist-get pl :no-end-time-face) face))
5878 (throw 'next t))
5879 (setq ts (match-string 1)
5880 te (match-string 3)
5881 ts (org-float-time
5882 (apply 'encode-time (org-parse-time-string ts)))
5883 te (org-float-time
5884 (apply 'encode-time (org-parse-time-string te)))
5885 dt (- te ts))))
5886 (cond
5887 ((> dt (* 60 maxtime))
5888 ;; a very long clocking chunk
5889 (setq issue (format "Clocking interval is very long: %s"
5890 (org-minutes-to-clocksum-string
5891 (floor (/ (float dt) 60.))))
5892 face (or (plist-get pl :long-face) face)))
5893 ((< dt (* 60 mintime))
5894 ;; a very short clocking chunk
5895 (setq issue (format "Clocking interval is very short: %s"
5896 (org-minutes-to-clocksum-string
5897 (floor (/ (float dt) 60.))))
5898 face (or (plist-get pl :short-face) face)))
5899 ((and (> tlend 0) (< ts tlend))
5900 ;; Two clock entries are overlapping
5901 (setq issue (format "Clocking overlap: %d minutes"
5902 (/ (- tlend ts) 60))
5903 face (or (plist-get pl :overlap-face) face)))
5904 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5905 ;; There is a gap, lets see if we need to report it
5906 (unless (org-agenda-check-clock-gap tlend ts gapok)
5907 (setq issue (format "Clocking gap: %d minutes"
5908 (/ (- ts tlend) 60))
5909 face (or (plist-get pl :gap-face) face))))
5910 (t nil)))
5911 (setq tlend (or te tlend) tlstart (or ts tlstart))
5912 (when issue
5913 ;; OK, there was some issue, add an overlay to show the issue
5914 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5915 (overlay-put ov 'before-string
5916 (concat
5917 (org-add-props
5918 (format "%-43s" (concat " " issue))
5920 'face face)
5921 "\n"))
5922 (overlay-put ov 'evaporate t)))))
5924 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5925 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5926 (catch 'exit
5927 (unless ok-list
5928 ;; there are no OK times for gaps...
5929 (throw 'exit nil))
5930 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5931 ;; This is more than 24 hours, so it is OK.
5932 ;; because we have at least one OK time, that must be in the
5933 ;; 24 hour interval.
5934 (throw 'exit t))
5935 ;; We have a shorter gap.
5936 ;; Now we have to get the minute of the day when these times are
5937 (let* ((t1dec (decode-time (seconds-to-time t1)))
5938 (t2dec (decode-time (seconds-to-time t2)))
5939 ;; compute the minute on the day
5940 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5941 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5942 (when (< min2 min1)
5943 ;; if min2 is smaller than min1, this means it is on the next day.
5944 ;; Wrap it to after midnight.
5945 (setq min2 (+ min2 1440)))
5946 ;; Now check if any of the OK times is in the gap
5947 (mapc (lambda (x)
5948 ;; Wrap the time to after midnight if necessary
5949 (if (< x min1) (setq x (+ x 1440)))
5950 ;; Check if in interval
5951 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5952 ok-list)
5953 ;; Nope, this gap is not OK
5954 nil)))
5956 (defun org-agenda-get-deadlines (&optional with-hour)
5957 "Return the deadline information for agenda display.
5958 When WITH-HOUR is non-nil, only return deadlines with an hour
5959 specification like [h]h:mm."
5960 (let* ((props (list 'mouse-face 'highlight
5961 'org-not-done-regexp org-not-done-regexp
5962 'org-todo-regexp org-todo-regexp
5963 'org-complex-heading-regexp org-complex-heading-regexp
5964 'help-echo
5965 (format "mouse-2 or RET jump to org file %s"
5966 (abbreviate-file-name buffer-file-name))))
5967 (regexp (if with-hour
5968 org-deadline-time-hour-regexp
5969 org-deadline-time-regexp))
5970 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5971 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5972 (dl0 (car org-agenda-deadline-leaders))
5973 (dl1 (nth 1 org-agenda-deadline-leaders))
5974 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
5975 d2 diff dfrac wdays pos pos1 category category-pos level
5976 tags suppress-prewarning ee txt head face s todo-state
5977 show-all upcomingp donep timestr warntime inherited-tags ts-date)
5978 (goto-char (point-min))
5979 (while (re-search-forward regexp nil t)
5980 (catch :skip
5981 (org-agenda-skip)
5982 (setq s (match-string 1)
5983 txt nil
5984 pos (1- (match-beginning 1))
5985 todo-state (save-match-data (org-get-todo-state))
5986 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5987 (member todo-state
5988 org-agenda-repeating-timestamp-show-all))
5989 d2 (org-time-string-to-absolute
5990 s d1 'past show-all (current-buffer) pos)
5991 diff (- d2 d1))
5992 (setq suppress-prewarning
5993 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
5994 (let ((item (buffer-substring (point-at-bol)
5995 (point-at-eol))))
5996 (save-match-data
5997 (and (string-match
5998 org-scheduled-time-regexp item)
5999 (match-string 1 item)))))))
6000 (cond
6001 ((not ds) nil)
6002 ;; The current item has a scheduled date (in ds), so
6003 ;; evaluate its prewarning lead time.
6004 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6005 ;; Use global prewarning-restart lead time.
6006 org-agenda-skip-deadline-prewarning-if-scheduled)
6007 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6008 'pre-scheduled)
6009 ;; Set prewarning to no earlier than scheduled.
6010 (min (- d2 (org-time-string-to-absolute
6011 ds d1 'past show-all (current-buffer) pos))
6012 org-deadline-warning-days))
6013 ;; Set prewarning to deadline.
6014 (t 0))))
6015 (setq wdays (if suppress-prewarning
6016 (let ((org-deadline-warning-days suppress-prewarning))
6017 (org-get-wdays s))
6018 (org-get-wdays s))
6019 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6020 upcomingp (and todayp (> diff 0)))
6021 ;; When to show a deadline in the calendar:
6022 ;; If the expiration is within wdays warning time.
6023 ;; Past-due deadlines are only shown on the current date
6024 (if (and (or (and (<= diff wdays)
6025 (and todayp (not org-agenda-only-exact-dates)))
6026 (= diff 0)))
6027 (save-excursion
6028 ;; (setq todo-state (org-get-todo-state))
6029 (setq donep (member todo-state org-done-keywords))
6030 (if (and donep
6031 (or org-agenda-skip-deadline-if-done
6032 (not (= diff 0))))
6033 (setq txt nil)
6034 (setq category (org-get-category)
6035 warntime (get-text-property (point) 'org-appt-warntime)
6036 category-pos (get-text-property (point) 'org-category-position))
6037 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6038 (throw :skip nil)
6039 (goto-char (match-end 0))
6040 (setq pos1 (match-beginning 0))
6041 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6042 (setq inherited-tags
6043 (or (eq org-agenda-show-inherited-tags 'always)
6044 (and (listp org-agenda-show-inherited-tags)
6045 (memq 'agenda org-agenda-show-inherited-tags))
6046 (and (eq org-agenda-show-inherited-tags t)
6047 (or (eq org-agenda-use-tag-inheritance t)
6048 (memq 'agenda org-agenda-use-tag-inheritance))))
6049 tags (org-get-tags-at pos1 (not inherited-tags)))
6050 (setq head (buffer-substring
6051 (point)
6052 (progn (skip-chars-forward "^\r\n")
6053 (point))))
6054 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6055 (setq timestr
6056 (concat (substring s (match-beginning 1)) " "))
6057 (setq timestr 'time))
6058 (setq txt (org-agenda-format-item
6059 (cond ((= diff 0) dl0)
6060 ((> diff 0)
6061 (if (functionp dl1)
6062 (funcall dl1 diff date)
6063 (format dl1 diff)))
6065 (if (functionp dl2)
6066 (funcall dl2 diff date)
6067 (format dl2 (if (string= dl2 dl1)
6068 diff (abs diff))))))
6069 head level category tags
6070 (if (not (= diff 0)) nil timestr)))))
6071 (when txt
6072 (setq face (org-agenda-deadline-face dfrac))
6073 (org-add-props txt props
6074 'org-marker (org-agenda-new-marker pos)
6075 'warntime warntime
6076 'level level
6077 'ts-date d2
6078 'org-hd-marker (org-agenda-new-marker pos1)
6079 'priority (+ (- diff)
6080 (org-get-priority txt))
6081 'org-category category
6082 'org-category-position category-pos
6083 'todo-state todo-state
6084 'type (if upcomingp "upcoming-deadline" "deadline")
6085 'date (if upcomingp date d2)
6086 'face (if donep 'org-agenda-done face)
6087 'undone-face face 'done-face 'org-agenda-done)
6088 (push txt ee))))))
6089 (nreverse ee)))
6091 (defun org-agenda-deadline-face (fraction)
6092 "Return the face to displaying a deadline item.
6093 FRACTION is what fraction of the head-warning time has passed."
6094 (let ((faces org-agenda-deadline-faces) f)
6095 (catch 'exit
6096 (while (setq f (pop faces))
6097 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6099 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6100 "Return the scheduled information for agenda display.
6101 When WITH-HOUR is non-nil, only return scheduled items with
6102 an hour specification like [h]h:mm."
6103 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6104 'org-todo-regexp org-todo-regexp
6105 'org-complex-heading-regexp org-complex-heading-regexp
6106 'done-face 'org-agenda-done
6107 'mouse-face 'highlight
6108 'help-echo
6109 (format "mouse-2 or RET jump to org file %s"
6110 (abbreviate-file-name buffer-file-name))))
6111 (regexp (if with-hour
6112 org-scheduled-time-hour-regexp
6113 org-scheduled-time-regexp))
6114 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6115 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6117 (deadline-position-alist
6118 (mapcar (lambda (a) (and (setq mm (get-text-property
6119 0 'org-hd-marker a))
6120 (cons (marker-position mm) a)))
6121 deadline-results))
6122 d2 diff pos pos1 category category-pos level tags donep
6123 ee txt head pastschedp todo-state face timestr s habitp show-all
6124 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6125 ddays)
6126 (goto-char (point-min))
6127 (while (re-search-forward regexp nil t)
6128 (catch :skip
6129 (org-agenda-skip)
6130 (setq s (match-string 1)
6131 txt nil
6132 pos (1- (match-beginning 1))
6133 todo-state (save-match-data (org-get-todo-state))
6134 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6135 (member todo-state
6136 org-agenda-repeating-timestamp-show-all))
6137 d2 (org-time-string-to-absolute
6138 s d1 'past show-all (current-buffer) pos)
6139 diff (- d2 d1)
6140 warntime (get-text-property (point) 'org-appt-warntime))
6141 (setq pastschedp (and todayp (< diff 0)))
6142 (setq did-habit-check-p nil)
6143 (setq suppress-delay
6144 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6145 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6146 (save-match-data
6147 (and (string-match
6148 org-deadline-time-regexp item)
6149 (match-string 1 item)))))))
6150 (cond
6151 ((not ds) nil)
6152 ;; The current item has a deadline date (in ds), so
6153 ;; evaluate its delay time.
6154 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6155 ;; Use global delay time.
6156 (- org-agenda-skip-scheduled-delay-if-deadline))
6157 ((eq org-agenda-skip-scheduled-delay-if-deadline
6158 'post-deadline)
6159 ;; Set delay to no later than deadline.
6160 (min (- d2 (org-time-string-to-absolute
6161 ds d1 'past show-all (current-buffer) pos))
6162 org-scheduled-delay-days))
6163 (t 0))))
6164 (setq ddays (if suppress-delay
6165 (let ((org-scheduled-delay-days suppress-delay))
6166 (org-get-wdays s t t))
6167 (org-get-wdays s t)))
6168 ;; Use a delay of 0 when there is a repeater and the delay is
6169 ;; of the form --3d
6170 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6171 (< (org-time-string-to-absolute s)
6172 (org-time-string-to-absolute
6173 s d2 'past nil (current-buffer) pos)))
6174 (setq ddays 0))
6175 ;; When to show a scheduled item in the calendar:
6176 ;; If it is on or past the date.
6177 (when (or (and (> ddays 0) (= diff (- ddays)))
6178 (and (zerop ddays) (= diff 0))
6179 (and (< (+ diff ddays) 0)
6180 (< (abs diff) org-scheduled-past-days)
6181 (and todayp (not org-agenda-only-exact-dates)))
6182 ;; org-is-habit-p uses org-entry-get, which is expansive
6183 ;; so we go extra mile to only call it once
6184 (and todayp
6185 (boundp 'org-habit-show-all-today)
6186 org-habit-show-all-today
6187 (setq did-habit-check-p t)
6188 (setq habitp (and (functionp 'org-is-habit-p)
6189 (org-is-habit-p)))))
6190 (save-excursion
6191 (setq donep (member todo-state org-done-keywords))
6192 (if (and donep
6193 (or org-agenda-skip-scheduled-if-done
6194 (not (= diff 0))
6195 (and (functionp 'org-is-habit-p)
6196 (org-is-habit-p))))
6197 (setq txt nil)
6198 (setq habitp (if did-habit-check-p habitp
6199 (and (functionp 'org-is-habit-p)
6200 (org-is-habit-p))))
6201 (setq category (org-get-category)
6202 category-pos (get-text-property (point) 'org-category-position))
6203 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6204 'repeated-after-deadline)
6205 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6206 (throw :skip nil))
6207 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6208 (throw :skip nil)
6209 (goto-char (match-end 0))
6210 (setq pos1 (match-beginning 0))
6211 (if habitp
6212 (if (or (not org-habit-show-habits)
6213 (and (not todayp)
6214 (boundp 'org-habit-show-habits-only-for-today)
6215 org-habit-show-habits-only-for-today))
6216 (throw :skip nil))
6217 (if (and
6218 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6219 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6220 pastschedp))
6221 (setq mm (assoc pos1 deadline-position-alist)))
6222 (throw :skip nil)))
6223 (setq inherited-tags
6224 (or (eq org-agenda-show-inherited-tags 'always)
6225 (and (listp org-agenda-show-inherited-tags)
6226 (memq 'agenda org-agenda-show-inherited-tags))
6227 (and (eq org-agenda-show-inherited-tags t)
6228 (or (eq org-agenda-use-tag-inheritance t)
6229 (memq 'agenda org-agenda-use-tag-inheritance))))
6231 tags (org-get-tags-at nil (not inherited-tags)))
6232 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6233 (setq head (buffer-substring
6234 (point)
6235 (progn (skip-chars-forward "^\r\n") (point))))
6236 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6237 (setq timestr
6238 (concat (substring s (match-beginning 1)) " "))
6239 (setq timestr 'time))
6240 (setq txt (org-agenda-format-item
6241 (if (= diff 0)
6242 (car org-agenda-scheduled-leaders)
6243 (format (nth 1 org-agenda-scheduled-leaders)
6244 (- 1 diff)))
6245 head level category tags
6246 (if (not (= diff 0)) nil timestr)
6247 nil habitp))))
6248 (when txt
6249 (setq face
6250 (cond
6251 ((and (not habitp) pastschedp)
6252 'org-scheduled-previously)
6253 (todayp 'org-scheduled-today)
6254 (t 'org-scheduled))
6255 habitp (and habitp (org-habit-parse-todo)))
6256 (org-add-props txt props
6257 'undone-face face
6258 'face (if donep 'org-agenda-done face)
6259 'org-marker (org-agenda-new-marker pos)
6260 'org-hd-marker (org-agenda-new-marker pos1)
6261 'type (if pastschedp "past-scheduled" "scheduled")
6262 'date (if pastschedp d2 date)
6263 'ts-date d2
6264 'warntime warntime
6265 'level level
6266 'priority (if habitp
6267 (org-habit-get-priority habitp)
6268 (+ 94 (- 5 diff) (org-get-priority txt)))
6269 'org-category category
6270 'category-position category-pos
6271 'org-habit-p habitp
6272 'todo-state todo-state)
6273 (push txt ee))))))
6274 (nreverse ee)))
6276 (defun org-agenda-get-blocks ()
6277 "Return the date-range information for agenda display."
6278 (let* ((props (list 'face nil
6279 'org-not-done-regexp org-not-done-regexp
6280 'org-todo-regexp org-todo-regexp
6281 'org-complex-heading-regexp org-complex-heading-regexp
6282 'mouse-face 'highlight
6283 'help-echo
6284 (format "mouse-2 or RET jump to org file %s"
6285 (abbreviate-file-name buffer-file-name))))
6286 (regexp org-tr-regexp)
6287 (d0 (calendar-absolute-from-gregorian date))
6288 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6289 level todo-state tags pos head donep inherited-tags)
6290 (goto-char (point-min))
6291 (while (re-search-forward regexp nil t)
6292 (catch :skip
6293 (org-agenda-skip)
6294 (setq pos (point))
6295 (let ((start-time (match-string 1))
6296 (end-time (match-string 2)))
6297 (setq s1 (match-string 1)
6298 s2 (match-string 2)
6299 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6300 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6301 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6302 ;; Only allow days between the limits, because the normal
6303 ;; date stamps will catch the limits.
6304 (save-excursion
6305 (setq todo-state (org-get-todo-state))
6306 (setq donep (member todo-state org-done-keywords))
6307 (if (and donep org-agenda-skip-timestamp-if-done)
6308 (throw :skip t))
6309 (setq marker (org-agenda-new-marker (point)))
6310 (setq category (org-get-category)
6311 category-pos (get-text-property (point) 'org-category-position))
6312 (if (not (re-search-backward org-outline-regexp-bol nil t))
6313 (throw :skip nil)
6314 (goto-char (match-beginning 0))
6315 (setq hdmarker (org-agenda-new-marker (point))
6316 inherited-tags
6317 (or (eq org-agenda-show-inherited-tags 'always)
6318 (and (listp org-agenda-show-inherited-tags)
6319 (memq 'agenda org-agenda-show-inherited-tags))
6320 (and (eq org-agenda-show-inherited-tags t)
6321 (or (eq org-agenda-use-tag-inheritance t)
6322 (memq 'agenda org-agenda-use-tag-inheritance))))
6324 tags (org-get-tags-at nil (not inherited-tags)))
6325 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6326 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6327 (setq head (match-string 1))
6328 (let ((remove-re
6329 (if org-agenda-remove-timeranges-from-blocks
6330 (concat
6331 "<" (regexp-quote s1) ".*?>"
6332 "--"
6333 "<" (regexp-quote s2) ".*?>")
6334 nil)))
6335 (setq txt (org-agenda-format-item
6336 (format
6337 (nth (if (= d1 d2) 0 1)
6338 org-agenda-timerange-leaders)
6339 (1+ (- d0 d1)) (1+ (- d2 d1)))
6340 head level category tags
6341 (cond ((and (= d1 d0) (= d2 d0))
6342 (concat "<" start-time ">--<" end-time ">"))
6343 ((= d1 d0)
6344 (concat "<" start-time ">"))
6345 ((= d2 d0)
6346 (concat "<" end-time ">")))
6347 remove-re))))
6348 (org-add-props txt props
6349 'org-marker marker 'org-hd-marker hdmarker
6350 'type "block" 'date date
6351 'level level
6352 'todo-state todo-state
6353 'priority (org-get-priority txt) 'org-category category
6354 'org-category-position category-pos)
6355 (push txt ee))))
6356 (goto-char pos)))
6357 ;; Sort the entries by expiration date.
6358 (nreverse ee)))
6360 ;;; Agenda presentation and sorting
6362 (defvar org-prefix-has-time nil
6363 "A flag, set by `org-compile-prefix-format'.
6364 The flag is set if the currently compiled format contains a `%t'.")
6365 (defvar org-prefix-has-tag nil
6366 "A flag, set by `org-compile-prefix-format'.
6367 The flag is set if the currently compiled format contains a `%T'.")
6368 (defvar org-prefix-has-effort nil
6369 "A flag, set by `org-compile-prefix-format'.
6370 The flag is set if the currently compiled format contains a `%e'.")
6371 (defvar org-prefix-has-breadcrumbs nil
6372 "A flag, set by `org-compile-prefix-format'.
6373 The flag is set if the currently compiled format contains a `%b'.")
6374 (defvar org-prefix-category-length nil
6375 "Used by `org-compile-prefix-format' to remember the category field width.")
6376 (defvar org-prefix-category-max-length nil
6377 "Used by `org-compile-prefix-format' to remember the category field width.")
6379 (defun org-agenda-get-category-icon (category)
6380 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6381 (dolist (entry org-agenda-category-icon-alist)
6382 (when (org-string-match-p (car entry) category)
6383 (if (listp (cadr entry))
6384 (return (cadr entry))
6385 (return (apply 'create-image (cdr entry)))))))
6387 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6388 remove-re habitp)
6389 "Format TXT to be inserted into the agenda buffer.
6390 In particular, add the prefix and corresponding text properties.
6392 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6393 LEVEL may be a string to replace the `%l' specifier.
6394 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6395 category taken from local variable or file name. It will replace the `%c'
6396 specifier in the format.
6397 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6398 TXT for sorting of this entry, and for the `%t' specifier in the format.
6399 When DOTIME is a string, this string is searched for a time before TXT is.
6400 TAGS can be the tags of the headline.
6401 Any match of REMOVE-RE will be removed from TXT."
6402 ;; We keep the org-prefix-* variable values along with a compiled
6403 ;; formatter, so that multiple agendas existing at the same time do
6404 ;; not step on each other toes.
6406 ;; It was inconvenient to make these variables buffer local in
6407 ;; Agenda buffers, because this function expects to be called with
6408 ;; the buffer where item comes from being current, and not agenda
6409 ;; buffer
6410 (let* ((bindings (car org-prefix-format-compiled))
6411 (formatter (cadr org-prefix-format-compiled)))
6412 (loop for (var value) in bindings
6413 do (set var value))
6414 (save-match-data
6415 ;; Diary entries sometimes have extra whitespace at the beginning
6416 (setq txt (org-trim txt))
6418 ;; Fix the tags part in txt
6419 (setq txt (org-agenda-fix-displayed-tags
6420 txt tags
6421 org-agenda-show-inherited-tags
6422 org-agenda-hide-tags-regexp))
6424 (let* ((category (or category
6425 (if (stringp org-category)
6426 org-category
6427 (and org-category (symbol-name org-category)))
6428 (if buffer-file-name
6429 (file-name-sans-extension
6430 (file-name-nondirectory buffer-file-name))
6431 "")))
6432 (category-icon (org-agenda-get-category-icon category))
6433 (category-icon (if category-icon
6434 (propertize " " 'display category-icon)
6435 ""))
6436 ;; time, tag, effort are needed for the eval of the prefix format
6437 (tag (if tags (nth (1- (length tags)) tags) ""))
6438 time effort neffort
6439 (ts (if dotime (concat
6440 (if (stringp dotime) dotime "")
6441 (and org-agenda-search-headline-for-time txt))))
6442 (time-of-day (and dotime (org-get-time-of-day ts)))
6443 stamp plain s0 s1 s2 rtn srp l
6444 duration thecategory breadcrumbs)
6445 (and (derived-mode-p 'org-mode) buffer-file-name
6446 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6447 (when (and dotime time-of-day)
6448 ;; Extract starting and ending time and move them to prefix
6449 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6450 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6451 (setq s0 (match-string 0 ts)
6452 srp (and stamp (match-end 3))
6453 s1 (match-string (if plain 1 2) ts)
6454 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6456 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6457 ;; them, we might want to remove them there to avoid duplication.
6458 ;; The user can turn this off with a variable.
6459 (if (and org-prefix-has-time
6460 org-agenda-remove-times-when-in-prefix (or stamp plain)
6461 (string-match (concat (regexp-quote s0) " *") txt)
6462 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6463 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6464 (= (match-beginning 0) 0)
6466 (setq txt (replace-match "" nil nil txt))))
6467 ;; Normalize the time(s) to 24 hour
6468 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6469 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6471 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6472 (let (org-time-clocksum-use-effort-durations)
6473 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6474 (setq s2
6475 (org-minutes-to-clocksum-string
6476 (+ (org-hh:mm-string-to-minutes s1)
6477 org-agenda-default-appointment-duration)))))
6479 ;; Compute the duration
6480 (when s2
6481 (setq duration (- (org-hh:mm-string-to-minutes s2)
6482 (org-hh:mm-string-to-minutes s1)))))
6484 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6485 txt)
6486 ;; Tags are in the string
6487 (if (or (eq org-agenda-remove-tags t)
6488 (and org-agenda-remove-tags
6489 org-prefix-has-tag))
6490 (setq txt (replace-match "" t t txt))
6491 (setq txt (replace-match
6492 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6493 (match-string 2 txt))
6494 t t txt))))
6495 (when (derived-mode-p 'org-mode)
6496 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6498 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6499 ;; current buffer, so move this check outside of above
6500 (if effort
6501 (setq neffort (org-duration-string-to-minutes effort)
6502 effort (setq effort (concat "[" effort "]")))
6503 ;; prevent erroring out with %e format when there is no effort
6504 (setq effort ""))
6506 (when remove-re
6507 (while (string-match remove-re txt)
6508 (setq txt (replace-match "" t t txt))))
6510 ;; Set org-heading property on `txt' to mark the start of the
6511 ;; heading.
6512 (add-text-properties 0 (length txt) '(org-heading t) txt)
6514 ;; Prepare the variables needed in the eval of the compiled format
6515 (if org-prefix-has-breadcrumbs
6516 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6517 (let ((s (org-display-outline-path nil nil "->" t)))
6518 (if (eq "" s) "" (concat s "->"))))))
6519 (setq time (cond (s2 (concat
6520 (org-agenda-time-of-day-to-ampm-maybe s1)
6521 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6522 (if org-agenda-timegrid-use-ampm " ")))
6523 (s1 (concat
6524 (org-agenda-time-of-day-to-ampm-maybe s1)
6525 (if org-agenda-timegrid-use-ampm
6526 "........ "
6527 "......")))
6528 (t ""))
6529 extra (or (and (not habitp) extra) "")
6530 category (if (symbolp category) (symbol-name category) category)
6531 thecategory (copy-sequence category)
6532 level (or level ""))
6533 (if (string-match org-bracket-link-regexp category)
6534 (progn
6535 (setq l (if (match-end 3)
6536 (- (match-end 3) (match-beginning 3))
6537 (- (match-end 1) (match-beginning 1))))
6538 (when (< l (or org-prefix-category-length 0))
6539 (setq category (copy-sequence category))
6540 (org-add-props category nil
6541 'extra-space (make-string
6542 (- org-prefix-category-length l 1) ?\ ))))
6543 (if (and org-prefix-category-max-length
6544 (>= (length category) org-prefix-category-max-length))
6545 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6546 ;; Evaluate the compiled format
6547 (setq rtn (concat (eval formatter) txt))
6549 ;; And finally add the text properties
6550 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6551 (org-add-props rtn nil
6552 'org-category (if thecategory (downcase thecategory) category)
6553 'tags (mapcar 'org-downcase-keep-props tags)
6554 'org-highest-priority org-highest-priority
6555 'org-lowest-priority org-lowest-priority
6556 'time-of-day time-of-day
6557 'duration duration
6558 'effort effort
6559 'effort-minutes neffort
6560 'breadcrumbs breadcrumbs
6561 'txt txt
6562 'level level
6563 'time time
6564 'extra extra
6565 'format org-prefix-format-compiled
6566 'dotime dotime)))))
6568 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6569 "Remove tags string from TXT, and add a modified list of tags.
6570 The modified list may contain inherited tags, and tags matched by
6571 `org-agenda-hide-tags-regexp' will be removed."
6572 (when (or add-inherited hide-re)
6573 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6574 (setq txt (substring txt 0 (match-beginning 0))))
6575 (setq tags
6576 (delq nil
6577 (mapcar (lambda (tg)
6578 (if (or (and hide-re (string-match hide-re tg))
6579 (and (not add-inherited)
6580 (get-text-property 0 'inherited tg)))
6582 tg))
6583 tags)))
6584 (when tags
6585 (let ((have-i (get-text-property 0 'inherited (car tags)))
6587 (setq txt (concat txt " :"
6588 (mapconcat
6589 (lambda (x)
6590 (setq i (get-text-property 0 'inherited x))
6591 (if (and have-i (not i))
6592 (progn
6593 (setq have-i nil)
6594 (concat ":" x))
6596 tags ":")
6597 (if have-i "::" ":"))))))
6598 txt)
6600 (defun org-downcase-keep-props (s)
6601 (let ((props (text-properties-at 0 s)))
6602 (setq s (downcase s))
6603 (add-text-properties 0 (length s) props s)
6606 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6608 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6609 "Add a time-grid for agenda items which need it.
6611 LIST is the list of agenda items formatted by `org-agenda-list'.
6612 NDAYS is the span of the current agenda view.
6613 TODAYP is `t' when the current agenda view is on today."
6614 (catch 'exit
6615 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6616 ((and todayp (member 'today (car org-agenda-time-grid))))
6617 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6618 ((member 'weekly (car org-agenda-time-grid)))
6619 (t (throw 'exit list)))
6620 (let* ((have (delq nil (mapcar
6621 (lambda (x) (get-text-property 1 'time-of-day x))
6622 list)))
6623 (string (nth 1 org-agenda-time-grid))
6624 (gridtimes (nth 2 org-agenda-time-grid))
6625 (req (car org-agenda-time-grid))
6626 (remove (member 'remove-match req))
6627 new time)
6628 (if (and (member 'require-timed req) (not have))
6629 ;; don't show empty grid
6630 (throw 'exit list))
6631 (while (setq time (pop gridtimes))
6632 (unless (and remove (member time have))
6633 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6634 (push (org-agenda-format-item
6635 nil string nil "" nil
6636 (concat (substring time 0 -2) ":" (substring time -2)))
6637 new)
6638 (put-text-property
6639 2 (length (car new)) 'face 'org-time-grid (car new))))
6640 (when (and todayp org-agenda-show-current-time-in-grid)
6641 (push (org-agenda-format-item
6642 nil org-agenda-current-time-string nil "" nil
6643 (format-time-string "%H:%M "))
6644 new)
6645 (put-text-property
6646 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6648 (if (member 'time-up org-agenda-sorting-strategy-selected)
6649 (append new list)
6650 (append list new)))))
6652 (defun org-compile-prefix-format (key)
6653 "Compile the prefix format into a Lisp form that can be evaluated.
6654 The resulting form and associated variable bindings is returned
6655 and stored in the variable `org-prefix-format-compiled'."
6656 (setq org-prefix-has-time nil
6657 org-prefix-has-tag nil
6658 org-prefix-category-length nil
6659 org-prefix-has-effort nil
6660 org-prefix-has-breadcrumbs nil)
6661 (let ((s (cond
6662 ((stringp org-agenda-prefix-format)
6663 org-agenda-prefix-format)
6664 ((assq key org-agenda-prefix-format)
6665 (cdr (assq key org-agenda-prefix-format)))
6666 (t " %-12:c%?-12t% s")))
6667 (start 0)
6668 varform vars var e c f opt)
6669 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6670 s start)
6671 (setq var (or (cdr (assoc (match-string 4 s)
6672 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6673 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6674 'eval)
6675 c (or (match-string 3 s) "")
6676 opt (match-beginning 1)
6677 start (1+ (match-beginning 0)))
6678 (if (equal var 'time) (setq org-prefix-has-time t))
6679 (if (equal var 'tag) (setq org-prefix-has-tag t))
6680 (if (equal var 'effort) (setq org-prefix-has-effort t))
6681 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6682 (setq f (concat "%" (match-string 2 s) "s"))
6683 (when (equal var 'category)
6684 (setq org-prefix-category-length
6685 (floor (abs (string-to-number (match-string 2 s)))))
6686 (setq org-prefix-category-max-length
6687 (let ((x (match-string 2 s)))
6688 (save-match-data
6689 (if (string-match "\\.[0-9]+" x)
6690 (string-to-number (substring (match-string 0 x) 1)))))))
6691 (if (eq var 'eval)
6692 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6693 (if opt
6694 (setq varform
6695 `(if (equal "" ,var)
6697 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6698 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6699 (setq s (replace-match "%s" t nil s))
6700 (push varform vars))
6701 (setq vars (nreverse vars))
6702 (with-current-buffer (or org-agenda-buffer (current-buffer))
6703 (setq org-prefix-format-compiled
6704 (list
6705 `((org-prefix-has-time ,org-prefix-has-time)
6706 (org-prefix-has-tag ,org-prefix-has-tag)
6707 (org-prefix-category-length ,org-prefix-category-length)
6708 (org-prefix-has-effort ,org-prefix-has-effort)
6709 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6710 `(format ,s ,@vars))))))
6712 (defun org-set-sorting-strategy (key)
6713 (if (symbolp (car org-agenda-sorting-strategy))
6714 ;; the old format
6715 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6716 (setq org-agenda-sorting-strategy-selected
6717 (or (cdr (assq key org-agenda-sorting-strategy))
6718 (cdr (assq 'agenda org-agenda-sorting-strategy))
6719 '(time-up category-keep priority-down)))))
6721 (defun org-get-time-of-day (s &optional string mod24)
6722 "Check string S for a time of day.
6723 If found, return it as a military time number between 0 and 2400.
6724 If not found, return nil.
6725 The optional STRING argument forces conversion into a 5 character wide string
6726 HH:MM."
6727 (save-match-data
6728 (when
6729 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6730 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6731 (let* ((h (string-to-number (match-string 1 s)))
6732 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6733 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6734 (am-p (equal ampm "am"))
6735 (h1 (cond ((not ampm) h)
6736 ((= h 12) (if am-p 0 12))
6737 (t (+ h (if am-p 0 12)))))
6738 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6739 (mod h1 24) h1))
6740 (t0 (+ (* 100 h2) m))
6741 (t1 (concat (if (>= h1 24) "+" " ")
6742 (if (and org-agenda-time-leading-zero
6743 (< t0 1000)) "0" "")
6744 (if (< t0 100) "0" "")
6745 (if (< t0 10) "0" "")
6746 (int-to-string t0))))
6747 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6749 (defvar org-agenda-before-sorting-filter-function nil
6750 "Function to be applied to agenda items prior to sorting.
6751 Prior to sorting also means just before they are inserted into the agenda.
6753 To aid sorting, you may revisit the original entries and add more text
6754 properties which will later be used by the sorting functions.
6756 The function should take a string argument, an agenda line.
6757 It has access to the text properties in that line, which contain among
6758 other things, the property `org-hd-marker' that points to the entry
6759 where the line comes from. Note that not all lines going into the agenda
6760 have this property, only most.
6762 The function should return the modified string. It is probably best
6763 to ONLY change text properties.
6765 You can also use this function as a filter, by returning nil for lines
6766 you don't want to have in the agenda at all. For this application, you
6767 could bind the variable in the options section of a custom command.")
6769 (defun org-agenda-finalize-entries (list &optional type)
6770 "Sort, limit and concatenate the LIST of agenda items.
6771 The optional argument TYPE tells the agenda type."
6772 (let ((max-effort (cond ((listp org-agenda-max-effort)
6773 (cdr (assoc type org-agenda-max-effort)))
6774 (t org-agenda-max-effort)))
6775 (max-todo (cond ((listp org-agenda-max-todos)
6776 (cdr (assoc type org-agenda-max-todos)))
6777 (t org-agenda-max-todos)))
6778 (max-tags (cond ((listp org-agenda-max-tags)
6779 (cdr (assoc type org-agenda-max-tags)))
6780 (t org-agenda-max-tags)))
6781 (max-entries (cond ((listp org-agenda-max-entries)
6782 (cdr (assoc type org-agenda-max-entries)))
6783 (t org-agenda-max-entries))) l)
6784 (when org-agenda-before-sorting-filter-function
6785 (setq list
6786 (delq nil
6787 (mapcar
6788 org-agenda-before-sorting-filter-function list))))
6789 (setq list (mapcar 'org-agenda-highlight-todo list)
6790 list (mapcar 'identity (sort list 'org-entries-lessp))
6791 list (org-agenda-limit-entries
6792 list 'effort-minutes max-effort 'identity)
6793 list (org-agenda-limit-entries list 'todo-state max-todo)
6794 list (org-agenda-limit-entries list 'tags max-tags)
6795 list (org-agenda-limit-entries list 'org-hd-marker max-entries)
6796 list (mapconcat 'identity list "\n"))))
6798 (defun org-agenda-limit-entries (list prop limit &optional fn)
6799 "Limit the number of agenda entries."
6800 (if limit
6801 (let ((f (or fn (lambda (p) (and p 1)))) (lim 0))
6802 (delq nil
6803 (mapcar
6804 (lambda (e)
6805 (let ((pval (funcall f (get-text-property 1 prop e))))
6806 (if pval (setq lim (+ lim pval)))
6807 (if (or (not pval) (<= lim limit)) e)))
6808 list)))
6809 list))
6811 (defun org-agenda-highlight-todo (x)
6812 (let ((org-done-keywords org-done-keywords-for-agenda)
6813 (case-fold-search nil)
6815 (if (eq x 'line)
6816 (save-excursion
6817 (beginning-of-line 1)
6818 (setq re (org-get-at-bol 'org-todo-regexp))
6819 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6820 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6821 (add-text-properties (match-beginning 0) (match-end 1)
6822 (list 'face (org-get-todo-face 1)))
6823 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6824 (delete-region (match-beginning 1) (1- (match-end 0)))
6825 (goto-char (match-beginning 1))
6826 (insert (format org-agenda-todo-keyword-format s)))))
6827 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6828 (setq re (get-text-property 0 'org-todo-regexp x))
6829 (when (and re
6830 ;; Test `pl' because if there's no heading content,
6831 ;; there's no point matching to highlight. Note
6832 ;; that if we didn't test `pl' first, and there
6833 ;; happened to be no keyword from `org-todo-regexp'
6834 ;; on this heading line, then the `equal' comparison
6835 ;; afterwards would spuriously succeed in the case
6836 ;; where `pl' is nil -- causing an args-out-of-range
6837 ;; error when we try to add text properties to text
6838 ;; that isn't there.
6840 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6841 x pl) pl))
6842 (add-text-properties
6843 (or (match-end 1) (match-end 0)) (match-end 0)
6844 (list 'face (org-get-todo-face (match-string 2 x)))
6846 (when (match-end 1)
6847 (setq x (concat (substring x 0 (match-end 1))
6848 (format org-agenda-todo-keyword-format
6849 (match-string 2 x))
6850 (org-add-props " " (text-properties-at 0 x))
6851 (substring x (match-end 3)))))))
6852 x)))
6854 (defsubst org-cmp-priority (a b)
6855 "Compare the priorities of string A and B."
6856 (let ((pa (or (get-text-property 1 'priority a) 0))
6857 (pb (or (get-text-property 1 'priority b) 0)))
6858 (cond ((> pa pb) +1)
6859 ((< pa pb) -1))))
6861 (defsubst org-cmp-effort (a b)
6862 "Compare the effort values of string A and B."
6863 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6864 (ea (or (get-text-property 1 'effort-minutes a) def))
6865 (eb (or (get-text-property 1 'effort-minutes b) def)))
6866 (cond ((> ea eb) +1)
6867 ((< ea eb) -1))))
6869 (defsubst org-cmp-category (a b)
6870 "Compare the string values of categories of strings A and B."
6871 (let ((ca (or (get-text-property 1 'org-category a) ""))
6872 (cb (or (get-text-property 1 'org-category b) "")))
6873 (cond ((string-lessp ca cb) -1)
6874 ((string-lessp cb ca) +1))))
6876 (defsubst org-cmp-todo-state (a b)
6877 "Compare the todo states of strings A and B."
6878 (let* ((ma (or (get-text-property 1 'org-marker a)
6879 (get-text-property 1 'org-hd-marker a)))
6880 (mb (or (get-text-property 1 'org-marker b)
6881 (get-text-property 1 'org-hd-marker b)))
6882 (fa (and ma (marker-buffer ma)))
6883 (fb (and mb (marker-buffer mb)))
6884 (todo-kwds
6885 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6886 (and fb (with-current-buffer fb org-todo-keywords-1))))
6887 (ta (or (get-text-property 1 'todo-state a) ""))
6888 (tb (or (get-text-property 1 'todo-state b) ""))
6889 (la (- (length (member ta todo-kwds))))
6890 (lb (- (length (member tb todo-kwds))))
6891 (donepa (member ta org-done-keywords-for-agenda))
6892 (donepb (member tb org-done-keywords-for-agenda)))
6893 (cond ((and donepa (not donepb)) -1)
6894 ((and (not donepa) donepb) +1)
6895 ((< la lb) -1)
6896 ((< lb la) +1))))
6898 (defsubst org-cmp-alpha (a b)
6899 "Compare the headlines, alphabetically."
6900 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6901 (plb (text-property-any 0 (length b) 'org-heading t b))
6902 (ta (and pla (substring a pla)))
6903 (tb (and plb (substring b plb))))
6904 (when pla
6905 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6906 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6907 (setq ta (substring ta (match-end 0))))
6908 (setq ta (downcase ta)))
6909 (when plb
6910 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6911 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6912 (setq tb (substring tb (match-end 0))))
6913 (setq tb (downcase tb)))
6914 (cond ((not ta) +1)
6915 ((not tb) -1)
6916 ((string-lessp ta tb) -1)
6917 ((string-lessp tb ta) +1))))
6919 (defsubst org-cmp-tag (a b)
6920 "Compare the string values of the first tags of A and B."
6921 (let ((ta (car (last (get-text-property 1 'tags a))))
6922 (tb (car (last (get-text-property 1 'tags b)))))
6923 (cond ((not ta) +1)
6924 ((not tb) -1)
6925 ((string-lessp ta tb) -1)
6926 ((string-lessp tb ta) +1))))
6928 (defsubst org-cmp-time (a b)
6929 "Compare the time-of-day values of strings A and B."
6930 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6931 (ta (or (get-text-property 1 'time-of-day a) def))
6932 (tb (or (get-text-property 1 'time-of-day b) def)))
6933 (cond ((< ta tb) -1)
6934 ((< tb ta) +1))))
6936 (defsubst org-cmp-ts (a b &optional type)
6937 "Compare the timestamps values of entries A and B.
6938 When TYPE is \"scheduled\", \"deadline\", \"timestamp\"
6939 or \"timestamp_ia\", compare within each of these type.
6940 When TYPE is the empty string, compare all timestamps
6941 without respect of their type."
6942 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6943 (ta (or (and (string-match type (get-text-property 1 'type a))
6944 (get-text-property 1 'ts-date a)) def))
6945 (tb (or (and (string-match type (get-text-property 1 'type b))
6946 (get-text-property 1 'ts-date b)) def)))
6947 (cond ((< ta tb) -1)
6948 ((< tb ta) +1))))
6950 (defsubst org-cmp-habit-p (a b)
6951 "Compare the todo states of strings A and B."
6952 (let ((ha (get-text-property 1 'org-habit-p a))
6953 (hb (get-text-property 1 'org-habit-p b)))
6954 (cond ((and ha (not hb)) -1)
6955 ((and (not ha) hb) +1))))
6957 (defun org-entries-lessp (a b)
6958 "Predicate for sorting agenda entries."
6959 ;; The following variables will be used when the form is evaluated.
6960 ;; So even though the compiler complains, keep them.
6961 (let* ((ss org-agenda-sorting-strategy-selected)
6962 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
6963 (org-cmp-ts a b "")))
6964 (timestamp-down (if timestamp-up (- timestamp-up) nil))
6965 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
6966 (org-cmp-ts a b "scheduled")))
6967 (scheduled-down (if scheduled-up (- scheduled-up) nil))
6968 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
6969 (org-cmp-ts a b "deadline")))
6970 (deadline-down (if deadline-up (- deadline-up) nil))
6971 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
6972 (org-cmp-ts a b "iatimestamp_ia")))
6973 (tsia-down (if tsia-up (- tsia-up) nil))
6974 (ts-up (and (org-em 'ts-up 'ts-down ss)
6975 (org-cmp-ts a b "timestamp")))
6976 (ts-down (if ts-up (- ts-up) nil))
6977 (time-up (and (org-em 'time-up 'time-down ss)
6978 (org-cmp-time a b)))
6979 (time-down (if time-up (- time-up) nil))
6980 (priority-up (and (org-em 'priority-up 'priority-down ss)
6981 (org-cmp-priority a b)))
6982 (priority-down (if priority-up (- priority-up) nil))
6983 (effort-up (and (org-em 'effort-up 'effort-down ss)
6984 (org-cmp-effort a b)))
6985 (effort-down (if effort-up (- effort-up) nil))
6986 (category-up (and (or (org-em 'category-up 'category-down ss)
6987 (memq 'category-keep ss))
6988 (org-cmp-category a b)))
6989 (category-down (if category-up (- category-up) nil))
6990 (category-keep (if category-up +1 nil))
6991 (tag-up (and (org-em 'tag-up 'tag-down ss)
6992 (org-cmp-tag a b)))
6993 (tag-down (if tag-up (- tag-up) nil))
6994 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6995 (org-cmp-todo-state a b)))
6996 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6997 (habit-up (and (org-em 'habit-up 'habit-down ss)
6998 (org-cmp-habit-p a b)))
6999 (habit-down (if habit-up (- habit-up) nil))
7000 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7001 (org-cmp-alpha a b)))
7002 (alpha-down (if alpha-up (- alpha-up) nil))
7003 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7004 user-defined-up user-defined-down)
7005 (if (and need-user-cmp org-agenda-cmp-user-defined
7006 (functionp org-agenda-cmp-user-defined))
7007 (setq user-defined-up
7008 (funcall org-agenda-cmp-user-defined a b)
7009 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7010 (cdr (assoc
7011 (eval (cons 'or org-agenda-sorting-strategy-selected))
7012 '((-1 . t) (1 . nil) (nil . nil))))))
7014 ;;; Agenda restriction lock
7016 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7017 "Overlay to mark the headline to which agenda commands are restricted.")
7018 (overlay-put org-agenda-restriction-lock-overlay
7019 'face 'org-agenda-restriction-lock)
7020 (overlay-put org-agenda-restriction-lock-overlay
7021 'help-echo "Agendas are currently limited to this subtree.")
7022 (org-detach-overlay org-agenda-restriction-lock-overlay)
7024 (defun org-agenda-set-restriction-lock (&optional type)
7025 "Set restriction lock for agenda, to current subtree or file.
7026 Restriction will be the file if TYPE is `file', or if type is the
7027 universal prefix '(4), or if the cursor is before the first headline
7028 in the file. Otherwise, restriction will be to the current subtree."
7029 (interactive "P")
7030 (and (equal type '(4)) (setq type 'file))
7031 (setq type (cond
7032 (type type)
7033 ((org-at-heading-p) 'subtree)
7034 ((condition-case nil (org-back-to-heading t) (error nil))
7035 'subtree)
7036 (t 'file)))
7037 (if (eq type 'subtree)
7038 (progn
7039 (setq org-agenda-restrict t)
7040 (setq org-agenda-overriding-restriction 'subtree)
7041 (put 'org-agenda-files 'org-restrict
7042 (list (buffer-file-name (buffer-base-buffer))))
7043 (org-back-to-heading t)
7044 (move-overlay org-agenda-restriction-lock-overlay
7045 (point) (save-excursion (org-end-of-subtree t t) (point)))
7046 (move-marker org-agenda-restrict-begin (point))
7047 (move-marker org-agenda-restrict-end
7048 (save-excursion (org-end-of-subtree t t)))
7049 (message "Locking agenda restriction to subtree"))
7050 (put 'org-agenda-files 'org-restrict
7051 (list (buffer-file-name (buffer-base-buffer))))
7052 (setq org-agenda-restrict nil)
7053 (setq org-agenda-overriding-restriction 'file)
7054 (move-marker org-agenda-restrict-begin nil)
7055 (move-marker org-agenda-restrict-end nil)
7056 (message "Locking agenda restriction to file"))
7057 (setq current-prefix-arg nil)
7058 (org-agenda-maybe-redo))
7060 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7061 "Remove the agenda restriction lock."
7062 (interactive "P")
7063 (org-detach-overlay org-agenda-restriction-lock-overlay)
7064 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7065 (setq org-agenda-overriding-restriction nil)
7066 (setq org-agenda-restrict nil)
7067 (put 'org-agenda-files 'org-restrict nil)
7068 (move-marker org-agenda-restrict-begin nil)
7069 (move-marker org-agenda-restrict-end nil)
7070 (setq current-prefix-arg nil)
7071 (message "Agenda restriction lock removed")
7072 (or noupdate (org-agenda-maybe-redo)))
7074 (defun org-agenda-maybe-redo ()
7075 "If there is any window showing the agenda view, update it."
7076 (let ((w (get-buffer-window org-agenda-buffer-name t))
7077 (w0 (selected-window)))
7078 (when w
7079 (select-window w)
7080 (org-agenda-redo)
7081 (select-window w0)
7082 (if org-agenda-overriding-restriction
7083 (message "Agenda view shifted to new %s restriction"
7084 org-agenda-overriding-restriction)
7085 (message "Agenda restriction lock removed")))))
7087 ;;; Agenda commands
7089 (defun org-agenda-check-type (error &rest types)
7090 "Check if agenda buffer is of allowed type.
7091 If ERROR is non-nil, throw an error, otherwise just return nil.
7092 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7093 (if (not org-agenda-type)
7094 (error "No Org agenda currently displayed")
7095 (if (memq org-agenda-type types)
7097 (if error
7098 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7099 nil))))
7101 (defun org-agenda-Quit ()
7102 "Exit the agenda and kill buffers loaded by `org-agenda'.
7103 Also restore the window configuration."
7104 (interactive)
7105 (if org-agenda-columns-active
7106 (org-columns-quit)
7107 (let ((buf (current-buffer)))
7108 (if (eq org-agenda-window-setup 'other-frame)
7109 (progn
7110 (org-agenda-reset-markers)
7111 (kill-buffer buf)
7112 (org-columns-remove-overlays)
7113 (setq org-agenda-archives-mode nil)
7114 (delete-frame))
7115 (and (not (eq org-agenda-window-setup 'current-window))
7116 (not (one-window-p))
7117 (delete-window))
7118 (org-agenda-reset-markers)
7119 (kill-buffer buf)
7120 (org-columns-remove-overlays)
7121 (setq org-agenda-archives-mode nil)))
7122 (setq org-agenda-buffer nil)
7123 ;; Maybe restore the pre-agenda window configuration.
7124 (and org-agenda-restore-windows-after-quit
7125 (not (eq org-agenda-window-setup 'other-frame))
7126 org-agenda-pre-window-conf
7127 (set-window-configuration org-agenda-pre-window-conf)
7128 (setq org-agenda-pre-window-conf nil))))
7130 (defun org-agenda-quit ()
7131 "Exit the agenda and restore the window configuration.
7132 When `org-agenda-sticky' is non-nil, only bury the agenda."
7133 (interactive)
7134 (if (and (eq org-indirect-buffer-display 'other-window)
7135 org-last-indirect-buffer)
7136 (let ((org-last-indirect-window
7137 (get-buffer-window org-last-indirect-buffer)))
7138 (if org-last-indirect-window
7139 (delete-window org-last-indirect-window))))
7140 (if org-agenda-columns-active
7141 (org-columns-quit)
7142 (if org-agenda-sticky
7143 (let ((buf (current-buffer)))
7144 (if (eq org-agenda-window-setup 'other-frame)
7145 (progn
7146 (delete-frame))
7147 (and (not (eq org-agenda-window-setup 'current-window))
7148 (not (one-window-p))
7149 (delete-window)))
7150 (with-current-buffer buf
7151 (bury-buffer)
7152 ;; Maybe restore the pre-agenda window configuration.
7153 (and org-agenda-restore-windows-after-quit
7154 (not (eq org-agenda-window-setup 'other-frame))
7155 org-agenda-pre-window-conf
7156 (set-window-configuration org-agenda-pre-window-conf)
7157 (setq org-agenda-pre-window-conf nil))))
7158 (org-agenda-Quit))))
7160 (defun org-agenda-exit ()
7161 "Exit the agenda and restore the window configuration.
7162 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7163 buffers visited directly by the user will not be touched."
7164 (interactive)
7165 (org-release-buffers org-agenda-new-buffers)
7166 (setq org-agenda-new-buffers nil)
7167 (org-agenda-Quit))
7169 (defun org-agenda-kill-all-agenda-buffers ()
7170 "Kill all buffers in `org-agenda-mode'.
7171 This is used when toggling sticky agendas.
7172 You can also explicitly invoke it with `C-c a C-k'."
7173 (interactive)
7174 (let (blist)
7175 (dolist (buf (buffer-list))
7176 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7177 (push buf blist)))
7178 (mapc 'kill-buffer blist)))
7180 (defun org-agenda-execute (arg)
7181 "Execute another agenda command, keeping same window.
7182 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7183 in the agenda."
7184 (interactive "P")
7185 (let ((org-agenda-window-setup 'current-window))
7186 (org-agenda arg)))
7188 (defun org-agenda-redo (&optional all)
7189 "Rebuild possibly ALL agenda view(s) in the current buffer."
7190 (interactive "P")
7191 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7192 (cpa (unless (eq all t) current-prefix-arg))
7193 (org-agenda-doing-sticky-redo org-agenda-sticky)
7194 (org-agenda-sticky nil)
7195 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7196 org-agenda-buffer-name))
7197 (org-agenda-keep-modes t)
7198 (tag-filter org-agenda-tag-filter)
7199 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7200 (top-cat-filter org-agenda-top-category-filter)
7201 (cat-filter org-agenda-category-filter)
7202 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7203 (re-filter org-agenda-regexp-filter)
7204 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7205 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7206 (cols org-agenda-columns-active)
7207 (line (org-current-line))
7208 (window-line (- line (org-current-line (window-start))))
7209 (lprops (get 'org-agenda-redo-command 'org-lprops))
7210 (redo-cmd (get-text-property p 'org-redo-cmd))
7211 (last-args (get-text-property p 'org-last-args))
7212 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7213 (org-agenda-overriding-cmd-arguments
7214 (unless (eq all t)
7215 (cond ((listp last-args)
7216 (cons (or cpa (car last-args)) (cdr last-args)))
7217 ((stringp last-args)
7218 last-args))))
7219 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7220 (put 'org-agenda-tag-filter :preset-filter nil)
7221 (put 'org-agenda-category-filter :preset-filter nil)
7222 (put 'org-agenda-regexp-filter :preset-filter nil)
7223 (and cols (org-columns-quit))
7224 (message "Rebuilding agenda buffer...")
7225 (if series-redo-cmd
7226 (eval series-redo-cmd)
7227 (org-let lprops '(eval redo-cmd)))
7228 (setq org-agenda-undo-list nil
7229 org-agenda-pending-undo-list nil
7230 org-agenda-tag-filter tag-filter
7231 org-agenda-category-filter cat-filter
7232 org-agenda-regexp-filter re-filter
7233 org-agenda-top-category-filter top-cat-filter)
7234 (message "Rebuilding agenda buffer...done")
7235 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7236 (put 'org-agenda-category-filter :preset-filter cat-preset)
7237 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7238 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7239 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7240 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7241 (and top-cat-filter (org-agenda-filter-top-category-apply top-cat-filter))
7242 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7243 (org-goto-line line)
7244 (recenter window-line)))
7246 (defvar org-global-tags-completion-table nil)
7247 (defvar org-agenda-filter-form nil)
7248 (defvar org-agenda-filtered-by-category nil)
7250 (defun org-agenda-filter-by-category (strip)
7251 "Keep only those lines in the agenda buffer that have a specific category.
7252 The category is that of the current line."
7253 (interactive "P")
7254 (if (and org-agenda-filtered-by-category
7255 org-agenda-category-filter)
7256 (org-agenda-filter-show-all-cat)
7257 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7258 (if (and cat (not (string= "" cat)))
7259 (org-agenda-filter-apply
7260 (setq org-agenda-category-filter
7261 (list (concat (if strip "-" "+") cat)))
7262 'category)
7263 (error "No category at point")))))
7265 (defun org-find-top-category (&optional pos)
7266 (save-excursion
7267 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7268 (if pos (goto-char pos))
7269 ;; Skip up to the topmost parent
7270 (while (ignore-errors (outline-up-heading 1) t))
7271 (ignore-errors
7272 (nth 4 (org-heading-components))))))
7274 (defvar org-agenda-filtered-by-top-category nil)
7275 (defun org-agenda-filter-by-top-category (strip)
7276 "Keep only those lines in the agenda buffer that have a specific category.
7277 The category is that of the current line."
7278 (interactive "P")
7279 (if org-agenda-filtered-by-top-category
7280 (progn
7281 (setq org-agenda-filtered-by-top-category nil
7282 org-agenda-top-category-filter nil)
7283 (org-agenda-filter-show-all-cat))
7284 (let ((cat (org-find-top-category (org-get-at-bol 'org-hd-marker))))
7285 (if cat (org-agenda-filter-top-category-apply cat strip)
7286 (error "No top-level category at point")))))
7288 (defvar org-agenda-regexp-filter nil)
7289 (defun org-agenda-filter-by-regexp (strip)
7290 "Filter agenda entries by a regular expression.
7291 Regexp filters are cumulative.
7292 With no prefix argument, keep entries matching the regexp.
7293 With one prefix argument, filter out entries matching the regexp.
7294 With two prefix arguments, remove the regexp filters."
7295 (interactive "P")
7296 (if (not (equal strip '(16)))
7297 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7298 (read-from-minibuffer
7299 (if (equal strip '(4))
7300 "Filter out entries matching regexp: "
7301 "Narrow to entries matching regexp: ")))))
7302 (push flt org-agenda-regexp-filter)
7303 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7304 (org-agenda-filter-show-all-re)
7305 (message "Regexp filter removed")))
7307 (defun org-agenda-filter-remove-all ()
7308 "Remove all filters from the current agenda buffer."
7309 (interactive)
7310 (when org-agenda-tag-filter
7311 (org-agenda-filter-show-all-tag))
7312 (when org-agenda-category-filter
7313 (org-agenda-filter-show-all-cat))
7314 (when org-agenda-regexp-filter
7315 (org-agenda-filter-show-all-re)))
7317 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7318 "Keep only those lines in the agenda buffer that have a specific tag.
7319 The tag is selected with its fast selection letter, as configured.
7320 With prefix argument STRIP, remove all lines that do have the tag.
7321 A lisp caller can specify CHAR. NARROW means that the new tag should be
7322 used to narrow the search - the interactive user can also press `-' or `+'
7323 to switch to narrowing."
7324 (interactive "P")
7325 (let* ((alist org-tag-alist-for-agenda)
7326 (tag-chars (mapconcat
7327 (lambda (x) (if (and (not (symbolp (car x)))
7328 (cdr x))
7329 (char-to-string (cdr x))
7330 ""))
7331 alist ""))
7332 (efforts (org-split-string
7333 (or (cdr (assoc (concat org-effort-property "_ALL")
7334 org-global-properties))
7335 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7336 "")))
7337 (effort-op org-agenda-filter-effort-default-operator)
7338 (effort-prompt "")
7339 (inhibit-read-only t)
7340 (current org-agenda-tag-filter)
7341 maybe-refresh a n tag)
7342 (unless char
7343 (message
7344 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7345 (if narrow "Narrow" "Filter") tag-chars
7346 (if org-agenda-auto-exclude-function "[RET], " ""))
7347 (setq char (read-char-exclusive)))
7348 (when (member char '(?+ ?-))
7349 ;; Narrowing down
7350 (cond ((equal char ?-) (setq strip t narrow t))
7351 ((equal char ?+) (setq strip nil narrow t)))
7352 (message
7353 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7354 (setq char (read-char-exclusive)))
7355 (when (member char '(?< ?> ?= ??))
7356 ;; An effort operator
7357 (setq effort-op (char-to-string char))
7358 (setq alist nil) ; to make sure it will be interpreted as effort.
7359 (unless (equal char ??)
7360 (loop for i from 0 to 9 do
7361 (setq effort-prompt
7362 (concat
7363 effort-prompt " ["
7364 (if (= i 9) "0" (int-to-string (1+ i)))
7365 "]" (nth i efforts))))
7366 (message "Effort%s: %s " effort-op effort-prompt)
7367 (setq char (read-char-exclusive))
7368 (when (or (< char ?0) (> char ?9))
7369 (error "Need 1-9,0 to select effort"))))
7370 (when (equal char ?\t)
7371 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7372 (org-set-local 'org-global-tags-completion-table
7373 (org-global-tags-completion-table)))
7374 (let ((completion-ignore-case t))
7375 (setq tag (org-icompleting-read
7376 "Tag: " org-global-tags-completion-table))))
7377 (cond
7378 ((equal char ?\r)
7379 (org-agenda-filter-show-all-tag)
7380 (when org-agenda-auto-exclude-function
7381 (setq org-agenda-tag-filter nil)
7382 (dolist (tag (org-agenda-get-represented-tags))
7383 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7384 (if modifier
7385 (push modifier org-agenda-tag-filter))))
7386 (if (not (null org-agenda-tag-filter))
7387 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7388 (setq maybe-refresh t))
7389 ((equal char ?/)
7390 (org-agenda-filter-show-all-tag)
7391 (when (get 'org-agenda-tag-filter :preset-filter)
7392 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7393 (setq maybe-refresh t))
7394 ((equal char ?. )
7395 (setq org-agenda-tag-filter
7396 (mapcar (lambda(tag) (concat "+" tag))
7397 (org-get-at-bol 'tags)))
7398 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7399 (setq maybe-refresh t))
7400 ((or (equal char ?\ )
7401 (setq a (rassoc char alist))
7402 (and (>= char ?0) (<= char ?9)
7403 (setq n (if (= char ?0) 9 (- char ?0 1))
7404 tag (concat effort-op (nth n efforts))
7405 a (cons tag nil)))
7406 (and (= char ??)
7407 (setq tag "?eff")
7408 a (cons tag nil))
7409 (and tag (setq a (cons tag nil))))
7410 (org-agenda-filter-show-all-tag)
7411 (setq tag (car a))
7412 (setq org-agenda-tag-filter
7413 (cons (concat (if strip "-" "+") tag)
7414 (if narrow current nil)))
7415 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7416 (setq maybe-refresh t))
7417 (t (error "Invalid tag selection character %c" char)))
7418 (when (and maybe-refresh
7419 (eq org-agenda-clockreport-mode 'with-filter))
7420 (org-agenda-redo))))
7422 (defun org-agenda-get-represented-tags ()
7423 "Get a list of all tags currently represented in the agenda."
7424 (let (p tags)
7425 (save-excursion
7426 (goto-char (point-min))
7427 (while (setq p (next-single-property-change (point) 'tags))
7428 (goto-char p)
7429 (mapc (lambda (x) (add-to-list 'tags x))
7430 (get-text-property (point) 'tags))))
7431 tags))
7433 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7434 "Refine the current filter. See `org-agenda-filter-by-tag'."
7435 (interactive "P")
7436 (org-agenda-filter-by-tag strip char 'refine))
7438 (defun org-agenda-filter-make-matcher (filter type)
7439 "Create the form that tests a line for agenda filter."
7440 (let (f f1)
7441 (cond
7442 ;; Tag filter
7443 ((eq type 'tag)
7444 (setq filter
7445 (delete-dups
7446 (append (get 'org-agenda-tag-filter :preset-filter)
7447 filter)))
7448 (dolist (x filter)
7449 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7450 (ffunc
7451 (lambda (nf0 nf01 fltr notgroup op)
7452 (dolist (x fltr)
7453 (if (member x '("-" "+"))
7454 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7455 (if (string-match "[<=>?]" x)
7456 (setq nf01 (org-agenda-filter-effort-form x))
7457 (setq nf01 (list 'member (downcase (substring x 1))
7458 'tags)))
7459 (when (equal (string-to-char x) ?-)
7460 (setq nf01 (list 'not nf01))
7461 (when (not notgroup) (setq op 'and))))
7462 (push nf01 nf0))
7463 (if notgroup
7464 (push (cons 'and nf0) f)
7465 (push (cons (or op 'or) nf0) f)))))
7466 (if (equal nfilter filter)
7467 (funcall ffunc f1 f filter t nil)
7468 (funcall ffunc nf1 nf nfilter nil nil)))))
7469 ;; Category filter
7470 ((eq type 'category)
7471 (setq filter
7472 (delete-dups
7473 (append (get 'org-agenda-category-filter :preset-filter)
7474 filter)))
7475 (dolist (x filter)
7476 (if (equal "-" (substring x 0 1))
7477 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7478 (setq f1 (list 'equal (substring x 1) 'cat)))
7479 (push f1 f)))
7480 ;; Regexp filter
7481 ((eq type 'regexp)
7482 (setq filter
7483 (delete-dups
7484 (append (get 'org-agenda-regexp-filter :preset-filter)
7485 filter)))
7486 (dolist (x filter)
7487 (if (equal "-" (substring x 0 1))
7488 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7489 (setq f1 (list 'string-match (substring x 1) 'txt)))
7490 (push f1 f))))
7491 (cons 'and (nreverse f))))
7493 (defun org-agenda-filter-effort-form (e)
7494 "Return the form to compare the effort of the current line with what E says.
7495 E looks like \"+<2:25\"."
7496 (let (op)
7497 (setq e (substring e 1))
7498 (setq op (string-to-char e) e (substring e 1))
7499 (setq op (cond ((equal op ?<) '<=)
7500 ((equal op ?>) '>=)
7501 ((equal op ??) op)
7502 (t '=)))
7503 (list 'org-agenda-compare-effort (list 'quote op)
7504 (org-duration-string-to-minutes e))))
7506 (defun org-agenda-compare-effort (op value)
7507 "Compare the effort of the current line with VALUE, using OP.
7508 If the line does not have an effort defined, return nil."
7509 (let ((eff (org-get-at-bol 'effort-minutes)))
7510 (if (equal op ??)
7511 (not eff)
7512 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7513 value))))
7515 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7516 "Expand group tags in FILTER for the agenda.
7517 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7518 (if org-group-tags
7519 (let ((case-fold-search t) rtn)
7520 (mapc
7521 (lambda (f)
7522 (let (f0 dir)
7523 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7524 (setq dir (match-string 1 f) f0 (match-string 2 f))
7525 (setq dir (if no-operator "" "+") f0 f))
7526 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7527 (org-tags-expand f0 t t))
7528 rtn))))
7529 filter)
7530 (reverse rtn))
7531 filter))
7533 (defun org-agenda-filter-apply (filter type)
7534 "Set FILTER as the new agenda filter and apply it."
7535 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7536 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7537 (let (tags cat txt)
7538 (setq org-agenda-filter-form
7539 (org-agenda-filter-make-matcher filter type))
7540 (if (and (eq type 'category)
7541 (not (equal (substring (car filter) 0 1) "-")))
7542 ;; Only set `org-agenda-filtered-by-category' to t
7543 ;; when a unique category is used as the filter
7544 (setq org-agenda-filtered-by-category t))
7545 (org-agenda-set-mode-name)
7546 (save-excursion
7547 (goto-char (point-min))
7548 (while (not (eobp))
7549 (if (org-get-at-bol 'org-marker)
7550 (progn
7551 (setq tags ; used in eval
7552 (apply 'append
7553 (mapcar (lambda (f)
7554 (org-agenda-filter-expand-tags (list f) t))
7555 (org-get-at-bol 'tags)))
7556 cat (get-text-property (point) 'org-category)
7557 txt (get-text-property (point) 'txt))
7558 (if (not (eval org-agenda-filter-form))
7559 (org-agenda-filter-hide-line type))
7560 (beginning-of-line 2))
7561 (beginning-of-line 2))))
7562 (if (get-char-property (point) 'invisible)
7563 (ignore-errors (org-agenda-previous-line)))))
7565 (defun org-agenda-filter-top-category-apply (category &optional negative)
7566 "Set FILTER as the new agenda filter and apply it."
7567 (org-agenda-set-mode-name)
7568 (save-excursion
7569 (goto-char (point-min))
7570 (while (not (eobp))
7571 (let* ((pos (org-get-at-bol 'org-hd-marker))
7572 (topcat (and pos (org-find-top-category pos))))
7573 (if (and topcat (funcall (if negative 'identity 'not)
7574 (string= category topcat)))
7575 (org-agenda-filter-hide-line 'category)))
7576 (beginning-of-line 2)))
7577 (if (get-char-property (point) 'invisible)
7578 (org-agenda-previous-line))
7579 (setq org-agenda-top-category-filter category
7580 org-agenda-filtered-by-top-category t))
7582 (defun org-agenda-filter-hide-line (type)
7583 "Hide lines with TYPE in the agenda buffer."
7584 (let (ov)
7585 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
7586 (point-at-eol)))
7587 (overlay-put ov 'invisible t)
7588 (overlay-put ov 'type type)
7589 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7590 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7591 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7593 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7594 (setq pos (or pos (point)))
7595 (save-excursion
7596 (dolist (ov (overlays-at pos))
7597 (when (and (overlay-get ov 'invisible)
7598 (eq (overlay-get ov 'type) 'tag))
7599 (goto-char pos)
7600 (if (< (overlay-start ov) (point-at-eol))
7601 (move-overlay ov (point-at-eol)
7602 (overlay-end ov)))))))
7604 (defun org-agenda-filter-show-all-tag nil
7605 "Remove tag filter overlays from the agenda buffer."
7606 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7607 (setq org-agenda-tag-filter-overlays nil
7608 org-agenda-tag-filter nil
7609 org-agenda-filter-form nil)
7610 (org-agenda-set-mode-name))
7612 (defun org-agenda-filter-show-all-re nil
7613 "Remove regexp filter overlays from the agenda buffer."
7614 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7615 (setq org-agenda-re-filter-overlays nil
7616 org-agenda-regexp-filter nil
7617 org-agenda-filter-form nil)
7618 (org-agenda-set-mode-name))
7620 (defun org-agenda-filter-show-all-cat nil
7621 "Remove category filter overlays from the agenda buffer."
7622 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7623 (setq org-agenda-cat-filter-overlays nil
7624 org-agenda-filtered-by-category nil
7625 org-agenda-category-filter nil
7626 org-agenda-filter-form nil)
7627 (org-agenda-set-mode-name))
7629 (defun org-agenda-manipulate-query-add ()
7630 "Manipulate the query by adding a search term with positive selection.
7631 Positive selection means the term must be matched for selection of an entry."
7632 (interactive)
7633 (org-agenda-manipulate-query ?\[))
7634 (defun org-agenda-manipulate-query-subtract ()
7635 "Manipulate the query by adding a search term with negative selection.
7636 Negative selection means term must not be matched for selection of an entry."
7637 (interactive)
7638 (org-agenda-manipulate-query ?\]))
7639 (defun org-agenda-manipulate-query-add-re ()
7640 "Manipulate the query by adding a search regexp with positive selection.
7641 Positive selection means the regexp must match for selection of an entry."
7642 (interactive)
7643 (org-agenda-manipulate-query ?\{))
7644 (defun org-agenda-manipulate-query-subtract-re ()
7645 "Manipulate the query by adding a search regexp with negative selection.
7646 Negative selection means regexp must not match for selection of an entry."
7647 (interactive)
7648 (org-agenda-manipulate-query ?\}))
7649 (defun org-agenda-manipulate-query (char)
7650 (cond
7651 ((memq org-agenda-type '(timeline agenda))
7652 (let ((org-agenda-include-inactive-timestamps t))
7653 (org-agenda-redo))
7654 (message "Display now includes inactive timestamps as well"))
7655 ((eq org-agenda-type 'search)
7656 (org-add-to-string
7657 'org-agenda-query-string
7658 (if org-agenda-last-search-view-search-was-boolean
7659 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7660 (?\{ . " +{}") (?\} . " -{}"))))
7661 " "))
7662 (setq org-agenda-redo-command
7663 (list 'org-search-view
7664 (car (get-text-property (min (1- (point-max)) (point))
7665 'org-last-args))
7666 org-agenda-query-string
7667 (+ (length org-agenda-query-string)
7668 (if (member char '(?\{ ?\})) 0 1))))
7669 (set-register org-agenda-query-register org-agenda-query-string)
7670 (let ((org-agenda-overriding-arguments
7671 (cdr org-agenda-redo-command)))
7672 (org-agenda-redo)))
7673 (t (error "Cannot manipulate query for %s-type agenda buffers"
7674 org-agenda-type))))
7676 (defun org-add-to-string (var string)
7677 (set var (concat (symbol-value var) string)))
7679 (defun org-agenda-goto-date (span)
7680 "Jump to DATE in agenda."
7681 (interactive "P")
7682 (let* ((org-read-date-prefer-future
7683 (eval org-agenda-jump-prefer-future))
7684 (date (org-read-date))
7685 (org-agenda-sticky-orig org-agenda-sticky)
7686 (org-agenda-buffer-tmp-name (buffer-name))
7687 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7688 (0-arg (or current-prefix-arg (car args)))
7689 (2-arg (nth 2 args))
7690 (newcmd (list 'org-agenda-list 0-arg date
7691 (org-agenda-span-to-ndays 2-arg)))
7692 (newargs (cdr newcmd))
7693 (inhibit-read-only t)
7694 org-agenda-sticky)
7695 (if (not (org-agenda-check-type t 'agenda))
7696 (error "Not available in non-agenda blocks")
7697 (add-text-properties (point-min) (point-max)
7698 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7699 (org-agenda-redo)
7700 (setq org-agenda-sticky org-agenda-sticky-orig
7701 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7703 (defun org-agenda-goto-today ()
7704 "Go to today."
7705 (interactive)
7706 (org-agenda-check-type t 'timeline 'agenda)
7707 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7708 (curspan (nth 2 args))
7709 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7710 (cond
7711 (tdpos (goto-char tdpos))
7712 ((eq org-agenda-type 'agenda)
7713 (let* ((sd (org-agenda-compute-starting-span
7714 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7715 (org-agenda-overriding-arguments args))
7716 (setf (nth 1 org-agenda-overriding-arguments) sd)
7717 (org-agenda-redo)
7718 (org-agenda-find-same-or-today-or-agenda)))
7719 (t (error "Cannot find today")))))
7721 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7722 (goto-char
7723 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7724 (text-property-any (point-min) (point-max) 'org-today t)
7725 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7726 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7727 (org-agenda-goto-block-beginning))
7728 (point-min))))
7730 (defun org-agenda-goto-block-beginning ()
7731 "Go the agenda block beginning."
7732 (interactive)
7733 (if (not (derived-mode-p 'org-agenda-mode))
7734 (error "Cannot execute this command outside of org-agenda-mode buffers")
7735 (let (dest)
7736 (save-excursion
7737 (unless (looking-at "\\'")
7738 (forward-char))
7739 (let* ((prop 'org-agenda-structural-header)
7740 (p (previous-single-property-change (point) prop))
7741 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7742 (1- (point))) prop)))
7743 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7744 (if (not dest)
7745 (error "Cannot find the beginning of the blog")
7746 (goto-char dest)
7747 (move-beginning-of-line 1)))))
7749 (defun org-agenda-later (arg)
7750 "Go forward in time by the current span.
7751 With prefix ARG, go forward that many times the current span."
7752 (interactive "p")
7753 (org-agenda-check-type t 'agenda)
7754 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7755 (span (or (nth 2 args) org-agenda-current-span))
7756 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7757 (greg (calendar-gregorian-from-absolute sd))
7758 (cnt (org-get-at-bol 'org-day-cnt))
7759 greg2)
7760 (cond
7761 ((numberp span)
7762 (setq sd (+ (* span arg) sd)))
7763 ((eq span 'day)
7764 (setq sd (+ arg sd)))
7765 ((eq span 'week)
7766 (setq sd (+ (* 7 arg) sd)))
7767 ((eq span 'month)
7768 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7769 sd (calendar-absolute-from-gregorian greg2))
7770 (setcar greg2 (1+ (car greg2))))
7771 ((eq span 'year)
7772 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7773 sd (calendar-absolute-from-gregorian greg2))
7774 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7776 (setq sd (+ (* span arg) sd))))
7777 (let ((org-agenda-overriding-cmd
7778 ;; `cmd' may have been set by `org-agenda-run-series' which
7779 ;; uses `org-agenda-overriding-cmd' to decide whether
7780 ;; overriding is allowed for `cmd'
7781 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7782 (org-agenda-overriding-arguments
7783 (list (car args) sd span)))
7784 (org-agenda-redo)
7785 (org-agenda-find-same-or-today-or-agenda cnt))))
7787 (defun org-agenda-earlier (arg)
7788 "Go backward in time by the current span.
7789 With prefix ARG, go backward that many times the current span."
7790 (interactive "p")
7791 (org-agenda-later (- arg)))
7793 (defun org-agenda-view-mode-dispatch ()
7794 "Call one of the view mode commands."
7795 (interactive)
7796 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7797 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7798 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7799 (let ((a (read-char-exclusive)))
7800 (case a
7801 (?\ (call-interactively 'org-agenda-reset-view))
7802 (?d (call-interactively 'org-agenda-day-view))
7803 (?w (call-interactively 'org-agenda-week-view))
7804 (?m (call-interactively 'org-agenda-month-view))
7805 (?y (call-interactively 'org-agenda-year-view))
7806 (?l (call-interactively 'org-agenda-log-mode))
7807 (?L (org-agenda-log-mode '(4)))
7808 (?c (org-agenda-log-mode 'clockcheck))
7809 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7810 (?a (call-interactively 'org-agenda-archives-mode))
7811 (?A (org-agenda-archives-mode 'files))
7812 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7813 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7814 (?G (call-interactively 'org-agenda-toggle-time-grid))
7815 (?D (call-interactively 'org-agenda-toggle-diary))
7816 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7817 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7818 (org-agenda-check-type t 'timeline 'agenda)
7819 (org-agenda-redo))
7820 (message "Display now includes inactive timestamps as well"))
7821 (?q (message "Abort"))
7822 (otherwise (error "Invalid key" )))))
7824 (defun org-agenda-reset-view ()
7825 "Switch to default view for agenda."
7826 (interactive)
7827 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7828 (defun org-agenda-day-view (&optional day-of-month)
7829 "Switch to daily view for agenda.
7830 With argument DAY-OF-MONTH, switch to that day of the month."
7831 (interactive "P")
7832 (org-agenda-change-time-span 'day day-of-month))
7833 (defun org-agenda-week-view (&optional iso-week)
7834 "Switch to daily view for agenda.
7835 With argument ISO-WEEK, switch to the corresponding ISO week.
7836 If ISO-WEEK has more then 2 digits, only the last two encode the
7837 week. Any digits before this encode a year. So 200712 means
7838 week 12 of year 2007. Years in the range 1938-2037 can also be
7839 written as 2-digit years."
7840 (interactive "P")
7841 (org-agenda-change-time-span 'week iso-week))
7842 (defun org-agenda-month-view (&optional month)
7843 "Switch to monthly view for agenda.
7844 With argument MONTH, switch to that month."
7845 (interactive "P")
7846 (org-agenda-change-time-span 'month month))
7847 (defun org-agenda-year-view (&optional year)
7848 "Switch to yearly view for agenda.
7849 With argument YEAR, switch to that year.
7850 If MONTH has more then 2 digits, only the last two encode the
7851 month. Any digits before this encode a year. So 200712 means
7852 December year 2007. Years in the range 1938-2037 can also be
7853 written as 2-digit years."
7854 (interactive "P")
7855 (when year
7856 (setq year (org-small-year-to-year year)))
7857 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7858 (org-agenda-change-time-span 'year year)
7859 (error "Abort")))
7861 (defun org-agenda-change-time-span (span &optional n)
7862 "Change the agenda view to SPAN.
7863 SPAN may be `day', `week', `month', `year'."
7864 (org-agenda-check-type t 'agenda)
7865 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7866 (curspan (nth 2 args)))
7867 (if (and (not n) (equal curspan span))
7868 (error "Viewing span is already \"%s\"" span))
7869 (let* ((sd (or (org-get-at-bol 'day)
7870 (nth 1 args)
7871 org-starting-day))
7872 (sd (org-agenda-compute-starting-span sd span n))
7873 (org-agenda-overriding-cmd
7874 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7875 (org-agenda-overriding-arguments
7876 (list (car args) sd span)))
7877 (org-agenda-redo)
7878 (org-agenda-find-same-or-today-or-agenda))
7879 (org-agenda-set-mode-name)
7880 (message "Switched to %s view" span)))
7882 (defun org-agenda-compute-starting-span (sd span &optional n)
7883 "Compute starting date for agenda.
7884 SPAN may be `day', `week', `month', `year'. The return value
7885 is a cons cell with the starting date and the number of days,
7886 so that the date SD will be in that range."
7887 (let* ((greg (calendar-gregorian-from-absolute sd))
7888 (dg (nth 1 greg))
7889 (mg (car greg))
7890 (yg (nth 2 greg)))
7891 (cond
7892 ((eq span 'day)
7893 (when n
7894 (setq sd (+ (calendar-absolute-from-gregorian
7895 (list mg 1 yg))
7896 n -1))))
7897 ((eq span 'week)
7898 (let* ((nt (calendar-day-of-week
7899 (calendar-gregorian-from-absolute sd)))
7900 (d (if org-agenda-start-on-weekday
7901 (- nt org-agenda-start-on-weekday)
7904 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7905 (when n
7906 (require 'cal-iso)
7907 (when (> n 99)
7908 (setq y1 (org-small-year-to-year (/ n 100))
7909 n (mod n 100)))
7910 (setq sd
7911 (calendar-absolute-from-iso
7912 (list n 1
7913 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7914 ((eq span 'month)
7915 (let (y1)
7916 (when (and n (> n 99))
7917 (setq y1 (org-small-year-to-year (/ n 100))
7918 n (mod n 100)))
7919 (setq sd (calendar-absolute-from-gregorian
7920 (list (or n mg) 1 (or y1 yg))))))
7921 ((eq span 'year)
7922 (setq sd (calendar-absolute-from-gregorian
7923 (list 1 1 (or n yg))))))
7924 sd))
7926 (defun org-agenda-next-date-line (&optional arg)
7927 "Jump to the next line indicating a date in agenda buffer."
7928 (interactive "p")
7929 (org-agenda-check-type t 'agenda 'timeline)
7930 (beginning-of-line 1)
7931 ;; This does not work if user makes date format that starts with a blank
7932 (if (looking-at "^\\S-") (forward-char 1))
7933 (if (not (re-search-forward "^\\S-" nil t arg))
7934 (progn
7935 (backward-char 1)
7936 (error "No next date after this line in this buffer")))
7937 (goto-char (match-beginning 0)))
7939 (defun org-agenda-previous-date-line (&optional arg)
7940 "Jump to the previous line indicating a date in agenda buffer."
7941 (interactive "p")
7942 (org-agenda-check-type t 'agenda 'timeline)
7943 (beginning-of-line 1)
7944 (if (not (re-search-backward "^\\S-" nil t arg))
7945 (error "No previous date before this line in this buffer")))
7947 ;; Initialize the highlight
7948 (defvar org-hl (make-overlay 1 1))
7949 (overlay-put org-hl 'face 'highlight)
7951 (defun org-highlight (begin end &optional buffer)
7952 "Highlight a region with overlay."
7953 (move-overlay org-hl begin end (or buffer (current-buffer))))
7955 (defun org-unhighlight ()
7956 "Detach overlay INDEX."
7957 (org-detach-overlay org-hl))
7959 (defun org-unhighlight-once ()
7960 "Remove the highlight from its position, and this function from the hook."
7961 (remove-hook 'pre-command-hook 'org-unhighlight-once)
7962 (org-unhighlight))
7964 (defvar org-agenda-pre-follow-window-conf nil)
7965 (defun org-agenda-follow-mode ()
7966 "Toggle follow mode in an agenda buffer."
7967 (interactive)
7968 (unless org-agenda-follow-mode
7969 (setq org-agenda-pre-follow-window-conf
7970 (current-window-configuration)))
7971 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
7972 (unless org-agenda-follow-mode
7973 (set-window-configuration org-agenda-pre-follow-window-conf))
7974 (org-agenda-set-mode-name)
7975 (org-agenda-do-context-action)
7976 (message "Follow mode is %s"
7977 (if org-agenda-follow-mode "on" "off")))
7979 (defun org-agenda-entry-text-mode (&optional arg)
7980 "Toggle entry text mode in an agenda buffer."
7981 (interactive "P")
7982 (if (or org-agenda-tag-filter
7983 org-agenda-category-filter
7984 org-agenda-top-category-filter)
7985 (user-error "Can't show entry text in filtered views")
7986 (setq org-agenda-entry-text-mode (or (integerp arg)
7987 (not org-agenda-entry-text-mode)))
7988 (org-agenda-entry-text-hide)
7989 (and org-agenda-entry-text-mode
7990 (let ((org-agenda-entry-text-maxlines
7991 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7992 (org-agenda-entry-text-show)))
7993 (org-agenda-set-mode-name)
7994 (message "Entry text mode is %s%s"
7995 (if org-agenda-entry-text-mode "on" "off")
7996 (if (not org-agenda-entry-text-mode) ""
7997 (format " (maximum number of lines is %d)"
7998 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8000 (defun org-agenda-clockreport-mode (&optional with-filter)
8001 "Toggle clocktable mode in an agenda buffer.
8002 With prefix arg WITH-FILTER, make the clocktable respect the current
8003 agenda filter."
8004 (interactive "P")
8005 (org-agenda-check-type t 'agenda)
8006 (if with-filter
8007 (setq org-agenda-clockreport-mode 'with-filter)
8008 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8009 (org-agenda-set-mode-name)
8010 (org-agenda-redo)
8011 (message "Clocktable mode is %s"
8012 (if org-agenda-clockreport-mode "on" "off")))
8014 (defun org-agenda-log-mode (&optional special)
8015 "Toggle log mode in an agenda buffer.
8016 With argument SPECIAL, show all possible log items, not only the ones
8017 configured in `org-agenda-log-mode-items'.
8018 With a double `C-u' prefix arg, show *only* log items, nothing else."
8019 (interactive "P")
8020 (org-agenda-check-type t 'agenda 'timeline)
8021 (setq org-agenda-show-log
8022 (cond
8023 ((equal special '(16)) 'only)
8024 ((eq special 'clockcheck)
8025 (if (eq org-agenda-show-log 'clockcheck)
8026 nil 'clockcheck))
8027 (special '(closed clock state))
8028 (t (not org-agenda-show-log))))
8029 (org-agenda-set-mode-name)
8030 (org-agenda-redo)
8031 (message "Log mode is %s"
8032 (if org-agenda-show-log "on" "off")))
8034 (defun org-agenda-archives-mode (&optional with-files)
8035 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8036 When called with a prefix argument, include all archive files as well."
8037 (interactive "P")
8038 (setq org-agenda-archives-mode
8039 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8040 (org-agenda-set-mode-name)
8041 (org-agenda-redo)
8042 (message
8043 "%s"
8044 (cond
8045 ((eq org-agenda-archives-mode nil)
8046 "No archives are included")
8047 ((eq org-agenda-archives-mode 'trees)
8048 (format "Trees with :%s: tag are included" org-archive-tag))
8049 ((eq org-agenda-archives-mode t)
8050 (format "Trees with :%s: tag and all active archive files are included"
8051 org-archive-tag)))))
8053 (defun org-agenda-toggle-diary ()
8054 "Toggle diary inclusion in an agenda buffer."
8055 (interactive)
8056 (org-agenda-check-type t 'agenda)
8057 (setq org-agenda-include-diary (not org-agenda-include-diary))
8058 (org-agenda-redo)
8059 (org-agenda-set-mode-name)
8060 (message "Diary inclusion turned %s"
8061 (if org-agenda-include-diary "on" "off")))
8063 (defun org-agenda-toggle-deadlines ()
8064 "Toggle inclusion of entries with a deadline in an agenda buffer."
8065 (interactive)
8066 (org-agenda-check-type t 'agenda)
8067 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8068 (org-agenda-redo)
8069 (org-agenda-set-mode-name)
8070 (message "Deadlines inclusion turned %s"
8071 (if org-agenda-include-deadlines "on" "off")))
8073 (defun org-agenda-toggle-time-grid ()
8074 "Toggle time grid in an agenda buffer."
8075 (interactive)
8076 (org-agenda-check-type t 'agenda)
8077 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8078 (org-agenda-redo)
8079 (org-agenda-set-mode-name)
8080 (message "Time-grid turned %s"
8081 (if org-agenda-use-time-grid "on" "off")))
8083 (defun org-agenda-set-mode-name ()
8084 "Set the mode name to indicate all the small mode settings."
8085 (setq mode-name
8086 (list "Org-Agenda"
8087 (if (get 'org-agenda-files 'org-restrict) " []" "")
8089 '(:eval (org-agenda-span-name org-agenda-current-span))
8090 (if org-agenda-follow-mode " Follow" "")
8091 (if org-agenda-entry-text-mode " ETxt" "")
8092 (if org-agenda-include-diary " Diary" "")
8093 (if org-agenda-include-deadlines " Ddl" "")
8094 (if org-agenda-use-time-grid " Grid" "")
8095 (if (and (boundp 'org-habit-show-habits)
8096 org-habit-show-habits) " Habit" "")
8097 (cond
8098 ((consp org-agenda-show-log) " LogAll")
8099 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8100 (org-agenda-show-log " Log")
8101 (t ""))
8102 (if (or org-agenda-category-filter
8103 (get 'org-agenda-category-filter :preset-filter))
8104 '(:eval (org-propertize
8105 (concat " <"
8106 (mapconcat
8107 'identity
8108 (append
8109 (get 'org-agenda-category-filter :preset-filter)
8110 org-agenda-category-filter)
8112 ">")
8113 'face 'org-agenda-filter-category
8114 'help-echo "Category used in filtering")) "")
8115 (if (or org-agenda-tag-filter
8116 (get 'org-agenda-tag-filter :preset-filter))
8117 '(:eval (org-propertize
8118 (concat " {"
8119 (mapconcat
8120 'identity
8121 (append
8122 (get 'org-agenda-tag-filter :preset-filter)
8123 org-agenda-tag-filter)
8125 "}")
8126 'face 'org-agenda-filter-tags
8127 'help-echo "Tags used in filtering")) "")
8128 (if (or org-agenda-regexp-filter
8129 (get 'org-agenda-regexp-filter :preset-filter))
8130 '(:eval (org-propertize
8131 (concat " ["
8132 (mapconcat
8133 'identity
8134 (append
8135 (get 'org-agenda-regexp-filter :preset-filter)
8136 org-agenda-regexp-filter)
8138 "]")
8139 'face 'org-agenda-filter-regexp
8140 'help-echo "Regexp used in filtering")) "")
8141 (if org-agenda-archives-mode
8142 (if (eq org-agenda-archives-mode t)
8143 " Archives"
8144 (format " :%s:" org-archive-tag))
8146 (if org-agenda-clockreport-mode
8147 (if (eq org-agenda-clockreport-mode 'with-filter)
8148 " Clock{}" " Clock")
8149 "")))
8150 (force-mode-line-update))
8152 (define-obsolete-function-alias
8153 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8155 (defun org-agenda-update-agenda-type ()
8156 "Update the agenda type after each command."
8157 (setq org-agenda-type
8158 (or (get-text-property (point) 'org-agenda-type)
8159 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8161 (defun org-agenda-next-line ()
8162 "Move cursor to the next line, and show if follow mode is active."
8163 (interactive)
8164 (call-interactively 'next-line)
8165 (org-agenda-do-context-action))
8167 (defun org-agenda-previous-line ()
8168 "Move cursor to the previous line, and show if follow-mode is active."
8169 (interactive)
8170 (call-interactively 'previous-line)
8171 (org-agenda-do-context-action))
8173 (defun org-agenda-next-item (n)
8174 "Move cursor to next agenda item."
8175 (interactive "p")
8176 (let ((col (current-column)))
8177 (dotimes (c n)
8178 (when (next-single-property-change (point-at-eol) 'org-marker)
8179 (move-end-of-line 1)
8180 (goto-char (next-single-property-change (point) 'org-marker))))
8181 (org-move-to-column col))
8182 (org-agenda-do-context-action))
8184 (defun org-agenda-previous-item (n)
8185 "Move cursor to next agenda item."
8186 (interactive "p")
8187 (dotimes (c n)
8188 (let ((col (current-column))
8189 (goto (save-excursion
8190 (move-end-of-line 0)
8191 (previous-single-property-change (point) 'org-marker))))
8192 (if goto (goto-char goto))
8193 (org-move-to-column col)))
8194 (org-agenda-do-context-action))
8196 (defun org-agenda-do-context-action ()
8197 "Show outline path and, maybe, follow mode window."
8198 (let ((m (org-get-at-bol 'org-marker)))
8199 (when (and (markerp m) (marker-buffer m))
8200 (and org-agenda-follow-mode
8201 (if org-agenda-follow-indirect
8202 (org-agenda-tree-to-indirect-buffer nil)
8203 (org-agenda-show)))
8204 (and org-agenda-show-outline-path
8205 (org-with-point-at m (org-display-outline-path t))))))
8207 (defun org-agenda-show-tags ()
8208 "Show the tags applicable to the current item."
8209 (interactive)
8210 (let* ((tags (org-get-at-bol 'tags)))
8211 (if tags
8212 (message "Tags are :%s:"
8213 (org-no-properties (mapconcat 'identity tags ":")))
8214 (message "No tags associated with this line"))))
8216 (defun org-agenda-goto (&optional highlight)
8217 "Go to the Org-mode file which contains the item at point."
8218 (interactive)
8219 (let* ((marker (or (org-get-at-bol 'org-marker)
8220 (org-agenda-error)))
8221 (buffer (marker-buffer marker))
8222 (pos (marker-position marker)))
8223 (switch-to-buffer-other-window buffer)
8224 (widen)
8225 (push-mark)
8226 (goto-char pos)
8227 (when (derived-mode-p 'org-mode)
8228 (org-show-context 'agenda)
8229 (save-excursion
8230 (and (outline-next-heading)
8231 (org-flag-heading nil)))) ; show the next heading
8232 (when (outline-invisible-p)
8233 (show-entry)) ; display invisible text
8234 (recenter (/ (window-height) 2))
8235 (run-hooks 'org-agenda-after-show-hook)
8236 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8238 (defvar org-agenda-after-show-hook nil
8239 "Normal hook run after an item has been shown from the agenda.
8240 Point is in the buffer where the item originated.")
8242 (defun org-agenda-kill ()
8243 "Kill the entry or subtree belonging to the current agenda entry."
8244 (interactive)
8245 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8246 (let* ((bufname-orig (buffer-name))
8247 (marker (or (org-get-at-bol 'org-marker)
8248 (org-agenda-error)))
8249 (buffer (marker-buffer marker))
8250 (pos (marker-position marker))
8251 (type (org-get-at-bol 'type))
8252 dbeg dend (n 0) conf)
8253 (org-with-remote-undo buffer
8254 (with-current-buffer buffer
8255 (save-excursion
8256 (goto-char pos)
8257 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8258 (setq dbeg (progn (org-back-to-heading t) (point))
8259 dend (org-end-of-subtree t t))
8260 (setq dbeg (point-at-bol)
8261 dend (min (point-max) (1+ (point-at-eol)))))
8262 (goto-char dbeg)
8263 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8264 (setq conf (or (eq t org-agenda-confirm-kill)
8265 (and (numberp org-agenda-confirm-kill)
8266 (> n org-agenda-confirm-kill))))
8267 (and conf
8268 (not (y-or-n-p
8269 (format "Delete entry with %d lines in buffer \"%s\"? "
8270 n (buffer-name buffer))))
8271 (error "Abort"))
8272 (let ((org-agenda-buffer-name bufname-orig))
8273 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8274 (with-current-buffer buffer (delete-region dbeg dend))
8275 (message "Agenda item and source killed"))))
8277 (defvar org-archive-default-command) ; defined in org-archive.el
8278 (defun org-agenda-archive-default ()
8279 "Archive the entry or subtree belonging to the current agenda entry."
8280 (interactive)
8281 (require 'org-archive)
8282 (org-agenda-archive-with org-archive-default-command))
8284 (defun org-agenda-archive-default-with-confirmation ()
8285 "Archive the entry or subtree belonging to the current agenda entry."
8286 (interactive)
8287 (require 'org-archive)
8288 (org-agenda-archive-with org-archive-default-command 'confirm))
8290 (defun org-agenda-archive ()
8291 "Archive the entry or subtree belonging to the current agenda entry."
8292 (interactive)
8293 (org-agenda-archive-with 'org-archive-subtree))
8295 (defun org-agenda-archive-to-archive-sibling ()
8296 "Move the entry to the archive sibling."
8297 (interactive)
8298 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8300 (defun org-agenda-archive-with (cmd &optional confirm)
8301 "Move the entry to the archive sibling."
8302 (interactive)
8303 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8304 (let* ((bufname-orig (buffer-name))
8305 (marker (or (org-get-at-bol 'org-marker)
8306 (org-agenda-error)))
8307 (buffer (marker-buffer marker))
8308 (pos (marker-position marker)))
8309 (org-with-remote-undo buffer
8310 (with-current-buffer buffer
8311 (if (derived-mode-p 'org-mode)
8312 (if (and confirm
8313 (not (y-or-n-p "Archive this subtree or entry? ")))
8314 (error "Abort")
8315 (save-excursion
8316 (goto-char pos)
8317 (let ((org-agenda-buffer-name bufname-orig))
8318 (org-remove-subtree-entries-from-agenda))
8319 (org-back-to-heading t)
8320 (funcall cmd)))
8321 (error "Archiving works only in Org-mode files"))))))
8323 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8324 "Remove all lines in the agenda that correspond to a given subtree.
8325 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8326 If this information is not given, the function uses the tree at point."
8327 (let ((buf (or buf (current-buffer))) m p)
8328 (save-excursion
8329 (unless (and beg end)
8330 (org-back-to-heading t)
8331 (setq beg (point))
8332 (org-end-of-subtree t)
8333 (setq end (point)))
8334 (set-buffer (get-buffer org-agenda-buffer-name))
8335 (save-excursion
8336 (goto-char (point-max))
8337 (beginning-of-line 1)
8338 (while (not (bobp))
8339 (when (and (setq m (org-get-at-bol 'org-marker))
8340 (equal buf (marker-buffer m))
8341 (setq p (marker-position m))
8342 (>= p beg)
8343 (< p end))
8344 (let ((inhibit-read-only t))
8345 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8346 (beginning-of-line 0))))))
8348 (defun org-agenda-refile (&optional goto rfloc no-update)
8349 "Refile the item at point.
8351 When GOTO is 0 or '(64), clear the refile cache.
8352 When GOTO is '(16), go to the location of the last refiled item.
8353 RFLOC can be a refile location obtained in a different way.
8354 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8355 (interactive "P")
8356 (cond
8357 ((member goto '(0 (64)))
8358 (org-refile-cache-clear))
8359 ((equal goto '(16))
8360 (org-refile-goto-last-stored))
8362 (let* ((buffer-orig (buffer-name))
8363 (marker (or (org-get-at-bol 'org-hd-marker)
8364 (org-agenda-error)))
8365 (buffer (marker-buffer marker))
8366 (pos (marker-position marker))
8367 (rfloc (or rfloc
8368 (org-refile-get-location
8369 (if goto "Goto" "Refile to") buffer
8370 org-refile-allow-creating-parent-nodes))))
8371 (with-current-buffer buffer
8372 (save-excursion
8373 (save-restriction
8374 (widen)
8375 (goto-char marker)
8376 (let ((org-agenda-buffer-name buffer-orig))
8377 (org-remove-subtree-entries-from-agenda))
8378 (org-refile goto buffer rfloc)))))
8379 (unless no-update (org-agenda-redo)))))
8381 (defun org-agenda-open-link (&optional arg)
8382 "Open the link(s) in the current entry, if any.
8383 This looks for a link in the displayed line in the agenda.
8384 It also looks at the text of the entry itself."
8385 (interactive "P")
8386 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8387 (org-get-at-bol 'org-marker)))
8388 (buffer (and marker (marker-buffer marker)))
8389 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8390 (lkall (org-offer-links-in-entry buffer marker arg prefix))
8391 (lk0 (car lkall))
8392 (lk (if (stringp lk0) (list lk0) lk0))
8393 (lkend (cdr lkall))
8394 trg)
8395 (cond
8396 ((and buffer lk)
8397 (mapcar (lambda(l)
8398 (with-current-buffer buffer
8399 (setq trg (and (string-match org-bracket-link-regexp l)
8400 (match-string 1 l)))
8401 (if (or (not trg) (string-match org-any-link-re trg))
8402 (save-excursion
8403 (save-restriction
8404 (widen)
8405 (goto-char marker)
8406 (when (search-forward l nil lkend)
8407 (goto-char (match-beginning 0))
8408 (org-open-at-point))))
8409 ;; This is an internal link, widen the buffer
8410 (switch-to-buffer-other-window buffer)
8411 (widen)
8412 (goto-char marker)
8413 (when (search-forward l nil lkend)
8414 (goto-char (match-beginning 0))
8415 (org-open-at-point)))))
8416 lk))
8417 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8418 (save-excursion
8419 (beginning-of-line 1)
8420 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8421 (org-open-link-from-string (match-string 1)))
8422 (t (message "No link to open here")))))
8424 (defun org-agenda-copy-local-variable (var)
8425 "Get a variable from a referenced buffer and install it here."
8426 (let ((m (org-get-at-bol 'org-marker)))
8427 (when (and m (buffer-live-p (marker-buffer m)))
8428 (org-set-local var (with-current-buffer (marker-buffer m)
8429 (symbol-value var))))))
8431 (defun org-agenda-switch-to (&optional delete-other-windows)
8432 "Go to the Org-mode file which contains the item at point."
8433 (interactive)
8434 (if (and org-return-follows-link
8435 (not (org-get-at-bol 'org-marker))
8436 (org-in-regexp org-bracket-link-regexp))
8437 (org-open-link-from-string (match-string 0))
8438 (let* ((marker (or (org-get-at-bol 'org-marker)
8439 (org-agenda-error)))
8440 (buffer (marker-buffer marker))
8441 (pos (marker-position marker)))
8442 (org-pop-to-buffer-same-window buffer)
8443 (and delete-other-windows (delete-other-windows))
8444 (widen)
8445 (goto-char pos)
8446 (when (derived-mode-p 'org-mode)
8447 (org-show-context 'agenda)
8448 (save-excursion
8449 (and (outline-next-heading)
8450 (org-flag-heading nil))) ; show the next heading
8451 (when (outline-invisible-p)
8452 (show-entry)) ; display invisible text
8453 (run-hooks 'org-agenda-after-show-hook)))))
8455 (defun org-agenda-goto-mouse (ev)
8456 "Go to the Org-mode file which contains the item at the mouse click."
8457 (interactive "e")
8458 (mouse-set-point ev)
8459 (org-agenda-goto))
8461 (defun org-agenda-show (&optional full-entry)
8462 "Display the Org-mode file which contains the item at point.
8463 With prefix argument FULL-ENTRY, make the entire entry visible
8464 if it was hidden in the outline."
8465 (interactive "P")
8466 (let ((win (selected-window)))
8467 (if full-entry
8468 (let ((org-show-entry-below t))
8469 (org-agenda-goto t))
8470 (org-agenda-goto t))
8471 (select-window win)))
8473 (defvar org-agenda-show-window nil)
8474 (defun org-agenda-show-and-scroll-up (&optional arg)
8475 "Display the Org-mode file which contains the item at point.
8476 When called repeatedly, scroll the window that is displaying the buffer.
8477 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8478 `show-subtree' to display the item, so that drawers and logbooks stay
8479 folded."
8480 (interactive "P")
8481 (let ((win (selected-window)))
8482 (if (and (window-live-p org-agenda-show-window)
8483 (eq this-command last-command))
8484 (progn
8485 (select-window org-agenda-show-window)
8486 (ignore-errors (scroll-up)))
8487 (org-agenda-goto t)
8488 (if arg (org-show-entry) (show-subtree))
8489 (setq org-agenda-show-window (selected-window)))
8490 (select-window win)))
8492 (defun org-agenda-show-scroll-down ()
8493 "Scroll down the window showing the agenda."
8494 (interactive)
8495 (let ((win (selected-window)))
8496 (when (window-live-p org-agenda-show-window)
8497 (select-window org-agenda-show-window)
8498 (ignore-errors (scroll-down))
8499 (select-window win))))
8501 (defun org-agenda-show-1 (&optional more)
8502 "Display the Org-mode file which contains the item at point.
8503 The prefix arg selects the amount of information to display:
8505 0 hide the subtree
8506 1 just show the entry according to defaults.
8507 2 show the children view
8508 3 show the subtree view
8509 4 show the entire subtree and any LOGBOOK drawers
8510 5 show the entire subtree and any drawers
8511 With prefix argument FULL-ENTRY, make the entire entry visible
8512 if it was hidden in the outline."
8513 (interactive "p")
8514 (let ((win (selected-window)))
8515 (org-agenda-goto t)
8516 (org-recenter-heading 1)
8517 (cond
8518 ((= more 0)
8519 (hide-subtree)
8520 (save-excursion
8521 (org-back-to-heading)
8522 (run-hook-with-args 'org-cycle-hook 'folded))
8523 (message "Remote: FOLDED"))
8524 ((and (org-called-interactively-p 'any) (= more 1))
8525 (message "Remote: show with default settings"))
8526 ((= more 2)
8527 (show-entry)
8528 (show-children)
8529 (save-excursion
8530 (org-back-to-heading)
8531 (run-hook-with-args 'org-cycle-hook 'children))
8532 (message "Remote: CHILDREN"))
8533 ((= more 3)
8534 (show-subtree)
8535 (save-excursion
8536 (org-back-to-heading)
8537 (run-hook-with-args 'org-cycle-hook 'subtree))
8538 (message "Remote: SUBTREE"))
8539 ((= more 4)
8540 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8541 (org-drawer-regexp
8542 (concat "^[ \t]*:\\("
8543 (mapconcat 'regexp-quote org-drawers "\\|")
8544 "\\):[ \t]*$")))
8545 (show-subtree)
8546 (save-excursion
8547 (org-back-to-heading)
8548 (org-cycle-hide-drawers 'subtree)))
8549 (message "Remote: SUBTREE AND LOGBOOK"))
8550 ((> more 4)
8551 (show-subtree)
8552 (message "Remote: SUBTREE AND ALL DRAWERS")))
8553 (select-window win)))
8555 (defun org-recenter-heading (n)
8556 (save-excursion
8557 (org-back-to-heading)
8558 (recenter n)))
8560 (defvar org-agenda-cycle-counter nil)
8561 (defun org-agenda-cycle-show (&optional n)
8562 "Show the current entry in another window, with default settings.
8563 Default settings are taken from `org-show-hierarchy-above' and siblings.
8564 When use repeatedly in immediate succession, the remote entry will cycle
8565 through visibility
8567 children -> subtree -> folded
8569 When called with a numeric prefix arg, that arg will be passed through to
8570 `org-agenda-show-1'. For the interpretation of that argument, see the
8571 docstring of `org-agenda-show-1'."
8572 (interactive "P")
8573 (if (integerp n)
8574 (setq org-agenda-cycle-counter n)
8575 (if (not (eq last-command this-command))
8576 (setq org-agenda-cycle-counter 1)
8577 (if (equal org-agenda-cycle-counter 0)
8578 (setq org-agenda-cycle-counter 2)
8579 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8580 (if (> org-agenda-cycle-counter 3)
8581 (setq org-agenda-cycle-counter 0)))))
8582 (org-agenda-show-1 org-agenda-cycle-counter))
8584 (defun org-agenda-recenter (arg)
8585 "Display the Org-mode file which contains the item at point and recenter."
8586 (interactive "P")
8587 (let ((win (selected-window)))
8588 (org-agenda-goto t)
8589 (recenter arg)
8590 (select-window win)))
8592 (defun org-agenda-show-mouse (ev)
8593 "Display the Org-mode file which contains the item at the mouse click."
8594 (interactive "e")
8595 (mouse-set-point ev)
8596 (org-agenda-show))
8598 (defun org-agenda-check-no-diary ()
8599 "Check if the entry is a diary link and abort if yes."
8600 (if (org-get-at-bol 'org-agenda-diary-link)
8601 (org-agenda-error)))
8603 (defun org-agenda-error ()
8604 (error "Command not allowed in this line"))
8606 (defun org-agenda-tree-to-indirect-buffer (arg)
8607 "Show the subtree corresponding to the current entry in an indirect buffer.
8608 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8610 With a numerical prefix ARG, go up to this level and then take that tree.
8611 With a negative numeric ARG, go up by this number of levels.
8612 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8613 use the dedicated frame)."
8614 (interactive "P")
8615 (if current-prefix-arg
8616 (org-agenda-do-tree-to-indirect-buffer arg)
8617 (let ((agenda-buffer (buffer-name))
8618 (agenda-window (selected-window))
8619 (indirect-window
8620 (and org-last-indirect-buffer
8621 (get-buffer-window org-last-indirect-buffer))))
8622 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8623 (unless (or (eq org-indirect-buffer-display 'new-frame)
8624 (eq org-indirect-buffer-display 'dedicated-frame))
8625 (unwind-protect
8626 (unless (and indirect-window (window-live-p indirect-window))
8627 (setq indirect-window (split-window agenda-window)))
8628 (and indirect-window (select-window indirect-window))
8629 (switch-to-buffer org-last-indirect-buffer :norecord)
8630 (fit-window-to-buffer indirect-window)))
8631 (select-window (get-buffer-window agenda-buffer)))))
8633 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8634 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8635 (org-agenda-check-no-diary)
8636 (let* ((marker (or (org-get-at-bol 'org-marker)
8637 (org-agenda-error)))
8638 (buffer (marker-buffer marker))
8639 (pos (marker-position marker)))
8640 (with-current-buffer buffer
8641 (save-excursion
8642 (goto-char pos)
8643 (funcall 'org-tree-to-indirect-buffer arg)))))
8645 (defvar org-last-heading-marker (make-marker)
8646 "Marker pointing to the headline that last changed its TODO state
8647 by a remote command from the agenda.")
8649 (defun org-agenda-todo-nextset ()
8650 "Switch TODO entry to next sequence."
8651 (interactive)
8652 (org-agenda-todo 'nextset))
8654 (defun org-agenda-todo-previousset ()
8655 "Switch TODO entry to previous sequence."
8656 (interactive)
8657 (org-agenda-todo 'previousset))
8659 (defun org-agenda-todo (&optional arg)
8660 "Cycle TODO state of line at point, also in Org-mode file.
8661 This changes the line at point, all other lines in the agenda referring to
8662 the same tree node, and the headline of the tree node in the Org-mode file."
8663 (interactive "P")
8664 (org-agenda-check-no-diary)
8665 (let* ((col (current-column))
8666 (marker (or (org-get-at-bol 'org-marker)
8667 (org-agenda-error)))
8668 (buffer (marker-buffer marker))
8669 (pos (marker-position marker))
8670 (hdmarker (org-get-at-bol 'org-hd-marker))
8671 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8672 (inhibit-read-only t)
8673 org-agenda-headline-snapshot-before-repeat newhead just-one)
8674 (org-with-remote-undo buffer
8675 (with-current-buffer buffer
8676 (widen)
8677 (goto-char pos)
8678 (org-show-context 'agenda)
8679 (save-excursion
8680 (and (outline-next-heading)
8681 (org-flag-heading nil))) ; show the next heading
8682 (let ((current-prefix-arg arg))
8683 (call-interactively 'org-todo))
8684 (and (bolp) (forward-char 1))
8685 (setq newhead (org-get-heading))
8686 (when (and (org-bound-and-true-p
8687 org-agenda-headline-snapshot-before-repeat)
8688 (not (equal org-agenda-headline-snapshot-before-repeat
8689 newhead))
8690 todayp)
8691 (setq newhead org-agenda-headline-snapshot-before-repeat
8692 just-one t))
8693 (save-excursion
8694 (org-back-to-heading)
8695 (move-marker org-last-heading-marker (point))))
8696 (beginning-of-line 1)
8697 (save-excursion
8698 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8699 (org-move-to-column col))))
8701 (defun org-agenda-add-note (&optional arg)
8702 "Add a time-stamped note to the entry at point."
8703 (interactive "P")
8704 (org-agenda-check-no-diary)
8705 (let* ((marker (or (org-get-at-bol 'org-marker)
8706 (org-agenda-error)))
8707 (buffer (marker-buffer marker))
8708 (pos (marker-position marker))
8709 (hdmarker (org-get-at-bol 'org-hd-marker))
8710 (inhibit-read-only t))
8711 (with-current-buffer buffer
8712 (widen)
8713 (goto-char pos)
8714 (org-show-context 'agenda)
8715 (save-excursion
8716 (and (outline-next-heading)
8717 (org-flag-heading nil))) ; show the next heading
8718 (org-add-note))))
8720 (defun org-agenda-change-all-lines (newhead hdmarker
8721 &optional fixface just-this)
8722 "Change all lines in the agenda buffer which match HDMARKER.
8723 The new content of the line will be NEWHEAD (as modified by
8724 `org-agenda-format-item'). HDMARKER is checked with
8725 `equal' against all `org-hd-marker' text properties in the file.
8726 If FIXFACE is non-nil, the face of each item is modified according to
8727 the new TODO state.
8728 If JUST-THIS is non-nil, change just the current line, not all.
8729 If FORCE-TAGS is non nil, the car of it returns the new tags."
8730 (let* ((inhibit-read-only t)
8731 (line (org-current-line))
8732 (org-agenda-buffer (current-buffer))
8733 (thetags (with-current-buffer (marker-buffer hdmarker)
8734 (save-excursion (save-restriction (widen)
8735 (goto-char hdmarker)
8736 (org-get-tags-at)))))
8737 props m pl undone-face done-face finish new dotime level cat tags)
8738 (save-excursion
8739 (goto-char (point-max))
8740 (beginning-of-line 1)
8741 (while (not finish)
8742 (setq finish (bobp))
8743 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8744 (or (not just-this) (= (org-current-line) line))
8745 (equal m hdmarker))
8746 (setq props (text-properties-at (point))
8747 dotime (org-get-at-bol 'dotime)
8748 cat (org-get-at-bol 'org-category)
8749 level (org-get-at-bol 'level)
8750 tags thetags
8752 (let ((org-prefix-format-compiled
8753 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8754 org-prefix-format-compiled))
8755 (extra (org-get-at-bol 'extra)))
8756 (with-current-buffer (marker-buffer hdmarker)
8757 (save-excursion
8758 (save-restriction
8759 (widen)
8760 (org-agenda-format-item extra newhead level cat tags dotime)))))
8761 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8762 undone-face (org-get-at-bol 'undone-face)
8763 done-face (org-get-at-bol 'done-face))
8764 (beginning-of-line 1)
8765 (cond
8766 ((equal new "")
8767 (and (looking-at ".*\n?") (replace-match "")))
8768 ((looking-at ".*")
8769 (replace-match new t t)
8770 (beginning-of-line 1)
8771 (add-text-properties (point-at-bol) (point-at-eol) props)
8772 (when fixface
8773 (add-text-properties
8774 (point-at-bol) (point-at-eol)
8775 (list 'face
8776 (if org-last-todo-state-is-todo
8777 undone-face done-face))))
8778 (org-agenda-highlight-todo 'line)
8779 (beginning-of-line 1))
8780 (t (error "Line update did not work")))
8781 (save-restriction
8782 (narrow-to-region (point-at-bol) (point-at-eol))
8783 (org-agenda-finalize)))
8784 (beginning-of-line 0)))))
8786 (defun org-agenda-align-tags (&optional line)
8787 "Align all tags in agenda items to `org-agenda-tags-column'."
8788 (let ((inhibit-read-only t) l c)
8789 (save-excursion
8790 (goto-char (if line (point-at-bol) (point-min)))
8791 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8792 (if line (point-at-eol) nil) t)
8793 (add-text-properties
8794 (match-beginning 2) (match-end 2)
8795 (list 'face (delq nil (let ((prop (get-text-property
8796 (match-beginning 2) 'face)))
8797 (or (listp prop) (setq prop (list prop)))
8798 (if (memq 'org-tag prop)
8799 prop
8800 (cons 'org-tag prop))))))
8801 (setq l (- (match-end 2) (match-beginning 2))
8802 c (if (< org-agenda-tags-column 0)
8803 (- (abs org-agenda-tags-column) l)
8804 org-agenda-tags-column))
8805 (delete-region (match-beginning 1) (match-end 1))
8806 (goto-char (match-beginning 1))
8807 (insert (org-add-props
8808 (make-string (max 1 (- c (current-column))) ?\ )
8809 (plist-put (copy-sequence (text-properties-at (point)))
8810 'face nil))))
8811 (goto-char (point-min))
8812 (org-font-lock-add-tag-faces (point-max)))))
8814 (defun org-agenda-priority-up ()
8815 "Increase the priority of line at point, also in Org-mode file."
8816 (interactive)
8817 (org-agenda-priority 'up))
8819 (defun org-agenda-priority-down ()
8820 "Decrease the priority of line at point, also in Org-mode file."
8821 (interactive)
8822 (org-agenda-priority 'down))
8824 (defun org-agenda-priority (&optional force-direction)
8825 "Set the priority of line at point, also in Org-mode file.
8826 This changes the line at point, all other lines in the agenda referring to
8827 the same tree node, and the headline of the tree node in the Org-mode file.
8828 Called with a universal prefix arg, show the priority instead of setting it."
8829 (interactive "P")
8830 (if (equal force-direction '(4))
8831 (org-show-priority)
8832 (unless org-enable-priority-commands
8833 (error "Priority commands are disabled"))
8834 (org-agenda-check-no-diary)
8835 (let* ((marker (or (org-get-at-bol 'org-marker)
8836 (org-agenda-error)))
8837 (hdmarker (org-get-at-bol 'org-hd-marker))
8838 (buffer (marker-buffer hdmarker))
8839 (pos (marker-position hdmarker))
8840 (inhibit-read-only t)
8841 newhead)
8842 (org-with-remote-undo buffer
8843 (with-current-buffer buffer
8844 (widen)
8845 (goto-char pos)
8846 (org-show-context 'agenda)
8847 (save-excursion
8848 (and (outline-next-heading)
8849 (org-flag-heading nil))) ; show the next heading
8850 (funcall 'org-priority force-direction)
8851 (end-of-line 1)
8852 (setq newhead (org-get-heading)))
8853 (org-agenda-change-all-lines newhead hdmarker)
8854 (beginning-of-line 1)))))
8856 ;; FIXME: should fix the tags property of the agenda line.
8857 (defun org-agenda-set-tags (&optional tag onoff)
8858 "Set tags for the current headline."
8859 (interactive)
8860 (org-agenda-check-no-diary)
8861 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8862 (call-interactively 'org-change-tag-in-region)
8863 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8864 (org-agenda-error)))
8865 (buffer (marker-buffer hdmarker))
8866 (pos (marker-position hdmarker))
8867 (inhibit-read-only t)
8868 newhead)
8869 (org-with-remote-undo buffer
8870 (with-current-buffer buffer
8871 (widen)
8872 (goto-char pos)
8873 (save-excursion
8874 (org-show-context 'agenda))
8875 (save-excursion
8876 (and (outline-next-heading)
8877 (org-flag-heading nil))) ; show the next heading
8878 (goto-char pos)
8879 (if tag
8880 (org-toggle-tag tag onoff)
8881 (call-interactively 'org-set-tags))
8882 (end-of-line 1)
8883 (setq newhead (org-get-heading)))
8884 (org-agenda-change-all-lines newhead hdmarker)
8885 (beginning-of-line 1)))))
8887 (defun org-agenda-set-property ()
8888 "Set a property for the current headline."
8889 (interactive)
8890 (org-agenda-check-no-diary)
8891 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8892 (org-agenda-error)))
8893 (buffer (marker-buffer hdmarker))
8894 (pos (marker-position hdmarker))
8895 (inhibit-read-only t)
8896 newhead)
8897 (org-with-remote-undo buffer
8898 (with-current-buffer buffer
8899 (widen)
8900 (goto-char pos)
8901 (save-excursion
8902 (org-show-context 'agenda))
8903 (save-excursion
8904 (and (outline-next-heading)
8905 (org-flag-heading nil))) ; show the next heading
8906 (goto-char pos)
8907 (call-interactively 'org-set-property)))))
8909 (defun org-agenda-set-effort ()
8910 "Set the effort property for the current headline."
8911 (interactive)
8912 (org-agenda-check-no-diary)
8913 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8914 (org-agenda-error)))
8915 (buffer (marker-buffer hdmarker))
8916 (pos (marker-position hdmarker))
8917 (inhibit-read-only t)
8918 newhead)
8919 (org-with-remote-undo buffer
8920 (with-current-buffer buffer
8921 (widen)
8922 (goto-char pos)
8923 (save-excursion
8924 (org-show-context 'agenda))
8925 (save-excursion
8926 (and (outline-next-heading)
8927 (org-flag-heading nil))) ; show the next heading
8928 (goto-char pos)
8929 (call-interactively 'org-set-effort)
8930 (end-of-line 1)
8931 (setq newhead (org-get-heading)))
8932 (org-agenda-change-all-lines newhead hdmarker))))
8934 (defun org-agenda-toggle-archive-tag ()
8935 "Toggle the archive tag for the current entry."
8936 (interactive)
8937 (org-agenda-check-no-diary)
8938 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8939 (org-agenda-error)))
8940 (buffer (marker-buffer hdmarker))
8941 (pos (marker-position hdmarker))
8942 (inhibit-read-only t)
8943 newhead)
8944 (org-with-remote-undo buffer
8945 (with-current-buffer buffer
8946 (widen)
8947 (goto-char pos)
8948 (org-show-context 'agenda)
8949 (save-excursion
8950 (and (outline-next-heading)
8951 (org-flag-heading nil))) ; show the next heading
8952 (call-interactively 'org-toggle-archive-tag)
8953 (end-of-line 1)
8954 (setq newhead (org-get-heading)))
8955 (org-agenda-change-all-lines newhead hdmarker)
8956 (beginning-of-line 1))))
8958 (defun org-agenda-do-date-later (arg)
8959 (interactive "P")
8960 (cond
8961 ((or (equal arg '(16))
8962 (memq last-command
8963 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
8964 (setq this-command 'org-agenda-date-later-minutes)
8965 (org-agenda-date-later-minutes 1))
8966 ((or (equal arg '(4))
8967 (memq last-command
8968 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8969 (setq this-command 'org-agenda-date-later-hours)
8970 (org-agenda-date-later-hours 1))
8972 (org-agenda-date-later (prefix-numeric-value arg)))))
8974 (defun org-agenda-do-date-earlier (arg)
8975 (interactive "P")
8976 (cond
8977 ((or (equal arg '(16))
8978 (memq last-command
8979 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
8980 (setq this-command 'org-agenda-date-earlier-minutes)
8981 (org-agenda-date-earlier-minutes 1))
8982 ((or (equal arg '(4))
8983 (memq last-command
8984 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8985 (setq this-command 'org-agenda-date-earlier-hours)
8986 (org-agenda-date-earlier-hours 1))
8988 (org-agenda-date-earlier (prefix-numeric-value arg)))))
8990 (defun org-agenda-date-later (arg &optional what)
8991 "Change the date of this item to ARG day(s) later."
8992 (interactive "p")
8993 (org-agenda-check-type t 'agenda 'timeline)
8994 (org-agenda-check-no-diary)
8995 (let* ((marker (or (org-get-at-bol 'org-marker)
8996 (org-agenda-error)))
8997 (buffer (marker-buffer marker))
8998 (pos (marker-position marker))
8999 cdate today)
9000 (org-with-remote-undo buffer
9001 (with-current-buffer buffer
9002 (widen)
9003 (goto-char pos)
9004 (if (not (org-at-timestamp-p))
9005 (error "Cannot find time stamp"))
9006 (when (and org-agenda-move-date-from-past-immediately-to-today
9007 (equal arg 1)
9008 (or (not what) (eq what 'day))
9009 (not (save-match-data (org-at-date-range-p))))
9010 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9011 cdate (calendar-absolute-from-gregorian
9012 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9013 today (org-today))
9014 (if (> today cdate)
9015 ;; immediately shift to today
9016 (setq arg (- today cdate))))
9017 (org-timestamp-change arg (or what 'day))
9018 (when (and (org-at-date-range-p)
9019 (re-search-backward org-tr-regexp-both (point-at-bol)))
9020 (let ((end org-last-changed-timestamp))
9021 (org-timestamp-change arg (or what 'day))
9022 (setq org-last-changed-timestamp
9023 (concat org-last-changed-timestamp "--" end)))))
9024 (org-agenda-show-new-time marker org-last-changed-timestamp))
9025 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9027 (defun org-agenda-date-earlier (arg &optional what)
9028 "Change the date of this item to ARG day(s) earlier."
9029 (interactive "p")
9030 (org-agenda-date-later (- arg) what))
9032 (defun org-agenda-date-later-minutes (arg)
9033 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9034 (interactive "p")
9035 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9036 (org-agenda-date-later arg 'minute))
9038 (defun org-agenda-date-earlier-minutes (arg)
9039 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9040 (interactive "p")
9041 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9042 (org-agenda-date-earlier arg 'minute))
9044 (defun org-agenda-date-later-hours (arg)
9045 "Change the time of this item, in hour steps."
9046 (interactive "p")
9047 (org-agenda-date-later arg 'hour))
9049 (defun org-agenda-date-earlier-hours (arg)
9050 "Change the time of this item, in hour steps."
9051 (interactive "p")
9052 (org-agenda-date-earlier arg 'hour))
9054 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9055 "Show new date stamp via text properties."
9056 ;; We use text properties to make this undoable
9057 (let ((inhibit-read-only t))
9058 (setq stamp (concat prefix " => " stamp " "))
9059 (save-excursion
9060 (goto-char (point-max))
9061 (while (not (bobp))
9062 (when (equal marker (org-get-at-bol 'org-marker))
9063 (org-move-to-column (- (window-width) (length stamp)) t)
9064 (org-agenda-fix-tags-filter-overlays-at (point))
9065 (if (featurep 'xemacs)
9066 ;; Use `duplicable' property to trigger undo recording
9067 (let ((ex (make-extent nil nil))
9068 (gl (make-glyph stamp)))
9069 (set-glyph-face gl 'secondary-selection)
9070 (set-extent-properties
9071 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9072 (insert-extent ex (1- (point)) (point-at-eol)))
9073 (add-text-properties
9074 (1- (point)) (point-at-eol)
9075 (list 'display (org-add-props stamp nil
9076 'face 'secondary-selection))))
9077 (beginning-of-line 1))
9078 (beginning-of-line 0)))))
9080 (defun org-agenda-date-prompt (arg)
9081 "Change the date of this item. Date is prompted for, with default today.
9082 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9083 be used to request time specification in the time stamp."
9084 (interactive "P")
9085 (org-agenda-check-type t 'agenda 'timeline)
9086 (org-agenda-check-no-diary)
9087 (let* ((marker (or (org-get-at-bol 'org-marker)
9088 (org-agenda-error)))
9089 (buffer (marker-buffer marker))
9090 (pos (marker-position marker)))
9091 (org-with-remote-undo buffer
9092 (with-current-buffer buffer
9093 (widen)
9094 (goto-char pos)
9095 (if (not (org-at-timestamp-p t))
9096 (error "Cannot find time stamp"))
9097 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9098 (org-agenda-show-new-time marker org-last-changed-timestamp))
9099 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9101 (defun org-agenda-schedule (arg &optional time)
9102 "Schedule the item at point.
9103 ARG is passed through to `org-schedule'."
9104 (interactive "P")
9105 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9106 (org-agenda-check-no-diary)
9107 (let* ((marker (or (org-get-at-bol 'org-marker)
9108 (org-agenda-error)))
9109 (type (marker-insertion-type marker))
9110 (buffer (marker-buffer marker))
9111 (pos (marker-position marker))
9112 (org-insert-labeled-timestamps-at-point nil)
9114 (set-marker-insertion-type marker t)
9115 (org-with-remote-undo buffer
9116 (with-current-buffer buffer
9117 (widen)
9118 (goto-char pos)
9119 (setq ts (org-schedule arg time)))
9120 (org-agenda-show-new-time marker ts " S"))
9121 (message "%s" ts)))
9123 (defun org-agenda-deadline (arg &optional time)
9124 "Schedule the item at point.
9125 ARG is passed through to `org-deadline'."
9126 (interactive "P")
9127 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9128 (org-agenda-check-no-diary)
9129 (let* ((marker (or (org-get-at-bol 'org-marker)
9130 (org-agenda-error)))
9131 (buffer (marker-buffer marker))
9132 (pos (marker-position marker))
9133 (org-insert-labeled-timestamps-at-point nil)
9135 (org-with-remote-undo buffer
9136 (with-current-buffer buffer
9137 (widen)
9138 (goto-char pos)
9139 (setq ts (org-deadline arg time)))
9140 (org-agenda-show-new-time marker ts " D"))
9141 (message "%s" ts)))
9143 (defun org-agenda-clock-in (&optional arg)
9144 "Start the clock on the currently selected item."
9145 (interactive "P")
9146 (org-agenda-check-no-diary)
9147 (if (equal arg '(4))
9148 (org-clock-in arg)
9149 (let* ((marker (or (org-get-at-bol 'org-marker)
9150 (org-agenda-error)))
9151 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9152 (pos (marker-position marker))
9153 (col (current-column))
9154 newhead)
9155 (org-with-remote-undo (marker-buffer marker)
9156 (with-current-buffer (marker-buffer marker)
9157 (widen)
9158 (goto-char pos)
9159 (org-show-context 'agenda)
9160 (org-show-entry)
9161 (org-cycle-hide-drawers 'children)
9162 (org-clock-in arg)
9163 (setq newhead (org-get-heading)))
9164 (org-agenda-change-all-lines newhead hdmarker))
9165 (org-move-to-column col))))
9167 (defun org-agenda-clock-out ()
9168 "Stop the currently running clock."
9169 (interactive)
9170 (unless (marker-buffer org-clock-marker)
9171 (error "No running clock"))
9172 (let ((marker (make-marker)) (col (current-column)) newhead)
9173 (org-with-remote-undo (marker-buffer org-clock-marker)
9174 (with-current-buffer (marker-buffer org-clock-marker)
9175 (save-excursion
9176 (save-restriction
9177 (widen)
9178 (goto-char org-clock-marker)
9179 (org-back-to-heading t)
9180 (move-marker marker (point))
9181 (org-clock-out)
9182 (setq newhead (org-get-heading))))))
9183 (org-agenda-change-all-lines newhead marker)
9184 (move-marker marker nil)
9185 (org-move-to-column col)
9186 (org-agenda-unmark-clocking-task)))
9188 (defun org-agenda-clock-cancel (&optional arg)
9189 "Cancel the currently running clock."
9190 (interactive "P")
9191 (unless (marker-buffer org-clock-marker)
9192 (error "No running clock"))
9193 (org-with-remote-undo (marker-buffer org-clock-marker)
9194 (org-clock-cancel)))
9196 (defun org-agenda-clock-goto ()
9197 "Jump to the currently clocked in task within the agenda.
9198 If the currently clocked in task is not listed in the agenda
9199 buffer, display it in another window."
9200 (interactive)
9201 (let (pos)
9202 (mapc (lambda (o)
9203 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9204 (setq pos (overlay-start o))))
9205 (overlays-in (point-min) (point-max)))
9206 (cond (pos (goto-char pos))
9207 ;; If the currently clocked entry is not in the agenda
9208 ;; buffer, we visit it in another window:
9209 (org-clock-current-task
9210 (org-switch-to-buffer-other-window (org-clock-goto)))
9211 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9213 (defun org-agenda-diary-entry-in-org-file ()
9214 "Make a diary entry in the file `org-agenda-diary-file'."
9215 (let (d1 d2 char (text "") dp1 dp2)
9216 (if (equal (buffer-name) "*Calendar*")
9217 (setq d1 (calendar-cursor-to-date t)
9218 d2 (car calendar-mark-ring))
9219 (setq dp1 (get-text-property (point-at-bol) 'day))
9220 (unless dp1 (error "No date defined in current line"))
9221 (setq d1 (calendar-gregorian-from-absolute dp1)
9222 d2 (and (ignore-errors (mark))
9223 (save-excursion
9224 (goto-char (mark))
9225 (setq dp2 (get-text-property (point-at-bol) 'day)))
9226 (calendar-gregorian-from-absolute dp2))))
9227 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9228 (setq char (read-char-exclusive))
9229 (cond
9230 ((equal char ?d)
9231 (setq text (read-string "Day entry: "))
9232 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9233 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9234 ((equal char ?a)
9235 (setq d1 (list (car d1) (nth 1 d1)
9236 (read-number (format "Reference year [%d]: " (nth 2 d1))
9237 (nth 2 d1))))
9238 (setq text (read-string "Anniversary (use %d to show years): "))
9239 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9240 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9241 ((equal char ?b)
9242 (setq text (read-string "Block entry: "))
9243 (unless (and d1 d2 (not (equal d1 d2)))
9244 (error "No block of days selected"))
9245 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9246 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9247 ((equal char ?j)
9248 (org-switch-to-buffer-other-window
9249 (find-file-noselect org-agenda-diary-file))
9250 (require 'org-datetree)
9251 (org-datetree-find-date-create d1)
9252 (org-reveal t))
9253 (t (error "Invalid selection character `%c'" char)))))
9255 (defcustom org-agenda-insert-diary-strategy 'date-tree
9256 "Where in `org-agenda-diary-file' should new entries be added?
9257 Valid values:
9259 date-tree in the date tree, as child of the date
9260 top-level as top-level entries at the end of the file."
9261 :group 'org-agenda
9262 :type '(choice
9263 (const :tag "in a date tree" date-tree)
9264 (const :tag "as top level at end of file" top-level)))
9266 (defcustom org-agenda-insert-diary-extract-time nil
9267 "Non-nil means extract any time specification from the diary entry."
9268 :group 'org-agenda
9269 :version "24.1"
9270 :type 'boolean)
9272 (defcustom org-agenda-bulk-mark-char ">"
9273 "A single-character string to be used as the bulk mark."
9274 :group 'org-agenda
9275 :version "24.1"
9276 :type 'string)
9278 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9279 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9280 If TEXT is not empty, it will become the headline of the new entry, and
9281 the resulting entry will not be shown. When TEXT is empty, switch to
9282 `org-agenda-diary-file' and let the user finish the entry there."
9283 (let ((cw (current-window-configuration)))
9284 (org-switch-to-buffer-other-window
9285 (find-file-noselect org-agenda-diary-file))
9286 (widen)
9287 (goto-char (point-min))
9288 (cond
9289 ((eq type 'anniversary)
9290 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9291 (progn
9292 (or (org-at-heading-p t)
9293 (progn
9294 (outline-next-heading)
9295 (insert "* Anniversaries\n\n")
9296 (beginning-of-line -1)))))
9297 (outline-next-heading)
9298 (org-back-over-empty-lines)
9299 (backward-char 1)
9300 (insert "\n")
9301 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9302 (nth 2 d1) (car d1) (nth 1 d1) text)))
9303 ((eq type 'day)
9304 (let ((org-prefix-has-time t)
9305 (org-agenda-time-leading-zero t)
9306 fmt time time2)
9307 (if org-agenda-insert-diary-extract-time
9308 ;; Use org-agenda-format-item to parse text for a time-range and
9309 ;; remove it. FIXME: This is a hack, we should refactor
9310 ;; that function to make time extraction available separately
9311 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9312 time (get-text-property 0 'time fmt)
9313 time2 (if (> (length time) 0)
9314 ;; split-string removes trailing ...... if
9315 ;; no end time given. First space
9316 ;; separates time from date.
9317 (concat " " (car (split-string time "\\.")))
9318 nil)
9319 text (get-text-property 0 'txt fmt)))
9320 (if (eq org-agenda-insert-diary-strategy 'top-level)
9321 (org-agenda-insert-diary-as-top-level text)
9322 (require 'org-datetree)
9323 (org-datetree-find-date-create d1)
9324 (org-agenda-insert-diary-make-new-entry text))
9325 (org-insert-time-stamp (org-time-from-absolute
9326 (calendar-absolute-from-gregorian d1))
9327 nil nil nil nil time2))
9328 (end-of-line 0))
9329 ((eq type 'block)
9330 (if (> (calendar-absolute-from-gregorian d1)
9331 (calendar-absolute-from-gregorian d2))
9332 (setq d1 (prog1 d2 (setq d2 d1))))
9333 (if (eq org-agenda-insert-diary-strategy 'top-level)
9334 (org-agenda-insert-diary-as-top-level text)
9335 (require 'org-datetree)
9336 (org-datetree-find-date-create d1)
9337 (org-agenda-insert-diary-make-new-entry text))
9338 (org-insert-time-stamp (org-time-from-absolute
9339 (calendar-absolute-from-gregorian d1)))
9340 (insert "--")
9341 (org-insert-time-stamp (org-time-from-absolute
9342 (calendar-absolute-from-gregorian d2)))
9343 (end-of-line 0)))
9344 (if (string-match "\\S-" text)
9345 (progn
9346 (set-window-configuration cw)
9347 (message "%s entry added to %s"
9348 (capitalize (symbol-name type))
9349 (abbreviate-file-name org-agenda-diary-file)))
9350 (org-reveal t)
9351 (message "Please finish entry here"))))
9353 (defun org-agenda-insert-diary-as-top-level (text)
9354 "Make new entry as a top-level entry at the end of the file.
9355 Add TEXT as headline, and position the cursor in the second line so that
9356 a timestamp can be added there."
9357 (widen)
9358 (goto-char (point-max))
9359 (or (bolp) (insert "\n"))
9360 (insert "* " text "\n")
9361 (if org-adapt-indentation (org-indent-to-column 2)))
9363 (defun org-agenda-insert-diary-make-new-entry (text)
9364 "Make new entry as last child of current entry.
9365 Add TEXT as headline, and position the cursor in the second line so that
9366 a timestamp can be added there."
9367 (let ((org-show-following-heading t)
9368 (org-show-siblings t)
9369 (org-show-hierarchy-above t)
9370 (org-show-entry-below t)
9371 col)
9372 (outline-next-heading)
9373 (org-back-over-empty-lines)
9374 (or (looking-at "[ \t]*$")
9375 (progn (insert "\n") (backward-char 1)))
9376 (org-insert-heading nil t)
9377 (org-do-demote)
9378 (setq col (current-column))
9379 (insert text "\n")
9380 (if org-adapt-indentation (org-indent-to-column col))
9381 (let ((org-show-following-heading t)
9382 (org-show-siblings t)
9383 (org-show-hierarchy-above t)
9384 (org-show-entry-below t))
9385 (org-show-context))))
9387 (defun org-agenda-diary-entry ()
9388 "Make a diary entry, like the `i' command from the calendar.
9389 All the standard commands work: block, weekly etc.
9390 When `org-agenda-diary-file' points to a file,
9391 `org-agenda-diary-entry-in-org-file' is called instead to create
9392 entries in that Org-mode file."
9393 (interactive)
9394 (if (not (eq org-agenda-diary-file 'diary-file))
9395 (org-agenda-diary-entry-in-org-file)
9396 (require 'diary-lib)
9397 (let* ((char (progn
9398 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9399 (read-char-exclusive)))
9400 (cmd (cdr (assoc char
9401 '((?d . insert-diary-entry)
9402 (?w . insert-weekly-diary-entry)
9403 (?m . insert-monthly-diary-entry)
9404 (?y . insert-yearly-diary-entry)
9405 (?a . insert-anniversary-diary-entry)
9406 (?b . insert-block-diary-entry)
9407 (?c . insert-cyclic-diary-entry)))))
9408 (oldf (symbol-function 'calendar-cursor-to-date))
9409 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9410 (point (point))
9411 (mark (or (mark t) (point))))
9412 (unless cmd
9413 (error "No command associated with <%c>" char))
9414 (unless (and (get-text-property point 'day)
9415 (or (not (equal ?b char))
9416 (get-text-property mark 'day)))
9417 (error "Don't know which date to use for diary entry"))
9418 ;; We implement this by hacking the `calendar-cursor-to-date' function
9419 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9420 (let ((calendar-mark-ring
9421 (list (calendar-gregorian-from-absolute
9422 (or (get-text-property mark 'day)
9423 (get-text-property point 'day))))))
9424 (unwind-protect
9425 (progn
9426 (fset 'calendar-cursor-to-date
9427 (lambda (&optional error dummy)
9428 (calendar-gregorian-from-absolute
9429 (get-text-property point 'day))))
9430 (call-interactively cmd))
9431 (fset 'calendar-cursor-to-date oldf))))))
9433 (defun org-agenda-execute-calendar-command (cmd)
9434 "Execute a calendar command from the agenda with date from cursor."
9435 (org-agenda-check-type t 'agenda 'timeline)
9436 (require 'diary-lib)
9437 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9438 (error "Don't know which date to use for the calendar command"))
9439 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9440 (point (point))
9441 (date (calendar-gregorian-from-absolute
9442 (get-text-property point 'day)))
9443 ;; the following 2 vars are needed in the calendar
9444 (displayed-month (car date))
9445 (displayed-year (nth 2 date)))
9446 (unwind-protect
9447 (progn
9448 (fset 'calendar-cursor-to-date
9449 (lambda (&optional error dummy)
9450 (calendar-gregorian-from-absolute
9451 (get-text-property point 'day))))
9452 (call-interactively cmd))
9453 (fset 'calendar-cursor-to-date oldf))))
9455 (defun org-agenda-phases-of-moon ()
9456 "Display the phases of the moon for the 3 months around the cursor date."
9457 (interactive)
9458 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9460 (defun org-agenda-holidays ()
9461 "Display the holidays for the 3 months around the cursor date."
9462 (interactive)
9463 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9465 (defvar calendar-longitude) ; defined in calendar.el
9466 (defvar calendar-latitude) ; defined in calendar.el
9467 (defvar calendar-location-name) ; defined in calendar.el
9469 (defun org-agenda-sunrise-sunset (arg)
9470 "Display sunrise and sunset for the cursor date.
9471 Latitude and longitude can be specified with the variables
9472 `calendar-latitude' and `calendar-longitude'. When called with prefix
9473 argument, latitude and longitude will be prompted for."
9474 (interactive "P")
9475 (require 'solar)
9476 (let ((calendar-longitude (if arg nil calendar-longitude))
9477 (calendar-latitude (if arg nil calendar-latitude))
9478 (calendar-location-name
9479 (if arg "the given coordinates" calendar-location-name)))
9480 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9482 (defun org-agenda-goto-calendar ()
9483 "Open the Emacs calendar with the date at the cursor."
9484 (interactive)
9485 (org-agenda-check-type t 'agenda 'timeline)
9486 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9487 (error "Don't know which date to open in calendar")))
9488 (date (calendar-gregorian-from-absolute day))
9489 (calendar-move-hook nil)
9490 (calendar-view-holidays-initially-flag nil)
9491 (calendar-view-diary-initially-flag nil))
9492 (calendar)
9493 (calendar-goto-date date)))
9495 ;;;###autoload
9496 (defun org-calendar-goto-agenda ()
9497 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9498 This is a command that has to be installed in `calendar-mode-map'."
9499 (interactive)
9500 (org-agenda-list nil (calendar-absolute-from-gregorian
9501 (calendar-cursor-to-date))
9502 nil))
9504 (defun org-agenda-convert-date ()
9505 (interactive)
9506 (org-agenda-check-type t 'agenda 'timeline)
9507 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9508 date s)
9509 (unless day
9510 (error "Don't know which date to convert"))
9511 (setq date (calendar-gregorian-from-absolute day))
9512 (setq s (concat
9513 "Gregorian: " (calendar-date-string date) "\n"
9514 "ISO: " (calendar-iso-date-string date) "\n"
9515 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9516 "Julian: " (calendar-julian-date-string date) "\n"
9517 "Astron. JD: " (calendar-astro-date-string date)
9518 " (Julian date number at noon UTC)\n"
9519 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9520 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9521 "French: " (calendar-french-date-string date) "\n"
9522 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9523 "Mayan: " (calendar-mayan-date-string date) "\n"
9524 "Coptic: " (calendar-coptic-date-string date) "\n"
9525 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9526 "Persian: " (calendar-persian-date-string date) "\n"
9527 "Chinese: " (calendar-chinese-date-string date) "\n"))
9528 (with-output-to-temp-buffer "*Dates*"
9529 (princ s))
9530 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9532 ;;; Bulk commands
9534 (defun org-agenda-bulk-marked-p ()
9535 (eq (get-char-property (point-at-bol) 'type)
9536 'org-marked-entry-overlay))
9538 (defun org-agenda-bulk-mark (&optional arg)
9539 "Mark the entry at point for future bulk action."
9540 (interactive "p")
9541 (dotimes (i (or arg 1))
9542 (unless (org-get-at-bol 'org-agenda-diary-link)
9543 (let* ((m (org-get-at-bol 'org-hd-marker))
9545 (unless (org-agenda-bulk-marked-p)
9546 (unless m (error "Nothing to mark at point"))
9547 (push m org-agenda-bulk-marked-entries)
9548 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9549 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9550 (org-get-todo-face "TODO")
9551 'evaporate)
9552 (overlay-put ov 'type 'org-marked-entry-overlay))
9553 (beginning-of-line 2)
9554 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9555 (beginning-of-line 2))
9556 (message "%d entries marked for bulk action"
9557 (length org-agenda-bulk-marked-entries))))))
9559 (defun org-agenda-bulk-mark-all ()
9560 "Mark all entries for future agenda bulk action."
9561 (interactive)
9562 (org-agenda-bulk-mark-regexp "."))
9564 (defun org-agenda-bulk-mark-regexp (regexp)
9565 "Mark entries matching REGEXP for future agenda bulk action."
9566 (interactive "sMark entries matching regexp: ")
9567 (let ((entries-marked 0))
9568 (save-excursion
9569 (goto-char (point-min))
9570 (goto-char (next-single-property-change (point) 'txt))
9571 (while (re-search-forward regexp nil t)
9572 (when (string-match regexp (get-text-property (point) 'txt))
9573 (setq entries-marked (1+ entries-marked))
9574 (call-interactively 'org-agenda-bulk-mark))))
9575 (if (not entries-marked)
9576 (message "No entry matching this regexp."))))
9578 (defun org-agenda-bulk-unmark (&optional arg)
9579 "Unmark the entry at point for future bulk action."
9580 (interactive "P")
9581 (if arg
9582 (org-agenda-bulk-unmark-all)
9583 (cond ((org-agenda-bulk-marked-p)
9584 (org-agenda-bulk-remove-overlays
9585 (point-at-bol) (+ 2 (point-at-bol)))
9586 (setq org-agenda-bulk-marked-entries
9587 (delete (org-get-at-bol 'org-hd-marker)
9588 org-agenda-bulk-marked-entries))
9589 (beginning-of-line 2)
9590 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9591 (beginning-of-line 2))
9592 (message "%d entries left marked for bulk action"
9593 (length org-agenda-bulk-marked-entries)))
9594 (t (message "No entry to unmark here")))))
9596 (defun org-agenda-bulk-toggle ()
9597 "Toggle marking the entry at point for bulk action."
9598 (interactive)
9599 (if (org-agenda-bulk-marked-p)
9600 (org-agenda-bulk-unmark)
9601 (org-agenda-bulk-mark)))
9603 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9604 "Remove the mark overlays between BEG and END in the agenda buffer.
9605 BEG and END default to the buffer limits.
9607 This only removes the overlays, it does not remove the markers
9608 from the list in `org-agenda-bulk-marked-entries'."
9609 (interactive)
9610 (mapc (lambda (ov)
9611 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9612 (delete-overlay ov)))
9613 (overlays-in (or beg (point-min)) (or end (point-max)))))
9615 (defun org-agenda-bulk-unmark-all ()
9616 "Remove all marks in the agenda buffer.
9617 This will remove the markers and the overlays."
9618 (interactive)
9619 (if (null org-agenda-bulk-marked-entries)
9620 (message "No entry to unmark")
9621 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9622 (setq org-agenda-bulk-marked-entries nil)
9623 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9625 (defcustom org-agenda-persistent-marks nil
9626 "Non-nil means marked items will stay marked after a bulk action.
9627 You can toggle this interactively by typing `p' when prompted for a
9628 bulk action."
9629 :group 'org-agenda
9630 :version "24.1"
9631 :type 'boolean)
9633 (defun org-agenda-bulk-action (&optional arg)
9634 "Execute an remote-editing action on all marked entries.
9635 The prefix arg is passed through to the command if possible."
9636 (interactive "P")
9637 ;; Make sure we have markers, and only valid ones
9638 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
9639 (mapc
9640 (lambda (m)
9641 (unless (and (markerp m)
9642 (marker-buffer m)
9643 (buffer-live-p (marker-buffer m))
9644 (marker-position m))
9645 (error "Marker %s for bulk command is invalid" m)))
9646 org-agenda-bulk-marked-entries)
9648 ;; Prompt for the bulk command
9649 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9650 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9651 "[S]catter [f]unction "
9652 (when org-agenda-bulk-custom-functions
9653 (concat " Custom: ["
9654 (mapconcat (lambda(f) (char-to-string (car f)))
9655 org-agenda-bulk-custom-functions "")
9656 "]"))))
9657 (catch 'exit
9658 (let* ((action (read-char-exclusive))
9659 (org-log-refile (if org-log-refile 'time nil))
9660 (entries (reverse org-agenda-bulk-marked-entries))
9661 (org-overriding-default-time
9662 (if (get-text-property (point) 'org-agenda-date-header)
9663 (org-get-cursor-date)))
9664 redo-at-end
9665 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9666 (cond
9667 ((equal action ?p)
9668 (let ((org-agenda-persistent-marks
9669 (not org-agenda-persistent-marks)))
9670 (org-agenda-bulk-action)
9671 (throw 'exit nil)))
9673 ((equal action ?$)
9674 (setq cmd '(org-agenda-archive)))
9676 ((equal action ?A)
9677 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9679 ((member action '(?r ?w))
9680 (setq rfloc (org-refile-get-location
9681 "Refile to"
9682 (marker-buffer (car entries))
9683 org-refile-allow-creating-parent-nodes))
9684 (if (nth 3 rfloc)
9685 (setcar (nthcdr 3 rfloc)
9686 (move-marker (make-marker) (nth 3 rfloc)
9687 (or (get-file-buffer (nth 1 rfloc))
9688 (find-buffer-visiting (nth 1 rfloc))
9689 (error "This should not happen")))))
9691 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9692 redo-at-end t))
9694 ((equal action ?t)
9695 (setq state (org-icompleting-read
9696 "Todo state: "
9697 (with-current-buffer (marker-buffer (car entries))
9698 (mapcar 'list org-todo-keywords-1))))
9699 (setq cmd `(let ((org-inhibit-blocking t)
9700 (org-inhibit-logging 'note))
9701 (org-agenda-todo ,state))))
9703 ((memq action '(?- ?+))
9704 (setq tag (org-icompleting-read
9705 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9706 (with-current-buffer (marker-buffer (car entries))
9707 (delq nil
9708 (mapcar (lambda (x)
9709 (if (stringp (car x)) x)) org-tag-alist)))))
9710 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9712 ((memq action '(?s ?d))
9713 (let* ((time
9714 (unless arg
9715 (org-read-date
9716 nil nil nil
9717 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9718 org-overriding-default-time)))
9719 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9720 (setq cmd `(eval '(,c1 arg ,time)))))
9722 ((equal action ?S)
9723 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9724 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9725 (let ((days (read-number
9726 (format "Scatter tasks across how many %sdays: "
9727 (if arg "week" "")) 7)))
9728 (setq cmd
9729 `(let ((distance (1+ (random ,days))))
9730 (if arg
9731 (let ((dist distance)
9732 (day-of-week
9733 (calendar-day-of-week
9734 (calendar-gregorian-from-absolute (org-today)))))
9735 (dotimes (i (1+ dist))
9736 (while (member day-of-week org-agenda-weekend-days)
9737 (incf distance)
9738 (incf day-of-week)
9739 (if (= day-of-week 7)
9740 (setq day-of-week 0)))
9741 (incf day-of-week)
9742 (if (= day-of-week 7)
9743 (setq day-of-week 0)))))
9744 ;; silently fail when try to replan a sexp entry
9745 (condition-case nil
9746 (let* ((date (calendar-gregorian-from-absolute
9747 (+ (org-today) distance)))
9748 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9749 (nth 2 date))))
9750 (org-agenda-schedule nil time))
9751 (error nil)))))))
9753 ((assoc action org-agenda-bulk-custom-functions)
9754 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9755 redo-at-end t))
9757 ((equal action ?f)
9758 (setq cmd (list (intern
9759 (org-icompleting-read "Function: "
9760 obarray 'fboundp t nil nil)))))
9762 (t (error "Invalid bulk action")))
9764 ;; Sort the markers, to make sure that parents are handled before children
9765 (setq entries (sort entries
9766 (lambda (a b)
9767 (cond
9768 ((equal (marker-buffer a) (marker-buffer b))
9769 (< (marker-position a) (marker-position b)))
9771 (string< (buffer-name (marker-buffer a))
9772 (buffer-name (marker-buffer b))))))))
9774 ;; Now loop over all markers and apply cmd
9775 (while (setq e (pop entries))
9776 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9777 (if (not pos)
9778 (progn (message "Skipping removed entry at %s" e)
9779 (setq cntskip (1+ cntskip)))
9780 (goto-char pos)
9781 (let (org-loop-over-headlines-in-active-region)
9782 (eval cmd))
9783 (setq cnt (1+ cnt))))
9784 (when redo-at-end (org-agenda-redo))
9785 (unless org-agenda-persistent-marks
9786 (org-agenda-bulk-unmark-all))
9787 (message "Acted on %d entries%s%s"
9789 (if (= cntskip 0)
9791 (format ", skipped %d (disappeared before their turn)"
9792 cntskip))
9793 (if (not org-agenda-persistent-marks)
9794 "" " (kept marked)"))))))
9796 (defun org-agenda-capture (&optional with-time)
9797 "Call `org-capture' with the date at point.
9798 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9799 current HH:MM time."
9800 (interactive "P")
9801 (if (not (eq major-mode 'org-agenda-mode))
9802 (user-error "You cannot do this outside of agenda buffers")
9803 (let ((org-overriding-default-time
9804 (org-get-cursor-date (equal with-time 1))))
9805 (call-interactively 'org-capture))))
9807 ;;; Flagging notes
9809 (defun org-agenda-show-the-flagging-note ()
9810 "Display the flagging note in the other window.
9811 When called a second time in direct sequence, offer to remove the FLAGGING
9812 tag and (if present) the flagging note."
9813 (interactive)
9814 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9815 (win (selected-window))
9816 note heading newhead)
9817 (unless hdmarker
9818 (error "No linked entry at point"))
9819 (if (and (eq this-command last-command)
9820 (y-or-n-p "Unflag and remove any flagging note? "))
9821 (progn
9822 (org-agenda-remove-flag hdmarker)
9823 (let ((win (get-buffer-window "*Flagging Note*")))
9824 (and win (delete-window win)))
9825 (message "Entry unflagged"))
9826 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9827 (unless note
9828 (error "No flagging note"))
9829 (org-kill-new note)
9830 (org-switch-to-buffer-other-window "*Flagging Note*")
9831 (erase-buffer)
9832 (insert note)
9833 (goto-char (point-min))
9834 (while (re-search-forward "\\\\n" nil t)
9835 (replace-match "\n" t t))
9836 (goto-char (point-min))
9837 (select-window win)
9838 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9840 (defun org-agenda-remove-flag (marker)
9841 "Remove the FLAGGED tag and any flagging note in the entry."
9842 (let (newhead)
9843 (org-with-point-at marker
9844 (org-toggle-tag "FLAGGED" 'off)
9845 (org-entry-delete nil "THEFLAGGINGNOTE")
9846 (setq newhead (org-get-heading)))
9847 (org-agenda-change-all-lines newhead marker)
9848 (message "Entry unflagged")))
9850 (defun org-agenda-get-any-marker (&optional pos)
9851 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9852 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9854 ;;; Appointment reminders
9856 (defvar appt-time-msg-list) ; defined in appt.el
9858 ;;;###autoload
9859 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9860 "Activate appointments found in `org-agenda-files'.
9861 With a \\[universal-argument] prefix, refresh the list of
9862 appointments.
9864 If FILTER is t, interactively prompt the user for a regular
9865 expression, and filter out entries that don't match it.
9867 If FILTER is a string, use this string as a regular expression
9868 for filtering entries out.
9870 If FILTER is a function, filter out entries against which
9871 calling the function returns nil. This function takes one
9872 argument: an entry from `org-agenda-get-day-entries'.
9874 FILTER can also be an alist with the car of each cell being
9875 either 'headline or 'category. For example:
9877 '((headline \"IMPORTANT\")
9878 (category \"Work\"))
9880 will only add headlines containing IMPORTANT or headlines
9881 belonging to the \"Work\" category.
9883 ARGS are symbols indicating what kind of entries to consider.
9884 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
9885 \(i.e., deadlines and scheduled items with a hh:mm specification)
9886 and :timestamp entries. See the docstring of `org-diary' for
9887 details and examples.
9889 If an entry has a APPT_WARNTIME property, its value will be used
9890 to override `appt-message-warning-time'."
9891 (interactive "P")
9892 (if refresh (setq appt-time-msg-list nil))
9893 (if (eq filter t)
9894 (setq filter (read-from-minibuffer "Regexp filter: ")))
9895 (let* ((cnt 0) ; count added events
9896 (scope (or args '(:deadline* :scheduled* :timestamp)))
9897 (org-agenda-new-buffers nil)
9898 (org-deadline-warning-days 0)
9899 ;; Do not use `org-today' here because appt only takes
9900 ;; time and without date as argument, so it may pass wrong
9901 ;; information otherwise
9902 (today (org-date-to-gregorian
9903 (time-to-days (current-time))))
9904 (org-agenda-restrict nil)
9905 (files (org-agenda-files 'unrestricted)) entries file
9906 (org-agenda-buffer nil))
9907 ;; Get all entries which may contain an appt
9908 (org-agenda-prepare-buffers files)
9909 (while (setq file (pop files))
9910 (setq entries
9911 (delq nil
9912 (append entries
9913 (apply 'org-agenda-get-day-entries
9914 file today scope)))))
9915 ;; Map thru entries and find if we should filter them out
9916 (mapc
9917 (lambda(x)
9918 (let* ((evt (org-trim
9919 (replace-regexp-in-string
9920 org-bracket-link-regexp "\\3"
9921 (or (get-text-property 1 'txt x) ""))))
9922 (cat (get-text-property 1 'org-category x))
9923 (tod (get-text-property 1 'time-of-day x))
9924 (ok (or (null filter)
9925 (and (stringp filter) (string-match filter evt))
9926 (and (functionp filter) (funcall filter x))
9927 (and (listp filter)
9928 (let ((cat-filter (cadr (assoc 'category filter)))
9929 (evt-filter (cadr (assoc 'headline filter))))
9930 (or (and (stringp cat-filter)
9931 (string-match cat-filter cat))
9932 (and (stringp evt-filter)
9933 (string-match evt-filter evt)))))))
9934 (wrn (get-text-property 1 'warntime x)))
9935 ;; FIXME: Shall we remove text-properties for the appt text?
9936 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
9937 (when (and ok tod)
9938 (setq tod (concat "00" (number-to-string tod))
9939 tod (when (string-match
9940 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
9941 (concat (match-string 1 tod) ":"
9942 (match-string 2 tod))))
9943 (if (version< emacs-version "23.3")
9944 (appt-add tod evt)
9945 (appt-add tod evt wrn))
9946 (setq cnt (1+ cnt))))) entries)
9947 (org-release-buffers org-agenda-new-buffers)
9948 (if (eq cnt 0)
9949 (message "No event to add")
9950 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
9952 (defun org-agenda-todayp (date)
9953 "Does DATE mean today, when considering `org-extend-today-until'?"
9954 (let ((today (org-today))
9955 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
9956 date)))
9957 (eq date today)))
9959 (defun org-agenda-todo-yesterday (&optional arg)
9960 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
9961 (interactive "P")
9962 (let* ((hour (third (decode-time
9963 (org-current-time))))
9964 (org-extend-today-until (1+ hour)))
9965 (org-agenda-todo arg)))
9967 (provide 'org-agenda)
9969 ;;; org-agenda.el ends here