Manually revert maint to e85080.
[org-mode.git] / lisp / org-agenda.el
blob1457356d55312c6b9e6ad988545399feb968985d
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (eval-when-compile
50 (require 'cl))
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-absolute-from-iso "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function calendar-check-holidays "holidays" (date))
71 (declare-function org-datetree-find-date-create "org-datetree"
72 (date &optional keep-restriction))
73 (declare-function org-columns-quit "org-colview" ())
74 (declare-function diary-date-display-form "diary-lib" (&optional type))
75 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
76 (declare-function org-habit-insert-consistency-graphs
77 "org-habit" (&optional line))
78 (declare-function org-is-habit-p "org-habit" (&optional pom))
79 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
80 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
81 (declare-function org-pop-to-buffer-same-window "org-compat"
82 (&optional buffer-or-name norecord label))
84 (defvar calendar-mode-map)
85 (defvar org-clock-current-task) ; defined in org-clock.el
86 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
87 (defvar org-habit-show-habits)
88 (defvar org-habit-show-habits-only-for-today)
90 ;; Defined somewhere in this file, but used before definition.
91 (defvar org-agenda-buffer-name)
92 (defvar org-agenda-overriding-header)
93 (defvar org-agenda-title-append nil)
94 (defvar entry)
95 (defvar date)
96 (defvar org-agenda-undo-list)
97 (defvar org-agenda-pending-undo-list)
98 (defvar original-date) ; dynamically scoped, calendar.el does scope this
100 (defcustom org-agenda-confirm-kill 1
101 "When set, remote killing from the agenda buffer needs confirmation.
102 When t, a confirmation is always needed. When a number N, confirmation is
103 only needed when the text to be killed contains more than N non-white lines."
104 :group 'org-agenda
105 :type '(choice
106 (const :tag "Never" nil)
107 (const :tag "Always" t)
108 (integer :tag "When more than N lines")))
110 (defcustom org-agenda-compact-blocks nil
111 "Non-nil means make the block agenda more compact.
112 This is done globally by leaving out lines like the agenda span
113 name and week number or the separator lines."
114 :group 'org-agenda
115 :type 'boolean)
117 (defcustom org-agenda-block-separator ?=
118 "The separator between blocks in the agenda.
119 If this is a string, it will be used as the separator, with a newline added.
120 If it is a character, it will be repeated to fill the window width.
121 If nil the separator is disabled. In `org-agenda-custom-commands' this
122 addresses the separator between the current and the previous block."
123 :group 'org-agenda
124 :type '(choice
125 (const :tag "Disabled" nil)
126 (character)
127 (string)))
129 (defgroup org-agenda-export nil
130 "Options concerning exporting agenda views in Org-mode."
131 :tag "Org Agenda Export"
132 :group 'org-agenda)
134 (defcustom org-agenda-with-colors t
135 "Non-nil means use colors in agenda views."
136 :group 'org-agenda-export
137 :type 'boolean)
139 (defcustom org-agenda-exporter-settings nil
140 "Alist of variable/value pairs that should be active during agenda export.
141 This is a good place to set options for ps-print and for htmlize.
142 Note that the way this is implemented, the values will be evaluated
143 before assigned to the variables. So make sure to quote values you do
144 *not* want evaluated, for example
146 (setq org-agenda-exporter-settings
147 '((ps-print-color-p 'black-white)))"
148 :group 'org-agenda-export
149 :type '(repeat
150 (list
151 (variable)
152 (sexp :tag "Value"))))
154 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
155 "Hook run in temporary buffer before writing it to an export file.
156 A useful function is `org-agenda-add-entry-text'."
157 :group 'org-agenda-export
158 :type 'hook
159 :options '(org-agenda-add-entry-text))
161 (defcustom org-agenda-add-entry-text-maxlines 0
162 "Maximum number of entry text lines to be added to agenda.
163 This is only relevant when `org-agenda-add-entry-text' is part of
164 `org-agenda-before-write-hook', which it is by default.
165 When this is 0, nothing will happen. When it is greater than 0, it
166 specifies the maximum number of lines that will be added for each entry
167 that is listed in the agenda view.
169 Note that this variable is not used during display, only when exporting
170 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
171 and `org-agenda-entry-text-maxlines'."
172 :group 'org-agenda
173 :type 'integer)
175 (defcustom org-agenda-add-entry-text-descriptive-links t
176 "Non-nil means export org-links as descriptive links in agenda added text.
177 This variable applies to the text added to the agenda when
178 `org-agenda-add-entry-text-maxlines' is larger than 0.
179 When this variable nil, the URL will (also) be shown."
180 :group 'org-agenda
181 :type 'boolean)
183 (defcustom org-agenda-export-html-style ""
184 "The style specification for exported HTML Agenda files.
185 If this variable contains a string, it will replace the default <style>
186 section as produced by `htmlize'.
187 Since there are different ways of setting style information, this variable
188 needs to contain the full HTML structure to provide a style, including the
189 surrounding HTML tags. The style specifications should include definitions
190 the fonts used by the agenda, here is an example:
192 <style type=\"text/css\">
193 p { font-weight: normal; color: gray; }
194 .org-agenda-structure {
195 font-size: 110%;
196 color: #003399;
197 font-weight: 600;
199 .org-todo {
200 color: #cc6666;
201 font-weight: bold;
203 .org-agenda-done {
204 color: #339933;
206 .org-done {
207 color: #339933;
209 .title { text-align: center; }
210 .todo, .deadline { color: red; }
211 .done { color: green; }
212 </style>
214 or, if you want to keep the style in a file,
216 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
218 As the value of this option simply gets inserted into the HTML <head> header,
219 you can \"misuse\" it to also add other text to the header. However,
220 <style>...</style> is required, if not present the variable will be ignored."
221 :group 'org-agenda-export
222 :group 'org-export-html
223 :type 'string)
225 (defcustom org-agenda-persistent-filter nil
226 "When set, keep filters from one agenda view to the next."
227 :group 'org-agenda
228 :type 'boolean)
230 (defgroup org-agenda-custom-commands nil
231 "Options concerning agenda views in Org-mode."
232 :tag "Org Agenda Custom Commands"
233 :group 'org-agenda)
235 (defconst org-sorting-choice
236 '(choice
237 (const time-up) (const time-down)
238 (const category-keep) (const category-up) (const category-down)
239 (const tag-down) (const tag-up)
240 (const priority-up) (const priority-down)
241 (const todo-state-up) (const todo-state-down)
242 (const effort-up) (const effort-down)
243 (const habit-up) (const habit-down)
244 (const alpha-up) (const alpha-down)
245 (const user-defined-up) (const user-defined-down))
246 "Sorting choices.")
248 ;; Keep custom values for `org-agenda-filter-preset' compatible with
249 ;; the new variable `org-agenda-tag-filter-preset'.
250 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
252 (defconst org-agenda-custom-commands-local-options
253 `(repeat :tag "Local settings for this command. Remember to quote values"
254 (choice :tag "Setting"
255 (list :tag "Heading for this block"
256 (const org-agenda-overriding-header)
257 (string :tag "Headline"))
258 (list :tag "Files to be searched"
259 (const org-agenda-files)
260 (list
261 (const :format "" quote)
262 (repeat (file))))
263 (list :tag "Sorting strategy"
264 (const org-agenda-sorting-strategy)
265 (list
266 (const :format "" quote)
267 (repeat
268 ,org-sorting-choice)))
269 (list :tag "Prefix format"
270 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
271 (string))
272 (list :tag "Number of days in agenda"
273 (const org-agenda-span)
274 (choice (const :tag "Day" 'day)
275 (const :tag "Week" 'week)
276 (const :tag "Month" 'month)
277 (const :tag "Year" 'year)
278 (integer :tag "Custom")))
279 (list :tag "Fixed starting date"
280 (const org-agenda-start-day)
281 (string :value "2007-11-01"))
282 (list :tag "Start on day of week"
283 (const org-agenda-start-on-weekday)
284 (choice :value 1
285 (const :tag "Today" nil)
286 (integer :tag "Weekday No.")))
287 (list :tag "Include data from diary"
288 (const org-agenda-include-diary)
289 (boolean))
290 (list :tag "Deadline Warning days"
291 (const org-deadline-warning-days)
292 (integer :value 1))
293 (list :tag "Category filter preset"
294 (const org-agenda-category-filter-preset)
295 (list
296 (const :format "" quote)
297 (repeat
298 (string :tag "+category or -category"))))
299 (list :tag "Tags filter preset"
300 (const org-agenda-tag-filter-preset)
301 (list
302 (const :format "" quote)
303 (repeat
304 (string :tag "+tag or -tag"))))
305 (list :tag "Set daily/weekly entry types"
306 (const org-agenda-entry-types)
307 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
308 (const :deadline)
309 (const :scheduled)
310 (const :timestamp)
311 (const :sexp)))
312 (list :tag "Standard skipping condition"
313 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
314 (const org-agenda-skip-function)
315 (list
316 (const :format "" quote)
317 (list
318 (choice
319 :tag "Skipping range"
320 (const :tag "Skip entry" org-agenda-skip-entry-if)
321 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
322 (repeat :inline t :tag "Conditions for skipping"
323 (choice
324 :tag "Condition type"
325 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
326 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
327 (list :tag "TODO state is" :inline t
328 (const 'todo)
329 (choice
330 (const :tag "any not-done state" 'todo)
331 (const :tag "any done state" 'done)
332 (const :tag "any state" 'any)
333 (list :tag "Keyword list"
334 (const :format "" quote)
335 (repeat (string :tag "Keyword")))))
336 (list :tag "TODO state is not" :inline t
337 (const 'nottodo)
338 (choice
339 (const :tag "any not-done state" 'todo)
340 (const :tag "any done state" 'done)
341 (const :tag "any state" 'any)
342 (list :tag "Keyword list"
343 (const :format "" quote)
344 (repeat (string :tag "Keyword")))))
345 (const :tag "scheduled" 'scheduled)
346 (const :tag "not scheduled" 'notscheduled)
347 (const :tag "deadline" 'deadline)
348 (const :tag "no deadline" 'notdeadline)
349 (const :tag "timestamp" 'timestamp)
350 (const :tag "no timestamp" 'nottimestamp))))))
351 (list :tag "Non-standard skipping condition"
352 :value (org-agenda-skip-function)
353 (const org-agenda-skip-function)
354 (sexp :tag "Function or form (quoted!)"))
355 (list :tag "Any variable"
356 (variable :tag "Variable")
357 (sexp :tag "Value (sexp)"))))
358 "Selection of examples for agenda command settings.
359 This will be spliced into the custom type of
360 `org-agenda-custom-commands'.")
363 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
364 ((agenda "") (alltodo))))
365 "Custom commands for the agenda.
366 These commands will be offered on the splash screen displayed by the
367 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
369 (key desc type match settings files)
371 key The key (one or more characters as a string) to be associated
372 with the command.
373 desc A description of the command, when omitted or nil, a default
374 description is built using MATCH.
375 type The command type, any of the following symbols:
376 agenda The daily/weekly agenda.
377 todo Entries with a specific TODO keyword, in all agenda files.
378 search Entries containing search words entry or headline.
379 tags Tags/Property/TODO match in all agenda files.
380 tags-todo Tags/P/T match in all agenda files, TODO entries only.
381 todo-tree Sparse tree of specific TODO keyword in *current* file.
382 tags-tree Sparse tree with all tags matches in *current* file.
383 occur-tree Occur sparse tree for *current* file.
384 ... A user-defined function.
385 match What to search for:
386 - a single keyword for TODO keyword searches
387 - a tags match expression for tags searches
388 - a word search expression for text searches.
389 - a regular expression for occur searches
390 For all other commands, this should be the empty string.
391 settings A list of option settings, similar to that in a let form, so like
392 this: ((opt1 val1) (opt2 val2) ...). The values will be
393 evaluated at the moment of execution, so quote them when needed.
394 files A list of files file to write the produced agenda buffer to
395 with the command `org-store-agenda-views'.
396 If a file name ends in \".html\", an HTML version of the buffer
397 is written out. If it ends in \".ps\", a postscript version is
398 produced. Otherwise, only the plain text is written to the file.
400 You can also define a set of commands, to create a composite agenda buffer.
401 In this case, an entry looks like this:
403 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
405 where
407 desc A description string to be displayed in the dispatcher menu.
408 cmd An agenda command, similar to the above. However, tree commands
409 are not allowed, but instead you can get agenda and global todo list.
410 So valid commands for a set are:
411 (agenda \"\" settings)
412 (alltodo \"\" settings)
413 (stuck \"\" settings)
414 (todo \"match\" settings files)
415 (search \"match\" settings files)
416 (tags \"match\" settings files)
417 (tags-todo \"match\" settings files)
419 Each command can carry a list of options, and another set of options can be
420 given for the whole set of commands. Individual command options take
421 precedence over the general options.
423 When using several characters as key to a command, the first characters
424 are prefix commands. For the dispatcher to display useful information, you
425 should provide a description for the prefix, like
427 (setq org-agenda-custom-commands
428 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
429 (\"hl\" tags \"+HOME+Lisa\")
430 (\"hp\" tags \"+HOME+Peter\")
431 (\"hk\" tags \"+HOME+Kim\")))"
432 :group 'org-agenda-custom-commands
433 :type `(repeat
434 (choice :value ("x" "Describe command here" tags "" nil)
435 (list :tag "Single command"
436 (string :tag "Access Key(s) ")
437 (option (string :tag "Description"))
438 (choice
439 (const :tag "Agenda" agenda)
440 (const :tag "TODO list" alltodo)
441 (const :tag "Search words" search)
442 (const :tag "Stuck projects" stuck)
443 (const :tag "Tags/Property match (all agenda files)" tags)
444 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
445 (const :tag "TODO keyword search (all agenda files)" todo)
446 (const :tag "Tags sparse tree (current buffer)" tags-tree)
447 (const :tag "TODO keyword tree (current buffer)" todo-tree)
448 (const :tag "Occur tree (current buffer)" occur-tree)
449 (sexp :tag "Other, user-defined function"))
450 (string :tag "Match (only for some commands)")
451 ,org-agenda-custom-commands-local-options
452 (option (repeat :tag "Export" (file :tag "Export to"))))
453 (list :tag "Command series, all agenda files"
454 (string :tag "Access Key(s)")
455 (string :tag "Description ")
456 (repeat :tag "Component"
457 (choice
458 (list :tag "Agenda"
459 (const :format "" agenda)
460 (const :tag "" :format "" "")
461 ,org-agenda-custom-commands-local-options)
462 (list :tag "TODO list (all keywords)"
463 (const :format "" alltodo)
464 (const :tag "" :format "" "")
465 ,org-agenda-custom-commands-local-options)
466 (list :tag "Search words"
467 (const :format "" search)
468 (string :tag "Match")
469 ,org-agenda-custom-commands-local-options)
470 (list :tag "Stuck projects"
471 (const :format "" stuck)
472 (const :tag "" :format "" "")
473 ,org-agenda-custom-commands-local-options)
474 (list :tag "Tags search"
475 (const :format "" tags)
476 (string :tag "Match")
477 ,org-agenda-custom-commands-local-options)
478 (list :tag "Tags search, TODO entries only"
479 (const :format "" tags-todo)
480 (string :tag "Match")
481 ,org-agenda-custom-commands-local-options)
482 (list :tag "TODO keyword search"
483 (const :format "" todo)
484 (string :tag "Match")
485 ,org-agenda-custom-commands-local-options)
486 (list :tag "Other, user-defined function"
487 (symbol :tag "function")
488 (string :tag "Match")
489 ,org-agenda-custom-commands-local-options)))
491 (repeat :tag "Settings for entire command set"
492 (list (variable :tag "Any variable")
493 (sexp :tag "Value")))
494 (option (repeat :tag "Export" (file :tag "Export to"))))
495 (cons :tag "Prefix key documentation"
496 (string :tag "Access Key(s)")
497 (string :tag "Description ")))))
499 (defcustom org-agenda-query-register ?o
500 "The register holding the current query string.
501 The purpose of this is that if you construct a query string interactively,
502 you can then use it to define a custom command."
503 :group 'org-agenda-custom-commands
504 :type 'character)
506 (defcustom org-stuck-projects
507 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
508 "How to identify stuck projects.
509 This is a list of four items:
510 1. A tags/todo/property matcher string that is used to identify a project.
511 See the manual for a description of tag and property searches.
512 The entire tree below a headline matched by this is considered one project.
513 2. A list of TODO keywords identifying non-stuck projects.
514 If the project subtree contains any headline with one of these todo
515 keywords, the project is considered to be not stuck. If you specify
516 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
517 3. A list of tags identifying non-stuck projects.
518 If the project subtree contains any headline with one of these tags,
519 the project is considered to be not stuck. If you specify \"*\" as
520 a tag, any tag will mark the project unstuck. Note that this is about
521 the explicit presence of a tag somewhere in the subtree, inherited
522 tags to not count here. If inherited tags make a project not stuck,
523 use \"-TAG\" in the tags part of the matcher under (1.) above.
524 4. An arbitrary regular expression matching non-stuck projects.
526 If the project turns out to be not stuck, search continues also in the
527 subtree to see if any of the subtasks have project status.
529 See also the variable `org-tags-match-list-sublevels' which applies
530 to projects matched by this search as well.
532 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
533 or `C-c a #' to produce the list."
534 :group 'org-agenda-custom-commands
535 :type '(list
536 (string :tag "Tags/TODO match to identify a project")
537 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
538 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
539 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
541 (defcustom org-agenda-filter-effort-default-operator "<"
542 "The default operator for effort estimate filtering.
543 If you select an effort estimate limit without first pressing an operator,
544 this one will be used."
545 :group 'org-agenda-custom-commands
546 :type '(choice (const :tag "less or equal" "<")
547 (const :tag "greater or equal"">")
548 (const :tag "equal" "=")))
550 (defgroup org-agenda-skip nil
551 "Options concerning skipping parts of agenda files."
552 :tag "Org Agenda Skip"
553 :group 'org-agenda)
555 (defcustom org-agenda-skip-function-global nil
556 "Function to be called at each match during agenda construction.
557 If this function returns nil, the current match should not be skipped.
558 If the function decided to skip an agenda match, is must return the
559 buffer position from which the search should be continued.
560 This may also be a Lisp form, which will be evaluated.
562 This variable will be applied to every agenda match, including
563 tags/property searches and TODO lists. So try to make the test function
564 do its checking as efficiently as possible. To implement a skipping
565 condition just for specific agenda commands, use the variable
566 `org-agenda-skip-function' which can be set in the options section
567 of custom agenda commands."
568 :group 'org-agenda-skip
569 :type 'sexp)
571 (defgroup org-agenda-daily/weekly nil
572 "Options concerning the daily/weekly agenda."
573 :tag "Org Agenda Daily/Weekly"
574 :group 'org-agenda)
575 (defgroup org-agenda-todo-list nil
576 "Options concerning the global todo list agenda view."
577 :tag "Org Agenda Todo List"
578 :group 'org-agenda)
579 (defgroup org-agenda-match-view nil
580 "Options concerning the general tags/property/todo match agenda view."
581 :tag "Org Agenda Match View"
582 :group 'org-agenda)
583 (defgroup org-agenda-search-view nil
584 "Options concerning the general tags/property/todo match agenda view."
585 :tag "Org Agenda Match View"
586 :group 'org-agenda)
588 (defvar org-agenda-archives-mode nil
589 "Non-nil means the agenda will include archived items.
590 If this is the symbol `trees', trees in the selected agenda scope
591 that are marked with the ARCHIVE tag will be included anyway. When this is
592 t, also all archive files associated with the current selection of agenda
593 files will be included.")
595 (defcustom org-agenda-skip-comment-trees t
596 "Non-nil means skip trees that start with the COMMENT keyword.
597 When nil, these trees are also scanned by agenda commands."
598 :group 'org-agenda-skip
599 :type 'boolean)
601 (defcustom org-agenda-todo-list-sublevels t
602 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
603 When nil, the sublevels of a TODO entry are not checked, resulting in
604 potentially much shorter TODO lists."
605 :group 'org-agenda-skip
606 :group 'org-agenda-todo-list
607 :type 'boolean)
609 (defcustom org-agenda-todo-ignore-with-date nil
610 "Non-nil means don't show entries with a date in the global todo list.
611 You can use this if you prefer to mark mere appointments with a TODO keyword,
612 but don't want them to show up in the TODO list.
613 When this is set, it also covers deadlines and scheduled items, the settings
614 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
615 will be ignored.
616 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
617 :group 'org-agenda-skip
618 :group 'org-agenda-todo-list
619 :type 'boolean)
621 (defcustom org-agenda-todo-ignore-timestamp nil
622 "Non-nil means don't show entries with a timestamp.
623 This applies when creating the global todo list.
624 Valid values are:
626 past Don't show entries for today or in the past.
628 future Don't show entries with a timestamp in the future.
629 The idea behind this is that if it has a future
630 timestamp, you don't want to think about it until the
631 date.
633 all Don't show any entries with a timestamp in the global todo list.
634 The idea behind this is that by setting a timestamp, you
635 have already \"taken care\" of this item.
637 This variable can also have an integer as a value. If positive (N),
638 todos with a timestamp N or more days in the future will be ignored. If
639 negative (-N), todos with a timestamp N or more days in the past will be
640 ignored. If 0, todos with a timestamp either today or in the future will
641 be ignored. For example, a value of -1 will exclude todos with a
642 timestamp in the past (yesterday or earlier), while a value of 7 will
643 exclude todos with a timestamp a week or more in the future.
645 See also `org-agenda-todo-ignore-with-date'.
646 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
647 to make his option also apply to the tags-todo list."
648 :group 'org-agenda-skip
649 :group 'org-agenda-todo-list
650 :type '(choice
651 (const :tag "Ignore future timestamp todos" future)
652 (const :tag "Ignore past or present timestamp todos" past)
653 (const :tag "Ignore all timestamp todos" all)
654 (const :tag "Show timestamp todos" nil)
655 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
657 (defcustom org-agenda-todo-ignore-scheduled nil
658 "Non-nil means, ignore some scheduled TODO items when making TODO list.
659 This applies when creating the global todo list.
660 Valid values are:
662 past Don't show entries scheduled today or in the past.
664 future Don't show entries scheduled in the future.
665 The idea behind this is that by scheduling it, you don't want to
666 think about it until the scheduled date.
668 all Don't show any scheduled entries in the global todo list.
669 The idea behind this is that by scheduling it, you have already
670 \"taken care\" of this item.
672 t Same as `all', for backward compatibility.
674 This variable can also have an integer as a value. See
675 `org-agenda-todo-ignore-timestamp' for more details.
677 See also `org-agenda-todo-ignore-with-date'.
678 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
679 to make his option also apply to the tags-todo list."
680 :group 'org-agenda-skip
681 :group 'org-agenda-todo-list
682 :type '(choice
683 (const :tag "Ignore future-scheduled todos" future)
684 (const :tag "Ignore past- or present-scheduled todos" past)
685 (const :tag "Ignore all scheduled todos" all)
686 (const :tag "Ignore all scheduled todos (compatibility)" t)
687 (const :tag "Show scheduled todos" nil)
688 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
690 (defcustom org-agenda-todo-ignore-deadlines nil
691 "Non-nil means ignore some deadlined TODO items when making TODO list.
692 There are different motivations for using different values, please think
693 carefully when configuring this variable.
695 This applies when creating the global todo list.
696 Valid values are:
698 near Don't show near deadline entries. A deadline is near when it is
699 closer than `org-deadline-warning-days' days. The idea behind this
700 is that such items will appear in the agenda anyway.
702 far Don't show TODO entries where a deadline has been defined, but
703 the deadline is not near. This is useful if you don't want to
704 use the todo list to figure out what to do now.
706 past Don't show entries with a deadline timestamp for today or in the past.
708 future Don't show entries with a deadline timestamp in the future, not even
709 when they become `near' ones. Use it with caution.
711 all Ignore all TODO entries that do have a deadline.
713 t Same as `near', for backward compatibility.
715 This variable can also have an integer as a value. See
716 `org-agenda-todo-ignore-timestamp' for more details.
718 See also `org-agenda-todo-ignore-with-date'.
719 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
720 to make his option also apply to the tags-todo list."
721 :group 'org-agenda-skip
722 :group 'org-agenda-todo-list
723 :type '(choice
724 (const :tag "Ignore near deadlines" near)
725 (const :tag "Ignore near deadlines (compatibility)" t)
726 (const :tag "Ignore far deadlines" far)
727 (const :tag "Ignore all TODOs with a deadlines" all)
728 (const :tag "Show all TODOs, even if they have a deadline" nil)
729 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
731 (defcustom org-agenda-tags-todo-honor-ignore-options nil
732 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
733 The variables
734 `org-agenda-todo-ignore-with-date',
735 `org-agenda-todo-ignore-timestamp',
736 `org-agenda-todo-ignore-scheduled',
737 `org-agenda-todo-ignore-deadlines'
738 make the global TODO list skip entries that have time stamps of certain
739 kinds. If this option is set, the same options will also apply for the
740 tags-todo search, which is the general tags/property matcher
741 restricted to unfinished TODO entries only."
742 :group 'org-agenda-skip
743 :group 'org-agenda-todo-list
744 :group 'org-agenda-match-view
745 :type 'boolean)
747 (defcustom org-agenda-skip-scheduled-if-done nil
748 "Non-nil means don't show scheduled items in agenda when they are done.
749 This is relevant for the daily/weekly agenda, not for the TODO list. And
750 it applies only to the actual date of the scheduling. Warnings about
751 an item with a past scheduling dates are always turned off when the item
752 is DONE."
753 :group 'org-agenda-skip
754 :group 'org-agenda-daily/weekly
755 :type 'boolean)
757 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
758 "Non-nil means skip scheduling line if same entry shows because of deadline.
759 In the agenda of today, an entry can show up multiple times because
760 it is both scheduled and has a nearby deadline, and maybe a plain time
761 stamp as well.
762 When this variable is t, then only the deadline is shown and the fact that
763 the entry is scheduled today or was scheduled previously is not shown.
764 When this variable is nil, the entry will be shown several times. When
765 the variable is the symbol `not-today', then skip scheduled previously,
766 but not scheduled today."
767 :group 'org-agenda-skip
768 :group 'org-agenda-daily/weekly
769 :type '(choice
770 (const :tag "Never" nil)
771 (const :tag "Always" t)
772 (const :tag "Not when scheduled today" not-today)))
774 (defcustom org-agenda-skip-deadline-if-done nil
775 "Non-nil means don't show deadlines when the corresponding item is done.
776 When nil, the deadline is still shown and should give you a happy feeling.
777 This is relevant for the daily/weekly agenda. And it applied only to the
778 actually date of the deadline. Warnings about approaching and past-due
779 deadlines are always turned off when the item is DONE."
780 :group 'org-agenda-skip
781 :group 'org-agenda-daily/weekly
782 :type 'boolean)
784 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
785 "Non-nil means skip deadline prewarning when entry is also scheduled.
786 This will apply on all days where a prewarning for the deadline would
787 be shown, but not at the day when the entry is actually due. On that day,
788 the deadline will be shown anyway.
789 This variable may be set to nil, t, or a number which will then give
790 the number of days before the actual deadline when the prewarnings
791 should resume.
792 This can be used in a workflow where the first showing of the deadline will
793 trigger you to schedule it, and then you don't want to be reminded of it
794 because you will take care of it on the day when scheduled."
795 :group 'org-agenda-skip
796 :group 'org-agenda-daily/weekly
797 :type '(choice
798 (const :tag "Alwas show prewarning" nil)
799 (const :tag "Remove prewarning if entry is scheduled" t)
800 (integer :tag "Restart prewarning N days before deadline")))
802 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
803 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
804 When non-nil, after the search for timestamps has matched once in an
805 entry, the rest of the entry will not be searched."
806 :group 'org-agenda-skip
807 :type 'boolean)
809 (defcustom org-agenda-skip-timestamp-if-done nil
810 "Non-nil means don't select item by timestamp or -range if it is DONE."
811 :group 'org-agenda-skip
812 :group 'org-agenda-daily/weekly
813 :type 'boolean)
815 (defcustom org-agenda-dim-blocked-tasks t
816 "Non-nil means dim blocked tasks in the agenda display.
817 This causes some overhead during agenda construction, but if you
818 have turned on `org-enforce-todo-dependencies',
819 `org-enforce-todo-checkbox-dependencies', or any other blocking
820 mechanism, this will create useful feedback in the agenda.
822 Instead of t, this variable can also have the value `invisible'.
823 Then blocked tasks will be invisible and only become visible when
824 they become unblocked. An exemption to this behavior is when a task is
825 blocked because of unchecked checkboxes below it. Since checkboxes do
826 not show up in the agenda views, making this task invisible you remove any
827 trace from agenda views that there is something to do. Therefore, a task
828 that is blocked because of checkboxes will never be made invisible, it
829 will only be dimmed."
830 :group 'org-agenda-daily/weekly
831 :group 'org-agenda-todo-list
832 :type '(choice
833 (const :tag "Do not dim" nil)
834 (const :tag "Dim to a gray face" t)
835 (const :tag "Make invisible" invisible)))
837 (defcustom org-timeline-show-empty-dates 3
838 "Non-nil means `org-timeline' also shows dates without an entry.
839 When nil, only the days which actually have entries are shown.
840 When t, all days between the first and the last date are shown.
841 When an integer, show also empty dates, but if there is a gap of more than
842 N days, just insert a special line indicating the size of the gap."
843 :group 'org-agenda-skip
844 :type '(choice
845 (const :tag "None" nil)
846 (const :tag "All" t)
847 (integer :tag "at most")))
849 (defgroup org-agenda-startup nil
850 "Options concerning initial settings in the Agenda in Org Mode."
851 :tag "Org Agenda Startup"
852 :group 'org-agenda)
854 (defcustom org-agenda-menu-show-matcher t
855 "Non-nil means show the match string in the agenda dispatcher menu.
856 When nil, the matcher string is not shown, but is put into the help-echo
857 property so than moving the mouse over the command shows it.
858 Setting it to nil is good if matcher strings are very long and/or if
859 you want to use two-column display (see `org-agenda-menu-two-column')."
860 :group 'org-agenda
861 :type 'boolean)
863 (defcustom org-agenda-menu-two-column nil
864 "Non-nil means, use two columns to show custom commands in the dispatcher.
865 If you use this, you probably want to set `org-agenda-menu-show-matcher'
866 to nil."
867 :group 'org-agenda
868 :type 'boolean)
870 (defcustom org-finalize-agenda-hook nil
871 "Hook run just before displaying an agenda buffer."
872 :group 'org-agenda-startup
873 :type 'hook)
875 (defcustom org-agenda-mouse-1-follows-link nil
876 "Non-nil means mouse-1 on a link will follow the link in the agenda.
877 A longer mouse click will still set point. Does not work on XEmacs.
878 Needs to be set before org.el is loaded."
879 :group 'org-agenda-startup
880 :type 'boolean)
882 (defcustom org-agenda-start-with-follow-mode nil
883 "The initial value of follow mode in a newly created agenda window."
884 :group 'org-agenda-startup
885 :type 'boolean)
887 (defcustom org-agenda-follow-indirect nil
888 "Non-nil means `org-agenda-follow-mode' displays only the
889 current item's tree, in an indirect buffer."
890 :group 'org-agenda
891 :type 'boolean)
893 (defcustom org-agenda-show-outline-path t
894 "Non-nil means show outline path in echo area after line motion."
895 :group 'org-agenda-startup
896 :type 'boolean)
898 (defcustom org-agenda-start-with-entry-text-mode nil
899 "The initial value of entry-text-mode in a newly created agenda window."
900 :group 'org-agenda-startup
901 :type 'boolean)
903 (defcustom org-agenda-entry-text-maxlines 5
904 "Number of text lines to be added when `E' is pressed in the agenda.
906 Note that this variable only used during agenda display. Add add entry text
907 when exporting the agenda, configure the variable
908 `org-agenda-add-entry-ext-maxlines'."
909 :group 'org-agenda
910 :type 'integer)
912 (defcustom org-agenda-entry-text-exclude-regexps nil
913 "List of regular expressions to clean up entry text.
914 The complete matches of all regular expressions in this list will be
915 removed from entry text before it is shown in the agenda."
916 :group 'org-agenda
917 :type '(repeat (regexp)))
919 (defvar org-agenda-entry-text-cleanup-hook nil
920 "Hook that is run after basic cleanup of entry text to be shown in agenda.
921 This cleanup is done in a temporary buffer, so the function may inspect and
922 change the entire buffer.
923 Some default stuff like drawers and scheduling/deadline dates will already
924 have been removed when this is called, as will any matches for regular
925 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
927 (defvar org-agenda-include-inactive-timestamps nil
928 "Non-nil means include inactive time stamps in agenda and timeline.")
930 (defgroup org-agenda-windows nil
931 "Options concerning the windows used by the Agenda in Org Mode."
932 :tag "Org Agenda Windows"
933 :group 'org-agenda)
935 (defcustom org-agenda-window-setup 'reorganize-frame
936 "How the agenda buffer should be displayed.
937 Possible values for this option are:
939 current-window Show agenda in the current window, keeping all other windows.
940 other-window Use `switch-to-buffer-other-window' to display agenda.
941 reorganize-frame Show only two windows on the current frame, the current
942 window and the agenda.
943 other-frame Use `switch-to-buffer-other-frame' to display agenda.
944 Also, when exiting the agenda, kill that frame.
945 See also the variable `org-agenda-restore-windows-after-quit'."
946 :group 'org-agenda-windows
947 :type '(choice
948 (const current-window)
949 (const other-frame)
950 (const other-window)
951 (const reorganize-frame)))
953 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
954 "The min and max height of the agenda window as a fraction of frame height.
955 The value of the variable is a cons cell with two numbers between 0 and 1.
956 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
957 :group 'org-agenda-windows
958 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
960 (defcustom org-agenda-restore-windows-after-quit nil
961 "Non-nil means restore window configuration upon exiting agenda.
962 Before the window configuration is changed for displaying the agenda,
963 the current status is recorded. When the agenda is exited with
964 `q' or `x' and this option is set, the old state is restored. If
965 `org-agenda-window-setup' is `other-frame', the value of this
966 option will be ignored."
967 :group 'org-agenda-windows
968 :type 'boolean)
970 (defcustom org-agenda-ndays nil
971 "Number of days to include in overview display.
972 Should be 1 or 7.
973 Obsolete, see `org-agenda-span'."
974 :group 'org-agenda-daily/weekly
975 :type 'integer)
977 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
979 (defcustom org-agenda-span 'week
980 "Number of days to include in overview display.
981 Can be day, week, month, year, or any number of days.
982 Custom commands can set this variable in the options section."
983 :group 'org-agenda-daily/weekly
984 :type '(choice (const :tag "Day" day)
985 (const :tag "Week" week)
986 (const :tag "Month" month)
987 (const :tag "Year" year)
988 (integer :tag "Custom")))
990 (defcustom org-agenda-start-on-weekday 1
991 "Non-nil means start the overview always on the specified weekday.
992 0 denotes Sunday, 1 denotes Monday etc.
993 When nil, always start on the current day.
994 Custom commands can set this variable in the options section."
995 :group 'org-agenda-daily/weekly
996 :type '(choice (const :tag "Today" nil)
997 (integer :tag "Weekday No.")))
999 (defcustom org-agenda-show-all-dates t
1000 "Non-nil means `org-agenda' shows every day in the selected range.
1001 When nil, only the days which actually have entries are shown."
1002 :group 'org-agenda-daily/weekly
1003 :type 'boolean)
1005 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1006 "Format string for displaying dates in the agenda.
1007 Used by the daily/weekly agenda and by the timeline. This should be
1008 a format string understood by `format-time-string', or a function returning
1009 the formatted date as a string. The function must take a single argument,
1010 a calendar-style date list like (month day year)."
1011 :group 'org-agenda-daily/weekly
1012 :type '(choice
1013 (string :tag "Format string")
1014 (function :tag "Function")))
1016 (defun org-agenda-format-date-aligned (date)
1017 "Format a date string for display in the daily/weekly agenda, or timeline.
1018 This function makes sure that dates are aligned for easy reading."
1019 (require 'cal-iso)
1020 (let* ((dayname (calendar-day-name date))
1021 (day (cadr date))
1022 (day-of-week (calendar-day-of-week date))
1023 (month (car date))
1024 (monthname (calendar-month-name month))
1025 (year (nth 2 date))
1026 (iso-week (org-days-to-iso-week
1027 (calendar-absolute-from-gregorian date)))
1028 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1029 (1- year))
1030 ((and (= month 12) (<= iso-week 1))
1031 (1+ year))
1032 (t year)))
1033 (weekstring (if (= day-of-week 1)
1034 (format " W%02d" iso-week)
1035 "")))
1036 (format "%-10s %2d %s %4d%s"
1037 dayname day monthname year weekstring)))
1039 (defcustom org-agenda-time-leading-zero nil
1040 "Non-nil means use leading zero for military times in agenda.
1041 For example, 9:30am would become 09:30 rather than 9:30."
1042 :group 'org-agenda-daily/weekly
1043 :type 'boolean)
1045 (defcustom org-agenda-timegrid-use-ampm nil
1046 "When set, show AM/PM style timestamps on the timegrid."
1047 :group 'org-agenda
1048 :type 'boolean)
1050 (defun org-agenda-time-of-day-to-ampm (time)
1051 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1052 (let* ((hour-number (string-to-number (substring time 0 -3)))
1053 (minute (substring time -2))
1054 (ampm "am"))
1055 (cond
1056 ((equal hour-number 12)
1057 (setq ampm "pm"))
1058 ((> hour-number 12)
1059 (setq ampm "pm")
1060 (setq hour-number (- hour-number 12))))
1061 (concat
1062 (if org-agenda-time-leading-zero
1063 (format "%02d" hour-number)
1064 (format "%02s" (number-to-string hour-number)))
1065 ":" minute ampm)))
1067 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1068 "Conditionally convert TIME to AM/PM format
1069 based on `org-agenda-timegrid-use-ampm'"
1070 (if org-agenda-timegrid-use-ampm
1071 (org-agenda-time-of-day-to-ampm time)
1072 time))
1074 (defcustom org-agenda-weekend-days '(6 0)
1075 "Which days are weekend?
1076 These days get the special face `org-agenda-date-weekend' in the agenda
1077 and timeline buffers."
1078 :group 'org-agenda-daily/weekly
1079 :type '(set :greedy t
1080 (const :tag "Monday" 1)
1081 (const :tag "Tuesday" 2)
1082 (const :tag "Wednesday" 3)
1083 (const :tag "Thursday" 4)
1084 (const :tag "Friday" 5)
1085 (const :tag "Saturday" 6)
1086 (const :tag "Sunday" 0)))
1088 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1089 "Non-nil means jump to today when moving a past date forward in time.
1090 When using S-right in the agenda to move a a date forward, and the date
1091 stamp currently points to the past, the first key press will move it
1092 to today. WHen nil, just move one day forward even if the date stays
1093 in the past."
1094 :group 'org-agenda-daily/weekly
1095 :type 'boolean)
1097 (defcustom org-agenda-include-diary nil
1098 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1099 Custom commands can set this variable in the options section."
1100 :group 'org-agenda-daily/weekly
1101 :type 'boolean)
1103 (defcustom org-agenda-include-deadlines t
1104 "If non-nil, include entries within their deadline warning period.
1105 Custom commands can set this variable in the options section."
1106 :group 'org-agenda-daily/weekly
1107 :type 'boolean)
1109 (defcustom org-agenda-repeating-timestamp-show-all t
1110 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1111 When set to a list of strings, only show occurrences of repeating
1112 stamps for these TODO keywords. When nil, only one occurrence is
1113 shown, either today or the nearest into the future."
1114 :group 'org-agenda-daily/weekly
1115 :type '(choice
1116 (const :tag "Show repeating stamps" t)
1117 (repeat :tag "Show repeating stamps for these TODO keywords"
1118 (string :tag "TODO Keyword"))
1119 (const :tag "Don't show repeating stamps" nil)))
1121 (defcustom org-scheduled-past-days 10000
1122 "No. of days to continue listing scheduled items that are not marked DONE.
1123 When an item is scheduled on a date, it shows up in the agenda on this
1124 day and will be listed until it is marked done for the number of days
1125 given here."
1126 :group 'org-agenda-daily/weekly
1127 :type 'integer)
1129 (defcustom org-agenda-log-mode-items '(closed clock)
1130 "List of items that should be shown in agenda log mode.
1131 This list may contain the following symbols:
1133 closed Show entries that have been closed on that day.
1134 clock Show entries that have received clocked time on that day.
1135 state Show all logged state changes.
1136 Note that instead of changing this variable, you can also press `C-u l' in
1137 the agenda to display all available LOG items temporarily."
1138 :group 'org-agenda-daily/weekly
1139 :type '(set :greedy t (const closed) (const clock) (const state)))
1141 (defcustom org-agenda-clock-consistency-checks
1142 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1143 :gap-ok-around ("4:00")
1144 :default-face ((:background "DarkRed") (:foreground "white"))
1145 :overlap-face nil :gap-face nil :no-end-time-face nil
1146 :long-face nil :short-face nil)
1147 "This is a property list, with the following keys:
1149 :max-duration Mark clocking chunks that are longer than this time.
1150 This is a time string like \"HH:MM\", or the number
1151 of minutes as an integer.
1153 :min-duration Mark clocking chunks that are shorter that this.
1154 This is a time string like \"HH:MM\", or the number
1155 of minutes as an integer.
1157 :max-gap Mark gaps between clocking chunks that are longer than
1158 this duration. A number of minutes, or a string
1159 like \"HH:MM\".
1161 :gap-ok-around List of times during the day which are usually not working
1162 times. When a gap is detected, but the gap contains any
1163 of these times, the gap is *not* reported. For example,
1164 if this is (\"4:00\" \"13:00\") then gaps that contain
1165 4:00 in the morning (i.e. the night) and 13:00
1166 (i.e. a typical lunch time) do not cause a warning.
1167 You should have at least one time during the night in this
1168 list, or otherwise the first task each morning will trigger
1169 a warning because it follows a long gap.
1171 Furthermore, the following properties can be used to define faces for
1172 issue display.
1174 :default-face the default face, if the specific face is undefined
1175 :overlap-face face for overlapping clocks
1176 :gap-face face for gaps between clocks
1177 :no-end-time-face face for incomplete clocks
1178 :long-face face for clock intervals that are too long
1179 :short-face face for clock intervals that are too short"
1180 :group 'org-agenda-daily/weekly
1181 :group 'org-clock
1182 :type 'plist)
1184 (defcustom org-agenda-log-mode-add-notes t
1185 "Non-nil means add first line of notes to log entries in agenda views.
1186 If a log item like a state change or a clock entry is associated with
1187 notes, the first line of these notes will be added to the entry in the
1188 agenda display."
1189 :group 'org-agenda-daily/weekly
1190 :type 'boolean)
1192 (defcustom org-agenda-start-with-log-mode nil
1193 "The initial value of log-mode in a newly created agenda window."
1194 :group 'org-agenda-startup
1195 :group 'org-agenda-daily/weekly
1196 :type 'boolean)
1198 (defcustom org-agenda-start-with-clockreport-mode nil
1199 "The initial value of clockreport-mode in a newly created agenda window."
1200 :group 'org-agenda-startup
1201 :group 'org-agenda-daily/weekly
1202 :type 'boolean)
1204 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1205 "Property list with parameters for the clocktable in clockreport mode.
1206 This is the display mode that shows a clock table in the daily/weekly
1207 agenda, the properties for this dynamic block can be set here.
1208 The usual clocktable parameters are allowed here, but you cannot set
1209 the properties :name, :tstart, :tend, :block, and :scope - these will
1210 be overwritten to make sure the content accurately reflects the
1211 current display in the agenda."
1212 :group 'org-agenda-daily/weekly
1213 :type 'plist)
1215 (defcustom org-agenda-search-view-always-boolean nil
1216 "Non-nil means the search string is interpreted as individual parts.
1218 The search string for search view can either be interpreted as a phrase,
1219 or as a list of snippets that define a boolean search for a number of
1220 strings.
1222 When this is non-nil, the string will be split on whitespace, and each
1223 snippet will be searched individually, and all must match in order to
1224 select an entry. A snippet is then a single string of non-white
1225 characters, or a string in double quotes, or a regexp in {} braces.
1226 If a snippet is preceded by \"-\", the snippet must *not* match.
1227 \"+\" is syntactic sugar for positive selection. Each snippet may
1228 be found as a full word or a partial word, but see the variable
1229 `org-agenda-search-view-force-full-words'.
1231 When this is nil, search will look for the entire search phrase as one,
1232 with each space character matching any amount of whitespace, including
1233 line breaks.
1235 Even when this is nil, you can still switch to Boolean search dynamically
1236 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1237 is a regexp marked with braces like \"{abc}\", this will also switch to
1238 boolean search."
1239 :group 'org-agenda-search-view
1240 :type 'boolean)
1242 (if (fboundp 'defvaralias)
1243 (defvaralias 'org-agenda-search-view-search-words-only
1244 'org-agenda-search-view-always-boolean))
1246 (defcustom org-agenda-search-view-force-full-words nil
1247 "Non-nil means, search words must be matches as complete words.
1248 When nil, they may also match part of a word."
1249 :group 'org-agenda-search-view
1250 :type 'boolean)
1252 (defgroup org-agenda-time-grid nil
1253 "Options concerning the time grid in the Org-mode Agenda."
1254 :tag "Org Agenda Time Grid"
1255 :group 'org-agenda)
1257 (defcustom org-agenda-search-headline-for-time t
1258 "Non-nil means search headline for a time-of-day.
1259 If the headline contains a time-of-day in one format or another, it will
1260 be used to sort the entry into the time sequence of items for a day.
1261 Some people have time stamps in the headline that refer to the creation
1262 time or so, and then this produces an unwanted side effect. If this is
1263 the case for your, use this variable to turn off searching the headline
1264 for a time."
1265 :group 'org-agenda-time-grid
1266 :type 'boolean)
1268 (defcustom org-agenda-use-time-grid t
1269 "Non-nil means show a time grid in the agenda schedule.
1270 A time grid is a set of lines for specific times (like every two hours between
1271 8:00 and 20:00). The items scheduled for a day at specific times are
1272 sorted in between these lines.
1273 For details about when the grid will be shown, and what it will look like, see
1274 the variable `org-agenda-time-grid'."
1275 :group 'org-agenda-time-grid
1276 :type 'boolean)
1278 (defcustom org-agenda-time-grid
1279 '((daily today require-timed)
1280 "----------------"
1281 (800 1000 1200 1400 1600 1800 2000))
1283 "The settings for time grid for agenda display.
1284 This is a list of three items. The first item is again a list. It contains
1285 symbols specifying conditions when the grid should be displayed:
1287 daily if the agenda shows a single day
1288 weekly if the agenda shows an entire week
1289 today show grid on current date, independent of daily/weekly display
1290 require-timed show grid only if at least one item has a time specification
1292 The second item is a string which will be placed behind the grid time.
1294 The third item is a list of integers, indicating the times that should have
1295 a grid line."
1296 :group 'org-agenda-time-grid
1297 :type
1298 '(list
1299 (set :greedy t :tag "Grid Display Options"
1300 (const :tag "Show grid in single day agenda display" daily)
1301 (const :tag "Show grid in weekly agenda display" weekly)
1302 (const :tag "Always show grid for today" today)
1303 (const :tag "Show grid only if any timed entries are present"
1304 require-timed)
1305 (const :tag "Skip grid times already present in an entry"
1306 remove-match))
1307 (string :tag "Grid String")
1308 (repeat :tag "Grid Times" (integer :tag "Time"))))
1310 (defcustom org-agenda-show-current-time-in-grid t
1311 "Non-nil means show the current time in the time grid."
1312 :group 'org-agenda-time-grid
1313 :type 'boolean)
1315 (defcustom org-agenda-current-time-string
1316 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1317 "The string for the current time marker in the agenda."
1318 :group 'org-agenda-time-grid
1319 :type 'string)
1321 (defgroup org-agenda-sorting nil
1322 "Options concerning sorting in the Org-mode Agenda."
1323 :tag "Org Agenda Sorting"
1324 :group 'org-agenda)
1326 (defcustom org-agenda-sorting-strategy
1327 '((agenda habit-down time-up priority-down category-keep)
1328 (todo priority-down category-keep)
1329 (tags priority-down category-keep)
1330 (search category-keep))
1331 "Sorting structure for the agenda items of a single day.
1332 This is a list of symbols which will be used in sequence to determine
1333 if an entry should be listed before another entry. The following
1334 symbols are recognized:
1336 time-up Put entries with time-of-day indications first, early first
1337 time-down Put entries with time-of-day indications first, late first
1338 category-keep Keep the default order of categories, corresponding to the
1339 sequence in `org-agenda-files'.
1340 category-up Sort alphabetically by category, A-Z.
1341 category-down Sort alphabetically by category, Z-A.
1342 tag-up Sort alphabetically by last tag, A-Z.
1343 tag-down Sort alphabetically by last tag, Z-A.
1344 priority-up Sort numerically by priority, high priority last.
1345 priority-down Sort numerically by priority, high priority first.
1346 todo-state-up Sort by todo state, tasks that are done last.
1347 todo-state-down Sort by todo state, tasks that are done first.
1348 effort-up Sort numerically by estimated effort, high effort last.
1349 effort-down Sort numerically by estimated effort, high effort first.
1350 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1351 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1352 habit-up Put entries that are habits first
1353 habit-down Put entries that are habits last
1354 alpha-up Sort headlines alphabetically
1355 alpha-down Sort headlines alphabetically, reversed
1357 The different possibilities will be tried in sequence, and testing stops
1358 if one comparison returns a \"not-equal\". For example, the default
1359 '(time-up category-keep priority-down)
1360 means: Pull out all entries having a specified time of day and sort them,
1361 in order to make a time schedule for the current day the first thing in the
1362 agenda listing for the day. Of the entries without a time indication, keep
1363 the grouped in categories, don't sort the categories, but keep them in
1364 the sequence given in `org-agenda-files'. Within each category sort by
1365 priority.
1367 Leaving out `category-keep' would mean that items will be sorted across
1368 categories by priority.
1370 Instead of a single list, this can also be a set of list for specific
1371 contents, with a context symbol in the car of the list, any of
1372 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1374 Custom commands can bind this variable in the options section."
1375 :group 'org-agenda-sorting
1376 :type `(choice
1377 (repeat :tag "General" ,org-sorting-choice)
1378 (list :tag "Individually"
1379 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1380 (repeat ,org-sorting-choice))
1381 (cons (const :tag "Strategy for TODO lists" todo)
1382 (repeat ,org-sorting-choice))
1383 (cons (const :tag "Strategy for Tags matches" tags)
1384 (repeat ,org-sorting-choice))
1385 (cons (const :tag "Strategy for search matches" search)
1386 (repeat ,org-sorting-choice)))))
1388 (defcustom org-agenda-cmp-user-defined nil
1389 "A function to define the comparison `user-defined'.
1390 This function must receive two arguments, agenda entry a and b.
1391 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1392 the user comparison, return nil.
1393 When this is defined, you can make `user-defined-up' and `user-defined-down'
1394 part of an agenda sorting strategy."
1395 :group 'org-agenda-sorting
1396 :type 'symbol)
1398 (defcustom org-sort-agenda-notime-is-late t
1399 "Non-nil means items without time are considered late.
1400 This is only relevant for sorting. When t, items which have no explicit
1401 time like 15:30 will be considered as 99:01, i.e. later than any items which
1402 do have a time. When nil, the default time is before 0:00. You can use this
1403 option to decide if the schedule for today should come before or after timeless
1404 agenda entries."
1405 :group 'org-agenda-sorting
1406 :type 'boolean)
1408 (defcustom org-sort-agenda-noeffort-is-high t
1409 "Non-nil means items without effort estimate are sorted as high effort.
1410 This also applies when filtering an agenda view with respect to the
1411 < or > effort operator. Then, tasks with no effort defined will be treated
1412 as tasks with high effort.
1413 When nil, such items are sorted as 0 minutes effort."
1414 :group 'org-agenda-sorting
1415 :type 'boolean)
1417 (defgroup org-agenda-line-format nil
1418 "Options concerning the entry prefix in the Org-mode agenda display."
1419 :tag "Org Agenda Line Format"
1420 :group 'org-agenda)
1422 (defcustom org-agenda-prefix-format
1423 '((agenda . " %i %-12:c%?-12t% s")
1424 (timeline . " % s")
1425 (todo . " %i %-12:c")
1426 (tags . " %i %-12:c")
1427 (search . " %i %-12:c"))
1428 "Format specifications for the prefix of items in the agenda views.
1429 An alist with five entries, each for the different agenda types. The
1430 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1431 The values are format strings.
1433 This format works similar to a printf format, with the following meaning:
1435 %c the category of the item, \"Diary\" for entries from the diary,
1436 or as given by the CATEGORY keyword or derived from the file name
1437 %e the effort required by the item
1438 %i the icon category of the item, see `org-agenda-category-icon-alist'
1439 %T the last tag of the item (ignore inherited tags, which come first)
1440 %t the HH:MM time-of-day specification if one applies to the entry
1441 %s Scheduling/Deadline information, a short string
1442 %(expression) Eval EXPRESSION and replace the control string
1443 by the result
1445 All specifiers work basically like the standard `%s' of printf, but may
1446 contain two additional characters: a question mark just after the `%'
1447 and a whitespace/punctuation character just before the final letter.
1449 If the first character after `%' is a question mark, the entire field
1450 will only be included if the corresponding value applies to the current
1451 entry. This is useful for fields which should have fixed width when
1452 present, but zero width when absent. For example, \"%?-12t\" will
1453 result in a 12 character time field if a time of the day is specified,
1454 but will completely disappear in entries which do not contain a time.
1456 If there is punctuation or whitespace character just before the final
1457 format letter, this character will be appended to the field value if
1458 the value is not empty. For example, the format \"%-12:c\" leads to
1459 \"Diary: \" if the category is \"Diary\". If the category were be
1460 empty, no additional colon would be inserted.
1462 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1463 which means:
1465 - Indent the line with two space characters
1466 - Give the category a 12 chars wide field, padded with whitespace on
1467 the right (because of `-'). Append a colon if there is a category
1468 (because of `:').
1469 - If there is a time-of-day, put it into a 12 chars wide field. If no
1470 time, don't put in an empty field, just skip it (because of '?').
1471 - Finally, put the scheduling information.
1473 See also the variables `org-agenda-remove-times-when-in-prefix' and
1474 `org-agenda-remove-tags'.
1476 Custom commands can set this variable in the options section."
1477 :type '(choice
1478 (string :tag "General format")
1479 (list :greedy t :tag "View dependent"
1480 (cons (const agenda) (string :tag "Format"))
1481 (cons (const timeline) (string :tag "Format"))
1482 (cons (const todo) (string :tag "Format"))
1483 (cons (const tags) (string :tag "Format"))
1484 (cons (const search) (string :tag "Format"))))
1485 :group 'org-agenda-line-format)
1487 (defvar org-prefix-format-compiled nil
1488 "The compiled version of the most recently used prefix format.
1489 See the variable `org-agenda-prefix-format'.")
1491 (defcustom org-agenda-todo-keyword-format "%-1s"
1492 "Format for the TODO keyword in agenda lines.
1493 Set this to something like \"%-12s\" if you want all TODO keywords
1494 to occupy a fixed space in the agenda display."
1495 :group 'org-agenda-line-format
1496 :type 'string)
1498 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1499 "Text preceding timerange entries in the agenda view.
1500 This is a list with two strings. The first applies when the range
1501 is entirely on one day. The second applies if the range spans several days.
1502 The strings may have two \"%d\" format specifiers which will be filled
1503 with the sequence number of the days, and the total number of days in the
1504 range, respectively."
1505 :group 'org-agenda-line-format
1506 :type '(list
1507 (string :tag "Deadline today ")
1508 (choice :tag "Deadline relative"
1509 (string :tag "Format string")
1510 (function))))
1512 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1513 "Text preceding scheduled items in the agenda view.
1514 This is a list with two strings. The first applies when the item is
1515 scheduled on the current day. The second applies when it has been scheduled
1516 previously, it may contain a %d indicating that this is the nth time that
1517 this item is scheduled, due to automatic rescheduling of unfinished items
1518 for the following day. So this number is one larger than the number of days
1519 that passed since this item was scheduled first."
1520 :group 'org-agenda-line-format
1521 :type '(list
1522 (string :tag "Scheduled today ")
1523 (string :tag "Scheduled previously")))
1525 (defcustom org-agenda-inactive-leader "["
1526 "Text preceding item pulled into the agenda by inactive time stamps.
1527 These entries are added to the agenda when pressing \"[\"."
1528 :group 'org-agenda-line-format
1529 :type '(list
1530 (string :tag "Scheduled today ")
1531 (string :tag "Scheduled previously")))
1533 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1534 "Text preceding deadline items in the agenda view.
1535 This is a list with two strings. The first applies when the item has its
1536 deadline on the current day. The second applies when it is in the past or
1537 in the future, it may contain %d to capture how many days away the deadline
1538 is (was)."
1539 :group 'org-agenda-line-format
1540 :type '(list
1541 (string :tag "Deadline today ")
1542 (choice :tag "Deadline relative"
1543 (string :tag "Format string")
1544 (function))))
1546 (defcustom org-agenda-remove-times-when-in-prefix t
1547 "Non-nil means remove duplicate time specifications in agenda items.
1548 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1549 time-of-day specification in a headline or diary entry is extracted and
1550 placed into the prefix. If this option is non-nil, the original specification
1551 \(a timestamp or -range, or just a plain time(range) specification like
1552 11:30-4pm) will be removed for agenda display. This makes the agenda less
1553 cluttered.
1554 The option can be t or nil. It may also be the symbol `beg', indicating
1555 that the time should only be removed when it is located at the beginning of
1556 the headline/diary entry."
1557 :group 'org-agenda-line-format
1558 :type '(choice
1559 (const :tag "Always" t)
1560 (const :tag "Never" nil)
1561 (const :tag "When at beginning of entry" beg)))
1563 (defcustom org-agenda-remove-timeranges-from-blocks nil
1564 "Non-nil means remove time ranges specifications in agenda
1565 items that span on several days."
1566 :group 'org-agenda-line-format
1567 :type 'boolean)
1569 (defcustom org-agenda-default-appointment-duration nil
1570 "Default duration for appointments that only have a starting time.
1571 When nil, no duration is specified in such cases.
1572 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1573 :group 'org-agenda-line-format
1574 :type '(choice
1575 (integer :tag "Minutes")
1576 (const :tag "No default duration")))
1578 (defcustom org-agenda-show-inherited-tags t
1579 "Non-nil means show inherited tags in each agenda line."
1580 :group 'org-agenda-line-format
1581 :type 'boolean)
1583 (defcustom org-agenda-hide-tags-regexp nil
1584 "Regular expression used to filter away specific tags in agenda views.
1585 This means that these tags will be present, but not be shown in the agenda
1586 line. Secondary filtering will still work on the hidden tags.
1587 Nil means don't hide any tags."
1588 :group 'org-agenda-line-format
1589 :type '(choice
1590 (const :tag "Hide none" nil)
1591 (string :tag "Regexp ")))
1593 (defcustom org-agenda-remove-tags nil
1594 "Non-nil means remove the tags from the headline copy in the agenda.
1595 When this is the symbol `prefix', only remove tags when
1596 `org-agenda-prefix-format' contains a `%T' specifier."
1597 :group 'org-agenda-line-format
1598 :type '(choice
1599 (const :tag "Always" t)
1600 (const :tag "Never" nil)
1601 (const :tag "When prefix format contains %T" prefix)))
1603 (if (fboundp 'defvaralias)
1604 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1605 'org-agenda-remove-tags))
1607 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1608 "Shift tags in agenda items to this column.
1609 If this number is positive, it specifies the column. If it is negative,
1610 it means that the tags should be flushright to that column. For example,
1611 -80 works well for a normal 80 character screen."
1612 :group 'org-agenda-line-format
1613 :type 'integer)
1615 (if (fboundp 'defvaralias)
1616 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1618 (defcustom org-agenda-fontify-priorities 'cookies
1619 "Non-nil means highlight low and high priorities in agenda.
1620 When t, the highest priority entries are bold, lowest priority italic.
1621 However, settings in `org-priority-faces' will overrule these faces.
1622 When this variable is the symbol `cookies', only fontify the
1623 cookies, not the entire task.
1624 This may also be an association list of priority faces, whose
1625 keys are the character values of `org-highest-priority',
1626 `org-default-priority', and `org-lowest-priority' (the default values
1627 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1628 color as a string, or a list like `(:background \"Red\")'.
1629 If it is a color, the variable `org-faces-easy-properties'
1630 determines if it is a foreground or a background color."
1631 :group 'org-agenda-line-format
1632 :type '(choice
1633 (const :tag "Never" nil)
1634 (const :tag "Defaults" t)
1635 (const :tag "Cookies only" cookies)
1636 (repeat :tag "Specify"
1637 (list (character :tag "Priority" :value ?A)
1638 (choice :tag "Face "
1639 (string :tag "Color")
1640 (sexp :tag "Face"))))))
1642 (defcustom org-agenda-day-face-function nil
1643 "Function called to determine what face should be used to display a day.
1644 The only argument passed to that function is the day. It should
1645 returns a face, or nil if does not want to specify a face and let
1646 the normal rules apply."
1647 :group 'org-agenda-line-format
1648 :type 'function)
1650 (defcustom org-agenda-category-icon-alist nil
1651 "Alist of category icon to be displayed in agenda views.
1653 Each entry should have the following format:
1655 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1657 Where CATEGORY-REGEXP is a regexp matching the categories where
1658 the icon should be displayed.
1659 FILE-OR-DATA either a file path or a string containing image data.
1661 The other fields can be omitted safely if not needed:
1662 TYPE indicates the image type.
1663 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1664 image data.
1665 PROPS are additional image attributes to assign to the image,
1666 like, e.g. `:ascent center'.
1668 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1670 If you want to set the display properties yourself, just put a
1671 list as second element:
1673 (CATEGORY-REGEXP (MY PROPERTY LIST))
1675 For example, to display a 16px horizontal space for Emacs
1676 category, you can use:
1678 (\"Emacs\" '(space . (:width (16))))"
1679 :group 'org-agenda-line-format
1680 :type '(alist :key-type (string :tag "Regexp matching category")
1681 :value-type (choice (list :tag "Icon"
1682 (string :tag "File or data")
1683 (symbol :tag "Type")
1684 (boolean :tag "Data?")
1685 (repeat :tag "Extra image properties" :inline t symbol))
1686 (list :tag "Display properties" sexp))))
1688 (defgroup org-agenda-column-view nil
1689 "Options concerning column view in the agenda."
1690 :tag "Org Agenda Column View"
1691 :group 'org-agenda)
1693 (defcustom org-agenda-columns-show-summaries t
1694 "Non-nil means show summaries for columns displayed in the agenda view."
1695 :group 'org-agenda-column-view
1696 :type 'boolean)
1698 (defcustom org-agenda-columns-compute-summary-properties t
1699 "Non-nil means recompute all summary properties before column view.
1700 When column view in the agenda is listing properties that have a summary
1701 operator, it can go to all relevant buffers and recompute the summaries
1702 there. This can mean overhead for the agenda column view, but is necessary
1703 to have thing up to date.
1704 As a special case, a CLOCKSUM property also makes sure that the clock
1705 computations are current."
1706 :group 'org-agenda-column-view
1707 :type 'boolean)
1709 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1710 "Non-nil means the duration of an appointment will add to day effort.
1711 The property to which appointment durations will be added is the one given
1712 in the option `org-effort-property'. If an appointment does not have
1713 an end time, `org-agenda-default-appointment-duration' will be used. If that
1714 is not set, an appointment without end time will not contribute to the time
1715 estimate."
1716 :group 'org-agenda-column-view
1717 :type 'boolean)
1719 (defcustom org-agenda-auto-exclude-function nil
1720 "A function called with a tag to decide if it is filtered on '/ RET'.
1721 The sole argument to the function, which is called once for each
1722 possible tag, is a string giving the name of the tag. The
1723 function should return either nil if the tag should be included
1724 as normal, or \"-<TAG>\" to exclude the tag.
1725 Note that for the purpose of tag filtering, only the lower-case version of
1726 all tags will be considered, so that this function will only ever see
1727 the lower-case version of all tags."
1728 :group 'org-agenda
1729 :type 'function)
1731 (defcustom org-agenda-bulk-custom-functions nil
1732 "Alist of characters and custom functions for bulk actions.
1733 For example, this value makes those two functions available:
1735 '((?R set-category)
1736 (?C bulk-cut))
1738 With selected entries in an agenda buffer, `B R' will call
1739 the custom function `set-category' on the selected entries.
1740 Note that functions in this alist don't need to be quoted."
1741 :type 'alist
1742 :group 'org-agenda)
1744 (eval-when-compile
1745 (require 'cl))
1746 (require 'org)
1748 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1749 "Execute BODY with point at location given by `org-hd-marker' property.
1750 If STRING is non-nil, the text property will be fetched from position 0
1751 in that string. If STRING is nil, it will be fetched from the beginning
1752 of the current line."
1753 (org-with-gensyms (marker)
1754 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1755 'org-hd-marker ,string)))
1756 (with-current-buffer (marker-buffer ,marker)
1757 (save-excursion
1758 (goto-char ,marker)
1759 ,@body)))))
1760 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1762 (defun org-add-agenda-custom-command (entry)
1763 "Replace or add a command in `org-agenda-custom-commands'.
1764 This is mostly for hacking and trying a new command - once the command
1765 works you probably want to add it to `org-agenda-custom-commands' for good."
1766 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1767 (if ass
1768 (setcdr ass (cdr entry))
1769 (push entry org-agenda-custom-commands))))
1771 ;;; Define the Org-agenda-mode
1773 (defvar org-agenda-mode-map (make-sparse-keymap)
1774 "Keymap for `org-agenda-mode'.")
1775 (if (fboundp 'defvaralias)
1776 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1778 (defvar org-agenda-menu) ; defined later in this file.
1779 (defvar org-agenda-restrict) ; defined later in this file.
1780 (defvar org-agenda-follow-mode nil)
1781 (defvar org-agenda-entry-text-mode nil)
1782 (defvar org-agenda-clockreport-mode nil)
1783 (defvar org-agenda-show-log nil)
1784 (defvar org-agenda-redo-command nil)
1785 (defvar org-agenda-query-string nil)
1786 (defvar org-agenda-mode-hook nil
1787 "Hook for `org-agenda-mode', run after the mode is turned on.")
1788 (defvar org-agenda-type nil)
1789 (defvar org-agenda-force-single-file nil)
1790 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1792 (defun org-agenda-mode ()
1793 "Mode for time-sorted view on action items in Org-mode files.
1795 The following commands are available:
1797 \\{org-agenda-mode-map}"
1798 (interactive)
1799 (kill-all-local-variables)
1800 (setq org-agenda-undo-list nil
1801 org-agenda-pending-undo-list nil
1802 org-agenda-bulk-marked-entries nil)
1803 (setq major-mode 'org-agenda-mode)
1804 ;; Keep global-font-lock-mode from turning on font-lock-mode
1805 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1806 (setq mode-name "Org-Agenda")
1807 (use-local-map org-agenda-mode-map)
1808 (easy-menu-add org-agenda-menu)
1809 (if org-startup-truncated (setq truncate-lines t))
1810 (org-set-local 'line-move-visual nil)
1811 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1812 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1813 ;; Make sure properties are removed when copying text
1814 (when (boundp 'buffer-substring-filters)
1815 (org-set-local 'buffer-substring-filters
1816 (cons (lambda (x)
1817 (set-text-properties 0 (length x) nil x) x)
1818 buffer-substring-filters)))
1819 (unless org-agenda-keep-modes
1820 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1821 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1822 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1823 org-agenda-show-log org-agenda-start-with-log-mode))
1825 (easy-menu-change
1826 '("Agenda") "Agenda Files"
1827 (append
1828 (list
1829 (vector
1830 (if (get 'org-agenda-files 'org-restrict)
1831 "Restricted to single file"
1832 "Edit File List")
1833 '(org-edit-agenda-file-list)
1834 (not (get 'org-agenda-files 'org-restrict)))
1835 "--")
1836 (mapcar 'org-file-menu-entry (org-agenda-files))))
1837 (org-agenda-set-mode-name)
1838 (apply
1839 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1840 (list 'org-agenda-mode-hook)))
1842 (substitute-key-definition 'undo 'org-agenda-undo
1843 org-agenda-mode-map global-map)
1844 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1845 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1846 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1847 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1848 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1849 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1850 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1851 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1852 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1853 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1854 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1855 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1856 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1857 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1858 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1859 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1860 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1861 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1862 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1863 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1864 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1865 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1866 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1867 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1868 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1869 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1870 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1871 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1872 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1873 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1874 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1875 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1876 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1877 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1878 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1879 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1880 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1881 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1882 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1883 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1884 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1885 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1886 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1887 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1888 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1890 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1891 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1892 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1893 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1894 (while l (org-defkey org-agenda-mode-map
1895 (int-to-string (pop l)) 'digit-argument)))
1897 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1898 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1899 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1900 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1901 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1902 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1903 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1904 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1905 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1906 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1907 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1908 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1909 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1910 'org-clock-modify-effort-estimate)
1911 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1912 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1913 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1914 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
1915 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1916 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1917 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1918 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1919 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1920 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1921 (substitute-key-definition 'next-line 'org-agenda-next-line
1922 org-agenda-mode-map global-map)
1923 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1924 org-agenda-mode-map global-map)
1925 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1926 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1927 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1928 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1929 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1930 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1931 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1932 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1933 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1934 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1935 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1936 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1937 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1938 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1939 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1940 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1941 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1942 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1943 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1944 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1945 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1946 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1947 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1948 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1949 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1950 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1951 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1952 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1953 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1954 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1956 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1957 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1958 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1959 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1960 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1961 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1962 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
1963 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1964 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1965 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1966 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1968 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1969 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1970 (when org-agenda-mouse-1-follows-link
1971 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1972 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1973 '("Agenda"
1974 ("Agenda Files")
1975 "--"
1976 ("Agenda Dates"
1977 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1978 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1979 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1980 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1981 "--"
1982 ("View"
1983 ["Day View" org-agenda-day-view
1984 :active (org-agenda-check-type nil 'agenda)
1985 :style radio :selected (eq org-agenda-current-span 'day)
1986 :keys "v d (or just d)"]
1987 ["Week View" org-agenda-week-view
1988 :active (org-agenda-check-type nil 'agenda)
1989 :style radio :selected (eq org-agenda-current-span 'week)
1990 :keys "v w (or just w)"]
1991 ["Month View" org-agenda-month-view
1992 :active (org-agenda-check-type nil 'agenda)
1993 :style radio :selected (eq org-agenda-current-span 'month)
1994 :keys "v m"]
1995 ["Year View" org-agenda-year-view
1996 :active (org-agenda-check-type nil 'agenda)
1997 :style radio :selected (eq org-agenda-current-span 'year)
1998 :keys "v y"]
1999 "--"
2000 ["Include Diary" org-agenda-toggle-diary
2001 :style toggle :selected org-agenda-include-diary
2002 :active (org-agenda-check-type nil 'agenda)]
2003 ["Include Deadlines" org-agenda-toggle-deadlines
2004 :style toggle :selected org-agenda-include-deadlines
2005 :active (org-agenda-check-type nil 'agenda)]
2006 ["Use Time Grid" org-agenda-toggle-time-grid
2007 :style toggle :selected org-agenda-use-time-grid
2008 :active (org-agenda-check-type nil 'agenda)]
2009 "--"
2010 ["Show clock report" org-agenda-clockreport-mode
2011 :style toggle :selected org-agenda-clockreport-mode
2012 :active (org-agenda-check-type nil 'agenda)]
2013 ["Show some entry text" org-agenda-entry-text-mode
2014 :style toggle :selected org-agenda-entry-text-mode
2015 :active t]
2016 "--"
2017 ["Show Logbook entries" org-agenda-log-mode
2018 :style toggle :selected org-agenda-show-log
2019 :active (org-agenda-check-type nil 'agenda 'timeline)
2020 :keys "v l (or just l)"]
2021 ["Include archived trees" org-agenda-archives-mode
2022 :style toggle :selected org-agenda-archives-mode :active t
2023 :keys "v a"]
2024 ["Include archive files" (org-agenda-archives-mode t)
2025 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2026 :keys "v A"]
2027 "--"
2028 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2029 ["Write view to file" org-agenda-write t]
2030 ["Rebuild buffer" org-agenda-redo t]
2031 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2032 "--"
2033 ["Show original entry" org-agenda-show t]
2034 ["Go To (other window)" org-agenda-goto t]
2035 ["Go To (this window)" org-agenda-switch-to t]
2036 ["Follow Mode" org-agenda-follow-mode
2037 :style toggle :selected org-agenda-follow-mode :active t]
2038 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2039 "--"
2040 ("TODO"
2041 ["Cycle TODO" org-agenda-todo t]
2042 ["Next TODO set" org-agenda-todo-nextset t]
2043 ["Previous TODO set" org-agenda-todo-previousset t]
2044 ["Add note" org-agenda-add-note t])
2045 ("Archive/Refile/Delete"
2046 ["Archive default" org-agenda-archive-default t]
2047 ["Archive default" org-agenda-archive-default-with-confirmation t]
2048 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2049 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2050 ["Archive subtree" org-agenda-archive t]
2051 "--"
2052 ["Refile" org-agenda-refile t]
2053 "--"
2054 ["Delete subtree" org-agenda-kill t])
2055 ("Bulk action"
2056 ["Mark entry" org-agenda-bulk-mark t]
2057 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2058 ["Unmark entry" org-agenda-bulk-unmark t]
2059 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2060 ["Act on all marked" org-agenda-bulk-action t]
2061 "--"
2062 ("Tags and Properties"
2063 ["Show all Tags" org-agenda-show-tags t]
2064 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2065 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2066 "--"
2067 ["Column View" org-columns t])
2068 ("Deadline/Schedule"
2069 ["Schedule" org-agenda-schedule t]
2070 ["Set Deadline" org-agenda-deadline t]
2071 "--"
2072 ["Mark item" org-agenda-action :active t :keys "k m"]
2073 ["Show mark item" org-agenda-action :active t :keys "k v"]
2074 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2075 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2076 "--"
2077 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2078 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2079 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2080 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2081 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2082 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2083 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2084 ("Clock and Effort"
2085 ["Clock in" org-agenda-clock-in t]
2086 ["Clock out" org-agenda-clock-out t]
2087 ["Clock cancel" org-agenda-clock-cancel t]
2088 ["Goto running clock" org-clock-goto t]
2089 "--"
2090 ["Set Effort" org-agenda-set-effort t]
2091 ["Change clocked effort" org-clock-modify-effort-estimate
2092 (org-clock-is-active)])
2093 ("Priority"
2094 ["Set Priority" org-agenda-priority t]
2095 ["Increase Priority" org-agenda-priority-up t]
2096 ["Decrease Priority" org-agenda-priority-down t]
2097 ["Show Priority" org-agenda-show-priority t])
2098 ("Calendar/Diary"
2099 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2100 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2101 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2102 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2103 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2104 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2105 "--"
2106 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2107 "--"
2108 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2109 "--"
2110 ("MobileOrg"
2111 ["Push Files and Views" org-mobile-push t]
2112 ["Get Captured and Flagged" org-mobile-pull t]
2113 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2114 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2115 "--"
2116 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2117 "--"
2118 ["Quit" org-agenda-quit t]
2119 ["Exit and Release Buffers" org-agenda-exit t]
2122 ;;; Agenda undo
2124 (defvar org-agenda-allow-remote-undo t
2125 "Non-nil means allow remote undo from the agenda buffer.")
2126 (defvar org-agenda-undo-list nil
2127 "List of undoable operations in the agenda since last refresh.")
2128 (defvar org-agenda-undo-has-started-in nil
2129 "Buffers that have already seen `undo-start' in the current undo sequence.")
2130 (defvar org-agenda-pending-undo-list nil
2131 "In a series of undo commands, this is the list of remaining undo items.")
2133 (defun org-agenda-undo ()
2134 "Undo a remote editing step in the agenda.
2135 This undoes changes both in the agenda buffer and in the remote buffer
2136 that have been changed along."
2137 (interactive)
2138 (or org-agenda-allow-remote-undo
2139 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2140 (if (not (eq this-command last-command))
2141 (setq org-agenda-undo-has-started-in nil
2142 org-agenda-pending-undo-list org-agenda-undo-list))
2143 (if (not org-agenda-pending-undo-list)
2144 (error "No further undo information"))
2145 (let* ((entry (pop org-agenda-pending-undo-list))
2146 buf line cmd rembuf)
2147 (setq cmd (pop entry) line (pop entry))
2148 (setq rembuf (nth 2 entry))
2149 (org-with-remote-undo rembuf
2150 (while (bufferp (setq buf (pop entry)))
2151 (if (pop entry)
2152 (with-current-buffer buf
2153 (let ((last-undo-buffer buf)
2154 (inhibit-read-only t))
2155 (unless (memq buf org-agenda-undo-has-started-in)
2156 (push buf org-agenda-undo-has-started-in)
2157 (make-local-variable 'pending-undo-list)
2158 (undo-start))
2159 (while (and pending-undo-list
2160 (listp pending-undo-list)
2161 (not (car pending-undo-list)))
2162 (pop pending-undo-list))
2163 (undo-more 1))))))
2164 (org-goto-line line)
2165 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2167 (defun org-verify-change-for-undo (l1 l2)
2168 "Verify that a real change occurred between the undo lists L1 and L2."
2169 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2170 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2171 (not (eq l1 l2)))
2173 ;;; Agenda dispatch
2175 (defvar org-agenda-restrict nil)
2176 (defvar org-agenda-restrict-begin (make-marker))
2177 (defvar org-agenda-restrict-end (make-marker))
2178 (defvar org-agenda-last-dispatch-buffer nil)
2179 (defvar org-agenda-overriding-restriction nil)
2181 ;;;###autoload
2182 (defun org-agenda (&optional arg keys restriction)
2183 "Dispatch agenda commands to collect entries to the agenda buffer.
2184 Prompts for a command to execute. Any prefix arg will be passed
2185 on to the selected command. The default selections are:
2187 a Call `org-agenda-list' to display the agenda for current day or week.
2188 t Call `org-todo-list' to display the global todo list.
2189 T Call `org-todo-list' to display the global todo list, select only
2190 entries with a specific TODO keyword (the user gets a prompt).
2191 m Call `org-tags-view' to display headlines with tags matching
2192 a condition (the user is prompted for the condition).
2193 M Like `m', but select only TODO entries, no ordinary headlines.
2194 L Create a timeline for the current buffer.
2195 e Export views to associated files.
2196 s Search entries for keywords.
2197 / Multi occur across all agenda files and also files listed
2198 in `org-agenda-text-search-extra-files'.
2199 < Restrict agenda commands to buffer, subtree, or region.
2200 Press several times to get the desired effect.
2201 > Remove a previous restriction.
2202 # List \"stuck\" projects.
2203 ! Configure what \"stuck\" means.
2204 C Configure custom agenda commands.
2206 More commands can be added by configuring the variable
2207 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2208 searches can be pre-defined in this way.
2210 If the current buffer is in Org-mode and visiting a file, you can also
2211 first press `<' once to indicate that the agenda should be temporarily
2212 \(until the next use of \\[org-agenda]) restricted to the current file.
2213 Pressing `<' twice means to restrict to the current subtree or region
2214 \(if active)."
2215 (interactive "P")
2216 (catch 'exit
2217 (let* ((prefix-descriptions nil)
2218 (org-agenda-window-setup (if (equal (buffer-name)
2219 org-agenda-buffer-name)
2220 'current-window
2221 org-agenda-window-setup))
2222 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2223 (org-agenda-custom-commands
2224 ;; normalize different versions
2225 (delq nil
2226 (mapcar
2227 (lambda (x)
2228 (cond ((stringp (cdr x))
2229 (push x prefix-descriptions)
2230 nil)
2231 ((stringp (nth 1 x)) x)
2232 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2233 (t (cons (car x) (cons "" (cdr x))))))
2234 org-agenda-custom-commands)))
2235 (buf (current-buffer))
2236 (bfn (buffer-file-name (buffer-base-buffer)))
2237 entry key type match lprops ans)
2238 ;; Turn off restriction unless there is an overriding one,
2239 (unless org-agenda-overriding-restriction
2240 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2241 ;; There is a request to keep the file list in place
2242 (put 'org-agenda-files 'org-restrict nil))
2243 (setq org-agenda-restrict nil)
2244 (move-marker org-agenda-restrict-begin nil)
2245 (move-marker org-agenda-restrict-end nil))
2246 ;; Delete old local properties
2247 (put 'org-agenda-redo-command 'org-lprops nil)
2248 ;; Delete previously set last-arguments
2249 (put 'org-agenda-redo-command 'last-args nil)
2250 ;; Remember where this call originated
2251 (setq org-agenda-last-dispatch-buffer (current-buffer))
2252 (unless keys
2253 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2254 keys (car ans)
2255 restriction (cdr ans)))
2256 ;; Establish the restriction, if any
2257 (when (and (not org-agenda-overriding-restriction) restriction)
2258 (put 'org-agenda-files 'org-restrict (list bfn))
2259 (cond
2260 ((eq restriction 'region)
2261 (setq org-agenda-restrict t)
2262 (move-marker org-agenda-restrict-begin (region-beginning))
2263 (move-marker org-agenda-restrict-end (region-end)))
2264 ((eq restriction 'subtree)
2265 (save-excursion
2266 (setq org-agenda-restrict t)
2267 (org-back-to-heading t)
2268 (move-marker org-agenda-restrict-begin (point))
2269 (move-marker org-agenda-restrict-end
2270 (progn (org-end-of-subtree t)))))))
2272 ;; For example the todo list should not need it (but does...)
2273 (cond
2274 ((setq entry (assoc keys org-agenda-custom-commands))
2275 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2276 (progn
2277 (setq type (nth 2 entry) match (eval (nth 3 entry))
2278 lprops (nth 4 entry))
2279 (put 'org-agenda-redo-command 'org-lprops lprops)
2280 (cond
2281 ((eq type 'agenda)
2282 (org-let lprops '(org-agenda-list current-prefix-arg)))
2283 ((eq type 'alltodo)
2284 (org-let lprops '(org-todo-list current-prefix-arg)))
2285 ((eq type 'search)
2286 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2287 ((eq type 'stuck)
2288 (org-let lprops '(org-agenda-list-stuck-projects
2289 current-prefix-arg)))
2290 ((eq type 'tags)
2291 (org-let lprops '(org-tags-view current-prefix-arg match)))
2292 ((eq type 'tags-todo)
2293 (org-let lprops '(org-tags-view '(4) match)))
2294 ((eq type 'todo)
2295 (org-let lprops '(org-todo-list match)))
2296 ((eq type 'tags-tree)
2297 (org-check-for-org-mode)
2298 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2299 ((eq type 'todo-tree)
2300 (org-check-for-org-mode)
2301 (org-let lprops
2302 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2303 (regexp-quote match) "\\>"))))
2304 ((eq type 'occur-tree)
2305 (org-check-for-org-mode)
2306 (org-let lprops '(org-occur match)))
2307 ((functionp type)
2308 (org-let lprops '(funcall type match)))
2309 ((fboundp type)
2310 (org-let lprops '(funcall type match)))
2311 (t (error "Invalid custom agenda command type %s" type))))
2312 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2313 ((equal keys "C")
2314 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2315 (customize-variable 'org-agenda-custom-commands))
2316 ((equal keys "a") (call-interactively 'org-agenda-list))
2317 ((equal keys "s") (call-interactively 'org-search-view))
2318 ((equal keys "t") (call-interactively 'org-todo-list))
2319 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2320 ((equal keys "m") (call-interactively 'org-tags-view))
2321 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2322 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2323 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2324 (org-add-hook
2325 'post-command-hook
2326 (lambda ()
2327 (unless (current-message)
2328 (let* ((m (org-agenda-get-any-marker))
2329 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2330 (when note
2331 (message (concat
2332 "FLAGGING-NOTE ([?] for more info): "
2333 (org-add-props
2334 (replace-regexp-in-string
2335 "\\\\n" "//"
2336 (copy-sequence note))
2337 nil 'face 'org-warning)))))))
2338 t t))
2339 ((equal keys "L")
2340 (unless (eq major-mode 'org-mode)
2341 (error "This is not an Org-mode file"))
2342 (unless restriction
2343 (put 'org-agenda-files 'org-restrict (list bfn))
2344 (org-call-with-arg 'org-timeline arg)))
2345 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2346 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2347 ((equal keys "!") (customize-variable 'org-stuck-projects))
2348 (t (error "Invalid agenda key"))))))
2350 (defun org-agenda-append-agenda ()
2351 "Append another agenda view to the current one.
2352 This function allows interactive building of block agendas.
2353 Agenda views are separated by `org-agenda-block-separator'."
2354 (interactive)
2355 (unless (string= (buffer-name) org-agenda-buffer-name)
2356 (error "Can only append from within agenda buffer"))
2357 (let ((org-agenda-multi t))
2358 (org-agenda)
2359 (widen)))
2361 (defun org-agenda-normalize-custom-commands (cmds)
2362 (delq nil
2363 (mapcar
2364 (lambda (x)
2365 (cond ((stringp (cdr x)) nil)
2366 ((stringp (nth 1 x)) x)
2367 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2368 (t (cons (car x) (cons "" (cdr x))))))
2369 cmds)))
2371 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2372 "The user interface for selecting an agenda command."
2373 (catch 'exit
2374 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2375 (restrict-ok (and bfn (eq major-mode 'org-mode)))
2376 (region-p (org-region-active-p))
2377 (custom org-agenda-custom-commands)
2378 (selstring "")
2379 restriction second-time
2380 c entry key type match prefixes rmheader header-end custom1 desc
2381 line lines left right n n1)
2382 (save-window-excursion
2383 (delete-other-windows)
2384 (org-switch-to-buffer-other-window " *Agenda Commands*")
2385 (erase-buffer)
2386 (insert (eval-when-compile
2387 (let ((header
2389 Press key for an agenda command: < Buffer, subtree/region restriction
2390 -------------------------------- > Remove restriction
2391 a Agenda for current week or day e Export agenda views
2392 t List of all TODO entries T Entries with special TODO kwd
2393 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2394 L Timeline for current buffer # List stuck projects (!=configure)
2395 s Search for keywords C Configure custom agenda commands
2396 / Multi-occur ? Find :FLAGGED: entries
2398 (start 0))
2399 (while (string-match
2400 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2401 header start)
2402 (setq start (match-end 0))
2403 (add-text-properties (match-beginning 2) (match-end 2)
2404 '(face bold) header))
2405 header)))
2406 (setq header-end (move-marker (make-marker) (point)))
2407 (while t
2408 (setq custom1 custom)
2409 (when (eq rmheader t)
2410 (org-goto-line 1)
2411 (re-search-forward ":" nil t)
2412 (delete-region (match-end 0) (point-at-eol))
2413 (forward-char 1)
2414 (looking-at "-+")
2415 (delete-region (match-end 0) (point-at-eol))
2416 (move-marker header-end (match-end 0)))
2417 (goto-char header-end)
2418 (delete-region (point) (point-max))
2420 ;; Produce all the lines that describe custom commands and prefixes
2421 (setq lines nil)
2422 (while (setq entry (pop custom1))
2423 (setq key (car entry) desc (nth 1 entry)
2424 type (nth 2 entry)
2425 match (nth 3 entry))
2426 (if (> (length key) 1)
2427 (add-to-list 'prefixes (string-to-char key))
2428 (setq line
2429 (format
2430 "%-4s%-14s"
2431 (org-add-props (copy-sequence key)
2432 '(face bold))
2433 (cond
2434 ((string-match "\\S-" desc) desc)
2435 ((eq type 'agenda) "Agenda for current week or day")
2436 ((eq type 'alltodo) "List of all TODO entries")
2437 ((eq type 'search) "Word search")
2438 ((eq type 'stuck) "List of stuck projects")
2439 ((eq type 'todo) "TODO keyword")
2440 ((eq type 'tags) "Tags query")
2441 ((eq type 'tags-todo) "Tags (TODO)")
2442 ((eq type 'tags-tree) "Tags tree")
2443 ((eq type 'todo-tree) "TODO kwd tree")
2444 ((eq type 'occur-tree) "Occur tree")
2445 ((functionp type) (if (symbolp type)
2446 (symbol-name type)
2447 "Lambda expression"))
2448 (t "???"))))
2449 (if org-agenda-menu-show-matcher
2450 (setq line
2451 (concat line ": "
2452 (cond
2453 ((stringp match)
2454 (setq match (copy-sequence match))
2455 (org-add-props match nil 'face 'org-warning))
2456 (match
2457 (format "set of %d commands" (length match)))
2458 (t ""))))
2459 (if (org-string-nw-p match)
2460 (add-text-properties
2461 0 (length line) (list 'help-echo
2462 (concat "Matcher: "match)) line)))
2463 (push line lines)))
2464 (setq lines (nreverse lines))
2465 (when prefixes
2466 (mapc (lambda (x)
2467 (push
2468 (format "%s %s"
2469 (org-add-props (char-to-string x)
2470 nil 'face 'bold)
2471 (or (cdr (assoc (concat selstring
2472 (char-to-string x))
2473 prefix-descriptions))
2474 "Prefix key"))
2475 lines))
2476 prefixes))
2478 ;; Check if we should display in two columns
2479 (if org-agenda-menu-two-column
2480 (progn
2481 (setq n (length lines)
2482 n1 (+ (/ n 2) (mod n 2))
2483 right (nthcdr n1 lines)
2484 left (copy-sequence lines))
2485 (setcdr (nthcdr (1- n1) left) nil))
2486 (setq left lines right nil))
2487 (while left
2488 (insert "\n" (pop left))
2489 (when right
2490 (if (< (current-column) 40)
2491 (move-to-column 40 t)
2492 (insert " "))
2493 (insert (pop right))))
2495 ;; Make the window the right size
2496 (goto-char (point-min))
2497 (if second-time
2498 (if (not (pos-visible-in-window-p (point-max)))
2499 (org-fit-window-to-buffer))
2500 (setq second-time t)
2501 (org-fit-window-to-buffer))
2503 ;; Ask for selection
2504 (message "Press key for agenda command%s:"
2505 (if (or restrict-ok org-agenda-overriding-restriction)
2506 (if org-agenda-overriding-restriction
2507 " (restriction lock active)"
2508 (if restriction
2509 (format " (restricted to %s)" restriction)
2510 " (unrestricted)"))
2511 ""))
2512 (setq c (read-char-exclusive))
2513 (message "")
2514 (cond
2515 ((assoc (char-to-string c) custom)
2516 (setq selstring (concat selstring (char-to-string c)))
2517 (throw 'exit (cons selstring restriction)))
2518 ((memq c prefixes)
2519 (setq selstring (concat selstring (char-to-string c))
2520 prefixes nil
2521 rmheader (or rmheader t)
2522 custom (delq nil (mapcar
2523 (lambda (x)
2524 (if (or (= (length (car x)) 1)
2525 (/= (string-to-char (car x)) c))
2527 (cons (substring (car x) 1) (cdr x))))
2528 custom))))
2529 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2530 (message "Restriction is only possible in Org-mode buffers")
2531 (ding) (sit-for 1))
2532 ((eq c ?1)
2533 (org-agenda-remove-restriction-lock 'noupdate)
2534 (setq restriction 'buffer))
2535 ((eq c ?0)
2536 (org-agenda-remove-restriction-lock 'noupdate)
2537 (setq restriction (if region-p 'region 'subtree)))
2538 ((eq c ?<)
2539 (org-agenda-remove-restriction-lock 'noupdate)
2540 (setq restriction
2541 (cond
2542 ((eq restriction 'buffer)
2543 (if region-p 'region 'subtree))
2544 ((memq restriction '(subtree region))
2545 nil)
2546 (t 'buffer))))
2547 ((eq c ?>)
2548 (org-agenda-remove-restriction-lock 'noupdate)
2549 (setq restriction nil))
2550 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2551 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2552 ((and (> (length selstring) 0) (eq c ?\d))
2553 (delete-window)
2554 (org-agenda-get-restriction-and-command prefix-descriptions))
2556 ((equal c ?q) (error "Abort"))
2557 (t (error "Invalid key %c" c))))))))
2559 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2560 (defvar org-agenda-last-arguments nil
2561 "The arguments of the previous call to `org-agenda'.")
2562 (defun org-agenda-run-series (name series)
2563 (org-let (nth 1 series) '(org-prepare-agenda name))
2564 (let* ((org-agenda-multi t)
2565 (redo (list 'org-agenda-run-series name (list 'quote series)))
2566 (org-agenda-overriding-arguments
2567 (or org-agenda-overriding-arguments
2568 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2569 (get 'org-agenda-redo-command 'last-args))))
2570 (cmds (car series))
2571 (gprops (nth 1 series))
2572 match ;; The byte compiler incorrectly complains about this. Keep it!
2573 cmd type lprops)
2574 (while (setq cmd (pop cmds))
2575 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2576 (cond
2577 ((eq type 'agenda)
2578 (org-let2 gprops lprops
2579 '(call-interactively 'org-agenda-list)))
2580 ((eq type 'alltodo)
2581 (org-let2 gprops lprops
2582 '(call-interactively 'org-todo-list)))
2583 ((eq type 'search)
2584 (org-let2 gprops lprops
2585 '(org-search-view current-prefix-arg match nil)))
2586 ((eq type 'stuck)
2587 (org-let2 gprops lprops
2588 '(call-interactively 'org-agenda-list-stuck-projects)))
2589 ((eq type 'tags)
2590 (org-let2 gprops lprops
2591 '(org-tags-view current-prefix-arg match)))
2592 ((eq type 'tags-todo)
2593 (org-let2 gprops lprops
2594 '(org-tags-view '(4) match)))
2595 ((eq type 'todo)
2596 (org-let2 gprops lprops
2597 '(org-todo-list match)))
2598 ((fboundp type)
2599 (org-let2 gprops lprops
2600 '(funcall type match)))
2601 (t (error "Invalid type in command series"))))
2602 (widen)
2603 (setq org-agenda-redo-command redo)
2604 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2605 (goto-char (point-min)))
2606 (org-fit-agenda-window)
2607 (org-let (nth 1 series) '(org-finalize-agenda)))
2609 ;;;###autoload
2610 (defmacro org-batch-agenda (cmd-key &rest parameters)
2611 "Run an agenda command in batch mode and send the result to STDOUT.
2612 If CMD-KEY is a string of length 1, it is used as a key in
2613 `org-agenda-custom-commands' and triggers this command. If it is a
2614 longer string it is used as a tags/todo match string.
2615 Parameters are alternating variable names and values that will be bound
2616 before running the agenda command."
2617 (org-eval-in-environment (org-make-parameter-alist parameters)
2618 (if (> (length cmd-key) 2)
2619 (org-tags-view nil cmd-key)
2620 (org-agenda nil cmd-key)))
2621 (set-buffer org-agenda-buffer-name)
2622 (princ (buffer-string)))
2623 (def-edebug-spec org-batch-agenda (form &rest sexp))
2625 (defvar org-agenda-info nil)
2627 ;;;###autoload
2628 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2629 "Run an agenda command in batch mode and send the result to STDOUT.
2630 If CMD-KEY is a string of length 1, it is used as a key in
2631 `org-agenda-custom-commands' and triggers this command. If it is a
2632 longer string it is used as a tags/todo match string.
2633 Parameters are alternating variable names and values that will be bound
2634 before running the agenda command.
2636 The output gives a line for each selected agenda item. Each
2637 item is a list of comma-separated values, like this:
2639 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2641 category The category of the item
2642 head The headline, without TODO kwd, TAGS and PRIORITY
2643 type The type of the agenda entry, can be
2644 todo selected in TODO match
2645 tagsmatch selected in tags match
2646 diary imported from diary
2647 deadline a deadline on given date
2648 scheduled scheduled on given date
2649 timestamp entry has timestamp on given date
2650 closed entry was closed on given date
2651 upcoming-deadline warning about deadline
2652 past-scheduled forwarded scheduled item
2653 block entry has date block including g. date
2654 todo The todo keyword, if any
2655 tags All tags including inherited ones, separated by colons
2656 date The relevant date, like 2007-2-14
2657 time The time, like 15:00-16:50
2658 extra Sting with extra planning info
2659 priority-l The priority letter if any was given
2660 priority-n The computed numerical priority
2661 agenda-day The day in the agenda where this is listed"
2662 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2663 (org-make-parameter-alist parameters))
2664 (if (> (length cmd-key) 2)
2665 (org-tags-view nil cmd-key)
2666 (org-agenda nil cmd-key)))
2667 (set-buffer org-agenda-buffer-name)
2668 (let* ((lines (org-split-string (buffer-string) "\n"))
2669 line)
2670 (while (setq line (pop lines))
2671 (catch 'next
2672 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2673 (setq org-agenda-info
2674 (org-fix-agenda-info (text-properties-at 0 line)))
2675 (princ
2676 (mapconcat 'org-agenda-export-csv-mapper
2677 '(org-category txt type todo tags date time extra
2678 priority-letter priority agenda-day)
2679 ","))
2680 (princ "\n")))))
2681 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2683 (defun org-fix-agenda-info (props)
2684 "Make sure all properties on an agenda item have a canonical form.
2685 This ensures the export commands can easily use it."
2686 (let (tmp re)
2687 (when (setq tmp (plist-get props 'tags))
2688 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2689 (when (setq tmp (plist-get props 'date))
2690 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2691 (let ((calendar-date-display-form '(year "-" month "-" day)))
2692 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2694 (setq tmp (calendar-date-string tmp)))
2695 (setq props (plist-put props 'date tmp)))
2696 (when (setq tmp (plist-get props 'day))
2697 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2698 (let ((calendar-date-display-form '(year "-" month "-" day)))
2699 (setq tmp (calendar-date-string tmp)))
2700 (setq props (plist-put props 'day tmp))
2701 (setq props (plist-put props 'agenda-day tmp)))
2702 (when (setq tmp (plist-get props 'txt))
2703 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2704 (plist-put props 'priority-letter (match-string 1 tmp))
2705 (setq tmp (replace-match "" t t tmp)))
2706 (when (and (setq re (plist-get props 'org-todo-regexp))
2707 (setq re (concat "\\`\\.*" re " ?"))
2708 (string-match re tmp))
2709 (plist-put props 'todo (match-string 1 tmp))
2710 (setq tmp (replace-match "" t t tmp)))
2711 (plist-put props 'txt tmp)))
2712 props)
2714 (defun org-agenda-export-csv-mapper (prop)
2715 (let ((res (plist-get org-agenda-info prop)))
2716 (setq res
2717 (cond
2718 ((not res) "")
2719 ((stringp res) res)
2720 (t (prin1-to-string res))))
2721 (while (string-match "," res)
2722 (setq res (replace-match ";" t t res)))
2723 (org-trim res)))
2726 ;;;###autoload
2727 (defun org-store-agenda-views (&rest parameters)
2728 (interactive)
2729 (eval (list 'org-batch-store-agenda-views)))
2731 ;;;###autoload
2732 (defmacro org-batch-store-agenda-views (&rest parameters)
2733 "Run all custom agenda commands that have a file argument."
2734 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2735 (pop-up-frames nil)
2736 (dir default-directory)
2737 (pars (org-make-parameter-alist parameters))
2738 cmd thiscmdkey files opts cmd-or-set)
2739 (save-window-excursion
2740 (while cmds
2741 (setq cmd (pop cmds)
2742 thiscmdkey (car cmd)
2743 cmd-or-set (nth 2 cmd)
2744 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2745 files (nth (if (listp cmd-or-set) 4 5) cmd))
2746 (if (stringp files) (setq files (list files)))
2747 (when files
2748 (org-eval-in-environment (append org-agenda-exporter-settings
2749 opts pars)
2750 (org-agenda nil thiscmdkey))
2751 (set-buffer org-agenda-buffer-name)
2752 (while files
2753 (org-eval-in-environment (append org-agenda-exporter-settings
2754 opts pars)
2755 (org-agenda-write (expand-file-name (pop files) dir) nil t)))
2756 (and (get-buffer org-agenda-buffer-name)
2757 (kill-buffer org-agenda-buffer-name)))))))
2758 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2760 (defun org-agenda-mark-header-line (pos)
2761 "Mark the line at POS as an agenda structure header."
2762 (save-excursion
2763 (goto-char pos)
2764 (put-text-property (point-at-bol) (point-at-eol)
2765 'org-agenda-structural-header t)
2766 (when org-agenda-title-append
2767 (put-text-property (point-at-bol) (point-at-eol)
2768 'org-agenda-title-append org-agenda-title-append))))
2770 (defvar org-mobile-creating-agendas)
2771 (defvar org-agenda-write-buffer-name "Agenda View")
2772 (defun org-agenda-write (file &optional open nosettings)
2773 "Write the current buffer (an agenda view) as a file.
2774 Depending on the extension of the file name, plain text (.txt),
2775 HTML (.html or .htm) or Postscript (.ps) is produced.
2776 If the extension is .ics, run icalendar export over all files used
2777 to construct the agenda and limit the export to entries listed in the
2778 agenda now.
2779 With prefix argument OPEN, open the new file immediately.
2780 If NOSETTINGS is given, do not scope the settings of
2781 `org-agenda-exporter-settings' into the export commands. This is used when
2782 the settings have already been scoped and we do not wish to overrule other,
2783 higher priority settings."
2784 (interactive "FWrite agenda to file: \nP")
2785 (if (not (file-writable-p file))
2786 (error "Cannot write agenda to file %s" file))
2787 (org-let (if nosettings nil org-agenda-exporter-settings)
2788 '(save-excursion
2789 (save-window-excursion
2790 (org-agenda-mark-filtered-text)
2791 (let ((bs (copy-sequence (buffer-string))) beg)
2792 (org-agenda-unmark-filtered-text)
2793 (with-temp-buffer
2794 (rename-buffer org-agenda-write-buffer-name t)
2795 (set-buffer-modified-p nil)
2796 (insert bs)
2797 (org-agenda-remove-marked-text 'org-filtered)
2798 (while (setq beg (text-property-any (point-min) (point-max)
2799 'org-filtered t))
2800 (delete-region
2801 beg (or (next-single-property-change beg 'org-filtered)
2802 (point-max))))
2803 (run-hooks 'org-agenda-before-write-hook)
2804 (cond
2805 ((org-bound-and-true-p org-mobile-creating-agendas)
2806 (org-mobile-write-agenda-for-mobile file))
2807 ((string-match "\\.html?\\'" file)
2808 (require 'htmlize)
2809 (set-buffer (htmlize-buffer (current-buffer)))
2811 (when (and org-agenda-export-html-style
2812 (string-match "<style>" org-agenda-export-html-style))
2813 ;; replace <style> section with org-agenda-export-html-style
2814 (goto-char (point-min))
2815 (kill-region (- (search-forward "<style") 6)
2816 (search-forward "</style>"))
2817 (insert org-agenda-export-html-style))
2818 (write-file file)
2819 (kill-buffer (current-buffer))
2820 (message "HTML written to %s" file))
2821 ((string-match "\\.ps\\'" file)
2822 (require 'ps-print)
2823 (ps-print-buffer-with-faces file)
2824 (message "Postscript written to %s" file))
2825 ((string-match "\\.pdf\\'" file)
2826 (require 'ps-print)
2827 (ps-print-buffer-with-faces
2828 (concat (file-name-sans-extension file) ".ps"))
2829 (call-process "ps2pdf" nil nil nil
2830 (expand-file-name
2831 (concat (file-name-sans-extension file) ".ps"))
2832 (expand-file-name file))
2833 (delete-file (concat (file-name-sans-extension file) ".ps"))
2834 (message "PDF written to %s" file))
2835 ((string-match "\\.ics\\'" file)
2836 (require 'org-icalendar)
2837 (let ((org-agenda-marker-table
2838 (org-create-marker-find-array
2839 (org-agenda-collect-markers)))
2840 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2841 (org-combined-agenda-icalendar-file file))
2842 (apply 'org-export-icalendar 'combine
2843 (org-agenda-files nil 'ifmode))))
2845 (let ((bs (buffer-string)))
2846 (find-file file)
2847 (erase-buffer)
2848 (insert bs)
2849 (save-buffer 0)
2850 (kill-buffer (current-buffer))
2851 (message "Plain text written to %s" file))))))))
2852 (set-buffer org-agenda-buffer-name))
2853 (when open (org-open-file file)))
2855 (defvar org-agenda-tag-filter-overlays nil)
2856 (defvar org-agenda-cat-filter-overlays nil)
2858 (defun org-agenda-mark-filtered-text ()
2859 "Mark all text hidden by filtering with a text property."
2860 (let ((inhibit-read-only t))
2861 (mapc
2862 (lambda (o)
2863 (when (equal (overlay-buffer o) (current-buffer))
2864 (put-text-property
2865 (overlay-start o) (overlay-end o)
2866 'org-filtered t)))
2867 (append org-agenda-tag-filter-overlays
2868 org-agenda-cat-filter-overlays))))
2870 (defun org-agenda-unmark-filtered-text ()
2871 "Remove the filtering text property."
2872 (let ((inhibit-read-only t))
2873 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2875 (defun org-agenda-remove-marked-text (property &optional value)
2876 "Delete all text marked with VALUE of PROPERTY.
2877 VALUE defaults to t."
2878 (let (beg)
2879 (setq value (or value t))
2880 (while (setq beg (text-property-any (point-min) (point-max)
2881 property value))
2882 (delete-region
2883 beg (or (next-single-property-change beg 'org-filtered)
2884 (point-max))))))
2886 (defun org-agenda-add-entry-text ()
2887 "Add entry text to agenda lines.
2888 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2889 entry text following headings shown in the agenda.
2890 Drawers will be excluded, also the line with scheduling/deadline info."
2891 (when (and (> org-agenda-add-entry-text-maxlines 0)
2892 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2893 (let (m txt)
2894 (goto-char (point-min))
2895 (while (not (eobp))
2896 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2897 (beginning-of-line 2)
2898 (setq txt (org-agenda-get-some-entry-text
2899 m org-agenda-add-entry-text-maxlines " > "))
2900 (end-of-line 1)
2901 (if (string-match "\\S-" txt)
2902 (insert "\n" txt)
2903 (or (eobp) (forward-char 1))))))))
2905 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2906 &rest keep)
2907 "Extract entry text from MARKER, at most N-LINES lines.
2908 This will ignore drawers etc, just get the text.
2909 If INDENT is given, prefix every line with this string. If KEEP is
2910 given, it is a list of symbols, defining stuff that should not be
2911 removed from the entry content. Currently only `planning' is allowed here."
2912 (let (txt drawer-re kwd-time-re ind)
2913 (save-excursion
2914 (with-current-buffer (marker-buffer marker)
2915 (if (not (eq major-mode 'org-mode))
2916 (setq txt "")
2917 (save-excursion
2918 (save-restriction
2919 (widen)
2920 (goto-char marker)
2921 (end-of-line 1)
2922 (setq txt (buffer-substring
2923 (min (1+ (point)) (point-max))
2924 (progn (outline-next-heading) (point)))
2925 drawer-re org-drawer-regexp
2926 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2927 ".*\n?"))
2928 (with-temp-buffer
2929 (insert txt)
2930 (when org-agenda-add-entry-text-descriptive-links
2931 (goto-char (point-min))
2932 (while (org-activate-bracket-links (point-max))
2933 (add-text-properties (match-beginning 0) (match-end 0)
2934 '(face org-link))))
2935 (goto-char (point-min))
2936 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2937 (set-text-properties (match-beginning 0) (match-end 0)
2938 nil))
2939 (goto-char (point-min))
2940 (while (re-search-forward drawer-re nil t)
2941 (delete-region
2942 (match-beginning 0)
2943 (progn (re-search-forward
2944 "^[ \t]*:END:.*\n?" nil 'move)
2945 (point))))
2946 (unless (member 'planning keep)
2947 (goto-char (point-min))
2948 (while (re-search-forward kwd-time-re nil t)
2949 (replace-match "")))
2950 (goto-char (point-min))
2951 (when org-agenda-entry-text-exclude-regexps
2952 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2953 (while (setq re (pop re-list))
2954 (goto-char (point-min))
2955 (while (re-search-forward re nil t)
2956 (replace-match "")))))
2957 (goto-char (point-max))
2958 (skip-chars-backward " \t\n")
2959 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2961 ;; find and remove min common indentation
2962 (goto-char (point-min))
2963 (untabify (point-min) (point-max))
2964 (setq ind (org-get-indentation))
2965 (while (not (eobp))
2966 (unless (looking-at "[ \t]*$")
2967 (setq ind (min ind (org-get-indentation))))
2968 (beginning-of-line 2))
2969 (goto-char (point-min))
2970 (while (not (eobp))
2971 (unless (looking-at "[ \t]*$")
2972 (move-to-column ind)
2973 (delete-region (point-at-bol) (point)))
2974 (beginning-of-line 2))
2976 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2978 (goto-char (point-min))
2979 (when indent
2980 (while (and (not (eobp)) (re-search-forward "^" nil t))
2981 (replace-match indent t t)))
2982 (goto-char (point-min))
2983 (while (looking-at "[ \t]*\n") (replace-match ""))
2984 (goto-char (point-max))
2985 (when (> (org-current-line)
2986 n-lines)
2987 (org-goto-line (1+ n-lines))
2988 (backward-char 1))
2989 (setq txt (buffer-substring (point-min) (point)))))))))
2990 txt))
2992 (defun org-agenda-collect-markers ()
2993 "Collect the markers pointing to entries in the agenda buffer."
2994 (let (m markers)
2995 (save-excursion
2996 (goto-char (point-min))
2997 (while (not (eobp))
2998 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2999 (org-get-at-bol 'org-marker)))
3000 (push m markers))
3001 (beginning-of-line 2)))
3002 (nreverse markers)))
3004 (defun org-create-marker-find-array (marker-list)
3005 "Create a alist of files names with all marker positions in that file."
3006 (let (f tbl m a p)
3007 (while (setq m (pop marker-list))
3008 (setq p (marker-position m)
3009 f (buffer-file-name (or (buffer-base-buffer
3010 (marker-buffer m))
3011 (marker-buffer m))))
3012 (if (setq a (assoc f tbl))
3013 (push (marker-position m) (cdr a))
3014 (push (list f p) tbl)))
3015 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3016 tbl)))
3018 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3019 (defun org-check-agenda-marker-table ()
3020 "Check of the current entry is on the marker list."
3021 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3023 (and (setq a (assoc file org-agenda-marker-table))
3024 (save-match-data
3025 (save-excursion
3026 (org-back-to-heading t)
3027 (member (point) (cdr a)))))))
3029 (defun org-check-for-org-mode ()
3030 "Make sure current buffer is in org-mode. Error if not."
3031 (or (eq major-mode 'org-mode)
3032 (error "Cannot execute org-mode agenda command on buffer in %s"
3033 major-mode)))
3035 (defun org-fit-agenda-window ()
3036 "Fit the window to the buffer size."
3037 (and (memq org-agenda-window-setup '(reorganize-frame))
3038 (fboundp 'fit-window-to-buffer)
3039 (org-fit-window-to-buffer
3041 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3042 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3044 ;;; Agenda prepare and finalize
3046 (defvar org-agenda-multi nil) ; dynamically scoped
3047 (defvar org-agenda-buffer-name "*Org Agenda*")
3048 (defvar org-pre-agenda-window-conf nil)
3049 (defvar org-agenda-columns-active nil)
3050 (defvar org-agenda-name nil)
3051 (defvar org-agenda-tag-filter nil)
3052 (defvar org-agenda-category-filter nil)
3053 (defvar org-agenda-tag-filter-while-redo nil)
3054 (defvar org-agenda-tag-filter-preset nil
3055 "A preset of the tags filter used for secondary agenda filtering.
3056 This must be a list of strings, each string must be a single tag preceded
3057 by \"+\" or \"-\".
3058 This variable should not be set directly, but agenda custom commands can
3059 bind it in the options section. The preset filter is a global property of
3060 the entire agenda view. In a block agenda, it will not work reliably to
3061 define a filter for one of the individual blocks. You need to set it in
3062 the global options and expect it to be applied to the entire view.")
3064 (defvar org-agenda-category-filter-preset nil
3065 "A preset of the category filter used for secondary agenda filtering.
3066 This must be a list of strings, each string must be a single category
3067 preceded by \"+\" or \"-\".
3068 This variable should not be set directly, but agenda custom commands can
3069 bind it in the options section. The preset filter is a global property of
3070 the entire agenda view. In a block agenda, it will not work reliably to
3071 define a filter for one of the individual blocks. You need to set it in
3072 the global options and expect it to be applied to the entire view.")
3074 (defun org-prepare-agenda (&optional name)
3075 (setq org-todo-keywords-for-agenda nil)
3076 (setq org-done-keywords-for-agenda nil)
3077 (setq org-drawers-for-agenda nil)
3078 (unless org-agenda-persistent-filter
3079 (setq org-agenda-tag-filter nil
3080 org-agenda-category-filter nil))
3081 (put 'org-agenda-tag-filter :preset-filter org-agenda-tag-filter-preset)
3082 (put 'org-agenda-category-filter :preset-filter org-agenda-category-filter-preset)
3083 (if org-agenda-multi
3084 (progn
3085 (setq buffer-read-only nil)
3086 (goto-char (point-max))
3087 (unless (or (bobp) org-agenda-compact-blocks
3088 (not org-agenda-block-separator))
3089 (insert "\n"
3090 (if (stringp org-agenda-block-separator)
3091 org-agenda-block-separator
3092 (make-string (window-width) org-agenda-block-separator))
3093 "\n"))
3094 (narrow-to-region (point) (point-max)))
3095 (org-agenda-reset-markers)
3096 (setq org-agenda-contributing-files nil)
3097 (setq org-agenda-columns-active nil)
3098 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3099 (setq org-todo-keywords-for-agenda
3100 (org-uniquify org-todo-keywords-for-agenda))
3101 (setq org-done-keywords-for-agenda
3102 (org-uniquify org-done-keywords-for-agenda))
3103 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3104 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3105 (awin (get-buffer-window abuf)))
3106 (cond
3107 ((equal (current-buffer) abuf) nil)
3108 (awin (select-window awin))
3109 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3110 ((equal org-agenda-window-setup 'current-window)
3111 (org-pop-to-buffer-same-window abuf))
3112 ((equal org-agenda-window-setup 'other-window)
3113 (org-switch-to-buffer-other-window abuf))
3114 ((equal org-agenda-window-setup 'other-frame)
3115 (switch-to-buffer-other-frame abuf))
3116 ((equal org-agenda-window-setup 'reorganize-frame)
3117 (delete-other-windows)
3118 (org-switch-to-buffer-other-window abuf)))
3119 ;; additional test in case agenda is invoked from within agenda
3120 ;; buffer via elisp link
3121 (unless (equal (current-buffer) abuf)
3122 (org-pop-to-buffer-same-window abuf)))
3123 (setq buffer-read-only nil)
3124 (let ((inhibit-read-only t)) (erase-buffer))
3125 (org-agenda-mode)
3126 (and name (not org-agenda-name)
3127 (org-set-local 'org-agenda-name name)))
3128 (setq buffer-read-only nil))
3130 (defun org-finalize-agenda ()
3131 "Finishing touch for the agenda buffer, called just before displaying it."
3132 (unless org-agenda-multi
3133 (save-excursion
3134 (let ((inhibit-read-only t))
3135 (goto-char (point-min))
3136 (while (org-activate-bracket-links (point-max))
3137 (add-text-properties (match-beginning 0) (match-end 0)
3138 '(face org-link)))
3139 (org-agenda-align-tags)
3140 (unless org-agenda-with-colors
3141 (remove-text-properties (point-min) (point-max) '(face nil))))
3142 (if (and (boundp 'org-agenda-overriding-columns-format)
3143 org-agenda-overriding-columns-format)
3144 (org-set-local 'org-agenda-overriding-columns-format
3145 org-agenda-overriding-columns-format))
3146 (if (and (boundp 'org-agenda-view-columns-initially)
3147 org-agenda-view-columns-initially)
3148 (org-agenda-columns))
3149 (when org-agenda-fontify-priorities
3150 (org-agenda-fontify-priorities))
3151 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3152 (org-agenda-dim-blocked-tasks))
3153 (org-agenda-mark-clocking-task)
3154 (when org-agenda-entry-text-mode
3155 (org-agenda-entry-text-hide)
3156 (org-agenda-entry-text-show))
3157 (if (functionp 'org-habit-insert-consistency-graphs)
3158 (org-habit-insert-consistency-graphs))
3159 (run-hooks 'org-finalize-agenda-hook)
3160 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3161 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3162 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3163 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3164 (org-agenda-filter-apply org-agenda-category-filter 'category))
3167 (defun org-agenda-mark-clocking-task ()
3168 "Mark the current clock entry in the agenda if it is present."
3169 (mapc (lambda (o)
3170 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3171 (delete-overlay o)))
3172 (overlays-in (point-min) (point-max)))
3173 (when (marker-buffer org-clock-hd-marker)
3174 (save-excursion
3175 (goto-char (point-min))
3176 (let (s ov)
3177 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3178 (goto-char s)
3179 (when (equal (org-get-at-bol 'org-hd-marker)
3180 org-clock-hd-marker)
3181 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3182 (overlay-put ov 'type 'org-agenda-clocking)
3183 (overlay-put ov 'face 'org-agenda-clocking)
3184 (overlay-put ov 'help-echo
3185 "The clock is running in this item")))))))
3187 (defun org-agenda-fontify-priorities ()
3188 "Make highest priority lines bold, and lowest italic."
3189 (interactive)
3190 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3191 (delete-overlay o)))
3192 (overlays-in (point-min) (point-max)))
3193 (save-excursion
3194 (let ((inhibit-read-only t)
3195 b e p ov h l)
3196 (goto-char (point-min))
3197 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3198 (setq h (or (get-char-property (point) 'org-highest-priority)
3199 org-highest-priority)
3200 l (or (get-char-property (point) 'org-lowest-priority)
3201 org-lowest-priority)
3202 p (string-to-char (match-string 1))
3203 b (match-beginning 0)
3204 e (if (eq org-agenda-fontify-priorities 'cookies)
3205 (match-end 0)
3206 (point-at-eol))
3207 ov (make-overlay b e))
3208 (overlay-put
3209 ov 'face
3210 (cond ((org-face-from-face-or-color
3211 'priority nil
3212 (cdr (assoc p org-priority-faces))))
3213 ((and (listp org-agenda-fontify-priorities)
3214 (org-face-from-face-or-color
3215 'priority nil
3216 (cdr (assoc p org-agenda-fontify-priorities)))))
3217 ((equal p l) 'italic)
3218 ((equal p h) 'bold)))
3219 (overlay-put ov 'org-type 'org-priority)))))
3221 (defun org-agenda-dim-blocked-tasks ()
3222 "Dim currently blocked TODO's in the agenda display."
3223 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3224 (delete-overlay o)))
3225 (overlays-in (point-min) (point-max)))
3226 (save-excursion
3227 (let ((inhibit-read-only t)
3228 (org-depend-tag-blocked nil)
3229 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3230 org-blocked-by-checkboxes
3231 invis1 b e p ov h l)
3232 (goto-char (point-min))
3233 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3234 (and pos (goto-char (1+ pos))))
3235 (setq org-blocked-by-checkboxes nil invis1 invis)
3236 (let ((marker (org-get-at-bol 'org-hd-marker)))
3237 (when (and marker
3238 (with-current-buffer (marker-buffer marker)
3239 (save-excursion (goto-char marker)
3240 (org-entry-blocked-p))))
3241 (if org-blocked-by-checkboxes (setq invis1 nil))
3242 (setq b (if invis1
3243 (max (point-min) (1- (point-at-bol)))
3244 (point-at-bol))
3245 e (point-at-eol)
3246 ov (make-overlay b e))
3247 (if invis1
3248 (overlay-put ov 'invisible t)
3249 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3250 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3252 (defvar org-agenda-skip-function nil
3253 "Function to be called at each match during agenda construction.
3254 If this function returns nil, the current match should not be skipped.
3255 Otherwise, the function must return a position from where the search
3256 should be continued.
3257 This may also be a Lisp form, it will be evaluated.
3258 Never set this variable using `setq' or so, because then it will apply
3259 to all future agenda commands. If you do want a global skipping condition,
3260 use the option `org-agenda-skip-function-global' instead.
3261 The correct usage for `org-agenda-skip-function' is to bind it with
3262 `let' to scope it dynamically into the agenda-constructing command.
3263 A good way to set it is through options in `org-agenda-custom-commands'.")
3265 (defun org-agenda-skip ()
3266 "Throw to `:skip' in places that should be skipped.
3267 Also moves point to the end of the skipped region, so that search can
3268 continue from there."
3269 (let ((p (point-at-bol)) to)
3270 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3271 (get-text-property p :org-archived)
3272 (org-end-of-subtree t)
3273 (throw :skip t))
3274 (and org-agenda-skip-comment-trees
3275 (get-text-property p :org-comment)
3276 (org-end-of-subtree t)
3277 (throw :skip t))
3278 (if (equal (char-after p) ?#) (throw :skip t))
3279 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3280 (org-agenda-skip-eval org-agenda-skip-function)))
3281 (goto-char to)
3282 (throw :skip t))))
3284 (defun org-agenda-skip-eval (form)
3285 "If FORM is a function or a list, call (or eval) is and return result.
3286 `save-excursion' and `save-match-data' are wrapped around the call, so point
3287 and match data are returned to the previous state no matter what these
3288 functions do."
3289 (let (fp)
3290 (and form
3291 (or (setq fp (functionp form))
3292 (consp form))
3293 (save-excursion
3294 (save-match-data
3295 (if fp
3296 (funcall form)
3297 (eval form)))))))
3299 (defvar org-agenda-markers nil
3300 "List of all currently active markers created by `org-agenda'.")
3301 (defvar org-agenda-last-marker-time (org-float-time)
3302 "Creation time of the last agenda marker.")
3304 (defun org-agenda-new-marker (&optional pos)
3305 "Return a new agenda marker.
3306 Org-mode keeps a list of these markers and resets them when they are
3307 no longer in use."
3308 (let ((m (copy-marker (or pos (point)))))
3309 (setq org-agenda-last-marker-time (org-float-time))
3310 (push m org-agenda-markers)
3313 (defun org-agenda-reset-markers ()
3314 "Reset markers created by `org-agenda'."
3315 (while org-agenda-markers
3316 (move-marker (pop org-agenda-markers) nil)))
3318 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3319 "Save relative positions of markers in region."
3320 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3321 org-agenda-markers))
3323 ;;; Entry text mode
3325 (defun org-agenda-entry-text-show-here ()
3326 "Add some text from the entry as context to the current line."
3327 (let (m txt o)
3328 (setq m (org-get-at-bol 'org-hd-marker))
3329 (unless (marker-buffer m)
3330 (error "No marker points to an entry here"))
3331 (setq txt (concat "\n" (org-no-properties
3332 (org-agenda-get-some-entry-text
3333 m org-agenda-entry-text-maxlines " > "))))
3334 (when (string-match "\\S-" txt)
3335 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3336 (overlay-put o 'evaporate t)
3337 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3338 (overlay-put o 'after-string txt))))
3340 (defun org-agenda-entry-text-show ()
3341 "Add entry context for all agenda lines."
3342 (interactive)
3343 (save-excursion
3344 (goto-char (point-max))
3345 (beginning-of-line 1)
3346 (while (not (bobp))
3347 (when (org-get-at-bol 'org-hd-marker)
3348 (org-agenda-entry-text-show-here))
3349 (beginning-of-line 0))))
3351 (defun org-agenda-entry-text-hide ()
3352 "Remove any shown entry context."
3353 (delq nil
3354 (mapcar (lambda (o)
3355 (if (eq (overlay-get o 'org-overlay-type)
3356 'agenda-entry-content)
3357 (progn (delete-overlay o) t)))
3358 (overlays-in (point-min) (point-max)))))
3360 (defun org-agenda-get-day-face (date)
3361 "Return the face DATE should be displayed with."
3362 (or (and (functionp org-agenda-day-face-function)
3363 (funcall org-agenda-day-face-function date))
3364 (cond ((org-agenda-todayp date)
3365 'org-agenda-date-today)
3366 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3367 'org-agenda-date-weekend)
3368 (t 'org-agenda-date))))
3370 ;;; Agenda timeline
3372 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3374 (defun org-timeline (&optional dotodo)
3375 "Show a time-sorted view of the entries in the current org file.
3376 Only entries with a time stamp of today or later will be listed. With
3377 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3378 under the current date.
3379 If the buffer contains an active region, only check the region for
3380 dates."
3381 (interactive "P")
3382 (org-compile-prefix-format 'timeline)
3383 (org-set-sorting-strategy 'timeline)
3384 (let* ((dopast t)
3385 (doclosed org-agenda-show-log)
3386 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3387 (current-buffer))))
3388 (date (calendar-current-date))
3389 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3390 (end (if (org-region-active-p) (region-end) (point-max)))
3391 (day-numbers (org-get-all-dates beg end 'no-ranges
3392 t doclosed ; always include today
3393 org-timeline-show-empty-dates))
3394 (org-deadline-warning-days 0)
3395 (org-agenda-only-exact-dates t)
3396 (today (org-today))
3397 (past t)
3398 args
3399 s e rtn d emptyp)
3400 (setq org-agenda-redo-command
3401 (list 'progn
3402 (list 'org-switch-to-buffer-other-window (current-buffer))
3403 (list 'org-timeline (list 'quote dotodo))))
3404 (if (not dopast)
3405 ;; Remove past dates from the list of dates.
3406 (setq day-numbers (delq nil (mapcar (lambda(x)
3407 (if (>= x today) x nil))
3408 day-numbers))))
3409 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3410 (if doclosed (push :closed args))
3411 (push :timestamp args)
3412 (push :deadline args)
3413 (push :scheduled args)
3414 (push :sexp args)
3415 (if dotodo (push :todo args))
3416 (insert "Timeline of file " entry "\n")
3417 (add-text-properties (point-min) (point)
3418 (list 'face 'org-agenda-structure))
3419 (org-agenda-mark-header-line (point-min))
3420 (while (setq d (pop day-numbers))
3421 (if (and (listp d) (eq (car d) :omitted))
3422 (progn
3423 (setq s (point))
3424 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3425 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3426 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3427 (if (and (>= d today)
3428 dopast
3429 past)
3430 (progn
3431 (setq past nil)
3432 (insert (make-string 79 ?-) "\n")))
3433 (setq date (calendar-gregorian-from-absolute d))
3434 (setq s (point))
3435 (setq rtn (and (not emptyp)
3436 (apply 'org-agenda-get-day-entries entry
3437 date args)))
3438 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3439 (progn
3440 (insert
3441 (if (stringp org-agenda-format-date)
3442 (format-time-string org-agenda-format-date
3443 (org-time-from-absolute date))
3444 (funcall org-agenda-format-date date))
3445 "\n")
3446 (put-text-property s (1- (point)) 'face
3447 (org-agenda-get-day-face date))
3448 (put-text-property s (1- (point)) 'org-date-line t)
3449 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3450 (if (equal d today)
3451 (put-text-property s (1- (point)) 'org-today t))
3452 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3453 (put-text-property s (1- (point)) 'day d)))))
3454 (goto-char (point-min))
3455 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3456 (point-min)))
3457 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3458 (org-finalize-agenda)
3459 (setq buffer-read-only t)))
3461 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3462 "Return a list of all relevant day numbers from BEG to END buffer positions.
3463 If NO-RANGES is non-nil, include only the start and end dates of a range,
3464 not every single day in the range. If FORCE-TODAY is non-nil, make
3465 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3466 inactive time stamps (those in square brackets) are included.
3467 When EMPTY is non-nil, also include days without any entries."
3468 (let ((re (concat
3469 (if pre-re pre-re "")
3470 (if inactive org-ts-regexp-both org-ts-regexp)))
3471 dates dates1 date day day1 day2 ts1 ts2 pos)
3472 (if force-today
3473 (setq dates (list (org-today))))
3474 (save-excursion
3475 (goto-char beg)
3476 (while (re-search-forward re end t)
3477 (setq day (time-to-days (org-time-string-to-time
3478 (substring (match-string 1) 0 10)
3479 (current-buffer) (match-beginning 0))))
3480 (or (memq day dates) (push day dates)))
3481 (unless no-ranges
3482 (goto-char beg)
3483 (while (re-search-forward org-tr-regexp end t)
3484 (setq pos (match-beginning 0))
3485 (setq ts1 (substring (match-string 1) 0 10)
3486 ts2 (substring (match-string 2) 0 10)
3487 day1 (time-to-days (org-time-string-to-time
3488 ts1 (current-buffer) pos))
3489 day2 (time-to-days (org-time-string-to-time
3490 ts2 (current-buffer) pos)))
3491 (while (< (setq day1 (1+ day1)) day2)
3492 (or (memq day1 dates) (push day1 dates)))))
3493 (setq dates (sort dates '<))
3494 (when empty
3495 (while (setq day (pop dates))
3496 (setq day2 (car dates))
3497 (push day dates1)
3498 (when (and day2 empty)
3499 (if (or (eq empty t)
3500 (and (numberp empty) (<= (- day2 day) empty)))
3501 (while (< (setq day (1+ day)) day2)
3502 (push (list day) dates1))
3503 (push (cons :omitted (- day2 day)) dates1))))
3504 (setq dates (nreverse dates1)))
3505 dates)))
3507 ;;; Agenda Daily/Weekly
3509 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3510 "Start day for the agenda view.
3511 Custom commands can set this variable in the options section.")
3512 (defvar org-starting-day nil) ; local variable in the agenda buffer
3513 (defvar org-agenda-current-span nil
3514 "The current span used in the agenda view.") ; local variable in the agenda buffer
3515 (defvar org-arg-loc nil) ; local variable
3517 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3518 "List of types searched for when creating the daily/weekly agenda.
3519 This variable is a list of symbols that controls the types of
3520 items that appear in the daily/weekly agenda. Allowed symbols in this
3521 list are are
3523 :timestamp List items containing a date stamp or date range matching
3524 the selected date. This includes sexp entries in
3525 angular brackets.
3527 :sexp List entries resulting from plain diary-like sexps.
3529 :deadline List deadline due on that date. When the date is today,
3530 also list any deadlines past due, or due within
3531 `org-deadline-warning-days'. `:deadline' must appear before
3532 `:scheduled' if the setting of
3533 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3534 any effect.
3536 :scheduled List all items which are scheduled for the given date.
3537 The diary for *today* also contains items which were
3538 scheduled earlier and are not yet marked DONE.
3540 By default, all four types are turned on.
3542 Never set this variable globally using `setq', because then it
3543 will apply to all future agenda commands. Instead, bind it with
3544 `let' to scope it dynamically into the agenda-constructing
3545 command. A good way to set it is through options in
3546 `org-agenda-custom-commands'. For a more flexible (though
3547 somewhat less efficient) way of determining what is included in
3548 the daily/weekly agenda, see `org-agenda-skip-function'.")
3550 ;;;###autoload
3551 (defun org-agenda-list (&optional arg start-day span)
3552 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3553 The view will be for the current day or week, but from the overview buffer
3554 you will be able to go to other days/weeks.
3556 With a numeric prefix argument in an interactive call, the agenda will
3557 span ARG days. Lisp programs should instead specify SPAN to change
3558 the number of days. SPAN defaults to `org-agenda-span'.
3560 START-DAY defaults to TODAY, or to the most recent match for the weekday
3561 given in `org-agenda-start-on-weekday'."
3562 (interactive "P")
3563 (if (and (integerp arg) (> arg 0))
3564 (setq span arg arg nil))
3565 (setq start-day (or start-day org-agenda-start-day))
3566 (if org-agenda-overriding-arguments
3567 (setq arg (car org-agenda-overriding-arguments)
3568 start-day (nth 1 org-agenda-overriding-arguments)
3569 span (nth 2 org-agenda-overriding-arguments)))
3570 (if (stringp start-day)
3571 ;; Convert to an absolute day number
3572 (setq start-day (time-to-days (org-read-date nil t start-day))))
3573 (setq org-agenda-last-arguments (list arg start-day span))
3574 (org-compile-prefix-format 'agenda)
3575 (org-set-sorting-strategy 'agenda)
3576 (let* ((span (org-agenda-ndays-to-span
3577 (or span org-agenda-ndays org-agenda-span)))
3578 (today (org-today))
3579 (sd (or start-day today))
3580 (ndays (org-agenda-span-to-ndays span sd))
3581 (org-agenda-start-on-weekday
3582 (if (eq ndays 7)
3583 org-agenda-start-on-weekday))
3584 (thefiles (org-agenda-files nil 'ifmode))
3585 (files thefiles)
3586 (start (if (or (null org-agenda-start-on-weekday)
3587 (< ndays 7))
3589 (let* ((nt (calendar-day-of-week
3590 (calendar-gregorian-from-absolute sd)))
3591 (n1 org-agenda-start-on-weekday)
3592 (d (- nt n1)))
3593 (- sd (+ (if (< d 0) 7 0) d)))))
3594 (day-numbers (list start))
3595 (day-cnt 0)
3596 (inhibit-redisplay (not debug-on-error))
3597 s e rtn rtnall file date d start-pos end-pos todayp
3598 clocktable-start clocktable-end filter)
3599 (setq org-agenda-redo-command
3600 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3601 (dotimes (n (1- ndays))
3602 (push (1+ (car day-numbers)) day-numbers))
3603 (setq day-numbers (nreverse day-numbers))
3604 (setq clocktable-start (car day-numbers)
3605 clocktable-end (1+ (or (org-last day-numbers) 0)))
3606 (org-prepare-agenda "Day/Week")
3607 (org-set-local 'org-starting-day (car day-numbers))
3608 (org-set-local 'org-arg-loc arg)
3609 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3610 (unless org-agenda-compact-blocks
3611 (let* ((d1 (car day-numbers))
3612 (d2 (org-last day-numbers))
3613 (w1 (org-days-to-iso-week d1))
3614 (w2 (org-days-to-iso-week d2)))
3615 (setq s (point))
3616 (if org-agenda-overriding-header
3617 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3618 nil 'face 'org-agenda-structure) "\n")
3619 (insert (org-agenda-span-name span)
3620 "-agenda"
3621 (if (< (- d2 d1) 350)
3622 (if (= w1 w2)
3623 (format " (W%02d)" w1)
3624 (format " (W%02d-W%02d)" w1 w2))
3626 ":\n")))
3627 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3628 'org-date-line t))
3629 (org-agenda-mark-header-line s))
3630 (while (setq d (pop day-numbers))
3631 (setq date (calendar-gregorian-from-absolute d)
3632 s (point))
3633 (if (or (setq todayp (= d today))
3634 (and (not start-pos) (= d sd)))
3635 (setq start-pos (point))
3636 (if (and start-pos (not end-pos))
3637 (setq end-pos (point))))
3638 (setq files thefiles
3639 rtnall nil)
3640 (while (setq file (pop files))
3641 (catch 'nextfile
3642 (org-check-agenda-file file)
3643 (let ((org-agenda-entry-types org-agenda-entry-types))
3644 (unless org-agenda-include-deadlines
3645 (setq org-agenda-entry-types
3646 (delq :deadline org-agenda-entry-types)))
3647 (cond
3648 ((memq org-agenda-show-log '(only clockcheck))
3649 (setq rtn (org-agenda-get-day-entries
3650 file date :closed)))
3651 (org-agenda-show-log
3652 (setq rtn (apply 'org-agenda-get-day-entries
3653 file date
3654 (append '(:closed) org-agenda-entry-types))))
3656 (setq rtn (apply 'org-agenda-get-day-entries
3657 file date
3658 org-agenda-entry-types)))))
3659 (setq rtnall (append rtnall rtn)))) ;; all entries
3660 (if org-agenda-include-diary
3661 (let ((org-agenda-search-headline-for-time t))
3662 (require 'diary-lib)
3663 (setq rtn (org-get-entries-from-diary date))
3664 (setq rtnall (append rtnall rtn))))
3665 (if (or rtnall org-agenda-show-all-dates)
3666 (progn
3667 (setq day-cnt (1+ day-cnt))
3668 (insert
3669 (if (stringp org-agenda-format-date)
3670 (format-time-string org-agenda-format-date
3671 (org-time-from-absolute date))
3672 (funcall org-agenda-format-date date))
3673 "\n")
3674 (put-text-property s (1- (point)) 'face
3675 (org-agenda-get-day-face date))
3676 (put-text-property s (1- (point)) 'org-date-line t)
3677 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3678 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3679 (when todayp
3680 (put-text-property s (1- (point)) 'org-today t))
3681 (if rtnall (insert ;; all entries
3682 (org-finalize-agenda-entries
3683 (org-agenda-add-time-grid-maybe
3684 rtnall ndays todayp))
3685 "\n"))
3686 (put-text-property s (1- (point)) 'day d)
3687 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3688 (when (and org-agenda-clockreport-mode clocktable-start)
3689 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3690 ;; the above line is to ensure the restricted range!
3691 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3692 tbl)
3693 (setq p (org-plist-delete p :block))
3694 (setq p (plist-put p :tstart clocktable-start))
3695 (setq p (plist-put p :tend clocktable-end))
3696 (setq p (plist-put p :scope 'agenda))
3697 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3698 (setq filter (or org-agenda-tag-filter-while-redo
3699 (get 'org-agenda-tag-filter :preset-filter))))
3700 (setq p (plist-put p :tags (mapconcat (lambda (x)
3701 (if (string-match "[<>=]" x)
3704 filter ""))))
3705 (setq tbl (apply 'org-get-clocktable p))
3706 (insert tbl)))
3707 (goto-char (point-min))
3708 (or org-agenda-multi (org-fit-agenda-window))
3709 (unless (and (pos-visible-in-window-p (point-min))
3710 (pos-visible-in-window-p (point-max)))
3711 (goto-char (1- (point-max)))
3712 (recenter -1)
3713 (if (not (pos-visible-in-window-p (or start-pos 1)))
3714 (progn
3715 (goto-char (or start-pos 1))
3716 (recenter 1))))
3717 (goto-char (or start-pos 1))
3718 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3719 (if (eq org-agenda-show-log 'clockcheck)
3720 (org-agenda-show-clocking-issues))
3721 (org-finalize-agenda)
3722 (setq buffer-read-only t)
3723 (message "")))
3725 (defun org-agenda-ndays-to-span (n)
3726 "Return a span symbol for a span of N days, or N if none matches."
3727 (cond ((symbolp n) n)
3728 ((= n 1) 'day)
3729 ((= n 7) 'week)
3730 (t n)))
3732 (defun org-agenda-span-to-ndays (span start-day)
3733 "Return ndays from SPAN starting at START-DAY."
3734 (cond ((numberp span) span)
3735 ((eq span 'day) 1)
3736 ((eq span 'week) 7)
3737 ((eq span 'month)
3738 (let ((date (calendar-gregorian-from-absolute start-day)))
3739 (calendar-last-day-of-month (car date) (caddr date))))
3740 ((eq span 'year)
3741 (let ((date (calendar-gregorian-from-absolute start-day)))
3742 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3744 (defun org-agenda-span-name (span)
3745 "Return a SPAN name."
3746 (if (null span)
3748 (if (symbolp span)
3749 (capitalize (symbol-name span))
3750 (format "%d days" span))))
3752 ;;; Agenda word search
3754 (defvar org-agenda-search-history nil)
3755 (defvar org-todo-only nil)
3757 (defvar org-search-syntax-table nil
3758 "Special syntax table for org-mode search.
3759 In this table, we have single quotes not as word constituents, to
3760 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3762 (defun org-search-syntax-table ()
3763 (unless org-search-syntax-table
3764 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3765 (modify-syntax-entry ?' "." org-search-syntax-table)
3766 (modify-syntax-entry ?` "." org-search-syntax-table))
3767 org-search-syntax-table)
3769 (defvar org-agenda-last-search-view-search-was-boolean nil)
3771 ;;;###autoload
3772 (defun org-search-view (&optional todo-only string edit-at)
3773 "Show all entries that contain a phrase or words or regular expressions.
3775 With optional prefix argument TODO-ONLY, only consider entries that are
3776 TODO entries. The argument STRING can be used to pass a default search
3777 string into this function. If EDIT-AT is non-nil, it means that the
3778 user should get a chance to edit this string, with cursor at position
3779 EDIT-AT.
3781 The search string can be viewed either as a phrase that should be found as
3782 is, or it can be broken into a number of snippets, each of which must match
3783 in a Boolean way to select an entry. The default depends on the variable
3784 `org-agenda-search-view-always-boolean'.
3785 Even if this is turned off (the default) you can always switch to
3786 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3788 The default is a direct search of the whole phrase, where each space in
3789 the search string can expand to an arbitrary amount of whitespace,
3790 including newlines.
3792 If using a Boolean search, the search string is split on whitespace and
3793 each snippet is searched separately, with logical AND to select an entry.
3794 Words prefixed with a minus must *not* occur in the entry. Words without
3795 a prefix or prefixed with a plus must occur in the entry. Matching is
3796 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3797 match whole words, not parts of a word) if
3798 `org-agenda-search-view-force-full-words' is set (default is nil).
3800 Boolean search snippets enclosed by curly braces are interpreted as
3801 regular expressions that must or (when preceded with \"-\") must not
3802 match in the entry. Snippets enclosed into double quotes will be taken
3803 as a whole, to include whitespace.
3805 - If the search string starts with an asterisk, search only in headlines.
3806 - If (possibly after the leading star) the search string starts with an
3807 exclamation mark, this also means to look at TODO entries only, an effect
3808 that can also be achieved with a prefix argument.
3809 - If (possibly after star and exclamation mark) the search string starts
3810 with a colon, this will mean that the (non-regexp) snippets of the
3811 Boolean search must match as full words.
3813 This command searches the agenda files, and in addition the files listed
3814 in `org-agenda-text-search-extra-files'."
3815 (interactive "P")
3816 (org-compile-prefix-format 'search)
3817 (org-set-sorting-strategy 'search)
3818 (org-prepare-agenda "SEARCH")
3819 (let* ((props (list 'face nil
3820 'done-face 'org-agenda-done
3821 'org-not-done-regexp org-not-done-regexp
3822 'org-todo-regexp org-todo-regexp
3823 'org-complex-heading-regexp org-complex-heading-regexp
3824 'mouse-face 'highlight
3825 'help-echo (format "mouse-2 or RET jump to location")))
3826 (full-words org-agenda-search-view-force-full-words)
3827 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3828 regexp rtn rtnall files file pos
3829 marker category org-category-pos tags c neg re boolean
3830 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3831 (unless (and (not edit-at)
3832 (stringp string)
3833 (string-match "\\S-" string))
3834 (setq string (read-string
3835 (if org-agenda-search-view-always-boolean
3836 "[+-]Word/{Regexp} ...: "
3837 "Phrase, or [+-]Word/{Regexp} ...: ")
3838 (cond
3839 ((integerp edit-at) (cons string edit-at))
3840 (edit-at string))
3841 'org-agenda-search-history)))
3842 (org-set-local 'org-todo-only todo-only)
3843 (setq org-agenda-redo-command
3844 (list 'org-search-view (if todo-only t nil) string
3845 '(if current-prefix-arg 1 nil)))
3846 (setq org-agenda-query-string string)
3848 (if (equal (string-to-char string) ?*)
3849 (setq hdl-only t
3850 words (substring string 1))
3851 (setq words string))
3852 (when (equal (string-to-char words) ?!)
3853 (setq todo-only t
3854 words (substring words 1)))
3855 (when (equal (string-to-char words) ?:)
3856 (setq full-words t
3857 words (substring words 1)))
3858 (if (or org-agenda-search-view-always-boolean
3859 (member (string-to-char words) '(?- ?+ ?\{)))
3860 (setq boolean t))
3861 (setq words (org-split-string words))
3862 (let (www w)
3863 (while (setq w (pop words))
3864 (while (and (string-match "\\\\\\'" w) words)
3865 (setq w (concat (substring w 0 -1) " " (pop words))))
3866 (push w www))
3867 (setq words (nreverse www) www nil)
3868 (while (setq w (pop words))
3869 (when (and (string-match "\\`[-+]?{" w)
3870 (not (string-match "}\\'" w)))
3871 (while (and words (not (string-match "}\\'" (car words))))
3872 (setq w (concat w " " (pop words))))
3873 (setq w (concat w " " (pop words))))
3874 (push w www))
3875 (setq words (nreverse www)))
3876 (setq org-agenda-last-search-view-search-was-boolean boolean)
3877 (when boolean
3878 (let (wds w)
3879 (while (setq w (pop words))
3880 (if (or (equal (substring w 0 1) "\"")
3881 (and (> (length w) 1)
3882 (member (substring w 0 1) '("+" "-"))
3883 (equal (substring w 1 2) "\"")))
3884 (while (and words (not (equal (substring w -1) "\"")))
3885 (setq w (concat w " " (pop words)))))
3886 (and (string-match "\\`\\([-+]?\\)\"" w)
3887 (setq w (replace-match "\\1" nil nil w)))
3888 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3889 (push w wds))
3890 (setq words (nreverse wds))))
3891 (if boolean
3892 (mapc (lambda (w)
3893 (setq c (string-to-char w))
3894 (if (equal c ?-)
3895 (setq neg t w (substring w 1))
3896 (if (equal c ?+)
3897 (setq neg nil w (substring w 1))
3898 (setq neg nil)))
3899 (if (string-match "\\`{.*}\\'" w)
3900 (setq re (substring w 1 -1))
3901 (if full-words
3902 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3903 (setq re (regexp-quote (downcase w)))))
3904 (if neg (push re regexps-) (push re regexps+)))
3905 words)
3906 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3907 regexps+))
3908 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3909 (if (not regexps+)
3910 (setq regexp org-outline-regexp-bol)
3911 (setq regexp (pop regexps+))
3912 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
3913 regexp))))
3914 (setq files (org-agenda-files nil 'ifmode))
3915 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3916 (pop org-agenda-text-search-extra-files)
3917 (setq files (org-add-archive-files files)))
3918 (setq files (append files org-agenda-text-search-extra-files)
3919 rtnall nil)
3920 (while (setq file (pop files))
3921 (setq ee nil)
3922 (catch 'nextfile
3923 (org-check-agenda-file file)
3924 (setq buffer (if (file-exists-p file)
3925 (org-get-agenda-file-buffer file)
3926 (error "No such file %s" file)))
3927 (if (not buffer)
3928 ;; If file does not exist, make sure an error message is sent
3929 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3930 file))))
3931 (with-current-buffer buffer
3932 (with-syntax-table (org-search-syntax-table)
3933 (unless (eq major-mode 'org-mode)
3934 (error "Agenda file %s is not in `org-mode'" file))
3935 (let ((case-fold-search t))
3936 (save-excursion
3937 (save-restriction
3938 (if org-agenda-restrict
3939 (narrow-to-region org-agenda-restrict-begin
3940 org-agenda-restrict-end)
3941 (widen))
3942 (goto-char (point-min))
3943 (unless (or (org-at-heading-p)
3944 (outline-next-heading))
3945 (throw 'nextfile t))
3946 (goto-char (max (point-min) (1- (point))))
3947 (while (re-search-forward regexp nil t)
3948 (org-back-to-heading t)
3949 (skip-chars-forward "* ")
3950 (setq beg (point-at-bol)
3951 beg1 (point)
3952 end (progn (outline-next-heading) (point)))
3953 (catch :skip
3954 (goto-char beg)
3955 (org-agenda-skip)
3956 (setq str (buffer-substring-no-properties
3957 (point-at-bol)
3958 (if hdl-only (point-at-eol) end)))
3959 (mapc (lambda (wr) (when (string-match wr str)
3960 (goto-char (1- end))
3961 (throw :skip t)))
3962 regexps-)
3963 (mapc (lambda (wr) (unless (string-match wr str)
3964 (goto-char (1- end))
3965 (throw :skip t)))
3966 (if todo-only
3967 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3968 regexps+)
3969 regexps+))
3970 (goto-char beg)
3971 (setq marker (org-agenda-new-marker (point))
3972 category (org-get-category)
3973 org-category-pos (get-text-property (point) 'org-category-position)
3974 tags (org-get-tags-at (point))
3975 txt (org-agenda-format-item
3977 (buffer-substring-no-properties
3978 beg1 (point-at-eol))
3979 category tags))
3980 (org-add-props txt props
3981 'org-marker marker 'org-hd-marker marker
3982 'org-todo-regexp org-todo-regexp
3983 'org-complex-heading-regexp org-complex-heading-regexp
3984 'priority 1000 'org-category category
3985 'org-category-position org-category-pos
3986 'type "search")
3987 (push txt ee)
3988 (goto-char (1- end))))))))))
3989 (setq rtn (nreverse ee))
3990 (setq rtnall (append rtnall rtn)))
3991 (if org-agenda-overriding-header
3992 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3993 nil 'face 'org-agenda-structure) "\n")
3994 (insert "Search words: ")
3995 (add-text-properties (point-min) (1- (point))
3996 (list 'face 'org-agenda-structure))
3997 (setq pos (point))
3998 (insert string "\n")
3999 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4000 (setq pos (point))
4001 (unless org-agenda-multi
4002 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4003 (add-text-properties pos (1- (point))
4004 (list 'face 'org-agenda-structure))))
4005 (org-agenda-mark-header-line (point-min))
4006 (when rtnall
4007 (insert (org-finalize-agenda-entries rtnall) "\n"))
4008 (goto-char (point-min))
4009 (or org-agenda-multi (org-fit-agenda-window))
4010 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
4011 (org-finalize-agenda)
4012 (setq buffer-read-only t)))
4014 ;;; Agenda TODO list
4016 (defvar org-select-this-todo-keyword nil)
4017 (defvar org-last-arg nil)
4019 ;;;###autoload
4020 (defun org-todo-list (arg)
4021 "Show all (not done) TODO entries from all agenda file in a single list.
4022 The prefix arg can be used to select a specific TODO keyword and limit
4023 the list to these. When using \\[universal-argument], you will be prompted
4024 for a keyword. A numeric prefix directly selects the Nth keyword in
4025 `org-todo-keywords-1'."
4026 (interactive "P")
4027 (org-compile-prefix-format 'todo)
4028 (org-set-sorting-strategy 'todo)
4029 (org-prepare-agenda "TODO")
4030 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4031 (let* ((today (org-today))
4032 (date (calendar-gregorian-from-absolute today))
4033 (kwds org-todo-keywords-for-agenda)
4034 (completion-ignore-case t)
4035 (org-select-this-todo-keyword
4036 (if (stringp arg) arg
4037 (and arg (integerp arg) (> arg 0)
4038 (nth (1- arg) kwds))))
4039 rtn rtnall files file pos)
4040 (when (equal arg '(4))
4041 (setq org-select-this-todo-keyword
4042 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4043 (mapcar 'list kwds) nil nil)))
4044 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4045 (org-set-local 'org-last-arg arg)
4046 (setq org-agenda-redo-command
4047 '(org-todo-list (or current-prefix-arg org-last-arg)))
4048 (setq files (org-agenda-files nil 'ifmode)
4049 rtnall nil)
4050 (while (setq file (pop files))
4051 (catch 'nextfile
4052 (org-check-agenda-file file)
4053 (setq rtn (org-agenda-get-day-entries file date :todo))
4054 (setq rtnall (append rtnall rtn))))
4055 (if org-agenda-overriding-header
4056 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4057 nil 'face 'org-agenda-structure) "\n")
4058 (insert "Global list of TODO items of type: ")
4059 (add-text-properties (point-min) (1- (point))
4060 (list 'face 'org-agenda-structure
4061 'short-heading
4062 (concat "ToDo: "
4063 (or org-select-this-todo-keyword "ALL"))))
4064 (org-agenda-mark-header-line (point-min))
4065 (setq pos (point))
4066 (insert (or org-select-this-todo-keyword "ALL") "\n")
4067 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4068 (setq pos (point))
4069 (unless org-agenda-multi
4070 (insert "Available with `N r': (0)ALL")
4071 (let ((n 0) s)
4072 (mapc (lambda (x)
4073 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4074 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4075 (insert "\n "))
4076 (insert " " s))
4077 kwds))
4078 (insert "\n"))
4079 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4080 (org-agenda-mark-header-line (point-min))
4081 (when rtnall
4082 (insert (org-finalize-agenda-entries rtnall) "\n"))
4083 (goto-char (point-min))
4084 (or org-agenda-multi (org-fit-agenda-window))
4085 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4086 (org-finalize-agenda)
4087 (setq buffer-read-only t)))
4089 ;;; Agenda tags match
4091 ;;;###autoload
4092 (defun org-tags-view (&optional todo-only match)
4093 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4094 The prefix arg TODO-ONLY limits the search to TODO entries."
4095 (interactive "P")
4096 (org-compile-prefix-format 'tags)
4097 (org-set-sorting-strategy 'tags)
4098 (let* ((org-tags-match-list-sublevels
4099 org-tags-match-list-sublevels)
4100 (completion-ignore-case t)
4101 rtn rtnall files file pos matcher
4102 buffer)
4103 (when (and (stringp match) (not (string-match "\\S-" match)))
4104 (setq match nil))
4105 (setq matcher (org-make-tags-matcher match)
4106 match (car matcher) matcher (cdr matcher))
4107 (org-prepare-agenda (concat "TAGS " match))
4108 (setq org-agenda-query-string match)
4109 (setq org-agenda-redo-command
4110 (list 'org-tags-view (list 'quote todo-only)
4111 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4112 (setq files (org-agenda-files nil 'ifmode)
4113 rtnall nil)
4114 (while (setq file (pop files))
4115 (catch 'nextfile
4116 (org-check-agenda-file file)
4117 (setq buffer (if (file-exists-p file)
4118 (org-get-agenda-file-buffer file)
4119 (error "No such file %s" file)))
4120 (if (not buffer)
4121 ;; If file does not exist, error message to agenda
4122 (setq rtn (list
4123 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4124 rtnall (append rtnall rtn))
4125 (with-current-buffer buffer
4126 (unless (eq major-mode 'org-mode)
4127 (error "Agenda file %s is not in `org-mode'" file))
4128 (save-excursion
4129 (save-restriction
4130 (if org-agenda-restrict
4131 (narrow-to-region org-agenda-restrict-begin
4132 org-agenda-restrict-end)
4133 (widen))
4134 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4135 (setq rtnall (append rtnall rtn))))))))
4136 (if org-agenda-overriding-header
4137 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4138 nil 'face 'org-agenda-structure) "\n")
4139 (insert "Headlines with TAGS match: ")
4140 (add-text-properties (point-min) (1- (point))
4141 (list 'face 'org-agenda-structure
4142 'short-heading
4143 (concat "Match: " match)))
4144 (setq pos (point))
4145 (insert match "\n")
4146 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4147 (setq pos (point))
4148 (unless org-agenda-multi
4149 (insert "Press `C-u r' to search again with new search string\n"))
4150 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4151 (org-agenda-mark-header-line (point-min))
4152 (when rtnall
4153 (insert (org-finalize-agenda-entries rtnall) "\n"))
4154 (goto-char (point-min))
4155 (or org-agenda-multi (org-fit-agenda-window))
4156 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4157 (org-finalize-agenda)
4158 (setq buffer-read-only t)))
4160 ;;; Agenda Finding stuck projects
4162 (defvar org-agenda-skip-regexp nil
4163 "Regular expression used in skipping subtrees for the agenda.
4164 This is basically a temporary global variable that can be set and then
4165 used by user-defined selections using `org-agenda-skip-function'.")
4167 (defvar org-agenda-overriding-header nil
4168 "When set during agenda, todo and tags searches it replaces the header.
4169 This variable should not be set directly, but custom commands can bind it
4170 in the options section.")
4172 (defun org-agenda-skip-entry-when-regexp-matches ()
4173 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4174 If yes, it returns the end position of this entry, causing agenda commands
4175 to skip the entry but continuing the search in the subtree. This is a
4176 function that can be put into `org-agenda-skip-function' for the duration
4177 of a command."
4178 (let ((end (save-excursion (org-end-of-subtree t)))
4179 skip)
4180 (save-excursion
4181 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4182 (and skip end)))
4184 (defun org-agenda-skip-subtree-when-regexp-matches ()
4185 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4186 If yes, it returns the end position of this tree, causing agenda commands
4187 to skip this subtree. This is a function that can be put into
4188 `org-agenda-skip-function' for the duration of a command."
4189 (let ((end (save-excursion (org-end-of-subtree t)))
4190 skip)
4191 (save-excursion
4192 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4193 (and skip end)))
4195 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4196 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4197 If yes, it returns the end position of the current entry (NOT the tree),
4198 causing agenda commands to skip the entry but continuing the search in
4199 the subtree. This is a function that can be put into
4200 `org-agenda-skip-function' for the duration of a command. An important
4201 use of this function is for the stuck project list."
4202 (let ((end (save-excursion (org-end-of-subtree t)))
4203 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4204 skip)
4205 (save-excursion
4206 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4207 (and skip entry-end)))
4209 (defun org-agenda-skip-entry-if (&rest conditions)
4210 "Skip entry if any of CONDITIONS is true.
4211 See `org-agenda-skip-if' for details."
4212 (org-agenda-skip-if nil conditions))
4214 (defun org-agenda-skip-subtree-if (&rest conditions)
4215 "Skip entry if any of CONDITIONS is true.
4216 See `org-agenda-skip-if' for details."
4217 (org-agenda-skip-if t conditions))
4219 (defun org-agenda-skip-if (subtree conditions)
4220 "Checks current entity for CONDITIONS.
4221 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4222 the entry (i.e. the text before the next heading) is checked.
4224 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4225 from different tests. Valid conditions are:
4227 scheduled Check if there is a scheduled cookie
4228 notscheduled Check if there is no scheduled cookie
4229 deadline Check if there is a deadline
4230 notdeadline Check if there is no deadline
4231 timestamp Check if there is a timestamp (also deadline or scheduled)
4232 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4233 regexp Check if regexp matches
4234 notregexp Check if regexp does not match.
4235 todo Check if TODO keyword matches
4236 nottodo Check if TODO keyword does not match
4238 The regexp is taken from the conditions list, it must come right after
4239 the `regexp' or `notregexp' element.
4241 `todo' and `nottodo' accept as an argument a list of todo
4242 keywords, which may include \"*\" to match any todo keyword.
4244 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4246 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4248 Instead of a list, a keyword class may be given. For example:
4250 (org-agenda-skip-entry-if 'nottodo 'done)
4252 would skip entries that haven't been marked with any of \"DONE\"
4253 keywords. Possible classes are: `todo', `done', `any'.
4255 If any of these conditions is met, this function returns the end point of
4256 the entity, causing the search to continue from there. This is a function
4257 that can be put into `org-agenda-skip-function' for the duration of a command."
4258 (let (beg end m)
4259 (org-back-to-heading t)
4260 (setq beg (point)
4261 end (if subtree
4262 (progn (org-end-of-subtree t) (point))
4263 (progn (outline-next-heading) (1- (point)))))
4264 (goto-char beg)
4265 (and
4267 (and (memq 'scheduled conditions)
4268 (re-search-forward org-scheduled-time-regexp end t))
4269 (and (memq 'notscheduled conditions)
4270 (not (re-search-forward org-scheduled-time-regexp end t)))
4271 (and (memq 'deadline conditions)
4272 (re-search-forward org-deadline-time-regexp end t))
4273 (and (memq 'notdeadline conditions)
4274 (not (re-search-forward org-deadline-time-regexp end t)))
4275 (and (memq 'timestamp conditions)
4276 (re-search-forward org-ts-regexp end t))
4277 (and (memq 'nottimestamp conditions)
4278 (not (re-search-forward org-ts-regexp end t)))
4279 (and (setq m (memq 'regexp conditions))
4280 (stringp (nth 1 m))
4281 (re-search-forward (nth 1 m) end t))
4282 (and (setq m (memq 'notregexp conditions))
4283 (stringp (nth 1 m))
4284 (not (re-search-forward (nth 1 m) end t)))
4285 (and (or
4286 (setq m (memq 'nottodo conditions))
4287 (setq m (memq 'todo conditions)))
4288 (org-agenda-skip-if-todo m end)))
4289 end)))
4291 (defun org-agenda-skip-if-todo (args end)
4292 "Helper function for `org-agenda-skip-if', do not use it directly.
4293 ARGS is a list with first element either `todo' or `nottodo'.
4294 The remainder is either a list of TODO keywords, or a state symbol
4295 `todo' or `done' or `any'."
4296 (let ((kw (car args))
4297 (arg (cadr args))
4298 todo-wds todo-re)
4299 (setq todo-wds
4300 (org-uniquify
4301 (cond
4302 ((listp arg) ;; list of keywords
4303 (if (member "*" arg)
4304 (mapcar 'substring-no-properties org-todo-keywords-1)
4305 arg))
4306 ((symbolp arg) ;; keyword class name
4307 (cond
4308 ((eq arg 'todo)
4309 (org-delete-all org-done-keywords
4310 (mapcar 'substring-no-properties
4311 org-todo-keywords-1)))
4312 ((eq arg 'done) org-done-keywords)
4313 ((eq arg 'any)
4314 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4315 (setq todo-re
4316 (concat "^\\*+[ \t]+\\<\\("
4317 (mapconcat 'identity todo-wds "\\|")
4318 "\\)\\>"))
4319 (if (eq kw 'todo)
4320 (re-search-forward todo-re end t)
4321 (not (re-search-forward todo-re end t)))))
4323 ;;;###autoload
4324 (defun org-agenda-list-stuck-projects (&rest ignore)
4325 "Create agenda view for projects that are stuck.
4326 Stuck projects are project that have no next actions. For the definitions
4327 of what a project is and how to check if it stuck, customize the variable
4328 `org-stuck-projects'."
4329 (interactive)
4330 (let* ((org-agenda-skip-function
4331 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4332 ;; We could have used org-agenda-skip-if here.
4333 (org-agenda-overriding-header
4334 (or org-agenda-overriding-header "List of stuck projects: "))
4335 (matcher (nth 0 org-stuck-projects))
4336 (todo (nth 1 org-stuck-projects))
4337 (todo-wds (if (member "*" todo)
4338 (progn
4339 (org-prepare-agenda-buffers (org-agenda-files
4340 nil 'ifmode))
4341 (org-delete-all
4342 org-done-keywords-for-agenda
4343 (copy-sequence org-todo-keywords-for-agenda)))
4344 todo))
4345 (todo-re (concat "^\\*+[ \t]+\\("
4346 (mapconcat 'identity todo-wds "\\|")
4347 "\\)\\>"))
4348 (tags (nth 2 org-stuck-projects))
4349 (tags-re (if (member "*" tags)
4350 (concat org-outline-regexp-bol
4351 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4352 (if tags
4353 (concat org-outline-regexp-bol
4354 ".*:\\("
4355 (mapconcat 'identity tags "\\|")
4356 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4357 (gen-re (nth 3 org-stuck-projects))
4358 (re-list
4359 (delq nil
4360 (list
4361 (if todo todo-re)
4362 (if tags tags-re)
4363 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4364 gen-re)))))
4365 (setq org-agenda-skip-regexp
4366 (if re-list
4367 (mapconcat 'identity re-list "\\|")
4368 (error "No information how to identify unstuck projects")))
4369 (org-tags-view nil matcher)
4370 (with-current-buffer org-agenda-buffer-name
4371 (setq org-agenda-redo-command
4372 '(org-agenda-list-stuck-projects
4373 (or current-prefix-arg org-last-arg))))))
4375 ;;; Diary integration
4377 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4378 (defvar list-diary-entries-hook)
4379 (defvar diary-time-regexp)
4380 (defun org-get-entries-from-diary (date)
4381 "Get the (Emacs Calendar) diary entries for DATE."
4382 (require 'diary-lib)
4383 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4384 (diary-display-hook '(fancy-diary-display))
4385 (diary-display-function 'fancy-diary-display)
4386 (pop-up-frames nil)
4387 (list-diary-entries-hook
4388 (cons 'org-diary-default-entry list-diary-entries-hook))
4389 (diary-file-name-prefix-function nil) ; turn this feature off
4390 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4391 entries
4392 (org-disable-agenda-to-diary t))
4393 (save-excursion
4394 (save-window-excursion
4395 (funcall (if (fboundp 'diary-list-entries)
4396 'diary-list-entries 'list-diary-entries)
4397 date 1)))
4398 (if (not (get-buffer diary-fancy-buffer))
4399 (setq entries nil)
4400 (with-current-buffer diary-fancy-buffer
4401 (setq buffer-read-only nil)
4402 (if (zerop (buffer-size))
4403 ;; No entries
4404 (setq entries nil)
4405 ;; Omit the date and other unnecessary stuff
4406 (org-agenda-cleanup-fancy-diary)
4407 ;; Add prefix to each line and extend the text properties
4408 (if (zerop (buffer-size))
4409 (setq entries nil)
4410 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4411 (setq entries
4412 (with-temp-buffer
4413 (insert entries) (goto-char (point-min))
4414 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4415 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4416 (replace-match (concat "; " (match-string 1)))))
4417 (buffer-string)))))
4418 (set-buffer-modified-p nil)
4419 (kill-buffer diary-fancy-buffer)))
4420 (when entries
4421 (setq entries (org-split-string entries "\n"))
4422 (setq entries
4423 (mapcar
4424 (lambda (x)
4425 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4426 ;; Extend the text properties to the beginning of the line
4427 (org-add-props x (text-properties-at (1- (length x)) x)
4428 'type "diary" 'date date 'face 'org-agenda-diary))
4429 entries)))))
4431 (defvar org-agenda-cleanup-fancy-diary-hook nil
4432 "Hook run when the fancy diary buffer is cleaned up.")
4434 (defun org-agenda-cleanup-fancy-diary ()
4435 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4436 This gets rid of the date, the underline under the date, and
4437 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4438 date. It also removes lines that contain only whitespace."
4439 (goto-char (point-min))
4440 (if (looking-at ".*?:[ \t]*")
4441 (progn
4442 (replace-match "")
4443 (re-search-forward "\n=+$" nil t)
4444 (replace-match "")
4445 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4446 (re-search-forward "\n=+$" nil t)
4447 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4448 (goto-char (point-min))
4449 (while (re-search-forward "^ +\n" nil t)
4450 (replace-match ""))
4451 (goto-char (point-min))
4452 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4453 (replace-match ""))
4454 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4456 ;; Make sure entries from the diary have the right text properties.
4457 (eval-after-load "diary-lib"
4458 '(if (boundp 'diary-modify-entry-list-string-function)
4459 ;; We can rely on the hook, nothing to do
4461 ;; Hook not available, must use advice to make this work
4462 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4463 "Make the position visible."
4464 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4465 (stringp string)
4466 buffer-file-name)
4467 (setq string (org-modify-diary-entry-string string))))))
4469 (defun org-modify-diary-entry-string (string)
4470 "Add text properties to string, allowing org-mode to act on it."
4471 (org-add-props string nil
4472 'mouse-face 'highlight
4473 'help-echo (if buffer-file-name
4474 (format "mouse-2 or RET jump to diary file %s"
4475 (abbreviate-file-name buffer-file-name))
4477 'org-agenda-diary-link t
4478 'org-marker (org-agenda-new-marker (point-at-bol))))
4480 (defun org-diary-default-entry ()
4481 "Add a dummy entry to the diary.
4482 Needed to avoid empty dates which mess up holiday display."
4483 ;; Catch the error if dealing with the new add-to-diary-alist
4484 (when org-disable-agenda-to-diary
4485 (condition-case nil
4486 (org-add-to-diary-list original-date "Org-mode dummy" "")
4487 (error
4488 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4490 (defun org-add-to-diary-list (&rest args)
4491 (if (fboundp 'diary-add-to-list)
4492 (apply 'diary-add-to-list args)
4493 (apply 'add-to-diary-list args)))
4495 (defvar org-diary-last-run-time nil)
4497 ;;;###autoload
4498 (defun org-diary (&rest args)
4499 "Return diary information from org-files.
4500 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4501 It accesses org files and extracts information from those files to be
4502 listed in the diary. The function accepts arguments specifying what
4503 items should be listed. For a list of arguments allowed here, see the
4504 variable `org-agenda-entry-types'.
4506 The call in the diary file should look like this:
4508 &%%(org-diary) ~/path/to/some/orgfile.org
4510 Use a separate line for each org file to check. Or, if you omit the file name,
4511 all files listed in `org-agenda-files' will be checked automatically:
4513 &%%(org-diary)
4515 If you don't give any arguments (as in the example above), the default
4516 arguments (:deadline :scheduled :timestamp :sexp) are used.
4517 So the example above may also be written as
4519 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4521 The function expects the lisp variables `entry' and `date' to be provided
4522 by the caller, because this is how the calendar works. Don't use this
4523 function from a program - use `org-agenda-get-day-entries' instead."
4524 (when (> (- (org-float-time)
4525 org-agenda-last-marker-time)
4527 (org-agenda-reset-markers))
4528 (org-compile-prefix-format 'agenda)
4529 (org-set-sorting-strategy 'agenda)
4530 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4531 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4532 (list entry)
4533 (org-agenda-files t)))
4534 (time (org-float-time))
4535 file rtn results)
4536 (when (or (not org-diary-last-run-time)
4537 (> (- time
4538 org-diary-last-run-time)
4540 (org-prepare-agenda-buffers files))
4541 (setq org-diary-last-run-time time)
4542 ;; If this is called during org-agenda, don't return any entries to
4543 ;; the calendar. Org Agenda will list these entries itself.
4544 (if org-disable-agenda-to-diary (setq files nil))
4545 (while (setq file (pop files))
4546 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4547 (setq results (append results rtn)))
4548 (if results
4549 (concat (org-finalize-agenda-entries results) "\n"))))
4551 ;;; Agenda entry finders
4553 (defun org-agenda-get-day-entries (file date &rest args)
4554 "Does the work for `org-diary' and `org-agenda'.
4555 FILE is the path to a file to be checked for entries. DATE is date like
4556 the one returned by `calendar-current-date'. ARGS are symbols indicating
4557 which kind of entries should be extracted. For details about these, see
4558 the documentation of `org-diary'."
4559 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4560 (let* ((org-startup-folded nil)
4561 (org-startup-align-all-tables nil)
4562 (buffer (if (file-exists-p file)
4563 (org-get-agenda-file-buffer file)
4564 (error "No such file %s" file)))
4565 arg results rtn deadline-results)
4566 (if (not buffer)
4567 ;; If file does not exist, make sure an error message ends up in diary
4568 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4569 (with-current-buffer buffer
4570 (unless (eq major-mode 'org-mode)
4571 (error "Agenda file %s is not in `org-mode'" file))
4572 (let ((case-fold-search nil))
4573 (save-excursion
4574 (save-restriction
4575 (if org-agenda-restrict
4576 (narrow-to-region org-agenda-restrict-begin
4577 org-agenda-restrict-end)
4578 (widen))
4579 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4580 (while (setq arg (pop args))
4581 (cond
4582 ((and (eq arg :todo)
4583 (equal date (calendar-gregorian-from-absolute
4584 (org-today))))
4585 (setq rtn (org-agenda-get-todos))
4586 (setq results (append results rtn)))
4587 ((eq arg :timestamp)
4588 (setq rtn (org-agenda-get-blocks))
4589 (setq results (append results rtn))
4590 (setq rtn (org-agenda-get-timestamps))
4591 (setq results (append results rtn)))
4592 ((eq arg :sexp)
4593 (setq rtn (org-agenda-get-sexps))
4594 (setq results (append results rtn)))
4595 ((eq arg :scheduled)
4596 (setq rtn (org-agenda-get-scheduled deadline-results))
4597 (setq results (append results rtn)))
4598 ((eq arg :closed)
4599 (setq rtn (org-agenda-get-progress))
4600 (setq results (append results rtn)))
4601 ((eq arg :deadline)
4602 (setq rtn (org-agenda-get-deadlines))
4603 (setq deadline-results (copy-sequence rtn))
4604 (setq results (append results rtn))))))))
4605 results))))
4607 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4608 (defun org-agenda-get-todos ()
4609 "Return the TODO information for agenda display."
4610 (let* ((props (list 'face nil
4611 'done-face 'org-agenda-done
4612 'org-not-done-regexp org-not-done-regexp
4613 'org-todo-regexp org-todo-regexp
4614 'org-complex-heading-regexp org-complex-heading-regexp
4615 'mouse-face 'highlight
4616 'help-echo
4617 (format "mouse-2 or RET jump to org file %s"
4618 (abbreviate-file-name buffer-file-name))))
4619 (regexp (format org-heading-keyword-regexp-format
4620 (cond
4621 ((and org-select-this-todo-keyword
4622 (equal org-select-this-todo-keyword "*"))
4623 org-todo-regexp)
4624 (org-select-this-todo-keyword
4625 (concat "\\("
4626 (mapconcat 'identity
4627 (org-split-string
4628 org-select-this-todo-keyword
4629 "|")
4630 "\\|") "\\)"))
4631 (t org-not-done-regexp))))
4632 marker priority category org-category-pos tags todo-state
4633 ee txt beg end)
4634 (goto-char (point-min))
4635 (while (re-search-forward regexp nil t)
4636 (catch :skip
4637 (save-match-data
4638 (beginning-of-line)
4639 (org-agenda-skip)
4640 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4641 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4642 (goto-char (1+ beg))
4643 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4644 (throw :skip nil)))
4645 (goto-char (match-beginning 2))
4646 (setq marker (org-agenda-new-marker (match-beginning 0))
4647 category (org-get-category)
4648 org-category-pos (get-text-property (point) 'org-category-position)
4649 txt (org-trim
4650 (buffer-substring (match-beginning 2) (match-end 0)))
4651 tags (org-get-tags-at (point))
4652 txt (org-agenda-format-item "" txt category tags)
4653 priority (1+ (org-get-priority txt))
4654 todo-state (org-get-todo-state))
4655 (org-add-props txt props
4656 'org-marker marker 'org-hd-marker marker
4657 'priority priority 'org-category category
4658 'org-category-position org-category-pos
4659 'type "todo" 'todo-state todo-state)
4660 (push txt ee)
4661 (if org-agenda-todo-list-sublevels
4662 (goto-char (match-end 2))
4663 (org-end-of-subtree 'invisible))))
4664 (nreverse ee)))
4666 (defun org-agenda-todo-custom-ignore-p (time n)
4667 "Check whether timestamp is farther away then n number of days.
4668 This function is invoked if `org-agenda-todo-ignore-deadlines',
4669 `org-agenda-todo-ignore-scheduled' or
4670 `org-agenda-todo-ignore-timestamp' is set to an integer."
4671 (let ((days (org-days-to-time time)))
4672 (if (>= n 0)
4673 (>= days n)
4674 (<= days n))))
4676 ;;;###autoload
4677 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4678 (&optional end)
4679 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4680 (when (or org-agenda-todo-ignore-with-date
4681 org-agenda-todo-ignore-scheduled
4682 org-agenda-todo-ignore-deadlines
4683 org-agenda-todo-ignore-timestamp)
4684 (setq end (or end (save-excursion (outline-next-heading) (point))))
4685 (save-excursion
4686 (or (and org-agenda-todo-ignore-with-date
4687 (re-search-forward org-ts-regexp end t))
4688 (and org-agenda-todo-ignore-scheduled
4689 (re-search-forward org-scheduled-time-regexp end t)
4690 (cond
4691 ((eq org-agenda-todo-ignore-scheduled 'future)
4692 (> (org-days-to-time (match-string 1)) 0))
4693 ((eq org-agenda-todo-ignore-scheduled 'past)
4694 (<= (org-days-to-time (match-string 1)) 0))
4695 ((numberp org-agenda-todo-ignore-scheduled)
4696 (org-agenda-todo-custom-ignore-p
4697 (match-string 1) org-agenda-todo-ignore-scheduled))
4698 (t)))
4699 (and org-agenda-todo-ignore-deadlines
4700 (re-search-forward org-deadline-time-regexp end t)
4701 (cond
4702 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4703 ((eq org-agenda-todo-ignore-deadlines 'far)
4704 (not (org-deadline-close (match-string 1))))
4705 ((eq org-agenda-todo-ignore-deadlines 'future)
4706 (> (org-days-to-time (match-string 1)) 0))
4707 ((eq org-agenda-todo-ignore-deadlines 'past)
4708 (<= (org-days-to-time (match-string 1)) 0))
4709 ((numberp org-agenda-todo-ignore-deadlines)
4710 (org-agenda-todo-custom-ignore-p
4711 (match-string 1) org-agenda-todo-ignore-deadlines))
4712 (t (org-deadline-close (match-string 1)))))
4713 (and org-agenda-todo-ignore-timestamp
4714 (let ((buffer (current-buffer))
4715 (regexp
4716 (concat
4717 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4718 (start (point)))
4719 ;; Copy current buffer into a temporary one
4720 (with-temp-buffer
4721 (insert-buffer-substring buffer start end)
4722 (goto-char (point-min))
4723 ;; Delete SCHEDULED and DEADLINE items
4724 (while (re-search-forward regexp end t)
4725 (delete-region (match-beginning 0) (match-end 0)))
4726 (goto-char (point-min))
4727 ;; No search for timestamp left
4728 (when (re-search-forward org-ts-regexp nil t)
4729 (cond
4730 ((eq org-agenda-todo-ignore-timestamp 'future)
4731 (> (org-days-to-time (match-string 1)) 0))
4732 ((eq org-agenda-todo-ignore-timestamp 'past)
4733 (<= (org-days-to-time (match-string 1)) 0))
4734 ((numberp org-agenda-todo-ignore-timestamp)
4735 (org-agenda-todo-custom-ignore-p
4736 (match-string 1) org-agenda-todo-ignore-timestamp))
4737 (t))))))))))
4739 (defconst org-agenda-no-heading-message
4740 "No heading for this item in buffer or region.")
4742 (defun org-agenda-get-timestamps ()
4743 "Return the date stamp information for agenda display."
4744 (let* ((props (list 'face 'org-agenda-calendar-event
4745 'org-not-done-regexp org-not-done-regexp
4746 'org-todo-regexp org-todo-regexp
4747 'org-complex-heading-regexp org-complex-heading-regexp
4748 'mouse-face 'highlight
4749 'help-echo
4750 (format "mouse-2 or RET jump to org file %s"
4751 (abbreviate-file-name buffer-file-name))))
4752 (d1 (calendar-absolute-from-gregorian date))
4753 (remove-re
4754 (concat
4755 (regexp-quote
4756 (format-time-string
4757 "<%Y-%m-%d"
4758 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4759 ".*?>"))
4760 (regexp
4761 (concat
4762 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4763 (regexp-quote
4764 (substring
4765 (format-time-string
4766 (car org-time-stamp-formats)
4767 (apply 'encode-time ; DATE bound by calendar
4768 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4769 1 11))
4770 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4771 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4772 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4773 donep tmp priority category org-category-pos ee txt timestr tags
4774 b0 b3 e3 head todo-state end-of-match show-all)
4775 (goto-char (point-min))
4776 (while (setq end-of-match (re-search-forward regexp nil t))
4777 (setq b0 (match-beginning 0)
4778 b3 (match-beginning 3) e3 (match-end 3)
4779 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4780 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4781 (member todo-state
4782 org-agenda-repeating-timestamp-show-all)))
4783 (catch :skip
4784 (and (org-at-date-range-p) (throw :skip nil))
4785 (org-agenda-skip)
4786 (if (and (match-end 1)
4787 (not (= d1 (org-time-string-to-absolute
4788 (match-string 1) d1 nil show-all
4789 (current-buffer) b0))))
4790 (throw :skip nil))
4791 (if (and e3
4792 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4793 (throw :skip nil))
4794 (setq tmp (buffer-substring (max (point-min)
4795 (- b0 org-ds-keyword-length))
4797 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4798 inactivep (= (char-after b0) ?\[)
4799 deadlinep (string-match org-deadline-regexp tmp)
4800 scheduledp (string-match org-scheduled-regexp tmp)
4801 closedp (and org-agenda-include-inactive-timestamps
4802 (string-match org-closed-string tmp))
4803 clockp (and org-agenda-include-inactive-timestamps
4804 (or (string-match org-clock-string tmp)
4805 (string-match "]-+\\'" tmp)))
4806 donep (member todo-state org-done-keywords))
4807 (if (or scheduledp deadlinep closedp clockp
4808 (and donep org-agenda-skip-timestamp-if-done))
4809 (throw :skip t))
4810 (if (string-match ">" timestr)
4811 ;; substring should only run to end of time stamp
4812 (setq timestr (substring timestr 0 (match-end 0))))
4813 (setq marker (org-agenda-new-marker b0)
4814 category (org-get-category b0)
4815 org-category-pos (get-text-property b0 'org-category-position))
4816 (save-excursion
4817 (if (not (re-search-backward org-outline-regexp-bol nil t))
4818 (setq txt org-agenda-no-heading-message)
4819 (goto-char (match-beginning 0))
4820 (setq hdmarker (org-agenda-new-marker)
4821 tags (org-get-tags-at))
4822 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4823 (setq head (or (match-string 1) ""))
4824 (setq txt (org-agenda-format-item
4825 (if inactivep org-agenda-inactive-leader nil)
4826 head category tags timestr
4827 remove-re)))
4828 (setq priority (org-get-priority txt))
4829 (org-add-props txt props
4830 'org-marker marker 'org-hd-marker hdmarker)
4831 (org-add-props txt nil 'priority priority
4832 'org-category category 'date date
4833 'org-category-position org-category-pos
4834 'todo-state todo-state
4835 'type "timestamp")
4836 (push txt ee))
4837 (if org-agenda-skip-additional-timestamps-same-entry
4838 (outline-next-heading)
4839 (goto-char end-of-match))))
4840 (nreverse ee)))
4842 (defun org-agenda-get-sexps ()
4843 "Return the sexp information for agenda display."
4844 (require 'diary-lib)
4845 (let* ((props (list 'face 'org-agenda-calendar-sexp
4846 'mouse-face 'highlight
4847 'help-echo
4848 (format "mouse-2 or RET jump to org file %s"
4849 (abbreviate-file-name buffer-file-name))))
4850 (regexp "^&?%%(")
4851 marker category org-category-pos ee txt tags entry
4852 result beg b sexp sexp-entry todo-state)
4853 (goto-char (point-min))
4854 (while (re-search-forward regexp nil t)
4855 (catch :skip
4856 (org-agenda-skip)
4857 (setq beg (match-beginning 0))
4858 (goto-char (1- (match-end 0)))
4859 (setq b (point))
4860 (forward-sexp 1)
4861 (setq sexp (buffer-substring b (point)))
4862 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4863 (org-trim (match-string 1))
4864 ""))
4865 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4866 (when result
4867 (setq marker (org-agenda-new-marker beg)
4868 category (org-get-category beg)
4869 org-category-pos (get-text-property beg 'org-category-position)
4870 todo-state (org-get-todo-state))
4872 (dolist (r (if (stringp result)
4873 (list result)
4874 result)) ;; we expect a list here
4875 (if (string-match "\\S-" r)
4876 (setq txt r)
4877 (setq txt "SEXP entry returned empty string"))
4879 (setq txt (org-agenda-format-item
4880 "" txt category tags 'time))
4881 (org-add-props txt props 'org-marker marker)
4882 (org-add-props txt nil
4883 'org-category category 'date date 'todo-state todo-state
4884 'org-category-position org-category-pos
4885 'type "sexp")
4886 (push txt ee)))))
4887 (nreverse ee)))
4889 ;; Calendar sanity: define some functions that are independent of
4890 ;; `calendar-date-style'.
4891 ;; Normally I would like to use ISO format when calling the diary functions,
4892 ;; but to make sure we still have Emacs 22 compatibility we bind
4893 ;; also `european-calendar-style' and use european format
4894 (defun org-anniversary (year month day &optional mark)
4895 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4896 (org-no-warnings
4897 (let ((calendar-date-style 'european) (european-calendar-style t))
4898 (diary-anniversary day month year mark))))
4899 (defun org-cyclic (N year month day &optional mark)
4900 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4901 (org-no-warnings
4902 (let ((calendar-date-style 'european) (european-calendar-style t))
4903 (diary-cyclic N day month year mark))))
4904 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4905 "Like `diary-block', but with fixed (ISO) order of arguments."
4906 (org-no-warnings
4907 (let ((calendar-date-style 'european) (european-calendar-style t))
4908 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4909 (defun org-date (year month day &optional mark)
4910 "Like `diary-date', but with fixed (ISO) order of arguments."
4911 (org-no-warnings
4912 (let ((calendar-date-style 'european) (european-calendar-style t))
4913 (diary-date day month year mark))))
4914 (defalias 'org-float 'diary-float)
4916 ;; Define the` org-class' function
4917 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4918 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4919 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
4920 SKIP-WEEKS is any number of ISO weeks in the block period for which the
4921 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
4922 `holidays', then any date that is known by the Emacs calendar to be a
4923 holiday will also be skipped."
4924 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4925 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4926 (d (calendar-absolute-from-gregorian date)))
4927 (and
4928 (<= date1 d)
4929 (<= d date2)
4930 (= (calendar-day-of-week date) dayname)
4931 (or (not skip-weeks)
4932 (progn
4933 (require 'cal-iso)
4934 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4935 (not (and (memq 'holidays skip-weeks)
4936 (calendar-check-holidays date)))
4937 entry)))
4939 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4940 "Like `org-class', but honor `calendar-date-style'.
4941 The order of the first 2 times 3 arguments depends on the variable
4942 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4943 So for American calendars, give this as MONTH DAY YEAR, for European as
4944 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4945 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4946 is any number of ISO weeks in the block period for which the item should
4947 be skipped.
4949 This function is here only for backward compatibility and it is deprecated,
4950 please use `org-class' instead."
4951 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4952 (date2 (org-order-calendar-date-args m2 d2 y2)))
4953 (org-class
4954 (nth 2 date1) (car date1) (nth 1 date1)
4955 (nth 2 date2) (car date2) (nth 1 date2)
4956 dayname skip-weeks)))
4957 (make-obsolete 'org-diary-class 'org-class "")
4959 (defalias 'org-get-closed 'org-agenda-get-progress)
4960 (defun org-agenda-get-progress ()
4961 "Return the logged TODO entries for agenda display."
4962 (let* ((props (list 'mouse-face 'highlight
4963 'org-not-done-regexp org-not-done-regexp
4964 'org-todo-regexp org-todo-regexp
4965 'org-complex-heading-regexp org-complex-heading-regexp
4966 'help-echo
4967 (format "mouse-2 or RET jump to org file %s"
4968 (abbreviate-file-name buffer-file-name))))
4969 (items (if (consp org-agenda-show-log)
4970 org-agenda-show-log
4971 (if (eq org-agenda-show-log 'clockcheck)
4972 '(clock)
4973 org-agenda-log-mode-items)))
4974 (parts
4975 (delq nil
4976 (list
4977 (if (memq 'closed items) (concat "\\<" org-closed-string))
4978 (if (memq 'clock items) (concat "\\<" org-clock-string))
4979 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4980 (parts-re (if parts (mapconcat 'identity parts "\\|")
4981 (error "`org-agenda-log-mode-items' is empty")))
4982 (regexp (concat
4983 "\\(" parts-re "\\)"
4984 " *\\["
4985 (regexp-quote
4986 (substring
4987 (format-time-string
4988 (car org-time-stamp-formats)
4989 (apply 'encode-time ; DATE bound by calendar
4990 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4991 1 11))))
4992 (org-agenda-search-headline-for-time nil)
4993 marker hdmarker priority category org-category-pos tags closedp
4994 statep clockp state ee txt extra timestr rest clocked)
4995 (goto-char (point-min))
4996 (while (re-search-forward regexp nil t)
4997 (catch :skip
4998 (org-agenda-skip)
4999 (setq marker (org-agenda-new-marker (match-beginning 0))
5000 closedp (equal (match-string 1) org-closed-string)
5001 statep (equal (string-to-char (match-string 1)) ?-)
5002 clockp (not (or closedp statep))
5003 state (and statep (match-string 2))
5004 category (org-get-category (match-beginning 0))
5005 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
5006 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5007 (when (string-match "\\]" timestr)
5008 ;; substring should only run to end of time stamp
5009 (setq rest (substring timestr (match-end 0))
5010 timestr (substring timestr 0 (match-end 0)))
5011 (if (and (not closedp) (not statep)
5012 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5013 rest))
5014 (progn (setq timestr (concat (substring timestr 0 -1)
5015 "-" (match-string 1 rest) "]"))
5016 (setq clocked (match-string 2 rest)))
5017 (setq clocked "-")))
5018 (save-excursion
5019 (setq extra
5020 (cond
5021 ((not org-agenda-log-mode-add-notes) nil)
5022 (statep
5023 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5024 (match-string 1)))
5025 (clockp
5026 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5027 (match-string 1)))))
5028 (if (not (re-search-backward org-outline-regexp-bol nil t))
5029 (setq txt org-agenda-no-heading-message)
5030 (goto-char (match-beginning 0))
5031 (setq hdmarker (org-agenda-new-marker)
5032 tags (org-get-tags-at))
5033 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5034 (setq txt (match-string 1))
5035 (when extra
5036 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5037 (setq txt (concat (substring txt 0 (match-beginning 1))
5038 " - " extra " " (match-string 2 txt)))
5039 (setq txt (concat txt " - " extra))))
5040 (setq txt (org-agenda-format-item
5041 (cond
5042 (closedp "Closed: ")
5043 (statep (concat "State: (" state ")"))
5044 (t (concat "Clocked: (" clocked ")")))
5045 txt category tags timestr)))
5046 (setq priority 100000)
5047 (org-add-props txt props
5048 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5049 'priority priority 'org-category category
5050 'org-category-position org-category-pos
5051 'type "closed" 'date date
5052 'undone-face 'org-warning 'done-face 'org-agenda-done)
5053 (push txt ee))
5054 (goto-char (point-at-eol))))
5055 (nreverse ee)))
5057 (defun org-agenda-show-clocking-issues ()
5058 "Add overlays, showing issues with clocking.
5059 See also the user option `org-agenda-clock-consistency-checks'."
5060 (interactive)
5061 (let* ((pl org-agenda-clock-consistency-checks)
5062 (re (concat "^[ \t]*"
5063 org-clock-string
5064 "[ \t]+"
5065 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5066 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5067 (tlstart 0.)
5068 (tlend 0.)
5069 (maxtime (org-hh:mm-string-to-minutes
5070 (or (plist-get pl :max-duration) "24:00")))
5071 (mintime (org-hh:mm-string-to-minutes
5072 (or (plist-get pl :min-duration) 0)))
5073 (maxgap (org-hh:mm-string-to-minutes
5074 ;; default 30:00 means never complain
5075 (or (plist-get pl :max-gap) "30:00")))
5076 (gapok (mapcar 'org-hh:mm-string-to-minutes
5077 (plist-get pl :gap-ok-around)))
5078 (def-face (or (plist-get pl :default-face)
5079 '((:background "DarkRed") (:foreground "white"))))
5080 issue face m te ts dt ov)
5081 (goto-char (point-min))
5082 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5083 (setq issue nil face def-face)
5084 (catch 'next
5085 (setq m (org-get-at-bol 'org-marker)
5086 te nil ts nil)
5087 (unless (and m (markerp m))
5088 (setq issue "No valid clock line") (throw 'next t))
5089 (org-with-point-at m
5090 (save-excursion
5091 (goto-char (point-at-bol))
5092 (unless (looking-at re)
5093 (error "No valid Clock line")
5094 (throw 'next t))
5095 (unless (match-end 3)
5096 (setq issue "No end time"
5097 face (or (plist-get pl :no-end-time-face) face))
5098 (throw 'next t))
5099 (setq ts (match-string 1)
5100 te (match-string 3)
5101 ts (org-float-time
5102 (apply 'encode-time (org-parse-time-string ts)))
5103 te (org-float-time
5104 (apply 'encode-time (org-parse-time-string te)))
5105 dt (- te ts))))
5106 (cond
5107 ((> dt (* 60 maxtime))
5108 ;; a very long clocking chunk
5109 (setq issue (format "Clocking interval is very long: %s"
5110 (org-minutes-to-hh:mm-string
5111 (floor (/ (float dt) 60.))))
5112 face (or (plist-get pl :long-face) face)))
5113 ((< dt (* 60 mintime))
5114 ;; a very short clocking chunk
5115 (setq issue (format "Clocking interval is very short: %s"
5116 (org-minutes-to-hh:mm-string
5117 (floor (/ (float dt) 60.))))
5118 face (or (plist-get pl :short-face) face)))
5119 ((and (> tlend 0) (< ts tlend))
5120 ;; Two clock entries are overlapping
5121 (setq issue (format "Clocking overlap: %d minutes"
5122 (/ (- tlend ts) 60))
5123 face (or (plist-get pl :overlap-face) face)))
5124 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5125 ;; There is a gap, lets see if we need to report it
5126 (unless (org-agenda-check-clock-gap tlend ts gapok)
5127 (setq issue (format "Clocking gap: %d minutes"
5128 (/ (- ts tlend) 60))
5129 face (or (plist-get pl :gap-face) face))))
5130 (t nil)))
5131 (setq tlend (or te tlend) tlstart (or ts tlstart))
5132 (when issue
5133 ;; OK, there was some issue, add an overlay to show the issue
5134 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5135 (overlay-put ov 'before-string
5136 (concat
5137 (org-add-props
5138 (format "%-43s" (concat " " issue))
5140 'face face)
5141 "\n"))
5142 (overlay-put ov 'evaporate t)))))
5144 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5145 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5146 (catch 'exit
5147 (unless ok-list
5148 ;; there are no OK times for gaps...
5149 (throw 'exit nil))
5150 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5151 ;; This is more than 24 hours, so it is OK.
5152 ;; because we have at least one OK time, that must be in the
5153 ;; 24 hour interval.
5154 (throw 'exit t))
5155 ;; We have a shorter gap.
5156 ;; Now we have to get the minute of the day when these times are
5157 (let* ((t1dec (decode-time (seconds-to-time t1)))
5158 (t2dec (decode-time (seconds-to-time t2)))
5159 ;; compute the minute on the day
5160 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5161 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5162 (when (< min2 min1)
5163 ;; if min2 is smaller than min1, this means it is on the next day.
5164 ;; Wrap it to after midnight.
5165 (setq min2 (+ min2 1440)))
5166 ;; Now check if any of the OK times is in the gap
5167 (mapc (lambda (x)
5168 ;; Wrap the time to after midnight if necessary
5169 (if (< x min1) (setq x (+ x 1440)))
5170 ;; Check if in interval
5171 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5172 ok-list)
5173 ;; Nope, this gap is not OK
5174 nil)))
5176 (defun org-agenda-get-deadlines ()
5177 "Return the deadline information for agenda display."
5178 (let* ((props (list 'mouse-face 'highlight
5179 'org-not-done-regexp org-not-done-regexp
5180 'org-todo-regexp org-todo-regexp
5181 'org-complex-heading-regexp org-complex-heading-regexp
5182 'help-echo
5183 (format "mouse-2 or RET jump to org file %s"
5184 (abbreviate-file-name buffer-file-name))))
5185 (regexp org-deadline-time-regexp)
5186 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5187 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5188 d2 diff dfrac wdays pos pos1 category org-category-pos
5189 tags suppress-prewarning ee txt head face s todo-state
5190 show-all upcomingp donep timestr)
5191 (goto-char (point-min))
5192 (while (re-search-forward regexp nil t)
5193 (setq suppress-prewarning nil)
5194 (catch :skip
5195 (org-agenda-skip)
5196 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5197 (save-match-data
5198 (string-match org-scheduled-time-regexp
5199 (buffer-substring (point-at-bol)
5200 (point-at-eol)))))
5201 (setq suppress-prewarning
5202 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5203 org-agenda-skip-deadline-prewarning-if-scheduled
5204 0)))
5205 (setq s (match-string 1)
5206 txt nil
5207 pos (1- (match-beginning 1))
5208 todo-state (save-match-data (org-get-todo-state))
5209 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5210 (member todo-state
5211 org-agenda-repeating-timestamp-show-all))
5212 d2 (org-time-string-to-absolute
5213 (match-string 1) d1 'past show-all
5214 (current-buffer) pos)
5215 diff (- d2 d1)
5216 wdays (if suppress-prewarning
5217 (let ((org-deadline-warning-days suppress-prewarning))
5218 (org-get-wdays s))
5219 (org-get-wdays s))
5220 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5221 upcomingp (and todayp (> diff 0)))
5222 ;; When to show a deadline in the calendar:
5223 ;; If the expiration is within wdays warning time.
5224 ;; Past-due deadlines are only shown on the current date
5225 (if (and (or (and (<= diff wdays)
5226 (and todayp (not org-agenda-only-exact-dates)))
5227 (= diff 0)))
5228 (save-excursion
5229 ;; (setq todo-state (org-get-todo-state))
5230 (setq donep (member todo-state org-done-keywords))
5231 (if (and donep
5232 (or org-agenda-skip-deadline-if-done
5233 (not (= diff 0))))
5234 (setq txt nil)
5235 (setq category (org-get-category)
5236 org-category-pos (get-text-property (point) 'org-category-position))
5237 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5238 (setq txt org-agenda-no-heading-message)
5239 (goto-char (match-end 0))
5240 (setq pos1 (match-beginning 0))
5241 (setq tags (org-get-tags-at pos1))
5242 (setq head (buffer-substring-no-properties
5243 (point)
5244 (progn (skip-chars-forward "^\r\n")
5245 (point))))
5246 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5247 (setq timestr
5248 (concat (substring s (match-beginning 1)) " "))
5249 (setq timestr 'time))
5250 (setq txt (org-agenda-format-item
5251 (if (= diff 0)
5252 (car org-agenda-deadline-leaders)
5253 (if (functionp
5254 (nth 1 org-agenda-deadline-leaders))
5255 (funcall
5256 (nth 1 org-agenda-deadline-leaders)
5257 diff date)
5258 (format (nth 1 org-agenda-deadline-leaders)
5259 diff)))
5260 head category tags
5261 (if (not (= diff 0)) nil timestr)))))
5262 (when txt
5263 (setq face (org-agenda-deadline-face dfrac))
5264 (org-add-props txt props
5265 'org-marker (org-agenda-new-marker pos)
5266 'org-hd-marker (org-agenda-new-marker pos1)
5267 'priority (+ (- diff)
5268 (org-get-priority txt))
5269 'org-category category
5270 'org-category-position org-category-pos
5271 'todo-state todo-state
5272 'type (if upcomingp "upcoming-deadline" "deadline")
5273 'date (if upcomingp date d2)
5274 'face (if donep 'org-agenda-done face)
5275 'undone-face face 'done-face 'org-agenda-done)
5276 (push txt ee))))))
5277 (nreverse ee)))
5279 (defun org-agenda-deadline-face (fraction)
5280 "Return the face to displaying a deadline item.
5281 FRACTION is what fraction of the head-warning time has passed."
5282 (let ((faces org-agenda-deadline-faces) f)
5283 (catch 'exit
5284 (while (setq f (pop faces))
5285 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5287 (defun org-agenda-get-scheduled (&optional deadline-results)
5288 "Return the scheduled information for agenda display."
5289 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5290 'org-todo-regexp org-todo-regexp
5291 'org-complex-heading-regexp org-complex-heading-regexp
5292 'done-face 'org-agenda-done
5293 'mouse-face 'highlight
5294 'help-echo
5295 (format "mouse-2 or RET jump to org file %s"
5296 (abbreviate-file-name buffer-file-name))))
5297 (regexp org-scheduled-time-regexp)
5298 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5299 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5301 (deadline-position-alist
5302 (mapcar (lambda (a) (and (setq mm (get-text-property
5303 0 'org-hd-marker a))
5304 (cons (marker-position mm) a)))
5305 deadline-results))
5306 d2 diff pos pos1 category org-category-pos tags donep
5307 ee txt head pastschedp todo-state face timestr s habitp show-all)
5308 (goto-char (point-min))
5309 (while (re-search-forward regexp nil t)
5310 (catch :skip
5311 (org-agenda-skip)
5312 (setq s (match-string 1)
5313 txt nil
5314 pos (1- (match-beginning 1))
5315 todo-state (save-match-data (org-get-todo-state))
5316 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5317 (member todo-state
5318 org-agenda-repeating-timestamp-show-all))
5319 d2 (org-time-string-to-absolute
5320 (match-string 1) d1 'past show-all
5321 (current-buffer) pos)
5322 diff (- d2 d1))
5323 (setq pastschedp (and todayp (< diff 0)))
5324 ;; When to show a scheduled item in the calendar:
5325 ;; If it is on or past the date.
5326 (when (or (and (< diff 0)
5327 (< (abs diff) org-scheduled-past-days)
5328 (and todayp (not org-agenda-only-exact-dates)))
5329 (= diff 0))
5330 (save-excursion
5331 (setq donep (member todo-state org-done-keywords))
5332 (if (and donep
5333 (or org-agenda-skip-scheduled-if-done
5334 (not (= diff 0))
5335 (and (functionp 'org-is-habit-p)
5336 (org-is-habit-p))))
5337 (setq txt nil)
5338 (setq habitp (and (functionp 'org-is-habit-p)
5339 (org-is-habit-p)))
5340 (setq category (org-get-category)
5341 org-category-pos (get-text-property (point) 'org-category-position))
5342 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5343 (setq txt org-agenda-no-heading-message)
5344 (goto-char (match-end 0))
5345 (setq pos1 (match-beginning 0))
5346 (if habitp
5347 (if (or (not org-habit-show-habits)
5348 (and (not todayp)
5349 org-habit-show-habits-only-for-today))
5350 (throw :skip nil))
5351 (if (and
5352 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5353 (and org-agenda-skip-scheduled-if-deadline-is-shown
5354 pastschedp))
5355 (setq mm (assoc pos1 deadline-position-alist)))
5356 (throw :skip nil)))
5357 (setq tags (org-get-tags-at))
5358 (setq head (buffer-substring-no-properties
5359 (point)
5360 (progn (skip-chars-forward "^\r\n") (point))))
5361 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5362 (setq timestr
5363 (concat (substring s (match-beginning 1)) " "))
5364 (setq timestr 'time))
5365 (setq txt (org-agenda-format-item
5366 (if (= diff 0)
5367 (car org-agenda-scheduled-leaders)
5368 (format (nth 1 org-agenda-scheduled-leaders)
5369 (- 1 diff)))
5370 head category tags
5371 (if (not (= diff 0)) nil timestr)
5372 nil habitp))))
5373 (when txt
5374 (setq face
5375 (cond
5376 ((and (not habitp) pastschedp)
5377 'org-scheduled-previously)
5378 (todayp 'org-scheduled-today)
5379 (t 'org-scheduled))
5380 habitp (and habitp (org-habit-parse-todo)))
5381 (org-add-props txt props
5382 'undone-face face
5383 'face (if donep 'org-agenda-done face)
5384 'org-marker (org-agenda-new-marker pos)
5385 'org-hd-marker (org-agenda-new-marker pos1)
5386 'type (if pastschedp "past-scheduled" "scheduled")
5387 'date (if pastschedp d2 date)
5388 'priority (if habitp
5389 (org-habit-get-priority habitp)
5390 (+ 94 (- 5 diff) (org-get-priority txt)))
5391 'org-category category
5392 'org-category-position org-category-pos
5393 'org-habit-p habitp
5394 'todo-state todo-state)
5395 (push txt ee))))))
5396 (nreverse ee)))
5398 (defun org-agenda-get-blocks ()
5399 "Return the date-range information for agenda display."
5400 (let* ((props (list 'face nil
5401 'org-not-done-regexp org-not-done-regexp
5402 'org-todo-regexp org-todo-regexp
5403 'org-complex-heading-regexp org-complex-heading-regexp
5404 'mouse-face 'highlight
5405 'help-echo
5406 (format "mouse-2 or RET jump to org file %s"
5407 (abbreviate-file-name buffer-file-name))))
5408 (regexp org-tr-regexp)
5409 (d0 (calendar-absolute-from-gregorian date))
5410 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5411 todo-state tags pos head donep)
5412 (goto-char (point-min))
5413 (while (re-search-forward regexp nil t)
5414 (catch :skip
5415 (org-agenda-skip)
5416 (setq pos (point))
5417 (let ((start-time (match-string 1))
5418 (end-time (match-string 2)))
5419 (setq s1 (match-string 1)
5420 s2 (match-string 2)
5421 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5422 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5423 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5424 ;; Only allow days between the limits, because the normal
5425 ;; date stamps will catch the limits.
5426 (save-excursion
5427 (setq todo-state (org-get-todo-state))
5428 (setq donep (member todo-state org-done-keywords))
5429 (if (and donep org-agenda-skip-timestamp-if-done)
5430 (throw :skip t))
5431 (setq marker (org-agenda-new-marker (point)))
5432 (setq category (org-get-category)
5433 org-category-pos (get-text-property (point) 'org-category-position))
5434 (if (not (re-search-backward org-outline-regexp-bol nil t))
5435 (setq txt org-agenda-no-heading-message)
5436 (goto-char (match-beginning 0))
5437 (setq hdmarker (org-agenda-new-marker (point)))
5438 (setq tags (org-get-tags-at))
5439 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5440 (setq head (match-string 1))
5441 (let ((remove-re
5442 (if org-agenda-remove-timeranges-from-blocks
5443 (concat
5444 "<" (regexp-quote s1) ".*?>"
5445 "--"
5446 "<" (regexp-quote s2) ".*?>")
5447 nil)))
5448 (setq txt (org-agenda-format-item
5449 (format
5450 (nth (if (= d1 d2) 0 1)
5451 org-agenda-timerange-leaders)
5452 (1+ (- d0 d1)) (1+ (- d2 d1)))
5453 head category tags
5454 (cond ((and (= d1 d0) (= d2 d0))
5455 (concat "<" start-time ">--<" end-time ">"))
5456 ((= d1 d0)
5457 (concat "<" start-time ">"))
5458 ((= d2 d0)
5459 (concat "<" end-time ">"))
5460 (t nil))
5461 remove-re))))
5462 (org-add-props txt props
5463 'org-marker marker 'org-hd-marker hdmarker
5464 'type "block" 'date date
5465 'todo-state todo-state
5466 'priority (org-get-priority txt) 'org-category category
5467 'org-category-position org-category-pos)
5468 (push txt ee))))
5469 (goto-char pos)))
5470 ;; Sort the entries by expiration date.
5471 (nreverse ee)))
5473 ;;; Agenda presentation and sorting
5475 (defvar org-prefix-has-time nil
5476 "A flag, set by `org-compile-prefix-format'.
5477 The flag is set if the currently compiled format contains a `%t'.")
5478 (defvar org-prefix-has-tag nil
5479 "A flag, set by `org-compile-prefix-format'.
5480 The flag is set if the currently compiled format contains a `%T'.")
5481 (defvar org-prefix-has-effort nil
5482 "A flag, set by `org-compile-prefix-format'.
5483 The flag is set if the currently compiled format contains a `%e'.")
5484 (defvar org-prefix-category-length nil
5485 "Used by `org-compile-prefix-format' to remember the category field width.")
5486 (defvar org-prefix-category-max-length nil
5487 "Used by `org-compile-prefix-format' to remember the category field width.")
5489 (defun org-agenda-get-category-icon (category)
5490 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5491 (dolist (entry org-agenda-category-icon-alist)
5492 (when (org-string-match-p (car entry) category)
5493 (if (listp (cadr entry))
5494 (return (cadr entry))
5495 (return (apply 'create-image (cdr entry)))))))
5497 (defun org-agenda-format-item (extra txt &optional category tags dotime
5498 remove-re habitp)
5499 "Format TXT to be inserted into the agenda buffer.
5500 In particular, it adds the prefix and corresponding text properties. EXTRA
5501 must be a string and replaces the `%s' specifier in the prefix format.
5502 CATEGORY (string, symbol or nil) may be used to overrule the default
5503 category taken from local variable or file name. It will replace the `%c'
5504 specifier in the format. DOTIME, when non-nil, indicates that a
5505 time-of-day should be extracted from TXT for sorting of this entry, and for
5506 the `%t' specifier in the format. When DOTIME is a string, this string is
5507 searched for a time before TXT is. TAGS can be the tags of the headline.
5508 Any match of REMOVE-RE will be removed from TXT."
5509 (save-match-data
5510 ;; Diary entries sometimes have extra whitespace at the beginning
5511 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5513 ;; Fix the tags part in txt
5514 (setq txt (org-agenda-fix-displayed-tags
5515 txt tags
5516 org-agenda-show-inherited-tags
5517 org-agenda-hide-tags-regexp))
5518 (let* ((category (or category
5519 (if (stringp org-category)
5520 org-category
5521 (and org-category (symbol-name org-category)))
5522 (if buffer-file-name
5523 (file-name-sans-extension
5524 (file-name-nondirectory buffer-file-name))
5525 "")))
5526 (category-icon (org-agenda-get-category-icon category))
5527 (category-icon (if category-icon
5528 (propertize " " 'display category-icon)
5529 ""))
5530 ;; time, tag, effort are needed for the eval of the prefix format
5531 (tag (if tags (nth (1- (length tags)) tags) ""))
5532 time effort neffort
5533 (ts (if dotime (concat
5534 (if (stringp dotime) dotime "")
5535 (and org-agenda-search-headline-for-time txt))))
5536 (time-of-day (and dotime (org-get-time-of-day ts)))
5537 stamp plain s0 s1 s2 rtn srp l
5538 duration thecategory)
5539 (and (eq major-mode 'org-mode) buffer-file-name
5540 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5541 (when (and dotime time-of-day)
5542 ;; Extract starting and ending time and move them to prefix
5543 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5544 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5545 (setq s0 (match-string 0 ts)
5546 srp (and stamp (match-end 3))
5547 s1 (match-string (if plain 1 2) ts)
5548 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5550 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5551 ;; them, we might want to remove them there to avoid duplication.
5552 ;; The user can turn this off with a variable.
5553 (if (and org-prefix-has-time
5554 org-agenda-remove-times-when-in-prefix (or stamp plain)
5555 (string-match (concat (regexp-quote s0) " *") txt)
5556 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5557 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5558 (= (match-beginning 0) 0)
5560 (setq txt (replace-match "" nil nil txt))))
5561 ;; Normalize the time(s) to 24 hour
5562 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5563 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5565 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5566 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5567 (setq s2
5568 (org-minutes-to-hh:mm-string
5569 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5571 ;; Compute the duration
5572 (when s2
5573 (setq duration (- (org-hh:mm-string-to-minutes s2)
5574 (org-hh:mm-string-to-minutes s1)))))
5576 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5577 txt)
5578 ;; Tags are in the string
5579 (if (or (eq org-agenda-remove-tags t)
5580 (and org-agenda-remove-tags
5581 org-prefix-has-tag))
5582 (setq txt (replace-match "" t t txt))
5583 (setq txt (replace-match
5584 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5585 (match-string 2 txt))
5586 t t txt))))
5587 (when (eq major-mode 'org-mode)
5588 (setq effort
5589 (condition-case nil
5590 (org-get-effort
5591 (or (get-text-property 0 'org-hd-marker txt)
5592 (get-text-property 0 'org-marker txt)))
5593 (error nil)))
5594 (when effort
5595 (setq neffort (org-duration-string-to-minutes effort)
5596 effort (setq effort (concat "[" effort "]")))))
5597 ;; prevent erroring out with %e format when there is no effort
5598 (or effort (setq effort ""))
5600 (when remove-re
5601 (while (string-match remove-re txt)
5602 (setq txt (replace-match "" t t txt))))
5604 ;; Set org-heading property on `txt' to mark the start of the
5605 ;; heading.
5606 (add-text-properties 0 (length txt) '(org-heading t) txt)
5608 ;; Prepare the variables needed in the eval of the compiled format
5609 (setq time (cond (s2 (concat
5610 (org-agenda-time-of-day-to-ampm-maybe s1)
5611 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5612 (if org-agenda-timegrid-use-ampm " ")))
5613 (s1 (concat
5614 (org-agenda-time-of-day-to-ampm-maybe s1)
5615 (if org-agenda-timegrid-use-ampm
5616 "........ "
5617 "......")))
5618 (t ""))
5619 extra (or (and (not habitp) extra) "")
5620 category (if (symbolp category) (symbol-name category) category)
5621 thecategory (copy-sequence category))
5622 (if (string-match org-bracket-link-regexp category)
5623 (progn
5624 (setq l (if (match-end 3)
5625 (- (match-end 3) (match-beginning 3))
5626 (- (match-end 1) (match-beginning 1))))
5627 (when (< l (or org-prefix-category-length 0))
5628 (setq category (copy-sequence category))
5629 (org-add-props category nil
5630 'extra-space (make-string
5631 (- org-prefix-category-length l 1) ?\ ))))
5632 (if (and org-prefix-category-max-length
5633 (>= (length category) org-prefix-category-max-length))
5634 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5635 ;; Evaluate the compiled format
5636 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5638 ;; And finally add the text properties
5639 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5640 (org-add-props rtn nil
5641 'org-category (if thecategory (downcase thecategory) category)
5642 'tags (mapcar 'org-downcase-keep-props tags)
5643 'org-highest-priority org-highest-priority
5644 'org-lowest-priority org-lowest-priority
5645 'time-of-day time-of-day
5646 'duration duration
5647 'effort effort
5648 'effort-minutes neffort
5649 'txt txt
5650 'time time
5651 'extra extra
5652 'format org-prefix-format-compiled
5653 'dotime dotime))))
5655 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5656 "Remove tags string from TXT, and add a modified list of tags.
5657 The modified list may contain inherited tags, and tags matched by
5658 `org-agenda-hide-tags-regexp' will be removed."
5659 (when (or add-inherited hide-re)
5660 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5661 (setq txt (substring txt 0 (match-beginning 0))))
5662 (setq tags
5663 (delq nil
5664 (mapcar (lambda (tg)
5665 (if (or (and hide-re (string-match hide-re tg))
5666 (and (not add-inherited)
5667 (get-text-property 0 'inherited tg)))
5669 tg))
5670 tags)))
5671 (when tags
5672 (let ((have-i (get-text-property 0 'inherited (car tags)))
5674 (setq txt (concat txt " :"
5675 (mapconcat
5676 (lambda (x)
5677 (setq i (get-text-property 0 'inherited x))
5678 (if (and have-i (not i))
5679 (progn
5680 (setq have-i nil)
5681 (concat ":" x))
5683 tags ":")
5684 (if have-i "::" ":"))))))
5685 txt)
5687 (defun org-downcase-keep-props (s)
5688 (let ((props (text-properties-at 0 s)))
5689 (setq s (downcase s))
5690 (add-text-properties 0 (length s) props s)
5693 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5694 (defvar org-agenda-sorting-strategy-selected nil)
5696 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5697 (catch 'exit
5698 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5699 ((and todayp (member 'today (car org-agenda-time-grid))))
5700 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5701 ((member 'weekly (car org-agenda-time-grid)))
5702 (t (throw 'exit list)))
5703 (let* ((have (delq nil (mapcar
5704 (lambda (x) (get-text-property 1 'time-of-day x))
5705 list)))
5706 (string (nth 1 org-agenda-time-grid))
5707 (gridtimes (nth 2 org-agenda-time-grid))
5708 (req (car org-agenda-time-grid))
5709 (remove (member 'remove-match req))
5710 new time)
5711 (if (and (member 'require-timed req) (not have))
5712 ;; don't show empty grid
5713 (throw 'exit list))
5714 (while (setq time (pop gridtimes))
5715 (unless (and remove (member time have))
5716 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5717 (push (org-agenda-format-item
5718 nil string "" nil
5719 (concat (substring time 0 -2) ":" (substring time -2)))
5720 new)
5721 (put-text-property
5722 2 (length (car new)) 'face 'org-time-grid (car new))))
5723 (when (and todayp org-agenda-show-current-time-in-grid)
5724 (push (org-agenda-format-item
5726 org-agenda-current-time-string
5727 "" nil
5728 (format-time-string "%H:%M "))
5729 new)
5730 (put-text-property
5731 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5733 (if (member 'time-up org-agenda-sorting-strategy-selected)
5734 (append new list)
5735 (append list new)))))
5737 (defun org-compile-prefix-format (key)
5738 "Compile the prefix format into a Lisp form that can be evaluated.
5739 The resulting form is returned and stored in the variable
5740 `org-prefix-format-compiled'."
5741 (setq org-prefix-has-time nil org-prefix-has-tag nil
5742 org-prefix-category-length nil
5743 org-prefix-has-effort nil)
5744 (let ((s (cond
5745 ((stringp org-agenda-prefix-format)
5746 org-agenda-prefix-format)
5747 ((assq key org-agenda-prefix-format)
5748 (cdr (assq key org-agenda-prefix-format)))
5749 (t " %-12:c%?-12t% s")))
5750 (start 0)
5751 varform vars var e c f opt)
5752 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5753 s start)
5754 (setq var (or (cdr (assoc (match-string 4 s)
5755 '(("c" . category) ("t" . time) ("s" . extra)
5756 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5757 'eval)
5758 c (or (match-string 3 s) "")
5759 opt (match-beginning 1)
5760 start (1+ (match-beginning 0)))
5761 (if (equal var 'time) (setq org-prefix-has-time t))
5762 (if (equal var 'tag) (setq org-prefix-has-tag t))
5763 (if (equal var 'effort) (setq org-prefix-has-effort t))
5764 (setq f (concat "%" (match-string 2 s) "s"))
5765 (when (equal var 'category)
5766 (setq org-prefix-category-length
5767 (floor (abs (string-to-number (match-string 2 s)))))
5768 (setq org-prefix-category-max-length
5769 (let ((x (match-string 2 s)))
5770 (save-match-data
5771 (if (string-match "\\.[0-9]+" x)
5772 (string-to-number (substring (match-string 0 x) 1)))))))
5773 (if (eq var 'eval)
5774 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5775 (if opt
5776 (setq varform
5777 `(if (equal "" ,var)
5779 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5780 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5781 (setq s (replace-match "%s" t nil s))
5782 (push varform vars))
5783 (setq vars (nreverse vars))
5784 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5786 (defun org-set-sorting-strategy (key)
5787 (if (symbolp (car org-agenda-sorting-strategy))
5788 ;; the old format
5789 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5790 (setq org-agenda-sorting-strategy-selected
5791 (or (cdr (assq key org-agenda-sorting-strategy))
5792 (cdr (assq 'agenda org-agenda-sorting-strategy))
5793 '(time-up category-keep priority-down)))))
5795 (defun org-get-time-of-day (s &optional string mod24)
5796 "Check string S for a time of day.
5797 If found, return it as a military time number between 0 and 2400.
5798 If not found, return nil.
5799 The optional STRING argument forces conversion into a 5 character wide string
5800 HH:MM."
5801 (save-match-data
5802 (when
5803 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5804 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5805 (let* ((h (string-to-number (match-string 1 s)))
5806 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5807 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5808 (am-p (equal ampm "am"))
5809 (h1 (cond ((not ampm) h)
5810 ((= h 12) (if am-p 0 12))
5811 (t (+ h (if am-p 0 12)))))
5812 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5813 (mod h1 24) h1))
5814 (t0 (+ (* 100 h2) m))
5815 (t1 (concat (if (>= h1 24) "+" " ")
5816 (if (and org-agenda-time-leading-zero
5817 (< t0 1000)) "0" "")
5818 (if (< t0 100) "0" "")
5819 (if (< t0 10) "0" "")
5820 (int-to-string t0))))
5821 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5823 (defvar org-agenda-before-sorting-filter-function nil
5824 "Function to be applied to agenda items prior to sorting.
5825 Prior to sorting also means just before they are inserted into the agenda.
5827 To aid sorting, you may revisit the original entries and add more text
5828 properties which will later be used by the sorting functions.
5830 The function should take a string argument, an agenda line.
5831 It has access to the text properties in that line, which contain among
5832 other things, the property `org-hd-marker' that points to the entry
5833 where the line comes from. Note that not all lines going into the agenda
5834 have this property, only most.
5836 The function should return the modified string. It is probably best
5837 to ONLY change text properties.
5839 You can also use this function as a filter, by returning nil for lines
5840 you don't want to have in the agenda at all. For this application, you
5841 could bind the variable in the options section of a custom command.")
5843 (defun org-finalize-agenda-entries (list &optional nosort)
5844 "Sort and concatenate the agenda items."
5845 (setq list (mapcar 'org-agenda-highlight-todo list))
5846 (if nosort
5847 list
5848 (when org-agenda-before-sorting-filter-function
5849 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5850 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5852 (defun org-agenda-highlight-todo (x)
5853 (let ((org-done-keywords org-done-keywords-for-agenda)
5854 (case-fold-search nil)
5856 (if (eq x 'line)
5857 (save-excursion
5858 (beginning-of-line 1)
5859 (setq re (org-get-at-bol 'org-todo-regexp))
5860 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5861 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5862 (add-text-properties (match-beginning 0) (match-end 1)
5863 (list 'face (org-get-todo-face 1)))
5864 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5865 (delete-region (match-beginning 1) (1- (match-end 0)))
5866 (goto-char (match-beginning 1))
5867 (insert (format org-agenda-todo-keyword-format s)))))
5868 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5869 (setq re (get-text-property 0 'org-todo-regexp x))
5870 (when (and re
5871 ;; Test `pl' because if there's no heading content,
5872 ;; there's no point matching to highlight. Note
5873 ;; that if we didn't test `pl' first, and there
5874 ;; happened to be no keyword from `org-todo-regexp'
5875 ;; on this heading line, then the `equal' comparison
5876 ;; afterwards would spuriously succeed in the case
5877 ;; where `pl' is nil -- causing an args-out-of-range
5878 ;; error when we try to add text properties to text
5879 ;; that isn't there.
5881 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5882 x pl) pl))
5883 (add-text-properties
5884 (or (match-end 1) (match-end 0)) (match-end 0)
5885 (list 'face (org-get-todo-face (match-string 2 x)))
5887 (when (match-end 1)
5888 (setq x (concat (substring x 0 (match-end 1))
5889 (format org-agenda-todo-keyword-format
5890 (match-string 2 x))
5891 (org-add-props " " (text-properties-at 0 x))
5892 (substring x (match-end 3)))))))
5893 x)))
5895 (defsubst org-cmp-priority (a b)
5896 "Compare the priorities of string A and B."
5897 (let ((pa (or (get-text-property 1 'priority a) 0))
5898 (pb (or (get-text-property 1 'priority b) 0)))
5899 (cond ((> pa pb) +1)
5900 ((< pa pb) -1)
5901 (t nil))))
5903 (defsubst org-cmp-effort (a b)
5904 "Compare the effort values of string A and B."
5905 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5906 (ea (or (get-text-property 1 'effort-minutes a) def))
5907 (eb (or (get-text-property 1 'effort-minutes b) def)))
5908 (cond ((> ea eb) +1)
5909 ((< ea eb) -1)
5910 (t nil))))
5912 (defsubst org-cmp-category (a b)
5913 "Compare the string values of categories of strings A and B."
5914 (let ((ca (or (get-text-property 1 'org-category a) ""))
5915 (cb (or (get-text-property 1 'org-category b) "")))
5916 (cond ((string-lessp ca cb) -1)
5917 ((string-lessp cb ca) +1)
5918 (t nil))))
5920 (defsubst org-cmp-todo-state (a b)
5921 "Compare the todo states of strings A and B."
5922 (let* ((ma (or (get-text-property 1 'org-marker a)
5923 (get-text-property 1 'org-hd-marker a)))
5924 (mb (or (get-text-property 1 'org-marker b)
5925 (get-text-property 1 'org-hd-marker b)))
5926 (fa (and ma (marker-buffer ma)))
5927 (fb (and mb (marker-buffer mb)))
5928 (todo-kwds
5929 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5930 (and fb (with-current-buffer fb org-todo-keywords-1))))
5931 (ta (or (get-text-property 1 'todo-state a) ""))
5932 (tb (or (get-text-property 1 'todo-state b) ""))
5933 (la (- (length (member ta todo-kwds))))
5934 (lb (- (length (member tb todo-kwds))))
5935 (donepa (member ta org-done-keywords-for-agenda))
5936 (donepb (member tb org-done-keywords-for-agenda)))
5937 (cond ((and donepa (not donepb)) -1)
5938 ((and (not donepa) donepb) +1)
5939 ((< la lb) -1)
5940 ((< lb la) +1)
5941 (t nil))))
5943 (defsubst org-cmp-alpha (a b)
5944 "Compare the headlines, alphabetically."
5945 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5946 (plb (text-property-any 0 (length b) 'org-heading t b))
5947 (ta (and pla (substring a pla)))
5948 (tb (and plb (substring b plb))))
5949 (when pla
5950 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5951 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5952 (setq ta (substring ta (match-end 0))))
5953 (setq ta (downcase ta)))
5954 (when plb
5955 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5956 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5957 (setq tb (substring tb (match-end 0))))
5958 (setq tb (downcase tb)))
5959 (cond ((not ta) +1)
5960 ((not tb) -1)
5961 ((string-lessp ta tb) -1)
5962 ((string-lessp tb ta) +1)
5963 (t nil))))
5965 (defsubst org-cmp-tag (a b)
5966 "Compare the string values of the first tags of A and B."
5967 (let ((ta (car (last (get-text-property 1 'tags a))))
5968 (tb (car (last (get-text-property 1 'tags b)))))
5969 (cond ((not ta) +1)
5970 ((not tb) -1)
5971 ((string-lessp ta tb) -1)
5972 ((string-lessp tb ta) +1)
5973 (t nil))))
5975 (defsubst org-cmp-time (a b)
5976 "Compare the time-of-day values of strings A and B."
5977 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5978 (ta (or (get-text-property 1 'time-of-day a) def))
5979 (tb (or (get-text-property 1 'time-of-day b) def)))
5980 (cond ((< ta tb) -1)
5981 ((< tb ta) +1)
5982 (t nil))))
5984 (defsubst org-cmp-habit-p (a b)
5985 "Compare the todo states of strings A and B."
5986 (let ((ha (get-text-property 1 'org-habit-p a))
5987 (hb (get-text-property 1 'org-habit-p b)))
5988 (cond ((and ha (not hb)) -1)
5989 ((and (not ha) hb) +1)
5990 (t nil))))
5992 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5994 (defun org-entries-lessp (a b)
5995 "Predicate for sorting agenda entries."
5996 ;; The following variables will be used when the form is evaluated.
5997 ;; So even though the compiler complains, keep them.
5998 (let* ((ss org-agenda-sorting-strategy-selected)
5999 (time-up (and (org-em 'time-up 'time-down ss)
6000 (org-cmp-time a b)))
6001 (time-down (if time-up (- time-up) nil))
6002 (priority-up (and (org-em 'priority-up 'priority-down ss)
6003 (org-cmp-priority a b)))
6004 (priority-down (if priority-up (- priority-up) nil))
6005 (effort-up (and (org-em 'effort-up 'effort-down ss)
6006 (org-cmp-effort a b)))
6007 (effort-down (if effort-up (- effort-up) nil))
6008 (category-up (and (or (org-em 'category-up 'category-down ss)
6009 (memq 'category-keep ss))
6010 (org-cmp-category a b)))
6011 (category-down (if category-up (- category-up) nil))
6012 (category-keep (if category-up +1 nil))
6013 (tag-up (and (org-em 'tag-up 'tag-down ss)
6014 (org-cmp-tag a b)))
6015 (tag-down (if tag-up (- tag-up) nil))
6016 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6017 (org-cmp-todo-state a b)))
6018 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6019 (habit-up (and (org-em 'habit-up 'habit-down ss)
6020 (org-cmp-habit-p a b)))
6021 (habit-down (if habit-up (- habit-up) nil))
6022 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6023 (org-cmp-alpha a b)))
6024 (alpha-down (if alpha-up (- alpha-up) nil))
6025 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6026 user-defined-up user-defined-down)
6027 (if (and need-user-cmp org-agenda-cmp-user-defined
6028 (functionp org-agenda-cmp-user-defined))
6029 (setq user-defined-up
6030 (funcall org-agenda-cmp-user-defined a b)
6031 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6032 (cdr (assoc
6033 (eval (cons 'or org-agenda-sorting-strategy-selected))
6034 '((-1 . t) (1 . nil) (nil . nil))))))
6036 ;;; Agenda restriction lock
6038 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6039 "Overlay to mark the headline to which agenda commands are restricted.")
6040 (overlay-put org-agenda-restriction-lock-overlay
6041 'face 'org-agenda-restriction-lock)
6042 (overlay-put org-agenda-restriction-lock-overlay
6043 'help-echo "Agendas are currently limited to this subtree.")
6044 (org-detach-overlay org-agenda-restriction-lock-overlay)
6046 (defun org-agenda-set-restriction-lock (&optional type)
6047 "Set restriction lock for agenda, to current subtree or file.
6048 Restriction will be the file if TYPE is `file', or if type is the
6049 universal prefix '(4), or if the cursor is before the first headline
6050 in the file. Otherwise, restriction will be to the current subtree."
6051 (interactive "P")
6052 (and (equal type '(4)) (setq type 'file))
6053 (setq type (cond
6054 (type type)
6055 ((org-at-heading-p) 'subtree)
6056 ((condition-case nil (org-back-to-heading t) (error nil))
6057 'subtree)
6058 (t 'file)))
6059 (if (eq type 'subtree)
6060 (progn
6061 (setq org-agenda-restrict t)
6062 (setq org-agenda-overriding-restriction 'subtree)
6063 (put 'org-agenda-files 'org-restrict
6064 (list (buffer-file-name (buffer-base-buffer))))
6065 (org-back-to-heading t)
6066 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6067 (move-marker org-agenda-restrict-begin (point))
6068 (move-marker org-agenda-restrict-end
6069 (save-excursion (org-end-of-subtree t)))
6070 (message "Locking agenda restriction to subtree"))
6071 (put 'org-agenda-files 'org-restrict
6072 (list (buffer-file-name (buffer-base-buffer))))
6073 (setq org-agenda-restrict nil)
6074 (setq org-agenda-overriding-restriction 'file)
6075 (move-marker org-agenda-restrict-begin nil)
6076 (move-marker org-agenda-restrict-end nil)
6077 (message "Locking agenda restriction to file"))
6078 (setq current-prefix-arg nil)
6079 (org-agenda-maybe-redo))
6081 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6082 "Remove the agenda restriction lock."
6083 (interactive "P")
6084 (org-detach-overlay org-agenda-restriction-lock-overlay)
6085 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6086 (setq org-agenda-overriding-restriction nil)
6087 (setq org-agenda-restrict nil)
6088 (put 'org-agenda-files 'org-restrict nil)
6089 (move-marker org-agenda-restrict-begin nil)
6090 (move-marker org-agenda-restrict-end nil)
6091 (setq current-prefix-arg nil)
6092 (message "Agenda restriction lock removed")
6093 (or noupdate (org-agenda-maybe-redo)))
6095 (defun org-agenda-maybe-redo ()
6096 "If there is any window showing the agenda view, update it."
6097 (let ((w (get-buffer-window org-agenda-buffer-name t))
6098 (w0 (selected-window)))
6099 (when w
6100 (select-window w)
6101 (org-agenda-redo)
6102 (select-window w0)
6103 (if org-agenda-overriding-restriction
6104 (message "Agenda view shifted to new %s restriction"
6105 org-agenda-overriding-restriction)
6106 (message "Agenda restriction lock removed")))))
6108 ;;; Agenda commands
6110 (defun org-agenda-check-type (error &rest types)
6111 "Check if agenda buffer is of allowed type.
6112 If ERROR is non-nil, throw an error, otherwise just return nil."
6113 (if (memq org-agenda-type types)
6115 (if error
6116 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6117 nil)))
6119 (defun org-agenda-quit ()
6120 "Exit agenda by removing the window or the buffer."
6121 (interactive)
6122 (if org-agenda-columns-active
6123 (org-columns-quit)
6124 (let ((buf (current-buffer)))
6125 (if (eq org-agenda-window-setup 'other-frame)
6126 (progn
6127 (kill-buffer buf)
6128 (org-agenda-reset-markers)
6129 (org-columns-remove-overlays)
6130 (setq org-agenda-archives-mode nil)
6131 (delete-frame))
6132 (and (not (eq org-agenda-window-setup 'current-window))
6133 (not (one-window-p))
6134 (delete-window))
6135 (kill-buffer buf)
6136 (org-agenda-reset-markers)
6137 (org-columns-remove-overlays)
6138 (setq org-agenda-archives-mode nil)))
6139 ;; Maybe restore the pre-agenda window configuration.
6140 (and org-agenda-restore-windows-after-quit
6141 (not (eq org-agenda-window-setup 'other-frame))
6142 org-pre-agenda-window-conf
6143 (set-window-configuration org-pre-agenda-window-conf))))
6145 (defun org-agenda-exit ()
6146 "Exit agenda by removing the window or the buffer.
6147 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6148 Org-mode buffers visited directly by the user will not be touched."
6149 (interactive)
6150 (org-release-buffers org-agenda-new-buffers)
6151 (setq org-agenda-new-buffers nil)
6152 (org-agenda-quit))
6154 (defun org-agenda-execute (arg)
6155 "Execute another agenda command, keeping same window.
6156 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6157 in the agenda."
6158 (interactive "P")
6159 (let ((org-agenda-window-setup 'current-window))
6160 (org-agenda arg)))
6162 (defun org-agenda-redo ()
6163 "Rebuild Agenda.
6164 When this is the global TODO list, a prefix argument will be interpreted."
6165 (interactive)
6166 (let* ((org-agenda-keep-modes t)
6167 (tag-filter org-agenda-tag-filter)
6168 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
6169 (cat-filter org-agenda-category-filter)
6170 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6171 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
6172 (cols org-agenda-columns-active)
6173 (line (org-current-line))
6174 (window-line (- line (org-current-line (window-start))))
6175 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6176 (put 'org-agenda-tag-filter :preset-filter nil)
6177 (put 'org-agenda-category-filter :preset-filter nil)
6178 (and cols (org-columns-quit))
6179 (message "Rebuilding agenda buffer...")
6180 (org-let lprops '(eval org-agenda-redo-command))
6181 (setq org-agenda-undo-list nil
6182 org-agenda-pending-undo-list nil)
6183 (message "Rebuilding agenda buffer...done")
6184 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6185 (put 'org-agenda-category-filter :preset-filter cat-preset)
6186 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6187 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
6188 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6189 (org-goto-line line)
6190 (recenter window-line)))
6192 (defvar org-global-tags-completion-table nil)
6193 (defvar org-agenda-filter-form nil)
6195 (defun org-agenda-filter-by-category (strip)
6196 "Keep only those lines in the agenda buffer that have a specific category.
6197 The category is that of the current line."
6198 (interactive "P")
6199 (if org-agenda-filtered-by-category
6200 (org-agenda-filter-show-all-cat)
6201 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6202 (if cat (org-agenda-filter-apply
6203 (list (concat (if strip "-" "+") cat)) 'category)
6204 (error "No category at point")))))
6206 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6207 "Keep only those lines in the agenda buffer that have a specific tag.
6208 The tag is selected with its fast selection letter, as configured.
6209 With prefix argument STRIP, remove all lines that do have the tag.
6210 A lisp caller can specify CHAR. NARROW means that the new tag should be
6211 used to narrow the search - the interactive user can also press `-' or `+'
6212 to switch to narrowing."
6213 (interactive "P")
6214 (let* ((alist org-tag-alist-for-agenda)
6215 (tag-chars (mapconcat
6216 (lambda (x) (if (and (not (symbolp (car x)))
6217 (cdr x))
6218 (char-to-string (cdr x))
6219 ""))
6220 alist ""))
6221 (efforts (org-split-string
6222 (or (cdr (assoc (concat org-effort-property "_ALL")
6223 org-global-properties))
6224 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6225 "")))
6226 (effort-op org-agenda-filter-effort-default-operator)
6227 (effort-prompt "")
6228 (inhibit-read-only t)
6229 (current org-agenda-tag-filter)
6230 maybe-refresh a n tag)
6231 (unless char
6232 (message
6233 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6234 (if narrow "Narrow" "Filter") tag-chars
6235 (if org-agenda-auto-exclude-function "[RET], " ""))
6236 (setq char (read-char-exclusive)))
6237 (when (member char '(?+ ?-))
6238 ;; Narrowing down
6239 (cond ((equal char ?-) (setq strip t narrow t))
6240 ((equal char ?+) (setq strip nil narrow t)))
6241 (message
6242 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6243 (setq char (read-char-exclusive)))
6244 (when (member char '(?< ?> ?= ??))
6245 ;; An effort operator
6246 (setq effort-op (char-to-string char))
6247 (setq alist nil) ; to make sure it will be interpreted as effort.
6248 (unless (equal char ??)
6249 (loop for i from 0 to 9 do
6250 (setq effort-prompt
6251 (concat
6252 effort-prompt " ["
6253 (if (= i 9) "0" (int-to-string (1+ i)))
6254 "]" (nth i efforts))))
6255 (message "Effort%s: %s " effort-op effort-prompt)
6256 (setq char (read-char-exclusive))
6257 (when (or (< char ?0) (> char ?9))
6258 (error "Need 1-9,0 to select effort" ))))
6259 (when (equal char ?\t)
6260 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6261 (org-set-local 'org-global-tags-completion-table
6262 (org-global-tags-completion-table)))
6263 (let ((completion-ignore-case t))
6264 (setq tag (org-icompleting-read
6265 "Tag: " org-global-tags-completion-table))))
6266 (cond
6267 ((equal char ?\r)
6268 (org-agenda-filter-show-all-tag)
6269 (when org-agenda-auto-exclude-function
6270 (setq org-agenda-tag-filter '())
6271 (dolist (tag (org-agenda-get-represented-tags))
6272 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6273 (if modifier
6274 (push modifier org-agenda-tag-filter))))
6275 (if (not (null org-agenda-tag-filter))
6276 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
6277 (setq maybe-refresh t))
6278 ((equal char ?/)
6279 (org-agenda-filter-show-all-tag)
6280 (when (get 'org-agenda-tag-filter :preset-filter)
6281 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
6282 (setq maybe-refresh t))
6283 ((equal char ?. )
6284 (setq org-agenda-tag-filter
6285 (mapcar (lambda(tag) (concat "+" tag))
6286 (org-get-at-bol 'tags)))
6287 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6288 (setq maybe-refresh t))
6289 ((or (equal char ?\ )
6290 (setq a (rassoc char alist))
6291 (and (>= char ?0) (<= char ?9)
6292 (setq n (if (= char ?0) 9 (- char ?0 1))
6293 tag (concat effort-op (nth n efforts))
6294 a (cons tag nil)))
6295 (and (= char ??)
6296 (setq tag "?eff")
6297 a (cons tag nil))
6298 (and tag (setq a (cons tag nil))))
6299 (org-agenda-filter-show-all-tag)
6300 (setq tag (car a))
6301 (setq org-agenda-tag-filter
6302 (cons (concat (if strip "-" "+") tag)
6303 (if narrow current nil)))
6304 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6305 (setq maybe-refresh t))
6306 (t (error "Invalid tag selection character %c" char)))
6307 (when (and maybe-refresh
6308 (eq org-agenda-clockreport-mode 'with-filter))
6309 (org-agenda-redo))))
6311 (defun org-agenda-get-represented-tags ()
6312 "Get a list of all tags currently represented in the agenda."
6313 (let (p tags)
6314 (save-excursion
6315 (goto-char (point-min))
6316 (while (setq p (next-single-property-change (point) 'tags))
6317 (goto-char p)
6318 (mapc (lambda (x) (add-to-list 'tags x))
6319 (get-text-property (point) 'tags))))
6320 tags))
6322 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6323 "Refine the current filter. See `org-agenda-filter-by-tag'."
6324 (interactive "P")
6325 (org-agenda-filter-by-tag strip char 'refine))
6327 (defun org-agenda-filter-make-matcher ()
6328 "Create the form that tests a line for agenda filter."
6329 (let (f f1)
6330 ;; first compute the tag-filter matcher
6331 (dolist (x (delete-dups
6332 (append (get 'org-agenda-tag-filter
6333 :preset-filter) org-agenda-tag-filter)))
6334 (if (member x '("-" "+"))
6335 (setq f1 (if (equal x "-") 'tags '(not tags)))
6336 (if (string-match "[<=>?]" x)
6337 (setq f1 (org-agenda-filter-effort-form x))
6338 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6339 (if (equal (string-to-char x) ?-)
6340 (setq f1 (list 'not f1))))
6341 (push f1 f))
6342 ;; then compute the category-filter matcher
6343 (dolist (x (delete-dups
6344 (append (get 'org-agenda-category-filter
6345 :preset-filter) org-agenda-category-filter)))
6346 (setq f1 (list 'equal (substring x 1) 'cat))
6347 (push f1 f))
6348 (cons 'and (nreverse f))))
6350 (defun org-agenda-filter-effort-form (e)
6351 "Return the form to compare the effort of the current line with what E says.
6352 E looks like \"+<2:25\"."
6353 (let (op)
6354 (setq e (substring e 1))
6355 (setq op (string-to-char e) e (substring e 1))
6356 (setq op (cond ((equal op ?<) '<=)
6357 ((equal op ?>) '>=)
6358 ((equal op ??) op)
6359 (t '=)))
6360 (list 'org-agenda-compare-effort (list 'quote op)
6361 (org-duration-string-to-minutes e))))
6363 (defun org-agenda-compare-effort (op value)
6364 "Compare the effort of the current line with VALUE, using OP.
6365 If the line does not have an effort defined, return nil."
6366 (let ((eff (org-get-at-bol 'effort-minutes)))
6367 (if (equal op ??)
6368 (not eff)
6369 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6370 value))))
6372 (defvar org-agenda-filtered-by-category nil)
6373 (defun org-agenda-filter-apply (filter type)
6374 "Set FILTER as the new agenda filter and apply it."
6375 (let (tags)
6376 (if (eq type 'tag)
6377 (setq org-agenda-tag-filter filter)
6378 (setq org-agenda-category-filter filter
6379 org-agenda-filtered-by-category t))
6380 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
6381 (org-agenda-set-mode-name)
6382 (save-excursion
6383 (goto-char (point-min))
6384 (while (not (eobp))
6385 (if (org-get-at-bol 'org-marker)
6386 (progn
6387 (setq tags (org-get-at-bol 'tags) ; used in eval
6388 cat (get-text-property (point) 'org-category))
6389 (if (not (eval org-agenda-filter-form))
6390 (org-agenda-filter-hide-line type))
6391 (beginning-of-line 2))
6392 (beginning-of-line 2))))
6393 (if (get-char-property (point) 'invisible)
6394 (org-agenda-previous-line))))
6396 (defun org-agenda-filter-hide-line (type)
6397 (let (ov)
6398 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6399 (point-at-eol)))
6400 (overlay-put ov 'invisible t)
6401 (overlay-put ov 'type type)
6402 (if (eq type 'tag)
6403 (push ov org-agenda-tag-filter-overlays)
6404 (push ov org-agenda-cat-filter-overlays))))
6406 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6407 (setq pos (or pos (point)))
6408 (save-excursion
6409 (dolist (ov (overlays-at pos))
6410 (when (and (overlay-get ov 'invisible)
6411 (eq (overlay-get ov 'type) 'tag))
6412 (goto-char pos)
6413 (if (< (overlay-start ov) (point-at-eol))
6414 (move-overlay ov (point-at-eol)
6415 (overlay-end ov)))))))
6417 (defun org-agenda-filter-show-all-tag nil
6418 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
6419 (setq org-agenda-tag-filter-overlays nil
6420 org-agenda-tag-filter nil
6421 org-agenda-filter-form nil)
6422 (org-agenda-set-mode-name))
6424 (defun org-agenda-filter-show-all-cat nil
6425 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
6426 (setq org-agenda-cat-filter-overlays nil
6427 org-agenda-filtered-by-category nil
6428 org-agenda-category-filter nil
6429 org-agenda-filter-form nil)
6430 (org-agenda-set-mode-name))
6432 (defun org-agenda-manipulate-query-add ()
6433 "Manipulate the query by adding a search term with positive selection.
6434 Positive selection means the term must be matched for selection of an entry."
6435 (interactive)
6436 (org-agenda-manipulate-query ?\[))
6437 (defun org-agenda-manipulate-query-subtract ()
6438 "Manipulate the query by adding a search term with negative selection.
6439 Negative selection means term must not be matched for selection of an entry."
6440 (interactive)
6441 (org-agenda-manipulate-query ?\]))
6442 (defun org-agenda-manipulate-query-add-re ()
6443 "Manipulate the query by adding a search regexp with positive selection.
6444 Positive selection means the regexp must match for selection of an entry."
6445 (interactive)
6446 (org-agenda-manipulate-query ?\{))
6447 (defun org-agenda-manipulate-query-subtract-re ()
6448 "Manipulate the query by adding a search regexp with negative selection.
6449 Negative selection means regexp must not match for selection of an entry."
6450 (interactive)
6451 (org-agenda-manipulate-query ?\}))
6452 (defun org-agenda-manipulate-query (char)
6453 (cond
6454 ((memq org-agenda-type '(timeline agenda))
6455 (let ((org-agenda-include-inactive-timestamps t))
6456 (org-agenda-redo))
6457 (message "Display now includes inactive timestamps as well"))
6458 ((eq org-agenda-type 'search)
6459 (org-add-to-string
6460 'org-agenda-query-string
6461 (if org-agenda-last-search-view-search-was-boolean
6462 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6463 (?\{ . " +{}") (?\} . " -{}"))))
6464 " "))
6465 (setq org-agenda-redo-command
6466 (list 'org-search-view
6467 org-todo-only
6468 org-agenda-query-string
6469 (+ (length org-agenda-query-string)
6470 (if (member char '(?\{ ?\})) 0 1))))
6471 (set-register org-agenda-query-register org-agenda-query-string)
6472 (org-agenda-redo))
6473 (t (error "Cannot manipulate query for %s-type agenda buffers"
6474 org-agenda-type))))
6476 (defun org-add-to-string (var string)
6477 (set var (concat (symbol-value var) string)))
6479 (defun org-agenda-goto-date (date)
6480 "Jump to DATE in agenda."
6481 (interactive (list (let ((org-read-date-prefer-future
6482 (eval org-agenda-jump-prefer-future)))
6483 (org-read-date))))
6484 (org-agenda-list nil date))
6486 (defun org-agenda-goto-today ()
6487 "Go to today."
6488 (interactive)
6489 (org-agenda-check-type t 'timeline 'agenda)
6490 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6491 (cond
6492 (tdpos (goto-char tdpos))
6493 ((eq org-agenda-type 'agenda)
6494 (let* ((sd (org-agenda-compute-starting-span
6495 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6496 (org-agenda-overriding-arguments org-agenda-last-arguments))
6497 (setf (nth 1 org-agenda-overriding-arguments) sd)
6498 (org-agenda-redo)
6499 (org-agenda-find-same-or-today-or-agenda)))
6500 (t (error "Cannot find today")))))
6502 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6503 (goto-char
6504 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6505 (text-property-any (point-min) (point-max) 'org-today t)
6506 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6507 (point-min))))
6509 (defun org-agenda-later (arg)
6510 "Go forward in time by thee current span.
6511 With prefix ARG, go forward that many times the current span."
6512 (interactive "p")
6513 (org-agenda-check-type t 'agenda)
6514 (let* ((span org-agenda-current-span)
6515 (sd org-starting-day)
6516 (greg (calendar-gregorian-from-absolute sd))
6517 (cnt (org-get-at-bol 'org-day-cnt))
6518 greg2)
6519 (cond
6520 ((eq span 'day)
6521 (setq sd (+ arg sd)))
6522 ((eq span 'week)
6523 (setq sd (+ (* 7 arg) sd)))
6524 ((eq span 'month)
6525 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6526 sd (calendar-absolute-from-gregorian greg2))
6527 (setcar greg2 (1+ (car greg2))))
6528 ((eq span 'year)
6529 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6530 sd (calendar-absolute-from-gregorian greg2))
6531 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6533 (setq sd (+ (* span arg) sd))))
6534 (let ((org-agenda-overriding-arguments
6535 (list (car org-agenda-last-arguments) sd span t)))
6536 (org-agenda-redo)
6537 (org-agenda-find-same-or-today-or-agenda cnt))))
6539 (defun org-agenda-earlier (arg)
6540 "Go backward in time by the current span.
6541 With prefix ARG, go backward that many times the current span."
6542 (interactive "p")
6543 (org-agenda-later (- arg)))
6545 (defun org-agenda-view-mode-dispatch ()
6546 "Call one of the view mode commands."
6547 (interactive)
6548 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6549 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6550 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6551 [E]ntryText")
6552 (let ((a (read-char-exclusive)))
6553 (case a
6554 (?\ (call-interactively 'org-agenda-reset-view))
6555 (?d (call-interactively 'org-agenda-day-view))
6556 (?w (call-interactively 'org-agenda-week-view))
6557 (?m (call-interactively 'org-agenda-month-view))
6558 (?y (call-interactively 'org-agenda-year-view))
6559 (?l (call-interactively 'org-agenda-log-mode))
6560 (?L (org-agenda-log-mode '(4)))
6561 (?c (org-agenda-log-mode 'clockcheck))
6562 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6563 (?a (call-interactively 'org-agenda-archives-mode))
6564 (?A (org-agenda-archives-mode 'files))
6565 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6566 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6567 (?G (call-interactively 'org-agenda-toggle-time-grid))
6568 (?D (call-interactively 'org-agenda-toggle-diary))
6569 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6570 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6571 (org-agenda-check-type t 'timeline 'agenda)
6572 (org-agenda-redo))
6573 (message "Display now includes inactive timestamps as well"))
6574 (?q (message "Abort"))
6575 (otherwise (error "Invalid key" )))))
6577 (defun org-agenda-reset-view ()
6578 "Switch to default view for agenda."
6579 (interactive)
6580 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6581 (defun org-agenda-day-view (&optional day-of-year)
6582 "Switch to daily view for agenda.
6583 With argument DAY-OF-YEAR, switch to that day of the year."
6584 (interactive "P")
6585 (org-agenda-change-time-span 'day day-of-year))
6586 (defun org-agenda-week-view (&optional iso-week)
6587 "Switch to daily view for agenda.
6588 With argument ISO-WEEK, switch to the corresponding ISO week.
6589 If ISO-WEEK has more then 2 digits, only the last two encode the
6590 week. Any digits before this encode a year. So 200712 means
6591 week 12 of year 2007. Years in the range 1938-2037 can also be
6592 written as 2-digit years."
6593 (interactive "P")
6594 (org-agenda-change-time-span 'week iso-week))
6595 (defun org-agenda-month-view (&optional month)
6596 "Switch to monthly view for agenda.
6597 With argument MONTH, switch to that month."
6598 (interactive "P")
6599 (org-agenda-change-time-span 'month month))
6600 (defun org-agenda-year-view (&optional year)
6601 "Switch to yearly view for agenda.
6602 With argument YEAR, switch to that year.
6603 If MONTH has more then 2 digits, only the last two encode the
6604 month. Any digits before this encode a year. So 200712 means
6605 December year 2007. Years in the range 1938-2037 can also be
6606 written as 2-digit years."
6607 (interactive "P")
6608 (when year
6609 (setq year (org-small-year-to-year year)))
6610 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6611 (org-agenda-change-time-span 'year year)
6612 (error "Abort")))
6614 (defun org-agenda-change-time-span (span &optional n)
6615 "Change the agenda view to SPAN.
6616 SPAN may be `day', `week', `month', `year'."
6617 (org-agenda-check-type t 'agenda)
6618 (if (and (not n) (equal org-agenda-current-span span))
6619 (error "Viewing span is already \"%s\"" span))
6620 (let* ((sd (or (org-get-at-bol 'day)
6621 org-starting-day))
6622 (sd (org-agenda-compute-starting-span sd span n))
6623 (org-agenda-overriding-arguments
6624 (or org-agenda-overriding-arguments
6625 (list (car org-agenda-last-arguments) sd span t))))
6626 (org-agenda-redo)
6627 (org-agenda-find-same-or-today-or-agenda))
6628 (org-agenda-set-mode-name)
6629 (message "Switched to %s view" span))
6631 (defun org-agenda-compute-starting-span (sd span &optional n)
6632 "Compute starting date for agenda.
6633 SPAN may be `day', `week', `month', `year'. The return value
6634 is a cons cell with the starting date and the number of days,
6635 so that the date SD will be in that range."
6636 (let* ((greg (calendar-gregorian-from-absolute sd))
6637 (dg (nth 1 greg))
6638 (mg (car greg))
6639 (yg (nth 2 greg)))
6640 (cond
6641 ((eq span 'day)
6642 (when n
6643 (setq sd (+ (calendar-absolute-from-gregorian
6644 (list mg 1 yg))
6645 n -1))))
6646 ((eq span 'week)
6647 (let* ((nt (calendar-day-of-week
6648 (calendar-gregorian-from-absolute sd)))
6649 (d (if org-agenda-start-on-weekday
6650 (- nt org-agenda-start-on-weekday)
6653 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6654 (when n
6655 (require 'cal-iso)
6656 (when (> n 99)
6657 (setq y1 (org-small-year-to-year (/ n 100))
6658 n (mod n 100)))
6659 (setq sd
6660 (calendar-absolute-from-iso
6661 (list n 1
6662 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6663 ((eq span 'month)
6664 (let (y1)
6665 (when (and n (> n 99))
6666 (setq y1 (org-small-year-to-year (/ n 100))
6667 n (mod n 100)))
6668 (setq sd (calendar-absolute-from-gregorian
6669 (list (or n mg) 1 (or y1 yg))))))
6670 ((eq span 'year)
6671 (setq sd (calendar-absolute-from-gregorian
6672 (list 1 1 (or n yg))))))
6673 sd))
6675 (defun org-agenda-next-date-line (&optional arg)
6676 "Jump to the next line indicating a date in agenda buffer."
6677 (interactive "p")
6678 (org-agenda-check-type t 'agenda 'timeline)
6679 (beginning-of-line 1)
6680 ;; This does not work if user makes date format that starts with a blank
6681 (if (looking-at "^\\S-") (forward-char 1))
6682 (if (not (re-search-forward "^\\S-" nil t arg))
6683 (progn
6684 (backward-char 1)
6685 (error "No next date after this line in this buffer")))
6686 (goto-char (match-beginning 0)))
6688 (defun org-agenda-previous-date-line (&optional arg)
6689 "Jump to the previous line indicating a date in agenda buffer."
6690 (interactive "p")
6691 (org-agenda-check-type t 'agenda 'timeline)
6692 (beginning-of-line 1)
6693 (if (not (re-search-backward "^\\S-" nil t arg))
6694 (error "No previous date before this line in this buffer")))
6696 ;; Initialize the highlight
6697 (defvar org-hl (make-overlay 1 1))
6698 (overlay-put org-hl 'face 'highlight)
6700 (defun org-highlight (begin end &optional buffer)
6701 "Highlight a region with overlay."
6702 (move-overlay org-hl begin end (or buffer (current-buffer))))
6704 (defun org-unhighlight ()
6705 "Detach overlay INDEX."
6706 (org-detach-overlay org-hl))
6708 ;; FIXME this is currently not used.
6709 (defun org-highlight-until-next-command (beg end &optional buffer)
6710 "Move the highlight overlay to BEG/END, remove it before the next command."
6711 (org-highlight beg end buffer)
6712 (add-hook 'pre-command-hook 'org-unhighlight-once))
6713 (defun org-unhighlight-once ()
6714 "Remove the highlight from its position, and this function from the hook."
6715 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6716 (org-unhighlight))
6718 (defun org-agenda-follow-mode ()
6719 "Toggle follow mode in an agenda buffer."
6720 (interactive)
6721 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6722 (org-agenda-set-mode-name)
6723 (org-agenda-do-context-action)
6724 (message "Follow mode is %s"
6725 (if org-agenda-follow-mode "on" "off")))
6727 (defun org-agenda-entry-text-mode (&optional arg)
6728 "Toggle entry text mode in an agenda buffer."
6729 (interactive "P")
6730 (setq org-agenda-entry-text-mode (or (integerp arg)
6731 (not org-agenda-entry-text-mode)))
6732 (org-agenda-entry-text-hide)
6733 (and org-agenda-entry-text-mode
6734 (let ((org-agenda-entry-text-maxlines
6735 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6736 (org-agenda-entry-text-show)))
6737 (org-agenda-set-mode-name)
6738 (message "Entry text mode is %s. Maximum number of lines is %d"
6739 (if org-agenda-entry-text-mode "on" "off")
6740 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6742 (defun org-agenda-clockreport-mode (&optional with-filter)
6743 "Toggle clocktable mode in an agenda buffer.
6744 With prefix arg WITH-FILTER, make the clocktable respect the current
6745 agenda filter."
6746 (interactive "P")
6747 (org-agenda-check-type t 'agenda)
6748 (if with-filter
6749 (setq org-agenda-clockreport-mode 'with-filter)
6750 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6751 (org-agenda-set-mode-name)
6752 (org-agenda-redo)
6753 (message "Clocktable mode is %s"
6754 (if org-agenda-clockreport-mode "on" "off")))
6756 (defun org-agenda-log-mode (&optional special)
6757 "Toggle log mode in an agenda buffer.
6758 With argument SPECIAL, show all possible log items, not only the ones
6759 configured in `org-agenda-log-mode-items'.
6760 With a double `C-u' prefix arg, show *only* log items, nothing else."
6761 (interactive "P")
6762 (org-agenda-check-type t 'agenda 'timeline)
6763 (setq org-agenda-show-log
6764 (cond
6765 ((equal special '(16)) 'only)
6766 ((eq special 'clockcheck)
6767 (if (eq org-agenda-show-log 'clockcheck)
6768 nil 'clockcheck))
6769 (special '(closed clock state))
6770 (t (not org-agenda-show-log))))
6771 (org-agenda-set-mode-name)
6772 (org-agenda-redo)
6773 (message "Log mode is %s"
6774 (if org-agenda-show-log "on" "off")))
6776 (defun org-agenda-archives-mode (&optional with-files)
6777 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6778 When called with a prefix argument, include all archive files as well."
6779 (interactive "P")
6780 (setq org-agenda-archives-mode
6781 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6782 (org-agenda-set-mode-name)
6783 (org-agenda-redo)
6784 (message
6785 "%s"
6786 (cond
6787 ((eq org-agenda-archives-mode nil)
6788 "No archives are included")
6789 ((eq org-agenda-archives-mode 'trees)
6790 (format "Trees with :%s: tag are included" org-archive-tag))
6791 ((eq org-agenda-archives-mode t)
6792 (format "Trees with :%s: tag and all active archive files are included"
6793 org-archive-tag)))))
6795 (defun org-agenda-toggle-diary ()
6796 "Toggle diary inclusion in an agenda buffer."
6797 (interactive)
6798 (org-agenda-check-type t 'agenda)
6799 (setq org-agenda-include-diary (not org-agenda-include-diary))
6800 (org-agenda-redo)
6801 (org-agenda-set-mode-name)
6802 (message "Diary inclusion turned %s"
6803 (if org-agenda-include-diary "on" "off")))
6805 (defun org-agenda-toggle-deadlines ()
6806 "Toggle inclusion of entries with a deadline in an agenda buffer."
6807 (interactive)
6808 (org-agenda-check-type t 'agenda)
6809 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6810 (org-agenda-redo)
6811 (org-agenda-set-mode-name)
6812 (message "Deadlines inclusion turned %s"
6813 (if org-agenda-include-deadlines "on" "off")))
6815 (defun org-agenda-toggle-time-grid ()
6816 "Toggle time grid in an agenda buffer."
6817 (interactive)
6818 (org-agenda-check-type t 'agenda)
6819 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6820 (org-agenda-redo)
6821 (org-agenda-set-mode-name)
6822 (message "Time-grid turned %s"
6823 (if org-agenda-use-time-grid "on" "off")))
6825 (defun org-agenda-set-mode-name ()
6826 "Set the mode name to indicate all the small mode settings."
6827 (setq mode-name
6828 (list "Org-Agenda"
6829 (if (get 'org-agenda-files 'org-restrict) " []" "")
6831 '(:eval (org-agenda-span-name org-agenda-current-span))
6832 (if org-agenda-follow-mode " Follow" "")
6833 (if org-agenda-entry-text-mode " ETxt" "")
6834 (if org-agenda-include-diary " Diary" "")
6835 (if org-agenda-include-deadlines " Ddl" "")
6836 (if org-agenda-use-time-grid " Grid" "")
6837 (if (and (boundp 'org-habit-show-habits)
6838 org-habit-show-habits) " Habit" "")
6839 (cond
6840 ((consp org-agenda-show-log) " LogAll")
6841 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6842 (org-agenda-show-log " Log")
6843 (t ""))
6844 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
6845 :preset-filter))
6846 '(:eval (org-propertize
6847 (concat " <"
6848 (mapconcat
6849 'identity
6850 (append
6851 (get 'org-agenda-category-filter :preset-filter)
6852 org-agenda-category-filter)
6854 ">")
6855 'face 'org-agenda-filter-category
6856 'help-echo "Category used in filtering"))
6858 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
6859 :preset-filter))
6860 '(:eval (org-propertize
6861 (concat " {"
6862 (mapconcat
6863 'identity
6864 (append
6865 (get 'org-agenda-tag-filter :preset-filter)
6866 org-agenda-tag-filter)
6868 "}")
6869 'face 'org-agenda-filter-tags
6870 'help-echo "Tags used in filtering"))
6872 (if org-agenda-archives-mode
6873 (if (eq org-agenda-archives-mode t)
6874 " Archives"
6875 (format " :%s:" org-archive-tag))
6877 (if org-agenda-clockreport-mode
6878 (if (eq org-agenda-clockreport-mode 'with-filter)
6879 " Clock{}" " Clock")
6880 "")))
6881 (force-mode-line-update))
6883 (defun org-agenda-post-command-hook ()
6884 (setq org-agenda-type
6885 (or (get-text-property (point) 'org-agenda-type)
6886 (get-text-property (max (point-min) (1- (point)))
6887 'org-agenda-type))))
6889 (defun org-agenda-next-line ()
6890 "Move cursor to the next line, and show if follow mode is active."
6891 (interactive)
6892 (call-interactively 'next-line)
6893 (org-agenda-do-context-action))
6895 (defun org-agenda-previous-line ()
6896 "Move cursor to the previous line, and show if follow-mode is active."
6897 (interactive)
6898 (call-interactively 'previous-line)
6899 (org-agenda-do-context-action))
6901 (defun org-agenda-do-context-action ()
6902 "Show outline path and, maybe, follow mode window."
6903 (let ((m (org-get-at-bol 'org-marker)))
6904 (when (and (markerp m) (marker-buffer m))
6905 (and org-agenda-follow-mode
6906 (if org-agenda-follow-indirect
6907 (org-agenda-tree-to-indirect-buffer)
6908 (org-agenda-show)))
6909 (and org-agenda-show-outline-path
6910 (org-with-point-at m (org-display-outline-path t))))))
6912 (defun org-agenda-show-priority ()
6913 "Show the priority of the current item.
6914 This priority is composed of the main priority given with the [#A] cookies,
6915 and by additional input from the age of a schedules or deadline entry."
6916 (interactive)
6917 (let* ((pri (org-get-at-bol 'priority)))
6918 (message "Priority is %d" (if pri pri -1000))))
6920 (defun org-agenda-show-tags ()
6921 "Show the tags applicable to the current item."
6922 (interactive)
6923 (let* ((tags (org-get-at-bol 'tags)))
6924 (if tags
6925 (message "Tags are :%s:"
6926 (org-no-properties (mapconcat 'identity tags ":")))
6927 (message "No tags associated with this line"))))
6929 (defun org-agenda-goto (&optional highlight)
6930 "Go to the Org-mode file which contains the item at point."
6931 (interactive)
6932 (let* ((marker (or (org-get-at-bol 'org-marker)
6933 (org-agenda-error)))
6934 (buffer (marker-buffer marker))
6935 (pos (marker-position marker)))
6936 (switch-to-buffer-other-window buffer)
6937 (widen)
6938 (push-mark)
6939 (goto-char pos)
6940 (when (eq major-mode 'org-mode)
6941 (org-show-context 'agenda)
6942 (save-excursion
6943 (and (outline-next-heading)
6944 (org-flag-heading nil)))) ; show the next heading
6945 (when (outline-invisible-p)
6946 (show-entry)) ; display invisible text
6947 (recenter (/ (window-height) 2))
6948 (run-hooks 'org-agenda-after-show-hook)
6949 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6951 (defvar org-agenda-after-show-hook nil
6952 "Normal hook run after an item has been shown from the agenda.
6953 Point is in the buffer where the item originated.")
6955 (defun org-agenda-kill ()
6956 "Kill the entry or subtree belonging to the current agenda entry."
6957 (interactive)
6958 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6959 (let* ((marker (or (org-get-at-bol 'org-marker)
6960 (org-agenda-error)))
6961 (buffer (marker-buffer marker))
6962 (pos (marker-position marker))
6963 (type (org-get-at-bol 'type))
6964 dbeg dend (n 0) conf)
6965 (org-with-remote-undo buffer
6966 (with-current-buffer buffer
6967 (save-excursion
6968 (goto-char pos)
6969 (if (and (eq major-mode 'org-mode) (not (member type '("sexp"))))
6970 (setq dbeg (progn (org-back-to-heading t) (point))
6971 dend (org-end-of-subtree t t))
6972 (setq dbeg (point-at-bol)
6973 dend (min (point-max) (1+ (point-at-eol)))))
6974 (goto-char dbeg)
6975 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6976 (setq conf (or (eq t org-agenda-confirm-kill)
6977 (and (numberp org-agenda-confirm-kill)
6978 (> n org-agenda-confirm-kill))))
6979 (and conf
6980 (not (y-or-n-p
6981 (format "Delete entry with %d lines in buffer \"%s\"? "
6982 n (buffer-name buffer))))
6983 (error "Abort"))
6984 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6985 (with-current-buffer buffer (delete-region dbeg dend))
6986 (message "Agenda item and source killed"))))
6988 (defvar org-archive-default-command)
6989 (defun org-agenda-archive-default ()
6990 "Archive the entry or subtree belonging to the current agenda entry."
6991 (interactive)
6992 (require 'org-archive)
6993 (org-agenda-archive-with org-archive-default-command))
6995 (defun org-agenda-archive-default-with-confirmation ()
6996 "Archive the entry or subtree belonging to the current agenda entry."
6997 (interactive)
6998 (require 'org-archive)
6999 (org-agenda-archive-with org-archive-default-command 'confirm))
7001 (defun org-agenda-archive ()
7002 "Archive the entry or subtree belonging to the current agenda entry."
7003 (interactive)
7004 (org-agenda-archive-with 'org-archive-subtree))
7006 (defun org-agenda-archive-to-archive-sibling ()
7007 "Move the entry to the archive sibling."
7008 (interactive)
7009 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7011 (defun org-agenda-archive-with (cmd &optional confirm)
7012 "Move the entry to the archive sibling."
7013 (interactive)
7014 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7015 (let* ((marker (or (org-get-at-bol 'org-marker)
7016 (org-agenda-error)))
7017 (buffer (marker-buffer marker))
7018 (pos (marker-position marker)))
7019 (org-with-remote-undo buffer
7020 (with-current-buffer buffer
7021 (if (eq major-mode 'org-mode)
7022 (if (and confirm
7023 (not (y-or-n-p "Archive this subtree or entry? ")))
7024 (error "Abort")
7025 (save-excursion
7026 (goto-char pos)
7027 (org-remove-subtree-entries-from-agenda)
7028 (org-back-to-heading t)
7029 (funcall cmd)))
7030 (error "Archiving works only in Org-mode files"))))))
7032 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7033 "Remove all lines in the agenda that correspond to a given subtree.
7034 The subtree is the one in buffer BUF, starting at BEG and ending at END.
7035 If this information is not given, the function uses the tree at point."
7036 (let ((buf (or buf (current-buffer))) m p)
7037 (save-excursion
7038 (unless (and beg end)
7039 (org-back-to-heading t)
7040 (setq beg (point))
7041 (org-end-of-subtree t)
7042 (setq end (point)))
7043 (set-buffer (get-buffer org-agenda-buffer-name))
7044 (save-excursion
7045 (goto-char (point-max))
7046 (beginning-of-line 1)
7047 (while (not (bobp))
7048 (when (and (setq m (org-get-at-bol 'org-marker))
7049 (equal buf (marker-buffer m))
7050 (setq p (marker-position m))
7051 (>= p beg)
7052 (< p end))
7053 (let ((inhibit-read-only t))
7054 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7055 (beginning-of-line 0))))))
7057 (defun org-agenda-refile (&optional goto rfloc no-update)
7058 "Refile the item at point."
7059 (interactive "P")
7060 (if (equal goto '(16))
7061 (org-refile-goto-last-stored)
7062 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7063 (org-agenda-error)))
7064 (buffer (marker-buffer marker))
7065 (pos (marker-position marker))
7066 (rfloc (or rfloc
7067 (org-refile-get-location
7068 (if goto "Goto" "Refile to") buffer
7069 org-refile-allow-creating-parent-nodes))))
7070 (with-current-buffer buffer
7071 (save-excursion
7072 (save-restriction
7073 (widen)
7074 (goto-char marker)
7075 (org-remove-subtree-entries-from-agenda)
7076 (org-refile goto buffer rfloc)))))
7077 (unless no-update (org-agenda-redo))))
7079 (defun org-agenda-open-link (&optional arg)
7080 "Follow the link in the current line, if any.
7081 This looks for a link in the displayed line in the agenda. It also looks
7082 at the text of the entry itself."
7083 (interactive "P")
7084 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7085 (org-get-at-bol 'org-marker)))
7086 (buffer (and marker (marker-buffer marker)))
7087 (prefix (buffer-substring
7088 (point-at-bol) (point-at-eol))))
7089 (cond
7090 (buffer
7091 (with-current-buffer buffer
7092 (save-excursion
7093 (save-restriction
7094 (widen)
7095 (goto-char marker)
7096 (org-offer-links-in-entry arg prefix)))))
7097 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7098 (save-excursion
7099 (beginning-of-line 1)
7100 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7101 (org-open-link-from-string (match-string 1)))
7102 (t (error "No link to open here")))))
7104 (defun org-agenda-copy-local-variable (var)
7105 "Get a variable from a referenced buffer and install it here."
7106 (let ((m (org-get-at-bol 'org-marker)))
7107 (when (and m (buffer-live-p (marker-buffer m)))
7108 (org-set-local var (with-current-buffer (marker-buffer m)
7109 (symbol-value var))))))
7111 (defun org-agenda-switch-to (&optional delete-other-windows)
7112 "Go to the Org-mode file which contains the item at point."
7113 (interactive)
7114 (if (and org-return-follows-link
7115 (not (org-get-at-bol 'org-marker))
7116 (org-in-regexp org-bracket-link-regexp))
7117 (org-open-link-from-string (match-string 0))
7118 (let* ((marker (or (org-get-at-bol 'org-marker)
7119 (org-agenda-error)))
7120 (buffer (marker-buffer marker))
7121 (pos (marker-position marker)))
7122 (org-pop-to-buffer-same-window buffer)
7123 (and delete-other-windows (delete-other-windows))
7124 (widen)
7125 (goto-char pos)
7126 (when (eq major-mode 'org-mode)
7127 (org-show-context 'agenda)
7128 (save-excursion
7129 (and (outline-next-heading)
7130 (org-flag-heading nil))) ; show the next heading
7131 (when (outline-invisible-p)
7132 (show-entry)))))) ; display invisible text
7134 (defun org-agenda-goto-mouse (ev)
7135 "Go to the Org-mode file which contains the item at the mouse click."
7136 (interactive "e")
7137 (mouse-set-point ev)
7138 (org-agenda-goto))
7140 (defun org-agenda-show (&optional full-entry)
7141 "Display the Org-mode file which contains the item at point.
7142 With prefix argument FULL-ENTRY, make the entire entry visible
7143 if it was hidden in the outline."
7144 (interactive "P")
7145 (let ((win (selected-window)))
7146 (if full-entry
7147 (let ((org-show-entry-below t))
7148 (org-agenda-goto t))
7149 (org-agenda-goto t))
7150 (select-window win)))
7152 (defvar org-agenda-show-window nil)
7153 (defun org-agenda-show-and-scroll-up ()
7154 "Display the Org-mode file which contains the item at point.
7155 When called repeatedly, scroll the window that is displaying the buffer."
7156 (interactive)
7157 (let ((win (selected-window)))
7158 (if (and (window-live-p org-agenda-show-window)
7159 (eq this-command last-command))
7160 (progn
7161 (select-window org-agenda-show-window)
7162 (ignore-errors (scroll-up)))
7163 (org-agenda-goto t)
7164 (show-subtree)
7165 (setq org-agenda-show-window (selected-window)))
7166 (select-window win)))
7168 (defun org-agenda-show-scroll-down ()
7169 "Scroll down the window showing the agenda."
7170 (interactive)
7171 (let ((win (selected-window)))
7172 (when (window-live-p org-agenda-show-window)
7173 (select-window org-agenda-show-window)
7174 (ignore-errors (scroll-down))
7175 (select-window win))))
7177 (defun org-agenda-show-1 (&optional more)
7178 "Display the Org-mode file which contains the item at point.
7179 The prefix arg selects the amount of information to display:
7181 0 hide the subtree
7182 1 just show the entry according to defaults.
7183 2 show the children view
7184 3 show the subtree view
7185 4 show the entire subtree and any LOGBOOK drawers
7186 5 show the entire subtree and any drawers
7187 With prefix argument FULL-ENTRY, make the entire entry visible
7188 if it was hidden in the outline."
7189 (interactive "p")
7190 (let ((win (selected-window)))
7191 (org-agenda-goto t)
7192 (org-recenter-heading 1)
7193 (cond
7194 ((= more 0)
7195 (hide-subtree)
7196 (save-excursion
7197 (org-back-to-heading)
7198 (run-hook-with-args 'org-cycle-hook 'folded))
7199 (message "Remote: FOLDED"))
7200 ((and (org-called-interactively-p 'any) (= more 1))
7201 (message "Remote: show with default settings"))
7202 ((= more 2)
7203 (show-entry)
7204 (show-children)
7205 (save-excursion
7206 (org-back-to-heading)
7207 (run-hook-with-args 'org-cycle-hook 'children))
7208 (message "Remote: CHILDREN"))
7209 ((= more 3)
7210 (show-subtree)
7211 (save-excursion
7212 (org-back-to-heading)
7213 (run-hook-with-args 'org-cycle-hook 'subtree))
7214 (message "Remote: SUBTREE"))
7215 ((= more 4)
7216 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7217 (org-drawer-regexp
7218 (concat "^[ \t]*:\\("
7219 (mapconcat 'regexp-quote org-drawers "\\|")
7220 "\\):[ \t]*$")))
7221 (show-subtree)
7222 (save-excursion
7223 (org-back-to-heading)
7224 (org-cycle-hide-drawers 'subtree)))
7225 (message "Remote: SUBTREE AND LOGBOOK"))
7226 ((> more 4)
7227 (show-subtree)
7228 (message "Remote: SUBTREE AND ALL DRAWERS")))
7229 (select-window win)))
7231 (defun org-recenter-heading (n)
7232 (save-excursion
7233 (org-back-to-heading)
7234 (recenter n)))
7236 (defvar org-agenda-cycle-counter nil)
7237 (defun org-agenda-cycle-show (&optional n)
7238 "Show the current entry in another window, with default settings.
7239 Default settings are taken from `org-show-hierarchy-above' and siblings.
7240 When use repeatedly in immediate succession, the remote entry will cycle
7241 through visibility
7243 children -> subtree -> folded
7245 When called with a numeric prefix arg, that arg will be passed through to
7246 `org-agenda-show-1'. For the interpretation of that argument, see the
7247 docstring of `org-agenda-show-1'."
7248 (interactive "P")
7249 (if (integerp n)
7250 (setq org-agenda-cycle-counter n)
7251 (if (not (eq last-command this-command))
7252 (setq org-agenda-cycle-counter 1)
7253 (if (equal org-agenda-cycle-counter 0)
7254 (setq org-agenda-cycle-counter 2)
7255 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7256 (if (> org-agenda-cycle-counter 3)
7257 (setq org-agenda-cycle-counter 0)))))
7258 (org-agenda-show-1 org-agenda-cycle-counter))
7260 (defun org-agenda-recenter (arg)
7261 "Display the Org-mode file which contains the item at point and recenter."
7262 (interactive "P")
7263 (let ((win (selected-window)))
7264 (org-agenda-goto t)
7265 (recenter arg)
7266 (select-window win)))
7268 (defun org-agenda-show-mouse (ev)
7269 "Display the Org-mode file which contains the item at the mouse click."
7270 (interactive "e")
7271 (mouse-set-point ev)
7272 (org-agenda-show))
7274 (defun org-agenda-check-no-diary ()
7275 "Check if the entry is a diary link and abort if yes."
7276 (if (org-get-at-bol 'org-agenda-diary-link)
7277 (org-agenda-error)))
7279 (defun org-agenda-error ()
7280 (error "Command not allowed in this line"))
7282 (defun org-agenda-tree-to-indirect-buffer ()
7283 "Show the subtree corresponding to the current entry in an indirect buffer.
7284 This calls the command `org-tree-to-indirect-buffer' from the original
7285 Org-mode buffer.
7286 With numerical prefix arg ARG, go up to this level and then take that tree.
7287 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7288 use the dedicated frame)."
7289 (interactive)
7290 (if (and current-prefix-arg (listp current-prefix-arg))
7291 (org-agenda-do-tree-to-indirect-buffer)
7292 (let ((agenda-window (selected-window))
7293 (indirect-window
7294 (and org-last-indirect-buffer
7295 (get-buffer-window org-last-indirect-buffer))))
7296 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer))
7297 (unwind-protect
7298 (progn
7299 (unless (and indirect-window (window-live-p indirect-window))
7300 (setq indirect-window (split-window agenda-window)))
7301 (select-window indirect-window)
7302 (switch-to-buffer org-last-indirect-buffer :norecord)
7303 (fit-window-to-buffer indirect-window))
7304 (select-window (get-buffer-window org-agenda-buffer-name))))))
7306 (defun org-agenda-do-tree-to-indirect-buffer ()
7307 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7308 (org-agenda-check-no-diary)
7309 (let* ((marker (or (org-get-at-bol 'org-marker)
7310 (org-agenda-error)))
7311 (buffer (marker-buffer marker))
7312 (pos (marker-position marker)))
7313 (with-current-buffer buffer
7314 (save-excursion
7315 (goto-char pos)
7316 (call-interactively 'org-tree-to-indirect-buffer)))))
7318 (defvar org-last-heading-marker (make-marker)
7319 "Marker pointing to the headline that last changed its TODO state
7320 by a remote command from the agenda.")
7322 (defun org-agenda-todo-nextset ()
7323 "Switch TODO entry to next sequence."
7324 (interactive)
7325 (org-agenda-todo 'nextset))
7327 (defun org-agenda-todo-previousset ()
7328 "Switch TODO entry to previous sequence."
7329 (interactive)
7330 (org-agenda-todo 'previousset))
7332 (defun org-agenda-todo (&optional arg)
7333 "Cycle TODO state of line at point, also in Org-mode file.
7334 This changes the line at point, all other lines in the agenda referring to
7335 the same tree node, and the headline of the tree node in the Org-mode file."
7336 (interactive "P")
7337 (org-agenda-check-no-diary)
7338 (let* ((col (current-column))
7339 (marker (or (org-get-at-bol 'org-marker)
7340 (org-agenda-error)))
7341 (buffer (marker-buffer marker))
7342 (pos (marker-position marker))
7343 (hdmarker (org-get-at-bol 'org-hd-marker))
7344 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7345 (inhibit-read-only t)
7346 org-agenda-headline-snapshot-before-repeat newhead just-one)
7347 (org-with-remote-undo buffer
7348 (with-current-buffer buffer
7349 (widen)
7350 (goto-char pos)
7351 (org-show-context 'agenda)
7352 (save-excursion
7353 (and (outline-next-heading)
7354 (org-flag-heading nil))) ; show the next heading
7355 (let ((current-prefix-arg arg))
7356 (call-interactively 'org-todo))
7357 (and (bolp) (forward-char 1))
7358 (setq newhead (org-get-heading))
7359 (when (and (org-bound-and-true-p
7360 org-agenda-headline-snapshot-before-repeat)
7361 (not (equal org-agenda-headline-snapshot-before-repeat
7362 newhead))
7363 todayp)
7364 (setq newhead org-agenda-headline-snapshot-before-repeat
7365 just-one t))
7366 (save-excursion
7367 (org-back-to-heading)
7368 (move-marker org-last-heading-marker (point))))
7369 (beginning-of-line 1)
7370 (save-excursion
7371 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7372 (org-move-to-column col))))
7374 (defun org-agenda-add-note (&optional arg)
7375 "Add a time-stamped note to the entry at point."
7376 (interactive "P")
7377 (org-agenda-check-no-diary)
7378 (let* ((marker (or (org-get-at-bol 'org-marker)
7379 (org-agenda-error)))
7380 (buffer (marker-buffer marker))
7381 (pos (marker-position marker))
7382 (hdmarker (org-get-at-bol 'org-hd-marker))
7383 (inhibit-read-only t))
7384 (with-current-buffer buffer
7385 (widen)
7386 (goto-char pos)
7387 (org-show-context 'agenda)
7388 (save-excursion
7389 (and (outline-next-heading)
7390 (org-flag-heading nil))) ; show the next heading
7391 (org-add-note))))
7393 (defun org-agenda-change-all-lines (newhead hdmarker
7394 &optional fixface just-this)
7395 "Change all lines in the agenda buffer which match HDMARKER.
7396 The new content of the line will be NEWHEAD (as modified by
7397 `org-agenda-format-item'). HDMARKER is checked with
7398 `equal' against all `org-hd-marker' text properties in the file.
7399 If FIXFACE is non-nil, the face of each item is modified according to
7400 the new TODO state.
7401 If JUST-THIS is non-nil, change just the current line, not all.
7402 If FORCE-TAGS is non nil, the car of it returns the new tags."
7403 (let* ((inhibit-read-only t)
7404 (line (org-current-line))
7405 (thetags (with-current-buffer (marker-buffer hdmarker)
7406 (save-excursion (save-restriction (widen)
7407 (goto-char hdmarker)
7408 (org-get-tags-at)))))
7409 props m pl undone-face done-face finish new dotime cat tags)
7410 (save-excursion
7411 (goto-char (point-max))
7412 (beginning-of-line 1)
7413 (while (not finish)
7414 (setq finish (bobp))
7415 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7416 (or (not just-this) (= (org-current-line) line))
7417 (equal m hdmarker))
7418 (setq props (text-properties-at (point))
7419 dotime (org-get-at-bol 'dotime)
7420 cat (org-get-at-bol 'org-category)
7421 tags thetags
7423 (let ((org-prefix-format-compiled
7424 (or (get-text-property (point) 'format)
7425 org-prefix-format-compiled)))
7426 (with-current-buffer (marker-buffer hdmarker)
7427 (save-excursion
7428 (save-restriction
7429 (widen)
7430 (org-agenda-format-item (org-get-at-bol 'extra)
7431 newhead cat tags dotime)))))
7432 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7433 undone-face (org-get-at-bol 'undone-face)
7434 done-face (org-get-at-bol 'done-face))
7435 (beginning-of-line 1)
7436 (cond
7437 ((equal new "")
7438 (and (looking-at ".*\n?") (replace-match "")))
7439 ((looking-at ".*")
7440 (replace-match new t t)
7441 (beginning-of-line 1)
7442 (add-text-properties (point-at-bol) (point-at-eol) props)
7443 (when fixface
7444 (add-text-properties
7445 (point-at-bol) (point-at-eol)
7446 (list 'face
7447 (if org-last-todo-state-is-todo
7448 undone-face done-face))))
7449 (org-agenda-highlight-todo 'line)
7450 (beginning-of-line 1))
7451 (t (error "Line update did not work"))))
7452 (beginning-of-line 0)))
7453 (org-finalize-agenda)))
7455 (defun org-agenda-align-tags (&optional line)
7456 "Align all tags in agenda items to `org-agenda-tags-column'."
7457 (let ((inhibit-read-only t) l c)
7458 (save-excursion
7459 (goto-char (if line (point-at-bol) (point-min)))
7460 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7461 (if line (point-at-eol) nil) t)
7462 (add-text-properties
7463 (match-beginning 2) (match-end 2)
7464 (list 'face (delq nil (let ((prop (get-text-property
7465 (match-beginning 2) 'face)))
7466 (or (listp prop) (setq prop (list prop)))
7467 (if (memq 'org-tag prop)
7468 prop
7469 (cons 'org-tag prop))))))
7470 (setq l (- (match-end 2) (match-beginning 2))
7471 c (if (< org-agenda-tags-column 0)
7472 (- (abs org-agenda-tags-column) l)
7473 org-agenda-tags-column))
7474 (delete-region (match-beginning 1) (match-end 1))
7475 (goto-char (match-beginning 1))
7476 (insert (org-add-props
7477 (make-string (max 1 (- c (current-column))) ?\ )
7478 (plist-put (copy-sequence (text-properties-at (point)))
7479 'face nil))))
7480 (goto-char (point-min))
7481 (org-font-lock-add-tag-faces (point-max)))))
7483 (defun org-agenda-priority-up ()
7484 "Increase the priority of line at point, also in Org-mode file."
7485 (interactive)
7486 (org-agenda-priority 'up))
7488 (defun org-agenda-priority-down ()
7489 "Decrease the priority of line at point, also in Org-mode file."
7490 (interactive)
7491 (org-agenda-priority 'down))
7493 (defun org-agenda-priority (&optional force-direction)
7494 "Set the priority of line at point, also in Org-mode file.
7495 This changes the line at point, all other lines in the agenda referring to
7496 the same tree node, and the headline of the tree node in the Org-mode file."
7497 (interactive)
7498 (unless org-enable-priority-commands
7499 (error "Priority commands are disabled"))
7500 (org-agenda-check-no-diary)
7501 (let* ((marker (or (org-get-at-bol 'org-marker)
7502 (org-agenda-error)))
7503 (hdmarker (org-get-at-bol 'org-hd-marker))
7504 (buffer (marker-buffer hdmarker))
7505 (pos (marker-position hdmarker))
7506 (inhibit-read-only t)
7507 newhead)
7508 (org-with-remote-undo buffer
7509 (with-current-buffer buffer
7510 (widen)
7511 (goto-char pos)
7512 (org-show-context 'agenda)
7513 (save-excursion
7514 (and (outline-next-heading)
7515 (org-flag-heading nil))) ; show the next heading
7516 (funcall 'org-priority force-direction)
7517 (end-of-line 1)
7518 (setq newhead (org-get-heading)))
7519 (org-agenda-change-all-lines newhead hdmarker)
7520 (beginning-of-line 1))))
7522 ;; FIXME: should fix the tags property of the agenda line.
7523 (defun org-agenda-set-tags (&optional tag onoff)
7524 "Set tags for the current headline."
7525 (interactive)
7526 (org-agenda-check-no-diary)
7527 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7528 (call-interactively 'org-change-tag-in-region)
7529 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7530 (org-agenda-error)))
7531 (buffer (marker-buffer hdmarker))
7532 (pos (marker-position hdmarker))
7533 (inhibit-read-only t)
7534 newhead)
7535 (org-with-remote-undo buffer
7536 (with-current-buffer buffer
7537 (widen)
7538 (goto-char pos)
7539 (save-excursion
7540 (org-show-context 'agenda))
7541 (save-excursion
7542 (and (outline-next-heading)
7543 (org-flag-heading nil))) ; show the next heading
7544 (goto-char pos)
7545 (if tag
7546 (org-toggle-tag tag onoff)
7547 (call-interactively 'org-set-tags))
7548 (end-of-line 1)
7549 (setq newhead (org-get-heading)))
7550 (org-agenda-change-all-lines newhead hdmarker)
7551 (beginning-of-line 1)))))
7553 (defun org-agenda-set-property ()
7554 "Set a property for the current headline."
7555 (interactive)
7556 (org-agenda-check-no-diary)
7557 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7558 (org-agenda-error)))
7559 (buffer (marker-buffer hdmarker))
7560 (pos (marker-position hdmarker))
7561 (inhibit-read-only t)
7562 newhead)
7563 (org-with-remote-undo buffer
7564 (with-current-buffer buffer
7565 (widen)
7566 (goto-char pos)
7567 (save-excursion
7568 (org-show-context 'agenda))
7569 (save-excursion
7570 (and (outline-next-heading)
7571 (org-flag-heading nil))) ; show the next heading
7572 (goto-char pos)
7573 (call-interactively 'org-set-property)))))
7575 (defun org-agenda-set-effort ()
7576 "Set the effort property for the current headline."
7577 (interactive)
7578 (org-agenda-check-no-diary)
7579 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7580 (org-agenda-error)))
7581 (buffer (marker-buffer hdmarker))
7582 (pos (marker-position hdmarker))
7583 (inhibit-read-only t)
7584 newhead)
7585 (org-with-remote-undo buffer
7586 (with-current-buffer buffer
7587 (widen)
7588 (goto-char pos)
7589 (save-excursion
7590 (org-show-context 'agenda))
7591 (save-excursion
7592 (and (outline-next-heading)
7593 (org-flag-heading nil))) ; show the next heading
7594 (goto-char pos)
7595 (call-interactively 'org-set-effort)
7596 (end-of-line 1)
7597 (setq newhead (org-get-heading)))
7598 (org-agenda-change-all-lines newhead hdmarker))))
7600 (defun org-agenda-toggle-archive-tag ()
7601 "Toggle the archive tag for the current entry."
7602 (interactive)
7603 (org-agenda-check-no-diary)
7604 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7605 (org-agenda-error)))
7606 (buffer (marker-buffer hdmarker))
7607 (pos (marker-position hdmarker))
7608 (inhibit-read-only t)
7609 newhead)
7610 (org-with-remote-undo buffer
7611 (with-current-buffer buffer
7612 (widen)
7613 (goto-char pos)
7614 (org-show-context 'agenda)
7615 (save-excursion
7616 (and (outline-next-heading)
7617 (org-flag-heading nil))) ; show the next heading
7618 (call-interactively 'org-toggle-archive-tag)
7619 (end-of-line 1)
7620 (setq newhead (org-get-heading)))
7621 (org-agenda-change-all-lines newhead hdmarker)
7622 (beginning-of-line 1))))
7624 (defun org-agenda-do-date-later (arg)
7625 (interactive "P")
7626 (cond
7627 ((or (equal arg '(16))
7628 (memq last-command
7629 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7630 (setq this-command 'org-agenda-date-later-minutes)
7631 (org-agenda-date-later-minutes 1))
7632 ((or (equal arg '(4))
7633 (memq last-command
7634 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7635 (setq this-command 'org-agenda-date-later-hours)
7636 (org-agenda-date-later-hours 1))
7638 (org-agenda-date-later (prefix-numeric-value arg)))))
7640 (defun org-agenda-do-date-earlier (arg)
7641 (interactive "P")
7642 (cond
7643 ((or (equal arg '(16))
7644 (memq last-command
7645 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7646 (setq this-command 'org-agenda-date-earlier-minutes)
7647 (org-agenda-date-earlier-minutes 1))
7648 ((or (equal arg '(4))
7649 (memq last-command
7650 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7651 (setq this-command 'org-agenda-date-earlier-hours)
7652 (org-agenda-date-earlier-hours 1))
7654 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7656 (defun org-agenda-date-later (arg &optional what)
7657 "Change the date of this item to ARG day(s) later."
7658 (interactive "p")
7659 (org-agenda-check-type t 'agenda 'timeline)
7660 (org-agenda-check-no-diary)
7661 (let* ((marker (or (org-get-at-bol 'org-marker)
7662 (org-agenda-error)))
7663 (buffer (marker-buffer marker))
7664 (pos (marker-position marker))
7665 cdate today)
7666 (org-with-remote-undo buffer
7667 (with-current-buffer buffer
7668 (widen)
7669 (goto-char pos)
7670 (if (not (org-at-timestamp-p))
7671 (error "Cannot find time stamp"))
7672 (when (and org-agenda-move-date-from-past-immediately-to-today
7673 (equal arg 1)
7674 (or (not what) (eq what 'day))
7675 (not (save-match-data (org-at-date-range-p))))
7676 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
7677 cdate (calendar-absolute-from-gregorian
7678 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
7679 today (org-today))
7680 (if (> today cdate)
7681 ;; immediately shift to today
7682 (setq arg (- today cdate))))
7683 (org-timestamp-change arg (or what 'day))
7684 (when (and (org-at-date-range-p)
7685 (re-search-backward org-tr-regexp-both (point-at-bol)))
7686 (let ((end org-last-changed-timestamp))
7687 (org-timestamp-change arg (or what 'day))
7688 (setq org-last-changed-timestamp
7689 (concat org-last-changed-timestamp "--" end)))))
7690 (org-agenda-show-new-time marker org-last-changed-timestamp))
7691 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7693 (defun org-agenda-date-earlier (arg &optional what)
7694 "Change the date of this item to ARG day(s) earlier."
7695 (interactive "p")
7696 (org-agenda-date-later (- arg) what))
7698 (defun org-agenda-date-later-minutes (arg)
7699 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7700 (interactive "p")
7701 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7702 (org-agenda-date-later arg 'minute))
7704 (defun org-agenda-date-earlier-minutes (arg)
7705 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7706 (interactive "p")
7707 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7708 (org-agenda-date-earlier arg 'minute))
7710 (defun org-agenda-date-later-hours (arg)
7711 "Change the time of this item, in hour steps."
7712 (interactive "p")
7713 (org-agenda-date-later arg 'hour))
7715 (defun org-agenda-date-earlier-hours (arg)
7716 "Change the time of this item, in hour steps."
7717 (interactive "p")
7718 (org-agenda-date-earlier arg 'hour))
7720 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7721 "Show new date stamp via text properties."
7722 ;; We use text properties to make this undoable
7723 (let ((inhibit-read-only t)
7724 (buffer-invisibility-spec))
7725 (setq stamp (concat " " prefix " => " stamp))
7726 (save-excursion
7727 (goto-char (point-max))
7728 (while (not (bobp))
7729 (when (equal marker (org-get-at-bol 'org-marker))
7730 (org-move-to-column (- (window-width) (length stamp)) t)
7731 (org-agenda-fix-tags-filter-overlays-at (point))
7732 (if (featurep 'xemacs)
7733 ;; Use `duplicable' property to trigger undo recording
7734 (let ((ex (make-extent nil nil))
7735 (gl (make-glyph stamp)))
7736 (set-glyph-face gl 'secondary-selection)
7737 (set-extent-properties
7738 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7739 (insert-extent ex (1- (point)) (point-at-eol)))
7740 (add-text-properties
7741 (1- (point)) (point-at-eol)
7742 (list 'display (org-add-props stamp nil
7743 'face 'secondary-selection))))
7744 (beginning-of-line 1))
7745 (beginning-of-line 0)))))
7747 (defun org-agenda-date-prompt (arg)
7748 "Change the date of this item. Date is prompted for, with default today.
7749 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7750 be used to request time specification in the time stamp."
7751 (interactive "P")
7752 (org-agenda-check-type t 'agenda 'timeline)
7753 (org-agenda-check-no-diary)
7754 (let* ((marker (or (org-get-at-bol 'org-marker)
7755 (org-agenda-error)))
7756 (buffer (marker-buffer marker))
7757 (pos (marker-position marker)))
7758 (org-with-remote-undo buffer
7759 (with-current-buffer buffer
7760 (widen)
7761 (goto-char pos)
7762 (if (not (org-at-timestamp-p t))
7763 (error "Cannot find time stamp"))
7764 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7765 (org-agenda-show-new-time marker org-last-changed-timestamp))
7766 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7768 (defun org-agenda-schedule (arg &optional time)
7769 "Schedule the item at point.
7770 ARG is passed through to `org-schedule'."
7771 (interactive "P")
7772 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7773 (org-agenda-check-no-diary)
7774 (let* ((marker (or (org-get-at-bol 'org-marker)
7775 (org-agenda-error)))
7776 (type (marker-insertion-type marker))
7777 (buffer (marker-buffer marker))
7778 (pos (marker-position marker))
7779 (org-insert-labeled-timestamps-at-point nil)
7781 (set-marker-insertion-type marker t)
7782 (org-with-remote-undo buffer
7783 (with-current-buffer buffer
7784 (widen)
7785 (goto-char pos)
7786 (setq ts (org-schedule arg time)))
7787 (org-agenda-show-new-time marker ts "S"))
7788 (message "Item scheduled for %s" ts)))
7790 (defun org-agenda-deadline (arg &optional time)
7791 "Schedule the item at point.
7792 ARG is passed through to `org-deadline'."
7793 (interactive "P")
7794 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7795 (org-agenda-check-no-diary)
7796 (let* ((marker (or (org-get-at-bol 'org-marker)
7797 (org-agenda-error)))
7798 (buffer (marker-buffer marker))
7799 (pos (marker-position marker))
7800 (org-insert-labeled-timestamps-at-point nil)
7802 (org-with-remote-undo buffer
7803 (with-current-buffer buffer
7804 (widen)
7805 (goto-char pos)
7806 (setq ts (org-deadline arg time)))
7807 (org-agenda-show-new-time marker ts "D"))
7808 (message "Deadline for this item set to %s" ts)))
7810 (defun org-agenda-action ()
7811 "Select entry for agenda action, or execute an agenda action.
7812 This command prompts for another letter. Valid inputs are:
7814 m Mark the entry at point for an agenda action
7815 s Schedule the marked entry to the date at the cursor
7816 d Set the deadline of the marked entry to the date at the cursor
7817 r Call `org-remember' with cursor date as the default date
7818 c Call `org-capture' with cursor date as the default date
7819 SPC Show marked entry in other window
7820 TAB Visit marked entry in other window
7822 The cursor may be at a date in the calendar, or in the Org agenda."
7823 (interactive)
7824 (let (ans)
7825 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7826 (setq ans (read-char-exclusive))
7827 (cond
7828 ((equal ans ?m)
7829 ;; Mark this entry
7830 (if (eq major-mode 'org-agenda-mode)
7831 (let ((m (or (org-get-at-bol 'org-hd-marker)
7832 (org-get-at-bol 'org-marker))))
7833 (if m
7834 (progn
7835 (move-marker org-agenda-action-marker
7836 (marker-position m) (marker-buffer m))
7837 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7838 (error "Don't know which entry to mark")))
7839 (error "This command works only in the agenda")))
7840 ((equal ans ?s)
7841 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7842 ((equal ans ?d)
7843 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7844 ((equal ans ?r)
7845 (org-agenda-do-action '(org-remember) t))
7846 ((equal ans ?c)
7847 (org-agenda-do-action '(org-capture) t))
7848 ((equal ans ?\ )
7849 (let ((cw (selected-window)))
7850 (org-switch-to-buffer-other-window
7851 (marker-buffer org-agenda-action-marker))
7852 (goto-char org-agenda-action-marker)
7853 (org-show-context 'agenda)
7854 (select-window cw)))
7855 ((equal ans ?\C-i)
7856 (org-switch-to-buffer-other-window
7857 (marker-buffer org-agenda-action-marker))
7858 (goto-char org-agenda-action-marker)
7859 (org-show-context 'agenda))
7860 (t (error "Invalid agenda action %c" ans)))))
7862 (defun org-agenda-do-action (form &optional current-buffer)
7863 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7864 (let ((org-overriding-default-time (org-get-cursor-date)))
7865 (if current-buffer
7866 (eval form)
7867 (if (not (marker-buffer org-agenda-action-marker))
7868 (error "No entry has been selected for agenda action")
7869 (with-current-buffer (marker-buffer org-agenda-action-marker)
7870 (save-excursion
7871 (save-restriction
7872 (widen)
7873 (goto-char org-agenda-action-marker)
7874 (eval form))))))))
7876 (defun org-agenda-clock-in (&optional arg)
7877 "Start the clock on the currently selected item."
7878 (interactive "P")
7879 (org-agenda-check-no-diary)
7880 (if (equal arg '(4))
7881 (org-clock-in arg)
7882 (let* ((marker (or (org-get-at-bol 'org-marker)
7883 (org-agenda-error)))
7884 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7885 marker))
7886 (pos (marker-position marker))
7887 newhead)
7888 (org-with-remote-undo (marker-buffer marker)
7889 (with-current-buffer (marker-buffer marker)
7890 (widen)
7891 (goto-char pos)
7892 (org-show-context 'agenda)
7893 (org-show-entry)
7894 (org-cycle-hide-drawers 'children)
7895 (org-clock-in arg)
7896 (setq newhead (org-get-heading)))
7897 (org-agenda-change-all-lines newhead hdmarker)))))
7899 (defun org-agenda-clock-out ()
7900 "Stop the currently running clock."
7901 (interactive)
7902 (unless (marker-buffer org-clock-marker)
7903 (error "No running clock"))
7904 (let ((marker (make-marker)) newhead)
7905 (org-with-remote-undo (marker-buffer org-clock-marker)
7906 (with-current-buffer (marker-buffer org-clock-marker)
7907 (save-excursion
7908 (save-restriction
7909 (widen)
7910 (goto-char org-clock-marker)
7911 (org-back-to-heading t)
7912 (move-marker marker (point))
7913 (org-clock-out)
7914 (setq newhead (org-get-heading))))))
7915 (org-agenda-change-all-lines newhead marker)
7916 (move-marker marker nil)))
7918 (defun org-agenda-clock-cancel (&optional arg)
7919 "Cancel the currently running clock."
7920 (interactive "P")
7921 (unless (marker-buffer org-clock-marker)
7922 (error "No running clock"))
7923 (org-with-remote-undo (marker-buffer org-clock-marker)
7924 (org-clock-cancel)))
7926 (defun org-agenda-clock-goto ()
7927 "Jump to the currently clocked in task within the agenda.
7928 If the currently clocked in task is not listed in the agenda
7929 buffer, display it in another window."
7930 (interactive)
7931 (let (pos)
7932 (mapc (lambda (o)
7933 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7934 (setq pos (overlay-start o))))
7935 (overlays-in (point-min) (point-max)))
7936 (cond (pos (goto-char pos))
7937 ;; If the currently clocked entry is not in the agenda
7938 ;; buffer, we visit it in another window:
7939 (org-clock-current-task
7940 (org-switch-to-buffer-other-window (org-clock-goto)))
7941 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7943 (defun org-agenda-diary-entry-in-org-file ()
7944 "Make a diary entry in the file `org-agenda-diary-file'."
7945 (let (d1 d2 char (text "") dp1 dp2)
7946 (if (equal (buffer-name) "*Calendar*")
7947 (setq d1 (calendar-cursor-to-date t)
7948 d2 (car calendar-mark-ring))
7949 (setq dp1 (get-text-property (point-at-bol) 'day))
7950 (unless dp1 (error "No date defined in current line"))
7951 (setq d1 (calendar-gregorian-from-absolute dp1)
7952 d2 (and (ignore-errors (mark))
7953 (save-excursion
7954 (goto-char (mark))
7955 (setq dp2 (get-text-property (point-at-bol) 'day)))
7956 (calendar-gregorian-from-absolute dp2))))
7957 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7958 (setq char (read-char-exclusive))
7959 (cond
7960 ((equal char ?d)
7961 (setq text (read-string "Day entry: "))
7962 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7963 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7964 ((equal char ?a)
7965 (setq d1 (list (car d1) (nth 1 d1)
7966 (read-number (format "Reference year [%d]: " (nth 2 d1))
7967 (nth 2 d1))))
7968 (setq text (read-string "Anniversary (use %d to show years): "))
7969 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7970 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7971 ((equal char ?b)
7972 (setq text (read-string "Block entry: "))
7973 (unless (and d1 d2 (not (equal d1 d2)))
7974 (error "No block of days selected"))
7975 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7976 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7977 ((equal char ?j)
7978 (org-switch-to-buffer-other-window
7979 (find-file-noselect org-agenda-diary-file))
7980 (require 'org-datetree)
7981 (org-datetree-find-date-create d1)
7982 (org-reveal t))
7983 (t (error "Invalid selection character `%c'" char)))))
7985 (defcustom org-agenda-insert-diary-strategy 'date-tree
7986 "Where in `org-agenda-diary-file' should new entries be added?
7987 Valid values:
7989 date-tree in the date tree, as child of the date
7990 top-level as top-level entries at the end of the file."
7991 :group 'org-agenda
7992 :type '(choice
7993 (const :tag "in a date tree" date-tree)
7994 (const :tag "as top level at end of file" top-level)))
7996 (defcustom org-agenda-insert-diary-extract-time nil
7997 "Non-nil means extract any time specification from the diary entry."
7998 :group 'org-agenda
7999 :type 'boolean)
8001 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8002 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8003 If TEXT is not empty, it will become the headline of the new entry, and
8004 the resulting entry will not be shown. When TEXT is empty, switch to
8005 `org-agenda-diary-file' and let the user finish the entry there."
8006 (let ((cw (current-window-configuration)))
8007 (org-switch-to-buffer-other-window
8008 (find-file-noselect org-agenda-diary-file))
8009 (widen)
8010 (goto-char (point-min))
8011 (cond
8012 ((eq type 'anniversary)
8013 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8014 (progn
8015 (or (org-at-heading-p t)
8016 (progn
8017 (outline-next-heading)
8018 (insert "* Anniversaries\n\n")
8019 (beginning-of-line -1)))))
8020 (outline-next-heading)
8021 (org-back-over-empty-lines)
8022 (backward-char 1)
8023 (insert "\n")
8024 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8025 (nth 2 d1) (car d1) (nth 1 d1) text)))
8026 ((eq type 'day)
8027 (let ((org-prefix-has-time t)
8028 (org-agenda-time-leading-zero t)
8029 fmt time time2)
8030 (if org-agenda-insert-diary-extract-time
8031 ;; Use org-agenda-format-item to parse text for a time-range and
8032 ;; remove it. FIXME: This is a hack, we should refactor
8033 ;; that function to make time extraction available separately
8034 (setq fmt (org-agenda-format-item nil text nil nil t)
8035 time (get-text-property 0 'time fmt)
8036 time2 (if (> (length time) 0)
8037 ;; split-string removes trailing ...... if
8038 ;; no end time given. First space
8039 ;; separates time from date.
8040 (concat " " (car (split-string time "\\.")))
8041 nil)
8042 text (get-text-property 0 'txt fmt)))
8043 (if (eq org-agenda-insert-diary-strategy 'top-level)
8044 (org-agenda-insert-diary-as-top-level text)
8045 (require 'org-datetree)
8046 (org-datetree-find-date-create d1)
8047 (org-agenda-insert-diary-make-new-entry text))
8048 (org-insert-time-stamp (org-time-from-absolute
8049 (calendar-absolute-from-gregorian d1))
8050 nil nil nil nil time2))
8051 (end-of-line 0))
8052 ((eq type 'block)
8053 (if (> (calendar-absolute-from-gregorian d1)
8054 (calendar-absolute-from-gregorian d2))
8055 (setq d1 (prog1 d2 (setq d2 d1))))
8056 (if (eq org-agenda-insert-diary-strategy 'top-level)
8057 (org-agenda-insert-diary-as-top-level text)
8058 (require 'org-datetree)
8059 (org-datetree-find-date-create d1)
8060 (org-agenda-insert-diary-make-new-entry text))
8061 (org-insert-time-stamp (org-time-from-absolute
8062 (calendar-absolute-from-gregorian d1)))
8063 (insert "--")
8064 (org-insert-time-stamp (org-time-from-absolute
8065 (calendar-absolute-from-gregorian d2)))
8066 (end-of-line 0)))
8067 (if (string-match "\\S-" text)
8068 (progn
8069 (set-window-configuration cw)
8070 (message "%s entry added to %s"
8071 (capitalize (symbol-name type))
8072 (abbreviate-file-name org-agenda-diary-file)))
8073 (org-reveal t)
8074 (message "Please finish entry here"))))
8076 (defun org-agenda-insert-diary-as-top-level (text)
8077 "Make new entry as a top-level entry at the end of the file.
8078 Add TEXT as headline, and position the cursor in the second line so that
8079 a timestamp can be added there."
8080 (widen)
8081 (goto-char (point-max))
8082 (or (bolp) (insert "\n"))
8083 (insert "* " text "\n")
8084 (if org-adapt-indentation (org-indent-to-column 2)))
8086 (defun org-agenda-insert-diary-make-new-entry (text)
8087 "Make new entry as last child of current entry.
8088 Add TEXT as headline, and position the cursor in the second line so that
8089 a timestamp can be added there."
8090 (let ((org-show-following-heading t)
8091 (org-show-siblings t)
8092 (org-show-hierarchy-above t)
8093 (org-show-entry-below t)
8094 col)
8095 (outline-next-heading)
8096 (org-back-over-empty-lines)
8097 (or (looking-at "[ \t]*$")
8098 (progn (insert "\n") (backward-char 1)))
8099 (org-insert-heading nil t)
8100 (org-do-demote)
8101 (setq col (current-column))
8102 (insert text "\n")
8103 (if org-adapt-indentation (org-indent-to-column col))
8104 (let ((org-show-following-heading t)
8105 (org-show-siblings t)
8106 (org-show-hierarchy-above t)
8107 (org-show-entry-below t))
8108 (org-show-context))))
8110 (defun org-agenda-diary-entry ()
8111 "Make a diary entry, like the `i' command from the calendar.
8112 All the standard commands work: block, weekly etc.
8113 When `org-agenda-diary-file' points to a file,
8114 `org-agenda-diary-entry-in-org-file' is called instead to create
8115 entries in that Org-mode file."
8116 (interactive)
8117 (org-agenda-check-type t 'agenda 'timeline)
8118 (if (not (eq org-agenda-diary-file 'diary-file))
8119 (org-agenda-diary-entry-in-org-file)
8120 (require 'diary-lib)
8121 (let* ((char (progn
8122 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8123 (read-char-exclusive)))
8124 (cmd (cdr (assoc char
8125 '((?d . insert-diary-entry)
8126 (?w . insert-weekly-diary-entry)
8127 (?m . insert-monthly-diary-entry)
8128 (?y . insert-yearly-diary-entry)
8129 (?a . insert-anniversary-diary-entry)
8130 (?b . insert-block-diary-entry)
8131 (?c . insert-cyclic-diary-entry)))))
8132 (oldf (symbol-function 'calendar-cursor-to-date))
8133 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8134 (point (point))
8135 (mark (or (mark t) (point))))
8136 (unless cmd
8137 (error "No command associated with <%c>" char))
8138 (unless (and (get-text-property point 'day)
8139 (or (not (equal ?b char))
8140 (get-text-property mark 'day)))
8141 (error "Don't know which date to use for diary entry"))
8142 ;; We implement this by hacking the `calendar-cursor-to-date' function
8143 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8144 (let ((calendar-mark-ring
8145 (list (calendar-gregorian-from-absolute
8146 (or (get-text-property mark 'day)
8147 (get-text-property point 'day))))))
8148 (unwind-protect
8149 (progn
8150 (fset 'calendar-cursor-to-date
8151 (lambda (&optional error dummy)
8152 (calendar-gregorian-from-absolute
8153 (get-text-property point 'day))))
8154 (call-interactively cmd))
8155 (fset 'calendar-cursor-to-date oldf))))))
8157 (defun org-agenda-execute-calendar-command (cmd)
8158 "Execute a calendar command from the agenda, with the date associated to
8159 the cursor position."
8160 (org-agenda-check-type t 'agenda 'timeline)
8161 (require 'diary-lib)
8162 (unless (get-text-property (point) 'day)
8163 (error "Don't know which date to use for calendar command"))
8164 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8165 (point (point))
8166 (date (calendar-gregorian-from-absolute
8167 (get-text-property point 'day)))
8168 ;; the following 2 vars are needed in the calendar
8169 (displayed-month (car date))
8170 (displayed-year (nth 2 date)))
8171 (unwind-protect
8172 (progn
8173 (fset 'calendar-cursor-to-date
8174 (lambda (&optional error dummy)
8175 (calendar-gregorian-from-absolute
8176 (get-text-property point 'day))))
8177 (call-interactively cmd))
8178 (fset 'calendar-cursor-to-date oldf))))
8180 (defun org-agenda-phases-of-moon ()
8181 "Display the phases of the moon for the 3 months around the cursor date."
8182 (interactive)
8183 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8185 (defun org-agenda-holidays ()
8186 "Display the holidays for the 3 months around the cursor date."
8187 (interactive)
8188 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8190 (defvar calendar-longitude)
8191 (defvar calendar-latitude)
8192 (defvar calendar-location-name)
8194 (defun org-agenda-sunrise-sunset (arg)
8195 "Display sunrise and sunset for the cursor date.
8196 Latitude and longitude can be specified with the variables
8197 `calendar-latitude' and `calendar-longitude'. When called with prefix
8198 argument, latitude and longitude will be prompted for."
8199 (interactive "P")
8200 (require 'solar)
8201 (let ((calendar-longitude (if arg nil calendar-longitude))
8202 (calendar-latitude (if arg nil calendar-latitude))
8203 (calendar-location-name
8204 (if arg "the given coordinates" calendar-location-name)))
8205 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8207 (defun org-agenda-goto-calendar ()
8208 "Open the Emacs calendar with the date at the cursor."
8209 (interactive)
8210 (org-agenda-check-type t 'agenda 'timeline)
8211 (let* ((day (or (get-text-property (point) 'day)
8212 (error "Don't know which date to open in calendar")))
8213 (date (calendar-gregorian-from-absolute day))
8214 (calendar-move-hook nil)
8215 (calendar-view-holidays-initially-flag nil)
8216 (calendar-view-diary-initially-flag nil))
8217 (calendar)
8218 (calendar-goto-date date)))
8220 ;;;###autoload
8221 (defun org-calendar-goto-agenda ()
8222 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8223 This is a command that has to be installed in `calendar-mode-map'."
8224 (interactive)
8225 (org-agenda-list nil (calendar-absolute-from-gregorian
8226 (calendar-cursor-to-date))
8227 nil))
8229 (defun org-agenda-convert-date ()
8230 (interactive)
8231 (org-agenda-check-type t 'agenda 'timeline)
8232 (let ((day (get-text-property (point) 'day))
8233 date s)
8234 (unless day
8235 (error "Don't know which date to convert"))
8236 (setq date (calendar-gregorian-from-absolute day))
8237 (setq s (concat
8238 "Gregorian: " (calendar-date-string date) "\n"
8239 "ISO: " (calendar-iso-date-string date) "\n"
8240 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8241 "Julian: " (calendar-julian-date-string date) "\n"
8242 "Astron. JD: " (calendar-astro-date-string date)
8243 " (Julian date number at noon UTC)\n"
8244 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8245 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8246 "French: " (calendar-french-date-string date) "\n"
8247 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8248 "Mayan: " (calendar-mayan-date-string date) "\n"
8249 "Coptic: " (calendar-coptic-date-string date) "\n"
8250 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8251 "Persian: " (calendar-persian-date-string date) "\n"
8252 "Chinese: " (calendar-chinese-date-string date) "\n"))
8253 (with-output-to-temp-buffer "*Dates*"
8254 (princ s))
8255 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8257 ;;; Bulk commands
8259 (defvar org-agenda-bulk-marked-entries nil
8260 "List of markers that refer to marked entries in the agenda.")
8262 (defun org-agenda-bulk-marked-p ()
8263 (eq (get-char-property (point-at-bol) 'type)
8264 'org-marked-entry-overlay))
8266 (defun org-agenda-bulk-mark (&optional arg)
8267 "Mark the entry at point for future bulk action."
8268 (interactive "p")
8269 (dotimes (i (max arg 1))
8270 (unless (org-get-at-bol 'org-agenda-diary-link)
8271 (let* ((m (org-get-at-bol 'org-hd-marker))
8273 (unless (org-agenda-bulk-marked-p)
8274 (unless m (error "Nothing to mark at point"))
8275 (push m org-agenda-bulk-marked-entries)
8276 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8277 (org-overlay-display ov "> "
8278 (org-get-todo-face "TODO")
8279 'evaporate)
8280 (overlay-put ov 'type 'org-marked-entry-overlay))
8281 (beginning-of-line 2)
8282 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8283 (beginning-of-line 2))
8284 (message "%d entries marked for bulk action"
8285 (length org-agenda-bulk-marked-entries))))))
8287 (defun org-agenda-bulk-mark-regexp (regexp)
8288 "Mark entries match REGEXP."
8289 (interactive "sMark entries matching regexp: ")
8290 (let (entries-marked)
8291 (save-excursion
8292 (goto-char (point-min))
8293 (goto-char (next-single-property-change (point) 'txt))
8294 (while (re-search-forward regexp nil t)
8295 (when (string-match regexp (get-text-property (point) 'txt))
8296 (setq entries-marked (+ entries-marked 1))
8297 (call-interactively 'org-agenda-bulk-mark))))
8298 (if (not entries-marked)
8299 (message "No entry matching this regexp."))))
8301 (defun org-agenda-bulk-unmark ()
8302 "Unmark the entry at point for future bulk action."
8303 (interactive)
8304 (when (org-agenda-bulk-marked-p)
8305 (org-agenda-bulk-remove-overlays
8306 (point-at-bol) (+ 2 (point-at-bol)))
8307 (setq org-agenda-bulk-marked-entries
8308 (delete (org-get-at-bol 'org-hd-marker)
8309 org-agenda-bulk-marked-entries)))
8310 (beginning-of-line 2)
8311 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8312 (beginning-of-line 2))
8313 (message "%d entries marked for bulk action"
8314 (length org-agenda-bulk-marked-entries)))
8316 (defun org-agenda-bulk-toggle ()
8317 "Toggle marking the entry at point for bulk action."
8318 (interactive)
8319 (if (org-agenda-bulk-marked-p)
8320 (org-agenda-bulk-unmark)
8321 (org-agenda-bulk-mark)))
8323 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8324 "Remove the mark overlays between BEG and END in the agenda buffer.
8325 BEG and END default to the buffer limits.
8327 This only removes the overlays, it does not remove the markers
8328 from the list in `org-agenda-bulk-marked-entries'."
8329 (interactive)
8330 (mapc (lambda (ov)
8331 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8332 (delete-overlay ov)))
8333 (overlays-in (or beg (point-min)) (or end (point-max)))))
8335 (defun org-agenda-bulk-remove-all-marks ()
8336 "Remove all marks in the agenda buffer.
8337 This will remove the markers, and the overlays."
8338 (interactive)
8339 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8340 (setq org-agenda-bulk-marked-entries nil)
8341 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8343 (defun org-agenda-bulk-action (&optional arg)
8344 "Execute an remote-editing action on all marked entries.
8345 The prefix arg is passed through to the command if possible."
8346 (interactive "P")
8347 ;; Make sure we have markers, and only valid ones
8348 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8349 (mapc
8350 (lambda (m)
8351 (unless (and (markerp m)
8352 (marker-buffer m)
8353 (buffer-live-p (marker-buffer m))
8354 (marker-position m))
8355 (error "Marker %s for bulk command is invalid" m)))
8356 org-agenda-bulk-marked-entries)
8358 ;; Prompt for the bulk command
8359 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8360 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8361 (when org-agenda-bulk-custom-functions
8362 (concat " Custom: ["
8363 (mapconcat (lambda(f) (char-to-string (car f)))
8364 org-agenda-bulk-custom-functions "")
8365 "]"))))
8366 (let* ((action (read-char-exclusive))
8367 (org-log-refile (if org-log-refile 'time nil))
8368 (entries (reverse org-agenda-bulk-marked-entries))
8369 redo-at-end
8370 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8371 (cond
8372 ((equal action ?$)
8373 (setq cmd '(org-agenda-archive)))
8375 ((equal action ?A)
8376 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8378 ((member action '(?r ?w))
8379 (setq rfloc (org-refile-get-location
8380 "Refile to"
8381 (marker-buffer (car org-agenda-bulk-marked-entries))
8382 org-refile-allow-creating-parent-nodes))
8383 (if (nth 3 rfloc)
8384 (setcar (nthcdr 3 rfloc)
8385 (move-marker (make-marker) (nth 3 rfloc)
8386 (or (get-file-buffer (nth 1 rfloc))
8387 (find-buffer-visiting (nth 1 rfloc))
8388 (error "This should not happen")))))
8390 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8391 redo-at-end t))
8393 ((equal action ?t)
8394 (setq state (org-icompleting-read
8395 "Todo state: "
8396 (with-current-buffer (marker-buffer (car entries))
8397 (mapcar 'list org-todo-keywords-1))))
8398 (setq cmd `(let ((org-inhibit-blocking t)
8399 (org-inhibit-logging 'note))
8400 (org-agenda-todo ,state))))
8402 ((memq action '(?- ?+))
8403 (setq tag (org-icompleting-read
8404 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8405 (with-current-buffer (marker-buffer (car entries))
8406 (delq nil
8407 (mapcar (lambda (x)
8408 (if (stringp (car x)) x)) org-tag-alist)))))
8409 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8411 ((memq action '(?s ?d))
8412 (let* ((date (unless arg
8413 (org-read-date
8414 nil nil nil
8415 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8416 (ans (if arg nil org-read-date-final-answer))
8417 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8418 (setq cmd `(let* ((bound (fboundp 'read-string))
8419 (old (and bound (symbol-function 'read-string))))
8420 (unwind-protect
8421 (progn
8422 (fset 'read-string (lambda (&rest ignore) ,ans))
8423 (eval '(,c1 arg)))
8424 (if bound
8425 (fset 'read-string old)
8426 (fmakunbound 'read-string)))))))
8428 ((equal action ?S)
8429 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8430 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8431 (let ((days (read-number
8432 (format "Scatter tasks across how many %sdays: "
8433 (if arg "week" "")) 7)))
8434 (setq cmd
8435 `(let ((distance (1+ (random ,days))))
8436 (if arg
8437 (let ((dist distance)
8438 (day-of-week
8439 (calendar-day-of-week
8440 (calendar-gregorian-from-absolute (org-today)))))
8441 (dotimes (i (1+ dist))
8442 (while (member day-of-week org-agenda-weekend-days)
8443 (incf distance)
8444 (incf day-of-week)
8445 (if (= day-of-week 7)
8446 (setq day-of-week 0)))
8447 (incf day-of-week)
8448 (if (= day-of-week 7)
8449 (setq day-of-week 0)))))
8450 ;; silently fail when try to replan a sexp entry
8451 (condition-case nil
8452 (let* ((date (calendar-gregorian-from-absolute
8453 (+ (org-today) distance)))
8454 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8455 (nth 2 date))))
8456 (org-agenda-schedule nil time))
8457 (error nil)))))))
8459 ((assoc action org-agenda-bulk-custom-functions)
8460 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8461 redo-at-end t))
8463 ((equal action ?f)
8464 (setq cmd (list (intern
8465 (org-icompleting-read "Function: "
8466 obarray 'fboundp t nil nil)))))
8468 (t (error "Invalid bulk action")))
8470 ;; Sort the markers, to make sure that parents are handled before children
8471 (setq entries (sort entries
8472 (lambda (a b)
8473 (cond
8474 ((equal (marker-buffer a) (marker-buffer b))
8475 (< (marker-position a) (marker-position b)))
8477 (string< (buffer-name (marker-buffer a))
8478 (buffer-name (marker-buffer b))))))))
8480 ;; Now loop over all markers and apply cmd
8481 (while (setq e (pop entries))
8482 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8483 (if (not pos)
8484 (progn (message "Skipping removed entry at %s" e)
8485 (setq cntskip (1+ cntskip)))
8486 (goto-char pos)
8487 (let (org-loop-over-headlines-in-active-region)
8488 (eval cmd))
8489 (setq org-agenda-bulk-marked-entries
8490 (delete e org-agenda-bulk-marked-entries))
8491 (setq cnt (1+ cnt))))
8492 (setq org-agenda-bulk-marked-entries nil)
8493 (org-agenda-bulk-remove-all-marks)
8494 (when redo-at-end (org-agenda-redo))
8495 (message "Acted on %d entries%s"
8497 (if (= cntskip 0)
8499 (format ", skipped %d (disappeared before their turn)"
8500 cntskip)))))
8502 ;;; Flagging notes
8504 (defun org-agenda-show-the-flagging-note ()
8505 "Display the flagging note in the other window.
8506 When called a second time in direct sequence, offer to remove the FLAGGING
8507 tag and (if present) the flagging note."
8508 (interactive)
8509 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8510 (win (selected-window))
8511 note heading newhead)
8512 (unless hdmarker
8513 (error "No linked entry at point"))
8514 (if (and (eq this-command last-command)
8515 (y-or-n-p "Unflag and remove any flagging note? "))
8516 (progn
8517 (org-agenda-remove-flag hdmarker)
8518 (let ((win (get-buffer-window "*Flagging Note*")))
8519 (and win (delete-window win)))
8520 (message "Entry unflagged"))
8521 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8522 (unless note
8523 (error "No flagging note"))
8524 (org-kill-new note)
8525 (org-switch-to-buffer-other-window "*Flagging Note*")
8526 (erase-buffer)
8527 (insert note)
8528 (goto-char (point-min))
8529 (while (re-search-forward "\\\\n" nil t)
8530 (replace-match "\n" t t))
8531 (goto-char (point-min))
8532 (select-window win)
8533 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8535 (defun org-agenda-remove-flag (marker)
8536 "Remove the FLAGGED tag and any flagging note in the entry."
8537 (let (newhead)
8538 (org-with-point-at marker
8539 (org-toggle-tag "FLAGGED" 'off)
8540 (org-entry-delete nil "THEFLAGGINGNOTE")
8541 (setq newhead (org-get-heading)))
8542 (org-agenda-change-all-lines newhead marker)
8543 (message "Entry unflagged")))
8545 (defun org-agenda-get-any-marker (&optional pos)
8546 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8547 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8549 ;;; Appointment reminders
8551 (defvar appt-time-msg-list)
8553 ;;;###autoload
8554 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8555 "Activate appointments found in `org-agenda-files'.
8556 With a \\[universal-argument] prefix, refresh the list of
8557 appointments.
8559 If FILTER is t, interactively prompt the user for a regular
8560 expression, and filter out entries that don't match it.
8562 If FILTER is a string, use this string as a regular expression
8563 for filtering entries out.
8565 If FILTER is a function, filter out entries against which
8566 calling the function returns nil. This function takes one
8567 argument: an entry from `org-agenda-get-day-entries'.
8569 FILTER can also be an alist with the car of each cell being
8570 either 'headline or 'category. For example:
8572 '((headline \"IMPORTANT\")
8573 (category \"Work\"))
8575 will only add headlines containing IMPORTANT or headlines
8576 belonging to the \"Work\" category.
8578 ARGS are symbols indicating what kind of entries to consider.
8579 By default `org-agenda-to-appt' will use :deadline, :scheduled
8580 and :timestamp entries. See the docstring of `org-diary' for
8581 details and examples."
8582 (interactive "P")
8583 (if refresh (setq appt-time-msg-list nil))
8584 (if (eq filter t)
8585 (setq filter (read-from-minibuffer "Regexp filter: ")))
8586 (let* ((cnt 0) ; count added events
8587 (scope (or args '(:deadline :scheduled :timestamp)))
8588 (org-agenda-new-buffers nil)
8589 (org-deadline-warning-days 0)
8590 ;; Do not use `org-today' here because appt only takes
8591 ;; time and without date as argument, so it may pass wrong
8592 ;; information otherwise
8593 (today (org-date-to-gregorian
8594 (time-to-days (current-time))))
8595 (org-agenda-restrict nil)
8596 (files (org-agenda-files 'unrestricted)) entries file)
8597 ;; Get all entries which may contain an appt
8598 (org-prepare-agenda-buffers files)
8599 (while (setq file (pop files))
8600 (setq entries
8601 (delq nil
8602 (append entries
8603 (apply 'org-agenda-get-day-entries
8604 file today scope)))))
8605 ;; Map thru entries and find if we should filter them out
8606 (mapc
8607 (lambda(x)
8608 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8609 (cat (get-text-property 1 'org-category x))
8610 (tod (get-text-property 1 'time-of-day x))
8611 (ok (or (null filter)
8612 (and (stringp filter) (string-match filter evt))
8613 (and (functionp filter) (funcall filter x))
8614 (and (listp filter)
8615 (let ((cat-filter (cadr (assoc 'category filter)))
8616 (evt-filter (cadr (assoc 'headline filter))))
8617 (or (and (stringp cat-filter)
8618 (string-match cat-filter cat))
8619 (and (stringp evt-filter)
8620 (string-match evt-filter evt))))))))
8621 ;; FIXME: Shall we remove text-properties for the appt text?
8622 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8623 (when (and ok tod)
8624 (setq tod (concat "00" (number-to-string tod))
8625 tod (when (string-match
8626 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8627 (concat (match-string 1 tod) ":"
8628 (match-string 2 tod))))
8629 (appt-add tod evt)
8630 (setq cnt (1+ cnt))))) entries)
8631 (org-release-buffers org-agenda-new-buffers)
8632 (if (eq cnt 0)
8633 (message "No event to add")
8634 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8636 (defun org-agenda-todayp (date)
8637 "Does DATE mean today, when considering `org-extend-today-until'?"
8638 (let ((today (org-today))
8639 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8640 date)))
8641 (eq date today)))
8643 (defun org-agenda-todo-yesterday (&optional arg)
8644 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8645 (interactive "P")
8646 (let* ((hour (third (decode-time
8647 (org-current-time))))
8648 (org-extend-today-until (1+ hour)))
8649 (org-agenda-todo arg)))
8651 (provide 'org-agenda)
8653 ;;; org-agenda.el ends here