org-agenda.el: (org-agenda-bulk-toggle-all): New command
[org-mode.git] / lisp / org-agenda.el
blobf13e6ede151f206e5c7c8121c09ba7fc6729a123
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-restriction-lock-highlight-subtree t
661 "Non-nil means highlight the whole subtree when restriction is active.
662 Otherwise only highlight the headline. Highlighting the whole subtree is
663 useful to ensure no edits happen beyond the restricted region."
664 :group 'org-agenda
665 :type 'boolean)
667 (defcustom org-agenda-skip-comment-trees t
668 "Non-nil means skip trees that start with the COMMENT keyword.
669 When nil, these trees are also scanned by agenda commands."
670 :group 'org-agenda-skip
671 :type 'boolean)
673 (defcustom org-agenda-todo-list-sublevels t
674 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
675 When nil, the sublevels of a TODO entry are not checked, resulting in
676 potentially much shorter TODO lists."
677 :group 'org-agenda-skip
678 :group 'org-agenda-todo-list
679 :type 'boolean)
681 (defcustom org-agenda-todo-ignore-with-date nil
682 "Non-nil means don't show entries with a date in the global todo list.
683 You can use this if you prefer to mark mere appointments with a TODO keyword,
684 but don't want them to show up in the TODO list.
685 When this is set, it also covers deadlines and scheduled items, the settings
686 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
687 will be ignored.
688 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
689 :group 'org-agenda-skip
690 :group 'org-agenda-todo-list
691 :type 'boolean)
693 (defcustom org-agenda-todo-ignore-timestamp nil
694 "Non-nil means don't show entries with a timestamp.
695 This applies when creating the global todo list.
696 Valid values are:
698 past Don't show entries for today or in the past.
700 future Don't show entries with a timestamp in the future.
701 The idea behind this is that if it has a future
702 timestamp, you don't want to think about it until the
703 date.
705 all Don't show any entries with a timestamp in the global todo list.
706 The idea behind this is that by setting a timestamp, you
707 have already \"taken care\" of this item.
709 This variable can also have an integer as a value. If positive (N),
710 todos with a timestamp N or more days in the future will be ignored. If
711 negative (-N), todos with a timestamp N or more days in the past will be
712 ignored. If 0, todos with a timestamp either today or in the future will
713 be ignored. For example, a value of -1 will exclude todos with a
714 timestamp in the past (yesterday or earlier), while a value of 7 will
715 exclude todos with a timestamp a week or more in the future.
717 See also `org-agenda-todo-ignore-with-date'.
718 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
719 to make his option also apply to the tags-todo list."
720 :group 'org-agenda-skip
721 :group 'org-agenda-todo-list
722 :version "24.1"
723 :type '(choice
724 (const :tag "Ignore future timestamp todos" future)
725 (const :tag "Ignore past or present timestamp todos" past)
726 (const :tag "Ignore all timestamp todos" all)
727 (const :tag "Show timestamp todos" nil)
728 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
730 (defcustom org-agenda-todo-ignore-scheduled nil
731 "Non-nil means, ignore some scheduled TODO items when making TODO list.
732 This applies when creating the global todo list.
733 Valid values are:
735 past Don't show entries scheduled today or in the past.
737 future Don't show entries scheduled in the future.
738 The idea behind this is that by scheduling it, you don't want to
739 think about it until the scheduled date.
741 all Don't show any scheduled entries in the global todo list.
742 The idea behind this is that by scheduling it, you have already
743 \"taken care\" of this item.
745 t Same as `all', for backward compatibility.
747 This variable can also have an integer as a value. See
748 `org-agenda-todo-ignore-timestamp' for more details.
750 See also `org-agenda-todo-ignore-with-date'.
751 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
752 to make his option also apply to the tags-todo list."
753 :group 'org-agenda-skip
754 :group 'org-agenda-todo-list
755 :type '(choice
756 (const :tag "Ignore future-scheduled todos" future)
757 (const :tag "Ignore past- or present-scheduled todos" past)
758 (const :tag "Ignore all scheduled todos" all)
759 (const :tag "Ignore all scheduled todos (compatibility)" t)
760 (const :tag "Show scheduled todos" nil)
761 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
763 (defcustom org-agenda-todo-ignore-deadlines nil
764 "Non-nil means ignore some deadlined TODO items when making TODO list.
765 There are different motivations for using different values, please think
766 carefully when configuring this variable.
768 This applies when creating the global todo list.
769 Valid values are:
771 near Don't show near deadline entries. A deadline is near when it is
772 closer than `org-deadline-warning-days' days. The idea behind this
773 is that such items will appear in the agenda anyway.
775 far Don't show TODO entries where a deadline has been defined, but
776 the deadline is not near. This is useful if you don't want to
777 use the todo list to figure out what to do now.
779 past Don't show entries with a deadline timestamp for today or in the past.
781 future Don't show entries with a deadline timestamp in the future, not even
782 when they become `near' ones. Use it with caution.
784 all Ignore all TODO entries that do have a deadline.
786 t Same as `near', for backward compatibility.
788 This variable can also have an integer as a value. See
789 `org-agenda-todo-ignore-timestamp' for more details.
791 See also `org-agenda-todo-ignore-with-date'.
792 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
793 to make his option also apply to the tags-todo list."
794 :group 'org-agenda-skip
795 :group 'org-agenda-todo-list
796 :type '(choice
797 (const :tag "Ignore near deadlines" near)
798 (const :tag "Ignore near deadlines (compatibility)" t)
799 (const :tag "Ignore far deadlines" far)
800 (const :tag "Ignore all TODOs with a deadlines" all)
801 (const :tag "Show all TODOs, even if they have a deadline" nil)
802 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
804 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
805 "Time unit to use when possibly ignoring an agenda item.
807 See the docstring of various `org-agenda-todo-ignore-*' options.
808 The default is to compare time stamps using days. An item is thus
809 considered to be in the future if it is at least one day after today.
810 Non-nil means to compare time stamps using seconds. An item is then
811 considered future if it has a time value later than current time."
812 :group 'org-agenda-skip
813 :group 'org-agenda-todo-list
814 :version "24.4"
815 :package-version '(Org . "8.0")
816 :type '(choice
817 (const :tag "Compare time with days" nil)
818 (const :tag "Compare time with seconds" t)))
820 (defcustom org-agenda-tags-todo-honor-ignore-options nil
821 "Non-nil means honor todo-list ignores options also in tags-todo search.
822 The variables
823 `org-agenda-todo-ignore-with-date',
824 `org-agenda-todo-ignore-timestamp',
825 `org-agenda-todo-ignore-scheduled',
826 `org-agenda-todo-ignore-deadlines'
827 make the global TODO list skip entries that have time stamps of certain
828 kinds. If this option is set, the same options will also apply for the
829 tags-todo search, which is the general tags/property matcher
830 restricted to unfinished TODO entries only."
831 :group 'org-agenda-skip
832 :group 'org-agenda-todo-list
833 :group 'org-agenda-match-view
834 :type 'boolean)
836 (defcustom org-agenda-skip-scheduled-if-done nil
837 "Non-nil means don't show scheduled items in agenda when they are done.
838 This is relevant for the daily/weekly agenda, not for the TODO list. And
839 it applies only to the actual date of the scheduling. Warnings about
840 an item with a past scheduling dates are always turned off when the item
841 is DONE."
842 :group 'org-agenda-skip
843 :group 'org-agenda-daily/weekly
844 :type 'boolean)
846 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
847 "Non-nil means skip scheduling line if same entry shows because of deadline.
849 In the agenda of today, an entry can show up multiple times
850 because it is both scheduled and has a nearby deadline, and maybe
851 a plain time stamp as well.
853 When this variable is nil, the entry will be shown several times.
855 When set to t, then only the deadline is shown and the fact that
856 the entry is scheduled today or was scheduled previously is not
857 shown.
859 When set to the symbol `not-today', skip scheduled previously,
860 but not scheduled today.
862 When set to the symbol `repeated-after-deadline', skip scheduled
863 items if they are repeated beyond the current dealine."
864 :group 'org-agenda-skip
865 :group 'org-agenda-daily/weekly
866 :type '(choice
867 (const :tag "Never" nil)
868 (const :tag "Always" t)
869 (const :tag "Not when scheduled today" not-today)
870 (const :tag "When repeated past deadline" repeated-after-deadline)))
872 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
873 "Non-nil means skip timestamp line if same entry shows because of deadline.
874 In the agenda of today, an entry can show up multiple times
875 because it has both a plain timestamp and has a nearby deadline.
876 When this variable is t, then only the deadline is shown and the
877 fact that the entry has a timestamp for or including today is not
878 shown. When this variable is nil, the entry will be shown
879 several times."
880 :group 'org-agenda-skip
881 :group 'org-agenda-daily/weekly
882 :version "24.1"
883 :type '(choice
884 (const :tag "Never" nil)
885 (const :tag "Always" t)))
887 (defcustom org-agenda-skip-deadline-if-done nil
888 "Non-nil means don't show deadlines when the corresponding item is done.
889 When nil, the deadline is still shown and should give you a happy feeling.
890 This is relevant for the daily/weekly agenda. And it applied only to the
891 actually date of the deadline. Warnings about approaching and past-due
892 deadlines are always turned off when the item is DONE."
893 :group 'org-agenda-skip
894 :group 'org-agenda-daily/weekly
895 :type 'boolean)
897 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
898 "Non-nil means skip deadline prewarning when entry is also scheduled.
899 This will apply on all days where a prewarning for the deadline would
900 be shown, but not at the day when the entry is actually due. On that day,
901 the deadline will be shown anyway.
902 This variable may be set to nil, t, the symbol `pre-scheduled',
903 or a number which will then give the number of days before the actual
904 deadline when the prewarnings should resume. The symbol `pre-scheduled'
905 eliminates the deadline prewarning only prior to the scheduled date.
906 This can be used in a workflow where the first showing of the deadline will
907 trigger you to schedule it, and then you don't want to be reminded of it
908 because you will take care of it on the day when scheduled."
909 :group 'org-agenda-skip
910 :group 'org-agenda-daily/weekly
911 :version "24.1"
912 :type '(choice
913 (const :tag "Always show prewarning" nil)
914 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
915 (const :tag "Remove prewarning if entry is scheduled" t)
916 (integer :tag "Restart prewarning N days before deadline")))
918 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
919 "Non-nil means skip scheduled delay when entry also has a deadline.
920 This variable may be set to nil, t, the symbol `post-deadline',
921 or a number which will then give the number of days after the actual
922 scheduled date when the delay should expire. The symbol `post-deadline'
923 eliminates the schedule delay when the date is posterior to the deadline."
924 :group 'org-agenda-skip
925 :group 'org-agenda-daily/weekly
926 :version "24.4"
927 :package-version '(Org . "8.0")
928 :type '(choice
929 (const :tag "Always honor delay" nil)
930 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
931 (const :tag "Ignore delay if entry has a deadline" t)
932 (integer :tag "Honor delay up until N days after the scheduled date")))
934 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
935 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
936 When non-nil, after the search for timestamps has matched once in an
937 entry, the rest of the entry will not be searched."
938 :group 'org-agenda-skip
939 :type 'boolean)
941 (defcustom org-agenda-skip-timestamp-if-done nil
942 "Non-nil means don't select item by timestamp or -range if it is DONE."
943 :group 'org-agenda-skip
944 :group 'org-agenda-daily/weekly
945 :type 'boolean)
947 (defcustom org-agenda-dim-blocked-tasks t
948 "Non-nil means dim blocked tasks in the agenda display.
949 This causes some overhead during agenda construction, but if you
950 have turned on `org-enforce-todo-dependencies',
951 `org-enforce-todo-checkbox-dependencies', or any other blocking
952 mechanism, this will create useful feedback in the agenda.
954 Instead of t, this variable can also have the value `invisible'.
955 Then blocked tasks will be invisible and only become visible when
956 they become unblocked. An exemption to this behavior is when a task is
957 blocked because of unchecked checkboxes below it. Since checkboxes do
958 not show up in the agenda views, making this task invisible you remove any
959 trace from agenda views that there is something to do. Therefore, a task
960 that is blocked because of checkboxes will never be made invisible, it
961 will only be dimmed."
962 :group 'org-agenda-daily/weekly
963 :group 'org-agenda-todo-list
964 :version "24.3"
965 :type '(choice
966 (const :tag "Do not dim" nil)
967 (const :tag "Dim to a gray face" t)
968 (const :tag "Make invisible" invisible)))
970 (defcustom org-timeline-show-empty-dates 3
971 "Non-nil means `org-timeline' also shows dates without an entry.
972 When nil, only the days which actually have entries are shown.
973 When t, all days between the first and the last date are shown.
974 When an integer, show also empty dates, but if there is a gap of more than
975 N days, just insert a special line indicating the size of the gap."
976 :group 'org-agenda-skip
977 :type '(choice
978 (const :tag "None" nil)
979 (const :tag "All" t)
980 (integer :tag "at most")))
982 (defgroup org-agenda-startup nil
983 "Options concerning initial settings in the Agenda in Org Mode."
984 :tag "Org Agenda Startup"
985 :group 'org-agenda)
987 (defcustom org-agenda-menu-show-matcher t
988 "Non-nil means show the match string in the agenda dispatcher menu.
989 When nil, the matcher string is not shown, but is put into the help-echo
990 property so than moving the mouse over the command shows it.
991 Setting it to nil is good if matcher strings are very long and/or if
992 you want to use two-columns display (see `org-agenda-menu-two-columns')."
993 :group 'org-agenda
994 :version "24.1"
995 :type 'boolean)
997 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
999 (defcustom org-agenda-menu-two-columns nil
1000 "Non-nil means, use two columns to show custom commands in the dispatcher.
1001 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1002 to nil."
1003 :group 'org-agenda
1004 :version "24.1"
1005 :type 'boolean)
1007 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1008 (defcustom org-agenda-finalize-hook nil
1009 "Hook run just before displaying an agenda buffer.
1010 The buffer is still writable when the hook is called.
1012 You can modify some of the buffer substrings but you should be
1013 extra careful not to modify the text properties of the agenda
1014 headlines as the agenda display heavily relies on them."
1015 :group 'org-agenda-startup
1016 :type 'hook)
1018 (defcustom org-agenda-mouse-1-follows-link nil
1019 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1020 A longer mouse click will still set point. Does not work on XEmacs.
1021 Needs to be set before org.el is loaded."
1022 :group 'org-agenda-startup
1023 :type 'boolean)
1025 (defcustom org-agenda-start-with-follow-mode nil
1026 "The initial value of follow mode in a newly created agenda window."
1027 :group 'org-agenda-startup
1028 :type 'boolean)
1030 (defcustom org-agenda-follow-indirect nil
1031 "Non-nil means `org-agenda-follow-mode' displays only the
1032 current item's tree, in an indirect buffer."
1033 :group 'org-agenda
1034 :version "24.1"
1035 :type 'boolean)
1037 (defcustom org-agenda-show-outline-path t
1038 "Non-nil means show outline path in echo area after line motion."
1039 :group 'org-agenda-startup
1040 :type 'boolean)
1042 (defcustom org-agenda-start-with-entry-text-mode nil
1043 "The initial value of entry-text-mode in a newly created agenda window."
1044 :group 'org-agenda-startup
1045 :type 'boolean)
1047 (defcustom org-agenda-entry-text-maxlines 5
1048 "Number of text lines to be added when `E' is pressed in the agenda.
1050 Note that this variable only used during agenda display. Add add entry text
1051 when exporting the agenda, configure the variable
1052 `org-agenda-add-entry-ext-maxlines'."
1053 :group 'org-agenda
1054 :type 'integer)
1056 (defcustom org-agenda-entry-text-exclude-regexps nil
1057 "List of regular expressions to clean up entry text.
1058 The complete matches of all regular expressions in this list will be
1059 removed from entry text before it is shown in the agenda."
1060 :group 'org-agenda
1061 :type '(repeat (regexp)))
1063 (defcustom org-agenda-entry-text-leaders " > "
1064 "Text prepended to the entry text in agenda buffers."
1065 :version "24.4"
1066 :package-version '(Org . "8.0")
1067 :group 'org-agenda
1068 :type 'string)
1070 (defvar org-agenda-entry-text-cleanup-hook nil
1071 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1072 This cleanup is done in a temporary buffer, so the function may inspect and
1073 change the entire buffer.
1074 Some default stuff like drawers and scheduling/deadline dates will already
1075 have been removed when this is called, as will any matches for regular
1076 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1078 (defvar org-agenda-include-inactive-timestamps nil
1079 "Non-nil means include inactive time stamps in agenda and timeline.
1080 Dynamically scoped.")
1082 (defgroup org-agenda-windows nil
1083 "Options concerning the windows used by the Agenda in Org Mode."
1084 :tag "Org Agenda Windows"
1085 :group 'org-agenda)
1087 (defcustom org-agenda-window-setup 'reorganize-frame
1088 "How the agenda buffer should be displayed.
1089 Possible values for this option are:
1091 current-window Show agenda in the current window, keeping all other windows.
1092 other-window Use `switch-to-buffer-other-window' to display agenda.
1093 reorganize-frame Show only two windows on the current frame, the current
1094 window and the agenda.
1095 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1096 Also, when exiting the agenda, kill that frame.
1097 See also the variable `org-agenda-restore-windows-after-quit'."
1098 :group 'org-agenda-windows
1099 :type '(choice
1100 (const current-window)
1101 (const other-frame)
1102 (const other-window)
1103 (const reorganize-frame)))
1105 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1106 "The min and max height of the agenda window as a fraction of frame height.
1107 The value of the variable is a cons cell with two numbers between 0 and 1.
1108 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1109 :group 'org-agenda-windows
1110 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1112 (defcustom org-agenda-restore-windows-after-quit nil
1113 "Non-nil means restore window configuration upon exiting agenda.
1114 Before the window configuration is changed for displaying the agenda,
1115 the current status is recorded. When the agenda is exited with
1116 `q' or `x' and this option is set, the old state is restored. If
1117 `org-agenda-window-setup' is `other-frame', the value of this
1118 option will be ignored."
1119 :group 'org-agenda-windows
1120 :type 'boolean)
1122 (defcustom org-agenda-ndays nil
1123 "Number of days to include in overview display.
1124 Should be 1 or 7.
1125 Obsolete, see `org-agenda-span'."
1126 :group 'org-agenda-daily/weekly
1127 :type 'integer)
1129 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1131 (defcustom org-agenda-span 'week
1132 "Number of days to include in overview display.
1133 Can be day, week, month, year, or any number of days.
1134 Custom commands can set this variable in the options section."
1135 :group 'org-agenda-daily/weekly
1136 :type '(choice (const :tag "Day" day)
1137 (const :tag "Week" week)
1138 (const :tag "Month" month)
1139 (const :tag "Year" year)
1140 (integer :tag "Custom")))
1142 (defcustom org-agenda-start-on-weekday 1
1143 "Non-nil means start the overview always on the specified weekday.
1144 0 denotes Sunday, 1 denotes Monday, etc.
1145 When nil, always start on the current day.
1146 Custom commands can set this variable in the options section."
1147 :group 'org-agenda-daily/weekly
1148 :type '(choice (const :tag "Today" nil)
1149 (integer :tag "Weekday No.")))
1151 (defcustom org-agenda-show-all-dates t
1152 "Non-nil means `org-agenda' shows every day in the selected range.
1153 When nil, only the days which actually have entries are shown."
1154 :group 'org-agenda-daily/weekly
1155 :type 'boolean)
1157 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1158 "Format string for displaying dates in the agenda.
1159 Used by the daily/weekly agenda and by the timeline. This should be
1160 a format string understood by `format-time-string', or a function returning
1161 the formatted date as a string. The function must take a single argument,
1162 a calendar-style date list like (month day year)."
1163 :group 'org-agenda-daily/weekly
1164 :type '(choice
1165 (string :tag "Format string")
1166 (function :tag "Function")))
1168 (defun org-agenda-format-date-aligned (date)
1169 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1170 This function makes sure that dates are aligned for easy reading."
1171 (require 'cal-iso)
1172 (let* ((dayname (calendar-day-name date))
1173 (day (cadr date))
1174 (day-of-week (calendar-day-of-week date))
1175 (month (car date))
1176 (monthname (calendar-month-name month))
1177 (year (nth 2 date))
1178 (iso-week (org-days-to-iso-week
1179 (calendar-absolute-from-gregorian date)))
1180 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1181 (1- year))
1182 ((and (= month 12) (<= iso-week 1))
1183 (1+ year))
1184 (t year)))
1185 (weekstring (if (= day-of-week 1)
1186 (format " W%02d" iso-week)
1187 "")))
1188 (format "%-10s %2d %s %4d%s"
1189 dayname day monthname year weekstring)))
1191 (defcustom org-agenda-time-leading-zero nil
1192 "Non-nil means use leading zero for military times in agenda.
1193 For example, 9:30am would become 09:30 rather than 9:30."
1194 :group 'org-agenda-daily/weekly
1195 :version "24.1"
1196 :type 'boolean)
1198 (defcustom org-agenda-timegrid-use-ampm nil
1199 "When set, show AM/PM style timestamps on the timegrid."
1200 :group 'org-agenda
1201 :version "24.1"
1202 :type 'boolean)
1204 (defun org-agenda-time-of-day-to-ampm (time)
1205 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1206 (let* ((hour-number (string-to-number (substring time 0 -3)))
1207 (minute (substring time -2))
1208 (ampm "am"))
1209 (cond
1210 ((equal hour-number 12)
1211 (setq ampm "pm"))
1212 ((> hour-number 12)
1213 (setq ampm "pm")
1214 (setq hour-number (- hour-number 12))))
1215 (concat
1216 (if org-agenda-time-leading-zero
1217 (format "%02d" hour-number)
1218 (format "%02s" (number-to-string hour-number)))
1219 ":" minute ampm)))
1221 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1222 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1223 (if org-agenda-timegrid-use-ampm
1224 (org-agenda-time-of-day-to-ampm time)
1225 time))
1227 (defcustom org-agenda-weekend-days '(6 0)
1228 "Which days are weekend?
1229 These days get the special face `org-agenda-date-weekend' in the agenda
1230 and timeline buffers."
1231 :group 'org-agenda-daily/weekly
1232 :type '(set :greedy t
1233 (const :tag "Monday" 1)
1234 (const :tag "Tuesday" 2)
1235 (const :tag "Wednesday" 3)
1236 (const :tag "Thursday" 4)
1237 (const :tag "Friday" 5)
1238 (const :tag "Saturday" 6)
1239 (const :tag "Sunday" 0)))
1241 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1242 "Non-nil means jump to today when moving a past date forward in time.
1243 When using S-right in the agenda to move a a date forward, and the date
1244 stamp currently points to the past, the first key press will move it
1245 to today. WHen nil, just move one day forward even if the date stays
1246 in the past."
1247 :group 'org-agenda-daily/weekly
1248 :version "24.1"
1249 :type 'boolean)
1251 (defcustom org-agenda-include-diary nil
1252 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1253 Custom commands can set this variable in the options section."
1254 :group 'org-agenda-daily/weekly
1255 :type 'boolean)
1257 (defcustom org-agenda-include-deadlines t
1258 "If non-nil, include entries within their deadline warning period.
1259 Custom commands can set this variable in the options section."
1260 :group 'org-agenda-daily/weekly
1261 :version "24.1"
1262 :type 'boolean)
1264 (defcustom org-agenda-repeating-timestamp-show-all t
1265 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1266 When set to a list of strings, only show occurrences of repeating
1267 stamps for these TODO keywords. When nil, only one occurrence is
1268 shown, either today or the nearest into the future."
1269 :group 'org-agenda-daily/weekly
1270 :type '(choice
1271 (const :tag "Show repeating stamps" t)
1272 (repeat :tag "Show repeating stamps for these TODO keywords"
1273 (string :tag "TODO Keyword"))
1274 (const :tag "Don't show repeating stamps" nil)))
1276 (defcustom org-scheduled-past-days 10000
1277 "Number of days to continue listing scheduled items not marked DONE.
1278 When an item is scheduled on a date, it shows up in the agenda on this
1279 day and will be listed until it is marked done for the number of days
1280 given here."
1281 :group 'org-agenda-daily/weekly
1282 :type 'integer)
1284 (defcustom org-agenda-log-mode-items '(closed clock)
1285 "List of items that should be shown in agenda log mode.
1286 This list may contain the following symbols:
1288 closed Show entries that have been closed on that day.
1289 clock Show entries that have received clocked time on that day.
1290 state Show all logged state changes.
1291 Note that instead of changing this variable, you can also press `C-u l' in
1292 the agenda to display all available LOG items temporarily."
1293 :group 'org-agenda-daily/weekly
1294 :type '(set :greedy t (const closed) (const clock) (const state)))
1296 (defcustom org-agenda-clock-consistency-checks
1297 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1298 :gap-ok-around ("4:00")
1299 :default-face ((:background "DarkRed") (:foreground "white"))
1300 :overlap-face nil :gap-face nil :no-end-time-face nil
1301 :long-face nil :short-face nil)
1302 "This is a property list, with the following keys:
1304 :max-duration Mark clocking chunks that are longer than this time.
1305 This is a time string like \"HH:MM\", or the number
1306 of minutes as an integer.
1308 :min-duration Mark clocking chunks that are shorter that this.
1309 This is a time string like \"HH:MM\", or the number
1310 of minutes as an integer.
1312 :max-gap Mark gaps between clocking chunks that are longer than
1313 this duration. A number of minutes, or a string
1314 like \"HH:MM\".
1316 :gap-ok-around List of times during the day which are usually not working
1317 times. When a gap is detected, but the gap contains any
1318 of these times, the gap is *not* reported. For example,
1319 if this is (\"4:00\" \"13:00\") then gaps that contain
1320 4:00 in the morning (i.e. the night) and 13:00
1321 (i.e. a typical lunch time) do not cause a warning.
1322 You should have at least one time during the night in this
1323 list, or otherwise the first task each morning will trigger
1324 a warning because it follows a long gap.
1326 Furthermore, the following properties can be used to define faces for
1327 issue display.
1329 :default-face the default face, if the specific face is undefined
1330 :overlap-face face for overlapping clocks
1331 :gap-face face for gaps between clocks
1332 :no-end-time-face face for incomplete clocks
1333 :long-face face for clock intervals that are too long
1334 :short-face face for clock intervals that are too short"
1335 :group 'org-agenda-daily/weekly
1336 :group 'org-clock
1337 :version "24.1"
1338 :type 'plist)
1340 (defcustom org-agenda-log-mode-add-notes t
1341 "Non-nil means add first line of notes to log entries in agenda views.
1342 If a log item like a state change or a clock entry is associated with
1343 notes, the first line of these notes will be added to the entry in the
1344 agenda display."
1345 :group 'org-agenda-daily/weekly
1346 :type 'boolean)
1348 (defcustom org-agenda-start-with-log-mode nil
1349 "The initial value of log-mode in a newly created agenda window.
1350 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1351 explanations on the possible values."
1352 :group 'org-agenda-startup
1353 :group 'org-agenda-daily/weekly
1354 :type '(choice (const :tag "Don't show log items" nil)
1355 (const :tag "Show only log items" 'only)
1356 (const :tag "Show all possible log items" 'clockcheck)
1357 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1358 (choice (const :tag "Show closed log items" 'closed)
1359 (const :tag "Show clocked log items" 'clock)
1360 (const :tag "Show all logged state changes" 'state)))))
1362 (defcustom org-agenda-start-with-clockreport-mode nil
1363 "The initial value of clockreport-mode in a newly created agenda window."
1364 :group 'org-agenda-startup
1365 :group 'org-agenda-daily/weekly
1366 :type 'boolean)
1368 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1369 "Property list with parameters for the clocktable in clockreport mode.
1370 This is the display mode that shows a clock table in the daily/weekly
1371 agenda, the properties for this dynamic block can be set here.
1372 The usual clocktable parameters are allowed here, but you cannot set
1373 the properties :name, :tstart, :tend, :block, and :scope - these will
1374 be overwritten to make sure the content accurately reflects the
1375 current display in the agenda."
1376 :group 'org-agenda-daily/weekly
1377 :type 'plist)
1379 (defcustom org-agenda-search-view-always-boolean nil
1380 "Non-nil means the search string is interpreted as individual parts.
1382 The search string for search view can either be interpreted as a phrase,
1383 or as a list of snippets that define a boolean search for a number of
1384 strings.
1386 When this is non-nil, the string will be split on whitespace, and each
1387 snippet will be searched individually, and all must match in order to
1388 select an entry. A snippet is then a single string of non-white
1389 characters, or a string in double quotes, or a regexp in {} braces.
1390 If a snippet is preceded by \"-\", the snippet must *not* match.
1391 \"+\" is syntactic sugar for positive selection. Each snippet may
1392 be found as a full word or a partial word, but see the variable
1393 `org-agenda-search-view-force-full-words'.
1395 When this is nil, search will look for the entire search phrase as one,
1396 with each space character matching any amount of whitespace, including
1397 line breaks.
1399 Even when this is nil, you can still switch to Boolean search dynamically
1400 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1401 is a regexp marked with braces like \"{abc}\", this will also switch to
1402 boolean search."
1403 :group 'org-agenda-search-view
1404 :version "24.1"
1405 :type 'boolean)
1407 (org-defvaralias 'org-agenda-search-view-search-words-only
1408 'org-agenda-search-view-always-boolean)
1410 (defcustom org-agenda-search-view-force-full-words nil
1411 "Non-nil means, search words must be matches as complete words.
1412 When nil, they may also match part of a word."
1413 :group 'org-agenda-search-view
1414 :version "24.1"
1415 :type 'boolean)
1417 (defcustom org-agenda-search-view-max-outline-level nil
1418 "Maximum outline level to display in search view.
1419 E.g. when this is set to 1, the search view will only
1420 show headlines of level 1."
1421 :group 'org-agenda-search-view
1422 :version "24.4"
1423 :package-version '(Org . "8.0")
1424 :type 'integer)
1426 (defgroup org-agenda-time-grid nil
1427 "Options concerning the time grid in the Org-mode Agenda."
1428 :tag "Org Agenda Time Grid"
1429 :group 'org-agenda)
1431 (defcustom org-agenda-search-headline-for-time t
1432 "Non-nil means search headline for a time-of-day.
1433 If the headline contains a time-of-day in one format or another, it will
1434 be used to sort the entry into the time sequence of items for a day.
1435 Some people have time stamps in the headline that refer to the creation
1436 time or so, and then this produces an unwanted side effect. If this is
1437 the case for your, use this variable to turn off searching the headline
1438 for a time."
1439 :group 'org-agenda-time-grid
1440 :type 'boolean)
1442 (defcustom org-agenda-use-time-grid t
1443 "Non-nil means show a time grid in the agenda schedule.
1444 A time grid is a set of lines for specific times (like every two hours between
1445 8:00 and 20:00). The items scheduled for a day at specific times are
1446 sorted in between these lines.
1447 For details about when the grid will be shown, and what it will look like, see
1448 the variable `org-agenda-time-grid'."
1449 :group 'org-agenda-time-grid
1450 :type 'boolean)
1452 (defcustom org-agenda-time-grid
1453 '((daily today require-timed)
1454 "----------------"
1455 (800 1000 1200 1400 1600 1800 2000))
1457 "The settings for time grid for agenda display.
1458 This is a list of three items. The first item is again a list. It contains
1459 symbols specifying conditions when the grid should be displayed:
1461 daily if the agenda shows a single day
1462 weekly if the agenda shows an entire week
1463 today show grid on current date, independent of daily/weekly display
1464 require-timed show grid only if at least one item has a time specification
1466 The second item is a string which will be placed behind the grid time.
1468 The third item is a list of integers, indicating the times that should have
1469 a grid line."
1470 :group 'org-agenda-time-grid
1471 :type
1472 '(list
1473 (set :greedy t :tag "Grid Display Options"
1474 (const :tag "Show grid in single day agenda display" daily)
1475 (const :tag "Show grid in weekly agenda display" weekly)
1476 (const :tag "Always show grid for today" today)
1477 (const :tag "Show grid only if any timed entries are present"
1478 require-timed)
1479 (const :tag "Skip grid times already present in an entry"
1480 remove-match))
1481 (string :tag "Grid String")
1482 (repeat :tag "Grid Times" (integer :tag "Time"))))
1484 (defcustom org-agenda-show-current-time-in-grid t
1485 "Non-nil means show the current time in the time grid."
1486 :group 'org-agenda-time-grid
1487 :version "24.1"
1488 :type 'boolean)
1490 (defcustom org-agenda-current-time-string
1491 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1492 "The string for the current time marker in the agenda."
1493 :group 'org-agenda-time-grid
1494 :version "24.1"
1495 :type 'string)
1497 (defgroup org-agenda-sorting nil
1498 "Options concerning sorting in the Org-mode Agenda."
1499 :tag "Org Agenda Sorting"
1500 :group 'org-agenda)
1502 (defcustom org-agenda-sorting-strategy
1503 '((agenda habit-down time-up priority-down category-keep)
1504 (todo priority-down category-keep)
1505 (tags priority-down category-keep)
1506 (search category-keep))
1507 "Sorting structure for the agenda items of a single day.
1508 This is a list of symbols which will be used in sequence to determine
1509 if an entry should be listed before another entry. The following
1510 symbols are recognized:
1512 time-up Put entries with time-of-day indications first, early first
1513 time-down Put entries with time-of-day indications first, late first
1514 timestamp-up Sort by any timestamp, early first
1515 timestamp-down Sort by any timestamp, late first
1516 scheduled-up Sort by scheduled timestamp, early first
1517 scheduled-down Sort by scheduled timestamp, late first
1518 deadline-up Sort by deadline timestamp, early first
1519 deadline-down Sort by deadline timestamp, late first
1520 ts-up Sort by active timestamp, early first
1521 ts-down Sort by active timestamp, late first
1522 tsia-up Sort by inactive timestamp, early first
1523 tsia-down Sort by inactive timestamp, late first
1524 category-keep Keep the default order of categories, corresponding to the
1525 sequence in `org-agenda-files'.
1526 category-up Sort alphabetically by category, A-Z.
1527 category-down Sort alphabetically by category, Z-A.
1528 tag-up Sort alphabetically by last tag, A-Z.
1529 tag-down Sort alphabetically by last tag, Z-A.
1530 priority-up Sort numerically by priority, high priority last.
1531 priority-down Sort numerically by priority, high priority first.
1532 todo-state-up Sort by todo state, tasks that are done last.
1533 todo-state-down Sort by todo state, tasks that are done first.
1534 effort-up Sort numerically by estimated effort, high effort last.
1535 effort-down Sort numerically by estimated effort, high effort first.
1536 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1537 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1538 habit-up Put entries that are habits first
1539 habit-down Put entries that are habits last
1540 alpha-up Sort headlines alphabetically
1541 alpha-down Sort headlines alphabetically, reversed
1543 The different possibilities will be tried in sequence, and testing stops
1544 if one comparison returns a \"not-equal\". For example, the default
1545 '(time-up category-keep priority-down)
1546 means: Pull out all entries having a specified time of day and sort them,
1547 in order to make a time schedule for the current day the first thing in the
1548 agenda listing for the day. Of the entries without a time indication, keep
1549 the grouped in categories, don't sort the categories, but keep them in
1550 the sequence given in `org-agenda-files'. Within each category sort by
1551 priority.
1553 Leaving out `category-keep' would mean that items will be sorted across
1554 categories by priority.
1556 Instead of a single list, this can also be a set of list for specific
1557 contents, with a context symbol in the car of the list, any of
1558 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1560 Custom commands can bind this variable in the options section."
1561 :group 'org-agenda-sorting
1562 :type `(choice
1563 (repeat :tag "General" ,org-sorting-choice)
1564 (list :tag "Individually"
1565 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1566 (repeat ,org-sorting-choice))
1567 (cons (const :tag "Strategy for TODO lists" todo)
1568 (repeat ,org-sorting-choice))
1569 (cons (const :tag "Strategy for Tags matches" tags)
1570 (repeat ,org-sorting-choice))
1571 (cons (const :tag "Strategy for search matches" search)
1572 (repeat ,org-sorting-choice)))))
1574 (defcustom org-agenda-cmp-user-defined nil
1575 "A function to define the comparison `user-defined'.
1576 This function must receive two arguments, agenda entry a and b.
1577 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1578 the user comparison, return nil.
1579 When this is defined, you can make `user-defined-up' and `user-defined-down'
1580 part of an agenda sorting strategy."
1581 :group 'org-agenda-sorting
1582 :type 'symbol)
1584 (defcustom org-sort-agenda-notime-is-late t
1585 "Non-nil means items without time are considered late.
1586 This is only relevant for sorting. When t, items which have no explicit
1587 time like 15:30 will be considered as 99:01, i.e. later than any items which
1588 do have a time. When nil, the default time is before 0:00. You can use this
1589 option to decide if the schedule for today should come before or after timeless
1590 agenda entries."
1591 :group 'org-agenda-sorting
1592 :type 'boolean)
1594 (defcustom org-sort-agenda-noeffort-is-high t
1595 "Non-nil means items without effort estimate are sorted as high effort.
1596 This also applies when filtering an agenda view with respect to the
1597 < or > effort operator. Then, tasks with no effort defined will be treated
1598 as tasks with high effort.
1599 When nil, such items are sorted as 0 minutes effort."
1600 :group 'org-agenda-sorting
1601 :type 'boolean)
1603 (defgroup org-agenda-line-format nil
1604 "Options concerning the entry prefix in the Org-mode agenda display."
1605 :tag "Org Agenda Line Format"
1606 :group 'org-agenda)
1608 (defcustom org-agenda-prefix-format
1609 '((agenda . " %i %-12:c%?-12t% s")
1610 (timeline . " % s")
1611 (todo . " %i %-12:c")
1612 (tags . " %i %-12:c")
1613 (search . " %i %-12:c"))
1614 "Format specifications for the prefix of items in the agenda views.
1615 An alist with five entries, each for the different agenda types. The
1616 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1617 The values are format strings.
1619 This format works similar to a printf format, with the following meaning:
1621 %c the category of the item, \"Diary\" for entries from the diary,
1622 or as given by the CATEGORY keyword or derived from the file name
1623 %e the effort required by the item
1624 %l the level of the item (insert X space(s) if item is of level X)
1625 %i the icon category of the item, see `org-agenda-category-icon-alist'
1626 %T the last tag of the item (ignore inherited tags, which come first)
1627 %t the HH:MM time-of-day specification if one applies to the entry
1628 %s Scheduling/Deadline information, a short string
1629 %b show breadcrumbs, i.e., the names of the higher levels
1630 %(expression) Eval EXPRESSION and replace the control string
1631 by the result
1633 All specifiers work basically like the standard `%s' of printf, but may
1634 contain two additional characters: a question mark just after the `%'
1635 and a whitespace/punctuation character just before the final letter.
1637 If the first character after `%' is a question mark, the entire field
1638 will only be included if the corresponding value applies to the current
1639 entry. This is useful for fields which should have fixed width when
1640 present, but zero width when absent. For example, \"%?-12t\" will
1641 result in a 12 character time field if a time of the day is specified,
1642 but will completely disappear in entries which do not contain a time.
1644 If there is punctuation or whitespace character just before the
1645 final format letter, this character will be appended to the field
1646 value if the value is not empty. For example, the format
1647 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1648 the category is empty, no additional colon is inserted.
1650 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1651 which means:
1653 - Indent the line with two space characters
1654 - Give the category a 12 chars wide field, padded with whitespace on
1655 the right (because of `-'). Append a colon if there is a category
1656 (because of `:').
1657 - If there is a time-of-day, put it into a 12 chars wide field. If no
1658 time, don't put in an empty field, just skip it (because of '?').
1659 - Finally, put the scheduling information.
1661 See also the variables `org-agenda-remove-times-when-in-prefix' and
1662 `org-agenda-remove-tags'.
1664 Custom commands can set this variable in the options section."
1665 :type '(choice
1666 (string :tag "General format")
1667 (list :greedy t :tag "View dependent"
1668 (cons (const agenda) (string :tag "Format"))
1669 (cons (const timeline) (string :tag "Format"))
1670 (cons (const todo) (string :tag "Format"))
1671 (cons (const tags) (string :tag "Format"))
1672 (cons (const search) (string :tag "Format"))))
1673 :group 'org-agenda-line-format)
1675 (defvar org-prefix-format-compiled nil
1676 "The compiled prefix format and associated variables.
1677 This is a list where first element is a list of variable bindings, and second
1678 element is the compiled format expression. See the variable
1679 `org-agenda-prefix-format'.")
1681 (defcustom org-agenda-todo-keyword-format "%-1s"
1682 "Format for the TODO keyword in agenda lines.
1683 Set this to something like \"%-12s\" if you want all TODO keywords
1684 to occupy a fixed space in the agenda display."
1685 :group 'org-agenda-line-format
1686 :type 'string)
1688 (defcustom org-agenda-diary-sexp-prefix nil
1689 "A regexp that matches part of a diary sexp entry
1690 which should be treated as scheduling/deadline information in
1691 `org-agenda'.
1693 For example, you can use this to extract the `diary-remind-message' from
1694 `diary-remind' entries."
1695 :group 'org-agenda-line-format
1696 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1698 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1699 "Text preceding timerange entries in the agenda view.
1700 This is a list with two strings. The first applies when the range
1701 is entirely on one day. The second applies if the range spans several days.
1702 The strings may have two \"%d\" format specifiers which will be filled
1703 with the sequence number of the days, and the total number of days in the
1704 range, respectively."
1705 :group 'org-agenda-line-format
1706 :type '(list
1707 (string :tag "Deadline today ")
1708 (choice :tag "Deadline relative"
1709 (string :tag "Format string")
1710 (function))))
1712 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1713 "Text preceding scheduled items in the agenda view.
1714 This is a list with two strings. The first applies when the item is
1715 scheduled on the current day. The second applies when it has been scheduled
1716 previously, it may contain a %d indicating that this is the nth time that
1717 this item is scheduled, due to automatic rescheduling of unfinished items
1718 for the following day. So this number is one larger than the number of days
1719 that passed since this item was scheduled first."
1720 :group 'org-agenda-line-format
1721 :version "24.4"
1722 :package-version '(Org . "8.0")
1723 :type '(list
1724 (string :tag "Scheduled today ")
1725 (string :tag "Scheduled previously")))
1727 (defcustom org-agenda-inactive-leader "["
1728 "Text preceding item pulled into the agenda by inactive time stamps.
1729 These entries are added to the agenda when pressing \"[\"."
1730 :group 'org-agenda-line-format
1731 :version "24.1"
1732 :type '(list
1733 (string :tag "Scheduled today ")
1734 (string :tag "Scheduled previously")))
1736 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1737 "Text preceding deadline items in the agenda view.
1738 This is a list with three strings. The first applies when the item has its
1739 deadline on the current day. The second applies when the deadline is in the
1740 future, the third one when it is in the past. The strings may contain %d
1741 to capture the number of days."
1742 :group 'org-agenda-line-format
1743 :version "24.4"
1744 :package-version '(Org . "8.0")
1745 :type '(list
1746 (string :tag "Deadline today ")
1747 (choice :tag "Deadline relative"
1748 (string :tag "Format string")
1749 (function))))
1751 (defcustom org-agenda-remove-times-when-in-prefix t
1752 "Non-nil means remove duplicate time specifications in agenda items.
1753 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1754 time-of-day specification in a headline or diary entry is extracted and
1755 placed into the prefix. If this option is non-nil, the original specification
1756 \(a timestamp or -range, or just a plain time(range) specification like
1757 11:30-4pm) will be removed for agenda display. This makes the agenda less
1758 cluttered.
1759 The option can be t or nil. It may also be the symbol `beg', indicating
1760 that the time should only be removed when it is located at the beginning of
1761 the headline/diary entry."
1762 :group 'org-agenda-line-format
1763 :type '(choice
1764 (const :tag "Always" t)
1765 (const :tag "Never" nil)
1766 (const :tag "When at beginning of entry" beg)))
1768 (defcustom org-agenda-remove-timeranges-from-blocks nil
1769 "Non-nil means remove time ranges specifications in agenda
1770 items that span on several days."
1771 :group 'org-agenda-line-format
1772 :version "24.1"
1773 :type 'boolean)
1775 (defcustom org-agenda-default-appointment-duration nil
1776 "Default duration for appointments that only have a starting time.
1777 When nil, no duration is specified in such cases.
1778 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1779 :group 'org-agenda-line-format
1780 :type '(choice
1781 (integer :tag "Minutes")
1782 (const :tag "No default duration")))
1784 (defcustom org-agenda-show-inherited-tags t
1785 "Non-nil means show inherited tags in each agenda line.
1787 When this option is set to 'always, it take precedences over
1788 `org-agenda-use-tag-inheritance' and inherited tags are shown
1789 in every agenda.
1791 When this option is set to t (the default), inherited tags are
1792 shown when they are available, i.e. when the value of
1793 `org-agenda-use-tag-inheritance' has been taken into account.
1795 This can be set to a list of agenda types in which the agenda
1796 must display the inherited tags. Available types are 'todo,
1797 'agenda, 'search and 'timeline.
1799 When set to nil, never show inherited tags in agenda lines."
1800 :group 'org-agenda-line-format
1801 :group 'org-agenda
1802 :version "24.3"
1803 :type '(choice
1804 (const :tag "Show inherited tags when available" t)
1805 (const :tag "Always show inherited tags" 'always)
1806 (repeat :tag "Show inherited tags only in selected agenda types"
1807 (symbol :tag "Agenda type"))))
1809 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1810 "List of agenda view types where to use tag inheritance.
1812 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1813 controlled by `org-use-tag-inheritance'. In other agenda types,
1814 `org-use-tag-inheritance' is not used for the selection of the
1815 agenda entries. Still, you may want the agenda to be aware of
1816 the inherited tags anyway, e.g. for later tag filtering.
1818 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1820 This variable has no effect if `org-agenda-show-inherited-tags'
1821 is set to 'always. In that case, the agenda is aware of those
1822 tags.
1824 The default value sets tags in every agenda type. Setting this
1825 option to nil will speed up non-tags agenda view a lot."
1826 :group 'org-agenda
1827 :version "24.3"
1828 :type '(choice
1829 (const :tag "Use tag inheritance in all agenda types" t)
1830 (repeat :tag "Use tag inheritance in selected agenda types"
1831 (symbol :tag "Agenda type"))))
1833 (defcustom org-agenda-hide-tags-regexp nil
1834 "Regular expression used to filter away specific tags in agenda views.
1835 This means that these tags will be present, but not be shown in the agenda
1836 line. Secondary filtering will still work on the hidden tags.
1837 Nil means don't hide any tags."
1838 :group 'org-agenda-line-format
1839 :type '(choice
1840 (const :tag "Hide none" nil)
1841 (string :tag "Regexp ")))
1843 (defcustom org-agenda-remove-tags nil
1844 "Non-nil means remove the tags from the headline copy in the agenda.
1845 When this is the symbol `prefix', only remove tags when
1846 `org-agenda-prefix-format' contains a `%T' specifier."
1847 :group 'org-agenda-line-format
1848 :type '(choice
1849 (const :tag "Always" t)
1850 (const :tag "Never" nil)
1851 (const :tag "When prefix format contains %T" prefix)))
1853 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1854 'org-agenda-remove-tags)
1856 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1857 "Shift tags in agenda items to this column.
1858 If this number is positive, it specifies the column. If it is negative,
1859 it means that the tags should be flushright to that column. For example,
1860 -80 works well for a normal 80 character screen."
1861 :group 'org-agenda-line-format
1862 :type 'integer)
1864 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1866 (defcustom org-agenda-fontify-priorities 'cookies
1867 "Non-nil means highlight low and high priorities in agenda.
1868 When t, the highest priority entries are bold, lowest priority italic.
1869 However, settings in `org-priority-faces' will overrule these faces.
1870 When this variable is the symbol `cookies', only fontify the
1871 cookies, not the entire task.
1872 This may also be an association list of priority faces, whose
1873 keys are the character values of `org-highest-priority',
1874 `org-default-priority', and `org-lowest-priority' (the default values
1875 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1876 color as a string, or a list like `(:background \"Red\")'.
1877 If it is a color, the variable `org-faces-easy-properties'
1878 determines if it is a foreground or a background color."
1879 :group 'org-agenda-line-format
1880 :type '(choice
1881 (const :tag "Never" nil)
1882 (const :tag "Defaults" t)
1883 (const :tag "Cookies only" cookies)
1884 (repeat :tag "Specify"
1885 (list (character :tag "Priority" :value ?A)
1886 (choice :tag "Face "
1887 (string :tag "Color")
1888 (sexp :tag "Face"))))))
1890 (defcustom org-agenda-day-face-function nil
1891 "Function called to determine what face should be used to display a day.
1892 The only argument passed to that function is the day. It should
1893 returns a face, or nil if does not want to specify a face and let
1894 the normal rules apply."
1895 :group 'org-agenda-line-format
1896 :version "24.1"
1897 :type 'function)
1899 (defcustom org-agenda-category-icon-alist nil
1900 "Alist of category icon to be displayed in agenda views.
1902 Each entry should have the following format:
1904 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1906 Where CATEGORY-REGEXP is a regexp matching the categories where
1907 the icon should be displayed.
1908 FILE-OR-DATA either a file path or a string containing image data.
1910 The other fields can be omitted safely if not needed:
1911 TYPE indicates the image type.
1912 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1913 image data.
1914 PROPS are additional image attributes to assign to the image,
1915 like, e.g. `:ascent center'.
1917 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1919 If you want to set the display properties yourself, just put a
1920 list as second element:
1922 (CATEGORY-REGEXP (MY PROPERTY LIST))
1924 For example, to display a 16px horizontal space for Emacs
1925 category, you can use:
1927 (\"Emacs\" '(space . (:width (16))))"
1928 :group 'org-agenda-line-format
1929 :version "24.1"
1930 :type '(alist :key-type (string :tag "Regexp matching category")
1931 :value-type (choice (list :tag "Icon"
1932 (string :tag "File or data")
1933 (symbol :tag "Type")
1934 (boolean :tag "Data?")
1935 (repeat :tag "Extra image properties" :inline t symbol))
1936 (list :tag "Display properties" sexp))))
1938 (defgroup org-agenda-column-view nil
1939 "Options concerning column view in the agenda."
1940 :tag "Org Agenda Column View"
1941 :group 'org-agenda)
1943 (defcustom org-agenda-columns-show-summaries t
1944 "Non-nil means show summaries for columns displayed in the agenda view."
1945 :group 'org-agenda-column-view
1946 :type 'boolean)
1948 (defcustom org-agenda-columns-compute-summary-properties t
1949 "Non-nil means recompute all summary properties before column view.
1950 When column view in the agenda is listing properties that have a summary
1951 operator, it can go to all relevant buffers and recompute the summaries
1952 there. This can mean overhead for the agenda column view, but is necessary
1953 to have thing up to date.
1954 As a special case, a CLOCKSUM property also makes sure that the clock
1955 computations are current."
1956 :group 'org-agenda-column-view
1957 :type 'boolean)
1959 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1960 "Non-nil means the duration of an appointment will add to day effort.
1961 The property to which appointment durations will be added is the one given
1962 in the option `org-effort-property'. If an appointment does not have
1963 an end time, `org-agenda-default-appointment-duration' will be used. If that
1964 is not set, an appointment without end time will not contribute to the time
1965 estimate."
1966 :group 'org-agenda-column-view
1967 :type 'boolean)
1969 (defcustom org-agenda-auto-exclude-function nil
1970 "A function called with a tag to decide if it is filtered on '/ RET'.
1971 The sole argument to the function, which is called once for each
1972 possible tag, is a string giving the name of the tag. The
1973 function should return either nil if the tag should be included
1974 as normal, or \"-<TAG>\" to exclude the tag.
1975 Note that for the purpose of tag filtering, only the lower-case version of
1976 all tags will be considered, so that this function will only ever see
1977 the lower-case version of all tags."
1978 :group 'org-agenda
1979 :type 'function)
1981 (defcustom org-agenda-bulk-custom-functions nil
1982 "Alist of characters and custom functions for bulk actions.
1983 For example, this value makes those two functions available:
1985 '((?R set-category)
1986 (?C bulk-cut))
1988 With selected entries in an agenda buffer, `B R' will call
1989 the custom function `set-category' on the selected entries.
1990 Note that functions in this alist don't need to be quoted."
1991 :type 'alist
1992 :version "24.1"
1993 :group 'org-agenda)
1995 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1996 "Execute BODY with point at location given by `org-hd-marker' property.
1997 If STRING is non-nil, the text property will be fetched from position 0
1998 in that string. If STRING is nil, it will be fetched from the beginning
1999 of the current line."
2000 (org-with-gensyms (marker)
2001 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2002 'org-hd-marker ,string)))
2003 (with-current-buffer (marker-buffer ,marker)
2004 (save-excursion
2005 (goto-char ,marker)
2006 ,@body)))))
2007 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2009 (defun org-add-agenda-custom-command (entry)
2010 "Replace or add a command in `org-agenda-custom-commands'.
2011 This is mostly for hacking and trying a new command - once the command
2012 works you probably want to add it to `org-agenda-custom-commands' for good."
2013 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2014 (if ass
2015 (setcdr ass (cdr entry))
2016 (push entry org-agenda-custom-commands))))
2018 ;;; Define the org-agenda-mode
2020 (defvar org-agenda-mode-map (make-sparse-keymap)
2021 "Keymap for `org-agenda-mode'.")
2022 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2024 (defvar org-agenda-menu) ; defined later in this file.
2025 (defvar org-agenda-restrict nil) ; defined later in this file.
2026 (defvar org-agenda-follow-mode nil)
2027 (defvar org-agenda-entry-text-mode nil)
2028 (defvar org-agenda-clockreport-mode nil)
2029 (defvar org-agenda-show-log nil)
2030 (defvar org-agenda-redo-command nil)
2031 (defvar org-agenda-query-string nil)
2032 (defvar org-agenda-mode-hook nil
2033 "Hook run after `org-agenda-mode' is turned on.
2034 The buffer is still writable when this hook is called.")
2035 (defvar org-agenda-type nil)
2036 (defvar org-agenda-force-single-file nil)
2037 (defvar org-agenda-bulk-marked-entries nil
2038 "List of markers that refer to marked entries in the agenda.")
2040 ;;; Multiple agenda buffers support
2042 (defcustom org-agenda-sticky nil
2043 "Non-nil means agenda q key will bury agenda buffers.
2044 Agenda commands will then show existing buffer instead of generating new ones.
2045 When nil, `q' will kill the single agenda buffer."
2046 :group 'org-agenda
2047 :version "24.3"
2048 :type 'boolean)
2051 ;;;###autoload
2052 (defun org-toggle-sticky-agenda (&optional arg)
2053 "Toggle `org-agenda-sticky'."
2054 (interactive "P")
2055 (let ((new-value (if arg
2056 (> (prefix-numeric-value arg) 0)
2057 (not org-agenda-sticky))))
2058 (if (equal new-value org-agenda-sticky)
2059 (and (org-called-interactively-p 'interactive)
2060 (message "Sticky agenda was already %s"
2061 (if org-agenda-sticky "enabled" "disabled")))
2062 (setq org-agenda-sticky new-value)
2063 (org-agenda-kill-all-agenda-buffers)
2064 (and (org-called-interactively-p 'interactive)
2065 (message "Sticky agenda was %s"
2066 (if org-agenda-sticky "enabled" "disabled"))))))
2068 (defvar org-agenda-buffer nil
2069 "Agenda buffer currently being generated.")
2071 (defvar org-agenda-last-prefix-arg nil)
2072 (defvar org-agenda-this-buffer-name nil)
2073 (defvar org-agenda-doing-sticky-redo nil)
2074 (defvar org-agenda-this-buffer-is-sticky nil)
2076 (defconst org-agenda-local-vars
2077 '(org-agenda-this-buffer-name
2078 org-agenda-undo-list
2079 org-agenda-pending-undo-list
2080 org-agenda-follow-mode
2081 org-agenda-entry-text-mode
2082 org-agenda-clockreport-mode
2083 org-agenda-show-log
2084 org-agenda-redo-command
2085 org-agenda-query-string
2086 org-agenda-type
2087 org-agenda-bulk-marked-entries
2088 org-agenda-undo-has-started-in
2089 org-agenda-info
2090 org-agenda-pre-window-conf
2091 org-agenda-columns-active
2092 org-agenda-tag-filter-overlays
2093 org-agenda-tag-filter
2094 org-agenda-cat-filter-overlays
2095 org-agenda-category-filter
2096 org-agenda-re-filter-overlays
2097 org-agenda-regexp-filter
2098 org-agenda-markers
2099 org-agenda-last-search-view-search-was-boolean
2100 org-agenda-filtered-by-category
2101 org-agenda-filter-form
2102 org-agenda-cycle-counter
2103 org-agenda-last-prefix-arg)
2104 "Variables that must be local in agenda buffers to allow multiple buffers.")
2106 (defun org-agenda-mode ()
2107 "Mode for time-sorted view on action items in Org-mode files.
2109 The following commands are available:
2111 \\{org-agenda-mode-map}"
2112 (interactive)
2113 (cond (org-agenda-doing-sticky-redo
2114 ;; Refreshing sticky agenda-buffer
2116 ;; Preserve the value of `org-agenda-local-vars' variables,
2117 ;; while letting `kill-all-local-variables' kill the rest
2118 (let ((save (buffer-local-variables)))
2119 (kill-all-local-variables)
2120 (mapc 'make-local-variable org-agenda-local-vars)
2121 (dolist (elem save)
2122 (let ((var (car elem))
2123 (val (cdr elem)))
2124 (when (and val
2125 (member var org-agenda-local-vars))
2126 (set var val)))))
2127 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2128 (org-agenda-sticky
2129 ;; Creating a sticky Agenda buffer for the first time
2130 (kill-all-local-variables)
2131 (mapc 'make-local-variable org-agenda-local-vars)
2132 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2134 ;; Creating a non-sticky agenda buffer
2135 (kill-all-local-variables)
2136 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2137 (setq org-agenda-undo-list nil
2138 org-agenda-pending-undo-list nil
2139 org-agenda-bulk-marked-entries nil)
2140 (setq major-mode 'org-agenda-mode)
2141 ;; Keep global-font-lock-mode from turning on font-lock-mode
2142 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2143 (setq mode-name "Org-Agenda")
2144 (use-local-map org-agenda-mode-map)
2145 (easy-menu-add org-agenda-menu)
2146 (if org-startup-truncated (setq truncate-lines t))
2147 (org-set-local 'line-move-visual nil)
2148 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2149 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2150 ;; Make sure properties are removed when copying text
2151 (make-local-variable 'filter-buffer-substring-functions)
2152 (add-hook 'filter-buffer-substring-functions
2153 (lambda (fun start end delete)
2154 (substring-no-properties (funcall fun start end delete))))
2155 (unless org-agenda-keep-modes
2156 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2157 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2158 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2159 org-agenda-show-log org-agenda-start-with-log-mode))
2161 (easy-menu-change
2162 '("Agenda") "Agenda Files"
2163 (append
2164 (list
2165 (vector
2166 (if (get 'org-agenda-files 'org-restrict)
2167 "Restricted to single file"
2168 "Edit File List")
2169 '(org-edit-agenda-file-list)
2170 (not (get 'org-agenda-files 'org-restrict)))
2171 "--")
2172 (mapcar 'org-file-menu-entry (org-agenda-files))))
2173 (org-agenda-set-mode-name)
2174 (apply
2175 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2176 (list 'org-agenda-mode-hook)))
2178 (substitute-key-definition 'undo 'org-agenda-undo
2179 org-agenda-mode-map global-map)
2180 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2181 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2182 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2183 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2184 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2185 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2186 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2187 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2188 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2189 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2190 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2191 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2192 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2193 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2194 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2195 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2196 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2197 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2198 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2199 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2200 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2201 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2202 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2203 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2204 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2205 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2206 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2207 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2208 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2209 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2210 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2211 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2212 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2213 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2214 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2215 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2216 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2217 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2218 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2219 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2220 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2221 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2222 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2223 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2224 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2225 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2226 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2227 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2229 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2230 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2231 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2232 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2233 (while l (org-defkey org-agenda-mode-map
2234 (int-to-string (pop l)) 'digit-argument)))
2236 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2237 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2238 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2239 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2240 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2241 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2242 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2243 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2244 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2245 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2246 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2247 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2248 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2249 'org-clock-modify-effort-estimate)
2250 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2251 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2252 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2253 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2254 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2255 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2256 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2257 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2258 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2259 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2260 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2261 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2262 (substitute-key-definition 'next-line 'org-agenda-next-line
2263 org-agenda-mode-map global-map)
2264 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2265 org-agenda-mode-map global-map)
2266 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2267 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2268 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2269 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2270 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2271 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2272 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2273 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2274 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2275 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2276 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2277 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2278 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2279 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2280 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2281 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2282 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2283 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2284 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2285 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2286 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2287 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2288 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2289 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2290 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2291 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2292 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2293 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2294 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2295 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2297 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2298 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2299 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2300 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2301 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2302 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2303 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2304 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2305 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2306 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2307 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2308 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2309 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2310 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2311 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2313 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2314 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2315 (when org-agenda-mouse-1-follows-link
2316 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2317 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2318 '("Agenda"
2319 ("Agenda Files")
2320 "--"
2321 ("Agenda Dates"
2322 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2323 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2324 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2325 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2326 "--"
2327 ("View"
2328 ["Day View" org-agenda-day-view
2329 :active (org-agenda-check-type nil 'agenda)
2330 :style radio :selected (eq org-agenda-current-span 'day)
2331 :keys "v d (or just d)"]
2332 ["Week View" org-agenda-week-view
2333 :active (org-agenda-check-type nil 'agenda)
2334 :style radio :selected (eq org-agenda-current-span 'week)
2335 :keys "v w (or just w)"]
2336 ["Month View" org-agenda-month-view
2337 :active (org-agenda-check-type nil 'agenda)
2338 :style radio :selected (eq org-agenda-current-span 'month)
2339 :keys "v m"]
2340 ["Year View" org-agenda-year-view
2341 :active (org-agenda-check-type nil 'agenda)
2342 :style radio :selected (eq org-agenda-current-span 'year)
2343 :keys "v y"]
2344 "--"
2345 ["Include Diary" org-agenda-toggle-diary
2346 :style toggle :selected org-agenda-include-diary
2347 :active (org-agenda-check-type nil 'agenda)]
2348 ["Include Deadlines" org-agenda-toggle-deadlines
2349 :style toggle :selected org-agenda-include-deadlines
2350 :active (org-agenda-check-type nil 'agenda)]
2351 ["Use Time Grid" org-agenda-toggle-time-grid
2352 :style toggle :selected org-agenda-use-time-grid
2353 :active (org-agenda-check-type nil 'agenda)]
2354 "--"
2355 ["Show clock report" org-agenda-clockreport-mode
2356 :style toggle :selected org-agenda-clockreport-mode
2357 :active (org-agenda-check-type nil 'agenda)]
2358 ["Show some entry text" org-agenda-entry-text-mode
2359 :style toggle :selected org-agenda-entry-text-mode
2360 :active t]
2361 "--"
2362 ["Show Logbook entries" org-agenda-log-mode
2363 :style toggle :selected org-agenda-show-log
2364 :active (org-agenda-check-type nil 'agenda 'timeline)
2365 :keys "v l (or just l)"]
2366 ["Include archived trees" org-agenda-archives-mode
2367 :style toggle :selected org-agenda-archives-mode :active t
2368 :keys "v a"]
2369 ["Include archive files" (org-agenda-archives-mode t)
2370 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2371 :keys "v A"]
2372 "--"
2373 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2374 ["Write view to file" org-agenda-write t]
2375 ["Rebuild buffer" org-agenda-redo t]
2376 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2377 "--"
2378 ["Show original entry" org-agenda-show t]
2379 ["Go To (other window)" org-agenda-goto t]
2380 ["Go To (this window)" org-agenda-switch-to t]
2381 ["Capture with cursor date" org-agenda-capture t]
2382 ["Follow Mode" org-agenda-follow-mode
2383 :style toggle :selected org-agenda-follow-mode :active t]
2384 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2385 "--"
2386 ("TODO"
2387 ["Cycle TODO" org-agenda-todo t]
2388 ["Next TODO set" org-agenda-todo-nextset t]
2389 ["Previous TODO set" org-agenda-todo-previousset t]
2390 ["Add note" org-agenda-add-note t])
2391 ("Archive/Refile/Delete"
2392 ["Archive default" org-agenda-archive-default t]
2393 ["Archive default" org-agenda-archive-default-with-confirmation t]
2394 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2395 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2396 ["Archive subtree" org-agenda-archive t]
2397 "--"
2398 ["Refile" org-agenda-refile t]
2399 "--"
2400 ["Delete subtree" org-agenda-kill t])
2401 ("Bulk action"
2402 ["Mark entry" org-agenda-bulk-mark t]
2403 ["Mark all" org-agenda-bulk-mark-all t]
2404 ["Unmark entry" org-agenda-bulk-unmark t]
2405 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2406 ["Toggle mark" org-agenda-bulk-toggle t]
2407 ["Toggle all" org-agenda-bulk-toggle-all t]
2408 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2409 ["Act on all marked" org-agenda-bulk-action t]
2410 "--"
2411 ("Tags and Properties"
2412 ["Show all Tags" org-agenda-show-tags t]
2413 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2414 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2415 "--"
2416 ["Column View" org-columns t])
2417 ("Deadline/Schedule"
2418 ["Schedule" org-agenda-schedule t]
2419 ["Set Deadline" org-agenda-deadline t]
2420 "--"
2421 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2422 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2423 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2424 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2425 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2426 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2427 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2428 ("Clock and Effort"
2429 ["Clock in" org-agenda-clock-in t]
2430 ["Clock out" org-agenda-clock-out t]
2431 ["Clock cancel" org-agenda-clock-cancel t]
2432 ["Goto running clock" org-clock-goto t]
2433 "--"
2434 ["Set Effort" org-agenda-set-effort t]
2435 ["Change clocked effort" org-clock-modify-effort-estimate
2436 (org-clock-is-active)])
2437 ("Priority"
2438 ["Set Priority" org-agenda-priority t]
2439 ["Increase Priority" org-agenda-priority-up t]
2440 ["Decrease Priority" org-agenda-priority-down t]
2441 ["Show Priority" org-show-priority t])
2442 ("Calendar/Diary"
2443 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2444 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2445 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2446 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2447 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2448 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2449 "--"
2450 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2451 "--"
2452 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2453 "--"
2454 ("MobileOrg"
2455 ["Push Files and Views" org-mobile-push t]
2456 ["Get Captured and Flagged" org-mobile-pull t]
2457 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2458 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2459 "--"
2460 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2461 "--"
2462 ["Quit" org-agenda-quit t]
2463 ["Exit and Release Buffers" org-agenda-exit t]
2466 ;;; Agenda undo
2468 (defvar org-agenda-allow-remote-undo t
2469 "Non-nil means allow remote undo from the agenda buffer.")
2470 (defvar org-agenda-undo-has-started-in nil
2471 "Buffers that have already seen `undo-start' in the current undo sequence.")
2473 (defun org-agenda-undo ()
2474 "Undo a remote editing step in the agenda.
2475 This undoes changes both in the agenda buffer and in the remote buffer
2476 that have been changed along."
2477 (interactive)
2478 (or org-agenda-allow-remote-undo
2479 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2480 (if (not (eq this-command last-command))
2481 (setq org-agenda-undo-has-started-in nil
2482 org-agenda-pending-undo-list org-agenda-undo-list))
2483 (if (not org-agenda-pending-undo-list)
2484 (user-error "No further undo information"))
2485 (let* ((entry (pop org-agenda-pending-undo-list))
2486 buf line cmd rembuf)
2487 (setq cmd (pop entry) line (pop entry))
2488 (setq rembuf (nth 2 entry))
2489 (org-with-remote-undo rembuf
2490 (while (bufferp (setq buf (pop entry)))
2491 (if (pop entry)
2492 (with-current-buffer buf
2493 (let ((last-undo-buffer buf)
2494 (inhibit-read-only t))
2495 (unless (memq buf org-agenda-undo-has-started-in)
2496 (push buf org-agenda-undo-has-started-in)
2497 (make-local-variable 'pending-undo-list)
2498 (undo-start))
2499 (while (and pending-undo-list
2500 (listp pending-undo-list)
2501 (not (car pending-undo-list)))
2502 (pop pending-undo-list))
2503 (undo-more 1))))))
2504 (org-goto-line line)
2505 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2507 (defun org-verify-change-for-undo (l1 l2)
2508 "Verify that a real change occurred between the undo lists L1 and L2."
2509 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2510 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2511 (not (eq l1 l2)))
2513 ;;; Agenda dispatch
2515 (defvar org-agenda-restrict-begin (make-marker))
2516 (defvar org-agenda-restrict-end (make-marker))
2517 (defvar org-agenda-last-dispatch-buffer nil)
2518 (defvar org-agenda-overriding-restriction nil)
2520 (defcustom org-agenda-custom-commands-contexts nil
2521 "Alist of custom agenda keys and contextual rules.
2523 For example, if you have a custom agenda command \"p\" and you
2524 want this command to be accessible only from plain text files,
2525 use this:
2527 '((\"p\" ((in-file . \"\\.txt\"))))
2529 Here are the available contexts definitions:
2531 in-file: command displayed only in matching files
2532 in-mode: command displayed only in matching modes
2533 not-in-file: command not displayed in matching files
2534 not-in-mode: command not displayed in matching modes
2535 in-buffer: command displayed only in matching buffers
2536 not-in-buffer: command not displayed in matching buffers
2537 [function]: a custom function taking no argument
2539 If you define several checks, the agenda command will be
2540 accessible if there is at least one valid check.
2542 You can also bind a key to another agenda custom command
2543 depending on contextual rules.
2545 '((\"p\" \"q\" ((in-file . \"\\.txt\"))))
2547 Here it means: in .txt files, use \"p\" as the key for the
2548 agenda command otherwise associated with \"q\". (The command
2549 originally associated with \"q\" is not displayed to avoid
2550 duplicates.)"
2551 :version "24.3"
2552 :group 'org-agenda-custom-commands
2553 :type '(repeat (list :tag "Rule"
2554 (string :tag " Agenda key")
2555 (string :tag "Replace by command")
2556 (repeat :tag "Available when"
2557 (choice
2558 (cons :tag "Condition"
2559 (choice
2560 (const :tag "In file" in-file)
2561 (const :tag "Not in file" not-in-file)
2562 (const :tag "In buffer" in-buffer)
2563 (const :tag "Not in buffer" not-in-buffer)
2564 (const :tag "In mode" in-mode)
2565 (const :tag "Not in mode" not-in-mode))
2566 (regexp))
2567 (function :tag "Custom function"))))))
2569 (defcustom org-agenda-max-entries nil
2570 "Maximum number of entries to display in an agenda.
2571 This can be nil (no limit) or an integer or an alist of agenda
2572 types with an associated number of entries to display in this
2573 type."
2574 :version "24.4"
2575 :package-version '(Org . "8.0")
2576 :group 'org-agenda-custom-commands
2577 :type '(choice (symbol :tag "No limit" nil)
2578 (integer :tag "Max number of entries")
2579 (repeat
2580 (cons (choice :tag "Agenda type"
2581 (const agenda)
2582 (const todo)
2583 (const tags)
2584 (const search)
2585 (const timeline))
2586 (integer :tag "Max number of entries")))))
2588 (defcustom org-agenda-max-todos nil
2589 "Maximum number of TODOs to display in an agenda.
2590 This can be nil (no limit) or an integer or an alist of agenda
2591 types with an associated number of entries to display in this
2592 type."
2593 :version "24.4"
2594 :package-version '(Org . "8.0")
2595 :group 'org-agenda-custom-commands
2596 :type '(choice (symbol :tag "No limit" nil)
2597 (integer :tag "Max number of entries")
2598 (repeat
2599 (cons (choice :tag "Agenda type"
2600 (const agenda)
2601 (const todo)
2602 (const tags)
2603 (const search)
2604 (const timeline))
2605 (integer :tag "Max number of entries")))))
2607 (defcustom org-agenda-max-tags nil
2608 "Maximum number of tagged entries to display in an agenda.
2609 This can be nil (no limit) or an integer or an alist of agenda
2610 types with an associated number of entries to display in this
2611 type."
2612 :version "24.4"
2613 :package-version '(Org . "8.0")
2614 :group 'org-agenda-custom-commands
2615 :type '(choice (symbol :tag "No limit" nil)
2616 (integer :tag "Max number of entries")
2617 (repeat
2618 (cons (choice :tag "Agenda type"
2619 (const agenda)
2620 (const todo)
2621 (const tags)
2622 (const search)
2623 (const timeline))
2624 (integer :tag "Max number of entries")))))
2626 (defcustom org-agenda-max-effort nil
2627 "Maximum cumulated effort duration for the agenda.
2628 This can be nil (no limit) or a number of minutes (as an integer)
2629 or an alist of agenda types with an associated number of minutes
2630 to limit entries to in this type."
2631 :version "24.4"
2632 :package-version '(Org . "8.0")
2633 :group 'org-agenda-custom-commands
2634 :type '(choice (symbol :tag "No limit" nil)
2635 (integer :tag "Max number of entries")
2636 (repeat
2637 (cons (choice :tag "Agenda type"
2638 (const agenda)
2639 (const todo)
2640 (const tags)
2641 (const search)
2642 (const timeline))
2643 (integer :tag "Max number of entries")))))
2645 (defvar org-keys nil)
2646 (defvar org-match nil)
2647 ;;;###autoload
2648 (defun org-agenda (&optional arg org-keys restriction)
2649 "Dispatch agenda commands to collect entries to the agenda buffer.
2650 Prompts for a command to execute. Any prefix arg will be passed
2651 on to the selected command. The default selections are:
2653 a Call `org-agenda-list' to display the agenda for current day or week.
2654 t Call `org-todo-list' to display the global todo list.
2655 T Call `org-todo-list' to display the global todo list, select only
2656 entries with a specific TODO keyword (the user gets a prompt).
2657 m Call `org-tags-view' to display headlines with tags matching
2658 a condition (the user is prompted for the condition).
2659 M Like `m', but select only TODO entries, no ordinary headlines.
2660 L Create a timeline for the current buffer.
2661 e Export views to associated files.
2662 s Search entries for keywords.
2663 S Search entries for keywords, only with TODO keywords.
2664 / Multi occur across all agenda files and also files listed
2665 in `org-agenda-text-search-extra-files'.
2666 < Restrict agenda commands to buffer, subtree, or region.
2667 Press several times to get the desired effect.
2668 > Remove a previous restriction.
2669 # List \"stuck\" projects.
2670 ! Configure what \"stuck\" means.
2671 C Configure custom agenda commands.
2673 More commands can be added by configuring the variable
2674 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2675 searches can be pre-defined in this way.
2677 If the current buffer is in Org-mode and visiting a file, you can also
2678 first press `<' once to indicate that the agenda should be temporarily
2679 \(until the next use of \\[org-agenda]) restricted to the current file.
2680 Pressing `<' twice means to restrict to the current subtree or region
2681 \(if active)."
2682 (interactive "P")
2683 (catch 'exit
2684 (let* ((prefix-descriptions nil)
2685 (org-agenda-buffer-name org-agenda-buffer-name)
2686 (org-agenda-window-setup (if (equal (buffer-name)
2687 org-agenda-buffer-name)
2688 'current-window
2689 org-agenda-window-setup))
2690 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2691 (org-agenda-custom-commands
2692 ;; normalize different versions
2693 (delq nil
2694 (mapcar
2695 (lambda (x)
2696 (cond ((stringp (cdr x))
2697 (push x prefix-descriptions)
2698 nil)
2699 ((stringp (nth 1 x)) x)
2700 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2701 (t (cons (car x) (cons "" (cdr x))))))
2702 org-agenda-custom-commands)))
2703 (org-agenda-custom-commands
2704 (org-contextualize-keys
2705 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2706 (buf (current-buffer))
2707 (bfn (buffer-file-name (buffer-base-buffer)))
2708 entry key type org-match lprops ans)
2709 ;; Turn off restriction unless there is an overriding one,
2710 (unless org-agenda-overriding-restriction
2711 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2712 ;; There is a request to keep the file list in place
2713 (put 'org-agenda-files 'org-restrict nil))
2714 (setq org-agenda-restrict nil)
2715 (move-marker org-agenda-restrict-begin nil)
2716 (move-marker org-agenda-restrict-end nil))
2717 ;; Delete old local properties
2718 (put 'org-agenda-redo-command 'org-lprops nil)
2719 ;; Delete previously set last-arguments
2720 (put 'org-agenda-redo-command 'last-args nil)
2721 ;; Remember where this call originated
2722 (setq org-agenda-last-dispatch-buffer (current-buffer))
2723 (unless org-keys
2724 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2725 org-keys (car ans)
2726 restriction (cdr ans)))
2727 ;; If we have sticky agenda buffers, set a name for the buffer,
2728 ;; depending on the invoking keys. The user may still set this
2729 ;; as a command option, which will overwrite what we do here.
2730 (if org-agenda-sticky
2731 (setq org-agenda-buffer-name
2732 (format "*Org Agenda(%s)*" org-keys)))
2733 ;; Establish the restriction, if any
2734 (when (and (not org-agenda-overriding-restriction) restriction)
2735 (put 'org-agenda-files 'org-restrict (list bfn))
2736 (cond
2737 ((eq restriction 'region)
2738 (setq org-agenda-restrict t)
2739 (move-marker org-agenda-restrict-begin (region-beginning))
2740 (move-marker org-agenda-restrict-end (region-end)))
2741 ((eq restriction 'subtree)
2742 (save-excursion
2743 (setq org-agenda-restrict t)
2744 (org-back-to-heading t)
2745 (move-marker org-agenda-restrict-begin (point))
2746 (move-marker org-agenda-restrict-end
2747 (progn (org-end-of-subtree t)))))))
2749 ;; For example the todo list should not need it (but does...)
2750 (cond
2751 ((setq entry (assoc org-keys org-agenda-custom-commands))
2752 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2753 (progn
2754 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2755 lprops (nth 4 entry))
2756 (if org-agenda-sticky
2757 (setq org-agenda-buffer-name
2758 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2759 (format "*Org Agenda(%s)*" org-keys))))
2760 (put 'org-agenda-redo-command 'org-lprops lprops)
2761 (cond
2762 ((eq type 'agenda)
2763 (org-let lprops '(org-agenda-list current-prefix-arg)))
2764 ((eq type 'agenda*)
2765 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2766 ((eq type 'alltodo)
2767 (org-let lprops '(org-todo-list current-prefix-arg)))
2768 ((eq type 'search)
2769 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2770 ((eq type 'stuck)
2771 (org-let lprops '(org-agenda-list-stuck-projects
2772 current-prefix-arg)))
2773 ((eq type 'tags)
2774 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2775 ((eq type 'tags-todo)
2776 (org-let lprops '(org-tags-view '(4) org-match)))
2777 ((eq type 'todo)
2778 (org-let lprops '(org-todo-list org-match)))
2779 ((eq type 'tags-tree)
2780 (org-check-for-org-mode)
2781 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2782 ((eq type 'todo-tree)
2783 (org-check-for-org-mode)
2784 (org-let lprops
2785 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2786 (regexp-quote org-match) "\\>"))))
2787 ((eq type 'occur-tree)
2788 (org-check-for-org-mode)
2789 (org-let lprops '(org-occur org-match)))
2790 ((functionp type)
2791 (org-let lprops '(funcall type org-match)))
2792 ((fboundp type)
2793 (org-let lprops '(funcall type org-match)))
2794 (t (user-error "Invalid custom agenda command type %s" type))))
2795 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2796 ((equal org-keys "C")
2797 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2798 (customize-variable 'org-agenda-custom-commands))
2799 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2800 ((equal org-keys "s") (call-interactively 'org-search-view))
2801 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2802 ((equal org-keys "t") (call-interactively 'org-todo-list))
2803 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2804 ((equal org-keys "m") (call-interactively 'org-tags-view))
2805 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2806 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2807 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2808 (org-add-hook
2809 'post-command-hook
2810 (lambda ()
2811 (unless (current-message)
2812 (let* ((m (org-agenda-get-any-marker))
2813 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2814 (when note
2815 (message (concat
2816 "FLAGGING-NOTE ([?] for more info): "
2817 (org-add-props
2818 (replace-regexp-in-string
2819 "\\\\n" "//"
2820 (copy-sequence note))
2821 nil 'face 'org-warning)))))))
2822 t t))
2823 ((equal org-keys "L")
2824 (unless (derived-mode-p 'org-mode)
2825 (user-error "This is not an Org-mode file"))
2826 (unless restriction
2827 (put 'org-agenda-files 'org-restrict (list bfn))
2828 (org-call-with-arg 'org-timeline arg)))
2829 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2830 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2831 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2832 (t (user-error "Invalid agenda key"))))))
2834 (defun org-agenda-append-agenda ()
2835 "Append another agenda view to the current one.
2836 This function allows interactive building of block agendas.
2837 Agenda views are separated by `org-agenda-block-separator'."
2838 (interactive)
2839 (unless (derived-mode-p 'org-agenda-mode)
2840 (user-error "Can only append from within agenda buffer"))
2841 (let ((org-agenda-multi t))
2842 (org-agenda)
2843 (widen)
2844 (org-agenda-finalize)
2845 (setq buffer-read-only t)
2846 (org-agenda-fit-window-to-buffer)))
2848 (defun org-agenda-normalize-custom-commands (cmds)
2849 "Normalize custom commands CMDS."
2850 (delq nil
2851 (mapcar
2852 (lambda (x)
2853 (cond ((stringp (cdr x)) nil)
2854 ((stringp (nth 1 x)) x)
2855 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2856 (t (cons (car x) (cons "" (cdr x))))))
2857 cmds)))
2859 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2860 "The user interface for selecting an agenda command."
2861 (catch 'exit
2862 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2863 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2864 (region-p (org-region-active-p))
2865 (custom org-agenda-custom-commands)
2866 (selstring "")
2867 restriction second-time
2868 c entry key type match prefixes rmheader header-end custom1 desc
2869 line lines left right n n1)
2870 (save-window-excursion
2871 (delete-other-windows)
2872 (org-switch-to-buffer-other-window " *Agenda Commands*")
2873 (erase-buffer)
2874 (insert (eval-when-compile
2875 (let ((header
2876 "Press key for an agenda command: < Buffer, subtree/region restriction
2877 -------------------------------- > Remove restriction
2878 a Agenda for current week or day e Export agenda views
2879 t List of all TODO entries T Entries with special TODO kwd
2880 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2881 s Search for keywords S Like s, but only TODO entries
2882 L Timeline for current buffer # List stuck projects (!=configure)
2883 / Multi-occur C Configure custom agenda commands
2884 ? Find :FLAGGED: entries * Toggle sticky agenda views
2886 (start 0))
2887 (while (string-match
2888 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2889 header start)
2890 (setq start (match-end 0))
2891 (add-text-properties (match-beginning 2) (match-end 2)
2892 '(face bold) header))
2893 header)))
2894 (setq header-end (point-marker))
2895 (while t
2896 (setq custom1 custom)
2897 (when (eq rmheader t)
2898 (org-goto-line 1)
2899 (re-search-forward ":" nil t)
2900 (delete-region (match-end 0) (point-at-eol))
2901 (forward-char 1)
2902 (looking-at "-+")
2903 (delete-region (match-end 0) (point-at-eol))
2904 (move-marker header-end (match-end 0)))
2905 (goto-char header-end)
2906 (delete-region (point) (point-max))
2908 ;; Produce all the lines that describe custom commands and prefixes
2909 (setq lines nil)
2910 (while (setq entry (pop custom1))
2911 (setq key (car entry) desc (nth 1 entry)
2912 type (nth 2 entry)
2913 match (nth 3 entry))
2914 (if (> (length key) 1)
2915 (add-to-list 'prefixes (string-to-char key))
2916 (setq line
2917 (format
2918 "%-4s%-14s"
2919 (org-add-props (copy-sequence key)
2920 '(face bold))
2921 (cond
2922 ((string-match "\\S-" desc) desc)
2923 ((eq type 'agenda) "Agenda for current week or day")
2924 ((eq type 'agenda*) "Appointments for current week or day")
2925 ((eq type 'alltodo) "List of all TODO entries")
2926 ((eq type 'search) "Word search")
2927 ((eq type 'stuck) "List of stuck projects")
2928 ((eq type 'todo) "TODO keyword")
2929 ((eq type 'tags) "Tags query")
2930 ((eq type 'tags-todo) "Tags (TODO)")
2931 ((eq type 'tags-tree) "Tags tree")
2932 ((eq type 'todo-tree) "TODO kwd tree")
2933 ((eq type 'occur-tree) "Occur tree")
2934 ((functionp type) (if (symbolp type)
2935 (symbol-name type)
2936 "Lambda expression"))
2937 (t "???"))))
2938 (if org-agenda-menu-show-matcher
2939 (setq line
2940 (concat line ": "
2941 (cond
2942 ((stringp match)
2943 (setq match (copy-sequence match))
2944 (org-add-props match nil 'face 'org-warning))
2945 ((listp type)
2946 (format "set of %d commands" (length type))))))
2947 (if (org-string-nw-p match)
2948 (add-text-properties
2949 0 (length line) (list 'help-echo
2950 (concat "Matcher: " match)) line)))
2951 (push line lines)))
2952 (setq lines (nreverse lines))
2953 (when prefixes
2954 (mapc (lambda (x)
2955 (push
2956 (format "%s %s"
2957 (org-add-props (char-to-string x)
2958 nil 'face 'bold)
2959 (or (cdr (assoc (concat selstring
2960 (char-to-string x))
2961 prefix-descriptions))
2962 "Prefix key"))
2963 lines))
2964 prefixes))
2966 ;; Check if we should display in two columns
2967 (if org-agenda-menu-two-columns
2968 (progn
2969 (setq n (length lines)
2970 n1 (+ (/ n 2) (mod n 2))
2971 right (nthcdr n1 lines)
2972 left (copy-sequence lines))
2973 (setcdr (nthcdr (1- n1) left) nil))
2974 (setq left lines right nil))
2975 (while left
2976 (insert "\n" (pop left))
2977 (when right
2978 (if (< (current-column) 40)
2979 (move-to-column 40 t)
2980 (insert " "))
2981 (insert (pop right))))
2983 ;; Make the window the right size
2984 (goto-char (point-min))
2985 (if second-time
2986 (if (not (pos-visible-in-window-p (point-max)))
2987 (org-fit-window-to-buffer))
2988 (setq second-time t)
2989 (org-fit-window-to-buffer))
2991 ;; Ask for selection
2992 (message "Press key for agenda command%s:"
2993 (if (or restrict-ok org-agenda-overriding-restriction)
2994 (if org-agenda-overriding-restriction
2995 " (restriction lock active)"
2996 (if restriction
2997 (format " (restricted to %s)" restriction)
2998 " (unrestricted)"))
2999 ""))
3000 (setq c (read-char-exclusive))
3001 (message "")
3002 (cond
3003 ((assoc (char-to-string c) custom)
3004 (setq selstring (concat selstring (char-to-string c)))
3005 (throw 'exit (cons selstring restriction)))
3006 ((memq c prefixes)
3007 (setq selstring (concat selstring (char-to-string c))
3008 prefixes nil
3009 rmheader (or rmheader t)
3010 custom (delq nil (mapcar
3011 (lambda (x)
3012 (if (or (= (length (car x)) 1)
3013 (/= (string-to-char (car x)) c))
3015 (cons (substring (car x) 1) (cdr x))))
3016 custom))))
3017 ((eq c ?*)
3018 (call-interactively 'org-toggle-sticky-agenda)
3019 (sit-for 2))
3020 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3021 (message "Restriction is only possible in Org-mode buffers")
3022 (ding) (sit-for 1))
3023 ((eq c ?1)
3024 (org-agenda-remove-restriction-lock 'noupdate)
3025 (setq restriction 'buffer))
3026 ((eq c ?0)
3027 (org-agenda-remove-restriction-lock 'noupdate)
3028 (setq restriction (if region-p 'region 'subtree)))
3029 ((eq c ?<)
3030 (org-agenda-remove-restriction-lock 'noupdate)
3031 (setq restriction
3032 (cond
3033 ((eq restriction 'buffer)
3034 (if region-p 'region 'subtree))
3035 ((memq restriction '(subtree region))
3036 nil)
3037 (t 'buffer))))
3038 ((eq c ?>)
3039 (org-agenda-remove-restriction-lock 'noupdate)
3040 (setq restriction nil))
3041 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3042 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3043 ((and (> (length selstring) 0) (eq c ?\d))
3044 (delete-window)
3045 (org-agenda-get-restriction-and-command prefix-descriptions))
3047 ((equal c ?q) (error "Abort"))
3048 (t (user-error "Invalid key %c" c))))))))
3050 (defun org-agenda-fit-window-to-buffer ()
3051 "Fit the window to the buffer size."
3052 (and (memq org-agenda-window-setup '(reorganize-frame))
3053 (fboundp 'fit-window-to-buffer)
3054 (org-fit-window-to-buffer
3056 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3057 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3059 (defvar org-cmd nil)
3060 (defvar org-agenda-overriding-cmd nil)
3061 (defvar org-agenda-overriding-arguments nil)
3062 (defvar org-agenda-overriding-cmd-arguments nil)
3063 (defun org-agenda-run-series (name series)
3064 "Run agenda NAME as a SERIES of agenda commands."
3065 (org-let (nth 1 series) '(org-agenda-prepare name))
3066 ;; We need to reset agenda markers here, because when constructing a
3067 ;; block agenda, the individual blocks do not do that.
3068 (org-agenda-reset-markers)
3069 (let* ((org-agenda-multi t)
3070 (redo (list 'org-agenda-run-series name (list 'quote series)))
3071 (cmds (car series))
3072 (gprops (nth 1 series))
3073 match ;; The byte compiler incorrectly complains about this. Keep it!
3074 org-cmd type lprops)
3075 (while (setq org-cmd (pop cmds))
3076 (setq type (car org-cmd)
3077 match (eval (nth 1 org-cmd))
3078 lprops (nth 2 org-cmd))
3079 (let ((org-agenda-overriding-arguments
3080 (if (eq org-agenda-overriding-cmd org-cmd)
3081 (or org-agenda-overriding-arguments
3082 org-agenda-overriding-cmd-arguments))))
3083 (cond
3084 ((eq type 'agenda)
3085 (org-let2 gprops lprops
3086 '(call-interactively 'org-agenda-list)))
3087 ((eq type 'agenda*)
3088 (org-let2 gprops lprops
3089 '(funcall 'org-agenda-list nil nil t)))
3090 ((eq type 'alltodo)
3091 (org-let2 gprops lprops
3092 '(call-interactively 'org-todo-list)))
3093 ((eq type 'search)
3094 (org-let2 gprops lprops
3095 '(org-search-view current-prefix-arg match nil)))
3096 ((eq type 'stuck)
3097 (org-let2 gprops lprops
3098 '(call-interactively 'org-agenda-list-stuck-projects)))
3099 ((eq type 'tags)
3100 (org-let2 gprops lprops
3101 '(org-tags-view current-prefix-arg match)))
3102 ((eq type 'tags-todo)
3103 (org-let2 gprops lprops
3104 '(org-tags-view '(4) match)))
3105 ((eq type 'todo)
3106 (org-let2 gprops lprops
3107 '(org-todo-list match)))
3108 ((fboundp type)
3109 (org-let2 gprops lprops
3110 '(funcall type match)))
3111 (t (error "Invalid type in command series")))))
3112 (widen)
3113 (let ((inhibit-read-only t))
3114 (add-text-properties (point-min) (point-max)
3115 `(org-series t org-series-redo-cmd ,redo)))
3116 (setq org-agenda-redo-command redo)
3117 (goto-char (point-min)))
3118 (org-agenda-fit-window-to-buffer)
3119 (org-let (nth 1 series) '(org-agenda-finalize)))
3121 ;;;###autoload
3122 (defmacro org-batch-agenda (cmd-key &rest parameters)
3123 "Run an agenda command in batch mode and send the result to STDOUT.
3124 If CMD-KEY is a string of length 1, it is used as a key in
3125 `org-agenda-custom-commands' and triggers this command. If it is a
3126 longer string it is used as a tags/todo match string.
3127 Parameters are alternating variable names and values that will be bound
3128 before running the agenda command."
3129 (org-eval-in-environment (org-make-parameter-alist parameters)
3130 (if (> (length cmd-key) 2)
3131 (org-tags-view nil cmd-key)
3132 (org-agenda nil cmd-key)))
3133 (set-buffer org-agenda-buffer-name)
3134 (princ (buffer-string)))
3136 (defvar org-agenda-info nil)
3138 ;;;###autoload
3139 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3140 "Run an agenda command in batch mode and send the result to STDOUT.
3141 If CMD-KEY is a string of length 1, it is used as a key in
3142 `org-agenda-custom-commands' and triggers this command. If it is a
3143 longer string it is used as a tags/todo match string.
3144 Parameters are alternating variable names and values that will be bound
3145 before running the agenda command.
3147 The output gives a line for each selected agenda item. Each
3148 item is a list of comma-separated values, like this:
3150 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3152 category The category of the item
3153 head The headline, without TODO kwd, TAGS and PRIORITY
3154 type The type of the agenda entry, can be
3155 todo selected in TODO match
3156 tagsmatch selected in tags match
3157 diary imported from diary
3158 deadline a deadline on given date
3159 scheduled scheduled on given date
3160 timestamp entry has timestamp on given date
3161 closed entry was closed on given date
3162 upcoming-deadline warning about deadline
3163 past-scheduled forwarded scheduled item
3164 block entry has date block including g. date
3165 todo The todo keyword, if any
3166 tags All tags including inherited ones, separated by colons
3167 date The relevant date, like 2007-2-14
3168 time The time, like 15:00-16:50
3169 extra Sting with extra planning info
3170 priority-l The priority letter if any was given
3171 priority-n The computed numerical priority
3172 agenda-day The day in the agenda where this is listed"
3173 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3174 (org-make-parameter-alist parameters))
3175 (if (> (length cmd-key) 2)
3176 (org-tags-view nil cmd-key)
3177 (org-agenda nil cmd-key)))
3178 (set-buffer org-agenda-buffer-name)
3179 (let* ((lines (org-split-string (buffer-string) "\n"))
3180 line)
3181 (while (setq line (pop lines))
3182 (catch 'next
3183 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3184 (setq org-agenda-info
3185 (org-fix-agenda-info (text-properties-at 0 line)))
3186 (princ
3187 (mapconcat 'org-agenda-export-csv-mapper
3188 '(org-category txt type todo tags date time extra
3189 priority-letter priority agenda-day)
3190 ","))
3191 (princ "\n")))))
3193 (defun org-fix-agenda-info (props)
3194 "Make sure all properties on an agenda item have a canonical form.
3195 This ensures the export commands can easily use it."
3196 (let (tmp re)
3197 (when (setq tmp (plist-get props 'tags))
3198 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3199 (when (setq tmp (plist-get props 'date))
3200 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3201 (let ((calendar-date-display-form '(year "-" month "-" day)))
3202 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3204 (setq tmp (calendar-date-string tmp)))
3205 (setq props (plist-put props 'date tmp)))
3206 (when (setq tmp (plist-get props 'day))
3207 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3208 (let ((calendar-date-display-form '(year "-" month "-" day)))
3209 (setq tmp (calendar-date-string tmp)))
3210 (setq props (plist-put props 'day tmp))
3211 (setq props (plist-put props 'agenda-day tmp)))
3212 (when (setq tmp (plist-get props 'txt))
3213 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3214 (plist-put props 'priority-letter (match-string 1 tmp))
3215 (setq tmp (replace-match "" t t tmp)))
3216 (when (and (setq re (plist-get props 'org-todo-regexp))
3217 (setq re (concat "\\`\\.*" re " ?"))
3218 (string-match re tmp))
3219 (plist-put props 'todo (match-string 1 tmp))
3220 (setq tmp (replace-match "" t t tmp)))
3221 (plist-put props 'txt tmp)))
3222 props)
3224 (defun org-agenda-export-csv-mapper (prop)
3225 (let ((res (plist-get org-agenda-info prop)))
3226 (setq res
3227 (cond
3228 ((not res) "")
3229 ((stringp res) res)
3230 (t (prin1-to-string res))))
3231 (while (string-match "," res)
3232 (setq res (replace-match ";" t t res)))
3233 (org-trim res)))
3235 ;;;###autoload
3236 (defun org-store-agenda-views (&rest parameters)
3237 "Store agenda views."
3238 (interactive)
3239 (eval (list 'org-batch-store-agenda-views)))
3241 ;;;###autoload
3242 (defmacro org-batch-store-agenda-views (&rest parameters)
3243 "Run all custom agenda commands that have a file argument."
3244 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3245 (pop-up-frames nil)
3246 (dir default-directory)
3247 (pars (org-make-parameter-alist parameters))
3248 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3249 (save-window-excursion
3250 (while cmds
3251 (setq cmd (pop cmds)
3252 thiscmdkey (car cmd)
3253 thiscmdcmd (cdr cmd)
3254 match (nth 2 thiscmdcmd)
3255 bufname (if org-agenda-sticky
3256 (or (and (stringp match)
3257 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3258 (format "*Org Agenda(%s)*" thiscmdkey))
3259 org-agenda-buffer-name)
3260 cmd-or-set (nth 2 cmd)
3261 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3262 files (nth (if (listp cmd-or-set) 4 5) cmd))
3263 (if (stringp files) (setq files (list files)))
3264 (when files
3265 (org-eval-in-environment (append org-agenda-exporter-settings
3266 opts pars)
3267 (org-agenda nil thiscmdkey))
3268 (set-buffer bufname)
3269 (while files
3270 (org-eval-in-environment (append org-agenda-exporter-settings
3271 opts pars)
3272 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3273 (and (get-buffer bufname)
3274 (kill-buffer bufname)))))))
3276 (defvar org-agenda-current-span nil
3277 "The current span used in the agenda view.") ; local variable in the agenda buffer
3278 (defun org-agenda-mark-header-line (pos)
3279 "Mark the line at POS as an agenda structure header."
3280 (save-excursion
3281 (goto-char pos)
3282 (put-text-property (point-at-bol) (point-at-eol)
3283 'org-agenda-structural-header t)
3284 (when org-agenda-title-append
3285 (put-text-property (point-at-bol) (point-at-eol)
3286 'org-agenda-title-append org-agenda-title-append))))
3288 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3289 (defvar org-agenda-write-buffer-name "Agenda View")
3290 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3291 "Write the current buffer (an agenda view) as a file.
3292 Depending on the extension of the file name, plain text (.txt),
3293 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3294 If the extension is .ics, run icalendar export over all files used
3295 to construct the agenda and limit the export to entries listed in the
3296 agenda now.
3297 If the extension is .org, collect all subtrees corresponding to the
3298 agenda entries and add them in an .org file.
3299 With prefix argument OPEN, open the new file immediately.
3300 If NOSETTINGS is given, do not scope the settings of
3301 `org-agenda-exporter-settings' into the export commands. This is used when
3302 the settings have already been scoped and we do not wish to overrule other,
3303 higher priority settings.
3304 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3305 (interactive "FWrite agenda to file: \nP")
3306 (if (or (not (file-writable-p file))
3307 (and (file-exists-p file)
3308 (if (called-interactively-p 'any)
3309 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3310 (user-error "Cannot write agenda to file %s" file))
3311 (org-let (if nosettings nil org-agenda-exporter-settings)
3312 '(save-excursion
3313 (save-window-excursion
3314 (org-agenda-mark-filtered-text)
3315 (let ((bs (copy-sequence (buffer-string))) beg content)
3316 (org-agenda-unmark-filtered-text)
3317 (with-temp-buffer
3318 (rename-buffer org-agenda-write-buffer-name t)
3319 (set-buffer-modified-p nil)
3320 (insert bs)
3321 (org-agenda-remove-marked-text 'org-filtered)
3322 (while (setq beg (text-property-any (point-min) (point-max)
3323 'org-filtered t))
3324 (delete-region
3325 beg (or (next-single-property-change beg 'org-filtered)
3326 (point-max))))
3327 (run-hooks 'org-agenda-before-write-hook)
3328 (cond
3329 ((org-bound-and-true-p org-mobile-creating-agendas)
3330 (org-mobile-write-agenda-for-mobile file))
3331 ((string-match "\\.org\\'" file)
3332 (let (content p m message-log-max)
3333 (goto-char (point-min))
3334 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3335 (goto-char p)
3336 (setq m (get-text-property (point) 'org-hd-marker))
3337 (when m
3338 (push (save-excursion
3339 (set-buffer (marker-buffer m))
3340 (goto-char m)
3341 (org-copy-subtree 1 nil t t)
3342 org-subtree-clip)
3343 content)))
3344 (find-file file)
3345 (erase-buffer)
3346 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3347 (write-file file)
3348 (kill-buffer (current-buffer))
3349 (message "Org file written to %s" file)))
3350 ((string-match "\\.html?\\'" file)
3351 (require 'htmlize)
3352 (set-buffer (htmlize-buffer (current-buffer)))
3353 (when org-agenda-export-html-style
3354 ;; replace <style> section with org-agenda-export-html-style
3355 (goto-char (point-min))
3356 (kill-region (- (search-forward "<style") 6)
3357 (search-forward "</style>"))
3358 (insert org-agenda-export-html-style))
3359 (write-file file)
3360 (kill-buffer (current-buffer))
3361 (message "HTML written to %s" file))
3362 ((string-match "\\.ps\\'" file)
3363 (require 'ps-print)
3364 (ps-print-buffer-with-faces file)
3365 (message "Postscript written to %s" file))
3366 ((string-match "\\.pdf\\'" file)
3367 (require 'ps-print)
3368 (ps-print-buffer-with-faces
3369 (concat (file-name-sans-extension file) ".ps"))
3370 (call-process "ps2pdf" nil nil nil
3371 (expand-file-name
3372 (concat (file-name-sans-extension file) ".ps"))
3373 (expand-file-name file))
3374 (delete-file (concat (file-name-sans-extension file) ".ps"))
3375 (message "PDF written to %s" file))
3376 ((string-match "\\.ics\\'" file)
3377 (require 'ox-icalendar)
3378 (org-icalendar-export-current-agenda (expand-file-name file)))
3380 (let ((bs (buffer-string)))
3381 (find-file file)
3382 (erase-buffer)
3383 (insert bs)
3384 (save-buffer 0)
3385 (kill-buffer (current-buffer))
3386 (message "Plain text written to %s" file))))))))
3387 (set-buffer (or agenda-bufname
3388 (and (org-called-interactively-p 'any) (buffer-name))
3389 org-agenda-buffer-name)))
3390 (when open (org-open-file file)))
3392 (defvar org-agenda-tag-filter-overlays nil)
3393 (defvar org-agenda-cat-filter-overlays nil)
3394 (defvar org-agenda-re-filter-overlays nil)
3396 (defun org-agenda-mark-filtered-text ()
3397 "Mark all text hidden by filtering with a text property."
3398 (let ((inhibit-read-only t))
3399 (mapc
3400 (lambda (o)
3401 (when (equal (overlay-buffer o) (current-buffer))
3402 (put-text-property
3403 (overlay-start o) (overlay-end o)
3404 'org-filtered t)))
3405 (append org-agenda-tag-filter-overlays
3406 org-agenda-cat-filter-overlays
3407 org-agenda-re-filter-overlays))))
3409 (defun org-agenda-unmark-filtered-text ()
3410 "Remove the filtering text property."
3411 (let ((inhibit-read-only t))
3412 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3414 (defun org-agenda-remove-marked-text (property &optional value)
3415 "Delete all text marked with VALUE of PROPERTY.
3416 VALUE defaults to t."
3417 (let (beg)
3418 (setq value (or value t))
3419 (while (setq beg (text-property-any (point-min) (point-max)
3420 property value))
3421 (delete-region
3422 beg (or (next-single-property-change beg 'org-filtered)
3423 (point-max))))))
3425 (defun org-agenda-add-entry-text ()
3426 "Add entry text to agenda lines.
3427 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3428 entry text following headings shown in the agenda.
3429 Drawers will be excluded, also the line with scheduling/deadline info."
3430 (when (and (> org-agenda-add-entry-text-maxlines 0)
3431 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3432 (let (m txt)
3433 (goto-char (point-min))
3434 (while (not (eobp))
3435 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3436 (beginning-of-line 2)
3437 (setq txt (org-agenda-get-some-entry-text
3438 m org-agenda-add-entry-text-maxlines " > "))
3439 (end-of-line 1)
3440 (if (string-match "\\S-" txt)
3441 (insert "\n" txt)
3442 (or (eobp) (forward-char 1))))))))
3444 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3445 &rest keep)
3446 "Extract entry text from MARKER, at most N-LINES lines.
3447 This will ignore drawers etc, just get the text.
3448 If INDENT is given, prefix every line with this string. If KEEP is
3449 given, it is a list of symbols, defining stuff that should not be
3450 removed from the entry content. Currently only `planning' is allowed here."
3451 (let (txt drawer-re kwd-time-re ind)
3452 (save-excursion
3453 (with-current-buffer (marker-buffer marker)
3454 (if (not (derived-mode-p 'org-mode))
3455 (setq txt "")
3456 (save-excursion
3457 (save-restriction
3458 (widen)
3459 (goto-char marker)
3460 (end-of-line 1)
3461 (setq txt (buffer-substring
3462 (min (1+ (point)) (point-max))
3463 (progn (outline-next-heading) (point)))
3464 drawer-re org-drawer-regexp
3465 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3466 ".*\n?"))
3467 (with-temp-buffer
3468 (insert txt)
3469 (when org-agenda-add-entry-text-descriptive-links
3470 (goto-char (point-min))
3471 (while (org-activate-bracket-links (point-max))
3472 (add-text-properties (match-beginning 0) (match-end 0)
3473 '(face org-link))))
3474 (goto-char (point-min))
3475 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3476 (set-text-properties (match-beginning 0) (match-end 0)
3477 nil))
3478 (goto-char (point-min))
3479 (while (re-search-forward drawer-re nil t)
3480 (delete-region
3481 (match-beginning 0)
3482 (progn (re-search-forward
3483 "^[ \t]*:END:.*\n?" nil 'move)
3484 (point))))
3485 (unless (member 'planning keep)
3486 (goto-char (point-min))
3487 (while (re-search-forward kwd-time-re nil t)
3488 (replace-match "")))
3489 (goto-char (point-min))
3490 (when org-agenda-entry-text-exclude-regexps
3491 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3492 (while (setq re (pop re-list))
3493 (goto-char (point-min))
3494 (while (re-search-forward re nil t)
3495 (replace-match "")))))
3496 (goto-char (point-max))
3497 (skip-chars-backward " \t\n")
3498 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3500 ;; find and remove min common indentation
3501 (goto-char (point-min))
3502 (untabify (point-min) (point-max))
3503 (setq ind (org-get-indentation))
3504 (while (not (eobp))
3505 (unless (looking-at "[ \t]*$")
3506 (setq ind (min ind (org-get-indentation))))
3507 (beginning-of-line 2))
3508 (goto-char (point-min))
3509 (while (not (eobp))
3510 (unless (looking-at "[ \t]*$")
3511 (move-to-column ind)
3512 (delete-region (point-at-bol) (point)))
3513 (beginning-of-line 2))
3515 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3517 (goto-char (point-min))
3518 (when indent
3519 (while (and (not (eobp)) (re-search-forward "^" nil t))
3520 (replace-match indent t t)))
3521 (goto-char (point-min))
3522 (while (looking-at "[ \t]*\n") (replace-match ""))
3523 (goto-char (point-max))
3524 (when (> (org-current-line)
3525 n-lines)
3526 (org-goto-line (1+ n-lines))
3527 (backward-char 1))
3528 (setq txt (buffer-substring (point-min) (point)))))))))
3529 txt))
3531 (defun org-check-for-org-mode ()
3532 "Make sure current buffer is in org-mode. Error if not."
3533 (or (derived-mode-p 'org-mode)
3534 (error "Cannot execute org-mode agenda command on buffer in %s"
3535 major-mode)))
3537 ;;; Agenda prepare and finalize
3539 (defvar org-agenda-multi nil) ; dynamically scoped
3540 (defvar org-agenda-pre-window-conf nil)
3541 (defvar org-agenda-columns-active nil)
3542 (defvar org-agenda-name nil)
3543 (defvar org-agenda-tag-filter nil)
3544 (defvar org-agenda-category-filter nil)
3545 (defvar org-agenda-regexp-filter nil)
3546 (defvar org-agenda-top-headline-filter nil)
3547 (defvar org-agenda-tag-filter-while-redo nil)
3548 (defvar org-agenda-tag-filter-preset nil
3549 "A preset of the tags filter used for secondary agenda filtering.
3550 This must be a list of strings, each string must be a single tag preceded
3551 by \"+\" or \"-\".
3552 This variable should not be set directly, but agenda custom commands can
3553 bind it in the options section. The preset filter is a global property of
3554 the entire agenda view. In a block agenda, it will not work reliably to
3555 define a filter for one of the individual blocks. You need to set it in
3556 the global options and expect it to be applied to the entire view.")
3558 (defvar org-agenda-category-filter-preset nil
3559 "A preset of the category filter used for secondary agenda filtering.
3560 This must be a list of strings, each string must be a single category
3561 preceded by \"+\" or \"-\".
3562 This variable should not be set directly, but agenda custom commands can
3563 bind it in the options section. The preset filter is a global property of
3564 the entire agenda view. In a block agenda, it will not work reliably to
3565 define a filter for one of the individual blocks. You need to set it in
3566 the global options and expect it to be applied to the entire view.")
3568 (defvar org-agenda-regexp-filter-preset nil
3569 "A preset of the regexp filter used for secondary agenda filtering.
3570 This must be a list of strings, each string must be a single category
3571 preceded by \"+\" or \"-\".
3572 This variable should not be set directly, but agenda custom commands can
3573 bind it in the options section. The preset filter is a global property of
3574 the entire agenda view. In a block agenda, it will not work reliably to
3575 define a filter for one of the individual blocks. You need to set it in
3576 the global options and expect it to be applied to the entire view.")
3578 (defun org-agenda-use-sticky-p ()
3579 "Return non-nil if an agenda buffer named
3580 `org-agenda-buffer-name' exists and should be shown instead of
3581 generating a new one."
3582 (and
3583 ;; turned off by user
3584 org-agenda-sticky
3585 ;; For multi-agenda buffer already exists
3586 (not org-agenda-multi)
3587 ;; buffer found
3588 (get-buffer org-agenda-buffer-name)
3589 ;; C-u parameter is same as last call
3590 (with-current-buffer (get-buffer org-agenda-buffer-name)
3591 (and
3592 (equal current-prefix-arg
3593 org-agenda-last-prefix-arg)
3594 ;; In case user turned stickiness on, while having existing
3595 ;; Agenda buffer active, don't reuse that buffer, because it
3596 ;; does not have org variables local
3597 org-agenda-this-buffer-is-sticky))))
3599 (defun org-agenda-prepare-window (abuf)
3600 "Setup agenda buffer in the window."
3601 (let* ((awin (get-buffer-window abuf))
3602 wconf)
3603 (cond
3604 ((equal (current-buffer) abuf) nil)
3605 (awin (select-window awin))
3606 ((not (setq wconf (current-window-configuration))))
3607 ((equal org-agenda-window-setup 'current-window)
3608 (org-pop-to-buffer-same-window abuf))
3609 ((equal org-agenda-window-setup 'other-window)
3610 (org-switch-to-buffer-other-window abuf))
3611 ((equal org-agenda-window-setup 'other-frame)
3612 (switch-to-buffer-other-frame abuf))
3613 ((equal org-agenda-window-setup 'reorganize-frame)
3614 (delete-other-windows)
3615 (org-switch-to-buffer-other-window abuf)))
3616 ;; additional test in case agenda is invoked from within agenda
3617 ;; buffer via elisp link
3618 (unless (equal (current-buffer) abuf)
3619 (org-pop-to-buffer-same-window abuf))
3620 (setq org-agenda-pre-window-conf
3621 (or org-agenda-pre-window-conf wconf))))
3623 (defun org-agenda-prepare (&optional name)
3624 (if (org-agenda-use-sticky-p)
3625 (progn
3626 ;; Popup existing buffer
3627 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3628 (message "Sticky Agenda buffer, use `r' to refresh")
3629 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3630 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3631 (setq org-todo-keywords-for-agenda nil)
3632 (setq org-drawers-for-agenda nil)
3633 (unless org-agenda-persistent-filter
3634 (setq org-agenda-tag-filter nil
3635 org-agenda-category-filter nil
3636 org-agenda-regexp-filter nil))
3637 (put 'org-agenda-tag-filter :preset-filter
3638 org-agenda-tag-filter-preset)
3639 (put 'org-agenda-category-filter :preset-filter
3640 org-agenda-category-filter-preset)
3641 (put 'org-agenda-regexp-filter :preset-filter
3642 org-agenda-regexp-filter-preset)
3643 (if org-agenda-multi
3644 (progn
3645 (setq buffer-read-only nil)
3646 (goto-char (point-max))
3647 (unless (or (bobp) org-agenda-compact-blocks
3648 (not org-agenda-block-separator))
3649 (insert "\n"
3650 (if (stringp org-agenda-block-separator)
3651 org-agenda-block-separator
3652 (make-string (window-width) org-agenda-block-separator))
3653 "\n"))
3654 (narrow-to-region (point) (point-max)))
3655 (setq org-done-keywords-for-agenda nil)
3657 ;; Setting any org variables that are in org-agenda-local-vars
3658 ;; list need to be done after the prepare call
3659 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3660 (setq buffer-read-only nil)
3661 (org-agenda-reset-markers)
3662 (let ((inhibit-read-only t)) (erase-buffer))
3663 (org-agenda-mode)
3664 (setq org-agenda-buffer (current-buffer))
3665 (setq org-agenda-contributing-files nil)
3666 (setq org-agenda-columns-active nil)
3667 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3668 (setq org-todo-keywords-for-agenda
3669 (org-uniquify org-todo-keywords-for-agenda))
3670 (setq org-done-keywords-for-agenda
3671 (org-uniquify org-done-keywords-for-agenda))
3672 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3673 (setq org-agenda-last-prefix-arg current-prefix-arg)
3674 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3675 (and name (not org-agenda-name)
3676 (org-set-local 'org-agenda-name name)))
3677 (setq buffer-read-only nil)))
3679 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3680 (defun org-agenda-finalize ()
3681 "Finishing touch for the agenda buffer, called just before displaying it."
3682 (unless org-agenda-multi
3683 (save-excursion
3684 (let ((inhibit-read-only t))
3685 (goto-char (point-min))
3686 (save-excursion
3687 (while (org-activate-bracket-links (point-max))
3688 (add-text-properties (match-beginning 0) (match-end 0)
3689 '(face org-link))))
3690 (save-excursion
3691 (while (org-activate-plain-links (point-max))
3692 (add-text-properties (match-beginning 0) (match-end 0)
3693 '(face org-link))))
3694 (unless (eq org-agenda-remove-tags t)
3695 (org-agenda-align-tags))
3696 (unless org-agenda-with-colors
3697 (remove-text-properties (point-min) (point-max) '(face nil)))
3698 (if (and (boundp 'org-agenda-overriding-columns-format)
3699 org-agenda-overriding-columns-format)
3700 (org-set-local 'org-agenda-overriding-columns-format
3701 org-agenda-overriding-columns-format))
3702 (if (and (boundp 'org-agenda-view-columns-initially)
3703 org-agenda-view-columns-initially)
3704 (org-agenda-columns))
3705 (when org-agenda-fontify-priorities
3706 (org-agenda-fontify-priorities))
3707 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3708 (org-agenda-dim-blocked-tasks))
3709 ;; We need to widen when `org-agenda-finalize' is called from
3710 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3711 (when org-clock-current-task
3712 (save-restriction
3713 (widen)
3714 (org-agenda-mark-clocking-task)))
3715 (when org-agenda-entry-text-mode
3716 (org-agenda-entry-text-hide)
3717 (org-agenda-entry-text-show))
3718 (if (and (functionp 'org-habit-insert-consistency-graphs)
3719 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3720 (org-habit-insert-consistency-graphs))
3721 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3722 (unless (or (eq org-agenda-show-inherited-tags 'always)
3723 (and (listp org-agenda-show-inherited-tags)
3724 (memq org-agenda-type org-agenda-show-inherited-tags))
3725 (and (eq org-agenda-show-inherited-tags t)
3726 (or (eq org-agenda-use-tag-inheritance t)
3727 (and (listp org-agenda-use-tag-inheritance)
3728 (not (memq org-agenda-type
3729 org-agenda-use-tag-inheritance))))))
3730 (let (mrk)
3731 (save-excursion
3732 (goto-char (point-min))
3733 (while (equal (forward-line) 0)
3734 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3735 (get-text-property (point) 'org-hd-marker)))
3736 (put-text-property (point-at-bol) (point-at-eol)
3737 'tags (org-with-point-at mrk
3738 (delete-dups
3739 (mapcar 'downcase (org-get-tags-at))))))))))
3740 (run-hooks 'org-agenda-finalize-hook)
3741 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3742 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3743 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3744 (org-agenda-filter-apply org-agenda-category-filter 'category))
3745 (when (or org-agenda-regexp-filter (get 'org-agenda-regexp-filter :preset-filter))
3746 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3747 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3749 (defun org-agenda-mark-clocking-task ()
3750 "Mark the current clock entry in the agenda if it is present."
3751 (org-agenda-unmark-clocking-task)
3752 (when (marker-buffer org-clock-hd-marker)
3753 (save-excursion
3754 (goto-char (point-min))
3755 (let (s ov)
3756 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3757 (goto-char s)
3758 (when (equal (org-get-at-bol 'org-hd-marker)
3759 org-clock-hd-marker)
3760 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3761 (overlay-put ov 'type 'org-agenda-clocking)
3762 (overlay-put ov 'face 'org-agenda-clocking)
3763 (overlay-put ov 'help-echo
3764 "The clock is running in this item")))))))
3766 (defun org-agenda-unmark-clocking-task ()
3767 "Unmark the current clocking task."
3768 (mapc (lambda (o)
3769 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3770 (delete-overlay o)))
3771 (overlays-in (point-min) (point-max))))
3773 (defun org-agenda-fontify-priorities ()
3774 "Make highest priority lines bold, and lowest italic."
3775 (interactive)
3776 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3777 (delete-overlay o)))
3778 (overlays-in (point-min) (point-max)))
3779 (save-excursion
3780 (let (b e p ov h l)
3781 (goto-char (point-min))
3782 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3783 (setq h (or (get-char-property (point) 'org-highest-priority)
3784 org-highest-priority)
3785 l (or (get-char-property (point) 'org-lowest-priority)
3786 org-lowest-priority)
3787 p (string-to-char (match-string 1))
3788 b (match-beginning 0)
3789 e (if (eq org-agenda-fontify-priorities 'cookies)
3790 (match-end 0)
3791 (point-at-eol))
3792 ov (make-overlay b e))
3793 (overlay-put
3794 ov 'face
3795 (cons (cond ((org-face-from-face-or-color
3796 'priority nil
3797 (cdr (assoc p org-priority-faces))))
3798 ((and (listp org-agenda-fontify-priorities)
3799 (org-face-from-face-or-color
3800 'priority nil
3801 (cdr (assoc p org-agenda-fontify-priorities)))))
3802 ((equal p l) 'italic)
3803 ((equal p h) 'bold))
3804 'org-priority))
3805 (overlay-put ov 'org-type 'org-priority)))))
3807 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3808 "Dim currently blocked TODO's in the agenda display.
3809 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3810 dimming them."
3811 (interactive "P")
3812 (when (org-called-interactively-p 'interactive)
3813 (message "Dim or hide blocked tasks..."))
3814 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3815 (delete-overlay o)))
3816 (overlays-in (point-min) (point-max)))
3817 (save-excursion
3818 (let ((inhibit-read-only t)
3819 (org-depend-tag-blocked nil)
3820 (invis (or (not (null invisible))
3821 (eq org-agenda-dim-blocked-tasks 'invisible)))
3822 org-blocked-by-checkboxes
3823 invis1 b e p ov h l)
3824 (goto-char (point-min))
3825 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3826 (and pos (goto-char (1+ pos))))
3827 (setq org-blocked-by-checkboxes nil invis1 invis)
3828 (let ((marker (org-get-at-bol 'org-hd-marker)))
3829 (when (and marker
3830 (with-current-buffer (marker-buffer marker)
3831 (save-excursion (goto-char marker)
3832 (org-entry-blocked-p))))
3833 (if org-blocked-by-checkboxes (setq invis1 nil))
3834 (setq b (if invis1
3835 (max (point-min) (1- (point-at-bol)))
3836 (point-at-bol))
3837 e (point-at-eol)
3838 ov (make-overlay b e))
3839 (if invis1
3840 (overlay-put ov 'invisible t)
3841 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3842 (overlay-put ov 'org-type 'org-blocked-todo))))))
3843 (when (org-called-interactively-p 'interactive)
3844 (message "Dim or hide blocked tasks...done")))
3846 (defvar org-agenda-skip-function nil
3847 "Function to be called at each match during agenda construction.
3848 If this function returns nil, the current match should not be skipped.
3849 Otherwise, the function must return a position from where the search
3850 should be continued.
3851 This may also be a Lisp form, it will be evaluated.
3852 Never set this variable using `setq' or so, because then it will apply
3853 to all future agenda commands. If you do want a global skipping condition,
3854 use the option `org-agenda-skip-function-global' instead.
3855 The correct usage for `org-agenda-skip-function' is to bind it with
3856 `let' to scope it dynamically into the agenda-constructing command.
3857 A good way to set it is through options in `org-agenda-custom-commands'.")
3859 (defun org-agenda-skip ()
3860 "Throw to `:skip' in places that should be skipped.
3861 Also moves point to the end of the skipped region, so that search can
3862 continue from there."
3863 (let ((p (point-at-bol)) to)
3864 (when (or
3865 (save-excursion (goto-char p) (looking-at comment-start-skip))
3866 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3867 (get-text-property p :org-archived)
3868 (org-end-of-subtree t))
3869 (and org-agenda-skip-comment-trees
3870 (get-text-property p :org-comment)
3871 (org-end-of-subtree t))
3872 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3873 (org-agenda-skip-eval org-agenda-skip-function)))
3874 (goto-char to))
3875 (org-in-src-block-p t))
3876 (throw :skip t))))
3878 (defun org-agenda-skip-eval (form)
3879 "If FORM is a function or a list, call (or eval) is and return result.
3880 `save-excursion' and `save-match-data' are wrapped around the call, so point
3881 and match data are returned to the previous state no matter what these
3882 functions do."
3883 (let (fp)
3884 (and form
3885 (or (setq fp (functionp form))
3886 (consp form))
3887 (save-excursion
3888 (save-match-data
3889 (if fp
3890 (funcall form)
3891 (eval form)))))))
3893 (defvar org-agenda-markers nil
3894 "List of all currently active markers created by `org-agenda'.")
3895 (defvar org-agenda-last-marker-time (org-float-time)
3896 "Creation time of the last agenda marker.")
3898 (defun org-agenda-new-marker (&optional pos)
3899 "Return a new agenda marker.
3900 Org-mode keeps a list of these markers and resets them when they are
3901 no longer in use."
3902 (let ((m (copy-marker (or pos (point)))))
3903 (setq org-agenda-last-marker-time (org-float-time))
3904 (if org-agenda-buffer
3905 (with-current-buffer org-agenda-buffer
3906 (push m org-agenda-markers))
3907 (push m org-agenda-markers))
3910 (defun org-agenda-reset-markers ()
3911 "Reset markers created by `org-agenda'."
3912 (while org-agenda-markers
3913 (move-marker (pop org-agenda-markers) nil)))
3915 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3916 "Save relative positions of markers in region.
3917 This check for agenda markers in all agenda buffers currently active."
3918 (dolist (buf (buffer-list))
3919 (with-current-buffer buf
3920 (when (eq major-mode 'org-agenda-mode)
3921 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3922 org-agenda-markers)))))
3924 ;;; Entry text mode
3926 (defun org-agenda-entry-text-show-here ()
3927 "Add some text from the entry as context to the current line."
3928 (let (m txt o)
3929 (setq m (org-get-at-bol 'org-hd-marker))
3930 (unless (marker-buffer m)
3931 (error "No marker points to an entry here"))
3932 (setq txt (concat "\n" (org-no-properties
3933 (org-agenda-get-some-entry-text
3934 m org-agenda-entry-text-maxlines
3935 org-agenda-entry-text-leaders))))
3936 (when (string-match "\\S-" txt)
3937 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3938 (overlay-put o 'evaporate t)
3939 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3940 (overlay-put o 'after-string txt))))
3942 (defun org-agenda-entry-text-show ()
3943 "Add entry context for all agenda lines."
3944 (interactive)
3945 (save-excursion
3946 (goto-char (point-max))
3947 (beginning-of-line 1)
3948 (while (not (bobp))
3949 (when (org-get-at-bol 'org-hd-marker)
3950 (org-agenda-entry-text-show-here))
3951 (beginning-of-line 0))))
3953 (defun org-agenda-entry-text-hide ()
3954 "Remove any shown entry context."
3955 (delq nil
3956 (mapcar (lambda (o)
3957 (if (eq (overlay-get o 'org-overlay-type)
3958 'agenda-entry-content)
3959 (progn (delete-overlay o) t)))
3960 (overlays-in (point-min) (point-max)))))
3962 (defun org-agenda-get-day-face (date)
3963 "Return the face DATE should be displayed with."
3964 (or (and (functionp org-agenda-day-face-function)
3965 (funcall org-agenda-day-face-function date))
3966 (cond ((org-agenda-todayp date)
3967 'org-agenda-date-today)
3968 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3969 'org-agenda-date-weekend)
3970 (t 'org-agenda-date))))
3972 ;;; Agenda timeline
3974 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3976 (defun org-timeline (&optional dotodo)
3977 "Show a time-sorted view of the entries in the current org file.
3978 Only entries with a time stamp of today or later will be listed. With
3979 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3980 under the current date.
3981 If the buffer contains an active region, only check the region for
3982 dates."
3983 (interactive "P")
3984 (let* ((dopast t)
3985 (org-agenda-show-log-scoped org-agenda-show-log)
3986 (org-agenda-show-log org-agenda-show-log-scoped)
3987 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3988 (current-buffer))))
3989 (date (calendar-current-date))
3990 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3991 (end (if (org-region-active-p) (region-end) (point-max)))
3992 (day-numbers (org-get-all-dates
3993 beg end 'no-ranges
3994 t org-agenda-show-log-scoped ; always include today
3995 org-timeline-show-empty-dates))
3996 (org-deadline-warning-days 0)
3997 (org-agenda-only-exact-dates t)
3998 (today (org-today))
3999 (past t)
4000 args
4001 s e rtn d emptyp)
4002 (setq org-agenda-redo-command
4003 (list 'let
4004 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4005 (list 'org-switch-to-buffer-other-window (current-buffer))
4006 (list 'org-timeline (list 'quote dotodo))))
4007 (put 'org-agenda-redo-command 'org-lprops nil)
4008 (if (not dopast)
4009 ;; Remove past dates from the list of dates.
4010 (setq day-numbers (delq nil (mapcar (lambda(x)
4011 (if (>= x today) x nil))
4012 day-numbers))))
4013 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4014 (org-compile-prefix-format 'timeline)
4015 (org-set-sorting-strategy 'timeline)
4016 (if org-agenda-show-log-scoped (push :closed args))
4017 (push :timestamp args)
4018 (push :deadline args)
4019 (push :scheduled args)
4020 (push :sexp args)
4021 (if dotodo (push :todo args))
4022 (insert "Timeline of file " entry "\n")
4023 (add-text-properties (point-min) (point)
4024 (list 'face 'org-agenda-structure))
4025 (org-agenda-mark-header-line (point-min))
4026 (while (setq d (pop day-numbers))
4027 (if (and (listp d) (eq (car d) :omitted))
4028 (progn
4029 (setq s (point))
4030 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4031 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4032 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4033 (if (and (>= d today)
4034 dopast
4035 past)
4036 (progn
4037 (setq past nil)
4038 (insert (make-string 79 ?-) "\n")))
4039 (setq date (calendar-gregorian-from-absolute d))
4040 (setq s (point))
4041 (setq rtn (and (not emptyp)
4042 (apply 'org-agenda-get-day-entries entry
4043 date args)))
4044 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4045 (progn
4046 (insert
4047 (if (stringp org-agenda-format-date)
4048 (format-time-string org-agenda-format-date
4049 (org-time-from-absolute date))
4050 (funcall org-agenda-format-date date))
4051 "\n")
4052 (put-text-property s (1- (point)) 'face
4053 (org-agenda-get-day-face date))
4054 (put-text-property s (1- (point)) 'org-date-line t)
4055 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4056 (if (equal d today)
4057 (put-text-property s (1- (point)) 'org-today t))
4058 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4059 (put-text-property s (1- (point)) 'day d)))))
4060 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4061 (point-min)))
4062 (add-text-properties
4063 (point-min) (point-max)
4064 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4065 (org-agenda-finalize)
4066 (setq buffer-read-only t)))
4068 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4069 "Return a list of all relevant day numbers from BEG to END buffer positions.
4070 If NO-RANGES is non-nil, include only the start and end dates of a range,
4071 not every single day in the range. If FORCE-TODAY is non-nil, make
4072 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4073 inactive time stamps (those in square brackets) are included.
4074 When EMPTY is non-nil, also include days without any entries."
4075 (let ((re (concat
4076 (if pre-re pre-re "")
4077 (if inactive org-ts-regexp-both org-ts-regexp)))
4078 dates dates1 date day day1 day2 ts1 ts2 pos)
4079 (if force-today
4080 (setq dates (list (org-today))))
4081 (save-excursion
4082 (goto-char beg)
4083 (while (re-search-forward re end t)
4084 (setq day (time-to-days (org-time-string-to-time
4085 (substring (match-string 1) 0 10)
4086 (current-buffer) (match-beginning 0))))
4087 (or (memq day dates) (push day dates)))
4088 (unless no-ranges
4089 (goto-char beg)
4090 (while (re-search-forward org-tr-regexp end t)
4091 (setq pos (match-beginning 0))
4092 (setq ts1 (substring (match-string 1) 0 10)
4093 ts2 (substring (match-string 2) 0 10)
4094 day1 (time-to-days (org-time-string-to-time
4095 ts1 (current-buffer) pos))
4096 day2 (time-to-days (org-time-string-to-time
4097 ts2 (current-buffer) pos)))
4098 (while (< (setq day1 (1+ day1)) day2)
4099 (or (memq day1 dates) (push day1 dates)))))
4100 (setq dates (sort dates '<))
4101 (when empty
4102 (while (setq day (pop dates))
4103 (setq day2 (car dates))
4104 (push day dates1)
4105 (when (and day2 empty)
4106 (if (or (eq empty t)
4107 (and (numberp empty) (<= (- day2 day) empty)))
4108 (while (< (setq day (1+ day)) day2)
4109 (push (list day) dates1))
4110 (push (cons :omitted (- day2 day)) dates1))))
4111 (setq dates (nreverse dates1)))
4112 dates)))
4114 ;;; Agenda Daily/Weekly
4116 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4117 "Start day for the agenda view.
4118 Custom commands can set this variable in the options section.
4119 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4120 input allowed when reading a date through the Org calendar.
4121 See the docstring of `org-read-date' for details.")
4122 (defvar org-starting-day nil) ; local variable in the agenda buffer
4123 (defvar org-arg-loc nil) ; local variable
4125 (defvar org-agenda-buffer-tmp-name nil)
4126 ;;;###autoload
4127 (defun org-agenda-list (&optional arg start-day span with-hour)
4128 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4129 The view will be for the current day or week, but from the overview buffer
4130 you will be able to go to other days/weeks.
4132 With a numeric prefix argument in an interactive call, the agenda will
4133 span ARG days. Lisp programs should instead specify SPAN to change
4134 the number of days. SPAN defaults to `org-agenda-span'.
4136 START-DAY defaults to TODAY, or to the most recent match for the weekday
4137 given in `org-agenda-start-on-weekday'.
4139 When WITH-HOUR is non-nil, only include scheduled and deadline
4140 items if they have an hour specification like [h]h:mm."
4141 (interactive "P")
4142 (if org-agenda-overriding-arguments
4143 (setq arg (car org-agenda-overriding-arguments)
4144 start-day (nth 1 org-agenda-overriding-arguments)
4145 span (nth 2 org-agenda-overriding-arguments)))
4146 (if (and (integerp arg) (> arg 0))
4147 (setq span arg arg nil))
4148 (catch 'exit
4149 (setq org-agenda-buffer-name
4150 (or org-agenda-buffer-tmp-name
4151 (if org-agenda-sticky
4152 (cond ((and org-keys (stringp org-match))
4153 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4154 (org-keys
4155 (format "*Org Agenda(%s)*" org-keys))
4156 (t "*Org Agenda(a)*")))
4157 org-agenda-buffer-name))
4158 (org-agenda-prepare "Day/Week")
4159 (setq start-day (or start-day org-agenda-start-day))
4160 (if (stringp start-day)
4161 ;; Convert to an absolute day number
4162 (setq start-day (time-to-days (org-read-date nil t start-day))))
4163 (org-compile-prefix-format 'agenda)
4164 (org-set-sorting-strategy 'agenda)
4165 (let* ((span (org-agenda-ndays-to-span
4166 (or span org-agenda-ndays org-agenda-span)))
4167 (today (org-today))
4168 (sd (or start-day today))
4169 (ndays (org-agenda-span-to-ndays span sd))
4170 (org-agenda-start-on-weekday
4171 (if (eq ndays 7)
4172 org-agenda-start-on-weekday))
4173 (thefiles (org-agenda-files nil 'ifmode))
4174 (files thefiles)
4175 (start (if (or (null org-agenda-start-on-weekday)
4176 (< ndays 7))
4178 (let* ((nt (calendar-day-of-week
4179 (calendar-gregorian-from-absolute sd)))
4180 (n1 org-agenda-start-on-weekday)
4181 (d (- nt n1)))
4182 (- sd (+ (if (< d 0) 7 0) d)))))
4183 (day-numbers (list start))
4184 (day-cnt 0)
4185 (inhibit-redisplay (not debug-on-error))
4186 (org-agenda-show-log-scoped org-agenda-show-log)
4187 s e rtn rtnall file date d start-pos end-pos todayp
4188 clocktable-start clocktable-end filter)
4189 (setq org-agenda-redo-command
4190 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4191 (dotimes (n (1- ndays))
4192 (push (1+ (car day-numbers)) day-numbers))
4193 (setq day-numbers (nreverse day-numbers))
4194 (setq clocktable-start (car day-numbers)
4195 clocktable-end (1+ (or (org-last day-numbers) 0)))
4196 (org-set-local 'org-starting-day (car day-numbers))
4197 (org-set-local 'org-arg-loc arg)
4198 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4199 (unless org-agenda-compact-blocks
4200 (let* ((d1 (car day-numbers))
4201 (d2 (org-last day-numbers))
4202 (w1 (org-days-to-iso-week d1))
4203 (w2 (org-days-to-iso-week d2)))
4204 (setq s (point))
4205 (if org-agenda-overriding-header
4206 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4207 nil 'face 'org-agenda-structure) "\n")
4208 (insert (org-agenda-span-name span)
4209 "-agenda"
4210 (if (< (- d2 d1) 350)
4211 (if (= w1 w2)
4212 (format " (W%02d)" w1)
4213 (format " (W%02d-W%02d)" w1 w2))
4215 ":\n")))
4216 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4217 'org-date-line t))
4218 (org-agenda-mark-header-line s))
4219 (while (setq d (pop day-numbers))
4220 (setq date (calendar-gregorian-from-absolute d)
4221 s (point))
4222 (if (or (setq todayp (= d today))
4223 (and (not start-pos) (= d sd)))
4224 (setq start-pos (point))
4225 (if (and start-pos (not end-pos))
4226 (setq end-pos (point))))
4227 (setq files thefiles
4228 rtnall nil)
4229 (while (setq file (pop files))
4230 (catch 'nextfile
4231 (org-check-agenda-file file)
4232 (let ((org-agenda-entry-types org-agenda-entry-types))
4233 ;; Starred types override non-starred equivalents
4234 (when (member :deadline* org-agenda-entry-types)
4235 (setq org-agenda-entry-types
4236 (delq :deadline org-agenda-entry-types)))
4237 (when (member :scheduled* org-agenda-entry-types)
4238 (setq org-agenda-entry-types
4239 (delq :scheduled org-agenda-entry-types)))
4240 ;; Honor with-hour
4241 (when with-hour
4242 (when (member :deadline org-agenda-entry-types)
4243 (setq org-agenda-entry-types
4244 (delq :deadline org-agenda-entry-types))
4245 (push :deadline* org-agenda-entry-types))
4246 (when (member :scheduled org-agenda-entry-types)
4247 (setq org-agenda-entry-types
4248 (delq :scheduled org-agenda-entry-types))
4249 (push :scheduled* org-agenda-entry-types)))
4250 (unless org-agenda-include-deadlines
4251 (setq org-agenda-entry-types
4252 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4253 (cond
4254 ((memq org-agenda-show-log-scoped '(only clockcheck))
4255 (setq rtn (org-agenda-get-day-entries
4256 file date :closed)))
4257 (org-agenda-show-log-scoped
4258 (setq rtn (apply 'org-agenda-get-day-entries
4259 file date
4260 (append '(:closed) org-agenda-entry-types))))
4262 (setq rtn (apply 'org-agenda-get-day-entries
4263 file date
4264 org-agenda-entry-types)))))
4265 (setq rtnall (append rtnall rtn)))) ;; all entries
4266 (if org-agenda-include-diary
4267 (let ((org-agenda-search-headline-for-time t))
4268 (require 'diary-lib)
4269 (setq rtn (org-get-entries-from-diary date))
4270 (setq rtnall (append rtnall rtn))))
4271 (if (or rtnall org-agenda-show-all-dates)
4272 (progn
4273 (setq day-cnt (1+ day-cnt))
4274 (insert
4275 (if (stringp org-agenda-format-date)
4276 (format-time-string org-agenda-format-date
4277 (org-time-from-absolute date))
4278 (funcall org-agenda-format-date date))
4279 "\n")
4280 (put-text-property s (1- (point)) 'face
4281 (org-agenda-get-day-face date))
4282 (put-text-property s (1- (point)) 'org-date-line t)
4283 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4284 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4285 (when todayp
4286 (put-text-property s (1- (point)) 'org-today t))
4287 (setq rtnall
4288 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4289 (if rtnall (insert ;; all entries
4290 (org-agenda-finalize-entries rtnall 'agenda)
4291 "\n"))
4292 (put-text-property s (1- (point)) 'day d)
4293 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4294 (when (and org-agenda-clockreport-mode clocktable-start)
4295 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4296 ;; the above line is to ensure the restricted range!
4297 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4298 tbl)
4299 (setq p (org-plist-delete p :block))
4300 (setq p (plist-put p :tstart clocktable-start))
4301 (setq p (plist-put p :tend clocktable-end))
4302 (setq p (plist-put p :scope 'agenda))
4303 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4304 (setq filter (or org-agenda-tag-filter-while-redo
4305 (get 'org-agenda-tag-filter :preset-filter))))
4306 (setq p (plist-put p :tags (mapconcat (lambda (x)
4307 (if (string-match "[<>=]" x)
4310 filter ""))))
4311 (setq tbl (apply 'org-clock-get-clocktable p))
4312 (insert tbl)))
4313 (goto-char (point-min))
4314 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4315 (unless (and (pos-visible-in-window-p (point-min))
4316 (pos-visible-in-window-p (point-max)))
4317 (goto-char (1- (point-max)))
4318 (recenter -1)
4319 (if (not (pos-visible-in-window-p (or start-pos 1)))
4320 (progn
4321 (goto-char (or start-pos 1))
4322 (recenter 1))))
4323 (goto-char (or start-pos 1))
4324 (add-text-properties (point-min) (point-max)
4325 `(org-agenda-type agenda
4326 org-last-args (,arg ,start-day ,span)
4327 org-redo-cmd ,org-agenda-redo-command
4328 org-series-cmd ,org-cmd))
4329 (if (eq org-agenda-show-log-scoped 'clockcheck)
4330 (org-agenda-show-clocking-issues))
4331 (org-agenda-finalize)
4332 (setq buffer-read-only t)
4333 (message ""))))
4335 (defun org-agenda-ndays-to-span (n)
4336 "Return a span symbol for a span of N days, or N if none matches."
4337 (cond ((symbolp n) n)
4338 ((= n 1) 'day)
4339 ((= n 7) 'week)
4340 (t n)))
4342 (defun org-agenda-span-to-ndays (span &optional start-day)
4343 "Return ndays from SPAN, possibly starting at START-DAY.
4344 START-DAY is an absolute time value."
4345 (cond ((numberp span) span)
4346 ((eq span 'day) 1)
4347 ((eq span 'week) 7)
4348 ((eq span 'month)
4349 (let ((date (calendar-gregorian-from-absolute start-day)))
4350 (calendar-last-day-of-month (car date) (caddr date))))
4351 ((eq span 'year)
4352 (let ((date (calendar-gregorian-from-absolute start-day)))
4353 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4355 (defun org-agenda-span-name (span)
4356 "Return a SPAN name."
4357 (if (null span)
4359 (if (symbolp span)
4360 (capitalize (symbol-name span))
4361 (format "%d days" span))))
4363 ;;; Agenda word search
4365 (defvar org-agenda-search-history nil)
4367 (defvar org-search-syntax-table nil
4368 "Special syntax table for org-mode search.
4369 In this table, we have single quotes not as word constituents, to
4370 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4372 (defvar org-mode-syntax-table) ; From org.el
4373 (defun org-search-syntax-table ()
4374 (unless org-search-syntax-table
4375 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4376 (modify-syntax-entry ?' "." org-search-syntax-table)
4377 (modify-syntax-entry ?` "." org-search-syntax-table))
4378 org-search-syntax-table)
4380 (defvar org-agenda-last-search-view-search-was-boolean nil)
4382 ;;;###autoload
4383 (defun org-search-view (&optional todo-only string edit-at)
4384 "Show all entries that contain a phrase or words or regular expressions.
4386 With optional prefix argument TODO-ONLY, only consider entries that are
4387 TODO entries. The argument STRING can be used to pass a default search
4388 string into this function. If EDIT-AT is non-nil, it means that the
4389 user should get a chance to edit this string, with cursor at position
4390 EDIT-AT.
4392 The search string can be viewed either as a phrase that should be found as
4393 is, or it can be broken into a number of snippets, each of which must match
4394 in a Boolean way to select an entry. The default depends on the variable
4395 `org-agenda-search-view-always-boolean'.
4396 Even if this is turned off (the default) you can always switch to
4397 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4399 The default is a direct search of the whole phrase, where each space in
4400 the search string can expand to an arbitrary amount of whitespace,
4401 including newlines.
4403 If using a Boolean search, the search string is split on whitespace and
4404 each snippet is searched separately, with logical AND to select an entry.
4405 Words prefixed with a minus must *not* occur in the entry. Words without
4406 a prefix or prefixed with a plus must occur in the entry. Matching is
4407 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4408 match whole words, not parts of a word) if
4409 `org-agenda-search-view-force-full-words' is set (default is nil).
4411 Boolean search snippets enclosed by curly braces are interpreted as
4412 regular expressions that must or (when preceded with \"-\") must not
4413 match in the entry. Snippets enclosed into double quotes will be taken
4414 as a whole, to include whitespace.
4416 - If the search string starts with an asterisk, search only in headlines.
4417 - If (possibly after the leading star) the search string starts with an
4418 exclamation mark, this also means to look at TODO entries only, an effect
4419 that can also be achieved with a prefix argument.
4420 - If (possibly after star and exclamation mark) the search string starts
4421 with a colon, this will mean that the (non-regexp) snippets of the
4422 Boolean search must match as full words.
4424 This command searches the agenda files, and in addition the files listed
4425 in `org-agenda-text-search-extra-files'."
4426 (interactive "P")
4427 (if org-agenda-overriding-arguments
4428 (setq todo-only (car org-agenda-overriding-arguments)
4429 string (nth 1 org-agenda-overriding-arguments)
4430 edit-at (nth 2 org-agenda-overriding-arguments)))
4431 (let* ((props (list 'face nil
4432 'done-face 'org-agenda-done
4433 'org-not-done-regexp org-not-done-regexp
4434 'org-todo-regexp org-todo-regexp
4435 'org-complex-heading-regexp org-complex-heading-regexp
4436 'mouse-face 'highlight
4437 'help-echo (format "mouse-2 or RET jump to location")))
4438 (full-words org-agenda-search-view-force-full-words)
4439 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4440 regexp rtn rtnall files file pos inherited-tags
4441 marker category category-pos level tags c neg re boolean
4442 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4443 (unless (and (not edit-at)
4444 (stringp string)
4445 (string-match "\\S-" string))
4446 (setq string (read-string
4447 (if org-agenda-search-view-always-boolean
4448 "[+-]Word/{Regexp} ...: "
4449 "Phrase or [+-]Word/{Regexp} ...: ")
4450 (cond
4451 ((integerp edit-at) (cons string edit-at))
4452 (edit-at string))
4453 'org-agenda-search-history)))
4454 (catch 'exit
4455 (if org-agenda-sticky
4456 (setq org-agenda-buffer-name
4457 (if (stringp string)
4458 (format "*Org Agenda(%s:%s)*"
4459 (or org-keys (or (and todo-only "S") "s")) string)
4460 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4461 (org-agenda-prepare "SEARCH")
4462 (org-compile-prefix-format 'search)
4463 (org-set-sorting-strategy 'search)
4464 (setq org-agenda-redo-command
4465 (list 'org-search-view (if todo-only t nil)
4466 (list 'if 'current-prefix-arg nil string)))
4467 (setq org-agenda-query-string string)
4468 (if (equal (string-to-char string) ?*)
4469 (setq hdl-only t
4470 words (substring string 1))
4471 (setq words string))
4472 (when (equal (string-to-char words) ?!)
4473 (setq todo-only t
4474 words (substring words 1)))
4475 (when (equal (string-to-char words) ?:)
4476 (setq full-words t
4477 words (substring words 1)))
4478 (if (or org-agenda-search-view-always-boolean
4479 (member (string-to-char words) '(?- ?+ ?\{)))
4480 (setq boolean t))
4481 (setq words (org-split-string words))
4482 (let (www w)
4483 (while (setq w (pop words))
4484 (while (and (string-match "\\\\\\'" w) words)
4485 (setq w (concat (substring w 0 -1) " " (pop words))))
4486 (push w www))
4487 (setq words (nreverse www) www nil)
4488 (while (setq w (pop words))
4489 (when (and (string-match "\\`[-+]?{" w)
4490 (not (string-match "}\\'" w)))
4491 (while (and words (not (string-match "}\\'" (car words))))
4492 (setq w (concat w " " (pop words))))
4493 (setq w (concat w " " (pop words))))
4494 (push w www))
4495 (setq words (nreverse www)))
4496 (setq org-agenda-last-search-view-search-was-boolean boolean)
4497 (when boolean
4498 (let (wds w)
4499 (while (setq w (pop words))
4500 (if (or (equal (substring w 0 1) "\"")
4501 (and (> (length w) 1)
4502 (member (substring w 0 1) '("+" "-"))
4503 (equal (substring w 1 2) "\"")))
4504 (while (and words (not (equal (substring w -1) "\"")))
4505 (setq w (concat w " " (pop words)))))
4506 (and (string-match "\\`\\([-+]?\\)\"" w)
4507 (setq w (replace-match "\\1" nil nil w)))
4508 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4509 (push w wds))
4510 (setq words (nreverse wds))))
4511 (if boolean
4512 (mapc (lambda (w)
4513 (setq c (string-to-char w))
4514 (if (equal c ?-)
4515 (setq neg t w (substring w 1))
4516 (if (equal c ?+)
4517 (setq neg nil w (substring w 1))
4518 (setq neg nil)))
4519 (if (string-match "\\`{.*}\\'" w)
4520 (setq re (substring w 1 -1))
4521 (if full-words
4522 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4523 (setq re (regexp-quote (downcase w)))))
4524 (if neg (push re regexps-) (push re regexps+)))
4525 words)
4526 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4527 regexps+))
4528 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4529 (if (not regexps+)
4530 (setq regexp org-outline-regexp-bol)
4531 (setq regexp (pop regexps+))
4532 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4533 regexp))))
4534 (setq files (org-agenda-files nil 'ifmode))
4535 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4536 (pop org-agenda-text-search-extra-files)
4537 (setq files (org-add-archive-files files)))
4538 (setq files (append files org-agenda-text-search-extra-files)
4539 rtnall nil)
4540 (while (setq file (pop files))
4541 (setq ee nil)
4542 (catch 'nextfile
4543 (org-check-agenda-file file)
4544 (setq buffer (if (file-exists-p file)
4545 (org-get-agenda-file-buffer file)
4546 (error "No such file %s" file)))
4547 (if (not buffer)
4548 ;; If file does not exist, make sure an error message is sent
4549 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4550 file))))
4551 (with-current-buffer buffer
4552 (with-syntax-table (org-search-syntax-table)
4553 (unless (derived-mode-p 'org-mode)
4554 (error "Agenda file %s is not in `org-mode'" file))
4555 (let ((case-fold-search t))
4556 (save-excursion
4557 (save-restriction
4558 (if org-agenda-restrict
4559 (narrow-to-region org-agenda-restrict-begin
4560 org-agenda-restrict-end)
4561 (widen))
4562 (goto-char (point-min))
4563 (unless (or (org-at-heading-p)
4564 (outline-next-heading))
4565 (throw 'nextfile t))
4566 (goto-char (max (point-min) (1- (point))))
4567 (while (re-search-forward regexp nil t)
4568 (org-back-to-heading t)
4569 (while (and org-agenda-search-view-max-outline-level
4570 (> (org-reduced-level (org-outline-level))
4571 org-agenda-search-view-max-outline-level)
4572 (forward-line -1)
4573 (outline-back-to-heading t)))
4574 (skip-chars-forward "* ")
4575 (setq beg (point-at-bol)
4576 beg1 (point)
4577 end (progn
4578 (outline-next-heading)
4579 (while (and org-agenda-search-view-max-outline-level
4580 (> (org-reduced-level (org-outline-level))
4581 org-agenda-search-view-max-outline-level)
4582 (forward-line 1)
4583 (outline-next-heading)))
4584 (point)))
4586 (catch :skip
4587 (goto-char beg)
4588 (org-agenda-skip)
4589 (setq str (buffer-substring-no-properties
4590 (point-at-bol)
4591 (if hdl-only (point-at-eol) end)))
4592 (mapc (lambda (wr) (when (string-match wr str)
4593 (goto-char (1- end))
4594 (throw :skip t)))
4595 regexps-)
4596 (mapc (lambda (wr) (unless (string-match wr str)
4597 (goto-char (1- end))
4598 (throw :skip t)))
4599 (if todo-only
4600 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4601 regexps+)
4602 regexps+))
4603 (goto-char beg)
4604 (setq marker (org-agenda-new-marker (point))
4605 category (org-get-category)
4606 level (make-string (org-reduced-level (org-outline-level)) ? )
4607 category-pos (get-text-property (point) 'org-category-position)
4608 inherited-tags
4609 (or (eq org-agenda-show-inherited-tags 'always)
4610 (and (listp org-agenda-show-inherited-tags)
4611 (memq 'todo org-agenda-show-inherited-tags))
4612 (and (eq org-agenda-show-inherited-tags t)
4613 (or (eq org-agenda-use-tag-inheritance t)
4614 (memq 'todo org-agenda-use-tag-inheritance))))
4615 tags (org-get-tags-at nil (not inherited-tags))
4616 txt (org-agenda-format-item
4618 (buffer-substring-no-properties
4619 beg1 (point-at-eol))
4620 level category tags t))
4621 (org-add-props txt props
4622 'org-marker marker 'org-hd-marker marker
4623 'org-todo-regexp org-todo-regexp
4624 'level level
4625 'org-complex-heading-regexp org-complex-heading-regexp
4626 'priority 1000 'org-category category
4627 'org-category-position category-pos
4628 'type "search")
4629 (push txt ee)
4630 (goto-char (1- end))))))))))
4631 (setq rtn (nreverse ee))
4632 (setq rtnall (append rtnall rtn)))
4633 (if org-agenda-overriding-header
4634 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4635 nil 'face 'org-agenda-structure) "\n")
4636 (insert "Search words: ")
4637 (add-text-properties (point-min) (1- (point))
4638 (list 'face 'org-agenda-structure))
4639 (setq pos (point))
4640 (insert string "\n")
4641 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4642 (setq pos (point))
4643 (unless org-agenda-multi
4644 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4645 (add-text-properties pos (1- (point))
4646 (list 'face 'org-agenda-structure))))
4647 (org-agenda-mark-header-line (point-min))
4648 (when rtnall
4649 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4650 (goto-char (point-min))
4651 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4652 (add-text-properties (point-min) (point-max)
4653 `(org-agenda-type search
4654 org-last-args (,todo-only ,string ,edit-at)
4655 org-redo-cmd ,org-agenda-redo-command
4656 org-series-cmd ,org-cmd))
4657 (org-agenda-finalize)
4658 (setq buffer-read-only t))))
4660 ;;; Agenda TODO list
4662 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4663 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4664 (concat
4665 (if (or (equal keywords "ALL") (not keywords))
4666 (propertize "ALL" 'face 'warning)
4667 (mapconcat
4668 (lambda (kw)
4669 (propertize kw 'face (org-get-todo-face kw)))
4670 (org-split-string keywords "|")
4671 "|"))
4672 "\n"))
4674 (defvar org-select-this-todo-keyword nil)
4675 (defvar org-last-arg nil)
4677 ;;;###autoload
4678 (defun org-todo-list (&optional arg)
4679 "Show all (not done) TODO entries from all agenda file in a single list.
4680 The prefix arg can be used to select a specific TODO keyword and limit
4681 the list to these. When using \\[universal-argument], you will be prompted
4682 for a keyword. A numeric prefix directly selects the Nth keyword in
4683 `org-todo-keywords-1'."
4684 (interactive "P")
4685 (if org-agenda-overriding-arguments
4686 (setq arg org-agenda-overriding-arguments))
4687 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4688 (let* ((today (org-today))
4689 (date (calendar-gregorian-from-absolute today))
4690 (kwds org-todo-keywords-for-agenda)
4691 (completion-ignore-case t)
4692 (org-select-this-todo-keyword
4693 (if (stringp arg) arg
4694 (and arg (integerp arg) (> arg 0)
4695 (nth (1- arg) kwds))))
4696 rtn rtnall files file pos)
4697 (when (equal arg '(4))
4698 (setq org-select-this-todo-keyword
4699 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4700 (mapcar 'list kwds) nil nil)))
4701 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4702 (catch 'exit
4703 (if org-agenda-sticky
4704 (setq org-agenda-buffer-name
4705 (if (stringp org-select-this-todo-keyword)
4706 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4707 org-select-this-todo-keyword)
4708 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4709 (org-agenda-prepare "TODO")
4710 (org-compile-prefix-format 'todo)
4711 (org-set-sorting-strategy 'todo)
4712 (setq org-agenda-redo-command
4713 `(org-todo-list (or (and (numberp current-prefix-arg)
4714 current-prefix-arg)
4715 ,org-select-this-todo-keyword
4716 current-prefix-arg ,arg)))
4717 (setq files (org-agenda-files nil 'ifmode)
4718 rtnall nil)
4719 (while (setq file (pop files))
4720 (catch 'nextfile
4721 (org-check-agenda-file file)
4722 (setq rtn (org-agenda-get-day-entries file date :todo))
4723 (setq rtnall (append rtnall rtn))))
4724 (if org-agenda-overriding-header
4725 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4726 nil 'face 'org-agenda-structure) "\n")
4727 (insert "Global list of TODO items of type: ")
4728 (add-text-properties (point-min) (1- (point))
4729 (list 'face 'org-agenda-structure
4730 'short-heading
4731 (concat "ToDo: "
4732 (or org-select-this-todo-keyword "ALL"))))
4733 (org-agenda-mark-header-line (point-min))
4734 (insert (org-agenda-propertize-selected-todo-keywords
4735 org-select-this-todo-keyword))
4736 (setq pos (point))
4737 (unless org-agenda-multi
4738 (insert "Available with `N r': (0)[ALL]")
4739 (let ((n 0) s)
4740 (mapc (lambda (x)
4741 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4742 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4743 (insert "\n "))
4744 (insert " " s))
4745 kwds))
4746 (insert "\n"))
4747 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4748 (org-agenda-mark-header-line (point-min))
4749 (when rtnall
4750 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4751 (goto-char (point-min))
4752 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4753 (add-text-properties (point-min) (point-max)
4754 `(org-agenda-type todo
4755 org-last-args ,arg
4756 org-redo-cmd ,org-agenda-redo-command
4757 org-series-cmd ,org-cmd))
4758 (org-agenda-finalize)
4759 (setq buffer-read-only t))))
4761 ;;; Agenda tags match
4763 ;;;###autoload
4764 (defun org-tags-view (&optional todo-only match)
4765 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4766 The prefix arg TODO-ONLY limits the search to TODO entries."
4767 (interactive "P")
4768 (if org-agenda-overriding-arguments
4769 (setq todo-only (car org-agenda-overriding-arguments)
4770 match (nth 1 org-agenda-overriding-arguments)))
4771 (let* ((org-tags-match-list-sublevels
4772 org-tags-match-list-sublevels)
4773 (completion-ignore-case t)
4774 rtn rtnall files file pos matcher
4775 buffer)
4776 (when (and (stringp match) (not (string-match "\\S-" match)))
4777 (setq match nil))
4778 (catch 'exit
4779 (if org-agenda-sticky
4780 (setq org-agenda-buffer-name
4781 (if (stringp match)
4782 (format "*Org Agenda(%s:%s)*"
4783 (or org-keys (or (and todo-only "M") "m")) match)
4784 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4785 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4786 ;; expanding tags within `org-make-tags-matcher'
4787 (org-agenda-prepare (concat "TAGS " match))
4788 (setq matcher (org-make-tags-matcher match)
4789 match (car matcher) matcher (cdr matcher))
4790 (org-compile-prefix-format 'tags)
4791 (org-set-sorting-strategy 'tags)
4792 (setq org-agenda-query-string match)
4793 (setq org-agenda-redo-command
4794 (list 'org-tags-view `(quote ,todo-only)
4795 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4796 (setq files (org-agenda-files nil 'ifmode)
4797 rtnall nil)
4798 (while (setq file (pop files))
4799 (catch 'nextfile
4800 (org-check-agenda-file file)
4801 (setq buffer (if (file-exists-p file)
4802 (org-get-agenda-file-buffer file)
4803 (error "No such file %s" file)))
4804 (if (not buffer)
4805 ;; If file does not exist, error message to agenda
4806 (setq rtn (list
4807 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4808 rtnall (append rtnall rtn))
4809 (with-current-buffer buffer
4810 (unless (derived-mode-p 'org-mode)
4811 (error "Agenda file %s is not in `org-mode'" file))
4812 (save-excursion
4813 (save-restriction
4814 (if org-agenda-restrict
4815 (narrow-to-region org-agenda-restrict-begin
4816 org-agenda-restrict-end)
4817 (widen))
4818 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4819 (setq rtnall (append rtnall rtn))))))))
4820 (if org-agenda-overriding-header
4821 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4822 nil 'face 'org-agenda-structure) "\n")
4823 (insert "Headlines with TAGS match: ")
4824 (add-text-properties (point-min) (1- (point))
4825 (list 'face 'org-agenda-structure
4826 'short-heading
4827 (concat "Match: " match)))
4828 (setq pos (point))
4829 (insert match "\n")
4830 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4831 (setq pos (point))
4832 (unless org-agenda-multi
4833 (insert "Press `C-u r' to search again with new search string\n"))
4834 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4835 (org-agenda-mark-header-line (point-min))
4836 (when rtnall
4837 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4838 (goto-char (point-min))
4839 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4840 (add-text-properties (point-min) (point-max)
4841 `(org-agenda-type tags
4842 org-last-args (,todo-only ,match)
4843 org-redo-cmd ,org-agenda-redo-command
4844 org-series-cmd ,org-cmd))
4845 (org-agenda-finalize)
4846 (setq buffer-read-only t))))
4848 ;;; Agenda Finding stuck projects
4850 (defvar org-agenda-skip-regexp nil
4851 "Regular expression used in skipping subtrees for the agenda.
4852 This is basically a temporary global variable that can be set and then
4853 used by user-defined selections using `org-agenda-skip-function'.")
4855 (defvar org-agenda-overriding-header nil
4856 "When set during agenda, todo and tags searches it replaces the header.
4857 This variable should not be set directly, but custom commands can bind it
4858 in the options section.")
4860 (defun org-agenda-skip-entry-when-regexp-matches ()
4861 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4862 If yes, it returns the end position of this entry, causing agenda commands
4863 to skip the entry but continuing the search in the subtree. This is a
4864 function that can be put into `org-agenda-skip-function' for the duration
4865 of a command."
4866 (let ((end (save-excursion (org-end-of-subtree t)))
4867 skip)
4868 (save-excursion
4869 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4870 (and skip end)))
4872 (defun org-agenda-skip-subtree-when-regexp-matches ()
4873 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4874 If yes, it returns the end position of this tree, causing agenda commands
4875 to skip this subtree. This is a function that can be put into
4876 `org-agenda-skip-function' for the duration of a command."
4877 (let ((end (save-excursion (org-end-of-subtree t)))
4878 skip)
4879 (save-excursion
4880 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4881 (and skip end)))
4883 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4884 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4885 If yes, it returns the end position of the current entry (NOT the tree),
4886 causing agenda commands to skip the entry but continuing the search in
4887 the subtree. This is a function that can be put into
4888 `org-agenda-skip-function' for the duration of a command. An important
4889 use of this function is for the stuck project list."
4890 (let ((end (save-excursion (org-end-of-subtree t)))
4891 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4892 skip)
4893 (save-excursion
4894 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4895 (and skip entry-end)))
4897 (defun org-agenda-skip-entry-if (&rest conditions)
4898 "Skip entry if any of CONDITIONS is true.
4899 See `org-agenda-skip-if' for details."
4900 (org-agenda-skip-if nil conditions))
4902 (defun org-agenda-skip-subtree-if (&rest conditions)
4903 "Skip entry if any of CONDITIONS is true.
4904 See `org-agenda-skip-if' for details."
4905 (org-agenda-skip-if t conditions))
4907 (defun org-agenda-skip-if (subtree conditions)
4908 "Checks current entity for CONDITIONS.
4909 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4910 the entry (i.e. the text before the next heading) is checked.
4912 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4913 from different tests. Valid conditions are:
4915 scheduled Check if there is a scheduled cookie
4916 notscheduled Check if there is no scheduled cookie
4917 deadline Check if there is a deadline
4918 notdeadline Check if there is no deadline
4919 timestamp Check if there is a timestamp (also deadline or scheduled)
4920 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4921 regexp Check if regexp matches
4922 notregexp Check if regexp does not match.
4923 todo Check if TODO keyword matches
4924 nottodo Check if TODO keyword does not match
4926 The regexp is taken from the conditions list, it must come right after
4927 the `regexp' or `notregexp' element.
4929 `todo' and `nottodo' accept as an argument a list of todo
4930 keywords, which may include \"*\" to match any todo keyword.
4932 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4934 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4936 Instead of a list, a keyword class may be given. For example:
4938 (org-agenda-skip-entry-if 'nottodo 'done)
4940 would skip entries that haven't been marked with any of \"DONE\"
4941 keywords. Possible classes are: `todo', `done', `any'.
4943 If any of these conditions is met, this function returns the end point of
4944 the entity, causing the search to continue from there. This is a function
4945 that can be put into `org-agenda-skip-function' for the duration of a command."
4946 (let (beg end m)
4947 (org-back-to-heading t)
4948 (setq beg (point)
4949 end (if subtree
4950 (progn (org-end-of-subtree t) (point))
4951 (progn (outline-next-heading) (1- (point)))))
4952 (goto-char beg)
4953 (and
4955 (and (memq 'scheduled conditions)
4956 (re-search-forward org-scheduled-time-regexp end t))
4957 (and (memq 'notscheduled conditions)
4958 (not (re-search-forward org-scheduled-time-regexp end t)))
4959 (and (memq 'deadline conditions)
4960 (re-search-forward org-deadline-time-regexp end t))
4961 (and (memq 'notdeadline conditions)
4962 (not (re-search-forward org-deadline-time-regexp end t)))
4963 (and (memq 'timestamp conditions)
4964 (re-search-forward org-ts-regexp end t))
4965 (and (memq 'nottimestamp conditions)
4966 (not (re-search-forward org-ts-regexp end t)))
4967 (and (setq m (memq 'regexp conditions))
4968 (stringp (nth 1 m))
4969 (re-search-forward (nth 1 m) end t))
4970 (and (setq m (memq 'notregexp conditions))
4971 (stringp (nth 1 m))
4972 (not (re-search-forward (nth 1 m) end t)))
4973 (and (or
4974 (setq m (memq 'nottodo conditions))
4975 (setq m (memq 'todo-unblocked conditions))
4976 (setq m (memq 'nottodo-unblocked conditions))
4977 (setq m (memq 'todo conditions)))
4978 (org-agenda-skip-if-todo m end)))
4979 end)))
4981 (defun org-agenda-skip-if-todo (args end)
4982 "Helper function for `org-agenda-skip-if', do not use it directly.
4983 ARGS is a list with first element either `todo', `nottodo',
4984 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4985 a list of TODO keywords, or a state symbol `todo' or `done' or
4986 `any'."
4987 (let ((kw (car args))
4988 (arg (cadr args))
4989 todo-wds todo-re)
4990 (setq todo-wds
4991 (org-uniquify
4992 (cond
4993 ((listp arg) ;; list of keywords
4994 (if (member "*" arg)
4995 (mapcar 'substring-no-properties org-todo-keywords-1)
4996 arg))
4997 ((symbolp arg) ;; keyword class name
4998 (cond
4999 ((eq arg 'todo)
5000 (org-delete-all org-done-keywords
5001 (mapcar 'substring-no-properties
5002 org-todo-keywords-1)))
5003 ((eq arg 'done) org-done-keywords)
5004 ((eq arg 'any)
5005 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5006 (setq todo-re
5007 (concat "^\\*+[ \t]+\\<\\("
5008 (mapconcat 'identity todo-wds "\\|")
5009 "\\)\\>"))
5010 (cond
5011 ((eq kw 'todo) (re-search-forward todo-re end t))
5012 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5013 ((eq kw 'todo-unblocked)
5014 (catch 'unblocked
5015 (while (re-search-forward todo-re end t)
5016 (or (org-entry-blocked-p) (throw 'unblocked t)))
5017 nil))
5018 ((eq kw 'nottodo-unblocked)
5019 (catch 'unblocked
5020 (while (re-search-forward todo-re end t)
5021 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5025 ;;;###autoload
5026 (defun org-agenda-list-stuck-projects (&rest ignore)
5027 "Create agenda view for projects that are stuck.
5028 Stuck projects are project that have no next actions. For the definitions
5029 of what a project is and how to check if it stuck, customize the variable
5030 `org-stuck-projects'."
5031 (interactive)
5032 (let* ((org-agenda-skip-function
5033 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5034 ;; We could have used org-agenda-skip-if here.
5035 (org-agenda-overriding-header
5036 (or org-agenda-overriding-header "List of stuck projects: "))
5037 (matcher (nth 0 org-stuck-projects))
5038 (todo (nth 1 org-stuck-projects))
5039 (todo-wds (if (member "*" todo)
5040 (progn
5041 (org-agenda-prepare-buffers (org-agenda-files
5042 nil 'ifmode))
5043 (org-delete-all
5044 org-done-keywords-for-agenda
5045 (copy-sequence org-todo-keywords-for-agenda)))
5046 todo))
5047 (todo-re (concat "^\\*+[ \t]+\\("
5048 (mapconcat 'identity todo-wds "\\|")
5049 "\\)\\>"))
5050 (tags (nth 2 org-stuck-projects))
5051 (tags-re (if (member "*" tags)
5052 (concat org-outline-regexp-bol
5053 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5054 (if tags
5055 (concat org-outline-regexp-bol
5056 ".*:\\("
5057 (mapconcat 'identity tags "\\|")
5058 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5059 (gen-re (nth 3 org-stuck-projects))
5060 (re-list
5061 (delq nil
5062 (list
5063 (if todo todo-re)
5064 (if tags tags-re)
5065 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5066 gen-re)))))
5067 (setq org-agenda-skip-regexp
5068 (if re-list
5069 (mapconcat 'identity re-list "\\|")
5070 (error "No information how to identify unstuck projects")))
5071 (org-tags-view nil matcher)
5072 (with-current-buffer org-agenda-buffer-name
5073 (setq org-agenda-redo-command
5074 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5076 ;;; Diary integration
5078 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5079 (defvar diary-list-entries-hook)
5080 (defvar diary-time-regexp)
5081 (defun org-get-entries-from-diary (date)
5082 "Get the (Emacs Calendar) diary entries for DATE."
5083 (require 'diary-lib)
5084 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5085 (diary-display-hook '(fancy-diary-display))
5086 (diary-display-function 'fancy-diary-display)
5087 (pop-up-frames nil)
5088 (diary-list-entries-hook
5089 (cons 'org-diary-default-entry diary-list-entries-hook))
5090 (diary-file-name-prefix nil) ; turn this feature off
5091 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5092 entries
5093 (org-disable-agenda-to-diary t))
5094 (save-excursion
5095 (save-window-excursion
5096 (funcall (if (fboundp 'diary-list-entries)
5097 'diary-list-entries 'list-diary-entries)
5098 date 1)))
5099 (if (not (get-buffer diary-fancy-buffer))
5100 (setq entries nil)
5101 (with-current-buffer diary-fancy-buffer
5102 (setq buffer-read-only nil)
5103 (if (zerop (buffer-size))
5104 ;; No entries
5105 (setq entries nil)
5106 ;; Omit the date and other unnecessary stuff
5107 (org-agenda-cleanup-fancy-diary)
5108 ;; Add prefix to each line and extend the text properties
5109 (if (zerop (buffer-size))
5110 (setq entries nil)
5111 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5112 (setq entries
5113 (with-temp-buffer
5114 (insert entries) (goto-char (point-min))
5115 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5116 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5117 (replace-match (concat "; " (match-string 1)))))
5118 (buffer-string)))))
5119 (set-buffer-modified-p nil)
5120 (kill-buffer diary-fancy-buffer)))
5121 (when entries
5122 (setq entries (org-split-string entries "\n"))
5123 (setq entries
5124 (mapcar
5125 (lambda (x)
5126 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5127 ;; Extend the text properties to the beginning of the line
5128 (org-add-props x (text-properties-at (1- (length x)) x)
5129 'type "diary" 'date date 'face 'org-agenda-diary))
5130 entries)))))
5132 (defvar org-agenda-cleanup-fancy-diary-hook nil
5133 "Hook run when the fancy diary buffer is cleaned up.")
5135 (defun org-agenda-cleanup-fancy-diary ()
5136 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5137 This gets rid of the date, the underline under the date, and
5138 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5139 date. It also removes lines that contain only whitespace."
5140 (goto-char (point-min))
5141 (if (looking-at ".*?:[ \t]*")
5142 (progn
5143 (replace-match "")
5144 (re-search-forward "\n=+$" nil t)
5145 (replace-match "")
5146 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5147 (re-search-forward "\n=+$" nil t)
5148 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5149 (goto-char (point-min))
5150 (while (re-search-forward "^ +\n" nil t)
5151 (replace-match ""))
5152 (goto-char (point-min))
5153 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5154 (replace-match ""))
5155 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5157 ;; Make sure entries from the diary have the right text properties.
5158 (eval-after-load "diary-lib"
5159 '(if (boundp 'diary-modify-entry-list-string-function)
5160 ;; We can rely on the hook, nothing to do
5162 ;; Hook not available, must use advice to make this work
5163 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5164 "Make the position visible."
5165 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5166 (stringp string)
5167 buffer-file-name)
5168 (setq string (org-modify-diary-entry-string string))))))
5170 (defun org-modify-diary-entry-string (string)
5171 "Add text properties to string, allowing org-mode to act on it."
5172 (org-add-props string nil
5173 'mouse-face 'highlight
5174 'help-echo (if buffer-file-name
5175 (format "mouse-2 or RET jump to diary file %s"
5176 (abbreviate-file-name buffer-file-name))
5178 'org-agenda-diary-link t
5179 'org-marker (org-agenda-new-marker (point-at-bol))))
5181 (defun org-diary-default-entry ()
5182 "Add a dummy entry to the diary.
5183 Needed to avoid empty dates which mess up holiday display."
5184 ;; Catch the error if dealing with the new add-to-diary-alist
5185 (when org-disable-agenda-to-diary
5186 (condition-case nil
5187 (org-add-to-diary-list original-date "Org-mode dummy" "")
5188 (error
5189 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5191 (defun org-add-to-diary-list (&rest args)
5192 (if (fboundp 'diary-add-to-list)
5193 (apply 'diary-add-to-list args)
5194 (apply 'add-to-diary-list args)))
5196 (defvar org-diary-last-run-time nil)
5198 ;;;###autoload
5199 (defun org-diary (&rest args)
5200 "Return diary information from org files.
5201 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5202 It accesses org files and extracts information from those files to be
5203 listed in the diary. The function accepts arguments specifying what
5204 items should be listed. For a list of arguments allowed here, see the
5205 variable `org-agenda-entry-types'.
5207 The call in the diary file should look like this:
5209 &%%(org-diary) ~/path/to/some/orgfile.org
5211 Use a separate line for each org file to check. Or, if you omit the file name,
5212 all files listed in `org-agenda-files' will be checked automatically:
5214 &%%(org-diary)
5216 If you don't give any arguments (as in the example above), the default value
5217 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5218 So the example above may also be written as
5220 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5222 The function expects the lisp variables `entry' and `date' to be provided
5223 by the caller, because this is how the calendar works. Don't use this
5224 function from a program - use `org-agenda-get-day-entries' instead."
5225 (when (> (- (org-float-time)
5226 org-agenda-last-marker-time)
5228 ;; I am not sure if this works with sticky agendas, because the marker
5229 ;; list is then no longer a global variable.
5230 (org-agenda-reset-markers))
5231 (org-compile-prefix-format 'agenda)
5232 (org-set-sorting-strategy 'agenda)
5233 (setq args (or args org-agenda-entry-types))
5234 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5235 (list entry)
5236 (org-agenda-files t)))
5237 (time (org-float-time))
5238 file rtn results)
5239 (when (or (not org-diary-last-run-time)
5240 (> (- time
5241 org-diary-last-run-time)
5243 (org-agenda-prepare-buffers files))
5244 (setq org-diary-last-run-time time)
5245 ;; If this is called during org-agenda, don't return any entries to
5246 ;; the calendar. Org Agenda will list these entries itself.
5247 (if org-disable-agenda-to-diary (setq files nil))
5248 (while (setq file (pop files))
5249 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5250 (setq results (append results rtn)))
5251 (when results
5252 (setq results
5253 (mapcar (lambda (i) (replace-regexp-in-string
5254 org-bracket-link-regexp "\\3" i)) results))
5255 (concat (org-agenda-finalize-entries results) "\n"))))
5257 ;;; Agenda entry finders
5259 (defun org-agenda-get-day-entries (file date &rest args)
5260 "Does the work for `org-diary' and `org-agenda'.
5261 FILE is the path to a file to be checked for entries. DATE is date like
5262 the one returned by `calendar-current-date'. ARGS are symbols indicating
5263 which kind of entries should be extracted. For details about these, see
5264 the documentation of `org-diary'."
5265 (setq args (or args org-agenda-entry-types))
5266 (let* ((org-startup-folded nil)
5267 (org-startup-align-all-tables nil)
5268 (buffer (if (file-exists-p file)
5269 (org-get-agenda-file-buffer file)
5270 (error "No such file %s" file)))
5271 arg results rtn deadline-results)
5272 (if (not buffer)
5273 ;; If file does not exist, make sure an error message ends up in diary
5274 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5275 (with-current-buffer buffer
5276 (unless (derived-mode-p 'org-mode)
5277 (error "Agenda file %s is not in `org-mode'" file))
5278 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5279 (let ((case-fold-search nil))
5280 (save-excursion
5281 (save-restriction
5282 (if org-agenda-restrict
5283 (narrow-to-region org-agenda-restrict-begin
5284 org-agenda-restrict-end)
5285 (widen))
5286 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5287 (while (setq arg (pop args))
5288 (cond
5289 ((and (eq arg :todo)
5290 (equal date (calendar-gregorian-from-absolute
5291 (org-today))))
5292 (setq rtn (org-agenda-get-todos))
5293 (setq results (append results rtn)))
5294 ((eq arg :timestamp)
5295 (setq rtn (org-agenda-get-blocks))
5296 (setq results (append results rtn))
5297 (setq rtn (org-agenda-get-timestamps deadline-results))
5298 (setq results (append results rtn)))
5299 ((eq arg :sexp)
5300 (setq rtn (org-agenda-get-sexps))
5301 (setq results (append results rtn)))
5302 ((eq arg :scheduled)
5303 (setq rtn (org-agenda-get-scheduled deadline-results))
5304 (setq results (append results rtn)))
5305 ((eq arg :scheduled*)
5306 (setq rtn (org-agenda-get-scheduled deadline-results t))
5307 (setq results (append results rtn)))
5308 ((eq arg :closed)
5309 (setq rtn (org-agenda-get-progress))
5310 (setq results (append results rtn)))
5311 ((eq arg :deadline)
5312 (setq rtn (org-agenda-get-deadlines))
5313 (setq deadline-results (copy-sequence rtn))
5314 (setq results (append results rtn)))
5315 ((eq arg :deadline*)
5316 (setq rtn (org-agenda-get-deadlines t))
5317 (setq deadline-results (copy-sequence rtn))
5318 (setq results (append results rtn))))))))
5319 results))))
5321 (defsubst org-em (x y list)
5322 "Is X or Y a member of LIST?"
5323 (or (memq x list) (memq y list)))
5325 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5326 (defvar org-agenda-sorting-strategy-selected nil)
5328 (defun org-agenda-get-todos ()
5329 "Return the TODO information for agenda display."
5330 (let* ((props (list 'face nil
5331 'done-face 'org-agenda-done
5332 'org-not-done-regexp org-not-done-regexp
5333 'org-todo-regexp org-todo-regexp
5334 'org-complex-heading-regexp org-complex-heading-regexp
5335 'mouse-face 'highlight
5336 'help-echo
5337 (format "mouse-2 or RET jump to org file %s"
5338 (abbreviate-file-name buffer-file-name))))
5339 (regexp (format org-heading-keyword-regexp-format
5340 (cond
5341 ((and org-select-this-todo-keyword
5342 (equal org-select-this-todo-keyword "*"))
5343 org-todo-regexp)
5344 (org-select-this-todo-keyword
5345 (concat "\\("
5346 (mapconcat 'identity
5347 (org-split-string
5348 org-select-this-todo-keyword
5349 "|")
5350 "\\|") "\\)"))
5351 (t org-not-done-regexp))))
5352 marker priority category category-pos level tags todo-state ts-date ts-date-type
5353 ee txt beg end inherited-tags todo-state-end-pos)
5354 (goto-char (point-min))
5355 (while (re-search-forward regexp nil t)
5356 (catch :skip
5357 (save-match-data
5358 (beginning-of-line)
5359 (org-agenda-skip)
5360 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5361 (unless (and (setq todo-state (org-get-todo-state))
5362 (setq todo-state-end-pos (match-end 2)))
5363 (goto-char end)
5364 (throw :skip nil))
5365 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5366 (goto-char (1+ beg))
5367 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5368 (throw :skip nil)))
5369 (goto-char (match-beginning 2))
5370 (setq marker (org-agenda-new-marker (match-beginning 0))
5371 category (org-get-category)
5372 ts-date (let (ts)
5373 (save-match-data
5374 (cond ((org-em 'scheduled-up 'scheduled-down
5375 org-agenda-sorting-strategy-selected)
5376 (setq ts (org-entry-get (point) "SCHEDULED")
5377 ts-date-type " scheduled"))
5378 ((org-em 'deadline-up 'deadline-down
5379 org-agenda-sorting-strategy-selected)
5380 (setq ts (org-entry-get (point) "DEADLINE")
5381 ts-date-type " deadline"))
5382 ((org-em 'ts-up 'ts-down
5383 org-agenda-sorting-strategy-selected)
5384 (setq ts (org-entry-get (point) "TIMESTAMP")
5385 ts-date-type " timestamp"))
5386 ((org-em 'tsia-up 'tsia-down
5387 org-agenda-sorting-strategy-selected)
5388 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5389 ts-date-type " timestamp_ia"))
5390 ((org-em 'timestamp-up 'timestamp-down
5391 org-agenda-sorting-strategy-selected)
5392 (setq ts (or (org-entry-get (point) "SCHEDULED")
5393 (org-entry-get (point) "DEADLINE")
5394 (org-entry-get (point) "TIMESTAMP")
5395 (org-entry-get (point) "TIMESTAMP_IA"))
5396 ts-date-type ""))
5397 (t (setq ts-date-type "")))
5398 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5399 category-pos (get-text-property (point) 'org-category-position)
5400 txt (org-trim
5401 (buffer-substring (match-beginning 2) (match-end 0)))
5402 inherited-tags
5403 (or (eq org-agenda-show-inherited-tags 'always)
5404 (and (listp org-agenda-show-inherited-tags)
5405 (memq 'todo org-agenda-show-inherited-tags))
5406 (and (eq org-agenda-show-inherited-tags t)
5407 (or (eq org-agenda-use-tag-inheritance t)
5408 (memq 'todo org-agenda-use-tag-inheritance))))
5409 tags (org-get-tags-at nil (not inherited-tags))
5410 level (make-string (org-reduced-level (org-outline-level)) ? )
5411 txt (org-agenda-format-item "" txt level category tags t)
5412 priority (1+ (org-get-priority txt)))
5413 (org-add-props txt props
5414 'org-marker marker 'org-hd-marker marker
5415 'priority priority 'org-category category
5416 'level level
5417 'ts-date ts-date
5418 'org-category-position category-pos
5419 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5420 (push txt ee)
5421 (if org-agenda-todo-list-sublevels
5422 (goto-char todo-state-end-pos)
5423 (org-end-of-subtree 'invisible))))
5424 (nreverse ee)))
5426 (defun org-agenda-todo-custom-ignore-p (time n)
5427 "Check whether timestamp is farther away than n number of days.
5428 This function is invoked if `org-agenda-todo-ignore-deadlines',
5429 `org-agenda-todo-ignore-scheduled' or
5430 `org-agenda-todo-ignore-timestamp' is set to an integer."
5431 (let ((days (org-time-stamp-to-now
5432 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5433 (if (>= n 0)
5434 (>= days n)
5435 (<= days n))))
5437 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5438 (&optional end)
5439 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5440 (when (or org-agenda-todo-ignore-with-date
5441 org-agenda-todo-ignore-scheduled
5442 org-agenda-todo-ignore-deadlines
5443 org-agenda-todo-ignore-timestamp)
5444 (setq end (or end (save-excursion (outline-next-heading) (point))))
5445 (save-excursion
5446 (or (and org-agenda-todo-ignore-with-date
5447 (re-search-forward org-ts-regexp end t))
5448 (and org-agenda-todo-ignore-scheduled
5449 (re-search-forward org-scheduled-time-regexp end t)
5450 (cond
5451 ((eq org-agenda-todo-ignore-scheduled 'future)
5452 (> (org-time-stamp-to-now
5453 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5454 ((eq org-agenda-todo-ignore-scheduled 'past)
5455 (<= (org-time-stamp-to-now
5456 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5457 ((numberp org-agenda-todo-ignore-scheduled)
5458 (org-agenda-todo-custom-ignore-p
5459 (match-string 1) org-agenda-todo-ignore-scheduled))
5460 (t)))
5461 (and org-agenda-todo-ignore-deadlines
5462 (re-search-forward org-deadline-time-regexp end t)
5463 (cond
5464 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5465 ((eq org-agenda-todo-ignore-deadlines 'far)
5466 (not (org-deadline-close (match-string 1))))
5467 ((eq org-agenda-todo-ignore-deadlines 'future)
5468 (> (org-time-stamp-to-now
5469 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5470 ((eq org-agenda-todo-ignore-deadlines 'past)
5471 (<= (org-time-stamp-to-now
5472 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5473 ((numberp org-agenda-todo-ignore-deadlines)
5474 (org-agenda-todo-custom-ignore-p
5475 (match-string 1) org-agenda-todo-ignore-deadlines))
5476 (t (org-deadline-close (match-string 1)))))
5477 (and org-agenda-todo-ignore-timestamp
5478 (let ((buffer (current-buffer))
5479 (regexp
5480 (concat
5481 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5482 (start (point)))
5483 ;; Copy current buffer into a temporary one
5484 (with-temp-buffer
5485 (insert-buffer-substring buffer start end)
5486 (goto-char (point-min))
5487 ;; Delete SCHEDULED and DEADLINE items
5488 (while (re-search-forward regexp end t)
5489 (delete-region (match-beginning 0) (match-end 0)))
5490 (goto-char (point-min))
5491 ;; No search for timestamp left
5492 (when (re-search-forward org-ts-regexp nil t)
5493 (cond
5494 ((eq org-agenda-todo-ignore-timestamp 'future)
5495 (> (org-time-stamp-to-now
5496 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5497 ((eq org-agenda-todo-ignore-timestamp 'past)
5498 (<= (org-time-stamp-to-now
5499 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5500 ((numberp org-agenda-todo-ignore-timestamp)
5501 (org-agenda-todo-custom-ignore-p
5502 (match-string 1) org-agenda-todo-ignore-timestamp))
5503 (t))))))))))
5505 (defun org-agenda-get-timestamps (&optional deadline-results)
5506 "Return the date stamp information for agenda display."
5507 (let* ((props (list 'face 'org-agenda-calendar-event
5508 'org-not-done-regexp org-not-done-regexp
5509 'org-todo-regexp org-todo-regexp
5510 'org-complex-heading-regexp org-complex-heading-regexp
5511 'mouse-face 'highlight
5512 'help-echo
5513 (format "mouse-2 or RET jump to org file %s"
5514 (abbreviate-file-name buffer-file-name))))
5515 (d1 (calendar-absolute-from-gregorian date))
5517 (deadline-position-alist
5518 (mapcar (lambda (a) (and (setq mm (get-text-property
5519 0 'org-hd-marker a))
5520 (cons (marker-position mm) a)))
5521 deadline-results))
5522 (remove-re org-ts-regexp)
5523 (regexp
5524 (concat
5525 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5526 (regexp-quote
5527 (substring
5528 (format-time-string
5529 (car org-time-stamp-formats)
5530 (apply 'encode-time ; DATE bound by calendar
5531 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5532 1 11))
5533 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5534 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5535 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5536 donep tmp priority category category-pos level ee txt timestr tags
5537 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5538 inherited-tags ts-date)
5539 (goto-char (point-min))
5540 (while (setq end-of-match (re-search-forward regexp nil t))
5541 (setq b0 (match-beginning 0)
5542 b3 (match-beginning 3) e3 (match-end 3)
5543 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5544 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5545 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5546 (member todo-state
5547 org-agenda-repeating-timestamp-show-all)))
5548 (catch :skip
5549 (and (org-at-date-range-p) (throw :skip nil))
5550 (org-agenda-skip)
5551 (if (and (match-end 1)
5552 (not (= d1 (org-time-string-to-absolute
5553 (match-string 1) d1 nil show-all
5554 (current-buffer) b0))))
5555 (throw :skip nil))
5556 (if (and e3
5557 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5558 (throw :skip nil))
5559 (setq tmp (buffer-substring (max (point-min)
5560 (- b0 org-ds-keyword-length))
5562 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5563 inactivep (= (char-after b0) ?\[)
5564 deadlinep (string-match org-deadline-regexp tmp)
5565 scheduledp (string-match org-scheduled-regexp tmp)
5566 closedp (and org-agenda-include-inactive-timestamps
5567 (string-match org-closed-string tmp))
5568 clockp (and org-agenda-include-inactive-timestamps
5569 (or (string-match org-clock-string tmp)
5570 (string-match "]-+\\'" tmp)))
5571 warntime (get-text-property (point) 'org-appt-warntime)
5572 donep (member todo-state org-done-keywords))
5573 (if (or scheduledp deadlinep closedp clockp
5574 (and donep org-agenda-skip-timestamp-if-done))
5575 (throw :skip t))
5576 (if (string-match ">" timestr)
5577 ;; substring should only run to end of time stamp
5578 (setq timestr (substring timestr 0 (match-end 0))))
5579 (setq marker (org-agenda-new-marker b0)
5580 category (org-get-category b0)
5581 category-pos (get-text-property b0 'org-category-position))
5582 (save-excursion
5583 (if (not (re-search-backward org-outline-regexp-bol nil t))
5584 (throw :skip nil)
5585 (goto-char (match-beginning 0))
5586 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5587 (assoc (point) deadline-position-alist))
5588 (throw :skip nil))
5589 (setq hdmarker (org-agenda-new-marker)
5590 inherited-tags
5591 (or (eq org-agenda-show-inherited-tags 'always)
5592 (and (listp org-agenda-show-inherited-tags)
5593 (memq 'agenda org-agenda-show-inherited-tags))
5594 (and (eq org-agenda-show-inherited-tags t)
5595 (or (eq org-agenda-use-tag-inheritance t)
5596 (memq 'agenda org-agenda-use-tag-inheritance))))
5597 tags (org-get-tags-at nil (not inherited-tags))
5598 level (make-string (org-reduced-level (org-outline-level)) ? ))
5599 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5600 (setq head (or (match-string 1) ""))
5601 (setq txt (org-agenda-format-item
5602 (if inactivep org-agenda-inactive-leader nil)
5603 head level category tags timestr
5604 remove-re habitp)))
5605 (setq priority (org-get-priority txt))
5606 (org-add-props txt props 'priority priority
5607 'org-marker marker 'org-hd-marker hdmarker
5608 'org-category category 'date date
5609 'level level
5610 'ts-date
5611 (ignore-errors (org-time-string-to-absolute timestr))
5612 'org-category-position category-pos
5613 'todo-state todo-state
5614 'warntime warntime
5615 'type "timestamp")
5616 (push txt ee))
5617 (if org-agenda-skip-additional-timestamps-same-entry
5618 (outline-next-heading)
5619 (goto-char end-of-match))))
5620 (nreverse ee)))
5622 (defun org-agenda-get-sexps ()
5623 "Return the sexp information for agenda display."
5624 (require 'diary-lib)
5625 (let* ((props (list 'face 'org-agenda-calendar-sexp
5626 'mouse-face 'highlight
5627 'help-echo
5628 (format "mouse-2 or RET jump to org file %s"
5629 (abbreviate-file-name buffer-file-name))))
5630 (regexp "^&?%%(")
5631 marker category extra category-pos level ee txt tags entry
5632 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5633 (goto-char (point-min))
5634 (while (re-search-forward regexp nil t)
5635 (catch :skip
5636 (org-agenda-skip)
5637 (setq beg (match-beginning 0))
5638 (goto-char (1- (match-end 0)))
5639 (setq b (point))
5640 (forward-sexp 1)
5641 (setq sexp (buffer-substring b (point)))
5642 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5643 (org-trim (match-string 1))
5644 ""))
5645 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5646 (when result
5647 (setq marker (org-agenda-new-marker beg)
5648 level (make-string (org-reduced-level (org-outline-level)) ? )
5649 category (org-get-category beg)
5650 category-pos (get-text-property beg 'org-category-position)
5651 inherited-tags
5652 (or (eq org-agenda-show-inherited-tags 'always)
5653 (and (listp org-agenda-show-inherited-tags)
5654 (memq 'agenda org-agenda-show-inherited-tags))
5655 (and (eq org-agenda-show-inherited-tags t)
5656 (or (eq org-agenda-use-tag-inheritance t)
5657 (memq 'agenda org-agenda-use-tag-inheritance))))
5658 tags (org-get-tags-at nil (not inherited-tags))
5659 todo-state (org-get-todo-state)
5660 warntime (get-text-property (point) 'org-appt-warntime)
5661 extra nil)
5663 (dolist (r (if (stringp result)
5664 (list result)
5665 result)) ;; we expect a list here
5666 (when (and org-agenda-diary-sexp-prefix
5667 (string-match org-agenda-diary-sexp-prefix r))
5668 (setq extra (match-string 0 r)
5669 r (replace-match "" nil nil r)))
5670 (if (string-match "\\S-" r)
5671 (setq txt r)
5672 (setq txt "SEXP entry returned empty string"))
5673 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5674 (org-add-props txt props 'org-marker marker
5675 'org-category category 'date date 'todo-state todo-state
5676 'org-category-position category-pos 'tags tags
5677 'level level
5678 'type "sexp" 'warntime warntime)
5679 (push txt ee)))))
5680 (nreverse ee)))
5682 ;; Calendar sanity: define some functions that are independent of
5683 ;; `calendar-date-style'.
5684 ;; Normally I would like to use ISO format when calling the diary functions,
5685 ;; but to make sure we still have Emacs 22 compatibility we bind
5686 ;; also `european-calendar-style' and use european format
5687 (defun org-anniversary (year month day &optional mark)
5688 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5689 (org-no-warnings
5690 (let ((calendar-date-style 'european) (european-calendar-style t))
5691 (diary-anniversary day month year mark))))
5692 (defun org-cyclic (N year month day &optional mark)
5693 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5694 (org-no-warnings
5695 (let ((calendar-date-style 'european) (european-calendar-style t))
5696 (diary-cyclic N day month year mark))))
5697 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5698 "Like `diary-block', but with fixed (ISO) order of arguments."
5699 (org-no-warnings
5700 (let ((calendar-date-style 'european) (european-calendar-style t))
5701 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5702 (defun org-date (year month day &optional mark)
5703 "Like `diary-date', but with fixed (ISO) order of arguments."
5704 (org-no-warnings
5705 (let ((calendar-date-style 'european) (european-calendar-style t))
5706 (diary-date day month year mark))))
5708 ;; Define the` org-class' function
5709 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5710 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5711 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5712 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5713 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5714 `holidays', then any date that is known by the Emacs calendar to be a
5715 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5716 then those holidays will be skipped."
5717 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5718 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5719 (d (calendar-absolute-from-gregorian date))
5720 (h (when skip-weeks (calendar-check-holidays date))))
5721 (and
5722 (<= date1 d)
5723 (<= d date2)
5724 (= (calendar-day-of-week date) dayname)
5725 (or (not skip-weeks)
5726 (progn
5727 (require 'cal-iso)
5728 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5729 (not (or (and h (memq 'holidays skip-weeks))
5730 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5731 entry)))
5733 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5734 "Like `org-class', but honor `calendar-date-style'.
5735 The order of the first 2 times 3 arguments depends on the variable
5736 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5737 So for American calendars, give this as MONTH DAY YEAR, for European as
5738 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5739 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5740 is any number of ISO weeks in the block period for which the item should
5741 be skipped.
5743 This function is here only for backward compatibility and it is deprecated,
5744 please use `org-class' instead."
5745 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5746 (date2 (org-order-calendar-date-args m2 d2 y2)))
5747 (org-class
5748 (nth 2 date1) (car date1) (nth 1 date1)
5749 (nth 2 date2) (car date2) (nth 1 date2)
5750 dayname skip-weeks)))
5751 (make-obsolete 'org-diary-class 'org-class "")
5753 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
5754 (defalias 'org-get-closed 'org-agenda-get-progress)
5755 (defun org-agenda-get-progress ()
5756 "Return the logged TODO entries for agenda display."
5757 (let* ((props (list 'mouse-face 'highlight
5758 'org-not-done-regexp org-not-done-regexp
5759 'org-todo-regexp org-todo-regexp
5760 'org-complex-heading-regexp org-complex-heading-regexp
5761 'help-echo
5762 (format "mouse-2 or RET jump to org file %s"
5763 (abbreviate-file-name buffer-file-name))))
5764 (items (if (consp org-agenda-show-log-scoped)
5765 org-agenda-show-log-scoped
5766 (if (eq org-agenda-show-log-scoped 'clockcheck)
5767 '(clock)
5768 org-agenda-log-mode-items)))
5769 (parts
5770 (delq nil
5771 (list
5772 (if (memq 'closed items) (concat "\\<" org-closed-string))
5773 (if (memq 'clock items) (concat "\\<" org-clock-string))
5774 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5775 (parts-re (if parts (mapconcat 'identity parts "\\|")
5776 (error "`org-agenda-log-mode-items' is empty")))
5777 (regexp (concat
5778 "\\(" parts-re "\\)"
5779 " *\\["
5780 (regexp-quote
5781 (substring
5782 (format-time-string
5783 (car org-time-stamp-formats)
5784 (apply 'encode-time ; DATE bound by calendar
5785 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5786 1 11))))
5787 (org-agenda-search-headline-for-time nil)
5788 marker hdmarker priority category category-pos level tags closedp
5789 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5790 (goto-char (point-min))
5791 (while (re-search-forward regexp nil t)
5792 (catch :skip
5793 (org-agenda-skip)
5794 (setq marker (org-agenda-new-marker (match-beginning 0))
5795 closedp (equal (match-string 1) org-closed-string)
5796 statep (equal (string-to-char (match-string 1)) ?-)
5797 clockp (not (or closedp statep))
5798 state (and statep (match-string 2))
5799 category (org-get-category (match-beginning 0))
5800 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5801 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5802 (when (string-match "\\]" timestr)
5803 ;; substring should only run to end of time stamp
5804 (setq rest (substring timestr (match-end 0))
5805 timestr (substring timestr 0 (match-end 0)))
5806 (if (and (not closedp) (not statep)
5807 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5808 rest))
5809 (progn (setq timestr (concat (substring timestr 0 -1)
5810 "-" (match-string 1 rest) "]"))
5811 (setq clocked (match-string 2 rest)))
5812 (setq clocked "-")))
5813 (save-excursion
5814 (setq extra
5815 (cond
5816 ((not org-agenda-log-mode-add-notes) nil)
5817 (statep
5818 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5819 (match-string 1)))
5820 (clockp
5821 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5822 (match-string 1)))))
5823 (if (not (re-search-backward org-outline-regexp-bol nil t))
5824 (throw :skip nil)
5825 (goto-char (match-beginning 0))
5826 (setq hdmarker (org-agenda-new-marker)
5827 inherited-tags
5828 (or (eq org-agenda-show-inherited-tags 'always)
5829 (and (listp org-agenda-show-inherited-tags)
5830 (memq 'todo org-agenda-show-inherited-tags))
5831 (and (eq org-agenda-show-inherited-tags t)
5832 (or (eq org-agenda-use-tag-inheritance t)
5833 (memq 'todo org-agenda-use-tag-inheritance))))
5834 tags (org-get-tags-at nil (not inherited-tags))
5835 level (make-string (org-reduced-level (org-outline-level)) ? ))
5836 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5837 (setq txt (match-string 1))
5838 (when extra
5839 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5840 (setq txt (concat (substring txt 0 (match-beginning 1))
5841 " - " extra " " (match-string 2 txt)))
5842 (setq txt (concat txt " - " extra))))
5843 (setq txt (org-agenda-format-item
5844 (cond
5845 (closedp "Closed: ")
5846 (statep (concat "State: (" state ")"))
5847 (t (concat "Clocked: (" clocked ")")))
5848 txt level category tags timestr)))
5849 (setq priority 100000)
5850 (org-add-props txt props
5851 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5852 'priority priority 'org-category category
5853 'org-category-position category-pos
5854 'level level
5855 'type "closed" 'date date
5856 'undone-face 'org-warning 'done-face 'org-agenda-done)
5857 (push txt ee))
5858 (goto-char (point-at-eol))))
5859 (nreverse ee)))
5861 (defun org-agenda-show-clocking-issues ()
5862 "Add overlays, showing issues with clocking.
5863 See also the user option `org-agenda-clock-consistency-checks'."
5864 (interactive)
5865 (let* ((org-time-clocksum-use-effort-durations nil)
5866 (pl org-agenda-clock-consistency-checks)
5867 (re (concat "^[ \t]*"
5868 org-clock-string
5869 "[ \t]+"
5870 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5871 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5872 (tlstart 0.)
5873 (tlend 0.)
5874 (maxtime (org-hh:mm-string-to-minutes
5875 (or (plist-get pl :max-duration) "24:00")))
5876 (mintime (org-hh:mm-string-to-minutes
5877 (or (plist-get pl :min-duration) 0)))
5878 (maxgap (org-hh:mm-string-to-minutes
5879 ;; default 30:00 means never complain
5880 (or (plist-get pl :max-gap) "30:00")))
5881 (gapok (mapcar 'org-hh:mm-string-to-minutes
5882 (plist-get pl :gap-ok-around)))
5883 (def-face (or (plist-get pl :default-face)
5884 '((:background "DarkRed") (:foreground "white"))))
5885 issue face m te ts dt ov)
5886 (goto-char (point-min))
5887 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5888 (setq issue nil face def-face)
5889 (catch 'next
5890 (setq m (org-get-at-bol 'org-marker)
5891 te nil ts nil)
5892 (unless (and m (markerp m))
5893 (setq issue "No valid clock line") (throw 'next t))
5894 (org-with-point-at m
5895 (save-excursion
5896 (goto-char (point-at-bol))
5897 (unless (looking-at re)
5898 (error "No valid Clock line")
5899 (throw 'next t))
5900 (unless (match-end 3)
5901 (setq issue "No end time"
5902 face (or (plist-get pl :no-end-time-face) face))
5903 (throw 'next t))
5904 (setq ts (match-string 1)
5905 te (match-string 3)
5906 ts (org-float-time
5907 (apply 'encode-time (org-parse-time-string ts)))
5908 te (org-float-time
5909 (apply 'encode-time (org-parse-time-string te)))
5910 dt (- te ts))))
5911 (cond
5912 ((> dt (* 60 maxtime))
5913 ;; a very long clocking chunk
5914 (setq issue (format "Clocking interval is very long: %s"
5915 (org-minutes-to-clocksum-string
5916 (floor (/ (float dt) 60.))))
5917 face (or (plist-get pl :long-face) face)))
5918 ((< dt (* 60 mintime))
5919 ;; a very short clocking chunk
5920 (setq issue (format "Clocking interval is very short: %s"
5921 (org-minutes-to-clocksum-string
5922 (floor (/ (float dt) 60.))))
5923 face (or (plist-get pl :short-face) face)))
5924 ((and (> tlend 0) (< ts tlend))
5925 ;; Two clock entries are overlapping
5926 (setq issue (format "Clocking overlap: %d minutes"
5927 (/ (- tlend ts) 60))
5928 face (or (plist-get pl :overlap-face) face)))
5929 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5930 ;; There is a gap, lets see if we need to report it
5931 (unless (org-agenda-check-clock-gap tlend ts gapok)
5932 (setq issue (format "Clocking gap: %d minutes"
5933 (/ (- ts tlend) 60))
5934 face (or (plist-get pl :gap-face) face))))
5935 (t nil)))
5936 (setq tlend (or te tlend) tlstart (or ts tlstart))
5937 (when issue
5938 ;; OK, there was some issue, add an overlay to show the issue
5939 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5940 (overlay-put ov 'before-string
5941 (concat
5942 (org-add-props
5943 (format "%-43s" (concat " " issue))
5945 'face face)
5946 "\n"))
5947 (overlay-put ov 'evaporate t)))))
5949 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5950 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5951 (catch 'exit
5952 (unless ok-list
5953 ;; there are no OK times for gaps...
5954 (throw 'exit nil))
5955 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5956 ;; This is more than 24 hours, so it is OK.
5957 ;; because we have at least one OK time, that must be in the
5958 ;; 24 hour interval.
5959 (throw 'exit t))
5960 ;; We have a shorter gap.
5961 ;; Now we have to get the minute of the day when these times are
5962 (let* ((t1dec (decode-time (seconds-to-time t1)))
5963 (t2dec (decode-time (seconds-to-time t2)))
5964 ;; compute the minute on the day
5965 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5966 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5967 (when (< min2 min1)
5968 ;; if min2 is smaller than min1, this means it is on the next day.
5969 ;; Wrap it to after midnight.
5970 (setq min2 (+ min2 1440)))
5971 ;; Now check if any of the OK times is in the gap
5972 (mapc (lambda (x)
5973 ;; Wrap the time to after midnight if necessary
5974 (if (< x min1) (setq x (+ x 1440)))
5975 ;; Check if in interval
5976 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5977 ok-list)
5978 ;; Nope, this gap is not OK
5979 nil)))
5981 (defun org-agenda-get-deadlines (&optional with-hour)
5982 "Return the deadline information for agenda display.
5983 When WITH-HOUR is non-nil, only return deadlines with an hour
5984 specification like [h]h:mm."
5985 (let* ((props (list 'mouse-face 'highlight
5986 'org-not-done-regexp org-not-done-regexp
5987 'org-todo-regexp org-todo-regexp
5988 'org-complex-heading-regexp org-complex-heading-regexp
5989 'help-echo
5990 (format "mouse-2 or RET jump to org file %s"
5991 (abbreviate-file-name buffer-file-name))))
5992 (regexp (if with-hour
5993 org-deadline-time-hour-regexp
5994 org-deadline-time-regexp))
5995 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5996 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5997 (dl0 (car org-agenda-deadline-leaders))
5998 (dl1 (nth 1 org-agenda-deadline-leaders))
5999 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6000 d2 diff dfrac wdays pos pos1 category category-pos level
6001 tags suppress-prewarning ee txt head face s todo-state
6002 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6003 (goto-char (point-min))
6004 (while (re-search-forward regexp nil t)
6005 (catch :skip
6006 (org-agenda-skip)
6007 (setq s (match-string 1)
6008 txt nil
6009 pos (1- (match-beginning 1))
6010 todo-state (save-match-data (org-get-todo-state))
6011 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6012 (member todo-state
6013 org-agenda-repeating-timestamp-show-all))
6014 d2 (org-time-string-to-absolute
6015 s d1 'past show-all (current-buffer) pos)
6016 diff (- d2 d1))
6017 (setq suppress-prewarning
6018 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6019 (let ((item (buffer-substring (point-at-bol)
6020 (point-at-eol))))
6021 (save-match-data
6022 (and (string-match
6023 org-scheduled-time-regexp item)
6024 (match-string 1 item)))))))
6025 (cond
6026 ((not ds) nil)
6027 ;; The current item has a scheduled date (in ds), so
6028 ;; evaluate its prewarning lead time.
6029 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6030 ;; Use global prewarning-restart lead time.
6031 org-agenda-skip-deadline-prewarning-if-scheduled)
6032 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6033 'pre-scheduled)
6034 ;; Set prewarning to no earlier than scheduled.
6035 (min (- d2 (org-time-string-to-absolute
6036 ds d1 'past show-all (current-buffer) pos))
6037 org-deadline-warning-days))
6038 ;; Set prewarning to deadline.
6039 (t 0))))
6040 (setq wdays (if suppress-prewarning
6041 (let ((org-deadline-warning-days suppress-prewarning))
6042 (org-get-wdays s))
6043 (org-get-wdays s))
6044 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6045 upcomingp (and todayp (> diff 0)))
6046 ;; When to show a deadline in the calendar:
6047 ;; If the expiration is within wdays warning time.
6048 ;; Past-due deadlines are only shown on the current date
6049 (if (and (or (and (<= diff wdays)
6050 (and todayp (not org-agenda-only-exact-dates)))
6051 (= diff 0)))
6052 (save-excursion
6053 ;; (setq todo-state (org-get-todo-state))
6054 (setq donep (member todo-state org-done-keywords))
6055 (if (and donep
6056 (or org-agenda-skip-deadline-if-done
6057 (not (= diff 0))))
6058 (setq txt nil)
6059 (setq category (org-get-category)
6060 warntime (get-text-property (point) 'org-appt-warntime)
6061 category-pos (get-text-property (point) 'org-category-position))
6062 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6063 (throw :skip nil)
6064 (goto-char (match-end 0))
6065 (setq pos1 (match-beginning 0))
6066 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6067 (setq inherited-tags
6068 (or (eq org-agenda-show-inherited-tags 'always)
6069 (and (listp org-agenda-show-inherited-tags)
6070 (memq 'agenda org-agenda-show-inherited-tags))
6071 (and (eq org-agenda-show-inherited-tags t)
6072 (or (eq org-agenda-use-tag-inheritance t)
6073 (memq 'agenda org-agenda-use-tag-inheritance))))
6074 tags (org-get-tags-at pos1 (not inherited-tags)))
6075 (setq head (buffer-substring
6076 (point)
6077 (progn (skip-chars-forward "^\r\n")
6078 (point))))
6079 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6080 (setq timestr
6081 (concat (substring s (match-beginning 1)) " "))
6082 (setq timestr 'time))
6083 (setq txt (org-agenda-format-item
6084 (cond ((= diff 0) dl0)
6085 ((> diff 0)
6086 (if (functionp dl1)
6087 (funcall dl1 diff date)
6088 (format dl1 diff)))
6090 (if (functionp dl2)
6091 (funcall dl2 diff date)
6092 (format dl2 (if (string= dl2 dl1)
6093 diff (abs diff))))))
6094 head level category tags
6095 (if (not (= diff 0)) nil timestr)))))
6096 (when txt
6097 (setq face (org-agenda-deadline-face dfrac))
6098 (org-add-props txt props
6099 'org-marker (org-agenda-new-marker pos)
6100 'warntime warntime
6101 'level level
6102 'ts-date d2
6103 'org-hd-marker (org-agenda-new-marker pos1)
6104 'priority (+ (- diff)
6105 (org-get-priority txt))
6106 'org-category category
6107 'org-category-position category-pos
6108 'todo-state todo-state
6109 'type (if upcomingp "upcoming-deadline" "deadline")
6110 'date (if upcomingp date d2)
6111 'face (if donep 'org-agenda-done face)
6112 'undone-face face 'done-face 'org-agenda-done)
6113 (push txt ee))))))
6114 (nreverse ee)))
6116 (defun org-agenda-deadline-face (fraction)
6117 "Return the face to displaying a deadline item.
6118 FRACTION is what fraction of the head-warning time has passed."
6119 (let ((faces org-agenda-deadline-faces) f)
6120 (catch 'exit
6121 (while (setq f (pop faces))
6122 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6124 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6125 "Return the scheduled information for agenda display.
6126 When WITH-HOUR is non-nil, only return scheduled items with
6127 an hour specification like [h]h:mm."
6128 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6129 'org-todo-regexp org-todo-regexp
6130 'org-complex-heading-regexp org-complex-heading-regexp
6131 'done-face 'org-agenda-done
6132 'mouse-face 'highlight
6133 'help-echo
6134 (format "mouse-2 or RET jump to org file %s"
6135 (abbreviate-file-name buffer-file-name))))
6136 (regexp (if with-hour
6137 org-scheduled-time-hour-regexp
6138 org-scheduled-time-regexp))
6139 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6140 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6142 (deadline-position-alist
6143 (mapcar (lambda (a) (and (setq mm (get-text-property
6144 0 'org-hd-marker a))
6145 (cons (marker-position mm) a)))
6146 deadline-results))
6147 d2 diff pos pos1 category category-pos level tags donep
6148 ee txt head pastschedp todo-state face timestr s habitp show-all
6149 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6150 ddays)
6151 (goto-char (point-min))
6152 (while (re-search-forward regexp nil t)
6153 (catch :skip
6154 (org-agenda-skip)
6155 (setq s (match-string 1)
6156 txt nil
6157 pos (1- (match-beginning 1))
6158 todo-state (save-match-data (org-get-todo-state))
6159 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6160 (member todo-state
6161 org-agenda-repeating-timestamp-show-all))
6162 d2 (org-time-string-to-absolute
6163 s d1 'past show-all (current-buffer) pos)
6164 diff (- d2 d1)
6165 warntime (get-text-property (point) 'org-appt-warntime))
6166 (setq pastschedp (and todayp (< diff 0)))
6167 (setq did-habit-check-p nil)
6168 (setq suppress-delay
6169 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6170 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6171 (save-match-data
6172 (and (string-match
6173 org-deadline-time-regexp item)
6174 (match-string 1 item)))))))
6175 (cond
6176 ((not ds) nil)
6177 ;; The current item has a deadline date (in ds), so
6178 ;; evaluate its delay time.
6179 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6180 ;; Use global delay time.
6181 (- org-agenda-skip-scheduled-delay-if-deadline))
6182 ((eq org-agenda-skip-scheduled-delay-if-deadline
6183 'post-deadline)
6184 ;; Set delay to no later than deadline.
6185 (min (- d2 (org-time-string-to-absolute
6186 ds d1 'past show-all (current-buffer) pos))
6187 org-scheduled-delay-days))
6188 (t 0))))
6189 (setq ddays (if suppress-delay
6190 (let ((org-scheduled-delay-days suppress-delay))
6191 (org-get-wdays s t t))
6192 (org-get-wdays s t)))
6193 ;; Use a delay of 0 when there is a repeater and the delay is
6194 ;; of the form --3d
6195 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6196 (< (org-time-string-to-absolute s)
6197 (org-time-string-to-absolute
6198 s d2 'past nil (current-buffer) pos)))
6199 (setq ddays 0))
6200 ;; When to show a scheduled item in the calendar:
6201 ;; If it is on or past the date.
6202 (when (or (and (> ddays 0) (= diff (- ddays)))
6203 (and (zerop ddays) (= diff 0))
6204 (and (< (+ diff ddays) 0)
6205 (< (abs diff) org-scheduled-past-days)
6206 (and todayp (not org-agenda-only-exact-dates)))
6207 ;; org-is-habit-p uses org-entry-get, which is expansive
6208 ;; so we go extra mile to only call it once
6209 (and todayp
6210 (boundp 'org-habit-show-all-today)
6211 org-habit-show-all-today
6212 (setq did-habit-check-p t)
6213 (setq habitp (and (functionp 'org-is-habit-p)
6214 (org-is-habit-p)))))
6215 (save-excursion
6216 (setq donep (member todo-state org-done-keywords))
6217 (if (and donep
6218 (or org-agenda-skip-scheduled-if-done
6219 (not (= diff 0))
6220 (and (functionp 'org-is-habit-p)
6221 (org-is-habit-p))))
6222 (setq txt nil)
6223 (setq habitp (if did-habit-check-p habitp
6224 (and (functionp 'org-is-habit-p)
6225 (org-is-habit-p))))
6226 (setq category (org-get-category)
6227 category-pos (get-text-property (point) 'org-category-position))
6228 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6229 'repeated-after-deadline)
6230 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6231 (throw :skip nil))
6232 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6233 (throw :skip nil)
6234 (goto-char (match-end 0))
6235 (setq pos1 (match-beginning 0))
6236 (if habitp
6237 (if (or (not org-habit-show-habits)
6238 (and (not todayp)
6239 (boundp 'org-habit-show-habits-only-for-today)
6240 org-habit-show-habits-only-for-today))
6241 (throw :skip nil))
6242 (if (and
6243 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6244 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6245 pastschedp))
6246 (setq mm (assoc pos1 deadline-position-alist)))
6247 (throw :skip nil)))
6248 (setq inherited-tags
6249 (or (eq org-agenda-show-inherited-tags 'always)
6250 (and (listp org-agenda-show-inherited-tags)
6251 (memq 'agenda org-agenda-show-inherited-tags))
6252 (and (eq org-agenda-show-inherited-tags t)
6253 (or (eq org-agenda-use-tag-inheritance t)
6254 (memq 'agenda org-agenda-use-tag-inheritance))))
6256 tags (org-get-tags-at nil (not inherited-tags)))
6257 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6258 (setq head (buffer-substring
6259 (point)
6260 (progn (skip-chars-forward "^\r\n") (point))))
6261 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6262 (setq timestr
6263 (concat (substring s (match-beginning 1)) " "))
6264 (setq timestr 'time))
6265 (setq txt (org-agenda-format-item
6266 (if (= diff 0)
6267 (car org-agenda-scheduled-leaders)
6268 (format (nth 1 org-agenda-scheduled-leaders)
6269 (- 1 diff)))
6270 head level category tags
6271 (if (not (= diff 0)) nil timestr)
6272 nil habitp))))
6273 (when txt
6274 (setq face
6275 (cond
6276 ((and (not habitp) pastschedp)
6277 'org-scheduled-previously)
6278 (todayp 'org-scheduled-today)
6279 (t 'org-scheduled))
6280 habitp (and habitp (org-habit-parse-todo)))
6281 (org-add-props txt props
6282 'undone-face face
6283 'face (if donep 'org-agenda-done face)
6284 'org-marker (org-agenda-new-marker pos)
6285 'org-hd-marker (org-agenda-new-marker pos1)
6286 'type (if pastschedp "past-scheduled" "scheduled")
6287 'date (if pastschedp d2 date)
6288 'ts-date d2
6289 'warntime warntime
6290 'level level
6291 'priority (if habitp
6292 (org-habit-get-priority habitp)
6293 (+ 94 (- 5 diff) (org-get-priority txt)))
6294 'org-category category
6295 'category-position category-pos
6296 'org-habit-p habitp
6297 'todo-state todo-state)
6298 (push txt ee))))))
6299 (nreverse ee)))
6301 (defun org-agenda-get-blocks ()
6302 "Return the date-range information for agenda display."
6303 (let* ((props (list 'face nil
6304 'org-not-done-regexp org-not-done-regexp
6305 'org-todo-regexp org-todo-regexp
6306 'org-complex-heading-regexp org-complex-heading-regexp
6307 'mouse-face 'highlight
6308 'help-echo
6309 (format "mouse-2 or RET jump to org file %s"
6310 (abbreviate-file-name buffer-file-name))))
6311 (regexp org-tr-regexp)
6312 (d0 (calendar-absolute-from-gregorian date))
6313 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6314 level todo-state tags pos head donep inherited-tags)
6315 (goto-char (point-min))
6316 (while (re-search-forward regexp nil t)
6317 (catch :skip
6318 (org-agenda-skip)
6319 (setq pos (point))
6320 (let ((start-time (match-string 1))
6321 (end-time (match-string 2)))
6322 (setq s1 (match-string 1)
6323 s2 (match-string 2)
6324 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6325 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6326 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6327 ;; Only allow days between the limits, because the normal
6328 ;; date stamps will catch the limits.
6329 (save-excursion
6330 (setq todo-state (org-get-todo-state))
6331 (setq donep (member todo-state org-done-keywords))
6332 (if (and donep org-agenda-skip-timestamp-if-done)
6333 (throw :skip t))
6334 (setq marker (org-agenda-new-marker (point)))
6335 (setq category (org-get-category)
6336 category-pos (get-text-property (point) 'org-category-position))
6337 (if (not (re-search-backward org-outline-regexp-bol nil t))
6338 (throw :skip nil)
6339 (goto-char (match-beginning 0))
6340 (setq hdmarker (org-agenda-new-marker (point))
6341 inherited-tags
6342 (or (eq org-agenda-show-inherited-tags 'always)
6343 (and (listp org-agenda-show-inherited-tags)
6344 (memq 'agenda org-agenda-show-inherited-tags))
6345 (and (eq org-agenda-show-inherited-tags t)
6346 (or (eq org-agenda-use-tag-inheritance t)
6347 (memq 'agenda org-agenda-use-tag-inheritance))))
6349 tags (org-get-tags-at nil (not inherited-tags)))
6350 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6351 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6352 (setq head (match-string 1))
6353 (let ((remove-re
6354 (if org-agenda-remove-timeranges-from-blocks
6355 (concat
6356 "<" (regexp-quote s1) ".*?>"
6357 "--"
6358 "<" (regexp-quote s2) ".*?>")
6359 nil)))
6360 (setq txt (org-agenda-format-item
6361 (format
6362 (nth (if (= d1 d2) 0 1)
6363 org-agenda-timerange-leaders)
6364 (1+ (- d0 d1)) (1+ (- d2 d1)))
6365 head level category tags
6366 (cond ((and (= d1 d0) (= d2 d0))
6367 (concat "<" start-time ">--<" end-time ">"))
6368 ((= d1 d0)
6369 (concat "<" start-time ">"))
6370 ((= d2 d0)
6371 (concat "<" end-time ">")))
6372 remove-re))))
6373 (org-add-props txt props
6374 'org-marker marker 'org-hd-marker hdmarker
6375 'type "block" 'date date
6376 'level level
6377 'todo-state todo-state
6378 'priority (org-get-priority txt) 'org-category category
6379 'org-category-position category-pos)
6380 (push txt ee))))
6381 (goto-char pos)))
6382 ;; Sort the entries by expiration date.
6383 (nreverse ee)))
6385 ;;; Agenda presentation and sorting
6387 (defvar org-prefix-has-time nil
6388 "A flag, set by `org-compile-prefix-format'.
6389 The flag is set if the currently compiled format contains a `%t'.")
6390 (defvar org-prefix-has-tag nil
6391 "A flag, set by `org-compile-prefix-format'.
6392 The flag is set if the currently compiled format contains a `%T'.")
6393 (defvar org-prefix-has-effort nil
6394 "A flag, set by `org-compile-prefix-format'.
6395 The flag is set if the currently compiled format contains a `%e'.")
6396 (defvar org-prefix-has-breadcrumbs nil
6397 "A flag, set by `org-compile-prefix-format'.
6398 The flag is set if the currently compiled format contains a `%b'.")
6399 (defvar org-prefix-category-length nil
6400 "Used by `org-compile-prefix-format' to remember the category field width.")
6401 (defvar org-prefix-category-max-length nil
6402 "Used by `org-compile-prefix-format' to remember the category field width.")
6404 (defun org-agenda-get-category-icon (category)
6405 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6406 (dolist (entry org-agenda-category-icon-alist)
6407 (when (org-string-match-p (car entry) category)
6408 (if (listp (cadr entry))
6409 (return (cadr entry))
6410 (return (apply 'create-image (cdr entry)))))))
6412 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6413 remove-re habitp)
6414 "Format TXT to be inserted into the agenda buffer.
6415 In particular, add the prefix and corresponding text properties.
6417 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6418 LEVEL may be a string to replace the `%l' specifier.
6419 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6420 category taken from local variable or file name. It will replace the `%c'
6421 specifier in the format.
6422 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6423 TXT for sorting of this entry, and for the `%t' specifier in the format.
6424 When DOTIME is a string, this string is searched for a time before TXT is.
6425 TAGS can be the tags of the headline.
6426 Any match of REMOVE-RE will be removed from TXT."
6427 ;; We keep the org-prefix-* variable values along with a compiled
6428 ;; formatter, so that multiple agendas existing at the same time do
6429 ;; not step on each other toes.
6431 ;; It was inconvenient to make these variables buffer local in
6432 ;; Agenda buffers, because this function expects to be called with
6433 ;; the buffer where item comes from being current, and not agenda
6434 ;; buffer
6435 (let* ((bindings (car org-prefix-format-compiled))
6436 (formatter (cadr org-prefix-format-compiled)))
6437 (loop for (var value) in bindings
6438 do (set var value))
6439 (save-match-data
6440 ;; Diary entries sometimes have extra whitespace at the beginning
6441 (setq txt (org-trim txt))
6443 ;; Fix the tags part in txt
6444 (setq txt (org-agenda-fix-displayed-tags
6445 txt tags
6446 org-agenda-show-inherited-tags
6447 org-agenda-hide-tags-regexp))
6449 (let* ((category (or category
6450 (if (stringp org-category)
6451 org-category
6452 (and org-category (symbol-name org-category)))
6453 (if buffer-file-name
6454 (file-name-sans-extension
6455 (file-name-nondirectory buffer-file-name))
6456 "")))
6457 (category-icon (org-agenda-get-category-icon category))
6458 (category-icon (if category-icon
6459 (propertize " " 'display category-icon)
6460 ""))
6461 ;; time, tag, effort are needed for the eval of the prefix format
6462 (tag (if tags (nth (1- (length tags)) tags) ""))
6463 time effort neffort
6464 (ts (if dotime (concat
6465 (if (stringp dotime) dotime "")
6466 (and org-agenda-search-headline-for-time txt))))
6467 (time-of-day (and dotime (org-get-time-of-day ts)))
6468 stamp plain s0 s1 s2 rtn srp l
6469 duration thecategory breadcrumbs)
6470 (and (derived-mode-p 'org-mode) buffer-file-name
6471 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6472 (when (and dotime time-of-day)
6473 ;; Extract starting and ending time and move them to prefix
6474 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6475 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6476 (setq s0 (match-string 0 ts)
6477 srp (and stamp (match-end 3))
6478 s1 (match-string (if plain 1 2) ts)
6479 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6481 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6482 ;; them, we might want to remove them there to avoid duplication.
6483 ;; The user can turn this off with a variable.
6484 (if (and org-prefix-has-time
6485 org-agenda-remove-times-when-in-prefix (or stamp plain)
6486 (string-match (concat (regexp-quote s0) " *") txt)
6487 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6488 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6489 (= (match-beginning 0) 0)
6491 (setq txt (replace-match "" nil nil txt))))
6492 ;; Normalize the time(s) to 24 hour
6493 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6494 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6496 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6497 (let (org-time-clocksum-use-effort-durations)
6498 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6499 (setq s2
6500 (org-minutes-to-clocksum-string
6501 (+ (org-hh:mm-string-to-minutes s1)
6502 org-agenda-default-appointment-duration)))))
6504 ;; Compute the duration
6505 (when s2
6506 (setq duration (- (org-hh:mm-string-to-minutes s2)
6507 (org-hh:mm-string-to-minutes s1)))))
6509 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6510 txt)
6511 ;; Tags are in the string
6512 (if (or (eq org-agenda-remove-tags t)
6513 (and org-agenda-remove-tags
6514 org-prefix-has-tag))
6515 (setq txt (replace-match "" t t txt))
6516 (setq txt (replace-match
6517 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6518 (match-string 2 txt))
6519 t t txt))))
6520 (when (derived-mode-p 'org-mode)
6521 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6523 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6524 ;; current buffer, so move this check outside of above
6525 (if effort
6526 (setq neffort (org-duration-string-to-minutes effort)
6527 effort (setq effort (concat "[" effort "]")))
6528 ;; prevent erroring out with %e format when there is no effort
6529 (setq effort ""))
6531 (when remove-re
6532 (while (string-match remove-re txt)
6533 (setq txt (replace-match "" t t txt))))
6535 ;; Set org-heading property on `txt' to mark the start of the
6536 ;; heading.
6537 (add-text-properties 0 (length txt) '(org-heading t) txt)
6539 ;; Prepare the variables needed in the eval of the compiled format
6540 (if org-prefix-has-breadcrumbs
6541 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6542 (let ((s (org-display-outline-path nil nil "->" t)))
6543 (if (eq "" s) "" (concat s "->"))))))
6544 (setq time (cond (s2 (concat
6545 (org-agenda-time-of-day-to-ampm-maybe s1)
6546 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6547 (if org-agenda-timegrid-use-ampm " ")))
6548 (s1 (concat
6549 (org-agenda-time-of-day-to-ampm-maybe s1)
6550 (if org-agenda-timegrid-use-ampm
6551 "........ "
6552 "......")))
6553 (t ""))
6554 extra (or (and (not habitp) extra) "")
6555 category (if (symbolp category) (symbol-name category) category)
6556 thecategory (copy-sequence category)
6557 level (or level ""))
6558 (if (string-match org-bracket-link-regexp category)
6559 (progn
6560 (setq l (if (match-end 3)
6561 (- (match-end 3) (match-beginning 3))
6562 (- (match-end 1) (match-beginning 1))))
6563 (when (< l (or org-prefix-category-length 0))
6564 (setq category (copy-sequence category))
6565 (org-add-props category nil
6566 'extra-space (make-string
6567 (- org-prefix-category-length l 1) ?\ ))))
6568 (if (and org-prefix-category-max-length
6569 (>= (length category) org-prefix-category-max-length))
6570 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6571 ;; Evaluate the compiled format
6572 (setq rtn (concat (eval formatter) txt))
6574 ;; And finally add the text properties
6575 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6576 (org-add-props rtn nil
6577 'org-category (if thecategory (downcase thecategory) category)
6578 'tags (mapcar 'org-downcase-keep-props tags)
6579 'org-highest-priority org-highest-priority
6580 'org-lowest-priority org-lowest-priority
6581 'time-of-day time-of-day
6582 'duration duration
6583 'effort effort
6584 'effort-minutes neffort
6585 'breadcrumbs breadcrumbs
6586 'txt txt
6587 'level level
6588 'time time
6589 'extra extra
6590 'format org-prefix-format-compiled
6591 'dotime dotime)))))
6593 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6594 "Remove tags string from TXT, and add a modified list of tags.
6595 The modified list may contain inherited tags, and tags matched by
6596 `org-agenda-hide-tags-regexp' will be removed."
6597 (when (or add-inherited hide-re)
6598 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6599 (setq txt (substring txt 0 (match-beginning 0))))
6600 (setq tags
6601 (delq nil
6602 (mapcar (lambda (tg)
6603 (if (or (and hide-re (string-match hide-re tg))
6604 (and (not add-inherited)
6605 (get-text-property 0 'inherited tg)))
6607 tg))
6608 tags)))
6609 (when tags
6610 (let ((have-i (get-text-property 0 'inherited (car tags)))
6612 (setq txt (concat txt " :"
6613 (mapconcat
6614 (lambda (x)
6615 (setq i (get-text-property 0 'inherited x))
6616 (if (and have-i (not i))
6617 (progn
6618 (setq have-i nil)
6619 (concat ":" x))
6621 tags ":")
6622 (if have-i "::" ":"))))))
6623 txt)
6625 (defun org-downcase-keep-props (s)
6626 (let ((props (text-properties-at 0 s)))
6627 (setq s (downcase s))
6628 (add-text-properties 0 (length s) props s)
6631 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6633 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6634 "Add a time-grid for agenda items which need it.
6636 LIST is the list of agenda items formatted by `org-agenda-list'.
6637 NDAYS is the span of the current agenda view.
6638 TODAYP is `t' when the current agenda view is on today."
6639 (catch 'exit
6640 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6641 ((and todayp (member 'today (car org-agenda-time-grid))))
6642 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6643 ((member 'weekly (car org-agenda-time-grid)))
6644 (t (throw 'exit list)))
6645 (let* ((have (delq nil (mapcar
6646 (lambda (x) (get-text-property 1 'time-of-day x))
6647 list)))
6648 (string (nth 1 org-agenda-time-grid))
6649 (gridtimes (nth 2 org-agenda-time-grid))
6650 (req (car org-agenda-time-grid))
6651 (remove (member 'remove-match req))
6652 new time)
6653 (if (and (member 'require-timed req) (not have))
6654 ;; don't show empty grid
6655 (throw 'exit list))
6656 (while (setq time (pop gridtimes))
6657 (unless (and remove (member time have))
6658 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6659 (push (org-agenda-format-item
6660 nil string nil "" nil
6661 (concat (substring time 0 -2) ":" (substring time -2)))
6662 new)
6663 (put-text-property
6664 2 (length (car new)) 'face 'org-time-grid (car new))))
6665 (when (and todayp org-agenda-show-current-time-in-grid)
6666 (push (org-agenda-format-item
6667 nil org-agenda-current-time-string nil "" nil
6668 (format-time-string "%H:%M "))
6669 new)
6670 (put-text-property
6671 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6673 (if (member 'time-up org-agenda-sorting-strategy-selected)
6674 (append new list)
6675 (append list new)))))
6677 (defun org-compile-prefix-format (key)
6678 "Compile the prefix format into a Lisp form that can be evaluated.
6679 The resulting form and associated variable bindings is returned
6680 and stored in the variable `org-prefix-format-compiled'."
6681 (setq org-prefix-has-time nil
6682 org-prefix-has-tag nil
6683 org-prefix-category-length nil
6684 org-prefix-has-effort nil
6685 org-prefix-has-breadcrumbs nil)
6686 (let ((s (cond
6687 ((stringp org-agenda-prefix-format)
6688 org-agenda-prefix-format)
6689 ((assq key org-agenda-prefix-format)
6690 (cdr (assq key org-agenda-prefix-format)))
6691 (t " %-12:c%?-12t% s")))
6692 (start 0)
6693 varform vars var e c f opt)
6694 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6695 s start)
6696 (setq var (or (cdr (assoc (match-string 4 s)
6697 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6698 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6699 'eval)
6700 c (or (match-string 3 s) "")
6701 opt (match-beginning 1)
6702 start (1+ (match-beginning 0)))
6703 (if (equal var 'time) (setq org-prefix-has-time t))
6704 (if (equal var 'tag) (setq org-prefix-has-tag t))
6705 (if (equal var 'effort) (setq org-prefix-has-effort t))
6706 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6707 (setq f (concat "%" (match-string 2 s) "s"))
6708 (when (equal var 'category)
6709 (setq org-prefix-category-length
6710 (floor (abs (string-to-number (match-string 2 s)))))
6711 (setq org-prefix-category-max-length
6712 (let ((x (match-string 2 s)))
6713 (save-match-data
6714 (if (string-match "\\.[0-9]+" x)
6715 (string-to-number (substring (match-string 0 x) 1)))))))
6716 (if (eq var 'eval)
6717 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6718 (if opt
6719 (setq varform
6720 `(if (equal "" ,var)
6722 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6723 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6724 (setq s (replace-match "%s" t nil s))
6725 (push varform vars))
6726 (setq vars (nreverse vars))
6727 (with-current-buffer (or org-agenda-buffer (current-buffer))
6728 (setq org-prefix-format-compiled
6729 (list
6730 `((org-prefix-has-time ,org-prefix-has-time)
6731 (org-prefix-has-tag ,org-prefix-has-tag)
6732 (org-prefix-category-length ,org-prefix-category-length)
6733 (org-prefix-has-effort ,org-prefix-has-effort)
6734 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6735 `(format ,s ,@vars))))))
6737 (defun org-set-sorting-strategy (key)
6738 (if (symbolp (car org-agenda-sorting-strategy))
6739 ;; the old format
6740 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6741 (setq org-agenda-sorting-strategy-selected
6742 (or (cdr (assq key org-agenda-sorting-strategy))
6743 (cdr (assq 'agenda org-agenda-sorting-strategy))
6744 '(time-up category-keep priority-down)))))
6746 (defun org-get-time-of-day (s &optional string mod24)
6747 "Check string S for a time of day.
6748 If found, return it as a military time number between 0 and 2400.
6749 If not found, return nil.
6750 The optional STRING argument forces conversion into a 5 character wide string
6751 HH:MM."
6752 (save-match-data
6753 (when
6754 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6755 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6756 (let* ((h (string-to-number (match-string 1 s)))
6757 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6758 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6759 (am-p (equal ampm "am"))
6760 (h1 (cond ((not ampm) h)
6761 ((= h 12) (if am-p 0 12))
6762 (t (+ h (if am-p 0 12)))))
6763 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6764 (mod h1 24) h1))
6765 (t0 (+ (* 100 h2) m))
6766 (t1 (concat (if (>= h1 24) "+" " ")
6767 (if (and org-agenda-time-leading-zero
6768 (< t0 1000)) "0" "")
6769 (if (< t0 100) "0" "")
6770 (if (< t0 10) "0" "")
6771 (int-to-string t0))))
6772 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6774 (defvar org-agenda-before-sorting-filter-function nil
6775 "Function to be applied to agenda items prior to sorting.
6776 Prior to sorting also means just before they are inserted into the agenda.
6778 To aid sorting, you may revisit the original entries and add more text
6779 properties which will later be used by the sorting functions.
6781 The function should take a string argument, an agenda line.
6782 It has access to the text properties in that line, which contain among
6783 other things, the property `org-hd-marker' that points to the entry
6784 where the line comes from. Note that not all lines going into the agenda
6785 have this property, only most.
6787 The function should return the modified string. It is probably best
6788 to ONLY change text properties.
6790 You can also use this function as a filter, by returning nil for lines
6791 you don't want to have in the agenda at all. For this application, you
6792 could bind the variable in the options section of a custom command.")
6794 (defun org-agenda-finalize-entries (list &optional type)
6795 "Sort, limit and concatenate the LIST of agenda items.
6796 The optional argument TYPE tells the agenda type."
6797 (let ((max-effort (cond ((listp org-agenda-max-effort)
6798 (cdr (assoc type org-agenda-max-effort)))
6799 (t org-agenda-max-effort)))
6800 (max-todo (cond ((listp org-agenda-max-todos)
6801 (cdr (assoc type org-agenda-max-todos)))
6802 (t org-agenda-max-todos)))
6803 (max-tags (cond ((listp org-agenda-max-tags)
6804 (cdr (assoc type org-agenda-max-tags)))
6805 (t org-agenda-max-tags)))
6806 (max-entries (cond ((listp org-agenda-max-entries)
6807 (cdr (assoc type org-agenda-max-entries)))
6808 (t org-agenda-max-entries))) l)
6809 (when org-agenda-before-sorting-filter-function
6810 (setq list
6811 (delq nil
6812 (mapcar
6813 org-agenda-before-sorting-filter-function list))))
6814 (setq list (mapcar 'org-agenda-highlight-todo list)
6815 list (mapcar 'identity (sort list 'org-entries-lessp)))
6816 (when max-effort
6817 (setq list (org-agenda-limit-entries
6818 list 'effort-minutes max-effort 'identity)))
6819 (when max-todo
6820 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6821 (when max-tags
6822 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6823 (when max-entries
6824 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6825 (mapconcat 'identity list "\n")))
6827 (defun org-agenda-limit-entries (list prop limit &optional fn)
6828 "Limit the number of agenda entries."
6829 (let ((include (and limit (< limit 0))))
6830 (if limit
6831 (let ((fun (or fn (lambda (p) (if p 1))))
6832 (lim 0))
6833 (delq nil
6834 (mapcar
6835 (lambda (e)
6836 (let ((pval (funcall fun (get-text-property 1 prop e))))
6837 (if pval (setq lim (+ lim pval)))
6838 (cond ((and pval (<= lim (abs limit))) e)
6839 ((and include (not pval)) e))))
6840 list)))
6841 list)))
6843 (defun org-agenda-limit-interactively ()
6844 "In agenda, interactively limit entries to various maximums."
6845 (interactive)
6846 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6847 (num (string-to-number (read-from-minibuffer "How many? "))))
6848 (cond ((equal max ?e)
6849 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6850 ((equal max ?t)
6851 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6852 ((equal max ?T)
6853 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6854 ((equal max ?E)
6855 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6856 (org-agenda-fit-window-to-buffer))
6858 (defun org-agenda-highlight-todo (x)
6859 (let ((org-done-keywords org-done-keywords-for-agenda)
6860 (case-fold-search nil)
6862 (if (eq x 'line)
6863 (save-excursion
6864 (beginning-of-line 1)
6865 (setq re (org-get-at-bol 'org-todo-regexp))
6866 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6867 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6868 (add-text-properties (match-beginning 0) (match-end 1)
6869 (list 'face (org-get-todo-face 1)))
6870 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6871 (delete-region (match-beginning 1) (1- (match-end 0)))
6872 (goto-char (match-beginning 1))
6873 (insert (format org-agenda-todo-keyword-format s)))))
6874 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6875 (setq re (get-text-property 0 'org-todo-regexp x))
6876 (when (and re
6877 ;; Test `pl' because if there's no heading content,
6878 ;; there's no point matching to highlight. Note
6879 ;; that if we didn't test `pl' first, and there
6880 ;; happened to be no keyword from `org-todo-regexp'
6881 ;; on this heading line, then the `equal' comparison
6882 ;; afterwards would spuriously succeed in the case
6883 ;; where `pl' is nil -- causing an args-out-of-range
6884 ;; error when we try to add text properties to text
6885 ;; that isn't there.
6887 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6888 x pl) pl))
6889 (add-text-properties
6890 (or (match-end 1) (match-end 0)) (match-end 0)
6891 (list 'face (org-get-todo-face (match-string 2 x)))
6893 (when (match-end 1)
6894 (setq x (concat (substring x 0 (match-end 1))
6895 (format org-agenda-todo-keyword-format
6896 (match-string 2 x))
6897 (org-add-props " " (text-properties-at 0 x))
6898 (substring x (match-end 3)))))))
6899 x)))
6901 (defsubst org-cmp-priority (a b)
6902 "Compare the priorities of string A and B."
6903 (let ((pa (or (get-text-property 1 'priority a) 0))
6904 (pb (or (get-text-property 1 'priority b) 0)))
6905 (cond ((> pa pb) +1)
6906 ((< pa pb) -1))))
6908 (defsubst org-cmp-effort (a b)
6909 "Compare the effort values of string A and B."
6910 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6911 (ea (or (get-text-property 1 'effort-minutes a) def))
6912 (eb (or (get-text-property 1 'effort-minutes b) def)))
6913 (cond ((> ea eb) +1)
6914 ((< ea eb) -1))))
6916 (defsubst org-cmp-category (a b)
6917 "Compare the string values of categories of strings A and B."
6918 (let ((ca (or (get-text-property 1 'org-category a) ""))
6919 (cb (or (get-text-property 1 'org-category b) "")))
6920 (cond ((string-lessp ca cb) -1)
6921 ((string-lessp cb ca) +1))))
6923 (defsubst org-cmp-todo-state (a b)
6924 "Compare the todo states of strings A and B."
6925 (let* ((ma (or (get-text-property 1 'org-marker a)
6926 (get-text-property 1 'org-hd-marker a)))
6927 (mb (or (get-text-property 1 'org-marker b)
6928 (get-text-property 1 'org-hd-marker b)))
6929 (fa (and ma (marker-buffer ma)))
6930 (fb (and mb (marker-buffer mb)))
6931 (todo-kwds
6932 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6933 (and fb (with-current-buffer fb org-todo-keywords-1))))
6934 (ta (or (get-text-property 1 'todo-state a) ""))
6935 (tb (or (get-text-property 1 'todo-state b) ""))
6936 (la (- (length (member ta todo-kwds))))
6937 (lb (- (length (member tb todo-kwds))))
6938 (donepa (member ta org-done-keywords-for-agenda))
6939 (donepb (member tb org-done-keywords-for-agenda)))
6940 (cond ((and donepa (not donepb)) -1)
6941 ((and (not donepa) donepb) +1)
6942 ((< la lb) -1)
6943 ((< lb la) +1))))
6945 (defsubst org-cmp-alpha (a b)
6946 "Compare the headlines, alphabetically."
6947 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6948 (plb (text-property-any 0 (length b) 'org-heading t b))
6949 (ta (and pla (substring a pla)))
6950 (tb (and plb (substring b plb))))
6951 (when pla
6952 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6953 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6954 (setq ta (substring ta (match-end 0))))
6955 (setq ta (downcase ta)))
6956 (when plb
6957 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6958 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6959 (setq tb (substring tb (match-end 0))))
6960 (setq tb (downcase tb)))
6961 (cond ((not ta) +1)
6962 ((not tb) -1)
6963 ((string-lessp ta tb) -1)
6964 ((string-lessp tb ta) +1))))
6966 (defsubst org-cmp-tag (a b)
6967 "Compare the string values of the first tags of A and B."
6968 (let ((ta (car (last (get-text-property 1 'tags a))))
6969 (tb (car (last (get-text-property 1 'tags b)))))
6970 (cond ((not ta) +1)
6971 ((not tb) -1)
6972 ((string-lessp ta tb) -1)
6973 ((string-lessp tb ta) +1))))
6975 (defsubst org-cmp-time (a b)
6976 "Compare the time-of-day values of strings A and B."
6977 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6978 (ta (or (get-text-property 1 'time-of-day a) def))
6979 (tb (or (get-text-property 1 'time-of-day b) def)))
6980 (cond ((< ta tb) -1)
6981 ((< tb ta) +1))))
6983 (defsubst org-cmp-ts (a b &optional type)
6984 "Compare the timestamps values of entries A and B.
6985 When TYPE is \"scheduled\", \"deadline\", \"timestamp\"
6986 or \"timestamp_ia\", compare within each of these type.
6987 When TYPE is the empty string, compare all timestamps
6988 without respect of their type."
6989 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6990 (ta (or (and (string-match type (get-text-property 1 'type a))
6991 (get-text-property 1 'ts-date a)) def))
6992 (tb (or (and (string-match type (get-text-property 1 'type b))
6993 (get-text-property 1 'ts-date b)) def)))
6994 (cond ((< ta tb) -1)
6995 ((< tb ta) +1))))
6997 (defsubst org-cmp-habit-p (a b)
6998 "Compare the todo states of strings A and B."
6999 (let ((ha (get-text-property 1 'org-habit-p a))
7000 (hb (get-text-property 1 'org-habit-p b)))
7001 (cond ((and ha (not hb)) -1)
7002 ((and (not ha) hb) +1))))
7004 (defun org-entries-lessp (a b)
7005 "Predicate for sorting agenda entries."
7006 ;; The following variables will be used when the form is evaluated.
7007 ;; So even though the compiler complains, keep them.
7008 (let* ((ss org-agenda-sorting-strategy-selected)
7009 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7010 (org-cmp-ts a b "")))
7011 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7012 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7013 (org-cmp-ts a b "scheduled")))
7014 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7015 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7016 (org-cmp-ts a b "deadline")))
7017 (deadline-down (if deadline-up (- deadline-up) nil))
7018 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7019 (org-cmp-ts a b "iatimestamp_ia")))
7020 (tsia-down (if tsia-up (- tsia-up) nil))
7021 (ts-up (and (org-em 'ts-up 'ts-down ss)
7022 (org-cmp-ts a b "timestamp")))
7023 (ts-down (if ts-up (- ts-up) nil))
7024 (time-up (and (org-em 'time-up 'time-down ss)
7025 (org-cmp-time a b)))
7026 (time-down (if time-up (- time-up) nil))
7027 (priority-up (and (org-em 'priority-up 'priority-down ss)
7028 (org-cmp-priority a b)))
7029 (priority-down (if priority-up (- priority-up) nil))
7030 (effort-up (and (org-em 'effort-up 'effort-down ss)
7031 (org-cmp-effort a b)))
7032 (effort-down (if effort-up (- effort-up) nil))
7033 (category-up (and (or (org-em 'category-up 'category-down ss)
7034 (memq 'category-keep ss))
7035 (org-cmp-category a b)))
7036 (category-down (if category-up (- category-up) nil))
7037 (category-keep (if category-up +1 nil))
7038 (tag-up (and (org-em 'tag-up 'tag-down ss)
7039 (org-cmp-tag a b)))
7040 (tag-down (if tag-up (- tag-up) nil))
7041 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7042 (org-cmp-todo-state a b)))
7043 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7044 (habit-up (and (org-em 'habit-up 'habit-down ss)
7045 (org-cmp-habit-p a b)))
7046 (habit-down (if habit-up (- habit-up) nil))
7047 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7048 (org-cmp-alpha a b)))
7049 (alpha-down (if alpha-up (- alpha-up) nil))
7050 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7051 user-defined-up user-defined-down)
7052 (if (and need-user-cmp org-agenda-cmp-user-defined
7053 (functionp org-agenda-cmp-user-defined))
7054 (setq user-defined-up
7055 (funcall org-agenda-cmp-user-defined a b)
7056 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7057 (cdr (assoc
7058 (eval (cons 'or org-agenda-sorting-strategy-selected))
7059 '((-1 . t) (1 . nil) (nil . nil))))))
7061 ;;; Agenda restriction lock
7063 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7064 "Overlay to mark the headline to which agenda commands are restricted.")
7065 (overlay-put org-agenda-restriction-lock-overlay
7066 'face 'org-agenda-restriction-lock)
7067 (overlay-put org-agenda-restriction-lock-overlay
7068 'help-echo "Agendas are currently limited to this subtree.")
7069 (org-detach-overlay org-agenda-restriction-lock-overlay)
7071 (defun org-agenda-set-restriction-lock (&optional type)
7072 "Set restriction lock for agenda, to current subtree or file.
7073 Restriction will be the file if TYPE is `file', or if type is the
7074 universal prefix '(4), or if the cursor is before the first headline
7075 in the file. Otherwise, restriction will be to the current subtree."
7076 (interactive "P")
7077 (and (equal type '(4)) (setq type 'file))
7078 (setq type (cond
7079 (type type)
7080 ((org-at-heading-p) 'subtree)
7081 ((condition-case nil (org-back-to-heading t) (error nil))
7082 'subtree)
7083 (t 'file)))
7084 (if (eq type 'subtree)
7085 (progn
7086 (setq org-agenda-restrict t)
7087 (setq org-agenda-overriding-restriction 'subtree)
7088 (put 'org-agenda-files 'org-restrict
7089 (list (buffer-file-name (buffer-base-buffer))))
7090 (org-back-to-heading t)
7091 (move-overlay org-agenda-restriction-lock-overlay
7092 (point)
7093 (if org-agenda-restriction-lock-highlight-subtree
7094 (save-excursion (org-end-of-subtree t t) (point))
7095 (point-at-eol)))
7096 (move-marker org-agenda-restrict-begin (point))
7097 (move-marker org-agenda-restrict-end
7098 (save-excursion (org-end-of-subtree t t)))
7099 (message "Locking agenda restriction to subtree"))
7100 (put 'org-agenda-files 'org-restrict
7101 (list (buffer-file-name (buffer-base-buffer))))
7102 (setq org-agenda-restrict nil)
7103 (setq org-agenda-overriding-restriction 'file)
7104 (move-marker org-agenda-restrict-begin nil)
7105 (move-marker org-agenda-restrict-end nil)
7106 (message "Locking agenda restriction to file"))
7107 (setq current-prefix-arg nil)
7108 (org-agenda-maybe-redo))
7110 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7111 "Remove the agenda restriction lock."
7112 (interactive "P")
7113 (org-detach-overlay org-agenda-restriction-lock-overlay)
7114 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7115 (setq org-agenda-overriding-restriction nil)
7116 (setq org-agenda-restrict nil)
7117 (put 'org-agenda-files 'org-restrict nil)
7118 (move-marker org-agenda-restrict-begin nil)
7119 (move-marker org-agenda-restrict-end nil)
7120 (setq current-prefix-arg nil)
7121 (message "Agenda restriction lock removed")
7122 (or noupdate (org-agenda-maybe-redo)))
7124 (defun org-agenda-maybe-redo ()
7125 "If there is any window showing the agenda view, update it."
7126 (let ((w (get-buffer-window org-agenda-buffer-name t))
7127 (w0 (selected-window)))
7128 (when w
7129 (select-window w)
7130 (org-agenda-redo)
7131 (select-window w0)
7132 (if org-agenda-overriding-restriction
7133 (message "Agenda view shifted to new %s restriction"
7134 org-agenda-overriding-restriction)
7135 (message "Agenda restriction lock removed")))))
7137 ;;; Agenda commands
7139 (defun org-agenda-check-type (error &rest types)
7140 "Check if agenda buffer is of allowed type.
7141 If ERROR is non-nil, throw an error, otherwise just return nil.
7142 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7143 (if (not org-agenda-type)
7144 (error "No Org agenda currently displayed")
7145 (if (memq org-agenda-type types)
7147 (if error
7148 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7149 nil))))
7151 (defun org-agenda-Quit ()
7152 "Exit the agenda and kill buffers loaded by `org-agenda'.
7153 Also restore the window configuration."
7154 (interactive)
7155 (if org-agenda-columns-active
7156 (org-columns-quit)
7157 (let ((buf (current-buffer)))
7158 (if (eq org-agenda-window-setup 'other-frame)
7159 (progn
7160 (org-agenda-reset-markers)
7161 (kill-buffer buf)
7162 (org-columns-remove-overlays)
7163 (setq org-agenda-archives-mode nil)
7164 (delete-frame))
7165 (and (not (eq org-agenda-window-setup 'current-window))
7166 (not (one-window-p))
7167 (delete-window))
7168 (org-agenda-reset-markers)
7169 (kill-buffer buf)
7170 (org-columns-remove-overlays)
7171 (setq org-agenda-archives-mode nil)))
7172 (setq org-agenda-buffer nil)
7173 ;; Maybe restore the pre-agenda window configuration.
7174 (and org-agenda-restore-windows-after-quit
7175 (not (eq org-agenda-window-setup 'other-frame))
7176 org-agenda-pre-window-conf
7177 (set-window-configuration org-agenda-pre-window-conf)
7178 (setq org-agenda-pre-window-conf nil))))
7180 (defun org-agenda-quit ()
7181 "Exit the agenda and restore the window configuration.
7182 When `org-agenda-sticky' is non-nil, only bury the agenda."
7183 (interactive)
7184 (if (and (eq org-indirect-buffer-display 'other-window)
7185 org-last-indirect-buffer)
7186 (let ((org-last-indirect-window
7187 (get-buffer-window org-last-indirect-buffer)))
7188 (if org-last-indirect-window
7189 (delete-window org-last-indirect-window))))
7190 (if org-agenda-columns-active
7191 (org-columns-quit)
7192 (if org-agenda-sticky
7193 (let ((buf (current-buffer)))
7194 (if (eq org-agenda-window-setup 'other-frame)
7195 (progn
7196 (delete-frame))
7197 (and (not (eq org-agenda-window-setup 'current-window))
7198 (not (one-window-p))
7199 (delete-window)))
7200 (with-current-buffer buf
7201 (bury-buffer)
7202 ;; Maybe restore the pre-agenda window configuration.
7203 (and org-agenda-restore-windows-after-quit
7204 (not (eq org-agenda-window-setup 'other-frame))
7205 org-agenda-pre-window-conf
7206 (set-window-configuration org-agenda-pre-window-conf)
7207 (setq org-agenda-pre-window-conf nil))))
7208 (org-agenda-Quit))))
7210 (defun org-agenda-exit ()
7211 "Exit the agenda and restore the window configuration.
7212 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7213 buffers visited directly by the user will not be touched."
7214 (interactive)
7215 (org-release-buffers org-agenda-new-buffers)
7216 (setq org-agenda-new-buffers nil)
7217 (org-agenda-Quit))
7219 (defun org-agenda-kill-all-agenda-buffers ()
7220 "Kill all buffers in `org-agenda-mode'.
7221 This is used when toggling sticky agendas.
7222 You can also explicitly invoke it with `C-c a C-k'."
7223 (interactive)
7224 (let (blist)
7225 (dolist (buf (buffer-list))
7226 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7227 (push buf blist)))
7228 (mapc 'kill-buffer blist)))
7230 (defun org-agenda-execute (arg)
7231 "Execute another agenda command, keeping same window.
7232 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7233 in the agenda."
7234 (interactive "P")
7235 (let ((org-agenda-window-setup 'current-window))
7236 (org-agenda arg)))
7238 (defun org-agenda-redo (&optional all)
7239 "Rebuild possibly ALL agenda view(s) in the current buffer."
7240 (interactive "P")
7241 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7242 (cpa (unless (eq all t) current-prefix-arg))
7243 (org-agenda-doing-sticky-redo org-agenda-sticky)
7244 (org-agenda-sticky nil)
7245 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7246 org-agenda-buffer-name))
7247 (org-agenda-keep-modes t)
7248 (tag-filter org-agenda-tag-filter)
7249 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7250 (top-hl-filter org-agenda-top-headline-filter)
7251 (cat-filter org-agenda-category-filter)
7252 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7253 (re-filter org-agenda-regexp-filter)
7254 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7255 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7256 (cols org-agenda-columns-active)
7257 (line (org-current-line))
7258 (window-line (- line (org-current-line (window-start))))
7259 (lprops (get 'org-agenda-redo-command 'org-lprops))
7260 (redo-cmd (get-text-property p 'org-redo-cmd))
7261 (last-args (get-text-property p 'org-last-args))
7262 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7263 (org-agenda-overriding-cmd-arguments
7264 (unless (eq all t)
7265 (cond ((listp last-args)
7266 (cons (or cpa (car last-args)) (cdr last-args)))
7267 ((stringp last-args)
7268 last-args))))
7269 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7270 (put 'org-agenda-tag-filter :preset-filter nil)
7271 (put 'org-agenda-category-filter :preset-filter nil)
7272 (put 'org-agenda-regexp-filter :preset-filter nil)
7273 (and cols (org-columns-quit))
7274 (message "Rebuilding agenda buffer...")
7275 (if series-redo-cmd
7276 (eval series-redo-cmd)
7277 (org-let lprops redo-cmd))
7278 (setq org-agenda-undo-list nil
7279 org-agenda-pending-undo-list nil
7280 org-agenda-tag-filter tag-filter
7281 org-agenda-category-filter cat-filter
7282 org-agenda-regexp-filter re-filter
7283 org-agenda-top-headline-filter top-hl-filter)
7284 (message "Rebuilding agenda buffer...done")
7285 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7286 (put 'org-agenda-category-filter :preset-filter cat-preset)
7287 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7288 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7289 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7290 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7291 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7292 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7293 (org-goto-line line)
7294 (recenter window-line)))
7296 (defvar org-global-tags-completion-table nil)
7297 (defvar org-agenda-filter-form nil)
7298 (defvar org-agenda-filtered-by-category nil)
7300 (defun org-agenda-filter-by-category (strip)
7301 "Keep only those lines in the agenda buffer that have a specific category.
7302 The category is that of the current line."
7303 (interactive "P")
7304 (if (and org-agenda-filtered-by-category
7305 org-agenda-category-filter)
7306 (org-agenda-filter-show-all-cat)
7307 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7308 (if (and cat (not (string= "" cat)))
7309 (org-agenda-filter-apply
7310 (setq org-agenda-category-filter
7311 (list (concat (if strip "-" "+") cat)))
7312 'category)
7313 (error "No category at point")))))
7315 (defun org-find-top-headline (&optional pos)
7316 "Find the topmost parent headline and return it."
7317 (save-excursion
7318 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7319 (if pos (goto-char pos))
7320 ;; Skip up to the topmost parent
7321 (while (ignore-errors (outline-up-heading 1) t))
7322 (ignore-errors
7323 (nth 4 (org-heading-components))))))
7325 (defvar org-agenda-filtered-by-top-headline nil)
7326 (defun org-agenda-filter-by-top-headline (strip)
7327 "Keep only those lines that are descendants from the same top headline.
7328 The top headline is that of the current line."
7329 (interactive "P")
7330 (if org-agenda-filtered-by-top-headline
7331 (progn
7332 (setq org-agenda-filtered-by-top-headline nil
7333 org-agenda-top-headline-filter nil)
7334 (org-agenda-filter-show-all-cat))
7335 (let ((cat (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7336 (if cat (org-agenda-filter-top-headline-apply cat strip)
7337 (error "No top-level category at point")))))
7339 (defvar org-agenda-regexp-filter nil)
7340 (defun org-agenda-filter-by-regexp (strip)
7341 "Filter agenda entries by a regular expression.
7342 Regexp filters are cumulative.
7343 With no prefix argument, keep entries matching the regexp.
7344 With one prefix argument, filter out entries matching the regexp.
7345 With two prefix arguments, remove the regexp filters."
7346 (interactive "P")
7347 (if (not (equal strip '(16)))
7348 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7349 (read-from-minibuffer
7350 (if (equal strip '(4))
7351 "Filter out entries matching regexp: "
7352 "Narrow to entries matching regexp: ")))))
7353 (push flt org-agenda-regexp-filter)
7354 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7355 (org-agenda-filter-show-all-re)
7356 (message "Regexp filter removed")))
7358 (defun org-agenda-filter-remove-all ()
7359 "Remove all filters from the current agenda buffer."
7360 (interactive)
7361 (when org-agenda-tag-filter
7362 (org-agenda-filter-show-all-tag))
7363 (when org-agenda-category-filter
7364 (org-agenda-filter-show-all-cat))
7365 (when org-agenda-regexp-filter
7366 (org-agenda-filter-show-all-re)))
7368 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7369 "Keep only those lines in the agenda buffer that have a specific tag.
7370 The tag is selected with its fast selection letter, as configured.
7371 With prefix argument STRIP, remove all lines that do have the tag.
7372 A lisp caller can specify CHAR. NARROW means that the new tag should be
7373 used to narrow the search - the interactive user can also press `-' or `+'
7374 to switch to narrowing."
7375 (interactive "P")
7376 (let* ((alist org-tag-alist-for-agenda)
7377 (tag-chars (mapconcat
7378 (lambda (x) (if (and (not (symbolp (car x)))
7379 (cdr x))
7380 (char-to-string (cdr x))
7381 ""))
7382 alist ""))
7383 (efforts (org-split-string
7384 (or (cdr (assoc (concat org-effort-property "_ALL")
7385 org-global-properties))
7386 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7387 "")))
7388 (effort-op org-agenda-filter-effort-default-operator)
7389 (effort-prompt "")
7390 (inhibit-read-only t)
7391 (current org-agenda-tag-filter)
7392 maybe-refresh a n tag)
7393 (unless char
7394 (message
7395 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7396 (if narrow "Narrow" "Filter") tag-chars
7397 (if org-agenda-auto-exclude-function "[RET], " ""))
7398 (setq char (read-char-exclusive)))
7399 (when (member char '(?+ ?-))
7400 ;; Narrowing down
7401 (cond ((equal char ?-) (setq strip t narrow t))
7402 ((equal char ?+) (setq strip nil narrow t)))
7403 (message
7404 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7405 (setq char (read-char-exclusive)))
7406 (when (member char '(?< ?> ?= ??))
7407 ;; An effort operator
7408 (setq effort-op (char-to-string char))
7409 (setq alist nil) ; to make sure it will be interpreted as effort.
7410 (unless (equal char ??)
7411 (loop for i from 0 to 9 do
7412 (setq effort-prompt
7413 (concat
7414 effort-prompt " ["
7415 (if (= i 9) "0" (int-to-string (1+ i)))
7416 "]" (nth i efforts))))
7417 (message "Effort%s: %s " effort-op effort-prompt)
7418 (setq char (read-char-exclusive))
7419 (when (or (< char ?0) (> char ?9))
7420 (error "Need 1-9,0 to select effort"))))
7421 (when (equal char ?\t)
7422 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7423 (org-set-local 'org-global-tags-completion-table
7424 (org-global-tags-completion-table)))
7425 (let ((completion-ignore-case t))
7426 (setq tag (org-icompleting-read
7427 "Tag: " org-global-tags-completion-table))))
7428 (cond
7429 ((equal char ?\r)
7430 (org-agenda-filter-show-all-tag)
7431 (when org-agenda-auto-exclude-function
7432 (setq org-agenda-tag-filter nil)
7433 (dolist (tag (org-agenda-get-represented-tags))
7434 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7435 (if modifier
7436 (push modifier org-agenda-tag-filter))))
7437 (if (not (null org-agenda-tag-filter))
7438 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7439 (setq maybe-refresh t))
7440 ((equal char ?/)
7441 (org-agenda-filter-show-all-tag)
7442 (when (get 'org-agenda-tag-filter :preset-filter)
7443 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7444 (setq maybe-refresh t))
7445 ((equal char ?. )
7446 (setq org-agenda-tag-filter
7447 (mapcar (lambda(tag) (concat "+" tag))
7448 (org-get-at-bol 'tags)))
7449 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7450 (setq maybe-refresh t))
7451 ((or (equal char ?\ )
7452 (setq a (rassoc char alist))
7453 (and (>= char ?0) (<= char ?9)
7454 (setq n (if (= char ?0) 9 (- char ?0 1))
7455 tag (concat effort-op (nth n efforts))
7456 a (cons tag nil)))
7457 (and (= char ??)
7458 (setq tag "?eff")
7459 a (cons tag nil))
7460 (and tag (setq a (cons tag nil))))
7461 (org-agenda-filter-show-all-tag)
7462 (setq tag (car a))
7463 (setq org-agenda-tag-filter
7464 (cons (concat (if strip "-" "+") tag)
7465 (if narrow current nil)))
7466 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7467 (setq maybe-refresh t))
7468 (t (error "Invalid tag selection character %c" char)))
7469 (when (and maybe-refresh
7470 (eq org-agenda-clockreport-mode 'with-filter))
7471 (org-agenda-redo))))
7473 (defun org-agenda-get-represented-tags ()
7474 "Get a list of all tags currently represented in the agenda."
7475 (let (p tags)
7476 (save-excursion
7477 (goto-char (point-min))
7478 (while (setq p (next-single-property-change (point) 'tags))
7479 (goto-char p)
7480 (mapc (lambda (x) (add-to-list 'tags x))
7481 (get-text-property (point) 'tags))))
7482 tags))
7484 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7485 "Refine the current filter. See `org-agenda-filter-by-tag'."
7486 (interactive "P")
7487 (org-agenda-filter-by-tag strip char 'refine))
7489 (defun org-agenda-filter-make-matcher (filter type)
7490 "Create the form that tests a line for agenda filter."
7491 (let (f f1)
7492 (cond
7493 ;; Tag filter
7494 ((eq type 'tag)
7495 (setq filter
7496 (delete-dups
7497 (append (get 'org-agenda-tag-filter :preset-filter)
7498 filter)))
7499 (dolist (x filter)
7500 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7501 (ffunc
7502 (lambda (nf0 nf01 fltr notgroup op)
7503 (dolist (x fltr)
7504 (if (member x '("-" "+"))
7505 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7506 (if (string-match "[<=>?]" x)
7507 (setq nf01 (org-agenda-filter-effort-form x))
7508 (setq nf01 (list 'member (downcase (substring x 1))
7509 'tags)))
7510 (when (equal (string-to-char x) ?-)
7511 (setq nf01 (list 'not nf01))
7512 (when (not notgroup) (setq op 'and))))
7513 (push nf01 nf0))
7514 (if notgroup
7515 (push (cons 'and nf0) f)
7516 (push (cons (or op 'or) nf0) f)))))
7517 (if (equal nfilter filter)
7518 (funcall ffunc f1 f filter t nil)
7519 (funcall ffunc nf1 nf nfilter nil nil)))))
7520 ;; Category filter
7521 ((eq type 'category)
7522 (setq filter
7523 (delete-dups
7524 (append (get 'org-agenda-category-filter :preset-filter)
7525 filter)))
7526 (dolist (x filter)
7527 (if (equal "-" (substring x 0 1))
7528 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7529 (setq f1 (list 'equal (substring x 1) 'cat)))
7530 (push f1 f)))
7531 ;; Regexp filter
7532 ((eq type 'regexp)
7533 (setq filter
7534 (delete-dups
7535 (append (get 'org-agenda-regexp-filter :preset-filter)
7536 filter)))
7537 (dolist (x filter)
7538 (if (equal "-" (substring x 0 1))
7539 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7540 (setq f1 (list 'string-match (substring x 1) 'txt)))
7541 (push f1 f))))
7542 (cons 'and (nreverse f))))
7544 (defun org-agenda-filter-effort-form (e)
7545 "Return the form to compare the effort of the current line with what E says.
7546 E looks like \"+<2:25\"."
7547 (let (op)
7548 (setq e (substring e 1))
7549 (setq op (string-to-char e) e (substring e 1))
7550 (setq op (cond ((equal op ?<) '<=)
7551 ((equal op ?>) '>=)
7552 ((equal op ??) op)
7553 (t '=)))
7554 (list 'org-agenda-compare-effort (list 'quote op)
7555 (org-duration-string-to-minutes e))))
7557 (defun org-agenda-compare-effort (op value)
7558 "Compare the effort of the current line with VALUE, using OP.
7559 If the line does not have an effort defined, return nil."
7560 (let ((eff (org-get-at-bol 'effort-minutes)))
7561 (if (equal op ??)
7562 (not eff)
7563 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7564 value))))
7566 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7567 "Expand group tags in FILTER for the agenda.
7568 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7569 (if org-group-tags
7570 (let ((case-fold-search t) rtn)
7571 (mapc
7572 (lambda (f)
7573 (let (f0 dir)
7574 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7575 (setq dir (match-string 1 f) f0 (match-string 2 f))
7576 (setq dir (if no-operator "" "+") f0 f))
7577 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7578 (org-tags-expand f0 t t))
7579 rtn))))
7580 filter)
7581 (reverse rtn))
7582 filter))
7584 (defun org-agenda-filter-apply (filter type)
7585 "Set FILTER as the new agenda filter and apply it."
7586 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7587 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7588 (let (tags cat txt)
7589 (setq org-agenda-filter-form
7590 (org-agenda-filter-make-matcher filter type))
7591 (if (and (eq type 'category)
7592 (not (equal (substring (car filter) 0 1) "-")))
7593 ;; Only set `org-agenda-filtered-by-category' to t
7594 ;; when a unique category is used as the filter
7595 (setq org-agenda-filtered-by-category t))
7596 (org-agenda-set-mode-name)
7597 (save-excursion
7598 (goto-char (point-min))
7599 (while (not (eobp))
7600 (if (org-get-at-bol 'org-marker)
7601 (progn
7602 (setq tags ; used in eval
7603 (apply 'append
7604 (mapcar (lambda (f)
7605 (org-agenda-filter-expand-tags (list f) t))
7606 (org-get-at-bol 'tags)))
7607 cat (get-text-property (point) 'org-category)
7608 txt (get-text-property (point) 'txt))
7609 (if (not (eval org-agenda-filter-form))
7610 (org-agenda-filter-hide-line type))
7611 (beginning-of-line 2))
7612 (beginning-of-line 2))))
7613 (if (get-char-property (point) 'invisible)
7614 (ignore-errors (org-agenda-previous-line)))))
7616 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7617 "Filter by top headline HL."
7618 (org-agenda-set-mode-name)
7619 (save-excursion
7620 (goto-char (point-min))
7621 (while (not (eobp))
7622 (let* ((pos (org-get-at-bol 'org-hd-marker))
7623 (tophl (and pos (org-find-top-headline pos))))
7624 (if (and tophl (funcall (if negative 'identity 'not)
7625 (string= hl tophl)))
7626 (org-agenda-filter-hide-line 'category)))
7627 (beginning-of-line 2)))
7628 (if (get-char-property (point) 'invisible)
7629 (org-agenda-previous-line))
7630 (setq org-agenda-top-headline-filter hl
7631 org-agenda-filtered-by-top-headline t))
7633 (defun org-agenda-filter-hide-line (type)
7634 "Hide lines with TYPE in the agenda buffer."
7635 (let (ov)
7636 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
7637 (point-at-eol)))
7638 (overlay-put ov 'invisible t)
7639 (overlay-put ov 'type type)
7640 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7641 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7642 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7644 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7645 (setq pos (or pos (point)))
7646 (save-excursion
7647 (dolist (ov (overlays-at pos))
7648 (when (and (overlay-get ov 'invisible)
7649 (eq (overlay-get ov 'type) 'tag))
7650 (goto-char pos)
7651 (if (< (overlay-start ov) (point-at-eol))
7652 (move-overlay ov (point-at-eol)
7653 (overlay-end ov)))))))
7655 (defun org-agenda-filter-show-all-tag nil
7656 "Remove tag filter overlays from the agenda buffer."
7657 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7658 (setq org-agenda-tag-filter-overlays nil
7659 org-agenda-tag-filter nil
7660 org-agenda-filter-form nil)
7661 (org-agenda-set-mode-name))
7663 (defun org-agenda-filter-show-all-re nil
7664 "Remove regexp filter overlays from the agenda buffer."
7665 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7666 (setq org-agenda-re-filter-overlays nil
7667 org-agenda-regexp-filter nil
7668 org-agenda-filter-form nil)
7669 (org-agenda-set-mode-name))
7671 (defun org-agenda-filter-show-all-cat nil
7672 "Remove category filter overlays from the agenda buffer."
7673 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7674 (setq org-agenda-cat-filter-overlays nil
7675 org-agenda-filtered-by-category nil
7676 org-agenda-category-filter nil
7677 org-agenda-filter-form nil)
7678 (org-agenda-set-mode-name))
7680 (defun org-agenda-manipulate-query-add ()
7681 "Manipulate the query by adding a search term with positive selection.
7682 Positive selection means the term must be matched for selection of an entry."
7683 (interactive)
7684 (org-agenda-manipulate-query ?\[))
7685 (defun org-agenda-manipulate-query-subtract ()
7686 "Manipulate the query by adding a search term with negative selection.
7687 Negative selection means term must not be matched for selection of an entry."
7688 (interactive)
7689 (org-agenda-manipulate-query ?\]))
7690 (defun org-agenda-manipulate-query-add-re ()
7691 "Manipulate the query by adding a search regexp with positive selection.
7692 Positive selection means the regexp must match for selection of an entry."
7693 (interactive)
7694 (org-agenda-manipulate-query ?\{))
7695 (defun org-agenda-manipulate-query-subtract-re ()
7696 "Manipulate the query by adding a search regexp with negative selection.
7697 Negative selection means regexp must not match for selection of an entry."
7698 (interactive)
7699 (org-agenda-manipulate-query ?\}))
7700 (defun org-agenda-manipulate-query (char)
7701 (cond
7702 ((memq org-agenda-type '(timeline agenda))
7703 (let ((org-agenda-include-inactive-timestamps t))
7704 (org-agenda-redo))
7705 (message "Display now includes inactive timestamps as well"))
7706 ((eq org-agenda-type 'search)
7707 (org-add-to-string
7708 'org-agenda-query-string
7709 (if org-agenda-last-search-view-search-was-boolean
7710 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7711 (?\{ . " +{}") (?\} . " -{}"))))
7712 " "))
7713 (setq org-agenda-redo-command
7714 (list 'org-search-view
7715 (car (get-text-property (min (1- (point-max)) (point))
7716 'org-last-args))
7717 org-agenda-query-string
7718 (+ (length org-agenda-query-string)
7719 (if (member char '(?\{ ?\})) 0 1))))
7720 (set-register org-agenda-query-register org-agenda-query-string)
7721 (let ((org-agenda-overriding-arguments
7722 (cdr org-agenda-redo-command)))
7723 (org-agenda-redo)))
7724 (t (error "Cannot manipulate query for %s-type agenda buffers"
7725 org-agenda-type))))
7727 (defun org-add-to-string (var string)
7728 (set var (concat (symbol-value var) string)))
7730 (defun org-agenda-goto-date (span)
7731 "Jump to DATE in agenda."
7732 (interactive "P")
7733 (let* ((org-read-date-prefer-future
7734 (eval org-agenda-jump-prefer-future))
7735 (date (org-read-date))
7736 (day (time-to-days (org-time-string-to-time date)))
7737 (org-agenda-sticky-orig org-agenda-sticky)
7738 (org-agenda-buffer-tmp-name (buffer-name))
7739 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7740 (0-arg (or current-prefix-arg (car args)))
7741 (2-arg (nth 2 args))
7742 (with-hour-p (nth 4 org-agenda-redo-command))
7743 (newcmd (list 'org-agenda-list 0-arg date
7744 (org-agenda-span-to-ndays
7745 2-arg (org-time-string-to-absolute date))
7746 with-hour-p))
7747 (newargs (cdr newcmd))
7748 (inhibit-read-only t)
7749 org-agenda-sticky)
7750 (if (not (org-agenda-check-type t 'agenda))
7751 (error "Not available in non-agenda views")
7752 (add-text-properties (point-min) (point-max)
7753 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7754 (org-agenda-redo)
7755 (goto-char (point-min))
7756 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7757 (save-excursion (move-beginning-of-line 2) (eobp))))
7758 (move-beginning-of-line 2))
7759 (setq org-agenda-sticky org-agenda-sticky-orig
7760 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7762 (defun org-agenda-goto-today ()
7763 "Go to today."
7764 (interactive)
7765 (org-agenda-check-type t 'timeline 'agenda)
7766 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7767 (curspan (nth 2 args))
7768 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7769 (cond
7770 (tdpos (goto-char tdpos))
7771 ((eq org-agenda-type 'agenda)
7772 (let* ((sd (org-agenda-compute-starting-span
7773 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7774 (org-agenda-overriding-arguments args))
7775 (setf (nth 1 org-agenda-overriding-arguments) sd)
7776 (org-agenda-redo)
7777 (org-agenda-find-same-or-today-or-agenda)))
7778 (t (error "Cannot find today")))))
7780 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7781 (goto-char
7782 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7783 (text-property-any (point-min) (point-max) 'org-today t)
7784 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7785 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7786 (org-agenda-goto-block-beginning))
7787 (point-min))))
7789 (defun org-agenda-goto-block-beginning ()
7790 "Go the agenda block beginning."
7791 (interactive)
7792 (if (not (derived-mode-p 'org-agenda-mode))
7793 (error "Cannot execute this command outside of org-agenda-mode buffers")
7794 (let (dest)
7795 (save-excursion
7796 (unless (looking-at "\\'")
7797 (forward-char))
7798 (let* ((prop 'org-agenda-structural-header)
7799 (p (previous-single-property-change (point) prop))
7800 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7801 (1- (point))) prop)))
7802 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7803 (if (not dest)
7804 (error "Cannot find the beginning of the blog")
7805 (goto-char dest)
7806 (move-beginning-of-line 1)))))
7808 (defun org-agenda-later (arg)
7809 "Go forward in time by the current span.
7810 With prefix ARG, go forward that many times the current span."
7811 (interactive "p")
7812 (org-agenda-check-type t 'agenda)
7813 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7814 (span (or (nth 2 args) org-agenda-current-span))
7815 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7816 (greg (calendar-gregorian-from-absolute sd))
7817 (cnt (org-get-at-bol 'org-day-cnt))
7818 greg2)
7819 (cond
7820 ((numberp span)
7821 (setq sd (+ (* span arg) sd)))
7822 ((eq span 'day)
7823 (setq sd (+ arg sd)))
7824 ((eq span 'week)
7825 (setq sd (+ (* 7 arg) sd)))
7826 ((eq span 'month)
7827 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7828 sd (calendar-absolute-from-gregorian greg2))
7829 (setcar greg2 (1+ (car greg2))))
7830 ((eq span 'year)
7831 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7832 sd (calendar-absolute-from-gregorian greg2))
7833 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7835 (setq sd (+ (* span arg) sd))))
7836 (let ((org-agenda-overriding-cmd
7837 ;; `cmd' may have been set by `org-agenda-run-series' which
7838 ;; uses `org-agenda-overriding-cmd' to decide whether
7839 ;; overriding is allowed for `cmd'
7840 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7841 (org-agenda-overriding-arguments
7842 (list (car args) sd span)))
7843 (org-agenda-redo)
7844 (org-agenda-find-same-or-today-or-agenda cnt))))
7846 (defun org-agenda-earlier (arg)
7847 "Go backward in time by the current span.
7848 With prefix ARG, go backward that many times the current span."
7849 (interactive "p")
7850 (org-agenda-later (- arg)))
7852 (defun org-agenda-view-mode-dispatch ()
7853 "Call one of the view mode commands."
7854 (interactive)
7855 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7856 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7857 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7858 (let ((a (read-char-exclusive)))
7859 (case a
7860 (?\ (call-interactively 'org-agenda-reset-view))
7861 (?d (call-interactively 'org-agenda-day-view))
7862 (?w (call-interactively 'org-agenda-week-view))
7863 (?m (call-interactively 'org-agenda-month-view))
7864 (?y (call-interactively 'org-agenda-year-view))
7865 (?l (call-interactively 'org-agenda-log-mode))
7866 (?L (org-agenda-log-mode '(4)))
7867 (?c (org-agenda-log-mode 'clockcheck))
7868 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7869 (?a (call-interactively 'org-agenda-archives-mode))
7870 (?A (org-agenda-archives-mode 'files))
7871 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7872 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7873 (?G (call-interactively 'org-agenda-toggle-time-grid))
7874 (?D (call-interactively 'org-agenda-toggle-diary))
7875 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7876 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7877 (org-agenda-check-type t 'timeline 'agenda)
7878 (org-agenda-redo))
7879 (message "Display now includes inactive timestamps as well"))
7880 (?q (message "Abort"))
7881 (otherwise (error "Invalid key" )))))
7883 (defun org-agenda-reset-view ()
7884 "Switch to default view for agenda."
7885 (interactive)
7886 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7887 (defun org-agenda-day-view (&optional day-of-month)
7888 "Switch to daily view for agenda.
7889 With argument DAY-OF-MONTH, switch to that day of the month."
7890 (interactive "P")
7891 (org-agenda-change-time-span 'day day-of-month))
7892 (defun org-agenda-week-view (&optional iso-week)
7893 "Switch to daily view for agenda.
7894 With argument ISO-WEEK, switch to the corresponding ISO week.
7895 If ISO-WEEK has more then 2 digits, only the last two encode the
7896 week. Any digits before this encode a year. So 200712 means
7897 week 12 of year 2007. Years in the range 1938-2037 can also be
7898 written as 2-digit years."
7899 (interactive "P")
7900 (org-agenda-change-time-span 'week iso-week))
7901 (defun org-agenda-month-view (&optional month)
7902 "Switch to monthly view for agenda.
7903 With argument MONTH, switch to that month."
7904 (interactive "P")
7905 (org-agenda-change-time-span 'month month))
7906 (defun org-agenda-year-view (&optional year)
7907 "Switch to yearly view for agenda.
7908 With argument YEAR, switch to that year.
7909 If MONTH has more then 2 digits, only the last two encode the
7910 month. Any digits before this encode a year. So 200712 means
7911 December year 2007. Years in the range 1938-2037 can also be
7912 written as 2-digit years."
7913 (interactive "P")
7914 (when year
7915 (setq year (org-small-year-to-year year)))
7916 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7917 (org-agenda-change-time-span 'year year)
7918 (error "Abort")))
7920 (defun org-agenda-change-time-span (span &optional n)
7921 "Change the agenda view to SPAN.
7922 SPAN may be `day', `week', `month', `year'."
7923 (org-agenda-check-type t 'agenda)
7924 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7925 (curspan (nth 2 args)))
7926 (if (and (not n) (equal curspan span))
7927 (error "Viewing span is already \"%s\"" span))
7928 (let* ((sd (or (org-get-at-bol 'day)
7929 (nth 1 args)
7930 org-starting-day))
7931 (sd (org-agenda-compute-starting-span sd span n))
7932 (org-agenda-overriding-cmd
7933 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7934 (org-agenda-overriding-arguments
7935 (list (car args) sd span)))
7936 (org-agenda-redo)
7937 (org-agenda-find-same-or-today-or-agenda))
7938 (org-agenda-set-mode-name)
7939 (message "Switched to %s view" span)))
7941 (defun org-agenda-compute-starting-span (sd span &optional n)
7942 "Compute starting date for agenda.
7943 SPAN may be `day', `week', `month', `year'. The return value
7944 is a cons cell with the starting date and the number of days,
7945 so that the date SD will be in that range."
7946 (let* ((greg (calendar-gregorian-from-absolute sd))
7947 (dg (nth 1 greg))
7948 (mg (car greg))
7949 (yg (nth 2 greg)))
7950 (cond
7951 ((eq span 'day)
7952 (when n
7953 (setq sd (+ (calendar-absolute-from-gregorian
7954 (list mg 1 yg))
7955 n -1))))
7956 ((eq span 'week)
7957 (let* ((nt (calendar-day-of-week
7958 (calendar-gregorian-from-absolute sd)))
7959 (d (if org-agenda-start-on-weekday
7960 (- nt org-agenda-start-on-weekday)
7963 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7964 (when n
7965 (require 'cal-iso)
7966 (when (> n 99)
7967 (setq y1 (org-small-year-to-year (/ n 100))
7968 n (mod n 100)))
7969 (setq sd
7970 (calendar-absolute-from-iso
7971 (list n 1
7972 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7973 ((eq span 'month)
7974 (let (y1)
7975 (when (and n (> n 99))
7976 (setq y1 (org-small-year-to-year (/ n 100))
7977 n (mod n 100)))
7978 (setq sd (calendar-absolute-from-gregorian
7979 (list (or n mg) 1 (or y1 yg))))))
7980 ((eq span 'year)
7981 (setq sd (calendar-absolute-from-gregorian
7982 (list 1 1 (or n yg))))))
7983 sd))
7985 (defun org-agenda-next-date-line (&optional arg)
7986 "Jump to the next line indicating a date in agenda buffer."
7987 (interactive "p")
7988 (org-agenda-check-type t 'agenda 'timeline)
7989 (beginning-of-line 1)
7990 ;; This does not work if user makes date format that starts with a blank
7991 (if (looking-at "^\\S-") (forward-char 1))
7992 (if (not (re-search-forward "^\\S-" nil t arg))
7993 (progn
7994 (backward-char 1)
7995 (error "No next date after this line in this buffer")))
7996 (goto-char (match-beginning 0)))
7998 (defun org-agenda-previous-date-line (&optional arg)
7999 "Jump to the previous line indicating a date in agenda buffer."
8000 (interactive "p")
8001 (org-agenda-check-type t 'agenda 'timeline)
8002 (beginning-of-line 1)
8003 (if (not (re-search-backward "^\\S-" nil t arg))
8004 (error "No previous date before this line in this buffer")))
8006 ;; Initialize the highlight
8007 (defvar org-hl (make-overlay 1 1))
8008 (overlay-put org-hl 'face 'highlight)
8010 (defun org-highlight (begin end &optional buffer)
8011 "Highlight a region with overlay."
8012 (move-overlay org-hl begin end (or buffer (current-buffer))))
8014 (defun org-unhighlight ()
8015 "Detach overlay INDEX."
8016 (org-detach-overlay org-hl))
8018 (defun org-unhighlight-once ()
8019 "Remove the highlight from its position, and this function from the hook."
8020 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8021 (org-unhighlight))
8023 (defvar org-agenda-pre-follow-window-conf nil)
8024 (defun org-agenda-follow-mode ()
8025 "Toggle follow mode in an agenda buffer."
8026 (interactive)
8027 (unless org-agenda-follow-mode
8028 (setq org-agenda-pre-follow-window-conf
8029 (current-window-configuration)))
8030 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8031 (unless org-agenda-follow-mode
8032 (set-window-configuration org-agenda-pre-follow-window-conf))
8033 (org-agenda-set-mode-name)
8034 (org-agenda-do-context-action)
8035 (message "Follow mode is %s"
8036 (if org-agenda-follow-mode "on" "off")))
8038 (defun org-agenda-entry-text-mode (&optional arg)
8039 "Toggle entry text mode in an agenda buffer."
8040 (interactive "P")
8041 (if (or org-agenda-tag-filter
8042 org-agenda-category-filter
8043 org-agenda-regexp-filter
8044 org-agenda-top-headline-filter)
8045 (user-error "Can't show entry text in filtered views")
8046 (setq org-agenda-entry-text-mode (or (integerp arg)
8047 (not org-agenda-entry-text-mode)))
8048 (org-agenda-entry-text-hide)
8049 (and org-agenda-entry-text-mode
8050 (let ((org-agenda-entry-text-maxlines
8051 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8052 (org-agenda-entry-text-show)))
8053 (org-agenda-set-mode-name)
8054 (message "Entry text mode is %s%s"
8055 (if org-agenda-entry-text-mode "on" "off")
8056 (if (not org-agenda-entry-text-mode) ""
8057 (format " (maximum number of lines is %d)"
8058 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8060 (defun org-agenda-clockreport-mode (&optional with-filter)
8061 "Toggle clocktable mode in an agenda buffer.
8062 With prefix arg WITH-FILTER, make the clocktable respect the current
8063 agenda filter."
8064 (interactive "P")
8065 (org-agenda-check-type t 'agenda)
8066 (if with-filter
8067 (setq org-agenda-clockreport-mode 'with-filter)
8068 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8069 (org-agenda-set-mode-name)
8070 (org-agenda-redo)
8071 (message "Clocktable mode is %s"
8072 (if org-agenda-clockreport-mode "on" "off")))
8074 (defun org-agenda-log-mode (&optional special)
8075 "Toggle log mode in an agenda buffer.
8076 With argument SPECIAL, show all possible log items, not only the ones
8077 configured in `org-agenda-log-mode-items'.
8078 With a double `C-u' prefix arg, show *only* log items, nothing else."
8079 (interactive "P")
8080 (org-agenda-check-type t 'agenda 'timeline)
8081 (setq org-agenda-show-log
8082 (cond
8083 ((equal special '(16)) 'only)
8084 ((eq special 'clockcheck)
8085 (if (eq org-agenda-show-log 'clockcheck)
8086 nil 'clockcheck))
8087 (special '(closed clock state))
8088 (t (not org-agenda-show-log))))
8089 (org-agenda-set-mode-name)
8090 (org-agenda-redo)
8091 (message "Log mode is %s"
8092 (if org-agenda-show-log "on" "off")))
8094 (defun org-agenda-archives-mode (&optional with-files)
8095 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8096 When called with a prefix argument, include all archive files as well."
8097 (interactive "P")
8098 (setq org-agenda-archives-mode
8099 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8100 (org-agenda-set-mode-name)
8101 (org-agenda-redo)
8102 (message
8103 "%s"
8104 (cond
8105 ((eq org-agenda-archives-mode nil)
8106 "No archives are included")
8107 ((eq org-agenda-archives-mode 'trees)
8108 (format "Trees with :%s: tag are included" org-archive-tag))
8109 ((eq org-agenda-archives-mode t)
8110 (format "Trees with :%s: tag and all active archive files are included"
8111 org-archive-tag)))))
8113 (defun org-agenda-toggle-diary ()
8114 "Toggle diary inclusion in an agenda buffer."
8115 (interactive)
8116 (org-agenda-check-type t 'agenda)
8117 (setq org-agenda-include-diary (not org-agenda-include-diary))
8118 (org-agenda-redo)
8119 (org-agenda-set-mode-name)
8120 (message "Diary inclusion turned %s"
8121 (if org-agenda-include-diary "on" "off")))
8123 (defun org-agenda-toggle-deadlines ()
8124 "Toggle inclusion of entries with a deadline in an agenda buffer."
8125 (interactive)
8126 (org-agenda-check-type t 'agenda)
8127 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8128 (org-agenda-redo)
8129 (org-agenda-set-mode-name)
8130 (message "Deadlines inclusion turned %s"
8131 (if org-agenda-include-deadlines "on" "off")))
8133 (defun org-agenda-toggle-time-grid ()
8134 "Toggle time grid in an agenda buffer."
8135 (interactive)
8136 (org-agenda-check-type t 'agenda)
8137 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8138 (org-agenda-redo)
8139 (org-agenda-set-mode-name)
8140 (message "Time-grid turned %s"
8141 (if org-agenda-use-time-grid "on" "off")))
8143 (defun org-agenda-set-mode-name ()
8144 "Set the mode name to indicate all the small mode settings."
8145 (setq mode-name
8146 (list "Org-Agenda"
8147 (if (get 'org-agenda-files 'org-restrict) " []" "")
8149 '(:eval (org-agenda-span-name org-agenda-current-span))
8150 (if org-agenda-follow-mode " Follow" "")
8151 (if org-agenda-entry-text-mode " ETxt" "")
8152 (if org-agenda-include-diary " Diary" "")
8153 (if org-agenda-include-deadlines " Ddl" "")
8154 (if org-agenda-use-time-grid " Grid" "")
8155 (if (and (boundp 'org-habit-show-habits)
8156 org-habit-show-habits) " Habit" "")
8157 (cond
8158 ((consp org-agenda-show-log) " LogAll")
8159 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8160 (org-agenda-show-log " Log")
8161 (t ""))
8162 (if (or org-agenda-category-filter
8163 (get 'org-agenda-category-filter :preset-filter))
8164 '(:eval (org-propertize
8165 (concat " <"
8166 (mapconcat
8167 'identity
8168 (append
8169 (get 'org-agenda-category-filter :preset-filter)
8170 org-agenda-category-filter)
8172 ">")
8173 'face 'org-agenda-filter-category
8174 'help-echo "Category used in filtering")) "")
8175 (if (or org-agenda-tag-filter
8176 (get 'org-agenda-tag-filter :preset-filter))
8177 '(:eval (org-propertize
8178 (concat " {"
8179 (mapconcat
8180 'identity
8181 (append
8182 (get 'org-agenda-tag-filter :preset-filter)
8183 org-agenda-tag-filter)
8185 "}")
8186 'face 'org-agenda-filter-tags
8187 'help-echo "Tags used in filtering")) "")
8188 (if (or org-agenda-regexp-filter
8189 (get 'org-agenda-regexp-filter :preset-filter))
8190 '(:eval (org-propertize
8191 (concat " ["
8192 (mapconcat
8193 'identity
8194 (append
8195 (get 'org-agenda-regexp-filter :preset-filter)
8196 org-agenda-regexp-filter)
8198 "]")
8199 'face 'org-agenda-filter-regexp
8200 'help-echo "Regexp used in filtering")) "")
8201 (if org-agenda-archives-mode
8202 (if (eq org-agenda-archives-mode t)
8203 " Archives"
8204 (format " :%s:" org-archive-tag))
8206 (if org-agenda-clockreport-mode
8207 (if (eq org-agenda-clockreport-mode 'with-filter)
8208 " Clock{}" " Clock")
8209 "")))
8210 (force-mode-line-update))
8212 (define-obsolete-function-alias
8213 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8215 (defun org-agenda-update-agenda-type ()
8216 "Update the agenda type after each command."
8217 (setq org-agenda-type
8218 (or (get-text-property (point) 'org-agenda-type)
8219 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8221 (defun org-agenda-next-line ()
8222 "Move cursor to the next line, and show if follow mode is active."
8223 (interactive)
8224 (call-interactively 'next-line)
8225 (org-agenda-do-context-action))
8227 (defun org-agenda-previous-line ()
8228 "Move cursor to the previous line, and show if follow-mode is active."
8229 (interactive)
8230 (call-interactively 'previous-line)
8231 (org-agenda-do-context-action))
8233 (defun org-agenda-next-item (n)
8234 "Move cursor to next agenda item."
8235 (interactive "p")
8236 (let ((col (current-column)))
8237 (dotimes (c n)
8238 (when (next-single-property-change (point-at-eol) 'org-marker)
8239 (move-end-of-line 1)
8240 (goto-char (next-single-property-change (point) 'org-marker))))
8241 (org-move-to-column col))
8242 (org-agenda-do-context-action))
8244 (defun org-agenda-previous-item (n)
8245 "Move cursor to next agenda item."
8246 (interactive "p")
8247 (dotimes (c n)
8248 (let ((col (current-column))
8249 (goto (save-excursion
8250 (move-end-of-line 0)
8251 (previous-single-property-change (point) 'org-marker))))
8252 (if goto (goto-char goto))
8253 (org-move-to-column col)))
8254 (org-agenda-do-context-action))
8256 (defun org-agenda-do-context-action ()
8257 "Show outline path and, maybe, follow mode window."
8258 (let ((m (org-get-at-bol 'org-marker)))
8259 (when (and (markerp m) (marker-buffer m))
8260 (and org-agenda-follow-mode
8261 (if org-agenda-follow-indirect
8262 (org-agenda-tree-to-indirect-buffer nil)
8263 (org-agenda-show)))
8264 (and org-agenda-show-outline-path
8265 (org-with-point-at m (org-display-outline-path t))))))
8267 (defun org-agenda-show-tags ()
8268 "Show the tags applicable to the current item."
8269 (interactive)
8270 (let* ((tags (org-get-at-bol 'tags)))
8271 (if tags
8272 (message "Tags are :%s:"
8273 (org-no-properties (mapconcat 'identity tags ":")))
8274 (message "No tags associated with this line"))))
8276 (defun org-agenda-goto (&optional highlight)
8277 "Go to the Org-mode file which contains the item at point."
8278 (interactive)
8279 (let* ((marker (or (org-get-at-bol 'org-marker)
8280 (org-agenda-error)))
8281 (buffer (marker-buffer marker))
8282 (pos (marker-position marker)))
8283 (switch-to-buffer-other-window buffer)
8284 (widen)
8285 (push-mark)
8286 (goto-char pos)
8287 (when (derived-mode-p 'org-mode)
8288 (org-show-context 'agenda)
8289 (save-excursion
8290 (and (outline-next-heading)
8291 (org-flag-heading nil)))) ; show the next heading
8292 (when (outline-invisible-p)
8293 (show-entry)) ; display invisible text
8294 (recenter (/ (window-height) 2))
8295 (run-hooks 'org-agenda-after-show-hook)
8296 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8298 (defvar org-agenda-after-show-hook nil
8299 "Normal hook run after an item has been shown from the agenda.
8300 Point is in the buffer where the item originated.")
8302 (defun org-agenda-kill ()
8303 "Kill the entry or subtree belonging to the current agenda entry."
8304 (interactive)
8305 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8306 (let* ((bufname-orig (buffer-name))
8307 (marker (or (org-get-at-bol 'org-marker)
8308 (org-agenda-error)))
8309 (buffer (marker-buffer marker))
8310 (pos (marker-position marker))
8311 (type (org-get-at-bol 'type))
8312 dbeg dend (n 0) conf)
8313 (org-with-remote-undo buffer
8314 (with-current-buffer buffer
8315 (save-excursion
8316 (goto-char pos)
8317 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8318 (setq dbeg (progn (org-back-to-heading t) (point))
8319 dend (org-end-of-subtree t t))
8320 (setq dbeg (point-at-bol)
8321 dend (min (point-max) (1+ (point-at-eol)))))
8322 (goto-char dbeg)
8323 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8324 (setq conf (or (eq t org-agenda-confirm-kill)
8325 (and (numberp org-agenda-confirm-kill)
8326 (> n org-agenda-confirm-kill))))
8327 (and conf
8328 (not (y-or-n-p
8329 (format "Delete entry with %d lines in buffer \"%s\"? "
8330 n (buffer-name buffer))))
8331 (error "Abort"))
8332 (let ((org-agenda-buffer-name bufname-orig))
8333 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8334 (with-current-buffer buffer (delete-region dbeg dend))
8335 (message "Agenda item and source killed"))))
8337 (defvar org-archive-default-command) ; defined in org-archive.el
8338 (defun org-agenda-archive-default ()
8339 "Archive the entry or subtree belonging to the current agenda entry."
8340 (interactive)
8341 (require 'org-archive)
8342 (org-agenda-archive-with org-archive-default-command))
8344 (defun org-agenda-archive-default-with-confirmation ()
8345 "Archive the entry or subtree belonging to the current agenda entry."
8346 (interactive)
8347 (require 'org-archive)
8348 (org-agenda-archive-with org-archive-default-command 'confirm))
8350 (defun org-agenda-archive ()
8351 "Archive the entry or subtree belonging to the current agenda entry."
8352 (interactive)
8353 (org-agenda-archive-with 'org-archive-subtree))
8355 (defun org-agenda-archive-to-archive-sibling ()
8356 "Move the entry to the archive sibling."
8357 (interactive)
8358 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8360 (defun org-agenda-archive-with (cmd &optional confirm)
8361 "Move the entry to the archive sibling."
8362 (interactive)
8363 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8364 (let* ((bufname-orig (buffer-name))
8365 (marker (or (org-get-at-bol 'org-marker)
8366 (org-agenda-error)))
8367 (buffer (marker-buffer marker))
8368 (pos (marker-position marker)))
8369 (org-with-remote-undo buffer
8370 (with-current-buffer buffer
8371 (if (derived-mode-p 'org-mode)
8372 (if (and confirm
8373 (not (y-or-n-p "Archive this subtree or entry? ")))
8374 (error "Abort")
8375 (save-excursion
8376 (goto-char pos)
8377 (let ((org-agenda-buffer-name bufname-orig))
8378 (org-remove-subtree-entries-from-agenda))
8379 (org-back-to-heading t)
8380 (funcall cmd)))
8381 (error "Archiving works only in Org-mode files"))))))
8383 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8384 "Remove all lines in the agenda that correspond to a given subtree.
8385 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8386 If this information is not given, the function uses the tree at point."
8387 (let ((buf (or buf (current-buffer))) m p)
8388 (save-excursion
8389 (unless (and beg end)
8390 (org-back-to-heading t)
8391 (setq beg (point))
8392 (org-end-of-subtree t)
8393 (setq end (point)))
8394 (set-buffer (get-buffer org-agenda-buffer-name))
8395 (save-excursion
8396 (goto-char (point-max))
8397 (beginning-of-line 1)
8398 (while (not (bobp))
8399 (when (and (setq m (org-get-at-bol 'org-marker))
8400 (equal buf (marker-buffer m))
8401 (setq p (marker-position m))
8402 (>= p beg)
8403 (< p end))
8404 (let ((inhibit-read-only t))
8405 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8406 (beginning-of-line 0))))))
8408 (defun org-agenda-refile (&optional goto rfloc no-update)
8409 "Refile the item at point.
8411 When GOTO is 0 or '(64), clear the refile cache.
8412 When GOTO is '(16), go to the location of the last refiled item.
8413 RFLOC can be a refile location obtained in a different way.
8414 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8415 (interactive "P")
8416 (cond
8417 ((member goto '(0 (64)))
8418 (org-refile-cache-clear))
8419 ((equal goto '(16))
8420 (org-refile-goto-last-stored))
8422 (let* ((buffer-orig (buffer-name))
8423 (marker (or (org-get-at-bol 'org-hd-marker)
8424 (org-agenda-error)))
8425 (buffer (marker-buffer marker))
8426 (pos (marker-position marker))
8427 (rfloc (or rfloc
8428 (org-refile-get-location
8429 (if goto "Goto" "Refile to") buffer
8430 org-refile-allow-creating-parent-nodes))))
8431 (with-current-buffer buffer
8432 (save-excursion
8433 (save-restriction
8434 (widen)
8435 (goto-char marker)
8436 (let ((org-agenda-buffer-name buffer-orig))
8437 (org-remove-subtree-entries-from-agenda))
8438 (org-refile goto buffer rfloc)))))
8439 (unless no-update (org-agenda-redo)))))
8441 (defun org-agenda-open-link (&optional arg)
8442 "Open the link(s) in the current entry, if any.
8443 This looks for a link in the displayed line in the agenda.
8444 It also looks at the text of the entry itself."
8445 (interactive "P")
8446 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8447 (org-get-at-bol 'org-marker)))
8448 (buffer (and marker (marker-buffer marker)))
8449 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8450 (lkall (org-offer-links-in-entry buffer marker arg prefix))
8451 (lk0 (car lkall))
8452 (lk (if (stringp lk0) (list lk0) lk0))
8453 (lkend (cdr lkall))
8454 trg)
8455 (cond
8456 ((and buffer lk)
8457 (mapcar (lambda(l)
8458 (with-current-buffer buffer
8459 (setq trg (and (string-match org-bracket-link-regexp l)
8460 (match-string 1 l)))
8461 (if (or (not trg) (string-match org-any-link-re trg))
8462 (save-excursion
8463 (save-restriction
8464 (widen)
8465 (goto-char marker)
8466 (when (search-forward l nil lkend)
8467 (goto-char (match-beginning 0))
8468 (org-open-at-point))))
8469 ;; This is an internal link, widen the buffer
8470 (switch-to-buffer-other-window buffer)
8471 (widen)
8472 (goto-char marker)
8473 (when (search-forward l nil lkend)
8474 (goto-char (match-beginning 0))
8475 (org-open-at-point)))))
8476 lk))
8477 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8478 (save-excursion
8479 (beginning-of-line 1)
8480 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8481 (org-open-link-from-string (match-string 1)))
8482 (t (message "No link to open here")))))
8484 (defun org-agenda-copy-local-variable (var)
8485 "Get a variable from a referenced buffer and install it here."
8486 (let ((m (org-get-at-bol 'org-marker)))
8487 (when (and m (buffer-live-p (marker-buffer m)))
8488 (org-set-local var (with-current-buffer (marker-buffer m)
8489 (symbol-value var))))))
8491 (defun org-agenda-switch-to (&optional delete-other-windows)
8492 "Go to the Org-mode file which contains the item at point."
8493 (interactive)
8494 (if (and org-return-follows-link
8495 (not (org-get-at-bol 'org-marker))
8496 (org-in-regexp org-bracket-link-regexp))
8497 (org-open-link-from-string (match-string 0))
8498 (let* ((marker (or (org-get-at-bol 'org-marker)
8499 (org-agenda-error)))
8500 (buffer (marker-buffer marker))
8501 (pos (marker-position marker)))
8502 (org-pop-to-buffer-same-window buffer)
8503 (and delete-other-windows (delete-other-windows))
8504 (widen)
8505 (goto-char pos)
8506 (when (derived-mode-p 'org-mode)
8507 (org-show-context 'agenda)
8508 (save-excursion
8509 (and (outline-next-heading)
8510 (org-flag-heading nil))) ; show the next heading
8511 (when (outline-invisible-p)
8512 (show-entry)) ; display invisible text
8513 (run-hooks 'org-agenda-after-show-hook)))))
8515 (defun org-agenda-goto-mouse (ev)
8516 "Go to the Org-mode file which contains the item at the mouse click."
8517 (interactive "e")
8518 (mouse-set-point ev)
8519 (org-agenda-goto))
8521 (defun org-agenda-show (&optional full-entry)
8522 "Display the Org-mode file which contains the item at point.
8523 With prefix argument FULL-ENTRY, make the entire entry visible
8524 if it was hidden in the outline."
8525 (interactive "P")
8526 (let ((win (selected-window)))
8527 (if full-entry
8528 (let ((org-show-entry-below t))
8529 (org-agenda-goto t))
8530 (org-agenda-goto t))
8531 (select-window win)))
8533 (defvar org-agenda-show-window nil)
8534 (defun org-agenda-show-and-scroll-up (&optional arg)
8535 "Display the Org-mode file which contains the item at point.
8536 When called repeatedly, scroll the window that is displaying the buffer.
8537 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8538 `show-subtree' to display the item, so that drawers and logbooks stay
8539 folded."
8540 (interactive "P")
8541 (let ((win (selected-window)))
8542 (if (and (window-live-p org-agenda-show-window)
8543 (eq this-command last-command))
8544 (progn
8545 (select-window org-agenda-show-window)
8546 (ignore-errors (scroll-up)))
8547 (org-agenda-goto t)
8548 (if arg (org-show-entry) (show-subtree))
8549 (setq org-agenda-show-window (selected-window)))
8550 (select-window win)))
8552 (defun org-agenda-show-scroll-down ()
8553 "Scroll down the window showing the agenda."
8554 (interactive)
8555 (let ((win (selected-window)))
8556 (when (window-live-p org-agenda-show-window)
8557 (select-window org-agenda-show-window)
8558 (ignore-errors (scroll-down))
8559 (select-window win))))
8561 (defun org-agenda-show-1 (&optional more)
8562 "Display the Org-mode file which contains the item at point.
8563 The prefix arg selects the amount of information to display:
8565 0 hide the subtree
8566 1 just show the entry according to defaults.
8567 2 show the children view
8568 3 show the subtree view
8569 4 show the entire subtree and any LOGBOOK drawers
8570 5 show the entire subtree and any drawers
8571 With prefix argument FULL-ENTRY, make the entire entry visible
8572 if it was hidden in the outline."
8573 (interactive "p")
8574 (let ((win (selected-window)))
8575 (org-agenda-goto t)
8576 (org-recenter-heading 1)
8577 (cond
8578 ((= more 0)
8579 (hide-subtree)
8580 (save-excursion
8581 (org-back-to-heading)
8582 (run-hook-with-args 'org-cycle-hook 'folded))
8583 (message "Remote: FOLDED"))
8584 ((and (org-called-interactively-p 'any) (= more 1))
8585 (message "Remote: show with default settings"))
8586 ((= more 2)
8587 (show-entry)
8588 (show-children)
8589 (save-excursion
8590 (org-back-to-heading)
8591 (run-hook-with-args 'org-cycle-hook 'children))
8592 (message "Remote: CHILDREN"))
8593 ((= more 3)
8594 (show-subtree)
8595 (save-excursion
8596 (org-back-to-heading)
8597 (run-hook-with-args 'org-cycle-hook 'subtree))
8598 (message "Remote: SUBTREE"))
8599 ((= more 4)
8600 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8601 (org-drawer-regexp
8602 (concat "^[ \t]*:\\("
8603 (mapconcat 'regexp-quote org-drawers "\\|")
8604 "\\):[ \t]*$")))
8605 (show-subtree)
8606 (save-excursion
8607 (org-back-to-heading)
8608 (org-cycle-hide-drawers 'subtree)))
8609 (message "Remote: SUBTREE AND LOGBOOK"))
8610 ((> more 4)
8611 (show-subtree)
8612 (message "Remote: SUBTREE AND ALL DRAWERS")))
8613 (select-window win)))
8615 (defun org-recenter-heading (n)
8616 (save-excursion
8617 (org-back-to-heading)
8618 (recenter n)))
8620 (defvar org-agenda-cycle-counter nil)
8621 (defun org-agenda-cycle-show (&optional n)
8622 "Show the current entry in another window, with default settings.
8623 Default settings are taken from `org-show-hierarchy-above' and siblings.
8624 When use repeatedly in immediate succession, the remote entry will cycle
8625 through visibility
8627 children -> subtree -> folded
8629 When called with a numeric prefix arg, that arg will be passed through to
8630 `org-agenda-show-1'. For the interpretation of that argument, see the
8631 docstring of `org-agenda-show-1'."
8632 (interactive "P")
8633 (if (integerp n)
8634 (setq org-agenda-cycle-counter n)
8635 (if (not (eq last-command this-command))
8636 (setq org-agenda-cycle-counter 1)
8637 (if (equal org-agenda-cycle-counter 0)
8638 (setq org-agenda-cycle-counter 2)
8639 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8640 (if (> org-agenda-cycle-counter 3)
8641 (setq org-agenda-cycle-counter 0)))))
8642 (org-agenda-show-1 org-agenda-cycle-counter))
8644 (defun org-agenda-recenter (arg)
8645 "Display the Org-mode file which contains the item at point and recenter."
8646 (interactive "P")
8647 (let ((win (selected-window)))
8648 (org-agenda-goto t)
8649 (recenter arg)
8650 (select-window win)))
8652 (defun org-agenda-show-mouse (ev)
8653 "Display the Org-mode file which contains the item at the mouse click."
8654 (interactive "e")
8655 (mouse-set-point ev)
8656 (org-agenda-show))
8658 (defun org-agenda-check-no-diary ()
8659 "Check if the entry is a diary link and abort if yes."
8660 (if (org-get-at-bol 'org-agenda-diary-link)
8661 (org-agenda-error)))
8663 (defun org-agenda-error ()
8664 (error "Command not allowed in this line"))
8666 (defun org-agenda-tree-to-indirect-buffer (arg)
8667 "Show the subtree corresponding to the current entry in an indirect buffer.
8668 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8670 With a numerical prefix ARG, go up to this level and then take that tree.
8671 With a negative numeric ARG, go up by this number of levels.
8672 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8673 use the dedicated frame)."
8674 (interactive "P")
8675 (if current-prefix-arg
8676 (org-agenda-do-tree-to-indirect-buffer arg)
8677 (let ((agenda-buffer (buffer-name))
8678 (agenda-window (selected-window))
8679 (indirect-window
8680 (and org-last-indirect-buffer
8681 (get-buffer-window org-last-indirect-buffer))))
8682 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8683 (unless (or (eq org-indirect-buffer-display 'new-frame)
8684 (eq org-indirect-buffer-display 'dedicated-frame))
8685 (unwind-protect
8686 (unless (and indirect-window (window-live-p indirect-window))
8687 (setq indirect-window (split-window agenda-window)))
8688 (and indirect-window (select-window indirect-window))
8689 (switch-to-buffer org-last-indirect-buffer :norecord)
8690 (fit-window-to-buffer indirect-window)))
8691 (select-window (get-buffer-window agenda-buffer)))))
8693 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8694 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8695 (org-agenda-check-no-diary)
8696 (let* ((marker (or (org-get-at-bol 'org-marker)
8697 (org-agenda-error)))
8698 (buffer (marker-buffer marker))
8699 (pos (marker-position marker)))
8700 (with-current-buffer buffer
8701 (save-excursion
8702 (goto-char pos)
8703 (funcall 'org-tree-to-indirect-buffer arg)))))
8705 (defvar org-last-heading-marker (make-marker)
8706 "Marker pointing to the headline that last changed its TODO state
8707 by a remote command from the agenda.")
8709 (defun org-agenda-todo-nextset ()
8710 "Switch TODO entry to next sequence."
8711 (interactive)
8712 (org-agenda-todo 'nextset))
8714 (defun org-agenda-todo-previousset ()
8715 "Switch TODO entry to previous sequence."
8716 (interactive)
8717 (org-agenda-todo 'previousset))
8719 (defun org-agenda-todo (&optional arg)
8720 "Cycle TODO state of line at point, also in Org-mode file.
8721 This changes the line at point, all other lines in the agenda referring to
8722 the same tree node, and the headline of the tree node in the Org-mode file."
8723 (interactive "P")
8724 (org-agenda-check-no-diary)
8725 (let* ((col (current-column))
8726 (marker (or (org-get-at-bol 'org-marker)
8727 (org-agenda-error)))
8728 (buffer (marker-buffer marker))
8729 (pos (marker-position marker))
8730 (hdmarker (org-get-at-bol 'org-hd-marker))
8731 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8732 (inhibit-read-only t)
8733 org-agenda-headline-snapshot-before-repeat newhead just-one)
8734 (org-with-remote-undo buffer
8735 (with-current-buffer buffer
8736 (widen)
8737 (goto-char pos)
8738 (org-show-context 'agenda)
8739 (save-excursion
8740 (and (outline-next-heading)
8741 (org-flag-heading nil))) ; show the next heading
8742 (let ((current-prefix-arg arg))
8743 (call-interactively 'org-todo))
8744 (and (bolp) (forward-char 1))
8745 (setq newhead (org-get-heading))
8746 (when (and (org-bound-and-true-p
8747 org-agenda-headline-snapshot-before-repeat)
8748 (not (equal org-agenda-headline-snapshot-before-repeat
8749 newhead))
8750 todayp)
8751 (setq newhead org-agenda-headline-snapshot-before-repeat
8752 just-one t))
8753 (save-excursion
8754 (org-back-to-heading)
8755 (move-marker org-last-heading-marker (point))))
8756 (beginning-of-line 1)
8757 (save-excursion
8758 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8759 (org-move-to-column col))))
8761 (defun org-agenda-add-note (&optional arg)
8762 "Add a time-stamped note to the entry at point."
8763 (interactive "P")
8764 (org-agenda-check-no-diary)
8765 (let* ((marker (or (org-get-at-bol 'org-marker)
8766 (org-agenda-error)))
8767 (buffer (marker-buffer marker))
8768 (pos (marker-position marker))
8769 (hdmarker (org-get-at-bol 'org-hd-marker))
8770 (inhibit-read-only t))
8771 (with-current-buffer buffer
8772 (widen)
8773 (goto-char pos)
8774 (org-show-context 'agenda)
8775 (save-excursion
8776 (and (outline-next-heading)
8777 (org-flag-heading nil))) ; show the next heading
8778 (org-add-note))))
8780 (defun org-agenda-change-all-lines (newhead hdmarker
8781 &optional fixface just-this)
8782 "Change all lines in the agenda buffer which match HDMARKER.
8783 The new content of the line will be NEWHEAD (as modified by
8784 `org-agenda-format-item'). HDMARKER is checked with
8785 `equal' against all `org-hd-marker' text properties in the file.
8786 If FIXFACE is non-nil, the face of each item is modified according to
8787 the new TODO state.
8788 If JUST-THIS is non-nil, change just the current line, not all.
8789 If FORCE-TAGS is non nil, the car of it returns the new tags."
8790 (let* ((inhibit-read-only t)
8791 (line (org-current-line))
8792 (org-agenda-buffer (current-buffer))
8793 (thetags (with-current-buffer (marker-buffer hdmarker)
8794 (save-excursion (save-restriction (widen)
8795 (goto-char hdmarker)
8796 (org-get-tags-at)))))
8797 props m pl undone-face done-face finish new dotime level cat tags)
8798 (save-excursion
8799 (goto-char (point-max))
8800 (beginning-of-line 1)
8801 (while (not finish)
8802 (setq finish (bobp))
8803 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8804 (or (not just-this) (= (org-current-line) line))
8805 (equal m hdmarker))
8806 (setq props (text-properties-at (point))
8807 dotime (org-get-at-bol 'dotime)
8808 cat (org-get-at-bol 'org-category)
8809 level (org-get-at-bol 'level)
8810 tags thetags
8812 (let ((org-prefix-format-compiled
8813 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8814 org-prefix-format-compiled))
8815 (extra (org-get-at-bol 'extra)))
8816 (with-current-buffer (marker-buffer hdmarker)
8817 (save-excursion
8818 (save-restriction
8819 (widen)
8820 (org-agenda-format-item extra newhead level cat tags dotime)))))
8821 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8822 undone-face (org-get-at-bol 'undone-face)
8823 done-face (org-get-at-bol 'done-face))
8824 (beginning-of-line 1)
8825 (cond
8826 ((equal new "")
8827 (and (looking-at ".*\n?") (replace-match "")))
8828 ((looking-at ".*")
8829 (replace-match new t t)
8830 (beginning-of-line 1)
8831 (add-text-properties (point-at-bol) (point-at-eol) props)
8832 (when fixface
8833 (add-text-properties
8834 (point-at-bol) (point-at-eol)
8835 (list 'face
8836 (if org-last-todo-state-is-todo
8837 undone-face done-face))))
8838 (org-agenda-highlight-todo 'line)
8839 (beginning-of-line 1))
8840 (t (error "Line update did not work")))
8841 (save-restriction
8842 (narrow-to-region (point-at-bol) (point-at-eol))
8843 (org-agenda-finalize)))
8844 (beginning-of-line 0)))))
8846 (defun org-agenda-align-tags (&optional line)
8847 "Align all tags in agenda items to `org-agenda-tags-column'."
8848 (let ((inhibit-read-only t) l c)
8849 (save-excursion
8850 (goto-char (if line (point-at-bol) (point-min)))
8851 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8852 (if line (point-at-eol) nil) t)
8853 (add-text-properties
8854 (match-beginning 2) (match-end 2)
8855 (list 'face (delq nil (let ((prop (get-text-property
8856 (match-beginning 2) 'face)))
8857 (or (listp prop) (setq prop (list prop)))
8858 (if (memq 'org-tag prop)
8859 prop
8860 (cons 'org-tag prop))))))
8861 (setq l (- (match-end 2) (match-beginning 2))
8862 c (if (< org-agenda-tags-column 0)
8863 (- (abs org-agenda-tags-column) l)
8864 org-agenda-tags-column))
8865 (delete-region (match-beginning 1) (match-end 1))
8866 (goto-char (match-beginning 1))
8867 (insert (org-add-props
8868 (make-string (max 1 (- c (current-column))) ?\ )
8869 (plist-put (copy-sequence (text-properties-at (point)))
8870 'face nil))))
8871 (goto-char (point-min))
8872 (org-font-lock-add-tag-faces (point-max)))))
8874 (defun org-agenda-priority-up ()
8875 "Increase the priority of line at point, also in Org-mode file."
8876 (interactive)
8877 (org-agenda-priority 'up))
8879 (defun org-agenda-priority-down ()
8880 "Decrease the priority of line at point, also in Org-mode file."
8881 (interactive)
8882 (org-agenda-priority 'down))
8884 (defun org-agenda-priority (&optional force-direction)
8885 "Set the priority of line at point, also in Org-mode file.
8886 This changes the line at point, all other lines in the agenda referring to
8887 the same tree node, and the headline of the tree node in the Org-mode file.
8888 Called with a universal prefix arg, show the priority instead of setting it."
8889 (interactive "P")
8890 (if (equal force-direction '(4))
8891 (org-show-priority)
8892 (unless org-enable-priority-commands
8893 (error "Priority commands are disabled"))
8894 (org-agenda-check-no-diary)
8895 (let* ((marker (or (org-get-at-bol 'org-marker)
8896 (org-agenda-error)))
8897 (hdmarker (org-get-at-bol 'org-hd-marker))
8898 (buffer (marker-buffer hdmarker))
8899 (pos (marker-position hdmarker))
8900 (inhibit-read-only t)
8901 newhead)
8902 (org-with-remote-undo buffer
8903 (with-current-buffer buffer
8904 (widen)
8905 (goto-char pos)
8906 (org-show-context 'agenda)
8907 (save-excursion
8908 (and (outline-next-heading)
8909 (org-flag-heading nil))) ; show the next heading
8910 (funcall 'org-priority force-direction)
8911 (end-of-line 1)
8912 (setq newhead (org-get-heading)))
8913 (org-agenda-change-all-lines newhead hdmarker)
8914 (beginning-of-line 1)))))
8916 ;; FIXME: should fix the tags property of the agenda line.
8917 (defun org-agenda-set-tags (&optional tag onoff)
8918 "Set tags for the current headline."
8919 (interactive)
8920 (org-agenda-check-no-diary)
8921 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8922 (call-interactively 'org-change-tag-in-region)
8923 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8924 (org-agenda-error)))
8925 (buffer (marker-buffer hdmarker))
8926 (pos (marker-position hdmarker))
8927 (inhibit-read-only t)
8928 newhead)
8929 (org-with-remote-undo buffer
8930 (with-current-buffer buffer
8931 (widen)
8932 (goto-char pos)
8933 (save-excursion
8934 (org-show-context 'agenda))
8935 (save-excursion
8936 (and (outline-next-heading)
8937 (org-flag-heading nil))) ; show the next heading
8938 (goto-char pos)
8939 (if tag
8940 (org-toggle-tag tag onoff)
8941 (call-interactively 'org-set-tags))
8942 (end-of-line 1)
8943 (setq newhead (org-get-heading)))
8944 (org-agenda-change-all-lines newhead hdmarker)
8945 (beginning-of-line 1)))))
8947 (defun org-agenda-set-property ()
8948 "Set a property for the current headline."
8949 (interactive)
8950 (org-agenda-check-no-diary)
8951 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8952 (org-agenda-error)))
8953 (buffer (marker-buffer hdmarker))
8954 (pos (marker-position hdmarker))
8955 (inhibit-read-only t)
8956 newhead)
8957 (org-with-remote-undo buffer
8958 (with-current-buffer buffer
8959 (widen)
8960 (goto-char pos)
8961 (save-excursion
8962 (org-show-context 'agenda))
8963 (save-excursion
8964 (and (outline-next-heading)
8965 (org-flag-heading nil))) ; show the next heading
8966 (goto-char pos)
8967 (call-interactively 'org-set-property)))))
8969 (defun org-agenda-set-effort ()
8970 "Set the effort property for the current headline."
8971 (interactive)
8972 (org-agenda-check-no-diary)
8973 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8974 (org-agenda-error)))
8975 (buffer (marker-buffer hdmarker))
8976 (pos (marker-position hdmarker))
8977 (inhibit-read-only t)
8978 newhead)
8979 (org-with-remote-undo buffer
8980 (with-current-buffer buffer
8981 (widen)
8982 (goto-char pos)
8983 (save-excursion
8984 (org-show-context 'agenda))
8985 (save-excursion
8986 (and (outline-next-heading)
8987 (org-flag-heading nil))) ; show the next heading
8988 (goto-char pos)
8989 (call-interactively 'org-set-effort)
8990 (end-of-line 1)
8991 (setq newhead (org-get-heading)))
8992 (org-agenda-change-all-lines newhead hdmarker))))
8994 (defun org-agenda-toggle-archive-tag ()
8995 "Toggle the archive tag for the current entry."
8996 (interactive)
8997 (org-agenda-check-no-diary)
8998 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8999 (org-agenda-error)))
9000 (buffer (marker-buffer hdmarker))
9001 (pos (marker-position hdmarker))
9002 (inhibit-read-only t)
9003 newhead)
9004 (org-with-remote-undo buffer
9005 (with-current-buffer buffer
9006 (widen)
9007 (goto-char pos)
9008 (org-show-context 'agenda)
9009 (save-excursion
9010 (and (outline-next-heading)
9011 (org-flag-heading nil))) ; show the next heading
9012 (call-interactively 'org-toggle-archive-tag)
9013 (end-of-line 1)
9014 (setq newhead (org-get-heading)))
9015 (org-agenda-change-all-lines newhead hdmarker)
9016 (beginning-of-line 1))))
9018 (defun org-agenda-do-date-later (arg)
9019 (interactive "P")
9020 (cond
9021 ((or (equal arg '(16))
9022 (memq last-command
9023 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9024 (setq this-command 'org-agenda-date-later-minutes)
9025 (org-agenda-date-later-minutes 1))
9026 ((or (equal arg '(4))
9027 (memq last-command
9028 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9029 (setq this-command 'org-agenda-date-later-hours)
9030 (org-agenda-date-later-hours 1))
9032 (org-agenda-date-later (prefix-numeric-value arg)))))
9034 (defun org-agenda-do-date-earlier (arg)
9035 (interactive "P")
9036 (cond
9037 ((or (equal arg '(16))
9038 (memq last-command
9039 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9040 (setq this-command 'org-agenda-date-earlier-minutes)
9041 (org-agenda-date-earlier-minutes 1))
9042 ((or (equal arg '(4))
9043 (memq last-command
9044 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9045 (setq this-command 'org-agenda-date-earlier-hours)
9046 (org-agenda-date-earlier-hours 1))
9048 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9050 (defun org-agenda-date-later (arg &optional what)
9051 "Change the date of this item to ARG day(s) later."
9052 (interactive "p")
9053 (org-agenda-check-type t 'agenda 'timeline)
9054 (org-agenda-check-no-diary)
9055 (let* ((marker (or (org-get-at-bol 'org-marker)
9056 (org-agenda-error)))
9057 (buffer (marker-buffer marker))
9058 (pos (marker-position marker))
9059 cdate today)
9060 (org-with-remote-undo buffer
9061 (with-current-buffer buffer
9062 (widen)
9063 (goto-char pos)
9064 (if (not (org-at-timestamp-p))
9065 (error "Cannot find time stamp"))
9066 (when (and org-agenda-move-date-from-past-immediately-to-today
9067 (equal arg 1)
9068 (or (not what) (eq what 'day))
9069 (not (save-match-data (org-at-date-range-p))))
9070 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9071 cdate (calendar-absolute-from-gregorian
9072 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9073 today (org-today))
9074 (if (> today cdate)
9075 ;; immediately shift to today
9076 (setq arg (- today cdate))))
9077 (org-timestamp-change arg (or what 'day))
9078 (when (and (org-at-date-range-p)
9079 (re-search-backward org-tr-regexp-both (point-at-bol)))
9080 (let ((end org-last-changed-timestamp))
9081 (org-timestamp-change arg (or what 'day))
9082 (setq org-last-changed-timestamp
9083 (concat org-last-changed-timestamp "--" end)))))
9084 (org-agenda-show-new-time marker org-last-changed-timestamp))
9085 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9087 (defun org-agenda-date-earlier (arg &optional what)
9088 "Change the date of this item to ARG day(s) earlier."
9089 (interactive "p")
9090 (org-agenda-date-later (- arg) what))
9092 (defun org-agenda-date-later-minutes (arg)
9093 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9094 (interactive "p")
9095 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9096 (org-agenda-date-later arg 'minute))
9098 (defun org-agenda-date-earlier-minutes (arg)
9099 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9100 (interactive "p")
9101 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9102 (org-agenda-date-earlier arg 'minute))
9104 (defun org-agenda-date-later-hours (arg)
9105 "Change the time of this item, in hour steps."
9106 (interactive "p")
9107 (org-agenda-date-later arg 'hour))
9109 (defun org-agenda-date-earlier-hours (arg)
9110 "Change the time of this item, in hour steps."
9111 (interactive "p")
9112 (org-agenda-date-earlier arg 'hour))
9114 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9115 "Show new date stamp via text properties."
9116 ;; We use text properties to make this undoable
9117 (let ((inhibit-read-only t))
9118 (setq stamp (concat prefix " => " stamp " "))
9119 (save-excursion
9120 (goto-char (point-max))
9121 (while (not (bobp))
9122 (when (equal marker (org-get-at-bol 'org-marker))
9123 (org-move-to-column (- (window-width) (length stamp)) t)
9124 (org-agenda-fix-tags-filter-overlays-at (point))
9125 (if (featurep 'xemacs)
9126 ;; Use `duplicable' property to trigger undo recording
9127 (let ((ex (make-extent nil nil))
9128 (gl (make-glyph stamp)))
9129 (set-glyph-face gl 'secondary-selection)
9130 (set-extent-properties
9131 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9132 (insert-extent ex (1- (point)) (point-at-eol)))
9133 (add-text-properties
9134 (1- (point)) (point-at-eol)
9135 (list 'display (org-add-props stamp nil
9136 'face 'secondary-selection))))
9137 (beginning-of-line 1))
9138 (beginning-of-line 0)))))
9140 (defun org-agenda-date-prompt (arg)
9141 "Change the date of this item. Date is prompted for, with default today.
9142 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9143 be used to request time specification in the time stamp."
9144 (interactive "P")
9145 (org-agenda-check-type t 'agenda 'timeline)
9146 (org-agenda-check-no-diary)
9147 (let* ((marker (or (org-get-at-bol 'org-marker)
9148 (org-agenda-error)))
9149 (buffer (marker-buffer marker))
9150 (pos (marker-position marker)))
9151 (org-with-remote-undo buffer
9152 (with-current-buffer buffer
9153 (widen)
9154 (goto-char pos)
9155 (if (not (org-at-timestamp-p t))
9156 (error "Cannot find time stamp"))
9157 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9158 (org-agenda-show-new-time marker org-last-changed-timestamp))
9159 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9161 (defun org-agenda-schedule (arg &optional time)
9162 "Schedule the item at point.
9163 ARG is passed through to `org-schedule'."
9164 (interactive "P")
9165 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9166 (org-agenda-check-no-diary)
9167 (let* ((marker (or (org-get-at-bol 'org-marker)
9168 (org-agenda-error)))
9169 (type (marker-insertion-type marker))
9170 (buffer (marker-buffer marker))
9171 (pos (marker-position marker))
9172 (org-insert-labeled-timestamps-at-point nil)
9174 (set-marker-insertion-type marker t)
9175 (org-with-remote-undo buffer
9176 (with-current-buffer buffer
9177 (widen)
9178 (goto-char pos)
9179 (setq ts (org-schedule arg time)))
9180 (org-agenda-show-new-time marker ts " S"))
9181 (message "%s" ts)))
9183 (defun org-agenda-deadline (arg &optional time)
9184 "Schedule the item at point.
9185 ARG is passed through to `org-deadline'."
9186 (interactive "P")
9187 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9188 (org-agenda-check-no-diary)
9189 (let* ((marker (or (org-get-at-bol 'org-marker)
9190 (org-agenda-error)))
9191 (buffer (marker-buffer marker))
9192 (pos (marker-position marker))
9193 (org-insert-labeled-timestamps-at-point nil)
9195 (org-with-remote-undo buffer
9196 (with-current-buffer buffer
9197 (widen)
9198 (goto-char pos)
9199 (setq ts (org-deadline arg time)))
9200 (org-agenda-show-new-time marker ts " D"))
9201 (message "%s" ts)))
9203 (defun org-agenda-clock-in (&optional arg)
9204 "Start the clock on the currently selected item."
9205 (interactive "P")
9206 (org-agenda-check-no-diary)
9207 (if (equal arg '(4))
9208 (org-clock-in arg)
9209 (let* ((marker (or (org-get-at-bol 'org-marker)
9210 (org-agenda-error)))
9211 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9212 (pos (marker-position marker))
9213 (col (current-column))
9214 newhead)
9215 (org-with-remote-undo (marker-buffer marker)
9216 (with-current-buffer (marker-buffer marker)
9217 (widen)
9218 (goto-char pos)
9219 (org-show-context 'agenda)
9220 (org-show-entry)
9221 (org-cycle-hide-drawers 'children)
9222 (org-clock-in arg)
9223 (setq newhead (org-get-heading)))
9224 (org-agenda-change-all-lines newhead hdmarker))
9225 (org-move-to-column col))))
9227 (defun org-agenda-clock-out ()
9228 "Stop the currently running clock."
9229 (interactive)
9230 (unless (marker-buffer org-clock-marker)
9231 (error "No running clock"))
9232 (let ((marker (make-marker)) (col (current-column)) newhead)
9233 (org-with-remote-undo (marker-buffer org-clock-marker)
9234 (with-current-buffer (marker-buffer org-clock-marker)
9235 (save-excursion
9236 (save-restriction
9237 (widen)
9238 (goto-char org-clock-marker)
9239 (org-back-to-heading t)
9240 (move-marker marker (point))
9241 (org-clock-out)
9242 (setq newhead (org-get-heading))))))
9243 (org-agenda-change-all-lines newhead marker)
9244 (move-marker marker nil)
9245 (org-move-to-column col)
9246 (org-agenda-unmark-clocking-task)))
9248 (defun org-agenda-clock-cancel (&optional arg)
9249 "Cancel the currently running clock."
9250 (interactive "P")
9251 (unless (marker-buffer org-clock-marker)
9252 (user-error "No running clock"))
9253 (org-with-remote-undo (marker-buffer org-clock-marker)
9254 (org-clock-cancel)))
9256 (defun org-agenda-clock-goto ()
9257 "Jump to the currently clocked in task within the agenda.
9258 If the currently clocked in task is not listed in the agenda
9259 buffer, display it in another window."
9260 (interactive)
9261 (let (pos)
9262 (mapc (lambda (o)
9263 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9264 (setq pos (overlay-start o))))
9265 (overlays-in (point-min) (point-max)))
9266 (cond (pos (goto-char pos))
9267 ;; If the currently clocked entry is not in the agenda
9268 ;; buffer, we visit it in another window:
9269 (org-clock-current-task
9270 (org-switch-to-buffer-other-window (org-clock-goto)))
9271 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9273 (defun org-agenda-diary-entry-in-org-file ()
9274 "Make a diary entry in the file `org-agenda-diary-file'."
9275 (let (d1 d2 char (text "") dp1 dp2)
9276 (if (equal (buffer-name) "*Calendar*")
9277 (setq d1 (calendar-cursor-to-date t)
9278 d2 (car calendar-mark-ring))
9279 (setq dp1 (get-text-property (point-at-bol) 'day))
9280 (unless dp1 (user-error "No date defined in current line"))
9281 (setq d1 (calendar-gregorian-from-absolute dp1)
9282 d2 (and (ignore-errors (mark))
9283 (save-excursion
9284 (goto-char (mark))
9285 (setq dp2 (get-text-property (point-at-bol) 'day)))
9286 (calendar-gregorian-from-absolute dp2))))
9287 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9288 (setq char (read-char-exclusive))
9289 (cond
9290 ((equal char ?d)
9291 (setq text (read-string "Day entry: "))
9292 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9293 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9294 ((equal char ?a)
9295 (setq d1 (list (car d1) (nth 1 d1)
9296 (read-number (format "Reference year [%d]: " (nth 2 d1))
9297 (nth 2 d1))))
9298 (setq text (read-string "Anniversary (use %d to show years): "))
9299 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9300 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9301 ((equal char ?b)
9302 (setq text (read-string "Block entry: "))
9303 (unless (and d1 d2 (not (equal d1 d2)))
9304 (user-error "No block of days selected"))
9305 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9306 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9307 ((equal char ?j)
9308 (org-switch-to-buffer-other-window
9309 (find-file-noselect org-agenda-diary-file))
9310 (require 'org-datetree)
9311 (org-datetree-find-date-create d1)
9312 (org-reveal t))
9313 (t (user-error "Invalid selection character `%c'" char)))))
9315 (defcustom org-agenda-insert-diary-strategy 'date-tree
9316 "Where in `org-agenda-diary-file' should new entries be added?
9317 Valid values:
9319 date-tree in the date tree, as child of the date
9320 top-level as top-level entries at the end of the file."
9321 :group 'org-agenda
9322 :type '(choice
9323 (const :tag "in a date tree" date-tree)
9324 (const :tag "as top level at end of file" top-level)))
9326 (defcustom org-agenda-insert-diary-extract-time nil
9327 "Non-nil means extract any time specification from the diary entry."
9328 :group 'org-agenda
9329 :version "24.1"
9330 :type 'boolean)
9332 (defcustom org-agenda-bulk-mark-char ">"
9333 "A single-character string to be used as the bulk mark."
9334 :group 'org-agenda
9335 :version "24.1"
9336 :type 'string)
9338 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9339 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9340 If TEXT is not empty, it will become the headline of the new entry, and
9341 the resulting entry will not be shown. When TEXT is empty, switch to
9342 `org-agenda-diary-file' and let the user finish the entry there."
9343 (let ((cw (current-window-configuration)))
9344 (org-switch-to-buffer-other-window
9345 (find-file-noselect org-agenda-diary-file))
9346 (widen)
9347 (goto-char (point-min))
9348 (cond
9349 ((eq type 'anniversary)
9350 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9351 (progn
9352 (or (org-at-heading-p t)
9353 (progn
9354 (outline-next-heading)
9355 (insert "* Anniversaries\n\n")
9356 (beginning-of-line -1)))))
9357 (outline-next-heading)
9358 (org-back-over-empty-lines)
9359 (backward-char 1)
9360 (insert "\n")
9361 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9362 (nth 2 d1) (car d1) (nth 1 d1) text)))
9363 ((eq type 'day)
9364 (let ((org-prefix-has-time t)
9365 (org-agenda-time-leading-zero t)
9366 fmt time time2)
9367 (if org-agenda-insert-diary-extract-time
9368 ;; Use org-agenda-format-item to parse text for a time-range and
9369 ;; remove it. FIXME: This is a hack, we should refactor
9370 ;; that function to make time extraction available separately
9371 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9372 time (get-text-property 0 'time fmt)
9373 time2 (if (> (length time) 0)
9374 ;; split-string removes trailing ...... if
9375 ;; no end time given. First space
9376 ;; separates time from date.
9377 (concat " " (car (split-string time "\\.")))
9378 nil)
9379 text (get-text-property 0 'txt fmt)))
9380 (if (eq org-agenda-insert-diary-strategy 'top-level)
9381 (org-agenda-insert-diary-as-top-level text)
9382 (require 'org-datetree)
9383 (org-datetree-find-date-create d1)
9384 (org-agenda-insert-diary-make-new-entry text))
9385 (org-insert-time-stamp (org-time-from-absolute
9386 (calendar-absolute-from-gregorian d1))
9387 nil nil nil nil time2))
9388 (end-of-line 0))
9389 ((eq type 'block)
9390 (if (> (calendar-absolute-from-gregorian d1)
9391 (calendar-absolute-from-gregorian d2))
9392 (setq d1 (prog1 d2 (setq d2 d1))))
9393 (if (eq org-agenda-insert-diary-strategy 'top-level)
9394 (org-agenda-insert-diary-as-top-level text)
9395 (require 'org-datetree)
9396 (org-datetree-find-date-create d1)
9397 (org-agenda-insert-diary-make-new-entry text))
9398 (org-insert-time-stamp (org-time-from-absolute
9399 (calendar-absolute-from-gregorian d1)))
9400 (insert "--")
9401 (org-insert-time-stamp (org-time-from-absolute
9402 (calendar-absolute-from-gregorian d2)))
9403 (end-of-line 0)))
9404 (if (string-match "\\S-" text)
9405 (progn
9406 (set-window-configuration cw)
9407 (message "%s entry added to %s"
9408 (capitalize (symbol-name type))
9409 (abbreviate-file-name org-agenda-diary-file)))
9410 (org-reveal t)
9411 (message "Please finish entry here"))))
9413 (defun org-agenda-insert-diary-as-top-level (text)
9414 "Make new entry as a top-level entry at the end of the file.
9415 Add TEXT as headline, and position the cursor in the second line so that
9416 a timestamp can be added there."
9417 (widen)
9418 (goto-char (point-max))
9419 (or (bolp) (insert "\n"))
9420 (insert "* " text "\n")
9421 (if org-adapt-indentation (org-indent-to-column 2)))
9423 (defun org-agenda-insert-diary-make-new-entry (text)
9424 "Make new entry as last child of current entry.
9425 Add TEXT as headline, and position the cursor in the second line so that
9426 a timestamp can be added there."
9427 (let ((org-show-following-heading t)
9428 (org-show-siblings t)
9429 (org-show-hierarchy-above t)
9430 (org-show-entry-below t)
9431 col)
9432 (outline-next-heading)
9433 (org-back-over-empty-lines)
9434 (or (looking-at "[ \t]*$")
9435 (progn (insert "\n") (backward-char 1)))
9436 (org-insert-heading nil t)
9437 (org-do-demote)
9438 (setq col (current-column))
9439 (insert text "\n")
9440 (if org-adapt-indentation (org-indent-to-column col))
9441 (let ((org-show-following-heading t)
9442 (org-show-siblings t)
9443 (org-show-hierarchy-above t)
9444 (org-show-entry-below t))
9445 (org-show-context))))
9447 (defun org-agenda-diary-entry ()
9448 "Make a diary entry, like the `i' command from the calendar.
9449 All the standard commands work: block, weekly etc.
9450 When `org-agenda-diary-file' points to a file,
9451 `org-agenda-diary-entry-in-org-file' is called instead to create
9452 entries in that Org-mode file."
9453 (interactive)
9454 (if (not (eq org-agenda-diary-file 'diary-file))
9455 (org-agenda-diary-entry-in-org-file)
9456 (require 'diary-lib)
9457 (let* ((char (progn
9458 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9459 (read-char-exclusive)))
9460 (cmd (cdr (assoc char
9461 '((?d . insert-diary-entry)
9462 (?w . insert-weekly-diary-entry)
9463 (?m . insert-monthly-diary-entry)
9464 (?y . insert-yearly-diary-entry)
9465 (?a . insert-anniversary-diary-entry)
9466 (?b . insert-block-diary-entry)
9467 (?c . insert-cyclic-diary-entry)))))
9468 (oldf (symbol-function 'calendar-cursor-to-date))
9469 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9470 (point (point))
9471 (mark (or (mark t) (point))))
9472 (unless cmd
9473 (user-error "No command associated with <%c>" char))
9474 (unless (and (get-text-property point 'day)
9475 (or (not (equal ?b char))
9476 (get-text-property mark 'day)))
9477 (user-error "Don't know which date to use for diary entry"))
9478 ;; We implement this by hacking the `calendar-cursor-to-date' function
9479 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9480 (let ((calendar-mark-ring
9481 (list (calendar-gregorian-from-absolute
9482 (or (get-text-property mark 'day)
9483 (get-text-property point 'day))))))
9484 (unwind-protect
9485 (progn
9486 (fset 'calendar-cursor-to-date
9487 (lambda (&optional error dummy)
9488 (calendar-gregorian-from-absolute
9489 (get-text-property point 'day))))
9490 (call-interactively cmd))
9491 (fset 'calendar-cursor-to-date oldf))))))
9493 (defun org-agenda-execute-calendar-command (cmd)
9494 "Execute a calendar command from the agenda with date from cursor."
9495 (org-agenda-check-type t 'agenda 'timeline)
9496 (require 'diary-lib)
9497 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9498 (user-error "Don't know which date to use for the calendar command"))
9499 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9500 (point (point))
9501 (date (calendar-gregorian-from-absolute
9502 (get-text-property point 'day)))
9503 ;; the following 2 vars are needed in the calendar
9504 (displayed-month (car date))
9505 (displayed-year (nth 2 date)))
9506 (unwind-protect
9507 (progn
9508 (fset 'calendar-cursor-to-date
9509 (lambda (&optional error dummy)
9510 (calendar-gregorian-from-absolute
9511 (get-text-property point 'day))))
9512 (call-interactively cmd))
9513 (fset 'calendar-cursor-to-date oldf))))
9515 (defun org-agenda-phases-of-moon ()
9516 "Display the phases of the moon for the 3 months around the cursor date."
9517 (interactive)
9518 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9520 (defun org-agenda-holidays ()
9521 "Display the holidays for the 3 months around the cursor date."
9522 (interactive)
9523 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9525 (defvar calendar-longitude) ; defined in calendar.el
9526 (defvar calendar-latitude) ; defined in calendar.el
9527 (defvar calendar-location-name) ; defined in calendar.el
9529 (defun org-agenda-sunrise-sunset (arg)
9530 "Display sunrise and sunset for the cursor date.
9531 Latitude and longitude can be specified with the variables
9532 `calendar-latitude' and `calendar-longitude'. When called with prefix
9533 argument, latitude and longitude will be prompted for."
9534 (interactive "P")
9535 (require 'solar)
9536 (let ((calendar-longitude (if arg nil calendar-longitude))
9537 (calendar-latitude (if arg nil calendar-latitude))
9538 (calendar-location-name
9539 (if arg "the given coordinates" calendar-location-name)))
9540 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9542 (defun org-agenda-goto-calendar ()
9543 "Open the Emacs calendar with the date at the cursor."
9544 (interactive)
9545 (org-agenda-check-type t 'agenda 'timeline)
9546 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9547 (user-error "Don't know which date to open in calendar")))
9548 (date (calendar-gregorian-from-absolute day))
9549 (calendar-move-hook nil)
9550 (calendar-view-holidays-initially-flag nil)
9551 (calendar-view-diary-initially-flag nil))
9552 (calendar)
9553 (calendar-goto-date date)))
9555 ;;;###autoload
9556 (defun org-calendar-goto-agenda ()
9557 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9558 This is a command that has to be installed in `calendar-mode-map'."
9559 (interactive)
9560 (org-agenda-list nil (calendar-absolute-from-gregorian
9561 (calendar-cursor-to-date))
9562 nil))
9564 (defun org-agenda-convert-date ()
9565 (interactive)
9566 (org-agenda-check-type t 'agenda 'timeline)
9567 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9568 date s)
9569 (unless day
9570 (user-error "Don't know which date to convert"))
9571 (setq date (calendar-gregorian-from-absolute day))
9572 (setq s (concat
9573 "Gregorian: " (calendar-date-string date) "\n"
9574 "ISO: " (calendar-iso-date-string date) "\n"
9575 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9576 "Julian: " (calendar-julian-date-string date) "\n"
9577 "Astron. JD: " (calendar-astro-date-string date)
9578 " (Julian date number at noon UTC)\n"
9579 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9580 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9581 "French: " (calendar-french-date-string date) "\n"
9582 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9583 "Mayan: " (calendar-mayan-date-string date) "\n"
9584 "Coptic: " (calendar-coptic-date-string date) "\n"
9585 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9586 "Persian: " (calendar-persian-date-string date) "\n"
9587 "Chinese: " (calendar-chinese-date-string date) "\n"))
9588 (with-output-to-temp-buffer "*Dates*"
9589 (princ s))
9590 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9592 ;;; Bulk commands
9594 (defun org-agenda-bulk-marked-p ()
9595 (eq (get-char-property (point-at-bol) 'type)
9596 'org-marked-entry-overlay))
9598 (defun org-agenda-bulk-mark (&optional arg)
9599 "Mark the entry at point for future bulk action."
9600 (interactive "p")
9601 (dotimes (i (or arg 1))
9602 (unless (org-get-at-bol 'org-agenda-diary-link)
9603 (let* ((m (org-get-at-bol 'org-hd-marker))
9605 (unless (org-agenda-bulk-marked-p)
9606 (unless m (user-error "Nothing to mark at point"))
9607 (push m org-agenda-bulk-marked-entries)
9608 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9609 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9610 (org-get-todo-face "TODO")
9611 'evaporate)
9612 (overlay-put ov 'type 'org-marked-entry-overlay))
9613 (end-of-line 1)
9614 (or (ignore-errors
9615 (goto-char (next-single-property-change (point) 'txt)))
9616 (beginning-of-line 2))
9617 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9618 (beginning-of-line 2))
9619 (message "%d entries marked for bulk action"
9620 (length org-agenda-bulk-marked-entries))))))
9622 (defun org-agenda-bulk-mark-all ()
9623 "Mark all entries for future agenda bulk action."
9624 (interactive)
9625 (org-agenda-bulk-mark-regexp "."))
9627 (defun org-agenda-bulk-mark-regexp (regexp)
9628 "Mark entries matching REGEXP for future agenda bulk action."
9629 (interactive "sMark entries matching regexp: ")
9630 (let ((entries-marked 0) txt-at-point)
9631 (save-excursion
9632 (goto-char (point-min))
9633 (goto-char (next-single-property-change (point) 'txt))
9634 (while (and (re-search-forward regexp nil t)
9635 (setq txt-at-point (get-text-property (point) 'txt)))
9636 (when (string-match regexp txt-at-point)
9637 (setq entries-marked (1+ entries-marked))
9638 (call-interactively 'org-agenda-bulk-mark))))
9639 (if (not entries-marked)
9640 (message "No entry matching this regexp."))))
9642 (defun org-agenda-bulk-unmark (&optional arg)
9643 "Unmark the entry at point for future bulk action."
9644 (interactive "P")
9645 (if arg
9646 (org-agenda-bulk-unmark-all)
9647 (cond ((org-agenda-bulk-marked-p)
9648 (org-agenda-bulk-remove-overlays
9649 (point-at-bol) (+ 2 (point-at-bol)))
9650 (setq org-agenda-bulk-marked-entries
9651 (delete (org-get-at-bol 'org-hd-marker)
9652 org-agenda-bulk-marked-entries))
9653 (end-of-line 1)
9654 (or (ignore-errors
9655 (goto-char (next-single-property-change (point) 'txt)))
9656 (beginning-of-line 2))
9657 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9658 (beginning-of-line 2))
9659 (message "%d entries left marked for bulk action"
9660 (length org-agenda-bulk-marked-entries)))
9661 (t (message "No entry to unmark here")))))
9663 (defun org-agenda-bulk-toggle-all ()
9664 "Toggle all marks for bulk action."
9665 (interactive)
9666 (save-excursion
9667 (goto-char (point-min))
9668 (while (ignore-errors
9669 (goto-char (next-single-property-change (point) 'txt)))
9670 (org-agenda-bulk-toggle))))
9672 (defun org-agenda-bulk-toggle ()
9673 "Toggle the mark at point for bulk action."
9674 (interactive)
9675 (if (org-agenda-bulk-marked-p)
9676 (org-agenda-bulk-unmark)
9677 (org-agenda-bulk-mark)))
9679 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9680 "Remove the mark overlays between BEG and END in the agenda buffer.
9681 BEG and END default to the buffer limits.
9683 This only removes the overlays, it does not remove the markers
9684 from the list in `org-agenda-bulk-marked-entries'."
9685 (interactive)
9686 (mapc (lambda (ov)
9687 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9688 (delete-overlay ov)))
9689 (overlays-in (or beg (point-min)) (or end (point-max)))))
9691 (defun org-agenda-bulk-unmark-all ()
9692 "Remove all marks in the agenda buffer.
9693 This will remove the markers and the overlays."
9694 (interactive)
9695 (if (null org-agenda-bulk-marked-entries)
9696 (message "No entry to unmark")
9697 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9698 (setq org-agenda-bulk-marked-entries nil)
9699 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9701 (defcustom org-agenda-persistent-marks nil
9702 "Non-nil means marked items will stay marked after a bulk action.
9703 You can toggle this interactively by typing `p' when prompted for a
9704 bulk action."
9705 :group 'org-agenda
9706 :version "24.1"
9707 :type 'boolean)
9709 (defun org-agenda-bulk-action (&optional arg)
9710 "Execute an remote-editing action on all marked entries.
9711 The prefix arg is passed through to the command if possible."
9712 (interactive "P")
9713 ;; Make sure we have markers, and only valid ones
9714 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9715 (mapc
9716 (lambda (m)
9717 (unless (and (markerp m)
9718 (marker-buffer m)
9719 (buffer-live-p (marker-buffer m))
9720 (marker-position m))
9721 (user-error "Marker %s for bulk command is invalid" m)))
9722 org-agenda-bulk-marked-entries)
9724 ;; Prompt for the bulk command
9725 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9726 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9727 "[S]catter [f]unction "
9728 (when org-agenda-bulk-custom-functions
9729 (concat " Custom: ["
9730 (mapconcat (lambda(f) (char-to-string (car f)))
9731 org-agenda-bulk-custom-functions "")
9732 "]"))))
9733 (catch 'exit
9734 (let* ((action (read-char-exclusive))
9735 (org-log-refile (if org-log-refile 'time nil))
9736 (entries (reverse org-agenda-bulk-marked-entries))
9737 (org-overriding-default-time
9738 (if (get-text-property (point) 'org-agenda-date-header)
9739 (org-get-cursor-date)))
9740 redo-at-end
9741 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9742 (cond
9743 ((equal action ?p)
9744 (let ((org-agenda-persistent-marks
9745 (not org-agenda-persistent-marks)))
9746 (org-agenda-bulk-action)
9747 (throw 'exit nil)))
9749 ((equal action ?$)
9750 (setq cmd '(org-agenda-archive)))
9752 ((equal action ?A)
9753 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9755 ((member action '(?r ?w))
9756 (setq rfloc (org-refile-get-location
9757 "Refile to"
9758 (marker-buffer (car entries))
9759 org-refile-allow-creating-parent-nodes))
9760 (if (nth 3 rfloc)
9761 (setcar (nthcdr 3 rfloc)
9762 (move-marker (make-marker) (nth 3 rfloc)
9763 (or (get-file-buffer (nth 1 rfloc))
9764 (find-buffer-visiting (nth 1 rfloc))
9765 (error "This should not happen")))))
9767 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9768 redo-at-end t))
9770 ((equal action ?t)
9771 (setq state (org-icompleting-read
9772 "Todo state: "
9773 (with-current-buffer (marker-buffer (car entries))
9774 (mapcar 'list org-todo-keywords-1))))
9775 (setq cmd `(let ((org-inhibit-blocking t)
9776 (org-inhibit-logging 'note))
9777 (org-agenda-todo ,state))))
9779 ((memq action '(?- ?+))
9780 (setq tag (org-icompleting-read
9781 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9782 (with-current-buffer (marker-buffer (car entries))
9783 (delq nil
9784 (mapcar (lambda (x)
9785 (if (stringp (car x)) x)) org-tag-alist)))))
9786 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9788 ((memq action '(?s ?d))
9789 (let* ((time
9790 (unless arg
9791 (org-read-date
9792 nil nil nil
9793 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9794 org-overriding-default-time)))
9795 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9796 (setq cmd `(eval '(,c1 arg ,time)))))
9798 ((equal action ?S)
9799 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9800 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9801 (let ((days (read-number
9802 (format "Scatter tasks across how many %sdays: "
9803 (if arg "week" "")) 7)))
9804 (setq cmd
9805 `(let ((distance (1+ (random ,days))))
9806 (if arg
9807 (let ((dist distance)
9808 (day-of-week
9809 (calendar-day-of-week
9810 (calendar-gregorian-from-absolute (org-today)))))
9811 (dotimes (i (1+ dist))
9812 (while (member day-of-week org-agenda-weekend-days)
9813 (incf distance)
9814 (incf day-of-week)
9815 (if (= day-of-week 7)
9816 (setq day-of-week 0)))
9817 (incf day-of-week)
9818 (if (= day-of-week 7)
9819 (setq day-of-week 0)))))
9820 ;; silently fail when try to replan a sexp entry
9821 (condition-case nil
9822 (let* ((date (calendar-gregorian-from-absolute
9823 (+ (org-today) distance)))
9824 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9825 (nth 2 date))))
9826 (org-agenda-schedule nil time))
9827 (error nil)))))))
9829 ((assoc action org-agenda-bulk-custom-functions)
9830 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9831 redo-at-end t))
9833 ((equal action ?f)
9834 (setq cmd (list (intern
9835 (org-icompleting-read "Function: "
9836 obarray 'fboundp t nil nil)))))
9838 (t (user-error "Invalid bulk action")))
9840 ;; Sort the markers, to make sure that parents are handled before children
9841 (setq entries (sort entries
9842 (lambda (a b)
9843 (cond
9844 ((equal (marker-buffer a) (marker-buffer b))
9845 (< (marker-position a) (marker-position b)))
9847 (string< (buffer-name (marker-buffer a))
9848 (buffer-name (marker-buffer b))))))))
9850 ;; Now loop over all markers and apply cmd
9851 (while (setq e (pop entries))
9852 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9853 (if (not pos)
9854 (progn (message "Skipping removed entry at %s" e)
9855 (setq cntskip (1+ cntskip)))
9856 (goto-char pos)
9857 (let (org-loop-over-headlines-in-active-region)
9858 (eval cmd))
9859 (setq cnt (1+ cnt))))
9860 (when redo-at-end (org-agenda-redo))
9861 (unless org-agenda-persistent-marks
9862 (org-agenda-bulk-unmark-all))
9863 (message "Acted on %d entries%s%s"
9865 (if (= cntskip 0)
9867 (format ", skipped %d (disappeared before their turn)"
9868 cntskip))
9869 (if (not org-agenda-persistent-marks)
9870 "" " (kept marked)"))))))
9872 (defun org-agenda-capture (&optional with-time)
9873 "Call `org-capture' with the date at point.
9874 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9875 current HH:MM time."
9876 (interactive "P")
9877 (if (not (eq major-mode 'org-agenda-mode))
9878 (user-error "You cannot do this outside of agenda buffers")
9879 (let ((org-overriding-default-time
9880 (org-get-cursor-date (equal with-time 1))))
9881 (call-interactively 'org-capture))))
9883 ;;; Flagging notes
9885 (defun org-agenda-show-the-flagging-note ()
9886 "Display the flagging note in the other window.
9887 When called a second time in direct sequence, offer to remove the FLAGGING
9888 tag and (if present) the flagging note."
9889 (interactive)
9890 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9891 (win (selected-window))
9892 note heading newhead)
9893 (unless hdmarker
9894 (user-error "No linked entry at point"))
9895 (if (and (eq this-command last-command)
9896 (y-or-n-p "Unflag and remove any flagging note? "))
9897 (progn
9898 (org-agenda-remove-flag hdmarker)
9899 (let ((win (get-buffer-window "*Flagging Note*")))
9900 (and win (delete-window win)))
9901 (message "Entry unflagged"))
9902 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9903 (unless note
9904 (user-error "No flagging note"))
9905 (org-kill-new note)
9906 (org-switch-to-buffer-other-window "*Flagging Note*")
9907 (erase-buffer)
9908 (insert note)
9909 (goto-char (point-min))
9910 (while (re-search-forward "\\\\n" nil t)
9911 (replace-match "\n" t t))
9912 (goto-char (point-min))
9913 (select-window win)
9914 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9916 (defun org-agenda-remove-flag (marker)
9917 "Remove the FLAGGED tag and any flagging note in the entry."
9918 (let (newhead)
9919 (org-with-point-at marker
9920 (org-toggle-tag "FLAGGED" 'off)
9921 (org-entry-delete nil "THEFLAGGINGNOTE")
9922 (setq newhead (org-get-heading)))
9923 (org-agenda-change-all-lines newhead marker)
9924 (message "Entry unflagged")))
9926 (defun org-agenda-get-any-marker (&optional pos)
9927 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9928 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9930 ;;; Appointment reminders
9932 (defvar appt-time-msg-list) ; defined in appt.el
9934 ;;;###autoload
9935 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9936 "Activate appointments found in `org-agenda-files'.
9937 With a \\[universal-argument] prefix, refresh the list of
9938 appointments.
9940 If FILTER is t, interactively prompt the user for a regular
9941 expression, and filter out entries that don't match it.
9943 If FILTER is a string, use this string as a regular expression
9944 for filtering entries out.
9946 If FILTER is a function, filter out entries against which
9947 calling the function returns nil. This function takes one
9948 argument: an entry from `org-agenda-get-day-entries'.
9950 FILTER can also be an alist with the car of each cell being
9951 either 'headline or 'category. For example:
9953 '((headline \"IMPORTANT\")
9954 (category \"Work\"))
9956 will only add headlines containing IMPORTANT or headlines
9957 belonging to the \"Work\" category.
9959 ARGS are symbols indicating what kind of entries to consider.
9960 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
9961 \(i.e., deadlines and scheduled items with a hh:mm specification)
9962 and :timestamp entries. See the docstring of `org-diary' for
9963 details and examples.
9965 If an entry has a APPT_WARNTIME property, its value will be used
9966 to override `appt-message-warning-time'."
9967 (interactive "P")
9968 (if refresh (setq appt-time-msg-list nil))
9969 (if (eq filter t)
9970 (setq filter (read-from-minibuffer "Regexp filter: ")))
9971 (let* ((cnt 0) ; count added events
9972 (scope (or args '(:deadline* :scheduled* :timestamp)))
9973 (org-agenda-new-buffers nil)
9974 (org-deadline-warning-days 0)
9975 ;; Do not use `org-today' here because appt only takes
9976 ;; time and without date as argument, so it may pass wrong
9977 ;; information otherwise
9978 (today (org-date-to-gregorian
9979 (time-to-days (current-time))))
9980 (org-agenda-restrict nil)
9981 (files (org-agenda-files 'unrestricted)) entries file
9982 (org-agenda-buffer nil))
9983 ;; Get all entries which may contain an appt
9984 (org-agenda-prepare-buffers files)
9985 (while (setq file (pop files))
9986 (setq entries
9987 (delq nil
9988 (append entries
9989 (apply 'org-agenda-get-day-entries
9990 file today scope)))))
9991 ;; Map thru entries and find if we should filter them out
9992 (mapc
9993 (lambda(x)
9994 (let* ((evt (org-trim
9995 (replace-regexp-in-string
9996 org-bracket-link-regexp "\\3"
9997 (or (get-text-property 1 'txt x) ""))))
9998 (cat (get-text-property 1 'org-category x))
9999 (tod (get-text-property 1 'time-of-day x))
10000 (ok (or (null filter)
10001 (and (stringp filter) (string-match filter evt))
10002 (and (functionp filter) (funcall filter x))
10003 (and (listp filter)
10004 (let ((cat-filter (cadr (assoc 'category filter)))
10005 (evt-filter (cadr (assoc 'headline filter))))
10006 (or (and (stringp cat-filter)
10007 (string-match cat-filter cat))
10008 (and (stringp evt-filter)
10009 (string-match evt-filter evt)))))))
10010 (wrn (get-text-property 1 'warntime x)))
10011 ;; FIXME: Shall we remove text-properties for the appt text?
10012 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10013 (when (and ok tod)
10014 (setq tod (concat "00" (number-to-string tod))
10015 tod (when (string-match
10016 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10017 (concat (match-string 1 tod) ":"
10018 (match-string 2 tod))))
10019 (if (version< emacs-version "23.3")
10020 (appt-add tod evt)
10021 (appt-add tod evt wrn))
10022 (setq cnt (1+ cnt))))) entries)
10023 (org-release-buffers org-agenda-new-buffers)
10024 (if (eq cnt 0)
10025 (message "No event to add")
10026 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10028 (defun org-agenda-todayp (date)
10029 "Does DATE mean today, when considering `org-extend-today-until'?"
10030 (let ((today (org-today))
10031 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10032 date)))
10033 (eq date today)))
10035 (defun org-agenda-todo-yesterday (&optional arg)
10036 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10037 (interactive "P")
10038 (let* ((hour (third (decode-time
10039 (org-current-time))))
10040 (org-extend-today-until (1+ hour)))
10041 (org-agenda-todo arg)))
10043 (provide 'org-agenda)
10045 ;;; org-agenda.el ends here