org.el: Fix `org-diary' again by checking `org-agenda-buffer' correctly.
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-agenda.el
blob510a73ce7a0b6f3355ff2f63cfea625b2d7c473a
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (eval-when-compile
50 (require 'cl))
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-absolute-from-iso "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-datetree-find-date-create "org-datetree"
72 (date &optional keep-restriction))
73 (declare-function org-columns-quit "org-colview" ())
74 (declare-function diary-date-display-form "diary-lib" (&optional type))
75 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
76 (declare-function org-habit-insert-consistency-graphs
77 "org-habit" (&optional line))
78 (declare-function org-is-habit-p "org-habit" (&optional pom))
79 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
80 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
81 (declare-function org-pop-to-buffer-same-window "org-compat"
82 (&optional buffer-or-name norecord label))
84 (defvar calendar-mode-map)
85 (defvar org-clock-current-task) ; defined in org-clock.el
86 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
87 (defvar org-habit-show-habits)
88 (defvar org-habit-show-habits-only-for-today)
90 ;; Defined somewhere in this file, but used before definition.
91 (defvar org-agenda-buffer-name)
92 (defvar org-agenda-overriding-header)
93 (defvar org-agenda-title-append nil)
94 (defvar entry)
95 (defvar date)
96 (defvar org-agenda-undo-list)
97 (defvar org-agenda-pending-undo-list)
98 (defvar original-date) ; dynamically scoped, calendar.el does scope this
100 (defcustom org-agenda-confirm-kill 1
101 "When set, remote killing from the agenda buffer needs confirmation.
102 When t, a confirmation is always needed. When a number N, confirmation is
103 only needed when the text to be killed contains more than N non-white lines."
104 :group 'org-agenda
105 :type '(choice
106 (const :tag "Never" nil)
107 (const :tag "Always" t)
108 (integer :tag "When more than N lines")))
110 (defcustom org-agenda-compact-blocks nil
111 "Non-nil means make the block agenda more compact.
112 This is done globally by leaving out lines like the agenda span
113 name and week number or the separator lines."
114 :group 'org-agenda
115 :type 'boolean)
117 (defcustom org-agenda-block-separator ?=
118 "The separator between blocks in the agenda.
119 If this is a string, it will be used as the separator, with a newline added.
120 If it is a character, it will be repeated to fill the window width.
121 If nil the separator is disabled. In `org-agenda-custom-commands' this
122 addresses the separator between the current and the previous block."
123 :group 'org-agenda
124 :type '(choice
125 (const :tag "Disabled" nil)
126 (character)
127 (string)))
129 (defgroup org-agenda-export nil
130 "Options concerning exporting agenda views in Org-mode."
131 :tag "Org Agenda Export"
132 :group 'org-agenda)
134 (defcustom org-agenda-with-colors t
135 "Non-nil means use colors in agenda views."
136 :group 'org-agenda-export
137 :type 'boolean)
139 (defcustom org-agenda-exporter-settings nil
140 "Alist of variable/value pairs that should be active during agenda export.
141 This is a good place to set options for ps-print and for htmlize.
142 Note that the way this is implemented, the values will be evaluated
143 before assigned to the variables. So make sure to quote values you do
144 *not* want evaluated, for example
146 (setq org-agenda-exporter-settings
147 '((ps-print-color-p 'black-white)))"
148 :group 'org-agenda-export
149 :type '(repeat
150 (list
151 (variable)
152 (sexp :tag "Value"))))
154 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
155 "Hook run in temporary buffer before writing it to an export file.
156 A useful function is `org-agenda-add-entry-text'."
157 :group 'org-agenda-export
158 :type 'hook
159 :options '(org-agenda-add-entry-text))
161 (defcustom org-agenda-add-entry-text-maxlines 0
162 "Maximum number of entry text lines to be added to agenda.
163 This is only relevant when `org-agenda-add-entry-text' is part of
164 `org-agenda-before-write-hook', which it is by default.
165 When this is 0, nothing will happen. When it is greater than 0, it
166 specifies the maximum number of lines that will be added for each entry
167 that is listed in the agenda view.
169 Note that this variable is not used during display, only when exporting
170 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
171 and `org-agenda-entry-text-maxlines'."
172 :group 'org-agenda
173 :type 'integer)
175 (defcustom org-agenda-add-entry-text-descriptive-links t
176 "Non-nil means export org-links as descriptive links in agenda added text.
177 This variable applies to the text added to the agenda when
178 `org-agenda-add-entry-text-maxlines' is larger than 0.
179 When this variable nil, the URL will (also) be shown."
180 :group 'org-agenda
181 :type 'boolean)
183 (defcustom org-agenda-export-html-style ""
184 "The style specification for exported HTML Agenda files.
185 If this variable contains a string, it will replace the default <style>
186 section as produced by `htmlize'.
187 Since there are different ways of setting style information, this variable
188 needs to contain the full HTML structure to provide a style, including the
189 surrounding HTML tags. The style specifications should include definitions
190 the fonts used by the agenda, here is an example:
192 <style type=\"text/css\">
193 p { font-weight: normal; color: gray; }
194 .org-agenda-structure {
195 font-size: 110%;
196 color: #003399;
197 font-weight: 600;
199 .org-todo {
200 color: #cc6666;
201 font-weight: bold;
203 .org-agenda-done {
204 color: #339933;
206 .org-done {
207 color: #339933;
209 .title { text-align: center; }
210 .todo, .deadline { color: red; }
211 .done { color: green; }
212 </style>
214 or, if you want to keep the style in a file,
216 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
218 As the value of this option simply gets inserted into the HTML <head> header,
219 you can \"misuse\" it to also add other text to the header. However,
220 <style>...</style> is required, if not present the variable will be ignored."
221 :group 'org-agenda-export
222 :group 'org-export-html
223 :type 'string)
225 (defcustom org-agenda-persistent-filter nil
226 "When set, keep filters from one agenda view to the next."
227 :group 'org-agenda
228 :type 'boolean)
230 (defgroup org-agenda-custom-commands nil
231 "Options concerning agenda views in Org-mode."
232 :tag "Org Agenda Custom Commands"
233 :group 'org-agenda)
235 (defconst org-sorting-choice
236 '(choice
237 (const time-up) (const time-down)
238 (const category-keep) (const category-up) (const category-down)
239 (const tag-down) (const tag-up)
240 (const priority-up) (const priority-down)
241 (const todo-state-up) (const todo-state-down)
242 (const effort-up) (const effort-down)
243 (const habit-up) (const habit-down)
244 (const alpha-up) (const alpha-down)
245 (const user-defined-up) (const user-defined-down))
246 "Sorting choices.")
248 ;; Keep custom values for `org-agenda-filter-preset' compatible with
249 ;; the new variable `org-agenda-tag-filter-preset'.
250 (if (fboundp 'defvaralias)
251 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
252 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter))
254 (defconst org-agenda-custom-commands-local-options
255 `(repeat :tag "Local settings for this command. Remember to quote values"
256 (choice :tag "Setting"
257 (list :tag "Heading for this block"
258 (const org-agenda-overriding-header)
259 (string :tag "Headline"))
260 (list :tag "Files to be searched"
261 (const org-agenda-files)
262 (list
263 (const :format "" quote)
264 (repeat (file))))
265 (list :tag "Sorting strategy"
266 (const org-agenda-sorting-strategy)
267 (list
268 (const :format "" quote)
269 (repeat
270 ,org-sorting-choice)))
271 (list :tag "Prefix format"
272 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
273 (string))
274 (list :tag "Number of days in agenda"
275 (const org-agenda-span)
276 (choice (const :tag "Day" 'day)
277 (const :tag "Week" 'week)
278 (const :tag "Month" 'month)
279 (const :tag "Year" 'year)
280 (integer :tag "Custom")))
281 (list :tag "Fixed starting date"
282 (const org-agenda-start-day)
283 (string :value "2007-11-01"))
284 (list :tag "Start on day of week"
285 (const org-agenda-start-on-weekday)
286 (choice :value 1
287 (const :tag "Today" nil)
288 (integer :tag "Weekday No.")))
289 (list :tag "Include data from diary"
290 (const org-agenda-include-diary)
291 (boolean))
292 (list :tag "Deadline Warning days"
293 (const org-deadline-warning-days)
294 (integer :value 1))
295 (list :tag "Category filter preset"
296 (const org-agenda-category-filter-preset)
297 (list
298 (const :format "" quote)
299 (repeat
300 (string :tag "+category or -category"))))
301 (list :tag "Tags filter preset"
302 (const org-agenda-tag-filter-preset)
303 (list
304 (const :format "" quote)
305 (repeat
306 (string :tag "+tag or -tag"))))
307 (list :tag "Set daily/weekly entry types"
308 (const org-agenda-entry-types)
309 (list
310 (const :format "" quote)
311 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
312 (const :deadline)
313 (const :scheduled)
314 (const :timestamp)
315 (const :sexp))))
316 (list :tag "Standard skipping condition"
317 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
318 (const org-agenda-skip-function)
319 (list
320 (const :format "" quote)
321 (list
322 (choice
323 :tag "Skipping range"
324 (const :tag "Skip entry" org-agenda-skip-entry-if)
325 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
326 (repeat :inline t :tag "Conditions for skipping"
327 (choice
328 :tag "Condition type"
329 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
330 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
331 (list :tag "TODO state is" :inline t
332 (const 'todo)
333 (choice
334 (const :tag "any not-done state" 'todo)
335 (const :tag "any done state" 'done)
336 (const :tag "any state" 'any)
337 (list :tag "Keyword list"
338 (const :format "" quote)
339 (repeat (string :tag "Keyword")))))
340 (list :tag "TODO state is not" :inline t
341 (const 'nottodo)
342 (choice
343 (const :tag "any not-done state" 'todo)
344 (const :tag "any done state" 'done)
345 (const :tag "any state" 'any)
346 (list :tag "Keyword list"
347 (const :format "" quote)
348 (repeat (string :tag "Keyword")))))
349 (const :tag "scheduled" 'scheduled)
350 (const :tag "not scheduled" 'notscheduled)
351 (const :tag "deadline" 'deadline)
352 (const :tag "no deadline" 'notdeadline)
353 (const :tag "timestamp" 'timestamp)
354 (const :tag "no timestamp" 'nottimestamp))))))
355 (list :tag "Non-standard skipping condition"
356 :value (org-agenda-skip-function)
357 (const org-agenda-skip-function)
358 (sexp :tag "Function or form (quoted!)"))
359 (list :tag "Any variable"
360 (variable :tag "Variable")
361 (sexp :tag "Value (sexp)"))))
362 "Selection of examples for agenda command settings.
363 This will be spliced into the custom type of
364 `org-agenda-custom-commands'.")
367 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
368 ((agenda "") (alltodo))))
369 "Custom commands for the agenda.
370 These commands will be offered on the splash screen displayed by the
371 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
373 (key desc type match settings files)
375 key The key (one or more characters as a string) to be associated
376 with the command.
377 desc A description of the command, when omitted or nil, a default
378 description is built using MATCH.
379 type The command type, any of the following symbols:
380 agenda The daily/weekly agenda.
381 todo Entries with a specific TODO keyword, in all agenda files.
382 search Entries containing search words entry or headline.
383 tags Tags/Property/TODO match in all agenda files.
384 tags-todo Tags/P/T match in all agenda files, TODO entries only.
385 todo-tree Sparse tree of specific TODO keyword in *current* file.
386 tags-tree Sparse tree with all tags matches in *current* file.
387 occur-tree Occur sparse tree for *current* file.
388 ... A user-defined function.
389 match What to search for:
390 - a single keyword for TODO keyword searches
391 - a tags match expression for tags searches
392 - a word search expression for text searches.
393 - a regular expression for occur searches
394 For all other commands, this should be the empty string.
395 settings A list of option settings, similar to that in a let form, so like
396 this: ((opt1 val1) (opt2 val2) ...). The values will be
397 evaluated at the moment of execution, so quote them when needed.
398 files A list of files file to write the produced agenda buffer to
399 with the command `org-store-agenda-views'.
400 If a file name ends in \".html\", an HTML version of the buffer
401 is written out. If it ends in \".ps\", a postscript version is
402 produced. Otherwise, only the plain text is written to the file.
404 You can also define a set of commands, to create a composite agenda buffer.
405 In this case, an entry looks like this:
407 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
409 where
411 desc A description string to be displayed in the dispatcher menu.
412 cmd An agenda command, similar to the above. However, tree commands
413 are not allowed, but instead you can get agenda and global todo list.
414 So valid commands for a set are:
415 (agenda \"\" settings)
416 (alltodo \"\" settings)
417 (stuck \"\" settings)
418 (todo \"match\" settings files)
419 (search \"match\" settings files)
420 (tags \"match\" settings files)
421 (tags-todo \"match\" settings files)
423 Each command can carry a list of options, and another set of options can be
424 given for the whole set of commands. Individual command options take
425 precedence over the general options.
427 When using several characters as key to a command, the first characters
428 are prefix commands. For the dispatcher to display useful information, you
429 should provide a description for the prefix, like
431 (setq org-agenda-custom-commands
432 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
433 (\"hl\" tags \"+HOME+Lisa\")
434 (\"hp\" tags \"+HOME+Peter\")
435 (\"hk\" tags \"+HOME+Kim\")))"
436 :group 'org-agenda-custom-commands
437 :type `(repeat
438 (choice :value ("x" "Describe command here" tags "" nil)
439 (list :tag "Single command"
440 (string :tag "Access Key(s) ")
441 (option (string :tag "Description"))
442 (choice
443 (const :tag "Agenda" agenda)
444 (const :tag "TODO list" alltodo)
445 (const :tag "Search words" search)
446 (const :tag "Stuck projects" stuck)
447 (const :tag "Tags/Property match (all agenda files)" tags)
448 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
449 (const :tag "TODO keyword search (all agenda files)" todo)
450 (const :tag "Tags sparse tree (current buffer)" tags-tree)
451 (const :tag "TODO keyword tree (current buffer)" todo-tree)
452 (const :tag "Occur tree (current buffer)" occur-tree)
453 (sexp :tag "Other, user-defined function"))
454 (string :tag "Match (only for some commands)")
455 ,org-agenda-custom-commands-local-options
456 (option (repeat :tag "Export" (file :tag "Export to"))))
457 (list :tag "Command series, all agenda files"
458 (string :tag "Access Key(s)")
459 (string :tag "Description ")
460 (repeat :tag "Component"
461 (choice
462 (list :tag "Agenda"
463 (const :format "" agenda)
464 (const :tag "" :format "" "")
465 ,org-agenda-custom-commands-local-options)
466 (list :tag "TODO list (all keywords)"
467 (const :format "" alltodo)
468 (const :tag "" :format "" "")
469 ,org-agenda-custom-commands-local-options)
470 (list :tag "Search words"
471 (const :format "" search)
472 (string :tag "Match")
473 ,org-agenda-custom-commands-local-options)
474 (list :tag "Stuck projects"
475 (const :format "" stuck)
476 (const :tag "" :format "" "")
477 ,org-agenda-custom-commands-local-options)
478 (list :tag "Tags search"
479 (const :format "" tags)
480 (string :tag "Match")
481 ,org-agenda-custom-commands-local-options)
482 (list :tag "Tags search, TODO entries only"
483 (const :format "" tags-todo)
484 (string :tag "Match")
485 ,org-agenda-custom-commands-local-options)
486 (list :tag "TODO keyword search"
487 (const :format "" todo)
488 (string :tag "Match")
489 ,org-agenda-custom-commands-local-options)
490 (list :tag "Other, user-defined function"
491 (symbol :tag "function")
492 (string :tag "Match")
493 ,org-agenda-custom-commands-local-options)))
495 (repeat :tag "Settings for entire command set"
496 (list (variable :tag "Any variable")
497 (sexp :tag "Value")))
498 (option (repeat :tag "Export" (file :tag "Export to"))))
499 (cons :tag "Prefix key documentation"
500 (string :tag "Access Key(s)")
501 (string :tag "Description ")))))
503 (defcustom org-agenda-query-register ?o
504 "The register holding the current query string.
505 The purpose of this is that if you construct a query string interactively,
506 you can then use it to define a custom command."
507 :group 'org-agenda-custom-commands
508 :type 'character)
510 (defcustom org-stuck-projects
511 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
512 "How to identify stuck projects.
513 This is a list of four items:
514 1. A tags/todo/property matcher string that is used to identify a project.
515 See the manual for a description of tag and property searches.
516 The entire tree below a headline matched by this is considered one project.
517 2. A list of TODO keywords identifying non-stuck projects.
518 If the project subtree contains any headline with one of these todo
519 keywords, the project is considered to be not stuck. If you specify
520 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
521 3. A list of tags identifying non-stuck projects.
522 If the project subtree contains any headline with one of these tags,
523 the project is considered to be not stuck. If you specify \"*\" as
524 a tag, any tag will mark the project unstuck. Note that this is about
525 the explicit presence of a tag somewhere in the subtree, inherited
526 tags to not count here. If inherited tags make a project not stuck,
527 use \"-TAG\" in the tags part of the matcher under (1.) above.
528 4. An arbitrary regular expression matching non-stuck projects.
530 If the project turns out to be not stuck, search continues also in the
531 subtree to see if any of the subtasks have project status.
533 See also the variable `org-tags-match-list-sublevels' which applies
534 to projects matched by this search as well.
536 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
537 or `C-c a #' to produce the list."
538 :group 'org-agenda-custom-commands
539 :type '(list
540 (string :tag "Tags/TODO match to identify a project")
541 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
542 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
543 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
545 (defcustom org-agenda-filter-effort-default-operator "<"
546 "The default operator for effort estimate filtering.
547 If you select an effort estimate limit without first pressing an operator,
548 this one will be used."
549 :group 'org-agenda-custom-commands
550 :type '(choice (const :tag "less or equal" "<")
551 (const :tag "greater or equal"">")
552 (const :tag "equal" "=")))
554 (defgroup org-agenda-skip nil
555 "Options concerning skipping parts of agenda files."
556 :tag "Org Agenda Skip"
557 :group 'org-agenda)
559 (defcustom org-agenda-skip-function-global nil
560 "Function to be called at each match during agenda construction.
561 If this function returns nil, the current match should not be skipped.
562 If the function decided to skip an agenda match, is must return the
563 buffer position from which the search should be continued.
564 This may also be a Lisp form, which will be evaluated.
566 This variable will be applied to every agenda match, including
567 tags/property searches and TODO lists. So try to make the test function
568 do its checking as efficiently as possible. To implement a skipping
569 condition just for specific agenda commands, use the variable
570 `org-agenda-skip-function' which can be set in the options section
571 of custom agenda commands."
572 :group 'org-agenda-skip
573 :type 'sexp)
575 (defgroup org-agenda-daily/weekly nil
576 "Options concerning the daily/weekly agenda."
577 :tag "Org Agenda Daily/Weekly"
578 :group 'org-agenda)
579 (defgroup org-agenda-todo-list nil
580 "Options concerning the global todo list agenda view."
581 :tag "Org Agenda Todo List"
582 :group 'org-agenda)
583 (defgroup org-agenda-match-view nil
584 "Options concerning the general tags/property/todo match agenda view."
585 :tag "Org Agenda Match View"
586 :group 'org-agenda)
587 (defgroup org-agenda-search-view nil
588 "Options concerning the general tags/property/todo match agenda view."
589 :tag "Org Agenda Match View"
590 :group 'org-agenda)
592 (defvar org-agenda-archives-mode nil
593 "Non-nil means the agenda will include archived items.
594 If this is the symbol `trees', trees in the selected agenda scope
595 that are marked with the ARCHIVE tag will be included anyway. When this is
596 t, also all archive files associated with the current selection of agenda
597 files will be included.")
599 (defcustom org-agenda-skip-comment-trees t
600 "Non-nil means skip trees that start with the COMMENT keyword.
601 When nil, these trees are also scanned by agenda commands."
602 :group 'org-agenda-skip
603 :type 'boolean)
605 (defcustom org-agenda-todo-list-sublevels t
606 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
607 When nil, the sublevels of a TODO entry are not checked, resulting in
608 potentially much shorter TODO lists."
609 :group 'org-agenda-skip
610 :group 'org-agenda-todo-list
611 :type 'boolean)
613 (defcustom org-agenda-todo-ignore-with-date nil
614 "Non-nil means don't show entries with a date in the global todo list.
615 You can use this if you prefer to mark mere appointments with a TODO keyword,
616 but don't want them to show up in the TODO list.
617 When this is set, it also covers deadlines and scheduled items, the settings
618 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
619 will be ignored.
620 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
621 :group 'org-agenda-skip
622 :group 'org-agenda-todo-list
623 :type 'boolean)
625 (defcustom org-agenda-todo-ignore-timestamp nil
626 "Non-nil means don't show entries with a timestamp.
627 This applies when creating the global todo list.
628 Valid values are:
630 past Don't show entries for today or in the past.
632 future Don't show entries with a timestamp in the future.
633 The idea behind this is that if it has a future
634 timestamp, you don't want to think about it until the
635 date.
637 all Don't show any entries with a timestamp in the global todo list.
638 The idea behind this is that by setting a timestamp, you
639 have already \"taken care\" of this item.
641 This variable can also have an integer as a value. If positive (N),
642 todos with a timestamp N or more days in the future will be ignored. If
643 negative (-N), todos with a timestamp N or more days in the past will be
644 ignored. If 0, todos with a timestamp either today or in the future will
645 be ignored. For example, a value of -1 will exclude todos with a
646 timestamp in the past (yesterday or earlier), while a value of 7 will
647 exclude todos with a timestamp a week or more in the future.
649 See also `org-agenda-todo-ignore-with-date'.
650 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
651 to make his option also apply to the tags-todo list."
652 :group 'org-agenda-skip
653 :group 'org-agenda-todo-list
654 :version "24.1"
655 :type '(choice
656 (const :tag "Ignore future timestamp todos" future)
657 (const :tag "Ignore past or present timestamp todos" past)
658 (const :tag "Ignore all timestamp todos" all)
659 (const :tag "Show timestamp todos" nil)
660 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
662 (defcustom org-agenda-todo-ignore-scheduled nil
663 "Non-nil means, ignore some scheduled TODO items when making TODO list.
664 This applies when creating the global todo list.
665 Valid values are:
667 past Don't show entries scheduled today or in the past.
669 future Don't show entries scheduled in the future.
670 The idea behind this is that by scheduling it, you don't want to
671 think about it until the scheduled date.
673 all Don't show any scheduled entries in the global todo list.
674 The idea behind this is that by scheduling it, you have already
675 \"taken care\" of this item.
677 t Same as `all', for backward compatibility.
679 This variable can also have an integer as a value. See
680 `org-agenda-todo-ignore-timestamp' for more details.
682 See also `org-agenda-todo-ignore-with-date'.
683 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
684 to make his option also apply to the tags-todo list."
685 :group 'org-agenda-skip
686 :group 'org-agenda-todo-list
687 :type '(choice
688 (const :tag "Ignore future-scheduled todos" future)
689 (const :tag "Ignore past- or present-scheduled todos" past)
690 (const :tag "Ignore all scheduled todos" all)
691 (const :tag "Ignore all scheduled todos (compatibility)" t)
692 (const :tag "Show scheduled todos" nil)
693 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
695 (defcustom org-agenda-todo-ignore-deadlines nil
696 "Non-nil means ignore some deadlined TODO items when making TODO list.
697 There are different motivations for using different values, please think
698 carefully when configuring this variable.
700 This applies when creating the global todo list.
701 Valid values are:
703 near Don't show near deadline entries. A deadline is near when it is
704 closer than `org-deadline-warning-days' days. The idea behind this
705 is that such items will appear in the agenda anyway.
707 far Don't show TODO entries where a deadline has been defined, but
708 the deadline is not near. This is useful if you don't want to
709 use the todo list to figure out what to do now.
711 past Don't show entries with a deadline timestamp for today or in the past.
713 future Don't show entries with a deadline timestamp in the future, not even
714 when they become `near' ones. Use it with caution.
716 all Ignore all TODO entries that do have a deadline.
718 t Same as `near', for backward compatibility.
720 This variable can also have an integer as a value. See
721 `org-agenda-todo-ignore-timestamp' for more details.
723 See also `org-agenda-todo-ignore-with-date'.
724 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
725 to make his option also apply to the tags-todo list."
726 :group 'org-agenda-skip
727 :group 'org-agenda-todo-list
728 :type '(choice
729 (const :tag "Ignore near deadlines" near)
730 (const :tag "Ignore near deadlines (compatibility)" t)
731 (const :tag "Ignore far deadlines" far)
732 (const :tag "Ignore all TODOs with a deadlines" all)
733 (const :tag "Show all TODOs, even if they have a deadline" nil)
734 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
736 (defcustom org-agenda-tags-todo-honor-ignore-options nil
737 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
738 The variables
739 `org-agenda-todo-ignore-with-date',
740 `org-agenda-todo-ignore-timestamp',
741 `org-agenda-todo-ignore-scheduled',
742 `org-agenda-todo-ignore-deadlines'
743 make the global TODO list skip entries that have time stamps of certain
744 kinds. If this option is set, the same options will also apply for the
745 tags-todo search, which is the general tags/property matcher
746 restricted to unfinished TODO entries only."
747 :group 'org-agenda-skip
748 :group 'org-agenda-todo-list
749 :group 'org-agenda-match-view
750 :type 'boolean)
752 (defcustom org-agenda-skip-scheduled-if-done nil
753 "Non-nil means don't show scheduled items in agenda when they are done.
754 This is relevant for the daily/weekly agenda, not for the TODO list. And
755 it applies only to the actual date of the scheduling. Warnings about
756 an item with a past scheduling dates are always turned off when the item
757 is DONE."
758 :group 'org-agenda-skip
759 :group 'org-agenda-daily/weekly
760 :type 'boolean)
762 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
763 "Non-nil means skip scheduling line if same entry shows because of deadline.
764 In the agenda of today, an entry can show up multiple times because
765 it is both scheduled and has a nearby deadline, and maybe a plain time
766 stamp as well.
767 When this variable is t, then only the deadline is shown and the fact that
768 the entry is scheduled today or was scheduled previously is not shown.
769 When this variable is nil, the entry will be shown several times. When
770 the variable is the symbol `not-today', then skip scheduled previously,
771 but not scheduled today."
772 :group 'org-agenda-skip
773 :group 'org-agenda-daily/weekly
774 :type '(choice
775 (const :tag "Never" nil)
776 (const :tag "Always" t)
777 (const :tag "Not when scheduled today" not-today)))
779 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
780 "Non-nil means skip timestamp line if same entry shows because of deadline.
781 In the agenda of today, an entry can show up multiple times
782 because it has both a plain timestamp and has a nearby deadline.
783 When this variable is t, then only the deadline is shown and the
784 fact that the entry has a timestamp for or including today is not
785 shown. When this variable is nil, the entry will be shown
786 several times."
787 :group 'org-agenda-skip
788 :group 'org-agenda-daily/weekly
789 :version "24.1"
790 :type '(choice
791 (const :tag "Never" nil)
792 (const :tag "Always" t)))
794 (defcustom org-agenda-skip-deadline-if-done nil
795 "Non-nil means don't show deadlines when the corresponding item is done.
796 When nil, the deadline is still shown and should give you a happy feeling.
797 This is relevant for the daily/weekly agenda. And it applied only to the
798 actually date of the deadline. Warnings about approaching and past-due
799 deadlines are always turned off when the item is DONE."
800 :group 'org-agenda-skip
801 :group 'org-agenda-daily/weekly
802 :type 'boolean)
804 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
805 "Non-nil means skip deadline prewarning when entry is also scheduled.
806 This will apply on all days where a prewarning for the deadline would
807 be shown, but not at the day when the entry is actually due. On that day,
808 the deadline will be shown anyway.
809 This variable may be set to nil, t, or a number which will then give
810 the number of days before the actual deadline when the prewarnings
811 should resume.
812 This can be used in a workflow where the first showing of the deadline will
813 trigger you to schedule it, and then you don't want to be reminded of it
814 because you will take care of it on the day when scheduled."
815 :group 'org-agenda-skip
816 :group 'org-agenda-daily/weekly
817 :version "24.1"
818 :type '(choice
819 (const :tag "Alwas show prewarning" nil)
820 (const :tag "Remove prewarning if entry is scheduled" t)
821 (integer :tag "Restart prewarning N days before deadline")))
823 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
824 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
825 When non-nil, after the search for timestamps has matched once in an
826 entry, the rest of the entry will not be searched."
827 :group 'org-agenda-skip
828 :type 'boolean)
830 (defcustom org-agenda-skip-timestamp-if-done nil
831 "Non-nil means don't select item by timestamp or -range if it is DONE."
832 :group 'org-agenda-skip
833 :group 'org-agenda-daily/weekly
834 :type 'boolean)
836 (defcustom org-agenda-dim-blocked-tasks t
837 "Non-nil means dim blocked tasks in the agenda display.
838 This causes some overhead during agenda construction, but if you
839 have turned on `org-enforce-todo-dependencies',
840 `org-enforce-todo-checkbox-dependencies', or any other blocking
841 mechanism, this will create useful feedback in the agenda.
843 Instead of t, this variable can also have the value `invisible'.
844 Then blocked tasks will be invisible and only become visible when
845 they become unblocked. An exemption to this behavior is when a task is
846 blocked because of unchecked checkboxes below it. Since checkboxes do
847 not show up in the agenda views, making this task invisible you remove any
848 trace from agenda views that there is something to do. Therefore, a task
849 that is blocked because of checkboxes will never be made invisible, it
850 will only be dimmed."
851 :group 'org-agenda-daily/weekly
852 :group 'org-agenda-todo-list
853 :type '(choice
854 (const :tag "Do not dim" nil)
855 (const :tag "Dim to a gray face" t)
856 (const :tag "Make invisible" invisible)))
858 (defcustom org-timeline-show-empty-dates 3
859 "Non-nil means `org-timeline' also shows dates without an entry.
860 When nil, only the days which actually have entries are shown.
861 When t, all days between the first and the last date are shown.
862 When an integer, show also empty dates, but if there is a gap of more than
863 N days, just insert a special line indicating the size of the gap."
864 :group 'org-agenda-skip
865 :type '(choice
866 (const :tag "None" nil)
867 (const :tag "All" t)
868 (integer :tag "at most")))
870 (defgroup org-agenda-startup nil
871 "Options concerning initial settings in the Agenda in Org Mode."
872 :tag "Org Agenda Startup"
873 :group 'org-agenda)
875 (defcustom org-agenda-menu-show-matcher t
876 "Non-nil means show the match string in the agenda dispatcher menu.
877 When nil, the matcher string is not shown, but is put into the help-echo
878 property so than moving the mouse over the command shows it.
879 Setting it to nil is good if matcher strings are very long and/or if
880 you want to use two-column display (see `org-agenda-menu-two-column')."
881 :group 'org-agenda
882 :version "24.1"
883 :type 'boolean)
885 (defcustom org-agenda-menu-two-column nil
886 "Non-nil means, use two columns to show custom commands in the dispatcher.
887 If you use this, you probably want to set `org-agenda-menu-show-matcher'
888 to nil."
889 :group 'org-agenda
890 :version "24.1"
891 :type 'boolean)
893 (defcustom org-finalize-agenda-hook nil
894 "Hook run just before displaying an agenda buffer."
895 :group 'org-agenda-startup
896 :type 'hook)
898 (defcustom org-agenda-mouse-1-follows-link nil
899 "Non-nil means mouse-1 on a link will follow the link in the agenda.
900 A longer mouse click will still set point. Does not work on XEmacs.
901 Needs to be set before org.el is loaded."
902 :group 'org-agenda-startup
903 :type 'boolean)
905 (defcustom org-agenda-start-with-follow-mode nil
906 "The initial value of follow mode in a newly created agenda window."
907 :group 'org-agenda-startup
908 :type 'boolean)
910 (defcustom org-agenda-follow-indirect nil
911 "Non-nil means `org-agenda-follow-mode' displays only the
912 current item's tree, in an indirect buffer."
913 :group 'org-agenda
914 :version "24.1"
915 :type 'boolean)
917 (defcustom org-agenda-show-outline-path t
918 "Non-nil means show outline path in echo area after line motion."
919 :group 'org-agenda-startup
920 :type 'boolean)
922 (defcustom org-agenda-start-with-entry-text-mode nil
923 "The initial value of entry-text-mode in a newly created agenda window."
924 :group 'org-agenda-startup
925 :type 'boolean)
927 (defcustom org-agenda-entry-text-maxlines 5
928 "Number of text lines to be added when `E' is pressed in the agenda.
930 Note that this variable only used during agenda display. Add add entry text
931 when exporting the agenda, configure the variable
932 `org-agenda-add-entry-ext-maxlines'."
933 :group 'org-agenda
934 :type 'integer)
936 (defcustom org-agenda-entry-text-exclude-regexps nil
937 "List of regular expressions to clean up entry text.
938 The complete matches of all regular expressions in this list will be
939 removed from entry text before it is shown in the agenda."
940 :group 'org-agenda
941 :type '(repeat (regexp)))
943 (defvar org-agenda-entry-text-cleanup-hook nil
944 "Hook that is run after basic cleanup of entry text to be shown in agenda.
945 This cleanup is done in a temporary buffer, so the function may inspect and
946 change the entire buffer.
947 Some default stuff like drawers and scheduling/deadline dates will already
948 have been removed when this is called, as will any matches for regular
949 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
951 (defvar org-agenda-include-inactive-timestamps nil
952 "Non-nil means include inactive time stamps in agenda and timeline.
953 Dynamically scoped.")
955 (defgroup org-agenda-windows nil
956 "Options concerning the windows used by the Agenda in Org Mode."
957 :tag "Org Agenda Windows"
958 :group 'org-agenda)
960 (defcustom org-agenda-window-setup 'reorganize-frame
961 "How the agenda buffer should be displayed.
962 Possible values for this option are:
964 current-window Show agenda in the current window, keeping all other windows.
965 other-window Use `switch-to-buffer-other-window' to display agenda.
966 reorganize-frame Show only two windows on the current frame, the current
967 window and the agenda.
968 other-frame Use `switch-to-buffer-other-frame' to display agenda.
969 Also, when exiting the agenda, kill that frame.
970 See also the variable `org-agenda-restore-windows-after-quit'."
971 :group 'org-agenda-windows
972 :type '(choice
973 (const current-window)
974 (const other-frame)
975 (const other-window)
976 (const reorganize-frame)))
978 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
979 "The min and max height of the agenda window as a fraction of frame height.
980 The value of the variable is a cons cell with two numbers between 0 and 1.
981 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
982 :group 'org-agenda-windows
983 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
985 (defcustom org-agenda-restore-windows-after-quit nil
986 "Non-nil means restore window configuration upon exiting agenda.
987 Before the window configuration is changed for displaying the agenda,
988 the current status is recorded. When the agenda is exited with
989 `q' or `x' and this option is set, the old state is restored. If
990 `org-agenda-window-setup' is `other-frame', the value of this
991 option will be ignored."
992 :group 'org-agenda-windows
993 :type 'boolean)
995 (defcustom org-agenda-ndays nil
996 "Number of days to include in overview display.
997 Should be 1 or 7.
998 Obsolete, see `org-agenda-span'."
999 :group 'org-agenda-daily/weekly
1000 :type 'integer)
1002 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1004 (defcustom org-agenda-span 'week
1005 "Number of days to include in overview display.
1006 Can be day, week, month, year, or any number of days.
1007 Custom commands can set this variable in the options section."
1008 :group 'org-agenda-daily/weekly
1009 :type '(choice (const :tag "Day" day)
1010 (const :tag "Week" week)
1011 (const :tag "Month" month)
1012 (const :tag "Year" year)
1013 (integer :tag "Custom")))
1015 (defcustom org-agenda-start-on-weekday 1
1016 "Non-nil means start the overview always on the specified weekday.
1017 0 denotes Sunday, 1 denotes Monday etc.
1018 When nil, always start on the current day.
1019 Custom commands can set this variable in the options section."
1020 :group 'org-agenda-daily/weekly
1021 :type '(choice (const :tag "Today" nil)
1022 (integer :tag "Weekday No.")))
1024 (defcustom org-agenda-show-all-dates t
1025 "Non-nil means `org-agenda' shows every day in the selected range.
1026 When nil, only the days which actually have entries are shown."
1027 :group 'org-agenda-daily/weekly
1028 :type 'boolean)
1030 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1031 "Format string for displaying dates in the agenda.
1032 Used by the daily/weekly agenda and by the timeline. This should be
1033 a format string understood by `format-time-string', or a function returning
1034 the formatted date as a string. The function must take a single argument,
1035 a calendar-style date list like (month day year)."
1036 :group 'org-agenda-daily/weekly
1037 :type '(choice
1038 (string :tag "Format string")
1039 (function :tag "Function")))
1041 (defun org-agenda-format-date-aligned (date)
1042 "Format a date string for display in the daily/weekly agenda, or timeline.
1043 This function makes sure that dates are aligned for easy reading."
1044 (require 'cal-iso)
1045 (let* ((dayname (calendar-day-name date))
1046 (day (cadr date))
1047 (day-of-week (calendar-day-of-week date))
1048 (month (car date))
1049 (monthname (calendar-month-name month))
1050 (year (nth 2 date))
1051 (iso-week (org-days-to-iso-week
1052 (calendar-absolute-from-gregorian date)))
1053 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1054 (1- year))
1055 ((and (= month 12) (<= iso-week 1))
1056 (1+ year))
1057 (t year)))
1058 (weekstring (if (= day-of-week 1)
1059 (format " W%02d" iso-week)
1060 "")))
1061 (format "%-10s %2d %s %4d%s"
1062 dayname day monthname year weekstring)))
1064 (defcustom org-agenda-time-leading-zero nil
1065 "Non-nil means use leading zero for military times in agenda.
1066 For example, 9:30am would become 09:30 rather than 9:30."
1067 :group 'org-agenda-daily/weekly
1068 :version "24.1"
1069 :type 'boolean)
1071 (defcustom org-agenda-timegrid-use-ampm nil
1072 "When set, show AM/PM style timestamps on the timegrid."
1073 :group 'org-agenda
1074 :version "24.1"
1075 :type 'boolean)
1077 (defun org-agenda-time-of-day-to-ampm (time)
1078 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1079 (let* ((hour-number (string-to-number (substring time 0 -3)))
1080 (minute (substring time -2))
1081 (ampm "am"))
1082 (cond
1083 ((equal hour-number 12)
1084 (setq ampm "pm"))
1085 ((> hour-number 12)
1086 (setq ampm "pm")
1087 (setq hour-number (- hour-number 12))))
1088 (concat
1089 (if org-agenda-time-leading-zero
1090 (format "%02d" hour-number)
1091 (format "%02s" (number-to-string hour-number)))
1092 ":" minute ampm)))
1094 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1095 "Conditionally convert TIME to AM/PM format
1096 based on `org-agenda-timegrid-use-ampm'"
1097 (if org-agenda-timegrid-use-ampm
1098 (org-agenda-time-of-day-to-ampm time)
1099 time))
1101 (defcustom org-agenda-weekend-days '(6 0)
1102 "Which days are weekend?
1103 These days get the special face `org-agenda-date-weekend' in the agenda
1104 and timeline buffers."
1105 :group 'org-agenda-daily/weekly
1106 :type '(set :greedy t
1107 (const :tag "Monday" 1)
1108 (const :tag "Tuesday" 2)
1109 (const :tag "Wednesday" 3)
1110 (const :tag "Thursday" 4)
1111 (const :tag "Friday" 5)
1112 (const :tag "Saturday" 6)
1113 (const :tag "Sunday" 0)))
1115 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1116 "Non-nil means jump to today when moving a past date forward in time.
1117 When using S-right in the agenda to move a a date forward, and the date
1118 stamp currently points to the past, the first key press will move it
1119 to today. WHen nil, just move one day forward even if the date stays
1120 in the past."
1121 :group 'org-agenda-daily/weekly
1122 :version "24.1"
1123 :type 'boolean)
1125 (defcustom org-agenda-include-diary nil
1126 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1127 Custom commands can set this variable in the options section."
1128 :group 'org-agenda-daily/weekly
1129 :type 'boolean)
1131 (defcustom org-agenda-include-deadlines t
1132 "If non-nil, include entries within their deadline warning period.
1133 Custom commands can set this variable in the options section."
1134 :group 'org-agenda-daily/weekly
1135 :version "24.1"
1136 :type 'boolean)
1138 (defcustom org-agenda-repeating-timestamp-show-all t
1139 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1140 When set to a list of strings, only show occurrences of repeating
1141 stamps for these TODO keywords. When nil, only one occurrence is
1142 shown, either today or the nearest into the future."
1143 :group 'org-agenda-daily/weekly
1144 :type '(choice
1145 (const :tag "Show repeating stamps" t)
1146 (repeat :tag "Show repeating stamps for these TODO keywords"
1147 (string :tag "TODO Keyword"))
1148 (const :tag "Don't show repeating stamps" nil)))
1150 (defcustom org-scheduled-past-days 10000
1151 "No. of days to continue listing scheduled items that are not marked DONE.
1152 When an item is scheduled on a date, it shows up in the agenda on this
1153 day and will be listed until it is marked done for the number of days
1154 given here."
1155 :group 'org-agenda-daily/weekly
1156 :type 'integer)
1158 (defcustom org-agenda-log-mode-items '(closed clock)
1159 "List of items that should be shown in agenda log mode.
1160 This list may contain the following symbols:
1162 closed Show entries that have been closed on that day.
1163 clock Show entries that have received clocked time on that day.
1164 state Show all logged state changes.
1165 Note that instead of changing this variable, you can also press `C-u l' in
1166 the agenda to display all available LOG items temporarily."
1167 :group 'org-agenda-daily/weekly
1168 :type '(set :greedy t (const closed) (const clock) (const state)))
1170 (defcustom org-agenda-clock-consistency-checks
1171 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1172 :gap-ok-around ("4:00")
1173 :default-face ((:background "DarkRed") (:foreground "white"))
1174 :overlap-face nil :gap-face nil :no-end-time-face nil
1175 :long-face nil :short-face nil)
1176 "This is a property list, with the following keys:
1178 :max-duration Mark clocking chunks that are longer than this time.
1179 This is a time string like \"HH:MM\", or the number
1180 of minutes as an integer.
1182 :min-duration Mark clocking chunks that are shorter that this.
1183 This is a time string like \"HH:MM\", or the number
1184 of minutes as an integer.
1186 :max-gap Mark gaps between clocking chunks that are longer than
1187 this duration. A number of minutes, or a string
1188 like \"HH:MM\".
1190 :gap-ok-around List of times during the day which are usually not working
1191 times. When a gap is detected, but the gap contains any
1192 of these times, the gap is *not* reported. For example,
1193 if this is (\"4:00\" \"13:00\") then gaps that contain
1194 4:00 in the morning (i.e. the night) and 13:00
1195 (i.e. a typical lunch time) do not cause a warning.
1196 You should have at least one time during the night in this
1197 list, or otherwise the first task each morning will trigger
1198 a warning because it follows a long gap.
1200 Furthermore, the following properties can be used to define faces for
1201 issue display.
1203 :default-face the default face, if the specific face is undefined
1204 :overlap-face face for overlapping clocks
1205 :gap-face face for gaps between clocks
1206 :no-end-time-face face for incomplete clocks
1207 :long-face face for clock intervals that are too long
1208 :short-face face for clock intervals that are too short"
1209 :group 'org-agenda-daily/weekly
1210 :group 'org-clock
1211 :version "24.1"
1212 :type 'plist)
1214 (defcustom org-agenda-log-mode-add-notes t
1215 "Non-nil means add first line of notes to log entries in agenda views.
1216 If a log item like a state change or a clock entry is associated with
1217 notes, the first line of these notes will be added to the entry in the
1218 agenda display."
1219 :group 'org-agenda-daily/weekly
1220 :type 'boolean)
1222 (defcustom org-agenda-start-with-log-mode nil
1223 "The initial value of log-mode in a newly created agenda window."
1224 :group 'org-agenda-startup
1225 :group 'org-agenda-daily/weekly
1226 :type 'boolean)
1228 (defcustom org-agenda-start-with-clockreport-mode nil
1229 "The initial value of clockreport-mode in a newly created agenda window."
1230 :group 'org-agenda-startup
1231 :group 'org-agenda-daily/weekly
1232 :type 'boolean)
1234 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1235 "Property list with parameters for the clocktable in clockreport mode.
1236 This is the display mode that shows a clock table in the daily/weekly
1237 agenda, the properties for this dynamic block can be set here.
1238 The usual clocktable parameters are allowed here, but you cannot set
1239 the properties :name, :tstart, :tend, :block, and :scope - these will
1240 be overwritten to make sure the content accurately reflects the
1241 current display in the agenda."
1242 :group 'org-agenda-daily/weekly
1243 :type 'plist)
1245 (defcustom org-agenda-search-view-always-boolean nil
1246 "Non-nil means the search string is interpreted as individual parts.
1248 The search string for search view can either be interpreted as a phrase,
1249 or as a list of snippets that define a boolean search for a number of
1250 strings.
1252 When this is non-nil, the string will be split on whitespace, and each
1253 snippet will be searched individually, and all must match in order to
1254 select an entry. A snippet is then a single string of non-white
1255 characters, or a string in double quotes, or a regexp in {} braces.
1256 If a snippet is preceded by \"-\", the snippet must *not* match.
1257 \"+\" is syntactic sugar for positive selection. Each snippet may
1258 be found as a full word or a partial word, but see the variable
1259 `org-agenda-search-view-force-full-words'.
1261 When this is nil, search will look for the entire search phrase as one,
1262 with each space character matching any amount of whitespace, including
1263 line breaks.
1265 Even when this is nil, you can still switch to Boolean search dynamically
1266 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1267 is a regexp marked with braces like \"{abc}\", this will also switch to
1268 boolean search."
1269 :group 'org-agenda-search-view
1270 :version "24.1"
1271 :type 'boolean)
1273 (if (fboundp 'defvaralias)
1274 (defvaralias 'org-agenda-search-view-search-words-only
1275 'org-agenda-search-view-always-boolean))
1277 (defcustom org-agenda-search-view-force-full-words nil
1278 "Non-nil means, search words must be matches as complete words.
1279 When nil, they may also match part of a word."
1280 :group 'org-agenda-search-view
1281 :version "24.1"
1282 :type 'boolean)
1284 (defgroup org-agenda-time-grid nil
1285 "Options concerning the time grid in the Org-mode Agenda."
1286 :tag "Org Agenda Time Grid"
1287 :group 'org-agenda)
1289 (defcustom org-agenda-search-headline-for-time t
1290 "Non-nil means search headline for a time-of-day.
1291 If the headline contains a time-of-day in one format or another, it will
1292 be used to sort the entry into the time sequence of items for a day.
1293 Some people have time stamps in the headline that refer to the creation
1294 time or so, and then this produces an unwanted side effect. If this is
1295 the case for your, use this variable to turn off searching the headline
1296 for a time."
1297 :group 'org-agenda-time-grid
1298 :type 'boolean)
1300 (defcustom org-agenda-use-time-grid t
1301 "Non-nil means show a time grid in the agenda schedule.
1302 A time grid is a set of lines for specific times (like every two hours between
1303 8:00 and 20:00). The items scheduled for a day at specific times are
1304 sorted in between these lines.
1305 For details about when the grid will be shown, and what it will look like, see
1306 the variable `org-agenda-time-grid'."
1307 :group 'org-agenda-time-grid
1308 :type 'boolean)
1310 (defcustom org-agenda-time-grid
1311 '((daily today require-timed)
1312 "----------------"
1313 (800 1000 1200 1400 1600 1800 2000))
1315 "The settings for time grid for agenda display.
1316 This is a list of three items. The first item is again a list. It contains
1317 symbols specifying conditions when the grid should be displayed:
1319 daily if the agenda shows a single day
1320 weekly if the agenda shows an entire week
1321 today show grid on current date, independent of daily/weekly display
1322 require-timed show grid only if at least one item has a time specification
1324 The second item is a string which will be placed behind the grid time.
1326 The third item is a list of integers, indicating the times that should have
1327 a grid line."
1328 :group 'org-agenda-time-grid
1329 :type
1330 '(list
1331 (set :greedy t :tag "Grid Display Options"
1332 (const :tag "Show grid in single day agenda display" daily)
1333 (const :tag "Show grid in weekly agenda display" weekly)
1334 (const :tag "Always show grid for today" today)
1335 (const :tag "Show grid only if any timed entries are present"
1336 require-timed)
1337 (const :tag "Skip grid times already present in an entry"
1338 remove-match))
1339 (string :tag "Grid String")
1340 (repeat :tag "Grid Times" (integer :tag "Time"))))
1342 (defcustom org-agenda-show-current-time-in-grid t
1343 "Non-nil means show the current time in the time grid."
1344 :group 'org-agenda-time-grid
1345 :version "24.1"
1346 :type 'boolean)
1348 (defcustom org-agenda-current-time-string
1349 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1350 "The string for the current time marker in the agenda."
1351 :group 'org-agenda-time-grid
1352 :version "24.1"
1353 :type 'string)
1355 (defgroup org-agenda-sorting nil
1356 "Options concerning sorting in the Org-mode Agenda."
1357 :tag "Org Agenda Sorting"
1358 :group 'org-agenda)
1360 (defcustom org-agenda-sorting-strategy
1361 '((agenda habit-down time-up priority-down category-keep)
1362 (todo priority-down category-keep)
1363 (tags priority-down category-keep)
1364 (search category-keep))
1365 "Sorting structure for the agenda items of a single day.
1366 This is a list of symbols which will be used in sequence to determine
1367 if an entry should be listed before another entry. The following
1368 symbols are recognized:
1370 time-up Put entries with time-of-day indications first, early first
1371 time-down Put entries with time-of-day indications first, late first
1372 category-keep Keep the default order of categories, corresponding to the
1373 sequence in `org-agenda-files'.
1374 category-up Sort alphabetically by category, A-Z.
1375 category-down Sort alphabetically by category, Z-A.
1376 tag-up Sort alphabetically by last tag, A-Z.
1377 tag-down Sort alphabetically by last tag, Z-A.
1378 priority-up Sort numerically by priority, high priority last.
1379 priority-down Sort numerically by priority, high priority first.
1380 todo-state-up Sort by todo state, tasks that are done last.
1381 todo-state-down Sort by todo state, tasks that are done first.
1382 effort-up Sort numerically by estimated effort, high effort last.
1383 effort-down Sort numerically by estimated effort, high effort first.
1384 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1385 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1386 habit-up Put entries that are habits first
1387 habit-down Put entries that are habits last
1388 alpha-up Sort headlines alphabetically
1389 alpha-down Sort headlines alphabetically, reversed
1391 The different possibilities will be tried in sequence, and testing stops
1392 if one comparison returns a \"not-equal\". For example, the default
1393 '(time-up category-keep priority-down)
1394 means: Pull out all entries having a specified time of day and sort them,
1395 in order to make a time schedule for the current day the first thing in the
1396 agenda listing for the day. Of the entries without a time indication, keep
1397 the grouped in categories, don't sort the categories, but keep them in
1398 the sequence given in `org-agenda-files'. Within each category sort by
1399 priority.
1401 Leaving out `category-keep' would mean that items will be sorted across
1402 categories by priority.
1404 Instead of a single list, this can also be a set of list for specific
1405 contents, with a context symbol in the car of the list, any of
1406 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1408 Custom commands can bind this variable in the options section."
1409 :group 'org-agenda-sorting
1410 :type `(choice
1411 (repeat :tag "General" ,org-sorting-choice)
1412 (list :tag "Individually"
1413 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1414 (repeat ,org-sorting-choice))
1415 (cons (const :tag "Strategy for TODO lists" todo)
1416 (repeat ,org-sorting-choice))
1417 (cons (const :tag "Strategy for Tags matches" tags)
1418 (repeat ,org-sorting-choice))
1419 (cons (const :tag "Strategy for search matches" search)
1420 (repeat ,org-sorting-choice)))))
1422 (defcustom org-agenda-cmp-user-defined nil
1423 "A function to define the comparison `user-defined'.
1424 This function must receive two arguments, agenda entry a and b.
1425 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1426 the user comparison, return nil.
1427 When this is defined, you can make `user-defined-up' and `user-defined-down'
1428 part of an agenda sorting strategy."
1429 :group 'org-agenda-sorting
1430 :type 'symbol)
1432 (defcustom org-sort-agenda-notime-is-late t
1433 "Non-nil means items without time are considered late.
1434 This is only relevant for sorting. When t, items which have no explicit
1435 time like 15:30 will be considered as 99:01, i.e. later than any items which
1436 do have a time. When nil, the default time is before 0:00. You can use this
1437 option to decide if the schedule for today should come before or after timeless
1438 agenda entries."
1439 :group 'org-agenda-sorting
1440 :type 'boolean)
1442 (defcustom org-sort-agenda-noeffort-is-high t
1443 "Non-nil means items without effort estimate are sorted as high effort.
1444 This also applies when filtering an agenda view with respect to the
1445 < or > effort operator. Then, tasks with no effort defined will be treated
1446 as tasks with high effort.
1447 When nil, such items are sorted as 0 minutes effort."
1448 :group 'org-agenda-sorting
1449 :type 'boolean)
1451 (defgroup org-agenda-line-format nil
1452 "Options concerning the entry prefix in the Org-mode agenda display."
1453 :tag "Org Agenda Line Format"
1454 :group 'org-agenda)
1456 (defcustom org-agenda-prefix-format
1457 '((agenda . " %i %-12:c%?-12t% s")
1458 (timeline . " % s")
1459 (todo . " %i %-12:c")
1460 (tags . " %i %-12:c")
1461 (search . " %i %-12:c"))
1462 "Format specifications for the prefix of items in the agenda views.
1463 An alist with five entries, each for the different agenda types. The
1464 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1465 The values are format strings.
1467 This format works similar to a printf format, with the following meaning:
1469 %c the category of the item, \"Diary\" for entries from the diary,
1470 or as given by the CATEGORY keyword or derived from the file name
1471 %e the effort required by the item
1472 %i the icon category of the item, see `org-agenda-category-icon-alist'
1473 %T the last tag of the item (ignore inherited tags, which come first)
1474 %t the HH:MM time-of-day specification if one applies to the entry
1475 %s Scheduling/Deadline information, a short string
1476 %(expression) Eval EXPRESSION and replace the control string
1477 by the result
1479 All specifiers work basically like the standard `%s' of printf, but may
1480 contain two additional characters: a question mark just after the `%'
1481 and a whitespace/punctuation character just before the final letter.
1483 If the first character after `%' is a question mark, the entire field
1484 will only be included if the corresponding value applies to the current
1485 entry. This is useful for fields which should have fixed width when
1486 present, but zero width when absent. For example, \"%?-12t\" will
1487 result in a 12 character time field if a time of the day is specified,
1488 but will completely disappear in entries which do not contain a time.
1490 If there is punctuation or whitespace character just before the final
1491 format letter, this character will be appended to the field value if
1492 the value is not empty. For example, the format \"%-12:c\" leads to
1493 \"Diary: \" if the category is \"Diary\". If the category were be
1494 empty, no additional colon would be inserted.
1496 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1497 which means:
1499 - Indent the line with two space characters
1500 - Give the category a 12 chars wide field, padded with whitespace on
1501 the right (because of `-'). Append a colon if there is a category
1502 (because of `:').
1503 - If there is a time-of-day, put it into a 12 chars wide field. If no
1504 time, don't put in an empty field, just skip it (because of '?').
1505 - Finally, put the scheduling information.
1507 See also the variables `org-agenda-remove-times-when-in-prefix' and
1508 `org-agenda-remove-tags'.
1510 Custom commands can set this variable in the options section."
1511 :type '(choice
1512 (string :tag "General format")
1513 (list :greedy t :tag "View dependent"
1514 (cons (const agenda) (string :tag "Format"))
1515 (cons (const timeline) (string :tag "Format"))
1516 (cons (const todo) (string :tag "Format"))
1517 (cons (const tags) (string :tag "Format"))
1518 (cons (const search) (string :tag "Format"))))
1519 :group 'org-agenda-line-format)
1521 (defvar org-prefix-format-compiled nil
1522 "The compiled prefix format and associated variables.
1523 This is a list where first element is a list of variable bindings, and second
1524 element is the compiled format expression. See the variable
1525 `org-agenda-prefix-format'.")
1527 (defcustom org-agenda-todo-keyword-format "%-1s"
1528 "Format for the TODO keyword in agenda lines.
1529 Set this to something like \"%-12s\" if you want all TODO keywords
1530 to occupy a fixed space in the agenda display."
1531 :group 'org-agenda-line-format
1532 :type 'string)
1534 (defcustom org-agenda-diary-sexp-prefix nil
1535 "A regexp that matches part of a diary sexp entry
1536 which should be treated as scheduling/deadline information in
1537 `org-agenda'.
1539 For example, you can use this to extract the `diary-remind-message' from
1540 `diary-remind' entries."
1541 :group 'org-agenda-line-format
1542 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1544 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1545 "Text preceding timerange entries in the agenda view.
1546 This is a list with two strings. The first applies when the range
1547 is entirely on one day. The second applies if the range spans several days.
1548 The strings may have two \"%d\" format specifiers which will be filled
1549 with the sequence number of the days, and the total number of days in the
1550 range, respectively."
1551 :group 'org-agenda-line-format
1552 :type '(list
1553 (string :tag "Deadline today ")
1554 (choice :tag "Deadline relative"
1555 (string :tag "Format string")
1556 (function))))
1558 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1559 "Text preceding scheduled items in the agenda view.
1560 This is a list with two strings. The first applies when the item is
1561 scheduled on the current day. The second applies when it has been scheduled
1562 previously, it may contain a %d indicating that this is the nth time that
1563 this item is scheduled, due to automatic rescheduling of unfinished items
1564 for the following day. So this number is one larger than the number of days
1565 that passed since this item was scheduled first."
1566 :group 'org-agenda-line-format
1567 :type '(list
1568 (string :tag "Scheduled today ")
1569 (string :tag "Scheduled previously")))
1571 (defcustom org-agenda-inactive-leader "["
1572 "Text preceding item pulled into the agenda by inactive time stamps.
1573 These entries are added to the agenda when pressing \"[\"."
1574 :group 'org-agenda-line-format
1575 :version "24.1"
1576 :type '(list
1577 (string :tag "Scheduled today ")
1578 (string :tag "Scheduled previously")))
1580 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1581 "Text preceding deadline items in the agenda view.
1582 This is a list with two strings. The first applies when the item has its
1583 deadline on the current day. The second applies when it is in the past or
1584 in the future, it may contain %d to capture how many days away the deadline
1585 is (was)."
1586 :group 'org-agenda-line-format
1587 :type '(list
1588 (string :tag "Deadline today ")
1589 (choice :tag "Deadline relative"
1590 (string :tag "Format string")
1591 (function))))
1593 (defcustom org-agenda-remove-times-when-in-prefix t
1594 "Non-nil means remove duplicate time specifications in agenda items.
1595 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1596 time-of-day specification in a headline or diary entry is extracted and
1597 placed into the prefix. If this option is non-nil, the original specification
1598 \(a timestamp or -range, or just a plain time(range) specification like
1599 11:30-4pm) will be removed for agenda display. This makes the agenda less
1600 cluttered.
1601 The option can be t or nil. It may also be the symbol `beg', indicating
1602 that the time should only be removed when it is located at the beginning of
1603 the headline/diary entry."
1604 :group 'org-agenda-line-format
1605 :type '(choice
1606 (const :tag "Always" t)
1607 (const :tag "Never" nil)
1608 (const :tag "When at beginning of entry" beg)))
1610 (defcustom org-agenda-remove-timeranges-from-blocks nil
1611 "Non-nil means remove time ranges specifications in agenda
1612 items that span on several days."
1613 :group 'org-agenda-line-format
1614 :version "24.1"
1615 :type 'boolean)
1617 (defcustom org-agenda-default-appointment-duration nil
1618 "Default duration for appointments that only have a starting time.
1619 When nil, no duration is specified in such cases.
1620 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1621 :group 'org-agenda-line-format
1622 :type '(choice
1623 (integer :tag "Minutes")
1624 (const :tag "No default duration")))
1626 (defcustom org-agenda-show-inherited-tags t
1627 "Non-nil means show inherited tags in each agenda line."
1628 :group 'org-agenda-line-format
1629 :type 'boolean)
1631 (defcustom org-agenda-hide-tags-regexp nil
1632 "Regular expression used to filter away specific tags in agenda views.
1633 This means that these tags will be present, but not be shown in the agenda
1634 line. Secondary filtering will still work on the hidden tags.
1635 Nil means don't hide any tags."
1636 :group 'org-agenda-line-format
1637 :type '(choice
1638 (const :tag "Hide none" nil)
1639 (string :tag "Regexp ")))
1641 (defcustom org-agenda-remove-tags nil
1642 "Non-nil means remove the tags from the headline copy in the agenda.
1643 When this is the symbol `prefix', only remove tags when
1644 `org-agenda-prefix-format' contains a `%T' specifier."
1645 :group 'org-agenda-line-format
1646 :type '(choice
1647 (const :tag "Always" t)
1648 (const :tag "Never" nil)
1649 (const :tag "When prefix format contains %T" prefix)))
1651 (if (fboundp 'defvaralias)
1652 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1653 'org-agenda-remove-tags))
1655 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1656 "Shift tags in agenda items to this column.
1657 If this number is positive, it specifies the column. If it is negative,
1658 it means that the tags should be flushright to that column. For example,
1659 -80 works well for a normal 80 character screen."
1660 :group 'org-agenda-line-format
1661 :type 'integer)
1663 (if (fboundp 'defvaralias)
1664 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1666 (defcustom org-agenda-fontify-priorities 'cookies
1667 "Non-nil means highlight low and high priorities in agenda.
1668 When t, the highest priority entries are bold, lowest priority italic.
1669 However, settings in `org-priority-faces' will overrule these faces.
1670 When this variable is the symbol `cookies', only fontify the
1671 cookies, not the entire task.
1672 This may also be an association list of priority faces, whose
1673 keys are the character values of `org-highest-priority',
1674 `org-default-priority', and `org-lowest-priority' (the default values
1675 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1676 color as a string, or a list like `(:background \"Red\")'.
1677 If it is a color, the variable `org-faces-easy-properties'
1678 determines if it is a foreground or a background color."
1679 :group 'org-agenda-line-format
1680 :type '(choice
1681 (const :tag "Never" nil)
1682 (const :tag "Defaults" t)
1683 (const :tag "Cookies only" cookies)
1684 (repeat :tag "Specify"
1685 (list (character :tag "Priority" :value ?A)
1686 (choice :tag "Face "
1687 (string :tag "Color")
1688 (sexp :tag "Face"))))))
1690 (defcustom org-agenda-day-face-function nil
1691 "Function called to determine what face should be used to display a day.
1692 The only argument passed to that function is the day. It should
1693 returns a face, or nil if does not want to specify a face and let
1694 the normal rules apply."
1695 :group 'org-agenda-line-format
1696 :version "24.1"
1697 :type 'function)
1699 (defcustom org-agenda-category-icon-alist nil
1700 "Alist of category icon to be displayed in agenda views.
1702 Each entry should have the following format:
1704 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1706 Where CATEGORY-REGEXP is a regexp matching the categories where
1707 the icon should be displayed.
1708 FILE-OR-DATA either a file path or a string containing image data.
1710 The other fields can be omitted safely if not needed:
1711 TYPE indicates the image type.
1712 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1713 image data.
1714 PROPS are additional image attributes to assign to the image,
1715 like, e.g. `:ascent center'.
1717 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1719 If you want to set the display properties yourself, just put a
1720 list as second element:
1722 (CATEGORY-REGEXP (MY PROPERTY LIST))
1724 For example, to display a 16px horizontal space for Emacs
1725 category, you can use:
1727 (\"Emacs\" '(space . (:width (16))))"
1728 :group 'org-agenda-line-format
1729 :version "24.1"
1730 :type '(alist :key-type (string :tag "Regexp matching category")
1731 :value-type (choice (list :tag "Icon"
1732 (string :tag "File or data")
1733 (symbol :tag "Type")
1734 (boolean :tag "Data?")
1735 (repeat :tag "Extra image properties" :inline t symbol))
1736 (list :tag "Display properties" sexp))))
1738 (defgroup org-agenda-column-view nil
1739 "Options concerning column view in the agenda."
1740 :tag "Org Agenda Column View"
1741 :group 'org-agenda)
1743 (defcustom org-agenda-columns-show-summaries t
1744 "Non-nil means show summaries for columns displayed in the agenda view."
1745 :group 'org-agenda-column-view
1746 :type 'boolean)
1748 (defcustom org-agenda-columns-compute-summary-properties t
1749 "Non-nil means recompute all summary properties before column view.
1750 When column view in the agenda is listing properties that have a summary
1751 operator, it can go to all relevant buffers and recompute the summaries
1752 there. This can mean overhead for the agenda column view, but is necessary
1753 to have thing up to date.
1754 As a special case, a CLOCKSUM property also makes sure that the clock
1755 computations are current."
1756 :group 'org-agenda-column-view
1757 :type 'boolean)
1759 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1760 "Non-nil means the duration of an appointment will add to day effort.
1761 The property to which appointment durations will be added is the one given
1762 in the option `org-effort-property'. If an appointment does not have
1763 an end time, `org-agenda-default-appointment-duration' will be used. If that
1764 is not set, an appointment without end time will not contribute to the time
1765 estimate."
1766 :group 'org-agenda-column-view
1767 :type 'boolean)
1769 (defcustom org-agenda-auto-exclude-function nil
1770 "A function called with a tag to decide if it is filtered on '/ RET'.
1771 The sole argument to the function, which is called once for each
1772 possible tag, is a string giving the name of the tag. The
1773 function should return either nil if the tag should be included
1774 as normal, or \"-<TAG>\" to exclude the tag.
1775 Note that for the purpose of tag filtering, only the lower-case version of
1776 all tags will be considered, so that this function will only ever see
1777 the lower-case version of all tags."
1778 :group 'org-agenda
1779 :type 'function)
1781 (defcustom org-agenda-bulk-custom-functions nil
1782 "Alist of characters and custom functions for bulk actions.
1783 For example, this value makes those two functions available:
1785 '((?R set-category)
1786 (?C bulk-cut))
1788 With selected entries in an agenda buffer, `B R' will call
1789 the custom function `set-category' on the selected entries.
1790 Note that functions in this alist don't need to be quoted."
1791 :type 'alist
1792 :version "24.1"
1793 :group 'org-agenda)
1795 (eval-when-compile
1796 (require 'cl))
1797 (require 'org)
1799 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1800 "Execute BODY with point at location given by `org-hd-marker' property.
1801 If STRING is non-nil, the text property will be fetched from position 0
1802 in that string. If STRING is nil, it will be fetched from the beginning
1803 of the current line."
1804 (org-with-gensyms (marker)
1805 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1806 'org-hd-marker ,string)))
1807 (with-current-buffer (marker-buffer ,marker)
1808 (save-excursion
1809 (goto-char ,marker)
1810 ,@body)))))
1811 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1813 (defun org-add-agenda-custom-command (entry)
1814 "Replace or add a command in `org-agenda-custom-commands'.
1815 This is mostly for hacking and trying a new command - once the command
1816 works you probably want to add it to `org-agenda-custom-commands' for good."
1817 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1818 (if ass
1819 (setcdr ass (cdr entry))
1820 (push entry org-agenda-custom-commands))))
1822 ;;; Define the org-agenda-mode
1824 (defvar org-agenda-mode-map (make-sparse-keymap)
1825 "Keymap for `org-agenda-mode'.")
1826 (if (fboundp 'defvaralias)
1827 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1829 (defvar org-agenda-menu) ; defined later in this file.
1830 (defvar org-agenda-restrict) ; defined later in this file.
1831 (defvar org-agenda-follow-mode nil)
1832 (defvar org-agenda-entry-text-mode nil)
1833 (defvar org-agenda-clockreport-mode nil)
1834 (defvar org-agenda-show-log nil)
1835 (defvar org-agenda-redo-command nil)
1836 (defvar org-agenda-query-string nil)
1837 (defvar org-agenda-mode-hook nil
1838 "Hook for `org-agenda-mode', run after the mode is turned on.")
1839 (defvar org-agenda-type nil)
1840 (defvar org-agenda-force-single-file nil)
1841 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1844 ;;; Multiple agenda buffers support
1846 (defcustom org-agenda-sticky nil
1847 "Non-nil means agenda q key will bury agenda buffers.
1848 Agenda commands will then show existing buffer instead of generating new ones.
1849 When nil, `q' will kill the single agenda buffer."
1850 :group 'org-agenda
1851 :type 'boolean
1852 :set (lambda (var val)
1853 (if (boundp var)
1854 (org-toggle-sticky-agenda (if val 1 0))
1855 (set var val))))
1857 (defun org-toggle-sticky-agenda (&optional arg)
1858 "Toggle `org-agenda-sticky'."
1859 (interactive "P")
1860 (let ((new-value (if arg
1861 (> (prefix-numeric-value arg) 0)
1862 (not org-agenda-sticky))))
1863 (if (equal new-value org-agenda-sticky)
1864 (and (org-called-interactively-p 'interactive)
1865 (message "Sticky agenda was already %s"
1866 (if org-agenda-sticky "enabled" "disabled")))
1867 (setq org-agenda-sticky new-value)
1868 (org-agenda-kill-all-agenda-buffers)
1869 (and (org-called-interactively-p 'interactive)
1870 (message "Sticky agenda was %s"
1871 (if org-agenda-sticky "enabled" "disabled"))))))
1873 (defvar org-agenda-buffer nil
1874 "Agenda buffer currently being generated.")
1876 (defvar org-agenda-last-prefix-arg nil)
1877 (defvar org-agenda-this-buffer-name nil)
1878 (defvar org-agenda-doing-sticky-redo nil)
1879 (defvar org-agenda-this-buffer-is-sticky nil)
1881 (defconst org-agenda-local-vars
1882 '(org-agenda-this-buffer-name
1883 org-agenda-undo-list
1884 org-agenda-pending-undo-list
1885 org-agenda-follow-mode
1886 org-agenda-entry-text-mode
1887 org-agenda-clockreport-mode
1888 org-agenda-show-log
1889 org-agenda-redo-command
1890 org-agenda-query-string
1891 org-agenda-type
1892 org-agenda-bulk-marked-entries
1893 org-agenda-undo-has-started-in
1894 org-agenda-last-arguments
1895 org-agenda-info
1896 org-agenda-tag-filter-overlays
1897 org-agenda-cat-filter-overlays
1898 org-pre-agenda-window-conf
1899 org-agenda-columns-active
1900 org-agenda-tag-filter
1901 org-agenda-category-filter
1902 org-agenda-markers
1903 org-agenda-last-search-view-search-was-boolean
1904 org-agenda-filtered-by-category
1905 org-agenda-filter-form
1906 org-agenda-show-window
1907 org-agenda-cycle-counter
1908 org-agenda-last-prefix-arg)
1909 "Variables that must be local in agenda buffers to allow multiple buffers.")
1911 (defun org-agenda-mode ()
1912 "Mode for time-sorted view on action items in Org-mode files.
1914 The following commands are available:
1916 \\{org-agenda-mode-map}"
1917 (interactive)
1918 (cond (org-agenda-doing-sticky-redo
1919 ;; Refreshing sticky agenda-buffer
1921 ;; Preserve the value of `org-agenda-local-vars' variables,
1922 ;; while letting `kill-all-local-variables' kill the rest
1923 (let ((save (buffer-local-variables)))
1924 (kill-all-local-variables)
1925 (mapc 'make-local-variable org-agenda-local-vars)
1926 (dolist (elem save)
1927 (let ((var (car elem))
1928 (val (cdr elem)))
1929 (when (and val
1930 (member var org-agenda-local-vars))
1931 (set var val)))))
1932 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1933 (org-agenda-sticky
1934 ;; Creating a sticky Agenda buffer for the first time
1935 (kill-all-local-variables)
1936 (mapc 'make-local-variable org-agenda-local-vars)
1937 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1939 ;; Creating a non-sticky agenda buffer
1940 (kill-all-local-variables)
1941 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
1942 (setq org-agenda-undo-list nil
1943 org-agenda-pending-undo-list nil
1944 org-agenda-bulk-marked-entries nil)
1945 (setq major-mode 'org-agenda-mode)
1946 ;; Keep global-font-lock-mode from turning on font-lock-mode
1947 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1948 (setq mode-name "Org-Agenda")
1949 (use-local-map org-agenda-mode-map)
1950 (easy-menu-add org-agenda-menu)
1951 (if org-startup-truncated (setq truncate-lines t))
1952 (org-set-local 'line-move-visual nil)
1953 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1954 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1955 ;; Make sure properties are removed when copying text
1956 (make-local-variable 'filter-buffer-substring-functions)
1957 (add-hook 'filter-buffer-substring-functions
1958 (lambda (fun start end delete)
1959 (substring-no-properties (funcall fun start end delete))))
1960 (unless org-agenda-keep-modes
1961 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1962 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1963 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1964 org-agenda-show-log org-agenda-start-with-log-mode))
1966 (easy-menu-change
1967 '("Agenda") "Agenda Files"
1968 (append
1969 (list
1970 (vector
1971 (if (get 'org-agenda-files 'org-restrict)
1972 "Restricted to single file"
1973 "Edit File List")
1974 '(org-edit-agenda-file-list)
1975 (not (get 'org-agenda-files 'org-restrict)))
1976 "--")
1977 (mapcar 'org-file-menu-entry (org-agenda-files))))
1978 (org-agenda-set-mode-name)
1979 (apply
1980 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1981 (list 'org-agenda-mode-hook)))
1983 (substitute-key-definition 'undo 'org-agenda-undo
1984 org-agenda-mode-map global-map)
1985 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1986 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1987 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1988 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1989 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1990 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1991 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1992 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1993 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1994 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1995 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1996 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1997 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1998 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1999 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2000 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2001 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2002 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2003 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2004 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2005 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2006 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2007 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2008 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2009 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2010 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2011 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2012 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2013 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2014 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2015 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2016 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2017 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2018 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2019 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2020 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2021 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2022 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2023 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2024 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
2025 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
2026 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2027 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2028 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2029 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2031 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2032 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2033 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2034 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2035 (while l (org-defkey org-agenda-mode-map
2036 (int-to-string (pop l)) 'digit-argument)))
2038 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2039 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2040 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2041 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2042 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2043 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2044 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2045 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2046 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2047 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
2048 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2049 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2050 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2051 'org-clock-modify-effort-estimate)
2052 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2053 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2054 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2055 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2056 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2057 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2058 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2059 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
2060 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2061 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2062 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2063 (substitute-key-definition 'next-line 'org-agenda-next-line
2064 org-agenda-mode-map global-map)
2065 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2066 org-agenda-mode-map global-map)
2067 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2068 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2069 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2070 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2071 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2072 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2073 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2074 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2075 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2076 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2077 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2078 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2079 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2080 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2081 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2082 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2083 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2084 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2085 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2086 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2087 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2088 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2089 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2090 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2091 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2092 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2093 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2094 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2095 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2096 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2098 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2099 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2100 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2101 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2102 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2103 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2104 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2105 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-category)
2106 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2107 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2108 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2109 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2111 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2112 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2113 (when org-agenda-mouse-1-follows-link
2114 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2115 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2116 '("Agenda"
2117 ("Agenda Files")
2118 "--"
2119 ("Agenda Dates"
2120 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2121 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2122 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2123 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2124 "--"
2125 ("View"
2126 ["Day View" org-agenda-day-view
2127 :active (org-agenda-check-type nil 'agenda)
2128 :style radio :selected (eq org-agenda-current-span 'day)
2129 :keys "v d (or just d)"]
2130 ["Week View" org-agenda-week-view
2131 :active (org-agenda-check-type nil 'agenda)
2132 :style radio :selected (eq org-agenda-current-span 'week)
2133 :keys "v w (or just w)"]
2134 ["Month View" org-agenda-month-view
2135 :active (org-agenda-check-type nil 'agenda)
2136 :style radio :selected (eq org-agenda-current-span 'month)
2137 :keys "v m"]
2138 ["Year View" org-agenda-year-view
2139 :active (org-agenda-check-type nil 'agenda)
2140 :style radio :selected (eq org-agenda-current-span 'year)
2141 :keys "v y"]
2142 "--"
2143 ["Include Diary" org-agenda-toggle-diary
2144 :style toggle :selected org-agenda-include-diary
2145 :active (org-agenda-check-type nil 'agenda)]
2146 ["Include Deadlines" org-agenda-toggle-deadlines
2147 :style toggle :selected org-agenda-include-deadlines
2148 :active (org-agenda-check-type nil 'agenda)]
2149 ["Use Time Grid" org-agenda-toggle-time-grid
2150 :style toggle :selected org-agenda-use-time-grid
2151 :active (org-agenda-check-type nil 'agenda)]
2152 "--"
2153 ["Show clock report" org-agenda-clockreport-mode
2154 :style toggle :selected org-agenda-clockreport-mode
2155 :active (org-agenda-check-type nil 'agenda)]
2156 ["Show some entry text" org-agenda-entry-text-mode
2157 :style toggle :selected org-agenda-entry-text-mode
2158 :active t]
2159 "--"
2160 ["Show Logbook entries" org-agenda-log-mode
2161 :style toggle :selected org-agenda-show-log
2162 :active (org-agenda-check-type nil 'agenda 'timeline)
2163 :keys "v l (or just l)"]
2164 ["Include archived trees" org-agenda-archives-mode
2165 :style toggle :selected org-agenda-archives-mode :active t
2166 :keys "v a"]
2167 ["Include archive files" (org-agenda-archives-mode t)
2168 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2169 :keys "v A"]
2170 "--"
2171 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2172 ["Write view to file" org-agenda-write t]
2173 ["Rebuild buffer" org-agenda-redo t]
2174 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2175 "--"
2176 ["Show original entry" org-agenda-show t]
2177 ["Go To (other window)" org-agenda-goto t]
2178 ["Go To (this window)" org-agenda-switch-to t]
2179 ["Follow Mode" org-agenda-follow-mode
2180 :style toggle :selected org-agenda-follow-mode :active t]
2181 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2182 "--"
2183 ("TODO"
2184 ["Cycle TODO" org-agenda-todo t]
2185 ["Next TODO set" org-agenda-todo-nextset t]
2186 ["Previous TODO set" org-agenda-todo-previousset t]
2187 ["Add note" org-agenda-add-note t])
2188 ("Archive/Refile/Delete"
2189 ["Archive default" org-agenda-archive-default t]
2190 ["Archive default" org-agenda-archive-default-with-confirmation t]
2191 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2192 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2193 ["Archive subtree" org-agenda-archive t]
2194 "--"
2195 ["Refile" org-agenda-refile t]
2196 "--"
2197 ["Delete subtree" org-agenda-kill t])
2198 ("Bulk action"
2199 ["Mark entry" org-agenda-bulk-mark t]
2200 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2201 ["Unmark entry" org-agenda-bulk-unmark t]
2202 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2203 ["Act on all marked" org-agenda-bulk-action t]
2204 "--"
2205 ("Tags and Properties"
2206 ["Show all Tags" org-agenda-show-tags t]
2207 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2208 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2209 "--"
2210 ["Column View" org-columns t])
2211 ("Deadline/Schedule"
2212 ["Schedule" org-agenda-schedule t]
2213 ["Set Deadline" org-agenda-deadline t]
2214 "--"
2215 ["Mark item" org-agenda-action :active t :keys "k m"]
2216 ["Show mark item" org-agenda-action :active t :keys "k v"]
2217 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2218 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2219 "--"
2220 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2221 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2222 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2223 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2224 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2225 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2226 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2227 ("Clock and Effort"
2228 ["Clock in" org-agenda-clock-in t]
2229 ["Clock out" org-agenda-clock-out t]
2230 ["Clock cancel" org-agenda-clock-cancel t]
2231 ["Goto running clock" org-clock-goto t]
2232 "--"
2233 ["Set Effort" org-agenda-set-effort t]
2234 ["Change clocked effort" org-clock-modify-effort-estimate
2235 (org-clock-is-active)])
2236 ("Priority"
2237 ["Set Priority" org-agenda-priority t]
2238 ["Increase Priority" org-agenda-priority-up t]
2239 ["Decrease Priority" org-agenda-priority-down t]
2240 ["Show Priority" org-agenda-show-priority t])
2241 ("Calendar/Diary"
2242 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2243 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2244 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2245 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2246 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2247 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2248 "--"
2249 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2250 "--"
2251 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2252 "--"
2253 ("MobileOrg"
2254 ["Push Files and Views" org-mobile-push t]
2255 ["Get Captured and Flagged" org-mobile-pull t]
2256 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2257 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2258 "--"
2259 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2260 "--"
2261 ["Quit" org-agenda-quit t]
2262 ["Exit and Release Buffers" org-agenda-exit t]
2265 ;;; Agenda undo
2267 (defvar org-agenda-allow-remote-undo t
2268 "Non-nil means allow remote undo from the agenda buffer.")
2269 (defvar org-agenda-undo-list nil
2270 "List of undoable operations in the agenda since last refresh.")
2271 (defvar org-agenda-undo-has-started-in nil
2272 "Buffers that have already seen `undo-start' in the current undo sequence.")
2273 (defvar org-agenda-pending-undo-list nil
2274 "In a series of undo commands, this is the list of remaining undo items.")
2276 (defun org-agenda-undo ()
2277 "Undo a remote editing step in the agenda.
2278 This undoes changes both in the agenda buffer and in the remote buffer
2279 that have been changed along."
2280 (interactive)
2281 (or org-agenda-allow-remote-undo
2282 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2283 (if (not (eq this-command last-command))
2284 (setq org-agenda-undo-has-started-in nil
2285 org-agenda-pending-undo-list org-agenda-undo-list))
2286 (if (not org-agenda-pending-undo-list)
2287 (error "No further undo information"))
2288 (let* ((entry (pop org-agenda-pending-undo-list))
2289 buf line cmd rembuf)
2290 (setq cmd (pop entry) line (pop entry))
2291 (setq rembuf (nth 2 entry))
2292 (org-with-remote-undo rembuf
2293 (while (bufferp (setq buf (pop entry)))
2294 (if (pop entry)
2295 (with-current-buffer buf
2296 (let ((last-undo-buffer buf)
2297 (inhibit-read-only t))
2298 (unless (memq buf org-agenda-undo-has-started-in)
2299 (push buf org-agenda-undo-has-started-in)
2300 (make-local-variable 'pending-undo-list)
2301 (undo-start))
2302 (while (and pending-undo-list
2303 (listp pending-undo-list)
2304 (not (car pending-undo-list)))
2305 (pop pending-undo-list))
2306 (undo-more 1))))))
2307 (org-goto-line line)
2308 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2310 (defun org-verify-change-for-undo (l1 l2)
2311 "Verify that a real change occurred between the undo lists L1 and L2."
2312 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2313 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2314 (not (eq l1 l2)))
2316 ;;; Agenda dispatch
2318 (defvar org-agenda-restrict nil)
2319 (defvar org-agenda-restrict-begin (make-marker))
2320 (defvar org-agenda-restrict-end (make-marker))
2321 (defvar org-agenda-last-dispatch-buffer nil)
2322 (defvar org-agenda-overriding-restriction nil)
2324 ;;;###autoload
2325 (defun org-agenda (&optional arg keys restriction)
2326 "Dispatch agenda commands to collect entries to the agenda buffer.
2327 Prompts for a command to execute. Any prefix arg will be passed
2328 on to the selected command. The default selections are:
2330 a Call `org-agenda-list' to display the agenda for current day or week.
2331 t Call `org-todo-list' to display the global todo list.
2332 T Call `org-todo-list' to display the global todo list, select only
2333 entries with a specific TODO keyword (the user gets a prompt).
2334 m Call `org-tags-view' to display headlines with tags matching
2335 a condition (the user is prompted for the condition).
2336 M Like `m', but select only TODO entries, no ordinary headlines.
2337 L Create a timeline for the current buffer.
2338 e Export views to associated files.
2339 s Search entries for keywords.
2340 / Multi occur across all agenda files and also files listed
2341 in `org-agenda-text-search-extra-files'.
2342 < Restrict agenda commands to buffer, subtree, or region.
2343 Press several times to get the desired effect.
2344 > Remove a previous restriction.
2345 # List \"stuck\" projects.
2346 ! Configure what \"stuck\" means.
2347 C Configure custom agenda commands.
2349 More commands can be added by configuring the variable
2350 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2351 searches can be pre-defined in this way.
2353 If the current buffer is in Org-mode and visiting a file, you can also
2354 first press `<' once to indicate that the agenda should be temporarily
2355 \(until the next use of \\[org-agenda]) restricted to the current file.
2356 Pressing `<' twice means to restrict to the current subtree or region
2357 \(if active)."
2358 (interactive "P")
2359 (catch 'exit
2360 (let* ((prefix-descriptions nil)
2361 (org-agenda-buffer-name org-agenda-buffer-name)
2362 (org-agenda-window-setup (if (equal (buffer-name)
2363 org-agenda-buffer-name)
2364 'current-window
2365 org-agenda-window-setup))
2366 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2367 (org-agenda-custom-commands
2368 ;; normalize different versions
2369 (delq nil
2370 (mapcar
2371 (lambda (x)
2372 (cond ((stringp (cdr x))
2373 (push x prefix-descriptions)
2374 nil)
2375 ((stringp (nth 1 x)) x)
2376 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2377 (t (cons (car x) (cons "" (cdr x))))))
2378 org-agenda-custom-commands)))
2379 (buf (current-buffer))
2380 (bfn (buffer-file-name (buffer-base-buffer)))
2381 entry key type match lprops ans)
2382 ;; Turn off restriction unless there is an overriding one,
2383 (unless org-agenda-overriding-restriction
2384 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2385 ;; There is a request to keep the file list in place
2386 (put 'org-agenda-files 'org-restrict nil))
2387 (setq org-agenda-restrict nil)
2388 (move-marker org-agenda-restrict-begin nil)
2389 (move-marker org-agenda-restrict-end nil))
2390 ;; Delete old local properties
2391 (put 'org-agenda-redo-command 'org-lprops nil)
2392 ;; Delete previously set last-arguments
2393 (put 'org-agenda-redo-command 'last-args nil)
2394 ;; Remember where this call originated
2395 (setq org-agenda-last-dispatch-buffer (current-buffer))
2396 (unless keys
2397 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2398 keys (car ans)
2399 restriction (cdr ans)))
2400 ;; If we have sticky agenda buffers, set a name for the buffer,
2401 ;; depending on the invoking keys. The user may still set this
2402 ;; as a command option, which will overwrite what we do here.
2403 (if org-agenda-sticky
2404 (setq org-agenda-buffer-name (format "*Org Agenda(%s)*" keys)))
2405 ;; Establish the restriction, if any
2406 (when (and (not org-agenda-overriding-restriction) restriction)
2407 (put 'org-agenda-files 'org-restrict (list bfn))
2408 (cond
2409 ((eq restriction 'region)
2410 (setq org-agenda-restrict t)
2411 (move-marker org-agenda-restrict-begin (region-beginning))
2412 (move-marker org-agenda-restrict-end (region-end)))
2413 ((eq restriction 'subtree)
2414 (save-excursion
2415 (setq org-agenda-restrict t)
2416 (org-back-to-heading t)
2417 (move-marker org-agenda-restrict-begin (point))
2418 (move-marker org-agenda-restrict-end
2419 (progn (org-end-of-subtree t)))))))
2421 ;; For example the todo list should not need it (but does...)
2422 (cond
2423 ((setq entry (assoc keys org-agenda-custom-commands))
2424 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2425 (progn
2426 (setq type (nth 2 entry) match (eval (nth 3 entry))
2427 lprops (nth 4 entry))
2428 (put 'org-agenda-redo-command 'org-lprops lprops)
2429 (cond
2430 ((eq type 'agenda)
2431 (org-let lprops '(org-agenda-list current-prefix-arg)))
2432 ((eq type 'alltodo)
2433 (org-let lprops '(org-todo-list current-prefix-arg)))
2434 ((eq type 'search)
2435 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2436 ((eq type 'stuck)
2437 (org-let lprops '(org-agenda-list-stuck-projects
2438 current-prefix-arg)))
2439 ((eq type 'tags)
2440 (org-let lprops '(org-tags-view current-prefix-arg match)))
2441 ((eq type 'tags-todo)
2442 (org-let lprops '(org-tags-view '(4) match)))
2443 ((eq type 'todo)
2444 (org-let lprops '(org-todo-list match)))
2445 ((eq type 'tags-tree)
2446 (org-check-for-org-mode)
2447 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2448 ((eq type 'todo-tree)
2449 (org-check-for-org-mode)
2450 (org-let lprops
2451 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2452 (regexp-quote match) "\\>"))))
2453 ((eq type 'occur-tree)
2454 (org-check-for-org-mode)
2455 (org-let lprops '(org-occur match)))
2456 ((functionp type)
2457 (org-let lprops '(funcall type match)))
2458 ((fboundp type)
2459 (org-let lprops '(funcall type match)))
2460 (t (error "Invalid custom agenda command type %s" type))))
2461 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2462 ((equal keys "C")
2463 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2464 (customize-variable 'org-agenda-custom-commands))
2465 ((equal keys "a") (call-interactively 'org-agenda-list))
2466 ((equal keys "s") (call-interactively 'org-search-view))
2467 ((equal keys "t") (call-interactively 'org-todo-list))
2468 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2469 ((equal keys "m") (call-interactively 'org-tags-view))
2470 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2471 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2472 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2473 (org-add-hook
2474 'post-command-hook
2475 (lambda ()
2476 (unless (current-message)
2477 (let* ((m (org-agenda-get-any-marker))
2478 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2479 (when note
2480 (message (concat
2481 "FLAGGING-NOTE ([?] for more info): "
2482 (org-add-props
2483 (replace-regexp-in-string
2484 "\\\\n" "//"
2485 (copy-sequence note))
2486 nil 'face 'org-warning)))))))
2487 t t))
2488 ((equal keys "L")
2489 (unless (derived-mode-p 'org-mode)
2490 (error "This is not an Org-mode file"))
2491 (unless restriction
2492 (put 'org-agenda-files 'org-restrict (list bfn))
2493 (org-call-with-arg 'org-timeline arg)))
2494 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2495 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2496 ((equal keys "!") (customize-variable 'org-stuck-projects))
2497 (t (error "Invalid agenda key"))))))
2499 (defun org-agenda-append-agenda ()
2500 "Append another agenda view to the current one.
2501 This function allows interactive building of block agendas.
2502 Agenda views are separated by `org-agenda-block-separator'."
2503 (interactive)
2504 (unless (string= (buffer-name) org-agenda-buffer-name)
2505 (error "Can only append from within agenda buffer"))
2506 (let ((org-agenda-multi t))
2507 (org-agenda)
2508 (widen)))
2510 (defun org-agenda-normalize-custom-commands (cmds)
2511 (delq nil
2512 (mapcar
2513 (lambda (x)
2514 (cond ((stringp (cdr x)) nil)
2515 ((stringp (nth 1 x)) x)
2516 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2517 (t (cons (car x) (cons "" (cdr x))))))
2518 cmds)))
2520 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2521 "The user interface for selecting an agenda command."
2522 (catch 'exit
2523 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2524 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2525 (region-p (org-region-active-p))
2526 (custom org-agenda-custom-commands)
2527 (selstring "")
2528 restriction second-time
2529 c entry key type match prefixes rmheader header-end custom1 desc
2530 line lines left right n n1)
2531 (save-window-excursion
2532 (delete-other-windows)
2533 (org-switch-to-buffer-other-window " *Agenda Commands*")
2534 (erase-buffer)
2535 (insert (eval-when-compile
2536 (let ((header
2537 "Press key for an agenda command: < Buffer, subtree/region restriction
2538 -------------------------------- > Remove restriction
2539 a Agenda for current week or day e Export agenda views
2540 t List of all TODO entries T Entries with special TODO kwd
2541 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2542 L Timeline for current buffer # List stuck projects (!=configure)
2543 s Search for keywords * Toggle sticky agenda views
2544 / Multi-occur ? Find :FLAGGED: entries
2545 C Configure custom agenda commands
2547 (start 0))
2548 (while (string-match
2549 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2550 header start)
2551 (setq start (match-end 0))
2552 (add-text-properties (match-beginning 2) (match-end 2)
2553 '(face bold) header))
2554 header)))
2555 (setq header-end (move-marker (make-marker) (point)))
2556 (while t
2557 (setq custom1 custom)
2558 (when (eq rmheader t)
2559 (org-goto-line 1)
2560 (re-search-forward ":" nil t)
2561 (delete-region (match-end 0) (point-at-eol))
2562 (forward-char 1)
2563 (looking-at "-+")
2564 (delete-region (match-end 0) (point-at-eol))
2565 (move-marker header-end (match-end 0)))
2566 (goto-char header-end)
2567 (delete-region (point) (point-max))
2569 ;; Produce all the lines that describe custom commands and prefixes
2570 (setq lines nil)
2571 (while (setq entry (pop custom1))
2572 (setq key (car entry) desc (nth 1 entry)
2573 type (nth 2 entry)
2574 match (nth 3 entry))
2575 (if (> (length key) 1)
2576 (add-to-list 'prefixes (string-to-char key))
2577 (setq line
2578 (format
2579 "%-4s%-14s"
2580 (org-add-props (copy-sequence key)
2581 '(face bold))
2582 (cond
2583 ((string-match "\\S-" desc) desc)
2584 ((eq type 'agenda) "Agenda for current week or day")
2585 ((eq type 'alltodo) "List of all TODO entries")
2586 ((eq type 'search) "Word search")
2587 ((eq type 'stuck) "List of stuck projects")
2588 ((eq type 'todo) "TODO keyword")
2589 ((eq type 'tags) "Tags query")
2590 ((eq type 'tags-todo) "Tags (TODO)")
2591 ((eq type 'tags-tree) "Tags tree")
2592 ((eq type 'todo-tree) "TODO kwd tree")
2593 ((eq type 'occur-tree) "Occur tree")
2594 ((functionp type) (if (symbolp type)
2595 (symbol-name type)
2596 "Lambda expression"))
2597 (t "???"))))
2598 (if org-agenda-menu-show-matcher
2599 (setq line
2600 (concat line ": "
2601 (cond
2602 ((stringp match)
2603 (setq match (copy-sequence match))
2604 (org-add-props match nil 'face 'org-warning))
2605 (match
2606 (format "set of %d commands" (length match)))
2607 (t ""))))
2608 (if (org-string-nw-p match)
2609 (add-text-properties
2610 0 (length line) (list 'help-echo
2611 (concat "Matcher: "match)) line)))
2612 (push line lines)))
2613 (setq lines (nreverse lines))
2614 (when prefixes
2615 (mapc (lambda (x)
2616 (push
2617 (format "%s %s"
2618 (org-add-props (char-to-string x)
2619 nil 'face 'bold)
2620 (or (cdr (assoc (concat selstring
2621 (char-to-string x))
2622 prefix-descriptions))
2623 "Prefix key"))
2624 lines))
2625 prefixes))
2627 ;; Check if we should display in two columns
2628 (if org-agenda-menu-two-column
2629 (progn
2630 (setq n (length lines)
2631 n1 (+ (/ n 2) (mod n 2))
2632 right (nthcdr n1 lines)
2633 left (copy-sequence lines))
2634 (setcdr (nthcdr (1- n1) left) nil))
2635 (setq left lines right nil))
2636 (while left
2637 (insert "\n" (pop left))
2638 (when right
2639 (if (< (current-column) 40)
2640 (move-to-column 40 t)
2641 (insert " "))
2642 (insert (pop right))))
2644 ;; Make the window the right size
2645 (goto-char (point-min))
2646 (if second-time
2647 (if (not (pos-visible-in-window-p (point-max)))
2648 (org-fit-window-to-buffer))
2649 (setq second-time t)
2650 (org-fit-window-to-buffer))
2652 ;; Ask for selection
2653 (message "Press key for agenda command%s:"
2654 (if (or restrict-ok org-agenda-overriding-restriction)
2655 (if org-agenda-overriding-restriction
2656 " (restriction lock active)"
2657 (if restriction
2658 (format " (restricted to %s)" restriction)
2659 " (unrestricted)"))
2660 ""))
2661 (setq c (read-char-exclusive))
2662 (message "")
2663 (cond
2664 ((assoc (char-to-string c) custom)
2665 (setq selstring (concat selstring (char-to-string c)))
2666 (throw 'exit (cons selstring restriction)))
2667 ((memq c prefixes)
2668 (setq selstring (concat selstring (char-to-string c))
2669 prefixes nil
2670 rmheader (or rmheader t)
2671 custom (delq nil (mapcar
2672 (lambda (x)
2673 (if (or (= (length (car x)) 1)
2674 (/= (string-to-char (car x)) c))
2676 (cons (substring (car x) 1) (cdr x))))
2677 custom))))
2678 ((eq c ?*)
2679 (call-interactively 'org-toggle-sticky-agenda)
2680 (sit-for 2))
2681 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2682 (message "Restriction is only possible in Org-mode buffers")
2683 (ding) (sit-for 1))
2684 ((eq c ?1)
2685 (org-agenda-remove-restriction-lock 'noupdate)
2686 (setq restriction 'buffer))
2687 ((eq c ?0)
2688 (org-agenda-remove-restriction-lock 'noupdate)
2689 (setq restriction (if region-p 'region 'subtree)))
2690 ((eq c ?<)
2691 (org-agenda-remove-restriction-lock 'noupdate)
2692 (setq restriction
2693 (cond
2694 ((eq restriction 'buffer)
2695 (if region-p 'region 'subtree))
2696 ((memq restriction '(subtree region))
2697 nil)
2698 (t 'buffer))))
2699 ((eq c ?>)
2700 (org-agenda-remove-restriction-lock 'noupdate)
2701 (setq restriction nil))
2702 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2703 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2704 ((and (> (length selstring) 0) (eq c ?\d))
2705 (delete-window)
2706 (org-agenda-get-restriction-and-command prefix-descriptions))
2708 ((equal c ?q) (error "Abort"))
2709 (t (error "Invalid key %c" c))))))))
2711 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2712 (defvar org-agenda-last-arguments nil
2713 "The arguments of the previous call to `org-agenda'.")
2714 (defun org-agenda-run-series (name series)
2715 (org-let (nth 1 series) '(org-prepare-agenda name))
2716 ;; We need to reset agenda markers here, because when constructing a
2717 ;; block agenda, the individual blocks do not do that.
2718 (org-agenda-reset-markers)
2719 (let* ((org-agenda-multi t)
2720 (redo (list 'org-agenda-run-series name (list 'quote series)))
2721 (org-agenda-overriding-arguments
2722 (or org-agenda-overriding-arguments
2723 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2724 (get 'org-agenda-redo-command 'last-args))))
2725 (cmds (car series))
2726 (gprops (nth 1 series))
2727 match ;; The byte compiler incorrectly complains about this. Keep it!
2728 cmd type lprops)
2729 (while (setq cmd (pop cmds))
2730 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2731 (cond
2732 ((eq type 'agenda)
2733 (org-let2 gprops lprops
2734 '(call-interactively 'org-agenda-list)))
2735 ((eq type 'alltodo)
2736 (org-let2 gprops lprops
2737 '(call-interactively 'org-todo-list)))
2738 ((eq type 'search)
2739 (org-let2 gprops lprops
2740 '(org-search-view current-prefix-arg match nil)))
2741 ((eq type 'stuck)
2742 (org-let2 gprops lprops
2743 '(call-interactively 'org-agenda-list-stuck-projects)))
2744 ((eq type 'tags)
2745 (org-let2 gprops lprops
2746 '(org-tags-view current-prefix-arg match)))
2747 ((eq type 'tags-todo)
2748 (org-let2 gprops lprops
2749 '(org-tags-view '(4) match)))
2750 ((eq type 'todo)
2751 (org-let2 gprops lprops
2752 '(org-todo-list match)))
2753 ((fboundp type)
2754 (org-let2 gprops lprops
2755 '(funcall type match)))
2756 (t (error "Invalid type in command series"))))
2757 (widen)
2758 (setq org-agenda-redo-command redo)
2759 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2760 (goto-char (point-min)))
2761 (org-fit-agenda-window)
2762 (org-let (nth 1 series) '(org-finalize-agenda)))
2764 ;;;###autoload
2765 (defmacro org-batch-agenda (cmd-key &rest parameters)
2766 "Run an agenda command in batch mode and send the result to STDOUT.
2767 If CMD-KEY is a string of length 1, it is used as a key in
2768 `org-agenda-custom-commands' and triggers this command. If it is a
2769 longer string it is used as a tags/todo match string.
2770 Parameters are alternating variable names and values that will be bound
2771 before running the agenda command."
2772 (org-eval-in-environment (org-make-parameter-alist parameters)
2773 (if (> (length cmd-key) 2)
2774 (org-tags-view nil cmd-key)
2775 (org-agenda nil cmd-key)))
2776 (set-buffer org-agenda-buffer-name)
2777 (princ (buffer-string)))
2778 (def-edebug-spec org-batch-agenda (form &rest sexp))
2780 (defvar org-agenda-info nil)
2782 ;;;###autoload
2783 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2784 "Run an agenda command in batch mode and send the result to STDOUT.
2785 If CMD-KEY is a string of length 1, it is used as a key in
2786 `org-agenda-custom-commands' and triggers this command. If it is a
2787 longer string it is used as a tags/todo match string.
2788 Parameters are alternating variable names and values that will be bound
2789 before running the agenda command.
2791 The output gives a line for each selected agenda item. Each
2792 item is a list of comma-separated values, like this:
2794 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2796 category The category of the item
2797 head The headline, without TODO kwd, TAGS and PRIORITY
2798 type The type of the agenda entry, can be
2799 todo selected in TODO match
2800 tagsmatch selected in tags match
2801 diary imported from diary
2802 deadline a deadline on given date
2803 scheduled scheduled on given date
2804 timestamp entry has timestamp on given date
2805 closed entry was closed on given date
2806 upcoming-deadline warning about deadline
2807 past-scheduled forwarded scheduled item
2808 block entry has date block including g. date
2809 todo The todo keyword, if any
2810 tags All tags including inherited ones, separated by colons
2811 date The relevant date, like 2007-2-14
2812 time The time, like 15:00-16:50
2813 extra Sting with extra planning info
2814 priority-l The priority letter if any was given
2815 priority-n The computed numerical priority
2816 agenda-day The day in the agenda where this is listed"
2817 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2818 (org-make-parameter-alist parameters))
2819 (if (> (length cmd-key) 2)
2820 (org-tags-view nil cmd-key)
2821 (org-agenda nil cmd-key)))
2822 (set-buffer org-agenda-buffer-name)
2823 (let* ((lines (org-split-string (buffer-string) "\n"))
2824 line)
2825 (while (setq line (pop lines))
2826 (catch 'next
2827 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2828 (setq org-agenda-info
2829 (org-fix-agenda-info (text-properties-at 0 line)))
2830 (princ
2831 (mapconcat 'org-agenda-export-csv-mapper
2832 '(org-category txt type todo tags date time extra
2833 priority-letter priority agenda-day)
2834 ","))
2835 (princ "\n")))))
2836 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2838 (defun org-fix-agenda-info (props)
2839 "Make sure all properties on an agenda item have a canonical form.
2840 This ensures the export commands can easily use it."
2841 (let (tmp re)
2842 (when (setq tmp (plist-get props 'tags))
2843 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2844 (when (setq tmp (plist-get props 'date))
2845 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2846 (let ((calendar-date-display-form '(year "-" month "-" day)))
2847 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2849 (setq tmp (calendar-date-string tmp)))
2850 (setq props (plist-put props 'date tmp)))
2851 (when (setq tmp (plist-get props 'day))
2852 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2853 (let ((calendar-date-display-form '(year "-" month "-" day)))
2854 (setq tmp (calendar-date-string tmp)))
2855 (setq props (plist-put props 'day tmp))
2856 (setq props (plist-put props 'agenda-day tmp)))
2857 (when (setq tmp (plist-get props 'txt))
2858 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2859 (plist-put props 'priority-letter (match-string 1 tmp))
2860 (setq tmp (replace-match "" t t tmp)))
2861 (when (and (setq re (plist-get props 'org-todo-regexp))
2862 (setq re (concat "\\`\\.*" re " ?"))
2863 (string-match re tmp))
2864 (plist-put props 'todo (match-string 1 tmp))
2865 (setq tmp (replace-match "" t t tmp)))
2866 (plist-put props 'txt tmp)))
2867 props)
2869 (defun org-agenda-export-csv-mapper (prop)
2870 (let ((res (plist-get org-agenda-info prop)))
2871 (setq res
2872 (cond
2873 ((not res) "")
2874 ((stringp res) res)
2875 (t (prin1-to-string res))))
2876 (while (string-match "," res)
2877 (setq res (replace-match ";" t t res)))
2878 (org-trim res)))
2881 ;;;###autoload
2882 (defun org-store-agenda-views (&rest parameters)
2883 (interactive)
2884 (eval (list 'org-batch-store-agenda-views)))
2886 ;;;###autoload
2887 (defmacro org-batch-store-agenda-views (&rest parameters)
2888 "Run all custom agenda commands that have a file argument."
2889 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2890 (pop-up-frames nil)
2891 (dir default-directory)
2892 (pars (org-make-parameter-alist parameters))
2893 cmd thiscmdkey files opts cmd-or-set)
2894 (save-window-excursion
2895 (while cmds
2896 (setq cmd (pop cmds)
2897 thiscmdkey (car cmd)
2898 cmd-or-set (nth 2 cmd)
2899 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2900 files (nth (if (listp cmd-or-set) 4 5) cmd))
2901 (if (stringp files) (setq files (list files)))
2902 (when files
2903 (org-eval-in-environment (append org-agenda-exporter-settings
2904 opts pars)
2905 (org-agenda nil thiscmdkey))
2906 (set-buffer org-agenda-buffer-name)
2907 (while files
2908 (org-eval-in-environment (append org-agenda-exporter-settings
2909 opts pars)
2910 (org-agenda-write (expand-file-name (pop files) dir) nil t)))
2911 (and (get-buffer org-agenda-buffer-name)
2912 (kill-buffer org-agenda-buffer-name)))))))
2913 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2915 (defun org-agenda-mark-header-line (pos)
2916 "Mark the line at POS as an agenda structure header."
2917 (save-excursion
2918 (goto-char pos)
2919 (put-text-property (point-at-bol) (point-at-eol)
2920 'org-agenda-structural-header t)
2921 (when org-agenda-title-append
2922 (put-text-property (point-at-bol) (point-at-eol)
2923 'org-agenda-title-append org-agenda-title-append))))
2925 (defvar org-mobile-creating-agendas)
2926 (defvar org-agenda-write-buffer-name "Agenda View")
2927 (defun org-agenda-write (file &optional open nosettings)
2928 "Write the current buffer (an agenda view) as a file.
2929 Depending on the extension of the file name, plain text (.txt),
2930 HTML (.html or .htm) or Postscript (.ps) is produced.
2931 If the extension is .ics, run icalendar export over all files used
2932 to construct the agenda and limit the export to entries listed in the
2933 agenda now.
2934 With prefix argument OPEN, open the new file immediately.
2935 If NOSETTINGS is given, do not scope the settings of
2936 `org-agenda-exporter-settings' into the export commands. This is used when
2937 the settings have already been scoped and we do not wish to overrule other,
2938 higher priority settings."
2939 (interactive "FWrite agenda to file: \nP")
2940 (if (not (file-writable-p file))
2941 (error "Cannot write agenda to file %s" file))
2942 (org-let (if nosettings nil org-agenda-exporter-settings)
2943 '(save-excursion
2944 (save-window-excursion
2945 (org-agenda-mark-filtered-text)
2946 (let ((bs (copy-sequence (buffer-string))) beg)
2947 (org-agenda-unmark-filtered-text)
2948 (with-temp-buffer
2949 (rename-buffer org-agenda-write-buffer-name t)
2950 (set-buffer-modified-p nil)
2951 (insert bs)
2952 (org-agenda-remove-marked-text 'org-filtered)
2953 (while (setq beg (text-property-any (point-min) (point-max)
2954 'org-filtered t))
2955 (delete-region
2956 beg (or (next-single-property-change beg 'org-filtered)
2957 (point-max))))
2958 (run-hooks 'org-agenda-before-write-hook)
2959 (cond
2960 ((org-bound-and-true-p org-mobile-creating-agendas)
2961 (org-mobile-write-agenda-for-mobile file))
2962 ((string-match "\\.html?\\'" file)
2963 (require 'htmlize)
2964 (set-buffer (htmlize-buffer (current-buffer)))
2966 (when (and org-agenda-export-html-style
2967 (string-match "<style>" org-agenda-export-html-style))
2968 ;; replace <style> section with org-agenda-export-html-style
2969 (goto-char (point-min))
2970 (kill-region (- (search-forward "<style") 6)
2971 (search-forward "</style>"))
2972 (insert org-agenda-export-html-style))
2973 (write-file file)
2974 (kill-buffer (current-buffer))
2975 (message "HTML written to %s" file))
2976 ((string-match "\\.ps\\'" file)
2977 (require 'ps-print)
2978 (ps-print-buffer-with-faces file)
2979 (message "Postscript written to %s" file))
2980 ((string-match "\\.pdf\\'" file)
2981 (require 'ps-print)
2982 (ps-print-buffer-with-faces
2983 (concat (file-name-sans-extension file) ".ps"))
2984 (call-process "ps2pdf" nil nil nil
2985 (expand-file-name
2986 (concat (file-name-sans-extension file) ".ps"))
2987 (expand-file-name file))
2988 (delete-file (concat (file-name-sans-extension file) ".ps"))
2989 (message "PDF written to %s" file))
2990 ((string-match "\\.ics\\'" file)
2991 (require 'org-icalendar)
2992 (let ((org-agenda-marker-table
2993 (org-create-marker-find-array
2994 (org-agenda-collect-markers)))
2995 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2996 (org-combined-agenda-icalendar-file file))
2997 (apply 'org-export-icalendar 'combine
2998 (org-agenda-files nil 'ifmode))))
3000 (let ((bs (buffer-string)))
3001 (find-file file)
3002 (erase-buffer)
3003 (insert bs)
3004 (save-buffer 0)
3005 (kill-buffer (current-buffer))
3006 (message "Plain text written to %s" file))))))))
3007 (set-buffer org-agenda-buffer-name))
3008 (when open (org-open-file file)))
3010 (defvar org-agenda-tag-filter-overlays nil)
3011 (defvar org-agenda-cat-filter-overlays nil)
3013 (defun org-agenda-mark-filtered-text ()
3014 "Mark all text hidden by filtering with a text property."
3015 (let ((inhibit-read-only t))
3016 (mapc
3017 (lambda (o)
3018 (when (equal (overlay-buffer o) (current-buffer))
3019 (put-text-property
3020 (overlay-start o) (overlay-end o)
3021 'org-filtered t)))
3022 (append org-agenda-tag-filter-overlays
3023 org-agenda-cat-filter-overlays))))
3025 (defun org-agenda-unmark-filtered-text ()
3026 "Remove the filtering text property."
3027 (let ((inhibit-read-only t))
3028 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3030 (defun org-agenda-remove-marked-text (property &optional value)
3031 "Delete all text marked with VALUE of PROPERTY.
3032 VALUE defaults to t."
3033 (let (beg)
3034 (setq value (or value t))
3035 (while (setq beg (text-property-any (point-min) (point-max)
3036 property value))
3037 (delete-region
3038 beg (or (next-single-property-change beg 'org-filtered)
3039 (point-max))))))
3041 (defun org-agenda-add-entry-text ()
3042 "Add entry text to agenda lines.
3043 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3044 entry text following headings shown in the agenda.
3045 Drawers will be excluded, also the line with scheduling/deadline info."
3046 (when (and (> org-agenda-add-entry-text-maxlines 0)
3047 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3048 (let (m txt)
3049 (goto-char (point-min))
3050 (while (not (eobp))
3051 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3052 (beginning-of-line 2)
3053 (setq txt (org-agenda-get-some-entry-text
3054 m org-agenda-add-entry-text-maxlines " > "))
3055 (end-of-line 1)
3056 (if (string-match "\\S-" txt)
3057 (insert "\n" txt)
3058 (or (eobp) (forward-char 1))))))))
3060 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3061 &rest keep)
3062 "Extract entry text from MARKER, at most N-LINES lines.
3063 This will ignore drawers etc, just get the text.
3064 If INDENT is given, prefix every line with this string. If KEEP is
3065 given, it is a list of symbols, defining stuff that should not be
3066 removed from the entry content. Currently only `planning' is allowed here."
3067 (let (txt drawer-re kwd-time-re ind)
3068 (save-excursion
3069 (with-current-buffer (marker-buffer marker)
3070 (if (not (derived-mode-p 'org-mode))
3071 (setq txt "")
3072 (save-excursion
3073 (save-restriction
3074 (widen)
3075 (goto-char marker)
3076 (end-of-line 1)
3077 (setq txt (buffer-substring
3078 (min (1+ (point)) (point-max))
3079 (progn (outline-next-heading) (point)))
3080 drawer-re org-drawer-regexp
3081 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3082 ".*\n?"))
3083 (with-temp-buffer
3084 (insert txt)
3085 (when org-agenda-add-entry-text-descriptive-links
3086 (goto-char (point-min))
3087 (while (org-activate-bracket-links (point-max))
3088 (add-text-properties (match-beginning 0) (match-end 0)
3089 '(face org-link))))
3090 (goto-char (point-min))
3091 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3092 (set-text-properties (match-beginning 0) (match-end 0)
3093 nil))
3094 (goto-char (point-min))
3095 (while (re-search-forward drawer-re nil t)
3096 (delete-region
3097 (match-beginning 0)
3098 (progn (re-search-forward
3099 "^[ \t]*:END:.*\n?" nil 'move)
3100 (point))))
3101 (unless (member 'planning keep)
3102 (goto-char (point-min))
3103 (while (re-search-forward kwd-time-re nil t)
3104 (replace-match "")))
3105 (goto-char (point-min))
3106 (when org-agenda-entry-text-exclude-regexps
3107 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3108 (while (setq re (pop re-list))
3109 (goto-char (point-min))
3110 (while (re-search-forward re nil t)
3111 (replace-match "")))))
3112 (goto-char (point-max))
3113 (skip-chars-backward " \t\n")
3114 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3116 ;; find and remove min common indentation
3117 (goto-char (point-min))
3118 (untabify (point-min) (point-max))
3119 (setq ind (org-get-indentation))
3120 (while (not (eobp))
3121 (unless (looking-at "[ \t]*$")
3122 (setq ind (min ind (org-get-indentation))))
3123 (beginning-of-line 2))
3124 (goto-char (point-min))
3125 (while (not (eobp))
3126 (unless (looking-at "[ \t]*$")
3127 (move-to-column ind)
3128 (delete-region (point-at-bol) (point)))
3129 (beginning-of-line 2))
3131 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3133 (goto-char (point-min))
3134 (when indent
3135 (while (and (not (eobp)) (re-search-forward "^" nil t))
3136 (replace-match indent t t)))
3137 (goto-char (point-min))
3138 (while (looking-at "[ \t]*\n") (replace-match ""))
3139 (goto-char (point-max))
3140 (when (> (org-current-line)
3141 n-lines)
3142 (org-goto-line (1+ n-lines))
3143 (backward-char 1))
3144 (setq txt (buffer-substring (point-min) (point)))))))))
3145 txt))
3147 (defun org-agenda-collect-markers ()
3148 "Collect the markers pointing to entries in the agenda buffer."
3149 (let (m markers)
3150 (save-excursion
3151 (goto-char (point-min))
3152 (while (not (eobp))
3153 (when (setq m (or (org-get-at-bol 'org-hd-marker)
3154 (org-get-at-bol 'org-marker)))
3155 (push m markers))
3156 (beginning-of-line 2)))
3157 (nreverse markers)))
3159 (defun org-create-marker-find-array (marker-list)
3160 "Create a alist of files names with all marker positions in that file."
3161 (let (f tbl m a p)
3162 (while (setq m (pop marker-list))
3163 (setq p (marker-position m)
3164 f (buffer-file-name (or (buffer-base-buffer
3165 (marker-buffer m))
3166 (marker-buffer m))))
3167 (if (setq a (assoc f tbl))
3168 (push (marker-position m) (cdr a))
3169 (push (list f p) tbl)))
3170 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3171 tbl)))
3173 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3174 (defun org-check-agenda-marker-table ()
3175 "Check of the current entry is on the marker list."
3176 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3178 (and (setq a (assoc file org-agenda-marker-table))
3179 (save-match-data
3180 (save-excursion
3181 (org-back-to-heading t)
3182 (member (point) (cdr a)))))))
3184 (defun org-check-for-org-mode ()
3185 "Make sure current buffer is in org-mode. Error if not."
3186 (or (derived-mode-p 'org-mode)
3187 (error "Cannot execute org-mode agenda command on buffer in %s"
3188 major-mode)))
3190 (defun org-fit-agenda-window ()
3191 "Fit the window to the buffer size."
3192 (and (memq org-agenda-window-setup '(reorganize-frame))
3193 (fboundp 'fit-window-to-buffer)
3194 (org-fit-window-to-buffer
3196 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3197 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3199 ;;; Agenda prepare and finalize
3201 (defvar org-agenda-multi nil) ; dynamically scoped
3202 (defvar org-agenda-buffer-name "*Org Agenda*")
3203 (defvar org-pre-agenda-window-conf nil)
3204 (defvar org-agenda-columns-active nil)
3205 (defvar org-agenda-name nil)
3206 (defvar org-agenda-tag-filter nil)
3207 (defvar org-agenda-category-filter nil)
3208 (defvar org-agenda-top-category-filter nil)
3209 (defvar org-agenda-tag-filter-while-redo nil)
3210 (defvar org-agenda-tag-filter-preset nil
3211 "A preset of the tags filter used for secondary agenda filtering.
3212 This must be a list of strings, each string must be a single tag preceded
3213 by \"+\" or \"-\".
3214 This variable should not be set directly, but agenda custom commands can
3215 bind it in the options section. The preset filter is a global property of
3216 the entire agenda view. In a block agenda, it will not work reliably to
3217 define a filter for one of the individual blocks. You need to set it in
3218 the global options and expect it to be applied to the entire view.")
3220 (defvar org-agenda-category-filter-preset nil
3221 "A preset of the category filter used for secondary agenda filtering.
3222 This must be a list of strings, each string must be a single category
3223 preceded by \"+\" or \"-\".
3224 This variable should not be set directly, but agenda custom commands can
3225 bind it in the options section. The preset filter is a global property of
3226 the entire agenda view. In a block agenda, it will not work reliably to
3227 define a filter for one of the individual blocks. You need to set it in
3228 the global options and expect it to be applied to the entire view.")
3231 (defun org-agenda-use-sticky-p ()
3232 "Return non-NIL if existing agenda buffer named
3233 `org-agenda-buffer-name' exists, and should be shown instead of
3234 generating a new one"
3235 (and
3236 ;; turned off by user
3237 org-agenda-sticky
3238 ;; For multi-agenda buffer already exists
3239 (not org-agenda-multi)
3240 ;; buffer found
3241 (get-buffer org-agenda-buffer-name)
3242 ;; C-u parameter is same as last call
3243 (with-current-buffer (get-buffer org-agenda-buffer-name)
3244 (and
3245 (equal current-prefix-arg
3246 org-agenda-last-prefix-arg)
3247 ;; In case user turned stickiness on, while having existing
3248 ;; Agenda buffer active, don't reuse that buffer, because it
3249 ;; does not have org variables local
3250 org-agenda-this-buffer-is-sticky))))
3252 (defun org-prepare-agenda-window (abuf)
3253 "Setup agenda buffer in the window"
3254 (let* ((awin (get-buffer-window abuf))
3255 wconf)
3256 (cond
3257 ((equal (current-buffer) abuf) nil)
3258 (awin (select-window awin))
3259 ((not (setq wconf (current-window-configuration))))
3260 ((equal org-agenda-window-setup 'current-window)
3261 (org-pop-to-buffer-same-window abuf))
3262 ((equal org-agenda-window-setup 'other-window)
3263 (org-switch-to-buffer-other-window abuf))
3264 ((equal org-agenda-window-setup 'other-frame)
3265 (switch-to-buffer-other-frame abuf))
3266 ((equal org-agenda-window-setup 'reorganize-frame)
3267 (delete-other-windows)
3268 (org-switch-to-buffer-other-window abuf)))
3269 ;; additional test in case agenda is invoked from within agenda
3270 ;; buffer via elisp link
3271 (unless (equal (current-buffer) abuf)
3272 (org-pop-to-buffer-same-window abuf))
3273 (setq org-pre-agenda-window-conf wconf)))
3275 (defun org-prepare-agenda (&optional name)
3276 (if (org-agenda-use-sticky-p)
3277 (progn
3278 ;; Popup existing buffer
3279 (org-prepare-agenda-window (get-buffer org-agenda-buffer-name))
3280 (message "Sticky Agenda buffer, use `r' to refresh")
3281 (throw 'exit nil))
3282 (setq org-todo-keywords-for-agenda nil)
3283 (setq org-drawers-for-agenda nil)
3284 (unless org-agenda-persistent-filter
3285 (setq org-agenda-tag-filter nil
3286 org-agenda-category-filter nil))
3287 (put 'org-agenda-tag-filter :preset-filter org-agenda-tag-filter-preset)
3288 (put 'org-agenda-category-filter :preset-filter
3289 org-agenda-category-filter-preset)
3290 (if org-agenda-multi
3291 (progn
3292 (setq buffer-read-only nil)
3293 (goto-char (point-max))
3294 (unless (or (bobp) org-agenda-compact-blocks
3295 (not org-agenda-block-separator))
3296 (insert "\n"
3297 (if (stringp org-agenda-block-separator)
3298 org-agenda-block-separator
3299 (make-string (window-width) org-agenda-block-separator))
3300 "\n"))
3301 (narrow-to-region (point) (point-max)))
3302 (setq org-done-keywords-for-agenda nil)
3304 ;; Setting any org variables that are in org-agenda-local-vars
3305 ;; list need to be done after the prepare call
3306 (org-prepare-agenda-window (get-buffer-create org-agenda-buffer-name))
3307 (setq buffer-read-only nil)
3308 (org-agenda-reset-markers)
3309 (let ((inhibit-read-only t)) (erase-buffer))
3310 (org-agenda-mode)
3311 (setq org-agenda-buffer (current-buffer))
3312 (setq org-agenda-contributing-files nil)
3313 (setq org-agenda-columns-active nil)
3314 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3315 (setq org-todo-keywords-for-agenda
3316 (org-uniquify org-todo-keywords-for-agenda))
3317 (setq org-done-keywords-for-agenda
3318 (org-uniquify org-done-keywords-for-agenda))
3319 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3320 (setq org-agenda-last-prefix-arg current-prefix-arg)
3321 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3322 (and name (not org-agenda-name)
3323 (org-set-local 'org-agenda-name name)))
3324 (setq buffer-read-only nil)))
3326 (defun org-finalize-agenda ()
3327 "Finishing touch for the agenda buffer, called just before displaying it."
3328 (unless org-agenda-multi
3329 (save-excursion
3330 (let ((inhibit-read-only t))
3331 (goto-char (point-min))
3332 (while (org-activate-bracket-links (point-max))
3333 (add-text-properties (match-beginning 0) (match-end 0)
3334 '(face org-link)))
3335 (org-agenda-align-tags)
3336 (unless org-agenda-with-colors
3337 (remove-text-properties (point-min) (point-max) '(face nil))))
3338 (if (and (boundp 'org-agenda-overriding-columns-format)
3339 org-agenda-overriding-columns-format)
3340 (org-set-local 'org-agenda-overriding-columns-format
3341 org-agenda-overriding-columns-format))
3342 (if (and (boundp 'org-agenda-view-columns-initially)
3343 org-agenda-view-columns-initially)
3344 (org-agenda-columns))
3345 (when org-agenda-fontify-priorities
3346 (org-agenda-fontify-priorities))
3347 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3348 (org-agenda-dim-blocked-tasks))
3349 (org-agenda-mark-clocking-task)
3350 (when org-agenda-entry-text-mode
3351 (org-agenda-entry-text-hide)
3352 (org-agenda-entry-text-show))
3353 (if (functionp 'org-habit-insert-consistency-graphs)
3354 (org-habit-insert-consistency-graphs))
3355 (run-hooks 'org-finalize-agenda-hook)
3356 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3357 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3358 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3359 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3360 (org-agenda-filter-apply org-agenda-category-filter 'category))
3361 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)
3364 (defun org-agenda-mark-clocking-task ()
3365 "Mark the current clock entry in the agenda if it is present."
3366 (mapc (lambda (o)
3367 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3368 (delete-overlay o)))
3369 (overlays-in (point-min) (point-max)))
3370 (when (marker-buffer org-clock-hd-marker)
3371 (save-excursion
3372 (goto-char (point-min))
3373 (let (s ov)
3374 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3375 (goto-char s)
3376 (when (equal (org-get-at-bol 'org-hd-marker)
3377 org-clock-hd-marker)
3378 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3379 (overlay-put ov 'type 'org-agenda-clocking)
3380 (overlay-put ov 'face 'org-agenda-clocking)
3381 (overlay-put ov 'help-echo
3382 "The clock is running in this item")))))))
3384 (defun org-agenda-fontify-priorities ()
3385 "Make highest priority lines bold, and lowest italic."
3386 (interactive)
3387 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3388 (delete-overlay o)))
3389 (overlays-in (point-min) (point-max)))
3390 (save-excursion
3391 (let ((inhibit-read-only t)
3392 b e p ov h l)
3393 (goto-char (point-min))
3394 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3395 (setq h (or (get-char-property (point) 'org-highest-priority)
3396 org-highest-priority)
3397 l (or (get-char-property (point) 'org-lowest-priority)
3398 org-lowest-priority)
3399 p (string-to-char (match-string 1))
3400 b (match-beginning 0)
3401 e (if (eq org-agenda-fontify-priorities 'cookies)
3402 (match-end 0)
3403 (point-at-eol))
3404 ov (make-overlay b e))
3405 (overlay-put
3406 ov 'face
3407 (cond ((org-face-from-face-or-color
3408 'priority nil
3409 (cdr (assoc p org-priority-faces))))
3410 ((and (listp org-agenda-fontify-priorities)
3411 (org-face-from-face-or-color
3412 'priority nil
3413 (cdr (assoc p org-agenda-fontify-priorities)))))
3414 ((equal p l) 'italic)
3415 ((equal p h) 'bold)))
3416 (overlay-put ov 'org-type 'org-priority)))))
3418 (defun org-agenda-dim-blocked-tasks ()
3419 "Dim currently blocked TODO's in the agenda display."
3420 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3421 (delete-overlay o)))
3422 (overlays-in (point-min) (point-max)))
3423 (save-excursion
3424 (let ((inhibit-read-only t)
3425 (org-depend-tag-blocked nil)
3426 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3427 org-blocked-by-checkboxes
3428 invis1 b e p ov h l)
3429 (goto-char (point-min))
3430 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3431 (and pos (goto-char (1+ pos))))
3432 (setq org-blocked-by-checkboxes nil invis1 invis)
3433 (let ((marker (org-get-at-bol 'org-hd-marker)))
3434 (when (and marker
3435 (with-current-buffer (marker-buffer marker)
3436 (save-excursion (goto-char marker)
3437 (org-entry-blocked-p))))
3438 (if org-blocked-by-checkboxes (setq invis1 nil))
3439 (setq b (if invis1
3440 (max (point-min) (1- (point-at-bol)))
3441 (point-at-bol))
3442 e (point-at-eol)
3443 ov (make-overlay b e))
3444 (if invis1
3445 (overlay-put ov 'invisible t)
3446 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3447 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3449 (defvar org-agenda-skip-function nil
3450 "Function to be called at each match during agenda construction.
3451 If this function returns nil, the current match should not be skipped.
3452 Otherwise, the function must return a position from where the search
3453 should be continued.
3454 This may also be a Lisp form, it will be evaluated.
3455 Never set this variable using `setq' or so, because then it will apply
3456 to all future agenda commands. If you do want a global skipping condition,
3457 use the option `org-agenda-skip-function-global' instead.
3458 The correct usage for `org-agenda-skip-function' is to bind it with
3459 `let' to scope it dynamically into the agenda-constructing command.
3460 A good way to set it is through options in `org-agenda-custom-commands'.")
3462 (defun org-agenda-skip ()
3463 "Throw to `:skip' in places that should be skipped.
3464 Also moves point to the end of the skipped region, so that search can
3465 continue from there."
3466 (let ((p (point-at-bol)) to)
3467 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3468 (get-text-property p :org-archived)
3469 (org-end-of-subtree t)
3470 (throw :skip t))
3471 (and org-agenda-skip-comment-trees
3472 (get-text-property p :org-comment)
3473 (org-end-of-subtree t)
3474 (throw :skip t))
3475 (if (equal (char-after p) ?#) (throw :skip t))
3476 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3477 (org-agenda-skip-eval org-agenda-skip-function)))
3478 (goto-char to)
3479 (throw :skip t))))
3481 (defun org-agenda-skip-eval (form)
3482 "If FORM is a function or a list, call (or eval) is and return result.
3483 `save-excursion' and `save-match-data' are wrapped around the call, so point
3484 and match data are returned to the previous state no matter what these
3485 functions do."
3486 (let (fp)
3487 (and form
3488 (or (setq fp (functionp form))
3489 (consp form))
3490 (save-excursion
3491 (save-match-data
3492 (if fp
3493 (funcall form)
3494 (eval form)))))))
3496 (defvar org-agenda-markers nil
3497 "List of all currently active markers created by `org-agenda'.")
3498 (defvar org-agenda-last-marker-time (org-float-time)
3499 "Creation time of the last agenda marker.")
3501 (defun org-agenda-new-marker (&optional pos)
3502 "Return a new agenda marker.
3503 Org-mode keeps a list of these markers and resets them when they are
3504 no longer in use."
3505 (let ((m (copy-marker (or pos (point)))))
3506 (setq org-agenda-last-marker-time (org-float-time))
3507 (if org-agenda-buffer
3508 (with-current-buffer org-agenda-buffer
3509 (push m org-agenda-markers))
3510 (push m org-agenda-markers))
3513 (defun org-agenda-reset-markers ()
3514 "Reset markers created by `org-agenda'."
3515 (while org-agenda-markers
3516 (move-marker (pop org-agenda-markers) nil)))
3518 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3519 "Save relative positions of markers in region.
3520 This check for agenda markers in all agenda buffers currently active."
3521 (dolist (buf (buffer-list))
3522 (with-current-buffer buf
3523 (when (eq major-mode 'org-agenda-mode)
3524 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3525 org-agenda-markers)))))
3527 ;;; Entry text mode
3529 (defun org-agenda-entry-text-show-here ()
3530 "Add some text from the entry as context to the current line."
3531 (let (m txt o)
3532 (setq m (org-get-at-bol 'org-hd-marker))
3533 (unless (marker-buffer m)
3534 (error "No marker points to an entry here"))
3535 (setq txt (concat "\n" (org-no-properties
3536 (org-agenda-get-some-entry-text
3537 m org-agenda-entry-text-maxlines " > "))))
3538 (when (string-match "\\S-" txt)
3539 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3540 (overlay-put o 'evaporate t)
3541 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3542 (overlay-put o 'after-string txt))))
3544 (defun org-agenda-entry-text-show ()
3545 "Add entry context for all agenda lines."
3546 (interactive)
3547 (save-excursion
3548 (goto-char (point-max))
3549 (beginning-of-line 1)
3550 (while (not (bobp))
3551 (when (org-get-at-bol 'org-hd-marker)
3552 (org-agenda-entry-text-show-here))
3553 (beginning-of-line 0))))
3555 (defun org-agenda-entry-text-hide ()
3556 "Remove any shown entry context."
3557 (delq nil
3558 (mapcar (lambda (o)
3559 (if (eq (overlay-get o 'org-overlay-type)
3560 'agenda-entry-content)
3561 (progn (delete-overlay o) t)))
3562 (overlays-in (point-min) (point-max)))))
3564 (defun org-agenda-get-day-face (date)
3565 "Return the face DATE should be displayed with."
3566 (or (and (functionp org-agenda-day-face-function)
3567 (funcall org-agenda-day-face-function date))
3568 (cond ((org-agenda-todayp date)
3569 'org-agenda-date-today)
3570 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3571 'org-agenda-date-weekend)
3572 (t 'org-agenda-date))))
3574 ;;; Agenda timeline
3576 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3578 (defun org-timeline (&optional dotodo)
3579 "Show a time-sorted view of the entries in the current org file.
3580 Only entries with a time stamp of today or later will be listed. With
3581 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3582 under the current date.
3583 If the buffer contains an active region, only check the region for
3584 dates."
3585 (interactive "P")
3586 (let* ((dopast t)
3587 (org-agenda-show-log-scoped org-agenda-show-log)
3588 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3589 (current-buffer))))
3590 (date (calendar-current-date))
3591 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3592 (end (if (org-region-active-p) (region-end) (point-max)))
3593 (day-numbers (org-get-all-dates
3594 beg end 'no-ranges
3595 t org-agenda-show-log-scoped ; always include today
3596 org-timeline-show-empty-dates))
3597 (org-deadline-warning-days 0)
3598 (org-agenda-only-exact-dates t)
3599 (today (org-today))
3600 (past t)
3601 args
3602 s e rtn d emptyp)
3603 (setq org-agenda-redo-command
3604 (list 'progn
3605 (list 'org-switch-to-buffer-other-window (current-buffer))
3606 (list 'org-timeline (list 'quote dotodo))))
3607 (if (not dopast)
3608 ;; Remove past dates from the list of dates.
3609 (setq day-numbers (delq nil (mapcar (lambda(x)
3610 (if (>= x today) x nil))
3611 day-numbers))))
3612 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3613 (org-compile-prefix-format 'timeline)
3614 (org-set-sorting-strategy 'timeline)
3615 (if org-agenda-show-log-scoped (push :closed args))
3616 (push :timestamp args)
3617 (push :deadline args)
3618 (push :scheduled args)
3619 (push :sexp args)
3620 (if dotodo (push :todo args))
3621 (insert "Timeline of file " entry "\n")
3622 (add-text-properties (point-min) (point)
3623 (list 'face 'org-agenda-structure))
3624 (org-agenda-mark-header-line (point-min))
3625 (while (setq d (pop day-numbers))
3626 (if (and (listp d) (eq (car d) :omitted))
3627 (progn
3628 (setq s (point))
3629 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3630 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3631 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3632 (if (and (>= d today)
3633 dopast
3634 past)
3635 (progn
3636 (setq past nil)
3637 (insert (make-string 79 ?-) "\n")))
3638 (setq date (calendar-gregorian-from-absolute d))
3639 (setq s (point))
3640 (setq rtn (and (not emptyp)
3641 (apply 'org-agenda-get-day-entries entry
3642 date args)))
3643 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3644 (progn
3645 (insert
3646 (if (stringp org-agenda-format-date)
3647 (format-time-string org-agenda-format-date
3648 (org-time-from-absolute date))
3649 (funcall org-agenda-format-date date))
3650 "\n")
3651 (put-text-property s (1- (point)) 'face
3652 (org-agenda-get-day-face date))
3653 (put-text-property s (1- (point)) 'org-date-line t)
3654 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3655 (if (equal d today)
3656 (put-text-property s (1- (point)) 'org-today t))
3657 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3658 (put-text-property s (1- (point)) 'day d)))))
3659 (goto-char (point-min))
3660 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3661 (point-min)))
3662 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3663 (org-finalize-agenda)
3664 (setq buffer-read-only t)))
3666 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3667 "Return a list of all relevant day numbers from BEG to END buffer positions.
3668 If NO-RANGES is non-nil, include only the start and end dates of a range,
3669 not every single day in the range. If FORCE-TODAY is non-nil, make
3670 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3671 inactive time stamps (those in square brackets) are included.
3672 When EMPTY is non-nil, also include days without any entries."
3673 (let ((re (concat
3674 (if pre-re pre-re "")
3675 (if inactive org-ts-regexp-both org-ts-regexp)))
3676 dates dates1 date day day1 day2 ts1 ts2 pos)
3677 (if force-today
3678 (setq dates (list (org-today))))
3679 (save-excursion
3680 (goto-char beg)
3681 (while (re-search-forward re end t)
3682 (setq day (time-to-days (org-time-string-to-time
3683 (substring (match-string 1) 0 10)
3684 (current-buffer) (match-beginning 0))))
3685 (or (memq day dates) (push day dates)))
3686 (unless no-ranges
3687 (goto-char beg)
3688 (while (re-search-forward org-tr-regexp end t)
3689 (setq pos (match-beginning 0))
3690 (setq ts1 (substring (match-string 1) 0 10)
3691 ts2 (substring (match-string 2) 0 10)
3692 day1 (time-to-days (org-time-string-to-time
3693 ts1 (current-buffer) pos))
3694 day2 (time-to-days (org-time-string-to-time
3695 ts2 (current-buffer) pos)))
3696 (while (< (setq day1 (1+ day1)) day2)
3697 (or (memq day1 dates) (push day1 dates)))))
3698 (setq dates (sort dates '<))
3699 (when empty
3700 (while (setq day (pop dates))
3701 (setq day2 (car dates))
3702 (push day dates1)
3703 (when (and day2 empty)
3704 (if (or (eq empty t)
3705 (and (numberp empty) (<= (- day2 day) empty)))
3706 (while (< (setq day (1+ day)) day2)
3707 (push (list day) dates1))
3708 (push (cons :omitted (- day2 day)) dates1))))
3709 (setq dates (nreverse dates1)))
3710 dates)))
3712 ;;; Agenda Daily/Weekly
3714 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3715 "Start day for the agenda view.
3716 Custom commands can set this variable in the options section.")
3717 (defvar org-starting-day nil) ; local variable in the agenda buffer
3718 (defvar org-agenda-current-span nil
3719 "The current span used in the agenda view.") ; local variable in the agenda buffer
3720 (defvar org-arg-loc nil) ; local variable
3722 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3723 "List of types searched for when creating the daily/weekly agenda.
3724 This variable is a list of symbols that controls the types of
3725 items that appear in the daily/weekly agenda. Allowed symbols in this
3726 list are are
3728 :timestamp List items containing a date stamp or date range matching
3729 the selected date. This includes sexp entries in
3730 angular brackets.
3732 :sexp List entries resulting from plain diary-like sexps.
3734 :deadline List deadline due on that date. When the date is today,
3735 also list any deadlines past due, or due within
3736 `org-deadline-warning-days'. `:deadline' must appear before
3737 `:scheduled' if the setting of
3738 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3739 any effect.
3741 :scheduled List all items which are scheduled for the given date.
3742 The diary for *today* also contains items which were
3743 scheduled earlier and are not yet marked DONE.
3745 By default, all four types are turned on.
3747 Never set this variable globally using `setq', because then it
3748 will apply to all future agenda commands. Instead, bind it with
3749 `let' to scope it dynamically into the agenda-constructing
3750 command. A good way to set it is through options in
3751 `org-agenda-custom-commands'. For a more flexible (though
3752 somewhat less efficient) way of determining what is included in
3753 the daily/weekly agenda, see `org-agenda-skip-function'.")
3755 ;;;###autoload
3756 (defun org-agenda-list (&optional arg start-day span)
3757 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3758 The view will be for the current day or week, but from the overview buffer
3759 you will be able to go to other days/weeks.
3761 With a numeric prefix argument in an interactive call, the agenda will
3762 span ARG days. Lisp programs should instead specify SPAN to change
3763 the number of days. SPAN defaults to `org-agenda-span'.
3765 START-DAY defaults to TODAY, or to the most recent match for the weekday
3766 given in `org-agenda-start-on-weekday'."
3767 (interactive "P")
3768 (if (and (integerp arg) (> arg 0))
3769 (setq span arg arg nil))
3770 (org-prepare-agenda "Day/Week")
3771 (setq start-day (or start-day org-agenda-start-day))
3772 (if org-agenda-overriding-arguments
3773 (setq arg (car org-agenda-overriding-arguments)
3774 start-day (nth 1 org-agenda-overriding-arguments)
3775 span (nth 2 org-agenda-overriding-arguments)))
3776 (if (stringp start-day)
3777 ;; Convert to an absolute day number
3778 (setq start-day (time-to-days (org-read-date nil t start-day))))
3779 (setq org-agenda-last-arguments (list arg start-day span))
3780 (org-compile-prefix-format 'agenda)
3781 (org-set-sorting-strategy 'agenda)
3782 (let* ((span (org-agenda-ndays-to-span
3783 (or span org-agenda-ndays org-agenda-span)))
3784 (today (org-today))
3785 (sd (or start-day today))
3786 (ndays (org-agenda-span-to-ndays span sd))
3787 (org-agenda-start-on-weekday
3788 (if (eq ndays 7)
3789 org-agenda-start-on-weekday))
3790 (thefiles (org-agenda-files nil 'ifmode))
3791 (files thefiles)
3792 (start (if (or (null org-agenda-start-on-weekday)
3793 (< ndays 7))
3795 (let* ((nt (calendar-day-of-week
3796 (calendar-gregorian-from-absolute sd)))
3797 (n1 org-agenda-start-on-weekday)
3798 (d (- nt n1)))
3799 (- sd (+ (if (< d 0) 7 0) d)))))
3800 (day-numbers (list start))
3801 (day-cnt 0)
3802 (inhibit-redisplay (not debug-on-error))
3803 (org-agenda-show-log-scoped org-agenda-show-log)
3804 s e rtn rtnall file date d start-pos end-pos todayp
3805 clocktable-start clocktable-end filter)
3806 (setq org-agenda-redo-command
3807 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3808 (dotimes (n (1- ndays))
3809 (push (1+ (car day-numbers)) day-numbers))
3810 (setq day-numbers (nreverse day-numbers))
3811 (setq clocktable-start (car day-numbers)
3812 clocktable-end (1+ (or (org-last day-numbers) 0)))
3813 (org-set-local 'org-starting-day (car day-numbers))
3814 (org-set-local 'org-arg-loc arg)
3815 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3816 (unless org-agenda-compact-blocks
3817 (let* ((d1 (car day-numbers))
3818 (d2 (org-last day-numbers))
3819 (w1 (org-days-to-iso-week d1))
3820 (w2 (org-days-to-iso-week d2)))
3821 (setq s (point))
3822 (if org-agenda-overriding-header
3823 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3824 nil 'face 'org-agenda-structure) "\n")
3825 (insert (org-agenda-span-name span)
3826 "-agenda"
3827 (if (< (- d2 d1) 350)
3828 (if (= w1 w2)
3829 (format " (W%02d)" w1)
3830 (format " (W%02d-W%02d)" w1 w2))
3832 ":\n")))
3833 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3834 'org-date-line t))
3835 (org-agenda-mark-header-line s))
3836 (while (setq d (pop day-numbers))
3837 (setq date (calendar-gregorian-from-absolute d)
3838 s (point))
3839 (if (or (setq todayp (= d today))
3840 (and (not start-pos) (= d sd)))
3841 (setq start-pos (point))
3842 (if (and start-pos (not end-pos))
3843 (setq end-pos (point))))
3844 (setq files thefiles
3845 rtnall nil)
3846 (while (setq file (pop files))
3847 (catch 'nextfile
3848 (org-check-agenda-file file)
3849 (let ((org-agenda-entry-types org-agenda-entry-types))
3850 (unless org-agenda-include-deadlines
3851 (setq org-agenda-entry-types
3852 (delq :deadline org-agenda-entry-types)))
3853 (cond
3854 ((memq org-agenda-show-log-scoped '(only clockcheck))
3855 (setq rtn (org-agenda-get-day-entries
3856 file date :closed)))
3857 (org-agenda-show-log-scoped
3858 (setq rtn (apply 'org-agenda-get-day-entries
3859 file date
3860 (append '(:closed) org-agenda-entry-types))))
3862 (setq rtn (apply 'org-agenda-get-day-entries
3863 file date
3864 org-agenda-entry-types)))))
3865 (setq rtnall (append rtnall rtn)))) ;; all entries
3866 (if org-agenda-include-diary
3867 (let ((org-agenda-search-headline-for-time t))
3868 (require 'diary-lib)
3869 (setq rtn (org-get-entries-from-diary date))
3870 (setq rtnall (append rtnall rtn))))
3871 (if (or rtnall org-agenda-show-all-dates)
3872 (progn
3873 (setq day-cnt (1+ day-cnt))
3874 (insert
3875 (if (stringp org-agenda-format-date)
3876 (format-time-string org-agenda-format-date
3877 (org-time-from-absolute date))
3878 (funcall org-agenda-format-date date))
3879 "\n")
3880 (put-text-property s (1- (point)) 'face
3881 (org-agenda-get-day-face date))
3882 (put-text-property s (1- (point)) 'org-date-line t)
3883 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3884 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3885 (when todayp
3886 (put-text-property s (1- (point)) 'org-today t))
3887 (if rtnall (insert ;; all entries
3888 (org-finalize-agenda-entries
3889 (org-agenda-add-time-grid-maybe
3890 rtnall ndays todayp))
3891 "\n"))
3892 (put-text-property s (1- (point)) 'day d)
3893 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3894 (when (and org-agenda-clockreport-mode clocktable-start)
3895 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3896 ;; the above line is to ensure the restricted range!
3897 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3898 tbl)
3899 (setq p (org-plist-delete p :block))
3900 (setq p (plist-put p :tstart clocktable-start))
3901 (setq p (plist-put p :tend clocktable-end))
3902 (setq p (plist-put p :scope 'agenda))
3903 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3904 (setq filter (or org-agenda-tag-filter-while-redo
3905 (get 'org-agenda-tag-filter :preset-filter))))
3906 (setq p (plist-put p :tags (mapconcat (lambda (x)
3907 (if (string-match "[<>=]" x)
3910 filter ""))))
3911 (setq tbl (apply 'org-get-clocktable p))
3912 (insert tbl)))
3913 (goto-char (point-min))
3914 (or org-agenda-multi (org-fit-agenda-window))
3915 (unless (and (pos-visible-in-window-p (point-min))
3916 (pos-visible-in-window-p (point-max)))
3917 (goto-char (1- (point-max)))
3918 (recenter -1)
3919 (if (not (pos-visible-in-window-p (or start-pos 1)))
3920 (progn
3921 (goto-char (or start-pos 1))
3922 (recenter 1))))
3923 (goto-char (or start-pos 1))
3924 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3925 (if (eq org-agenda-show-log-scoped 'clockcheck)
3926 (org-agenda-show-clocking-issues))
3927 (org-finalize-agenda)
3928 (setq buffer-read-only t)
3929 (message "")))
3931 (defun org-agenda-ndays-to-span (n)
3932 "Return a span symbol for a span of N days, or N if none matches."
3933 (cond ((symbolp n) n)
3934 ((= n 1) 'day)
3935 ((= n 7) 'week)
3936 (t n)))
3938 (defun org-agenda-span-to-ndays (span start-day)
3939 "Return ndays from SPAN starting at START-DAY."
3940 (cond ((numberp span) span)
3941 ((eq span 'day) 1)
3942 ((eq span 'week) 7)
3943 ((eq span 'month)
3944 (let ((date (calendar-gregorian-from-absolute start-day)))
3945 (calendar-last-day-of-month (car date) (caddr date))))
3946 ((eq span 'year)
3947 (let ((date (calendar-gregorian-from-absolute start-day)))
3948 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3950 (defun org-agenda-span-name (span)
3951 "Return a SPAN name."
3952 (if (null span)
3954 (if (symbolp span)
3955 (capitalize (symbol-name span))
3956 (format "%d days" span))))
3958 ;;; Agenda word search
3960 (defvar org-agenda-search-history nil)
3961 (defvar org-todo-only nil)
3963 (defvar org-search-syntax-table nil
3964 "Special syntax table for org-mode search.
3965 In this table, we have single quotes not as word constituents, to
3966 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3968 (defun org-search-syntax-table ()
3969 (unless org-search-syntax-table
3970 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3971 (modify-syntax-entry ?' "." org-search-syntax-table)
3972 (modify-syntax-entry ?` "." org-search-syntax-table))
3973 org-search-syntax-table)
3975 (defvar org-agenda-last-search-view-search-was-boolean nil)
3977 ;;;###autoload
3978 (defun org-search-view (&optional todo-only string edit-at)
3979 "Show all entries that contain a phrase or words or regular expressions.
3981 With optional prefix argument TODO-ONLY, only consider entries that are
3982 TODO entries. The argument STRING can be used to pass a default search
3983 string into this function. If EDIT-AT is non-nil, it means that the
3984 user should get a chance to edit this string, with cursor at position
3985 EDIT-AT.
3987 The search string can be viewed either as a phrase that should be found as
3988 is, or it can be broken into a number of snippets, each of which must match
3989 in a Boolean way to select an entry. The default depends on the variable
3990 `org-agenda-search-view-always-boolean'.
3991 Even if this is turned off (the default) you can always switch to
3992 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3994 The default is a direct search of the whole phrase, where each space in
3995 the search string can expand to an arbitrary amount of whitespace,
3996 including newlines.
3998 If using a Boolean search, the search string is split on whitespace and
3999 each snippet is searched separately, with logical AND to select an entry.
4000 Words prefixed with a minus must *not* occur in the entry. Words without
4001 a prefix or prefixed with a plus must occur in the entry. Matching is
4002 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4003 match whole words, not parts of a word) if
4004 `org-agenda-search-view-force-full-words' is set (default is nil).
4006 Boolean search snippets enclosed by curly braces are interpreted as
4007 regular expressions that must or (when preceded with \"-\") must not
4008 match in the entry. Snippets enclosed into double quotes will be taken
4009 as a whole, to include whitespace.
4011 - If the search string starts with an asterisk, search only in headlines.
4012 - If (possibly after the leading star) the search string starts with an
4013 exclamation mark, this also means to look at TODO entries only, an effect
4014 that can also be achieved with a prefix argument.
4015 - If (possibly after star and exclamation mark) the search string starts
4016 with a colon, this will mean that the (non-regexp) snippets of the
4017 Boolean search must match as full words.
4019 This command searches the agenda files, and in addition the files listed
4020 in `org-agenda-text-search-extra-files'."
4021 (interactive "P")
4022 (org-prepare-agenda "SEARCH")
4023 (org-compile-prefix-format 'search)
4024 (org-set-sorting-strategy 'search)
4025 (let* ((props (list 'face nil
4026 'done-face 'org-agenda-done
4027 'org-not-done-regexp org-not-done-regexp
4028 'org-todo-regexp org-todo-regexp
4029 'org-complex-heading-regexp org-complex-heading-regexp
4030 'mouse-face 'highlight
4031 'help-echo (format "mouse-2 or RET jump to location")))
4032 (full-words org-agenda-search-view-force-full-words)
4033 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4034 regexp rtn rtnall files file pos
4035 marker category org-category-pos tags c neg re boolean
4036 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4037 (unless (and (not edit-at)
4038 (stringp string)
4039 (string-match "\\S-" string))
4040 (setq string (read-string
4041 (if org-agenda-search-view-always-boolean
4042 "[+-]Word/{Regexp} ...: "
4043 "Phrase, or [+-]Word/{Regexp} ...: ")
4044 (cond
4045 ((integerp edit-at) (cons string edit-at))
4046 (edit-at string))
4047 'org-agenda-search-history)))
4048 (org-set-local 'org-todo-only todo-only)
4049 (setq org-agenda-redo-command
4050 (list 'org-search-view (if todo-only t nil) string
4051 '(if current-prefix-arg 1 nil)))
4052 (setq org-agenda-query-string string)
4054 (if (equal (string-to-char string) ?*)
4055 (setq hdl-only t
4056 words (substring string 1))
4057 (setq words string))
4058 (when (equal (string-to-char words) ?!)
4059 (setq todo-only t
4060 words (substring words 1)))
4061 (when (equal (string-to-char words) ?:)
4062 (setq full-words t
4063 words (substring words 1)))
4064 (if (or org-agenda-search-view-always-boolean
4065 (member (string-to-char words) '(?- ?+ ?\{)))
4066 (setq boolean t))
4067 (setq words (org-split-string words))
4068 (let (www w)
4069 (while (setq w (pop words))
4070 (while (and (string-match "\\\\\\'" w) words)
4071 (setq w (concat (substring w 0 -1) " " (pop words))))
4072 (push w www))
4073 (setq words (nreverse www) www nil)
4074 (while (setq w (pop words))
4075 (when (and (string-match "\\`[-+]?{" w)
4076 (not (string-match "}\\'" w)))
4077 (while (and words (not (string-match "}\\'" (car words))))
4078 (setq w (concat w " " (pop words))))
4079 (setq w (concat w " " (pop words))))
4080 (push w www))
4081 (setq words (nreverse www)))
4082 (setq org-agenda-last-search-view-search-was-boolean boolean)
4083 (when boolean
4084 (let (wds w)
4085 (while (setq w (pop words))
4086 (if (or (equal (substring w 0 1) "\"")
4087 (and (> (length w) 1)
4088 (member (substring w 0 1) '("+" "-"))
4089 (equal (substring w 1 2) "\"")))
4090 (while (and words (not (equal (substring w -1) "\"")))
4091 (setq w (concat w " " (pop words)))))
4092 (and (string-match "\\`\\([-+]?\\)\"" w)
4093 (setq w (replace-match "\\1" nil nil w)))
4094 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4095 (push w wds))
4096 (setq words (nreverse wds))))
4097 (if boolean
4098 (mapc (lambda (w)
4099 (setq c (string-to-char w))
4100 (if (equal c ?-)
4101 (setq neg t w (substring w 1))
4102 (if (equal c ?+)
4103 (setq neg nil w (substring w 1))
4104 (setq neg nil)))
4105 (if (string-match "\\`{.*}\\'" w)
4106 (setq re (substring w 1 -1))
4107 (if full-words
4108 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4109 (setq re (regexp-quote (downcase w)))))
4110 (if neg (push re regexps-) (push re regexps+)))
4111 words)
4112 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4113 regexps+))
4114 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4115 (if (not regexps+)
4116 (setq regexp org-outline-regexp-bol)
4117 (setq regexp (pop regexps+))
4118 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4119 regexp))))
4120 (setq files (org-agenda-files nil 'ifmode))
4121 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4122 (pop org-agenda-text-search-extra-files)
4123 (setq files (org-add-archive-files files)))
4124 (setq files (append files org-agenda-text-search-extra-files)
4125 rtnall nil)
4126 (while (setq file (pop files))
4127 (setq ee nil)
4128 (catch 'nextfile
4129 (org-check-agenda-file file)
4130 (setq buffer (if (file-exists-p file)
4131 (org-get-agenda-file-buffer file)
4132 (error "No such file %s" file)))
4133 (if (not buffer)
4134 ;; If file does not exist, make sure an error message is sent
4135 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4136 file))))
4137 (with-current-buffer buffer
4138 (with-syntax-table (org-search-syntax-table)
4139 (unless (derived-mode-p 'org-mode)
4140 (error "Agenda file %s is not in `org-mode'" file))
4141 (let ((case-fold-search t))
4142 (save-excursion
4143 (save-restriction
4144 (if org-agenda-restrict
4145 (narrow-to-region org-agenda-restrict-begin
4146 org-agenda-restrict-end)
4147 (widen))
4148 (goto-char (point-min))
4149 (unless (or (org-at-heading-p)
4150 (outline-next-heading))
4151 (throw 'nextfile t))
4152 (goto-char (max (point-min) (1- (point))))
4153 (while (re-search-forward regexp nil t)
4154 (org-back-to-heading t)
4155 (skip-chars-forward "* ")
4156 (setq beg (point-at-bol)
4157 beg1 (point)
4158 end (progn (outline-next-heading) (point)))
4159 (catch :skip
4160 (goto-char beg)
4161 (org-agenda-skip)
4162 (setq str (buffer-substring-no-properties
4163 (point-at-bol)
4164 (if hdl-only (point-at-eol) end)))
4165 (mapc (lambda (wr) (when (string-match wr str)
4166 (goto-char (1- end))
4167 (throw :skip t)))
4168 regexps-)
4169 (mapc (lambda (wr) (unless (string-match wr str)
4170 (goto-char (1- end))
4171 (throw :skip t)))
4172 (if todo-only
4173 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4174 regexps+)
4175 regexps+))
4176 (goto-char beg)
4177 (setq marker (org-agenda-new-marker (point))
4178 category (org-get-category)
4179 org-category-pos (get-text-property (point) 'org-category-position)
4180 tags (org-get-tags-at (point))
4181 txt (org-agenda-format-item
4183 (buffer-substring-no-properties
4184 beg1 (point-at-eol))
4185 category tags))
4186 (org-add-props txt props
4187 'org-marker marker 'org-hd-marker marker
4188 'org-todo-regexp org-todo-regexp
4189 'org-complex-heading-regexp org-complex-heading-regexp
4190 'priority 1000 'org-category category
4191 'org-category-position org-category-pos
4192 'type "search")
4193 (push txt ee)
4194 (goto-char (1- end))))))))))
4195 (setq rtn (nreverse ee))
4196 (setq rtnall (append rtnall rtn)))
4197 (if org-agenda-overriding-header
4198 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4199 nil 'face 'org-agenda-structure) "\n")
4200 (insert "Search words: ")
4201 (add-text-properties (point-min) (1- (point))
4202 (list 'face 'org-agenda-structure))
4203 (setq pos (point))
4204 (insert string "\n")
4205 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4206 (setq pos (point))
4207 (unless org-agenda-multi
4208 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4209 (add-text-properties pos (1- (point))
4210 (list 'face 'org-agenda-structure))))
4211 (org-agenda-mark-header-line (point-min))
4212 (when rtnall
4213 (insert (org-finalize-agenda-entries rtnall) "\n"))
4214 (goto-char (point-min))
4215 (or org-agenda-multi (org-fit-agenda-window))
4216 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
4217 (org-finalize-agenda)
4218 (setq buffer-read-only t)))
4220 ;;; Agenda TODO list
4222 (defvar org-select-this-todo-keyword nil)
4223 (defvar org-last-arg nil)
4225 ;;;###autoload
4226 (defun org-todo-list (arg)
4227 "Show all (not done) TODO entries from all agenda file in a single list.
4228 The prefix arg can be used to select a specific TODO keyword and limit
4229 the list to these. When using \\[universal-argument], you will be prompted
4230 for a keyword. A numeric prefix directly selects the Nth keyword in
4231 `org-todo-keywords-1'."
4232 (interactive "P")
4233 (org-prepare-agenda "TODO")
4234 (org-compile-prefix-format 'todo)
4235 (org-set-sorting-strategy 'todo)
4236 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4237 (let* ((today (org-today))
4238 (date (calendar-gregorian-from-absolute today))
4239 (kwds org-todo-keywords-for-agenda)
4240 (completion-ignore-case t)
4241 (org-select-this-todo-keyword
4242 (if (stringp arg) arg
4243 (and arg (integerp arg) (> arg 0)
4244 (nth (1- arg) kwds))))
4245 rtn rtnall files file pos)
4246 (when (equal arg '(4))
4247 (setq org-select-this-todo-keyword
4248 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4249 (mapcar 'list kwds) nil nil)))
4250 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4251 (org-set-local 'org-last-arg arg)
4252 (setq org-agenda-redo-command
4253 '(org-todo-list (or current-prefix-arg org-last-arg)))
4254 (setq files (org-agenda-files nil 'ifmode)
4255 rtnall nil)
4256 (while (setq file (pop files))
4257 (catch 'nextfile
4258 (org-check-agenda-file file)
4259 (setq rtn (org-agenda-get-day-entries file date :todo))
4260 (setq rtnall (append rtnall rtn))))
4261 (if org-agenda-overriding-header
4262 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4263 nil 'face 'org-agenda-structure) "\n")
4264 (insert "Global list of TODO items of type: ")
4265 (add-text-properties (point-min) (1- (point))
4266 (list 'face 'org-agenda-structure
4267 'short-heading
4268 (concat "ToDo: "
4269 (or org-select-this-todo-keyword "ALL"))))
4270 (org-agenda-mark-header-line (point-min))
4271 (setq pos (point))
4272 (insert (or org-select-this-todo-keyword "ALL") "\n")
4273 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4274 (setq pos (point))
4275 (unless org-agenda-multi
4276 (insert "Available with `N r': (0)ALL")
4277 (let ((n 0) s)
4278 (mapc (lambda (x)
4279 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4280 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4281 (insert "\n "))
4282 (insert " " s))
4283 kwds))
4284 (insert "\n"))
4285 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4286 (org-agenda-mark-header-line (point-min))
4287 (when rtnall
4288 (insert (org-finalize-agenda-entries rtnall) "\n"))
4289 (goto-char (point-min))
4290 (or org-agenda-multi (org-fit-agenda-window))
4291 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4292 (org-finalize-agenda)
4293 (setq buffer-read-only t)))
4295 ;;; Agenda tags match
4297 ;;;###autoload
4298 (defun org-tags-view (&optional todo-only match)
4299 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4300 The prefix arg TODO-ONLY limits the search to TODO entries."
4301 (interactive "P")
4302 (let* ((org-tags-match-list-sublevels
4303 org-tags-match-list-sublevels)
4304 (completion-ignore-case t)
4305 rtn rtnall files file pos matcher
4306 buffer)
4307 (when (and (stringp match) (not (string-match "\\S-" match)))
4308 (setq match nil))
4309 (setq matcher (org-make-tags-matcher match)
4310 match (car matcher) matcher (cdr matcher))
4311 (org-prepare-agenda (concat "TAGS " match))
4312 (org-compile-prefix-format 'tags)
4313 (org-set-sorting-strategy 'tags)
4314 (setq org-agenda-query-string match)
4315 (setq org-agenda-redo-command
4316 (list 'org-tags-view (list 'quote todo-only)
4317 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4318 (setq files (org-agenda-files nil 'ifmode)
4319 rtnall nil)
4320 (while (setq file (pop files))
4321 (catch 'nextfile
4322 (org-check-agenda-file file)
4323 (setq buffer (if (file-exists-p file)
4324 (org-get-agenda-file-buffer file)
4325 (error "No such file %s" file)))
4326 (if (not buffer)
4327 ;; If file does not exist, error message to agenda
4328 (setq rtn (list
4329 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4330 rtnall (append rtnall rtn))
4331 (with-current-buffer buffer
4332 (unless (derived-mode-p 'org-mode)
4333 (error "Agenda file %s is not in `org-mode'" file))
4334 (save-excursion
4335 (save-restriction
4336 (if org-agenda-restrict
4337 (narrow-to-region org-agenda-restrict-begin
4338 org-agenda-restrict-end)
4339 (widen))
4340 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4341 (setq rtnall (append rtnall rtn))))))))
4342 (if org-agenda-overriding-header
4343 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4344 nil 'face 'org-agenda-structure) "\n")
4345 (insert "Headlines with TAGS match: ")
4346 (add-text-properties (point-min) (1- (point))
4347 (list 'face 'org-agenda-structure
4348 'short-heading
4349 (concat "Match: " match)))
4350 (setq pos (point))
4351 (insert match "\n")
4352 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4353 (setq pos (point))
4354 (unless org-agenda-multi
4355 (insert "Press `C-u r' to search again with new search string\n"))
4356 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4357 (org-agenda-mark-header-line (point-min))
4358 (when rtnall
4359 (insert (org-finalize-agenda-entries rtnall) "\n"))
4360 (goto-char (point-min))
4361 (or org-agenda-multi (org-fit-agenda-window))
4362 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4363 (org-finalize-agenda)
4364 (setq buffer-read-only t)))
4366 ;;; Agenda Finding stuck projects
4368 (defvar org-agenda-skip-regexp nil
4369 "Regular expression used in skipping subtrees for the agenda.
4370 This is basically a temporary global variable that can be set and then
4371 used by user-defined selections using `org-agenda-skip-function'.")
4373 (defvar org-agenda-overriding-header nil
4374 "When set during agenda, todo and tags searches it replaces the header.
4375 This variable should not be set directly, but custom commands can bind it
4376 in the options section.")
4378 (defun org-agenda-skip-entry-when-regexp-matches ()
4379 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4380 If yes, it returns the end position of this entry, causing agenda commands
4381 to skip the entry but continuing the search in the subtree. This is a
4382 function that can be put into `org-agenda-skip-function' for the duration
4383 of a command."
4384 (let ((end (save-excursion (org-end-of-subtree t)))
4385 skip)
4386 (save-excursion
4387 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4388 (and skip end)))
4390 (defun org-agenda-skip-subtree-when-regexp-matches ()
4391 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4392 If yes, it returns the end position of this tree, causing agenda commands
4393 to skip this subtree. This is a function that can be put into
4394 `org-agenda-skip-function' for the duration of a command."
4395 (let ((end (save-excursion (org-end-of-subtree t)))
4396 skip)
4397 (save-excursion
4398 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4399 (and skip end)))
4401 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4402 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4403 If yes, it returns the end position of the current entry (NOT the tree),
4404 causing agenda commands to skip the entry but continuing the search in
4405 the subtree. This is a function that can be put into
4406 `org-agenda-skip-function' for the duration of a command. An important
4407 use of this function is for the stuck project list."
4408 (let ((end (save-excursion (org-end-of-subtree t)))
4409 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4410 skip)
4411 (save-excursion
4412 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4413 (and skip entry-end)))
4415 (defun org-agenda-skip-entry-if (&rest conditions)
4416 "Skip entry if any of CONDITIONS is true.
4417 See `org-agenda-skip-if' for details."
4418 (org-agenda-skip-if nil conditions))
4420 (defun org-agenda-skip-subtree-if (&rest conditions)
4421 "Skip entry if any of CONDITIONS is true.
4422 See `org-agenda-skip-if' for details."
4423 (org-agenda-skip-if t conditions))
4425 (defun org-agenda-skip-if (subtree conditions)
4426 "Checks current entity for CONDITIONS.
4427 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4428 the entry (i.e. the text before the next heading) is checked.
4430 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4431 from different tests. Valid conditions are:
4433 scheduled Check if there is a scheduled cookie
4434 notscheduled Check if there is no scheduled cookie
4435 deadline Check if there is a deadline
4436 notdeadline Check if there is no deadline
4437 timestamp Check if there is a timestamp (also deadline or scheduled)
4438 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4439 regexp Check if regexp matches
4440 notregexp Check if regexp does not match.
4441 todo Check if TODO keyword matches
4442 nottodo Check if TODO keyword does not match
4444 The regexp is taken from the conditions list, it must come right after
4445 the `regexp' or `notregexp' element.
4447 `todo' and `nottodo' accept as an argument a list of todo
4448 keywords, which may include \"*\" to match any todo keyword.
4450 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4452 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4454 Instead of a list, a keyword class may be given. For example:
4456 (org-agenda-skip-entry-if 'nottodo 'done)
4458 would skip entries that haven't been marked with any of \"DONE\"
4459 keywords. Possible classes are: `todo', `done', `any'.
4461 If any of these conditions is met, this function returns the end point of
4462 the entity, causing the search to continue from there. This is a function
4463 that can be put into `org-agenda-skip-function' for the duration of a command."
4464 (let (beg end m)
4465 (org-back-to-heading t)
4466 (setq beg (point)
4467 end (if subtree
4468 (progn (org-end-of-subtree t) (point))
4469 (progn (outline-next-heading) (1- (point)))))
4470 (goto-char beg)
4471 (and
4473 (and (memq 'scheduled conditions)
4474 (re-search-forward org-scheduled-time-regexp end t))
4475 (and (memq 'notscheduled conditions)
4476 (not (re-search-forward org-scheduled-time-regexp end t)))
4477 (and (memq 'deadline conditions)
4478 (re-search-forward org-deadline-time-regexp end t))
4479 (and (memq 'notdeadline conditions)
4480 (not (re-search-forward org-deadline-time-regexp end t)))
4481 (and (memq 'timestamp conditions)
4482 (re-search-forward org-ts-regexp end t))
4483 (and (memq 'nottimestamp conditions)
4484 (not (re-search-forward org-ts-regexp end t)))
4485 (and (setq m (memq 'regexp conditions))
4486 (stringp (nth 1 m))
4487 (re-search-forward (nth 1 m) end t))
4488 (and (setq m (memq 'notregexp conditions))
4489 (stringp (nth 1 m))
4490 (not (re-search-forward (nth 1 m) end t)))
4491 (and (or
4492 (setq m (memq 'nottodo conditions))
4493 (setq m (memq 'todo-unblocked conditions))
4494 (setq m (memq 'nottodo-unblocked conditions))
4495 (setq m (memq 'todo conditions)))
4496 (org-agenda-skip-if-todo m end)))
4497 end)))
4499 (defun org-agenda-skip-if-todo (args end)
4500 "Helper function for `org-agenda-skip-if', do not use it directly.
4501 ARGS is a list with first element either `todo', `nottodo',
4502 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4503 a list of TODO keywords, or a state symbol `todo' or `done' or
4504 `any'."
4505 (let ((kw (car args))
4506 (arg (cadr args))
4507 todo-wds todo-re)
4508 (setq todo-wds
4509 (org-uniquify
4510 (cond
4511 ((listp arg) ;; list of keywords
4512 (if (member "*" arg)
4513 (mapcar 'substring-no-properties org-todo-keywords-1)
4514 arg))
4515 ((symbolp arg) ;; keyword class name
4516 (cond
4517 ((eq arg 'todo)
4518 (org-delete-all org-done-keywords
4519 (mapcar 'substring-no-properties
4520 org-todo-keywords-1)))
4521 ((eq arg 'done) org-done-keywords)
4522 ((eq arg 'any)
4523 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4524 (setq todo-re
4525 (concat "^\\*+[ \t]+\\<\\("
4526 (mapconcat 'identity todo-wds "\\|")
4527 "\\)\\>"))
4528 (cond
4529 ((eq kw 'todo) (re-search-forward todo-re end t))
4530 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4531 ((eq kw 'todo-unblocked)
4532 (catch 'unblocked
4533 (while (re-search-forward todo-re end t)
4534 (or (org-entry-blocked-p) (throw 'unblocked t)))
4535 nil))
4536 ((eq kw 'nottodo-unblocked)
4537 (catch 'unblocked
4538 (while (re-search-forward todo-re end t)
4539 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4543 ;;;###autoload
4544 (defun org-agenda-list-stuck-projects (&rest ignore)
4545 "Create agenda view for projects that are stuck.
4546 Stuck projects are project that have no next actions. For the definitions
4547 of what a project is and how to check if it stuck, customize the variable
4548 `org-stuck-projects'."
4549 (interactive)
4550 (let* ((org-agenda-skip-function
4551 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4552 ;; We could have used org-agenda-skip-if here.
4553 (org-agenda-overriding-header
4554 (or org-agenda-overriding-header "List of stuck projects: "))
4555 (matcher (nth 0 org-stuck-projects))
4556 (todo (nth 1 org-stuck-projects))
4557 (todo-wds (if (member "*" todo)
4558 (progn
4559 (org-prepare-agenda-buffers (org-agenda-files
4560 nil 'ifmode))
4561 (org-delete-all
4562 org-done-keywords-for-agenda
4563 (copy-sequence org-todo-keywords-for-agenda)))
4564 todo))
4565 (todo-re (concat "^\\*+[ \t]+\\("
4566 (mapconcat 'identity todo-wds "\\|")
4567 "\\)\\>"))
4568 (tags (nth 2 org-stuck-projects))
4569 (tags-re (if (member "*" tags)
4570 (concat org-outline-regexp-bol
4571 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4572 (if tags
4573 (concat org-outline-regexp-bol
4574 ".*:\\("
4575 (mapconcat 'identity tags "\\|")
4576 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4577 (gen-re (nth 3 org-stuck-projects))
4578 (re-list
4579 (delq nil
4580 (list
4581 (if todo todo-re)
4582 (if tags tags-re)
4583 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4584 gen-re)))))
4585 (setq org-agenda-skip-regexp
4586 (if re-list
4587 (mapconcat 'identity re-list "\\|")
4588 (error "No information how to identify unstuck projects")))
4589 (org-tags-view nil matcher)
4590 (with-current-buffer org-agenda-buffer-name
4591 (setq org-agenda-redo-command
4592 '(org-agenda-list-stuck-projects
4593 (or current-prefix-arg org-last-arg))))))
4595 ;;; Diary integration
4597 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4598 (defvar diary-list-entries-hook)
4599 (defvar diary-time-regexp)
4600 (defun org-get-entries-from-diary (date)
4601 "Get the (Emacs Calendar) diary entries for DATE."
4602 (require 'diary-lib)
4603 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4604 (diary-display-hook '(fancy-diary-display))
4605 (diary-display-function 'fancy-diary-display)
4606 (pop-up-frames nil)
4607 (diary-list-entries-hook
4608 (cons 'org-diary-default-entry diary-list-entries-hook))
4609 (diary-file-name-prefix-function nil) ; turn this feature off
4610 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4611 entries
4612 (org-disable-agenda-to-diary t))
4613 (save-excursion
4614 (save-window-excursion
4615 (funcall (if (fboundp 'diary-list-entries)
4616 'diary-list-entries 'list-diary-entries)
4617 date 1)))
4618 (if (not (get-buffer diary-fancy-buffer))
4619 (setq entries nil)
4620 (with-current-buffer diary-fancy-buffer
4621 (setq buffer-read-only nil)
4622 (if (zerop (buffer-size))
4623 ;; No entries
4624 (setq entries nil)
4625 ;; Omit the date and other unnecessary stuff
4626 (org-agenda-cleanup-fancy-diary)
4627 ;; Add prefix to each line and extend the text properties
4628 (if (zerop (buffer-size))
4629 (setq entries nil)
4630 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4631 (setq entries
4632 (with-temp-buffer
4633 (insert entries) (goto-char (point-min))
4634 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4635 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4636 (replace-match (concat "; " (match-string 1)))))
4637 (buffer-string)))))
4638 (set-buffer-modified-p nil)
4639 (kill-buffer diary-fancy-buffer)))
4640 (when entries
4641 (setq entries (org-split-string entries "\n"))
4642 (setq entries
4643 (mapcar
4644 (lambda (x)
4645 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4646 ;; Extend the text properties to the beginning of the line
4647 (org-add-props x (text-properties-at (1- (length x)) x)
4648 'type "diary" 'date date 'face 'org-agenda-diary))
4649 entries)))))
4651 (defvar org-agenda-cleanup-fancy-diary-hook nil
4652 "Hook run when the fancy diary buffer is cleaned up.")
4654 (defun org-agenda-cleanup-fancy-diary ()
4655 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4656 This gets rid of the date, the underline under the date, and
4657 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4658 date. It also removes lines that contain only whitespace."
4659 (goto-char (point-min))
4660 (if (looking-at ".*?:[ \t]*")
4661 (progn
4662 (replace-match "")
4663 (re-search-forward "\n=+$" nil t)
4664 (replace-match "")
4665 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4666 (re-search-forward "\n=+$" nil t)
4667 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4668 (goto-char (point-min))
4669 (while (re-search-forward "^ +\n" nil t)
4670 (replace-match ""))
4671 (goto-char (point-min))
4672 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4673 (replace-match ""))
4674 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4676 ;; Make sure entries from the diary have the right text properties.
4677 (eval-after-load "diary-lib"
4678 '(if (boundp 'diary-modify-entry-list-string-function)
4679 ;; We can rely on the hook, nothing to do
4681 ;; Hook not available, must use advice to make this work
4682 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4683 "Make the position visible."
4684 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4685 (stringp string)
4686 buffer-file-name)
4687 (setq string (org-modify-diary-entry-string string))))))
4689 (defun org-modify-diary-entry-string (string)
4690 "Add text properties to string, allowing org-mode to act on it."
4691 (org-add-props string nil
4692 'mouse-face 'highlight
4693 'help-echo (if buffer-file-name
4694 (format "mouse-2 or RET jump to diary file %s"
4695 (abbreviate-file-name buffer-file-name))
4697 'org-agenda-diary-link t
4698 'org-marker (org-agenda-new-marker (point-at-bol))))
4700 (defun org-diary-default-entry ()
4701 "Add a dummy entry to the diary.
4702 Needed to avoid empty dates which mess up holiday display."
4703 ;; Catch the error if dealing with the new add-to-diary-alist
4704 (when org-disable-agenda-to-diary
4705 (condition-case nil
4706 (org-add-to-diary-list original-date "Org-mode dummy" "")
4707 (error
4708 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4710 (defun org-add-to-diary-list (&rest args)
4711 (if (fboundp 'diary-add-to-list)
4712 (apply 'diary-add-to-list args)
4713 (apply 'add-to-diary-list args)))
4715 (defvar org-diary-last-run-time nil)
4717 ;;;###autoload
4718 (defun org-diary (&rest args)
4719 "Return diary information from org-files.
4720 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4721 It accesses org files and extracts information from those files to be
4722 listed in the diary. The function accepts arguments specifying what
4723 items should be listed. For a list of arguments allowed here, see the
4724 variable `org-agenda-entry-types'.
4726 The call in the diary file should look like this:
4728 &%%(org-diary) ~/path/to/some/orgfile.org
4730 Use a separate line for each org file to check. Or, if you omit the file name,
4731 all files listed in `org-agenda-files' will be checked automatically:
4733 &%%(org-diary)
4735 If you don't give any arguments (as in the example above), the default
4736 arguments (:deadline :scheduled :timestamp :sexp) are used.
4737 So the example above may also be written as
4739 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4741 The function expects the lisp variables `entry' and `date' to be provided
4742 by the caller, because this is how the calendar works. Don't use this
4743 function from a program - use `org-agenda-get-day-entries' instead."
4744 (when (> (- (org-float-time)
4745 org-agenda-last-marker-time)
4747 ;; I am not sure if this works with sticky agendas, because the marker
4748 ;; list is then no longer a global variable.
4749 (org-agenda-reset-markers))
4750 ;; Prevent `org-compile-prefix-format' to fail when there is no agenda
4751 (when (buffer-live-p org-agenda-buffer)
4752 (org-compile-prefix-format 'agenda))
4753 (org-set-sorting-strategy 'agenda)
4754 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4755 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4756 (list entry)
4757 (org-agenda-files t)))
4758 (time (org-float-time))
4759 file rtn results)
4760 (when (or (not org-diary-last-run-time)
4761 (> (- time
4762 org-diary-last-run-time)
4764 (org-prepare-agenda-buffers files))
4765 (setq org-diary-last-run-time time)
4766 ;; If this is called during org-agenda, don't return any entries to
4767 ;; the calendar. Org Agenda will list these entries itself.
4768 (if org-disable-agenda-to-diary (setq files nil))
4769 (while (setq file (pop files))
4770 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4771 (setq results (append results rtn)))
4772 (if results
4773 (concat (org-finalize-agenda-entries results) "\n"))))
4775 ;;; Agenda entry finders
4777 (defun org-agenda-get-day-entries (file date &rest args)
4778 "Does the work for `org-diary' and `org-agenda'.
4779 FILE is the path to a file to be checked for entries. DATE is date like
4780 the one returned by `calendar-current-date'. ARGS are symbols indicating
4781 which kind of entries should be extracted. For details about these, see
4782 the documentation of `org-diary'."
4783 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4784 (let* ((org-startup-folded nil)
4785 (org-startup-align-all-tables nil)
4786 (buffer (if (file-exists-p file)
4787 (org-get-agenda-file-buffer file)
4788 (error "No such file %s" file)))
4789 arg results rtn deadline-results)
4790 (if (not buffer)
4791 ;; If file does not exist, make sure an error message ends up in diary
4792 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4793 (with-current-buffer buffer
4794 (unless (derived-mode-p 'org-mode)
4795 (error "Agenda file %s is not in `org-mode'" file))
4796 (let ((case-fold-search nil))
4797 (save-excursion
4798 (save-restriction
4799 (if org-agenda-restrict
4800 (narrow-to-region org-agenda-restrict-begin
4801 org-agenda-restrict-end)
4802 (widen))
4803 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4804 (while (setq arg (pop args))
4805 (cond
4806 ((and (eq arg :todo)
4807 (equal date (calendar-gregorian-from-absolute
4808 (org-today))))
4809 (setq rtn (org-agenda-get-todos))
4810 (setq results (append results rtn)))
4811 ((eq arg :timestamp)
4812 (setq rtn (org-agenda-get-blocks))
4813 (setq results (append results rtn))
4814 (setq rtn (org-agenda-get-timestamps deadline-results))
4815 (setq results (append results rtn)))
4816 ((eq arg :sexp)
4817 (setq rtn (org-agenda-get-sexps))
4818 (setq results (append results rtn)))
4819 ((eq arg :scheduled)
4820 (setq rtn (org-agenda-get-scheduled deadline-results))
4821 (setq results (append results rtn)))
4822 ((eq arg :closed)
4823 (setq rtn (org-agenda-get-progress))
4824 (setq results (append results rtn)))
4825 ((eq arg :deadline)
4826 (setq rtn (org-agenda-get-deadlines))
4827 (setq deadline-results (copy-sequence rtn))
4828 (setq results (append results rtn))))))))
4829 results))))
4831 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4832 (defun org-agenda-get-todos ()
4833 "Return the TODO information for agenda display."
4834 (let* ((props (list 'face nil
4835 'done-face 'org-agenda-done
4836 'org-not-done-regexp org-not-done-regexp
4837 'org-todo-regexp org-todo-regexp
4838 'org-complex-heading-regexp org-complex-heading-regexp
4839 'mouse-face 'highlight
4840 'help-echo
4841 (format "mouse-2 or RET jump to org file %s"
4842 (abbreviate-file-name buffer-file-name))))
4843 (regexp (format org-heading-keyword-regexp-format
4844 (cond
4845 ((and org-select-this-todo-keyword
4846 (equal org-select-this-todo-keyword "*"))
4847 org-todo-regexp)
4848 (org-select-this-todo-keyword
4849 (concat "\\("
4850 (mapconcat 'identity
4851 (org-split-string
4852 org-select-this-todo-keyword
4853 "|")
4854 "\\|") "\\)"))
4855 (t org-not-done-regexp))))
4856 marker priority category org-category-pos tags todo-state
4857 ee txt beg end)
4858 (goto-char (point-min))
4859 (while (re-search-forward regexp nil t)
4860 (catch :skip
4861 (save-match-data
4862 (beginning-of-line)
4863 (org-agenda-skip)
4864 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4865 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4866 (goto-char (1+ beg))
4867 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4868 (throw :skip nil)))
4869 (goto-char (match-beginning 2))
4870 (setq marker (org-agenda-new-marker (match-beginning 0))
4871 category (org-get-category)
4872 org-category-pos (get-text-property (point) 'org-category-position)
4873 txt (org-trim
4874 (buffer-substring (match-beginning 2) (match-end 0)))
4875 tags (org-get-tags-at (point))
4876 txt (org-agenda-format-item "" txt category tags)
4877 priority (1+ (org-get-priority txt))
4878 todo-state (org-get-todo-state))
4879 (org-add-props txt props
4880 'org-marker marker 'org-hd-marker marker
4881 'priority priority 'org-category category
4882 'org-category-position org-category-pos
4883 'type "todo" 'todo-state todo-state)
4884 (push txt ee)
4885 (if org-agenda-todo-list-sublevels
4886 (goto-char (match-end 2))
4887 (org-end-of-subtree 'invisible))))
4888 (nreverse ee)))
4890 (defun org-agenda-todo-custom-ignore-p (time n)
4891 "Check whether timestamp is farther away then n number of days.
4892 This function is invoked if `org-agenda-todo-ignore-deadlines',
4893 `org-agenda-todo-ignore-scheduled' or
4894 `org-agenda-todo-ignore-timestamp' is set to an integer."
4895 (let ((days (org-days-to-time time)))
4896 (if (>= n 0)
4897 (>= days n)
4898 (<= days n))))
4900 ;;;###autoload
4901 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4902 (&optional end)
4903 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4904 (when (or org-agenda-todo-ignore-with-date
4905 org-agenda-todo-ignore-scheduled
4906 org-agenda-todo-ignore-deadlines
4907 org-agenda-todo-ignore-timestamp)
4908 (setq end (or end (save-excursion (outline-next-heading) (point))))
4909 (save-excursion
4910 (or (and org-agenda-todo-ignore-with-date
4911 (re-search-forward org-ts-regexp end t))
4912 (and org-agenda-todo-ignore-scheduled
4913 (re-search-forward org-scheduled-time-regexp end t)
4914 (cond
4915 ((eq org-agenda-todo-ignore-scheduled 'future)
4916 (> (org-days-to-time (match-string 1)) 0))
4917 ((eq org-agenda-todo-ignore-scheduled 'past)
4918 (<= (org-days-to-time (match-string 1)) 0))
4919 ((numberp org-agenda-todo-ignore-scheduled)
4920 (org-agenda-todo-custom-ignore-p
4921 (match-string 1) org-agenda-todo-ignore-scheduled))
4922 (t)))
4923 (and org-agenda-todo-ignore-deadlines
4924 (re-search-forward org-deadline-time-regexp end t)
4925 (cond
4926 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4927 ((eq org-agenda-todo-ignore-deadlines 'far)
4928 (not (org-deadline-close (match-string 1))))
4929 ((eq org-agenda-todo-ignore-deadlines 'future)
4930 (> (org-days-to-time (match-string 1)) 0))
4931 ((eq org-agenda-todo-ignore-deadlines 'past)
4932 (<= (org-days-to-time (match-string 1)) 0))
4933 ((numberp org-agenda-todo-ignore-deadlines)
4934 (org-agenda-todo-custom-ignore-p
4935 (match-string 1) org-agenda-todo-ignore-deadlines))
4936 (t (org-deadline-close (match-string 1)))))
4937 (and org-agenda-todo-ignore-timestamp
4938 (let ((buffer (current-buffer))
4939 (regexp
4940 (concat
4941 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4942 (start (point)))
4943 ;; Copy current buffer into a temporary one
4944 (with-temp-buffer
4945 (insert-buffer-substring buffer start end)
4946 (goto-char (point-min))
4947 ;; Delete SCHEDULED and DEADLINE items
4948 (while (re-search-forward regexp end t)
4949 (delete-region (match-beginning 0) (match-end 0)))
4950 (goto-char (point-min))
4951 ;; No search for timestamp left
4952 (when (re-search-forward org-ts-regexp nil t)
4953 (cond
4954 ((eq org-agenda-todo-ignore-timestamp 'future)
4955 (> (org-days-to-time (match-string 1)) 0))
4956 ((eq org-agenda-todo-ignore-timestamp 'past)
4957 (<= (org-days-to-time (match-string 1)) 0))
4958 ((numberp org-agenda-todo-ignore-timestamp)
4959 (org-agenda-todo-custom-ignore-p
4960 (match-string 1) org-agenda-todo-ignore-timestamp))
4961 (t))))))))))
4963 (defconst org-agenda-no-heading-message
4964 "No heading for this item in buffer or region.")
4966 (defun org-agenda-get-timestamps (&optional deadline-results)
4967 "Return the date stamp information for agenda display."
4968 (let* ((props (list 'face 'org-agenda-calendar-event
4969 'org-not-done-regexp org-not-done-regexp
4970 'org-todo-regexp org-todo-regexp
4971 'org-complex-heading-regexp org-complex-heading-regexp
4972 'mouse-face 'highlight
4973 'help-echo
4974 (format "mouse-2 or RET jump to org file %s"
4975 (abbreviate-file-name buffer-file-name))))
4976 (d1 (calendar-absolute-from-gregorian date))
4978 (deadline-position-alist
4979 (mapcar (lambda (a) (and (setq mm (get-text-property
4980 0 'org-hd-marker a))
4981 (cons (marker-position mm) a)))
4982 deadline-results))
4983 (remove-re
4984 (concat
4985 (regexp-quote
4986 (format-time-string
4987 "<%Y-%m-%d"
4988 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4989 ".*?>"))
4990 (regexp
4991 (concat
4992 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4993 (regexp-quote
4994 (substring
4995 (format-time-string
4996 (car org-time-stamp-formats)
4997 (apply 'encode-time ; DATE bound by calendar
4998 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4999 1 11))
5000 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5001 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5002 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5003 donep tmp priority category org-category-pos ee txt timestr tags
5004 b0 b3 e3 head todo-state end-of-match show-all)
5005 (goto-char (point-min))
5006 (while (setq end-of-match (re-search-forward regexp nil t))
5007 (setq b0 (match-beginning 0)
5008 b3 (match-beginning 3) e3 (match-end 3)
5009 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5010 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5011 (member todo-state
5012 org-agenda-repeating-timestamp-show-all)))
5013 (catch :skip
5014 (and (org-at-date-range-p) (throw :skip nil))
5015 (org-agenda-skip)
5016 (if (and (match-end 1)
5017 (not (= d1 (org-time-string-to-absolute
5018 (match-string 1) d1 nil show-all
5019 (current-buffer) b0))))
5020 (throw :skip nil))
5021 (if (and e3
5022 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5023 (throw :skip nil))
5024 (setq tmp (buffer-substring (max (point-min)
5025 (- b0 org-ds-keyword-length))
5027 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5028 inactivep (= (char-after b0) ?\[)
5029 deadlinep (string-match org-deadline-regexp tmp)
5030 scheduledp (string-match org-scheduled-regexp tmp)
5031 closedp (and org-agenda-include-inactive-timestamps
5032 (string-match org-closed-string tmp))
5033 clockp (and org-agenda-include-inactive-timestamps
5034 (or (string-match org-clock-string tmp)
5035 (string-match "]-+\\'" tmp)))
5036 donep (member todo-state org-done-keywords))
5037 (if (or scheduledp deadlinep closedp clockp
5038 (and donep org-agenda-skip-timestamp-if-done))
5039 (throw :skip t))
5040 (if (string-match ">" timestr)
5041 ;; substring should only run to end of time stamp
5042 (setq timestr (substring timestr 0 (match-end 0))))
5043 (setq marker (org-agenda-new-marker b0)
5044 category (org-get-category b0)
5045 org-category-pos (get-text-property b0 'org-category-position))
5046 (save-excursion
5047 (if (not (re-search-backward org-outline-regexp-bol nil t))
5048 (setq txt org-agenda-no-heading-message)
5049 (goto-char (match-beginning 0))
5050 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5051 (assoc (point) deadline-position-alist))
5052 (throw :skip nil))
5053 (setq hdmarker (org-agenda-new-marker)
5054 tags (org-get-tags-at))
5055 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5056 (setq head (or (match-string 1) ""))
5057 (setq txt (org-agenda-format-item
5058 (if inactivep org-agenda-inactive-leader nil)
5059 head category tags timestr
5060 remove-re)))
5061 (setq priority (org-get-priority txt))
5062 (org-add-props txt props
5063 'org-marker marker 'org-hd-marker hdmarker)
5064 (org-add-props txt nil 'priority priority
5065 'org-category category 'date date
5066 'org-category-position org-category-pos
5067 'todo-state todo-state
5068 'type "timestamp")
5069 (push txt ee))
5070 (if org-agenda-skip-additional-timestamps-same-entry
5071 (outline-next-heading)
5072 (goto-char end-of-match))))
5073 (nreverse ee)))
5075 (defun org-agenda-get-sexps ()
5076 "Return the sexp information for agenda display."
5077 (require 'diary-lib)
5078 (let* ((props (list 'face 'org-agenda-calendar-sexp
5079 'mouse-face 'highlight
5080 'help-echo
5081 (format "mouse-2 or RET jump to org file %s"
5082 (abbreviate-file-name buffer-file-name))))
5083 (regexp "^&?%%(")
5084 marker category extra org-category-pos ee txt tags entry
5085 result beg b sexp sexp-entry todo-state)
5086 (goto-char (point-min))
5087 (while (re-search-forward regexp nil t)
5088 (catch :skip
5089 (org-agenda-skip)
5090 (setq beg (match-beginning 0))
5091 (goto-char (1- (match-end 0)))
5092 (setq b (point))
5093 (forward-sexp 1)
5094 (setq sexp (buffer-substring b (point)))
5095 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5096 (org-trim (match-string 1))
5097 ""))
5098 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5099 (when result
5100 (setq marker (org-agenda-new-marker beg)
5101 category (org-get-category beg)
5102 org-category-pos (get-text-property beg 'org-category-position)
5103 todo-state (org-get-todo-state))
5105 (dolist (r (if (stringp result)
5106 (list result)
5107 result)) ;; we expect a list here
5108 (when (and org-agenda-diary-sexp-prefix
5109 (string-match org-agenda-diary-sexp-prefix r))
5110 (setq extra (match-string 0 r)
5111 r (replace-match "" nil nil r)))
5112 (if (string-match "\\S-" r)
5113 (setq txt r)
5114 (setq txt "SEXP entry returned empty string"))
5116 (setq txt (org-agenda-format-item
5117 extra txt category tags 'time))
5118 (org-add-props txt props 'org-marker marker)
5119 (org-add-props txt nil
5120 'org-category category 'date date 'todo-state todo-state
5121 'org-category-position org-category-pos
5122 'type "sexp")
5123 (push txt ee)))))
5124 (nreverse ee)))
5126 ;; Calendar sanity: define some functions that are independent of
5127 ;; `calendar-date-style'.
5128 ;; Normally I would like to use ISO format when calling the diary functions,
5129 ;; but to make sure we still have Emacs 22 compatibility we bind
5130 ;; also `european-calendar-style' and use european format
5131 (defun org-anniversary (year month day &optional mark)
5132 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5133 (org-no-warnings
5134 (let ((calendar-date-style 'european) (european-calendar-style t))
5135 (diary-anniversary day month year mark))))
5136 (defun org-cyclic (N year month day &optional mark)
5137 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5138 (org-no-warnings
5139 (let ((calendar-date-style 'european) (european-calendar-style t))
5140 (diary-cyclic N day month year mark))))
5141 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5142 "Like `diary-block', but with fixed (ISO) order of arguments."
5143 (org-no-warnings
5144 (let ((calendar-date-style 'european) (european-calendar-style t))
5145 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5146 (defun org-date (year month day &optional mark)
5147 "Like `diary-date', but with fixed (ISO) order of arguments."
5148 (org-no-warnings
5149 (let ((calendar-date-style 'european) (european-calendar-style t))
5150 (diary-date day month year mark))))
5151 (defalias 'org-float 'diary-float)
5153 ;; Define the` org-class' function
5154 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5155 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5156 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5157 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5158 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5159 `holidays', then any date that is known by the Emacs calendar to be a
5160 holiday will also be skipped."
5161 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5162 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5163 (d (calendar-absolute-from-gregorian date)))
5164 (and
5165 (<= date1 d)
5166 (<= d date2)
5167 (= (calendar-day-of-week date) dayname)
5168 (or (not skip-weeks)
5169 (progn
5170 (require 'cal-iso)
5171 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5172 (not (and (memq 'holidays skip-weeks)
5173 (calendar-check-holidays date)))
5174 entry)))
5176 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5177 "Like `org-class', but honor `calendar-date-style'.
5178 The order of the first 2 times 3 arguments depends on the variable
5179 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5180 So for American calendars, give this as MONTH DAY YEAR, for European as
5181 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5182 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5183 is any number of ISO weeks in the block period for which the item should
5184 be skipped.
5186 This function is here only for backward compatibility and it is deprecated,
5187 please use `org-class' instead."
5188 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5189 (date2 (org-order-calendar-date-args m2 d2 y2)))
5190 (org-class
5191 (nth 2 date1) (car date1) (nth 1 date1)
5192 (nth 2 date2) (car date2) (nth 1 date2)
5193 dayname skip-weeks)))
5194 (make-obsolete 'org-diary-class 'org-class "")
5196 (defvar org-agenda-show-log-scoped) ;; dynamically scope in ̀org-timeline' or`org-agenda-list'
5197 (defalias 'org-get-closed 'org-agenda-get-progress)
5198 (defun org-agenda-get-progress ()
5199 "Return the logged TODO entries for agenda display."
5200 (let* ((props (list 'mouse-face 'highlight
5201 'org-not-done-regexp org-not-done-regexp
5202 'org-todo-regexp org-todo-regexp
5203 'org-complex-heading-regexp org-complex-heading-regexp
5204 'help-echo
5205 (format "mouse-2 or RET jump to org file %s"
5206 (abbreviate-file-name buffer-file-name))))
5207 (items (if (consp org-agenda-show-log-scoped)
5208 org-agenda-show-log-scoped
5209 (if (eq org-agenda-show-log-scoped 'clockcheck)
5210 '(clock)
5211 org-agenda-log-mode-items)))
5212 (parts
5213 (delq nil
5214 (list
5215 (if (memq 'closed items) (concat "\\<" org-closed-string))
5216 (if (memq 'clock items) (concat "\\<" org-clock-string))
5217 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5218 (parts-re (if parts (mapconcat 'identity parts "\\|")
5219 (error "`org-agenda-log-mode-items' is empty")))
5220 (regexp (concat
5221 "\\(" parts-re "\\)"
5222 " *\\["
5223 (regexp-quote
5224 (substring
5225 (format-time-string
5226 (car org-time-stamp-formats)
5227 (apply 'encode-time ; DATE bound by calendar
5228 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5229 1 11))))
5230 (org-agenda-search-headline-for-time nil)
5231 marker hdmarker priority category org-category-pos tags closedp
5232 statep clockp state ee txt extra timestr rest clocked)
5233 (goto-char (point-min))
5234 (while (re-search-forward regexp nil t)
5235 (catch :skip
5236 (org-agenda-skip)
5237 (setq marker (org-agenda-new-marker (match-beginning 0))
5238 closedp (equal (match-string 1) org-closed-string)
5239 statep (equal (string-to-char (match-string 1)) ?-)
5240 clockp (not (or closedp statep))
5241 state (and statep (match-string 2))
5242 category (org-get-category (match-beginning 0))
5243 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
5244 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5245 (when (string-match "\\]" timestr)
5246 ;; substring should only run to end of time stamp
5247 (setq rest (substring timestr (match-end 0))
5248 timestr (substring timestr 0 (match-end 0)))
5249 (if (and (not closedp) (not statep)
5250 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5251 rest))
5252 (progn (setq timestr (concat (substring timestr 0 -1)
5253 "-" (match-string 1 rest) "]"))
5254 (setq clocked (match-string 2 rest)))
5255 (setq clocked "-")))
5256 (save-excursion
5257 (setq extra
5258 (cond
5259 ((not org-agenda-log-mode-add-notes) nil)
5260 (statep
5261 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5262 (match-string 1)))
5263 (clockp
5264 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5265 (match-string 1)))))
5266 (if (not (re-search-backward org-outline-regexp-bol nil t))
5267 (setq txt org-agenda-no-heading-message)
5268 (goto-char (match-beginning 0))
5269 (setq hdmarker (org-agenda-new-marker)
5270 tags (org-get-tags-at))
5271 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5272 (setq txt (match-string 1))
5273 (when extra
5274 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5275 (setq txt (concat (substring txt 0 (match-beginning 1))
5276 " - " extra " " (match-string 2 txt)))
5277 (setq txt (concat txt " - " extra))))
5278 (setq txt (org-agenda-format-item
5279 (cond
5280 (closedp "Closed: ")
5281 (statep (concat "State: (" state ")"))
5282 (t (concat "Clocked: (" clocked ")")))
5283 txt category tags timestr)))
5284 (setq priority 100000)
5285 (org-add-props txt props
5286 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5287 'priority priority 'org-category category
5288 'org-category-position org-category-pos
5289 'type "closed" 'date date
5290 'undone-face 'org-warning 'done-face 'org-agenda-done)
5291 (push txt ee))
5292 (goto-char (point-at-eol))))
5293 (nreverse ee)))
5295 (defun org-agenda-show-clocking-issues ()
5296 "Add overlays, showing issues with clocking.
5297 See also the user option `org-agenda-clock-consistency-checks'."
5298 (interactive)
5299 (let* ((pl org-agenda-clock-consistency-checks)
5300 (re (concat "^[ \t]*"
5301 org-clock-string
5302 "[ \t]+"
5303 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5304 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5305 (tlstart 0.)
5306 (tlend 0.)
5307 (maxtime (org-hh:mm-string-to-minutes
5308 (or (plist-get pl :max-duration) "24:00")))
5309 (mintime (org-hh:mm-string-to-minutes
5310 (or (plist-get pl :min-duration) 0)))
5311 (maxgap (org-hh:mm-string-to-minutes
5312 ;; default 30:00 means never complain
5313 (or (plist-get pl :max-gap) "30:00")))
5314 (gapok (mapcar 'org-hh:mm-string-to-minutes
5315 (plist-get pl :gap-ok-around)))
5316 (def-face (or (plist-get pl :default-face)
5317 '((:background "DarkRed") (:foreground "white"))))
5318 issue face m te ts dt ov)
5319 (goto-char (point-min))
5320 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5321 (setq issue nil face def-face)
5322 (catch 'next
5323 (setq m (org-get-at-bol 'org-marker)
5324 te nil ts nil)
5325 (unless (and m (markerp m))
5326 (setq issue "No valid clock line") (throw 'next t))
5327 (org-with-point-at m
5328 (save-excursion
5329 (goto-char (point-at-bol))
5330 (unless (looking-at re)
5331 (error "No valid Clock line")
5332 (throw 'next t))
5333 (unless (match-end 3)
5334 (setq issue "No end time"
5335 face (or (plist-get pl :no-end-time-face) face))
5336 (throw 'next t))
5337 (setq ts (match-string 1)
5338 te (match-string 3)
5339 ts (org-float-time
5340 (apply 'encode-time (org-parse-time-string ts)))
5341 te (org-float-time
5342 (apply 'encode-time (org-parse-time-string te)))
5343 dt (- te ts))))
5344 (cond
5345 ((> dt (* 60 maxtime))
5346 ;; a very long clocking chunk
5347 (setq issue (format "Clocking interval is very long: %s"
5348 (org-minutes-to-hh:mm-string
5349 (floor (/ (float dt) 60.))))
5350 face (or (plist-get pl :long-face) face)))
5351 ((< dt (* 60 mintime))
5352 ;; a very short clocking chunk
5353 (setq issue (format "Clocking interval is very short: %s"
5354 (org-minutes-to-hh:mm-string
5355 (floor (/ (float dt) 60.))))
5356 face (or (plist-get pl :short-face) face)))
5357 ((and (> tlend 0) (< ts tlend))
5358 ;; Two clock entries are overlapping
5359 (setq issue (format "Clocking overlap: %d minutes"
5360 (/ (- tlend ts) 60))
5361 face (or (plist-get pl :overlap-face) face)))
5362 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5363 ;; There is a gap, lets see if we need to report it
5364 (unless (org-agenda-check-clock-gap tlend ts gapok)
5365 (setq issue (format "Clocking gap: %d minutes"
5366 (/ (- ts tlend) 60))
5367 face (or (plist-get pl :gap-face) face))))
5368 (t nil)))
5369 (setq tlend (or te tlend) tlstart (or ts tlstart))
5370 (when issue
5371 ;; OK, there was some issue, add an overlay to show the issue
5372 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5373 (overlay-put ov 'before-string
5374 (concat
5375 (org-add-props
5376 (format "%-43s" (concat " " issue))
5378 'face face)
5379 "\n"))
5380 (overlay-put ov 'evaporate t)))))
5382 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5383 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5384 (catch 'exit
5385 (unless ok-list
5386 ;; there are no OK times for gaps...
5387 (throw 'exit nil))
5388 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5389 ;; This is more than 24 hours, so it is OK.
5390 ;; because we have at least one OK time, that must be in the
5391 ;; 24 hour interval.
5392 (throw 'exit t))
5393 ;; We have a shorter gap.
5394 ;; Now we have to get the minute of the day when these times are
5395 (let* ((t1dec (decode-time (seconds-to-time t1)))
5396 (t2dec (decode-time (seconds-to-time t2)))
5397 ;; compute the minute on the day
5398 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5399 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5400 (when (< min2 min1)
5401 ;; if min2 is smaller than min1, this means it is on the next day.
5402 ;; Wrap it to after midnight.
5403 (setq min2 (+ min2 1440)))
5404 ;; Now check if any of the OK times is in the gap
5405 (mapc (lambda (x)
5406 ;; Wrap the time to after midnight if necessary
5407 (if (< x min1) (setq x (+ x 1440)))
5408 ;; Check if in interval
5409 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5410 ok-list)
5411 ;; Nope, this gap is not OK
5412 nil)))
5414 (defun org-agenda-get-deadlines ()
5415 "Return the deadline information for agenda display."
5416 (let* ((props (list 'mouse-face 'highlight
5417 'org-not-done-regexp org-not-done-regexp
5418 'org-todo-regexp org-todo-regexp
5419 'org-complex-heading-regexp org-complex-heading-regexp
5420 'help-echo
5421 (format "mouse-2 or RET jump to org file %s"
5422 (abbreviate-file-name buffer-file-name))))
5423 (regexp org-deadline-time-regexp)
5424 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5425 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5426 d2 diff dfrac wdays pos pos1 category org-category-pos
5427 tags suppress-prewarning ee txt head face s todo-state
5428 show-all upcomingp donep timestr)
5429 (goto-char (point-min))
5430 (while (re-search-forward regexp nil t)
5431 (setq suppress-prewarning nil)
5432 (catch :skip
5433 (org-agenda-skip)
5434 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5435 (save-match-data
5436 (string-match org-scheduled-time-regexp
5437 (buffer-substring (point-at-bol)
5438 (point-at-eol)))))
5439 (setq suppress-prewarning
5440 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5441 org-agenda-skip-deadline-prewarning-if-scheduled
5442 0)))
5443 (setq s (match-string 1)
5444 txt nil
5445 pos (1- (match-beginning 1))
5446 todo-state (save-match-data (org-get-todo-state))
5447 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5448 (member todo-state
5449 org-agenda-repeating-timestamp-show-all))
5450 d2 (org-time-string-to-absolute
5451 (match-string 1) d1 'past show-all
5452 (current-buffer) pos)
5453 diff (- d2 d1)
5454 wdays (if suppress-prewarning
5455 (let ((org-deadline-warning-days suppress-prewarning))
5456 (org-get-wdays s))
5457 (org-get-wdays s))
5458 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5459 upcomingp (and todayp (> diff 0)))
5460 ;; When to show a deadline in the calendar:
5461 ;; If the expiration is within wdays warning time.
5462 ;; Past-due deadlines are only shown on the current date
5463 (if (and (or (and (<= diff wdays)
5464 (and todayp (not org-agenda-only-exact-dates)))
5465 (= diff 0)))
5466 (save-excursion
5467 ;; (setq todo-state (org-get-todo-state))
5468 (setq donep (member todo-state org-done-keywords))
5469 (if (and donep
5470 (or org-agenda-skip-deadline-if-done
5471 (not (= diff 0))))
5472 (setq txt nil)
5473 (setq category (org-get-category)
5474 org-category-pos (get-text-property (point) 'org-category-position))
5475 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5476 (setq txt org-agenda-no-heading-message)
5477 (goto-char (match-end 0))
5478 (setq pos1 (match-beginning 0))
5479 (setq tags (org-get-tags-at pos1))
5480 (setq head (buffer-substring-no-properties
5481 (point)
5482 (progn (skip-chars-forward "^\r\n")
5483 (point))))
5484 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5485 (setq timestr
5486 (concat (substring s (match-beginning 1)) " "))
5487 (setq timestr 'time))
5488 (setq txt (org-agenda-format-item
5489 (if (= diff 0)
5490 (car org-agenda-deadline-leaders)
5491 (if (functionp
5492 (nth 1 org-agenda-deadline-leaders))
5493 (funcall
5494 (nth 1 org-agenda-deadline-leaders)
5495 diff date)
5496 (format (nth 1 org-agenda-deadline-leaders)
5497 diff)))
5498 head category tags
5499 (if (not (= diff 0)) nil timestr)))))
5500 (when txt
5501 (setq face (org-agenda-deadline-face dfrac))
5502 (org-add-props txt props
5503 'org-marker (org-agenda-new-marker pos)
5504 'org-hd-marker (org-agenda-new-marker pos1)
5505 'priority (+ (- diff)
5506 (org-get-priority txt))
5507 'org-category category
5508 'org-category-position org-category-pos
5509 'todo-state todo-state
5510 'type (if upcomingp "upcoming-deadline" "deadline")
5511 'date (if upcomingp date d2)
5512 'face (if donep 'org-agenda-done face)
5513 'undone-face face 'done-face 'org-agenda-done)
5514 (push txt ee))))))
5515 (nreverse ee)))
5517 (defun org-agenda-deadline-face (fraction)
5518 "Return the face to displaying a deadline item.
5519 FRACTION is what fraction of the head-warning time has passed."
5520 (let ((faces org-agenda-deadline-faces) f)
5521 (catch 'exit
5522 (while (setq f (pop faces))
5523 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5525 (defun org-agenda-get-scheduled (&optional deadline-results)
5526 "Return the scheduled information for agenda display."
5527 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5528 'org-todo-regexp org-todo-regexp
5529 'org-complex-heading-regexp org-complex-heading-regexp
5530 'done-face 'org-agenda-done
5531 'mouse-face 'highlight
5532 'help-echo
5533 (format "mouse-2 or RET jump to org file %s"
5534 (abbreviate-file-name buffer-file-name))))
5535 (regexp org-scheduled-time-regexp)
5536 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5537 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5539 (deadline-position-alist
5540 (mapcar (lambda (a) (and (setq mm (get-text-property
5541 0 'org-hd-marker a))
5542 (cons (marker-position mm) a)))
5543 deadline-results))
5544 d2 diff pos pos1 category org-category-pos tags donep
5545 ee txt head pastschedp todo-state face timestr s habitp show-all)
5546 (goto-char (point-min))
5547 (while (re-search-forward regexp nil t)
5548 (catch :skip
5549 (org-agenda-skip)
5550 (setq s (match-string 1)
5551 txt nil
5552 pos (1- (match-beginning 1))
5553 todo-state (save-match-data (org-get-todo-state))
5554 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5555 (member todo-state
5556 org-agenda-repeating-timestamp-show-all))
5557 d2 (org-time-string-to-absolute
5558 (match-string 1) d1 'past show-all
5559 (current-buffer) pos)
5560 diff (- d2 d1))
5561 (setq pastschedp (and todayp (< diff 0)))
5562 ;; When to show a scheduled item in the calendar:
5563 ;; If it is on or past the date.
5564 (when (or (and (< diff 0)
5565 (< (abs diff) org-scheduled-past-days)
5566 (and todayp (not org-agenda-only-exact-dates)))
5567 (= diff 0))
5568 (save-excursion
5569 (setq donep (member todo-state org-done-keywords))
5570 (if (and donep
5571 (or org-agenda-skip-scheduled-if-done
5572 (not (= diff 0))
5573 (and (functionp 'org-is-habit-p)
5574 (org-is-habit-p))))
5575 (setq txt nil)
5576 (setq habitp (and (functionp 'org-is-habit-p)
5577 (org-is-habit-p)))
5578 (setq category (org-get-category)
5579 org-category-pos (get-text-property (point) 'org-category-position))
5580 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5581 (setq txt org-agenda-no-heading-message)
5582 (goto-char (match-end 0))
5583 (setq pos1 (match-beginning 0))
5584 (if habitp
5585 (if (or (not org-habit-show-habits)
5586 (and (not todayp)
5587 org-habit-show-habits-only-for-today))
5588 (throw :skip nil))
5589 (if (and
5590 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5591 (and org-agenda-skip-scheduled-if-deadline-is-shown
5592 pastschedp))
5593 (setq mm (assoc pos1 deadline-position-alist)))
5594 (throw :skip nil)))
5595 (setq tags (org-get-tags-at))
5596 (setq head (buffer-substring-no-properties
5597 (point)
5598 (progn (skip-chars-forward "^\r\n") (point))))
5599 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5600 (setq timestr
5601 (concat (substring s (match-beginning 1)) " "))
5602 (setq timestr 'time))
5603 (setq txt (org-agenda-format-item
5604 (if (= diff 0)
5605 (car org-agenda-scheduled-leaders)
5606 (format (nth 1 org-agenda-scheduled-leaders)
5607 (- 1 diff)))
5608 head category tags
5609 (if (not (= diff 0)) nil timestr)
5610 nil habitp))))
5611 (when txt
5612 (setq face
5613 (cond
5614 ((and (not habitp) pastschedp)
5615 'org-scheduled-previously)
5616 (todayp 'org-scheduled-today)
5617 (t 'org-scheduled))
5618 habitp (and habitp (org-habit-parse-todo)))
5619 (org-add-props txt props
5620 'undone-face face
5621 'face (if donep 'org-agenda-done face)
5622 'org-marker (org-agenda-new-marker pos)
5623 'org-hd-marker (org-agenda-new-marker pos1)
5624 'type (if pastschedp "past-scheduled" "scheduled")
5625 'date (if pastschedp d2 date)
5626 'priority (if habitp
5627 (org-habit-get-priority habitp)
5628 (+ 94 (- 5 diff) (org-get-priority txt)))
5629 'org-category category
5630 'org-category-position org-category-pos
5631 'org-habit-p habitp
5632 'todo-state todo-state)
5633 (push txt ee))))))
5634 (nreverse ee)))
5636 (defun org-agenda-get-blocks ()
5637 "Return the date-range information for agenda display."
5638 (let* ((props (list 'face nil
5639 'org-not-done-regexp org-not-done-regexp
5640 'org-todo-regexp org-todo-regexp
5641 'org-complex-heading-regexp org-complex-heading-regexp
5642 'mouse-face 'highlight
5643 'help-echo
5644 (format "mouse-2 or RET jump to org file %s"
5645 (abbreviate-file-name buffer-file-name))))
5646 (regexp org-tr-regexp)
5647 (d0 (calendar-absolute-from-gregorian date))
5648 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5649 todo-state tags pos head donep)
5650 (goto-char (point-min))
5651 (while (re-search-forward regexp nil t)
5652 (catch :skip
5653 (org-agenda-skip)
5654 (setq pos (point))
5655 (let ((start-time (match-string 1))
5656 (end-time (match-string 2)))
5657 (setq s1 (match-string 1)
5658 s2 (match-string 2)
5659 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5660 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5661 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5662 ;; Only allow days between the limits, because the normal
5663 ;; date stamps will catch the limits.
5664 (save-excursion
5665 (setq todo-state (org-get-todo-state))
5666 (setq donep (member todo-state org-done-keywords))
5667 (if (and donep org-agenda-skip-timestamp-if-done)
5668 (throw :skip t))
5669 (setq marker (org-agenda-new-marker (point)))
5670 (setq category (org-get-category)
5671 org-category-pos (get-text-property (point) 'org-category-position))
5672 (if (not (re-search-backward org-outline-regexp-bol nil t))
5673 (setq txt org-agenda-no-heading-message)
5674 (goto-char (match-beginning 0))
5675 (setq hdmarker (org-agenda-new-marker (point)))
5676 (setq tags (org-get-tags-at))
5677 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5678 (setq head (match-string 1))
5679 (let ((remove-re
5680 (if org-agenda-remove-timeranges-from-blocks
5681 (concat
5682 "<" (regexp-quote s1) ".*?>"
5683 "--"
5684 "<" (regexp-quote s2) ".*?>")
5685 nil)))
5686 (setq txt (org-agenda-format-item
5687 (format
5688 (nth (if (= d1 d2) 0 1)
5689 org-agenda-timerange-leaders)
5690 (1+ (- d0 d1)) (1+ (- d2 d1)))
5691 head category tags
5692 (cond ((and (= d1 d0) (= d2 d0))
5693 (concat "<" start-time ">--<" end-time ">"))
5694 ((= d1 d0)
5695 (concat "<" start-time ">"))
5696 ((= d2 d0)
5697 (concat "<" end-time ">"))
5698 (t nil))
5699 remove-re))))
5700 (org-add-props txt props
5701 'org-marker marker 'org-hd-marker hdmarker
5702 'type "block" 'date date
5703 'todo-state todo-state
5704 'priority (org-get-priority txt) 'org-category category
5705 'org-category-position org-category-pos)
5706 (push txt ee))))
5707 (goto-char pos)))
5708 ;; Sort the entries by expiration date.
5709 (nreverse ee)))
5711 ;;; Agenda presentation and sorting
5713 (defvar org-prefix-has-time nil
5714 "A flag, set by `org-compile-prefix-format'.
5715 The flag is set if the currently compiled format contains a `%t'.")
5716 (defvar org-prefix-has-tag nil
5717 "A flag, set by `org-compile-prefix-format'.
5718 The flag is set if the currently compiled format contains a `%T'.")
5719 (defvar org-prefix-has-effort nil
5720 "A flag, set by `org-compile-prefix-format'.
5721 The flag is set if the currently compiled format contains a `%e'.")
5722 (defvar org-prefix-category-length nil
5723 "Used by `org-compile-prefix-format' to remember the category field width.")
5724 (defvar org-prefix-category-max-length nil
5725 "Used by `org-compile-prefix-format' to remember the category field width.")
5727 (defun org-agenda-get-category-icon (category)
5728 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5729 (dolist (entry org-agenda-category-icon-alist)
5730 (when (org-string-match-p (car entry) category)
5731 (if (listp (cadr entry))
5732 (return (cadr entry))
5733 (return (apply 'create-image (cdr entry)))))))
5735 (defun org-agenda-format-item (extra txt &optional category tags dotime
5736 remove-re habitp)
5737 "Format TXT to be inserted into the agenda buffer.
5738 In particular, it adds the prefix and corresponding text properties. EXTRA
5739 must be a string and replaces the `%s' specifier in the prefix format.
5740 CATEGORY (string, symbol or nil) may be used to overrule the default
5741 category taken from local variable or file name. It will replace the `%c'
5742 specifier in the format. DOTIME, when non-nil, indicates that a
5743 time-of-day should be extracted from TXT for sorting of this entry, and for
5744 the `%t' specifier in the format. When DOTIME is a string, this string is
5745 searched for a time before TXT is. TAGS can be the tags of the headline.
5746 Any match of REMOVE-RE will be removed from TXT."
5747 ;; We keep the org-prefix-* variable values along with a compiled
5748 ;; formatter, so that multiple agendas existing at the same time, do
5749 ;; not step on each other toes.
5751 ;; It was inconvenient to make these variables buffer local in
5752 ;; Agenda buffers, because this function expects to be called with
5753 ;; the buffer where item comes from being current, and not agenda
5754 ;; buffer
5755 (let* ((bindings (car org-prefix-format-compiled))
5756 (formatter (cadr org-prefix-format-compiled)))
5757 (loop for (var value) in bindings
5758 do (set var value))
5759 (save-match-data
5760 ;; Diary entries sometimes have extra whitespace at the beginning
5761 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5763 ;; Fix the tags part in txt
5764 (setq txt (org-agenda-fix-displayed-tags
5765 txt tags
5766 org-agenda-show-inherited-tags
5767 org-agenda-hide-tags-regexp))
5768 (let* ((category (or category
5769 (if (stringp org-category)
5770 org-category
5771 (and org-category (symbol-name org-category)))
5772 (if buffer-file-name
5773 (file-name-sans-extension
5774 (file-name-nondirectory buffer-file-name))
5775 "")))
5776 (category-icon (org-agenda-get-category-icon category))
5777 (category-icon (if category-icon
5778 (propertize " " 'display category-icon)
5779 ""))
5780 ;; time, tag, effort are needed for the eval of the prefix format
5781 (tag (if tags (nth (1- (length tags)) tags) ""))
5782 time effort neffort
5783 (ts (if dotime (concat
5784 (if (stringp dotime) dotime "")
5785 (and org-agenda-search-headline-for-time txt))))
5786 (time-of-day (and dotime (org-get-time-of-day ts)))
5787 stamp plain s0 s1 s2 rtn srp l
5788 duration thecategory)
5789 (and (derived-mode-p 'org-mode) buffer-file-name
5790 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5791 (when (and dotime time-of-day)
5792 ;; Extract starting and ending time and move them to prefix
5793 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5794 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5795 (setq s0 (match-string 0 ts)
5796 srp (and stamp (match-end 3))
5797 s1 (match-string (if plain 1 2) ts)
5798 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5800 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5801 ;; them, we might want to remove them there to avoid duplication.
5802 ;; The user can turn this off with a variable.
5803 (if (and org-prefix-has-time
5804 org-agenda-remove-times-when-in-prefix (or stamp plain)
5805 (string-match (concat (regexp-quote s0) " *") txt)
5806 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5807 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5808 (= (match-beginning 0) 0)
5810 (setq txt (replace-match "" nil nil txt))))
5811 ;; Normalize the time(s) to 24 hour
5812 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5813 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5815 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5816 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5817 (setq s2
5818 (org-minutes-to-hh:mm-string
5819 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5821 ;; Compute the duration
5822 (when s2
5823 (setq duration (- (org-hh:mm-string-to-minutes s2)
5824 (org-hh:mm-string-to-minutes s1)))))
5826 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5827 txt)
5828 ;; Tags are in the string
5829 (if (or (eq org-agenda-remove-tags t)
5830 (and org-agenda-remove-tags
5831 org-prefix-has-tag))
5832 (setq txt (replace-match "" t t txt))
5833 (setq txt (replace-match
5834 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5835 (match-string 2 txt))
5836 t t txt))))
5837 (when (derived-mode-p 'org-mode)
5838 (setq effort
5839 (condition-case nil
5840 (org-get-effort
5841 (or (get-text-property 0 'org-hd-marker txt)
5842 (get-text-property 0 'org-marker txt)))
5843 (error nil)))
5844 (when effort
5845 (setq neffort (org-duration-string-to-minutes effort)
5846 effort (setq effort (concat "[" effort "]")))))
5847 ;; prevent erroring out with %e format when there is no effort
5848 (or effort (setq effort ""))
5850 (when remove-re
5851 (while (string-match remove-re txt)
5852 (setq txt (replace-match "" t t txt))))
5854 ;; Set org-heading property on `txt' to mark the start of the
5855 ;; heading.
5856 (add-text-properties 0 (length txt) '(org-heading t) txt)
5858 ;; Prepare the variables needed in the eval of the compiled format
5859 (setq time (cond (s2 (concat
5860 (org-agenda-time-of-day-to-ampm-maybe s1)
5861 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5862 (if org-agenda-timegrid-use-ampm " ")))
5863 (s1 (concat
5864 (org-agenda-time-of-day-to-ampm-maybe s1)
5865 (if org-agenda-timegrid-use-ampm
5866 "........ "
5867 "......")))
5868 (t ""))
5869 extra (or (and (not habitp) extra) "")
5870 category (if (symbolp category) (symbol-name category) category)
5871 thecategory (copy-sequence category))
5872 (if (string-match org-bracket-link-regexp category)
5873 (progn
5874 (setq l (if (match-end 3)
5875 (- (match-end 3) (match-beginning 3))
5876 (- (match-end 1) (match-beginning 1))))
5877 (when (< l (or org-prefix-category-length 0))
5878 (setq category (copy-sequence category))
5879 (org-add-props category nil
5880 'extra-space (make-string
5881 (- org-prefix-category-length l 1) ?\ ))))
5882 (if (and org-prefix-category-max-length
5883 (>= (length category) org-prefix-category-max-length))
5884 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5885 ;; Evaluate the compiled format
5886 (setq rtn (concat (eval formatter) txt))
5888 ;; And finally add the text properties
5889 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5890 (org-add-props rtn nil
5891 'org-category (if thecategory (downcase thecategory) category)
5892 'tags (mapcar 'org-downcase-keep-props tags)
5893 'org-highest-priority org-highest-priority
5894 'org-lowest-priority org-lowest-priority
5895 'time-of-day time-of-day
5896 'duration duration
5897 'effort effort
5898 'effort-minutes neffort
5899 'txt txt
5900 'time time
5901 'extra extra
5902 'format org-prefix-format-compiled
5903 'dotime dotime)))))
5905 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5906 "Remove tags string from TXT, and add a modified list of tags.
5907 The modified list may contain inherited tags, and tags matched by
5908 `org-agenda-hide-tags-regexp' will be removed."
5909 (when (or add-inherited hide-re)
5910 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5911 (setq txt (substring txt 0 (match-beginning 0))))
5912 (setq tags
5913 (delq nil
5914 (mapcar (lambda (tg)
5915 (if (or (and hide-re (string-match hide-re tg))
5916 (and (not add-inherited)
5917 (get-text-property 0 'inherited tg)))
5919 tg))
5920 tags)))
5921 (when tags
5922 (let ((have-i (get-text-property 0 'inherited (car tags)))
5924 (setq txt (concat txt " :"
5925 (mapconcat
5926 (lambda (x)
5927 (setq i (get-text-property 0 'inherited x))
5928 (if (and have-i (not i))
5929 (progn
5930 (setq have-i nil)
5931 (concat ":" x))
5933 tags ":")
5934 (if have-i "::" ":"))))))
5935 txt)
5937 (defun org-downcase-keep-props (s)
5938 (let ((props (text-properties-at 0 s)))
5939 (setq s (downcase s))
5940 (add-text-properties 0 (length s) props s)
5943 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5944 (defvar org-agenda-sorting-strategy-selected nil)
5946 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5947 (catch 'exit
5948 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5949 ((and todayp (member 'today (car org-agenda-time-grid))))
5950 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5951 ((member 'weekly (car org-agenda-time-grid)))
5952 (t (throw 'exit list)))
5953 (let* ((have (delq nil (mapcar
5954 (lambda (x) (get-text-property 1 'time-of-day x))
5955 list)))
5956 (string (nth 1 org-agenda-time-grid))
5957 (gridtimes (nth 2 org-agenda-time-grid))
5958 (req (car org-agenda-time-grid))
5959 (remove (member 'remove-match req))
5960 new time)
5961 (if (and (member 'require-timed req) (not have))
5962 ;; don't show empty grid
5963 (throw 'exit list))
5964 (while (setq time (pop gridtimes))
5965 (unless (and remove (member time have))
5966 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5967 (push (org-agenda-format-item
5968 nil string "" nil
5969 (concat (substring time 0 -2) ":" (substring time -2)))
5970 new)
5971 (put-text-property
5972 2 (length (car new)) 'face 'org-time-grid (car new))))
5973 (when (and todayp org-agenda-show-current-time-in-grid)
5974 (push (org-agenda-format-item
5976 org-agenda-current-time-string
5977 "" nil
5978 (format-time-string "%H:%M "))
5979 new)
5980 (put-text-property
5981 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5983 (if (member 'time-up org-agenda-sorting-strategy-selected)
5984 (append new list)
5985 (append list new)))))
5987 (defun org-compile-prefix-format (key)
5988 "Compile the prefix format into a Lisp form that can be evaluated.
5989 The resulting form and associated variable bindings is returned
5990 and stored in the variable `org-prefix-format-compiled'."
5991 (setq org-prefix-has-time nil org-prefix-has-tag nil
5992 org-prefix-category-length nil
5993 org-prefix-has-effort nil)
5994 (let ((s (cond
5995 ((stringp org-agenda-prefix-format)
5996 org-agenda-prefix-format)
5997 ((assq key org-agenda-prefix-format)
5998 (cdr (assq key org-agenda-prefix-format)))
5999 (t " %-12:c%?-12t% s")))
6000 (start 0)
6001 varform vars var e c f opt)
6002 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
6003 s start)
6004 (setq var (or (cdr (assoc (match-string 4 s)
6005 '(("c" . category) ("t" . time) ("s" . extra)
6006 ("i" . category-icon) ("T" . tag) ("e" . effort))))
6007 'eval)
6008 c (or (match-string 3 s) "")
6009 opt (match-beginning 1)
6010 start (1+ (match-beginning 0)))
6011 (if (equal var 'time) (setq org-prefix-has-time t))
6012 (if (equal var 'tag) (setq org-prefix-has-tag t))
6013 (if (equal var 'effort) (setq org-prefix-has-effort t))
6014 (setq f (concat "%" (match-string 2 s) "s"))
6015 (when (equal var 'category)
6016 (setq org-prefix-category-length
6017 (floor (abs (string-to-number (match-string 2 s)))))
6018 (setq org-prefix-category-max-length
6019 (let ((x (match-string 2 s)))
6020 (save-match-data
6021 (if (string-match "\\.[0-9]+" x)
6022 (string-to-number (substring (match-string 0 x) 1)))))))
6023 (if (eq var 'eval)
6024 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6025 (if opt
6026 (setq varform
6027 `(if (equal "" ,var)
6029 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6030 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6031 (setq s (replace-match "%s" t nil s))
6032 (push varform vars))
6033 (setq vars (nreverse vars))
6034 (with-current-buffer org-agenda-buffer
6035 (setq org-prefix-format-compiled
6036 (list
6037 `((org-prefix-has-time ,org-prefix-has-time)
6038 (org-prefix-has-tag ,org-prefix-has-tag)
6039 (org-prefix-category-length ,org-prefix-category-length)
6040 (org-prefix-has-effort ,org-prefix-has-effort))
6041 `(format ,s ,@vars))))))
6043 (defun org-set-sorting-strategy (key)
6044 (if (symbolp (car org-agenda-sorting-strategy))
6045 ;; the old format
6046 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6047 (setq org-agenda-sorting-strategy-selected
6048 (or (cdr (assq key org-agenda-sorting-strategy))
6049 (cdr (assq 'agenda org-agenda-sorting-strategy))
6050 '(time-up category-keep priority-down)))))
6052 (defun org-get-time-of-day (s &optional string mod24)
6053 "Check string S for a time of day.
6054 If found, return it as a military time number between 0 and 2400.
6055 If not found, return nil.
6056 The optional STRING argument forces conversion into a 5 character wide string
6057 HH:MM."
6058 (save-match-data
6059 (when
6060 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6061 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6062 (let* ((h (string-to-number (match-string 1 s)))
6063 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6064 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6065 (am-p (equal ampm "am"))
6066 (h1 (cond ((not ampm) h)
6067 ((= h 12) (if am-p 0 12))
6068 (t (+ h (if am-p 0 12)))))
6069 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6070 (mod h1 24) h1))
6071 (t0 (+ (* 100 h2) m))
6072 (t1 (concat (if (>= h1 24) "+" " ")
6073 (if (and org-agenda-time-leading-zero
6074 (< t0 1000)) "0" "")
6075 (if (< t0 100) "0" "")
6076 (if (< t0 10) "0" "")
6077 (int-to-string t0))))
6078 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6080 (defvar org-agenda-before-sorting-filter-function nil
6081 "Function to be applied to agenda items prior to sorting.
6082 Prior to sorting also means just before they are inserted into the agenda.
6084 To aid sorting, you may revisit the original entries and add more text
6085 properties which will later be used by the sorting functions.
6087 The function should take a string argument, an agenda line.
6088 It has access to the text properties in that line, which contain among
6089 other things, the property `org-hd-marker' that points to the entry
6090 where the line comes from. Note that not all lines going into the agenda
6091 have this property, only most.
6093 The function should return the modified string. It is probably best
6094 to ONLY change text properties.
6096 You can also use this function as a filter, by returning nil for lines
6097 you don't want to have in the agenda at all. For this application, you
6098 could bind the variable in the options section of a custom command.")
6100 (defun org-finalize-agenda-entries (list &optional nosort)
6101 "Sort and concatenate the agenda items."
6102 (setq list (mapcar 'org-agenda-highlight-todo list))
6103 (if nosort
6104 list
6105 (when org-agenda-before-sorting-filter-function
6106 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
6107 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
6109 (defun org-agenda-highlight-todo (x)
6110 (let ((org-done-keywords org-done-keywords-for-agenda)
6111 (case-fold-search nil)
6113 (if (eq x 'line)
6114 (save-excursion
6115 (beginning-of-line 1)
6116 (setq re (org-get-at-bol 'org-todo-regexp))
6117 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6118 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6119 (add-text-properties (match-beginning 0) (match-end 1)
6120 (list 'face (org-get-todo-face 1)))
6121 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6122 (delete-region (match-beginning 1) (1- (match-end 0)))
6123 (goto-char (match-beginning 1))
6124 (insert (format org-agenda-todo-keyword-format s)))))
6125 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6126 (setq re (get-text-property 0 'org-todo-regexp x))
6127 (when (and re
6128 ;; Test `pl' because if there's no heading content,
6129 ;; there's no point matching to highlight. Note
6130 ;; that if we didn't test `pl' first, and there
6131 ;; happened to be no keyword from `org-todo-regexp'
6132 ;; on this heading line, then the `equal' comparison
6133 ;; afterwards would spuriously succeed in the case
6134 ;; where `pl' is nil -- causing an args-out-of-range
6135 ;; error when we try to add text properties to text
6136 ;; that isn't there.
6138 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6139 x pl) pl))
6140 (add-text-properties
6141 (or (match-end 1) (match-end 0)) (match-end 0)
6142 (list 'face (org-get-todo-face (match-string 2 x)))
6144 (when (match-end 1)
6145 (setq x (concat (substring x 0 (match-end 1))
6146 (format org-agenda-todo-keyword-format
6147 (match-string 2 x))
6148 (org-add-props " " (text-properties-at 0 x))
6149 (substring x (match-end 3)))))))
6150 x)))
6152 (defsubst org-cmp-priority (a b)
6153 "Compare the priorities of string A and B."
6154 (let ((pa (or (get-text-property 1 'priority a) 0))
6155 (pb (or (get-text-property 1 'priority b) 0)))
6156 (cond ((> pa pb) +1)
6157 ((< pa pb) -1)
6158 (t nil))))
6160 (defsubst org-cmp-effort (a b)
6161 "Compare the effort values of string A and B."
6162 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6163 (ea (or (get-text-property 1 'effort-minutes a) def))
6164 (eb (or (get-text-property 1 'effort-minutes b) def)))
6165 (cond ((> ea eb) +1)
6166 ((< ea eb) -1)
6167 (t nil))))
6169 (defsubst org-cmp-category (a b)
6170 "Compare the string values of categories of strings A and B."
6171 (let ((ca (or (get-text-property 1 'org-category a) ""))
6172 (cb (or (get-text-property 1 'org-category b) "")))
6173 (cond ((string-lessp ca cb) -1)
6174 ((string-lessp cb ca) +1)
6175 (t nil))))
6177 (defsubst org-cmp-todo-state (a b)
6178 "Compare the todo states of strings A and B."
6179 (let* ((ma (or (get-text-property 1 'org-marker a)
6180 (get-text-property 1 'org-hd-marker a)))
6181 (mb (or (get-text-property 1 'org-marker b)
6182 (get-text-property 1 'org-hd-marker b)))
6183 (fa (and ma (marker-buffer ma)))
6184 (fb (and mb (marker-buffer mb)))
6185 (todo-kwds
6186 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6187 (and fb (with-current-buffer fb org-todo-keywords-1))))
6188 (ta (or (get-text-property 1 'todo-state a) ""))
6189 (tb (or (get-text-property 1 'todo-state b) ""))
6190 (la (- (length (member ta todo-kwds))))
6191 (lb (- (length (member tb todo-kwds))))
6192 (donepa (member ta org-done-keywords-for-agenda))
6193 (donepb (member tb org-done-keywords-for-agenda)))
6194 (cond ((and donepa (not donepb)) -1)
6195 ((and (not donepa) donepb) +1)
6196 ((< la lb) -1)
6197 ((< lb la) +1)
6198 (t nil))))
6200 (defsubst org-cmp-alpha (a b)
6201 "Compare the headlines, alphabetically."
6202 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6203 (plb (text-property-any 0 (length b) 'org-heading t b))
6204 (ta (and pla (substring a pla)))
6205 (tb (and plb (substring b plb))))
6206 (when pla
6207 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6208 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6209 (setq ta (substring ta (match-end 0))))
6210 (setq ta (downcase ta)))
6211 (when plb
6212 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6213 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6214 (setq tb (substring tb (match-end 0))))
6215 (setq tb (downcase tb)))
6216 (cond ((not ta) +1)
6217 ((not tb) -1)
6218 ((string-lessp ta tb) -1)
6219 ((string-lessp tb ta) +1)
6220 (t nil))))
6222 (defsubst org-cmp-tag (a b)
6223 "Compare the string values of the first tags of A and B."
6224 (let ((ta (car (last (get-text-property 1 'tags a))))
6225 (tb (car (last (get-text-property 1 'tags b)))))
6226 (cond ((not ta) +1)
6227 ((not tb) -1)
6228 ((string-lessp ta tb) -1)
6229 ((string-lessp tb ta) +1)
6230 (t nil))))
6232 (defsubst org-cmp-time (a b)
6233 "Compare the time-of-day values of strings A and B."
6234 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6235 (ta (or (get-text-property 1 'time-of-day a) def))
6236 (tb (or (get-text-property 1 'time-of-day b) def)))
6237 (cond ((< ta tb) -1)
6238 ((< tb ta) +1)
6239 (t nil))))
6241 (defsubst org-cmp-habit-p (a b)
6242 "Compare the todo states of strings A and B."
6243 (let ((ha (get-text-property 1 'org-habit-p a))
6244 (hb (get-text-property 1 'org-habit-p b)))
6245 (cond ((and ha (not hb)) -1)
6246 ((and (not ha) hb) +1)
6247 (t nil))))
6249 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
6251 (defun org-entries-lessp (a b)
6252 "Predicate for sorting agenda entries."
6253 ;; The following variables will be used when the form is evaluated.
6254 ;; So even though the compiler complains, keep them.
6255 (let* ((ss org-agenda-sorting-strategy-selected)
6256 (time-up (and (org-em 'time-up 'time-down ss)
6257 (org-cmp-time a b)))
6258 (time-down (if time-up (- time-up) nil))
6259 (priority-up (and (org-em 'priority-up 'priority-down ss)
6260 (org-cmp-priority a b)))
6261 (priority-down (if priority-up (- priority-up) nil))
6262 (effort-up (and (org-em 'effort-up 'effort-down ss)
6263 (org-cmp-effort a b)))
6264 (effort-down (if effort-up (- effort-up) nil))
6265 (category-up (and (or (org-em 'category-up 'category-down ss)
6266 (memq 'category-keep ss))
6267 (org-cmp-category a b)))
6268 (category-down (if category-up (- category-up) nil))
6269 (category-keep (if category-up +1 nil))
6270 (tag-up (and (org-em 'tag-up 'tag-down ss)
6271 (org-cmp-tag a b)))
6272 (tag-down (if tag-up (- tag-up) nil))
6273 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6274 (org-cmp-todo-state a b)))
6275 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6276 (habit-up (and (org-em 'habit-up 'habit-down ss)
6277 (org-cmp-habit-p a b)))
6278 (habit-down (if habit-up (- habit-up) nil))
6279 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6280 (org-cmp-alpha a b)))
6281 (alpha-down (if alpha-up (- alpha-up) nil))
6282 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6283 user-defined-up user-defined-down)
6284 (if (and need-user-cmp org-agenda-cmp-user-defined
6285 (functionp org-agenda-cmp-user-defined))
6286 (setq user-defined-up
6287 (funcall org-agenda-cmp-user-defined a b)
6288 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6289 (cdr (assoc
6290 (eval (cons 'or org-agenda-sorting-strategy-selected))
6291 '((-1 . t) (1 . nil) (nil . nil))))))
6293 ;;; Agenda restriction lock
6295 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6296 "Overlay to mark the headline to which agenda commands are restricted.")
6297 (overlay-put org-agenda-restriction-lock-overlay
6298 'face 'org-agenda-restriction-lock)
6299 (overlay-put org-agenda-restriction-lock-overlay
6300 'help-echo "Agendas are currently limited to this subtree.")
6301 (org-detach-overlay org-agenda-restriction-lock-overlay)
6303 (defun org-agenda-set-restriction-lock (&optional type)
6304 "Set restriction lock for agenda, to current subtree or file.
6305 Restriction will be the file if TYPE is `file', or if type is the
6306 universal prefix '(4), or if the cursor is before the first headline
6307 in the file. Otherwise, restriction will be to the current subtree."
6308 (interactive "P")
6309 (and (equal type '(4)) (setq type 'file))
6310 (setq type (cond
6311 (type type)
6312 ((org-at-heading-p) 'subtree)
6313 ((condition-case nil (org-back-to-heading t) (error nil))
6314 'subtree)
6315 (t 'file)))
6316 (if (eq type 'subtree)
6317 (progn
6318 (setq org-agenda-restrict t)
6319 (setq org-agenda-overriding-restriction 'subtree)
6320 (put 'org-agenda-files 'org-restrict
6321 (list (buffer-file-name (buffer-base-buffer))))
6322 (org-back-to-heading t)
6323 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6324 (move-marker org-agenda-restrict-begin (point))
6325 (move-marker org-agenda-restrict-end
6326 (save-excursion (org-end-of-subtree t)))
6327 (message "Locking agenda restriction to subtree"))
6328 (put 'org-agenda-files 'org-restrict
6329 (list (buffer-file-name (buffer-base-buffer))))
6330 (setq org-agenda-restrict nil)
6331 (setq org-agenda-overriding-restriction 'file)
6332 (move-marker org-agenda-restrict-begin nil)
6333 (move-marker org-agenda-restrict-end nil)
6334 (message "Locking agenda restriction to file"))
6335 (setq current-prefix-arg nil)
6336 (org-agenda-maybe-redo))
6338 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6339 "Remove the agenda restriction lock."
6340 (interactive "P")
6341 (org-detach-overlay org-agenda-restriction-lock-overlay)
6342 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6343 (setq org-agenda-overriding-restriction nil)
6344 (setq org-agenda-restrict nil)
6345 (put 'org-agenda-files 'org-restrict nil)
6346 (move-marker org-agenda-restrict-begin nil)
6347 (move-marker org-agenda-restrict-end nil)
6348 (setq current-prefix-arg nil)
6349 (message "Agenda restriction lock removed")
6350 (or noupdate (org-agenda-maybe-redo)))
6352 (defun org-agenda-maybe-redo ()
6353 "If there is any window showing the agenda view, update it."
6354 (let ((w (get-buffer-window org-agenda-buffer-name t))
6355 (w0 (selected-window)))
6356 (when w
6357 (select-window w)
6358 (org-agenda-redo)
6359 (select-window w0)
6360 (if org-agenda-overriding-restriction
6361 (message "Agenda view shifted to new %s restriction"
6362 org-agenda-overriding-restriction)
6363 (message "Agenda restriction lock removed")))))
6365 ;;; Agenda commands
6367 (defun org-agenda-check-type (error &rest types)
6368 "Check if agenda buffer is of allowed type.
6369 If ERROR is non-nil, throw an error, otherwise just return nil."
6370 (if (memq org-agenda-type types)
6372 (if error
6373 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6374 nil)))
6376 (defun org-agenda-Quit (&optional arg)
6377 "Exit agenda by removing the window or the buffer"
6378 (interactive)
6379 (if org-agenda-columns-active
6380 (org-columns-quit)
6381 (let ((buf (current-buffer)))
6382 (if (eq org-agenda-window-setup 'other-frame)
6383 (progn
6384 (org-agenda-reset-markers)
6385 (kill-buffer buf)
6386 (org-columns-remove-overlays)
6387 (setq org-agenda-archives-mode nil)
6388 (delete-frame))
6389 (and (not (eq org-agenda-window-setup 'current-window))
6390 (not (one-window-p))
6391 (delete-window))
6392 (org-agenda-reset-markers)
6393 (kill-buffer buf)
6394 (org-columns-remove-overlays)
6395 (setq org-agenda-archives-mode nil)))
6396 ;; Maybe restore the pre-agenda window configuration.
6397 (and org-agenda-restore-windows-after-quit
6398 (not (eq org-agenda-window-setup 'other-frame))
6399 org-pre-agenda-window-conf
6400 (set-window-configuration org-pre-agenda-window-conf))))
6402 (defun org-agenda-quit ()
6403 "Exit agenda by killing agenda buffer or burying it when
6404 `org-agenda-sticky' is non-NIL"
6405 (interactive)
6406 (if org-agenda-columns-active
6407 (org-columns-quit)
6408 (if org-agenda-sticky
6409 (let ((buf (current-buffer)))
6410 (if (eq org-agenda-window-setup 'other-frame)
6411 (progn
6412 (delete-frame))
6413 (and (not (eq org-agenda-window-setup 'current-window))
6414 (not (one-window-p))
6415 (delete-window)))
6416 (with-current-buffer buf
6417 (bury-buffer)
6418 ;; Maybe restore the pre-agenda window configuration.
6419 (and org-agenda-restore-windows-after-quit
6420 (not (eq org-agenda-window-setup 'other-frame))
6421 org-pre-agenda-window-conf
6422 (set-window-configuration org-pre-agenda-window-conf))))
6423 (org-agenda-Quit))))
6425 (defun org-agenda-exit ()
6426 "Exit agenda by removing the window or the buffer.
6427 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6428 Org-mode buffers visited directly by the user will not be touched."
6429 (interactive)
6430 (org-release-buffers org-agenda-new-buffers)
6431 (setq org-agenda-new-buffers nil)
6432 (org-agenda-Quit))
6434 (defun org-agenda-kill-all-agenda-buffers ()
6435 "Kill all buffers in `org-agena-mode'.
6436 This is used when toggling sticky agendas. You can also explicitly invoke it
6437 with `C-c a C-k'."
6438 (interactive)
6439 (let (blist)
6440 (dolist (buf (buffer-list))
6441 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
6442 (push buf blist)))
6443 (mapc 'kill-buffer blist)))
6445 (defun org-agenda-execute (arg)
6446 "Execute another agenda command, keeping same window.
6447 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6448 in the agenda."
6449 (interactive "P")
6450 (let ((org-agenda-window-setup 'current-window))
6451 (org-agenda arg)))
6453 (defun org-agenda-redo ()
6454 "Rebuild Agenda.
6455 When this is the global TODO list, a prefix argument will be interpreted."
6456 (interactive)
6457 (let* ((org-agenda-doing-sticky-redo org-agenda-sticky)
6458 (org-agenda-sticky nil)
6459 (org-agenda-buffer-name (or org-agenda-this-buffer-name
6460 org-agenda-buffer-name))
6461 (org-agenda-keep-modes t)
6462 (tag-filter org-agenda-tag-filter)
6463 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
6464 (top-cat-filter org-agenda-top-category-filter)
6465 (cat-filter org-agenda-category-filter)
6466 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6467 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
6468 (cols org-agenda-columns-active)
6469 (line (org-current-line))
6470 (window-line (- line (org-current-line (window-start))))
6471 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6472 (put 'org-agenda-tag-filter :preset-filter nil)
6473 (put 'org-agenda-category-filter :preset-filter nil)
6474 (and cols (org-columns-quit))
6475 (message "Rebuilding agenda buffer...")
6476 (org-let lprops '(eval org-agenda-redo-command))
6477 (setq org-agenda-undo-list nil
6478 org-agenda-pending-undo-list nil)
6479 (message "Rebuilding agenda buffer...done")
6480 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6481 (put 'org-agenda-category-filter :preset-filter cat-preset)
6482 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6483 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
6484 (and top-cat-filter (org-agenda-filter-top-category-apply top-cat-filter))
6485 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6486 (org-goto-line line)
6487 (recenter window-line)))
6489 (defvar org-global-tags-completion-table nil)
6490 (defvar org-agenda-filter-form nil)
6491 (defvar org-agenda-filtered-by-category nil)
6493 (defun org-agenda-filter-by-category (strip)
6494 "Keep only those lines in the agenda buffer that have a specific category.
6495 The category is that of the current line."
6496 (interactive "P")
6497 (if org-agenda-filtered-by-category
6498 (org-agenda-filter-show-all-cat)
6499 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6500 (if cat (org-agenda-filter-apply
6501 (list (concat (if strip "-" "+") cat)) 'category)
6502 (error "No category at point")))))
6504 (defun org-find-top-category (&optional pos)
6505 (save-excursion
6506 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
6507 (if pos (goto-char pos))
6508 ;; Skip up to the topmost parent
6509 (while (ignore-errors (outline-up-heading 1) t))
6510 (ignore-errors
6511 (nth 4 (org-heading-components))))))
6513 (defvar org-agenda-filtered-by-top-category nil)
6515 (defun org-agenda-filter-by-top-category (strip)
6516 "Keep only those lines in the agenda buffer that have a specific category.
6517 The category is that of the current line."
6518 (interactive "P")
6519 (if org-agenda-filtered-by-top-category
6520 (progn
6521 (setq org-agenda-filtered-by-top-category nil
6522 org-agenda-top-category-filter nil)
6523 (org-agenda-filter-show-all-cat))
6524 (let ((cat (org-find-top-category (org-get-at-bol 'org-hd-marker))))
6525 (if cat (org-agenda-filter-top-category-apply cat strip)
6526 (error "No top-level category at point")))))
6528 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6529 "Keep only those lines in the agenda buffer that have a specific tag.
6530 The tag is selected with its fast selection letter, as configured.
6531 With prefix argument STRIP, remove all lines that do have the tag.
6532 A lisp caller can specify CHAR. NARROW means that the new tag should be
6533 used to narrow the search - the interactive user can also press `-' or `+'
6534 to switch to narrowing."
6535 (interactive "P")
6536 (let* ((alist org-tag-alist-for-agenda)
6537 (tag-chars (mapconcat
6538 (lambda (x) (if (and (not (symbolp (car x)))
6539 (cdr x))
6540 (char-to-string (cdr x))
6541 ""))
6542 alist ""))
6543 (efforts (org-split-string
6544 (or (cdr (assoc (concat org-effort-property "_ALL")
6545 org-global-properties))
6546 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6547 "")))
6548 (effort-op org-agenda-filter-effort-default-operator)
6549 (effort-prompt "")
6550 (inhibit-read-only t)
6551 (current org-agenda-tag-filter)
6552 maybe-refresh a n tag)
6553 (unless char
6554 (message
6555 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6556 (if narrow "Narrow" "Filter") tag-chars
6557 (if org-agenda-auto-exclude-function "[RET], " ""))
6558 (setq char (read-char-exclusive)))
6559 (when (member char '(?+ ?-))
6560 ;; Narrowing down
6561 (cond ((equal char ?-) (setq strip t narrow t))
6562 ((equal char ?+) (setq strip nil narrow t)))
6563 (message
6564 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6565 (setq char (read-char-exclusive)))
6566 (when (member char '(?< ?> ?= ??))
6567 ;; An effort operator
6568 (setq effort-op (char-to-string char))
6569 (setq alist nil) ; to make sure it will be interpreted as effort.
6570 (unless (equal char ??)
6571 (loop for i from 0 to 9 do
6572 (setq effort-prompt
6573 (concat
6574 effort-prompt " ["
6575 (if (= i 9) "0" (int-to-string (1+ i)))
6576 "]" (nth i efforts))))
6577 (message "Effort%s: %s " effort-op effort-prompt)
6578 (setq char (read-char-exclusive))
6579 (when (or (< char ?0) (> char ?9))
6580 (error "Need 1-9,0 to select effort"))))
6581 (when (equal char ?\t)
6582 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6583 (org-set-local 'org-global-tags-completion-table
6584 (org-global-tags-completion-table)))
6585 (let ((completion-ignore-case t))
6586 (setq tag (org-icompleting-read
6587 "Tag: " org-global-tags-completion-table))))
6588 (cond
6589 ((equal char ?\r)
6590 (org-agenda-filter-show-all-tag)
6591 (when org-agenda-auto-exclude-function
6592 (setq org-agenda-tag-filter '())
6593 (dolist (tag (org-agenda-get-represented-tags))
6594 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6595 (if modifier
6596 (push modifier org-agenda-tag-filter))))
6597 (if (not (null org-agenda-tag-filter))
6598 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
6599 (setq maybe-refresh t))
6600 ((equal char ?/)
6601 (org-agenda-filter-show-all-tag)
6602 (when (get 'org-agenda-tag-filter :preset-filter)
6603 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
6604 (setq maybe-refresh t))
6605 ((equal char ?. )
6606 (setq org-agenda-tag-filter
6607 (mapcar (lambda(tag) (concat "+" tag))
6608 (org-get-at-bol 'tags)))
6609 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6610 (setq maybe-refresh t))
6611 ((or (equal char ?\ )
6612 (setq a (rassoc char alist))
6613 (and (>= char ?0) (<= char ?9)
6614 (setq n (if (= char ?0) 9 (- char ?0 1))
6615 tag (concat effort-op (nth n efforts))
6616 a (cons tag nil)))
6617 (and (= char ??)
6618 (setq tag "?eff")
6619 a (cons tag nil))
6620 (and tag (setq a (cons tag nil))))
6621 (org-agenda-filter-show-all-tag)
6622 (setq tag (car a))
6623 (setq org-agenda-tag-filter
6624 (cons (concat (if strip "-" "+") tag)
6625 (if narrow current nil)))
6626 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6627 (setq maybe-refresh t))
6628 (t (error "Invalid tag selection character %c" char)))
6629 (when (and maybe-refresh
6630 (eq org-agenda-clockreport-mode 'with-filter))
6631 (org-agenda-redo))))
6633 (defun org-agenda-get-represented-tags ()
6634 "Get a list of all tags currently represented in the agenda."
6635 (let (p tags)
6636 (save-excursion
6637 (goto-char (point-min))
6638 (while (setq p (next-single-property-change (point) 'tags))
6639 (goto-char p)
6640 (mapc (lambda (x) (add-to-list 'tags x))
6641 (get-text-property (point) 'tags))))
6642 tags))
6644 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6645 "Refine the current filter. See `org-agenda-filter-by-tag'."
6646 (interactive "P")
6647 (org-agenda-filter-by-tag strip char 'refine))
6649 (defun org-agenda-filter-make-matcher ()
6650 "Create the form that tests a line for agenda filter."
6651 (let (f f1)
6652 ;; first compute the tag-filter matcher
6653 (dolist (x (delete-dups
6654 (append (get 'org-agenda-tag-filter
6655 :preset-filter) org-agenda-tag-filter)))
6656 (if (member x '("-" "+"))
6657 (setq f1 (if (equal x "-") 'tags '(not tags)))
6658 (if (string-match "[<=>?]" x)
6659 (setq f1 (org-agenda-filter-effort-form x))
6660 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6661 (if (equal (string-to-char x) ?-)
6662 (setq f1 (list 'not f1))))
6663 (push f1 f))
6664 ;; then compute the category-filter matcher
6665 (dolist (x (delete-dups
6666 (append (get 'org-agenda-category-filter
6667 :preset-filter) org-agenda-category-filter)))
6668 (if (equal "-" (substring x 0 1))
6669 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
6670 (setq f1 (list 'equal (substring x 1) 'cat)))
6671 (push f1 f))
6672 (cons 'and (nreverse f))))
6674 (defun org-agenda-filter-effort-form (e)
6675 "Return the form to compare the effort of the current line with what E says.
6676 E looks like \"+<2:25\"."
6677 (let (op)
6678 (setq e (substring e 1))
6679 (setq op (string-to-char e) e (substring e 1))
6680 (setq op (cond ((equal op ?<) '<=)
6681 ((equal op ?>) '>=)
6682 ((equal op ??) op)
6683 (t '=)))
6684 (list 'org-agenda-compare-effort (list 'quote op)
6685 (org-duration-string-to-minutes e))))
6687 (defun org-agenda-compare-effort (op value)
6688 "Compare the effort of the current line with VALUE, using OP.
6689 If the line does not have an effort defined, return nil."
6690 (let ((eff (org-get-at-bol 'effort-minutes)))
6691 (if (equal op ??)
6692 (not eff)
6693 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6694 value))))
6696 (defun org-agenda-filter-apply (filter type)
6697 "Set FILTER as the new agenda filter and apply it."
6698 (let (tags cat)
6699 (if (eq type 'tag)
6700 (setq org-agenda-tag-filter filter)
6701 (setq org-agenda-category-filter filter))
6702 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
6703 (if (and (eq type 'category)
6704 (not (equal (substring (car filter) 0 1) "-")))
6705 ;; Only set `org-agenda-filtered-by-category' to t
6706 ;; when a unique category is used as the filter
6707 (setq org-agenda-filtered-by-category t))
6708 (org-agenda-set-mode-name)
6709 (save-excursion
6710 (goto-char (point-min))
6711 (while (not (eobp))
6712 (if (org-get-at-bol 'org-marker)
6713 (progn
6714 (setq tags (org-get-at-bol 'tags) ; used in eval
6715 cat (get-text-property (point) 'org-category))
6716 (if (not (eval org-agenda-filter-form))
6717 (org-agenda-filter-hide-line type))
6718 (beginning-of-line 2))
6719 (beginning-of-line 2))))
6720 (if (get-char-property (point) 'invisible)
6721 (ignore-errors (org-agenda-previous-line)))))
6723 (defun org-agenda-filter-top-category-apply (category &optional negative)
6724 "Set FILTER as the new agenda filter and apply it."
6725 (org-agenda-set-mode-name)
6726 (save-excursion
6727 (goto-char (point-min))
6728 (while (not (eobp))
6729 (let* ((pos (org-get-at-bol 'org-hd-marker))
6730 (topcat (and pos (org-find-top-category pos))))
6731 (if (and topcat (funcall (if negative 'identity 'not)
6732 (string= category topcat)))
6733 (org-agenda-filter-hide-line 'category)))
6734 (beginning-of-line 2)))
6735 (if (get-char-property (point) 'invisible)
6736 (org-agenda-previous-line))
6737 (setq org-agenda-top-category-filter category
6738 org-agenda-filtered-by-top-category t))
6740 (defun org-agenda-filter-hide-line (type)
6741 (let (ov)
6742 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6743 (point-at-eol)))
6744 (overlay-put ov 'invisible t)
6745 (overlay-put ov 'type type)
6746 (if (eq type 'tag)
6747 (push ov org-agenda-tag-filter-overlays)
6748 (push ov org-agenda-cat-filter-overlays))))
6750 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6751 (setq pos (or pos (point)))
6752 (save-excursion
6753 (dolist (ov (overlays-at pos))
6754 (when (and (overlay-get ov 'invisible)
6755 (eq (overlay-get ov 'type) 'tag))
6756 (goto-char pos)
6757 (if (< (overlay-start ov) (point-at-eol))
6758 (move-overlay ov (point-at-eol)
6759 (overlay-end ov)))))))
6761 (defun org-agenda-filter-show-all-tag nil
6762 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
6763 (setq org-agenda-tag-filter-overlays nil
6764 org-agenda-tag-filter nil
6765 org-agenda-filter-form nil)
6766 (org-agenda-set-mode-name))
6768 (defun org-agenda-filter-show-all-cat nil
6769 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
6770 (setq org-agenda-cat-filter-overlays nil
6771 org-agenda-filtered-by-category nil
6772 org-agenda-category-filter nil
6773 org-agenda-filter-form nil)
6774 (org-agenda-set-mode-name))
6776 (defun org-agenda-manipulate-query-add ()
6777 "Manipulate the query by adding a search term with positive selection.
6778 Positive selection means the term must be matched for selection of an entry."
6779 (interactive)
6780 (org-agenda-manipulate-query ?\[))
6781 (defun org-agenda-manipulate-query-subtract ()
6782 "Manipulate the query by adding a search term with negative selection.
6783 Negative selection means term must not be matched for selection of an entry."
6784 (interactive)
6785 (org-agenda-manipulate-query ?\]))
6786 (defun org-agenda-manipulate-query-add-re ()
6787 "Manipulate the query by adding a search regexp with positive selection.
6788 Positive selection means the regexp must match for selection of an entry."
6789 (interactive)
6790 (org-agenda-manipulate-query ?\{))
6791 (defun org-agenda-manipulate-query-subtract-re ()
6792 "Manipulate the query by adding a search regexp with negative selection.
6793 Negative selection means regexp must not match for selection of an entry."
6794 (interactive)
6795 (org-agenda-manipulate-query ?\}))
6796 (defun org-agenda-manipulate-query (char)
6797 (cond
6798 ((memq org-agenda-type '(timeline agenda))
6799 (let ((org-agenda-include-inactive-timestamps t))
6800 (org-agenda-redo))
6801 (message "Display now includes inactive timestamps as well"))
6802 ((eq org-agenda-type 'search)
6803 (org-add-to-string
6804 'org-agenda-query-string
6805 (if org-agenda-last-search-view-search-was-boolean
6806 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6807 (?\{ . " +{}") (?\} . " -{}"))))
6808 " "))
6809 (setq org-agenda-redo-command
6810 (list 'org-search-view
6811 org-todo-only
6812 org-agenda-query-string
6813 (+ (length org-agenda-query-string)
6814 (if (member char '(?\{ ?\})) 0 1))))
6815 (set-register org-agenda-query-register org-agenda-query-string)
6816 (org-agenda-redo))
6817 (t (error "Cannot manipulate query for %s-type agenda buffers"
6818 org-agenda-type))))
6820 (defun org-add-to-string (var string)
6821 (set var (concat (symbol-value var) string)))
6823 (defun org-agenda-goto-date (date)
6824 "Jump to DATE in agenda."
6825 (interactive (list (let ((org-read-date-prefer-future
6826 (eval org-agenda-jump-prefer-future)))
6827 (org-read-date))))
6828 (org-agenda-list nil date))
6830 (defun org-agenda-goto-today ()
6831 "Go to today."
6832 (interactive)
6833 (org-agenda-check-type t 'timeline 'agenda)
6834 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6835 (cond
6836 (tdpos (goto-char tdpos))
6837 ((eq org-agenda-type 'agenda)
6838 (let* ((sd (org-agenda-compute-starting-span
6839 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6840 (org-agenda-overriding-arguments org-agenda-last-arguments))
6841 (setf (nth 1 org-agenda-overriding-arguments) sd)
6842 (org-agenda-redo)
6843 (org-agenda-find-same-or-today-or-agenda)))
6844 (t (error "Cannot find today")))))
6846 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6847 (goto-char
6848 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6849 (text-property-any (point-min) (point-max) 'org-today t)
6850 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6851 (point-min))))
6853 (defun org-agenda-later (arg)
6854 "Go forward in time by thee current span.
6855 With prefix ARG, go forward that many times the current span."
6856 (interactive "p")
6857 (org-agenda-check-type t 'agenda)
6858 (let* ((span org-agenda-current-span)
6859 (sd org-starting-day)
6860 (greg (calendar-gregorian-from-absolute sd))
6861 (cnt (org-get-at-bol 'org-day-cnt))
6862 greg2)
6863 (cond
6864 ((eq span 'day)
6865 (setq sd (+ arg sd)))
6866 ((eq span 'week)
6867 (setq sd (+ (* 7 arg) sd)))
6868 ((eq span 'month)
6869 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6870 sd (calendar-absolute-from-gregorian greg2))
6871 (setcar greg2 (1+ (car greg2))))
6872 ((eq span 'year)
6873 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6874 sd (calendar-absolute-from-gregorian greg2))
6875 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6877 (setq sd (+ (* span arg) sd))))
6878 (let ((org-agenda-overriding-arguments
6879 (list (car org-agenda-last-arguments) sd span t)))
6880 (org-agenda-redo)
6881 (org-agenda-find-same-or-today-or-agenda cnt))))
6883 (defun org-agenda-earlier (arg)
6884 "Go backward in time by the current span.
6885 With prefix ARG, go backward that many times the current span."
6886 (interactive "p")
6887 (org-agenda-later (- arg)))
6889 (defun org-agenda-view-mode-dispatch ()
6890 "Call one of the view mode commands."
6891 (interactive)
6892 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6893 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6894 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6895 [E]ntryText")
6896 (let ((a (read-char-exclusive)))
6897 (case a
6898 (?\ (call-interactively 'org-agenda-reset-view))
6899 (?d (call-interactively 'org-agenda-day-view))
6900 (?w (call-interactively 'org-agenda-week-view))
6901 (?m (call-interactively 'org-agenda-month-view))
6902 (?y (call-interactively 'org-agenda-year-view))
6903 (?l (call-interactively 'org-agenda-log-mode))
6904 (?L (org-agenda-log-mode '(4)))
6905 (?c (org-agenda-log-mode 'clockcheck))
6906 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6907 (?a (call-interactively 'org-agenda-archives-mode))
6908 (?A (org-agenda-archives-mode 'files))
6909 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6910 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6911 (?G (call-interactively 'org-agenda-toggle-time-grid))
6912 (?D (call-interactively 'org-agenda-toggle-diary))
6913 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6914 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6915 (org-agenda-check-type t 'timeline 'agenda)
6916 (org-agenda-redo))
6917 (message "Display now includes inactive timestamps as well"))
6918 (?q (message "Abort"))
6919 (otherwise (error "Invalid key" )))))
6921 (defun org-agenda-reset-view ()
6922 "Switch to default view for agenda."
6923 (interactive)
6924 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6925 (defun org-agenda-day-view (&optional day-of-year)
6926 "Switch to daily view for agenda.
6927 With argument DAY-OF-YEAR, switch to that day of the year."
6928 (interactive "P")
6929 (org-agenda-change-time-span 'day day-of-year))
6930 (defun org-agenda-week-view (&optional iso-week)
6931 "Switch to daily view for agenda.
6932 With argument ISO-WEEK, switch to the corresponding ISO week.
6933 If ISO-WEEK has more then 2 digits, only the last two encode the
6934 week. Any digits before this encode a year. So 200712 means
6935 week 12 of year 2007. Years in the range 1938-2037 can also be
6936 written as 2-digit years."
6937 (interactive "P")
6938 (org-agenda-change-time-span 'week iso-week))
6939 (defun org-agenda-month-view (&optional month)
6940 "Switch to monthly view for agenda.
6941 With argument MONTH, switch to that month."
6942 (interactive "P")
6943 (org-agenda-change-time-span 'month month))
6944 (defun org-agenda-year-view (&optional year)
6945 "Switch to yearly view for agenda.
6946 With argument YEAR, switch to that year.
6947 If MONTH has more then 2 digits, only the last two encode the
6948 month. Any digits before this encode a year. So 200712 means
6949 December year 2007. Years in the range 1938-2037 can also be
6950 written as 2-digit years."
6951 (interactive "P")
6952 (when year
6953 (setq year (org-small-year-to-year year)))
6954 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6955 (org-agenda-change-time-span 'year year)
6956 (error "Abort")))
6958 (defun org-agenda-change-time-span (span &optional n)
6959 "Change the agenda view to SPAN.
6960 SPAN may be `day', `week', `month', `year'."
6961 (org-agenda-check-type t 'agenda)
6962 (if (and (not n) (equal org-agenda-current-span span))
6963 (error "Viewing span is already \"%s\"" span))
6964 (let* ((sd (or (org-get-at-bol 'day)
6965 org-starting-day))
6966 (sd (org-agenda-compute-starting-span sd span n))
6967 (org-agenda-overriding-arguments
6968 (or org-agenda-overriding-arguments
6969 (list (car org-agenda-last-arguments) sd span t))))
6970 (org-agenda-redo)
6971 (org-agenda-find-same-or-today-or-agenda))
6972 (org-agenda-set-mode-name)
6973 (message "Switched to %s view" span))
6975 (defun org-agenda-compute-starting-span (sd span &optional n)
6976 "Compute starting date for agenda.
6977 SPAN may be `day', `week', `month', `year'. The return value
6978 is a cons cell with the starting date and the number of days,
6979 so that the date SD will be in that range."
6980 (let* ((greg (calendar-gregorian-from-absolute sd))
6981 (dg (nth 1 greg))
6982 (mg (car greg))
6983 (yg (nth 2 greg)))
6984 (cond
6985 ((eq span 'day)
6986 (when n
6987 (setq sd (+ (calendar-absolute-from-gregorian
6988 (list mg 1 yg))
6989 n -1))))
6990 ((eq span 'week)
6991 (let* ((nt (calendar-day-of-week
6992 (calendar-gregorian-from-absolute sd)))
6993 (d (if org-agenda-start-on-weekday
6994 (- nt org-agenda-start-on-weekday)
6997 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6998 (when n
6999 (require 'cal-iso)
7000 (when (> n 99)
7001 (setq y1 (org-small-year-to-year (/ n 100))
7002 n (mod n 100)))
7003 (setq sd
7004 (calendar-absolute-from-iso
7005 (list n 1
7006 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7007 ((eq span 'month)
7008 (let (y1)
7009 (when (and n (> n 99))
7010 (setq y1 (org-small-year-to-year (/ n 100))
7011 n (mod n 100)))
7012 (setq sd (calendar-absolute-from-gregorian
7013 (list (or n mg) 1 (or y1 yg))))))
7014 ((eq span 'year)
7015 (setq sd (calendar-absolute-from-gregorian
7016 (list 1 1 (or n yg))))))
7017 sd))
7019 (defun org-agenda-next-date-line (&optional arg)
7020 "Jump to the next line indicating a date in agenda buffer."
7021 (interactive "p")
7022 (org-agenda-check-type t 'agenda 'timeline)
7023 (beginning-of-line 1)
7024 ;; This does not work if user makes date format that starts with a blank
7025 (if (looking-at "^\\S-") (forward-char 1))
7026 (if (not (re-search-forward "^\\S-" nil t arg))
7027 (progn
7028 (backward-char 1)
7029 (error "No next date after this line in this buffer")))
7030 (goto-char (match-beginning 0)))
7032 (defun org-agenda-previous-date-line (&optional arg)
7033 "Jump to the previous line indicating a date in agenda buffer."
7034 (interactive "p")
7035 (org-agenda-check-type t 'agenda 'timeline)
7036 (beginning-of-line 1)
7037 (if (not (re-search-backward "^\\S-" nil t arg))
7038 (error "No previous date before this line in this buffer")))
7040 ;; Initialize the highlight
7041 (defvar org-hl (make-overlay 1 1))
7042 (overlay-put org-hl 'face 'highlight)
7044 (defun org-highlight (begin end &optional buffer)
7045 "Highlight a region with overlay."
7046 (move-overlay org-hl begin end (or buffer (current-buffer))))
7048 (defun org-unhighlight ()
7049 "Detach overlay INDEX."
7050 (org-detach-overlay org-hl))
7052 ;; FIXME this is currently not used.
7053 (defun org-highlight-until-next-command (beg end &optional buffer)
7054 "Move the highlight overlay to BEG/END, remove it before the next command."
7055 (org-highlight beg end buffer)
7056 (add-hook 'pre-command-hook 'org-unhighlight-once))
7057 (defun org-unhighlight-once ()
7058 "Remove the highlight from its position, and this function from the hook."
7059 (remove-hook 'pre-command-hook 'org-unhighlight-once)
7060 (org-unhighlight))
7062 (defun org-agenda-follow-mode ()
7063 "Toggle follow mode in an agenda buffer."
7064 (interactive)
7065 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
7066 (org-agenda-set-mode-name)
7067 (org-agenda-do-context-action)
7068 (message "Follow mode is %s"
7069 (if org-agenda-follow-mode "on" "off")))
7071 (defun org-agenda-entry-text-mode (&optional arg)
7072 "Toggle entry text mode in an agenda buffer."
7073 (interactive "P")
7074 (setq org-agenda-entry-text-mode (or (integerp arg)
7075 (not org-agenda-entry-text-mode)))
7076 (org-agenda-entry-text-hide)
7077 (and org-agenda-entry-text-mode
7078 (let ((org-agenda-entry-text-maxlines
7079 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7080 (org-agenda-entry-text-show)))
7081 (org-agenda-set-mode-name)
7082 (message "Entry text mode is %s. Maximum number of lines is %d"
7083 (if org-agenda-entry-text-mode "on" "off")
7084 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7086 (defun org-agenda-clockreport-mode (&optional with-filter)
7087 "Toggle clocktable mode in an agenda buffer.
7088 With prefix arg WITH-FILTER, make the clocktable respect the current
7089 agenda filter."
7090 (interactive "P")
7091 (org-agenda-check-type t 'agenda)
7092 (if with-filter
7093 (setq org-agenda-clockreport-mode 'with-filter)
7094 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
7095 (org-agenda-set-mode-name)
7096 (org-agenda-redo)
7097 (message "Clocktable mode is %s"
7098 (if org-agenda-clockreport-mode "on" "off")))
7100 (defun org-agenda-log-mode (&optional special)
7101 "Toggle log mode in an agenda buffer.
7102 With argument SPECIAL, show all possible log items, not only the ones
7103 configured in `org-agenda-log-mode-items'.
7104 With a double `C-u' prefix arg, show *only* log items, nothing else."
7105 (interactive "P")
7106 (org-agenda-check-type t 'agenda 'timeline)
7107 (setq org-agenda-show-log
7108 (cond
7109 ((equal special '(16)) 'only)
7110 ((eq special 'clockcheck)
7111 (if (eq org-agenda-show-log 'clockcheck)
7112 nil 'clockcheck))
7113 (special '(closed clock state))
7114 (t (not org-agenda-show-log))))
7115 (org-agenda-set-mode-name)
7116 (org-agenda-redo)
7117 (message "Log mode is %s"
7118 (if org-agenda-show-log "on" "off")))
7120 (defun org-agenda-archives-mode (&optional with-files)
7121 "Toggle inclusion of items in trees marked with :ARCHIVE:.
7122 When called with a prefix argument, include all archive files as well."
7123 (interactive "P")
7124 (setq org-agenda-archives-mode
7125 (if with-files t (if org-agenda-archives-mode nil 'trees)))
7126 (org-agenda-set-mode-name)
7127 (org-agenda-redo)
7128 (message
7129 "%s"
7130 (cond
7131 ((eq org-agenda-archives-mode nil)
7132 "No archives are included")
7133 ((eq org-agenda-archives-mode 'trees)
7134 (format "Trees with :%s: tag are included" org-archive-tag))
7135 ((eq org-agenda-archives-mode t)
7136 (format "Trees with :%s: tag and all active archive files are included"
7137 org-archive-tag)))))
7139 (defun org-agenda-toggle-diary ()
7140 "Toggle diary inclusion in an agenda buffer."
7141 (interactive)
7142 (org-agenda-check-type t 'agenda)
7143 (setq org-agenda-include-diary (not org-agenda-include-diary))
7144 (org-agenda-redo)
7145 (org-agenda-set-mode-name)
7146 (message "Diary inclusion turned %s"
7147 (if org-agenda-include-diary "on" "off")))
7149 (defun org-agenda-toggle-deadlines ()
7150 "Toggle inclusion of entries with a deadline in an agenda buffer."
7151 (interactive)
7152 (org-agenda-check-type t 'agenda)
7153 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
7154 (org-agenda-redo)
7155 (org-agenda-set-mode-name)
7156 (message "Deadlines inclusion turned %s"
7157 (if org-agenda-include-deadlines "on" "off")))
7159 (defun org-agenda-toggle-time-grid ()
7160 "Toggle time grid in an agenda buffer."
7161 (interactive)
7162 (org-agenda-check-type t 'agenda)
7163 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
7164 (org-agenda-redo)
7165 (org-agenda-set-mode-name)
7166 (message "Time-grid turned %s"
7167 (if org-agenda-use-time-grid "on" "off")))
7169 (defun org-agenda-set-mode-name ()
7170 "Set the mode name to indicate all the small mode settings."
7171 (setq mode-name
7172 (list "Org-Agenda"
7173 (if (get 'org-agenda-files 'org-restrict) " []" "")
7175 '(:eval (org-agenda-span-name org-agenda-current-span))
7176 (if org-agenda-follow-mode " Follow" "")
7177 (if org-agenda-entry-text-mode " ETxt" "")
7178 (if org-agenda-include-diary " Diary" "")
7179 (if org-agenda-include-deadlines " Ddl" "")
7180 (if org-agenda-use-time-grid " Grid" "")
7181 (if (and (boundp 'org-habit-show-habits)
7182 org-habit-show-habits) " Habit" "")
7183 (cond
7184 ((consp org-agenda-show-log) " LogAll")
7185 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
7186 (org-agenda-show-log " Log")
7187 (t ""))
7188 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
7189 :preset-filter))
7190 '(:eval (org-propertize
7191 (concat " <"
7192 (mapconcat
7193 'identity
7194 (append
7195 (get 'org-agenda-category-filter :preset-filter)
7196 org-agenda-category-filter)
7198 ">")
7199 'face 'org-agenda-filter-category
7200 'help-echo "Category used in filtering"))
7202 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
7203 :preset-filter))
7204 '(:eval (org-propertize
7205 (concat " {"
7206 (mapconcat
7207 'identity
7208 (append
7209 (get 'org-agenda-tag-filter :preset-filter)
7210 org-agenda-tag-filter)
7212 "}")
7213 'face 'org-agenda-filter-tags
7214 'help-echo "Tags used in filtering"))
7216 (if org-agenda-archives-mode
7217 (if (eq org-agenda-archives-mode t)
7218 " Archives"
7219 (format " :%s:" org-archive-tag))
7221 (if org-agenda-clockreport-mode
7222 (if (eq org-agenda-clockreport-mode 'with-filter)
7223 " Clock{}" " Clock")
7224 "")))
7225 (force-mode-line-update))
7227 (defun org-agenda-post-command-hook ()
7228 (setq org-agenda-type
7229 (or (get-text-property (point) 'org-agenda-type)
7230 (get-text-property (max (point-min) (1- (point)))
7231 'org-agenda-type))))
7233 (defun org-agenda-next-line ()
7234 "Move cursor to the next line, and show if follow mode is active."
7235 (interactive)
7236 (call-interactively 'next-line)
7237 (org-agenda-do-context-action))
7239 (defun org-agenda-previous-line ()
7240 "Move cursor to the previous line, and show if follow-mode is active."
7241 (interactive)
7242 (call-interactively 'previous-line)
7243 (org-agenda-do-context-action))
7245 (defun org-agenda-do-context-action ()
7246 "Show outline path and, maybe, follow mode window."
7247 (let ((m (org-get-at-bol 'org-marker)))
7248 (when (and (markerp m) (marker-buffer m))
7249 (and org-agenda-follow-mode
7250 (if org-agenda-follow-indirect
7251 (org-agenda-tree-to-indirect-buffer)
7252 (org-agenda-show)))
7253 (and org-agenda-show-outline-path
7254 (org-with-point-at m (org-display-outline-path t))))))
7256 (defun org-agenda-show-priority ()
7257 "Show the priority of the current item.
7258 This priority is composed of the main priority given with the [#A] cookies,
7259 and by additional input from the age of a schedules or deadline entry."
7260 (interactive)
7261 (let* ((pri (org-get-at-bol 'priority)))
7262 (message "Priority is %d" (if pri pri -1000))))
7264 (defun org-agenda-show-tags ()
7265 "Show the tags applicable to the current item."
7266 (interactive)
7267 (let* ((tags (org-get-at-bol 'tags)))
7268 (if tags
7269 (message "Tags are :%s:"
7270 (org-no-properties (mapconcat 'identity tags ":")))
7271 (message "No tags associated with this line"))))
7273 (defun org-agenda-goto (&optional highlight)
7274 "Go to the Org-mode file which contains the item at point."
7275 (interactive)
7276 (let* ((marker (or (org-get-at-bol 'org-marker)
7277 (org-agenda-error)))
7278 (buffer (marker-buffer marker))
7279 (pos (marker-position marker)))
7280 (switch-to-buffer-other-window buffer)
7281 (widen)
7282 (push-mark)
7283 (goto-char pos)
7284 (when (derived-mode-p 'org-mode)
7285 (org-show-context 'agenda)
7286 (save-excursion
7287 (and (outline-next-heading)
7288 (org-flag-heading nil)))) ; show the next heading
7289 (when (outline-invisible-p)
7290 (show-entry)) ; display invisible text
7291 (recenter (/ (window-height) 2))
7292 (run-hooks 'org-agenda-after-show-hook)
7293 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
7295 (defvar org-agenda-after-show-hook nil
7296 "Normal hook run after an item has been shown from the agenda.
7297 Point is in the buffer where the item originated.")
7299 (defun org-agenda-kill ()
7300 "Kill the entry or subtree belonging to the current agenda entry."
7301 (interactive)
7302 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7303 (let* ((marker (or (org-get-at-bol 'org-marker)
7304 (org-agenda-error)))
7305 (buffer (marker-buffer marker))
7306 (pos (marker-position marker))
7307 (type (org-get-at-bol 'type))
7308 dbeg dend (n 0) conf)
7309 (org-with-remote-undo buffer
7310 (with-current-buffer buffer
7311 (save-excursion
7312 (goto-char pos)
7313 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
7314 (setq dbeg (progn (org-back-to-heading t) (point))
7315 dend (org-end-of-subtree t t))
7316 (setq dbeg (point-at-bol)
7317 dend (min (point-max) (1+ (point-at-eol)))))
7318 (goto-char dbeg)
7319 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
7320 (setq conf (or (eq t org-agenda-confirm-kill)
7321 (and (numberp org-agenda-confirm-kill)
7322 (> n org-agenda-confirm-kill))))
7323 (and conf
7324 (not (y-or-n-p
7325 (format "Delete entry with %d lines in buffer \"%s\"? "
7326 n (buffer-name buffer))))
7327 (error "Abort"))
7328 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
7329 (with-current-buffer buffer (delete-region dbeg dend))
7330 (message "Agenda item and source killed"))))
7332 (defvar org-archive-default-command)
7333 (defun org-agenda-archive-default ()
7334 "Archive the entry or subtree belonging to the current agenda entry."
7335 (interactive)
7336 (require 'org-archive)
7337 (org-agenda-archive-with org-archive-default-command))
7339 (defun org-agenda-archive-default-with-confirmation ()
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 'confirm))
7345 (defun org-agenda-archive ()
7346 "Archive the entry or subtree belonging to the current agenda entry."
7347 (interactive)
7348 (org-agenda-archive-with 'org-archive-subtree))
7350 (defun org-agenda-archive-to-archive-sibling ()
7351 "Move the entry to the archive sibling."
7352 (interactive)
7353 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7355 (defun org-agenda-archive-with (cmd &optional confirm)
7356 "Move the entry to the archive sibling."
7357 (interactive)
7358 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7359 (let* ((marker (or (org-get-at-bol 'org-marker)
7360 (org-agenda-error)))
7361 (buffer (marker-buffer marker))
7362 (pos (marker-position marker)))
7363 (org-with-remote-undo buffer
7364 (with-current-buffer buffer
7365 (if (derived-mode-p 'org-mode)
7366 (if (and confirm
7367 (not (y-or-n-p "Archive this subtree or entry? ")))
7368 (error "Abort")
7369 (save-excursion
7370 (goto-char pos)
7371 (org-remove-subtree-entries-from-agenda)
7372 (org-back-to-heading t)
7373 (funcall cmd)))
7374 (error "Archiving works only in Org-mode files"))))))
7376 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7377 "Remove all lines in the agenda that correspond to a given subtree.
7378 The subtree is the one in buffer BUF, starting at BEG and ending at END.
7379 If this information is not given, the function uses the tree at point."
7380 (let ((buf (or buf (current-buffer))) m p)
7381 (save-excursion
7382 (unless (and beg end)
7383 (org-back-to-heading t)
7384 (setq beg (point))
7385 (org-end-of-subtree t)
7386 (setq end (point)))
7387 (set-buffer (get-buffer org-agenda-buffer-name))
7388 (save-excursion
7389 (goto-char (point-max))
7390 (beginning-of-line 1)
7391 (while (not (bobp))
7392 (when (and (setq m (org-get-at-bol 'org-marker))
7393 (equal buf (marker-buffer m))
7394 (setq p (marker-position m))
7395 (>= p beg)
7396 (< p end))
7397 (let ((inhibit-read-only t))
7398 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7399 (beginning-of-line 0))))))
7401 (defun org-agenda-refile (&optional goto rfloc no-update)
7402 "Refile the item at point."
7403 (interactive "P")
7404 (if (equal goto '(16))
7405 (org-refile-goto-last-stored)
7406 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7407 (org-agenda-error)))
7408 (buffer (marker-buffer marker))
7409 (pos (marker-position marker))
7410 (rfloc (or rfloc
7411 (org-refile-get-location
7412 (if goto "Goto" "Refile to") buffer
7413 org-refile-allow-creating-parent-nodes))))
7414 (with-current-buffer buffer
7415 (save-excursion
7416 (save-restriction
7417 (widen)
7418 (goto-char marker)
7419 (org-remove-subtree-entries-from-agenda)
7420 (org-refile goto buffer rfloc)))))
7421 (unless no-update (org-agenda-redo))))
7423 (defun org-agenda-open-link (&optional arg)
7424 "Follow the link in the current line, if any.
7425 This looks for a link in the displayed line in the agenda. It also looks
7426 at the text of the entry itself."
7427 (interactive "P")
7428 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7429 (org-get-at-bol 'org-marker)))
7430 (buffer (and marker (marker-buffer marker)))
7431 (prefix (buffer-substring
7432 (point-at-bol) (point-at-eol))))
7433 (cond
7434 (buffer
7435 (with-current-buffer buffer
7436 (save-excursion
7437 (save-restriction
7438 (widen)
7439 (goto-char marker)
7440 (org-offer-links-in-entry arg prefix)))))
7441 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7442 (save-excursion
7443 (beginning-of-line 1)
7444 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7445 (org-open-link-from-string (match-string 1)))
7446 (t (error "No link to open here")))))
7448 (defun org-agenda-copy-local-variable (var)
7449 "Get a variable from a referenced buffer and install it here."
7450 (let ((m (org-get-at-bol 'org-marker)))
7451 (when (and m (buffer-live-p (marker-buffer m)))
7452 (org-set-local var (with-current-buffer (marker-buffer m)
7453 (symbol-value var))))))
7455 (defun org-agenda-switch-to (&optional delete-other-windows)
7456 "Go to the Org-mode file which contains the item at point."
7457 (interactive)
7458 (if (and org-return-follows-link
7459 (not (org-get-at-bol 'org-marker))
7460 (org-in-regexp org-bracket-link-regexp))
7461 (org-open-link-from-string (match-string 0))
7462 (let* ((marker (or (org-get-at-bol 'org-marker)
7463 (org-agenda-error)))
7464 (buffer (marker-buffer marker))
7465 (pos (marker-position marker)))
7466 (org-pop-to-buffer-same-window buffer)
7467 (and delete-other-windows (delete-other-windows))
7468 (widen)
7469 (goto-char pos)
7470 (when (derived-mode-p 'org-mode)
7471 (org-show-context 'agenda)
7472 (save-excursion
7473 (and (outline-next-heading)
7474 (org-flag-heading nil))) ; show the next heading
7475 (when (outline-invisible-p)
7476 (show-entry)))))) ; display invisible text
7478 (defun org-agenda-goto-mouse (ev)
7479 "Go to the Org-mode file which contains the item at the mouse click."
7480 (interactive "e")
7481 (mouse-set-point ev)
7482 (org-agenda-goto))
7484 (defun org-agenda-show (&optional full-entry)
7485 "Display the Org-mode file which contains the item at point.
7486 With prefix argument FULL-ENTRY, make the entire entry visible
7487 if it was hidden in the outline."
7488 (interactive "P")
7489 (let ((win (selected-window)))
7490 (if full-entry
7491 (let ((org-show-entry-below t))
7492 (org-agenda-goto t))
7493 (org-agenda-goto t))
7494 (select-window win)))
7496 (defvar org-agenda-show-window nil)
7497 (defun org-agenda-show-and-scroll-up (&optional arg)
7498 "Display the Org-mode file which contains the item at point.
7499 When called repeatedly, scroll the window that is displaying the buffer.
7500 With a \\[universal-argument] prefix, use `org-show-entry' instead of
7501 `show-subtree' to display the item, so that drawers and logbooks stay
7502 folded."
7503 (interactive "P")
7504 (let ((win (selected-window)))
7505 (if (and (window-live-p org-agenda-show-window)
7506 (eq this-command last-command))
7507 (progn
7508 (select-window org-agenda-show-window)
7509 (ignore-errors (scroll-up)))
7510 (org-agenda-goto t)
7511 (if arg (org-show-entry) (show-subtree))
7512 (setq org-agenda-show-window (selected-window)))
7513 (select-window win)))
7515 (defun org-agenda-show-scroll-down ()
7516 "Scroll down the window showing the agenda."
7517 (interactive)
7518 (let ((win (selected-window)))
7519 (when (window-live-p org-agenda-show-window)
7520 (select-window org-agenda-show-window)
7521 (ignore-errors (scroll-down))
7522 (select-window win))))
7524 (defun org-agenda-show-1 (&optional more)
7525 "Display the Org-mode file which contains the item at point.
7526 The prefix arg selects the amount of information to display:
7528 0 hide the subtree
7529 1 just show the entry according to defaults.
7530 2 show the children view
7531 3 show the subtree view
7532 4 show the entire subtree and any LOGBOOK drawers
7533 5 show the entire subtree and any drawers
7534 With prefix argument FULL-ENTRY, make the entire entry visible
7535 if it was hidden in the outline."
7536 (interactive "p")
7537 (let ((win (selected-window)))
7538 (org-agenda-goto t)
7539 (org-recenter-heading 1)
7540 (cond
7541 ((= more 0)
7542 (hide-subtree)
7543 (save-excursion
7544 (org-back-to-heading)
7545 (run-hook-with-args 'org-cycle-hook 'folded))
7546 (message "Remote: FOLDED"))
7547 ((and (org-called-interactively-p 'any) (= more 1))
7548 (message "Remote: show with default settings"))
7549 ((= more 2)
7550 (show-entry)
7551 (show-children)
7552 (save-excursion
7553 (org-back-to-heading)
7554 (run-hook-with-args 'org-cycle-hook 'children))
7555 (message "Remote: CHILDREN"))
7556 ((= more 3)
7557 (show-subtree)
7558 (save-excursion
7559 (org-back-to-heading)
7560 (run-hook-with-args 'org-cycle-hook 'subtree))
7561 (message "Remote: SUBTREE"))
7562 ((= more 4)
7563 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7564 (org-drawer-regexp
7565 (concat "^[ \t]*:\\("
7566 (mapconcat 'regexp-quote org-drawers "\\|")
7567 "\\):[ \t]*$")))
7568 (show-subtree)
7569 (save-excursion
7570 (org-back-to-heading)
7571 (org-cycle-hide-drawers 'subtree)))
7572 (message "Remote: SUBTREE AND LOGBOOK"))
7573 ((> more 4)
7574 (show-subtree)
7575 (message "Remote: SUBTREE AND ALL DRAWERS")))
7576 (select-window win)))
7578 (defun org-recenter-heading (n)
7579 (save-excursion
7580 (org-back-to-heading)
7581 (recenter n)))
7583 (defvar org-agenda-cycle-counter nil)
7584 (defun org-agenda-cycle-show (&optional n)
7585 "Show the current entry in another window, with default settings.
7586 Default settings are taken from `org-show-hierarchy-above' and siblings.
7587 When use repeatedly in immediate succession, the remote entry will cycle
7588 through visibility
7590 children -> subtree -> folded
7592 When called with a numeric prefix arg, that arg will be passed through to
7593 `org-agenda-show-1'. For the interpretation of that argument, see the
7594 docstring of `org-agenda-show-1'."
7595 (interactive "P")
7596 (if (integerp n)
7597 (setq org-agenda-cycle-counter n)
7598 (if (not (eq last-command this-command))
7599 (setq org-agenda-cycle-counter 1)
7600 (if (equal org-agenda-cycle-counter 0)
7601 (setq org-agenda-cycle-counter 2)
7602 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7603 (if (> org-agenda-cycle-counter 3)
7604 (setq org-agenda-cycle-counter 0)))))
7605 (org-agenda-show-1 org-agenda-cycle-counter))
7607 (defun org-agenda-recenter (arg)
7608 "Display the Org-mode file which contains the item at point and recenter."
7609 (interactive "P")
7610 (let ((win (selected-window)))
7611 (org-agenda-goto t)
7612 (recenter arg)
7613 (select-window win)))
7615 (defun org-agenda-show-mouse (ev)
7616 "Display the Org-mode file which contains the item at the mouse click."
7617 (interactive "e")
7618 (mouse-set-point ev)
7619 (org-agenda-show))
7621 (defun org-agenda-check-no-diary ()
7622 "Check if the entry is a diary link and abort if yes."
7623 (if (org-get-at-bol 'org-agenda-diary-link)
7624 (org-agenda-error)))
7626 (defun org-agenda-error ()
7627 (error "Command not allowed in this line"))
7629 (defun org-agenda-tree-to-indirect-buffer ()
7630 "Show the subtree corresponding to the current entry in an indirect buffer.
7631 This calls the command `org-tree-to-indirect-buffer' from the original
7632 Org-mode buffer.
7633 With numerical prefix arg ARG, go up to this level and then take that tree.
7634 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7635 use the dedicated frame)."
7636 (interactive)
7637 (if (and current-prefix-arg (listp current-prefix-arg))
7638 (org-agenda-do-tree-to-indirect-buffer)
7639 (let ((agenda-window (selected-window))
7640 (indirect-window
7641 (and org-last-indirect-buffer
7642 (get-buffer-window org-last-indirect-buffer))))
7643 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer))
7644 (unwind-protect
7645 (progn
7646 (unless (and indirect-window (window-live-p indirect-window))
7647 (setq indirect-window (split-window agenda-window)))
7648 (select-window indirect-window)
7649 (switch-to-buffer org-last-indirect-buffer :norecord)
7650 (fit-window-to-buffer indirect-window))
7651 (select-window (get-buffer-window org-agenda-buffer-name))))))
7653 (defun org-agenda-do-tree-to-indirect-buffer ()
7654 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7655 (org-agenda-check-no-diary)
7656 (let* ((marker (or (org-get-at-bol 'org-marker)
7657 (org-agenda-error)))
7658 (buffer (marker-buffer marker))
7659 (pos (marker-position marker)))
7660 (with-current-buffer buffer
7661 (save-excursion
7662 (goto-char pos)
7663 (call-interactively 'org-tree-to-indirect-buffer)))))
7665 (defvar org-last-heading-marker (make-marker)
7666 "Marker pointing to the headline that last changed its TODO state
7667 by a remote command from the agenda.")
7669 (defun org-agenda-todo-nextset ()
7670 "Switch TODO entry to next sequence."
7671 (interactive)
7672 (org-agenda-todo 'nextset))
7674 (defun org-agenda-todo-previousset ()
7675 "Switch TODO entry to previous sequence."
7676 (interactive)
7677 (org-agenda-todo 'previousset))
7679 (defun org-agenda-todo (&optional arg)
7680 "Cycle TODO state of line at point, also in Org-mode file.
7681 This changes the line at point, all other lines in the agenda referring to
7682 the same tree node, and the headline of the tree node in the Org-mode file."
7683 (interactive "P")
7684 (org-agenda-check-no-diary)
7685 (let* ((col (current-column))
7686 (marker (or (org-get-at-bol 'org-marker)
7687 (org-agenda-error)))
7688 (buffer (marker-buffer marker))
7689 (pos (marker-position marker))
7690 (hdmarker (org-get-at-bol 'org-hd-marker))
7691 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7692 (inhibit-read-only t)
7693 org-agenda-headline-snapshot-before-repeat newhead just-one)
7694 (org-with-remote-undo buffer
7695 (with-current-buffer buffer
7696 (widen)
7697 (goto-char pos)
7698 (org-show-context 'agenda)
7699 (save-excursion
7700 (and (outline-next-heading)
7701 (org-flag-heading nil))) ; show the next heading
7702 (let ((current-prefix-arg arg))
7703 (call-interactively 'org-todo))
7704 (and (bolp) (forward-char 1))
7705 (setq newhead (org-get-heading))
7706 (when (and (org-bound-and-true-p
7707 org-agenda-headline-snapshot-before-repeat)
7708 (not (equal org-agenda-headline-snapshot-before-repeat
7709 newhead))
7710 todayp)
7711 (setq newhead org-agenda-headline-snapshot-before-repeat
7712 just-one t))
7713 (save-excursion
7714 (org-back-to-heading)
7715 (move-marker org-last-heading-marker (point))))
7716 (beginning-of-line 1)
7717 (save-excursion
7718 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7719 (org-move-to-column col))))
7721 (defun org-agenda-add-note (&optional arg)
7722 "Add a time-stamped note to the entry at point."
7723 (interactive "P")
7724 (org-agenda-check-no-diary)
7725 (let* ((marker (or (org-get-at-bol 'org-marker)
7726 (org-agenda-error)))
7727 (buffer (marker-buffer marker))
7728 (pos (marker-position marker))
7729 (hdmarker (org-get-at-bol 'org-hd-marker))
7730 (inhibit-read-only t))
7731 (with-current-buffer buffer
7732 (widen)
7733 (goto-char pos)
7734 (org-show-context 'agenda)
7735 (save-excursion
7736 (and (outline-next-heading)
7737 (org-flag-heading nil))) ; show the next heading
7738 (org-add-note))))
7740 (defun org-agenda-change-all-lines (newhead hdmarker
7741 &optional fixface just-this)
7742 "Change all lines in the agenda buffer which match HDMARKER.
7743 The new content of the line will be NEWHEAD (as modified by
7744 `org-agenda-format-item'). HDMARKER is checked with
7745 `equal' against all `org-hd-marker' text properties in the file.
7746 If FIXFACE is non-nil, the face of each item is modified according to
7747 the new TODO state.
7748 If JUST-THIS is non-nil, change just the current line, not all.
7749 If FORCE-TAGS is non nil, the car of it returns the new tags."
7750 (let* ((inhibit-read-only t)
7751 (line (org-current-line))
7752 (org-agenda-buffer (current-buffer))
7753 (thetags (with-current-buffer (marker-buffer hdmarker)
7754 (save-excursion (save-restriction (widen)
7755 (goto-char hdmarker)
7756 (org-get-tags-at)))))
7757 props m pl undone-face done-face finish new dotime cat tags)
7758 (save-excursion
7759 (goto-char (point-max))
7760 (beginning-of-line 1)
7761 (while (not finish)
7762 (setq finish (bobp))
7763 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7764 (or (not just-this) (= (org-current-line) line))
7765 (equal m hdmarker))
7766 (setq props (text-properties-at (point))
7767 dotime (org-get-at-bol 'dotime)
7768 cat (org-get-at-bol 'org-category)
7769 tags thetags
7771 (let ((org-prefix-format-compiled
7772 (or (get-text-property (point) 'format)
7773 org-prefix-format-compiled))
7774 (extra (org-get-at-bol 'extra)))
7775 (with-current-buffer (marker-buffer hdmarker)
7776 (save-excursion
7777 (save-restriction
7778 (widen)
7779 (org-agenda-format-item extra newhead cat tags dotime)))))
7780 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7781 undone-face (org-get-at-bol 'undone-face)
7782 done-face (org-get-at-bol 'done-face))
7783 (beginning-of-line 1)
7784 (cond
7785 ((equal new "")
7786 (and (looking-at ".*\n?") (replace-match "")))
7787 ((looking-at ".*")
7788 (replace-match new t t)
7789 (beginning-of-line 1)
7790 (add-text-properties (point-at-bol) (point-at-eol) props)
7791 (when fixface
7792 (add-text-properties
7793 (point-at-bol) (point-at-eol)
7794 (list 'face
7795 (if org-last-todo-state-is-todo
7796 undone-face done-face))))
7797 (org-agenda-highlight-todo 'line)
7798 (beginning-of-line 1))
7799 (t (error "Line update did not work"))))
7800 (beginning-of-line 0)))
7801 (org-finalize-agenda)))
7803 (defun org-agenda-align-tags (&optional line)
7804 "Align all tags in agenda items to `org-agenda-tags-column'."
7805 (let ((inhibit-read-only t) l c)
7806 (save-excursion
7807 (goto-char (if line (point-at-bol) (point-min)))
7808 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7809 (if line (point-at-eol) nil) t)
7810 (add-text-properties
7811 (match-beginning 2) (match-end 2)
7812 (list 'face (delq nil (let ((prop (get-text-property
7813 (match-beginning 2) 'face)))
7814 (or (listp prop) (setq prop (list prop)))
7815 (if (memq 'org-tag prop)
7816 prop
7817 (cons 'org-tag prop))))))
7818 (setq l (- (match-end 2) (match-beginning 2))
7819 c (if (< org-agenda-tags-column 0)
7820 (- (abs org-agenda-tags-column) l)
7821 org-agenda-tags-column))
7822 (delete-region (match-beginning 1) (match-end 1))
7823 (goto-char (match-beginning 1))
7824 (insert (org-add-props
7825 (make-string (max 1 (- c (current-column))) ?\ )
7826 (plist-put (copy-sequence (text-properties-at (point)))
7827 'face nil))))
7828 (goto-char (point-min))
7829 (org-font-lock-add-tag-faces (point-max)))))
7831 (defun org-agenda-priority-up ()
7832 "Increase the priority of line at point, also in Org-mode file."
7833 (interactive)
7834 (org-agenda-priority 'up))
7836 (defun org-agenda-priority-down ()
7837 "Decrease the priority of line at point, also in Org-mode file."
7838 (interactive)
7839 (org-agenda-priority 'down))
7841 (defun org-agenda-priority (&optional force-direction)
7842 "Set the priority of line at point, also in Org-mode file.
7843 This changes the line at point, all other lines in the agenda referring to
7844 the same tree node, and the headline of the tree node in the Org-mode file."
7845 (interactive)
7846 (unless org-enable-priority-commands
7847 (error "Priority commands are disabled"))
7848 (org-agenda-check-no-diary)
7849 (let* ((marker (or (org-get-at-bol 'org-marker)
7850 (org-agenda-error)))
7851 (hdmarker (org-get-at-bol 'org-hd-marker))
7852 (buffer (marker-buffer hdmarker))
7853 (pos (marker-position hdmarker))
7854 (inhibit-read-only t)
7855 newhead)
7856 (org-with-remote-undo buffer
7857 (with-current-buffer buffer
7858 (widen)
7859 (goto-char pos)
7860 (org-show-context 'agenda)
7861 (save-excursion
7862 (and (outline-next-heading)
7863 (org-flag-heading nil))) ; show the next heading
7864 (funcall 'org-priority force-direction)
7865 (end-of-line 1)
7866 (setq newhead (org-get-heading)))
7867 (org-agenda-change-all-lines newhead hdmarker)
7868 (beginning-of-line 1))))
7870 ;; FIXME: should fix the tags property of the agenda line.
7871 (defun org-agenda-set-tags (&optional tag onoff)
7872 "Set tags for the current headline."
7873 (interactive)
7874 (org-agenda-check-no-diary)
7875 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7876 (call-interactively 'org-change-tag-in-region)
7877 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7878 (org-agenda-error)))
7879 (buffer (marker-buffer hdmarker))
7880 (pos (marker-position hdmarker))
7881 (inhibit-read-only t)
7882 newhead)
7883 (org-with-remote-undo buffer
7884 (with-current-buffer buffer
7885 (widen)
7886 (goto-char pos)
7887 (save-excursion
7888 (org-show-context 'agenda))
7889 (save-excursion
7890 (and (outline-next-heading)
7891 (org-flag-heading nil))) ; show the next heading
7892 (goto-char pos)
7893 (if tag
7894 (org-toggle-tag tag onoff)
7895 (call-interactively 'org-set-tags))
7896 (end-of-line 1)
7897 (setq newhead (org-get-heading)))
7898 (org-agenda-change-all-lines newhead hdmarker)
7899 (beginning-of-line 1)))))
7901 (defun org-agenda-set-property ()
7902 "Set a property for the current headline."
7903 (interactive)
7904 (org-agenda-check-no-diary)
7905 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7906 (org-agenda-error)))
7907 (buffer (marker-buffer hdmarker))
7908 (pos (marker-position hdmarker))
7909 (inhibit-read-only t)
7910 newhead)
7911 (org-with-remote-undo buffer
7912 (with-current-buffer buffer
7913 (widen)
7914 (goto-char pos)
7915 (save-excursion
7916 (org-show-context 'agenda))
7917 (save-excursion
7918 (and (outline-next-heading)
7919 (org-flag-heading nil))) ; show the next heading
7920 (goto-char pos)
7921 (call-interactively 'org-set-property)))))
7923 (defun org-agenda-set-effort ()
7924 "Set the effort property for the current headline."
7925 (interactive)
7926 (org-agenda-check-no-diary)
7927 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7928 (org-agenda-error)))
7929 (buffer (marker-buffer hdmarker))
7930 (pos (marker-position hdmarker))
7931 (inhibit-read-only t)
7932 newhead)
7933 (org-with-remote-undo buffer
7934 (with-current-buffer buffer
7935 (widen)
7936 (goto-char pos)
7937 (save-excursion
7938 (org-show-context 'agenda))
7939 (save-excursion
7940 (and (outline-next-heading)
7941 (org-flag-heading nil))) ; show the next heading
7942 (goto-char pos)
7943 (call-interactively 'org-set-effort)
7944 (end-of-line 1)
7945 (setq newhead (org-get-heading)))
7946 (org-agenda-change-all-lines newhead hdmarker))))
7948 (defun org-agenda-toggle-archive-tag ()
7949 "Toggle the archive tag for the current entry."
7950 (interactive)
7951 (org-agenda-check-no-diary)
7952 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7953 (org-agenda-error)))
7954 (buffer (marker-buffer hdmarker))
7955 (pos (marker-position hdmarker))
7956 (inhibit-read-only t)
7957 newhead)
7958 (org-with-remote-undo buffer
7959 (with-current-buffer buffer
7960 (widen)
7961 (goto-char pos)
7962 (org-show-context 'agenda)
7963 (save-excursion
7964 (and (outline-next-heading)
7965 (org-flag-heading nil))) ; show the next heading
7966 (call-interactively 'org-toggle-archive-tag)
7967 (end-of-line 1)
7968 (setq newhead (org-get-heading)))
7969 (org-agenda-change-all-lines newhead hdmarker)
7970 (beginning-of-line 1))))
7972 (defun org-agenda-do-date-later (arg)
7973 (interactive "P")
7974 (cond
7975 ((or (equal arg '(16))
7976 (memq last-command
7977 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7978 (setq this-command 'org-agenda-date-later-minutes)
7979 (org-agenda-date-later-minutes 1))
7980 ((or (equal arg '(4))
7981 (memq last-command
7982 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7983 (setq this-command 'org-agenda-date-later-hours)
7984 (org-agenda-date-later-hours 1))
7986 (org-agenda-date-later (prefix-numeric-value arg)))))
7988 (defun org-agenda-do-date-earlier (arg)
7989 (interactive "P")
7990 (cond
7991 ((or (equal arg '(16))
7992 (memq last-command
7993 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7994 (setq this-command 'org-agenda-date-earlier-minutes)
7995 (org-agenda-date-earlier-minutes 1))
7996 ((or (equal arg '(4))
7997 (memq last-command
7998 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7999 (setq this-command 'org-agenda-date-earlier-hours)
8000 (org-agenda-date-earlier-hours 1))
8002 (org-agenda-date-earlier (prefix-numeric-value arg)))))
8004 (defun org-agenda-date-later (arg &optional what)
8005 "Change the date of this item to ARG day(s) later."
8006 (interactive "p")
8007 (org-agenda-check-type t 'agenda 'timeline)
8008 (org-agenda-check-no-diary)
8009 (let* ((marker (or (org-get-at-bol 'org-marker)
8010 (org-agenda-error)))
8011 (buffer (marker-buffer marker))
8012 (pos (marker-position marker))
8013 cdate today)
8014 (org-with-remote-undo buffer
8015 (with-current-buffer buffer
8016 (widen)
8017 (goto-char pos)
8018 (if (not (org-at-timestamp-p))
8019 (error "Cannot find time stamp"))
8020 (when (and org-agenda-move-date-from-past-immediately-to-today
8021 (equal arg 1)
8022 (or (not what) (eq what 'day))
8023 (not (save-match-data (org-at-date-range-p))))
8024 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
8025 cdate (calendar-absolute-from-gregorian
8026 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
8027 today (org-today))
8028 (if (> today cdate)
8029 ;; immediately shift to today
8030 (setq arg (- today cdate))))
8031 (org-timestamp-change arg (or what 'day))
8032 (when (and (org-at-date-range-p)
8033 (re-search-backward org-tr-regexp-both (point-at-bol)))
8034 (let ((end org-last-changed-timestamp))
8035 (org-timestamp-change arg (or what 'day))
8036 (setq org-last-changed-timestamp
8037 (concat org-last-changed-timestamp "--" end)))))
8038 (org-agenda-show-new-time marker org-last-changed-timestamp))
8039 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8041 (defun org-agenda-date-earlier (arg &optional what)
8042 "Change the date of this item to ARG day(s) earlier."
8043 (interactive "p")
8044 (org-agenda-date-later (- arg) what))
8046 (defun org-agenda-date-later-minutes (arg)
8047 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8048 (interactive "p")
8049 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8050 (org-agenda-date-later arg 'minute))
8052 (defun org-agenda-date-earlier-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-earlier arg 'minute))
8058 (defun org-agenda-date-later-hours (arg)
8059 "Change the time of this item, in hour steps."
8060 (interactive "p")
8061 (org-agenda-date-later arg 'hour))
8063 (defun org-agenda-date-earlier-hours (arg)
8064 "Change the time of this item, in hour steps."
8065 (interactive "p")
8066 (org-agenda-date-earlier arg 'hour))
8068 (defun org-agenda-show-new-time (marker stamp &optional prefix)
8069 "Show new date stamp via text properties."
8070 ;; We use text properties to make this undoable
8071 (let ((inhibit-read-only t)
8072 (buffer-invisibility-spec))
8073 (setq stamp (concat " " prefix " => " stamp))
8074 (save-excursion
8075 (goto-char (point-max))
8076 (while (not (bobp))
8077 (when (equal marker (org-get-at-bol 'org-marker))
8078 (org-move-to-column (- (window-width) (length stamp)) t)
8079 (org-agenda-fix-tags-filter-overlays-at (point))
8080 (if (featurep 'xemacs)
8081 ;; Use `duplicable' property to trigger undo recording
8082 (let ((ex (make-extent nil nil))
8083 (gl (make-glyph stamp)))
8084 (set-glyph-face gl 'secondary-selection)
8085 (set-extent-properties
8086 ex (list 'invisible t 'end-glyph gl 'duplicable t))
8087 (insert-extent ex (1- (point)) (point-at-eol)))
8088 (add-text-properties
8089 (1- (point)) (point-at-eol)
8090 (list 'display (org-add-props stamp nil
8091 'face 'secondary-selection))))
8092 (beginning-of-line 1))
8093 (beginning-of-line 0)))))
8095 (defun org-agenda-date-prompt (arg)
8096 "Change the date of this item. Date is prompted for, with default today.
8097 The prefix ARG is passed to the `org-time-stamp' command and can therefore
8098 be used to request time specification in the time stamp."
8099 (interactive "P")
8100 (org-agenda-check-type t 'agenda 'timeline)
8101 (org-agenda-check-no-diary)
8102 (let* ((marker (or (org-get-at-bol 'org-marker)
8103 (org-agenda-error)))
8104 (buffer (marker-buffer marker))
8105 (pos (marker-position marker)))
8106 (org-with-remote-undo buffer
8107 (with-current-buffer buffer
8108 (widen)
8109 (goto-char pos)
8110 (if (not (org-at-timestamp-p t))
8111 (error "Cannot find time stamp"))
8112 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8113 (org-agenda-show-new-time marker org-last-changed-timestamp))
8114 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8116 (defun org-agenda-schedule (arg &optional time)
8117 "Schedule the item at point.
8118 ARG is passed through to `org-schedule'."
8119 (interactive "P")
8120 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8121 (org-agenda-check-no-diary)
8122 (let* ((marker (or (org-get-at-bol 'org-marker)
8123 (org-agenda-error)))
8124 (type (marker-insertion-type marker))
8125 (buffer (marker-buffer marker))
8126 (pos (marker-position marker))
8127 (org-insert-labeled-timestamps-at-point nil)
8129 (set-marker-insertion-type marker t)
8130 (org-with-remote-undo buffer
8131 (with-current-buffer buffer
8132 (widen)
8133 (goto-char pos)
8134 (setq ts (org-schedule arg time)))
8135 (org-agenda-show-new-time marker ts "S"))
8136 (message "Item scheduled for %s" ts)))
8138 (defun org-agenda-deadline (arg &optional time)
8139 "Schedule the item at point.
8140 ARG is passed through to `org-deadline'."
8141 (interactive "P")
8142 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8143 (org-agenda-check-no-diary)
8144 (let* ((marker (or (org-get-at-bol 'org-marker)
8145 (org-agenda-error)))
8146 (buffer (marker-buffer marker))
8147 (pos (marker-position marker))
8148 (org-insert-labeled-timestamps-at-point nil)
8150 (org-with-remote-undo buffer
8151 (with-current-buffer buffer
8152 (widen)
8153 (goto-char pos)
8154 (setq ts (org-deadline arg time)))
8155 (org-agenda-show-new-time marker ts "D"))
8156 (message "Deadline for this item set to %s" ts)))
8158 (defun org-agenda-action ()
8159 "Select entry for agenda action, or execute an agenda action.
8160 This command prompts for another letter. Valid inputs are:
8162 m Mark the entry at point for an agenda action
8163 s Schedule the marked entry to the date at the cursor
8164 d Set the deadline of the marked entry to the date at the cursor
8165 r Call `org-remember' with cursor date as the default date
8166 c Call `org-capture' with cursor date as the default date
8167 SPC Show marked entry in other window
8168 TAB Visit marked entry in other window
8170 The cursor may be at a date in the calendar, or in the Org agenda."
8171 (interactive)
8172 (let (ans)
8173 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
8174 (setq ans (read-char-exclusive))
8175 (cond
8176 ((equal ans ?m)
8177 ;; Mark this entry
8178 (if (eq major-mode 'org-agenda-mode)
8179 (let ((m (or (org-get-at-bol 'org-hd-marker)
8180 (org-get-at-bol 'org-marker))))
8181 (if m
8182 (progn
8183 (move-marker org-agenda-action-marker
8184 (marker-position m) (marker-buffer m))
8185 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
8186 (error "Don't know which entry to mark")))
8187 (error "This command works only in the agenda")))
8188 ((equal ans ?s)
8189 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
8190 ((equal ans ?d)
8191 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
8192 ((equal ans ?r)
8193 (org-agenda-do-action '(org-remember) t))
8194 ((equal ans ?c)
8195 (org-agenda-do-action '(org-capture) t))
8196 ((equal ans ?\ )
8197 (let ((cw (selected-window)))
8198 (org-switch-to-buffer-other-window
8199 (marker-buffer org-agenda-action-marker))
8200 (goto-char org-agenda-action-marker)
8201 (org-show-context 'agenda)
8202 (select-window cw)))
8203 ((equal ans ?\C-i)
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 (t (error "Invalid agenda action %c" ans)))))
8210 (defun org-agenda-do-action (form &optional current-buffer)
8211 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
8212 (let ((org-overriding-default-time (org-get-cursor-date)))
8213 (if current-buffer
8214 (eval form)
8215 (if (not (marker-buffer org-agenda-action-marker))
8216 (error "No entry has been selected for agenda action")
8217 (with-current-buffer (marker-buffer org-agenda-action-marker)
8218 (save-excursion
8219 (save-restriction
8220 (widen)
8221 (goto-char org-agenda-action-marker)
8222 (eval form))))))))
8224 (defun org-agenda-clock-in (&optional arg)
8225 "Start the clock on the currently selected item."
8226 (interactive "P")
8227 (org-agenda-check-no-diary)
8228 (if (equal arg '(4))
8229 (org-clock-in arg)
8230 (let* ((marker (or (org-get-at-bol 'org-marker)
8231 (org-agenda-error)))
8232 (hdmarker (or (org-get-at-bol 'org-hd-marker)
8233 marker))
8234 (pos (marker-position marker))
8235 newhead)
8236 (org-with-remote-undo (marker-buffer marker)
8237 (with-current-buffer (marker-buffer marker)
8238 (widen)
8239 (goto-char pos)
8240 (org-show-context 'agenda)
8241 (org-show-entry)
8242 (org-cycle-hide-drawers 'children)
8243 (org-clock-in arg)
8244 (setq newhead (org-get-heading)))
8245 (org-agenda-change-all-lines newhead hdmarker)))))
8247 (defun org-agenda-clock-out ()
8248 "Stop the currently running clock."
8249 (interactive)
8250 (unless (marker-buffer org-clock-marker)
8251 (error "No running clock"))
8252 (let ((marker (make-marker)) newhead)
8253 (org-with-remote-undo (marker-buffer org-clock-marker)
8254 (with-current-buffer (marker-buffer org-clock-marker)
8255 (save-excursion
8256 (save-restriction
8257 (widen)
8258 (goto-char org-clock-marker)
8259 (org-back-to-heading t)
8260 (move-marker marker (point))
8261 (org-clock-out)
8262 (setq newhead (org-get-heading))))))
8263 (org-agenda-change-all-lines newhead marker)
8264 (move-marker marker nil)))
8266 (defun org-agenda-clock-cancel (&optional arg)
8267 "Cancel the currently running clock."
8268 (interactive "P")
8269 (unless (marker-buffer org-clock-marker)
8270 (error "No running clock"))
8271 (org-with-remote-undo (marker-buffer org-clock-marker)
8272 (org-clock-cancel)))
8274 (defun org-agenda-clock-goto ()
8275 "Jump to the currently clocked in task within the agenda.
8276 If the currently clocked in task is not listed in the agenda
8277 buffer, display it in another window."
8278 (interactive)
8279 (let (pos)
8280 (mapc (lambda (o)
8281 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
8282 (setq pos (overlay-start o))))
8283 (overlays-in (point-min) (point-max)))
8284 (cond (pos (goto-char pos))
8285 ;; If the currently clocked entry is not in the agenda
8286 ;; buffer, we visit it in another window:
8287 (org-clock-current-task
8288 (org-switch-to-buffer-other-window (org-clock-goto)))
8289 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
8291 (defun org-agenda-diary-entry-in-org-file ()
8292 "Make a diary entry in the file `org-agenda-diary-file'."
8293 (let (d1 d2 char (text "") dp1 dp2)
8294 (if (equal (buffer-name) "*Calendar*")
8295 (setq d1 (calendar-cursor-to-date t)
8296 d2 (car calendar-mark-ring))
8297 (setq dp1 (get-text-property (point-at-bol) 'day))
8298 (unless dp1 (error "No date defined in current line"))
8299 (setq d1 (calendar-gregorian-from-absolute dp1)
8300 d2 (and (ignore-errors (mark))
8301 (save-excursion
8302 (goto-char (mark))
8303 (setq dp2 (get-text-property (point-at-bol) 'day)))
8304 (calendar-gregorian-from-absolute dp2))))
8305 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
8306 (setq char (read-char-exclusive))
8307 (cond
8308 ((equal char ?d)
8309 (setq text (read-string "Day entry: "))
8310 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
8311 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8312 ((equal char ?a)
8313 (setq d1 (list (car d1) (nth 1 d1)
8314 (read-number (format "Reference year [%d]: " (nth 2 d1))
8315 (nth 2 d1))))
8316 (setq text (read-string "Anniversary (use %d to show years): "))
8317 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
8318 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8319 ((equal char ?b)
8320 (setq text (read-string "Block entry: "))
8321 (unless (and d1 d2 (not (equal d1 d2)))
8322 (error "No block of days selected"))
8323 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
8324 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8325 ((equal char ?j)
8326 (org-switch-to-buffer-other-window
8327 (find-file-noselect org-agenda-diary-file))
8328 (require 'org-datetree)
8329 (org-datetree-find-date-create d1)
8330 (org-reveal t))
8331 (t (error "Invalid selection character `%c'" char)))))
8333 (defcustom org-agenda-insert-diary-strategy 'date-tree
8334 "Where in `org-agenda-diary-file' should new entries be added?
8335 Valid values:
8337 date-tree in the date tree, as child of the date
8338 top-level as top-level entries at the end of the file."
8339 :group 'org-agenda
8340 :type '(choice
8341 (const :tag "in a date tree" date-tree)
8342 (const :tag "as top level at end of file" top-level)))
8344 (defcustom org-agenda-insert-diary-extract-time nil
8345 "Non-nil means extract any time specification from the diary entry."
8346 :group 'org-agenda
8347 :version "24.1"
8348 :type 'boolean)
8350 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8351 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8352 If TEXT is not empty, it will become the headline of the new entry, and
8353 the resulting entry will not be shown. When TEXT is empty, switch to
8354 `org-agenda-diary-file' and let the user finish the entry there."
8355 (let ((cw (current-window-configuration)))
8356 (org-switch-to-buffer-other-window
8357 (find-file-noselect org-agenda-diary-file))
8358 (widen)
8359 (goto-char (point-min))
8360 (cond
8361 ((eq type 'anniversary)
8362 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8363 (progn
8364 (or (org-at-heading-p t)
8365 (progn
8366 (outline-next-heading)
8367 (insert "* Anniversaries\n\n")
8368 (beginning-of-line -1)))))
8369 (outline-next-heading)
8370 (org-back-over-empty-lines)
8371 (backward-char 1)
8372 (insert "\n")
8373 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8374 (nth 2 d1) (car d1) (nth 1 d1) text)))
8375 ((eq type 'day)
8376 (let ((org-prefix-has-time t)
8377 (org-agenda-time-leading-zero t)
8378 fmt time time2)
8379 (if org-agenda-insert-diary-extract-time
8380 ;; Use org-agenda-format-item to parse text for a time-range and
8381 ;; remove it. FIXME: This is a hack, we should refactor
8382 ;; that function to make time extraction available separately
8383 (setq fmt (org-agenda-format-item nil text nil nil t)
8384 time (get-text-property 0 'time fmt)
8385 time2 (if (> (length time) 0)
8386 ;; split-string removes trailing ...... if
8387 ;; no end time given. First space
8388 ;; separates time from date.
8389 (concat " " (car (split-string time "\\.")))
8390 nil)
8391 text (get-text-property 0 'txt fmt)))
8392 (if (eq org-agenda-insert-diary-strategy 'top-level)
8393 (org-agenda-insert-diary-as-top-level text)
8394 (require 'org-datetree)
8395 (org-datetree-find-date-create d1)
8396 (org-agenda-insert-diary-make-new-entry text))
8397 (org-insert-time-stamp (org-time-from-absolute
8398 (calendar-absolute-from-gregorian d1))
8399 nil nil nil nil time2))
8400 (end-of-line 0))
8401 ((eq type 'block)
8402 (if (> (calendar-absolute-from-gregorian d1)
8403 (calendar-absolute-from-gregorian d2))
8404 (setq d1 (prog1 d2 (setq d2 d1))))
8405 (if (eq org-agenda-insert-diary-strategy 'top-level)
8406 (org-agenda-insert-diary-as-top-level text)
8407 (require 'org-datetree)
8408 (org-datetree-find-date-create d1)
8409 (org-agenda-insert-diary-make-new-entry text))
8410 (org-insert-time-stamp (org-time-from-absolute
8411 (calendar-absolute-from-gregorian d1)))
8412 (insert "--")
8413 (org-insert-time-stamp (org-time-from-absolute
8414 (calendar-absolute-from-gregorian d2)))
8415 (end-of-line 0)))
8416 (if (string-match "\\S-" text)
8417 (progn
8418 (set-window-configuration cw)
8419 (message "%s entry added to %s"
8420 (capitalize (symbol-name type))
8421 (abbreviate-file-name org-agenda-diary-file)))
8422 (org-reveal t)
8423 (message "Please finish entry here"))))
8425 (defun org-agenda-insert-diary-as-top-level (text)
8426 "Make new entry as a top-level entry at the end of the file.
8427 Add TEXT as headline, and position the cursor in the second line so that
8428 a timestamp can be added there."
8429 (widen)
8430 (goto-char (point-max))
8431 (or (bolp) (insert "\n"))
8432 (insert "* " text "\n")
8433 (if org-adapt-indentation (org-indent-to-column 2)))
8435 (defun org-agenda-insert-diary-make-new-entry (text)
8436 "Make new entry as last child of current entry.
8437 Add TEXT as headline, and position the cursor in the second line so that
8438 a timestamp can be added there."
8439 (let ((org-show-following-heading t)
8440 (org-show-siblings t)
8441 (org-show-hierarchy-above t)
8442 (org-show-entry-below t)
8443 col)
8444 (outline-next-heading)
8445 (org-back-over-empty-lines)
8446 (or (looking-at "[ \t]*$")
8447 (progn (insert "\n") (backward-char 1)))
8448 (org-insert-heading nil t)
8449 (org-do-demote)
8450 (setq col (current-column))
8451 (insert text "\n")
8452 (if org-adapt-indentation (org-indent-to-column col))
8453 (let ((org-show-following-heading t)
8454 (org-show-siblings t)
8455 (org-show-hierarchy-above t)
8456 (org-show-entry-below t))
8457 (org-show-context))))
8459 (defun org-agenda-diary-entry ()
8460 "Make a diary entry, like the `i' command from the calendar.
8461 All the standard commands work: block, weekly etc.
8462 When `org-agenda-diary-file' points to a file,
8463 `org-agenda-diary-entry-in-org-file' is called instead to create
8464 entries in that Org-mode file."
8465 (interactive)
8466 (org-agenda-check-type t 'agenda 'timeline)
8467 (if (not (eq org-agenda-diary-file 'diary-file))
8468 (org-agenda-diary-entry-in-org-file)
8469 (require 'diary-lib)
8470 (let* ((char (progn
8471 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8472 (read-char-exclusive)))
8473 (cmd (cdr (assoc char
8474 '((?d . insert-diary-entry)
8475 (?w . insert-weekly-diary-entry)
8476 (?m . insert-monthly-diary-entry)
8477 (?y . insert-yearly-diary-entry)
8478 (?a . insert-anniversary-diary-entry)
8479 (?b . insert-block-diary-entry)
8480 (?c . insert-cyclic-diary-entry)))))
8481 (oldf (symbol-function 'calendar-cursor-to-date))
8482 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8483 (point (point))
8484 (mark (or (mark t) (point))))
8485 (unless cmd
8486 (error "No command associated with <%c>" char))
8487 (unless (and (get-text-property point 'day)
8488 (or (not (equal ?b char))
8489 (get-text-property mark 'day)))
8490 (error "Don't know which date to use for diary entry"))
8491 ;; We implement this by hacking the `calendar-cursor-to-date' function
8492 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8493 (let ((calendar-mark-ring
8494 (list (calendar-gregorian-from-absolute
8495 (or (get-text-property mark 'day)
8496 (get-text-property point 'day))))))
8497 (unwind-protect
8498 (progn
8499 (fset 'calendar-cursor-to-date
8500 (lambda (&optional error dummy)
8501 (calendar-gregorian-from-absolute
8502 (get-text-property point 'day))))
8503 (call-interactively cmd))
8504 (fset 'calendar-cursor-to-date oldf))))))
8506 (defun org-agenda-execute-calendar-command (cmd)
8507 "Execute a calendar command from the agenda, with the date associated to
8508 the cursor position."
8509 (org-agenda-check-type t 'agenda 'timeline)
8510 (require 'diary-lib)
8511 (unless (get-text-property (point) 'day)
8512 (error "Don't know which date to use for calendar command"))
8513 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8514 (point (point))
8515 (date (calendar-gregorian-from-absolute
8516 (get-text-property point 'day)))
8517 ;; the following 2 vars are needed in the calendar
8518 (displayed-month (car date))
8519 (displayed-year (nth 2 date)))
8520 (unwind-protect
8521 (progn
8522 (fset 'calendar-cursor-to-date
8523 (lambda (&optional error dummy)
8524 (calendar-gregorian-from-absolute
8525 (get-text-property point 'day))))
8526 (call-interactively cmd))
8527 (fset 'calendar-cursor-to-date oldf))))
8529 (defun org-agenda-phases-of-moon ()
8530 "Display the phases of the moon for the 3 months around the cursor date."
8531 (interactive)
8532 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8534 (defun org-agenda-holidays ()
8535 "Display the holidays for the 3 months around the cursor date."
8536 (interactive)
8537 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8539 (defvar calendar-longitude)
8540 (defvar calendar-latitude)
8541 (defvar calendar-location-name)
8543 (defun org-agenda-sunrise-sunset (arg)
8544 "Display sunrise and sunset for the cursor date.
8545 Latitude and longitude can be specified with the variables
8546 `calendar-latitude' and `calendar-longitude'. When called with prefix
8547 argument, latitude and longitude will be prompted for."
8548 (interactive "P")
8549 (require 'solar)
8550 (let ((calendar-longitude (if arg nil calendar-longitude))
8551 (calendar-latitude (if arg nil calendar-latitude))
8552 (calendar-location-name
8553 (if arg "the given coordinates" calendar-location-name)))
8554 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8556 (defun org-agenda-goto-calendar ()
8557 "Open the Emacs calendar with the date at the cursor."
8558 (interactive)
8559 (org-agenda-check-type t 'agenda 'timeline)
8560 (let* ((day (or (get-text-property (point) 'day)
8561 (error "Don't know which date to open in calendar")))
8562 (date (calendar-gregorian-from-absolute day))
8563 (calendar-move-hook nil)
8564 (calendar-view-holidays-initially-flag nil)
8565 (calendar-view-diary-initially-flag nil))
8566 (calendar)
8567 (calendar-goto-date date)))
8569 ;;;###autoload
8570 (defun org-calendar-goto-agenda ()
8571 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8572 This is a command that has to be installed in `calendar-mode-map'."
8573 (interactive)
8574 (org-agenda-list nil (calendar-absolute-from-gregorian
8575 (calendar-cursor-to-date))
8576 nil))
8578 (defun org-agenda-convert-date ()
8579 (interactive)
8580 (org-agenda-check-type t 'agenda 'timeline)
8581 (let ((day (get-text-property (point) 'day))
8582 date s)
8583 (unless day
8584 (error "Don't know which date to convert"))
8585 (setq date (calendar-gregorian-from-absolute day))
8586 (setq s (concat
8587 "Gregorian: " (calendar-date-string date) "\n"
8588 "ISO: " (calendar-iso-date-string date) "\n"
8589 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8590 "Julian: " (calendar-julian-date-string date) "\n"
8591 "Astron. JD: " (calendar-astro-date-string date)
8592 " (Julian date number at noon UTC)\n"
8593 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8594 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8595 "French: " (calendar-french-date-string date) "\n"
8596 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8597 "Mayan: " (calendar-mayan-date-string date) "\n"
8598 "Coptic: " (calendar-coptic-date-string date) "\n"
8599 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8600 "Persian: " (calendar-persian-date-string date) "\n"
8601 "Chinese: " (calendar-chinese-date-string date) "\n"))
8602 (with-output-to-temp-buffer "*Dates*"
8603 (princ s))
8604 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8606 ;;; Bulk commands
8608 (defvar org-agenda-bulk-marked-entries nil
8609 "List of markers that refer to marked entries in the agenda.")
8611 (defun org-agenda-bulk-marked-p ()
8612 (eq (get-char-property (point-at-bol) 'type)
8613 'org-marked-entry-overlay))
8615 (defun org-agenda-bulk-mark (&optional arg)
8616 "Mark the entry at point for future bulk action."
8617 (interactive "p")
8618 (dotimes (i (or arg 1))
8619 (unless (org-get-at-bol 'org-agenda-diary-link)
8620 (let* ((m (org-get-at-bol 'org-hd-marker))
8622 (unless (org-agenda-bulk-marked-p)
8623 (unless m (error "Nothing to mark at point"))
8624 (push m org-agenda-bulk-marked-entries)
8625 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8626 (org-overlay-display ov "> "
8627 (org-get-todo-face "TODO")
8628 'evaporate)
8629 (overlay-put ov 'type 'org-marked-entry-overlay))
8630 (beginning-of-line 2)
8631 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8632 (beginning-of-line 2))
8633 (message "%d entries marked for bulk action"
8634 (length org-agenda-bulk-marked-entries))))))
8636 (defun org-agenda-bulk-mark-regexp (regexp)
8637 "Mark entries match REGEXP."
8638 (interactive "sMark entries matching regexp: ")
8639 (let (entries-marked)
8640 (save-excursion
8641 (goto-char (point-min))
8642 (goto-char (next-single-property-change (point) 'txt))
8643 (while (re-search-forward regexp nil t)
8644 (when (string-match regexp (get-text-property (point) 'txt))
8645 (setq entries-marked (+ entries-marked 1))
8646 (call-interactively 'org-agenda-bulk-mark))))
8647 (if (not entries-marked)
8648 (message "No entry matching this regexp."))))
8650 (defun org-agenda-bulk-unmark ()
8651 "Unmark the entry at point for future bulk action."
8652 (interactive)
8653 (when (org-agenda-bulk-marked-p)
8654 (org-agenda-bulk-remove-overlays
8655 (point-at-bol) (+ 2 (point-at-bol)))
8656 (setq org-agenda-bulk-marked-entries
8657 (delete (org-get-at-bol 'org-hd-marker)
8658 org-agenda-bulk-marked-entries)))
8659 (beginning-of-line 2)
8660 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8661 (beginning-of-line 2))
8662 (message "%d entries marked for bulk action"
8663 (length org-agenda-bulk-marked-entries)))
8665 (defun org-agenda-bulk-toggle ()
8666 "Toggle marking the entry at point for bulk action."
8667 (interactive)
8668 (if (org-agenda-bulk-marked-p)
8669 (org-agenda-bulk-unmark)
8670 (org-agenda-bulk-mark)))
8672 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8673 "Remove the mark overlays between BEG and END in the agenda buffer.
8674 BEG and END default to the buffer limits.
8676 This only removes the overlays, it does not remove the markers
8677 from the list in `org-agenda-bulk-marked-entries'."
8678 (interactive)
8679 (mapc (lambda (ov)
8680 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8681 (delete-overlay ov)))
8682 (overlays-in (or beg (point-min)) (or end (point-max)))))
8684 (defun org-agenda-bulk-remove-all-marks ()
8685 "Remove all marks in the agenda buffer.
8686 This will remove the markers, and the overlays."
8687 (interactive)
8688 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8689 (setq org-agenda-bulk-marked-entries nil)
8690 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8692 (defcustom org-agenda-persistent-marks nil
8693 "Non-nil means marked items will stay marked after a bulk action.
8694 You can interactively and temporarily toggle by typing `p' when you
8695 are prompted for a bulk action."
8696 :group 'org-agenda
8697 :version "24.1"
8698 :type 'boolean)
8700 (defun org-agenda-bulk-action (&optional arg)
8701 "Execute an remote-editing action on all marked entries.
8702 The prefix arg is passed through to the command if possible."
8703 (interactive "P")
8704 ;; Make sure we have markers, and only valid ones
8705 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8706 (mapc
8707 (lambda (m)
8708 (unless (and (markerp m)
8709 (marker-buffer m)
8710 (buffer-live-p (marker-buffer m))
8711 (marker-position m))
8712 (error "Marker %s for bulk command is invalid" m)))
8713 org-agenda-bulk-marked-entries)
8715 ;; Prompt for the bulk command
8716 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
8717 (message (concat msg "[r]efile [$]arch [A]rch->sib [t]odo"
8718 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction "
8719 (when org-agenda-bulk-custom-functions
8720 (concat " Custom: ["
8721 (mapconcat (lambda(f) (char-to-string (car f)))
8722 org-agenda-bulk-custom-functions "")
8723 "]"))))
8724 (catch 'exit
8725 (let* ((action (read-char-exclusive))
8726 (org-log-refile (if org-log-refile 'time nil))
8727 (entries (reverse org-agenda-bulk-marked-entries))
8728 redo-at-end
8729 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8730 (cond
8731 ((equal action ?p)
8732 (let ((org-agenda-persistent-marks
8733 (not org-agenda-persistent-marks)))
8734 (org-agenda-bulk-action)
8735 (throw 'exit nil)))
8737 ((equal action ?$)
8738 (setq cmd '(org-agenda-archive)))
8740 ((equal action ?A)
8741 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8743 ((member action '(?r ?w))
8744 (setq rfloc (org-refile-get-location
8745 "Refile to"
8746 (marker-buffer (car org-agenda-bulk-marked-entries))
8747 org-refile-allow-creating-parent-nodes))
8748 (if (nth 3 rfloc)
8749 (setcar (nthcdr 3 rfloc)
8750 (move-marker (make-marker) (nth 3 rfloc)
8751 (or (get-file-buffer (nth 1 rfloc))
8752 (find-buffer-visiting (nth 1 rfloc))
8753 (error "This should not happen")))))
8755 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8756 redo-at-end t))
8758 ((equal action ?t)
8759 (setq state (org-icompleting-read
8760 "Todo state: "
8761 (with-current-buffer (marker-buffer (car entries))
8762 (mapcar 'list org-todo-keywords-1))))
8763 (setq cmd `(let ((org-inhibit-blocking t)
8764 (org-inhibit-logging 'note))
8765 (org-agenda-todo ,state))))
8767 ((memq action '(?- ?+))
8768 (setq tag (org-icompleting-read
8769 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8770 (with-current-buffer (marker-buffer (car entries))
8771 (delq nil
8772 (mapcar (lambda (x)
8773 (if (stringp (car x)) x)) org-tag-alist)))))
8774 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8776 ((memq action '(?s ?d))
8777 (let* ((date (unless arg
8778 (org-read-date
8779 nil nil nil
8780 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8781 (ans (if arg nil org-read-date-final-answer))
8782 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8783 (setq cmd `(let* ((bound (fboundp 'read-string))
8784 (old (and bound (symbol-function 'read-string))))
8785 (unwind-protect
8786 (progn
8787 (fset 'read-string (lambda (&rest ignore) ,ans))
8788 (eval '(,c1 arg)))
8789 (if bound
8790 (fset 'read-string old)
8791 (fmakunbound 'read-string)))))))
8793 ((equal action ?S)
8794 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8795 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8796 (let ((days (read-number
8797 (format "Scatter tasks across how many %sdays: "
8798 (if arg "week" "")) 7)))
8799 (setq cmd
8800 `(let ((distance (1+ (random ,days))))
8801 (if arg
8802 (let ((dist distance)
8803 (day-of-week
8804 (calendar-day-of-week
8805 (calendar-gregorian-from-absolute (org-today)))))
8806 (dotimes (i (1+ dist))
8807 (while (member day-of-week org-agenda-weekend-days)
8808 (incf distance)
8809 (incf day-of-week)
8810 (if (= day-of-week 7)
8811 (setq day-of-week 0)))
8812 (incf day-of-week)
8813 (if (= day-of-week 7)
8814 (setq day-of-week 0)))))
8815 ;; silently fail when try to replan a sexp entry
8816 (condition-case nil
8817 (let* ((date (calendar-gregorian-from-absolute
8818 (+ (org-today) distance)))
8819 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8820 (nth 2 date))))
8821 (org-agenda-schedule nil time))
8822 (error nil)))))))
8824 ((assoc action org-agenda-bulk-custom-functions)
8825 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8826 redo-at-end t))
8828 ((equal action ?f)
8829 (setq cmd (list (intern
8830 (org-icompleting-read "Function: "
8831 obarray 'fboundp t nil nil)))))
8833 (t (error "Invalid bulk action")))
8835 ;; Sort the markers, to make sure that parents are handled before children
8836 (setq entries (sort entries
8837 (lambda (a b)
8838 (cond
8839 ((equal (marker-buffer a) (marker-buffer b))
8840 (< (marker-position a) (marker-position b)))
8842 (string< (buffer-name (marker-buffer a))
8843 (buffer-name (marker-buffer b))))))))
8845 ;; Now loop over all markers and apply cmd
8846 (while (setq e (pop entries))
8847 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8848 (if (not pos)
8849 (progn (message "Skipping removed entry at %s" e)
8850 (setq cntskip (1+ cntskip)))
8851 (goto-char pos)
8852 (let (org-loop-over-headlines-in-active-region)
8853 (eval cmd))
8854 (when (not org-agenda-persistent-marks)
8855 (setq org-agenda-bulk-marked-entries
8856 (delete e org-agenda-bulk-marked-entries)))
8857 (setq cnt (1+ cnt))))
8858 (when (not org-agenda-persistent-marks)
8859 (org-agenda-bulk-remove-all-marks))
8860 (when redo-at-end (org-agenda-redo))
8861 (message "Acted on %d entries%s%s"
8863 (if (= cntskip 0)
8865 (format ", skipped %d (disappeared before their turn)"
8866 cntskip))
8867 (if (not org-agenda-persistent-marks)
8868 "" " (kept marked)"))))))
8870 ;;; Flagging notes
8872 (defun org-agenda-show-the-flagging-note ()
8873 "Display the flagging note in the other window.
8874 When called a second time in direct sequence, offer to remove the FLAGGING
8875 tag and (if present) the flagging note."
8876 (interactive)
8877 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8878 (win (selected-window))
8879 note heading newhead)
8880 (unless hdmarker
8881 (error "No linked entry at point"))
8882 (if (and (eq this-command last-command)
8883 (y-or-n-p "Unflag and remove any flagging note? "))
8884 (progn
8885 (org-agenda-remove-flag hdmarker)
8886 (let ((win (get-buffer-window "*Flagging Note*")))
8887 (and win (delete-window win)))
8888 (message "Entry unflagged"))
8889 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8890 (unless note
8891 (error "No flagging note"))
8892 (org-kill-new note)
8893 (org-switch-to-buffer-other-window "*Flagging Note*")
8894 (erase-buffer)
8895 (insert note)
8896 (goto-char (point-min))
8897 (while (re-search-forward "\\\\n" nil t)
8898 (replace-match "\n" t t))
8899 (goto-char (point-min))
8900 (select-window win)
8901 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8903 (defun org-agenda-remove-flag (marker)
8904 "Remove the FLAGGED tag and any flagging note in the entry."
8905 (let (newhead)
8906 (org-with-point-at marker
8907 (org-toggle-tag "FLAGGED" 'off)
8908 (org-entry-delete nil "THEFLAGGINGNOTE")
8909 (setq newhead (org-get-heading)))
8910 (org-agenda-change-all-lines newhead marker)
8911 (message "Entry unflagged")))
8913 (defun org-agenda-get-any-marker (&optional pos)
8914 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8915 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8917 ;;; Appointment reminders
8919 (defvar appt-time-msg-list)
8921 ;;;###autoload
8922 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8923 "Activate appointments found in `org-agenda-files'.
8924 With a \\[universal-argument] prefix, refresh the list of
8925 appointments.
8927 If FILTER is t, interactively prompt the user for a regular
8928 expression, and filter out entries that don't match it.
8930 If FILTER is a string, use this string as a regular expression
8931 for filtering entries out.
8933 If FILTER is a function, filter out entries against which
8934 calling the function returns nil. This function takes one
8935 argument: an entry from `org-agenda-get-day-entries'.
8937 FILTER can also be an alist with the car of each cell being
8938 either 'headline or 'category. For example:
8940 '((headline \"IMPORTANT\")
8941 (category \"Work\"))
8943 will only add headlines containing IMPORTANT or headlines
8944 belonging to the \"Work\" category.
8946 ARGS are symbols indicating what kind of entries to consider.
8947 By default `org-agenda-to-appt' will use :deadline, :scheduled
8948 and :timestamp entries. See the docstring of `org-diary' for
8949 details and examples."
8950 (interactive "P")
8951 (if refresh (setq appt-time-msg-list nil))
8952 (if (eq filter t)
8953 (setq filter (read-from-minibuffer "Regexp filter: ")))
8954 (let* ((cnt 0) ; count added events
8955 (scope (or args '(:deadline :scheduled :timestamp)))
8956 (org-agenda-new-buffers nil)
8957 (org-deadline-warning-days 0)
8958 ;; Do not use `org-today' here because appt only takes
8959 ;; time and without date as argument, so it may pass wrong
8960 ;; information otherwise
8961 (today (org-date-to-gregorian
8962 (time-to-days (current-time))))
8963 (org-agenda-restrict nil)
8964 (files (org-agenda-files 'unrestricted)) entries file
8965 (org-agenda-buffer nil))
8966 ;; Get all entries which may contain an appt
8967 (org-prepare-agenda-buffers files)
8968 (while (setq file (pop files))
8969 (setq entries
8970 (delq nil
8971 (append entries
8972 (apply 'org-agenda-get-day-entries
8973 file today scope)))))
8974 ;; Map thru entries and find if we should filter them out
8975 (mapc
8976 (lambda(x)
8977 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8978 (cat (get-text-property 1 'org-category x))
8979 (tod (get-text-property 1 'time-of-day x))
8980 (ok (or (null filter)
8981 (and (stringp filter) (string-match filter evt))
8982 (and (functionp filter) (funcall filter x))
8983 (and (listp filter)
8984 (let ((cat-filter (cadr (assoc 'category filter)))
8985 (evt-filter (cadr (assoc 'headline filter))))
8986 (or (and (stringp cat-filter)
8987 (string-match cat-filter cat))
8988 (and (stringp evt-filter)
8989 (string-match evt-filter evt))))))))
8990 ;; FIXME: Shall we remove text-properties for the appt text?
8991 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8992 (when (and ok tod)
8993 (setq tod (concat "00" (number-to-string tod))
8994 tod (when (string-match
8995 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8996 (concat (match-string 1 tod) ":"
8997 (match-string 2 tod))))
8998 (appt-add tod evt)
8999 (setq cnt (1+ cnt))))) entries)
9000 (org-release-buffers org-agenda-new-buffers)
9001 (if (eq cnt 0)
9002 (message "No event to add")
9003 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
9005 (defun org-agenda-todayp (date)
9006 "Does DATE mean today, when considering `org-extend-today-until'?"
9007 (let ((today (org-today))
9008 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
9009 date)))
9010 (eq date today)))
9012 (defun org-agenda-todo-yesterday (&optional arg)
9013 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
9014 (interactive "P")
9015 (let* ((hour (third (decode-time
9016 (org-current-time))))
9017 (org-extend-today-until (1+ hour)))
9018 (org-agenda-todo arg)))
9020 (provide 'org-agenda)
9022 ;;; org-agenda.el ends here