Merge origin/maint
[org-mode.git] / lisp / org-agenda.el
blob926f281cec1c6c48b4aa80a01c47776b6c8b42e8
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)
89 (defvar org-habit-show-all-today nil)
91 ;; Defined somewhere in this file, but used before definition.
92 (defvar org-agenda-buffer-name)
93 (defvar org-agenda-overriding-header)
94 (defvar org-agenda-title-append nil)
95 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
96 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
97 (defvar org-agenda-undo-list)
98 (defvar org-agenda-pending-undo-list)
99 (defvar original-date) ; dynamically scoped, calendar.el does scope this
101 (defcustom org-agenda-confirm-kill 1
102 "When set, remote killing from the agenda buffer needs confirmation.
103 When t, a confirmation is always needed. When a number N, confirmation is
104 only needed when the text to be killed contains more than N non-white lines."
105 :group 'org-agenda
106 :type '(choice
107 (const :tag "Never" nil)
108 (const :tag "Always" t)
109 (integer :tag "When more than N lines")))
111 (defcustom org-agenda-compact-blocks nil
112 "Non-nil means make the block agenda more compact.
113 This is done globally by leaving out lines like the agenda span
114 name and week number or the separator lines."
115 :group 'org-agenda
116 :type 'boolean)
118 (defcustom org-agenda-block-separator ?=
119 "The separator between blocks in the agenda.
120 If this is a string, it will be used as the separator, with a newline added.
121 If it is a character, it will be repeated to fill the window width.
122 If nil the separator is disabled. In `org-agenda-custom-commands' this
123 addresses the separator between the current and the previous block."
124 :group 'org-agenda
125 :type '(choice
126 (const :tag "Disabled" nil)
127 (character)
128 (string)))
130 (defgroup org-agenda-export nil
131 "Options concerning exporting agenda views in Org-mode."
132 :tag "Org Agenda Export"
133 :group 'org-agenda)
135 (defcustom org-agenda-with-colors t
136 "Non-nil means use colors in agenda views."
137 :group 'org-agenda-export
138 :type 'boolean)
140 (defcustom org-agenda-exporter-settings nil
141 "Alist of variable/value pairs that should be active during agenda export.
142 This is a good place to set options for ps-print and for htmlize.
143 Note that the way this is implemented, the values will be evaluated
144 before assigned to the variables. So make sure to quote values you do
145 *not* want evaluated, for example
147 (setq org-agenda-exporter-settings
148 '((ps-print-color-p 'black-white)))"
149 :group 'org-agenda-export
150 :type '(repeat
151 (list
152 (variable)
153 (sexp :tag "Value"))))
155 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
156 "Hook run in temporary buffer before writing it to an export file.
157 A useful function is `org-agenda-add-entry-text'."
158 :group 'org-agenda-export
159 :type 'hook
160 :options '(org-agenda-add-entry-text))
162 (defcustom org-agenda-add-entry-text-maxlines 0
163 "Maximum number of entry text lines to be added to agenda.
164 This is only relevant when `org-agenda-add-entry-text' is part of
165 `org-agenda-before-write-hook', which it is by default.
166 When this is 0, nothing will happen. When it is greater than 0, it
167 specifies the maximum number of lines that will be added for each entry
168 that is listed in the agenda view.
170 Note that this variable is not used during display, only when exporting
171 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
172 and `org-agenda-entry-text-maxlines'."
173 :group 'org-agenda
174 :type 'integer)
176 (defcustom org-agenda-add-entry-text-descriptive-links t
177 "Non-nil means export org-links as descriptive links in agenda added text.
178 This variable applies to the text added to the agenda when
179 `org-agenda-add-entry-text-maxlines' is larger than 0.
180 When this variable nil, the URL will (also) be shown."
181 :group 'org-agenda
182 :type 'boolean)
184 (defcustom org-agenda-export-html-style ""
185 "The style specification for exported HTML Agenda files.
186 If this variable contains a string, it will replace the default <style>
187 section as produced by `htmlize'.
188 Since there are different ways of setting style information, this variable
189 needs to contain the full HTML structure to provide a style, including the
190 surrounding HTML tags. The style specifications should include definitions
191 the fonts used by the agenda, here is an example:
193 <style type=\"text/css\">
194 p { font-weight: normal; color: gray; }
195 .org-agenda-structure {
196 font-size: 110%;
197 color: #003399;
198 font-weight: 600;
200 .org-todo {
201 color: #cc6666;
202 font-weight: bold;
204 .org-agenda-done {
205 color: #339933;
207 .org-done {
208 color: #339933;
210 .title { text-align: center; }
211 .todo, .deadline { color: red; }
212 .done { color: green; }
213 </style>
215 or, if you want to keep the style in a file,
217 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
219 As the value of this option simply gets inserted into the HTML <head> header,
220 you can \"misuse\" it to also add other text to the header. However,
221 <style>...</style> is required, if not present the variable will be ignored."
222 :group 'org-agenda-export
223 :group 'org-export-html
224 :type 'string)
226 (defcustom org-agenda-persistent-filter nil
227 "When set, keep filters from one agenda view to the next."
228 :group 'org-agenda
229 :type 'boolean)
231 (defgroup org-agenda-custom-commands nil
232 "Options concerning agenda views in Org-mode."
233 :tag "Org Agenda Custom Commands"
234 :group 'org-agenda)
236 (defconst org-sorting-choice
237 '(choice
238 (const time-up) (const time-down)
239 (const category-keep) (const category-up) (const category-down)
240 (const tag-down) (const tag-up)
241 (const priority-up) (const priority-down)
242 (const todo-state-up) (const todo-state-down)
243 (const effort-up) (const effort-down)
244 (const habit-up) (const habit-down)
245 (const alpha-up) (const alpha-down)
246 (const user-defined-up) (const user-defined-down))
247 "Sorting choices.")
249 ;; Keep custom values for `org-agenda-filter-preset' compatible with
250 ;; the new variable `org-agenda-tag-filter-preset'.
251 (if (fboundp 'defvaralias)
252 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
253 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter))
255 (defconst org-agenda-custom-commands-local-options
256 `(repeat :tag "Local settings for this command. Remember to quote values"
257 (choice :tag "Setting"
258 (list :tag "Heading for this block"
259 (const org-agenda-overriding-header)
260 (string :tag "Headline"))
261 (list :tag "Files to be searched"
262 (const org-agenda-files)
263 (list
264 (const :format "" quote)
265 (repeat (file))))
266 (list :tag "Sorting strategy"
267 (const org-agenda-sorting-strategy)
268 (list
269 (const :format "" quote)
270 (repeat
271 ,org-sorting-choice)))
272 (list :tag "Prefix format"
273 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
274 (string))
275 (list :tag "Number of days in agenda"
276 (const org-agenda-span)
277 (choice (const :tag "Day" 'day)
278 (const :tag "Week" 'week)
279 (const :tag "Month" 'month)
280 (const :tag "Year" 'year)
281 (integer :tag "Custom")))
282 (list :tag "Fixed starting date"
283 (const org-agenda-start-day)
284 (string :value "2007-11-01"))
285 (list :tag "Start on day of week"
286 (const org-agenda-start-on-weekday)
287 (choice :value 1
288 (const :tag "Today" nil)
289 (integer :tag "Weekday No.")))
290 (list :tag "Include data from diary"
291 (const org-agenda-include-diary)
292 (boolean))
293 (list :tag "Deadline Warning days"
294 (const org-deadline-warning-days)
295 (integer :value 1))
296 (list :tag "Category filter preset"
297 (const org-agenda-category-filter-preset)
298 (list
299 (const :format "" quote)
300 (repeat
301 (string :tag "+category or -category"))))
302 (list :tag "Tags filter preset"
303 (const org-agenda-tag-filter-preset)
304 (list
305 (const :format "" quote)
306 (repeat
307 (string :tag "+tag or -tag"))))
308 (list :tag "Set daily/weekly entry types"
309 (const org-agenda-entry-types)
310 (list
311 (const :format "" quote)
312 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
313 (const :deadline)
314 (const :scheduled)
315 (const :timestamp)
316 (const :sexp))))
317 (list :tag "Standard skipping condition"
318 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
319 (const org-agenda-skip-function)
320 (list
321 (const :format "" quote)
322 (list
323 (choice
324 :tag "Skipping range"
325 (const :tag "Skip entry" org-agenda-skip-entry-if)
326 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
327 (repeat :inline t :tag "Conditions for skipping"
328 (choice
329 :tag "Condition type"
330 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
331 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
332 (list :tag "TODO state is" :inline t
333 (const 'todo)
334 (choice
335 (const :tag "any not-done state" 'todo)
336 (const :tag "any done state" 'done)
337 (const :tag "any state" 'any)
338 (list :tag "Keyword list"
339 (const :format "" quote)
340 (repeat (string :tag "Keyword")))))
341 (list :tag "TODO state is not" :inline t
342 (const 'nottodo)
343 (choice
344 (const :tag "any not-done state" 'todo)
345 (const :tag "any done state" 'done)
346 (const :tag "any state" 'any)
347 (list :tag "Keyword list"
348 (const :format "" quote)
349 (repeat (string :tag "Keyword")))))
350 (const :tag "scheduled" 'scheduled)
351 (const :tag "not scheduled" 'notscheduled)
352 (const :tag "deadline" 'deadline)
353 (const :tag "no deadline" 'notdeadline)
354 (const :tag "timestamp" 'timestamp)
355 (const :tag "no timestamp" 'nottimestamp))))))
356 (list :tag "Non-standard skipping condition"
357 :value (org-agenda-skip-function)
358 (const org-agenda-skip-function)
359 (sexp :tag "Function or form (quoted!)"))
360 (list :tag "Any variable"
361 (variable :tag "Variable")
362 (sexp :tag "Value (sexp)"))))
363 "Selection of examples for agenda command settings.
364 This will be spliced into the custom type of
365 `org-agenda-custom-commands'.")
368 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
369 ((agenda "") (alltodo))))
370 "Custom commands for the agenda.
371 These commands will be offered on the splash screen displayed by the
372 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
374 (key desc type match settings files)
376 key The key (one or more characters as a string) to be associated
377 with the command.
378 desc A description of the command, when omitted or nil, a default
379 description is built using MATCH.
380 type The command type, any of the following symbols:
381 agenda The daily/weekly agenda.
382 todo Entries with a specific TODO keyword, in all agenda files.
383 search Entries containing search words entry or headline.
384 tags Tags/Property/TODO match in all agenda files.
385 tags-todo Tags/P/T match in all agenda files, TODO entries only.
386 todo-tree Sparse tree of specific TODO keyword in *current* file.
387 tags-tree Sparse tree with all tags matches in *current* file.
388 occur-tree Occur sparse tree for *current* file.
389 ... A user-defined function.
390 match What to search for:
391 - a single keyword for TODO keyword searches
392 - a tags match expression for tags searches
393 - a word search expression for text searches.
394 - a regular expression for occur searches
395 For all other commands, this should be the empty string.
396 settings A list of option settings, similar to that in a let form, so like
397 this: ((opt1 val1) (opt2 val2) ...). The values will be
398 evaluated at the moment of execution, so quote them when needed.
399 files A list of files file to write the produced agenda buffer to
400 with the command `org-store-agenda-views'.
401 If a file name ends in \".html\", an HTML version of the buffer
402 is written out. If it ends in \".ps\", a postscript version is
403 produced. Otherwise, only the plain text is written to the file.
405 You can also define a set of commands, to create a composite agenda buffer.
406 In this case, an entry looks like this:
408 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
410 where
412 desc A description string to be displayed in the dispatcher menu.
413 cmd An agenda command, similar to the above. However, tree commands
414 are not allowed, but instead you can get agenda and global todo list.
415 So valid commands for a set are:
416 (agenda \"\" settings)
417 (alltodo \"\" settings)
418 (stuck \"\" settings)
419 (todo \"match\" settings files)
420 (search \"match\" settings files)
421 (tags \"match\" settings files)
422 (tags-todo \"match\" settings files)
424 Each command can carry a list of options, and another set of options can be
425 given for the whole set of commands. Individual command options take
426 precedence over the general options.
428 When using several characters as key to a command, the first characters
429 are prefix commands. For the dispatcher to display useful information, you
430 should provide a description for the prefix, like
432 (setq org-agenda-custom-commands
433 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
434 (\"hl\" tags \"+HOME+Lisa\")
435 (\"hp\" tags \"+HOME+Peter\")
436 (\"hk\" tags \"+HOME+Kim\")))"
437 :group 'org-agenda-custom-commands
438 :type `(repeat
439 (choice :value ("x" "Describe command here" tags "" nil)
440 (list :tag "Single command"
441 (string :tag "Access Key(s) ")
442 (option (string :tag "Description"))
443 (choice
444 (const :tag "Agenda" agenda)
445 (const :tag "TODO list" alltodo)
446 (const :tag "Search words" search)
447 (const :tag "Stuck projects" stuck)
448 (const :tag "Tags/Property match (all agenda files)" tags)
449 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
450 (const :tag "TODO keyword search (all agenda files)" todo)
451 (const :tag "Tags sparse tree (current buffer)" tags-tree)
452 (const :tag "TODO keyword tree (current buffer)" todo-tree)
453 (const :tag "Occur tree (current buffer)" occur-tree)
454 (sexp :tag "Other, user-defined function"))
455 (string :tag "Match (only for some commands)")
456 ,org-agenda-custom-commands-local-options
457 (option (repeat :tag "Export" (file :tag "Export to"))))
458 (list :tag "Command series, all agenda files"
459 (string :tag "Access Key(s)")
460 (string :tag "Description ")
461 (repeat :tag "Component"
462 (choice
463 (list :tag "Agenda"
464 (const :format "" agenda)
465 (const :tag "" :format "" "")
466 ,org-agenda-custom-commands-local-options)
467 (list :tag "TODO list (all keywords)"
468 (const :format "" alltodo)
469 (const :tag "" :format "" "")
470 ,org-agenda-custom-commands-local-options)
471 (list :tag "Search words"
472 (const :format "" search)
473 (string :tag "Match")
474 ,org-agenda-custom-commands-local-options)
475 (list :tag "Stuck projects"
476 (const :format "" stuck)
477 (const :tag "" :format "" "")
478 ,org-agenda-custom-commands-local-options)
479 (list :tag "Tags search"
480 (const :format "" tags)
481 (string :tag "Match")
482 ,org-agenda-custom-commands-local-options)
483 (list :tag "Tags search, TODO entries only"
484 (const :format "" tags-todo)
485 (string :tag "Match")
486 ,org-agenda-custom-commands-local-options)
487 (list :tag "TODO keyword search"
488 (const :format "" todo)
489 (string :tag "Match")
490 ,org-agenda-custom-commands-local-options)
491 (list :tag "Other, user-defined function"
492 (symbol :tag "function")
493 (string :tag "Match")
494 ,org-agenda-custom-commands-local-options)))
496 (repeat :tag "Settings for entire command set"
497 (list (variable :tag "Any variable")
498 (sexp :tag "Value")))
499 (option (repeat :tag "Export" (file :tag "Export to"))))
500 (cons :tag "Prefix key documentation"
501 (string :tag "Access Key(s)")
502 (string :tag "Description ")))))
504 (defcustom org-agenda-query-register ?o
505 "The register holding the current query string.
506 The purpose of this is that if you construct a query string interactively,
507 you can then use it to define a custom command."
508 :group 'org-agenda-custom-commands
509 :type 'character)
511 (defcustom org-stuck-projects
512 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
513 "How to identify stuck projects.
514 This is a list of four items:
515 1. A tags/todo/property matcher string that is used to identify a project.
516 See the manual for a description of tag and property searches.
517 The entire tree below a headline matched by this is considered one project.
518 2. A list of TODO keywords identifying non-stuck projects.
519 If the project subtree contains any headline with one of these todo
520 keywords, the project is considered to be not stuck. If you specify
521 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
522 3. A list of tags identifying non-stuck projects.
523 If the project subtree contains any headline with one of these tags,
524 the project is considered to be not stuck. If you specify \"*\" as
525 a tag, any tag will mark the project unstuck. Note that this is about
526 the explicit presence of a tag somewhere in the subtree, inherited
527 tags to not count here. If inherited tags make a project not stuck,
528 use \"-TAG\" in the tags part of the matcher under (1.) above.
529 4. An arbitrary regular expression matching non-stuck projects.
531 If the project turns out to be not stuck, search continues also in the
532 subtree to see if any of the subtasks have project status.
534 See also the variable `org-tags-match-list-sublevels' which applies
535 to projects matched by this search as well.
537 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
538 or `C-c a #' to produce the list."
539 :group 'org-agenda-custom-commands
540 :type '(list
541 (string :tag "Tags/TODO match to identify a project")
542 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
543 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
544 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
546 (defcustom org-agenda-filter-effort-default-operator "<"
547 "The default operator for effort estimate filtering.
548 If you select an effort estimate limit without first pressing an operator,
549 this one will be used."
550 :group 'org-agenda-custom-commands
551 :type '(choice (const :tag "less or equal" "<")
552 (const :tag "greater or equal"">")
553 (const :tag "equal" "=")))
555 (defgroup org-agenda-skip nil
556 "Options concerning skipping parts of agenda files."
557 :tag "Org Agenda Skip"
558 :group 'org-agenda)
560 (defcustom org-agenda-skip-function-global nil
561 "Function to be called at each match during agenda construction.
562 If this function returns nil, the current match should not be skipped.
563 If the function decided to skip an agenda match, is must return the
564 buffer position from which the search should be continued.
565 This may also be a Lisp form, which will be evaluated.
567 This variable will be applied to every agenda match, including
568 tags/property searches and TODO lists. So try to make the test function
569 do its checking as efficiently as possible. To implement a skipping
570 condition just for specific agenda commands, use the variable
571 `org-agenda-skip-function' which can be set in the options section
572 of custom agenda commands."
573 :group 'org-agenda-skip
574 :type 'sexp)
576 (defgroup org-agenda-daily/weekly nil
577 "Options concerning the daily/weekly agenda."
578 :tag "Org Agenda Daily/Weekly"
579 :group 'org-agenda)
580 (defgroup org-agenda-todo-list nil
581 "Options concerning the global todo list agenda view."
582 :tag "Org Agenda Todo List"
583 :group 'org-agenda)
584 (defgroup org-agenda-match-view nil
585 "Options concerning the general tags/property/todo match agenda view."
586 :tag "Org Agenda Match View"
587 :group 'org-agenda)
588 (defgroup org-agenda-search-view nil
589 "Options concerning the general tags/property/todo match agenda view."
590 :tag "Org Agenda Match View"
591 :group 'org-agenda)
593 (defvar org-agenda-archives-mode nil
594 "Non-nil means the agenda will include archived items.
595 If this is the symbol `trees', trees in the selected agenda scope
596 that are marked with the ARCHIVE tag will be included anyway. When this is
597 t, also all archive files associated with the current selection of agenda
598 files will be included.")
600 (defcustom org-agenda-skip-comment-trees t
601 "Non-nil means skip trees that start with the COMMENT keyword.
602 When nil, these trees are also scanned by agenda commands."
603 :group 'org-agenda-skip
604 :type 'boolean)
606 (defcustom org-agenda-todo-list-sublevels t
607 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
608 When nil, the sublevels of a TODO entry are not checked, resulting in
609 potentially much shorter TODO lists."
610 :group 'org-agenda-skip
611 :group 'org-agenda-todo-list
612 :type 'boolean)
614 (defcustom org-agenda-todo-ignore-with-date nil
615 "Non-nil means don't show entries with a date in the global todo list.
616 You can use this if you prefer to mark mere appointments with a TODO keyword,
617 but don't want them to show up in the TODO list.
618 When this is set, it also covers deadlines and scheduled items, the settings
619 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
620 will be ignored.
621 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
622 :group 'org-agenda-skip
623 :group 'org-agenda-todo-list
624 :type 'boolean)
626 (defcustom org-agenda-todo-ignore-timestamp nil
627 "Non-nil means don't show entries with a timestamp.
628 This applies when creating the global todo list.
629 Valid values are:
631 past Don't show entries for today or in the past.
633 future Don't show entries with a timestamp in the future.
634 The idea behind this is that if it has a future
635 timestamp, you don't want to think about it until the
636 date.
638 all Don't show any entries with a timestamp in the global todo list.
639 The idea behind this is that by setting a timestamp, you
640 have already \"taken care\" of this item.
642 This variable can also have an integer as a value. If positive (N),
643 todos with a timestamp N or more days in the future will be ignored. If
644 negative (-N), todos with a timestamp N or more days in the past will be
645 ignored. If 0, todos with a timestamp either today or in the future will
646 be ignored. For example, a value of -1 will exclude todos with a
647 timestamp in the past (yesterday or earlier), while a value of 7 will
648 exclude todos with a timestamp a week or more in the future.
650 See also `org-agenda-todo-ignore-with-date'.
651 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
652 to make his option also apply to the tags-todo list."
653 :group 'org-agenda-skip
654 :group 'org-agenda-todo-list
655 :version "24.1"
656 :type '(choice
657 (const :tag "Ignore future timestamp todos" future)
658 (const :tag "Ignore past or present timestamp todos" past)
659 (const :tag "Ignore all timestamp todos" all)
660 (const :tag "Show timestamp todos" nil)
661 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
663 (defcustom org-agenda-todo-ignore-scheduled nil
664 "Non-nil means, ignore some scheduled TODO items when making TODO list.
665 This applies when creating the global todo list.
666 Valid values are:
668 past Don't show entries scheduled today or in the past.
670 future Don't show entries scheduled in the future.
671 The idea behind this is that by scheduling it, you don't want to
672 think about it until the scheduled date.
674 all Don't show any scheduled entries in the global todo list.
675 The idea behind this is that by scheduling it, you have already
676 \"taken care\" of this item.
678 t Same as `all', for backward compatibility.
680 This variable can also have an integer as a value. See
681 `org-agenda-todo-ignore-timestamp' for more details.
683 See also `org-agenda-todo-ignore-with-date'.
684 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
685 to make his option also apply to the tags-todo list."
686 :group 'org-agenda-skip
687 :group 'org-agenda-todo-list
688 :type '(choice
689 (const :tag "Ignore future-scheduled todos" future)
690 (const :tag "Ignore past- or present-scheduled todos" past)
691 (const :tag "Ignore all scheduled todos" all)
692 (const :tag "Ignore all scheduled todos (compatibility)" t)
693 (const :tag "Show scheduled todos" nil)
694 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
696 (defcustom org-agenda-todo-ignore-deadlines nil
697 "Non-nil means ignore some deadlined TODO items when making TODO list.
698 There are different motivations for using different values, please think
699 carefully when configuring this variable.
701 This applies when creating the global todo list.
702 Valid values are:
704 near Don't show near deadline entries. A deadline is near when it is
705 closer than `org-deadline-warning-days' days. The idea behind this
706 is that such items will appear in the agenda anyway.
708 far Don't show TODO entries where a deadline has been defined, but
709 the deadline is not near. This is useful if you don't want to
710 use the todo list to figure out what to do now.
712 past Don't show entries with a deadline timestamp for today or in the past.
714 future Don't show entries with a deadline timestamp in the future, not even
715 when they become `near' ones. Use it with caution.
717 all Ignore all TODO entries that do have a deadline.
719 t Same as `near', for backward compatibility.
721 This variable can also have an integer as a value. See
722 `org-agenda-todo-ignore-timestamp' for more details.
724 See also `org-agenda-todo-ignore-with-date'.
725 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
726 to make his option also apply to the tags-todo list."
727 :group 'org-agenda-skip
728 :group 'org-agenda-todo-list
729 :type '(choice
730 (const :tag "Ignore near deadlines" near)
731 (const :tag "Ignore near deadlines (compatibility)" t)
732 (const :tag "Ignore far deadlines" far)
733 (const :tag "Ignore all TODOs with a deadlines" all)
734 (const :tag "Show all TODOs, even if they have a deadline" nil)
735 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
737 (defcustom org-agenda-tags-todo-honor-ignore-options nil
738 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
739 The variables
740 `org-agenda-todo-ignore-with-date',
741 `org-agenda-todo-ignore-timestamp',
742 `org-agenda-todo-ignore-scheduled',
743 `org-agenda-todo-ignore-deadlines'
744 make the global TODO list skip entries that have time stamps of certain
745 kinds. If this option is set, the same options will also apply for the
746 tags-todo search, which is the general tags/property matcher
747 restricted to unfinished TODO entries only."
748 :group 'org-agenda-skip
749 :group 'org-agenda-todo-list
750 :group 'org-agenda-match-view
751 :type 'boolean)
753 (defcustom org-agenda-skip-scheduled-if-done nil
754 "Non-nil means don't show scheduled items in agenda when they are done.
755 This is relevant for the daily/weekly agenda, not for the TODO list. And
756 it applies only to the actual date of the scheduling. Warnings about
757 an item with a past scheduling dates are always turned off when the item
758 is DONE."
759 :group 'org-agenda-skip
760 :group 'org-agenda-daily/weekly
761 :type 'boolean)
763 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
764 "Non-nil means skip scheduling line if same entry shows because of deadline.
765 In the agenda of today, an entry can show up multiple times because
766 it is both scheduled and has a nearby deadline, and maybe a plain time
767 stamp as well.
768 When this variable is t, then only the deadline is shown and the fact that
769 the entry is scheduled today or was scheduled previously is not shown.
770 When this variable is nil, the entry will be shown several times. When
771 the variable is the symbol `not-today', then skip scheduled previously,
772 but not scheduled today."
773 :group 'org-agenda-skip
774 :group 'org-agenda-daily/weekly
775 :type '(choice
776 (const :tag "Never" nil)
777 (const :tag "Always" t)
778 (const :tag "Not when scheduled today" not-today)))
780 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
781 "Non-nil means skip timestamp line if same entry shows because of deadline.
782 In the agenda of today, an entry can show up multiple times
783 because it has both a plain timestamp and has a nearby deadline.
784 When this variable is t, then only the deadline is shown and the
785 fact that the entry has a timestamp for or including today is not
786 shown. When this variable is nil, the entry will be shown
787 several times."
788 :group 'org-agenda-skip
789 :group 'org-agenda-daily/weekly
790 :version "24.1"
791 :type '(choice
792 (const :tag "Never" nil)
793 (const :tag "Always" t)))
795 (defcustom org-agenda-skip-deadline-if-done nil
796 "Non-nil means don't show deadlines when the corresponding item is done.
797 When nil, the deadline is still shown and should give you a happy feeling.
798 This is relevant for the daily/weekly agenda. And it applied only to the
799 actually date of the deadline. Warnings about approaching and past-due
800 deadlines are always turned off when the item is DONE."
801 :group 'org-agenda-skip
802 :group 'org-agenda-daily/weekly
803 :type 'boolean)
805 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
806 "Non-nil means skip deadline prewarning when entry is also scheduled.
807 This will apply on all days where a prewarning for the deadline would
808 be shown, but not at the day when the entry is actually due. On that day,
809 the deadline will be shown anyway.
810 This variable may be set to nil, t, or a number which will then give
811 the number of days before the actual deadline when the prewarnings
812 should resume.
813 This can be used in a workflow where the first showing of the deadline will
814 trigger you to schedule it, and then you don't want to be reminded of it
815 because you will take care of it on the day when scheduled."
816 :group 'org-agenda-skip
817 :group 'org-agenda-daily/weekly
818 :version "24.1"
819 :type '(choice
820 (const :tag "Alwas show prewarning" nil)
821 (const :tag "Remove prewarning if entry is scheduled" t)
822 (integer :tag "Restart prewarning N days before deadline")))
824 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
825 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
826 When non-nil, after the search for timestamps has matched once in an
827 entry, the rest of the entry will not be searched."
828 :group 'org-agenda-skip
829 :type 'boolean)
831 (defcustom org-agenda-skip-timestamp-if-done nil
832 "Non-nil means don't select item by timestamp or -range if it is DONE."
833 :group 'org-agenda-skip
834 :group 'org-agenda-daily/weekly
835 :type 'boolean)
837 (defcustom org-agenda-dim-blocked-tasks t
838 "Non-nil means dim blocked tasks in the agenda display.
839 This causes some overhead during agenda construction, but if you
840 have turned on `org-enforce-todo-dependencies',
841 `org-enforce-todo-checkbox-dependencies', or any other blocking
842 mechanism, this will create useful feedback in the agenda.
844 Instead of t, this variable can also have the value `invisible'.
845 Then blocked tasks will be invisible and only become visible when
846 they become unblocked. An exemption to this behavior is when a task is
847 blocked because of unchecked checkboxes below it. Since checkboxes do
848 not show up in the agenda views, making this task invisible you remove any
849 trace from agenda views that there is something to do. Therefore, a task
850 that is blocked because of checkboxes will never be made invisible, it
851 will only be dimmed."
852 :group 'org-agenda-daily/weekly
853 :group 'org-agenda-todo-list
854 :type '(choice
855 (const :tag "Do not dim" nil)
856 (const :tag "Dim to a gray face" t)
857 (const :tag "Make invisible" invisible)))
859 (defcustom org-timeline-show-empty-dates 3
860 "Non-nil means `org-timeline' also shows dates without an entry.
861 When nil, only the days which actually have entries are shown.
862 When t, all days between the first and the last date are shown.
863 When an integer, show also empty dates, but if there is a gap of more than
864 N days, just insert a special line indicating the size of the gap."
865 :group 'org-agenda-skip
866 :type '(choice
867 (const :tag "None" nil)
868 (const :tag "All" t)
869 (integer :tag "at most")))
871 (defgroup org-agenda-startup nil
872 "Options concerning initial settings in the Agenda in Org Mode."
873 :tag "Org Agenda Startup"
874 :group 'org-agenda)
876 (defcustom org-agenda-menu-show-matcher t
877 "Non-nil means show the match string in the agenda dispatcher menu.
878 When nil, the matcher string is not shown, but is put into the help-echo
879 property so than moving the mouse over the command shows it.
880 Setting it to nil is good if matcher strings are very long and/or if
881 you want to use two-column display (see `org-agenda-menu-two-column')."
882 :group 'org-agenda
883 :version "24.1"
884 :type 'boolean)
886 (defcustom org-agenda-menu-two-column nil
887 "Non-nil means, use two columns to show custom commands in the dispatcher.
888 If you use this, you probably want to set `org-agenda-menu-show-matcher'
889 to nil."
890 :group 'org-agenda
891 :version "24.1"
892 :type 'boolean)
894 (defcustom org-finalize-agenda-hook nil
895 "Hook run just before displaying an agenda buffer."
896 :group 'org-agenda-startup
897 :type 'hook)
899 (defcustom org-agenda-mouse-1-follows-link nil
900 "Non-nil means mouse-1 on a link will follow the link in the agenda.
901 A longer mouse click will still set point. Does not work on XEmacs.
902 Needs to be set before org.el is loaded."
903 :group 'org-agenda-startup
904 :type 'boolean)
906 (defcustom org-agenda-start-with-follow-mode nil
907 "The initial value of follow mode in a newly created agenda window."
908 :group 'org-agenda-startup
909 :type 'boolean)
911 (defcustom org-agenda-follow-indirect nil
912 "Non-nil means `org-agenda-follow-mode' displays only the
913 current item's tree, in an indirect buffer."
914 :group 'org-agenda
915 :version "24.1"
916 :type 'boolean)
918 (defcustom org-agenda-show-outline-path t
919 "Non-nil means show outline path in echo area after line motion."
920 :group 'org-agenda-startup
921 :type 'boolean)
923 (defcustom org-agenda-start-with-entry-text-mode nil
924 "The initial value of entry-text-mode in a newly created agenda window."
925 :group 'org-agenda-startup
926 :type 'boolean)
928 (defcustom org-agenda-entry-text-maxlines 5
929 "Number of text lines to be added when `E' is pressed in the agenda.
931 Note that this variable only used during agenda display. Add add entry text
932 when exporting the agenda, configure the variable
933 `org-agenda-add-entry-ext-maxlines'."
934 :group 'org-agenda
935 :type 'integer)
937 (defcustom org-agenda-entry-text-exclude-regexps nil
938 "List of regular expressions to clean up entry text.
939 The complete matches of all regular expressions in this list will be
940 removed from entry text before it is shown in the agenda."
941 :group 'org-agenda
942 :type '(repeat (regexp)))
944 (defvar org-agenda-entry-text-cleanup-hook nil
945 "Hook that is run after basic cleanup of entry text to be shown in agenda.
946 This cleanup is done in a temporary buffer, so the function may inspect and
947 change the entire buffer.
948 Some default stuff like drawers and scheduling/deadline dates will already
949 have been removed when this is called, as will any matches for regular
950 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
952 (defvar org-agenda-include-inactive-timestamps nil
953 "Non-nil means include inactive time stamps in agenda and timeline.
954 Dynamically scoped.")
956 (defgroup org-agenda-windows nil
957 "Options concerning the windows used by the Agenda in Org Mode."
958 :tag "Org Agenda Windows"
959 :group 'org-agenda)
961 (defcustom org-agenda-window-setup 'reorganize-frame
962 "How the agenda buffer should be displayed.
963 Possible values for this option are:
965 current-window Show agenda in the current window, keeping all other windows.
966 other-window Use `switch-to-buffer-other-window' to display agenda.
967 reorganize-frame Show only two windows on the current frame, the current
968 window and the agenda.
969 other-frame Use `switch-to-buffer-other-frame' to display agenda.
970 Also, when exiting the agenda, kill that frame.
971 See also the variable `org-agenda-restore-windows-after-quit'."
972 :group 'org-agenda-windows
973 :type '(choice
974 (const current-window)
975 (const other-frame)
976 (const other-window)
977 (const reorganize-frame)))
979 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
980 "The min and max height of the agenda window as a fraction of frame height.
981 The value of the variable is a cons cell with two numbers between 0 and 1.
982 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
983 :group 'org-agenda-windows
984 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
986 (defcustom org-agenda-restore-windows-after-quit nil
987 "Non-nil means restore window configuration upon exiting agenda.
988 Before the window configuration is changed for displaying the agenda,
989 the current status is recorded. When the agenda is exited with
990 `q' or `x' and this option is set, the old state is restored. If
991 `org-agenda-window-setup' is `other-frame', the value of this
992 option will be ignored."
993 :group 'org-agenda-windows
994 :type 'boolean)
996 (defcustom org-agenda-ndays nil
997 "Number of days to include in overview display.
998 Should be 1 or 7.
999 Obsolete, see `org-agenda-span'."
1000 :group 'org-agenda-daily/weekly
1001 :type 'integer)
1003 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1005 (defcustom org-agenda-span 'week
1006 "Number of days to include in overview display.
1007 Can be day, week, month, year, or any number of days.
1008 Custom commands can set this variable in the options section."
1009 :group 'org-agenda-daily/weekly
1010 :type '(choice (const :tag "Day" day)
1011 (const :tag "Week" week)
1012 (const :tag "Month" month)
1013 (const :tag "Year" year)
1014 (integer :tag "Custom")))
1016 (defcustom org-agenda-start-on-weekday 1
1017 "Non-nil means start the overview always on the specified weekday.
1018 0 denotes Sunday, 1 denotes Monday etc.
1019 When nil, always start on the current day.
1020 Custom commands can set this variable in the options section."
1021 :group 'org-agenda-daily/weekly
1022 :type '(choice (const :tag "Today" nil)
1023 (integer :tag "Weekday No.")))
1025 (defcustom org-agenda-show-all-dates t
1026 "Non-nil means `org-agenda' shows every day in the selected range.
1027 When nil, only the days which actually have entries are shown."
1028 :group 'org-agenda-daily/weekly
1029 :type 'boolean)
1031 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1032 "Format string for displaying dates in the agenda.
1033 Used by the daily/weekly agenda and by the timeline. This should be
1034 a format string understood by `format-time-string', or a function returning
1035 the formatted date as a string. The function must take a single argument,
1036 a calendar-style date list like (month day year)."
1037 :group 'org-agenda-daily/weekly
1038 :type '(choice
1039 (string :tag "Format string")
1040 (function :tag "Function")))
1042 (defun org-agenda-format-date-aligned (date)
1043 "Format a date string for display in the daily/weekly agenda, or timeline.
1044 This function makes sure that dates are aligned for easy reading."
1045 (require 'cal-iso)
1046 (let* ((dayname (calendar-day-name date))
1047 (day (cadr date))
1048 (day-of-week (calendar-day-of-week date))
1049 (month (car date))
1050 (monthname (calendar-month-name month))
1051 (year (nth 2 date))
1052 (iso-week (org-days-to-iso-week
1053 (calendar-absolute-from-gregorian date)))
1054 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1055 (1- year))
1056 ((and (= month 12) (<= iso-week 1))
1057 (1+ year))
1058 (t year)))
1059 (weekstring (if (= day-of-week 1)
1060 (format " W%02d" iso-week)
1061 "")))
1062 (format "%-10s %2d %s %4d%s"
1063 dayname day monthname year weekstring)))
1065 (defcustom org-agenda-time-leading-zero nil
1066 "Non-nil means use leading zero for military times in agenda.
1067 For example, 9:30am would become 09:30 rather than 9:30."
1068 :group 'org-agenda-daily/weekly
1069 :version "24.1"
1070 :type 'boolean)
1072 (defcustom org-agenda-timegrid-use-ampm nil
1073 "When set, show AM/PM style timestamps on the timegrid."
1074 :group 'org-agenda
1075 :version "24.1"
1076 :type 'boolean)
1078 (defun org-agenda-time-of-day-to-ampm (time)
1079 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1080 (let* ((hour-number (string-to-number (substring time 0 -3)))
1081 (minute (substring time -2))
1082 (ampm "am"))
1083 (cond
1084 ((equal hour-number 12)
1085 (setq ampm "pm"))
1086 ((> hour-number 12)
1087 (setq ampm "pm")
1088 (setq hour-number (- hour-number 12))))
1089 (concat
1090 (if org-agenda-time-leading-zero
1091 (format "%02d" hour-number)
1092 (format "%02s" (number-to-string hour-number)))
1093 ":" minute ampm)))
1095 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1096 "Conditionally convert TIME to AM/PM format
1097 based on `org-agenda-timegrid-use-ampm'"
1098 (if org-agenda-timegrid-use-ampm
1099 (org-agenda-time-of-day-to-ampm time)
1100 time))
1102 (defcustom org-agenda-weekend-days '(6 0)
1103 "Which days are weekend?
1104 These days get the special face `org-agenda-date-weekend' in the agenda
1105 and timeline buffers."
1106 :group 'org-agenda-daily/weekly
1107 :type '(set :greedy t
1108 (const :tag "Monday" 1)
1109 (const :tag "Tuesday" 2)
1110 (const :tag "Wednesday" 3)
1111 (const :tag "Thursday" 4)
1112 (const :tag "Friday" 5)
1113 (const :tag "Saturday" 6)
1114 (const :tag "Sunday" 0)))
1116 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1117 "Non-nil means jump to today when moving a past date forward in time.
1118 When using S-right in the agenda to move a a date forward, and the date
1119 stamp currently points to the past, the first key press will move it
1120 to today. WHen nil, just move one day forward even if the date stays
1121 in the past."
1122 :group 'org-agenda-daily/weekly
1123 :version "24.1"
1124 :type 'boolean)
1126 (defcustom org-agenda-include-diary nil
1127 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1128 Custom commands can set this variable in the options section."
1129 :group 'org-agenda-daily/weekly
1130 :type 'boolean)
1132 (defcustom org-agenda-include-deadlines t
1133 "If non-nil, include entries within their deadline warning period.
1134 Custom commands can set this variable in the options section."
1135 :group 'org-agenda-daily/weekly
1136 :version "24.1"
1137 :type 'boolean)
1139 (defcustom org-agenda-repeating-timestamp-show-all t
1140 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1141 When set to a list of strings, only show occurrences of repeating
1142 stamps for these TODO keywords. When nil, only one occurrence is
1143 shown, either today or the nearest into the future."
1144 :group 'org-agenda-daily/weekly
1145 :type '(choice
1146 (const :tag "Show repeating stamps" t)
1147 (repeat :tag "Show repeating stamps for these TODO keywords"
1148 (string :tag "TODO Keyword"))
1149 (const :tag "Don't show repeating stamps" nil)))
1151 (defcustom org-scheduled-past-days 10000
1152 "No. of days to continue listing scheduled items that are not marked DONE.
1153 When an item is scheduled on a date, it shows up in the agenda on this
1154 day and will be listed until it is marked done for the number of days
1155 given here."
1156 :group 'org-agenda-daily/weekly
1157 :type 'integer)
1159 (defcustom org-agenda-log-mode-items '(closed clock)
1160 "List of items that should be shown in agenda log mode.
1161 This list may contain the following symbols:
1163 closed Show entries that have been closed on that day.
1164 clock Show entries that have received clocked time on that day.
1165 state Show all logged state changes.
1166 Note that instead of changing this variable, you can also press `C-u l' in
1167 the agenda to display all available LOG items temporarily."
1168 :group 'org-agenda-daily/weekly
1169 :type '(set :greedy t (const closed) (const clock) (const state)))
1171 (defcustom org-agenda-clock-consistency-checks
1172 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1173 :gap-ok-around ("4:00")
1174 :default-face ((:background "DarkRed") (:foreground "white"))
1175 :overlap-face nil :gap-face nil :no-end-time-face nil
1176 :long-face nil :short-face nil)
1177 "This is a property list, with the following keys:
1179 :max-duration Mark clocking chunks that are longer than this time.
1180 This is a time string like \"HH:MM\", or the number
1181 of minutes as an integer.
1183 :min-duration Mark clocking chunks that are shorter that this.
1184 This is a time string like \"HH:MM\", or the number
1185 of minutes as an integer.
1187 :max-gap Mark gaps between clocking chunks that are longer than
1188 this duration. A number of minutes, or a string
1189 like \"HH:MM\".
1191 :gap-ok-around List of times during the day which are usually not working
1192 times. When a gap is detected, but the gap contains any
1193 of these times, the gap is *not* reported. For example,
1194 if this is (\"4:00\" \"13:00\") then gaps that contain
1195 4:00 in the morning (i.e. the night) and 13:00
1196 (i.e. a typical lunch time) do not cause a warning.
1197 You should have at least one time during the night in this
1198 list, or otherwise the first task each morning will trigger
1199 a warning because it follows a long gap.
1201 Furthermore, the following properties can be used to define faces for
1202 issue display.
1204 :default-face the default face, if the specific face is undefined
1205 :overlap-face face for overlapping clocks
1206 :gap-face face for gaps between clocks
1207 :no-end-time-face face for incomplete clocks
1208 :long-face face for clock intervals that are too long
1209 :short-face face for clock intervals that are too short"
1210 :group 'org-agenda-daily/weekly
1211 :group 'org-clock
1212 :version "24.1"
1213 :type 'plist)
1215 (defcustom org-agenda-log-mode-add-notes t
1216 "Non-nil means add first line of notes to log entries in agenda views.
1217 If a log item like a state change or a clock entry is associated with
1218 notes, the first line of these notes will be added to the entry in the
1219 agenda display."
1220 :group 'org-agenda-daily/weekly
1221 :type 'boolean)
1223 (defcustom org-agenda-start-with-log-mode nil
1224 "The initial value of log-mode in a newly created agenda window."
1225 :group 'org-agenda-startup
1226 :group 'org-agenda-daily/weekly
1227 :type 'boolean)
1229 (defcustom org-agenda-start-with-clockreport-mode nil
1230 "The initial value of clockreport-mode in a newly created agenda window."
1231 :group 'org-agenda-startup
1232 :group 'org-agenda-daily/weekly
1233 :type 'boolean)
1235 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1236 "Property list with parameters for the clocktable in clockreport mode.
1237 This is the display mode that shows a clock table in the daily/weekly
1238 agenda, the properties for this dynamic block can be set here.
1239 The usual clocktable parameters are allowed here, but you cannot set
1240 the properties :name, :tstart, :tend, :block, and :scope - these will
1241 be overwritten to make sure the content accurately reflects the
1242 current display in the agenda."
1243 :group 'org-agenda-daily/weekly
1244 :type 'plist)
1246 (defcustom org-agenda-search-view-always-boolean nil
1247 "Non-nil means the search string is interpreted as individual parts.
1249 The search string for search view can either be interpreted as a phrase,
1250 or as a list of snippets that define a boolean search for a number of
1251 strings.
1253 When this is non-nil, the string will be split on whitespace, and each
1254 snippet will be searched individually, and all must match in order to
1255 select an entry. A snippet is then a single string of non-white
1256 characters, or a string in double quotes, or a regexp in {} braces.
1257 If a snippet is preceded by \"-\", the snippet must *not* match.
1258 \"+\" is syntactic sugar for positive selection. Each snippet may
1259 be found as a full word or a partial word, but see the variable
1260 `org-agenda-search-view-force-full-words'.
1262 When this is nil, search will look for the entire search phrase as one,
1263 with each space character matching any amount of whitespace, including
1264 line breaks.
1266 Even when this is nil, you can still switch to Boolean search dynamically
1267 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1268 is a regexp marked with braces like \"{abc}\", this will also switch to
1269 boolean search."
1270 :group 'org-agenda-search-view
1271 :version "24.1"
1272 :type 'boolean)
1274 (if (fboundp 'defvaralias)
1275 (defvaralias 'org-agenda-search-view-search-words-only
1276 'org-agenda-search-view-always-boolean))
1278 (defcustom org-agenda-search-view-force-full-words nil
1279 "Non-nil means, search words must be matches as complete words.
1280 When nil, they may also match part of a word."
1281 :group 'org-agenda-search-view
1282 :version "24.1"
1283 :type 'boolean)
1285 (defgroup org-agenda-time-grid nil
1286 "Options concerning the time grid in the Org-mode Agenda."
1287 :tag "Org Agenda Time Grid"
1288 :group 'org-agenda)
1290 (defcustom org-agenda-search-headline-for-time t
1291 "Non-nil means search headline for a time-of-day.
1292 If the headline contains a time-of-day in one format or another, it will
1293 be used to sort the entry into the time sequence of items for a day.
1294 Some people have time stamps in the headline that refer to the creation
1295 time or so, and then this produces an unwanted side effect. If this is
1296 the case for your, use this variable to turn off searching the headline
1297 for a time."
1298 :group 'org-agenda-time-grid
1299 :type 'boolean)
1301 (defcustom org-agenda-use-time-grid t
1302 "Non-nil means show a time grid in the agenda schedule.
1303 A time grid is a set of lines for specific times (like every two hours between
1304 8:00 and 20:00). The items scheduled for a day at specific times are
1305 sorted in between these lines.
1306 For details about when the grid will be shown, and what it will look like, see
1307 the variable `org-agenda-time-grid'."
1308 :group 'org-agenda-time-grid
1309 :type 'boolean)
1311 (defcustom org-agenda-time-grid
1312 '((daily today require-timed)
1313 "----------------"
1314 (800 1000 1200 1400 1600 1800 2000))
1316 "The settings for time grid for agenda display.
1317 This is a list of three items. The first item is again a list. It contains
1318 symbols specifying conditions when the grid should be displayed:
1320 daily if the agenda shows a single day
1321 weekly if the agenda shows an entire week
1322 today show grid on current date, independent of daily/weekly display
1323 require-timed show grid only if at least one item has a time specification
1325 The second item is a string which will be placed behind the grid time.
1327 The third item is a list of integers, indicating the times that should have
1328 a grid line."
1329 :group 'org-agenda-time-grid
1330 :type
1331 '(list
1332 (set :greedy t :tag "Grid Display Options"
1333 (const :tag "Show grid in single day agenda display" daily)
1334 (const :tag "Show grid in weekly agenda display" weekly)
1335 (const :tag "Always show grid for today" today)
1336 (const :tag "Show grid only if any timed entries are present"
1337 require-timed)
1338 (const :tag "Skip grid times already present in an entry"
1339 remove-match))
1340 (string :tag "Grid String")
1341 (repeat :tag "Grid Times" (integer :tag "Time"))))
1343 (defcustom org-agenda-show-current-time-in-grid t
1344 "Non-nil means show the current time in the time grid."
1345 :group 'org-agenda-time-grid
1346 :version "24.1"
1347 :type 'boolean)
1349 (defcustom org-agenda-current-time-string
1350 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1351 "The string for the current time marker in the agenda."
1352 :group 'org-agenda-time-grid
1353 :version "24.1"
1354 :type 'string)
1356 (defgroup org-agenda-sorting nil
1357 "Options concerning sorting in the Org-mode Agenda."
1358 :tag "Org Agenda Sorting"
1359 :group 'org-agenda)
1361 (defcustom org-agenda-sorting-strategy
1362 '((agenda habit-down time-up priority-down category-keep)
1363 (todo priority-down category-keep)
1364 (tags priority-down category-keep)
1365 (search category-keep))
1366 "Sorting structure for the agenda items of a single day.
1367 This is a list of symbols which will be used in sequence to determine
1368 if an entry should be listed before another entry. The following
1369 symbols are recognized:
1371 time-up Put entries with time-of-day indications first, early first
1372 time-down Put entries with time-of-day indications first, late first
1373 category-keep Keep the default order of categories, corresponding to the
1374 sequence in `org-agenda-files'.
1375 category-up Sort alphabetically by category, A-Z.
1376 category-down Sort alphabetically by category, Z-A.
1377 tag-up Sort alphabetically by last tag, A-Z.
1378 tag-down Sort alphabetically by last tag, Z-A.
1379 priority-up Sort numerically by priority, high priority last.
1380 priority-down Sort numerically by priority, high priority first.
1381 todo-state-up Sort by todo state, tasks that are done last.
1382 todo-state-down Sort by todo state, tasks that are done first.
1383 effort-up Sort numerically by estimated effort, high effort last.
1384 effort-down Sort numerically by estimated effort, high effort first.
1385 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1386 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1387 habit-up Put entries that are habits first
1388 habit-down Put entries that are habits last
1389 alpha-up Sort headlines alphabetically
1390 alpha-down Sort headlines alphabetically, reversed
1392 The different possibilities will be tried in sequence, and testing stops
1393 if one comparison returns a \"not-equal\". For example, the default
1394 '(time-up category-keep priority-down)
1395 means: Pull out all entries having a specified time of day and sort them,
1396 in order to make a time schedule for the current day the first thing in the
1397 agenda listing for the day. Of the entries without a time indication, keep
1398 the grouped in categories, don't sort the categories, but keep them in
1399 the sequence given in `org-agenda-files'. Within each category sort by
1400 priority.
1402 Leaving out `category-keep' would mean that items will be sorted across
1403 categories by priority.
1405 Instead of a single list, this can also be a set of list for specific
1406 contents, with a context symbol in the car of the list, any of
1407 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1409 Custom commands can bind this variable in the options section."
1410 :group 'org-agenda-sorting
1411 :type `(choice
1412 (repeat :tag "General" ,org-sorting-choice)
1413 (list :tag "Individually"
1414 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1415 (repeat ,org-sorting-choice))
1416 (cons (const :tag "Strategy for TODO lists" todo)
1417 (repeat ,org-sorting-choice))
1418 (cons (const :tag "Strategy for Tags matches" tags)
1419 (repeat ,org-sorting-choice))
1420 (cons (const :tag "Strategy for search matches" search)
1421 (repeat ,org-sorting-choice)))))
1423 (defcustom org-agenda-cmp-user-defined nil
1424 "A function to define the comparison `user-defined'.
1425 This function must receive two arguments, agenda entry a and b.
1426 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1427 the user comparison, return nil.
1428 When this is defined, you can make `user-defined-up' and `user-defined-down'
1429 part of an agenda sorting strategy."
1430 :group 'org-agenda-sorting
1431 :type 'symbol)
1433 (defcustom org-sort-agenda-notime-is-late t
1434 "Non-nil means items without time are considered late.
1435 This is only relevant for sorting. When t, items which have no explicit
1436 time like 15:30 will be considered as 99:01, i.e. later than any items which
1437 do have a time. When nil, the default time is before 0:00. You can use this
1438 option to decide if the schedule for today should come before or after timeless
1439 agenda entries."
1440 :group 'org-agenda-sorting
1441 :type 'boolean)
1443 (defcustom org-sort-agenda-noeffort-is-high t
1444 "Non-nil means items without effort estimate are sorted as high effort.
1445 This also applies when filtering an agenda view with respect to the
1446 < or > effort operator. Then, tasks with no effort defined will be treated
1447 as tasks with high effort.
1448 When nil, such items are sorted as 0 minutes effort."
1449 :group 'org-agenda-sorting
1450 :type 'boolean)
1452 (defgroup org-agenda-line-format nil
1453 "Options concerning the entry prefix in the Org-mode agenda display."
1454 :tag "Org Agenda Line Format"
1455 :group 'org-agenda)
1457 (defcustom org-agenda-prefix-format
1458 '((agenda . " %i %-12:c%?-12t% s")
1459 (timeline . " % s")
1460 (todo . " %i %-12:c")
1461 (tags . " %i %-12:c")
1462 (search . " %i %-12:c"))
1463 "Format specifications for the prefix of items in the agenda views.
1464 An alist with five entries, each for the different agenda types. The
1465 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1466 The values are format strings.
1468 This format works similar to a printf format, with the following meaning:
1470 %c the category of the item, \"Diary\" for entries from the diary,
1471 or as given by the CATEGORY keyword or derived from the file name
1472 %e the effort required by the item
1473 %i the icon category of the item, see `org-agenda-category-icon-alist'
1474 %T the last tag of the item (ignore inherited tags, which come first)
1475 %t the HH:MM time-of-day specification if one applies to the entry
1476 %s Scheduling/Deadline information, a short string
1477 %(expression) Eval EXPRESSION and replace the control string
1478 by the result
1480 All specifiers work basically like the standard `%s' of printf, but may
1481 contain two additional characters: a question mark just after the `%'
1482 and a whitespace/punctuation character just before the final letter.
1484 If the first character after `%' is a question mark, the entire field
1485 will only be included if the corresponding value applies to the current
1486 entry. This is useful for fields which should have fixed width when
1487 present, but zero width when absent. For example, \"%?-12t\" will
1488 result in a 12 character time field if a time of the day is specified,
1489 but will completely disappear in entries which do not contain a time.
1491 If there is punctuation or whitespace character just before the final
1492 format letter, this character will be appended to the field value if
1493 the value is not empty. For example, the format \"%-12:c\" leads to
1494 \"Diary: \" if the category is \"Diary\". If the category were be
1495 empty, no additional colon would be inserted.
1497 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1498 which means:
1500 - Indent the line with two space characters
1501 - Give the category a 12 chars wide field, padded with whitespace on
1502 the right (because of `-'). Append a colon if there is a category
1503 (because of `:').
1504 - If there is a time-of-day, put it into a 12 chars wide field. If no
1505 time, don't put in an empty field, just skip it (because of '?').
1506 - Finally, put the scheduling information.
1508 See also the variables `org-agenda-remove-times-when-in-prefix' and
1509 `org-agenda-remove-tags'.
1511 Custom commands can set this variable in the options section."
1512 :type '(choice
1513 (string :tag "General format")
1514 (list :greedy t :tag "View dependent"
1515 (cons (const agenda) (string :tag "Format"))
1516 (cons (const timeline) (string :tag "Format"))
1517 (cons (const todo) (string :tag "Format"))
1518 (cons (const tags) (string :tag "Format"))
1519 (cons (const search) (string :tag "Format"))))
1520 :group 'org-agenda-line-format)
1522 (defvar org-prefix-format-compiled nil
1523 "The compiled prefix format and associated variables.
1524 This is a list where first element is a list of variable bindings, and second
1525 element is the compiled format expression. See the variable
1526 `org-agenda-prefix-format'.")
1528 (defcustom org-agenda-todo-keyword-format "%-1s"
1529 "Format for the TODO keyword in agenda lines.
1530 Set this to something like \"%-12s\" if you want all TODO keywords
1531 to occupy a fixed space in the agenda display."
1532 :group 'org-agenda-line-format
1533 :type 'string)
1535 (defcustom org-agenda-diary-sexp-prefix nil
1536 "A regexp that matches part of a diary sexp entry
1537 which should be treated as scheduling/deadline information in
1538 `org-agenda'.
1540 For example, you can use this to extract the `diary-remind-message' from
1541 `diary-remind' entries."
1542 :group 'org-agenda-line-format
1543 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1545 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1546 "Text preceding timerange entries in the agenda view.
1547 This is a list with two strings. The first applies when the range
1548 is entirely on one day. The second applies if the range spans several days.
1549 The strings may have two \"%d\" format specifiers which will be filled
1550 with the sequence number of the days, and the total number of days in the
1551 range, respectively."
1552 :group 'org-agenda-line-format
1553 :type '(list
1554 (string :tag "Deadline today ")
1555 (choice :tag "Deadline relative"
1556 (string :tag "Format string")
1557 (function))))
1559 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1560 "Text preceding scheduled items in the agenda view.
1561 This is a list with two strings. The first applies when the item is
1562 scheduled on the current day. The second applies when it has been scheduled
1563 previously, it may contain a %d indicating that this is the nth time that
1564 this item is scheduled, due to automatic rescheduling of unfinished items
1565 for the following day. So this number is one larger than the number of days
1566 that passed since this item was scheduled first."
1567 :group 'org-agenda-line-format
1568 :type '(list
1569 (string :tag "Scheduled today ")
1570 (string :tag "Scheduled previously")))
1572 (defcustom org-agenda-inactive-leader "["
1573 "Text preceding item pulled into the agenda by inactive time stamps.
1574 These entries are added to the agenda when pressing \"[\"."
1575 :group 'org-agenda-line-format
1576 :version "24.1"
1577 :type '(list
1578 (string :tag "Scheduled today ")
1579 (string :tag "Scheduled previously")))
1581 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1582 "Text preceding deadline items in the agenda view.
1583 This is a list with two strings. The first applies when the item has its
1584 deadline on the current day. The second applies when it is in the past or
1585 in the future, it may contain %d to capture how many days away the deadline
1586 is (was)."
1587 :group 'org-agenda-line-format
1588 :type '(list
1589 (string :tag "Deadline today ")
1590 (choice :tag "Deadline relative"
1591 (string :tag "Format string")
1592 (function))))
1594 (defcustom org-agenda-remove-times-when-in-prefix t
1595 "Non-nil means remove duplicate time specifications in agenda items.
1596 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1597 time-of-day specification in a headline or diary entry is extracted and
1598 placed into the prefix. If this option is non-nil, the original specification
1599 \(a timestamp or -range, or just a plain time(range) specification like
1600 11:30-4pm) will be removed for agenda display. This makes the agenda less
1601 cluttered.
1602 The option can be t or nil. It may also be the symbol `beg', indicating
1603 that the time should only be removed when it is located at the beginning of
1604 the headline/diary entry."
1605 :group 'org-agenda-line-format
1606 :type '(choice
1607 (const :tag "Always" t)
1608 (const :tag "Never" nil)
1609 (const :tag "When at beginning of entry" beg)))
1611 (defcustom org-agenda-remove-timeranges-from-blocks nil
1612 "Non-nil means remove time ranges specifications in agenda
1613 items that span on several days."
1614 :group 'org-agenda-line-format
1615 :version "24.1"
1616 :type 'boolean)
1618 (defcustom org-agenda-default-appointment-duration nil
1619 "Default duration for appointments that only have a starting time.
1620 When nil, no duration is specified in such cases.
1621 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1622 :group 'org-agenda-line-format
1623 :type '(choice
1624 (integer :tag "Minutes")
1625 (const :tag "No default duration")))
1627 (defcustom org-agenda-show-inherited-tags t
1628 "Non-nil means show inherited tags in each agenda line."
1629 :group 'org-agenda-line-format
1630 :type 'boolean)
1632 (defcustom org-agenda-hide-tags-regexp nil
1633 "Regular expression used to filter away specific tags in agenda views.
1634 This means that these tags will be present, but not be shown in the agenda
1635 line. Secondary filtering will still work on the hidden tags.
1636 Nil means don't hide any tags."
1637 :group 'org-agenda-line-format
1638 :type '(choice
1639 (const :tag "Hide none" nil)
1640 (string :tag "Regexp ")))
1642 (defcustom org-agenda-remove-tags nil
1643 "Non-nil means remove the tags from the headline copy in the agenda.
1644 When this is the symbol `prefix', only remove tags when
1645 `org-agenda-prefix-format' contains a `%T' specifier."
1646 :group 'org-agenda-line-format
1647 :type '(choice
1648 (const :tag "Always" t)
1649 (const :tag "Never" nil)
1650 (const :tag "When prefix format contains %T" prefix)))
1652 (if (fboundp 'defvaralias)
1653 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1654 'org-agenda-remove-tags))
1656 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1657 "Shift tags in agenda items to this column.
1658 If this number is positive, it specifies the column. If it is negative,
1659 it means that the tags should be flushright to that column. For example,
1660 -80 works well for a normal 80 character screen."
1661 :group 'org-agenda-line-format
1662 :type 'integer)
1664 (if (fboundp 'defvaralias)
1665 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1667 (defcustom org-agenda-fontify-priorities 'cookies
1668 "Non-nil means highlight low and high priorities in agenda.
1669 When t, the highest priority entries are bold, lowest priority italic.
1670 However, settings in `org-priority-faces' will overrule these faces.
1671 When this variable is the symbol `cookies', only fontify the
1672 cookies, not the entire task.
1673 This may also be an association list of priority faces, whose
1674 keys are the character values of `org-highest-priority',
1675 `org-default-priority', and `org-lowest-priority' (the default values
1676 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1677 color as a string, or a list like `(:background \"Red\")'.
1678 If it is a color, the variable `org-faces-easy-properties'
1679 determines if it is a foreground or a background color."
1680 :group 'org-agenda-line-format
1681 :type '(choice
1682 (const :tag "Never" nil)
1683 (const :tag "Defaults" t)
1684 (const :tag "Cookies only" cookies)
1685 (repeat :tag "Specify"
1686 (list (character :tag "Priority" :value ?A)
1687 (choice :tag "Face "
1688 (string :tag "Color")
1689 (sexp :tag "Face"))))))
1691 (defcustom org-agenda-day-face-function nil
1692 "Function called to determine what face should be used to display a day.
1693 The only argument passed to that function is the day. It should
1694 returns a face, or nil if does not want to specify a face and let
1695 the normal rules apply."
1696 :group 'org-agenda-line-format
1697 :version "24.1"
1698 :type 'function)
1700 (defcustom org-agenda-category-icon-alist nil
1701 "Alist of category icon to be displayed in agenda views.
1703 Each entry should have the following format:
1705 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1707 Where CATEGORY-REGEXP is a regexp matching the categories where
1708 the icon should be displayed.
1709 FILE-OR-DATA either a file path or a string containing image data.
1711 The other fields can be omitted safely if not needed:
1712 TYPE indicates the image type.
1713 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1714 image data.
1715 PROPS are additional image attributes to assign to the image,
1716 like, e.g. `:ascent center'.
1718 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1720 If you want to set the display properties yourself, just put a
1721 list as second element:
1723 (CATEGORY-REGEXP (MY PROPERTY LIST))
1725 For example, to display a 16px horizontal space for Emacs
1726 category, you can use:
1728 (\"Emacs\" '(space . (:width (16))))"
1729 :group 'org-agenda-line-format
1730 :version "24.1"
1731 :type '(alist :key-type (string :tag "Regexp matching category")
1732 :value-type (choice (list :tag "Icon"
1733 (string :tag "File or data")
1734 (symbol :tag "Type")
1735 (boolean :tag "Data?")
1736 (repeat :tag "Extra image properties" :inline t symbol))
1737 (list :tag "Display properties" sexp))))
1739 (defgroup org-agenda-column-view nil
1740 "Options concerning column view in the agenda."
1741 :tag "Org Agenda Column View"
1742 :group 'org-agenda)
1744 (defcustom org-agenda-columns-show-summaries t
1745 "Non-nil means show summaries for columns displayed in the agenda view."
1746 :group 'org-agenda-column-view
1747 :type 'boolean)
1749 (defcustom org-agenda-columns-compute-summary-properties t
1750 "Non-nil means recompute all summary properties before column view.
1751 When column view in the agenda is listing properties that have a summary
1752 operator, it can go to all relevant buffers and recompute the summaries
1753 there. This can mean overhead for the agenda column view, but is necessary
1754 to have thing up to date.
1755 As a special case, a CLOCKSUM property also makes sure that the clock
1756 computations are current."
1757 :group 'org-agenda-column-view
1758 :type 'boolean)
1760 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1761 "Non-nil means the duration of an appointment will add to day effort.
1762 The property to which appointment durations will be added is the one given
1763 in the option `org-effort-property'. If an appointment does not have
1764 an end time, `org-agenda-default-appointment-duration' will be used. If that
1765 is not set, an appointment without end time will not contribute to the time
1766 estimate."
1767 :group 'org-agenda-column-view
1768 :type 'boolean)
1770 (defcustom org-agenda-auto-exclude-function nil
1771 "A function called with a tag to decide if it is filtered on '/ RET'.
1772 The sole argument to the function, which is called once for each
1773 possible tag, is a string giving the name of the tag. The
1774 function should return either nil if the tag should be included
1775 as normal, or \"-<TAG>\" to exclude the tag.
1776 Note that for the purpose of tag filtering, only the lower-case version of
1777 all tags will be considered, so that this function will only ever see
1778 the lower-case version of all tags."
1779 :group 'org-agenda
1780 :type 'function)
1782 (defcustom org-agenda-bulk-custom-functions nil
1783 "Alist of characters and custom functions for bulk actions.
1784 For example, this value makes those two functions available:
1786 '((?R set-category)
1787 (?C bulk-cut))
1789 With selected entries in an agenda buffer, `B R' will call
1790 the custom function `set-category' on the selected entries.
1791 Note that functions in this alist don't need to be quoted."
1792 :type 'alist
1793 :version "24.1"
1794 :group 'org-agenda)
1796 (eval-when-compile
1797 (require 'cl))
1798 (require 'org)
1800 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1801 "Execute BODY with point at location given by `org-hd-marker' property.
1802 If STRING is non-nil, the text property will be fetched from position 0
1803 in that string. If STRING is nil, it will be fetched from the beginning
1804 of the current line."
1805 (org-with-gensyms (marker)
1806 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1807 'org-hd-marker ,string)))
1808 (with-current-buffer (marker-buffer ,marker)
1809 (save-excursion
1810 (goto-char ,marker)
1811 ,@body)))))
1812 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1814 (defun org-add-agenda-custom-command (entry)
1815 "Replace or add a command in `org-agenda-custom-commands'.
1816 This is mostly for hacking and trying a new command - once the command
1817 works you probably want to add it to `org-agenda-custom-commands' for good."
1818 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1819 (if ass
1820 (setcdr ass (cdr entry))
1821 (push entry org-agenda-custom-commands))))
1823 ;;; Define the org-agenda-mode
1825 (defvar org-agenda-mode-map (make-sparse-keymap)
1826 "Keymap for `org-agenda-mode'.")
1827 (if (fboundp 'defvaralias)
1828 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1830 (defvar org-agenda-menu) ; defined later in this file.
1831 (defvar org-agenda-restrict) ; defined later in this file.
1832 (defvar org-agenda-follow-mode nil)
1833 (defvar org-agenda-entry-text-mode nil)
1834 (defvar org-agenda-clockreport-mode nil)
1835 (defvar org-agenda-show-log nil)
1836 (defvar org-agenda-redo-command nil)
1837 (defvar org-agenda-query-string nil)
1838 (defvar org-agenda-mode-hook nil
1839 "Hook for `org-agenda-mode', run after the mode is turned on.")
1840 (defvar org-agenda-type nil)
1841 (defvar org-agenda-force-single-file nil)
1842 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1845 ;;; Multiple agenda buffers support
1847 (defcustom org-agenda-sticky nil
1848 "Non-nil means agenda q key will bury agenda buffers.
1849 Agenda commands will then show existing buffer instead of generating new ones.
1850 When nil, `q' will kill the single agenda buffer."
1851 :group 'org-agenda
1852 :type 'boolean
1853 :set (lambda (var val)
1854 (if (boundp var)
1855 (org-toggle-sticky-agenda (if val 1 0))
1856 (set var val))))
1858 (defun org-toggle-sticky-agenda (&optional arg)
1859 "Toggle `org-agenda-sticky'."
1860 (interactive "P")
1861 (let ((new-value (if arg
1862 (> (prefix-numeric-value arg) 0)
1863 (not org-agenda-sticky))))
1864 (if (equal new-value org-agenda-sticky)
1865 (and (org-called-interactively-p 'interactive)
1866 (message "Sticky agenda was already %s"
1867 (if org-agenda-sticky "enabled" "disabled")))
1868 (setq org-agenda-sticky new-value)
1869 (org-agenda-kill-all-agenda-buffers)
1870 (and (org-called-interactively-p 'interactive)
1871 (message "Sticky agenda was %s"
1872 (if org-agenda-sticky "enabled" "disabled"))))))
1874 (defvar org-agenda-buffer nil
1875 "Agenda buffer currently being generated.")
1877 (defvar org-agenda-last-prefix-arg nil)
1878 (defvar org-agenda-this-buffer-name nil)
1879 (defvar org-agenda-doing-sticky-redo nil)
1880 (defvar org-agenda-this-buffer-is-sticky nil)
1882 (defconst org-agenda-local-vars
1883 '(org-agenda-this-buffer-name
1884 org-agenda-undo-list
1885 org-agenda-pending-undo-list
1886 org-agenda-follow-mode
1887 org-agenda-entry-text-mode
1888 org-agenda-clockreport-mode
1889 org-agenda-show-log
1890 org-agenda-redo-command
1891 org-agenda-query-string
1892 org-agenda-type
1893 org-agenda-bulk-marked-entries
1894 org-agenda-undo-has-started-in
1895 org-agenda-last-arguments
1896 org-agenda-info
1897 org-agenda-tag-filter-overlays
1898 org-agenda-cat-filter-overlays
1899 org-pre-agenda-window-conf
1900 org-agenda-columns-active
1901 org-agenda-tag-filter
1902 org-agenda-category-filter
1903 org-agenda-markers
1904 org-agenda-last-search-view-search-was-boolean
1905 org-agenda-filtered-by-category
1906 org-agenda-filter-form
1907 org-agenda-show-window
1908 org-agenda-cycle-counter
1909 org-agenda-last-prefix-arg)
1910 "Variables that must be local in agenda buffers to allow multiple buffers.")
1912 (defun org-agenda-mode ()
1913 "Mode for time-sorted view on action items in Org-mode files.
1915 The following commands are available:
1917 \\{org-agenda-mode-map}"
1918 (interactive)
1919 (cond (org-agenda-doing-sticky-redo
1920 ;; Refreshing sticky agenda-buffer
1922 ;; Preserve the value of `org-agenda-local-vars' variables,
1923 ;; while letting `kill-all-local-variables' kill the rest
1924 (let ((save (buffer-local-variables)))
1925 (kill-all-local-variables)
1926 (mapc 'make-local-variable org-agenda-local-vars)
1927 (dolist (elem save)
1928 (let ((var (car elem))
1929 (val (cdr elem)))
1930 (when (and val
1931 (member var org-agenda-local-vars))
1932 (set var val)))))
1933 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1934 (org-agenda-sticky
1935 ;; Creating a sticky Agenda buffer for the first time
1936 (kill-all-local-variables)
1937 (mapc 'make-local-variable org-agenda-local-vars)
1938 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1940 ;; Creating a non-sticky agenda buffer
1941 (kill-all-local-variables)
1942 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
1943 (setq org-agenda-undo-list nil
1944 org-agenda-pending-undo-list nil
1945 org-agenda-bulk-marked-entries nil)
1946 (setq major-mode 'org-agenda-mode)
1947 ;; Keep global-font-lock-mode from turning on font-lock-mode
1948 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1949 (setq mode-name "Org-Agenda")
1950 (use-local-map org-agenda-mode-map)
1951 (easy-menu-add org-agenda-menu)
1952 (if org-startup-truncated (setq truncate-lines t))
1953 (org-set-local 'line-move-visual nil)
1954 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1955 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1956 ;; Make sure properties are removed when copying text
1957 (make-local-variable 'filter-buffer-substring-functions)
1958 (add-hook 'filter-buffer-substring-functions
1959 (lambda (fun start end delete)
1960 (substring-no-properties (funcall fun start end delete))))
1961 (unless org-agenda-keep-modes
1962 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1963 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1964 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1965 org-agenda-show-log org-agenda-start-with-log-mode))
1967 (easy-menu-change
1968 '("Agenda") "Agenda Files"
1969 (append
1970 (list
1971 (vector
1972 (if (get 'org-agenda-files 'org-restrict)
1973 "Restricted to single file"
1974 "Edit File List")
1975 '(org-edit-agenda-file-list)
1976 (not (get 'org-agenda-files 'org-restrict)))
1977 "--")
1978 (mapcar 'org-file-menu-entry (org-agenda-files))))
1979 (org-agenda-set-mode-name)
1980 (apply
1981 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1982 (list 'org-agenda-mode-hook)))
1984 (substitute-key-definition 'undo 'org-agenda-undo
1985 org-agenda-mode-map global-map)
1986 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1987 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1988 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1989 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1990 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1991 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1992 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
1993 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1994 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1995 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
1996 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1997 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1998 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1999 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2000 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2001 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2002 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2003 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2004 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2005 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2006 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2007 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2008 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2009 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2010 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2011 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2012 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2013 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2014 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2015 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2016 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2017 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2018 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2019 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2020 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2021 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2022 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2023 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2024 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2025 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2026 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
2027 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
2028 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2029 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2030 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2031 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2033 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2034 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2035 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2036 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2037 (while l (org-defkey org-agenda-mode-map
2038 (int-to-string (pop l)) 'digit-argument)))
2040 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2041 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2042 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2043 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2044 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2045 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2046 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2047 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2048 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2049 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
2050 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2051 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2052 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2053 'org-clock-modify-effort-estimate)
2054 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2055 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2056 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2057 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2058 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2059 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2060 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2061 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
2062 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2063 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2064 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2065 (substitute-key-definition 'next-line 'org-agenda-next-line
2066 org-agenda-mode-map global-map)
2067 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2068 org-agenda-mode-map global-map)
2069 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2070 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2071 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2072 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2073 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2074 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2075 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2076 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2077 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2078 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2079 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2080 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2081 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2082 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2083 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2084 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2085 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2086 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2087 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2088 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2089 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2090 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2091 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2092 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2093 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2094 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2095 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2096 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2097 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2098 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2100 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2101 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2102 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2103 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2104 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2105 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2106 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2107 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-category)
2108 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2109 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2110 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2111 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2113 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2114 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2115 (when org-agenda-mouse-1-follows-link
2116 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2117 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2118 '("Agenda"
2119 ("Agenda Files")
2120 "--"
2121 ("Agenda Dates"
2122 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2123 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2124 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2125 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2126 "--"
2127 ("View"
2128 ["Day View" org-agenda-day-view
2129 :active (org-agenda-check-type nil 'agenda)
2130 :style radio :selected (eq org-agenda-current-span 'day)
2131 :keys "v d (or just d)"]
2132 ["Week View" org-agenda-week-view
2133 :active (org-agenda-check-type nil 'agenda)
2134 :style radio :selected (eq org-agenda-current-span 'week)
2135 :keys "v w (or just w)"]
2136 ["Month View" org-agenda-month-view
2137 :active (org-agenda-check-type nil 'agenda)
2138 :style radio :selected (eq org-agenda-current-span 'month)
2139 :keys "v m"]
2140 ["Year View" org-agenda-year-view
2141 :active (org-agenda-check-type nil 'agenda)
2142 :style radio :selected (eq org-agenda-current-span 'year)
2143 :keys "v y"]
2144 "--"
2145 ["Include Diary" org-agenda-toggle-diary
2146 :style toggle :selected org-agenda-include-diary
2147 :active (org-agenda-check-type nil 'agenda)]
2148 ["Include Deadlines" org-agenda-toggle-deadlines
2149 :style toggle :selected org-agenda-include-deadlines
2150 :active (org-agenda-check-type nil 'agenda)]
2151 ["Use Time Grid" org-agenda-toggle-time-grid
2152 :style toggle :selected org-agenda-use-time-grid
2153 :active (org-agenda-check-type nil 'agenda)]
2154 "--"
2155 ["Show clock report" org-agenda-clockreport-mode
2156 :style toggle :selected org-agenda-clockreport-mode
2157 :active (org-agenda-check-type nil 'agenda)]
2158 ["Show some entry text" org-agenda-entry-text-mode
2159 :style toggle :selected org-agenda-entry-text-mode
2160 :active t]
2161 "--"
2162 ["Show Logbook entries" org-agenda-log-mode
2163 :style toggle :selected org-agenda-show-log
2164 :active (org-agenda-check-type nil 'agenda 'timeline)
2165 :keys "v l (or just l)"]
2166 ["Include archived trees" org-agenda-archives-mode
2167 :style toggle :selected org-agenda-archives-mode :active t
2168 :keys "v a"]
2169 ["Include archive files" (org-agenda-archives-mode t)
2170 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2171 :keys "v A"]
2172 "--"
2173 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2174 ["Write view to file" org-agenda-write t]
2175 ["Rebuild buffer" org-agenda-redo t]
2176 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2177 "--"
2178 ["Show original entry" org-agenda-show t]
2179 ["Go To (other window)" org-agenda-goto t]
2180 ["Go To (this window)" org-agenda-switch-to t]
2181 ["Follow Mode" org-agenda-follow-mode
2182 :style toggle :selected org-agenda-follow-mode :active t]
2183 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2184 "--"
2185 ("TODO"
2186 ["Cycle TODO" org-agenda-todo t]
2187 ["Next TODO set" org-agenda-todo-nextset t]
2188 ["Previous TODO set" org-agenda-todo-previousset t]
2189 ["Add note" org-agenda-add-note t])
2190 ("Archive/Refile/Delete"
2191 ["Archive default" org-agenda-archive-default t]
2192 ["Archive default" org-agenda-archive-default-with-confirmation t]
2193 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2194 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2195 ["Archive subtree" org-agenda-archive t]
2196 "--"
2197 ["Refile" org-agenda-refile t]
2198 "--"
2199 ["Delete subtree" org-agenda-kill t])
2200 ("Bulk action"
2201 ["Mark entry" org-agenda-bulk-mark t]
2202 ["Mark all" org-agenda-bulk-mark-all t]
2203 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2204 ["Unmark entry" org-agenda-bulk-unmark t]
2205 ["Unmark all entries" org-agenda-bulk-unmark-all :active t :keys "C-u s"])
2206 ["Act on all marked" org-agenda-bulk-action t]
2207 "--"
2208 ("Tags and Properties"
2209 ["Show all Tags" org-agenda-show-tags t]
2210 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2211 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2212 "--"
2213 ["Column View" org-columns t])
2214 ("Deadline/Schedule"
2215 ["Schedule" org-agenda-schedule t]
2216 ["Set Deadline" org-agenda-deadline t]
2217 "--"
2218 ["Mark item" org-agenda-action :active t :keys "k m"]
2219 ["Show mark item" org-agenda-action :active t :keys "k v"]
2220 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2221 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2222 "--"
2223 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2224 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2225 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2226 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2227 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2228 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2229 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2230 ("Clock and Effort"
2231 ["Clock in" org-agenda-clock-in t]
2232 ["Clock out" org-agenda-clock-out t]
2233 ["Clock cancel" org-agenda-clock-cancel t]
2234 ["Goto running clock" org-clock-goto t]
2235 "--"
2236 ["Set Effort" org-agenda-set-effort t]
2237 ["Change clocked effort" org-clock-modify-effort-estimate
2238 (org-clock-is-active)])
2239 ("Priority"
2240 ["Set Priority" org-agenda-priority t]
2241 ["Increase Priority" org-agenda-priority-up t]
2242 ["Decrease Priority" org-agenda-priority-down t]
2243 ["Show Priority" org-agenda-show-priority t])
2244 ("Calendar/Diary"
2245 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2246 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2247 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2248 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2249 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2250 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2251 "--"
2252 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2253 "--"
2254 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2255 "--"
2256 ("MobileOrg"
2257 ["Push Files and Views" org-mobile-push t]
2258 ["Get Captured and Flagged" org-mobile-pull t]
2259 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2260 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2261 "--"
2262 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2263 "--"
2264 ["Quit" org-agenda-quit t]
2265 ["Exit and Release Buffers" org-agenda-exit t]
2268 ;;; Agenda undo
2270 (defvar org-agenda-allow-remote-undo t
2271 "Non-nil means allow remote undo from the agenda buffer.")
2272 (defvar org-agenda-undo-list nil
2273 "List of undoable operations in the agenda since last refresh.")
2274 (defvar org-agenda-undo-has-started-in nil
2275 "Buffers that have already seen `undo-start' in the current undo sequence.")
2276 (defvar org-agenda-pending-undo-list nil
2277 "In a series of undo commands, this is the list of remaining undo items.")
2279 (defun org-agenda-undo ()
2280 "Undo a remote editing step in the agenda.
2281 This undoes changes both in the agenda buffer and in the remote buffer
2282 that have been changed along."
2283 (interactive)
2284 (or org-agenda-allow-remote-undo
2285 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2286 (if (not (eq this-command last-command))
2287 (setq org-agenda-undo-has-started-in nil
2288 org-agenda-pending-undo-list org-agenda-undo-list))
2289 (if (not org-agenda-pending-undo-list)
2290 (error "No further undo information"))
2291 (let* ((entry (pop org-agenda-pending-undo-list))
2292 buf line cmd rembuf)
2293 (setq cmd (pop entry) line (pop entry))
2294 (setq rembuf (nth 2 entry))
2295 (org-with-remote-undo rembuf
2296 (while (bufferp (setq buf (pop entry)))
2297 (if (pop entry)
2298 (with-current-buffer buf
2299 (let ((last-undo-buffer buf)
2300 (inhibit-read-only t))
2301 (unless (memq buf org-agenda-undo-has-started-in)
2302 (push buf org-agenda-undo-has-started-in)
2303 (make-local-variable 'pending-undo-list)
2304 (undo-start))
2305 (while (and pending-undo-list
2306 (listp pending-undo-list)
2307 (not (car pending-undo-list)))
2308 (pop pending-undo-list))
2309 (undo-more 1))))))
2310 (org-goto-line line)
2311 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2313 (defun org-verify-change-for-undo (l1 l2)
2314 "Verify that a real change occurred between the undo lists L1 and L2."
2315 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2316 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2317 (not (eq l1 l2)))
2319 ;;; Agenda dispatch
2321 (defvar org-agenda-restrict nil)
2322 (defvar org-agenda-restrict-begin (make-marker))
2323 (defvar org-agenda-restrict-end (make-marker))
2324 (defvar org-agenda-last-dispatch-buffer nil)
2325 (defvar org-agenda-overriding-restriction nil)
2327 ;;;###autoload
2328 (defun org-agenda (&optional arg keys restriction)
2329 "Dispatch agenda commands to collect entries to the agenda buffer.
2330 Prompts for a command to execute. Any prefix arg will be passed
2331 on to the selected command. The default selections are:
2333 a Call `org-agenda-list' to display the agenda for current day or week.
2334 t Call `org-todo-list' to display the global todo list.
2335 T Call `org-todo-list' to display the global todo list, select only
2336 entries with a specific TODO keyword (the user gets a prompt).
2337 m Call `org-tags-view' to display headlines with tags matching
2338 a condition (the user is prompted for the condition).
2339 M Like `m', but select only TODO entries, no ordinary headlines.
2340 L Create a timeline for the current buffer.
2341 e Export views to associated files.
2342 s Search entries for keywords.
2343 / Multi occur across all agenda files and also files listed
2344 in `org-agenda-text-search-extra-files'.
2345 < Restrict agenda commands to buffer, subtree, or region.
2346 Press several times to get the desired effect.
2347 > Remove a previous restriction.
2348 # List \"stuck\" projects.
2349 ! Configure what \"stuck\" means.
2350 C Configure custom agenda commands.
2352 More commands can be added by configuring the variable
2353 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2354 searches can be pre-defined in this way.
2356 If the current buffer is in Org-mode and visiting a file, you can also
2357 first press `<' once to indicate that the agenda should be temporarily
2358 \(until the next use of \\[org-agenda]) restricted to the current file.
2359 Pressing `<' twice means to restrict to the current subtree or region
2360 \(if active)."
2361 (interactive "P")
2362 (catch 'exit
2363 (let* ((prefix-descriptions nil)
2364 (org-agenda-buffer-name org-agenda-buffer-name)
2365 (org-agenda-window-setup (if (equal (buffer-name)
2366 org-agenda-buffer-name)
2367 'current-window
2368 org-agenda-window-setup))
2369 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2370 (org-agenda-custom-commands
2371 ;; normalize different versions
2372 (delq nil
2373 (mapcar
2374 (lambda (x)
2375 (cond ((stringp (cdr x))
2376 (push x prefix-descriptions)
2377 nil)
2378 ((stringp (nth 1 x)) x)
2379 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2380 (t (cons (car x) (cons "" (cdr x))))))
2381 org-agenda-custom-commands)))
2382 (buf (current-buffer))
2383 (bfn (buffer-file-name (buffer-base-buffer)))
2384 entry key type match lprops ans)
2385 ;; Turn off restriction unless there is an overriding one,
2386 (unless org-agenda-overriding-restriction
2387 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2388 ;; There is a request to keep the file list in place
2389 (put 'org-agenda-files 'org-restrict nil))
2390 (setq org-agenda-restrict nil)
2391 (move-marker org-agenda-restrict-begin nil)
2392 (move-marker org-agenda-restrict-end nil))
2393 ;; Delete old local properties
2394 (put 'org-agenda-redo-command 'org-lprops nil)
2395 ;; Delete previously set last-arguments
2396 (put 'org-agenda-redo-command 'last-args nil)
2397 ;; Remember where this call originated
2398 (setq org-agenda-last-dispatch-buffer (current-buffer))
2399 (unless keys
2400 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2401 keys (car ans)
2402 restriction (cdr ans)))
2403 ;; If we have sticky agenda buffers, set a name for the buffer,
2404 ;; depending on the invoking keys. The user may still set this
2405 ;; as a command option, which will overwrite what we do here.
2406 (if org-agenda-sticky
2407 (setq org-agenda-buffer-name (format "*Org Agenda(%s)*" keys)))
2408 ;; Establish the restriction, if any
2409 (when (and (not org-agenda-overriding-restriction) restriction)
2410 (put 'org-agenda-files 'org-restrict (list bfn))
2411 (cond
2412 ((eq restriction 'region)
2413 (setq org-agenda-restrict t)
2414 (move-marker org-agenda-restrict-begin (region-beginning))
2415 (move-marker org-agenda-restrict-end (region-end)))
2416 ((eq restriction 'subtree)
2417 (save-excursion
2418 (setq org-agenda-restrict t)
2419 (org-back-to-heading t)
2420 (move-marker org-agenda-restrict-begin (point))
2421 (move-marker org-agenda-restrict-end
2422 (progn (org-end-of-subtree t)))))))
2424 ;; For example the todo list should not need it (but does...)
2425 (cond
2426 ((setq entry (assoc keys org-agenda-custom-commands))
2427 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2428 (progn
2429 (setq type (nth 2 entry) match (eval (nth 3 entry))
2430 lprops (nth 4 entry))
2431 (put 'org-agenda-redo-command 'org-lprops lprops)
2432 (cond
2433 ((eq type 'agenda)
2434 (org-let lprops '(org-agenda-list current-prefix-arg)))
2435 ((eq type 'alltodo)
2436 (org-let lprops '(org-todo-list current-prefix-arg)))
2437 ((eq type 'search)
2438 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2439 ((eq type 'stuck)
2440 (org-let lprops '(org-agenda-list-stuck-projects
2441 current-prefix-arg)))
2442 ((eq type 'tags)
2443 (org-let lprops '(org-tags-view current-prefix-arg match)))
2444 ((eq type 'tags-todo)
2445 (org-let lprops '(org-tags-view '(4) match)))
2446 ((eq type 'todo)
2447 (org-let lprops '(org-todo-list match)))
2448 ((eq type 'tags-tree)
2449 (org-check-for-org-mode)
2450 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2451 ((eq type 'todo-tree)
2452 (org-check-for-org-mode)
2453 (org-let lprops
2454 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2455 (regexp-quote match) "\\>"))))
2456 ((eq type 'occur-tree)
2457 (org-check-for-org-mode)
2458 (org-let lprops '(org-occur match)))
2459 ((functionp type)
2460 (org-let lprops '(funcall type match)))
2461 ((fboundp type)
2462 (org-let lprops '(funcall type match)))
2463 (t (error "Invalid custom agenda command type %s" type))))
2464 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2465 ((equal keys "C")
2466 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2467 (customize-variable 'org-agenda-custom-commands))
2468 ((equal keys "a") (call-interactively 'org-agenda-list))
2469 ((equal keys "s") (call-interactively 'org-search-view))
2470 ((equal keys "t") (call-interactively 'org-todo-list))
2471 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2472 ((equal keys "m") (call-interactively 'org-tags-view))
2473 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2474 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2475 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2476 (org-add-hook
2477 'post-command-hook
2478 (lambda ()
2479 (unless (current-message)
2480 (let* ((m (org-agenda-get-any-marker))
2481 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2482 (when note
2483 (message (concat
2484 "FLAGGING-NOTE ([?] for more info): "
2485 (org-add-props
2486 (replace-regexp-in-string
2487 "\\\\n" "//"
2488 (copy-sequence note))
2489 nil 'face 'org-warning)))))))
2490 t t))
2491 ((equal keys "L")
2492 (unless (derived-mode-p 'org-mode)
2493 (error "This is not an Org-mode file"))
2494 (unless restriction
2495 (put 'org-agenda-files 'org-restrict (list bfn))
2496 (org-call-with-arg 'org-timeline arg)))
2497 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2498 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2499 ((equal keys "!") (customize-variable 'org-stuck-projects))
2500 (t (error "Invalid agenda key"))))))
2502 (defun org-agenda-append-agenda ()
2503 "Append another agenda view to the current one.
2504 This function allows interactive building of block agendas.
2505 Agenda views are separated by `org-agenda-block-separator'."
2506 (interactive)
2507 (unless (string= (buffer-name) org-agenda-buffer-name)
2508 (error "Can only append from within agenda buffer"))
2509 (let ((org-agenda-multi t))
2510 (org-agenda)
2511 (widen)))
2513 (defun org-agenda-normalize-custom-commands (cmds)
2514 (delq nil
2515 (mapcar
2516 (lambda (x)
2517 (cond ((stringp (cdr x)) nil)
2518 ((stringp (nth 1 x)) x)
2519 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2520 (t (cons (car x) (cons "" (cdr x))))))
2521 cmds)))
2523 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2524 "The user interface for selecting an agenda command."
2525 (catch 'exit
2526 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2527 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2528 (region-p (org-region-active-p))
2529 (custom org-agenda-custom-commands)
2530 (selstring "")
2531 restriction second-time
2532 c entry key type match prefixes rmheader header-end custom1 desc
2533 line lines left right n n1)
2534 (save-window-excursion
2535 (delete-other-windows)
2536 (org-switch-to-buffer-other-window " *Agenda Commands*")
2537 (erase-buffer)
2538 (insert (eval-when-compile
2539 (let ((header
2540 "Press key for an agenda command: < Buffer, subtree/region restriction
2541 -------------------------------- > Remove restriction
2542 a Agenda for current week or day e Export agenda views
2543 t List of all TODO entries T Entries with special TODO kwd
2544 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2545 L Timeline for current buffer # List stuck projects (!=configure)
2546 s Search for keywords * Toggle sticky agenda views
2547 / Multi-occur ? Find :FLAGGED: entries
2548 C Configure custom agenda commands
2550 (start 0))
2551 (while (string-match
2552 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2553 header start)
2554 (setq start (match-end 0))
2555 (add-text-properties (match-beginning 2) (match-end 2)
2556 '(face bold) header))
2557 header)))
2558 (setq header-end (move-marker (make-marker) (point)))
2559 (while t
2560 (setq custom1 custom)
2561 (when (eq rmheader t)
2562 (org-goto-line 1)
2563 (re-search-forward ":" nil t)
2564 (delete-region (match-end 0) (point-at-eol))
2565 (forward-char 1)
2566 (looking-at "-+")
2567 (delete-region (match-end 0) (point-at-eol))
2568 (move-marker header-end (match-end 0)))
2569 (goto-char header-end)
2570 (delete-region (point) (point-max))
2572 ;; Produce all the lines that describe custom commands and prefixes
2573 (setq lines nil)
2574 (while (setq entry (pop custom1))
2575 (setq key (car entry) desc (nth 1 entry)
2576 type (nth 2 entry)
2577 match (nth 3 entry))
2578 (if (> (length key) 1)
2579 (add-to-list 'prefixes (string-to-char key))
2580 (setq line
2581 (format
2582 "%-4s%-14s"
2583 (org-add-props (copy-sequence key)
2584 '(face bold))
2585 (cond
2586 ((string-match "\\S-" desc) desc)
2587 ((eq type 'agenda) "Agenda for current week or day")
2588 ((eq type 'alltodo) "List of all TODO entries")
2589 ((eq type 'search) "Word search")
2590 ((eq type 'stuck) "List of stuck projects")
2591 ((eq type 'todo) "TODO keyword")
2592 ((eq type 'tags) "Tags query")
2593 ((eq type 'tags-todo) "Tags (TODO)")
2594 ((eq type 'tags-tree) "Tags tree")
2595 ((eq type 'todo-tree) "TODO kwd tree")
2596 ((eq type 'occur-tree) "Occur tree")
2597 ((functionp type) (if (symbolp type)
2598 (symbol-name type)
2599 "Lambda expression"))
2600 (t "???"))))
2601 (if org-agenda-menu-show-matcher
2602 (setq line
2603 (concat line ": "
2604 (cond
2605 ((stringp match)
2606 (setq match (copy-sequence match))
2607 (org-add-props match nil 'face 'org-warning))
2608 (match
2609 (format "set of %d commands" (length match)))
2610 (t ""))))
2611 (if (org-string-nw-p match)
2612 (add-text-properties
2613 0 (length line) (list 'help-echo
2614 (concat "Matcher: "match)) line)))
2615 (push line lines)))
2616 (setq lines (nreverse lines))
2617 (when prefixes
2618 (mapc (lambda (x)
2619 (push
2620 (format "%s %s"
2621 (org-add-props (char-to-string x)
2622 nil 'face 'bold)
2623 (or (cdr (assoc (concat selstring
2624 (char-to-string x))
2625 prefix-descriptions))
2626 "Prefix key"))
2627 lines))
2628 prefixes))
2630 ;; Check if we should display in two columns
2631 (if org-agenda-menu-two-column
2632 (progn
2633 (setq n (length lines)
2634 n1 (+ (/ n 2) (mod n 2))
2635 right (nthcdr n1 lines)
2636 left (copy-sequence lines))
2637 (setcdr (nthcdr (1- n1) left) nil))
2638 (setq left lines right nil))
2639 (while left
2640 (insert "\n" (pop left))
2641 (when right
2642 (if (< (current-column) 40)
2643 (move-to-column 40 t)
2644 (insert " "))
2645 (insert (pop right))))
2647 ;; Make the window the right size
2648 (goto-char (point-min))
2649 (if second-time
2650 (if (not (pos-visible-in-window-p (point-max)))
2651 (org-fit-window-to-buffer))
2652 (setq second-time t)
2653 (org-fit-window-to-buffer))
2655 ;; Ask for selection
2656 (message "Press key for agenda command%s:"
2657 (if (or restrict-ok org-agenda-overriding-restriction)
2658 (if org-agenda-overriding-restriction
2659 " (restriction lock active)"
2660 (if restriction
2661 (format " (restricted to %s)" restriction)
2662 " (unrestricted)"))
2663 ""))
2664 (setq c (read-char-exclusive))
2665 (message "")
2666 (cond
2667 ((assoc (char-to-string c) custom)
2668 (setq selstring (concat selstring (char-to-string c)))
2669 (throw 'exit (cons selstring restriction)))
2670 ((memq c prefixes)
2671 (setq selstring (concat selstring (char-to-string c))
2672 prefixes nil
2673 rmheader (or rmheader t)
2674 custom (delq nil (mapcar
2675 (lambda (x)
2676 (if (or (= (length (car x)) 1)
2677 (/= (string-to-char (car x)) c))
2679 (cons (substring (car x) 1) (cdr x))))
2680 custom))))
2681 ((eq c ?*)
2682 (call-interactively 'org-toggle-sticky-agenda)
2683 (sit-for 2))
2684 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2685 (message "Restriction is only possible in Org-mode buffers")
2686 (ding) (sit-for 1))
2687 ((eq c ?1)
2688 (org-agenda-remove-restriction-lock 'noupdate)
2689 (setq restriction 'buffer))
2690 ((eq c ?0)
2691 (org-agenda-remove-restriction-lock 'noupdate)
2692 (setq restriction (if region-p 'region 'subtree)))
2693 ((eq c ?<)
2694 (org-agenda-remove-restriction-lock 'noupdate)
2695 (setq restriction
2696 (cond
2697 ((eq restriction 'buffer)
2698 (if region-p 'region 'subtree))
2699 ((memq restriction '(subtree region))
2700 nil)
2701 (t 'buffer))))
2702 ((eq c ?>)
2703 (org-agenda-remove-restriction-lock 'noupdate)
2704 (setq restriction nil))
2705 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2706 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2707 ((and (> (length selstring) 0) (eq c ?\d))
2708 (delete-window)
2709 (org-agenda-get-restriction-and-command prefix-descriptions))
2711 ((equal c ?q) (error "Abort"))
2712 (t (error "Invalid key %c" c))))))))
2714 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2715 (defvar org-agenda-last-arguments nil
2716 "The arguments of the previous call to `org-agenda'.")
2717 (defun org-agenda-run-series (name series)
2718 (org-let (nth 1 series) '(org-prepare-agenda name))
2719 ;; We need to reset agenda markers here, because when constructing a
2720 ;; block agenda, the individual blocks do not do that.
2721 (org-agenda-reset-markers)
2722 (let* ((org-agenda-multi t)
2723 (redo (list 'org-agenda-run-series name (list 'quote series)))
2724 (org-agenda-overriding-arguments
2725 (or org-agenda-overriding-arguments
2726 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2727 (get 'org-agenda-redo-command 'last-args))))
2728 (cmds (car series))
2729 (gprops (nth 1 series))
2730 match ;; The byte compiler incorrectly complains about this. Keep it!
2731 cmd type lprops)
2732 (while (setq cmd (pop cmds))
2733 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2734 (cond
2735 ((eq type 'agenda)
2736 (org-let2 gprops lprops
2737 '(call-interactively 'org-agenda-list)))
2738 ((eq type 'alltodo)
2739 (org-let2 gprops lprops
2740 '(call-interactively 'org-todo-list)))
2741 ((eq type 'search)
2742 (org-let2 gprops lprops
2743 '(org-search-view current-prefix-arg match nil)))
2744 ((eq type 'stuck)
2745 (org-let2 gprops lprops
2746 '(call-interactively 'org-agenda-list-stuck-projects)))
2747 ((eq type 'tags)
2748 (org-let2 gprops lprops
2749 '(org-tags-view current-prefix-arg match)))
2750 ((eq type 'tags-todo)
2751 (org-let2 gprops lprops
2752 '(org-tags-view '(4) match)))
2753 ((eq type 'todo)
2754 (org-let2 gprops lprops
2755 '(org-todo-list match)))
2756 ((fboundp type)
2757 (org-let2 gprops lprops
2758 '(funcall type match)))
2759 (t (error "Invalid type in command series"))))
2760 (widen)
2761 (setq org-agenda-redo-command redo)
2762 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2763 (goto-char (point-min)))
2764 (org-fit-agenda-window)
2765 (org-let (nth 1 series) '(org-finalize-agenda)))
2767 ;;;###autoload
2768 (defmacro org-batch-agenda (cmd-key &rest parameters)
2769 "Run an agenda command in batch mode and send the result to STDOUT.
2770 If CMD-KEY is a string of length 1, it is used as a key in
2771 `org-agenda-custom-commands' and triggers this command. If it is a
2772 longer string it is used as a tags/todo match string.
2773 Parameters are alternating variable names and values that will be bound
2774 before running the agenda command."
2775 (org-eval-in-environment (org-make-parameter-alist parameters)
2776 (if (> (length cmd-key) 2)
2777 (org-tags-view nil cmd-key)
2778 (org-agenda nil cmd-key)))
2779 (set-buffer org-agenda-buffer-name)
2780 (princ (buffer-string)))
2781 (def-edebug-spec org-batch-agenda (form &rest sexp))
2783 (defvar org-agenda-info nil)
2785 ;;;###autoload
2786 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2787 "Run an agenda command in batch mode and send the result to STDOUT.
2788 If CMD-KEY is a string of length 1, it is used as a key in
2789 `org-agenda-custom-commands' and triggers this command. If it is a
2790 longer string it is used as a tags/todo match string.
2791 Parameters are alternating variable names and values that will be bound
2792 before running the agenda command.
2794 The output gives a line for each selected agenda item. Each
2795 item is a list of comma-separated values, like this:
2797 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2799 category The category of the item
2800 head The headline, without TODO kwd, TAGS and PRIORITY
2801 type The type of the agenda entry, can be
2802 todo selected in TODO match
2803 tagsmatch selected in tags match
2804 diary imported from diary
2805 deadline a deadline on given date
2806 scheduled scheduled on given date
2807 timestamp entry has timestamp on given date
2808 closed entry was closed on given date
2809 upcoming-deadline warning about deadline
2810 past-scheduled forwarded scheduled item
2811 block entry has date block including g. date
2812 todo The todo keyword, if any
2813 tags All tags including inherited ones, separated by colons
2814 date The relevant date, like 2007-2-14
2815 time The time, like 15:00-16:50
2816 extra Sting with extra planning info
2817 priority-l The priority letter if any was given
2818 priority-n The computed numerical priority
2819 agenda-day The day in the agenda where this is listed"
2820 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2821 (org-make-parameter-alist parameters))
2822 (if (> (length cmd-key) 2)
2823 (org-tags-view nil cmd-key)
2824 (org-agenda nil cmd-key)))
2825 (set-buffer org-agenda-buffer-name)
2826 (let* ((lines (org-split-string (buffer-string) "\n"))
2827 line)
2828 (while (setq line (pop lines))
2829 (catch 'next
2830 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2831 (setq org-agenda-info
2832 (org-fix-agenda-info (text-properties-at 0 line)))
2833 (princ
2834 (mapconcat 'org-agenda-export-csv-mapper
2835 '(org-category txt type todo tags date time extra
2836 priority-letter priority agenda-day)
2837 ","))
2838 (princ "\n")))))
2839 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2841 (defun org-fix-agenda-info (props)
2842 "Make sure all properties on an agenda item have a canonical form.
2843 This ensures the export commands can easily use it."
2844 (let (tmp re)
2845 (when (setq tmp (plist-get props 'tags))
2846 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2847 (when (setq tmp (plist-get props 'date))
2848 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2849 (let ((calendar-date-display-form '(year "-" month "-" day)))
2850 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2852 (setq tmp (calendar-date-string tmp)))
2853 (setq props (plist-put props 'date tmp)))
2854 (when (setq tmp (plist-get props 'day))
2855 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2856 (let ((calendar-date-display-form '(year "-" month "-" day)))
2857 (setq tmp (calendar-date-string tmp)))
2858 (setq props (plist-put props 'day tmp))
2859 (setq props (plist-put props 'agenda-day tmp)))
2860 (when (setq tmp (plist-get props 'txt))
2861 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2862 (plist-put props 'priority-letter (match-string 1 tmp))
2863 (setq tmp (replace-match "" t t tmp)))
2864 (when (and (setq re (plist-get props 'org-todo-regexp))
2865 (setq re (concat "\\`\\.*" re " ?"))
2866 (string-match re tmp))
2867 (plist-put props 'todo (match-string 1 tmp))
2868 (setq tmp (replace-match "" t t tmp)))
2869 (plist-put props 'txt tmp)))
2870 props)
2872 (defun org-agenda-export-csv-mapper (prop)
2873 (let ((res (plist-get org-agenda-info prop)))
2874 (setq res
2875 (cond
2876 ((not res) "")
2877 ((stringp res) res)
2878 (t (prin1-to-string res))))
2879 (while (string-match "," res)
2880 (setq res (replace-match ";" t t res)))
2881 (org-trim res)))
2884 ;;;###autoload
2885 (defun org-store-agenda-views (&rest parameters)
2886 (interactive)
2887 (eval (list 'org-batch-store-agenda-views)))
2889 ;;;###autoload
2890 (defmacro org-batch-store-agenda-views (&rest parameters)
2891 "Run all custom agenda commands that have a file argument."
2892 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2893 (pop-up-frames nil)
2894 (dir default-directory)
2895 (pars (org-make-parameter-alist parameters))
2896 cmd thiscmdkey files opts cmd-or-set)
2897 (save-window-excursion
2898 (while cmds
2899 (setq cmd (pop cmds)
2900 thiscmdkey (car cmd)
2901 cmd-or-set (nth 2 cmd)
2902 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2903 files (nth (if (listp cmd-or-set) 4 5) cmd))
2904 (if (stringp files) (setq files (list files)))
2905 (when files
2906 (org-eval-in-environment (append org-agenda-exporter-settings
2907 opts pars)
2908 (org-agenda nil thiscmdkey))
2909 (set-buffer org-agenda-buffer-name)
2910 (while files
2911 (org-eval-in-environment (append org-agenda-exporter-settings
2912 opts pars)
2913 (org-agenda-write (expand-file-name (pop files) dir) nil t)))
2914 (and (get-buffer org-agenda-buffer-name)
2915 (kill-buffer org-agenda-buffer-name)))))))
2916 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2918 (defun org-agenda-mark-header-line (pos)
2919 "Mark the line at POS as an agenda structure header."
2920 (save-excursion
2921 (goto-char pos)
2922 (put-text-property (point-at-bol) (point-at-eol)
2923 'org-agenda-structural-header t)
2924 (when org-agenda-title-append
2925 (put-text-property (point-at-bol) (point-at-eol)
2926 'org-agenda-title-append org-agenda-title-append))))
2928 (defvar org-mobile-creating-agendas)
2929 (defvar org-agenda-write-buffer-name "Agenda View")
2930 (defun org-agenda-write (file &optional open nosettings)
2931 "Write the current buffer (an agenda view) as a file.
2932 Depending on the extension of the file name, plain text (.txt),
2933 HTML (.html or .htm) or Postscript (.ps) is produced.
2934 If the extension is .ics, run icalendar export over all files used
2935 to construct the agenda and limit the export to entries listed in the
2936 agenda now.
2937 With prefix argument OPEN, open the new file immediately.
2938 If NOSETTINGS is given, do not scope the settings of
2939 `org-agenda-exporter-settings' into the export commands. This is used when
2940 the settings have already been scoped and we do not wish to overrule other,
2941 higher priority settings."
2942 (interactive "FWrite agenda to file: \nP")
2943 (if (not (file-writable-p file))
2944 (error "Cannot write agenda to file %s" file))
2945 (org-let (if nosettings nil org-agenda-exporter-settings)
2946 '(save-excursion
2947 (save-window-excursion
2948 (org-agenda-mark-filtered-text)
2949 (let ((bs (copy-sequence (buffer-string))) beg)
2950 (org-agenda-unmark-filtered-text)
2951 (with-temp-buffer
2952 (rename-buffer org-agenda-write-buffer-name t)
2953 (set-buffer-modified-p nil)
2954 (insert bs)
2955 (org-agenda-remove-marked-text 'org-filtered)
2956 (while (setq beg (text-property-any (point-min) (point-max)
2957 'org-filtered t))
2958 (delete-region
2959 beg (or (next-single-property-change beg 'org-filtered)
2960 (point-max))))
2961 (run-hooks 'org-agenda-before-write-hook)
2962 (cond
2963 ((org-bound-and-true-p org-mobile-creating-agendas)
2964 (org-mobile-write-agenda-for-mobile file))
2965 ((string-match "\\.html?\\'" file)
2966 (require 'htmlize)
2967 (set-buffer (htmlize-buffer (current-buffer)))
2969 (when (and org-agenda-export-html-style
2970 (string-match "<style>" org-agenda-export-html-style))
2971 ;; replace <style> section with org-agenda-export-html-style
2972 (goto-char (point-min))
2973 (kill-region (- (search-forward "<style") 6)
2974 (search-forward "</style>"))
2975 (insert org-agenda-export-html-style))
2976 (write-file file)
2977 (kill-buffer (current-buffer))
2978 (message "HTML written to %s" file))
2979 ((string-match "\\.ps\\'" file)
2980 (require 'ps-print)
2981 (ps-print-buffer-with-faces file)
2982 (message "Postscript written to %s" file))
2983 ((string-match "\\.pdf\\'" file)
2984 (require 'ps-print)
2985 (ps-print-buffer-with-faces
2986 (concat (file-name-sans-extension file) ".ps"))
2987 (call-process "ps2pdf" nil nil nil
2988 (expand-file-name
2989 (concat (file-name-sans-extension file) ".ps"))
2990 (expand-file-name file))
2991 (delete-file (concat (file-name-sans-extension file) ".ps"))
2992 (message "PDF written to %s" file))
2993 ((string-match "\\.ics\\'" file)
2994 (require 'org-icalendar)
2995 (let ((org-agenda-marker-table
2996 (org-create-marker-find-array
2997 (org-agenda-collect-markers)))
2998 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2999 (org-combined-agenda-icalendar-file file))
3000 (apply 'org-export-icalendar 'combine
3001 (org-agenda-files nil 'ifmode))))
3003 (let ((bs (buffer-string)))
3004 (find-file file)
3005 (erase-buffer)
3006 (insert bs)
3007 (save-buffer 0)
3008 (kill-buffer (current-buffer))
3009 (message "Plain text written to %s" file))))))))
3010 (set-buffer org-agenda-buffer-name))
3011 (when open (org-open-file file)))
3013 (defvar org-agenda-tag-filter-overlays nil)
3014 (defvar org-agenda-cat-filter-overlays nil)
3016 (defun org-agenda-mark-filtered-text ()
3017 "Mark all text hidden by filtering with a text property."
3018 (let ((inhibit-read-only t))
3019 (mapc
3020 (lambda (o)
3021 (when (equal (overlay-buffer o) (current-buffer))
3022 (put-text-property
3023 (overlay-start o) (overlay-end o)
3024 'org-filtered t)))
3025 (append org-agenda-tag-filter-overlays
3026 org-agenda-cat-filter-overlays))))
3028 (defun org-agenda-unmark-filtered-text ()
3029 "Remove the filtering text property."
3030 (let ((inhibit-read-only t))
3031 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3033 (defun org-agenda-remove-marked-text (property &optional value)
3034 "Delete all text marked with VALUE of PROPERTY.
3035 VALUE defaults to t."
3036 (let (beg)
3037 (setq value (or value t))
3038 (while (setq beg (text-property-any (point-min) (point-max)
3039 property value))
3040 (delete-region
3041 beg (or (next-single-property-change beg 'org-filtered)
3042 (point-max))))))
3044 (defun org-agenda-add-entry-text ()
3045 "Add entry text to agenda lines.
3046 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3047 entry text following headings shown in the agenda.
3048 Drawers will be excluded, also the line with scheduling/deadline info."
3049 (when (and (> org-agenda-add-entry-text-maxlines 0)
3050 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3051 (let (m txt)
3052 (goto-char (point-min))
3053 (while (not (eobp))
3054 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3055 (beginning-of-line 2)
3056 (setq txt (org-agenda-get-some-entry-text
3057 m org-agenda-add-entry-text-maxlines " > "))
3058 (end-of-line 1)
3059 (if (string-match "\\S-" txt)
3060 (insert "\n" txt)
3061 (or (eobp) (forward-char 1))))))))
3063 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3064 &rest keep)
3065 "Extract entry text from MARKER, at most N-LINES lines.
3066 This will ignore drawers etc, just get the text.
3067 If INDENT is given, prefix every line with this string. If KEEP is
3068 given, it is a list of symbols, defining stuff that should not be
3069 removed from the entry content. Currently only `planning' is allowed here."
3070 (let (txt drawer-re kwd-time-re ind)
3071 (save-excursion
3072 (with-current-buffer (marker-buffer marker)
3073 (if (not (derived-mode-p 'org-mode))
3074 (setq txt "")
3075 (save-excursion
3076 (save-restriction
3077 (widen)
3078 (goto-char marker)
3079 (end-of-line 1)
3080 (setq txt (buffer-substring
3081 (min (1+ (point)) (point-max))
3082 (progn (outline-next-heading) (point)))
3083 drawer-re org-drawer-regexp
3084 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3085 ".*\n?"))
3086 (with-temp-buffer
3087 (insert txt)
3088 (when org-agenda-add-entry-text-descriptive-links
3089 (goto-char (point-min))
3090 (while (org-activate-bracket-links (point-max))
3091 (add-text-properties (match-beginning 0) (match-end 0)
3092 '(face org-link))))
3093 (goto-char (point-min))
3094 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3095 (set-text-properties (match-beginning 0) (match-end 0)
3096 nil))
3097 (goto-char (point-min))
3098 (while (re-search-forward drawer-re nil t)
3099 (delete-region
3100 (match-beginning 0)
3101 (progn (re-search-forward
3102 "^[ \t]*:END:.*\n?" nil 'move)
3103 (point))))
3104 (unless (member 'planning keep)
3105 (goto-char (point-min))
3106 (while (re-search-forward kwd-time-re nil t)
3107 (replace-match "")))
3108 (goto-char (point-min))
3109 (when org-agenda-entry-text-exclude-regexps
3110 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3111 (while (setq re (pop re-list))
3112 (goto-char (point-min))
3113 (while (re-search-forward re nil t)
3114 (replace-match "")))))
3115 (goto-char (point-max))
3116 (skip-chars-backward " \t\n")
3117 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3119 ;; find and remove min common indentation
3120 (goto-char (point-min))
3121 (untabify (point-min) (point-max))
3122 (setq ind (org-get-indentation))
3123 (while (not (eobp))
3124 (unless (looking-at "[ \t]*$")
3125 (setq ind (min ind (org-get-indentation))))
3126 (beginning-of-line 2))
3127 (goto-char (point-min))
3128 (while (not (eobp))
3129 (unless (looking-at "[ \t]*$")
3130 (move-to-column ind)
3131 (delete-region (point-at-bol) (point)))
3132 (beginning-of-line 2))
3134 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3136 (goto-char (point-min))
3137 (when indent
3138 (while (and (not (eobp)) (re-search-forward "^" nil t))
3139 (replace-match indent t t)))
3140 (goto-char (point-min))
3141 (while (looking-at "[ \t]*\n") (replace-match ""))
3142 (goto-char (point-max))
3143 (when (> (org-current-line)
3144 n-lines)
3145 (org-goto-line (1+ n-lines))
3146 (backward-char 1))
3147 (setq txt (buffer-substring (point-min) (point)))))))))
3148 txt))
3150 (defun org-agenda-collect-markers ()
3151 "Collect the markers pointing to entries in the agenda buffer."
3152 (let (m markers)
3153 (save-excursion
3154 (goto-char (point-min))
3155 (while (not (eobp))
3156 (when (setq m (or (org-get-at-bol 'org-hd-marker)
3157 (org-get-at-bol 'org-marker)))
3158 (push m markers))
3159 (beginning-of-line 2)))
3160 (nreverse markers)))
3162 (defun org-create-marker-find-array (marker-list)
3163 "Create a alist of files names with all marker positions in that file."
3164 (let (f tbl m a p)
3165 (while (setq m (pop marker-list))
3166 (setq p (marker-position m)
3167 f (buffer-file-name (or (buffer-base-buffer
3168 (marker-buffer m))
3169 (marker-buffer m))))
3170 (if (setq a (assoc f tbl))
3171 (push (marker-position m) (cdr a))
3172 (push (list f p) tbl)))
3173 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3174 tbl)))
3176 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3177 (defun org-check-agenda-marker-table ()
3178 "Check of the current entry is on the marker list."
3179 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3181 (and (setq a (assoc file org-agenda-marker-table))
3182 (save-match-data
3183 (save-excursion
3184 (org-back-to-heading t)
3185 (member (point) (cdr a)))))))
3187 (defun org-check-for-org-mode ()
3188 "Make sure current buffer is in org-mode. Error if not."
3189 (or (derived-mode-p 'org-mode)
3190 (error "Cannot execute org-mode agenda command on buffer in %s"
3191 major-mode)))
3193 (defun org-fit-agenda-window ()
3194 "Fit the window to the buffer size."
3195 (and (memq org-agenda-window-setup '(reorganize-frame))
3196 (fboundp 'fit-window-to-buffer)
3197 (org-fit-window-to-buffer
3199 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3200 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3202 ;;; Agenda prepare and finalize
3204 (defvar org-agenda-multi nil) ; dynamically scoped
3205 (defvar org-agenda-buffer-name "*Org Agenda*")
3206 (defvar org-pre-agenda-window-conf nil)
3207 (defvar org-agenda-columns-active nil)
3208 (defvar org-agenda-name nil)
3209 (defvar org-agenda-tag-filter nil)
3210 (defvar org-agenda-category-filter nil)
3211 (defvar org-agenda-top-category-filter nil)
3212 (defvar org-agenda-tag-filter-while-redo nil)
3213 (defvar org-agenda-tag-filter-preset nil
3214 "A preset of the tags filter used for secondary agenda filtering.
3215 This must be a list of strings, each string must be a single tag preceded
3216 by \"+\" or \"-\".
3217 This variable should not be set directly, but agenda custom commands can
3218 bind it in the options section. The preset filter is a global property of
3219 the entire agenda view. In a block agenda, it will not work reliably to
3220 define a filter for one of the individual blocks. You need to set it in
3221 the global options and expect it to be applied to the entire view.")
3223 (defvar org-agenda-category-filter-preset nil
3224 "A preset of the category filter used for secondary agenda filtering.
3225 This must be a list of strings, each string must be a single category
3226 preceded by \"+\" or \"-\".
3227 This variable should not be set directly, but agenda custom commands can
3228 bind it in the options section. The preset filter is a global property of
3229 the entire agenda view. In a block agenda, it will not work reliably to
3230 define a filter for one of the individual blocks. You need to set it in
3231 the global options and expect it to be applied to the entire view.")
3234 (defun org-agenda-use-sticky-p ()
3235 "Return non-NIL if existing agenda buffer named
3236 `org-agenda-buffer-name' exists, and should be shown instead of
3237 generating a new one"
3238 (and
3239 ;; turned off by user
3240 org-agenda-sticky
3241 ;; For multi-agenda buffer already exists
3242 (not org-agenda-multi)
3243 ;; buffer found
3244 (get-buffer org-agenda-buffer-name)
3245 ;; C-u parameter is same as last call
3246 (with-current-buffer (get-buffer org-agenda-buffer-name)
3247 (and
3248 (equal current-prefix-arg
3249 org-agenda-last-prefix-arg)
3250 ;; In case user turned stickiness on, while having existing
3251 ;; Agenda buffer active, don't reuse that buffer, because it
3252 ;; does not have org variables local
3253 org-agenda-this-buffer-is-sticky))))
3255 (defun org-prepare-agenda-window (abuf)
3256 "Setup agenda buffer in the window"
3257 (let* ((awin (get-buffer-window abuf))
3258 wconf)
3259 (cond
3260 ((equal (current-buffer) abuf) nil)
3261 (awin (select-window awin))
3262 ((not (setq wconf (current-window-configuration))))
3263 ((equal org-agenda-window-setup 'current-window)
3264 (org-pop-to-buffer-same-window abuf))
3265 ((equal org-agenda-window-setup 'other-window)
3266 (org-switch-to-buffer-other-window abuf))
3267 ((equal org-agenda-window-setup 'other-frame)
3268 (switch-to-buffer-other-frame abuf))
3269 ((equal org-agenda-window-setup 'reorganize-frame)
3270 (delete-other-windows)
3271 (org-switch-to-buffer-other-window abuf)))
3272 ;; additional test in case agenda is invoked from within agenda
3273 ;; buffer via elisp link
3274 (unless (equal (current-buffer) abuf)
3275 (org-pop-to-buffer-same-window abuf))
3276 (setq org-pre-agenda-window-conf wconf)))
3278 (defun org-prepare-agenda (&optional name)
3279 (if (org-agenda-use-sticky-p)
3280 (progn
3281 ;; Popup existing buffer
3282 (org-prepare-agenda-window (get-buffer org-agenda-buffer-name))
3283 (message "Sticky Agenda buffer, use `r' to refresh")
3284 (throw 'exit nil))
3285 (setq org-todo-keywords-for-agenda nil)
3286 (setq org-drawers-for-agenda nil)
3287 (unless org-agenda-persistent-filter
3288 (setq org-agenda-tag-filter nil
3289 org-agenda-category-filter nil))
3290 (put 'org-agenda-tag-filter :preset-filter org-agenda-tag-filter-preset)
3291 (put 'org-agenda-category-filter :preset-filter
3292 org-agenda-category-filter-preset)
3293 (if org-agenda-multi
3294 (progn
3295 (setq buffer-read-only nil)
3296 (goto-char (point-max))
3297 (unless (or (bobp) org-agenda-compact-blocks
3298 (not org-agenda-block-separator))
3299 (insert "\n"
3300 (if (stringp org-agenda-block-separator)
3301 org-agenda-block-separator
3302 (make-string (window-width) org-agenda-block-separator))
3303 "\n"))
3304 (narrow-to-region (point) (point-max)))
3305 (setq org-done-keywords-for-agenda nil)
3307 ;; Setting any org variables that are in org-agenda-local-vars
3308 ;; list need to be done after the prepare call
3309 (org-prepare-agenda-window (get-buffer-create org-agenda-buffer-name))
3310 (setq buffer-read-only nil)
3311 (org-agenda-reset-markers)
3312 (let ((inhibit-read-only t)) (erase-buffer))
3313 (org-agenda-mode)
3314 (setq org-agenda-buffer (current-buffer))
3315 (setq org-agenda-contributing-files nil)
3316 (setq org-agenda-columns-active nil)
3317 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3318 (setq org-todo-keywords-for-agenda
3319 (org-uniquify org-todo-keywords-for-agenda))
3320 (setq org-done-keywords-for-agenda
3321 (org-uniquify org-done-keywords-for-agenda))
3322 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3323 (setq org-agenda-last-prefix-arg current-prefix-arg)
3324 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3325 (and name (not org-agenda-name)
3326 (org-set-local 'org-agenda-name name)))
3327 (setq buffer-read-only nil)))
3329 (defun org-finalize-agenda ()
3330 "Finishing touch for the agenda buffer, called just before displaying it."
3331 (unless org-agenda-multi
3332 (save-excursion
3333 (let ((inhibit-read-only t))
3334 (goto-char (point-min))
3335 (while (org-activate-bracket-links (point-max))
3336 (add-text-properties (match-beginning 0) (match-end 0)
3337 '(face org-link)))
3338 (org-agenda-align-tags)
3339 (unless org-agenda-with-colors
3340 (remove-text-properties (point-min) (point-max) '(face nil))))
3341 (if (and (boundp 'org-agenda-overriding-columns-format)
3342 org-agenda-overriding-columns-format)
3343 (org-set-local 'org-agenda-overriding-columns-format
3344 org-agenda-overriding-columns-format))
3345 (if (and (boundp 'org-agenda-view-columns-initially)
3346 org-agenda-view-columns-initially)
3347 (org-agenda-columns))
3348 (when org-agenda-fontify-priorities
3349 (org-agenda-fontify-priorities))
3350 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3351 (org-agenda-dim-blocked-tasks))
3352 (org-agenda-mark-clocking-task)
3353 (when org-agenda-entry-text-mode
3354 (org-agenda-entry-text-hide)
3355 (org-agenda-entry-text-show))
3356 (if (functionp 'org-habit-insert-consistency-graphs)
3357 (org-habit-insert-consistency-graphs))
3358 (run-hooks 'org-finalize-agenda-hook)
3359 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3360 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3361 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3362 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3363 (org-agenda-filter-apply org-agenda-category-filter 'category))
3364 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)
3367 (defun org-agenda-mark-clocking-task ()
3368 "Mark the current clock entry in the agenda if it is present."
3369 (mapc (lambda (o)
3370 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3371 (delete-overlay o)))
3372 (overlays-in (point-min) (point-max)))
3373 (when (marker-buffer org-clock-hd-marker)
3374 (save-excursion
3375 (goto-char (point-min))
3376 (let (s ov)
3377 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3378 (goto-char s)
3379 (when (equal (org-get-at-bol 'org-hd-marker)
3380 org-clock-hd-marker)
3381 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3382 (overlay-put ov 'type 'org-agenda-clocking)
3383 (overlay-put ov 'face 'org-agenda-clocking)
3384 (overlay-put ov 'help-echo
3385 "The clock is running in this item")))))))
3387 (defun org-agenda-fontify-priorities ()
3388 "Make highest priority lines bold, and lowest italic."
3389 (interactive)
3390 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3391 (delete-overlay o)))
3392 (overlays-in (point-min) (point-max)))
3393 (save-excursion
3394 (let ((inhibit-read-only t)
3395 b e p ov h l)
3396 (goto-char (point-min))
3397 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3398 (setq h (or (get-char-property (point) 'org-highest-priority)
3399 org-highest-priority)
3400 l (or (get-char-property (point) 'org-lowest-priority)
3401 org-lowest-priority)
3402 p (string-to-char (match-string 1))
3403 b (match-beginning 0)
3404 e (if (eq org-agenda-fontify-priorities 'cookies)
3405 (match-end 0)
3406 (point-at-eol))
3407 ov (make-overlay b e))
3408 (overlay-put
3409 ov 'face
3410 (cond ((org-face-from-face-or-color
3411 'priority nil
3412 (cdr (assoc p org-priority-faces))))
3413 ((and (listp org-agenda-fontify-priorities)
3414 (org-face-from-face-or-color
3415 'priority nil
3416 (cdr (assoc p org-agenda-fontify-priorities)))))
3417 ((equal p l) 'italic)
3418 ((equal p h) 'bold)))
3419 (overlay-put ov 'org-type 'org-priority)))))
3421 (defun org-agenda-dim-blocked-tasks ()
3422 "Dim currently blocked TODO's in the agenda display."
3423 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3424 (delete-overlay o)))
3425 (overlays-in (point-min) (point-max)))
3426 (save-excursion
3427 (let ((inhibit-read-only t)
3428 (org-depend-tag-blocked nil)
3429 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3430 org-blocked-by-checkboxes
3431 invis1 b e p ov h l)
3432 (goto-char (point-min))
3433 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3434 (and pos (goto-char (1+ pos))))
3435 (setq org-blocked-by-checkboxes nil invis1 invis)
3436 (let ((marker (org-get-at-bol 'org-hd-marker)))
3437 (when (and marker
3438 (with-current-buffer (marker-buffer marker)
3439 (save-excursion (goto-char marker)
3440 (org-entry-blocked-p))))
3441 (if org-blocked-by-checkboxes (setq invis1 nil))
3442 (setq b (if invis1
3443 (max (point-min) (1- (point-at-bol)))
3444 (point-at-bol))
3445 e (point-at-eol)
3446 ov (make-overlay b e))
3447 (if invis1
3448 (overlay-put ov 'invisible t)
3449 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3450 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3452 (defvar org-agenda-skip-function nil
3453 "Function to be called at each match during agenda construction.
3454 If this function returns nil, the current match should not be skipped.
3455 Otherwise, the function must return a position from where the search
3456 should be continued.
3457 This may also be a Lisp form, it will be evaluated.
3458 Never set this variable using `setq' or so, because then it will apply
3459 to all future agenda commands. If you do want a global skipping condition,
3460 use the option `org-agenda-skip-function-global' instead.
3461 The correct usage for `org-agenda-skip-function' is to bind it with
3462 `let' to scope it dynamically into the agenda-constructing command.
3463 A good way to set it is through options in `org-agenda-custom-commands'.")
3465 (defun org-agenda-skip ()
3466 "Throw to `:skip' in places that should be skipped.
3467 Also moves point to the end of the skipped region, so that search can
3468 continue from there."
3469 (let ((p (point-at-bol)) to)
3470 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3471 (get-text-property p :org-archived)
3472 (org-end-of-subtree t)
3473 (throw :skip t))
3474 (and org-agenda-skip-comment-trees
3475 (get-text-property p :org-comment)
3476 (org-end-of-subtree t)
3477 (throw :skip t))
3478 (if (equal (char-after p) ?#) (throw :skip t))
3479 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3480 (org-agenda-skip-eval org-agenda-skip-function)))
3481 (goto-char to)
3482 (throw :skip t))))
3484 (defun org-agenda-skip-eval (form)
3485 "If FORM is a function or a list, call (or eval) is and return result.
3486 `save-excursion' and `save-match-data' are wrapped around the call, so point
3487 and match data are returned to the previous state no matter what these
3488 functions do."
3489 (let (fp)
3490 (and form
3491 (or (setq fp (functionp form))
3492 (consp form))
3493 (save-excursion
3494 (save-match-data
3495 (if fp
3496 (funcall form)
3497 (eval form)))))))
3499 (defvar org-agenda-markers nil
3500 "List of all currently active markers created by `org-agenda'.")
3501 (defvar org-agenda-last-marker-time (org-float-time)
3502 "Creation time of the last agenda marker.")
3504 (defun org-agenda-new-marker (&optional pos)
3505 "Return a new agenda marker.
3506 Org-mode keeps a list of these markers and resets them when they are
3507 no longer in use."
3508 (let ((m (copy-marker (or pos (point)))))
3509 (setq org-agenda-last-marker-time (org-float-time))
3510 (if org-agenda-buffer
3511 (with-current-buffer org-agenda-buffer
3512 (push m org-agenda-markers))
3513 (push m org-agenda-markers))
3516 (defun org-agenda-reset-markers ()
3517 "Reset markers created by `org-agenda'."
3518 (while org-agenda-markers
3519 (move-marker (pop org-agenda-markers) nil)))
3521 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3522 "Save relative positions of markers in region.
3523 This check for agenda markers in all agenda buffers currently active."
3524 (dolist (buf (buffer-list))
3525 (with-current-buffer buf
3526 (when (eq major-mode 'org-agenda-mode)
3527 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3528 org-agenda-markers)))))
3530 ;;; Entry text mode
3532 (defun org-agenda-entry-text-show-here ()
3533 "Add some text from the entry as context to the current line."
3534 (let (m txt o)
3535 (setq m (org-get-at-bol 'org-hd-marker))
3536 (unless (marker-buffer m)
3537 (error "No marker points to an entry here"))
3538 (setq txt (concat "\n" (org-no-properties
3539 (org-agenda-get-some-entry-text
3540 m org-agenda-entry-text-maxlines " > "))))
3541 (when (string-match "\\S-" txt)
3542 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3543 (overlay-put o 'evaporate t)
3544 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3545 (overlay-put o 'after-string txt))))
3547 (defun org-agenda-entry-text-show ()
3548 "Add entry context for all agenda lines."
3549 (interactive)
3550 (save-excursion
3551 (goto-char (point-max))
3552 (beginning-of-line 1)
3553 (while (not (bobp))
3554 (when (org-get-at-bol 'org-hd-marker)
3555 (org-agenda-entry-text-show-here))
3556 (beginning-of-line 0))))
3558 (defun org-agenda-entry-text-hide ()
3559 "Remove any shown entry context."
3560 (delq nil
3561 (mapcar (lambda (o)
3562 (if (eq (overlay-get o 'org-overlay-type)
3563 'agenda-entry-content)
3564 (progn (delete-overlay o) t)))
3565 (overlays-in (point-min) (point-max)))))
3567 (defun org-agenda-get-day-face (date)
3568 "Return the face DATE should be displayed with."
3569 (or (and (functionp org-agenda-day-face-function)
3570 (funcall org-agenda-day-face-function date))
3571 (cond ((org-agenda-todayp date)
3572 'org-agenda-date-today)
3573 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3574 'org-agenda-date-weekend)
3575 (t 'org-agenda-date))))
3577 ;;; Agenda timeline
3579 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3581 (defun org-timeline (&optional dotodo)
3582 "Show a time-sorted view of the entries in the current org file.
3583 Only entries with a time stamp of today or later will be listed. With
3584 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3585 under the current date.
3586 If the buffer contains an active region, only check the region for
3587 dates."
3588 (interactive "P")
3589 (let* ((dopast t)
3590 (org-agenda-show-log-scoped org-agenda-show-log)
3591 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3592 (current-buffer))))
3593 (date (calendar-current-date))
3594 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3595 (end (if (org-region-active-p) (region-end) (point-max)))
3596 (day-numbers (org-get-all-dates
3597 beg end 'no-ranges
3598 t org-agenda-show-log-scoped ; always include today
3599 org-timeline-show-empty-dates))
3600 (org-deadline-warning-days 0)
3601 (org-agenda-only-exact-dates t)
3602 (today (org-today))
3603 (past t)
3604 args
3605 s e rtn d emptyp)
3606 (setq org-agenda-redo-command
3607 (list 'progn
3608 (list 'org-switch-to-buffer-other-window (current-buffer))
3609 (list 'org-timeline (list 'quote dotodo))))
3610 (if (not dopast)
3611 ;; Remove past dates from the list of dates.
3612 (setq day-numbers (delq nil (mapcar (lambda(x)
3613 (if (>= x today) x nil))
3614 day-numbers))))
3615 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3616 (org-compile-prefix-format 'timeline)
3617 (org-set-sorting-strategy 'timeline)
3618 (if org-agenda-show-log-scoped (push :closed args))
3619 (push :timestamp args)
3620 (push :deadline args)
3621 (push :scheduled args)
3622 (push :sexp args)
3623 (if dotodo (push :todo args))
3624 (insert "Timeline of file " entry "\n")
3625 (add-text-properties (point-min) (point)
3626 (list 'face 'org-agenda-structure))
3627 (org-agenda-mark-header-line (point-min))
3628 (while (setq d (pop day-numbers))
3629 (if (and (listp d) (eq (car d) :omitted))
3630 (progn
3631 (setq s (point))
3632 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3633 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3634 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3635 (if (and (>= d today)
3636 dopast
3637 past)
3638 (progn
3639 (setq past nil)
3640 (insert (make-string 79 ?-) "\n")))
3641 (setq date (calendar-gregorian-from-absolute d))
3642 (setq s (point))
3643 (setq rtn (and (not emptyp)
3644 (apply 'org-agenda-get-day-entries entry
3645 date args)))
3646 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3647 (progn
3648 (insert
3649 (if (stringp org-agenda-format-date)
3650 (format-time-string org-agenda-format-date
3651 (org-time-from-absolute date))
3652 (funcall org-agenda-format-date date))
3653 "\n")
3654 (put-text-property s (1- (point)) 'face
3655 (org-agenda-get-day-face date))
3656 (put-text-property s (1- (point)) 'org-date-line t)
3657 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3658 (if (equal d today)
3659 (put-text-property s (1- (point)) 'org-today t))
3660 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3661 (put-text-property s (1- (point)) 'day d)))))
3662 (goto-char (point-min))
3663 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3664 (point-min)))
3665 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3666 (org-finalize-agenda)
3667 (setq buffer-read-only t)))
3669 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3670 "Return a list of all relevant day numbers from BEG to END buffer positions.
3671 If NO-RANGES is non-nil, include only the start and end dates of a range,
3672 not every single day in the range. If FORCE-TODAY is non-nil, make
3673 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3674 inactive time stamps (those in square brackets) are included.
3675 When EMPTY is non-nil, also include days without any entries."
3676 (let ((re (concat
3677 (if pre-re pre-re "")
3678 (if inactive org-ts-regexp-both org-ts-regexp)))
3679 dates dates1 date day day1 day2 ts1 ts2 pos)
3680 (if force-today
3681 (setq dates (list (org-today))))
3682 (save-excursion
3683 (goto-char beg)
3684 (while (re-search-forward re end t)
3685 (setq day (time-to-days (org-time-string-to-time
3686 (substring (match-string 1) 0 10)
3687 (current-buffer) (match-beginning 0))))
3688 (or (memq day dates) (push day dates)))
3689 (unless no-ranges
3690 (goto-char beg)
3691 (while (re-search-forward org-tr-regexp end t)
3692 (setq pos (match-beginning 0))
3693 (setq ts1 (substring (match-string 1) 0 10)
3694 ts2 (substring (match-string 2) 0 10)
3695 day1 (time-to-days (org-time-string-to-time
3696 ts1 (current-buffer) pos))
3697 day2 (time-to-days (org-time-string-to-time
3698 ts2 (current-buffer) pos)))
3699 (while (< (setq day1 (1+ day1)) day2)
3700 (or (memq day1 dates) (push day1 dates)))))
3701 (setq dates (sort dates '<))
3702 (when empty
3703 (while (setq day (pop dates))
3704 (setq day2 (car dates))
3705 (push day dates1)
3706 (when (and day2 empty)
3707 (if (or (eq empty t)
3708 (and (numberp empty) (<= (- day2 day) empty)))
3709 (while (< (setq day (1+ day)) day2)
3710 (push (list day) dates1))
3711 (push (cons :omitted (- day2 day)) dates1))))
3712 (setq dates (nreverse dates1)))
3713 dates)))
3715 ;;; Agenda Daily/Weekly
3717 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3718 "Start day for the agenda view.
3719 Custom commands can set this variable in the options section.")
3720 (defvar org-starting-day nil) ; local variable in the agenda buffer
3721 (defvar org-agenda-current-span nil
3722 "The current span used in the agenda view.") ; local variable in the agenda buffer
3723 (defvar org-arg-loc nil) ; local variable
3725 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3726 "List of types searched for when creating the daily/weekly agenda.
3727 This variable is a list of symbols that controls the types of
3728 items that appear in the daily/weekly agenda. Allowed symbols in this
3729 list are are
3731 :timestamp List items containing a date stamp or date range matching
3732 the selected date. This includes sexp entries in
3733 angular brackets.
3735 :sexp List entries resulting from plain diary-like sexps.
3737 :deadline List deadline due on that date. When the date is today,
3738 also list any deadlines past due, or due within
3739 `org-deadline-warning-days'. `:deadline' must appear before
3740 `:scheduled' if the setting of
3741 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3742 any effect.
3744 :scheduled List all items which are scheduled for the given date.
3745 The diary for *today* also contains items which were
3746 scheduled earlier and are not yet marked DONE.
3748 By default, all four types are turned on.
3750 Never set this variable globally using `setq', because then it
3751 will apply to all future agenda commands. Instead, bind it with
3752 `let' to scope it dynamically into the agenda-constructing
3753 command. A good way to set it is through options in
3754 `org-agenda-custom-commands'. For a more flexible (though
3755 somewhat less efficient) way of determining what is included in
3756 the daily/weekly agenda, see `org-agenda-skip-function'.")
3758 ;;;###autoload
3759 (defun org-agenda-list (&optional arg start-day span)
3760 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3761 The view will be for the current day or week, but from the overview buffer
3762 you will be able to go to other days/weeks.
3764 With a numeric prefix argument in an interactive call, the agenda will
3765 span ARG days. Lisp programs should instead specify SPAN to change
3766 the number of days. SPAN defaults to `org-agenda-span'.
3768 START-DAY defaults to TODAY, or to the most recent match for the weekday
3769 given in `org-agenda-start-on-weekday'."
3770 (interactive "P")
3771 (if (and (integerp arg) (> arg 0))
3772 (setq span arg arg nil))
3773 (org-prepare-agenda "Day/Week")
3774 (setq start-day (or start-day org-agenda-start-day))
3775 (if org-agenda-overriding-arguments
3776 (setq arg (car org-agenda-overriding-arguments)
3777 start-day (nth 1 org-agenda-overriding-arguments)
3778 span (nth 2 org-agenda-overriding-arguments)))
3779 (if (stringp start-day)
3780 ;; Convert to an absolute day number
3781 (setq start-day (time-to-days (org-read-date nil t start-day))))
3782 (setq org-agenda-last-arguments (list arg start-day span))
3783 (org-compile-prefix-format 'agenda)
3784 (org-set-sorting-strategy 'agenda)
3785 (let* ((span (org-agenda-ndays-to-span
3786 (or span org-agenda-ndays org-agenda-span)))
3787 (today (org-today))
3788 (sd (or start-day today))
3789 (ndays (org-agenda-span-to-ndays span sd))
3790 (org-agenda-start-on-weekday
3791 (if (eq ndays 7)
3792 org-agenda-start-on-weekday))
3793 (thefiles (org-agenda-files nil 'ifmode))
3794 (files thefiles)
3795 (start (if (or (null org-agenda-start-on-weekday)
3796 (< ndays 7))
3798 (let* ((nt (calendar-day-of-week
3799 (calendar-gregorian-from-absolute sd)))
3800 (n1 org-agenda-start-on-weekday)
3801 (d (- nt n1)))
3802 (- sd (+ (if (< d 0) 7 0) d)))))
3803 (day-numbers (list start))
3804 (day-cnt 0)
3805 (inhibit-redisplay (not debug-on-error))
3806 (org-agenda-show-log-scoped org-agenda-show-log)
3807 s e rtn rtnall file date d start-pos end-pos todayp
3808 clocktable-start clocktable-end filter)
3809 (setq org-agenda-redo-command
3810 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3811 (dotimes (n (1- ndays))
3812 (push (1+ (car day-numbers)) day-numbers))
3813 (setq day-numbers (nreverse day-numbers))
3814 (setq clocktable-start (car day-numbers)
3815 clocktable-end (1+ (or (org-last day-numbers) 0)))
3816 (org-set-local 'org-starting-day (car day-numbers))
3817 (org-set-local 'org-arg-loc arg)
3818 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3819 (unless org-agenda-compact-blocks
3820 (let* ((d1 (car day-numbers))
3821 (d2 (org-last day-numbers))
3822 (w1 (org-days-to-iso-week d1))
3823 (w2 (org-days-to-iso-week d2)))
3824 (setq s (point))
3825 (if org-agenda-overriding-header
3826 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3827 nil 'face 'org-agenda-structure) "\n")
3828 (insert (org-agenda-span-name span)
3829 "-agenda"
3830 (if (< (- d2 d1) 350)
3831 (if (= w1 w2)
3832 (format " (W%02d)" w1)
3833 (format " (W%02d-W%02d)" w1 w2))
3835 ":\n")))
3836 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3837 'org-date-line t))
3838 (org-agenda-mark-header-line s))
3839 (while (setq d (pop day-numbers))
3840 (setq date (calendar-gregorian-from-absolute d)
3841 s (point))
3842 (if (or (setq todayp (= d today))
3843 (and (not start-pos) (= d sd)))
3844 (setq start-pos (point))
3845 (if (and start-pos (not end-pos))
3846 (setq end-pos (point))))
3847 (setq files thefiles
3848 rtnall nil)
3849 (while (setq file (pop files))
3850 (catch 'nextfile
3851 (org-check-agenda-file file)
3852 (let ((org-agenda-entry-types org-agenda-entry-types))
3853 (unless org-agenda-include-deadlines
3854 (setq org-agenda-entry-types
3855 (delq :deadline org-agenda-entry-types)))
3856 (cond
3857 ((memq org-agenda-show-log-scoped '(only clockcheck))
3858 (setq rtn (org-agenda-get-day-entries
3859 file date :closed)))
3860 (org-agenda-show-log-scoped
3861 (setq rtn (apply 'org-agenda-get-day-entries
3862 file date
3863 (append '(:closed) org-agenda-entry-types))))
3865 (setq rtn (apply 'org-agenda-get-day-entries
3866 file date
3867 org-agenda-entry-types)))))
3868 (setq rtnall (append rtnall rtn)))) ;; all entries
3869 (if org-agenda-include-diary
3870 (let ((org-agenda-search-headline-for-time t))
3871 (require 'diary-lib)
3872 (setq rtn (org-get-entries-from-diary date))
3873 (setq rtnall (append rtnall rtn))))
3874 (if (or rtnall org-agenda-show-all-dates)
3875 (progn
3876 (setq day-cnt (1+ day-cnt))
3877 (insert
3878 (if (stringp org-agenda-format-date)
3879 (format-time-string org-agenda-format-date
3880 (org-time-from-absolute date))
3881 (funcall org-agenda-format-date date))
3882 "\n")
3883 (put-text-property s (1- (point)) 'face
3884 (org-agenda-get-day-face date))
3885 (put-text-property s (1- (point)) 'org-date-line t)
3886 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3887 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3888 (when todayp
3889 (put-text-property s (1- (point)) 'org-today t))
3890 (if rtnall (insert ;; all entries
3891 (org-finalize-agenda-entries
3892 (org-agenda-add-time-grid-maybe
3893 rtnall ndays todayp))
3894 "\n"))
3895 (put-text-property s (1- (point)) 'day d)
3896 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3897 (when (and org-agenda-clockreport-mode clocktable-start)
3898 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3899 ;; the above line is to ensure the restricted range!
3900 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3901 tbl)
3902 (setq p (org-plist-delete p :block))
3903 (setq p (plist-put p :tstart clocktable-start))
3904 (setq p (plist-put p :tend clocktable-end))
3905 (setq p (plist-put p :scope 'agenda))
3906 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3907 (setq filter (or org-agenda-tag-filter-while-redo
3908 (get 'org-agenda-tag-filter :preset-filter))))
3909 (setq p (plist-put p :tags (mapconcat (lambda (x)
3910 (if (string-match "[<>=]" x)
3913 filter ""))))
3914 (setq tbl (apply 'org-get-clocktable p))
3915 (insert tbl)))
3916 (goto-char (point-min))
3917 (or org-agenda-multi (org-fit-agenda-window))
3918 (unless (and (pos-visible-in-window-p (point-min))
3919 (pos-visible-in-window-p (point-max)))
3920 (goto-char (1- (point-max)))
3921 (recenter -1)
3922 (if (not (pos-visible-in-window-p (or start-pos 1)))
3923 (progn
3924 (goto-char (or start-pos 1))
3925 (recenter 1))))
3926 (goto-char (or start-pos 1))
3927 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3928 (if (eq org-agenda-show-log-scoped 'clockcheck)
3929 (org-agenda-show-clocking-issues))
3930 (org-finalize-agenda)
3931 (setq buffer-read-only t)
3932 (message "")))
3934 (defun org-agenda-ndays-to-span (n)
3935 "Return a span symbol for a span of N days, or N if none matches."
3936 (cond ((symbolp n) n)
3937 ((= n 1) 'day)
3938 ((= n 7) 'week)
3939 (t n)))
3941 (defun org-agenda-span-to-ndays (span start-day)
3942 "Return ndays from SPAN starting at START-DAY."
3943 (cond ((numberp span) span)
3944 ((eq span 'day) 1)
3945 ((eq span 'week) 7)
3946 ((eq span 'month)
3947 (let ((date (calendar-gregorian-from-absolute start-day)))
3948 (calendar-last-day-of-month (car date) (caddr date))))
3949 ((eq span 'year)
3950 (let ((date (calendar-gregorian-from-absolute start-day)))
3951 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3953 (defun org-agenda-span-name (span)
3954 "Return a SPAN name."
3955 (if (null span)
3957 (if (symbolp span)
3958 (capitalize (symbol-name span))
3959 (format "%d days" span))))
3961 ;;; Agenda word search
3963 (defvar org-agenda-search-history nil)
3964 (defvar org-todo-only nil)
3966 (defvar org-search-syntax-table nil
3967 "Special syntax table for org-mode search.
3968 In this table, we have single quotes not as word constituents, to
3969 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3971 (defun org-search-syntax-table ()
3972 (unless org-search-syntax-table
3973 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3974 (modify-syntax-entry ?' "." org-search-syntax-table)
3975 (modify-syntax-entry ?` "." org-search-syntax-table))
3976 org-search-syntax-table)
3978 (defvar org-agenda-last-search-view-search-was-boolean nil)
3980 ;;;###autoload
3981 (defun org-search-view (&optional todo-only string edit-at)
3982 "Show all entries that contain a phrase or words or regular expressions.
3984 With optional prefix argument TODO-ONLY, only consider entries that are
3985 TODO entries. The argument STRING can be used to pass a default search
3986 string into this function. If EDIT-AT is non-nil, it means that the
3987 user should get a chance to edit this string, with cursor at position
3988 EDIT-AT.
3990 The search string can be viewed either as a phrase that should be found as
3991 is, or it can be broken into a number of snippets, each of which must match
3992 in a Boolean way to select an entry. The default depends on the variable
3993 `org-agenda-search-view-always-boolean'.
3994 Even if this is turned off (the default) you can always switch to
3995 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3997 The default is a direct search of the whole phrase, where each space in
3998 the search string can expand to an arbitrary amount of whitespace,
3999 including newlines.
4001 If using a Boolean search, the search string is split on whitespace and
4002 each snippet is searched separately, with logical AND to select an entry.
4003 Words prefixed with a minus must *not* occur in the entry. Words without
4004 a prefix or prefixed with a plus must occur in the entry. Matching is
4005 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4006 match whole words, not parts of a word) if
4007 `org-agenda-search-view-force-full-words' is set (default is nil).
4009 Boolean search snippets enclosed by curly braces are interpreted as
4010 regular expressions that must or (when preceded with \"-\") must not
4011 match in the entry. Snippets enclosed into double quotes will be taken
4012 as a whole, to include whitespace.
4014 - If the search string starts with an asterisk, search only in headlines.
4015 - If (possibly after the leading star) the search string starts with an
4016 exclamation mark, this also means to look at TODO entries only, an effect
4017 that can also be achieved with a prefix argument.
4018 - If (possibly after star and exclamation mark) the search string starts
4019 with a colon, this will mean that the (non-regexp) snippets of the
4020 Boolean search must match as full words.
4022 This command searches the agenda files, and in addition the files listed
4023 in `org-agenda-text-search-extra-files'."
4024 (interactive "P")
4025 (org-prepare-agenda "SEARCH")
4026 (org-compile-prefix-format 'search)
4027 (org-set-sorting-strategy 'search)
4028 (let* ((props (list 'face nil
4029 'done-face 'org-agenda-done
4030 'org-not-done-regexp org-not-done-regexp
4031 'org-todo-regexp org-todo-regexp
4032 'org-complex-heading-regexp org-complex-heading-regexp
4033 'mouse-face 'highlight
4034 'help-echo (format "mouse-2 or RET jump to location")))
4035 (full-words org-agenda-search-view-force-full-words)
4036 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4037 regexp rtn rtnall files file pos
4038 marker category org-category-pos tags c neg re boolean
4039 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4040 (unless (and (not edit-at)
4041 (stringp string)
4042 (string-match "\\S-" string))
4043 (setq string (read-string
4044 (if org-agenda-search-view-always-boolean
4045 "[+-]Word/{Regexp} ...: "
4046 "Phrase, or [+-]Word/{Regexp} ...: ")
4047 (cond
4048 ((integerp edit-at) (cons string edit-at))
4049 (edit-at string))
4050 'org-agenda-search-history)))
4051 (org-set-local 'org-todo-only todo-only)
4052 (setq org-agenda-redo-command
4053 (list 'org-search-view (if todo-only t nil) string
4054 '(if current-prefix-arg 1 nil)))
4055 (setq org-agenda-query-string string)
4057 (if (equal (string-to-char string) ?*)
4058 (setq hdl-only t
4059 words (substring string 1))
4060 (setq words string))
4061 (when (equal (string-to-char words) ?!)
4062 (setq todo-only t
4063 words (substring words 1)))
4064 (when (equal (string-to-char words) ?:)
4065 (setq full-words t
4066 words (substring words 1)))
4067 (if (or org-agenda-search-view-always-boolean
4068 (member (string-to-char words) '(?- ?+ ?\{)))
4069 (setq boolean t))
4070 (setq words (org-split-string words))
4071 (let (www w)
4072 (while (setq w (pop words))
4073 (while (and (string-match "\\\\\\'" w) words)
4074 (setq w (concat (substring w 0 -1) " " (pop words))))
4075 (push w www))
4076 (setq words (nreverse www) www nil)
4077 (while (setq w (pop words))
4078 (when (and (string-match "\\`[-+]?{" w)
4079 (not (string-match "}\\'" w)))
4080 (while (and words (not (string-match "}\\'" (car words))))
4081 (setq w (concat w " " (pop words))))
4082 (setq w (concat w " " (pop words))))
4083 (push w www))
4084 (setq words (nreverse www)))
4085 (setq org-agenda-last-search-view-search-was-boolean boolean)
4086 (when boolean
4087 (let (wds w)
4088 (while (setq w (pop words))
4089 (if (or (equal (substring w 0 1) "\"")
4090 (and (> (length w) 1)
4091 (member (substring w 0 1) '("+" "-"))
4092 (equal (substring w 1 2) "\"")))
4093 (while (and words (not (equal (substring w -1) "\"")))
4094 (setq w (concat w " " (pop words)))))
4095 (and (string-match "\\`\\([-+]?\\)\"" w)
4096 (setq w (replace-match "\\1" nil nil w)))
4097 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4098 (push w wds))
4099 (setq words (nreverse wds))))
4100 (if boolean
4101 (mapc (lambda (w)
4102 (setq c (string-to-char w))
4103 (if (equal c ?-)
4104 (setq neg t w (substring w 1))
4105 (if (equal c ?+)
4106 (setq neg nil w (substring w 1))
4107 (setq neg nil)))
4108 (if (string-match "\\`{.*}\\'" w)
4109 (setq re (substring w 1 -1))
4110 (if full-words
4111 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4112 (setq re (regexp-quote (downcase w)))))
4113 (if neg (push re regexps-) (push re regexps+)))
4114 words)
4115 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4116 regexps+))
4117 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4118 (if (not regexps+)
4119 (setq regexp org-outline-regexp-bol)
4120 (setq regexp (pop regexps+))
4121 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4122 regexp))))
4123 (setq files (org-agenda-files nil 'ifmode))
4124 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4125 (pop org-agenda-text-search-extra-files)
4126 (setq files (org-add-archive-files files)))
4127 (setq files (append files org-agenda-text-search-extra-files)
4128 rtnall nil)
4129 (while (setq file (pop files))
4130 (setq ee nil)
4131 (catch 'nextfile
4132 (org-check-agenda-file file)
4133 (setq buffer (if (file-exists-p file)
4134 (org-get-agenda-file-buffer file)
4135 (error "No such file %s" file)))
4136 (if (not buffer)
4137 ;; If file does not exist, make sure an error message is sent
4138 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4139 file))))
4140 (with-current-buffer buffer
4141 (with-syntax-table (org-search-syntax-table)
4142 (unless (derived-mode-p 'org-mode)
4143 (error "Agenda file %s is not in `org-mode'" file))
4144 (let ((case-fold-search t))
4145 (save-excursion
4146 (save-restriction
4147 (if org-agenda-restrict
4148 (narrow-to-region org-agenda-restrict-begin
4149 org-agenda-restrict-end)
4150 (widen))
4151 (goto-char (point-min))
4152 (unless (or (org-at-heading-p)
4153 (outline-next-heading))
4154 (throw 'nextfile t))
4155 (goto-char (max (point-min) (1- (point))))
4156 (while (re-search-forward regexp nil t)
4157 (org-back-to-heading t)
4158 (skip-chars-forward "* ")
4159 (setq beg (point-at-bol)
4160 beg1 (point)
4161 end (progn (outline-next-heading) (point)))
4162 (catch :skip
4163 (goto-char beg)
4164 (org-agenda-skip)
4165 (setq str (buffer-substring-no-properties
4166 (point-at-bol)
4167 (if hdl-only (point-at-eol) end)))
4168 (mapc (lambda (wr) (when (string-match wr str)
4169 (goto-char (1- end))
4170 (throw :skip t)))
4171 regexps-)
4172 (mapc (lambda (wr) (unless (string-match wr str)
4173 (goto-char (1- end))
4174 (throw :skip t)))
4175 (if todo-only
4176 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4177 regexps+)
4178 regexps+))
4179 (goto-char beg)
4180 (setq marker (org-agenda-new-marker (point))
4181 category (org-get-category)
4182 org-category-pos (get-text-property (point) 'org-category-position)
4183 tags (org-get-tags-at (point))
4184 txt (org-agenda-format-item
4186 (buffer-substring-no-properties
4187 beg1 (point-at-eol))
4188 category tags))
4189 (org-add-props txt props
4190 'org-marker marker 'org-hd-marker marker
4191 'org-todo-regexp org-todo-regexp
4192 'org-complex-heading-regexp org-complex-heading-regexp
4193 'priority 1000 'org-category category
4194 'org-category-position org-category-pos
4195 'type "search")
4196 (push txt ee)
4197 (goto-char (1- end))))))))))
4198 (setq rtn (nreverse ee))
4199 (setq rtnall (append rtnall rtn)))
4200 (if org-agenda-overriding-header
4201 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4202 nil 'face 'org-agenda-structure) "\n")
4203 (insert "Search words: ")
4204 (add-text-properties (point-min) (1- (point))
4205 (list 'face 'org-agenda-structure))
4206 (setq pos (point))
4207 (insert string "\n")
4208 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4209 (setq pos (point))
4210 (unless org-agenda-multi
4211 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4212 (add-text-properties pos (1- (point))
4213 (list 'face 'org-agenda-structure))))
4214 (org-agenda-mark-header-line (point-min))
4215 (when rtnall
4216 (insert (org-finalize-agenda-entries rtnall) "\n"))
4217 (goto-char (point-min))
4218 (or org-agenda-multi (org-fit-agenda-window))
4219 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
4220 (org-finalize-agenda)
4221 (setq buffer-read-only t)))
4223 ;;; Agenda TODO list
4225 (defvar org-select-this-todo-keyword nil)
4226 (defvar org-last-arg nil)
4228 ;;;###autoload
4229 (defun org-todo-list (arg)
4230 "Show all (not done) TODO entries from all agenda file in a single list.
4231 The prefix arg can be used to select a specific TODO keyword and limit
4232 the list to these. When using \\[universal-argument], you will be prompted
4233 for a keyword. A numeric prefix directly selects the Nth keyword in
4234 `org-todo-keywords-1'."
4235 (interactive "P")
4236 (org-prepare-agenda "TODO")
4237 (org-compile-prefix-format 'todo)
4238 (org-set-sorting-strategy 'todo)
4239 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4240 (let* ((today (org-today))
4241 (date (calendar-gregorian-from-absolute today))
4242 (kwds org-todo-keywords-for-agenda)
4243 (completion-ignore-case t)
4244 (org-select-this-todo-keyword
4245 (if (stringp arg) arg
4246 (and arg (integerp arg) (> arg 0)
4247 (nth (1- arg) kwds))))
4248 rtn rtnall files file pos)
4249 (when (equal arg '(4))
4250 (setq org-select-this-todo-keyword
4251 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4252 (mapcar 'list kwds) nil nil)))
4253 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4254 (org-set-local 'org-last-arg arg)
4255 (setq org-agenda-redo-command
4256 '(org-todo-list (or current-prefix-arg org-last-arg)))
4257 (setq files (org-agenda-files nil 'ifmode)
4258 rtnall nil)
4259 (while (setq file (pop files))
4260 (catch 'nextfile
4261 (org-check-agenda-file file)
4262 (setq rtn (org-agenda-get-day-entries file date :todo))
4263 (setq rtnall (append rtnall rtn))))
4264 (if org-agenda-overriding-header
4265 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4266 nil 'face 'org-agenda-structure) "\n")
4267 (insert "Global list of TODO items of type: ")
4268 (add-text-properties (point-min) (1- (point))
4269 (list 'face 'org-agenda-structure
4270 'short-heading
4271 (concat "ToDo: "
4272 (or org-select-this-todo-keyword "ALL"))))
4273 (org-agenda-mark-header-line (point-min))
4274 (setq pos (point))
4275 (insert (or org-select-this-todo-keyword "ALL") "\n")
4276 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4277 (setq pos (point))
4278 (unless org-agenda-multi
4279 (insert "Available with `N r': (0)ALL")
4280 (let ((n 0) s)
4281 (mapc (lambda (x)
4282 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4283 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4284 (insert "\n "))
4285 (insert " " s))
4286 kwds))
4287 (insert "\n"))
4288 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4289 (org-agenda-mark-header-line (point-min))
4290 (when rtnall
4291 (insert (org-finalize-agenda-entries rtnall) "\n"))
4292 (goto-char (point-min))
4293 (or org-agenda-multi (org-fit-agenda-window))
4294 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4295 (org-finalize-agenda)
4296 (setq buffer-read-only t)))
4298 ;;; Agenda tags match
4300 ;;;###autoload
4301 (defun org-tags-view (&optional todo-only match)
4302 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4303 The prefix arg TODO-ONLY limits the search to TODO entries."
4304 (interactive "P")
4305 (let* ((org-tags-match-list-sublevels
4306 org-tags-match-list-sublevels)
4307 (completion-ignore-case t)
4308 rtn rtnall files file pos matcher
4309 buffer)
4310 (when (and (stringp match) (not (string-match "\\S-" match)))
4311 (setq match nil))
4312 (setq matcher (org-make-tags-matcher match)
4313 match (car matcher) matcher (cdr matcher))
4314 (org-prepare-agenda (concat "TAGS " match))
4315 (org-compile-prefix-format 'tags)
4316 (org-set-sorting-strategy 'tags)
4317 (setq org-agenda-query-string match)
4318 (setq org-agenda-redo-command
4319 (list 'org-tags-view (list 'quote todo-only)
4320 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4321 (setq files (org-agenda-files nil 'ifmode)
4322 rtnall nil)
4323 (while (setq file (pop files))
4324 (catch 'nextfile
4325 (org-check-agenda-file file)
4326 (setq buffer (if (file-exists-p file)
4327 (org-get-agenda-file-buffer file)
4328 (error "No such file %s" file)))
4329 (if (not buffer)
4330 ;; If file does not exist, error message to agenda
4331 (setq rtn (list
4332 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4333 rtnall (append rtnall rtn))
4334 (with-current-buffer buffer
4335 (unless (derived-mode-p 'org-mode)
4336 (error "Agenda file %s is not in `org-mode'" file))
4337 (save-excursion
4338 (save-restriction
4339 (if org-agenda-restrict
4340 (narrow-to-region org-agenda-restrict-begin
4341 org-agenda-restrict-end)
4342 (widen))
4343 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4344 (setq rtnall (append rtnall rtn))))))))
4345 (if org-agenda-overriding-header
4346 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4347 nil 'face 'org-agenda-structure) "\n")
4348 (insert "Headlines with TAGS match: ")
4349 (add-text-properties (point-min) (1- (point))
4350 (list 'face 'org-agenda-structure
4351 'short-heading
4352 (concat "Match: " match)))
4353 (setq pos (point))
4354 (insert match "\n")
4355 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4356 (setq pos (point))
4357 (unless org-agenda-multi
4358 (insert "Press `C-u r' to search again with new search string\n"))
4359 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4360 (org-agenda-mark-header-line (point-min))
4361 (when rtnall
4362 (insert (org-finalize-agenda-entries rtnall) "\n"))
4363 (goto-char (point-min))
4364 (or org-agenda-multi (org-fit-agenda-window))
4365 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4366 (org-finalize-agenda)
4367 (setq buffer-read-only t)))
4369 ;;; Agenda Finding stuck projects
4371 (defvar org-agenda-skip-regexp nil
4372 "Regular expression used in skipping subtrees for the agenda.
4373 This is basically a temporary global variable that can be set and then
4374 used by user-defined selections using `org-agenda-skip-function'.")
4376 (defvar org-agenda-overriding-header nil
4377 "When set during agenda, todo and tags searches it replaces the header.
4378 This variable should not be set directly, but custom commands can bind it
4379 in the options section.")
4381 (defun org-agenda-skip-entry-when-regexp-matches ()
4382 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4383 If yes, it returns the end position of this entry, causing agenda commands
4384 to skip the entry but continuing the search in the subtree. This is a
4385 function that can be put into `org-agenda-skip-function' for the duration
4386 of a command."
4387 (let ((end (save-excursion (org-end-of-subtree t)))
4388 skip)
4389 (save-excursion
4390 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4391 (and skip end)))
4393 (defun org-agenda-skip-subtree-when-regexp-matches ()
4394 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4395 If yes, it returns the end position of this tree, causing agenda commands
4396 to skip this subtree. This is a function that can be put into
4397 `org-agenda-skip-function' for the duration of a command."
4398 (let ((end (save-excursion (org-end-of-subtree t)))
4399 skip)
4400 (save-excursion
4401 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4402 (and skip end)))
4404 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4405 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4406 If yes, it returns the end position of the current entry (NOT the tree),
4407 causing agenda commands to skip the entry but continuing the search in
4408 the subtree. This is a function that can be put into
4409 `org-agenda-skip-function' for the duration of a command. An important
4410 use of this function is for the stuck project list."
4411 (let ((end (save-excursion (org-end-of-subtree t)))
4412 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4413 skip)
4414 (save-excursion
4415 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4416 (and skip entry-end)))
4418 (defun org-agenda-skip-entry-if (&rest conditions)
4419 "Skip entry if any of CONDITIONS is true.
4420 See `org-agenda-skip-if' for details."
4421 (org-agenda-skip-if nil conditions))
4423 (defun org-agenda-skip-subtree-if (&rest conditions)
4424 "Skip entry if any of CONDITIONS is true.
4425 See `org-agenda-skip-if' for details."
4426 (org-agenda-skip-if t conditions))
4428 (defun org-agenda-skip-if (subtree conditions)
4429 "Checks current entity for CONDITIONS.
4430 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4431 the entry (i.e. the text before the next heading) is checked.
4433 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4434 from different tests. Valid conditions are:
4436 scheduled Check if there is a scheduled cookie
4437 notscheduled Check if there is no scheduled cookie
4438 deadline Check if there is a deadline
4439 notdeadline Check if there is no deadline
4440 timestamp Check if there is a timestamp (also deadline or scheduled)
4441 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4442 regexp Check if regexp matches
4443 notregexp Check if regexp does not match.
4444 todo Check if TODO keyword matches
4445 nottodo Check if TODO keyword does not match
4447 The regexp is taken from the conditions list, it must come right after
4448 the `regexp' or `notregexp' element.
4450 `todo' and `nottodo' accept as an argument a list of todo
4451 keywords, which may include \"*\" to match any todo keyword.
4453 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4455 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4457 Instead of a list, a keyword class may be given. For example:
4459 (org-agenda-skip-entry-if 'nottodo 'done)
4461 would skip entries that haven't been marked with any of \"DONE\"
4462 keywords. Possible classes are: `todo', `done', `any'.
4464 If any of these conditions is met, this function returns the end point of
4465 the entity, causing the search to continue from there. This is a function
4466 that can be put into `org-agenda-skip-function' for the duration of a command."
4467 (let (beg end m)
4468 (org-back-to-heading t)
4469 (setq beg (point)
4470 end (if subtree
4471 (progn (org-end-of-subtree t) (point))
4472 (progn (outline-next-heading) (1- (point)))))
4473 (goto-char beg)
4474 (and
4476 (and (memq 'scheduled conditions)
4477 (re-search-forward org-scheduled-time-regexp end t))
4478 (and (memq 'notscheduled conditions)
4479 (not (re-search-forward org-scheduled-time-regexp end t)))
4480 (and (memq 'deadline conditions)
4481 (re-search-forward org-deadline-time-regexp end t))
4482 (and (memq 'notdeadline conditions)
4483 (not (re-search-forward org-deadline-time-regexp end t)))
4484 (and (memq 'timestamp conditions)
4485 (re-search-forward org-ts-regexp end t))
4486 (and (memq 'nottimestamp conditions)
4487 (not (re-search-forward org-ts-regexp end t)))
4488 (and (setq m (memq 'regexp conditions))
4489 (stringp (nth 1 m))
4490 (re-search-forward (nth 1 m) end t))
4491 (and (setq m (memq 'notregexp conditions))
4492 (stringp (nth 1 m))
4493 (not (re-search-forward (nth 1 m) end t)))
4494 (and (or
4495 (setq m (memq 'nottodo conditions))
4496 (setq m (memq 'todo-unblocked conditions))
4497 (setq m (memq 'nottodo-unblocked conditions))
4498 (setq m (memq 'todo conditions)))
4499 (org-agenda-skip-if-todo m end)))
4500 end)))
4502 (defun org-agenda-skip-if-todo (args end)
4503 "Helper function for `org-agenda-skip-if', do not use it directly.
4504 ARGS is a list with first element either `todo', `nottodo',
4505 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4506 a list of TODO keywords, or a state symbol `todo' or `done' or
4507 `any'."
4508 (let ((kw (car args))
4509 (arg (cadr args))
4510 todo-wds todo-re)
4511 (setq todo-wds
4512 (org-uniquify
4513 (cond
4514 ((listp arg) ;; list of keywords
4515 (if (member "*" arg)
4516 (mapcar 'substring-no-properties org-todo-keywords-1)
4517 arg))
4518 ((symbolp arg) ;; keyword class name
4519 (cond
4520 ((eq arg 'todo)
4521 (org-delete-all org-done-keywords
4522 (mapcar 'substring-no-properties
4523 org-todo-keywords-1)))
4524 ((eq arg 'done) org-done-keywords)
4525 ((eq arg 'any)
4526 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4527 (setq todo-re
4528 (concat "^\\*+[ \t]+\\<\\("
4529 (mapconcat 'identity todo-wds "\\|")
4530 "\\)\\>"))
4531 (cond
4532 ((eq kw 'todo) (re-search-forward todo-re end t))
4533 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4534 ((eq kw 'todo-unblocked)
4535 (catch 'unblocked
4536 (while (re-search-forward todo-re end t)
4537 (or (org-entry-blocked-p) (throw 'unblocked t)))
4538 nil))
4539 ((eq kw 'nottodo-unblocked)
4540 (catch 'unblocked
4541 (while (re-search-forward todo-re end t)
4542 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4546 ;;;###autoload
4547 (defun org-agenda-list-stuck-projects (&rest ignore)
4548 "Create agenda view for projects that are stuck.
4549 Stuck projects are project that have no next actions. For the definitions
4550 of what a project is and how to check if it stuck, customize the variable
4551 `org-stuck-projects'."
4552 (interactive)
4553 (let* ((org-agenda-skip-function
4554 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4555 ;; We could have used org-agenda-skip-if here.
4556 (org-agenda-overriding-header
4557 (or org-agenda-overriding-header "List of stuck projects: "))
4558 (matcher (nth 0 org-stuck-projects))
4559 (todo (nth 1 org-stuck-projects))
4560 (todo-wds (if (member "*" todo)
4561 (progn
4562 (org-prepare-agenda-buffers (org-agenda-files
4563 nil 'ifmode))
4564 (org-delete-all
4565 org-done-keywords-for-agenda
4566 (copy-sequence org-todo-keywords-for-agenda)))
4567 todo))
4568 (todo-re (concat "^\\*+[ \t]+\\("
4569 (mapconcat 'identity todo-wds "\\|")
4570 "\\)\\>"))
4571 (tags (nth 2 org-stuck-projects))
4572 (tags-re (if (member "*" tags)
4573 (concat org-outline-regexp-bol
4574 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4575 (if tags
4576 (concat org-outline-regexp-bol
4577 ".*:\\("
4578 (mapconcat 'identity tags "\\|")
4579 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4580 (gen-re (nth 3 org-stuck-projects))
4581 (re-list
4582 (delq nil
4583 (list
4584 (if todo todo-re)
4585 (if tags tags-re)
4586 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4587 gen-re)))))
4588 (setq org-agenda-skip-regexp
4589 (if re-list
4590 (mapconcat 'identity re-list "\\|")
4591 (error "No information how to identify unstuck projects")))
4592 (org-tags-view nil matcher)
4593 (with-current-buffer org-agenda-buffer-name
4594 (setq org-agenda-redo-command
4595 '(org-agenda-list-stuck-projects
4596 (or current-prefix-arg org-last-arg))))))
4598 ;;; Diary integration
4600 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4601 (defvar diary-list-entries-hook)
4602 (defvar diary-time-regexp)
4603 (defun org-get-entries-from-diary (date)
4604 "Get the (Emacs Calendar) diary entries for DATE."
4605 (require 'diary-lib)
4606 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4607 (diary-display-hook '(fancy-diary-display))
4608 (diary-display-function 'fancy-diary-display)
4609 (pop-up-frames nil)
4610 (diary-list-entries-hook
4611 (cons 'org-diary-default-entry diary-list-entries-hook))
4612 (diary-file-name-prefix-function nil) ; turn this feature off
4613 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4614 entries
4615 (org-disable-agenda-to-diary t))
4616 (save-excursion
4617 (save-window-excursion
4618 (funcall (if (fboundp 'diary-list-entries)
4619 'diary-list-entries 'list-diary-entries)
4620 date 1)))
4621 (if (not (get-buffer diary-fancy-buffer))
4622 (setq entries nil)
4623 (with-current-buffer diary-fancy-buffer
4624 (setq buffer-read-only nil)
4625 (if (zerop (buffer-size))
4626 ;; No entries
4627 (setq entries nil)
4628 ;; Omit the date and other unnecessary stuff
4629 (org-agenda-cleanup-fancy-diary)
4630 ;; Add prefix to each line and extend the text properties
4631 (if (zerop (buffer-size))
4632 (setq entries nil)
4633 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4634 (setq entries
4635 (with-temp-buffer
4636 (insert entries) (goto-char (point-min))
4637 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4638 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4639 (replace-match (concat "; " (match-string 1)))))
4640 (buffer-string)))))
4641 (set-buffer-modified-p nil)
4642 (kill-buffer diary-fancy-buffer)))
4643 (when entries
4644 (setq entries (org-split-string entries "\n"))
4645 (setq entries
4646 (mapcar
4647 (lambda (x)
4648 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4649 ;; Extend the text properties to the beginning of the line
4650 (org-add-props x (text-properties-at (1- (length x)) x)
4651 'type "diary" 'date date 'face 'org-agenda-diary))
4652 entries)))))
4654 (defvar org-agenda-cleanup-fancy-diary-hook nil
4655 "Hook run when the fancy diary buffer is cleaned up.")
4657 (defun org-agenda-cleanup-fancy-diary ()
4658 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4659 This gets rid of the date, the underline under the date, and
4660 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4661 date. It also removes lines that contain only whitespace."
4662 (goto-char (point-min))
4663 (if (looking-at ".*?:[ \t]*")
4664 (progn
4665 (replace-match "")
4666 (re-search-forward "\n=+$" nil t)
4667 (replace-match "")
4668 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4669 (re-search-forward "\n=+$" nil t)
4670 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4671 (goto-char (point-min))
4672 (while (re-search-forward "^ +\n" nil t)
4673 (replace-match ""))
4674 (goto-char (point-min))
4675 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4676 (replace-match ""))
4677 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4679 ;; Make sure entries from the diary have the right text properties.
4680 (eval-after-load "diary-lib"
4681 '(if (boundp 'diary-modify-entry-list-string-function)
4682 ;; We can rely on the hook, nothing to do
4684 ;; Hook not available, must use advice to make this work
4685 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4686 "Make the position visible."
4687 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4688 (stringp string)
4689 buffer-file-name)
4690 (setq string (org-modify-diary-entry-string string))))))
4692 (defun org-modify-diary-entry-string (string)
4693 "Add text properties to string, allowing org-mode to act on it."
4694 (org-add-props string nil
4695 'mouse-face 'highlight
4696 'help-echo (if buffer-file-name
4697 (format "mouse-2 or RET jump to diary file %s"
4698 (abbreviate-file-name buffer-file-name))
4700 'org-agenda-diary-link t
4701 'org-marker (org-agenda-new-marker (point-at-bol))))
4703 (defun org-diary-default-entry ()
4704 "Add a dummy entry to the diary.
4705 Needed to avoid empty dates which mess up holiday display."
4706 ;; Catch the error if dealing with the new add-to-diary-alist
4707 (when org-disable-agenda-to-diary
4708 (condition-case nil
4709 (org-add-to-diary-list original-date "Org-mode dummy" "")
4710 (error
4711 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4713 (defun org-add-to-diary-list (&rest args)
4714 (if (fboundp 'diary-add-to-list)
4715 (apply 'diary-add-to-list args)
4716 (apply 'add-to-diary-list args)))
4718 (defvar org-diary-last-run-time nil)
4720 ;;;###autoload
4721 (defun org-diary (&rest args)
4722 "Return diary information from org-files.
4723 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4724 It accesses org files and extracts information from those files to be
4725 listed in the diary. The function accepts arguments specifying what
4726 items should be listed. For a list of arguments allowed here, see the
4727 variable `org-agenda-entry-types'.
4729 The call in the diary file should look like this:
4731 &%%(org-diary) ~/path/to/some/orgfile.org
4733 Use a separate line for each org file to check. Or, if you omit the file name,
4734 all files listed in `org-agenda-files' will be checked automatically:
4736 &%%(org-diary)
4738 If you don't give any arguments (as in the example above), the default
4739 arguments (:deadline :scheduled :timestamp :sexp) are used.
4740 So the example above may also be written as
4742 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4744 The function expects the lisp variables `entry' and `date' to be provided
4745 by the caller, because this is how the calendar works. Don't use this
4746 function from a program - use `org-agenda-get-day-entries' instead."
4747 (when (> (- (org-float-time)
4748 org-agenda-last-marker-time)
4750 ;; I am not sure if this works with sticky agendas, because the marker
4751 ;; list is then no longer a global variable.
4752 (org-agenda-reset-markers))
4753 ;; Prevent `org-compile-prefix-format' to fail when there is no agenda
4754 (when (buffer-live-p org-agenda-buffer)
4755 (org-compile-prefix-format 'agenda))
4756 (org-set-sorting-strategy 'agenda)
4757 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4758 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4759 (list entry)
4760 (org-agenda-files t)))
4761 (time (org-float-time))
4762 file rtn results)
4763 (when (or (not org-diary-last-run-time)
4764 (> (- time
4765 org-diary-last-run-time)
4767 (org-prepare-agenda-buffers files))
4768 (setq org-diary-last-run-time time)
4769 ;; If this is called during org-agenda, don't return any entries to
4770 ;; the calendar. Org Agenda will list these entries itself.
4771 (if org-disable-agenda-to-diary (setq files nil))
4772 (while (setq file (pop files))
4773 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4774 (setq results (append results rtn)))
4775 (if results
4776 (concat (org-finalize-agenda-entries results) "\n"))))
4778 ;;; Agenda entry finders
4780 (defun org-agenda-get-day-entries (file date &rest args)
4781 "Does the work for `org-diary' and `org-agenda'.
4782 FILE is the path to a file to be checked for entries. DATE is date like
4783 the one returned by `calendar-current-date'. ARGS are symbols indicating
4784 which kind of entries should be extracted. For details about these, see
4785 the documentation of `org-diary'."
4786 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4787 (let* ((org-startup-folded nil)
4788 (org-startup-align-all-tables nil)
4789 (buffer (if (file-exists-p file)
4790 (org-get-agenda-file-buffer file)
4791 (error "No such file %s" file)))
4792 arg results rtn deadline-results)
4793 (if (not buffer)
4794 ;; If file does not exist, make sure an error message ends up in diary
4795 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4796 (with-current-buffer buffer
4797 (unless (derived-mode-p 'org-mode)
4798 (error "Agenda file %s is not in `org-mode'" file))
4799 (let ((case-fold-search nil))
4800 (save-excursion
4801 (save-restriction
4802 (if org-agenda-restrict
4803 (narrow-to-region org-agenda-restrict-begin
4804 org-agenda-restrict-end)
4805 (widen))
4806 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4807 (while (setq arg (pop args))
4808 (cond
4809 ((and (eq arg :todo)
4810 (equal date (calendar-gregorian-from-absolute
4811 (org-today))))
4812 (setq rtn (org-agenda-get-todos))
4813 (setq results (append results rtn)))
4814 ((eq arg :timestamp)
4815 (setq rtn (org-agenda-get-blocks))
4816 (setq results (append results rtn))
4817 (setq rtn (org-agenda-get-timestamps deadline-results))
4818 (setq results (append results rtn)))
4819 ((eq arg :sexp)
4820 (setq rtn (org-agenda-get-sexps))
4821 (setq results (append results rtn)))
4822 ((eq arg :scheduled)
4823 (setq rtn (org-agenda-get-scheduled deadline-results))
4824 (setq results (append results rtn)))
4825 ((eq arg :closed)
4826 (setq rtn (org-agenda-get-progress))
4827 (setq results (append results rtn)))
4828 ((eq arg :deadline)
4829 (setq rtn (org-agenda-get-deadlines))
4830 (setq deadline-results (copy-sequence rtn))
4831 (setq results (append results rtn))))))))
4832 results))))
4834 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4835 (defun org-agenda-get-todos ()
4836 "Return the TODO information for agenda display."
4837 (let* ((props (list 'face nil
4838 'done-face 'org-agenda-done
4839 'org-not-done-regexp org-not-done-regexp
4840 'org-todo-regexp org-todo-regexp
4841 'org-complex-heading-regexp org-complex-heading-regexp
4842 'mouse-face 'highlight
4843 'help-echo
4844 (format "mouse-2 or RET jump to org file %s"
4845 (abbreviate-file-name buffer-file-name))))
4846 (regexp (format org-heading-keyword-regexp-format
4847 (cond
4848 ((and org-select-this-todo-keyword
4849 (equal org-select-this-todo-keyword "*"))
4850 org-todo-regexp)
4851 (org-select-this-todo-keyword
4852 (concat "\\("
4853 (mapconcat 'identity
4854 (org-split-string
4855 org-select-this-todo-keyword
4856 "|")
4857 "\\|") "\\)"))
4858 (t org-not-done-regexp))))
4859 marker priority category org-category-pos tags todo-state
4860 ee txt beg end)
4861 (goto-char (point-min))
4862 (while (re-search-forward regexp nil t)
4863 (catch :skip
4864 (save-match-data
4865 (beginning-of-line)
4866 (org-agenda-skip)
4867 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4868 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4869 (goto-char (1+ beg))
4870 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4871 (throw :skip nil)))
4872 (goto-char (match-beginning 2))
4873 (setq marker (org-agenda-new-marker (match-beginning 0))
4874 category (org-get-category)
4875 org-category-pos (get-text-property (point) 'org-category-position)
4876 txt (org-trim
4877 (buffer-substring (match-beginning 2) (match-end 0)))
4878 tags (org-get-tags-at (point))
4879 txt (org-agenda-format-item "" txt category tags)
4880 priority (1+ (org-get-priority txt))
4881 todo-state (org-get-todo-state))
4882 (org-add-props txt props
4883 'org-marker marker 'org-hd-marker marker
4884 'priority priority 'org-category category
4885 'org-category-position org-category-pos
4886 'type "todo" 'todo-state todo-state)
4887 (push txt ee)
4888 (if org-agenda-todo-list-sublevels
4889 (goto-char (match-end 2))
4890 (org-end-of-subtree 'invisible))))
4891 (nreverse ee)))
4893 (defun org-agenda-todo-custom-ignore-p (time n)
4894 "Check whether timestamp is farther away then n number of days.
4895 This function is invoked if `org-agenda-todo-ignore-deadlines',
4896 `org-agenda-todo-ignore-scheduled' or
4897 `org-agenda-todo-ignore-timestamp' is set to an integer."
4898 (let ((days (org-days-to-time time)))
4899 (if (>= n 0)
4900 (>= days n)
4901 (<= days n))))
4903 ;;;###autoload
4904 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4905 (&optional end)
4906 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4907 (when (or org-agenda-todo-ignore-with-date
4908 org-agenda-todo-ignore-scheduled
4909 org-agenda-todo-ignore-deadlines
4910 org-agenda-todo-ignore-timestamp)
4911 (setq end (or end (save-excursion (outline-next-heading) (point))))
4912 (save-excursion
4913 (or (and org-agenda-todo-ignore-with-date
4914 (re-search-forward org-ts-regexp end t))
4915 (and org-agenda-todo-ignore-scheduled
4916 (re-search-forward org-scheduled-time-regexp end t)
4917 (cond
4918 ((eq org-agenda-todo-ignore-scheduled 'future)
4919 (> (org-days-to-time (match-string 1)) 0))
4920 ((eq org-agenda-todo-ignore-scheduled 'past)
4921 (<= (org-days-to-time (match-string 1)) 0))
4922 ((numberp org-agenda-todo-ignore-scheduled)
4923 (org-agenda-todo-custom-ignore-p
4924 (match-string 1) org-agenda-todo-ignore-scheduled))
4925 (t)))
4926 (and org-agenda-todo-ignore-deadlines
4927 (re-search-forward org-deadline-time-regexp end t)
4928 (cond
4929 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4930 ((eq org-agenda-todo-ignore-deadlines 'far)
4931 (not (org-deadline-close (match-string 1))))
4932 ((eq org-agenda-todo-ignore-deadlines 'future)
4933 (> (org-days-to-time (match-string 1)) 0))
4934 ((eq org-agenda-todo-ignore-deadlines 'past)
4935 (<= (org-days-to-time (match-string 1)) 0))
4936 ((numberp org-agenda-todo-ignore-deadlines)
4937 (org-agenda-todo-custom-ignore-p
4938 (match-string 1) org-agenda-todo-ignore-deadlines))
4939 (t (org-deadline-close (match-string 1)))))
4940 (and org-agenda-todo-ignore-timestamp
4941 (let ((buffer (current-buffer))
4942 (regexp
4943 (concat
4944 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4945 (start (point)))
4946 ;; Copy current buffer into a temporary one
4947 (with-temp-buffer
4948 (insert-buffer-substring buffer start end)
4949 (goto-char (point-min))
4950 ;; Delete SCHEDULED and DEADLINE items
4951 (while (re-search-forward regexp end t)
4952 (delete-region (match-beginning 0) (match-end 0)))
4953 (goto-char (point-min))
4954 ;; No search for timestamp left
4955 (when (re-search-forward org-ts-regexp nil t)
4956 (cond
4957 ((eq org-agenda-todo-ignore-timestamp 'future)
4958 (> (org-days-to-time (match-string 1)) 0))
4959 ((eq org-agenda-todo-ignore-timestamp 'past)
4960 (<= (org-days-to-time (match-string 1)) 0))
4961 ((numberp org-agenda-todo-ignore-timestamp)
4962 (org-agenda-todo-custom-ignore-p
4963 (match-string 1) org-agenda-todo-ignore-timestamp))
4964 (t))))))))))
4966 (defconst org-agenda-no-heading-message
4967 "No heading for this item in buffer or region.")
4969 (defun org-agenda-get-timestamps (&optional deadline-results)
4970 "Return the date stamp information for agenda display."
4971 (let* ((props (list 'face 'org-agenda-calendar-event
4972 'org-not-done-regexp org-not-done-regexp
4973 'org-todo-regexp org-todo-regexp
4974 'org-complex-heading-regexp org-complex-heading-regexp
4975 'mouse-face 'highlight
4976 'help-echo
4977 (format "mouse-2 or RET jump to org file %s"
4978 (abbreviate-file-name buffer-file-name))))
4979 (d1 (calendar-absolute-from-gregorian date))
4981 (deadline-position-alist
4982 (mapcar (lambda (a) (and (setq mm (get-text-property
4983 0 'org-hd-marker a))
4984 (cons (marker-position mm) a)))
4985 deadline-results))
4986 (remove-re org-ts-regexp)
4987 (regexp
4988 (concat
4989 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4990 (regexp-quote
4991 (substring
4992 (format-time-string
4993 (car org-time-stamp-formats)
4994 (apply 'encode-time ; DATE bound by calendar
4995 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4996 1 11))
4997 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
4998 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4999 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5000 donep tmp priority category org-category-pos ee txt timestr tags
5001 b0 b3 e3 head todo-state end-of-match show-all)
5002 (goto-char (point-min))
5003 (while (setq end-of-match (re-search-forward regexp nil t))
5004 (setq b0 (match-beginning 0)
5005 b3 (match-beginning 3) e3 (match-end 3)
5006 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5007 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5008 (member todo-state
5009 org-agenda-repeating-timestamp-show-all)))
5010 (catch :skip
5011 (and (org-at-date-range-p) (throw :skip nil))
5012 (org-agenda-skip)
5013 (if (and (match-end 1)
5014 (not (= d1 (org-time-string-to-absolute
5015 (match-string 1) d1 nil show-all
5016 (current-buffer) b0))))
5017 (throw :skip nil))
5018 (if (and e3
5019 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5020 (throw :skip nil))
5021 (setq tmp (buffer-substring (max (point-min)
5022 (- b0 org-ds-keyword-length))
5024 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5025 inactivep (= (char-after b0) ?\[)
5026 deadlinep (string-match org-deadline-regexp tmp)
5027 scheduledp (string-match org-scheduled-regexp tmp)
5028 closedp (and org-agenda-include-inactive-timestamps
5029 (string-match org-closed-string tmp))
5030 clockp (and org-agenda-include-inactive-timestamps
5031 (or (string-match org-clock-string tmp)
5032 (string-match "]-+\\'" tmp)))
5033 donep (member todo-state org-done-keywords))
5034 (if (or scheduledp deadlinep closedp clockp
5035 (and donep org-agenda-skip-timestamp-if-done))
5036 (throw :skip t))
5037 (if (string-match ">" timestr)
5038 ;; substring should only run to end of time stamp
5039 (setq timestr (substring timestr 0 (match-end 0))))
5040 (setq marker (org-agenda-new-marker b0)
5041 category (org-get-category b0)
5042 org-category-pos (get-text-property b0 'org-category-position))
5043 (save-excursion
5044 (if (not (re-search-backward org-outline-regexp-bol nil t))
5045 (setq txt org-agenda-no-heading-message)
5046 (goto-char (match-beginning 0))
5047 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5048 (assoc (point) deadline-position-alist))
5049 (throw :skip nil))
5050 (setq hdmarker (org-agenda-new-marker)
5051 tags (org-get-tags-at))
5052 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5053 (setq head (or (match-string 1) ""))
5054 (setq txt (org-agenda-format-item
5055 (if inactivep org-agenda-inactive-leader nil)
5056 head category tags timestr
5057 remove-re)))
5058 (setq priority (org-get-priority txt))
5059 (org-add-props txt props
5060 'org-marker marker 'org-hd-marker hdmarker)
5061 (org-add-props txt nil 'priority priority
5062 'org-category category 'date date
5063 'org-category-position org-category-pos
5064 'todo-state todo-state
5065 'type "timestamp")
5066 (push txt ee))
5067 (if org-agenda-skip-additional-timestamps-same-entry
5068 (outline-next-heading)
5069 (goto-char end-of-match))))
5070 (nreverse ee)))
5072 (defun org-agenda-get-sexps ()
5073 "Return the sexp information for agenda display."
5074 (require 'diary-lib)
5075 (let* ((props (list 'face 'org-agenda-calendar-sexp
5076 'mouse-face 'highlight
5077 'help-echo
5078 (format "mouse-2 or RET jump to org file %s"
5079 (abbreviate-file-name buffer-file-name))))
5080 (regexp "^&?%%(")
5081 marker category extra org-category-pos ee txt tags entry
5082 result beg b sexp sexp-entry todo-state)
5083 (goto-char (point-min))
5084 (while (re-search-forward regexp nil t)
5085 (catch :skip
5086 (org-agenda-skip)
5087 (setq beg (match-beginning 0))
5088 (goto-char (1- (match-end 0)))
5089 (setq b (point))
5090 (forward-sexp 1)
5091 (setq sexp (buffer-substring b (point)))
5092 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5093 (org-trim (match-string 1))
5094 ""))
5095 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5096 (when result
5097 (setq marker (org-agenda-new-marker beg)
5098 category (org-get-category beg)
5099 org-category-pos (get-text-property beg 'org-category-position)
5100 todo-state (org-get-todo-state))
5102 (dolist (r (if (stringp result)
5103 (list result)
5104 result)) ;; we expect a list here
5105 (when (and org-agenda-diary-sexp-prefix
5106 (string-match org-agenda-diary-sexp-prefix r))
5107 (setq extra (match-string 0 r)
5108 r (replace-match "" nil nil r)))
5109 (if (string-match "\\S-" r)
5110 (setq txt r)
5111 (setq txt "SEXP entry returned empty string"))
5113 (setq txt (org-agenda-format-item
5114 extra txt category tags 'time))
5115 (org-add-props txt props 'org-marker marker)
5116 (org-add-props txt nil
5117 'org-category category 'date date 'todo-state todo-state
5118 'org-category-position org-category-pos
5119 'type "sexp")
5120 (push txt ee)))))
5121 (nreverse ee)))
5123 ;; Calendar sanity: define some functions that are independent of
5124 ;; `calendar-date-style'.
5125 ;; Normally I would like to use ISO format when calling the diary functions,
5126 ;; but to make sure we still have Emacs 22 compatibility we bind
5127 ;; also `european-calendar-style' and use european format
5128 (defun org-anniversary (year month day &optional mark)
5129 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5130 (org-no-warnings
5131 (let ((calendar-date-style 'european) (european-calendar-style t))
5132 (diary-anniversary day month year mark))))
5133 (defun org-cyclic (N year month day &optional mark)
5134 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5135 (org-no-warnings
5136 (let ((calendar-date-style 'european) (european-calendar-style t))
5137 (diary-cyclic N day month year mark))))
5138 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5139 "Like `diary-block', but with fixed (ISO) order of arguments."
5140 (org-no-warnings
5141 (let ((calendar-date-style 'european) (european-calendar-style t))
5142 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5143 (defun org-date (year month day &optional mark)
5144 "Like `diary-date', but with fixed (ISO) order of arguments."
5145 (org-no-warnings
5146 (let ((calendar-date-style 'european) (european-calendar-style t))
5147 (diary-date day month year mark))))
5148 (defalias 'org-float 'diary-float)
5150 ;; Define the` org-class' function
5151 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5152 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5153 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5154 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5155 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5156 `holidays', then any date that is known by the Emacs calendar to be a
5157 holiday will also be skipped."
5158 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5159 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5160 (d (calendar-absolute-from-gregorian date)))
5161 (and
5162 (<= date1 d)
5163 (<= d date2)
5164 (= (calendar-day-of-week date) dayname)
5165 (or (not skip-weeks)
5166 (progn
5167 (require 'cal-iso)
5168 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5169 (not (and (memq 'holidays skip-weeks)
5170 (calendar-check-holidays date)))
5171 entry)))
5173 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5174 "Like `org-class', but honor `calendar-date-style'.
5175 The order of the first 2 times 3 arguments depends on the variable
5176 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5177 So for American calendars, give this as MONTH DAY YEAR, for European as
5178 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5179 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5180 is any number of ISO weeks in the block period for which the item should
5181 be skipped.
5183 This function is here only for backward compatibility and it is deprecated,
5184 please use `org-class' instead."
5185 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5186 (date2 (org-order-calendar-date-args m2 d2 y2)))
5187 (org-class
5188 (nth 2 date1) (car date1) (nth 1 date1)
5189 (nth 2 date2) (car date2) (nth 1 date2)
5190 dayname skip-weeks)))
5191 (make-obsolete 'org-diary-class 'org-class "")
5193 (defvar org-agenda-show-log-scoped) ;; dynamically scope in ̀org-timeline' or`org-agenda-list'
5194 (defalias 'org-get-closed 'org-agenda-get-progress)
5195 (defun org-agenda-get-progress ()
5196 "Return the logged TODO entries for agenda display."
5197 (let* ((props (list 'mouse-face 'highlight
5198 'org-not-done-regexp org-not-done-regexp
5199 'org-todo-regexp org-todo-regexp
5200 'org-complex-heading-regexp org-complex-heading-regexp
5201 'help-echo
5202 (format "mouse-2 or RET jump to org file %s"
5203 (abbreviate-file-name buffer-file-name))))
5204 (items (if (consp org-agenda-show-log-scoped)
5205 org-agenda-show-log-scoped
5206 (if (eq org-agenda-show-log-scoped 'clockcheck)
5207 '(clock)
5208 org-agenda-log-mode-items)))
5209 (parts
5210 (delq nil
5211 (list
5212 (if (memq 'closed items) (concat "\\<" org-closed-string))
5213 (if (memq 'clock items) (concat "\\<" org-clock-string))
5214 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5215 (parts-re (if parts (mapconcat 'identity parts "\\|")
5216 (error "`org-agenda-log-mode-items' is empty")))
5217 (regexp (concat
5218 "\\(" parts-re "\\)"
5219 " *\\["
5220 (regexp-quote
5221 (substring
5222 (format-time-string
5223 (car org-time-stamp-formats)
5224 (apply 'encode-time ; DATE bound by calendar
5225 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5226 1 11))))
5227 (org-agenda-search-headline-for-time nil)
5228 marker hdmarker priority category org-category-pos tags closedp
5229 statep clockp state ee txt extra timestr rest clocked)
5230 (goto-char (point-min))
5231 (while (re-search-forward regexp nil t)
5232 (catch :skip
5233 (org-agenda-skip)
5234 (setq marker (org-agenda-new-marker (match-beginning 0))
5235 closedp (equal (match-string 1) org-closed-string)
5236 statep (equal (string-to-char (match-string 1)) ?-)
5237 clockp (not (or closedp statep))
5238 state (and statep (match-string 2))
5239 category (org-get-category (match-beginning 0))
5240 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
5241 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5242 (when (string-match "\\]" timestr)
5243 ;; substring should only run to end of time stamp
5244 (setq rest (substring timestr (match-end 0))
5245 timestr (substring timestr 0 (match-end 0)))
5246 (if (and (not closedp) (not statep)
5247 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5248 rest))
5249 (progn (setq timestr (concat (substring timestr 0 -1)
5250 "-" (match-string 1 rest) "]"))
5251 (setq clocked (match-string 2 rest)))
5252 (setq clocked "-")))
5253 (save-excursion
5254 (setq extra
5255 (cond
5256 ((not org-agenda-log-mode-add-notes) nil)
5257 (statep
5258 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5259 (match-string 1)))
5260 (clockp
5261 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5262 (match-string 1)))))
5263 (if (not (re-search-backward org-outline-regexp-bol nil t))
5264 (setq txt org-agenda-no-heading-message)
5265 (goto-char (match-beginning 0))
5266 (setq hdmarker (org-agenda-new-marker)
5267 tags (org-get-tags-at))
5268 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5269 (setq txt (match-string 1))
5270 (when extra
5271 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5272 (setq txt (concat (substring txt 0 (match-beginning 1))
5273 " - " extra " " (match-string 2 txt)))
5274 (setq txt (concat txt " - " extra))))
5275 (setq txt (org-agenda-format-item
5276 (cond
5277 (closedp "Closed: ")
5278 (statep (concat "State: (" state ")"))
5279 (t (concat "Clocked: (" clocked ")")))
5280 txt category tags timestr)))
5281 (setq priority 100000)
5282 (org-add-props txt props
5283 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5284 'priority priority 'org-category category
5285 'org-category-position org-category-pos
5286 'type "closed" 'date date
5287 'undone-face 'org-warning 'done-face 'org-agenda-done)
5288 (push txt ee))
5289 (goto-char (point-at-eol))))
5290 (nreverse ee)))
5292 (defun org-agenda-show-clocking-issues ()
5293 "Add overlays, showing issues with clocking.
5294 See also the user option `org-agenda-clock-consistency-checks'."
5295 (interactive)
5296 (let* ((pl org-agenda-clock-consistency-checks)
5297 (re (concat "^[ \t]*"
5298 org-clock-string
5299 "[ \t]+"
5300 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5301 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5302 (tlstart 0.)
5303 (tlend 0.)
5304 (maxtime (org-hh:mm-string-to-minutes
5305 (or (plist-get pl :max-duration) "24:00")))
5306 (mintime (org-hh:mm-string-to-minutes
5307 (or (plist-get pl :min-duration) 0)))
5308 (maxgap (org-hh:mm-string-to-minutes
5309 ;; default 30:00 means never complain
5310 (or (plist-get pl :max-gap) "30:00")))
5311 (gapok (mapcar 'org-hh:mm-string-to-minutes
5312 (plist-get pl :gap-ok-around)))
5313 (def-face (or (plist-get pl :default-face)
5314 '((:background "DarkRed") (:foreground "white"))))
5315 issue face m te ts dt ov)
5316 (goto-char (point-min))
5317 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5318 (setq issue nil face def-face)
5319 (catch 'next
5320 (setq m (org-get-at-bol 'org-marker)
5321 te nil ts nil)
5322 (unless (and m (markerp m))
5323 (setq issue "No valid clock line") (throw 'next t))
5324 (org-with-point-at m
5325 (save-excursion
5326 (goto-char (point-at-bol))
5327 (unless (looking-at re)
5328 (error "No valid Clock line")
5329 (throw 'next t))
5330 (unless (match-end 3)
5331 (setq issue "No end time"
5332 face (or (plist-get pl :no-end-time-face) face))
5333 (throw 'next t))
5334 (setq ts (match-string 1)
5335 te (match-string 3)
5336 ts (org-float-time
5337 (apply 'encode-time (org-parse-time-string ts)))
5338 te (org-float-time
5339 (apply 'encode-time (org-parse-time-string te)))
5340 dt (- te ts))))
5341 (cond
5342 ((> dt (* 60 maxtime))
5343 ;; a very long clocking chunk
5344 (setq issue (format "Clocking interval is very long: %s"
5345 (org-minutes-to-hh:mm-string
5346 (floor (/ (float dt) 60.))))
5347 face (or (plist-get pl :long-face) face)))
5348 ((< dt (* 60 mintime))
5349 ;; a very short clocking chunk
5350 (setq issue (format "Clocking interval is very short: %s"
5351 (org-minutes-to-hh:mm-string
5352 (floor (/ (float dt) 60.))))
5353 face (or (plist-get pl :short-face) face)))
5354 ((and (> tlend 0) (< ts tlend))
5355 ;; Two clock entries are overlapping
5356 (setq issue (format "Clocking overlap: %d minutes"
5357 (/ (- tlend ts) 60))
5358 face (or (plist-get pl :overlap-face) face)))
5359 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5360 ;; There is a gap, lets see if we need to report it
5361 (unless (org-agenda-check-clock-gap tlend ts gapok)
5362 (setq issue (format "Clocking gap: %d minutes"
5363 (/ (- ts tlend) 60))
5364 face (or (plist-get pl :gap-face) face))))
5365 (t nil)))
5366 (setq tlend (or te tlend) tlstart (or ts tlstart))
5367 (when issue
5368 ;; OK, there was some issue, add an overlay to show the issue
5369 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5370 (overlay-put ov 'before-string
5371 (concat
5372 (org-add-props
5373 (format "%-43s" (concat " " issue))
5375 'face face)
5376 "\n"))
5377 (overlay-put ov 'evaporate t)))))
5379 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5380 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5381 (catch 'exit
5382 (unless ok-list
5383 ;; there are no OK times for gaps...
5384 (throw 'exit nil))
5385 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5386 ;; This is more than 24 hours, so it is OK.
5387 ;; because we have at least one OK time, that must be in the
5388 ;; 24 hour interval.
5389 (throw 'exit t))
5390 ;; We have a shorter gap.
5391 ;; Now we have to get the minute of the day when these times are
5392 (let* ((t1dec (decode-time (seconds-to-time t1)))
5393 (t2dec (decode-time (seconds-to-time t2)))
5394 ;; compute the minute on the day
5395 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5396 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5397 (when (< min2 min1)
5398 ;; if min2 is smaller than min1, this means it is on the next day.
5399 ;; Wrap it to after midnight.
5400 (setq min2 (+ min2 1440)))
5401 ;; Now check if any of the OK times is in the gap
5402 (mapc (lambda (x)
5403 ;; Wrap the time to after midnight if necessary
5404 (if (< x min1) (setq x (+ x 1440)))
5405 ;; Check if in interval
5406 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5407 ok-list)
5408 ;; Nope, this gap is not OK
5409 nil)))
5411 (defun org-agenda-get-deadlines ()
5412 "Return the deadline information for agenda display."
5413 (let* ((props (list 'mouse-face 'highlight
5414 'org-not-done-regexp org-not-done-regexp
5415 'org-todo-regexp org-todo-regexp
5416 'org-complex-heading-regexp org-complex-heading-regexp
5417 'help-echo
5418 (format "mouse-2 or RET jump to org file %s"
5419 (abbreviate-file-name buffer-file-name))))
5420 (regexp org-deadline-time-regexp)
5421 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5422 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5423 d2 diff dfrac wdays pos pos1 category org-category-pos
5424 tags suppress-prewarning ee txt head face s todo-state
5425 show-all upcomingp donep timestr)
5426 (goto-char (point-min))
5427 (while (re-search-forward regexp nil t)
5428 (setq suppress-prewarning nil)
5429 (catch :skip
5430 (org-agenda-skip)
5431 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5432 (save-match-data
5433 (string-match org-scheduled-time-regexp
5434 (buffer-substring (point-at-bol)
5435 (point-at-eol)))))
5436 (setq suppress-prewarning
5437 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5438 org-agenda-skip-deadline-prewarning-if-scheduled
5439 0)))
5440 (setq s (match-string 1)
5441 txt nil
5442 pos (1- (match-beginning 1))
5443 todo-state (save-match-data (org-get-todo-state))
5444 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5445 (member todo-state
5446 org-agenda-repeating-timestamp-show-all))
5447 d2 (org-time-string-to-absolute
5448 (match-string 1) d1 'past show-all
5449 (current-buffer) pos)
5450 diff (- d2 d1)
5451 wdays (if suppress-prewarning
5452 (let ((org-deadline-warning-days suppress-prewarning))
5453 (org-get-wdays s))
5454 (org-get-wdays s))
5455 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5456 upcomingp (and todayp (> diff 0)))
5457 ;; When to show a deadline in the calendar:
5458 ;; If the expiration is within wdays warning time.
5459 ;; Past-due deadlines are only shown on the current date
5460 (if (and (or (and (<= diff wdays)
5461 (and todayp (not org-agenda-only-exact-dates)))
5462 (= diff 0)))
5463 (save-excursion
5464 ;; (setq todo-state (org-get-todo-state))
5465 (setq donep (member todo-state org-done-keywords))
5466 (if (and donep
5467 (or org-agenda-skip-deadline-if-done
5468 (not (= diff 0))))
5469 (setq txt nil)
5470 (setq category (org-get-category)
5471 org-category-pos (get-text-property (point) 'org-category-position))
5472 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5473 (setq txt org-agenda-no-heading-message)
5474 (goto-char (match-end 0))
5475 (setq pos1 (match-beginning 0))
5476 (setq tags (org-get-tags-at pos1))
5477 (setq head (buffer-substring-no-properties
5478 (point)
5479 (progn (skip-chars-forward "^\r\n")
5480 (point))))
5481 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5482 (setq timestr
5483 (concat (substring s (match-beginning 1)) " "))
5484 (setq timestr 'time))
5485 (setq txt (org-agenda-format-item
5486 (if (= diff 0)
5487 (car org-agenda-deadline-leaders)
5488 (if (functionp
5489 (nth 1 org-agenda-deadline-leaders))
5490 (funcall
5491 (nth 1 org-agenda-deadline-leaders)
5492 diff date)
5493 (format (nth 1 org-agenda-deadline-leaders)
5494 diff)))
5495 head category tags
5496 (if (not (= diff 0)) nil timestr)))))
5497 (when txt
5498 (setq face (org-agenda-deadline-face dfrac))
5499 (org-add-props txt props
5500 'org-marker (org-agenda-new-marker pos)
5501 'org-hd-marker (org-agenda-new-marker pos1)
5502 'priority (+ (- diff)
5503 (org-get-priority txt))
5504 'org-category category
5505 'org-category-position org-category-pos
5506 'todo-state todo-state
5507 'type (if upcomingp "upcoming-deadline" "deadline")
5508 'date (if upcomingp date d2)
5509 'face (if donep 'org-agenda-done face)
5510 'undone-face face 'done-face 'org-agenda-done)
5511 (push txt ee))))))
5512 (nreverse ee)))
5514 (defun org-agenda-deadline-face (fraction)
5515 "Return the face to displaying a deadline item.
5516 FRACTION is what fraction of the head-warning time has passed."
5517 (let ((faces org-agenda-deadline-faces) f)
5518 (catch 'exit
5519 (while (setq f (pop faces))
5520 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5522 (defun org-agenda-get-scheduled (&optional deadline-results)
5523 "Return the scheduled information for agenda display."
5524 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5525 'org-todo-regexp org-todo-regexp
5526 'org-complex-heading-regexp org-complex-heading-regexp
5527 'done-face 'org-agenda-done
5528 'mouse-face 'highlight
5529 'help-echo
5530 (format "mouse-2 or RET jump to org file %s"
5531 (abbreviate-file-name buffer-file-name))))
5532 (regexp org-scheduled-time-regexp)
5533 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5534 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5536 (deadline-position-alist
5537 (mapcar (lambda (a) (and (setq mm (get-text-property
5538 0 'org-hd-marker a))
5539 (cons (marker-position mm) a)))
5540 deadline-results))
5541 d2 diff pos pos1 category org-category-pos tags donep
5542 ee txt head pastschedp todo-state face timestr s habitp show-all
5543 did-habit-check-p)
5544 (goto-char (point-min))
5545 (while (re-search-forward regexp nil t)
5546 (catch :skip
5547 (org-agenda-skip)
5548 (setq s (match-string 1)
5549 txt nil
5550 pos (1- (match-beginning 1))
5551 todo-state (save-match-data (org-get-todo-state))
5552 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5553 (member todo-state
5554 org-agenda-repeating-timestamp-show-all))
5555 d2 (org-time-string-to-absolute
5556 (match-string 1) d1 'past show-all
5557 (current-buffer) pos)
5558 diff (- d2 d1))
5559 (setq pastschedp (and todayp (< diff 0)))
5560 (setq did-habit-check-p nil)
5561 ;; When to show a scheduled item in the calendar:
5562 ;; If it is on or past the date.
5563 (when (or (and (< diff 0)
5564 (< (abs diff) org-scheduled-past-days)
5565 (and todayp (not org-agenda-only-exact-dates)))
5566 (= diff 0)
5567 ;; org-is-habit-p uses org-entry-get, which is expansive
5568 ;; so we go extra mile to only call it once
5569 (and todayp
5570 org-habit-show-all-today
5571 (setq did-habit-check-p t)
5572 (setq habitp (and (functionp 'org-is-habit-p)
5573 (org-is-habit-p)))))
5574 (save-excursion
5575 (setq donep (member todo-state org-done-keywords))
5576 (if (and donep
5577 (or org-agenda-skip-scheduled-if-done
5578 (not (= diff 0))
5579 (and (functionp 'org-is-habit-p)
5580 (org-is-habit-p))))
5581 (setq txt nil)
5582 (setq habitp (if did-habit-check-p habitp
5583 (and (functionp 'org-is-habit-p)
5584 (org-is-habit-p))))
5585 (setq category (org-get-category)
5586 org-category-pos (get-text-property (point) 'org-category-position))
5587 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5588 (setq txt org-agenda-no-heading-message)
5589 (goto-char (match-end 0))
5590 (setq pos1 (match-beginning 0))
5591 (if habitp
5592 (if (or (not org-habit-show-habits)
5593 (and (not todayp)
5594 org-habit-show-habits-only-for-today))
5595 (throw :skip nil))
5596 (if (and
5597 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5598 (and org-agenda-skip-scheduled-if-deadline-is-shown
5599 pastschedp))
5600 (setq mm (assoc pos1 deadline-position-alist)))
5601 (throw :skip nil)))
5602 (setq tags (org-get-tags-at))
5603 (setq head (buffer-substring-no-properties
5604 (point)
5605 (progn (skip-chars-forward "^\r\n") (point))))
5606 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5607 (setq timestr
5608 (concat (substring s (match-beginning 1)) " "))
5609 (setq timestr 'time))
5610 (setq txt (org-agenda-format-item
5611 (if (= diff 0)
5612 (car org-agenda-scheduled-leaders)
5613 (format (nth 1 org-agenda-scheduled-leaders)
5614 (- 1 diff)))
5615 head category tags
5616 (if (not (= diff 0)) nil timestr)
5617 nil habitp))))
5618 (when txt
5619 (setq face
5620 (cond
5621 ((and (not habitp) pastschedp)
5622 'org-scheduled-previously)
5623 (todayp 'org-scheduled-today)
5624 (t 'org-scheduled))
5625 habitp (and habitp (org-habit-parse-todo)))
5626 (org-add-props txt props
5627 'undone-face face
5628 'face (if donep 'org-agenda-done face)
5629 'org-marker (org-agenda-new-marker pos)
5630 'org-hd-marker (org-agenda-new-marker pos1)
5631 'type (if pastschedp "past-scheduled" "scheduled")
5632 'date (if pastschedp d2 date)
5633 'priority (if habitp
5634 (org-habit-get-priority habitp)
5635 (+ 94 (- 5 diff) (org-get-priority txt)))
5636 'org-category category
5637 'org-category-position org-category-pos
5638 'org-habit-p habitp
5639 'todo-state todo-state)
5640 (push txt ee))))))
5641 (nreverse ee)))
5643 (defun org-agenda-get-blocks ()
5644 "Return the date-range information for agenda display."
5645 (let* ((props (list 'face nil
5646 'org-not-done-regexp org-not-done-regexp
5647 'org-todo-regexp org-todo-regexp
5648 'org-complex-heading-regexp org-complex-heading-regexp
5649 'mouse-face 'highlight
5650 'help-echo
5651 (format "mouse-2 or RET jump to org file %s"
5652 (abbreviate-file-name buffer-file-name))))
5653 (regexp org-tr-regexp)
5654 (d0 (calendar-absolute-from-gregorian date))
5655 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5656 todo-state tags pos head donep)
5657 (goto-char (point-min))
5658 (while (re-search-forward regexp nil t)
5659 (catch :skip
5660 (org-agenda-skip)
5661 (setq pos (point))
5662 (let ((start-time (match-string 1))
5663 (end-time (match-string 2)))
5664 (setq s1 (match-string 1)
5665 s2 (match-string 2)
5666 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5667 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5668 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5669 ;; Only allow days between the limits, because the normal
5670 ;; date stamps will catch the limits.
5671 (save-excursion
5672 (setq todo-state (org-get-todo-state))
5673 (setq donep (member todo-state org-done-keywords))
5674 (if (and donep org-agenda-skip-timestamp-if-done)
5675 (throw :skip t))
5676 (setq marker (org-agenda-new-marker (point)))
5677 (setq category (org-get-category)
5678 org-category-pos (get-text-property (point) 'org-category-position))
5679 (if (not (re-search-backward org-outline-regexp-bol nil t))
5680 (setq txt org-agenda-no-heading-message)
5681 (goto-char (match-beginning 0))
5682 (setq hdmarker (org-agenda-new-marker (point)))
5683 (setq tags (org-get-tags-at))
5684 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5685 (setq head (match-string 1))
5686 (let ((remove-re
5687 (if org-agenda-remove-timeranges-from-blocks
5688 (concat
5689 "<" (regexp-quote s1) ".*?>"
5690 "--"
5691 "<" (regexp-quote s2) ".*?>")
5692 nil)))
5693 (setq txt (org-agenda-format-item
5694 (format
5695 (nth (if (= d1 d2) 0 1)
5696 org-agenda-timerange-leaders)
5697 (1+ (- d0 d1)) (1+ (- d2 d1)))
5698 head category tags
5699 (cond ((and (= d1 d0) (= d2 d0))
5700 (concat "<" start-time ">--<" end-time ">"))
5701 ((= d1 d0)
5702 (concat "<" start-time ">"))
5703 ((= d2 d0)
5704 (concat "<" end-time ">"))
5705 (t nil))
5706 remove-re))))
5707 (org-add-props txt props
5708 'org-marker marker 'org-hd-marker hdmarker
5709 'type "block" 'date date
5710 'todo-state todo-state
5711 'priority (org-get-priority txt) 'org-category category
5712 'org-category-position org-category-pos)
5713 (push txt ee))))
5714 (goto-char pos)))
5715 ;; Sort the entries by expiration date.
5716 (nreverse ee)))
5718 ;;; Agenda presentation and sorting
5720 (defvar org-prefix-has-time nil
5721 "A flag, set by `org-compile-prefix-format'.
5722 The flag is set if the currently compiled format contains a `%t'.")
5723 (defvar org-prefix-has-tag nil
5724 "A flag, set by `org-compile-prefix-format'.
5725 The flag is set if the currently compiled format contains a `%T'.")
5726 (defvar org-prefix-has-effort nil
5727 "A flag, set by `org-compile-prefix-format'.
5728 The flag is set if the currently compiled format contains a `%e'.")
5729 (defvar org-prefix-category-length nil
5730 "Used by `org-compile-prefix-format' to remember the category field width.")
5731 (defvar org-prefix-category-max-length nil
5732 "Used by `org-compile-prefix-format' to remember the category field width.")
5734 (defun org-agenda-get-category-icon (category)
5735 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5736 (dolist (entry org-agenda-category-icon-alist)
5737 (when (org-string-match-p (car entry) category)
5738 (if (listp (cadr entry))
5739 (return (cadr entry))
5740 (return (apply 'create-image (cdr entry)))))))
5742 (defun org-agenda-format-item (extra txt &optional category tags dotime
5743 remove-re habitp)
5744 "Format TXT to be inserted into the agenda buffer.
5745 In particular, it adds the prefix and corresponding text properties. EXTRA
5746 must be a string and replaces the `%s' specifier in the prefix format.
5747 CATEGORY (string, symbol or nil) may be used to overrule the default
5748 category taken from local variable or file name. It will replace the `%c'
5749 specifier in the format. DOTIME, when non-nil, indicates that a
5750 time-of-day should be extracted from TXT for sorting of this entry, and for
5751 the `%t' specifier in the format. When DOTIME is a string, this string is
5752 searched for a time before TXT is. TAGS can be the tags of the headline.
5753 Any match of REMOVE-RE will be removed from TXT."
5754 ;; We keep the org-prefix-* variable values along with a compiled
5755 ;; formatter, so that multiple agendas existing at the same time, do
5756 ;; not step on each other toes.
5758 ;; It was inconvenient to make these variables buffer local in
5759 ;; Agenda buffers, because this function expects to be called with
5760 ;; the buffer where item comes from being current, and not agenda
5761 ;; buffer
5762 (let* ((bindings (car org-prefix-format-compiled))
5763 (formatter (cadr org-prefix-format-compiled)))
5764 (loop for (var value) in bindings
5765 do (set var value))
5766 (save-match-data
5767 ;; Diary entries sometimes have extra whitespace at the beginning
5768 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5770 ;; Fix the tags part in txt
5771 (setq txt (org-agenda-fix-displayed-tags
5772 txt tags
5773 org-agenda-show-inherited-tags
5774 org-agenda-hide-tags-regexp))
5775 (let* ((category (or category
5776 (if (stringp org-category)
5777 org-category
5778 (and org-category (symbol-name org-category)))
5779 (if buffer-file-name
5780 (file-name-sans-extension
5781 (file-name-nondirectory buffer-file-name))
5782 "")))
5783 (category-icon (org-agenda-get-category-icon category))
5784 (category-icon (if category-icon
5785 (propertize " " 'display category-icon)
5786 ""))
5787 ;; time, tag, effort are needed for the eval of the prefix format
5788 (tag (if tags (nth (1- (length tags)) tags) ""))
5789 time effort neffort
5790 (ts (if dotime (concat
5791 (if (stringp dotime) dotime "")
5792 (and org-agenda-search-headline-for-time txt))))
5793 (time-of-day (and dotime (org-get-time-of-day ts)))
5794 stamp plain s0 s1 s2 rtn srp l
5795 duration thecategory)
5796 (and (derived-mode-p 'org-mode) buffer-file-name
5797 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5798 (when (and dotime time-of-day)
5799 ;; Extract starting and ending time and move them to prefix
5800 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5801 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5802 (setq s0 (match-string 0 ts)
5803 srp (and stamp (match-end 3))
5804 s1 (match-string (if plain 1 2) ts)
5805 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5807 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5808 ;; them, we might want to remove them there to avoid duplication.
5809 ;; The user can turn this off with a variable.
5810 (if (and org-prefix-has-time
5811 org-agenda-remove-times-when-in-prefix (or stamp plain)
5812 (string-match (concat (regexp-quote s0) " *") txt)
5813 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5814 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5815 (= (match-beginning 0) 0)
5817 (setq txt (replace-match "" nil nil txt))))
5818 ;; Normalize the time(s) to 24 hour
5819 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5820 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5822 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5823 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5824 (setq s2
5825 (org-minutes-to-hh:mm-string
5826 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5828 ;; Compute the duration
5829 (when s2
5830 (setq duration (- (org-hh:mm-string-to-minutes s2)
5831 (org-hh:mm-string-to-minutes s1)))))
5833 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5834 txt)
5835 ;; Tags are in the string
5836 (if (or (eq org-agenda-remove-tags t)
5837 (and org-agenda-remove-tags
5838 org-prefix-has-tag))
5839 (setq txt (replace-match "" t t txt))
5840 (setq txt (replace-match
5841 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5842 (match-string 2 txt))
5843 t t txt))))
5844 (when (derived-mode-p 'org-mode)
5845 (setq effort
5846 (condition-case nil
5847 (org-get-effort
5848 (or (get-text-property 0 'org-hd-marker txt)
5849 (get-text-property 0 'org-marker txt)))
5850 (error nil)))
5851 (when effort
5852 (setq neffort (org-duration-string-to-minutes effort)
5853 effort (setq effort (concat "[" effort "]")))))
5854 ;; prevent erroring out with %e format when there is no effort
5855 (or effort (setq effort ""))
5857 (when remove-re
5858 (while (string-match remove-re txt)
5859 (setq txt (replace-match "" t t txt))))
5861 ;; Set org-heading property on `txt' to mark the start of the
5862 ;; heading.
5863 (add-text-properties 0 (length txt) '(org-heading t) txt)
5865 ;; Prepare the variables needed in the eval of the compiled format
5866 (setq time (cond (s2 (concat
5867 (org-agenda-time-of-day-to-ampm-maybe s1)
5868 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5869 (if org-agenda-timegrid-use-ampm " ")))
5870 (s1 (concat
5871 (org-agenda-time-of-day-to-ampm-maybe s1)
5872 (if org-agenda-timegrid-use-ampm
5873 "........ "
5874 "......")))
5875 (t ""))
5876 extra (or (and (not habitp) extra) "")
5877 category (if (symbolp category) (symbol-name category) category)
5878 thecategory (copy-sequence category))
5879 (if (string-match org-bracket-link-regexp category)
5880 (progn
5881 (setq l (if (match-end 3)
5882 (- (match-end 3) (match-beginning 3))
5883 (- (match-end 1) (match-beginning 1))))
5884 (when (< l (or org-prefix-category-length 0))
5885 (setq category (copy-sequence category))
5886 (org-add-props category nil
5887 'extra-space (make-string
5888 (- org-prefix-category-length l 1) ?\ ))))
5889 (if (and org-prefix-category-max-length
5890 (>= (length category) org-prefix-category-max-length))
5891 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5892 ;; Evaluate the compiled format
5893 (setq rtn (concat (eval formatter) txt))
5895 ;; And finally add the text properties
5896 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5897 (org-add-props rtn nil
5898 'org-category (if thecategory (downcase thecategory) category)
5899 'tags (mapcar 'org-downcase-keep-props tags)
5900 'org-highest-priority org-highest-priority
5901 'org-lowest-priority org-lowest-priority
5902 'time-of-day time-of-day
5903 'duration duration
5904 'effort effort
5905 'effort-minutes neffort
5906 'txt txt
5907 'time time
5908 'extra extra
5909 'format org-prefix-format-compiled
5910 'dotime dotime)))))
5912 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5913 "Remove tags string from TXT, and add a modified list of tags.
5914 The modified list may contain inherited tags, and tags matched by
5915 `org-agenda-hide-tags-regexp' will be removed."
5916 (when (or add-inherited hide-re)
5917 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5918 (setq txt (substring txt 0 (match-beginning 0))))
5919 (setq tags
5920 (delq nil
5921 (mapcar (lambda (tg)
5922 (if (or (and hide-re (string-match hide-re tg))
5923 (and (not add-inherited)
5924 (get-text-property 0 'inherited tg)))
5926 tg))
5927 tags)))
5928 (when tags
5929 (let ((have-i (get-text-property 0 'inherited (car tags)))
5931 (setq txt (concat txt " :"
5932 (mapconcat
5933 (lambda (x)
5934 (setq i (get-text-property 0 'inherited x))
5935 (if (and have-i (not i))
5936 (progn
5937 (setq have-i nil)
5938 (concat ":" x))
5940 tags ":")
5941 (if have-i "::" ":"))))))
5942 txt)
5944 (defun org-downcase-keep-props (s)
5945 (let ((props (text-properties-at 0 s)))
5946 (setq s (downcase s))
5947 (add-text-properties 0 (length s) props s)
5950 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5951 (defvar org-agenda-sorting-strategy-selected nil)
5953 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5954 (catch 'exit
5955 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5956 ((and todayp (member 'today (car org-agenda-time-grid))))
5957 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5958 ((member 'weekly (car org-agenda-time-grid)))
5959 (t (throw 'exit list)))
5960 (let* ((have (delq nil (mapcar
5961 (lambda (x) (get-text-property 1 'time-of-day x))
5962 list)))
5963 (string (nth 1 org-agenda-time-grid))
5964 (gridtimes (nth 2 org-agenda-time-grid))
5965 (req (car org-agenda-time-grid))
5966 (remove (member 'remove-match req))
5967 new time)
5968 (if (and (member 'require-timed req) (not have))
5969 ;; don't show empty grid
5970 (throw 'exit list))
5971 (while (setq time (pop gridtimes))
5972 (unless (and remove (member time have))
5973 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5974 (push (org-agenda-format-item
5975 nil string "" nil
5976 (concat (substring time 0 -2) ":" (substring time -2)))
5977 new)
5978 (put-text-property
5979 2 (length (car new)) 'face 'org-time-grid (car new))))
5980 (when (and todayp org-agenda-show-current-time-in-grid)
5981 (push (org-agenda-format-item
5983 org-agenda-current-time-string
5984 "" nil
5985 (format-time-string "%H:%M "))
5986 new)
5987 (put-text-property
5988 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5990 (if (member 'time-up org-agenda-sorting-strategy-selected)
5991 (append new list)
5992 (append list new)))))
5994 (defun org-compile-prefix-format (key)
5995 "Compile the prefix format into a Lisp form that can be evaluated.
5996 The resulting form and associated variable bindings is returned
5997 and stored in the variable `org-prefix-format-compiled'."
5998 (setq org-prefix-has-time nil org-prefix-has-tag nil
5999 org-prefix-category-length nil
6000 org-prefix-has-effort nil)
6001 (let ((s (cond
6002 ((stringp org-agenda-prefix-format)
6003 org-agenda-prefix-format)
6004 ((assq key org-agenda-prefix-format)
6005 (cdr (assq key org-agenda-prefix-format)))
6006 (t " %-12:c%?-12t% s")))
6007 (start 0)
6008 varform vars var e c f opt)
6009 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
6010 s start)
6011 (setq var (or (cdr (assoc (match-string 4 s)
6012 '(("c" . category) ("t" . time) ("s" . extra)
6013 ("i" . category-icon) ("T" . tag) ("e" . effort))))
6014 'eval)
6015 c (or (match-string 3 s) "")
6016 opt (match-beginning 1)
6017 start (1+ (match-beginning 0)))
6018 (if (equal var 'time) (setq org-prefix-has-time t))
6019 (if (equal var 'tag) (setq org-prefix-has-tag t))
6020 (if (equal var 'effort) (setq org-prefix-has-effort t))
6021 (setq f (concat "%" (match-string 2 s) "s"))
6022 (when (equal var 'category)
6023 (setq org-prefix-category-length
6024 (floor (abs (string-to-number (match-string 2 s)))))
6025 (setq org-prefix-category-max-length
6026 (let ((x (match-string 2 s)))
6027 (save-match-data
6028 (if (string-match "\\.[0-9]+" x)
6029 (string-to-number (substring (match-string 0 x) 1)))))))
6030 (if (eq var 'eval)
6031 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6032 (if opt
6033 (setq varform
6034 `(if (equal "" ,var)
6036 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6037 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6038 (setq s (replace-match "%s" t nil s))
6039 (push varform vars))
6040 (setq vars (nreverse vars))
6041 (with-current-buffer org-agenda-buffer
6042 (setq org-prefix-format-compiled
6043 (list
6044 `((org-prefix-has-time ,org-prefix-has-time)
6045 (org-prefix-has-tag ,org-prefix-has-tag)
6046 (org-prefix-category-length ,org-prefix-category-length)
6047 (org-prefix-has-effort ,org-prefix-has-effort))
6048 `(format ,s ,@vars))))))
6050 (defun org-set-sorting-strategy (key)
6051 (if (symbolp (car org-agenda-sorting-strategy))
6052 ;; the old format
6053 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6054 (setq org-agenda-sorting-strategy-selected
6055 (or (cdr (assq key org-agenda-sorting-strategy))
6056 (cdr (assq 'agenda org-agenda-sorting-strategy))
6057 '(time-up category-keep priority-down)))))
6059 (defun org-get-time-of-day (s &optional string mod24)
6060 "Check string S for a time of day.
6061 If found, return it as a military time number between 0 and 2400.
6062 If not found, return nil.
6063 The optional STRING argument forces conversion into a 5 character wide string
6064 HH:MM."
6065 (save-match-data
6066 (when
6067 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6068 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6069 (let* ((h (string-to-number (match-string 1 s)))
6070 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6071 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6072 (am-p (equal ampm "am"))
6073 (h1 (cond ((not ampm) h)
6074 ((= h 12) (if am-p 0 12))
6075 (t (+ h (if am-p 0 12)))))
6076 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6077 (mod h1 24) h1))
6078 (t0 (+ (* 100 h2) m))
6079 (t1 (concat (if (>= h1 24) "+" " ")
6080 (if (and org-agenda-time-leading-zero
6081 (< t0 1000)) "0" "")
6082 (if (< t0 100) "0" "")
6083 (if (< t0 10) "0" "")
6084 (int-to-string t0))))
6085 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6087 (defvar org-agenda-before-sorting-filter-function nil
6088 "Function to be applied to agenda items prior to sorting.
6089 Prior to sorting also means just before they are inserted into the agenda.
6091 To aid sorting, you may revisit the original entries and add more text
6092 properties which will later be used by the sorting functions.
6094 The function should take a string argument, an agenda line.
6095 It has access to the text properties in that line, which contain among
6096 other things, the property `org-hd-marker' that points to the entry
6097 where the line comes from. Note that not all lines going into the agenda
6098 have this property, only most.
6100 The function should return the modified string. It is probably best
6101 to ONLY change text properties.
6103 You can also use this function as a filter, by returning nil for lines
6104 you don't want to have in the agenda at all. For this application, you
6105 could bind the variable in the options section of a custom command.")
6107 (defun org-finalize-agenda-entries (list &optional nosort)
6108 "Sort and concatenate the agenda items."
6109 (setq list (mapcar 'org-agenda-highlight-todo list))
6110 (if nosort
6111 list
6112 (when org-agenda-before-sorting-filter-function
6113 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
6114 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
6116 (defun org-agenda-highlight-todo (x)
6117 (let ((org-done-keywords org-done-keywords-for-agenda)
6118 (case-fold-search nil)
6120 (if (eq x 'line)
6121 (save-excursion
6122 (beginning-of-line 1)
6123 (setq re (org-get-at-bol 'org-todo-regexp))
6124 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6125 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6126 (add-text-properties (match-beginning 0) (match-end 1)
6127 (list 'face (org-get-todo-face 1)))
6128 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6129 (delete-region (match-beginning 1) (1- (match-end 0)))
6130 (goto-char (match-beginning 1))
6131 (insert (format org-agenda-todo-keyword-format s)))))
6132 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6133 (setq re (get-text-property 0 'org-todo-regexp x))
6134 (when (and re
6135 ;; Test `pl' because if there's no heading content,
6136 ;; there's no point matching to highlight. Note
6137 ;; that if we didn't test `pl' first, and there
6138 ;; happened to be no keyword from `org-todo-regexp'
6139 ;; on this heading line, then the `equal' comparison
6140 ;; afterwards would spuriously succeed in the case
6141 ;; where `pl' is nil -- causing an args-out-of-range
6142 ;; error when we try to add text properties to text
6143 ;; that isn't there.
6145 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6146 x pl) pl))
6147 (add-text-properties
6148 (or (match-end 1) (match-end 0)) (match-end 0)
6149 (list 'face (org-get-todo-face (match-string 2 x)))
6151 (when (match-end 1)
6152 (setq x (concat (substring x 0 (match-end 1))
6153 (format org-agenda-todo-keyword-format
6154 (match-string 2 x))
6155 (org-add-props " " (text-properties-at 0 x))
6156 (substring x (match-end 3)))))))
6157 x)))
6159 (defsubst org-cmp-priority (a b)
6160 "Compare the priorities of string A and B."
6161 (let ((pa (or (get-text-property 1 'priority a) 0))
6162 (pb (or (get-text-property 1 'priority b) 0)))
6163 (cond ((> pa pb) +1)
6164 ((< pa pb) -1)
6165 (t nil))))
6167 (defsubst org-cmp-effort (a b)
6168 "Compare the effort values of string A and B."
6169 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6170 (ea (or (get-text-property 1 'effort-minutes a) def))
6171 (eb (or (get-text-property 1 'effort-minutes b) def)))
6172 (cond ((> ea eb) +1)
6173 ((< ea eb) -1)
6174 (t nil))))
6176 (defsubst org-cmp-category (a b)
6177 "Compare the string values of categories of strings A and B."
6178 (let ((ca (or (get-text-property 1 'org-category a) ""))
6179 (cb (or (get-text-property 1 'org-category b) "")))
6180 (cond ((string-lessp ca cb) -1)
6181 ((string-lessp cb ca) +1)
6182 (t nil))))
6184 (defsubst org-cmp-todo-state (a b)
6185 "Compare the todo states of strings A and B."
6186 (let* ((ma (or (get-text-property 1 'org-marker a)
6187 (get-text-property 1 'org-hd-marker a)))
6188 (mb (or (get-text-property 1 'org-marker b)
6189 (get-text-property 1 'org-hd-marker b)))
6190 (fa (and ma (marker-buffer ma)))
6191 (fb (and mb (marker-buffer mb)))
6192 (todo-kwds
6193 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6194 (and fb (with-current-buffer fb org-todo-keywords-1))))
6195 (ta (or (get-text-property 1 'todo-state a) ""))
6196 (tb (or (get-text-property 1 'todo-state b) ""))
6197 (la (- (length (member ta todo-kwds))))
6198 (lb (- (length (member tb todo-kwds))))
6199 (donepa (member ta org-done-keywords-for-agenda))
6200 (donepb (member tb org-done-keywords-for-agenda)))
6201 (cond ((and donepa (not donepb)) -1)
6202 ((and (not donepa) donepb) +1)
6203 ((< la lb) -1)
6204 ((< lb la) +1)
6205 (t nil))))
6207 (defsubst org-cmp-alpha (a b)
6208 "Compare the headlines, alphabetically."
6209 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6210 (plb (text-property-any 0 (length b) 'org-heading t b))
6211 (ta (and pla (substring a pla)))
6212 (tb (and plb (substring b plb))))
6213 (when pla
6214 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6215 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6216 (setq ta (substring ta (match-end 0))))
6217 (setq ta (downcase ta)))
6218 (when plb
6219 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6220 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6221 (setq tb (substring tb (match-end 0))))
6222 (setq tb (downcase tb)))
6223 (cond ((not ta) +1)
6224 ((not tb) -1)
6225 ((string-lessp ta tb) -1)
6226 ((string-lessp tb ta) +1)
6227 (t nil))))
6229 (defsubst org-cmp-tag (a b)
6230 "Compare the string values of the first tags of A and B."
6231 (let ((ta (car (last (get-text-property 1 'tags a))))
6232 (tb (car (last (get-text-property 1 'tags b)))))
6233 (cond ((not ta) +1)
6234 ((not tb) -1)
6235 ((string-lessp ta tb) -1)
6236 ((string-lessp tb ta) +1)
6237 (t nil))))
6239 (defsubst org-cmp-time (a b)
6240 "Compare the time-of-day values of strings A and B."
6241 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6242 (ta (or (get-text-property 1 'time-of-day a) def))
6243 (tb (or (get-text-property 1 'time-of-day b) def)))
6244 (cond ((< ta tb) -1)
6245 ((< tb ta) +1)
6246 (t nil))))
6248 (defsubst org-cmp-habit-p (a b)
6249 "Compare the todo states of strings A and B."
6250 (let ((ha (get-text-property 1 'org-habit-p a))
6251 (hb (get-text-property 1 'org-habit-p b)))
6252 (cond ((and ha (not hb)) -1)
6253 ((and (not ha) hb) +1)
6254 (t nil))))
6256 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
6258 (defun org-entries-lessp (a b)
6259 "Predicate for sorting agenda entries."
6260 ;; The following variables will be used when the form is evaluated.
6261 ;; So even though the compiler complains, keep them.
6262 (let* ((ss org-agenda-sorting-strategy-selected)
6263 (time-up (and (org-em 'time-up 'time-down ss)
6264 (org-cmp-time a b)))
6265 (time-down (if time-up (- time-up) nil))
6266 (priority-up (and (org-em 'priority-up 'priority-down ss)
6267 (org-cmp-priority a b)))
6268 (priority-down (if priority-up (- priority-up) nil))
6269 (effort-up (and (org-em 'effort-up 'effort-down ss)
6270 (org-cmp-effort a b)))
6271 (effort-down (if effort-up (- effort-up) nil))
6272 (category-up (and (or (org-em 'category-up 'category-down ss)
6273 (memq 'category-keep ss))
6274 (org-cmp-category a b)))
6275 (category-down (if category-up (- category-up) nil))
6276 (category-keep (if category-up +1 nil))
6277 (tag-up (and (org-em 'tag-up 'tag-down ss)
6278 (org-cmp-tag a b)))
6279 (tag-down (if tag-up (- tag-up) nil))
6280 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6281 (org-cmp-todo-state a b)))
6282 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6283 (habit-up (and (org-em 'habit-up 'habit-down ss)
6284 (org-cmp-habit-p a b)))
6285 (habit-down (if habit-up (- habit-up) nil))
6286 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6287 (org-cmp-alpha a b)))
6288 (alpha-down (if alpha-up (- alpha-up) nil))
6289 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6290 user-defined-up user-defined-down)
6291 (if (and need-user-cmp org-agenda-cmp-user-defined
6292 (functionp org-agenda-cmp-user-defined))
6293 (setq user-defined-up
6294 (funcall org-agenda-cmp-user-defined a b)
6295 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6296 (cdr (assoc
6297 (eval (cons 'or org-agenda-sorting-strategy-selected))
6298 '((-1 . t) (1 . nil) (nil . nil))))))
6300 ;;; Agenda restriction lock
6302 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6303 "Overlay to mark the headline to which agenda commands are restricted.")
6304 (overlay-put org-agenda-restriction-lock-overlay
6305 'face 'org-agenda-restriction-lock)
6306 (overlay-put org-agenda-restriction-lock-overlay
6307 'help-echo "Agendas are currently limited to this subtree.")
6308 (org-detach-overlay org-agenda-restriction-lock-overlay)
6310 (defun org-agenda-set-restriction-lock (&optional type)
6311 "Set restriction lock for agenda, to current subtree or file.
6312 Restriction will be the file if TYPE is `file', or if type is the
6313 universal prefix '(4), or if the cursor is before the first headline
6314 in the file. Otherwise, restriction will be to the current subtree."
6315 (interactive "P")
6316 (and (equal type '(4)) (setq type 'file))
6317 (setq type (cond
6318 (type type)
6319 ((org-at-heading-p) 'subtree)
6320 ((condition-case nil (org-back-to-heading t) (error nil))
6321 'subtree)
6322 (t 'file)))
6323 (if (eq type 'subtree)
6324 (progn
6325 (setq org-agenda-restrict t)
6326 (setq org-agenda-overriding-restriction 'subtree)
6327 (put 'org-agenda-files 'org-restrict
6328 (list (buffer-file-name (buffer-base-buffer))))
6329 (org-back-to-heading t)
6330 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6331 (move-marker org-agenda-restrict-begin (point))
6332 (move-marker org-agenda-restrict-end
6333 (save-excursion (org-end-of-subtree t)))
6334 (message "Locking agenda restriction to subtree"))
6335 (put 'org-agenda-files 'org-restrict
6336 (list (buffer-file-name (buffer-base-buffer))))
6337 (setq org-agenda-restrict nil)
6338 (setq org-agenda-overriding-restriction 'file)
6339 (move-marker org-agenda-restrict-begin nil)
6340 (move-marker org-agenda-restrict-end nil)
6341 (message "Locking agenda restriction to file"))
6342 (setq current-prefix-arg nil)
6343 (org-agenda-maybe-redo))
6345 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6346 "Remove the agenda restriction lock."
6347 (interactive "P")
6348 (org-detach-overlay org-agenda-restriction-lock-overlay)
6349 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6350 (setq org-agenda-overriding-restriction nil)
6351 (setq org-agenda-restrict nil)
6352 (put 'org-agenda-files 'org-restrict nil)
6353 (move-marker org-agenda-restrict-begin nil)
6354 (move-marker org-agenda-restrict-end nil)
6355 (setq current-prefix-arg nil)
6356 (message "Agenda restriction lock removed")
6357 (or noupdate (org-agenda-maybe-redo)))
6359 (defun org-agenda-maybe-redo ()
6360 "If there is any window showing the agenda view, update it."
6361 (let ((w (get-buffer-window org-agenda-buffer-name t))
6362 (w0 (selected-window)))
6363 (when w
6364 (select-window w)
6365 (org-agenda-redo)
6366 (select-window w0)
6367 (if org-agenda-overriding-restriction
6368 (message "Agenda view shifted to new %s restriction"
6369 org-agenda-overriding-restriction)
6370 (message "Agenda restriction lock removed")))))
6372 ;;; Agenda commands
6374 (defun org-agenda-check-type (error &rest types)
6375 "Check if agenda buffer is of allowed type.
6376 If ERROR is non-nil, throw an error, otherwise just return nil."
6377 (if (memq org-agenda-type types)
6379 (if error
6380 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6381 nil)))
6383 (defun org-agenda-Quit (&optional arg)
6384 "Exit agenda by removing the window or the buffer"
6385 (interactive)
6386 (if org-agenda-columns-active
6387 (org-columns-quit)
6388 (let ((buf (current-buffer)))
6389 (if (eq org-agenda-window-setup 'other-frame)
6390 (progn
6391 (org-agenda-reset-markers)
6392 (kill-buffer buf)
6393 (org-columns-remove-overlays)
6394 (setq org-agenda-archives-mode nil)
6395 (delete-frame))
6396 (and (not (eq org-agenda-window-setup 'current-window))
6397 (not (one-window-p))
6398 (delete-window))
6399 (org-agenda-reset-markers)
6400 (kill-buffer buf)
6401 (org-columns-remove-overlays)
6402 (setq org-agenda-archives-mode nil)))
6403 ;; Maybe restore the pre-agenda window configuration.
6404 (and org-agenda-restore-windows-after-quit
6405 (not (eq org-agenda-window-setup 'other-frame))
6406 org-pre-agenda-window-conf
6407 (set-window-configuration org-pre-agenda-window-conf))))
6409 (defun org-agenda-quit ()
6410 "Exit agenda by killing agenda buffer or burying it when
6411 `org-agenda-sticky' is non-NIL"
6412 (interactive)
6413 (if org-agenda-columns-active
6414 (org-columns-quit)
6415 (if org-agenda-sticky
6416 (let ((buf (current-buffer)))
6417 (if (eq org-agenda-window-setup 'other-frame)
6418 (progn
6419 (delete-frame))
6420 (and (not (eq org-agenda-window-setup 'current-window))
6421 (not (one-window-p))
6422 (delete-window)))
6423 (with-current-buffer buf
6424 (bury-buffer)
6425 ;; Maybe restore the pre-agenda window configuration.
6426 (and org-agenda-restore-windows-after-quit
6427 (not (eq org-agenda-window-setup 'other-frame))
6428 org-pre-agenda-window-conf
6429 (set-window-configuration org-pre-agenda-window-conf))))
6430 (org-agenda-Quit))))
6432 (defun org-agenda-exit ()
6433 "Exit agenda by removing the window or the buffer.
6434 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6435 Org-mode buffers visited directly by the user will not be touched."
6436 (interactive)
6437 (org-release-buffers org-agenda-new-buffers)
6438 (setq org-agenda-new-buffers nil)
6439 (org-agenda-Quit))
6441 (defun org-agenda-kill-all-agenda-buffers ()
6442 "Kill all buffers in `org-agena-mode'.
6443 This is used when toggling sticky agendas. You can also explicitly invoke it
6444 with `C-c a C-k'."
6445 (interactive)
6446 (let (blist)
6447 (dolist (buf (buffer-list))
6448 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
6449 (push buf blist)))
6450 (mapc 'kill-buffer blist)))
6452 (defun org-agenda-execute (arg)
6453 "Execute another agenda command, keeping same window.
6454 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6455 in the agenda."
6456 (interactive "P")
6457 (let ((org-agenda-window-setup 'current-window))
6458 (org-agenda arg)))
6460 (defun org-agenda-redo ()
6461 "Rebuild Agenda.
6462 When this is the global TODO list, a prefix argument will be interpreted."
6463 (interactive)
6464 (let* ((org-agenda-doing-sticky-redo org-agenda-sticky)
6465 (org-agenda-sticky nil)
6466 (org-agenda-buffer-name (or org-agenda-this-buffer-name
6467 org-agenda-buffer-name))
6468 (org-agenda-keep-modes t)
6469 (tag-filter org-agenda-tag-filter)
6470 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
6471 (top-cat-filter org-agenda-top-category-filter)
6472 (cat-filter org-agenda-category-filter)
6473 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6474 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
6475 (cols org-agenda-columns-active)
6476 (line (org-current-line))
6477 (window-line (- line (org-current-line (window-start))))
6478 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6479 (put 'org-agenda-tag-filter :preset-filter nil)
6480 (put 'org-agenda-category-filter :preset-filter nil)
6481 (and cols (org-columns-quit))
6482 (message "Rebuilding agenda buffer...")
6483 (org-let lprops '(eval org-agenda-redo-command))
6484 (setq org-agenda-undo-list nil
6485 org-agenda-pending-undo-list nil)
6486 (message "Rebuilding agenda buffer...done")
6487 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6488 (put 'org-agenda-category-filter :preset-filter cat-preset)
6489 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6490 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
6491 (and top-cat-filter (org-agenda-filter-top-category-apply top-cat-filter))
6492 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6493 (org-goto-line line)
6494 (recenter window-line)))
6496 (defvar org-global-tags-completion-table nil)
6497 (defvar org-agenda-filter-form nil)
6498 (defvar org-agenda-filtered-by-category nil)
6500 (defun org-agenda-filter-by-category (strip)
6501 "Keep only those lines in the agenda buffer that have a specific category.
6502 The category is that of the current line."
6503 (interactive "P")
6504 (if org-agenda-filtered-by-category
6505 (org-agenda-filter-show-all-cat)
6506 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6507 (if cat (org-agenda-filter-apply
6508 (list (concat (if strip "-" "+") cat)) 'category)
6509 (error "No category at point")))))
6511 (defun org-find-top-category (&optional pos)
6512 (save-excursion
6513 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
6514 (if pos (goto-char pos))
6515 ;; Skip up to the topmost parent
6516 (while (ignore-errors (outline-up-heading 1) t))
6517 (ignore-errors
6518 (nth 4 (org-heading-components))))))
6520 (defvar org-agenda-filtered-by-top-category nil)
6522 (defun org-agenda-filter-by-top-category (strip)
6523 "Keep only those lines in the agenda buffer that have a specific category.
6524 The category is that of the current line."
6525 (interactive "P")
6526 (if org-agenda-filtered-by-top-category
6527 (progn
6528 (setq org-agenda-filtered-by-top-category nil
6529 org-agenda-top-category-filter nil)
6530 (org-agenda-filter-show-all-cat))
6531 (let ((cat (org-find-top-category (org-get-at-bol 'org-hd-marker))))
6532 (if cat (org-agenda-filter-top-category-apply cat strip)
6533 (error "No top-level category at point")))))
6535 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6536 "Keep only those lines in the agenda buffer that have a specific tag.
6537 The tag is selected with its fast selection letter, as configured.
6538 With prefix argument STRIP, remove all lines that do have the tag.
6539 A lisp caller can specify CHAR. NARROW means that the new tag should be
6540 used to narrow the search - the interactive user can also press `-' or `+'
6541 to switch to narrowing."
6542 (interactive "P")
6543 (let* ((alist org-tag-alist-for-agenda)
6544 (tag-chars (mapconcat
6545 (lambda (x) (if (and (not (symbolp (car x)))
6546 (cdr x))
6547 (char-to-string (cdr x))
6548 ""))
6549 alist ""))
6550 (efforts (org-split-string
6551 (or (cdr (assoc (concat org-effort-property "_ALL")
6552 org-global-properties))
6553 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6554 "")))
6555 (effort-op org-agenda-filter-effort-default-operator)
6556 (effort-prompt "")
6557 (inhibit-read-only t)
6558 (current org-agenda-tag-filter)
6559 maybe-refresh a n tag)
6560 (unless char
6561 (message
6562 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6563 (if narrow "Narrow" "Filter") tag-chars
6564 (if org-agenda-auto-exclude-function "[RET], " ""))
6565 (setq char (read-char-exclusive)))
6566 (when (member char '(?+ ?-))
6567 ;; Narrowing down
6568 (cond ((equal char ?-) (setq strip t narrow t))
6569 ((equal char ?+) (setq strip nil narrow t)))
6570 (message
6571 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6572 (setq char (read-char-exclusive)))
6573 (when (member char '(?< ?> ?= ??))
6574 ;; An effort operator
6575 (setq effort-op (char-to-string char))
6576 (setq alist nil) ; to make sure it will be interpreted as effort.
6577 (unless (equal char ??)
6578 (loop for i from 0 to 9 do
6579 (setq effort-prompt
6580 (concat
6581 effort-prompt " ["
6582 (if (= i 9) "0" (int-to-string (1+ i)))
6583 "]" (nth i efforts))))
6584 (message "Effort%s: %s " effort-op effort-prompt)
6585 (setq char (read-char-exclusive))
6586 (when (or (< char ?0) (> char ?9))
6587 (error "Need 1-9,0 to select effort"))))
6588 (when (equal char ?\t)
6589 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6590 (org-set-local 'org-global-tags-completion-table
6591 (org-global-tags-completion-table)))
6592 (let ((completion-ignore-case t))
6593 (setq tag (org-icompleting-read
6594 "Tag: " org-global-tags-completion-table))))
6595 (cond
6596 ((equal char ?\r)
6597 (org-agenda-filter-show-all-tag)
6598 (when org-agenda-auto-exclude-function
6599 (setq org-agenda-tag-filter '())
6600 (dolist (tag (org-agenda-get-represented-tags))
6601 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6602 (if modifier
6603 (push modifier org-agenda-tag-filter))))
6604 (if (not (null org-agenda-tag-filter))
6605 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
6606 (setq maybe-refresh t))
6607 ((equal char ?/)
6608 (org-agenda-filter-show-all-tag)
6609 (when (get 'org-agenda-tag-filter :preset-filter)
6610 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
6611 (setq maybe-refresh t))
6612 ((equal char ?. )
6613 (setq org-agenda-tag-filter
6614 (mapcar (lambda(tag) (concat "+" tag))
6615 (org-get-at-bol 'tags)))
6616 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6617 (setq maybe-refresh t))
6618 ((or (equal char ?\ )
6619 (setq a (rassoc char alist))
6620 (and (>= char ?0) (<= char ?9)
6621 (setq n (if (= char ?0) 9 (- char ?0 1))
6622 tag (concat effort-op (nth n efforts))
6623 a (cons tag nil)))
6624 (and (= char ??)
6625 (setq tag "?eff")
6626 a (cons tag nil))
6627 (and tag (setq a (cons tag nil))))
6628 (org-agenda-filter-show-all-tag)
6629 (setq tag (car a))
6630 (setq org-agenda-tag-filter
6631 (cons (concat (if strip "-" "+") tag)
6632 (if narrow current nil)))
6633 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6634 (setq maybe-refresh t))
6635 (t (error "Invalid tag selection character %c" char)))
6636 (when (and maybe-refresh
6637 (eq org-agenda-clockreport-mode 'with-filter))
6638 (org-agenda-redo))))
6640 (defun org-agenda-get-represented-tags ()
6641 "Get a list of all tags currently represented in the agenda."
6642 (let (p tags)
6643 (save-excursion
6644 (goto-char (point-min))
6645 (while (setq p (next-single-property-change (point) 'tags))
6646 (goto-char p)
6647 (mapc (lambda (x) (add-to-list 'tags x))
6648 (get-text-property (point) 'tags))))
6649 tags))
6651 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6652 "Refine the current filter. See `org-agenda-filter-by-tag'."
6653 (interactive "P")
6654 (org-agenda-filter-by-tag strip char 'refine))
6656 (defun org-agenda-filter-make-matcher ()
6657 "Create the form that tests a line for agenda filter."
6658 (let (f f1)
6659 ;; first compute the tag-filter matcher
6660 (dolist (x (delete-dups
6661 (append (get 'org-agenda-tag-filter
6662 :preset-filter) org-agenda-tag-filter)))
6663 (if (member x '("-" "+"))
6664 (setq f1 (if (equal x "-") 'tags '(not tags)))
6665 (if (string-match "[<=>?]" x)
6666 (setq f1 (org-agenda-filter-effort-form x))
6667 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6668 (if (equal (string-to-char x) ?-)
6669 (setq f1 (list 'not f1))))
6670 (push f1 f))
6671 ;; then compute the category-filter matcher
6672 (dolist (x (delete-dups
6673 (append (get 'org-agenda-category-filter
6674 :preset-filter) org-agenda-category-filter)))
6675 (if (equal "-" (substring x 0 1))
6676 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
6677 (setq f1 (list 'equal (substring x 1) 'cat)))
6678 (push f1 f))
6679 (cons 'and (nreverse f))))
6681 (defun org-agenda-filter-effort-form (e)
6682 "Return the form to compare the effort of the current line with what E says.
6683 E looks like \"+<2:25\"."
6684 (let (op)
6685 (setq e (substring e 1))
6686 (setq op (string-to-char e) e (substring e 1))
6687 (setq op (cond ((equal op ?<) '<=)
6688 ((equal op ?>) '>=)
6689 ((equal op ??) op)
6690 (t '=)))
6691 (list 'org-agenda-compare-effort (list 'quote op)
6692 (org-duration-string-to-minutes e))))
6694 (defun org-agenda-compare-effort (op value)
6695 "Compare the effort of the current line with VALUE, using OP.
6696 If the line does not have an effort defined, return nil."
6697 (let ((eff (org-get-at-bol 'effort-minutes)))
6698 (if (equal op ??)
6699 (not eff)
6700 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6701 value))))
6703 (defun org-agenda-filter-apply (filter type)
6704 "Set FILTER as the new agenda filter and apply it."
6705 (let (tags cat)
6706 (if (eq type 'tag)
6707 (setq org-agenda-tag-filter filter)
6708 (setq org-agenda-category-filter filter))
6709 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
6710 (if (and (eq type 'category)
6711 (not (equal (substring (car filter) 0 1) "-")))
6712 ;; Only set `org-agenda-filtered-by-category' to t
6713 ;; when a unique category is used as the filter
6714 (setq org-agenda-filtered-by-category t))
6715 (org-agenda-set-mode-name)
6716 (save-excursion
6717 (goto-char (point-min))
6718 (while (not (eobp))
6719 (if (org-get-at-bol 'org-marker)
6720 (progn
6721 (setq tags (org-get-at-bol 'tags) ; used in eval
6722 cat (get-text-property (point) 'org-category))
6723 (if (not (eval org-agenda-filter-form))
6724 (org-agenda-filter-hide-line type))
6725 (beginning-of-line 2))
6726 (beginning-of-line 2))))
6727 (if (get-char-property (point) 'invisible)
6728 (ignore-errors (org-agenda-previous-line)))))
6730 (defun org-agenda-filter-top-category-apply (category &optional negative)
6731 "Set FILTER as the new agenda filter and apply it."
6732 (org-agenda-set-mode-name)
6733 (save-excursion
6734 (goto-char (point-min))
6735 (while (not (eobp))
6736 (let* ((pos (org-get-at-bol 'org-hd-marker))
6737 (topcat (and pos (org-find-top-category pos))))
6738 (if (and topcat (funcall (if negative 'identity 'not)
6739 (string= category topcat)))
6740 (org-agenda-filter-hide-line 'category)))
6741 (beginning-of-line 2)))
6742 (if (get-char-property (point) 'invisible)
6743 (org-agenda-previous-line))
6744 (setq org-agenda-top-category-filter category
6745 org-agenda-filtered-by-top-category t))
6747 (defun org-agenda-filter-hide-line (type)
6748 (let (ov)
6749 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6750 (point-at-eol)))
6751 (overlay-put ov 'invisible t)
6752 (overlay-put ov 'type type)
6753 (if (eq type 'tag)
6754 (push ov org-agenda-tag-filter-overlays)
6755 (push ov org-agenda-cat-filter-overlays))))
6757 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6758 (setq pos (or pos (point)))
6759 (save-excursion
6760 (dolist (ov (overlays-at pos))
6761 (when (and (overlay-get ov 'invisible)
6762 (eq (overlay-get ov 'type) 'tag))
6763 (goto-char pos)
6764 (if (< (overlay-start ov) (point-at-eol))
6765 (move-overlay ov (point-at-eol)
6766 (overlay-end ov)))))))
6768 (defun org-agenda-filter-show-all-tag nil
6769 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
6770 (setq org-agenda-tag-filter-overlays nil
6771 org-agenda-tag-filter nil
6772 org-agenda-filter-form nil)
6773 (org-agenda-set-mode-name))
6775 (defun org-agenda-filter-show-all-cat nil
6776 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
6777 (setq org-agenda-cat-filter-overlays nil
6778 org-agenda-filtered-by-category nil
6779 org-agenda-category-filter nil
6780 org-agenda-filter-form nil)
6781 (org-agenda-set-mode-name))
6783 (defun org-agenda-manipulate-query-add ()
6784 "Manipulate the query by adding a search term with positive selection.
6785 Positive selection means the term must be matched for selection of an entry."
6786 (interactive)
6787 (org-agenda-manipulate-query ?\[))
6788 (defun org-agenda-manipulate-query-subtract ()
6789 "Manipulate the query by adding a search term with negative selection.
6790 Negative selection means term must not be matched for selection of an entry."
6791 (interactive)
6792 (org-agenda-manipulate-query ?\]))
6793 (defun org-agenda-manipulate-query-add-re ()
6794 "Manipulate the query by adding a search regexp with positive selection.
6795 Positive selection means the regexp must match for selection of an entry."
6796 (interactive)
6797 (org-agenda-manipulate-query ?\{))
6798 (defun org-agenda-manipulate-query-subtract-re ()
6799 "Manipulate the query by adding a search regexp with negative selection.
6800 Negative selection means regexp must not match for selection of an entry."
6801 (interactive)
6802 (org-agenda-manipulate-query ?\}))
6803 (defun org-agenda-manipulate-query (char)
6804 (cond
6805 ((memq org-agenda-type '(timeline agenda))
6806 (let ((org-agenda-include-inactive-timestamps t))
6807 (org-agenda-redo))
6808 (message "Display now includes inactive timestamps as well"))
6809 ((eq org-agenda-type 'search)
6810 (org-add-to-string
6811 'org-agenda-query-string
6812 (if org-agenda-last-search-view-search-was-boolean
6813 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6814 (?\{ . " +{}") (?\} . " -{}"))))
6815 " "))
6816 (setq org-agenda-redo-command
6817 (list 'org-search-view
6818 org-todo-only
6819 org-agenda-query-string
6820 (+ (length org-agenda-query-string)
6821 (if (member char '(?\{ ?\})) 0 1))))
6822 (set-register org-agenda-query-register org-agenda-query-string)
6823 (org-agenda-redo))
6824 (t (error "Cannot manipulate query for %s-type agenda buffers"
6825 org-agenda-type))))
6827 (defun org-add-to-string (var string)
6828 (set var (concat (symbol-value var) string)))
6830 (defun org-agenda-goto-date (date)
6831 "Jump to DATE in agenda."
6832 (interactive (list (let ((org-read-date-prefer-future
6833 (eval org-agenda-jump-prefer-future)))
6834 (org-read-date))))
6835 (org-agenda-list nil date))
6837 (defun org-agenda-goto-today ()
6838 "Go to today."
6839 (interactive)
6840 (org-agenda-check-type t 'timeline 'agenda)
6841 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6842 (cond
6843 (tdpos (goto-char tdpos))
6844 ((eq org-agenda-type 'agenda)
6845 (let* ((sd (org-agenda-compute-starting-span
6846 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6847 (org-agenda-overriding-arguments org-agenda-last-arguments))
6848 (setf (nth 1 org-agenda-overriding-arguments) sd)
6849 (org-agenda-redo)
6850 (org-agenda-find-same-or-today-or-agenda)))
6851 (t (error "Cannot find today")))))
6853 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6854 (goto-char
6855 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6856 (text-property-any (point-min) (point-max) 'org-today t)
6857 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6858 (point-min))))
6860 (defun org-agenda-later (arg)
6861 "Go forward in time by thee current span.
6862 With prefix ARG, go forward that many times the current span."
6863 (interactive "p")
6864 (org-agenda-check-type t 'agenda)
6865 (let* ((span org-agenda-current-span)
6866 (sd org-starting-day)
6867 (greg (calendar-gregorian-from-absolute sd))
6868 (cnt (org-get-at-bol 'org-day-cnt))
6869 greg2)
6870 (cond
6871 ((eq span 'day)
6872 (setq sd (+ arg sd)))
6873 ((eq span 'week)
6874 (setq sd (+ (* 7 arg) sd)))
6875 ((eq span 'month)
6876 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6877 sd (calendar-absolute-from-gregorian greg2))
6878 (setcar greg2 (1+ (car greg2))))
6879 ((eq span 'year)
6880 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6881 sd (calendar-absolute-from-gregorian greg2))
6882 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6884 (setq sd (+ (* span arg) sd))))
6885 (let ((org-agenda-overriding-arguments
6886 (list (car org-agenda-last-arguments) sd span t)))
6887 (org-agenda-redo)
6888 (org-agenda-find-same-or-today-or-agenda cnt))))
6890 (defun org-agenda-earlier (arg)
6891 "Go backward in time by the current span.
6892 With prefix ARG, go backward that many times the current span."
6893 (interactive "p")
6894 (org-agenda-later (- arg)))
6896 (defun org-agenda-view-mode-dispatch ()
6897 "Call one of the view mode commands."
6898 (interactive)
6899 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6900 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6901 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
6902 (let ((a (read-char-exclusive)))
6903 (case a
6904 (?\ (call-interactively 'org-agenda-reset-view))
6905 (?d (call-interactively 'org-agenda-day-view))
6906 (?w (call-interactively 'org-agenda-week-view))
6907 (?m (call-interactively 'org-agenda-month-view))
6908 (?y (call-interactively 'org-agenda-year-view))
6909 (?l (call-interactively 'org-agenda-log-mode))
6910 (?L (org-agenda-log-mode '(4)))
6911 (?c (org-agenda-log-mode 'clockcheck))
6912 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6913 (?a (call-interactively 'org-agenda-archives-mode))
6914 (?A (org-agenda-archives-mode 'files))
6915 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6916 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6917 (?G (call-interactively 'org-agenda-toggle-time-grid))
6918 (?D (call-interactively 'org-agenda-toggle-diary))
6919 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6920 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6921 (org-agenda-check-type t 'timeline 'agenda)
6922 (org-agenda-redo))
6923 (message "Display now includes inactive timestamps as well"))
6924 (?q (message "Abort"))
6925 (otherwise (error "Invalid key" )))))
6927 (defun org-agenda-reset-view ()
6928 "Switch to default view for agenda."
6929 (interactive)
6930 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6931 (defun org-agenda-day-view (&optional day-of-year)
6932 "Switch to daily view for agenda.
6933 With argument DAY-OF-YEAR, switch to that day of the year."
6934 (interactive "P")
6935 (org-agenda-change-time-span 'day day-of-year))
6936 (defun org-agenda-week-view (&optional iso-week)
6937 "Switch to daily view for agenda.
6938 With argument ISO-WEEK, switch to the corresponding ISO week.
6939 If ISO-WEEK has more then 2 digits, only the last two encode the
6940 week. Any digits before this encode a year. So 200712 means
6941 week 12 of year 2007. Years in the range 1938-2037 can also be
6942 written as 2-digit years."
6943 (interactive "P")
6944 (org-agenda-change-time-span 'week iso-week))
6945 (defun org-agenda-month-view (&optional month)
6946 "Switch to monthly view for agenda.
6947 With argument MONTH, switch to that month."
6948 (interactive "P")
6949 (org-agenda-change-time-span 'month month))
6950 (defun org-agenda-year-view (&optional year)
6951 "Switch to yearly view for agenda.
6952 With argument YEAR, switch to that year.
6953 If MONTH has more then 2 digits, only the last two encode the
6954 month. Any digits before this encode a year. So 200712 means
6955 December year 2007. Years in the range 1938-2037 can also be
6956 written as 2-digit years."
6957 (interactive "P")
6958 (when year
6959 (setq year (org-small-year-to-year year)))
6960 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6961 (org-agenda-change-time-span 'year year)
6962 (error "Abort")))
6964 (defun org-agenda-change-time-span (span &optional n)
6965 "Change the agenda view to SPAN.
6966 SPAN may be `day', `week', `month', `year'."
6967 (org-agenda-check-type t 'agenda)
6968 (if (and (not n) (equal org-agenda-current-span span))
6969 (error "Viewing span is already \"%s\"" span))
6970 (let* ((sd (or (org-get-at-bol 'day)
6971 org-starting-day))
6972 (sd (org-agenda-compute-starting-span sd span n))
6973 (org-agenda-overriding-arguments
6974 (or org-agenda-overriding-arguments
6975 (list (car org-agenda-last-arguments) sd span t))))
6976 (org-agenda-redo)
6977 (org-agenda-find-same-or-today-or-agenda))
6978 (org-agenda-set-mode-name)
6979 (message "Switched to %s view" span))
6981 (defun org-agenda-compute-starting-span (sd span &optional n)
6982 "Compute starting date for agenda.
6983 SPAN may be `day', `week', `month', `year'. The return value
6984 is a cons cell with the starting date and the number of days,
6985 so that the date SD will be in that range."
6986 (let* ((greg (calendar-gregorian-from-absolute sd))
6987 (dg (nth 1 greg))
6988 (mg (car greg))
6989 (yg (nth 2 greg)))
6990 (cond
6991 ((eq span 'day)
6992 (when n
6993 (setq sd (+ (calendar-absolute-from-gregorian
6994 (list mg 1 yg))
6995 n -1))))
6996 ((eq span 'week)
6997 (let* ((nt (calendar-day-of-week
6998 (calendar-gregorian-from-absolute sd)))
6999 (d (if org-agenda-start-on-weekday
7000 (- nt org-agenda-start-on-weekday)
7003 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7004 (when n
7005 (require 'cal-iso)
7006 (when (> n 99)
7007 (setq y1 (org-small-year-to-year (/ n 100))
7008 n (mod n 100)))
7009 (setq sd
7010 (calendar-absolute-from-iso
7011 (list n 1
7012 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7013 ((eq span 'month)
7014 (let (y1)
7015 (when (and n (> n 99))
7016 (setq y1 (org-small-year-to-year (/ n 100))
7017 n (mod n 100)))
7018 (setq sd (calendar-absolute-from-gregorian
7019 (list (or n mg) 1 (or y1 yg))))))
7020 ((eq span 'year)
7021 (setq sd (calendar-absolute-from-gregorian
7022 (list 1 1 (or n yg))))))
7023 sd))
7025 (defun org-agenda-next-date-line (&optional arg)
7026 "Jump to the next line indicating a date in agenda buffer."
7027 (interactive "p")
7028 (org-agenda-check-type t 'agenda 'timeline)
7029 (beginning-of-line 1)
7030 ;; This does not work if user makes date format that starts with a blank
7031 (if (looking-at "^\\S-") (forward-char 1))
7032 (if (not (re-search-forward "^\\S-" nil t arg))
7033 (progn
7034 (backward-char 1)
7035 (error "No next date after this line in this buffer")))
7036 (goto-char (match-beginning 0)))
7038 (defun org-agenda-previous-date-line (&optional arg)
7039 "Jump to the previous line indicating a date in agenda buffer."
7040 (interactive "p")
7041 (org-agenda-check-type t 'agenda 'timeline)
7042 (beginning-of-line 1)
7043 (if (not (re-search-backward "^\\S-" nil t arg))
7044 (error "No previous date before this line in this buffer")))
7046 ;; Initialize the highlight
7047 (defvar org-hl (make-overlay 1 1))
7048 (overlay-put org-hl 'face 'highlight)
7050 (defun org-highlight (begin end &optional buffer)
7051 "Highlight a region with overlay."
7052 (move-overlay org-hl begin end (or buffer (current-buffer))))
7054 (defun org-unhighlight ()
7055 "Detach overlay INDEX."
7056 (org-detach-overlay org-hl))
7058 ;; FIXME this is currently not used.
7059 (defun org-highlight-until-next-command (beg end &optional buffer)
7060 "Move the highlight overlay to BEG/END, remove it before the next command."
7061 (org-highlight beg end buffer)
7062 (add-hook 'pre-command-hook 'org-unhighlight-once))
7063 (defun org-unhighlight-once ()
7064 "Remove the highlight from its position, and this function from the hook."
7065 (remove-hook 'pre-command-hook 'org-unhighlight-once)
7066 (org-unhighlight))
7068 (defun org-agenda-follow-mode ()
7069 "Toggle follow mode in an agenda buffer."
7070 (interactive)
7071 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
7072 (org-agenda-set-mode-name)
7073 (org-agenda-do-context-action)
7074 (message "Follow mode is %s"
7075 (if org-agenda-follow-mode "on" "off")))
7077 (defun org-agenda-entry-text-mode (&optional arg)
7078 "Toggle entry text mode in an agenda buffer."
7079 (interactive "P")
7080 (setq org-agenda-entry-text-mode (or (integerp arg)
7081 (not org-agenda-entry-text-mode)))
7082 (org-agenda-entry-text-hide)
7083 (and org-agenda-entry-text-mode
7084 (let ((org-agenda-entry-text-maxlines
7085 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7086 (org-agenda-entry-text-show)))
7087 (org-agenda-set-mode-name)
7088 (message "Entry text mode is %s. Maximum number of lines is %d"
7089 (if org-agenda-entry-text-mode "on" "off")
7090 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7092 (defun org-agenda-clockreport-mode (&optional with-filter)
7093 "Toggle clocktable mode in an agenda buffer.
7094 With prefix arg WITH-FILTER, make the clocktable respect the current
7095 agenda filter."
7096 (interactive "P")
7097 (org-agenda-check-type t 'agenda)
7098 (if with-filter
7099 (setq org-agenda-clockreport-mode 'with-filter)
7100 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
7101 (org-agenda-set-mode-name)
7102 (org-agenda-redo)
7103 (message "Clocktable mode is %s"
7104 (if org-agenda-clockreport-mode "on" "off")))
7106 (defun org-agenda-log-mode (&optional special)
7107 "Toggle log mode in an agenda buffer.
7108 With argument SPECIAL, show all possible log items, not only the ones
7109 configured in `org-agenda-log-mode-items'.
7110 With a double `C-u' prefix arg, show *only* log items, nothing else."
7111 (interactive "P")
7112 (org-agenda-check-type t 'agenda 'timeline)
7113 (setq org-agenda-show-log
7114 (cond
7115 ((equal special '(16)) 'only)
7116 ((eq special 'clockcheck)
7117 (if (eq org-agenda-show-log 'clockcheck)
7118 nil 'clockcheck))
7119 (special '(closed clock state))
7120 (t (not org-agenda-show-log))))
7121 (org-agenda-set-mode-name)
7122 (org-agenda-redo)
7123 (message "Log mode is %s"
7124 (if org-agenda-show-log "on" "off")))
7126 (defun org-agenda-archives-mode (&optional with-files)
7127 "Toggle inclusion of items in trees marked with :ARCHIVE:.
7128 When called with a prefix argument, include all archive files as well."
7129 (interactive "P")
7130 (setq org-agenda-archives-mode
7131 (if with-files t (if org-agenda-archives-mode nil 'trees)))
7132 (org-agenda-set-mode-name)
7133 (org-agenda-redo)
7134 (message
7135 "%s"
7136 (cond
7137 ((eq org-agenda-archives-mode nil)
7138 "No archives are included")
7139 ((eq org-agenda-archives-mode 'trees)
7140 (format "Trees with :%s: tag are included" org-archive-tag))
7141 ((eq org-agenda-archives-mode t)
7142 (format "Trees with :%s: tag and all active archive files are included"
7143 org-archive-tag)))))
7145 (defun org-agenda-toggle-diary ()
7146 "Toggle diary inclusion in an agenda buffer."
7147 (interactive)
7148 (org-agenda-check-type t 'agenda)
7149 (setq org-agenda-include-diary (not org-agenda-include-diary))
7150 (org-agenda-redo)
7151 (org-agenda-set-mode-name)
7152 (message "Diary inclusion turned %s"
7153 (if org-agenda-include-diary "on" "off")))
7155 (defun org-agenda-toggle-deadlines ()
7156 "Toggle inclusion of entries with a deadline in an agenda buffer."
7157 (interactive)
7158 (org-agenda-check-type t 'agenda)
7159 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
7160 (org-agenda-redo)
7161 (org-agenda-set-mode-name)
7162 (message "Deadlines inclusion turned %s"
7163 (if org-agenda-include-deadlines "on" "off")))
7165 (defun org-agenda-toggle-time-grid ()
7166 "Toggle time grid in an agenda buffer."
7167 (interactive)
7168 (org-agenda-check-type t 'agenda)
7169 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
7170 (org-agenda-redo)
7171 (org-agenda-set-mode-name)
7172 (message "Time-grid turned %s"
7173 (if org-agenda-use-time-grid "on" "off")))
7175 (defun org-agenda-set-mode-name ()
7176 "Set the mode name to indicate all the small mode settings."
7177 (setq mode-name
7178 (list "Org-Agenda"
7179 (if (get 'org-agenda-files 'org-restrict) " []" "")
7181 '(:eval (org-agenda-span-name org-agenda-current-span))
7182 (if org-agenda-follow-mode " Follow" "")
7183 (if org-agenda-entry-text-mode " ETxt" "")
7184 (if org-agenda-include-diary " Diary" "")
7185 (if org-agenda-include-deadlines " Ddl" "")
7186 (if org-agenda-use-time-grid " Grid" "")
7187 (if (and (boundp 'org-habit-show-habits)
7188 org-habit-show-habits) " Habit" "")
7189 (cond
7190 ((consp org-agenda-show-log) " LogAll")
7191 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
7192 (org-agenda-show-log " Log")
7193 (t ""))
7194 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
7195 :preset-filter))
7196 '(:eval (org-propertize
7197 (concat " <"
7198 (mapconcat
7199 'identity
7200 (append
7201 (get 'org-agenda-category-filter :preset-filter)
7202 org-agenda-category-filter)
7204 ">")
7205 'face 'org-agenda-filter-category
7206 'help-echo "Category used in filtering"))
7208 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
7209 :preset-filter))
7210 '(:eval (org-propertize
7211 (concat " {"
7212 (mapconcat
7213 'identity
7214 (append
7215 (get 'org-agenda-tag-filter :preset-filter)
7216 org-agenda-tag-filter)
7218 "}")
7219 'face 'org-agenda-filter-tags
7220 'help-echo "Tags used in filtering"))
7222 (if org-agenda-archives-mode
7223 (if (eq org-agenda-archives-mode t)
7224 " Archives"
7225 (format " :%s:" org-archive-tag))
7227 (if org-agenda-clockreport-mode
7228 (if (eq org-agenda-clockreport-mode 'with-filter)
7229 " Clock{}" " Clock")
7230 "")))
7231 (force-mode-line-update))
7233 (defun org-agenda-post-command-hook ()
7234 (setq org-agenda-type
7235 (or (get-text-property (point) 'org-agenda-type)
7236 (get-text-property (max (point-min) (1- (point)))
7237 'org-agenda-type))))
7239 (defun org-agenda-next-line ()
7240 "Move cursor to the next line, and show if follow mode is active."
7241 (interactive)
7242 (call-interactively 'next-line)
7243 (org-agenda-do-context-action))
7245 (defun org-agenda-previous-line ()
7246 "Move cursor to the previous line, and show if follow-mode is active."
7247 (interactive)
7248 (call-interactively 'previous-line)
7249 (org-agenda-do-context-action))
7251 (defun org-agenda-do-context-action ()
7252 "Show outline path and, maybe, follow mode window."
7253 (let ((m (org-get-at-bol 'org-marker)))
7254 (when (and (markerp m) (marker-buffer m))
7255 (and org-agenda-follow-mode
7256 (if org-agenda-follow-indirect
7257 (org-agenda-tree-to-indirect-buffer)
7258 (org-agenda-show)))
7259 (and org-agenda-show-outline-path
7260 (org-with-point-at m (org-display-outline-path t))))))
7262 (defun org-agenda-show-priority ()
7263 "Show the priority of the current item.
7264 This priority is composed of the main priority given with the [#A] cookies,
7265 and by additional input from the age of a schedules or deadline entry."
7266 (interactive)
7267 (let* ((pri (org-get-at-bol 'priority)))
7268 (message "Priority is %d" (if pri pri -1000))))
7270 (defun org-agenda-show-tags ()
7271 "Show the tags applicable to the current item."
7272 (interactive)
7273 (let* ((tags (org-get-at-bol 'tags)))
7274 (if tags
7275 (message "Tags are :%s:"
7276 (org-no-properties (mapconcat 'identity tags ":")))
7277 (message "No tags associated with this line"))))
7279 (defun org-agenda-goto (&optional highlight)
7280 "Go to the Org-mode file which contains the item at point."
7281 (interactive)
7282 (let* ((marker (or (org-get-at-bol 'org-marker)
7283 (org-agenda-error)))
7284 (buffer (marker-buffer marker))
7285 (pos (marker-position marker)))
7286 (switch-to-buffer-other-window buffer)
7287 (widen)
7288 (push-mark)
7289 (goto-char pos)
7290 (when (derived-mode-p 'org-mode)
7291 (org-show-context 'agenda)
7292 (save-excursion
7293 (and (outline-next-heading)
7294 (org-flag-heading nil)))) ; show the next heading
7295 (when (outline-invisible-p)
7296 (show-entry)) ; display invisible text
7297 (recenter (/ (window-height) 2))
7298 (run-hooks 'org-agenda-after-show-hook)
7299 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
7301 (defvar org-agenda-after-show-hook nil
7302 "Normal hook run after an item has been shown from the agenda.
7303 Point is in the buffer where the item originated.")
7305 (defun org-agenda-kill ()
7306 "Kill the entry or subtree belonging to the current agenda entry."
7307 (interactive)
7308 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7309 (let* ((marker (or (org-get-at-bol 'org-marker)
7310 (org-agenda-error)))
7311 (buffer (marker-buffer marker))
7312 (pos (marker-position marker))
7313 (type (org-get-at-bol 'type))
7314 dbeg dend (n 0) conf)
7315 (org-with-remote-undo buffer
7316 (with-current-buffer buffer
7317 (save-excursion
7318 (goto-char pos)
7319 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
7320 (setq dbeg (progn (org-back-to-heading t) (point))
7321 dend (org-end-of-subtree t t))
7322 (setq dbeg (point-at-bol)
7323 dend (min (point-max) (1+ (point-at-eol)))))
7324 (goto-char dbeg)
7325 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
7326 (setq conf (or (eq t org-agenda-confirm-kill)
7327 (and (numberp org-agenda-confirm-kill)
7328 (> n org-agenda-confirm-kill))))
7329 (and conf
7330 (not (y-or-n-p
7331 (format "Delete entry with %d lines in buffer \"%s\"? "
7332 n (buffer-name buffer))))
7333 (error "Abort"))
7334 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
7335 (with-current-buffer buffer (delete-region dbeg dend))
7336 (message "Agenda item and source killed"))))
7338 (defvar org-archive-default-command)
7339 (defun org-agenda-archive-default ()
7340 "Archive the entry or subtree belonging to the current agenda entry."
7341 (interactive)
7342 (require 'org-archive)
7343 (org-agenda-archive-with org-archive-default-command))
7345 (defun org-agenda-archive-default-with-confirmation ()
7346 "Archive the entry or subtree belonging to the current agenda entry."
7347 (interactive)
7348 (require 'org-archive)
7349 (org-agenda-archive-with org-archive-default-command 'confirm))
7351 (defun org-agenda-archive ()
7352 "Archive the entry or subtree belonging to the current agenda entry."
7353 (interactive)
7354 (org-agenda-archive-with 'org-archive-subtree))
7356 (defun org-agenda-archive-to-archive-sibling ()
7357 "Move the entry to the archive sibling."
7358 (interactive)
7359 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7361 (defun org-agenda-archive-with (cmd &optional confirm)
7362 "Move the entry to the archive sibling."
7363 (interactive)
7364 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7365 (let* ((marker (or (org-get-at-bol 'org-marker)
7366 (org-agenda-error)))
7367 (buffer (marker-buffer marker))
7368 (pos (marker-position marker)))
7369 (org-with-remote-undo buffer
7370 (with-current-buffer buffer
7371 (if (derived-mode-p 'org-mode)
7372 (if (and confirm
7373 (not (y-or-n-p "Archive this subtree or entry? ")))
7374 (error "Abort")
7375 (save-excursion
7376 (goto-char pos)
7377 (org-remove-subtree-entries-from-agenda)
7378 (org-back-to-heading t)
7379 (funcall cmd)))
7380 (error "Archiving works only in Org-mode files"))))))
7382 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7383 "Remove all lines in the agenda that correspond to a given subtree.
7384 The subtree is the one in buffer BUF, starting at BEG and ending at END.
7385 If this information is not given, the function uses the tree at point."
7386 (let ((buf (or buf (current-buffer))) m p)
7387 (save-excursion
7388 (unless (and beg end)
7389 (org-back-to-heading t)
7390 (setq beg (point))
7391 (org-end-of-subtree t)
7392 (setq end (point)))
7393 (set-buffer (get-buffer org-agenda-buffer-name))
7394 (save-excursion
7395 (goto-char (point-max))
7396 (beginning-of-line 1)
7397 (while (not (bobp))
7398 (when (and (setq m (org-get-at-bol 'org-marker))
7399 (equal buf (marker-buffer m))
7400 (setq p (marker-position m))
7401 (>= p beg)
7402 (< p end))
7403 (let ((inhibit-read-only t))
7404 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7405 (beginning-of-line 0))))))
7407 (defun org-agenda-refile (&optional goto rfloc no-update)
7408 "Refile the item at point."
7409 (interactive "P")
7410 (if (equal goto '(16))
7411 (org-refile-goto-last-stored)
7412 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7413 (org-agenda-error)))
7414 (buffer (marker-buffer marker))
7415 (pos (marker-position marker))
7416 (rfloc (or rfloc
7417 (org-refile-get-location
7418 (if goto "Goto" "Refile to") buffer
7419 org-refile-allow-creating-parent-nodes))))
7420 (with-current-buffer buffer
7421 (save-excursion
7422 (save-restriction
7423 (widen)
7424 (goto-char marker)
7425 (org-remove-subtree-entries-from-agenda)
7426 (org-refile goto buffer rfloc)))))
7427 (unless no-update (org-agenda-redo))))
7429 (defun org-agenda-open-link (&optional arg)
7430 "Follow the link in the current line, if any.
7431 This looks for a link in the displayed line in the agenda. It also looks
7432 at the text of the entry itself."
7433 (interactive "P")
7434 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7435 (org-get-at-bol 'org-marker)))
7436 (buffer (and marker (marker-buffer marker)))
7437 (prefix (buffer-substring
7438 (point-at-bol) (point-at-eol))))
7439 (cond
7440 (buffer
7441 (with-current-buffer buffer
7442 (save-excursion
7443 (save-restriction
7444 (widen)
7445 (goto-char marker)
7446 (org-offer-links-in-entry arg prefix)))))
7447 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7448 (save-excursion
7449 (beginning-of-line 1)
7450 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7451 (org-open-link-from-string (match-string 1)))
7452 (t (error "No link to open here")))))
7454 (defun org-agenda-copy-local-variable (var)
7455 "Get a variable from a referenced buffer and install it here."
7456 (let ((m (org-get-at-bol 'org-marker)))
7457 (when (and m (buffer-live-p (marker-buffer m)))
7458 (org-set-local var (with-current-buffer (marker-buffer m)
7459 (symbol-value var))))))
7461 (defun org-agenda-switch-to (&optional delete-other-windows)
7462 "Go to the Org-mode file which contains the item at point."
7463 (interactive)
7464 (if (and org-return-follows-link
7465 (not (org-get-at-bol 'org-marker))
7466 (org-in-regexp org-bracket-link-regexp))
7467 (org-open-link-from-string (match-string 0))
7468 (let* ((marker (or (org-get-at-bol 'org-marker)
7469 (org-agenda-error)))
7470 (buffer (marker-buffer marker))
7471 (pos (marker-position marker)))
7472 (org-pop-to-buffer-same-window buffer)
7473 (and delete-other-windows (delete-other-windows))
7474 (widen)
7475 (goto-char pos)
7476 (when (derived-mode-p 'org-mode)
7477 (org-show-context 'agenda)
7478 (save-excursion
7479 (and (outline-next-heading)
7480 (org-flag-heading nil))) ; show the next heading
7481 (when (outline-invisible-p)
7482 (show-entry)))))) ; display invisible text
7484 (defun org-agenda-goto-mouse (ev)
7485 "Go to the Org-mode file which contains the item at the mouse click."
7486 (interactive "e")
7487 (mouse-set-point ev)
7488 (org-agenda-goto))
7490 (defun org-agenda-show (&optional full-entry)
7491 "Display the Org-mode file which contains the item at point.
7492 With prefix argument FULL-ENTRY, make the entire entry visible
7493 if it was hidden in the outline."
7494 (interactive "P")
7495 (let ((win (selected-window)))
7496 (if full-entry
7497 (let ((org-show-entry-below t))
7498 (org-agenda-goto t))
7499 (org-agenda-goto t))
7500 (select-window win)))
7502 (defvar org-agenda-show-window nil)
7503 (defun org-agenda-show-and-scroll-up (&optional arg)
7504 "Display the Org-mode file which contains the item at point.
7505 When called repeatedly, scroll the window that is displaying the buffer.
7506 With a \\[universal-argument] prefix, use `org-show-entry' instead of
7507 `show-subtree' to display the item, so that drawers and logbooks stay
7508 folded."
7509 (interactive "P")
7510 (let ((win (selected-window)))
7511 (if (and (window-live-p org-agenda-show-window)
7512 (eq this-command last-command))
7513 (progn
7514 (select-window org-agenda-show-window)
7515 (ignore-errors (scroll-up)))
7516 (org-agenda-goto t)
7517 (if arg (org-show-entry) (show-subtree))
7518 (setq org-agenda-show-window (selected-window)))
7519 (select-window win)))
7521 (defun org-agenda-show-scroll-down ()
7522 "Scroll down the window showing the agenda."
7523 (interactive)
7524 (let ((win (selected-window)))
7525 (when (window-live-p org-agenda-show-window)
7526 (select-window org-agenda-show-window)
7527 (ignore-errors (scroll-down))
7528 (select-window win))))
7530 (defun org-agenda-show-1 (&optional more)
7531 "Display the Org-mode file which contains the item at point.
7532 The prefix arg selects the amount of information to display:
7534 0 hide the subtree
7535 1 just show the entry according to defaults.
7536 2 show the children view
7537 3 show the subtree view
7538 4 show the entire subtree and any LOGBOOK drawers
7539 5 show the entire subtree and any drawers
7540 With prefix argument FULL-ENTRY, make the entire entry visible
7541 if it was hidden in the outline."
7542 (interactive "p")
7543 (let ((win (selected-window)))
7544 (org-agenda-goto t)
7545 (org-recenter-heading 1)
7546 (cond
7547 ((= more 0)
7548 (hide-subtree)
7549 (save-excursion
7550 (org-back-to-heading)
7551 (run-hook-with-args 'org-cycle-hook 'folded))
7552 (message "Remote: FOLDED"))
7553 ((and (org-called-interactively-p 'any) (= more 1))
7554 (message "Remote: show with default settings"))
7555 ((= more 2)
7556 (show-entry)
7557 (show-children)
7558 (save-excursion
7559 (org-back-to-heading)
7560 (run-hook-with-args 'org-cycle-hook 'children))
7561 (message "Remote: CHILDREN"))
7562 ((= more 3)
7563 (show-subtree)
7564 (save-excursion
7565 (org-back-to-heading)
7566 (run-hook-with-args 'org-cycle-hook 'subtree))
7567 (message "Remote: SUBTREE"))
7568 ((= more 4)
7569 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7570 (org-drawer-regexp
7571 (concat "^[ \t]*:\\("
7572 (mapconcat 'regexp-quote org-drawers "\\|")
7573 "\\):[ \t]*$")))
7574 (show-subtree)
7575 (save-excursion
7576 (org-back-to-heading)
7577 (org-cycle-hide-drawers 'subtree)))
7578 (message "Remote: SUBTREE AND LOGBOOK"))
7579 ((> more 4)
7580 (show-subtree)
7581 (message "Remote: SUBTREE AND ALL DRAWERS")))
7582 (select-window win)))
7584 (defun org-recenter-heading (n)
7585 (save-excursion
7586 (org-back-to-heading)
7587 (recenter n)))
7589 (defvar org-agenda-cycle-counter nil)
7590 (defun org-agenda-cycle-show (&optional n)
7591 "Show the current entry in another window, with default settings.
7592 Default settings are taken from `org-show-hierarchy-above' and siblings.
7593 When use repeatedly in immediate succession, the remote entry will cycle
7594 through visibility
7596 children -> subtree -> folded
7598 When called with a numeric prefix arg, that arg will be passed through to
7599 `org-agenda-show-1'. For the interpretation of that argument, see the
7600 docstring of `org-agenda-show-1'."
7601 (interactive "P")
7602 (if (integerp n)
7603 (setq org-agenda-cycle-counter n)
7604 (if (not (eq last-command this-command))
7605 (setq org-agenda-cycle-counter 1)
7606 (if (equal org-agenda-cycle-counter 0)
7607 (setq org-agenda-cycle-counter 2)
7608 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7609 (if (> org-agenda-cycle-counter 3)
7610 (setq org-agenda-cycle-counter 0)))))
7611 (org-agenda-show-1 org-agenda-cycle-counter))
7613 (defun org-agenda-recenter (arg)
7614 "Display the Org-mode file which contains the item at point and recenter."
7615 (interactive "P")
7616 (let ((win (selected-window)))
7617 (org-agenda-goto t)
7618 (recenter arg)
7619 (select-window win)))
7621 (defun org-agenda-show-mouse (ev)
7622 "Display the Org-mode file which contains the item at the mouse click."
7623 (interactive "e")
7624 (mouse-set-point ev)
7625 (org-agenda-show))
7627 (defun org-agenda-check-no-diary ()
7628 "Check if the entry is a diary link and abort if yes."
7629 (if (org-get-at-bol 'org-agenda-diary-link)
7630 (org-agenda-error)))
7632 (defun org-agenda-error ()
7633 (error "Command not allowed in this line"))
7635 (defun org-agenda-tree-to-indirect-buffer ()
7636 "Show the subtree corresponding to the current entry in an indirect buffer.
7637 This calls the command `org-tree-to-indirect-buffer' from the original
7638 Org-mode buffer.
7639 With numerical prefix arg ARG, go up to this level and then take that tree.
7640 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7641 use the dedicated frame)."
7642 (interactive)
7643 (if (and current-prefix-arg (listp current-prefix-arg))
7644 (org-agenda-do-tree-to-indirect-buffer)
7645 (let ((agenda-window (selected-window))
7646 (indirect-window
7647 (and org-last-indirect-buffer
7648 (get-buffer-window org-last-indirect-buffer))))
7649 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer))
7650 (unwind-protect
7651 (progn
7652 (unless (and indirect-window (window-live-p indirect-window))
7653 (setq indirect-window (split-window agenda-window)))
7654 (select-window indirect-window)
7655 (switch-to-buffer org-last-indirect-buffer :norecord)
7656 (fit-window-to-buffer indirect-window))
7657 (select-window (get-buffer-window org-agenda-buffer-name))))))
7659 (defun org-agenda-do-tree-to-indirect-buffer ()
7660 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7661 (org-agenda-check-no-diary)
7662 (let* ((marker (or (org-get-at-bol 'org-marker)
7663 (org-agenda-error)))
7664 (buffer (marker-buffer marker))
7665 (pos (marker-position marker)))
7666 (with-current-buffer buffer
7667 (save-excursion
7668 (goto-char pos)
7669 (call-interactively 'org-tree-to-indirect-buffer)))))
7671 (defvar org-last-heading-marker (make-marker)
7672 "Marker pointing to the headline that last changed its TODO state
7673 by a remote command from the agenda.")
7675 (defun org-agenda-todo-nextset ()
7676 "Switch TODO entry to next sequence."
7677 (interactive)
7678 (org-agenda-todo 'nextset))
7680 (defun org-agenda-todo-previousset ()
7681 "Switch TODO entry to previous sequence."
7682 (interactive)
7683 (org-agenda-todo 'previousset))
7685 (defun org-agenda-todo (&optional arg)
7686 "Cycle TODO state of line at point, also in Org-mode file.
7687 This changes the line at point, all other lines in the agenda referring to
7688 the same tree node, and the headline of the tree node in the Org-mode file."
7689 (interactive "P")
7690 (org-agenda-check-no-diary)
7691 (let* ((col (current-column))
7692 (marker (or (org-get-at-bol 'org-marker)
7693 (org-agenda-error)))
7694 (buffer (marker-buffer marker))
7695 (pos (marker-position marker))
7696 (hdmarker (org-get-at-bol 'org-hd-marker))
7697 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7698 (inhibit-read-only t)
7699 org-agenda-headline-snapshot-before-repeat newhead just-one)
7700 (org-with-remote-undo buffer
7701 (with-current-buffer buffer
7702 (widen)
7703 (goto-char pos)
7704 (org-show-context 'agenda)
7705 (save-excursion
7706 (and (outline-next-heading)
7707 (org-flag-heading nil))) ; show the next heading
7708 (let ((current-prefix-arg arg))
7709 (call-interactively 'org-todo))
7710 (and (bolp) (forward-char 1))
7711 (setq newhead (org-get-heading))
7712 (when (and (org-bound-and-true-p
7713 org-agenda-headline-snapshot-before-repeat)
7714 (not (equal org-agenda-headline-snapshot-before-repeat
7715 newhead))
7716 todayp)
7717 (setq newhead org-agenda-headline-snapshot-before-repeat
7718 just-one t))
7719 (save-excursion
7720 (org-back-to-heading)
7721 (move-marker org-last-heading-marker (point))))
7722 (beginning-of-line 1)
7723 (save-excursion
7724 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7725 (org-move-to-column col))))
7727 (defun org-agenda-add-note (&optional arg)
7728 "Add a time-stamped note to the entry at point."
7729 (interactive "P")
7730 (org-agenda-check-no-diary)
7731 (let* ((marker (or (org-get-at-bol 'org-marker)
7732 (org-agenda-error)))
7733 (buffer (marker-buffer marker))
7734 (pos (marker-position marker))
7735 (hdmarker (org-get-at-bol 'org-hd-marker))
7736 (inhibit-read-only t))
7737 (with-current-buffer buffer
7738 (widen)
7739 (goto-char pos)
7740 (org-show-context 'agenda)
7741 (save-excursion
7742 (and (outline-next-heading)
7743 (org-flag-heading nil))) ; show the next heading
7744 (org-add-note))))
7746 (defun org-agenda-change-all-lines (newhead hdmarker
7747 &optional fixface just-this)
7748 "Change all lines in the agenda buffer which match HDMARKER.
7749 The new content of the line will be NEWHEAD (as modified by
7750 `org-agenda-format-item'). HDMARKER is checked with
7751 `equal' against all `org-hd-marker' text properties in the file.
7752 If FIXFACE is non-nil, the face of each item is modified according to
7753 the new TODO state.
7754 If JUST-THIS is non-nil, change just the current line, not all.
7755 If FORCE-TAGS is non nil, the car of it returns the new tags."
7756 (let* ((inhibit-read-only t)
7757 (line (org-current-line))
7758 (org-agenda-buffer (current-buffer))
7759 (thetags (with-current-buffer (marker-buffer hdmarker)
7760 (save-excursion (save-restriction (widen)
7761 (goto-char hdmarker)
7762 (org-get-tags-at)))))
7763 props m pl undone-face done-face finish new dotime cat tags)
7764 (save-excursion
7765 (goto-char (point-max))
7766 (beginning-of-line 1)
7767 (while (not finish)
7768 (setq finish (bobp))
7769 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7770 (or (not just-this) (= (org-current-line) line))
7771 (equal m hdmarker))
7772 (setq props (text-properties-at (point))
7773 dotime (org-get-at-bol 'dotime)
7774 cat (org-get-at-bol 'org-category)
7775 tags thetags
7777 (let ((org-prefix-format-compiled
7778 (or (get-text-property (point) 'format)
7779 org-prefix-format-compiled))
7780 (extra (org-get-at-bol 'extra)))
7781 (with-current-buffer (marker-buffer hdmarker)
7782 (save-excursion
7783 (save-restriction
7784 (widen)
7785 (org-agenda-format-item extra newhead cat tags dotime)))))
7786 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7787 undone-face (org-get-at-bol 'undone-face)
7788 done-face (org-get-at-bol 'done-face))
7789 (beginning-of-line 1)
7790 (cond
7791 ((equal new "")
7792 (and (looking-at ".*\n?") (replace-match "")))
7793 ((looking-at ".*")
7794 (replace-match new t t)
7795 (beginning-of-line 1)
7796 (add-text-properties (point-at-bol) (point-at-eol) props)
7797 (when fixface
7798 (add-text-properties
7799 (point-at-bol) (point-at-eol)
7800 (list 'face
7801 (if org-last-todo-state-is-todo
7802 undone-face done-face))))
7803 (org-agenda-highlight-todo 'line)
7804 (beginning-of-line 1))
7805 (t (error "Line update did not work"))))
7806 (beginning-of-line 0)))
7807 (org-finalize-agenda)))
7809 (defun org-agenda-align-tags (&optional line)
7810 "Align all tags in agenda items to `org-agenda-tags-column'."
7811 (let ((inhibit-read-only t) l c)
7812 (save-excursion
7813 (goto-char (if line (point-at-bol) (point-min)))
7814 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7815 (if line (point-at-eol) nil) t)
7816 (add-text-properties
7817 (match-beginning 2) (match-end 2)
7818 (list 'face (delq nil (let ((prop (get-text-property
7819 (match-beginning 2) 'face)))
7820 (or (listp prop) (setq prop (list prop)))
7821 (if (memq 'org-tag prop)
7822 prop
7823 (cons 'org-tag prop))))))
7824 (setq l (- (match-end 2) (match-beginning 2))
7825 c (if (< org-agenda-tags-column 0)
7826 (- (abs org-agenda-tags-column) l)
7827 org-agenda-tags-column))
7828 (delete-region (match-beginning 1) (match-end 1))
7829 (goto-char (match-beginning 1))
7830 (insert (org-add-props
7831 (make-string (max 1 (- c (current-column))) ?\ )
7832 (plist-put (copy-sequence (text-properties-at (point)))
7833 'face nil))))
7834 (goto-char (point-min))
7835 (org-font-lock-add-tag-faces (point-max)))))
7837 (defun org-agenda-priority-up ()
7838 "Increase the priority of line at point, also in Org-mode file."
7839 (interactive)
7840 (org-agenda-priority 'up))
7842 (defun org-agenda-priority-down ()
7843 "Decrease the priority of line at point, also in Org-mode file."
7844 (interactive)
7845 (org-agenda-priority 'down))
7847 (defun org-agenda-priority (&optional force-direction)
7848 "Set the priority of line at point, also in Org-mode file.
7849 This changes the line at point, all other lines in the agenda referring to
7850 the same tree node, and the headline of the tree node in the Org-mode file."
7851 (interactive)
7852 (unless org-enable-priority-commands
7853 (error "Priority commands are disabled"))
7854 (org-agenda-check-no-diary)
7855 (let* ((marker (or (org-get-at-bol 'org-marker)
7856 (org-agenda-error)))
7857 (hdmarker (org-get-at-bol 'org-hd-marker))
7858 (buffer (marker-buffer hdmarker))
7859 (pos (marker-position hdmarker))
7860 (inhibit-read-only t)
7861 newhead)
7862 (org-with-remote-undo buffer
7863 (with-current-buffer buffer
7864 (widen)
7865 (goto-char pos)
7866 (org-show-context 'agenda)
7867 (save-excursion
7868 (and (outline-next-heading)
7869 (org-flag-heading nil))) ; show the next heading
7870 (funcall 'org-priority force-direction)
7871 (end-of-line 1)
7872 (setq newhead (org-get-heading)))
7873 (org-agenda-change-all-lines newhead hdmarker)
7874 (beginning-of-line 1))))
7876 ;; FIXME: should fix the tags property of the agenda line.
7877 (defun org-agenda-set-tags (&optional tag onoff)
7878 "Set tags for the current headline."
7879 (interactive)
7880 (org-agenda-check-no-diary)
7881 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7882 (call-interactively 'org-change-tag-in-region)
7883 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7884 (org-agenda-error)))
7885 (buffer (marker-buffer hdmarker))
7886 (pos (marker-position hdmarker))
7887 (inhibit-read-only t)
7888 newhead)
7889 (org-with-remote-undo buffer
7890 (with-current-buffer buffer
7891 (widen)
7892 (goto-char pos)
7893 (save-excursion
7894 (org-show-context 'agenda))
7895 (save-excursion
7896 (and (outline-next-heading)
7897 (org-flag-heading nil))) ; show the next heading
7898 (goto-char pos)
7899 (if tag
7900 (org-toggle-tag tag onoff)
7901 (call-interactively 'org-set-tags))
7902 (end-of-line 1)
7903 (setq newhead (org-get-heading)))
7904 (org-agenda-change-all-lines newhead hdmarker)
7905 (beginning-of-line 1)))))
7907 (defun org-agenda-set-property ()
7908 "Set a property for the current headline."
7909 (interactive)
7910 (org-agenda-check-no-diary)
7911 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7912 (org-agenda-error)))
7913 (buffer (marker-buffer hdmarker))
7914 (pos (marker-position hdmarker))
7915 (inhibit-read-only t)
7916 newhead)
7917 (org-with-remote-undo buffer
7918 (with-current-buffer buffer
7919 (widen)
7920 (goto-char pos)
7921 (save-excursion
7922 (org-show-context 'agenda))
7923 (save-excursion
7924 (and (outline-next-heading)
7925 (org-flag-heading nil))) ; show the next heading
7926 (goto-char pos)
7927 (call-interactively 'org-set-property)))))
7929 (defun org-agenda-set-effort ()
7930 "Set the effort property for the current headline."
7931 (interactive)
7932 (org-agenda-check-no-diary)
7933 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7934 (org-agenda-error)))
7935 (buffer (marker-buffer hdmarker))
7936 (pos (marker-position hdmarker))
7937 (inhibit-read-only t)
7938 newhead)
7939 (org-with-remote-undo buffer
7940 (with-current-buffer buffer
7941 (widen)
7942 (goto-char pos)
7943 (save-excursion
7944 (org-show-context 'agenda))
7945 (save-excursion
7946 (and (outline-next-heading)
7947 (org-flag-heading nil))) ; show the next heading
7948 (goto-char pos)
7949 (call-interactively 'org-set-effort)
7950 (end-of-line 1)
7951 (setq newhead (org-get-heading)))
7952 (org-agenda-change-all-lines newhead hdmarker))))
7954 (defun org-agenda-toggle-archive-tag ()
7955 "Toggle the archive tag for the current entry."
7956 (interactive)
7957 (org-agenda-check-no-diary)
7958 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7959 (org-agenda-error)))
7960 (buffer (marker-buffer hdmarker))
7961 (pos (marker-position hdmarker))
7962 (inhibit-read-only t)
7963 newhead)
7964 (org-with-remote-undo buffer
7965 (with-current-buffer buffer
7966 (widen)
7967 (goto-char pos)
7968 (org-show-context 'agenda)
7969 (save-excursion
7970 (and (outline-next-heading)
7971 (org-flag-heading nil))) ; show the next heading
7972 (call-interactively 'org-toggle-archive-tag)
7973 (end-of-line 1)
7974 (setq newhead (org-get-heading)))
7975 (org-agenda-change-all-lines newhead hdmarker)
7976 (beginning-of-line 1))))
7978 (defun org-agenda-do-date-later (arg)
7979 (interactive "P")
7980 (cond
7981 ((or (equal arg '(16))
7982 (memq last-command
7983 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7984 (setq this-command 'org-agenda-date-later-minutes)
7985 (org-agenda-date-later-minutes 1))
7986 ((or (equal arg '(4))
7987 (memq last-command
7988 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7989 (setq this-command 'org-agenda-date-later-hours)
7990 (org-agenda-date-later-hours 1))
7992 (org-agenda-date-later (prefix-numeric-value arg)))))
7994 (defun org-agenda-do-date-earlier (arg)
7995 (interactive "P")
7996 (cond
7997 ((or (equal arg '(16))
7998 (memq last-command
7999 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
8000 (setq this-command 'org-agenda-date-earlier-minutes)
8001 (org-agenda-date-earlier-minutes 1))
8002 ((or (equal arg '(4))
8003 (memq last-command
8004 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8005 (setq this-command 'org-agenda-date-earlier-hours)
8006 (org-agenda-date-earlier-hours 1))
8008 (org-agenda-date-earlier (prefix-numeric-value arg)))))
8010 (defun org-agenda-date-later (arg &optional what)
8011 "Change the date of this item to ARG day(s) later."
8012 (interactive "p")
8013 (org-agenda-check-type t 'agenda 'timeline)
8014 (org-agenda-check-no-diary)
8015 (let* ((marker (or (org-get-at-bol 'org-marker)
8016 (org-agenda-error)))
8017 (buffer (marker-buffer marker))
8018 (pos (marker-position marker))
8019 cdate today)
8020 (org-with-remote-undo buffer
8021 (with-current-buffer buffer
8022 (widen)
8023 (goto-char pos)
8024 (if (not (org-at-timestamp-p))
8025 (error "Cannot find time stamp"))
8026 (when (and org-agenda-move-date-from-past-immediately-to-today
8027 (equal arg 1)
8028 (or (not what) (eq what 'day))
8029 (not (save-match-data (org-at-date-range-p))))
8030 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
8031 cdate (calendar-absolute-from-gregorian
8032 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
8033 today (org-today))
8034 (if (> today cdate)
8035 ;; immediately shift to today
8036 (setq arg (- today cdate))))
8037 (org-timestamp-change arg (or what 'day))
8038 (when (and (org-at-date-range-p)
8039 (re-search-backward org-tr-regexp-both (point-at-bol)))
8040 (let ((end org-last-changed-timestamp))
8041 (org-timestamp-change arg (or what 'day))
8042 (setq org-last-changed-timestamp
8043 (concat org-last-changed-timestamp "--" end)))))
8044 (org-agenda-show-new-time marker org-last-changed-timestamp))
8045 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8047 (defun org-agenda-date-earlier (arg &optional what)
8048 "Change the date of this item to ARG day(s) earlier."
8049 (interactive "p")
8050 (org-agenda-date-later (- arg) what))
8052 (defun org-agenda-date-later-minutes (arg)
8053 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8054 (interactive "p")
8055 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8056 (org-agenda-date-later arg 'minute))
8058 (defun org-agenda-date-earlier-minutes (arg)
8059 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8060 (interactive "p")
8061 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8062 (org-agenda-date-earlier arg 'minute))
8064 (defun org-agenda-date-later-hours (arg)
8065 "Change the time of this item, in hour steps."
8066 (interactive "p")
8067 (org-agenda-date-later arg 'hour))
8069 (defun org-agenda-date-earlier-hours (arg)
8070 "Change the time of this item, in hour steps."
8071 (interactive "p")
8072 (org-agenda-date-earlier arg 'hour))
8074 (defun org-agenda-show-new-time (marker stamp &optional prefix)
8075 "Show new date stamp via text properties."
8076 ;; We use text properties to make this undoable
8077 (let ((inhibit-read-only t)
8078 (buffer-invisibility-spec))
8079 (setq stamp (concat " " prefix " => " stamp))
8080 (save-excursion
8081 (goto-char (point-max))
8082 (while (not (bobp))
8083 (when (equal marker (org-get-at-bol 'org-marker))
8084 (org-move-to-column (- (window-width) (length stamp)) t)
8085 (org-agenda-fix-tags-filter-overlays-at (point))
8086 (if (featurep 'xemacs)
8087 ;; Use `duplicable' property to trigger undo recording
8088 (let ((ex (make-extent nil nil))
8089 (gl (make-glyph stamp)))
8090 (set-glyph-face gl 'secondary-selection)
8091 (set-extent-properties
8092 ex (list 'invisible t 'end-glyph gl 'duplicable t))
8093 (insert-extent ex (1- (point)) (point-at-eol)))
8094 (add-text-properties
8095 (1- (point)) (point-at-eol)
8096 (list 'display (org-add-props stamp nil
8097 'face 'secondary-selection))))
8098 (beginning-of-line 1))
8099 (beginning-of-line 0)))))
8101 (defun org-agenda-date-prompt (arg)
8102 "Change the date of this item. Date is prompted for, with default today.
8103 The prefix ARG is passed to the `org-time-stamp' command and can therefore
8104 be used to request time specification in the time stamp."
8105 (interactive "P")
8106 (org-agenda-check-type t 'agenda 'timeline)
8107 (org-agenda-check-no-diary)
8108 (let* ((marker (or (org-get-at-bol 'org-marker)
8109 (org-agenda-error)))
8110 (buffer (marker-buffer marker))
8111 (pos (marker-position marker)))
8112 (org-with-remote-undo buffer
8113 (with-current-buffer buffer
8114 (widen)
8115 (goto-char pos)
8116 (if (not (org-at-timestamp-p t))
8117 (error "Cannot find time stamp"))
8118 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8119 (org-agenda-show-new-time marker org-last-changed-timestamp))
8120 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8122 (defun org-agenda-schedule (arg &optional time)
8123 "Schedule the item at point.
8124 ARG is passed through to `org-schedule'."
8125 (interactive "P")
8126 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8127 (org-agenda-check-no-diary)
8128 (let* ((marker (or (org-get-at-bol 'org-marker)
8129 (org-agenda-error)))
8130 (type (marker-insertion-type marker))
8131 (buffer (marker-buffer marker))
8132 (pos (marker-position marker))
8133 (org-insert-labeled-timestamps-at-point nil)
8135 (set-marker-insertion-type marker t)
8136 (org-with-remote-undo buffer
8137 (with-current-buffer buffer
8138 (widen)
8139 (goto-char pos)
8140 (setq ts (org-schedule arg time)))
8141 (org-agenda-show-new-time marker ts "S"))
8142 (message "Item scheduled for %s" ts)))
8144 (defun org-agenda-deadline (arg &optional time)
8145 "Schedule the item at point.
8146 ARG is passed through to `org-deadline'."
8147 (interactive "P")
8148 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8149 (org-agenda-check-no-diary)
8150 (let* ((marker (or (org-get-at-bol 'org-marker)
8151 (org-agenda-error)))
8152 (buffer (marker-buffer marker))
8153 (pos (marker-position marker))
8154 (org-insert-labeled-timestamps-at-point nil)
8156 (org-with-remote-undo buffer
8157 (with-current-buffer buffer
8158 (widen)
8159 (goto-char pos)
8160 (setq ts (org-deadline arg time)))
8161 (org-agenda-show-new-time marker ts "D"))
8162 (message "Deadline for this item set to %s" ts)))
8164 (defun org-agenda-action ()
8165 "Select entry for agenda action, or execute an agenda action.
8166 This command prompts for another letter. Valid inputs are:
8168 m Mark the entry at point for an agenda action
8169 s Schedule the marked entry to the date at the cursor
8170 d Set the deadline of the marked entry to the date at the cursor
8171 r Call `org-remember' with cursor date as the default date
8172 c Call `org-capture' with cursor date as the default date
8173 SPC Show marked entry in other window
8174 TAB Visit marked entry in other window
8176 The cursor may be at a date in the calendar, or in the Org agenda."
8177 (interactive)
8178 (let (ans)
8179 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
8180 (setq ans (read-char-exclusive))
8181 (cond
8182 ((equal ans ?m)
8183 ;; Mark this entry
8184 (if (eq major-mode 'org-agenda-mode)
8185 (let ((m (or (org-get-at-bol 'org-hd-marker)
8186 (org-get-at-bol 'org-marker))))
8187 (if m
8188 (progn
8189 (move-marker org-agenda-action-marker
8190 (marker-position m) (marker-buffer m))
8191 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
8192 (error "Don't know which entry to mark")))
8193 (error "This command works only in the agenda")))
8194 ((equal ans ?s)
8195 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
8196 ((equal ans ?d)
8197 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
8198 ((equal ans ?r)
8199 (org-agenda-do-action '(org-remember) t))
8200 ((equal ans ?c)
8201 (org-agenda-do-action '(org-capture) t))
8202 ((equal ans ?\ )
8203 (let ((cw (selected-window)))
8204 (org-switch-to-buffer-other-window
8205 (marker-buffer org-agenda-action-marker))
8206 (goto-char org-agenda-action-marker)
8207 (org-show-context 'agenda)
8208 (select-window cw)))
8209 ((equal ans ?\C-i)
8210 (org-switch-to-buffer-other-window
8211 (marker-buffer org-agenda-action-marker))
8212 (goto-char org-agenda-action-marker)
8213 (org-show-context 'agenda))
8214 (t (error "Invalid agenda action %c" ans)))))
8216 (defun org-agenda-do-action (form &optional current-buffer)
8217 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
8218 (let ((org-overriding-default-time (org-get-cursor-date)))
8219 (if current-buffer
8220 (eval form)
8221 (if (not (marker-buffer org-agenda-action-marker))
8222 (error "No entry has been selected for agenda action")
8223 (with-current-buffer (marker-buffer org-agenda-action-marker)
8224 (save-excursion
8225 (save-restriction
8226 (widen)
8227 (goto-char org-agenda-action-marker)
8228 (eval form))))))))
8230 (defun org-agenda-clock-in (&optional arg)
8231 "Start the clock on the currently selected item."
8232 (interactive "P")
8233 (org-agenda-check-no-diary)
8234 (if (equal arg '(4))
8235 (org-clock-in arg)
8236 (let* ((marker (or (org-get-at-bol 'org-marker)
8237 (org-agenda-error)))
8238 (hdmarker (or (org-get-at-bol 'org-hd-marker)
8239 marker))
8240 (pos (marker-position marker))
8241 newhead)
8242 (org-with-remote-undo (marker-buffer marker)
8243 (with-current-buffer (marker-buffer marker)
8244 (widen)
8245 (goto-char pos)
8246 (org-show-context 'agenda)
8247 (org-show-entry)
8248 (org-cycle-hide-drawers 'children)
8249 (org-clock-in arg)
8250 (setq newhead (org-get-heading)))
8251 (org-agenda-change-all-lines newhead hdmarker)))))
8253 (defun org-agenda-clock-out ()
8254 "Stop the currently running clock."
8255 (interactive)
8256 (unless (marker-buffer org-clock-marker)
8257 (error "No running clock"))
8258 (let ((marker (make-marker)) newhead)
8259 (org-with-remote-undo (marker-buffer org-clock-marker)
8260 (with-current-buffer (marker-buffer org-clock-marker)
8261 (save-excursion
8262 (save-restriction
8263 (widen)
8264 (goto-char org-clock-marker)
8265 (org-back-to-heading t)
8266 (move-marker marker (point))
8267 (org-clock-out)
8268 (setq newhead (org-get-heading))))))
8269 (org-agenda-change-all-lines newhead marker)
8270 (move-marker marker nil)))
8272 (defun org-agenda-clock-cancel (&optional arg)
8273 "Cancel the currently running clock."
8274 (interactive "P")
8275 (unless (marker-buffer org-clock-marker)
8276 (error "No running clock"))
8277 (org-with-remote-undo (marker-buffer org-clock-marker)
8278 (org-clock-cancel)))
8280 (defun org-agenda-clock-goto ()
8281 "Jump to the currently clocked in task within the agenda.
8282 If the currently clocked in task is not listed in the agenda
8283 buffer, display it in another window."
8284 (interactive)
8285 (let (pos)
8286 (mapc (lambda (o)
8287 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
8288 (setq pos (overlay-start o))))
8289 (overlays-in (point-min) (point-max)))
8290 (cond (pos (goto-char pos))
8291 ;; If the currently clocked entry is not in the agenda
8292 ;; buffer, we visit it in another window:
8293 (org-clock-current-task
8294 (org-switch-to-buffer-other-window (org-clock-goto)))
8295 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
8297 (defun org-agenda-diary-entry-in-org-file ()
8298 "Make a diary entry in the file `org-agenda-diary-file'."
8299 (let (d1 d2 char (text "") dp1 dp2)
8300 (if (equal (buffer-name) "*Calendar*")
8301 (setq d1 (calendar-cursor-to-date t)
8302 d2 (car calendar-mark-ring))
8303 (setq dp1 (get-text-property (point-at-bol) 'day))
8304 (unless dp1 (error "No date defined in current line"))
8305 (setq d1 (calendar-gregorian-from-absolute dp1)
8306 d2 (and (ignore-errors (mark))
8307 (save-excursion
8308 (goto-char (mark))
8309 (setq dp2 (get-text-property (point-at-bol) 'day)))
8310 (calendar-gregorian-from-absolute dp2))))
8311 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
8312 (setq char (read-char-exclusive))
8313 (cond
8314 ((equal char ?d)
8315 (setq text (read-string "Day entry: "))
8316 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
8317 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8318 ((equal char ?a)
8319 (setq d1 (list (car d1) (nth 1 d1)
8320 (read-number (format "Reference year [%d]: " (nth 2 d1))
8321 (nth 2 d1))))
8322 (setq text (read-string "Anniversary (use %d to show years): "))
8323 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
8324 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8325 ((equal char ?b)
8326 (setq text (read-string "Block entry: "))
8327 (unless (and d1 d2 (not (equal d1 d2)))
8328 (error "No block of days selected"))
8329 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
8330 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8331 ((equal char ?j)
8332 (org-switch-to-buffer-other-window
8333 (find-file-noselect org-agenda-diary-file))
8334 (require 'org-datetree)
8335 (org-datetree-find-date-create d1)
8336 (org-reveal t))
8337 (t (error "Invalid selection character `%c'" char)))))
8339 (defcustom org-agenda-insert-diary-strategy 'date-tree
8340 "Where in `org-agenda-diary-file' should new entries be added?
8341 Valid values:
8343 date-tree in the date tree, as child of the date
8344 top-level as top-level entries at the end of the file."
8345 :group 'org-agenda
8346 :type '(choice
8347 (const :tag "in a date tree" date-tree)
8348 (const :tag "as top level at end of file" top-level)))
8350 (defcustom org-agenda-insert-diary-extract-time nil
8351 "Non-nil means extract any time specification from the diary entry."
8352 :group 'org-agenda
8353 :version "24.1"
8354 :type 'boolean)
8356 (defcustom org-agenda-bulk-mark-char ">"
8357 "A single-character string to be used as the bulk mark."
8358 :group 'org-agenda
8359 :version "24.1"
8360 :type 'string)
8362 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8363 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8364 If TEXT is not empty, it will become the headline of the new entry, and
8365 the resulting entry will not be shown. When TEXT is empty, switch to
8366 `org-agenda-diary-file' and let the user finish the entry there."
8367 (let ((cw (current-window-configuration)))
8368 (org-switch-to-buffer-other-window
8369 (find-file-noselect org-agenda-diary-file))
8370 (widen)
8371 (goto-char (point-min))
8372 (cond
8373 ((eq type 'anniversary)
8374 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8375 (progn
8376 (or (org-at-heading-p t)
8377 (progn
8378 (outline-next-heading)
8379 (insert "* Anniversaries\n\n")
8380 (beginning-of-line -1)))))
8381 (outline-next-heading)
8382 (org-back-over-empty-lines)
8383 (backward-char 1)
8384 (insert "\n")
8385 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8386 (nth 2 d1) (car d1) (nth 1 d1) text)))
8387 ((eq type 'day)
8388 (let ((org-prefix-has-time t)
8389 (org-agenda-time-leading-zero t)
8390 fmt time time2)
8391 (if org-agenda-insert-diary-extract-time
8392 ;; Use org-agenda-format-item to parse text for a time-range and
8393 ;; remove it. FIXME: This is a hack, we should refactor
8394 ;; that function to make time extraction available separately
8395 (setq fmt (org-agenda-format-item nil text nil nil t)
8396 time (get-text-property 0 'time fmt)
8397 time2 (if (> (length time) 0)
8398 ;; split-string removes trailing ...... if
8399 ;; no end time given. First space
8400 ;; separates time from date.
8401 (concat " " (car (split-string time "\\.")))
8402 nil)
8403 text (get-text-property 0 'txt fmt)))
8404 (if (eq org-agenda-insert-diary-strategy 'top-level)
8405 (org-agenda-insert-diary-as-top-level text)
8406 (require 'org-datetree)
8407 (org-datetree-find-date-create d1)
8408 (org-agenda-insert-diary-make-new-entry text))
8409 (org-insert-time-stamp (org-time-from-absolute
8410 (calendar-absolute-from-gregorian d1))
8411 nil nil nil nil time2))
8412 (end-of-line 0))
8413 ((eq type 'block)
8414 (if (> (calendar-absolute-from-gregorian d1)
8415 (calendar-absolute-from-gregorian d2))
8416 (setq d1 (prog1 d2 (setq d2 d1))))
8417 (if (eq org-agenda-insert-diary-strategy 'top-level)
8418 (org-agenda-insert-diary-as-top-level text)
8419 (require 'org-datetree)
8420 (org-datetree-find-date-create d1)
8421 (org-agenda-insert-diary-make-new-entry text))
8422 (org-insert-time-stamp (org-time-from-absolute
8423 (calendar-absolute-from-gregorian d1)))
8424 (insert "--")
8425 (org-insert-time-stamp (org-time-from-absolute
8426 (calendar-absolute-from-gregorian d2)))
8427 (end-of-line 0)))
8428 (if (string-match "\\S-" text)
8429 (progn
8430 (set-window-configuration cw)
8431 (message "%s entry added to %s"
8432 (capitalize (symbol-name type))
8433 (abbreviate-file-name org-agenda-diary-file)))
8434 (org-reveal t)
8435 (message "Please finish entry here"))))
8437 (defun org-agenda-insert-diary-as-top-level (text)
8438 "Make new entry as a top-level entry at the end of the file.
8439 Add TEXT as headline, and position the cursor in the second line so that
8440 a timestamp can be added there."
8441 (widen)
8442 (goto-char (point-max))
8443 (or (bolp) (insert "\n"))
8444 (insert "* " text "\n")
8445 (if org-adapt-indentation (org-indent-to-column 2)))
8447 (defun org-agenda-insert-diary-make-new-entry (text)
8448 "Make new entry as last child of current entry.
8449 Add TEXT as headline, and position the cursor in the second line so that
8450 a timestamp can be added there."
8451 (let ((org-show-following-heading t)
8452 (org-show-siblings t)
8453 (org-show-hierarchy-above t)
8454 (org-show-entry-below t)
8455 col)
8456 (outline-next-heading)
8457 (org-back-over-empty-lines)
8458 (or (looking-at "[ \t]*$")
8459 (progn (insert "\n") (backward-char 1)))
8460 (org-insert-heading nil t)
8461 (org-do-demote)
8462 (setq col (current-column))
8463 (insert text "\n")
8464 (if org-adapt-indentation (org-indent-to-column col))
8465 (let ((org-show-following-heading t)
8466 (org-show-siblings t)
8467 (org-show-hierarchy-above t)
8468 (org-show-entry-below t))
8469 (org-show-context))))
8471 (defun org-agenda-diary-entry ()
8472 "Make a diary entry, like the `i' command from the calendar.
8473 All the standard commands work: block, weekly etc.
8474 When `org-agenda-diary-file' points to a file,
8475 `org-agenda-diary-entry-in-org-file' is called instead to create
8476 entries in that Org-mode file."
8477 (interactive)
8478 (org-agenda-check-type t 'agenda 'timeline)
8479 (if (not (eq org-agenda-diary-file 'diary-file))
8480 (org-agenda-diary-entry-in-org-file)
8481 (require 'diary-lib)
8482 (let* ((char (progn
8483 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8484 (read-char-exclusive)))
8485 (cmd (cdr (assoc char
8486 '((?d . insert-diary-entry)
8487 (?w . insert-weekly-diary-entry)
8488 (?m . insert-monthly-diary-entry)
8489 (?y . insert-yearly-diary-entry)
8490 (?a . insert-anniversary-diary-entry)
8491 (?b . insert-block-diary-entry)
8492 (?c . insert-cyclic-diary-entry)))))
8493 (oldf (symbol-function 'calendar-cursor-to-date))
8494 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8495 (point (point))
8496 (mark (or (mark t) (point))))
8497 (unless cmd
8498 (error "No command associated with <%c>" char))
8499 (unless (and (get-text-property point 'day)
8500 (or (not (equal ?b char))
8501 (get-text-property mark 'day)))
8502 (error "Don't know which date to use for diary entry"))
8503 ;; We implement this by hacking the `calendar-cursor-to-date' function
8504 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8505 (let ((calendar-mark-ring
8506 (list (calendar-gregorian-from-absolute
8507 (or (get-text-property mark 'day)
8508 (get-text-property point 'day))))))
8509 (unwind-protect
8510 (progn
8511 (fset 'calendar-cursor-to-date
8512 (lambda (&optional error dummy)
8513 (calendar-gregorian-from-absolute
8514 (get-text-property point 'day))))
8515 (call-interactively cmd))
8516 (fset 'calendar-cursor-to-date oldf))))))
8518 (defun org-agenda-execute-calendar-command (cmd)
8519 "Execute a calendar command from the agenda, with the date associated to
8520 the cursor position."
8521 (org-agenda-check-type t 'agenda 'timeline)
8522 (require 'diary-lib)
8523 (unless (get-text-property (point) 'day)
8524 (error "Don't know which date to use for calendar command"))
8525 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8526 (point (point))
8527 (date (calendar-gregorian-from-absolute
8528 (get-text-property point 'day)))
8529 ;; the following 2 vars are needed in the calendar
8530 (displayed-month (car date))
8531 (displayed-year (nth 2 date)))
8532 (unwind-protect
8533 (progn
8534 (fset 'calendar-cursor-to-date
8535 (lambda (&optional error dummy)
8536 (calendar-gregorian-from-absolute
8537 (get-text-property point 'day))))
8538 (call-interactively cmd))
8539 (fset 'calendar-cursor-to-date oldf))))
8541 (defun org-agenda-phases-of-moon ()
8542 "Display the phases of the moon for the 3 months around the cursor date."
8543 (interactive)
8544 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8546 (defun org-agenda-holidays ()
8547 "Display the holidays for the 3 months around the cursor date."
8548 (interactive)
8549 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8551 (defvar calendar-longitude)
8552 (defvar calendar-latitude)
8553 (defvar calendar-location-name)
8555 (defun org-agenda-sunrise-sunset (arg)
8556 "Display sunrise and sunset for the cursor date.
8557 Latitude and longitude can be specified with the variables
8558 `calendar-latitude' and `calendar-longitude'. When called with prefix
8559 argument, latitude and longitude will be prompted for."
8560 (interactive "P")
8561 (require 'solar)
8562 (let ((calendar-longitude (if arg nil calendar-longitude))
8563 (calendar-latitude (if arg nil calendar-latitude))
8564 (calendar-location-name
8565 (if arg "the given coordinates" calendar-location-name)))
8566 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8568 (defun org-agenda-goto-calendar ()
8569 "Open the Emacs calendar with the date at the cursor."
8570 (interactive)
8571 (org-agenda-check-type t 'agenda 'timeline)
8572 (let* ((day (or (get-text-property (point) 'day)
8573 (error "Don't know which date to open in calendar")))
8574 (date (calendar-gregorian-from-absolute day))
8575 (calendar-move-hook nil)
8576 (calendar-view-holidays-initially-flag nil)
8577 (calendar-view-diary-initially-flag nil))
8578 (calendar)
8579 (calendar-goto-date date)))
8581 ;;;###autoload
8582 (defun org-calendar-goto-agenda ()
8583 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8584 This is a command that has to be installed in `calendar-mode-map'."
8585 (interactive)
8586 (org-agenda-list nil (calendar-absolute-from-gregorian
8587 (calendar-cursor-to-date))
8588 nil))
8590 (defun org-agenda-convert-date ()
8591 (interactive)
8592 (org-agenda-check-type t 'agenda 'timeline)
8593 (let ((day (get-text-property (point) 'day))
8594 date s)
8595 (unless day
8596 (error "Don't know which date to convert"))
8597 (setq date (calendar-gregorian-from-absolute day))
8598 (setq s (concat
8599 "Gregorian: " (calendar-date-string date) "\n"
8600 "ISO: " (calendar-iso-date-string date) "\n"
8601 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8602 "Julian: " (calendar-julian-date-string date) "\n"
8603 "Astron. JD: " (calendar-astro-date-string date)
8604 " (Julian date number at noon UTC)\n"
8605 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8606 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8607 "French: " (calendar-french-date-string date) "\n"
8608 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8609 "Mayan: " (calendar-mayan-date-string date) "\n"
8610 "Coptic: " (calendar-coptic-date-string date) "\n"
8611 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8612 "Persian: " (calendar-persian-date-string date) "\n"
8613 "Chinese: " (calendar-chinese-date-string date) "\n"))
8614 (with-output-to-temp-buffer "*Dates*"
8615 (princ s))
8616 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8618 ;;; Bulk commands
8620 (defvar org-agenda-bulk-marked-entries nil
8621 "List of markers that refer to marked entries in the agenda.")
8623 (defun org-agenda-bulk-marked-p ()
8624 (eq (get-char-property (point-at-bol) 'type)
8625 'org-marked-entry-overlay))
8627 (defun org-agenda-bulk-mark (&optional arg)
8628 "Mark the entry at point for future bulk action."
8629 (interactive "p")
8630 (dotimes (i (or arg 1))
8631 (unless (org-get-at-bol 'org-agenda-diary-link)
8632 (let* ((m (org-get-at-bol 'org-hd-marker))
8634 (unless (org-agenda-bulk-marked-p)
8635 (unless m (error "Nothing to mark at point"))
8636 (push m org-agenda-bulk-marked-entries)
8637 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8638 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
8639 (org-get-todo-face "TODO")
8640 'evaporate)
8641 (overlay-put ov 'type 'org-marked-entry-overlay))
8642 (beginning-of-line 2)
8643 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8644 (beginning-of-line 2))
8645 (message "%d entries marked for bulk action"
8646 (length org-agenda-bulk-marked-entries))))))
8648 (defun org-agenda-bulk-mark-all ()
8649 "Mark all entries for future agenda bulk action."
8650 (interactive)
8651 (org-agenda-bulk-mark-regexp "."))
8653 (defun org-agenda-bulk-mark-regexp (regexp)
8654 "Mark entries matching REGEXP for future agenda bulk action."
8655 (interactive "sMark entries matching regexp: ")
8656 (let ((entries-marked 0))
8657 (save-excursion
8658 (goto-char (point-min))
8659 (goto-char (next-single-property-change (point) 'txt))
8660 (while (re-search-forward regexp nil t)
8661 (when (string-match regexp (get-text-property (point) 'txt))
8662 (setq entries-marked (1+ entries-marked))
8663 (call-interactively 'org-agenda-bulk-mark))))
8664 (if (not entries-marked)
8665 (message "No entry matching this regexp."))))
8667 (defun org-agenda-bulk-unmark (&optional arg)
8668 "Unmark the entry at point for future bulk action."
8669 (interactive "P")
8670 (if arg
8671 (org-agenda-bulk-unmark-all)
8672 (cond ((org-agenda-bulk-marked-p)
8673 (org-agenda-bulk-remove-overlays
8674 (point-at-bol) (+ 2 (point-at-bol)))
8675 (setq org-agenda-bulk-marked-entries
8676 (delete (org-get-at-bol 'org-hd-marker)
8677 org-agenda-bulk-marked-entries))
8678 (beginning-of-line 2)
8679 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8680 (beginning-of-line 2))
8681 (message "%d entries left marked for bulk action"
8682 (length org-agenda-bulk-marked-entries)))
8683 (t (message "No entry to unmark here")))))
8685 (defun org-agenda-bulk-toggle ()
8686 "Toggle marking the entry at point for bulk action."
8687 (interactive)
8688 (if (org-agenda-bulk-marked-p)
8689 (org-agenda-bulk-unmark)
8690 (org-agenda-bulk-mark)))
8692 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8693 "Remove the mark overlays between BEG and END in the agenda buffer.
8694 BEG and END default to the buffer limits.
8696 This only removes the overlays, it does not remove the markers
8697 from the list in `org-agenda-bulk-marked-entries'."
8698 (interactive)
8699 (mapc (lambda (ov)
8700 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8701 (delete-overlay ov)))
8702 (overlays-in (or beg (point-min)) (or end (point-max)))))
8704 (defun org-agenda-bulk-unmark-all ()
8705 "Remove all marks in the agenda buffer.
8706 This will remove the markers and the overlays."
8707 (interactive)
8708 (if (null org-agenda-bulk-marked-entries)
8709 (message "No entry to unmark")
8710 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8711 (setq org-agenda-bulk-marked-entries nil)
8712 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
8714 (defcustom org-agenda-persistent-marks nil
8715 "Non-nil means marked items will stay marked after a bulk action.
8716 You can interactively and temporarily toggle by typing `p' when you
8717 are prompted for a bulk action."
8718 :group 'org-agenda
8719 :version "24.1"
8720 :type 'boolean)
8722 (defun org-agenda-bulk-action (&optional arg)
8723 "Execute an remote-editing action on all marked entries.
8724 The prefix arg is passed through to the command if possible."
8725 (interactive "P")
8726 ;; Make sure we have markers, and only valid ones
8727 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8728 (mapc
8729 (lambda (m)
8730 (unless (and (markerp m)
8731 (marker-buffer m)
8732 (buffer-live-p (marker-buffer m))
8733 (marker-position m))
8734 (error "Marker %s for bulk command is invalid" m)))
8735 org-agenda-bulk-marked-entries)
8737 ;; Prompt for the bulk command
8738 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
8739 (message (concat msg "[r]efile [$]arch [A]rch->sib [t]odo"
8740 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction "
8741 (when org-agenda-bulk-custom-functions
8742 (concat " Custom: ["
8743 (mapconcat (lambda(f) (char-to-string (car f)))
8744 org-agenda-bulk-custom-functions "")
8745 "]"))))
8746 (catch 'exit
8747 (let* ((action (read-char-exclusive))
8748 (org-log-refile (if org-log-refile 'time nil))
8749 (entries (reverse org-agenda-bulk-marked-entries))
8750 redo-at-end
8751 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8752 (cond
8753 ((equal action ?p)
8754 (let ((org-agenda-persistent-marks
8755 (not org-agenda-persistent-marks)))
8756 (org-agenda-bulk-action)
8757 (throw 'exit nil)))
8759 ((equal action ?$)
8760 (setq cmd '(org-agenda-archive)))
8762 ((equal action ?A)
8763 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8765 ((member action '(?r ?w))
8766 (setq rfloc (org-refile-get-location
8767 "Refile to"
8768 (marker-buffer (car org-agenda-bulk-marked-entries))
8769 org-refile-allow-creating-parent-nodes))
8770 (if (nth 3 rfloc)
8771 (setcar (nthcdr 3 rfloc)
8772 (move-marker (make-marker) (nth 3 rfloc)
8773 (or (get-file-buffer (nth 1 rfloc))
8774 (find-buffer-visiting (nth 1 rfloc))
8775 (error "This should not happen")))))
8777 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8778 redo-at-end t))
8780 ((equal action ?t)
8781 (setq state (org-icompleting-read
8782 "Todo state: "
8783 (with-current-buffer (marker-buffer (car entries))
8784 (mapcar 'list org-todo-keywords-1))))
8785 (setq cmd `(let ((org-inhibit-blocking t)
8786 (org-inhibit-logging 'note))
8787 (org-agenda-todo ,state))))
8789 ((memq action '(?- ?+))
8790 (setq tag (org-icompleting-read
8791 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8792 (with-current-buffer (marker-buffer (car entries))
8793 (delq nil
8794 (mapcar (lambda (x)
8795 (if (stringp (car x)) x)) org-tag-alist)))))
8796 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8798 ((memq action '(?s ?d))
8799 (let* ((date (unless arg
8800 (org-read-date
8801 nil nil nil
8802 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8803 (ans (if arg nil org-read-date-final-answer))
8804 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8805 (setq cmd `(let* ((bound (fboundp 'read-string))
8806 (old (and bound (symbol-function 'read-string))))
8807 (unwind-protect
8808 (progn
8809 (fset 'read-string (lambda (&rest ignore) ,ans))
8810 (eval '(,c1 arg)))
8811 (if bound
8812 (fset 'read-string old)
8813 (fmakunbound 'read-string)))))))
8815 ((equal action ?S)
8816 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8817 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8818 (let ((days (read-number
8819 (format "Scatter tasks across how many %sdays: "
8820 (if arg "week" "")) 7)))
8821 (setq cmd
8822 `(let ((distance (1+ (random ,days))))
8823 (if arg
8824 (let ((dist distance)
8825 (day-of-week
8826 (calendar-day-of-week
8827 (calendar-gregorian-from-absolute (org-today)))))
8828 (dotimes (i (1+ dist))
8829 (while (member day-of-week org-agenda-weekend-days)
8830 (incf distance)
8831 (incf day-of-week)
8832 (if (= day-of-week 7)
8833 (setq day-of-week 0)))
8834 (incf day-of-week)
8835 (if (= day-of-week 7)
8836 (setq day-of-week 0)))))
8837 ;; silently fail when try to replan a sexp entry
8838 (condition-case nil
8839 (let* ((date (calendar-gregorian-from-absolute
8840 (+ (org-today) distance)))
8841 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8842 (nth 2 date))))
8843 (org-agenda-schedule nil time))
8844 (error nil)))))))
8846 ((assoc action org-agenda-bulk-custom-functions)
8847 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8848 redo-at-end t))
8850 ((equal action ?f)
8851 (setq cmd (list (intern
8852 (org-icompleting-read "Function: "
8853 obarray 'fboundp t nil nil)))))
8855 (t (error "Invalid bulk action")))
8857 ;; Sort the markers, to make sure that parents are handled before children
8858 (setq entries (sort entries
8859 (lambda (a b)
8860 (cond
8861 ((equal (marker-buffer a) (marker-buffer b))
8862 (< (marker-position a) (marker-position b)))
8864 (string< (buffer-name (marker-buffer a))
8865 (buffer-name (marker-buffer b))))))))
8867 ;; Now loop over all markers and apply cmd
8868 (while (setq e (pop entries))
8869 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8870 (if (not pos)
8871 (progn (message "Skipping removed entry at %s" e)
8872 (setq cntskip (1+ cntskip)))
8873 (goto-char pos)
8874 (let (org-loop-over-headlines-in-active-region)
8875 (eval cmd))
8876 (when (not org-agenda-persistent-marks)
8877 (setq org-agenda-bulk-marked-entries
8878 (delete e org-agenda-bulk-marked-entries)))
8879 (setq cnt (1+ cnt))))
8880 (when (not org-agenda-persistent-marks)
8881 (org-agenda-bulk-unmark-all))
8882 (when redo-at-end (org-agenda-redo))
8883 (message "Acted on %d entries%s%s"
8885 (if (= cntskip 0)
8887 (format ", skipped %d (disappeared before their turn)"
8888 cntskip))
8889 (if (not org-agenda-persistent-marks)
8890 "" " (kept marked)"))))))
8892 ;;; Flagging notes
8894 (defun org-agenda-show-the-flagging-note ()
8895 "Display the flagging note in the other window.
8896 When called a second time in direct sequence, offer to remove the FLAGGING
8897 tag and (if present) the flagging note."
8898 (interactive)
8899 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8900 (win (selected-window))
8901 note heading newhead)
8902 (unless hdmarker
8903 (error "No linked entry at point"))
8904 (if (and (eq this-command last-command)
8905 (y-or-n-p "Unflag and remove any flagging note? "))
8906 (progn
8907 (org-agenda-remove-flag hdmarker)
8908 (let ((win (get-buffer-window "*Flagging Note*")))
8909 (and win (delete-window win)))
8910 (message "Entry unflagged"))
8911 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8912 (unless note
8913 (error "No flagging note"))
8914 (org-kill-new note)
8915 (org-switch-to-buffer-other-window "*Flagging Note*")
8916 (erase-buffer)
8917 (insert note)
8918 (goto-char (point-min))
8919 (while (re-search-forward "\\\\n" nil t)
8920 (replace-match "\n" t t))
8921 (goto-char (point-min))
8922 (select-window win)
8923 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8925 (defun org-agenda-remove-flag (marker)
8926 "Remove the FLAGGED tag and any flagging note in the entry."
8927 (let (newhead)
8928 (org-with-point-at marker
8929 (org-toggle-tag "FLAGGED" 'off)
8930 (org-entry-delete nil "THEFLAGGINGNOTE")
8931 (setq newhead (org-get-heading)))
8932 (org-agenda-change-all-lines newhead marker)
8933 (message "Entry unflagged")))
8935 (defun org-agenda-get-any-marker (&optional pos)
8936 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8937 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8939 ;;; Appointment reminders
8941 (defvar appt-time-msg-list)
8943 ;;;###autoload
8944 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8945 "Activate appointments found in `org-agenda-files'.
8946 With a \\[universal-argument] prefix, refresh the list of
8947 appointments.
8949 If FILTER is t, interactively prompt the user for a regular
8950 expression, and filter out entries that don't match it.
8952 If FILTER is a string, use this string as a regular expression
8953 for filtering entries out.
8955 If FILTER is a function, filter out entries against which
8956 calling the function returns nil. This function takes one
8957 argument: an entry from `org-agenda-get-day-entries'.
8959 FILTER can also be an alist with the car of each cell being
8960 either 'headline or 'category. For example:
8962 '((headline \"IMPORTANT\")
8963 (category \"Work\"))
8965 will only add headlines containing IMPORTANT or headlines
8966 belonging to the \"Work\" category.
8968 ARGS are symbols indicating what kind of entries to consider.
8969 By default `org-agenda-to-appt' will use :deadline, :scheduled
8970 and :timestamp entries. See the docstring of `org-diary' for
8971 details and examples."
8972 (interactive "P")
8973 (if refresh (setq appt-time-msg-list nil))
8974 (if (eq filter t)
8975 (setq filter (read-from-minibuffer "Regexp filter: ")))
8976 (let* ((cnt 0) ; count added events
8977 (scope (or args '(:deadline :scheduled :timestamp)))
8978 (org-agenda-new-buffers nil)
8979 (org-deadline-warning-days 0)
8980 ;; Do not use `org-today' here because appt only takes
8981 ;; time and without date as argument, so it may pass wrong
8982 ;; information otherwise
8983 (today (org-date-to-gregorian
8984 (time-to-days (current-time))))
8985 (org-agenda-restrict nil)
8986 (files (org-agenda-files 'unrestricted)) entries file
8987 (org-agenda-buffer nil))
8988 ;; Get all entries which may contain an appt
8989 (org-prepare-agenda-buffers files)
8990 (while (setq file (pop files))
8991 (setq entries
8992 (delq nil
8993 (append entries
8994 (apply 'org-agenda-get-day-entries
8995 file today scope)))))
8996 ;; Map thru entries and find if we should filter them out
8997 (mapc
8998 (lambda(x)
8999 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
9000 (cat (get-text-property 1 'org-category x))
9001 (tod (get-text-property 1 'time-of-day x))
9002 (ok (or (null filter)
9003 (and (stringp filter) (string-match filter evt))
9004 (and (functionp filter) (funcall filter x))
9005 (and (listp filter)
9006 (let ((cat-filter (cadr (assoc 'category filter)))
9007 (evt-filter (cadr (assoc 'headline filter))))
9008 (or (and (stringp cat-filter)
9009 (string-match cat-filter cat))
9010 (and (stringp evt-filter)
9011 (string-match evt-filter evt))))))))
9012 ;; FIXME: Shall we remove text-properties for the appt text?
9013 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
9014 (when (and ok tod)
9015 (setq tod (concat "00" (number-to-string tod))
9016 tod (when (string-match
9017 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
9018 (concat (match-string 1 tod) ":"
9019 (match-string 2 tod))))
9020 (appt-add tod evt)
9021 (setq cnt (1+ cnt))))) entries)
9022 (org-release-buffers org-agenda-new-buffers)
9023 (if (eq cnt 0)
9024 (message "No event to add")
9025 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
9027 (defun org-agenda-todayp (date)
9028 "Does DATE mean today, when considering `org-extend-today-until'?"
9029 (let ((today (org-today))
9030 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
9031 date)))
9032 (eq date today)))
9034 (defun org-agenda-todo-yesterday (&optional arg)
9035 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
9036 (interactive "P")
9037 (let* ((hour (third (decode-time
9038 (org-current-time))))
9039 (org-extend-today-until (1+ hour)))
9040 (org-agenda-todo arg)))
9042 (provide 'org-agenda)
9044 ;;; org-agenda.el ends here