Fix q key in sticky agenda not honoring org-agenda-restore-windows-after-quit
[org-mode.git] / lisp / org-agenda.el
blobb35829c3e03e38c51d66b493016c1e9fc7e0b75f
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2012 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 (eval-when-compile
50 (require 'cl))
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-absolute-from-iso "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-datetree-find-date-create "org-datetree"
72 (date &optional keep-restriction))
73 (declare-function org-columns-quit "org-colview" ())
74 (declare-function diary-date-display-form "diary-lib" (&optional type))
75 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
76 (declare-function org-habit-insert-consistency-graphs
77 "org-habit" (&optional line))
78 (declare-function org-is-habit-p "org-habit" (&optional pom))
79 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
80 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
81 (declare-function org-pop-to-buffer-same-window "org-compat"
82 (&optional buffer-or-name norecord label))
84 (defvar calendar-mode-map)
85 (defvar org-clock-current-task) ; defined in org-clock.el
86 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
87 (defvar org-habit-show-habits)
88 (defvar org-habit-show-habits-only-for-today)
90 ;; Defined somewhere in this file, but used before definition.
91 (defvar org-agenda-buffer-name)
92 (defvar org-agenda-overriding-header)
93 (defvar org-agenda-title-append nil)
94 (defvar entry)
95 (defvar date)
96 (defvar org-agenda-undo-list)
97 (defvar org-agenda-pending-undo-list)
98 (defvar original-date) ; dynamically scoped, calendar.el does scope this
100 (defcustom org-agenda-confirm-kill 1
101 "When set, remote killing from the agenda buffer needs confirmation.
102 When t, a confirmation is always needed. When a number N, confirmation is
103 only needed when the text to be killed contains more than N non-white lines."
104 :group 'org-agenda
105 :type '(choice
106 (const :tag "Never" nil)
107 (const :tag "Always" t)
108 (integer :tag "When more than N lines")))
110 (defcustom org-agenda-compact-blocks nil
111 "Non-nil means make the block agenda more compact.
112 This is done globally by leaving out lines like the agenda span
113 name and week number or the separator lines."
114 :group 'org-agenda
115 :type 'boolean)
117 (defcustom org-agenda-block-separator ?=
118 "The separator between blocks in the agenda.
119 If this is a string, it will be used as the separator, with a newline added.
120 If it is a character, it will be repeated to fill the window width.
121 If nil the separator is disabled. In `org-agenda-custom-commands' this
122 addresses the separator between the current and the previous block."
123 :group 'org-agenda
124 :type '(choice
125 (const :tag "Disabled" nil)
126 (character)
127 (string)))
129 (defgroup org-agenda-export nil
130 "Options concerning exporting agenda views in Org-mode."
131 :tag "Org Agenda Export"
132 :group 'org-agenda)
134 (defcustom org-agenda-with-colors t
135 "Non-nil means use colors in agenda views."
136 :group 'org-agenda-export
137 :type 'boolean)
139 (defcustom org-agenda-exporter-settings nil
140 "Alist of variable/value pairs that should be active during agenda export.
141 This is a good place to set options for ps-print and for htmlize.
142 Note that the way this is implemented, the values will be evaluated
143 before assigned to the variables. So make sure to quote values you do
144 *not* want evaluated, for example
146 (setq org-agenda-exporter-settings
147 '((ps-print-color-p 'black-white)))"
148 :group 'org-agenda-export
149 :type '(repeat
150 (list
151 (variable)
152 (sexp :tag "Value"))))
154 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
155 "Hook run in temporary buffer before writing it to an export file.
156 A useful function is `org-agenda-add-entry-text'."
157 :group 'org-agenda-export
158 :type 'hook
159 :options '(org-agenda-add-entry-text))
161 (defcustom org-agenda-add-entry-text-maxlines 0
162 "Maximum number of entry text lines to be added to agenda.
163 This is only relevant when `org-agenda-add-entry-text' is part of
164 `org-agenda-before-write-hook', which it is by default.
165 When this is 0, nothing will happen. When it is greater than 0, it
166 specifies the maximum number of lines that will be added for each entry
167 that is listed in the agenda view.
169 Note that this variable is not used during display, only when exporting
170 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
171 and `org-agenda-entry-text-maxlines'."
172 :group 'org-agenda
173 :type 'integer)
175 (defcustom org-agenda-add-entry-text-descriptive-links t
176 "Non-nil means export org-links as descriptive links in agenda added text.
177 This variable applies to the text added to the agenda when
178 `org-agenda-add-entry-text-maxlines' is larger than 0.
179 When this variable nil, the URL will (also) be shown."
180 :group 'org-agenda
181 :type 'boolean)
183 (defcustom org-agenda-export-html-style ""
184 "The style specification for exported HTML Agenda files.
185 If this variable contains a string, it will replace the default <style>
186 section as produced by `htmlize'.
187 Since there are different ways of setting style information, this variable
188 needs to contain the full HTML structure to provide a style, including the
189 surrounding HTML tags. The style specifications should include definitions
190 the fonts used by the agenda, here is an example:
192 <style type=\"text/css\">
193 p { font-weight: normal; color: gray; }
194 .org-agenda-structure {
195 font-size: 110%;
196 color: #003399;
197 font-weight: 600;
199 .org-todo {
200 color: #cc6666;
201 font-weight: bold;
203 .org-agenda-done {
204 color: #339933;
206 .org-done {
207 color: #339933;
209 .title { text-align: center; }
210 .todo, .deadline { color: red; }
211 .done { color: green; }
212 </style>
214 or, if you want to keep the style in a file,
216 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
218 As the value of this option simply gets inserted into the HTML <head> header,
219 you can \"misuse\" it to also add other text to the header. However,
220 <style>...</style> is required, if not present the variable will be ignored."
221 :group 'org-agenda-export
222 :group 'org-export-html
223 :type 'string)
225 (defcustom org-agenda-persistent-filter nil
226 "When set, keep filters from one agenda view to the next."
227 :group 'org-agenda
228 :type 'boolean)
230 (defgroup org-agenda-custom-commands nil
231 "Options concerning agenda views in Org-mode."
232 :tag "Org Agenda Custom Commands"
233 :group 'org-agenda)
235 (defconst org-sorting-choice
236 '(choice
237 (const time-up) (const time-down)
238 (const category-keep) (const category-up) (const category-down)
239 (const tag-down) (const tag-up)
240 (const priority-up) (const priority-down)
241 (const todo-state-up) (const todo-state-down)
242 (const effort-up) (const effort-down)
243 (const habit-up) (const habit-down)
244 (const alpha-up) (const alpha-down)
245 (const user-defined-up) (const user-defined-down))
246 "Sorting choices.")
248 ;; Keep custom values for `org-agenda-filter-preset' compatible with
249 ;; the new variable `org-agenda-tag-filter-preset'.
250 (if (fboundp 'defvaralias)
251 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
252 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter))
254 (defconst org-agenda-custom-commands-local-options
255 `(repeat :tag "Local settings for this command. Remember to quote values"
256 (choice :tag "Setting"
257 (list :tag "Heading for this block"
258 (const org-agenda-overriding-header)
259 (string :tag "Headline"))
260 (list :tag "Files to be searched"
261 (const org-agenda-files)
262 (list
263 (const :format "" quote)
264 (repeat (file))))
265 (list :tag "Sorting strategy"
266 (const org-agenda-sorting-strategy)
267 (list
268 (const :format "" quote)
269 (repeat
270 ,org-sorting-choice)))
271 (list :tag "Prefix format"
272 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
273 (string))
274 (list :tag "Number of days in agenda"
275 (const org-agenda-span)
276 (choice (const :tag "Day" 'day)
277 (const :tag "Week" 'week)
278 (const :tag "Month" 'month)
279 (const :tag "Year" 'year)
280 (integer :tag "Custom")))
281 (list :tag "Fixed starting date"
282 (const org-agenda-start-day)
283 (string :value "2007-11-01"))
284 (list :tag "Start on day of week"
285 (const org-agenda-start-on-weekday)
286 (choice :value 1
287 (const :tag "Today" nil)
288 (integer :tag "Weekday No.")))
289 (list :tag "Include data from diary"
290 (const org-agenda-include-diary)
291 (boolean))
292 (list :tag "Deadline Warning days"
293 (const org-deadline-warning-days)
294 (integer :value 1))
295 (list :tag "Category filter preset"
296 (const org-agenda-category-filter-preset)
297 (list
298 (const :format "" quote)
299 (repeat
300 (string :tag "+category or -category"))))
301 (list :tag "Tags filter preset"
302 (const org-agenda-tag-filter-preset)
303 (list
304 (const :format "" quote)
305 (repeat
306 (string :tag "+tag or -tag"))))
307 (list :tag "Set daily/weekly entry types"
308 (const org-agenda-entry-types)
309 (list
310 (const :format "" quote)
311 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
312 (const :deadline)
313 (const :scheduled)
314 (const :timestamp)
315 (const :sexp))))
316 (list :tag "Standard skipping condition"
317 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
318 (const org-agenda-skip-function)
319 (list
320 (const :format "" quote)
321 (list
322 (choice
323 :tag "Skipping range"
324 (const :tag "Skip entry" org-agenda-skip-entry-if)
325 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
326 (repeat :inline t :tag "Conditions for skipping"
327 (choice
328 :tag "Condition type"
329 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
330 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
331 (list :tag "TODO state is" :inline t
332 (const 'todo)
333 (choice
334 (const :tag "any not-done state" 'todo)
335 (const :tag "any done state" 'done)
336 (const :tag "any state" 'any)
337 (list :tag "Keyword list"
338 (const :format "" quote)
339 (repeat (string :tag "Keyword")))))
340 (list :tag "TODO state is not" :inline t
341 (const 'nottodo)
342 (choice
343 (const :tag "any not-done state" 'todo)
344 (const :tag "any done state" 'done)
345 (const :tag "any state" 'any)
346 (list :tag "Keyword list"
347 (const :format "" quote)
348 (repeat (string :tag "Keyword")))))
349 (const :tag "scheduled" 'scheduled)
350 (const :tag "not scheduled" 'notscheduled)
351 (const :tag "deadline" 'deadline)
352 (const :tag "no deadline" 'notdeadline)
353 (const :tag "timestamp" 'timestamp)
354 (const :tag "no timestamp" 'nottimestamp))))))
355 (list :tag "Non-standard skipping condition"
356 :value (org-agenda-skip-function)
357 (const org-agenda-skip-function)
358 (sexp :tag "Function or form (quoted!)"))
359 (list :tag "Any variable"
360 (variable :tag "Variable")
361 (sexp :tag "Value (sexp)"))))
362 "Selection of examples for agenda command settings.
363 This will be spliced into the custom type of
364 `org-agenda-custom-commands'.")
367 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
368 ((agenda "") (alltodo))))
369 "Custom commands for the agenda.
370 These commands will be offered on the splash screen displayed by the
371 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
373 (key desc type match settings files)
375 key The key (one or more characters as a string) to be associated
376 with the command.
377 desc A description of the command, when omitted or nil, a default
378 description is built using MATCH.
379 type The command type, any of the following symbols:
380 agenda The daily/weekly agenda.
381 todo Entries with a specific TODO keyword, in all agenda files.
382 search Entries containing search words entry or headline.
383 tags Tags/Property/TODO match in all agenda files.
384 tags-todo Tags/P/T match in all agenda files, TODO entries only.
385 todo-tree Sparse tree of specific TODO keyword in *current* file.
386 tags-tree Sparse tree with all tags matches in *current* file.
387 occur-tree Occur sparse tree for *current* file.
388 ... A user-defined function.
389 match What to search for:
390 - a single keyword for TODO keyword searches
391 - a tags match expression for tags searches
392 - a word search expression for text searches.
393 - a regular expression for occur searches
394 For all other commands, this should be the empty string.
395 settings A list of option settings, similar to that in a let form, so like
396 this: ((opt1 val1) (opt2 val2) ...). The values will be
397 evaluated at the moment of execution, so quote them when needed.
398 files A list of files file to write the produced agenda buffer to
399 with the command `org-store-agenda-views'.
400 If a file name ends in \".html\", an HTML version of the buffer
401 is written out. If it ends in \".ps\", a postscript version is
402 produced. Otherwise, only the plain text is written to the file.
404 You can also define a set of commands, to create a composite agenda buffer.
405 In this case, an entry looks like this:
407 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
409 where
411 desc A description string to be displayed in the dispatcher menu.
412 cmd An agenda command, similar to the above. However, tree commands
413 are not allowed, but instead you can get agenda and global todo list.
414 So valid commands for a set are:
415 (agenda \"\" settings)
416 (alltodo \"\" settings)
417 (stuck \"\" settings)
418 (todo \"match\" settings files)
419 (search \"match\" settings files)
420 (tags \"match\" settings files)
421 (tags-todo \"match\" settings files)
423 Each command can carry a list of options, and another set of options can be
424 given for the whole set of commands. Individual command options take
425 precedence over the general options.
427 When using several characters as key to a command, the first characters
428 are prefix commands. For the dispatcher to display useful information, you
429 should provide a description for the prefix, like
431 (setq org-agenda-custom-commands
432 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
433 (\"hl\" tags \"+HOME+Lisa\")
434 (\"hp\" tags \"+HOME+Peter\")
435 (\"hk\" tags \"+HOME+Kim\")))"
436 :group 'org-agenda-custom-commands
437 :type `(repeat
438 (choice :value ("x" "Describe command here" tags "" nil)
439 (list :tag "Single command"
440 (string :tag "Access Key(s) ")
441 (option (string :tag "Description"))
442 (choice
443 (const :tag "Agenda" agenda)
444 (const :tag "TODO list" alltodo)
445 (const :tag "Search words" search)
446 (const :tag "Stuck projects" stuck)
447 (const :tag "Tags/Property match (all agenda files)" tags)
448 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
449 (const :tag "TODO keyword search (all agenda files)" todo)
450 (const :tag "Tags sparse tree (current buffer)" tags-tree)
451 (const :tag "TODO keyword tree (current buffer)" todo-tree)
452 (const :tag "Occur tree (current buffer)" occur-tree)
453 (sexp :tag "Other, user-defined function"))
454 (string :tag "Match (only for some commands)")
455 ,org-agenda-custom-commands-local-options
456 (option (repeat :tag "Export" (file :tag "Export to"))))
457 (list :tag "Command series, all agenda files"
458 (string :tag "Access Key(s)")
459 (string :tag "Description ")
460 (repeat :tag "Component"
461 (choice
462 (list :tag "Agenda"
463 (const :format "" agenda)
464 (const :tag "" :format "" "")
465 ,org-agenda-custom-commands-local-options)
466 (list :tag "TODO list (all keywords)"
467 (const :format "" alltodo)
468 (const :tag "" :format "" "")
469 ,org-agenda-custom-commands-local-options)
470 (list :tag "Search words"
471 (const :format "" search)
472 (string :tag "Match")
473 ,org-agenda-custom-commands-local-options)
474 (list :tag "Stuck projects"
475 (const :format "" stuck)
476 (const :tag "" :format "" "")
477 ,org-agenda-custom-commands-local-options)
478 (list :tag "Tags search"
479 (const :format "" tags)
480 (string :tag "Match")
481 ,org-agenda-custom-commands-local-options)
482 (list :tag "Tags search, TODO entries only"
483 (const :format "" tags-todo)
484 (string :tag "Match")
485 ,org-agenda-custom-commands-local-options)
486 (list :tag "TODO keyword search"
487 (const :format "" todo)
488 (string :tag "Match")
489 ,org-agenda-custom-commands-local-options)
490 (list :tag "Other, user-defined function"
491 (symbol :tag "function")
492 (string :tag "Match")
493 ,org-agenda-custom-commands-local-options)))
495 (repeat :tag "Settings for entire command set"
496 (list (variable :tag "Any variable")
497 (sexp :tag "Value")))
498 (option (repeat :tag "Export" (file :tag "Export to"))))
499 (cons :tag "Prefix key documentation"
500 (string :tag "Access Key(s)")
501 (string :tag "Description ")))))
503 (defcustom org-agenda-query-register ?o
504 "The register holding the current query string.
505 The purpose of this is that if you construct a query string interactively,
506 you can then use it to define a custom command."
507 :group 'org-agenda-custom-commands
508 :type 'character)
510 (defcustom org-stuck-projects
511 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
512 "How to identify stuck projects.
513 This is a list of four items:
514 1. A tags/todo/property matcher string that is used to identify a project.
515 See the manual for a description of tag and property searches.
516 The entire tree below a headline matched by this is considered one project.
517 2. A list of TODO keywords identifying non-stuck projects.
518 If the project subtree contains any headline with one of these todo
519 keywords, the project is considered to be not stuck. If you specify
520 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
521 3. A list of tags identifying non-stuck projects.
522 If the project subtree contains any headline with one of these tags,
523 the project is considered to be not stuck. If you specify \"*\" as
524 a tag, any tag will mark the project unstuck. Note that this is about
525 the explicit presence of a tag somewhere in the subtree, inherited
526 tags to not count here. If inherited tags make a project not stuck,
527 use \"-TAG\" in the tags part of the matcher under (1.) above.
528 4. An arbitrary regular expression matching non-stuck projects.
530 If the project turns out to be not stuck, search continues also in the
531 subtree to see if any of the subtasks have project status.
533 See also the variable `org-tags-match-list-sublevels' which applies
534 to projects matched by this search as well.
536 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
537 or `C-c a #' to produce the list."
538 :group 'org-agenda-custom-commands
539 :type '(list
540 (string :tag "Tags/TODO match to identify a project")
541 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
542 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
543 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
545 (defcustom org-agenda-filter-effort-default-operator "<"
546 "The default operator for effort estimate filtering.
547 If you select an effort estimate limit without first pressing an operator,
548 this one will be used."
549 :group 'org-agenda-custom-commands
550 :type '(choice (const :tag "less or equal" "<")
551 (const :tag "greater or equal"">")
552 (const :tag "equal" "=")))
554 (defgroup org-agenda-skip nil
555 "Options concerning skipping parts of agenda files."
556 :tag "Org Agenda Skip"
557 :group 'org-agenda)
559 (defcustom org-agenda-skip-function-global nil
560 "Function to be called at each match during agenda construction.
561 If this function returns nil, the current match should not be skipped.
562 If the function decided to skip an agenda match, is must return the
563 buffer position from which the search should be continued.
564 This may also be a Lisp form, which will be evaluated.
566 This variable will be applied to every agenda match, including
567 tags/property searches and TODO lists. So try to make the test function
568 do its checking as efficiently as possible. To implement a skipping
569 condition just for specific agenda commands, use the variable
570 `org-agenda-skip-function' which can be set in the options section
571 of custom agenda commands."
572 :group 'org-agenda-skip
573 :type 'sexp)
575 (defgroup org-agenda-daily/weekly nil
576 "Options concerning the daily/weekly agenda."
577 :tag "Org Agenda Daily/Weekly"
578 :group 'org-agenda)
579 (defgroup org-agenda-todo-list nil
580 "Options concerning the global todo list agenda view."
581 :tag "Org Agenda Todo List"
582 :group 'org-agenda)
583 (defgroup org-agenda-match-view nil
584 "Options concerning the general tags/property/todo match agenda view."
585 :tag "Org Agenda Match View"
586 :group 'org-agenda)
587 (defgroup org-agenda-search-view nil
588 "Options concerning the general tags/property/todo match agenda view."
589 :tag "Org Agenda Match View"
590 :group 'org-agenda)
592 (defvar org-agenda-archives-mode nil
593 "Non-nil means the agenda will include archived items.
594 If this is the symbol `trees', trees in the selected agenda scope
595 that are marked with the ARCHIVE tag will be included anyway. When this is
596 t, also all archive files associated with the current selection of agenda
597 files will be included.")
599 (defcustom org-agenda-skip-comment-trees t
600 "Non-nil means skip trees that start with the COMMENT keyword.
601 When nil, these trees are also scanned by agenda commands."
602 :group 'org-agenda-skip
603 :type 'boolean)
605 (defcustom org-agenda-todo-list-sublevels t
606 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
607 When nil, the sublevels of a TODO entry are not checked, resulting in
608 potentially much shorter TODO lists."
609 :group 'org-agenda-skip
610 :group 'org-agenda-todo-list
611 :type 'boolean)
613 (defcustom org-agenda-todo-ignore-with-date nil
614 "Non-nil means don't show entries with a date in the global todo list.
615 You can use this if you prefer to mark mere appointments with a TODO keyword,
616 but don't want them to show up in the TODO list.
617 When this is set, it also covers deadlines and scheduled items, the settings
618 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
619 will be ignored.
620 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
621 :group 'org-agenda-skip
622 :group 'org-agenda-todo-list
623 :type 'boolean)
625 (defcustom org-agenda-todo-ignore-timestamp nil
626 "Non-nil means don't show entries with a timestamp.
627 This applies when creating the global todo list.
628 Valid values are:
630 past Don't show entries for today or in the past.
632 future Don't show entries with a timestamp in the future.
633 The idea behind this is that if it has a future
634 timestamp, you don't want to think about it until the
635 date.
637 all Don't show any entries with a timestamp in the global todo list.
638 The idea behind this is that by setting a timestamp, you
639 have already \"taken care\" of this item.
641 This variable can also have an integer as a value. If positive (N),
642 todos with a timestamp N or more days in the future will be ignored. If
643 negative (-N), todos with a timestamp N or more days in the past will be
644 ignored. If 0, todos with a timestamp either today or in the future will
645 be ignored. For example, a value of -1 will exclude todos with a
646 timestamp in the past (yesterday or earlier), while a value of 7 will
647 exclude todos with a timestamp a week or more in the future.
649 See also `org-agenda-todo-ignore-with-date'.
650 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
651 to make his option also apply to the tags-todo list."
652 :group 'org-agenda-skip
653 :group 'org-agenda-todo-list
654 :version "24.1"
655 :type '(choice
656 (const :tag "Ignore future timestamp todos" future)
657 (const :tag "Ignore past or present timestamp todos" past)
658 (const :tag "Ignore all timestamp todos" all)
659 (const :tag "Show timestamp todos" nil)
660 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
662 (defcustom org-agenda-todo-ignore-scheduled nil
663 "Non-nil means, ignore some scheduled TODO items when making TODO list.
664 This applies when creating the global todo list.
665 Valid values are:
667 past Don't show entries scheduled today or in the past.
669 future Don't show entries scheduled in the future.
670 The idea behind this is that by scheduling it, you don't want to
671 think about it until the scheduled date.
673 all Don't show any scheduled entries in the global todo list.
674 The idea behind this is that by scheduling it, you have already
675 \"taken care\" of this item.
677 t Same as `all', for backward compatibility.
679 This variable can also have an integer as a value. See
680 `org-agenda-todo-ignore-timestamp' for more details.
682 See also `org-agenda-todo-ignore-with-date'.
683 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
684 to make his option also apply to the tags-todo list."
685 :group 'org-agenda-skip
686 :group 'org-agenda-todo-list
687 :type '(choice
688 (const :tag "Ignore future-scheduled todos" future)
689 (const :tag "Ignore past- or present-scheduled todos" past)
690 (const :tag "Ignore all scheduled todos" all)
691 (const :tag "Ignore all scheduled todos (compatibility)" t)
692 (const :tag "Show scheduled todos" nil)
693 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
695 (defcustom org-agenda-todo-ignore-deadlines nil
696 "Non-nil means ignore some deadlined TODO items when making TODO list.
697 There are different motivations for using different values, please think
698 carefully when configuring this variable.
700 This applies when creating the global todo list.
701 Valid values are:
703 near Don't show near deadline entries. A deadline is near when it is
704 closer than `org-deadline-warning-days' days. The idea behind this
705 is that such items will appear in the agenda anyway.
707 far Don't show TODO entries where a deadline has been defined, but
708 the deadline is not near. This is useful if you don't want to
709 use the todo list to figure out what to do now.
711 past Don't show entries with a deadline timestamp for today or in the past.
713 future Don't show entries with a deadline timestamp in the future, not even
714 when they become `near' ones. Use it with caution.
716 all Ignore all TODO entries that do have a deadline.
718 t Same as `near', for backward compatibility.
720 This variable can also have an integer as a value. See
721 `org-agenda-todo-ignore-timestamp' for more details.
723 See also `org-agenda-todo-ignore-with-date'.
724 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
725 to make his option also apply to the tags-todo list."
726 :group 'org-agenda-skip
727 :group 'org-agenda-todo-list
728 :type '(choice
729 (const :tag "Ignore near deadlines" near)
730 (const :tag "Ignore near deadlines (compatibility)" t)
731 (const :tag "Ignore far deadlines" far)
732 (const :tag "Ignore all TODOs with a deadlines" all)
733 (const :tag "Show all TODOs, even if they have a deadline" nil)
734 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
736 (defcustom org-agenda-tags-todo-honor-ignore-options nil
737 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
738 The variables
739 `org-agenda-todo-ignore-with-date',
740 `org-agenda-todo-ignore-timestamp',
741 `org-agenda-todo-ignore-scheduled',
742 `org-agenda-todo-ignore-deadlines'
743 make the global TODO list skip entries that have time stamps of certain
744 kinds. If this option is set, the same options will also apply for the
745 tags-todo search, which is the general tags/property matcher
746 restricted to unfinished TODO entries only."
747 :group 'org-agenda-skip
748 :group 'org-agenda-todo-list
749 :group 'org-agenda-match-view
750 :type 'boolean)
752 (defcustom org-agenda-skip-scheduled-if-done nil
753 "Non-nil means don't show scheduled items in agenda when they are done.
754 This is relevant for the daily/weekly agenda, not for the TODO list. And
755 it applies only to the actual date of the scheduling. Warnings about
756 an item with a past scheduling dates are always turned off when the item
757 is DONE."
758 :group 'org-agenda-skip
759 :group 'org-agenda-daily/weekly
760 :type 'boolean)
762 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
763 "Non-nil means skip scheduling line if same entry shows because of deadline.
764 In the agenda of today, an entry can show up multiple times because
765 it is both scheduled and has a nearby deadline, and maybe a plain time
766 stamp as well.
767 When this variable is t, then only the deadline is shown and the fact that
768 the entry is scheduled today or was scheduled previously is not shown.
769 When this variable is nil, the entry will be shown several times. When
770 the variable is the symbol `not-today', then skip scheduled previously,
771 but not scheduled today."
772 :group 'org-agenda-skip
773 :group 'org-agenda-daily/weekly
774 :type '(choice
775 (const :tag "Never" nil)
776 (const :tag "Always" t)
777 (const :tag "Not when scheduled today" not-today)))
779 (defcustom org-agenda-skip-deadline-if-done nil
780 "Non-nil means don't show deadlines when the corresponding item is done.
781 When nil, the deadline is still shown and should give you a happy feeling.
782 This is relevant for the daily/weekly agenda. And it applied only to the
783 actually date of the deadline. Warnings about approaching and past-due
784 deadlines are always turned off when the item is DONE."
785 :group 'org-agenda-skip
786 :group 'org-agenda-daily/weekly
787 :type 'boolean)
789 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
790 "Non-nil means skip deadline prewarning when entry is also scheduled.
791 This will apply on all days where a prewarning for the deadline would
792 be shown, but not at the day when the entry is actually due. On that day,
793 the deadline will be shown anyway.
794 This variable may be set to nil, t, or a number which will then give
795 the number of days before the actual deadline when the prewarnings
796 should resume.
797 This can be used in a workflow where the first showing of the deadline will
798 trigger you to schedule it, and then you don't want to be reminded of it
799 because you will take care of it on the day when scheduled."
800 :group 'org-agenda-skip
801 :group 'org-agenda-daily/weekly
802 :version "24.1"
803 :type '(choice
804 (const :tag "Alwas show prewarning" nil)
805 (const :tag "Remove prewarning if entry is scheduled" t)
806 (integer :tag "Restart prewarning N days before deadline")))
808 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
809 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
810 When non-nil, after the search for timestamps has matched once in an
811 entry, the rest of the entry will not be searched."
812 :group 'org-agenda-skip
813 :type 'boolean)
815 (defcustom org-agenda-skip-timestamp-if-done nil
816 "Non-nil means don't select item by timestamp or -range if it is DONE."
817 :group 'org-agenda-skip
818 :group 'org-agenda-daily/weekly
819 :type 'boolean)
821 (defcustom org-agenda-dim-blocked-tasks t
822 "Non-nil means dim blocked tasks in the agenda display.
823 This causes some overhead during agenda construction, but if you
824 have turned on `org-enforce-todo-dependencies',
825 `org-enforce-todo-checkbox-dependencies', or any other blocking
826 mechanism, this will create useful feedback in the agenda.
828 Instead of t, this variable can also have the value `invisible'.
829 Then blocked tasks will be invisible and only become visible when
830 they become unblocked. An exemption to this behavior is when a task is
831 blocked because of unchecked checkboxes below it. Since checkboxes do
832 not show up in the agenda views, making this task invisible you remove any
833 trace from agenda views that there is something to do. Therefore, a task
834 that is blocked because of checkboxes will never be made invisible, it
835 will only be dimmed."
836 :group 'org-agenda-daily/weekly
837 :group 'org-agenda-todo-list
838 :type '(choice
839 (const :tag "Do not dim" nil)
840 (const :tag "Dim to a gray face" t)
841 (const :tag "Make invisible" invisible)))
843 (defcustom org-timeline-show-empty-dates 3
844 "Non-nil means `org-timeline' also shows dates without an entry.
845 When nil, only the days which actually have entries are shown.
846 When t, all days between the first and the last date are shown.
847 When an integer, show also empty dates, but if there is a gap of more than
848 N days, just insert a special line indicating the size of the gap."
849 :group 'org-agenda-skip
850 :type '(choice
851 (const :tag "None" nil)
852 (const :tag "All" t)
853 (integer :tag "at most")))
855 (defgroup org-agenda-startup nil
856 "Options concerning initial settings in the Agenda in Org Mode."
857 :tag "Org Agenda Startup"
858 :group 'org-agenda)
860 (defcustom org-agenda-menu-show-matcher t
861 "Non-nil means show the match string in the agenda dispatcher menu.
862 When nil, the matcher string is not shown, but is put into the help-echo
863 property so than moving the mouse over the command shows it.
864 Setting it to nil is good if matcher strings are very long and/or if
865 you want to use two-column display (see `org-agenda-menu-two-column')."
866 :group 'org-agenda
867 :version "24.1"
868 :type 'boolean)
870 (defcustom org-agenda-menu-two-column nil
871 "Non-nil means, use two columns to show custom commands in the dispatcher.
872 If you use this, you probably want to set `org-agenda-menu-show-matcher'
873 to nil."
874 :group 'org-agenda
875 :version "24.1"
876 :type 'boolean)
878 (defcustom org-finalize-agenda-hook nil
879 "Hook run just before displaying an agenda buffer."
880 :group 'org-agenda-startup
881 :type 'hook)
883 (defcustom org-agenda-mouse-1-follows-link nil
884 "Non-nil means mouse-1 on a link will follow the link in the agenda.
885 A longer mouse click will still set point. Does not work on XEmacs.
886 Needs to be set before org.el is loaded."
887 :group 'org-agenda-startup
888 :type 'boolean)
890 (defcustom org-agenda-start-with-follow-mode nil
891 "The initial value of follow mode in a newly created agenda window."
892 :group 'org-agenda-startup
893 :type 'boolean)
895 (defcustom org-agenda-follow-indirect nil
896 "Non-nil means `org-agenda-follow-mode' displays only the
897 current item's tree, in an indirect buffer."
898 :group 'org-agenda
899 :version "24.1"
900 :type 'boolean)
902 (defcustom org-agenda-show-outline-path t
903 "Non-nil means show outline path in echo area after line motion."
904 :group 'org-agenda-startup
905 :type 'boolean)
907 (defcustom org-agenda-start-with-entry-text-mode nil
908 "The initial value of entry-text-mode in a newly created agenda window."
909 :group 'org-agenda-startup
910 :type 'boolean)
912 (defcustom org-agenda-entry-text-maxlines 5
913 "Number of text lines to be added when `E' is pressed in the agenda.
915 Note that this variable only used during agenda display. Add add entry text
916 when exporting the agenda, configure the variable
917 `org-agenda-add-entry-ext-maxlines'."
918 :group 'org-agenda
919 :type 'integer)
921 (defcustom org-agenda-entry-text-exclude-regexps nil
922 "List of regular expressions to clean up entry text.
923 The complete matches of all regular expressions in this list will be
924 removed from entry text before it is shown in the agenda."
925 :group 'org-agenda
926 :type '(repeat (regexp)))
928 (defvar org-agenda-entry-text-cleanup-hook nil
929 "Hook that is run after basic cleanup of entry text to be shown in agenda.
930 This cleanup is done in a temporary buffer, so the function may inspect and
931 change the entire buffer.
932 Some default stuff like drawers and scheduling/deadline dates will already
933 have been removed when this is called, as will any matches for regular
934 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
936 (defvar org-agenda-include-inactive-timestamps nil
937 "Non-nil means include inactive time stamps in agenda and timeline.
938 Dynamically scoped.")
940 (defgroup org-agenda-windows nil
941 "Options concerning the windows used by the Agenda in Org Mode."
942 :tag "Org Agenda Windows"
943 :group 'org-agenda)
945 (defcustom org-agenda-window-setup 'reorganize-frame
946 "How the agenda buffer should be displayed.
947 Possible values for this option are:
949 current-window Show agenda in the current window, keeping all other windows.
950 other-window Use `switch-to-buffer-other-window' to display agenda.
951 reorganize-frame Show only two windows on the current frame, the current
952 window and the agenda.
953 other-frame Use `switch-to-buffer-other-frame' to display agenda.
954 Also, when exiting the agenda, kill that frame.
955 See also the variable `org-agenda-restore-windows-after-quit'."
956 :group 'org-agenda-windows
957 :type '(choice
958 (const current-window)
959 (const other-frame)
960 (const other-window)
961 (const reorganize-frame)))
963 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
964 "The min and max height of the agenda window as a fraction of frame height.
965 The value of the variable is a cons cell with two numbers between 0 and 1.
966 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
967 :group 'org-agenda-windows
968 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
970 (defcustom org-agenda-restore-windows-after-quit nil
971 "Non-nil means restore window configuration upon exiting agenda.
972 Before the window configuration is changed for displaying the agenda,
973 the current status is recorded. When the agenda is exited with
974 `q' or `x' and this option is set, the old state is restored. If
975 `org-agenda-window-setup' is `other-frame', the value of this
976 option will be ignored."
977 :group 'org-agenda-windows
978 :type 'boolean)
980 (defcustom org-agenda-ndays nil
981 "Number of days to include in overview display.
982 Should be 1 or 7.
983 Obsolete, see `org-agenda-span'."
984 :group 'org-agenda-daily/weekly
985 :type 'integer)
987 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
989 (defcustom org-agenda-span 'week
990 "Number of days to include in overview display.
991 Can be day, week, month, year, or any number of days.
992 Custom commands can set this variable in the options section."
993 :group 'org-agenda-daily/weekly
994 :type '(choice (const :tag "Day" day)
995 (const :tag "Week" week)
996 (const :tag "Month" month)
997 (const :tag "Year" year)
998 (integer :tag "Custom")))
1000 (defcustom org-agenda-start-on-weekday 1
1001 "Non-nil means start the overview always on the specified weekday.
1002 0 denotes Sunday, 1 denotes Monday etc.
1003 When nil, always start on the current day.
1004 Custom commands can set this variable in the options section."
1005 :group 'org-agenda-daily/weekly
1006 :type '(choice (const :tag "Today" nil)
1007 (integer :tag "Weekday No.")))
1009 (defcustom org-agenda-show-all-dates t
1010 "Non-nil means `org-agenda' shows every day in the selected range.
1011 When nil, only the days which actually have entries are shown."
1012 :group 'org-agenda-daily/weekly
1013 :type 'boolean)
1015 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1016 "Format string for displaying dates in the agenda.
1017 Used by the daily/weekly agenda and by the timeline. This should be
1018 a format string understood by `format-time-string', or a function returning
1019 the formatted date as a string. The function must take a single argument,
1020 a calendar-style date list like (month day year)."
1021 :group 'org-agenda-daily/weekly
1022 :type '(choice
1023 (string :tag "Format string")
1024 (function :tag "Function")))
1026 (defun org-agenda-format-date-aligned (date)
1027 "Format a date string for display in the daily/weekly agenda, or timeline.
1028 This function makes sure that dates are aligned for easy reading."
1029 (require 'cal-iso)
1030 (let* ((dayname (calendar-day-name date))
1031 (day (cadr date))
1032 (day-of-week (calendar-day-of-week date))
1033 (month (car date))
1034 (monthname (calendar-month-name month))
1035 (year (nth 2 date))
1036 (iso-week (org-days-to-iso-week
1037 (calendar-absolute-from-gregorian date)))
1038 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1039 (1- year))
1040 ((and (= month 12) (<= iso-week 1))
1041 (1+ year))
1042 (t year)))
1043 (weekstring (if (= day-of-week 1)
1044 (format " W%02d" iso-week)
1045 "")))
1046 (format "%-10s %2d %s %4d%s"
1047 dayname day monthname year weekstring)))
1049 (defcustom org-agenda-time-leading-zero nil
1050 "Non-nil means use leading zero for military times in agenda.
1051 For example, 9:30am would become 09:30 rather than 9:30."
1052 :group 'org-agenda-daily/weekly
1053 :version "24.1"
1054 :type 'boolean)
1056 (defcustom org-agenda-timegrid-use-ampm nil
1057 "When set, show AM/PM style timestamps on the timegrid."
1058 :group 'org-agenda
1059 :version "24.1"
1060 :type 'boolean)
1062 (defun org-agenda-time-of-day-to-ampm (time)
1063 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1064 (let* ((hour-number (string-to-number (substring time 0 -3)))
1065 (minute (substring time -2))
1066 (ampm "am"))
1067 (cond
1068 ((equal hour-number 12)
1069 (setq ampm "pm"))
1070 ((> hour-number 12)
1071 (setq ampm "pm")
1072 (setq hour-number (- hour-number 12))))
1073 (concat
1074 (if org-agenda-time-leading-zero
1075 (format "%02d" hour-number)
1076 (format "%02s" (number-to-string hour-number)))
1077 ":" minute ampm)))
1079 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1080 "Conditionally convert TIME to AM/PM format
1081 based on `org-agenda-timegrid-use-ampm'"
1082 (if org-agenda-timegrid-use-ampm
1083 (org-agenda-time-of-day-to-ampm time)
1084 time))
1086 (defcustom org-agenda-weekend-days '(6 0)
1087 "Which days are weekend?
1088 These days get the special face `org-agenda-date-weekend' in the agenda
1089 and timeline buffers."
1090 :group 'org-agenda-daily/weekly
1091 :type '(set :greedy t
1092 (const :tag "Monday" 1)
1093 (const :tag "Tuesday" 2)
1094 (const :tag "Wednesday" 3)
1095 (const :tag "Thursday" 4)
1096 (const :tag "Friday" 5)
1097 (const :tag "Saturday" 6)
1098 (const :tag "Sunday" 0)))
1100 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1101 "Non-nil means jump to today when moving a past date forward in time.
1102 When using S-right in the agenda to move a a date forward, and the date
1103 stamp currently points to the past, the first key press will move it
1104 to today. WHen nil, just move one day forward even if the date stays
1105 in the past."
1106 :group 'org-agenda-daily/weekly
1107 :version "24.1"
1108 :type 'boolean)
1110 (defcustom org-agenda-include-diary nil
1111 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1112 Custom commands can set this variable in the options section."
1113 :group 'org-agenda-daily/weekly
1114 :type 'boolean)
1116 (defcustom org-agenda-include-deadlines t
1117 "If non-nil, include entries within their deadline warning period.
1118 Custom commands can set this variable in the options section."
1119 :group 'org-agenda-daily/weekly
1120 :version "24.1"
1121 :type 'boolean)
1123 (defcustom org-agenda-repeating-timestamp-show-all t
1124 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1125 When set to a list of strings, only show occurrences of repeating
1126 stamps for these TODO keywords. When nil, only one occurrence is
1127 shown, either today or the nearest into the future."
1128 :group 'org-agenda-daily/weekly
1129 :type '(choice
1130 (const :tag "Show repeating stamps" t)
1131 (repeat :tag "Show repeating stamps for these TODO keywords"
1132 (string :tag "TODO Keyword"))
1133 (const :tag "Don't show repeating stamps" nil)))
1135 (defcustom org-scheduled-past-days 10000
1136 "No. of days to continue listing scheduled items that are not marked DONE.
1137 When an item is scheduled on a date, it shows up in the agenda on this
1138 day and will be listed until it is marked done for the number of days
1139 given here."
1140 :group 'org-agenda-daily/weekly
1141 :type 'integer)
1143 (defcustom org-agenda-log-mode-items '(closed clock)
1144 "List of items that should be shown in agenda log mode.
1145 This list may contain the following symbols:
1147 closed Show entries that have been closed on that day.
1148 clock Show entries that have received clocked time on that day.
1149 state Show all logged state changes.
1150 Note that instead of changing this variable, you can also press `C-u l' in
1151 the agenda to display all available LOG items temporarily."
1152 :group 'org-agenda-daily/weekly
1153 :type '(set :greedy t (const closed) (const clock) (const state)))
1155 (defcustom org-agenda-clock-consistency-checks
1156 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1157 :gap-ok-around ("4:00")
1158 :default-face ((:background "DarkRed") (:foreground "white"))
1159 :overlap-face nil :gap-face nil :no-end-time-face nil
1160 :long-face nil :short-face nil)
1161 "This is a property list, with the following keys:
1163 :max-duration Mark clocking chunks that are longer than this time.
1164 This is a time string like \"HH:MM\", or the number
1165 of minutes as an integer.
1167 :min-duration Mark clocking chunks that are shorter that this.
1168 This is a time string like \"HH:MM\", or the number
1169 of minutes as an integer.
1171 :max-gap Mark gaps between clocking chunks that are longer than
1172 this duration. A number of minutes, or a string
1173 like \"HH:MM\".
1175 :gap-ok-around List of times during the day which are usually not working
1176 times. When a gap is detected, but the gap contains any
1177 of these times, the gap is *not* reported. For example,
1178 if this is (\"4:00\" \"13:00\") then gaps that contain
1179 4:00 in the morning (i.e. the night) and 13:00
1180 (i.e. a typical lunch time) do not cause a warning.
1181 You should have at least one time during the night in this
1182 list, or otherwise the first task each morning will trigger
1183 a warning because it follows a long gap.
1185 Furthermore, the following properties can be used to define faces for
1186 issue display.
1188 :default-face the default face, if the specific face is undefined
1189 :overlap-face face for overlapping clocks
1190 :gap-face face for gaps between clocks
1191 :no-end-time-face face for incomplete clocks
1192 :long-face face for clock intervals that are too long
1193 :short-face face for clock intervals that are too short"
1194 :group 'org-agenda-daily/weekly
1195 :group 'org-clock
1196 :version "24.1"
1197 :type 'plist)
1199 (defcustom org-agenda-log-mode-add-notes t
1200 "Non-nil means add first line of notes to log entries in agenda views.
1201 If a log item like a state change or a clock entry is associated with
1202 notes, the first line of these notes will be added to the entry in the
1203 agenda display."
1204 :group 'org-agenda-daily/weekly
1205 :type 'boolean)
1207 (defcustom org-agenda-start-with-log-mode nil
1208 "The initial value of log-mode in a newly created agenda window."
1209 :group 'org-agenda-startup
1210 :group 'org-agenda-daily/weekly
1211 :type 'boolean)
1213 (defcustom org-agenda-start-with-clockreport-mode nil
1214 "The initial value of clockreport-mode in a newly created agenda window."
1215 :group 'org-agenda-startup
1216 :group 'org-agenda-daily/weekly
1217 :type 'boolean)
1219 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1220 "Property list with parameters for the clocktable in clockreport mode.
1221 This is the display mode that shows a clock table in the daily/weekly
1222 agenda, the properties for this dynamic block can be set here.
1223 The usual clocktable parameters are allowed here, but you cannot set
1224 the properties :name, :tstart, :tend, :block, and :scope - these will
1225 be overwritten to make sure the content accurately reflects the
1226 current display in the agenda."
1227 :group 'org-agenda-daily/weekly
1228 :type 'plist)
1230 (defcustom org-agenda-search-view-always-boolean nil
1231 "Non-nil means the search string is interpreted as individual parts.
1233 The search string for search view can either be interpreted as a phrase,
1234 or as a list of snippets that define a boolean search for a number of
1235 strings.
1237 When this is non-nil, the string will be split on whitespace, and each
1238 snippet will be searched individually, and all must match in order to
1239 select an entry. A snippet is then a single string of non-white
1240 characters, or a string in double quotes, or a regexp in {} braces.
1241 If a snippet is preceded by \"-\", the snippet must *not* match.
1242 \"+\" is syntactic sugar for positive selection. Each snippet may
1243 be found as a full word or a partial word, but see the variable
1244 `org-agenda-search-view-force-full-words'.
1246 When this is nil, search will look for the entire search phrase as one,
1247 with each space character matching any amount of whitespace, including
1248 line breaks.
1250 Even when this is nil, you can still switch to Boolean search dynamically
1251 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1252 is a regexp marked with braces like \"{abc}\", this will also switch to
1253 boolean search."
1254 :group 'org-agenda-search-view
1255 :version "24.1"
1256 :type 'boolean)
1258 (if (fboundp 'defvaralias)
1259 (defvaralias 'org-agenda-search-view-search-words-only
1260 'org-agenda-search-view-always-boolean))
1262 (defcustom org-agenda-search-view-force-full-words nil
1263 "Non-nil means, search words must be matches as complete words.
1264 When nil, they may also match part of a word."
1265 :group 'org-agenda-search-view
1266 :version "24.1"
1267 :type 'boolean)
1269 (defgroup org-agenda-time-grid nil
1270 "Options concerning the time grid in the Org-mode Agenda."
1271 :tag "Org Agenda Time Grid"
1272 :group 'org-agenda)
1274 (defcustom org-agenda-search-headline-for-time t
1275 "Non-nil means search headline for a time-of-day.
1276 If the headline contains a time-of-day in one format or another, it will
1277 be used to sort the entry into the time sequence of items for a day.
1278 Some people have time stamps in the headline that refer to the creation
1279 time or so, and then this produces an unwanted side effect. If this is
1280 the case for your, use this variable to turn off searching the headline
1281 for a time."
1282 :group 'org-agenda-time-grid
1283 :type 'boolean)
1285 (defcustom org-agenda-use-time-grid t
1286 "Non-nil means show a time grid in the agenda schedule.
1287 A time grid is a set of lines for specific times (like every two hours between
1288 8:00 and 20:00). The items scheduled for a day at specific times are
1289 sorted in between these lines.
1290 For details about when the grid will be shown, and what it will look like, see
1291 the variable `org-agenda-time-grid'."
1292 :group 'org-agenda-time-grid
1293 :type 'boolean)
1295 (defcustom org-agenda-time-grid
1296 '((daily today require-timed)
1297 "----------------"
1298 (800 1000 1200 1400 1600 1800 2000))
1300 "The settings for time grid for agenda display.
1301 This is a list of three items. The first item is again a list. It contains
1302 symbols specifying conditions when the grid should be displayed:
1304 daily if the agenda shows a single day
1305 weekly if the agenda shows an entire week
1306 today show grid on current date, independent of daily/weekly display
1307 require-timed show grid only if at least one item has a time specification
1309 The second item is a string which will be placed behind the grid time.
1311 The third item is a list of integers, indicating the times that should have
1312 a grid line."
1313 :group 'org-agenda-time-grid
1314 :type
1315 '(list
1316 (set :greedy t :tag "Grid Display Options"
1317 (const :tag "Show grid in single day agenda display" daily)
1318 (const :tag "Show grid in weekly agenda display" weekly)
1319 (const :tag "Always show grid for today" today)
1320 (const :tag "Show grid only if any timed entries are present"
1321 require-timed)
1322 (const :tag "Skip grid times already present in an entry"
1323 remove-match))
1324 (string :tag "Grid String")
1325 (repeat :tag "Grid Times" (integer :tag "Time"))))
1327 (defcustom org-agenda-show-current-time-in-grid t
1328 "Non-nil means show the current time in the time grid."
1329 :group 'org-agenda-time-grid
1330 :version "24.1"
1331 :type 'boolean)
1333 (defcustom org-agenda-current-time-string
1334 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1335 "The string for the current time marker in the agenda."
1336 :group 'org-agenda-time-grid
1337 :version "24.1"
1338 :type 'string)
1340 (defgroup org-agenda-sorting nil
1341 "Options concerning sorting in the Org-mode Agenda."
1342 :tag "Org Agenda Sorting"
1343 :group 'org-agenda)
1345 (defcustom org-agenda-sorting-strategy
1346 '((agenda habit-down time-up priority-down category-keep)
1347 (todo priority-down category-keep)
1348 (tags priority-down category-keep)
1349 (search category-keep))
1350 "Sorting structure for the agenda items of a single day.
1351 This is a list of symbols which will be used in sequence to determine
1352 if an entry should be listed before another entry. The following
1353 symbols are recognized:
1355 time-up Put entries with time-of-day indications first, early first
1356 time-down Put entries with time-of-day indications first, late first
1357 category-keep Keep the default order of categories, corresponding to the
1358 sequence in `org-agenda-files'.
1359 category-up Sort alphabetically by category, A-Z.
1360 category-down Sort alphabetically by category, Z-A.
1361 tag-up Sort alphabetically by last tag, A-Z.
1362 tag-down Sort alphabetically by last tag, Z-A.
1363 priority-up Sort numerically by priority, high priority last.
1364 priority-down Sort numerically by priority, high priority first.
1365 todo-state-up Sort by todo state, tasks that are done last.
1366 todo-state-down Sort by todo state, tasks that are done first.
1367 effort-up Sort numerically by estimated effort, high effort last.
1368 effort-down Sort numerically by estimated effort, high effort first.
1369 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1370 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1371 habit-up Put entries that are habits first
1372 habit-down Put entries that are habits last
1373 alpha-up Sort headlines alphabetically
1374 alpha-down Sort headlines alphabetically, reversed
1376 The different possibilities will be tried in sequence, and testing stops
1377 if one comparison returns a \"not-equal\". For example, the default
1378 '(time-up category-keep priority-down)
1379 means: Pull out all entries having a specified time of day and sort them,
1380 in order to make a time schedule for the current day the first thing in the
1381 agenda listing for the day. Of the entries without a time indication, keep
1382 the grouped in categories, don't sort the categories, but keep them in
1383 the sequence given in `org-agenda-files'. Within each category sort by
1384 priority.
1386 Leaving out `category-keep' would mean that items will be sorted across
1387 categories by priority.
1389 Instead of a single list, this can also be a set of list for specific
1390 contents, with a context symbol in the car of the list, any of
1391 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1393 Custom commands can bind this variable in the options section."
1394 :group 'org-agenda-sorting
1395 :type `(choice
1396 (repeat :tag "General" ,org-sorting-choice)
1397 (list :tag "Individually"
1398 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1399 (repeat ,org-sorting-choice))
1400 (cons (const :tag "Strategy for TODO lists" todo)
1401 (repeat ,org-sorting-choice))
1402 (cons (const :tag "Strategy for Tags matches" tags)
1403 (repeat ,org-sorting-choice))
1404 (cons (const :tag "Strategy for search matches" search)
1405 (repeat ,org-sorting-choice)))))
1407 (defcustom org-agenda-cmp-user-defined nil
1408 "A function to define the comparison `user-defined'.
1409 This function must receive two arguments, agenda entry a and b.
1410 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1411 the user comparison, return nil.
1412 When this is defined, you can make `user-defined-up' and `user-defined-down'
1413 part of an agenda sorting strategy."
1414 :group 'org-agenda-sorting
1415 :type 'symbol)
1417 (defcustom org-sort-agenda-notime-is-late t
1418 "Non-nil means items without time are considered late.
1419 This is only relevant for sorting. When t, items which have no explicit
1420 time like 15:30 will be considered as 99:01, i.e. later than any items which
1421 do have a time. When nil, the default time is before 0:00. You can use this
1422 option to decide if the schedule for today should come before or after timeless
1423 agenda entries."
1424 :group 'org-agenda-sorting
1425 :type 'boolean)
1427 (defcustom org-sort-agenda-noeffort-is-high t
1428 "Non-nil means items without effort estimate are sorted as high effort.
1429 This also applies when filtering an agenda view with respect to the
1430 < or > effort operator. Then, tasks with no effort defined will be treated
1431 as tasks with high effort.
1432 When nil, such items are sorted as 0 minutes effort."
1433 :group 'org-agenda-sorting
1434 :type 'boolean)
1436 (defgroup org-agenda-line-format nil
1437 "Options concerning the entry prefix in the Org-mode agenda display."
1438 :tag "Org Agenda Line Format"
1439 :group 'org-agenda)
1441 (defcustom org-agenda-prefix-format
1442 '((agenda . " %i %-12:c%?-12t% s")
1443 (timeline . " % s")
1444 (todo . " %i %-12:c")
1445 (tags . " %i %-12:c")
1446 (search . " %i %-12:c"))
1447 "Format specifications for the prefix of items in the agenda views.
1448 An alist with five entries, each for the different agenda types. The
1449 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1450 The values are format strings.
1452 This format works similar to a printf format, with the following meaning:
1454 %c the category of the item, \"Diary\" for entries from the diary,
1455 or as given by the CATEGORY keyword or derived from the file name
1456 %e the effort required by the item
1457 %i the icon category of the item, see `org-agenda-category-icon-alist'
1458 %T the last tag of the item (ignore inherited tags, which come first)
1459 %t the HH:MM time-of-day specification if one applies to the entry
1460 %s Scheduling/Deadline information, a short string
1461 %(expression) Eval EXPRESSION and replace the control string
1462 by the result
1464 All specifiers work basically like the standard `%s' of printf, but may
1465 contain two additional characters: a question mark just after the `%'
1466 and a whitespace/punctuation character just before the final letter.
1468 If the first character after `%' is a question mark, the entire field
1469 will only be included if the corresponding value applies to the current
1470 entry. This is useful for fields which should have fixed width when
1471 present, but zero width when absent. For example, \"%?-12t\" will
1472 result in a 12 character time field if a time of the day is specified,
1473 but will completely disappear in entries which do not contain a time.
1475 If there is punctuation or whitespace character just before the final
1476 format letter, this character will be appended to the field value if
1477 the value is not empty. For example, the format \"%-12:c\" leads to
1478 \"Diary: \" if the category is \"Diary\". If the category were be
1479 empty, no additional colon would be inserted.
1481 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1482 which means:
1484 - Indent the line with two space characters
1485 - Give the category a 12 chars wide field, padded with whitespace on
1486 the right (because of `-'). Append a colon if there is a category
1487 (because of `:').
1488 - If there is a time-of-day, put it into a 12 chars wide field. If no
1489 time, don't put in an empty field, just skip it (because of '?').
1490 - Finally, put the scheduling information.
1492 See also the variables `org-agenda-remove-times-when-in-prefix' and
1493 `org-agenda-remove-tags'.
1495 Custom commands can set this variable in the options section."
1496 :type '(choice
1497 (string :tag "General format")
1498 (list :greedy t :tag "View dependent"
1499 (cons (const agenda) (string :tag "Format"))
1500 (cons (const timeline) (string :tag "Format"))
1501 (cons (const todo) (string :tag "Format"))
1502 (cons (const tags) (string :tag "Format"))
1503 (cons (const search) (string :tag "Format"))))
1504 :group 'org-agenda-line-format)
1506 (defvar org-prefix-format-compiled nil
1507 "The compiled prefix format and associated variables.
1508 This is a list where first element is a list of variable bindings, and second
1509 element is the compiled format expression. See the variable
1510 `org-agenda-prefix-format'.")
1512 (defcustom org-agenda-todo-keyword-format "%-1s"
1513 "Format for the TODO keyword in agenda lines.
1514 Set this to something like \"%-12s\" if you want all TODO keywords
1515 to occupy a fixed space in the agenda display."
1516 :group 'org-agenda-line-format
1517 :type 'string)
1519 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1520 "Text preceding timerange entries in the agenda view.
1521 This is a list with two strings. The first applies when the range
1522 is entirely on one day. The second applies if the range spans several days.
1523 The strings may have two \"%d\" format specifiers which will be filled
1524 with the sequence number of the days, and the total number of days in the
1525 range, respectively."
1526 :group 'org-agenda-line-format
1527 :type '(list
1528 (string :tag "Deadline today ")
1529 (choice :tag "Deadline relative"
1530 (string :tag "Format string")
1531 (function))))
1533 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1534 "Text preceding scheduled items in the agenda view.
1535 This is a list with two strings. The first applies when the item is
1536 scheduled on the current day. The second applies when it has been scheduled
1537 previously, it may contain a %d indicating that this is the nth time that
1538 this item is scheduled, due to automatic rescheduling of unfinished items
1539 for the following day. So this number is one larger than the number of days
1540 that passed since this item was scheduled first."
1541 :group 'org-agenda-line-format
1542 :type '(list
1543 (string :tag "Scheduled today ")
1544 (string :tag "Scheduled previously")))
1546 (defcustom org-agenda-inactive-leader "["
1547 "Text preceding item pulled into the agenda by inactive time stamps.
1548 These entries are added to the agenda when pressing \"[\"."
1549 :group 'org-agenda-line-format
1550 :version "24.1"
1551 :type '(list
1552 (string :tag "Scheduled today ")
1553 (string :tag "Scheduled previously")))
1555 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1556 "Text preceding deadline items in the agenda view.
1557 This is a list with two strings. The first applies when the item has its
1558 deadline on the current day. The second applies when it is in the past or
1559 in the future, it may contain %d to capture how many days away the deadline
1560 is (was)."
1561 :group 'org-agenda-line-format
1562 :type '(list
1563 (string :tag "Deadline today ")
1564 (choice :tag "Deadline relative"
1565 (string :tag "Format string")
1566 (function))))
1568 (defcustom org-agenda-remove-times-when-in-prefix t
1569 "Non-nil means remove duplicate time specifications in agenda items.
1570 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1571 time-of-day specification in a headline or diary entry is extracted and
1572 placed into the prefix. If this option is non-nil, the original specification
1573 \(a timestamp or -range, or just a plain time(range) specification like
1574 11:30-4pm) will be removed for agenda display. This makes the agenda less
1575 cluttered.
1576 The option can be t or nil. It may also be the symbol `beg', indicating
1577 that the time should only be removed when it is located at the beginning of
1578 the headline/diary entry."
1579 :group 'org-agenda-line-format
1580 :type '(choice
1581 (const :tag "Always" t)
1582 (const :tag "Never" nil)
1583 (const :tag "When at beginning of entry" beg)))
1585 (defcustom org-agenda-remove-timeranges-from-blocks nil
1586 "Non-nil means remove time ranges specifications in agenda
1587 items that span on several days."
1588 :group 'org-agenda-line-format
1589 :version "24.1"
1590 :type 'boolean)
1592 (defcustom org-agenda-default-appointment-duration nil
1593 "Default duration for appointments that only have a starting time.
1594 When nil, no duration is specified in such cases.
1595 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1596 :group 'org-agenda-line-format
1597 :type '(choice
1598 (integer :tag "Minutes")
1599 (const :tag "No default duration")))
1601 (defcustom org-agenda-show-inherited-tags t
1602 "Non-nil means show inherited tags in each agenda line."
1603 :group 'org-agenda-line-format
1604 :type 'boolean)
1606 (defcustom org-agenda-hide-tags-regexp nil
1607 "Regular expression used to filter away specific tags in agenda views.
1608 This means that these tags will be present, but not be shown in the agenda
1609 line. Secondary filtering will still work on the hidden tags.
1610 Nil means don't hide any tags."
1611 :group 'org-agenda-line-format
1612 :type '(choice
1613 (const :tag "Hide none" nil)
1614 (string :tag "Regexp ")))
1616 (defcustom org-agenda-remove-tags nil
1617 "Non-nil means remove the tags from the headline copy in the agenda.
1618 When this is the symbol `prefix', only remove tags when
1619 `org-agenda-prefix-format' contains a `%T' specifier."
1620 :group 'org-agenda-line-format
1621 :type '(choice
1622 (const :tag "Always" t)
1623 (const :tag "Never" nil)
1624 (const :tag "When prefix format contains %T" prefix)))
1626 (if (fboundp 'defvaralias)
1627 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1628 'org-agenda-remove-tags))
1630 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1631 "Shift tags in agenda items to this column.
1632 If this number is positive, it specifies the column. If it is negative,
1633 it means that the tags should be flushright to that column. For example,
1634 -80 works well for a normal 80 character screen."
1635 :group 'org-agenda-line-format
1636 :type 'integer)
1638 (if (fboundp 'defvaralias)
1639 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1641 (defcustom org-agenda-fontify-priorities 'cookies
1642 "Non-nil means highlight low and high priorities in agenda.
1643 When t, the highest priority entries are bold, lowest priority italic.
1644 However, settings in `org-priority-faces' will overrule these faces.
1645 When this variable is the symbol `cookies', only fontify the
1646 cookies, not the entire task.
1647 This may also be an association list of priority faces, whose
1648 keys are the character values of `org-highest-priority',
1649 `org-default-priority', and `org-lowest-priority' (the default values
1650 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1651 color as a string, or a list like `(:background \"Red\")'.
1652 If it is a color, the variable `org-faces-easy-properties'
1653 determines if it is a foreground or a background color."
1654 :group 'org-agenda-line-format
1655 :type '(choice
1656 (const :tag "Never" nil)
1657 (const :tag "Defaults" t)
1658 (const :tag "Cookies only" cookies)
1659 (repeat :tag "Specify"
1660 (list (character :tag "Priority" :value ?A)
1661 (choice :tag "Face "
1662 (string :tag "Color")
1663 (sexp :tag "Face"))))))
1665 (defcustom org-agenda-day-face-function nil
1666 "Function called to determine what face should be used to display a day.
1667 The only argument passed to that function is the day. It should
1668 returns a face, or nil if does not want to specify a face and let
1669 the normal rules apply."
1670 :group 'org-agenda-line-format
1671 :version "24.1"
1672 :type 'function)
1674 (defcustom org-agenda-category-icon-alist nil
1675 "Alist of category icon to be displayed in agenda views.
1677 Each entry should have the following format:
1679 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1681 Where CATEGORY-REGEXP is a regexp matching the categories where
1682 the icon should be displayed.
1683 FILE-OR-DATA either a file path or a string containing image data.
1685 The other fields can be omitted safely if not needed:
1686 TYPE indicates the image type.
1687 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1688 image data.
1689 PROPS are additional image attributes to assign to the image,
1690 like, e.g. `:ascent center'.
1692 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1694 If you want to set the display properties yourself, just put a
1695 list as second element:
1697 (CATEGORY-REGEXP (MY PROPERTY LIST))
1699 For example, to display a 16px horizontal space for Emacs
1700 category, you can use:
1702 (\"Emacs\" '(space . (:width (16))))"
1703 :group 'org-agenda-line-format
1704 :version "24.1"
1705 :type '(alist :key-type (string :tag "Regexp matching category")
1706 :value-type (choice (list :tag "Icon"
1707 (string :tag "File or data")
1708 (symbol :tag "Type")
1709 (boolean :tag "Data?")
1710 (repeat :tag "Extra image properties" :inline t symbol))
1711 (list :tag "Display properties" sexp))))
1713 (defgroup org-agenda-column-view nil
1714 "Options concerning column view in the agenda."
1715 :tag "Org Agenda Column View"
1716 :group 'org-agenda)
1718 (defcustom org-agenda-columns-show-summaries t
1719 "Non-nil means show summaries for columns displayed in the agenda view."
1720 :group 'org-agenda-column-view
1721 :type 'boolean)
1723 (defcustom org-agenda-columns-compute-summary-properties t
1724 "Non-nil means recompute all summary properties before column view.
1725 When column view in the agenda is listing properties that have a summary
1726 operator, it can go to all relevant buffers and recompute the summaries
1727 there. This can mean overhead for the agenda column view, but is necessary
1728 to have thing up to date.
1729 As a special case, a CLOCKSUM property also makes sure that the clock
1730 computations are current."
1731 :group 'org-agenda-column-view
1732 :type 'boolean)
1734 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1735 "Non-nil means the duration of an appointment will add to day effort.
1736 The property to which appointment durations will be added is the one given
1737 in the option `org-effort-property'. If an appointment does not have
1738 an end time, `org-agenda-default-appointment-duration' will be used. If that
1739 is not set, an appointment without end time will not contribute to the time
1740 estimate."
1741 :group 'org-agenda-column-view
1742 :type 'boolean)
1744 (defcustom org-agenda-auto-exclude-function nil
1745 "A function called with a tag to decide if it is filtered on '/ RET'.
1746 The sole argument to the function, which is called once for each
1747 possible tag, is a string giving the name of the tag. The
1748 function should return either nil if the tag should be included
1749 as normal, or \"-<TAG>\" to exclude the tag.
1750 Note that for the purpose of tag filtering, only the lower-case version of
1751 all tags will be considered, so that this function will only ever see
1752 the lower-case version of all tags."
1753 :group 'org-agenda
1754 :type 'function)
1756 (defcustom org-agenda-bulk-custom-functions nil
1757 "Alist of characters and custom functions for bulk actions.
1758 For example, this value makes those two functions available:
1760 '((?R set-category)
1761 (?C bulk-cut))
1763 With selected entries in an agenda buffer, `B R' will call
1764 the custom function `set-category' on the selected entries.
1765 Note that functions in this alist don't need to be quoted."
1766 :type 'alist
1767 :version "24.1"
1768 :group 'org-agenda)
1770 (eval-when-compile
1771 (require 'cl))
1772 (require 'org)
1774 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1775 "Execute BODY with point at location given by `org-hd-marker' property.
1776 If STRING is non-nil, the text property will be fetched from position 0
1777 in that string. If STRING is nil, it will be fetched from the beginning
1778 of the current line."
1779 (org-with-gensyms (marker)
1780 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1781 'org-hd-marker ,string)))
1782 (with-current-buffer (marker-buffer ,marker)
1783 (save-excursion
1784 (goto-char ,marker)
1785 ,@body)))))
1786 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1788 (defun org-add-agenda-custom-command (entry)
1789 "Replace or add a command in `org-agenda-custom-commands'.
1790 This is mostly for hacking and trying a new command - once the command
1791 works you probably want to add it to `org-agenda-custom-commands' for good."
1792 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1793 (if ass
1794 (setcdr ass (cdr entry))
1795 (push entry org-agenda-custom-commands))))
1797 ;;; Define the org-agenda-mode
1799 (defvar org-agenda-mode-map (make-sparse-keymap)
1800 "Keymap for `org-agenda-mode'.")
1801 (if (fboundp 'defvaralias)
1802 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1804 (defvar org-agenda-menu) ; defined later in this file.
1805 (defvar org-agenda-restrict) ; defined later in this file.
1806 (defvar org-agenda-follow-mode nil)
1807 (defvar org-agenda-entry-text-mode nil)
1808 (defvar org-agenda-clockreport-mode nil)
1809 (defvar org-agenda-show-log nil)
1810 (defvar org-agenda-redo-command nil)
1811 (defvar org-agenda-query-string nil)
1812 (defvar org-agenda-mode-hook nil
1813 "Hook for `org-agenda-mode', run after the mode is turned on.")
1814 (defvar org-agenda-type nil)
1815 (defvar org-agenda-force-single-file nil)
1816 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1819 ;;; Multiple agenda buffers support
1821 (defcustom org-agenda-sticky nil
1822 "Non-nil means agenda q key will bury agenda buffers.
1823 Agenda commands will then show existing buffer instead of generating new ones.
1824 When nil, `q' will kill the single agenda buffer."
1825 :group 'org-agenda
1826 :type 'boolean
1827 :set (lambda (var val)
1828 (if (boundp var)
1829 (org-toggle-sticky-agenda (if val 1 0))
1830 (set var val))))
1832 (defun org-toggle-sticky-agenda (&optional arg)
1833 "Toggle `org-agenda-sticky'."
1834 (interactive "P")
1835 (let ((new-value (if arg
1836 (> (prefix-numeric-value arg) 0)
1837 (not org-agenda-sticky))))
1838 (if (equal new-value org-agenda-sticky)
1839 (message "Sticky agenda was already %s"
1840 (if org-agenda-sticky "enabled" "disabled"))
1841 (setq org-agenda-sticky new-value)
1842 (org-agenda-kill-all-agenda-buffers)
1843 (message "Sticky agenda was %s"
1844 (if org-agenda-sticky "enabled" "disabled")))))
1846 (defvar org-agenda-buffer nil
1847 "Agenda buffer currently being generated.")
1849 (defvar org-agenda-last-prefix-arg nil)
1850 (defvar org-agenda-this-buffer-name nil)
1851 (defvar org-agenda-doing-sticky-redo nil)
1852 (defvar org-agenda-this-buffer-is-sticky nil)
1854 (defconst org-agenda-local-vars
1855 '(org-agenda-this-buffer-name
1856 org-agenda-undo-list
1857 org-agenda-pending-undo-list
1858 org-agenda-follow-mode
1859 org-agenda-entry-text-mode
1860 org-agenda-clockreport-mode
1861 org-agenda-show-log
1862 org-agenda-redo-command
1863 org-agenda-query-string
1864 org-agenda-type
1865 org-agenda-bulk-marked-entries
1866 org-agenda-undo-has-started-in
1867 org-agenda-last-arguments
1868 org-agenda-info
1869 org-agenda-tag-filter-overlays
1870 org-agenda-cat-filter-overlays
1871 org-pre-agenda-window-conf
1872 org-agenda-columns-active
1873 org-agenda-tag-filter
1874 org-agenda-category-filter
1875 org-agenda-markers
1876 org-agenda-last-search-view-search-was-boolean
1877 org-agenda-filtered-by-category
1878 org-agenda-filter-form
1879 org-agenda-show-window
1880 org-agenda-cycle-counter
1881 org-agenda-last-prefix-arg)
1882 "Variables that must be local in agenda buffers to allow multiple buffers.")
1884 (defun org-agenda-mode ()
1885 "Mode for time-sorted view on action items in Org-mode files.
1887 The following commands are available:
1889 \\{org-agenda-mode-map}"
1890 (interactive)
1891 (cond (org-agenda-doing-sticky-redo
1892 ;; Refreshing sticky agenda-buffer
1894 ;; Preserve the value of `org-agenda-local-vars' variables,
1895 ;; while letting `kill-all-local-variables' kill the rest
1896 (let ((save (buffer-local-variables)))
1897 (kill-all-local-variables)
1898 (mapc 'make-local-variable org-agenda-local-vars)
1899 (dolist (elem save)
1900 (let ((var (car elem))
1901 (val (cdr elem)))
1902 (when (and val
1903 (member var org-agenda-local-vars))
1904 (set var val)))))
1905 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1906 (org-agenda-sticky
1907 ;; Creating a sticky Agenda buffer for the first time
1908 (kill-all-local-variables)
1909 (mapc 'make-local-variable org-agenda-local-vars)
1910 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1912 ;; Creating a non-sticky agenda buffer
1913 (kill-all-local-variables)
1914 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
1915 (setq org-agenda-undo-list nil
1916 org-agenda-pending-undo-list nil
1917 org-agenda-bulk-marked-entries nil)
1918 (setq major-mode 'org-agenda-mode)
1919 ;; Keep global-font-lock-mode from turning on font-lock-mode
1920 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1921 (setq mode-name "Org-Agenda")
1922 (use-local-map org-agenda-mode-map)
1923 (easy-menu-add org-agenda-menu)
1924 (if org-startup-truncated (setq truncate-lines t))
1925 (org-set-local 'line-move-visual nil)
1926 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1927 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1928 ;; Make sure properties are removed when copying text
1929 (when (boundp 'buffer-substring-filters)
1930 (org-set-local 'buffer-substring-filters
1931 (cons (lambda (x)
1932 (set-text-properties 0 (length x) nil x) x)
1933 buffer-substring-filters)))
1934 (unless org-agenda-keep-modes
1935 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1936 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1937 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1938 org-agenda-show-log org-agenda-start-with-log-mode))
1940 (easy-menu-change
1941 '("Agenda") "Agenda Files"
1942 (append
1943 (list
1944 (vector
1945 (if (get 'org-agenda-files 'org-restrict)
1946 "Restricted to single file"
1947 "Edit File List")
1948 '(org-edit-agenda-file-list)
1949 (not (get 'org-agenda-files 'org-restrict)))
1950 "--")
1951 (mapcar 'org-file-menu-entry (org-agenda-files))))
1952 (org-agenda-set-mode-name)
1953 (apply
1954 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1955 (list 'org-agenda-mode-hook)))
1957 (substitute-key-definition 'undo 'org-agenda-undo
1958 org-agenda-mode-map global-map)
1959 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1960 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1961 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1962 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1963 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1964 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1965 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1966 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1967 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1968 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1969 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1970 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1971 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1972 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1973 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1974 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1975 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1976 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1977 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1978 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1979 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1980 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1981 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1982 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1983 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1984 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1985 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1986 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1987 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1988 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1989 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1990 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1991 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1992 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1993 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1994 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1995 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1996 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1997 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1998 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1999 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
2000 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2001 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2002 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2003 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2005 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2006 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2007 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2008 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2009 (while l (org-defkey org-agenda-mode-map
2010 (int-to-string (pop l)) 'digit-argument)))
2012 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2013 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2014 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2015 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2016 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2017 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2018 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2019 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2020 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2021 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
2022 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2023 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2024 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2025 'org-clock-modify-effort-estimate)
2026 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2027 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2028 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2029 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2030 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2031 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2032 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2033 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
2034 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2035 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2036 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2037 (substitute-key-definition 'next-line 'org-agenda-next-line
2038 org-agenda-mode-map global-map)
2039 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2040 org-agenda-mode-map global-map)
2041 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2042 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2043 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2044 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2045 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2046 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2047 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2048 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2049 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2050 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2051 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2052 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2053 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2054 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2055 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2056 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2057 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2058 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2059 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2060 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2061 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2062 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2063 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2064 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2065 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2066 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2067 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2068 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2069 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2070 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2072 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2073 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2074 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2075 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2076 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2077 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2078 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2079 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-category)
2080 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2081 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2082 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2083 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2085 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2086 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2087 (when org-agenda-mouse-1-follows-link
2088 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2089 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2090 '("Agenda"
2091 ("Agenda Files")
2092 "--"
2093 ("Agenda Dates"
2094 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2095 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2096 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2097 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2098 "--"
2099 ("View"
2100 ["Day View" org-agenda-day-view
2101 :active (org-agenda-check-type nil 'agenda)
2102 :style radio :selected (eq org-agenda-current-span 'day)
2103 :keys "v d (or just d)"]
2104 ["Week View" org-agenda-week-view
2105 :active (org-agenda-check-type nil 'agenda)
2106 :style radio :selected (eq org-agenda-current-span 'week)
2107 :keys "v w (or just w)"]
2108 ["Month View" org-agenda-month-view
2109 :active (org-agenda-check-type nil 'agenda)
2110 :style radio :selected (eq org-agenda-current-span 'month)
2111 :keys "v m"]
2112 ["Year View" org-agenda-year-view
2113 :active (org-agenda-check-type nil 'agenda)
2114 :style radio :selected (eq org-agenda-current-span 'year)
2115 :keys "v y"]
2116 "--"
2117 ["Include Diary" org-agenda-toggle-diary
2118 :style toggle :selected org-agenda-include-diary
2119 :active (org-agenda-check-type nil 'agenda)]
2120 ["Include Deadlines" org-agenda-toggle-deadlines
2121 :style toggle :selected org-agenda-include-deadlines
2122 :active (org-agenda-check-type nil 'agenda)]
2123 ["Use Time Grid" org-agenda-toggle-time-grid
2124 :style toggle :selected org-agenda-use-time-grid
2125 :active (org-agenda-check-type nil 'agenda)]
2126 "--"
2127 ["Show clock report" org-agenda-clockreport-mode
2128 :style toggle :selected org-agenda-clockreport-mode
2129 :active (org-agenda-check-type nil 'agenda)]
2130 ["Show some entry text" org-agenda-entry-text-mode
2131 :style toggle :selected org-agenda-entry-text-mode
2132 :active t]
2133 "--"
2134 ["Show Logbook entries" org-agenda-log-mode
2135 :style toggle :selected org-agenda-show-log
2136 :active (org-agenda-check-type nil 'agenda 'timeline)
2137 :keys "v l (or just l)"]
2138 ["Include archived trees" org-agenda-archives-mode
2139 :style toggle :selected org-agenda-archives-mode :active t
2140 :keys "v a"]
2141 ["Include archive files" (org-agenda-archives-mode t)
2142 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2143 :keys "v A"]
2144 "--"
2145 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2146 ["Write view to file" org-agenda-write t]
2147 ["Rebuild buffer" org-agenda-redo t]
2148 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2149 "--"
2150 ["Show original entry" org-agenda-show t]
2151 ["Go To (other window)" org-agenda-goto t]
2152 ["Go To (this window)" org-agenda-switch-to t]
2153 ["Follow Mode" org-agenda-follow-mode
2154 :style toggle :selected org-agenda-follow-mode :active t]
2155 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2156 "--"
2157 ("TODO"
2158 ["Cycle TODO" org-agenda-todo t]
2159 ["Next TODO set" org-agenda-todo-nextset t]
2160 ["Previous TODO set" org-agenda-todo-previousset t]
2161 ["Add note" org-agenda-add-note t])
2162 ("Archive/Refile/Delete"
2163 ["Archive default" org-agenda-archive-default t]
2164 ["Archive default" org-agenda-archive-default-with-confirmation t]
2165 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2166 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2167 ["Archive subtree" org-agenda-archive t]
2168 "--"
2169 ["Refile" org-agenda-refile t]
2170 "--"
2171 ["Delete subtree" org-agenda-kill t])
2172 ("Bulk action"
2173 ["Mark entry" org-agenda-bulk-mark t]
2174 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2175 ["Unmark entry" org-agenda-bulk-unmark t]
2176 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2177 ["Act on all marked" org-agenda-bulk-action t]
2178 "--"
2179 ("Tags and Properties"
2180 ["Show all Tags" org-agenda-show-tags t]
2181 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2182 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2183 "--"
2184 ["Column View" org-columns t])
2185 ("Deadline/Schedule"
2186 ["Schedule" org-agenda-schedule t]
2187 ["Set Deadline" org-agenda-deadline t]
2188 "--"
2189 ["Mark item" org-agenda-action :active t :keys "k m"]
2190 ["Show mark item" org-agenda-action :active t :keys "k v"]
2191 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2192 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2193 "--"
2194 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2195 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2196 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2197 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2198 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2199 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2200 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2201 ("Clock and Effort"
2202 ["Clock in" org-agenda-clock-in t]
2203 ["Clock out" org-agenda-clock-out t]
2204 ["Clock cancel" org-agenda-clock-cancel t]
2205 ["Goto running clock" org-clock-goto t]
2206 "--"
2207 ["Set Effort" org-agenda-set-effort t]
2208 ["Change clocked effort" org-clock-modify-effort-estimate
2209 (org-clock-is-active)])
2210 ("Priority"
2211 ["Set Priority" org-agenda-priority t]
2212 ["Increase Priority" org-agenda-priority-up t]
2213 ["Decrease Priority" org-agenda-priority-down t]
2214 ["Show Priority" org-agenda-show-priority t])
2215 ("Calendar/Diary"
2216 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2217 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2218 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2219 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2220 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2221 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2222 "--"
2223 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2224 "--"
2225 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2226 "--"
2227 ("MobileOrg"
2228 ["Push Files and Views" org-mobile-push t]
2229 ["Get Captured and Flagged" org-mobile-pull t]
2230 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2231 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2232 "--"
2233 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2234 "--"
2235 ["Quit" org-agenda-quit t]
2236 ["Exit and Release Buffers" org-agenda-exit t]
2239 ;;; Agenda undo
2241 (defvar org-agenda-allow-remote-undo t
2242 "Non-nil means allow remote undo from the agenda buffer.")
2243 (defvar org-agenda-undo-list nil
2244 "List of undoable operations in the agenda since last refresh.")
2245 (defvar org-agenda-undo-has-started-in nil
2246 "Buffers that have already seen `undo-start' in the current undo sequence.")
2247 (defvar org-agenda-pending-undo-list nil
2248 "In a series of undo commands, this is the list of remaining undo items.")
2250 (defun org-agenda-undo ()
2251 "Undo a remote editing step in the agenda.
2252 This undoes changes both in the agenda buffer and in the remote buffer
2253 that have been changed along."
2254 (interactive)
2255 (or org-agenda-allow-remote-undo
2256 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2257 (if (not (eq this-command last-command))
2258 (setq org-agenda-undo-has-started-in nil
2259 org-agenda-pending-undo-list org-agenda-undo-list))
2260 (if (not org-agenda-pending-undo-list)
2261 (error "No further undo information"))
2262 (let* ((entry (pop org-agenda-pending-undo-list))
2263 buf line cmd rembuf)
2264 (setq cmd (pop entry) line (pop entry))
2265 (setq rembuf (nth 2 entry))
2266 (org-with-remote-undo rembuf
2267 (while (bufferp (setq buf (pop entry)))
2268 (if (pop entry)
2269 (with-current-buffer buf
2270 (let ((last-undo-buffer buf)
2271 (inhibit-read-only t))
2272 (unless (memq buf org-agenda-undo-has-started-in)
2273 (push buf org-agenda-undo-has-started-in)
2274 (make-local-variable 'pending-undo-list)
2275 (undo-start))
2276 (while (and pending-undo-list
2277 (listp pending-undo-list)
2278 (not (car pending-undo-list)))
2279 (pop pending-undo-list))
2280 (undo-more 1))))))
2281 (org-goto-line line)
2282 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2284 (defun org-verify-change-for-undo (l1 l2)
2285 "Verify that a real change occurred between the undo lists L1 and L2."
2286 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2287 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2288 (not (eq l1 l2)))
2290 ;;; Agenda dispatch
2292 (defvar org-agenda-restrict nil)
2293 (defvar org-agenda-restrict-begin (make-marker))
2294 (defvar org-agenda-restrict-end (make-marker))
2295 (defvar org-agenda-last-dispatch-buffer nil)
2296 (defvar org-agenda-overriding-restriction nil)
2298 ;;;###autoload
2299 (defun org-agenda (&optional arg keys restriction)
2300 "Dispatch agenda commands to collect entries to the agenda buffer.
2301 Prompts for a command to execute. Any prefix arg will be passed
2302 on to the selected command. The default selections are:
2304 a Call `org-agenda-list' to display the agenda for current day or week.
2305 t Call `org-todo-list' to display the global todo list.
2306 T Call `org-todo-list' to display the global todo list, select only
2307 entries with a specific TODO keyword (the user gets a prompt).
2308 m Call `org-tags-view' to display headlines with tags matching
2309 a condition (the user is prompted for the condition).
2310 M Like `m', but select only TODO entries, no ordinary headlines.
2311 L Create a timeline for the current buffer.
2312 e Export views to associated files.
2313 s Search entries for keywords.
2314 / Multi occur across all agenda files and also files listed
2315 in `org-agenda-text-search-extra-files'.
2316 < Restrict agenda commands to buffer, subtree, or region.
2317 Press several times to get the desired effect.
2318 > Remove a previous restriction.
2319 # List \"stuck\" projects.
2320 ! Configure what \"stuck\" means.
2321 C Configure custom agenda commands.
2323 More commands can be added by configuring the variable
2324 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2325 searches can be pre-defined in this way.
2327 If the current buffer is in Org-mode and visiting a file, you can also
2328 first press `<' once to indicate that the agenda should be temporarily
2329 \(until the next use of \\[org-agenda]) restricted to the current file.
2330 Pressing `<' twice means to restrict to the current subtree or region
2331 \(if active)."
2332 (interactive "P")
2333 (catch 'exit
2334 (let* ((prefix-descriptions nil)
2335 (org-agenda-buffer-name org-agenda-buffer-name)
2336 (org-agenda-window-setup (if (equal (buffer-name)
2337 org-agenda-buffer-name)
2338 'current-window
2339 org-agenda-window-setup))
2340 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2341 (org-agenda-custom-commands
2342 ;; normalize different versions
2343 (delq nil
2344 (mapcar
2345 (lambda (x)
2346 (cond ((stringp (cdr x))
2347 (push x prefix-descriptions)
2348 nil)
2349 ((stringp (nth 1 x)) x)
2350 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2351 (t (cons (car x) (cons "" (cdr x))))))
2352 org-agenda-custom-commands)))
2353 (buf (current-buffer))
2354 (bfn (buffer-file-name (buffer-base-buffer)))
2355 entry key type match lprops ans)
2356 ;; Turn off restriction unless there is an overriding one,
2357 (unless org-agenda-overriding-restriction
2358 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2359 ;; There is a request to keep the file list in place
2360 (put 'org-agenda-files 'org-restrict nil))
2361 (setq org-agenda-restrict nil)
2362 (move-marker org-agenda-restrict-begin nil)
2363 (move-marker org-agenda-restrict-end nil))
2364 ;; Delete old local properties
2365 (put 'org-agenda-redo-command 'org-lprops nil)
2366 ;; Delete previously set last-arguments
2367 (put 'org-agenda-redo-command 'last-args nil)
2368 ;; Remember where this call originated
2369 (setq org-agenda-last-dispatch-buffer (current-buffer))
2370 (unless keys
2371 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2372 keys (car ans)
2373 restriction (cdr ans)))
2374 ;; If we have sticky agenda buffers, set a name for the buffer,
2375 ;; depending on the invoking keys. The user may still set this
2376 ;; as a command option, which will overwrite what we do here.
2377 (if org-agenda-sticky
2378 (setq org-agenda-buffer-name (format "*Org Agenda(%s)*" keys)))
2379 ;; Establish the restriction, if any
2380 (when (and (not org-agenda-overriding-restriction) restriction)
2381 (put 'org-agenda-files 'org-restrict (list bfn))
2382 (cond
2383 ((eq restriction 'region)
2384 (setq org-agenda-restrict t)
2385 (move-marker org-agenda-restrict-begin (region-beginning))
2386 (move-marker org-agenda-restrict-end (region-end)))
2387 ((eq restriction 'subtree)
2388 (save-excursion
2389 (setq org-agenda-restrict t)
2390 (org-back-to-heading t)
2391 (move-marker org-agenda-restrict-begin (point))
2392 (move-marker org-agenda-restrict-end
2393 (progn (org-end-of-subtree t)))))))
2395 ;; For example the todo list should not need it (but does...)
2396 (cond
2397 ((setq entry (assoc keys org-agenda-custom-commands))
2398 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2399 (progn
2400 (setq type (nth 2 entry) match (eval (nth 3 entry))
2401 lprops (nth 4 entry))
2402 (put 'org-agenda-redo-command 'org-lprops lprops)
2403 (cond
2404 ((eq type 'agenda)
2405 (org-let lprops '(org-agenda-list current-prefix-arg)))
2406 ((eq type 'alltodo)
2407 (org-let lprops '(org-todo-list current-prefix-arg)))
2408 ((eq type 'search)
2409 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2410 ((eq type 'stuck)
2411 (org-let lprops '(org-agenda-list-stuck-projects
2412 current-prefix-arg)))
2413 ((eq type 'tags)
2414 (org-let lprops '(org-tags-view current-prefix-arg match)))
2415 ((eq type 'tags-todo)
2416 (org-let lprops '(org-tags-view '(4) match)))
2417 ((eq type 'todo)
2418 (org-let lprops '(org-todo-list match)))
2419 ((eq type 'tags-tree)
2420 (org-check-for-org-mode)
2421 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2422 ((eq type 'todo-tree)
2423 (org-check-for-org-mode)
2424 (org-let lprops
2425 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2426 (regexp-quote match) "\\>"))))
2427 ((eq type 'occur-tree)
2428 (org-check-for-org-mode)
2429 (org-let lprops '(org-occur match)))
2430 ((functionp type)
2431 (org-let lprops '(funcall type match)))
2432 ((fboundp type)
2433 (org-let lprops '(funcall type match)))
2434 (t (error "Invalid custom agenda command type %s" type))))
2435 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2436 ((equal keys "C")
2437 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2438 (customize-variable 'org-agenda-custom-commands))
2439 ((equal keys "a") (call-interactively 'org-agenda-list))
2440 ((equal keys "s") (call-interactively 'org-search-view))
2441 ((equal keys "t") (call-interactively 'org-todo-list))
2442 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2443 ((equal keys "m") (call-interactively 'org-tags-view))
2444 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2445 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2446 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2447 (org-add-hook
2448 'post-command-hook
2449 (lambda ()
2450 (unless (current-message)
2451 (let* ((m (org-agenda-get-any-marker))
2452 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2453 (when note
2454 (message (concat
2455 "FLAGGING-NOTE ([?] for more info): "
2456 (org-add-props
2457 (replace-regexp-in-string
2458 "\\\\n" "//"
2459 (copy-sequence note))
2460 nil 'face 'org-warning)))))))
2461 t t))
2462 ((equal keys "L")
2463 (unless (derived-mode-p 'org-mode)
2464 (error "This is not an Org-mode file"))
2465 (unless restriction
2466 (put 'org-agenda-files 'org-restrict (list bfn))
2467 (org-call-with-arg 'org-timeline arg)))
2468 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2469 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2470 ((equal keys "!") (customize-variable 'org-stuck-projects))
2471 (t (error "Invalid agenda key"))))))
2473 (defun org-agenda-append-agenda ()
2474 "Append another agenda view to the current one.
2475 This function allows interactive building of block agendas.
2476 Agenda views are separated by `org-agenda-block-separator'."
2477 (interactive)
2478 (unless (string= (buffer-name) org-agenda-buffer-name)
2479 (error "Can only append from within agenda buffer"))
2480 (let ((org-agenda-multi t))
2481 (org-agenda)
2482 (widen)))
2484 (defun org-agenda-normalize-custom-commands (cmds)
2485 (delq nil
2486 (mapcar
2487 (lambda (x)
2488 (cond ((stringp (cdr x)) nil)
2489 ((stringp (nth 1 x)) x)
2490 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2491 (t (cons (car x) (cons "" (cdr x))))))
2492 cmds)))
2494 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2495 "The user interface for selecting an agenda command."
2496 (catch 'exit
2497 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2498 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2499 (region-p (org-region-active-p))
2500 (custom org-agenda-custom-commands)
2501 (selstring "")
2502 restriction second-time
2503 c entry key type match prefixes rmheader header-end custom1 desc
2504 line lines left right n n1)
2505 (save-window-excursion
2506 (delete-other-windows)
2507 (org-switch-to-buffer-other-window " *Agenda Commands*")
2508 (erase-buffer)
2509 (insert (eval-when-compile
2510 (let ((header
2511 "Press key for an agenda command: < Buffer, subtree/region restriction
2512 -------------------------------- > Remove restriction
2513 a Agenda for current week or day e Export agenda views
2514 t List of all TODO entries T Entries with special TODO kwd
2515 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2516 L Timeline for current buffer # List stuck projects (!=configure)
2517 s Search for keywords * Toggle sticky agenda views
2518 / Multi-occur ? Find :FLAGGED: entries
2519 C Configure custom agenda commands
2521 (start 0))
2522 (while (string-match
2523 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2524 header start)
2525 (setq start (match-end 0))
2526 (add-text-properties (match-beginning 2) (match-end 2)
2527 '(face bold) header))
2528 header)))
2529 (setq header-end (move-marker (make-marker) (point)))
2530 (while t
2531 (setq custom1 custom)
2532 (when (eq rmheader t)
2533 (org-goto-line 1)
2534 (re-search-forward ":" nil t)
2535 (delete-region (match-end 0) (point-at-eol))
2536 (forward-char 1)
2537 (looking-at "-+")
2538 (delete-region (match-end 0) (point-at-eol))
2539 (move-marker header-end (match-end 0)))
2540 (goto-char header-end)
2541 (delete-region (point) (point-max))
2543 ;; Produce all the lines that describe custom commands and prefixes
2544 (setq lines nil)
2545 (while (setq entry (pop custom1))
2546 (setq key (car entry) desc (nth 1 entry)
2547 type (nth 2 entry)
2548 match (nth 3 entry))
2549 (if (> (length key) 1)
2550 (add-to-list 'prefixes (string-to-char key))
2551 (setq line
2552 (format
2553 "%-4s%-14s"
2554 (org-add-props (copy-sequence key)
2555 '(face bold))
2556 (cond
2557 ((string-match "\\S-" desc) desc)
2558 ((eq type 'agenda) "Agenda for current week or day")
2559 ((eq type 'alltodo) "List of all TODO entries")
2560 ((eq type 'search) "Word search")
2561 ((eq type 'stuck) "List of stuck projects")
2562 ((eq type 'todo) "TODO keyword")
2563 ((eq type 'tags) "Tags query")
2564 ((eq type 'tags-todo) "Tags (TODO)")
2565 ((eq type 'tags-tree) "Tags tree")
2566 ((eq type 'todo-tree) "TODO kwd tree")
2567 ((eq type 'occur-tree) "Occur tree")
2568 ((functionp type) (if (symbolp type)
2569 (symbol-name type)
2570 "Lambda expression"))
2571 (t "???"))))
2572 (if org-agenda-menu-show-matcher
2573 (setq line
2574 (concat line ": "
2575 (cond
2576 ((stringp match)
2577 (setq match (copy-sequence match))
2578 (org-add-props match nil 'face 'org-warning))
2579 (match
2580 (format "set of %d commands" (length match)))
2581 (t ""))))
2582 (if (org-string-nw-p match)
2583 (add-text-properties
2584 0 (length line) (list 'help-echo
2585 (concat "Matcher: "match)) line)))
2586 (push line lines)))
2587 (setq lines (nreverse lines))
2588 (when prefixes
2589 (mapc (lambda (x)
2590 (push
2591 (format "%s %s"
2592 (org-add-props (char-to-string x)
2593 nil 'face 'bold)
2594 (or (cdr (assoc (concat selstring
2595 (char-to-string x))
2596 prefix-descriptions))
2597 "Prefix key"))
2598 lines))
2599 prefixes))
2601 ;; Check if we should display in two columns
2602 (if org-agenda-menu-two-column
2603 (progn
2604 (setq n (length lines)
2605 n1 (+ (/ n 2) (mod n 2))
2606 right (nthcdr n1 lines)
2607 left (copy-sequence lines))
2608 (setcdr (nthcdr (1- n1) left) nil))
2609 (setq left lines right nil))
2610 (while left
2611 (insert "\n" (pop left))
2612 (when right
2613 (if (< (current-column) 40)
2614 (move-to-column 40 t)
2615 (insert " "))
2616 (insert (pop right))))
2618 ;; Make the window the right size
2619 (goto-char (point-min))
2620 (if second-time
2621 (if (not (pos-visible-in-window-p (point-max)))
2622 (org-fit-window-to-buffer))
2623 (setq second-time t)
2624 (org-fit-window-to-buffer))
2626 ;; Ask for selection
2627 (message "Press key for agenda command%s:"
2628 (if (or restrict-ok org-agenda-overriding-restriction)
2629 (if org-agenda-overriding-restriction
2630 " (restriction lock active)"
2631 (if restriction
2632 (format " (restricted to %s)" restriction)
2633 " (unrestricted)"))
2634 ""))
2635 (setq c (read-char-exclusive))
2636 (message "")
2637 (cond
2638 ((assoc (char-to-string c) custom)
2639 (setq selstring (concat selstring (char-to-string c)))
2640 (throw 'exit (cons selstring restriction)))
2641 ((memq c prefixes)
2642 (setq selstring (concat selstring (char-to-string c))
2643 prefixes nil
2644 rmheader (or rmheader t)
2645 custom (delq nil (mapcar
2646 (lambda (x)
2647 (if (or (= (length (car x)) 1)
2648 (/= (string-to-char (car x)) c))
2650 (cons (substring (car x) 1) (cdr x))))
2651 custom))))
2652 ((eq c ?*)
2653 (org-toggle-sticky-agenda)
2654 (sit-for 2))
2655 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2656 (message "Restriction is only possible in Org-mode buffers")
2657 (ding) (sit-for 1))
2658 ((eq c ?1)
2659 (org-agenda-remove-restriction-lock 'noupdate)
2660 (setq restriction 'buffer))
2661 ((eq c ?0)
2662 (org-agenda-remove-restriction-lock 'noupdate)
2663 (setq restriction (if region-p 'region 'subtree)))
2664 ((eq c ?<)
2665 (org-agenda-remove-restriction-lock 'noupdate)
2666 (setq restriction
2667 (cond
2668 ((eq restriction 'buffer)
2669 (if region-p 'region 'subtree))
2670 ((memq restriction '(subtree region))
2671 nil)
2672 (t 'buffer))))
2673 ((eq c ?>)
2674 (org-agenda-remove-restriction-lock 'noupdate)
2675 (setq restriction nil))
2676 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2677 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2678 ((and (> (length selstring) 0) (eq c ?\d))
2679 (delete-window)
2680 (org-agenda-get-restriction-and-command prefix-descriptions))
2682 ((equal c ?q) (error "Abort"))
2683 (t (error "Invalid key %c" c))))))))
2685 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2686 (defvar org-agenda-last-arguments nil
2687 "The arguments of the previous call to `org-agenda'.")
2688 (defun org-agenda-run-series (name series)
2689 (org-let (nth 1 series) '(org-prepare-agenda name))
2690 ;; We need to reset agenda markers here, because when constructing a
2691 ;; block agenda, the individual blocks do not do that.
2692 (org-agenda-reset-markers)
2693 (let* ((org-agenda-multi t)
2694 (redo (list 'org-agenda-run-series name (list 'quote series)))
2695 (org-agenda-overriding-arguments
2696 (or org-agenda-overriding-arguments
2697 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2698 (get 'org-agenda-redo-command 'last-args))))
2699 (cmds (car series))
2700 (gprops (nth 1 series))
2701 match ;; The byte compiler incorrectly complains about this. Keep it!
2702 cmd type lprops)
2703 (while (setq cmd (pop cmds))
2704 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2705 (cond
2706 ((eq type 'agenda)
2707 (org-let2 gprops lprops
2708 '(call-interactively 'org-agenda-list)))
2709 ((eq type 'alltodo)
2710 (org-let2 gprops lprops
2711 '(call-interactively 'org-todo-list)))
2712 ((eq type 'search)
2713 (org-let2 gprops lprops
2714 '(org-search-view current-prefix-arg match nil)))
2715 ((eq type 'stuck)
2716 (org-let2 gprops lprops
2717 '(call-interactively 'org-agenda-list-stuck-projects)))
2718 ((eq type 'tags)
2719 (org-let2 gprops lprops
2720 '(org-tags-view current-prefix-arg match)))
2721 ((eq type 'tags-todo)
2722 (org-let2 gprops lprops
2723 '(org-tags-view '(4) match)))
2724 ((eq type 'todo)
2725 (org-let2 gprops lprops
2726 '(org-todo-list match)))
2727 ((fboundp type)
2728 (org-let2 gprops lprops
2729 '(funcall type match)))
2730 (t (error "Invalid type in command series"))))
2731 (widen)
2732 (setq org-agenda-redo-command redo)
2733 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2734 (goto-char (point-min)))
2735 (org-fit-agenda-window)
2736 (org-let (nth 1 series) '(org-finalize-agenda)))
2738 ;;;###autoload
2739 (defmacro org-batch-agenda (cmd-key &rest parameters)
2740 "Run an agenda command in batch mode and send the result to STDOUT.
2741 If CMD-KEY is a string of length 1, it is used as a key in
2742 `org-agenda-custom-commands' and triggers this command. If it is a
2743 longer string it is used as a tags/todo match string.
2744 Parameters are alternating variable names and values that will be bound
2745 before running the agenda command."
2746 (org-eval-in-environment (org-make-parameter-alist parameters)
2747 (if (> (length cmd-key) 2)
2748 (org-tags-view nil cmd-key)
2749 (org-agenda nil cmd-key)))
2750 (set-buffer org-agenda-buffer-name)
2751 (princ (buffer-string)))
2752 (def-edebug-spec org-batch-agenda (form &rest sexp))
2754 (defvar org-agenda-info nil)
2756 ;;;###autoload
2757 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2758 "Run an agenda command in batch mode and send the result to STDOUT.
2759 If CMD-KEY is a string of length 1, it is used as a key in
2760 `org-agenda-custom-commands' and triggers this command. If it is a
2761 longer string it is used as a tags/todo match string.
2762 Parameters are alternating variable names and values that will be bound
2763 before running the agenda command.
2765 The output gives a line for each selected agenda item. Each
2766 item is a list of comma-separated values, like this:
2768 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2770 category The category of the item
2771 head The headline, without TODO kwd, TAGS and PRIORITY
2772 type The type of the agenda entry, can be
2773 todo selected in TODO match
2774 tagsmatch selected in tags match
2775 diary imported from diary
2776 deadline a deadline on given date
2777 scheduled scheduled on given date
2778 timestamp entry has timestamp on given date
2779 closed entry was closed on given date
2780 upcoming-deadline warning about deadline
2781 past-scheduled forwarded scheduled item
2782 block entry has date block including g. date
2783 todo The todo keyword, if any
2784 tags All tags including inherited ones, separated by colons
2785 date The relevant date, like 2007-2-14
2786 time The time, like 15:00-16:50
2787 extra Sting with extra planning info
2788 priority-l The priority letter if any was given
2789 priority-n The computed numerical priority
2790 agenda-day The day in the agenda where this is listed"
2791 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2792 (org-make-parameter-alist parameters))
2793 (if (> (length cmd-key) 2)
2794 (org-tags-view nil cmd-key)
2795 (org-agenda nil cmd-key)))
2796 (set-buffer org-agenda-buffer-name)
2797 (let* ((lines (org-split-string (buffer-string) "\n"))
2798 line)
2799 (while (setq line (pop lines))
2800 (catch 'next
2801 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2802 (setq org-agenda-info
2803 (org-fix-agenda-info (text-properties-at 0 line)))
2804 (princ
2805 (mapconcat 'org-agenda-export-csv-mapper
2806 '(org-category txt type todo tags date time extra
2807 priority-letter priority agenda-day)
2808 ","))
2809 (princ "\n")))))
2810 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2812 (defun org-fix-agenda-info (props)
2813 "Make sure all properties on an agenda item have a canonical form.
2814 This ensures the export commands can easily use it."
2815 (let (tmp re)
2816 (when (setq tmp (plist-get props 'tags))
2817 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2818 (when (setq tmp (plist-get props 'date))
2819 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2820 (let ((calendar-date-display-form '(year "-" month "-" day)))
2821 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2823 (setq tmp (calendar-date-string tmp)))
2824 (setq props (plist-put props 'date tmp)))
2825 (when (setq tmp (plist-get props 'day))
2826 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2827 (let ((calendar-date-display-form '(year "-" month "-" day)))
2828 (setq tmp (calendar-date-string tmp)))
2829 (setq props (plist-put props 'day tmp))
2830 (setq props (plist-put props 'agenda-day tmp)))
2831 (when (setq tmp (plist-get props 'txt))
2832 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2833 (plist-put props 'priority-letter (match-string 1 tmp))
2834 (setq tmp (replace-match "" t t tmp)))
2835 (when (and (setq re (plist-get props 'org-todo-regexp))
2836 (setq re (concat "\\`\\.*" re " ?"))
2837 (string-match re tmp))
2838 (plist-put props 'todo (match-string 1 tmp))
2839 (setq tmp (replace-match "" t t tmp)))
2840 (plist-put props 'txt tmp)))
2841 props)
2843 (defun org-agenda-export-csv-mapper (prop)
2844 (let ((res (plist-get org-agenda-info prop)))
2845 (setq res
2846 (cond
2847 ((not res) "")
2848 ((stringp res) res)
2849 (t (prin1-to-string res))))
2850 (while (string-match "," res)
2851 (setq res (replace-match ";" t t res)))
2852 (org-trim res)))
2855 ;;;###autoload
2856 (defun org-store-agenda-views (&rest parameters)
2857 (interactive)
2858 (eval (list 'org-batch-store-agenda-views)))
2860 ;;;###autoload
2861 (defmacro org-batch-store-agenda-views (&rest parameters)
2862 "Run all custom agenda commands that have a file argument."
2863 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2864 (pop-up-frames nil)
2865 (dir default-directory)
2866 (pars (org-make-parameter-alist parameters))
2867 cmd thiscmdkey files opts cmd-or-set)
2868 (save-window-excursion
2869 (while cmds
2870 (setq cmd (pop cmds)
2871 thiscmdkey (car cmd)
2872 cmd-or-set (nth 2 cmd)
2873 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2874 files (nth (if (listp cmd-or-set) 4 5) cmd))
2875 (if (stringp files) (setq files (list files)))
2876 (when files
2877 (org-eval-in-environment (append org-agenda-exporter-settings
2878 opts pars)
2879 (org-agenda nil thiscmdkey))
2880 (set-buffer org-agenda-buffer-name)
2881 (while files
2882 (org-eval-in-environment (append org-agenda-exporter-settings
2883 opts pars)
2884 (org-agenda-write (expand-file-name (pop files) dir) nil t)))
2885 (and (get-buffer org-agenda-buffer-name)
2886 (kill-buffer org-agenda-buffer-name)))))))
2887 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2889 (defun org-agenda-mark-header-line (pos)
2890 "Mark the line at POS as an agenda structure header."
2891 (save-excursion
2892 (goto-char pos)
2893 (put-text-property (point-at-bol) (point-at-eol)
2894 'org-agenda-structural-header t)
2895 (when org-agenda-title-append
2896 (put-text-property (point-at-bol) (point-at-eol)
2897 'org-agenda-title-append org-agenda-title-append))))
2899 (defvar org-mobile-creating-agendas)
2900 (defvar org-agenda-write-buffer-name "Agenda View")
2901 (defun org-agenda-write (file &optional open nosettings)
2902 "Write the current buffer (an agenda view) as a file.
2903 Depending on the extension of the file name, plain text (.txt),
2904 HTML (.html or .htm) or Postscript (.ps) is produced.
2905 If the extension is .ics, run icalendar export over all files used
2906 to construct the agenda and limit the export to entries listed in the
2907 agenda now.
2908 With prefix argument OPEN, open the new file immediately.
2909 If NOSETTINGS is given, do not scope the settings of
2910 `org-agenda-exporter-settings' into the export commands. This is used when
2911 the settings have already been scoped and we do not wish to overrule other,
2912 higher priority settings."
2913 (interactive "FWrite agenda to file: \nP")
2914 (if (not (file-writable-p file))
2915 (error "Cannot write agenda to file %s" file))
2916 (org-let (if nosettings nil org-agenda-exporter-settings)
2917 '(save-excursion
2918 (save-window-excursion
2919 (org-agenda-mark-filtered-text)
2920 (let ((bs (copy-sequence (buffer-string))) beg)
2921 (org-agenda-unmark-filtered-text)
2922 (with-temp-buffer
2923 (rename-buffer org-agenda-write-buffer-name t)
2924 (set-buffer-modified-p nil)
2925 (insert bs)
2926 (org-agenda-remove-marked-text 'org-filtered)
2927 (while (setq beg (text-property-any (point-min) (point-max)
2928 'org-filtered t))
2929 (delete-region
2930 beg (or (next-single-property-change beg 'org-filtered)
2931 (point-max))))
2932 (run-hooks 'org-agenda-before-write-hook)
2933 (cond
2934 ((org-bound-and-true-p org-mobile-creating-agendas)
2935 (org-mobile-write-agenda-for-mobile file))
2936 ((string-match "\\.html?\\'" file)
2937 (require 'htmlize)
2938 (set-buffer (htmlize-buffer (current-buffer)))
2940 (when (and org-agenda-export-html-style
2941 (string-match "<style>" org-agenda-export-html-style))
2942 ;; replace <style> section with org-agenda-export-html-style
2943 (goto-char (point-min))
2944 (kill-region (- (search-forward "<style") 6)
2945 (search-forward "</style>"))
2946 (insert org-agenda-export-html-style))
2947 (write-file file)
2948 (kill-buffer (current-buffer))
2949 (message "HTML written to %s" file))
2950 ((string-match "\\.ps\\'" file)
2951 (require 'ps-print)
2952 (ps-print-buffer-with-faces file)
2953 (message "Postscript written to %s" file))
2954 ((string-match "\\.pdf\\'" file)
2955 (require 'ps-print)
2956 (ps-print-buffer-with-faces
2957 (concat (file-name-sans-extension file) ".ps"))
2958 (call-process "ps2pdf" nil nil nil
2959 (expand-file-name
2960 (concat (file-name-sans-extension file) ".ps"))
2961 (expand-file-name file))
2962 (delete-file (concat (file-name-sans-extension file) ".ps"))
2963 (message "PDF written to %s" file))
2964 ((string-match "\\.ics\\'" file)
2965 (require 'org-icalendar)
2966 (let ((org-agenda-marker-table
2967 (org-create-marker-find-array
2968 (org-agenda-collect-markers)))
2969 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2970 (org-combined-agenda-icalendar-file file))
2971 (apply 'org-export-icalendar 'combine
2972 (org-agenda-files nil 'ifmode))))
2974 (let ((bs (buffer-string)))
2975 (find-file file)
2976 (erase-buffer)
2977 (insert bs)
2978 (save-buffer 0)
2979 (kill-buffer (current-buffer))
2980 (message "Plain text written to %s" file))))))))
2981 (set-buffer org-agenda-buffer-name))
2982 (when open (org-open-file file)))
2984 (defvar org-agenda-tag-filter-overlays nil)
2985 (defvar org-agenda-cat-filter-overlays nil)
2987 (defun org-agenda-mark-filtered-text ()
2988 "Mark all text hidden by filtering with a text property."
2989 (let ((inhibit-read-only t))
2990 (mapc
2991 (lambda (o)
2992 (when (equal (overlay-buffer o) (current-buffer))
2993 (put-text-property
2994 (overlay-start o) (overlay-end o)
2995 'org-filtered t)))
2996 (append org-agenda-tag-filter-overlays
2997 org-agenda-cat-filter-overlays))))
2999 (defun org-agenda-unmark-filtered-text ()
3000 "Remove the filtering text property."
3001 (let ((inhibit-read-only t))
3002 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3004 (defun org-agenda-remove-marked-text (property &optional value)
3005 "Delete all text marked with VALUE of PROPERTY.
3006 VALUE defaults to t."
3007 (let (beg)
3008 (setq value (or value t))
3009 (while (setq beg (text-property-any (point-min) (point-max)
3010 property value))
3011 (delete-region
3012 beg (or (next-single-property-change beg 'org-filtered)
3013 (point-max))))))
3015 (defun org-agenda-add-entry-text ()
3016 "Add entry text to agenda lines.
3017 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3018 entry text following headings shown in the agenda.
3019 Drawers will be excluded, also the line with scheduling/deadline info."
3020 (when (and (> org-agenda-add-entry-text-maxlines 0)
3021 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3022 (let (m txt)
3023 (goto-char (point-min))
3024 (while (not (eobp))
3025 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3026 (beginning-of-line 2)
3027 (setq txt (org-agenda-get-some-entry-text
3028 m org-agenda-add-entry-text-maxlines " > "))
3029 (end-of-line 1)
3030 (if (string-match "\\S-" txt)
3031 (insert "\n" txt)
3032 (or (eobp) (forward-char 1))))))))
3034 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3035 &rest keep)
3036 "Extract entry text from MARKER, at most N-LINES lines.
3037 This will ignore drawers etc, just get the text.
3038 If INDENT is given, prefix every line with this string. If KEEP is
3039 given, it is a list of symbols, defining stuff that should not be
3040 removed from the entry content. Currently only `planning' is allowed here."
3041 (let (txt drawer-re kwd-time-re ind)
3042 (save-excursion
3043 (with-current-buffer (marker-buffer marker)
3044 (if (not (derived-mode-p 'org-mode))
3045 (setq txt "")
3046 (save-excursion
3047 (save-restriction
3048 (widen)
3049 (goto-char marker)
3050 (end-of-line 1)
3051 (setq txt (buffer-substring
3052 (min (1+ (point)) (point-max))
3053 (progn (outline-next-heading) (point)))
3054 drawer-re org-drawer-regexp
3055 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3056 ".*\n?"))
3057 (with-temp-buffer
3058 (insert txt)
3059 (when org-agenda-add-entry-text-descriptive-links
3060 (goto-char (point-min))
3061 (while (org-activate-bracket-links (point-max))
3062 (add-text-properties (match-beginning 0) (match-end 0)
3063 '(face org-link))))
3064 (goto-char (point-min))
3065 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3066 (set-text-properties (match-beginning 0) (match-end 0)
3067 nil))
3068 (goto-char (point-min))
3069 (while (re-search-forward drawer-re nil t)
3070 (delete-region
3071 (match-beginning 0)
3072 (progn (re-search-forward
3073 "^[ \t]*:END:.*\n?" nil 'move)
3074 (point))))
3075 (unless (member 'planning keep)
3076 (goto-char (point-min))
3077 (while (re-search-forward kwd-time-re nil t)
3078 (replace-match "")))
3079 (goto-char (point-min))
3080 (when org-agenda-entry-text-exclude-regexps
3081 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3082 (while (setq re (pop re-list))
3083 (goto-char (point-min))
3084 (while (re-search-forward re nil t)
3085 (replace-match "")))))
3086 (goto-char (point-max))
3087 (skip-chars-backward " \t\n")
3088 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3090 ;; find and remove min common indentation
3091 (goto-char (point-min))
3092 (untabify (point-min) (point-max))
3093 (setq ind (org-get-indentation))
3094 (while (not (eobp))
3095 (unless (looking-at "[ \t]*$")
3096 (setq ind (min ind (org-get-indentation))))
3097 (beginning-of-line 2))
3098 (goto-char (point-min))
3099 (while (not (eobp))
3100 (unless (looking-at "[ \t]*$")
3101 (move-to-column ind)
3102 (delete-region (point-at-bol) (point)))
3103 (beginning-of-line 2))
3105 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3107 (goto-char (point-min))
3108 (when indent
3109 (while (and (not (eobp)) (re-search-forward "^" nil t))
3110 (replace-match indent t t)))
3111 (goto-char (point-min))
3112 (while (looking-at "[ \t]*\n") (replace-match ""))
3113 (goto-char (point-max))
3114 (when (> (org-current-line)
3115 n-lines)
3116 (org-goto-line (1+ n-lines))
3117 (backward-char 1))
3118 (setq txt (buffer-substring (point-min) (point)))))))))
3119 txt))
3121 (defun org-agenda-collect-markers ()
3122 "Collect the markers pointing to entries in the agenda buffer."
3123 (let (m markers)
3124 (save-excursion
3125 (goto-char (point-min))
3126 (while (not (eobp))
3127 (when (setq m (or (org-get-at-bol 'org-hd-marker)
3128 (org-get-at-bol 'org-marker)))
3129 (push m markers))
3130 (beginning-of-line 2)))
3131 (nreverse markers)))
3133 (defun org-create-marker-find-array (marker-list)
3134 "Create a alist of files names with all marker positions in that file."
3135 (let (f tbl m a p)
3136 (while (setq m (pop marker-list))
3137 (setq p (marker-position m)
3138 f (buffer-file-name (or (buffer-base-buffer
3139 (marker-buffer m))
3140 (marker-buffer m))))
3141 (if (setq a (assoc f tbl))
3142 (push (marker-position m) (cdr a))
3143 (push (list f p) tbl)))
3144 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3145 tbl)))
3147 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3148 (defun org-check-agenda-marker-table ()
3149 "Check of the current entry is on the marker list."
3150 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3152 (and (setq a (assoc file org-agenda-marker-table))
3153 (save-match-data
3154 (save-excursion
3155 (org-back-to-heading t)
3156 (member (point) (cdr a)))))))
3158 (defun org-check-for-org-mode ()
3159 "Make sure current buffer is in org-mode. Error if not."
3160 (or (derived-mode-p 'org-mode)
3161 (error "Cannot execute org-mode agenda command on buffer in %s"
3162 major-mode)))
3164 (defun org-fit-agenda-window ()
3165 "Fit the window to the buffer size."
3166 (and (memq org-agenda-window-setup '(reorganize-frame))
3167 (fboundp 'fit-window-to-buffer)
3168 (org-fit-window-to-buffer
3170 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3171 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3173 ;;; Agenda prepare and finalize
3175 (defvar org-agenda-multi nil) ; dynamically scoped
3176 (defvar org-agenda-buffer-name "*Org Agenda*")
3177 (defvar org-pre-agenda-window-conf nil)
3178 (defvar org-agenda-columns-active nil)
3179 (defvar org-agenda-name nil)
3180 (defvar org-agenda-tag-filter nil)
3181 (defvar org-agenda-category-filter nil)
3182 (defvar org-agenda-tag-filter-while-redo nil)
3183 (defvar org-agenda-tag-filter-preset nil
3184 "A preset of the tags filter used for secondary agenda filtering.
3185 This must be a list of strings, each string must be a single tag preceded
3186 by \"+\" or \"-\".
3187 This variable should not be set directly, but agenda custom commands can
3188 bind it in the options section. The preset filter is a global property of
3189 the entire agenda view. In a block agenda, it will not work reliably to
3190 define a filter for one of the individual blocks. You need to set it in
3191 the global options and expect it to be applied to the entire view.")
3193 (defvar org-agenda-category-filter-preset nil
3194 "A preset of the category filter used for secondary agenda filtering.
3195 This must be a list of strings, each string must be a single category
3196 preceded by \"+\" or \"-\".
3197 This variable should not be set directly, but agenda custom commands can
3198 bind it in the options section. The preset filter is a global property of
3199 the entire agenda view. In a block agenda, it will not work reliably to
3200 define a filter for one of the individual blocks. You need to set it in
3201 the global options and expect it to be applied to the entire view.")
3204 (defun org-agenda-use-sticky-p ()
3205 "Return non-NIL if existing agenda buffer named
3206 `org-agenda-buffer-name' exists, and should be shown instead of
3207 generating a new one"
3208 (and
3209 ;; turned off by user
3210 org-agenda-sticky
3211 ;; For multi-agenda buffer already exists
3212 (not org-agenda-multi)
3213 ;; buffer found
3214 (get-buffer org-agenda-buffer-name)
3215 ;; C-u parameter is same as last call
3216 (with-current-buffer (get-buffer org-agenda-buffer-name)
3217 (and
3218 (equal current-prefix-arg
3219 org-agenda-last-prefix-arg)
3220 ;; In case user turned stickiness on, while having existing
3221 ;; Agenda buffer active, don't reuse that buffer, because it
3222 ;; does not have org variables local
3223 org-agenda-this-buffer-is-sticky))))
3225 (defun org-prepare-agenda-window (abuf)
3226 "Setup agenda buffer in the window"
3227 (let* ((awin (get-buffer-window abuf))
3228 wconf)
3229 (cond
3230 ((equal (current-buffer) abuf) nil)
3231 (awin (select-window awin))
3232 ((not (setq wconf (current-window-configuration))))
3233 ((equal org-agenda-window-setup 'current-window)
3234 (org-pop-to-buffer-same-window abuf))
3235 ((equal org-agenda-window-setup 'other-window)
3236 (org-switch-to-buffer-other-window abuf))
3237 ((equal org-agenda-window-setup 'other-frame)
3238 (switch-to-buffer-other-frame abuf))
3239 ((equal org-agenda-window-setup 'reorganize-frame)
3240 (delete-other-windows)
3241 (org-switch-to-buffer-other-window abuf)))
3242 ;; additional test in case agenda is invoked from within agenda
3243 ;; buffer via elisp link
3244 (unless (equal (current-buffer) abuf)
3245 (org-pop-to-buffer-same-window abuf))
3246 (setq org-pre-agenda-window-conf wconf)))
3248 (defun org-prepare-agenda (&optional name)
3249 (if (org-agenda-use-sticky-p)
3250 (progn
3251 ;; Popup existing buffer
3252 (org-prepare-agenda-window (get-buffer org-agenda-buffer-name))
3253 (message "Sticky Agenda buffer, use `r' to refresh")
3254 (throw 'exit nil))
3255 (setq org-todo-keywords-for-agenda nil)
3256 (setq org-drawers-for-agenda nil)
3257 (unless org-agenda-persistent-filter
3258 (setq org-agenda-tag-filter nil
3259 org-agenda-category-filter nil))
3260 (put 'org-agenda-tag-filter :preset-filter org-agenda-tag-filter-preset)
3261 (put 'org-agenda-category-filter :preset-filter
3262 org-agenda-category-filter-preset)
3263 (if org-agenda-multi
3264 (progn
3265 (setq buffer-read-only nil)
3266 (goto-char (point-max))
3267 (unless (or (bobp) org-agenda-compact-blocks
3268 (not org-agenda-block-separator))
3269 (insert "\n"
3270 (if (stringp org-agenda-block-separator)
3271 org-agenda-block-separator
3272 (make-string (window-width) org-agenda-block-separator))
3273 "\n"))
3274 (narrow-to-region (point) (point-max)))
3275 (setq org-done-keywords-for-agenda nil)
3277 ;; Setting any org variables that are in org-agenda-local-vars
3278 ;; list need to be done after the prepare call
3279 (org-prepare-agenda-window (get-buffer-create org-agenda-buffer-name))
3280 (setq buffer-read-only nil)
3281 (org-agenda-reset-markers)
3282 (let ((inhibit-read-only t)) (erase-buffer))
3283 (org-agenda-mode)
3284 (setq org-agenda-buffer (current-buffer))
3285 (setq org-agenda-contributing-files nil)
3286 (setq org-agenda-columns-active nil)
3287 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3288 (setq org-todo-keywords-for-agenda
3289 (org-uniquify org-todo-keywords-for-agenda))
3290 (setq org-done-keywords-for-agenda
3291 (org-uniquify org-done-keywords-for-agenda))
3292 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3293 (setq org-agenda-last-prefix-arg current-prefix-arg)
3294 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3295 (and name (not org-agenda-name)
3296 (org-set-local 'org-agenda-name name)))
3297 (setq buffer-read-only nil)))
3299 (defun org-finalize-agenda ()
3300 "Finishing touch for the agenda buffer, called just before displaying it."
3301 (unless org-agenda-multi
3302 (save-excursion
3303 (let ((inhibit-read-only t))
3304 (goto-char (point-min))
3305 (while (org-activate-bracket-links (point-max))
3306 (add-text-properties (match-beginning 0) (match-end 0)
3307 '(face org-link)))
3308 (org-agenda-align-tags)
3309 (unless org-agenda-with-colors
3310 (remove-text-properties (point-min) (point-max) '(face nil))))
3311 (if (and (boundp 'org-agenda-overriding-columns-format)
3312 org-agenda-overriding-columns-format)
3313 (org-set-local 'org-agenda-overriding-columns-format
3314 org-agenda-overriding-columns-format))
3315 (if (and (boundp 'org-agenda-view-columns-initially)
3316 org-agenda-view-columns-initially)
3317 (org-agenda-columns))
3318 (when org-agenda-fontify-priorities
3319 (org-agenda-fontify-priorities))
3320 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3321 (org-agenda-dim-blocked-tasks))
3322 (org-agenda-mark-clocking-task)
3323 (when org-agenda-entry-text-mode
3324 (org-agenda-entry-text-hide)
3325 (org-agenda-entry-text-show))
3326 (if (functionp 'org-habit-insert-consistency-graphs)
3327 (org-habit-insert-consistency-graphs))
3328 (run-hooks 'org-finalize-agenda-hook)
3329 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3330 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3331 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3332 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3333 (org-agenda-filter-apply org-agenda-category-filter 'category))
3334 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)
3337 (defun org-agenda-mark-clocking-task ()
3338 "Mark the current clock entry in the agenda if it is present."
3339 (mapc (lambda (o)
3340 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3341 (delete-overlay o)))
3342 (overlays-in (point-min) (point-max)))
3343 (when (marker-buffer org-clock-hd-marker)
3344 (save-excursion
3345 (goto-char (point-min))
3346 (let (s ov)
3347 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3348 (goto-char s)
3349 (when (equal (org-get-at-bol 'org-hd-marker)
3350 org-clock-hd-marker)
3351 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3352 (overlay-put ov 'type 'org-agenda-clocking)
3353 (overlay-put ov 'face 'org-agenda-clocking)
3354 (overlay-put ov 'help-echo
3355 "The clock is running in this item")))))))
3357 (defun org-agenda-fontify-priorities ()
3358 "Make highest priority lines bold, and lowest italic."
3359 (interactive)
3360 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3361 (delete-overlay o)))
3362 (overlays-in (point-min) (point-max)))
3363 (save-excursion
3364 (let ((inhibit-read-only t)
3365 b e p ov h l)
3366 (goto-char (point-min))
3367 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3368 (setq h (or (get-char-property (point) 'org-highest-priority)
3369 org-highest-priority)
3370 l (or (get-char-property (point) 'org-lowest-priority)
3371 org-lowest-priority)
3372 p (string-to-char (match-string 1))
3373 b (match-beginning 0)
3374 e (if (eq org-agenda-fontify-priorities 'cookies)
3375 (match-end 0)
3376 (point-at-eol))
3377 ov (make-overlay b e))
3378 (overlay-put
3379 ov 'face
3380 (cond ((org-face-from-face-or-color
3381 'priority nil
3382 (cdr (assoc p org-priority-faces))))
3383 ((and (listp org-agenda-fontify-priorities)
3384 (org-face-from-face-or-color
3385 'priority nil
3386 (cdr (assoc p org-agenda-fontify-priorities)))))
3387 ((equal p l) 'italic)
3388 ((equal p h) 'bold)))
3389 (overlay-put ov 'org-type 'org-priority)))))
3391 (defun org-agenda-dim-blocked-tasks ()
3392 "Dim currently blocked TODO's in the agenda display."
3393 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3394 (delete-overlay o)))
3395 (overlays-in (point-min) (point-max)))
3396 (save-excursion
3397 (let ((inhibit-read-only t)
3398 (org-depend-tag-blocked nil)
3399 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3400 org-blocked-by-checkboxes
3401 invis1 b e p ov h l)
3402 (goto-char (point-min))
3403 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3404 (and pos (goto-char (1+ pos))))
3405 (setq org-blocked-by-checkboxes nil invis1 invis)
3406 (let ((marker (org-get-at-bol 'org-hd-marker)))
3407 (when (and marker
3408 (with-current-buffer (marker-buffer marker)
3409 (save-excursion (goto-char marker)
3410 (org-entry-blocked-p))))
3411 (if org-blocked-by-checkboxes (setq invis1 nil))
3412 (setq b (if invis1
3413 (max (point-min) (1- (point-at-bol)))
3414 (point-at-bol))
3415 e (point-at-eol)
3416 ov (make-overlay b e))
3417 (if invis1
3418 (overlay-put ov 'invisible t)
3419 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3420 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3422 (defvar org-agenda-skip-function nil
3423 "Function to be called at each match during agenda construction.
3424 If this function returns nil, the current match should not be skipped.
3425 Otherwise, the function must return a position from where the search
3426 should be continued.
3427 This may also be a Lisp form, it will be evaluated.
3428 Never set this variable using `setq' or so, because then it will apply
3429 to all future agenda commands. If you do want a global skipping condition,
3430 use the option `org-agenda-skip-function-global' instead.
3431 The correct usage for `org-agenda-skip-function' is to bind it with
3432 `let' to scope it dynamically into the agenda-constructing command.
3433 A good way to set it is through options in `org-agenda-custom-commands'.")
3435 (defun org-agenda-skip ()
3436 "Throw to `:skip' in places that should be skipped.
3437 Also moves point to the end of the skipped region, so that search can
3438 continue from there."
3439 (let ((p (point-at-bol)) to)
3440 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3441 (get-text-property p :org-archived)
3442 (org-end-of-subtree t)
3443 (throw :skip t))
3444 (and org-agenda-skip-comment-trees
3445 (get-text-property p :org-comment)
3446 (org-end-of-subtree t)
3447 (throw :skip t))
3448 (if (equal (char-after p) ?#) (throw :skip t))
3449 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3450 (org-agenda-skip-eval org-agenda-skip-function)))
3451 (goto-char to)
3452 (throw :skip t))))
3454 (defun org-agenda-skip-eval (form)
3455 "If FORM is a function or a list, call (or eval) is and return result.
3456 `save-excursion' and `save-match-data' are wrapped around the call, so point
3457 and match data are returned to the previous state no matter what these
3458 functions do."
3459 (let (fp)
3460 (and form
3461 (or (setq fp (functionp form))
3462 (consp form))
3463 (save-excursion
3464 (save-match-data
3465 (if fp
3466 (funcall form)
3467 (eval form)))))))
3469 (defvar org-agenda-markers nil
3470 "List of all currently active markers created by `org-agenda'.")
3471 (defvar org-agenda-last-marker-time (org-float-time)
3472 "Creation time of the last agenda marker.")
3474 (defun org-agenda-new-marker (&optional pos)
3475 "Return a new agenda marker.
3476 Org-mode keeps a list of these markers and resets them when they are
3477 no longer in use."
3478 (let ((m (copy-marker (or pos (point)))))
3479 (setq org-agenda-last-marker-time (org-float-time))
3480 (if org-agenda-buffer
3481 (with-current-buffer org-agenda-buffer
3482 (push m org-agenda-markers))
3483 (push m org-agenda-markers))
3486 (defun org-agenda-reset-markers ()
3487 "Reset markers created by `org-agenda'."
3488 (while org-agenda-markers
3489 (move-marker (pop org-agenda-markers) nil)))
3491 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3492 "Save relative positions of markers in region.
3493 This check for agenda markers in all agenda buffers currently active."
3494 (dolist (buf (buffer-list))
3495 (with-current-buffer buf
3496 (when (eq major-mode 'org-agenda-mode)
3497 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3498 org-agenda-markers)))))
3500 ;;; Entry text mode
3502 (defun org-agenda-entry-text-show-here ()
3503 "Add some text from the entry as context to the current line."
3504 (let (m txt o)
3505 (setq m (org-get-at-bol 'org-hd-marker))
3506 (unless (marker-buffer m)
3507 (error "No marker points to an entry here"))
3508 (setq txt (concat "\n" (org-no-properties
3509 (org-agenda-get-some-entry-text
3510 m org-agenda-entry-text-maxlines " > "))))
3511 (when (string-match "\\S-" txt)
3512 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3513 (overlay-put o 'evaporate t)
3514 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3515 (overlay-put o 'after-string txt))))
3517 (defun org-agenda-entry-text-show ()
3518 "Add entry context for all agenda lines."
3519 (interactive)
3520 (save-excursion
3521 (goto-char (point-max))
3522 (beginning-of-line 1)
3523 (while (not (bobp))
3524 (when (org-get-at-bol 'org-hd-marker)
3525 (org-agenda-entry-text-show-here))
3526 (beginning-of-line 0))))
3528 (defun org-agenda-entry-text-hide ()
3529 "Remove any shown entry context."
3530 (delq nil
3531 (mapcar (lambda (o)
3532 (if (eq (overlay-get o 'org-overlay-type)
3533 'agenda-entry-content)
3534 (progn (delete-overlay o) t)))
3535 (overlays-in (point-min) (point-max)))))
3537 (defun org-agenda-get-day-face (date)
3538 "Return the face DATE should be displayed with."
3539 (or (and (functionp org-agenda-day-face-function)
3540 (funcall org-agenda-day-face-function date))
3541 (cond ((org-agenda-todayp date)
3542 'org-agenda-date-today)
3543 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3544 'org-agenda-date-weekend)
3545 (t 'org-agenda-date))))
3547 ;;; Agenda timeline
3549 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3551 (defun org-timeline (&optional dotodo)
3552 "Show a time-sorted view of the entries in the current org file.
3553 Only entries with a time stamp of today or later will be listed. With
3554 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3555 under the current date.
3556 If the buffer contains an active region, only check the region for
3557 dates."
3558 (interactive "P")
3559 (let* ((dopast t)
3560 (org-agenda-show-log-scoped org-agenda-show-log)
3561 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3562 (current-buffer))))
3563 (date (calendar-current-date))
3564 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3565 (end (if (org-region-active-p) (region-end) (point-max)))
3566 (day-numbers (org-get-all-dates
3567 beg end 'no-ranges
3568 t org-agenda-show-log-scoped ; always include today
3569 org-timeline-show-empty-dates))
3570 (org-deadline-warning-days 0)
3571 (org-agenda-only-exact-dates t)
3572 (today (org-today))
3573 (past t)
3574 args
3575 s e rtn d emptyp)
3576 (setq org-agenda-redo-command
3577 (list 'progn
3578 (list 'org-switch-to-buffer-other-window (current-buffer))
3579 (list 'org-timeline (list 'quote dotodo))))
3580 (if (not dopast)
3581 ;; Remove past dates from the list of dates.
3582 (setq day-numbers (delq nil (mapcar (lambda(x)
3583 (if (>= x today) x nil))
3584 day-numbers))))
3585 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3586 (org-compile-prefix-format 'timeline)
3587 (org-set-sorting-strategy 'timeline)
3588 (if org-agenda-show-log-scoped (push :closed args))
3589 (push :timestamp args)
3590 (push :deadline args)
3591 (push :scheduled args)
3592 (push :sexp args)
3593 (if dotodo (push :todo args))
3594 (insert "Timeline of file " entry "\n")
3595 (add-text-properties (point-min) (point)
3596 (list 'face 'org-agenda-structure))
3597 (org-agenda-mark-header-line (point-min))
3598 (while (setq d (pop day-numbers))
3599 (if (and (listp d) (eq (car d) :omitted))
3600 (progn
3601 (setq s (point))
3602 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3603 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3604 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3605 (if (and (>= d today)
3606 dopast
3607 past)
3608 (progn
3609 (setq past nil)
3610 (insert (make-string 79 ?-) "\n")))
3611 (setq date (calendar-gregorian-from-absolute d))
3612 (setq s (point))
3613 (setq rtn (and (not emptyp)
3614 (apply 'org-agenda-get-day-entries entry
3615 date args)))
3616 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3617 (progn
3618 (insert
3619 (if (stringp org-agenda-format-date)
3620 (format-time-string org-agenda-format-date
3621 (org-time-from-absolute date))
3622 (funcall org-agenda-format-date date))
3623 "\n")
3624 (put-text-property s (1- (point)) 'face
3625 (org-agenda-get-day-face date))
3626 (put-text-property s (1- (point)) 'org-date-line t)
3627 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3628 (if (equal d today)
3629 (put-text-property s (1- (point)) 'org-today t))
3630 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3631 (put-text-property s (1- (point)) 'day d)))))
3632 (goto-char (point-min))
3633 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3634 (point-min)))
3635 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3636 (org-finalize-agenda)
3637 (setq buffer-read-only t)))
3639 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3640 "Return a list of all relevant day numbers from BEG to END buffer positions.
3641 If NO-RANGES is non-nil, include only the start and end dates of a range,
3642 not every single day in the range. If FORCE-TODAY is non-nil, make
3643 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3644 inactive time stamps (those in square brackets) are included.
3645 When EMPTY is non-nil, also include days without any entries."
3646 (let ((re (concat
3647 (if pre-re pre-re "")
3648 (if inactive org-ts-regexp-both org-ts-regexp)))
3649 dates dates1 date day day1 day2 ts1 ts2 pos)
3650 (if force-today
3651 (setq dates (list (org-today))))
3652 (save-excursion
3653 (goto-char beg)
3654 (while (re-search-forward re end t)
3655 (setq day (time-to-days (org-time-string-to-time
3656 (substring (match-string 1) 0 10)
3657 (current-buffer) (match-beginning 0))))
3658 (or (memq day dates) (push day dates)))
3659 (unless no-ranges
3660 (goto-char beg)
3661 (while (re-search-forward org-tr-regexp end t)
3662 (setq pos (match-beginning 0))
3663 (setq ts1 (substring (match-string 1) 0 10)
3664 ts2 (substring (match-string 2) 0 10)
3665 day1 (time-to-days (org-time-string-to-time
3666 ts1 (current-buffer) pos))
3667 day2 (time-to-days (org-time-string-to-time
3668 ts2 (current-buffer) pos)))
3669 (while (< (setq day1 (1+ day1)) day2)
3670 (or (memq day1 dates) (push day1 dates)))))
3671 (setq dates (sort dates '<))
3672 (when empty
3673 (while (setq day (pop dates))
3674 (setq day2 (car dates))
3675 (push day dates1)
3676 (when (and day2 empty)
3677 (if (or (eq empty t)
3678 (and (numberp empty) (<= (- day2 day) empty)))
3679 (while (< (setq day (1+ day)) day2)
3680 (push (list day) dates1))
3681 (push (cons :omitted (- day2 day)) dates1))))
3682 (setq dates (nreverse dates1)))
3683 dates)))
3685 ;;; Agenda Daily/Weekly
3687 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3688 "Start day for the agenda view.
3689 Custom commands can set this variable in the options section.")
3690 (defvar org-starting-day nil) ; local variable in the agenda buffer
3691 (defvar org-agenda-current-span nil
3692 "The current span used in the agenda view.") ; local variable in the agenda buffer
3693 (defvar org-arg-loc nil) ; local variable
3695 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3696 "List of types searched for when creating the daily/weekly agenda.
3697 This variable is a list of symbols that controls the types of
3698 items that appear in the daily/weekly agenda. Allowed symbols in this
3699 list are are
3701 :timestamp List items containing a date stamp or date range matching
3702 the selected date. This includes sexp entries in
3703 angular brackets.
3705 :sexp List entries resulting from plain diary-like sexps.
3707 :deadline List deadline due on that date. When the date is today,
3708 also list any deadlines past due, or due within
3709 `org-deadline-warning-days'. `:deadline' must appear before
3710 `:scheduled' if the setting of
3711 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3712 any effect.
3714 :scheduled List all items which are scheduled for the given date.
3715 The diary for *today* also contains items which were
3716 scheduled earlier and are not yet marked DONE.
3718 By default, all four types are turned on.
3720 Never set this variable globally using `setq', because then it
3721 will apply to all future agenda commands. Instead, bind it with
3722 `let' to scope it dynamically into the agenda-constructing
3723 command. A good way to set it is through options in
3724 `org-agenda-custom-commands'. For a more flexible (though
3725 somewhat less efficient) way of determining what is included in
3726 the daily/weekly agenda, see `org-agenda-skip-function'.")
3728 ;;;###autoload
3729 (defun org-agenda-list (&optional arg start-day span)
3730 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3731 The view will be for the current day or week, but from the overview buffer
3732 you will be able to go to other days/weeks.
3734 With a numeric prefix argument in an interactive call, the agenda will
3735 span ARG days. Lisp programs should instead specify SPAN to change
3736 the number of days. SPAN defaults to `org-agenda-span'.
3738 START-DAY defaults to TODAY, or to the most recent match for the weekday
3739 given in `org-agenda-start-on-weekday'."
3740 (interactive "P")
3741 (if (and (integerp arg) (> arg 0))
3742 (setq span arg arg nil))
3743 (org-prepare-agenda "Day/Week")
3744 (setq start-day (or start-day org-agenda-start-day))
3745 (if org-agenda-overriding-arguments
3746 (setq arg (car org-agenda-overriding-arguments)
3747 start-day (nth 1 org-agenda-overriding-arguments)
3748 span (nth 2 org-agenda-overriding-arguments)))
3749 (if (stringp start-day)
3750 ;; Convert to an absolute day number
3751 (setq start-day (time-to-days (org-read-date nil t start-day))))
3752 (setq org-agenda-last-arguments (list arg start-day span))
3753 (org-compile-prefix-format 'agenda)
3754 (org-set-sorting-strategy 'agenda)
3755 (let* ((span (org-agenda-ndays-to-span
3756 (or span org-agenda-ndays org-agenda-span)))
3757 (today (org-today))
3758 (sd (or start-day today))
3759 (ndays (org-agenda-span-to-ndays span sd))
3760 (org-agenda-start-on-weekday
3761 (if (eq ndays 7)
3762 org-agenda-start-on-weekday))
3763 (thefiles (org-agenda-files nil 'ifmode))
3764 (files thefiles)
3765 (start (if (or (null org-agenda-start-on-weekday)
3766 (< ndays 7))
3768 (let* ((nt (calendar-day-of-week
3769 (calendar-gregorian-from-absolute sd)))
3770 (n1 org-agenda-start-on-weekday)
3771 (d (- nt n1)))
3772 (- sd (+ (if (< d 0) 7 0) d)))))
3773 (day-numbers (list start))
3774 (day-cnt 0)
3775 (inhibit-redisplay (not debug-on-error))
3776 (org-agenda-show-log-scoped org-agenda-show-log)
3777 s e rtn rtnall file date d start-pos end-pos todayp
3778 clocktable-start clocktable-end filter)
3779 (setq org-agenda-redo-command
3780 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3781 (dotimes (n (1- ndays))
3782 (push (1+ (car day-numbers)) day-numbers))
3783 (setq day-numbers (nreverse day-numbers))
3784 (setq clocktable-start (car day-numbers)
3785 clocktable-end (1+ (or (org-last day-numbers) 0)))
3786 (org-set-local 'org-starting-day (car day-numbers))
3787 (org-set-local 'org-arg-loc arg)
3788 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3789 (unless org-agenda-compact-blocks
3790 (let* ((d1 (car day-numbers))
3791 (d2 (org-last day-numbers))
3792 (w1 (org-days-to-iso-week d1))
3793 (w2 (org-days-to-iso-week d2)))
3794 (setq s (point))
3795 (if org-agenda-overriding-header
3796 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3797 nil 'face 'org-agenda-structure) "\n")
3798 (insert (org-agenda-span-name span)
3799 "-agenda"
3800 (if (< (- d2 d1) 350)
3801 (if (= w1 w2)
3802 (format " (W%02d)" w1)
3803 (format " (W%02d-W%02d)" w1 w2))
3805 ":\n")))
3806 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3807 'org-date-line t))
3808 (org-agenda-mark-header-line s))
3809 (while (setq d (pop day-numbers))
3810 (setq date (calendar-gregorian-from-absolute d)
3811 s (point))
3812 (if (or (setq todayp (= d today))
3813 (and (not start-pos) (= d sd)))
3814 (setq start-pos (point))
3815 (if (and start-pos (not end-pos))
3816 (setq end-pos (point))))
3817 (setq files thefiles
3818 rtnall nil)
3819 (while (setq file (pop files))
3820 (catch 'nextfile
3821 (org-check-agenda-file file)
3822 (let ((org-agenda-entry-types org-agenda-entry-types))
3823 (unless org-agenda-include-deadlines
3824 (setq org-agenda-entry-types
3825 (delq :deadline org-agenda-entry-types)))
3826 (cond
3827 ((memq org-agenda-show-log-scoped '(only clockcheck))
3828 (setq rtn (org-agenda-get-day-entries
3829 file date :closed)))
3830 (org-agenda-show-log-scoped
3831 (setq rtn (apply 'org-agenda-get-day-entries
3832 file date
3833 (append '(:closed) org-agenda-entry-types))))
3835 (setq rtn (apply 'org-agenda-get-day-entries
3836 file date
3837 org-agenda-entry-types)))))
3838 (setq rtnall (append rtnall rtn)))) ;; all entries
3839 (if org-agenda-include-diary
3840 (let ((org-agenda-search-headline-for-time t))
3841 (require 'diary-lib)
3842 (setq rtn (org-get-entries-from-diary date))
3843 (setq rtnall (append rtnall rtn))))
3844 (if (or rtnall org-agenda-show-all-dates)
3845 (progn
3846 (setq day-cnt (1+ day-cnt))
3847 (insert
3848 (if (stringp org-agenda-format-date)
3849 (format-time-string org-agenda-format-date
3850 (org-time-from-absolute date))
3851 (funcall org-agenda-format-date date))
3852 "\n")
3853 (put-text-property s (1- (point)) 'face
3854 (org-agenda-get-day-face date))
3855 (put-text-property s (1- (point)) 'org-date-line t)
3856 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3857 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3858 (when todayp
3859 (put-text-property s (1- (point)) 'org-today t))
3860 (if rtnall (insert ;; all entries
3861 (org-finalize-agenda-entries
3862 (org-agenda-add-time-grid-maybe
3863 rtnall ndays todayp))
3864 "\n"))
3865 (put-text-property s (1- (point)) 'day d)
3866 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3867 (when (and org-agenda-clockreport-mode clocktable-start)
3868 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3869 ;; the above line is to ensure the restricted range!
3870 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3871 tbl)
3872 (setq p (org-plist-delete p :block))
3873 (setq p (plist-put p :tstart clocktable-start))
3874 (setq p (plist-put p :tend clocktable-end))
3875 (setq p (plist-put p :scope 'agenda))
3876 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3877 (setq filter (or org-agenda-tag-filter-while-redo
3878 (get 'org-agenda-tag-filter :preset-filter))))
3879 (setq p (plist-put p :tags (mapconcat (lambda (x)
3880 (if (string-match "[<>=]" x)
3883 filter ""))))
3884 (setq tbl (apply 'org-get-clocktable p))
3885 (insert tbl)))
3886 (goto-char (point-min))
3887 (or org-agenda-multi (org-fit-agenda-window))
3888 (unless (and (pos-visible-in-window-p (point-min))
3889 (pos-visible-in-window-p (point-max)))
3890 (goto-char (1- (point-max)))
3891 (recenter -1)
3892 (if (not (pos-visible-in-window-p (or start-pos 1)))
3893 (progn
3894 (goto-char (or start-pos 1))
3895 (recenter 1))))
3896 (goto-char (or start-pos 1))
3897 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3898 (if (eq org-agenda-show-log-scoped 'clockcheck)
3899 (org-agenda-show-clocking-issues))
3900 (org-finalize-agenda)
3901 (setq buffer-read-only t)
3902 (message "")))
3904 (defun org-agenda-ndays-to-span (n)
3905 "Return a span symbol for a span of N days, or N if none matches."
3906 (cond ((symbolp n) n)
3907 ((= n 1) 'day)
3908 ((= n 7) 'week)
3909 (t n)))
3911 (defun org-agenda-span-to-ndays (span start-day)
3912 "Return ndays from SPAN starting at START-DAY."
3913 (cond ((numberp span) span)
3914 ((eq span 'day) 1)
3915 ((eq span 'week) 7)
3916 ((eq span 'month)
3917 (let ((date (calendar-gregorian-from-absolute start-day)))
3918 (calendar-last-day-of-month (car date) (caddr date))))
3919 ((eq span 'year)
3920 (let ((date (calendar-gregorian-from-absolute start-day)))
3921 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3923 (defun org-agenda-span-name (span)
3924 "Return a SPAN name."
3925 (if (null span)
3927 (if (symbolp span)
3928 (capitalize (symbol-name span))
3929 (format "%d days" span))))
3931 ;;; Agenda word search
3933 (defvar org-agenda-search-history nil)
3934 (defvar org-todo-only nil)
3936 (defvar org-search-syntax-table nil
3937 "Special syntax table for org-mode search.
3938 In this table, we have single quotes not as word constituents, to
3939 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3941 (defun org-search-syntax-table ()
3942 (unless org-search-syntax-table
3943 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3944 (modify-syntax-entry ?' "." org-search-syntax-table)
3945 (modify-syntax-entry ?` "." org-search-syntax-table))
3946 org-search-syntax-table)
3948 (defvar org-agenda-last-search-view-search-was-boolean nil)
3950 ;;;###autoload
3951 (defun org-search-view (&optional todo-only string edit-at)
3952 "Show all entries that contain a phrase or words or regular expressions.
3954 With optional prefix argument TODO-ONLY, only consider entries that are
3955 TODO entries. The argument STRING can be used to pass a default search
3956 string into this function. If EDIT-AT is non-nil, it means that the
3957 user should get a chance to edit this string, with cursor at position
3958 EDIT-AT.
3960 The search string can be viewed either as a phrase that should be found as
3961 is, or it can be broken into a number of snippets, each of which must match
3962 in a Boolean way to select an entry. The default depends on the variable
3963 `org-agenda-search-view-always-boolean'.
3964 Even if this is turned off (the default) you can always switch to
3965 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3967 The default is a direct search of the whole phrase, where each space in
3968 the search string can expand to an arbitrary amount of whitespace,
3969 including newlines.
3971 If using a Boolean search, the search string is split on whitespace and
3972 each snippet is searched separately, with logical AND to select an entry.
3973 Words prefixed with a minus must *not* occur in the entry. Words without
3974 a prefix or prefixed with a plus must occur in the entry. Matching is
3975 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3976 match whole words, not parts of a word) if
3977 `org-agenda-search-view-force-full-words' is set (default is nil).
3979 Boolean search snippets enclosed by curly braces are interpreted as
3980 regular expressions that must or (when preceded with \"-\") must not
3981 match in the entry. Snippets enclosed into double quotes will be taken
3982 as a whole, to include whitespace.
3984 - If the search string starts with an asterisk, search only in headlines.
3985 - If (possibly after the leading star) the search string starts with an
3986 exclamation mark, this also means to look at TODO entries only, an effect
3987 that can also be achieved with a prefix argument.
3988 - If (possibly after star and exclamation mark) the search string starts
3989 with a colon, this will mean that the (non-regexp) snippets of the
3990 Boolean search must match as full words.
3992 This command searches the agenda files, and in addition the files listed
3993 in `org-agenda-text-search-extra-files'."
3994 (interactive "P")
3995 (org-prepare-agenda "SEARCH")
3996 (org-compile-prefix-format 'search)
3997 (org-set-sorting-strategy 'search)
3998 (let* ((props (list 'face nil
3999 'done-face 'org-agenda-done
4000 'org-not-done-regexp org-not-done-regexp
4001 'org-todo-regexp org-todo-regexp
4002 'org-complex-heading-regexp org-complex-heading-regexp
4003 'mouse-face 'highlight
4004 'help-echo (format "mouse-2 or RET jump to location")))
4005 (full-words org-agenda-search-view-force-full-words)
4006 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4007 regexp rtn rtnall files file pos
4008 marker category org-category-pos tags c neg re boolean
4009 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4010 (unless (and (not edit-at)
4011 (stringp string)
4012 (string-match "\\S-" string))
4013 (setq string (read-string
4014 (if org-agenda-search-view-always-boolean
4015 "[+-]Word/{Regexp} ...: "
4016 "Phrase, or [+-]Word/{Regexp} ...: ")
4017 (cond
4018 ((integerp edit-at) (cons string edit-at))
4019 (edit-at string))
4020 'org-agenda-search-history)))
4021 (org-set-local 'org-todo-only todo-only)
4022 (setq org-agenda-redo-command
4023 (list 'org-search-view (if todo-only t nil) string
4024 '(if current-prefix-arg 1 nil)))
4025 (setq org-agenda-query-string string)
4027 (if (equal (string-to-char string) ?*)
4028 (setq hdl-only t
4029 words (substring string 1))
4030 (setq words string))
4031 (when (equal (string-to-char words) ?!)
4032 (setq todo-only t
4033 words (substring words 1)))
4034 (when (equal (string-to-char words) ?:)
4035 (setq full-words t
4036 words (substring words 1)))
4037 (if (or org-agenda-search-view-always-boolean
4038 (member (string-to-char words) '(?- ?+ ?\{)))
4039 (setq boolean t))
4040 (setq words (org-split-string words))
4041 (let (www w)
4042 (while (setq w (pop words))
4043 (while (and (string-match "\\\\\\'" w) words)
4044 (setq w (concat (substring w 0 -1) " " (pop words))))
4045 (push w www))
4046 (setq words (nreverse www) www nil)
4047 (while (setq w (pop words))
4048 (when (and (string-match "\\`[-+]?{" w)
4049 (not (string-match "}\\'" w)))
4050 (while (and words (not (string-match "}\\'" (car words))))
4051 (setq w (concat w " " (pop words))))
4052 (setq w (concat w " " (pop words))))
4053 (push w www))
4054 (setq words (nreverse www)))
4055 (setq org-agenda-last-search-view-search-was-boolean boolean)
4056 (when boolean
4057 (let (wds w)
4058 (while (setq w (pop words))
4059 (if (or (equal (substring w 0 1) "\"")
4060 (and (> (length w) 1)
4061 (member (substring w 0 1) '("+" "-"))
4062 (equal (substring w 1 2) "\"")))
4063 (while (and words (not (equal (substring w -1) "\"")))
4064 (setq w (concat w " " (pop words)))))
4065 (and (string-match "\\`\\([-+]?\\)\"" w)
4066 (setq w (replace-match "\\1" nil nil w)))
4067 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4068 (push w wds))
4069 (setq words (nreverse wds))))
4070 (if boolean
4071 (mapc (lambda (w)
4072 (setq c (string-to-char w))
4073 (if (equal c ?-)
4074 (setq neg t w (substring w 1))
4075 (if (equal c ?+)
4076 (setq neg nil w (substring w 1))
4077 (setq neg nil)))
4078 (if (string-match "\\`{.*}\\'" w)
4079 (setq re (substring w 1 -1))
4080 (if full-words
4081 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4082 (setq re (regexp-quote (downcase w)))))
4083 (if neg (push re regexps-) (push re regexps+)))
4084 words)
4085 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4086 regexps+))
4087 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4088 (if (not regexps+)
4089 (setq regexp org-outline-regexp-bol)
4090 (setq regexp (pop regexps+))
4091 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4092 regexp))))
4093 (setq files (org-agenda-files nil 'ifmode))
4094 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4095 (pop org-agenda-text-search-extra-files)
4096 (setq files (org-add-archive-files files)))
4097 (setq files (append files org-agenda-text-search-extra-files)
4098 rtnall nil)
4099 (while (setq file (pop files))
4100 (setq ee nil)
4101 (catch 'nextfile
4102 (org-check-agenda-file file)
4103 (setq buffer (if (file-exists-p file)
4104 (org-get-agenda-file-buffer file)
4105 (error "No such file %s" file)))
4106 (if (not buffer)
4107 ;; If file does not exist, make sure an error message is sent
4108 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4109 file))))
4110 (with-current-buffer buffer
4111 (with-syntax-table (org-search-syntax-table)
4112 (unless (derived-mode-p 'org-mode)
4113 (error "Agenda file %s is not in `org-mode'" file))
4114 (let ((case-fold-search t))
4115 (save-excursion
4116 (save-restriction
4117 (if org-agenda-restrict
4118 (narrow-to-region org-agenda-restrict-begin
4119 org-agenda-restrict-end)
4120 (widen))
4121 (goto-char (point-min))
4122 (unless (or (org-at-heading-p)
4123 (outline-next-heading))
4124 (throw 'nextfile t))
4125 (goto-char (max (point-min) (1- (point))))
4126 (while (re-search-forward regexp nil t)
4127 (org-back-to-heading t)
4128 (skip-chars-forward "* ")
4129 (setq beg (point-at-bol)
4130 beg1 (point)
4131 end (progn (outline-next-heading) (point)))
4132 (catch :skip
4133 (goto-char beg)
4134 (org-agenda-skip)
4135 (setq str (buffer-substring-no-properties
4136 (point-at-bol)
4137 (if hdl-only (point-at-eol) end)))
4138 (mapc (lambda (wr) (when (string-match wr str)
4139 (goto-char (1- end))
4140 (throw :skip t)))
4141 regexps-)
4142 (mapc (lambda (wr) (unless (string-match wr str)
4143 (goto-char (1- end))
4144 (throw :skip t)))
4145 (if todo-only
4146 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4147 regexps+)
4148 regexps+))
4149 (goto-char beg)
4150 (setq marker (org-agenda-new-marker (point))
4151 category (org-get-category)
4152 org-category-pos (get-text-property (point) 'org-category-position)
4153 tags (org-get-tags-at (point))
4154 txt (org-agenda-format-item
4156 (buffer-substring-no-properties
4157 beg1 (point-at-eol))
4158 category tags))
4159 (org-add-props txt props
4160 'org-marker marker 'org-hd-marker marker
4161 'org-todo-regexp org-todo-regexp
4162 'org-complex-heading-regexp org-complex-heading-regexp
4163 'priority 1000 'org-category category
4164 'org-category-position org-category-pos
4165 'type "search")
4166 (push txt ee)
4167 (goto-char (1- end))))))))))
4168 (setq rtn (nreverse ee))
4169 (setq rtnall (append rtnall rtn)))
4170 (if org-agenda-overriding-header
4171 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4172 nil 'face 'org-agenda-structure) "\n")
4173 (insert "Search words: ")
4174 (add-text-properties (point-min) (1- (point))
4175 (list 'face 'org-agenda-structure))
4176 (setq pos (point))
4177 (insert string "\n")
4178 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4179 (setq pos (point))
4180 (unless org-agenda-multi
4181 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4182 (add-text-properties pos (1- (point))
4183 (list 'face 'org-agenda-structure))))
4184 (org-agenda-mark-header-line (point-min))
4185 (when rtnall
4186 (insert (org-finalize-agenda-entries rtnall) "\n"))
4187 (goto-char (point-min))
4188 (or org-agenda-multi (org-fit-agenda-window))
4189 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
4190 (org-finalize-agenda)
4191 (setq buffer-read-only t)))
4193 ;;; Agenda TODO list
4195 (defvar org-select-this-todo-keyword nil)
4196 (defvar org-last-arg nil)
4198 ;;;###autoload
4199 (defun org-todo-list (arg)
4200 "Show all (not done) TODO entries from all agenda file in a single list.
4201 The prefix arg can be used to select a specific TODO keyword and limit
4202 the list to these. When using \\[universal-argument], you will be prompted
4203 for a keyword. A numeric prefix directly selects the Nth keyword in
4204 `org-todo-keywords-1'."
4205 (interactive "P")
4206 (org-prepare-agenda "TODO")
4207 (org-compile-prefix-format 'todo)
4208 (org-set-sorting-strategy 'todo)
4209 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4210 (let* ((today (org-today))
4211 (date (calendar-gregorian-from-absolute today))
4212 (kwds org-todo-keywords-for-agenda)
4213 (completion-ignore-case t)
4214 (org-select-this-todo-keyword
4215 (if (stringp arg) arg
4216 (and arg (integerp arg) (> arg 0)
4217 (nth (1- arg) kwds))))
4218 rtn rtnall files file pos)
4219 (when (equal arg '(4))
4220 (setq org-select-this-todo-keyword
4221 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4222 (mapcar 'list kwds) nil nil)))
4223 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4224 (org-set-local 'org-last-arg arg)
4225 (setq org-agenda-redo-command
4226 '(org-todo-list (or current-prefix-arg org-last-arg)))
4227 (setq files (org-agenda-files nil 'ifmode)
4228 rtnall nil)
4229 (while (setq file (pop files))
4230 (catch 'nextfile
4231 (org-check-agenda-file file)
4232 (setq rtn (org-agenda-get-day-entries file date :todo))
4233 (setq rtnall (append rtnall rtn))))
4234 (if org-agenda-overriding-header
4235 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4236 nil 'face 'org-agenda-structure) "\n")
4237 (insert "Global list of TODO items of type: ")
4238 (add-text-properties (point-min) (1- (point))
4239 (list 'face 'org-agenda-structure
4240 'short-heading
4241 (concat "ToDo: "
4242 (or org-select-this-todo-keyword "ALL"))))
4243 (org-agenda-mark-header-line (point-min))
4244 (setq pos (point))
4245 (insert (or org-select-this-todo-keyword "ALL") "\n")
4246 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4247 (setq pos (point))
4248 (unless org-agenda-multi
4249 (insert "Available with `N r': (0)ALL")
4250 (let ((n 0) s)
4251 (mapc (lambda (x)
4252 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4253 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4254 (insert "\n "))
4255 (insert " " s))
4256 kwds))
4257 (insert "\n"))
4258 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4259 (org-agenda-mark-header-line (point-min))
4260 (when rtnall
4261 (insert (org-finalize-agenda-entries rtnall) "\n"))
4262 (goto-char (point-min))
4263 (or org-agenda-multi (org-fit-agenda-window))
4264 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4265 (org-finalize-agenda)
4266 (setq buffer-read-only t)))
4268 ;;; Agenda tags match
4270 ;;;###autoload
4271 (defun org-tags-view (&optional todo-only match)
4272 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4273 The prefix arg TODO-ONLY limits the search to TODO entries."
4274 (interactive "P")
4275 (let* ((org-tags-match-list-sublevels
4276 org-tags-match-list-sublevels)
4277 (completion-ignore-case t)
4278 rtn rtnall files file pos matcher
4279 buffer)
4280 (when (and (stringp match) (not (string-match "\\S-" match)))
4281 (setq match nil))
4282 (setq matcher (org-make-tags-matcher match)
4283 match (car matcher) matcher (cdr matcher))
4284 (org-prepare-agenda (concat "TAGS " match))
4285 (org-compile-prefix-format 'tags)
4286 (org-set-sorting-strategy 'tags)
4287 (setq org-agenda-query-string match)
4288 (setq org-agenda-redo-command
4289 (list 'org-tags-view (list 'quote todo-only)
4290 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4291 (setq files (org-agenda-files nil 'ifmode)
4292 rtnall nil)
4293 (while (setq file (pop files))
4294 (catch 'nextfile
4295 (org-check-agenda-file file)
4296 (setq buffer (if (file-exists-p file)
4297 (org-get-agenda-file-buffer file)
4298 (error "No such file %s" file)))
4299 (if (not buffer)
4300 ;; If file does not exist, error message to agenda
4301 (setq rtn (list
4302 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4303 rtnall (append rtnall rtn))
4304 (with-current-buffer buffer
4305 (unless (derived-mode-p 'org-mode)
4306 (error "Agenda file %s is not in `org-mode'" file))
4307 (save-excursion
4308 (save-restriction
4309 (if org-agenda-restrict
4310 (narrow-to-region org-agenda-restrict-begin
4311 org-agenda-restrict-end)
4312 (widen))
4313 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4314 (setq rtnall (append rtnall rtn))))))))
4315 (if org-agenda-overriding-header
4316 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4317 nil 'face 'org-agenda-structure) "\n")
4318 (insert "Headlines with TAGS match: ")
4319 (add-text-properties (point-min) (1- (point))
4320 (list 'face 'org-agenda-structure
4321 'short-heading
4322 (concat "Match: " match)))
4323 (setq pos (point))
4324 (insert match "\n")
4325 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4326 (setq pos (point))
4327 (unless org-agenda-multi
4328 (insert "Press `C-u r' to search again with new search string\n"))
4329 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4330 (org-agenda-mark-header-line (point-min))
4331 (when rtnall
4332 (insert (org-finalize-agenda-entries rtnall) "\n"))
4333 (goto-char (point-min))
4334 (or org-agenda-multi (org-fit-agenda-window))
4335 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4336 (org-finalize-agenda)
4337 (setq buffer-read-only t)))
4339 ;;; Agenda Finding stuck projects
4341 (defvar org-agenda-skip-regexp nil
4342 "Regular expression used in skipping subtrees for the agenda.
4343 This is basically a temporary global variable that can be set and then
4344 used by user-defined selections using `org-agenda-skip-function'.")
4346 (defvar org-agenda-overriding-header nil
4347 "When set during agenda, todo and tags searches it replaces the header.
4348 This variable should not be set directly, but custom commands can bind it
4349 in the options section.")
4351 (defun org-agenda-skip-entry-when-regexp-matches ()
4352 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4353 If yes, it returns the end position of this entry, causing agenda commands
4354 to skip the entry but continuing the search in the subtree. This is a
4355 function that can be put into `org-agenda-skip-function' for the duration
4356 of a command."
4357 (let ((end (save-excursion (org-end-of-subtree t)))
4358 skip)
4359 (save-excursion
4360 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4361 (and skip end)))
4363 (defun org-agenda-skip-subtree-when-regexp-matches ()
4364 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4365 If yes, it returns the end position of this tree, causing agenda commands
4366 to skip this subtree. This is a function that can be put into
4367 `org-agenda-skip-function' for the duration of a command."
4368 (let ((end (save-excursion (org-end-of-subtree t)))
4369 skip)
4370 (save-excursion
4371 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4372 (and skip end)))
4374 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4375 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4376 If yes, it returns the end position of the current entry (NOT the tree),
4377 causing agenda commands to skip the entry but continuing the search in
4378 the subtree. This is a function that can be put into
4379 `org-agenda-skip-function' for the duration of a command. An important
4380 use of this function is for the stuck project list."
4381 (let ((end (save-excursion (org-end-of-subtree t)))
4382 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4383 skip)
4384 (save-excursion
4385 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4386 (and skip entry-end)))
4388 (defun org-agenda-skip-entry-if (&rest conditions)
4389 "Skip entry if any of CONDITIONS is true.
4390 See `org-agenda-skip-if' for details."
4391 (org-agenda-skip-if nil conditions))
4393 (defun org-agenda-skip-subtree-if (&rest conditions)
4394 "Skip entry if any of CONDITIONS is true.
4395 See `org-agenda-skip-if' for details."
4396 (org-agenda-skip-if t conditions))
4398 (defun org-agenda-skip-if (subtree conditions)
4399 "Checks current entity for CONDITIONS.
4400 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4401 the entry (i.e. the text before the next heading) is checked.
4403 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4404 from different tests. Valid conditions are:
4406 scheduled Check if there is a scheduled cookie
4407 notscheduled Check if there is no scheduled cookie
4408 deadline Check if there is a deadline
4409 notdeadline Check if there is no deadline
4410 timestamp Check if there is a timestamp (also deadline or scheduled)
4411 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4412 regexp Check if regexp matches
4413 notregexp Check if regexp does not match.
4414 todo Check if TODO keyword matches
4415 nottodo Check if TODO keyword does not match
4417 The regexp is taken from the conditions list, it must come right after
4418 the `regexp' or `notregexp' element.
4420 `todo' and `nottodo' accept as an argument a list of todo
4421 keywords, which may include \"*\" to match any todo keyword.
4423 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4425 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4427 Instead of a list, a keyword class may be given. For example:
4429 (org-agenda-skip-entry-if 'nottodo 'done)
4431 would skip entries that haven't been marked with any of \"DONE\"
4432 keywords. Possible classes are: `todo', `done', `any'.
4434 If any of these conditions is met, this function returns the end point of
4435 the entity, causing the search to continue from there. This is a function
4436 that can be put into `org-agenda-skip-function' for the duration of a command."
4437 (let (beg end m)
4438 (org-back-to-heading t)
4439 (setq beg (point)
4440 end (if subtree
4441 (progn (org-end-of-subtree t) (point))
4442 (progn (outline-next-heading) (1- (point)))))
4443 (goto-char beg)
4444 (and
4446 (and (memq 'scheduled conditions)
4447 (re-search-forward org-scheduled-time-regexp end t))
4448 (and (memq 'notscheduled conditions)
4449 (not (re-search-forward org-scheduled-time-regexp end t)))
4450 (and (memq 'deadline conditions)
4451 (re-search-forward org-deadline-time-regexp end t))
4452 (and (memq 'notdeadline conditions)
4453 (not (re-search-forward org-deadline-time-regexp end t)))
4454 (and (memq 'timestamp conditions)
4455 (re-search-forward org-ts-regexp end t))
4456 (and (memq 'nottimestamp conditions)
4457 (not (re-search-forward org-ts-regexp end t)))
4458 (and (setq m (memq 'regexp conditions))
4459 (stringp (nth 1 m))
4460 (re-search-forward (nth 1 m) end t))
4461 (and (setq m (memq 'notregexp conditions))
4462 (stringp (nth 1 m))
4463 (not (re-search-forward (nth 1 m) end t)))
4464 (and (or
4465 (setq m (memq 'nottodo conditions))
4466 (setq m (memq 'todo-unblocked conditions))
4467 (setq m (memq 'nottodo-unblocked conditions))
4468 (setq m (memq 'todo conditions)))
4469 (org-agenda-skip-if-todo m end)))
4470 end)))
4472 (defun org-agenda-skip-if-todo (args end)
4473 "Helper function for `org-agenda-skip-if', do not use it directly.
4474 ARGS is a list with first element either `todo', `nottodo',
4475 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4476 a list of TODO keywords, or a state symbol `todo' or `done' or
4477 `any'."
4478 (let ((kw (car args))
4479 (arg (cadr args))
4480 todo-wds todo-re)
4481 (setq todo-wds
4482 (org-uniquify
4483 (cond
4484 ((listp arg) ;; list of keywords
4485 (if (member "*" arg)
4486 (mapcar 'substring-no-properties org-todo-keywords-1)
4487 arg))
4488 ((symbolp arg) ;; keyword class name
4489 (cond
4490 ((eq arg 'todo)
4491 (org-delete-all org-done-keywords
4492 (mapcar 'substring-no-properties
4493 org-todo-keywords-1)))
4494 ((eq arg 'done) org-done-keywords)
4495 ((eq arg 'any)
4496 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4497 (setq todo-re
4498 (concat "^\\*+[ \t]+\\<\\("
4499 (mapconcat 'identity todo-wds "\\|")
4500 "\\)\\>"))
4501 (cond
4502 ((eq kw 'todo) (re-search-forward todo-re end t))
4503 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4504 ((eq kw 'todo-unblocked)
4505 (catch 'unblocked
4506 (while (re-search-forward todo-re end t)
4507 (or (org-entry-blocked-p) (throw 'unblocked t)))
4508 nil))
4509 ((eq kw 'nottodo-unblocked)
4510 (catch 'unblocked
4511 (while (re-search-forward todo-re end t)
4512 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4516 ;;;###autoload
4517 (defun org-agenda-list-stuck-projects (&rest ignore)
4518 "Create agenda view for projects that are stuck.
4519 Stuck projects are project that have no next actions. For the definitions
4520 of what a project is and how to check if it stuck, customize the variable
4521 `org-stuck-projects'."
4522 (interactive)
4523 (let* ((org-agenda-skip-function
4524 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4525 ;; We could have used org-agenda-skip-if here.
4526 (org-agenda-overriding-header
4527 (or org-agenda-overriding-header "List of stuck projects: "))
4528 (matcher (nth 0 org-stuck-projects))
4529 (todo (nth 1 org-stuck-projects))
4530 (todo-wds (if (member "*" todo)
4531 (progn
4532 (org-prepare-agenda-buffers (org-agenda-files
4533 nil 'ifmode))
4534 (org-delete-all
4535 org-done-keywords-for-agenda
4536 (copy-sequence org-todo-keywords-for-agenda)))
4537 todo))
4538 (todo-re (concat "^\\*+[ \t]+\\("
4539 (mapconcat 'identity todo-wds "\\|")
4540 "\\)\\>"))
4541 (tags (nth 2 org-stuck-projects))
4542 (tags-re (if (member "*" tags)
4543 (concat org-outline-regexp-bol
4544 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4545 (if tags
4546 (concat org-outline-regexp-bol
4547 ".*:\\("
4548 (mapconcat 'identity tags "\\|")
4549 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4550 (gen-re (nth 3 org-stuck-projects))
4551 (re-list
4552 (delq nil
4553 (list
4554 (if todo todo-re)
4555 (if tags tags-re)
4556 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4557 gen-re)))))
4558 (setq org-agenda-skip-regexp
4559 (if re-list
4560 (mapconcat 'identity re-list "\\|")
4561 (error "No information how to identify unstuck projects")))
4562 (org-tags-view nil matcher)
4563 (with-current-buffer org-agenda-buffer-name
4564 (setq org-agenda-redo-command
4565 '(org-agenda-list-stuck-projects
4566 (or current-prefix-arg org-last-arg))))))
4568 ;;; Diary integration
4570 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4571 (defvar diary-list-entries-hook)
4572 (defvar diary-time-regexp)
4573 (defun org-get-entries-from-diary (date)
4574 "Get the (Emacs Calendar) diary entries for DATE."
4575 (require 'diary-lib)
4576 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4577 (diary-display-hook '(fancy-diary-display))
4578 (diary-display-function 'fancy-diary-display)
4579 (pop-up-frames nil)
4580 (diary-list-entries-hook
4581 (cons 'org-diary-default-entry diary-list-entries-hook))
4582 (diary-file-name-prefix-function nil) ; turn this feature off
4583 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4584 entries
4585 (org-disable-agenda-to-diary t))
4586 (save-excursion
4587 (save-window-excursion
4588 (funcall (if (fboundp 'diary-list-entries)
4589 'diary-list-entries 'list-diary-entries)
4590 date 1)))
4591 (if (not (get-buffer diary-fancy-buffer))
4592 (setq entries nil)
4593 (with-current-buffer diary-fancy-buffer
4594 (setq buffer-read-only nil)
4595 (if (zerop (buffer-size))
4596 ;; No entries
4597 (setq entries nil)
4598 ;; Omit the date and other unnecessary stuff
4599 (org-agenda-cleanup-fancy-diary)
4600 ;; Add prefix to each line and extend the text properties
4601 (if (zerop (buffer-size))
4602 (setq entries nil)
4603 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4604 (setq entries
4605 (with-temp-buffer
4606 (insert entries) (goto-char (point-min))
4607 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4608 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4609 (replace-match (concat "; " (match-string 1)))))
4610 (buffer-string)))))
4611 (set-buffer-modified-p nil)
4612 (kill-buffer diary-fancy-buffer)))
4613 (when entries
4614 (setq entries (org-split-string entries "\n"))
4615 (setq entries
4616 (mapcar
4617 (lambda (x)
4618 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4619 ;; Extend the text properties to the beginning of the line
4620 (org-add-props x (text-properties-at (1- (length x)) x)
4621 'type "diary" 'date date 'face 'org-agenda-diary))
4622 entries)))))
4624 (defvar org-agenda-cleanup-fancy-diary-hook nil
4625 "Hook run when the fancy diary buffer is cleaned up.")
4627 (defun org-agenda-cleanup-fancy-diary ()
4628 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4629 This gets rid of the date, the underline under the date, and
4630 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4631 date. It also removes lines that contain only whitespace."
4632 (goto-char (point-min))
4633 (if (looking-at ".*?:[ \t]*")
4634 (progn
4635 (replace-match "")
4636 (re-search-forward "\n=+$" nil t)
4637 (replace-match "")
4638 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4639 (re-search-forward "\n=+$" nil t)
4640 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4641 (goto-char (point-min))
4642 (while (re-search-forward "^ +\n" nil t)
4643 (replace-match ""))
4644 (goto-char (point-min))
4645 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4646 (replace-match ""))
4647 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4649 ;; Make sure entries from the diary have the right text properties.
4650 (eval-after-load "diary-lib"
4651 '(if (boundp 'diary-modify-entry-list-string-function)
4652 ;; We can rely on the hook, nothing to do
4654 ;; Hook not available, must use advice to make this work
4655 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4656 "Make the position visible."
4657 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4658 (stringp string)
4659 buffer-file-name)
4660 (setq string (org-modify-diary-entry-string string))))))
4662 (defun org-modify-diary-entry-string (string)
4663 "Add text properties to string, allowing org-mode to act on it."
4664 (org-add-props string nil
4665 'mouse-face 'highlight
4666 'help-echo (if buffer-file-name
4667 (format "mouse-2 or RET jump to diary file %s"
4668 (abbreviate-file-name buffer-file-name))
4670 'org-agenda-diary-link t
4671 'org-marker (org-agenda-new-marker (point-at-bol))))
4673 (defun org-diary-default-entry ()
4674 "Add a dummy entry to the diary.
4675 Needed to avoid empty dates which mess up holiday display."
4676 ;; Catch the error if dealing with the new add-to-diary-alist
4677 (when org-disable-agenda-to-diary
4678 (condition-case nil
4679 (org-add-to-diary-list original-date "Org-mode dummy" "")
4680 (error
4681 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4683 (defun org-add-to-diary-list (&rest args)
4684 (if (fboundp 'diary-add-to-list)
4685 (apply 'diary-add-to-list args)
4686 (apply 'add-to-diary-list args)))
4688 (defvar org-diary-last-run-time nil)
4690 ;;;###autoload
4691 (defun org-diary (&rest args)
4692 "Return diary information from org-files.
4693 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4694 It accesses org files and extracts information from those files to be
4695 listed in the diary. The function accepts arguments specifying what
4696 items should be listed. For a list of arguments allowed here, see the
4697 variable `org-agenda-entry-types'.
4699 The call in the diary file should look like this:
4701 &%%(org-diary) ~/path/to/some/orgfile.org
4703 Use a separate line for each org file to check. Or, if you omit the file name,
4704 all files listed in `org-agenda-files' will be checked automatically:
4706 &%%(org-diary)
4708 If you don't give any arguments (as in the example above), the default
4709 arguments (:deadline :scheduled :timestamp :sexp) are used.
4710 So the example above may also be written as
4712 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4714 The function expects the lisp variables `entry' and `date' to be provided
4715 by the caller, because this is how the calendar works. Don't use this
4716 function from a program - use `org-agenda-get-day-entries' instead."
4717 (when (> (- (org-float-time)
4718 org-agenda-last-marker-time)
4720 ;; I am not sure if this works with sticky agendas, because the marker
4721 ;; list is then no longer a global variable.
4722 (org-agenda-reset-markers))
4723 (org-compile-prefix-format 'agenda)
4724 (org-set-sorting-strategy 'agenda)
4725 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4726 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4727 (list entry)
4728 (org-agenda-files t)))
4729 (time (org-float-time))
4730 file rtn results)
4731 (when (or (not org-diary-last-run-time)
4732 (> (- time
4733 org-diary-last-run-time)
4735 (org-prepare-agenda-buffers files))
4736 (setq org-diary-last-run-time time)
4737 ;; If this is called during org-agenda, don't return any entries to
4738 ;; the calendar. Org Agenda will list these entries itself.
4739 (if org-disable-agenda-to-diary (setq files nil))
4740 (while (setq file (pop files))
4741 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4742 (setq results (append results rtn)))
4743 (if results
4744 (concat (org-finalize-agenda-entries results) "\n"))))
4746 ;;; Agenda entry finders
4748 (defun org-agenda-get-day-entries (file date &rest args)
4749 "Does the work for `org-diary' and `org-agenda'.
4750 FILE is the path to a file to be checked for entries. DATE is date like
4751 the one returned by `calendar-current-date'. ARGS are symbols indicating
4752 which kind of entries should be extracted. For details about these, see
4753 the documentation of `org-diary'."
4754 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4755 (let* ((org-startup-folded nil)
4756 (org-startup-align-all-tables nil)
4757 (buffer (if (file-exists-p file)
4758 (org-get-agenda-file-buffer file)
4759 (error "No such file %s" file)))
4760 arg results rtn deadline-results)
4761 (if (not buffer)
4762 ;; If file does not exist, make sure an error message ends up in diary
4763 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4764 (with-current-buffer buffer
4765 (unless (derived-mode-p 'org-mode)
4766 (error "Agenda file %s is not in `org-mode'" file))
4767 (let ((case-fold-search nil))
4768 (save-excursion
4769 (save-restriction
4770 (if org-agenda-restrict
4771 (narrow-to-region org-agenda-restrict-begin
4772 org-agenda-restrict-end)
4773 (widen))
4774 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4775 (while (setq arg (pop args))
4776 (cond
4777 ((and (eq arg :todo)
4778 (equal date (calendar-gregorian-from-absolute
4779 (org-today))))
4780 (setq rtn (org-agenda-get-todos))
4781 (setq results (append results rtn)))
4782 ((eq arg :timestamp)
4783 (setq rtn (org-agenda-get-blocks))
4784 (setq results (append results rtn))
4785 (setq rtn (org-agenda-get-timestamps))
4786 (setq results (append results rtn)))
4787 ((eq arg :sexp)
4788 (setq rtn (org-agenda-get-sexps))
4789 (setq results (append results rtn)))
4790 ((eq arg :scheduled)
4791 (setq rtn (org-agenda-get-scheduled deadline-results))
4792 (setq results (append results rtn)))
4793 ((eq arg :closed)
4794 (setq rtn (org-agenda-get-progress))
4795 (setq results (append results rtn)))
4796 ((eq arg :deadline)
4797 (setq rtn (org-agenda-get-deadlines))
4798 (setq deadline-results (copy-sequence rtn))
4799 (setq results (append results rtn))))))))
4800 results))))
4802 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4803 (defun org-agenda-get-todos ()
4804 "Return the TODO information for agenda display."
4805 (let* ((props (list 'face nil
4806 'done-face 'org-agenda-done
4807 'org-not-done-regexp org-not-done-regexp
4808 'org-todo-regexp org-todo-regexp
4809 'org-complex-heading-regexp org-complex-heading-regexp
4810 'mouse-face 'highlight
4811 'help-echo
4812 (format "mouse-2 or RET jump to org file %s"
4813 (abbreviate-file-name buffer-file-name))))
4814 (regexp (format org-heading-keyword-regexp-format
4815 (cond
4816 ((and org-select-this-todo-keyword
4817 (equal org-select-this-todo-keyword "*"))
4818 org-todo-regexp)
4819 (org-select-this-todo-keyword
4820 (concat "\\("
4821 (mapconcat 'identity
4822 (org-split-string
4823 org-select-this-todo-keyword
4824 "|")
4825 "\\|") "\\)"))
4826 (t org-not-done-regexp))))
4827 marker priority category org-category-pos tags todo-state
4828 ee txt beg end)
4829 (goto-char (point-min))
4830 (while (re-search-forward regexp nil t)
4831 (catch :skip
4832 (save-match-data
4833 (beginning-of-line)
4834 (org-agenda-skip)
4835 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4836 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4837 (goto-char (1+ beg))
4838 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4839 (throw :skip nil)))
4840 (goto-char (match-beginning 2))
4841 (setq marker (org-agenda-new-marker (match-beginning 0))
4842 category (org-get-category)
4843 org-category-pos (get-text-property (point) 'org-category-position)
4844 txt (org-trim
4845 (buffer-substring (match-beginning 2) (match-end 0)))
4846 tags (org-get-tags-at (point))
4847 txt (org-agenda-format-item "" txt category tags)
4848 priority (1+ (org-get-priority txt))
4849 todo-state (org-get-todo-state))
4850 (org-add-props txt props
4851 'org-marker marker 'org-hd-marker marker
4852 'priority priority 'org-category category
4853 'org-category-position org-category-pos
4854 'type "todo" 'todo-state todo-state)
4855 (push txt ee)
4856 (if org-agenda-todo-list-sublevels
4857 (goto-char (match-end 2))
4858 (org-end-of-subtree 'invisible))))
4859 (nreverse ee)))
4861 (defun org-agenda-todo-custom-ignore-p (time n)
4862 "Check whether timestamp is farther away then n number of days.
4863 This function is invoked if `org-agenda-todo-ignore-deadlines',
4864 `org-agenda-todo-ignore-scheduled' or
4865 `org-agenda-todo-ignore-timestamp' is set to an integer."
4866 (let ((days (org-days-to-time time)))
4867 (if (>= n 0)
4868 (>= days n)
4869 (<= days n))))
4871 ;;;###autoload
4872 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4873 (&optional end)
4874 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4875 (when (or org-agenda-todo-ignore-with-date
4876 org-agenda-todo-ignore-scheduled
4877 org-agenda-todo-ignore-deadlines
4878 org-agenda-todo-ignore-timestamp)
4879 (setq end (or end (save-excursion (outline-next-heading) (point))))
4880 (save-excursion
4881 (or (and org-agenda-todo-ignore-with-date
4882 (re-search-forward org-ts-regexp end t))
4883 (and org-agenda-todo-ignore-scheduled
4884 (re-search-forward org-scheduled-time-regexp end t)
4885 (cond
4886 ((eq org-agenda-todo-ignore-scheduled 'future)
4887 (> (org-days-to-time (match-string 1)) 0))
4888 ((eq org-agenda-todo-ignore-scheduled 'past)
4889 (<= (org-days-to-time (match-string 1)) 0))
4890 ((numberp org-agenda-todo-ignore-scheduled)
4891 (org-agenda-todo-custom-ignore-p
4892 (match-string 1) org-agenda-todo-ignore-scheduled))
4893 (t)))
4894 (and org-agenda-todo-ignore-deadlines
4895 (re-search-forward org-deadline-time-regexp end t)
4896 (cond
4897 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4898 ((eq org-agenda-todo-ignore-deadlines 'far)
4899 (not (org-deadline-close (match-string 1))))
4900 ((eq org-agenda-todo-ignore-deadlines 'future)
4901 (> (org-days-to-time (match-string 1)) 0))
4902 ((eq org-agenda-todo-ignore-deadlines 'past)
4903 (<= (org-days-to-time (match-string 1)) 0))
4904 ((numberp org-agenda-todo-ignore-deadlines)
4905 (org-agenda-todo-custom-ignore-p
4906 (match-string 1) org-agenda-todo-ignore-deadlines))
4907 (t (org-deadline-close (match-string 1)))))
4908 (and org-agenda-todo-ignore-timestamp
4909 (let ((buffer (current-buffer))
4910 (regexp
4911 (concat
4912 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4913 (start (point)))
4914 ;; Copy current buffer into a temporary one
4915 (with-temp-buffer
4916 (insert-buffer-substring buffer start end)
4917 (goto-char (point-min))
4918 ;; Delete SCHEDULED and DEADLINE items
4919 (while (re-search-forward regexp end t)
4920 (delete-region (match-beginning 0) (match-end 0)))
4921 (goto-char (point-min))
4922 ;; No search for timestamp left
4923 (when (re-search-forward org-ts-regexp nil t)
4924 (cond
4925 ((eq org-agenda-todo-ignore-timestamp 'future)
4926 (> (org-days-to-time (match-string 1)) 0))
4927 ((eq org-agenda-todo-ignore-timestamp 'past)
4928 (<= (org-days-to-time (match-string 1)) 0))
4929 ((numberp org-agenda-todo-ignore-timestamp)
4930 (org-agenda-todo-custom-ignore-p
4931 (match-string 1) org-agenda-todo-ignore-timestamp))
4932 (t))))))))))
4934 (defconst org-agenda-no-heading-message
4935 "No heading for this item in buffer or region.")
4937 (defun org-agenda-get-timestamps ()
4938 "Return the date stamp information for agenda display."
4939 (let* ((props (list 'face 'org-agenda-calendar-event
4940 'org-not-done-regexp org-not-done-regexp
4941 'org-todo-regexp org-todo-regexp
4942 'org-complex-heading-regexp org-complex-heading-regexp
4943 'mouse-face 'highlight
4944 'help-echo
4945 (format "mouse-2 or RET jump to org file %s"
4946 (abbreviate-file-name buffer-file-name))))
4947 (d1 (calendar-absolute-from-gregorian date))
4948 (remove-re
4949 (concat
4950 (regexp-quote
4951 (format-time-string
4952 "<%Y-%m-%d"
4953 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4954 ".*?>"))
4955 (regexp
4956 (concat
4957 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4958 (regexp-quote
4959 (substring
4960 (format-time-string
4961 (car org-time-stamp-formats)
4962 (apply 'encode-time ; DATE bound by calendar
4963 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4964 1 11))
4965 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
4966 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4967 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4968 donep tmp priority category org-category-pos ee txt timestr tags
4969 b0 b3 e3 head todo-state end-of-match show-all)
4970 (goto-char (point-min))
4971 (while (setq end-of-match (re-search-forward regexp nil t))
4972 (setq b0 (match-beginning 0)
4973 b3 (match-beginning 3) e3 (match-end 3)
4974 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4975 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4976 (member todo-state
4977 org-agenda-repeating-timestamp-show-all)))
4978 (catch :skip
4979 (and (org-at-date-range-p) (throw :skip nil))
4980 (org-agenda-skip)
4981 (if (and (match-end 1)
4982 (not (= d1 (org-time-string-to-absolute
4983 (match-string 1) d1 nil show-all
4984 (current-buffer) b0))))
4985 (throw :skip nil))
4986 (if (and e3
4987 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4988 (throw :skip nil))
4989 (setq tmp (buffer-substring (max (point-min)
4990 (- b0 org-ds-keyword-length))
4992 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4993 inactivep (= (char-after b0) ?\[)
4994 deadlinep (string-match org-deadline-regexp tmp)
4995 scheduledp (string-match org-scheduled-regexp tmp)
4996 closedp (and org-agenda-include-inactive-timestamps
4997 (string-match org-closed-string tmp))
4998 clockp (and org-agenda-include-inactive-timestamps
4999 (or (string-match org-clock-string tmp)
5000 (string-match "]-+\\'" tmp)))
5001 donep (member todo-state org-done-keywords))
5002 (if (or scheduledp deadlinep closedp clockp
5003 (and donep org-agenda-skip-timestamp-if-done))
5004 (throw :skip t))
5005 (if (string-match ">" timestr)
5006 ;; substring should only run to end of time stamp
5007 (setq timestr (substring timestr 0 (match-end 0))))
5008 (setq marker (org-agenda-new-marker b0)
5009 category (org-get-category b0)
5010 org-category-pos (get-text-property b0 'org-category-position))
5011 (save-excursion
5012 (if (not (re-search-backward org-outline-regexp-bol nil t))
5013 (setq txt org-agenda-no-heading-message)
5014 (goto-char (match-beginning 0))
5015 (setq hdmarker (org-agenda-new-marker)
5016 tags (org-get-tags-at))
5017 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5018 (setq head (or (match-string 1) ""))
5019 (setq txt (org-agenda-format-item
5020 (if inactivep org-agenda-inactive-leader nil)
5021 head category tags timestr
5022 remove-re)))
5023 (setq priority (org-get-priority txt))
5024 (org-add-props txt props
5025 'org-marker marker 'org-hd-marker hdmarker)
5026 (org-add-props txt nil 'priority priority
5027 'org-category category 'date date
5028 'org-category-position org-category-pos
5029 'todo-state todo-state
5030 'type "timestamp")
5031 (push txt ee))
5032 (if org-agenda-skip-additional-timestamps-same-entry
5033 (outline-next-heading)
5034 (goto-char end-of-match))))
5035 (nreverse ee)))
5037 (defun org-agenda-get-sexps ()
5038 "Return the sexp information for agenda display."
5039 (require 'diary-lib)
5040 (let* ((props (list 'face 'org-agenda-calendar-sexp
5041 'mouse-face 'highlight
5042 'help-echo
5043 (format "mouse-2 or RET jump to org file %s"
5044 (abbreviate-file-name buffer-file-name))))
5045 (regexp "^&?%%(")
5046 marker category org-category-pos ee txt tags entry
5047 result beg b sexp sexp-entry todo-state)
5048 (goto-char (point-min))
5049 (while (re-search-forward regexp nil t)
5050 (catch :skip
5051 (org-agenda-skip)
5052 (setq beg (match-beginning 0))
5053 (goto-char (1- (match-end 0)))
5054 (setq b (point))
5055 (forward-sexp 1)
5056 (setq sexp (buffer-substring b (point)))
5057 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5058 (org-trim (match-string 1))
5059 ""))
5060 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5061 (when result
5062 (setq marker (org-agenda-new-marker beg)
5063 category (org-get-category beg)
5064 org-category-pos (get-text-property beg 'org-category-position)
5065 todo-state (org-get-todo-state))
5067 (dolist (r (if (stringp result)
5068 (list result)
5069 result)) ;; we expect a list here
5070 (if (string-match "\\S-" r)
5071 (setq txt r)
5072 (setq txt "SEXP entry returned empty string"))
5074 (setq txt (org-agenda-format-item
5075 "" txt category tags 'time))
5076 (org-add-props txt props 'org-marker marker)
5077 (org-add-props txt nil
5078 'org-category category 'date date 'todo-state todo-state
5079 'org-category-position org-category-pos
5080 'type "sexp")
5081 (push txt ee)))))
5082 (nreverse ee)))
5084 ;; Calendar sanity: define some functions that are independent of
5085 ;; `calendar-date-style'.
5086 ;; Normally I would like to use ISO format when calling the diary functions,
5087 ;; but to make sure we still have Emacs 22 compatibility we bind
5088 ;; also `european-calendar-style' and use european format
5089 (defun org-anniversary (year month day &optional mark)
5090 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5091 (org-no-warnings
5092 (let ((calendar-date-style 'european) (european-calendar-style t))
5093 (diary-anniversary day month year mark))))
5094 (defun org-cyclic (N year month day &optional mark)
5095 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5096 (org-no-warnings
5097 (let ((calendar-date-style 'european) (european-calendar-style t))
5098 (diary-cyclic N day month year mark))))
5099 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5100 "Like `diary-block', but with fixed (ISO) order of arguments."
5101 (org-no-warnings
5102 (let ((calendar-date-style 'european) (european-calendar-style t))
5103 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5104 (defun org-date (year month day &optional mark)
5105 "Like `diary-date', but with fixed (ISO) order of arguments."
5106 (org-no-warnings
5107 (let ((calendar-date-style 'european) (european-calendar-style t))
5108 (diary-date day month year mark))))
5109 (defalias 'org-float 'diary-float)
5111 ;; Define the` org-class' function
5112 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5113 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5114 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5115 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5116 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5117 `holidays', then any date that is known by the Emacs calendar to be a
5118 holiday will also be skipped."
5119 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5120 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5121 (d (calendar-absolute-from-gregorian date)))
5122 (and
5123 (<= date1 d)
5124 (<= d date2)
5125 (= (calendar-day-of-week date) dayname)
5126 (or (not skip-weeks)
5127 (progn
5128 (require 'cal-iso)
5129 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5130 (not (and (memq 'holidays skip-weeks)
5131 (calendar-check-holidays date)))
5132 entry)))
5134 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5135 "Like `org-class', but honor `calendar-date-style'.
5136 The order of the first 2 times 3 arguments depends on the variable
5137 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5138 So for American calendars, give this as MONTH DAY YEAR, for European as
5139 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5140 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5141 is any number of ISO weeks in the block period for which the item should
5142 be skipped.
5144 This function is here only for backward compatibility and it is deprecated,
5145 please use `org-class' instead."
5146 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5147 (date2 (org-order-calendar-date-args m2 d2 y2)))
5148 (org-class
5149 (nth 2 date1) (car date1) (nth 1 date1)
5150 (nth 2 date2) (car date2) (nth 1 date2)
5151 dayname skip-weeks)))
5152 (make-obsolete 'org-diary-class 'org-class "")
5154 (defvar org-agenda-show-log-scoped) ;; dynamically scope in ̀org-timeline' or`org-agenda-list'
5155 (defalias 'org-get-closed 'org-agenda-get-progress)
5156 (defun org-agenda-get-progress ()
5157 "Return the logged TODO entries for agenda display."
5158 (let* ((props (list 'mouse-face 'highlight
5159 'org-not-done-regexp org-not-done-regexp
5160 'org-todo-regexp org-todo-regexp
5161 'org-complex-heading-regexp org-complex-heading-regexp
5162 'help-echo
5163 (format "mouse-2 or RET jump to org file %s"
5164 (abbreviate-file-name buffer-file-name))))
5165 (items (if (consp org-agenda-show-log-scoped)
5166 org-agenda-show-log-scoped
5167 (if (eq org-agenda-show-log-scoped 'clockcheck)
5168 '(clock)
5169 org-agenda-log-mode-items)))
5170 (parts
5171 (delq nil
5172 (list
5173 (if (memq 'closed items) (concat "\\<" org-closed-string))
5174 (if (memq 'clock items) (concat "\\<" org-clock-string))
5175 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5176 (parts-re (if parts (mapconcat 'identity parts "\\|")
5177 (error "`org-agenda-log-mode-items' is empty")))
5178 (regexp (concat
5179 "\\(" parts-re "\\)"
5180 " *\\["
5181 (regexp-quote
5182 (substring
5183 (format-time-string
5184 (car org-time-stamp-formats)
5185 (apply 'encode-time ; DATE bound by calendar
5186 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5187 1 11))))
5188 (org-agenda-search-headline-for-time nil)
5189 marker hdmarker priority category org-category-pos tags closedp
5190 statep clockp state ee txt extra timestr rest clocked)
5191 (goto-char (point-min))
5192 (while (re-search-forward regexp nil t)
5193 (catch :skip
5194 (org-agenda-skip)
5195 (setq marker (org-agenda-new-marker (match-beginning 0))
5196 closedp (equal (match-string 1) org-closed-string)
5197 statep (equal (string-to-char (match-string 1)) ?-)
5198 clockp (not (or closedp statep))
5199 state (and statep (match-string 2))
5200 category (org-get-category (match-beginning 0))
5201 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
5202 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5203 (when (string-match "\\]" timestr)
5204 ;; substring should only run to end of time stamp
5205 (setq rest (substring timestr (match-end 0))
5206 timestr (substring timestr 0 (match-end 0)))
5207 (if (and (not closedp) (not statep)
5208 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5209 rest))
5210 (progn (setq timestr (concat (substring timestr 0 -1)
5211 "-" (match-string 1 rest) "]"))
5212 (setq clocked (match-string 2 rest)))
5213 (setq clocked "-")))
5214 (save-excursion
5215 (setq extra
5216 (cond
5217 ((not org-agenda-log-mode-add-notes) nil)
5218 (statep
5219 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5220 (match-string 1)))
5221 (clockp
5222 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5223 (match-string 1)))))
5224 (if (not (re-search-backward org-outline-regexp-bol nil t))
5225 (setq txt org-agenda-no-heading-message)
5226 (goto-char (match-beginning 0))
5227 (setq hdmarker (org-agenda-new-marker)
5228 tags (org-get-tags-at))
5229 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5230 (setq txt (match-string 1))
5231 (when extra
5232 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5233 (setq txt (concat (substring txt 0 (match-beginning 1))
5234 " - " extra " " (match-string 2 txt)))
5235 (setq txt (concat txt " - " extra))))
5236 (setq txt (org-agenda-format-item
5237 (cond
5238 (closedp "Closed: ")
5239 (statep (concat "State: (" state ")"))
5240 (t (concat "Clocked: (" clocked ")")))
5241 txt category tags timestr)))
5242 (setq priority 100000)
5243 (org-add-props txt props
5244 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5245 'priority priority 'org-category category
5246 'org-category-position org-category-pos
5247 'type "closed" 'date date
5248 'undone-face 'org-warning 'done-face 'org-agenda-done)
5249 (push txt ee))
5250 (goto-char (point-at-eol))))
5251 (nreverse ee)))
5253 (defun org-agenda-show-clocking-issues ()
5254 "Add overlays, showing issues with clocking.
5255 See also the user option `org-agenda-clock-consistency-checks'."
5256 (interactive)
5257 (let* ((pl org-agenda-clock-consistency-checks)
5258 (re (concat "^[ \t]*"
5259 org-clock-string
5260 "[ \t]+"
5261 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5262 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5263 (tlstart 0.)
5264 (tlend 0.)
5265 (maxtime (org-hh:mm-string-to-minutes
5266 (or (plist-get pl :max-duration) "24:00")))
5267 (mintime (org-hh:mm-string-to-minutes
5268 (or (plist-get pl :min-duration) 0)))
5269 (maxgap (org-hh:mm-string-to-minutes
5270 ;; default 30:00 means never complain
5271 (or (plist-get pl :max-gap) "30:00")))
5272 (gapok (mapcar 'org-hh:mm-string-to-minutes
5273 (plist-get pl :gap-ok-around)))
5274 (def-face (or (plist-get pl :default-face)
5275 '((:background "DarkRed") (:foreground "white"))))
5276 issue face m te ts dt ov)
5277 (goto-char (point-min))
5278 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5279 (setq issue nil face def-face)
5280 (catch 'next
5281 (setq m (org-get-at-bol 'org-marker)
5282 te nil ts nil)
5283 (unless (and m (markerp m))
5284 (setq issue "No valid clock line") (throw 'next t))
5285 (org-with-point-at m
5286 (save-excursion
5287 (goto-char (point-at-bol))
5288 (unless (looking-at re)
5289 (error "No valid Clock line")
5290 (throw 'next t))
5291 (unless (match-end 3)
5292 (setq issue "No end time"
5293 face (or (plist-get pl :no-end-time-face) face))
5294 (throw 'next t))
5295 (setq ts (match-string 1)
5296 te (match-string 3)
5297 ts (org-float-time
5298 (apply 'encode-time (org-parse-time-string ts)))
5299 te (org-float-time
5300 (apply 'encode-time (org-parse-time-string te)))
5301 dt (- te ts))))
5302 (cond
5303 ((> dt (* 60 maxtime))
5304 ;; a very long clocking chunk
5305 (setq issue (format "Clocking interval is very long: %s"
5306 (org-minutes-to-hh:mm-string
5307 (floor (/ (float dt) 60.))))
5308 face (or (plist-get pl :long-face) face)))
5309 ((< dt (* 60 mintime))
5310 ;; a very short clocking chunk
5311 (setq issue (format "Clocking interval is very short: %s"
5312 (org-minutes-to-hh:mm-string
5313 (floor (/ (float dt) 60.))))
5314 face (or (plist-get pl :short-face) face)))
5315 ((and (> tlend 0) (< ts tlend))
5316 ;; Two clock entries are overlapping
5317 (setq issue (format "Clocking overlap: %d minutes"
5318 (/ (- tlend ts) 60))
5319 face (or (plist-get pl :overlap-face) face)))
5320 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5321 ;; There is a gap, lets see if we need to report it
5322 (unless (org-agenda-check-clock-gap tlend ts gapok)
5323 (setq issue (format "Clocking gap: %d minutes"
5324 (/ (- ts tlend) 60))
5325 face (or (plist-get pl :gap-face) face))))
5326 (t nil)))
5327 (setq tlend (or te tlend) tlstart (or ts tlstart))
5328 (when issue
5329 ;; OK, there was some issue, add an overlay to show the issue
5330 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5331 (overlay-put ov 'before-string
5332 (concat
5333 (org-add-props
5334 (format "%-43s" (concat " " issue))
5336 'face face)
5337 "\n"))
5338 (overlay-put ov 'evaporate t)))))
5340 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5341 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5342 (catch 'exit
5343 (unless ok-list
5344 ;; there are no OK times for gaps...
5345 (throw 'exit nil))
5346 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5347 ;; This is more than 24 hours, so it is OK.
5348 ;; because we have at least one OK time, that must be in the
5349 ;; 24 hour interval.
5350 (throw 'exit t))
5351 ;; We have a shorter gap.
5352 ;; Now we have to get the minute of the day when these times are
5353 (let* ((t1dec (decode-time (seconds-to-time t1)))
5354 (t2dec (decode-time (seconds-to-time t2)))
5355 ;; compute the minute on the day
5356 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5357 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5358 (when (< min2 min1)
5359 ;; if min2 is smaller than min1, this means it is on the next day.
5360 ;; Wrap it to after midnight.
5361 (setq min2 (+ min2 1440)))
5362 ;; Now check if any of the OK times is in the gap
5363 (mapc (lambda (x)
5364 ;; Wrap the time to after midnight if necessary
5365 (if (< x min1) (setq x (+ x 1440)))
5366 ;; Check if in interval
5367 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5368 ok-list)
5369 ;; Nope, this gap is not OK
5370 nil)))
5372 (defun org-agenda-get-deadlines ()
5373 "Return the deadline information for agenda display."
5374 (let* ((props (list 'mouse-face 'highlight
5375 'org-not-done-regexp org-not-done-regexp
5376 'org-todo-regexp org-todo-regexp
5377 'org-complex-heading-regexp org-complex-heading-regexp
5378 'help-echo
5379 (format "mouse-2 or RET jump to org file %s"
5380 (abbreviate-file-name buffer-file-name))))
5381 (regexp org-deadline-time-regexp)
5382 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5383 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5384 d2 diff dfrac wdays pos pos1 category org-category-pos
5385 tags suppress-prewarning ee txt head face s todo-state
5386 show-all upcomingp donep timestr)
5387 (goto-char (point-min))
5388 (while (re-search-forward regexp nil t)
5389 (setq suppress-prewarning nil)
5390 (catch :skip
5391 (org-agenda-skip)
5392 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5393 (save-match-data
5394 (string-match org-scheduled-time-regexp
5395 (buffer-substring (point-at-bol)
5396 (point-at-eol)))))
5397 (setq suppress-prewarning
5398 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5399 org-agenda-skip-deadline-prewarning-if-scheduled
5400 0)))
5401 (setq s (match-string 1)
5402 txt nil
5403 pos (1- (match-beginning 1))
5404 todo-state (save-match-data (org-get-todo-state))
5405 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5406 (member todo-state
5407 org-agenda-repeating-timestamp-show-all))
5408 d2 (org-time-string-to-absolute
5409 (match-string 1) d1 'past show-all
5410 (current-buffer) pos)
5411 diff (- d2 d1)
5412 wdays (if suppress-prewarning
5413 (let ((org-deadline-warning-days suppress-prewarning))
5414 (org-get-wdays s))
5415 (org-get-wdays s))
5416 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5417 upcomingp (and todayp (> diff 0)))
5418 ;; When to show a deadline in the calendar:
5419 ;; If the expiration is within wdays warning time.
5420 ;; Past-due deadlines are only shown on the current date
5421 (if (and (or (and (<= diff wdays)
5422 (and todayp (not org-agenda-only-exact-dates)))
5423 (= diff 0)))
5424 (save-excursion
5425 ;; (setq todo-state (org-get-todo-state))
5426 (setq donep (member todo-state org-done-keywords))
5427 (if (and donep
5428 (or org-agenda-skip-deadline-if-done
5429 (not (= diff 0))))
5430 (setq txt nil)
5431 (setq category (org-get-category)
5432 org-category-pos (get-text-property (point) 'org-category-position))
5433 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5434 (setq txt org-agenda-no-heading-message)
5435 (goto-char (match-end 0))
5436 (setq pos1 (match-beginning 0))
5437 (setq tags (org-get-tags-at pos1))
5438 (setq head (buffer-substring-no-properties
5439 (point)
5440 (progn (skip-chars-forward "^\r\n")
5441 (point))))
5442 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5443 (setq timestr
5444 (concat (substring s (match-beginning 1)) " "))
5445 (setq timestr 'time))
5446 (setq txt (org-agenda-format-item
5447 (if (= diff 0)
5448 (car org-agenda-deadline-leaders)
5449 (if (functionp
5450 (nth 1 org-agenda-deadline-leaders))
5451 (funcall
5452 (nth 1 org-agenda-deadline-leaders)
5453 diff date)
5454 (format (nth 1 org-agenda-deadline-leaders)
5455 diff)))
5456 head category tags
5457 (if (not (= diff 0)) nil timestr)))))
5458 (when txt
5459 (setq face (org-agenda-deadline-face dfrac))
5460 (org-add-props txt props
5461 'org-marker (org-agenda-new-marker pos)
5462 'org-hd-marker (org-agenda-new-marker pos1)
5463 'priority (+ (- diff)
5464 (org-get-priority txt))
5465 'org-category category
5466 'org-category-position org-category-pos
5467 'todo-state todo-state
5468 'type (if upcomingp "upcoming-deadline" "deadline")
5469 'date (if upcomingp date d2)
5470 'face (if donep 'org-agenda-done face)
5471 'undone-face face 'done-face 'org-agenda-done)
5472 (push txt ee))))))
5473 (nreverse ee)))
5475 (defun org-agenda-deadline-face (fraction)
5476 "Return the face to displaying a deadline item.
5477 FRACTION is what fraction of the head-warning time has passed."
5478 (let ((faces org-agenda-deadline-faces) f)
5479 (catch 'exit
5480 (while (setq f (pop faces))
5481 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5483 (defun org-agenda-get-scheduled (&optional deadline-results)
5484 "Return the scheduled information for agenda display."
5485 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5486 'org-todo-regexp org-todo-regexp
5487 'org-complex-heading-regexp org-complex-heading-regexp
5488 'done-face 'org-agenda-done
5489 'mouse-face 'highlight
5490 'help-echo
5491 (format "mouse-2 or RET jump to org file %s"
5492 (abbreviate-file-name buffer-file-name))))
5493 (regexp org-scheduled-time-regexp)
5494 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5495 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5497 (deadline-position-alist
5498 (mapcar (lambda (a) (and (setq mm (get-text-property
5499 0 'org-hd-marker a))
5500 (cons (marker-position mm) a)))
5501 deadline-results))
5502 d2 diff pos pos1 category org-category-pos tags donep
5503 ee txt head pastschedp todo-state face timestr s habitp show-all)
5504 (goto-char (point-min))
5505 (while (re-search-forward regexp nil t)
5506 (catch :skip
5507 (org-agenda-skip)
5508 (setq s (match-string 1)
5509 txt nil
5510 pos (1- (match-beginning 1))
5511 todo-state (save-match-data (org-get-todo-state))
5512 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5513 (member todo-state
5514 org-agenda-repeating-timestamp-show-all))
5515 d2 (org-time-string-to-absolute
5516 (match-string 1) d1 'past show-all
5517 (current-buffer) pos)
5518 diff (- d2 d1))
5519 (setq pastschedp (and todayp (< diff 0)))
5520 ;; When to show a scheduled item in the calendar:
5521 ;; If it is on or past the date.
5522 (when (or (and (< diff 0)
5523 (< (abs diff) org-scheduled-past-days)
5524 (and todayp (not org-agenda-only-exact-dates)))
5525 (= diff 0))
5526 (save-excursion
5527 (setq donep (member todo-state org-done-keywords))
5528 (if (and donep
5529 (or org-agenda-skip-scheduled-if-done
5530 (not (= diff 0))
5531 (and (functionp 'org-is-habit-p)
5532 (org-is-habit-p))))
5533 (setq txt nil)
5534 (setq habitp (and (functionp 'org-is-habit-p)
5535 (org-is-habit-p)))
5536 (setq category (org-get-category)
5537 org-category-pos (get-text-property (point) 'org-category-position))
5538 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5539 (setq txt org-agenda-no-heading-message)
5540 (goto-char (match-end 0))
5541 (setq pos1 (match-beginning 0))
5542 (if habitp
5543 (if (or (not org-habit-show-habits)
5544 (and (not todayp)
5545 org-habit-show-habits-only-for-today))
5546 (throw :skip nil))
5547 (if (and
5548 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5549 (and org-agenda-skip-scheduled-if-deadline-is-shown
5550 pastschedp))
5551 (setq mm (assoc pos1 deadline-position-alist)))
5552 (throw :skip nil)))
5553 (setq tags (org-get-tags-at))
5554 (setq head (buffer-substring-no-properties
5555 (point)
5556 (progn (skip-chars-forward "^\r\n") (point))))
5557 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5558 (setq timestr
5559 (concat (substring s (match-beginning 1)) " "))
5560 (setq timestr 'time))
5561 (setq txt (org-agenda-format-item
5562 (if (= diff 0)
5563 (car org-agenda-scheduled-leaders)
5564 (format (nth 1 org-agenda-scheduled-leaders)
5565 (- 1 diff)))
5566 head category tags
5567 (if (not (= diff 0)) nil timestr)
5568 nil habitp))))
5569 (when txt
5570 (setq face
5571 (cond
5572 ((and (not habitp) pastschedp)
5573 'org-scheduled-previously)
5574 (todayp 'org-scheduled-today)
5575 (t 'org-scheduled))
5576 habitp (and habitp (org-habit-parse-todo)))
5577 (org-add-props txt props
5578 'undone-face face
5579 'face (if donep 'org-agenda-done face)
5580 'org-marker (org-agenda-new-marker pos)
5581 'org-hd-marker (org-agenda-new-marker pos1)
5582 'type (if pastschedp "past-scheduled" "scheduled")
5583 'date (if pastschedp d2 date)
5584 'priority (if habitp
5585 (org-habit-get-priority habitp)
5586 (+ 94 (- 5 diff) (org-get-priority txt)))
5587 'org-category category
5588 'org-category-position org-category-pos
5589 'org-habit-p habitp
5590 'todo-state todo-state)
5591 (push txt ee))))))
5592 (nreverse ee)))
5594 (defun org-agenda-get-blocks ()
5595 "Return the date-range information for agenda display."
5596 (let* ((props (list 'face nil
5597 'org-not-done-regexp org-not-done-regexp
5598 'org-todo-regexp org-todo-regexp
5599 'org-complex-heading-regexp org-complex-heading-regexp
5600 'mouse-face 'highlight
5601 'help-echo
5602 (format "mouse-2 or RET jump to org file %s"
5603 (abbreviate-file-name buffer-file-name))))
5604 (regexp org-tr-regexp)
5605 (d0 (calendar-absolute-from-gregorian date))
5606 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5607 todo-state tags pos head donep)
5608 (goto-char (point-min))
5609 (while (re-search-forward regexp nil t)
5610 (catch :skip
5611 (org-agenda-skip)
5612 (setq pos (point))
5613 (let ((start-time (match-string 1))
5614 (end-time (match-string 2)))
5615 (setq s1 (match-string 1)
5616 s2 (match-string 2)
5617 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5618 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5619 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5620 ;; Only allow days between the limits, because the normal
5621 ;; date stamps will catch the limits.
5622 (save-excursion
5623 (setq todo-state (org-get-todo-state))
5624 (setq donep (member todo-state org-done-keywords))
5625 (if (and donep org-agenda-skip-timestamp-if-done)
5626 (throw :skip t))
5627 (setq marker (org-agenda-new-marker (point)))
5628 (setq category (org-get-category)
5629 org-category-pos (get-text-property (point) 'org-category-position))
5630 (if (not (re-search-backward org-outline-regexp-bol nil t))
5631 (setq txt org-agenda-no-heading-message)
5632 (goto-char (match-beginning 0))
5633 (setq hdmarker (org-agenda-new-marker (point)))
5634 (setq tags (org-get-tags-at))
5635 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5636 (setq head (match-string 1))
5637 (let ((remove-re
5638 (if org-agenda-remove-timeranges-from-blocks
5639 (concat
5640 "<" (regexp-quote s1) ".*?>"
5641 "--"
5642 "<" (regexp-quote s2) ".*?>")
5643 nil)))
5644 (setq txt (org-agenda-format-item
5645 (format
5646 (nth (if (= d1 d2) 0 1)
5647 org-agenda-timerange-leaders)
5648 (1+ (- d0 d1)) (1+ (- d2 d1)))
5649 head category tags
5650 (cond ((and (= d1 d0) (= d2 d0))
5651 (concat "<" start-time ">--<" end-time ">"))
5652 ((= d1 d0)
5653 (concat "<" start-time ">"))
5654 ((= d2 d0)
5655 (concat "<" end-time ">"))
5656 (t nil))
5657 remove-re))))
5658 (org-add-props txt props
5659 'org-marker marker 'org-hd-marker hdmarker
5660 'type "block" 'date date
5661 'todo-state todo-state
5662 'priority (org-get-priority txt) 'org-category category
5663 'org-category-position org-category-pos)
5664 (push txt ee))))
5665 (goto-char pos)))
5666 ;; Sort the entries by expiration date.
5667 (nreverse ee)))
5669 ;;; Agenda presentation and sorting
5671 (defvar org-prefix-has-time nil
5672 "A flag, set by `org-compile-prefix-format'.
5673 The flag is set if the currently compiled format contains a `%t'.")
5674 (defvar org-prefix-has-tag nil
5675 "A flag, set by `org-compile-prefix-format'.
5676 The flag is set if the currently compiled format contains a `%T'.")
5677 (defvar org-prefix-has-effort nil
5678 "A flag, set by `org-compile-prefix-format'.
5679 The flag is set if the currently compiled format contains a `%e'.")
5680 (defvar org-prefix-category-length nil
5681 "Used by `org-compile-prefix-format' to remember the category field width.")
5682 (defvar org-prefix-category-max-length nil
5683 "Used by `org-compile-prefix-format' to remember the category field width.")
5685 (defun org-agenda-get-category-icon (category)
5686 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5687 (dolist (entry org-agenda-category-icon-alist)
5688 (when (org-string-match-p (car entry) category)
5689 (if (listp (cadr entry))
5690 (return (cadr entry))
5691 (return (apply 'create-image (cdr entry)))))))
5693 (defun org-agenda-format-item (extra txt &optional category tags dotime
5694 remove-re habitp)
5695 "Format TXT to be inserted into the agenda buffer.
5696 In particular, it adds the prefix and corresponding text properties. EXTRA
5697 must be a string and replaces the `%s' specifier in the prefix format.
5698 CATEGORY (string, symbol or nil) may be used to overrule the default
5699 category taken from local variable or file name. It will replace the `%c'
5700 specifier in the format. DOTIME, when non-nil, indicates that a
5701 time-of-day should be extracted from TXT for sorting of this entry, and for
5702 the `%t' specifier in the format. When DOTIME is a string, this string is
5703 searched for a time before TXT is. TAGS can be the tags of the headline.
5704 Any match of REMOVE-RE will be removed from TXT."
5705 ;; We keep the org-prefix-* variable values along with a compiled
5706 ;; formatter, so that multiple agendas existing at the same time, do
5707 ;; not step on each other toes.
5709 ;; It was inconvenient to make these variables buffer local in
5710 ;; Agenda buffers, because this function expects to be called with
5711 ;; the buffer where item comes from being current, and not agenda
5712 ;; buffer
5713 (let* ((bindings (car org-prefix-format-compiled))
5714 (formatter (cadr org-prefix-format-compiled)))
5715 (loop for (var value) in bindings
5716 do (set var value))
5717 (save-match-data
5718 ;; Diary entries sometimes have extra whitespace at the beginning
5719 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5721 ;; Fix the tags part in txt
5722 (setq txt (org-agenda-fix-displayed-tags
5723 txt tags
5724 org-agenda-show-inherited-tags
5725 org-agenda-hide-tags-regexp))
5726 (let* ((category (or category
5727 (if (stringp org-category)
5728 org-category
5729 (and org-category (symbol-name org-category)))
5730 (if buffer-file-name
5731 (file-name-sans-extension
5732 (file-name-nondirectory buffer-file-name))
5733 "")))
5734 (category-icon (org-agenda-get-category-icon category))
5735 (category-icon (if category-icon
5736 (propertize " " 'display category-icon)
5737 ""))
5738 ;; time, tag, effort are needed for the eval of the prefix format
5739 (tag (if tags (nth (1- (length tags)) tags) ""))
5740 time effort neffort
5741 (ts (if dotime (concat
5742 (if (stringp dotime) dotime "")
5743 (and org-agenda-search-headline-for-time txt))))
5744 (time-of-day (and dotime (org-get-time-of-day ts)))
5745 stamp plain s0 s1 s2 rtn srp l
5746 duration thecategory)
5747 (and (derived-mode-p 'org-mode) buffer-file-name
5748 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5749 (when (and dotime time-of-day)
5750 ;; Extract starting and ending time and move them to prefix
5751 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5752 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5753 (setq s0 (match-string 0 ts)
5754 srp (and stamp (match-end 3))
5755 s1 (match-string (if plain 1 2) ts)
5756 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5758 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5759 ;; them, we might want to remove them there to avoid duplication.
5760 ;; The user can turn this off with a variable.
5761 (if (and org-prefix-has-time
5762 org-agenda-remove-times-when-in-prefix (or stamp plain)
5763 (string-match (concat (regexp-quote s0) " *") txt)
5764 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5765 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5766 (= (match-beginning 0) 0)
5768 (setq txt (replace-match "" nil nil txt))))
5769 ;; Normalize the time(s) to 24 hour
5770 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5771 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5773 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5774 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5775 (setq s2
5776 (org-minutes-to-hh:mm-string
5777 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5779 ;; Compute the duration
5780 (when s2
5781 (setq duration (- (org-hh:mm-string-to-minutes s2)
5782 (org-hh:mm-string-to-minutes s1)))))
5784 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5785 txt)
5786 ;; Tags are in the string
5787 (if (or (eq org-agenda-remove-tags t)
5788 (and org-agenda-remove-tags
5789 org-prefix-has-tag))
5790 (setq txt (replace-match "" t t txt))
5791 (setq txt (replace-match
5792 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5793 (match-string 2 txt))
5794 t t txt))))
5795 (when (derived-mode-p 'org-mode)
5796 (setq effort
5797 (condition-case nil
5798 (org-get-effort
5799 (or (get-text-property 0 'org-hd-marker txt)
5800 (get-text-property 0 'org-marker txt)))
5801 (error nil)))
5802 (when effort
5803 (setq neffort (org-duration-string-to-minutes effort)
5804 effort (setq effort (concat "[" effort "]")))))
5805 ;; prevent erroring out with %e format when there is no effort
5806 (or effort (setq effort ""))
5808 (when remove-re
5809 (while (string-match remove-re txt)
5810 (setq txt (replace-match "" t t txt))))
5812 ;; Set org-heading property on `txt' to mark the start of the
5813 ;; heading.
5814 (add-text-properties 0 (length txt) '(org-heading t) txt)
5816 ;; Prepare the variables needed in the eval of the compiled format
5817 (setq time (cond (s2 (concat
5818 (org-agenda-time-of-day-to-ampm-maybe s1)
5819 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5820 (if org-agenda-timegrid-use-ampm " ")))
5821 (s1 (concat
5822 (org-agenda-time-of-day-to-ampm-maybe s1)
5823 (if org-agenda-timegrid-use-ampm
5824 "........ "
5825 "......")))
5826 (t ""))
5827 extra (or (and (not habitp) extra) "")
5828 category (if (symbolp category) (symbol-name category) category)
5829 thecategory (copy-sequence category))
5830 (if (string-match org-bracket-link-regexp category)
5831 (progn
5832 (setq l (if (match-end 3)
5833 (- (match-end 3) (match-beginning 3))
5834 (- (match-end 1) (match-beginning 1))))
5835 (when (< l (or org-prefix-category-length 0))
5836 (setq category (copy-sequence category))
5837 (org-add-props category nil
5838 'extra-space (make-string
5839 (- org-prefix-category-length l 1) ?\ ))))
5840 (if (and org-prefix-category-max-length
5841 (>= (length category) org-prefix-category-max-length))
5842 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5843 ;; Evaluate the compiled format
5844 (setq rtn (concat (eval formatter) txt))
5846 ;; And finally add the text properties
5847 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5848 (org-add-props rtn nil
5849 'org-category (if thecategory (downcase thecategory) category)
5850 'tags (mapcar 'org-downcase-keep-props tags)
5851 'org-highest-priority org-highest-priority
5852 'org-lowest-priority org-lowest-priority
5853 'time-of-day time-of-day
5854 'duration duration
5855 'effort effort
5856 'effort-minutes neffort
5857 'txt txt
5858 'time time
5859 'extra extra
5860 'format org-prefix-format-compiled
5861 'dotime dotime)))))
5863 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5864 "Remove tags string from TXT, and add a modified list of tags.
5865 The modified list may contain inherited tags, and tags matched by
5866 `org-agenda-hide-tags-regexp' will be removed."
5867 (when (or add-inherited hide-re)
5868 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5869 (setq txt (substring txt 0 (match-beginning 0))))
5870 (setq tags
5871 (delq nil
5872 (mapcar (lambda (tg)
5873 (if (or (and hide-re (string-match hide-re tg))
5874 (and (not add-inherited)
5875 (get-text-property 0 'inherited tg)))
5877 tg))
5878 tags)))
5879 (when tags
5880 (let ((have-i (get-text-property 0 'inherited (car tags)))
5882 (setq txt (concat txt " :"
5883 (mapconcat
5884 (lambda (x)
5885 (setq i (get-text-property 0 'inherited x))
5886 (if (and have-i (not i))
5887 (progn
5888 (setq have-i nil)
5889 (concat ":" x))
5891 tags ":")
5892 (if have-i "::" ":"))))))
5893 txt)
5895 (defun org-downcase-keep-props (s)
5896 (let ((props (text-properties-at 0 s)))
5897 (setq s (downcase s))
5898 (add-text-properties 0 (length s) props s)
5901 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5902 (defvar org-agenda-sorting-strategy-selected nil)
5904 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5905 (catch 'exit
5906 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5907 ((and todayp (member 'today (car org-agenda-time-grid))))
5908 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5909 ((member 'weekly (car org-agenda-time-grid)))
5910 (t (throw 'exit list)))
5911 (let* ((have (delq nil (mapcar
5912 (lambda (x) (get-text-property 1 'time-of-day x))
5913 list)))
5914 (string (nth 1 org-agenda-time-grid))
5915 (gridtimes (nth 2 org-agenda-time-grid))
5916 (req (car org-agenda-time-grid))
5917 (remove (member 'remove-match req))
5918 new time)
5919 (if (and (member 'require-timed req) (not have))
5920 ;; don't show empty grid
5921 (throw 'exit list))
5922 (while (setq time (pop gridtimes))
5923 (unless (and remove (member time have))
5924 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5925 (push (org-agenda-format-item
5926 nil string "" nil
5927 (concat (substring time 0 -2) ":" (substring time -2)))
5928 new)
5929 (put-text-property
5930 2 (length (car new)) 'face 'org-time-grid (car new))))
5931 (when (and todayp org-agenda-show-current-time-in-grid)
5932 (push (org-agenda-format-item
5934 org-agenda-current-time-string
5935 "" nil
5936 (format-time-string "%H:%M "))
5937 new)
5938 (put-text-property
5939 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5941 (if (member 'time-up org-agenda-sorting-strategy-selected)
5942 (append new list)
5943 (append list new)))))
5945 (defun org-compile-prefix-format (key)
5946 "Compile the prefix format into a Lisp form that can be evaluated.
5947 The resulting form and associated variable bindings is returned
5948 and stored in the variable `org-prefix-format-compiled'."
5949 (setq org-prefix-has-time nil org-prefix-has-tag nil
5950 org-prefix-category-length nil
5951 org-prefix-has-effort nil)
5952 (let ((s (cond
5953 ((stringp org-agenda-prefix-format)
5954 org-agenda-prefix-format)
5955 ((assq key org-agenda-prefix-format)
5956 (cdr (assq key org-agenda-prefix-format)))
5957 (t " %-12:c%?-12t% s")))
5958 (start 0)
5959 varform vars var e c f opt)
5960 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5961 s start)
5962 (setq var (or (cdr (assoc (match-string 4 s)
5963 '(("c" . category) ("t" . time) ("s" . extra)
5964 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5965 'eval)
5966 c (or (match-string 3 s) "")
5967 opt (match-beginning 1)
5968 start (1+ (match-beginning 0)))
5969 (if (equal var 'time) (setq org-prefix-has-time t))
5970 (if (equal var 'tag) (setq org-prefix-has-tag t))
5971 (if (equal var 'effort) (setq org-prefix-has-effort t))
5972 (setq f (concat "%" (match-string 2 s) "s"))
5973 (when (equal var 'category)
5974 (setq org-prefix-category-length
5975 (floor (abs (string-to-number (match-string 2 s)))))
5976 (setq org-prefix-category-max-length
5977 (let ((x (match-string 2 s)))
5978 (save-match-data
5979 (if (string-match "\\.[0-9]+" x)
5980 (string-to-number (substring (match-string 0 x) 1)))))))
5981 (if (eq var 'eval)
5982 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5983 (if opt
5984 (setq varform
5985 `(if (equal "" ,var)
5987 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5988 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5989 (setq s (replace-match "%s" t nil s))
5990 (push varform vars))
5991 (setq vars (nreverse vars))
5992 (with-current-buffer org-agenda-buffer
5993 (setq org-prefix-format-compiled
5994 (list
5995 `((org-prefix-has-time ,org-prefix-has-time)
5996 (org-prefix-has-tag ,org-prefix-has-tag)
5997 (org-prefix-category-length ,org-prefix-category-length)
5998 (org-prefix-has-effort ,org-prefix-has-effort))
5999 `(format ,s ,@vars))))))
6001 (defun org-set-sorting-strategy (key)
6002 (if (symbolp (car org-agenda-sorting-strategy))
6003 ;; the old format
6004 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6005 (setq org-agenda-sorting-strategy-selected
6006 (or (cdr (assq key org-agenda-sorting-strategy))
6007 (cdr (assq 'agenda org-agenda-sorting-strategy))
6008 '(time-up category-keep priority-down)))))
6010 (defun org-get-time-of-day (s &optional string mod24)
6011 "Check string S for a time of day.
6012 If found, return it as a military time number between 0 and 2400.
6013 If not found, return nil.
6014 The optional STRING argument forces conversion into a 5 character wide string
6015 HH:MM."
6016 (save-match-data
6017 (when
6018 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6019 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6020 (let* ((h (string-to-number (match-string 1 s)))
6021 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6022 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6023 (am-p (equal ampm "am"))
6024 (h1 (cond ((not ampm) h)
6025 ((= h 12) (if am-p 0 12))
6026 (t (+ h (if am-p 0 12)))))
6027 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6028 (mod h1 24) h1))
6029 (t0 (+ (* 100 h2) m))
6030 (t1 (concat (if (>= h1 24) "+" " ")
6031 (if (and org-agenda-time-leading-zero
6032 (< t0 1000)) "0" "")
6033 (if (< t0 100) "0" "")
6034 (if (< t0 10) "0" "")
6035 (int-to-string t0))))
6036 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6038 (defvar org-agenda-before-sorting-filter-function nil
6039 "Function to be applied to agenda items prior to sorting.
6040 Prior to sorting also means just before they are inserted into the agenda.
6042 To aid sorting, you may revisit the original entries and add more text
6043 properties which will later be used by the sorting functions.
6045 The function should take a string argument, an agenda line.
6046 It has access to the text properties in that line, which contain among
6047 other things, the property `org-hd-marker' that points to the entry
6048 where the line comes from. Note that not all lines going into the agenda
6049 have this property, only most.
6051 The function should return the modified string. It is probably best
6052 to ONLY change text properties.
6054 You can also use this function as a filter, by returning nil for lines
6055 you don't want to have in the agenda at all. For this application, you
6056 could bind the variable in the options section of a custom command.")
6058 (defun org-finalize-agenda-entries (list &optional nosort)
6059 "Sort and concatenate the agenda items."
6060 (setq list (mapcar 'org-agenda-highlight-todo list))
6061 (if nosort
6062 list
6063 (when org-agenda-before-sorting-filter-function
6064 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
6065 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
6067 (defun org-agenda-highlight-todo (x)
6068 (let ((org-done-keywords org-done-keywords-for-agenda)
6069 (case-fold-search nil)
6071 (if (eq x 'line)
6072 (save-excursion
6073 (beginning-of-line 1)
6074 (setq re (org-get-at-bol 'org-todo-regexp))
6075 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6076 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6077 (add-text-properties (match-beginning 0) (match-end 1)
6078 (list 'face (org-get-todo-face 1)))
6079 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6080 (delete-region (match-beginning 1) (1- (match-end 0)))
6081 (goto-char (match-beginning 1))
6082 (insert (format org-agenda-todo-keyword-format s)))))
6083 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6084 (setq re (get-text-property 0 'org-todo-regexp x))
6085 (when (and re
6086 ;; Test `pl' because if there's no heading content,
6087 ;; there's no point matching to highlight. Note
6088 ;; that if we didn't test `pl' first, and there
6089 ;; happened to be no keyword from `org-todo-regexp'
6090 ;; on this heading line, then the `equal' comparison
6091 ;; afterwards would spuriously succeed in the case
6092 ;; where `pl' is nil -- causing an args-out-of-range
6093 ;; error when we try to add text properties to text
6094 ;; that isn't there.
6096 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6097 x pl) pl))
6098 (add-text-properties
6099 (or (match-end 1) (match-end 0)) (match-end 0)
6100 (list 'face (org-get-todo-face (match-string 2 x)))
6102 (when (match-end 1)
6103 (setq x (concat (substring x 0 (match-end 1))
6104 (format org-agenda-todo-keyword-format
6105 (match-string 2 x))
6106 (org-add-props " " (text-properties-at 0 x))
6107 (substring x (match-end 3)))))))
6108 x)))
6110 (defsubst org-cmp-priority (a b)
6111 "Compare the priorities of string A and B."
6112 (let ((pa (or (get-text-property 1 'priority a) 0))
6113 (pb (or (get-text-property 1 'priority b) 0)))
6114 (cond ((> pa pb) +1)
6115 ((< pa pb) -1)
6116 (t nil))))
6118 (defsubst org-cmp-effort (a b)
6119 "Compare the effort values of string A and B."
6120 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6121 (ea (or (get-text-property 1 'effort-minutes a) def))
6122 (eb (or (get-text-property 1 'effort-minutes b) def)))
6123 (cond ((> ea eb) +1)
6124 ((< ea eb) -1)
6125 (t nil))))
6127 (defsubst org-cmp-category (a b)
6128 "Compare the string values of categories of strings A and B."
6129 (let ((ca (or (get-text-property 1 'org-category a) ""))
6130 (cb (or (get-text-property 1 'org-category b) "")))
6131 (cond ((string-lessp ca cb) -1)
6132 ((string-lessp cb ca) +1)
6133 (t nil))))
6135 (defsubst org-cmp-todo-state (a b)
6136 "Compare the todo states of strings A and B."
6137 (let* ((ma (or (get-text-property 1 'org-marker a)
6138 (get-text-property 1 'org-hd-marker a)))
6139 (mb (or (get-text-property 1 'org-marker b)
6140 (get-text-property 1 'org-hd-marker b)))
6141 (fa (and ma (marker-buffer ma)))
6142 (fb (and mb (marker-buffer mb)))
6143 (todo-kwds
6144 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6145 (and fb (with-current-buffer fb org-todo-keywords-1))))
6146 (ta (or (get-text-property 1 'todo-state a) ""))
6147 (tb (or (get-text-property 1 'todo-state b) ""))
6148 (la (- (length (member ta todo-kwds))))
6149 (lb (- (length (member tb todo-kwds))))
6150 (donepa (member ta org-done-keywords-for-agenda))
6151 (donepb (member tb org-done-keywords-for-agenda)))
6152 (cond ((and donepa (not donepb)) -1)
6153 ((and (not donepa) donepb) +1)
6154 ((< la lb) -1)
6155 ((< lb la) +1)
6156 (t nil))))
6158 (defsubst org-cmp-alpha (a b)
6159 "Compare the headlines, alphabetically."
6160 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6161 (plb (text-property-any 0 (length b) 'org-heading t b))
6162 (ta (and pla (substring a pla)))
6163 (tb (and plb (substring b plb))))
6164 (when pla
6165 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6166 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6167 (setq ta (substring ta (match-end 0))))
6168 (setq ta (downcase ta)))
6169 (when plb
6170 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6171 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6172 (setq tb (substring tb (match-end 0))))
6173 (setq tb (downcase tb)))
6174 (cond ((not ta) +1)
6175 ((not tb) -1)
6176 ((string-lessp ta tb) -1)
6177 ((string-lessp tb ta) +1)
6178 (t nil))))
6180 (defsubst org-cmp-tag (a b)
6181 "Compare the string values of the first tags of A and B."
6182 (let ((ta (car (last (get-text-property 1 'tags a))))
6183 (tb (car (last (get-text-property 1 'tags b)))))
6184 (cond ((not ta) +1)
6185 ((not tb) -1)
6186 ((string-lessp ta tb) -1)
6187 ((string-lessp tb ta) +1)
6188 (t nil))))
6190 (defsubst org-cmp-time (a b)
6191 "Compare the time-of-day values of strings A and B."
6192 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6193 (ta (or (get-text-property 1 'time-of-day a) def))
6194 (tb (or (get-text-property 1 'time-of-day b) def)))
6195 (cond ((< ta tb) -1)
6196 ((< tb ta) +1)
6197 (t nil))))
6199 (defsubst org-cmp-habit-p (a b)
6200 "Compare the todo states of strings A and B."
6201 (let ((ha (get-text-property 1 'org-habit-p a))
6202 (hb (get-text-property 1 'org-habit-p b)))
6203 (cond ((and ha (not hb)) -1)
6204 ((and (not ha) hb) +1)
6205 (t nil))))
6207 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
6209 (defun org-entries-lessp (a b)
6210 "Predicate for sorting agenda entries."
6211 ;; The following variables will be used when the form is evaluated.
6212 ;; So even though the compiler complains, keep them.
6213 (let* ((ss org-agenda-sorting-strategy-selected)
6214 (time-up (and (org-em 'time-up 'time-down ss)
6215 (org-cmp-time a b)))
6216 (time-down (if time-up (- time-up) nil))
6217 (priority-up (and (org-em 'priority-up 'priority-down ss)
6218 (org-cmp-priority a b)))
6219 (priority-down (if priority-up (- priority-up) nil))
6220 (effort-up (and (org-em 'effort-up 'effort-down ss)
6221 (org-cmp-effort a b)))
6222 (effort-down (if effort-up (- effort-up) nil))
6223 (category-up (and (or (org-em 'category-up 'category-down ss)
6224 (memq 'category-keep ss))
6225 (org-cmp-category a b)))
6226 (category-down (if category-up (- category-up) nil))
6227 (category-keep (if category-up +1 nil))
6228 (tag-up (and (org-em 'tag-up 'tag-down ss)
6229 (org-cmp-tag a b)))
6230 (tag-down (if tag-up (- tag-up) nil))
6231 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6232 (org-cmp-todo-state a b)))
6233 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6234 (habit-up (and (org-em 'habit-up 'habit-down ss)
6235 (org-cmp-habit-p a b)))
6236 (habit-down (if habit-up (- habit-up) nil))
6237 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6238 (org-cmp-alpha a b)))
6239 (alpha-down (if alpha-up (- alpha-up) nil))
6240 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6241 user-defined-up user-defined-down)
6242 (if (and need-user-cmp org-agenda-cmp-user-defined
6243 (functionp org-agenda-cmp-user-defined))
6244 (setq user-defined-up
6245 (funcall org-agenda-cmp-user-defined a b)
6246 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6247 (cdr (assoc
6248 (eval (cons 'or org-agenda-sorting-strategy-selected))
6249 '((-1 . t) (1 . nil) (nil . nil))))))
6251 ;;; Agenda restriction lock
6253 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6254 "Overlay to mark the headline to which agenda commands are restricted.")
6255 (overlay-put org-agenda-restriction-lock-overlay
6256 'face 'org-agenda-restriction-lock)
6257 (overlay-put org-agenda-restriction-lock-overlay
6258 'help-echo "Agendas are currently limited to this subtree.")
6259 (org-detach-overlay org-agenda-restriction-lock-overlay)
6261 (defun org-agenda-set-restriction-lock (&optional type)
6262 "Set restriction lock for agenda, to current subtree or file.
6263 Restriction will be the file if TYPE is `file', or if type is the
6264 universal prefix '(4), or if the cursor is before the first headline
6265 in the file. Otherwise, restriction will be to the current subtree."
6266 (interactive "P")
6267 (and (equal type '(4)) (setq type 'file))
6268 (setq type (cond
6269 (type type)
6270 ((org-at-heading-p) 'subtree)
6271 ((condition-case nil (org-back-to-heading t) (error nil))
6272 'subtree)
6273 (t 'file)))
6274 (if (eq type 'subtree)
6275 (progn
6276 (setq org-agenda-restrict t)
6277 (setq org-agenda-overriding-restriction 'subtree)
6278 (put 'org-agenda-files 'org-restrict
6279 (list (buffer-file-name (buffer-base-buffer))))
6280 (org-back-to-heading t)
6281 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6282 (move-marker org-agenda-restrict-begin (point))
6283 (move-marker org-agenda-restrict-end
6284 (save-excursion (org-end-of-subtree t)))
6285 (message "Locking agenda restriction to subtree"))
6286 (put 'org-agenda-files 'org-restrict
6287 (list (buffer-file-name (buffer-base-buffer))))
6288 (setq org-agenda-restrict nil)
6289 (setq org-agenda-overriding-restriction 'file)
6290 (move-marker org-agenda-restrict-begin nil)
6291 (move-marker org-agenda-restrict-end nil)
6292 (message "Locking agenda restriction to file"))
6293 (setq current-prefix-arg nil)
6294 (org-agenda-maybe-redo))
6296 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6297 "Remove the agenda restriction lock."
6298 (interactive "P")
6299 (org-detach-overlay org-agenda-restriction-lock-overlay)
6300 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6301 (setq org-agenda-overriding-restriction nil)
6302 (setq org-agenda-restrict nil)
6303 (put 'org-agenda-files 'org-restrict nil)
6304 (move-marker org-agenda-restrict-begin nil)
6305 (move-marker org-agenda-restrict-end nil)
6306 (setq current-prefix-arg nil)
6307 (message "Agenda restriction lock removed")
6308 (or noupdate (org-agenda-maybe-redo)))
6310 (defun org-agenda-maybe-redo ()
6311 "If there is any window showing the agenda view, update it."
6312 (let ((w (get-buffer-window org-agenda-buffer-name t))
6313 (w0 (selected-window)))
6314 (when w
6315 (select-window w)
6316 (org-agenda-redo)
6317 (select-window w0)
6318 (if org-agenda-overriding-restriction
6319 (message "Agenda view shifted to new %s restriction"
6320 org-agenda-overriding-restriction)
6321 (message "Agenda restriction lock removed")))))
6323 ;;; Agenda commands
6325 (defun org-agenda-check-type (error &rest types)
6326 "Check if agenda buffer is of allowed type.
6327 If ERROR is non-nil, throw an error, otherwise just return nil."
6328 (if (memq org-agenda-type types)
6330 (if error
6331 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6332 nil)))
6334 (defun org-agenda-Quit (&optional arg)
6335 "Exit agenda by removing the window or the buffer"
6336 (interactive)
6337 (if org-agenda-columns-active
6338 (org-columns-quit)
6339 (let ((buf (current-buffer)))
6340 (if (eq org-agenda-window-setup 'other-frame)
6341 (progn
6342 (org-agenda-reset-markers)
6343 (kill-buffer buf)
6344 (org-columns-remove-overlays)
6345 (setq org-agenda-archives-mode nil)
6346 (delete-frame))
6347 (and (not (eq org-agenda-window-setup 'current-window))
6348 (not (one-window-p))
6349 (delete-window))
6350 (org-agenda-reset-markers)
6351 (kill-buffer buf)
6352 (org-columns-remove-overlays)
6353 (setq org-agenda-archives-mode nil)))
6354 ;; Maybe restore the pre-agenda window configuration.
6355 (and org-agenda-restore-windows-after-quit
6356 (not (eq org-agenda-window-setup 'other-frame))
6357 org-pre-agenda-window-conf
6358 (set-window-configuration org-pre-agenda-window-conf))))
6360 (defun org-agenda-quit ()
6361 "Exit agenda by killing agenda buffer or burying it when
6362 `org-agenda-sticky' is non-NIL"
6363 (interactive)
6364 (if org-agenda-columns-active
6365 (org-columns-quit)
6366 (if org-agenda-sticky
6367 (let ((buf (current-buffer)))
6368 (if (eq org-agenda-window-setup 'other-frame)
6369 (progn
6370 (delete-frame))
6371 (and (not (eq org-agenda-window-setup 'current-window))
6372 (not (one-window-p))
6373 (delete-window)))
6374 (with-current-buffer buf
6375 (bury-buffer)
6376 ;; Maybe restore the pre-agenda window configuration.
6377 (and org-agenda-restore-windows-after-quit
6378 (not (eq org-agenda-window-setup 'other-frame))
6379 org-pre-agenda-window-conf
6380 (set-window-configuration org-pre-agenda-window-conf))))))
6381 (org-agenda-Quit))))
6383 (defun org-agenda-exit ()
6384 "Exit agenda by removing the window or the buffer.
6385 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6386 Org-mode buffers visited directly by the user will not be touched."
6387 (interactive)
6388 (org-release-buffers org-agenda-new-buffers)
6389 (setq org-agenda-new-buffers nil)
6390 (org-agenda-Quit))
6392 (defun org-agenda-kill-all-agenda-buffers ()
6393 "Kill all buffers in `org-agena-mode'.
6394 This is used when toggling sticky agendas. You can also explicitly invoke it
6395 with `C-c a C-k'."
6396 (interactive)
6397 (let (blist)
6398 (dolist (buf (buffer-list))
6399 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
6400 (push buf blist)))
6401 (mapc 'kill-buffer blist)))
6403 (defun org-agenda-execute (arg)
6404 "Execute another agenda command, keeping same window.
6405 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6406 in the agenda."
6407 (interactive "P")
6408 (let ((org-agenda-window-setup 'current-window))
6409 (org-agenda arg)))
6411 (defun org-agenda-redo ()
6412 "Rebuild Agenda.
6413 When this is the global TODO list, a prefix argument will be interpreted."
6414 (interactive)
6415 (let* ((org-agenda-doing-sticky-redo org-agenda-sticky)
6416 (org-agenda-sticky nil)
6417 (org-agenda-buffer-name (or org-agenda-this-buffer-name
6418 org-agenda-buffer-name))
6419 (org-agenda-keep-modes t)
6420 (tag-filter org-agenda-tag-filter)
6421 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
6422 (cat-filter org-agenda-category-filter)
6423 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6424 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
6425 (cols org-agenda-columns-active)
6426 (line (org-current-line))
6427 (window-line (- line (org-current-line (window-start))))
6428 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6429 (put 'org-agenda-tag-filter :preset-filter nil)
6430 (put 'org-agenda-category-filter :preset-filter nil)
6431 (and cols (org-columns-quit))
6432 (message "Rebuilding agenda buffer...")
6433 (org-let lprops '(eval org-agenda-redo-command))
6434 (setq org-agenda-undo-list nil
6435 org-agenda-pending-undo-list nil)
6436 (message "Rebuilding agenda buffer...done")
6437 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6438 (put 'org-agenda-category-filter :preset-filter cat-preset)
6439 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6440 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
6441 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6442 (org-goto-line line)
6443 (recenter window-line)))
6445 (defvar org-global-tags-completion-table nil)
6446 (defvar org-agenda-filter-form nil)
6447 (defvar org-agenda-filtered-by-category nil)
6449 (defun org-agenda-filter-by-category (strip)
6450 "Keep only those lines in the agenda buffer that have a specific category.
6451 The category is that of the current line."
6452 (interactive "P")
6453 (if org-agenda-filtered-by-category
6454 (org-agenda-filter-show-all-cat)
6455 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6456 (if cat (org-agenda-filter-apply
6457 (list (concat (if strip "-" "+") cat)) 'category)
6458 (error "No category at point")))))
6460 (defun org-find-top-category (&optional pos)
6461 (save-excursion
6462 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
6463 (if pos (goto-char pos))
6464 ;; Skip up to the topmost parent
6465 (while (ignore-errors (outline-up-heading 1) t))
6466 (ignore-errors
6467 (nth 4 (org-heading-components))))))
6469 (defvar org-agenda-filtered-by-top-category nil)
6471 (defun org-agenda-filter-by-top-category (strip)
6472 "Keep only those lines in the agenda buffer that have a specific category.
6473 The category is that of the current line."
6474 (interactive "P")
6475 (if org-agenda-filtered-by-top-category
6476 (progn
6477 (setq org-agenda-filtered-by-top-category nil)
6478 (org-agenda-filter-show-all-cat))
6479 (let ((cat (org-find-top-category (org-get-at-bol 'org-hd-marker))))
6480 (if cat (org-agenda-filter-top-category-apply cat strip)
6481 (error "No top-level category at point")))))
6483 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6484 "Keep only those lines in the agenda buffer that have a specific tag.
6485 The tag is selected with its fast selection letter, as configured.
6486 With prefix argument STRIP, remove all lines that do have the tag.
6487 A lisp caller can specify CHAR. NARROW means that the new tag should be
6488 used to narrow the search - the interactive user can also press `-' or `+'
6489 to switch to narrowing."
6490 (interactive "P")
6491 (let* ((alist org-tag-alist-for-agenda)
6492 (tag-chars (mapconcat
6493 (lambda (x) (if (and (not (symbolp (car x)))
6494 (cdr x))
6495 (char-to-string (cdr x))
6496 ""))
6497 alist ""))
6498 (efforts (org-split-string
6499 (or (cdr (assoc (concat org-effort-property "_ALL")
6500 org-global-properties))
6501 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6502 "")))
6503 (effort-op org-agenda-filter-effort-default-operator)
6504 (effort-prompt "")
6505 (inhibit-read-only t)
6506 (current org-agenda-tag-filter)
6507 maybe-refresh a n tag)
6508 (unless char
6509 (message
6510 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6511 (if narrow "Narrow" "Filter") tag-chars
6512 (if org-agenda-auto-exclude-function "[RET], " ""))
6513 (setq char (read-char-exclusive)))
6514 (when (member char '(?+ ?-))
6515 ;; Narrowing down
6516 (cond ((equal char ?-) (setq strip t narrow t))
6517 ((equal char ?+) (setq strip nil narrow t)))
6518 (message
6519 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6520 (setq char (read-char-exclusive)))
6521 (when (member char '(?< ?> ?= ??))
6522 ;; An effort operator
6523 (setq effort-op (char-to-string char))
6524 (setq alist nil) ; to make sure it will be interpreted as effort.
6525 (unless (equal char ??)
6526 (loop for i from 0 to 9 do
6527 (setq effort-prompt
6528 (concat
6529 effort-prompt " ["
6530 (if (= i 9) "0" (int-to-string (1+ i)))
6531 "]" (nth i efforts))))
6532 (message "Effort%s: %s " effort-op effort-prompt)
6533 (setq char (read-char-exclusive))
6534 (when (or (< char ?0) (> char ?9))
6535 (error "Need 1-9,0 to select effort"))))
6536 (when (equal char ?\t)
6537 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6538 (org-set-local 'org-global-tags-completion-table
6539 (org-global-tags-completion-table)))
6540 (let ((completion-ignore-case t))
6541 (setq tag (org-icompleting-read
6542 "Tag: " org-global-tags-completion-table))))
6543 (cond
6544 ((equal char ?\r)
6545 (org-agenda-filter-show-all-tag)
6546 (when org-agenda-auto-exclude-function
6547 (setq org-agenda-tag-filter '())
6548 (dolist (tag (org-agenda-get-represented-tags))
6549 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6550 (if modifier
6551 (push modifier org-agenda-tag-filter))))
6552 (if (not (null org-agenda-tag-filter))
6553 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
6554 (setq maybe-refresh t))
6555 ((equal char ?/)
6556 (org-agenda-filter-show-all-tag)
6557 (when (get 'org-agenda-tag-filter :preset-filter)
6558 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
6559 (setq maybe-refresh t))
6560 ((equal char ?. )
6561 (setq org-agenda-tag-filter
6562 (mapcar (lambda(tag) (concat "+" tag))
6563 (org-get-at-bol 'tags)))
6564 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6565 (setq maybe-refresh t))
6566 ((or (equal char ?\ )
6567 (setq a (rassoc char alist))
6568 (and (>= char ?0) (<= char ?9)
6569 (setq n (if (= char ?0) 9 (- char ?0 1))
6570 tag (concat effort-op (nth n efforts))
6571 a (cons tag nil)))
6572 (and (= char ??)
6573 (setq tag "?eff")
6574 a (cons tag nil))
6575 (and tag (setq a (cons tag nil))))
6576 (org-agenda-filter-show-all-tag)
6577 (setq tag (car a))
6578 (setq org-agenda-tag-filter
6579 (cons (concat (if strip "-" "+") tag)
6580 (if narrow current nil)))
6581 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6582 (setq maybe-refresh t))
6583 (t (error "Invalid tag selection character %c" char)))
6584 (when (and maybe-refresh
6585 (eq org-agenda-clockreport-mode 'with-filter))
6586 (org-agenda-redo))))
6588 (defun org-agenda-get-represented-tags ()
6589 "Get a list of all tags currently represented in the agenda."
6590 (let (p tags)
6591 (save-excursion
6592 (goto-char (point-min))
6593 (while (setq p (next-single-property-change (point) 'tags))
6594 (goto-char p)
6595 (mapc (lambda (x) (add-to-list 'tags x))
6596 (get-text-property (point) 'tags))))
6597 tags))
6599 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6600 "Refine the current filter. See `org-agenda-filter-by-tag'."
6601 (interactive "P")
6602 (org-agenda-filter-by-tag strip char 'refine))
6604 (defun org-agenda-filter-make-matcher ()
6605 "Create the form that tests a line for agenda filter."
6606 (let (f f1)
6607 ;; first compute the tag-filter matcher
6608 (dolist (x (delete-dups
6609 (append (get 'org-agenda-tag-filter
6610 :preset-filter) org-agenda-tag-filter)))
6611 (if (member x '("-" "+"))
6612 (setq f1 (if (equal x "-") 'tags '(not tags)))
6613 (if (string-match "[<=>?]" x)
6614 (setq f1 (org-agenda-filter-effort-form x))
6615 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6616 (if (equal (string-to-char x) ?-)
6617 (setq f1 (list 'not f1))))
6618 (push f1 f))
6619 ;; then compute the category-filter matcher
6620 (dolist (x (delete-dups
6621 (append (get 'org-agenda-category-filter
6622 :preset-filter) org-agenda-category-filter)))
6623 (if (equal "-" (substring x 0 1))
6624 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
6625 (setq f1 (list 'equal (substring x 1) 'cat)))
6626 (push f1 f))
6627 (cons 'and (nreverse f))))
6629 (defun org-agenda-filter-effort-form (e)
6630 "Return the form to compare the effort of the current line with what E says.
6631 E looks like \"+<2:25\"."
6632 (let (op)
6633 (setq e (substring e 1))
6634 (setq op (string-to-char e) e (substring e 1))
6635 (setq op (cond ((equal op ?<) '<=)
6636 ((equal op ?>) '>=)
6637 ((equal op ??) op)
6638 (t '=)))
6639 (list 'org-agenda-compare-effort (list 'quote op)
6640 (org-duration-string-to-minutes e))))
6642 (defun org-agenda-compare-effort (op value)
6643 "Compare the effort of the current line with VALUE, using OP.
6644 If the line does not have an effort defined, return nil."
6645 (let ((eff (org-get-at-bol 'effort-minutes)))
6646 (if (equal op ??)
6647 (not eff)
6648 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6649 value))))
6651 (defun org-agenda-filter-apply (filter type)
6652 "Set FILTER as the new agenda filter and apply it."
6653 (let (tags cat)
6654 (if (eq type 'tag)
6655 (setq org-agenda-tag-filter filter)
6656 (setq org-agenda-category-filter filter))
6657 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
6658 (if (and (eq type 'category)
6659 (not (equal (substring (car filter) 0 1) "-")))
6660 ;; Only set `org-agenda-filtered-by-category' to t
6661 ;; when a unique category is used as the filter
6662 (setq org-agenda-filtered-by-category t))
6663 (org-agenda-set-mode-name)
6664 (save-excursion
6665 (goto-char (point-min))
6666 (while (not (eobp))
6667 (if (org-get-at-bol 'org-marker)
6668 (progn
6669 (setq tags (org-get-at-bol 'tags) ; used in eval
6670 cat (get-text-property (point) 'org-category))
6671 (if (not (eval org-agenda-filter-form))
6672 (org-agenda-filter-hide-line type))
6673 (beginning-of-line 2))
6674 (beginning-of-line 2))))
6675 (if (get-char-property (point) 'invisible)
6676 (ignore-errors (org-agenda-previous-line)))))
6678 (defun org-agenda-filter-top-category-apply (category &optional negative)
6679 "Set FILTER as the new agenda filter and apply it."
6680 (org-agenda-set-mode-name)
6681 (save-excursion
6682 (goto-char (point-min))
6683 (while (not (eobp))
6684 (let* ((pos (org-get-at-bol 'org-hd-marker))
6685 (topcat (and pos (org-find-top-category pos))))
6686 (if (and topcat (funcall (if negative 'identity 'not)
6687 (string= category topcat)))
6688 (org-agenda-filter-hide-line 'category)))
6689 (beginning-of-line 2)))
6690 (if (get-char-property (point) 'invisible)
6691 (org-agenda-previous-line))
6692 (setq org-agenda-filtered-by-top-category t))
6694 (defun org-agenda-filter-hide-line (type)
6695 (let (ov)
6696 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6697 (point-at-eol)))
6698 (overlay-put ov 'invisible t)
6699 (overlay-put ov 'type type)
6700 (if (eq type 'tag)
6701 (push ov org-agenda-tag-filter-overlays)
6702 (push ov org-agenda-cat-filter-overlays))))
6704 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6705 (setq pos (or pos (point)))
6706 (save-excursion
6707 (dolist (ov (overlays-at pos))
6708 (when (and (overlay-get ov 'invisible)
6709 (eq (overlay-get ov 'type) 'tag))
6710 (goto-char pos)
6711 (if (< (overlay-start ov) (point-at-eol))
6712 (move-overlay ov (point-at-eol)
6713 (overlay-end ov)))))))
6715 (defun org-agenda-filter-show-all-tag nil
6716 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
6717 (setq org-agenda-tag-filter-overlays nil
6718 org-agenda-tag-filter nil
6719 org-agenda-filter-form nil)
6720 (org-agenda-set-mode-name))
6722 (defun org-agenda-filter-show-all-cat nil
6723 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
6724 (setq org-agenda-cat-filter-overlays nil
6725 org-agenda-filtered-by-category nil
6726 org-agenda-category-filter nil
6727 org-agenda-filter-form nil)
6728 (org-agenda-set-mode-name))
6730 (defun org-agenda-manipulate-query-add ()
6731 "Manipulate the query by adding a search term with positive selection.
6732 Positive selection means the term must be matched for selection of an entry."
6733 (interactive)
6734 (org-agenda-manipulate-query ?\[))
6735 (defun org-agenda-manipulate-query-subtract ()
6736 "Manipulate the query by adding a search term with negative selection.
6737 Negative selection means term must not be matched for selection of an entry."
6738 (interactive)
6739 (org-agenda-manipulate-query ?\]))
6740 (defun org-agenda-manipulate-query-add-re ()
6741 "Manipulate the query by adding a search regexp with positive selection.
6742 Positive selection means the regexp must match for selection of an entry."
6743 (interactive)
6744 (org-agenda-manipulate-query ?\{))
6745 (defun org-agenda-manipulate-query-subtract-re ()
6746 "Manipulate the query by adding a search regexp with negative selection.
6747 Negative selection means regexp must not match for selection of an entry."
6748 (interactive)
6749 (org-agenda-manipulate-query ?\}))
6750 (defun org-agenda-manipulate-query (char)
6751 (cond
6752 ((memq org-agenda-type '(timeline agenda))
6753 (let ((org-agenda-include-inactive-timestamps t))
6754 (org-agenda-redo))
6755 (message "Display now includes inactive timestamps as well"))
6756 ((eq org-agenda-type 'search)
6757 (org-add-to-string
6758 'org-agenda-query-string
6759 (if org-agenda-last-search-view-search-was-boolean
6760 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6761 (?\{ . " +{}") (?\} . " -{}"))))
6762 " "))
6763 (setq org-agenda-redo-command
6764 (list 'org-search-view
6765 org-todo-only
6766 org-agenda-query-string
6767 (+ (length org-agenda-query-string)
6768 (if (member char '(?\{ ?\})) 0 1))))
6769 (set-register org-agenda-query-register org-agenda-query-string)
6770 (org-agenda-redo))
6771 (t (error "Cannot manipulate query for %s-type agenda buffers"
6772 org-agenda-type))))
6774 (defun org-add-to-string (var string)
6775 (set var (concat (symbol-value var) string)))
6777 (defun org-agenda-goto-date (date)
6778 "Jump to DATE in agenda."
6779 (interactive (list (let ((org-read-date-prefer-future
6780 (eval org-agenda-jump-prefer-future)))
6781 (org-read-date))))
6782 (org-agenda-list nil date))
6784 (defun org-agenda-goto-today ()
6785 "Go to today."
6786 (interactive)
6787 (org-agenda-check-type t 'timeline 'agenda)
6788 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6789 (cond
6790 (tdpos (goto-char tdpos))
6791 ((eq org-agenda-type 'agenda)
6792 (let* ((sd (org-agenda-compute-starting-span
6793 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6794 (org-agenda-overriding-arguments org-agenda-last-arguments))
6795 (setf (nth 1 org-agenda-overriding-arguments) sd)
6796 (org-agenda-redo)
6797 (org-agenda-find-same-or-today-or-agenda)))
6798 (t (error "Cannot find today")))))
6800 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6801 (goto-char
6802 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6803 (text-property-any (point-min) (point-max) 'org-today t)
6804 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6805 (point-min))))
6807 (defun org-agenda-later (arg)
6808 "Go forward in time by thee current span.
6809 With prefix ARG, go forward that many times the current span."
6810 (interactive "p")
6811 (org-agenda-check-type t 'agenda)
6812 (let* ((span org-agenda-current-span)
6813 (sd org-starting-day)
6814 (greg (calendar-gregorian-from-absolute sd))
6815 (cnt (org-get-at-bol 'org-day-cnt))
6816 greg2)
6817 (cond
6818 ((eq span 'day)
6819 (setq sd (+ arg sd)))
6820 ((eq span 'week)
6821 (setq sd (+ (* 7 arg) sd)))
6822 ((eq span 'month)
6823 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6824 sd (calendar-absolute-from-gregorian greg2))
6825 (setcar greg2 (1+ (car greg2))))
6826 ((eq span 'year)
6827 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6828 sd (calendar-absolute-from-gregorian greg2))
6829 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6831 (setq sd (+ (* span arg) sd))))
6832 (let ((org-agenda-overriding-arguments
6833 (list (car org-agenda-last-arguments) sd span t)))
6834 (org-agenda-redo)
6835 (org-agenda-find-same-or-today-or-agenda cnt))))
6837 (defun org-agenda-earlier (arg)
6838 "Go backward in time by the current span.
6839 With prefix ARG, go backward that many times the current span."
6840 (interactive "p")
6841 (org-agenda-later (- arg)))
6843 (defun org-agenda-view-mode-dispatch ()
6844 "Call one of the view mode commands."
6845 (interactive)
6846 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6847 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6848 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6849 [E]ntryText")
6850 (let ((a (read-char-exclusive)))
6851 (case a
6852 (?\ (call-interactively 'org-agenda-reset-view))
6853 (?d (call-interactively 'org-agenda-day-view))
6854 (?w (call-interactively 'org-agenda-week-view))
6855 (?m (call-interactively 'org-agenda-month-view))
6856 (?y (call-interactively 'org-agenda-year-view))
6857 (?l (call-interactively 'org-agenda-log-mode))
6858 (?L (org-agenda-log-mode '(4)))
6859 (?c (org-agenda-log-mode 'clockcheck))
6860 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6861 (?a (call-interactively 'org-agenda-archives-mode))
6862 (?A (org-agenda-archives-mode 'files))
6863 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6864 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6865 (?G (call-interactively 'org-agenda-toggle-time-grid))
6866 (?D (call-interactively 'org-agenda-toggle-diary))
6867 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6868 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6869 (org-agenda-check-type t 'timeline 'agenda)
6870 (org-agenda-redo))
6871 (message "Display now includes inactive timestamps as well"))
6872 (?q (message "Abort"))
6873 (otherwise (error "Invalid key" )))))
6875 (defun org-agenda-reset-view ()
6876 "Switch to default view for agenda."
6877 (interactive)
6878 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6879 (defun org-agenda-day-view (&optional day-of-year)
6880 "Switch to daily view for agenda.
6881 With argument DAY-OF-YEAR, switch to that day of the year."
6882 (interactive "P")
6883 (org-agenda-change-time-span 'day day-of-year))
6884 (defun org-agenda-week-view (&optional iso-week)
6885 "Switch to daily view for agenda.
6886 With argument ISO-WEEK, switch to the corresponding ISO week.
6887 If ISO-WEEK has more then 2 digits, only the last two encode the
6888 week. Any digits before this encode a year. So 200712 means
6889 week 12 of year 2007. Years in the range 1938-2037 can also be
6890 written as 2-digit years."
6891 (interactive "P")
6892 (org-agenda-change-time-span 'week iso-week))
6893 (defun org-agenda-month-view (&optional month)
6894 "Switch to monthly view for agenda.
6895 With argument MONTH, switch to that month."
6896 (interactive "P")
6897 (org-agenda-change-time-span 'month month))
6898 (defun org-agenda-year-view (&optional year)
6899 "Switch to yearly view for agenda.
6900 With argument YEAR, switch to that year.
6901 If MONTH has more then 2 digits, only the last two encode the
6902 month. Any digits before this encode a year. So 200712 means
6903 December year 2007. Years in the range 1938-2037 can also be
6904 written as 2-digit years."
6905 (interactive "P")
6906 (when year
6907 (setq year (org-small-year-to-year year)))
6908 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6909 (org-agenda-change-time-span 'year year)
6910 (error "Abort")))
6912 (defun org-agenda-change-time-span (span &optional n)
6913 "Change the agenda view to SPAN.
6914 SPAN may be `day', `week', `month', `year'."
6915 (org-agenda-check-type t 'agenda)
6916 (if (and (not n) (equal org-agenda-current-span span))
6917 (error "Viewing span is already \"%s\"" span))
6918 (let* ((sd (or (org-get-at-bol 'day)
6919 org-starting-day))
6920 (sd (org-agenda-compute-starting-span sd span n))
6921 (org-agenda-overriding-arguments
6922 (or org-agenda-overriding-arguments
6923 (list (car org-agenda-last-arguments) sd span t))))
6924 (org-agenda-redo)
6925 (org-agenda-find-same-or-today-or-agenda))
6926 (org-agenda-set-mode-name)
6927 (message "Switched to %s view" span))
6929 (defun org-agenda-compute-starting-span (sd span &optional n)
6930 "Compute starting date for agenda.
6931 SPAN may be `day', `week', `month', `year'. The return value
6932 is a cons cell with the starting date and the number of days,
6933 so that the date SD will be in that range."
6934 (let* ((greg (calendar-gregorian-from-absolute sd))
6935 (dg (nth 1 greg))
6936 (mg (car greg))
6937 (yg (nth 2 greg)))
6938 (cond
6939 ((eq span 'day)
6940 (when n
6941 (setq sd (+ (calendar-absolute-from-gregorian
6942 (list mg 1 yg))
6943 n -1))))
6944 ((eq span 'week)
6945 (let* ((nt (calendar-day-of-week
6946 (calendar-gregorian-from-absolute sd)))
6947 (d (if org-agenda-start-on-weekday
6948 (- nt org-agenda-start-on-weekday)
6951 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6952 (when n
6953 (require 'cal-iso)
6954 (when (> n 99)
6955 (setq y1 (org-small-year-to-year (/ n 100))
6956 n (mod n 100)))
6957 (setq sd
6958 (calendar-absolute-from-iso
6959 (list n 1
6960 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6961 ((eq span 'month)
6962 (let (y1)
6963 (when (and n (> n 99))
6964 (setq y1 (org-small-year-to-year (/ n 100))
6965 n (mod n 100)))
6966 (setq sd (calendar-absolute-from-gregorian
6967 (list (or n mg) 1 (or y1 yg))))))
6968 ((eq span 'year)
6969 (setq sd (calendar-absolute-from-gregorian
6970 (list 1 1 (or n yg))))))
6971 sd))
6973 (defun org-agenda-next-date-line (&optional arg)
6974 "Jump to the next line indicating a date in agenda buffer."
6975 (interactive "p")
6976 (org-agenda-check-type t 'agenda 'timeline)
6977 (beginning-of-line 1)
6978 ;; This does not work if user makes date format that starts with a blank
6979 (if (looking-at "^\\S-") (forward-char 1))
6980 (if (not (re-search-forward "^\\S-" nil t arg))
6981 (progn
6982 (backward-char 1)
6983 (error "No next date after this line in this buffer")))
6984 (goto-char (match-beginning 0)))
6986 (defun org-agenda-previous-date-line (&optional arg)
6987 "Jump to the previous line indicating a date in agenda buffer."
6988 (interactive "p")
6989 (org-agenda-check-type t 'agenda 'timeline)
6990 (beginning-of-line 1)
6991 (if (not (re-search-backward "^\\S-" nil t arg))
6992 (error "No previous date before this line in this buffer")))
6994 ;; Initialize the highlight
6995 (defvar org-hl (make-overlay 1 1))
6996 (overlay-put org-hl 'face 'highlight)
6998 (defun org-highlight (begin end &optional buffer)
6999 "Highlight a region with overlay."
7000 (move-overlay org-hl begin end (or buffer (current-buffer))))
7002 (defun org-unhighlight ()
7003 "Detach overlay INDEX."
7004 (org-detach-overlay org-hl))
7006 ;; FIXME this is currently not used.
7007 (defun org-highlight-until-next-command (beg end &optional buffer)
7008 "Move the highlight overlay to BEG/END, remove it before the next command."
7009 (org-highlight beg end buffer)
7010 (add-hook 'pre-command-hook 'org-unhighlight-once))
7011 (defun org-unhighlight-once ()
7012 "Remove the highlight from its position, and this function from the hook."
7013 (remove-hook 'pre-command-hook 'org-unhighlight-once)
7014 (org-unhighlight))
7016 (defun org-agenda-follow-mode ()
7017 "Toggle follow mode in an agenda buffer."
7018 (interactive)
7019 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
7020 (org-agenda-set-mode-name)
7021 (org-agenda-do-context-action)
7022 (message "Follow mode is %s"
7023 (if org-agenda-follow-mode "on" "off")))
7025 (defun org-agenda-entry-text-mode (&optional arg)
7026 "Toggle entry text mode in an agenda buffer."
7027 (interactive "P")
7028 (setq org-agenda-entry-text-mode (or (integerp arg)
7029 (not org-agenda-entry-text-mode)))
7030 (org-agenda-entry-text-hide)
7031 (and org-agenda-entry-text-mode
7032 (let ((org-agenda-entry-text-maxlines
7033 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7034 (org-agenda-entry-text-show)))
7035 (org-agenda-set-mode-name)
7036 (message "Entry text mode is %s. Maximum number of lines is %d"
7037 (if org-agenda-entry-text-mode "on" "off")
7038 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7040 (defun org-agenda-clockreport-mode (&optional with-filter)
7041 "Toggle clocktable mode in an agenda buffer.
7042 With prefix arg WITH-FILTER, make the clocktable respect the current
7043 agenda filter."
7044 (interactive "P")
7045 (org-agenda-check-type t 'agenda)
7046 (if with-filter
7047 (setq org-agenda-clockreport-mode 'with-filter)
7048 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
7049 (org-agenda-set-mode-name)
7050 (org-agenda-redo)
7051 (message "Clocktable mode is %s"
7052 (if org-agenda-clockreport-mode "on" "off")))
7054 (defun org-agenda-log-mode (&optional special)
7055 "Toggle log mode in an agenda buffer.
7056 With argument SPECIAL, show all possible log items, not only the ones
7057 configured in `org-agenda-log-mode-items'.
7058 With a double `C-u' prefix arg, show *only* log items, nothing else."
7059 (interactive "P")
7060 (org-agenda-check-type t 'agenda 'timeline)
7061 (setq org-agenda-show-log
7062 (cond
7063 ((equal special '(16)) 'only)
7064 ((eq special 'clockcheck)
7065 (if (eq org-agenda-show-log 'clockcheck)
7066 nil 'clockcheck))
7067 (special '(closed clock state))
7068 (t (not org-agenda-show-log))))
7069 (org-agenda-set-mode-name)
7070 (org-agenda-redo)
7071 (message "Log mode is %s"
7072 (if org-agenda-show-log "on" "off")))
7074 (defun org-agenda-archives-mode (&optional with-files)
7075 "Toggle inclusion of items in trees marked with :ARCHIVE:.
7076 When called with a prefix argument, include all archive files as well."
7077 (interactive "P")
7078 (setq org-agenda-archives-mode
7079 (if with-files t (if org-agenda-archives-mode nil 'trees)))
7080 (org-agenda-set-mode-name)
7081 (org-agenda-redo)
7082 (message
7083 "%s"
7084 (cond
7085 ((eq org-agenda-archives-mode nil)
7086 "No archives are included")
7087 ((eq org-agenda-archives-mode 'trees)
7088 (format "Trees with :%s: tag are included" org-archive-tag))
7089 ((eq org-agenda-archives-mode t)
7090 (format "Trees with :%s: tag and all active archive files are included"
7091 org-archive-tag)))))
7093 (defun org-agenda-toggle-diary ()
7094 "Toggle diary inclusion in an agenda buffer."
7095 (interactive)
7096 (org-agenda-check-type t 'agenda)
7097 (setq org-agenda-include-diary (not org-agenda-include-diary))
7098 (org-agenda-redo)
7099 (org-agenda-set-mode-name)
7100 (message "Diary inclusion turned %s"
7101 (if org-agenda-include-diary "on" "off")))
7103 (defun org-agenda-toggle-deadlines ()
7104 "Toggle inclusion of entries with a deadline in an agenda buffer."
7105 (interactive)
7106 (org-agenda-check-type t 'agenda)
7107 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
7108 (org-agenda-redo)
7109 (org-agenda-set-mode-name)
7110 (message "Deadlines inclusion turned %s"
7111 (if org-agenda-include-deadlines "on" "off")))
7113 (defun org-agenda-toggle-time-grid ()
7114 "Toggle time grid in an agenda buffer."
7115 (interactive)
7116 (org-agenda-check-type t 'agenda)
7117 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
7118 (org-agenda-redo)
7119 (org-agenda-set-mode-name)
7120 (message "Time-grid turned %s"
7121 (if org-agenda-use-time-grid "on" "off")))
7123 (defun org-agenda-set-mode-name ()
7124 "Set the mode name to indicate all the small mode settings."
7125 (setq mode-name
7126 (list "Org-Agenda"
7127 (if (get 'org-agenda-files 'org-restrict) " []" "")
7129 '(:eval (org-agenda-span-name org-agenda-current-span))
7130 (if org-agenda-follow-mode " Follow" "")
7131 (if org-agenda-entry-text-mode " ETxt" "")
7132 (if org-agenda-include-diary " Diary" "")
7133 (if org-agenda-include-deadlines " Ddl" "")
7134 (if org-agenda-use-time-grid " Grid" "")
7135 (if (and (boundp 'org-habit-show-habits)
7136 org-habit-show-habits) " Habit" "")
7137 (cond
7138 ((consp org-agenda-show-log) " LogAll")
7139 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
7140 (org-agenda-show-log " Log")
7141 (t ""))
7142 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
7143 :preset-filter))
7144 '(:eval (org-propertize
7145 (concat " <"
7146 (mapconcat
7147 'identity
7148 (append
7149 (get 'org-agenda-category-filter :preset-filter)
7150 org-agenda-category-filter)
7152 ">")
7153 'face 'org-agenda-filter-category
7154 'help-echo "Category used in filtering"))
7156 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
7157 :preset-filter))
7158 '(:eval (org-propertize
7159 (concat " {"
7160 (mapconcat
7161 'identity
7162 (append
7163 (get 'org-agenda-tag-filter :preset-filter)
7164 org-agenda-tag-filter)
7166 "}")
7167 'face 'org-agenda-filter-tags
7168 'help-echo "Tags used in filtering"))
7170 (if org-agenda-archives-mode
7171 (if (eq org-agenda-archives-mode t)
7172 " Archives"
7173 (format " :%s:" org-archive-tag))
7175 (if org-agenda-clockreport-mode
7176 (if (eq org-agenda-clockreport-mode 'with-filter)
7177 " Clock{}" " Clock")
7178 "")))
7179 (force-mode-line-update))
7181 (defun org-agenda-post-command-hook ()
7182 (setq org-agenda-type
7183 (or (get-text-property (point) 'org-agenda-type)
7184 (get-text-property (max (point-min) (1- (point)))
7185 'org-agenda-type))))
7187 (defun org-agenda-next-line ()
7188 "Move cursor to the next line, and show if follow mode is active."
7189 (interactive)
7190 (call-interactively 'next-line)
7191 (org-agenda-do-context-action))
7193 (defun org-agenda-previous-line ()
7194 "Move cursor to the previous line, and show if follow-mode is active."
7195 (interactive)
7196 (call-interactively 'previous-line)
7197 (org-agenda-do-context-action))
7199 (defun org-agenda-do-context-action ()
7200 "Show outline path and, maybe, follow mode window."
7201 (let ((m (org-get-at-bol 'org-marker)))
7202 (when (and (markerp m) (marker-buffer m))
7203 (and org-agenda-follow-mode
7204 (if org-agenda-follow-indirect
7205 (org-agenda-tree-to-indirect-buffer)
7206 (org-agenda-show)))
7207 (and org-agenda-show-outline-path
7208 (org-with-point-at m (org-display-outline-path t))))))
7210 (defun org-agenda-show-priority ()
7211 "Show the priority of the current item.
7212 This priority is composed of the main priority given with the [#A] cookies,
7213 and by additional input from the age of a schedules or deadline entry."
7214 (interactive)
7215 (let* ((pri (org-get-at-bol 'priority)))
7216 (message "Priority is %d" (if pri pri -1000))))
7218 (defun org-agenda-show-tags ()
7219 "Show the tags applicable to the current item."
7220 (interactive)
7221 (let* ((tags (org-get-at-bol 'tags)))
7222 (if tags
7223 (message "Tags are :%s:"
7224 (org-no-properties (mapconcat 'identity tags ":")))
7225 (message "No tags associated with this line"))))
7227 (defun org-agenda-goto (&optional highlight)
7228 "Go to the Org-mode file which contains the item at point."
7229 (interactive)
7230 (let* ((marker (or (org-get-at-bol 'org-marker)
7231 (org-agenda-error)))
7232 (buffer (marker-buffer marker))
7233 (pos (marker-position marker)))
7234 (switch-to-buffer-other-window buffer)
7235 (widen)
7236 (push-mark)
7237 (goto-char pos)
7238 (when (derived-mode-p 'org-mode)
7239 (org-show-context 'agenda)
7240 (save-excursion
7241 (and (outline-next-heading)
7242 (org-flag-heading nil)))) ; show the next heading
7243 (when (outline-invisible-p)
7244 (show-entry)) ; display invisible text
7245 (recenter (/ (window-height) 2))
7246 (run-hooks 'org-agenda-after-show-hook)
7247 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
7249 (defvar org-agenda-after-show-hook nil
7250 "Normal hook run after an item has been shown from the agenda.
7251 Point is in the buffer where the item originated.")
7253 (defun org-agenda-kill ()
7254 "Kill the entry or subtree belonging to the current agenda entry."
7255 (interactive)
7256 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7257 (let* ((marker (or (org-get-at-bol 'org-marker)
7258 (org-agenda-error)))
7259 (buffer (marker-buffer marker))
7260 (pos (marker-position marker))
7261 (type (org-get-at-bol 'type))
7262 dbeg dend (n 0) conf)
7263 (org-with-remote-undo buffer
7264 (with-current-buffer buffer
7265 (save-excursion
7266 (goto-char pos)
7267 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
7268 (setq dbeg (progn (org-back-to-heading t) (point))
7269 dend (org-end-of-subtree t t))
7270 (setq dbeg (point-at-bol)
7271 dend (min (point-max) (1+ (point-at-eol)))))
7272 (goto-char dbeg)
7273 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
7274 (setq conf (or (eq t org-agenda-confirm-kill)
7275 (and (numberp org-agenda-confirm-kill)
7276 (> n org-agenda-confirm-kill))))
7277 (and conf
7278 (not (y-or-n-p
7279 (format "Delete entry with %d lines in buffer \"%s\"? "
7280 n (buffer-name buffer))))
7281 (error "Abort"))
7282 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
7283 (with-current-buffer buffer (delete-region dbeg dend))
7284 (message "Agenda item and source killed"))))
7286 (defvar org-archive-default-command)
7287 (defun org-agenda-archive-default ()
7288 "Archive the entry or subtree belonging to the current agenda entry."
7289 (interactive)
7290 (require 'org-archive)
7291 (org-agenda-archive-with org-archive-default-command))
7293 (defun org-agenda-archive-default-with-confirmation ()
7294 "Archive the entry or subtree belonging to the current agenda entry."
7295 (interactive)
7296 (require 'org-archive)
7297 (org-agenda-archive-with org-archive-default-command 'confirm))
7299 (defun org-agenda-archive ()
7300 "Archive the entry or subtree belonging to the current agenda entry."
7301 (interactive)
7302 (org-agenda-archive-with 'org-archive-subtree))
7304 (defun org-agenda-archive-to-archive-sibling ()
7305 "Move the entry to the archive sibling."
7306 (interactive)
7307 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7309 (defun org-agenda-archive-with (cmd &optional confirm)
7310 "Move the entry to the archive sibling."
7311 (interactive)
7312 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7313 (let* ((marker (or (org-get-at-bol 'org-marker)
7314 (org-agenda-error)))
7315 (buffer (marker-buffer marker))
7316 (pos (marker-position marker)))
7317 (org-with-remote-undo buffer
7318 (with-current-buffer buffer
7319 (if (derived-mode-p 'org-mode)
7320 (if (and confirm
7321 (not (y-or-n-p "Archive this subtree or entry? ")))
7322 (error "Abort")
7323 (save-excursion
7324 (goto-char pos)
7325 (org-remove-subtree-entries-from-agenda)
7326 (org-back-to-heading t)
7327 (funcall cmd)))
7328 (error "Archiving works only in Org-mode files"))))))
7330 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7331 "Remove all lines in the agenda that correspond to a given subtree.
7332 The subtree is the one in buffer BUF, starting at BEG and ending at END.
7333 If this information is not given, the function uses the tree at point."
7334 (let ((buf (or buf (current-buffer))) m p)
7335 (save-excursion
7336 (unless (and beg end)
7337 (org-back-to-heading t)
7338 (setq beg (point))
7339 (org-end-of-subtree t)
7340 (setq end (point)))
7341 (set-buffer (get-buffer org-agenda-buffer-name))
7342 (save-excursion
7343 (goto-char (point-max))
7344 (beginning-of-line 1)
7345 (while (not (bobp))
7346 (when (and (setq m (org-get-at-bol 'org-marker))
7347 (equal buf (marker-buffer m))
7348 (setq p (marker-position m))
7349 (>= p beg)
7350 (< p end))
7351 (let ((inhibit-read-only t))
7352 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7353 (beginning-of-line 0))))))
7355 (defun org-agenda-refile (&optional goto rfloc no-update)
7356 "Refile the item at point."
7357 (interactive "P")
7358 (if (equal goto '(16))
7359 (org-refile-goto-last-stored)
7360 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7361 (org-agenda-error)))
7362 (buffer (marker-buffer marker))
7363 (pos (marker-position marker))
7364 (rfloc (or rfloc
7365 (org-refile-get-location
7366 (if goto "Goto" "Refile to") buffer
7367 org-refile-allow-creating-parent-nodes))))
7368 (with-current-buffer buffer
7369 (save-excursion
7370 (save-restriction
7371 (widen)
7372 (goto-char marker)
7373 (org-remove-subtree-entries-from-agenda)
7374 (org-refile goto buffer rfloc)))))
7375 (unless no-update (org-agenda-redo))))
7377 (defun org-agenda-open-link (&optional arg)
7378 "Follow the link in the current line, if any.
7379 This looks for a link in the displayed line in the agenda. It also looks
7380 at the text of the entry itself."
7381 (interactive "P")
7382 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7383 (org-get-at-bol 'org-marker)))
7384 (buffer (and marker (marker-buffer marker)))
7385 (prefix (buffer-substring
7386 (point-at-bol) (point-at-eol))))
7387 (cond
7388 (buffer
7389 (with-current-buffer buffer
7390 (save-excursion
7391 (save-restriction
7392 (widen)
7393 (goto-char marker)
7394 (org-offer-links-in-entry arg prefix)))))
7395 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7396 (save-excursion
7397 (beginning-of-line 1)
7398 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7399 (org-open-link-from-string (match-string 1)))
7400 (t (error "No link to open here")))))
7402 (defun org-agenda-copy-local-variable (var)
7403 "Get a variable from a referenced buffer and install it here."
7404 (let ((m (org-get-at-bol 'org-marker)))
7405 (when (and m (buffer-live-p (marker-buffer m)))
7406 (org-set-local var (with-current-buffer (marker-buffer m)
7407 (symbol-value var))))))
7409 (defun org-agenda-switch-to (&optional delete-other-windows)
7410 "Go to the Org-mode file which contains the item at point."
7411 (interactive)
7412 (if (and org-return-follows-link
7413 (not (org-get-at-bol 'org-marker))
7414 (org-in-regexp org-bracket-link-regexp))
7415 (org-open-link-from-string (match-string 0))
7416 (let* ((marker (or (org-get-at-bol 'org-marker)
7417 (org-agenda-error)))
7418 (buffer (marker-buffer marker))
7419 (pos (marker-position marker)))
7420 (org-pop-to-buffer-same-window buffer)
7421 (and delete-other-windows (delete-other-windows))
7422 (widen)
7423 (goto-char pos)
7424 (when (derived-mode-p 'org-mode)
7425 (org-show-context 'agenda)
7426 (save-excursion
7427 (and (outline-next-heading)
7428 (org-flag-heading nil))) ; show the next heading
7429 (when (outline-invisible-p)
7430 (show-entry)))))) ; display invisible text
7432 (defun org-agenda-goto-mouse (ev)
7433 "Go to the Org-mode file which contains the item at the mouse click."
7434 (interactive "e")
7435 (mouse-set-point ev)
7436 (org-agenda-goto))
7438 (defun org-agenda-show (&optional full-entry)
7439 "Display the Org-mode file which contains the item at point.
7440 With prefix argument FULL-ENTRY, make the entire entry visible
7441 if it was hidden in the outline."
7442 (interactive "P")
7443 (let ((win (selected-window)))
7444 (if full-entry
7445 (let ((org-show-entry-below t))
7446 (org-agenda-goto t))
7447 (org-agenda-goto t))
7448 (select-window win)))
7450 (defvar org-agenda-show-window nil)
7451 (defun org-agenda-show-and-scroll-up (&optional arg)
7452 "Display the Org-mode file which contains the item at point.
7453 When called repeatedly, scroll the window that is displaying the buffer.
7454 With a \\[universal-argument] prefix, use `org-show-entry' instead of
7455 `show-subtree' to display the item, so that drawers and logbooks stay
7456 folded."
7457 (interactive "P")
7458 (let ((win (selected-window)))
7459 (if (and (window-live-p org-agenda-show-window)
7460 (eq this-command last-command))
7461 (progn
7462 (select-window org-agenda-show-window)
7463 (ignore-errors (scroll-up)))
7464 (org-agenda-goto t)
7465 (if arg (org-show-entry) (show-subtree))
7466 (setq org-agenda-show-window (selected-window)))
7467 (select-window win)))
7469 (defun org-agenda-show-scroll-down ()
7470 "Scroll down the window showing the agenda."
7471 (interactive)
7472 (let ((win (selected-window)))
7473 (when (window-live-p org-agenda-show-window)
7474 (select-window org-agenda-show-window)
7475 (ignore-errors (scroll-down))
7476 (select-window win))))
7478 (defun org-agenda-show-1 (&optional more)
7479 "Display the Org-mode file which contains the item at point.
7480 The prefix arg selects the amount of information to display:
7482 0 hide the subtree
7483 1 just show the entry according to defaults.
7484 2 show the children view
7485 3 show the subtree view
7486 4 show the entire subtree and any LOGBOOK drawers
7487 5 show the entire subtree and any drawers
7488 With prefix argument FULL-ENTRY, make the entire entry visible
7489 if it was hidden in the outline."
7490 (interactive "p")
7491 (let ((win (selected-window)))
7492 (org-agenda-goto t)
7493 (org-recenter-heading 1)
7494 (cond
7495 ((= more 0)
7496 (hide-subtree)
7497 (save-excursion
7498 (org-back-to-heading)
7499 (run-hook-with-args 'org-cycle-hook 'folded))
7500 (message "Remote: FOLDED"))
7501 ((and (org-called-interactively-p 'any) (= more 1))
7502 (message "Remote: show with default settings"))
7503 ((= more 2)
7504 (show-entry)
7505 (show-children)
7506 (save-excursion
7507 (org-back-to-heading)
7508 (run-hook-with-args 'org-cycle-hook 'children))
7509 (message "Remote: CHILDREN"))
7510 ((= more 3)
7511 (show-subtree)
7512 (save-excursion
7513 (org-back-to-heading)
7514 (run-hook-with-args 'org-cycle-hook 'subtree))
7515 (message "Remote: SUBTREE"))
7516 ((= more 4)
7517 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7518 (org-drawer-regexp
7519 (concat "^[ \t]*:\\("
7520 (mapconcat 'regexp-quote org-drawers "\\|")
7521 "\\):[ \t]*$")))
7522 (show-subtree)
7523 (save-excursion
7524 (org-back-to-heading)
7525 (org-cycle-hide-drawers 'subtree)))
7526 (message "Remote: SUBTREE AND LOGBOOK"))
7527 ((> more 4)
7528 (show-subtree)
7529 (message "Remote: SUBTREE AND ALL DRAWERS")))
7530 (select-window win)))
7532 (defun org-recenter-heading (n)
7533 (save-excursion
7534 (org-back-to-heading)
7535 (recenter n)))
7537 (defvar org-agenda-cycle-counter nil)
7538 (defun org-agenda-cycle-show (&optional n)
7539 "Show the current entry in another window, with default settings.
7540 Default settings are taken from `org-show-hierarchy-above' and siblings.
7541 When use repeatedly in immediate succession, the remote entry will cycle
7542 through visibility
7544 children -> subtree -> folded
7546 When called with a numeric prefix arg, that arg will be passed through to
7547 `org-agenda-show-1'. For the interpretation of that argument, see the
7548 docstring of `org-agenda-show-1'."
7549 (interactive "P")
7550 (if (integerp n)
7551 (setq org-agenda-cycle-counter n)
7552 (if (not (eq last-command this-command))
7553 (setq org-agenda-cycle-counter 1)
7554 (if (equal org-agenda-cycle-counter 0)
7555 (setq org-agenda-cycle-counter 2)
7556 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7557 (if (> org-agenda-cycle-counter 3)
7558 (setq org-agenda-cycle-counter 0)))))
7559 (org-agenda-show-1 org-agenda-cycle-counter))
7561 (defun org-agenda-recenter (arg)
7562 "Display the Org-mode file which contains the item at point and recenter."
7563 (interactive "P")
7564 (let ((win (selected-window)))
7565 (org-agenda-goto t)
7566 (recenter arg)
7567 (select-window win)))
7569 (defun org-agenda-show-mouse (ev)
7570 "Display the Org-mode file which contains the item at the mouse click."
7571 (interactive "e")
7572 (mouse-set-point ev)
7573 (org-agenda-show))
7575 (defun org-agenda-check-no-diary ()
7576 "Check if the entry is a diary link and abort if yes."
7577 (if (org-get-at-bol 'org-agenda-diary-link)
7578 (org-agenda-error)))
7580 (defun org-agenda-error ()
7581 (error "Command not allowed in this line"))
7583 (defun org-agenda-tree-to-indirect-buffer ()
7584 "Show the subtree corresponding to the current entry in an indirect buffer.
7585 This calls the command `org-tree-to-indirect-buffer' from the original
7586 Org-mode buffer.
7587 With numerical prefix arg ARG, go up to this level and then take that tree.
7588 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7589 use the dedicated frame)."
7590 (interactive)
7591 (if (and current-prefix-arg (listp current-prefix-arg))
7592 (org-agenda-do-tree-to-indirect-buffer)
7593 (let ((agenda-window (selected-window))
7594 (indirect-window
7595 (and org-last-indirect-buffer
7596 (get-buffer-window org-last-indirect-buffer))))
7597 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer))
7598 (unwind-protect
7599 (progn
7600 (unless (and indirect-window (window-live-p indirect-window))
7601 (setq indirect-window (split-window agenda-window)))
7602 (select-window indirect-window)
7603 (switch-to-buffer org-last-indirect-buffer :norecord)
7604 (fit-window-to-buffer indirect-window))
7605 (select-window (get-buffer-window org-agenda-buffer-name))))))
7607 (defun org-agenda-do-tree-to-indirect-buffer ()
7608 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7609 (org-agenda-check-no-diary)
7610 (let* ((marker (or (org-get-at-bol 'org-marker)
7611 (org-agenda-error)))
7612 (buffer (marker-buffer marker))
7613 (pos (marker-position marker)))
7614 (with-current-buffer buffer
7615 (save-excursion
7616 (goto-char pos)
7617 (call-interactively 'org-tree-to-indirect-buffer)))))
7619 (defvar org-last-heading-marker (make-marker)
7620 "Marker pointing to the headline that last changed its TODO state
7621 by a remote command from the agenda.")
7623 (defun org-agenda-todo-nextset ()
7624 "Switch TODO entry to next sequence."
7625 (interactive)
7626 (org-agenda-todo 'nextset))
7628 (defun org-agenda-todo-previousset ()
7629 "Switch TODO entry to previous sequence."
7630 (interactive)
7631 (org-agenda-todo 'previousset))
7633 (defun org-agenda-todo (&optional arg)
7634 "Cycle TODO state of line at point, also in Org-mode file.
7635 This changes the line at point, all other lines in the agenda referring to
7636 the same tree node, and the headline of the tree node in the Org-mode file."
7637 (interactive "P")
7638 (org-agenda-check-no-diary)
7639 (let* ((col (current-column))
7640 (marker (or (org-get-at-bol 'org-marker)
7641 (org-agenda-error)))
7642 (buffer (marker-buffer marker))
7643 (pos (marker-position marker))
7644 (hdmarker (org-get-at-bol 'org-hd-marker))
7645 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7646 (inhibit-read-only t)
7647 org-agenda-headline-snapshot-before-repeat newhead just-one)
7648 (org-with-remote-undo buffer
7649 (with-current-buffer buffer
7650 (widen)
7651 (goto-char pos)
7652 (org-show-context 'agenda)
7653 (save-excursion
7654 (and (outline-next-heading)
7655 (org-flag-heading nil))) ; show the next heading
7656 (let ((current-prefix-arg arg))
7657 (call-interactively 'org-todo))
7658 (and (bolp) (forward-char 1))
7659 (setq newhead (org-get-heading))
7660 (when (and (org-bound-and-true-p
7661 org-agenda-headline-snapshot-before-repeat)
7662 (not (equal org-agenda-headline-snapshot-before-repeat
7663 newhead))
7664 todayp)
7665 (setq newhead org-agenda-headline-snapshot-before-repeat
7666 just-one t))
7667 (save-excursion
7668 (org-back-to-heading)
7669 (move-marker org-last-heading-marker (point))))
7670 (beginning-of-line 1)
7671 (save-excursion
7672 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7673 (org-move-to-column col))))
7675 (defun org-agenda-add-note (&optional arg)
7676 "Add a time-stamped note to the entry at point."
7677 (interactive "P")
7678 (org-agenda-check-no-diary)
7679 (let* ((marker (or (org-get-at-bol 'org-marker)
7680 (org-agenda-error)))
7681 (buffer (marker-buffer marker))
7682 (pos (marker-position marker))
7683 (hdmarker (org-get-at-bol 'org-hd-marker))
7684 (inhibit-read-only t))
7685 (with-current-buffer buffer
7686 (widen)
7687 (goto-char pos)
7688 (org-show-context 'agenda)
7689 (save-excursion
7690 (and (outline-next-heading)
7691 (org-flag-heading nil))) ; show the next heading
7692 (org-add-note))))
7694 (defun org-agenda-change-all-lines (newhead hdmarker
7695 &optional fixface just-this)
7696 "Change all lines in the agenda buffer which match HDMARKER.
7697 The new content of the line will be NEWHEAD (as modified by
7698 `org-agenda-format-item'). HDMARKER is checked with
7699 `equal' against all `org-hd-marker' text properties in the file.
7700 If FIXFACE is non-nil, the face of each item is modified according to
7701 the new TODO state.
7702 If JUST-THIS is non-nil, change just the current line, not all.
7703 If FORCE-TAGS is non nil, the car of it returns the new tags."
7704 (let* ((inhibit-read-only t)
7705 (line (org-current-line))
7706 (org-agenda-buffer (current-buffer))
7707 (thetags (with-current-buffer (marker-buffer hdmarker)
7708 (save-excursion (save-restriction (widen)
7709 (goto-char hdmarker)
7710 (org-get-tags-at)))))
7711 props m pl undone-face done-face finish new dotime cat tags)
7712 (save-excursion
7713 (goto-char (point-max))
7714 (beginning-of-line 1)
7715 (while (not finish)
7716 (setq finish (bobp))
7717 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7718 (or (not just-this) (= (org-current-line) line))
7719 (equal m hdmarker))
7720 (setq props (text-properties-at (point))
7721 dotime (org-get-at-bol 'dotime)
7722 cat (org-get-at-bol 'org-category)
7723 tags thetags
7725 (let ((org-prefix-format-compiled
7726 (or (get-text-property (point) 'format)
7727 org-prefix-format-compiled))
7728 (extra (org-get-at-bol 'extra)))
7729 (with-current-buffer (marker-buffer hdmarker)
7730 (save-excursion
7731 (save-restriction
7732 (widen)
7733 (org-agenda-format-item extra newhead cat tags dotime)))))
7734 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7735 undone-face (org-get-at-bol 'undone-face)
7736 done-face (org-get-at-bol 'done-face))
7737 (beginning-of-line 1)
7738 (cond
7739 ((equal new "")
7740 (and (looking-at ".*\n?") (replace-match "")))
7741 ((looking-at ".*")
7742 (replace-match new t t)
7743 (beginning-of-line 1)
7744 (add-text-properties (point-at-bol) (point-at-eol) props)
7745 (when fixface
7746 (add-text-properties
7747 (point-at-bol) (point-at-eol)
7748 (list 'face
7749 (if org-last-todo-state-is-todo
7750 undone-face done-face))))
7751 (org-agenda-highlight-todo 'line)
7752 (beginning-of-line 1))
7753 (t (error "Line update did not work"))))
7754 (beginning-of-line 0)))
7755 (org-finalize-agenda)))
7757 (defun org-agenda-align-tags (&optional line)
7758 "Align all tags in agenda items to `org-agenda-tags-column'."
7759 (let ((inhibit-read-only t) l c)
7760 (save-excursion
7761 (goto-char (if line (point-at-bol) (point-min)))
7762 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7763 (if line (point-at-eol) nil) t)
7764 (add-text-properties
7765 (match-beginning 2) (match-end 2)
7766 (list 'face (delq nil (let ((prop (get-text-property
7767 (match-beginning 2) 'face)))
7768 (or (listp prop) (setq prop (list prop)))
7769 (if (memq 'org-tag prop)
7770 prop
7771 (cons 'org-tag prop))))))
7772 (setq l (- (match-end 2) (match-beginning 2))
7773 c (if (< org-agenda-tags-column 0)
7774 (- (abs org-agenda-tags-column) l)
7775 org-agenda-tags-column))
7776 (delete-region (match-beginning 1) (match-end 1))
7777 (goto-char (match-beginning 1))
7778 (insert (org-add-props
7779 (make-string (max 1 (- c (current-column))) ?\ )
7780 (plist-put (copy-sequence (text-properties-at (point)))
7781 'face nil))))
7782 (goto-char (point-min))
7783 (org-font-lock-add-tag-faces (point-max)))))
7785 (defun org-agenda-priority-up ()
7786 "Increase the priority of line at point, also in Org-mode file."
7787 (interactive)
7788 (org-agenda-priority 'up))
7790 (defun org-agenda-priority-down ()
7791 "Decrease the priority of line at point, also in Org-mode file."
7792 (interactive)
7793 (org-agenda-priority 'down))
7795 (defun org-agenda-priority (&optional force-direction)
7796 "Set the priority of line at point, also in Org-mode file.
7797 This changes the line at point, all other lines in the agenda referring to
7798 the same tree node, and the headline of the tree node in the Org-mode file."
7799 (interactive)
7800 (unless org-enable-priority-commands
7801 (error "Priority commands are disabled"))
7802 (org-agenda-check-no-diary)
7803 (let* ((marker (or (org-get-at-bol 'org-marker)
7804 (org-agenda-error)))
7805 (hdmarker (org-get-at-bol 'org-hd-marker))
7806 (buffer (marker-buffer hdmarker))
7807 (pos (marker-position hdmarker))
7808 (inhibit-read-only t)
7809 newhead)
7810 (org-with-remote-undo buffer
7811 (with-current-buffer buffer
7812 (widen)
7813 (goto-char pos)
7814 (org-show-context 'agenda)
7815 (save-excursion
7816 (and (outline-next-heading)
7817 (org-flag-heading nil))) ; show the next heading
7818 (funcall 'org-priority force-direction)
7819 (end-of-line 1)
7820 (setq newhead (org-get-heading)))
7821 (org-agenda-change-all-lines newhead hdmarker)
7822 (beginning-of-line 1))))
7824 ;; FIXME: should fix the tags property of the agenda line.
7825 (defun org-agenda-set-tags (&optional tag onoff)
7826 "Set tags for the current headline."
7827 (interactive)
7828 (org-agenda-check-no-diary)
7829 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7830 (call-interactively 'org-change-tag-in-region)
7831 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7832 (org-agenda-error)))
7833 (buffer (marker-buffer hdmarker))
7834 (pos (marker-position hdmarker))
7835 (inhibit-read-only t)
7836 newhead)
7837 (org-with-remote-undo buffer
7838 (with-current-buffer buffer
7839 (widen)
7840 (goto-char pos)
7841 (save-excursion
7842 (org-show-context 'agenda))
7843 (save-excursion
7844 (and (outline-next-heading)
7845 (org-flag-heading nil))) ; show the next heading
7846 (goto-char pos)
7847 (if tag
7848 (org-toggle-tag tag onoff)
7849 (call-interactively 'org-set-tags))
7850 (end-of-line 1)
7851 (setq newhead (org-get-heading)))
7852 (org-agenda-change-all-lines newhead hdmarker)
7853 (beginning-of-line 1)))))
7855 (defun org-agenda-set-property ()
7856 "Set a property for the current headline."
7857 (interactive)
7858 (org-agenda-check-no-diary)
7859 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7860 (org-agenda-error)))
7861 (buffer (marker-buffer hdmarker))
7862 (pos (marker-position hdmarker))
7863 (inhibit-read-only t)
7864 newhead)
7865 (org-with-remote-undo buffer
7866 (with-current-buffer buffer
7867 (widen)
7868 (goto-char pos)
7869 (save-excursion
7870 (org-show-context 'agenda))
7871 (save-excursion
7872 (and (outline-next-heading)
7873 (org-flag-heading nil))) ; show the next heading
7874 (goto-char pos)
7875 (call-interactively 'org-set-property)))))
7877 (defun org-agenda-set-effort ()
7878 "Set the effort property for the current headline."
7879 (interactive)
7880 (org-agenda-check-no-diary)
7881 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7882 (org-agenda-error)))
7883 (buffer (marker-buffer hdmarker))
7884 (pos (marker-position hdmarker))
7885 (inhibit-read-only t)
7886 newhead)
7887 (org-with-remote-undo buffer
7888 (with-current-buffer buffer
7889 (widen)
7890 (goto-char pos)
7891 (save-excursion
7892 (org-show-context 'agenda))
7893 (save-excursion
7894 (and (outline-next-heading)
7895 (org-flag-heading nil))) ; show the next heading
7896 (goto-char pos)
7897 (call-interactively 'org-set-effort)
7898 (end-of-line 1)
7899 (setq newhead (org-get-heading)))
7900 (org-agenda-change-all-lines newhead hdmarker))))
7902 (defun org-agenda-toggle-archive-tag ()
7903 "Toggle the archive tag for the current entry."
7904 (interactive)
7905 (org-agenda-check-no-diary)
7906 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7907 (org-agenda-error)))
7908 (buffer (marker-buffer hdmarker))
7909 (pos (marker-position hdmarker))
7910 (inhibit-read-only t)
7911 newhead)
7912 (org-with-remote-undo buffer
7913 (with-current-buffer buffer
7914 (widen)
7915 (goto-char pos)
7916 (org-show-context 'agenda)
7917 (save-excursion
7918 (and (outline-next-heading)
7919 (org-flag-heading nil))) ; show the next heading
7920 (call-interactively 'org-toggle-archive-tag)
7921 (end-of-line 1)
7922 (setq newhead (org-get-heading)))
7923 (org-agenda-change-all-lines newhead hdmarker)
7924 (beginning-of-line 1))))
7926 (defun org-agenda-do-date-later (arg)
7927 (interactive "P")
7928 (cond
7929 ((or (equal arg '(16))
7930 (memq last-command
7931 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7932 (setq this-command 'org-agenda-date-later-minutes)
7933 (org-agenda-date-later-minutes 1))
7934 ((or (equal arg '(4))
7935 (memq last-command
7936 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7937 (setq this-command 'org-agenda-date-later-hours)
7938 (org-agenda-date-later-hours 1))
7940 (org-agenda-date-later (prefix-numeric-value arg)))))
7942 (defun org-agenda-do-date-earlier (arg)
7943 (interactive "P")
7944 (cond
7945 ((or (equal arg '(16))
7946 (memq last-command
7947 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7948 (setq this-command 'org-agenda-date-earlier-minutes)
7949 (org-agenda-date-earlier-minutes 1))
7950 ((or (equal arg '(4))
7951 (memq last-command
7952 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7953 (setq this-command 'org-agenda-date-earlier-hours)
7954 (org-agenda-date-earlier-hours 1))
7956 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7958 (defun org-agenda-date-later (arg &optional what)
7959 "Change the date of this item to ARG day(s) later."
7960 (interactive "p")
7961 (org-agenda-check-type t 'agenda 'timeline)
7962 (org-agenda-check-no-diary)
7963 (let* ((marker (or (org-get-at-bol 'org-marker)
7964 (org-agenda-error)))
7965 (buffer (marker-buffer marker))
7966 (pos (marker-position marker))
7967 cdate today)
7968 (org-with-remote-undo buffer
7969 (with-current-buffer buffer
7970 (widen)
7971 (goto-char pos)
7972 (if (not (org-at-timestamp-p))
7973 (error "Cannot find time stamp"))
7974 (when (and org-agenda-move-date-from-past-immediately-to-today
7975 (equal arg 1)
7976 (or (not what) (eq what 'day))
7977 (not (save-match-data (org-at-date-range-p))))
7978 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
7979 cdate (calendar-absolute-from-gregorian
7980 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
7981 today (org-today))
7982 (if (> today cdate)
7983 ;; immediately shift to today
7984 (setq arg (- today cdate))))
7985 (org-timestamp-change arg (or what 'day))
7986 (when (and (org-at-date-range-p)
7987 (re-search-backward org-tr-regexp-both (point-at-bol)))
7988 (let ((end org-last-changed-timestamp))
7989 (org-timestamp-change arg (or what 'day))
7990 (setq org-last-changed-timestamp
7991 (concat org-last-changed-timestamp "--" end)))))
7992 (org-agenda-show-new-time marker org-last-changed-timestamp))
7993 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7995 (defun org-agenda-date-earlier (arg &optional what)
7996 "Change the date of this item to ARG day(s) earlier."
7997 (interactive "p")
7998 (org-agenda-date-later (- arg) what))
8000 (defun org-agenda-date-later-minutes (arg)
8001 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8002 (interactive "p")
8003 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8004 (org-agenda-date-later arg 'minute))
8006 (defun org-agenda-date-earlier-minutes (arg)
8007 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8008 (interactive "p")
8009 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8010 (org-agenda-date-earlier arg 'minute))
8012 (defun org-agenda-date-later-hours (arg)
8013 "Change the time of this item, in hour steps."
8014 (interactive "p")
8015 (org-agenda-date-later arg 'hour))
8017 (defun org-agenda-date-earlier-hours (arg)
8018 "Change the time of this item, in hour steps."
8019 (interactive "p")
8020 (org-agenda-date-earlier arg 'hour))
8022 (defun org-agenda-show-new-time (marker stamp &optional prefix)
8023 "Show new date stamp via text properties."
8024 ;; We use text properties to make this undoable
8025 (let ((inhibit-read-only t)
8026 (buffer-invisibility-spec))
8027 (setq stamp (concat " " prefix " => " stamp))
8028 (save-excursion
8029 (goto-char (point-max))
8030 (while (not (bobp))
8031 (when (equal marker (org-get-at-bol 'org-marker))
8032 (org-move-to-column (- (window-width) (length stamp)) t)
8033 (org-agenda-fix-tags-filter-overlays-at (point))
8034 (if (featurep 'xemacs)
8035 ;; Use `duplicable' property to trigger undo recording
8036 (let ((ex (make-extent nil nil))
8037 (gl (make-glyph stamp)))
8038 (set-glyph-face gl 'secondary-selection)
8039 (set-extent-properties
8040 ex (list 'invisible t 'end-glyph gl 'duplicable t))
8041 (insert-extent ex (1- (point)) (point-at-eol)))
8042 (add-text-properties
8043 (1- (point)) (point-at-eol)
8044 (list 'display (org-add-props stamp nil
8045 'face 'secondary-selection))))
8046 (beginning-of-line 1))
8047 (beginning-of-line 0)))))
8049 (defun org-agenda-date-prompt (arg)
8050 "Change the date of this item. Date is prompted for, with default today.
8051 The prefix ARG is passed to the `org-time-stamp' command and can therefore
8052 be used to request time specification in the time stamp."
8053 (interactive "P")
8054 (org-agenda-check-type t 'agenda 'timeline)
8055 (org-agenda-check-no-diary)
8056 (let* ((marker (or (org-get-at-bol 'org-marker)
8057 (org-agenda-error)))
8058 (buffer (marker-buffer marker))
8059 (pos (marker-position marker)))
8060 (org-with-remote-undo buffer
8061 (with-current-buffer buffer
8062 (widen)
8063 (goto-char pos)
8064 (if (not (org-at-timestamp-p t))
8065 (error "Cannot find time stamp"))
8066 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8067 (org-agenda-show-new-time marker org-last-changed-timestamp))
8068 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8070 (defun org-agenda-schedule (arg &optional time)
8071 "Schedule the item at point.
8072 ARG is passed through to `org-schedule'."
8073 (interactive "P")
8074 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8075 (org-agenda-check-no-diary)
8076 (let* ((marker (or (org-get-at-bol 'org-marker)
8077 (org-agenda-error)))
8078 (type (marker-insertion-type marker))
8079 (buffer (marker-buffer marker))
8080 (pos (marker-position marker))
8081 (org-insert-labeled-timestamps-at-point nil)
8083 (set-marker-insertion-type marker t)
8084 (org-with-remote-undo buffer
8085 (with-current-buffer buffer
8086 (widen)
8087 (goto-char pos)
8088 (setq ts (org-schedule arg time)))
8089 (org-agenda-show-new-time marker ts "S"))
8090 (message "Item scheduled for %s" ts)))
8092 (defun org-agenda-deadline (arg &optional time)
8093 "Schedule the item at point.
8094 ARG is passed through to `org-deadline'."
8095 (interactive "P")
8096 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8097 (org-agenda-check-no-diary)
8098 (let* ((marker (or (org-get-at-bol 'org-marker)
8099 (org-agenda-error)))
8100 (buffer (marker-buffer marker))
8101 (pos (marker-position marker))
8102 (org-insert-labeled-timestamps-at-point nil)
8104 (org-with-remote-undo buffer
8105 (with-current-buffer buffer
8106 (widen)
8107 (goto-char pos)
8108 (setq ts (org-deadline arg time)))
8109 (org-agenda-show-new-time marker ts "D"))
8110 (message "Deadline for this item set to %s" ts)))
8112 (defun org-agenda-action ()
8113 "Select entry for agenda action, or execute an agenda action.
8114 This command prompts for another letter. Valid inputs are:
8116 m Mark the entry at point for an agenda action
8117 s Schedule the marked entry to the date at the cursor
8118 d Set the deadline of the marked entry to the date at the cursor
8119 r Call `org-remember' with cursor date as the default date
8120 c Call `org-capture' with cursor date as the default date
8121 SPC Show marked entry in other window
8122 TAB Visit marked entry in other window
8124 The cursor may be at a date in the calendar, or in the Org agenda."
8125 (interactive)
8126 (let (ans)
8127 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
8128 (setq ans (read-char-exclusive))
8129 (cond
8130 ((equal ans ?m)
8131 ;; Mark this entry
8132 (if (eq major-mode 'org-agenda-mode)
8133 (let ((m (or (org-get-at-bol 'org-hd-marker)
8134 (org-get-at-bol 'org-marker))))
8135 (if m
8136 (progn
8137 (move-marker org-agenda-action-marker
8138 (marker-position m) (marker-buffer m))
8139 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
8140 (error "Don't know which entry to mark")))
8141 (error "This command works only in the agenda")))
8142 ((equal ans ?s)
8143 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
8144 ((equal ans ?d)
8145 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
8146 ((equal ans ?r)
8147 (org-agenda-do-action '(org-remember) t))
8148 ((equal ans ?c)
8149 (org-agenda-do-action '(org-capture) t))
8150 ((equal ans ?\ )
8151 (let ((cw (selected-window)))
8152 (org-switch-to-buffer-other-window
8153 (marker-buffer org-agenda-action-marker))
8154 (goto-char org-agenda-action-marker)
8155 (org-show-context 'agenda)
8156 (select-window cw)))
8157 ((equal ans ?\C-i)
8158 (org-switch-to-buffer-other-window
8159 (marker-buffer org-agenda-action-marker))
8160 (goto-char org-agenda-action-marker)
8161 (org-show-context 'agenda))
8162 (t (error "Invalid agenda action %c" ans)))))
8164 (defun org-agenda-do-action (form &optional current-buffer)
8165 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
8166 (let ((org-overriding-default-time (org-get-cursor-date)))
8167 (if current-buffer
8168 (eval form)
8169 (if (not (marker-buffer org-agenda-action-marker))
8170 (error "No entry has been selected for agenda action")
8171 (with-current-buffer (marker-buffer org-agenda-action-marker)
8172 (save-excursion
8173 (save-restriction
8174 (widen)
8175 (goto-char org-agenda-action-marker)
8176 (eval form))))))))
8178 (defun org-agenda-clock-in (&optional arg)
8179 "Start the clock on the currently selected item."
8180 (interactive "P")
8181 (org-agenda-check-no-diary)
8182 (if (equal arg '(4))
8183 (org-clock-in arg)
8184 (let* ((marker (or (org-get-at-bol 'org-marker)
8185 (org-agenda-error)))
8186 (hdmarker (or (org-get-at-bol 'org-hd-marker)
8187 marker))
8188 (pos (marker-position marker))
8189 newhead)
8190 (org-with-remote-undo (marker-buffer marker)
8191 (with-current-buffer (marker-buffer marker)
8192 (widen)
8193 (goto-char pos)
8194 (org-show-context 'agenda)
8195 (org-show-entry)
8196 (org-cycle-hide-drawers 'children)
8197 (org-clock-in arg)
8198 (setq newhead (org-get-heading)))
8199 (org-agenda-change-all-lines newhead hdmarker)))))
8201 (defun org-agenda-clock-out ()
8202 "Stop the currently running clock."
8203 (interactive)
8204 (unless (marker-buffer org-clock-marker)
8205 (error "No running clock"))
8206 (let ((marker (make-marker)) newhead)
8207 (org-with-remote-undo (marker-buffer org-clock-marker)
8208 (with-current-buffer (marker-buffer org-clock-marker)
8209 (save-excursion
8210 (save-restriction
8211 (widen)
8212 (goto-char org-clock-marker)
8213 (org-back-to-heading t)
8214 (move-marker marker (point))
8215 (org-clock-out)
8216 (setq newhead (org-get-heading))))))
8217 (org-agenda-change-all-lines newhead marker)
8218 (move-marker marker nil)))
8220 (defun org-agenda-clock-cancel (&optional arg)
8221 "Cancel the currently running clock."
8222 (interactive "P")
8223 (unless (marker-buffer org-clock-marker)
8224 (error "No running clock"))
8225 (org-with-remote-undo (marker-buffer org-clock-marker)
8226 (org-clock-cancel)))
8228 (defun org-agenda-clock-goto ()
8229 "Jump to the currently clocked in task within the agenda.
8230 If the currently clocked in task is not listed in the agenda
8231 buffer, display it in another window."
8232 (interactive)
8233 (let (pos)
8234 (mapc (lambda (o)
8235 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
8236 (setq pos (overlay-start o))))
8237 (overlays-in (point-min) (point-max)))
8238 (cond (pos (goto-char pos))
8239 ;; If the currently clocked entry is not in the agenda
8240 ;; buffer, we visit it in another window:
8241 (org-clock-current-task
8242 (org-switch-to-buffer-other-window (org-clock-goto)))
8243 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
8245 (defun org-agenda-diary-entry-in-org-file ()
8246 "Make a diary entry in the file `org-agenda-diary-file'."
8247 (let (d1 d2 char (text "") dp1 dp2)
8248 (if (equal (buffer-name) "*Calendar*")
8249 (setq d1 (calendar-cursor-to-date t)
8250 d2 (car calendar-mark-ring))
8251 (setq dp1 (get-text-property (point-at-bol) 'day))
8252 (unless dp1 (error "No date defined in current line"))
8253 (setq d1 (calendar-gregorian-from-absolute dp1)
8254 d2 (and (ignore-errors (mark))
8255 (save-excursion
8256 (goto-char (mark))
8257 (setq dp2 (get-text-property (point-at-bol) 'day)))
8258 (calendar-gregorian-from-absolute dp2))))
8259 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
8260 (setq char (read-char-exclusive))
8261 (cond
8262 ((equal char ?d)
8263 (setq text (read-string "Day entry: "))
8264 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
8265 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8266 ((equal char ?a)
8267 (setq d1 (list (car d1) (nth 1 d1)
8268 (read-number (format "Reference year [%d]: " (nth 2 d1))
8269 (nth 2 d1))))
8270 (setq text (read-string "Anniversary (use %d to show years): "))
8271 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
8272 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8273 ((equal char ?b)
8274 (setq text (read-string "Block entry: "))
8275 (unless (and d1 d2 (not (equal d1 d2)))
8276 (error "No block of days selected"))
8277 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
8278 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8279 ((equal char ?j)
8280 (org-switch-to-buffer-other-window
8281 (find-file-noselect org-agenda-diary-file))
8282 (require 'org-datetree)
8283 (org-datetree-find-date-create d1)
8284 (org-reveal t))
8285 (t (error "Invalid selection character `%c'" char)))))
8287 (defcustom org-agenda-insert-diary-strategy 'date-tree
8288 "Where in `org-agenda-diary-file' should new entries be added?
8289 Valid values:
8291 date-tree in the date tree, as child of the date
8292 top-level as top-level entries at the end of the file."
8293 :group 'org-agenda
8294 :type '(choice
8295 (const :tag "in a date tree" date-tree)
8296 (const :tag "as top level at end of file" top-level)))
8298 (defcustom org-agenda-insert-diary-extract-time nil
8299 "Non-nil means extract any time specification from the diary entry."
8300 :group 'org-agenda
8301 :version "24.1"
8302 :type 'boolean)
8304 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8305 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8306 If TEXT is not empty, it will become the headline of the new entry, and
8307 the resulting entry will not be shown. When TEXT is empty, switch to
8308 `org-agenda-diary-file' and let the user finish the entry there."
8309 (let ((cw (current-window-configuration)))
8310 (org-switch-to-buffer-other-window
8311 (find-file-noselect org-agenda-diary-file))
8312 (widen)
8313 (goto-char (point-min))
8314 (cond
8315 ((eq type 'anniversary)
8316 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8317 (progn
8318 (or (org-at-heading-p t)
8319 (progn
8320 (outline-next-heading)
8321 (insert "* Anniversaries\n\n")
8322 (beginning-of-line -1)))))
8323 (outline-next-heading)
8324 (org-back-over-empty-lines)
8325 (backward-char 1)
8326 (insert "\n")
8327 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8328 (nth 2 d1) (car d1) (nth 1 d1) text)))
8329 ((eq type 'day)
8330 (let ((org-prefix-has-time t)
8331 (org-agenda-time-leading-zero t)
8332 fmt time time2)
8333 (if org-agenda-insert-diary-extract-time
8334 ;; Use org-agenda-format-item to parse text for a time-range and
8335 ;; remove it. FIXME: This is a hack, we should refactor
8336 ;; that function to make time extraction available separately
8337 (setq fmt (org-agenda-format-item nil text nil nil t)
8338 time (get-text-property 0 'time fmt)
8339 time2 (if (> (length time) 0)
8340 ;; split-string removes trailing ...... if
8341 ;; no end time given. First space
8342 ;; separates time from date.
8343 (concat " " (car (split-string time "\\.")))
8344 nil)
8345 text (get-text-property 0 'txt fmt)))
8346 (if (eq org-agenda-insert-diary-strategy 'top-level)
8347 (org-agenda-insert-diary-as-top-level text)
8348 (require 'org-datetree)
8349 (org-datetree-find-date-create d1)
8350 (org-agenda-insert-diary-make-new-entry text))
8351 (org-insert-time-stamp (org-time-from-absolute
8352 (calendar-absolute-from-gregorian d1))
8353 nil nil nil nil time2))
8354 (end-of-line 0))
8355 ((eq type 'block)
8356 (if (> (calendar-absolute-from-gregorian d1)
8357 (calendar-absolute-from-gregorian d2))
8358 (setq d1 (prog1 d2 (setq d2 d1))))
8359 (if (eq org-agenda-insert-diary-strategy 'top-level)
8360 (org-agenda-insert-diary-as-top-level text)
8361 (require 'org-datetree)
8362 (org-datetree-find-date-create d1)
8363 (org-agenda-insert-diary-make-new-entry text))
8364 (org-insert-time-stamp (org-time-from-absolute
8365 (calendar-absolute-from-gregorian d1)))
8366 (insert "--")
8367 (org-insert-time-stamp (org-time-from-absolute
8368 (calendar-absolute-from-gregorian d2)))
8369 (end-of-line 0)))
8370 (if (string-match "\\S-" text)
8371 (progn
8372 (set-window-configuration cw)
8373 (message "%s entry added to %s"
8374 (capitalize (symbol-name type))
8375 (abbreviate-file-name org-agenda-diary-file)))
8376 (org-reveal t)
8377 (message "Please finish entry here"))))
8379 (defun org-agenda-insert-diary-as-top-level (text)
8380 "Make new entry as a top-level entry at the end of the file.
8381 Add TEXT as headline, and position the cursor in the second line so that
8382 a timestamp can be added there."
8383 (widen)
8384 (goto-char (point-max))
8385 (or (bolp) (insert "\n"))
8386 (insert "* " text "\n")
8387 (if org-adapt-indentation (org-indent-to-column 2)))
8389 (defun org-agenda-insert-diary-make-new-entry (text)
8390 "Make new entry as last child of current entry.
8391 Add TEXT as headline, and position the cursor in the second line so that
8392 a timestamp can be added there."
8393 (let ((org-show-following-heading t)
8394 (org-show-siblings t)
8395 (org-show-hierarchy-above t)
8396 (org-show-entry-below t)
8397 col)
8398 (outline-next-heading)
8399 (org-back-over-empty-lines)
8400 (or (looking-at "[ \t]*$")
8401 (progn (insert "\n") (backward-char 1)))
8402 (org-insert-heading nil t)
8403 (org-do-demote)
8404 (setq col (current-column))
8405 (insert text "\n")
8406 (if org-adapt-indentation (org-indent-to-column col))
8407 (let ((org-show-following-heading t)
8408 (org-show-siblings t)
8409 (org-show-hierarchy-above t)
8410 (org-show-entry-below t))
8411 (org-show-context))))
8413 (defun org-agenda-diary-entry ()
8414 "Make a diary entry, like the `i' command from the calendar.
8415 All the standard commands work: block, weekly etc.
8416 When `org-agenda-diary-file' points to a file,
8417 `org-agenda-diary-entry-in-org-file' is called instead to create
8418 entries in that Org-mode file."
8419 (interactive)
8420 (org-agenda-check-type t 'agenda 'timeline)
8421 (if (not (eq org-agenda-diary-file 'diary-file))
8422 (org-agenda-diary-entry-in-org-file)
8423 (require 'diary-lib)
8424 (let* ((char (progn
8425 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8426 (read-char-exclusive)))
8427 (cmd (cdr (assoc char
8428 '((?d . insert-diary-entry)
8429 (?w . insert-weekly-diary-entry)
8430 (?m . insert-monthly-diary-entry)
8431 (?y . insert-yearly-diary-entry)
8432 (?a . insert-anniversary-diary-entry)
8433 (?b . insert-block-diary-entry)
8434 (?c . insert-cyclic-diary-entry)))))
8435 (oldf (symbol-function 'calendar-cursor-to-date))
8436 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8437 (point (point))
8438 (mark (or (mark t) (point))))
8439 (unless cmd
8440 (error "No command associated with <%c>" char))
8441 (unless (and (get-text-property point 'day)
8442 (or (not (equal ?b char))
8443 (get-text-property mark 'day)))
8444 (error "Don't know which date to use for diary entry"))
8445 ;; We implement this by hacking the `calendar-cursor-to-date' function
8446 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8447 (let ((calendar-mark-ring
8448 (list (calendar-gregorian-from-absolute
8449 (or (get-text-property mark 'day)
8450 (get-text-property point 'day))))))
8451 (unwind-protect
8452 (progn
8453 (fset 'calendar-cursor-to-date
8454 (lambda (&optional error dummy)
8455 (calendar-gregorian-from-absolute
8456 (get-text-property point 'day))))
8457 (call-interactively cmd))
8458 (fset 'calendar-cursor-to-date oldf))))))
8460 (defun org-agenda-execute-calendar-command (cmd)
8461 "Execute a calendar command from the agenda, with the date associated to
8462 the cursor position."
8463 (org-agenda-check-type t 'agenda 'timeline)
8464 (require 'diary-lib)
8465 (unless (get-text-property (point) 'day)
8466 (error "Don't know which date to use for calendar command"))
8467 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8468 (point (point))
8469 (date (calendar-gregorian-from-absolute
8470 (get-text-property point 'day)))
8471 ;; the following 2 vars are needed in the calendar
8472 (displayed-month (car date))
8473 (displayed-year (nth 2 date)))
8474 (unwind-protect
8475 (progn
8476 (fset 'calendar-cursor-to-date
8477 (lambda (&optional error dummy)
8478 (calendar-gregorian-from-absolute
8479 (get-text-property point 'day))))
8480 (call-interactively cmd))
8481 (fset 'calendar-cursor-to-date oldf))))
8483 (defun org-agenda-phases-of-moon ()
8484 "Display the phases of the moon for the 3 months around the cursor date."
8485 (interactive)
8486 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8488 (defun org-agenda-holidays ()
8489 "Display the holidays for the 3 months around the cursor date."
8490 (interactive)
8491 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8493 (defvar calendar-longitude)
8494 (defvar calendar-latitude)
8495 (defvar calendar-location-name)
8497 (defun org-agenda-sunrise-sunset (arg)
8498 "Display sunrise and sunset for the cursor date.
8499 Latitude and longitude can be specified with the variables
8500 `calendar-latitude' and `calendar-longitude'. When called with prefix
8501 argument, latitude and longitude will be prompted for."
8502 (interactive "P")
8503 (require 'solar)
8504 (let ((calendar-longitude (if arg nil calendar-longitude))
8505 (calendar-latitude (if arg nil calendar-latitude))
8506 (calendar-location-name
8507 (if arg "the given coordinates" calendar-location-name)))
8508 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8510 (defun org-agenda-goto-calendar ()
8511 "Open the Emacs calendar with the date at the cursor."
8512 (interactive)
8513 (org-agenda-check-type t 'agenda 'timeline)
8514 (let* ((day (or (get-text-property (point) 'day)
8515 (error "Don't know which date to open in calendar")))
8516 (date (calendar-gregorian-from-absolute day))
8517 (calendar-move-hook nil)
8518 (calendar-view-holidays-initially-flag nil)
8519 (calendar-view-diary-initially-flag nil))
8520 (calendar)
8521 (calendar-goto-date date)))
8523 ;;;###autoload
8524 (defun org-calendar-goto-agenda ()
8525 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8526 This is a command that has to be installed in `calendar-mode-map'."
8527 (interactive)
8528 (org-agenda-list nil (calendar-absolute-from-gregorian
8529 (calendar-cursor-to-date))
8530 nil))
8532 (defun org-agenda-convert-date ()
8533 (interactive)
8534 (org-agenda-check-type t 'agenda 'timeline)
8535 (let ((day (get-text-property (point) 'day))
8536 date s)
8537 (unless day
8538 (error "Don't know which date to convert"))
8539 (setq date (calendar-gregorian-from-absolute day))
8540 (setq s (concat
8541 "Gregorian: " (calendar-date-string date) "\n"
8542 "ISO: " (calendar-iso-date-string date) "\n"
8543 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8544 "Julian: " (calendar-julian-date-string date) "\n"
8545 "Astron. JD: " (calendar-astro-date-string date)
8546 " (Julian date number at noon UTC)\n"
8547 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8548 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8549 "French: " (calendar-french-date-string date) "\n"
8550 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8551 "Mayan: " (calendar-mayan-date-string date) "\n"
8552 "Coptic: " (calendar-coptic-date-string date) "\n"
8553 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8554 "Persian: " (calendar-persian-date-string date) "\n"
8555 "Chinese: " (calendar-chinese-date-string date) "\n"))
8556 (with-output-to-temp-buffer "*Dates*"
8557 (princ s))
8558 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8560 ;;; Bulk commands
8562 (defvar org-agenda-bulk-marked-entries nil
8563 "List of markers that refer to marked entries in the agenda.")
8565 (defun org-agenda-bulk-marked-p ()
8566 (eq (get-char-property (point-at-bol) 'type)
8567 'org-marked-entry-overlay))
8569 (defun org-agenda-bulk-mark (&optional arg)
8570 "Mark the entry at point for future bulk action."
8571 (interactive "p")
8572 (dotimes (i (or arg 1))
8573 (unless (org-get-at-bol 'org-agenda-diary-link)
8574 (let* ((m (org-get-at-bol 'org-hd-marker))
8576 (unless (org-agenda-bulk-marked-p)
8577 (unless m (error "Nothing to mark at point"))
8578 (push m org-agenda-bulk-marked-entries)
8579 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8580 (org-overlay-display ov "> "
8581 (org-get-todo-face "TODO")
8582 'evaporate)
8583 (overlay-put ov 'type 'org-marked-entry-overlay))
8584 (beginning-of-line 2)
8585 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8586 (beginning-of-line 2))
8587 (message "%d entries marked for bulk action"
8588 (length org-agenda-bulk-marked-entries))))))
8590 (defun org-agenda-bulk-mark-regexp (regexp)
8591 "Mark entries match REGEXP."
8592 (interactive "sMark entries matching regexp: ")
8593 (let (entries-marked)
8594 (save-excursion
8595 (goto-char (point-min))
8596 (goto-char (next-single-property-change (point) 'txt))
8597 (while (re-search-forward regexp nil t)
8598 (when (string-match regexp (get-text-property (point) 'txt))
8599 (setq entries-marked (+ entries-marked 1))
8600 (call-interactively 'org-agenda-bulk-mark))))
8601 (if (not entries-marked)
8602 (message "No entry matching this regexp."))))
8604 (defun org-agenda-bulk-unmark ()
8605 "Unmark the entry at point for future bulk action."
8606 (interactive)
8607 (when (org-agenda-bulk-marked-p)
8608 (org-agenda-bulk-remove-overlays
8609 (point-at-bol) (+ 2 (point-at-bol)))
8610 (setq org-agenda-bulk-marked-entries
8611 (delete (org-get-at-bol 'org-hd-marker)
8612 org-agenda-bulk-marked-entries)))
8613 (beginning-of-line 2)
8614 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8615 (beginning-of-line 2))
8616 (message "%d entries marked for bulk action"
8617 (length org-agenda-bulk-marked-entries)))
8619 (defun org-agenda-bulk-toggle ()
8620 "Toggle marking the entry at point for bulk action."
8621 (interactive)
8622 (if (org-agenda-bulk-marked-p)
8623 (org-agenda-bulk-unmark)
8624 (org-agenda-bulk-mark)))
8626 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8627 "Remove the mark overlays between BEG and END in the agenda buffer.
8628 BEG and END default to the buffer limits.
8630 This only removes the overlays, it does not remove the markers
8631 from the list in `org-agenda-bulk-marked-entries'."
8632 (interactive)
8633 (mapc (lambda (ov)
8634 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8635 (delete-overlay ov)))
8636 (overlays-in (or beg (point-min)) (or end (point-max)))))
8638 (defun org-agenda-bulk-remove-all-marks ()
8639 "Remove all marks in the agenda buffer.
8640 This will remove the markers, and the overlays."
8641 (interactive)
8642 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8643 (setq org-agenda-bulk-marked-entries nil)
8644 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8646 (defun org-agenda-bulk-action (&optional arg)
8647 "Execute an remote-editing action on all marked entries.
8648 The prefix arg is passed through to the command if possible."
8649 (interactive "P")
8650 ;; Make sure we have markers, and only valid ones
8651 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8652 (mapc
8653 (lambda (m)
8654 (unless (and (markerp m)
8655 (marker-buffer m)
8656 (buffer-live-p (marker-buffer m))
8657 (marker-position m))
8658 (error "Marker %s for bulk command is invalid" m)))
8659 org-agenda-bulk-marked-entries)
8661 ;; Prompt for the bulk command
8662 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8663 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8664 (when org-agenda-bulk-custom-functions
8665 (concat " Custom: ["
8666 (mapconcat (lambda(f) (char-to-string (car f)))
8667 org-agenda-bulk-custom-functions "")
8668 "]"))))
8669 (let* ((action (read-char-exclusive))
8670 (org-log-refile (if org-log-refile 'time nil))
8671 (entries (reverse org-agenda-bulk-marked-entries))
8672 redo-at-end
8673 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8674 (cond
8675 ((equal action ?$)
8676 (setq cmd '(org-agenda-archive)))
8678 ((equal action ?A)
8679 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8681 ((member action '(?r ?w))
8682 (setq rfloc (org-refile-get-location
8683 "Refile to"
8684 (marker-buffer (car org-agenda-bulk-marked-entries))
8685 org-refile-allow-creating-parent-nodes))
8686 (if (nth 3 rfloc)
8687 (setcar (nthcdr 3 rfloc)
8688 (move-marker (make-marker) (nth 3 rfloc)
8689 (or (get-file-buffer (nth 1 rfloc))
8690 (find-buffer-visiting (nth 1 rfloc))
8691 (error "This should not happen")))))
8693 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8694 redo-at-end t))
8696 ((equal action ?t)
8697 (setq state (org-icompleting-read
8698 "Todo state: "
8699 (with-current-buffer (marker-buffer (car entries))
8700 (mapcar 'list org-todo-keywords-1))))
8701 (setq cmd `(let ((org-inhibit-blocking t)
8702 (org-inhibit-logging 'note))
8703 (org-agenda-todo ,state))))
8705 ((memq action '(?- ?+))
8706 (setq tag (org-icompleting-read
8707 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8708 (with-current-buffer (marker-buffer (car entries))
8709 (delq nil
8710 (mapcar (lambda (x)
8711 (if (stringp (car x)) x)) org-tag-alist)))))
8712 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8714 ((memq action '(?s ?d))
8715 (let* ((date (unless arg
8716 (org-read-date
8717 nil nil nil
8718 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8719 (ans (if arg nil org-read-date-final-answer))
8720 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8721 (setq cmd `(let* ((bound (fboundp 'read-string))
8722 (old (and bound (symbol-function 'read-string))))
8723 (unwind-protect
8724 (progn
8725 (fset 'read-string (lambda (&rest ignore) ,ans))
8726 (eval '(,c1 arg)))
8727 (if bound
8728 (fset 'read-string old)
8729 (fmakunbound 'read-string)))))))
8731 ((equal action ?S)
8732 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8733 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8734 (let ((days (read-number
8735 (format "Scatter tasks across how many %sdays: "
8736 (if arg "week" "")) 7)))
8737 (setq cmd
8738 `(let ((distance (1+ (random ,days))))
8739 (if arg
8740 (let ((dist distance)
8741 (day-of-week
8742 (calendar-day-of-week
8743 (calendar-gregorian-from-absolute (org-today)))))
8744 (dotimes (i (1+ dist))
8745 (while (member day-of-week org-agenda-weekend-days)
8746 (incf distance)
8747 (incf day-of-week)
8748 (if (= day-of-week 7)
8749 (setq day-of-week 0)))
8750 (incf day-of-week)
8751 (if (= day-of-week 7)
8752 (setq day-of-week 0)))))
8753 ;; silently fail when try to replan a sexp entry
8754 (condition-case nil
8755 (let* ((date (calendar-gregorian-from-absolute
8756 (+ (org-today) distance)))
8757 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8758 (nth 2 date))))
8759 (org-agenda-schedule nil time))
8760 (error nil)))))))
8762 ((assoc action org-agenda-bulk-custom-functions)
8763 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8764 redo-at-end t))
8766 ((equal action ?f)
8767 (setq cmd (list (intern
8768 (org-icompleting-read "Function: "
8769 obarray 'fboundp t nil nil)))))
8771 (t (error "Invalid bulk action")))
8773 ;; Sort the markers, to make sure that parents are handled before children
8774 (setq entries (sort entries
8775 (lambda (a b)
8776 (cond
8777 ((equal (marker-buffer a) (marker-buffer b))
8778 (< (marker-position a) (marker-position b)))
8780 (string< (buffer-name (marker-buffer a))
8781 (buffer-name (marker-buffer b))))))))
8783 ;; Now loop over all markers and apply cmd
8784 (while (setq e (pop entries))
8785 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8786 (if (not pos)
8787 (progn (message "Skipping removed entry at %s" e)
8788 (setq cntskip (1+ cntskip)))
8789 (goto-char pos)
8790 (let (org-loop-over-headlines-in-active-region)
8791 (eval cmd))
8792 (setq org-agenda-bulk-marked-entries
8793 (delete e org-agenda-bulk-marked-entries))
8794 (setq cnt (1+ cnt))))
8795 (setq org-agenda-bulk-marked-entries nil)
8796 (org-agenda-bulk-remove-all-marks)
8797 (when redo-at-end (org-agenda-redo))
8798 (message "Acted on %d entries%s"
8800 (if (= cntskip 0)
8802 (format ", skipped %d (disappeared before their turn)"
8803 cntskip)))))
8805 ;;; Flagging notes
8807 (defun org-agenda-show-the-flagging-note ()
8808 "Display the flagging note in the other window.
8809 When called a second time in direct sequence, offer to remove the FLAGGING
8810 tag and (if present) the flagging note."
8811 (interactive)
8812 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8813 (win (selected-window))
8814 note heading newhead)
8815 (unless hdmarker
8816 (error "No linked entry at point"))
8817 (if (and (eq this-command last-command)
8818 (y-or-n-p "Unflag and remove any flagging note? "))
8819 (progn
8820 (org-agenda-remove-flag hdmarker)
8821 (let ((win (get-buffer-window "*Flagging Note*")))
8822 (and win (delete-window win)))
8823 (message "Entry unflagged"))
8824 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8825 (unless note
8826 (error "No flagging note"))
8827 (org-kill-new note)
8828 (org-switch-to-buffer-other-window "*Flagging Note*")
8829 (erase-buffer)
8830 (insert note)
8831 (goto-char (point-min))
8832 (while (re-search-forward "\\\\n" nil t)
8833 (replace-match "\n" t t))
8834 (goto-char (point-min))
8835 (select-window win)
8836 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8838 (defun org-agenda-remove-flag (marker)
8839 "Remove the FLAGGED tag and any flagging note in the entry."
8840 (let (newhead)
8841 (org-with-point-at marker
8842 (org-toggle-tag "FLAGGED" 'off)
8843 (org-entry-delete nil "THEFLAGGINGNOTE")
8844 (setq newhead (org-get-heading)))
8845 (org-agenda-change-all-lines newhead marker)
8846 (message "Entry unflagged")))
8848 (defun org-agenda-get-any-marker (&optional pos)
8849 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8850 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8852 ;;; Appointment reminders
8854 (defvar appt-time-msg-list)
8856 ;;;###autoload
8857 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8858 "Activate appointments found in `org-agenda-files'.
8859 With a \\[universal-argument] prefix, refresh the list of
8860 appointments.
8862 If FILTER is t, interactively prompt the user for a regular
8863 expression, and filter out entries that don't match it.
8865 If FILTER is a string, use this string as a regular expression
8866 for filtering entries out.
8868 If FILTER is a function, filter out entries against which
8869 calling the function returns nil. This function takes one
8870 argument: an entry from `org-agenda-get-day-entries'.
8872 FILTER can also be an alist with the car of each cell being
8873 either 'headline or 'category. For example:
8875 '((headline \"IMPORTANT\")
8876 (category \"Work\"))
8878 will only add headlines containing IMPORTANT or headlines
8879 belonging to the \"Work\" category.
8881 ARGS are symbols indicating what kind of entries to consider.
8882 By default `org-agenda-to-appt' will use :deadline, :scheduled
8883 and :timestamp entries. See the docstring of `org-diary' for
8884 details and examples."
8885 (interactive "P")
8886 (if refresh (setq appt-time-msg-list nil))
8887 (if (eq filter t)
8888 (setq filter (read-from-minibuffer "Regexp filter: ")))
8889 (let* ((cnt 0) ; count added events
8890 (scope (or args '(:deadline :scheduled :timestamp)))
8891 (org-agenda-new-buffers nil)
8892 (org-deadline-warning-days 0)
8893 ;; Do not use `org-today' here because appt only takes
8894 ;; time and without date as argument, so it may pass wrong
8895 ;; information otherwise
8896 (today (org-date-to-gregorian
8897 (time-to-days (current-time))))
8898 (org-agenda-restrict nil)
8899 (files (org-agenda-files 'unrestricted)) entries file
8900 (org-agenda-buffer nil))
8901 ;; Get all entries which may contain an appt
8902 (org-prepare-agenda-buffers files)
8903 (while (setq file (pop files))
8904 (setq entries
8905 (delq nil
8906 (append entries
8907 (apply 'org-agenda-get-day-entries
8908 file today scope)))))
8909 ;; Map thru entries and find if we should filter them out
8910 (mapc
8911 (lambda(x)
8912 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8913 (cat (get-text-property 1 'org-category x))
8914 (tod (get-text-property 1 'time-of-day x))
8915 (ok (or (null filter)
8916 (and (stringp filter) (string-match filter evt))
8917 (and (functionp filter) (funcall filter x))
8918 (and (listp filter)
8919 (let ((cat-filter (cadr (assoc 'category filter)))
8920 (evt-filter (cadr (assoc 'headline filter))))
8921 (or (and (stringp cat-filter)
8922 (string-match cat-filter cat))
8923 (and (stringp evt-filter)
8924 (string-match evt-filter evt))))))))
8925 ;; FIXME: Shall we remove text-properties for the appt text?
8926 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8927 (when (and ok tod)
8928 (setq tod (concat "00" (number-to-string tod))
8929 tod (when (string-match
8930 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8931 (concat (match-string 1 tod) ":"
8932 (match-string 2 tod))))
8933 (appt-add tod evt)
8934 (setq cnt (1+ cnt))))) entries)
8935 (org-release-buffers org-agenda-new-buffers)
8936 (if (eq cnt 0)
8937 (message "No event to add")
8938 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8940 (defun org-agenda-todayp (date)
8941 "Does DATE mean today, when considering `org-extend-today-until'?"
8942 (let ((today (org-today))
8943 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8944 date)))
8945 (eq date today)))
8947 (defun org-agenda-todo-yesterday (&optional arg)
8948 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8949 (interactive "P")
8950 (let* ((hour (third (decode-time
8951 (org-current-time))))
8952 (org-extend-today-until (1+ hour)))
8953 (org-agenda-todo arg)))
8955 (provide 'org-agenda)
8957 ;;; org-agenda.el ends here