Merge branch 'master' of orgmode.org:org-mode
[org-mode.git] / lisp / org-agenda.el
blob66686f92052e1a03124833286c1132e9ba79e425
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 conveniant 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 org-datetree-find-date-create "org-datetree"
70 (date &optional keep-restriction))
71 (declare-function org-columns-quit "org-colview" ())
72 (declare-function diary-date-display-form "diary-lib" (&optional type))
73 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
74 (declare-function org-habit-insert-consistency-graphs
75 "org-habit" (&optional line))
76 (declare-function org-is-habit-p "org-habit" (&optional pom))
77 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
78 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
79 (declare-function org-pop-to-buffer-same-window "org-compat"
80 (&optional buffer-or-name norecord label))
82 (defvar calendar-mode-map)
83 (defvar org-clock-current-task) ; defined in org-clock.el
84 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
85 (defvar org-habit-show-habits)
86 (defvar org-habit-show-habits-only-for-today)
88 ;; Defined somewhere in this file, but used before definition.
89 (defvar org-agenda-buffer-name)
90 (defvar org-agenda-overriding-header)
91 (defvar org-agenda-title-append nil)
92 (defvar entry)
93 (defvar date)
94 (defvar org-agenda-undo-list)
95 (defvar org-agenda-pending-undo-list)
96 (defvar original-date) ; dynamically scoped, calendar.el does scope this
98 (defcustom org-agenda-confirm-kill 1
99 "When set, remote killing from the agenda buffer needs confirmation.
100 When t, a confirmation is always needed. When a number N, confirmation is
101 only needed when the text to be killed contains more than N non-white lines."
102 :group 'org-agenda
103 :type '(choice
104 (const :tag "Never" nil)
105 (const :tag "Always" t)
106 (integer :tag "When more than N lines")))
108 (defcustom org-agenda-compact-blocks nil
109 "Non-nil means make the block agenda more compact.
110 This is done globally by leaving out lines like the agenda span
111 name and week number or the separator lines."
112 :group 'org-agenda
113 :type 'boolean)
115 (defcustom org-agenda-block-separator ?=
116 "The separator between blocks in the agenda.
117 If this is a string, it will be used as the separator, with a newline added.
118 If it is a character, it will be repeated to fill the window width.
119 If nil the separator is disabled. In `org-agenda-custom-commands' this
120 addresses the separator between the current and the previous block."
121 :group 'org-agenda
122 :type '(choice
123 (const :tag "Disabled" nil)
124 (character)
125 (string)))
127 (defgroup org-agenda-export nil
128 "Options concerning exporting agenda views in Org-mode."
129 :tag "Org Agenda Export"
130 :group 'org-agenda)
132 (defcustom org-agenda-with-colors t
133 "Non-nil means use colors in agenda views."
134 :group 'org-agenda-export
135 :type 'boolean)
137 (defcustom org-agenda-exporter-settings nil
138 "Alist of variable/value pairs that should be active during agenda export.
139 This is a good place to set options for ps-print and for htmlize.
140 Note that the way this is implemented, the values will be evaluated
141 before assigned to the variables. So make sure to quote values you do
142 *not* want evaluated, for example
144 (setq org-agenda-exporter-settings
145 '((ps-print-color-p 'black-white)))"
146 :group 'org-agenda-export
147 :type '(repeat
148 (list
149 (variable)
150 (sexp :tag "Value"))))
152 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
153 "Hook run in temporary buffer before writing it to an export file.
154 A useful function is `org-agenda-add-entry-text'."
155 :group 'org-agenda-export
156 :type 'hook
157 :options '(org-agenda-add-entry-text))
159 (defcustom org-agenda-add-entry-text-maxlines 0
160 "Maximum number of entry text lines to be added to agenda.
161 This is only relevant when `org-agenda-add-entry-text' is part of
162 `org-agenda-before-write-hook', which it is by default.
163 When this is 0, nothing will happen. When it is greater than 0, it
164 specifies the maximum number of lines that will be added for each entry
165 that is listed in the agenda view.
167 Note that this variable is not used during display, only when exporting
168 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
169 and `org-agenda-entry-text-maxlines'."
170 :group 'org-agenda
171 :type 'integer)
173 (defcustom org-agenda-add-entry-text-descriptive-links t
174 "Non-nil means export org-links as descriptive links in agenda added text.
175 This variable applies to the text added to the agenda when
176 `org-agenda-add-entry-text-maxlines' is larger than 0.
177 When this variable nil, the URL will (also) be shown."
178 :group 'org-agenda
179 :type 'boolean)
181 (defcustom org-agenda-export-html-style ""
182 "The style specification for exported HTML Agenda files.
183 If this variable contains a string, it will replace the default <style>
184 section as produced by `htmlize'.
185 Since there are different ways of setting style information, this variable
186 needs to contain the full HTML structure to provide a style, including the
187 surrounding HTML tags. The style specifications should include definitions
188 the fonts used by the agenda, here is an example:
190 <style type=\"text/css\">
191 p { font-weight: normal; color: gray; }
192 .org-agenda-structure {
193 font-size: 110%;
194 color: #003399;
195 font-weight: 600;
197 .org-todo {
198 color: #cc6666;
199 font-weight: bold;
201 .org-agenda-done {
202 color: #339933;
204 .org-done {
205 color: #339933;
207 .title { text-align: center; }
208 .todo, .deadline { color: red; }
209 .done { color: green; }
210 </style>
212 or, if you want to keep the style in a file,
214 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
216 As the value of this option simply gets inserted into the HTML <head> header,
217 you can \"misuse\" it to also add other text to the header. However,
218 <style>...</style> is required, if not present the variable will be ignored."
219 :group 'org-agenda-export
220 :group 'org-export-html
221 :type 'string)
223 (defcustom org-agenda-persistent-filter nil
224 "When set, keep filters from one agenda view to the next."
225 :group 'org-agenda
226 :type 'boolean)
228 (defgroup org-agenda-custom-commands nil
229 "Options concerning agenda views in Org-mode."
230 :tag "Org Agenda Custom Commands"
231 :group 'org-agenda)
233 (defconst org-sorting-choice
234 '(choice
235 (const time-up) (const time-down)
236 (const category-keep) (const category-up) (const category-down)
237 (const tag-down) (const tag-up)
238 (const priority-up) (const priority-down)
239 (const todo-state-up) (const todo-state-down)
240 (const effort-up) (const effort-down)
241 (const habit-up) (const habit-down)
242 (const alpha-up) (const alpha-down)
243 (const user-defined-up) (const user-defined-down))
244 "Sorting choices.")
246 (defconst org-agenda-custom-commands-local-options
247 `(repeat :tag "Local settings for this command. Remember to quote values"
248 (choice :tag "Setting"
249 (list :tag "Heading for this block"
250 (const org-agenda-overriding-header)
251 (string :tag "Headline"))
252 (list :tag "Files to be searched"
253 (const org-agenda-files)
254 (list
255 (const :format "" quote)
256 (repeat (file))))
257 (list :tag "Sorting strategy"
258 (const org-agenda-sorting-strategy)
259 (list
260 (const :format "" quote)
261 (repeat
262 ,org-sorting-choice)))
263 (list :tag "Prefix format"
264 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
265 (string))
266 (list :tag "Number of days in agenda"
267 (const org-agenda-span)
268 (choice (const :tag "Day" 'day)
269 (const :tag "Week" 'week)
270 (const :tag "Month" 'month)
271 (const :tag "Year" 'year)
272 (integer :tag "Custom")))
273 (list :tag "Fixed starting date"
274 (const org-agenda-start-day)
275 (string :value "2007-11-01"))
276 (list :tag "Start on day of week"
277 (const org-agenda-start-on-weekday)
278 (choice :value 1
279 (const :tag "Today" nil)
280 (integer :tag "Weekday No.")))
281 (list :tag "Include data from diary"
282 (const org-agenda-include-diary)
283 (boolean))
284 (list :tag "Deadline Warning days"
285 (const org-deadline-warning-days)
286 (integer :value 1))
287 (list :tag "Tags filter preset"
288 (const org-agenda-filter-preset)
289 (list
290 (const :format "" quote)
291 (repeat
292 (string :tag "+tag or -tag"))))
293 (list :tag "Set daily/weekly entry types"
294 (const org-agenda-entry-types)
295 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
296 (const :deadline)
297 (const :scheduled)
298 (const :timestamp)
299 (const :sexp)))
300 (list :tag "Standard skipping condition"
301 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
302 (const org-agenda-skip-function)
303 (list
304 (const :format "" quote)
305 (list
306 (choice
307 :tag "Skipping range"
308 (const :tag "Skip entry" org-agenda-skip-entry-if)
309 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
310 (repeat :inline t :tag "Conditions for skipping"
311 (choice
312 :tag "Condition type"
313 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
314 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
315 (list :tag "TODO state is" :inline t
316 (const 'todo)
317 (choice
318 (const :tag "any not-done state" 'todo)
319 (const :tag "any done state" 'done)
320 (const :tag "any state" 'any)
321 (list :tag "Keyword list"
322 (const :format "" quote)
323 (repeat (string :tag "Keyword")))))
324 (list :tag "TODO state is not" :inline t
325 (const 'nottodo)
326 (choice
327 (const :tag "any not-done state" 'todo)
328 (const :tag "any done state" 'done)
329 (const :tag "any state" 'any)
330 (list :tag "Keyword list"
331 (const :format "" quote)
332 (repeat (string :tag "Keyword")))))
333 (const :tag "scheduled" 'scheduled)
334 (const :tag "not scheduled" 'notscheduled)
335 (const :tag "deadline" 'deadline)
336 (const :tag "no deadline" 'notdeadline)
337 (const :tag "timestamp" 'timestamp)
338 (const :tag "no timestamp" 'nottimestamp))))))
339 (list :tag "Non-standard skipping condition"
340 :value (org-agenda-skip-function)
341 (const org-agenda-skip-function)
342 (sexp :tag "Function or form (quoted!)"))
343 (list :tag "Any variable"
344 (variable :tag "Variable")
345 (sexp :tag "Value (sexp)"))))
346 "Selection of examples for agenda command settings.
347 This will be spliced into the custom type of
348 `org-agenda-custom-commands'.")
351 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
352 ((agenda "") (alltodo))))
353 "Custom commands for the agenda.
354 These commands will be offered on the splash screen displayed by the
355 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
357 (key desc type match settings files)
359 key The key (one or more characters as a string) to be associated
360 with the command.
361 desc A description of the command, when omitted or nil, a default
362 description is built using MATCH.
363 type The command type, any of the following symbols:
364 agenda The daily/weekly agenda.
365 todo Entries with a specific TODO keyword, in all agenda files.
366 search Entries containing search words entry or headline.
367 tags Tags/Property/TODO match in all agenda files.
368 tags-todo Tags/P/T match in all agenda files, TODO entries only.
369 todo-tree Sparse tree of specific TODO keyword in *current* file.
370 tags-tree Sparse tree with all tags matches in *current* file.
371 occur-tree Occur sparse tree for *current* file.
372 ... A user-defined function.
373 match What to search for:
374 - a single keyword for TODO keyword searches
375 - a tags match expression for tags searches
376 - a word search expression for text searches.
377 - a regular expression for occur searches
378 For all other commands, this should be the empty string.
379 settings A list of option settings, similar to that in a let form, so like
380 this: ((opt1 val1) (opt2 val2) ...). The values will be
381 evaluated at the moment of execution, so quote them when needed.
382 files A list of files file to write the produced agenda buffer to
383 with the command `org-store-agenda-views'.
384 If a file name ends in \".html\", an HTML version of the buffer
385 is written out. If it ends in \".ps\", a postscript version is
386 produced. Otherwise, only the plain text is written to the file.
388 You can also define a set of commands, to create a composite agenda buffer.
389 In this case, an entry looks like this:
391 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
393 where
395 desc A description string to be displayed in the dispatcher menu.
396 cmd An agenda command, similar to the above. However, tree commands
397 are no allowed, but instead you can get agenda and global todo list.
398 So valid commands for a set are:
399 (agenda \"\" settings)
400 (alltodo \"\" settings)
401 (stuck \"\" settings)
402 (todo \"match\" settings files)
403 (search \"match\" settings files)
404 (tags \"match\" settings files)
405 (tags-todo \"match\" settings files)
407 Each command can carry a list of options, and another set of options can be
408 given for the whole set of commands. Individual command options take
409 precedence over the general options.
411 When using several characters as key to a command, the first characters
412 are prefix commands. For the dispatcher to display useful information, you
413 should provide a description for the prefix, like
415 (setq org-agenda-custom-commands
416 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
417 (\"hl\" tags \"+HOME+Lisa\")
418 (\"hp\" tags \"+HOME+Peter\")
419 (\"hk\" tags \"+HOME+Kim\")))"
420 :group 'org-agenda-custom-commands
421 :type `(repeat
422 (choice :value ("x" "Describe command here" tags "" nil)
423 (list :tag "Single command"
424 (string :tag "Access Key(s) ")
425 (option (string :tag "Description"))
426 (choice
427 (const :tag "Agenda" agenda)
428 (const :tag "TODO list" alltodo)
429 (const :tag "Search words" search)
430 (const :tag "Stuck projects" stuck)
431 (const :tag "Tags/Property match (all agenda files)" tags)
432 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
433 (const :tag "TODO keyword search (all agenda files)" todo)
434 (const :tag "Tags sparse tree (current buffer)" tags-tree)
435 (const :tag "TODO keyword tree (current buffer)" todo-tree)
436 (const :tag "Occur tree (current buffer)" occur-tree)
437 (sexp :tag "Other, user-defined function"))
438 (string :tag "Match (only for some commands)")
439 ,org-agenda-custom-commands-local-options
440 (option (repeat :tag "Export" (file :tag "Export to"))))
441 (list :tag "Command series, all agenda files"
442 (string :tag "Access Key(s)")
443 (string :tag "Description ")
444 (repeat :tag "Component"
445 (choice
446 (list :tag "Agenda"
447 (const :format "" agenda)
448 (const :tag "" :format "" "")
449 ,org-agenda-custom-commands-local-options)
450 (list :tag "TODO list (all keywords)"
451 (const :format "" alltodo)
452 (const :tag "" :format "" "")
453 ,org-agenda-custom-commands-local-options)
454 (list :tag "Search words"
455 (const :format "" search)
456 (string :tag "Match")
457 ,org-agenda-custom-commands-local-options)
458 (list :tag "Stuck projects"
459 (const :format "" stuck)
460 (const :tag "" :format "" "")
461 ,org-agenda-custom-commands-local-options)
462 (list :tag "Tags search"
463 (const :format "" tags)
464 (string :tag "Match")
465 ,org-agenda-custom-commands-local-options)
466 (list :tag "Tags search, TODO entries only"
467 (const :format "" tags-todo)
468 (string :tag "Match")
469 ,org-agenda-custom-commands-local-options)
470 (list :tag "TODO keyword search"
471 (const :format "" todo)
472 (string :tag "Match")
473 ,org-agenda-custom-commands-local-options)
474 (list :tag "Other, user-defined function"
475 (symbol :tag "function")
476 (string :tag "Match")
477 ,org-agenda-custom-commands-local-options)))
479 (repeat :tag "Settings for entire command set"
480 (list (variable :tag "Any variable")
481 (sexp :tag "Value")))
482 (option (repeat :tag "Export" (file :tag "Export to"))))
483 (cons :tag "Prefix key documentation"
484 (string :tag "Access Key(s)")
485 (string :tag "Description ")))))
487 (defcustom org-agenda-query-register ?o
488 "The register holding the current query string.
489 The purpose of this is that if you construct a query string interactively,
490 you can then use it to define a custom command."
491 :group 'org-agenda-custom-commands
492 :type 'character)
494 (defcustom org-stuck-projects
495 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
496 "How to identify stuck projects.
497 This is a list of four items:
498 1. A tags/todo/property matcher string that is used to identify a project.
499 See the manual for a description of tag and property searches.
500 The entire tree below a headline matched by this is considered one project.
501 2. A list of TODO keywords identifying non-stuck projects.
502 If the project subtree contains any headline with one of these todo
503 keywords, the project is considered to be not stuck. If you specify
504 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
505 3. A list of tags identifying non-stuck projects.
506 If the project subtree contains any headline with one of these tags,
507 the project is considered to be not stuck. If you specify \"*\" as
508 a tag, any tag will mark the project unstuck. Note that this is about
509 the explicit presence of a tag somewhere in the subtree, inherited
510 tags to not count here. If inherited tags make a project not stuck,
511 use \"-TAG\" in the tags part of the matcher under (1.) above.
512 4. An arbitrary regular expression matching non-stuck projects.
514 If the project turns out to be not stuck, search continues also in the
515 subtree to see if any of the subtasks have project status.
517 See also the variable `org-tags-match-list-sublevels' which applies
518 to projects matched by this search as well.
520 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
521 or `C-c a #' to produce the list."
522 :group 'org-agenda-custom-commands
523 :type '(list
524 (string :tag "Tags/TODO match to identify a project")
525 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
526 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
527 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
529 (defcustom org-agenda-filter-effort-default-operator "<"
530 "The default operator for effort estimate filtering.
531 If you select an effort estimate limit without first pressing an operator,
532 this one will be used."
533 :group 'org-agenda-custom-commands
534 :type '(choice (const :tag "less or equal" "<")
535 (const :tag "greater or equal"">")
536 (const :tag "equal" "=")))
538 (defgroup org-agenda-skip nil
539 "Options concerning skipping parts of agenda files."
540 :tag "Org Agenda Skip"
541 :group 'org-agenda)
543 (defcustom org-agenda-skip-function-global nil
544 "Function to be called at each match during agenda construction.
545 If this function returns nil, the current match should not be skipped.
546 If the function decided to skip an agenda match, is must return the
547 buffer position from which the search should be continued.
548 This may also be a Lisp form, which will be evaluated.
550 This variable will be applied to every agenda match, including
551 tags/property searches and TODO lists. So try to make the test function
552 do its checking as efficiently as possible. To implement a skipping
553 condition just for specific agenda commands, use the variable
554 `org-agenda-skip-function' which can be set in the options section
555 of custom agenda commands."
556 :group 'org-agenda-skip
557 :type 'sexp)
559 (defgroup org-agenda-daily/weekly nil
560 "Options concerning the daily/weekly agenda."
561 :tag "Org Agenda Daily/Weekly"
562 :group 'org-agenda)
563 (defgroup org-agenda-todo-list nil
564 "Options concerning the global todo list agenda view."
565 :tag "Org Agenda Todo List"
566 :group 'org-agenda)
567 (defgroup org-agenda-match-view nil
568 "Options concerning the general tags/property/todo match agenda view."
569 :tag "Org Agenda Match View"
570 :group 'org-agenda)
571 (defgroup org-agenda-search-view nil
572 "Options concerning the general tags/property/todo match agenda view."
573 :tag "Org Agenda Match View"
574 :group 'org-agenda)
576 (defvar org-agenda-archives-mode nil
577 "Non-nil means the agenda will include archived items.
578 If this is the symbol `trees', trees in the selected agenda scope
579 that are marked with the ARCHIVE tag will be included anyway. When this is
580 t, also all archive files associated with the current selection of agenda
581 files will be included.")
583 (defcustom org-agenda-skip-comment-trees t
584 "Non-nil means skip trees that start with the COMMENT keyword.
585 When nil, these trees are also scanned by agenda commands."
586 :group 'org-agenda-skip
587 :type 'boolean)
589 (defcustom org-agenda-todo-list-sublevels t
590 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
591 When nil, the sublevels of a TODO entry are not checked, resulting in
592 potentially much shorter TODO lists."
593 :group 'org-agenda-skip
594 :group 'org-agenda-todo-list
595 :type 'boolean)
597 (defcustom org-agenda-todo-ignore-with-date nil
598 "Non-nil means don't show entries with a date in the global todo list.
599 You can use this if you prefer to mark mere appointments with a TODO keyword,
600 but don't want them to show up in the TODO list.
601 When this is set, it also covers deadlines and scheduled items, the settings
602 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
603 will be ignored.
604 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
605 :group 'org-agenda-skip
606 :group 'org-agenda-todo-list
607 :type 'boolean)
609 (defcustom org-agenda-todo-ignore-timestamp nil
610 "Non-nil means don't show entries with a timestamp.
611 This applies when creating the global todo list.
612 Valid values are:
614 past Don't show entries for today or in the past.
616 future Don't show entries with a timestamp in the future.
617 The idea behind this is that if it has a future
618 timestamp, you don't want to think about it until the
619 date.
621 all Don't show any entries with a timestamp in the global todo list.
622 The idea behind this is that by setting a timestamp, you
623 have already \"taken care\" of this item.
625 This variable can also have an integer as a value. If positive (N),
626 todos with a timestamp N or more days in the future will be ignored. If
627 negative (-N), todos with a timestamp N or more days in the past will be
628 ignored. If 0, todos with a timestamp either today or in the future will
629 be ignored. For example, a value of -1 will exclude todos with a
630 timestamp in the past (yesterday or earlier), while a value of 7 will
631 exclude todos with a timestamp a week or more in the future.
633 See also `org-agenda-todo-ignore-with-date'.
634 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
635 to make his option also apply to the tags-todo list."
636 :group 'org-agenda-skip
637 :group 'org-agenda-todo-list
638 :type '(choice
639 (const :tag "Ignore future timestamp todos" future)
640 (const :tag "Ignore past or present timestamp todos" past)
641 (const :tag "Ignore all timestamp todos" all)
642 (const :tag "Show timestamp todos" nil)
643 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
645 (defcustom org-agenda-todo-ignore-scheduled nil
646 "Non-nil means, ignore some scheduled TODO items when making TODO list.
647 This applies when creating the global todo list.
648 Valid values are:
650 past Don't show entries scheduled today or in the past.
652 future Don't show entries scheduled in the future.
653 The idea behind this is that by scheduling it, you don't want to
654 think about it until the scheduled date.
656 all Don't show any scheduled entries in the global todo list.
657 The idea behind this is that by scheduling it, you have already
658 \"taken care\" of this item.
660 t Same as `all', for backward compatibility.
662 This variable can also have an integer as a value. See
663 `org-agenda-todo-ignore-timestamp' for more details.
665 See also `org-agenda-todo-ignore-with-date'.
666 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
667 to make his option also apply to the tags-todo list."
668 :group 'org-agenda-skip
669 :group 'org-agenda-todo-list
670 :type '(choice
671 (const :tag "Ignore future-scheduled todos" future)
672 (const :tag "Ignore past- or present-scheduled todos" past)
673 (const :tag "Ignore all scheduled todos" all)
674 (const :tag "Ignore all scheduled todos (compatibility)" t)
675 (const :tag "Show scheduled todos" nil)
676 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
678 (defcustom org-agenda-todo-ignore-deadlines nil
679 "Non-nil means ignore some deadlined TODO items when making TODO list.
680 There are different motivations for using different values, please think
681 carefully when configuring this variable.
683 This applies when creating the global todo list.
684 Valid values are:
686 near Don't show near deadline entries. A deadline is near when it is
687 closer than `org-deadline-warning-days' days. The idea behind this
688 is that such items will appear in the agenda anyway.
690 far Don't show TODO entries where a deadline has been defined, but
691 the deadline is not near. This is useful if you don't want to
692 use the todo list to figure out what to do now.
694 past Don't show entries with a deadline timestamp for today or in the past.
696 future Don't show entries with a deadline timestamp in the future, not even
697 when they become `near' ones. Use it with caution.
699 all Ignore all TODO entries that do have a deadline.
701 t Same as `near', for backward compatibility.
703 This variable can also have an integer as a value. See
704 `org-agenda-todo-ignore-timestamp' for more details.
706 See also `org-agenda-todo-ignore-with-date'.
707 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
708 to make his option also apply to the tags-todo list."
709 :group 'org-agenda-skip
710 :group 'org-agenda-todo-list
711 :type '(choice
712 (const :tag "Ignore near deadlines" near)
713 (const :tag "Ignore near deadlines (compatibility)" t)
714 (const :tag "Ignore far deadlines" far)
715 (const :tag "Ignore all TODOs with a deadlines" all)
716 (const :tag "Show all TODOs, even if they have a deadline" nil)
717 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
719 (defcustom org-agenda-tags-todo-honor-ignore-options nil
720 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
721 The variables
722 `org-agenda-todo-ignore-with-date',
723 `org-agenda-todo-ignore-timestamp',
724 `org-agenda-todo-ignore-scheduled',
725 `org-agenda-todo-ignore-deadlines'
726 make the global TODO list skip entries that have time stamps of certain
727 kinds. If this option is set, the same options will also apply for the
728 tags-todo search, which is the general tags/property matcher
729 restricted to unfinished TODO entries only."
730 :group 'org-agenda-skip
731 :group 'org-agenda-todo-list
732 :group 'org-agenda-match-view
733 :type 'boolean)
735 (defcustom org-agenda-skip-scheduled-if-done nil
736 "Non-nil means don't show scheduled items in agenda when they are done.
737 This is relevant for the daily/weekly agenda, not for the TODO list. And
738 it applies only to the actual date of the scheduling. Warnings about
739 an item with a past scheduling dates are always turned off when the item
740 is DONE."
741 :group 'org-agenda-skip
742 :group 'org-agenda-daily/weekly
743 :type 'boolean)
745 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
746 "Non-nil means skip scheduling line if same entry shows because of deadline.
747 In the agenda of today, an entry can show up multiple times because
748 it is both scheduled and has a nearby deadline, and maybe a plain time
749 stamp as well.
750 When this variable is t, then only the deadline is shown and the fact that
751 the entry is scheduled today or was scheduled previously is not shown.
752 When this variable is nil, the entry will be shown several times. When
753 the variable is the symbol `not-today', then skip scheduled previously,
754 but not scheduled today."
755 :group 'org-agenda-skip
756 :group 'org-agenda-daily/weekly
757 :type '(choice
758 (const :tag "Never" nil)
759 (const :tag "Always" t)
760 (const :tag "Not when scheduled today" not-today)))
762 (defcustom org-agenda-skip-deadline-if-done nil
763 "Non-nil means don't show deadlines when the corresponding item is done.
764 When nil, the deadline is still shown and should give you a happy feeling.
765 This is relevant for the daily/weekly agenda. And it applied only to the
766 actually date of the deadline. Warnings about approaching and past-due
767 deadlines are always turned off when the item is DONE."
768 :group 'org-agenda-skip
769 :group 'org-agenda-daily/weekly
770 :type 'boolean)
772 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
773 "Non-nil means skip deadline prewarning when entry is also scheduled.
774 This will apply on all days where a prewarning for the deadline would
775 be shown, but not at the day when the entry is actually due. On that day,
776 the deadline will be shown anyway.
777 This variable may be set to nil, t, or a number which will then give
778 the number of days before the actual deadline when the prewarnings
779 should resume.
780 This can be used in a workflow where the first showing of the deadline will
781 trigger you to schedule it, and then you don't want to be reminded of it
782 because you will take care of it on the day when scheduled."
783 :group 'org-agenda-skip
784 :group 'org-agenda-daily/weekly
785 :type '(choice
786 (const :tag "Alwas show prewarning" nil)
787 (const :tag "Remove prewarning if entry is scheduled" t)
788 (integer :tag "Restart prewarning N days before deadline")))
790 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
791 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
792 When non-nil, after the search for timestamps has matched once in an
793 entry, the rest of the entry will not be searched."
794 :group 'org-agenda-skip
795 :type 'boolean)
797 (defcustom org-agenda-skip-timestamp-if-done nil
798 "Non-nil means don't select item by timestamp or -range if it is DONE."
799 :group 'org-agenda-skip
800 :group 'org-agenda-daily/weekly
801 :type 'boolean)
803 (defcustom org-agenda-dim-blocked-tasks t
804 "Non-nil means dim blocked tasks in the agenda display.
805 This causes some overhead during agenda construction, but if you
806 have turned on `org-enforce-todo-dependencies',
807 `org-enforce-todo-checkbox-dependencies', or any other blocking
808 mechanism, this will create useful feedback in the agenda.
810 Instead of t, this variable can also have the value `invisible'.
811 Then blocked tasks will be invisible and only become visible when
812 they become unblocked. An exemption to this behavior is when a task is
813 blocked because of unchecked checkboxes below it. Since checkboxes do
814 not show up in the agenda views, making this task invisible you remove any
815 trace from agenda views that there is something to do. Therefore, a task
816 that is blocked because of checkboxes will never be made invisible, it
817 will only be dimmed."
818 :group 'org-agenda-daily/weekly
819 :group 'org-agenda-todo-list
820 :type '(choice
821 (const :tag "Do not dim" nil)
822 (const :tag "Dim to a grey face" t)
823 (const :tag "Make invisible" invisible)))
825 (defcustom org-timeline-show-empty-dates 3
826 "Non-nil means `org-timeline' also shows dates without an entry.
827 When nil, only the days which actually have entries are shown.
828 When t, all days between the first and the last date are shown.
829 When an integer, show also empty dates, but if there is a gap of more than
830 N days, just insert a special line indicating the size of the gap."
831 :group 'org-agenda-skip
832 :type '(choice
833 (const :tag "None" nil)
834 (const :tag "All" t)
835 (integer :tag "at most")))
837 (defgroup org-agenda-startup nil
838 "Options concerning initial settings in the Agenda in Org Mode."
839 :tag "Org Agenda Startup"
840 :group 'org-agenda)
842 (defcustom org-agenda-menu-show-matcher t
843 "Non-nil means show the match string in the agenda dispatcher menu.
844 When nil, the matcher string is not shown, but is put into the help-echo
845 property so than moving the mouse over the command shows it.
846 Setting it to nil is good if matcher strings are very long and/or if
847 you want to use two-column display (see `org-agenda-menu-two-column')."
848 :group 'org-agenda
849 :type 'boolean)
851 (defcustom org-agenda-menu-two-column nil
852 "Non-nil means, use two columns to show custom commands in the dispatcher.
853 If you use this, you probably want to set `org-agenda-menu-show-matcher'
854 to nil."
855 :group 'org-agenda
856 :type 'boolean)
858 (defcustom org-finalize-agenda-hook nil
859 "Hook run just before displaying an agenda buffer."
860 :group 'org-agenda-startup
861 :type 'hook)
863 (defcustom org-agenda-mouse-1-follows-link nil
864 "Non-nil means mouse-1 on a link will follow the link in the agenda.
865 A longer mouse click will still set point. Does not work on XEmacs.
866 Needs to be set before org.el is loaded."
867 :group 'org-agenda-startup
868 :type 'boolean)
870 (defcustom org-agenda-start-with-follow-mode nil
871 "The initial value of follow mode in a newly created agenda window."
872 :group 'org-agenda-startup
873 :type 'boolean)
875 (defcustom org-agenda-follow-indirect nil
876 "Non-nil means `org-agenda-follow-mode' displays only the
877 current item's tree, in an indirect buffer."
878 :group 'org-agenda
879 :type 'boolean)
881 (defcustom org-agenda-show-outline-path t
882 "Non-nil means show outline path in echo area after line motion."
883 :group 'org-agenda-startup
884 :type 'boolean)
886 (defcustom org-agenda-start-with-entry-text-mode nil
887 "The initial value of entry-text-mode in a newly created agenda window."
888 :group 'org-agenda-startup
889 :type 'boolean)
891 (defcustom org-agenda-entry-text-maxlines 5
892 "Number of text lines to be added when `E' is pressed in the agenda.
894 Note that this variable only used during agenda display. Add add entry text
895 when exporting the agenda, configure the variable
896 `org-agenda-add-entry-ext-maxlines'."
897 :group 'org-agenda
898 :type 'integer)
900 (defcustom org-agenda-entry-text-exclude-regexps nil
901 "List of regular expressions to clean up entry text.
902 The complete matches of all regular expressions in this list will be
903 removed from entry text before it is shown in the agenda."
904 :group 'org-agenda
905 :type '(repeat (regexp)))
907 (defvar org-agenda-entry-text-cleanup-hook nil
908 "Hook that is run after basic cleanup of entry text to be shown in agenda.
909 This cleanup is done in a temporary buffer, so the function may inspect and
910 change the entire buffer.
911 Some default stuff like drawers and scheduling/deadline dates will already
912 have been removed when this is called, as will any matches for regular
913 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
915 (defvar org-agenda-include-inactive-timestamps nil
916 "Non-nil means include inactive time stamps in agenda and timeline.")
918 (defgroup org-agenda-windows nil
919 "Options concerning the windows used by the Agenda in Org Mode."
920 :tag "Org Agenda Windows"
921 :group 'org-agenda)
923 (defcustom org-agenda-window-setup 'reorganize-frame
924 "How the agenda buffer should be displayed.
925 Possible values for this option are:
927 current-window Show agenda in the current window, keeping all other windows.
928 other-window Use `switch-to-buffer-other-window' to display agenda.
929 reorganize-frame Show only two windows on the current frame, the current
930 window and the agenda.
931 other-frame Use `switch-to-buffer-other-frame' to display agenda.
932 Also, when exiting the agenda, kill that frame.
933 See also the variable `org-agenda-restore-windows-after-quit'."
934 :group 'org-agenda-windows
935 :type '(choice
936 (const current-window)
937 (const other-frame)
938 (const other-window)
939 (const reorganize-frame)))
941 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
942 "The min and max height of the agenda window as a fraction of frame height.
943 The value of the variable is a cons cell with two numbers between 0 and 1.
944 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
945 :group 'org-agenda-windows
946 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
948 (defcustom org-agenda-restore-windows-after-quit nil
949 "Non-nil means restore window configuration upon exiting agenda.
950 Before the window configuration is changed for displaying the agenda,
951 the current status is recorded. When the agenda is exited with
952 `q' or `x' and this option is set, the old state is restored. If
953 `org-agenda-window-setup' is `other-frame', the value of this
954 option will be ignored."
955 :group 'org-agenda-windows
956 :type 'boolean)
958 (defcustom org-agenda-ndays nil
959 "Number of days to include in overview display.
960 Should be 1 or 7.
961 Obsolete, see `org-agenda-span'."
962 :group 'org-agenda-daily/weekly
963 :type 'integer)
965 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
967 (defcustom org-agenda-span 'week
968 "Number of days to include in overview display.
969 Can be day, week, month, year, or any number of days.
970 Custom commands can set this variable in the options section."
971 :group 'org-agenda-daily/weekly
972 :type '(choice (const :tag "Day" day)
973 (const :tag "Week" week)
974 (const :tag "Month" month)
975 (const :tag "Year" year)
976 (integer :tag "Custom")))
978 (defcustom org-agenda-start-on-weekday 1
979 "Non-nil means start the overview always on the specified weekday.
980 0 denotes Sunday, 1 denotes Monday etc.
981 When nil, always start on the current day.
982 Custom commands can set this variable in the options section."
983 :group 'org-agenda-daily/weekly
984 :type '(choice (const :tag "Today" nil)
985 (integer :tag "Weekday No.")))
987 (defcustom org-agenda-show-all-dates t
988 "Non-nil means `org-agenda' shows every day in the selected range.
989 When nil, only the days which actually have entries are shown."
990 :group 'org-agenda-daily/weekly
991 :type 'boolean)
993 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
994 "Format string for displaying dates in the agenda.
995 Used by the daily/weekly agenda and by the timeline. This should be
996 a format string understood by `format-time-string', or a function returning
997 the formatted date as a string. The function must take a single argument,
998 a calendar-style date list like (month day year)."
999 :group 'org-agenda-daily/weekly
1000 :type '(choice
1001 (string :tag "Format string")
1002 (function :tag "Function")))
1004 (defun org-agenda-format-date-aligned (date)
1005 "Format a date string for display in the daily/weekly agenda, or timeline.
1006 This function makes sure that dates are aligned for easy reading."
1007 (require 'cal-iso)
1008 (let* ((dayname (calendar-day-name date))
1009 (day (cadr date))
1010 (day-of-week (calendar-day-of-week date))
1011 (month (car date))
1012 (monthname (calendar-month-name month))
1013 (year (nth 2 date))
1014 (iso-week (org-days-to-iso-week
1015 (calendar-absolute-from-gregorian date)))
1016 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1017 (1- year))
1018 ((and (= month 12) (<= iso-week 1))
1019 (1+ year))
1020 (t year)))
1021 (weekstring (if (= day-of-week 1)
1022 (format " W%02d" iso-week)
1023 "")))
1024 (format "%-10s %2d %s %4d%s"
1025 dayname day monthname year weekstring)))
1027 (defcustom org-agenda-time-leading-zero nil
1028 "Non-nil means use leading zero for military times in agenda.
1029 For example, 9:30am would become 09:30 rather than 9:30."
1030 :group 'org-agenda-daily/weekly
1031 :type 'boolean)
1033 (defcustom org-agenda-timegrid-use-ampm nil
1034 "When set, show AM/PM style timestamps on the timegrid."
1035 :group 'org-agenda
1036 :type 'boolean)
1038 (defun org-agenda-time-of-day-to-ampm (time)
1039 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1040 (let* ((hour-number (string-to-number (substring time 0 -3)))
1041 (minute (substring time -2))
1042 (ampm "am"))
1043 (cond
1044 ((equal hour-number 12)
1045 (setq ampm "pm"))
1046 ((> hour-number 12)
1047 (setq ampm "pm")
1048 (setq hour-number (- hour-number 12))))
1049 (concat
1050 (if org-agenda-time-leading-zero
1051 (format "%02d" hour-number)
1052 (format "%02s" (number-to-string hour-number)))
1053 ":" minute ampm)))
1055 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1056 "Conditionally convert TIME to AM/PM format
1057 based on `org-agenda-timegrid-use-ampm'"
1058 (if org-agenda-timegrid-use-ampm
1059 (org-agenda-time-of-day-to-ampm time)
1060 time))
1062 (defcustom org-agenda-weekend-days '(6 0)
1063 "Which days are weekend?
1064 These days get the special face `org-agenda-date-weekend' in the agenda
1065 and timeline buffers."
1066 :group 'org-agenda-daily/weekly
1067 :type '(set :greedy t
1068 (const :tag "Monday" 1)
1069 (const :tag "Tuesday" 2)
1070 (const :tag "Wednesday" 3)
1071 (const :tag "Thursday" 4)
1072 (const :tag "Friday" 5)
1073 (const :tag "Saturday" 6)
1074 (const :tag "Sunday" 0)))
1076 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1077 "Non-nil means jumpt to today when moving a past date forward in time.
1078 When using S-right in the agenda to move a a date forward, and the date
1079 stamp currently points to the past, the first key press will move it
1080 to today. WHen nil, just move one day forward even if the date stays
1081 in the past."
1082 :group 'org-agenda-daily/weekly
1083 :type 'boolean)
1085 (defcustom org-agenda-include-diary nil
1086 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1087 Custom commands can set this variable in the options section."
1088 :group 'org-agenda-daily/weekly
1089 :type 'boolean)
1091 (defcustom org-agenda-include-deadlines t
1092 "If non-nil, include entries within their deadline warning period.
1093 Custom commands can set this variable in the options section."
1094 :group 'org-agenda-daily/weekly
1095 :type 'boolean)
1097 (defcustom org-agenda-repeating-timestamp-show-all t
1098 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1099 When set to a list of strings, only show occurrences of repeating
1100 stamps for these TODO keywords. When nil, only one occurrence is
1101 shown, either today or the nearest into the future."
1102 :group 'org-agenda-daily/weekly
1103 :type '(choice
1104 (const :tag "Show repeating stamps" t)
1105 (repeat :tag "Show repeating stamps for these TODO keywords"
1106 (string :tag "TODO Keyword"))
1107 (const :tag "Don't show repeating stamps" nil)))
1109 (defcustom org-scheduled-past-days 10000
1110 "No. of days to continue listing scheduled items that are not marked DONE.
1111 When an item is scheduled on a date, it shows up in the agenda on this
1112 day and will be listed until it is marked done for the number of days
1113 given here."
1114 :group 'org-agenda-daily/weekly
1115 :type 'integer)
1117 (defcustom org-agenda-log-mode-items '(closed clock)
1118 "List of items that should be shown in agenda log mode.
1119 This list may contain the following symbols:
1121 closed Show entries that have been closed on that day.
1122 clock Show entries that have received clocked time on that day.
1123 state Show all logged state changes.
1124 Note that instead of changing this variable, you can also press `C-u l' in
1125 the agenda to display all available LOG items temporarily."
1126 :group 'org-agenda-daily/weekly
1127 :type '(set :greedy t (const closed) (const clock) (const state)))
1129 (defcustom org-agenda-clock-consistency-checks
1130 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1131 :gap-ok-around ("4:00")
1132 :default-face ((:background "DarkRed") (:foreground "white"))
1133 :overlap-face nil :gap-face nil :no-end-time-face nil
1134 :long-face nil :short-face nil)
1135 "This is a property list, with the following keys:
1137 :max-duration Mark clocking chunks that are longer than this time.
1138 This is a time string like \"HH:MM\", or the number
1139 of minutes as an integer.
1141 :min-duration Mark clocking chunks that are shorter that this.
1142 This is a time string like \"HH:MM\", or the number
1143 of minutes as an integer.
1145 :max-gap Mark gaps between clocking chunks that are longer than
1146 this duration. A number of minutes, or a string
1147 like \"HH:MM\".
1149 :gap-ok-around List of times during the day which are usually not working
1150 times. When a gap is detected, but the gap contains any
1151 of these times, the gap is *not* reported. For example,
1152 if this is (\"4:00\" \"13:00\") then gaps that contain
1153 4:00 in the morning (i.e. the night) and 13:00
1154 (i.e. a typical lunch time) do not cause a warning.
1155 You should have at least one time during the night in this
1156 list, or otherwise the first task each morning will trigger
1157 a warning because it follows a long gap.
1159 Furthermore, the following properties can be used to define faces for
1160 issue display.
1162 :default-face the default face, if the specific face is undefined
1163 :overlap-face face for overlapping clocks
1164 :gap-face face for gaps between clocks
1165 :no-end-time-face face for incomplete clocks
1166 :long-face face for clock intervals that are too long
1167 :short-face face for clock intervals that are too short"
1168 :group 'org-agenda-daily/weekly
1169 :group 'org-clock
1170 :type 'plist)
1172 (defcustom org-agenda-log-mode-add-notes t
1173 "Non-nil means add first line of notes to log entries in agenda views.
1174 If a log item like a state change or a clock entry is associated with
1175 notes, the first line of these notes will be added to the entry in the
1176 agenda display."
1177 :group 'org-agenda-daily/weekly
1178 :type 'boolean)
1180 (defcustom org-agenda-start-with-log-mode nil
1181 "The initial value of log-mode in a newly created agenda window."
1182 :group 'org-agenda-startup
1183 :group 'org-agenda-daily/weekly
1184 :type 'boolean)
1186 (defcustom org-agenda-start-with-clockreport-mode nil
1187 "The initial value of clockreport-mode in a newly created agenda window."
1188 :group 'org-agenda-startup
1189 :group 'org-agenda-daily/weekly
1190 :type 'boolean)
1192 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1193 "Property list with parameters for the clocktable in clockreport mode.
1194 This is the display mode that shows a clock table in the daily/weekly
1195 agenda, the properties for this dynamic block can be set here.
1196 The usual clocktable parameters are allowed here, but you cannot set
1197 the properties :name, :tstart, :tend, :block, and :scope - these will
1198 be overwritten to make sure the content accurately reflects the
1199 current display in the agenda."
1200 :group 'org-agenda-daily/weekly
1201 :type 'plist)
1203 (defcustom org-agenda-search-view-always-boolean nil
1204 "Non-nil means the search string is interpreted as individual parts.
1206 The search string for search view can either be interpreted as a phrase,
1207 or as a list of snippets that define a boolean search for a number of
1208 strings.
1210 When this is non-nil, the string will be split on whitespace, and each
1211 snippet will be searched individually, and all must match in order to
1212 select an entry. A snippet is then a single string of non-white
1213 characters, or a string in double quotes, or a regexp in {} braces.
1214 If a snippet is preceded by \"-\", the snippet must *not* match.
1215 \"+\" is syntactic sugar for positive selection. Each snippet may
1216 be found as a full word or a partial word, but see the variable
1217 `org-agenda-search-view-force-full-words'.
1219 When this is nil, search will look for the entire search phrase as one,
1220 with each space character matching any amount of whitespace, including
1221 line breaks.
1223 Even when this is nil, you can still switch to Boolean search dynamically
1224 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1225 is a regexp marked with braces like \"{abc}\", this will also switch to
1226 boolean search."
1227 :group 'org-agenda-search-view
1228 :type 'boolean)
1230 (if (fboundp 'defvaralias)
1231 (defvaralias 'org-agenda-search-view-search-words-only
1232 'org-agenda-search-view-always-boolean))
1234 (defcustom org-agenda-search-view-force-full-words nil
1235 "Non-nil means, search words must be matches as complete words.
1236 When nil, they may also match part of a word."
1237 :group 'org-agenda-search-view
1238 :type 'boolean)
1240 (defgroup org-agenda-time-grid nil
1241 "Options concerning the time grid in the Org-mode Agenda."
1242 :tag "Org Agenda Time Grid"
1243 :group 'org-agenda)
1245 (defcustom org-agenda-search-headline-for-time t
1246 "Non-nil means search headline for a time-of-day.
1247 If the headline contains a time-of-day in one format or another, it will
1248 be used to sort the entry into the time sequence of items for a day.
1249 Some people have time stamps in the headline that refer to the creation
1250 time or so, and then this produces an unwanted side effect. If this is
1251 the case for your, use this variable to turn off searching the headline
1252 for a time."
1253 :group 'org-agenda-time-grid
1254 :type 'boolean)
1256 (defcustom org-agenda-use-time-grid t
1257 "Non-nil means show a time grid in the agenda schedule.
1258 A time grid is a set of lines for specific times (like every two hours between
1259 8:00 and 20:00). The items scheduled for a day at specific times are
1260 sorted in between these lines.
1261 For details about when the grid will be shown, and what it will look like, see
1262 the variable `org-agenda-time-grid'."
1263 :group 'org-agenda-time-grid
1264 :type 'boolean)
1266 (defcustom org-agenda-time-grid
1267 '((daily today require-timed)
1268 "----------------"
1269 (800 1000 1200 1400 1600 1800 2000))
1271 "The settings for time grid for agenda display.
1272 This is a list of three items. The first item is again a list. It contains
1273 symbols specifying conditions when the grid should be displayed:
1275 daily if the agenda shows a single day
1276 weekly if the agenda shows an entire week
1277 today show grid on current date, independent of daily/weekly display
1278 require-timed show grid only if at least one item has a time specification
1280 The second item is a string which will be placed behind the grid time.
1282 The third item is a list of integers, indicating the times that should have
1283 a grid line."
1284 :group 'org-agenda-time-grid
1285 :type
1286 '(list
1287 (set :greedy t :tag "Grid Display Options"
1288 (const :tag "Show grid in single day agenda display" daily)
1289 (const :tag "Show grid in weekly agenda display" weekly)
1290 (const :tag "Always show grid for today" today)
1291 (const :tag "Show grid only if any timed entries are present"
1292 require-timed)
1293 (const :tag "Skip grid times already present in an entry"
1294 remove-match))
1295 (string :tag "Grid String")
1296 (repeat :tag "Grid Times" (integer :tag "Time"))))
1298 (defcustom org-agenda-show-current-time-in-grid t
1299 "Non-nil means show the current time in the time grid."
1300 :group 'org-agenda-time-grid
1301 :type 'boolean)
1303 (defcustom org-agenda-current-time-string
1304 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1305 "The string for the current time marker in the agenda."
1306 :group 'org-agenda-time-grid
1307 :type 'string)
1309 (defgroup org-agenda-sorting nil
1310 "Options concerning sorting in the Org-mode Agenda."
1311 :tag "Org Agenda Sorting"
1312 :group 'org-agenda)
1314 (defcustom org-agenda-sorting-strategy
1315 '((agenda habit-down time-up priority-down category-keep)
1316 (todo priority-down category-keep)
1317 (tags priority-down category-keep)
1318 (search category-keep))
1319 "Sorting structure for the agenda items of a single day.
1320 This is a list of symbols which will be used in sequence to determine
1321 if an entry should be listed before another entry. The following
1322 symbols are recognized:
1324 time-up Put entries with time-of-day indications first, early first
1325 time-down Put entries with time-of-day indications first, late first
1326 category-keep Keep the default order of categories, corresponding to the
1327 sequence in `org-agenda-files'.
1328 category-up Sort alphabetically by category, A-Z.
1329 category-down Sort alphabetically by category, Z-A.
1330 tag-up Sort alphabetically by last tag, A-Z.
1331 tag-down Sort alphabetically by last tag, Z-A.
1332 priority-up Sort numerically by priority, high priority last.
1333 priority-down Sort numerically by priority, high priority first.
1334 todo-state-up Sort by todo state, tasks that are done last.
1335 todo-state-down Sort by todo state, tasks that are done first.
1336 effort-up Sort numerically by estimated effort, high effort last.
1337 effort-down Sort numerically by estimated effort, high effort first.
1338 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1339 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1340 habit-up Put entries that are habits first
1341 habit-down Put entries that are habits last
1342 alpha-up Sort headlines alphabetically
1343 alpha-down Sort headlines alphabetically, reversed
1345 The different possibilities will be tried in sequence, and testing stops
1346 if one comparison returns a \"not-equal\". For example, the default
1347 '(time-up category-keep priority-down)
1348 means: Pull out all entries having a specified time of day and sort them,
1349 in order to make a time schedule for the current day the first thing in the
1350 agenda listing for the day. Of the entries without a time indication, keep
1351 the grouped in categories, don't sort the categories, but keep them in
1352 the sequence given in `org-agenda-files'. Within each category sort by
1353 priority.
1355 Leaving out `category-keep' would mean that items will be sorted across
1356 categories by priority.
1358 Instead of a single list, this can also be a set of list for specific
1359 contents, with a context symbol in the car of the list, any of
1360 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1362 Custom commands can bind this variable in the options section."
1363 :group 'org-agenda-sorting
1364 :type `(choice
1365 (repeat :tag "General" ,org-sorting-choice)
1366 (list :tag "Individually"
1367 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1368 (repeat ,org-sorting-choice))
1369 (cons (const :tag "Strategy for TODO lists" todo)
1370 (repeat ,org-sorting-choice))
1371 (cons (const :tag "Strategy for Tags matches" tags)
1372 (repeat ,org-sorting-choice))
1373 (cons (const :tag "Strategy for search matches" search)
1374 (repeat ,org-sorting-choice)))))
1376 (defcustom org-agenda-cmp-user-defined nil
1377 "A function to define the comparison `user-defined'.
1378 This function must receive two arguments, agenda entry a and b.
1379 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1380 the user comparison, return nil.
1381 When this is defined, you can make `user-defined-up' and `user-defined-down'
1382 part of an agenda sorting strategy."
1383 :group 'org-agenda-sorting
1384 :type 'symbol)
1386 (defcustom org-sort-agenda-notime-is-late t
1387 "Non-nil means items without time are considered late.
1388 This is only relevant for sorting. When t, items which have no explicit
1389 time like 15:30 will be considered as 99:01, i.e. later than any items which
1390 do have a time. When nil, the default time is before 0:00. You can use this
1391 option to decide if the schedule for today should come before or after timeless
1392 agenda entries."
1393 :group 'org-agenda-sorting
1394 :type 'boolean)
1396 (defcustom org-sort-agenda-noeffort-is-high t
1397 "Non-nil means items without effort estimate are sorted as high effort.
1398 This also applies when filtering an agenda view with respect to the
1399 < or > effort operator. Then, tasks with no effort defined will be treated
1400 as tasks with high effort.
1401 When nil, such items are sorted as 0 minutes effort."
1402 :group 'org-agenda-sorting
1403 :type 'boolean)
1405 (defgroup org-agenda-line-format nil
1406 "Options concerning the entry prefix in the Org-mode agenda display."
1407 :tag "Org Agenda Line Format"
1408 :group 'org-agenda)
1410 (defcustom org-agenda-prefix-format
1411 '((agenda . " %i %-12:c%?-12t% s")
1412 (timeline . " % s")
1413 (todo . " %i %-12:c")
1414 (tags . " %i %-12:c")
1415 (search . " %i %-12:c"))
1416 "Format specifications for the prefix of items in the agenda views.
1417 An alist with five entries, each for the different agenda types. The
1418 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1419 The values are format strings.
1421 This format works similar to a printf format, with the following meaning:
1423 %c the category of the item, \"Diary\" for entries from the diary,
1424 or as given by the CATEGORY keyword or derived from the file name
1425 %e the effort required by the item
1426 %i the icon category of the item, see `org-agenda-category-icon-alist'
1427 %T the last tag of the item (ignore inherited tags, which come first)
1428 %t the HH:MM time-of-day specification if one applies to the entry
1429 %s Scheduling/Deadline information, a short string
1430 %(expression) Eval EXPRESSION and replace the control string
1431 by the result
1433 All specifiers work basically like the standard `%s' of printf, but may
1434 contain two additional characters: a question mark just after the `%'
1435 and a whitespace/punctuation character just before the final letter.
1437 If the first character after `%' is a question mark, the entire field
1438 will only be included if the corresponding value applies to the current
1439 entry. This is useful for fields which should have fixed width when
1440 present, but zero width when absent. For example, \"%?-12t\" will
1441 result in a 12 character time field if a time of the day is specified,
1442 but will completely disappear in entries which do not contain a time.
1444 If there is punctuation or whitespace character just before the final
1445 format letter, this character will be appended to the field value if
1446 the value is not empty. For example, the format \"%-12:c\" leads to
1447 \"Diary: \" if the category is \"Diary\". If the category were be
1448 empty, no additional colon would be inserted.
1450 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1451 which means:
1453 - Indent the line with two space characters
1454 - Give the category a 12 chars wide field, padded with whitespace on
1455 the right (because of `-'). Append a colon if there is a category
1456 (because of `:').
1457 - If there is a time-of-day, put it into a 12 chars wide field. If no
1458 time, don't put in an empty field, just skip it (because of '?').
1459 - Finally, put the scheduling information.
1461 See also the variables `org-agenda-remove-times-when-in-prefix' and
1462 `org-agenda-remove-tags'.
1464 Custom commands can set this variable in the options section."
1465 :type '(choice
1466 (string :tag "General format")
1467 (list :greedy t :tag "View dependent"
1468 (cons (const agenda) (string :tag "Format"))
1469 (cons (const timeline) (string :tag "Format"))
1470 (cons (const todo) (string :tag "Format"))
1471 (cons (const tags) (string :tag "Format"))
1472 (cons (const search) (string :tag "Format"))))
1473 :group 'org-agenda-line-format)
1475 (defvar org-prefix-format-compiled nil
1476 "The compiled version of the most recently used prefix format.
1477 See the variable `org-agenda-prefix-format'.")
1479 (defcustom org-agenda-todo-keyword-format "%-1s"
1480 "Format for the TODO keyword in agenda lines.
1481 Set this to something like \"%-12s\" if you want all TODO keywords
1482 to occupy a fixed space in the agenda display."
1483 :group 'org-agenda-line-format
1484 :type 'string)
1486 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1487 "Text preceding timerange entries in the agenda view.
1488 This is a list with two strings. The first applies when the range
1489 is entirely on one day. The second applies if the range spans several days.
1490 The strings may have two \"%d\" format specifiers which will be filled
1491 with the sequence number of the days, and the total number of days in the
1492 range, respectively."
1493 :group 'org-agenda-line-format
1494 :type '(list
1495 (string :tag "Deadline today ")
1496 (choice :tag "Deadline relative"
1497 (string :tag "Format string")
1498 (function))))
1500 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1501 "Text preceding scheduled items in the agenda view.
1502 This is a list with two strings. The first applies when the item is
1503 scheduled on the current day. The second applies when it has been scheduled
1504 previously, it may contain a %d indicating that this is the nth time that
1505 this item is scheduled, due to automatic rescheduling of unfinished items
1506 for the following day. So this number is one larger than the number of days
1507 that passed since this item was scheduled first."
1508 :group 'org-agenda-line-format
1509 :type '(list
1510 (string :tag "Scheduled today ")
1511 (string :tag "Scheduled previously")))
1513 (defcustom org-agenda-inactive-leader "["
1514 "Text preceding item pulled into the agenda by inactive time stamps.
1515 These entries are added to the agenda when pressing \"[\"."
1516 :group 'org-agenda-line-format
1517 :type '(list
1518 (string :tag "Scheduled today ")
1519 (string :tag "Scheduled previously")))
1521 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1522 "Text preceding deadline items in the agenda view.
1523 This is a list with two strings. The first applies when the item has its
1524 deadline on the current day. The second applies when it is in the past or
1525 in the future, it may contain %d to capture how many days away the deadline
1526 is (was)."
1527 :group 'org-agenda-line-format
1528 :type '(list
1529 (string :tag "Deadline today ")
1530 (choice :tag "Deadline relative"
1531 (string :tag "Format string")
1532 (function))))
1534 (defcustom org-agenda-remove-times-when-in-prefix t
1535 "Non-nil means remove duplicate time specifications in agenda items.
1536 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1537 time-of-day specification in a headline or diary entry is extracted and
1538 placed into the prefix. If this option is non-nil, the original specification
1539 \(a timestamp or -range, or just a plain time(range) specification like
1540 11:30-4pm) will be removed for agenda display. This makes the agenda less
1541 cluttered.
1542 The option can be t or nil. It may also be the symbol `beg', indicating
1543 that the time should only be removed when it is located at the beginning of
1544 the headline/diary entry."
1545 :group 'org-agenda-line-format
1546 :type '(choice
1547 (const :tag "Always" t)
1548 (const :tag "Never" nil)
1549 (const :tag "When at beginning of entry" beg)))
1551 (defcustom org-agenda-remove-timeranges-from-blocks nil
1552 "Non-nil means remove time ranges specifications in agenda
1553 items that span on several days."
1554 :group 'org-agenda-line-format
1555 :type 'boolean)
1557 (defcustom org-agenda-default-appointment-duration nil
1558 "Default duration for appointments that only have a starting time.
1559 When nil, no duration is specified in such cases.
1560 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1561 :group 'org-agenda-line-format
1562 :type '(choice
1563 (integer :tag "Minutes")
1564 (const :tag "No default duration")))
1566 (defcustom org-agenda-show-inherited-tags t
1567 "Non-nil means show inherited tags in each agenda line."
1568 :group 'org-agenda-line-format
1569 :type 'boolean)
1571 (defcustom org-agenda-hide-tags-regexp nil
1572 "Regular expression used to filter away specific tags in agenda views.
1573 This means that these tags will be present, but not be shown in the agenda
1574 line. Secondary filtering will still work on the hidden tags.
1575 Nil means don't hide any tags."
1576 :group 'org-agenda-line-format
1577 :type '(choice
1578 (const :tag "Hide none" nil)
1579 (string :tag "Regexp ")))
1581 (defcustom org-agenda-remove-tags nil
1582 "Non-nil means remove the tags from the headline copy in the agenda.
1583 When this is the symbol `prefix', only remove tags when
1584 `org-agenda-prefix-format' contains a `%T' specifier."
1585 :group 'org-agenda-line-format
1586 :type '(choice
1587 (const :tag "Always" t)
1588 (const :tag "Never" nil)
1589 (const :tag "When prefix format contains %T" prefix)))
1591 (if (fboundp 'defvaralias)
1592 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1593 'org-agenda-remove-tags))
1595 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1596 "Shift tags in agenda items to this column.
1597 If this number is positive, it specifies the column. If it is negative,
1598 it means that the tags should be flushright to that column. For example,
1599 -80 works well for a normal 80 character screen."
1600 :group 'org-agenda-line-format
1601 :type 'integer)
1603 (if (fboundp 'defvaralias)
1604 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1606 (defcustom org-agenda-fontify-priorities 'cookies
1607 "Non-nil means highlight low and high priorities in agenda.
1608 When t, the highest priority entries are bold, lowest priority italic.
1609 However, settings in `org-priority-faces' will overrule these faces.
1610 When this variable is the symbol `cookies', only fontify the
1611 cookies, not the entire task.
1612 This may also be an association list of priority faces, whose
1613 keys are the character values of `org-highest-priority',
1614 `org-default-priority', and `org-lowest-priority' (the default values
1615 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1616 color as a string, or a list like `(:background \"Red\")'.
1617 If it is a color, the variable `org-faces-easy-properties'
1618 determines if it is a foreground or a background color."
1619 :group 'org-agenda-line-format
1620 :type '(choice
1621 (const :tag "Never" nil)
1622 (const :tag "Defaults" t)
1623 (const :tag "Cookies only" cookies)
1624 (repeat :tag "Specify"
1625 (list (character :tag "Priority" :value ?A)
1626 (choice :tag "Face "
1627 (string :tag "Color")
1628 (sexp :tag "Face"))))))
1630 (defcustom org-agenda-day-face-function nil
1631 "Function called to determine what face should be used to display a day.
1632 The only argument passed to that function is the day. It should
1633 returns a face, or nil if does not want to specify a face and let
1634 the normal rules apply."
1635 :group 'org-agenda-line-format
1636 :type 'function)
1638 (defcustom org-agenda-category-icon-alist nil
1639 "Alist of category icon to be displayed in agenda views.
1641 Each entry should have the following format:
1643 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1645 Where CATEGORY-REGEXP is a regexp matching the categories where
1646 the icon should be displayed.
1647 FILE-OR-DATA either a file path or a string containing image data.
1649 The other fields can be omited safely if not needed:
1650 TYPE indicates the image type.
1651 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1652 image data.
1653 PROPS are additional image attributes to assign to the image,
1654 like, e.g. `:ascent center'.
1656 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1658 If you want to set the display properties yourself, just put a
1659 list as second element:
1661 (CATEGORY-REGEXP (MY PROPERTY LIST))
1663 For example, to display a 16px horizontal space for Emacs
1664 category, you can use:
1666 (\"Emacs\" '(space . (:width (16))))"
1667 :group 'org-agenda-line-format
1668 :type '(alist :key-type (string :tag "Regexp matching category")
1669 :value-type (choice (list :tag "Icon"
1670 (string :tag "File or data")
1671 (symbol :tag "Type")
1672 (boolean :tag "Data?")
1673 (repeat :tag "Extra image properties" :inline t symbol))
1674 (list :tag "Display properties" sexp))))
1676 (defgroup org-agenda-column-view nil
1677 "Options concerning column view in the agenda."
1678 :tag "Org Agenda Column View"
1679 :group 'org-agenda)
1681 (defcustom org-agenda-columns-show-summaries t
1682 "Non-nil means show summaries for columns displayed in the agenda view."
1683 :group 'org-agenda-column-view
1684 :type 'boolean)
1686 (defcustom org-agenda-columns-compute-summary-properties t
1687 "Non-nil means recompute all summary properties before column view.
1688 When column view in the agenda is listing properties that have a summary
1689 operator, it can go to all relevant buffers and recompute the summaries
1690 there. This can mean overhead for the agenda column view, but is necessary
1691 to have thing up to date.
1692 As a special case, a CLOCKSUM property also makes sure that the clock
1693 computations are current."
1694 :group 'org-agenda-column-view
1695 :type 'boolean)
1697 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1698 "Non-nil means the duration of an appointment will add to day effort.
1699 The property to which appointment durations will be added is the one given
1700 in the option `org-effort-property'. If an appointment does not have
1701 an end time, `org-agenda-default-appointment-duration' will be used. If that
1702 is not set, an appointment without end time will not contribute to the time
1703 estimate."
1704 :group 'org-agenda-column-view
1705 :type 'boolean)
1707 (defcustom org-agenda-auto-exclude-function nil
1708 "A function called with a tag to decide if it is filtered on '/ RET'.
1709 The sole argument to the function, which is called once for each
1710 possible tag, is a string giving the name of the tag. The
1711 function should return either nil if the tag should be included
1712 as normal, or \"-<TAG>\" to exclude the tag.
1713 Note that for the purpose of tag filtering, only the lower-case version of
1714 all tags will be considered, so that this function will only ever see
1715 the lower-case version of all tags."
1716 :group 'org-agenda
1717 :type 'function)
1719 (defcustom org-agenda-bulk-custom-functions nil
1720 "Alist of characters and custom functions for bulk actions.
1721 For example, this value makes those two functions available:
1723 '((?R set-category)
1724 (?C bulk-cut))
1726 With selected entries in an agenda buffer, `B R' will call
1727 the custom function `set-category' on the selected entries.
1728 Note that functions in this alist don't need to be quoted."
1729 :type 'alist
1730 :group 'org-agenda)
1732 (eval-when-compile
1733 (require 'cl))
1734 (require 'org)
1736 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1737 "Execute BODY with point at location given by `org-hd-marker' property.
1738 If STRING is non-nil, the text property will be fetched from position 0
1739 in that string. If STRING is nil, it will be fetched from the beginning
1740 of the current line."
1741 (org-with-gensyms (marker)
1742 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1743 'org-hd-marker string)))
1744 (with-current-buffer (marker-buffer ,marker)
1745 (save-excursion
1746 (goto-char ,marker)
1747 ,@body)))))
1748 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1750 (defun org-add-agenda-custom-command (entry)
1751 "Replace or add a command in `org-agenda-custom-commands'.
1752 This is mostly for hacking and trying a new command - once the command
1753 works you probably want to add it to `org-agenda-custom-commands' for good."
1754 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1755 (if ass
1756 (setcdr ass (cdr entry))
1757 (push entry org-agenda-custom-commands))))
1759 ;;; Define the Org-agenda-mode
1761 (defvar org-agenda-mode-map (make-sparse-keymap)
1762 "Keymap for `org-agenda-mode'.")
1763 (if (fboundp 'defvaralias)
1764 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1766 (defvar org-agenda-menu) ; defined later in this file.
1767 (defvar org-agenda-restrict) ; defined later in this file.
1768 (defvar org-agenda-follow-mode nil)
1769 (defvar org-agenda-entry-text-mode nil)
1770 (defvar org-agenda-clockreport-mode nil)
1771 (defvar org-agenda-show-log nil)
1772 (defvar org-agenda-redo-command nil)
1773 (defvar org-agenda-query-string nil)
1774 (defvar org-agenda-mode-hook nil
1775 "Hook for `org-agenda-mode', run after the mode is turned on.")
1776 (defvar org-agenda-type nil)
1777 (defvar org-agenda-force-single-file nil)
1778 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1780 (defun org-agenda-mode ()
1781 "Mode for time-sorted view on action items in Org-mode files.
1783 The following commands are available:
1785 \\{org-agenda-mode-map}"
1786 (interactive)
1787 (kill-all-local-variables)
1788 (setq org-agenda-undo-list nil
1789 org-agenda-pending-undo-list nil
1790 org-agenda-bulk-marked-entries nil)
1791 (setq major-mode 'org-agenda-mode)
1792 ;; Keep global-font-lock-mode from turning on font-lock-mode
1793 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1794 (setq mode-name "Org-Agenda")
1795 (use-local-map org-agenda-mode-map)
1796 (easy-menu-add org-agenda-menu)
1797 (if org-startup-truncated (setq truncate-lines t))
1798 (org-set-local 'line-move-visual nil)
1799 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1800 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1801 ;; Make sure properties are removed when copying text
1802 (when (boundp 'buffer-substring-filters)
1803 (org-set-local 'buffer-substring-filters
1804 (cons (lambda (x)
1805 (set-text-properties 0 (length x) nil x) x)
1806 buffer-substring-filters)))
1807 (unless org-agenda-keep-modes
1808 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1809 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1810 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1811 org-agenda-show-log org-agenda-start-with-log-mode))
1813 (easy-menu-change
1814 '("Agenda") "Agenda Files"
1815 (append
1816 (list
1817 (vector
1818 (if (get 'org-agenda-files 'org-restrict)
1819 "Restricted to single file"
1820 "Edit File List")
1821 '(org-edit-agenda-file-list)
1822 (not (get 'org-agenda-files 'org-restrict)))
1823 "--")
1824 (mapcar 'org-file-menu-entry (org-agenda-files))))
1825 (org-agenda-set-mode-name)
1826 (apply
1827 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1828 (list 'org-agenda-mode-hook)))
1830 (substitute-key-definition 'undo 'org-agenda-undo
1831 org-agenda-mode-map global-map)
1832 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1833 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1834 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1835 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1836 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1837 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1838 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1839 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1840 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1841 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1842 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1843 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1844 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1845 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1846 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1847 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1848 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1849 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1850 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1851 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1852 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1853 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1854 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1855 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1856 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1857 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1858 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1859 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1860 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1861 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1862 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1863 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1864 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1865 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1866 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1867 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1868 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1869 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1870 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1871 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1872 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1873 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1874 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1875 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1876 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1878 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1879 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1880 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1881 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1882 (while l (org-defkey org-agenda-mode-map
1883 (int-to-string (pop l)) 'digit-argument)))
1885 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1886 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1887 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1888 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1889 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1890 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1891 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1892 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1893 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1894 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1895 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1896 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1897 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1898 'org-clock-modify-effort-estimate)
1899 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1900 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1901 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1902 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1903 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1904 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1905 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1906 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1907 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1908 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1909 (substitute-key-definition 'next-line 'org-agenda-next-line
1910 org-agenda-mode-map global-map)
1911 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1912 org-agenda-mode-map global-map)
1913 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1914 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1915 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1916 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1917 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1918 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1919 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1920 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1921 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1922 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1923 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1924 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1925 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1926 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1927 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1928 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1929 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1930 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1931 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1932 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1933 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1934 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1935 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1936 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1937 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1938 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1939 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1940 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1941 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1942 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1944 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1945 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1946 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1947 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1948 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1949 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1950 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1951 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1952 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1953 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1955 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1956 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1957 (when org-agenda-mouse-1-follows-link
1958 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1959 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1960 '("Agenda"
1961 ("Agenda Files")
1962 "--"
1963 ("Agenda Dates"
1964 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1965 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1966 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1967 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1968 "--"
1969 ("View"
1970 ["Day View" org-agenda-day-view
1971 :active (org-agenda-check-type nil 'agenda)
1972 :style radio :selected (eq org-agenda-current-span 'day)
1973 :keys "v d (or just d)"]
1974 ["Week View" org-agenda-week-view
1975 :active (org-agenda-check-type nil 'agenda)
1976 :style radio :selected (eq org-agenda-current-span 'week)
1977 :keys "v w (or just w)"]
1978 ["Month View" org-agenda-month-view
1979 :active (org-agenda-check-type nil 'agenda)
1980 :style radio :selected (eq org-agenda-current-span 'month)
1981 :keys "v m"]
1982 ["Year View" org-agenda-year-view
1983 :active (org-agenda-check-type nil 'agenda)
1984 :style radio :selected (eq org-agenda-current-span 'year)
1985 :keys "v y"]
1986 "--"
1987 ["Include Diary" org-agenda-toggle-diary
1988 :style toggle :selected org-agenda-include-diary
1989 :active (org-agenda-check-type nil 'agenda)]
1990 ["Include Deadlines" org-agenda-toggle-deadlines
1991 :style toggle :selected org-agenda-include-deadlines
1992 :active (org-agenda-check-type nil 'agenda)]
1993 ["Use Time Grid" org-agenda-toggle-time-grid
1994 :style toggle :selected org-agenda-use-time-grid
1995 :active (org-agenda-check-type nil 'agenda)]
1996 "--"
1997 ["Show clock report" org-agenda-clockreport-mode
1998 :style toggle :selected org-agenda-clockreport-mode
1999 :active (org-agenda-check-type nil 'agenda)]
2000 ["Show some entry text" org-agenda-entry-text-mode
2001 :style toggle :selected org-agenda-entry-text-mode
2002 :active t]
2003 "--"
2004 ["Show Logbook entries" org-agenda-log-mode
2005 :style toggle :selected org-agenda-show-log
2006 :active (org-agenda-check-type nil 'agenda 'timeline)
2007 :keys "v l (or just l)"]
2008 ["Include archived trees" org-agenda-archives-mode
2009 :style toggle :selected org-agenda-archives-mode :active t
2010 :keys "v a"]
2011 ["Include archive files" (org-agenda-archives-mode t)
2012 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2013 :keys "v A"]
2014 "--"
2015 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2016 ["Write view to file" org-write-agenda t]
2017 ["Rebuild buffer" org-agenda-redo t]
2018 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2019 "--"
2020 ["Show original entry" org-agenda-show t]
2021 ["Go To (other window)" org-agenda-goto t]
2022 ["Go To (this window)" org-agenda-switch-to t]
2023 ["Follow Mode" org-agenda-follow-mode
2024 :style toggle :selected org-agenda-follow-mode :active t]
2025 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2026 "--"
2027 ("TODO"
2028 ["Cycle TODO" org-agenda-todo t]
2029 ["Next TODO set" org-agenda-todo-nextset t]
2030 ["Previous TODO set" org-agenda-todo-previousset t]
2031 ["Add note" org-agenda-add-note t])
2032 ("Archive/Refile/Delete"
2033 ["Archive default" org-agenda-archive-default t]
2034 ["Archive default" org-agenda-archive-default-with-confirmation t]
2035 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2036 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2037 ["Archive subtree" org-agenda-archive t]
2038 "--"
2039 ["Refile" org-agenda-refile t]
2040 "--"
2041 ["Delete subtree" org-agenda-kill t])
2042 ("Bulk action"
2043 ["Mark entry" org-agenda-bulk-mark t]
2044 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2045 ["Unmark entry" org-agenda-bulk-unmark t]
2046 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2047 ["Act on all marked" org-agenda-bulk-action t]
2048 "--"
2049 ("Tags and Properties"
2050 ["Show all Tags" org-agenda-show-tags t]
2051 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2052 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2053 "--"
2054 ["Column View" org-columns t])
2055 ("Deadline/Schedule"
2056 ["Schedule" org-agenda-schedule t]
2057 ["Set Deadline" org-agenda-deadline t]
2058 "--"
2059 ["Mark item" org-agenda-action :active t :keys "k m"]
2060 ["Show mark item" org-agenda-action :active t :keys "k v"]
2061 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2062 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2063 "--"
2064 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2065 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2066 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2067 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2068 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2069 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2070 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2071 ("Clock and Effort"
2072 ["Clock in" org-agenda-clock-in t]
2073 ["Clock out" org-agenda-clock-out t]
2074 ["Clock cancel" org-agenda-clock-cancel t]
2075 ["Goto running clock" org-clock-goto t]
2076 "--"
2077 ["Set Effort" org-agenda-set-effort t]
2078 ["Change clocked effort" org-clock-modify-effort-estimate
2079 (org-clock-is-active)])
2080 ("Priority"
2081 ["Set Priority" org-agenda-priority t]
2082 ["Increase Priority" org-agenda-priority-up t]
2083 ["Decrease Priority" org-agenda-priority-down t]
2084 ["Show Priority" org-agenda-show-priority t])
2085 ("Calendar/Diary"
2086 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2087 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2088 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2089 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2090 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2091 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2092 "--"
2093 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2094 "--"
2095 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2096 "--"
2097 ("MobileOrg"
2098 ["Push Files and Views" org-mobile-push t]
2099 ["Get Captured and Flagged" org-mobile-pull t]
2100 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2101 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2102 "--"
2103 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2104 "--"
2105 ["Quit" org-agenda-quit t]
2106 ["Exit and Release Buffers" org-agenda-exit t]
2109 ;;; Agenda undo
2111 (defvar org-agenda-allow-remote-undo t
2112 "Non-nil means allow remote undo from the agenda buffer.")
2113 (defvar org-agenda-undo-list nil
2114 "List of undoable operations in the agenda since last refresh.")
2115 (defvar org-agenda-undo-has-started-in nil
2116 "Buffers that have already seen `undo-start' in the current undo sequence.")
2117 (defvar org-agenda-pending-undo-list nil
2118 "In a series of undo commands, this is the list of remaining undo items.")
2120 (defun org-agenda-undo ()
2121 "Undo a remote editing step in the agenda.
2122 This undoes changes both in the agenda buffer and in the remote buffer
2123 that have been changed along."
2124 (interactive)
2125 (or org-agenda-allow-remote-undo
2126 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2127 (if (not (eq this-command last-command))
2128 (setq org-agenda-undo-has-started-in nil
2129 org-agenda-pending-undo-list org-agenda-undo-list))
2130 (if (not org-agenda-pending-undo-list)
2131 (error "No further undo information"))
2132 (let* ((entry (pop org-agenda-pending-undo-list))
2133 buf line cmd rembuf)
2134 (setq cmd (pop entry) line (pop entry))
2135 (setq rembuf (nth 2 entry))
2136 (org-with-remote-undo rembuf
2137 (while (bufferp (setq buf (pop entry)))
2138 (if (pop entry)
2139 (with-current-buffer buf
2140 (let ((last-undo-buffer buf)
2141 (inhibit-read-only t))
2142 (unless (memq buf org-agenda-undo-has-started-in)
2143 (push buf org-agenda-undo-has-started-in)
2144 (make-local-variable 'pending-undo-list)
2145 (undo-start))
2146 (while (and pending-undo-list
2147 (listp pending-undo-list)
2148 (not (car pending-undo-list)))
2149 (pop pending-undo-list))
2150 (undo-more 1))))))
2151 (org-goto-line line)
2152 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2154 (defun org-verify-change-for-undo (l1 l2)
2155 "Verify that a real change occurred between the undo lists L1 and L2."
2156 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2157 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2158 (not (eq l1 l2)))
2160 ;;; Agenda dispatch
2162 (defvar org-agenda-restrict nil)
2163 (defvar org-agenda-restrict-begin (make-marker))
2164 (defvar org-agenda-restrict-end (make-marker))
2165 (defvar org-agenda-last-dispatch-buffer nil)
2166 (defvar org-agenda-overriding-restriction nil)
2168 ;;;###autoload
2169 (defun org-agenda (&optional arg keys restriction)
2170 "Dispatch agenda commands to collect entries to the agenda buffer.
2171 Prompts for a command to execute. Any prefix arg will be passed
2172 on to the selected command. The default selections are:
2174 a Call `org-agenda-list' to display the agenda for current day or week.
2175 t Call `org-todo-list' to display the global todo list.
2176 T Call `org-todo-list' to display the global todo list, select only
2177 entries with a specific TODO keyword (the user gets a prompt).
2178 m Call `org-tags-view' to display headlines with tags matching
2179 a condition (the user is prompted for the condition).
2180 M Like `m', but select only TODO entries, no ordinary headlines.
2181 L Create a timeline for the current buffer.
2182 e Export views to associated files.
2183 s Search entries for keywords.
2184 / Multi occur across all agenda files and also files listed
2185 in `org-agenda-text-search-extra-files'.
2186 < Restrict agenda commands to buffer, subtree, or region.
2187 Press several times to get the desired effect.
2188 > Remove a previous restriction.
2189 # List \"stuck\" projects.
2190 ! Configure what \"stuck\" means.
2191 C Configure custom agenda commands.
2193 More commands can be added by configuring the variable
2194 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2195 searches can be pre-defined in this way.
2197 If the current buffer is in Org-mode and visiting a file, you can also
2198 first press `<' once to indicate that the agenda should be temporarily
2199 \(until the next use of \\[org-agenda]) restricted to the current file.
2200 Pressing `<' twice means to restrict to the current subtree or region
2201 \(if active)."
2202 (interactive "P")
2203 (catch 'exit
2204 (let* ((prefix-descriptions nil)
2205 (org-agenda-window-setup (if (equal (buffer-name)
2206 org-agenda-buffer-name)
2207 'current-window
2208 org-agenda-window-setup))
2209 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2210 (org-agenda-custom-commands
2211 ;; normalize different versions
2212 (delq nil
2213 (mapcar
2214 (lambda (x)
2215 (cond ((stringp (cdr x))
2216 (push x prefix-descriptions)
2217 nil)
2218 ((stringp (nth 1 x)) x)
2219 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2220 (t (cons (car x) (cons "" (cdr x))))))
2221 org-agenda-custom-commands)))
2222 (buf (current-buffer))
2223 (bfn (buffer-file-name (buffer-base-buffer)))
2224 entry key type match lprops ans)
2225 ;; Turn off restriction unless there is an overriding one,
2226 (unless org-agenda-overriding-restriction
2227 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2228 ;; There is a request to keep the file list in place
2229 (put 'org-agenda-files 'org-restrict nil))
2230 (setq org-agenda-restrict nil)
2231 (move-marker org-agenda-restrict-begin nil)
2232 (move-marker org-agenda-restrict-end nil))
2233 ;; Delete old local properties
2234 (put 'org-agenda-redo-command 'org-lprops nil)
2235 ;; Delete previously set last-arguments
2236 (put 'org-agenda-redo-command 'last-args nil)
2237 ;; Remember where this call originated
2238 (setq org-agenda-last-dispatch-buffer (current-buffer))
2239 (unless keys
2240 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2241 keys (car ans)
2242 restriction (cdr ans)))
2243 ;; Establish the restriction, if any
2244 (when (and (not org-agenda-overriding-restriction) restriction)
2245 (put 'org-agenda-files 'org-restrict (list bfn))
2246 (cond
2247 ((eq restriction 'region)
2248 (setq org-agenda-restrict t)
2249 (move-marker org-agenda-restrict-begin (region-beginning))
2250 (move-marker org-agenda-restrict-end (region-end)))
2251 ((eq restriction 'subtree)
2252 (save-excursion
2253 (setq org-agenda-restrict t)
2254 (org-back-to-heading t)
2255 (move-marker org-agenda-restrict-begin (point))
2256 (move-marker org-agenda-restrict-end
2257 (progn (org-end-of-subtree t)))))))
2259 ;; For example the todo list should not need it (but does...)
2260 (cond
2261 ((setq entry (assoc keys org-agenda-custom-commands))
2262 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2263 (progn
2264 (setq type (nth 2 entry) match (eval (nth 3 entry))
2265 lprops (nth 4 entry))
2266 (put 'org-agenda-redo-command 'org-lprops lprops)
2267 (cond
2268 ((eq type 'agenda)
2269 (org-let lprops '(org-agenda-list current-prefix-arg)))
2270 ((eq type 'alltodo)
2271 (org-let lprops '(org-todo-list current-prefix-arg)))
2272 ((eq type 'search)
2273 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2274 ((eq type 'stuck)
2275 (org-let lprops '(org-agenda-list-stuck-projects
2276 current-prefix-arg)))
2277 ((eq type 'tags)
2278 (org-let lprops '(org-tags-view current-prefix-arg match)))
2279 ((eq type 'tags-todo)
2280 (org-let lprops '(org-tags-view '(4) match)))
2281 ((eq type 'todo)
2282 (org-let lprops '(org-todo-list match)))
2283 ((eq type 'tags-tree)
2284 (org-check-for-org-mode)
2285 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2286 ((eq type 'todo-tree)
2287 (org-check-for-org-mode)
2288 (org-let lprops
2289 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2290 (regexp-quote match) "\\>"))))
2291 ((eq type 'occur-tree)
2292 (org-check-for-org-mode)
2293 (org-let lprops '(org-occur match)))
2294 ((functionp type)
2295 (org-let lprops '(funcall type match)))
2296 ((fboundp type)
2297 (org-let lprops '(funcall type match)))
2298 (t (error "Invalid custom agenda command type %s" type))))
2299 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2300 ((equal keys "C")
2301 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2302 (customize-variable 'org-agenda-custom-commands))
2303 ((equal keys "a") (call-interactively 'org-agenda-list))
2304 ((equal keys "s") (call-interactively 'org-search-view))
2305 ((equal keys "t") (call-interactively 'org-todo-list))
2306 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2307 ((equal keys "m") (call-interactively 'org-tags-view))
2308 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2309 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2310 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2311 (org-add-hook
2312 'post-command-hook
2313 (lambda ()
2314 (unless (current-message)
2315 (let* ((m (org-agenda-get-any-marker))
2316 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2317 (when note
2318 (message (concat
2319 "FLAGGING-NOTE ([?] for more info): "
2320 (org-add-props
2321 (replace-regexp-in-string
2322 "\\\\n" "//"
2323 (copy-sequence note))
2324 nil 'face 'org-warning)))))))
2325 t t))
2326 ((equal keys "L")
2327 (unless (eq major-mode 'org-mode)
2328 (error "This is not an Org-mode file"))
2329 (unless restriction
2330 (put 'org-agenda-files 'org-restrict (list bfn))
2331 (org-call-with-arg 'org-timeline arg)))
2332 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2333 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2334 ((equal keys "!") (customize-variable 'org-stuck-projects))
2335 (t (error "Invalid agenda key"))))))
2337 (defun org-agenda-append-agenda ()
2338 "Append another agenda view to the current one.
2339 This function allows interactive building of block agendas.
2340 Agenda views are separated by `org-agenda-block-separator'."
2341 (interactive)
2342 (unless (string= (buffer-name) org-agenda-buffer-name)
2343 (error "Can only append from within agenda buffer"))
2344 (let ((org-agenda-multi t))
2345 (org-agenda)
2346 (widen)))
2348 (defun org-agenda-normalize-custom-commands (cmds)
2349 (delq nil
2350 (mapcar
2351 (lambda (x)
2352 (cond ((stringp (cdr x)) nil)
2353 ((stringp (nth 1 x)) x)
2354 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2355 (t (cons (car x) (cons "" (cdr x))))))
2356 cmds)))
2358 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2359 "The user interface for selecting an agenda command."
2360 (catch 'exit
2361 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2362 (restrict-ok (and bfn (eq major-mode 'org-mode)))
2363 (region-p (org-region-active-p))
2364 (custom org-agenda-custom-commands)
2365 (selstring "")
2366 restriction second-time
2367 c entry key type match prefixes rmheader header-end custom1 desc
2368 line lines left right n n1)
2369 (save-window-excursion
2370 (delete-other-windows)
2371 (org-switch-to-buffer-other-window " *Agenda Commands*")
2372 (erase-buffer)
2373 (insert (eval-when-compile
2374 (let ((header
2376 Press key for an agenda command: < Buffer, subtree/region restriction
2377 -------------------------------- > Remove restriction
2378 a Agenda for current week or day e Export agenda views
2379 t List of all TODO entries T Entries with special TODO kwd
2380 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2381 L Timeline for current buffer # List stuck projects (!=configure)
2382 s Search for keywords C Configure custom agenda commands
2383 / Multi-occur ? Find :FLAGGED: entries
2385 (start 0))
2386 (while (string-match
2387 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2388 header start)
2389 (setq start (match-end 0))
2390 (add-text-properties (match-beginning 2) (match-end 2)
2391 '(face bold) header))
2392 header)))
2393 (setq header-end (move-marker (make-marker) (point)))
2394 (while t
2395 (setq custom1 custom)
2396 (when (eq rmheader t)
2397 (org-goto-line 1)
2398 (re-search-forward ":" nil t)
2399 (delete-region (match-end 0) (point-at-eol))
2400 (forward-char 1)
2401 (looking-at "-+")
2402 (delete-region (match-end 0) (point-at-eol))
2403 (move-marker header-end (match-end 0)))
2404 (goto-char header-end)
2405 (delete-region (point) (point-max))
2407 ;; Produce all the lines that describe custom commands and prefixes
2408 (setq lines nil)
2409 (while (setq entry (pop custom1))
2410 (setq key (car entry) desc (nth 1 entry)
2411 type (nth 2 entry)
2412 match (nth 3 entry))
2413 (if (> (length key) 1)
2414 (add-to-list 'prefixes (string-to-char key))
2415 (setq line
2416 (format
2417 "%-4s%-14s"
2418 (org-add-props (copy-sequence key)
2419 '(face bold))
2420 (cond
2421 ((string-match "\\S-" desc) desc)
2422 ((eq type 'agenda) "Agenda for current week or day")
2423 ((eq type 'alltodo) "List of all TODO entries")
2424 ((eq type 'search) "Word search")
2425 ((eq type 'stuck) "List of stuck projects")
2426 ((eq type 'todo) "TODO keyword")
2427 ((eq type 'tags) "Tags query")
2428 ((eq type 'tags-todo) "Tags (TODO)")
2429 ((eq type 'tags-tree) "Tags tree")
2430 ((eq type 'todo-tree) "TODO kwd tree")
2431 ((eq type 'occur-tree) "Occur tree")
2432 ((functionp type) (if (symbolp type)
2433 (symbol-name type)
2434 "Lambda expression"))
2435 (t "???"))))
2436 (if org-agenda-menu-show-matcher
2437 (setq line
2438 (concat line ": "
2439 (cond
2440 ((stringp match)
2441 (setq match (copy-sequence match))
2442 (org-add-props match nil 'face 'org-warning))
2443 (match
2444 (format "set of %d commands" (length match)))
2445 (t ""))))
2446 (if (org-string-nw-p match)
2447 (add-text-properties
2448 0 (length line) (list 'help-echo
2449 (concat "Matcher: "match)) line)))
2450 (push line lines)))
2451 (setq lines (nreverse lines))
2452 (when prefixes
2453 (mapc (lambda (x)
2454 (push
2455 (format "%s %s"
2456 (org-add-props (char-to-string x)
2457 nil 'face 'bold)
2458 (or (cdr (assoc (concat selstring
2459 (char-to-string x))
2460 prefix-descriptions))
2461 "Prefix key"))
2462 lines))
2463 prefixes))
2465 ;; Check if we should display in two columns
2466 (if org-agenda-menu-two-column
2467 (progn
2468 (setq n (length lines)
2469 n1 (+ (/ n 2) (mod n 2))
2470 right (nthcdr n1 lines)
2471 left (copy-sequence lines))
2472 (setcdr (nthcdr (1- n1) left) nil))
2473 (setq left lines right nil))
2474 (while left
2475 (insert "\n" (pop left))
2476 (when right
2477 (if (< (current-column) 40)
2478 (move-to-column 40 t)
2479 (insert " "))
2480 (insert (pop right))))
2482 ;; Make the window the right size
2483 (goto-char (point-min))
2484 (if second-time
2485 (if (not (pos-visible-in-window-p (point-max)))
2486 (org-fit-window-to-buffer))
2487 (setq second-time t)
2488 (org-fit-window-to-buffer))
2490 ;; Ask for selection
2491 (message "Press key for agenda command%s:"
2492 (if (or restrict-ok org-agenda-overriding-restriction)
2493 (if org-agenda-overriding-restriction
2494 " (restriction lock active)"
2495 (if restriction
2496 (format " (restricted to %s)" restriction)
2497 " (unrestricted)"))
2498 ""))
2499 (setq c (read-char-exclusive))
2500 (message "")
2501 (cond
2502 ((assoc (char-to-string c) custom)
2503 (setq selstring (concat selstring (char-to-string c)))
2504 (throw 'exit (cons selstring restriction)))
2505 ((memq c prefixes)
2506 (setq selstring (concat selstring (char-to-string c))
2507 prefixes nil
2508 rmheader (or rmheader t)
2509 custom (delq nil (mapcar
2510 (lambda (x)
2511 (if (or (= (length (car x)) 1)
2512 (/= (string-to-char (car x)) c))
2514 (cons (substring (car x) 1) (cdr x))))
2515 custom))))
2516 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2517 (message "Restriction is only possible in Org-mode buffers")
2518 (ding) (sit-for 1))
2519 ((eq c ?1)
2520 (org-agenda-remove-restriction-lock 'noupdate)
2521 (setq restriction 'buffer))
2522 ((eq c ?0)
2523 (org-agenda-remove-restriction-lock 'noupdate)
2524 (setq restriction (if region-p 'region 'subtree)))
2525 ((eq c ?<)
2526 (org-agenda-remove-restriction-lock 'noupdate)
2527 (setq restriction
2528 (cond
2529 ((eq restriction 'buffer)
2530 (if region-p 'region 'subtree))
2531 ((memq restriction '(subtree region))
2532 nil)
2533 (t 'buffer))))
2534 ((eq c ?>)
2535 (org-agenda-remove-restriction-lock 'noupdate)
2536 (setq restriction nil))
2537 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2538 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2539 ((and (> (length selstring) 0) (eq c ?\d))
2540 (delete-window)
2541 (org-agenda-get-restriction-and-command prefix-descriptions))
2543 ((equal c ?q) (error "Abort"))
2544 (t (error "Invalid key %c" c))))))))
2546 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2547 (defvar org-agenda-last-arguments nil
2548 "The arguments of the previous call to `org-agenda'.")
2549 (defun org-agenda-run-series (name series)
2550 (org-let (nth 1 series) '(org-prepare-agenda name))
2551 (let* ((org-agenda-multi t)
2552 (redo (list 'org-agenda-run-series name (list 'quote series)))
2553 (org-agenda-overriding-arguments
2554 (or org-agenda-overriding-arguments
2555 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2556 (get 'org-agenda-redo-command 'last-args))))
2557 (cmds (car series))
2558 (gprops (nth 1 series))
2559 match ;; The byte compiler incorrectly complains about this. Keep it!
2560 cmd type lprops)
2561 (while (setq cmd (pop cmds))
2562 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2563 (cond
2564 ((eq type 'agenda)
2565 (org-let2 gprops lprops
2566 '(call-interactively 'org-agenda-list)))
2567 ((eq type 'alltodo)
2568 (org-let2 gprops lprops
2569 '(call-interactively 'org-todo-list)))
2570 ((eq type 'search)
2571 (org-let2 gprops lprops
2572 '(org-search-view current-prefix-arg match nil)))
2573 ((eq type 'stuck)
2574 (org-let2 gprops lprops
2575 '(call-interactively 'org-agenda-list-stuck-projects)))
2576 ((eq type 'tags)
2577 (org-let2 gprops lprops
2578 '(org-tags-view current-prefix-arg match)))
2579 ((eq type 'tags-todo)
2580 (org-let2 gprops lprops
2581 '(org-tags-view '(4) match)))
2582 ((eq type 'todo)
2583 (org-let2 gprops lprops
2584 '(org-todo-list match)))
2585 ((fboundp type)
2586 (org-let2 gprops lprops
2587 '(funcall type match)))
2588 (t (error "Invalid type in command series"))))
2589 (widen)
2590 (setq org-agenda-redo-command redo)
2591 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2592 (goto-char (point-min)))
2593 (org-fit-agenda-window)
2594 (org-let (nth 1 series) '(org-finalize-agenda)))
2596 ;;;###autoload
2597 (defmacro org-batch-agenda (cmd-key &rest parameters)
2598 "Run an agenda command in batch mode and send the result to STDOUT.
2599 If CMD-KEY is a string of length 1, it is used as a key in
2600 `org-agenda-custom-commands' and triggers this command. If it is a
2601 longer string it is used as a tags/todo match string.
2602 Parameters are alternating variable names and values that will be bound
2603 before running the agenda command."
2604 (org-eval-in-environment (org-make-parameter-alist parameters)
2605 (if (> (length cmd-key) 2)
2606 (org-tags-view nil cmd-key)
2607 (org-agenda nil cmd-key)))
2608 (set-buffer org-agenda-buffer-name)
2609 (princ (org-encode-for-stdout (buffer-string))))
2610 (def-edebug-spec org-batch-agenda (form &rest sexp))
2612 ;(defun org-encode-for-stdout (string)
2613 ; (if (fboundp 'encode-coding-string)
2614 ; (encode-coding-string string buffer-file-coding-system)
2615 ; string))
2617 (defun org-encode-for-stdout (string)
2618 string)
2620 (defvar org-agenda-info nil)
2622 ;;;###autoload
2623 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2624 "Run an agenda command in batch mode and send the result to STDOUT.
2625 If CMD-KEY is a string of length 1, it is used as a key in
2626 `org-agenda-custom-commands' and triggers this command. If it is a
2627 longer string it is used as a tags/todo match string.
2628 Parameters are alternating variable names and values that will be bound
2629 before running the agenda command.
2631 The output gives a line for each selected agenda item. Each
2632 item is a list of comma-separated values, like this:
2634 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2636 category The category of the item
2637 head The headline, without TODO kwd, TAGS and PRIORITY
2638 type The type of the agenda entry, can be
2639 todo selected in TODO match
2640 tagsmatch selected in tags match
2641 diary imported from diary
2642 deadline a deadline on given date
2643 scheduled scheduled on given date
2644 timestamp entry has timestamp on given date
2645 closed entry was closed on given date
2646 upcoming-deadline warning about deadline
2647 past-scheduled forwarded scheduled item
2648 block entry has date block including g. date
2649 todo The todo keyword, if any
2650 tags All tags including inherited ones, separated by colons
2651 date The relevant date, like 2007-2-14
2652 time The time, like 15:00-16:50
2653 extra Sting with extra planning info
2654 priority-l The priority letter if any was given
2655 priority-n The computed numerical priority
2656 agenda-day The day in the agenda where this is listed"
2657 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2658 (org-make-parameter-alist parameters))
2659 (if (> (length cmd-key) 2)
2660 (org-tags-view nil cmd-key)
2661 (org-agenda nil cmd-key)))
2662 (set-buffer org-agenda-buffer-name)
2663 (let* ((lines (org-split-string (buffer-string) "\n"))
2664 line)
2665 (while (setq line (pop lines))
2666 (catch 'next
2667 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2668 (setq org-agenda-info
2669 (org-fix-agenda-info (text-properties-at 0 line)))
2670 (princ
2671 (org-encode-for-stdout
2672 (mapconcat 'org-agenda-export-csv-mapper
2673 '(org-category txt type todo tags date time extra
2674 priority-letter priority agenda-day)
2675 ",")))
2676 (princ "\n")))))
2677 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2679 (defun org-fix-agenda-info (props)
2680 "Make sure all properties on an agenda item have a canonical form.
2681 This ensures the export commands can easily use it."
2682 (let (tmp re)
2683 (when (setq tmp (plist-get props 'tags))
2684 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2685 (when (setq tmp (plist-get props 'date))
2686 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2687 (let ((calendar-date-display-form '(year "-" month "-" day)))
2688 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2690 (setq tmp (calendar-date-string tmp)))
2691 (setq props (plist-put props 'date tmp)))
2692 (when (setq tmp (plist-get props 'day))
2693 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2694 (let ((calendar-date-display-form '(year "-" month "-" day)))
2695 (setq tmp (calendar-date-string tmp)))
2696 (setq props (plist-put props 'day tmp))
2697 (setq props (plist-put props 'agenda-day tmp)))
2698 (when (setq tmp (plist-get props 'txt))
2699 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2700 (plist-put props 'priority-letter (match-string 1 tmp))
2701 (setq tmp (replace-match "" t t tmp)))
2702 (when (and (setq re (plist-get props 'org-todo-regexp))
2703 (setq re (concat "\\`\\.*" re " ?"))
2704 (string-match re tmp))
2705 (plist-put props 'todo (match-string 1 tmp))
2706 (setq tmp (replace-match "" t t tmp)))
2707 (plist-put props 'txt tmp)))
2708 props)
2710 (defun org-agenda-export-csv-mapper (prop)
2711 (let ((res (plist-get org-agenda-info prop)))
2712 (setq res
2713 (cond
2714 ((not res) "")
2715 ((stringp res) res)
2716 (t (prin1-to-string res))))
2717 (while (string-match "," res)
2718 (setq res (replace-match ";" t t res)))
2719 (org-trim res)))
2722 ;;;###autoload
2723 (defun org-store-agenda-views (&rest parameters)
2724 (interactive)
2725 (eval (list 'org-batch-store-agenda-views)))
2727 ;;;###autoload
2728 (defmacro org-batch-store-agenda-views (&rest parameters)
2729 "Run all custom agenda commands that have a file argument."
2730 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2731 (pop-up-frames nil)
2732 (dir default-directory)
2733 (pars (org-make-parameter-alist parameters))
2734 cmd thiscmdkey files opts cmd-or-set)
2735 (save-window-excursion
2736 (while cmds
2737 (setq cmd (pop cmds)
2738 thiscmdkey (car cmd)
2739 cmd-or-set (nth 2 cmd)
2740 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2741 files (nth (if (listp cmd-or-set) 4 5) cmd))
2742 (if (stringp files) (setq files (list files)))
2743 (when files
2744 (org-eval-in-environment (append org-agenda-exporter-settings
2745 opts pars)
2746 (org-agenda nil thiscmdkey))
2747 (set-buffer org-agenda-buffer-name)
2748 (while files
2749 (org-eval-in-environment (append org-agenda-exporter-settings
2750 opts pars)
2751 (org-write-agenda (expand-file-name (pop files) dir) nil t)))
2752 (and (get-buffer org-agenda-buffer-name)
2753 (kill-buffer org-agenda-buffer-name)))))))
2754 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2756 (defun org-agenda-mark-header-line (pos)
2757 "Mark the line at POS as an agenda structure header."
2758 (save-excursion
2759 (goto-char pos)
2760 (put-text-property (point-at-bol) (point-at-eol)
2761 'org-agenda-structural-header t)
2762 (when org-agenda-title-append
2763 (put-text-property (point-at-bol) (point-at-eol)
2764 'org-agenda-title-append org-agenda-title-append))))
2766 (defvar org-mobile-creating-agendas)
2767 (defun org-write-agenda (file &optional open nosettings)
2768 "Write the current buffer (an agenda view) as a file.
2769 Depending on the extension of the file name, plain text (.txt),
2770 HTML (.html or .htm) or Postscript (.ps) is produced.
2771 If the extension is .ics, run icalendar export over all files used
2772 to construct the agenda and limit the export to entries listed in the
2773 agenda now.
2774 With prefix argument OPEN, open the new file immediately.
2775 If NOSETTINGS is given, do not scope the settings of
2776 `org-agenda-exporter-settings' into the export commands. This is used when
2777 the settings have already been scoped and we do not wish to overrule other,
2778 higher priority settings."
2779 (interactive "FWrite agenda to file: \nP")
2780 (if (not (file-writable-p file))
2781 (error "Cannot write agenda to file %s" file))
2782 (org-let (if nosettings nil org-agenda-exporter-settings)
2783 '(save-excursion
2784 (save-window-excursion
2785 (org-agenda-mark-filtered-text)
2786 (let ((bs (copy-sequence (buffer-string))) beg)
2787 (org-agenda-unmark-filtered-text)
2788 (with-temp-buffer
2789 (rename-buffer "Agenda View" t)
2790 (set-buffer-modified-p nil)
2791 (insert bs)
2792 (org-agenda-remove-marked-text 'org-filtered)
2793 (while (setq beg (text-property-any (point-min) (point-max)
2794 'org-filtered t))
2795 (delete-region
2796 beg (or (next-single-property-change beg 'org-filtered)
2797 (point-max))))
2798 (run-hooks 'org-agenda-before-write-hook)
2799 (cond
2800 ((org-bound-and-true-p org-mobile-creating-agendas)
2801 (org-mobile-write-agenda-for-mobile file))
2802 ((string-match "\\.html?\\'" file)
2803 (require 'htmlize)
2804 (set-buffer (htmlize-buffer (current-buffer)))
2806 (when (and org-agenda-export-html-style
2807 (string-match "<style>" org-agenda-export-html-style))
2808 ;; replace <style> section with org-agenda-export-html-style
2809 (goto-char (point-min))
2810 (kill-region (- (search-forward "<style") 6)
2811 (search-forward "</style>"))
2812 (insert org-agenda-export-html-style))
2813 (write-file file)
2814 (kill-buffer (current-buffer))
2815 (message "HTML written to %s" file))
2816 ((string-match "\\.ps\\'" file)
2817 (require 'ps-print)
2818 (ps-print-buffer-with-faces file)
2819 (message "Postscript written to %s" file))
2820 ((string-match "\\.pdf\\'" file)
2821 (require 'ps-print)
2822 (ps-print-buffer-with-faces
2823 (concat (file-name-sans-extension file) ".ps"))
2824 (call-process "ps2pdf" nil nil nil
2825 (expand-file-name
2826 (concat (file-name-sans-extension file) ".ps"))
2827 (expand-file-name file))
2828 (delete-file (concat (file-name-sans-extension file) ".ps"))
2829 (message "PDF written to %s" file))
2830 ((string-match "\\.ics\\'" file)
2831 (require 'org-icalendar)
2832 (let ((org-agenda-marker-table
2833 (org-create-marker-find-array
2834 (org-agenda-collect-markers)))
2835 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2836 (org-combined-agenda-icalendar-file file))
2837 (apply 'org-export-icalendar 'combine
2838 (org-agenda-files nil 'ifmode))))
2840 (let ((bs (buffer-string)))
2841 (find-file file)
2842 (erase-buffer)
2843 (insert bs)
2844 (save-buffer 0)
2845 (kill-buffer (current-buffer))
2846 (message "Plain text written to %s" file))))))))
2847 (set-buffer org-agenda-buffer-name))
2848 (when open (org-open-file file)))
2850 (defvar org-agenda-filter-overlays nil)
2852 (defun org-agenda-mark-filtered-text ()
2853 "Mark all text hidden by filtering with a text property."
2854 (let ((inhibit-read-only t))
2855 (mapc
2856 (lambda (o)
2857 (when (equal (overlay-buffer o) (current-buffer))
2858 (put-text-property
2859 (overlay-start o) (overlay-end o)
2860 'org-filtered t)))
2861 org-agenda-filter-overlays)))
2863 (defun org-agenda-unmark-filtered-text ()
2864 "Remove the filtering text property."
2865 (let ((inhibit-read-only t))
2866 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2868 (defun org-agenda-remove-marked-text (property &optional value)
2869 "Delete all text marked with VALUE of PROPERTY.
2870 VALUE defaults to t."
2871 (let (beg)
2872 (setq value (or value t))
2873 (while (setq beg (text-property-any (point-min) (point-max)
2874 property value))
2875 (delete-region
2876 beg (or (next-single-property-change beg 'org-filtered)
2877 (point-max))))))
2879 (defun org-agenda-add-entry-text ()
2880 "Add entry text to agenda lines.
2881 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2882 entry text following headings shown in the agenda.
2883 Drawers will be excluded, also the line with scheduling/deadline info."
2884 (when (and (> org-agenda-add-entry-text-maxlines 0)
2885 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2886 (let (m txt)
2887 (goto-char (point-min))
2888 (while (not (eobp))
2889 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2890 (beginning-of-line 2)
2891 (setq txt (org-agenda-get-some-entry-text
2892 m org-agenda-add-entry-text-maxlines " > "))
2893 (end-of-line 1)
2894 (if (string-match "\\S-" txt)
2895 (insert "\n" txt)
2896 (or (eobp) (forward-char 1))))))))
2898 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2899 &rest keep)
2900 "Extract entry text from MARKER, at most N-LINES lines.
2901 This will ignore drawers etc, just get the text.
2902 If INDENT is given, prefix every line with this string. If KEEP is
2903 given, it is a list of symbols, defining stuff that should not be
2904 removed from the entry content. Currently only `planning' is allowed here."
2905 (let (txt drawer-re kwd-time-re ind)
2906 (save-excursion
2907 (with-current-buffer (marker-buffer marker)
2908 (if (not (eq major-mode 'org-mode))
2909 (setq txt "")
2910 (save-excursion
2911 (save-restriction
2912 (widen)
2913 (goto-char marker)
2914 (end-of-line 1)
2915 (setq txt (buffer-substring
2916 (min (1+ (point)) (point-max))
2917 (progn (outline-next-heading) (point)))
2918 drawer-re org-drawer-regexp
2919 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2920 ".*\n?"))
2921 (with-temp-buffer
2922 (insert txt)
2923 (when org-agenda-add-entry-text-descriptive-links
2924 (goto-char (point-min))
2925 (while (org-activate-bracket-links (point-max))
2926 (add-text-properties (match-beginning 0) (match-end 0)
2927 '(face org-link))))
2928 (goto-char (point-min))
2929 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2930 (set-text-properties (match-beginning 0) (match-end 0)
2931 nil))
2932 (goto-char (point-min))
2933 (while (re-search-forward drawer-re nil t)
2934 (delete-region
2935 (match-beginning 0)
2936 (progn (re-search-forward
2937 "^[ \t]*:END:.*\n?" nil 'move)
2938 (point))))
2939 (unless (member 'planning keep)
2940 (goto-char (point-min))
2941 (while (re-search-forward kwd-time-re nil t)
2942 (replace-match "")))
2943 (goto-char (point-min))
2944 (when org-agenda-entry-text-exclude-regexps
2945 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2946 (while (setq re (pop re-list))
2947 (goto-char (point-min))
2948 (while (re-search-forward re nil t)
2949 (replace-match "")))))
2950 (goto-char (point-max))
2951 (skip-chars-backward " \t\n")
2952 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2954 ;; find and remove min common indentation
2955 (goto-char (point-min))
2956 (untabify (point-min) (point-max))
2957 (setq ind (org-get-indentation))
2958 (while (not (eobp))
2959 (unless (looking-at "[ \t]*$")
2960 (setq ind (min ind (org-get-indentation))))
2961 (beginning-of-line 2))
2962 (goto-char (point-min))
2963 (while (not (eobp))
2964 (unless (looking-at "[ \t]*$")
2965 (move-to-column ind)
2966 (delete-region (point-at-bol) (point)))
2967 (beginning-of-line 2))
2969 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2971 (goto-char (point-min))
2972 (when indent
2973 (while (and (not (eobp)) (re-search-forward "^" nil t))
2974 (replace-match indent t t)))
2975 (goto-char (point-min))
2976 (while (looking-at "[ \t]*\n") (replace-match ""))
2977 (goto-char (point-max))
2978 (when (> (org-current-line)
2979 n-lines)
2980 (org-goto-line (1+ n-lines))
2981 (backward-char 1))
2982 (setq txt (buffer-substring (point-min) (point)))))))))
2983 txt))
2985 (defun org-agenda-collect-markers ()
2986 "Collect the markers pointing to entries in the agenda buffer."
2987 (let (m markers)
2988 (save-excursion
2989 (goto-char (point-min))
2990 (while (not (eobp))
2991 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2992 (org-get-at-bol 'org-marker)))
2993 (push m markers))
2994 (beginning-of-line 2)))
2995 (nreverse markers)))
2997 (defun org-create-marker-find-array (marker-list)
2998 "Create a alist of files names with all marker positions in that file."
2999 (let (f tbl m a p)
3000 (while (setq m (pop marker-list))
3001 (setq p (marker-position m)
3002 f (buffer-file-name (or (buffer-base-buffer
3003 (marker-buffer m))
3004 (marker-buffer m))))
3005 (if (setq a (assoc f tbl))
3006 (push (marker-position m) (cdr a))
3007 (push (list f p) tbl)))
3008 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3009 tbl)))
3011 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3012 (defun org-check-agenda-marker-table ()
3013 "Check of the current entry is on the marker list."
3014 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3016 (and (setq a (assoc file org-agenda-marker-table))
3017 (save-match-data
3018 (save-excursion
3019 (org-back-to-heading t)
3020 (member (point) (cdr a)))))))
3022 (defun org-check-for-org-mode ()
3023 "Make sure current buffer is in org-mode. Error if not."
3024 (or (eq major-mode 'org-mode)
3025 (error "Cannot execute org-mode agenda command on buffer in %s"
3026 major-mode)))
3028 (defun org-fit-agenda-window ()
3029 "Fit the window to the buffer size."
3030 (and (memq org-agenda-window-setup '(reorganize-frame))
3031 (fboundp 'fit-window-to-buffer)
3032 (org-fit-window-to-buffer
3034 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3035 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3037 ;;; Agenda prepare and finalize
3039 (defvar org-agenda-multi nil) ; dynamically scoped
3040 (defvar org-agenda-buffer-name "*Org Agenda*")
3041 (defvar org-pre-agenda-window-conf nil)
3042 (defvar org-agenda-columns-active nil)
3043 (defvar org-agenda-name nil)
3044 (defvar org-agenda-filter nil)
3045 (defvar org-agenda-filter-while-redo nil)
3046 (defvar org-agenda-filter-preset nil
3047 "A preset of the tags filter used for secondary agenda filtering.
3048 This must be a list of strings, each string must be a single tag preceded
3049 by \"+\" or \"-\".
3050 This variable should not be set directly, but agenda custom commands can
3051 bind it in the options section. The preset filter is a global property of
3052 the entire agenda view. In a block agenda, it will not work reliably to
3053 define a filter for one of the individual blocks. You need to set it in
3054 the global options and expect it to be applied to the entire view.")
3056 (defun org-prepare-agenda (&optional name)
3057 (setq org-todo-keywords-for-agenda nil)
3058 (setq org-done-keywords-for-agenda nil)
3059 (setq org-drawers-for-agenda nil)
3060 (unless org-agenda-persistent-filter
3061 (setq org-agenda-filter nil))
3062 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3063 (if org-agenda-multi
3064 (progn
3065 (setq buffer-read-only nil)
3066 (goto-char (point-max))
3067 (unless (or (bobp) org-agenda-compact-blocks
3068 (not org-agenda-block-separator))
3069 (insert "\n"
3070 (if (stringp org-agenda-block-separator)
3071 org-agenda-block-separator
3072 (make-string (window-width) org-agenda-block-separator))
3073 "\n"))
3074 (narrow-to-region (point) (point-max)))
3075 (org-agenda-reset-markers)
3076 (setq org-agenda-contributing-files nil)
3077 (setq org-agenda-columns-active nil)
3078 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3079 (setq org-todo-keywords-for-agenda
3080 (org-uniquify org-todo-keywords-for-agenda))
3081 (setq org-done-keywords-for-agenda
3082 (org-uniquify org-done-keywords-for-agenda))
3083 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3084 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3085 (awin (get-buffer-window abuf)))
3086 (cond
3087 ((equal (current-buffer) abuf) nil)
3088 (awin (select-window awin))
3089 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3090 ((equal org-agenda-window-setup 'current-window)
3091 (org-pop-to-buffer-same-window abuf))
3092 ((equal org-agenda-window-setup 'other-window)
3093 (org-switch-to-buffer-other-window abuf))
3094 ((equal org-agenda-window-setup 'other-frame)
3095 (switch-to-buffer-other-frame abuf))
3096 ((equal org-agenda-window-setup 'reorganize-frame)
3097 (delete-other-windows)
3098 (org-switch-to-buffer-other-window abuf)))
3099 ;; additional test in case agenda is invoked from within agenda
3100 ;; buffer via elisp link
3101 (unless (equal (current-buffer) abuf)
3102 (org-pop-to-buffer-same-window abuf)))
3103 (setq buffer-read-only nil)
3104 (let ((inhibit-read-only t)) (erase-buffer))
3105 (org-agenda-mode)
3106 (and name (not org-agenda-name)
3107 (org-set-local 'org-agenda-name name)))
3108 (setq buffer-read-only nil))
3110 (defun org-finalize-agenda ()
3111 "Finishing touch for the agenda buffer, called just before displaying it."
3112 (unless org-agenda-multi
3113 (save-excursion
3114 (let ((inhibit-read-only t))
3115 (goto-char (point-min))
3116 (while (org-activate-bracket-links (point-max))
3117 (add-text-properties (match-beginning 0) (match-end 0)
3118 '(face org-link)))
3119 (org-agenda-align-tags)
3120 (unless org-agenda-with-colors
3121 (remove-text-properties (point-min) (point-max) '(face nil))))
3122 (if (and (boundp 'org-agenda-overriding-columns-format)
3123 org-agenda-overriding-columns-format)
3124 (org-set-local 'org-agenda-overriding-columns-format
3125 org-agenda-overriding-columns-format))
3126 (if (and (boundp 'org-agenda-view-columns-initially)
3127 org-agenda-view-columns-initially)
3128 (org-agenda-columns))
3129 (when org-agenda-fontify-priorities
3130 (org-agenda-fontify-priorities))
3131 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3132 (org-agenda-dim-blocked-tasks))
3133 (org-agenda-mark-clocking-task)
3134 (when org-agenda-entry-text-mode
3135 (org-agenda-entry-text-hide)
3136 (org-agenda-entry-text-show))
3137 (if (functionp 'org-habit-insert-consistency-graphs)
3138 (org-habit-insert-consistency-graphs))
3139 (run-hooks 'org-finalize-agenda-hook)
3140 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3141 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3142 (org-agenda-filter-apply org-agenda-filter))
3145 (defun org-agenda-mark-clocking-task ()
3146 "Mark the current clock entry in the agenda if it is present."
3147 (mapc (lambda (o)
3148 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3149 (delete-overlay o)))
3150 (overlays-in (point-min) (point-max)))
3151 (when (marker-buffer org-clock-hd-marker)
3152 (save-excursion
3153 (goto-char (point-min))
3154 (let (s ov)
3155 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3156 (goto-char s)
3157 (when (equal (org-get-at-bol 'org-hd-marker)
3158 org-clock-hd-marker)
3159 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3160 (overlay-put ov 'type 'org-agenda-clocking)
3161 (overlay-put ov 'face 'org-agenda-clocking)
3162 (overlay-put ov 'help-echo
3163 "The clock is running in this item")))))))
3165 (defun org-agenda-fontify-priorities ()
3166 "Make highest priority lines bold, and lowest italic."
3167 (interactive)
3168 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3169 (delete-overlay o)))
3170 (overlays-in (point-min) (point-max)))
3171 (save-excursion
3172 (let ((inhibit-read-only t)
3173 b e p ov h l)
3174 (goto-char (point-min))
3175 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3176 (setq h (or (get-char-property (point) 'org-highest-priority)
3177 org-highest-priority)
3178 l (or (get-char-property (point) 'org-lowest-priority)
3179 org-lowest-priority)
3180 p (string-to-char (match-string 1))
3181 b (match-beginning 0)
3182 e (if (eq org-agenda-fontify-priorities 'cookies)
3183 (match-end 0)
3184 (point-at-eol))
3185 ov (make-overlay b e))
3186 (overlay-put
3187 ov 'face
3188 (cond ((org-face-from-face-or-color
3189 'priority nil
3190 (cdr (assoc p org-priority-faces))))
3191 ((and (listp org-agenda-fontify-priorities)
3192 (org-face-from-face-or-color
3193 'priority nil
3194 (cdr (assoc p org-agenda-fontify-priorities)))))
3195 ((equal p l) 'italic)
3196 ((equal p h) 'bold)))
3197 (overlay-put ov 'org-type 'org-priority)))))
3199 (defun org-agenda-dim-blocked-tasks ()
3200 "Dim currently blocked TODO's in the agenda display."
3201 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3202 (delete-overlay o)))
3203 (overlays-in (point-min) (point-max)))
3204 (save-excursion
3205 (let ((inhibit-read-only t)
3206 (org-depend-tag-blocked nil)
3207 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3208 org-blocked-by-checkboxes
3209 invis1 b e p ov h l)
3210 (goto-char (point-min))
3211 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3212 (and pos (goto-char (1+ pos))))
3213 (setq org-blocked-by-checkboxes nil invis1 invis)
3214 (let ((marker (org-get-at-bol 'org-hd-marker)))
3215 (when (and marker
3216 (with-current-buffer (marker-buffer marker)
3217 (save-excursion (goto-char marker)
3218 (org-entry-blocked-p))))
3219 (if org-blocked-by-checkboxes (setq invis1 nil))
3220 (setq b (if invis1
3221 (max (point-min) (1- (point-at-bol)))
3222 (point-at-bol))
3223 e (point-at-eol)
3224 ov (make-overlay b e))
3225 (if invis1
3226 (overlay-put ov 'invisible t)
3227 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3228 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3230 (defvar org-agenda-skip-function nil
3231 "Function to be called at each match during agenda construction.
3232 If this function returns nil, the current match should not be skipped.
3233 Otherwise, the function must return a position from where the search
3234 should be continued.
3235 This may also be a Lisp form, it will be evaluated.
3236 Never set this variable using `setq' or so, because then it will apply
3237 to all future agenda commands. If you do want a global skipping condition,
3238 use the option `org-agenda-skip-function-global' instead.
3239 The correct usage for `org-agenda-skip-function' is to bind it with
3240 `let' to scope it dynamically into the agenda-constructing command.
3241 A good way to set it is through options in `org-agenda-custom-commands'.")
3243 (defun org-agenda-skip ()
3244 "Throw to `:skip' in places that should be skipped.
3245 Also moves point to the end of the skipped region, so that search can
3246 continue from there."
3247 (let ((p (point-at-bol)) to)
3248 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3249 (get-text-property p :org-archived)
3250 (org-end-of-subtree t)
3251 (throw :skip t))
3252 (and org-agenda-skip-comment-trees
3253 (get-text-property p :org-comment)
3254 (org-end-of-subtree t)
3255 (throw :skip t))
3256 (if (equal (char-after p) ?#) (throw :skip t))
3257 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3258 (org-agenda-skip-eval org-agenda-skip-function)))
3259 (goto-char to)
3260 (throw :skip t))))
3262 (defun org-agenda-skip-eval (form)
3263 "If FORM is a function or a list, call (or eval) is and return result.
3264 `save-excursion' and `save-match-data' are wrapped around the call, so point
3265 and match data are returned to the previous state no matter what these
3266 functions do."
3267 (let (fp)
3268 (and form
3269 (or (setq fp (functionp form))
3270 (consp form))
3271 (save-excursion
3272 (save-match-data
3273 (if fp
3274 (funcall form)
3275 (eval form)))))))
3277 (defvar org-agenda-markers nil
3278 "List of all currently active markers created by `org-agenda'.")
3279 (defvar org-agenda-last-marker-time (org-float-time)
3280 "Creation time of the last agenda marker.")
3282 (defun org-agenda-new-marker (&optional pos)
3283 "Return a new agenda marker.
3284 Org-mode keeps a list of these markers and resets them when they are
3285 no longer in use."
3286 (let ((m (copy-marker (or pos (point)))))
3287 (setq org-agenda-last-marker-time (org-float-time))
3288 (push m org-agenda-markers)
3291 (defun org-agenda-reset-markers ()
3292 "Reset markers created by `org-agenda'."
3293 (while org-agenda-markers
3294 (move-marker (pop org-agenda-markers) nil)))
3296 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3297 "Save relative positions of markers in region."
3298 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3299 org-agenda-markers))
3301 ;;; Entry text mode
3303 (defun org-agenda-entry-text-show-here ()
3304 "Add some text from the entry as context to the current line."
3305 (let (m txt o)
3306 (setq m (org-get-at-bol 'org-hd-marker))
3307 (unless (marker-buffer m)
3308 (error "No marker points to an entry here"))
3309 (setq txt (concat "\n" (org-no-properties
3310 (org-agenda-get-some-entry-text
3311 m org-agenda-entry-text-maxlines " > "))))
3312 (when (string-match "\\S-" txt)
3313 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3314 (overlay-put o 'evaporate t)
3315 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3316 (overlay-put o 'after-string txt))))
3318 (defun org-agenda-entry-text-show ()
3319 "Add entry context for all agenda lines."
3320 (interactive)
3321 (save-excursion
3322 (goto-char (point-max))
3323 (beginning-of-line 1)
3324 (while (not (bobp))
3325 (when (org-get-at-bol 'org-hd-marker)
3326 (org-agenda-entry-text-show-here))
3327 (beginning-of-line 0))))
3329 (defun org-agenda-entry-text-hide ()
3330 "Remove any shown entry context."
3331 (delq nil
3332 (mapcar (lambda (o)
3333 (if (eq (overlay-get o 'org-overlay-type)
3334 'agenda-entry-content)
3335 (progn (delete-overlay o) t)))
3336 (overlays-in (point-min) (point-max)))))
3338 (defun org-agenda-get-day-face (date)
3339 "Return the face DATE should be displayed with."
3340 (or (and (functionp org-agenda-day-face-function)
3341 (funcall org-agenda-day-face-function date))
3342 (cond ((org-agenda-todayp date)
3343 'org-agenda-date-today)
3344 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3345 'org-agenda-date-weekend)
3346 (t 'org-agenda-date))))
3348 ;;; Agenda timeline
3350 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3352 (defun org-timeline (&optional dotodo)
3353 "Show a time-sorted view of the entries in the current org file.
3354 Only entries with a time stamp of today or later will be listed. With
3355 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3356 under the current date.
3357 If the buffer contains an active region, only check the region for
3358 dates."
3359 (interactive "P")
3360 (org-compile-prefix-format 'timeline)
3361 (org-set-sorting-strategy 'timeline)
3362 (let* ((dopast t)
3363 (doclosed org-agenda-show-log)
3364 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3365 (current-buffer))))
3366 (date (calendar-current-date))
3367 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3368 (end (if (org-region-active-p) (region-end) (point-max)))
3369 (day-numbers (org-get-all-dates beg end 'no-ranges
3370 t doclosed ; always include today
3371 org-timeline-show-empty-dates))
3372 (org-deadline-warning-days 0)
3373 (org-agenda-only-exact-dates t)
3374 (today (org-today))
3375 (past t)
3376 args
3377 s e rtn d emptyp)
3378 (setq org-agenda-redo-command
3379 (list 'progn
3380 (list 'org-switch-to-buffer-other-window (current-buffer))
3381 (list 'org-timeline (list 'quote dotodo))))
3382 (if (not dopast)
3383 ;; Remove past dates from the list of dates.
3384 (setq day-numbers (delq nil (mapcar (lambda(x)
3385 (if (>= x today) x nil))
3386 day-numbers))))
3387 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3388 (if doclosed (push :closed args))
3389 (push :timestamp args)
3390 (push :deadline args)
3391 (push :scheduled args)
3392 (push :sexp args)
3393 (if dotodo (push :todo args))
3394 (insert "Timeline of file " entry "\n")
3395 (add-text-properties (point-min) (point)
3396 (list 'face 'org-agenda-structure))
3397 (org-agenda-mark-header-line (point-min))
3398 (while (setq d (pop day-numbers))
3399 (if (and (listp d) (eq (car d) :omitted))
3400 (progn
3401 (setq s (point))
3402 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3403 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3404 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3405 (if (and (>= d today)
3406 dopast
3407 past)
3408 (progn
3409 (setq past nil)
3410 (insert (make-string 79 ?-) "\n")))
3411 (setq date (calendar-gregorian-from-absolute d))
3412 (setq s (point))
3413 (setq rtn (and (not emptyp)
3414 (apply 'org-agenda-get-day-entries entry
3415 date args)))
3416 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3417 (progn
3418 (insert
3419 (if (stringp org-agenda-format-date)
3420 (format-time-string org-agenda-format-date
3421 (org-time-from-absolute date))
3422 (funcall org-agenda-format-date date))
3423 "\n")
3424 (put-text-property s (1- (point)) 'face
3425 (org-agenda-get-day-face date))
3426 (put-text-property s (1- (point)) 'org-date-line t)
3427 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3428 (if (equal d today)
3429 (put-text-property s (1- (point)) 'org-today t))
3430 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3431 (put-text-property s (1- (point)) 'day d)))))
3432 (goto-char (point-min))
3433 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3434 (point-min)))
3435 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3436 (org-finalize-agenda)
3437 (setq buffer-read-only t)))
3439 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3440 "Return a list of all relevant day numbers from BEG to END buffer positions.
3441 If NO-RANGES is non-nil, include only the start and end dates of a range,
3442 not every single day in the range. If FORCE-TODAY is non-nil, make
3443 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3444 inactive time stamps (those in square brackets) are included.
3445 When EMPTY is non-nil, also include days without any entries."
3446 (let ((re (concat
3447 (if pre-re pre-re "")
3448 (if inactive org-ts-regexp-both org-ts-regexp)))
3449 dates dates1 date day day1 day2 ts1 ts2 pos)
3450 (if force-today
3451 (setq dates (list (org-today))))
3452 (save-excursion
3453 (goto-char beg)
3454 (while (re-search-forward re end t)
3455 (setq day (time-to-days (org-time-string-to-time
3456 (substring (match-string 1) 0 10)
3457 (current-buffer) (match-beginning 0))))
3458 (or (memq day dates) (push day dates)))
3459 (unless no-ranges
3460 (goto-char beg)
3461 (while (re-search-forward org-tr-regexp end t)
3462 (setq pos (match-beginning 0))
3463 (setq ts1 (substring (match-string 1) 0 10)
3464 ts2 (substring (match-string 2) 0 10)
3465 day1 (time-to-days (org-time-string-to-time
3466 ts1 (current-buffer) pos))
3467 day2 (time-to-days (org-time-string-to-time
3468 ts2 (current-buffer) pos)))
3469 (while (< (setq day1 (1+ day1)) day2)
3470 (or (memq day1 dates) (push day1 dates)))))
3471 (setq dates (sort dates '<))
3472 (when empty
3473 (while (setq day (pop dates))
3474 (setq day2 (car dates))
3475 (push day dates1)
3476 (when (and day2 empty)
3477 (if (or (eq empty t)
3478 (and (numberp empty) (<= (- day2 day) empty)))
3479 (while (< (setq day (1+ day)) day2)
3480 (push (list day) dates1))
3481 (push (cons :omitted (- day2 day)) dates1))))
3482 (setq dates (nreverse dates1)))
3483 dates)))
3485 ;;; Agenda Daily/Weekly
3487 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3488 "Start day for the agenda view.
3489 Custom commands can set this variable in the options section.")
3490 (defvar org-starting-day nil) ; local variable in the agenda buffer
3491 (defvar org-agenda-current-span nil
3492 "The current span used in the agenda view.") ; local variable in the agenda buffer
3493 (defvar org-arg-loc nil) ; local variable
3495 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3496 "List of types searched for when creating the daily/weekly agenda.
3497 This variable is a list of symbols that controls the types of
3498 items that appear in the daily/weekly agenda. Allowed symbols in this
3499 list are are
3501 :timestamp List items containing a date stamp or date range matching
3502 the selected date. This includes sexp entries in
3503 angular brackets.
3505 :sexp List entries resulting from plain diary-like sexps.
3507 :deadline List deadline due on that date. When the date is today,
3508 also list any deadlines past due, or due within
3509 `org-deadline-warning-days'. `:deadline' must appear before
3510 `:scheduled' if the setting of
3511 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3512 any effect.
3514 :scheduled List all items which are scheduled for the given date.
3515 The diary for *today* also contains items which were
3516 scheduled earlier and are not yet marked DONE.
3518 By default, all four types are turned on.
3520 Never set this variable globally using `setq', because then it
3521 will apply to all future agenda commands. Instead, bind it with
3522 `let' to scope it dynamically into the agenda-constructing
3523 command. A good way to set it is through options in
3524 `org-agenda-custom-commands'. For a more flexible (though
3525 somewhat less efficient) way of determining what is included in
3526 the daily/weekly agenda, see `org-agenda-skip-function'.")
3528 ;;;###autoload
3529 (defun org-agenda-list (&optional arg start-day span)
3530 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3531 The view will be for the current day or week, but from the overview buffer
3532 you will be able to go to other days/weeks.
3534 With a numeric prefix argument in an interactive call, the agenda will
3535 span ARG days. Lisp programs should instead specify SPAN to change
3536 the number of days. SPAN defaults to `org-agenda-span'.
3538 START-DAY defaults to TODAY, or to the most recent match for the weekday
3539 given in `org-agenda-start-on-weekday'."
3540 (interactive "P")
3541 (if (and (integerp arg) (> arg 0))
3542 (setq span arg arg nil))
3543 (setq start-day (or start-day org-agenda-start-day))
3544 (if org-agenda-overriding-arguments
3545 (setq arg (car org-agenda-overriding-arguments)
3546 start-day (nth 1 org-agenda-overriding-arguments)
3547 span (nth 2 org-agenda-overriding-arguments)))
3548 (if (stringp start-day)
3549 ;; Convert to an absolute day number
3550 (setq start-day (time-to-days (org-read-date nil t start-day))))
3551 (setq org-agenda-last-arguments (list arg start-day span))
3552 (org-compile-prefix-format 'agenda)
3553 (org-set-sorting-strategy 'agenda)
3554 (let* ((span (org-agenda-ndays-to-span
3555 (or span org-agenda-ndays org-agenda-span)))
3556 (today (org-today))
3557 (sd (or start-day today))
3558 (ndays (org-agenda-span-to-ndays span sd))
3559 (org-agenda-start-on-weekday
3560 (if (eq ndays 7)
3561 org-agenda-start-on-weekday))
3562 (thefiles (org-agenda-files nil 'ifmode))
3563 (files thefiles)
3564 (start (if (or (null org-agenda-start-on-weekday)
3565 (< ndays 7))
3567 (let* ((nt (calendar-day-of-week
3568 (calendar-gregorian-from-absolute sd)))
3569 (n1 org-agenda-start-on-weekday)
3570 (d (- nt n1)))
3571 (- sd (+ (if (< d 0) 7 0) d)))))
3572 (day-numbers (list start))
3573 (day-cnt 0)
3574 (inhibit-redisplay (not debug-on-error))
3575 s e rtn rtnall file date d start-pos end-pos todayp
3576 clocktable-start clocktable-end filter)
3577 (setq org-agenda-redo-command
3578 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3579 (dotimes (n (1- ndays))
3580 (push (1+ (car day-numbers)) day-numbers))
3581 (setq day-numbers (nreverse day-numbers))
3582 (setq clocktable-start (car day-numbers)
3583 clocktable-end (1+ (or (org-last day-numbers) 0)))
3584 (org-prepare-agenda "Day/Week")
3585 (org-set-local 'org-starting-day (car day-numbers))
3586 (org-set-local 'org-arg-loc arg)
3587 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3588 (unless org-agenda-compact-blocks
3589 (let* ((d1 (car day-numbers))
3590 (d2 (org-last day-numbers))
3591 (w1 (org-days-to-iso-week d1))
3592 (w2 (org-days-to-iso-week d2)))
3593 (setq s (point))
3594 (if org-agenda-overriding-header
3595 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3596 nil 'face 'org-agenda-structure) "\n")
3597 (insert (org-agenda-span-name span)
3598 "-agenda"
3599 (if (< (- d2 d1) 350)
3600 (if (= w1 w2)
3601 (format " (W%02d)" w1)
3602 (format " (W%02d-W%02d)" w1 w2))
3604 ":\n")))
3605 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3606 'org-date-line t))
3607 (org-agenda-mark-header-line s))
3608 (while (setq d (pop day-numbers))
3609 (setq date (calendar-gregorian-from-absolute d)
3610 s (point))
3611 (if (or (setq todayp (= d today))
3612 (and (not start-pos) (= d sd)))
3613 (setq start-pos (point))
3614 (if (and start-pos (not end-pos))
3615 (setq end-pos (point))))
3616 (setq files thefiles
3617 rtnall nil)
3618 (while (setq file (pop files))
3619 (catch 'nextfile
3620 (org-check-agenda-file file)
3621 (let ((org-agenda-entry-types org-agenda-entry-types))
3622 (unless org-agenda-include-deadlines
3623 (setq org-agenda-entry-types
3624 (delq :deadline org-agenda-entry-types)))
3625 (cond
3626 ((memq org-agenda-show-log '(only clockcheck))
3627 (setq rtn (org-agenda-get-day-entries
3628 file date :closed)))
3629 (org-agenda-show-log
3630 (setq rtn (apply 'org-agenda-get-day-entries
3631 file date
3632 (append '(:closed) org-agenda-entry-types))))
3634 (setq rtn (apply 'org-agenda-get-day-entries
3635 file date
3636 org-agenda-entry-types)))))
3637 (setq rtnall (append rtnall rtn))))
3638 (if org-agenda-include-diary
3639 (let ((org-agenda-search-headline-for-time t))
3640 (require 'diary-lib)
3641 (setq rtn (org-get-entries-from-diary date))
3642 (setq rtnall (append rtnall rtn))))
3643 (if (or rtnall org-agenda-show-all-dates)
3644 (progn
3645 (setq day-cnt (1+ day-cnt))
3646 (insert
3647 (if (stringp org-agenda-format-date)
3648 (format-time-string org-agenda-format-date
3649 (org-time-from-absolute date))
3650 (funcall org-agenda-format-date date))
3651 "\n")
3652 (put-text-property s (1- (point)) 'face
3653 (org-agenda-get-day-face date))
3654 (put-text-property s (1- (point)) 'org-date-line t)
3655 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3656 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3657 (when todayp
3658 (put-text-property s (1- (point)) 'org-today t))
3659 (if rtnall (insert
3660 (org-finalize-agenda-entries
3661 (org-agenda-add-time-grid-maybe
3662 rtnall ndays todayp))
3663 "\n"))
3664 (put-text-property s (1- (point)) 'day d)
3665 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3666 (when (and org-agenda-clockreport-mode clocktable-start)
3667 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3668 ;; the above line is to ensure the restricted range!
3669 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3670 tbl)
3671 (setq p (org-plist-delete p :block))
3672 (setq p (plist-put p :tstart clocktable-start))
3673 (setq p (plist-put p :tend clocktable-end))
3674 (setq p (plist-put p :scope 'agenda))
3675 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3676 (setq filter (or org-agenda-filter-while-redo
3677 (get 'org-agenda-filter :preset-filter))))
3678 (setq p (plist-put p :tags (mapconcat (lambda (x)
3679 (if (string-match "[<>=]" x)
3682 filter ""))))
3683 (setq tbl (apply 'org-get-clocktable p))
3684 (insert tbl)))
3685 (goto-char (point-min))
3686 (or org-agenda-multi (org-fit-agenda-window))
3687 (unless (and (pos-visible-in-window-p (point-min))
3688 (pos-visible-in-window-p (point-max)))
3689 (goto-char (1- (point-max)))
3690 (recenter -1)
3691 (if (not (pos-visible-in-window-p (or start-pos 1)))
3692 (progn
3693 (goto-char (or start-pos 1))
3694 (recenter 1))))
3695 (goto-char (or start-pos 1))
3696 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3697 (if (eq org-agenda-show-log 'clockcheck)
3698 (org-agenda-show-clocking-issues))
3699 (org-finalize-agenda)
3700 (setq buffer-read-only t)
3701 (message "")))
3703 (defun org-agenda-ndays-to-span (n)
3704 "Return a span symbol for a span of N days, or N if none matches."
3705 (cond ((symbolp n) n)
3706 ((= n 1) 'day)
3707 ((= n 7) 'week)
3708 (t n)))
3710 (defun org-agenda-span-to-ndays (span start-day)
3711 "Return ndays from SPAN starting at START-DAY."
3712 (cond ((numberp span) span)
3713 ((eq span 'day) 1)
3714 ((eq span 'week) 7)
3715 ((eq span 'month)
3716 (let ((date (calendar-gregorian-from-absolute start-day)))
3717 (calendar-last-day-of-month (car date) (caddr date))))
3718 ((eq span 'year)
3719 (let ((date (calendar-gregorian-from-absolute start-day)))
3720 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3722 (defun org-agenda-span-name (span)
3723 "Return a SPAN name."
3724 (if (null span)
3726 (if (symbolp span)
3727 (capitalize (symbol-name span))
3728 (format "%d days" span))))
3730 ;;; Agenda word search
3732 (defvar org-agenda-search-history nil)
3733 (defvar org-todo-only nil)
3735 (defvar org-search-syntax-table nil
3736 "Special syntax table for org-mode search.
3737 In this table, we have single quotes not as word constituents, to
3738 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3740 (defun org-search-syntax-table ()
3741 (unless org-search-syntax-table
3742 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3743 (modify-syntax-entry ?' "." org-search-syntax-table)
3744 (modify-syntax-entry ?` "." org-search-syntax-table))
3745 org-search-syntax-table)
3747 (defvar org-agenda-last-search-view-search-was-boolean nil)
3749 ;;;###autoload
3750 (defun org-search-view (&optional todo-only string edit-at)
3751 "Show all entries that contain a phrase or words or regular expressions.
3753 With optional prefix argument TODO-ONLY, only consider entries that are
3754 TODO entries. The argument STRING can be used to pass a default search
3755 string into this function. If EDIT-AT is non-nil, it means that the
3756 user should get a chance to edit this string, with cursor at position
3757 EDIT-AT.
3759 The search string can be viewed either as a phrase that should be found as
3760 is, or it can be broken into a number of snippets, each of which must match
3761 in a Boolean way to select an entry. The default depends on the variable
3762 `org-agenda-search-view-always-boolean'.
3763 Even if this is turned off (the default) you can always switch to
3764 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3766 The default is a direct search of the whole phrase, where each space in
3767 the search string can expand to an arbitrary amount of whitespace,
3768 including newlines.
3770 If using a Boolean search, the search string is split on whitespace and
3771 each snippet is searched separately, with logical AND to select an entry.
3772 Words prefixed with a minus must *not* occur in the entry. Words without
3773 a prefix or prefixed with a plus must occur in the entry. Matching is
3774 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3775 match whole words, not parts of a word) if
3776 `org-agenda-search-view-force-full-words' is set (default is nil).
3778 Boolean search snippets enclosed by curly braces are interpreted as
3779 regular expressions that must or (when preceded with \"-\") must not
3780 match in the entry. Snippets enclosed into double quotes will be taken
3781 as a whole, to include whitespace.
3783 - If the search string starts with an asterisk, search only in headlines.
3784 - If (possibly after the leading star) the search string starts with an
3785 exclamation mark, this also means to look at TODO entries only, an effect
3786 that can also be achieved with a prefix argument.
3787 - If (possibly after star and exclamation mark) the search string starts
3788 with a colon, this will mean that the (non-regexp) snippets of the
3789 Boolean search must match as full words.
3791 This command searches the agenda files, and in addition the files listed
3792 in `org-agenda-text-search-extra-files'."
3793 (interactive "P")
3794 (org-compile-prefix-format 'search)
3795 (org-set-sorting-strategy 'search)
3796 (org-prepare-agenda "SEARCH")
3797 (let* ((props (list 'face nil
3798 'done-face 'org-agenda-done
3799 'org-not-done-regexp org-not-done-regexp
3800 'org-todo-regexp org-todo-regexp
3801 'org-complex-heading-regexp org-complex-heading-regexp
3802 'mouse-face 'highlight
3803 'help-echo (format "mouse-2 or RET jump to location")))
3804 (full-words org-agenda-search-view-force-full-words)
3805 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3806 regexp rtn rtnall files file pos
3807 marker category org-category-pos tags c neg re boolean
3808 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3809 (unless (and (not edit-at)
3810 (stringp string)
3811 (string-match "\\S-" string))
3812 (setq string (read-string
3813 (if org-agenda-search-view-always-boolean
3814 "[+-]Word/{Regexp} ...: "
3815 "Phrase, or [+-]Word/{Regexp} ...: ")
3816 (cond
3817 ((integerp edit-at) (cons string edit-at))
3818 (edit-at string))
3819 'org-agenda-search-history)))
3820 (org-set-local 'org-todo-only todo-only)
3821 (setq org-agenda-redo-command
3822 (list 'org-search-view (if todo-only t nil) string
3823 '(if current-prefix-arg 1 nil)))
3824 (setq org-agenda-query-string string)
3826 (if (equal (string-to-char string) ?*)
3827 (setq hdl-only t
3828 words (substring string 1))
3829 (setq words string))
3830 (when (equal (string-to-char words) ?!)
3831 (setq todo-only t
3832 words (substring words 1)))
3833 (when (equal (string-to-char words) ?:)
3834 (setq full-words t
3835 words (substring words 1)))
3836 (if (or org-agenda-search-view-always-boolean
3837 (member (string-to-char words) '(?- ?+ ?\{)))
3838 (setq boolean t))
3839 (setq words (org-split-string words))
3840 (let (www w)
3841 (while (setq w (pop words))
3842 (while (and (string-match "\\\\\\'" w) words)
3843 (setq w (concat (substring w 0 -1) " " (pop words))))
3844 (push w www))
3845 (setq words (nreverse www) www nil)
3846 (while (setq w (pop words))
3847 (when (and (string-match "\\`[-+]?{" w)
3848 (not (string-match "}\\'" w)))
3849 (while (and words (not (string-match "}\\'" (car words))))
3850 (setq w (concat w " " (pop words))))
3851 (setq w (concat w " " (pop words))))
3852 (push w www))
3853 (setq words (nreverse www)))
3854 (setq org-agenda-last-search-view-search-was-boolean boolean)
3855 (when boolean
3856 (let (wds w)
3857 (while (setq w (pop words))
3858 (if (or (equal (substring w 0 1) "\"")
3859 (and (> (length w) 1)
3860 (member (substring w 0 1) '("+" "-"))
3861 (equal (substring w 1 2) "\"")))
3862 (while (and words (not (equal (substring w -1) "\"")))
3863 (setq w (concat w " " (pop words)))))
3864 (and (string-match "\\`\\([-+]?\\)\"" w)
3865 (setq w (replace-match "\\1" nil nil w)))
3866 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3867 (push w wds))
3868 (setq words (nreverse wds))))
3869 (if boolean
3870 (mapc (lambda (w)
3871 (setq c (string-to-char w))
3872 (if (equal c ?-)
3873 (setq neg t w (substring w 1))
3874 (if (equal c ?+)
3875 (setq neg nil w (substring w 1))
3876 (setq neg nil)))
3877 (if (string-match "\\`{.*}\\'" w)
3878 (setq re (substring w 1 -1))
3879 (if full-words
3880 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3881 (setq re (regexp-quote (downcase w)))))
3882 (if neg (push re regexps-) (push re regexps+)))
3883 words)
3884 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3885 regexps+))
3886 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3887 (if (not regexps+)
3888 (setq regexp org-outline-regexp-bol)
3889 (setq regexp (pop regexps+))
3890 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
3891 regexp))))
3892 (setq files (org-agenda-files nil 'ifmode))
3893 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3894 (pop org-agenda-text-search-extra-files)
3895 (setq files (org-add-archive-files files)))
3896 (setq files (append files org-agenda-text-search-extra-files)
3897 rtnall nil)
3898 (while (setq file (pop files))
3899 (setq ee nil)
3900 (catch 'nextfile
3901 (org-check-agenda-file file)
3902 (setq buffer (if (file-exists-p file)
3903 (org-get-agenda-file-buffer file)
3904 (error "No such file %s" file)))
3905 (if (not buffer)
3906 ;; If file does not exist, make sure an error message is sent
3907 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3908 file))))
3909 (with-current-buffer buffer
3910 (with-syntax-table (org-search-syntax-table)
3911 (unless (eq major-mode 'org-mode)
3912 (error "Agenda file %s is not in `org-mode'" file))
3913 (let ((case-fold-search t))
3914 (save-excursion
3915 (save-restriction
3916 (if org-agenda-restrict
3917 (narrow-to-region org-agenda-restrict-begin
3918 org-agenda-restrict-end)
3919 (widen))
3920 (goto-char (point-min))
3921 (unless (or (org-on-heading-p)
3922 (outline-next-heading))
3923 (throw 'nextfile t))
3924 (goto-char (max (point-min) (1- (point))))
3925 (while (re-search-forward regexp nil t)
3926 (org-back-to-heading t)
3927 (skip-chars-forward "* ")
3928 (setq beg (point-at-bol)
3929 beg1 (point)
3930 end (progn (outline-next-heading) (point)))
3931 (catch :skip
3932 (goto-char beg)
3933 (org-agenda-skip)
3934 (setq str (buffer-substring-no-properties
3935 (point-at-bol)
3936 (if hdl-only (point-at-eol) end)))
3937 (mapc (lambda (wr) (when (string-match wr str)
3938 (goto-char (1- end))
3939 (throw :skip t)))
3940 regexps-)
3941 (mapc (lambda (wr) (unless (string-match wr str)
3942 (goto-char (1- end))
3943 (throw :skip t)))
3944 (if todo-only
3945 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3946 regexps+)
3947 regexps+))
3948 (goto-char beg)
3949 (setq marker (org-agenda-new-marker (point))
3950 category (org-get-category)
3951 org-category-pos (get-text-property (point) 'org-category-position)
3952 tags (org-get-tags-at (point))
3953 txt (org-agenda-format-item
3955 (buffer-substring-no-properties
3956 beg1 (point-at-eol))
3957 category tags))
3958 (org-add-props txt props
3959 'org-marker marker 'org-hd-marker marker
3960 'org-todo-regexp org-todo-regexp
3961 'org-complex-heading-regexp org-complex-heading-regexp
3962 'priority 1000 'org-category category
3963 'org-category-position org-category-pos
3964 'type "search")
3965 (push txt ee)
3966 (goto-char (1- end))))))))))
3967 (setq rtn (nreverse ee))
3968 (setq rtnall (append rtnall rtn)))
3969 (if org-agenda-overriding-header
3970 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3971 nil 'face 'org-agenda-structure) "\n")
3972 (insert "Search words: ")
3973 (add-text-properties (point-min) (1- (point))
3974 (list 'face 'org-agenda-structure))
3975 (setq pos (point))
3976 (insert string "\n")
3977 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3978 (setq pos (point))
3979 (unless org-agenda-multi
3980 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3981 (add-text-properties pos (1- (point))
3982 (list 'face 'org-agenda-structure))))
3983 (org-agenda-mark-header-line (point-min))
3984 (when rtnall
3985 (insert (org-finalize-agenda-entries rtnall) "\n"))
3986 (goto-char (point-min))
3987 (or org-agenda-multi (org-fit-agenda-window))
3988 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3989 (org-finalize-agenda)
3990 (setq buffer-read-only t)))
3992 ;;; Agenda TODO list
3994 (defvar org-select-this-todo-keyword nil)
3995 (defvar org-last-arg nil)
3997 ;;;###autoload
3998 (defun org-todo-list (arg)
3999 "Show all (not done) TODO entries from all agenda file in a single list.
4000 The prefix arg can be used to select a specific TODO keyword and limit
4001 the list to these. When using \\[universal-argument], you will be prompted
4002 for a keyword. A numeric prefix directly selects the Nth keyword in
4003 `org-todo-keywords-1'."
4004 (interactive "P")
4005 (org-compile-prefix-format 'todo)
4006 (org-set-sorting-strategy 'todo)
4007 (org-prepare-agenda "TODO")
4008 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4009 (let* ((today (org-today))
4010 (date (calendar-gregorian-from-absolute today))
4011 (kwds org-todo-keywords-for-agenda)
4012 (completion-ignore-case t)
4013 (org-select-this-todo-keyword
4014 (if (stringp arg) arg
4015 (and arg (integerp arg) (> arg 0)
4016 (nth (1- arg) kwds))))
4017 rtn rtnall files file pos)
4018 (when (equal arg '(4))
4019 (setq org-select-this-todo-keyword
4020 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4021 (mapcar 'list kwds) nil nil)))
4022 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4023 (org-set-local 'org-last-arg arg)
4024 (setq org-agenda-redo-command
4025 '(org-todo-list (or current-prefix-arg org-last-arg)))
4026 (setq files (org-agenda-files nil 'ifmode)
4027 rtnall nil)
4028 (while (setq file (pop files))
4029 (catch 'nextfile
4030 (org-check-agenda-file file)
4031 (setq rtn (org-agenda-get-day-entries file date :todo))
4032 (setq rtnall (append rtnall rtn))))
4033 (if org-agenda-overriding-header
4034 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4035 nil 'face 'org-agenda-structure) "\n")
4036 (insert "Global list of TODO items of type: ")
4037 (add-text-properties (point-min) (1- (point))
4038 (list 'face 'org-agenda-structure
4039 'short-heading
4040 (concat "ToDo: "
4041 (or org-select-this-todo-keyword "ALL"))))
4042 (org-agenda-mark-header-line (point-min))
4043 (setq pos (point))
4044 (insert (or org-select-this-todo-keyword "ALL") "\n")
4045 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4046 (setq pos (point))
4047 (unless org-agenda-multi
4048 (insert "Available with `N r': (0)ALL")
4049 (let ((n 0) s)
4050 (mapc (lambda (x)
4051 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4052 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4053 (insert "\n "))
4054 (insert " " s))
4055 kwds))
4056 (insert "\n"))
4057 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4058 (org-agenda-mark-header-line (point-min))
4059 (when rtnall
4060 (insert (org-finalize-agenda-entries rtnall) "\n"))
4061 (goto-char (point-min))
4062 (or org-agenda-multi (org-fit-agenda-window))
4063 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4064 (org-finalize-agenda)
4065 (setq buffer-read-only t)))
4067 ;;; Agenda tags match
4069 ;;;###autoload
4070 (defun org-tags-view (&optional todo-only match)
4071 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4072 The prefix arg TODO-ONLY limits the search to TODO entries."
4073 (interactive "P")
4074 (org-compile-prefix-format 'tags)
4075 (org-set-sorting-strategy 'tags)
4076 (let* ((org-tags-match-list-sublevels
4077 org-tags-match-list-sublevels)
4078 (completion-ignore-case t)
4079 rtn rtnall files file pos matcher
4080 buffer)
4081 (when (and (stringp match) (not (string-match "\\S-" match)))
4082 (setq match nil))
4083 (setq matcher (org-make-tags-matcher match)
4084 match (car matcher) matcher (cdr matcher))
4085 (org-prepare-agenda (concat "TAGS " match))
4086 (setq org-agenda-query-string match)
4087 (setq org-agenda-redo-command
4088 (list 'org-tags-view (list 'quote todo-only)
4089 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4090 (setq files (org-agenda-files nil 'ifmode)
4091 rtnall nil)
4092 (while (setq file (pop files))
4093 (catch 'nextfile
4094 (org-check-agenda-file file)
4095 (setq buffer (if (file-exists-p file)
4096 (org-get-agenda-file-buffer file)
4097 (error "No such file %s" file)))
4098 (if (not buffer)
4099 ;; If file does not exist, error message to agenda
4100 (setq rtn (list
4101 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4102 rtnall (append rtnall rtn))
4103 (with-current-buffer buffer
4104 (unless (eq major-mode 'org-mode)
4105 (error "Agenda file %s is not in `org-mode'" file))
4106 (save-excursion
4107 (save-restriction
4108 (if org-agenda-restrict
4109 (narrow-to-region org-agenda-restrict-begin
4110 org-agenda-restrict-end)
4111 (widen))
4112 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4113 (setq rtnall (append rtnall rtn))))))))
4114 (if org-agenda-overriding-header
4115 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4116 nil 'face 'org-agenda-structure) "\n")
4117 (insert "Headlines with TAGS match: ")
4118 (add-text-properties (point-min) (1- (point))
4119 (list 'face 'org-agenda-structure
4120 'short-heading
4121 (concat "Match: " match)))
4122 (setq pos (point))
4123 (insert match "\n")
4124 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4125 (setq pos (point))
4126 (unless org-agenda-multi
4127 (insert "Press `C-u r' to search again with new search string\n"))
4128 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4129 (org-agenda-mark-header-line (point-min))
4130 (when rtnall
4131 (insert (org-finalize-agenda-entries rtnall) "\n"))
4132 (goto-char (point-min))
4133 (or org-agenda-multi (org-fit-agenda-window))
4134 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4135 (org-finalize-agenda)
4136 (setq buffer-read-only t)))
4138 ;;; Agenda Finding stuck projects
4140 (defvar org-agenda-skip-regexp nil
4141 "Regular expression used in skipping subtrees for the agenda.
4142 This is basically a temporary global variable that can be set and then
4143 used by user-defined selections using `org-agenda-skip-function'.")
4145 (defvar org-agenda-overriding-header nil
4146 "When set during agenda, todo and tags searches it replaces the header.
4147 This variable should not be set directly, but custom commands can bind it
4148 in the options section.")
4150 (defun org-agenda-skip-entry-when-regexp-matches ()
4151 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4152 If yes, it returns the end position of this entry, causing agenda commands
4153 to skip the entry but continuing the search in the subtree. This is a
4154 function that can be put into `org-agenda-skip-function' for the duration
4155 of a command."
4156 (let ((end (save-excursion (org-end-of-subtree t)))
4157 skip)
4158 (save-excursion
4159 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4160 (and skip end)))
4162 (defun org-agenda-skip-subtree-when-regexp-matches ()
4163 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4164 If yes, it returns the end position of this tree, causing agenda commands
4165 to skip this subtree. This is a function that can be put into
4166 `org-agenda-skip-function' for the duration of a command."
4167 (let ((end (save-excursion (org-end-of-subtree t)))
4168 skip)
4169 (save-excursion
4170 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4171 (and skip end)))
4173 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4174 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4175 If yes, it returns the end position of the current entry (NOT the tree),
4176 causing agenda commands to skip the entry but continuing the search in
4177 the subtree. This is a function that can be put into
4178 `org-agenda-skip-function' for the duration of a command. An important
4179 use of this function is for the stuck project list."
4180 (let ((end (save-excursion (org-end-of-subtree t)))
4181 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4182 skip)
4183 (save-excursion
4184 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4185 (and skip entry-end)))
4187 (defun org-agenda-skip-entry-if (&rest conditions)
4188 "Skip entry if any of CONDITIONS is true.
4189 See `org-agenda-skip-if' for details."
4190 (org-agenda-skip-if nil conditions))
4192 (defun org-agenda-skip-subtree-if (&rest conditions)
4193 "Skip entry if any of CONDITIONS is true.
4194 See `org-agenda-skip-if' for details."
4195 (org-agenda-skip-if t conditions))
4197 (defun org-agenda-skip-if (subtree conditions)
4198 "Checks current entity for CONDITIONS.
4199 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4200 the entry, i.e. the text before the next heading is checked.
4202 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4203 from different tests. Valid conditions are:
4205 scheduled Check if there is a scheduled cookie
4206 notscheduled Check if there is no scheduled cookie
4207 deadline Check if there is a deadline
4208 notdeadline Check if there is no deadline
4209 timestamp Check if there is a timestamp (also deadline or scheduled)
4210 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4211 regexp Check if regexp matches
4212 notregexp Check if regexp does not match.
4213 todo Check if TODO keyword matches
4214 nottodo Check if TODO keyword does not match
4216 The regexp is taken from the conditions list, it must come right after
4217 the `regexp' or `notregexp' element.
4219 `todo' and `nottodo' accept as an argument a list of todo
4220 keywords, which may include \"*\" to match any todo keyword.
4222 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4224 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4226 Instead of a list a keyword class may be given
4228 (org-agenda-skip-entry-if 'nottodo 'done)
4230 would skip entries that haven't been marked with any of \"DONE\"
4231 keywords. Possible classes are: `todo', `done', `any'.
4233 If any of these conditions is met, this function returns the end point of
4234 the entity, causing the search to continue from there. This is a function
4235 that can be put into `org-agenda-skip-function' for the duration of a command."
4236 (let (beg end m)
4237 (org-back-to-heading t)
4238 (setq beg (point)
4239 end (if subtree
4240 (progn (org-end-of-subtree t) (point))
4241 (progn (outline-next-heading) (1- (point)))))
4242 (goto-char beg)
4243 (and
4245 (and (memq 'scheduled conditions)
4246 (re-search-forward org-scheduled-time-regexp end t))
4247 (and (memq 'notscheduled conditions)
4248 (not (re-search-forward org-scheduled-time-regexp end t)))
4249 (and (memq 'deadline conditions)
4250 (re-search-forward org-deadline-time-regexp end t))
4251 (and (memq 'notdeadline conditions)
4252 (not (re-search-forward org-deadline-time-regexp end t)))
4253 (and (memq 'timestamp conditions)
4254 (re-search-forward org-ts-regexp end t))
4255 (and (memq 'nottimestamp conditions)
4256 (not (re-search-forward org-ts-regexp end t)))
4257 (and (setq m (memq 'regexp conditions))
4258 (stringp (nth 1 m))
4259 (re-search-forward (nth 1 m) end t))
4260 (and (setq m (memq 'notregexp conditions))
4261 (stringp (nth 1 m))
4262 (not (re-search-forward (nth 1 m) end t)))
4263 (and (or
4264 (setq m (memq 'todo conditions))
4265 (setq m (memq 'nottodo conditions)))
4266 (org-agenda-skip-if-todo m end)))
4267 end)))
4269 (defun org-agenda-skip-if-todo (args end)
4270 "Helper function for `org-agenda-skip-if', do not use it directly.
4271 ARGS is a list with first element either `todo' or `nottodo'.
4272 The remainder is either a list of TODO keywords, or a state symbol
4273 `todo' or `done' or `any'."
4274 (let ((kw (car args))
4275 (arg (cadr args))
4276 todo-wds todo-re)
4277 (setq todo-wds
4278 (org-uniquify
4279 (cond
4280 ((listp arg) ;; list of keywords
4281 (if (member "*" arg)
4282 (mapcar 'substring-no-properties org-todo-keywords-1)
4283 arg))
4284 ((symbolp arg) ;; keyword class name
4285 (cond
4286 ((eq arg 'todo)
4287 (org-delete-all org-done-keywords
4288 (mapcar 'substring-no-properties
4289 org-todo-keywords-1)))
4290 ((eq arg 'done) org-done-keywords)
4291 ((eq arg 'any)
4292 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4293 (setq todo-re
4294 (concat "^\\*+[ \t]+\\<\\("
4295 (mapconcat 'identity todo-wds "\\|")
4296 "\\)\\>"))
4297 (if (eq kw 'todo)
4298 (re-search-forward todo-re end t)
4299 (not (re-search-forward todo-re end t)))))
4301 ;;;###autoload
4302 (defun org-agenda-list-stuck-projects (&rest ignore)
4303 "Create agenda view for projects that are stuck.
4304 Stuck projects are project that have no next actions. For the definitions
4305 of what a project is and how to check if it stuck, customize the variable
4306 `org-stuck-projects'."
4307 (interactive)
4308 (let* ((org-agenda-skip-function
4309 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4310 ;; We could have used org-agenda-skip-if here.
4311 (org-agenda-overriding-header
4312 (or org-agenda-overriding-header "List of stuck projects: "))
4313 (matcher (nth 0 org-stuck-projects))
4314 (todo (nth 1 org-stuck-projects))
4315 (todo-wds (if (member "*" todo)
4316 (progn
4317 (org-prepare-agenda-buffers (org-agenda-files
4318 nil 'ifmode))
4319 (org-delete-all
4320 org-done-keywords-for-agenda
4321 (copy-sequence org-todo-keywords-for-agenda)))
4322 todo))
4323 (todo-re (concat "^\\*+[ \t]+\\("
4324 (mapconcat 'identity todo-wds "\\|")
4325 "\\)\\>"))
4326 (tags (nth 2 org-stuck-projects))
4327 (tags-re (if (member "*" tags)
4328 (concat org-outline-regexp-bol
4329 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4330 (if tags
4331 (concat org-outline-regexp-bol
4332 ".*:\\("
4333 (mapconcat 'identity tags "\\|")
4334 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4335 (gen-re (nth 3 org-stuck-projects))
4336 (re-list
4337 (delq nil
4338 (list
4339 (if todo todo-re)
4340 (if tags tags-re)
4341 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4342 gen-re)))))
4343 (setq org-agenda-skip-regexp
4344 (if re-list
4345 (mapconcat 'identity re-list "\\|")
4346 (error "No information how to identify unstuck projects")))
4347 (org-tags-view nil matcher)
4348 (with-current-buffer org-agenda-buffer-name
4349 (setq org-agenda-redo-command
4350 '(org-agenda-list-stuck-projects
4351 (or current-prefix-arg org-last-arg))))))
4353 ;;; Diary integration
4355 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4356 (defvar list-diary-entries-hook)
4357 (defvar diary-time-regexp)
4358 (defun org-get-entries-from-diary (date)
4359 "Get the (Emacs Calendar) diary entries for DATE."
4360 (require 'diary-lib)
4361 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4362 (diary-display-hook '(fancy-diary-display))
4363 (diary-display-function 'fancy-diary-display)
4364 (pop-up-frames nil)
4365 (list-diary-entries-hook
4366 (cons 'org-diary-default-entry list-diary-entries-hook))
4367 (diary-file-name-prefix-function nil) ; turn this feature off
4368 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4369 entries
4370 (org-disable-agenda-to-diary t))
4371 (save-excursion
4372 (save-window-excursion
4373 (funcall (if (fboundp 'diary-list-entries)
4374 'diary-list-entries 'list-diary-entries)
4375 date 1)))
4376 (if (not (get-buffer diary-fancy-buffer))
4377 (setq entries nil)
4378 (with-current-buffer diary-fancy-buffer
4379 (setq buffer-read-only nil)
4380 (if (zerop (buffer-size))
4381 ;; No entries
4382 (setq entries nil)
4383 ;; Omit the date and other unnecessary stuff
4384 (org-agenda-cleanup-fancy-diary)
4385 ;; Add prefix to each line and extend the text properties
4386 (if (zerop (buffer-size))
4387 (setq entries nil)
4388 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4389 (setq entries
4390 (with-temp-buffer
4391 (insert entries) (goto-char (point-min))
4392 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4393 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4394 (replace-match (concat "; " (match-string 1)))))
4395 (buffer-string)))))
4396 (set-buffer-modified-p nil)
4397 (kill-buffer diary-fancy-buffer)))
4398 (when entries
4399 (setq entries (org-split-string entries "\n"))
4400 (setq entries
4401 (mapcar
4402 (lambda (x)
4403 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4404 ;; Extend the text properties to the beginning of the line
4405 (org-add-props x (text-properties-at (1- (length x)) x)
4406 'type "diary" 'date date 'face 'org-agenda-diary))
4407 entries)))))
4409 (defvar org-agenda-cleanup-fancy-diary-hook nil
4410 "Hook run when the fancy diary buffer is cleaned up.")
4412 (defun org-agenda-cleanup-fancy-diary ()
4413 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4414 This gets rid of the date, the underline under the date, and
4415 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4416 date. It also removes lines that contain only whitespace."
4417 (goto-char (point-min))
4418 (if (looking-at ".*?:[ \t]*")
4419 (progn
4420 (replace-match "")
4421 (re-search-forward "\n=+$" nil t)
4422 (replace-match "")
4423 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4424 (re-search-forward "\n=+$" nil t)
4425 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4426 (goto-char (point-min))
4427 (while (re-search-forward "^ +\n" nil t)
4428 (replace-match ""))
4429 (goto-char (point-min))
4430 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4431 (replace-match ""))
4432 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4434 ;; Make sure entries from the diary have the right text properties.
4435 (eval-after-load "diary-lib"
4436 '(if (boundp 'diary-modify-entry-list-string-function)
4437 ;; We can rely on the hook, nothing to do
4439 ;; Hook not available, must use advice to make this work
4440 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4441 "Make the position visible."
4442 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4443 (stringp string)
4444 buffer-file-name)
4445 (setq string (org-modify-diary-entry-string string))))))
4447 (defun org-modify-diary-entry-string (string)
4448 "Add text properties to string, allowing org-mode to act on it."
4449 (org-add-props string nil
4450 'mouse-face 'highlight
4451 'help-echo (if buffer-file-name
4452 (format "mouse-2 or RET jump to diary file %s"
4453 (abbreviate-file-name buffer-file-name))
4455 'org-agenda-diary-link t
4456 'org-marker (org-agenda-new-marker (point-at-bol))))
4458 (defun org-diary-default-entry ()
4459 "Add a dummy entry to the diary.
4460 Needed to avoid empty dates which mess up holiday display."
4461 ;; Catch the error if dealing with the new add-to-diary-alist
4462 (when org-disable-agenda-to-diary
4463 (condition-case nil
4464 (org-add-to-diary-list original-date "Org-mode dummy" "")
4465 (error
4466 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4468 (defun org-add-to-diary-list (&rest args)
4469 (if (fboundp 'diary-add-to-list)
4470 (apply 'diary-add-to-list args)
4471 (apply 'add-to-diary-list args)))
4473 (defvar org-diary-last-run-time nil)
4475 ;;;###autoload
4476 (defun org-diary (&rest args)
4477 "Return diary information from org-files.
4478 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4479 It accesses org files and extracts information from those files to be
4480 listed in the diary. The function accepts arguments specifying what
4481 items should be listed. For a list of arguments allowed here, see the
4482 variable `org-agenda-entry-types'.
4484 The call in the diary file should look like this:
4486 &%%(org-diary) ~/path/to/some/orgfile.org
4488 Use a separate line for each org file to check. Or, if you omit the file name,
4489 all files listed in `org-agenda-files' will be checked automatically:
4491 &%%(org-diary)
4493 If you don't give any arguments (as in the example above), the default
4494 arguments (:deadline :scheduled :timestamp :sexp) are used.
4495 So the example above may also be written as
4497 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4499 The function expects the lisp variables `entry' and `date' to be provided
4500 by the caller, because this is how the calendar works. Don't use this
4501 function from a program - use `org-agenda-get-day-entries' instead."
4502 (when (> (- (org-float-time)
4503 org-agenda-last-marker-time)
4505 (org-agenda-reset-markers))
4506 (org-compile-prefix-format 'agenda)
4507 (org-set-sorting-strategy 'agenda)
4508 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4509 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4510 (list entry)
4511 (org-agenda-files t)))
4512 (time (org-float-time))
4513 file rtn results)
4514 (when (or (not org-diary-last-run-time)
4515 (> (- time
4516 org-diary-last-run-time)
4518 (org-prepare-agenda-buffers files))
4519 (setq org-diary-last-run-time time)
4520 ;; If this is called during org-agenda, don't return any entries to
4521 ;; the calendar. Org Agenda will list these entries itself.
4522 (if org-disable-agenda-to-diary (setq files nil))
4523 (while (setq file (pop files))
4524 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4525 (setq results (append results rtn)))
4526 (if results
4527 (concat (org-finalize-agenda-entries results) "\n"))))
4529 ;;; Agenda entry finders
4531 (defun org-agenda-get-day-entries (file date &rest args)
4532 "Does the work for `org-diary' and `org-agenda'.
4533 FILE is the path to a file to be checked for entries. DATE is date like
4534 the one returned by `calendar-current-date'. ARGS are symbols indicating
4535 which kind of entries should be extracted. For details about these, see
4536 the documentation of `org-diary'."
4537 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4538 (let* ((org-startup-folded nil)
4539 (org-startup-align-all-tables nil)
4540 (buffer (if (file-exists-p file)
4541 (org-get-agenda-file-buffer file)
4542 (error "No such file %s" file)))
4543 arg results rtn deadline-results)
4544 (if (not buffer)
4545 ;; If file does not exist, make sure an error message ends up in diary
4546 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4547 (with-current-buffer buffer
4548 (unless (eq major-mode 'org-mode)
4549 (error "Agenda file %s is not in `org-mode'" file))
4550 (let ((case-fold-search nil))
4551 (save-excursion
4552 (save-restriction
4553 (if org-agenda-restrict
4554 (narrow-to-region org-agenda-restrict-begin
4555 org-agenda-restrict-end)
4556 (widen))
4557 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4558 (while (setq arg (pop args))
4559 (cond
4560 ((and (eq arg :todo)
4561 (equal date (calendar-gregorian-from-absolute
4562 (org-today))))
4563 (setq rtn (org-agenda-get-todos))
4564 (setq results (append results rtn)))
4565 ((eq arg :timestamp)
4566 (setq rtn (org-agenda-get-blocks))
4567 (setq results (append results rtn))
4568 (setq rtn (org-agenda-get-timestamps))
4569 (setq results (append results rtn)))
4570 ((eq arg :sexp)
4571 (setq rtn (org-agenda-get-sexps))
4572 (setq results (append results rtn)))
4573 ((eq arg :scheduled)
4574 (setq rtn (org-agenda-get-scheduled deadline-results))
4575 (setq results (append results rtn)))
4576 ((eq arg :closed)
4577 (setq rtn (org-agenda-get-progress))
4578 (setq results (append results rtn)))
4579 ((eq arg :deadline)
4580 (setq rtn (org-agenda-get-deadlines))
4581 (setq deadline-results (copy-sequence rtn))
4582 (setq results (append results rtn))))))))
4583 results))))
4585 (defun org-agenda-get-todos ()
4586 "Return the TODO information for agenda display."
4587 (let* ((props (list 'face nil
4588 'done-face 'org-agenda-done
4589 'org-not-done-regexp org-not-done-regexp
4590 'org-todo-regexp org-todo-regexp
4591 'org-complex-heading-regexp org-complex-heading-regexp
4592 'mouse-face 'highlight
4593 'help-echo
4594 (format "mouse-2 or RET jump to org file %s"
4595 (abbreviate-file-name buffer-file-name))))
4596 (regexp (format org-heading-keyword-regexp-format
4597 (cond
4598 ((and org-select-this-todo-keyword
4599 (equal org-select-this-todo-keyword "*"))
4600 org-todo-regexp)
4601 (org-select-this-todo-keyword
4602 (concat "\\("
4603 (mapconcat 'identity
4604 (org-split-string
4605 org-select-this-todo-keyword
4606 "|")
4607 "\\|") "\\)"))
4608 (t org-not-done-regexp))))
4609 marker priority category org-category-pos tags todo-state
4610 ee txt beg end)
4611 (goto-char (point-min))
4612 (while (re-search-forward regexp nil t)
4613 (catch :skip
4614 (save-match-data
4615 (beginning-of-line)
4616 (org-agenda-skip)
4617 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4618 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4619 (goto-char (1+ beg))
4620 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4621 (throw :skip nil)))
4622 (goto-char (match-beginning 2))
4623 (setq marker (org-agenda-new-marker (match-beginning 0))
4624 category (org-get-category)
4625 org-category-pos (get-text-property (point) 'org-category-position)
4626 txt (buffer-substring (match-beginning 2) (match-end 3))
4627 tags (org-get-tags-at (point))
4628 txt (org-agenda-format-item "" txt category tags)
4629 priority (1+ (org-get-priority txt))
4630 todo-state (org-get-todo-state))
4631 (org-add-props txt props
4632 'org-marker marker 'org-hd-marker marker
4633 'priority priority 'org-category category
4634 'org-category-position org-category-pos
4635 'type "todo" 'todo-state todo-state)
4636 (push txt ee)
4637 (if org-agenda-todo-list-sublevels
4638 (goto-char (match-end 2))
4639 (org-end-of-subtree 'invisible))))
4640 (nreverse ee)))
4642 (defun org-agenda-todo-custom-ignore-p (time n)
4643 "Check whether timestamp is farther away then n number of days.
4644 This function is invoked if `org-agenda-todo-ignore-deadlines',
4645 `org-agenda-todo-ignore-scheduled' or
4646 `org-agenda-todo-ignore-timestamp' is set to an integer."
4647 (let ((days (org-days-to-time time)))
4648 (if (>= n 0)
4649 (>= days n)
4650 (<= days n))))
4652 ;;;###autoload
4653 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4654 (&optional end)
4655 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4656 (when (or org-agenda-todo-ignore-with-date
4657 org-agenda-todo-ignore-scheduled
4658 org-agenda-todo-ignore-deadlines
4659 org-agenda-todo-ignore-timestamp)
4660 (setq end (or end (save-excursion (outline-next-heading) (point))))
4661 (save-excursion
4662 (or (and org-agenda-todo-ignore-with-date
4663 (re-search-forward org-ts-regexp end t))
4664 (and org-agenda-todo-ignore-scheduled
4665 (re-search-forward org-scheduled-time-regexp end t)
4666 (cond
4667 ((eq org-agenda-todo-ignore-scheduled 'future)
4668 (> (org-days-to-time (match-string 1)) 0))
4669 ((eq org-agenda-todo-ignore-scheduled 'past)
4670 (<= (org-days-to-time (match-string 1)) 0))
4671 ((numberp org-agenda-todo-ignore-scheduled)
4672 (org-agenda-todo-custom-ignore-p
4673 (match-string 1) org-agenda-todo-ignore-scheduled))
4674 (t)))
4675 (and org-agenda-todo-ignore-deadlines
4676 (re-search-forward org-deadline-time-regexp end t)
4677 (cond
4678 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4679 ((eq org-agenda-todo-ignore-deadlines 'far)
4680 (not (org-deadline-close (match-string 1))))
4681 ((eq org-agenda-todo-ignore-deadlines 'future)
4682 (> (org-days-to-time (match-string 1)) 0))
4683 ((eq org-agenda-todo-ignore-deadlines 'past)
4684 (<= (org-days-to-time (match-string 1)) 0))
4685 ((numberp org-agenda-todo-ignore-deadlines)
4686 (org-agenda-todo-custom-ignore-p
4687 (match-string 1) org-agenda-todo-ignore-deadlines))
4688 (t (org-deadline-close (match-string 1)))))
4689 (and org-agenda-todo-ignore-timestamp
4690 (let ((buffer (current-buffer))
4691 (regexp
4692 (concat
4693 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4694 (start (point)))
4695 ;; Copy current buffer into a temporary one
4696 (with-temp-buffer
4697 (insert-buffer-substring buffer start end)
4698 (goto-char (point-min))
4699 ;; Delete SCHEDULED and DEADLINE items
4700 (while (re-search-forward regexp end t)
4701 (delete-region (match-beginning 0) (match-end 0)))
4702 (goto-char (point-min))
4703 ;; No search for timestamp left
4704 (when (re-search-forward org-ts-regexp nil t)
4705 (cond
4706 ((eq org-agenda-todo-ignore-timestamp 'future)
4707 (> (org-days-to-time (match-string 1)) 0))
4708 ((eq org-agenda-todo-ignore-timestamp 'past)
4709 (<= (org-days-to-time (match-string 1)) 0))
4710 ((numberp org-agenda-todo-ignore-timestamp)
4711 (org-agenda-todo-custom-ignore-p
4712 (match-string 1) org-agenda-todo-ignore-timestamp))
4713 (t))))))))))
4715 (defconst org-agenda-no-heading-message
4716 "No heading for this item in buffer or region.")
4718 (defun org-agenda-get-timestamps ()
4719 "Return the date stamp information for agenda display."
4720 (let* ((props (list 'face nil
4721 'org-not-done-regexp org-not-done-regexp
4722 'org-todo-regexp org-todo-regexp
4723 'org-complex-heading-regexp org-complex-heading-regexp
4724 'mouse-face 'highlight
4725 'help-echo
4726 (format "mouse-2 or RET jump to org file %s"
4727 (abbreviate-file-name buffer-file-name))))
4728 (d1 (calendar-absolute-from-gregorian date))
4729 (remove-re
4730 (concat
4731 (regexp-quote
4732 (format-time-string
4733 "<%Y-%m-%d"
4734 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4735 ".*?>"))
4736 (regexp
4737 (concat
4738 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4739 (regexp-quote
4740 (substring
4741 (format-time-string
4742 (car org-time-stamp-formats)
4743 (apply 'encode-time ; DATE bound by calendar
4744 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4745 1 11))
4746 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4747 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4748 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4749 donep tmp priority category org-category-pos ee txt timestr tags
4750 b0 b3 e3 head todo-state end-of-match show-all)
4751 (goto-char (point-min))
4752 (while (setq end-of-match (re-search-forward regexp nil t))
4753 (setq b0 (match-beginning 0)
4754 b3 (match-beginning 3) e3 (match-end 3)
4755 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4756 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4757 (member todo-state
4758 org-agenda-repeating-timestamp-show-all)))
4759 (catch :skip
4760 (and (org-at-date-range-p) (throw :skip nil))
4761 (org-agenda-skip)
4762 (if (and (match-end 1)
4763 (not (= d1 (org-time-string-to-absolute
4764 (match-string 1) d1 nil show-all
4765 (current-buffer) b0))))
4766 (throw :skip nil))
4767 (if (and e3
4768 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4769 (throw :skip nil))
4770 (setq tmp (buffer-substring (max (point-min)
4771 (- b0 org-ds-keyword-length))
4773 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4774 inactivep (= (char-after b0) ?\[)
4775 deadlinep (string-match org-deadline-regexp tmp)
4776 scheduledp (string-match org-scheduled-regexp tmp)
4777 closedp (and org-agenda-include-inactive-timestamps
4778 (string-match org-closed-string tmp))
4779 clockp (and org-agenda-include-inactive-timestamps
4780 (or (string-match org-clock-string tmp)
4781 (string-match "]-+\\'" tmp)))
4782 donep (member todo-state org-done-keywords))
4783 (if (or scheduledp deadlinep closedp clockp
4784 (and donep org-agenda-skip-timestamp-if-done))
4785 (throw :skip t))
4786 (if (string-match ">" timestr)
4787 ;; substring should only run to end of time stamp
4788 (setq timestr (substring timestr 0 (match-end 0))))
4789 (setq marker (org-agenda-new-marker b0)
4790 category (org-get-category b0)
4791 org-category-pos (get-text-property b0 'org-category-position))
4792 (save-excursion
4793 (if (not (re-search-backward org-outline-regexp-bol nil t))
4794 (setq txt org-agenda-no-heading-message)
4795 (goto-char (match-beginning 0))
4796 (setq hdmarker (org-agenda-new-marker)
4797 tags (org-get-tags-at))
4798 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4799 (setq head (or (match-string 1) ""))
4800 (setq txt (org-agenda-format-item
4801 (if inactivep org-agenda-inactive-leader nil)
4802 head category tags timestr
4803 remove-re)))
4804 (setq priority (org-get-priority txt))
4805 (org-add-props txt props
4806 'org-marker marker 'org-hd-marker hdmarker)
4807 (org-add-props txt nil 'priority priority
4808 'org-category category 'date date
4809 'org-category-position org-category-pos
4810 'todo-state todo-state
4811 'type "timestamp")
4812 (push txt ee))
4813 (if org-agenda-skip-additional-timestamps-same-entry
4814 (outline-next-heading)
4815 (goto-char end-of-match))))
4816 (nreverse ee)))
4818 (defun org-agenda-get-sexps ()
4819 "Return the sexp information for agenda display."
4820 (require 'diary-lib)
4821 (let* ((props (list 'mouse-face 'highlight
4822 'help-echo
4823 (format "mouse-2 or RET jump to org file %s"
4824 (abbreviate-file-name buffer-file-name))))
4825 (regexp "^&?%%(")
4826 marker category org-category-pos ee txt tags entry
4827 result beg b sexp sexp-entry todo-state)
4828 (goto-char (point-min))
4829 (while (re-search-forward regexp nil t)
4830 (catch :skip
4831 (org-agenda-skip)
4832 (setq beg (match-beginning 0))
4833 (goto-char (1- (match-end 0)))
4834 (setq b (point))
4835 (forward-sexp 1)
4836 (setq sexp (buffer-substring b (point)))
4837 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4838 (org-trim (match-string 1))
4839 ""))
4840 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4841 (when result
4842 (setq marker (org-agenda-new-marker beg)
4843 category (org-get-category beg)
4844 org-category-pos (get-text-property beg 'org-category-position)
4845 todo-state (org-get-todo-state))
4847 (dolist (r (if (stringp result)
4848 (list result)
4849 result)) ;; we expect a list here
4850 (if (string-match "\\S-" r)
4851 (setq txt r)
4852 (setq txt "SEXP entry returned empty string"))
4854 (setq txt (org-agenda-format-item
4855 "" txt category tags 'time))
4856 (org-add-props txt props 'org-marker marker)
4857 (org-add-props txt nil
4858 'org-category category 'date date 'todo-state todo-state
4859 'org-category-position org-category-pos
4860 'type "sexp")
4861 (push txt ee)))))
4862 (nreverse ee)))
4864 ;; Calendar sanity: define some functions that are independent of
4865 ;; `calendar-date-style'.
4866 ;; Normally I would like to use ISO format when calling the diary functions,
4867 ;; but to make sure we still have Emacs 22 compatibility we bind
4868 ;; also `european-calendar-style' and use european format
4869 (defun org-anniversary (year month day &optional mark)
4870 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4871 (org-no-warnings
4872 (let ((calendar-date-style 'european) (european-calendar-style t))
4873 (diary-anniversary day month year mark))))
4874 (defun org-cyclic (N year month day &optional mark)
4875 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4876 (org-no-warnings
4877 (let ((calendar-date-style 'european) (european-calendar-style t))
4878 (diary-cyclic N day month year mark))))
4879 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4880 "Like `diary-block', but with fixed (ISO) order of arguments."
4881 (org-no-warnings
4882 (let ((calendar-date-style 'european) (european-calendar-style t))
4883 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4884 (defun org-date (year month day &optional mark)
4885 "Like `diary-date', but with fixed (ISO) order of arguments."
4886 (org-no-warnings
4887 (let ((calendar-date-style 'european) (european-calendar-style t))
4888 (diary-date day month year mark))))
4889 (defalias 'org-float 'diary-float)
4891 ;; Define the` org-class' function
4892 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4893 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4894 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
4895 SKIP-WEEKS is any number of ISO weeks in the block period for which the
4896 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
4897 `holidays', then any date that is known by the Emacs calendar to be a
4898 holidy will also be skipped."
4899 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4900 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4901 (d (calendar-absolute-from-gregorian date)))
4902 (and
4903 (<= date1 d)
4904 (<= d date2)
4905 (= (calendar-day-of-week date) dayname)
4906 (or (not skip-weeks)
4907 (progn
4908 (require 'cal-iso)
4909 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4910 (not (and (memq `holidays' skip-weeks)
4911 (calendar-check-holidays date)))
4912 entry)))
4914 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4915 "Like `org-class', but honor `calendar-date-style'.
4916 The order of the first 2 times 3 arguments depends on the variable
4917 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4918 So for American calendars, give this as MONTH DAY YEAR, for European as
4919 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4920 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4921 is any number of ISO weeks in the block period for which the item should
4922 be skipped.
4924 This function is here only for backward compatibility and it is deprecated,
4925 please use `org-class' instead."
4926 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4927 (date2 (org-order-calendar-date-args m2 d2 y2)))
4928 (org-class
4929 (nth 2 date1) (car date1) (nth 1 date1)
4930 (nth 2 date2) (car date2) (nth 1 date2)
4931 dayname skip-weeks)))
4932 (make-obsolete 'org-diary-class 'org-class "")
4934 (defalias 'org-get-closed 'org-agenda-get-progress)
4935 (defun org-agenda-get-progress ()
4936 "Return the logged TODO entries for agenda display."
4937 (let* ((props (list 'mouse-face 'highlight
4938 'org-not-done-regexp org-not-done-regexp
4939 'org-todo-regexp org-todo-regexp
4940 'org-complex-heading-regexp org-complex-heading-regexp
4941 'help-echo
4942 (format "mouse-2 or RET jump to org file %s"
4943 (abbreviate-file-name buffer-file-name))))
4944 (items (if (consp org-agenda-show-log)
4945 org-agenda-show-log
4946 (if (eq org-agenda-show-log 'clockcheck)
4947 '(clock)
4948 org-agenda-log-mode-items)))
4949 (parts
4950 (delq nil
4951 (list
4952 (if (memq 'closed items) (concat "\\<" org-closed-string))
4953 (if (memq 'clock items) (concat "\\<" org-clock-string))
4954 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4955 (parts-re (if parts (mapconcat 'identity parts "\\|")
4956 (error "`org-agenda-log-mode-items' is empty")))
4957 (regexp (concat
4958 "\\(" parts-re "\\)"
4959 " *\\["
4960 (regexp-quote
4961 (substring
4962 (format-time-string
4963 (car org-time-stamp-formats)
4964 (apply 'encode-time ; DATE bound by calendar
4965 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4966 1 11))))
4967 (org-agenda-search-headline-for-time nil)
4968 marker hdmarker priority category org-category-pos tags closedp
4969 statep clockp state ee txt extra timestr rest clocked)
4970 (goto-char (point-min))
4971 (while (re-search-forward regexp nil t)
4972 (catch :skip
4973 (org-agenda-skip)
4974 (setq marker (org-agenda-new-marker (match-beginning 0))
4975 closedp (equal (match-string 1) org-closed-string)
4976 statep (equal (string-to-char (match-string 1)) ?-)
4977 clockp (not (or closedp statep))
4978 state (and statep (match-string 2))
4979 category (org-get-category (match-beginning 0))
4980 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
4981 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
4982 (when (string-match "\\]" timestr)
4983 ;; substring should only run to end of time stamp
4984 (setq rest (substring timestr (match-end 0))
4985 timestr (substring timestr 0 (match-end 0)))
4986 (if (and (not closedp) (not statep)
4987 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
4988 rest))
4989 (progn (setq timestr (concat (substring timestr 0 -1)
4990 "-" (match-string 1 rest) "]"))
4991 (setq clocked (match-string 2 rest)))
4992 (setq clocked "-")))
4993 (save-excursion
4994 (setq extra
4995 (cond
4996 ((not org-agenda-log-mode-add-notes) nil)
4997 (statep
4998 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4999 (match-string 1)))
5000 (clockp
5001 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5002 (match-string 1)))))
5003 (if (not (re-search-backward org-outline-regexp-bol nil t))
5004 (setq txt org-agenda-no-heading-message)
5005 (goto-char (match-beginning 0))
5006 (setq hdmarker (org-agenda-new-marker)
5007 tags (org-get-tags-at))
5008 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5009 (setq txt (match-string 1))
5010 (when extra
5011 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5012 (setq txt (concat (substring txt 0 (match-beginning 1))
5013 " - " extra " " (match-string 2 txt)))
5014 (setq txt (concat txt " - " extra))))
5015 (setq txt (org-agenda-format-item
5016 (cond
5017 (closedp "Closed: ")
5018 (statep (concat "State: (" state ")"))
5019 (t (concat "Clocked: (" clocked ")")))
5020 txt category tags timestr)))
5021 (setq priority 100000)
5022 (org-add-props txt props
5023 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5024 'priority priority 'org-category category
5025 'org-category-position org-category-pos
5026 'type "closed" 'date date
5027 'undone-face 'org-warning 'done-face 'org-agenda-done)
5028 (push txt ee))
5029 (goto-char (point-at-eol))))
5030 (nreverse ee)))
5032 (defun org-agenda-show-clocking-issues ()
5033 "Add overlays, showing issues with clocking.
5034 See also the user option `org-agenda-clock-consistency-checks'."
5035 (interactive)
5036 (let* ((pl org-agenda-clock-consistency-checks)
5037 (re (concat "^[ \t]*"
5038 org-clock-string
5039 "[ \t]+"
5040 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5041 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5042 (tlstart 0.)
5043 (tlend 0.)
5044 (maxtime (org-hh:mm-string-to-minutes
5045 (or (plist-get pl :max-duration) "24:00")))
5046 (mintime (org-hh:mm-string-to-minutes
5047 (or (plist-get pl :min-duration) 0)))
5048 (maxgap (org-hh:mm-string-to-minutes
5049 ;; default 30:00 means never complain
5050 (or (plist-get pl :max-gap) "30:00")))
5051 (gapok (mapcar 'org-hh:mm-string-to-minutes
5052 (plist-get pl :gap-ok-around)))
5053 (def-face (or (plist-get pl :default-face)
5054 '((:background "DarkRed") (:foreground "white"))))
5055 issue face m te ts dt ov)
5056 (goto-char (point-min))
5057 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5058 (setq issue nil face def-face)
5059 (catch 'next
5060 (setq m (org-get-at-bol 'org-marker)
5061 te nil ts nil)
5062 (unless (and m (markerp m))
5063 (setq issue "No valid clock line") (throw 'next t))
5064 (org-with-point-at m
5065 (save-excursion
5066 (goto-char (point-at-bol))
5067 (unless (looking-at re)
5068 (error "No valid Clock line")
5069 (throw 'next t))
5070 (unless (match-end 3)
5071 (setq issue "No end time"
5072 face (or (plist-get pl :no-end-time-face) face))
5073 (throw 'next t))
5074 (setq ts (match-string 1)
5075 te (match-string 3)
5076 ts (org-float-time
5077 (apply 'encode-time (org-parse-time-string ts)))
5078 te (org-float-time
5079 (apply 'encode-time (org-parse-time-string te)))
5080 dt (- te ts))))
5081 (cond
5082 ((> dt (* 60 maxtime))
5083 ;; a very long clocking chunk
5084 (setq issue (format "Clocking interval is very long: %s"
5085 (org-minutes-to-hh:mm-string
5086 (floor (/ (float dt) 60.))))
5087 face (or (plist-get pl :long-face) face)))
5088 ((< dt (* 60 mintime))
5089 ;; a very short clocking chunk
5090 (setq issue (format "Clocking interval is very short: %s"
5091 (org-minutes-to-hh:mm-string
5092 (floor (/ (float dt) 60.))))
5093 face (or (plist-get pl :short-face) face)))
5094 ((and (> tlend 0) (< ts tlend))
5095 ;; Two clock entries are overlapping
5096 (setq issue (format "Clocking overlap: %d minutes"
5097 (/ (- tlend ts) 60))
5098 face (or (plist-get pl :overlap-face) face)))
5099 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5100 ;; There is a gap, lets see if we need to report it
5101 (unless (org-agenda-check-clock-gap tlend ts gapok)
5102 (setq issue (format "Clocking gap: %d minutes"
5103 (/ (- ts tlend) 60))
5104 face (or (plist-get pl :gap-face) face))))
5105 (t nil)))
5106 (setq tlend (or te tlend) tlstart (or ts tlstart))
5107 (when issue
5108 ;; OK, there was some issue, add an overlay to show the issue
5109 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5110 (overlay-put ov 'before-string
5111 (concat
5112 (org-add-props
5113 (format "%-43s" (concat " " issue))
5115 'face face)
5116 "\n"))
5117 (overlay-put ov 'evaporate t)))))
5119 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5120 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5121 (catch 'exit
5122 (unless ok-list
5123 ;; there are no OK times for gaps...
5124 (throw 'exit nil))
5125 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5126 ;; This is more than 24 hours, so it is OK.
5127 ;; because we have at least one OK time, that must be in the
5128 ;; 24 hour interval.
5129 (throw 'exit t))
5130 ;; We have a shorter gap.
5131 ;; Now we have to get the minute of the day when these times are
5132 (let* ((t1dec (decode-time (seconds-to-time t1)))
5133 (t2dec (decode-time (seconds-to-time t2)))
5134 ;; compute the minute on the day
5135 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5136 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5137 (when (< min2 min1)
5138 ;; if min2 is smaller than min1, this means it is on the next day.
5139 ;; Wrap it to after midnight.
5140 (setq min2 (+ min2 1440)))
5141 ;; Now check if any of the OK times is in the gap
5142 (mapc (lambda (x)
5143 ;; Wrap the time to after midnight if necessary
5144 (if (< x min1) (setq x (+ x 1440)))
5145 ;; Check if in interval
5146 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5147 ok-list)
5148 ;; Nope, this gap is not OK
5149 nil)))
5151 (defun org-agenda-get-deadlines ()
5152 "Return the deadline information for agenda display."
5153 (let* ((props (list 'mouse-face 'highlight
5154 'org-not-done-regexp org-not-done-regexp
5155 'org-todo-regexp org-todo-regexp
5156 'org-complex-heading-regexp org-complex-heading-regexp
5157 'help-echo
5158 (format "mouse-2 or RET jump to org file %s"
5159 (abbreviate-file-name buffer-file-name))))
5160 (regexp org-deadline-time-regexp)
5161 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5162 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5163 d2 diff dfrac wdays pos pos1 category org-category-pos
5164 tags suppress-prewarning ee txt head face s todo-state
5165 show-all upcomingp donep timestr)
5166 (goto-char (point-min))
5167 (while (re-search-forward regexp nil t)
5168 (setq suppress-prewarning nil)
5169 (catch :skip
5170 (org-agenda-skip)
5171 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5172 (save-match-data
5173 (string-match org-scheduled-time-regexp
5174 (buffer-substring (point-at-bol)
5175 (point-at-eol)))))
5176 (setq suppress-prewarning
5177 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5178 org-agenda-skip-deadline-prewarning-if-scheduled
5179 0)))
5180 (setq s (match-string 1)
5181 txt nil
5182 pos (1- (match-beginning 1))
5183 todo-state (save-match-data (org-get-todo-state))
5184 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5185 (member todo-state
5186 org-agenda-repeating-timestamp-show-all))
5187 d2 (org-time-string-to-absolute
5188 (match-string 1) d1 'past show-all
5189 (current-buffer) pos)
5190 diff (- d2 d1)
5191 wdays (if suppress-prewarning
5192 (let ((org-deadline-warning-days suppress-prewarning))
5193 (org-get-wdays s))
5194 (org-get-wdays s))
5195 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5196 upcomingp (and todayp (> diff 0)))
5197 ;; When to show a deadline in the calendar:
5198 ;; If the expiration is within wdays warning time.
5199 ;; Past-due deadlines are only shown on the current date
5200 (if (and (or (and (<= diff wdays)
5201 (and todayp (not org-agenda-only-exact-dates)))
5202 (= diff 0)))
5203 (save-excursion
5204 ;; (setq todo-state (org-get-todo-state))
5205 (setq donep (member todo-state org-done-keywords))
5206 (if (and donep
5207 (or org-agenda-skip-deadline-if-done
5208 (not (= diff 0))))
5209 (setq txt nil)
5210 (setq category (org-get-category)
5211 org-category-pos (get-text-property (point) 'org-category-position))
5212 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5213 (setq txt org-agenda-no-heading-message)
5214 (goto-char (match-end 0))
5215 (setq pos1 (match-beginning 0))
5216 (setq tags (org-get-tags-at pos1))
5217 (setq head (buffer-substring-no-properties
5218 (point)
5219 (progn (skip-chars-forward "^\r\n")
5220 (point))))
5221 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5222 (setq timestr
5223 (concat (substring s (match-beginning 1)) " "))
5224 (setq timestr 'time))
5225 (setq txt (org-agenda-format-item
5226 (if (= diff 0)
5227 (car org-agenda-deadline-leaders)
5228 (if (functionp
5229 (nth 1 org-agenda-deadline-leaders))
5230 (funcall
5231 (nth 1 org-agenda-deadline-leaders)
5232 diff date)
5233 (format (nth 1 org-agenda-deadline-leaders)
5234 diff)))
5235 head category tags
5236 (if (not (= diff 0)) nil timestr)))))
5237 (when txt
5238 (setq face (org-agenda-deadline-face dfrac))
5239 (org-add-props txt props
5240 'org-marker (org-agenda-new-marker pos)
5241 'org-hd-marker (org-agenda-new-marker pos1)
5242 'priority (+ (- diff)
5243 (org-get-priority txt))
5244 'org-category category
5245 'org-category-position org-category-pos
5246 'todo-state todo-state
5247 'type (if upcomingp "upcoming-deadline" "deadline")
5248 'date (if upcomingp date d2)
5249 'face (if donep 'org-agenda-done face)
5250 'undone-face face 'done-face 'org-agenda-done)
5251 (push txt ee))))))
5252 (nreverse ee)))
5254 (defun org-agenda-deadline-face (fraction)
5255 "Return the face to displaying a deadline item.
5256 FRACTION is what fraction of the head-warning time has passed."
5257 (let ((faces org-agenda-deadline-faces) f)
5258 (catch 'exit
5259 (while (setq f (pop faces))
5260 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5262 (defun org-agenda-get-scheduled (&optional deadline-results)
5263 "Return the scheduled information for agenda display."
5264 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5265 'org-todo-regexp org-todo-regexp
5266 'org-complex-heading-regexp org-complex-heading-regexp
5267 'done-face 'org-agenda-done
5268 'mouse-face 'highlight
5269 'help-echo
5270 (format "mouse-2 or RET jump to org file %s"
5271 (abbreviate-file-name buffer-file-name))))
5272 (regexp org-scheduled-time-regexp)
5273 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5274 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5276 (deadline-position-alist
5277 (mapcar (lambda (a) (and (setq mm (get-text-property
5278 0 'org-hd-marker a))
5279 (cons (marker-position mm) a)))
5280 deadline-results))
5281 d2 diff pos pos1 category org-category-pos tags donep
5282 ee txt head pastschedp todo-state face timestr s habitp show-all)
5283 (goto-char (point-min))
5284 (while (re-search-forward regexp nil t)
5285 (catch :skip
5286 (org-agenda-skip)
5287 (setq s (match-string 1)
5288 txt nil
5289 pos (1- (match-beginning 1))
5290 todo-state (save-match-data (org-get-todo-state))
5291 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5292 (member todo-state
5293 org-agenda-repeating-timestamp-show-all))
5294 d2 (org-time-string-to-absolute
5295 (match-string 1) d1 'past show-all
5296 (current-buffer) pos)
5297 diff (- d2 d1))
5298 (setq pastschedp (and todayp (< diff 0)))
5299 ;; When to show a scheduled item in the calendar:
5300 ;; If it is on or past the date.
5301 (when (or (and (< diff 0)
5302 (< (abs diff) org-scheduled-past-days)
5303 (and todayp (not org-agenda-only-exact-dates)))
5304 (= diff 0))
5305 (save-excursion
5306 (setq donep (member todo-state org-done-keywords))
5307 (if (and donep
5308 (or org-agenda-skip-scheduled-if-done
5309 (not (= diff 0))
5310 (and (functionp 'org-is-habit-p)
5311 (org-is-habit-p))))
5312 (setq txt nil)
5313 (setq habitp (and (functionp 'org-is-habit-p)
5314 (org-is-habit-p)))
5315 (setq category (org-get-category)
5316 org-category-pos (get-text-property (point) 'org-category-position))
5317 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5318 (setq txt org-agenda-no-heading-message)
5319 (goto-char (match-end 0))
5320 (setq pos1 (match-beginning 0))
5321 (if habitp
5322 (if (or (not org-habit-show-habits)
5323 (and (not todayp)
5324 org-habit-show-habits-only-for-today))
5325 (throw :skip nil))
5326 (if (and
5327 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5328 (and org-agenda-skip-scheduled-if-deadline-is-shown
5329 pastschedp))
5330 (setq mm (assoc pos1 deadline-position-alist)))
5331 (throw :skip nil)))
5332 (setq tags (org-get-tags-at))
5333 (setq head (buffer-substring-no-properties
5334 (point)
5335 (progn (skip-chars-forward "^\r\n") (point))))
5336 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5337 (setq timestr
5338 (concat (substring s (match-beginning 1)) " "))
5339 (setq timestr 'time))
5340 (setq txt (org-agenda-format-item
5341 (if (= diff 0)
5342 (car org-agenda-scheduled-leaders)
5343 (format (nth 1 org-agenda-scheduled-leaders)
5344 (- 1 diff)))
5345 head category tags
5346 (if (not (= diff 0)) nil timestr)
5347 nil habitp))))
5348 (when txt
5349 (setq face
5350 (cond
5351 ((and (not habitp) pastschedp)
5352 'org-scheduled-previously)
5353 (todayp 'org-scheduled-today)
5354 (t 'org-scheduled))
5355 habitp (and habitp (org-habit-parse-todo)))
5356 (org-add-props txt props
5357 'undone-face face
5358 'face (if donep 'org-agenda-done face)
5359 'org-marker (org-agenda-new-marker pos)
5360 'org-hd-marker (org-agenda-new-marker pos1)
5361 'type (if pastschedp "past-scheduled" "scheduled")
5362 'date (if pastschedp d2 date)
5363 'priority (if habitp
5364 (org-habit-get-priority habitp)
5365 (+ 94 (- 5 diff) (org-get-priority txt)))
5366 'org-category category
5367 'org-category-position org-category-pos
5368 'org-habit-p habitp
5369 'todo-state todo-state)
5370 (push txt ee))))))
5371 (nreverse ee)))
5373 (defun org-agenda-get-blocks ()
5374 "Return the date-range information for agenda display."
5375 (let* ((props (list 'face nil
5376 'org-not-done-regexp org-not-done-regexp
5377 'org-todo-regexp org-todo-regexp
5378 'org-complex-heading-regexp org-complex-heading-regexp
5379 'mouse-face 'highlight
5380 'help-echo
5381 (format "mouse-2 or RET jump to org file %s"
5382 (abbreviate-file-name buffer-file-name))))
5383 (regexp org-tr-regexp)
5384 (d0 (calendar-absolute-from-gregorian date))
5385 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5386 todo-state tags pos head donep)
5387 (goto-char (point-min))
5388 (while (re-search-forward regexp nil t)
5389 (catch :skip
5390 (org-agenda-skip)
5391 (setq pos (point))
5392 (let ((start-time (match-string 1))
5393 (end-time (match-string 2)))
5394 (setq s1 (match-string 1)
5395 s2 (match-string 2)
5396 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5397 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5398 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5399 ;; Only allow days between the limits, because the normal
5400 ;; date stamps will catch the limits.
5401 (save-excursion
5402 (setq todo-state (org-get-todo-state))
5403 (setq donep (member todo-state org-done-keywords))
5404 (if (and donep org-agenda-skip-timestamp-if-done)
5405 (throw :skip t))
5406 (setq marker (org-agenda-new-marker (point)))
5407 (setq category (org-get-category)
5408 org-category-pos (get-text-property (point) 'org-category-position))
5409 (if (not (re-search-backward org-outline-regexp-bol nil t))
5410 (setq txt org-agenda-no-heading-message)
5411 (goto-char (match-beginning 0))
5412 (setq hdmarker (org-agenda-new-marker (point)))
5413 (setq tags (org-get-tags-at))
5414 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5415 (setq head (match-string 1))
5416 (let ((remove-re
5417 (if org-agenda-remove-timeranges-from-blocks
5418 (concat
5419 "<" (regexp-quote s1) ".*?>"
5420 "--"
5421 "<" (regexp-quote s2) ".*?>")
5422 nil)))
5423 (setq txt (org-agenda-format-item
5424 (format
5425 (nth (if (= d1 d2) 0 1)
5426 org-agenda-timerange-leaders)
5427 (1+ (- d0 d1)) (1+ (- d2 d1)))
5428 head category tags
5429 (cond ((and (= d1 d0) (= d2 d0))
5430 (concat "<" start-time ">--<" end-time ">"))
5431 ((= d1 d0)
5432 (concat "<" start-time ">"))
5433 ((= d2 d0)
5434 (concat "<" end-time ">"))
5435 (t nil))
5436 remove-re))))
5437 (org-add-props txt props
5438 'org-marker marker 'org-hd-marker hdmarker
5439 'type "block" 'date date
5440 'todo-state todo-state
5441 'priority (org-get-priority txt) 'org-category category
5442 'org-category-position org-category-pos)
5443 (push txt ee))))
5444 (goto-char pos)))
5445 ;; Sort the entries by expiration date.
5446 (nreverse ee)))
5448 ;;; Agenda presentation and sorting
5450 (defvar org-prefix-has-time nil
5451 "A flag, set by `org-compile-prefix-format'.
5452 The flag is set if the currently compiled format contains a `%t'.")
5453 (defvar org-prefix-has-tag nil
5454 "A flag, set by `org-compile-prefix-format'.
5455 The flag is set if the currently compiled format contains a `%T'.")
5456 (defvar org-prefix-has-effort nil
5457 "A flag, set by `org-compile-prefix-format'.
5458 The flag is set if the currently compiled format contains a `%e'.")
5459 (defvar org-prefix-category-length nil
5460 "Used by `org-compile-prefix-format' to remember the category field width.")
5461 (defvar org-prefix-category-max-length nil
5462 "Used by `org-compile-prefix-format' to remember the category field width.")
5464 (defun org-agenda-get-category-icon (category)
5465 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5466 (dolist (entry org-agenda-category-icon-alist)
5467 (when (org-string-match-p (car entry) category)
5468 (if (listp (cadr entry))
5469 (return (cadr entry))
5470 (return (apply 'create-image (cdr entry)))))))
5472 (defun org-agenda-format-item (extra txt &optional category tags dotime
5473 remove-re habitp)
5474 "Format TXT to be inserted into the agenda buffer.
5475 In particular, it adds the prefix and corresponding text properties. EXTRA
5476 must be a string and replaces the `%s' specifier in the prefix format.
5477 CATEGORY (string, symbol or nil) may be used to overrule the default
5478 category taken from local variable or file name. It will replace the `%c'
5479 specifier in the format. DOTIME, when non-nil, indicates that a
5480 time-of-day should be extracted from TXT for sorting of this entry, and for
5481 the `%t' specifier in the format. When DOTIME is a string, this string is
5482 searched for a time before TXT is. TAGS can be the tags of the headline.
5483 Any match of REMOVE-RE will be removed from TXT."
5484 (save-match-data
5485 ;; Diary entries sometimes have extra whitespace at the beginning
5486 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5488 ;; Fix the tags part in txt
5489 (setq txt (org-agenda-fix-displayed-tags
5490 txt tags
5491 org-agenda-show-inherited-tags
5492 org-agenda-hide-tags-regexp))
5493 (let* ((category (or category
5494 (if (stringp org-category)
5495 org-category
5496 (and org-category (symbol-name org-category)))
5497 (if buffer-file-name
5498 (file-name-sans-extension
5499 (file-name-nondirectory buffer-file-name))
5500 "")))
5501 (category-icon (org-agenda-get-category-icon category))
5502 (category-icon (if category-icon
5503 (propertize " " 'display category-icon)
5504 ""))
5505 ;; time, tag, effort are needed for the eval of the prefix format
5506 (tag (if tags (nth (1- (length tags)) tags) ""))
5507 time effort neffort
5508 (ts (if dotime (concat
5509 (if (stringp dotime) dotime "")
5510 (and org-agenda-search-headline-for-time txt))))
5511 (time-of-day (and dotime (org-get-time-of-day ts)))
5512 stamp plain s0 s1 s2 rtn srp l
5513 duration thecategory)
5514 (and (eq major-mode 'org-mode) buffer-file-name
5515 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5516 (when (and dotime time-of-day)
5517 ;; Extract starting and ending time and move them to prefix
5518 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5519 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5520 (setq s0 (match-string 0 ts)
5521 srp (and stamp (match-end 3))
5522 s1 (match-string (if plain 1 2) ts)
5523 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5525 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5526 ;; them, we might want to remove them there to avoid duplication.
5527 ;; The user can turn this off with a variable.
5528 (if (and org-prefix-has-time
5529 org-agenda-remove-times-when-in-prefix (or stamp plain)
5530 (string-match (concat (regexp-quote s0) " *") txt)
5531 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5532 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5533 (= (match-beginning 0) 0)
5535 (setq txt (replace-match "" nil nil txt))))
5536 ;; Normalize the time(s) to 24 hour
5537 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5538 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5540 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5541 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5542 (setq s2
5543 (org-minutes-to-hh:mm-string
5544 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5546 ;; Compute the duration
5547 (when s2
5548 (setq duration (- (org-hh:mm-string-to-minutes s2)
5549 (org-hh:mm-string-to-minutes s1)))))
5551 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5552 txt)
5553 ;; Tags are in the string
5554 (if (or (eq org-agenda-remove-tags t)
5555 (and org-agenda-remove-tags
5556 org-prefix-has-tag))
5557 (setq txt (replace-match "" t t txt))
5558 (setq txt (replace-match
5559 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5560 (match-string 2 txt))
5561 t t txt))))
5562 (when (eq major-mode 'org-mode)
5563 (setq effort
5564 (condition-case nil
5565 (org-get-effort
5566 (or (get-text-property 0 'org-hd-marker txt)
5567 (get-text-property 0 'org-marker txt)))
5568 (error nil)))
5569 (when effort
5570 (setq neffort (org-duration-string-to-minutes effort)
5571 effort (setq effort (concat "[" effort "]")))))
5572 ;; prevent erroring out with %e format when there is no effort
5573 (or effort (setq effort ""))
5575 (when remove-re
5576 (while (string-match remove-re txt)
5577 (setq txt (replace-match "" t t txt))))
5579 ;; Set org-heading property on `txt' to mark the start of the
5580 ;; heading.
5581 (add-text-properties 0 (length txt) '(org-heading t) txt)
5583 ;; Prepare the variables needed in the eval of the compiled format
5584 (setq time (cond (s2 (concat
5585 (org-agenda-time-of-day-to-ampm-maybe s1)
5586 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5587 (if org-agenda-timegrid-use-ampm " ")))
5588 (s1 (concat
5589 (org-agenda-time-of-day-to-ampm-maybe s1)
5590 (if org-agenda-timegrid-use-ampm
5591 "........ "
5592 "......")))
5593 (t ""))
5594 extra (or (and (not habitp) extra) "")
5595 category (if (symbolp category) (symbol-name category) category)
5596 thecategory (copy-sequence category))
5597 (if (string-match org-bracket-link-regexp category)
5598 (progn
5599 (setq l (if (match-end 3)
5600 (- (match-end 3) (match-beginning 3))
5601 (- (match-end 1) (match-beginning 1))))
5602 (when (< l (or org-prefix-category-length 0))
5603 (setq category (copy-sequence category))
5604 (org-add-props category nil
5605 'extra-space (make-string
5606 (- org-prefix-category-length l 1) ?\ ))))
5607 (if (and org-prefix-category-max-length
5608 (>= (length category) org-prefix-category-max-length))
5609 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5610 ;; Evaluate the compiled format
5611 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5613 ;; And finally add the text properties
5614 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5615 (org-add-props rtn nil
5616 'org-category (if thecategory (downcase thecategory) category)
5617 'tags (mapcar 'org-downcase-keep-props tags)
5618 'org-highest-priority org-highest-priority
5619 'org-lowest-priority org-lowest-priority
5620 'time-of-day time-of-day
5621 'duration duration
5622 'effort effort
5623 'effort-minutes neffort
5624 'txt txt
5625 'time time
5626 'extra extra
5627 'format org-prefix-format-compiled
5628 'dotime dotime))))
5630 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5631 "Remove tags string from TXT, and add a modified list of tags.
5632 The modified list may contain inherited tags, and tags matched by
5633 `org-agenda-hide-tags-regexp' will be removed."
5634 (when (or add-inherited hide-re)
5635 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5636 (setq txt (substring txt 0 (match-beginning 0))))
5637 (setq tags
5638 (delq nil
5639 (mapcar (lambda (tg)
5640 (if (or (and hide-re (string-match hide-re tg))
5641 (and (not add-inherited)
5642 (get-text-property 0 'inherited tg)))
5644 tg))
5645 tags)))
5646 (when tags
5647 (let ((have-i (get-text-property 0 'inherited (car tags)))
5649 (setq txt (concat txt " :"
5650 (mapconcat
5651 (lambda (x)
5652 (setq i (get-text-property 0 'inherited x))
5653 (if (and have-i (not i))
5654 (progn
5655 (setq have-i nil)
5656 (concat ":" x))
5658 tags ":")
5659 (if have-i "::" ":"))))))
5660 txt)
5662 (defun org-downcase-keep-props (s)
5663 (let ((props (text-properties-at 0 s)))
5664 (setq s (downcase s))
5665 (add-text-properties 0 (length s) props s)
5668 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5669 (defvar org-agenda-sorting-strategy-selected nil)
5671 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5672 (catch 'exit
5673 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5674 ((and todayp (member 'today (car org-agenda-time-grid))))
5675 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5676 ((member 'weekly (car org-agenda-time-grid)))
5677 (t (throw 'exit list)))
5678 (let* ((have (delq nil (mapcar
5679 (lambda (x) (get-text-property 1 'time-of-day x))
5680 list)))
5681 (string (nth 1 org-agenda-time-grid))
5682 (gridtimes (nth 2 org-agenda-time-grid))
5683 (req (car org-agenda-time-grid))
5684 (remove (member 'remove-match req))
5685 new time)
5686 (if (and (member 'require-timed req) (not have))
5687 ;; don't show empty grid
5688 (throw 'exit list))
5689 (while (setq time (pop gridtimes))
5690 (unless (and remove (member time have))
5691 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5692 (push (org-agenda-format-item
5693 nil string "" nil
5694 (concat (substring time 0 -2) ":" (substring time -2)))
5695 new)
5696 (put-text-property
5697 2 (length (car new)) 'face 'org-time-grid (car new))))
5698 (when (and todayp org-agenda-show-current-time-in-grid)
5699 (push (org-agenda-format-item
5701 org-agenda-current-time-string
5702 "" nil
5703 (format-time-string "%H:%M "))
5704 new)
5705 (put-text-property
5706 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5708 (if (member 'time-up org-agenda-sorting-strategy-selected)
5709 (append new list)
5710 (append list new)))))
5712 (defun org-compile-prefix-format (key)
5713 "Compile the prefix format into a Lisp form that can be evaluated.
5714 The resulting form is returned and stored in the variable
5715 `org-prefix-format-compiled'."
5716 (setq org-prefix-has-time nil org-prefix-has-tag nil
5717 org-prefix-category-length nil
5718 org-prefix-has-effort nil)
5719 (let ((s (cond
5720 ((stringp org-agenda-prefix-format)
5721 org-agenda-prefix-format)
5722 ((assq key org-agenda-prefix-format)
5723 (cdr (assq key org-agenda-prefix-format)))
5724 (t " %-12:c%?-12t% s")))
5725 (start 0)
5726 varform vars var e c f opt)
5727 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5728 s start)
5729 (setq var (or (cdr (assoc (match-string 4 s)
5730 '(("c" . category) ("t" . time) ("s" . extra)
5731 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5732 'eval)
5733 c (or (match-string 3 s) "")
5734 opt (match-beginning 1)
5735 start (1+ (match-beginning 0)))
5736 (if (equal var 'time) (setq org-prefix-has-time t))
5737 (if (equal var 'tag) (setq org-prefix-has-tag t))
5738 (if (equal var 'effort) (setq org-prefix-has-effort t))
5739 (setq f (concat "%" (match-string 2 s) "s"))
5740 (when (equal var 'category)
5741 (setq org-prefix-category-length
5742 (floor (abs (string-to-number (match-string 2 s)))))
5743 (setq org-prefix-category-max-length
5744 (let ((x (match-string 2 s)))
5745 (save-match-data
5746 (if (string-match "\\.[0-9]+" x)
5747 (string-to-number (substring (match-string 0 x) 1)))))))
5748 (if (eq var 'eval)
5749 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5750 (if opt
5751 (setq varform
5752 `(if (equal "" ,var)
5754 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5755 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5756 (setq s (replace-match "%s" t nil s))
5757 (push varform vars))
5758 (setq vars (nreverse vars))
5759 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5761 (defun org-set-sorting-strategy (key)
5762 (if (symbolp (car org-agenda-sorting-strategy))
5763 ;; the old format
5764 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5765 (setq org-agenda-sorting-strategy-selected
5766 (or (cdr (assq key org-agenda-sorting-strategy))
5767 (cdr (assq 'agenda org-agenda-sorting-strategy))
5768 '(time-up category-keep priority-down)))))
5770 (defun org-get-time-of-day (s &optional string mod24)
5771 "Check string S for a time of day.
5772 If found, return it as a military time number between 0 and 2400.
5773 If not found, return nil.
5774 The optional STRING argument forces conversion into a 5 character wide string
5775 HH:MM."
5776 (save-match-data
5777 (when
5778 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5779 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5780 (let* ((h (string-to-number (match-string 1 s)))
5781 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5782 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5783 (am-p (equal ampm "am"))
5784 (h1 (cond ((not ampm) h)
5785 ((= h 12) (if am-p 0 12))
5786 (t (+ h (if am-p 0 12)))))
5787 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5788 (mod h1 24) h1))
5789 (t0 (+ (* 100 h2) m))
5790 (t1 (concat (if (>= h1 24) "+" " ")
5791 (if (and org-agenda-time-leading-zero
5792 (< t0 1000)) "0" "")
5793 (if (< t0 100) "0" "")
5794 (if (< t0 10) "0" "")
5795 (int-to-string t0))))
5796 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5798 (defvar org-agenda-before-sorting-filter-function nil
5799 "Function to be applied to agenda items prior to sorting.
5800 Prior to sorting also means just before they are inserted into the agenda.
5802 To aid sorting, you may revisit the original entries and add more text
5803 properties which will later be used by the sorting functions.
5805 The function should take a string argument, an agenda line.
5806 It has access to the text properties in that line, which contain among
5807 other things, the property `org-hd-marker' that points to the entry
5808 where the line comes from. Note that not all lines going into the agenda
5809 have this property, only most.
5811 The function should return the modified string. It is probably best
5812 to ONLY change text properties.
5814 You can also use this function as a filter, by returning nil for lines
5815 you don't want to have in the agenda at all. For this application, you
5816 could bind the variable in the options section of a custom command.")
5818 (defun org-finalize-agenda-entries (list &optional nosort)
5819 "Sort and concatenate the agenda items."
5820 (setq list (mapcar 'org-agenda-highlight-todo list))
5821 (if nosort
5822 list
5823 (when org-agenda-before-sorting-filter-function
5824 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5825 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5827 (defun org-agenda-highlight-todo (x)
5828 (let ((org-done-keywords org-done-keywords-for-agenda)
5829 (case-fold-search nil)
5831 (if (eq x 'line)
5832 (save-excursion
5833 (beginning-of-line 1)
5834 (setq re (org-get-at-bol 'org-todo-regexp))
5835 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5836 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5837 (add-text-properties (match-beginning 0) (match-end 1)
5838 (list 'face (org-get-todo-face 1)))
5839 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5840 (delete-region (match-beginning 1) (1- (match-end 0)))
5841 (goto-char (match-beginning 1))
5842 (insert (format org-agenda-todo-keyword-format s)))))
5843 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5844 (setq re (get-text-property 0 'org-todo-regexp x))
5845 (when (and re
5846 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5847 x (or pl 0)) pl))
5848 (add-text-properties
5849 (or (match-end 1) (match-end 0)) (match-end 0)
5850 (list 'face (org-get-todo-face (match-string 2 x)))
5852 (when (match-end 1)
5853 (setq x (concat (substring x 0 (match-end 1))
5854 (format org-agenda-todo-keyword-format
5855 (match-string 2 x))
5856 (org-add-props " " (text-properties-at 0 x))
5857 (substring x (match-end 3)))))))
5858 x)))
5860 (defsubst org-cmp-priority (a b)
5861 "Compare the priorities of string A and B."
5862 (let ((pa (or (get-text-property 1 'priority a) 0))
5863 (pb (or (get-text-property 1 'priority b) 0)))
5864 (cond ((> pa pb) +1)
5865 ((< pa pb) -1)
5866 (t nil))))
5868 (defsubst org-cmp-effort (a b)
5869 "Compare the effort values of string A and B."
5870 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5871 (ea (or (get-text-property 1 'effort-minutes a) def))
5872 (eb (or (get-text-property 1 'effort-minutes b) def)))
5873 (cond ((> ea eb) +1)
5874 ((< ea eb) -1)
5875 (t nil))))
5877 (defsubst org-cmp-category (a b)
5878 "Compare the string values of categories of strings A and B."
5879 (let ((ca (or (get-text-property 1 'org-category a) ""))
5880 (cb (or (get-text-property 1 'org-category b) "")))
5881 (cond ((string-lessp ca cb) -1)
5882 ((string-lessp cb ca) +1)
5883 (t nil))))
5885 (defsubst org-cmp-todo-state (a b)
5886 "Compare the todo states of strings A and B."
5887 (let* ((ma (or (get-text-property 1 'org-marker a)
5888 (get-text-property 1 'org-hd-marker a)))
5889 (mb (or (get-text-property 1 'org-marker b)
5890 (get-text-property 1 'org-hd-marker b)))
5891 (fa (and ma (marker-buffer ma)))
5892 (fb (and mb (marker-buffer mb)))
5893 (todo-kwds
5894 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5895 (and fb (with-current-buffer fb org-todo-keywords-1))))
5896 (ta (or (get-text-property 1 'todo-state a) ""))
5897 (tb (or (get-text-property 1 'todo-state b) ""))
5898 (la (- (length (member ta todo-kwds))))
5899 (lb (- (length (member tb todo-kwds))))
5900 (donepa (member ta org-done-keywords-for-agenda))
5901 (donepb (member tb org-done-keywords-for-agenda)))
5902 (cond ((and donepa (not donepb)) -1)
5903 ((and (not donepa) donepb) +1)
5904 ((< la lb) -1)
5905 ((< lb la) +1)
5906 (t nil))))
5908 (defsubst org-cmp-alpha (a b)
5909 "Compare the headlines, alphabetically."
5910 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5911 (plb (text-property-any 0 (length b) 'org-heading t b))
5912 (ta (and pla (substring a pla)))
5913 (tb (and plb (substring b plb))))
5914 (when pla
5915 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5916 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5917 (setq ta (substring ta (match-end 0))))
5918 (setq ta (downcase ta)))
5919 (when plb
5920 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5921 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5922 (setq tb (substring tb (match-end 0))))
5923 (setq tb (downcase tb)))
5924 (cond ((not ta) +1)
5925 ((not tb) -1)
5926 ((string-lessp ta tb) -1)
5927 ((string-lessp tb ta) +1)
5928 (t nil))))
5930 (defsubst org-cmp-tag (a b)
5931 "Compare the string values of the first tags of A and B."
5932 (let ((ta (car (last (get-text-property 1 'tags a))))
5933 (tb (car (last (get-text-property 1 'tags b)))))
5934 (cond ((not ta) +1)
5935 ((not tb) -1)
5936 ((string-lessp ta tb) -1)
5937 ((string-lessp tb ta) +1)
5938 (t nil))))
5940 (defsubst org-cmp-time (a b)
5941 "Compare the time-of-day values of strings A and B."
5942 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5943 (ta (or (get-text-property 1 'time-of-day a) def))
5944 (tb (or (get-text-property 1 'time-of-day b) def)))
5945 (cond ((< ta tb) -1)
5946 ((< tb ta) +1)
5947 (t nil))))
5949 (defsubst org-cmp-habit-p (a b)
5950 "Compare the todo states of strings A and B."
5951 (let ((ha (get-text-property 1 'org-habit-p a))
5952 (hb (get-text-property 1 'org-habit-p b)))
5953 (cond ((and ha (not hb)) -1)
5954 ((and (not ha) hb) +1)
5955 (t nil))))
5957 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5959 (defun org-entries-lessp (a b)
5960 "Predicate for sorting agenda entries."
5961 ;; The following variables will be used when the form is evaluated.
5962 ;; So even though the compiler complains, keep them.
5963 (let* ((ss org-agenda-sorting-strategy-selected)
5964 (time-up (and (org-em 'time-up 'time-down ss)
5965 (org-cmp-time a b)))
5966 (time-down (if time-up (- time-up) nil))
5967 (priority-up (and (org-em 'priority-up 'priority-down ss)
5968 (org-cmp-priority a b)))
5969 (priority-down (if priority-up (- priority-up) nil))
5970 (effort-up (and (org-em 'effort-up 'effort-down ss)
5971 (org-cmp-effort a b)))
5972 (effort-down (if effort-up (- effort-up) nil))
5973 (category-up (and (or (org-em 'category-up 'category-down ss)
5974 (memq 'category-keep ss))
5975 (org-cmp-category a b)))
5976 (category-down (if category-up (- category-up) nil))
5977 (category-keep (if category-up +1 nil))
5978 (tag-up (and (org-em 'tag-up 'tag-down ss)
5979 (org-cmp-tag a b)))
5980 (tag-down (if tag-up (- tag-up) nil))
5981 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5982 (org-cmp-todo-state a b)))
5983 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5984 (habit-up (and (org-em 'habit-up 'habit-down ss)
5985 (org-cmp-habit-p a b)))
5986 (habit-down (if habit-up (- habit-up) nil))
5987 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5988 (org-cmp-alpha a b)))
5989 (alpha-down (if alpha-up (- alpha-up) nil))
5990 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5991 user-defined-up user-defined-down)
5992 (if (and need-user-cmp org-agenda-cmp-user-defined
5993 (functionp org-agenda-cmp-user-defined))
5994 (setq user-defined-up
5995 (funcall org-agenda-cmp-user-defined a b)
5996 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5997 (cdr (assoc
5998 (eval (cons 'or org-agenda-sorting-strategy-selected))
5999 '((-1 . t) (1 . nil) (nil . nil))))))
6001 ;;; Agenda restriction lock
6003 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6004 "Overlay to mark the headline to which agenda commands are restricted.")
6005 (overlay-put org-agenda-restriction-lock-overlay
6006 'face 'org-agenda-restriction-lock)
6007 (overlay-put org-agenda-restriction-lock-overlay
6008 'help-echo "Agendas are currently limited to this subtree.")
6009 (org-detach-overlay org-agenda-restriction-lock-overlay)
6011 (defun org-agenda-set-restriction-lock (&optional type)
6012 "Set restriction lock for agenda, to current subtree or file.
6013 Restriction will be the file if TYPE is `file', or if type is the
6014 universal prefix '(4), or if the cursor is before the first headline
6015 in the file. Otherwise, restriction will be to the current subtree."
6016 (interactive "P")
6017 (and (equal type '(4)) (setq type 'file))
6018 (setq type (cond
6019 (type type)
6020 ((org-at-heading-p) 'subtree)
6021 ((condition-case nil (org-back-to-heading t) (error nil))
6022 'subtree)
6023 (t 'file)))
6024 (if (eq type 'subtree)
6025 (progn
6026 (setq org-agenda-restrict t)
6027 (setq org-agenda-overriding-restriction 'subtree)
6028 (put 'org-agenda-files 'org-restrict
6029 (list (buffer-file-name (buffer-base-buffer))))
6030 (org-back-to-heading t)
6031 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6032 (move-marker org-agenda-restrict-begin (point))
6033 (move-marker org-agenda-restrict-end
6034 (save-excursion (org-end-of-subtree t)))
6035 (message "Locking agenda restriction to subtree"))
6036 (put 'org-agenda-files 'org-restrict
6037 (list (buffer-file-name (buffer-base-buffer))))
6038 (setq org-agenda-restrict nil)
6039 (setq org-agenda-overriding-restriction 'file)
6040 (move-marker org-agenda-restrict-begin nil)
6041 (move-marker org-agenda-restrict-end nil)
6042 (message "Locking agenda restriction to file"))
6043 (setq current-prefix-arg nil)
6044 (org-agenda-maybe-redo))
6046 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6047 "Remove the agenda restriction lock."
6048 (interactive "P")
6049 (org-detach-overlay org-agenda-restriction-lock-overlay)
6050 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6051 (setq org-agenda-overriding-restriction nil)
6052 (setq org-agenda-restrict nil)
6053 (put 'org-agenda-files 'org-restrict nil)
6054 (move-marker org-agenda-restrict-begin nil)
6055 (move-marker org-agenda-restrict-end nil)
6056 (setq current-prefix-arg nil)
6057 (message "Agenda restriction lock removed")
6058 (or noupdate (org-agenda-maybe-redo)))
6060 (defun org-agenda-maybe-redo ()
6061 "If there is any window showing the agenda view, update it."
6062 (let ((w (get-buffer-window org-agenda-buffer-name t))
6063 (w0 (selected-window)))
6064 (when w
6065 (select-window w)
6066 (org-agenda-redo)
6067 (select-window w0)
6068 (if org-agenda-overriding-restriction
6069 (message "Agenda view shifted to new %s restriction"
6070 org-agenda-overriding-restriction)
6071 (message "Agenda restriction lock removed")))))
6073 ;;; Agenda commands
6075 (defun org-agenda-check-type (error &rest types)
6076 "Check if agenda buffer is of allowed type.
6077 If ERROR is non-nil, throw an error, otherwise just return nil."
6078 (if (memq org-agenda-type types)
6080 (if error
6081 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6082 nil)))
6084 (defun org-agenda-quit ()
6085 "Exit agenda by removing the window or the buffer."
6086 (interactive)
6087 (if org-agenda-columns-active
6088 (org-columns-quit)
6089 (let ((buf (current-buffer)))
6090 (if (eq org-agenda-window-setup 'other-frame)
6091 (progn
6092 (kill-buffer buf)
6093 (org-agenda-reset-markers)
6094 (org-columns-remove-overlays)
6095 (setq org-agenda-archives-mode nil)
6096 (delete-frame))
6097 (and (not (eq org-agenda-window-setup 'current-window))
6098 (not (one-window-p))
6099 (delete-window))
6100 (kill-buffer buf)
6101 (org-agenda-reset-markers)
6102 (org-columns-remove-overlays)
6103 (setq org-agenda-archives-mode nil)))
6104 ;; Maybe restore the pre-agenda window configuration.
6105 (and org-agenda-restore-windows-after-quit
6106 (not (eq org-agenda-window-setup 'other-frame))
6107 org-pre-agenda-window-conf
6108 (set-window-configuration org-pre-agenda-window-conf))))
6110 (defun org-agenda-exit ()
6111 "Exit agenda by removing the window or the buffer.
6112 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6113 Org-mode buffers visited directly by the user will not be touched."
6114 (interactive)
6115 (org-release-buffers org-agenda-new-buffers)
6116 (setq org-agenda-new-buffers nil)
6117 (org-agenda-quit))
6119 (defun org-agenda-execute (arg)
6120 "Execute another agenda command, keeping same window.
6121 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6122 in the agenda."
6123 (interactive "P")
6124 (let ((org-agenda-window-setup 'current-window))
6125 (org-agenda arg)))
6127 (defun org-agenda-redo ()
6128 "Rebuild Agenda.
6129 When this is the global TODO list, a prefix argument will be interpreted."
6130 (interactive)
6131 (let* ((org-agenda-keep-modes t)
6132 (filter org-agenda-filter)
6133 (preset (get 'org-agenda-filter :preset-filter))
6134 (org-agenda-filter-while-redo (or filter preset))
6135 (cols org-agenda-columns-active)
6136 (line (org-current-line))
6137 (window-line (- line (org-current-line (window-start))))
6138 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6139 (put 'org-agenda-filter :preset-filter nil)
6140 (and cols (org-columns-quit))
6141 (message "Rebuilding agenda buffer...")
6142 (org-let lprops '(eval org-agenda-redo-command))
6143 (setq org-agenda-undo-list nil
6144 org-agenda-pending-undo-list nil)
6145 (message "Rebuilding agenda buffer...done")
6146 (put 'org-agenda-filter :preset-filter preset)
6147 (and (or filter preset) (org-agenda-filter-apply filter))
6148 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6149 (org-goto-line line)
6150 (recenter window-line)))
6153 (defvar org-global-tags-completion-table nil)
6154 (defvar org-agenda-filter-form nil)
6155 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6156 "Keep only those lines in the agenda buffer that have a specific tag.
6157 The tag is selected with its fast selection letter, as configured.
6158 With prefix argument STRIP, remove all lines that do have the tag.
6159 A lisp caller can specify CHAR. NARROW means that the new tag should be
6160 used to narrow the search - the interactive user can also press `-' or `+'
6161 to switch to narrowing."
6162 (interactive "P")
6163 (let* ((alist org-tag-alist-for-agenda)
6164 (tag-chars (mapconcat
6165 (lambda (x) (if (and (not (symbolp (car x)))
6166 (cdr x))
6167 (char-to-string (cdr x))
6168 ""))
6169 alist ""))
6170 (efforts (org-split-string
6171 (or (cdr (assoc (concat org-effort-property "_ALL")
6172 org-global-properties))
6173 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6174 "")))
6175 (effort-op org-agenda-filter-effort-default-operator)
6176 (effort-prompt "")
6177 (inhibit-read-only t)
6178 (current org-agenda-filter)
6179 maybe-refresh a n tag)
6180 (unless char
6181 (message
6182 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6183 (if narrow "Narrow" "Filter") tag-chars
6184 (if org-agenda-auto-exclude-function "[RET], " ""))
6185 (setq char (read-char)))
6186 (when (member char '(?+ ?-))
6187 ;; Narrowing down
6188 (cond ((equal char ?-) (setq strip t narrow t))
6189 ((equal char ?+) (setq strip nil narrow t)))
6190 (message
6191 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6192 (setq char (read-char)))
6193 (when (member char '(?< ?> ?= ??))
6194 ;; An effort operator
6195 (setq effort-op (char-to-string char))
6196 (setq alist nil) ; to make sure it will be interpreted as effort.
6197 (unless (equal char ??)
6198 (loop for i from 0 to 9 do
6199 (setq effort-prompt
6200 (concat
6201 effort-prompt " ["
6202 (if (= i 9) "0" (int-to-string (1+ i)))
6203 "]" (nth i efforts))))
6204 (message "Effort%s: %s " effort-op effort-prompt)
6205 (setq char (read-char))
6206 (when (or (< char ?0) (> char ?9))
6207 (error "Need 1-9,0 to select effort" ))))
6208 (when (equal char ?\t)
6209 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6210 (org-set-local 'org-global-tags-completion-table
6211 (org-global-tags-completion-table)))
6212 (let ((completion-ignore-case t))
6213 (setq tag (org-icompleting-read
6214 "Tag: " org-global-tags-completion-table))))
6215 (cond
6216 ((equal char ?\r)
6217 (org-agenda-filter-by-tag-show-all)
6218 (when org-agenda-auto-exclude-function
6219 (setq org-agenda-filter '())
6220 (dolist (tag (org-agenda-get-represented-tags))
6221 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6222 (if modifier
6223 (push modifier org-agenda-filter))))
6224 (if (not (null org-agenda-filter))
6225 (org-agenda-filter-apply org-agenda-filter)))
6226 (setq maybe-refresh t))
6227 ((equal char ?/)
6228 (org-agenda-filter-by-tag-show-all)
6229 (when (get 'org-agenda-filter :preset-filter)
6230 (org-agenda-filter-apply org-agenda-filter))
6231 (setq maybe-refresh t))
6232 ((or (equal char ?\ )
6233 (setq a (rassoc char alist))
6234 (and (>= char ?0) (<= char ?9)
6235 (setq n (if (= char ?0) 9 (- char ?0 1))
6236 tag (concat effort-op (nth n efforts))
6237 a (cons tag nil)))
6238 (and (= char ??)
6239 (setq tag "?eff")
6240 a (cons tag nil))
6241 (and tag (setq a (cons tag nil))))
6242 (org-agenda-filter-by-tag-show-all)
6243 (setq tag (car a))
6244 (setq org-agenda-filter
6245 (cons (concat (if strip "-" "+") tag)
6246 (if narrow current nil)))
6247 (org-agenda-filter-apply org-agenda-filter)
6248 (setq maybe-refresh t))
6249 (t (error "Invalid tag selection character %c" char)))
6250 (when (and maybe-refresh
6251 (eq org-agenda-clockreport-mode 'with-filter))
6252 (org-agenda-redo))))
6254 (defun org-agenda-get-represented-tags ()
6255 "Get a list of all tags currently represented in the agenda."
6256 (let (p tags)
6257 (save-excursion
6258 (goto-char (point-min))
6259 (while (setq p (next-single-property-change (point) 'tags))
6260 (goto-char p)
6261 (mapc (lambda (x) (add-to-list 'tags x))
6262 (get-text-property (point) 'tags))))
6263 tags))
6265 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6266 "Refine the current filter. See `org-agenda-filter-by-tag'."
6267 (interactive "P")
6268 (org-agenda-filter-by-tag strip char 'refine))
6270 (defun org-agenda-filter-make-matcher ()
6271 "Create the form that tests a line for the agenda filter."
6272 (let (f f1)
6273 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6274 org-agenda-filter))
6275 (if (member x '("-" "+"))
6276 (setq f1 (if (equal x "-") 'tags '(not tags)))
6277 (if (string-match "[<=>?]" x)
6278 (setq f1 (org-agenda-filter-effort-form x))
6279 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6280 (if (equal (string-to-char x) ?-)
6281 (setq f1 (list 'not f1))))
6282 (push f1 f))
6283 (cons 'and (nreverse f))))
6285 (defun org-agenda-filter-effort-form (e)
6286 "Return the form to compare the effort of the current line with what E says.
6287 E looks like \"+<2:25\"."
6288 (let (op)
6289 (setq e (substring e 1))
6290 (setq op (string-to-char e) e (substring e 1))
6291 (setq op (cond ((equal op ?<) '<=)
6292 ((equal op ?>) '>=)
6293 ((equal op ??) op)
6294 (t '=)))
6295 (list 'org-agenda-compare-effort (list 'quote op)
6296 (org-duration-string-to-minutes e))))
6298 (defun org-agenda-compare-effort (op value)
6299 "Compare the effort of the current line with VALUE, using OP.
6300 If the line does not have an effort defined, return nil."
6301 (let ((eff (org-get-at-bol 'effort-minutes)))
6302 (if (equal op ??)
6303 (not eff)
6304 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6305 value))))
6307 (defun org-agenda-filter-apply (filter)
6308 "Set FILTER as the new agenda filter and apply it."
6309 (let (tags)
6310 (setq org-agenda-filter filter
6311 org-agenda-filter-form (org-agenda-filter-make-matcher))
6312 (org-agenda-set-mode-name)
6313 (save-excursion
6314 (goto-char (point-min))
6315 (while (not (eobp))
6316 (if (org-get-at-bol 'org-marker)
6317 (progn
6318 (setq tags (org-get-at-bol 'tags)) ; used in eval
6319 (if (not (eval org-agenda-filter-form))
6320 (org-agenda-filter-by-tag-hide-line))
6321 (beginning-of-line 2))
6322 (beginning-of-line 2))))
6323 (if (get-char-property (point) 'invisible)
6324 (org-agenda-previous-line))))
6326 (defun org-agenda-filter-by-tag-hide-line ()
6327 (let (ov)
6328 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6329 (point-at-eol)))
6330 (overlay-put ov 'invisible t)
6331 (overlay-put ov 'type 'tags-filter)
6332 (push ov org-agenda-filter-overlays)))
6334 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6335 (setq pos (or pos (point)))
6336 (save-excursion
6337 (dolist (ov (overlays-at pos))
6338 (when (and (overlay-get ov 'invisible)
6339 (eq (overlay-get ov 'type) 'tags-filter))
6340 (goto-char pos)
6341 (if (< (overlay-start ov) (point-at-eol))
6342 (move-overlay ov (point-at-eol)
6343 (overlay-end ov)))))))
6345 (defun org-agenda-filter-by-tag-show-all ()
6346 (mapc 'delete-overlay org-agenda-filter-overlays)
6347 (setq org-agenda-filter-overlays nil)
6348 (setq org-agenda-filter nil)
6349 (setq org-agenda-filter-form nil)
6350 (org-agenda-set-mode-name))
6352 (defun org-agenda-manipulate-query-add ()
6353 "Manipulate the query by adding a search term with positive selection.
6354 Positive selection means the term must be matched for selection of an entry."
6355 (interactive)
6356 (org-agenda-manipulate-query ?\[))
6357 (defun org-agenda-manipulate-query-subtract ()
6358 "Manipulate the query by adding a search term with negative selection.
6359 Negative selection means term must not be matched for selection of an entry."
6360 (interactive)
6361 (org-agenda-manipulate-query ?\]))
6362 (defun org-agenda-manipulate-query-add-re ()
6363 "Manipulate the query by adding a search regexp with positive selection.
6364 Positive selection means the regexp must match for selection of an entry."
6365 (interactive)
6366 (org-agenda-manipulate-query ?\{))
6367 (defun org-agenda-manipulate-query-subtract-re ()
6368 "Manipulate the query by adding a search regexp with negative selection.
6369 Negative selection means regexp must not match for selection of an entry."
6370 (interactive)
6371 (org-agenda-manipulate-query ?\}))
6372 (defun org-agenda-manipulate-query (char)
6373 (cond
6374 ((memq org-agenda-type '(timeline agenda))
6375 (let ((org-agenda-include-inactive-timestamps t))
6376 (org-agenda-redo))
6377 (message "Display now includes inactive timestamps as well"))
6378 ((eq org-agenda-type 'search)
6379 (org-add-to-string
6380 'org-agenda-query-string
6381 (if org-agenda-last-search-view-search-was-boolean
6382 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6383 (?\{ . " +{}") (?\} . " -{}"))))
6384 " "))
6385 (setq org-agenda-redo-command
6386 (list 'org-search-view
6387 org-todo-only
6388 org-agenda-query-string
6389 (+ (length org-agenda-query-string)
6390 (if (member char '(?\{ ?\})) 0 1))))
6391 (set-register org-agenda-query-register org-agenda-query-string)
6392 (org-agenda-redo))
6393 (t (error "Cannot manipulate query for %s-type agenda buffers"
6394 org-agenda-type))))
6396 (defun org-add-to-string (var string)
6397 (set var (concat (symbol-value var) string)))
6399 (defun org-agenda-goto-date (date)
6400 "Jump to DATE in agenda."
6401 (interactive (list (let ((org-read-date-prefer-future
6402 (eval org-agenda-jump-prefer-future)))
6403 (org-read-date))))
6404 (org-agenda-list nil date))
6406 (defun org-agenda-goto-today ()
6407 "Go to today."
6408 (interactive)
6409 (org-agenda-check-type t 'timeline 'agenda)
6410 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6411 (cond
6412 (tdpos (goto-char tdpos))
6413 ((eq org-agenda-type 'agenda)
6414 (let* ((sd (org-agenda-compute-starting-span
6415 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6416 (org-agenda-overriding-arguments org-agenda-last-arguments))
6417 (setf (nth 1 org-agenda-overriding-arguments) sd)
6418 (org-agenda-redo)
6419 (org-agenda-find-same-or-today-or-agenda)))
6420 (t (error "Cannot find today")))))
6422 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6423 (goto-char
6424 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6425 (text-property-any (point-min) (point-max) 'org-today t)
6426 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6427 (point-min))))
6429 (defun org-agenda-later (arg)
6430 "Go forward in time by thee current span.
6431 With prefix ARG, go forward that many times the current span."
6432 (interactive "p")
6433 (org-agenda-check-type t 'agenda)
6434 (let* ((span org-agenda-current-span)
6435 (sd org-starting-day)
6436 (greg (calendar-gregorian-from-absolute sd))
6437 (cnt (org-get-at-bol 'org-day-cnt))
6438 greg2)
6439 (cond
6440 ((eq span 'day)
6441 (setq sd (+ arg sd)))
6442 ((eq span 'week)
6443 (setq sd (+ (* 7 arg) sd)))
6444 ((eq span 'month)
6445 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6446 sd (calendar-absolute-from-gregorian greg2))
6447 (setcar greg2 (1+ (car greg2))))
6448 ((eq span 'year)
6449 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6450 sd (calendar-absolute-from-gregorian greg2))
6451 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6453 (setq sd (+ (* span arg) sd))))
6454 (let ((org-agenda-overriding-arguments
6455 (list (car org-agenda-last-arguments) sd span t)))
6456 (org-agenda-redo)
6457 (org-agenda-find-same-or-today-or-agenda cnt))))
6459 (defun org-agenda-earlier (arg)
6460 "Go backward in time by the current span.
6461 With prefix ARG, go backward that many times the current span."
6462 (interactive "p")
6463 (org-agenda-later (- arg)))
6465 (defun org-agenda-view-mode-dispatch ()
6466 "Call one of the view mode commands."
6467 (interactive)
6468 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6469 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6470 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6471 [E]ntryText")
6472 (let ((a (read-char-exclusive)))
6473 (case a
6474 (?\ (call-interactively 'org-agenda-reset-view))
6475 (?d (call-interactively 'org-agenda-day-view))
6476 (?w (call-interactively 'org-agenda-week-view))
6477 (?m (call-interactively 'org-agenda-month-view))
6478 (?y (call-interactively 'org-agenda-year-view))
6479 (?l (call-interactively 'org-agenda-log-mode))
6480 (?L (org-agenda-log-mode '(4)))
6481 (?c (org-agenda-log-mode 'clockcheck))
6482 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6483 (?a (call-interactively 'org-agenda-archives-mode))
6484 (?A (org-agenda-archives-mode 'files))
6485 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6486 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6487 (?G (call-interactively 'org-agenda-toggle-time-grid))
6488 (?D (call-interactively 'org-agenda-toggle-diary))
6489 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6490 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6491 (org-agenda-check-type t 'timeline 'agenda)
6492 (org-agenda-redo))
6493 (message "Display now includes inactive timestamps as well"))
6494 (?q (message "Abort"))
6495 (otherwise (error "Invalid key" )))))
6497 (defun org-agenda-reset-view ()
6498 "Switch to default view for agenda."
6499 (interactive)
6500 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6501 (defun org-agenda-day-view (&optional day-of-year)
6502 "Switch to daily view for agenda.
6503 With argument DAY-OF-YEAR, switch to that day of the year."
6504 (interactive "P")
6505 (org-agenda-change-time-span 'day day-of-year))
6506 (defun org-agenda-week-view (&optional iso-week)
6507 "Switch to daily view for agenda.
6508 With argument ISO-WEEK, switch to the corresponding ISO week.
6509 If ISO-WEEK has more then 2 digits, only the last two encode the
6510 week. Any digits before this encode a year. So 200712 means
6511 week 12 of year 2007. Years in the range 1938-2037 can also be
6512 written as 2-digit years."
6513 (interactive "P")
6514 (org-agenda-change-time-span 'week iso-week))
6515 (defun org-agenda-month-view (&optional month)
6516 "Switch to monthly view for agenda.
6517 With argument MONTH, switch to that month."
6518 (interactive "P")
6519 (org-agenda-change-time-span 'month month))
6520 (defun org-agenda-year-view (&optional year)
6521 "Switch to yearly view for agenda.
6522 With argument YEAR, switch to that year.
6523 If MONTH has more then 2 digits, only the last two encode the
6524 month. Any digits before this encode a year. So 200712 means
6525 December year 2007. Years in the range 1938-2037 can also be
6526 written as 2-digit years."
6527 (interactive "P")
6528 (when year
6529 (setq year (org-small-year-to-year year)))
6530 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6531 (org-agenda-change-time-span 'year year)
6532 (error "Abort")))
6534 (defun org-agenda-change-time-span (span &optional n)
6535 "Change the agenda view to SPAN.
6536 SPAN may be `day', `week', `month', `year'."
6537 (org-agenda-check-type t 'agenda)
6538 (if (and (not n) (equal org-agenda-current-span span))
6539 (error "Viewing span is already \"%s\"" span))
6540 (let* ((sd (or (org-get-at-bol 'day)
6541 org-starting-day))
6542 (sd (org-agenda-compute-starting-span sd span n))
6543 (org-agenda-overriding-arguments
6544 (or org-agenda-overriding-arguments
6545 (list (car org-agenda-last-arguments) sd span t))))
6546 (org-agenda-redo)
6547 (org-agenda-find-same-or-today-or-agenda))
6548 (org-agenda-set-mode-name)
6549 (message "Switched to %s view" span))
6551 (defun org-agenda-compute-starting-span (sd span &optional n)
6552 "Compute starting date for agenda.
6553 SPAN may be `day', `week', `month', `year'. The return value
6554 is a cons cell with the starting date and the number of days,
6555 so that the date SD will be in that range."
6556 (let* ((greg (calendar-gregorian-from-absolute sd))
6557 (dg (nth 1 greg))
6558 (mg (car greg))
6559 (yg (nth 2 greg)))
6560 (cond
6561 ((eq span 'day)
6562 (when n
6563 (setq sd (+ (calendar-absolute-from-gregorian
6564 (list mg 1 yg))
6565 n -1))))
6566 ((eq span 'week)
6567 (let* ((nt (calendar-day-of-week
6568 (calendar-gregorian-from-absolute sd)))
6569 (d (if org-agenda-start-on-weekday
6570 (- nt org-agenda-start-on-weekday)
6573 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6574 (when n
6575 (require 'cal-iso)
6576 (when (> n 99)
6577 (setq y1 (org-small-year-to-year (/ n 100))
6578 n (mod n 100)))
6579 (setq sd
6580 (calendar-absolute-from-iso
6581 (list n 1
6582 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6583 ((eq span 'month)
6584 (let (y1)
6585 (when (and n (> n 99))
6586 (setq y1 (org-small-year-to-year (/ n 100))
6587 n (mod n 100)))
6588 (setq sd (calendar-absolute-from-gregorian
6589 (list (or n mg) 1 (or y1 yg))))))
6590 ((eq span 'year)
6591 (setq sd (calendar-absolute-from-gregorian
6592 (list 1 1 (or n yg))))))
6593 sd))
6595 (defun org-agenda-next-date-line (&optional arg)
6596 "Jump to the next line indicating a date in agenda buffer."
6597 (interactive "p")
6598 (org-agenda-check-type t 'agenda 'timeline)
6599 (beginning-of-line 1)
6600 ;; This does not work if user makes date format that starts with a blank
6601 (if (looking-at "^\\S-") (forward-char 1))
6602 (if (not (re-search-forward "^\\S-" nil t arg))
6603 (progn
6604 (backward-char 1)
6605 (error "No next date after this line in this buffer")))
6606 (goto-char (match-beginning 0)))
6608 (defun org-agenda-previous-date-line (&optional arg)
6609 "Jump to the previous line indicating a date in agenda buffer."
6610 (interactive "p")
6611 (org-agenda-check-type t 'agenda 'timeline)
6612 (beginning-of-line 1)
6613 (if (not (re-search-backward "^\\S-" nil t arg))
6614 (error "No previous date before this line in this buffer")))
6616 ;; Initialize the highlight
6617 (defvar org-hl (make-overlay 1 1))
6618 (overlay-put org-hl 'face 'highlight)
6620 (defun org-highlight (begin end &optional buffer)
6621 "Highlight a region with overlay."
6622 (move-overlay org-hl begin end (or buffer (current-buffer))))
6624 (defun org-unhighlight ()
6625 "Detach overlay INDEX."
6626 (org-detach-overlay org-hl))
6628 ;; FIXME this is currently not used.
6629 (defun org-highlight-until-next-command (beg end &optional buffer)
6630 "Move the highlight overlay to BEG/END, remove it before the next command."
6631 (org-highlight beg end buffer)
6632 (add-hook 'pre-command-hook 'org-unhighlight-once))
6633 (defun org-unhighlight-once ()
6634 "Remove the highlight from its position, and this function from the hook."
6635 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6636 (org-unhighlight))
6638 (defun org-agenda-follow-mode ()
6639 "Toggle follow mode in an agenda buffer."
6640 (interactive)
6641 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6642 (org-agenda-set-mode-name)
6643 (org-agenda-do-context-action)
6644 (message "Follow mode is %s"
6645 (if org-agenda-follow-mode "on" "off")))
6647 (defun org-agenda-entry-text-mode (&optional arg)
6648 "Toggle entry text mode in an agenda buffer."
6649 (interactive "P")
6650 (setq org-agenda-entry-text-mode (or (integerp arg)
6651 (not org-agenda-entry-text-mode)))
6652 (org-agenda-entry-text-hide)
6653 (and org-agenda-entry-text-mode
6654 (let ((org-agenda-entry-text-maxlines
6655 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6656 (org-agenda-entry-text-show)))
6657 (org-agenda-set-mode-name)
6658 (message "Entry text mode is %s. Maximum number of lines is %d"
6659 (if org-agenda-entry-text-mode "on" "off")
6660 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6662 (defun org-agenda-clockreport-mode (&optional with-filter)
6663 "Toggle clocktable mode in an agenda buffer.
6664 With prefix arg WITH-FILTER, make the clocktable respect the current
6665 agenda filter."
6666 (interactive "P")
6667 (org-agenda-check-type t 'agenda)
6668 (if with-filter
6669 (setq org-agenda-clockreport-mode 'with-filter)
6670 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6671 (org-agenda-set-mode-name)
6672 (org-agenda-redo)
6673 (message "Clocktable mode is %s"
6674 (if org-agenda-clockreport-mode "on" "off")))
6676 (defun org-agenda-log-mode (&optional special)
6677 "Toggle log mode in an agenda buffer.
6678 With argument SPECIAL, show all possible log items, not only the ones
6679 configured in `org-agenda-log-mode-items'.
6680 With a double `C-u' prefix arg, show *only* log items, nothing else."
6681 (interactive "P")
6682 (org-agenda-check-type t 'agenda 'timeline)
6683 (setq org-agenda-show-log
6684 (cond
6685 ((equal special '(16)) 'only)
6686 ((eq special 'clockcheck)
6687 (if (eq org-agenda-show-log 'clockcheck)
6688 nil 'clockcheck))
6689 (special '(closed clock state))
6690 (t (not org-agenda-show-log))))
6691 (org-agenda-set-mode-name)
6692 (org-agenda-redo)
6693 (message "Log mode is %s"
6694 (if org-agenda-show-log "on" "off")))
6696 (defun org-agenda-archives-mode (&optional with-files)
6697 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6698 When called with a prefix argument, include all archive files as well."
6699 (interactive "P")
6700 (setq org-agenda-archives-mode
6701 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6702 (org-agenda-set-mode-name)
6703 (org-agenda-redo)
6704 (message
6705 "%s"
6706 (cond
6707 ((eq org-agenda-archives-mode nil)
6708 "No archives are included")
6709 ((eq org-agenda-archives-mode 'trees)
6710 (format "Trees with :%s: tag are included" org-archive-tag))
6711 ((eq org-agenda-archives-mode t)
6712 (format "Trees with :%s: tag and all active archive files are included"
6713 org-archive-tag)))))
6715 (defun org-agenda-toggle-diary ()
6716 "Toggle diary inclusion in an agenda buffer."
6717 (interactive)
6718 (org-agenda-check-type t 'agenda)
6719 (setq org-agenda-include-diary (not org-agenda-include-diary))
6720 (org-agenda-redo)
6721 (org-agenda-set-mode-name)
6722 (message "Diary inclusion turned %s"
6723 (if org-agenda-include-diary "on" "off")))
6725 (defun org-agenda-toggle-deadlines ()
6726 "Toggle inclusion of entries with a deadline in an agenda buffer."
6727 (interactive)
6728 (org-agenda-check-type t 'agenda)
6729 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6730 (org-agenda-redo)
6731 (org-agenda-set-mode-name)
6732 (message "Deadlines inclusion turned %s"
6733 (if org-agenda-include-deadlines "on" "off")))
6735 (defun org-agenda-toggle-time-grid ()
6736 "Toggle time grid in an agenda buffer."
6737 (interactive)
6738 (org-agenda-check-type t 'agenda)
6739 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6740 (org-agenda-redo)
6741 (org-agenda-set-mode-name)
6742 (message "Time-grid turned %s"
6743 (if org-agenda-use-time-grid "on" "off")))
6745 (defun org-agenda-set-mode-name ()
6746 "Set the mode name to indicate all the small mode settings."
6747 (setq mode-name
6748 (list "Org-Agenda"
6749 (if (get 'org-agenda-files 'org-restrict) " []" "")
6751 '(:eval (org-agenda-span-name org-agenda-current-span))
6752 (if org-agenda-follow-mode " Follow" "")
6753 (if org-agenda-entry-text-mode " ETxt" "")
6754 (if org-agenda-include-diary " Diary" "")
6755 (if org-agenda-include-deadlines " Ddl" "")
6756 (if org-agenda-use-time-grid " Grid" "")
6757 (if (and (boundp 'org-habit-show-habits)
6758 org-habit-show-habits) " Habit" "")
6759 (cond
6760 ((consp org-agenda-show-log) " LogAll")
6761 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6762 (org-agenda-show-log " Log")
6763 (t ""))
6764 (if (or org-agenda-filter (get 'org-agenda-filter
6765 :preset-filter))
6766 (concat " {" (mapconcat
6767 'identity
6768 (append (get 'org-agenda-filter
6769 :preset-filter)
6770 org-agenda-filter) "") "}")
6772 (if org-agenda-archives-mode
6773 (if (eq org-agenda-archives-mode t)
6774 " Archives"
6775 (format " :%s:" org-archive-tag))
6777 (if org-agenda-clockreport-mode
6778 (if (eq org-agenda-clockreport-mode 'with-filter)
6779 " Clock{}" " Clock")
6780 "")))
6781 (force-mode-line-update))
6783 (defun org-agenda-post-command-hook ()
6784 (setq org-agenda-type
6785 (or (get-text-property (point) 'org-agenda-type)
6786 (get-text-property (max (point-min) (1- (point)))
6787 'org-agenda-type))))
6789 (defun org-agenda-next-line ()
6790 "Move cursor to the next line, and show if follow mode is active."
6791 (interactive)
6792 (call-interactively 'next-line)
6793 (org-agenda-do-context-action))
6795 (defun org-agenda-previous-line ()
6796 "Move cursor to the previous line, and show if follow-mode is active."
6797 (interactive)
6798 (call-interactively 'previous-line)
6799 (org-agenda-do-context-action))
6801 (defun org-agenda-do-context-action ()
6802 "Show outline path and, maybe, follow mode window."
6803 (let ((m (org-get-at-bol 'org-marker)))
6804 (when (and (markerp m) (marker-buffer m))
6805 (and org-agenda-follow-mode
6806 (if org-agenda-follow-indirect
6807 (org-agenda-tree-to-indirect-buffer)
6808 (org-agenda-show)))
6809 (and org-agenda-show-outline-path
6810 (org-with-point-at m (org-display-outline-path t))))))
6812 (defun org-agenda-show-priority ()
6813 "Show the priority of the current item.
6814 This priority is composed of the main priority given with the [#A] cookies,
6815 and by additional input from the age of a schedules or deadline entry."
6816 (interactive)
6817 (let* ((pri (org-get-at-bol 'priority)))
6818 (message "Priority is %d" (if pri pri -1000))))
6820 (defun org-agenda-show-tags ()
6821 "Show the tags applicable to the current item."
6822 (interactive)
6823 (let* ((tags (org-get-at-bol 'tags)))
6824 (if tags
6825 (message "Tags are :%s:"
6826 (org-no-properties (mapconcat 'identity tags ":")))
6827 (message "No tags associated with this line"))))
6829 (defun org-agenda-goto (&optional highlight)
6830 "Go to the Org-mode file which contains the item at point."
6831 (interactive)
6832 (let* ((marker (or (org-get-at-bol 'org-marker)
6833 (org-agenda-error)))
6834 (buffer (marker-buffer marker))
6835 (pos (marker-position marker)))
6836 (switch-to-buffer-other-window buffer)
6837 (widen)
6838 (push-mark)
6839 (goto-char pos)
6840 (when (eq major-mode 'org-mode)
6841 (org-show-context 'agenda)
6842 (save-excursion
6843 (and (outline-next-heading)
6844 (org-flag-heading nil)))) ; show the next heading
6845 (when (outline-invisible-p)
6846 (show-entry)) ; display invisible text
6847 (recenter (/ (window-height) 2))
6848 (run-hooks 'org-agenda-after-show-hook)
6849 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6851 (defvar org-agenda-after-show-hook nil
6852 "Normal hook run after an item has been shown from the agenda.
6853 Point is in the buffer where the item originated.")
6855 (defun org-agenda-kill ()
6856 "Kill the entry or subtree belonging to the current agenda entry."
6857 (interactive)
6858 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6859 (let* ((marker (or (org-get-at-bol 'org-marker)
6860 (org-agenda-error)))
6861 (buffer (marker-buffer marker))
6862 (pos (marker-position marker))
6863 (type (org-get-at-bol 'type))
6864 dbeg dend (n 0) conf)
6865 (org-with-remote-undo buffer
6866 (with-current-buffer buffer
6867 (save-excursion
6868 (goto-char pos)
6869 (if (and (eq major-mode 'org-mode) (not (member type '("sexp"))))
6870 (setq dbeg (progn (org-back-to-heading t) (point))
6871 dend (org-end-of-subtree t t))
6872 (setq dbeg (point-at-bol)
6873 dend (min (point-max) (1+ (point-at-eol)))))
6874 (goto-char dbeg)
6875 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6876 (setq conf (or (eq t org-agenda-confirm-kill)
6877 (and (numberp org-agenda-confirm-kill)
6878 (> n org-agenda-confirm-kill))))
6879 (and conf
6880 (not (y-or-n-p
6881 (format "Delete entry with %d lines in buffer \"%s\"? "
6882 n (buffer-name buffer))))
6883 (error "Abort"))
6884 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6885 (with-current-buffer buffer (delete-region dbeg dend))
6886 (message "Agenda item and source killed"))))
6888 (defvar org-archive-default-command)
6889 (defun org-agenda-archive-default ()
6890 "Archive the entry or subtree belonging to the current agenda entry."
6891 (interactive)
6892 (require 'org-archive)
6893 (org-agenda-archive-with org-archive-default-command))
6895 (defun org-agenda-archive-default-with-confirmation ()
6896 "Archive the entry or subtree belonging to the current agenda entry."
6897 (interactive)
6898 (require 'org-archive)
6899 (org-agenda-archive-with org-archive-default-command 'confirm))
6901 (defun org-agenda-archive ()
6902 "Archive the entry or subtree belonging to the current agenda entry."
6903 (interactive)
6904 (org-agenda-archive-with 'org-archive-subtree))
6906 (defun org-agenda-archive-to-archive-sibling ()
6907 "Move the entry to the archive sibling."
6908 (interactive)
6909 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6911 (defun org-agenda-archive-with (cmd &optional confirm)
6912 "Move the entry to the archive sibling."
6913 (interactive)
6914 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6915 (let* ((marker (or (org-get-at-bol 'org-marker)
6916 (org-agenda-error)))
6917 (buffer (marker-buffer marker))
6918 (pos (marker-position marker)))
6919 (org-with-remote-undo buffer
6920 (with-current-buffer buffer
6921 (if (eq major-mode 'org-mode)
6922 (if (and confirm
6923 (not (y-or-n-p "Archive this subtree or entry? ")))
6924 (error "Abort")
6925 (save-excursion
6926 (goto-char pos)
6927 (org-remove-subtree-entries-from-agenda)
6928 (org-back-to-heading t)
6929 (funcall cmd)))
6930 (error "Archiving works only in Org-mode files"))))))
6932 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6933 "Remove all lines in the agenda that correspond to a given subtree.
6934 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6935 If this information is not given, the function uses the tree at point."
6936 (let ((buf (or buf (current-buffer))) m p)
6937 (save-excursion
6938 (unless (and beg end)
6939 (org-back-to-heading t)
6940 (setq beg (point))
6941 (org-end-of-subtree t)
6942 (setq end (point)))
6943 (set-buffer (get-buffer org-agenda-buffer-name))
6944 (save-excursion
6945 (goto-char (point-max))
6946 (beginning-of-line 1)
6947 (while (not (bobp))
6948 (when (and (setq m (org-get-at-bol 'org-marker))
6949 (equal buf (marker-buffer m))
6950 (setq p (marker-position m))
6951 (>= p beg)
6952 (< p end))
6953 (let ((inhibit-read-only t))
6954 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6955 (beginning-of-line 0))))))
6957 (defun org-agenda-refile (&optional goto rfloc no-update)
6958 "Refile the item at point."
6959 (interactive "P")
6960 (if (equal goto '(16))
6961 (org-refile-goto-last-stored)
6962 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6963 (org-agenda-error)))
6964 (buffer (marker-buffer marker))
6965 (pos (marker-position marker))
6966 (rfloc (or rfloc
6967 (org-refile-get-location
6968 (if goto "Goto" "Refile to") buffer
6969 org-refile-allow-creating-parent-nodes))))
6970 (with-current-buffer buffer
6971 (save-excursion
6972 (save-restriction
6973 (widen)
6974 (goto-char marker)
6975 (org-remove-subtree-entries-from-agenda)
6976 (org-refile goto buffer rfloc)))))
6977 (unless no-update (org-agenda-redo))))
6979 (defun org-agenda-open-link (&optional arg)
6980 "Follow the link in the current line, if any.
6981 This looks for a link in the displayed line in the agenda. It also looks
6982 at the text of the entry itself."
6983 (interactive "P")
6984 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6985 (org-get-at-bol 'org-marker)))
6986 (buffer (and marker (marker-buffer marker)))
6987 (prefix (buffer-substring
6988 (point-at-bol) (point-at-eol))))
6989 (cond
6990 (buffer
6991 (with-current-buffer buffer
6992 (save-excursion
6993 (save-restriction
6994 (widen)
6995 (goto-char marker)
6996 (org-offer-links-in-entry arg prefix)))))
6997 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6998 (save-excursion
6999 (beginning-of-line 1)
7000 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7001 (org-open-link-from-string (match-string 1)))
7002 (t (error "No link to open here")))))
7004 (defun org-agenda-copy-local-variable (var)
7005 "Get a variable from a referenced buffer and install it here."
7006 (let ((m (org-get-at-bol 'org-marker)))
7007 (when (and m (buffer-live-p (marker-buffer m)))
7008 (org-set-local var (with-current-buffer (marker-buffer m)
7009 (symbol-value var))))))
7011 (defun org-agenda-switch-to (&optional delete-other-windows)
7012 "Go to the Org-mode file which contains the item at point."
7013 (interactive)
7014 (if (and org-return-follows-link
7015 (not (org-get-at-bol 'org-marker))
7016 (org-in-regexp org-bracket-link-regexp))
7017 (org-open-link-from-string (match-string 0))
7018 (let* ((marker (or (org-get-at-bol 'org-marker)
7019 (org-agenda-error)))
7020 (buffer (marker-buffer marker))
7021 (pos (marker-position marker)))
7022 (org-pop-to-buffer-same-window buffer)
7023 (and delete-other-windows (delete-other-windows))
7024 (widen)
7025 (goto-char pos)
7026 (when (eq major-mode 'org-mode)
7027 (org-show-context 'agenda)
7028 (save-excursion
7029 (and (outline-next-heading)
7030 (org-flag-heading nil))) ; show the next heading
7031 (when (outline-invisible-p)
7032 (show-entry)))))) ; display invisible text
7034 (defun org-agenda-goto-mouse (ev)
7035 "Go to the Org-mode file which contains the item at the mouse click."
7036 (interactive "e")
7037 (mouse-set-point ev)
7038 (org-agenda-goto))
7040 (defun org-agenda-show (&optional full-entry)
7041 "Display the Org-mode file which contains the item at point.
7042 With prefix argument FULL-ENTRY, make the entire entry visible
7043 if it was hidden in the outline."
7044 (interactive "P")
7045 (let ((win (selected-window)))
7046 (if full-entry
7047 (let ((org-show-entry-below t))
7048 (org-agenda-goto t))
7049 (org-agenda-goto t))
7050 (select-window win)))
7052 (defvar org-agenda-show-window nil)
7053 (defun org-agenda-show-and-scroll-up ()
7054 "Display the Org-mode file which contains the item at point.
7055 When called repeatedly, scroll the window that is displaying the buffer."
7056 (interactive)
7057 (let ((win (selected-window)))
7058 (if (and (window-live-p org-agenda-show-window)
7059 (eq this-command last-command))
7060 (progn
7061 (select-window org-agenda-show-window)
7062 (ignore-errors (scroll-up)))
7063 (org-agenda-goto t)
7064 (show-subtree)
7065 (setq org-agenda-show-window (selected-window)))
7066 (select-window win)))
7068 (defun org-agenda-show-scroll-down ()
7069 "Scroll down the window showing the agenda."
7070 (interactive)
7071 (let ((win (selected-window)))
7072 (when (window-live-p org-agenda-show-window)
7073 (select-window org-agenda-show-window)
7074 (ignore-errors (scroll-down))
7075 (select-window win))))
7077 (defun org-agenda-show-1 (&optional more)
7078 "Display the Org-mode file which contains the item at point.
7079 The prefix arg selects the amount of information to display:
7081 0 hide the subtree
7082 1 just show the entry according to defaults.
7083 2 show the children view
7084 3 show the subtree view
7085 4 show the entire subtree and any LOGBOOK drawers
7086 5 show the entire subtree and any drawers
7087 With prefix argument FULL-ENTRY, make the entire entry visible
7088 if it was hidden in the outline."
7089 (interactive "p")
7090 (let ((win (selected-window)))
7091 (org-agenda-goto t)
7092 (org-recenter-heading 1)
7093 (cond
7094 ((= more 0)
7095 (hide-subtree)
7096 (save-excursion
7097 (org-back-to-heading)
7098 (run-hook-with-args 'org-cycle-hook 'folded))
7099 (message "Remote: FOLDED"))
7100 ((and (org-called-interactively-p 'any) (= more 1))
7101 (message "Remote: show with default settings"))
7102 ((= more 2)
7103 (show-entry)
7104 (show-children)
7105 (save-excursion
7106 (org-back-to-heading)
7107 (run-hook-with-args 'org-cycle-hook 'children))
7108 (message "Remote: CHILDREN"))
7109 ((= more 3)
7110 (show-subtree)
7111 (save-excursion
7112 (org-back-to-heading)
7113 (run-hook-with-args 'org-cycle-hook 'subtree))
7114 (message "Remote: SUBTREE"))
7115 ((= more 4)
7116 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7117 (org-drawer-regexp
7118 (concat "^[ \t]*:\\("
7119 (mapconcat 'regexp-quote org-drawers "\\|")
7120 "\\):[ \t]*$")))
7121 (show-subtree)
7122 (save-excursion
7123 (org-back-to-heading)
7124 (org-cycle-hide-drawers 'subtree)))
7125 (message "Remote: SUBTREE AND LOGBOOK"))
7126 ((> more 4)
7127 (show-subtree)
7128 (message "Remote: SUBTREE AND ALL DRAWERS")))
7129 (select-window win)))
7131 (defun org-recenter-heading (n)
7132 (save-excursion
7133 (org-back-to-heading)
7134 (recenter n)))
7136 (defvar org-agenda-cycle-counter nil)
7137 (defun org-agenda-cycle-show (&optional n)
7138 "Show the current entry in another window, with default settings.
7139 Default settings are taken from `org-show-hierarchy-above' and siblings.
7140 When use repeatedly in immediate succession, the remote entry will cycle
7141 through visibility
7143 children -> subtree -> folded
7145 When called with a numeric prefix arg, that arg will be passed through to
7146 `org-agenda-show-1'. For the interpretation of that argument, see the
7147 docstring of `org-agenda-show-1'."
7148 (interactive "P")
7149 (if (integerp n)
7150 (setq org-agenda-cycle-counter n)
7151 (if (not (eq last-command this-command))
7152 (setq org-agenda-cycle-counter 1)
7153 (if (equal org-agenda-cycle-counter 0)
7154 (setq org-agenda-cycle-counter 2)
7155 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7156 (if (> org-agenda-cycle-counter 3)
7157 (setq org-agenda-cycle-counter 0)))))
7158 (org-agenda-show-1 org-agenda-cycle-counter))
7160 (defun org-agenda-recenter (arg)
7161 "Display the Org-mode file which contains the item at point and recenter."
7162 (interactive "P")
7163 (let ((win (selected-window)))
7164 (org-agenda-goto t)
7165 (recenter arg)
7166 (select-window win)))
7168 (defun org-agenda-show-mouse (ev)
7169 "Display the Org-mode file which contains the item at the mouse click."
7170 (interactive "e")
7171 (mouse-set-point ev)
7172 (org-agenda-show))
7174 (defun org-agenda-check-no-diary ()
7175 "Check if the entry is a diary link and abort if yes."
7176 (if (org-get-at-bol 'org-agenda-diary-link)
7177 (org-agenda-error)))
7179 (defun org-agenda-error ()
7180 (error "Command not allowed in this line"))
7182 (defun org-agenda-tree-to-indirect-buffer ()
7183 "Show the subtree corresponding to the current entry in an indirect buffer.
7184 This calls the command `org-tree-to-indirect-buffer' from the original
7185 Org-mode buffer.
7186 With numerical prefix arg ARG, go up to this level and then take that tree.
7187 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7188 use the dedicated frame)."
7189 (interactive)
7190 (org-agenda-check-no-diary)
7191 (let* ((marker (or (org-get-at-bol 'org-marker)
7192 (org-agenda-error)))
7193 (buffer (marker-buffer marker))
7194 (pos (marker-position marker)))
7195 (with-current-buffer buffer
7196 (save-excursion
7197 (goto-char pos)
7198 (call-interactively 'org-tree-to-indirect-buffer)))))
7200 (defvar org-last-heading-marker (make-marker)
7201 "Marker pointing to the headline that last changed its TODO state
7202 by a remote command from the agenda.")
7204 (defun org-agenda-todo-nextset ()
7205 "Switch TODO entry to next sequence."
7206 (interactive)
7207 (org-agenda-todo 'nextset))
7209 (defun org-agenda-todo-previousset ()
7210 "Switch TODO entry to previous sequence."
7211 (interactive)
7212 (org-agenda-todo 'previousset))
7214 (defun org-agenda-todo (&optional arg)
7215 "Cycle TODO state of line at point, also in Org-mode file.
7216 This changes the line at point, all other lines in the agenda referring to
7217 the same tree node, and the headline of the tree node in the Org-mode file."
7218 (interactive "P")
7219 (org-agenda-check-no-diary)
7220 (let* ((col (current-column))
7221 (marker (or (org-get-at-bol 'org-marker)
7222 (org-agenda-error)))
7223 (buffer (marker-buffer marker))
7224 (pos (marker-position marker))
7225 (hdmarker (org-get-at-bol 'org-hd-marker))
7226 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7227 (inhibit-read-only t)
7228 org-agenda-headline-snapshot-before-repeat newhead just-one)
7229 (org-with-remote-undo buffer
7230 (with-current-buffer buffer
7231 (widen)
7232 (goto-char pos)
7233 (org-show-context 'agenda)
7234 (save-excursion
7235 (and (outline-next-heading)
7236 (org-flag-heading nil))) ; show the next heading
7237 (let ((current-prefix-arg arg))
7238 (call-interactively 'org-todo))
7239 (and (bolp) (forward-char 1))
7240 (setq newhead (org-get-heading))
7241 (when (and (org-bound-and-true-p
7242 org-agenda-headline-snapshot-before-repeat)
7243 (not (equal org-agenda-headline-snapshot-before-repeat
7244 newhead))
7245 todayp)
7246 (setq newhead org-agenda-headline-snapshot-before-repeat
7247 just-one t))
7248 (save-excursion
7249 (org-back-to-heading)
7250 (move-marker org-last-heading-marker (point))))
7251 (beginning-of-line 1)
7252 (save-excursion
7253 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7254 (org-move-to-column col))))
7256 (defun org-agenda-add-note (&optional arg)
7257 "Add a time-stamped note to the entry at point."
7258 (interactive "P")
7259 (org-agenda-check-no-diary)
7260 (let* ((marker (or (org-get-at-bol 'org-marker)
7261 (org-agenda-error)))
7262 (buffer (marker-buffer marker))
7263 (pos (marker-position marker))
7264 (hdmarker (org-get-at-bol 'org-hd-marker))
7265 (inhibit-read-only t))
7266 (with-current-buffer buffer
7267 (widen)
7268 (goto-char pos)
7269 (org-show-context 'agenda)
7270 (save-excursion
7271 (and (outline-next-heading)
7272 (org-flag-heading nil))) ; show the next heading
7273 (org-add-note))))
7275 (defun org-agenda-change-all-lines (newhead hdmarker
7276 &optional fixface just-this)
7277 "Change all lines in the agenda buffer which match HDMARKER.
7278 The new content of the line will be NEWHEAD (as modified by
7279 `org-agenda-format-item'). HDMARKER is checked with
7280 `equal' against all `org-hd-marker' text properties in the file.
7281 If FIXFACE is non-nil, the face of each item is modified according to
7282 the new TODO state.
7283 If JUST-THIS is non-nil, change just the current line, not all.
7284 If FORCE-TAGS is non nil, the car of it returns the new tags."
7285 (let* ((inhibit-read-only t)
7286 (line (org-current-line))
7287 (thetags (with-current-buffer (marker-buffer hdmarker)
7288 (save-excursion (save-restriction (widen)
7289 (goto-char hdmarker)
7290 (org-get-tags-at)))))
7291 props m pl undone-face done-face finish new dotime cat tags)
7292 (save-excursion
7293 (goto-char (point-max))
7294 (beginning-of-line 1)
7295 (while (not finish)
7296 (setq finish (bobp))
7297 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7298 (or (not just-this) (= (org-current-line) line))
7299 (equal m hdmarker))
7300 (setq props (text-properties-at (point))
7301 dotime (org-get-at-bol 'dotime)
7302 cat (org-get-at-bol 'org-category)
7303 tags thetags
7305 (let ((org-prefix-format-compiled
7306 (or (get-text-property (point) 'format)
7307 org-prefix-format-compiled)))
7308 (with-current-buffer (marker-buffer hdmarker)
7309 (save-excursion
7310 (save-restriction
7311 (widen)
7312 (org-agenda-format-item (org-get-at-bol 'extra)
7313 newhead cat tags dotime)))))
7314 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7315 undone-face (org-get-at-bol 'undone-face)
7316 done-face (org-get-at-bol 'done-face))
7317 (beginning-of-line 1)
7318 (cond
7319 ((equal new "")
7320 (and (looking-at ".*\n?") (replace-match "")))
7321 ((looking-at ".*")
7322 (replace-match new t t)
7323 (beginning-of-line 1)
7324 (add-text-properties (point-at-bol) (point-at-eol) props)
7325 (when fixface
7326 (add-text-properties
7327 (point-at-bol) (point-at-eol)
7328 (list 'face
7329 (if org-last-todo-state-is-todo
7330 undone-face done-face))))
7331 (org-agenda-highlight-todo 'line)
7332 (beginning-of-line 1))
7333 (t (error "Line update did not work"))))
7334 (beginning-of-line 0)))
7335 (org-finalize-agenda)))
7337 (defun org-agenda-align-tags (&optional line)
7338 "Align all tags in agenda items to `org-agenda-tags-column'."
7339 (let ((inhibit-read-only t) l c)
7340 (save-excursion
7341 (goto-char (if line (point-at-bol) (point-min)))
7342 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7343 (if line (point-at-eol) nil) t)
7344 (add-text-properties
7345 (match-beginning 2) (match-end 2)
7346 (list 'face (delq nil (let ((prop (get-text-property
7347 (match-beginning 2) 'face)))
7348 (or (listp prop) (setq prop (list prop)))
7349 (if (memq 'org-tag prop)
7350 prop
7351 (cons 'org-tag prop))))))
7352 (setq l (- (match-end 2) (match-beginning 2))
7353 c (if (< org-agenda-tags-column 0)
7354 (- (abs org-agenda-tags-column) l)
7355 org-agenda-tags-column))
7356 (delete-region (match-beginning 1) (match-end 1))
7357 (goto-char (match-beginning 1))
7358 (insert (org-add-props
7359 (make-string (max 1 (- c (current-column))) ?\ )
7360 (plist-put (copy-sequence (text-properties-at (point)))
7361 'face nil))))
7362 (goto-char (point-min))
7363 (org-font-lock-add-tag-faces (point-max)))))
7365 (defun org-agenda-priority-up ()
7366 "Increase the priority of line at point, also in Org-mode file."
7367 (interactive)
7368 (org-agenda-priority 'up))
7370 (defun org-agenda-priority-down ()
7371 "Decrease the priority of line at point, also in Org-mode file."
7372 (interactive)
7373 (org-agenda-priority 'down))
7375 (defun org-agenda-priority (&optional force-direction)
7376 "Set the priority of line at point, also in Org-mode file.
7377 This changes the line at point, all other lines in the agenda referring to
7378 the same tree node, and the headline of the tree node in the Org-mode file."
7379 (interactive)
7380 (unless org-enable-priority-commands
7381 (error "Priority commands are disabled"))
7382 (org-agenda-check-no-diary)
7383 (let* ((marker (or (org-get-at-bol 'org-marker)
7384 (org-agenda-error)))
7385 (hdmarker (org-get-at-bol 'org-hd-marker))
7386 (buffer (marker-buffer hdmarker))
7387 (pos (marker-position hdmarker))
7388 (inhibit-read-only t)
7389 newhead)
7390 (org-with-remote-undo buffer
7391 (with-current-buffer buffer
7392 (widen)
7393 (goto-char pos)
7394 (org-show-context 'agenda)
7395 (save-excursion
7396 (and (outline-next-heading)
7397 (org-flag-heading nil))) ; show the next heading
7398 (funcall 'org-priority force-direction)
7399 (end-of-line 1)
7400 (setq newhead (org-get-heading)))
7401 (org-agenda-change-all-lines newhead hdmarker)
7402 (beginning-of-line 1))))
7404 ;; FIXME: should fix the tags property of the agenda line.
7405 (defun org-agenda-set-tags (&optional tag onoff)
7406 "Set tags for the current headline."
7407 (interactive)
7408 (org-agenda-check-no-diary)
7409 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7410 (call-interactively 'org-change-tag-in-region)
7411 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7412 (org-agenda-error)))
7413 (buffer (marker-buffer hdmarker))
7414 (pos (marker-position hdmarker))
7415 (inhibit-read-only t)
7416 newhead)
7417 (org-with-remote-undo buffer
7418 (with-current-buffer buffer
7419 (widen)
7420 (goto-char pos)
7421 (save-excursion
7422 (org-show-context 'agenda))
7423 (save-excursion
7424 (and (outline-next-heading)
7425 (org-flag-heading nil))) ; show the next heading
7426 (goto-char pos)
7427 (if tag
7428 (org-toggle-tag tag onoff)
7429 (call-interactively 'org-set-tags))
7430 (end-of-line 1)
7431 (setq newhead (org-get-heading)))
7432 (org-agenda-change-all-lines newhead hdmarker)
7433 (beginning-of-line 1)))))
7435 (defun org-agenda-set-property ()
7436 "Set a property for the current headline."
7437 (interactive)
7438 (org-agenda-check-no-diary)
7439 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7440 (org-agenda-error)))
7441 (buffer (marker-buffer hdmarker))
7442 (pos (marker-position hdmarker))
7443 (inhibit-read-only t)
7444 newhead)
7445 (org-with-remote-undo buffer
7446 (with-current-buffer buffer
7447 (widen)
7448 (goto-char pos)
7449 (save-excursion
7450 (org-show-context 'agenda))
7451 (save-excursion
7452 (and (outline-next-heading)
7453 (org-flag-heading nil))) ; show the next heading
7454 (goto-char pos)
7455 (call-interactively 'org-set-property)))))
7457 (defun org-agenda-set-effort ()
7458 "Set the effort property for the current headline."
7459 (interactive)
7460 (org-agenda-check-no-diary)
7461 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7462 (org-agenda-error)))
7463 (buffer (marker-buffer hdmarker))
7464 (pos (marker-position hdmarker))
7465 (inhibit-read-only t)
7466 newhead)
7467 (org-with-remote-undo buffer
7468 (with-current-buffer buffer
7469 (widen)
7470 (goto-char pos)
7471 (save-excursion
7472 (org-show-context 'agenda))
7473 (save-excursion
7474 (and (outline-next-heading)
7475 (org-flag-heading nil))) ; show the next heading
7476 (goto-char pos)
7477 (call-interactively 'org-set-effort)
7478 (end-of-line 1)
7479 (setq newhead (org-get-heading)))
7480 (org-agenda-change-all-lines newhead hdmarker))))
7482 (defun org-agenda-toggle-archive-tag ()
7483 "Toggle the archive tag for the current entry."
7484 (interactive)
7485 (org-agenda-check-no-diary)
7486 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7487 (org-agenda-error)))
7488 (buffer (marker-buffer hdmarker))
7489 (pos (marker-position hdmarker))
7490 (inhibit-read-only t)
7491 newhead)
7492 (org-with-remote-undo buffer
7493 (with-current-buffer buffer
7494 (widen)
7495 (goto-char pos)
7496 (org-show-context 'agenda)
7497 (save-excursion
7498 (and (outline-next-heading)
7499 (org-flag-heading nil))) ; show the next heading
7500 (call-interactively 'org-toggle-archive-tag)
7501 (end-of-line 1)
7502 (setq newhead (org-get-heading)))
7503 (org-agenda-change-all-lines newhead hdmarker)
7504 (beginning-of-line 1))))
7506 (defun org-agenda-do-date-later (arg)
7507 (interactive "P")
7508 (cond
7509 ((or (equal arg '(16))
7510 (memq last-command
7511 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7512 (setq this-command 'org-agenda-date-later-minutes)
7513 (org-agenda-date-later-minutes 1))
7514 ((or (equal arg '(4))
7515 (memq last-command
7516 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7517 (setq this-command 'org-agenda-date-later-hours)
7518 (org-agenda-date-later-hours 1))
7520 (org-agenda-date-later (prefix-numeric-value arg)))))
7522 (defun org-agenda-do-date-earlier (arg)
7523 (interactive "P")
7524 (cond
7525 ((or (equal arg '(16))
7526 (memq last-command
7527 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7528 (setq this-command 'org-agenda-date-earlier-minutes)
7529 (org-agenda-date-earlier-minutes 1))
7530 ((or (equal arg '(4))
7531 (memq last-command
7532 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7533 (setq this-command 'org-agenda-date-earlier-hours)
7534 (org-agenda-date-earlier-hours 1))
7536 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7538 (defun org-agenda-date-later (arg &optional what)
7539 "Change the date of this item to ARG day(s) later."
7540 (interactive "p")
7541 (org-agenda-check-type t 'agenda 'timeline)
7542 (org-agenda-check-no-diary)
7543 (let* ((marker (or (org-get-at-bol 'org-marker)
7544 (org-agenda-error)))
7545 (buffer (marker-buffer marker))
7546 (pos (marker-position marker))
7547 cdate today)
7548 (org-with-remote-undo buffer
7549 (with-current-buffer buffer
7550 (widen)
7551 (goto-char pos)
7552 (if (not (org-at-timestamp-p))
7553 (error "Cannot find time stamp"))
7554 (when (and org-agenda-move-date-from-past-immediately-to-today
7555 (equal arg 1)
7556 (or (not what) (eq what 'day))
7557 (not (save-match-data (org-at-date-range-p))))
7558 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
7559 cdate (calendar-absolute-from-gregorian
7560 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
7561 today (org-today))
7562 (if (> today cdate)
7563 ;; immediately shift to today
7564 (setq arg (- today cdate))))
7565 (org-timestamp-change arg (or what 'day))
7566 (when (and (org-at-date-range-p)
7567 (re-search-backward org-tr-regexp-both (point-at-bol)))
7568 (let ((end org-last-changed-timestamp))
7569 (org-timestamp-change arg (or what 'day))
7570 (setq org-last-changed-timestamp
7571 (concat org-last-changed-timestamp "--" end)))))
7572 (org-agenda-show-new-time marker org-last-changed-timestamp))
7573 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7575 (defun org-agenda-date-earlier (arg &optional what)
7576 "Change the date of this item to ARG day(s) earlier."
7577 (interactive "p")
7578 (org-agenda-date-later (- arg) what))
7580 (defun org-agenda-date-later-minutes (arg)
7581 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7582 (interactive "p")
7583 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7584 (org-agenda-date-later arg 'minute))
7586 (defun org-agenda-date-earlier-minutes (arg)
7587 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7588 (interactive "p")
7589 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7590 (org-agenda-date-earlier arg 'minute))
7592 (defun org-agenda-date-later-hours (arg)
7593 "Change the time of this item, in hour steps."
7594 (interactive "p")
7595 (org-agenda-date-later arg 'hour))
7597 (defun org-agenda-date-earlier-hours (arg)
7598 "Change the time of this item, in hour steps."
7599 (interactive "p")
7600 (org-agenda-date-earlier arg 'hour))
7602 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7603 "Show new date stamp via text properties."
7604 ;; We use text properties to make this undoable
7605 (let ((inhibit-read-only t)
7606 (buffer-invisibility-spec))
7607 (setq stamp (concat " " prefix " => " stamp))
7608 (save-excursion
7609 (goto-char (point-max))
7610 (while (not (bobp))
7611 (when (equal marker (org-get-at-bol 'org-marker))
7612 (org-move-to-column (- (window-width) (length stamp)) t)
7613 (org-agenda-fix-tags-filter-overlays-at (point))
7614 (if (featurep 'xemacs)
7615 ;; Use `duplicable' property to trigger undo recording
7616 (let ((ex (make-extent nil nil))
7617 (gl (make-glyph stamp)))
7618 (set-glyph-face gl 'secondary-selection)
7619 (set-extent-properties
7620 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7621 (insert-extent ex (1- (point)) (point-at-eol)))
7622 (add-text-properties
7623 (1- (point)) (point-at-eol)
7624 (list 'display (org-add-props stamp nil
7625 'face 'secondary-selection))))
7626 (beginning-of-line 1))
7627 (beginning-of-line 0)))))
7629 (defun org-agenda-date-prompt (arg)
7630 "Change the date of this item. Date is prompted for, with default today.
7631 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7632 be used to request time specification in the time stamp."
7633 (interactive "P")
7634 (org-agenda-check-type t 'agenda 'timeline)
7635 (org-agenda-check-no-diary)
7636 (let* ((marker (or (org-get-at-bol 'org-marker)
7637 (org-agenda-error)))
7638 (buffer (marker-buffer marker))
7639 (pos (marker-position marker)))
7640 (org-with-remote-undo buffer
7641 (with-current-buffer buffer
7642 (widen)
7643 (goto-char pos)
7644 (if (not (org-at-timestamp-p t))
7645 (error "Cannot find time stamp"))
7646 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7647 (org-agenda-show-new-time marker org-last-changed-timestamp))
7648 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7650 (defun org-agenda-schedule (arg &optional time)
7651 "Schedule the item at point.
7652 ARG is passed through to `org-schedule'."
7653 (interactive "P")
7654 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7655 (org-agenda-check-no-diary)
7656 (let* ((marker (or (org-get-at-bol 'org-marker)
7657 (org-agenda-error)))
7658 (type (marker-insertion-type marker))
7659 (buffer (marker-buffer marker))
7660 (pos (marker-position marker))
7661 (org-insert-labeled-timestamps-at-point nil)
7663 (set-marker-insertion-type marker t)
7664 (org-with-remote-undo buffer
7665 (with-current-buffer buffer
7666 (widen)
7667 (goto-char pos)
7668 (setq ts (org-schedule arg time)))
7669 (org-agenda-show-new-time marker ts "S"))
7670 (message "Item scheduled for %s" ts)))
7672 (defun org-agenda-deadline (arg &optional time)
7673 "Schedule the item at point.
7674 ARG is passed through to `org-deadline'."
7675 (interactive "P")
7676 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7677 (org-agenda-check-no-diary)
7678 (let* ((marker (or (org-get-at-bol 'org-marker)
7679 (org-agenda-error)))
7680 (buffer (marker-buffer marker))
7681 (pos (marker-position marker))
7682 (org-insert-labeled-timestamps-at-point nil)
7684 (org-with-remote-undo buffer
7685 (with-current-buffer buffer
7686 (widen)
7687 (goto-char pos)
7688 (setq ts (org-deadline arg time)))
7689 (org-agenda-show-new-time marker ts "D"))
7690 (message "Deadline for this item set to %s" ts)))
7692 (defun org-agenda-action ()
7693 "Select entry for agenda action, or execute an agenda action.
7694 This command prompts for another letter. Valid inputs are:
7696 m Mark the entry at point for an agenda action
7697 s Schedule the marked entry to the date at the cursor
7698 d Set the deadline of the marked entry to the date at the cursor
7699 r Call `org-remember' with cursor date as the default date
7700 c Call `org-capture' with cursor date as the default date
7701 SPC Show marked entry in other window
7702 TAB Visit marked entry in other window
7704 The cursor may be at a date in the calendar, or in the Org agenda."
7705 (interactive)
7706 (let (ans)
7707 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7708 (setq ans (read-char-exclusive))
7709 (cond
7710 ((equal ans ?m)
7711 ;; Mark this entry
7712 (if (eq major-mode 'org-agenda-mode)
7713 (let ((m (or (org-get-at-bol 'org-hd-marker)
7714 (org-get-at-bol 'org-marker))))
7715 (if m
7716 (progn
7717 (move-marker org-agenda-action-marker
7718 (marker-position m) (marker-buffer m))
7719 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7720 (error "Don't know which entry to mark")))
7721 (error "This command works only in the agenda")))
7722 ((equal ans ?s)
7723 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7724 ((equal ans ?d)
7725 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7726 ((equal ans ?r)
7727 (org-agenda-do-action '(org-remember) t))
7728 ((equal ans ?c)
7729 (org-agenda-do-action '(org-capture) t))
7730 ((equal ans ?\ )
7731 (let ((cw (selected-window)))
7732 (org-switch-to-buffer-other-window
7733 (marker-buffer org-agenda-action-marker))
7734 (goto-char org-agenda-action-marker)
7735 (org-show-context 'agenda)
7736 (select-window cw)))
7737 ((equal ans ?\C-i)
7738 (org-switch-to-buffer-other-window
7739 (marker-buffer org-agenda-action-marker))
7740 (goto-char org-agenda-action-marker)
7741 (org-show-context 'agenda))
7742 (t (error "Invalid agenda action %c" ans)))))
7744 (defun org-agenda-do-action (form &optional current-buffer)
7745 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7746 (let ((org-overriding-default-time (org-get-cursor-date)))
7747 (if current-buffer
7748 (eval form)
7749 (if (not (marker-buffer org-agenda-action-marker))
7750 (error "No entry has been selected for agenda action")
7751 (with-current-buffer (marker-buffer org-agenda-action-marker)
7752 (save-excursion
7753 (save-restriction
7754 (widen)
7755 (goto-char org-agenda-action-marker)
7756 (eval form))))))))
7758 (defun org-agenda-clock-in (&optional arg)
7759 "Start the clock on the currently selected item."
7760 (interactive "P")
7761 (org-agenda-check-no-diary)
7762 (if (equal arg '(4))
7763 (org-clock-in arg)
7764 (let* ((marker (or (org-get-at-bol 'org-marker)
7765 (org-agenda-error)))
7766 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7767 marker))
7768 (pos (marker-position marker))
7769 newhead)
7770 (org-with-remote-undo (marker-buffer marker)
7771 (with-current-buffer (marker-buffer marker)
7772 (widen)
7773 (goto-char pos)
7774 (org-show-context 'agenda)
7775 (org-show-entry)
7776 (org-cycle-hide-drawers 'children)
7777 (org-clock-in arg)
7778 (setq newhead (org-get-heading)))
7779 (org-agenda-change-all-lines newhead hdmarker)))))
7781 (defun org-agenda-clock-out ()
7782 "Stop the currently running clock."
7783 (interactive)
7784 (unless (marker-buffer org-clock-marker)
7785 (error "No running clock"))
7786 (let ((marker (make-marker)) newhead)
7787 (org-with-remote-undo (marker-buffer org-clock-marker)
7788 (with-current-buffer (marker-buffer org-clock-marker)
7789 (save-excursion
7790 (save-restriction
7791 (widen)
7792 (goto-char org-clock-marker)
7793 (org-back-to-heading t)
7794 (move-marker marker (point))
7795 (org-clock-out)
7796 (setq newhead (org-get-heading))))))
7797 (org-agenda-change-all-lines newhead marker)
7798 (move-marker marker nil)))
7800 (defun org-agenda-clock-cancel (&optional arg)
7801 "Cancel the currently running clock."
7802 (interactive "P")
7803 (unless (marker-buffer org-clock-marker)
7804 (error "No running clock"))
7805 (org-with-remote-undo (marker-buffer org-clock-marker)
7806 (org-clock-cancel)))
7808 (defun org-agenda-clock-goto ()
7809 "Jump to the currently clocked in task within the agenda.
7810 If the currently clocked in task is not listed in the agenda
7811 buffer, display it in another window."
7812 (interactive)
7813 (let (pos)
7814 (mapc (lambda (o)
7815 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7816 (setq pos (overlay-start o))))
7817 (overlays-in (point-min) (point-max)))
7818 (cond (pos (goto-char pos))
7819 ;; If the currently clocked entry is not in the agenda
7820 ;; buffer, we visit it in another window:
7821 (org-clock-current-task
7822 (org-switch-to-buffer-other-window (org-clock-goto)))
7823 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7825 (defun org-agenda-diary-entry-in-org-file ()
7826 "Make a diary entry in the file `org-agenda-diary-file'."
7827 (let (d1 d2 char (text "") dp1 dp2)
7828 (if (equal (buffer-name) "*Calendar*")
7829 (setq d1 (calendar-cursor-to-date t)
7830 d2 (car calendar-mark-ring))
7831 (setq dp1 (get-text-property (point-at-bol) 'day))
7832 (unless dp1 (error "No date defined in current line"))
7833 (setq d1 (calendar-gregorian-from-absolute dp1)
7834 d2 (and (ignore-errors (mark))
7835 (save-excursion
7836 (goto-char (mark))
7837 (setq dp2 (get-text-property (point-at-bol) 'day)))
7838 (calendar-gregorian-from-absolute dp2))))
7839 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7840 (setq char (read-char-exclusive))
7841 (cond
7842 ((equal char ?d)
7843 (setq text (read-string "Day entry: "))
7844 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7845 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7846 ((equal char ?a)
7847 (setq d1 (list (car d1) (nth 1 d1)
7848 (read-number (format "Reference year [%d]: " (nth 2 d1))
7849 (nth 2 d1))))
7850 (setq text (read-string "Anniversary (use %d to show years): "))
7851 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7852 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7853 ((equal char ?b)
7854 (setq text (read-string "Block entry: "))
7855 (unless (and d1 d2 (not (equal d1 d2)))
7856 (error "No block of days selected"))
7857 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7858 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7859 ((equal char ?j)
7860 (org-switch-to-buffer-other-window
7861 (find-file-noselect org-agenda-diary-file))
7862 (require 'org-datetree)
7863 (org-datetree-find-date-create d1)
7864 (org-reveal t))
7865 (t (error "Invalid selection character `%c'" char)))))
7867 (defcustom org-agenda-insert-diary-strategy 'date-tree
7868 "Where in `org-agenda-diary-file' should new entries be added?
7869 Valid values:
7871 date-tree in the date tree, as child of the date
7872 top-level as top-level entries at the end of the file."
7873 :group 'org-agenda
7874 :type '(choice
7875 (const :tag "in a date tree" date-tree)
7876 (const :tag "as top level at end of file" top-level)))
7878 (defcustom org-agenda-insert-diary-extract-time nil
7879 "Non-nil means extract any time specification from the diary entry."
7880 :group 'org-agenda
7881 :type 'boolean)
7883 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7884 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7885 If TEXT is not empty, it will become the headline of the new entry, and
7886 the resulting entry will not be shown. When TEXT is empty, switch to
7887 `org-agenda-diary-file' and let the user finish the entry there."
7888 (let ((cw (current-window-configuration)))
7889 (org-switch-to-buffer-other-window
7890 (find-file-noselect org-agenda-diary-file))
7891 (widen)
7892 (goto-char (point-min))
7893 (cond
7894 ((eq type 'anniversary)
7895 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7896 (progn
7897 (or (org-on-heading-p t)
7898 (progn
7899 (outline-next-heading)
7900 (insert "* Anniversaries\n\n")
7901 (beginning-of-line -1)))))
7902 (outline-next-heading)
7903 (org-back-over-empty-lines)
7904 (backward-char 1)
7905 (insert "\n")
7906 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7907 (nth 2 d1) (car d1) (nth 1 d1) text)))
7908 ((eq type 'day)
7909 (let ((org-prefix-has-time t)
7910 (org-agenda-time-leading-zero t)
7911 fmt time time2)
7912 (if org-agenda-insert-diary-extract-time
7913 ;; Use org-agenda-format-item to parse text for a time-range and
7914 ;; remove it. FIXME: This is a hack, we should refactor
7915 ;; that function to make time extraction available separately
7916 (setq fmt (org-agenda-format-item nil text nil nil t)
7917 time (get-text-property 0 'time fmt)
7918 time2 (if (> (length time) 0)
7919 ;; split-string removes trailing ...... if
7920 ;; no end time given. First space
7921 ;; separates time from date.
7922 (concat " " (car (split-string time "\\.")))
7923 nil)
7924 text (get-text-property 0 'txt fmt)))
7925 (if (eq org-agenda-insert-diary-strategy 'top-level)
7926 (org-agenda-insert-diary-as-top-level text)
7927 (require 'org-datetree)
7928 (org-datetree-find-date-create d1)
7929 (org-agenda-insert-diary-make-new-entry text))
7930 (org-insert-time-stamp (org-time-from-absolute
7931 (calendar-absolute-from-gregorian d1))
7932 nil nil nil nil time2))
7933 (end-of-line 0))
7934 ((eq type 'block)
7935 (if (> (calendar-absolute-from-gregorian d1)
7936 (calendar-absolute-from-gregorian d2))
7937 (setq d1 (prog1 d2 (setq d2 d1))))
7938 (if (eq org-agenda-insert-diary-strategy 'top-level)
7939 (org-agenda-insert-diary-as-top-level text)
7940 (require 'org-datetree)
7941 (org-datetree-find-date-create d1)
7942 (org-agenda-insert-diary-make-new-entry text))
7943 (org-insert-time-stamp (org-time-from-absolute
7944 (calendar-absolute-from-gregorian d1)))
7945 (insert "--")
7946 (org-insert-time-stamp (org-time-from-absolute
7947 (calendar-absolute-from-gregorian d2)))
7948 (end-of-line 0)))
7949 (if (string-match "\\S-" text)
7950 (progn
7951 (set-window-configuration cw)
7952 (message "%s entry added to %s"
7953 (capitalize (symbol-name type))
7954 (abbreviate-file-name org-agenda-diary-file)))
7955 (org-reveal t)
7956 (message "Please finish entry here"))))
7958 (defun org-agenda-insert-diary-as-top-level (text)
7959 "Make new entry as a top-level entry at the end of the file.
7960 Add TEXT as headline, and position the cursor in the second line so that
7961 a timestamp can be added there."
7962 (widen)
7963 (goto-char (point-max))
7964 (or (bolp) (insert "\n"))
7965 (insert "* " text "\n")
7966 (if org-adapt-indentation (org-indent-to-column 2)))
7968 (defun org-agenda-insert-diary-make-new-entry (text)
7969 "Make new entry as last child of current entry.
7970 Add TEXT as headline, and position the cursor in the second line so that
7971 a timestamp can be added there."
7972 (let ((org-show-following-heading t)
7973 (org-show-siblings t)
7974 (org-show-hierarchy-above t)
7975 (org-show-entry-below t)
7976 col)
7977 (outline-next-heading)
7978 (org-back-over-empty-lines)
7979 (or (looking-at "[ \t]*$")
7980 (progn (insert "\n") (backward-char 1)))
7981 (org-insert-heading nil t)
7982 (org-do-demote)
7983 (setq col (current-column))
7984 (insert text "\n")
7985 (if org-adapt-indentation (org-indent-to-column col))
7986 (let ((org-show-following-heading t)
7987 (org-show-siblings t)
7988 (org-show-hierarchy-above t)
7989 (org-show-entry-below t))
7990 (org-show-context))))
7992 (defun org-agenda-diary-entry ()
7993 "Make a diary entry, like the `i' command from the calendar.
7994 All the standard commands work: block, weekly etc.
7995 When `org-agenda-diary-file' points to a file,
7996 `org-agenda-diary-entry-in-org-file' is called instead to create
7997 entries in that Org-mode file."
7998 (interactive)
7999 (org-agenda-check-type t 'agenda 'timeline)
8000 (if (not (eq org-agenda-diary-file 'diary-file))
8001 (org-agenda-diary-entry-in-org-file)
8002 (require 'diary-lib)
8003 (let* ((char (progn
8004 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8005 (read-char-exclusive)))
8006 (cmd (cdr (assoc char
8007 '((?d . insert-diary-entry)
8008 (?w . insert-weekly-diary-entry)
8009 (?m . insert-monthly-diary-entry)
8010 (?y . insert-yearly-diary-entry)
8011 (?a . insert-anniversary-diary-entry)
8012 (?b . insert-block-diary-entry)
8013 (?c . insert-cyclic-diary-entry)))))
8014 (oldf (symbol-function 'calendar-cursor-to-date))
8015 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8016 (point (point))
8017 (mark (or (mark t) (point))))
8018 (unless cmd
8019 (error "No command associated with <%c>" char))
8020 (unless (and (get-text-property point 'day)
8021 (or (not (equal ?b char))
8022 (get-text-property mark 'day)))
8023 (error "Don't know which date to use for diary entry"))
8024 ;; We implement this by hacking the `calendar-cursor-to-date' function
8025 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8026 (let ((calendar-mark-ring
8027 (list (calendar-gregorian-from-absolute
8028 (or (get-text-property mark 'day)
8029 (get-text-property point 'day))))))
8030 (unwind-protect
8031 (progn
8032 (fset 'calendar-cursor-to-date
8033 (lambda (&optional error dummy)
8034 (calendar-gregorian-from-absolute
8035 (get-text-property point 'day))))
8036 (call-interactively cmd))
8037 (fset 'calendar-cursor-to-date oldf))))))
8039 (defun org-agenda-execute-calendar-command (cmd)
8040 "Execute a calendar command from the agenda, with the date associated to
8041 the cursor position."
8042 (org-agenda-check-type t 'agenda 'timeline)
8043 (require 'diary-lib)
8044 (unless (get-text-property (point) 'day)
8045 (error "Don't know which date to use for calendar command"))
8046 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8047 (point (point))
8048 (date (calendar-gregorian-from-absolute
8049 (get-text-property point 'day)))
8050 ;; the following 2 vars are needed in the calendar
8051 (displayed-month (car date))
8052 (displayed-year (nth 2 date)))
8053 (unwind-protect
8054 (progn
8055 (fset 'calendar-cursor-to-date
8056 (lambda (&optional error dummy)
8057 (calendar-gregorian-from-absolute
8058 (get-text-property point 'day))))
8059 (call-interactively cmd))
8060 (fset 'calendar-cursor-to-date oldf))))
8062 (defun org-agenda-phases-of-moon ()
8063 "Display the phases of the moon for the 3 months around the cursor date."
8064 (interactive)
8065 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8067 (defun org-agenda-holidays ()
8068 "Display the holidays for the 3 months around the cursor date."
8069 (interactive)
8070 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8072 (defvar calendar-longitude)
8073 (defvar calendar-latitude)
8074 (defvar calendar-location-name)
8076 (defun org-agenda-sunrise-sunset (arg)
8077 "Display sunrise and sunset for the cursor date.
8078 Latitude and longitude can be specified with the variables
8079 `calendar-latitude' and `calendar-longitude'. When called with prefix
8080 argument, latitude and longitude will be prompted for."
8081 (interactive "P")
8082 (require 'solar)
8083 (let ((calendar-longitude (if arg nil calendar-longitude))
8084 (calendar-latitude (if arg nil calendar-latitude))
8085 (calendar-location-name
8086 (if arg "the given coordinates" calendar-location-name)))
8087 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8089 (defun org-agenda-goto-calendar ()
8090 "Open the Emacs calendar with the date at the cursor."
8091 (interactive)
8092 (org-agenda-check-type t 'agenda 'timeline)
8093 (let* ((day (or (get-text-property (point) 'day)
8094 (error "Don't know which date to open in calendar")))
8095 (date (calendar-gregorian-from-absolute day))
8096 (calendar-move-hook nil)
8097 (calendar-view-holidays-initially-flag nil)
8098 (calendar-view-diary-initially-flag nil))
8099 (calendar)
8100 (calendar-goto-date date)))
8102 ;;;###autoload
8103 (defun org-calendar-goto-agenda ()
8104 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8105 This is a command that has to be installed in `calendar-mode-map'."
8106 (interactive)
8107 (org-agenda-list nil (calendar-absolute-from-gregorian
8108 (calendar-cursor-to-date))
8109 nil))
8111 (defun org-agenda-convert-date ()
8112 (interactive)
8113 (org-agenda-check-type t 'agenda 'timeline)
8114 (let ((day (get-text-property (point) 'day))
8115 date s)
8116 (unless day
8117 (error "Don't know which date to convert"))
8118 (setq date (calendar-gregorian-from-absolute day))
8119 (setq s (concat
8120 "Gregorian: " (calendar-date-string date) "\n"
8121 "ISO: " (calendar-iso-date-string date) "\n"
8122 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8123 "Julian: " (calendar-julian-date-string date) "\n"
8124 "Astron. JD: " (calendar-astro-date-string date)
8125 " (Julian date number at noon UTC)\n"
8126 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8127 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8128 "French: " (calendar-french-date-string date) "\n"
8129 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8130 "Mayan: " (calendar-mayan-date-string date) "\n"
8131 "Coptic: " (calendar-coptic-date-string date) "\n"
8132 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8133 "Persian: " (calendar-persian-date-string date) "\n"
8134 "Chinese: " (calendar-chinese-date-string date) "\n"))
8135 (with-output-to-temp-buffer "*Dates*"
8136 (princ s))
8137 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8139 ;;; Bulk commands
8141 (defvar org-agenda-bulk-marked-entries nil
8142 "List of markers that refer to marked entries in the agenda.")
8144 (defun org-agenda-bulk-marked-p ()
8145 (eq (get-char-property (point-at-bol) 'type)
8146 'org-marked-entry-overlay))
8148 (defun org-agenda-bulk-mark (&optional arg)
8149 "Mark the entry at point for future bulk action."
8150 (interactive "p")
8151 (dotimes (i (max arg 1))
8152 (unless (org-get-at-bol 'org-agenda-diary-link)
8153 (let* ((m (org-get-at-bol 'org-hd-marker))
8155 (unless (org-agenda-bulk-marked-p)
8156 (unless m (error "Nothing to mark at point"))
8157 (push m org-agenda-bulk-marked-entries)
8158 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8159 (org-overlay-display ov "> "
8160 (org-get-todo-face "TODO")
8161 'evaporate)
8162 (overlay-put ov 'type 'org-marked-entry-overlay))
8163 (beginning-of-line 2)
8164 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8165 (beginning-of-line 2))
8166 (message "%d entries marked for bulk action"
8167 (length org-agenda-bulk-marked-entries))))))
8169 (defun org-agenda-bulk-mark-regexp (regexp)
8170 "Mark entries match REGEXP."
8171 (interactive "sMark entries matching regexp: ")
8172 (let (entries-marked)
8173 (save-excursion
8174 (goto-char (point-min))
8175 (goto-char (next-single-property-change (point) 'txt))
8176 (while (re-search-forward regexp nil t)
8177 (when (string-match regexp (get-text-property (point) 'txt))
8178 (setq entries-marked (+ entries-marked 1))
8179 (call-interactively 'org-agenda-bulk-mark))))
8180 (if (not entries-marked)
8181 (message "No entry matching this regexp."))))
8183 (defun org-agenda-bulk-unmark ()
8184 "Unmark the entry at point for future bulk action."
8185 (interactive)
8186 (when (org-agenda-bulk-marked-p)
8187 (org-agenda-bulk-remove-overlays
8188 (point-at-bol) (+ 2 (point-at-bol)))
8189 (setq org-agenda-bulk-marked-entries
8190 (delete (org-get-at-bol 'org-hd-marker)
8191 org-agenda-bulk-marked-entries)))
8192 (beginning-of-line 2)
8193 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8194 (beginning-of-line 2))
8195 (message "%d entries marked for bulk action"
8196 (length org-agenda-bulk-marked-entries)))
8198 (defun org-agenda-bulk-toggle ()
8199 "Toggle marking the entry at point for bulk action."
8200 (interactive)
8201 (if (org-agenda-bulk-marked-p)
8202 (org-agenda-bulk-unmark)
8203 (org-agenda-bulk-mark)))
8205 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8206 "Remove the mark overlays between BEG and END in the agenda buffer.
8207 BEG and END default to the buffer limits.
8209 This only removes the overlays, it does not remove the markers
8210 from the list in `org-agenda-bulk-marked-entries'."
8211 (interactive)
8212 (mapc (lambda (ov)
8213 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8214 (delete-overlay ov)))
8215 (overlays-in (or beg (point-min)) (or end (point-max)))))
8217 (defun org-agenda-bulk-remove-all-marks ()
8218 "Remove all marks in the agenda buffer.
8219 This will remove the markers, and the overlays."
8220 (interactive)
8221 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8222 (setq org-agenda-bulk-marked-entries nil)
8223 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8225 (defun org-agenda-bulk-action (&optional arg)
8226 "Execute an remote-editing action on all marked entries.
8227 The prefix arg is passed through to the command if possible."
8228 (interactive "P")
8229 ;; Make sure we have markers, and only valid ones
8230 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8231 (mapc
8232 (lambda (m)
8233 (unless (and (markerp m)
8234 (marker-buffer m)
8235 (buffer-live-p (marker-buffer m))
8236 (marker-position m))
8237 (error "Marker %s for bulk command is invalid" m)))
8238 org-agenda-bulk-marked-entries)
8240 ;; Prompt for the bulk command
8241 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8242 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8243 (when org-agenda-bulk-custom-functions
8244 (concat " Custom: ["
8245 (mapconcat (lambda(f) (char-to-string (car f)))
8246 org-agenda-bulk-custom-functions "")
8247 "]"))))
8248 (let* ((action (read-char-exclusive))
8249 (org-log-refile (if org-log-refile 'time nil))
8250 (entries (reverse org-agenda-bulk-marked-entries))
8251 redo-at-end
8252 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8253 (cond
8254 ((equal action ?$)
8255 (setq cmd '(org-agenda-archive)))
8257 ((equal action ?A)
8258 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8260 ((member action '(?r ?w))
8261 (setq rfloc (org-refile-get-location
8262 "Refile to"
8263 (marker-buffer (car org-agenda-bulk-marked-entries))
8264 org-refile-allow-creating-parent-nodes))
8265 (if (nth 3 rfloc)
8266 (setcar (nthcdr 3 rfloc)
8267 (move-marker (make-marker) (nth 3 rfloc)
8268 (or (get-file-buffer (nth 1 rfloc))
8269 (find-buffer-visiting (nth 1 rfloc))
8270 (error "This should not happen")))))
8272 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8273 redo-at-end t))
8275 ((equal action ?t)
8276 (setq state (org-icompleting-read
8277 "Todo state: "
8278 (with-current-buffer (marker-buffer (car entries))
8279 (mapcar 'list org-todo-keywords-1))))
8280 (setq cmd `(let ((org-inhibit-blocking t)
8281 (org-inhibit-logging 'note))
8282 (org-agenda-todo ,state))))
8284 ((memq action '(?- ?+))
8285 (setq tag (org-icompleting-read
8286 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8287 (with-current-buffer (marker-buffer (car entries))
8288 (delq nil
8289 (mapcar (lambda (x)
8290 (if (stringp (car x)) x)) org-tag-alist)))))
8291 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8293 ((memq action '(?s ?d))
8294 (let* ((date (unless arg
8295 (org-read-date
8296 nil nil nil
8297 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8298 (ans (if arg nil org-read-date-final-answer))
8299 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8300 (setq cmd `(let* ((bound (fboundp 'read-string))
8301 (old (and bound (symbol-function 'read-string))))
8302 (unwind-protect
8303 (progn
8304 (fset 'read-string (lambda (&rest ignore) ,ans))
8305 (eval '(,c1 arg)))
8306 (if bound
8307 (fset 'read-string old)
8308 (fmakunbound 'read-string)))))))
8310 ((equal action ?S)
8311 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8312 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8313 (let ((days (read-number
8314 (format "Scatter tasks across how many %sdays: "
8315 (if arg "week" "")) 7)))
8316 (setq cmd
8317 `(let ((distance (1+ (random ,days))))
8318 (if arg
8319 (let ((dist distance)
8320 (day-of-week
8321 (calendar-day-of-week
8322 (calendar-gregorian-from-absolute (org-today)))))
8323 (dotimes (i (1+ dist))
8324 (while (member day-of-week org-agenda-weekend-days)
8325 (incf distance)
8326 (incf day-of-week)
8327 (if (= day-of-week 7)
8328 (setq day-of-week 0)))
8329 (incf day-of-week)
8330 (if (= day-of-week 7)
8331 (setq day-of-week 0)))))
8332 ;; silently fail when try to replan a sexp entry
8333 (condition-case nil
8334 (let* ((date (calendar-gregorian-from-absolute
8335 (+ (org-today) distance)))
8336 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8337 (nth 2 date))))
8338 (org-agenda-schedule nil time))
8339 (error nil)))))))
8341 ((assoc action org-agenda-bulk-custom-functions)
8342 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8343 redo-at-end t))
8345 ((equal action ?f)
8346 (setq cmd (list (intern
8347 (org-icompleting-read "Function: "
8348 obarray 'fboundp t nil nil)))))
8350 (t (error "Invalid bulk action")))
8352 ;; Sort the markers, to make sure that parents are handled before children
8353 (setq entries (sort entries
8354 (lambda (a b)
8355 (cond
8356 ((equal (marker-buffer a) (marker-buffer b))
8357 (< (marker-position a) (marker-position b)))
8359 (string< (buffer-name (marker-buffer a))
8360 (buffer-name (marker-buffer b))))))))
8362 ;; Now loop over all markers and apply cmd
8363 (while (setq e (pop entries))
8364 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8365 (if (not pos)
8366 (progn (message "Skipping removed entry at %s" e)
8367 (setq cntskip (1+ cntskip)))
8368 (goto-char pos)
8369 (let (org-loop-over-headlines-in-active-region)
8370 (eval cmd))
8371 (setq org-agenda-bulk-marked-entries
8372 (delete e org-agenda-bulk-marked-entries))
8373 (setq cnt (1+ cnt))))
8374 (setq org-agenda-bulk-marked-entries nil)
8375 (org-agenda-bulk-remove-all-marks)
8376 (when redo-at-end (org-agenda-redo))
8377 (message "Acted on %d entries%s"
8379 (if (= cntskip 0)
8381 (format ", skipped %d (disappeared before their turn)"
8382 cntskip)))))
8384 ;;; Flagging notes
8386 (defun org-agenda-show-the-flagging-note ()
8387 "Display the flagging note in the other window.
8388 When called a second time in direct sequence, offer to remove the FLAGGING
8389 tag and (if present) the flagging note."
8390 (interactive)
8391 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8392 (win (selected-window))
8393 note heading newhead)
8394 (unless hdmarker
8395 (error "No linked entry at point"))
8396 (if (and (eq this-command last-command)
8397 (y-or-n-p "Unflag and remove any flagging note? "))
8398 (progn
8399 (org-agenda-remove-flag hdmarker)
8400 (let ((win (get-buffer-window "*Flagging Note*")))
8401 (and win (delete-window win)))
8402 (message "Entry unflaged"))
8403 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8404 (unless note
8405 (error "No flagging note"))
8406 (org-kill-new note)
8407 (org-switch-to-buffer-other-window "*Flagging Note*")
8408 (erase-buffer)
8409 (insert note)
8410 (goto-char (point-min))
8411 (while (re-search-forward "\\\\n" nil t)
8412 (replace-match "\n" t t))
8413 (goto-char (point-min))
8414 (select-window win)
8415 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8417 (defun org-agenda-remove-flag (marker)
8418 "Remove the FLAGGED tag and any flagging note in the entry."
8419 (let (newhead)
8420 (org-with-point-at marker
8421 (org-toggle-tag "FLAGGED" 'off)
8422 (org-entry-delete nil "THEFLAGGINGNOTE")
8423 (setq newhead (org-get-heading)))
8424 (org-agenda-change-all-lines newhead marker)
8425 (message "Entry unflaged")))
8427 (defun org-agenda-get-any-marker (&optional pos)
8428 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8429 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8431 ;;; Appointment reminders
8433 (defvar appt-time-msg-list)
8435 ;;;###autoload
8436 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8437 "Activate appointments found in `org-agenda-files'.
8438 With a \\[universal-argument] prefix, refresh the list of
8439 appointments.
8441 If FILTER is t, interactively prompt the user for a regular
8442 expression, and filter out entries that don't match it.
8444 If FILTER is a string, use this string as a regular expression
8445 for filtering entries out.
8447 If FILTER is a function, filter out entries against which
8448 calling the function returns nil. This function takes one
8449 argument: an entry from `org-agenda-get-day-entries'.
8451 FILTER can also be an alist with the car of each cell being
8452 either 'headline or 'category. For example:
8454 '((headline \"IMPORTANT\")
8455 (category \"Work\"))
8457 will only add headlines containing IMPORTANT or headlines
8458 belonging to the \"Work\" category.
8460 ARGS are symbols indicating what kind of entries to consider.
8461 By default `org-agenda-to-appt' will use :deadline, :scheduled
8462 and :timestamp entries. See the docstring of `org-diary' for
8463 details and examples."
8464 (interactive "P")
8465 (if refresh (setq appt-time-msg-list nil))
8466 (if (eq filter t)
8467 (setq filter (read-from-minibuffer "Regexp filter: ")))
8468 (let* ((cnt 0) ; count added events
8469 (scope (or args '(:deadline :scheduled :timestamp)))
8470 (org-agenda-new-buffers nil)
8471 (org-deadline-warning-days 0)
8472 ;; Do not use `org-today' here because appt only takes
8473 ;; time and without date as argument, so it may pass wrong
8474 ;; information otherwise
8475 (today (org-date-to-gregorian
8476 (time-to-days (current-time))))
8477 (org-agenda-restrict nil)
8478 (files (org-agenda-files 'unrestricted)) entries file)
8479 ;; Get all entries which may contain an appt
8480 (org-prepare-agenda-buffers files)
8481 (while (setq file (pop files))
8482 (setq entries
8483 (delq nil
8484 (append entries
8485 (apply 'org-agenda-get-day-entries
8486 file today scope)))))
8487 ;; Map thru entries and find if we should filter them out
8488 (mapc
8489 (lambda(x)
8490 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8491 (cat (get-text-property 1 'org-category x))
8492 (tod (get-text-property 1 'time-of-day x))
8493 (ok (or (null filter)
8494 (and (stringp filter) (string-match filter evt))
8495 (and (functionp filter) (funcall filter x))
8496 (and (listp filter)
8497 (let ((cat-filter (cadr (assoc 'category filter)))
8498 (evt-filter (cadr (assoc 'headline filter))))
8499 (or (and (stringp cat-filter)
8500 (string-match cat-filter cat))
8501 (and (stringp evt-filter)
8502 (string-match evt-filter evt))))))))
8503 ;; FIXME: Shall we remove text-properties for the appt text?
8504 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8505 (when (and ok tod)
8506 (setq tod (concat "00" (number-to-string tod))
8507 tod (when (string-match
8508 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8509 (concat (match-string 1 tod) ":"
8510 (match-string 2 tod))))
8511 (appt-add tod evt)
8512 (setq cnt (1+ cnt))))) entries)
8513 (org-release-buffers org-agenda-new-buffers)
8514 (if (eq cnt 0)
8515 (message "No event to add")
8516 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8518 (defun org-agenda-todayp (date)
8519 "Does DATE mean today, when considering `org-extend-today-until'?"
8520 (let ((today (org-today))
8521 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8522 date)))
8523 (eq date today)))
8525 (defun org-agenda-todo-yesterday (&optional arg)
8526 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8527 (interactive "P")
8528 (let* ((hour (third (decode-time
8529 (org-current-time))))
8530 (org-extend-today-until (1+ hour)))
8531 (org-agenda-todo arg)))
8533 (provide 'org-agenda)
8535 ;;; org-agenda.el ends here