New command `org-agenda-bulk-mark-all' bound to `*'. Other minor enhancements.
[org-mode.git] / lisp / org-agenda.el
blob245398ed85818a38bb97b81bcbfe2f3d39859bee
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-all)
1992 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1993 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1994 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
1995 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1996 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1997 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1998 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1999 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2000 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2001 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2002 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2003 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2004 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2005 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2006 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2007 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2008 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2009 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2010 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2011 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2012 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2013 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2014 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2015 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2016 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2017 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2018 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2019 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2020 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2021 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2022 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2023 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2024 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2025 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
2026 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
2027 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2028 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2029 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2030 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2032 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2033 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2034 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2035 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2036 (while l (org-defkey org-agenda-mode-map
2037 (int-to-string (pop l)) 'digit-argument)))
2039 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2040 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2041 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2042 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2043 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2044 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2045 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2046 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2047 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2048 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
2049 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2050 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2051 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2052 'org-clock-modify-effort-estimate)
2053 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2054 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2055 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2056 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2057 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2058 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2059 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2060 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
2061 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2062 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2063 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2064 (substitute-key-definition 'next-line 'org-agenda-next-line
2065 org-agenda-mode-map global-map)
2066 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2067 org-agenda-mode-map global-map)
2068 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2069 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2070 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2071 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2072 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2073 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2074 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2075 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2076 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2077 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2078 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2079 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2080 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2081 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2082 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2083 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2084 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2085 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2086 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2087 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2088 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2089 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2090 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2091 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2092 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2093 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2094 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2095 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2096 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2097 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2099 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2100 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2101 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2102 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2103 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2104 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2105 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2106 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-category)
2107 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2108 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2109 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2110 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2112 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2113 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2114 (when org-agenda-mouse-1-follows-link
2115 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2116 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2117 '("Agenda"
2118 ("Agenda Files")
2119 "--"
2120 ("Agenda Dates"
2121 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2122 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2123 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2124 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2125 "--"
2126 ("View"
2127 ["Day View" org-agenda-day-view
2128 :active (org-agenda-check-type nil 'agenda)
2129 :style radio :selected (eq org-agenda-current-span 'day)
2130 :keys "v d (or just d)"]
2131 ["Week View" org-agenda-week-view
2132 :active (org-agenda-check-type nil 'agenda)
2133 :style radio :selected (eq org-agenda-current-span 'week)
2134 :keys "v w (or just w)"]
2135 ["Month View" org-agenda-month-view
2136 :active (org-agenda-check-type nil 'agenda)
2137 :style radio :selected (eq org-agenda-current-span 'month)
2138 :keys "v m"]
2139 ["Year View" org-agenda-year-view
2140 :active (org-agenda-check-type nil 'agenda)
2141 :style radio :selected (eq org-agenda-current-span 'year)
2142 :keys "v y"]
2143 "--"
2144 ["Include Diary" org-agenda-toggle-diary
2145 :style toggle :selected org-agenda-include-diary
2146 :active (org-agenda-check-type nil 'agenda)]
2147 ["Include Deadlines" org-agenda-toggle-deadlines
2148 :style toggle :selected org-agenda-include-deadlines
2149 :active (org-agenda-check-type nil 'agenda)]
2150 ["Use Time Grid" org-agenda-toggle-time-grid
2151 :style toggle :selected org-agenda-use-time-grid
2152 :active (org-agenda-check-type nil 'agenda)]
2153 "--"
2154 ["Show clock report" org-agenda-clockreport-mode
2155 :style toggle :selected org-agenda-clockreport-mode
2156 :active (org-agenda-check-type nil 'agenda)]
2157 ["Show some entry text" org-agenda-entry-text-mode
2158 :style toggle :selected org-agenda-entry-text-mode
2159 :active t]
2160 "--"
2161 ["Show Logbook entries" org-agenda-log-mode
2162 :style toggle :selected org-agenda-show-log
2163 :active (org-agenda-check-type nil 'agenda 'timeline)
2164 :keys "v l (or just l)"]
2165 ["Include archived trees" org-agenda-archives-mode
2166 :style toggle :selected org-agenda-archives-mode :active t
2167 :keys "v a"]
2168 ["Include archive files" (org-agenda-archives-mode t)
2169 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2170 :keys "v A"]
2171 "--"
2172 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2173 ["Write view to file" org-agenda-write t]
2174 ["Rebuild buffer" org-agenda-redo t]
2175 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2176 "--"
2177 ["Show original entry" org-agenda-show t]
2178 ["Go To (other window)" org-agenda-goto t]
2179 ["Go To (this window)" org-agenda-switch-to t]
2180 ["Follow Mode" org-agenda-follow-mode
2181 :style toggle :selected org-agenda-follow-mode :active t]
2182 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2183 "--"
2184 ("TODO"
2185 ["Cycle TODO" org-agenda-todo t]
2186 ["Next TODO set" org-agenda-todo-nextset t]
2187 ["Previous TODO set" org-agenda-todo-previousset t]
2188 ["Add note" org-agenda-add-note t])
2189 ("Archive/Refile/Delete"
2190 ["Archive default" org-agenda-archive-default t]
2191 ["Archive default" org-agenda-archive-default-with-confirmation t]
2192 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2193 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2194 ["Archive subtree" org-agenda-archive t]
2195 "--"
2196 ["Refile" org-agenda-refile t]
2197 "--"
2198 ["Delete subtree" org-agenda-kill t])
2199 ("Bulk action"
2200 ["Mark entry" org-agenda-bulk-mark t]
2201 ["Mark all" org-agenda-bulk-mark-all t]
2202 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2203 ["Unmark entry" org-agenda-bulk-unmark t]
2204 ["Unmark all entries" org-agenda-bulk-unmark-all :active t :keys "C-u s"])
2205 ["Act on all marked" org-agenda-bulk-action t]
2206 "--"
2207 ("Tags and Properties"
2208 ["Show all Tags" org-agenda-show-tags t]
2209 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2210 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2211 "--"
2212 ["Column View" org-columns t])
2213 ("Deadline/Schedule"
2214 ["Schedule" org-agenda-schedule t]
2215 ["Set Deadline" org-agenda-deadline t]
2216 "--"
2217 ["Mark item" org-agenda-action :active t :keys "k m"]
2218 ["Show mark item" org-agenda-action :active t :keys "k v"]
2219 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2220 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2221 "--"
2222 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2223 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2224 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2225 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2226 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2227 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2228 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2229 ("Clock and Effort"
2230 ["Clock in" org-agenda-clock-in t]
2231 ["Clock out" org-agenda-clock-out t]
2232 ["Clock cancel" org-agenda-clock-cancel t]
2233 ["Goto running clock" org-clock-goto t]
2234 "--"
2235 ["Set Effort" org-agenda-set-effort t]
2236 ["Change clocked effort" org-clock-modify-effort-estimate
2237 (org-clock-is-active)])
2238 ("Priority"
2239 ["Set Priority" org-agenda-priority t]
2240 ["Increase Priority" org-agenda-priority-up t]
2241 ["Decrease Priority" org-agenda-priority-down t]
2242 ["Show Priority" org-agenda-show-priority t])
2243 ("Calendar/Diary"
2244 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2245 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2246 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2247 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2248 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2249 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2250 "--"
2251 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2252 "--"
2253 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2254 "--"
2255 ("MobileOrg"
2256 ["Push Files and Views" org-mobile-push t]
2257 ["Get Captured and Flagged" org-mobile-pull t]
2258 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2259 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2260 "--"
2261 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2262 "--"
2263 ["Quit" org-agenda-quit t]
2264 ["Exit and Release Buffers" org-agenda-exit t]
2267 ;;; Agenda undo
2269 (defvar org-agenda-allow-remote-undo t
2270 "Non-nil means allow remote undo from the agenda buffer.")
2271 (defvar org-agenda-undo-list nil
2272 "List of undoable operations in the agenda since last refresh.")
2273 (defvar org-agenda-undo-has-started-in nil
2274 "Buffers that have already seen `undo-start' in the current undo sequence.")
2275 (defvar org-agenda-pending-undo-list nil
2276 "In a series of undo commands, this is the list of remaining undo items.")
2278 (defun org-agenda-undo ()
2279 "Undo a remote editing step in the agenda.
2280 This undoes changes both in the agenda buffer and in the remote buffer
2281 that have been changed along."
2282 (interactive)
2283 (or org-agenda-allow-remote-undo
2284 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2285 (if (not (eq this-command last-command))
2286 (setq org-agenda-undo-has-started-in nil
2287 org-agenda-pending-undo-list org-agenda-undo-list))
2288 (if (not org-agenda-pending-undo-list)
2289 (error "No further undo information"))
2290 (let* ((entry (pop org-agenda-pending-undo-list))
2291 buf line cmd rembuf)
2292 (setq cmd (pop entry) line (pop entry))
2293 (setq rembuf (nth 2 entry))
2294 (org-with-remote-undo rembuf
2295 (while (bufferp (setq buf (pop entry)))
2296 (if (pop entry)
2297 (with-current-buffer buf
2298 (let ((last-undo-buffer buf)
2299 (inhibit-read-only t))
2300 (unless (memq buf org-agenda-undo-has-started-in)
2301 (push buf org-agenda-undo-has-started-in)
2302 (make-local-variable 'pending-undo-list)
2303 (undo-start))
2304 (while (and pending-undo-list
2305 (listp pending-undo-list)
2306 (not (car pending-undo-list)))
2307 (pop pending-undo-list))
2308 (undo-more 1))))))
2309 (org-goto-line line)
2310 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2312 (defun org-verify-change-for-undo (l1 l2)
2313 "Verify that a real change occurred between the undo lists L1 and L2."
2314 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2315 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2316 (not (eq l1 l2)))
2318 ;;; Agenda dispatch
2320 (defvar org-agenda-restrict nil)
2321 (defvar org-agenda-restrict-begin (make-marker))
2322 (defvar org-agenda-restrict-end (make-marker))
2323 (defvar org-agenda-last-dispatch-buffer nil)
2324 (defvar org-agenda-overriding-restriction nil)
2326 ;;;###autoload
2327 (defun org-agenda (&optional arg keys restriction)
2328 "Dispatch agenda commands to collect entries to the agenda buffer.
2329 Prompts for a command to execute. Any prefix arg will be passed
2330 on to the selected command. The default selections are:
2332 a Call `org-agenda-list' to display the agenda for current day or week.
2333 t Call `org-todo-list' to display the global todo list.
2334 T Call `org-todo-list' to display the global todo list, select only
2335 entries with a specific TODO keyword (the user gets a prompt).
2336 m Call `org-tags-view' to display headlines with tags matching
2337 a condition (the user is prompted for the condition).
2338 M Like `m', but select only TODO entries, no ordinary headlines.
2339 L Create a timeline for the current buffer.
2340 e Export views to associated files.
2341 s Search entries for keywords.
2342 / Multi occur across all agenda files and also files listed
2343 in `org-agenda-text-search-extra-files'.
2344 < Restrict agenda commands to buffer, subtree, or region.
2345 Press several times to get the desired effect.
2346 > Remove a previous restriction.
2347 # List \"stuck\" projects.
2348 ! Configure what \"stuck\" means.
2349 C Configure custom agenda commands.
2351 More commands can be added by configuring the variable
2352 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2353 searches can be pre-defined in this way.
2355 If the current buffer is in Org-mode and visiting a file, you can also
2356 first press `<' once to indicate that the agenda should be temporarily
2357 \(until the next use of \\[org-agenda]) restricted to the current file.
2358 Pressing `<' twice means to restrict to the current subtree or region
2359 \(if active)."
2360 (interactive "P")
2361 (catch 'exit
2362 (let* ((prefix-descriptions nil)
2363 (org-agenda-buffer-name org-agenda-buffer-name)
2364 (org-agenda-window-setup (if (equal (buffer-name)
2365 org-agenda-buffer-name)
2366 'current-window
2367 org-agenda-window-setup))
2368 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2369 (org-agenda-custom-commands
2370 ;; normalize different versions
2371 (delq nil
2372 (mapcar
2373 (lambda (x)
2374 (cond ((stringp (cdr x))
2375 (push x prefix-descriptions)
2376 nil)
2377 ((stringp (nth 1 x)) x)
2378 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2379 (t (cons (car x) (cons "" (cdr x))))))
2380 org-agenda-custom-commands)))
2381 (buf (current-buffer))
2382 (bfn (buffer-file-name (buffer-base-buffer)))
2383 entry key type match lprops ans)
2384 ;; Turn off restriction unless there is an overriding one,
2385 (unless org-agenda-overriding-restriction
2386 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2387 ;; There is a request to keep the file list in place
2388 (put 'org-agenda-files 'org-restrict nil))
2389 (setq org-agenda-restrict nil)
2390 (move-marker org-agenda-restrict-begin nil)
2391 (move-marker org-agenda-restrict-end nil))
2392 ;; Delete old local properties
2393 (put 'org-agenda-redo-command 'org-lprops nil)
2394 ;; Delete previously set last-arguments
2395 (put 'org-agenda-redo-command 'last-args nil)
2396 ;; Remember where this call originated
2397 (setq org-agenda-last-dispatch-buffer (current-buffer))
2398 (unless keys
2399 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2400 keys (car ans)
2401 restriction (cdr ans)))
2402 ;; If we have sticky agenda buffers, set a name for the buffer,
2403 ;; depending on the invoking keys. The user may still set this
2404 ;; as a command option, which will overwrite what we do here.
2405 (if org-agenda-sticky
2406 (setq org-agenda-buffer-name (format "*Org Agenda(%s)*" keys)))
2407 ;; Establish the restriction, if any
2408 (when (and (not org-agenda-overriding-restriction) restriction)
2409 (put 'org-agenda-files 'org-restrict (list bfn))
2410 (cond
2411 ((eq restriction 'region)
2412 (setq org-agenda-restrict t)
2413 (move-marker org-agenda-restrict-begin (region-beginning))
2414 (move-marker org-agenda-restrict-end (region-end)))
2415 ((eq restriction 'subtree)
2416 (save-excursion
2417 (setq org-agenda-restrict t)
2418 (org-back-to-heading t)
2419 (move-marker org-agenda-restrict-begin (point))
2420 (move-marker org-agenda-restrict-end
2421 (progn (org-end-of-subtree t)))))))
2423 ;; For example the todo list should not need it (but does...)
2424 (cond
2425 ((setq entry (assoc keys org-agenda-custom-commands))
2426 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2427 (progn
2428 (setq type (nth 2 entry) match (eval (nth 3 entry))
2429 lprops (nth 4 entry))
2430 (put 'org-agenda-redo-command 'org-lprops lprops)
2431 (cond
2432 ((eq type 'agenda)
2433 (org-let lprops '(org-agenda-list current-prefix-arg)))
2434 ((eq type 'alltodo)
2435 (org-let lprops '(org-todo-list current-prefix-arg)))
2436 ((eq type 'search)
2437 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2438 ((eq type 'stuck)
2439 (org-let lprops '(org-agenda-list-stuck-projects
2440 current-prefix-arg)))
2441 ((eq type 'tags)
2442 (org-let lprops '(org-tags-view current-prefix-arg match)))
2443 ((eq type 'tags-todo)
2444 (org-let lprops '(org-tags-view '(4) match)))
2445 ((eq type 'todo)
2446 (org-let lprops '(org-todo-list match)))
2447 ((eq type 'tags-tree)
2448 (org-check-for-org-mode)
2449 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2450 ((eq type 'todo-tree)
2451 (org-check-for-org-mode)
2452 (org-let lprops
2453 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2454 (regexp-quote match) "\\>"))))
2455 ((eq type 'occur-tree)
2456 (org-check-for-org-mode)
2457 (org-let lprops '(org-occur match)))
2458 ((functionp type)
2459 (org-let lprops '(funcall type match)))
2460 ((fboundp type)
2461 (org-let lprops '(funcall type match)))
2462 (t (error "Invalid custom agenda command type %s" type))))
2463 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2464 ((equal keys "C")
2465 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2466 (customize-variable 'org-agenda-custom-commands))
2467 ((equal keys "a") (call-interactively 'org-agenda-list))
2468 ((equal keys "s") (call-interactively 'org-search-view))
2469 ((equal keys "t") (call-interactively 'org-todo-list))
2470 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2471 ((equal keys "m") (call-interactively 'org-tags-view))
2472 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2473 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2474 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2475 (org-add-hook
2476 'post-command-hook
2477 (lambda ()
2478 (unless (current-message)
2479 (let* ((m (org-agenda-get-any-marker))
2480 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2481 (when note
2482 (message (concat
2483 "FLAGGING-NOTE ([?] for more info): "
2484 (org-add-props
2485 (replace-regexp-in-string
2486 "\\\\n" "//"
2487 (copy-sequence note))
2488 nil 'face 'org-warning)))))))
2489 t t))
2490 ((equal keys "L")
2491 (unless (derived-mode-p 'org-mode)
2492 (error "This is not an Org-mode file"))
2493 (unless restriction
2494 (put 'org-agenda-files 'org-restrict (list bfn))
2495 (org-call-with-arg 'org-timeline arg)))
2496 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2497 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2498 ((equal keys "!") (customize-variable 'org-stuck-projects))
2499 (t (error "Invalid agenda key"))))))
2501 (defun org-agenda-append-agenda ()
2502 "Append another agenda view to the current one.
2503 This function allows interactive building of block agendas.
2504 Agenda views are separated by `org-agenda-block-separator'."
2505 (interactive)
2506 (unless (string= (buffer-name) org-agenda-buffer-name)
2507 (error "Can only append from within agenda buffer"))
2508 (let ((org-agenda-multi t))
2509 (org-agenda)
2510 (widen)))
2512 (defun org-agenda-normalize-custom-commands (cmds)
2513 (delq nil
2514 (mapcar
2515 (lambda (x)
2516 (cond ((stringp (cdr x)) nil)
2517 ((stringp (nth 1 x)) x)
2518 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2519 (t (cons (car x) (cons "" (cdr x))))))
2520 cmds)))
2522 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2523 "The user interface for selecting an agenda command."
2524 (catch 'exit
2525 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2526 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2527 (region-p (org-region-active-p))
2528 (custom org-agenda-custom-commands)
2529 (selstring "")
2530 restriction second-time
2531 c entry key type match prefixes rmheader header-end custom1 desc
2532 line lines left right n n1)
2533 (save-window-excursion
2534 (delete-other-windows)
2535 (org-switch-to-buffer-other-window " *Agenda Commands*")
2536 (erase-buffer)
2537 (insert (eval-when-compile
2538 (let ((header
2539 "Press key for an agenda command: < Buffer, subtree/region restriction
2540 -------------------------------- > Remove restriction
2541 a Agenda for current week or day e Export agenda views
2542 t List of all TODO entries T Entries with special TODO kwd
2543 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2544 L Timeline for current buffer # List stuck projects (!=configure)
2545 s Search for keywords * Toggle sticky agenda views
2546 / Multi-occur ? Find :FLAGGED: entries
2547 C Configure custom agenda commands
2549 (start 0))
2550 (while (string-match
2551 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2552 header start)
2553 (setq start (match-end 0))
2554 (add-text-properties (match-beginning 2) (match-end 2)
2555 '(face bold) header))
2556 header)))
2557 (setq header-end (move-marker (make-marker) (point)))
2558 (while t
2559 (setq custom1 custom)
2560 (when (eq rmheader t)
2561 (org-goto-line 1)
2562 (re-search-forward ":" nil t)
2563 (delete-region (match-end 0) (point-at-eol))
2564 (forward-char 1)
2565 (looking-at "-+")
2566 (delete-region (match-end 0) (point-at-eol))
2567 (move-marker header-end (match-end 0)))
2568 (goto-char header-end)
2569 (delete-region (point) (point-max))
2571 ;; Produce all the lines that describe custom commands and prefixes
2572 (setq lines nil)
2573 (while (setq entry (pop custom1))
2574 (setq key (car entry) desc (nth 1 entry)
2575 type (nth 2 entry)
2576 match (nth 3 entry))
2577 (if (> (length key) 1)
2578 (add-to-list 'prefixes (string-to-char key))
2579 (setq line
2580 (format
2581 "%-4s%-14s"
2582 (org-add-props (copy-sequence key)
2583 '(face bold))
2584 (cond
2585 ((string-match "\\S-" desc) desc)
2586 ((eq type 'agenda) "Agenda for current week or day")
2587 ((eq type 'alltodo) "List of all TODO entries")
2588 ((eq type 'search) "Word search")
2589 ((eq type 'stuck) "List of stuck projects")
2590 ((eq type 'todo) "TODO keyword")
2591 ((eq type 'tags) "Tags query")
2592 ((eq type 'tags-todo) "Tags (TODO)")
2593 ((eq type 'tags-tree) "Tags tree")
2594 ((eq type 'todo-tree) "TODO kwd tree")
2595 ((eq type 'occur-tree) "Occur tree")
2596 ((functionp type) (if (symbolp type)
2597 (symbol-name type)
2598 "Lambda expression"))
2599 (t "???"))))
2600 (if org-agenda-menu-show-matcher
2601 (setq line
2602 (concat line ": "
2603 (cond
2604 ((stringp match)
2605 (setq match (copy-sequence match))
2606 (org-add-props match nil 'face 'org-warning))
2607 (match
2608 (format "set of %d commands" (length match)))
2609 (t ""))))
2610 (if (org-string-nw-p match)
2611 (add-text-properties
2612 0 (length line) (list 'help-echo
2613 (concat "Matcher: "match)) line)))
2614 (push line lines)))
2615 (setq lines (nreverse lines))
2616 (when prefixes
2617 (mapc (lambda (x)
2618 (push
2619 (format "%s %s"
2620 (org-add-props (char-to-string x)
2621 nil 'face 'bold)
2622 (or (cdr (assoc (concat selstring
2623 (char-to-string x))
2624 prefix-descriptions))
2625 "Prefix key"))
2626 lines))
2627 prefixes))
2629 ;; Check if we should display in two columns
2630 (if org-agenda-menu-two-column
2631 (progn
2632 (setq n (length lines)
2633 n1 (+ (/ n 2) (mod n 2))
2634 right (nthcdr n1 lines)
2635 left (copy-sequence lines))
2636 (setcdr (nthcdr (1- n1) left) nil))
2637 (setq left lines right nil))
2638 (while left
2639 (insert "\n" (pop left))
2640 (when right
2641 (if (< (current-column) 40)
2642 (move-to-column 40 t)
2643 (insert " "))
2644 (insert (pop right))))
2646 ;; Make the window the right size
2647 (goto-char (point-min))
2648 (if second-time
2649 (if (not (pos-visible-in-window-p (point-max)))
2650 (org-fit-window-to-buffer))
2651 (setq second-time t)
2652 (org-fit-window-to-buffer))
2654 ;; Ask for selection
2655 (message "Press key for agenda command%s:"
2656 (if (or restrict-ok org-agenda-overriding-restriction)
2657 (if org-agenda-overriding-restriction
2658 " (restriction lock active)"
2659 (if restriction
2660 (format " (restricted to %s)" restriction)
2661 " (unrestricted)"))
2662 ""))
2663 (setq c (read-char-exclusive))
2664 (message "")
2665 (cond
2666 ((assoc (char-to-string c) custom)
2667 (setq selstring (concat selstring (char-to-string c)))
2668 (throw 'exit (cons selstring restriction)))
2669 ((memq c prefixes)
2670 (setq selstring (concat selstring (char-to-string c))
2671 prefixes nil
2672 rmheader (or rmheader t)
2673 custom (delq nil (mapcar
2674 (lambda (x)
2675 (if (or (= (length (car x)) 1)
2676 (/= (string-to-char (car x)) c))
2678 (cons (substring (car x) 1) (cdr x))))
2679 custom))))
2680 ((eq c ?*)
2681 (call-interactively 'org-toggle-sticky-agenda)
2682 (sit-for 2))
2683 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2684 (message "Restriction is only possible in Org-mode buffers")
2685 (ding) (sit-for 1))
2686 ((eq c ?1)
2687 (org-agenda-remove-restriction-lock 'noupdate)
2688 (setq restriction 'buffer))
2689 ((eq c ?0)
2690 (org-agenda-remove-restriction-lock 'noupdate)
2691 (setq restriction (if region-p 'region 'subtree)))
2692 ((eq c ?<)
2693 (org-agenda-remove-restriction-lock 'noupdate)
2694 (setq restriction
2695 (cond
2696 ((eq restriction 'buffer)
2697 (if region-p 'region 'subtree))
2698 ((memq restriction '(subtree region))
2699 nil)
2700 (t 'buffer))))
2701 ((eq c ?>)
2702 (org-agenda-remove-restriction-lock 'noupdate)
2703 (setq restriction nil))
2704 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2705 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2706 ((and (> (length selstring) 0) (eq c ?\d))
2707 (delete-window)
2708 (org-agenda-get-restriction-and-command prefix-descriptions))
2710 ((equal c ?q) (error "Abort"))
2711 (t (error "Invalid key %c" c))))))))
2713 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2714 (defvar org-agenda-last-arguments nil
2715 "The arguments of the previous call to `org-agenda'.")
2716 (defun org-agenda-run-series (name series)
2717 (org-let (nth 1 series) '(org-prepare-agenda name))
2718 ;; We need to reset agenda markers here, because when constructing a
2719 ;; block agenda, the individual blocks do not do that.
2720 (org-agenda-reset-markers)
2721 (let* ((org-agenda-multi t)
2722 (redo (list 'org-agenda-run-series name (list 'quote series)))
2723 (org-agenda-overriding-arguments
2724 (or org-agenda-overriding-arguments
2725 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2726 (get 'org-agenda-redo-command 'last-args))))
2727 (cmds (car series))
2728 (gprops (nth 1 series))
2729 match ;; The byte compiler incorrectly complains about this. Keep it!
2730 cmd type lprops)
2731 (while (setq cmd (pop cmds))
2732 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2733 (cond
2734 ((eq type 'agenda)
2735 (org-let2 gprops lprops
2736 '(call-interactively 'org-agenda-list)))
2737 ((eq type 'alltodo)
2738 (org-let2 gprops lprops
2739 '(call-interactively 'org-todo-list)))
2740 ((eq type 'search)
2741 (org-let2 gprops lprops
2742 '(org-search-view current-prefix-arg match nil)))
2743 ((eq type 'stuck)
2744 (org-let2 gprops lprops
2745 '(call-interactively 'org-agenda-list-stuck-projects)))
2746 ((eq type 'tags)
2747 (org-let2 gprops lprops
2748 '(org-tags-view current-prefix-arg match)))
2749 ((eq type 'tags-todo)
2750 (org-let2 gprops lprops
2751 '(org-tags-view '(4) match)))
2752 ((eq type 'todo)
2753 (org-let2 gprops lprops
2754 '(org-todo-list match)))
2755 ((fboundp type)
2756 (org-let2 gprops lprops
2757 '(funcall type match)))
2758 (t (error "Invalid type in command series"))))
2759 (widen)
2760 (setq org-agenda-redo-command redo)
2761 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2762 (goto-char (point-min)))
2763 (org-fit-agenda-window)
2764 (org-let (nth 1 series) '(org-finalize-agenda)))
2766 ;;;###autoload
2767 (defmacro org-batch-agenda (cmd-key &rest parameters)
2768 "Run an agenda command in batch mode and send the result to STDOUT.
2769 If CMD-KEY is a string of length 1, it is used as a key in
2770 `org-agenda-custom-commands' and triggers this command. If it is a
2771 longer string it is used as a tags/todo match string.
2772 Parameters are alternating variable names and values that will be bound
2773 before running the agenda command."
2774 (org-eval-in-environment (org-make-parameter-alist parameters)
2775 (if (> (length cmd-key) 2)
2776 (org-tags-view nil cmd-key)
2777 (org-agenda nil cmd-key)))
2778 (set-buffer org-agenda-buffer-name)
2779 (princ (buffer-string)))
2780 (def-edebug-spec org-batch-agenda (form &rest sexp))
2782 (defvar org-agenda-info nil)
2784 ;;;###autoload
2785 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2786 "Run an agenda command in batch mode and send the result to STDOUT.
2787 If CMD-KEY is a string of length 1, it is used as a key in
2788 `org-agenda-custom-commands' and triggers this command. If it is a
2789 longer string it is used as a tags/todo match string.
2790 Parameters are alternating variable names and values that will be bound
2791 before running the agenda command.
2793 The output gives a line for each selected agenda item. Each
2794 item is a list of comma-separated values, like this:
2796 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2798 category The category of the item
2799 head The headline, without TODO kwd, TAGS and PRIORITY
2800 type The type of the agenda entry, can be
2801 todo selected in TODO match
2802 tagsmatch selected in tags match
2803 diary imported from diary
2804 deadline a deadline on given date
2805 scheduled scheduled on given date
2806 timestamp entry has timestamp on given date
2807 closed entry was closed on given date
2808 upcoming-deadline warning about deadline
2809 past-scheduled forwarded scheduled item
2810 block entry has date block including g. date
2811 todo The todo keyword, if any
2812 tags All tags including inherited ones, separated by colons
2813 date The relevant date, like 2007-2-14
2814 time The time, like 15:00-16:50
2815 extra Sting with extra planning info
2816 priority-l The priority letter if any was given
2817 priority-n The computed numerical priority
2818 agenda-day The day in the agenda where this is listed"
2819 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2820 (org-make-parameter-alist parameters))
2821 (if (> (length cmd-key) 2)
2822 (org-tags-view nil cmd-key)
2823 (org-agenda nil cmd-key)))
2824 (set-buffer org-agenda-buffer-name)
2825 (let* ((lines (org-split-string (buffer-string) "\n"))
2826 line)
2827 (while (setq line (pop lines))
2828 (catch 'next
2829 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2830 (setq org-agenda-info
2831 (org-fix-agenda-info (text-properties-at 0 line)))
2832 (princ
2833 (mapconcat 'org-agenda-export-csv-mapper
2834 '(org-category txt type todo tags date time extra
2835 priority-letter priority agenda-day)
2836 ","))
2837 (princ "\n")))))
2838 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2840 (defun org-fix-agenda-info (props)
2841 "Make sure all properties on an agenda item have a canonical form.
2842 This ensures the export commands can easily use it."
2843 (let (tmp re)
2844 (when (setq tmp (plist-get props 'tags))
2845 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2846 (when (setq tmp (plist-get props 'date))
2847 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2848 (let ((calendar-date-display-form '(year "-" month "-" day)))
2849 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2851 (setq tmp (calendar-date-string tmp)))
2852 (setq props (plist-put props 'date tmp)))
2853 (when (setq tmp (plist-get props 'day))
2854 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2855 (let ((calendar-date-display-form '(year "-" month "-" day)))
2856 (setq tmp (calendar-date-string tmp)))
2857 (setq props (plist-put props 'day tmp))
2858 (setq props (plist-put props 'agenda-day tmp)))
2859 (when (setq tmp (plist-get props 'txt))
2860 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2861 (plist-put props 'priority-letter (match-string 1 tmp))
2862 (setq tmp (replace-match "" t t tmp)))
2863 (when (and (setq re (plist-get props 'org-todo-regexp))
2864 (setq re (concat "\\`\\.*" re " ?"))
2865 (string-match re tmp))
2866 (plist-put props 'todo (match-string 1 tmp))
2867 (setq tmp (replace-match "" t t tmp)))
2868 (plist-put props 'txt tmp)))
2869 props)
2871 (defun org-agenda-export-csv-mapper (prop)
2872 (let ((res (plist-get org-agenda-info prop)))
2873 (setq res
2874 (cond
2875 ((not res) "")
2876 ((stringp res) res)
2877 (t (prin1-to-string res))))
2878 (while (string-match "," res)
2879 (setq res (replace-match ";" t t res)))
2880 (org-trim res)))
2883 ;;;###autoload
2884 (defun org-store-agenda-views (&rest parameters)
2885 (interactive)
2886 (eval (list 'org-batch-store-agenda-views)))
2888 ;;;###autoload
2889 (defmacro org-batch-store-agenda-views (&rest parameters)
2890 "Run all custom agenda commands that have a file argument."
2891 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2892 (pop-up-frames nil)
2893 (dir default-directory)
2894 (pars (org-make-parameter-alist parameters))
2895 cmd thiscmdkey files opts cmd-or-set)
2896 (save-window-excursion
2897 (while cmds
2898 (setq cmd (pop cmds)
2899 thiscmdkey (car cmd)
2900 cmd-or-set (nth 2 cmd)
2901 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2902 files (nth (if (listp cmd-or-set) 4 5) cmd))
2903 (if (stringp files) (setq files (list files)))
2904 (when files
2905 (org-eval-in-environment (append org-agenda-exporter-settings
2906 opts pars)
2907 (org-agenda nil thiscmdkey))
2908 (set-buffer org-agenda-buffer-name)
2909 (while files
2910 (org-eval-in-environment (append org-agenda-exporter-settings
2911 opts pars)
2912 (org-agenda-write (expand-file-name (pop files) dir) nil t)))
2913 (and (get-buffer org-agenda-buffer-name)
2914 (kill-buffer org-agenda-buffer-name)))))))
2915 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2917 (defun org-agenda-mark-header-line (pos)
2918 "Mark the line at POS as an agenda structure header."
2919 (save-excursion
2920 (goto-char pos)
2921 (put-text-property (point-at-bol) (point-at-eol)
2922 'org-agenda-structural-header t)
2923 (when org-agenda-title-append
2924 (put-text-property (point-at-bol) (point-at-eol)
2925 'org-agenda-title-append org-agenda-title-append))))
2927 (defvar org-mobile-creating-agendas)
2928 (defvar org-agenda-write-buffer-name "Agenda View")
2929 (defun org-agenda-write (file &optional open nosettings)
2930 "Write the current buffer (an agenda view) as a file.
2931 Depending on the extension of the file name, plain text (.txt),
2932 HTML (.html or .htm) or Postscript (.ps) is produced.
2933 If the extension is .ics, run icalendar export over all files used
2934 to construct the agenda and limit the export to entries listed in the
2935 agenda now.
2936 With prefix argument OPEN, open the new file immediately.
2937 If NOSETTINGS is given, do not scope the settings of
2938 `org-agenda-exporter-settings' into the export commands. This is used when
2939 the settings have already been scoped and we do not wish to overrule other,
2940 higher priority settings."
2941 (interactive "FWrite agenda to file: \nP")
2942 (if (not (file-writable-p file))
2943 (error "Cannot write agenda to file %s" file))
2944 (org-let (if nosettings nil org-agenda-exporter-settings)
2945 '(save-excursion
2946 (save-window-excursion
2947 (org-agenda-mark-filtered-text)
2948 (let ((bs (copy-sequence (buffer-string))) beg)
2949 (org-agenda-unmark-filtered-text)
2950 (with-temp-buffer
2951 (rename-buffer org-agenda-write-buffer-name t)
2952 (set-buffer-modified-p nil)
2953 (insert bs)
2954 (org-agenda-remove-marked-text 'org-filtered)
2955 (while (setq beg (text-property-any (point-min) (point-max)
2956 'org-filtered t))
2957 (delete-region
2958 beg (or (next-single-property-change beg 'org-filtered)
2959 (point-max))))
2960 (run-hooks 'org-agenda-before-write-hook)
2961 (cond
2962 ((org-bound-and-true-p org-mobile-creating-agendas)
2963 (org-mobile-write-agenda-for-mobile file))
2964 ((string-match "\\.html?\\'" file)
2965 (require 'htmlize)
2966 (set-buffer (htmlize-buffer (current-buffer)))
2968 (when (and org-agenda-export-html-style
2969 (string-match "<style>" org-agenda-export-html-style))
2970 ;; replace <style> section with org-agenda-export-html-style
2971 (goto-char (point-min))
2972 (kill-region (- (search-forward "<style") 6)
2973 (search-forward "</style>"))
2974 (insert org-agenda-export-html-style))
2975 (write-file file)
2976 (kill-buffer (current-buffer))
2977 (message "HTML written to %s" file))
2978 ((string-match "\\.ps\\'" file)
2979 (require 'ps-print)
2980 (ps-print-buffer-with-faces file)
2981 (message "Postscript written to %s" file))
2982 ((string-match "\\.pdf\\'" file)
2983 (require 'ps-print)
2984 (ps-print-buffer-with-faces
2985 (concat (file-name-sans-extension file) ".ps"))
2986 (call-process "ps2pdf" nil nil nil
2987 (expand-file-name
2988 (concat (file-name-sans-extension file) ".ps"))
2989 (expand-file-name file))
2990 (delete-file (concat (file-name-sans-extension file) ".ps"))
2991 (message "PDF written to %s" file))
2992 ((string-match "\\.ics\\'" file)
2993 (require 'org-icalendar)
2994 (let ((org-agenda-marker-table
2995 (org-create-marker-find-array
2996 (org-agenda-collect-markers)))
2997 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2998 (org-combined-agenda-icalendar-file file))
2999 (apply 'org-export-icalendar 'combine
3000 (org-agenda-files nil 'ifmode))))
3002 (let ((bs (buffer-string)))
3003 (find-file file)
3004 (erase-buffer)
3005 (insert bs)
3006 (save-buffer 0)
3007 (kill-buffer (current-buffer))
3008 (message "Plain text written to %s" file))))))))
3009 (set-buffer org-agenda-buffer-name))
3010 (when open (org-open-file file)))
3012 (defvar org-agenda-tag-filter-overlays nil)
3013 (defvar org-agenda-cat-filter-overlays nil)
3015 (defun org-agenda-mark-filtered-text ()
3016 "Mark all text hidden by filtering with a text property."
3017 (let ((inhibit-read-only t))
3018 (mapc
3019 (lambda (o)
3020 (when (equal (overlay-buffer o) (current-buffer))
3021 (put-text-property
3022 (overlay-start o) (overlay-end o)
3023 'org-filtered t)))
3024 (append org-agenda-tag-filter-overlays
3025 org-agenda-cat-filter-overlays))))
3027 (defun org-agenda-unmark-filtered-text ()
3028 "Remove the filtering text property."
3029 (let ((inhibit-read-only t))
3030 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3032 (defun org-agenda-remove-marked-text (property &optional value)
3033 "Delete all text marked with VALUE of PROPERTY.
3034 VALUE defaults to t."
3035 (let (beg)
3036 (setq value (or value t))
3037 (while (setq beg (text-property-any (point-min) (point-max)
3038 property value))
3039 (delete-region
3040 beg (or (next-single-property-change beg 'org-filtered)
3041 (point-max))))))
3043 (defun org-agenda-add-entry-text ()
3044 "Add entry text to agenda lines.
3045 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3046 entry text following headings shown in the agenda.
3047 Drawers will be excluded, also the line with scheduling/deadline info."
3048 (when (and (> org-agenda-add-entry-text-maxlines 0)
3049 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3050 (let (m txt)
3051 (goto-char (point-min))
3052 (while (not (eobp))
3053 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3054 (beginning-of-line 2)
3055 (setq txt (org-agenda-get-some-entry-text
3056 m org-agenda-add-entry-text-maxlines " > "))
3057 (end-of-line 1)
3058 (if (string-match "\\S-" txt)
3059 (insert "\n" txt)
3060 (or (eobp) (forward-char 1))))))))
3062 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3063 &rest keep)
3064 "Extract entry text from MARKER, at most N-LINES lines.
3065 This will ignore drawers etc, just get the text.
3066 If INDENT is given, prefix every line with this string. If KEEP is
3067 given, it is a list of symbols, defining stuff that should not be
3068 removed from the entry content. Currently only `planning' is allowed here."
3069 (let (txt drawer-re kwd-time-re ind)
3070 (save-excursion
3071 (with-current-buffer (marker-buffer marker)
3072 (if (not (derived-mode-p 'org-mode))
3073 (setq txt "")
3074 (save-excursion
3075 (save-restriction
3076 (widen)
3077 (goto-char marker)
3078 (end-of-line 1)
3079 (setq txt (buffer-substring
3080 (min (1+ (point)) (point-max))
3081 (progn (outline-next-heading) (point)))
3082 drawer-re org-drawer-regexp
3083 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3084 ".*\n?"))
3085 (with-temp-buffer
3086 (insert txt)
3087 (when org-agenda-add-entry-text-descriptive-links
3088 (goto-char (point-min))
3089 (while (org-activate-bracket-links (point-max))
3090 (add-text-properties (match-beginning 0) (match-end 0)
3091 '(face org-link))))
3092 (goto-char (point-min))
3093 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3094 (set-text-properties (match-beginning 0) (match-end 0)
3095 nil))
3096 (goto-char (point-min))
3097 (while (re-search-forward drawer-re nil t)
3098 (delete-region
3099 (match-beginning 0)
3100 (progn (re-search-forward
3101 "^[ \t]*:END:.*\n?" nil 'move)
3102 (point))))
3103 (unless (member 'planning keep)
3104 (goto-char (point-min))
3105 (while (re-search-forward kwd-time-re nil t)
3106 (replace-match "")))
3107 (goto-char (point-min))
3108 (when org-agenda-entry-text-exclude-regexps
3109 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3110 (while (setq re (pop re-list))
3111 (goto-char (point-min))
3112 (while (re-search-forward re nil t)
3113 (replace-match "")))))
3114 (goto-char (point-max))
3115 (skip-chars-backward " \t\n")
3116 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3118 ;; find and remove min common indentation
3119 (goto-char (point-min))
3120 (untabify (point-min) (point-max))
3121 (setq ind (org-get-indentation))
3122 (while (not (eobp))
3123 (unless (looking-at "[ \t]*$")
3124 (setq ind (min ind (org-get-indentation))))
3125 (beginning-of-line 2))
3126 (goto-char (point-min))
3127 (while (not (eobp))
3128 (unless (looking-at "[ \t]*$")
3129 (move-to-column ind)
3130 (delete-region (point-at-bol) (point)))
3131 (beginning-of-line 2))
3133 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3135 (goto-char (point-min))
3136 (when indent
3137 (while (and (not (eobp)) (re-search-forward "^" nil t))
3138 (replace-match indent t t)))
3139 (goto-char (point-min))
3140 (while (looking-at "[ \t]*\n") (replace-match ""))
3141 (goto-char (point-max))
3142 (when (> (org-current-line)
3143 n-lines)
3144 (org-goto-line (1+ n-lines))
3145 (backward-char 1))
3146 (setq txt (buffer-substring (point-min) (point)))))))))
3147 txt))
3149 (defun org-agenda-collect-markers ()
3150 "Collect the markers pointing to entries in the agenda buffer."
3151 (let (m markers)
3152 (save-excursion
3153 (goto-char (point-min))
3154 (while (not (eobp))
3155 (when (setq m (or (org-get-at-bol 'org-hd-marker)
3156 (org-get-at-bol 'org-marker)))
3157 (push m markers))
3158 (beginning-of-line 2)))
3159 (nreverse markers)))
3161 (defun org-create-marker-find-array (marker-list)
3162 "Create a alist of files names with all marker positions in that file."
3163 (let (f tbl m a p)
3164 (while (setq m (pop marker-list))
3165 (setq p (marker-position m)
3166 f (buffer-file-name (or (buffer-base-buffer
3167 (marker-buffer m))
3168 (marker-buffer m))))
3169 (if (setq a (assoc f tbl))
3170 (push (marker-position m) (cdr a))
3171 (push (list f p) tbl)))
3172 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3173 tbl)))
3175 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3176 (defun org-check-agenda-marker-table ()
3177 "Check of the current entry is on the marker list."
3178 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3180 (and (setq a (assoc file org-agenda-marker-table))
3181 (save-match-data
3182 (save-excursion
3183 (org-back-to-heading t)
3184 (member (point) (cdr a)))))))
3186 (defun org-check-for-org-mode ()
3187 "Make sure current buffer is in org-mode. Error if not."
3188 (or (derived-mode-p 'org-mode)
3189 (error "Cannot execute org-mode agenda command on buffer in %s"
3190 major-mode)))
3192 (defun org-fit-agenda-window ()
3193 "Fit the window to the buffer size."
3194 (and (memq org-agenda-window-setup '(reorganize-frame))
3195 (fboundp 'fit-window-to-buffer)
3196 (org-fit-window-to-buffer
3198 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3199 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3201 ;;; Agenda prepare and finalize
3203 (defvar org-agenda-multi nil) ; dynamically scoped
3204 (defvar org-agenda-buffer-name "*Org Agenda*")
3205 (defvar org-pre-agenda-window-conf nil)
3206 (defvar org-agenda-columns-active nil)
3207 (defvar org-agenda-name nil)
3208 (defvar org-agenda-tag-filter nil)
3209 (defvar org-agenda-category-filter nil)
3210 (defvar org-agenda-top-category-filter nil)
3211 (defvar org-agenda-tag-filter-while-redo nil)
3212 (defvar org-agenda-tag-filter-preset nil
3213 "A preset of the tags filter used for secondary agenda filtering.
3214 This must be a list of strings, each string must be a single tag preceded
3215 by \"+\" or \"-\".
3216 This variable should not be set directly, but agenda custom commands can
3217 bind it in the options section. The preset filter is a global property of
3218 the entire agenda view. In a block agenda, it will not work reliably to
3219 define a filter for one of the individual blocks. You need to set it in
3220 the global options and expect it to be applied to the entire view.")
3222 (defvar org-agenda-category-filter-preset nil
3223 "A preset of the category filter used for secondary agenda filtering.
3224 This must be a list of strings, each string must be a single category
3225 preceded by \"+\" or \"-\".
3226 This variable should not be set directly, but agenda custom commands can
3227 bind it in the options section. The preset filter is a global property of
3228 the entire agenda view. In a block agenda, it will not work reliably to
3229 define a filter for one of the individual blocks. You need to set it in
3230 the global options and expect it to be applied to the entire view.")
3233 (defun org-agenda-use-sticky-p ()
3234 "Return non-NIL if existing agenda buffer named
3235 `org-agenda-buffer-name' exists, and should be shown instead of
3236 generating a new one"
3237 (and
3238 ;; turned off by user
3239 org-agenda-sticky
3240 ;; For multi-agenda buffer already exists
3241 (not org-agenda-multi)
3242 ;; buffer found
3243 (get-buffer org-agenda-buffer-name)
3244 ;; C-u parameter is same as last call
3245 (with-current-buffer (get-buffer org-agenda-buffer-name)
3246 (and
3247 (equal current-prefix-arg
3248 org-agenda-last-prefix-arg)
3249 ;; In case user turned stickiness on, while having existing
3250 ;; Agenda buffer active, don't reuse that buffer, because it
3251 ;; does not have org variables local
3252 org-agenda-this-buffer-is-sticky))))
3254 (defun org-prepare-agenda-window (abuf)
3255 "Setup agenda buffer in the window"
3256 (let* ((awin (get-buffer-window abuf))
3257 wconf)
3258 (cond
3259 ((equal (current-buffer) abuf) nil)
3260 (awin (select-window awin))
3261 ((not (setq wconf (current-window-configuration))))
3262 ((equal org-agenda-window-setup 'current-window)
3263 (org-pop-to-buffer-same-window abuf))
3264 ((equal org-agenda-window-setup 'other-window)
3265 (org-switch-to-buffer-other-window abuf))
3266 ((equal org-agenda-window-setup 'other-frame)
3267 (switch-to-buffer-other-frame abuf))
3268 ((equal org-agenda-window-setup 'reorganize-frame)
3269 (delete-other-windows)
3270 (org-switch-to-buffer-other-window abuf)))
3271 ;; additional test in case agenda is invoked from within agenda
3272 ;; buffer via elisp link
3273 (unless (equal (current-buffer) abuf)
3274 (org-pop-to-buffer-same-window abuf))
3275 (setq org-pre-agenda-window-conf wconf)))
3277 (defun org-prepare-agenda (&optional name)
3278 (if (org-agenda-use-sticky-p)
3279 (progn
3280 ;; Popup existing buffer
3281 (org-prepare-agenda-window (get-buffer org-agenda-buffer-name))
3282 (message "Sticky Agenda buffer, use `r' to refresh")
3283 (throw 'exit nil))
3284 (setq org-todo-keywords-for-agenda nil)
3285 (setq org-drawers-for-agenda nil)
3286 (unless org-agenda-persistent-filter
3287 (setq org-agenda-tag-filter nil
3288 org-agenda-category-filter nil))
3289 (put 'org-agenda-tag-filter :preset-filter org-agenda-tag-filter-preset)
3290 (put 'org-agenda-category-filter :preset-filter
3291 org-agenda-category-filter-preset)
3292 (if org-agenda-multi
3293 (progn
3294 (setq buffer-read-only nil)
3295 (goto-char (point-max))
3296 (unless (or (bobp) org-agenda-compact-blocks
3297 (not org-agenda-block-separator))
3298 (insert "\n"
3299 (if (stringp org-agenda-block-separator)
3300 org-agenda-block-separator
3301 (make-string (window-width) org-agenda-block-separator))
3302 "\n"))
3303 (narrow-to-region (point) (point-max)))
3304 (setq org-done-keywords-for-agenda nil)
3306 ;; Setting any org variables that are in org-agenda-local-vars
3307 ;; list need to be done after the prepare call
3308 (org-prepare-agenda-window (get-buffer-create org-agenda-buffer-name))
3309 (setq buffer-read-only nil)
3310 (org-agenda-reset-markers)
3311 (let ((inhibit-read-only t)) (erase-buffer))
3312 (org-agenda-mode)
3313 (setq org-agenda-buffer (current-buffer))
3314 (setq org-agenda-contributing-files nil)
3315 (setq org-agenda-columns-active nil)
3316 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3317 (setq org-todo-keywords-for-agenda
3318 (org-uniquify org-todo-keywords-for-agenda))
3319 (setq org-done-keywords-for-agenda
3320 (org-uniquify org-done-keywords-for-agenda))
3321 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3322 (setq org-agenda-last-prefix-arg current-prefix-arg)
3323 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3324 (and name (not org-agenda-name)
3325 (org-set-local 'org-agenda-name name)))
3326 (setq buffer-read-only nil)))
3328 (defun org-finalize-agenda ()
3329 "Finishing touch for the agenda buffer, called just before displaying it."
3330 (unless org-agenda-multi
3331 (save-excursion
3332 (let ((inhibit-read-only t))
3333 (goto-char (point-min))
3334 (while (org-activate-bracket-links (point-max))
3335 (add-text-properties (match-beginning 0) (match-end 0)
3336 '(face org-link)))
3337 (org-agenda-align-tags)
3338 (unless org-agenda-with-colors
3339 (remove-text-properties (point-min) (point-max) '(face nil))))
3340 (if (and (boundp 'org-agenda-overriding-columns-format)
3341 org-agenda-overriding-columns-format)
3342 (org-set-local 'org-agenda-overriding-columns-format
3343 org-agenda-overriding-columns-format))
3344 (if (and (boundp 'org-agenda-view-columns-initially)
3345 org-agenda-view-columns-initially)
3346 (org-agenda-columns))
3347 (when org-agenda-fontify-priorities
3348 (org-agenda-fontify-priorities))
3349 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3350 (org-agenda-dim-blocked-tasks))
3351 (org-agenda-mark-clocking-task)
3352 (when org-agenda-entry-text-mode
3353 (org-agenda-entry-text-hide)
3354 (org-agenda-entry-text-show))
3355 (if (functionp 'org-habit-insert-consistency-graphs)
3356 (org-habit-insert-consistency-graphs))
3357 (run-hooks 'org-finalize-agenda-hook)
3358 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3359 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3360 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3361 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3362 (org-agenda-filter-apply org-agenda-category-filter 'category))
3363 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)
3366 (defun org-agenda-mark-clocking-task ()
3367 "Mark the current clock entry in the agenda if it is present."
3368 (mapc (lambda (o)
3369 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3370 (delete-overlay o)))
3371 (overlays-in (point-min) (point-max)))
3372 (when (marker-buffer org-clock-hd-marker)
3373 (save-excursion
3374 (goto-char (point-min))
3375 (let (s ov)
3376 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3377 (goto-char s)
3378 (when (equal (org-get-at-bol 'org-hd-marker)
3379 org-clock-hd-marker)
3380 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3381 (overlay-put ov 'type 'org-agenda-clocking)
3382 (overlay-put ov 'face 'org-agenda-clocking)
3383 (overlay-put ov 'help-echo
3384 "The clock is running in this item")))))))
3386 (defun org-agenda-fontify-priorities ()
3387 "Make highest priority lines bold, and lowest italic."
3388 (interactive)
3389 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3390 (delete-overlay o)))
3391 (overlays-in (point-min) (point-max)))
3392 (save-excursion
3393 (let ((inhibit-read-only t)
3394 b e p ov h l)
3395 (goto-char (point-min))
3396 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3397 (setq h (or (get-char-property (point) 'org-highest-priority)
3398 org-highest-priority)
3399 l (or (get-char-property (point) 'org-lowest-priority)
3400 org-lowest-priority)
3401 p (string-to-char (match-string 1))
3402 b (match-beginning 0)
3403 e (if (eq org-agenda-fontify-priorities 'cookies)
3404 (match-end 0)
3405 (point-at-eol))
3406 ov (make-overlay b e))
3407 (overlay-put
3408 ov 'face
3409 (cond ((org-face-from-face-or-color
3410 'priority nil
3411 (cdr (assoc p org-priority-faces))))
3412 ((and (listp org-agenda-fontify-priorities)
3413 (org-face-from-face-or-color
3414 'priority nil
3415 (cdr (assoc p org-agenda-fontify-priorities)))))
3416 ((equal p l) 'italic)
3417 ((equal p h) 'bold)))
3418 (overlay-put ov 'org-type 'org-priority)))))
3420 (defun org-agenda-dim-blocked-tasks ()
3421 "Dim currently blocked TODO's in the agenda display."
3422 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3423 (delete-overlay o)))
3424 (overlays-in (point-min) (point-max)))
3425 (save-excursion
3426 (let ((inhibit-read-only t)
3427 (org-depend-tag-blocked nil)
3428 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3429 org-blocked-by-checkboxes
3430 invis1 b e p ov h l)
3431 (goto-char (point-min))
3432 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3433 (and pos (goto-char (1+ pos))))
3434 (setq org-blocked-by-checkboxes nil invis1 invis)
3435 (let ((marker (org-get-at-bol 'org-hd-marker)))
3436 (when (and marker
3437 (with-current-buffer (marker-buffer marker)
3438 (save-excursion (goto-char marker)
3439 (org-entry-blocked-p))))
3440 (if org-blocked-by-checkboxes (setq invis1 nil))
3441 (setq b (if invis1
3442 (max (point-min) (1- (point-at-bol)))
3443 (point-at-bol))
3444 e (point-at-eol)
3445 ov (make-overlay b e))
3446 (if invis1
3447 (overlay-put ov 'invisible t)
3448 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3449 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3451 (defvar org-agenda-skip-function nil
3452 "Function to be called at each match during agenda construction.
3453 If this function returns nil, the current match should not be skipped.
3454 Otherwise, the function must return a position from where the search
3455 should be continued.
3456 This may also be a Lisp form, it will be evaluated.
3457 Never set this variable using `setq' or so, because then it will apply
3458 to all future agenda commands. If you do want a global skipping condition,
3459 use the option `org-agenda-skip-function-global' instead.
3460 The correct usage for `org-agenda-skip-function' is to bind it with
3461 `let' to scope it dynamically into the agenda-constructing command.
3462 A good way to set it is through options in `org-agenda-custom-commands'.")
3464 (defun org-agenda-skip ()
3465 "Throw to `:skip' in places that should be skipped.
3466 Also moves point to the end of the skipped region, so that search can
3467 continue from there."
3468 (let ((p (point-at-bol)) to)
3469 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3470 (get-text-property p :org-archived)
3471 (org-end-of-subtree t)
3472 (throw :skip t))
3473 (and org-agenda-skip-comment-trees
3474 (get-text-property p :org-comment)
3475 (org-end-of-subtree t)
3476 (throw :skip t))
3477 (if (equal (char-after p) ?#) (throw :skip t))
3478 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3479 (org-agenda-skip-eval org-agenda-skip-function)))
3480 (goto-char to)
3481 (throw :skip t))))
3483 (defun org-agenda-skip-eval (form)
3484 "If FORM is a function or a list, call (or eval) is and return result.
3485 `save-excursion' and `save-match-data' are wrapped around the call, so point
3486 and match data are returned to the previous state no matter what these
3487 functions do."
3488 (let (fp)
3489 (and form
3490 (or (setq fp (functionp form))
3491 (consp form))
3492 (save-excursion
3493 (save-match-data
3494 (if fp
3495 (funcall form)
3496 (eval form)))))))
3498 (defvar org-agenda-markers nil
3499 "List of all currently active markers created by `org-agenda'.")
3500 (defvar org-agenda-last-marker-time (org-float-time)
3501 "Creation time of the last agenda marker.")
3503 (defun org-agenda-new-marker (&optional pos)
3504 "Return a new agenda marker.
3505 Org-mode keeps a list of these markers and resets them when they are
3506 no longer in use."
3507 (let ((m (copy-marker (or pos (point)))))
3508 (setq org-agenda-last-marker-time (org-float-time))
3509 (if org-agenda-buffer
3510 (with-current-buffer org-agenda-buffer
3511 (push m org-agenda-markers))
3512 (push m org-agenda-markers))
3515 (defun org-agenda-reset-markers ()
3516 "Reset markers created by `org-agenda'."
3517 (while org-agenda-markers
3518 (move-marker (pop org-agenda-markers) nil)))
3520 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3521 "Save relative positions of markers in region.
3522 This check for agenda markers in all agenda buffers currently active."
3523 (dolist (buf (buffer-list))
3524 (with-current-buffer buf
3525 (when (eq major-mode 'org-agenda-mode)
3526 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3527 org-agenda-markers)))))
3529 ;;; Entry text mode
3531 (defun org-agenda-entry-text-show-here ()
3532 "Add some text from the entry as context to the current line."
3533 (let (m txt o)
3534 (setq m (org-get-at-bol 'org-hd-marker))
3535 (unless (marker-buffer m)
3536 (error "No marker points to an entry here"))
3537 (setq txt (concat "\n" (org-no-properties
3538 (org-agenda-get-some-entry-text
3539 m org-agenda-entry-text-maxlines " > "))))
3540 (when (string-match "\\S-" txt)
3541 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3542 (overlay-put o 'evaporate t)
3543 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3544 (overlay-put o 'after-string txt))))
3546 (defun org-agenda-entry-text-show ()
3547 "Add entry context for all agenda lines."
3548 (interactive)
3549 (save-excursion
3550 (goto-char (point-max))
3551 (beginning-of-line 1)
3552 (while (not (bobp))
3553 (when (org-get-at-bol 'org-hd-marker)
3554 (org-agenda-entry-text-show-here))
3555 (beginning-of-line 0))))
3557 (defun org-agenda-entry-text-hide ()
3558 "Remove any shown entry context."
3559 (delq nil
3560 (mapcar (lambda (o)
3561 (if (eq (overlay-get o 'org-overlay-type)
3562 'agenda-entry-content)
3563 (progn (delete-overlay o) t)))
3564 (overlays-in (point-min) (point-max)))))
3566 (defun org-agenda-get-day-face (date)
3567 "Return the face DATE should be displayed with."
3568 (or (and (functionp org-agenda-day-face-function)
3569 (funcall org-agenda-day-face-function date))
3570 (cond ((org-agenda-todayp date)
3571 'org-agenda-date-today)
3572 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3573 'org-agenda-date-weekend)
3574 (t 'org-agenda-date))))
3576 ;;; Agenda timeline
3578 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3580 (defun org-timeline (&optional dotodo)
3581 "Show a time-sorted view of the entries in the current org file.
3582 Only entries with a time stamp of today or later will be listed. With
3583 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3584 under the current date.
3585 If the buffer contains an active region, only check the region for
3586 dates."
3587 (interactive "P")
3588 (let* ((dopast t)
3589 (org-agenda-show-log-scoped org-agenda-show-log)
3590 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3591 (current-buffer))))
3592 (date (calendar-current-date))
3593 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3594 (end (if (org-region-active-p) (region-end) (point-max)))
3595 (day-numbers (org-get-all-dates
3596 beg end 'no-ranges
3597 t org-agenda-show-log-scoped ; always include today
3598 org-timeline-show-empty-dates))
3599 (org-deadline-warning-days 0)
3600 (org-agenda-only-exact-dates t)
3601 (today (org-today))
3602 (past t)
3603 args
3604 s e rtn d emptyp)
3605 (setq org-agenda-redo-command
3606 (list 'progn
3607 (list 'org-switch-to-buffer-other-window (current-buffer))
3608 (list 'org-timeline (list 'quote dotodo))))
3609 (if (not dopast)
3610 ;; Remove past dates from the list of dates.
3611 (setq day-numbers (delq nil (mapcar (lambda(x)
3612 (if (>= x today) x nil))
3613 day-numbers))))
3614 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3615 (org-compile-prefix-format 'timeline)
3616 (org-set-sorting-strategy 'timeline)
3617 (if org-agenda-show-log-scoped (push :closed args))
3618 (push :timestamp args)
3619 (push :deadline args)
3620 (push :scheduled args)
3621 (push :sexp args)
3622 (if dotodo (push :todo args))
3623 (insert "Timeline of file " entry "\n")
3624 (add-text-properties (point-min) (point)
3625 (list 'face 'org-agenda-structure))
3626 (org-agenda-mark-header-line (point-min))
3627 (while (setq d (pop day-numbers))
3628 (if (and (listp d) (eq (car d) :omitted))
3629 (progn
3630 (setq s (point))
3631 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3632 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3633 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3634 (if (and (>= d today)
3635 dopast
3636 past)
3637 (progn
3638 (setq past nil)
3639 (insert (make-string 79 ?-) "\n")))
3640 (setq date (calendar-gregorian-from-absolute d))
3641 (setq s (point))
3642 (setq rtn (and (not emptyp)
3643 (apply 'org-agenda-get-day-entries entry
3644 date args)))
3645 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3646 (progn
3647 (insert
3648 (if (stringp org-agenda-format-date)
3649 (format-time-string org-agenda-format-date
3650 (org-time-from-absolute date))
3651 (funcall org-agenda-format-date date))
3652 "\n")
3653 (put-text-property s (1- (point)) 'face
3654 (org-agenda-get-day-face date))
3655 (put-text-property s (1- (point)) 'org-date-line t)
3656 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3657 (if (equal d today)
3658 (put-text-property s (1- (point)) 'org-today t))
3659 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3660 (put-text-property s (1- (point)) 'day d)))))
3661 (goto-char (point-min))
3662 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3663 (point-min)))
3664 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3665 (org-finalize-agenda)
3666 (setq buffer-read-only t)))
3668 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3669 "Return a list of all relevant day numbers from BEG to END buffer positions.
3670 If NO-RANGES is non-nil, include only the start and end dates of a range,
3671 not every single day in the range. If FORCE-TODAY is non-nil, make
3672 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3673 inactive time stamps (those in square brackets) are included.
3674 When EMPTY is non-nil, also include days without any entries."
3675 (let ((re (concat
3676 (if pre-re pre-re "")
3677 (if inactive org-ts-regexp-both org-ts-regexp)))
3678 dates dates1 date day day1 day2 ts1 ts2 pos)
3679 (if force-today
3680 (setq dates (list (org-today))))
3681 (save-excursion
3682 (goto-char beg)
3683 (while (re-search-forward re end t)
3684 (setq day (time-to-days (org-time-string-to-time
3685 (substring (match-string 1) 0 10)
3686 (current-buffer) (match-beginning 0))))
3687 (or (memq day dates) (push day dates)))
3688 (unless no-ranges
3689 (goto-char beg)
3690 (while (re-search-forward org-tr-regexp end t)
3691 (setq pos (match-beginning 0))
3692 (setq ts1 (substring (match-string 1) 0 10)
3693 ts2 (substring (match-string 2) 0 10)
3694 day1 (time-to-days (org-time-string-to-time
3695 ts1 (current-buffer) pos))
3696 day2 (time-to-days (org-time-string-to-time
3697 ts2 (current-buffer) pos)))
3698 (while (< (setq day1 (1+ day1)) day2)
3699 (or (memq day1 dates) (push day1 dates)))))
3700 (setq dates (sort dates '<))
3701 (when empty
3702 (while (setq day (pop dates))
3703 (setq day2 (car dates))
3704 (push day dates1)
3705 (when (and day2 empty)
3706 (if (or (eq empty t)
3707 (and (numberp empty) (<= (- day2 day) empty)))
3708 (while (< (setq day (1+ day)) day2)
3709 (push (list day) dates1))
3710 (push (cons :omitted (- day2 day)) dates1))))
3711 (setq dates (nreverse dates1)))
3712 dates)))
3714 ;;; Agenda Daily/Weekly
3716 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3717 "Start day for the agenda view.
3718 Custom commands can set this variable in the options section.")
3719 (defvar org-starting-day nil) ; local variable in the agenda buffer
3720 (defvar org-agenda-current-span nil
3721 "The current span used in the agenda view.") ; local variable in the agenda buffer
3722 (defvar org-arg-loc nil) ; local variable
3724 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3725 "List of types searched for when creating the daily/weekly agenda.
3726 This variable is a list of symbols that controls the types of
3727 items that appear in the daily/weekly agenda. Allowed symbols in this
3728 list are are
3730 :timestamp List items containing a date stamp or date range matching
3731 the selected date. This includes sexp entries in
3732 angular brackets.
3734 :sexp List entries resulting from plain diary-like sexps.
3736 :deadline List deadline due on that date. When the date is today,
3737 also list any deadlines past due, or due within
3738 `org-deadline-warning-days'. `:deadline' must appear before
3739 `:scheduled' if the setting of
3740 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3741 any effect.
3743 :scheduled List all items which are scheduled for the given date.
3744 The diary for *today* also contains items which were
3745 scheduled earlier and are not yet marked DONE.
3747 By default, all four types are turned on.
3749 Never set this variable globally using `setq', because then it
3750 will apply to all future agenda commands. Instead, bind it with
3751 `let' to scope it dynamically into the agenda-constructing
3752 command. A good way to set it is through options in
3753 `org-agenda-custom-commands'. For a more flexible (though
3754 somewhat less efficient) way of determining what is included in
3755 the daily/weekly agenda, see `org-agenda-skip-function'.")
3757 ;;;###autoload
3758 (defun org-agenda-list (&optional arg start-day span)
3759 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3760 The view will be for the current day or week, but from the overview buffer
3761 you will be able to go to other days/weeks.
3763 With a numeric prefix argument in an interactive call, the agenda will
3764 span ARG days. Lisp programs should instead specify SPAN to change
3765 the number of days. SPAN defaults to `org-agenda-span'.
3767 START-DAY defaults to TODAY, or to the most recent match for the weekday
3768 given in `org-agenda-start-on-weekday'."
3769 (interactive "P")
3770 (if (and (integerp arg) (> arg 0))
3771 (setq span arg arg nil))
3772 (org-prepare-agenda "Day/Week")
3773 (setq start-day (or start-day org-agenda-start-day))
3774 (if org-agenda-overriding-arguments
3775 (setq arg (car org-agenda-overriding-arguments)
3776 start-day (nth 1 org-agenda-overriding-arguments)
3777 span (nth 2 org-agenda-overriding-arguments)))
3778 (if (stringp start-day)
3779 ;; Convert to an absolute day number
3780 (setq start-day (time-to-days (org-read-date nil t start-day))))
3781 (setq org-agenda-last-arguments (list arg start-day span))
3782 (org-compile-prefix-format 'agenda)
3783 (org-set-sorting-strategy 'agenda)
3784 (let* ((span (org-agenda-ndays-to-span
3785 (or span org-agenda-ndays org-agenda-span)))
3786 (today (org-today))
3787 (sd (or start-day today))
3788 (ndays (org-agenda-span-to-ndays span sd))
3789 (org-agenda-start-on-weekday
3790 (if (eq ndays 7)
3791 org-agenda-start-on-weekday))
3792 (thefiles (org-agenda-files nil 'ifmode))
3793 (files thefiles)
3794 (start (if (or (null org-agenda-start-on-weekday)
3795 (< ndays 7))
3797 (let* ((nt (calendar-day-of-week
3798 (calendar-gregorian-from-absolute sd)))
3799 (n1 org-agenda-start-on-weekday)
3800 (d (- nt n1)))
3801 (- sd (+ (if (< d 0) 7 0) d)))))
3802 (day-numbers (list start))
3803 (day-cnt 0)
3804 (inhibit-redisplay (not debug-on-error))
3805 (org-agenda-show-log-scoped org-agenda-show-log)
3806 s e rtn rtnall file date d start-pos end-pos todayp
3807 clocktable-start clocktable-end filter)
3808 (setq org-agenda-redo-command
3809 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3810 (dotimes (n (1- ndays))
3811 (push (1+ (car day-numbers)) day-numbers))
3812 (setq day-numbers (nreverse day-numbers))
3813 (setq clocktable-start (car day-numbers)
3814 clocktable-end (1+ (or (org-last day-numbers) 0)))
3815 (org-set-local 'org-starting-day (car day-numbers))
3816 (org-set-local 'org-arg-loc arg)
3817 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3818 (unless org-agenda-compact-blocks
3819 (let* ((d1 (car day-numbers))
3820 (d2 (org-last day-numbers))
3821 (w1 (org-days-to-iso-week d1))
3822 (w2 (org-days-to-iso-week d2)))
3823 (setq s (point))
3824 (if org-agenda-overriding-header
3825 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3826 nil 'face 'org-agenda-structure) "\n")
3827 (insert (org-agenda-span-name span)
3828 "-agenda"
3829 (if (< (- d2 d1) 350)
3830 (if (= w1 w2)
3831 (format " (W%02d)" w1)
3832 (format " (W%02d-W%02d)" w1 w2))
3834 ":\n")))
3835 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3836 'org-date-line t))
3837 (org-agenda-mark-header-line s))
3838 (while (setq d (pop day-numbers))
3839 (setq date (calendar-gregorian-from-absolute d)
3840 s (point))
3841 (if (or (setq todayp (= d today))
3842 (and (not start-pos) (= d sd)))
3843 (setq start-pos (point))
3844 (if (and start-pos (not end-pos))
3845 (setq end-pos (point))))
3846 (setq files thefiles
3847 rtnall nil)
3848 (while (setq file (pop files))
3849 (catch 'nextfile
3850 (org-check-agenda-file file)
3851 (let ((org-agenda-entry-types org-agenda-entry-types))
3852 (unless org-agenda-include-deadlines
3853 (setq org-agenda-entry-types
3854 (delq :deadline org-agenda-entry-types)))
3855 (cond
3856 ((memq org-agenda-show-log-scoped '(only clockcheck))
3857 (setq rtn (org-agenda-get-day-entries
3858 file date :closed)))
3859 (org-agenda-show-log-scoped
3860 (setq rtn (apply 'org-agenda-get-day-entries
3861 file date
3862 (append '(:closed) org-agenda-entry-types))))
3864 (setq rtn (apply 'org-agenda-get-day-entries
3865 file date
3866 org-agenda-entry-types)))))
3867 (setq rtnall (append rtnall rtn)))) ;; all entries
3868 (if org-agenda-include-diary
3869 (let ((org-agenda-search-headline-for-time t))
3870 (require 'diary-lib)
3871 (setq rtn (org-get-entries-from-diary date))
3872 (setq rtnall (append rtnall rtn))))
3873 (if (or rtnall org-agenda-show-all-dates)
3874 (progn
3875 (setq day-cnt (1+ day-cnt))
3876 (insert
3877 (if (stringp org-agenda-format-date)
3878 (format-time-string org-agenda-format-date
3879 (org-time-from-absolute date))
3880 (funcall org-agenda-format-date date))
3881 "\n")
3882 (put-text-property s (1- (point)) 'face
3883 (org-agenda-get-day-face date))
3884 (put-text-property s (1- (point)) 'org-date-line t)
3885 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3886 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3887 (when todayp
3888 (put-text-property s (1- (point)) 'org-today t))
3889 (if rtnall (insert ;; all entries
3890 (org-finalize-agenda-entries
3891 (org-agenda-add-time-grid-maybe
3892 rtnall ndays todayp))
3893 "\n"))
3894 (put-text-property s (1- (point)) 'day d)
3895 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3896 (when (and org-agenda-clockreport-mode clocktable-start)
3897 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3898 ;; the above line is to ensure the restricted range!
3899 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3900 tbl)
3901 (setq p (org-plist-delete p :block))
3902 (setq p (plist-put p :tstart clocktable-start))
3903 (setq p (plist-put p :tend clocktable-end))
3904 (setq p (plist-put p :scope 'agenda))
3905 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3906 (setq filter (or org-agenda-tag-filter-while-redo
3907 (get 'org-agenda-tag-filter :preset-filter))))
3908 (setq p (plist-put p :tags (mapconcat (lambda (x)
3909 (if (string-match "[<>=]" x)
3912 filter ""))))
3913 (setq tbl (apply 'org-get-clocktable p))
3914 (insert tbl)))
3915 (goto-char (point-min))
3916 (or org-agenda-multi (org-fit-agenda-window))
3917 (unless (and (pos-visible-in-window-p (point-min))
3918 (pos-visible-in-window-p (point-max)))
3919 (goto-char (1- (point-max)))
3920 (recenter -1)
3921 (if (not (pos-visible-in-window-p (or start-pos 1)))
3922 (progn
3923 (goto-char (or start-pos 1))
3924 (recenter 1))))
3925 (goto-char (or start-pos 1))
3926 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3927 (if (eq org-agenda-show-log-scoped 'clockcheck)
3928 (org-agenda-show-clocking-issues))
3929 (org-finalize-agenda)
3930 (setq buffer-read-only t)
3931 (message "")))
3933 (defun org-agenda-ndays-to-span (n)
3934 "Return a span symbol for a span of N days, or N if none matches."
3935 (cond ((symbolp n) n)
3936 ((= n 1) 'day)
3937 ((= n 7) 'week)
3938 (t n)))
3940 (defun org-agenda-span-to-ndays (span start-day)
3941 "Return ndays from SPAN starting at START-DAY."
3942 (cond ((numberp span) span)
3943 ((eq span 'day) 1)
3944 ((eq span 'week) 7)
3945 ((eq span 'month)
3946 (let ((date (calendar-gregorian-from-absolute start-day)))
3947 (calendar-last-day-of-month (car date) (caddr date))))
3948 ((eq span 'year)
3949 (let ((date (calendar-gregorian-from-absolute start-day)))
3950 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3952 (defun org-agenda-span-name (span)
3953 "Return a SPAN name."
3954 (if (null span)
3956 (if (symbolp span)
3957 (capitalize (symbol-name span))
3958 (format "%d days" span))))
3960 ;;; Agenda word search
3962 (defvar org-agenda-search-history nil)
3963 (defvar org-todo-only nil)
3965 (defvar org-search-syntax-table nil
3966 "Special syntax table for org-mode search.
3967 In this table, we have single quotes not as word constituents, to
3968 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3970 (defun org-search-syntax-table ()
3971 (unless org-search-syntax-table
3972 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3973 (modify-syntax-entry ?' "." org-search-syntax-table)
3974 (modify-syntax-entry ?` "." org-search-syntax-table))
3975 org-search-syntax-table)
3977 (defvar org-agenda-last-search-view-search-was-boolean nil)
3979 ;;;###autoload
3980 (defun org-search-view (&optional todo-only string edit-at)
3981 "Show all entries that contain a phrase or words or regular expressions.
3983 With optional prefix argument TODO-ONLY, only consider entries that are
3984 TODO entries. The argument STRING can be used to pass a default search
3985 string into this function. If EDIT-AT is non-nil, it means that the
3986 user should get a chance to edit this string, with cursor at position
3987 EDIT-AT.
3989 The search string can be viewed either as a phrase that should be found as
3990 is, or it can be broken into a number of snippets, each of which must match
3991 in a Boolean way to select an entry. The default depends on the variable
3992 `org-agenda-search-view-always-boolean'.
3993 Even if this is turned off (the default) you can always switch to
3994 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3996 The default is a direct search of the whole phrase, where each space in
3997 the search string can expand to an arbitrary amount of whitespace,
3998 including newlines.
4000 If using a Boolean search, the search string is split on whitespace and
4001 each snippet is searched separately, with logical AND to select an entry.
4002 Words prefixed with a minus must *not* occur in the entry. Words without
4003 a prefix or prefixed with a plus must occur in the entry. Matching is
4004 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4005 match whole words, not parts of a word) if
4006 `org-agenda-search-view-force-full-words' is set (default is nil).
4008 Boolean search snippets enclosed by curly braces are interpreted as
4009 regular expressions that must or (when preceded with \"-\") must not
4010 match in the entry. Snippets enclosed into double quotes will be taken
4011 as a whole, to include whitespace.
4013 - If the search string starts with an asterisk, search only in headlines.
4014 - If (possibly after the leading star) the search string starts with an
4015 exclamation mark, this also means to look at TODO entries only, an effect
4016 that can also be achieved with a prefix argument.
4017 - If (possibly after star and exclamation mark) the search string starts
4018 with a colon, this will mean that the (non-regexp) snippets of the
4019 Boolean search must match as full words.
4021 This command searches the agenda files, and in addition the files listed
4022 in `org-agenda-text-search-extra-files'."
4023 (interactive "P")
4024 (org-prepare-agenda "SEARCH")
4025 (org-compile-prefix-format 'search)
4026 (org-set-sorting-strategy 'search)
4027 (let* ((props (list 'face nil
4028 'done-face 'org-agenda-done
4029 'org-not-done-regexp org-not-done-regexp
4030 'org-todo-regexp org-todo-regexp
4031 'org-complex-heading-regexp org-complex-heading-regexp
4032 'mouse-face 'highlight
4033 'help-echo (format "mouse-2 or RET jump to location")))
4034 (full-words org-agenda-search-view-force-full-words)
4035 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4036 regexp rtn rtnall files file pos
4037 marker category org-category-pos tags c neg re boolean
4038 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4039 (unless (and (not edit-at)
4040 (stringp string)
4041 (string-match "\\S-" string))
4042 (setq string (read-string
4043 (if org-agenda-search-view-always-boolean
4044 "[+-]Word/{Regexp} ...: "
4045 "Phrase, or [+-]Word/{Regexp} ...: ")
4046 (cond
4047 ((integerp edit-at) (cons string edit-at))
4048 (edit-at string))
4049 'org-agenda-search-history)))
4050 (org-set-local 'org-todo-only todo-only)
4051 (setq org-agenda-redo-command
4052 (list 'org-search-view (if todo-only t nil) string
4053 '(if current-prefix-arg 1 nil)))
4054 (setq org-agenda-query-string string)
4056 (if (equal (string-to-char string) ?*)
4057 (setq hdl-only t
4058 words (substring string 1))
4059 (setq words string))
4060 (when (equal (string-to-char words) ?!)
4061 (setq todo-only t
4062 words (substring words 1)))
4063 (when (equal (string-to-char words) ?:)
4064 (setq full-words t
4065 words (substring words 1)))
4066 (if (or org-agenda-search-view-always-boolean
4067 (member (string-to-char words) '(?- ?+ ?\{)))
4068 (setq boolean t))
4069 (setq words (org-split-string words))
4070 (let (www w)
4071 (while (setq w (pop words))
4072 (while (and (string-match "\\\\\\'" w) words)
4073 (setq w (concat (substring w 0 -1) " " (pop words))))
4074 (push w www))
4075 (setq words (nreverse www) www nil)
4076 (while (setq w (pop words))
4077 (when (and (string-match "\\`[-+]?{" w)
4078 (not (string-match "}\\'" w)))
4079 (while (and words (not (string-match "}\\'" (car words))))
4080 (setq w (concat w " " (pop words))))
4081 (setq w (concat w " " (pop words))))
4082 (push w www))
4083 (setq words (nreverse www)))
4084 (setq org-agenda-last-search-view-search-was-boolean boolean)
4085 (when boolean
4086 (let (wds w)
4087 (while (setq w (pop words))
4088 (if (or (equal (substring w 0 1) "\"")
4089 (and (> (length w) 1)
4090 (member (substring w 0 1) '("+" "-"))
4091 (equal (substring w 1 2) "\"")))
4092 (while (and words (not (equal (substring w -1) "\"")))
4093 (setq w (concat w " " (pop words)))))
4094 (and (string-match "\\`\\([-+]?\\)\"" w)
4095 (setq w (replace-match "\\1" nil nil w)))
4096 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4097 (push w wds))
4098 (setq words (nreverse wds))))
4099 (if boolean
4100 (mapc (lambda (w)
4101 (setq c (string-to-char w))
4102 (if (equal c ?-)
4103 (setq neg t w (substring w 1))
4104 (if (equal c ?+)
4105 (setq neg nil w (substring w 1))
4106 (setq neg nil)))
4107 (if (string-match "\\`{.*}\\'" w)
4108 (setq re (substring w 1 -1))
4109 (if full-words
4110 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4111 (setq re (regexp-quote (downcase w)))))
4112 (if neg (push re regexps-) (push re regexps+)))
4113 words)
4114 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4115 regexps+))
4116 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4117 (if (not regexps+)
4118 (setq regexp org-outline-regexp-bol)
4119 (setq regexp (pop regexps+))
4120 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4121 regexp))))
4122 (setq files (org-agenda-files nil 'ifmode))
4123 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4124 (pop org-agenda-text-search-extra-files)
4125 (setq files (org-add-archive-files files)))
4126 (setq files (append files org-agenda-text-search-extra-files)
4127 rtnall nil)
4128 (while (setq file (pop files))
4129 (setq ee nil)
4130 (catch 'nextfile
4131 (org-check-agenda-file file)
4132 (setq buffer (if (file-exists-p file)
4133 (org-get-agenda-file-buffer file)
4134 (error "No such file %s" file)))
4135 (if (not buffer)
4136 ;; If file does not exist, make sure an error message is sent
4137 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4138 file))))
4139 (with-current-buffer buffer
4140 (with-syntax-table (org-search-syntax-table)
4141 (unless (derived-mode-p 'org-mode)
4142 (error "Agenda file %s is not in `org-mode'" file))
4143 (let ((case-fold-search t))
4144 (save-excursion
4145 (save-restriction
4146 (if org-agenda-restrict
4147 (narrow-to-region org-agenda-restrict-begin
4148 org-agenda-restrict-end)
4149 (widen))
4150 (goto-char (point-min))
4151 (unless (or (org-at-heading-p)
4152 (outline-next-heading))
4153 (throw 'nextfile t))
4154 (goto-char (max (point-min) (1- (point))))
4155 (while (re-search-forward regexp nil t)
4156 (org-back-to-heading t)
4157 (skip-chars-forward "* ")
4158 (setq beg (point-at-bol)
4159 beg1 (point)
4160 end (progn (outline-next-heading) (point)))
4161 (catch :skip
4162 (goto-char beg)
4163 (org-agenda-skip)
4164 (setq str (buffer-substring-no-properties
4165 (point-at-bol)
4166 (if hdl-only (point-at-eol) end)))
4167 (mapc (lambda (wr) (when (string-match wr str)
4168 (goto-char (1- end))
4169 (throw :skip t)))
4170 regexps-)
4171 (mapc (lambda (wr) (unless (string-match wr str)
4172 (goto-char (1- end))
4173 (throw :skip t)))
4174 (if todo-only
4175 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4176 regexps+)
4177 regexps+))
4178 (goto-char beg)
4179 (setq marker (org-agenda-new-marker (point))
4180 category (org-get-category)
4181 org-category-pos (get-text-property (point) 'org-category-position)
4182 tags (org-get-tags-at (point))
4183 txt (org-agenda-format-item
4185 (buffer-substring-no-properties
4186 beg1 (point-at-eol))
4187 category tags))
4188 (org-add-props txt props
4189 'org-marker marker 'org-hd-marker marker
4190 'org-todo-regexp org-todo-regexp
4191 'org-complex-heading-regexp org-complex-heading-regexp
4192 'priority 1000 'org-category category
4193 'org-category-position org-category-pos
4194 'type "search")
4195 (push txt ee)
4196 (goto-char (1- end))))))))))
4197 (setq rtn (nreverse ee))
4198 (setq rtnall (append rtnall rtn)))
4199 (if org-agenda-overriding-header
4200 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4201 nil 'face 'org-agenda-structure) "\n")
4202 (insert "Search words: ")
4203 (add-text-properties (point-min) (1- (point))
4204 (list 'face 'org-agenda-structure))
4205 (setq pos (point))
4206 (insert string "\n")
4207 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4208 (setq pos (point))
4209 (unless org-agenda-multi
4210 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4211 (add-text-properties pos (1- (point))
4212 (list 'face 'org-agenda-structure))))
4213 (org-agenda-mark-header-line (point-min))
4214 (when rtnall
4215 (insert (org-finalize-agenda-entries rtnall) "\n"))
4216 (goto-char (point-min))
4217 (or org-agenda-multi (org-fit-agenda-window))
4218 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
4219 (org-finalize-agenda)
4220 (setq buffer-read-only t)))
4222 ;;; Agenda TODO list
4224 (defvar org-select-this-todo-keyword nil)
4225 (defvar org-last-arg nil)
4227 ;;;###autoload
4228 (defun org-todo-list (arg)
4229 "Show all (not done) TODO entries from all agenda file in a single list.
4230 The prefix arg can be used to select a specific TODO keyword and limit
4231 the list to these. When using \\[universal-argument], you will be prompted
4232 for a keyword. A numeric prefix directly selects the Nth keyword in
4233 `org-todo-keywords-1'."
4234 (interactive "P")
4235 (org-prepare-agenda "TODO")
4236 (org-compile-prefix-format 'todo)
4237 (org-set-sorting-strategy 'todo)
4238 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4239 (let* ((today (org-today))
4240 (date (calendar-gregorian-from-absolute today))
4241 (kwds org-todo-keywords-for-agenda)
4242 (completion-ignore-case t)
4243 (org-select-this-todo-keyword
4244 (if (stringp arg) arg
4245 (and arg (integerp arg) (> arg 0)
4246 (nth (1- arg) kwds))))
4247 rtn rtnall files file pos)
4248 (when (equal arg '(4))
4249 (setq org-select-this-todo-keyword
4250 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4251 (mapcar 'list kwds) nil nil)))
4252 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4253 (org-set-local 'org-last-arg arg)
4254 (setq org-agenda-redo-command
4255 '(org-todo-list (or current-prefix-arg org-last-arg)))
4256 (setq files (org-agenda-files nil 'ifmode)
4257 rtnall nil)
4258 (while (setq file (pop files))
4259 (catch 'nextfile
4260 (org-check-agenda-file file)
4261 (setq rtn (org-agenda-get-day-entries file date :todo))
4262 (setq rtnall (append rtnall rtn))))
4263 (if org-agenda-overriding-header
4264 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4265 nil 'face 'org-agenda-structure) "\n")
4266 (insert "Global list of TODO items of type: ")
4267 (add-text-properties (point-min) (1- (point))
4268 (list 'face 'org-agenda-structure
4269 'short-heading
4270 (concat "ToDo: "
4271 (or org-select-this-todo-keyword "ALL"))))
4272 (org-agenda-mark-header-line (point-min))
4273 (setq pos (point))
4274 (insert (or org-select-this-todo-keyword "ALL") "\n")
4275 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4276 (setq pos (point))
4277 (unless org-agenda-multi
4278 (insert "Available with `N r': (0)ALL")
4279 (let ((n 0) s)
4280 (mapc (lambda (x)
4281 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4282 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4283 (insert "\n "))
4284 (insert " " s))
4285 kwds))
4286 (insert "\n"))
4287 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4288 (org-agenda-mark-header-line (point-min))
4289 (when rtnall
4290 (insert (org-finalize-agenda-entries rtnall) "\n"))
4291 (goto-char (point-min))
4292 (or org-agenda-multi (org-fit-agenda-window))
4293 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4294 (org-finalize-agenda)
4295 (setq buffer-read-only t)))
4297 ;;; Agenda tags match
4299 ;;;###autoload
4300 (defun org-tags-view (&optional todo-only match)
4301 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4302 The prefix arg TODO-ONLY limits the search to TODO entries."
4303 (interactive "P")
4304 (let* ((org-tags-match-list-sublevels
4305 org-tags-match-list-sublevels)
4306 (completion-ignore-case t)
4307 rtn rtnall files file pos matcher
4308 buffer)
4309 (when (and (stringp match) (not (string-match "\\S-" match)))
4310 (setq match nil))
4311 (setq matcher (org-make-tags-matcher match)
4312 match (car matcher) matcher (cdr matcher))
4313 (org-prepare-agenda (concat "TAGS " match))
4314 (org-compile-prefix-format 'tags)
4315 (org-set-sorting-strategy 'tags)
4316 (setq org-agenda-query-string match)
4317 (setq org-agenda-redo-command
4318 (list 'org-tags-view (list 'quote todo-only)
4319 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4320 (setq files (org-agenda-files nil 'ifmode)
4321 rtnall nil)
4322 (while (setq file (pop files))
4323 (catch 'nextfile
4324 (org-check-agenda-file file)
4325 (setq buffer (if (file-exists-p file)
4326 (org-get-agenda-file-buffer file)
4327 (error "No such file %s" file)))
4328 (if (not buffer)
4329 ;; If file does not exist, error message to agenda
4330 (setq rtn (list
4331 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4332 rtnall (append rtnall rtn))
4333 (with-current-buffer buffer
4334 (unless (derived-mode-p 'org-mode)
4335 (error "Agenda file %s is not in `org-mode'" file))
4336 (save-excursion
4337 (save-restriction
4338 (if org-agenda-restrict
4339 (narrow-to-region org-agenda-restrict-begin
4340 org-agenda-restrict-end)
4341 (widen))
4342 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4343 (setq rtnall (append rtnall rtn))))))))
4344 (if org-agenda-overriding-header
4345 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4346 nil 'face 'org-agenda-structure) "\n")
4347 (insert "Headlines with TAGS match: ")
4348 (add-text-properties (point-min) (1- (point))
4349 (list 'face 'org-agenda-structure
4350 'short-heading
4351 (concat "Match: " match)))
4352 (setq pos (point))
4353 (insert match "\n")
4354 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4355 (setq pos (point))
4356 (unless org-agenda-multi
4357 (insert "Press `C-u r' to search again with new search string\n"))
4358 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4359 (org-agenda-mark-header-line (point-min))
4360 (when rtnall
4361 (insert (org-finalize-agenda-entries rtnall) "\n"))
4362 (goto-char (point-min))
4363 (or org-agenda-multi (org-fit-agenda-window))
4364 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4365 (org-finalize-agenda)
4366 (setq buffer-read-only t)))
4368 ;;; Agenda Finding stuck projects
4370 (defvar org-agenda-skip-regexp nil
4371 "Regular expression used in skipping subtrees for the agenda.
4372 This is basically a temporary global variable that can be set and then
4373 used by user-defined selections using `org-agenda-skip-function'.")
4375 (defvar org-agenda-overriding-header nil
4376 "When set during agenda, todo and tags searches it replaces the header.
4377 This variable should not be set directly, but custom commands can bind it
4378 in the options section.")
4380 (defun org-agenda-skip-entry-when-regexp-matches ()
4381 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4382 If yes, it returns the end position of this entry, causing agenda commands
4383 to skip the entry but continuing the search in the subtree. This is a
4384 function that can be put into `org-agenda-skip-function' for the duration
4385 of a command."
4386 (let ((end (save-excursion (org-end-of-subtree t)))
4387 skip)
4388 (save-excursion
4389 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4390 (and skip end)))
4392 (defun org-agenda-skip-subtree-when-regexp-matches ()
4393 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4394 If yes, it returns the end position of this tree, causing agenda commands
4395 to skip this subtree. This is a function that can be put into
4396 `org-agenda-skip-function' for the duration of a command."
4397 (let ((end (save-excursion (org-end-of-subtree t)))
4398 skip)
4399 (save-excursion
4400 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4401 (and skip end)))
4403 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4404 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4405 If yes, it returns the end position of the current entry (NOT the tree),
4406 causing agenda commands to skip the entry but continuing the search in
4407 the subtree. This is a function that can be put into
4408 `org-agenda-skip-function' for the duration of a command. An important
4409 use of this function is for the stuck project list."
4410 (let ((end (save-excursion (org-end-of-subtree t)))
4411 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4412 skip)
4413 (save-excursion
4414 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4415 (and skip entry-end)))
4417 (defun org-agenda-skip-entry-if (&rest conditions)
4418 "Skip entry if any of CONDITIONS is true.
4419 See `org-agenda-skip-if' for details."
4420 (org-agenda-skip-if nil conditions))
4422 (defun org-agenda-skip-subtree-if (&rest conditions)
4423 "Skip entry if any of CONDITIONS is true.
4424 See `org-agenda-skip-if' for details."
4425 (org-agenda-skip-if t conditions))
4427 (defun org-agenda-skip-if (subtree conditions)
4428 "Checks current entity for CONDITIONS.
4429 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4430 the entry (i.e. the text before the next heading) is checked.
4432 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4433 from different tests. Valid conditions are:
4435 scheduled Check if there is a scheduled cookie
4436 notscheduled Check if there is no scheduled cookie
4437 deadline Check if there is a deadline
4438 notdeadline Check if there is no deadline
4439 timestamp Check if there is a timestamp (also deadline or scheduled)
4440 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4441 regexp Check if regexp matches
4442 notregexp Check if regexp does not match.
4443 todo Check if TODO keyword matches
4444 nottodo Check if TODO keyword does not match
4446 The regexp is taken from the conditions list, it must come right after
4447 the `regexp' or `notregexp' element.
4449 `todo' and `nottodo' accept as an argument a list of todo
4450 keywords, which may include \"*\" to match any todo keyword.
4452 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4454 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4456 Instead of a list, a keyword class may be given. For example:
4458 (org-agenda-skip-entry-if 'nottodo 'done)
4460 would skip entries that haven't been marked with any of \"DONE\"
4461 keywords. Possible classes are: `todo', `done', `any'.
4463 If any of these conditions is met, this function returns the end point of
4464 the entity, causing the search to continue from there. This is a function
4465 that can be put into `org-agenda-skip-function' for the duration of a command."
4466 (let (beg end m)
4467 (org-back-to-heading t)
4468 (setq beg (point)
4469 end (if subtree
4470 (progn (org-end-of-subtree t) (point))
4471 (progn (outline-next-heading) (1- (point)))))
4472 (goto-char beg)
4473 (and
4475 (and (memq 'scheduled conditions)
4476 (re-search-forward org-scheduled-time-regexp end t))
4477 (and (memq 'notscheduled conditions)
4478 (not (re-search-forward org-scheduled-time-regexp end t)))
4479 (and (memq 'deadline conditions)
4480 (re-search-forward org-deadline-time-regexp end t))
4481 (and (memq 'notdeadline conditions)
4482 (not (re-search-forward org-deadline-time-regexp end t)))
4483 (and (memq 'timestamp conditions)
4484 (re-search-forward org-ts-regexp end t))
4485 (and (memq 'nottimestamp conditions)
4486 (not (re-search-forward org-ts-regexp end t)))
4487 (and (setq m (memq 'regexp conditions))
4488 (stringp (nth 1 m))
4489 (re-search-forward (nth 1 m) end t))
4490 (and (setq m (memq 'notregexp conditions))
4491 (stringp (nth 1 m))
4492 (not (re-search-forward (nth 1 m) end t)))
4493 (and (or
4494 (setq m (memq 'nottodo conditions))
4495 (setq m (memq 'todo-unblocked conditions))
4496 (setq m (memq 'nottodo-unblocked conditions))
4497 (setq m (memq 'todo conditions)))
4498 (org-agenda-skip-if-todo m end)))
4499 end)))
4501 (defun org-agenda-skip-if-todo (args end)
4502 "Helper function for `org-agenda-skip-if', do not use it directly.
4503 ARGS is a list with first element either `todo', `nottodo',
4504 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4505 a list of TODO keywords, or a state symbol `todo' or `done' or
4506 `any'."
4507 (let ((kw (car args))
4508 (arg (cadr args))
4509 todo-wds todo-re)
4510 (setq todo-wds
4511 (org-uniquify
4512 (cond
4513 ((listp arg) ;; list of keywords
4514 (if (member "*" arg)
4515 (mapcar 'substring-no-properties org-todo-keywords-1)
4516 arg))
4517 ((symbolp arg) ;; keyword class name
4518 (cond
4519 ((eq arg 'todo)
4520 (org-delete-all org-done-keywords
4521 (mapcar 'substring-no-properties
4522 org-todo-keywords-1)))
4523 ((eq arg 'done) org-done-keywords)
4524 ((eq arg 'any)
4525 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4526 (setq todo-re
4527 (concat "^\\*+[ \t]+\\<\\("
4528 (mapconcat 'identity todo-wds "\\|")
4529 "\\)\\>"))
4530 (cond
4531 ((eq kw 'todo) (re-search-forward todo-re end t))
4532 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4533 ((eq kw 'todo-unblocked)
4534 (catch 'unblocked
4535 (while (re-search-forward todo-re end t)
4536 (or (org-entry-blocked-p) (throw 'unblocked t)))
4537 nil))
4538 ((eq kw 'nottodo-unblocked)
4539 (catch 'unblocked
4540 (while (re-search-forward todo-re end t)
4541 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4545 ;;;###autoload
4546 (defun org-agenda-list-stuck-projects (&rest ignore)
4547 "Create agenda view for projects that are stuck.
4548 Stuck projects are project that have no next actions. For the definitions
4549 of what a project is and how to check if it stuck, customize the variable
4550 `org-stuck-projects'."
4551 (interactive)
4552 (let* ((org-agenda-skip-function
4553 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4554 ;; We could have used org-agenda-skip-if here.
4555 (org-agenda-overriding-header
4556 (or org-agenda-overriding-header "List of stuck projects: "))
4557 (matcher (nth 0 org-stuck-projects))
4558 (todo (nth 1 org-stuck-projects))
4559 (todo-wds (if (member "*" todo)
4560 (progn
4561 (org-prepare-agenda-buffers (org-agenda-files
4562 nil 'ifmode))
4563 (org-delete-all
4564 org-done-keywords-for-agenda
4565 (copy-sequence org-todo-keywords-for-agenda)))
4566 todo))
4567 (todo-re (concat "^\\*+[ \t]+\\("
4568 (mapconcat 'identity todo-wds "\\|")
4569 "\\)\\>"))
4570 (tags (nth 2 org-stuck-projects))
4571 (tags-re (if (member "*" tags)
4572 (concat org-outline-regexp-bol
4573 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4574 (if tags
4575 (concat org-outline-regexp-bol
4576 ".*:\\("
4577 (mapconcat 'identity tags "\\|")
4578 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4579 (gen-re (nth 3 org-stuck-projects))
4580 (re-list
4581 (delq nil
4582 (list
4583 (if todo todo-re)
4584 (if tags tags-re)
4585 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4586 gen-re)))))
4587 (setq org-agenda-skip-regexp
4588 (if re-list
4589 (mapconcat 'identity re-list "\\|")
4590 (error "No information how to identify unstuck projects")))
4591 (org-tags-view nil matcher)
4592 (with-current-buffer org-agenda-buffer-name
4593 (setq org-agenda-redo-command
4594 '(org-agenda-list-stuck-projects
4595 (or current-prefix-arg org-last-arg))))))
4597 ;;; Diary integration
4599 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4600 (defvar diary-list-entries-hook)
4601 (defvar diary-time-regexp)
4602 (defun org-get-entries-from-diary (date)
4603 "Get the (Emacs Calendar) diary entries for DATE."
4604 (require 'diary-lib)
4605 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4606 (diary-display-hook '(fancy-diary-display))
4607 (diary-display-function 'fancy-diary-display)
4608 (pop-up-frames nil)
4609 (diary-list-entries-hook
4610 (cons 'org-diary-default-entry diary-list-entries-hook))
4611 (diary-file-name-prefix-function nil) ; turn this feature off
4612 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4613 entries
4614 (org-disable-agenda-to-diary t))
4615 (save-excursion
4616 (save-window-excursion
4617 (funcall (if (fboundp 'diary-list-entries)
4618 'diary-list-entries 'list-diary-entries)
4619 date 1)))
4620 (if (not (get-buffer diary-fancy-buffer))
4621 (setq entries nil)
4622 (with-current-buffer diary-fancy-buffer
4623 (setq buffer-read-only nil)
4624 (if (zerop (buffer-size))
4625 ;; No entries
4626 (setq entries nil)
4627 ;; Omit the date and other unnecessary stuff
4628 (org-agenda-cleanup-fancy-diary)
4629 ;; Add prefix to each line and extend the text properties
4630 (if (zerop (buffer-size))
4631 (setq entries nil)
4632 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4633 (setq entries
4634 (with-temp-buffer
4635 (insert entries) (goto-char (point-min))
4636 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4637 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4638 (replace-match (concat "; " (match-string 1)))))
4639 (buffer-string)))))
4640 (set-buffer-modified-p nil)
4641 (kill-buffer diary-fancy-buffer)))
4642 (when entries
4643 (setq entries (org-split-string entries "\n"))
4644 (setq entries
4645 (mapcar
4646 (lambda (x)
4647 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4648 ;; Extend the text properties to the beginning of the line
4649 (org-add-props x (text-properties-at (1- (length x)) x)
4650 'type "diary" 'date date 'face 'org-agenda-diary))
4651 entries)))))
4653 (defvar org-agenda-cleanup-fancy-diary-hook nil
4654 "Hook run when the fancy diary buffer is cleaned up.")
4656 (defun org-agenda-cleanup-fancy-diary ()
4657 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4658 This gets rid of the date, the underline under the date, and
4659 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4660 date. It also removes lines that contain only whitespace."
4661 (goto-char (point-min))
4662 (if (looking-at ".*?:[ \t]*")
4663 (progn
4664 (replace-match "")
4665 (re-search-forward "\n=+$" nil t)
4666 (replace-match "")
4667 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4668 (re-search-forward "\n=+$" nil t)
4669 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4670 (goto-char (point-min))
4671 (while (re-search-forward "^ +\n" nil t)
4672 (replace-match ""))
4673 (goto-char (point-min))
4674 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4675 (replace-match ""))
4676 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4678 ;; Make sure entries from the diary have the right text properties.
4679 (eval-after-load "diary-lib"
4680 '(if (boundp 'diary-modify-entry-list-string-function)
4681 ;; We can rely on the hook, nothing to do
4683 ;; Hook not available, must use advice to make this work
4684 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4685 "Make the position visible."
4686 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4687 (stringp string)
4688 buffer-file-name)
4689 (setq string (org-modify-diary-entry-string string))))))
4691 (defun org-modify-diary-entry-string (string)
4692 "Add text properties to string, allowing org-mode to act on it."
4693 (org-add-props string nil
4694 'mouse-face 'highlight
4695 'help-echo (if buffer-file-name
4696 (format "mouse-2 or RET jump to diary file %s"
4697 (abbreviate-file-name buffer-file-name))
4699 'org-agenda-diary-link t
4700 'org-marker (org-agenda-new-marker (point-at-bol))))
4702 (defun org-diary-default-entry ()
4703 "Add a dummy entry to the diary.
4704 Needed to avoid empty dates which mess up holiday display."
4705 ;; Catch the error if dealing with the new add-to-diary-alist
4706 (when org-disable-agenda-to-diary
4707 (condition-case nil
4708 (org-add-to-diary-list original-date "Org-mode dummy" "")
4709 (error
4710 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4712 (defun org-add-to-diary-list (&rest args)
4713 (if (fboundp 'diary-add-to-list)
4714 (apply 'diary-add-to-list args)
4715 (apply 'add-to-diary-list args)))
4717 (defvar org-diary-last-run-time nil)
4719 ;;;###autoload
4720 (defun org-diary (&rest args)
4721 "Return diary information from org-files.
4722 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4723 It accesses org files and extracts information from those files to be
4724 listed in the diary. The function accepts arguments specifying what
4725 items should be listed. For a list of arguments allowed here, see the
4726 variable `org-agenda-entry-types'.
4728 The call in the diary file should look like this:
4730 &%%(org-diary) ~/path/to/some/orgfile.org
4732 Use a separate line for each org file to check. Or, if you omit the file name,
4733 all files listed in `org-agenda-files' will be checked automatically:
4735 &%%(org-diary)
4737 If you don't give any arguments (as in the example above), the default
4738 arguments (:deadline :scheduled :timestamp :sexp) are used.
4739 So the example above may also be written as
4741 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4743 The function expects the lisp variables `entry' and `date' to be provided
4744 by the caller, because this is how the calendar works. Don't use this
4745 function from a program - use `org-agenda-get-day-entries' instead."
4746 (when (> (- (org-float-time)
4747 org-agenda-last-marker-time)
4749 ;; I am not sure if this works with sticky agendas, because the marker
4750 ;; list is then no longer a global variable.
4751 (org-agenda-reset-markers))
4752 ;; Prevent `org-compile-prefix-format' to fail when there is no agenda
4753 (when (buffer-live-p org-agenda-buffer)
4754 (org-compile-prefix-format 'agenda))
4755 (org-set-sorting-strategy 'agenda)
4756 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4757 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4758 (list entry)
4759 (org-agenda-files t)))
4760 (time (org-float-time))
4761 file rtn results)
4762 (when (or (not org-diary-last-run-time)
4763 (> (- time
4764 org-diary-last-run-time)
4766 (org-prepare-agenda-buffers files))
4767 (setq org-diary-last-run-time time)
4768 ;; If this is called during org-agenda, don't return any entries to
4769 ;; the calendar. Org Agenda will list these entries itself.
4770 (if org-disable-agenda-to-diary (setq files nil))
4771 (while (setq file (pop files))
4772 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4773 (setq results (append results rtn)))
4774 (if results
4775 (concat (org-finalize-agenda-entries results) "\n"))))
4777 ;;; Agenda entry finders
4779 (defun org-agenda-get-day-entries (file date &rest args)
4780 "Does the work for `org-diary' and `org-agenda'.
4781 FILE is the path to a file to be checked for entries. DATE is date like
4782 the one returned by `calendar-current-date'. ARGS are symbols indicating
4783 which kind of entries should be extracted. For details about these, see
4784 the documentation of `org-diary'."
4785 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4786 (let* ((org-startup-folded nil)
4787 (org-startup-align-all-tables nil)
4788 (buffer (if (file-exists-p file)
4789 (org-get-agenda-file-buffer file)
4790 (error "No such file %s" file)))
4791 arg results rtn deadline-results)
4792 (if (not buffer)
4793 ;; If file does not exist, make sure an error message ends up in diary
4794 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4795 (with-current-buffer buffer
4796 (unless (derived-mode-p 'org-mode)
4797 (error "Agenda file %s is not in `org-mode'" file))
4798 (let ((case-fold-search nil))
4799 (save-excursion
4800 (save-restriction
4801 (if org-agenda-restrict
4802 (narrow-to-region org-agenda-restrict-begin
4803 org-agenda-restrict-end)
4804 (widen))
4805 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4806 (while (setq arg (pop args))
4807 (cond
4808 ((and (eq arg :todo)
4809 (equal date (calendar-gregorian-from-absolute
4810 (org-today))))
4811 (setq rtn (org-agenda-get-todos))
4812 (setq results (append results rtn)))
4813 ((eq arg :timestamp)
4814 (setq rtn (org-agenda-get-blocks))
4815 (setq results (append results rtn))
4816 (setq rtn (org-agenda-get-timestamps deadline-results))
4817 (setq results (append results rtn)))
4818 ((eq arg :sexp)
4819 (setq rtn (org-agenda-get-sexps))
4820 (setq results (append results rtn)))
4821 ((eq arg :scheduled)
4822 (setq rtn (org-agenda-get-scheduled deadline-results))
4823 (setq results (append results rtn)))
4824 ((eq arg :closed)
4825 (setq rtn (org-agenda-get-progress))
4826 (setq results (append results rtn)))
4827 ((eq arg :deadline)
4828 (setq rtn (org-agenda-get-deadlines))
4829 (setq deadline-results (copy-sequence rtn))
4830 (setq results (append results rtn))))))))
4831 results))))
4833 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4834 (defun org-agenda-get-todos ()
4835 "Return the TODO information for agenda display."
4836 (let* ((props (list 'face nil
4837 'done-face 'org-agenda-done
4838 'org-not-done-regexp org-not-done-regexp
4839 'org-todo-regexp org-todo-regexp
4840 'org-complex-heading-regexp org-complex-heading-regexp
4841 'mouse-face 'highlight
4842 'help-echo
4843 (format "mouse-2 or RET jump to org file %s"
4844 (abbreviate-file-name buffer-file-name))))
4845 (regexp (format org-heading-keyword-regexp-format
4846 (cond
4847 ((and org-select-this-todo-keyword
4848 (equal org-select-this-todo-keyword "*"))
4849 org-todo-regexp)
4850 (org-select-this-todo-keyword
4851 (concat "\\("
4852 (mapconcat 'identity
4853 (org-split-string
4854 org-select-this-todo-keyword
4855 "|")
4856 "\\|") "\\)"))
4857 (t org-not-done-regexp))))
4858 marker priority category org-category-pos tags todo-state
4859 ee txt beg end)
4860 (goto-char (point-min))
4861 (while (re-search-forward regexp nil t)
4862 (catch :skip
4863 (save-match-data
4864 (beginning-of-line)
4865 (org-agenda-skip)
4866 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4867 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4868 (goto-char (1+ beg))
4869 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4870 (throw :skip nil)))
4871 (goto-char (match-beginning 2))
4872 (setq marker (org-agenda-new-marker (match-beginning 0))
4873 category (org-get-category)
4874 org-category-pos (get-text-property (point) 'org-category-position)
4875 txt (org-trim
4876 (buffer-substring (match-beginning 2) (match-end 0)))
4877 tags (org-get-tags-at (point))
4878 txt (org-agenda-format-item "" txt category tags)
4879 priority (1+ (org-get-priority txt))
4880 todo-state (org-get-todo-state))
4881 (org-add-props txt props
4882 'org-marker marker 'org-hd-marker marker
4883 'priority priority 'org-category category
4884 'org-category-position org-category-pos
4885 'type "todo" 'todo-state todo-state)
4886 (push txt ee)
4887 (if org-agenda-todo-list-sublevels
4888 (goto-char (match-end 2))
4889 (org-end-of-subtree 'invisible))))
4890 (nreverse ee)))
4892 (defun org-agenda-todo-custom-ignore-p (time n)
4893 "Check whether timestamp is farther away then n number of days.
4894 This function is invoked if `org-agenda-todo-ignore-deadlines',
4895 `org-agenda-todo-ignore-scheduled' or
4896 `org-agenda-todo-ignore-timestamp' is set to an integer."
4897 (let ((days (org-days-to-time time)))
4898 (if (>= n 0)
4899 (>= days n)
4900 (<= days n))))
4902 ;;;###autoload
4903 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4904 (&optional end)
4905 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4906 (when (or org-agenda-todo-ignore-with-date
4907 org-agenda-todo-ignore-scheduled
4908 org-agenda-todo-ignore-deadlines
4909 org-agenda-todo-ignore-timestamp)
4910 (setq end (or end (save-excursion (outline-next-heading) (point))))
4911 (save-excursion
4912 (or (and org-agenda-todo-ignore-with-date
4913 (re-search-forward org-ts-regexp end t))
4914 (and org-agenda-todo-ignore-scheduled
4915 (re-search-forward org-scheduled-time-regexp end t)
4916 (cond
4917 ((eq org-agenda-todo-ignore-scheduled 'future)
4918 (> (org-days-to-time (match-string 1)) 0))
4919 ((eq org-agenda-todo-ignore-scheduled 'past)
4920 (<= (org-days-to-time (match-string 1)) 0))
4921 ((numberp org-agenda-todo-ignore-scheduled)
4922 (org-agenda-todo-custom-ignore-p
4923 (match-string 1) org-agenda-todo-ignore-scheduled))
4924 (t)))
4925 (and org-agenda-todo-ignore-deadlines
4926 (re-search-forward org-deadline-time-regexp end t)
4927 (cond
4928 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4929 ((eq org-agenda-todo-ignore-deadlines 'far)
4930 (not (org-deadline-close (match-string 1))))
4931 ((eq org-agenda-todo-ignore-deadlines 'future)
4932 (> (org-days-to-time (match-string 1)) 0))
4933 ((eq org-agenda-todo-ignore-deadlines 'past)
4934 (<= (org-days-to-time (match-string 1)) 0))
4935 ((numberp org-agenda-todo-ignore-deadlines)
4936 (org-agenda-todo-custom-ignore-p
4937 (match-string 1) org-agenda-todo-ignore-deadlines))
4938 (t (org-deadline-close (match-string 1)))))
4939 (and org-agenda-todo-ignore-timestamp
4940 (let ((buffer (current-buffer))
4941 (regexp
4942 (concat
4943 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4944 (start (point)))
4945 ;; Copy current buffer into a temporary one
4946 (with-temp-buffer
4947 (insert-buffer-substring buffer start end)
4948 (goto-char (point-min))
4949 ;; Delete SCHEDULED and DEADLINE items
4950 (while (re-search-forward regexp end t)
4951 (delete-region (match-beginning 0) (match-end 0)))
4952 (goto-char (point-min))
4953 ;; No search for timestamp left
4954 (when (re-search-forward org-ts-regexp nil t)
4955 (cond
4956 ((eq org-agenda-todo-ignore-timestamp 'future)
4957 (> (org-days-to-time (match-string 1)) 0))
4958 ((eq org-agenda-todo-ignore-timestamp 'past)
4959 (<= (org-days-to-time (match-string 1)) 0))
4960 ((numberp org-agenda-todo-ignore-timestamp)
4961 (org-agenda-todo-custom-ignore-p
4962 (match-string 1) org-agenda-todo-ignore-timestamp))
4963 (t))))))))))
4965 (defconst org-agenda-no-heading-message
4966 "No heading for this item in buffer or region.")
4968 (defun org-agenda-get-timestamps (&optional deadline-results)
4969 "Return the date stamp information for agenda display."
4970 (let* ((props (list 'face 'org-agenda-calendar-event
4971 'org-not-done-regexp org-not-done-regexp
4972 'org-todo-regexp org-todo-regexp
4973 'org-complex-heading-regexp org-complex-heading-regexp
4974 'mouse-face 'highlight
4975 'help-echo
4976 (format "mouse-2 or RET jump to org file %s"
4977 (abbreviate-file-name buffer-file-name))))
4978 (d1 (calendar-absolute-from-gregorian date))
4980 (deadline-position-alist
4981 (mapcar (lambda (a) (and (setq mm (get-text-property
4982 0 'org-hd-marker a))
4983 (cons (marker-position mm) a)))
4984 deadline-results))
4985 (remove-re
4986 (concat
4987 (regexp-quote
4988 (format-time-string
4989 "<%Y-%m-%d"
4990 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4991 ".*?>"))
4992 (regexp
4993 (concat
4994 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4995 (regexp-quote
4996 (substring
4997 (format-time-string
4998 (car org-time-stamp-formats)
4999 (apply 'encode-time ; DATE bound by calendar
5000 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5001 1 11))
5002 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5003 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5004 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5005 donep tmp priority category org-category-pos ee txt timestr tags
5006 b0 b3 e3 head todo-state end-of-match show-all)
5007 (goto-char (point-min))
5008 (while (setq end-of-match (re-search-forward regexp nil t))
5009 (setq b0 (match-beginning 0)
5010 b3 (match-beginning 3) e3 (match-end 3)
5011 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5012 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5013 (member todo-state
5014 org-agenda-repeating-timestamp-show-all)))
5015 (catch :skip
5016 (and (org-at-date-range-p) (throw :skip nil))
5017 (org-agenda-skip)
5018 (if (and (match-end 1)
5019 (not (= d1 (org-time-string-to-absolute
5020 (match-string 1) d1 nil show-all
5021 (current-buffer) b0))))
5022 (throw :skip nil))
5023 (if (and e3
5024 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5025 (throw :skip nil))
5026 (setq tmp (buffer-substring (max (point-min)
5027 (- b0 org-ds-keyword-length))
5029 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5030 inactivep (= (char-after b0) ?\[)
5031 deadlinep (string-match org-deadline-regexp tmp)
5032 scheduledp (string-match org-scheduled-regexp tmp)
5033 closedp (and org-agenda-include-inactive-timestamps
5034 (string-match org-closed-string tmp))
5035 clockp (and org-agenda-include-inactive-timestamps
5036 (or (string-match org-clock-string tmp)
5037 (string-match "]-+\\'" tmp)))
5038 donep (member todo-state org-done-keywords))
5039 (if (or scheduledp deadlinep closedp clockp
5040 (and donep org-agenda-skip-timestamp-if-done))
5041 (throw :skip t))
5042 (if (string-match ">" timestr)
5043 ;; substring should only run to end of time stamp
5044 (setq timestr (substring timestr 0 (match-end 0))))
5045 (setq marker (org-agenda-new-marker b0)
5046 category (org-get-category b0)
5047 org-category-pos (get-text-property b0 'org-category-position))
5048 (save-excursion
5049 (if (not (re-search-backward org-outline-regexp-bol nil t))
5050 (setq txt org-agenda-no-heading-message)
5051 (goto-char (match-beginning 0))
5052 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5053 (assoc (point) deadline-position-alist))
5054 (throw :skip nil))
5055 (setq hdmarker (org-agenda-new-marker)
5056 tags (org-get-tags-at))
5057 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5058 (setq head (or (match-string 1) ""))
5059 (setq txt (org-agenda-format-item
5060 (if inactivep org-agenda-inactive-leader nil)
5061 head category tags timestr
5062 remove-re)))
5063 (setq priority (org-get-priority txt))
5064 (org-add-props txt props
5065 'org-marker marker 'org-hd-marker hdmarker)
5066 (org-add-props txt nil 'priority priority
5067 'org-category category 'date date
5068 'org-category-position org-category-pos
5069 'todo-state todo-state
5070 'type "timestamp")
5071 (push txt ee))
5072 (if org-agenda-skip-additional-timestamps-same-entry
5073 (outline-next-heading)
5074 (goto-char end-of-match))))
5075 (nreverse ee)))
5077 (defun org-agenda-get-sexps ()
5078 "Return the sexp information for agenda display."
5079 (require 'diary-lib)
5080 (let* ((props (list 'face 'org-agenda-calendar-sexp
5081 'mouse-face 'highlight
5082 'help-echo
5083 (format "mouse-2 or RET jump to org file %s"
5084 (abbreviate-file-name buffer-file-name))))
5085 (regexp "^&?%%(")
5086 marker category extra org-category-pos ee txt tags entry
5087 result beg b sexp sexp-entry todo-state)
5088 (goto-char (point-min))
5089 (while (re-search-forward regexp nil t)
5090 (catch :skip
5091 (org-agenda-skip)
5092 (setq beg (match-beginning 0))
5093 (goto-char (1- (match-end 0)))
5094 (setq b (point))
5095 (forward-sexp 1)
5096 (setq sexp (buffer-substring b (point)))
5097 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5098 (org-trim (match-string 1))
5099 ""))
5100 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5101 (when result
5102 (setq marker (org-agenda-new-marker beg)
5103 category (org-get-category beg)
5104 org-category-pos (get-text-property beg 'org-category-position)
5105 todo-state (org-get-todo-state))
5107 (dolist (r (if (stringp result)
5108 (list result)
5109 result)) ;; we expect a list here
5110 (when (and org-agenda-diary-sexp-prefix
5111 (string-match org-agenda-diary-sexp-prefix r))
5112 (setq extra (match-string 0 r)
5113 r (replace-match "" nil nil r)))
5114 (if (string-match "\\S-" r)
5115 (setq txt r)
5116 (setq txt "SEXP entry returned empty string"))
5118 (setq txt (org-agenda-format-item
5119 extra txt category tags 'time))
5120 (org-add-props txt props 'org-marker marker)
5121 (org-add-props txt nil
5122 'org-category category 'date date 'todo-state todo-state
5123 'org-category-position org-category-pos
5124 'type "sexp")
5125 (push txt ee)))))
5126 (nreverse ee)))
5128 ;; Calendar sanity: define some functions that are independent of
5129 ;; `calendar-date-style'.
5130 ;; Normally I would like to use ISO format when calling the diary functions,
5131 ;; but to make sure we still have Emacs 22 compatibility we bind
5132 ;; also `european-calendar-style' and use european format
5133 (defun org-anniversary (year month day &optional mark)
5134 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5135 (org-no-warnings
5136 (let ((calendar-date-style 'european) (european-calendar-style t))
5137 (diary-anniversary day month year mark))))
5138 (defun org-cyclic (N year month day &optional mark)
5139 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5140 (org-no-warnings
5141 (let ((calendar-date-style 'european) (european-calendar-style t))
5142 (diary-cyclic N day month year mark))))
5143 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5144 "Like `diary-block', but with fixed (ISO) order of arguments."
5145 (org-no-warnings
5146 (let ((calendar-date-style 'european) (european-calendar-style t))
5147 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5148 (defun org-date (year month day &optional mark)
5149 "Like `diary-date', but with fixed (ISO) order of arguments."
5150 (org-no-warnings
5151 (let ((calendar-date-style 'european) (european-calendar-style t))
5152 (diary-date day month year mark))))
5153 (defalias 'org-float 'diary-float)
5155 ;; Define the` org-class' function
5156 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5157 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5158 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5159 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5160 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5161 `holidays', then any date that is known by the Emacs calendar to be a
5162 holiday will also be skipped."
5163 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5164 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5165 (d (calendar-absolute-from-gregorian date)))
5166 (and
5167 (<= date1 d)
5168 (<= d date2)
5169 (= (calendar-day-of-week date) dayname)
5170 (or (not skip-weeks)
5171 (progn
5172 (require 'cal-iso)
5173 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5174 (not (and (memq 'holidays skip-weeks)
5175 (calendar-check-holidays date)))
5176 entry)))
5178 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5179 "Like `org-class', but honor `calendar-date-style'.
5180 The order of the first 2 times 3 arguments depends on the variable
5181 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5182 So for American calendars, give this as MONTH DAY YEAR, for European as
5183 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5184 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5185 is any number of ISO weeks in the block period for which the item should
5186 be skipped.
5188 This function is here only for backward compatibility and it is deprecated,
5189 please use `org-class' instead."
5190 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5191 (date2 (org-order-calendar-date-args m2 d2 y2)))
5192 (org-class
5193 (nth 2 date1) (car date1) (nth 1 date1)
5194 (nth 2 date2) (car date2) (nth 1 date2)
5195 dayname skip-weeks)))
5196 (make-obsolete 'org-diary-class 'org-class "")
5198 (defvar org-agenda-show-log-scoped) ;; dynamically scope in ̀org-timeline' or`org-agenda-list'
5199 (defalias 'org-get-closed 'org-agenda-get-progress)
5200 (defun org-agenda-get-progress ()
5201 "Return the logged TODO entries for agenda display."
5202 (let* ((props (list 'mouse-face 'highlight
5203 'org-not-done-regexp org-not-done-regexp
5204 'org-todo-regexp org-todo-regexp
5205 'org-complex-heading-regexp org-complex-heading-regexp
5206 'help-echo
5207 (format "mouse-2 or RET jump to org file %s"
5208 (abbreviate-file-name buffer-file-name))))
5209 (items (if (consp org-agenda-show-log-scoped)
5210 org-agenda-show-log-scoped
5211 (if (eq org-agenda-show-log-scoped 'clockcheck)
5212 '(clock)
5213 org-agenda-log-mode-items)))
5214 (parts
5215 (delq nil
5216 (list
5217 (if (memq 'closed items) (concat "\\<" org-closed-string))
5218 (if (memq 'clock items) (concat "\\<" org-clock-string))
5219 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5220 (parts-re (if parts (mapconcat 'identity parts "\\|")
5221 (error "`org-agenda-log-mode-items' is empty")))
5222 (regexp (concat
5223 "\\(" parts-re "\\)"
5224 " *\\["
5225 (regexp-quote
5226 (substring
5227 (format-time-string
5228 (car org-time-stamp-formats)
5229 (apply 'encode-time ; DATE bound by calendar
5230 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5231 1 11))))
5232 (org-agenda-search-headline-for-time nil)
5233 marker hdmarker priority category org-category-pos tags closedp
5234 statep clockp state ee txt extra timestr rest clocked)
5235 (goto-char (point-min))
5236 (while (re-search-forward regexp nil t)
5237 (catch :skip
5238 (org-agenda-skip)
5239 (setq marker (org-agenda-new-marker (match-beginning 0))
5240 closedp (equal (match-string 1) org-closed-string)
5241 statep (equal (string-to-char (match-string 1)) ?-)
5242 clockp (not (or closedp statep))
5243 state (and statep (match-string 2))
5244 category (org-get-category (match-beginning 0))
5245 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
5246 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5247 (when (string-match "\\]" timestr)
5248 ;; substring should only run to end of time stamp
5249 (setq rest (substring timestr (match-end 0))
5250 timestr (substring timestr 0 (match-end 0)))
5251 (if (and (not closedp) (not statep)
5252 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5253 rest))
5254 (progn (setq timestr (concat (substring timestr 0 -1)
5255 "-" (match-string 1 rest) "]"))
5256 (setq clocked (match-string 2 rest)))
5257 (setq clocked "-")))
5258 (save-excursion
5259 (setq extra
5260 (cond
5261 ((not org-agenda-log-mode-add-notes) nil)
5262 (statep
5263 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5264 (match-string 1)))
5265 (clockp
5266 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5267 (match-string 1)))))
5268 (if (not (re-search-backward org-outline-regexp-bol nil t))
5269 (setq txt org-agenda-no-heading-message)
5270 (goto-char (match-beginning 0))
5271 (setq hdmarker (org-agenda-new-marker)
5272 tags (org-get-tags-at))
5273 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5274 (setq txt (match-string 1))
5275 (when extra
5276 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5277 (setq txt (concat (substring txt 0 (match-beginning 1))
5278 " - " extra " " (match-string 2 txt)))
5279 (setq txt (concat txt " - " extra))))
5280 (setq txt (org-agenda-format-item
5281 (cond
5282 (closedp "Closed: ")
5283 (statep (concat "State: (" state ")"))
5284 (t (concat "Clocked: (" clocked ")")))
5285 txt category tags timestr)))
5286 (setq priority 100000)
5287 (org-add-props txt props
5288 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5289 'priority priority 'org-category category
5290 'org-category-position org-category-pos
5291 'type "closed" 'date date
5292 'undone-face 'org-warning 'done-face 'org-agenda-done)
5293 (push txt ee))
5294 (goto-char (point-at-eol))))
5295 (nreverse ee)))
5297 (defun org-agenda-show-clocking-issues ()
5298 "Add overlays, showing issues with clocking.
5299 See also the user option `org-agenda-clock-consistency-checks'."
5300 (interactive)
5301 (let* ((pl org-agenda-clock-consistency-checks)
5302 (re (concat "^[ \t]*"
5303 org-clock-string
5304 "[ \t]+"
5305 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5306 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5307 (tlstart 0.)
5308 (tlend 0.)
5309 (maxtime (org-hh:mm-string-to-minutes
5310 (or (plist-get pl :max-duration) "24:00")))
5311 (mintime (org-hh:mm-string-to-minutes
5312 (or (plist-get pl :min-duration) 0)))
5313 (maxgap (org-hh:mm-string-to-minutes
5314 ;; default 30:00 means never complain
5315 (or (plist-get pl :max-gap) "30:00")))
5316 (gapok (mapcar 'org-hh:mm-string-to-minutes
5317 (plist-get pl :gap-ok-around)))
5318 (def-face (or (plist-get pl :default-face)
5319 '((:background "DarkRed") (:foreground "white"))))
5320 issue face m te ts dt ov)
5321 (goto-char (point-min))
5322 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5323 (setq issue nil face def-face)
5324 (catch 'next
5325 (setq m (org-get-at-bol 'org-marker)
5326 te nil ts nil)
5327 (unless (and m (markerp m))
5328 (setq issue "No valid clock line") (throw 'next t))
5329 (org-with-point-at m
5330 (save-excursion
5331 (goto-char (point-at-bol))
5332 (unless (looking-at re)
5333 (error "No valid Clock line")
5334 (throw 'next t))
5335 (unless (match-end 3)
5336 (setq issue "No end time"
5337 face (or (plist-get pl :no-end-time-face) face))
5338 (throw 'next t))
5339 (setq ts (match-string 1)
5340 te (match-string 3)
5341 ts (org-float-time
5342 (apply 'encode-time (org-parse-time-string ts)))
5343 te (org-float-time
5344 (apply 'encode-time (org-parse-time-string te)))
5345 dt (- te ts))))
5346 (cond
5347 ((> dt (* 60 maxtime))
5348 ;; a very long clocking chunk
5349 (setq issue (format "Clocking interval is very long: %s"
5350 (org-minutes-to-hh:mm-string
5351 (floor (/ (float dt) 60.))))
5352 face (or (plist-get pl :long-face) face)))
5353 ((< dt (* 60 mintime))
5354 ;; a very short clocking chunk
5355 (setq issue (format "Clocking interval is very short: %s"
5356 (org-minutes-to-hh:mm-string
5357 (floor (/ (float dt) 60.))))
5358 face (or (plist-get pl :short-face) face)))
5359 ((and (> tlend 0) (< ts tlend))
5360 ;; Two clock entries are overlapping
5361 (setq issue (format "Clocking overlap: %d minutes"
5362 (/ (- tlend ts) 60))
5363 face (or (plist-get pl :overlap-face) face)))
5364 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5365 ;; There is a gap, lets see if we need to report it
5366 (unless (org-agenda-check-clock-gap tlend ts gapok)
5367 (setq issue (format "Clocking gap: %d minutes"
5368 (/ (- ts tlend) 60))
5369 face (or (plist-get pl :gap-face) face))))
5370 (t nil)))
5371 (setq tlend (or te tlend) tlstart (or ts tlstart))
5372 (when issue
5373 ;; OK, there was some issue, add an overlay to show the issue
5374 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5375 (overlay-put ov 'before-string
5376 (concat
5377 (org-add-props
5378 (format "%-43s" (concat " " issue))
5380 'face face)
5381 "\n"))
5382 (overlay-put ov 'evaporate t)))))
5384 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5385 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5386 (catch 'exit
5387 (unless ok-list
5388 ;; there are no OK times for gaps...
5389 (throw 'exit nil))
5390 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5391 ;; This is more than 24 hours, so it is OK.
5392 ;; because we have at least one OK time, that must be in the
5393 ;; 24 hour interval.
5394 (throw 'exit t))
5395 ;; We have a shorter gap.
5396 ;; Now we have to get the minute of the day when these times are
5397 (let* ((t1dec (decode-time (seconds-to-time t1)))
5398 (t2dec (decode-time (seconds-to-time t2)))
5399 ;; compute the minute on the day
5400 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5401 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5402 (when (< min2 min1)
5403 ;; if min2 is smaller than min1, this means it is on the next day.
5404 ;; Wrap it to after midnight.
5405 (setq min2 (+ min2 1440)))
5406 ;; Now check if any of the OK times is in the gap
5407 (mapc (lambda (x)
5408 ;; Wrap the time to after midnight if necessary
5409 (if (< x min1) (setq x (+ x 1440)))
5410 ;; Check if in interval
5411 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5412 ok-list)
5413 ;; Nope, this gap is not OK
5414 nil)))
5416 (defun org-agenda-get-deadlines ()
5417 "Return the deadline information for agenda display."
5418 (let* ((props (list 'mouse-face 'highlight
5419 'org-not-done-regexp org-not-done-regexp
5420 'org-todo-regexp org-todo-regexp
5421 'org-complex-heading-regexp org-complex-heading-regexp
5422 'help-echo
5423 (format "mouse-2 or RET jump to org file %s"
5424 (abbreviate-file-name buffer-file-name))))
5425 (regexp org-deadline-time-regexp)
5426 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5427 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5428 d2 diff dfrac wdays pos pos1 category org-category-pos
5429 tags suppress-prewarning ee txt head face s todo-state
5430 show-all upcomingp donep timestr)
5431 (goto-char (point-min))
5432 (while (re-search-forward regexp nil t)
5433 (setq suppress-prewarning nil)
5434 (catch :skip
5435 (org-agenda-skip)
5436 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5437 (save-match-data
5438 (string-match org-scheduled-time-regexp
5439 (buffer-substring (point-at-bol)
5440 (point-at-eol)))))
5441 (setq suppress-prewarning
5442 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5443 org-agenda-skip-deadline-prewarning-if-scheduled
5444 0)))
5445 (setq s (match-string 1)
5446 txt nil
5447 pos (1- (match-beginning 1))
5448 todo-state (save-match-data (org-get-todo-state))
5449 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5450 (member todo-state
5451 org-agenda-repeating-timestamp-show-all))
5452 d2 (org-time-string-to-absolute
5453 (match-string 1) d1 'past show-all
5454 (current-buffer) pos)
5455 diff (- d2 d1)
5456 wdays (if suppress-prewarning
5457 (let ((org-deadline-warning-days suppress-prewarning))
5458 (org-get-wdays s))
5459 (org-get-wdays s))
5460 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5461 upcomingp (and todayp (> diff 0)))
5462 ;; When to show a deadline in the calendar:
5463 ;; If the expiration is within wdays warning time.
5464 ;; Past-due deadlines are only shown on the current date
5465 (if (and (or (and (<= diff wdays)
5466 (and todayp (not org-agenda-only-exact-dates)))
5467 (= diff 0)))
5468 (save-excursion
5469 ;; (setq todo-state (org-get-todo-state))
5470 (setq donep (member todo-state org-done-keywords))
5471 (if (and donep
5472 (or org-agenda-skip-deadline-if-done
5473 (not (= diff 0))))
5474 (setq txt nil)
5475 (setq category (org-get-category)
5476 org-category-pos (get-text-property (point) 'org-category-position))
5477 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5478 (setq txt org-agenda-no-heading-message)
5479 (goto-char (match-end 0))
5480 (setq pos1 (match-beginning 0))
5481 (setq tags (org-get-tags-at pos1))
5482 (setq head (buffer-substring-no-properties
5483 (point)
5484 (progn (skip-chars-forward "^\r\n")
5485 (point))))
5486 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5487 (setq timestr
5488 (concat (substring s (match-beginning 1)) " "))
5489 (setq timestr 'time))
5490 (setq txt (org-agenda-format-item
5491 (if (= diff 0)
5492 (car org-agenda-deadline-leaders)
5493 (if (functionp
5494 (nth 1 org-agenda-deadline-leaders))
5495 (funcall
5496 (nth 1 org-agenda-deadline-leaders)
5497 diff date)
5498 (format (nth 1 org-agenda-deadline-leaders)
5499 diff)))
5500 head category tags
5501 (if (not (= diff 0)) nil timestr)))))
5502 (when txt
5503 (setq face (org-agenda-deadline-face dfrac))
5504 (org-add-props txt props
5505 'org-marker (org-agenda-new-marker pos)
5506 'org-hd-marker (org-agenda-new-marker pos1)
5507 'priority (+ (- diff)
5508 (org-get-priority txt))
5509 'org-category category
5510 'org-category-position org-category-pos
5511 'todo-state todo-state
5512 'type (if upcomingp "upcoming-deadline" "deadline")
5513 'date (if upcomingp date d2)
5514 'face (if donep 'org-agenda-done face)
5515 'undone-face face 'done-face 'org-agenda-done)
5516 (push txt ee))))))
5517 (nreverse ee)))
5519 (defun org-agenda-deadline-face (fraction)
5520 "Return the face to displaying a deadline item.
5521 FRACTION is what fraction of the head-warning time has passed."
5522 (let ((faces org-agenda-deadline-faces) f)
5523 (catch 'exit
5524 (while (setq f (pop faces))
5525 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5527 (defun org-agenda-get-scheduled (&optional deadline-results)
5528 "Return the scheduled information for agenda display."
5529 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5530 'org-todo-regexp org-todo-regexp
5531 'org-complex-heading-regexp org-complex-heading-regexp
5532 'done-face 'org-agenda-done
5533 'mouse-face 'highlight
5534 'help-echo
5535 (format "mouse-2 or RET jump to org file %s"
5536 (abbreviate-file-name buffer-file-name))))
5537 (regexp org-scheduled-time-regexp)
5538 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5539 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5541 (deadline-position-alist
5542 (mapcar (lambda (a) (and (setq mm (get-text-property
5543 0 'org-hd-marker a))
5544 (cons (marker-position mm) a)))
5545 deadline-results))
5546 d2 diff pos pos1 category org-category-pos tags donep
5547 ee txt head pastschedp todo-state face timestr s habitp show-all)
5548 (goto-char (point-min))
5549 (while (re-search-forward regexp nil t)
5550 (catch :skip
5551 (org-agenda-skip)
5552 (setq s (match-string 1)
5553 txt nil
5554 pos (1- (match-beginning 1))
5555 todo-state (save-match-data (org-get-todo-state))
5556 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5557 (member todo-state
5558 org-agenda-repeating-timestamp-show-all))
5559 d2 (org-time-string-to-absolute
5560 (match-string 1) d1 'past show-all
5561 (current-buffer) pos)
5562 diff (- d2 d1))
5563 (setq pastschedp (and todayp (< diff 0)))
5564 ;; When to show a scheduled item in the calendar:
5565 ;; If it is on or past the date.
5566 (when (or (and (< diff 0)
5567 (< (abs diff) org-scheduled-past-days)
5568 (and todayp (not org-agenda-only-exact-dates)))
5569 (= diff 0))
5570 (save-excursion
5571 (setq donep (member todo-state org-done-keywords))
5572 (if (and donep
5573 (or org-agenda-skip-scheduled-if-done
5574 (not (= diff 0))
5575 (and (functionp 'org-is-habit-p)
5576 (org-is-habit-p))))
5577 (setq txt nil)
5578 (setq habitp (and (functionp 'org-is-habit-p)
5579 (org-is-habit-p)))
5580 (setq category (org-get-category)
5581 org-category-pos (get-text-property (point) 'org-category-position))
5582 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5583 (setq txt org-agenda-no-heading-message)
5584 (goto-char (match-end 0))
5585 (setq pos1 (match-beginning 0))
5586 (if habitp
5587 (if (or (not org-habit-show-habits)
5588 (and (not todayp)
5589 org-habit-show-habits-only-for-today))
5590 (throw :skip nil))
5591 (if (and
5592 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5593 (and org-agenda-skip-scheduled-if-deadline-is-shown
5594 pastschedp))
5595 (setq mm (assoc pos1 deadline-position-alist)))
5596 (throw :skip nil)))
5597 (setq tags (org-get-tags-at))
5598 (setq head (buffer-substring-no-properties
5599 (point)
5600 (progn (skip-chars-forward "^\r\n") (point))))
5601 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5602 (setq timestr
5603 (concat (substring s (match-beginning 1)) " "))
5604 (setq timestr 'time))
5605 (setq txt (org-agenda-format-item
5606 (if (= diff 0)
5607 (car org-agenda-scheduled-leaders)
5608 (format (nth 1 org-agenda-scheduled-leaders)
5609 (- 1 diff)))
5610 head category tags
5611 (if (not (= diff 0)) nil timestr)
5612 nil habitp))))
5613 (when txt
5614 (setq face
5615 (cond
5616 ((and (not habitp) pastschedp)
5617 'org-scheduled-previously)
5618 (todayp 'org-scheduled-today)
5619 (t 'org-scheduled))
5620 habitp (and habitp (org-habit-parse-todo)))
5621 (org-add-props txt props
5622 'undone-face face
5623 'face (if donep 'org-agenda-done face)
5624 'org-marker (org-agenda-new-marker pos)
5625 'org-hd-marker (org-agenda-new-marker pos1)
5626 'type (if pastschedp "past-scheduled" "scheduled")
5627 'date (if pastschedp d2 date)
5628 'priority (if habitp
5629 (org-habit-get-priority habitp)
5630 (+ 94 (- 5 diff) (org-get-priority txt)))
5631 'org-category category
5632 'org-category-position org-category-pos
5633 'org-habit-p habitp
5634 'todo-state todo-state)
5635 (push txt ee))))))
5636 (nreverse ee)))
5638 (defun org-agenda-get-blocks ()
5639 "Return the date-range information for agenda display."
5640 (let* ((props (list 'face nil
5641 'org-not-done-regexp org-not-done-regexp
5642 'org-todo-regexp org-todo-regexp
5643 'org-complex-heading-regexp org-complex-heading-regexp
5644 'mouse-face 'highlight
5645 'help-echo
5646 (format "mouse-2 or RET jump to org file %s"
5647 (abbreviate-file-name buffer-file-name))))
5648 (regexp org-tr-regexp)
5649 (d0 (calendar-absolute-from-gregorian date))
5650 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5651 todo-state tags pos head donep)
5652 (goto-char (point-min))
5653 (while (re-search-forward regexp nil t)
5654 (catch :skip
5655 (org-agenda-skip)
5656 (setq pos (point))
5657 (let ((start-time (match-string 1))
5658 (end-time (match-string 2)))
5659 (setq s1 (match-string 1)
5660 s2 (match-string 2)
5661 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5662 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5663 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5664 ;; Only allow days between the limits, because the normal
5665 ;; date stamps will catch the limits.
5666 (save-excursion
5667 (setq todo-state (org-get-todo-state))
5668 (setq donep (member todo-state org-done-keywords))
5669 (if (and donep org-agenda-skip-timestamp-if-done)
5670 (throw :skip t))
5671 (setq marker (org-agenda-new-marker (point)))
5672 (setq category (org-get-category)
5673 org-category-pos (get-text-property (point) 'org-category-position))
5674 (if (not (re-search-backward org-outline-regexp-bol nil t))
5675 (setq txt org-agenda-no-heading-message)
5676 (goto-char (match-beginning 0))
5677 (setq hdmarker (org-agenda-new-marker (point)))
5678 (setq tags (org-get-tags-at))
5679 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5680 (setq head (match-string 1))
5681 (let ((remove-re
5682 (if org-agenda-remove-timeranges-from-blocks
5683 (concat
5684 "<" (regexp-quote s1) ".*?>"
5685 "--"
5686 "<" (regexp-quote s2) ".*?>")
5687 nil)))
5688 (setq txt (org-agenda-format-item
5689 (format
5690 (nth (if (= d1 d2) 0 1)
5691 org-agenda-timerange-leaders)
5692 (1+ (- d0 d1)) (1+ (- d2 d1)))
5693 head category tags
5694 (cond ((and (= d1 d0) (= d2 d0))
5695 (concat "<" start-time ">--<" end-time ">"))
5696 ((= d1 d0)
5697 (concat "<" start-time ">"))
5698 ((= d2 d0)
5699 (concat "<" end-time ">"))
5700 (t nil))
5701 remove-re))))
5702 (org-add-props txt props
5703 'org-marker marker 'org-hd-marker hdmarker
5704 'type "block" 'date date
5705 'todo-state todo-state
5706 'priority (org-get-priority txt) 'org-category category
5707 'org-category-position org-category-pos)
5708 (push txt ee))))
5709 (goto-char pos)))
5710 ;; Sort the entries by expiration date.
5711 (nreverse ee)))
5713 ;;; Agenda presentation and sorting
5715 (defvar org-prefix-has-time nil
5716 "A flag, set by `org-compile-prefix-format'.
5717 The flag is set if the currently compiled format contains a `%t'.")
5718 (defvar org-prefix-has-tag nil
5719 "A flag, set by `org-compile-prefix-format'.
5720 The flag is set if the currently compiled format contains a `%T'.")
5721 (defvar org-prefix-has-effort nil
5722 "A flag, set by `org-compile-prefix-format'.
5723 The flag is set if the currently compiled format contains a `%e'.")
5724 (defvar org-prefix-category-length nil
5725 "Used by `org-compile-prefix-format' to remember the category field width.")
5726 (defvar org-prefix-category-max-length nil
5727 "Used by `org-compile-prefix-format' to remember the category field width.")
5729 (defun org-agenda-get-category-icon (category)
5730 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5731 (dolist (entry org-agenda-category-icon-alist)
5732 (when (org-string-match-p (car entry) category)
5733 (if (listp (cadr entry))
5734 (return (cadr entry))
5735 (return (apply 'create-image (cdr entry)))))))
5737 (defun org-agenda-format-item (extra txt &optional category tags dotime
5738 remove-re habitp)
5739 "Format TXT to be inserted into the agenda buffer.
5740 In particular, it adds the prefix and corresponding text properties. EXTRA
5741 must be a string and replaces the `%s' specifier in the prefix format.
5742 CATEGORY (string, symbol or nil) may be used to overrule the default
5743 category taken from local variable or file name. It will replace the `%c'
5744 specifier in the format. DOTIME, when non-nil, indicates that a
5745 time-of-day should be extracted from TXT for sorting of this entry, and for
5746 the `%t' specifier in the format. When DOTIME is a string, this string is
5747 searched for a time before TXT is. TAGS can be the tags of the headline.
5748 Any match of REMOVE-RE will be removed from TXT."
5749 ;; We keep the org-prefix-* variable values along with a compiled
5750 ;; formatter, so that multiple agendas existing at the same time, do
5751 ;; not step on each other toes.
5753 ;; It was inconvenient to make these variables buffer local in
5754 ;; Agenda buffers, because this function expects to be called with
5755 ;; the buffer where item comes from being current, and not agenda
5756 ;; buffer
5757 (let* ((bindings (car org-prefix-format-compiled))
5758 (formatter (cadr org-prefix-format-compiled)))
5759 (loop for (var value) in bindings
5760 do (set var value))
5761 (save-match-data
5762 ;; Diary entries sometimes have extra whitespace at the beginning
5763 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5765 ;; Fix the tags part in txt
5766 (setq txt (org-agenda-fix-displayed-tags
5767 txt tags
5768 org-agenda-show-inherited-tags
5769 org-agenda-hide-tags-regexp))
5770 (let* ((category (or category
5771 (if (stringp org-category)
5772 org-category
5773 (and org-category (symbol-name org-category)))
5774 (if buffer-file-name
5775 (file-name-sans-extension
5776 (file-name-nondirectory buffer-file-name))
5777 "")))
5778 (category-icon (org-agenda-get-category-icon category))
5779 (category-icon (if category-icon
5780 (propertize " " 'display category-icon)
5781 ""))
5782 ;; time, tag, effort are needed for the eval of the prefix format
5783 (tag (if tags (nth (1- (length tags)) tags) ""))
5784 time effort neffort
5785 (ts (if dotime (concat
5786 (if (stringp dotime) dotime "")
5787 (and org-agenda-search-headline-for-time txt))))
5788 (time-of-day (and dotime (org-get-time-of-day ts)))
5789 stamp plain s0 s1 s2 rtn srp l
5790 duration thecategory)
5791 (and (derived-mode-p 'org-mode) buffer-file-name
5792 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5793 (when (and dotime time-of-day)
5794 ;; Extract starting and ending time and move them to prefix
5795 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5796 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5797 (setq s0 (match-string 0 ts)
5798 srp (and stamp (match-end 3))
5799 s1 (match-string (if plain 1 2) ts)
5800 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5802 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5803 ;; them, we might want to remove them there to avoid duplication.
5804 ;; The user can turn this off with a variable.
5805 (if (and org-prefix-has-time
5806 org-agenda-remove-times-when-in-prefix (or stamp plain)
5807 (string-match (concat (regexp-quote s0) " *") txt)
5808 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5809 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5810 (= (match-beginning 0) 0)
5812 (setq txt (replace-match "" nil nil txt))))
5813 ;; Normalize the time(s) to 24 hour
5814 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5815 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5817 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5818 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5819 (setq s2
5820 (org-minutes-to-hh:mm-string
5821 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5823 ;; Compute the duration
5824 (when s2
5825 (setq duration (- (org-hh:mm-string-to-minutes s2)
5826 (org-hh:mm-string-to-minutes s1)))))
5828 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5829 txt)
5830 ;; Tags are in the string
5831 (if (or (eq org-agenda-remove-tags t)
5832 (and org-agenda-remove-tags
5833 org-prefix-has-tag))
5834 (setq txt (replace-match "" t t txt))
5835 (setq txt (replace-match
5836 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5837 (match-string 2 txt))
5838 t t txt))))
5839 (when (derived-mode-p 'org-mode)
5840 (setq effort
5841 (condition-case nil
5842 (org-get-effort
5843 (or (get-text-property 0 'org-hd-marker txt)
5844 (get-text-property 0 'org-marker txt)))
5845 (error nil)))
5846 (when effort
5847 (setq neffort (org-duration-string-to-minutes effort)
5848 effort (setq effort (concat "[" effort "]")))))
5849 ;; prevent erroring out with %e format when there is no effort
5850 (or effort (setq effort ""))
5852 (when remove-re
5853 (while (string-match remove-re txt)
5854 (setq txt (replace-match "" t t txt))))
5856 ;; Set org-heading property on `txt' to mark the start of the
5857 ;; heading.
5858 (add-text-properties 0 (length txt) '(org-heading t) txt)
5860 ;; Prepare the variables needed in the eval of the compiled format
5861 (setq time (cond (s2 (concat
5862 (org-agenda-time-of-day-to-ampm-maybe s1)
5863 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5864 (if org-agenda-timegrid-use-ampm " ")))
5865 (s1 (concat
5866 (org-agenda-time-of-day-to-ampm-maybe s1)
5867 (if org-agenda-timegrid-use-ampm
5868 "........ "
5869 "......")))
5870 (t ""))
5871 extra (or (and (not habitp) extra) "")
5872 category (if (symbolp category) (symbol-name category) category)
5873 thecategory (copy-sequence category))
5874 (if (string-match org-bracket-link-regexp category)
5875 (progn
5876 (setq l (if (match-end 3)
5877 (- (match-end 3) (match-beginning 3))
5878 (- (match-end 1) (match-beginning 1))))
5879 (when (< l (or org-prefix-category-length 0))
5880 (setq category (copy-sequence category))
5881 (org-add-props category nil
5882 'extra-space (make-string
5883 (- org-prefix-category-length l 1) ?\ ))))
5884 (if (and org-prefix-category-max-length
5885 (>= (length category) org-prefix-category-max-length))
5886 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5887 ;; Evaluate the compiled format
5888 (setq rtn (concat (eval formatter) txt))
5890 ;; And finally add the text properties
5891 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5892 (org-add-props rtn nil
5893 'org-category (if thecategory (downcase thecategory) category)
5894 'tags (mapcar 'org-downcase-keep-props tags)
5895 'org-highest-priority org-highest-priority
5896 'org-lowest-priority org-lowest-priority
5897 'time-of-day time-of-day
5898 'duration duration
5899 'effort effort
5900 'effort-minutes neffort
5901 'txt txt
5902 'time time
5903 'extra extra
5904 'format org-prefix-format-compiled
5905 'dotime dotime)))))
5907 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5908 "Remove tags string from TXT, and add a modified list of tags.
5909 The modified list may contain inherited tags, and tags matched by
5910 `org-agenda-hide-tags-regexp' will be removed."
5911 (when (or add-inherited hide-re)
5912 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5913 (setq txt (substring txt 0 (match-beginning 0))))
5914 (setq tags
5915 (delq nil
5916 (mapcar (lambda (tg)
5917 (if (or (and hide-re (string-match hide-re tg))
5918 (and (not add-inherited)
5919 (get-text-property 0 'inherited tg)))
5921 tg))
5922 tags)))
5923 (when tags
5924 (let ((have-i (get-text-property 0 'inherited (car tags)))
5926 (setq txt (concat txt " :"
5927 (mapconcat
5928 (lambda (x)
5929 (setq i (get-text-property 0 'inherited x))
5930 (if (and have-i (not i))
5931 (progn
5932 (setq have-i nil)
5933 (concat ":" x))
5935 tags ":")
5936 (if have-i "::" ":"))))))
5937 txt)
5939 (defun org-downcase-keep-props (s)
5940 (let ((props (text-properties-at 0 s)))
5941 (setq s (downcase s))
5942 (add-text-properties 0 (length s) props s)
5945 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5946 (defvar org-agenda-sorting-strategy-selected nil)
5948 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5949 (catch 'exit
5950 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5951 ((and todayp (member 'today (car org-agenda-time-grid))))
5952 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5953 ((member 'weekly (car org-agenda-time-grid)))
5954 (t (throw 'exit list)))
5955 (let* ((have (delq nil (mapcar
5956 (lambda (x) (get-text-property 1 'time-of-day x))
5957 list)))
5958 (string (nth 1 org-agenda-time-grid))
5959 (gridtimes (nth 2 org-agenda-time-grid))
5960 (req (car org-agenda-time-grid))
5961 (remove (member 'remove-match req))
5962 new time)
5963 (if (and (member 'require-timed req) (not have))
5964 ;; don't show empty grid
5965 (throw 'exit list))
5966 (while (setq time (pop gridtimes))
5967 (unless (and remove (member time have))
5968 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5969 (push (org-agenda-format-item
5970 nil string "" nil
5971 (concat (substring time 0 -2) ":" (substring time -2)))
5972 new)
5973 (put-text-property
5974 2 (length (car new)) 'face 'org-time-grid (car new))))
5975 (when (and todayp org-agenda-show-current-time-in-grid)
5976 (push (org-agenda-format-item
5978 org-agenda-current-time-string
5979 "" nil
5980 (format-time-string "%H:%M "))
5981 new)
5982 (put-text-property
5983 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5985 (if (member 'time-up org-agenda-sorting-strategy-selected)
5986 (append new list)
5987 (append list new)))))
5989 (defun org-compile-prefix-format (key)
5990 "Compile the prefix format into a Lisp form that can be evaluated.
5991 The resulting form and associated variable bindings is returned
5992 and stored in the variable `org-prefix-format-compiled'."
5993 (setq org-prefix-has-time nil org-prefix-has-tag nil
5994 org-prefix-category-length nil
5995 org-prefix-has-effort nil)
5996 (let ((s (cond
5997 ((stringp org-agenda-prefix-format)
5998 org-agenda-prefix-format)
5999 ((assq key org-agenda-prefix-format)
6000 (cdr (assq key org-agenda-prefix-format)))
6001 (t " %-12:c%?-12t% s")))
6002 (start 0)
6003 varform vars var e c f opt)
6004 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
6005 s start)
6006 (setq var (or (cdr (assoc (match-string 4 s)
6007 '(("c" . category) ("t" . time) ("s" . extra)
6008 ("i" . category-icon) ("T" . tag) ("e" . effort))))
6009 'eval)
6010 c (or (match-string 3 s) "")
6011 opt (match-beginning 1)
6012 start (1+ (match-beginning 0)))
6013 (if (equal var 'time) (setq org-prefix-has-time t))
6014 (if (equal var 'tag) (setq org-prefix-has-tag t))
6015 (if (equal var 'effort) (setq org-prefix-has-effort t))
6016 (setq f (concat "%" (match-string 2 s) "s"))
6017 (when (equal var 'category)
6018 (setq org-prefix-category-length
6019 (floor (abs (string-to-number (match-string 2 s)))))
6020 (setq org-prefix-category-max-length
6021 (let ((x (match-string 2 s)))
6022 (save-match-data
6023 (if (string-match "\\.[0-9]+" x)
6024 (string-to-number (substring (match-string 0 x) 1)))))))
6025 (if (eq var 'eval)
6026 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6027 (if opt
6028 (setq varform
6029 `(if (equal "" ,var)
6031 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6032 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6033 (setq s (replace-match "%s" t nil s))
6034 (push varform vars))
6035 (setq vars (nreverse vars))
6036 (with-current-buffer org-agenda-buffer
6037 (setq org-prefix-format-compiled
6038 (list
6039 `((org-prefix-has-time ,org-prefix-has-time)
6040 (org-prefix-has-tag ,org-prefix-has-tag)
6041 (org-prefix-category-length ,org-prefix-category-length)
6042 (org-prefix-has-effort ,org-prefix-has-effort))
6043 `(format ,s ,@vars))))))
6045 (defun org-set-sorting-strategy (key)
6046 (if (symbolp (car org-agenda-sorting-strategy))
6047 ;; the old format
6048 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6049 (setq org-agenda-sorting-strategy-selected
6050 (or (cdr (assq key org-agenda-sorting-strategy))
6051 (cdr (assq 'agenda org-agenda-sorting-strategy))
6052 '(time-up category-keep priority-down)))))
6054 (defun org-get-time-of-day (s &optional string mod24)
6055 "Check string S for a time of day.
6056 If found, return it as a military time number between 0 and 2400.
6057 If not found, return nil.
6058 The optional STRING argument forces conversion into a 5 character wide string
6059 HH:MM."
6060 (save-match-data
6061 (when
6062 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6063 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6064 (let* ((h (string-to-number (match-string 1 s)))
6065 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6066 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6067 (am-p (equal ampm "am"))
6068 (h1 (cond ((not ampm) h)
6069 ((= h 12) (if am-p 0 12))
6070 (t (+ h (if am-p 0 12)))))
6071 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6072 (mod h1 24) h1))
6073 (t0 (+ (* 100 h2) m))
6074 (t1 (concat (if (>= h1 24) "+" " ")
6075 (if (and org-agenda-time-leading-zero
6076 (< t0 1000)) "0" "")
6077 (if (< t0 100) "0" "")
6078 (if (< t0 10) "0" "")
6079 (int-to-string t0))))
6080 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6082 (defvar org-agenda-before-sorting-filter-function nil
6083 "Function to be applied to agenda items prior to sorting.
6084 Prior to sorting also means just before they are inserted into the agenda.
6086 To aid sorting, you may revisit the original entries and add more text
6087 properties which will later be used by the sorting functions.
6089 The function should take a string argument, an agenda line.
6090 It has access to the text properties in that line, which contain among
6091 other things, the property `org-hd-marker' that points to the entry
6092 where the line comes from. Note that not all lines going into the agenda
6093 have this property, only most.
6095 The function should return the modified string. It is probably best
6096 to ONLY change text properties.
6098 You can also use this function as a filter, by returning nil for lines
6099 you don't want to have in the agenda at all. For this application, you
6100 could bind the variable in the options section of a custom command.")
6102 (defun org-finalize-agenda-entries (list &optional nosort)
6103 "Sort and concatenate the agenda items."
6104 (setq list (mapcar 'org-agenda-highlight-todo list))
6105 (if nosort
6106 list
6107 (when org-agenda-before-sorting-filter-function
6108 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
6109 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
6111 (defun org-agenda-highlight-todo (x)
6112 (let ((org-done-keywords org-done-keywords-for-agenda)
6113 (case-fold-search nil)
6115 (if (eq x 'line)
6116 (save-excursion
6117 (beginning-of-line 1)
6118 (setq re (org-get-at-bol 'org-todo-regexp))
6119 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6120 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6121 (add-text-properties (match-beginning 0) (match-end 1)
6122 (list 'face (org-get-todo-face 1)))
6123 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6124 (delete-region (match-beginning 1) (1- (match-end 0)))
6125 (goto-char (match-beginning 1))
6126 (insert (format org-agenda-todo-keyword-format s)))))
6127 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6128 (setq re (get-text-property 0 'org-todo-regexp x))
6129 (when (and re
6130 ;; Test `pl' because if there's no heading content,
6131 ;; there's no point matching to highlight. Note
6132 ;; that if we didn't test `pl' first, and there
6133 ;; happened to be no keyword from `org-todo-regexp'
6134 ;; on this heading line, then the `equal' comparison
6135 ;; afterwards would spuriously succeed in the case
6136 ;; where `pl' is nil -- causing an args-out-of-range
6137 ;; error when we try to add text properties to text
6138 ;; that isn't there.
6140 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6141 x pl) pl))
6142 (add-text-properties
6143 (or (match-end 1) (match-end 0)) (match-end 0)
6144 (list 'face (org-get-todo-face (match-string 2 x)))
6146 (when (match-end 1)
6147 (setq x (concat (substring x 0 (match-end 1))
6148 (format org-agenda-todo-keyword-format
6149 (match-string 2 x))
6150 (org-add-props " " (text-properties-at 0 x))
6151 (substring x (match-end 3)))))))
6152 x)))
6154 (defsubst org-cmp-priority (a b)
6155 "Compare the priorities of string A and B."
6156 (let ((pa (or (get-text-property 1 'priority a) 0))
6157 (pb (or (get-text-property 1 'priority b) 0)))
6158 (cond ((> pa pb) +1)
6159 ((< pa pb) -1)
6160 (t nil))))
6162 (defsubst org-cmp-effort (a b)
6163 "Compare the effort values of string A and B."
6164 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6165 (ea (or (get-text-property 1 'effort-minutes a) def))
6166 (eb (or (get-text-property 1 'effort-minutes b) def)))
6167 (cond ((> ea eb) +1)
6168 ((< ea eb) -1)
6169 (t nil))))
6171 (defsubst org-cmp-category (a b)
6172 "Compare the string values of categories of strings A and B."
6173 (let ((ca (or (get-text-property 1 'org-category a) ""))
6174 (cb (or (get-text-property 1 'org-category b) "")))
6175 (cond ((string-lessp ca cb) -1)
6176 ((string-lessp cb ca) +1)
6177 (t nil))))
6179 (defsubst org-cmp-todo-state (a b)
6180 "Compare the todo states of strings A and B."
6181 (let* ((ma (or (get-text-property 1 'org-marker a)
6182 (get-text-property 1 'org-hd-marker a)))
6183 (mb (or (get-text-property 1 'org-marker b)
6184 (get-text-property 1 'org-hd-marker b)))
6185 (fa (and ma (marker-buffer ma)))
6186 (fb (and mb (marker-buffer mb)))
6187 (todo-kwds
6188 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6189 (and fb (with-current-buffer fb org-todo-keywords-1))))
6190 (ta (or (get-text-property 1 'todo-state a) ""))
6191 (tb (or (get-text-property 1 'todo-state b) ""))
6192 (la (- (length (member ta todo-kwds))))
6193 (lb (- (length (member tb todo-kwds))))
6194 (donepa (member ta org-done-keywords-for-agenda))
6195 (donepb (member tb org-done-keywords-for-agenda)))
6196 (cond ((and donepa (not donepb)) -1)
6197 ((and (not donepa) donepb) +1)
6198 ((< la lb) -1)
6199 ((< lb la) +1)
6200 (t nil))))
6202 (defsubst org-cmp-alpha (a b)
6203 "Compare the headlines, alphabetically."
6204 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6205 (plb (text-property-any 0 (length b) 'org-heading t b))
6206 (ta (and pla (substring a pla)))
6207 (tb (and plb (substring b plb))))
6208 (when pla
6209 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6210 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6211 (setq ta (substring ta (match-end 0))))
6212 (setq ta (downcase ta)))
6213 (when plb
6214 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6215 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6216 (setq tb (substring tb (match-end 0))))
6217 (setq tb (downcase tb)))
6218 (cond ((not ta) +1)
6219 ((not tb) -1)
6220 ((string-lessp ta tb) -1)
6221 ((string-lessp tb ta) +1)
6222 (t nil))))
6224 (defsubst org-cmp-tag (a b)
6225 "Compare the string values of the first tags of A and B."
6226 (let ((ta (car (last (get-text-property 1 'tags a))))
6227 (tb (car (last (get-text-property 1 'tags b)))))
6228 (cond ((not ta) +1)
6229 ((not tb) -1)
6230 ((string-lessp ta tb) -1)
6231 ((string-lessp tb ta) +1)
6232 (t nil))))
6234 (defsubst org-cmp-time (a b)
6235 "Compare the time-of-day values of strings A and B."
6236 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6237 (ta (or (get-text-property 1 'time-of-day a) def))
6238 (tb (or (get-text-property 1 'time-of-day b) def)))
6239 (cond ((< ta tb) -1)
6240 ((< tb ta) +1)
6241 (t nil))))
6243 (defsubst org-cmp-habit-p (a b)
6244 "Compare the todo states of strings A and B."
6245 (let ((ha (get-text-property 1 'org-habit-p a))
6246 (hb (get-text-property 1 'org-habit-p b)))
6247 (cond ((and ha (not hb)) -1)
6248 ((and (not ha) hb) +1)
6249 (t nil))))
6251 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
6253 (defun org-entries-lessp (a b)
6254 "Predicate for sorting agenda entries."
6255 ;; The following variables will be used when the form is evaluated.
6256 ;; So even though the compiler complains, keep them.
6257 (let* ((ss org-agenda-sorting-strategy-selected)
6258 (time-up (and (org-em 'time-up 'time-down ss)
6259 (org-cmp-time a b)))
6260 (time-down (if time-up (- time-up) nil))
6261 (priority-up (and (org-em 'priority-up 'priority-down ss)
6262 (org-cmp-priority a b)))
6263 (priority-down (if priority-up (- priority-up) nil))
6264 (effort-up (and (org-em 'effort-up 'effort-down ss)
6265 (org-cmp-effort a b)))
6266 (effort-down (if effort-up (- effort-up) nil))
6267 (category-up (and (or (org-em 'category-up 'category-down ss)
6268 (memq 'category-keep ss))
6269 (org-cmp-category a b)))
6270 (category-down (if category-up (- category-up) nil))
6271 (category-keep (if category-up +1 nil))
6272 (tag-up (and (org-em 'tag-up 'tag-down ss)
6273 (org-cmp-tag a b)))
6274 (tag-down (if tag-up (- tag-up) nil))
6275 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6276 (org-cmp-todo-state a b)))
6277 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6278 (habit-up (and (org-em 'habit-up 'habit-down ss)
6279 (org-cmp-habit-p a b)))
6280 (habit-down (if habit-up (- habit-up) nil))
6281 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6282 (org-cmp-alpha a b)))
6283 (alpha-down (if alpha-up (- alpha-up) nil))
6284 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6285 user-defined-up user-defined-down)
6286 (if (and need-user-cmp org-agenda-cmp-user-defined
6287 (functionp org-agenda-cmp-user-defined))
6288 (setq user-defined-up
6289 (funcall org-agenda-cmp-user-defined a b)
6290 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6291 (cdr (assoc
6292 (eval (cons 'or org-agenda-sorting-strategy-selected))
6293 '((-1 . t) (1 . nil) (nil . nil))))))
6295 ;;; Agenda restriction lock
6297 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6298 "Overlay to mark the headline to which agenda commands are restricted.")
6299 (overlay-put org-agenda-restriction-lock-overlay
6300 'face 'org-agenda-restriction-lock)
6301 (overlay-put org-agenda-restriction-lock-overlay
6302 'help-echo "Agendas are currently limited to this subtree.")
6303 (org-detach-overlay org-agenda-restriction-lock-overlay)
6305 (defun org-agenda-set-restriction-lock (&optional type)
6306 "Set restriction lock for agenda, to current subtree or file.
6307 Restriction will be the file if TYPE is `file', or if type is the
6308 universal prefix '(4), or if the cursor is before the first headline
6309 in the file. Otherwise, restriction will be to the current subtree."
6310 (interactive "P")
6311 (and (equal type '(4)) (setq type 'file))
6312 (setq type (cond
6313 (type type)
6314 ((org-at-heading-p) 'subtree)
6315 ((condition-case nil (org-back-to-heading t) (error nil))
6316 'subtree)
6317 (t 'file)))
6318 (if (eq type 'subtree)
6319 (progn
6320 (setq org-agenda-restrict t)
6321 (setq org-agenda-overriding-restriction 'subtree)
6322 (put 'org-agenda-files 'org-restrict
6323 (list (buffer-file-name (buffer-base-buffer))))
6324 (org-back-to-heading t)
6325 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6326 (move-marker org-agenda-restrict-begin (point))
6327 (move-marker org-agenda-restrict-end
6328 (save-excursion (org-end-of-subtree t)))
6329 (message "Locking agenda restriction to subtree"))
6330 (put 'org-agenda-files 'org-restrict
6331 (list (buffer-file-name (buffer-base-buffer))))
6332 (setq org-agenda-restrict nil)
6333 (setq org-agenda-overriding-restriction 'file)
6334 (move-marker org-agenda-restrict-begin nil)
6335 (move-marker org-agenda-restrict-end nil)
6336 (message "Locking agenda restriction to file"))
6337 (setq current-prefix-arg nil)
6338 (org-agenda-maybe-redo))
6340 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6341 "Remove the agenda restriction lock."
6342 (interactive "P")
6343 (org-detach-overlay org-agenda-restriction-lock-overlay)
6344 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6345 (setq org-agenda-overriding-restriction nil)
6346 (setq org-agenda-restrict nil)
6347 (put 'org-agenda-files 'org-restrict nil)
6348 (move-marker org-agenda-restrict-begin nil)
6349 (move-marker org-agenda-restrict-end nil)
6350 (setq current-prefix-arg nil)
6351 (message "Agenda restriction lock removed")
6352 (or noupdate (org-agenda-maybe-redo)))
6354 (defun org-agenda-maybe-redo ()
6355 "If there is any window showing the agenda view, update it."
6356 (let ((w (get-buffer-window org-agenda-buffer-name t))
6357 (w0 (selected-window)))
6358 (when w
6359 (select-window w)
6360 (org-agenda-redo)
6361 (select-window w0)
6362 (if org-agenda-overriding-restriction
6363 (message "Agenda view shifted to new %s restriction"
6364 org-agenda-overriding-restriction)
6365 (message "Agenda restriction lock removed")))))
6367 ;;; Agenda commands
6369 (defun org-agenda-check-type (error &rest types)
6370 "Check if agenda buffer is of allowed type.
6371 If ERROR is non-nil, throw an error, otherwise just return nil."
6372 (if (memq org-agenda-type types)
6374 (if error
6375 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6376 nil)))
6378 (defun org-agenda-Quit (&optional arg)
6379 "Exit agenda by removing the window or the buffer"
6380 (interactive)
6381 (if org-agenda-columns-active
6382 (org-columns-quit)
6383 (let ((buf (current-buffer)))
6384 (if (eq org-agenda-window-setup 'other-frame)
6385 (progn
6386 (org-agenda-reset-markers)
6387 (kill-buffer buf)
6388 (org-columns-remove-overlays)
6389 (setq org-agenda-archives-mode nil)
6390 (delete-frame))
6391 (and (not (eq org-agenda-window-setup 'current-window))
6392 (not (one-window-p))
6393 (delete-window))
6394 (org-agenda-reset-markers)
6395 (kill-buffer buf)
6396 (org-columns-remove-overlays)
6397 (setq org-agenda-archives-mode nil)))
6398 ;; Maybe restore the pre-agenda window configuration.
6399 (and org-agenda-restore-windows-after-quit
6400 (not (eq org-agenda-window-setup 'other-frame))
6401 org-pre-agenda-window-conf
6402 (set-window-configuration org-pre-agenda-window-conf))))
6404 (defun org-agenda-quit ()
6405 "Exit agenda by killing agenda buffer or burying it when
6406 `org-agenda-sticky' is non-NIL"
6407 (interactive)
6408 (if org-agenda-columns-active
6409 (org-columns-quit)
6410 (if org-agenda-sticky
6411 (let ((buf (current-buffer)))
6412 (if (eq org-agenda-window-setup 'other-frame)
6413 (progn
6414 (delete-frame))
6415 (and (not (eq org-agenda-window-setup 'current-window))
6416 (not (one-window-p))
6417 (delete-window)))
6418 (with-current-buffer buf
6419 (bury-buffer)
6420 ;; Maybe restore the pre-agenda window configuration.
6421 (and org-agenda-restore-windows-after-quit
6422 (not (eq org-agenda-window-setup 'other-frame))
6423 org-pre-agenda-window-conf
6424 (set-window-configuration org-pre-agenda-window-conf))))
6425 (org-agenda-Quit))))
6427 (defun org-agenda-exit ()
6428 "Exit agenda by removing the window or the buffer.
6429 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6430 Org-mode buffers visited directly by the user will not be touched."
6431 (interactive)
6432 (org-release-buffers org-agenda-new-buffers)
6433 (setq org-agenda-new-buffers nil)
6434 (org-agenda-Quit))
6436 (defun org-agenda-kill-all-agenda-buffers ()
6437 "Kill all buffers in `org-agena-mode'.
6438 This is used when toggling sticky agendas. You can also explicitly invoke it
6439 with `C-c a C-k'."
6440 (interactive)
6441 (let (blist)
6442 (dolist (buf (buffer-list))
6443 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
6444 (push buf blist)))
6445 (mapc 'kill-buffer blist)))
6447 (defun org-agenda-execute (arg)
6448 "Execute another agenda command, keeping same window.
6449 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6450 in the agenda."
6451 (interactive "P")
6452 (let ((org-agenda-window-setup 'current-window))
6453 (org-agenda arg)))
6455 (defun org-agenda-redo ()
6456 "Rebuild Agenda.
6457 When this is the global TODO list, a prefix argument will be interpreted."
6458 (interactive)
6459 (let* ((org-agenda-doing-sticky-redo org-agenda-sticky)
6460 (org-agenda-sticky nil)
6461 (org-agenda-buffer-name (or org-agenda-this-buffer-name
6462 org-agenda-buffer-name))
6463 (org-agenda-keep-modes t)
6464 (tag-filter org-agenda-tag-filter)
6465 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
6466 (top-cat-filter org-agenda-top-category-filter)
6467 (cat-filter org-agenda-category-filter)
6468 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6469 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
6470 (cols org-agenda-columns-active)
6471 (line (org-current-line))
6472 (window-line (- line (org-current-line (window-start))))
6473 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6474 (put 'org-agenda-tag-filter :preset-filter nil)
6475 (put 'org-agenda-category-filter :preset-filter nil)
6476 (and cols (org-columns-quit))
6477 (message "Rebuilding agenda buffer...")
6478 (org-let lprops '(eval org-agenda-redo-command))
6479 (setq org-agenda-undo-list nil
6480 org-agenda-pending-undo-list nil)
6481 (message "Rebuilding agenda buffer...done")
6482 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6483 (put 'org-agenda-category-filter :preset-filter cat-preset)
6484 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6485 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
6486 (and top-cat-filter (org-agenda-filter-top-category-apply top-cat-filter))
6487 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6488 (org-goto-line line)
6489 (recenter window-line)))
6491 (defvar org-global-tags-completion-table nil)
6492 (defvar org-agenda-filter-form nil)
6493 (defvar org-agenda-filtered-by-category nil)
6495 (defun org-agenda-filter-by-category (strip)
6496 "Keep only those lines in the agenda buffer that have a specific category.
6497 The category is that of the current line."
6498 (interactive "P")
6499 (if org-agenda-filtered-by-category
6500 (org-agenda-filter-show-all-cat)
6501 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6502 (if cat (org-agenda-filter-apply
6503 (list (concat (if strip "-" "+") cat)) 'category)
6504 (error "No category at point")))))
6506 (defun org-find-top-category (&optional pos)
6507 (save-excursion
6508 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
6509 (if pos (goto-char pos))
6510 ;; Skip up to the topmost parent
6511 (while (ignore-errors (outline-up-heading 1) t))
6512 (ignore-errors
6513 (nth 4 (org-heading-components))))))
6515 (defvar org-agenda-filtered-by-top-category nil)
6517 (defun org-agenda-filter-by-top-category (strip)
6518 "Keep only those lines in the agenda buffer that have a specific category.
6519 The category is that of the current line."
6520 (interactive "P")
6521 (if org-agenda-filtered-by-top-category
6522 (progn
6523 (setq org-agenda-filtered-by-top-category nil
6524 org-agenda-top-category-filter nil)
6525 (org-agenda-filter-show-all-cat))
6526 (let ((cat (org-find-top-category (org-get-at-bol 'org-hd-marker))))
6527 (if cat (org-agenda-filter-top-category-apply cat strip)
6528 (error "No top-level category at point")))))
6530 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6531 "Keep only those lines in the agenda buffer that have a specific tag.
6532 The tag is selected with its fast selection letter, as configured.
6533 With prefix argument STRIP, remove all lines that do have the tag.
6534 A lisp caller can specify CHAR. NARROW means that the new tag should be
6535 used to narrow the search - the interactive user can also press `-' or `+'
6536 to switch to narrowing."
6537 (interactive "P")
6538 (let* ((alist org-tag-alist-for-agenda)
6539 (tag-chars (mapconcat
6540 (lambda (x) (if (and (not (symbolp (car x)))
6541 (cdr x))
6542 (char-to-string (cdr x))
6543 ""))
6544 alist ""))
6545 (efforts (org-split-string
6546 (or (cdr (assoc (concat org-effort-property "_ALL")
6547 org-global-properties))
6548 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6549 "")))
6550 (effort-op org-agenda-filter-effort-default-operator)
6551 (effort-prompt "")
6552 (inhibit-read-only t)
6553 (current org-agenda-tag-filter)
6554 maybe-refresh a n tag)
6555 (unless char
6556 (message
6557 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6558 (if narrow "Narrow" "Filter") tag-chars
6559 (if org-agenda-auto-exclude-function "[RET], " ""))
6560 (setq char (read-char-exclusive)))
6561 (when (member char '(?+ ?-))
6562 ;; Narrowing down
6563 (cond ((equal char ?-) (setq strip t narrow t))
6564 ((equal char ?+) (setq strip nil narrow t)))
6565 (message
6566 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6567 (setq char (read-char-exclusive)))
6568 (when (member char '(?< ?> ?= ??))
6569 ;; An effort operator
6570 (setq effort-op (char-to-string char))
6571 (setq alist nil) ; to make sure it will be interpreted as effort.
6572 (unless (equal char ??)
6573 (loop for i from 0 to 9 do
6574 (setq effort-prompt
6575 (concat
6576 effort-prompt " ["
6577 (if (= i 9) "0" (int-to-string (1+ i)))
6578 "]" (nth i efforts))))
6579 (message "Effort%s: %s " effort-op effort-prompt)
6580 (setq char (read-char-exclusive))
6581 (when (or (< char ?0) (> char ?9))
6582 (error "Need 1-9,0 to select effort"))))
6583 (when (equal char ?\t)
6584 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6585 (org-set-local 'org-global-tags-completion-table
6586 (org-global-tags-completion-table)))
6587 (let ((completion-ignore-case t))
6588 (setq tag (org-icompleting-read
6589 "Tag: " org-global-tags-completion-table))))
6590 (cond
6591 ((equal char ?\r)
6592 (org-agenda-filter-show-all-tag)
6593 (when org-agenda-auto-exclude-function
6594 (setq org-agenda-tag-filter '())
6595 (dolist (tag (org-agenda-get-represented-tags))
6596 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6597 (if modifier
6598 (push modifier org-agenda-tag-filter))))
6599 (if (not (null org-agenda-tag-filter))
6600 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
6601 (setq maybe-refresh t))
6602 ((equal char ?/)
6603 (org-agenda-filter-show-all-tag)
6604 (when (get 'org-agenda-tag-filter :preset-filter)
6605 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
6606 (setq maybe-refresh t))
6607 ((equal char ?. )
6608 (setq org-agenda-tag-filter
6609 (mapcar (lambda(tag) (concat "+" tag))
6610 (org-get-at-bol 'tags)))
6611 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6612 (setq maybe-refresh t))
6613 ((or (equal char ?\ )
6614 (setq a (rassoc char alist))
6615 (and (>= char ?0) (<= char ?9)
6616 (setq n (if (= char ?0) 9 (- char ?0 1))
6617 tag (concat effort-op (nth n efforts))
6618 a (cons tag nil)))
6619 (and (= char ??)
6620 (setq tag "?eff")
6621 a (cons tag nil))
6622 (and tag (setq a (cons tag nil))))
6623 (org-agenda-filter-show-all-tag)
6624 (setq tag (car a))
6625 (setq org-agenda-tag-filter
6626 (cons (concat (if strip "-" "+") tag)
6627 (if narrow current nil)))
6628 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6629 (setq maybe-refresh t))
6630 (t (error "Invalid tag selection character %c" char)))
6631 (when (and maybe-refresh
6632 (eq org-agenda-clockreport-mode 'with-filter))
6633 (org-agenda-redo))))
6635 (defun org-agenda-get-represented-tags ()
6636 "Get a list of all tags currently represented in the agenda."
6637 (let (p tags)
6638 (save-excursion
6639 (goto-char (point-min))
6640 (while (setq p (next-single-property-change (point) 'tags))
6641 (goto-char p)
6642 (mapc (lambda (x) (add-to-list 'tags x))
6643 (get-text-property (point) 'tags))))
6644 tags))
6646 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6647 "Refine the current filter. See `org-agenda-filter-by-tag'."
6648 (interactive "P")
6649 (org-agenda-filter-by-tag strip char 'refine))
6651 (defun org-agenda-filter-make-matcher ()
6652 "Create the form that tests a line for agenda filter."
6653 (let (f f1)
6654 ;; first compute the tag-filter matcher
6655 (dolist (x (delete-dups
6656 (append (get 'org-agenda-tag-filter
6657 :preset-filter) org-agenda-tag-filter)))
6658 (if (member x '("-" "+"))
6659 (setq f1 (if (equal x "-") 'tags '(not tags)))
6660 (if (string-match "[<=>?]" x)
6661 (setq f1 (org-agenda-filter-effort-form x))
6662 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6663 (if (equal (string-to-char x) ?-)
6664 (setq f1 (list 'not f1))))
6665 (push f1 f))
6666 ;; then compute the category-filter matcher
6667 (dolist (x (delete-dups
6668 (append (get 'org-agenda-category-filter
6669 :preset-filter) org-agenda-category-filter)))
6670 (if (equal "-" (substring x 0 1))
6671 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
6672 (setq f1 (list 'equal (substring x 1) 'cat)))
6673 (push f1 f))
6674 (cons 'and (nreverse f))))
6676 (defun org-agenda-filter-effort-form (e)
6677 "Return the form to compare the effort of the current line with what E says.
6678 E looks like \"+<2:25\"."
6679 (let (op)
6680 (setq e (substring e 1))
6681 (setq op (string-to-char e) e (substring e 1))
6682 (setq op (cond ((equal op ?<) '<=)
6683 ((equal op ?>) '>=)
6684 ((equal op ??) op)
6685 (t '=)))
6686 (list 'org-agenda-compare-effort (list 'quote op)
6687 (org-duration-string-to-minutes e))))
6689 (defun org-agenda-compare-effort (op value)
6690 "Compare the effort of the current line with VALUE, using OP.
6691 If the line does not have an effort defined, return nil."
6692 (let ((eff (org-get-at-bol 'effort-minutes)))
6693 (if (equal op ??)
6694 (not eff)
6695 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6696 value))))
6698 (defun org-agenda-filter-apply (filter type)
6699 "Set FILTER as the new agenda filter and apply it."
6700 (let (tags cat)
6701 (if (eq type 'tag)
6702 (setq org-agenda-tag-filter filter)
6703 (setq org-agenda-category-filter filter))
6704 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
6705 (if (and (eq type 'category)
6706 (not (equal (substring (car filter) 0 1) "-")))
6707 ;; Only set `org-agenda-filtered-by-category' to t
6708 ;; when a unique category is used as the filter
6709 (setq org-agenda-filtered-by-category t))
6710 (org-agenda-set-mode-name)
6711 (save-excursion
6712 (goto-char (point-min))
6713 (while (not (eobp))
6714 (if (org-get-at-bol 'org-marker)
6715 (progn
6716 (setq tags (org-get-at-bol 'tags) ; used in eval
6717 cat (get-text-property (point) 'org-category))
6718 (if (not (eval org-agenda-filter-form))
6719 (org-agenda-filter-hide-line type))
6720 (beginning-of-line 2))
6721 (beginning-of-line 2))))
6722 (if (get-char-property (point) 'invisible)
6723 (ignore-errors (org-agenda-previous-line)))))
6725 (defun org-agenda-filter-top-category-apply (category &optional negative)
6726 "Set FILTER as the new agenda filter and apply it."
6727 (org-agenda-set-mode-name)
6728 (save-excursion
6729 (goto-char (point-min))
6730 (while (not (eobp))
6731 (let* ((pos (org-get-at-bol 'org-hd-marker))
6732 (topcat (and pos (org-find-top-category pos))))
6733 (if (and topcat (funcall (if negative 'identity 'not)
6734 (string= category topcat)))
6735 (org-agenda-filter-hide-line 'category)))
6736 (beginning-of-line 2)))
6737 (if (get-char-property (point) 'invisible)
6738 (org-agenda-previous-line))
6739 (setq org-agenda-top-category-filter category
6740 org-agenda-filtered-by-top-category t))
6742 (defun org-agenda-filter-hide-line (type)
6743 (let (ov)
6744 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6745 (point-at-eol)))
6746 (overlay-put ov 'invisible t)
6747 (overlay-put ov 'type type)
6748 (if (eq type 'tag)
6749 (push ov org-agenda-tag-filter-overlays)
6750 (push ov org-agenda-cat-filter-overlays))))
6752 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6753 (setq pos (or pos (point)))
6754 (save-excursion
6755 (dolist (ov (overlays-at pos))
6756 (when (and (overlay-get ov 'invisible)
6757 (eq (overlay-get ov 'type) 'tag))
6758 (goto-char pos)
6759 (if (< (overlay-start ov) (point-at-eol))
6760 (move-overlay ov (point-at-eol)
6761 (overlay-end ov)))))))
6763 (defun org-agenda-filter-show-all-tag nil
6764 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
6765 (setq org-agenda-tag-filter-overlays nil
6766 org-agenda-tag-filter nil
6767 org-agenda-filter-form nil)
6768 (org-agenda-set-mode-name))
6770 (defun org-agenda-filter-show-all-cat nil
6771 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
6772 (setq org-agenda-cat-filter-overlays nil
6773 org-agenda-filtered-by-category nil
6774 org-agenda-category-filter nil
6775 org-agenda-filter-form nil)
6776 (org-agenda-set-mode-name))
6778 (defun org-agenda-manipulate-query-add ()
6779 "Manipulate the query by adding a search term with positive selection.
6780 Positive selection means the term must be matched for selection of an entry."
6781 (interactive)
6782 (org-agenda-manipulate-query ?\[))
6783 (defun org-agenda-manipulate-query-subtract ()
6784 "Manipulate the query by adding a search term with negative selection.
6785 Negative selection means term must not be matched for selection of an entry."
6786 (interactive)
6787 (org-agenda-manipulate-query ?\]))
6788 (defun org-agenda-manipulate-query-add-re ()
6789 "Manipulate the query by adding a search regexp with positive selection.
6790 Positive selection means the regexp must match for selection of an entry."
6791 (interactive)
6792 (org-agenda-manipulate-query ?\{))
6793 (defun org-agenda-manipulate-query-subtract-re ()
6794 "Manipulate the query by adding a search regexp with negative selection.
6795 Negative selection means regexp must not match for selection of an entry."
6796 (interactive)
6797 (org-agenda-manipulate-query ?\}))
6798 (defun org-agenda-manipulate-query (char)
6799 (cond
6800 ((memq org-agenda-type '(timeline agenda))
6801 (let ((org-agenda-include-inactive-timestamps t))
6802 (org-agenda-redo))
6803 (message "Display now includes inactive timestamps as well"))
6804 ((eq org-agenda-type 'search)
6805 (org-add-to-string
6806 'org-agenda-query-string
6807 (if org-agenda-last-search-view-search-was-boolean
6808 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6809 (?\{ . " +{}") (?\} . " -{}"))))
6810 " "))
6811 (setq org-agenda-redo-command
6812 (list 'org-search-view
6813 org-todo-only
6814 org-agenda-query-string
6815 (+ (length org-agenda-query-string)
6816 (if (member char '(?\{ ?\})) 0 1))))
6817 (set-register org-agenda-query-register org-agenda-query-string)
6818 (org-agenda-redo))
6819 (t (error "Cannot manipulate query for %s-type agenda buffers"
6820 org-agenda-type))))
6822 (defun org-add-to-string (var string)
6823 (set var (concat (symbol-value var) string)))
6825 (defun org-agenda-goto-date (date)
6826 "Jump to DATE in agenda."
6827 (interactive (list (let ((org-read-date-prefer-future
6828 (eval org-agenda-jump-prefer-future)))
6829 (org-read-date))))
6830 (org-agenda-list nil date))
6832 (defun org-agenda-goto-today ()
6833 "Go to today."
6834 (interactive)
6835 (org-agenda-check-type t 'timeline 'agenda)
6836 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6837 (cond
6838 (tdpos (goto-char tdpos))
6839 ((eq org-agenda-type 'agenda)
6840 (let* ((sd (org-agenda-compute-starting-span
6841 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6842 (org-agenda-overriding-arguments org-agenda-last-arguments))
6843 (setf (nth 1 org-agenda-overriding-arguments) sd)
6844 (org-agenda-redo)
6845 (org-agenda-find-same-or-today-or-agenda)))
6846 (t (error "Cannot find today")))))
6848 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6849 (goto-char
6850 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6851 (text-property-any (point-min) (point-max) 'org-today t)
6852 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6853 (point-min))))
6855 (defun org-agenda-later (arg)
6856 "Go forward in time by thee current span.
6857 With prefix ARG, go forward that many times the current span."
6858 (interactive "p")
6859 (org-agenda-check-type t 'agenda)
6860 (let* ((span org-agenda-current-span)
6861 (sd org-starting-day)
6862 (greg (calendar-gregorian-from-absolute sd))
6863 (cnt (org-get-at-bol 'org-day-cnt))
6864 greg2)
6865 (cond
6866 ((eq span 'day)
6867 (setq sd (+ arg sd)))
6868 ((eq span 'week)
6869 (setq sd (+ (* 7 arg) sd)))
6870 ((eq span 'month)
6871 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6872 sd (calendar-absolute-from-gregorian greg2))
6873 (setcar greg2 (1+ (car greg2))))
6874 ((eq span 'year)
6875 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6876 sd (calendar-absolute-from-gregorian greg2))
6877 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6879 (setq sd (+ (* span arg) sd))))
6880 (let ((org-agenda-overriding-arguments
6881 (list (car org-agenda-last-arguments) sd span t)))
6882 (org-agenda-redo)
6883 (org-agenda-find-same-or-today-or-agenda cnt))))
6885 (defun org-agenda-earlier (arg)
6886 "Go backward in time by the current span.
6887 With prefix ARG, go backward that many times the current span."
6888 (interactive "p")
6889 (org-agenda-later (- arg)))
6891 (defun org-agenda-view-mode-dispatch ()
6892 "Call one of the view mode commands."
6893 (interactive)
6894 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6895 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6896 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6897 [E]ntryText")
6898 (let ((a (read-char-exclusive)))
6899 (case a
6900 (?\ (call-interactively 'org-agenda-reset-view))
6901 (?d (call-interactively 'org-agenda-day-view))
6902 (?w (call-interactively 'org-agenda-week-view))
6903 (?m (call-interactively 'org-agenda-month-view))
6904 (?y (call-interactively 'org-agenda-year-view))
6905 (?l (call-interactively 'org-agenda-log-mode))
6906 (?L (org-agenda-log-mode '(4)))
6907 (?c (org-agenda-log-mode 'clockcheck))
6908 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6909 (?a (call-interactively 'org-agenda-archives-mode))
6910 (?A (org-agenda-archives-mode 'files))
6911 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6912 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6913 (?G (call-interactively 'org-agenda-toggle-time-grid))
6914 (?D (call-interactively 'org-agenda-toggle-diary))
6915 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6916 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6917 (org-agenda-check-type t 'timeline 'agenda)
6918 (org-agenda-redo))
6919 (message "Display now includes inactive timestamps as well"))
6920 (?q (message "Abort"))
6921 (otherwise (error "Invalid key" )))))
6923 (defun org-agenda-reset-view ()
6924 "Switch to default view for agenda."
6925 (interactive)
6926 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6927 (defun org-agenda-day-view (&optional day-of-year)
6928 "Switch to daily view for agenda.
6929 With argument DAY-OF-YEAR, switch to that day of the year."
6930 (interactive "P")
6931 (org-agenda-change-time-span 'day day-of-year))
6932 (defun org-agenda-week-view (&optional iso-week)
6933 "Switch to daily view for agenda.
6934 With argument ISO-WEEK, switch to the corresponding ISO week.
6935 If ISO-WEEK has more then 2 digits, only the last two encode the
6936 week. Any digits before this encode a year. So 200712 means
6937 week 12 of year 2007. Years in the range 1938-2037 can also be
6938 written as 2-digit years."
6939 (interactive "P")
6940 (org-agenda-change-time-span 'week iso-week))
6941 (defun org-agenda-month-view (&optional month)
6942 "Switch to monthly view for agenda.
6943 With argument MONTH, switch to that month."
6944 (interactive "P")
6945 (org-agenda-change-time-span 'month month))
6946 (defun org-agenda-year-view (&optional year)
6947 "Switch to yearly view for agenda.
6948 With argument YEAR, switch to that year.
6949 If MONTH has more then 2 digits, only the last two encode the
6950 month. Any digits before this encode a year. So 200712 means
6951 December year 2007. Years in the range 1938-2037 can also be
6952 written as 2-digit years."
6953 (interactive "P")
6954 (when year
6955 (setq year (org-small-year-to-year year)))
6956 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6957 (org-agenda-change-time-span 'year year)
6958 (error "Abort")))
6960 (defun org-agenda-change-time-span (span &optional n)
6961 "Change the agenda view to SPAN.
6962 SPAN may be `day', `week', `month', `year'."
6963 (org-agenda-check-type t 'agenda)
6964 (if (and (not n) (equal org-agenda-current-span span))
6965 (error "Viewing span is already \"%s\"" span))
6966 (let* ((sd (or (org-get-at-bol 'day)
6967 org-starting-day))
6968 (sd (org-agenda-compute-starting-span sd span n))
6969 (org-agenda-overriding-arguments
6970 (or org-agenda-overriding-arguments
6971 (list (car org-agenda-last-arguments) sd span t))))
6972 (org-agenda-redo)
6973 (org-agenda-find-same-or-today-or-agenda))
6974 (org-agenda-set-mode-name)
6975 (message "Switched to %s view" span))
6977 (defun org-agenda-compute-starting-span (sd span &optional n)
6978 "Compute starting date for agenda.
6979 SPAN may be `day', `week', `month', `year'. The return value
6980 is a cons cell with the starting date and the number of days,
6981 so that the date SD will be in that range."
6982 (let* ((greg (calendar-gregorian-from-absolute sd))
6983 (dg (nth 1 greg))
6984 (mg (car greg))
6985 (yg (nth 2 greg)))
6986 (cond
6987 ((eq span 'day)
6988 (when n
6989 (setq sd (+ (calendar-absolute-from-gregorian
6990 (list mg 1 yg))
6991 n -1))))
6992 ((eq span 'week)
6993 (let* ((nt (calendar-day-of-week
6994 (calendar-gregorian-from-absolute sd)))
6995 (d (if org-agenda-start-on-weekday
6996 (- nt org-agenda-start-on-weekday)
6999 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7000 (when n
7001 (require 'cal-iso)
7002 (when (> n 99)
7003 (setq y1 (org-small-year-to-year (/ n 100))
7004 n (mod n 100)))
7005 (setq sd
7006 (calendar-absolute-from-iso
7007 (list n 1
7008 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7009 ((eq span 'month)
7010 (let (y1)
7011 (when (and n (> n 99))
7012 (setq y1 (org-small-year-to-year (/ n 100))
7013 n (mod n 100)))
7014 (setq sd (calendar-absolute-from-gregorian
7015 (list (or n mg) 1 (or y1 yg))))))
7016 ((eq span 'year)
7017 (setq sd (calendar-absolute-from-gregorian
7018 (list 1 1 (or n yg))))))
7019 sd))
7021 (defun org-agenda-next-date-line (&optional arg)
7022 "Jump to the next line indicating a date in agenda buffer."
7023 (interactive "p")
7024 (org-agenda-check-type t 'agenda 'timeline)
7025 (beginning-of-line 1)
7026 ;; This does not work if user makes date format that starts with a blank
7027 (if (looking-at "^\\S-") (forward-char 1))
7028 (if (not (re-search-forward "^\\S-" nil t arg))
7029 (progn
7030 (backward-char 1)
7031 (error "No next date after this line in this buffer")))
7032 (goto-char (match-beginning 0)))
7034 (defun org-agenda-previous-date-line (&optional arg)
7035 "Jump to the previous line indicating a date in agenda buffer."
7036 (interactive "p")
7037 (org-agenda-check-type t 'agenda 'timeline)
7038 (beginning-of-line 1)
7039 (if (not (re-search-backward "^\\S-" nil t arg))
7040 (error "No previous date before this line in this buffer")))
7042 ;; Initialize the highlight
7043 (defvar org-hl (make-overlay 1 1))
7044 (overlay-put org-hl 'face 'highlight)
7046 (defun org-highlight (begin end &optional buffer)
7047 "Highlight a region with overlay."
7048 (move-overlay org-hl begin end (or buffer (current-buffer))))
7050 (defun org-unhighlight ()
7051 "Detach overlay INDEX."
7052 (org-detach-overlay org-hl))
7054 ;; FIXME this is currently not used.
7055 (defun org-highlight-until-next-command (beg end &optional buffer)
7056 "Move the highlight overlay to BEG/END, remove it before the next command."
7057 (org-highlight beg end buffer)
7058 (add-hook 'pre-command-hook 'org-unhighlight-once))
7059 (defun org-unhighlight-once ()
7060 "Remove the highlight from its position, and this function from the hook."
7061 (remove-hook 'pre-command-hook 'org-unhighlight-once)
7062 (org-unhighlight))
7064 (defun org-agenda-follow-mode ()
7065 "Toggle follow mode in an agenda buffer."
7066 (interactive)
7067 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
7068 (org-agenda-set-mode-name)
7069 (org-agenda-do-context-action)
7070 (message "Follow mode is %s"
7071 (if org-agenda-follow-mode "on" "off")))
7073 (defun org-agenda-entry-text-mode (&optional arg)
7074 "Toggle entry text mode in an agenda buffer."
7075 (interactive "P")
7076 (setq org-agenda-entry-text-mode (or (integerp arg)
7077 (not org-agenda-entry-text-mode)))
7078 (org-agenda-entry-text-hide)
7079 (and org-agenda-entry-text-mode
7080 (let ((org-agenda-entry-text-maxlines
7081 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7082 (org-agenda-entry-text-show)))
7083 (org-agenda-set-mode-name)
7084 (message "Entry text mode is %s. Maximum number of lines is %d"
7085 (if org-agenda-entry-text-mode "on" "off")
7086 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7088 (defun org-agenda-clockreport-mode (&optional with-filter)
7089 "Toggle clocktable mode in an agenda buffer.
7090 With prefix arg WITH-FILTER, make the clocktable respect the current
7091 agenda filter."
7092 (interactive "P")
7093 (org-agenda-check-type t 'agenda)
7094 (if with-filter
7095 (setq org-agenda-clockreport-mode 'with-filter)
7096 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
7097 (org-agenda-set-mode-name)
7098 (org-agenda-redo)
7099 (message "Clocktable mode is %s"
7100 (if org-agenda-clockreport-mode "on" "off")))
7102 (defun org-agenda-log-mode (&optional special)
7103 "Toggle log mode in an agenda buffer.
7104 With argument SPECIAL, show all possible log items, not only the ones
7105 configured in `org-agenda-log-mode-items'.
7106 With a double `C-u' prefix arg, show *only* log items, nothing else."
7107 (interactive "P")
7108 (org-agenda-check-type t 'agenda 'timeline)
7109 (setq org-agenda-show-log
7110 (cond
7111 ((equal special '(16)) 'only)
7112 ((eq special 'clockcheck)
7113 (if (eq org-agenda-show-log 'clockcheck)
7114 nil 'clockcheck))
7115 (special '(closed clock state))
7116 (t (not org-agenda-show-log))))
7117 (org-agenda-set-mode-name)
7118 (org-agenda-redo)
7119 (message "Log mode is %s"
7120 (if org-agenda-show-log "on" "off")))
7122 (defun org-agenda-archives-mode (&optional with-files)
7123 "Toggle inclusion of items in trees marked with :ARCHIVE:.
7124 When called with a prefix argument, include all archive files as well."
7125 (interactive "P")
7126 (setq org-agenda-archives-mode
7127 (if with-files t (if org-agenda-archives-mode nil 'trees)))
7128 (org-agenda-set-mode-name)
7129 (org-agenda-redo)
7130 (message
7131 "%s"
7132 (cond
7133 ((eq org-agenda-archives-mode nil)
7134 "No archives are included")
7135 ((eq org-agenda-archives-mode 'trees)
7136 (format "Trees with :%s: tag are included" org-archive-tag))
7137 ((eq org-agenda-archives-mode t)
7138 (format "Trees with :%s: tag and all active archive files are included"
7139 org-archive-tag)))))
7141 (defun org-agenda-toggle-diary ()
7142 "Toggle diary inclusion in an agenda buffer."
7143 (interactive)
7144 (org-agenda-check-type t 'agenda)
7145 (setq org-agenda-include-diary (not org-agenda-include-diary))
7146 (org-agenda-redo)
7147 (org-agenda-set-mode-name)
7148 (message "Diary inclusion turned %s"
7149 (if org-agenda-include-diary "on" "off")))
7151 (defun org-agenda-toggle-deadlines ()
7152 "Toggle inclusion of entries with a deadline in an agenda buffer."
7153 (interactive)
7154 (org-agenda-check-type t 'agenda)
7155 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
7156 (org-agenda-redo)
7157 (org-agenda-set-mode-name)
7158 (message "Deadlines inclusion turned %s"
7159 (if org-agenda-include-deadlines "on" "off")))
7161 (defun org-agenda-toggle-time-grid ()
7162 "Toggle time grid in an agenda buffer."
7163 (interactive)
7164 (org-agenda-check-type t 'agenda)
7165 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
7166 (org-agenda-redo)
7167 (org-agenda-set-mode-name)
7168 (message "Time-grid turned %s"
7169 (if org-agenda-use-time-grid "on" "off")))
7171 (defun org-agenda-set-mode-name ()
7172 "Set the mode name to indicate all the small mode settings."
7173 (setq mode-name
7174 (list "Org-Agenda"
7175 (if (get 'org-agenda-files 'org-restrict) " []" "")
7177 '(:eval (org-agenda-span-name org-agenda-current-span))
7178 (if org-agenda-follow-mode " Follow" "")
7179 (if org-agenda-entry-text-mode " ETxt" "")
7180 (if org-agenda-include-diary " Diary" "")
7181 (if org-agenda-include-deadlines " Ddl" "")
7182 (if org-agenda-use-time-grid " Grid" "")
7183 (if (and (boundp 'org-habit-show-habits)
7184 org-habit-show-habits) " Habit" "")
7185 (cond
7186 ((consp org-agenda-show-log) " LogAll")
7187 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
7188 (org-agenda-show-log " Log")
7189 (t ""))
7190 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
7191 :preset-filter))
7192 '(:eval (org-propertize
7193 (concat " <"
7194 (mapconcat
7195 'identity
7196 (append
7197 (get 'org-agenda-category-filter :preset-filter)
7198 org-agenda-category-filter)
7200 ">")
7201 'face 'org-agenda-filter-category
7202 'help-echo "Category used in filtering"))
7204 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
7205 :preset-filter))
7206 '(:eval (org-propertize
7207 (concat " {"
7208 (mapconcat
7209 'identity
7210 (append
7211 (get 'org-agenda-tag-filter :preset-filter)
7212 org-agenda-tag-filter)
7214 "}")
7215 'face 'org-agenda-filter-tags
7216 'help-echo "Tags used in filtering"))
7218 (if org-agenda-archives-mode
7219 (if (eq org-agenda-archives-mode t)
7220 " Archives"
7221 (format " :%s:" org-archive-tag))
7223 (if org-agenda-clockreport-mode
7224 (if (eq org-agenda-clockreport-mode 'with-filter)
7225 " Clock{}" " Clock")
7226 "")))
7227 (force-mode-line-update))
7229 (defun org-agenda-post-command-hook ()
7230 (setq org-agenda-type
7231 (or (get-text-property (point) 'org-agenda-type)
7232 (get-text-property (max (point-min) (1- (point)))
7233 'org-agenda-type))))
7235 (defun org-agenda-next-line ()
7236 "Move cursor to the next line, and show if follow mode is active."
7237 (interactive)
7238 (call-interactively 'next-line)
7239 (org-agenda-do-context-action))
7241 (defun org-agenda-previous-line ()
7242 "Move cursor to the previous line, and show if follow-mode is active."
7243 (interactive)
7244 (call-interactively 'previous-line)
7245 (org-agenda-do-context-action))
7247 (defun org-agenda-do-context-action ()
7248 "Show outline path and, maybe, follow mode window."
7249 (let ((m (org-get-at-bol 'org-marker)))
7250 (when (and (markerp m) (marker-buffer m))
7251 (and org-agenda-follow-mode
7252 (if org-agenda-follow-indirect
7253 (org-agenda-tree-to-indirect-buffer)
7254 (org-agenda-show)))
7255 (and org-agenda-show-outline-path
7256 (org-with-point-at m (org-display-outline-path t))))))
7258 (defun org-agenda-show-priority ()
7259 "Show the priority of the current item.
7260 This priority is composed of the main priority given with the [#A] cookies,
7261 and by additional input from the age of a schedules or deadline entry."
7262 (interactive)
7263 (let* ((pri (org-get-at-bol 'priority)))
7264 (message "Priority is %d" (if pri pri -1000))))
7266 (defun org-agenda-show-tags ()
7267 "Show the tags applicable to the current item."
7268 (interactive)
7269 (let* ((tags (org-get-at-bol 'tags)))
7270 (if tags
7271 (message "Tags are :%s:"
7272 (org-no-properties (mapconcat 'identity tags ":")))
7273 (message "No tags associated with this line"))))
7275 (defun org-agenda-goto (&optional highlight)
7276 "Go to the Org-mode file which contains the item at point."
7277 (interactive)
7278 (let* ((marker (or (org-get-at-bol 'org-marker)
7279 (org-agenda-error)))
7280 (buffer (marker-buffer marker))
7281 (pos (marker-position marker)))
7282 (switch-to-buffer-other-window buffer)
7283 (widen)
7284 (push-mark)
7285 (goto-char pos)
7286 (when (derived-mode-p 'org-mode)
7287 (org-show-context 'agenda)
7288 (save-excursion
7289 (and (outline-next-heading)
7290 (org-flag-heading nil)))) ; show the next heading
7291 (when (outline-invisible-p)
7292 (show-entry)) ; display invisible text
7293 (recenter (/ (window-height) 2))
7294 (run-hooks 'org-agenda-after-show-hook)
7295 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
7297 (defvar org-agenda-after-show-hook nil
7298 "Normal hook run after an item has been shown from the agenda.
7299 Point is in the buffer where the item originated.")
7301 (defun org-agenda-kill ()
7302 "Kill the entry or subtree belonging to the current agenda entry."
7303 (interactive)
7304 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7305 (let* ((marker (or (org-get-at-bol 'org-marker)
7306 (org-agenda-error)))
7307 (buffer (marker-buffer marker))
7308 (pos (marker-position marker))
7309 (type (org-get-at-bol 'type))
7310 dbeg dend (n 0) conf)
7311 (org-with-remote-undo buffer
7312 (with-current-buffer buffer
7313 (save-excursion
7314 (goto-char pos)
7315 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
7316 (setq dbeg (progn (org-back-to-heading t) (point))
7317 dend (org-end-of-subtree t t))
7318 (setq dbeg (point-at-bol)
7319 dend (min (point-max) (1+ (point-at-eol)))))
7320 (goto-char dbeg)
7321 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
7322 (setq conf (or (eq t org-agenda-confirm-kill)
7323 (and (numberp org-agenda-confirm-kill)
7324 (> n org-agenda-confirm-kill))))
7325 (and conf
7326 (not (y-or-n-p
7327 (format "Delete entry with %d lines in buffer \"%s\"? "
7328 n (buffer-name buffer))))
7329 (error "Abort"))
7330 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
7331 (with-current-buffer buffer (delete-region dbeg dend))
7332 (message "Agenda item and source killed"))))
7334 (defvar org-archive-default-command)
7335 (defun org-agenda-archive-default ()
7336 "Archive the entry or subtree belonging to the current agenda entry."
7337 (interactive)
7338 (require 'org-archive)
7339 (org-agenda-archive-with org-archive-default-command))
7341 (defun org-agenda-archive-default-with-confirmation ()
7342 "Archive the entry or subtree belonging to the current agenda entry."
7343 (interactive)
7344 (require 'org-archive)
7345 (org-agenda-archive-with org-archive-default-command 'confirm))
7347 (defun org-agenda-archive ()
7348 "Archive the entry or subtree belonging to the current agenda entry."
7349 (interactive)
7350 (org-agenda-archive-with 'org-archive-subtree))
7352 (defun org-agenda-archive-to-archive-sibling ()
7353 "Move the entry to the archive sibling."
7354 (interactive)
7355 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7357 (defun org-agenda-archive-with (cmd &optional confirm)
7358 "Move the entry to the archive sibling."
7359 (interactive)
7360 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7361 (let* ((marker (or (org-get-at-bol 'org-marker)
7362 (org-agenda-error)))
7363 (buffer (marker-buffer marker))
7364 (pos (marker-position marker)))
7365 (org-with-remote-undo buffer
7366 (with-current-buffer buffer
7367 (if (derived-mode-p 'org-mode)
7368 (if (and confirm
7369 (not (y-or-n-p "Archive this subtree or entry? ")))
7370 (error "Abort")
7371 (save-excursion
7372 (goto-char pos)
7373 (org-remove-subtree-entries-from-agenda)
7374 (org-back-to-heading t)
7375 (funcall cmd)))
7376 (error "Archiving works only in Org-mode files"))))))
7378 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7379 "Remove all lines in the agenda that correspond to a given subtree.
7380 The subtree is the one in buffer BUF, starting at BEG and ending at END.
7381 If this information is not given, the function uses the tree at point."
7382 (let ((buf (or buf (current-buffer))) m p)
7383 (save-excursion
7384 (unless (and beg end)
7385 (org-back-to-heading t)
7386 (setq beg (point))
7387 (org-end-of-subtree t)
7388 (setq end (point)))
7389 (set-buffer (get-buffer org-agenda-buffer-name))
7390 (save-excursion
7391 (goto-char (point-max))
7392 (beginning-of-line 1)
7393 (while (not (bobp))
7394 (when (and (setq m (org-get-at-bol 'org-marker))
7395 (equal buf (marker-buffer m))
7396 (setq p (marker-position m))
7397 (>= p beg)
7398 (< p end))
7399 (let ((inhibit-read-only t))
7400 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7401 (beginning-of-line 0))))))
7403 (defun org-agenda-refile (&optional goto rfloc no-update)
7404 "Refile the item at point."
7405 (interactive "P")
7406 (if (equal goto '(16))
7407 (org-refile-goto-last-stored)
7408 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7409 (org-agenda-error)))
7410 (buffer (marker-buffer marker))
7411 (pos (marker-position marker))
7412 (rfloc (or rfloc
7413 (org-refile-get-location
7414 (if goto "Goto" "Refile to") buffer
7415 org-refile-allow-creating-parent-nodes))))
7416 (with-current-buffer buffer
7417 (save-excursion
7418 (save-restriction
7419 (widen)
7420 (goto-char marker)
7421 (org-remove-subtree-entries-from-agenda)
7422 (org-refile goto buffer rfloc)))))
7423 (unless no-update (org-agenda-redo))))
7425 (defun org-agenda-open-link (&optional arg)
7426 "Follow the link in the current line, if any.
7427 This looks for a link in the displayed line in the agenda. It also looks
7428 at the text of the entry itself."
7429 (interactive "P")
7430 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7431 (org-get-at-bol 'org-marker)))
7432 (buffer (and marker (marker-buffer marker)))
7433 (prefix (buffer-substring
7434 (point-at-bol) (point-at-eol))))
7435 (cond
7436 (buffer
7437 (with-current-buffer buffer
7438 (save-excursion
7439 (save-restriction
7440 (widen)
7441 (goto-char marker)
7442 (org-offer-links-in-entry arg prefix)))))
7443 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7444 (save-excursion
7445 (beginning-of-line 1)
7446 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7447 (org-open-link-from-string (match-string 1)))
7448 (t (error "No link to open here")))))
7450 (defun org-agenda-copy-local-variable (var)
7451 "Get a variable from a referenced buffer and install it here."
7452 (let ((m (org-get-at-bol 'org-marker)))
7453 (when (and m (buffer-live-p (marker-buffer m)))
7454 (org-set-local var (with-current-buffer (marker-buffer m)
7455 (symbol-value var))))))
7457 (defun org-agenda-switch-to (&optional delete-other-windows)
7458 "Go to the Org-mode file which contains the item at point."
7459 (interactive)
7460 (if (and org-return-follows-link
7461 (not (org-get-at-bol 'org-marker))
7462 (org-in-regexp org-bracket-link-regexp))
7463 (org-open-link-from-string (match-string 0))
7464 (let* ((marker (or (org-get-at-bol 'org-marker)
7465 (org-agenda-error)))
7466 (buffer (marker-buffer marker))
7467 (pos (marker-position marker)))
7468 (org-pop-to-buffer-same-window buffer)
7469 (and delete-other-windows (delete-other-windows))
7470 (widen)
7471 (goto-char pos)
7472 (when (derived-mode-p 'org-mode)
7473 (org-show-context 'agenda)
7474 (save-excursion
7475 (and (outline-next-heading)
7476 (org-flag-heading nil))) ; show the next heading
7477 (when (outline-invisible-p)
7478 (show-entry)))))) ; display invisible text
7480 (defun org-agenda-goto-mouse (ev)
7481 "Go to the Org-mode file which contains the item at the mouse click."
7482 (interactive "e")
7483 (mouse-set-point ev)
7484 (org-agenda-goto))
7486 (defun org-agenda-show (&optional full-entry)
7487 "Display the Org-mode file which contains the item at point.
7488 With prefix argument FULL-ENTRY, make the entire entry visible
7489 if it was hidden in the outline."
7490 (interactive "P")
7491 (let ((win (selected-window)))
7492 (if full-entry
7493 (let ((org-show-entry-below t))
7494 (org-agenda-goto t))
7495 (org-agenda-goto t))
7496 (select-window win)))
7498 (defvar org-agenda-show-window nil)
7499 (defun org-agenda-show-and-scroll-up (&optional arg)
7500 "Display the Org-mode file which contains the item at point.
7501 When called repeatedly, scroll the window that is displaying the buffer.
7502 With a \\[universal-argument] prefix, use `org-show-entry' instead of
7503 `show-subtree' to display the item, so that drawers and logbooks stay
7504 folded."
7505 (interactive "P")
7506 (let ((win (selected-window)))
7507 (if (and (window-live-p org-agenda-show-window)
7508 (eq this-command last-command))
7509 (progn
7510 (select-window org-agenda-show-window)
7511 (ignore-errors (scroll-up)))
7512 (org-agenda-goto t)
7513 (if arg (org-show-entry) (show-subtree))
7514 (setq org-agenda-show-window (selected-window)))
7515 (select-window win)))
7517 (defun org-agenda-show-scroll-down ()
7518 "Scroll down the window showing the agenda."
7519 (interactive)
7520 (let ((win (selected-window)))
7521 (when (window-live-p org-agenda-show-window)
7522 (select-window org-agenda-show-window)
7523 (ignore-errors (scroll-down))
7524 (select-window win))))
7526 (defun org-agenda-show-1 (&optional more)
7527 "Display the Org-mode file which contains the item at point.
7528 The prefix arg selects the amount of information to display:
7530 0 hide the subtree
7531 1 just show the entry according to defaults.
7532 2 show the children view
7533 3 show the subtree view
7534 4 show the entire subtree and any LOGBOOK drawers
7535 5 show the entire subtree and any drawers
7536 With prefix argument FULL-ENTRY, make the entire entry visible
7537 if it was hidden in the outline."
7538 (interactive "p")
7539 (let ((win (selected-window)))
7540 (org-agenda-goto t)
7541 (org-recenter-heading 1)
7542 (cond
7543 ((= more 0)
7544 (hide-subtree)
7545 (save-excursion
7546 (org-back-to-heading)
7547 (run-hook-with-args 'org-cycle-hook 'folded))
7548 (message "Remote: FOLDED"))
7549 ((and (org-called-interactively-p 'any) (= more 1))
7550 (message "Remote: show with default settings"))
7551 ((= more 2)
7552 (show-entry)
7553 (show-children)
7554 (save-excursion
7555 (org-back-to-heading)
7556 (run-hook-with-args 'org-cycle-hook 'children))
7557 (message "Remote: CHILDREN"))
7558 ((= more 3)
7559 (show-subtree)
7560 (save-excursion
7561 (org-back-to-heading)
7562 (run-hook-with-args 'org-cycle-hook 'subtree))
7563 (message "Remote: SUBTREE"))
7564 ((= more 4)
7565 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7566 (org-drawer-regexp
7567 (concat "^[ \t]*:\\("
7568 (mapconcat 'regexp-quote org-drawers "\\|")
7569 "\\):[ \t]*$")))
7570 (show-subtree)
7571 (save-excursion
7572 (org-back-to-heading)
7573 (org-cycle-hide-drawers 'subtree)))
7574 (message "Remote: SUBTREE AND LOGBOOK"))
7575 ((> more 4)
7576 (show-subtree)
7577 (message "Remote: SUBTREE AND ALL DRAWERS")))
7578 (select-window win)))
7580 (defun org-recenter-heading (n)
7581 (save-excursion
7582 (org-back-to-heading)
7583 (recenter n)))
7585 (defvar org-agenda-cycle-counter nil)
7586 (defun org-agenda-cycle-show (&optional n)
7587 "Show the current entry in another window, with default settings.
7588 Default settings are taken from `org-show-hierarchy-above' and siblings.
7589 When use repeatedly in immediate succession, the remote entry will cycle
7590 through visibility
7592 children -> subtree -> folded
7594 When called with a numeric prefix arg, that arg will be passed through to
7595 `org-agenda-show-1'. For the interpretation of that argument, see the
7596 docstring of `org-agenda-show-1'."
7597 (interactive "P")
7598 (if (integerp n)
7599 (setq org-agenda-cycle-counter n)
7600 (if (not (eq last-command this-command))
7601 (setq org-agenda-cycle-counter 1)
7602 (if (equal org-agenda-cycle-counter 0)
7603 (setq org-agenda-cycle-counter 2)
7604 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7605 (if (> org-agenda-cycle-counter 3)
7606 (setq org-agenda-cycle-counter 0)))))
7607 (org-agenda-show-1 org-agenda-cycle-counter))
7609 (defun org-agenda-recenter (arg)
7610 "Display the Org-mode file which contains the item at point and recenter."
7611 (interactive "P")
7612 (let ((win (selected-window)))
7613 (org-agenda-goto t)
7614 (recenter arg)
7615 (select-window win)))
7617 (defun org-agenda-show-mouse (ev)
7618 "Display the Org-mode file which contains the item at the mouse click."
7619 (interactive "e")
7620 (mouse-set-point ev)
7621 (org-agenda-show))
7623 (defun org-agenda-check-no-diary ()
7624 "Check if the entry is a diary link and abort if yes."
7625 (if (org-get-at-bol 'org-agenda-diary-link)
7626 (org-agenda-error)))
7628 (defun org-agenda-error ()
7629 (error "Command not allowed in this line"))
7631 (defun org-agenda-tree-to-indirect-buffer ()
7632 "Show the subtree corresponding to the current entry in an indirect buffer.
7633 This calls the command `org-tree-to-indirect-buffer' from the original
7634 Org-mode buffer.
7635 With numerical prefix arg ARG, go up to this level and then take that tree.
7636 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7637 use the dedicated frame)."
7638 (interactive)
7639 (if (and current-prefix-arg (listp current-prefix-arg))
7640 (org-agenda-do-tree-to-indirect-buffer)
7641 (let ((agenda-window (selected-window))
7642 (indirect-window
7643 (and org-last-indirect-buffer
7644 (get-buffer-window org-last-indirect-buffer))))
7645 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer))
7646 (unwind-protect
7647 (progn
7648 (unless (and indirect-window (window-live-p indirect-window))
7649 (setq indirect-window (split-window agenda-window)))
7650 (select-window indirect-window)
7651 (switch-to-buffer org-last-indirect-buffer :norecord)
7652 (fit-window-to-buffer indirect-window))
7653 (select-window (get-buffer-window org-agenda-buffer-name))))))
7655 (defun org-agenda-do-tree-to-indirect-buffer ()
7656 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7657 (org-agenda-check-no-diary)
7658 (let* ((marker (or (org-get-at-bol 'org-marker)
7659 (org-agenda-error)))
7660 (buffer (marker-buffer marker))
7661 (pos (marker-position marker)))
7662 (with-current-buffer buffer
7663 (save-excursion
7664 (goto-char pos)
7665 (call-interactively 'org-tree-to-indirect-buffer)))))
7667 (defvar org-last-heading-marker (make-marker)
7668 "Marker pointing to the headline that last changed its TODO state
7669 by a remote command from the agenda.")
7671 (defun org-agenda-todo-nextset ()
7672 "Switch TODO entry to next sequence."
7673 (interactive)
7674 (org-agenda-todo 'nextset))
7676 (defun org-agenda-todo-previousset ()
7677 "Switch TODO entry to previous sequence."
7678 (interactive)
7679 (org-agenda-todo 'previousset))
7681 (defun org-agenda-todo (&optional arg)
7682 "Cycle TODO state of line at point, also in Org-mode file.
7683 This changes the line at point, all other lines in the agenda referring to
7684 the same tree node, and the headline of the tree node in the Org-mode file."
7685 (interactive "P")
7686 (org-agenda-check-no-diary)
7687 (let* ((col (current-column))
7688 (marker (or (org-get-at-bol 'org-marker)
7689 (org-agenda-error)))
7690 (buffer (marker-buffer marker))
7691 (pos (marker-position marker))
7692 (hdmarker (org-get-at-bol 'org-hd-marker))
7693 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7694 (inhibit-read-only t)
7695 org-agenda-headline-snapshot-before-repeat newhead just-one)
7696 (org-with-remote-undo buffer
7697 (with-current-buffer buffer
7698 (widen)
7699 (goto-char pos)
7700 (org-show-context 'agenda)
7701 (save-excursion
7702 (and (outline-next-heading)
7703 (org-flag-heading nil))) ; show the next heading
7704 (let ((current-prefix-arg arg))
7705 (call-interactively 'org-todo))
7706 (and (bolp) (forward-char 1))
7707 (setq newhead (org-get-heading))
7708 (when (and (org-bound-and-true-p
7709 org-agenda-headline-snapshot-before-repeat)
7710 (not (equal org-agenda-headline-snapshot-before-repeat
7711 newhead))
7712 todayp)
7713 (setq newhead org-agenda-headline-snapshot-before-repeat
7714 just-one t))
7715 (save-excursion
7716 (org-back-to-heading)
7717 (move-marker org-last-heading-marker (point))))
7718 (beginning-of-line 1)
7719 (save-excursion
7720 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7721 (org-move-to-column col))))
7723 (defun org-agenda-add-note (&optional arg)
7724 "Add a time-stamped note to the entry at point."
7725 (interactive "P")
7726 (org-agenda-check-no-diary)
7727 (let* ((marker (or (org-get-at-bol 'org-marker)
7728 (org-agenda-error)))
7729 (buffer (marker-buffer marker))
7730 (pos (marker-position marker))
7731 (hdmarker (org-get-at-bol 'org-hd-marker))
7732 (inhibit-read-only t))
7733 (with-current-buffer buffer
7734 (widen)
7735 (goto-char pos)
7736 (org-show-context 'agenda)
7737 (save-excursion
7738 (and (outline-next-heading)
7739 (org-flag-heading nil))) ; show the next heading
7740 (org-add-note))))
7742 (defun org-agenda-change-all-lines (newhead hdmarker
7743 &optional fixface just-this)
7744 "Change all lines in the agenda buffer which match HDMARKER.
7745 The new content of the line will be NEWHEAD (as modified by
7746 `org-agenda-format-item'). HDMARKER is checked with
7747 `equal' against all `org-hd-marker' text properties in the file.
7748 If FIXFACE is non-nil, the face of each item is modified according to
7749 the new TODO state.
7750 If JUST-THIS is non-nil, change just the current line, not all.
7751 If FORCE-TAGS is non nil, the car of it returns the new tags."
7752 (let* ((inhibit-read-only t)
7753 (line (org-current-line))
7754 (org-agenda-buffer (current-buffer))
7755 (thetags (with-current-buffer (marker-buffer hdmarker)
7756 (save-excursion (save-restriction (widen)
7757 (goto-char hdmarker)
7758 (org-get-tags-at)))))
7759 props m pl undone-face done-face finish new dotime cat tags)
7760 (save-excursion
7761 (goto-char (point-max))
7762 (beginning-of-line 1)
7763 (while (not finish)
7764 (setq finish (bobp))
7765 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7766 (or (not just-this) (= (org-current-line) line))
7767 (equal m hdmarker))
7768 (setq props (text-properties-at (point))
7769 dotime (org-get-at-bol 'dotime)
7770 cat (org-get-at-bol 'org-category)
7771 tags thetags
7773 (let ((org-prefix-format-compiled
7774 (or (get-text-property (point) 'format)
7775 org-prefix-format-compiled))
7776 (extra (org-get-at-bol 'extra)))
7777 (with-current-buffer (marker-buffer hdmarker)
7778 (save-excursion
7779 (save-restriction
7780 (widen)
7781 (org-agenda-format-item extra newhead cat tags dotime)))))
7782 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7783 undone-face (org-get-at-bol 'undone-face)
7784 done-face (org-get-at-bol 'done-face))
7785 (beginning-of-line 1)
7786 (cond
7787 ((equal new "")
7788 (and (looking-at ".*\n?") (replace-match "")))
7789 ((looking-at ".*")
7790 (replace-match new t t)
7791 (beginning-of-line 1)
7792 (add-text-properties (point-at-bol) (point-at-eol) props)
7793 (when fixface
7794 (add-text-properties
7795 (point-at-bol) (point-at-eol)
7796 (list 'face
7797 (if org-last-todo-state-is-todo
7798 undone-face done-face))))
7799 (org-agenda-highlight-todo 'line)
7800 (beginning-of-line 1))
7801 (t (error "Line update did not work"))))
7802 (beginning-of-line 0)))
7803 (org-finalize-agenda)))
7805 (defun org-agenda-align-tags (&optional line)
7806 "Align all tags in agenda items to `org-agenda-tags-column'."
7807 (let ((inhibit-read-only t) l c)
7808 (save-excursion
7809 (goto-char (if line (point-at-bol) (point-min)))
7810 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7811 (if line (point-at-eol) nil) t)
7812 (add-text-properties
7813 (match-beginning 2) (match-end 2)
7814 (list 'face (delq nil (let ((prop (get-text-property
7815 (match-beginning 2) 'face)))
7816 (or (listp prop) (setq prop (list prop)))
7817 (if (memq 'org-tag prop)
7818 prop
7819 (cons 'org-tag prop))))))
7820 (setq l (- (match-end 2) (match-beginning 2))
7821 c (if (< org-agenda-tags-column 0)
7822 (- (abs org-agenda-tags-column) l)
7823 org-agenda-tags-column))
7824 (delete-region (match-beginning 1) (match-end 1))
7825 (goto-char (match-beginning 1))
7826 (insert (org-add-props
7827 (make-string (max 1 (- c (current-column))) ?\ )
7828 (plist-put (copy-sequence (text-properties-at (point)))
7829 'face nil))))
7830 (goto-char (point-min))
7831 (org-font-lock-add-tag-faces (point-max)))))
7833 (defun org-agenda-priority-up ()
7834 "Increase the priority of line at point, also in Org-mode file."
7835 (interactive)
7836 (org-agenda-priority 'up))
7838 (defun org-agenda-priority-down ()
7839 "Decrease the priority of line at point, also in Org-mode file."
7840 (interactive)
7841 (org-agenda-priority 'down))
7843 (defun org-agenda-priority (&optional force-direction)
7844 "Set the priority of line at point, also in Org-mode file.
7845 This changes the line at point, all other lines in the agenda referring to
7846 the same tree node, and the headline of the tree node in the Org-mode file."
7847 (interactive)
7848 (unless org-enable-priority-commands
7849 (error "Priority commands are disabled"))
7850 (org-agenda-check-no-diary)
7851 (let* ((marker (or (org-get-at-bol 'org-marker)
7852 (org-agenda-error)))
7853 (hdmarker (org-get-at-bol 'org-hd-marker))
7854 (buffer (marker-buffer hdmarker))
7855 (pos (marker-position hdmarker))
7856 (inhibit-read-only t)
7857 newhead)
7858 (org-with-remote-undo buffer
7859 (with-current-buffer buffer
7860 (widen)
7861 (goto-char pos)
7862 (org-show-context 'agenda)
7863 (save-excursion
7864 (and (outline-next-heading)
7865 (org-flag-heading nil))) ; show the next heading
7866 (funcall 'org-priority force-direction)
7867 (end-of-line 1)
7868 (setq newhead (org-get-heading)))
7869 (org-agenda-change-all-lines newhead hdmarker)
7870 (beginning-of-line 1))))
7872 ;; FIXME: should fix the tags property of the agenda line.
7873 (defun org-agenda-set-tags (&optional tag onoff)
7874 "Set tags for the current headline."
7875 (interactive)
7876 (org-agenda-check-no-diary)
7877 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7878 (call-interactively 'org-change-tag-in-region)
7879 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7880 (org-agenda-error)))
7881 (buffer (marker-buffer hdmarker))
7882 (pos (marker-position hdmarker))
7883 (inhibit-read-only t)
7884 newhead)
7885 (org-with-remote-undo buffer
7886 (with-current-buffer buffer
7887 (widen)
7888 (goto-char pos)
7889 (save-excursion
7890 (org-show-context 'agenda))
7891 (save-excursion
7892 (and (outline-next-heading)
7893 (org-flag-heading nil))) ; show the next heading
7894 (goto-char pos)
7895 (if tag
7896 (org-toggle-tag tag onoff)
7897 (call-interactively 'org-set-tags))
7898 (end-of-line 1)
7899 (setq newhead (org-get-heading)))
7900 (org-agenda-change-all-lines newhead hdmarker)
7901 (beginning-of-line 1)))))
7903 (defun org-agenda-set-property ()
7904 "Set a property for the current headline."
7905 (interactive)
7906 (org-agenda-check-no-diary)
7907 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7908 (org-agenda-error)))
7909 (buffer (marker-buffer hdmarker))
7910 (pos (marker-position hdmarker))
7911 (inhibit-read-only t)
7912 newhead)
7913 (org-with-remote-undo buffer
7914 (with-current-buffer buffer
7915 (widen)
7916 (goto-char pos)
7917 (save-excursion
7918 (org-show-context 'agenda))
7919 (save-excursion
7920 (and (outline-next-heading)
7921 (org-flag-heading nil))) ; show the next heading
7922 (goto-char pos)
7923 (call-interactively 'org-set-property)))))
7925 (defun org-agenda-set-effort ()
7926 "Set the effort property for the current headline."
7927 (interactive)
7928 (org-agenda-check-no-diary)
7929 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7930 (org-agenda-error)))
7931 (buffer (marker-buffer hdmarker))
7932 (pos (marker-position hdmarker))
7933 (inhibit-read-only t)
7934 newhead)
7935 (org-with-remote-undo buffer
7936 (with-current-buffer buffer
7937 (widen)
7938 (goto-char pos)
7939 (save-excursion
7940 (org-show-context 'agenda))
7941 (save-excursion
7942 (and (outline-next-heading)
7943 (org-flag-heading nil))) ; show the next heading
7944 (goto-char pos)
7945 (call-interactively 'org-set-effort)
7946 (end-of-line 1)
7947 (setq newhead (org-get-heading)))
7948 (org-agenda-change-all-lines newhead hdmarker))))
7950 (defun org-agenda-toggle-archive-tag ()
7951 "Toggle the archive tag for the current entry."
7952 (interactive)
7953 (org-agenda-check-no-diary)
7954 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7955 (org-agenda-error)))
7956 (buffer (marker-buffer hdmarker))
7957 (pos (marker-position hdmarker))
7958 (inhibit-read-only t)
7959 newhead)
7960 (org-with-remote-undo buffer
7961 (with-current-buffer buffer
7962 (widen)
7963 (goto-char pos)
7964 (org-show-context 'agenda)
7965 (save-excursion
7966 (and (outline-next-heading)
7967 (org-flag-heading nil))) ; show the next heading
7968 (call-interactively 'org-toggle-archive-tag)
7969 (end-of-line 1)
7970 (setq newhead (org-get-heading)))
7971 (org-agenda-change-all-lines newhead hdmarker)
7972 (beginning-of-line 1))))
7974 (defun org-agenda-do-date-later (arg)
7975 (interactive "P")
7976 (cond
7977 ((or (equal arg '(16))
7978 (memq last-command
7979 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7980 (setq this-command 'org-agenda-date-later-minutes)
7981 (org-agenda-date-later-minutes 1))
7982 ((or (equal arg '(4))
7983 (memq last-command
7984 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7985 (setq this-command 'org-agenda-date-later-hours)
7986 (org-agenda-date-later-hours 1))
7988 (org-agenda-date-later (prefix-numeric-value arg)))))
7990 (defun org-agenda-do-date-earlier (arg)
7991 (interactive "P")
7992 (cond
7993 ((or (equal arg '(16))
7994 (memq last-command
7995 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7996 (setq this-command 'org-agenda-date-earlier-minutes)
7997 (org-agenda-date-earlier-minutes 1))
7998 ((or (equal arg '(4))
7999 (memq last-command
8000 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8001 (setq this-command 'org-agenda-date-earlier-hours)
8002 (org-agenda-date-earlier-hours 1))
8004 (org-agenda-date-earlier (prefix-numeric-value arg)))))
8006 (defun org-agenda-date-later (arg &optional what)
8007 "Change the date of this item to ARG day(s) later."
8008 (interactive "p")
8009 (org-agenda-check-type t 'agenda 'timeline)
8010 (org-agenda-check-no-diary)
8011 (let* ((marker (or (org-get-at-bol 'org-marker)
8012 (org-agenda-error)))
8013 (buffer (marker-buffer marker))
8014 (pos (marker-position marker))
8015 cdate today)
8016 (org-with-remote-undo buffer
8017 (with-current-buffer buffer
8018 (widen)
8019 (goto-char pos)
8020 (if (not (org-at-timestamp-p))
8021 (error "Cannot find time stamp"))
8022 (when (and org-agenda-move-date-from-past-immediately-to-today
8023 (equal arg 1)
8024 (or (not what) (eq what 'day))
8025 (not (save-match-data (org-at-date-range-p))))
8026 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
8027 cdate (calendar-absolute-from-gregorian
8028 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
8029 today (org-today))
8030 (if (> today cdate)
8031 ;; immediately shift to today
8032 (setq arg (- today cdate))))
8033 (org-timestamp-change arg (or what 'day))
8034 (when (and (org-at-date-range-p)
8035 (re-search-backward org-tr-regexp-both (point-at-bol)))
8036 (let ((end org-last-changed-timestamp))
8037 (org-timestamp-change arg (or what 'day))
8038 (setq org-last-changed-timestamp
8039 (concat org-last-changed-timestamp "--" end)))))
8040 (org-agenda-show-new-time marker org-last-changed-timestamp))
8041 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8043 (defun org-agenda-date-earlier (arg &optional what)
8044 "Change the date of this item to ARG day(s) earlier."
8045 (interactive "p")
8046 (org-agenda-date-later (- arg) what))
8048 (defun org-agenda-date-later-minutes (arg)
8049 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8050 (interactive "p")
8051 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8052 (org-agenda-date-later arg 'minute))
8054 (defun org-agenda-date-earlier-minutes (arg)
8055 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8056 (interactive "p")
8057 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8058 (org-agenda-date-earlier arg 'minute))
8060 (defun org-agenda-date-later-hours (arg)
8061 "Change the time of this item, in hour steps."
8062 (interactive "p")
8063 (org-agenda-date-later arg 'hour))
8065 (defun org-agenda-date-earlier-hours (arg)
8066 "Change the time of this item, in hour steps."
8067 (interactive "p")
8068 (org-agenda-date-earlier arg 'hour))
8070 (defun org-agenda-show-new-time (marker stamp &optional prefix)
8071 "Show new date stamp via text properties."
8072 ;; We use text properties to make this undoable
8073 (let ((inhibit-read-only t)
8074 (buffer-invisibility-spec))
8075 (setq stamp (concat " " prefix " => " stamp))
8076 (save-excursion
8077 (goto-char (point-max))
8078 (while (not (bobp))
8079 (when (equal marker (org-get-at-bol 'org-marker))
8080 (org-move-to-column (- (window-width) (length stamp)) t)
8081 (org-agenda-fix-tags-filter-overlays-at (point))
8082 (if (featurep 'xemacs)
8083 ;; Use `duplicable' property to trigger undo recording
8084 (let ((ex (make-extent nil nil))
8085 (gl (make-glyph stamp)))
8086 (set-glyph-face gl 'secondary-selection)
8087 (set-extent-properties
8088 ex (list 'invisible t 'end-glyph gl 'duplicable t))
8089 (insert-extent ex (1- (point)) (point-at-eol)))
8090 (add-text-properties
8091 (1- (point)) (point-at-eol)
8092 (list 'display (org-add-props stamp nil
8093 'face 'secondary-selection))))
8094 (beginning-of-line 1))
8095 (beginning-of-line 0)))))
8097 (defun org-agenda-date-prompt (arg)
8098 "Change the date of this item. Date is prompted for, with default today.
8099 The prefix ARG is passed to the `org-time-stamp' command and can therefore
8100 be used to request time specification in the time stamp."
8101 (interactive "P")
8102 (org-agenda-check-type t 'agenda 'timeline)
8103 (org-agenda-check-no-diary)
8104 (let* ((marker (or (org-get-at-bol 'org-marker)
8105 (org-agenda-error)))
8106 (buffer (marker-buffer marker))
8107 (pos (marker-position marker)))
8108 (org-with-remote-undo buffer
8109 (with-current-buffer buffer
8110 (widen)
8111 (goto-char pos)
8112 (if (not (org-at-timestamp-p t))
8113 (error "Cannot find time stamp"))
8114 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8115 (org-agenda-show-new-time marker org-last-changed-timestamp))
8116 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8118 (defun org-agenda-schedule (arg &optional time)
8119 "Schedule the item at point.
8120 ARG is passed through to `org-schedule'."
8121 (interactive "P")
8122 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8123 (org-agenda-check-no-diary)
8124 (let* ((marker (or (org-get-at-bol 'org-marker)
8125 (org-agenda-error)))
8126 (type (marker-insertion-type marker))
8127 (buffer (marker-buffer marker))
8128 (pos (marker-position marker))
8129 (org-insert-labeled-timestamps-at-point nil)
8131 (set-marker-insertion-type marker t)
8132 (org-with-remote-undo buffer
8133 (with-current-buffer buffer
8134 (widen)
8135 (goto-char pos)
8136 (setq ts (org-schedule arg time)))
8137 (org-agenda-show-new-time marker ts "S"))
8138 (message "Item scheduled for %s" ts)))
8140 (defun org-agenda-deadline (arg &optional time)
8141 "Schedule the item at point.
8142 ARG is passed through to `org-deadline'."
8143 (interactive "P")
8144 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8145 (org-agenda-check-no-diary)
8146 (let* ((marker (or (org-get-at-bol 'org-marker)
8147 (org-agenda-error)))
8148 (buffer (marker-buffer marker))
8149 (pos (marker-position marker))
8150 (org-insert-labeled-timestamps-at-point nil)
8152 (org-with-remote-undo buffer
8153 (with-current-buffer buffer
8154 (widen)
8155 (goto-char pos)
8156 (setq ts (org-deadline arg time)))
8157 (org-agenda-show-new-time marker ts "D"))
8158 (message "Deadline for this item set to %s" ts)))
8160 (defun org-agenda-action ()
8161 "Select entry for agenda action, or execute an agenda action.
8162 This command prompts for another letter. Valid inputs are:
8164 m Mark the entry at point for an agenda action
8165 s Schedule the marked entry to the date at the cursor
8166 d Set the deadline of the marked entry to the date at the cursor
8167 r Call `org-remember' with cursor date as the default date
8168 c Call `org-capture' with cursor date as the default date
8169 SPC Show marked entry in other window
8170 TAB Visit marked entry in other window
8172 The cursor may be at a date in the calendar, or in the Org agenda."
8173 (interactive)
8174 (let (ans)
8175 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
8176 (setq ans (read-char-exclusive))
8177 (cond
8178 ((equal ans ?m)
8179 ;; Mark this entry
8180 (if (eq major-mode 'org-agenda-mode)
8181 (let ((m (or (org-get-at-bol 'org-hd-marker)
8182 (org-get-at-bol 'org-marker))))
8183 (if m
8184 (progn
8185 (move-marker org-agenda-action-marker
8186 (marker-position m) (marker-buffer m))
8187 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
8188 (error "Don't know which entry to mark")))
8189 (error "This command works only in the agenda")))
8190 ((equal ans ?s)
8191 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
8192 ((equal ans ?d)
8193 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
8194 ((equal ans ?r)
8195 (org-agenda-do-action '(org-remember) t))
8196 ((equal ans ?c)
8197 (org-agenda-do-action '(org-capture) t))
8198 ((equal ans ?\ )
8199 (let ((cw (selected-window)))
8200 (org-switch-to-buffer-other-window
8201 (marker-buffer org-agenda-action-marker))
8202 (goto-char org-agenda-action-marker)
8203 (org-show-context 'agenda)
8204 (select-window cw)))
8205 ((equal ans ?\C-i)
8206 (org-switch-to-buffer-other-window
8207 (marker-buffer org-agenda-action-marker))
8208 (goto-char org-agenda-action-marker)
8209 (org-show-context 'agenda))
8210 (t (error "Invalid agenda action %c" ans)))))
8212 (defun org-agenda-do-action (form &optional current-buffer)
8213 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
8214 (let ((org-overriding-default-time (org-get-cursor-date)))
8215 (if current-buffer
8216 (eval form)
8217 (if (not (marker-buffer org-agenda-action-marker))
8218 (error "No entry has been selected for agenda action")
8219 (with-current-buffer (marker-buffer org-agenda-action-marker)
8220 (save-excursion
8221 (save-restriction
8222 (widen)
8223 (goto-char org-agenda-action-marker)
8224 (eval form))))))))
8226 (defun org-agenda-clock-in (&optional arg)
8227 "Start the clock on the currently selected item."
8228 (interactive "P")
8229 (org-agenda-check-no-diary)
8230 (if (equal arg '(4))
8231 (org-clock-in arg)
8232 (let* ((marker (or (org-get-at-bol 'org-marker)
8233 (org-agenda-error)))
8234 (hdmarker (or (org-get-at-bol 'org-hd-marker)
8235 marker))
8236 (pos (marker-position marker))
8237 newhead)
8238 (org-with-remote-undo (marker-buffer marker)
8239 (with-current-buffer (marker-buffer marker)
8240 (widen)
8241 (goto-char pos)
8242 (org-show-context 'agenda)
8243 (org-show-entry)
8244 (org-cycle-hide-drawers 'children)
8245 (org-clock-in arg)
8246 (setq newhead (org-get-heading)))
8247 (org-agenda-change-all-lines newhead hdmarker)))))
8249 (defun org-agenda-clock-out ()
8250 "Stop the currently running clock."
8251 (interactive)
8252 (unless (marker-buffer org-clock-marker)
8253 (error "No running clock"))
8254 (let ((marker (make-marker)) newhead)
8255 (org-with-remote-undo (marker-buffer org-clock-marker)
8256 (with-current-buffer (marker-buffer org-clock-marker)
8257 (save-excursion
8258 (save-restriction
8259 (widen)
8260 (goto-char org-clock-marker)
8261 (org-back-to-heading t)
8262 (move-marker marker (point))
8263 (org-clock-out)
8264 (setq newhead (org-get-heading))))))
8265 (org-agenda-change-all-lines newhead marker)
8266 (move-marker marker nil)))
8268 (defun org-agenda-clock-cancel (&optional arg)
8269 "Cancel the currently running clock."
8270 (interactive "P")
8271 (unless (marker-buffer org-clock-marker)
8272 (error "No running clock"))
8273 (org-with-remote-undo (marker-buffer org-clock-marker)
8274 (org-clock-cancel)))
8276 (defun org-agenda-clock-goto ()
8277 "Jump to the currently clocked in task within the agenda.
8278 If the currently clocked in task is not listed in the agenda
8279 buffer, display it in another window."
8280 (interactive)
8281 (let (pos)
8282 (mapc (lambda (o)
8283 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
8284 (setq pos (overlay-start o))))
8285 (overlays-in (point-min) (point-max)))
8286 (cond (pos (goto-char pos))
8287 ;; If the currently clocked entry is not in the agenda
8288 ;; buffer, we visit it in another window:
8289 (org-clock-current-task
8290 (org-switch-to-buffer-other-window (org-clock-goto)))
8291 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
8293 (defun org-agenda-diary-entry-in-org-file ()
8294 "Make a diary entry in the file `org-agenda-diary-file'."
8295 (let (d1 d2 char (text "") dp1 dp2)
8296 (if (equal (buffer-name) "*Calendar*")
8297 (setq d1 (calendar-cursor-to-date t)
8298 d2 (car calendar-mark-ring))
8299 (setq dp1 (get-text-property (point-at-bol) 'day))
8300 (unless dp1 (error "No date defined in current line"))
8301 (setq d1 (calendar-gregorian-from-absolute dp1)
8302 d2 (and (ignore-errors (mark))
8303 (save-excursion
8304 (goto-char (mark))
8305 (setq dp2 (get-text-property (point-at-bol) 'day)))
8306 (calendar-gregorian-from-absolute dp2))))
8307 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
8308 (setq char (read-char-exclusive))
8309 (cond
8310 ((equal char ?d)
8311 (setq text (read-string "Day entry: "))
8312 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
8313 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8314 ((equal char ?a)
8315 (setq d1 (list (car d1) (nth 1 d1)
8316 (read-number (format "Reference year [%d]: " (nth 2 d1))
8317 (nth 2 d1))))
8318 (setq text (read-string "Anniversary (use %d to show years): "))
8319 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
8320 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8321 ((equal char ?b)
8322 (setq text (read-string "Block entry: "))
8323 (unless (and d1 d2 (not (equal d1 d2)))
8324 (error "No block of days selected"))
8325 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
8326 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8327 ((equal char ?j)
8328 (org-switch-to-buffer-other-window
8329 (find-file-noselect org-agenda-diary-file))
8330 (require 'org-datetree)
8331 (org-datetree-find-date-create d1)
8332 (org-reveal t))
8333 (t (error "Invalid selection character `%c'" char)))))
8335 (defcustom org-agenda-insert-diary-strategy 'date-tree
8336 "Where in `org-agenda-diary-file' should new entries be added?
8337 Valid values:
8339 date-tree in the date tree, as child of the date
8340 top-level as top-level entries at the end of the file."
8341 :group 'org-agenda
8342 :type '(choice
8343 (const :tag "in a date tree" date-tree)
8344 (const :tag "as top level at end of file" top-level)))
8346 (defcustom org-agenda-insert-diary-extract-time nil
8347 "Non-nil means extract any time specification from the diary entry."
8348 :group 'org-agenda
8349 :version "24.1"
8350 :type 'boolean)
8352 (defcustom org-agenda-bulk-mark-char ">"
8353 "A single-character string to be used as the bulk mark."
8354 :group 'org-agenda
8355 :version "24.1"
8356 :type 'string)
8358 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8359 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8360 If TEXT is not empty, it will become the headline of the new entry, and
8361 the resulting entry will not be shown. When TEXT is empty, switch to
8362 `org-agenda-diary-file' and let the user finish the entry there."
8363 (let ((cw (current-window-configuration)))
8364 (org-switch-to-buffer-other-window
8365 (find-file-noselect org-agenda-diary-file))
8366 (widen)
8367 (goto-char (point-min))
8368 (cond
8369 ((eq type 'anniversary)
8370 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8371 (progn
8372 (or (org-at-heading-p t)
8373 (progn
8374 (outline-next-heading)
8375 (insert "* Anniversaries\n\n")
8376 (beginning-of-line -1)))))
8377 (outline-next-heading)
8378 (org-back-over-empty-lines)
8379 (backward-char 1)
8380 (insert "\n")
8381 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8382 (nth 2 d1) (car d1) (nth 1 d1) text)))
8383 ((eq type 'day)
8384 (let ((org-prefix-has-time t)
8385 (org-agenda-time-leading-zero t)
8386 fmt time time2)
8387 (if org-agenda-insert-diary-extract-time
8388 ;; Use org-agenda-format-item to parse text for a time-range and
8389 ;; remove it. FIXME: This is a hack, we should refactor
8390 ;; that function to make time extraction available separately
8391 (setq fmt (org-agenda-format-item nil text nil nil t)
8392 time (get-text-property 0 'time fmt)
8393 time2 (if (> (length time) 0)
8394 ;; split-string removes trailing ...... if
8395 ;; no end time given. First space
8396 ;; separates time from date.
8397 (concat " " (car (split-string time "\\.")))
8398 nil)
8399 text (get-text-property 0 'txt fmt)))
8400 (if (eq org-agenda-insert-diary-strategy 'top-level)
8401 (org-agenda-insert-diary-as-top-level text)
8402 (require 'org-datetree)
8403 (org-datetree-find-date-create d1)
8404 (org-agenda-insert-diary-make-new-entry text))
8405 (org-insert-time-stamp (org-time-from-absolute
8406 (calendar-absolute-from-gregorian d1))
8407 nil nil nil nil time2))
8408 (end-of-line 0))
8409 ((eq type 'block)
8410 (if (> (calendar-absolute-from-gregorian d1)
8411 (calendar-absolute-from-gregorian d2))
8412 (setq d1 (prog1 d2 (setq d2 d1))))
8413 (if (eq org-agenda-insert-diary-strategy 'top-level)
8414 (org-agenda-insert-diary-as-top-level text)
8415 (require 'org-datetree)
8416 (org-datetree-find-date-create d1)
8417 (org-agenda-insert-diary-make-new-entry text))
8418 (org-insert-time-stamp (org-time-from-absolute
8419 (calendar-absolute-from-gregorian d1)))
8420 (insert "--")
8421 (org-insert-time-stamp (org-time-from-absolute
8422 (calendar-absolute-from-gregorian d2)))
8423 (end-of-line 0)))
8424 (if (string-match "\\S-" text)
8425 (progn
8426 (set-window-configuration cw)
8427 (message "%s entry added to %s"
8428 (capitalize (symbol-name type))
8429 (abbreviate-file-name org-agenda-diary-file)))
8430 (org-reveal t)
8431 (message "Please finish entry here"))))
8433 (defun org-agenda-insert-diary-as-top-level (text)
8434 "Make new entry as a top-level entry at the end of the file.
8435 Add TEXT as headline, and position the cursor in the second line so that
8436 a timestamp can be added there."
8437 (widen)
8438 (goto-char (point-max))
8439 (or (bolp) (insert "\n"))
8440 (insert "* " text "\n")
8441 (if org-adapt-indentation (org-indent-to-column 2)))
8443 (defun org-agenda-insert-diary-make-new-entry (text)
8444 "Make new entry as last child of current entry.
8445 Add TEXT as headline, and position the cursor in the second line so that
8446 a timestamp can be added there."
8447 (let ((org-show-following-heading t)
8448 (org-show-siblings t)
8449 (org-show-hierarchy-above t)
8450 (org-show-entry-below t)
8451 col)
8452 (outline-next-heading)
8453 (org-back-over-empty-lines)
8454 (or (looking-at "[ \t]*$")
8455 (progn (insert "\n") (backward-char 1)))
8456 (org-insert-heading nil t)
8457 (org-do-demote)
8458 (setq col (current-column))
8459 (insert text "\n")
8460 (if org-adapt-indentation (org-indent-to-column col))
8461 (let ((org-show-following-heading t)
8462 (org-show-siblings t)
8463 (org-show-hierarchy-above t)
8464 (org-show-entry-below t))
8465 (org-show-context))))
8467 (defun org-agenda-diary-entry ()
8468 "Make a diary entry, like the `i' command from the calendar.
8469 All the standard commands work: block, weekly etc.
8470 When `org-agenda-diary-file' points to a file,
8471 `org-agenda-diary-entry-in-org-file' is called instead to create
8472 entries in that Org-mode file."
8473 (interactive)
8474 (org-agenda-check-type t 'agenda 'timeline)
8475 (if (not (eq org-agenda-diary-file 'diary-file))
8476 (org-agenda-diary-entry-in-org-file)
8477 (require 'diary-lib)
8478 (let* ((char (progn
8479 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8480 (read-char-exclusive)))
8481 (cmd (cdr (assoc char
8482 '((?d . insert-diary-entry)
8483 (?w . insert-weekly-diary-entry)
8484 (?m . insert-monthly-diary-entry)
8485 (?y . insert-yearly-diary-entry)
8486 (?a . insert-anniversary-diary-entry)
8487 (?b . insert-block-diary-entry)
8488 (?c . insert-cyclic-diary-entry)))))
8489 (oldf (symbol-function 'calendar-cursor-to-date))
8490 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8491 (point (point))
8492 (mark (or (mark t) (point))))
8493 (unless cmd
8494 (error "No command associated with <%c>" char))
8495 (unless (and (get-text-property point 'day)
8496 (or (not (equal ?b char))
8497 (get-text-property mark 'day)))
8498 (error "Don't know which date to use for diary entry"))
8499 ;; We implement this by hacking the `calendar-cursor-to-date' function
8500 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8501 (let ((calendar-mark-ring
8502 (list (calendar-gregorian-from-absolute
8503 (or (get-text-property mark 'day)
8504 (get-text-property point 'day))))))
8505 (unwind-protect
8506 (progn
8507 (fset 'calendar-cursor-to-date
8508 (lambda (&optional error dummy)
8509 (calendar-gregorian-from-absolute
8510 (get-text-property point 'day))))
8511 (call-interactively cmd))
8512 (fset 'calendar-cursor-to-date oldf))))))
8514 (defun org-agenda-execute-calendar-command (cmd)
8515 "Execute a calendar command from the agenda, with the date associated to
8516 the cursor position."
8517 (org-agenda-check-type t 'agenda 'timeline)
8518 (require 'diary-lib)
8519 (unless (get-text-property (point) 'day)
8520 (error "Don't know which date to use for calendar command"))
8521 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8522 (point (point))
8523 (date (calendar-gregorian-from-absolute
8524 (get-text-property point 'day)))
8525 ;; the following 2 vars are needed in the calendar
8526 (displayed-month (car date))
8527 (displayed-year (nth 2 date)))
8528 (unwind-protect
8529 (progn
8530 (fset 'calendar-cursor-to-date
8531 (lambda (&optional error dummy)
8532 (calendar-gregorian-from-absolute
8533 (get-text-property point 'day))))
8534 (call-interactively cmd))
8535 (fset 'calendar-cursor-to-date oldf))))
8537 (defun org-agenda-phases-of-moon ()
8538 "Display the phases of the moon for the 3 months around the cursor date."
8539 (interactive)
8540 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8542 (defun org-agenda-holidays ()
8543 "Display the holidays for the 3 months around the cursor date."
8544 (interactive)
8545 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8547 (defvar calendar-longitude)
8548 (defvar calendar-latitude)
8549 (defvar calendar-location-name)
8551 (defun org-agenda-sunrise-sunset (arg)
8552 "Display sunrise and sunset for the cursor date.
8553 Latitude and longitude can be specified with the variables
8554 `calendar-latitude' and `calendar-longitude'. When called with prefix
8555 argument, latitude and longitude will be prompted for."
8556 (interactive "P")
8557 (require 'solar)
8558 (let ((calendar-longitude (if arg nil calendar-longitude))
8559 (calendar-latitude (if arg nil calendar-latitude))
8560 (calendar-location-name
8561 (if arg "the given coordinates" calendar-location-name)))
8562 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8564 (defun org-agenda-goto-calendar ()
8565 "Open the Emacs calendar with the date at the cursor."
8566 (interactive)
8567 (org-agenda-check-type t 'agenda 'timeline)
8568 (let* ((day (or (get-text-property (point) 'day)
8569 (error "Don't know which date to open in calendar")))
8570 (date (calendar-gregorian-from-absolute day))
8571 (calendar-move-hook nil)
8572 (calendar-view-holidays-initially-flag nil)
8573 (calendar-view-diary-initially-flag nil))
8574 (calendar)
8575 (calendar-goto-date date)))
8577 ;;;###autoload
8578 (defun org-calendar-goto-agenda ()
8579 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8580 This is a command that has to be installed in `calendar-mode-map'."
8581 (interactive)
8582 (org-agenda-list nil (calendar-absolute-from-gregorian
8583 (calendar-cursor-to-date))
8584 nil))
8586 (defun org-agenda-convert-date ()
8587 (interactive)
8588 (org-agenda-check-type t 'agenda 'timeline)
8589 (let ((day (get-text-property (point) 'day))
8590 date s)
8591 (unless day
8592 (error "Don't know which date to convert"))
8593 (setq date (calendar-gregorian-from-absolute day))
8594 (setq s (concat
8595 "Gregorian: " (calendar-date-string date) "\n"
8596 "ISO: " (calendar-iso-date-string date) "\n"
8597 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8598 "Julian: " (calendar-julian-date-string date) "\n"
8599 "Astron. JD: " (calendar-astro-date-string date)
8600 " (Julian date number at noon UTC)\n"
8601 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8602 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8603 "French: " (calendar-french-date-string date) "\n"
8604 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8605 "Mayan: " (calendar-mayan-date-string date) "\n"
8606 "Coptic: " (calendar-coptic-date-string date) "\n"
8607 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8608 "Persian: " (calendar-persian-date-string date) "\n"
8609 "Chinese: " (calendar-chinese-date-string date) "\n"))
8610 (with-output-to-temp-buffer "*Dates*"
8611 (princ s))
8612 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8614 ;;; Bulk commands
8616 (defvar org-agenda-bulk-marked-entries nil
8617 "List of markers that refer to marked entries in the agenda.")
8619 (defun org-agenda-bulk-marked-p ()
8620 (eq (get-char-property (point-at-bol) 'type)
8621 'org-marked-entry-overlay))
8623 (defun org-agenda-bulk-mark (&optional arg)
8624 "Mark the entry at point for future bulk action."
8625 (interactive "p")
8626 (dotimes (i (or arg 1))
8627 (unless (org-get-at-bol 'org-agenda-diary-link)
8628 (let* ((m (org-get-at-bol 'org-hd-marker))
8630 (unless (org-agenda-bulk-marked-p)
8631 (unless m (error "Nothing to mark at point"))
8632 (push m org-agenda-bulk-marked-entries)
8633 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8634 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
8635 (org-get-todo-face "TODO")
8636 'evaporate)
8637 (overlay-put ov 'type 'org-marked-entry-overlay))
8638 (beginning-of-line 2)
8639 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8640 (beginning-of-line 2))
8641 (message "%d entries marked for bulk action"
8642 (length org-agenda-bulk-marked-entries))))))
8644 (defun org-agenda-bulk-mark-all ()
8645 "Mark all entries for future agenda bulk action."
8646 (interactive)
8647 (org-agenda-bulk-mark-regexp "."))
8649 (defun org-agenda-bulk-mark-regexp (regexp)
8650 "Mark entries matching REGEXP for future agenda bulk action."
8651 (interactive "sMark entries matching regexp: ")
8652 (let ((entries-marked 0))
8653 (save-excursion
8654 (goto-char (point-min))
8655 (goto-char (next-single-property-change (point) 'txt))
8656 (while (re-search-forward regexp nil t)
8657 (when (string-match regexp (get-text-property (point) 'txt))
8658 (setq entries-marked (1+ entries-marked))
8659 (call-interactively 'org-agenda-bulk-mark))))
8660 (if (not entries-marked)
8661 (message "No entry matching this regexp."))))
8663 (defun org-agenda-bulk-unmark (&optional arg)
8664 "Unmark the entry at point for future bulk action."
8665 (interactive "P")
8666 (if arg
8667 (org-agenda-bulk-unmark-all)
8668 (cond ((org-agenda-bulk-marked-p)
8669 (org-agenda-bulk-remove-overlays
8670 (point-at-bol) (+ 2 (point-at-bol)))
8671 (setq org-agenda-bulk-marked-entries
8672 (delete (org-get-at-bol 'org-hd-marker)
8673 org-agenda-bulk-marked-entries))
8674 (beginning-of-line 2)
8675 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8676 (beginning-of-line 2))
8677 (message "%d entries left marked for bulk action"
8678 (length org-agenda-bulk-marked-entries)))
8679 (t (message "No entry to unmark here")))))
8681 (defun org-agenda-bulk-toggle ()
8682 "Toggle marking the entry at point for bulk action."
8683 (interactive)
8684 (if (org-agenda-bulk-marked-p)
8685 (org-agenda-bulk-unmark)
8686 (org-agenda-bulk-mark)))
8688 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8689 "Remove the mark overlays between BEG and END in the agenda buffer.
8690 BEG and END default to the buffer limits.
8692 This only removes the overlays, it does not remove the markers
8693 from the list in `org-agenda-bulk-marked-entries'."
8694 (interactive)
8695 (mapc (lambda (ov)
8696 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8697 (delete-overlay ov)))
8698 (overlays-in (or beg (point-min)) (or end (point-max)))))
8700 (defun org-agenda-bulk-unmark-all ()
8701 "Remove all marks in the agenda buffer.
8702 This will remove the markers and the overlays."
8703 (interactive)
8704 (if (null org-agenda-bulk-marked-entries)
8705 (message "No entry to unmark")
8706 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8707 (setq org-agenda-bulk-marked-entries nil)
8708 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
8710 (defcustom org-agenda-persistent-marks nil
8711 "Non-nil means marked items will stay marked after a bulk action.
8712 You can interactively and temporarily toggle by typing `p' when you
8713 are prompted for a bulk action."
8714 :group 'org-agenda
8715 :version "24.1"
8716 :type 'boolean)
8718 (defun org-agenda-bulk-action (&optional arg)
8719 "Execute an remote-editing action on all marked entries.
8720 The prefix arg is passed through to the command if possible."
8721 (interactive "P")
8722 ;; Make sure we have markers, and only valid ones
8723 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8724 (mapc
8725 (lambda (m)
8726 (unless (and (markerp m)
8727 (marker-buffer m)
8728 (buffer-live-p (marker-buffer m))
8729 (marker-position m))
8730 (error "Marker %s for bulk command is invalid" m)))
8731 org-agenda-bulk-marked-entries)
8733 ;; Prompt for the bulk command
8734 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
8735 (message (concat msg "[r]efile [$]arch [A]rch->sib [t]odo"
8736 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction "
8737 (when org-agenda-bulk-custom-functions
8738 (concat " Custom: ["
8739 (mapconcat (lambda(f) (char-to-string (car f)))
8740 org-agenda-bulk-custom-functions "")
8741 "]"))))
8742 (catch 'exit
8743 (let* ((action (read-char-exclusive))
8744 (org-log-refile (if org-log-refile 'time nil))
8745 (entries (reverse org-agenda-bulk-marked-entries))
8746 redo-at-end
8747 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8748 (cond
8749 ((equal action ?p)
8750 (let ((org-agenda-persistent-marks
8751 (not org-agenda-persistent-marks)))
8752 (org-agenda-bulk-action)
8753 (throw 'exit nil)))
8755 ((equal action ?$)
8756 (setq cmd '(org-agenda-archive)))
8758 ((equal action ?A)
8759 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8761 ((member action '(?r ?w))
8762 (setq rfloc (org-refile-get-location
8763 "Refile to"
8764 (marker-buffer (car org-agenda-bulk-marked-entries))
8765 org-refile-allow-creating-parent-nodes))
8766 (if (nth 3 rfloc)
8767 (setcar (nthcdr 3 rfloc)
8768 (move-marker (make-marker) (nth 3 rfloc)
8769 (or (get-file-buffer (nth 1 rfloc))
8770 (find-buffer-visiting (nth 1 rfloc))
8771 (error "This should not happen")))))
8773 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8774 redo-at-end t))
8776 ((equal action ?t)
8777 (setq state (org-icompleting-read
8778 "Todo state: "
8779 (with-current-buffer (marker-buffer (car entries))
8780 (mapcar 'list org-todo-keywords-1))))
8781 (setq cmd `(let ((org-inhibit-blocking t)
8782 (org-inhibit-logging 'note))
8783 (org-agenda-todo ,state))))
8785 ((memq action '(?- ?+))
8786 (setq tag (org-icompleting-read
8787 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8788 (with-current-buffer (marker-buffer (car entries))
8789 (delq nil
8790 (mapcar (lambda (x)
8791 (if (stringp (car x)) x)) org-tag-alist)))))
8792 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8794 ((memq action '(?s ?d))
8795 (let* ((date (unless arg
8796 (org-read-date
8797 nil nil nil
8798 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8799 (ans (if arg nil org-read-date-final-answer))
8800 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8801 (setq cmd `(let* ((bound (fboundp 'read-string))
8802 (old (and bound (symbol-function 'read-string))))
8803 (unwind-protect
8804 (progn
8805 (fset 'read-string (lambda (&rest ignore) ,ans))
8806 (eval '(,c1 arg)))
8807 (if bound
8808 (fset 'read-string old)
8809 (fmakunbound 'read-string)))))))
8811 ((equal action ?S)
8812 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8813 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8814 (let ((days (read-number
8815 (format "Scatter tasks across how many %sdays: "
8816 (if arg "week" "")) 7)))
8817 (setq cmd
8818 `(let ((distance (1+ (random ,days))))
8819 (if arg
8820 (let ((dist distance)
8821 (day-of-week
8822 (calendar-day-of-week
8823 (calendar-gregorian-from-absolute (org-today)))))
8824 (dotimes (i (1+ dist))
8825 (while (member day-of-week org-agenda-weekend-days)
8826 (incf distance)
8827 (incf day-of-week)
8828 (if (= day-of-week 7)
8829 (setq day-of-week 0)))
8830 (incf day-of-week)
8831 (if (= day-of-week 7)
8832 (setq day-of-week 0)))))
8833 ;; silently fail when try to replan a sexp entry
8834 (condition-case nil
8835 (let* ((date (calendar-gregorian-from-absolute
8836 (+ (org-today) distance)))
8837 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8838 (nth 2 date))))
8839 (org-agenda-schedule nil time))
8840 (error nil)))))))
8842 ((assoc action org-agenda-bulk-custom-functions)
8843 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8844 redo-at-end t))
8846 ((equal action ?f)
8847 (setq cmd (list (intern
8848 (org-icompleting-read "Function: "
8849 obarray 'fboundp t nil nil)))))
8851 (t (error "Invalid bulk action")))
8853 ;; Sort the markers, to make sure that parents are handled before children
8854 (setq entries (sort entries
8855 (lambda (a b)
8856 (cond
8857 ((equal (marker-buffer a) (marker-buffer b))
8858 (< (marker-position a) (marker-position b)))
8860 (string< (buffer-name (marker-buffer a))
8861 (buffer-name (marker-buffer b))))))))
8863 ;; Now loop over all markers and apply cmd
8864 (while (setq e (pop entries))
8865 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8866 (if (not pos)
8867 (progn (message "Skipping removed entry at %s" e)
8868 (setq cntskip (1+ cntskip)))
8869 (goto-char pos)
8870 (let (org-loop-over-headlines-in-active-region)
8871 (eval cmd))
8872 (when (not org-agenda-persistent-marks)
8873 (setq org-agenda-bulk-marked-entries
8874 (delete e org-agenda-bulk-marked-entries)))
8875 (setq cnt (1+ cnt))))
8876 (when (not org-agenda-persistent-marks)
8877 (org-agenda-bulk-remove-all-marks))
8878 (when redo-at-end (org-agenda-redo))
8879 (message "Acted on %d entries%s%s"
8881 (if (= cntskip 0)
8883 (format ", skipped %d (disappeared before their turn)"
8884 cntskip))
8885 (if (not org-agenda-persistent-marks)
8886 "" " (kept marked)"))))))
8888 ;;; Flagging notes
8890 (defun org-agenda-show-the-flagging-note ()
8891 "Display the flagging note in the other window.
8892 When called a second time in direct sequence, offer to remove the FLAGGING
8893 tag and (if present) the flagging note."
8894 (interactive)
8895 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8896 (win (selected-window))
8897 note heading newhead)
8898 (unless hdmarker
8899 (error "No linked entry at point"))
8900 (if (and (eq this-command last-command)
8901 (y-or-n-p "Unflag and remove any flagging note? "))
8902 (progn
8903 (org-agenda-remove-flag hdmarker)
8904 (let ((win (get-buffer-window "*Flagging Note*")))
8905 (and win (delete-window win)))
8906 (message "Entry unflagged"))
8907 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8908 (unless note
8909 (error "No flagging note"))
8910 (org-kill-new note)
8911 (org-switch-to-buffer-other-window "*Flagging Note*")
8912 (erase-buffer)
8913 (insert note)
8914 (goto-char (point-min))
8915 (while (re-search-forward "\\\\n" nil t)
8916 (replace-match "\n" t t))
8917 (goto-char (point-min))
8918 (select-window win)
8919 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8921 (defun org-agenda-remove-flag (marker)
8922 "Remove the FLAGGED tag and any flagging note in the entry."
8923 (let (newhead)
8924 (org-with-point-at marker
8925 (org-toggle-tag "FLAGGED" 'off)
8926 (org-entry-delete nil "THEFLAGGINGNOTE")
8927 (setq newhead (org-get-heading)))
8928 (org-agenda-change-all-lines newhead marker)
8929 (message "Entry unflagged")))
8931 (defun org-agenda-get-any-marker (&optional pos)
8932 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8933 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8935 ;;; Appointment reminders
8937 (defvar appt-time-msg-list)
8939 ;;;###autoload
8940 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8941 "Activate appointments found in `org-agenda-files'.
8942 With a \\[universal-argument] prefix, refresh the list of
8943 appointments.
8945 If FILTER is t, interactively prompt the user for a regular
8946 expression, and filter out entries that don't match it.
8948 If FILTER is a string, use this string as a regular expression
8949 for filtering entries out.
8951 If FILTER is a function, filter out entries against which
8952 calling the function returns nil. This function takes one
8953 argument: an entry from `org-agenda-get-day-entries'.
8955 FILTER can also be an alist with the car of each cell being
8956 either 'headline or 'category. For example:
8958 '((headline \"IMPORTANT\")
8959 (category \"Work\"))
8961 will only add headlines containing IMPORTANT or headlines
8962 belonging to the \"Work\" category.
8964 ARGS are symbols indicating what kind of entries to consider.
8965 By default `org-agenda-to-appt' will use :deadline, :scheduled
8966 and :timestamp entries. See the docstring of `org-diary' for
8967 details and examples."
8968 (interactive "P")
8969 (if refresh (setq appt-time-msg-list nil))
8970 (if (eq filter t)
8971 (setq filter (read-from-minibuffer "Regexp filter: ")))
8972 (let* ((cnt 0) ; count added events
8973 (scope (or args '(:deadline :scheduled :timestamp)))
8974 (org-agenda-new-buffers nil)
8975 (org-deadline-warning-days 0)
8976 ;; Do not use `org-today' here because appt only takes
8977 ;; time and without date as argument, so it may pass wrong
8978 ;; information otherwise
8979 (today (org-date-to-gregorian
8980 (time-to-days (current-time))))
8981 (org-agenda-restrict nil)
8982 (files (org-agenda-files 'unrestricted)) entries file
8983 (org-agenda-buffer nil))
8984 ;; Get all entries which may contain an appt
8985 (org-prepare-agenda-buffers files)
8986 (while (setq file (pop files))
8987 (setq entries
8988 (delq nil
8989 (append entries
8990 (apply 'org-agenda-get-day-entries
8991 file today scope)))))
8992 ;; Map thru entries and find if we should filter them out
8993 (mapc
8994 (lambda(x)
8995 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8996 (cat (get-text-property 1 'org-category x))
8997 (tod (get-text-property 1 'time-of-day x))
8998 (ok (or (null filter)
8999 (and (stringp filter) (string-match filter evt))
9000 (and (functionp filter) (funcall filter x))
9001 (and (listp filter)
9002 (let ((cat-filter (cadr (assoc 'category filter)))
9003 (evt-filter (cadr (assoc 'headline filter))))
9004 (or (and (stringp cat-filter)
9005 (string-match cat-filter cat))
9006 (and (stringp evt-filter)
9007 (string-match evt-filter evt))))))))
9008 ;; FIXME: Shall we remove text-properties for the appt text?
9009 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
9010 (when (and ok tod)
9011 (setq tod (concat "00" (number-to-string tod))
9012 tod (when (string-match
9013 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
9014 (concat (match-string 1 tod) ":"
9015 (match-string 2 tod))))
9016 (appt-add tod evt)
9017 (setq cnt (1+ cnt))))) entries)
9018 (org-release-buffers org-agenda-new-buffers)
9019 (if (eq cnt 0)
9020 (message "No event to add")
9021 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
9023 (defun org-agenda-todayp (date)
9024 "Does DATE mean today, when considering `org-extend-today-until'?"
9025 (let ((today (org-today))
9026 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
9027 date)))
9028 (eq date today)))
9030 (defun org-agenda-todo-yesterday (&optional arg)
9031 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
9032 (interactive "P")
9033 (let* ((hour (third (decode-time
9034 (org-current-time))))
9035 (org-extend-today-until (1+ hour)))
9036 (org-agenda-todo arg)))
9038 (provide 'org-agenda)
9040 ;;; org-agenda.el ends here