org-agenda.el: Fix bug in `org-agenda-list'
[org-mode.git] / lisp / org-agenda.el
blob4479e827b2eea45d38d6f5b39a1dc78110863d0d
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 (require 'org-macs)
50 (eval-when-compile
51 (require 'cl))
53 (declare-function diary-add-to-list "diary-lib"
54 (date string specifier &optional marker globcolor literal))
55 (declare-function calendar-absolute-from-iso "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-pop-to-buffer-same-window "org-compat"
83 (&optional buffer-or-name norecord label))
84 (declare-function org-agenda-columns "org-colview" ())
85 (declare-function org-add-archive-files "org-archive" (files))
87 (defvar calendar-mode-map)
88 (defvar org-clock-current-task) ; defined in org-clock.el
89 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
90 (defvar org-habit-show-habits)
91 (defvar org-habit-show-habits-only-for-today)
92 (defvar org-habit-show-all-today nil)
94 ;; Defined somewhere in this file, but used before definition.
95 (defvar org-agenda-buffer-name)
96 (defvar org-agenda-overriding-header)
97 (defvar org-agenda-title-append nil)
98 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
99 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
100 (defvar org-agenda-undo-list)
101 (defvar org-agenda-pending-undo-list)
102 (defvar original-date) ; dynamically scoped, calendar.el does scope this
104 (defcustom org-agenda-confirm-kill 1
105 "When set, remote killing from the agenda buffer needs confirmation.
106 When t, a confirmation is always needed. When a number N, confirmation is
107 only needed when the text to be killed contains more than N non-white lines."
108 :group 'org-agenda
109 :type '(choice
110 (const :tag "Never" nil)
111 (const :tag "Always" t)
112 (integer :tag "When more than N lines")))
114 (defcustom org-agenda-compact-blocks nil
115 "Non-nil means make the block agenda more compact.
116 This is done globally by leaving out lines like the agenda span
117 name and week number or the separator lines."
118 :group 'org-agenda
119 :type 'boolean)
121 (defcustom org-agenda-block-separator ?=
122 "The separator between blocks in the agenda.
123 If this is a string, it will be used as the separator, with a newline added.
124 If it is a character, it will be repeated to fill the window width.
125 If nil the separator is disabled. In `org-agenda-custom-commands' this
126 addresses the separator between the current and the previous block."
127 :group 'org-agenda
128 :type '(choice
129 (const :tag "Disabled" nil)
130 (character)
131 (string)))
133 (defgroup org-agenda-export nil
134 "Options concerning exporting agenda views in Org-mode."
135 :tag "Org Agenda Export"
136 :group 'org-agenda)
138 (defcustom org-agenda-with-colors t
139 "Non-nil means use colors in agenda views."
140 :group 'org-agenda-export
141 :type 'boolean)
143 (defcustom org-agenda-exporter-settings nil
144 "Alist of variable/value pairs that should be active during agenda export.
145 This is a good place to set options for ps-print and for htmlize.
146 Note that the way this is implemented, the values will be evaluated
147 before assigned to the variables. So make sure to quote values you do
148 *not* want evaluated, for example
150 (setq org-agenda-exporter-settings
151 '((ps-print-color-p 'black-white)))"
152 :group 'org-agenda-export
153 :type '(repeat
154 (list
155 (variable)
156 (sexp :tag "Value"))))
158 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
159 "Hook run in temporary buffer before writing it to an export file.
160 A useful function is `org-agenda-add-entry-text'."
161 :group 'org-agenda-export
162 :type 'hook
163 :options '(org-agenda-add-entry-text))
165 (defcustom org-agenda-add-entry-text-maxlines 0
166 "Maximum number of entry text lines to be added to agenda.
167 This is only relevant when `org-agenda-add-entry-text' is part of
168 `org-agenda-before-write-hook', which it is by default.
169 When this is 0, nothing will happen. When it is greater than 0, it
170 specifies the maximum number of lines that will be added for each entry
171 that is listed in the agenda view.
173 Note that this variable is not used during display, only when exporting
174 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
175 and `org-agenda-entry-text-maxlines'."
176 :group 'org-agenda
177 :type 'integer)
179 (defcustom org-agenda-add-entry-text-descriptive-links t
180 "Non-nil means export org-links as descriptive links in agenda added text.
181 This variable applies to the text added to the agenda when
182 `org-agenda-add-entry-text-maxlines' is larger than 0.
183 When this variable nil, the URL will (also) be shown."
184 :group 'org-agenda
185 :type 'boolean)
187 (defcustom org-agenda-export-html-style ""
188 "The style specification for exported HTML Agenda files.
189 If this variable contains a string, it will replace the default <style>
190 section as produced by `htmlize'.
191 Since there are different ways of setting style information, this variable
192 needs to contain the full HTML structure to provide a style, including the
193 surrounding HTML tags. The style specifications should include definitions
194 the fonts used by the agenda, here is an example:
196 <style type=\"text/css\">
197 p { font-weight: normal; color: gray; }
198 .org-agenda-structure {
199 font-size: 110%;
200 color: #003399;
201 font-weight: 600;
203 .org-todo {
204 color: #cc6666;
205 font-weight: bold;
207 .org-agenda-done {
208 color: #339933;
210 .org-done {
211 color: #339933;
213 .title { text-align: center; }
214 .todo, .deadline { color: red; }
215 .done { color: green; }
216 </style>
218 or, if you want to keep the style in a file,
220 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
222 As the value of this option simply gets inserted into the HTML <head> header,
223 you can \"misuse\" it to also add other text to the header. However,
224 <style>...</style> is required, if not present the variable will be ignored."
225 :group 'org-agenda-export
226 :group 'org-export-html
227 :type 'string)
229 (defcustom org-agenda-persistent-filter nil
230 "When set, keep filters from one agenda view to the next."
231 :group 'org-agenda
232 :type 'boolean)
234 (defgroup org-agenda-custom-commands nil
235 "Options concerning agenda views in Org-mode."
236 :tag "Org Agenda Custom Commands"
237 :group 'org-agenda)
239 (defconst org-sorting-choice
240 '(choice
241 (const time-up) (const time-down)
242 (const category-keep) (const category-up) (const category-down)
243 (const tag-down) (const tag-up)
244 (const priority-up) (const priority-down)
245 (const todo-state-up) (const todo-state-down)
246 (const effort-up) (const effort-down)
247 (const habit-up) (const habit-down)
248 (const alpha-up) (const alpha-down)
249 (const user-defined-up) (const user-defined-down))
250 "Sorting choices.")
252 ;; Keep custom values for `org-agenda-filter-preset' compatible with
253 ;; the new variable `org-agenda-tag-filter-preset'.
254 (if (fboundp 'defvaralias)
255 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
256 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter))
258 (defconst org-agenda-custom-commands-local-options
259 `(repeat :tag "Local settings for this command. Remember to quote values"
260 (choice :tag "Setting"
261 (list :tag "Heading for this block"
262 (const org-agenda-overriding-header)
263 (string :tag "Headline"))
264 (list :tag "Files to be searched"
265 (const org-agenda-files)
266 (list
267 (const :format "" quote)
268 (repeat (file))))
269 (list :tag "Sorting strategy"
270 (const org-agenda-sorting-strategy)
271 (list
272 (const :format "" quote)
273 (repeat
274 ,org-sorting-choice)))
275 (list :tag "Prefix format"
276 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
277 (string))
278 (list :tag "Number of days in agenda"
279 (const org-agenda-span)
280 (choice (const :tag "Day" 'day)
281 (const :tag "Week" 'week)
282 (const :tag "Month" 'month)
283 (const :tag "Year" 'year)
284 (integer :tag "Custom")))
285 (list :tag "Fixed starting date"
286 (const org-agenda-start-day)
287 (string :value "2007-11-01"))
288 (list :tag "Start on day of week"
289 (const org-agenda-start-on-weekday)
290 (choice :value 1
291 (const :tag "Today" nil)
292 (integer :tag "Weekday No.")))
293 (list :tag "Include data from diary"
294 (const org-agenda-include-diary)
295 (boolean))
296 (list :tag "Deadline Warning days"
297 (const org-deadline-warning-days)
298 (integer :value 1))
299 (list :tag "Category filter preset"
300 (const org-agenda-category-filter-preset)
301 (list
302 (const :format "" quote)
303 (repeat
304 (string :tag "+category or -category"))))
305 (list :tag "Tags filter preset"
306 (const org-agenda-tag-filter-preset)
307 (list
308 (const :format "" quote)
309 (repeat
310 (string :tag "+tag or -tag"))))
311 (list :tag "Set daily/weekly entry types"
312 (const org-agenda-entry-types)
313 (list
314 (const :format "" quote)
315 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
316 (const :deadline)
317 (const :scheduled)
318 (const :timestamp)
319 (const :sexp))))
320 (list :tag "Standard skipping condition"
321 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
322 (const org-agenda-skip-function)
323 (list
324 (const :format "" quote)
325 (list
326 (choice
327 :tag "Skipping range"
328 (const :tag "Skip entry" org-agenda-skip-entry-if)
329 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
330 (repeat :inline t :tag "Conditions for skipping"
331 (choice
332 :tag "Condition type"
333 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
334 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
335 (list :tag "TODO state is" :inline t
336 (const 'todo)
337 (choice
338 (const :tag "any not-done state" 'todo)
339 (const :tag "any done state" 'done)
340 (const :tag "any state" 'any)
341 (list :tag "Keyword list"
342 (const :format "" quote)
343 (repeat (string :tag "Keyword")))))
344 (list :tag "TODO state is not" :inline t
345 (const 'nottodo)
346 (choice
347 (const :tag "any not-done state" 'todo)
348 (const :tag "any done state" 'done)
349 (const :tag "any state" 'any)
350 (list :tag "Keyword list"
351 (const :format "" quote)
352 (repeat (string :tag "Keyword")))))
353 (const :tag "scheduled" 'scheduled)
354 (const :tag "not scheduled" 'notscheduled)
355 (const :tag "deadline" 'deadline)
356 (const :tag "no deadline" 'notdeadline)
357 (const :tag "timestamp" 'timestamp)
358 (const :tag "no timestamp" 'nottimestamp))))))
359 (list :tag "Non-standard skipping condition"
360 :value (org-agenda-skip-function)
361 (const org-agenda-skip-function)
362 (sexp :tag "Function or form (quoted!)"))
363 (list :tag "Any variable"
364 (variable :tag "Variable")
365 (sexp :tag "Value (sexp)"))))
366 "Selection of examples for agenda command settings.
367 This will be spliced into the custom type of
368 `org-agenda-custom-commands'.")
371 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
372 ((agenda "") (alltodo))))
373 "Custom commands for the agenda.
374 These commands will be offered on the splash screen displayed by the
375 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
377 (key desc type match settings files)
379 key The key (one or more characters as a string) to be associated
380 with the command.
381 desc A description of the command, when omitted or nil, a default
382 description is built using MATCH.
383 type The command type, any of the following symbols:
384 agenda The daily/weekly agenda.
385 todo Entries with a specific TODO keyword, in all agenda files.
386 search Entries containing search words entry or headline.
387 tags Tags/Property/TODO match in all agenda files.
388 tags-todo Tags/P/T match in all agenda files, TODO entries only.
389 todo-tree Sparse tree of specific TODO keyword in *current* file.
390 tags-tree Sparse tree with all tags matches in *current* file.
391 occur-tree Occur sparse tree for *current* file.
392 ... A user-defined function.
393 match What to search for:
394 - a single keyword for TODO keyword searches
395 - a tags match expression for tags searches
396 - a word search expression for text searches.
397 - a regular expression for occur searches
398 For all other commands, this should be the empty string.
399 settings A list of option settings, similar to that in a let form, so like
400 this: ((opt1 val1) (opt2 val2) ...). The values will be
401 evaluated at the moment of execution, so quote them when needed.
402 files A list of files file to write the produced agenda buffer to
403 with the command `org-store-agenda-views'.
404 If a file name ends in \".html\", an HTML version of the buffer
405 is written out. If it ends in \".ps\", a postscript version is
406 produced. Otherwise, only the plain text is written to the file.
408 You can also define a set of commands, to create a composite agenda buffer.
409 In this case, an entry looks like this:
411 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
413 where
415 desc A description string to be displayed in the dispatcher menu.
416 cmd An agenda command, similar to the above. However, tree commands
417 are not allowed, but instead you can get agenda and global todo list.
418 So valid commands for a set are:
419 (agenda \"\" settings)
420 (alltodo \"\" settings)
421 (stuck \"\" settings)
422 (todo \"match\" settings files)
423 (search \"match\" settings files)
424 (tags \"match\" settings files)
425 (tags-todo \"match\" settings files)
427 Each command can carry a list of options, and another set of options can be
428 given for the whole set of commands. Individual command options take
429 precedence over the general options.
431 When using several characters as key to a command, the first characters
432 are prefix commands. For the dispatcher to display useful information, you
433 should provide a description for the prefix, like
435 (setq org-agenda-custom-commands
436 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
437 (\"hl\" tags \"+HOME+Lisa\")
438 (\"hp\" tags \"+HOME+Peter\")
439 (\"hk\" tags \"+HOME+Kim\")))"
440 :group 'org-agenda-custom-commands
441 :type `(repeat
442 (choice :value ("x" "Describe command here" tags "" nil)
443 (list :tag "Single command"
444 (string :tag "Access Key(s) ")
445 (option (string :tag "Description"))
446 (choice
447 (const :tag "Agenda" agenda)
448 (const :tag "TODO list" alltodo)
449 (const :tag "Search words" search)
450 (const :tag "Stuck projects" stuck)
451 (const :tag "Tags/Property match (all agenda files)" tags)
452 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
453 (const :tag "TODO keyword search (all agenda files)" todo)
454 (const :tag "Tags sparse tree (current buffer)" tags-tree)
455 (const :tag "TODO keyword tree (current buffer)" todo-tree)
456 (const :tag "Occur tree (current buffer)" occur-tree)
457 (sexp :tag "Other, user-defined function"))
458 (string :tag "Match (only for some commands)")
459 ,org-agenda-custom-commands-local-options
460 (option (repeat :tag "Export" (file :tag "Export to"))))
461 (list :tag "Command series, all agenda files"
462 (string :tag "Access Key(s)")
463 (string :tag "Description ")
464 (repeat :tag "Component"
465 (choice
466 (list :tag "Agenda"
467 (const :format "" agenda)
468 (const :tag "" :format "" "")
469 ,org-agenda-custom-commands-local-options)
470 (list :tag "TODO list (all keywords)"
471 (const :format "" alltodo)
472 (const :tag "" :format "" "")
473 ,org-agenda-custom-commands-local-options)
474 (list :tag "Search words"
475 (const :format "" search)
476 (string :tag "Match")
477 ,org-agenda-custom-commands-local-options)
478 (list :tag "Stuck projects"
479 (const :format "" stuck)
480 (const :tag "" :format "" "")
481 ,org-agenda-custom-commands-local-options)
482 (list :tag "Tags search"
483 (const :format "" tags)
484 (string :tag "Match")
485 ,org-agenda-custom-commands-local-options)
486 (list :tag "Tags search, TODO entries only"
487 (const :format "" tags-todo)
488 (string :tag "Match")
489 ,org-agenda-custom-commands-local-options)
490 (list :tag "TODO keyword search"
491 (const :format "" todo)
492 (string :tag "Match")
493 ,org-agenda-custom-commands-local-options)
494 (list :tag "Other, user-defined function"
495 (symbol :tag "function")
496 (string :tag "Match")
497 ,org-agenda-custom-commands-local-options)))
499 (repeat :tag "Settings for entire command set"
500 (list (variable :tag "Any variable")
501 (sexp :tag "Value")))
502 (option (repeat :tag "Export" (file :tag "Export to"))))
503 (cons :tag "Prefix key documentation"
504 (string :tag "Access Key(s)")
505 (string :tag "Description ")))))
507 (defcustom org-agenda-query-register ?o
508 "The register holding the current query string.
509 The purpose of this is that if you construct a query string interactively,
510 you can then use it to define a custom command."
511 :group 'org-agenda-custom-commands
512 :type 'character)
514 (defcustom org-stuck-projects
515 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
516 "How to identify stuck projects.
517 This is a list of four items:
518 1. A tags/todo/property matcher string that is used to identify a project.
519 See the manual for a description of tag and property searches.
520 The entire tree below a headline matched by this is considered one project.
521 2. A list of TODO keywords identifying non-stuck projects.
522 If the project subtree contains any headline with one of these todo
523 keywords, the project is considered to be not stuck. If you specify
524 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
525 3. A list of tags identifying non-stuck projects.
526 If the project subtree contains any headline with one of these tags,
527 the project is considered to be not stuck. If you specify \"*\" as
528 a tag, any tag will mark the project unstuck. Note that this is about
529 the explicit presence of a tag somewhere in the subtree, inherited
530 tags to not count here. If inherited tags make a project not stuck,
531 use \"-TAG\" in the tags part of the matcher under (1.) above.
532 4. An arbitrary regular expression matching non-stuck projects.
534 If the project turns out to be not stuck, search continues also in the
535 subtree to see if any of the subtasks have project status.
537 See also the variable `org-tags-match-list-sublevels' which applies
538 to projects matched by this search as well.
540 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
541 or `C-c a #' to produce the list."
542 :group 'org-agenda-custom-commands
543 :type '(list
544 (string :tag "Tags/TODO match to identify a project")
545 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
546 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
547 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
549 (defcustom org-agenda-filter-effort-default-operator "<"
550 "The default operator for effort estimate filtering.
551 If you select an effort estimate limit without first pressing an operator,
552 this one will be used."
553 :group 'org-agenda-custom-commands
554 :type '(choice (const :tag "less or equal" "<")
555 (const :tag "greater or equal"">")
556 (const :tag "equal" "=")))
558 (defgroup org-agenda-skip nil
559 "Options concerning skipping parts of agenda files."
560 :tag "Org Agenda Skip"
561 :group 'org-agenda)
563 (defcustom org-agenda-skip-function-global nil
564 "Function to be called at each match during agenda construction.
565 If this function returns nil, the current match should not be skipped.
566 If the function decided to skip an agenda match, is must return the
567 buffer position from which the search should be continued.
568 This may also be a Lisp form, which will be evaluated.
570 This variable will be applied to every agenda match, including
571 tags/property searches and TODO lists. So try to make the test function
572 do its checking as efficiently as possible. To implement a skipping
573 condition just for specific agenda commands, use the variable
574 `org-agenda-skip-function' which can be set in the options section
575 of custom agenda commands."
576 :group 'org-agenda-skip
577 :type 'sexp)
579 (defgroup org-agenda-daily/weekly nil
580 "Options concerning the daily/weekly agenda."
581 :tag "Org Agenda Daily/Weekly"
582 :group 'org-agenda)
583 (defgroup org-agenda-todo-list nil
584 "Options concerning the global todo list agenda view."
585 :tag "Org Agenda Todo List"
586 :group 'org-agenda)
587 (defgroup org-agenda-match-view nil
588 "Options concerning the general tags/property/todo match agenda view."
589 :tag "Org Agenda Match View"
590 :group 'org-agenda)
591 (defgroup org-agenda-search-view nil
592 "Options concerning the general tags/property/todo match agenda view."
593 :tag "Org Agenda Match View"
594 :group 'org-agenda)
596 (defvar org-agenda-archives-mode nil
597 "Non-nil means the agenda will include archived items.
598 If this is the symbol `trees', trees in the selected agenda scope
599 that are marked with the ARCHIVE tag will be included anyway. When this is
600 t, also all archive files associated with the current selection of agenda
601 files will be included.")
603 (defcustom org-agenda-skip-comment-trees t
604 "Non-nil means skip trees that start with the COMMENT keyword.
605 When nil, these trees are also scanned by agenda commands."
606 :group 'org-agenda-skip
607 :type 'boolean)
609 (defcustom org-agenda-todo-list-sublevels t
610 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
611 When nil, the sublevels of a TODO entry are not checked, resulting in
612 potentially much shorter TODO lists."
613 :group 'org-agenda-skip
614 :group 'org-agenda-todo-list
615 :type 'boolean)
617 (defcustom org-agenda-todo-ignore-with-date nil
618 "Non-nil means don't show entries with a date in the global todo list.
619 You can use this if you prefer to mark mere appointments with a TODO keyword,
620 but don't want them to show up in the TODO list.
621 When this is set, it also covers deadlines and scheduled items, the settings
622 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
623 will be ignored.
624 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
625 :group 'org-agenda-skip
626 :group 'org-agenda-todo-list
627 :type 'boolean)
629 (defcustom org-agenda-todo-ignore-timestamp nil
630 "Non-nil means don't show entries with a timestamp.
631 This applies when creating the global todo list.
632 Valid values are:
634 past Don't show entries for today or in the past.
636 future Don't show entries with a timestamp in the future.
637 The idea behind this is that if it has a future
638 timestamp, you don't want to think about it until the
639 date.
641 all Don't show any entries with a timestamp in the global todo list.
642 The idea behind this is that by setting a timestamp, you
643 have already \"taken care\" of this item.
645 This variable can also have an integer as a value. If positive (N),
646 todos with a timestamp N or more days in the future will be ignored. If
647 negative (-N), todos with a timestamp N or more days in the past will be
648 ignored. If 0, todos with a timestamp either today or in the future will
649 be ignored. For example, a value of -1 will exclude todos with a
650 timestamp in the past (yesterday or earlier), while a value of 7 will
651 exclude todos with a timestamp a week or more in the future.
653 See also `org-agenda-todo-ignore-with-date'.
654 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
655 to make his option also apply to the tags-todo list."
656 :group 'org-agenda-skip
657 :group 'org-agenda-todo-list
658 :version "24.1"
659 :type '(choice
660 (const :tag "Ignore future timestamp todos" future)
661 (const :tag "Ignore past or present timestamp todos" past)
662 (const :tag "Ignore all timestamp todos" all)
663 (const :tag "Show timestamp todos" nil)
664 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
666 (defcustom org-agenda-todo-ignore-scheduled nil
667 "Non-nil means, ignore some scheduled TODO items when making TODO list.
668 This applies when creating the global todo list.
669 Valid values are:
671 past Don't show entries scheduled today or in the past.
673 future Don't show entries scheduled in the future.
674 The idea behind this is that by scheduling it, you don't want to
675 think about it until the scheduled date.
677 all Don't show any scheduled entries in the global todo list.
678 The idea behind this is that by scheduling it, you have already
679 \"taken care\" of this item.
681 t Same as `all', for backward compatibility.
683 This variable can also have an integer as a value. See
684 `org-agenda-todo-ignore-timestamp' for more details.
686 See also `org-agenda-todo-ignore-with-date'.
687 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
688 to make his option also apply to the tags-todo list."
689 :group 'org-agenda-skip
690 :group 'org-agenda-todo-list
691 :type '(choice
692 (const :tag "Ignore future-scheduled todos" future)
693 (const :tag "Ignore past- or present-scheduled todos" past)
694 (const :tag "Ignore all scheduled todos" all)
695 (const :tag "Ignore all scheduled todos (compatibility)" t)
696 (const :tag "Show scheduled todos" nil)
697 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
699 (defcustom org-agenda-todo-ignore-deadlines nil
700 "Non-nil means ignore some deadlined TODO items when making TODO list.
701 There are different motivations for using different values, please think
702 carefully when configuring this variable.
704 This applies when creating the global todo list.
705 Valid values are:
707 near Don't show near deadline entries. A deadline is near when it is
708 closer than `org-deadline-warning-days' days. The idea behind this
709 is that such items will appear in the agenda anyway.
711 far Don't show TODO entries where a deadline has been defined, but
712 the deadline is not near. This is useful if you don't want to
713 use the todo list to figure out what to do now.
715 past Don't show entries with a deadline timestamp for today or in the past.
717 future Don't show entries with a deadline timestamp in the future, not even
718 when they become `near' ones. Use it with caution.
720 all Ignore all TODO entries that do have a deadline.
722 t Same as `near', for backward compatibility.
724 This variable can also have an integer as a value. See
725 `org-agenda-todo-ignore-timestamp' for more details.
727 See also `org-agenda-todo-ignore-with-date'.
728 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
729 to make his option also apply to the tags-todo list."
730 :group 'org-agenda-skip
731 :group 'org-agenda-todo-list
732 :type '(choice
733 (const :tag "Ignore near deadlines" near)
734 (const :tag "Ignore near deadlines (compatibility)" t)
735 (const :tag "Ignore far deadlines" far)
736 (const :tag "Ignore all TODOs with a deadlines" all)
737 (const :tag "Show all TODOs, even if they have a deadline" nil)
738 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
740 (defcustom org-agenda-tags-todo-honor-ignore-options nil
741 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
742 The variables
743 `org-agenda-todo-ignore-with-date',
744 `org-agenda-todo-ignore-timestamp',
745 `org-agenda-todo-ignore-scheduled',
746 `org-agenda-todo-ignore-deadlines'
747 make the global TODO list skip entries that have time stamps of certain
748 kinds. If this option is set, the same options will also apply for the
749 tags-todo search, which is the general tags/property matcher
750 restricted to unfinished TODO entries only."
751 :group 'org-agenda-skip
752 :group 'org-agenda-todo-list
753 :group 'org-agenda-match-view
754 :type 'boolean)
756 (defcustom org-agenda-skip-scheduled-if-done nil
757 "Non-nil means don't show scheduled items in agenda when they are done.
758 This is relevant for the daily/weekly agenda, not for the TODO list. And
759 it applies only to the actual date of the scheduling. Warnings about
760 an item with a past scheduling dates are always turned off when the item
761 is DONE."
762 :group 'org-agenda-skip
763 :group 'org-agenda-daily/weekly
764 :type 'boolean)
766 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
767 "Non-nil means skip scheduling line if same entry shows because of deadline.
768 In the agenda of today, an entry can show up multiple times because
769 it is both scheduled and has a nearby deadline, and maybe a plain time
770 stamp as well.
771 When this variable is t, then only the deadline is shown and the fact that
772 the entry is scheduled today or was scheduled previously is not shown.
773 When this variable is nil, the entry will be shown several times. When
774 the variable is the symbol `not-today', then skip scheduled previously,
775 but not scheduled today."
776 :group 'org-agenda-skip
777 :group 'org-agenda-daily/weekly
778 :type '(choice
779 (const :tag "Never" nil)
780 (const :tag "Always" t)
781 (const :tag "Not when scheduled today" not-today)))
783 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
784 "Non-nil means skip timestamp line if same entry shows because of deadline.
785 In the agenda of today, an entry can show up multiple times
786 because it has both a plain timestamp and has a nearby deadline.
787 When this variable is t, then only the deadline is shown and the
788 fact that the entry has a timestamp for or including today is not
789 shown. When this variable is nil, the entry will be shown
790 several times."
791 :group 'org-agenda-skip
792 :group 'org-agenda-daily/weekly
793 :version "24.1"
794 :type '(choice
795 (const :tag "Never" nil)
796 (const :tag "Always" t)))
798 (defcustom org-agenda-skip-deadline-if-done nil
799 "Non-nil means don't show deadlines when the corresponding item is done.
800 When nil, the deadline is still shown and should give you a happy feeling.
801 This is relevant for the daily/weekly agenda. And it applied only to the
802 actually date of the deadline. Warnings about approaching and past-due
803 deadlines are always turned off when the item is DONE."
804 :group 'org-agenda-skip
805 :group 'org-agenda-daily/weekly
806 :type 'boolean)
808 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
809 "Non-nil means skip deadline prewarning when entry is also scheduled.
810 This will apply on all days where a prewarning for the deadline would
811 be shown, but not at the day when the entry is actually due. On that day,
812 the deadline will be shown anyway.
813 This variable may be set to nil, t, or a number which will then give
814 the number of days before the actual deadline when the prewarnings
815 should resume.
816 This can be used in a workflow where the first showing of the deadline will
817 trigger you to schedule it, and then you don't want to be reminded of it
818 because you will take care of it on the day when scheduled."
819 :group 'org-agenda-skip
820 :group 'org-agenda-daily/weekly
821 :version "24.1"
822 :type '(choice
823 (const :tag "Alwas show prewarning" nil)
824 (const :tag "Remove prewarning if entry is scheduled" t)
825 (integer :tag "Restart prewarning N days before deadline")))
827 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
828 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
829 When non-nil, after the search for timestamps has matched once in an
830 entry, the rest of the entry will not be searched."
831 :group 'org-agenda-skip
832 :type 'boolean)
834 (defcustom org-agenda-skip-timestamp-if-done nil
835 "Non-nil means don't select item by timestamp or -range if it is DONE."
836 :group 'org-agenda-skip
837 :group 'org-agenda-daily/weekly
838 :type 'boolean)
840 (defcustom org-agenda-dim-blocked-tasks t
841 "Non-nil means dim blocked tasks in the agenda display.
842 This causes some overhead during agenda construction, but if you
843 have turned on `org-enforce-todo-dependencies',
844 `org-enforce-todo-checkbox-dependencies', or any other blocking
845 mechanism, this will create useful feedback in the agenda.
847 Instead of t, this variable can also have the value `invisible'.
848 Then blocked tasks will be invisible and only become visible when
849 they become unblocked. An exemption to this behavior is when a task is
850 blocked because of unchecked checkboxes below it. Since checkboxes do
851 not show up in the agenda views, making this task invisible you remove any
852 trace from agenda views that there is something to do. Therefore, a task
853 that is blocked because of checkboxes will never be made invisible, it
854 will only be dimmed."
855 :group 'org-agenda-daily/weekly
856 :group 'org-agenda-todo-list
857 :type '(choice
858 (const :tag "Do not dim" nil)
859 (const :tag "Dim to a gray face" t)
860 (const :tag "Make invisible" invisible)))
862 (defcustom org-timeline-show-empty-dates 3
863 "Non-nil means `org-timeline' also shows dates without an entry.
864 When nil, only the days which actually have entries are shown.
865 When t, all days between the first and the last date are shown.
866 When an integer, show also empty dates, but if there is a gap of more than
867 N days, just insert a special line indicating the size of the gap."
868 :group 'org-agenda-skip
869 :type '(choice
870 (const :tag "None" nil)
871 (const :tag "All" t)
872 (integer :tag "at most")))
874 (defgroup org-agenda-startup nil
875 "Options concerning initial settings in the Agenda in Org Mode."
876 :tag "Org Agenda Startup"
877 :group 'org-agenda)
879 (defcustom org-agenda-menu-show-matcher t
880 "Non-nil means show the match string in the agenda dispatcher menu.
881 When nil, the matcher string is not shown, but is put into the help-echo
882 property so than moving the mouse over the command shows it.
883 Setting it to nil is good if matcher strings are very long and/or if
884 you want to use two-columns display (see `org-agenda-menu-two-columns')."
885 :group 'org-agenda
886 :version "24.1"
887 :type 'boolean)
889 (make-obsolete-variable 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
891 (defcustom org-agenda-menu-two-columns nil
892 "Non-nil means, use two columns to show custom commands in the dispatcher.
893 If you use this, you probably want to set `org-agenda-menu-show-matcher'
894 to nil."
895 :group 'org-agenda
896 :version "24.1"
897 :type 'boolean)
899 (defcustom org-finalize-agenda-hook nil
900 "Hook run just before displaying an agenda buffer."
901 :group 'org-agenda-startup
902 :type 'hook)
904 (defcustom org-agenda-mouse-1-follows-link nil
905 "Non-nil means mouse-1 on a link will follow the link in the agenda.
906 A longer mouse click will still set point. Does not work on XEmacs.
907 Needs to be set before org.el is loaded."
908 :group 'org-agenda-startup
909 :type 'boolean)
911 (defcustom org-agenda-start-with-follow-mode nil
912 "The initial value of follow mode in a newly created agenda window."
913 :group 'org-agenda-startup
914 :type 'boolean)
916 (defcustom org-agenda-follow-indirect nil
917 "Non-nil means `org-agenda-follow-mode' displays only the
918 current item's tree, in an indirect buffer."
919 :group 'org-agenda
920 :version "24.1"
921 :type 'boolean)
923 (defcustom org-agenda-show-outline-path t
924 "Non-nil means show outline path in echo area after line motion."
925 :group 'org-agenda-startup
926 :type 'boolean)
928 (defcustom org-agenda-start-with-entry-text-mode nil
929 "The initial value of entry-text-mode in a newly created agenda window."
930 :group 'org-agenda-startup
931 :type 'boolean)
933 (defcustom org-agenda-entry-text-maxlines 5
934 "Number of text lines to be added when `E' is pressed in the agenda.
936 Note that this variable only used during agenda display. Add add entry text
937 when exporting the agenda, configure the variable
938 `org-agenda-add-entry-ext-maxlines'."
939 :group 'org-agenda
940 :type 'integer)
942 (defcustom org-agenda-entry-text-exclude-regexps nil
943 "List of regular expressions to clean up entry text.
944 The complete matches of all regular expressions in this list will be
945 removed from entry text before it is shown in the agenda."
946 :group 'org-agenda
947 :type '(repeat (regexp)))
949 (defvar org-agenda-entry-text-cleanup-hook nil
950 "Hook that is run after basic cleanup of entry text to be shown in agenda.
951 This cleanup is done in a temporary buffer, so the function may inspect and
952 change the entire buffer.
953 Some default stuff like drawers and scheduling/deadline dates will already
954 have been removed when this is called, as will any matches for regular
955 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
957 (defvar org-agenda-include-inactive-timestamps nil
958 "Non-nil means include inactive time stamps in agenda and timeline.
959 Dynamically scoped.")
961 (defgroup org-agenda-windows nil
962 "Options concerning the windows used by the Agenda in Org Mode."
963 :tag "Org Agenda Windows"
964 :group 'org-agenda)
966 (defcustom org-agenda-window-setup 'reorganize-frame
967 "How the agenda buffer should be displayed.
968 Possible values for this option are:
970 current-window Show agenda in the current window, keeping all other windows.
971 other-window Use `switch-to-buffer-other-window' to display agenda.
972 reorganize-frame Show only two windows on the current frame, the current
973 window and the agenda.
974 other-frame Use `switch-to-buffer-other-frame' to display agenda.
975 Also, when exiting the agenda, kill that frame.
976 See also the variable `org-agenda-restore-windows-after-quit'."
977 :group 'org-agenda-windows
978 :type '(choice
979 (const current-window)
980 (const other-frame)
981 (const other-window)
982 (const reorganize-frame)))
984 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
985 "The min and max height of the agenda window as a fraction of frame height.
986 The value of the variable is a cons cell with two numbers between 0 and 1.
987 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
988 :group 'org-agenda-windows
989 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
991 (defcustom org-agenda-restore-windows-after-quit nil
992 "Non-nil means restore window configuration upon exiting agenda.
993 Before the window configuration is changed for displaying the agenda,
994 the current status is recorded. When the agenda is exited with
995 `q' or `x' and this option is set, the old state is restored. If
996 `org-agenda-window-setup' is `other-frame', the value of this
997 option will be ignored."
998 :group 'org-agenda-windows
999 :type 'boolean)
1001 (defcustom org-agenda-ndays nil
1002 "Number of days to include in overview display.
1003 Should be 1 or 7.
1004 Obsolete, see `org-agenda-span'."
1005 :group 'org-agenda-daily/weekly
1006 :type 'integer)
1008 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1010 (defcustom org-agenda-span 'week
1011 "Number of days to include in overview display.
1012 Can be day, week, month, year, or any number of days.
1013 Custom commands can set this variable in the options section."
1014 :group 'org-agenda-daily/weekly
1015 :type '(choice (const :tag "Day" day)
1016 (const :tag "Week" week)
1017 (const :tag "Month" month)
1018 (const :tag "Year" year)
1019 (integer :tag "Custom")))
1021 (defcustom org-agenda-start-on-weekday 1
1022 "Non-nil means start the overview always on the specified weekday.
1023 0 denotes Sunday, 1 denotes Monday etc.
1024 When nil, always start on the current day.
1025 Custom commands can set this variable in the options section."
1026 :group 'org-agenda-daily/weekly
1027 :type '(choice (const :tag "Today" nil)
1028 (integer :tag "Weekday No.")))
1030 (defcustom org-agenda-show-all-dates t
1031 "Non-nil means `org-agenda' shows every day in the selected range.
1032 When nil, only the days which actually have entries are shown."
1033 :group 'org-agenda-daily/weekly
1034 :type 'boolean)
1036 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1037 "Format string for displaying dates in the agenda.
1038 Used by the daily/weekly agenda and by the timeline. This should be
1039 a format string understood by `format-time-string', or a function returning
1040 the formatted date as a string. The function must take a single argument,
1041 a calendar-style date list like (month day year)."
1042 :group 'org-agenda-daily/weekly
1043 :type '(choice
1044 (string :tag "Format string")
1045 (function :tag "Function")))
1047 (defun org-agenda-format-date-aligned (date)
1048 "Format a date string for display in the daily/weekly agenda, or timeline.
1049 This function makes sure that dates are aligned for easy reading."
1050 (require 'cal-iso)
1051 (let* ((dayname (calendar-day-name date))
1052 (day (cadr date))
1053 (day-of-week (calendar-day-of-week date))
1054 (month (car date))
1055 (monthname (calendar-month-name month))
1056 (year (nth 2 date))
1057 (iso-week (org-days-to-iso-week
1058 (calendar-absolute-from-gregorian date)))
1059 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1060 (1- year))
1061 ((and (= month 12) (<= iso-week 1))
1062 (1+ year))
1063 (t year)))
1064 (weekstring (if (= day-of-week 1)
1065 (format " W%02d" iso-week)
1066 "")))
1067 (format "%-10s %2d %s %4d%s"
1068 dayname day monthname year weekstring)))
1070 (defcustom org-agenda-time-leading-zero nil
1071 "Non-nil means use leading zero for military times in agenda.
1072 For example, 9:30am would become 09:30 rather than 9:30."
1073 :group 'org-agenda-daily/weekly
1074 :version "24.1"
1075 :type 'boolean)
1077 (defcustom org-agenda-timegrid-use-ampm nil
1078 "When set, show AM/PM style timestamps on the timegrid."
1079 :group 'org-agenda
1080 :version "24.1"
1081 :type 'boolean)
1083 (defun org-agenda-time-of-day-to-ampm (time)
1084 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1085 (let* ((hour-number (string-to-number (substring time 0 -3)))
1086 (minute (substring time -2))
1087 (ampm "am"))
1088 (cond
1089 ((equal hour-number 12)
1090 (setq ampm "pm"))
1091 ((> hour-number 12)
1092 (setq ampm "pm")
1093 (setq hour-number (- hour-number 12))))
1094 (concat
1095 (if org-agenda-time-leading-zero
1096 (format "%02d" hour-number)
1097 (format "%02s" (number-to-string hour-number)))
1098 ":" minute ampm)))
1100 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1101 "Conditionally convert TIME to AM/PM format
1102 based on `org-agenda-timegrid-use-ampm'"
1103 (if org-agenda-timegrid-use-ampm
1104 (org-agenda-time-of-day-to-ampm time)
1105 time))
1107 (defcustom org-agenda-weekend-days '(6 0)
1108 "Which days are weekend?
1109 These days get the special face `org-agenda-date-weekend' in the agenda
1110 and timeline buffers."
1111 :group 'org-agenda-daily/weekly
1112 :type '(set :greedy t
1113 (const :tag "Monday" 1)
1114 (const :tag "Tuesday" 2)
1115 (const :tag "Wednesday" 3)
1116 (const :tag "Thursday" 4)
1117 (const :tag "Friday" 5)
1118 (const :tag "Saturday" 6)
1119 (const :tag "Sunday" 0)))
1121 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1122 "Non-nil means jump to today when moving a past date forward in time.
1123 When using S-right in the agenda to move a a date forward, and the date
1124 stamp currently points to the past, the first key press will move it
1125 to today. WHen nil, just move one day forward even if the date stays
1126 in the past."
1127 :group 'org-agenda-daily/weekly
1128 :version "24.1"
1129 :type 'boolean)
1131 (defcustom org-agenda-include-diary nil
1132 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1133 Custom commands can set this variable in the options section."
1134 :group 'org-agenda-daily/weekly
1135 :type 'boolean)
1137 (defcustom org-agenda-include-deadlines t
1138 "If non-nil, include entries within their deadline warning period.
1139 Custom commands can set this variable in the options section."
1140 :group 'org-agenda-daily/weekly
1141 :version "24.1"
1142 :type 'boolean)
1144 (defcustom org-agenda-repeating-timestamp-show-all t
1145 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1146 When set to a list of strings, only show occurrences of repeating
1147 stamps for these TODO keywords. When nil, only one occurrence is
1148 shown, either today or the nearest into the future."
1149 :group 'org-agenda-daily/weekly
1150 :type '(choice
1151 (const :tag "Show repeating stamps" t)
1152 (repeat :tag "Show repeating stamps for these TODO keywords"
1153 (string :tag "TODO Keyword"))
1154 (const :tag "Don't show repeating stamps" nil)))
1156 (defcustom org-scheduled-past-days 10000
1157 "No. of days to continue listing scheduled items that are not marked DONE.
1158 When an item is scheduled on a date, it shows up in the agenda on this
1159 day and will be listed until it is marked done for the number of days
1160 given here."
1161 :group 'org-agenda-daily/weekly
1162 :type 'integer)
1164 (defcustom org-agenda-log-mode-items '(closed clock)
1165 "List of items that should be shown in agenda log mode.
1166 This list may contain the following symbols:
1168 closed Show entries that have been closed on that day.
1169 clock Show entries that have received clocked time on that day.
1170 state Show all logged state changes.
1171 Note that instead of changing this variable, you can also press `C-u l' in
1172 the agenda to display all available LOG items temporarily."
1173 :group 'org-agenda-daily/weekly
1174 :type '(set :greedy t (const closed) (const clock) (const state)))
1176 (defcustom org-agenda-clock-consistency-checks
1177 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1178 :gap-ok-around ("4:00")
1179 :default-face ((:background "DarkRed") (:foreground "white"))
1180 :overlap-face nil :gap-face nil :no-end-time-face nil
1181 :long-face nil :short-face nil)
1182 "This is a property list, with the following keys:
1184 :max-duration Mark clocking chunks that are longer than this time.
1185 This is a time string like \"HH:MM\", or the number
1186 of minutes as an integer.
1188 :min-duration Mark clocking chunks that are shorter that this.
1189 This is a time string like \"HH:MM\", or the number
1190 of minutes as an integer.
1192 :max-gap Mark gaps between clocking chunks that are longer than
1193 this duration. A number of minutes, or a string
1194 like \"HH:MM\".
1196 :gap-ok-around List of times during the day which are usually not working
1197 times. When a gap is detected, but the gap contains any
1198 of these times, the gap is *not* reported. For example,
1199 if this is (\"4:00\" \"13:00\") then gaps that contain
1200 4:00 in the morning (i.e. the night) and 13:00
1201 (i.e. a typical lunch time) do not cause a warning.
1202 You should have at least one time during the night in this
1203 list, or otherwise the first task each morning will trigger
1204 a warning because it follows a long gap.
1206 Furthermore, the following properties can be used to define faces for
1207 issue display.
1209 :default-face the default face, if the specific face is undefined
1210 :overlap-face face for overlapping clocks
1211 :gap-face face for gaps between clocks
1212 :no-end-time-face face for incomplete clocks
1213 :long-face face for clock intervals that are too long
1214 :short-face face for clock intervals that are too short"
1215 :group 'org-agenda-daily/weekly
1216 :group 'org-clock
1217 :version "24.1"
1218 :type 'plist)
1220 (defcustom org-agenda-log-mode-add-notes t
1221 "Non-nil means add first line of notes to log entries in agenda views.
1222 If a log item like a state change or a clock entry is associated with
1223 notes, the first line of these notes will be added to the entry in the
1224 agenda display."
1225 :group 'org-agenda-daily/weekly
1226 :type 'boolean)
1228 (defcustom org-agenda-start-with-log-mode nil
1229 "The initial value of log-mode in a newly created agenda window.
1230 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1231 explanations on the possible values."
1232 :group 'org-agenda-startup
1233 :group 'org-agenda-daily/weekly
1234 :type '(choice (const :tag "Don't show log items" nil)
1235 (const :tag "Show only log items" 'only)
1236 (const :tag "Show all possible log items" 'clockcheck)
1237 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1238 (choice (const :tag "Show closed log items" 'closed)
1239 (const :tag "Show clocked log items" 'clock)
1240 (const :tag "Show all logged state changes" 'state)))))
1242 (defcustom org-agenda-start-with-clockreport-mode nil
1243 "The initial value of clockreport-mode in a newly created agenda window."
1244 :group 'org-agenda-startup
1245 :group 'org-agenda-daily/weekly
1246 :type 'boolean)
1248 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1249 "Property list with parameters for the clocktable in clockreport mode.
1250 This is the display mode that shows a clock table in the daily/weekly
1251 agenda, the properties for this dynamic block can be set here.
1252 The usual clocktable parameters are allowed here, but you cannot set
1253 the properties :name, :tstart, :tend, :block, and :scope - these will
1254 be overwritten to make sure the content accurately reflects the
1255 current display in the agenda."
1256 :group 'org-agenda-daily/weekly
1257 :type 'plist)
1259 (defcustom org-agenda-search-view-always-boolean nil
1260 "Non-nil means the search string is interpreted as individual parts.
1262 The search string for search view can either be interpreted as a phrase,
1263 or as a list of snippets that define a boolean search for a number of
1264 strings.
1266 When this is non-nil, the string will be split on whitespace, and each
1267 snippet will be searched individually, and all must match in order to
1268 select an entry. A snippet is then a single string of non-white
1269 characters, or a string in double quotes, or a regexp in {} braces.
1270 If a snippet is preceded by \"-\", the snippet must *not* match.
1271 \"+\" is syntactic sugar for positive selection. Each snippet may
1272 be found as a full word or a partial word, but see the variable
1273 `org-agenda-search-view-force-full-words'.
1275 When this is nil, search will look for the entire search phrase as one,
1276 with each space character matching any amount of whitespace, including
1277 line breaks.
1279 Even when this is nil, you can still switch to Boolean search dynamically
1280 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1281 is a regexp marked with braces like \"{abc}\", this will also switch to
1282 boolean search."
1283 :group 'org-agenda-search-view
1284 :version "24.1"
1285 :type 'boolean)
1287 (if (fboundp 'defvaralias)
1288 (defvaralias 'org-agenda-search-view-search-words-only
1289 'org-agenda-search-view-always-boolean))
1291 (defcustom org-agenda-search-view-force-full-words nil
1292 "Non-nil means, search words must be matches as complete words.
1293 When nil, they may also match part of a word."
1294 :group 'org-agenda-search-view
1295 :version "24.1"
1296 :type 'boolean)
1298 (defgroup org-agenda-time-grid nil
1299 "Options concerning the time grid in the Org-mode Agenda."
1300 :tag "Org Agenda Time Grid"
1301 :group 'org-agenda)
1303 (defcustom org-agenda-search-headline-for-time t
1304 "Non-nil means search headline for a time-of-day.
1305 If the headline contains a time-of-day in one format or another, it will
1306 be used to sort the entry into the time sequence of items for a day.
1307 Some people have time stamps in the headline that refer to the creation
1308 time or so, and then this produces an unwanted side effect. If this is
1309 the case for your, use this variable to turn off searching the headline
1310 for a time."
1311 :group 'org-agenda-time-grid
1312 :type 'boolean)
1314 (defcustom org-agenda-use-time-grid t
1315 "Non-nil means show a time grid in the agenda schedule.
1316 A time grid is a set of lines for specific times (like every two hours between
1317 8:00 and 20:00). The items scheduled for a day at specific times are
1318 sorted in between these lines.
1319 For details about when the grid will be shown, and what it will look like, see
1320 the variable `org-agenda-time-grid'."
1321 :group 'org-agenda-time-grid
1322 :type 'boolean)
1324 (defcustom org-agenda-time-grid
1325 '((daily today require-timed)
1326 "----------------"
1327 (800 1000 1200 1400 1600 1800 2000))
1329 "The settings for time grid for agenda display.
1330 This is a list of three items. The first item is again a list. It contains
1331 symbols specifying conditions when the grid should be displayed:
1333 daily if the agenda shows a single day
1334 weekly if the agenda shows an entire week
1335 today show grid on current date, independent of daily/weekly display
1336 require-timed show grid only if at least one item has a time specification
1338 The second item is a string which will be placed behind the grid time.
1340 The third item is a list of integers, indicating the times that should have
1341 a grid line."
1342 :group 'org-agenda-time-grid
1343 :type
1344 '(list
1345 (set :greedy t :tag "Grid Display Options"
1346 (const :tag "Show grid in single day agenda display" daily)
1347 (const :tag "Show grid in weekly agenda display" weekly)
1348 (const :tag "Always show grid for today" today)
1349 (const :tag "Show grid only if any timed entries are present"
1350 require-timed)
1351 (const :tag "Skip grid times already present in an entry"
1352 remove-match))
1353 (string :tag "Grid String")
1354 (repeat :tag "Grid Times" (integer :tag "Time"))))
1356 (defcustom org-agenda-show-current-time-in-grid t
1357 "Non-nil means show the current time in the time grid."
1358 :group 'org-agenda-time-grid
1359 :version "24.1"
1360 :type 'boolean)
1362 (defcustom org-agenda-current-time-string
1363 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1364 "The string for the current time marker in the agenda."
1365 :group 'org-agenda-time-grid
1366 :version "24.1"
1367 :type 'string)
1369 (defgroup org-agenda-sorting nil
1370 "Options concerning sorting in the Org-mode Agenda."
1371 :tag "Org Agenda Sorting"
1372 :group 'org-agenda)
1374 (defcustom org-agenda-sorting-strategy
1375 '((agenda habit-down time-up priority-down category-keep)
1376 (todo priority-down category-keep)
1377 (tags priority-down category-keep)
1378 (search category-keep))
1379 "Sorting structure for the agenda items of a single day.
1380 This is a list of symbols which will be used in sequence to determine
1381 if an entry should be listed before another entry. The following
1382 symbols are recognized:
1384 time-up Put entries with time-of-day indications first, early first
1385 time-down Put entries with time-of-day indications first, late first
1386 category-keep Keep the default order of categories, corresponding to the
1387 sequence in `org-agenda-files'.
1388 category-up Sort alphabetically by category, A-Z.
1389 category-down Sort alphabetically by category, Z-A.
1390 tag-up Sort alphabetically by last tag, A-Z.
1391 tag-down Sort alphabetically by last tag, Z-A.
1392 priority-up Sort numerically by priority, high priority last.
1393 priority-down Sort numerically by priority, high priority first.
1394 todo-state-up Sort by todo state, tasks that are done last.
1395 todo-state-down Sort by todo state, tasks that are done first.
1396 effort-up Sort numerically by estimated effort, high effort last.
1397 effort-down Sort numerically by estimated effort, high effort first.
1398 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1399 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1400 habit-up Put entries that are habits first
1401 habit-down Put entries that are habits last
1402 alpha-up Sort headlines alphabetically
1403 alpha-down Sort headlines alphabetically, reversed
1405 The different possibilities will be tried in sequence, and testing stops
1406 if one comparison returns a \"not-equal\". For example, the default
1407 '(time-up category-keep priority-down)
1408 means: Pull out all entries having a specified time of day and sort them,
1409 in order to make a time schedule for the current day the first thing in the
1410 agenda listing for the day. Of the entries without a time indication, keep
1411 the grouped in categories, don't sort the categories, but keep them in
1412 the sequence given in `org-agenda-files'. Within each category sort by
1413 priority.
1415 Leaving out `category-keep' would mean that items will be sorted across
1416 categories by priority.
1418 Instead of a single list, this can also be a set of list for specific
1419 contents, with a context symbol in the car of the list, any of
1420 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1422 Custom commands can bind this variable in the options section."
1423 :group 'org-agenda-sorting
1424 :type `(choice
1425 (repeat :tag "General" ,org-sorting-choice)
1426 (list :tag "Individually"
1427 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1428 (repeat ,org-sorting-choice))
1429 (cons (const :tag "Strategy for TODO lists" todo)
1430 (repeat ,org-sorting-choice))
1431 (cons (const :tag "Strategy for Tags matches" tags)
1432 (repeat ,org-sorting-choice))
1433 (cons (const :tag "Strategy for search matches" search)
1434 (repeat ,org-sorting-choice)))))
1436 (defcustom org-agenda-cmp-user-defined nil
1437 "A function to define the comparison `user-defined'.
1438 This function must receive two arguments, agenda entry a and b.
1439 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1440 the user comparison, return nil.
1441 When this is defined, you can make `user-defined-up' and `user-defined-down'
1442 part of an agenda sorting strategy."
1443 :group 'org-agenda-sorting
1444 :type 'symbol)
1446 (defcustom org-sort-agenda-notime-is-late t
1447 "Non-nil means items without time are considered late.
1448 This is only relevant for sorting. When t, items which have no explicit
1449 time like 15:30 will be considered as 99:01, i.e. later than any items which
1450 do have a time. When nil, the default time is before 0:00. You can use this
1451 option to decide if the schedule for today should come before or after timeless
1452 agenda entries."
1453 :group 'org-agenda-sorting
1454 :type 'boolean)
1456 (defcustom org-sort-agenda-noeffort-is-high t
1457 "Non-nil means items without effort estimate are sorted as high effort.
1458 This also applies when filtering an agenda view with respect to the
1459 < or > effort operator. Then, tasks with no effort defined will be treated
1460 as tasks with high effort.
1461 When nil, such items are sorted as 0 minutes effort."
1462 :group 'org-agenda-sorting
1463 :type 'boolean)
1465 (defgroup org-agenda-line-format nil
1466 "Options concerning the entry prefix in the Org-mode agenda display."
1467 :tag "Org Agenda Line Format"
1468 :group 'org-agenda)
1470 (defcustom org-agenda-prefix-format
1471 '((agenda . " %i %-12:c%?-12t% s")
1472 (timeline . " % s")
1473 (todo . " %i %-12:c")
1474 (tags . " %i %-12:c")
1475 (search . " %i %-12:c"))
1476 "Format specifications for the prefix of items in the agenda views.
1477 An alist with five entries, each for the different agenda types. The
1478 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1479 The values are format strings.
1481 This format works similar to a printf format, with the following meaning:
1483 %c the category of the item, \"Diary\" for entries from the diary,
1484 or as given by the CATEGORY keyword or derived from the file name
1485 %e the effort required by the item
1486 %i the icon category of the item, see `org-agenda-category-icon-alist'
1487 %T the last tag of the item (ignore inherited tags, which come first)
1488 %t the HH:MM time-of-day specification if one applies to the entry
1489 %s Scheduling/Deadline information, a short string
1490 %(expression) Eval EXPRESSION and replace the control string
1491 by the result
1493 All specifiers work basically like the standard `%s' of printf, but may
1494 contain two additional characters: a question mark just after the `%'
1495 and a whitespace/punctuation character just before the final letter.
1497 If the first character after `%' is a question mark, the entire field
1498 will only be included if the corresponding value applies to the current
1499 entry. This is useful for fields which should have fixed width when
1500 present, but zero width when absent. For example, \"%?-12t\" will
1501 result in a 12 character time field if a time of the day is specified,
1502 but will completely disappear in entries which do not contain a time.
1504 If there is punctuation or whitespace character just before the final
1505 format letter, this character will be appended to the field value if
1506 the value is not empty. For example, the format \"%-12:c\" leads to
1507 \"Diary: \" if the category is \"Diary\". If the category were be
1508 empty, no additional colon would be inserted.
1510 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1511 which means:
1513 - Indent the line with two space characters
1514 - Give the category a 12 chars wide field, padded with whitespace on
1515 the right (because of `-'). Append a colon if there is a category
1516 (because of `:').
1517 - If there is a time-of-day, put it into a 12 chars wide field. If no
1518 time, don't put in an empty field, just skip it (because of '?').
1519 - Finally, put the scheduling information.
1521 See also the variables `org-agenda-remove-times-when-in-prefix' and
1522 `org-agenda-remove-tags'.
1524 Custom commands can set this variable in the options section."
1525 :type '(choice
1526 (string :tag "General format")
1527 (list :greedy t :tag "View dependent"
1528 (cons (const agenda) (string :tag "Format"))
1529 (cons (const timeline) (string :tag "Format"))
1530 (cons (const todo) (string :tag "Format"))
1531 (cons (const tags) (string :tag "Format"))
1532 (cons (const search) (string :tag "Format"))))
1533 :group 'org-agenda-line-format)
1535 (defvar org-prefix-format-compiled nil
1536 "The compiled prefix format and associated variables.
1537 This is a list where first element is a list of variable bindings, and second
1538 element is the compiled format expression. See the variable
1539 `org-agenda-prefix-format'.")
1541 (defcustom org-agenda-todo-keyword-format "%-1s"
1542 "Format for the TODO keyword in agenda lines.
1543 Set this to something like \"%-12s\" if you want all TODO keywords
1544 to occupy a fixed space in the agenda display."
1545 :group 'org-agenda-line-format
1546 :type 'string)
1548 (defcustom org-agenda-diary-sexp-prefix nil
1549 "A regexp that matches part of a diary sexp entry
1550 which should be treated as scheduling/deadline information in
1551 `org-agenda'.
1553 For example, you can use this to extract the `diary-remind-message' from
1554 `diary-remind' entries."
1555 :group 'org-agenda-line-format
1556 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1558 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1559 "Text preceding timerange entries in the agenda view.
1560 This is a list with two strings. The first applies when the range
1561 is entirely on one day. The second applies if the range spans several days.
1562 The strings may have two \"%d\" format specifiers which will be filled
1563 with the sequence number of the days, and the total number of days in the
1564 range, respectively."
1565 :group 'org-agenda-line-format
1566 :type '(list
1567 (string :tag "Deadline today ")
1568 (choice :tag "Deadline relative"
1569 (string :tag "Format string")
1570 (function))))
1572 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1573 "Text preceding scheduled items in the agenda view.
1574 This is a list with two strings. The first applies when the item is
1575 scheduled on the current day. The second applies when it has been scheduled
1576 previously, it may contain a %d indicating that this is the nth time that
1577 this item is scheduled, due to automatic rescheduling of unfinished items
1578 for the following day. So this number is one larger than the number of days
1579 that passed since this item was scheduled first."
1580 :group 'org-agenda-line-format
1581 :type '(list
1582 (string :tag "Scheduled today ")
1583 (string :tag "Scheduled previously")))
1585 (defcustom org-agenda-inactive-leader "["
1586 "Text preceding item pulled into the agenda by inactive time stamps.
1587 These entries are added to the agenda when pressing \"[\"."
1588 :group 'org-agenda-line-format
1589 :version "24.1"
1590 :type '(list
1591 (string :tag "Scheduled today ")
1592 (string :tag "Scheduled previously")))
1594 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1595 "Text preceding deadline items in the agenda view.
1596 This is a list with two strings. The first applies when the item has its
1597 deadline on the current day. The second applies when it is in the past or
1598 in the future, it may contain %d to capture how many days away the deadline
1599 is (was)."
1600 :group 'org-agenda-line-format
1601 :type '(list
1602 (string :tag "Deadline today ")
1603 (choice :tag "Deadline relative"
1604 (string :tag "Format string")
1605 (function))))
1607 (defcustom org-agenda-remove-times-when-in-prefix t
1608 "Non-nil means remove duplicate time specifications in agenda items.
1609 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1610 time-of-day specification in a headline or diary entry is extracted and
1611 placed into the prefix. If this option is non-nil, the original specification
1612 \(a timestamp or -range, or just a plain time(range) specification like
1613 11:30-4pm) will be removed for agenda display. This makes the agenda less
1614 cluttered.
1615 The option can be t or nil. It may also be the symbol `beg', indicating
1616 that the time should only be removed when it is located at the beginning of
1617 the headline/diary entry."
1618 :group 'org-agenda-line-format
1619 :type '(choice
1620 (const :tag "Always" t)
1621 (const :tag "Never" nil)
1622 (const :tag "When at beginning of entry" beg)))
1624 (defcustom org-agenda-remove-timeranges-from-blocks nil
1625 "Non-nil means remove time ranges specifications in agenda
1626 items that span on several days."
1627 :group 'org-agenda-line-format
1628 :version "24.1"
1629 :type 'boolean)
1631 (defcustom org-agenda-default-appointment-duration nil
1632 "Default duration for appointments that only have a starting time.
1633 When nil, no duration is specified in such cases.
1634 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1635 :group 'org-agenda-line-format
1636 :type '(choice
1637 (integer :tag "Minutes")
1638 (const :tag "No default duration")))
1640 (defcustom org-agenda-show-inherited-tags t
1641 "Non-nil means show inherited tags in each agenda line."
1642 :group 'org-agenda-line-format
1643 :type 'boolean)
1645 (defcustom org-agenda-hide-tags-regexp nil
1646 "Regular expression used to filter away specific tags in agenda views.
1647 This means that these tags will be present, but not be shown in the agenda
1648 line. Secondary filtering will still work on the hidden tags.
1649 Nil means don't hide any tags."
1650 :group 'org-agenda-line-format
1651 :type '(choice
1652 (const :tag "Hide none" nil)
1653 (string :tag "Regexp ")))
1655 (defcustom org-agenda-remove-tags nil
1656 "Non-nil means remove the tags from the headline copy in the agenda.
1657 When this is the symbol `prefix', only remove tags when
1658 `org-agenda-prefix-format' contains a `%T' specifier."
1659 :group 'org-agenda-line-format
1660 :type '(choice
1661 (const :tag "Always" t)
1662 (const :tag "Never" nil)
1663 (const :tag "When prefix format contains %T" prefix)))
1665 (if (fboundp 'defvaralias)
1666 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1667 'org-agenda-remove-tags))
1669 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1670 "Shift tags in agenda items to this column.
1671 If this number is positive, it specifies the column. If it is negative,
1672 it means that the tags should be flushright to that column. For example,
1673 -80 works well for a normal 80 character screen."
1674 :group 'org-agenda-line-format
1675 :type 'integer)
1677 (if (fboundp 'defvaralias)
1678 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1680 (defcustom org-agenda-fontify-priorities 'cookies
1681 "Non-nil means highlight low and high priorities in agenda.
1682 When t, the highest priority entries are bold, lowest priority italic.
1683 However, settings in `org-priority-faces' will overrule these faces.
1684 When this variable is the symbol `cookies', only fontify the
1685 cookies, not the entire task.
1686 This may also be an association list of priority faces, whose
1687 keys are the character values of `org-highest-priority',
1688 `org-default-priority', and `org-lowest-priority' (the default values
1689 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1690 color as a string, or a list like `(:background \"Red\")'.
1691 If it is a color, the variable `org-faces-easy-properties'
1692 determines if it is a foreground or a background color."
1693 :group 'org-agenda-line-format
1694 :type '(choice
1695 (const :tag "Never" nil)
1696 (const :tag "Defaults" t)
1697 (const :tag "Cookies only" cookies)
1698 (repeat :tag "Specify"
1699 (list (character :tag "Priority" :value ?A)
1700 (choice :tag "Face "
1701 (string :tag "Color")
1702 (sexp :tag "Face"))))))
1704 (defcustom org-agenda-day-face-function nil
1705 "Function called to determine what face should be used to display a day.
1706 The only argument passed to that function is the day. It should
1707 returns a face, or nil if does not want to specify a face and let
1708 the normal rules apply."
1709 :group 'org-agenda-line-format
1710 :version "24.1"
1711 :type 'function)
1713 (defcustom org-agenda-category-icon-alist nil
1714 "Alist of category icon to be displayed in agenda views.
1716 Each entry should have the following format:
1718 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1720 Where CATEGORY-REGEXP is a regexp matching the categories where
1721 the icon should be displayed.
1722 FILE-OR-DATA either a file path or a string containing image data.
1724 The other fields can be omitted safely if not needed:
1725 TYPE indicates the image type.
1726 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1727 image data.
1728 PROPS are additional image attributes to assign to the image,
1729 like, e.g. `:ascent center'.
1731 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1733 If you want to set the display properties yourself, just put a
1734 list as second element:
1736 (CATEGORY-REGEXP (MY PROPERTY LIST))
1738 For example, to display a 16px horizontal space for Emacs
1739 category, you can use:
1741 (\"Emacs\" '(space . (:width (16))))"
1742 :group 'org-agenda-line-format
1743 :version "24.1"
1744 :type '(alist :key-type (string :tag "Regexp matching category")
1745 :value-type (choice (list :tag "Icon"
1746 (string :tag "File or data")
1747 (symbol :tag "Type")
1748 (boolean :tag "Data?")
1749 (repeat :tag "Extra image properties" :inline t symbol))
1750 (list :tag "Display properties" sexp))))
1752 (defgroup org-agenda-column-view nil
1753 "Options concerning column view in the agenda."
1754 :tag "Org Agenda Column View"
1755 :group 'org-agenda)
1757 (defcustom org-agenda-columns-show-summaries t
1758 "Non-nil means show summaries for columns displayed in the agenda view."
1759 :group 'org-agenda-column-view
1760 :type 'boolean)
1762 (defcustom org-agenda-columns-compute-summary-properties t
1763 "Non-nil means recompute all summary properties before column view.
1764 When column view in the agenda is listing properties that have a summary
1765 operator, it can go to all relevant buffers and recompute the summaries
1766 there. This can mean overhead for the agenda column view, but is necessary
1767 to have thing up to date.
1768 As a special case, a CLOCKSUM property also makes sure that the clock
1769 computations are current."
1770 :group 'org-agenda-column-view
1771 :type 'boolean)
1773 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1774 "Non-nil means the duration of an appointment will add to day effort.
1775 The property to which appointment durations will be added is the one given
1776 in the option `org-effort-property'. If an appointment does not have
1777 an end time, `org-agenda-default-appointment-duration' will be used. If that
1778 is not set, an appointment without end time will not contribute to the time
1779 estimate."
1780 :group 'org-agenda-column-view
1781 :type 'boolean)
1783 (defcustom org-agenda-auto-exclude-function nil
1784 "A function called with a tag to decide if it is filtered on '/ RET'.
1785 The sole argument to the function, which is called once for each
1786 possible tag, is a string giving the name of the tag. The
1787 function should return either nil if the tag should be included
1788 as normal, or \"-<TAG>\" to exclude the tag.
1789 Note that for the purpose of tag filtering, only the lower-case version of
1790 all tags will be considered, so that this function will only ever see
1791 the lower-case version of all tags."
1792 :group 'org-agenda
1793 :type 'function)
1795 (defcustom org-agenda-bulk-custom-functions nil
1796 "Alist of characters and custom functions for bulk actions.
1797 For example, this value makes those two functions available:
1799 '((?R set-category)
1800 (?C bulk-cut))
1802 With selected entries in an agenda buffer, `B R' will call
1803 the custom function `set-category' on the selected entries.
1804 Note that functions in this alist don't need to be quoted."
1805 :type 'alist
1806 :version "24.1"
1807 :group 'org-agenda)
1809 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1810 "Execute BODY with point at location given by `org-hd-marker' property.
1811 If STRING is non-nil, the text property will be fetched from position 0
1812 in that string. If STRING is nil, it will be fetched from the beginning
1813 of the current line."
1814 (org-with-gensyms (marker)
1815 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1816 'org-hd-marker ,string)))
1817 (with-current-buffer (marker-buffer ,marker)
1818 (save-excursion
1819 (goto-char ,marker)
1820 ,@body)))))
1821 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1823 (defun org-add-agenda-custom-command (entry)
1824 "Replace or add a command in `org-agenda-custom-commands'.
1825 This is mostly for hacking and trying a new command - once the command
1826 works you probably want to add it to `org-agenda-custom-commands' for good."
1827 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1828 (if ass
1829 (setcdr ass (cdr entry))
1830 (push entry org-agenda-custom-commands))))
1832 ;;; Define the org-agenda-mode
1834 (defvar org-agenda-mode-map (make-sparse-keymap)
1835 "Keymap for `org-agenda-mode'.")
1836 (if (fboundp 'defvaralias)
1837 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1839 (defvar org-agenda-menu) ; defined later in this file.
1840 (defvar org-agenda-restrict) ; defined later in this file.
1841 (defvar org-agenda-follow-mode nil)
1842 (defvar org-agenda-entry-text-mode nil)
1843 (defvar org-agenda-clockreport-mode nil)
1844 (defvar org-agenda-show-log nil)
1845 (defvar org-agenda-redo-command nil)
1846 (defvar org-agenda-query-string nil)
1847 (defvar org-agenda-mode-hook nil
1848 "Hook for `org-agenda-mode', run after the mode is turned on.")
1849 (defvar org-agenda-type nil)
1850 (defvar org-agenda-force-single-file nil)
1851 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1854 ;;; Multiple agenda buffers support
1856 ;;;###autoload
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 (defcustom org-agenda-sticky nil
1874 "Non-nil means agenda q key will bury agenda buffers.
1875 Agenda commands will then show existing buffer instead of generating new ones.
1876 When nil, `q' will kill the single agenda buffer."
1877 :group 'org-agenda
1878 :type 'boolean
1879 :set (lambda (var val)
1880 (if (boundp var)
1881 (org-toggle-sticky-agenda (if val 1 0))
1882 (set var val))))
1884 (defvar org-agenda-buffer nil
1885 "Agenda buffer currently being generated.")
1887 (defvar org-agenda-last-prefix-arg nil)
1888 (defvar org-agenda-this-buffer-name nil)
1889 (defvar org-agenda-doing-sticky-redo nil)
1890 (defvar org-agenda-this-buffer-is-sticky nil)
1892 (defconst org-agenda-local-vars
1893 '(org-agenda-this-buffer-name
1894 org-agenda-undo-list
1895 org-agenda-pending-undo-list
1896 org-agenda-follow-mode
1897 org-agenda-entry-text-mode
1898 org-agenda-clockreport-mode
1899 org-agenda-show-log
1900 org-agenda-redo-command
1901 org-agenda-query-string
1902 org-agenda-type
1903 org-agenda-bulk-marked-entries
1904 org-agenda-undo-has-started-in
1905 org-agenda-last-arguments
1906 org-agenda-info
1907 org-agenda-tag-filter-overlays
1908 org-agenda-cat-filter-overlays
1909 org-pre-agenda-window-conf
1910 org-agenda-columns-active
1911 org-agenda-tag-filter
1912 org-agenda-category-filter
1913 org-agenda-markers
1914 org-agenda-last-search-view-search-was-boolean
1915 org-agenda-filtered-by-category
1916 org-agenda-filter-form
1917 org-agenda-show-window
1918 org-agenda-cycle-counter
1919 org-agenda-last-prefix-arg)
1920 "Variables that must be local in agenda buffers to allow multiple buffers.")
1922 (defun org-agenda-mode ()
1923 "Mode for time-sorted view on action items in Org-mode files.
1925 The following commands are available:
1927 \\{org-agenda-mode-map}"
1928 (interactive)
1929 (cond (org-agenda-doing-sticky-redo
1930 ;; Refreshing sticky agenda-buffer
1932 ;; Preserve the value of `org-agenda-local-vars' variables,
1933 ;; while letting `kill-all-local-variables' kill the rest
1934 (let ((save (buffer-local-variables)))
1935 (kill-all-local-variables)
1936 (mapc 'make-local-variable org-agenda-local-vars)
1937 (dolist (elem save)
1938 (let ((var (car elem))
1939 (val (cdr elem)))
1940 (when (and val
1941 (member var org-agenda-local-vars))
1942 (set var val)))))
1943 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1944 (org-agenda-sticky
1945 ;; Creating a sticky Agenda buffer for the first time
1946 (kill-all-local-variables)
1947 (mapc 'make-local-variable org-agenda-local-vars)
1948 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1950 ;; Creating a non-sticky agenda buffer
1951 (kill-all-local-variables)
1952 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
1953 (setq org-agenda-undo-list nil
1954 org-agenda-pending-undo-list nil
1955 org-agenda-bulk-marked-entries nil)
1956 (setq major-mode 'org-agenda-mode)
1957 ;; Keep global-font-lock-mode from turning on font-lock-mode
1958 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1959 (setq mode-name "Org-Agenda")
1960 (use-local-map org-agenda-mode-map)
1961 (easy-menu-add org-agenda-menu)
1962 (if org-startup-truncated (setq truncate-lines t))
1963 (org-set-local 'line-move-visual nil)
1964 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1965 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1966 ;; Make sure properties are removed when copying text
1967 (make-local-variable 'filter-buffer-substring-functions)
1968 (add-hook 'filter-buffer-substring-functions
1969 (lambda (fun start end delete)
1970 (substring-no-properties (funcall fun start end delete))))
1971 (unless org-agenda-keep-modes
1972 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1973 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1974 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1975 org-agenda-show-log org-agenda-start-with-log-mode))
1977 (easy-menu-change
1978 '("Agenda") "Agenda Files"
1979 (append
1980 (list
1981 (vector
1982 (if (get 'org-agenda-files 'org-restrict)
1983 "Restricted to single file"
1984 "Edit File List")
1985 '(org-edit-agenda-file-list)
1986 (not (get 'org-agenda-files 'org-restrict)))
1987 "--")
1988 (mapcar 'org-file-menu-entry (org-agenda-files))))
1989 (org-agenda-set-mode-name)
1990 (apply
1991 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1992 (list 'org-agenda-mode-hook)))
1994 (substitute-key-definition 'undo 'org-agenda-undo
1995 org-agenda-mode-map global-map)
1996 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1997 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1998 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1999 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2000 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2001 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2002 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2003 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2004 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2005 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2006 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2007 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2008 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2009 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2010 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2011 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2012 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2013 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2014 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2015 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2016 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2017 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2018 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2019 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2020 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2021 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2022 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2023 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2024 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2025 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2026 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2027 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2028 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2029 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2030 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2031 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2032 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2033 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2034 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2035 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2036 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
2037 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
2038 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2039 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2040 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2041 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2043 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2044 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2045 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2046 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2047 (while l (org-defkey org-agenda-mode-map
2048 (int-to-string (pop l)) 'digit-argument)))
2050 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2051 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2052 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2053 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2054 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2055 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2056 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2057 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2058 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2059 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
2060 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2061 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2062 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2063 'org-clock-modify-effort-estimate)
2064 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2065 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2066 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2067 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2068 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2069 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2070 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2071 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2072 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2073 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2074 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2075 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2076 (substitute-key-definition 'next-line 'org-agenda-next-line
2077 org-agenda-mode-map global-map)
2078 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2079 org-agenda-mode-map global-map)
2080 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2081 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2082 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2083 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2084 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-show-priority)
2085 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2086 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2087 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2088 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2089 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2090 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2091 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2092 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2093 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2094 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2095 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2096 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2097 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2098 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2099 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2100 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2101 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2102 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2103 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2104 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2105 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2106 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2107 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2108 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2109 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2111 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2112 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2113 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2114 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2115 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2116 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2117 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2118 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-category)
2119 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2120 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2121 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2122 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2124 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2125 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2126 (when org-agenda-mouse-1-follows-link
2127 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2128 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2129 '("Agenda"
2130 ("Agenda Files")
2131 "--"
2132 ("Agenda Dates"
2133 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2134 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2135 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2136 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2137 "--"
2138 ("View"
2139 ["Day View" org-agenda-day-view
2140 :active (org-agenda-check-type nil 'agenda)
2141 :style radio :selected (eq org-agenda-current-span 'day)
2142 :keys "v d (or just d)"]
2143 ["Week View" org-agenda-week-view
2144 :active (org-agenda-check-type nil 'agenda)
2145 :style radio :selected (eq org-agenda-current-span 'week)
2146 :keys "v w (or just w)"]
2147 ["Month View" org-agenda-month-view
2148 :active (org-agenda-check-type nil 'agenda)
2149 :style radio :selected (eq org-agenda-current-span 'month)
2150 :keys "v m"]
2151 ["Year View" org-agenda-year-view
2152 :active (org-agenda-check-type nil 'agenda)
2153 :style radio :selected (eq org-agenda-current-span 'year)
2154 :keys "v y"]
2155 "--"
2156 ["Include Diary" org-agenda-toggle-diary
2157 :style toggle :selected org-agenda-include-diary
2158 :active (org-agenda-check-type nil 'agenda)]
2159 ["Include Deadlines" org-agenda-toggle-deadlines
2160 :style toggle :selected org-agenda-include-deadlines
2161 :active (org-agenda-check-type nil 'agenda)]
2162 ["Use Time Grid" org-agenda-toggle-time-grid
2163 :style toggle :selected org-agenda-use-time-grid
2164 :active (org-agenda-check-type nil 'agenda)]
2165 "--"
2166 ["Show clock report" org-agenda-clockreport-mode
2167 :style toggle :selected org-agenda-clockreport-mode
2168 :active (org-agenda-check-type nil 'agenda)]
2169 ["Show some entry text" org-agenda-entry-text-mode
2170 :style toggle :selected org-agenda-entry-text-mode
2171 :active t]
2172 "--"
2173 ["Show Logbook entries" org-agenda-log-mode
2174 :style toggle :selected org-agenda-show-log
2175 :active (org-agenda-check-type nil 'agenda 'timeline)
2176 :keys "v l (or just l)"]
2177 ["Include archived trees" org-agenda-archives-mode
2178 :style toggle :selected org-agenda-archives-mode :active t
2179 :keys "v a"]
2180 ["Include archive files" (org-agenda-archives-mode t)
2181 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2182 :keys "v A"]
2183 "--"
2184 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2185 ["Write view to file" org-agenda-write t]
2186 ["Rebuild buffer" org-agenda-redo t]
2187 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2188 "--"
2189 ["Show original entry" org-agenda-show t]
2190 ["Go To (other window)" org-agenda-goto t]
2191 ["Go To (this window)" org-agenda-switch-to t]
2192 ["Follow Mode" org-agenda-follow-mode
2193 :style toggle :selected org-agenda-follow-mode :active t]
2194 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2195 "--"
2196 ("TODO"
2197 ["Cycle TODO" org-agenda-todo t]
2198 ["Next TODO set" org-agenda-todo-nextset t]
2199 ["Previous TODO set" org-agenda-todo-previousset t]
2200 ["Add note" org-agenda-add-note t])
2201 ("Archive/Refile/Delete"
2202 ["Archive default" org-agenda-archive-default t]
2203 ["Archive default" org-agenda-archive-default-with-confirmation t]
2204 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2205 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2206 ["Archive subtree" org-agenda-archive t]
2207 "--"
2208 ["Refile" org-agenda-refile t]
2209 "--"
2210 ["Delete subtree" org-agenda-kill t])
2211 ("Bulk action"
2212 ["Mark entry" org-agenda-bulk-mark t]
2213 ["Mark all" org-agenda-bulk-mark-all t]
2214 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2215 ["Unmark entry" org-agenda-bulk-unmark t]
2216 ["Unmark all entries" org-agenda-bulk-unmark-all :active t :keys "C-u s"])
2217 ["Act on all marked" org-agenda-bulk-action t]
2218 "--"
2219 ("Tags and Properties"
2220 ["Show all Tags" org-agenda-show-tags t]
2221 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2222 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2223 "--"
2224 ["Column View" org-columns t])
2225 ("Deadline/Schedule"
2226 ["Schedule" org-agenda-schedule t]
2227 ["Set Deadline" org-agenda-deadline t]
2228 "--"
2229 ["Mark item" org-agenda-action :active t :keys "k m"]
2230 ["Show mark item" org-agenda-action :active t :keys "k v"]
2231 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2232 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2233 "--"
2234 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2235 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2236 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2237 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2238 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2239 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2240 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2241 ("Clock and Effort"
2242 ["Clock in" org-agenda-clock-in t]
2243 ["Clock out" org-agenda-clock-out t]
2244 ["Clock cancel" org-agenda-clock-cancel t]
2245 ["Goto running clock" org-clock-goto t]
2246 "--"
2247 ["Set Effort" org-agenda-set-effort t]
2248 ["Change clocked effort" org-clock-modify-effort-estimate
2249 (org-clock-is-active)])
2250 ("Priority"
2251 ["Set Priority" org-agenda-priority t]
2252 ["Increase Priority" org-agenda-priority-up t]
2253 ["Decrease Priority" org-agenda-priority-down t]
2254 ["Show Priority" org-agenda-show-priority t])
2255 ("Calendar/Diary"
2256 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2257 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2258 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2259 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2260 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2261 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2262 "--"
2263 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2264 "--"
2265 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2266 "--"
2267 ("MobileOrg"
2268 ["Push Files and Views" org-mobile-push t]
2269 ["Get Captured and Flagged" org-mobile-pull t]
2270 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2271 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2272 "--"
2273 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2274 "--"
2275 ["Quit" org-agenda-quit t]
2276 ["Exit and Release Buffers" org-agenda-exit t]
2279 ;;; Agenda undo
2281 (defvar org-agenda-allow-remote-undo t
2282 "Non-nil means allow remote undo from the agenda buffer.")
2283 (defvar org-agenda-undo-list nil
2284 "List of undoable operations in the agenda since last refresh.")
2285 (defvar org-agenda-undo-has-started-in nil
2286 "Buffers that have already seen `undo-start' in the current undo sequence.")
2287 (defvar org-agenda-pending-undo-list nil
2288 "In a series of undo commands, this is the list of remaining undo items.")
2290 (defun org-agenda-undo ()
2291 "Undo a remote editing step in the agenda.
2292 This undoes changes both in the agenda buffer and in the remote buffer
2293 that have been changed along."
2294 (interactive)
2295 (or org-agenda-allow-remote-undo
2296 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2297 (if (not (eq this-command last-command))
2298 (setq org-agenda-undo-has-started-in nil
2299 org-agenda-pending-undo-list org-agenda-undo-list))
2300 (if (not org-agenda-pending-undo-list)
2301 (error "No further undo information"))
2302 (let* ((entry (pop org-agenda-pending-undo-list))
2303 buf line cmd rembuf)
2304 (setq cmd (pop entry) line (pop entry))
2305 (setq rembuf (nth 2 entry))
2306 (org-with-remote-undo rembuf
2307 (while (bufferp (setq buf (pop entry)))
2308 (if (pop entry)
2309 (with-current-buffer buf
2310 (let ((last-undo-buffer buf)
2311 (inhibit-read-only t))
2312 (unless (memq buf org-agenda-undo-has-started-in)
2313 (push buf org-agenda-undo-has-started-in)
2314 (make-local-variable 'pending-undo-list)
2315 (undo-start))
2316 (while (and pending-undo-list
2317 (listp pending-undo-list)
2318 (not (car pending-undo-list)))
2319 (pop pending-undo-list))
2320 (undo-more 1))))))
2321 (org-goto-line line)
2322 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2324 (defun org-verify-change-for-undo (l1 l2)
2325 "Verify that a real change occurred between the undo lists L1 and L2."
2326 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2327 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2328 (not (eq l1 l2)))
2330 ;;; Agenda dispatch
2332 (defvar org-agenda-restrict nil)
2333 (defvar org-agenda-restrict-begin (make-marker))
2334 (defvar org-agenda-restrict-end (make-marker))
2335 (defvar org-agenda-last-dispatch-buffer nil)
2336 (defvar org-agenda-overriding-restriction nil)
2338 (defcustom org-agenda-custom-commands-contexts nil
2339 "Alist of custom agenda keys and contextual rules.
2341 For example, if you have a custom agenda command \"p\" and you
2342 want this command to be accessible only from plain text files,
2343 use this:
2345 '((\"p\" (in-file . \"\\.txt\")))
2347 Here are the available contexts definitions:
2349 in-file: command displayed only in matching files
2350 in-mode: command displayed only in matching modes
2351 not-in-file: command not displayed in matching files
2352 not-in-mode: command not displayed in matching modes
2353 [function]: a custom function taking no argument
2355 If you define several checks, the agenda command will be
2356 accessible if there is at least one valid check.
2358 You can also bind a key to another agenda custom command
2359 depending on contextual rules.
2361 '((\"p\" \"q\" (in-file . \"\\.txt\")))
2363 Here it means: in .txt files, use \"p\" as the key for the
2364 agenda command otherwise associated with \"q\". (The command
2365 originally associated with \"q\" is not displayed to avoid
2366 duplicates.)"
2367 ;; :version "24.3"
2368 :group 'org-agenda-custom-commands
2369 :type '(repeat (list :tag "Rule"
2370 (string :tag " Agenda key")
2371 (string :tag "Replace by command")
2372 (repeat :tag "Available when"
2373 (choice
2374 (cons :tag "Condition"
2375 (choice
2376 (const :tag "In file" in-file)
2377 (const :tag "Not in file" not-in-file)
2378 (const :tag "In mode" in-mode)
2379 (const :tag "Not in mode" not-in-mode))
2380 (regexp))
2381 (function :tag "Custom function"))))))
2383 ;;;###autoload
2384 (defun org-agenda (&optional arg keys restriction)
2385 "Dispatch agenda commands to collect entries to the agenda buffer.
2386 Prompts for a command to execute. Any prefix arg will be passed
2387 on to the selected command. The default selections are:
2389 a Call `org-agenda-list' to display the agenda for current day or week.
2390 t Call `org-todo-list' to display the global todo list.
2391 T Call `org-todo-list' to display the global todo list, select only
2392 entries with a specific TODO keyword (the user gets a prompt).
2393 m Call `org-tags-view' to display headlines with tags matching
2394 a condition (the user is prompted for the condition).
2395 M Like `m', but select only TODO entries, no ordinary headlines.
2396 L Create a timeline for the current buffer.
2397 e Export views to associated files.
2398 s Search entries for keywords.
2399 S Search entries for keywords, only with TODO keywords.
2400 / Multi occur across all agenda files and also files listed
2401 in `org-agenda-text-search-extra-files'.
2402 < Restrict agenda commands to buffer, subtree, or region.
2403 Press several times to get the desired effect.
2404 > Remove a previous restriction.
2405 # List \"stuck\" projects.
2406 ! Configure what \"stuck\" means.
2407 C Configure custom agenda commands.
2409 More commands can be added by configuring the variable
2410 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2411 searches can be pre-defined in this way.
2413 If the current buffer is in Org-mode and visiting a file, you can also
2414 first press `<' once to indicate that the agenda should be temporarily
2415 \(until the next use of \\[org-agenda]) restricted to the current file.
2416 Pressing `<' twice means to restrict to the current subtree or region
2417 \(if active)."
2418 (interactive "P")
2419 (catch 'exit
2420 (let* ((prefix-descriptions nil)
2421 (org-agenda-buffer-name org-agenda-buffer-name)
2422 (org-agenda-window-setup (if (equal (buffer-name)
2423 org-agenda-buffer-name)
2424 'current-window
2425 org-agenda-window-setup))
2426 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2427 (org-agenda-custom-commands
2428 ;; normalize different versions
2429 (delq nil
2430 (mapcar
2431 (lambda (x)
2432 (cond ((stringp (cdr x))
2433 (push x prefix-descriptions)
2434 nil)
2435 ((stringp (nth 1 x)) x)
2436 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2437 (t (cons (car x) (cons "" (cdr x))))))
2438 org-agenda-custom-commands)))
2439 (org-agenda-custom-commands
2440 (org-contextualize-keys
2441 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2442 (buf (current-buffer))
2443 (bfn (buffer-file-name (buffer-base-buffer)))
2444 entry key type match lprops ans)
2445 ;; Turn off restriction unless there is an overriding one,
2446 (unless org-agenda-overriding-restriction
2447 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2448 ;; There is a request to keep the file list in place
2449 (put 'org-agenda-files 'org-restrict nil))
2450 (setq org-agenda-restrict nil)
2451 (move-marker org-agenda-restrict-begin nil)
2452 (move-marker org-agenda-restrict-end nil))
2453 ;; Delete old local properties
2454 (put 'org-agenda-redo-command 'org-lprops nil)
2455 ;; Delete previously set last-arguments
2456 (put 'org-agenda-redo-command 'last-args nil)
2457 ;; Remember where this call originated
2458 (setq org-agenda-last-dispatch-buffer (current-buffer))
2459 (unless keys
2460 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2461 keys (car ans)
2462 restriction (cdr ans)))
2463 ;; Establish the restriction, if any
2464 (when (and (not org-agenda-overriding-restriction) restriction)
2465 (put 'org-agenda-files 'org-restrict (list bfn))
2466 (cond
2467 ((eq restriction 'region)
2468 (setq org-agenda-restrict t)
2469 (move-marker org-agenda-restrict-begin (region-beginning))
2470 (move-marker org-agenda-restrict-end (region-end)))
2471 ((eq restriction 'subtree)
2472 (save-excursion
2473 (setq org-agenda-restrict t)
2474 (org-back-to-heading t)
2475 (move-marker org-agenda-restrict-begin (point))
2476 (move-marker org-agenda-restrict-end
2477 (progn (org-end-of-subtree t)))))))
2479 ;; For example the todo list should not need it (but does...)
2480 (cond
2481 ((setq entry (assoc keys org-agenda-custom-commands))
2482 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2483 (progn
2484 (setq type (nth 2 entry) match (eval (nth 3 entry))
2485 lprops (nth 4 entry))
2486 ;; If we have sticky agenda buffers, set a name for the buffer,
2487 ;; depending on the invoking keys. The user may still set this
2488 ;; as a command option, which will overwrite what we do here.
2489 (if org-agenda-sticky
2490 (setq org-agenda-buffer-name
2491 (or (and (stringp match) (format "*Org Agenda(%s:%s)*" keys match))
2492 (format "*Org Agenda(%s)*" keys))))
2493 (put 'org-agenda-redo-command 'org-lprops lprops)
2494 (cond
2495 ((eq type 'agenda)
2496 (org-let lprops '(org-agenda-list current-prefix-arg)))
2497 ((eq type 'alltodo)
2498 (org-let lprops '(org-todo-list current-prefix-arg)))
2499 ((eq type 'search)
2500 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2501 ((eq type 'stuck)
2502 (org-let lprops '(org-agenda-list-stuck-projects
2503 current-prefix-arg)))
2504 ((eq type 'tags)
2505 (org-let lprops '(org-tags-view current-prefix-arg match)))
2506 ((eq type 'tags-todo)
2507 (org-let lprops '(org-tags-view '(4) match)))
2508 ((eq type 'todo)
2509 (org-let lprops '(org-todo-list match)))
2510 ((eq type 'tags-tree)
2511 (org-check-for-org-mode)
2512 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2513 ((eq type 'todo-tree)
2514 (org-check-for-org-mode)
2515 (org-let lprops
2516 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2517 (regexp-quote match) "\\>"))))
2518 ((eq type 'occur-tree)
2519 (org-check-for-org-mode)
2520 (org-let lprops '(org-occur match)))
2521 ((functionp type)
2522 (org-let lprops '(funcall type match)))
2523 ((fboundp type)
2524 (org-let lprops '(funcall type match)))
2525 (t (error "Invalid custom agenda command type %s" type))))
2526 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2527 ((equal keys "C")
2528 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2529 (customize-variable 'org-agenda-custom-commands))
2530 ((equal keys "a") (call-interactively 'org-agenda-list))
2531 ((equal keys "s") (call-interactively 'org-search-view))
2532 ((equal keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2533 ((equal keys "t") (call-interactively 'org-todo-list))
2534 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2535 ((equal keys "m") (call-interactively 'org-tags-view))
2536 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2537 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2538 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2539 (org-add-hook
2540 'post-command-hook
2541 (lambda ()
2542 (unless (current-message)
2543 (let* ((m (org-agenda-get-any-marker))
2544 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2545 (when note
2546 (message (concat
2547 "FLAGGING-NOTE ([?] for more info): "
2548 (org-add-props
2549 (replace-regexp-in-string
2550 "\\\\n" "//"
2551 (copy-sequence note))
2552 nil 'face 'org-warning)))))))
2553 t t))
2554 ((equal keys "L")
2555 (unless (derived-mode-p 'org-mode)
2556 (error "This is not an Org-mode file"))
2557 (unless restriction
2558 (put 'org-agenda-files 'org-restrict (list bfn))
2559 (org-call-with-arg 'org-timeline arg)))
2560 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2561 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2562 ((equal keys "!") (customize-variable 'org-stuck-projects))
2563 (t (error "Invalid agenda key"))))))
2565 (defun org-agenda-append-agenda ()
2566 "Append another agenda view to the current one.
2567 This function allows interactive building of block agendas.
2568 Agenda views are separated by `org-agenda-block-separator'."
2569 (interactive)
2570 (unless (string= (buffer-name) org-agenda-buffer-name)
2571 (error "Can only append from within agenda buffer"))
2572 (let ((org-agenda-multi t))
2573 (org-agenda)
2574 (widen)))
2576 (defun org-agenda-normalize-custom-commands (cmds)
2577 (delq nil
2578 (mapcar
2579 (lambda (x)
2580 (cond ((stringp (cdr x)) nil)
2581 ((stringp (nth 1 x)) x)
2582 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2583 (t (cons (car x) (cons "" (cdr x))))))
2584 cmds)))
2586 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2587 "The user interface for selecting an agenda command."
2588 (catch 'exit
2589 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2590 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2591 (region-p (org-region-active-p))
2592 (custom org-agenda-custom-commands)
2593 (selstring "")
2594 restriction second-time
2595 c entry key type match prefixes rmheader header-end custom1 desc
2596 line lines left right n n1)
2597 (save-window-excursion
2598 (delete-other-windows)
2599 (org-switch-to-buffer-other-window " *Agenda Commands*")
2600 (erase-buffer)
2601 (insert (eval-when-compile
2602 (let ((header
2603 "Press key for an agenda command: < Buffer, subtree/region restriction
2604 -------------------------------- > Remove restriction
2605 a Agenda for current week or day e Export agenda views
2606 t List of all TODO entries T Entries with special TODO kwd
2607 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2608 s Search for keywords S Like s, but only TODO entries
2609 L Timeline for current buffer # List stuck projects (!=configure)
2610 / Multi-occur C Configure custom agenda commands
2611 ? Find :FLAGGED: entries * Toggle sticky agenda views
2613 (start 0))
2614 (while (string-match
2615 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2616 header start)
2617 (setq start (match-end 0))
2618 (add-text-properties (match-beginning 2) (match-end 2)
2619 '(face bold) header))
2620 header)))
2621 (setq header-end (move-marker (make-marker) (point)))
2622 (while t
2623 (setq custom1 custom)
2624 (when (eq rmheader t)
2625 (org-goto-line 1)
2626 (re-search-forward ":" nil t)
2627 (delete-region (match-end 0) (point-at-eol))
2628 (forward-char 1)
2629 (looking-at "-+")
2630 (delete-region (match-end 0) (point-at-eol))
2631 (move-marker header-end (match-end 0)))
2632 (goto-char header-end)
2633 (delete-region (point) (point-max))
2635 ;; Produce all the lines that describe custom commands and prefixes
2636 (setq lines nil)
2637 (while (setq entry (pop custom1))
2638 (setq key (car entry) desc (nth 1 entry)
2639 type (nth 2 entry)
2640 match (nth 3 entry))
2641 (if (> (length key) 1)
2642 (add-to-list 'prefixes (string-to-char key))
2643 (setq line
2644 (format
2645 "%-4s%-14s"
2646 (org-add-props (copy-sequence key)
2647 '(face bold))
2648 (cond
2649 ((string-match "\\S-" desc) desc)
2650 ((eq type 'agenda) "Agenda for current week or day")
2651 ((eq type 'alltodo) "List of all TODO entries")
2652 ((eq type 'search) "Word search")
2653 ((eq type 'stuck) "List of stuck projects")
2654 ((eq type 'todo) "TODO keyword")
2655 ((eq type 'tags) "Tags query")
2656 ((eq type 'tags-todo) "Tags (TODO)")
2657 ((eq type 'tags-tree) "Tags tree")
2658 ((eq type 'todo-tree) "TODO kwd tree")
2659 ((eq type 'occur-tree) "Occur tree")
2660 ((functionp type) (if (symbolp type)
2661 (symbol-name type)
2662 "Lambda expression"))
2663 (t "???"))))
2664 (if org-agenda-menu-show-matcher
2665 (setq line
2666 (concat line ": "
2667 (cond
2668 ((stringp match)
2669 (setq match (copy-sequence match))
2670 (org-add-props match nil 'face 'org-warning))
2671 (match
2672 (format "set of %d commands" (length match)))
2673 (t ""))))
2674 (if (org-string-nw-p match)
2675 (add-text-properties
2676 0 (length line) (list 'help-echo
2677 (concat "Matcher: "match)) line)))
2678 (push line lines)))
2679 (setq lines (nreverse lines))
2680 (when prefixes
2681 (mapc (lambda (x)
2682 (push
2683 (format "%s %s"
2684 (org-add-props (char-to-string x)
2685 nil 'face 'bold)
2686 (or (cdr (assoc (concat selstring
2687 (char-to-string x))
2688 prefix-descriptions))
2689 "Prefix key"))
2690 lines))
2691 prefixes))
2693 ;; Check if we should display in two columns
2694 (if org-agenda-menu-two-columns
2695 (progn
2696 (setq n (length lines)
2697 n1 (+ (/ n 2) (mod n 2))
2698 right (nthcdr n1 lines)
2699 left (copy-sequence lines))
2700 (setcdr (nthcdr (1- n1) left) nil))
2701 (setq left lines right nil))
2702 (while left
2703 (insert "\n" (pop left))
2704 (when right
2705 (if (< (current-column) 40)
2706 (move-to-column 40 t)
2707 (insert " "))
2708 (insert (pop right))))
2710 ;; Make the window the right size
2711 (goto-char (point-min))
2712 (if second-time
2713 (if (not (pos-visible-in-window-p (point-max)))
2714 (org-fit-window-to-buffer))
2715 (setq second-time t)
2716 (org-fit-window-to-buffer))
2718 ;; Ask for selection
2719 (message "Press key for agenda command%s:"
2720 (if (or restrict-ok org-agenda-overriding-restriction)
2721 (if org-agenda-overriding-restriction
2722 " (restriction lock active)"
2723 (if restriction
2724 (format " (restricted to %s)" restriction)
2725 " (unrestricted)"))
2726 ""))
2727 (setq c (read-char-exclusive))
2728 (message "")
2729 (cond
2730 ((assoc (char-to-string c) custom)
2731 (setq selstring (concat selstring (char-to-string c)))
2732 (throw 'exit (cons selstring restriction)))
2733 ((memq c prefixes)
2734 (setq selstring (concat selstring (char-to-string c))
2735 prefixes nil
2736 rmheader (or rmheader t)
2737 custom (delq nil (mapcar
2738 (lambda (x)
2739 (if (or (= (length (car x)) 1)
2740 (/= (string-to-char (car x)) c))
2742 (cons (substring (car x) 1) (cdr x))))
2743 custom))))
2744 ((eq c ?*)
2745 (call-interactively 'org-toggle-sticky-agenda)
2746 (sit-for 2))
2747 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2748 (message "Restriction is only possible in Org-mode buffers")
2749 (ding) (sit-for 1))
2750 ((eq c ?1)
2751 (org-agenda-remove-restriction-lock 'noupdate)
2752 (setq restriction 'buffer))
2753 ((eq c ?0)
2754 (org-agenda-remove-restriction-lock 'noupdate)
2755 (setq restriction (if region-p 'region 'subtree)))
2756 ((eq c ?<)
2757 (org-agenda-remove-restriction-lock 'noupdate)
2758 (setq restriction
2759 (cond
2760 ((eq restriction 'buffer)
2761 (if region-p 'region 'subtree))
2762 ((memq restriction '(subtree region))
2763 nil)
2764 (t 'buffer))))
2765 ((eq c ?>)
2766 (org-agenda-remove-restriction-lock 'noupdate)
2767 (setq restriction nil))
2768 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2769 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2770 ((and (> (length selstring) 0) (eq c ?\d))
2771 (delete-window)
2772 (org-agenda-get-restriction-and-command prefix-descriptions))
2774 ((equal c ?q) (error "Abort"))
2775 (t (error "Invalid key %c" c))))))))
2777 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2778 (defvar org-agenda-last-arguments nil
2779 "The arguments of the previous call to `org-agenda'.")
2780 (defvar org-agenda-overriding-cmd nil) ; Dynamically scoped
2781 (defvar org-agenda-multi-multiple-agenda nil)
2782 (defvar org-agenda-multi-current-cmd nil)
2783 (defvar org-agenda-multi-overriding-arguments nil)
2784 (defun org-agenda-run-series (name series)
2785 (org-let (nth 1 series) '(org-agenda-prepare name))
2786 ;; We need to reset agenda markers here, because when constructing a
2787 ;; block agenda, the individual blocks do not do that.
2788 (org-agenda-reset-markers)
2789 (let* ((org-agenda-multi t)
2790 (redo (list 'org-agenda-run-series name (list 'quote series)))
2791 (cmds (car series))
2792 (gprops (nth 1 series))
2793 match ;; The byte compiler incorrectly complains about this. Keep it!
2794 cmd type lprops)
2795 (setq org-agenda-multi-multiple-agenda
2796 (< 1 (length
2797 (delq nil (mapcar (lambda(c) (eq (car c) 'agenda)) cmds)))))
2798 (while (setq cmd (pop cmds))
2799 (setq org-agenda-multi-current-cmd cmd
2800 type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2801 (let ((org-agenda-overriding-arguments
2802 (cond ((not org-agenda-multi-multiple-agenda)
2803 org-agenda-multi-overriding-arguments)
2804 ((eq org-agenda-overriding-cmd cmd)
2805 org-agenda-overriding-arguments))))
2806 (cond
2807 ((eq type 'agenda)
2808 (org-let2 gprops lprops
2809 '(call-interactively 'org-agenda-list)))
2810 ((eq type 'alltodo)
2811 (org-let2 gprops lprops
2812 '(call-interactively 'org-todo-list)))
2813 ((eq type 'search)
2814 (org-let2 gprops lprops
2815 '(org-search-view current-prefix-arg match nil)))
2816 ((eq type 'stuck)
2817 (org-let2 gprops lprops
2818 '(call-interactively 'org-agenda-list-stuck-projects)))
2819 ((eq type 'tags)
2820 (org-let2 gprops lprops
2821 '(org-tags-view current-prefix-arg match)))
2822 ((eq type 'tags-todo)
2823 (org-let2 gprops lprops
2824 '(org-tags-view '(4) match)))
2825 ((eq type 'todo)
2826 (org-let2 gprops lprops
2827 '(org-todo-list match)))
2828 ((fboundp type)
2829 (org-let2 gprops lprops
2830 '(funcall type match)))
2831 (t (error "Invalid type in command series")))))
2832 (widen)
2833 (setq org-agenda-multi-current-cmd nil)
2834 (setq org-agenda-redo-command redo)
2835 (goto-char (point-min)))
2836 (org-fit-agenda-window)
2837 (org-let (nth 1 series) '(org-finalize-agenda)))
2839 ;;;###autoload
2840 (defmacro org-batch-agenda (cmd-key &rest parameters)
2841 "Run an agenda command in batch mode and send the result to STDOUT.
2842 If CMD-KEY is a string of length 1, it is used as a key in
2843 `org-agenda-custom-commands' and triggers this command. If it is a
2844 longer string it is used as a tags/todo match string.
2845 Parameters are alternating variable names and values that will be bound
2846 before running the agenda command."
2847 (org-eval-in-environment (org-make-parameter-alist parameters)
2848 (if (> (length cmd-key) 2)
2849 (org-tags-view nil cmd-key)
2850 (org-agenda nil cmd-key)))
2851 (set-buffer org-agenda-buffer-name)
2852 (princ (buffer-string)))
2853 (def-edebug-spec org-batch-agenda (form &rest sexp))
2855 (defvar org-agenda-info nil)
2857 ;;;###autoload
2858 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2859 "Run an agenda command in batch mode and send the result to STDOUT.
2860 If CMD-KEY is a string of length 1, it is used as a key in
2861 `org-agenda-custom-commands' and triggers this command. If it is a
2862 longer string it is used as a tags/todo match string.
2863 Parameters are alternating variable names and values that will be bound
2864 before running the agenda command.
2866 The output gives a line for each selected agenda item. Each
2867 item is a list of comma-separated values, like this:
2869 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2871 category The category of the item
2872 head The headline, without TODO kwd, TAGS and PRIORITY
2873 type The type of the agenda entry, can be
2874 todo selected in TODO match
2875 tagsmatch selected in tags match
2876 diary imported from diary
2877 deadline a deadline on given date
2878 scheduled scheduled on given date
2879 timestamp entry has timestamp on given date
2880 closed entry was closed on given date
2881 upcoming-deadline warning about deadline
2882 past-scheduled forwarded scheduled item
2883 block entry has date block including g. date
2884 todo The todo keyword, if any
2885 tags All tags including inherited ones, separated by colons
2886 date The relevant date, like 2007-2-14
2887 time The time, like 15:00-16:50
2888 extra Sting with extra planning info
2889 priority-l The priority letter if any was given
2890 priority-n The computed numerical priority
2891 agenda-day The day in the agenda where this is listed"
2892 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2893 (org-make-parameter-alist parameters))
2894 (if (> (length cmd-key) 2)
2895 (org-tags-view nil cmd-key)
2896 (org-agenda nil cmd-key)))
2897 (set-buffer org-agenda-buffer-name)
2898 (let* ((lines (org-split-string (buffer-string) "\n"))
2899 line)
2900 (while (setq line (pop lines))
2901 (catch 'next
2902 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2903 (setq org-agenda-info
2904 (org-fix-agenda-info (text-properties-at 0 line)))
2905 (princ
2906 (mapconcat 'org-agenda-export-csv-mapper
2907 '(org-category txt type todo tags date time extra
2908 priority-letter priority agenda-day)
2909 ","))
2910 (princ "\n")))))
2911 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2913 (defun org-fix-agenda-info (props)
2914 "Make sure all properties on an agenda item have a canonical form.
2915 This ensures the export commands can easily use it."
2916 (let (tmp re)
2917 (when (setq tmp (plist-get props 'tags))
2918 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2919 (when (setq tmp (plist-get props 'date))
2920 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2921 (let ((calendar-date-display-form '(year "-" month "-" day)))
2922 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2924 (setq tmp (calendar-date-string tmp)))
2925 (setq props (plist-put props 'date tmp)))
2926 (when (setq tmp (plist-get props 'day))
2927 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2928 (let ((calendar-date-display-form '(year "-" month "-" day)))
2929 (setq tmp (calendar-date-string tmp)))
2930 (setq props (plist-put props 'day tmp))
2931 (setq props (plist-put props 'agenda-day tmp)))
2932 (when (setq tmp (plist-get props 'txt))
2933 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2934 (plist-put props 'priority-letter (match-string 1 tmp))
2935 (setq tmp (replace-match "" t t tmp)))
2936 (when (and (setq re (plist-get props 'org-todo-regexp))
2937 (setq re (concat "\\`\\.*" re " ?"))
2938 (string-match re tmp))
2939 (plist-put props 'todo (match-string 1 tmp))
2940 (setq tmp (replace-match "" t t tmp)))
2941 (plist-put props 'txt tmp)))
2942 props)
2944 (defun org-agenda-export-csv-mapper (prop)
2945 (let ((res (plist-get org-agenda-info prop)))
2946 (setq res
2947 (cond
2948 ((not res) "")
2949 ((stringp res) res)
2950 (t (prin1-to-string res))))
2951 (while (string-match "," res)
2952 (setq res (replace-match ";" t t res)))
2953 (org-trim res)))
2955 ;;;###autoload
2956 (defun org-store-agenda-views (&rest parameters)
2957 (interactive)
2958 (eval (list 'org-batch-store-agenda-views)))
2960 ;;;###autoload
2961 (defmacro org-batch-store-agenda-views (&rest parameters)
2962 "Run all custom agenda commands that have a file argument."
2963 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2964 (pop-up-frames nil)
2965 (dir default-directory)
2966 (pars (org-make-parameter-alist parameters))
2967 cmd thiscmdkey thiscmdcmd files opts cmd-or-set bufname)
2968 (save-window-excursion
2969 (while cmds
2970 (setq cmd (pop cmds)
2971 thiscmdkey (car cmd)
2972 thiscmdcmd (cdr cmd)
2973 match (nth 2 thiscmdcmd)
2974 bufname (if org-agenda-sticky
2975 (or (and (stringp match)
2976 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
2977 (format "*Org Agenda(%s)*" thiscmdkey))
2978 org-agenda-buffer-name)
2979 cmd-or-set (nth 2 cmd)
2980 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2981 files (nth (if (listp cmd-or-set) 4 5) cmd))
2982 (if (stringp files) (setq files (list files)))
2983 (when files
2984 (org-eval-in-environment (append org-agenda-exporter-settings
2985 opts pars)
2986 (org-agenda nil thiscmdkey))
2987 (set-buffer bufname)
2988 (while files
2989 (org-eval-in-environment (append org-agenda-exporter-settings
2990 opts pars)
2991 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
2992 (and (get-buffer bufname)
2993 (kill-buffer bufname)))))))
2994 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2996 (defun org-agenda-mark-header-line (pos)
2997 "Mark the line at POS as an agenda structure header."
2998 (save-excursion
2999 (goto-char pos)
3000 (put-text-property (point-at-bol) (point-at-eol)
3001 'org-agenda-structural-header t)
3002 (when org-agenda-multi-current-cmd
3003 (put-text-property (point-at-bol) (point-at-eol)
3004 'org-agenda-cmd org-agenda-multi-current-cmd))
3005 (when org-agenda-multi-multiple-agenda
3006 (put-text-property (point-at-bol) (point-at-eol)
3007 'org-agenda-overriding-arguments
3008 org-agenda-overriding-arguments)
3009 (put-text-property (point-at-bol) (point-at-eol)
3010 'org-agenda-current-span
3011 org-agenda-current-span)
3012 (put-text-property (point-at-bol) (point-at-eol)
3013 'org-agenda-last-arguments
3014 org-agenda-last-arguments))
3015 (when org-agenda-title-append
3016 (put-text-property (point-at-bol) (point-at-eol)
3017 'org-agenda-title-append org-agenda-title-append))))
3019 (defvar org-mobile-creating-agendas)
3020 (defvar org-agenda-write-buffer-name "Agenda View")
3021 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3022 "Write the current buffer (an agenda view) as a file.
3023 Depending on the extension of the file name, plain text (.txt),
3024 HTML (.html or .htm) or Postscript (.ps) is produced.
3025 If the extension is .ics, run icalendar export over all files used
3026 to construct the agenda and limit the export to entries listed in the
3027 agenda now.
3028 With prefix argument OPEN, open the new file immediately.
3029 If NOSETTINGS is given, do not scope the settings of
3030 `org-agenda-exporter-settings' into the export commands. This is used when
3031 the settings have already been scoped and we do not wish to overrule other,
3032 higher priority settings.
3033 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3034 (interactive "FWrite agenda to file: \nP")
3035 (if (not (file-writable-p file))
3036 (error "Cannot write agenda to file %s" file))
3037 (org-let (if nosettings nil org-agenda-exporter-settings)
3038 '(save-excursion
3039 (save-window-excursion
3040 (org-agenda-mark-filtered-text)
3041 (let ((bs (copy-sequence (buffer-string))) beg)
3042 (org-agenda-unmark-filtered-text)
3043 (with-temp-buffer
3044 (rename-buffer org-agenda-write-buffer-name t)
3045 (set-buffer-modified-p nil)
3046 (insert bs)
3047 (org-agenda-remove-marked-text 'org-filtered)
3048 (while (setq beg (text-property-any (point-min) (point-max)
3049 'org-filtered t))
3050 (delete-region
3051 beg (or (next-single-property-change beg 'org-filtered)
3052 (point-max))))
3053 (run-hooks 'org-agenda-before-write-hook)
3054 (cond
3055 ((org-bound-and-true-p org-mobile-creating-agendas)
3056 (org-mobile-write-agenda-for-mobile file))
3057 ((string-match "\\.html?\\'" file)
3058 (require 'htmlize)
3059 (set-buffer (htmlize-buffer (current-buffer)))
3060 (when (and org-agenda-export-html-style
3061 (string-match "<style>" org-agenda-export-html-style))
3062 ;; replace <style> section with org-agenda-export-html-style
3063 (goto-char (point-min))
3064 (kill-region (- (search-forward "<style") 6)
3065 (search-forward "</style>"))
3066 (insert org-agenda-export-html-style))
3067 (write-file file)
3068 (kill-buffer (current-buffer))
3069 (message "HTML written to %s" file))
3070 ((string-match "\\.ps\\'" file)
3071 (require 'ps-print)
3072 (ps-print-buffer-with-faces file)
3073 (message "Postscript written to %s" file))
3074 ((string-match "\\.pdf\\'" file)
3075 (require 'ps-print)
3076 (ps-print-buffer-with-faces
3077 (concat (file-name-sans-extension file) ".ps"))
3078 (call-process "ps2pdf" nil nil nil
3079 (expand-file-name
3080 (concat (file-name-sans-extension file) ".ps"))
3081 (expand-file-name file))
3082 (delete-file (concat (file-name-sans-extension file) ".ps"))
3083 (message "PDF written to %s" file))
3084 ((string-match "\\.ics\\'" file)
3085 (require 'org-icalendar)
3086 (let ((org-agenda-marker-table
3087 (org-create-marker-find-array
3088 (org-agenda-collect-markers)))
3089 (org-icalendar-verify-function 'org-check-agenda-marker-table)
3090 (org-combined-agenda-icalendar-file file))
3091 (apply 'org-export-icalendar 'combine
3092 (org-agenda-files nil 'ifmode))))
3094 (let ((bs (buffer-string)))
3095 (find-file file)
3096 (erase-buffer)
3097 (insert bs)
3098 (save-buffer 0)
3099 (kill-buffer (current-buffer))
3100 (message "Plain text written to %s" file))))))))
3101 (set-buffer (or agenda-bufname
3102 (and (called-interactively-p 'any) (buffer-name))
3103 org-agenda-buffer-name)))
3104 (when open (org-open-file file)))
3106 (defvar org-agenda-tag-filter-overlays nil)
3107 (defvar org-agenda-cat-filter-overlays nil)
3109 (defun org-agenda-mark-filtered-text ()
3110 "Mark all text hidden by filtering with a text property."
3111 (let ((inhibit-read-only t))
3112 (mapc
3113 (lambda (o)
3114 (when (equal (overlay-buffer o) (current-buffer))
3115 (put-text-property
3116 (overlay-start o) (overlay-end o)
3117 'org-filtered t)))
3118 (append org-agenda-tag-filter-overlays
3119 org-agenda-cat-filter-overlays))))
3121 (defun org-agenda-unmark-filtered-text ()
3122 "Remove the filtering text property."
3123 (let ((inhibit-read-only t))
3124 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3126 (defun org-agenda-remove-marked-text (property &optional value)
3127 "Delete all text marked with VALUE of PROPERTY.
3128 VALUE defaults to t."
3129 (let (beg)
3130 (setq value (or value t))
3131 (while (setq beg (text-property-any (point-min) (point-max)
3132 property value))
3133 (delete-region
3134 beg (or (next-single-property-change beg 'org-filtered)
3135 (point-max))))))
3137 (defun org-agenda-add-entry-text ()
3138 "Add entry text to agenda lines.
3139 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3140 entry text following headings shown in the agenda.
3141 Drawers will be excluded, also the line with scheduling/deadline info."
3142 (when (and (> org-agenda-add-entry-text-maxlines 0)
3143 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3144 (let (m txt)
3145 (goto-char (point-min))
3146 (while (not (eobp))
3147 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3148 (beginning-of-line 2)
3149 (setq txt (org-agenda-get-some-entry-text
3150 m org-agenda-add-entry-text-maxlines " > "))
3151 (end-of-line 1)
3152 (if (string-match "\\S-" txt)
3153 (insert "\n" txt)
3154 (or (eobp) (forward-char 1))))))))
3156 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3157 &rest keep)
3158 "Extract entry text from MARKER, at most N-LINES lines.
3159 This will ignore drawers etc, just get the text.
3160 If INDENT is given, prefix every line with this string. If KEEP is
3161 given, it is a list of symbols, defining stuff that should not be
3162 removed from the entry content. Currently only `planning' is allowed here."
3163 (let (txt drawer-re kwd-time-re ind)
3164 (save-excursion
3165 (with-current-buffer (marker-buffer marker)
3166 (if (not (derived-mode-p 'org-mode))
3167 (setq txt "")
3168 (save-excursion
3169 (save-restriction
3170 (widen)
3171 (goto-char marker)
3172 (end-of-line 1)
3173 (setq txt (buffer-substring
3174 (min (1+ (point)) (point-max))
3175 (progn (outline-next-heading) (point)))
3176 drawer-re org-drawer-regexp
3177 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3178 ".*\n?"))
3179 (with-temp-buffer
3180 (insert txt)
3181 (when org-agenda-add-entry-text-descriptive-links
3182 (goto-char (point-min))
3183 (while (org-activate-bracket-links (point-max))
3184 (add-text-properties (match-beginning 0) (match-end 0)
3185 '(face org-link))))
3186 (goto-char (point-min))
3187 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3188 (set-text-properties (match-beginning 0) (match-end 0)
3189 nil))
3190 (goto-char (point-min))
3191 (while (re-search-forward drawer-re nil t)
3192 (delete-region
3193 (match-beginning 0)
3194 (progn (re-search-forward
3195 "^[ \t]*:END:.*\n?" nil 'move)
3196 (point))))
3197 (unless (member 'planning keep)
3198 (goto-char (point-min))
3199 (while (re-search-forward kwd-time-re nil t)
3200 (replace-match "")))
3201 (goto-char (point-min))
3202 (when org-agenda-entry-text-exclude-regexps
3203 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3204 (while (setq re (pop re-list))
3205 (goto-char (point-min))
3206 (while (re-search-forward re nil t)
3207 (replace-match "")))))
3208 (goto-char (point-max))
3209 (skip-chars-backward " \t\n")
3210 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3212 ;; find and remove min common indentation
3213 (goto-char (point-min))
3214 (untabify (point-min) (point-max))
3215 (setq ind (org-get-indentation))
3216 (while (not (eobp))
3217 (unless (looking-at "[ \t]*$")
3218 (setq ind (min ind (org-get-indentation))))
3219 (beginning-of-line 2))
3220 (goto-char (point-min))
3221 (while (not (eobp))
3222 (unless (looking-at "[ \t]*$")
3223 (move-to-column ind)
3224 (delete-region (point-at-bol) (point)))
3225 (beginning-of-line 2))
3227 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3229 (goto-char (point-min))
3230 (when indent
3231 (while (and (not (eobp)) (re-search-forward "^" nil t))
3232 (replace-match indent t t)))
3233 (goto-char (point-min))
3234 (while (looking-at "[ \t]*\n") (replace-match ""))
3235 (goto-char (point-max))
3236 (when (> (org-current-line)
3237 n-lines)
3238 (org-goto-line (1+ n-lines))
3239 (backward-char 1))
3240 (setq txt (buffer-substring (point-min) (point)))))))))
3241 txt))
3243 (defun org-agenda-collect-markers ()
3244 "Collect the markers pointing to entries in the agenda buffer."
3245 (let (m markers)
3246 (save-excursion
3247 (goto-char (point-min))
3248 (while (not (eobp))
3249 (when (setq m (or (org-get-at-bol 'org-hd-marker)
3250 (org-get-at-bol 'org-marker)))
3251 (push m markers))
3252 (beginning-of-line 2)))
3253 (nreverse markers)))
3255 (defun org-create-marker-find-array (marker-list)
3256 "Create a alist of files names with all marker positions in that file."
3257 (let (f tbl m a p)
3258 (while (setq m (pop marker-list))
3259 (setq p (marker-position m)
3260 f (buffer-file-name (or (buffer-base-buffer
3261 (marker-buffer m))
3262 (marker-buffer m))))
3263 (if (setq a (assoc f tbl))
3264 (push (marker-position m) (cdr a))
3265 (push (list f p) tbl)))
3266 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3267 tbl)))
3269 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3270 (defun org-check-agenda-marker-table ()
3271 "Check of the current entry is on the marker list."
3272 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3274 (and (setq a (assoc file org-agenda-marker-table))
3275 (save-match-data
3276 (save-excursion
3277 (org-back-to-heading t)
3278 (member (point) (cdr a)))))))
3280 (defun org-check-for-org-mode ()
3281 "Make sure current buffer is in org-mode. Error if not."
3282 (or (derived-mode-p 'org-mode)
3283 (error "Cannot execute org-mode agenda command on buffer in %s"
3284 major-mode)))
3286 (defun org-fit-agenda-window ()
3287 "Fit the window to the buffer size."
3288 (and (memq org-agenda-window-setup '(reorganize-frame))
3289 (fboundp 'fit-window-to-buffer)
3290 (org-fit-window-to-buffer
3292 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3293 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3295 ;;; Agenda prepare and finalize
3297 (defvar org-agenda-multi nil) ; dynamically scoped
3298 (defvar org-agenda-buffer-name "*Org Agenda*")
3299 (defvar org-pre-agenda-window-conf nil)
3300 (defvar org-agenda-columns-active nil)
3301 (defvar org-agenda-name nil)
3302 (defvar org-agenda-tag-filter nil)
3303 (defvar org-agenda-category-filter nil)
3304 (defvar org-agenda-top-category-filter nil)
3305 (defvar org-agenda-tag-filter-while-redo nil)
3306 (defvar org-agenda-tag-filter-preset nil
3307 "A preset of the tags filter used for secondary agenda filtering.
3308 This must be a list of strings, each string must be a single tag preceded
3309 by \"+\" or \"-\".
3310 This variable should not be set directly, but agenda custom commands can
3311 bind it in the options section. The preset filter is a global property of
3312 the entire agenda view. In a block agenda, it will not work reliably to
3313 define a filter for one of the individual blocks. You need to set it in
3314 the global options and expect it to be applied to the entire view.")
3316 (defvar org-agenda-category-filter-preset nil
3317 "A preset of the category filter used for secondary agenda filtering.
3318 This must be a list of strings, each string must be a single category
3319 preceded by \"+\" or \"-\".
3320 This variable should not be set directly, but agenda custom commands can
3321 bind it in the options section. The preset filter is a global property of
3322 the entire agenda view. In a block agenda, it will not work reliably to
3323 define a filter for one of the individual blocks. You need to set it in
3324 the global options and expect it to be applied to the entire view.")
3327 (defun org-agenda-use-sticky-p ()
3328 "Return non-nil if an agenda buffer named
3329 `org-agenda-buffer-name' exists and should be shown instead of
3330 generating a new one."
3331 (and
3332 ;; turned off by user
3333 org-agenda-sticky
3334 ;; For multi-agenda buffer already exists
3335 (not org-agenda-multi)
3336 ;; buffer found
3337 (get-buffer org-agenda-buffer-name)
3338 ;; C-u parameter is same as last call
3339 (with-current-buffer (get-buffer org-agenda-buffer-name)
3340 (and
3341 (equal current-prefix-arg
3342 org-agenda-last-prefix-arg)
3343 ;; In case user turned stickiness on, while having existing
3344 ;; Agenda buffer active, don't reuse that buffer, because it
3345 ;; does not have org variables local
3346 org-agenda-this-buffer-is-sticky))))
3348 (defun org-agenda-prepare-window (abuf)
3349 "Setup agenda buffer in the window."
3350 (let* ((awin (get-buffer-window abuf))
3351 wconf)
3352 (cond
3353 ((equal (current-buffer) abuf) nil)
3354 (awin (select-window awin))
3355 ((not (setq wconf (current-window-configuration))))
3356 ((equal org-agenda-window-setup 'current-window)
3357 (org-pop-to-buffer-same-window abuf))
3358 ((equal org-agenda-window-setup 'other-window)
3359 (org-switch-to-buffer-other-window abuf))
3360 ((equal org-agenda-window-setup 'other-frame)
3361 (switch-to-buffer-other-frame abuf))
3362 ((equal org-agenda-window-setup 'reorganize-frame)
3363 (delete-other-windows)
3364 (org-switch-to-buffer-other-window abuf)))
3365 ;; additional test in case agenda is invoked from within agenda
3366 ;; buffer via elisp link
3367 (unless (equal (current-buffer) abuf)
3368 (org-pop-to-buffer-same-window abuf))
3369 (setq org-pre-agenda-window-conf
3370 (or org-pre-agenda-window-conf wconf))))
3372 (defun org-agenda-prepare (&optional name)
3373 (if (org-agenda-use-sticky-p)
3374 (progn
3375 ;; Popup existing buffer
3376 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3377 (message "Sticky Agenda buffer, use `r' to refresh")
3378 (or org-agenda-multi (org-fit-agenda-window))
3379 (throw 'exit nil))
3380 (setq org-todo-keywords-for-agenda nil)
3381 (setq org-drawers-for-agenda nil)
3382 (unless org-agenda-persistent-filter
3383 (setq org-agenda-tag-filter nil
3384 org-agenda-category-filter nil))
3385 (put 'org-agenda-tag-filter :preset-filter
3386 org-agenda-tag-filter-preset)
3387 (put 'org-agenda-category-filter :preset-filter
3388 org-agenda-category-filter-preset)
3389 (if org-agenda-multi
3390 (progn
3391 (setq buffer-read-only nil)
3392 (goto-char (point-max))
3393 (unless (or (bobp) org-agenda-compact-blocks
3394 (not org-agenda-block-separator))
3395 (insert "\n"
3396 (if (stringp org-agenda-block-separator)
3397 org-agenda-block-separator
3398 (make-string (window-width) org-agenda-block-separator))
3399 "\n"))
3400 (narrow-to-region (point) (point-max)))
3401 (setq org-done-keywords-for-agenda nil)
3403 ;; Setting any org variables that are in org-agenda-local-vars
3404 ;; list need to be done after the prepare call
3405 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3406 (setq buffer-read-only nil)
3407 (org-agenda-reset-markers)
3408 (let ((inhibit-read-only t)) (erase-buffer))
3409 (org-agenda-mode)
3410 (setq org-agenda-buffer (current-buffer))
3411 (setq org-agenda-contributing-files nil)
3412 (setq org-agenda-columns-active nil)
3413 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3414 (setq org-todo-keywords-for-agenda
3415 (org-uniquify org-todo-keywords-for-agenda))
3416 (setq org-done-keywords-for-agenda
3417 (org-uniquify org-done-keywords-for-agenda))
3418 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3419 (setq org-agenda-last-prefix-arg current-prefix-arg)
3420 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3421 (and name (not org-agenda-name)
3422 (org-set-local 'org-agenda-name name)))
3423 (setq buffer-read-only nil)))
3425 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3426 (defun org-finalize-agenda ()
3427 "Finishing touch for the agenda buffer, called just before displaying it."
3428 (unless org-agenda-multi
3429 (save-excursion
3430 (let ((inhibit-read-only t))
3431 (goto-char (point-min))
3432 (while (org-activate-bracket-links (point-max))
3433 (add-text-properties (match-beginning 0) (match-end 0)
3434 '(face org-link)))
3435 (org-agenda-align-tags)
3436 (unless org-agenda-with-colors
3437 (remove-text-properties (point-min) (point-max) '(face nil))))
3438 (if (and (boundp 'org-agenda-overriding-columns-format)
3439 org-agenda-overriding-columns-format)
3440 (org-set-local 'org-agenda-overriding-columns-format
3441 org-agenda-overriding-columns-format))
3442 (if (and (boundp 'org-agenda-view-columns-initially)
3443 org-agenda-view-columns-initially)
3444 (org-agenda-columns))
3445 (when org-agenda-fontify-priorities
3446 (org-agenda-fontify-priorities))
3447 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3448 (org-agenda-dim-blocked-tasks))
3449 (org-agenda-mark-clocking-task)
3450 (when org-agenda-entry-text-mode
3451 (org-agenda-entry-text-hide)
3452 (org-agenda-entry-text-show))
3453 (if (functionp 'org-habit-insert-consistency-graphs)
3454 (org-habit-insert-consistency-graphs))
3455 (run-hooks 'org-finalize-agenda-hook)
3456 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3457 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3458 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3459 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3460 (org-agenda-filter-apply org-agenda-category-filter 'category))
3461 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)
3464 (defun org-agenda-mark-clocking-task ()
3465 "Mark the current clock entry in the agenda if it is present."
3466 (mapc (lambda (o)
3467 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3468 (delete-overlay o)))
3469 (overlays-in (point-min) (point-max)))
3470 (when (marker-buffer org-clock-hd-marker)
3471 (save-excursion
3472 (goto-char (point-min))
3473 (let (s ov)
3474 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3475 (goto-char s)
3476 (when (equal (org-get-at-bol 'org-hd-marker)
3477 org-clock-hd-marker)
3478 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3479 (overlay-put ov 'type 'org-agenda-clocking)
3480 (overlay-put ov 'face 'org-agenda-clocking)
3481 (overlay-put ov 'help-echo
3482 "The clock is running in this item")))))))
3484 (defun org-agenda-fontify-priorities ()
3485 "Make highest priority lines bold, and lowest italic."
3486 (interactive)
3487 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3488 (delete-overlay o)))
3489 (overlays-in (point-min) (point-max)))
3490 (save-excursion
3491 (let ((inhibit-read-only t)
3492 b e p ov h l)
3493 (goto-char (point-min))
3494 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3495 (setq h (or (get-char-property (point) 'org-highest-priority)
3496 org-highest-priority)
3497 l (or (get-char-property (point) 'org-lowest-priority)
3498 org-lowest-priority)
3499 p (string-to-char (match-string 1))
3500 b (match-beginning 0)
3501 e (if (eq org-agenda-fontify-priorities 'cookies)
3502 (match-end 0)
3503 (point-at-eol))
3504 ov (make-overlay b e))
3505 (overlay-put
3506 ov 'face
3507 (cond ((org-face-from-face-or-color
3508 'priority nil
3509 (cdr (assoc p org-priority-faces))))
3510 ((and (listp org-agenda-fontify-priorities)
3511 (org-face-from-face-or-color
3512 'priority nil
3513 (cdr (assoc p org-agenda-fontify-priorities)))))
3514 ((equal p l) 'italic)
3515 ((equal p h) 'bold)))
3516 (overlay-put ov 'org-type 'org-priority)))))
3518 (defun org-agenda-dim-blocked-tasks ()
3519 "Dim currently blocked TODO's in the agenda display."
3520 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3521 (delete-overlay o)))
3522 (overlays-in (point-min) (point-max)))
3523 (save-excursion
3524 (let ((inhibit-read-only t)
3525 (org-depend-tag-blocked nil)
3526 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3527 org-blocked-by-checkboxes
3528 invis1 b e p ov h l)
3529 (goto-char (point-min))
3530 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3531 (and pos (goto-char (1+ pos))))
3532 (setq org-blocked-by-checkboxes nil invis1 invis)
3533 (let ((marker (org-get-at-bol 'org-hd-marker)))
3534 (when (and marker
3535 (with-current-buffer (marker-buffer marker)
3536 (save-excursion (goto-char marker)
3537 (org-entry-blocked-p))))
3538 (if org-blocked-by-checkboxes (setq invis1 nil))
3539 (setq b (if invis1
3540 (max (point-min) (1- (point-at-bol)))
3541 (point-at-bol))
3542 e (point-at-eol)
3543 ov (make-overlay b e))
3544 (if invis1
3545 (overlay-put ov 'invisible t)
3546 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3547 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3549 (defvar org-agenda-skip-function nil
3550 "Function to be called at each match during agenda construction.
3551 If this function returns nil, the current match should not be skipped.
3552 Otherwise, the function must return a position from where the search
3553 should be continued.
3554 This may also be a Lisp form, it will be evaluated.
3555 Never set this variable using `setq' or so, because then it will apply
3556 to all future agenda commands. If you do want a global skipping condition,
3557 use the option `org-agenda-skip-function-global' instead.
3558 The correct usage for `org-agenda-skip-function' is to bind it with
3559 `let' to scope it dynamically into the agenda-constructing command.
3560 A good way to set it is through options in `org-agenda-custom-commands'.")
3562 (defun org-agenda-skip ()
3563 "Throw to `:skip' in places that should be skipped.
3564 Also moves point to the end of the skipped region, so that search can
3565 continue from there."
3566 (let ((p (point-at-bol)) to)
3567 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3568 (get-text-property p :org-archived)
3569 (org-end-of-subtree t)
3570 (throw :skip t))
3571 (and org-agenda-skip-comment-trees
3572 (get-text-property p :org-comment)
3573 (org-end-of-subtree t)
3574 (throw :skip t))
3575 (if (equal (char-after p) ?#) (throw :skip t))
3576 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3577 (org-agenda-skip-eval org-agenda-skip-function)))
3578 (goto-char to)
3579 (throw :skip t))))
3581 (defun org-agenda-skip-eval (form)
3582 "If FORM is a function or a list, call (or eval) is and return result.
3583 `save-excursion' and `save-match-data' are wrapped around the call, so point
3584 and match data are returned to the previous state no matter what these
3585 functions do."
3586 (let (fp)
3587 (and form
3588 (or (setq fp (functionp form))
3589 (consp form))
3590 (save-excursion
3591 (save-match-data
3592 (if fp
3593 (funcall form)
3594 (eval form)))))))
3596 (defvar org-agenda-markers nil
3597 "List of all currently active markers created by `org-agenda'.")
3598 (defvar org-agenda-last-marker-time (org-float-time)
3599 "Creation time of the last agenda marker.")
3601 (defun org-agenda-new-marker (&optional pos)
3602 "Return a new agenda marker.
3603 Org-mode keeps a list of these markers and resets them when they are
3604 no longer in use."
3605 (let ((m (copy-marker (or pos (point)))))
3606 (setq org-agenda-last-marker-time (org-float-time))
3607 (if org-agenda-buffer
3608 (with-current-buffer org-agenda-buffer
3609 (push m org-agenda-markers))
3610 (push m org-agenda-markers))
3613 (defun org-agenda-reset-markers ()
3614 "Reset markers created by `org-agenda'."
3615 (while org-agenda-markers
3616 (move-marker (pop org-agenda-markers) nil)))
3618 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3619 "Save relative positions of markers in region.
3620 This check for agenda markers in all agenda buffers currently active."
3621 (dolist (buf (buffer-list))
3622 (with-current-buffer buf
3623 (when (eq major-mode 'org-agenda-mode)
3624 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3625 org-agenda-markers)))))
3627 ;;; Entry text mode
3629 (defun org-agenda-entry-text-show-here ()
3630 "Add some text from the entry as context to the current line."
3631 (let (m txt o)
3632 (setq m (org-get-at-bol 'org-hd-marker))
3633 (unless (marker-buffer m)
3634 (error "No marker points to an entry here"))
3635 (setq txt (concat "\n" (org-no-properties
3636 (org-agenda-get-some-entry-text
3637 m org-agenda-entry-text-maxlines " > "))))
3638 (when (string-match "\\S-" txt)
3639 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3640 (overlay-put o 'evaporate t)
3641 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3642 (overlay-put o 'after-string txt))))
3644 (defun org-agenda-entry-text-show ()
3645 "Add entry context for all agenda lines."
3646 (interactive)
3647 (save-excursion
3648 (goto-char (point-max))
3649 (beginning-of-line 1)
3650 (while (not (bobp))
3651 (when (org-get-at-bol 'org-hd-marker)
3652 (org-agenda-entry-text-show-here))
3653 (beginning-of-line 0))))
3655 (defun org-agenda-entry-text-hide ()
3656 "Remove any shown entry context."
3657 (delq nil
3658 (mapcar (lambda (o)
3659 (if (eq (overlay-get o 'org-overlay-type)
3660 'agenda-entry-content)
3661 (progn (delete-overlay o) t)))
3662 (overlays-in (point-min) (point-max)))))
3664 (defun org-agenda-get-day-face (date)
3665 "Return the face DATE should be displayed with."
3666 (or (and (functionp org-agenda-day-face-function)
3667 (funcall org-agenda-day-face-function date))
3668 (cond ((org-agenda-todayp date)
3669 'org-agenda-date-today)
3670 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3671 'org-agenda-date-weekend)
3672 (t 'org-agenda-date))))
3674 ;;; Agenda timeline
3676 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3678 (defun org-timeline (&optional dotodo)
3679 "Show a time-sorted view of the entries in the current org file.
3680 Only entries with a time stamp of today or later will be listed. With
3681 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3682 under the current date.
3683 If the buffer contains an active region, only check the region for
3684 dates."
3685 (interactive "P")
3686 (let* ((dopast t)
3687 (org-agenda-show-log-scoped org-agenda-show-log)
3688 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3689 (current-buffer))))
3690 (date (calendar-current-date))
3691 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3692 (end (if (org-region-active-p) (region-end) (point-max)))
3693 (day-numbers (org-get-all-dates
3694 beg end 'no-ranges
3695 t org-agenda-show-log-scoped ; always include today
3696 org-timeline-show-empty-dates))
3697 (org-deadline-warning-days 0)
3698 (org-agenda-only-exact-dates t)
3699 (today (org-today))
3700 (past t)
3701 args
3702 s e rtn d emptyp)
3703 (setq org-agenda-redo-command
3704 (list 'progn
3705 (list 'org-switch-to-buffer-other-window (current-buffer))
3706 (list 'org-timeline (list 'quote dotodo))))
3707 (if (not dopast)
3708 ;; Remove past dates from the list of dates.
3709 (setq day-numbers (delq nil (mapcar (lambda(x)
3710 (if (>= x today) x nil))
3711 day-numbers))))
3712 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
3713 (org-compile-prefix-format 'timeline)
3714 (org-set-sorting-strategy 'timeline)
3715 (if org-agenda-show-log-scoped (push :closed args))
3716 (push :timestamp args)
3717 (push :deadline args)
3718 (push :scheduled args)
3719 (push :sexp args)
3720 (if dotodo (push :todo args))
3721 (insert "Timeline of file " entry "\n")
3722 (add-text-properties (point-min) (point)
3723 (list 'face 'org-agenda-structure))
3724 (org-agenda-mark-header-line (point-min))
3725 (while (setq d (pop day-numbers))
3726 (if (and (listp d) (eq (car d) :omitted))
3727 (progn
3728 (setq s (point))
3729 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3730 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3731 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3732 (if (and (>= d today)
3733 dopast
3734 past)
3735 (progn
3736 (setq past nil)
3737 (insert (make-string 79 ?-) "\n")))
3738 (setq date (calendar-gregorian-from-absolute d))
3739 (setq s (point))
3740 (setq rtn (and (not emptyp)
3741 (apply 'org-agenda-get-day-entries entry
3742 date args)))
3743 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3744 (progn
3745 (insert
3746 (if (stringp org-agenda-format-date)
3747 (format-time-string org-agenda-format-date
3748 (org-time-from-absolute date))
3749 (funcall org-agenda-format-date date))
3750 "\n")
3751 (put-text-property s (1- (point)) 'face
3752 (org-agenda-get-day-face date))
3753 (put-text-property s (1- (point)) 'org-date-line t)
3754 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3755 (if (equal d today)
3756 (put-text-property s (1- (point)) 'org-today t))
3757 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3758 (put-text-property s (1- (point)) 'day d)))))
3759 (goto-char (point-min))
3760 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3761 (point-min)))
3762 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3763 (org-finalize-agenda)
3764 (setq buffer-read-only t)))
3766 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3767 "Return a list of all relevant day numbers from BEG to END buffer positions.
3768 If NO-RANGES is non-nil, include only the start and end dates of a range,
3769 not every single day in the range. If FORCE-TODAY is non-nil, make
3770 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3771 inactive time stamps (those in square brackets) are included.
3772 When EMPTY is non-nil, also include days without any entries."
3773 (let ((re (concat
3774 (if pre-re pre-re "")
3775 (if inactive org-ts-regexp-both org-ts-regexp)))
3776 dates dates1 date day day1 day2 ts1 ts2 pos)
3777 (if force-today
3778 (setq dates (list (org-today))))
3779 (save-excursion
3780 (goto-char beg)
3781 (while (re-search-forward re end t)
3782 (setq day (time-to-days (org-time-string-to-time
3783 (substring (match-string 1) 0 10)
3784 (current-buffer) (match-beginning 0))))
3785 (or (memq day dates) (push day dates)))
3786 (unless no-ranges
3787 (goto-char beg)
3788 (while (re-search-forward org-tr-regexp end t)
3789 (setq pos (match-beginning 0))
3790 (setq ts1 (substring (match-string 1) 0 10)
3791 ts2 (substring (match-string 2) 0 10)
3792 day1 (time-to-days (org-time-string-to-time
3793 ts1 (current-buffer) pos))
3794 day2 (time-to-days (org-time-string-to-time
3795 ts2 (current-buffer) pos)))
3796 (while (< (setq day1 (1+ day1)) day2)
3797 (or (memq day1 dates) (push day1 dates)))))
3798 (setq dates (sort dates '<))
3799 (when empty
3800 (while (setq day (pop dates))
3801 (setq day2 (car dates))
3802 (push day dates1)
3803 (when (and day2 empty)
3804 (if (or (eq empty t)
3805 (and (numberp empty) (<= (- day2 day) empty)))
3806 (while (< (setq day (1+ day)) day2)
3807 (push (list day) dates1))
3808 (push (cons :omitted (- day2 day)) dates1))))
3809 (setq dates (nreverse dates1)))
3810 dates)))
3812 ;;; Agenda Daily/Weekly
3814 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3815 "Start day for the agenda view.
3816 Custom commands can set this variable in the options section.")
3817 (defvar org-starting-day nil) ; local variable in the agenda buffer
3818 (defvar org-agenda-current-span nil
3819 "The current span used in the agenda view.") ; local variable in the agenda buffer
3820 (defvar org-arg-loc nil) ; local variable
3822 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3823 "List of types searched for when creating the daily/weekly agenda.
3824 This variable is a list of symbols that controls the types of
3825 items that appear in the daily/weekly agenda. Allowed symbols in this
3826 list are are
3828 :timestamp List items containing a date stamp or date range matching
3829 the selected date. This includes sexp entries in
3830 angular brackets.
3832 :sexp List entries resulting from plain diary-like sexps.
3834 :deadline List deadline due on that date. When the date is today,
3835 also list any deadlines past due, or due within
3836 `org-deadline-warning-days'. `:deadline' must appear before
3837 `:scheduled' if the setting of
3838 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3839 any effect.
3841 :scheduled List all items which are scheduled for the given date.
3842 The diary for *today* also contains items which were
3843 scheduled earlier and are not yet marked DONE.
3845 By default, all four types are turned on.
3847 Never set this variable globally using `setq', because then it
3848 will apply to all future agenda commands. Instead, bind it with
3849 `let' to scope it dynamically into the agenda-constructing
3850 command. A good way to set it is through options in
3851 `org-agenda-custom-commands'. For a more flexible (though
3852 somewhat less efficient) way of determining what is included in
3853 the daily/weekly agenda, see `org-agenda-skip-function'.")
3855 ;;;###autoload
3856 (defun org-agenda-list (&optional arg start-day span)
3857 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3858 The view will be for the current day or week, but from the overview buffer
3859 you will be able to go to other days/weeks.
3861 With a numeric prefix argument in an interactive call, the agenda will
3862 span ARG days. Lisp programs should instead specify SPAN to change
3863 the number of days. SPAN defaults to `org-agenda-span'.
3865 START-DAY defaults to TODAY, or to the most recent match for the weekday
3866 given in `org-agenda-start-on-weekday'."
3867 (interactive "P")
3868 (if (and (integerp arg) (> arg 0))
3869 (setq span arg arg nil))
3870 (catch 'exit
3871 (if org-agenda-sticky
3872 (setq org-agenda-buffer-name
3873 (cond ((and keys (stringp match))
3874 (format "*Org Agenda(%s:%s)*" keys match))
3875 (keys
3876 (format "*Org Agenda(%s)*" keys))
3877 (t (format "*Org Agenda(a)*")))))
3878 (org-agenda-prepare "Day/Week")
3879 (setq start-day (or start-day org-agenda-start-day))
3880 (if org-agenda-overriding-arguments
3881 (setq arg (car org-agenda-overriding-arguments)
3882 start-day (nth 1 org-agenda-overriding-arguments)
3883 span (nth 2 org-agenda-overriding-arguments)))
3884 (if (stringp start-day)
3885 ;; Convert to an absolute day number
3886 (setq start-day (time-to-days (org-read-date nil t start-day))))
3887 (setq org-agenda-last-arguments (list arg start-day span))
3888 (org-compile-prefix-format 'agenda)
3889 (org-set-sorting-strategy 'agenda)
3890 (let* ((span (org-agenda-ndays-to-span
3891 (or span org-agenda-ndays org-agenda-span)))
3892 (today (org-today))
3893 (sd (or start-day today))
3894 (ndays (org-agenda-span-to-ndays span sd))
3895 (org-agenda-start-on-weekday
3896 (if (eq ndays 7)
3897 org-agenda-start-on-weekday))
3898 (thefiles (org-agenda-files nil 'ifmode))
3899 (files thefiles)
3900 (start (if (or (null org-agenda-start-on-weekday)
3901 (< ndays 7))
3903 (let* ((nt (calendar-day-of-week
3904 (calendar-gregorian-from-absolute sd)))
3905 (n1 org-agenda-start-on-weekday)
3906 (d (- nt n1)))
3907 (- sd (+ (if (< d 0) 7 0) d)))))
3908 (day-numbers (list start))
3909 (day-cnt 0)
3910 (inhibit-redisplay (not debug-on-error))
3911 (org-agenda-show-log-scoped org-agenda-show-log)
3912 s e rtn rtnall file date d start-pos end-pos todayp
3913 clocktable-start clocktable-end filter)
3914 (setq org-agenda-redo-command
3915 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3916 (dotimes (n (1- ndays))
3917 (push (1+ (car day-numbers)) day-numbers))
3918 (setq day-numbers (nreverse day-numbers))
3919 (setq clocktable-start (car day-numbers)
3920 clocktable-end (1+ (or (org-last day-numbers) 0)))
3921 (org-set-local 'org-starting-day (car day-numbers))
3922 (org-set-local 'org-arg-loc arg)
3923 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3924 (unless org-agenda-compact-blocks
3925 (let* ((d1 (car day-numbers))
3926 (d2 (org-last day-numbers))
3927 (w1 (org-days-to-iso-week d1))
3928 (w2 (org-days-to-iso-week d2)))
3929 (setq s (point))
3930 (if org-agenda-overriding-header
3931 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3932 nil 'face 'org-agenda-structure) "\n")
3933 (insert (org-agenda-span-name span)
3934 "-agenda"
3935 (if (< (- d2 d1) 350)
3936 (if (= w1 w2)
3937 (format " (W%02d)" w1)
3938 (format " (W%02d-W%02d)" w1 w2))
3940 ":\n")))
3941 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3942 'org-date-line t))
3943 (org-agenda-mark-header-line s))
3944 (while (setq d (pop day-numbers))
3945 (setq date (calendar-gregorian-from-absolute d)
3946 s (point))
3947 (if (or (setq todayp (= d today))
3948 (and (not start-pos) (= d sd)))
3949 (setq start-pos (point))
3950 (if (and start-pos (not end-pos))
3951 (setq end-pos (point))))
3952 (setq files thefiles
3953 rtnall nil)
3954 (while (setq file (pop files))
3955 (catch 'nextfile
3956 (org-check-agenda-file file)
3957 (let ((org-agenda-entry-types org-agenda-entry-types))
3958 (unless org-agenda-include-deadlines
3959 (setq org-agenda-entry-types
3960 (delq :deadline org-agenda-entry-types)))
3961 (cond
3962 ((memq org-agenda-show-log-scoped '(only clockcheck))
3963 (setq rtn (org-agenda-get-day-entries
3964 file date :closed)))
3965 (org-agenda-show-log-scoped
3966 (setq rtn (apply 'org-agenda-get-day-entries
3967 file date
3968 (append '(:closed) org-agenda-entry-types))))
3970 (setq rtn (apply 'org-agenda-get-day-entries
3971 file date
3972 org-agenda-entry-types)))))
3973 (setq rtnall (append rtnall rtn)))) ;; all entries
3974 (if org-agenda-include-diary
3975 (let ((org-agenda-search-headline-for-time t))
3976 (require 'diary-lib)
3977 (setq rtn (org-get-entries-from-diary date))
3978 (setq rtnall (append rtnall rtn))))
3979 (if (or rtnall org-agenda-show-all-dates)
3980 (progn
3981 (setq day-cnt (1+ day-cnt))
3982 (insert
3983 (if (stringp org-agenda-format-date)
3984 (format-time-string org-agenda-format-date
3985 (org-time-from-absolute date))
3986 (funcall org-agenda-format-date date))
3987 "\n")
3988 (put-text-property s (1- (point)) 'face
3989 (org-agenda-get-day-face date))
3990 (put-text-property s (1- (point)) 'org-date-line t)
3991 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3992 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3993 (when todayp
3994 (put-text-property s (1- (point)) 'org-today t))
3995 (setq rtnall
3996 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
3997 (if rtnall (insert ;; all entries
3998 (org-finalize-agenda-entries rtnall)
3999 "\n"))
4000 (put-text-property s (1- (point)) 'day d)
4001 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4002 (when (and org-agenda-clockreport-mode clocktable-start)
4003 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4004 ;; the above line is to ensure the restricted range!
4005 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4006 tbl)
4007 (setq p (org-plist-delete p :block))
4008 (setq p (plist-put p :tstart clocktable-start))
4009 (setq p (plist-put p :tend clocktable-end))
4010 (setq p (plist-put p :scope 'agenda))
4011 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4012 (setq filter (or org-agenda-tag-filter-while-redo
4013 (get 'org-agenda-tag-filter :preset-filter))))
4014 (setq p (plist-put p :tags (mapconcat (lambda (x)
4015 (if (string-match "[<>=]" x)
4018 filter ""))))
4019 (setq tbl (apply 'org-get-clocktable p))
4020 (insert tbl)))
4021 (goto-char (point-min))
4022 (or org-agenda-multi (org-fit-agenda-window))
4023 (unless (and (pos-visible-in-window-p (point-min))
4024 (pos-visible-in-window-p (point-max)))
4025 (goto-char (1- (point-max)))
4026 (recenter -1)
4027 (if (not (pos-visible-in-window-p (or start-pos 1)))
4028 (progn
4029 (goto-char (or start-pos 1))
4030 (recenter 1))))
4031 (goto-char (or start-pos 1))
4032 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
4033 (if (eq org-agenda-show-log-scoped 'clockcheck)
4034 (org-agenda-show-clocking-issues))
4035 (org-finalize-agenda)
4036 (setq buffer-read-only t)
4037 (message ""))))
4039 (defun org-agenda-ndays-to-span (n)
4040 "Return a span symbol for a span of N days, or N if none matches."
4041 (cond ((symbolp n) n)
4042 ((= n 1) 'day)
4043 ((= n 7) 'week)
4044 (t n)))
4046 (defun org-agenda-span-to-ndays (span start-day)
4047 "Return ndays from SPAN starting at START-DAY."
4048 (cond ((numberp span) span)
4049 ((eq span 'day) 1)
4050 ((eq span 'week) 7)
4051 ((eq span 'month)
4052 (let ((date (calendar-gregorian-from-absolute start-day)))
4053 (calendar-last-day-of-month (car date) (caddr date))))
4054 ((eq span 'year)
4055 (let ((date (calendar-gregorian-from-absolute start-day)))
4056 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4058 (defun org-agenda-span-name (span)
4059 "Return a SPAN name."
4060 (if (null span)
4062 (if (symbolp span)
4063 (capitalize (symbol-name span))
4064 (format "%d days" span))))
4066 ;;; Agenda word search
4068 (defvar org-agenda-search-history nil)
4069 (defvar org-todo-only nil)
4071 (defvar org-search-syntax-table nil
4072 "Special syntax table for org-mode search.
4073 In this table, we have single quotes not as word constituents, to
4074 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4076 (defvar org-mode-syntax-table) ; From org.el
4077 (defun org-search-syntax-table ()
4078 (unless org-search-syntax-table
4079 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4080 (modify-syntax-entry ?' "." org-search-syntax-table)
4081 (modify-syntax-entry ?` "." org-search-syntax-table))
4082 org-search-syntax-table)
4084 (defvar org-agenda-last-search-view-search-was-boolean nil)
4086 ;;;###autoload
4087 (defun org-search-view (&optional todo-only string edit-at)
4088 "Show all entries that contain a phrase or words or regular expressions.
4090 With optional prefix argument TODO-ONLY, only consider entries that are
4091 TODO entries. The argument STRING can be used to pass a default search
4092 string into this function. If EDIT-AT is non-nil, it means that the
4093 user should get a chance to edit this string, with cursor at position
4094 EDIT-AT.
4096 The search string can be viewed either as a phrase that should be found as
4097 is, or it can be broken into a number of snippets, each of which must match
4098 in a Boolean way to select an entry. The default depends on the variable
4099 `org-agenda-search-view-always-boolean'.
4100 Even if this is turned off (the default) you can always switch to
4101 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4103 The default is a direct search of the whole phrase, where each space in
4104 the search string can expand to an arbitrary amount of whitespace,
4105 including newlines.
4107 If using a Boolean search, the search string is split on whitespace and
4108 each snippet is searched separately, with logical AND to select an entry.
4109 Words prefixed with a minus must *not* occur in the entry. Words without
4110 a prefix or prefixed with a plus must occur in the entry. Matching is
4111 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4112 match whole words, not parts of a word) if
4113 `org-agenda-search-view-force-full-words' is set (default is nil).
4115 Boolean search snippets enclosed by curly braces are interpreted as
4116 regular expressions that must or (when preceded with \"-\") must not
4117 match in the entry. Snippets enclosed into double quotes will be taken
4118 as a whole, to include whitespace.
4120 - If the search string starts with an asterisk, search only in headlines.
4121 - If (possibly after the leading star) the search string starts with an
4122 exclamation mark, this also means to look at TODO entries only, an effect
4123 that can also be achieved with a prefix argument.
4124 - If (possibly after star and exclamation mark) the search string starts
4125 with a colon, this will mean that the (non-regexp) snippets of the
4126 Boolean search must match as full words.
4128 This command searches the agenda files, and in addition the files listed
4129 in `org-agenda-text-search-extra-files'."
4130 (interactive "P")
4131 (org-agenda-prepare "SEARCH")
4132 (org-compile-prefix-format 'search)
4133 (org-set-sorting-strategy 'search)
4134 (let* ((props (list 'face nil
4135 'done-face 'org-agenda-done
4136 'org-not-done-regexp org-not-done-regexp
4137 'org-todo-regexp org-todo-regexp
4138 'org-complex-heading-regexp org-complex-heading-regexp
4139 'mouse-face 'highlight
4140 'help-echo (format "mouse-2 or RET jump to location")))
4141 (full-words org-agenda-search-view-force-full-words)
4142 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4143 regexp rtn rtnall files file pos
4144 marker category category-pos tags c neg re boolean
4145 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4146 (unless (and (not edit-at)
4147 (stringp string)
4148 (string-match "\\S-" string))
4149 (setq string (read-string
4150 (if org-agenda-search-view-always-boolean
4151 "[+-]Word/{Regexp} ...: "
4152 "Phrase, or [+-]Word/{Regexp} ...: ")
4153 (cond
4154 ((integerp edit-at) (cons string edit-at))
4155 (edit-at string))
4156 'org-agenda-search-history)))
4157 (org-set-local 'org-todo-only todo-only)
4158 (setq org-agenda-redo-command
4159 (list 'org-search-view (if todo-only t nil) string
4160 '(if current-prefix-arg 1 nil)))
4161 (setq org-agenda-query-string string)
4163 (if (equal (string-to-char string) ?*)
4164 (setq hdl-only t
4165 words (substring string 1))
4166 (setq words string))
4167 (when (equal (string-to-char words) ?!)
4168 (setq todo-only t
4169 words (substring words 1)))
4170 (when (equal (string-to-char words) ?:)
4171 (setq full-words t
4172 words (substring words 1)))
4173 (if (or org-agenda-search-view-always-boolean
4174 (member (string-to-char words) '(?- ?+ ?\{)))
4175 (setq boolean t))
4176 (setq words (org-split-string words))
4177 (let (www w)
4178 (while (setq w (pop words))
4179 (while (and (string-match "\\\\\\'" w) words)
4180 (setq w (concat (substring w 0 -1) " " (pop words))))
4181 (push w www))
4182 (setq words (nreverse www) www nil)
4183 (while (setq w (pop words))
4184 (when (and (string-match "\\`[-+]?{" w)
4185 (not (string-match "}\\'" w)))
4186 (while (and words (not (string-match "}\\'" (car words))))
4187 (setq w (concat w " " (pop words))))
4188 (setq w (concat w " " (pop words))))
4189 (push w www))
4190 (setq words (nreverse www)))
4191 (setq org-agenda-last-search-view-search-was-boolean boolean)
4192 (when boolean
4193 (let (wds w)
4194 (while (setq w (pop words))
4195 (if (or (equal (substring w 0 1) "\"")
4196 (and (> (length w) 1)
4197 (member (substring w 0 1) '("+" "-"))
4198 (equal (substring w 1 2) "\"")))
4199 (while (and words (not (equal (substring w -1) "\"")))
4200 (setq w (concat w " " (pop words)))))
4201 (and (string-match "\\`\\([-+]?\\)\"" w)
4202 (setq w (replace-match "\\1" nil nil w)))
4203 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4204 (push w wds))
4205 (setq words (nreverse wds))))
4206 (if boolean
4207 (mapc (lambda (w)
4208 (setq c (string-to-char w))
4209 (if (equal c ?-)
4210 (setq neg t w (substring w 1))
4211 (if (equal c ?+)
4212 (setq neg nil w (substring w 1))
4213 (setq neg nil)))
4214 (if (string-match "\\`{.*}\\'" w)
4215 (setq re (substring w 1 -1))
4216 (if full-words
4217 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4218 (setq re (regexp-quote (downcase w)))))
4219 (if neg (push re regexps-) (push re regexps+)))
4220 words)
4221 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4222 regexps+))
4223 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4224 (if (not regexps+)
4225 (setq regexp org-outline-regexp-bol)
4226 (setq regexp (pop regexps+))
4227 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4228 regexp))))
4229 (setq files (org-agenda-files nil 'ifmode))
4230 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4231 (pop org-agenda-text-search-extra-files)
4232 (setq files (org-add-archive-files files)))
4233 (setq files (append files org-agenda-text-search-extra-files)
4234 rtnall nil)
4235 (while (setq file (pop files))
4236 (setq ee nil)
4237 (catch 'nextfile
4238 (org-check-agenda-file file)
4239 (setq buffer (if (file-exists-p file)
4240 (org-get-agenda-file-buffer file)
4241 (error "No such file %s" file)))
4242 (if (not buffer)
4243 ;; If file does not exist, make sure an error message is sent
4244 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4245 file))))
4246 (with-current-buffer buffer
4247 (with-syntax-table (org-search-syntax-table)
4248 (unless (derived-mode-p 'org-mode)
4249 (error "Agenda file %s is not in `org-mode'" file))
4250 (let ((case-fold-search t))
4251 (save-excursion
4252 (save-restriction
4253 (if org-agenda-restrict
4254 (narrow-to-region org-agenda-restrict-begin
4255 org-agenda-restrict-end)
4256 (widen))
4257 (goto-char (point-min))
4258 (unless (or (org-at-heading-p)
4259 (outline-next-heading))
4260 (throw 'nextfile t))
4261 (goto-char (max (point-min) (1- (point))))
4262 (while (re-search-forward regexp nil t)
4263 (org-back-to-heading t)
4264 (skip-chars-forward "* ")
4265 (setq beg (point-at-bol)
4266 beg1 (point)
4267 end (progn (outline-next-heading) (point)))
4268 (catch :skip
4269 (goto-char beg)
4270 (org-agenda-skip)
4271 (setq str (buffer-substring-no-properties
4272 (point-at-bol)
4273 (if hdl-only (point-at-eol) end)))
4274 (mapc (lambda (wr) (when (string-match wr str)
4275 (goto-char (1- end))
4276 (throw :skip t)))
4277 regexps-)
4278 (mapc (lambda (wr) (unless (string-match wr str)
4279 (goto-char (1- end))
4280 (throw :skip t)))
4281 (if todo-only
4282 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4283 regexps+)
4284 regexps+))
4285 (goto-char beg)
4286 (setq marker (org-agenda-new-marker (point))
4287 category (org-get-category)
4288 category-pos (get-text-property (point) 'org-category-position)
4289 tags (org-get-tags-at (point))
4290 txt (org-agenda-format-item
4292 (buffer-substring-no-properties
4293 beg1 (point-at-eol))
4294 category tags))
4295 (org-add-props txt props
4296 'org-marker marker 'org-hd-marker marker
4297 'org-todo-regexp org-todo-regexp
4298 'org-complex-heading-regexp org-complex-heading-regexp
4299 'priority 1000 'org-category category
4300 'org-category-position category-pos
4301 'type "search")
4302 (push txt ee)
4303 (goto-char (1- end))))))))))
4304 (setq rtn (nreverse ee))
4305 (setq rtnall (append rtnall rtn)))
4306 (if org-agenda-overriding-header
4307 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4308 nil 'face 'org-agenda-structure) "\n")
4309 (insert "Search words: ")
4310 (add-text-properties (point-min) (1- (point))
4311 (list 'face 'org-agenda-structure))
4312 (setq pos (point))
4313 (insert string "\n")
4314 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4315 (setq pos (point))
4316 (unless org-agenda-multi
4317 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4318 (add-text-properties pos (1- (point))
4319 (list 'face 'org-agenda-structure))))
4320 (org-agenda-mark-header-line (point-min))
4321 (when rtnall
4322 (insert (org-finalize-agenda-entries rtnall) "\n"))
4323 (goto-char (point-min))
4324 (or org-agenda-multi (org-fit-agenda-window))
4325 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
4326 (org-finalize-agenda)
4327 (setq buffer-read-only t)))
4329 ;;; Agenda TODO list
4331 (defvar org-select-this-todo-keyword nil)
4332 (defvar org-last-arg nil)
4334 ;;;###autoload
4335 (defun org-todo-list (&optional arg)
4336 "Show all (not done) TODO entries from all agenda file in a single list.
4337 The prefix arg can be used to select a specific TODO keyword and limit
4338 the list to these. When using \\[universal-argument], you will be prompted
4339 for a keyword. A numeric prefix directly selects the Nth keyword in
4340 `org-todo-keywords-1'."
4341 (interactive "P")
4342 (org-agenda-prepare "TODO")
4343 (org-compile-prefix-format 'todo)
4344 (org-set-sorting-strategy 'todo)
4345 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4346 (let* ((today (org-today))
4347 (date (calendar-gregorian-from-absolute today))
4348 (kwds org-todo-keywords-for-agenda)
4349 (completion-ignore-case t)
4350 (org-select-this-todo-keyword
4351 (if (stringp arg) arg
4352 (and arg (integerp arg) (> arg 0)
4353 (nth (1- arg) kwds))))
4354 rtn rtnall files file pos)
4355 (when (equal arg '(4))
4356 (setq org-select-this-todo-keyword
4357 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4358 (mapcar 'list kwds) nil nil)))
4359 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4360 (org-set-local 'org-last-arg arg)
4361 (setq org-agenda-redo-command
4362 '(org-todo-list (or current-prefix-arg org-last-arg)))
4363 (setq files (org-agenda-files nil 'ifmode)
4364 rtnall nil)
4365 (while (setq file (pop files))
4366 (catch 'nextfile
4367 (org-check-agenda-file file)
4368 (setq rtn (org-agenda-get-day-entries file date :todo))
4369 (setq rtnall (append rtnall rtn))))
4370 (if org-agenda-overriding-header
4371 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4372 nil 'face 'org-agenda-structure) "\n")
4373 (insert "Global list of TODO items of type: ")
4374 (add-text-properties (point-min) (1- (point))
4375 (list 'face 'org-agenda-structure
4376 'short-heading
4377 (concat "ToDo: "
4378 (or org-select-this-todo-keyword "ALL"))))
4379 (org-agenda-mark-header-line (point-min))
4380 (setq pos (point))
4381 (insert (or org-select-this-todo-keyword "ALL") "\n")
4382 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4383 (setq pos (point))
4384 (unless org-agenda-multi
4385 (insert "Available with `N r': (0)ALL")
4386 (let ((n 0) s)
4387 (mapc (lambda (x)
4388 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4389 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4390 (insert "\n "))
4391 (insert " " s))
4392 kwds))
4393 (insert "\n"))
4394 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4395 (org-agenda-mark-header-line (point-min))
4396 (when rtnall
4397 (insert (org-finalize-agenda-entries rtnall) "\n"))
4398 (goto-char (point-min))
4399 (or org-agenda-multi (org-fit-agenda-window))
4400 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4401 (org-finalize-agenda)
4402 (setq buffer-read-only t)))
4404 ;;; Agenda tags match
4406 ;;;###autoload
4407 (defun org-tags-view (&optional todo-only match)
4408 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4409 The prefix arg TODO-ONLY limits the search to TODO entries."
4410 (interactive "P")
4411 (let* ((org-tags-match-list-sublevels
4412 org-tags-match-list-sublevels)
4413 (completion-ignore-case t)
4414 rtn rtnall files file pos matcher
4415 buffer)
4416 (when (and (stringp match) (not (string-match "\\S-" match)))
4417 (setq match nil))
4418 (setq matcher (org-make-tags-matcher match)
4419 match (car matcher) matcher (cdr matcher))
4420 (org-agenda-prepare (concat "TAGS " match))
4421 (org-compile-prefix-format 'tags)
4422 (org-set-sorting-strategy 'tags)
4423 (setq org-agenda-query-string match)
4424 (setq org-agenda-redo-command
4425 (list 'org-tags-view (list 'quote todo-only)
4426 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4427 (setq files (org-agenda-files nil 'ifmode)
4428 rtnall nil)
4429 (while (setq file (pop files))
4430 (catch 'nextfile
4431 (org-check-agenda-file file)
4432 (setq buffer (if (file-exists-p file)
4433 (org-get-agenda-file-buffer file)
4434 (error "No such file %s" file)))
4435 (if (not buffer)
4436 ;; If file does not exist, error message to agenda
4437 (setq rtn (list
4438 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4439 rtnall (append rtnall rtn))
4440 (with-current-buffer buffer
4441 (unless (derived-mode-p 'org-mode)
4442 (error "Agenda file %s is not in `org-mode'" file))
4443 (save-excursion
4444 (save-restriction
4445 (if org-agenda-restrict
4446 (narrow-to-region org-agenda-restrict-begin
4447 org-agenda-restrict-end)
4448 (widen))
4449 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4450 (setq rtnall (append rtnall rtn))))))))
4451 (if org-agenda-overriding-header
4452 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4453 nil 'face 'org-agenda-structure) "\n")
4454 (insert "Headlines with TAGS match: ")
4455 (add-text-properties (point-min) (1- (point))
4456 (list 'face 'org-agenda-structure
4457 'short-heading
4458 (concat "Match: " match)))
4459 (setq pos (point))
4460 (insert match "\n")
4461 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4462 (setq pos (point))
4463 (unless org-agenda-multi
4464 (insert "Press `C-u r' to search again with new search string\n"))
4465 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4466 (org-agenda-mark-header-line (point-min))
4467 (when rtnall
4468 (insert (org-finalize-agenda-entries rtnall) "\n"))
4469 (goto-char (point-min))
4470 (or org-agenda-multi (org-fit-agenda-window))
4471 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4472 (org-finalize-agenda)
4473 (setq buffer-read-only t)))
4475 ;;; Agenda Finding stuck projects
4477 (defvar org-agenda-skip-regexp nil
4478 "Regular expression used in skipping subtrees for the agenda.
4479 This is basically a temporary global variable that can be set and then
4480 used by user-defined selections using `org-agenda-skip-function'.")
4482 (defvar org-agenda-overriding-header nil
4483 "When set during agenda, todo and tags searches it replaces the header.
4484 This variable should not be set directly, but custom commands can bind it
4485 in the options section.")
4487 (defun org-agenda-skip-entry-when-regexp-matches ()
4488 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4489 If yes, it returns the end position of this entry, causing agenda commands
4490 to skip the entry but continuing the search in the subtree. This is a
4491 function that can be put into `org-agenda-skip-function' for the duration
4492 of a command."
4493 (let ((end (save-excursion (org-end-of-subtree t)))
4494 skip)
4495 (save-excursion
4496 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4497 (and skip end)))
4499 (defun org-agenda-skip-subtree-when-regexp-matches ()
4500 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4501 If yes, it returns the end position of this tree, causing agenda commands
4502 to skip this subtree. This is a function that can be put into
4503 `org-agenda-skip-function' for the duration of a command."
4504 (let ((end (save-excursion (org-end-of-subtree t)))
4505 skip)
4506 (save-excursion
4507 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4508 (and skip end)))
4510 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4511 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4512 If yes, it returns the end position of the current entry (NOT the tree),
4513 causing agenda commands to skip the entry but continuing the search in
4514 the subtree. This is a function that can be put into
4515 `org-agenda-skip-function' for the duration of a command. An important
4516 use of this function is for the stuck project list."
4517 (let ((end (save-excursion (org-end-of-subtree t)))
4518 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4519 skip)
4520 (save-excursion
4521 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4522 (and skip entry-end)))
4524 (defun org-agenda-skip-entry-if (&rest conditions)
4525 "Skip entry if any of CONDITIONS is true.
4526 See `org-agenda-skip-if' for details."
4527 (org-agenda-skip-if nil conditions))
4529 (defun org-agenda-skip-subtree-if (&rest conditions)
4530 "Skip entry if any of CONDITIONS is true.
4531 See `org-agenda-skip-if' for details."
4532 (org-agenda-skip-if t conditions))
4534 (defun org-agenda-skip-if (subtree conditions)
4535 "Checks current entity for CONDITIONS.
4536 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4537 the entry (i.e. the text before the next heading) is checked.
4539 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4540 from different tests. Valid conditions are:
4542 scheduled Check if there is a scheduled cookie
4543 notscheduled Check if there is no scheduled cookie
4544 deadline Check if there is a deadline
4545 notdeadline Check if there is no deadline
4546 timestamp Check if there is a timestamp (also deadline or scheduled)
4547 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4548 regexp Check if regexp matches
4549 notregexp Check if regexp does not match.
4550 todo Check if TODO keyword matches
4551 nottodo Check if TODO keyword does not match
4553 The regexp is taken from the conditions list, it must come right after
4554 the `regexp' or `notregexp' element.
4556 `todo' and `nottodo' accept as an argument a list of todo
4557 keywords, which may include \"*\" to match any todo keyword.
4559 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4561 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4563 Instead of a list, a keyword class may be given. For example:
4565 (org-agenda-skip-entry-if 'nottodo 'done)
4567 would skip entries that haven't been marked with any of \"DONE\"
4568 keywords. Possible classes are: `todo', `done', `any'.
4570 If any of these conditions is met, this function returns the end point of
4571 the entity, causing the search to continue from there. This is a function
4572 that can be put into `org-agenda-skip-function' for the duration of a command."
4573 (let (beg end m)
4574 (org-back-to-heading t)
4575 (setq beg (point)
4576 end (if subtree
4577 (progn (org-end-of-subtree t) (point))
4578 (progn (outline-next-heading) (1- (point)))))
4579 (goto-char beg)
4580 (and
4582 (and (memq 'scheduled conditions)
4583 (re-search-forward org-scheduled-time-regexp end t))
4584 (and (memq 'notscheduled conditions)
4585 (not (re-search-forward org-scheduled-time-regexp end t)))
4586 (and (memq 'deadline conditions)
4587 (re-search-forward org-deadline-time-regexp end t))
4588 (and (memq 'notdeadline conditions)
4589 (not (re-search-forward org-deadline-time-regexp end t)))
4590 (and (memq 'timestamp conditions)
4591 (re-search-forward org-ts-regexp end t))
4592 (and (memq 'nottimestamp conditions)
4593 (not (re-search-forward org-ts-regexp end t)))
4594 (and (setq m (memq 'regexp conditions))
4595 (stringp (nth 1 m))
4596 (re-search-forward (nth 1 m) end t))
4597 (and (setq m (memq 'notregexp conditions))
4598 (stringp (nth 1 m))
4599 (not (re-search-forward (nth 1 m) end t)))
4600 (and (or
4601 (setq m (memq 'nottodo conditions))
4602 (setq m (memq 'todo-unblocked conditions))
4603 (setq m (memq 'nottodo-unblocked conditions))
4604 (setq m (memq 'todo conditions)))
4605 (org-agenda-skip-if-todo m end)))
4606 end)))
4608 (defun org-agenda-skip-if-todo (args end)
4609 "Helper function for `org-agenda-skip-if', do not use it directly.
4610 ARGS is a list with first element either `todo', `nottodo',
4611 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4612 a list of TODO keywords, or a state symbol `todo' or `done' or
4613 `any'."
4614 (let ((kw (car args))
4615 (arg (cadr args))
4616 todo-wds todo-re)
4617 (setq todo-wds
4618 (org-uniquify
4619 (cond
4620 ((listp arg) ;; list of keywords
4621 (if (member "*" arg)
4622 (mapcar 'substring-no-properties org-todo-keywords-1)
4623 arg))
4624 ((symbolp arg) ;; keyword class name
4625 (cond
4626 ((eq arg 'todo)
4627 (org-delete-all org-done-keywords
4628 (mapcar 'substring-no-properties
4629 org-todo-keywords-1)))
4630 ((eq arg 'done) org-done-keywords)
4631 ((eq arg 'any)
4632 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4633 (setq todo-re
4634 (concat "^\\*+[ \t]+\\<\\("
4635 (mapconcat 'identity todo-wds "\\|")
4636 "\\)\\>"))
4637 (cond
4638 ((eq kw 'todo) (re-search-forward todo-re end t))
4639 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4640 ((eq kw 'todo-unblocked)
4641 (catch 'unblocked
4642 (while (re-search-forward todo-re end t)
4643 (or (org-entry-blocked-p) (throw 'unblocked t)))
4644 nil))
4645 ((eq kw 'nottodo-unblocked)
4646 (catch 'unblocked
4647 (while (re-search-forward todo-re end t)
4648 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4652 ;;;###autoload
4653 (defun org-agenda-list-stuck-projects (&rest ignore)
4654 "Create agenda view for projects that are stuck.
4655 Stuck projects are project that have no next actions. For the definitions
4656 of what a project is and how to check if it stuck, customize the variable
4657 `org-stuck-projects'."
4658 (interactive)
4659 (let* ((org-agenda-skip-function
4660 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4661 ;; We could have used org-agenda-skip-if here.
4662 (org-agenda-overriding-header
4663 (or org-agenda-overriding-header "List of stuck projects: "))
4664 (matcher (nth 0 org-stuck-projects))
4665 (todo (nth 1 org-stuck-projects))
4666 (todo-wds (if (member "*" todo)
4667 (progn
4668 (org-agenda-prepare-buffers (org-agenda-files
4669 nil 'ifmode))
4670 (org-delete-all
4671 org-done-keywords-for-agenda
4672 (copy-sequence org-todo-keywords-for-agenda)))
4673 todo))
4674 (todo-re (concat "^\\*+[ \t]+\\("
4675 (mapconcat 'identity todo-wds "\\|")
4676 "\\)\\>"))
4677 (tags (nth 2 org-stuck-projects))
4678 (tags-re (if (member "*" tags)
4679 (concat org-outline-regexp-bol
4680 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4681 (if tags
4682 (concat org-outline-regexp-bol
4683 ".*:\\("
4684 (mapconcat 'identity tags "\\|")
4685 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4686 (gen-re (nth 3 org-stuck-projects))
4687 (re-list
4688 (delq nil
4689 (list
4690 (if todo todo-re)
4691 (if tags tags-re)
4692 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4693 gen-re)))))
4694 (setq org-agenda-skip-regexp
4695 (if re-list
4696 (mapconcat 'identity re-list "\\|")
4697 (error "No information how to identify unstuck projects")))
4698 (org-tags-view nil matcher)
4699 (with-current-buffer org-agenda-buffer-name
4700 (setq org-agenda-redo-command
4701 '(org-agenda-list-stuck-projects
4702 (or current-prefix-arg org-last-arg))))))
4704 ;;; Diary integration
4706 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4707 (defvar diary-list-entries-hook)
4708 (defvar diary-time-regexp)
4709 (defun org-get-entries-from-diary (date)
4710 "Get the (Emacs Calendar) diary entries for DATE."
4711 (require 'diary-lib)
4712 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4713 (diary-display-hook '(fancy-diary-display))
4714 (diary-display-function 'fancy-diary-display)
4715 (pop-up-frames nil)
4716 (diary-list-entries-hook
4717 (cons 'org-diary-default-entry diary-list-entries-hook))
4718 (diary-file-name-prefix-function nil) ; turn this feature off
4719 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4720 entries
4721 (org-disable-agenda-to-diary t))
4722 (save-excursion
4723 (save-window-excursion
4724 (funcall (if (fboundp 'diary-list-entries)
4725 'diary-list-entries 'list-diary-entries)
4726 date 1)))
4727 (if (not (get-buffer diary-fancy-buffer))
4728 (setq entries nil)
4729 (with-current-buffer diary-fancy-buffer
4730 (setq buffer-read-only nil)
4731 (if (zerop (buffer-size))
4732 ;; No entries
4733 (setq entries nil)
4734 ;; Omit the date and other unnecessary stuff
4735 (org-agenda-cleanup-fancy-diary)
4736 ;; Add prefix to each line and extend the text properties
4737 (if (zerop (buffer-size))
4738 (setq entries nil)
4739 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4740 (setq entries
4741 (with-temp-buffer
4742 (insert entries) (goto-char (point-min))
4743 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4744 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4745 (replace-match (concat "; " (match-string 1)))))
4746 (buffer-string)))))
4747 (set-buffer-modified-p nil)
4748 (kill-buffer diary-fancy-buffer)))
4749 (when entries
4750 (setq entries (org-split-string entries "\n"))
4751 (setq entries
4752 (mapcar
4753 (lambda (x)
4754 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4755 ;; Extend the text properties to the beginning of the line
4756 (org-add-props x (text-properties-at (1- (length x)) x)
4757 'type "diary" 'date date 'face 'org-agenda-diary))
4758 entries)))))
4760 (defvar org-agenda-cleanup-fancy-diary-hook nil
4761 "Hook run when the fancy diary buffer is cleaned up.")
4763 (defun org-agenda-cleanup-fancy-diary ()
4764 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4765 This gets rid of the date, the underline under the date, and
4766 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4767 date. It also removes lines that contain only whitespace."
4768 (goto-char (point-min))
4769 (if (looking-at ".*?:[ \t]*")
4770 (progn
4771 (replace-match "")
4772 (re-search-forward "\n=+$" nil t)
4773 (replace-match "")
4774 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4775 (re-search-forward "\n=+$" nil t)
4776 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4777 (goto-char (point-min))
4778 (while (re-search-forward "^ +\n" nil t)
4779 (replace-match ""))
4780 (goto-char (point-min))
4781 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4782 (replace-match ""))
4783 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4785 ;; Make sure entries from the diary have the right text properties.
4786 (eval-after-load "diary-lib"
4787 '(if (boundp 'diary-modify-entry-list-string-function)
4788 ;; We can rely on the hook, nothing to do
4790 ;; Hook not available, must use advice to make this work
4791 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4792 "Make the position visible."
4793 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4794 (stringp string)
4795 buffer-file-name)
4796 (setq string (org-modify-diary-entry-string string))))))
4798 (defun org-modify-diary-entry-string (string)
4799 "Add text properties to string, allowing org-mode to act on it."
4800 (org-add-props string nil
4801 'mouse-face 'highlight
4802 'help-echo (if buffer-file-name
4803 (format "mouse-2 or RET jump to diary file %s"
4804 (abbreviate-file-name buffer-file-name))
4806 'org-agenda-diary-link t
4807 'org-marker (org-agenda-new-marker (point-at-bol))))
4809 (defun org-diary-default-entry ()
4810 "Add a dummy entry to the diary.
4811 Needed to avoid empty dates which mess up holiday display."
4812 ;; Catch the error if dealing with the new add-to-diary-alist
4813 (when org-disable-agenda-to-diary
4814 (condition-case nil
4815 (org-add-to-diary-list original-date "Org-mode dummy" "")
4816 (error
4817 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4819 (defun org-add-to-diary-list (&rest args)
4820 (if (fboundp 'diary-add-to-list)
4821 (apply 'diary-add-to-list args)
4822 (apply 'add-to-diary-list args)))
4824 (defvar org-diary-last-run-time nil)
4826 ;;;###autoload
4827 (defun org-diary (&rest args)
4828 "Return diary information from org files.
4829 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4830 It accesses org files and extracts information from those files to be
4831 listed in the diary. The function accepts arguments specifying what
4832 items should be listed. For a list of arguments allowed here, see the
4833 variable `org-agenda-entry-types'.
4835 The call in the diary file should look like this:
4837 &%%(org-diary) ~/path/to/some/orgfile.org
4839 Use a separate line for each org file to check. Or, if you omit the file name,
4840 all files listed in `org-agenda-files' will be checked automatically:
4842 &%%(org-diary)
4844 If you don't give any arguments (as in the example above), the default
4845 arguments (:deadline :scheduled :timestamp :sexp) are used.
4846 So the example above may also be written as
4848 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4850 The function expects the lisp variables `entry' and `date' to be provided
4851 by the caller, because this is how the calendar works. Don't use this
4852 function from a program - use `org-agenda-get-day-entries' instead."
4853 (when (> (- (org-float-time)
4854 org-agenda-last-marker-time)
4856 ;; I am not sure if this works with sticky agendas, because the marker
4857 ;; list is then no longer a global variable.
4858 (org-agenda-reset-markers))
4859 ;; Prevent `org-compile-prefix-format' to fail when there is no agenda
4860 (when (buffer-live-p org-agenda-buffer)
4861 (org-compile-prefix-format 'agenda))
4862 (org-set-sorting-strategy 'agenda)
4863 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4864 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4865 (list entry)
4866 (org-agenda-files t)))
4867 (time (org-float-time))
4868 file rtn results)
4869 (when (or (not org-diary-last-run-time)
4870 (> (- time
4871 org-diary-last-run-time)
4873 (org-agenda-prepare-buffers files))
4874 (setq org-diary-last-run-time time)
4875 ;; If this is called during org-agenda, don't return any entries to
4876 ;; the calendar. Org Agenda will list these entries itself.
4877 (if org-disable-agenda-to-diary (setq files nil))
4878 (while (setq file (pop files))
4879 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4880 (setq results (append results rtn)))
4881 (if results
4882 (concat (org-finalize-agenda-entries results) "\n"))))
4884 ;;; Agenda entry finders
4886 (defun org-agenda-get-day-entries (file date &rest args)
4887 "Does the work for `org-diary' and `org-agenda'.
4888 FILE is the path to a file to be checked for entries. DATE is date like
4889 the one returned by `calendar-current-date'. ARGS are symbols indicating
4890 which kind of entries should be extracted. For details about these, see
4891 the documentation of `org-diary'."
4892 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4893 (let* ((org-startup-folded nil)
4894 (org-startup-align-all-tables nil)
4895 (buffer (if (file-exists-p file)
4896 (org-get-agenda-file-buffer file)
4897 (error "No such file %s" file)))
4898 arg results rtn deadline-results)
4899 (if (not buffer)
4900 ;; If file does not exist, make sure an error message ends up in diary
4901 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4902 (with-current-buffer buffer
4903 (unless (derived-mode-p 'org-mode)
4904 (error "Agenda file %s is not in `org-mode'" file))
4905 (let ((case-fold-search nil))
4906 (save-excursion
4907 (save-restriction
4908 (if org-agenda-restrict
4909 (narrow-to-region org-agenda-restrict-begin
4910 org-agenda-restrict-end)
4911 (widen))
4912 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4913 (while (setq arg (pop args))
4914 (cond
4915 ((and (eq arg :todo)
4916 (equal date (calendar-gregorian-from-absolute
4917 (org-today))))
4918 (setq rtn (org-agenda-get-todos))
4919 (setq results (append results rtn)))
4920 ((eq arg :timestamp)
4921 (setq rtn (org-agenda-get-blocks))
4922 (setq results (append results rtn))
4923 (setq rtn (org-agenda-get-timestamps deadline-results))
4924 (setq results (append results rtn)))
4925 ((eq arg :sexp)
4926 (setq rtn (org-agenda-get-sexps))
4927 (setq results (append results rtn)))
4928 ((eq arg :scheduled)
4929 (setq rtn (org-agenda-get-scheduled deadline-results))
4930 (setq results (append results rtn)))
4931 ((eq arg :closed)
4932 (setq rtn (org-agenda-get-progress))
4933 (setq results (append results rtn)))
4934 ((eq arg :deadline)
4935 (setq rtn (org-agenda-get-deadlines))
4936 (setq deadline-results (copy-sequence rtn))
4937 (setq results (append results rtn))))))))
4938 results))))
4940 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4941 (defun org-agenda-get-todos ()
4942 "Return the TODO information for agenda display."
4943 (let* ((props (list 'face nil
4944 'done-face 'org-agenda-done
4945 'org-not-done-regexp org-not-done-regexp
4946 'org-todo-regexp org-todo-regexp
4947 'org-complex-heading-regexp org-complex-heading-regexp
4948 'mouse-face 'highlight
4949 'help-echo
4950 (format "mouse-2 or RET jump to org file %s"
4951 (abbreviate-file-name buffer-file-name))))
4952 (regexp (format org-heading-keyword-regexp-format
4953 (cond
4954 ((and org-select-this-todo-keyword
4955 (equal org-select-this-todo-keyword "*"))
4956 org-todo-regexp)
4957 (org-select-this-todo-keyword
4958 (concat "\\("
4959 (mapconcat 'identity
4960 (org-split-string
4961 org-select-this-todo-keyword
4962 "|")
4963 "\\|") "\\)"))
4964 (t org-not-done-regexp))))
4965 marker priority category category-pos tags todo-state
4966 ee txt beg end)
4967 (goto-char (point-min))
4968 (while (re-search-forward regexp nil t)
4969 (catch :skip
4970 (save-match-data
4971 (beginning-of-line)
4972 (org-agenda-skip)
4973 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4974 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4975 (goto-char (1+ beg))
4976 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4977 (throw :skip nil)))
4978 (goto-char (match-beginning 2))
4979 (setq marker (org-agenda-new-marker (match-beginning 0))
4980 category (org-get-category)
4981 category-pos (get-text-property (point) 'org-category-position)
4982 txt (org-trim
4983 (buffer-substring (match-beginning 2) (match-end 0)))
4984 tags (org-get-tags-at (point))
4985 txt (org-agenda-format-item "" txt category tags)
4986 priority (1+ (org-get-priority txt))
4987 todo-state (org-get-todo-state))
4988 (org-add-props txt props
4989 'org-marker marker 'org-hd-marker marker
4990 'priority priority 'org-category category
4991 'org-category-position category-pos
4992 'type "todo" 'todo-state todo-state)
4993 (push txt ee)
4994 (if org-agenda-todo-list-sublevels
4995 (goto-char (match-end 2))
4996 (org-end-of-subtree 'invisible))))
4997 (nreverse ee)))
4999 (defun org-agenda-todo-custom-ignore-p (time n)
5000 "Check whether timestamp is farther away then n number of days.
5001 This function is invoked if `org-agenda-todo-ignore-deadlines',
5002 `org-agenda-todo-ignore-scheduled' or
5003 `org-agenda-todo-ignore-timestamp' is set to an integer."
5004 (let ((days (org-days-to-time time)))
5005 (if (>= n 0)
5006 (>= days n)
5007 (<= days n))))
5009 ;;;###autoload
5010 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5011 (&optional end)
5012 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5013 (when (or org-agenda-todo-ignore-with-date
5014 org-agenda-todo-ignore-scheduled
5015 org-agenda-todo-ignore-deadlines
5016 org-agenda-todo-ignore-timestamp)
5017 (setq end (or end (save-excursion (outline-next-heading) (point))))
5018 (save-excursion
5019 (or (and org-agenda-todo-ignore-with-date
5020 (re-search-forward org-ts-regexp end t))
5021 (and org-agenda-todo-ignore-scheduled
5022 (re-search-forward org-scheduled-time-regexp end t)
5023 (cond
5024 ((eq org-agenda-todo-ignore-scheduled 'future)
5025 (> (org-days-to-time (match-string 1)) 0))
5026 ((eq org-agenda-todo-ignore-scheduled 'past)
5027 (<= (org-days-to-time (match-string 1)) 0))
5028 ((numberp org-agenda-todo-ignore-scheduled)
5029 (org-agenda-todo-custom-ignore-p
5030 (match-string 1) org-agenda-todo-ignore-scheduled))
5031 (t)))
5032 (and org-agenda-todo-ignore-deadlines
5033 (re-search-forward org-deadline-time-regexp end t)
5034 (cond
5035 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5036 ((eq org-agenda-todo-ignore-deadlines 'far)
5037 (not (org-deadline-close (match-string 1))))
5038 ((eq org-agenda-todo-ignore-deadlines 'future)
5039 (> (org-days-to-time (match-string 1)) 0))
5040 ((eq org-agenda-todo-ignore-deadlines 'past)
5041 (<= (org-days-to-time (match-string 1)) 0))
5042 ((numberp org-agenda-todo-ignore-deadlines)
5043 (org-agenda-todo-custom-ignore-p
5044 (match-string 1) org-agenda-todo-ignore-deadlines))
5045 (t (org-deadline-close (match-string 1)))))
5046 (and org-agenda-todo-ignore-timestamp
5047 (let ((buffer (current-buffer))
5048 (regexp
5049 (concat
5050 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5051 (start (point)))
5052 ;; Copy current buffer into a temporary one
5053 (with-temp-buffer
5054 (insert-buffer-substring buffer start end)
5055 (goto-char (point-min))
5056 ;; Delete SCHEDULED and DEADLINE items
5057 (while (re-search-forward regexp end t)
5058 (delete-region (match-beginning 0) (match-end 0)))
5059 (goto-char (point-min))
5060 ;; No search for timestamp left
5061 (when (re-search-forward org-ts-regexp nil t)
5062 (cond
5063 ((eq org-agenda-todo-ignore-timestamp 'future)
5064 (> (org-days-to-time (match-string 1)) 0))
5065 ((eq org-agenda-todo-ignore-timestamp 'past)
5066 (<= (org-days-to-time (match-string 1)) 0))
5067 ((numberp org-agenda-todo-ignore-timestamp)
5068 (org-agenda-todo-custom-ignore-p
5069 (match-string 1) org-agenda-todo-ignore-timestamp))
5070 (t))))))))))
5072 (defconst org-agenda-no-heading-message
5073 "No heading for this item in buffer or region.")
5075 (defun org-agenda-get-timestamps (&optional deadline-results)
5076 "Return the date stamp information for agenda display."
5077 (let* ((props (list 'face 'org-agenda-calendar-event
5078 'org-not-done-regexp org-not-done-regexp
5079 'org-todo-regexp org-todo-regexp
5080 'org-complex-heading-regexp org-complex-heading-regexp
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 (d1 (calendar-absolute-from-gregorian date))
5087 (deadline-position-alist
5088 (mapcar (lambda (a) (and (setq mm (get-text-property
5089 0 'org-hd-marker a))
5090 (cons (marker-position mm) a)))
5091 deadline-results))
5092 (remove-re org-ts-regexp)
5093 (regexp
5094 (concat
5095 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5096 (regexp-quote
5097 (substring
5098 (format-time-string
5099 (car org-time-stamp-formats)
5100 (apply 'encode-time ; DATE bound by calendar
5101 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5102 1 11))
5103 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5104 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5105 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5106 donep tmp priority category category-pos ee txt timestr tags
5107 b0 b3 e3 head todo-state end-of-match show-all warntime)
5108 (goto-char (point-min))
5109 (while (setq end-of-match (re-search-forward regexp nil t))
5110 (setq b0 (match-beginning 0)
5111 b3 (match-beginning 3) e3 (match-end 3)
5112 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5113 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5114 (member todo-state
5115 org-agenda-repeating-timestamp-show-all)))
5116 (catch :skip
5117 (and (org-at-date-range-p) (throw :skip nil))
5118 (org-agenda-skip)
5119 (if (and (match-end 1)
5120 (not (= d1 (org-time-string-to-absolute
5121 (match-string 1) d1 nil show-all
5122 (current-buffer) b0))))
5123 (throw :skip nil))
5124 (if (and e3
5125 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5126 (throw :skip nil))
5127 (setq tmp (buffer-substring (max (point-min)
5128 (- b0 org-ds-keyword-length))
5130 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5131 inactivep (= (char-after b0) ?\[)
5132 deadlinep (string-match org-deadline-regexp tmp)
5133 scheduledp (string-match org-scheduled-regexp tmp)
5134 closedp (and org-agenda-include-inactive-timestamps
5135 (string-match org-closed-string tmp))
5136 clockp (and org-agenda-include-inactive-timestamps
5137 (or (string-match org-clock-string tmp)
5138 (string-match "]-+\\'" tmp)))
5139 warntime (org-entry-get (point) "APPT_WARNTIME")
5140 donep (member todo-state org-done-keywords))
5141 (if (or scheduledp deadlinep closedp clockp
5142 (and donep org-agenda-skip-timestamp-if-done))
5143 (throw :skip t))
5144 (if (string-match ">" timestr)
5145 ;; substring should only run to end of time stamp
5146 (setq timestr (substring timestr 0 (match-end 0))))
5147 (setq marker (org-agenda-new-marker b0)
5148 category (org-get-category b0)
5149 category-pos (get-text-property b0 'org-category-position))
5150 (save-excursion
5151 (if (not (re-search-backward org-outline-regexp-bol nil t))
5152 (setq txt org-agenda-no-heading-message)
5153 (goto-char (match-beginning 0))
5154 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5155 (assoc (point) deadline-position-alist))
5156 (throw :skip nil))
5157 (setq hdmarker (org-agenda-new-marker)
5158 tags (org-get-tags-at))
5159 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5160 (setq head (or (match-string 1) ""))
5161 (setq txt (org-agenda-format-item
5162 (if inactivep org-agenda-inactive-leader nil)
5163 head category tags timestr
5164 remove-re)))
5165 (setq priority (org-get-priority txt))
5166 (org-add-props txt props
5167 'org-marker marker 'org-hd-marker hdmarker)
5168 (org-add-props txt nil 'priority priority
5169 'org-category category 'date date
5170 'org-category-position category-pos
5171 'todo-state todo-state
5172 'warntime warntime
5173 'type "timestamp")
5174 (push txt ee))
5175 (if org-agenda-skip-additional-timestamps-same-entry
5176 (outline-next-heading)
5177 (goto-char end-of-match))))
5178 (nreverse ee)))
5180 (defun org-agenda-get-sexps ()
5181 "Return the sexp information for agenda display."
5182 (require 'diary-lib)
5183 (let* ((props (list 'face 'org-agenda-calendar-sexp
5184 'mouse-face 'highlight
5185 'help-echo
5186 (format "mouse-2 or RET jump to org file %s"
5187 (abbreviate-file-name buffer-file-name))))
5188 (regexp "^&?%%(")
5189 marker category extra category-pos ee txt tags entry
5190 result beg b sexp sexp-entry todo-state warntime)
5191 (goto-char (point-min))
5192 (while (re-search-forward regexp nil t)
5193 (catch :skip
5194 (org-agenda-skip)
5195 (setq beg (match-beginning 0))
5196 (goto-char (1- (match-end 0)))
5197 (setq b (point))
5198 (forward-sexp 1)
5199 (setq sexp (buffer-substring b (point)))
5200 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5201 (org-trim (match-string 1))
5202 ""))
5203 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5204 (when result
5205 (setq marker (org-agenda-new-marker beg)
5206 category (org-get-category beg)
5207 category-pos (get-text-property beg 'org-category-position)
5208 tags (save-excursion (org-backward-heading-same-level 0)
5209 (org-get-tags-at))
5210 todo-state (org-get-todo-state)
5211 warntime (org-entry-get (point) "APPT_WARNTIME"))
5213 (dolist (r (if (stringp result)
5214 (list result)
5215 result)) ;; we expect a list here
5216 (when (and org-agenda-diary-sexp-prefix
5217 (string-match org-agenda-diary-sexp-prefix r))
5218 (setq extra (match-string 0 r)
5219 r (replace-match "" nil nil r)))
5220 (if (string-match "\\S-" r)
5221 (setq txt r)
5222 (setq txt "SEXP entry returned empty string"))
5224 (setq txt (org-agenda-format-item
5225 extra txt category tags 'time))
5226 (org-add-props txt props 'org-marker marker)
5227 (org-add-props txt nil
5228 'org-category category 'date date 'todo-state todo-state
5229 'org-category-position category-pos 'tags tags
5230 'type "sexp" 'warntime warntime)
5231 (push txt ee)))))
5232 (nreverse ee)))
5234 ;; Calendar sanity: define some functions that are independent of
5235 ;; `calendar-date-style'.
5236 ;; Normally I would like to use ISO format when calling the diary functions,
5237 ;; but to make sure we still have Emacs 22 compatibility we bind
5238 ;; also `european-calendar-style' and use european format
5239 (defun org-anniversary (year month day &optional mark)
5240 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5241 (org-no-warnings
5242 (let ((calendar-date-style 'european) (european-calendar-style t))
5243 (diary-anniversary day month year mark))))
5244 (defun org-cyclic (N year month day &optional mark)
5245 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5246 (org-no-warnings
5247 (let ((calendar-date-style 'european) (european-calendar-style t))
5248 (diary-cyclic N day month year mark))))
5249 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5250 "Like `diary-block', but with fixed (ISO) order of arguments."
5251 (org-no-warnings
5252 (let ((calendar-date-style 'european) (european-calendar-style t))
5253 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5254 (defun org-date (year month day &optional mark)
5255 "Like `diary-date', but with fixed (ISO) order of arguments."
5256 (org-no-warnings
5257 (let ((calendar-date-style 'european) (european-calendar-style t))
5258 (diary-date day month year mark))))
5259 (defalias 'org-float 'diary-float)
5261 ;; Define the` org-class' function
5262 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5263 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5264 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5265 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5266 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5267 `holidays', then any date that is known by the Emacs calendar to be a
5268 holiday will also be skipped."
5269 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5270 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5271 (d (calendar-absolute-from-gregorian date)))
5272 (and
5273 (<= date1 d)
5274 (<= d date2)
5275 (= (calendar-day-of-week date) dayname)
5276 (or (not skip-weeks)
5277 (progn
5278 (require 'cal-iso)
5279 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5280 (not (and (memq 'holidays skip-weeks)
5281 (calendar-check-holidays date)))
5282 entry)))
5284 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5285 "Like `org-class', but honor `calendar-date-style'.
5286 The order of the first 2 times 3 arguments depends on the variable
5287 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5288 So for American calendars, give this as MONTH DAY YEAR, for European as
5289 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5290 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5291 is any number of ISO weeks in the block period for which the item should
5292 be skipped.
5294 This function is here only for backward compatibility and it is deprecated,
5295 please use `org-class' instead."
5296 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5297 (date2 (org-order-calendar-date-args m2 d2 y2)))
5298 (org-class
5299 (nth 2 date1) (car date1) (nth 1 date1)
5300 (nth 2 date2) (car date2) (nth 1 date2)
5301 dayname skip-weeks)))
5302 (make-obsolete 'org-diary-class 'org-class "")
5304 (defvar org-agenda-show-log-scoped) ;; dynamically scope in ̀org-timeline' or`org-agenda-list'
5305 (defalias 'org-get-closed 'org-agenda-get-progress)
5306 (defun org-agenda-get-progress ()
5307 "Return the logged TODO entries for agenda display."
5308 (let* ((props (list 'mouse-face 'highlight
5309 'org-not-done-regexp org-not-done-regexp
5310 'org-todo-regexp org-todo-regexp
5311 'org-complex-heading-regexp org-complex-heading-regexp
5312 'help-echo
5313 (format "mouse-2 or RET jump to org file %s"
5314 (abbreviate-file-name buffer-file-name))))
5315 (items (if (consp org-agenda-show-log-scoped)
5316 org-agenda-show-log-scoped
5317 (if (eq org-agenda-show-log-scoped 'clockcheck)
5318 '(clock)
5319 org-agenda-log-mode-items)))
5320 (parts
5321 (delq nil
5322 (list
5323 (if (memq 'closed items) (concat "\\<" org-closed-string))
5324 (if (memq 'clock items) (concat "\\<" org-clock-string))
5325 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5326 (parts-re (if parts (mapconcat 'identity parts "\\|")
5327 (error "`org-agenda-log-mode-items' is empty")))
5328 (regexp (concat
5329 "\\(" parts-re "\\)"
5330 " *\\["
5331 (regexp-quote
5332 (substring
5333 (format-time-string
5334 (car org-time-stamp-formats)
5335 (apply 'encode-time ; DATE bound by calendar
5336 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5337 1 11))))
5338 (org-agenda-search-headline-for-time nil)
5339 marker hdmarker priority category category-pos tags closedp
5340 statep clockp state ee txt extra timestr rest clocked)
5341 (goto-char (point-min))
5342 (while (re-search-forward regexp nil t)
5343 (catch :skip
5344 (org-agenda-skip)
5345 (setq marker (org-agenda-new-marker (match-beginning 0))
5346 closedp (equal (match-string 1) org-closed-string)
5347 statep (equal (string-to-char (match-string 1)) ?-)
5348 clockp (not (or closedp statep))
5349 state (and statep (match-string 2))
5350 category (org-get-category (match-beginning 0))
5351 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5352 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5353 (when (string-match "\\]" timestr)
5354 ;; substring should only run to end of time stamp
5355 (setq rest (substring timestr (match-end 0))
5356 timestr (substring timestr 0 (match-end 0)))
5357 (if (and (not closedp) (not statep)
5358 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5359 rest))
5360 (progn (setq timestr (concat (substring timestr 0 -1)
5361 "-" (match-string 1 rest) "]"))
5362 (setq clocked (match-string 2 rest)))
5363 (setq clocked "-")))
5364 (save-excursion
5365 (setq extra
5366 (cond
5367 ((not org-agenda-log-mode-add-notes) nil)
5368 (statep
5369 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5370 (match-string 1)))
5371 (clockp
5372 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5373 (match-string 1)))))
5374 (if (not (re-search-backward org-outline-regexp-bol nil t))
5375 (setq txt org-agenda-no-heading-message)
5376 (goto-char (match-beginning 0))
5377 (setq hdmarker (org-agenda-new-marker)
5378 tags (org-get-tags-at))
5379 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5380 (setq txt (match-string 1))
5381 (when extra
5382 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5383 (setq txt (concat (substring txt 0 (match-beginning 1))
5384 " - " extra " " (match-string 2 txt)))
5385 (setq txt (concat txt " - " extra))))
5386 (setq txt (org-agenda-format-item
5387 (cond
5388 (closedp "Closed: ")
5389 (statep (concat "State: (" state ")"))
5390 (t (concat "Clocked: (" clocked ")")))
5391 txt category tags timestr)))
5392 (setq priority 100000)
5393 (org-add-props txt props
5394 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5395 'priority priority 'org-category category
5396 'org-category-position category-pos
5397 'type "closed" 'date date
5398 'undone-face 'org-warning 'done-face 'org-agenda-done)
5399 (push txt ee))
5400 (goto-char (point-at-eol))))
5401 (nreverse ee)))
5403 (defun org-agenda-show-clocking-issues ()
5404 "Add overlays, showing issues with clocking.
5405 See also the user option `org-agenda-clock-consistency-checks'."
5406 (interactive)
5407 (let* ((pl org-agenda-clock-consistency-checks)
5408 (re (concat "^[ \t]*"
5409 org-clock-string
5410 "[ \t]+"
5411 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5412 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5413 (tlstart 0.)
5414 (tlend 0.)
5415 (maxtime (org-hh:mm-string-to-minutes
5416 (or (plist-get pl :max-duration) "24:00")))
5417 (mintime (org-hh:mm-string-to-minutes
5418 (or (plist-get pl :min-duration) 0)))
5419 (maxgap (org-hh:mm-string-to-minutes
5420 ;; default 30:00 means never complain
5421 (or (plist-get pl :max-gap) "30:00")))
5422 (gapok (mapcar 'org-hh:mm-string-to-minutes
5423 (plist-get pl :gap-ok-around)))
5424 (def-face (or (plist-get pl :default-face)
5425 '((:background "DarkRed") (:foreground "white"))))
5426 issue face m te ts dt ov)
5427 (goto-char (point-min))
5428 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5429 (setq issue nil face def-face)
5430 (catch 'next
5431 (setq m (org-get-at-bol 'org-marker)
5432 te nil ts nil)
5433 (unless (and m (markerp m))
5434 (setq issue "No valid clock line") (throw 'next t))
5435 (org-with-point-at m
5436 (save-excursion
5437 (goto-char (point-at-bol))
5438 (unless (looking-at re)
5439 (error "No valid Clock line")
5440 (throw 'next t))
5441 (unless (match-end 3)
5442 (setq issue "No end time"
5443 face (or (plist-get pl :no-end-time-face) face))
5444 (throw 'next t))
5445 (setq ts (match-string 1)
5446 te (match-string 3)
5447 ts (org-float-time
5448 (apply 'encode-time (org-parse-time-string ts)))
5449 te (org-float-time
5450 (apply 'encode-time (org-parse-time-string te)))
5451 dt (- te ts))))
5452 (cond
5453 ((> dt (* 60 maxtime))
5454 ;; a very long clocking chunk
5455 (setq issue (format "Clocking interval is very long: %s"
5456 (org-minutes-to-hh:mm-string
5457 (floor (/ (float dt) 60.))))
5458 face (or (plist-get pl :long-face) face)))
5459 ((< dt (* 60 mintime))
5460 ;; a very short clocking chunk
5461 (setq issue (format "Clocking interval is very short: %s"
5462 (org-minutes-to-hh:mm-string
5463 (floor (/ (float dt) 60.))))
5464 face (or (plist-get pl :short-face) face)))
5465 ((and (> tlend 0) (< ts tlend))
5466 ;; Two clock entries are overlapping
5467 (setq issue (format "Clocking overlap: %d minutes"
5468 (/ (- tlend ts) 60))
5469 face (or (plist-get pl :overlap-face) face)))
5470 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5471 ;; There is a gap, lets see if we need to report it
5472 (unless (org-agenda-check-clock-gap tlend ts gapok)
5473 (setq issue (format "Clocking gap: %d minutes"
5474 (/ (- ts tlend) 60))
5475 face (or (plist-get pl :gap-face) face))))
5476 (t nil)))
5477 (setq tlend (or te tlend) tlstart (or ts tlstart))
5478 (when issue
5479 ;; OK, there was some issue, add an overlay to show the issue
5480 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5481 (overlay-put ov 'before-string
5482 (concat
5483 (org-add-props
5484 (format "%-43s" (concat " " issue))
5486 'face face)
5487 "\n"))
5488 (overlay-put ov 'evaporate t)))))
5490 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5491 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5492 (catch 'exit
5493 (unless ok-list
5494 ;; there are no OK times for gaps...
5495 (throw 'exit nil))
5496 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5497 ;; This is more than 24 hours, so it is OK.
5498 ;; because we have at least one OK time, that must be in the
5499 ;; 24 hour interval.
5500 (throw 'exit t))
5501 ;; We have a shorter gap.
5502 ;; Now we have to get the minute of the day when these times are
5503 (let* ((t1dec (decode-time (seconds-to-time t1)))
5504 (t2dec (decode-time (seconds-to-time t2)))
5505 ;; compute the minute on the day
5506 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5507 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5508 (when (< min2 min1)
5509 ;; if min2 is smaller than min1, this means it is on the next day.
5510 ;; Wrap it to after midnight.
5511 (setq min2 (+ min2 1440)))
5512 ;; Now check if any of the OK times is in the gap
5513 (mapc (lambda (x)
5514 ;; Wrap the time to after midnight if necessary
5515 (if (< x min1) (setq x (+ x 1440)))
5516 ;; Check if in interval
5517 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5518 ok-list)
5519 ;; Nope, this gap is not OK
5520 nil)))
5522 (defun org-agenda-get-deadlines ()
5523 "Return the deadline information for agenda display."
5524 (let* ((props (list 'mouse-face 'highlight
5525 'org-not-done-regexp org-not-done-regexp
5526 'org-todo-regexp org-todo-regexp
5527 'org-complex-heading-regexp org-complex-heading-regexp
5528 'help-echo
5529 (format "mouse-2 or RET jump to org file %s"
5530 (abbreviate-file-name buffer-file-name))))
5531 (regexp org-deadline-time-regexp)
5532 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5533 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5534 d2 diff dfrac wdays pos pos1 category category-pos
5535 tags suppress-prewarning ee txt head face s todo-state
5536 show-all upcomingp donep timestr warntime)
5537 (goto-char (point-min))
5538 (while (re-search-forward regexp nil t)
5539 (setq suppress-prewarning nil)
5540 (catch :skip
5541 (org-agenda-skip)
5542 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5543 (save-match-data
5544 (string-match org-scheduled-time-regexp
5545 (buffer-substring (point-at-bol)
5546 (point-at-eol)))))
5547 (setq suppress-prewarning
5548 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5549 org-agenda-skip-deadline-prewarning-if-scheduled
5550 0)))
5551 (setq s (match-string 1)
5552 txt nil
5553 pos (1- (match-beginning 1))
5554 todo-state (save-match-data (org-get-todo-state))
5555 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5556 (member todo-state
5557 org-agenda-repeating-timestamp-show-all))
5558 d2 (org-time-string-to-absolute
5559 (match-string 1) d1 'past show-all
5560 (current-buffer) pos)
5561 diff (- d2 d1)
5562 wdays (if suppress-prewarning
5563 (let ((org-deadline-warning-days suppress-prewarning))
5564 (org-get-wdays s))
5565 (org-get-wdays s))
5566 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5567 upcomingp (and todayp (> diff 0)))
5568 ;; When to show a deadline in the calendar:
5569 ;; If the expiration is within wdays warning time.
5570 ;; Past-due deadlines are only shown on the current date
5571 (if (and (or (and (<= diff wdays)
5572 (and todayp (not org-agenda-only-exact-dates)))
5573 (= diff 0)))
5574 (save-excursion
5575 ;; (setq todo-state (org-get-todo-state))
5576 (setq donep (member todo-state org-done-keywords))
5577 (if (and donep
5578 (or org-agenda-skip-deadline-if-done
5579 (not (= diff 0))))
5580 (setq txt nil)
5581 (setq category (org-get-category)
5582 warntime (org-entry-get (point) "APPT_WARNTIME")
5583 category-pos (get-text-property (point) 'org-category-position))
5584 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5585 (setq txt org-agenda-no-heading-message)
5586 (goto-char (match-end 0))
5587 (setq pos1 (match-beginning 0))
5588 (setq tags (org-get-tags-at pos1))
5589 (setq head (buffer-substring-no-properties
5590 (point)
5591 (progn (skip-chars-forward "^\r\n")
5592 (point))))
5593 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5594 (setq timestr
5595 (concat (substring s (match-beginning 1)) " "))
5596 (setq timestr 'time))
5597 (setq txt (org-agenda-format-item
5598 (if (= diff 0)
5599 (car org-agenda-deadline-leaders)
5600 (if (functionp
5601 (nth 1 org-agenda-deadline-leaders))
5602 (funcall
5603 (nth 1 org-agenda-deadline-leaders)
5604 diff date)
5605 (format (nth 1 org-agenda-deadline-leaders)
5606 diff)))
5607 head category tags
5608 (if (not (= diff 0)) nil timestr)))))
5609 (when txt
5610 (setq face (org-agenda-deadline-face dfrac))
5611 (org-add-props txt props
5612 'org-marker (org-agenda-new-marker pos)
5613 'warntime warntime
5614 'org-hd-marker (org-agenda-new-marker pos1)
5615 'priority (+ (- diff)
5616 (org-get-priority txt))
5617 'org-category category
5618 'org-category-position category-pos
5619 'todo-state todo-state
5620 'type (if upcomingp "upcoming-deadline" "deadline")
5621 'date (if upcomingp date d2)
5622 'face (if donep 'org-agenda-done face)
5623 'undone-face face 'done-face 'org-agenda-done)
5624 (push txt ee))))))
5625 (nreverse ee)))
5627 (defun org-agenda-deadline-face (fraction)
5628 "Return the face to displaying a deadline item.
5629 FRACTION is what fraction of the head-warning time has passed."
5630 (let ((faces org-agenda-deadline-faces) f)
5631 (catch 'exit
5632 (while (setq f (pop faces))
5633 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5635 (defun org-agenda-get-scheduled (&optional deadline-results)
5636 "Return the scheduled information for agenda display."
5637 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5638 'org-todo-regexp org-todo-regexp
5639 'org-complex-heading-regexp org-complex-heading-regexp
5640 'done-face 'org-agenda-done
5641 'mouse-face 'highlight
5642 'help-echo
5643 (format "mouse-2 or RET jump to org file %s"
5644 (abbreviate-file-name buffer-file-name))))
5645 (regexp org-scheduled-time-regexp)
5646 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5647 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5649 (deadline-position-alist
5650 (mapcar (lambda (a) (and (setq mm (get-text-property
5651 0 'org-hd-marker a))
5652 (cons (marker-position mm) a)))
5653 deadline-results))
5654 d2 diff pos pos1 category category-pos tags donep
5655 ee txt head pastschedp todo-state face timestr s habitp show-all
5656 did-habit-check-p warntime)
5657 (goto-char (point-min))
5658 (while (re-search-forward regexp nil t)
5659 (catch :skip
5660 (org-agenda-skip)
5661 (setq s (match-string 1)
5662 txt nil
5663 pos (1- (match-beginning 1))
5664 todo-state (save-match-data (org-get-todo-state))
5665 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5666 (member todo-state
5667 org-agenda-repeating-timestamp-show-all))
5668 d2 (org-time-string-to-absolute
5669 (match-string 1) d1 'past show-all
5670 (current-buffer) pos)
5671 diff (- d2 d1)
5672 warntime (org-entry-get (point) "APPT_WARNTIME"))
5673 (setq pastschedp (and todayp (< diff 0)))
5674 (setq did-habit-check-p nil)
5675 ;; When to show a scheduled item in the calendar:
5676 ;; If it is on or past the date.
5677 (when (or (and (< diff 0)
5678 (< (abs diff) org-scheduled-past-days)
5679 (and todayp (not org-agenda-only-exact-dates)))
5680 (= diff 0)
5681 ;; org-is-habit-p uses org-entry-get, which is expansive
5682 ;; so we go extra mile to only call it once
5683 (and todayp
5684 org-habit-show-all-today
5685 (setq did-habit-check-p t)
5686 (setq habitp (and (functionp 'org-is-habit-p)
5687 (org-is-habit-p)))))
5688 (save-excursion
5689 (setq donep (member todo-state org-done-keywords))
5690 (if (and donep
5691 (or org-agenda-skip-scheduled-if-done
5692 (not (= diff 0))
5693 (and (functionp 'org-is-habit-p)
5694 (org-is-habit-p))))
5695 (setq txt nil)
5696 (setq habitp (if did-habit-check-p habitp
5697 (and (functionp 'org-is-habit-p)
5698 (org-is-habit-p))))
5699 (setq category (org-get-category)
5700 category-pos (get-text-property (point) 'org-category-position))
5701 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5702 (setq txt org-agenda-no-heading-message)
5703 (goto-char (match-end 0))
5704 (setq pos1 (match-beginning 0))
5705 (if habitp
5706 (if (or (not org-habit-show-habits)
5707 (and (not todayp)
5708 org-habit-show-habits-only-for-today))
5709 (throw :skip nil))
5710 (if (and
5711 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5712 (and org-agenda-skip-scheduled-if-deadline-is-shown
5713 pastschedp))
5714 (setq mm (assoc pos1 deadline-position-alist)))
5715 (throw :skip nil)))
5716 (setq tags (org-get-tags-at))
5717 (setq head (buffer-substring-no-properties
5718 (point)
5719 (progn (skip-chars-forward "^\r\n") (point))))
5720 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5721 (setq timestr
5722 (concat (substring s (match-beginning 1)) " "))
5723 (setq timestr 'time))
5724 (setq txt (org-agenda-format-item
5725 (if (= diff 0)
5726 (car org-agenda-scheduled-leaders)
5727 (format (nth 1 org-agenda-scheduled-leaders)
5728 (- 1 diff)))
5729 head category tags
5730 (if (not (= diff 0)) nil timestr)
5731 nil habitp))))
5732 (when txt
5733 (setq face
5734 (cond
5735 ((and (not habitp) pastschedp)
5736 'org-scheduled-previously)
5737 (todayp 'org-scheduled-today)
5738 (t 'org-scheduled))
5739 habitp (and habitp (org-habit-parse-todo)))
5740 (org-add-props txt props
5741 'undone-face face
5742 'face (if donep 'org-agenda-done face)
5743 'org-marker (org-agenda-new-marker pos)
5744 'org-hd-marker (org-agenda-new-marker pos1)
5745 'type (if pastschedp "past-scheduled" "scheduled")
5746 'date (if pastschedp d2 date)
5747 'warntime warntime
5748 'priority (if habitp
5749 (org-habit-get-priority habitp)
5750 (+ 94 (- 5 diff) (org-get-priority txt)))
5751 'org-category category
5752 'category-position category-pos
5753 'org-habit-p habitp
5754 'todo-state todo-state)
5755 (push txt ee))))))
5756 (nreverse ee)))
5758 (defun org-agenda-get-blocks ()
5759 "Return the date-range information for agenda display."
5760 (let* ((props (list 'face nil
5761 'org-not-done-regexp org-not-done-regexp
5762 'org-todo-regexp org-todo-regexp
5763 'org-complex-heading-regexp org-complex-heading-regexp
5764 'mouse-face 'highlight
5765 'help-echo
5766 (format "mouse-2 or RET jump to org file %s"
5767 (abbreviate-file-name buffer-file-name))))
5768 (regexp org-tr-regexp)
5769 (d0 (calendar-absolute-from-gregorian date))
5770 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
5771 todo-state tags pos head donep)
5772 (goto-char (point-min))
5773 (while (re-search-forward regexp nil t)
5774 (catch :skip
5775 (org-agenda-skip)
5776 (setq pos (point))
5777 (let ((start-time (match-string 1))
5778 (end-time (match-string 2)))
5779 (setq s1 (match-string 1)
5780 s2 (match-string 2)
5781 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5782 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5783 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5784 ;; Only allow days between the limits, because the normal
5785 ;; date stamps will catch the limits.
5786 (save-excursion
5787 (setq todo-state (org-get-todo-state))
5788 (setq donep (member todo-state org-done-keywords))
5789 (if (and donep org-agenda-skip-timestamp-if-done)
5790 (throw :skip t))
5791 (setq marker (org-agenda-new-marker (point)))
5792 (setq category (org-get-category)
5793 category-pos (get-text-property (point) 'org-category-position))
5794 (if (not (re-search-backward org-outline-regexp-bol nil t))
5795 (setq txt org-agenda-no-heading-message)
5796 (goto-char (match-beginning 0))
5797 (setq hdmarker (org-agenda-new-marker (point)))
5798 (setq tags (org-get-tags-at))
5799 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5800 (setq head (match-string 1))
5801 (let ((remove-re
5802 (if org-agenda-remove-timeranges-from-blocks
5803 (concat
5804 "<" (regexp-quote s1) ".*?>"
5805 "--"
5806 "<" (regexp-quote s2) ".*?>")
5807 nil)))
5808 (setq txt (org-agenda-format-item
5809 (format
5810 (nth (if (= d1 d2) 0 1)
5811 org-agenda-timerange-leaders)
5812 (1+ (- d0 d1)) (1+ (- d2 d1)))
5813 head category tags
5814 (cond ((and (= d1 d0) (= d2 d0))
5815 (concat "<" start-time ">--<" end-time ">"))
5816 ((= d1 d0)
5817 (concat "<" start-time ">"))
5818 ((= d2 d0)
5819 (concat "<" end-time ">")))
5820 remove-re))))
5821 (org-add-props txt props
5822 'org-marker marker 'org-hd-marker hdmarker
5823 'type "block" 'date date
5824 'todo-state todo-state
5825 'priority (org-get-priority txt) 'org-category category
5826 'org-category-position category-pos)
5827 (push txt ee))))
5828 (goto-char pos)))
5829 ;; Sort the entries by expiration date.
5830 (nreverse ee)))
5832 ;;; Agenda presentation and sorting
5834 (defvar org-prefix-has-time nil
5835 "A flag, set by `org-compile-prefix-format'.
5836 The flag is set if the currently compiled format contains a `%t'.")
5837 (defvar org-prefix-has-tag nil
5838 "A flag, set by `org-compile-prefix-format'.
5839 The flag is set if the currently compiled format contains a `%T'.")
5840 (defvar org-prefix-has-effort nil
5841 "A flag, set by `org-compile-prefix-format'.
5842 The flag is set if the currently compiled format contains a `%e'.")
5843 (defvar org-prefix-category-length nil
5844 "Used by `org-compile-prefix-format' to remember the category field width.")
5845 (defvar org-prefix-category-max-length nil
5846 "Used by `org-compile-prefix-format' to remember the category field width.")
5848 (defun org-agenda-get-category-icon (category)
5849 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5850 (dolist (entry org-agenda-category-icon-alist)
5851 (when (org-string-match-p (car entry) category)
5852 (if (listp (cadr entry))
5853 (return (cadr entry))
5854 (return (apply 'create-image (cdr entry)))))))
5856 (defun org-agenda-format-item (extra txt &optional category tags dotime
5857 remove-re habitp)
5858 "Format TXT to be inserted into the agenda buffer.
5859 In particular, it adds the prefix and corresponding text properties. EXTRA
5860 must be a string and replaces the `%s' specifier in the prefix format.
5861 CATEGORY (string, symbol or nil) may be used to overrule the default
5862 category taken from local variable or file name. It will replace the `%c'
5863 specifier in the format. DOTIME, when non-nil, indicates that a
5864 time-of-day should be extracted from TXT for sorting of this entry, and for
5865 the `%t' specifier in the format. When DOTIME is a string, this string is
5866 searched for a time before TXT is. TAGS can be the tags of the headline.
5867 Any match of REMOVE-RE will be removed from TXT."
5868 ;; We keep the org-prefix-* variable values along with a compiled
5869 ;; formatter, so that multiple agendas existing at the same time, do
5870 ;; not step on each other toes.
5872 ;; It was inconvenient to make these variables buffer local in
5873 ;; Agenda buffers, because this function expects to be called with
5874 ;; the buffer where item comes from being current, and not agenda
5875 ;; buffer
5876 (let* ((bindings (car org-prefix-format-compiled))
5877 (formatter (cadr org-prefix-format-compiled)))
5878 (loop for (var value) in bindings
5879 do (set var value))
5880 (save-match-data
5881 ;; Diary entries sometimes have extra whitespace at the beginning
5882 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5884 ;; Fix the tags part in txt
5885 (setq txt (org-agenda-fix-displayed-tags
5886 txt tags
5887 org-agenda-show-inherited-tags
5888 org-agenda-hide-tags-regexp))
5889 (let* ((category (or category
5890 (if (stringp org-category)
5891 org-category
5892 (and org-category (symbol-name org-category)))
5893 (if buffer-file-name
5894 (file-name-sans-extension
5895 (file-name-nondirectory buffer-file-name))
5896 "")))
5897 (category-icon (org-agenda-get-category-icon category))
5898 (category-icon (if category-icon
5899 (propertize " " 'display category-icon)
5900 ""))
5901 ;; time, tag, effort are needed for the eval of the prefix format
5902 (tag (if tags (nth (1- (length tags)) tags) ""))
5903 time effort neffort
5904 (ts (if dotime (concat
5905 (if (stringp dotime) dotime "")
5906 (and org-agenda-search-headline-for-time txt))))
5907 (time-of-day (and dotime (org-get-time-of-day ts)))
5908 stamp plain s0 s1 s2 rtn srp l
5909 duration thecategory)
5910 (and (derived-mode-p 'org-mode) buffer-file-name
5911 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5912 (when (and dotime time-of-day)
5913 ;; Extract starting and ending time and move them to prefix
5914 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5915 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5916 (setq s0 (match-string 0 ts)
5917 srp (and stamp (match-end 3))
5918 s1 (match-string (if plain 1 2) ts)
5919 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5921 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5922 ;; them, we might want to remove them there to avoid duplication.
5923 ;; The user can turn this off with a variable.
5924 (if (and org-prefix-has-time
5925 org-agenda-remove-times-when-in-prefix (or stamp plain)
5926 (string-match (concat (regexp-quote s0) " *") txt)
5927 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5928 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5929 (= (match-beginning 0) 0)
5931 (setq txt (replace-match "" nil nil txt))))
5932 ;; Normalize the time(s) to 24 hour
5933 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5934 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5936 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5937 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5938 (setq s2
5939 (org-minutes-to-hh:mm-string
5940 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5942 ;; Compute the duration
5943 (when s2
5944 (setq duration (- (org-hh:mm-string-to-minutes s2)
5945 (org-hh:mm-string-to-minutes s1)))))
5947 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5948 txt)
5949 ;; Tags are in the string
5950 (if (or (eq org-agenda-remove-tags t)
5951 (and org-agenda-remove-tags
5952 org-prefix-has-tag))
5953 (setq txt (replace-match "" t t txt))
5954 (setq txt (replace-match
5955 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5956 (match-string 2 txt))
5957 t t txt))))
5958 (when (derived-mode-p 'org-mode)
5959 (setq effort
5960 (condition-case nil
5961 (org-get-effort
5962 (or (get-text-property 0 'org-hd-marker txt)
5963 (get-text-property 0 'org-marker txt)))
5964 (error nil)))
5965 (when effort
5966 (setq neffort (org-duration-string-to-minutes effort)
5967 effort (setq effort (concat "[" effort "]")))))
5968 ;; prevent erroring out with %e format when there is no effort
5969 (or effort (setq effort ""))
5971 (when remove-re
5972 (while (string-match remove-re txt)
5973 (setq txt (replace-match "" t t txt))))
5975 ;; Set org-heading property on `txt' to mark the start of the
5976 ;; heading.
5977 (add-text-properties 0 (length txt) '(org-heading t) txt)
5979 ;; Prepare the variables needed in the eval of the compiled format
5980 (setq time (cond (s2 (concat
5981 (org-agenda-time-of-day-to-ampm-maybe s1)
5982 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5983 (if org-agenda-timegrid-use-ampm " ")))
5984 (s1 (concat
5985 (org-agenda-time-of-day-to-ampm-maybe s1)
5986 (if org-agenda-timegrid-use-ampm
5987 "........ "
5988 "......")))
5989 (t ""))
5990 extra (or (and (not habitp) extra) "")
5991 category (if (symbolp category) (symbol-name category) category)
5992 thecategory (copy-sequence category))
5993 (if (string-match org-bracket-link-regexp category)
5994 (progn
5995 (setq l (if (match-end 3)
5996 (- (match-end 3) (match-beginning 3))
5997 (- (match-end 1) (match-beginning 1))))
5998 (when (< l (or org-prefix-category-length 0))
5999 (setq category (copy-sequence category))
6000 (org-add-props category nil
6001 'extra-space (make-string
6002 (- org-prefix-category-length l 1) ?\ ))))
6003 (if (and org-prefix-category-max-length
6004 (>= (length category) org-prefix-category-max-length))
6005 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6006 ;; Evaluate the compiled format
6007 (setq rtn (concat (eval formatter) txt))
6009 ;; And finally add the text properties
6010 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6011 (org-add-props rtn nil
6012 'org-category (if thecategory (downcase thecategory) category)
6013 'tags (mapcar 'org-downcase-keep-props tags)
6014 'org-highest-priority org-highest-priority
6015 'org-lowest-priority org-lowest-priority
6016 'time-of-day time-of-day
6017 'duration duration
6018 'effort effort
6019 'effort-minutes neffort
6020 'txt txt
6021 'time time
6022 'extra extra
6023 'format org-prefix-format-compiled
6024 'dotime dotime)))))
6026 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6027 "Remove tags string from TXT, and add a modified list of tags.
6028 The modified list may contain inherited tags, and tags matched by
6029 `org-agenda-hide-tags-regexp' will be removed."
6030 (when (or add-inherited hide-re)
6031 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6032 (setq txt (substring txt 0 (match-beginning 0))))
6033 (setq tags
6034 (delq nil
6035 (mapcar (lambda (tg)
6036 (if (or (and hide-re (string-match hide-re tg))
6037 (and (not add-inherited)
6038 (get-text-property 0 'inherited tg)))
6040 tg))
6041 tags)))
6042 (when tags
6043 (let ((have-i (get-text-property 0 'inherited (car tags)))
6045 (setq txt (concat txt " :"
6046 (mapconcat
6047 (lambda (x)
6048 (setq i (get-text-property 0 'inherited x))
6049 (if (and have-i (not i))
6050 (progn
6051 (setq have-i nil)
6052 (concat ":" x))
6054 tags ":")
6055 (if have-i "::" ":"))))))
6056 txt)
6058 (defun org-downcase-keep-props (s)
6059 (let ((props (text-properties-at 0 s)))
6060 (setq s (downcase s))
6061 (add-text-properties 0 (length s) props s)
6064 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6065 (defvar org-agenda-sorting-strategy-selected nil)
6067 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6068 (catch 'exit
6069 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6070 ((and todayp (member 'today (car org-agenda-time-grid))))
6071 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6072 ((member 'weekly (car org-agenda-time-grid)))
6073 (t (throw 'exit list)))
6074 (let* ((have (delq nil (mapcar
6075 (lambda (x) (get-text-property 1 'time-of-day x))
6076 list)))
6077 (string (nth 1 org-agenda-time-grid))
6078 (gridtimes (nth 2 org-agenda-time-grid))
6079 (req (car org-agenda-time-grid))
6080 (remove (member 'remove-match req))
6081 new time)
6082 (if (and (member 'require-timed req) (not have))
6083 ;; don't show empty grid
6084 (throw 'exit list))
6085 (while (setq time (pop gridtimes))
6086 (unless (and remove (member time have))
6087 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6088 (push (org-agenda-format-item
6089 nil string "" nil
6090 (concat (substring time 0 -2) ":" (substring time -2)))
6091 new)
6092 (put-text-property
6093 2 (length (car new)) 'face 'org-time-grid (car new))))
6094 (when (and todayp org-agenda-show-current-time-in-grid)
6095 (push (org-agenda-format-item
6097 org-agenda-current-time-string
6098 "" nil
6099 (format-time-string "%H:%M "))
6100 new)
6101 (put-text-property
6102 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6104 (if (member 'time-up org-agenda-sorting-strategy-selected)
6105 (append new list)
6106 (append list new)))))
6108 (defun org-compile-prefix-format (key)
6109 "Compile the prefix format into a Lisp form that can be evaluated.
6110 The resulting form and associated variable bindings is returned
6111 and stored in the variable `org-prefix-format-compiled'."
6112 (setq org-prefix-has-time nil org-prefix-has-tag nil
6113 org-prefix-category-length nil
6114 org-prefix-has-effort nil)
6115 (let ((s (cond
6116 ((stringp org-agenda-prefix-format)
6117 org-agenda-prefix-format)
6118 ((assq key org-agenda-prefix-format)
6119 (cdr (assq key org-agenda-prefix-format)))
6120 (t " %-12:c%?-12t% s")))
6121 (start 0)
6122 varform vars var e c f opt)
6123 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
6124 s start)
6125 (setq var (or (cdr (assoc (match-string 4 s)
6126 '(("c" . category) ("t" . time) ("s" . extra)
6127 ("i" . category-icon) ("T" . tag) ("e" . effort))))
6128 'eval)
6129 c (or (match-string 3 s) "")
6130 opt (match-beginning 1)
6131 start (1+ (match-beginning 0)))
6132 (if (equal var 'time) (setq org-prefix-has-time t))
6133 (if (equal var 'tag) (setq org-prefix-has-tag t))
6134 (if (equal var 'effort) (setq org-prefix-has-effort t))
6135 (setq f (concat "%" (match-string 2 s) "s"))
6136 (when (equal var 'category)
6137 (setq org-prefix-category-length
6138 (floor (abs (string-to-number (match-string 2 s)))))
6139 (setq org-prefix-category-max-length
6140 (let ((x (match-string 2 s)))
6141 (save-match-data
6142 (if (string-match "\\.[0-9]+" x)
6143 (string-to-number (substring (match-string 0 x) 1)))))))
6144 (if (eq var 'eval)
6145 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6146 (if opt
6147 (setq varform
6148 `(if (equal "" ,var)
6150 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6151 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6152 (setq s (replace-match "%s" t nil s))
6153 (push varform vars))
6154 (setq vars (nreverse vars))
6155 (with-current-buffer org-agenda-buffer
6156 (setq org-prefix-format-compiled
6157 (list
6158 `((org-prefix-has-time ,org-prefix-has-time)
6159 (org-prefix-has-tag ,org-prefix-has-tag)
6160 (org-prefix-category-length ,org-prefix-category-length)
6161 (org-prefix-has-effort ,org-prefix-has-effort))
6162 `(format ,s ,@vars))))))
6164 (defun org-set-sorting-strategy (key)
6165 (if (symbolp (car org-agenda-sorting-strategy))
6166 ;; the old format
6167 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6168 (setq org-agenda-sorting-strategy-selected
6169 (or (cdr (assq key org-agenda-sorting-strategy))
6170 (cdr (assq 'agenda org-agenda-sorting-strategy))
6171 '(time-up category-keep priority-down)))))
6173 (defun org-get-time-of-day (s &optional string mod24)
6174 "Check string S for a time of day.
6175 If found, return it as a military time number between 0 and 2400.
6176 If not found, return nil.
6177 The optional STRING argument forces conversion into a 5 character wide string
6178 HH:MM."
6179 (save-match-data
6180 (when
6181 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6182 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6183 (let* ((h (string-to-number (match-string 1 s)))
6184 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6185 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6186 (am-p (equal ampm "am"))
6187 (h1 (cond ((not ampm) h)
6188 ((= h 12) (if am-p 0 12))
6189 (t (+ h (if am-p 0 12)))))
6190 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6191 (mod h1 24) h1))
6192 (t0 (+ (* 100 h2) m))
6193 (t1 (concat (if (>= h1 24) "+" " ")
6194 (if (and org-agenda-time-leading-zero
6195 (< t0 1000)) "0" "")
6196 (if (< t0 100) "0" "")
6197 (if (< t0 10) "0" "")
6198 (int-to-string t0))))
6199 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6201 (defvar org-agenda-before-sorting-filter-function nil
6202 "Function to be applied to agenda items prior to sorting.
6203 Prior to sorting also means just before they are inserted into the agenda.
6205 To aid sorting, you may revisit the original entries and add more text
6206 properties which will later be used by the sorting functions.
6208 The function should take a string argument, an agenda line.
6209 It has access to the text properties in that line, which contain among
6210 other things, the property `org-hd-marker' that points to the entry
6211 where the line comes from. Note that not all lines going into the agenda
6212 have this property, only most.
6214 The function should return the modified string. It is probably best
6215 to ONLY change text properties.
6217 You can also use this function as a filter, by returning nil for lines
6218 you don't want to have in the agenda at all. For this application, you
6219 could bind the variable in the options section of a custom command.")
6221 (defun org-finalize-agenda-entries (list &optional nosort)
6222 "Sort and concatenate the agenda items."
6223 (setq list (mapcar 'org-agenda-highlight-todo list))
6224 (if nosort
6225 list
6226 (when org-agenda-before-sorting-filter-function
6227 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
6228 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
6230 (defun org-agenda-highlight-todo (x)
6231 (let ((org-done-keywords org-done-keywords-for-agenda)
6232 (case-fold-search nil)
6234 (if (eq x 'line)
6235 (save-excursion
6236 (beginning-of-line 1)
6237 (setq re (org-get-at-bol 'org-todo-regexp))
6238 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6239 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6240 (add-text-properties (match-beginning 0) (match-end 1)
6241 (list 'face (org-get-todo-face 1)))
6242 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6243 (delete-region (match-beginning 1) (1- (match-end 0)))
6244 (goto-char (match-beginning 1))
6245 (insert (format org-agenda-todo-keyword-format s)))))
6246 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6247 (setq re (get-text-property 0 'org-todo-regexp x))
6248 (when (and re
6249 ;; Test `pl' because if there's no heading content,
6250 ;; there's no point matching to highlight. Note
6251 ;; that if we didn't test `pl' first, and there
6252 ;; happened to be no keyword from `org-todo-regexp'
6253 ;; on this heading line, then the `equal' comparison
6254 ;; afterwards would spuriously succeed in the case
6255 ;; where `pl' is nil -- causing an args-out-of-range
6256 ;; error when we try to add text properties to text
6257 ;; that isn't there.
6259 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6260 x pl) pl))
6261 (add-text-properties
6262 (or (match-end 1) (match-end 0)) (match-end 0)
6263 (list 'face (org-get-todo-face (match-string 2 x)))
6265 (when (match-end 1)
6266 (setq x (concat (substring x 0 (match-end 1))
6267 (format org-agenda-todo-keyword-format
6268 (match-string 2 x))
6269 (org-add-props " " (text-properties-at 0 x))
6270 (substring x (match-end 3)))))))
6271 x)))
6273 (defsubst org-cmp-priority (a b)
6274 "Compare the priorities of string A and B."
6275 (let ((pa (or (get-text-property 1 'priority a) 0))
6276 (pb (or (get-text-property 1 'priority b) 0)))
6277 (cond ((> pa pb) +1)
6278 ((< pa pb) -1))))
6280 (defsubst org-cmp-effort (a b)
6281 "Compare the effort values of string A and B."
6282 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6283 (ea (or (get-text-property 1 'effort-minutes a) def))
6284 (eb (or (get-text-property 1 'effort-minutes b) def)))
6285 (cond ((> ea eb) +1)
6286 ((< ea eb) -1))))
6288 (defsubst org-cmp-category (a b)
6289 "Compare the string values of categories of strings A and B."
6290 (let ((ca (or (get-text-property 1 'org-category a) ""))
6291 (cb (or (get-text-property 1 'org-category b) "")))
6292 (cond ((string-lessp ca cb) -1)
6293 ((string-lessp cb ca) +1))))
6295 (defsubst org-cmp-todo-state (a b)
6296 "Compare the todo states of strings A and B."
6297 (let* ((ma (or (get-text-property 1 'org-marker a)
6298 (get-text-property 1 'org-hd-marker a)))
6299 (mb (or (get-text-property 1 'org-marker b)
6300 (get-text-property 1 'org-hd-marker b)))
6301 (fa (and ma (marker-buffer ma)))
6302 (fb (and mb (marker-buffer mb)))
6303 (todo-kwds
6304 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6305 (and fb (with-current-buffer fb org-todo-keywords-1))))
6306 (ta (or (get-text-property 1 'todo-state a) ""))
6307 (tb (or (get-text-property 1 'todo-state b) ""))
6308 (la (- (length (member ta todo-kwds))))
6309 (lb (- (length (member tb todo-kwds))))
6310 (donepa (member ta org-done-keywords-for-agenda))
6311 (donepb (member tb org-done-keywords-for-agenda)))
6312 (cond ((and donepa (not donepb)) -1)
6313 ((and (not donepa) donepb) +1)
6314 ((< la lb) -1)
6315 ((< lb la) +1))))
6317 (defsubst org-cmp-alpha (a b)
6318 "Compare the headlines, alphabetically."
6319 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6320 (plb (text-property-any 0 (length b) 'org-heading t b))
6321 (ta (and pla (substring a pla)))
6322 (tb (and plb (substring b plb))))
6323 (when pla
6324 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6325 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6326 (setq ta (substring ta (match-end 0))))
6327 (setq ta (downcase ta)))
6328 (when plb
6329 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6330 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6331 (setq tb (substring tb (match-end 0))))
6332 (setq tb (downcase tb)))
6333 (cond ((not ta) +1)
6334 ((not tb) -1)
6335 ((string-lessp ta tb) -1)
6336 ((string-lessp tb ta) +1))))
6338 (defsubst org-cmp-tag (a b)
6339 "Compare the string values of the first tags of A and B."
6340 (let ((ta (car (last (get-text-property 1 'tags a))))
6341 (tb (car (last (get-text-property 1 'tags b)))))
6342 (cond ((not ta) +1)
6343 ((not tb) -1)
6344 ((string-lessp ta tb) -1)
6345 ((string-lessp tb ta) +1))))
6347 (defsubst org-cmp-time (a b)
6348 "Compare the time-of-day values of strings A and B."
6349 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6350 (ta (or (get-text-property 1 'time-of-day a) def))
6351 (tb (or (get-text-property 1 'time-of-day b) def)))
6352 (cond ((< ta tb) -1)
6353 ((< tb ta) +1))))
6355 (defsubst org-cmp-habit-p (a b)
6356 "Compare the todo states of strings A and B."
6357 (let ((ha (get-text-property 1 'org-habit-p a))
6358 (hb (get-text-property 1 'org-habit-p b)))
6359 (cond ((and ha (not hb)) -1)
6360 ((and (not ha) hb) +1))))
6362 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
6364 (defun org-entries-lessp (a b)
6365 "Predicate for sorting agenda entries."
6366 ;; The following variables will be used when the form is evaluated.
6367 ;; So even though the compiler complains, keep them.
6368 (let* ((ss org-agenda-sorting-strategy-selected)
6369 (time-up (and (org-em 'time-up 'time-down ss)
6370 (org-cmp-time a b)))
6371 (time-down (if time-up (- time-up) nil))
6372 (priority-up (and (org-em 'priority-up 'priority-down ss)
6373 (org-cmp-priority a b)))
6374 (priority-down (if priority-up (- priority-up) nil))
6375 (effort-up (and (org-em 'effort-up 'effort-down ss)
6376 (org-cmp-effort a b)))
6377 (effort-down (if effort-up (- effort-up) nil))
6378 (category-up (and (or (org-em 'category-up 'category-down ss)
6379 (memq 'category-keep ss))
6380 (org-cmp-category a b)))
6381 (category-down (if category-up (- category-up) nil))
6382 (category-keep (if category-up +1 nil))
6383 (tag-up (and (org-em 'tag-up 'tag-down ss)
6384 (org-cmp-tag a b)))
6385 (tag-down (if tag-up (- tag-up) nil))
6386 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6387 (org-cmp-todo-state a b)))
6388 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6389 (habit-up (and (org-em 'habit-up 'habit-down ss)
6390 (org-cmp-habit-p a b)))
6391 (habit-down (if habit-up (- habit-up) nil))
6392 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6393 (org-cmp-alpha a b)))
6394 (alpha-down (if alpha-up (- alpha-up) nil))
6395 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6396 user-defined-up user-defined-down)
6397 (if (and need-user-cmp org-agenda-cmp-user-defined
6398 (functionp org-agenda-cmp-user-defined))
6399 (setq user-defined-up
6400 (funcall org-agenda-cmp-user-defined a b)
6401 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6402 (cdr (assoc
6403 (eval (cons 'or org-agenda-sorting-strategy-selected))
6404 '((-1 . t) (1 . nil) (nil . nil))))))
6406 ;;; Agenda restriction lock
6408 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6409 "Overlay to mark the headline to which agenda commands are restricted.")
6410 (overlay-put org-agenda-restriction-lock-overlay
6411 'face 'org-agenda-restriction-lock)
6412 (overlay-put org-agenda-restriction-lock-overlay
6413 'help-echo "Agendas are currently limited to this subtree.")
6414 (org-detach-overlay org-agenda-restriction-lock-overlay)
6416 (defun org-agenda-set-restriction-lock (&optional type)
6417 "Set restriction lock for agenda, to current subtree or file.
6418 Restriction will be the file if TYPE is `file', or if type is the
6419 universal prefix '(4), or if the cursor is before the first headline
6420 in the file. Otherwise, restriction will be to the current subtree."
6421 (interactive "P")
6422 (and (equal type '(4)) (setq type 'file))
6423 (setq type (cond
6424 (type type)
6425 ((org-at-heading-p) 'subtree)
6426 ((condition-case nil (org-back-to-heading t) (error nil))
6427 'subtree)
6428 (t 'file)))
6429 (if (eq type 'subtree)
6430 (progn
6431 (setq org-agenda-restrict t)
6432 (setq org-agenda-overriding-restriction 'subtree)
6433 (put 'org-agenda-files 'org-restrict
6434 (list (buffer-file-name (buffer-base-buffer))))
6435 (org-back-to-heading t)
6436 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6437 (move-marker org-agenda-restrict-begin (point))
6438 (move-marker org-agenda-restrict-end
6439 (save-excursion (org-end-of-subtree t)))
6440 (message "Locking agenda restriction to subtree"))
6441 (put 'org-agenda-files 'org-restrict
6442 (list (buffer-file-name (buffer-base-buffer))))
6443 (setq org-agenda-restrict nil)
6444 (setq org-agenda-overriding-restriction 'file)
6445 (move-marker org-agenda-restrict-begin nil)
6446 (move-marker org-agenda-restrict-end nil)
6447 (message "Locking agenda restriction to file"))
6448 (setq current-prefix-arg nil)
6449 (org-agenda-maybe-redo))
6451 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6452 "Remove the agenda restriction lock."
6453 (interactive "P")
6454 (org-detach-overlay org-agenda-restriction-lock-overlay)
6455 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6456 (setq org-agenda-overriding-restriction nil)
6457 (setq org-agenda-restrict nil)
6458 (put 'org-agenda-files 'org-restrict nil)
6459 (move-marker org-agenda-restrict-begin nil)
6460 (move-marker org-agenda-restrict-end nil)
6461 (setq current-prefix-arg nil)
6462 (message "Agenda restriction lock removed")
6463 (or noupdate (org-agenda-maybe-redo)))
6465 (defun org-agenda-maybe-redo ()
6466 "If there is any window showing the agenda view, update it."
6467 (let ((w (get-buffer-window org-agenda-buffer-name t))
6468 (w0 (selected-window)))
6469 (when w
6470 (select-window w)
6471 (org-agenda-redo)
6472 (select-window w0)
6473 (if org-agenda-overriding-restriction
6474 (message "Agenda view shifted to new %s restriction"
6475 org-agenda-overriding-restriction)
6476 (message "Agenda restriction lock removed")))))
6478 ;;; Agenda commands
6480 (defun org-agenda-check-type (error &rest types)
6481 "Check if agenda buffer is of allowed type.
6482 If ERROR is non-nil, throw an error, otherwise just return nil."
6483 (if (not org-agenda-type)
6484 (error "No Org agenda currently displayed")
6485 (if (memq org-agenda-type types)
6487 (if error
6488 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6489 nil))))
6491 (defun org-agenda-Quit (&optional arg)
6492 "Exit agenda by removing the window or the buffer."
6493 (interactive)
6494 (if org-agenda-columns-active
6495 (org-columns-quit)
6496 (let ((buf (current-buffer)))
6497 (if (eq org-agenda-window-setup 'other-frame)
6498 (progn
6499 (org-agenda-reset-markers)
6500 (kill-buffer buf)
6501 (org-columns-remove-overlays)
6502 (setq org-agenda-archives-mode nil)
6503 (delete-frame))
6504 (and (not (eq org-agenda-window-setup 'current-window))
6505 (not (one-window-p))
6506 (delete-window))
6507 (org-agenda-reset-markers)
6508 (kill-buffer buf)
6509 (org-columns-remove-overlays)
6510 (setq org-agenda-archives-mode nil)))
6511 ;; Maybe restore the pre-agenda window configuration.
6512 (and org-agenda-restore-windows-after-quit
6513 (not (eq org-agenda-window-setup 'other-frame))
6514 org-pre-agenda-window-conf
6515 (set-window-configuration org-pre-agenda-window-conf)
6516 (setq org-pre-agenda-window-conf nil))))
6518 (defun org-agenda-quit ()
6519 "Exit agenda by killing agenda buffer or burying it when
6520 `org-agenda-sticky' is non-NIL"
6521 (interactive)
6522 (if org-agenda-columns-active
6523 (org-columns-quit)
6524 (if org-agenda-sticky
6525 (let ((buf (current-buffer)))
6526 (if (eq org-agenda-window-setup 'other-frame)
6527 (progn
6528 (delete-frame))
6529 (and (not (eq org-agenda-window-setup 'current-window))
6530 (not (one-window-p))
6531 (delete-window)))
6532 (with-current-buffer buf
6533 (bury-buffer)
6534 ;; Maybe restore the pre-agenda window configuration.
6535 (and org-agenda-restore-windows-after-quit
6536 (not (eq org-agenda-window-setup 'other-frame))
6537 org-pre-agenda-window-conf
6538 (set-window-configuration org-pre-agenda-window-conf)
6539 (setq org-pre-agenda-window-conf nil))))
6540 (org-agenda-Quit))))
6542 (defun org-agenda-exit ()
6543 "Exit agenda by removing the window or the buffer.
6544 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6545 Org-mode buffers visited directly by the user will not be touched."
6546 (interactive)
6547 (org-release-buffers org-agenda-new-buffers)
6548 (setq org-agenda-new-buffers nil)
6549 (org-agenda-Quit))
6551 (defun org-agenda-kill-all-agenda-buffers ()
6552 "Kill all buffers in `org-agena-mode'.
6553 This is used when toggling sticky agendas. You can also explicitly invoke it
6554 with `C-c a C-k'."
6555 (interactive)
6556 (let (blist)
6557 (dolist (buf (buffer-list))
6558 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
6559 (push buf blist)))
6560 (mapc 'kill-buffer blist)))
6562 (defun org-agenda-execute (arg)
6563 "Execute another agenda command, keeping same window.
6564 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6565 in the agenda."
6566 (interactive "P")
6567 (let ((org-agenda-window-setup 'current-window))
6568 (org-agenda arg)))
6570 (defun org-agenda-redo ()
6571 "Rebuild Agenda.
6572 When this is the global TODO list, a prefix argument will be interpreted."
6573 (interactive)
6574 (let* ((org-agenda-doing-sticky-redo org-agenda-sticky)
6575 (org-agenda-sticky nil)
6576 (org-agenda-buffer-name (or org-agenda-this-buffer-name
6577 org-agenda-buffer-name))
6578 (org-agenda-keep-modes t)
6579 (tag-filter org-agenda-tag-filter)
6580 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
6581 (top-cat-filter org-agenda-top-category-filter)
6582 (cat-filter org-agenda-category-filter)
6583 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6584 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
6585 (cols org-agenda-columns-active)
6586 (line (org-current-line))
6587 (window-line (- line (org-current-line (window-start))))
6588 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6589 (put 'org-agenda-tag-filter :preset-filter nil)
6590 (put 'org-agenda-category-filter :preset-filter nil)
6591 (and cols (org-columns-quit))
6592 (message "Rebuilding agenda buffer...")
6593 (org-let lprops '(eval org-agenda-redo-command))
6594 (setq org-agenda-undo-list nil
6595 org-agenda-pending-undo-list nil)
6596 (message "Rebuilding agenda buffer...done")
6597 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6598 (put 'org-agenda-category-filter :preset-filter cat-preset)
6599 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6600 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
6601 (and top-cat-filter (org-agenda-filter-top-category-apply top-cat-filter))
6602 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6603 (org-goto-line line)
6604 (recenter window-line)))
6606 (defvar org-global-tags-completion-table nil)
6607 (defvar org-agenda-filter-form nil)
6608 (defvar org-agenda-filtered-by-category nil)
6610 (defun org-agenda-filter-by-category (strip)
6611 "Keep only those lines in the agenda buffer that have a specific category.
6612 The category is that of the current line."
6613 (interactive "P")
6614 (if (and org-agenda-filtered-by-category
6615 org-agenda-category-filter)
6616 (org-agenda-filter-show-all-cat)
6617 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6618 (if cat (org-agenda-filter-apply
6619 (list (concat (if strip "-" "+") cat)) 'category)
6620 (error "No category at point")))))
6622 (defun org-find-top-category (&optional pos)
6623 (save-excursion
6624 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
6625 (if pos (goto-char pos))
6626 ;; Skip up to the topmost parent
6627 (while (ignore-errors (outline-up-heading 1) t))
6628 (ignore-errors
6629 (nth 4 (org-heading-components))))))
6631 (defvar org-agenda-filtered-by-top-category nil)
6633 (defun org-agenda-filter-by-top-category (strip)
6634 "Keep only those lines in the agenda buffer that have a specific category.
6635 The category is that of the current line."
6636 (interactive "P")
6637 (if org-agenda-filtered-by-top-category
6638 (progn
6639 (setq org-agenda-filtered-by-top-category nil
6640 org-agenda-top-category-filter nil)
6641 (org-agenda-filter-show-all-cat))
6642 (let ((cat (org-find-top-category (org-get-at-bol 'org-hd-marker))))
6643 (if cat (org-agenda-filter-top-category-apply cat strip)
6644 (error "No top-level category at point")))))
6646 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6647 "Keep only those lines in the agenda buffer that have a specific tag.
6648 The tag is selected with its fast selection letter, as configured.
6649 With prefix argument STRIP, remove all lines that do have the tag.
6650 A lisp caller can specify CHAR. NARROW means that the new tag should be
6651 used to narrow the search - the interactive user can also press `-' or `+'
6652 to switch to narrowing."
6653 (interactive "P")
6654 (let* ((alist org-tag-alist-for-agenda)
6655 (tag-chars (mapconcat
6656 (lambda (x) (if (and (not (symbolp (car x)))
6657 (cdr x))
6658 (char-to-string (cdr x))
6659 ""))
6660 alist ""))
6661 (efforts (org-split-string
6662 (or (cdr (assoc (concat org-effort-property "_ALL")
6663 org-global-properties))
6664 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6665 "")))
6666 (effort-op org-agenda-filter-effort-default-operator)
6667 (effort-prompt "")
6668 (inhibit-read-only t)
6669 (current org-agenda-tag-filter)
6670 maybe-refresh a n tag)
6671 (unless char
6672 (message
6673 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6674 (if narrow "Narrow" "Filter") tag-chars
6675 (if org-agenda-auto-exclude-function "[RET], " ""))
6676 (setq char (read-char-exclusive)))
6677 (when (member char '(?+ ?-))
6678 ;; Narrowing down
6679 (cond ((equal char ?-) (setq strip t narrow t))
6680 ((equal char ?+) (setq strip nil narrow t)))
6681 (message
6682 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6683 (setq char (read-char-exclusive)))
6684 (when (member char '(?< ?> ?= ??))
6685 ;; An effort operator
6686 (setq effort-op (char-to-string char))
6687 (setq alist nil) ; to make sure it will be interpreted as effort.
6688 (unless (equal char ??)
6689 (loop for i from 0 to 9 do
6690 (setq effort-prompt
6691 (concat
6692 effort-prompt " ["
6693 (if (= i 9) "0" (int-to-string (1+ i)))
6694 "]" (nth i efforts))))
6695 (message "Effort%s: %s " effort-op effort-prompt)
6696 (setq char (read-char-exclusive))
6697 (when (or (< char ?0) (> char ?9))
6698 (error "Need 1-9,0 to select effort"))))
6699 (when (equal char ?\t)
6700 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6701 (org-set-local 'org-global-tags-completion-table
6702 (org-global-tags-completion-table)))
6703 (let ((completion-ignore-case t))
6704 (setq tag (org-icompleting-read
6705 "Tag: " org-global-tags-completion-table))))
6706 (cond
6707 ((equal char ?\r)
6708 (org-agenda-filter-show-all-tag)
6709 (when org-agenda-auto-exclude-function
6710 (setq org-agenda-tag-filter '())
6711 (dolist (tag (org-agenda-get-represented-tags))
6712 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6713 (if modifier
6714 (push modifier org-agenda-tag-filter))))
6715 (if (not (null org-agenda-tag-filter))
6716 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
6717 (setq maybe-refresh t))
6718 ((equal char ?/)
6719 (org-agenda-filter-show-all-tag)
6720 (when (get 'org-agenda-tag-filter :preset-filter)
6721 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
6722 (setq maybe-refresh t))
6723 ((equal char ?. )
6724 (setq org-agenda-tag-filter
6725 (mapcar (lambda(tag) (concat "+" tag))
6726 (org-get-at-bol 'tags)))
6727 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6728 (setq maybe-refresh t))
6729 ((or (equal char ?\ )
6730 (setq a (rassoc char alist))
6731 (and (>= char ?0) (<= char ?9)
6732 (setq n (if (= char ?0) 9 (- char ?0 1))
6733 tag (concat effort-op (nth n efforts))
6734 a (cons tag nil)))
6735 (and (= char ??)
6736 (setq tag "?eff")
6737 a (cons tag nil))
6738 (and tag (setq a (cons tag nil))))
6739 (org-agenda-filter-show-all-tag)
6740 (setq tag (car a))
6741 (setq org-agenda-tag-filter
6742 (cons (concat (if strip "-" "+") tag)
6743 (if narrow current nil)))
6744 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6745 (setq maybe-refresh t))
6746 (t (error "Invalid tag selection character %c" char)))
6747 (when (and maybe-refresh
6748 (eq org-agenda-clockreport-mode 'with-filter))
6749 (org-agenda-redo))))
6751 (defun org-agenda-get-represented-tags ()
6752 "Get a list of all tags currently represented in the agenda."
6753 (let (p tags)
6754 (save-excursion
6755 (goto-char (point-min))
6756 (while (setq p (next-single-property-change (point) 'tags))
6757 (goto-char p)
6758 (mapc (lambda (x) (add-to-list 'tags x))
6759 (get-text-property (point) 'tags))))
6760 tags))
6762 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6763 "Refine the current filter. See `org-agenda-filter-by-tag'."
6764 (interactive "P")
6765 (org-agenda-filter-by-tag strip char 'refine))
6767 (defun org-agenda-filter-make-matcher ()
6768 "Create the form that tests a line for agenda filter."
6769 (let (f f1)
6770 ;; first compute the tag-filter matcher
6771 (dolist (x (delete-dups
6772 (append (get 'org-agenda-tag-filter
6773 :preset-filter) org-agenda-tag-filter)))
6774 (if (member x '("-" "+"))
6775 (setq f1 (if (equal x "-") 'tags '(not tags)))
6776 (if (string-match "[<=>?]" x)
6777 (setq f1 (org-agenda-filter-effort-form x))
6778 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6779 (if (equal (string-to-char x) ?-)
6780 (setq f1 (list 'not f1))))
6781 (push f1 f))
6782 ;; then compute the category-filter matcher
6783 (dolist (x (delete-dups
6784 (append (get 'org-agenda-category-filter
6785 :preset-filter) org-agenda-category-filter)))
6786 (if (equal "-" (substring x 0 1))
6787 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
6788 (setq f1 (list 'equal (substring x 1) 'cat)))
6789 (push f1 f))
6790 (cons 'and (nreverse f))))
6792 (defun org-agenda-filter-effort-form (e)
6793 "Return the form to compare the effort of the current line with what E says.
6794 E looks like \"+<2:25\"."
6795 (let (op)
6796 (setq e (substring e 1))
6797 (setq op (string-to-char e) e (substring e 1))
6798 (setq op (cond ((equal op ?<) '<=)
6799 ((equal op ?>) '>=)
6800 ((equal op ??) op)
6801 (t '=)))
6802 (list 'org-agenda-compare-effort (list 'quote op)
6803 (org-duration-string-to-minutes e))))
6805 (defun org-agenda-compare-effort (op value)
6806 "Compare the effort of the current line with VALUE, using OP.
6807 If the line does not have an effort defined, return nil."
6808 (let ((eff (org-get-at-bol 'effort-minutes)))
6809 (if (equal op ??)
6810 (not eff)
6811 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6812 value))))
6814 (defun org-agenda-filter-apply (filter type)
6815 "Set FILTER as the new agenda filter and apply it."
6816 (let (tags cat)
6817 (if (eq type 'tag)
6818 (setq org-agenda-tag-filter filter)
6819 (setq org-agenda-category-filter filter))
6820 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
6821 (if (and (eq type 'category)
6822 (not (equal (substring (car filter) 0 1) "-")))
6823 ;; Only set `org-agenda-filtered-by-category' to t
6824 ;; when a unique category is used as the filter
6825 (setq org-agenda-filtered-by-category t))
6826 (org-agenda-set-mode-name)
6827 (save-excursion
6828 (goto-char (point-min))
6829 (while (not (eobp))
6830 (if (org-get-at-bol 'org-marker)
6831 (progn
6832 (setq tags (org-get-at-bol 'tags) ; used in eval
6833 cat (get-text-property (point) 'org-category))
6834 (if (not (eval org-agenda-filter-form))
6835 (org-agenda-filter-hide-line type))
6836 (beginning-of-line 2))
6837 (beginning-of-line 2))))
6838 (if (get-char-property (point) 'invisible)
6839 (ignore-errors (org-agenda-previous-line)))))
6841 (defun org-agenda-filter-top-category-apply (category &optional negative)
6842 "Set FILTER as the new agenda filter and apply it."
6843 (org-agenda-set-mode-name)
6844 (save-excursion
6845 (goto-char (point-min))
6846 (while (not (eobp))
6847 (let* ((pos (org-get-at-bol 'org-hd-marker))
6848 (topcat (and pos (org-find-top-category pos))))
6849 (if (and topcat (funcall (if negative 'identity 'not)
6850 (string= category topcat)))
6851 (org-agenda-filter-hide-line 'category)))
6852 (beginning-of-line 2)))
6853 (if (get-char-property (point) 'invisible)
6854 (org-agenda-previous-line))
6855 (setq org-agenda-top-category-filter category
6856 org-agenda-filtered-by-top-category t))
6858 (defun org-agenda-filter-hide-line (type)
6859 (let (ov)
6860 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6861 (point-at-eol)))
6862 (overlay-put ov 'invisible t)
6863 (overlay-put ov 'type type)
6864 (if (eq type 'tag)
6865 (push ov org-agenda-tag-filter-overlays)
6866 (push ov org-agenda-cat-filter-overlays))))
6868 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6869 (setq pos (or pos (point)))
6870 (save-excursion
6871 (dolist (ov (overlays-at pos))
6872 (when (and (overlay-get ov 'invisible)
6873 (eq (overlay-get ov 'type) 'tag))
6874 (goto-char pos)
6875 (if (< (overlay-start ov) (point-at-eol))
6876 (move-overlay ov (point-at-eol)
6877 (overlay-end ov)))))))
6879 (defun org-agenda-filter-show-all-tag nil
6880 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
6881 (setq org-agenda-tag-filter-overlays nil
6882 org-agenda-tag-filter nil
6883 org-agenda-filter-form nil)
6884 (org-agenda-set-mode-name))
6886 (defun org-agenda-filter-show-all-cat nil
6887 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
6888 (setq org-agenda-cat-filter-overlays nil
6889 org-agenda-filtered-by-category nil
6890 org-agenda-category-filter nil
6891 org-agenda-filter-form nil)
6892 (org-agenda-set-mode-name))
6894 (defun org-agenda-manipulate-query-add ()
6895 "Manipulate the query by adding a search term with positive selection.
6896 Positive selection means the term must be matched for selection of an entry."
6897 (interactive)
6898 (org-agenda-manipulate-query ?\[))
6899 (defun org-agenda-manipulate-query-subtract ()
6900 "Manipulate the query by adding a search term with negative selection.
6901 Negative selection means term must not be matched for selection of an entry."
6902 (interactive)
6903 (org-agenda-manipulate-query ?\]))
6904 (defun org-agenda-manipulate-query-add-re ()
6905 "Manipulate the query by adding a search regexp with positive selection.
6906 Positive selection means the regexp must match for selection of an entry."
6907 (interactive)
6908 (org-agenda-manipulate-query ?\{))
6909 (defun org-agenda-manipulate-query-subtract-re ()
6910 "Manipulate the query by adding a search regexp with negative selection.
6911 Negative selection means regexp must not match for selection of an entry."
6912 (interactive)
6913 (org-agenda-manipulate-query ?\}))
6914 (defun org-agenda-manipulate-query (char)
6915 (cond
6916 ((memq org-agenda-type '(timeline agenda))
6917 (let ((org-agenda-include-inactive-timestamps t))
6918 (org-agenda-redo))
6919 (message "Display now includes inactive timestamps as well"))
6920 ((eq org-agenda-type 'search)
6921 (org-add-to-string
6922 'org-agenda-query-string
6923 (if org-agenda-last-search-view-search-was-boolean
6924 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6925 (?\{ . " +{}") (?\} . " -{}"))))
6926 " "))
6927 (setq org-agenda-redo-command
6928 (list 'org-search-view
6929 org-todo-only
6930 org-agenda-query-string
6931 (+ (length org-agenda-query-string)
6932 (if (member char '(?\{ ?\})) 0 1))))
6933 (set-register org-agenda-query-register org-agenda-query-string)
6934 (org-agenda-redo))
6935 (t (error "Cannot manipulate query for %s-type agenda buffers"
6936 org-agenda-type))))
6938 (defun org-add-to-string (var string)
6939 (set var (concat (symbol-value var) string)))
6941 (defun org-agenda-goto-date (date)
6942 "Jump to DATE in agenda."
6943 (interactive (list (let ((org-read-date-prefer-future
6944 (eval org-agenda-jump-prefer-future)))
6945 (org-read-date))))
6946 (org-agenda-list nil date))
6948 (defun org-agenda-goto-today ()
6949 "Go to today."
6950 (interactive)
6951 (org-agenda-check-type t 'timeline 'agenda)
6952 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6953 (cond
6954 (tdpos (goto-char tdpos))
6955 ((eq org-agenda-type 'agenda)
6956 (let* ((sd (org-agenda-compute-starting-span
6957 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6958 (org-agenda-overriding-arguments org-agenda-last-arguments))
6959 (setf (nth 1 org-agenda-overriding-arguments) sd)
6960 (org-agenda-redo)
6961 (org-agenda-find-same-or-today-or-agenda)))
6962 (t (error "Cannot find today")))))
6964 (defvar org-agenda-multi-back-to-pos nil)
6965 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6966 (goto-char
6967 (or (and org-agenda-multi-back-to-pos (move-beginning-of-line 1))
6968 (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6969 (text-property-any (point-min) (point-max) 'org-today t)
6970 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6971 (point-min))))
6973 (defun org-agenda-get-text-property (prop)
6974 "Find text property PROP.
6975 The search starts by looking backward, to find the previous text
6976 property PROP, then continues forward if none has been found."
6977 (save-excursion
6978 (unless (looking-at "\\'")
6979 (forward-char))
6980 (let ((p (previous-single-property-change (point) prop))
6981 (n (next-single-property-change (or (and (looking-at "\\`") 1)
6982 (1- (point))) prop)))
6983 (cond ((eq n (point-at-eol))
6984 (cons (get-text-property (1- n) prop) (1- n)))
6985 (p (cons (get-text-property (1- p) prop) (1- p)))))))
6987 (defun org-agenda-later (arg)
6988 "Go forward in time by thee current span.
6989 With prefix ARG, go forward that many times the current span."
6990 (interactive "p")
6991 (org-agenda-check-type t 'agenda)
6992 (let* ((span (or (car (org-agenda-get-text-property
6993 'org-agenda-current-span))
6994 org-agenda-current-span))
6995 (sd (or (cadr (car (org-agenda-get-text-property
6996 'org-agenda-overriding-arguments)))
6997 org-starting-day))
6998 (greg (calendar-gregorian-from-absolute sd))
6999 (cnt (org-get-at-bol 'org-day-cnt))
7000 greg2)
7001 (cond
7002 ((numberp span)
7003 (setq sd (+ span sd)))
7004 ((eq span 'day)
7005 (setq sd (+ arg sd)))
7006 ((eq span 'week)
7007 (setq sd (+ (* 7 arg) sd)))
7008 ((eq span 'month)
7009 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7010 sd (calendar-absolute-from-gregorian greg2))
7011 (setcar greg2 (1+ (car greg2))))
7012 ((eq span 'year)
7013 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7014 sd (calendar-absolute-from-gregorian greg2))
7015 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7017 (setq sd (+ (* span arg) sd))))
7018 (let ((org-agenda-overriding-cmd
7019 ;; `cmd' may have been set by `org-agenda-run-series' which
7020 ;; uses `org-agenda-overriding-cmd' to decide whether
7021 ;; overriding is allowed for `cmd'
7022 (car (org-agenda-get-text-property 'org-agenda-cmd)))
7023 (org-agenda-overriding-arguments
7024 (list (car org-agenda-last-arguments) sd span)))
7025 (setq org-agenda-multi-back-to-pos
7026 (cdr (org-agenda-get-text-property 'org-agenda-cmd))
7027 org-agenda-multi-overriding-arguments
7028 org-agenda-overriding-arguments)
7029 (org-agenda-redo)
7030 (org-agenda-find-same-or-today-or-agenda cnt))))
7032 (defun org-agenda-earlier (arg)
7033 "Go backward in time by the current span.
7034 With prefix ARG, go backward that many times the current span."
7035 (interactive "p")
7036 (org-agenda-later (- arg)))
7038 (defun org-agenda-view-mode-dispatch ()
7039 "Call one of the view mode commands."
7040 (interactive)
7041 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7042 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7043 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7044 (let ((a (read-char-exclusive)))
7045 (case a
7046 (?\ (call-interactively 'org-agenda-reset-view))
7047 (?d (call-interactively 'org-agenda-day-view))
7048 (?w (call-interactively 'org-agenda-week-view))
7049 (?m (call-interactively 'org-agenda-month-view))
7050 (?y (call-interactively 'org-agenda-year-view))
7051 (?l (call-interactively 'org-agenda-log-mode))
7052 (?L (org-agenda-log-mode '(4)))
7053 (?c (org-agenda-log-mode 'clockcheck))
7054 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7055 (?a (call-interactively 'org-agenda-archives-mode))
7056 (?A (org-agenda-archives-mode 'files))
7057 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7058 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7059 (?G (call-interactively 'org-agenda-toggle-time-grid))
7060 (?D (call-interactively 'org-agenda-toggle-diary))
7061 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7062 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7063 (org-agenda-check-type t 'timeline 'agenda)
7064 (org-agenda-redo))
7065 (message "Display now includes inactive timestamps as well"))
7066 (?q (message "Abort"))
7067 (otherwise (error "Invalid key" )))))
7069 (defun org-agenda-reset-view ()
7070 "Switch to default view for agenda."
7071 (interactive)
7072 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7073 (defun org-agenda-day-view (&optional day-of-year)
7074 "Switch to daily view for agenda.
7075 With argument DAY-OF-YEAR, switch to that day of the year."
7076 (interactive "P")
7077 (org-agenda-change-time-span 'day day-of-year))
7078 (defun org-agenda-week-view (&optional iso-week)
7079 "Switch to daily view for agenda.
7080 With argument ISO-WEEK, switch to the corresponding ISO week.
7081 If ISO-WEEK has more then 2 digits, only the last two encode the
7082 week. Any digits before this encode a year. So 200712 means
7083 week 12 of year 2007. Years in the range 1938-2037 can also be
7084 written as 2-digit years."
7085 (interactive "P")
7086 (org-agenda-change-time-span 'week iso-week))
7087 (defun org-agenda-month-view (&optional month)
7088 "Switch to monthly view for agenda.
7089 With argument MONTH, switch to that month."
7090 (interactive "P")
7091 (org-agenda-change-time-span 'month month))
7092 (defun org-agenda-year-view (&optional year)
7093 "Switch to yearly view for agenda.
7094 With argument YEAR, switch to that year.
7095 If MONTH has more then 2 digits, only the last two encode the
7096 month. Any digits before this encode a year. So 200712 means
7097 December year 2007. Years in the range 1938-2037 can also be
7098 written as 2-digit years."
7099 (interactive "P")
7100 (when year
7101 (setq year (org-small-year-to-year year)))
7102 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7103 (org-agenda-change-time-span 'year year)
7104 (error "Abort")))
7106 (defun org-agenda-change-time-span (span &optional n)
7107 "Change the agenda view to SPAN.
7108 SPAN may be `day', `week', `month', `year'."
7109 (org-agenda-check-type t 'agenda)
7110 (let ((org-agenda-cur-span
7111 (or (car (org-agenda-get-text-property
7112 'org-agenda-current-span))
7113 org-agenda-current-span))
7114 (org-agenda-overriding-arguments
7115 (or (car (org-agenda-get-text-property
7116 'org-agenda-overriding-arguments))
7117 org-agenda-overriding-arguments)))
7118 (setq org-agenda-multi-back-to-pos
7119 (cdr (org-agenda-get-text-property 'org-agenda-cmd)))
7120 (if (and (not n) (equal org-agenda-cur-span span))
7121 (error "Viewing span is already \"%s\"" span))
7122 (let* ((sd (or (org-get-at-bol 'day)
7123 org-starting-day))
7124 (sd (org-agenda-compute-starting-span sd span n))
7125 (org-agenda-overriding-cmd
7126 (car (org-agenda-get-text-property 'org-agenda-cmd)))
7127 (org-agenda-overriding-arguments
7128 (list (car org-agenda-last-arguments) sd span)))
7129 (org-agenda-redo)
7130 (org-agenda-find-same-or-today-or-agenda))
7131 (org-agenda-set-mode-name)
7132 (message "Switched to %s view" span)))
7134 (defun org-agenda-compute-starting-span (sd span &optional n)
7135 "Compute starting date for agenda.
7136 SPAN may be `day', `week', `month', `year'. The return value
7137 is a cons cell with the starting date and the number of days,
7138 so that the date SD will be in that range."
7139 (let* ((greg (calendar-gregorian-from-absolute sd))
7140 (dg (nth 1 greg))
7141 (mg (car greg))
7142 (yg (nth 2 greg)))
7143 (cond
7144 ((eq span 'day)
7145 (when n
7146 (setq sd (+ (calendar-absolute-from-gregorian
7147 (list mg 1 yg))
7148 n -1))))
7149 ((eq span 'week)
7150 (let* ((nt (calendar-day-of-week
7151 (calendar-gregorian-from-absolute sd)))
7152 (d (if org-agenda-start-on-weekday
7153 (- nt org-agenda-start-on-weekday)
7156 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7157 (when n
7158 (require 'cal-iso)
7159 (when (> n 99)
7160 (setq y1 (org-small-year-to-year (/ n 100))
7161 n (mod n 100)))
7162 (setq sd
7163 (calendar-absolute-from-iso
7164 (list n 1
7165 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7166 ((eq span 'month)
7167 (let (y1)
7168 (when (and n (> n 99))
7169 (setq y1 (org-small-year-to-year (/ n 100))
7170 n (mod n 100)))
7171 (setq sd (calendar-absolute-from-gregorian
7172 (list (or n mg) 1 (or y1 yg))))))
7173 ((eq span 'year)
7174 (setq sd (calendar-absolute-from-gregorian
7175 (list 1 1 (or n yg))))))
7176 sd))
7178 (defun org-agenda-next-date-line (&optional arg)
7179 "Jump to the next line indicating a date in agenda buffer."
7180 (interactive "p")
7181 (org-agenda-check-type t 'agenda 'timeline)
7182 (beginning-of-line 1)
7183 ;; This does not work if user makes date format that starts with a blank
7184 (if (looking-at "^\\S-") (forward-char 1))
7185 (if (not (re-search-forward "^\\S-" nil t arg))
7186 (progn
7187 (backward-char 1)
7188 (error "No next date after this line in this buffer")))
7189 (goto-char (match-beginning 0)))
7191 (defun org-agenda-previous-date-line (&optional arg)
7192 "Jump to the previous line indicating a date in agenda buffer."
7193 (interactive "p")
7194 (org-agenda-check-type t 'agenda 'timeline)
7195 (beginning-of-line 1)
7196 (if (not (re-search-backward "^\\S-" nil t arg))
7197 (error "No previous date before this line in this buffer")))
7199 ;; Initialize the highlight
7200 (defvar org-hl (make-overlay 1 1))
7201 (overlay-put org-hl 'face 'highlight)
7203 (defun org-highlight (begin end &optional buffer)
7204 "Highlight a region with overlay."
7205 (move-overlay org-hl begin end (or buffer (current-buffer))))
7207 (defun org-unhighlight ()
7208 "Detach overlay INDEX."
7209 (org-detach-overlay org-hl))
7211 ;; FIXME this is currently not used.
7212 (defun org-highlight-until-next-command (beg end &optional buffer)
7213 "Move the highlight overlay to BEG/END, remove it before the next command."
7214 (org-highlight beg end buffer)
7215 (add-hook 'pre-command-hook 'org-unhighlight-once))
7216 (defun org-unhighlight-once ()
7217 "Remove the highlight from its position, and this function from the hook."
7218 (remove-hook 'pre-command-hook 'org-unhighlight-once)
7219 (org-unhighlight))
7221 (defun org-agenda-follow-mode ()
7222 "Toggle follow mode in an agenda buffer."
7223 (interactive)
7224 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
7225 (org-agenda-set-mode-name)
7226 (org-agenda-do-context-action)
7227 (message "Follow mode is %s"
7228 (if org-agenda-follow-mode "on" "off")))
7230 (defun org-agenda-entry-text-mode (&optional arg)
7231 "Toggle entry text mode in an agenda buffer."
7232 (interactive "P")
7233 (setq org-agenda-entry-text-mode (or (integerp arg)
7234 (not org-agenda-entry-text-mode)))
7235 (org-agenda-entry-text-hide)
7236 (and org-agenda-entry-text-mode
7237 (let ((org-agenda-entry-text-maxlines
7238 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7239 (org-agenda-entry-text-show)))
7240 (org-agenda-set-mode-name)
7241 (message "Entry text mode is %s. Maximum number of lines is %d"
7242 (if org-agenda-entry-text-mode "on" "off")
7243 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7245 (defun org-agenda-clockreport-mode (&optional with-filter)
7246 "Toggle clocktable mode in an agenda buffer.
7247 With prefix arg WITH-FILTER, make the clocktable respect the current
7248 agenda filter."
7249 (interactive "P")
7250 (org-agenda-check-type t 'agenda)
7251 (if with-filter
7252 (setq org-agenda-clockreport-mode 'with-filter)
7253 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
7254 (org-agenda-set-mode-name)
7255 (org-agenda-redo)
7256 (message "Clocktable mode is %s"
7257 (if org-agenda-clockreport-mode "on" "off")))
7259 (defun org-agenda-log-mode (&optional special)
7260 "Toggle log mode in an agenda buffer.
7261 With argument SPECIAL, show all possible log items, not only the ones
7262 configured in `org-agenda-log-mode-items'.
7263 With a double `C-u' prefix arg, show *only* log items, nothing else."
7264 (interactive "P")
7265 (org-agenda-check-type t 'agenda 'timeline)
7266 (setq org-agenda-show-log
7267 (cond
7268 ((equal special '(16)) 'only)
7269 ((eq special 'clockcheck)
7270 (if (eq org-agenda-show-log 'clockcheck)
7271 nil 'clockcheck))
7272 (special '(closed clock state))
7273 (t (not org-agenda-show-log))))
7274 (org-agenda-set-mode-name)
7275 (org-agenda-redo)
7276 (message "Log mode is %s"
7277 (if org-agenda-show-log "on" "off")))
7279 (defun org-agenda-archives-mode (&optional with-files)
7280 "Toggle inclusion of items in trees marked with :ARCHIVE:.
7281 When called with a prefix argument, include all archive files as well."
7282 (interactive "P")
7283 (setq org-agenda-archives-mode
7284 (if with-files t (if org-agenda-archives-mode nil 'trees)))
7285 (org-agenda-set-mode-name)
7286 (org-agenda-redo)
7287 (message
7288 "%s"
7289 (cond
7290 ((eq org-agenda-archives-mode nil)
7291 "No archives are included")
7292 ((eq org-agenda-archives-mode 'trees)
7293 (format "Trees with :%s: tag are included" org-archive-tag))
7294 ((eq org-agenda-archives-mode t)
7295 (format "Trees with :%s: tag and all active archive files are included"
7296 org-archive-tag)))))
7298 (defun org-agenda-toggle-diary ()
7299 "Toggle diary inclusion in an agenda buffer."
7300 (interactive)
7301 (org-agenda-check-type t 'agenda)
7302 (setq org-agenda-include-diary (not org-agenda-include-diary))
7303 (org-agenda-redo)
7304 (org-agenda-set-mode-name)
7305 (message "Diary inclusion turned %s"
7306 (if org-agenda-include-diary "on" "off")))
7308 (defun org-agenda-toggle-deadlines ()
7309 "Toggle inclusion of entries with a deadline in an agenda buffer."
7310 (interactive)
7311 (org-agenda-check-type t 'agenda)
7312 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
7313 (org-agenda-redo)
7314 (org-agenda-set-mode-name)
7315 (message "Deadlines inclusion turned %s"
7316 (if org-agenda-include-deadlines "on" "off")))
7318 (defun org-agenda-toggle-time-grid ()
7319 "Toggle time grid in an agenda buffer."
7320 (interactive)
7321 (org-agenda-check-type t 'agenda)
7322 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
7323 (org-agenda-redo)
7324 (org-agenda-set-mode-name)
7325 (message "Time-grid turned %s"
7326 (if org-agenda-use-time-grid "on" "off")))
7328 (defun org-agenda-set-mode-name ()
7329 "Set the mode name to indicate all the small mode settings."
7330 (setq mode-name
7331 (list "Org-Agenda"
7332 (if (get 'org-agenda-files 'org-restrict) " []" "")
7334 '(:eval (org-agenda-span-name org-agenda-current-span))
7335 (if org-agenda-follow-mode " Follow" "")
7336 (if org-agenda-entry-text-mode " ETxt" "")
7337 (if org-agenda-include-diary " Diary" "")
7338 (if org-agenda-include-deadlines " Ddl" "")
7339 (if org-agenda-use-time-grid " Grid" "")
7340 (if (and (boundp 'org-habit-show-habits)
7341 org-habit-show-habits) " Habit" "")
7342 (cond
7343 ((consp org-agenda-show-log) " LogAll")
7344 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
7345 (org-agenda-show-log " Log")
7346 (t ""))
7347 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
7348 :preset-filter))
7349 '(:eval (org-propertize
7350 (concat " <"
7351 (mapconcat
7352 'identity
7353 (append
7354 (get 'org-agenda-category-filter :preset-filter)
7355 org-agenda-category-filter)
7357 ">")
7358 'face 'org-agenda-filter-category
7359 'help-echo "Category used in filtering"))
7361 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
7362 :preset-filter))
7363 '(:eval (org-propertize
7364 (concat " {"
7365 (mapconcat
7366 'identity
7367 (append
7368 (get 'org-agenda-tag-filter :preset-filter)
7369 org-agenda-tag-filter)
7371 "}")
7372 'face 'org-agenda-filter-tags
7373 'help-echo "Tags used in filtering"))
7375 (if org-agenda-archives-mode
7376 (if (eq org-agenda-archives-mode t)
7377 " Archives"
7378 (format " :%s:" org-archive-tag))
7380 (if org-agenda-clockreport-mode
7381 (if (eq org-agenda-clockreport-mode 'with-filter)
7382 " Clock{}" " Clock")
7383 "")))
7384 (force-mode-line-update))
7386 (defun org-agenda-post-command-hook ()
7387 (setq org-agenda-type
7388 (or (get-text-property (point) 'org-agenda-type)
7389 (get-text-property (max (point-min) (1- (point)))
7390 'org-agenda-type))))
7392 (defun org-agenda-next-line ()
7393 "Move cursor to the next line, and show if follow mode is active."
7394 (interactive)
7395 (call-interactively 'next-line)
7396 (org-agenda-do-context-action))
7398 (defun org-agenda-previous-line ()
7399 "Move cursor to the previous line, and show if follow-mode is active."
7400 (interactive)
7401 (call-interactively 'previous-line)
7402 (org-agenda-do-context-action))
7404 (defun org-agenda-next-item (n)
7405 "Move cursor to next agenda item."
7406 (interactive "p")
7407 (let ((col (current-column)))
7408 (dotimes (c n)
7409 (when (next-single-property-change (point-at-eol) 'org-marker)
7410 (move-end-of-line 1)
7411 (goto-char (next-single-property-change (point) 'org-marker))))
7412 (org-move-to-column col))
7413 (org-agenda-do-context-action))
7415 (defun org-agenda-previous-item (n)
7416 "Move cursor to next agenda item."
7417 (interactive "p")
7418 (dotimes (c n)
7419 (let ((col (current-column))
7420 (goto (save-excursion
7421 (move-end-of-line 0)
7422 (previous-single-property-change (point) 'org-marker))))
7423 (if goto (goto-char goto))
7424 (org-move-to-column col)))
7425 (org-agenda-do-context-action))
7427 (defun org-agenda-do-context-action ()
7428 "Show outline path and, maybe, follow mode window."
7429 (let ((m (org-get-at-bol 'org-marker)))
7430 (when (and (markerp m) (marker-buffer m))
7431 (and org-agenda-follow-mode
7432 (if org-agenda-follow-indirect
7433 (org-agenda-tree-to-indirect-buffer)
7434 (org-agenda-show)))
7435 (and org-agenda-show-outline-path
7436 (org-with-point-at m (org-display-outline-path t))))))
7438 (defun org-agenda-show-priority ()
7439 "Show the priority of the current item.
7440 This priority is composed of the main priority given with the [#A] cookies,
7441 and by additional input from the age of a schedules or deadline entry."
7442 (interactive)
7443 (let* ((pri (org-get-at-bol 'priority)))
7444 (message "Priority is %d" (if pri pri -1000))))
7446 (defun org-agenda-show-tags ()
7447 "Show the tags applicable to the current item."
7448 (interactive)
7449 (let* ((tags (org-get-at-bol 'tags)))
7450 (if tags
7451 (message "Tags are :%s:"
7452 (org-no-properties (mapconcat 'identity tags ":")))
7453 (message "No tags associated with this line"))))
7455 (defun org-agenda-goto (&optional highlight)
7456 "Go to the Org-mode file which contains the item at point."
7457 (interactive)
7458 (let* ((marker (or (org-get-at-bol 'org-marker)
7459 (org-agenda-error)))
7460 (buffer (marker-buffer marker))
7461 (pos (marker-position marker)))
7462 (switch-to-buffer-other-window buffer)
7463 (widen)
7464 (push-mark)
7465 (goto-char pos)
7466 (when (derived-mode-p 'org-mode)
7467 (org-show-context 'agenda)
7468 (save-excursion
7469 (and (outline-next-heading)
7470 (org-flag-heading nil)))) ; show the next heading
7471 (when (outline-invisible-p)
7472 (show-entry)) ; display invisible text
7473 (recenter (/ (window-height) 2))
7474 (run-hooks 'org-agenda-after-show-hook)
7475 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
7477 (defvar org-agenda-after-show-hook nil
7478 "Normal hook run after an item has been shown from the agenda.
7479 Point is in the buffer where the item originated.")
7481 (defun org-agenda-kill ()
7482 "Kill the entry or subtree belonging to the current agenda entry."
7483 (interactive)
7484 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7485 (let* ((bufname-orig (buffer-name))
7486 (marker (or (org-get-at-bol 'org-marker)
7487 (org-agenda-error)))
7488 (buffer (marker-buffer marker))
7489 (pos (marker-position marker))
7490 (type (org-get-at-bol 'type))
7491 dbeg dend (n 0) conf)
7492 (org-with-remote-undo buffer
7493 (with-current-buffer buffer
7494 (save-excursion
7495 (goto-char pos)
7496 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
7497 (setq dbeg (progn (org-back-to-heading t) (point))
7498 dend (org-end-of-subtree t t))
7499 (setq dbeg (point-at-bol)
7500 dend (min (point-max) (1+ (point-at-eol)))))
7501 (goto-char dbeg)
7502 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
7503 (setq conf (or (eq t org-agenda-confirm-kill)
7504 (and (numberp org-agenda-confirm-kill)
7505 (> n org-agenda-confirm-kill))))
7506 (and conf
7507 (not (y-or-n-p
7508 (format "Delete entry with %d lines in buffer \"%s\"? "
7509 n (buffer-name buffer))))
7510 (error "Abort"))
7511 (let ((org-agenda-buffer-name bufname-orig))
7512 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
7513 (with-current-buffer buffer (delete-region dbeg dend))
7514 (message "Agenda item and source killed"))))
7516 (defvar org-archive-default-command)
7517 (defun org-agenda-archive-default ()
7518 "Archive the entry or subtree belonging to the current agenda entry."
7519 (interactive)
7520 (require 'org-archive)
7521 (org-agenda-archive-with org-archive-default-command))
7523 (defun org-agenda-archive-default-with-confirmation ()
7524 "Archive the entry or subtree belonging to the current agenda entry."
7525 (interactive)
7526 (require 'org-archive)
7527 (org-agenda-archive-with org-archive-default-command 'confirm))
7529 (defun org-agenda-archive ()
7530 "Archive the entry or subtree belonging to the current agenda entry."
7531 (interactive)
7532 (org-agenda-archive-with 'org-archive-subtree))
7534 (defun org-agenda-archive-to-archive-sibling ()
7535 "Move the entry to the archive sibling."
7536 (interactive)
7537 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7539 (defun org-agenda-archive-with (cmd &optional confirm)
7540 "Move the entry to the archive sibling."
7541 (interactive)
7542 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7543 (let* ((bufname-orig (buffer-name))
7544 (marker (or (org-get-at-bol 'org-marker)
7545 (org-agenda-error)))
7546 (buffer (marker-buffer marker))
7547 (pos (marker-position marker)))
7548 (org-with-remote-undo buffer
7549 (with-current-buffer buffer
7550 (if (derived-mode-p 'org-mode)
7551 (if (and confirm
7552 (not (y-or-n-p "Archive this subtree or entry? ")))
7553 (error "Abort")
7554 (save-excursion
7555 (goto-char pos)
7556 (let ((org-agenda-buffer-name bufname-orig))
7557 (org-remove-subtree-entries-from-agenda))
7558 (org-back-to-heading t)
7559 (funcall cmd)))
7560 (error "Archiving works only in Org-mode files"))))))
7562 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7563 "Remove all lines in the agenda that correspond to a given subtree.
7564 The subtree is the one in buffer BUF, starting at BEG and ending at END.
7565 If this information is not given, the function uses the tree at point."
7566 (let ((buf (or buf (current-buffer))) m p)
7567 (save-excursion
7568 (unless (and beg end)
7569 (org-back-to-heading t)
7570 (setq beg (point))
7571 (org-end-of-subtree t)
7572 (setq end (point)))
7573 (set-buffer (get-buffer org-agenda-buffer-name))
7574 (save-excursion
7575 (goto-char (point-max))
7576 (beginning-of-line 1)
7577 (while (not (bobp))
7578 (when (and (setq m (org-get-at-bol 'org-marker))
7579 (equal buf (marker-buffer m))
7580 (setq p (marker-position m))
7581 (>= p beg)
7582 (< p end))
7583 (let ((inhibit-read-only t))
7584 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7585 (beginning-of-line 0))))))
7587 (defun org-agenda-refile (&optional goto rfloc no-update)
7588 "Refile the item at point."
7589 (interactive "P")
7590 (if (equal goto '(16))
7591 (org-refile-goto-last-stored)
7592 (let* ((buffer-orig (buffer-name))
7593 (marker (or (org-get-at-bol 'org-hd-marker)
7594 (org-agenda-error)))
7595 (buffer (marker-buffer marker))
7596 (pos (marker-position marker))
7597 (rfloc (or rfloc
7598 (org-refile-get-location
7599 (if goto "Goto" "Refile to") buffer
7600 org-refile-allow-creating-parent-nodes))))
7601 (with-current-buffer buffer
7602 (save-excursion
7603 (save-restriction
7604 (widen)
7605 (goto-char marker)
7606 (let ((org-agenda-buffer-name buffer-orig))
7607 (org-remove-subtree-entries-from-agenda))
7608 (org-refile goto buffer rfloc)))))
7609 (unless no-update (org-agenda-redo))))
7611 (defun org-agenda-open-link (&optional arg)
7612 "Follow the link in the current line, if any.
7613 This looks for a link in the displayed line in the agenda. It also looks
7614 at the text of the entry itself."
7615 (interactive "P")
7616 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7617 (org-get-at-bol 'org-marker)))
7618 (buffer (and marker (marker-buffer marker)))
7619 (prefix (buffer-substring
7620 (point-at-bol) (point-at-eol))))
7621 (cond
7622 (buffer
7623 (with-current-buffer buffer
7624 (save-excursion
7625 (save-restriction
7626 (widen)
7627 (goto-char marker)
7628 (org-offer-links-in-entry arg prefix)))))
7629 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7630 (save-excursion
7631 (beginning-of-line 1)
7632 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7633 (org-open-link-from-string (match-string 1)))
7634 (t (error "No link to open here")))))
7636 (defun org-agenda-copy-local-variable (var)
7637 "Get a variable from a referenced buffer and install it here."
7638 (let ((m (org-get-at-bol 'org-marker)))
7639 (when (and m (buffer-live-p (marker-buffer m)))
7640 (org-set-local var (with-current-buffer (marker-buffer m)
7641 (symbol-value var))))))
7643 (defun org-agenda-switch-to (&optional delete-other-windows)
7644 "Go to the Org-mode file which contains the item at point."
7645 (interactive)
7646 (if (and org-return-follows-link
7647 (not (org-get-at-bol 'org-marker))
7648 (org-in-regexp org-bracket-link-regexp))
7649 (org-open-link-from-string (match-string 0))
7650 (let* ((marker (or (org-get-at-bol 'org-marker)
7651 (org-agenda-error)))
7652 (buffer (marker-buffer marker))
7653 (pos (marker-position marker)))
7654 (org-pop-to-buffer-same-window buffer)
7655 (and delete-other-windows (delete-other-windows))
7656 (widen)
7657 (goto-char pos)
7658 (when (derived-mode-p 'org-mode)
7659 (org-show-context 'agenda)
7660 (save-excursion
7661 (and (outline-next-heading)
7662 (org-flag-heading nil))) ; show the next heading
7663 (when (outline-invisible-p)
7664 (show-entry)) ; display invisible text
7665 (run-hooks 'org-agenda-after-show-hook)))))
7667 (defun org-agenda-goto-mouse (ev)
7668 "Go to the Org-mode file which contains the item at the mouse click."
7669 (interactive "e")
7670 (mouse-set-point ev)
7671 (org-agenda-goto))
7673 (defun org-agenda-show (&optional full-entry)
7674 "Display the Org-mode file which contains the item at point.
7675 With prefix argument FULL-ENTRY, make the entire entry visible
7676 if it was hidden in the outline."
7677 (interactive "P")
7678 (let ((win (selected-window)))
7679 (if full-entry
7680 (let ((org-show-entry-below t))
7681 (org-agenda-goto t))
7682 (org-agenda-goto t))
7683 (select-window win)))
7685 (defvar org-agenda-show-window nil)
7686 (defun org-agenda-show-and-scroll-up (&optional arg)
7687 "Display the Org-mode file which contains the item at point.
7688 When called repeatedly, scroll the window that is displaying the buffer.
7689 With a \\[universal-argument] prefix, use `org-show-entry' instead of
7690 `show-subtree' to display the item, so that drawers and logbooks stay
7691 folded."
7692 (interactive "P")
7693 (let ((win (selected-window)))
7694 (if (and (window-live-p org-agenda-show-window)
7695 (eq this-command last-command))
7696 (progn
7697 (select-window org-agenda-show-window)
7698 (ignore-errors (scroll-up)))
7699 (org-agenda-goto t)
7700 (if arg (org-show-entry) (show-subtree))
7701 (setq org-agenda-show-window (selected-window)))
7702 (select-window win)))
7704 (defun org-agenda-show-scroll-down ()
7705 "Scroll down the window showing the agenda."
7706 (interactive)
7707 (let ((win (selected-window)))
7708 (when (window-live-p org-agenda-show-window)
7709 (select-window org-agenda-show-window)
7710 (ignore-errors (scroll-down))
7711 (select-window win))))
7713 (defun org-agenda-show-1 (&optional more)
7714 "Display the Org-mode file which contains the item at point.
7715 The prefix arg selects the amount of information to display:
7717 0 hide the subtree
7718 1 just show the entry according to defaults.
7719 2 show the children view
7720 3 show the subtree view
7721 4 show the entire subtree and any LOGBOOK drawers
7722 5 show the entire subtree and any drawers
7723 With prefix argument FULL-ENTRY, make the entire entry visible
7724 if it was hidden in the outline."
7725 (interactive "p")
7726 (let ((win (selected-window)))
7727 (org-agenda-goto t)
7728 (org-recenter-heading 1)
7729 (cond
7730 ((= more 0)
7731 (hide-subtree)
7732 (save-excursion
7733 (org-back-to-heading)
7734 (run-hook-with-args 'org-cycle-hook 'folded))
7735 (message "Remote: FOLDED"))
7736 ((and (org-called-interactively-p 'any) (= more 1))
7737 (message "Remote: show with default settings"))
7738 ((= more 2)
7739 (show-entry)
7740 (show-children)
7741 (save-excursion
7742 (org-back-to-heading)
7743 (run-hook-with-args 'org-cycle-hook 'children))
7744 (message "Remote: CHILDREN"))
7745 ((= more 3)
7746 (show-subtree)
7747 (save-excursion
7748 (org-back-to-heading)
7749 (run-hook-with-args 'org-cycle-hook 'subtree))
7750 (message "Remote: SUBTREE"))
7751 ((= more 4)
7752 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7753 (org-drawer-regexp
7754 (concat "^[ \t]*:\\("
7755 (mapconcat 'regexp-quote org-drawers "\\|")
7756 "\\):[ \t]*$")))
7757 (show-subtree)
7758 (save-excursion
7759 (org-back-to-heading)
7760 (org-cycle-hide-drawers 'subtree)))
7761 (message "Remote: SUBTREE AND LOGBOOK"))
7762 ((> more 4)
7763 (show-subtree)
7764 (message "Remote: SUBTREE AND ALL DRAWERS")))
7765 (select-window win)))
7767 (defun org-recenter-heading (n)
7768 (save-excursion
7769 (org-back-to-heading)
7770 (recenter n)))
7772 (defvar org-agenda-cycle-counter nil)
7773 (defun org-agenda-cycle-show (&optional n)
7774 "Show the current entry in another window, with default settings.
7775 Default settings are taken from `org-show-hierarchy-above' and siblings.
7776 When use repeatedly in immediate succession, the remote entry will cycle
7777 through visibility
7779 children -> subtree -> folded
7781 When called with a numeric prefix arg, that arg will be passed through to
7782 `org-agenda-show-1'. For the interpretation of that argument, see the
7783 docstring of `org-agenda-show-1'."
7784 (interactive "P")
7785 (if (integerp n)
7786 (setq org-agenda-cycle-counter n)
7787 (if (not (eq last-command this-command))
7788 (setq org-agenda-cycle-counter 1)
7789 (if (equal org-agenda-cycle-counter 0)
7790 (setq org-agenda-cycle-counter 2)
7791 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7792 (if (> org-agenda-cycle-counter 3)
7793 (setq org-agenda-cycle-counter 0)))))
7794 (org-agenda-show-1 org-agenda-cycle-counter))
7796 (defun org-agenda-recenter (arg)
7797 "Display the Org-mode file which contains the item at point and recenter."
7798 (interactive "P")
7799 (let ((win (selected-window)))
7800 (org-agenda-goto t)
7801 (recenter arg)
7802 (select-window win)))
7804 (defun org-agenda-show-mouse (ev)
7805 "Display the Org-mode file which contains the item at the mouse click."
7806 (interactive "e")
7807 (mouse-set-point ev)
7808 (org-agenda-show))
7810 (defun org-agenda-check-no-diary ()
7811 "Check if the entry is a diary link and abort if yes."
7812 (if (org-get-at-bol 'org-agenda-diary-link)
7813 (org-agenda-error)))
7815 (defun org-agenda-error ()
7816 (error "Command not allowed in this line"))
7818 (defun org-agenda-tree-to-indirect-buffer ()
7819 "Show the subtree corresponding to the current entry in an indirect buffer.
7820 This calls the command `org-tree-to-indirect-buffer' from the original
7821 Org-mode buffer.
7822 With numerical prefix arg ARG, go up to this level and then take that tree.
7823 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7824 use the dedicated frame)."
7825 (interactive)
7826 (if (and current-prefix-arg (listp current-prefix-arg))
7827 (org-agenda-do-tree-to-indirect-buffer)
7828 (let ((agenda-window (selected-window))
7829 (indirect-window
7830 (and org-last-indirect-buffer
7831 (get-buffer-window org-last-indirect-buffer))))
7832 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer))
7833 (unwind-protect
7834 (progn
7835 (unless (and indirect-window (window-live-p indirect-window))
7836 (setq indirect-window (split-window agenda-window)))
7837 (select-window indirect-window)
7838 (switch-to-buffer org-last-indirect-buffer :norecord)
7839 (fit-window-to-buffer indirect-window))
7840 (select-window (get-buffer-window org-agenda-buffer-name))))))
7842 (defun org-agenda-do-tree-to-indirect-buffer ()
7843 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7844 (org-agenda-check-no-diary)
7845 (let* ((marker (or (org-get-at-bol 'org-marker)
7846 (org-agenda-error)))
7847 (buffer (marker-buffer marker))
7848 (pos (marker-position marker)))
7849 (with-current-buffer buffer
7850 (save-excursion
7851 (goto-char pos)
7852 (call-interactively 'org-tree-to-indirect-buffer)))))
7854 (defvar org-last-heading-marker (make-marker)
7855 "Marker pointing to the headline that last changed its TODO state
7856 by a remote command from the agenda.")
7858 (defun org-agenda-todo-nextset ()
7859 "Switch TODO entry to next sequence."
7860 (interactive)
7861 (org-agenda-todo 'nextset))
7863 (defun org-agenda-todo-previousset ()
7864 "Switch TODO entry to previous sequence."
7865 (interactive)
7866 (org-agenda-todo 'previousset))
7868 (defun org-agenda-todo (&optional arg)
7869 "Cycle TODO state of line at point, also in Org-mode file.
7870 This changes the line at point, all other lines in the agenda referring to
7871 the same tree node, and the headline of the tree node in the Org-mode file."
7872 (interactive "P")
7873 (org-agenda-check-no-diary)
7874 (let* ((col (current-column))
7875 (marker (or (org-get-at-bol 'org-marker)
7876 (org-agenda-error)))
7877 (buffer (marker-buffer marker))
7878 (pos (marker-position marker))
7879 (hdmarker (org-get-at-bol 'org-hd-marker))
7880 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7881 (inhibit-read-only t)
7882 org-agenda-headline-snapshot-before-repeat newhead just-one)
7883 (org-with-remote-undo buffer
7884 (with-current-buffer buffer
7885 (widen)
7886 (goto-char pos)
7887 (org-show-context 'agenda)
7888 (save-excursion
7889 (and (outline-next-heading)
7890 (org-flag-heading nil))) ; show the next heading
7891 (let ((current-prefix-arg arg))
7892 (call-interactively 'org-todo))
7893 (and (bolp) (forward-char 1))
7894 (setq newhead (org-get-heading))
7895 (when (and (org-bound-and-true-p
7896 org-agenda-headline-snapshot-before-repeat)
7897 (not (equal org-agenda-headline-snapshot-before-repeat
7898 newhead))
7899 todayp)
7900 (setq newhead org-agenda-headline-snapshot-before-repeat
7901 just-one t))
7902 (save-excursion
7903 (org-back-to-heading)
7904 (move-marker org-last-heading-marker (point))))
7905 (beginning-of-line 1)
7906 (save-excursion
7907 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7908 (org-move-to-column col))))
7910 (defun org-agenda-add-note (&optional arg)
7911 "Add a time-stamped note to the entry at point."
7912 (interactive "P")
7913 (org-agenda-check-no-diary)
7914 (let* ((marker (or (org-get-at-bol 'org-marker)
7915 (org-agenda-error)))
7916 (buffer (marker-buffer marker))
7917 (pos (marker-position marker))
7918 (hdmarker (org-get-at-bol 'org-hd-marker))
7919 (inhibit-read-only t))
7920 (with-current-buffer buffer
7921 (widen)
7922 (goto-char pos)
7923 (org-show-context 'agenda)
7924 (save-excursion
7925 (and (outline-next-heading)
7926 (org-flag-heading nil))) ; show the next heading
7927 (org-add-note))))
7929 (defun org-agenda-change-all-lines (newhead hdmarker
7930 &optional fixface just-this)
7931 "Change all lines in the agenda buffer which match HDMARKER.
7932 The new content of the line will be NEWHEAD (as modified by
7933 `org-agenda-format-item'). HDMARKER is checked with
7934 `equal' against all `org-hd-marker' text properties in the file.
7935 If FIXFACE is non-nil, the face of each item is modified according to
7936 the new TODO state.
7937 If JUST-THIS is non-nil, change just the current line, not all.
7938 If FORCE-TAGS is non nil, the car of it returns the new tags."
7939 (let* ((inhibit-read-only t)
7940 (line (org-current-line))
7941 (org-agenda-buffer (current-buffer))
7942 (thetags (with-current-buffer (marker-buffer hdmarker)
7943 (save-excursion (save-restriction (widen)
7944 (goto-char hdmarker)
7945 (org-get-tags-at)))))
7946 props m pl undone-face done-face finish new dotime cat tags)
7947 (save-excursion
7948 (goto-char (point-max))
7949 (beginning-of-line 1)
7950 (while (not finish)
7951 (setq finish (bobp))
7952 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7953 (or (not just-this) (= (org-current-line) line))
7954 (equal m hdmarker))
7955 (setq props (text-properties-at (point))
7956 dotime (org-get-at-bol 'dotime)
7957 cat (org-get-at-bol 'org-category)
7958 tags thetags
7960 (let ((org-prefix-format-compiled
7961 (or (get-text-property (point) 'format)
7962 org-prefix-format-compiled))
7963 (extra (org-get-at-bol 'extra)))
7964 (with-current-buffer (marker-buffer hdmarker)
7965 (save-excursion
7966 (save-restriction
7967 (widen)
7968 (org-agenda-format-item extra newhead cat tags dotime)))))
7969 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7970 undone-face (org-get-at-bol 'undone-face)
7971 done-face (org-get-at-bol 'done-face))
7972 (beginning-of-line 1)
7973 (cond
7974 ((equal new "")
7975 (and (looking-at ".*\n?") (replace-match "")))
7976 ((looking-at ".*")
7977 (replace-match new t t)
7978 (beginning-of-line 1)
7979 (add-text-properties (point-at-bol) (point-at-eol) props)
7980 (when fixface
7981 (add-text-properties
7982 (point-at-bol) (point-at-eol)
7983 (list 'face
7984 (if org-last-todo-state-is-todo
7985 undone-face done-face))))
7986 (org-agenda-highlight-todo 'line)
7987 (beginning-of-line 1))
7988 (t (error "Line update did not work"))))
7989 (beginning-of-line 0)))
7990 (org-finalize-agenda)))
7992 (defun org-agenda-align-tags (&optional line)
7993 "Align all tags in agenda items to `org-agenda-tags-column'."
7994 (let ((inhibit-read-only t) l c)
7995 (save-excursion
7996 (goto-char (if line (point-at-bol) (point-min)))
7997 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7998 (if line (point-at-eol) nil) t)
7999 (add-text-properties
8000 (match-beginning 2) (match-end 2)
8001 (list 'face (delq nil (let ((prop (get-text-property
8002 (match-beginning 2) 'face)))
8003 (or (listp prop) (setq prop (list prop)))
8004 (if (memq 'org-tag prop)
8005 prop
8006 (cons 'org-tag prop))))))
8007 (setq l (- (match-end 2) (match-beginning 2))
8008 c (if (< org-agenda-tags-column 0)
8009 (- (abs org-agenda-tags-column) l)
8010 org-agenda-tags-column))
8011 (delete-region (match-beginning 1) (match-end 1))
8012 (goto-char (match-beginning 1))
8013 (insert (org-add-props
8014 (make-string (max 1 (- c (current-column))) ?\ )
8015 (plist-put (copy-sequence (text-properties-at (point)))
8016 'face nil))))
8017 (goto-char (point-min))
8018 (org-font-lock-add-tag-faces (point-max)))))
8020 (defun org-agenda-priority-up ()
8021 "Increase the priority of line at point, also in Org-mode file."
8022 (interactive)
8023 (org-agenda-priority 'up))
8025 (defun org-agenda-priority-down ()
8026 "Decrease the priority of line at point, also in Org-mode file."
8027 (interactive)
8028 (org-agenda-priority 'down))
8030 (defun org-agenda-priority (&optional force-direction)
8031 "Set the priority of line at point, also in Org-mode file.
8032 This changes the line at point, all other lines in the agenda referring to
8033 the same tree node, and the headline of the tree node in the Org-mode file."
8034 (interactive)
8035 (unless org-enable-priority-commands
8036 (error "Priority commands are disabled"))
8037 (org-agenda-check-no-diary)
8038 (let* ((marker (or (org-get-at-bol 'org-marker)
8039 (org-agenda-error)))
8040 (hdmarker (org-get-at-bol 'org-hd-marker))
8041 (buffer (marker-buffer hdmarker))
8042 (pos (marker-position hdmarker))
8043 (inhibit-read-only t)
8044 newhead)
8045 (org-with-remote-undo buffer
8046 (with-current-buffer buffer
8047 (widen)
8048 (goto-char pos)
8049 (org-show-context 'agenda)
8050 (save-excursion
8051 (and (outline-next-heading)
8052 (org-flag-heading nil))) ; show the next heading
8053 (funcall 'org-priority force-direction)
8054 (end-of-line 1)
8055 (setq newhead (org-get-heading)))
8056 (org-agenda-change-all-lines newhead hdmarker)
8057 (beginning-of-line 1))))
8059 ;; FIXME: should fix the tags property of the agenda line.
8060 (defun org-agenda-set-tags (&optional tag onoff)
8061 "Set tags for the current headline."
8062 (interactive)
8063 (org-agenda-check-no-diary)
8064 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8065 (call-interactively 'org-change-tag-in-region)
8066 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8067 (org-agenda-error)))
8068 (buffer (marker-buffer hdmarker))
8069 (pos (marker-position hdmarker))
8070 (inhibit-read-only t)
8071 newhead)
8072 (org-with-remote-undo buffer
8073 (with-current-buffer buffer
8074 (widen)
8075 (goto-char pos)
8076 (save-excursion
8077 (org-show-context 'agenda))
8078 (save-excursion
8079 (and (outline-next-heading)
8080 (org-flag-heading nil))) ; show the next heading
8081 (goto-char pos)
8082 (if tag
8083 (org-toggle-tag tag onoff)
8084 (call-interactively 'org-set-tags))
8085 (end-of-line 1)
8086 (setq newhead (org-get-heading)))
8087 (org-agenda-change-all-lines newhead hdmarker)
8088 (beginning-of-line 1)))))
8090 (defun org-agenda-set-property ()
8091 "Set a property for the current headline."
8092 (interactive)
8093 (org-agenda-check-no-diary)
8094 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8095 (org-agenda-error)))
8096 (buffer (marker-buffer hdmarker))
8097 (pos (marker-position hdmarker))
8098 (inhibit-read-only t)
8099 newhead)
8100 (org-with-remote-undo buffer
8101 (with-current-buffer buffer
8102 (widen)
8103 (goto-char pos)
8104 (save-excursion
8105 (org-show-context 'agenda))
8106 (save-excursion
8107 (and (outline-next-heading)
8108 (org-flag-heading nil))) ; show the next heading
8109 (goto-char pos)
8110 (call-interactively 'org-set-property)))))
8112 (defun org-agenda-set-effort ()
8113 "Set the effort property for the current headline."
8114 (interactive)
8115 (org-agenda-check-no-diary)
8116 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8117 (org-agenda-error)))
8118 (buffer (marker-buffer hdmarker))
8119 (pos (marker-position hdmarker))
8120 (inhibit-read-only t)
8121 newhead)
8122 (org-with-remote-undo buffer
8123 (with-current-buffer buffer
8124 (widen)
8125 (goto-char pos)
8126 (save-excursion
8127 (org-show-context 'agenda))
8128 (save-excursion
8129 (and (outline-next-heading)
8130 (org-flag-heading nil))) ; show the next heading
8131 (goto-char pos)
8132 (call-interactively 'org-set-effort)
8133 (end-of-line 1)
8134 (setq newhead (org-get-heading)))
8135 (org-agenda-change-all-lines newhead hdmarker))))
8137 (defun org-agenda-toggle-archive-tag ()
8138 "Toggle the archive tag for the current entry."
8139 (interactive)
8140 (org-agenda-check-no-diary)
8141 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8142 (org-agenda-error)))
8143 (buffer (marker-buffer hdmarker))
8144 (pos (marker-position hdmarker))
8145 (inhibit-read-only t)
8146 newhead)
8147 (org-with-remote-undo buffer
8148 (with-current-buffer buffer
8149 (widen)
8150 (goto-char pos)
8151 (org-show-context 'agenda)
8152 (save-excursion
8153 (and (outline-next-heading)
8154 (org-flag-heading nil))) ; show the next heading
8155 (call-interactively 'org-toggle-archive-tag)
8156 (end-of-line 1)
8157 (setq newhead (org-get-heading)))
8158 (org-agenda-change-all-lines newhead hdmarker)
8159 (beginning-of-line 1))))
8161 (defun org-agenda-do-date-later (arg)
8162 (interactive "P")
8163 (cond
8164 ((or (equal arg '(16))
8165 (memq last-command
8166 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
8167 (setq this-command 'org-agenda-date-later-minutes)
8168 (org-agenda-date-later-minutes 1))
8169 ((or (equal arg '(4))
8170 (memq last-command
8171 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8172 (setq this-command 'org-agenda-date-later-hours)
8173 (org-agenda-date-later-hours 1))
8175 (org-agenda-date-later (prefix-numeric-value arg)))))
8177 (defun org-agenda-do-date-earlier (arg)
8178 (interactive "P")
8179 (cond
8180 ((or (equal arg '(16))
8181 (memq last-command
8182 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
8183 (setq this-command 'org-agenda-date-earlier-minutes)
8184 (org-agenda-date-earlier-minutes 1))
8185 ((or (equal arg '(4))
8186 (memq last-command
8187 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8188 (setq this-command 'org-agenda-date-earlier-hours)
8189 (org-agenda-date-earlier-hours 1))
8191 (org-agenda-date-earlier (prefix-numeric-value arg)))))
8193 (defun org-agenda-date-later (arg &optional what)
8194 "Change the date of this item to ARG day(s) later."
8195 (interactive "p")
8196 (org-agenda-check-type t 'agenda 'timeline)
8197 (org-agenda-check-no-diary)
8198 (let* ((marker (or (org-get-at-bol 'org-marker)
8199 (org-agenda-error)))
8200 (buffer (marker-buffer marker))
8201 (pos (marker-position marker))
8202 cdate today)
8203 (org-with-remote-undo buffer
8204 (with-current-buffer buffer
8205 (widen)
8206 (goto-char pos)
8207 (if (not (org-at-timestamp-p))
8208 (error "Cannot find time stamp"))
8209 (when (and org-agenda-move-date-from-past-immediately-to-today
8210 (equal arg 1)
8211 (or (not what) (eq what 'day))
8212 (not (save-match-data (org-at-date-range-p))))
8213 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
8214 cdate (calendar-absolute-from-gregorian
8215 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
8216 today (org-today))
8217 (if (> today cdate)
8218 ;; immediately shift to today
8219 (setq arg (- today cdate))))
8220 (org-timestamp-change arg (or what 'day))
8221 (when (and (org-at-date-range-p)
8222 (re-search-backward org-tr-regexp-both (point-at-bol)))
8223 (let ((end org-last-changed-timestamp))
8224 (org-timestamp-change arg (or what 'day))
8225 (setq org-last-changed-timestamp
8226 (concat org-last-changed-timestamp "--" end)))))
8227 (org-agenda-show-new-time marker org-last-changed-timestamp))
8228 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8230 (defun org-agenda-date-earlier (arg &optional what)
8231 "Change the date of this item to ARG day(s) earlier."
8232 (interactive "p")
8233 (org-agenda-date-later (- arg) what))
8235 (defun org-agenda-date-later-minutes (arg)
8236 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8237 (interactive "p")
8238 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8239 (org-agenda-date-later arg 'minute))
8241 (defun org-agenda-date-earlier-minutes (arg)
8242 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8243 (interactive "p")
8244 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8245 (org-agenda-date-earlier arg 'minute))
8247 (defun org-agenda-date-later-hours (arg)
8248 "Change the time of this item, in hour steps."
8249 (interactive "p")
8250 (org-agenda-date-later arg 'hour))
8252 (defun org-agenda-date-earlier-hours (arg)
8253 "Change the time of this item, in hour steps."
8254 (interactive "p")
8255 (org-agenda-date-earlier arg 'hour))
8257 (defun org-agenda-show-new-time (marker stamp &optional prefix)
8258 "Show new date stamp via text properties."
8259 ;; We use text properties to make this undoable
8260 (let ((inhibit-read-only t)
8261 (buffer-invisibility-spec))
8262 (setq stamp (concat " " prefix " => " stamp))
8263 (save-excursion
8264 (goto-char (point-max))
8265 (while (not (bobp))
8266 (when (equal marker (org-get-at-bol 'org-marker))
8267 (org-move-to-column (- (window-width) (length stamp)) t)
8268 (org-agenda-fix-tags-filter-overlays-at (point))
8269 (if (featurep 'xemacs)
8270 ;; Use `duplicable' property to trigger undo recording
8271 (let ((ex (make-extent nil nil))
8272 (gl (make-glyph stamp)))
8273 (set-glyph-face gl 'secondary-selection)
8274 (set-extent-properties
8275 ex (list 'invisible t 'end-glyph gl 'duplicable t))
8276 (insert-extent ex (1- (point)) (point-at-eol)))
8277 (add-text-properties
8278 (1- (point)) (point-at-eol)
8279 (list 'display (org-add-props stamp nil
8280 'face 'secondary-selection))))
8281 (beginning-of-line 1))
8282 (beginning-of-line 0)))))
8284 (defun org-agenda-date-prompt (arg)
8285 "Change the date of this item. Date is prompted for, with default today.
8286 The prefix ARG is passed to the `org-time-stamp' command and can therefore
8287 be used to request time specification in the time stamp."
8288 (interactive "P")
8289 (org-agenda-check-type t 'agenda 'timeline)
8290 (org-agenda-check-no-diary)
8291 (let* ((marker (or (org-get-at-bol 'org-marker)
8292 (org-agenda-error)))
8293 (buffer (marker-buffer marker))
8294 (pos (marker-position marker)))
8295 (org-with-remote-undo buffer
8296 (with-current-buffer buffer
8297 (widen)
8298 (goto-char pos)
8299 (if (not (org-at-timestamp-p t))
8300 (error "Cannot find time stamp"))
8301 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8302 (org-agenda-show-new-time marker org-last-changed-timestamp))
8303 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8305 (defun org-agenda-schedule (arg &optional time)
8306 "Schedule the item at point.
8307 ARG is passed through to `org-schedule'."
8308 (interactive "P")
8309 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8310 (org-agenda-check-no-diary)
8311 (let* ((marker (or (org-get-at-bol 'org-marker)
8312 (org-agenda-error)))
8313 (type (marker-insertion-type marker))
8314 (buffer (marker-buffer marker))
8315 (pos (marker-position marker))
8316 (org-insert-labeled-timestamps-at-point nil)
8318 (set-marker-insertion-type marker t)
8319 (org-with-remote-undo buffer
8320 (with-current-buffer buffer
8321 (widen)
8322 (goto-char pos)
8323 (setq ts (org-schedule arg time)))
8324 (org-agenda-show-new-time marker ts "S"))
8325 (message "Item scheduled for %s" ts)))
8327 (defun org-agenda-deadline (arg &optional time)
8328 "Schedule the item at point.
8329 ARG is passed through to `org-deadline'."
8330 (interactive "P")
8331 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8332 (org-agenda-check-no-diary)
8333 (let* ((marker (or (org-get-at-bol 'org-marker)
8334 (org-agenda-error)))
8335 (buffer (marker-buffer marker))
8336 (pos (marker-position marker))
8337 (org-insert-labeled-timestamps-at-point nil)
8339 (org-with-remote-undo buffer
8340 (with-current-buffer buffer
8341 (widen)
8342 (goto-char pos)
8343 (setq ts (org-deadline arg time)))
8344 (org-agenda-show-new-time marker ts "D"))
8345 (message "Deadline for this item set to %s" ts)))
8347 (defun org-agenda-action ()
8348 "Select entry for agenda action, or execute an agenda action.
8349 This command prompts for another letter. Valid inputs are:
8351 m Mark the entry at point for an agenda action
8352 s Schedule the marked entry to the date at the cursor
8353 d Set the deadline of the marked entry to the date at the cursor
8354 r Call `org-remember' with cursor date as the default date
8355 c Call `org-capture' with cursor date as the default date
8356 SPC Show marked entry in other window
8357 TAB Visit marked entry in other window
8359 The cursor may be at a date in the calendar, or in the Org agenda."
8360 (interactive)
8361 (let (ans)
8362 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
8363 (setq ans (read-char-exclusive))
8364 (cond
8365 ((equal ans ?m)
8366 ;; Mark this entry
8367 (if (eq major-mode 'org-agenda-mode)
8368 (let ((m (or (org-get-at-bol 'org-hd-marker)
8369 (org-get-at-bol 'org-marker))))
8370 (if m
8371 (progn
8372 (move-marker org-agenda-action-marker
8373 (marker-position m) (marker-buffer m))
8374 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
8375 (error "Don't know which entry to mark")))
8376 (error "This command works only in the agenda")))
8377 ((equal ans ?s)
8378 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
8379 ((equal ans ?d)
8380 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
8381 ((equal ans ?r)
8382 (org-agenda-do-action '(org-remember) t))
8383 ((equal ans ?c)
8384 (org-agenda-do-action '(org-capture) t))
8385 ((equal ans ?\ )
8386 (let ((cw (selected-window)))
8387 (org-switch-to-buffer-other-window
8388 (marker-buffer org-agenda-action-marker))
8389 (goto-char org-agenda-action-marker)
8390 (org-show-context 'agenda)
8391 (select-window cw)))
8392 ((equal ans ?\C-i)
8393 (org-switch-to-buffer-other-window
8394 (marker-buffer org-agenda-action-marker))
8395 (goto-char org-agenda-action-marker)
8396 (org-show-context 'agenda))
8397 (t (error "Invalid agenda action %c" ans)))))
8399 (defun org-agenda-do-action (form &optional current-buffer)
8400 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
8401 (let ((org-overriding-default-time (org-get-cursor-date)))
8402 (if current-buffer
8403 (eval form)
8404 (if (not (marker-buffer org-agenda-action-marker))
8405 (error "No entry has been selected for agenda action")
8406 (with-current-buffer (marker-buffer org-agenda-action-marker)
8407 (save-excursion
8408 (save-restriction
8409 (widen)
8410 (goto-char org-agenda-action-marker)
8411 (eval form))))))))
8413 (defun org-agenda-clock-in (&optional arg)
8414 "Start the clock on the currently selected item."
8415 (interactive "P")
8416 (org-agenda-check-no-diary)
8417 (if (equal arg '(4))
8418 (org-clock-in arg)
8419 (let* ((marker (or (org-get-at-bol 'org-marker)
8420 (org-agenda-error)))
8421 (hdmarker (or (org-get-at-bol 'org-hd-marker)
8422 marker))
8423 (pos (marker-position marker))
8424 newhead)
8425 (org-with-remote-undo (marker-buffer marker)
8426 (with-current-buffer (marker-buffer marker)
8427 (widen)
8428 (goto-char pos)
8429 (org-show-context 'agenda)
8430 (org-show-entry)
8431 (org-cycle-hide-drawers 'children)
8432 (org-clock-in arg)
8433 (setq newhead (org-get-heading)))
8434 (org-agenda-change-all-lines newhead hdmarker)))))
8436 (defun org-agenda-clock-out ()
8437 "Stop the currently running clock."
8438 (interactive)
8439 (unless (marker-buffer org-clock-marker)
8440 (error "No running clock"))
8441 (let ((marker (make-marker)) newhead)
8442 (org-with-remote-undo (marker-buffer org-clock-marker)
8443 (with-current-buffer (marker-buffer org-clock-marker)
8444 (save-excursion
8445 (save-restriction
8446 (widen)
8447 (goto-char org-clock-marker)
8448 (org-back-to-heading t)
8449 (move-marker marker (point))
8450 (org-clock-out)
8451 (setq newhead (org-get-heading))))))
8452 (org-agenda-change-all-lines newhead marker)
8453 (move-marker marker nil)))
8455 (defun org-agenda-clock-cancel (&optional arg)
8456 "Cancel the currently running clock."
8457 (interactive "P")
8458 (unless (marker-buffer org-clock-marker)
8459 (error "No running clock"))
8460 (org-with-remote-undo (marker-buffer org-clock-marker)
8461 (org-clock-cancel)))
8463 (defun org-agenda-clock-goto ()
8464 "Jump to the currently clocked in task within the agenda.
8465 If the currently clocked in task is not listed in the agenda
8466 buffer, display it in another window."
8467 (interactive)
8468 (let (pos)
8469 (mapc (lambda (o)
8470 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
8471 (setq pos (overlay-start o))))
8472 (overlays-in (point-min) (point-max)))
8473 (cond (pos (goto-char pos))
8474 ;; If the currently clocked entry is not in the agenda
8475 ;; buffer, we visit it in another window:
8476 (org-clock-current-task
8477 (org-switch-to-buffer-other-window (org-clock-goto)))
8478 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
8480 (defun org-agenda-diary-entry-in-org-file ()
8481 "Make a diary entry in the file `org-agenda-diary-file'."
8482 (let (d1 d2 char (text "") dp1 dp2)
8483 (if (equal (buffer-name) "*Calendar*")
8484 (setq d1 (calendar-cursor-to-date t)
8485 d2 (car calendar-mark-ring))
8486 (setq dp1 (get-text-property (point-at-bol) 'day))
8487 (unless dp1 (error "No date defined in current line"))
8488 (setq d1 (calendar-gregorian-from-absolute dp1)
8489 d2 (and (ignore-errors (mark))
8490 (save-excursion
8491 (goto-char (mark))
8492 (setq dp2 (get-text-property (point-at-bol) 'day)))
8493 (calendar-gregorian-from-absolute dp2))))
8494 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
8495 (setq char (read-char-exclusive))
8496 (cond
8497 ((equal char ?d)
8498 (setq text (read-string "Day entry: "))
8499 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
8500 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8501 ((equal char ?a)
8502 (setq d1 (list (car d1) (nth 1 d1)
8503 (read-number (format "Reference year [%d]: " (nth 2 d1))
8504 (nth 2 d1))))
8505 (setq text (read-string "Anniversary (use %d to show years): "))
8506 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
8507 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8508 ((equal char ?b)
8509 (setq text (read-string "Block entry: "))
8510 (unless (and d1 d2 (not (equal d1 d2)))
8511 (error "No block of days selected"))
8512 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
8513 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8514 ((equal char ?j)
8515 (org-switch-to-buffer-other-window
8516 (find-file-noselect org-agenda-diary-file))
8517 (require 'org-datetree)
8518 (org-datetree-find-date-create d1)
8519 (org-reveal t))
8520 (t (error "Invalid selection character `%c'" char)))))
8522 (defcustom org-agenda-insert-diary-strategy 'date-tree
8523 "Where in `org-agenda-diary-file' should new entries be added?
8524 Valid values:
8526 date-tree in the date tree, as child of the date
8527 top-level as top-level entries at the end of the file."
8528 :group 'org-agenda
8529 :type '(choice
8530 (const :tag "in a date tree" date-tree)
8531 (const :tag "as top level at end of file" top-level)))
8533 (defcustom org-agenda-insert-diary-extract-time nil
8534 "Non-nil means extract any time specification from the diary entry."
8535 :group 'org-agenda
8536 :version "24.1"
8537 :type 'boolean)
8539 (defcustom org-agenda-bulk-mark-char ">"
8540 "A single-character string to be used as the bulk mark."
8541 :group 'org-agenda
8542 :version "24.1"
8543 :type 'string)
8545 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8546 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8547 If TEXT is not empty, it will become the headline of the new entry, and
8548 the resulting entry will not be shown. When TEXT is empty, switch to
8549 `org-agenda-diary-file' and let the user finish the entry there."
8550 (let ((cw (current-window-configuration)))
8551 (org-switch-to-buffer-other-window
8552 (find-file-noselect org-agenda-diary-file))
8553 (widen)
8554 (goto-char (point-min))
8555 (cond
8556 ((eq type 'anniversary)
8557 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8558 (progn
8559 (or (org-at-heading-p t)
8560 (progn
8561 (outline-next-heading)
8562 (insert "* Anniversaries\n\n")
8563 (beginning-of-line -1)))))
8564 (outline-next-heading)
8565 (org-back-over-empty-lines)
8566 (backward-char 1)
8567 (insert "\n")
8568 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8569 (nth 2 d1) (car d1) (nth 1 d1) text)))
8570 ((eq type 'day)
8571 (let ((org-prefix-has-time t)
8572 (org-agenda-time-leading-zero t)
8573 fmt time time2)
8574 (if org-agenda-insert-diary-extract-time
8575 ;; Use org-agenda-format-item to parse text for a time-range and
8576 ;; remove it. FIXME: This is a hack, we should refactor
8577 ;; that function to make time extraction available separately
8578 (setq fmt (org-agenda-format-item nil text nil nil t)
8579 time (get-text-property 0 'time fmt)
8580 time2 (if (> (length time) 0)
8581 ;; split-string removes trailing ...... if
8582 ;; no end time given. First space
8583 ;; separates time from date.
8584 (concat " " (car (split-string time "\\.")))
8585 nil)
8586 text (get-text-property 0 'txt fmt)))
8587 (if (eq org-agenda-insert-diary-strategy 'top-level)
8588 (org-agenda-insert-diary-as-top-level text)
8589 (require 'org-datetree)
8590 (org-datetree-find-date-create d1)
8591 (org-agenda-insert-diary-make-new-entry text))
8592 (org-insert-time-stamp (org-time-from-absolute
8593 (calendar-absolute-from-gregorian d1))
8594 nil nil nil nil time2))
8595 (end-of-line 0))
8596 ((eq type 'block)
8597 (if (> (calendar-absolute-from-gregorian d1)
8598 (calendar-absolute-from-gregorian d2))
8599 (setq d1 (prog1 d2 (setq d2 d1))))
8600 (if (eq org-agenda-insert-diary-strategy 'top-level)
8601 (org-agenda-insert-diary-as-top-level text)
8602 (require 'org-datetree)
8603 (org-datetree-find-date-create d1)
8604 (org-agenda-insert-diary-make-new-entry text))
8605 (org-insert-time-stamp (org-time-from-absolute
8606 (calendar-absolute-from-gregorian d1)))
8607 (insert "--")
8608 (org-insert-time-stamp (org-time-from-absolute
8609 (calendar-absolute-from-gregorian d2)))
8610 (end-of-line 0)))
8611 (if (string-match "\\S-" text)
8612 (progn
8613 (set-window-configuration cw)
8614 (message "%s entry added to %s"
8615 (capitalize (symbol-name type))
8616 (abbreviate-file-name org-agenda-diary-file)))
8617 (org-reveal t)
8618 (message "Please finish entry here"))))
8620 (defun org-agenda-insert-diary-as-top-level (text)
8621 "Make new entry as a top-level entry at the end of the file.
8622 Add TEXT as headline, and position the cursor in the second line so that
8623 a timestamp can be added there."
8624 (widen)
8625 (goto-char (point-max))
8626 (or (bolp) (insert "\n"))
8627 (insert "* " text "\n")
8628 (if org-adapt-indentation (org-indent-to-column 2)))
8630 (defun org-agenda-insert-diary-make-new-entry (text)
8631 "Make new entry as last child of current entry.
8632 Add TEXT as headline, and position the cursor in the second line so that
8633 a timestamp can be added there."
8634 (let ((org-show-following-heading t)
8635 (org-show-siblings t)
8636 (org-show-hierarchy-above t)
8637 (org-show-entry-below t)
8638 col)
8639 (outline-next-heading)
8640 (org-back-over-empty-lines)
8641 (or (looking-at "[ \t]*$")
8642 (progn (insert "\n") (backward-char 1)))
8643 (org-insert-heading nil t)
8644 (org-do-demote)
8645 (setq col (current-column))
8646 (insert text "\n")
8647 (if org-adapt-indentation (org-indent-to-column col))
8648 (let ((org-show-following-heading t)
8649 (org-show-siblings t)
8650 (org-show-hierarchy-above t)
8651 (org-show-entry-below t))
8652 (org-show-context))))
8654 (defun org-agenda-diary-entry ()
8655 "Make a diary entry, like the `i' command from the calendar.
8656 All the standard commands work: block, weekly etc.
8657 When `org-agenda-diary-file' points to a file,
8658 `org-agenda-diary-entry-in-org-file' is called instead to create
8659 entries in that Org-mode file."
8660 (interactive)
8661 (org-agenda-check-type t 'agenda 'timeline)
8662 (if (not (eq org-agenda-diary-file 'diary-file))
8663 (org-agenda-diary-entry-in-org-file)
8664 (require 'diary-lib)
8665 (let* ((char (progn
8666 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8667 (read-char-exclusive)))
8668 (cmd (cdr (assoc char
8669 '((?d . insert-diary-entry)
8670 (?w . insert-weekly-diary-entry)
8671 (?m . insert-monthly-diary-entry)
8672 (?y . insert-yearly-diary-entry)
8673 (?a . insert-anniversary-diary-entry)
8674 (?b . insert-block-diary-entry)
8675 (?c . insert-cyclic-diary-entry)))))
8676 (oldf (symbol-function 'calendar-cursor-to-date))
8677 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8678 (point (point))
8679 (mark (or (mark t) (point))))
8680 (unless cmd
8681 (error "No command associated with <%c>" char))
8682 (unless (and (get-text-property point 'day)
8683 (or (not (equal ?b char))
8684 (get-text-property mark 'day)))
8685 (error "Don't know which date to use for diary entry"))
8686 ;; We implement this by hacking the `calendar-cursor-to-date' function
8687 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8688 (let ((calendar-mark-ring
8689 (list (calendar-gregorian-from-absolute
8690 (or (get-text-property mark 'day)
8691 (get-text-property point 'day))))))
8692 (unwind-protect
8693 (progn
8694 (fset 'calendar-cursor-to-date
8695 (lambda (&optional error dummy)
8696 (calendar-gregorian-from-absolute
8697 (get-text-property point 'day))))
8698 (call-interactively cmd))
8699 (fset 'calendar-cursor-to-date oldf))))))
8701 (defun org-agenda-execute-calendar-command (cmd)
8702 "Execute a calendar command from the agenda, with the date associated to
8703 the cursor position."
8704 (org-agenda-check-type t 'agenda 'timeline)
8705 (require 'diary-lib)
8706 (unless (get-text-property (point) 'day)
8707 (error "Don't know which date to use for calendar command"))
8708 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8709 (point (point))
8710 (date (calendar-gregorian-from-absolute
8711 (get-text-property point 'day)))
8712 ;; the following 2 vars are needed in the calendar
8713 (displayed-month (car date))
8714 (displayed-year (nth 2 date)))
8715 (unwind-protect
8716 (progn
8717 (fset 'calendar-cursor-to-date
8718 (lambda (&optional error dummy)
8719 (calendar-gregorian-from-absolute
8720 (get-text-property point 'day))))
8721 (call-interactively cmd))
8722 (fset 'calendar-cursor-to-date oldf))))
8724 (defun org-agenda-phases-of-moon ()
8725 "Display the phases of the moon for the 3 months around the cursor date."
8726 (interactive)
8727 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8729 (defun org-agenda-holidays ()
8730 "Display the holidays for the 3 months around the cursor date."
8731 (interactive)
8732 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8734 (defvar calendar-longitude)
8735 (defvar calendar-latitude)
8736 (defvar calendar-location-name)
8738 (defun org-agenda-sunrise-sunset (arg)
8739 "Display sunrise and sunset for the cursor date.
8740 Latitude and longitude can be specified with the variables
8741 `calendar-latitude' and `calendar-longitude'. When called with prefix
8742 argument, latitude and longitude will be prompted for."
8743 (interactive "P")
8744 (require 'solar)
8745 (let ((calendar-longitude (if arg nil calendar-longitude))
8746 (calendar-latitude (if arg nil calendar-latitude))
8747 (calendar-location-name
8748 (if arg "the given coordinates" calendar-location-name)))
8749 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8751 (defun org-agenda-goto-calendar ()
8752 "Open the Emacs calendar with the date at the cursor."
8753 (interactive)
8754 (org-agenda-check-type t 'agenda 'timeline)
8755 (let* ((day (or (get-text-property (point) 'day)
8756 (error "Don't know which date to open in calendar")))
8757 (date (calendar-gregorian-from-absolute day))
8758 (calendar-move-hook nil)
8759 (calendar-view-holidays-initially-flag nil)
8760 (calendar-view-diary-initially-flag nil))
8761 (calendar)
8762 (calendar-goto-date date)))
8764 ;;;###autoload
8765 (defun org-calendar-goto-agenda ()
8766 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8767 This is a command that has to be installed in `calendar-mode-map'."
8768 (interactive)
8769 (org-agenda-list nil (calendar-absolute-from-gregorian
8770 (calendar-cursor-to-date))
8771 nil))
8773 (defun org-agenda-convert-date ()
8774 (interactive)
8775 (org-agenda-check-type t 'agenda 'timeline)
8776 (let ((day (get-text-property (point) 'day))
8777 date s)
8778 (unless day
8779 (error "Don't know which date to convert"))
8780 (setq date (calendar-gregorian-from-absolute day))
8781 (setq s (concat
8782 "Gregorian: " (calendar-date-string date) "\n"
8783 "ISO: " (calendar-iso-date-string date) "\n"
8784 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8785 "Julian: " (calendar-julian-date-string date) "\n"
8786 "Astron. JD: " (calendar-astro-date-string date)
8787 " (Julian date number at noon UTC)\n"
8788 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8789 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8790 "French: " (calendar-french-date-string date) "\n"
8791 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8792 "Mayan: " (calendar-mayan-date-string date) "\n"
8793 "Coptic: " (calendar-coptic-date-string date) "\n"
8794 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8795 "Persian: " (calendar-persian-date-string date) "\n"
8796 "Chinese: " (calendar-chinese-date-string date) "\n"))
8797 (with-output-to-temp-buffer "*Dates*"
8798 (princ s))
8799 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8801 ;;; Bulk commands
8803 (defvar org-agenda-bulk-marked-entries nil
8804 "List of markers that refer to marked entries in the agenda.")
8806 (defun org-agenda-bulk-marked-p ()
8807 (eq (get-char-property (point-at-bol) 'type)
8808 'org-marked-entry-overlay))
8810 (defun org-agenda-bulk-mark (&optional arg)
8811 "Mark the entry at point for future bulk action."
8812 (interactive "p")
8813 (dotimes (i (or arg 1))
8814 (unless (org-get-at-bol 'org-agenda-diary-link)
8815 (let* ((m (org-get-at-bol 'org-hd-marker))
8817 (unless (org-agenda-bulk-marked-p)
8818 (unless m (error "Nothing to mark at point"))
8819 (push m org-agenda-bulk-marked-entries)
8820 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8821 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
8822 (org-get-todo-face "TODO")
8823 'evaporate)
8824 (overlay-put ov 'type 'org-marked-entry-overlay))
8825 (beginning-of-line 2)
8826 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8827 (beginning-of-line 2))
8828 (message "%d entries marked for bulk action"
8829 (length org-agenda-bulk-marked-entries))))))
8831 (defun org-agenda-bulk-mark-all ()
8832 "Mark all entries for future agenda bulk action."
8833 (interactive)
8834 (org-agenda-bulk-mark-regexp "."))
8836 (defun org-agenda-bulk-mark-regexp (regexp)
8837 "Mark entries matching REGEXP for future agenda bulk action."
8838 (interactive "sMark entries matching regexp: ")
8839 (let ((entries-marked 0))
8840 (save-excursion
8841 (goto-char (point-min))
8842 (goto-char (next-single-property-change (point) 'txt))
8843 (while (re-search-forward regexp nil t)
8844 (when (string-match regexp (get-text-property (point) 'txt))
8845 (setq entries-marked (1+ entries-marked))
8846 (call-interactively 'org-agenda-bulk-mark))))
8847 (if (not entries-marked)
8848 (message "No entry matching this regexp."))))
8850 (defun org-agenda-bulk-unmark (&optional arg)
8851 "Unmark the entry at point for future bulk action."
8852 (interactive "P")
8853 (if arg
8854 (org-agenda-bulk-unmark-all)
8855 (cond ((org-agenda-bulk-marked-p)
8856 (org-agenda-bulk-remove-overlays
8857 (point-at-bol) (+ 2 (point-at-bol)))
8858 (setq org-agenda-bulk-marked-entries
8859 (delete (org-get-at-bol 'org-hd-marker)
8860 org-agenda-bulk-marked-entries))
8861 (beginning-of-line 2)
8862 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8863 (beginning-of-line 2))
8864 (message "%d entries left marked for bulk action"
8865 (length org-agenda-bulk-marked-entries)))
8866 (t (message "No entry to unmark here")))))
8868 (defun org-agenda-bulk-toggle ()
8869 "Toggle marking the entry at point for bulk action."
8870 (interactive)
8871 (if (org-agenda-bulk-marked-p)
8872 (org-agenda-bulk-unmark)
8873 (org-agenda-bulk-mark)))
8875 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8876 "Remove the mark overlays between BEG and END in the agenda buffer.
8877 BEG and END default to the buffer limits.
8879 This only removes the overlays, it does not remove the markers
8880 from the list in `org-agenda-bulk-marked-entries'."
8881 (interactive)
8882 (mapc (lambda (ov)
8883 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8884 (delete-overlay ov)))
8885 (overlays-in (or beg (point-min)) (or end (point-max)))))
8887 (defun org-agenda-bulk-unmark-all ()
8888 "Remove all marks in the agenda buffer.
8889 This will remove the markers and the overlays."
8890 (interactive)
8891 (if (null org-agenda-bulk-marked-entries)
8892 (message "No entry to unmark")
8893 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8894 (setq org-agenda-bulk-marked-entries nil)
8895 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
8897 (defcustom org-agenda-persistent-marks nil
8898 "Non-nil means marked items will stay marked after a bulk action.
8899 You can toggle this interactively by typing `p' when prompted for a
8900 bulk action."
8901 :group 'org-agenda
8902 :version "24.1"
8903 :type 'boolean)
8905 (defun org-agenda-bulk-action (&optional arg)
8906 "Execute an remote-editing action on all marked entries.
8907 The prefix arg is passed through to the command if possible."
8908 (interactive "P")
8909 ;; Make sure we have markers, and only valid ones
8910 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8911 (mapc
8912 (lambda (m)
8913 (unless (and (markerp m)
8914 (marker-buffer m)
8915 (buffer-live-p (marker-buffer m))
8916 (marker-position m))
8917 (error "Marker %s for bulk command is invalid" m)))
8918 org-agenda-bulk-marked-entries)
8920 ;; Prompt for the bulk command
8921 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
8922 (message (concat msg "[r]efile [$]arch [A]rch->sib [t]odo"
8923 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction "
8924 (when org-agenda-bulk-custom-functions
8925 (concat " Custom: ["
8926 (mapconcat (lambda(f) (char-to-string (car f)))
8927 org-agenda-bulk-custom-functions "")
8928 "]"))))
8929 (catch 'exit
8930 (let* ((action (read-char-exclusive))
8931 (org-log-refile (if org-log-refile 'time nil))
8932 (entries (reverse org-agenda-bulk-marked-entries))
8933 redo-at-end
8934 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8935 (cond
8936 ((equal action ?p)
8937 (let ((org-agenda-persistent-marks
8938 (not org-agenda-persistent-marks)))
8939 (org-agenda-bulk-action)
8940 (throw 'exit nil)))
8942 ((equal action ?$)
8943 (setq cmd '(org-agenda-archive)))
8945 ((equal action ?A)
8946 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8948 ((member action '(?r ?w))
8949 (setq rfloc (org-refile-get-location
8950 "Refile to"
8951 (marker-buffer (car org-agenda-bulk-marked-entries))
8952 org-refile-allow-creating-parent-nodes))
8953 (if (nth 3 rfloc)
8954 (setcar (nthcdr 3 rfloc)
8955 (move-marker (make-marker) (nth 3 rfloc)
8956 (or (get-file-buffer (nth 1 rfloc))
8957 (find-buffer-visiting (nth 1 rfloc))
8958 (error "This should not happen")))))
8960 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8961 redo-at-end t))
8963 ((equal action ?t)
8964 (setq state (org-icompleting-read
8965 "Todo state: "
8966 (with-current-buffer (marker-buffer (car entries))
8967 (mapcar 'list org-todo-keywords-1))))
8968 (setq cmd `(let ((org-inhibit-blocking t)
8969 (org-inhibit-logging 'note))
8970 (org-agenda-todo ,state))))
8972 ((memq action '(?- ?+))
8973 (setq tag (org-icompleting-read
8974 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8975 (with-current-buffer (marker-buffer (car entries))
8976 (delq nil
8977 (mapcar (lambda (x)
8978 (if (stringp (car x)) x)) org-tag-alist)))))
8979 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8981 ((memq action '(?s ?d))
8982 (let* ((date (unless arg
8983 (org-read-date
8984 nil nil nil
8985 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8986 (ans (if arg nil org-read-date-final-answer))
8987 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8988 (setq cmd `(let* ((bound (fboundp 'read-string))
8989 (old (and bound (symbol-function 'read-string))))
8990 (unwind-protect
8991 (progn
8992 (fset 'read-string (lambda (&rest ignore) ,ans))
8993 (eval '(,c1 arg)))
8994 (if bound
8995 (fset 'read-string old)
8996 (fmakunbound 'read-string)))))))
8998 ((equal action ?S)
8999 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9000 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9001 (let ((days (read-number
9002 (format "Scatter tasks across how many %sdays: "
9003 (if arg "week" "")) 7)))
9004 (setq cmd
9005 `(let ((distance (1+ (random ,days))))
9006 (if arg
9007 (let ((dist distance)
9008 (day-of-week
9009 (calendar-day-of-week
9010 (calendar-gregorian-from-absolute (org-today)))))
9011 (dotimes (i (1+ dist))
9012 (while (member day-of-week org-agenda-weekend-days)
9013 (incf distance)
9014 (incf day-of-week)
9015 (if (= day-of-week 7)
9016 (setq day-of-week 0)))
9017 (incf day-of-week)
9018 (if (= day-of-week 7)
9019 (setq day-of-week 0)))))
9020 ;; silently fail when try to replan a sexp entry
9021 (condition-case nil
9022 (let* ((date (calendar-gregorian-from-absolute
9023 (+ (org-today) distance)))
9024 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9025 (nth 2 date))))
9026 (org-agenda-schedule nil time))
9027 (error nil)))))))
9029 ((assoc action org-agenda-bulk-custom-functions)
9030 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9031 redo-at-end t))
9033 ((equal action ?f)
9034 (setq cmd (list (intern
9035 (org-icompleting-read "Function: "
9036 obarray 'fboundp t nil nil)))))
9038 (t (error "Invalid bulk action")))
9040 ;; Sort the markers, to make sure that parents are handled before children
9041 (setq entries (sort entries
9042 (lambda (a b)
9043 (cond
9044 ((equal (marker-buffer a) (marker-buffer b))
9045 (< (marker-position a) (marker-position b)))
9047 (string< (buffer-name (marker-buffer a))
9048 (buffer-name (marker-buffer b))))))))
9050 ;; Now loop over all markers and apply cmd
9051 (while (setq e (pop entries))
9052 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9053 (if (not pos)
9054 (progn (message "Skipping removed entry at %s" e)
9055 (setq cntskip (1+ cntskip)))
9056 (goto-char pos)
9057 (let (org-loop-over-headlines-in-active-region)
9058 (eval cmd))
9059 (when (not org-agenda-persistent-marks)
9060 (setq org-agenda-bulk-marked-entries
9061 (delete e org-agenda-bulk-marked-entries)))
9062 (setq cnt (1+ cnt))))
9063 (when (not org-agenda-persistent-marks)
9064 (org-agenda-bulk-unmark-all))
9065 (when redo-at-end (org-agenda-redo))
9066 (message "Acted on %d entries%s%s"
9068 (if (= cntskip 0)
9070 (format ", skipped %d (disappeared before their turn)"
9071 cntskip))
9072 (if (not org-agenda-persistent-marks)
9073 "" " (kept marked)"))))))
9075 ;;; Flagging notes
9077 (defun org-agenda-show-the-flagging-note ()
9078 "Display the flagging note in the other window.
9079 When called a second time in direct sequence, offer to remove the FLAGGING
9080 tag and (if present) the flagging note."
9081 (interactive)
9082 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9083 (win (selected-window))
9084 note heading newhead)
9085 (unless hdmarker
9086 (error "No linked entry at point"))
9087 (if (and (eq this-command last-command)
9088 (y-or-n-p "Unflag and remove any flagging note? "))
9089 (progn
9090 (org-agenda-remove-flag hdmarker)
9091 (let ((win (get-buffer-window "*Flagging Note*")))
9092 (and win (delete-window win)))
9093 (message "Entry unflagged"))
9094 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9095 (unless note
9096 (error "No flagging note"))
9097 (org-kill-new note)
9098 (org-switch-to-buffer-other-window "*Flagging Note*")
9099 (erase-buffer)
9100 (insert note)
9101 (goto-char (point-min))
9102 (while (re-search-forward "\\\\n" nil t)
9103 (replace-match "\n" t t))
9104 (goto-char (point-min))
9105 (select-window win)
9106 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9108 (defun org-agenda-remove-flag (marker)
9109 "Remove the FLAGGED tag and any flagging note in the entry."
9110 (let (newhead)
9111 (org-with-point-at marker
9112 (org-toggle-tag "FLAGGED" 'off)
9113 (org-entry-delete nil "THEFLAGGINGNOTE")
9114 (setq newhead (org-get-heading)))
9115 (org-agenda-change-all-lines newhead marker)
9116 (message "Entry unflagged")))
9118 (defun org-agenda-get-any-marker (&optional pos)
9119 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9120 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9122 ;;; Appointment reminders
9124 (defvar appt-time-msg-list)
9126 ;;;###autoload
9127 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9128 "Activate appointments found in `org-agenda-files'.
9129 With a \\[universal-argument] prefix, refresh the list of
9130 appointments.
9132 If FILTER is t, interactively prompt the user for a regular
9133 expression, and filter out entries that don't match it.
9135 If FILTER is a string, use this string as a regular expression
9136 for filtering entries out.
9138 If FILTER is a function, filter out entries against which
9139 calling the function returns nil. This function takes one
9140 argument: an entry from `org-agenda-get-day-entries'.
9142 FILTER can also be an alist with the car of each cell being
9143 either 'headline or 'category. For example:
9145 '((headline \"IMPORTANT\")
9146 (category \"Work\"))
9148 will only add headlines containing IMPORTANT or headlines
9149 belonging to the \"Work\" category.
9151 ARGS are symbols indicating what kind of entries to consider.
9152 By default `org-agenda-to-appt' will use :deadline, :scheduled
9153 and :timestamp entries. See the docstring of `org-diary' for
9154 details and examples.
9156 If an entry as a APPT_WARNTIME property, its value will be used
9157 to override `appt-message-warning-time'."
9158 (interactive "P")
9159 (if refresh (setq appt-time-msg-list nil))
9160 (if (eq filter t)
9161 (setq filter (read-from-minibuffer "Regexp filter: ")))
9162 (let* ((cnt 0) ; count added events
9163 (scope (or args '(:deadline :scheduled :timestamp)))
9164 (org-agenda-new-buffers nil)
9165 (org-deadline-warning-days 0)
9166 ;; Do not use `org-today' here because appt only takes
9167 ;; time and without date as argument, so it may pass wrong
9168 ;; information otherwise
9169 (today (org-date-to-gregorian
9170 (time-to-days (current-time))))
9171 (org-agenda-restrict nil)
9172 (files (org-agenda-files 'unrestricted)) entries file
9173 (org-agenda-buffer nil))
9174 ;; Get all entries which may contain an appt
9175 (org-agenda-prepare-buffers files)
9176 (while (setq file (pop files))
9177 (setq entries
9178 (delq nil
9179 (append entries
9180 (apply 'org-agenda-get-day-entries
9181 file today scope)))))
9182 ;; Map thru entries and find if we should filter them out
9183 (mapc
9184 (lambda(x)
9185 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
9186 (cat (get-text-property 1 'org-category x))
9187 (tod (get-text-property 1 'time-of-day x))
9188 (ok (or (null filter)
9189 (and (stringp filter) (string-match filter evt))
9190 (and (functionp filter) (funcall filter x))
9191 (and (listp filter)
9192 (let ((cat-filter (cadr (assoc 'category filter)))
9193 (evt-filter (cadr (assoc 'headline filter))))
9194 (or (and (stringp cat-filter)
9195 (string-match cat-filter cat))
9196 (and (stringp evt-filter)
9197 (string-match evt-filter evt)))))))
9198 (wrn (get-text-property 1 'warntime x)))
9199 ;; FIXME: Shall we remove text-properties for the appt text?
9200 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
9201 (when (and ok tod)
9202 (setq tod (concat "00" (number-to-string tod))
9203 tod (when (string-match
9204 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
9205 (concat (match-string 1 tod) ":"
9206 (match-string 2 tod))))
9207 (if (version< emacs-version "23.3")
9208 (appt-add tod evt)
9209 (appt-add tod evt wrn))
9210 (setq cnt (1+ cnt))))) entries)
9211 (org-release-buffers org-agenda-new-buffers)
9212 (if (eq cnt 0)
9213 (message "No event to add")
9214 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
9216 (defun org-agenda-todayp (date)
9217 "Does DATE mean today, when considering `org-extend-today-until'?"
9218 (let ((today (org-today))
9219 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
9220 date)))
9221 (eq date today)))
9223 (defun org-agenda-todo-yesterday (&optional arg)
9224 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
9225 (interactive "P")
9226 (let* ((hour (third (decode-time
9227 (org-current-time))))
9228 (org-extend-today-until (1+ hour)))
9229 (org-agenda-todo arg)))
9231 (provide 'org-agenda)
9233 ;;; org-agenda.el ends here