Merge branch 'refactor/org-macros'
[org-mode.git] / lisp / org-agenda.el
blob7e346a7a4b7bfd95b0c23a6e541d275ab3b877a1
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.7
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the code for creating and using the Agenda for Org-mode.
31 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
32 ;; `org-batch-store-agenda-views' are implemented as macros to provide
33 ;; a conveniant way for extracting agenda information from the command
34 ;; line. The Lisp does not evaluate parameters of a macro call; thus
35 ;; it is not necessary to quote the parameters passed to one of those
36 ;; functions. E.g. you can write:
38 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
40 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
41 ;; have been implemented as a regular function you'd have to quote the
42 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
43 ;; value you would have to double quote the symbol.
45 ;; This is a hack, but it works even when running Org byte-compiled.
48 ;;; Code:
50 (require 'org)
51 (eval-when-compile
52 (require 'cl))
54 (declare-function diary-add-to-list "diary-lib"
55 (date string specifier &optional marker globcolor literal))
56 (declare-function calendar-absolute-from-iso "cal-iso" (date))
57 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
58 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
59 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
60 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
62 (declare-function calendar-french-date-string "cal-french" (&optional date))
63 (declare-function calendar-goto-date "cal-move" (date))
64 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
65 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
66 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
67 (declare-function calendar-iso-from-absolute "cal-iso" (date))
68 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
69 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
70 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
71 (declare-function org-datetree-find-date-create "org-datetree"
72 (date &optional keep-restriction))
73 (declare-function org-columns-quit "org-colview" ())
74 (declare-function diary-date-display-form "diary-lib" (&optional type))
75 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
76 (declare-function org-habit-insert-consistency-graphs
77 "org-habit" (&optional line))
78 (declare-function org-is-habit-p "org-habit" (&optional pom))
79 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
80 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
81 (declare-function org-pop-to-buffer-same-window "org-compat"
82 (&optional buffer-or-name norecord label))
84 (defvar calendar-mode-map)
85 (defvar org-clock-current-task) ; defined in org-clock.el
86 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
87 (defvar org-habit-show-habits)
88 (defvar org-habit-show-habits-only-for-today)
90 ;; Defined somewhere in this file, but used before definition.
91 (defvar org-agenda-buffer-name)
92 (defvar org-agenda-overriding-header)
93 (defvar org-agenda-title-append nil)
94 (defvar entry)
95 (defvar date)
96 (defvar org-agenda-undo-list)
97 (defvar org-agenda-pending-undo-list)
98 (defvar original-date) ; dynamically scoped, calendar.el does scope this
100 (defcustom org-agenda-confirm-kill 1
101 "When set, remote killing from the agenda buffer needs confirmation.
102 When t, a confirmation is always needed. When a number N, confirmation is
103 only needed when the text to be killed contains more than N non-white lines."
104 :group 'org-agenda
105 :type '(choice
106 (const :tag "Never" nil)
107 (const :tag "Always" t)
108 (integer :tag "When more than N lines")))
110 (defcustom org-agenda-compact-blocks nil
111 "Non-nil means make the block agenda more compact.
112 This is done globally by leaving out lines like the agenda span
113 name and week number or the separator lines."
114 :group 'org-agenda
115 :type 'boolean)
117 (defcustom org-agenda-block-separator ?=
118 "The separator between blocks in the agenda.
119 If this is a string, it will be used as the separator, with a newline added.
120 If it is a character, it will be repeated to fill the window width.
121 If nil the separator is disabled. In `org-agenda-custom-commands' this
122 addresses the separator between the current and the previous block."
123 :group 'org-agenda
124 :type '(choice
125 (const :tag "Disabled" nil)
126 (character)
127 (string)))
129 (defgroup org-agenda-export nil
130 "Options concerning exporting agenda views in Org-mode."
131 :tag "Org Agenda Export"
132 :group 'org-agenda)
134 (defcustom org-agenda-with-colors t
135 "Non-nil means use colors in agenda views."
136 :group 'org-agenda-export
137 :type 'boolean)
139 (defcustom org-agenda-exporter-settings nil
140 "Alist of variable/value pairs that should be active during agenda export.
141 This is a good place to set options for ps-print and for htmlize.
142 Note that the way this is implemented, the values will be evaluated
143 before assigned to the variables. So make sure to quote values you do
144 *not* want evaluated, for example
146 (setq org-agenda-exporter-settings
147 '((ps-print-color-p 'black-white)))"
148 :group 'org-agenda-export
149 :type '(repeat
150 (list
151 (variable)
152 (sexp :tag "Value"))))
154 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
155 "Hook run in temporary buffer before writing it to an export file.
156 A useful function is `org-agenda-add-entry-text'."
157 :group 'org-agenda-export
158 :type 'hook
159 :options '(org-agenda-add-entry-text))
161 (defcustom org-agenda-add-entry-text-maxlines 0
162 "Maximum number of entry text lines to be added to agenda.
163 This is only relevant when `org-agenda-add-entry-text' is part of
164 `org-agenda-before-write-hook', which it is by default.
165 When this is 0, nothing will happen. When it is greater than 0, it
166 specifies the maximum number of lines that will be added for each entry
167 that is listed in the agenda view.
169 Note that this variable is not used during display, only when exporting
170 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
171 and `org-agenda-entry-text-maxlines'."
172 :group 'org-agenda
173 :type 'integer)
175 (defcustom org-agenda-add-entry-text-descriptive-links t
176 "Non-nil means export org-links as descriptive links in agenda added text.
177 This variable applies to the text added to the agenda when
178 `org-agenda-add-entry-text-maxlines' is larger than 0.
179 When this variable nil, the URL will (also) be shown."
180 :group 'org-agenda
181 :type 'boolean)
183 (defcustom org-agenda-export-html-style ""
184 "The style specification for exported HTML Agenda files.
185 If this variable contains a string, it will replace the default <style>
186 section as produced by `htmlize'.
187 Since there are different ways of setting style information, this variable
188 needs to contain the full HTML structure to provide a style, including the
189 surrounding HTML tags. The style specifications should include definitions
190 the fonts used by the agenda, here is an example:
192 <style type=\"text/css\">
193 p { font-weight: normal; color: gray; }
194 .org-agenda-structure {
195 font-size: 110%;
196 color: #003399;
197 font-weight: 600;
199 .org-todo {
200 color: #cc6666;
201 font-weight: bold;
203 .org-agenda-done {
204 color: #339933;
206 .org-done {
207 color: #339933;
209 .title { text-align: center; }
210 .todo, .deadline { color: red; }
211 .done { color: green; }
212 </style>
214 or, if you want to keep the style in a file,
216 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
218 As the value of this option simply gets inserted into the HTML <head> header,
219 you can \"misuse\" it to also add other text to the header. However,
220 <style>...</style> is required, if not present the variable will be ignored."
221 :group 'org-agenda-export
222 :group 'org-export-html
223 :type 'string)
225 (defcustom org-agenda-persistent-filter nil
226 "When set, keep filters from one agenda view to the next."
227 :group 'org-agenda
228 :type 'boolean)
230 (defgroup org-agenda-custom-commands nil
231 "Options concerning agenda views in Org-mode."
232 :tag "Org Agenda Custom Commands"
233 :group 'org-agenda)
235 (defconst org-sorting-choice
236 '(choice
237 (const time-up) (const time-down)
238 (const category-keep) (const category-up) (const category-down)
239 (const tag-down) (const tag-up)
240 (const priority-up) (const priority-down)
241 (const todo-state-up) (const todo-state-down)
242 (const effort-up) (const effort-down)
243 (const habit-up) (const habit-down)
244 (const alpha-up) (const alpha-down)
245 (const user-defined-up) (const user-defined-down))
246 "Sorting choices.")
248 (defconst org-agenda-custom-commands-local-options
249 `(repeat :tag "Local settings for this command. Remember to quote values"
250 (choice :tag "Setting"
251 (list :tag "Heading for this block"
252 (const org-agenda-overriding-header)
253 (string :tag "Headline"))
254 (list :tag "Files to be searched"
255 (const org-agenda-files)
256 (list
257 (const :format "" quote)
258 (repeat (file))))
259 (list :tag "Sorting strategy"
260 (const org-agenda-sorting-strategy)
261 (list
262 (const :format "" quote)
263 (repeat
264 ,org-sorting-choice)))
265 (list :tag "Prefix format"
266 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
267 (string))
268 (list :tag "Number of days in agenda"
269 (const org-agenda-span)
270 (choice (const :tag "Day" 'day)
271 (const :tag "Week" 'week)
272 (const :tag "Month" 'month)
273 (const :tag "Year" 'year)
274 (integer :tag "Custom")))
275 (list :tag "Fixed starting date"
276 (const org-agenda-start-day)
277 (string :value "2007-11-01"))
278 (list :tag "Start on day of week"
279 (const org-agenda-start-on-weekday)
280 (choice :value 1
281 (const :tag "Today" nil)
282 (integer :tag "Weekday No.")))
283 (list :tag "Include data from diary"
284 (const org-agenda-include-diary)
285 (boolean))
286 (list :tag "Deadline Warning days"
287 (const org-deadline-warning-days)
288 (integer :value 1))
289 (list :tag "Tags filter preset"
290 (const org-agenda-filter-preset)
291 (list
292 (const :format "" quote)
293 (repeat
294 (string :tag "+tag or -tag"))))
295 (list :tag "Set daily/weekly entry types"
296 (const org-agenda-entry-types)
297 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
298 (const :deadline)
299 (const :scheduled)
300 (const :timestamp)
301 (const :sexp)))
302 (list :tag "Standard skipping condition"
303 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
304 (const org-agenda-skip-function)
305 (list
306 (const :format "" quote)
307 (list
308 (choice
309 :tag "Skipping range"
310 (const :tag "Skip entry" org-agenda-skip-entry-if)
311 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
312 (repeat :inline t :tag "Conditions for skipping"
313 (choice
314 :tag "Condition type"
315 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
316 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
317 (list :tag "TODO state is" :inline t
318 (const 'todo)
319 (choice
320 (const :tag "any not-done state" 'todo)
321 (const :tag "any done state" 'done)
322 (const :tag "any state" 'any)
323 (list :tag "Keyword list"
324 (const :format "" quote)
325 (repeat (string :tag "Keyword")))))
326 (list :tag "TODO state is not" :inline t
327 (const 'nottodo)
328 (choice
329 (const :tag "any not-done state" 'todo)
330 (const :tag "any done state" 'done)
331 (const :tag "any state" 'any)
332 (list :tag "Keyword list"
333 (const :format "" quote)
334 (repeat (string :tag "Keyword")))))
335 (const :tag "scheduled" 'scheduled)
336 (const :tag "not scheduled" 'notscheduled)
337 (const :tag "deadline" 'deadline)
338 (const :tag "no deadline" 'notdeadline)
339 (const :tag "timestamp" 'timestamp)
340 (const :tag "no timestamp" 'nottimestamp))))))
341 (list :tag "Non-standard skipping condition"
342 :value (org-agenda-skip-function)
343 (const org-agenda-skip-function)
344 (sexp :tag "Function or form (quoted!)"))
345 (list :tag "Any variable"
346 (variable :tag "Variable")
347 (sexp :tag "Value (sexp)"))))
348 "Selection of examples for agenda command settings.
349 This will be spliced into the custom type of
350 `org-agenda-custom-commands'.")
353 (defcustom org-agenda-custom-commands nil
354 "Custom commands for the agenda.
355 These commands will be offered on the splash screen displayed by the
356 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
358 (key desc type match settings files)
360 key The key (one or more characters as a string) to be associated
361 with the command.
362 desc A description of the command, when omitted or nil, a default
363 description is built using MATCH.
364 type The command type, any of the following symbols:
365 agenda The daily/weekly agenda.
366 todo Entries with a specific TODO keyword, in all agenda files.
367 search Entries containing search words entry or headline.
368 tags Tags/Property/TODO match in all agenda files.
369 tags-todo Tags/P/T match in all agenda files, TODO entries only.
370 todo-tree Sparse tree of specific TODO keyword in *current* file.
371 tags-tree Sparse tree with all tags matches in *current* file.
372 occur-tree Occur sparse tree for *current* file.
373 ... A user-defined function.
374 match What to search for:
375 - a single keyword for TODO keyword searches
376 - a tags match expression for tags searches
377 - a word search expression for text searches.
378 - a regular expression for occur searches
379 For all other commands, this should be the empty string.
380 settings A list of option settings, similar to that in a let form, so like
381 this: ((opt1 val1) (opt2 val2) ...). The values will be
382 evaluated at the moment of execution, so quote them when needed.
383 files A list of files file to write the produced agenda buffer to
384 with the command `org-store-agenda-views'.
385 If a file name ends in \".html\", an HTML version of the buffer
386 is written out. If it ends in \".ps\", a postscript version is
387 produced. Otherwise, only the plain text is written to the file.
389 You can also define a set of commands, to create a composite agenda buffer.
390 In this case, an entry looks like this:
392 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
394 where
396 desc A description string to be displayed in the dispatcher menu.
397 cmd An agenda command, similar to the above. However, tree commands
398 are no allowed, but instead you can get agenda and global todo list.
399 So valid commands for a set are:
400 (agenda \"\" settings)
401 (alltodo \"\" settings)
402 (stuck \"\" settings)
403 (todo \"match\" settings files)
404 (search \"match\" settings files)
405 (tags \"match\" settings files)
406 (tags-todo \"match\" settings files)
408 Each command can carry a list of options, and another set of options can be
409 given for the whole set of commands. Individual command options take
410 precedence over the general options.
412 When using several characters as key to a command, the first characters
413 are prefix commands. For the dispatcher to display useful information, you
414 should provide a description for the prefix, like
416 (setq org-agenda-custom-commands
417 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
418 (\"hl\" tags \"+HOME+Lisa\")
419 (\"hp\" tags \"+HOME+Peter\")
420 (\"hk\" tags \"+HOME+Kim\")))"
421 :group 'org-agenda-custom-commands
422 :type `(repeat
423 (choice :value ("x" "Describe command here" tags "" nil)
424 (list :tag "Single command"
425 (string :tag "Access Key(s) ")
426 (option (string :tag "Description"))
427 (choice
428 (const :tag "Agenda" agenda)
429 (const :tag "TODO list" alltodo)
430 (const :tag "Search words" search)
431 (const :tag "Stuck projects" stuck)
432 (const :tag "Tags/Property match (all agenda files)" tags)
433 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
434 (const :tag "TODO keyword search (all agenda files)" todo)
435 (const :tag "Tags sparse tree (current buffer)" tags-tree)
436 (const :tag "TODO keyword tree (current buffer)" todo-tree)
437 (const :tag "Occur tree (current buffer)" occur-tree)
438 (sexp :tag "Other, user-defined function"))
439 (string :tag "Match (only for some commands)")
440 ,org-agenda-custom-commands-local-options
441 (option (repeat :tag "Export" (file :tag "Export to"))))
442 (list :tag "Command series, all agenda files"
443 (string :tag "Access Key(s)")
444 (string :tag "Description ")
445 (repeat :tag "Component"
446 (choice
447 (list :tag "Agenda"
448 (const :format "" agenda)
449 (const :tag "" :format "" "")
450 ,org-agenda-custom-commands-local-options)
451 (list :tag "TODO list (all keywords)"
452 (const :format "" alltodo)
453 (const :tag "" :format "" "")
454 ,org-agenda-custom-commands-local-options)
455 (list :tag "Search words"
456 (const :format "" search)
457 (string :tag "Match")
458 ,org-agenda-custom-commands-local-options)
459 (list :tag "Stuck projects"
460 (const :format "" stuck)
461 (const :tag "" :format "" "")
462 ,org-agenda-custom-commands-local-options)
463 (list :tag "Tags search"
464 (const :format "" tags)
465 (string :tag "Match")
466 ,org-agenda-custom-commands-local-options)
467 (list :tag "Tags search, TODO entries only"
468 (const :format "" tags-todo)
469 (string :tag "Match")
470 ,org-agenda-custom-commands-local-options)
471 (list :tag "TODO keyword search"
472 (const :format "" todo)
473 (string :tag "Match")
474 ,org-agenda-custom-commands-local-options)
475 (list :tag "Other, user-defined function"
476 (symbol :tag "function")
477 (string :tag "Match")
478 ,org-agenda-custom-commands-local-options)))
480 (repeat :tag "Settings for entire command set"
481 (list (variable :tag "Any variable")
482 (sexp :tag "Value")))
483 (option (repeat :tag "Export" (file :tag "Export to"))))
484 (cons :tag "Prefix key documentation"
485 (string :tag "Access Key(s)")
486 (string :tag "Description ")))))
488 (defcustom org-agenda-query-register ?o
489 "The register holding the current query string.
490 The purpose of this is that if you construct a query string interactively,
491 you can then use it to define a custom command."
492 :group 'org-agenda-custom-commands
493 :type 'character)
495 (defcustom org-stuck-projects
496 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
497 "How to identify stuck projects.
498 This is a list of four items:
499 1. A tags/todo/property matcher string that is used to identify a project.
500 See the manual for a description of tag and property searches.
501 The entire tree below a headline matched by this is considered one project.
502 2. A list of TODO keywords identifying non-stuck projects.
503 If the project subtree contains any headline with one of these todo
504 keywords, the project is considered to be not stuck. If you specify
505 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
506 3. A list of tags identifying non-stuck projects.
507 If the project subtree contains any headline with one of these tags,
508 the project is considered to be not stuck. If you specify \"*\" as
509 a tag, any tag will mark the project unstuck. Note that this is about
510 the explicit presence of a tag somewhere in the subtree, inherited
511 tags to not count here. If inherited tags make a project not stuck,
512 use \"-TAG\" in the tags part of the matcher under (1.) above.
513 4. An arbitrary regular expression matching non-stuck projects.
515 If the project turns out to be not stuck, search continues also in the
516 subtree to see if any of the subtasks have project status.
518 See also the variable `org-tags-match-list-sublevels' which applies
519 to projects matched by this search as well.
521 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
522 or `C-c a #' to produce the list."
523 :group 'org-agenda-custom-commands
524 :type '(list
525 (string :tag "Tags/TODO match to identify a project")
526 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
527 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
528 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
530 (defcustom org-agenda-filter-effort-default-operator "<"
531 "The default operator for effort estimate filtering.
532 If you select an effort estimate limit without first pressing an operator,
533 this one will be used."
534 :group 'org-agenda-custom-commands
535 :type '(choice (const :tag "less or equal" "<")
536 (const :tag "greater or equal"">")
537 (const :tag "equal" "=")))
539 (defgroup org-agenda-skip nil
540 "Options concerning skipping parts of agenda files."
541 :tag "Org Agenda Skip"
542 :group 'org-agenda)
544 (defcustom org-agenda-skip-function-global nil
545 "Function to be called at each match during agenda construction.
546 If this function returns nil, the current match should not be skipped.
547 If the function decided to skip an agenda match, is must return the
548 buffer position from which the search should be continued.
549 This may also be a Lisp form, which will be evaluated.
551 This variable will be applied to every agenda match, including
552 tags/property searches and TODO lists. So try to make the test function
553 do its checking as efficiently as possible. To implement a skipping
554 condition just for specific agenda commands, use the variable
555 `org-agenda-skip-function' which can be set in the options section
556 of custom agenda commands."
557 :group 'org-agenda-skip
558 :type 'sexp)
560 (defgroup org-agenda-daily/weekly nil
561 "Options concerning the daily/weekly agenda."
562 :tag "Org Agenda Daily/Weekly"
563 :group 'org-agenda)
564 (defgroup org-agenda-todo-list nil
565 "Options concerning the global todo list agenda view."
566 :tag "Org Agenda Todo List"
567 :group 'org-agenda)
568 (defgroup org-agenda-match-view nil
569 "Options concerning the general tags/property/todo match agenda view."
570 :tag "Org Agenda Match View"
571 :group 'org-agenda)
572 (defgroup org-agenda-search-view nil
573 "Options concerning the general tags/property/todo match agenda view."
574 :tag "Org Agenda Match View"
575 :group 'org-agenda)
577 (defvar org-agenda-archives-mode nil
578 "Non-nil means the agenda will include archived items.
579 If this is the symbol `trees', trees in the selected agenda scope
580 that are marked with the ARCHIVE tag will be included anyway. When this is
581 t, also all archive files associated with the current selection of agenda
582 files will be included.")
584 (defcustom org-agenda-skip-comment-trees t
585 "Non-nil means skip trees that start with the COMMENT keyword.
586 When nil, these trees are also scanned by agenda commands."
587 :group 'org-agenda-skip
588 :type 'boolean)
590 (defcustom org-agenda-todo-list-sublevels t
591 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
592 When nil, the sublevels of a TODO entry are not checked, resulting in
593 potentially much shorter TODO lists."
594 :group 'org-agenda-skip
595 :group 'org-agenda-todo-list
596 :type 'boolean)
598 (defcustom org-agenda-todo-ignore-with-date nil
599 "Non-nil means don't show entries with a date in the global todo list.
600 You can use this if you prefer to mark mere appointments with a TODO keyword,
601 but don't want them to show up in the TODO list.
602 When this is set, it also covers deadlines and scheduled items, the settings
603 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
604 will be ignored.
605 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
606 :group 'org-agenda-skip
607 :group 'org-agenda-todo-list
608 :type 'boolean)
610 (defcustom org-agenda-todo-ignore-timestamp nil
611 "Non-nil means don't show entries with a timestamp.
612 This applies when creating the global todo list.
613 Valid values are:
615 past Don't show entries for today or in the past.
617 future Don't show entries with a timestamp in the future.
618 The idea behind this is that if it has a future
619 timestamp, you don't want to think about it until the
620 date.
622 all Don't show any entries with a timestamp in the global todo list.
623 The idea behind this is that by setting a timestamp, you
624 have already \"taken care\" of this item.
626 This variable can also have an integer as a value. If positive (N),
627 todos with a timestamp N or more days in the future will be ignored. If
628 negative (-N), todos with a timestamp N or more days in the past will be
629 ignored. If 0, todos with a timestamp either today or in the future will
630 be ignored. For example, a value of -1 will exclude todos with a
631 timestamp in the past (yesterday or earlier), while a value of 7 will
632 exclude todos with a timestamp a week or more in the future.
634 See also `org-agenda-todo-ignore-with-date'.
635 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
636 to make his option also apply to the tags-todo list."
637 :group 'org-agenda-skip
638 :group 'org-agenda-todo-list
639 :type '(choice
640 (const :tag "Ignore future timestamp todos" future)
641 (const :tag "Ignore past or present timestamp todos" past)
642 (const :tag "Ignore all timestamp todos" all)
643 (const :tag "Show timestamp todos" nil)
644 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
646 (defcustom org-agenda-todo-ignore-scheduled nil
647 "Non-nil means, ignore some scheduled TODO items when making TODO list.
648 This applies when creating the global todo list.
649 Valid values are:
651 past Don't show entries scheduled today or in the past.
653 future Don't show entries scheduled in the future.
654 The idea behind this is that by scheduling it, you don't want to
655 think about it until the scheduled date.
657 all Don't show any scheduled entries in the global todo list.
658 The idea behind this is that by scheduling it, you have already
659 \"taken care\" of this item.
661 t Same as `all', for backward compatibility.
663 This variable can also have an integer as a value. See
664 `org-agenda-todo-ignore-timestamp' for more details.
666 See also `org-agenda-todo-ignore-with-date'.
667 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
668 to make his option also apply to the tags-todo list."
669 :group 'org-agenda-skip
670 :group 'org-agenda-todo-list
671 :type '(choice
672 (const :tag "Ignore future-scheduled todos" future)
673 (const :tag "Ignore past- or present-scheduled todos" past)
674 (const :tag "Ignore all scheduled todos" all)
675 (const :tag "Ignore all scheduled todos (compatibility)" t)
676 (const :tag "Show scheduled todos" nil)
677 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
679 (defcustom org-agenda-todo-ignore-deadlines nil
680 "Non-nil means ignore some deadlined TODO items when making TODO list.
681 There are different motivations for using different values, please think
682 carefully when configuring this variable.
684 This applies when creating the global todo list.
685 Valid values are:
687 near Don't show near deadline entries. A deadline is near when it is
688 closer than `org-deadline-warning-days' days. The idea behind this
689 is that such items will appear in the agenda anyway.
691 far Don't show TODO entries where a deadline has been defined, but
692 the deadline is not near. This is useful if you don't want to
693 use the todo list to figure out what to do now.
695 past Don't show entries with a deadline timestamp for today or in the past.
697 future Don't show entries with a deadline timestamp in the future, not even
698 when they become `near' ones. Use it with caution.
700 all Ignore all TODO entries that do have a deadline.
702 t Same as `near', for backward compatibility.
704 This variable can also have an integer as a value. See
705 `org-agenda-todo-ignore-timestamp' for more details.
707 See also `org-agenda-todo-ignore-with-date'.
708 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
709 to make his option also apply to the tags-todo list."
710 :group 'org-agenda-skip
711 :group 'org-agenda-todo-list
712 :type '(choice
713 (const :tag "Ignore near deadlines" near)
714 (const :tag "Ignore near deadlines (compatibility)" t)
715 (const :tag "Ignore far deadlines" far)
716 (const :tag "Ignore all TODOs with a deadlines" all)
717 (const :tag "Show all TODOs, even if they have a deadline" nil)
718 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
720 (defcustom org-agenda-tags-todo-honor-ignore-options nil
721 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
722 The variables
723 `org-agenda-todo-ignore-with-date',
724 `org-agenda-todo-ignore-timestamp',
725 `org-agenda-todo-ignore-scheduled',
726 `org-agenda-todo-ignore-deadlines'
727 make the global TODO list skip entries that have time stamps of certain
728 kinds. If this option is set, the same options will also apply for the
729 tags-todo search, which is the general tags/property matcher
730 restricted to unfinished TODO entries only."
731 :group 'org-agenda-skip
732 :group 'org-agenda-todo-list
733 :group 'org-agenda-match-view
734 :type 'boolean)
736 (defcustom org-agenda-skip-scheduled-if-done nil
737 "Non-nil means don't show scheduled items in agenda when they are done.
738 This is relevant for the daily/weekly agenda, not for the TODO list. And
739 it applies only to the actual date of the scheduling. Warnings about
740 an item with a past scheduling dates are always turned off when the item
741 is DONE."
742 :group 'org-agenda-skip
743 :group 'org-agenda-daily/weekly
744 :type 'boolean)
746 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
747 "Non-nil means skip scheduling line if same entry shows because of deadline.
748 In the agenda of today, an entry can show up multiple times because
749 it is both scheduled and has a nearby deadline, and maybe a plain time
750 stamp as well.
751 When this variable is t, then only the deadline is shown and the fact that
752 the entry is scheduled today or was scheduled previously is not shown.
753 When this variable is nil, the entry will be shown several times. When
754 the variable is the symbol `not-today', then skip scheduled previously,
755 but not scheduled today."
756 :group 'org-agenda-skip
757 :group 'org-agenda-daily/weekly
758 :type '(choice
759 (const :tag "Never" nil)
760 (const :tag "Always" t)
761 (const :tag "Not when scheduled today" not-today)))
763 (defcustom org-agenda-skip-deadline-if-done nil
764 "Non-nil means don't show deadlines when the corresponding item is done.
765 When nil, the deadline is still shown and should give you a happy feeling.
766 This is relevant for the daily/weekly agenda. And it applied only to the
767 actually date of the deadline. Warnings about approaching and past-due
768 deadlines are always turned off when the item is DONE."
769 :group 'org-agenda-skip
770 :group 'org-agenda-daily/weekly
771 :type 'boolean)
773 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
774 "Non-nil means skip deadline prewarning when entry is also scheduled.
775 This will apply on all days where a prewarning for the deadline would
776 be shown, but not at the day when the entry is actually due. On that day,
777 the deadline will be shown anyway.
778 This variable may be set to nil, t, or a number which will then give
779 the number of days before the actual deadline when the prewarnings
780 should resume.
781 This can be used in a workflow where the first showing of the deadline will
782 trigger you to schedule it, and then you don't want to be reminded of it
783 because you will take care of it on the day when scheduled."
784 :group 'org-agenda-skip
785 :group 'org-agenda-daily/weekly
786 :type '(choice
787 (const :tag "Alwas show prewarning" nil)
788 (const :tag "Remove prewarning if entry is scheduled" t)
789 (integer :tag "Restart prewarning N days before deadline")))
791 (defcustom org-agenda-skip-additional-timestamps-same-entry t
792 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
793 When non-nil, after the search for timestamps has matched once in an
794 entry, the rest of the entry will not be searched."
795 :group 'org-agenda-skip
796 :type 'boolean)
798 (defcustom org-agenda-skip-timestamp-if-done nil
799 "Non-nil means don't select item by timestamp or -range if it is DONE."
800 :group 'org-agenda-skip
801 :group 'org-agenda-daily/weekly
802 :type 'boolean)
804 (defcustom org-agenda-dim-blocked-tasks t
805 "Non-nil means dim blocked tasks in the agenda display.
806 This causes some overhead during agenda construction, but if you
807 have turned on `org-enforce-todo-dependencies',
808 `org-enforce-todo-checkbox-dependencies', or any other blocking
809 mechanism, this will create useful feedback in the agenda.
811 Instead of t, this variable can also have the value `invisible'.
812 Then blocked tasks will be invisible and only become visible when
813 they become unblocked. An exemption to this behavior is when a task is
814 blocked because of unchecked checkboxes below it. Since checkboxes do
815 not show up in the agenda views, making this task invisible you remove any
816 trace from agenda views that there is something to do. Therefore, a task
817 that is blocked because of checkboxes will never be made invisible, it
818 will only be dimmed."
819 :group 'org-agenda-daily/weekly
820 :group 'org-agenda-todo-list
821 :type '(choice
822 (const :tag "Do not dim" nil)
823 (const :tag "Dim to a grey face" t)
824 (const :tag "Make invisible" invisible)))
826 (defcustom org-timeline-show-empty-dates 3
827 "Non-nil means `org-timeline' also shows dates without an entry.
828 When nil, only the days which actually have entries are shown.
829 When t, all days between the first and the last date are shown.
830 When an integer, show also empty dates, but if there is a gap of more than
831 N days, just insert a special line indicating the size of the gap."
832 :group 'org-agenda-skip
833 :type '(choice
834 (const :tag "None" nil)
835 (const :tag "All" t)
836 (integer :tag "at most")))
838 (defgroup org-agenda-startup nil
839 "Options concerning initial settings in the Agenda in Org Mode."
840 :tag "Org Agenda Startup"
841 :group 'org-agenda)
843 (defcustom org-agenda-menu-show-matcher t
844 "Non-nil means show the match string in the agenda dispatcher menu.
845 When nil, the matcher string is not shown, but is put into the help-echo
846 property so than moving the mouse over the command shows it.
847 Setting it to nil is good if matcher strings are very long and/or if
848 you want to use two-column display (see `org-agenda-menu-two-column')."
849 :group 'org-agenda
850 :type 'boolean)
852 (defcustom org-agenda-menu-two-column nil
853 "Non-nil means, use two columns to show custom commands in the dispatcher.
854 If you use this, you probably want to set `org-agenda-menu-show-matcher'
855 to nil."
856 :group 'org-agenda
857 :type 'boolean)
859 (defcustom org-finalize-agenda-hook nil
860 "Hook run just before displaying an agenda buffer."
861 :group 'org-agenda-startup
862 :type 'hook)
864 (defcustom org-agenda-mouse-1-follows-link nil
865 "Non-nil means mouse-1 on a link will follow the link in the agenda.
866 A longer mouse click will still set point. Does not work on XEmacs.
867 Needs to be set before org.el is loaded."
868 :group 'org-agenda-startup
869 :type 'boolean)
871 (defcustom org-agenda-start-with-follow-mode nil
872 "The initial value of follow mode in a newly created agenda window."
873 :group 'org-agenda-startup
874 :type 'boolean)
876 (defcustom org-agenda-show-outline-path t
877 "Non-nil means show outline path in echo area after line motion."
878 :group 'org-agenda-startup
879 :type 'boolean)
881 (defcustom org-agenda-start-with-entry-text-mode nil
882 "The initial value of entry-text-mode in a newly created agenda window."
883 :group 'org-agenda-startup
884 :type 'boolean)
886 (defcustom org-agenda-entry-text-maxlines 5
887 "Number of text lines to be added when `E' is pressed in the agenda.
889 Note that this variable only used during agenda display. Add add entry text
890 when exporting the agenda, configure the variable
891 `org-agenda-add-entry-ext-maxlines'."
892 :group 'org-agenda
893 :type 'integer)
895 (defcustom org-agenda-entry-text-exclude-regexps nil
896 "List of regular expressions to clean up entry text.
897 The complete matches of all regular expressions in this list will be
898 removed from entry text before it is shown in the agenda."
899 :group 'org-agenda
900 :type '(repeat (regexp)))
902 (defvar org-agenda-entry-text-cleanup-hook nil
903 "Hook that is run after basic cleanup of entry text to be shown in agenda.
904 This cleanup is done in a temporary buffer, so the function may inspect and
905 change the entire buffer.
906 Some default stuff like drawers and scheduling/deadline dates will already
907 have been removed when this is called, as will any matches for regular
908 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
910 (defvar org-agenda-include-inactive-timestamps nil
911 "Non-nil means include inactive time stamps in agenda and timeline.")
913 (defgroup org-agenda-windows nil
914 "Options concerning the windows used by the Agenda in Org Mode."
915 :tag "Org Agenda Windows"
916 :group 'org-agenda)
918 (defcustom org-agenda-window-setup 'reorganize-frame
919 "How the agenda buffer should be displayed.
920 Possible values for this option are:
922 current-window Show agenda in the current window, keeping all other windows.
923 other-window Use `switch-to-buffer-other-window' to display agenda.
924 reorganize-frame Show only two windows on the current frame, the current
925 window and the agenda.
926 other-frame Use `switch-to-buffer-other-frame' to display agenda.
927 Also, when exiting the agenda, kill that frame.
928 See also the variable `org-agenda-restore-windows-after-quit'."
929 :group 'org-agenda-windows
930 :type '(choice
931 (const current-window)
932 (const other-frame)
933 (const other-window)
934 (const reorganize-frame)))
936 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
937 "The min and max height of the agenda window as a fraction of frame height.
938 The value of the variable is a cons cell with two numbers between 0 and 1.
939 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
940 :group 'org-agenda-windows
941 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
943 (defcustom org-agenda-restore-windows-after-quit nil
944 "Non-nil means restore window configuration upon exiting agenda.
945 Before the window configuration is changed for displaying the agenda,
946 the current status is recorded. When the agenda is exited with
947 `q' or `x' and this option is set, the old state is restored. If
948 `org-agenda-window-setup' is `other-frame', the value of this
949 option will be ignored."
950 :group 'org-agenda-windows
951 :type 'boolean)
953 (defcustom org-agenda-ndays nil
954 "Number of days to include in overview display.
955 Should be 1 or 7.
956 Obsolete, see `org-agenda-span'."
957 :group 'org-agenda-daily/weekly
958 :type 'integer)
960 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
962 (defcustom org-agenda-span 'week
963 "Number of days to include in overview display.
964 Can be day, week, month, year, or any number of days.
965 Custom commands can set this variable in the options section."
966 :group 'org-agenda-daily/weekly
967 :type '(choice (const :tag "Day" day)
968 (const :tag "Week" week)
969 (const :tag "Month" month)
970 (const :tag "Year" year)
971 (integer :tag "Custom")))
973 (defcustom org-agenda-start-on-weekday 1
974 "Non-nil means start the overview always on the specified weekday.
975 0 denotes Sunday, 1 denotes Monday etc.
976 When nil, always start on the current day.
977 Custom commands can set this variable in the options section."
978 :group 'org-agenda-daily/weekly
979 :type '(choice (const :tag "Today" nil)
980 (integer :tag "Weekday No.")))
982 (defcustom org-agenda-show-all-dates t
983 "Non-nil means `org-agenda' shows every day in the selected range.
984 When nil, only the days which actually have entries are shown."
985 :group 'org-agenda-daily/weekly
986 :type 'boolean)
988 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
989 "Format string for displaying dates in the agenda.
990 Used by the daily/weekly agenda and by the timeline. This should be
991 a format string understood by `format-time-string', or a function returning
992 the formatted date as a string. The function must take a single argument,
993 a calendar-style date list like (month day year)."
994 :group 'org-agenda-daily/weekly
995 :type '(choice
996 (string :tag "Format string")
997 (function :tag "Function")))
999 (defun org-agenda-format-date-aligned (date)
1000 "Format a date string for display in the daily/weekly agenda, or timeline.
1001 This function makes sure that dates are aligned for easy reading."
1002 (require 'cal-iso)
1003 (let* ((dayname (calendar-day-name date))
1004 (day (cadr date))
1005 (day-of-week (calendar-day-of-week date))
1006 (month (car date))
1007 (monthname (calendar-month-name month))
1008 (year (nth 2 date))
1009 (iso-week (org-days-to-iso-week
1010 (calendar-absolute-from-gregorian date)))
1011 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1012 (1- year))
1013 ((and (= month 12) (<= iso-week 1))
1014 (1+ year))
1015 (t year)))
1016 (weekstring (if (= day-of-week 1)
1017 (format " W%02d" iso-week)
1018 "")))
1019 (format "%-10s %2d %s %4d%s"
1020 dayname day monthname year weekstring)))
1022 (defcustom org-agenda-time-leading-zero nil
1023 "Non-nil means use leading zero for military times in agenda.
1024 For example, 9:30am would become 09:30 rather than 9:30."
1025 :group 'org-agenda-daily/weekly
1026 :type 'boolean)
1028 (defcustom org-agenda-timegrid-use-ampm nil
1029 "When set, show AM/PM style timestamps on the timegrid."
1030 :group 'org-agenda
1031 :type 'boolean)
1033 (defun org-agenda-time-of-day-to-ampm (time)
1034 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1035 (let* ((hour-number (string-to-number (substring time 0 -3)))
1036 (minute (substring time -2))
1037 (ampm "am"))
1038 (cond
1039 ((equal hour-number 12)
1040 (setq ampm "pm"))
1041 ((> hour-number 12)
1042 (setq ampm "pm")
1043 (setq hour-number (- hour-number 12))))
1044 (concat
1045 (if org-agenda-time-leading-zero
1046 (format "%02d" hour-number)
1047 (format "%02s" (number-to-string hour-number)))
1048 ":" minute ampm)))
1050 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1051 "Conditionally convert TIME to AM/PM format
1052 based on `org-agenda-timegrid-use-ampm'"
1053 (if org-agenda-timegrid-use-ampm
1054 (org-agenda-time-of-day-to-ampm time)
1055 time))
1057 (defcustom org-agenda-weekend-days '(6 0)
1058 "Which days are weekend?
1059 These days get the special face `org-agenda-date-weekend' in the agenda
1060 and timeline buffers."
1061 :group 'org-agenda-daily/weekly
1062 :type '(set :greedy t
1063 (const :tag "Monday" 1)
1064 (const :tag "Tuesday" 2)
1065 (const :tag "Wednesday" 3)
1066 (const :tag "Thursday" 4)
1067 (const :tag "Friday" 5)
1068 (const :tag "Saturday" 6)
1069 (const :tag "Sunday" 0)))
1071 (defcustom org-agenda-include-diary nil
1072 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1073 Custom commands can set this variable in the options section."
1074 :group 'org-agenda-daily/weekly
1075 :type 'boolean)
1077 (defcustom org-agenda-include-deadlines t
1078 "If non-nil, include entries within their deadline warning period.
1079 Custom commands can set this variable in the options section."
1080 :group 'org-agenda-daily/weekly
1081 :type 'boolean)
1083 (defcustom org-agenda-include-all-todo nil
1084 "Set means weekly/daily agenda will always contain all TODO entries.
1085 The TODO entries will be listed at the top of the agenda, before
1086 the entries for specific days.
1087 This option is deprecated, it is better to define a block agenda instead."
1088 :group 'org-agenda-daily/weekly
1089 :type 'boolean)
1091 (defcustom org-agenda-repeating-timestamp-show-all t
1092 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1093 When set to a list of strings, only show occurrences of repeating
1094 stamps for these TODO keywords. When nil, only one occurrence is
1095 shown, either today or the nearest into the future."
1096 :group 'org-agenda-daily/weekly
1097 :type '(choice
1098 (const :tag "Show repeating stamps" t)
1099 (repeat :tag "Show repeating stamps for these TODO keywords"
1100 (string :tag "TODO Keyword"))
1101 (const :tag "Don't show repeating stamps" nil)))
1103 (defcustom org-scheduled-past-days 10000
1104 "No. of days to continue listing scheduled items that are not marked DONE.
1105 When an item is scheduled on a date, it shows up in the agenda on this
1106 day and will be listed until it is marked done for the number of days
1107 given here."
1108 :group 'org-agenda-daily/weekly
1109 :type 'integer)
1111 (defcustom org-agenda-log-mode-items '(closed clock)
1112 "List of items that should be shown in agenda log mode.
1113 This list may contain the following symbols:
1115 closed Show entries that have been closed on that day.
1116 clock Show entries that have received clocked time on that day.
1117 state Show all logged state changes.
1118 Note that instead of changing this variable, you can also press `C-u l' in
1119 the agenda to display all available LOG items temporarily."
1120 :group 'org-agenda-daily/weekly
1121 :type '(set :greedy t (const closed) (const clock) (const state)))
1123 (defcustom org-agenda-clock-consistency-checks
1124 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1125 :gap-ok-around ("4:00")
1126 :default-face ((:background "DarkRed") (:foreground "white"))
1127 :overlap-face nil :gap-face nil :no-end-time-face nil
1128 :long-face nil :short-face nil)
1129 "This is a property list, with the following keys:
1131 :max-duration Mark clocking chunks that are longer than this time.
1132 This is a time string like \"HH:MM\", or the number
1133 of minutes as an integer.
1135 :min-duration Mark clocking chunks that are shorter that this.
1136 This is a time string like \"HH:MM\", or the number
1137 of minutes as an integer.
1139 :max-gap Mark gaps between clocking chunks that are longer than
1140 this duration. A number of minutes, or a string
1141 like \"HH:MM\".
1143 :gap-ok-around List of times during the day which are usually not working
1144 times. When a gap is detected, but the gap contains any
1145 of these times, the gap is *not* reported. For example,
1146 if this is (\"4:00\" \"13:00\") then gaps that contain
1147 4:00 in the morning (i.e. the night) and 13:00
1148 (i.e. a typical lunch time) do not cause a warning.
1149 You should have at least one time during the night in this
1150 list, or otherwise the first task each morning will trigger
1151 a warning because it follows a long gap.
1153 Furthermore, the following properties can be used to define faces for
1154 issue display.
1156 :default-face the default face, if the specific face is undefined
1157 :overlap-face face for overlapping clocks
1158 :gap-face face for gaps between clocks
1159 :no-end-time-face face for incomplete clocks
1160 :long-face face for clock intervals that are too long
1161 :short-face face for clock intervals that are too short"
1162 :group 'org-agenda-daily/weekly
1163 :group 'org-clock
1164 :type 'plist)
1166 (defcustom org-agenda-log-mode-add-notes t
1167 "Non-nil means add first line of notes to log entries in agenda views.
1168 If a log item like a state change or a clock entry is associated with
1169 notes, the first line of these notes will be added to the entry in the
1170 agenda display."
1171 :group 'org-agenda-daily/weekly
1172 :type 'boolean)
1174 (defcustom org-agenda-start-with-log-mode nil
1175 "The initial value of log-mode in a newly created agenda window."
1176 :group 'org-agenda-startup
1177 :group 'org-agenda-daily/weekly
1178 :type 'boolean)
1180 (defcustom org-agenda-start-with-clockreport-mode nil
1181 "The initial value of clockreport-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-clockreport-parameter-plist '(:link t :maxlevel 2)
1187 "Property list with parameters for the clocktable in clockreport mode.
1188 This is the display mode that shows a clock table in the daily/weekly
1189 agenda, the properties for this dynamic block can be set here.
1190 The usual clocktable parameters are allowed here, but you cannot set
1191 the properties :name, :tstart, :tend, :block, and :scope - these will
1192 be overwritten to make sure the content accurately reflects the
1193 current display in the agenda."
1194 :group 'org-agenda-daily/weekly
1195 :type 'plist)
1197 (defcustom org-agenda-search-view-always-boolean nil
1198 "Non-nil means the search string is interpreted as individual parts.
1200 The search string for search view can either be interpreted as a phrase,
1201 or as a list of snippets that define a boolean search for a number of
1202 strings.
1204 When this is non-nil, the string will be split on whitespace, and each
1205 snippet will be searched individually, and all must match in order to
1206 select an entry. A snippet is then a single string of non-white
1207 characters, or a string in double quotes, or a regexp in {} braces.
1208 If a snippet is preceded by \"-\", the snippet must *not* match.
1209 \"+\" is syntactic sugar for positive selection. Each snippet may
1210 be found as a full word or a partial word, but see the variable
1211 `org-agenda-search-view-force-full-words'.
1213 When this is nil, search will look for the entire search phrase as one,
1214 with each space character matching any amount of whitespace, including
1215 line breaks.
1217 Even when this is nil, you can still switch to Boolean search dynamically
1218 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1219 is a regexp marked with braces like \"{abc}\", this will also switch to
1220 boolean search."
1221 :group 'org-agenda-search-view
1222 :type 'boolean)
1224 (if (fboundp 'defvaralias)
1225 (defvaralias 'org-agenda-search-view-search-words-only
1226 'org-agenda-search-view-always-boolean))
1228 (defcustom org-agenda-search-view-force-full-words nil
1229 "Non-nil means, search words must be matches as complete words.
1230 When nil, they may also match part of a word."
1231 :group 'org-agenda-search-view
1232 :type 'boolean)
1234 (defgroup org-agenda-time-grid nil
1235 "Options concerning the time grid in the Org-mode Agenda."
1236 :tag "Org Agenda Time Grid"
1237 :group 'org-agenda)
1239 (defcustom org-agenda-search-headline-for-time t
1240 "Non-nil means search headline for a time-of-day.
1241 If the headline contains a time-of-day in one format or another, it will
1242 be used to sort the entry into the time sequence of items for a day.
1243 Some people have time stamps in the headline that refer to the creation
1244 time or so, and then this produces an unwanted side effect. If this is
1245 the case for your, use this variable to turn off searching the headline
1246 for a time."
1247 :group 'org-agenda-time-grid
1248 :type 'boolean)
1250 (defcustom org-agenda-use-time-grid t
1251 "Non-nil means show a time grid in the agenda schedule.
1252 A time grid is a set of lines for specific times (like every two hours between
1253 8:00 and 20:00). The items scheduled for a day at specific times are
1254 sorted in between these lines.
1255 For details about when the grid will be shown, and what it will look like, see
1256 the variable `org-agenda-time-grid'."
1257 :group 'org-agenda-time-grid
1258 :type 'boolean)
1260 (defcustom org-agenda-time-grid
1261 '((daily today require-timed)
1262 "----------------"
1263 (800 1000 1200 1400 1600 1800 2000))
1265 "The settings for time grid for agenda display.
1266 This is a list of three items. The first item is again a list. It contains
1267 symbols specifying conditions when the grid should be displayed:
1269 daily if the agenda shows a single day
1270 weekly if the agenda shows an entire week
1271 today show grid on current date, independent of daily/weekly display
1272 require-timed show grid only if at least one item has a time specification
1274 The second item is a string which will be placed behind the grid time.
1276 The third item is a list of integers, indicating the times that should have
1277 a grid line."
1278 :group 'org-agenda-time-grid
1279 :type
1280 '(list
1281 (set :greedy t :tag "Grid Display Options"
1282 (const :tag "Show grid in single day agenda display" daily)
1283 (const :tag "Show grid in weekly agenda display" weekly)
1284 (const :tag "Always show grid for today" today)
1285 (const :tag "Show grid only if any timed entries are present"
1286 require-timed)
1287 (const :tag "Skip grid times already present in an entry"
1288 remove-match))
1289 (string :tag "Grid String")
1290 (repeat :tag "Grid Times" (integer :tag "Time"))))
1292 (defcustom org-agenda-show-current-time-in-grid t
1293 "Non-nil means show the current time in the time grid."
1294 :group 'org-agenda-time-grid
1295 :type 'boolean)
1297 (defcustom org-agenda-current-time-string
1298 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1299 "The string for the current time marker in the agenda."
1300 :group 'org-agenda-time-grid
1301 :type 'string)
1303 (defgroup org-agenda-sorting nil
1304 "Options concerning sorting in the Org-mode Agenda."
1305 :tag "Org Agenda Sorting"
1306 :group 'org-agenda)
1308 (defcustom org-agenda-sorting-strategy
1309 '((agenda habit-down time-up priority-down category-keep)
1310 (todo priority-down category-keep)
1311 (tags priority-down category-keep)
1312 (search category-keep))
1313 "Sorting structure for the agenda items of a single day.
1314 This is a list of symbols which will be used in sequence to determine
1315 if an entry should be listed before another entry. The following
1316 symbols are recognized:
1318 time-up Put entries with time-of-day indications first, early first
1319 time-down Put entries with time-of-day indications first, late first
1320 category-keep Keep the default order of categories, corresponding to the
1321 sequence in `org-agenda-files'.
1322 category-up Sort alphabetically by category, A-Z.
1323 category-down Sort alphabetically by category, Z-A.
1324 tag-up Sort alphabetically by last tag, A-Z.
1325 tag-down Sort alphabetically by last tag, Z-A.
1326 priority-up Sort numerically by priority, high priority last.
1327 priority-down Sort numerically by priority, high priority first.
1328 todo-state-up Sort by todo state, tasks that are done last.
1329 todo-state-down Sort by todo state, tasks that are done first.
1330 effort-up Sort numerically by estimated effort, high effort last.
1331 effort-down Sort numerically by estimated effort, high effort first.
1332 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1333 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1334 habit-up Put entries that are habits first
1335 habit-down Put entries that are habits last
1336 alpha-up Sort headlines alphabetically
1337 alpha-down Sort headlines alphabetically, reversed
1339 The different possibilities will be tried in sequence, and testing stops
1340 if one comparison returns a \"not-equal\". For example, the default
1341 '(time-up category-keep priority-down)
1342 means: Pull out all entries having a specified time of day and sort them,
1343 in order to make a time schedule for the current day the first thing in the
1344 agenda listing for the day. Of the entries without a time indication, keep
1345 the grouped in categories, don't sort the categories, but keep them in
1346 the sequence given in `org-agenda-files'. Within each category sort by
1347 priority.
1349 Leaving out `category-keep' would mean that items will be sorted across
1350 categories by priority.
1352 Instead of a single list, this can also be a set of list for specific
1353 contents, with a context symbol in the car of the list, any of
1354 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1356 Custom commands can bind this variable in the options section."
1357 :group 'org-agenda-sorting
1358 :type `(choice
1359 (repeat :tag "General" ,org-sorting-choice)
1360 (list :tag "Individually"
1361 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1362 (repeat ,org-sorting-choice))
1363 (cons (const :tag "Strategy for TODO lists" todo)
1364 (repeat ,org-sorting-choice))
1365 (cons (const :tag "Strategy for Tags matches" tags)
1366 (repeat ,org-sorting-choice))
1367 (cons (const :tag "Strategy for search matches" search)
1368 (repeat ,org-sorting-choice)))))
1370 (defcustom org-agenda-cmp-user-defined nil
1371 "A function to define the comparison `user-defined'.
1372 This function must receive two arguments, agenda entry a and b.
1373 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1374 the user comparison, return nil.
1375 When this is defined, you can make `user-defined-up' and `user-defined-down'
1376 part of an agenda sorting strategy."
1377 :group 'org-agenda-sorting
1378 :type 'symbol)
1380 (defcustom org-sort-agenda-notime-is-late t
1381 "Non-nil means items without time are considered late.
1382 This is only relevant for sorting. When t, items which have no explicit
1383 time like 15:30 will be considered as 99:01, i.e. later than any items which
1384 do have a time. When nil, the default time is before 0:00. You can use this
1385 option to decide if the schedule for today should come before or after timeless
1386 agenda entries."
1387 :group 'org-agenda-sorting
1388 :type 'boolean)
1390 (defcustom org-sort-agenda-noeffort-is-high t
1391 "Non-nil means items without effort estimate are sorted as high effort.
1392 This also applies when filtering an agenda view with respect to the
1393 < or > effort operator. Then, tasks with no effort defined will be treated
1394 as tasks with high effort.
1395 When nil, such items are sorted as 0 minutes effort."
1396 :group 'org-agenda-sorting
1397 :type 'boolean)
1399 (defgroup org-agenda-line-format nil
1400 "Options concerning the entry prefix in the Org-mode agenda display."
1401 :tag "Org Agenda Line Format"
1402 :group 'org-agenda)
1404 (defcustom org-agenda-prefix-format
1405 '((agenda . " %i %-12:c%?-12t% s")
1406 (timeline . " % s")
1407 (todo . " %i %-12:c")
1408 (tags . " %i %-12:c")
1409 (search . " %i %-12:c"))
1410 "Format specifications for the prefix of items in the agenda views.
1411 An alist with five entries, each for the different agenda types. The
1412 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1413 The values are format strings.
1415 This format works similar to a printf format, with the following meaning:
1417 %c the category of the item, \"Diary\" for entries from the diary,
1418 or as given by the CATEGORY keyword or derived from the file name
1419 %i the icon category of the item, see `org-agenda-category-icon-alist'
1420 %T the last tag of the item (ignore inherited tags, which come first)
1421 %t the HH:MM time-of-day specification if one applies to the entry
1422 %s Scheduling/Deadline information, a short string
1423 %(expression) Eval EXPRESSION and replace the control string
1424 by the result
1426 All specifiers work basically like the standard `%s' of printf, but may
1427 contain two additional characters: a question mark just after the `%'
1428 and a whitespace/punctuation character just before the final letter.
1430 If the first character after `%' is a question mark, the entire field
1431 will only be included if the corresponding value applies to the current
1432 entry. This is useful for fields which should have fixed width when
1433 present, but zero width when absent. For example, \"%?-12t\" will
1434 result in a 12 character time field if a time of the day is specified,
1435 but will completely disappear in entries which do not contain a time.
1437 If there is punctuation or whitespace character just before the final
1438 format letter, this character will be appended to the field value if
1439 the value is not empty. For example, the format \"%-12:c\" leads to
1440 \"Diary: \" if the category is \"Diary\". If the category were be
1441 empty, no additional colon would be inserted.
1443 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1444 which means:
1446 - Indent the line with two space characters
1447 - Give the category a 12 chars wide field, padded with whitespace on
1448 the right (because of `-'). Append a colon if there is a category
1449 (because of `:').
1450 - If there is a time-of-day, put it into a 12 chars wide field. If no
1451 time, don't put in an empty field, just skip it (because of '?').
1452 - Finally, put the scheduling information.
1454 See also the variables `org-agenda-remove-times-when-in-prefix' and
1455 `org-agenda-remove-tags'.
1457 Custom commands can set this variable in the options section."
1458 :type '(choice
1459 (string :tag "General format")
1460 (list :greedy t :tag "View dependent"
1461 (cons (const agenda) (string :tag "Format"))
1462 (cons (const timeline) (string :tag "Format"))
1463 (cons (const todo) (string :tag "Format"))
1464 (cons (const tags) (string :tag "Format"))
1465 (cons (const search) (string :tag "Format"))))
1466 :group 'org-agenda-line-format)
1468 (defvar org-prefix-format-compiled nil
1469 "The compiled version of the most recently used prefix format.
1470 See the variable `org-agenda-prefix-format'.")
1472 (defcustom org-agenda-todo-keyword-format "%-1s"
1473 "Format for the TODO keyword in agenda lines.
1474 Set this to something like \"%-12s\" if you want all TODO keywords
1475 to occupy a fixed space in the agenda display."
1476 :group 'org-agenda-line-format
1477 :type 'string)
1479 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1480 "Text preceding timerange entries in the agenda view.
1481 This is a list with two strings. The first applies when the range
1482 is entirely on one day. The second applies if the range spans several days.
1483 The strings may have two \"%d\" format specifiers which will be filled
1484 with the sequence number of the days, and the total number of days in the
1485 range, respectively."
1486 :group 'org-agenda-line-format
1487 :type '(list
1488 (string :tag "Deadline today ")
1489 (choice :tag "Deadline relative"
1490 (string :tag "Format string")
1491 (function))))
1493 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1494 "Text preceding scheduled items in the agenda view.
1495 This is a list with two strings. The first applies when the item is
1496 scheduled on the current day. The second applies when it has been scheduled
1497 previously, it may contain a %d indicating that this is the nth time that
1498 this item is scheduled, due to automatic rescheduling of unfinished items
1499 for the following day. So this number is one larger than the number of days
1500 that passed since this item was scheduled first."
1501 :group 'org-agenda-line-format
1502 :type '(list
1503 (string :tag "Scheduled today ")
1504 (string :tag "Scheduled previously")))
1506 (defcustom org-agenda-inactive-leader "["
1507 "Text preceding item pulled into the agenda by inactive time stamps.
1508 These entries are added to the agenda when pressing \"[\"."
1509 :group 'org-agenda-line-format
1510 :type '(list
1511 (string :tag "Scheduled today ")
1512 (string :tag "Scheduled previously")))
1514 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1515 "Text preceding deadline items in the agenda view.
1516 This is a list with two strings. The first applies when the item has its
1517 deadline on the current day. The second applies when it is in the past or
1518 in the future, it may contain %d to capture how many days away the deadline
1519 is (was)."
1520 :group 'org-agenda-line-format
1521 :type '(list
1522 (string :tag "Deadline today ")
1523 (choice :tag "Deadline relative"
1524 (string :tag "Format string")
1525 (function))))
1527 (defcustom org-agenda-remove-times-when-in-prefix t
1528 "Non-nil means remove duplicate time specifications in agenda items.
1529 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1530 time-of-day specification in a headline or diary entry is extracted and
1531 placed into the prefix. If this option is non-nil, the original specification
1532 \(a timestamp or -range, or just a plain time(range) specification like
1533 11:30-4pm) will be removed for agenda display. This makes the agenda less
1534 cluttered.
1535 The option can be t or nil. It may also be the symbol `beg', indicating
1536 that the time should only be removed when it is located at the beginning of
1537 the headline/diary entry."
1538 :group 'org-agenda-line-format
1539 :type '(choice
1540 (const :tag "Always" t)
1541 (const :tag "Never" nil)
1542 (const :tag "When at beginning of entry" beg)))
1544 (defcustom org-agenda-remove-timeranges-from-blocks nil
1545 "Non-nil means remove time ranges specifications in agenda
1546 items that span on several days."
1547 :group 'org-agenda-line-format
1548 :type 'boolean)
1550 (defcustom org-agenda-default-appointment-duration nil
1551 "Default duration for appointments that only have a starting time.
1552 When nil, no duration is specified in such cases.
1553 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1554 :group 'org-agenda-line-format
1555 :type '(choice
1556 (integer :tag "Minutes")
1557 (const :tag "No default duration")))
1559 (defcustom org-agenda-show-inherited-tags t
1560 "Non-nil means show inherited tags in each agenda line."
1561 :group 'org-agenda-line-format
1562 :type 'boolean)
1564 (defcustom org-agenda-hide-tags-regexp nil
1565 "Regular expression used to filter away specific tags in agenda views.
1566 This means that these tags will be present, but not be shown in the agenda
1567 line. Secondary filtering will still work on the hidden tags.
1568 Nil means don't hide any tags."
1569 :group 'org-agenda-line-format
1570 :type '(choice
1571 (const :tag "Hide none" nil)
1572 (string :tag "Regexp ")))
1574 (defcustom org-agenda-remove-tags nil
1575 "Non-nil means remove the tags from the headline copy in the agenda.
1576 When this is the symbol `prefix', only remove tags when
1577 `org-agenda-prefix-format' contains a `%T' specifier."
1578 :group 'org-agenda-line-format
1579 :type '(choice
1580 (const :tag "Always" t)
1581 (const :tag "Never" nil)
1582 (const :tag "When prefix format contains %T" prefix)))
1584 (if (fboundp 'defvaralias)
1585 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1586 'org-agenda-remove-tags))
1588 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1589 "Shift tags in agenda items to this column.
1590 If this number is positive, it specifies the column. If it is negative,
1591 it means that the tags should be flushright to that column. For example,
1592 -80 works well for a normal 80 character screen."
1593 :group 'org-agenda-line-format
1594 :type 'integer)
1596 (if (fboundp 'defvaralias)
1597 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1599 (defcustom org-agenda-fontify-priorities 'cookies
1600 "Non-nil means highlight low and high priorities in agenda.
1601 When t, the highest priority entries are bold, lowest priority italic.
1602 However, settings in `org-priority-faces' will overrule these faces.
1603 When this variable is the symbol `cookies', only fontify the
1604 cookies, not the entire task.
1605 This may also be an association list of priority faces, whose
1606 keys are the character values of `org-highest-priority',
1607 `org-default-priority', and `org-lowest-priority' (the default values
1608 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1609 color as a string, or a list like `(:background \"Red\")'.
1610 If it is a color, the variable `org-faces-easy-properties'
1611 determines if it is a foreground or a background color."
1612 :group 'org-agenda-line-format
1613 :type '(choice
1614 (const :tag "Never" nil)
1615 (const :tag "Defaults" t)
1616 (const :tag "Cookies only" cookies)
1617 (repeat :tag "Specify"
1618 (list (character :tag "Priority" :value ?A)
1619 (choice :tag "Face "
1620 (string :tag "Color")
1621 (sexp :tag "Face"))))))
1623 (defcustom org-agenda-day-face-function nil
1624 "Function called to determine what face should be used to display a day.
1625 The only argument passed to that function is the day. It should
1626 returns a face, or nil if does not want to specify a face and let
1627 the normal rules apply."
1628 :group 'org-agenda-line-format
1629 :type 'function)
1631 (defcustom org-agenda-category-icon-alist nil
1632 "Alist of category icon to be displayed in agenda views.
1634 Each entry should have the following format:
1636 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1638 Where CATEGORY-REGEXP is a regexp matching the categories where
1639 the icon should be displayed.
1640 FILE-OR-DATA either a file path or a string containing image data.
1642 The other fields can be omited safely if not needed:
1643 TYPE indicates the image type.
1644 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1645 image data.
1646 PROPS are additional image attributes to assign to the image,
1647 like, e.g. `:ascent center'.
1649 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1651 If you want to set the display properties yourself, just put a
1652 list as second element:
1654 (CATEGORY-REGEXP (MY PROPERTY LIST))
1656 For example, to display a 16px horizontal space for Emacs
1657 category, you can use:
1659 (\"Emacs\" '(space . (:width (16))))"
1660 :group 'org-agenda-line-format
1661 :type '(alist :key-type (string :tag "Regexp matching category")
1662 :value-type (choice (list :tag "Icon"
1663 (string :tag "File or data")
1664 (symbol :tag "Type")
1665 (boolean :tag "Data?")
1666 (repeat :tag "Extra image properties" :inline t symbol))
1667 (list :tag "Display properties" sexp))))
1669 (defgroup org-agenda-column-view nil
1670 "Options concerning column view in the agenda."
1671 :tag "Org Agenda Column View"
1672 :group 'org-agenda)
1674 (defcustom org-agenda-columns-show-summaries t
1675 "Non-nil means show summaries for columns displayed in the agenda view."
1676 :group 'org-agenda-column-view
1677 :type 'boolean)
1679 (defcustom org-agenda-columns-compute-summary-properties t
1680 "Non-nil means recompute all summary properties before column view.
1681 When column view in the agenda is listing properties that have a summary
1682 operator, it can go to all relevant buffers and recompute the summaries
1683 there. This can mean overhead for the agenda column view, but is necessary
1684 to have thing up to date.
1685 As a special case, a CLOCKSUM property also makes sure that the clock
1686 computations are current."
1687 :group 'org-agenda-column-view
1688 :type 'boolean)
1690 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1691 "Non-nil means the duration of an appointment will add to day effort.
1692 The property to which appointment durations will be added is the one given
1693 in the option `org-effort-property'. If an appointment does not have
1694 an end time, `org-agenda-default-appointment-duration' will be used. If that
1695 is not set, an appointment without end time will not contribute to the time
1696 estimate."
1697 :group 'org-agenda-column-view
1698 :type 'boolean)
1700 (defcustom org-agenda-auto-exclude-function nil
1701 "A function called with a tag to decide if it is filtered on '/ RET'.
1702 The sole argument to the function, which is called once for each
1703 possible tag, is a string giving the name of the tag. The
1704 function should return either nil if the tag should be included
1705 as normal, or \"-<TAG>\" to exclude the tag.
1706 Note that for the purpose of tag filtering, only the lower-case version of
1707 all tags will be considered, so that this function will only ever see
1708 the lower-case version of all tags."
1709 :group 'org-agenda
1710 :type 'function)
1712 (defcustom org-agenda-bulk-custom-functions nil
1713 "Alist of characters and custom functions for bulk actions.
1714 For example, this value makes those two functions available:
1716 '((?R set-category)
1717 (?C bulk-cut))
1719 With selected entries in an agenda buffer, `B R' will call
1720 the custom function `set-category' on the selected entries.
1721 Note that functions in this alist don't need to be quoted."
1722 :type 'alist
1723 :group 'org-agenda)
1725 (eval-when-compile
1726 (require 'cl))
1727 (require 'org)
1729 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1730 "Execute BODY with point at location given by `org-hd-marker' property.
1731 If STRING is non-nil, the text property will be fetched from position 0
1732 in that string. If STRING is nil, it will be fetched from the beginning
1733 of the current line."
1734 (org-with-gensyms (marker)
1735 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1736 'org-hd-marker string)))
1737 (with-current-buffer (marker-buffer ,marker)
1738 (save-excursion
1739 (goto-char ,marker)
1740 ,@body)))))
1741 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1743 (defun org-add-agenda-custom-command (entry)
1744 "Replace or add a command in `org-agenda-custom-commands'.
1745 This is mostly for hacking and trying a new command - once the command
1746 works you probably want to add it to `org-agenda-custom-commands' for good."
1747 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1748 (if ass
1749 (setcdr ass (cdr entry))
1750 (push entry org-agenda-custom-commands))))
1752 ;;; Define the Org-agenda-mode
1754 (defvar org-agenda-mode-map (make-sparse-keymap)
1755 "Keymap for `org-agenda-mode'.")
1756 (if (fboundp 'defvaralias)
1757 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1759 (defvar org-agenda-menu) ; defined later in this file.
1760 (defvar org-agenda-restrict) ; defined later in this file.
1761 (defvar org-agenda-follow-mode nil)
1762 (defvar org-agenda-entry-text-mode nil)
1763 (defvar org-agenda-clockreport-mode nil)
1764 (defvar org-agenda-show-log nil)
1765 (defvar org-agenda-redo-command nil)
1766 (defvar org-agenda-query-string nil)
1767 (defvar org-agenda-mode-hook nil
1768 "Hook for `org-agenda-mode', run after the mode is turned on.")
1769 (defvar org-agenda-type nil)
1770 (defvar org-agenda-force-single-file nil)
1771 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1773 (defun org-agenda-mode ()
1774 "Mode for time-sorted view on action items in Org-mode files.
1776 The following commands are available:
1778 \\{org-agenda-mode-map}"
1779 (interactive)
1780 (kill-all-local-variables)
1781 (setq org-agenda-undo-list nil
1782 org-agenda-pending-undo-list nil
1783 org-agenda-bulk-marked-entries nil)
1784 (setq major-mode 'org-agenda-mode)
1785 ;; Keep global-font-lock-mode from turning on font-lock-mode
1786 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1787 (setq mode-name "Org-Agenda")
1788 (use-local-map org-agenda-mode-map)
1789 (easy-menu-add org-agenda-menu)
1790 (if org-startup-truncated (setq truncate-lines t))
1791 (org-set-local 'line-move-visual nil)
1792 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1793 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1794 ;; Make sure properties are removed when copying text
1795 (when (boundp 'buffer-substring-filters)
1796 (org-set-local 'buffer-substring-filters
1797 (cons (lambda (x)
1798 (set-text-properties 0 (length x) nil x) x)
1799 buffer-substring-filters)))
1800 (unless org-agenda-keep-modes
1801 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1802 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1803 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1804 org-agenda-show-log org-agenda-start-with-log-mode))
1806 (easy-menu-change
1807 '("Agenda") "Agenda Files"
1808 (append
1809 (list
1810 (vector
1811 (if (get 'org-agenda-files 'org-restrict)
1812 "Restricted to single file"
1813 "Edit File List")
1814 '(org-edit-agenda-file-list)
1815 (not (get 'org-agenda-files 'org-restrict)))
1816 "--")
1817 (mapcar 'org-file-menu-entry (org-agenda-files))))
1818 (org-agenda-set-mode-name)
1819 (apply
1820 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1821 (list 'org-agenda-mode-hook)))
1823 (substitute-key-definition 'undo 'org-agenda-undo
1824 org-agenda-mode-map global-map)
1825 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1826 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1827 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1828 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1829 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1830 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1831 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1832 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1833 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1834 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1835 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1836 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1837 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1838 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1839 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1840 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1841 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1842 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1843 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1844 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1845 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1846 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1847 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1848 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1849 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1850 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1851 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1852 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1853 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1854 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1855 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1856 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1857 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1858 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1859 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1860 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1861 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1862 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1863 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1864 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1865 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1866 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1867 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1868 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1869 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1871 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1872 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1873 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1874 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1875 (while l (org-defkey org-agenda-mode-map
1876 (int-to-string (pop l)) 'digit-argument)))
1878 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1879 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1880 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1881 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1882 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1883 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1884 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1885 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1886 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1887 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1888 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1889 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1890 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1891 'org-clock-modify-effort-estimate)
1892 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1893 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1894 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1895 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1896 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1897 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1898 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1899 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1900 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1901 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1902 (substitute-key-definition 'next-line 'org-agenda-next-line
1903 org-agenda-mode-map global-map)
1904 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1905 org-agenda-mode-map global-map)
1906 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1907 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1908 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1909 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1910 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1911 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1912 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1913 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1914 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1915 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1916 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1917 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1918 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1919 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1920 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1921 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1922 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1923 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1924 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1925 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1926 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1927 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1928 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1929 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1930 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1931 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1932 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1933 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1934 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1935 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1937 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1938 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1939 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1940 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1941 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1942 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1943 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1944 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1945 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1946 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1948 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1949 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1950 (when org-agenda-mouse-1-follows-link
1951 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1952 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1953 '("Agenda"
1954 ("Agenda Files")
1955 "--"
1956 ("Agenda Dates"
1957 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1958 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1959 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1960 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1961 "--"
1962 ("View"
1963 ["Day View" org-agenda-day-view
1964 :active (org-agenda-check-type nil 'agenda)
1965 :style radio :selected (eq org-agenda-current-span 'day)
1966 :keys "v d (or just d)"]
1967 ["Week View" org-agenda-week-view
1968 :active (org-agenda-check-type nil 'agenda)
1969 :style radio :selected (eq org-agenda-current-span 'week)
1970 :keys "v w (or just w)"]
1971 ["Month View" org-agenda-month-view
1972 :active (org-agenda-check-type nil 'agenda)
1973 :style radio :selected (eq org-agenda-current-span 'month)
1974 :keys "v m"]
1975 ["Year View" org-agenda-year-view
1976 :active (org-agenda-check-type nil 'agenda)
1977 :style radio :selected (eq org-agenda-current-span 'year)
1978 :keys "v y"]
1979 "--"
1980 ["Include Diary" org-agenda-toggle-diary
1981 :style toggle :selected org-agenda-include-diary
1982 :active (org-agenda-check-type nil 'agenda)]
1983 ["Include Deadlines" org-agenda-toggle-deadlines
1984 :style toggle :selected org-agenda-include-deadlines
1985 :active (org-agenda-check-type nil 'agenda)]
1986 ["Use Time Grid" org-agenda-toggle-time-grid
1987 :style toggle :selected org-agenda-use-time-grid
1988 :active (org-agenda-check-type nil 'agenda)]
1989 "--"
1990 ["Show clock report" org-agenda-clockreport-mode
1991 :style toggle :selected org-agenda-clockreport-mode
1992 :active (org-agenda-check-type nil 'agenda)]
1993 ["Show some entry text" org-agenda-entry-text-mode
1994 :style toggle :selected org-agenda-entry-text-mode
1995 :active t]
1996 "--"
1997 ["Show Logbook entries" org-agenda-log-mode
1998 :style toggle :selected org-agenda-show-log
1999 :active (org-agenda-check-type nil 'agenda 'timeline)
2000 :keys "v l (or just l)"]
2001 ["Include archived trees" org-agenda-archives-mode
2002 :style toggle :selected org-agenda-archives-mode :active t
2003 :keys "v a"]
2004 ["Include archive files" (org-agenda-archives-mode t)
2005 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2006 :keys "v A"]
2007 "--"
2008 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2009 ["Write view to file" org-write-agenda t]
2010 ["Rebuild buffer" org-agenda-redo t]
2011 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2012 "--"
2013 ["Show original entry" org-agenda-show t]
2014 ["Go To (other window)" org-agenda-goto t]
2015 ["Go To (this window)" org-agenda-switch-to t]
2016 ["Follow Mode" org-agenda-follow-mode
2017 :style toggle :selected org-agenda-follow-mode :active t]
2018 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2019 "--"
2020 ("TODO"
2021 ["Cycle TODO" org-agenda-todo t]
2022 ["Next TODO set" org-agenda-todo-nextset t]
2023 ["Previous TODO set" org-agenda-todo-previousset t]
2024 ["Add note" org-agenda-add-note t])
2025 ("Archive/Refile/Delete"
2026 ["Archive default" org-agenda-archive-default t]
2027 ["Archive default" org-agenda-archive-default-with-confirmation t]
2028 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2029 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2030 ["Archive subtree" org-agenda-archive t]
2031 "--"
2032 ["Refile" org-agenda-refile t]
2033 "--"
2034 ["Delete subtree" org-agenda-kill t])
2035 ("Bulk action"
2036 ["Mark entry" org-agenda-bulk-mark t]
2037 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2038 ["Unmark entry" org-agenda-bulk-unmark t]
2039 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2040 ["Act on all marked" org-agenda-bulk-action t]
2041 "--"
2042 ("Tags and Properties"
2043 ["Show all Tags" org-agenda-show-tags t]
2044 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2045 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2046 "--"
2047 ["Column View" org-columns t])
2048 ("Deadline/Schedule"
2049 ["Schedule" org-agenda-schedule t]
2050 ["Set Deadline" org-agenda-deadline t]
2051 "--"
2052 ["Mark item" org-agenda-action :active t :keys "k m"]
2053 ["Show mark item" org-agenda-action :active t :keys "k v"]
2054 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2055 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2056 "--"
2057 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2058 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2059 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2060 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2061 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2062 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2063 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2064 ("Clock and Effort"
2065 ["Clock in" org-agenda-clock-in t]
2066 ["Clock out" org-agenda-clock-out t]
2067 ["Clock cancel" org-agenda-clock-cancel t]
2068 ["Goto running clock" org-clock-goto t]
2069 "--"
2070 ["Set Effort" org-agenda-set-effort t]
2071 ["Change clocked effort" org-clock-modify-effort-estimate
2072 (org-clock-is-active)])
2073 ("Priority"
2074 ["Set Priority" org-agenda-priority t]
2075 ["Increase Priority" org-agenda-priority-up t]
2076 ["Decrease Priority" org-agenda-priority-down t]
2077 ["Show Priority" org-agenda-show-priority t])
2078 ("Calendar/Diary"
2079 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2080 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2081 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2082 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2083 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2084 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2085 "--"
2086 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2087 "--"
2088 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2089 "--"
2090 ("MobileOrg"
2091 ["Push Files and Views" org-mobile-push t]
2092 ["Get Captured and Flagged" org-mobile-pull t]
2093 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2094 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2095 "--"
2096 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2097 "--"
2098 ["Quit" org-agenda-quit t]
2099 ["Exit and Release Buffers" org-agenda-exit t]
2102 ;;; Agenda undo
2104 (defvar org-agenda-allow-remote-undo t
2105 "Non-nil means allow remote undo from the agenda buffer.")
2106 (defvar org-agenda-undo-list nil
2107 "List of undoable operations in the agenda since last refresh.")
2108 (defvar org-agenda-undo-has-started-in nil
2109 "Buffers that have already seen `undo-start' in the current undo sequence.")
2110 (defvar org-agenda-pending-undo-list nil
2111 "In a series of undo commands, this is the list of remaining undo items.")
2113 (defun org-agenda-undo ()
2114 "Undo a remote editing step in the agenda.
2115 This undoes changes both in the agenda buffer and in the remote buffer
2116 that have been changed along."
2117 (interactive)
2118 (or org-agenda-allow-remote-undo
2119 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2120 (if (not (eq this-command last-command))
2121 (setq org-agenda-undo-has-started-in nil
2122 org-agenda-pending-undo-list org-agenda-undo-list))
2123 (if (not org-agenda-pending-undo-list)
2124 (error "No further undo information"))
2125 (let* ((entry (pop org-agenda-pending-undo-list))
2126 buf line cmd rembuf)
2127 (setq cmd (pop entry) line (pop entry))
2128 (setq rembuf (nth 2 entry))
2129 (org-with-remote-undo rembuf
2130 (while (bufferp (setq buf (pop entry)))
2131 (if (pop entry)
2132 (with-current-buffer buf
2133 (let ((last-undo-buffer buf)
2134 (inhibit-read-only t))
2135 (unless (memq buf org-agenda-undo-has-started-in)
2136 (push buf org-agenda-undo-has-started-in)
2137 (make-local-variable 'pending-undo-list)
2138 (undo-start))
2139 (while (and pending-undo-list
2140 (listp pending-undo-list)
2141 (not (car pending-undo-list)))
2142 (pop pending-undo-list))
2143 (undo-more 1))))))
2144 (org-goto-line line)
2145 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2147 (defun org-verify-change-for-undo (l1 l2)
2148 "Verify that a real change occurred between the undo lists L1 and L2."
2149 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2150 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2151 (not (eq l1 l2)))
2153 ;;; Agenda dispatch
2155 (defvar org-agenda-restrict nil)
2156 (defvar org-agenda-restrict-begin (make-marker))
2157 (defvar org-agenda-restrict-end (make-marker))
2158 (defvar org-agenda-last-dispatch-buffer nil)
2159 (defvar org-agenda-overriding-restriction nil)
2161 ;;;###autoload
2162 (defun org-agenda (&optional arg keys restriction)
2163 "Dispatch agenda commands to collect entries to the agenda buffer.
2164 Prompts for a command to execute. Any prefix arg will be passed
2165 on to the selected command. The default selections are:
2167 a Call `org-agenda-list' to display the agenda for current day or week.
2168 t Call `org-todo-list' to display the global todo list.
2169 T Call `org-todo-list' to display the global todo list, select only
2170 entries with a specific TODO keyword (the user gets a prompt).
2171 m Call `org-tags-view' to display headlines with tags matching
2172 a condition (the user is prompted for the condition).
2173 M Like `m', but select only TODO entries, no ordinary headlines.
2174 L Create a timeline for the current buffer.
2175 e Export views to associated files.
2176 s Search entries for keywords.
2177 / Multi occur across all agenda files and also files listed
2178 in `org-agenda-text-search-extra-files'.
2179 < Restrict agenda commands to buffer, subtree, or region.
2180 Press several times to get the desired effect.
2181 > Remove a previous restriction.
2182 # List \"stuck\" projects.
2183 ! Configure what \"stuck\" means.
2184 C Configure custom agenda commands.
2186 More commands can be added by configuring the variable
2187 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2188 searches can be pre-defined in this way.
2190 If the current buffer is in Org-mode and visiting a file, you can also
2191 first press `<' once to indicate that the agenda should be temporarily
2192 \(until the next use of \\[org-agenda]) restricted to the current file.
2193 Pressing `<' twice means to restrict to the current subtree or region
2194 \(if active)."
2195 (interactive "P")
2196 (catch 'exit
2197 (let* ((prefix-descriptions nil)
2198 (org-agenda-window-setup (if (equal (buffer-name)
2199 org-agenda-buffer-name)
2200 'current-window
2201 org-agenda-window-setup))
2202 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2203 (org-agenda-custom-commands
2204 ;; normalize different versions
2205 (delq nil
2206 (mapcar
2207 (lambda (x)
2208 (cond ((stringp (cdr x))
2209 (push x prefix-descriptions)
2210 nil)
2211 ((stringp (nth 1 x)) x)
2212 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2213 (t (cons (car x) (cons "" (cdr x))))))
2214 org-agenda-custom-commands)))
2215 (buf (current-buffer))
2216 (bfn (buffer-file-name (buffer-base-buffer)))
2217 entry key type match lprops ans)
2218 ;; Turn off restriction unless there is an overriding one,
2219 (unless org-agenda-overriding-restriction
2220 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2221 ;; There is a request to keep the file list in place
2222 (put 'org-agenda-files 'org-restrict nil))
2223 (setq org-agenda-restrict nil)
2224 (move-marker org-agenda-restrict-begin nil)
2225 (move-marker org-agenda-restrict-end nil))
2226 ;; Delete old local properties
2227 (put 'org-agenda-redo-command 'org-lprops nil)
2228 ;; Delete previously set last-arguments
2229 (put 'org-agenda-redo-command 'last-args nil)
2230 ;; Remember where this call originated
2231 (setq org-agenda-last-dispatch-buffer (current-buffer))
2232 (unless keys
2233 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2234 keys (car ans)
2235 restriction (cdr ans)))
2236 ;; Establish the restriction, if any
2237 (when (and (not org-agenda-overriding-restriction) restriction)
2238 (put 'org-agenda-files 'org-restrict (list bfn))
2239 (cond
2240 ((eq restriction 'region)
2241 (setq org-agenda-restrict t)
2242 (move-marker org-agenda-restrict-begin (region-beginning))
2243 (move-marker org-agenda-restrict-end (region-end)))
2244 ((eq restriction 'subtree)
2245 (save-excursion
2246 (setq org-agenda-restrict t)
2247 (org-back-to-heading t)
2248 (move-marker org-agenda-restrict-begin (point))
2249 (move-marker org-agenda-restrict-end
2250 (progn (org-end-of-subtree t)))))))
2252 ;; For example the todo list should not need it (but does...)
2253 (cond
2254 ((setq entry (assoc keys org-agenda-custom-commands))
2255 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2256 (progn
2257 (setq type (nth 2 entry) match (eval (nth 3 entry))
2258 lprops (nth 4 entry))
2259 (put 'org-agenda-redo-command 'org-lprops lprops)
2260 (cond
2261 ((eq type 'agenda)
2262 (org-let lprops '(org-agenda-list current-prefix-arg)))
2263 ((eq type 'alltodo)
2264 (org-let lprops '(org-todo-list current-prefix-arg)))
2265 ((eq type 'search)
2266 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2267 ((eq type 'stuck)
2268 (org-let lprops '(org-agenda-list-stuck-projects
2269 current-prefix-arg)))
2270 ((eq type 'tags)
2271 (org-let lprops '(org-tags-view current-prefix-arg match)))
2272 ((eq type 'tags-todo)
2273 (org-let lprops '(org-tags-view '(4) match)))
2274 ((eq type 'todo)
2275 (org-let lprops '(org-todo-list match)))
2276 ((eq type 'tags-tree)
2277 (org-check-for-org-mode)
2278 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2279 ((eq type 'todo-tree)
2280 (org-check-for-org-mode)
2281 (org-let lprops
2282 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2283 (regexp-quote match) "\\>"))))
2284 ((eq type 'occur-tree)
2285 (org-check-for-org-mode)
2286 (org-let lprops '(org-occur match)))
2287 ((functionp type)
2288 (org-let lprops '(funcall type match)))
2289 ((fboundp type)
2290 (org-let lprops '(funcall type match)))
2291 (t (error "Invalid custom agenda command type %s" type))))
2292 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2293 ((equal keys "C")
2294 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2295 (customize-variable 'org-agenda-custom-commands))
2296 ((equal keys "a") (call-interactively 'org-agenda-list))
2297 ((equal keys "s") (call-interactively 'org-search-view))
2298 ((equal keys "t") (call-interactively 'org-todo-list))
2299 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2300 ((equal keys "m") (call-interactively 'org-tags-view))
2301 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2302 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2303 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2304 (org-add-hook
2305 'post-command-hook
2306 (lambda ()
2307 (unless (current-message)
2308 (let* ((m (org-agenda-get-any-marker))
2309 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2310 (when note
2311 (message (concat
2312 "FLAGGING-NOTE ([?] for more info): "
2313 (org-add-props
2314 (replace-regexp-in-string
2315 "\\\\n" "//"
2316 (copy-sequence note))
2317 nil 'face 'org-warning)))))))
2318 t t))
2319 ((equal keys "L")
2320 (unless (org-mode-p)
2321 (error "This is not an Org-mode file"))
2322 (unless restriction
2323 (put 'org-agenda-files 'org-restrict (list bfn))
2324 (org-call-with-arg 'org-timeline arg)))
2325 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2326 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2327 ((equal keys "!") (customize-variable 'org-stuck-projects))
2328 (t (error "Invalid agenda key"))))))
2330 (defun org-agenda-append-agenda ()
2331 "Append another agenda view to the current one.
2332 This function allows interactive building of block agendas.
2333 Agenda views are separated by `org-agenda-block-separator'."
2334 (interactive)
2335 (unless (string= (buffer-name) org-agenda-buffer-name)
2336 (error "Can only append from within agenda buffer"))
2337 (let ((org-agenda-multi t))
2338 (org-agenda)
2339 (widen)))
2341 (defun org-agenda-normalize-custom-commands (cmds)
2342 (delq nil
2343 (mapcar
2344 (lambda (x)
2345 (cond ((stringp (cdr x)) nil)
2346 ((stringp (nth 1 x)) x)
2347 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2348 (t (cons (car x) (cons "" (cdr x))))))
2349 cmds)))
2351 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2352 "The user interface for selecting an agenda command."
2353 (catch 'exit
2354 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2355 (restrict-ok (and bfn (org-mode-p)))
2356 (region-p (org-region-active-p))
2357 (custom org-agenda-custom-commands)
2358 (selstring "")
2359 restriction second-time
2360 c entry key type match prefixes rmheader header-end custom1 desc
2361 line lines left right n n1)
2362 (save-window-excursion
2363 (delete-other-windows)
2364 (org-switch-to-buffer-other-window " *Agenda Commands*")
2365 (erase-buffer)
2366 (insert (eval-when-compile
2367 (let ((header
2369 Press key for an agenda command: < Buffer, subtree/region restriction
2370 -------------------------------- > Remove restriction
2371 a Agenda for current week or day e Export agenda views
2372 t List of all TODO entries T Entries with special TODO kwd
2373 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2374 L Timeline for current buffer # List stuck projects (!=configure)
2375 s Search for keywords C Configure custom agenda commands
2376 / Multi-occur ? Find :FLAGGED: entries
2378 (start 0))
2379 (while (string-match
2380 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2381 header start)
2382 (setq start (match-end 0))
2383 (add-text-properties (match-beginning 2) (match-end 2)
2384 '(face bold) header))
2385 header)))
2386 (setq header-end (move-marker (make-marker) (point)))
2387 (while t
2388 (setq custom1 custom)
2389 (when (eq rmheader t)
2390 (org-goto-line 1)
2391 (re-search-forward ":" nil t)
2392 (delete-region (match-end 0) (point-at-eol))
2393 (forward-char 1)
2394 (looking-at "-+")
2395 (delete-region (match-end 0) (point-at-eol))
2396 (move-marker header-end (match-end 0)))
2397 (goto-char header-end)
2398 (delete-region (point) (point-max))
2400 ;; Produce all the lines that describe custom commands and prefixes
2401 (setq lines nil)
2402 (while (setq entry (pop custom1))
2403 (setq key (car entry) desc (nth 1 entry)
2404 type (nth 2 entry)
2405 match (nth 3 entry))
2406 (if (> (length key) 1)
2407 (add-to-list 'prefixes (string-to-char key))
2408 (setq line
2409 (format
2410 "%-4s%-14s"
2411 (org-add-props (copy-sequence key)
2412 '(face bold))
2413 (cond
2414 ((string-match "\\S-" desc) desc)
2415 ((eq type 'agenda) "Agenda for current week or day")
2416 ((eq type 'alltodo) "List of all TODO entries")
2417 ((eq type 'search) "Word search")
2418 ((eq type 'stuck) "List of stuck projects")
2419 ((eq type 'todo) "TODO keyword")
2420 ((eq type 'tags) "Tags query")
2421 ((eq type 'tags-todo) "Tags (TODO)")
2422 ((eq type 'tags-tree) "Tags tree")
2423 ((eq type 'todo-tree) "TODO kwd tree")
2424 ((eq type 'occur-tree) "Occur tree")
2425 ((functionp type) (if (symbolp type)
2426 (symbol-name type)
2427 "Lambda expression"))
2428 (t "???"))))
2429 (if org-agenda-menu-show-matcher
2430 (setq line
2431 (concat line ": "
2432 (cond
2433 ((stringp match)
2434 (setq match (copy-sequence match))
2435 (org-add-props match nil 'face 'org-warning))
2436 (match
2437 (format "set of %d commands" (length match)))
2438 (t ""))))
2439 (if (org-string-nw-p match)
2440 (add-text-properties
2441 0 (length line) (list 'help-echo
2442 (concat "Matcher: "match)) line)))
2443 (push line lines)))
2444 (setq lines (nreverse lines))
2445 (when prefixes
2446 (mapc (lambda (x)
2447 (push
2448 (format "%s %s"
2449 (org-add-props (char-to-string x)
2450 nil 'face 'bold)
2451 (or (cdr (assoc (concat selstring
2452 (char-to-string x))
2453 prefix-descriptions))
2454 "Prefix key"))
2455 lines))
2456 prefixes))
2458 ;; Check if we should display in two columns
2459 (if org-agenda-menu-two-column
2460 (progn
2461 (setq n (length lines)
2462 n1 (+ (/ n 2) (mod n 2))
2463 right (nthcdr n1 lines)
2464 left (copy-sequence lines))
2465 (setcdr (nthcdr (1- n1) left) nil))
2466 (setq left lines right nil))
2467 (while left
2468 (insert "\n" (pop left))
2469 (when right
2470 (if (< (current-column) 40)
2471 (move-to-column 40 t)
2472 (insert " "))
2473 (insert (pop right))))
2475 ;; Make the window the right size
2476 (goto-char (point-min))
2477 (if second-time
2478 (if (not (pos-visible-in-window-p (point-max)))
2479 (org-fit-window-to-buffer))
2480 (setq second-time t)
2481 (org-fit-window-to-buffer))
2483 ;; Ask for selection
2484 (message "Press key for agenda command%s:"
2485 (if (or restrict-ok org-agenda-overriding-restriction)
2486 (if org-agenda-overriding-restriction
2487 " (restriction lock active)"
2488 (if restriction
2489 (format " (restricted to %s)" restriction)
2490 " (unrestricted)"))
2491 ""))
2492 (setq c (read-char-exclusive))
2493 (message "")
2494 (cond
2495 ((assoc (char-to-string c) custom)
2496 (setq selstring (concat selstring (char-to-string c)))
2497 (throw 'exit (cons selstring restriction)))
2498 ((memq c prefixes)
2499 (setq selstring (concat selstring (char-to-string c))
2500 prefixes nil
2501 rmheader (or rmheader t)
2502 custom (delq nil (mapcar
2503 (lambda (x)
2504 (if (or (= (length (car x)) 1)
2505 (/= (string-to-char (car x)) c))
2507 (cons (substring (car x) 1) (cdr x))))
2508 custom))))
2509 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2510 (message "Restriction is only possible in Org-mode buffers")
2511 (ding) (sit-for 1))
2512 ((eq c ?1)
2513 (org-agenda-remove-restriction-lock 'noupdate)
2514 (setq restriction 'buffer))
2515 ((eq c ?0)
2516 (org-agenda-remove-restriction-lock 'noupdate)
2517 (setq restriction (if region-p 'region 'subtree)))
2518 ((eq c ?<)
2519 (org-agenda-remove-restriction-lock 'noupdate)
2520 (setq restriction
2521 (cond
2522 ((eq restriction 'buffer)
2523 (if region-p 'region 'subtree))
2524 ((memq restriction '(subtree region))
2525 nil)
2526 (t 'buffer))))
2527 ((eq c ?>)
2528 (org-agenda-remove-restriction-lock 'noupdate)
2529 (setq restriction nil))
2530 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2531 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2532 ((and (> (length selstring) 0) (eq c ?\d))
2533 (delete-window)
2534 (org-agenda-get-restriction-and-command prefix-descriptions))
2536 ((equal c ?q) (error "Abort"))
2537 (t (error "Invalid key %c" c))))))))
2539 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2540 (defvar org-agenda-last-arguments nil
2541 "The arguments of the previous call to `org-agenda'.")
2542 (defun org-agenda-run-series (name series)
2543 (org-let (nth 1 series) '(org-prepare-agenda name))
2544 (let* ((org-agenda-multi t)
2545 (redo (list 'org-agenda-run-series name (list 'quote series)))
2546 (org-agenda-overriding-arguments
2547 (or org-agenda-overriding-arguments
2548 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2549 (get 'org-agenda-redo-command 'last-args))))
2550 (cmds (car series))
2551 (gprops (nth 1 series))
2552 match ;; The byte compiler incorrectly complains about this. Keep it!
2553 cmd type lprops)
2554 (while (setq cmd (pop cmds))
2555 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2556 (cond
2557 ((eq type 'agenda)
2558 (org-let2 gprops lprops
2559 '(call-interactively 'org-agenda-list)))
2560 ((eq type 'alltodo)
2561 (org-let2 gprops lprops
2562 '(call-interactively 'org-todo-list)))
2563 ((eq type 'search)
2564 (org-let2 gprops lprops
2565 '(org-search-view current-prefix-arg match nil)))
2566 ((eq type 'stuck)
2567 (org-let2 gprops lprops
2568 '(call-interactively 'org-agenda-list-stuck-projects)))
2569 ((eq type 'tags)
2570 (org-let2 gprops lprops
2571 '(org-tags-view current-prefix-arg match)))
2572 ((eq type 'tags-todo)
2573 (org-let2 gprops lprops
2574 '(org-tags-view '(4) match)))
2575 ((eq type 'todo)
2576 (org-let2 gprops lprops
2577 '(org-todo-list match)))
2578 ((fboundp type)
2579 (org-let2 gprops lprops
2580 '(funcall type match)))
2581 (t (error "Invalid type in command series"))))
2582 (widen)
2583 (setq org-agenda-redo-command redo)
2584 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2585 (goto-char (point-min)))
2586 (org-fit-agenda-window)
2587 (org-let (nth 1 series) '(org-finalize-agenda)))
2589 ;;;###autoload
2590 (defmacro org-batch-agenda (cmd-key &rest parameters)
2591 "Run an agenda command in batch mode and send the result to STDOUT.
2592 If CMD-KEY is a string of length 1, it is used as a key in
2593 `org-agenda-custom-commands' and triggers this command. If it is a
2594 longer string it is used as a tags/todo match string.
2595 Parameters are alternating variable names and values that will be bound
2596 before running the agenda command."
2597 (org-eval-in-environment (org-make-parameter-alist parameters)
2598 (if (> (length cmd-key) 2)
2599 (org-tags-view nil cmd-key)
2600 (org-agenda nil cmd-key)))
2601 (set-buffer org-agenda-buffer-name)
2602 (princ (org-encode-for-stdout (buffer-string))))
2603 (def-edebug-spec org-batch-agenda (form &rest sexp))
2605 ;(defun org-encode-for-stdout (string)
2606 ; (if (fboundp 'encode-coding-string)
2607 ; (encode-coding-string string buffer-file-coding-system)
2608 ; string))
2610 (defun org-encode-for-stdout (string)
2611 string)
2613 (defvar org-agenda-info nil)
2615 ;;;###autoload
2616 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2617 "Run an agenda command in batch mode and send the result to STDOUT.
2618 If CMD-KEY is a string of length 1, it is used as a key in
2619 `org-agenda-custom-commands' and triggers this command. If it is a
2620 longer string it is used as a tags/todo match string.
2621 Parameters are alternating variable names and values that will be bound
2622 before running the agenda command.
2624 The output gives a line for each selected agenda item. Each
2625 item is a list of comma-separated values, like this:
2627 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2629 category The category of the item
2630 head The headline, without TODO kwd, TAGS and PRIORITY
2631 type The type of the agenda entry, can be
2632 todo selected in TODO match
2633 tagsmatch selected in tags match
2634 diary imported from diary
2635 deadline a deadline on given date
2636 scheduled scheduled on given date
2637 timestamp entry has timestamp on given date
2638 closed entry was closed on given date
2639 upcoming-deadline warning about deadline
2640 past-scheduled forwarded scheduled item
2641 block entry has date block including g. date
2642 todo The todo keyword, if any
2643 tags All tags including inherited ones, separated by colons
2644 date The relevant date, like 2007-2-14
2645 time The time, like 15:00-16:50
2646 extra Sting with extra planning info
2647 priority-l The priority letter if any was given
2648 priority-n The computed numerical priority
2649 agenda-day The day in the agenda where this is listed"
2650 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2651 (org-make-parameter-alist parameters))
2652 (if (> (length cmd-key) 2)
2653 (org-tags-view nil cmd-key)
2654 (org-agenda nil cmd-key)))
2655 (set-buffer org-agenda-buffer-name)
2656 (let* ((lines (org-split-string (buffer-string) "\n"))
2657 line)
2658 (while (setq line (pop lines))
2659 (catch 'next
2660 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2661 (setq org-agenda-info
2662 (org-fix-agenda-info (text-properties-at 0 line)))
2663 (princ
2664 (org-encode-for-stdout
2665 (mapconcat 'org-agenda-export-csv-mapper
2666 '(org-category txt type todo tags date time extra
2667 priority-letter priority agenda-day)
2668 ",")))
2669 (princ "\n")))))
2670 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2672 (defun org-fix-agenda-info (props)
2673 "Make sure all properties on an agenda item have a canonical form.
2674 This ensures the export commands can easily use it."
2675 (let (tmp re)
2676 (when (setq tmp (plist-get props 'tags))
2677 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2678 (when (setq tmp (plist-get props 'date))
2679 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2680 (let ((calendar-date-display-form '(year "-" month "-" day)))
2681 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2683 (setq tmp (calendar-date-string tmp)))
2684 (setq props (plist-put props 'date tmp)))
2685 (when (setq tmp (plist-get props 'day))
2686 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2687 (let ((calendar-date-display-form '(year "-" month "-" day)))
2688 (setq tmp (calendar-date-string tmp)))
2689 (setq props (plist-put props 'day tmp))
2690 (setq props (plist-put props 'agenda-day tmp)))
2691 (when (setq tmp (plist-get props 'txt))
2692 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2693 (plist-put props 'priority-letter (match-string 1 tmp))
2694 (setq tmp (replace-match "" t t tmp)))
2695 (when (and (setq re (plist-get props 'org-todo-regexp))
2696 (setq re (concat "\\`\\.*" re " ?"))
2697 (string-match re tmp))
2698 (plist-put props 'todo (match-string 1 tmp))
2699 (setq tmp (replace-match "" t t tmp)))
2700 (plist-put props 'txt tmp)))
2701 props)
2703 (defun org-agenda-export-csv-mapper (prop)
2704 (let ((res (plist-get org-agenda-info prop)))
2705 (setq res
2706 (cond
2707 ((not res) "")
2708 ((stringp res) res)
2709 (t (prin1-to-string res))))
2710 (while (string-match "," res)
2711 (setq res (replace-match ";" t t res)))
2712 (org-trim res)))
2715 ;;;###autoload
2716 (defun org-store-agenda-views (&rest parameters)
2717 (interactive)
2718 (eval (list 'org-batch-store-agenda-views)))
2720 ;;;###autoload
2721 (defmacro org-batch-store-agenda-views (&rest parameters)
2722 "Run all custom agenda commands that have a file argument."
2723 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2724 (pop-up-frames nil)
2725 (dir default-directory)
2726 (pars (org-make-parameter-alist parameters))
2727 cmd thiscmdkey files opts cmd-or-set)
2728 (save-window-excursion
2729 (while cmds
2730 (setq cmd (pop cmds)
2731 thiscmdkey (car cmd)
2732 cmd-or-set (nth 2 cmd)
2733 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2734 files (nth (if (listp cmd-or-set) 4 5) cmd))
2735 (if (stringp files) (setq files (list files)))
2736 (when files
2737 (org-eval-in-environment (append org-agenda-exporter-settings
2738 opts pars)
2739 (org-agenda nil thiscmdkey))
2740 (set-buffer org-agenda-buffer-name)
2741 (while files
2742 (org-eval-in-environment (append org-agenda-exporter-settings
2743 opts pars)
2744 (org-write-agenda (expand-file-name (pop files) dir) nil t)))
2745 (and (get-buffer org-agenda-buffer-name)
2746 (kill-buffer org-agenda-buffer-name)))))))
2747 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2749 (defun org-agenda-mark-header-line (pos)
2750 "Mark the line at POS as an agenda structure header."
2751 (save-excursion
2752 (goto-char pos)
2753 (put-text-property (point-at-bol) (point-at-eol)
2754 'org-agenda-structural-header t)
2755 (when org-agenda-title-append
2756 (put-text-property (point-at-bol) (point-at-eol)
2757 'org-agenda-title-append org-agenda-title-append))))
2759 (defvar org-mobile-creating-agendas)
2760 (defun org-write-agenda (file &optional open nosettings)
2761 "Write the current buffer (an agenda view) as a file.
2762 Depending on the extension of the file name, plain text (.txt),
2763 HTML (.html or .htm) or Postscript (.ps) is produced.
2764 If the extension is .ics, run icalendar export over all files used
2765 to construct the agenda and limit the export to entries listed in the
2766 agenda now.
2767 With prefix argument OPEN, open the new file immediately.
2768 If NOSETTINGS is given, do not scope the settings of
2769 `org-agenda-exporter-settings' into the export commands. This is used when
2770 the settings have already been scoped and we do not wish to overrule other,
2771 higher priority settings."
2772 (interactive "FWrite agenda to file: \nP")
2773 (if (not (file-writable-p file))
2774 (error "Cannot write agenda to file %s" file))
2775 (org-let (if nosettings nil org-agenda-exporter-settings)
2776 '(save-excursion
2777 (save-window-excursion
2778 (org-agenda-mark-filtered-text)
2779 (let ((bs (copy-sequence (buffer-string))) beg)
2780 (org-agenda-unmark-filtered-text)
2781 (with-temp-buffer
2782 (rename-buffer "Agenda View" t)
2783 (set-buffer-modified-p nil)
2784 (insert bs)
2785 (org-agenda-remove-marked-text 'org-filtered)
2786 (while (setq beg (text-property-any (point-min) (point-max)
2787 'org-filtered t))
2788 (delete-region
2789 beg (or (next-single-property-change beg 'org-filtered)
2790 (point-max))))
2791 (run-hooks 'org-agenda-before-write-hook)
2792 (cond
2793 ((org-bound-and-true-p org-mobile-creating-agendas)
2794 (org-mobile-write-agenda-for-mobile file))
2795 ((string-match "\\.html?\\'" file)
2796 (require 'htmlize)
2797 (set-buffer (htmlize-buffer (current-buffer)))
2799 (when (and org-agenda-export-html-style
2800 (string-match "<style>" org-agenda-export-html-style))
2801 ;; replace <style> section with org-agenda-export-html-style
2802 (goto-char (point-min))
2803 (kill-region (- (search-forward "<style") 6)
2804 (search-forward "</style>"))
2805 (insert org-agenda-export-html-style))
2806 (write-file file)
2807 (kill-buffer (current-buffer))
2808 (message "HTML written to %s" file))
2809 ((string-match "\\.ps\\'" file)
2810 (require 'ps-print)
2811 (ps-print-buffer-with-faces file)
2812 (message "Postscript written to %s" file))
2813 ((string-match "\\.pdf\\'" file)
2814 (require 'ps-print)
2815 (ps-print-buffer-with-faces
2816 (concat (file-name-sans-extension file) ".ps"))
2817 (call-process "ps2pdf" nil nil nil
2818 (expand-file-name
2819 (concat (file-name-sans-extension file) ".ps"))
2820 (expand-file-name file))
2821 (delete-file (concat (file-name-sans-extension file) ".ps"))
2822 (message "PDF written to %s" file))
2823 ((string-match "\\.ics\\'" file)
2824 (require 'org-icalendar)
2825 (let ((org-agenda-marker-table
2826 (org-create-marker-find-array
2827 (org-agenda-collect-markers)))
2828 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2829 (org-combined-agenda-icalendar-file file))
2830 (apply 'org-export-icalendar 'combine
2831 (org-agenda-files nil 'ifmode))))
2833 (let ((bs (buffer-string)))
2834 (find-file file)
2835 (erase-buffer)
2836 (insert bs)
2837 (save-buffer 0)
2838 (kill-buffer (current-buffer))
2839 (message "Plain text written to %s" file))))))))
2840 (set-buffer org-agenda-buffer-name))
2841 (when open (org-open-file file)))
2843 (defvar org-agenda-filter-overlays nil)
2845 (defun org-agenda-mark-filtered-text ()
2846 "Mark all text hidden by filtering with a text property."
2847 (let ((inhibit-read-only t))
2848 (mapc
2849 (lambda (o)
2850 (when (equal (overlay-buffer o) (current-buffer))
2851 (put-text-property
2852 (overlay-start o) (overlay-end o)
2853 'org-filtered t)))
2854 org-agenda-filter-overlays)))
2856 (defun org-agenda-unmark-filtered-text ()
2857 "Remove the filtering text property."
2858 (let ((inhibit-read-only t))
2859 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2861 (defun org-agenda-remove-marked-text (property &optional value)
2862 "Delete all text marked with VALUE of PROPERTY.
2863 VALUE defaults to t."
2864 (let (beg)
2865 (setq value (or value t))
2866 (while (setq beg (text-property-any (point-min) (point-max)
2867 property value))
2868 (delete-region
2869 beg (or (next-single-property-change beg 'org-filtered)
2870 (point-max))))))
2872 (defun org-agenda-add-entry-text ()
2873 "Add entry text to agenda lines.
2874 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2875 entry text following headings shown in the agenda.
2876 Drawers will be excluded, also the line with scheduling/deadline info."
2877 (when (and (> org-agenda-add-entry-text-maxlines 0)
2878 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2879 (let (m txt)
2880 (goto-char (point-min))
2881 (while (not (eobp))
2882 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2883 (beginning-of-line 2)
2884 (setq txt (org-agenda-get-some-entry-text
2885 m org-agenda-add-entry-text-maxlines " > "))
2886 (end-of-line 1)
2887 (if (string-match "\\S-" txt)
2888 (insert "\n" txt)
2889 (or (eobp) (forward-char 1))))))))
2891 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2892 &rest keep)
2893 "Extract entry text from MARKER, at most N-LINES lines.
2894 This will ignore drawers etc, just get the text.
2895 If INDENT is given, prefix every line with this string. If KEEP is
2896 given, it is a list of symbols, defining stuff that should not be
2897 removed from the entry content. Currently only `planning' is allowed here."
2898 (let (txt drawer-re kwd-time-re ind)
2899 (save-excursion
2900 (with-current-buffer (marker-buffer marker)
2901 (if (not (org-mode-p))
2902 (setq txt "")
2903 (save-excursion
2904 (save-restriction
2905 (widen)
2906 (goto-char marker)
2907 (end-of-line 1)
2908 (setq txt (buffer-substring
2909 (min (1+ (point)) (point-max))
2910 (progn (outline-next-heading) (point)))
2911 drawer-re org-drawer-regexp
2912 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2913 ".*\n?"))
2914 (with-temp-buffer
2915 (insert txt)
2916 (when org-agenda-add-entry-text-descriptive-links
2917 (goto-char (point-min))
2918 (while (org-activate-bracket-links (point-max))
2919 (add-text-properties (match-beginning 0) (match-end 0)
2920 '(face org-link))))
2921 (goto-char (point-min))
2922 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2923 (set-text-properties (match-beginning 0) (match-end 0)
2924 nil))
2925 (goto-char (point-min))
2926 (while (re-search-forward drawer-re nil t)
2927 (delete-region
2928 (match-beginning 0)
2929 (progn (re-search-forward
2930 "^[ \t]*:END:.*\n?" nil 'move)
2931 (point))))
2932 (unless (member 'planning keep)
2933 (goto-char (point-min))
2934 (while (re-search-forward kwd-time-re nil t)
2935 (replace-match "")))
2936 (goto-char (point-min))
2937 (when org-agenda-entry-text-exclude-regexps
2938 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2939 (while (setq re (pop re-list))
2940 (goto-char (point-min))
2941 (while (re-search-forward re nil t)
2942 (replace-match "")))))
2943 (goto-char (point-max))
2944 (skip-chars-backward " \t\n")
2945 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2947 ;; find and remove min common indentation
2948 (goto-char (point-min))
2949 (untabify (point-min) (point-max))
2950 (setq ind (org-get-indentation))
2951 (while (not (eobp))
2952 (unless (looking-at "[ \t]*$")
2953 (setq ind (min ind (org-get-indentation))))
2954 (beginning-of-line 2))
2955 (goto-char (point-min))
2956 (while (not (eobp))
2957 (unless (looking-at "[ \t]*$")
2958 (move-to-column ind)
2959 (delete-region (point-at-bol) (point)))
2960 (beginning-of-line 2))
2962 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2964 (goto-char (point-min))
2965 (when indent
2966 (while (and (not (eobp)) (re-search-forward "^" nil t))
2967 (replace-match indent t t)))
2968 (goto-char (point-min))
2969 (while (looking-at "[ \t]*\n") (replace-match ""))
2970 (goto-char (point-max))
2971 (when (> (org-current-line)
2972 n-lines)
2973 (org-goto-line (1+ n-lines))
2974 (backward-char 1))
2975 (setq txt (buffer-substring (point-min) (point)))))))))
2976 txt))
2978 (defun org-agenda-collect-markers ()
2979 "Collect the markers pointing to entries in the agenda buffer."
2980 (let (m markers)
2981 (save-excursion
2982 (goto-char (point-min))
2983 (while (not (eobp))
2984 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2985 (org-get-at-bol 'org-marker)))
2986 (push m markers))
2987 (beginning-of-line 2)))
2988 (nreverse markers)))
2990 (defun org-create-marker-find-array (marker-list)
2991 "Create a alist of files names with all marker positions in that file."
2992 (let (f tbl m a p)
2993 (while (setq m (pop marker-list))
2994 (setq p (marker-position m)
2995 f (buffer-file-name (or (buffer-base-buffer
2996 (marker-buffer m))
2997 (marker-buffer m))))
2998 (if (setq a (assoc f tbl))
2999 (push (marker-position m) (cdr a))
3000 (push (list f p) tbl)))
3001 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3002 tbl)))
3004 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3005 (defun org-check-agenda-marker-table ()
3006 "Check of the current entry is on the marker list."
3007 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3009 (and (setq a (assoc file org-agenda-marker-table))
3010 (save-match-data
3011 (save-excursion
3012 (org-back-to-heading t)
3013 (member (point) (cdr a)))))))
3015 (defun org-check-for-org-mode ()
3016 "Make sure current buffer is in org-mode. Error if not."
3017 (or (org-mode-p)
3018 (error "Cannot execute org-mode agenda command on buffer in %s"
3019 major-mode)))
3021 (defun org-fit-agenda-window ()
3022 "Fit the window to the buffer size."
3023 (and (memq org-agenda-window-setup '(reorganize-frame))
3024 (fboundp 'fit-window-to-buffer)
3025 (org-fit-window-to-buffer
3027 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3028 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3030 ;;; Agenda prepare and finalize
3032 (defvar org-agenda-multi nil) ; dynamically scoped
3033 (defvar org-agenda-buffer-name "*Org Agenda*")
3034 (defvar org-pre-agenda-window-conf nil)
3035 (defvar org-agenda-columns-active nil)
3036 (defvar org-agenda-name nil)
3037 (defvar org-agenda-filter nil)
3038 (defvar org-agenda-filter-while-redo nil)
3039 (defvar org-agenda-filter-preset nil
3040 "A preset of the tags filter used for secondary agenda filtering.
3041 This must be a list of strings, each string must be a single tag preceded
3042 by \"+\" or \"-\".
3043 This variable should not be set directly, but agenda custom commands can
3044 bind it in the options section. The preset filter is a global property of
3045 the entire agenda view. In a block agenda, it will not work reliably to
3046 define a filter for one of the individual blocks. You need to set it in
3047 the global options and expect it to be applied to the entire view.")
3049 (defun org-prepare-agenda (&optional name)
3050 (setq org-todo-keywords-for-agenda nil)
3051 (setq org-done-keywords-for-agenda nil)
3052 (setq org-drawers-for-agenda nil)
3053 (unless org-agenda-persistent-filter
3054 (setq org-agenda-filter nil))
3055 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3056 (if org-agenda-multi
3057 (progn
3058 (setq buffer-read-only nil)
3059 (goto-char (point-max))
3060 (unless (or (bobp) org-agenda-compact-blocks
3061 (not org-agenda-block-separator))
3062 (insert "\n"
3063 (if (stringp org-agenda-block-separator)
3064 org-agenda-block-separator
3065 (make-string (window-width) org-agenda-block-separator))
3066 "\n"))
3067 (narrow-to-region (point) (point-max)))
3068 (org-agenda-reset-markers)
3069 (setq org-agenda-contributing-files nil)
3070 (setq org-agenda-columns-active nil)
3071 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3072 (setq org-todo-keywords-for-agenda
3073 (org-uniquify org-todo-keywords-for-agenda))
3074 (setq org-done-keywords-for-agenda
3075 (org-uniquify org-done-keywords-for-agenda))
3076 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3077 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3078 (awin (get-buffer-window abuf)))
3079 (cond
3080 ((equal (current-buffer) abuf) nil)
3081 (awin (select-window awin))
3082 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3083 ((equal org-agenda-window-setup 'current-window)
3084 (org-pop-to-buffer-same-window abuf))
3085 ((equal org-agenda-window-setup 'other-window)
3086 (org-switch-to-buffer-other-window abuf))
3087 ((equal org-agenda-window-setup 'other-frame)
3088 (switch-to-buffer-other-frame abuf))
3089 ((equal org-agenda-window-setup 'reorganize-frame)
3090 (delete-other-windows)
3091 (org-switch-to-buffer-other-window abuf)))
3092 ;; additional test in case agenda is invoked from within agenda
3093 ;; buffer via elisp link
3094 (unless (equal (current-buffer) abuf)
3095 (org-pop-to-buffer-same-window abuf)))
3096 (setq buffer-read-only nil)
3097 (let ((inhibit-read-only t)) (erase-buffer))
3098 (org-agenda-mode)
3099 (and name (not org-agenda-name)
3100 (org-set-local 'org-agenda-name name)))
3101 (setq buffer-read-only nil))
3103 (defun org-finalize-agenda ()
3104 "Finishing touch for the agenda buffer, called just before displaying it."
3105 (unless org-agenda-multi
3106 (save-excursion
3107 (let ((inhibit-read-only t))
3108 (goto-char (point-min))
3109 (while (org-activate-bracket-links (point-max))
3110 (add-text-properties (match-beginning 0) (match-end 0)
3111 '(face org-link)))
3112 (org-agenda-align-tags)
3113 (unless org-agenda-with-colors
3114 (remove-text-properties (point-min) (point-max) '(face nil))))
3115 (if (and (boundp 'org-agenda-overriding-columns-format)
3116 org-agenda-overriding-columns-format)
3117 (org-set-local 'org-agenda-overriding-columns-format
3118 org-agenda-overriding-columns-format))
3119 (if (and (boundp 'org-agenda-view-columns-initially)
3120 org-agenda-view-columns-initially)
3121 (org-agenda-columns))
3122 (when org-agenda-fontify-priorities
3123 (org-agenda-fontify-priorities))
3124 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3125 (org-agenda-dim-blocked-tasks))
3126 (org-agenda-mark-clocking-task)
3127 (when org-agenda-entry-text-mode
3128 (org-agenda-entry-text-hide)
3129 (org-agenda-entry-text-show))
3130 (if (functionp 'org-habit-insert-consistency-graphs)
3131 (org-habit-insert-consistency-graphs))
3132 (run-hooks 'org-finalize-agenda-hook)
3133 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3134 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3135 (org-agenda-filter-apply org-agenda-filter))
3138 (defun org-agenda-mark-clocking-task ()
3139 "Mark the current clock entry in the agenda if it is present."
3140 (mapc (lambda (o)
3141 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3142 (delete-overlay o)))
3143 (overlays-in (point-min) (point-max)))
3144 (when (marker-buffer org-clock-hd-marker)
3145 (save-excursion
3146 (goto-char (point-min))
3147 (let (s ov)
3148 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3149 (goto-char s)
3150 (when (equal (org-get-at-bol 'org-hd-marker)
3151 org-clock-hd-marker)
3152 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3153 (overlay-put ov 'type 'org-agenda-clocking)
3154 (overlay-put ov 'face 'org-agenda-clocking)
3155 (overlay-put ov 'help-echo
3156 "The clock is running in this item")))))))
3158 (defun org-agenda-fontify-priorities ()
3159 "Make highest priority lines bold, and lowest italic."
3160 (interactive)
3161 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3162 (delete-overlay o)))
3163 (overlays-in (point-min) (point-max)))
3164 (save-excursion
3165 (let ((inhibit-read-only t)
3166 b e p ov h l)
3167 (goto-char (point-min))
3168 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3169 (setq h (or (get-char-property (point) 'org-highest-priority)
3170 org-highest-priority)
3171 l (or (get-char-property (point) 'org-lowest-priority)
3172 org-lowest-priority)
3173 p (string-to-char (match-string 1))
3174 b (match-beginning 0)
3175 e (if (eq org-agenda-fontify-priorities 'cookies)
3176 (match-end 0)
3177 (point-at-eol))
3178 ov (make-overlay b e))
3179 (overlay-put
3180 ov 'face
3181 (cond ((org-face-from-face-or-color
3182 'priority nil
3183 (cdr (assoc p org-priority-faces))))
3184 ((and (listp org-agenda-fontify-priorities)
3185 (org-face-from-face-or-color
3186 'priority nil
3187 (cdr (assoc p org-agenda-fontify-priorities)))))
3188 ((equal p l) 'italic)
3189 ((equal p h) 'bold)))
3190 (overlay-put ov 'org-type 'org-priority)))))
3192 (defun org-agenda-dim-blocked-tasks ()
3193 "Dim currently blocked TODO's in the agenda display."
3194 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3195 (delete-overlay o)))
3196 (overlays-in (point-min) (point-max)))
3197 (save-excursion
3198 (let ((inhibit-read-only t)
3199 (org-depend-tag-blocked nil)
3200 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3201 org-blocked-by-checkboxes
3202 invis1 b e p ov h l)
3203 (goto-char (point-min))
3204 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3205 (and pos (goto-char (1+ pos))))
3206 (setq org-blocked-by-checkboxes nil invis1 invis)
3207 (let ((marker (org-get-at-bol 'org-hd-marker)))
3208 (when (and marker
3209 (not (with-current-buffer (marker-buffer marker)
3210 (save-excursion
3211 (goto-char marker)
3212 (if (org-entry-get nil "NOBLOCKING")
3213 t ;; Never block this entry
3214 (run-hook-with-args-until-failure
3215 'org-blocker-hook
3216 (list :type 'todo-state-change
3217 :position marker
3218 :from 'todo
3219 :to 'done)))))))
3220 (if org-blocked-by-checkboxes (setq invis1 nil))
3221 (setq b (if invis1
3222 (max (point-min) (1- (point-at-bol)))
3223 (point-at-bol))
3224 e (point-at-eol)
3225 ov (make-overlay b e))
3226 (if invis1
3227 (overlay-put ov 'invisible t)
3228 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3229 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3231 (defvar org-agenda-skip-function nil
3232 "Function to be called at each match during agenda construction.
3233 If this function returns nil, the current match should not be skipped.
3234 Otherwise, the function must return a position from where the search
3235 should be continued.
3236 This may also be a Lisp form, it will be evaluated.
3237 Never set this variable using `setq' or so, because then it will apply
3238 to all future agenda commands. If you do want a global skipping condition,
3239 use the option `org-agenda-skip-function-global' instead.
3240 The correct usage for `org-agenda-skip-function' is to bind it with
3241 `let' to scope it dynamically into the agenda-constructing command.
3242 A good way to set it is through options in `org-agenda-custom-commands'.")
3244 (defun org-agenda-skip ()
3245 "Throw to `:skip' in places that should be skipped.
3246 Also moves point to the end of the skipped region, so that search can
3247 continue from there."
3248 (let ((p (point-at-bol)) to)
3249 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3250 (get-text-property p :org-archived)
3251 (org-end-of-subtree t)
3252 (throw :skip t))
3253 (and org-agenda-skip-comment-trees
3254 (get-text-property p :org-comment)
3255 (org-end-of-subtree t)
3256 (throw :skip t))
3257 (if (equal (char-after p) ?#) (throw :skip t))
3258 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3259 (org-agenda-skip-eval org-agenda-skip-function)))
3260 (goto-char to)
3261 (throw :skip t))))
3263 (defun org-agenda-skip-eval (form)
3264 "If FORM is a function or a list, call (or eval) is and return result.
3265 `save-excursion' and `save-match-data' are wrapped around the call, so point
3266 and match data are returned to the previous state no matter what these
3267 functions do."
3268 (let (fp)
3269 (and form
3270 (or (setq fp (functionp form))
3271 (consp form))
3272 (save-excursion
3273 (save-match-data
3274 (if fp
3275 (funcall form)
3276 (eval form)))))))
3278 (defvar org-agenda-markers nil
3279 "List of all currently active markers created by `org-agenda'.")
3280 (defvar org-agenda-last-marker-time (org-float-time)
3281 "Creation time of the last agenda marker.")
3283 (defun org-agenda-new-marker (&optional pos)
3284 "Return a new agenda marker.
3285 Org-mode keeps a list of these markers and resets them when they are
3286 no longer in use."
3287 (let ((m (copy-marker (or pos (point)))))
3288 (setq org-agenda-last-marker-time (org-float-time))
3289 (push m org-agenda-markers)
3292 (defun org-agenda-reset-markers ()
3293 "Reset markers created by `org-agenda'."
3294 (while org-agenda-markers
3295 (move-marker (pop org-agenda-markers) nil)))
3297 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3298 "Save relative positions of markers in region."
3299 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3300 org-agenda-markers))
3302 ;;; Entry text mode
3304 (defun org-agenda-entry-text-show-here ()
3305 "Add some text from the entry as context to the current line."
3306 (let (m txt o)
3307 (setq m (org-get-at-bol 'org-hd-marker))
3308 (unless (marker-buffer m)
3309 (error "No marker points to an entry here"))
3310 (setq txt (concat "\n" (org-no-properties
3311 (org-agenda-get-some-entry-text
3312 m org-agenda-entry-text-maxlines " > "))))
3313 (when (string-match "\\S-" txt)
3314 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3315 (overlay-put o 'evaporate t)
3316 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3317 (overlay-put o 'after-string txt))))
3319 (defun org-agenda-entry-text-show ()
3320 "Add entry context for all agenda lines."
3321 (interactive)
3322 (save-excursion
3323 (goto-char (point-max))
3324 (beginning-of-line 1)
3325 (while (not (bobp))
3326 (when (org-get-at-bol 'org-hd-marker)
3327 (org-agenda-entry-text-show-here))
3328 (beginning-of-line 0))))
3330 (defun org-agenda-entry-text-hide ()
3331 "Remove any shown entry context."
3332 (delq nil
3333 (mapcar (lambda (o)
3334 (if (eq (overlay-get o 'org-overlay-type)
3335 'agenda-entry-content)
3336 (progn (delete-overlay o) t)))
3337 (overlays-in (point-min) (point-max)))))
3339 (defun org-agenda-get-day-face (date)
3340 "Return the face DATE should be displayed with."
3341 (or (and (functionp org-agenda-day-face-function)
3342 (funcall org-agenda-day-face-function date))
3343 (cond ((org-agenda-todayp date)
3344 'org-agenda-date-today)
3345 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3346 'org-agenda-date-weekend)
3347 (t 'org-agenda-date))))
3349 ;;; Agenda timeline
3351 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3353 (defun org-timeline (&optional include-all)
3354 "Show a time-sorted view of the entries in the current org file.
3355 Only entries with a time stamp of today or later will be listed. With
3356 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3357 under the current date.
3358 If the buffer contains an active region, only check the region for
3359 dates."
3360 (interactive "P")
3361 (org-compile-prefix-format 'timeline)
3362 (org-set-sorting-strategy 'timeline)
3363 (let* ((dopast t)
3364 (dotodo include-all)
3365 (doclosed org-agenda-show-log)
3366 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3367 (current-buffer))))
3368 (date (calendar-current-date))
3369 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3370 (end (if (org-region-active-p) (region-end) (point-max)))
3371 (day-numbers (org-get-all-dates beg end 'no-ranges
3372 t doclosed ; always include today
3373 org-timeline-show-empty-dates))
3374 (org-deadline-warning-days 0)
3375 (org-agenda-only-exact-dates t)
3376 (today (org-today))
3377 (past t)
3378 args
3379 s e rtn d emptyp)
3380 (setq org-agenda-redo-command
3381 (list 'progn
3382 (list 'org-switch-to-buffer-other-window (current-buffer))
3383 (list 'org-timeline (list 'quote include-all))))
3384 (if (not dopast)
3385 ;; Remove past dates from the list of dates.
3386 (setq day-numbers (delq nil (mapcar (lambda(x)
3387 (if (>= x today) x nil))
3388 day-numbers))))
3389 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3390 (if doclosed (push :closed args))
3391 (push :timestamp args)
3392 (push :deadline args)
3393 (push :scheduled args)
3394 (push :sexp args)
3395 (if dotodo (push :todo args))
3396 (insert "Timeline of file " entry "\n")
3397 (add-text-properties (point-min) (point)
3398 (list 'face 'org-agenda-structure))
3399 (org-agenda-mark-header-line (point-min))
3400 (while (setq d (pop day-numbers))
3401 (if (and (listp d) (eq (car d) :omitted))
3402 (progn
3403 (setq s (point))
3404 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3405 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3406 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3407 (if (and (>= d today)
3408 dopast
3409 past)
3410 (progn
3411 (setq past nil)
3412 (insert (make-string 79 ?-) "\n")))
3413 (setq date (calendar-gregorian-from-absolute d))
3414 (setq s (point))
3415 (setq rtn (and (not emptyp)
3416 (apply 'org-agenda-get-day-entries entry
3417 date args)))
3418 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3419 (progn
3420 (insert
3421 (if (stringp org-agenda-format-date)
3422 (format-time-string org-agenda-format-date
3423 (org-time-from-absolute date))
3424 (funcall org-agenda-format-date date))
3425 "\n")
3426 (put-text-property s (1- (point)) 'face
3427 (org-agenda-get-day-face date))
3428 (put-text-property s (1- (point)) 'org-date-line t)
3429 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3430 (if (equal d today)
3431 (put-text-property s (1- (point)) 'org-today t))
3432 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3433 (put-text-property s (1- (point)) 'day d)))))
3434 (goto-char (point-min))
3435 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3436 (point-min)))
3437 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3438 (org-finalize-agenda)
3439 (setq buffer-read-only t)))
3441 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3442 "Return a list of all relevant day numbers from BEG to END buffer positions.
3443 If NO-RANGES is non-nil, include only the start and end dates of a range,
3444 not every single day in the range. If FORCE-TODAY is non-nil, make
3445 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3446 inactive time stamps (those in square brackets) are included.
3447 When EMPTY is non-nil, also include days without any entries."
3448 (let ((re (concat
3449 (if pre-re pre-re "")
3450 (if inactive org-ts-regexp-both org-ts-regexp)))
3451 dates dates1 date day day1 day2 ts1 ts2)
3452 (if force-today
3453 (setq dates (list (org-today))))
3454 (save-excursion
3455 (goto-char beg)
3456 (while (re-search-forward re end t)
3457 (setq day (time-to-days (org-time-string-to-time
3458 (substring (match-string 1) 0 10))))
3459 (or (memq day dates) (push day dates)))
3460 (unless no-ranges
3461 (goto-char beg)
3462 (while (re-search-forward org-tr-regexp end t)
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 ts1))
3466 day2 (time-to-days (org-time-string-to-time ts2)))
3467 (while (< (setq day1 (1+ day1)) day2)
3468 (or (memq day1 dates) (push day1 dates)))))
3469 (setq dates (sort dates '<))
3470 (when empty
3471 (while (setq day (pop dates))
3472 (setq day2 (car dates))
3473 (push day dates1)
3474 (when (and day2 empty)
3475 (if (or (eq empty t)
3476 (and (numberp empty) (<= (- day2 day) empty)))
3477 (while (< (setq day (1+ day)) day2)
3478 (push (list day) dates1))
3479 (push (cons :omitted (- day2 day)) dates1))))
3480 (setq dates (nreverse dates1)))
3481 dates)))
3483 ;;; Agenda Daily/Weekly
3485 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3486 "Start day for the agenda view.
3487 Custom commands can set this variable in the options section.")
3488 (defvar org-starting-day nil) ; local variable in the agenda buffer
3489 (defvar org-agenda-current-span nil
3490 "The current span used in the agenda view.") ; local variable in the agenda buffer
3491 (defvar org-include-all-loc nil) ; local variable
3493 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3494 "List of types searched for when creating the daily/weekly agenda.
3495 This variable is a list of symbols that controls the types of
3496 items that appear in the daily/weekly agenda. Allowed symbols in this
3497 list are are
3499 :timestamp List items containing a date stamp or date range matching
3500 the selected date. This includes sexp entries in
3501 angular brackets.
3503 :sexp List entries resulting from plain diary-like sexps.
3505 :deadline List deadline due on that date. When the date is today,
3506 also list any deadlines past due, or due within
3507 `org-deadline-warning-days'. `:deadline' must appear before
3508 `:scheduled' if the setting of
3509 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3510 any effect.
3512 :scheduled List all items which are scheduled for the given date.
3513 The diary for *today* also contains items which were
3514 scheduled earlier and are not yet marked DONE.
3516 By default, all four types are turned on.
3518 Never set this variable globally using `setq', because then it
3519 will apply to all future agenda commands. Instead, bind it with
3520 `let' to scope it dynamically into the agenda-constructing
3521 command. A good way to set it is through options in
3522 `org-agenda-custom-commands'. For a more flexible (though
3523 somewhat less efficient) way of determining what is included in
3524 the daily/weekly agenda, see `org-agenda-skip-function'.")
3526 ;;;###autoload
3527 (defun org-agenda-list (&optional include-all start-day span)
3528 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3529 The view will be for the current day or week, but from the overview buffer
3530 you will be able to go to other days/weeks.
3532 With a numeric prefix argument in an interactive call, the agenda will
3533 span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3534 the number of days. SPAN defaults to `org-agenda-span'.
3536 START-DAY defaults to TODAY, or to the most recent match for the weekday
3537 given in `org-agenda-start-on-weekday'."
3538 (interactive "P")
3539 (if (and (integerp include-all) (> include-all 0))
3540 (setq span include-all include-all nil))
3541 (setq start-day (or start-day org-agenda-start-day))
3542 (if org-agenda-overriding-arguments
3543 (setq include-all (car org-agenda-overriding-arguments)
3544 start-day (nth 1 org-agenda-overriding-arguments)
3545 span (nth 2 org-agenda-overriding-arguments)))
3546 (if (stringp start-day)
3547 ;; Convert to an absolute day number
3548 (setq start-day (time-to-days (org-read-date nil t start-day))))
3549 (setq org-agenda-last-arguments (list include-all start-day span))
3550 (org-compile-prefix-format 'agenda)
3551 (org-set-sorting-strategy 'agenda)
3552 (let* ((span (org-agenda-ndays-to-span
3553 (or span org-agenda-ndays org-agenda-span)))
3554 (today (org-today))
3555 (sd (or start-day today))
3556 (ndays (org-agenda-span-to-ndays span sd))
3557 (org-agenda-start-on-weekday
3558 (if (eq ndays 7)
3559 org-agenda-start-on-weekday))
3560 (thefiles (org-agenda-files nil 'ifmode))
3561 (files thefiles)
3562 (start (if (or (null org-agenda-start-on-weekday)
3563 (< ndays 7))
3565 (let* ((nt (calendar-day-of-week
3566 (calendar-gregorian-from-absolute sd)))
3567 (n1 org-agenda-start-on-weekday)
3568 (d (- nt n1)))
3569 (- sd (+ (if (< d 0) 7 0) d)))))
3570 (day-numbers (list start))
3571 (day-cnt 0)
3572 (inhibit-redisplay (not debug-on-error))
3573 s e rtn rtnall file date d start-pos end-pos todayp
3574 clocktable-start clocktable-end filter)
3575 (setq org-agenda-redo-command
3576 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3577 (dotimes (n (1- ndays))
3578 (push (1+ (car day-numbers)) day-numbers))
3579 (setq day-numbers (nreverse day-numbers))
3580 (setq clocktable-start (car day-numbers)
3581 clocktable-end (1+ (or (org-last day-numbers) 0)))
3582 (org-prepare-agenda "Day/Week")
3583 (org-set-local 'org-starting-day (car day-numbers))
3584 (org-set-local 'org-include-all-loc include-all)
3585 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3586 (unless org-agenda-compact-blocks
3587 (let* ((d1 (car day-numbers))
3588 (d2 (org-last day-numbers))
3589 (w1 (org-days-to-iso-week d1))
3590 (w2 (org-days-to-iso-week d2)))
3591 (setq s (point))
3592 (if org-agenda-overriding-header
3593 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3594 nil 'face 'org-agenda-structure) "\n")
3595 (insert (org-agenda-span-name span)
3596 "-agenda"
3597 (if (< (- d2 d1) 350)
3598 (if (= w1 w2)
3599 (format " (W%02d)" w1)
3600 (format " (W%02d-W%02d)" w1 w2))
3602 ":\n")))
3603 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3604 'org-date-line t))
3605 (org-agenda-mark-header-line s))
3606 (while (setq d (pop day-numbers))
3607 (setq date (calendar-gregorian-from-absolute d)
3608 s (point))
3609 (if (or (setq todayp (= d today))
3610 (and (not start-pos) (= d sd)))
3611 (setq start-pos (point))
3612 (if (and start-pos (not end-pos))
3613 (setq end-pos (point))))
3614 (setq files thefiles
3615 rtnall nil)
3616 (while (setq file (pop files))
3617 (catch 'nextfile
3618 (org-check-agenda-file file)
3619 (let ((org-agenda-entry-types org-agenda-entry-types))
3620 (unless org-agenda-include-deadlines
3621 (setq org-agenda-entry-types
3622 (delq :deadline org-agenda-entry-types)))
3623 (cond
3624 ((memq org-agenda-show-log '(only clockcheck))
3625 (setq rtn (org-agenda-get-day-entries
3626 file date :closed)))
3627 (org-agenda-show-log
3628 (setq rtn (apply 'org-agenda-get-day-entries
3629 file date
3630 (append '(:closed) org-agenda-entry-types))))
3632 (setq rtn (apply 'org-agenda-get-day-entries
3633 file date
3634 org-agenda-entry-types)))))
3635 (setq rtnall (append rtnall rtn))))
3636 (if org-agenda-include-diary
3637 (let ((org-agenda-search-headline-for-time t))
3638 (require 'diary-lib)
3639 (setq rtn (org-get-entries-from-diary date))
3640 (setq rtnall (append rtnall rtn))))
3641 (if (or rtnall org-agenda-show-all-dates)
3642 (progn
3643 (setq day-cnt (1+ day-cnt))
3644 (insert
3645 (if (stringp org-agenda-format-date)
3646 (format-time-string org-agenda-format-date
3647 (org-time-from-absolute date))
3648 (funcall org-agenda-format-date date))
3649 "\n")
3650 (put-text-property s (1- (point)) 'face
3651 (org-agenda-get-day-face date))
3652 (put-text-property s (1- (point)) 'org-date-line t)
3653 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3654 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3655 (when todayp
3656 (put-text-property s (1- (point)) 'org-today t))
3657 (if rtnall (insert
3658 (org-finalize-agenda-entries
3659 (org-agenda-add-time-grid-maybe
3660 rtnall ndays todayp))
3661 "\n"))
3662 (put-text-property s (1- (point)) 'day d)
3663 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3664 (when (and org-agenda-clockreport-mode clocktable-start)
3665 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3666 ;; the above line is to ensure the restricted range!
3667 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3668 tbl)
3669 (setq p (org-plist-delete p :block))
3670 (setq p (plist-put p :tstart clocktable-start))
3671 (setq p (plist-put p :tend clocktable-end))
3672 (setq p (plist-put p :scope 'agenda))
3673 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3674 (setq filter (or org-agenda-filter-while-redo
3675 (get 'org-agenda-filter :preset-filter))))
3676 (setq p (plist-put p :tags (mapconcat (lambda (x)
3677 (if (string-match "[<>=]" x)
3680 filter ""))))
3681 (setq tbl (apply 'org-get-clocktable p))
3682 (insert tbl)))
3683 (goto-char (point-min))
3684 (or org-agenda-multi (org-fit-agenda-window))
3685 (unless (and (pos-visible-in-window-p (point-min))
3686 (pos-visible-in-window-p (point-max)))
3687 (goto-char (1- (point-max)))
3688 (recenter -1)
3689 (if (not (pos-visible-in-window-p (or start-pos 1)))
3690 (progn
3691 (goto-char (or start-pos 1))
3692 (recenter 1))))
3693 (goto-char (or start-pos 1))
3694 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3695 (if (eq org-agenda-show-log 'clockcheck)
3696 (org-agenda-show-clocking-issues))
3697 (org-finalize-agenda)
3698 (setq buffer-read-only t)
3699 (message "")))
3701 (defun org-agenda-ndays-to-span (n)
3702 "Return a span symbol for a span of N days, or N if none matches."
3703 (cond ((symbolp n) n)
3704 ((= n 1) 'day)
3705 ((= n 7) 'week)
3706 (t n)))
3708 (defun org-agenda-span-to-ndays (span start-day)
3709 "Return ndays from SPAN starting at START-DAY."
3710 (cond ((numberp span) span)
3711 ((eq span 'day) 1)
3712 ((eq span 'week) 7)
3713 ((eq span 'month)
3714 (let ((date (calendar-gregorian-from-absolute start-day)))
3715 (calendar-last-day-of-month (car date) (caddr date))))
3716 ((eq span 'year)
3717 (let ((date (calendar-gregorian-from-absolute start-day)))
3718 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3720 (defun org-agenda-span-name (span)
3721 "Return a SPAN name."
3722 (if (null span)
3724 (if (symbolp span)
3725 (capitalize (symbol-name span))
3726 (format "%d days" span))))
3728 ;;; Agenda word search
3730 (defvar org-agenda-search-history nil)
3731 (defvar org-todo-only nil)
3733 (defvar org-search-syntax-table nil
3734 "Special syntax table for org-mode search.
3735 In this table, we have single quotes not as word constituents, to
3736 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3738 (defun org-search-syntax-table ()
3739 (unless org-search-syntax-table
3740 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3741 (modify-syntax-entry ?' "." org-search-syntax-table)
3742 (modify-syntax-entry ?` "." org-search-syntax-table))
3743 org-search-syntax-table)
3745 (defvar org-agenda-last-search-view-search-was-boolean nil)
3747 ;;;###autoload
3748 (defun org-search-view (&optional todo-only string edit-at)
3749 "Show all entries that contain a phrase or words or regular expressions.
3751 With optional prefix argument TODO-ONLY, only consider entries that are
3752 TODO entries. The argument STRING can be used to pass a default search
3753 string into this function. If EDIT-AT is non-nil, it means that the
3754 user should get a chance to edit this string, with cursor at position
3755 EDIT-AT.
3757 The search string can be viewed either as a phrase that should be found as
3758 is, or it can be broken into a number of snippets, each of which must match
3759 in a Boolean way to select an entry. The default depends on the variable
3760 `org-agenda-search-view-always-boolean'.
3761 Even if this is turned off (the default) you can always switch to
3762 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3764 The default is a direct search of the whole phrase, where each space in
3765 the search string can expand to an arbitrary amount of whitespace,
3766 including newlines.
3768 If using a Boolean search, the search string is split on whitespace and
3769 each snippet is searched separately, with logical AND to select an entry.
3770 Words prefixed with a minus must *not* occur in the entry. Words without
3771 a prefix or prefixed with a plus must occur in the entry. Matching is
3772 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3773 match whole words, not parts of a word) if
3774 `org-agenda-search-view-force-full-words' is set (default is nil).
3776 Boolean search snippets enclosed by curly braces are interpreted as
3777 regular expressions that must or (when preceded with \"-\") must not
3778 match in the entry. Snippets enclosed into double quotes will be taken
3779 as a whole, to include whitespace.
3781 - If the search string starts with an asterisk, search only in headlines.
3782 - If (possibly after the leading star) the search string starts with an
3783 exclamation mark, this also means to look at TODO entries only, an effect
3784 that can also be achieved with a prefix argument.
3785 - If (possibly after star and exclamation mark) the search string starts
3786 with a colon, this will mean that the (non-regexp) snippets of the
3787 Boolean search must match as full words.
3789 This command searches the agenda files, and in addition the files listed
3790 in `org-agenda-text-search-extra-files'."
3791 (interactive "P")
3792 (org-compile-prefix-format 'search)
3793 (org-set-sorting-strategy 'search)
3794 (org-prepare-agenda "SEARCH")
3795 (let* ((props (list 'face nil
3796 'done-face 'org-agenda-done
3797 'org-not-done-regexp org-not-done-regexp
3798 'org-todo-regexp org-todo-regexp
3799 'org-complex-heading-regexp org-complex-heading-regexp
3800 'mouse-face 'highlight
3801 'help-echo (format "mouse-2 or RET jump to location")))
3802 (full-words org-agenda-search-view-force-full-words)
3803 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3804 regexp rtn rtnall files file pos
3805 marker category tags c neg re boolean
3806 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3807 (unless (and (not edit-at)
3808 (stringp string)
3809 (string-match "\\S-" string))
3810 (setq string (read-string
3811 (if org-agenda-search-view-always-boolean
3812 "[+-]Word/{Regexp} ...: "
3813 "Phrase, or [+-]Word/{Regexp} ...: ")
3814 (cond
3815 ((integerp edit-at) (cons string edit-at))
3816 (edit-at string))
3817 'org-agenda-search-history)))
3818 (org-set-local 'org-todo-only todo-only)
3819 (setq org-agenda-redo-command
3820 (list 'org-search-view (if todo-only t nil) string
3821 '(if current-prefix-arg 1 nil)))
3822 (setq org-agenda-query-string string)
3824 (if (equal (string-to-char string) ?*)
3825 (setq hdl-only t
3826 words (substring string 1))
3827 (setq words string))
3828 (when (equal (string-to-char words) ?!)
3829 (setq todo-only t
3830 words (substring words 1)))
3831 (when (equal (string-to-char words) ?:)
3832 (setq full-words t
3833 words (substring words 1)))
3834 (if (or org-agenda-search-view-always-boolean
3835 (member (string-to-char words) '(?- ?+ ?\{)))
3836 (setq boolean t))
3837 (setq words (org-split-string words))
3838 (let (www w)
3839 (while (setq w (pop words))
3840 (while (and (string-match "\\\\\\'" w) words)
3841 (setq w (concat (substring w 0 -1) " " (pop words))))
3842 (push w www))
3843 (setq words (nreverse www) www nil)
3844 (while (setq w (pop words))
3845 (when (and (string-match "\\`[-+]?{" w)
3846 (not (string-match "}\\'" w)))
3847 (while (and words (not (string-match "}\\'" (car words))))
3848 (setq w (concat w " " (pop words))))
3849 (setq w (concat w " " (pop words))))
3850 (push w www))
3851 (setq words (nreverse www)))
3852 (setq org-agenda-last-search-view-search-was-boolean boolean)
3853 (when boolean
3854 (let (wds w)
3855 (while (setq w (pop words))
3856 (if (or (equal (substring w 0 1) "\"")
3857 (and (> (length w) 1)
3858 (member (substring w 0 1) '("+" "-"))
3859 (equal (substring w 1 2) "\"")))
3860 (while (and words (not (equal (substring w -1) "\"")))
3861 (setq w (concat w " " (pop words)))))
3862 (and (string-match "\\`\\([-+]?\\)\"" w)
3863 (setq w (replace-match "\\1" nil nil w)))
3864 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3865 (push w wds))
3866 (setq words (nreverse wds))))
3867 (if boolean
3868 (mapc (lambda (w)
3869 (setq c (string-to-char w))
3870 (if (equal c ?-)
3871 (setq neg t w (substring w 1))
3872 (if (equal c ?+)
3873 (setq neg nil w (substring w 1))
3874 (setq neg nil)))
3875 (if (string-match "\\`{.*}\\'" w)
3876 (setq re (substring w 1 -1))
3877 (if full-words
3878 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3879 (setq re (regexp-quote (downcase w)))))
3880 (if neg (push re regexps-) (push re regexps+)))
3881 words)
3882 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3883 regexps+))
3884 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3885 (if (not regexps+)
3886 (setq regexp org-outline-regexp-bol)
3887 (setq regexp (pop regexps+))
3888 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3889 regexp))))
3890 (setq files (org-agenda-files nil 'ifmode))
3891 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3892 (pop org-agenda-text-search-extra-files)
3893 (setq files (org-add-archive-files files)))
3894 (setq files (append files org-agenda-text-search-extra-files)
3895 rtnall nil)
3896 (while (setq file (pop files))
3897 (setq ee nil)
3898 (catch 'nextfile
3899 (org-check-agenda-file file)
3900 (setq buffer (if (file-exists-p file)
3901 (org-get-agenda-file-buffer file)
3902 (error "No such file %s" file)))
3903 (if (not buffer)
3904 ;; If file does not exist, make sure an error message is sent
3905 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3906 file))))
3907 (with-current-buffer buffer
3908 (with-syntax-table (org-search-syntax-table)
3909 (unless (org-mode-p)
3910 (error "Agenda file %s is not in `org-mode'" file))
3911 (let ((case-fold-search t))
3912 (save-excursion
3913 (save-restriction
3914 (if org-agenda-restrict
3915 (narrow-to-region org-agenda-restrict-begin
3916 org-agenda-restrict-end)
3917 (widen))
3918 (goto-char (point-min))
3919 (unless (or (org-on-heading-p)
3920 (outline-next-heading))
3921 (throw 'nextfile t))
3922 (goto-char (max (point-min) (1- (point))))
3923 (while (re-search-forward regexp nil t)
3924 (org-back-to-heading t)
3925 (skip-chars-forward "* ")
3926 (setq beg (point-at-bol)
3927 beg1 (point)
3928 end (progn (outline-next-heading) (point)))
3929 (catch :skip
3930 (goto-char beg)
3931 (org-agenda-skip)
3932 (setq str (buffer-substring-no-properties
3933 (point-at-bol)
3934 (if hdl-only (point-at-eol) end)))
3935 (mapc (lambda (wr) (when (string-match wr str)
3936 (goto-char (1- end))
3937 (throw :skip t)))
3938 regexps-)
3939 (mapc (lambda (wr) (unless (string-match wr str)
3940 (goto-char (1- end))
3941 (throw :skip t)))
3942 (if todo-only
3943 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3944 regexps+)
3945 regexps+))
3946 (goto-char beg)
3947 (setq marker (org-agenda-new-marker (point))
3948 category (org-get-category)
3949 tags (org-get-tags-at (point))
3950 txt (org-format-agenda-item
3952 (buffer-substring-no-properties
3953 beg1 (point-at-eol))
3954 category tags))
3955 (org-add-props txt props
3956 'org-marker marker 'org-hd-marker marker
3957 'org-todo-regexp org-todo-regexp
3958 'org-complex-heading-regexp org-complex-heading-regexp
3959 'priority 1000 'org-category category
3960 'type "search")
3961 (push txt ee)
3962 (goto-char (1- end))))))))))
3963 (setq rtn (nreverse ee))
3964 (setq rtnall (append rtnall rtn)))
3965 (if org-agenda-overriding-header
3966 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3967 nil 'face 'org-agenda-structure) "\n")
3968 (insert "Search words: ")
3969 (add-text-properties (point-min) (1- (point))
3970 (list 'face 'org-agenda-structure))
3971 (setq pos (point))
3972 (insert string "\n")
3973 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3974 (setq pos (point))
3975 (unless org-agenda-multi
3976 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3977 (add-text-properties pos (1- (point))
3978 (list 'face 'org-agenda-structure))))
3979 (org-agenda-mark-header-line (point-min))
3980 (when rtnall
3981 (insert (org-finalize-agenda-entries rtnall) "\n"))
3982 (goto-char (point-min))
3983 (or org-agenda-multi (org-fit-agenda-window))
3984 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3985 (org-finalize-agenda)
3986 (setq buffer-read-only t)))
3988 ;;; Agenda TODO list
3990 (defvar org-select-this-todo-keyword nil)
3991 (defvar org-last-arg nil)
3993 ;;;###autoload
3994 (defun org-todo-list (arg)
3995 "Show all (not done) TODO entries from all agenda file in a single list.
3996 The prefix arg can be used to select a specific TODO keyword and limit
3997 the list to these. When using \\[universal-argument], you will be prompted
3998 for a keyword. A numeric prefix directly selects the Nth keyword in
3999 `org-todo-keywords-1'."
4000 (interactive "P")
4001 (org-compile-prefix-format 'todo)
4002 (org-set-sorting-strategy 'todo)
4003 (org-prepare-agenda "TODO")
4004 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4005 (let* ((today (org-today))
4006 (date (calendar-gregorian-from-absolute today))
4007 (kwds org-todo-keywords-for-agenda)
4008 (completion-ignore-case t)
4009 (org-select-this-todo-keyword
4010 (if (stringp arg) arg
4011 (and arg (integerp arg) (> arg 0)
4012 (nth (1- arg) kwds))))
4013 rtn rtnall files file pos)
4014 (when (equal arg '(4))
4015 (setq org-select-this-todo-keyword
4016 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4017 (mapcar 'list kwds) nil nil)))
4018 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4019 (org-set-local 'org-last-arg arg)
4020 (setq org-agenda-redo-command
4021 '(org-todo-list (or current-prefix-arg org-last-arg)))
4022 (setq files (org-agenda-files nil 'ifmode)
4023 rtnall nil)
4024 (while (setq file (pop files))
4025 (catch 'nextfile
4026 (org-check-agenda-file file)
4027 (setq rtn (org-agenda-get-day-entries file date :todo))
4028 (setq rtnall (append rtnall rtn))))
4029 (if org-agenda-overriding-header
4030 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4031 nil 'face 'org-agenda-structure) "\n")
4032 (insert "Global list of TODO items of type: ")
4033 (add-text-properties (point-min) (1- (point))
4034 (list 'face 'org-agenda-structure
4035 'short-heading
4036 (concat "ToDo: "
4037 (or org-select-this-todo-keyword "ALL"))))
4038 (org-agenda-mark-header-line (point-min))
4039 (setq pos (point))
4040 (insert (or org-select-this-todo-keyword "ALL") "\n")
4041 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4042 (setq pos (point))
4043 (unless org-agenda-multi
4044 (insert "Available with `N r': (0)ALL")
4045 (let ((n 0) s)
4046 (mapc (lambda (x)
4047 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4048 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4049 (insert "\n "))
4050 (insert " " s))
4051 kwds))
4052 (insert "\n"))
4053 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4054 (org-agenda-mark-header-line (point-min))
4055 (when rtnall
4056 (insert (org-finalize-agenda-entries rtnall) "\n"))
4057 (goto-char (point-min))
4058 (or org-agenda-multi (org-fit-agenda-window))
4059 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4060 (org-finalize-agenda)
4061 (setq buffer-read-only t)))
4063 ;;; Agenda tags match
4065 ;;;###autoload
4066 (defun org-tags-view (&optional todo-only match)
4067 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4068 The prefix arg TODO-ONLY limits the search to TODO entries."
4069 (interactive "P")
4070 (org-compile-prefix-format 'tags)
4071 (org-set-sorting-strategy 'tags)
4072 (let* ((org-tags-match-list-sublevels
4073 org-tags-match-list-sublevels)
4074 (completion-ignore-case t)
4075 rtn rtnall files file pos matcher
4076 buffer)
4077 (when (and (stringp match) (not (string-match "\\S-" match)))
4078 (setq match nil))
4079 (setq matcher (org-make-tags-matcher match)
4080 match (car matcher) matcher (cdr matcher))
4081 (org-prepare-agenda (concat "TAGS " match))
4082 (setq org-agenda-query-string match)
4083 (setq org-agenda-redo-command
4084 (list 'org-tags-view (list 'quote todo-only)
4085 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4086 (setq files (org-agenda-files nil 'ifmode)
4087 rtnall nil)
4088 (while (setq file (pop files))
4089 (catch 'nextfile
4090 (org-check-agenda-file file)
4091 (setq buffer (if (file-exists-p file)
4092 (org-get-agenda-file-buffer file)
4093 (error "No such file %s" file)))
4094 (if (not buffer)
4095 ;; If file does not exist, error message to agenda
4096 (setq rtn (list
4097 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4098 rtnall (append rtnall rtn))
4099 (with-current-buffer buffer
4100 (unless (org-mode-p)
4101 (error "Agenda file %s is not in `org-mode'" file))
4102 (save-excursion
4103 (save-restriction
4104 (if org-agenda-restrict
4105 (narrow-to-region org-agenda-restrict-begin
4106 org-agenda-restrict-end)
4107 (widen))
4108 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4109 (setq rtnall (append rtnall rtn))))))))
4110 (if org-agenda-overriding-header
4111 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4112 nil 'face 'org-agenda-structure) "\n")
4113 (insert "Headlines with TAGS match: ")
4114 (add-text-properties (point-min) (1- (point))
4115 (list 'face 'org-agenda-structure
4116 'short-heading
4117 (concat "Match: " match)))
4118 (setq pos (point))
4119 (insert match "\n")
4120 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4121 (setq pos (point))
4122 (unless org-agenda-multi
4123 (insert "Press `C-u r' to search again with new search string\n"))
4124 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4125 (org-agenda-mark-header-line (point-min))
4126 (when rtnall
4127 (insert (org-finalize-agenda-entries rtnall) "\n"))
4128 (goto-char (point-min))
4129 (or org-agenda-multi (org-fit-agenda-window))
4130 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4131 (org-finalize-agenda)
4132 (setq buffer-read-only t)))
4134 ;;; Agenda Finding stuck projects
4136 (defvar org-agenda-skip-regexp nil
4137 "Regular expression used in skipping subtrees for the agenda.
4138 This is basically a temporary global variable that can be set and then
4139 used by user-defined selections using `org-agenda-skip-function'.")
4141 (defvar org-agenda-overriding-header nil
4142 "When set during agenda, todo and tags searches it replaces the header.
4143 This variable should not be set directly, but custom commands can bind it
4144 in the options section.")
4146 (defun org-agenda-skip-entry-when-regexp-matches ()
4147 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4148 If yes, it returns the end position of this entry, causing agenda commands
4149 to skip the entry but continuing the search in the subtree. This is a
4150 function that can be put into `org-agenda-skip-function' for the duration
4151 of a command."
4152 (let ((end (save-excursion (org-end-of-subtree t)))
4153 skip)
4154 (save-excursion
4155 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4156 (and skip end)))
4158 (defun org-agenda-skip-subtree-when-regexp-matches ()
4159 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4160 If yes, it returns the end position of this tree, causing agenda commands
4161 to skip this subtree. This is a function that can be put into
4162 `org-agenda-skip-function' for the duration of a command."
4163 (let ((end (save-excursion (org-end-of-subtree t)))
4164 skip)
4165 (save-excursion
4166 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4167 (and skip end)))
4169 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4170 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4171 If yes, it returns the end position of the current entry (NOT the tree),
4172 causing agenda commands to skip the entry but continuing the search in
4173 the subtree. This is a function that can be put into
4174 `org-agenda-skip-function' for the duration of a command. An important
4175 use of this function is for the stuck project list."
4176 (let ((end (save-excursion (org-end-of-subtree t)))
4177 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4178 skip)
4179 (save-excursion
4180 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4181 (and skip entry-end)))
4183 (defun org-agenda-skip-entry-if (&rest conditions)
4184 "Skip entry if any of CONDITIONS is true.
4185 See `org-agenda-skip-if' for details."
4186 (org-agenda-skip-if nil conditions))
4188 (defun org-agenda-skip-subtree-if (&rest conditions)
4189 "Skip entry if any of CONDITIONS is true.
4190 See `org-agenda-skip-if' for details."
4191 (org-agenda-skip-if t conditions))
4193 (defun org-agenda-skip-if (subtree conditions)
4194 "Checks current entity for CONDITIONS.
4195 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4196 the entry, i.e. the text before the next heading is checked.
4198 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4199 from different tests. Valid conditions are:
4201 scheduled Check if there is a scheduled cookie
4202 notscheduled Check if there is no scheduled cookie
4203 deadline Check if there is a deadline
4204 notdeadline Check if there is no deadline
4205 timestamp Check if there is a timestamp (also deadline or scheduled)
4206 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4207 regexp Check if regexp matches
4208 notregexp Check if regexp does not match.
4209 todo Check if TODO keyword matches
4210 nottodo Check if TODO keyword does not match
4212 The regexp is taken from the conditions list, it must come right after
4213 the `regexp' or `notregexp' element.
4215 `todo' and `nottodo' accept as an argument a list of todo
4216 keywords, which may include \"*\" to match any todo keyword.
4218 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4220 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4222 Instead of a list a keyword class may be given
4224 (org-agenda-skip-entry-if 'nottodo 'done)
4226 would skip entries that haven't been marked with any of \"DONE\"
4227 keywords. Possible classes are: `todo', `done', `any'.
4229 If any of these conditions is met, this function returns the end point of
4230 the entity, causing the search to continue from there. This is a function
4231 that can be put into `org-agenda-skip-function' for the duration of a command."
4232 (let (beg end m)
4233 (org-back-to-heading t)
4234 (setq beg (point)
4235 end (if subtree
4236 (progn (org-end-of-subtree t) (point))
4237 (progn (outline-next-heading) (1- (point)))))
4238 (goto-char beg)
4239 (and
4241 (and (memq 'scheduled conditions)
4242 (re-search-forward org-scheduled-time-regexp end t))
4243 (and (memq 'notscheduled conditions)
4244 (not (re-search-forward org-scheduled-time-regexp end t)))
4245 (and (memq 'deadline conditions)
4246 (re-search-forward org-deadline-time-regexp end t))
4247 (and (memq 'notdeadline conditions)
4248 (not (re-search-forward org-deadline-time-regexp end t)))
4249 (and (memq 'timestamp conditions)
4250 (re-search-forward org-ts-regexp end t))
4251 (and (memq 'nottimestamp conditions)
4252 (not (re-search-forward org-ts-regexp end t)))
4253 (and (setq m (memq 'regexp conditions))
4254 (stringp (nth 1 m))
4255 (re-search-forward (nth 1 m) end t))
4256 (and (setq m (memq 'notregexp conditions))
4257 (stringp (nth 1 m))
4258 (not (re-search-forward (nth 1 m) end t)))
4259 (and (or
4260 (setq m (memq 'todo conditions))
4261 (setq m (memq 'nottodo conditions)))
4262 (org-agenda-skip-if-todo m end)))
4263 end)))
4265 (defun org-agenda-skip-if-todo (args end)
4266 "Helper function for `org-agenda-skip-if', do not use it directly.
4267 ARGS is a list with first element either `todo' or `nottodo'.
4268 The remainder is either a list of TODO keywords, or a state symbol
4269 `todo' or `done' or `any'."
4270 (let ((kw (car args))
4271 (arg (cadr args))
4272 todo-wds todo-re)
4273 (setq todo-wds
4274 (org-uniquify
4275 (cond
4276 ((listp arg) ;; list of keywords
4277 (if (member "*" arg)
4278 (mapcar 'substring-no-properties org-todo-keywords-1)
4279 arg))
4280 ((symbolp arg) ;; keyword class name
4281 (cond
4282 ((eq arg 'todo)
4283 (org-delete-all org-done-keywords
4284 (mapcar 'substring-no-properties
4285 org-todo-keywords-1)))
4286 ((eq arg 'done) org-done-keywords)
4287 ((eq arg 'any)
4288 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4289 (setq todo-re
4290 (concat "^\\*+[ \t]+\\<\\("
4291 (mapconcat 'identity todo-wds "\\|")
4292 "\\)\\>"))
4293 (if (eq kw 'todo)
4294 (re-search-forward todo-re end t)
4295 (not (re-search-forward todo-re end t)))))
4297 ;;;###autoload
4298 (defun org-agenda-list-stuck-projects (&rest ignore)
4299 "Create agenda view for projects that are stuck.
4300 Stuck projects are project that have no next actions. For the definitions
4301 of what a project is and how to check if it stuck, customize the variable
4302 `org-stuck-projects'."
4303 (interactive)
4304 (let* ((org-agenda-skip-function
4305 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4306 ;; We could have used org-agenda-skip-if here.
4307 (org-agenda-overriding-header
4308 (or org-agenda-overriding-header "List of stuck projects: "))
4309 (matcher (nth 0 org-stuck-projects))
4310 (todo (nth 1 org-stuck-projects))
4311 (todo-wds (if (member "*" todo)
4312 (progn
4313 (org-prepare-agenda-buffers (org-agenda-files
4314 nil 'ifmode))
4315 (org-delete-all
4316 org-done-keywords-for-agenda
4317 (copy-sequence org-todo-keywords-for-agenda)))
4318 todo))
4319 (todo-re (concat "^\\*+[ \t]+\\("
4320 (mapconcat 'identity todo-wds "\\|")
4321 "\\)\\>"))
4322 (tags (nth 2 org-stuck-projects))
4323 (tags-re (if (member "*" tags)
4324 (org-re (concat org-outline-regexp-bol
4325 ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4326 (if tags
4327 (concat org-outline-regexp-bol
4328 ".*:\\("
4329 (mapconcat 'identity tags "\\|")
4330 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4331 (gen-re (nth 3 org-stuck-projects))
4332 (re-list
4333 (delq nil
4334 (list
4335 (if todo todo-re)
4336 (if tags tags-re)
4337 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4338 gen-re)))))
4339 (setq org-agenda-skip-regexp
4340 (if re-list
4341 (mapconcat 'identity re-list "\\|")
4342 (error "No information how to identify unstuck projects")))
4343 (org-tags-view nil matcher)
4344 (with-current-buffer org-agenda-buffer-name
4345 (setq org-agenda-redo-command
4346 '(org-agenda-list-stuck-projects
4347 (or current-prefix-arg org-last-arg))))))
4349 ;;; Diary integration
4351 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4352 (defvar list-diary-entries-hook)
4353 (defvar diary-time-regexp)
4354 (defun org-get-entries-from-diary (date)
4355 "Get the (Emacs Calendar) diary entries for DATE."
4356 (require 'diary-lib)
4357 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4358 (diary-display-hook '(fancy-diary-display))
4359 (diary-display-function 'fancy-diary-display)
4360 (pop-up-frames nil)
4361 (list-diary-entries-hook
4362 (cons 'org-diary-default-entry list-diary-entries-hook))
4363 (diary-file-name-prefix-function nil) ; turn this feature off
4364 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4365 entries
4366 (org-disable-agenda-to-diary t))
4367 (save-excursion
4368 (save-window-excursion
4369 (funcall (if (fboundp 'diary-list-entries)
4370 'diary-list-entries 'list-diary-entries)
4371 date 1)))
4372 (if (not (get-buffer diary-fancy-buffer))
4373 (setq entries nil)
4374 (with-current-buffer diary-fancy-buffer
4375 (setq buffer-read-only nil)
4376 (if (zerop (buffer-size))
4377 ;; No entries
4378 (setq entries nil)
4379 ;; Omit the date and other unnecessary stuff
4380 (org-agenda-cleanup-fancy-diary)
4381 ;; Add prefix to each line and extend the text properties
4382 (if (zerop (buffer-size))
4383 (setq entries nil)
4384 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4385 (setq entries
4386 (with-temp-buffer
4387 (insert entries) (goto-char (point-min))
4388 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4389 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4390 (replace-match (concat "; " (match-string 1)))))
4391 (buffer-string)))))
4392 (set-buffer-modified-p nil)
4393 (kill-buffer diary-fancy-buffer)))
4394 (when entries
4395 (setq entries (org-split-string entries "\n"))
4396 (setq entries
4397 (mapcar
4398 (lambda (x)
4399 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4400 ;; Extend the text properties to the beginning of the line
4401 (org-add-props x (text-properties-at (1- (length x)) x)
4402 'type "diary" 'date date 'face 'org-agenda-diary))
4403 entries)))))
4405 (defvar org-agenda-cleanup-fancy-diary-hook nil
4406 "Hook run when the fancy diary buffer is cleaned up.")
4408 (defun org-agenda-cleanup-fancy-diary ()
4409 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4410 This gets rid of the date, the underline under the date, and
4411 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4412 date. It also removes lines that contain only whitespace."
4413 (goto-char (point-min))
4414 (if (looking-at ".*?:[ \t]*")
4415 (progn
4416 (replace-match "")
4417 (re-search-forward "\n=+$" nil t)
4418 (replace-match "")
4419 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4420 (re-search-forward "\n=+$" nil t)
4421 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4422 (goto-char (point-min))
4423 (while (re-search-forward "^ +\n" nil t)
4424 (replace-match ""))
4425 (goto-char (point-min))
4426 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4427 (replace-match ""))
4428 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4430 ;; Make sure entries from the diary have the right text properties.
4431 (eval-after-load "diary-lib"
4432 '(if (boundp 'diary-modify-entry-list-string-function)
4433 ;; We can rely on the hook, nothing to do
4435 ;; Hook not available, must use advice to make this work
4436 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4437 "Make the position visible."
4438 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4439 (stringp string)
4440 buffer-file-name)
4441 (setq string (org-modify-diary-entry-string string))))))
4443 (defun org-modify-diary-entry-string (string)
4444 "Add text properties to string, allowing org-mode to act on it."
4445 (org-add-props string nil
4446 'mouse-face 'highlight
4447 'help-echo (if buffer-file-name
4448 (format "mouse-2 or RET jump to diary file %s"
4449 (abbreviate-file-name buffer-file-name))
4451 'org-agenda-diary-link t
4452 'org-marker (org-agenda-new-marker (point-at-bol))))
4454 (defun org-diary-default-entry ()
4455 "Add a dummy entry to the diary.
4456 Needed to avoid empty dates which mess up holiday display."
4457 ;; Catch the error if dealing with the new add-to-diary-alist
4458 (when org-disable-agenda-to-diary
4459 (condition-case nil
4460 (org-add-to-diary-list original-date "Org-mode dummy" "")
4461 (error
4462 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4464 (defun org-add-to-diary-list (&rest args)
4465 (if (fboundp 'diary-add-to-list)
4466 (apply 'diary-add-to-list args)
4467 (apply 'add-to-diary-list args)))
4469 (defvar org-diary-last-run-time nil)
4471 ;;;###autoload
4472 (defun org-diary (&rest args)
4473 "Return diary information from org-files.
4474 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4475 It accesses org files and extracts information from those files to be
4476 listed in the diary. The function accepts arguments specifying what
4477 items should be listed. For a list of arguments allowed here, see the
4478 variable `org-agenda-entry-types'.
4480 The call in the diary file should look like this:
4482 &%%(org-diary) ~/path/to/some/orgfile.org
4484 Use a separate line for each org file to check. Or, if you omit the file name,
4485 all files listed in `org-agenda-files' will be checked automatically:
4487 &%%(org-diary)
4489 If you don't give any arguments (as in the example above), the default
4490 arguments (:deadline :scheduled :timestamp :sexp) are used.
4491 So the example above may also be written as
4493 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4495 The function expects the lisp variables `entry' and `date' to be provided
4496 by the caller, because this is how the calendar works. Don't use this
4497 function from a program - use `org-agenda-get-day-entries' instead."
4498 (when (> (- (org-float-time)
4499 org-agenda-last-marker-time)
4501 (org-agenda-reset-markers))
4502 (org-compile-prefix-format 'agenda)
4503 (org-set-sorting-strategy 'agenda)
4504 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4505 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4506 (list entry)
4507 (org-agenda-files t)))
4508 (time (org-float-time))
4509 file rtn results)
4510 (when (or (not org-diary-last-run-time)
4511 (> (- time
4512 org-diary-last-run-time)
4514 (org-prepare-agenda-buffers files))
4515 (setq org-diary-last-run-time time)
4516 ;; If this is called during org-agenda, don't return any entries to
4517 ;; the calendar. Org Agenda will list these entries itself.
4518 (if org-disable-agenda-to-diary (setq files nil))
4519 (while (setq file (pop files))
4520 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4521 (setq results (append results rtn)))
4522 (if results
4523 (concat (org-finalize-agenda-entries results) "\n"))))
4525 ;;; Agenda entry finders
4527 (defun org-agenda-get-day-entries (file date &rest args)
4528 "Does the work for `org-diary' and `org-agenda'.
4529 FILE is the path to a file to be checked for entries. DATE is date like
4530 the one returned by `calendar-current-date'. ARGS are symbols indicating
4531 which kind of entries should be extracted. For details about these, see
4532 the documentation of `org-diary'."
4533 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4534 (let* ((org-startup-folded nil)
4535 (org-startup-align-all-tables nil)
4536 (buffer (if (file-exists-p file)
4537 (org-get-agenda-file-buffer file)
4538 (error "No such file %s" file)))
4539 arg results rtn deadline-results)
4540 (if (not buffer)
4541 ;; If file does not exist, make sure an error message ends up in diary
4542 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4543 (with-current-buffer buffer
4544 (unless (org-mode-p)
4545 (error "Agenda file %s is not in `org-mode'" file))
4546 (let ((case-fold-search nil))
4547 (save-excursion
4548 (save-restriction
4549 (if org-agenda-restrict
4550 (narrow-to-region org-agenda-restrict-begin
4551 org-agenda-restrict-end)
4552 (widen))
4553 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4554 (while (setq arg (pop args))
4555 (cond
4556 ((and (eq arg :todo)
4557 (equal date (calendar-gregorian-from-absolute
4558 (org-today))))
4559 (setq rtn (org-agenda-get-todos))
4560 (setq results (append results rtn)))
4561 ((eq arg :timestamp)
4562 (setq rtn (org-agenda-get-blocks))
4563 (setq results (append results rtn))
4564 (setq rtn (org-agenda-get-timestamps))
4565 (setq results (append results rtn)))
4566 ((eq arg :sexp)
4567 (setq rtn (org-agenda-get-sexps))
4568 (setq results (append results rtn)))
4569 ((eq arg :scheduled)
4570 (setq rtn (org-agenda-get-scheduled deadline-results))
4571 (setq results (append results rtn)))
4572 ((eq arg :closed)
4573 (setq rtn (org-agenda-get-progress))
4574 (setq results (append results rtn)))
4575 ((eq arg :deadline)
4576 (setq rtn (org-agenda-get-deadlines))
4577 (setq deadline-results (copy-sequence rtn))
4578 (setq results (append results rtn))))))))
4579 results))))
4581 (defun org-agenda-get-todos ()
4582 "Return the TODO information for agenda display."
4583 (let* ((props (list 'face nil
4584 'done-face 'org-agenda-done
4585 'org-not-done-regexp org-not-done-regexp
4586 'org-todo-regexp org-todo-regexp
4587 'org-complex-heading-regexp org-complex-heading-regexp
4588 'mouse-face 'highlight
4589 'help-echo
4590 (format "mouse-2 or RET jump to org file %s"
4591 (abbreviate-file-name buffer-file-name))))
4592 (regexp (concat "^\\*+[ \t]+\\("
4593 (if org-select-this-todo-keyword
4594 (if (equal org-select-this-todo-keyword "*")
4595 org-todo-regexp
4596 (concat "\\<\\("
4597 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4598 "\\)\\>"))
4599 org-not-done-regexp)
4600 "[^\n\r]*\\)"))
4601 marker priority category tags todo-state
4602 ee txt beg end)
4603 (goto-char (point-min))
4604 (while (re-search-forward regexp nil t)
4605 (catch :skip
4606 (save-match-data
4607 (beginning-of-line)
4608 (org-agenda-skip)
4609 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4610 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4611 (goto-char (1+ beg))
4612 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4613 (throw :skip nil)))
4614 (goto-char (match-beginning 1))
4615 (setq marker (org-agenda-new-marker (match-beginning 0))
4616 category (org-get-category)
4617 txt (match-string 1)
4618 tags (org-get-tags-at (point))
4619 txt (org-format-agenda-item "" txt category tags)
4620 priority (1+ (org-get-priority txt))
4621 todo-state (org-get-todo-state))
4622 (org-add-props txt props
4623 'org-marker marker 'org-hd-marker marker
4624 'priority priority 'org-category category
4625 'type "todo" 'todo-state todo-state)
4626 (push txt ee)
4627 (if org-agenda-todo-list-sublevels
4628 (goto-char (match-end 1))
4629 (org-end-of-subtree 'invisible))))
4630 (nreverse ee)))
4632 (defun org-agenda-todo-custom-ignore-p (time n)
4633 "Check whether timestamp is farther away then n number of days.
4634 This function is invoked if `org-agenda-todo-ignore-deadlines',
4635 `org-agenda-todo-ignore-scheduled' or
4636 `org-agenda-todo-ignore-timestamp' is set to an integer."
4637 (let ((days (org-days-to-time time)))
4638 (if (>= n 0)
4639 (>= days n)
4640 (<= days n))))
4642 ;;;###autoload
4643 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4644 (&optional end)
4645 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4646 (when (or org-agenda-todo-ignore-with-date
4647 org-agenda-todo-ignore-scheduled
4648 org-agenda-todo-ignore-deadlines
4649 org-agenda-todo-ignore-timestamp)
4650 (setq end (or end (save-excursion (outline-next-heading) (point))))
4651 (save-excursion
4652 (or (and org-agenda-todo-ignore-with-date
4653 (re-search-forward org-ts-regexp end t))
4654 (and org-agenda-todo-ignore-scheduled
4655 (re-search-forward org-scheduled-time-regexp end t)
4656 (cond
4657 ((eq org-agenda-todo-ignore-scheduled 'future)
4658 (> (org-days-to-time (match-string 1)) 0))
4659 ((eq org-agenda-todo-ignore-scheduled 'past)
4660 (<= (org-days-to-time (match-string 1)) 0))
4661 ((numberp org-agenda-todo-ignore-scheduled)
4662 (org-agenda-todo-custom-ignore-p
4663 (match-string 1) org-agenda-todo-ignore-scheduled))
4664 (t)))
4665 (and org-agenda-todo-ignore-deadlines
4666 (re-search-forward org-deadline-time-regexp end t)
4667 (cond
4668 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4669 ((eq org-agenda-todo-ignore-deadlines 'far)
4670 (not (org-deadline-close (match-string 1))))
4671 ((eq org-agenda-todo-ignore-deadlines 'future)
4672 (> (org-days-to-time (match-string 1)) 0))
4673 ((eq org-agenda-todo-ignore-deadlines 'past)
4674 (<= (org-days-to-time (match-string 1)) 0))
4675 ((numberp org-agenda-todo-ignore-deadlines)
4676 (org-agenda-todo-custom-ignore-p
4677 (match-string 1) org-agenda-todo-ignore-deadlines))
4678 (t (org-deadline-close (match-string 1)))))
4679 (and org-agenda-todo-ignore-timestamp
4680 (let ((buffer (current-buffer))
4681 (regexp
4682 (concat
4683 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4684 (start (point)))
4685 ;; Copy current buffer into a temporary one
4686 (with-temp-buffer
4687 (insert-buffer-substring buffer start end)
4688 (goto-char (point-min))
4689 ;; Delete SCHEDULED and DEADLINE items
4690 (while (re-search-forward regexp end t)
4691 (delete-region (match-beginning 0) (match-end 0)))
4692 (goto-char (point-min))
4693 ;; No search for timestamp left
4694 (when (re-search-forward org-ts-regexp nil t)
4695 (cond
4696 ((eq org-agenda-todo-ignore-timestamp 'future)
4697 (> (org-days-to-time (match-string 1)) 0))
4698 ((eq org-agenda-todo-ignore-timestamp 'past)
4699 (<= (org-days-to-time (match-string 1)) 0))
4700 ((numberp org-agenda-todo-ignore-timestamp)
4701 (org-agenda-todo-custom-ignore-p
4702 (match-string 1) org-agenda-todo-ignore-timestamp))
4703 (t))))))))))
4705 (defconst org-agenda-no-heading-message
4706 "No heading for this item in buffer or region.")
4708 (defun org-agenda-get-timestamps ()
4709 "Return the date stamp information for agenda display."
4710 (let* ((props (list 'face nil
4711 'org-not-done-regexp org-not-done-regexp
4712 'org-todo-regexp org-todo-regexp
4713 'org-complex-heading-regexp org-complex-heading-regexp
4714 'mouse-face 'highlight
4715 'help-echo
4716 (format "mouse-2 or RET jump to org file %s"
4717 (abbreviate-file-name buffer-file-name))))
4718 (d1 (calendar-absolute-from-gregorian date))
4719 (remove-re
4720 (concat
4721 (regexp-quote
4722 (format-time-string
4723 "<%Y-%m-%d"
4724 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4725 ".*?>"))
4726 (regexp
4727 (concat
4728 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4729 (regexp-quote
4730 (substring
4731 (format-time-string
4732 (car org-time-stamp-formats)
4733 (apply 'encode-time ; DATE bound by calendar
4734 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4735 1 11))
4736 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4737 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4738 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4739 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4740 todo-state end-of-match show-all)
4741 (goto-char (point-min))
4742 (while (setq end-of-match (re-search-forward regexp nil t))
4743 (setq b0 (match-beginning 0)
4744 b3 (match-beginning 3) e3 (match-end 3)
4745 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4746 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4747 (member todo-state
4748 org-agenda-repeating-timestamp-show-all)))
4749 (catch :skip
4750 (and (org-at-date-range-p) (throw :skip nil))
4751 (org-agenda-skip)
4752 (if (and (match-end 1)
4753 (not (= d1 (org-time-string-to-absolute
4754 (match-string 1) d1 nil show-all))))
4755 (throw :skip nil))
4756 (if (and e3
4757 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4758 (throw :skip nil))
4759 (setq tmp (buffer-substring (max (point-min)
4760 (- b0 org-ds-keyword-length))
4762 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4763 inactivep (= (char-after b0) ?\[)
4764 deadlinep (string-match org-deadline-regexp tmp)
4765 scheduledp (string-match org-scheduled-regexp tmp)
4766 closedp (and org-agenda-include-inactive-timestamps
4767 (string-match org-closed-string tmp))
4768 clockp (and org-agenda-include-inactive-timestamps
4769 (or (string-match org-clock-string tmp)
4770 (string-match "]-+\\'" tmp)))
4771 donep (member todo-state org-done-keywords))
4772 (if (or scheduledp deadlinep closedp clockp
4773 (and donep org-agenda-skip-timestamp-if-done))
4774 (throw :skip t))
4775 (if (string-match ">" timestr)
4776 ;; substring should only run to end of time stamp
4777 (setq timestr (substring timestr 0 (match-end 0))))
4778 (setq marker (org-agenda-new-marker b0)
4779 category (org-get-category b0))
4780 (save-excursion
4781 (if (not (re-search-backward org-outline-regexp-bol nil t))
4782 (setq txt org-agenda-no-heading-message)
4783 (goto-char (match-beginning 0))
4784 (setq hdmarker (org-agenda-new-marker)
4785 tags (org-get-tags-at))
4786 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4787 (setq head (or (match-string 1) ""))
4788 (setq txt (org-format-agenda-item
4789 (if inactivep org-agenda-inactive-leader nil)
4790 head category tags timestr
4791 remove-re)))
4792 (setq priority (org-get-priority txt))
4793 (org-add-props txt props
4794 'org-marker marker 'org-hd-marker hdmarker)
4795 (org-add-props txt nil 'priority priority
4796 'org-category category 'date date
4797 'todo-state todo-state
4798 'type "timestamp")
4799 (push txt ee))
4800 (if org-agenda-skip-additional-timestamps-same-entry
4801 (outline-next-heading)
4802 (goto-char end-of-match))))
4803 (nreverse ee)))
4805 (defun org-agenda-get-sexps ()
4806 "Return the sexp information for agenda display."
4807 (require 'diary-lib)
4808 (let* ((props (list 'mouse-face 'highlight
4809 'help-echo
4810 (format "mouse-2 or RET jump to org file %s"
4811 (abbreviate-file-name buffer-file-name))))
4812 (regexp "^&?%%(")
4813 marker category ee txt tags entry result beg b sexp sexp-entry
4814 todo-state)
4815 (goto-char (point-min))
4816 (while (re-search-forward regexp nil t)
4817 (catch :skip
4818 (org-agenda-skip)
4819 (setq beg (match-beginning 0))
4820 (goto-char (1- (match-end 0)))
4821 (setq b (point))
4822 (forward-sexp 1)
4823 (setq sexp (buffer-substring b (point)))
4824 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4825 (org-trim (match-string 1))
4826 ""))
4827 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4828 (when result
4829 (setq marker (org-agenda-new-marker beg)
4830 category (org-get-category beg)
4831 todo-state (org-get-todo-state))
4833 (dolist (r (if (stringp result)
4834 (list result)
4835 result)) ;; we expect a list here
4836 (if (string-match "\\S-" r)
4837 (setq txt r)
4838 (setq txt "SEXP entry returned empty string"))
4840 (setq txt (org-format-agenda-item
4841 "" txt category tags 'time))
4842 (org-add-props txt props 'org-marker marker)
4843 (org-add-props txt nil
4844 'org-category category 'date date 'todo-state todo-state
4845 'type "sexp")
4846 (push txt ee)))))
4847 (nreverse ee)))
4849 ;; Calendar sanity: define some functions that are independent of
4850 ;; `calendar-date-style'.
4851 ;; Normally I would like to use ISO format when calling the diary functions,
4852 ;; but to make sure we still have Emacs 22 compatibility we bind
4853 ;; also `european-calendar-style' and use european format
4854 (defun org-anniversary (year month day &optional mark)
4855 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4856 (org-no-warnings
4857 (let ((calendar-date-style 'european) (european-calendar-style t))
4858 (diary-anniversary day month year mark))))
4859 (defun org-cyclic (N year month day &optional mark)
4860 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4861 (org-no-warnings
4862 (let ((calendar-date-style 'european) (european-calendar-style t))
4863 (diary-cyclic N day month year mark))))
4864 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4865 "Like `diary-block', but with fixed (ISO) order of arguments."
4866 (org-no-warnings
4867 (let ((calendar-date-style 'european) (european-calendar-style t))
4868 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4869 (defun org-date (year month day &optional mark)
4870 "Like `diary-date', but with fixed (ISO) order of arguments."
4871 (org-no-warnings
4872 (let ((calendar-date-style 'european) (european-calendar-style t))
4873 (diary-date day month year mark))))
4874 (defalias 'org-float 'diary-float)
4876 ;; Define the` org-class' function
4877 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4878 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4879 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4880 is any number of ISO weeks in the block period for which the item should
4881 be skipped."
4882 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4883 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4884 (d (calendar-absolute-from-gregorian date)))
4885 (and
4886 (<= date1 d)
4887 (<= d date2)
4888 (= (calendar-day-of-week date) dayname)
4889 (or (not skip-weeks)
4890 (progn
4891 (require 'cal-iso)
4892 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4893 entry)))
4895 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4896 "Like `org-class', but honor `calendar-date-style'.
4897 The order of the first 2 times 3 arguments depends on the variable
4898 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4899 So for American calendars, give this as MONTH DAY YEAR, for European as
4900 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4901 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4902 is any number of ISO weeks in the block period for which the item should
4903 be skipped.
4905 This function is here only for backward compatibility and it is deprecated,
4906 please use `org-class' instead."
4907 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4908 (date2 (org-order-calendar-date-args m2 d2 y2)))
4909 (org-class
4910 (nth 2 date1) (car date1) (nth 1 date1)
4911 (nth 2 date2) (car date2) (nth 1 date2)
4912 dayname skip-weeks)))
4914 (defalias 'org-get-closed 'org-agenda-get-progress)
4915 (defun org-agenda-get-progress ()
4916 "Return the logged TODO entries for agenda display."
4917 (let* ((props (list 'mouse-face 'highlight
4918 'org-not-done-regexp org-not-done-regexp
4919 'org-todo-regexp org-todo-regexp
4920 'org-complex-heading-regexp org-complex-heading-regexp
4921 'help-echo
4922 (format "mouse-2 or RET jump to org file %s"
4923 (abbreviate-file-name buffer-file-name))))
4924 (items (if (consp org-agenda-show-log)
4925 org-agenda-show-log
4926 (if (eq org-agenda-show-log 'clockcheck)
4927 '(clock)
4928 org-agenda-log-mode-items)))
4929 (parts
4930 (delq nil
4931 (list
4932 (if (memq 'closed items) (concat "\\<" org-closed-string))
4933 (if (memq 'clock items) (concat "\\<" org-clock-string))
4934 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4935 (parts-re (if parts (mapconcat 'identity parts "\\|")
4936 (error "`org-agenda-log-mode-items' is empty")))
4937 (regexp (concat
4938 "\\(" parts-re "\\)"
4939 " *\\["
4940 (regexp-quote
4941 (substring
4942 (format-time-string
4943 (car org-time-stamp-formats)
4944 (apply 'encode-time ; DATE bound by calendar
4945 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4946 1 11))))
4947 (org-agenda-search-headline-for-time nil)
4948 marker hdmarker priority category tags closedp statep clockp state
4949 ee txt extra timestr rest clocked)
4950 (goto-char (point-min))
4951 (while (re-search-forward regexp nil t)
4952 (catch :skip
4953 (org-agenda-skip)
4954 (setq marker (org-agenda-new-marker (match-beginning 0))
4955 closedp (equal (match-string 1) org-closed-string)
4956 statep (equal (string-to-char (match-string 1)) ?-)
4957 clockp (not (or closedp statep))
4958 state (and statep (match-string 2))
4959 category (org-get-category (match-beginning 0))
4960 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4962 (when (string-match "\\]" timestr)
4963 ;; substring should only run to end of time stamp
4964 (setq rest (substring timestr (match-end 0))
4965 timestr (substring timestr 0 (match-end 0)))
4966 (if (and (not closedp) (not statep)
4967 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4968 (progn (setq timestr (concat (substring timestr 0 -1)
4969 "-" (match-string 1 rest) "]"))
4970 (setq clocked (match-string 2 rest)))
4971 (setq clocked "-")))
4972 (save-excursion
4973 (setq extra
4974 (cond
4975 ((not org-agenda-log-mode-add-notes) nil)
4976 (statep
4977 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4978 (match-string 1)))
4979 (clockp
4980 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4981 (match-string 1)))))
4982 (if (not (re-search-backward org-outline-regexp-bol nil t))
4983 (setq txt org-agenda-no-heading-message)
4984 (goto-char (match-beginning 0))
4985 (setq hdmarker (org-agenda-new-marker)
4986 tags (org-get-tags-at))
4987 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4988 (setq txt (match-string 1))
4989 (when extra
4990 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4991 (setq txt (concat (substring txt 0 (match-beginning 1))
4992 " - " extra " " (match-string 2 txt)))
4993 (setq txt (concat txt " - " extra))))
4994 (setq txt (org-format-agenda-item
4995 (cond
4996 (closedp "Closed: ")
4997 (statep (concat "State: (" state ")"))
4998 (t (concat "Clocked: (" clocked ")")))
4999 txt category tags timestr)))
5000 (setq priority 100000)
5001 (org-add-props txt props
5002 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5003 'priority priority 'org-category category
5004 'type "closed" 'date date
5005 'undone-face 'org-warning 'done-face 'org-agenda-done)
5006 (push txt ee))
5007 (goto-char (point-at-eol))))
5008 (nreverse ee)))
5010 (defun org-agenda-show-clocking-issues ()
5011 "Add overlays, showing issues with clocking.
5012 See also the user option `org-agenda-clock-consistency-checks'."
5013 (interactive)
5014 (let* ((pl org-agenda-clock-consistency-checks)
5015 (re (concat "^[ \t]*"
5016 org-clock-string
5017 "[ \t]+"
5018 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5019 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5020 (tlstart 0.)
5021 (tlend 0.)
5022 (maxtime (org-hh:mm-string-to-minutes
5023 (or (plist-get pl :max-duration) "24:00")))
5024 (mintime (org-hh:mm-string-to-minutes
5025 (or (plist-get pl :min-duration) 0)))
5026 (maxgap (org-hh:mm-string-to-minutes
5027 ;; default 30:00 means never complain
5028 (or (plist-get pl :max-gap) "30:00")))
5029 (gapok (mapcar 'org-hh:mm-string-to-minutes
5030 (plist-get pl :gap-ok-around)))
5031 (def-face (or (plist-get pl :default-face)
5032 '((:background "DarkRed") (:foreground "white"))))
5033 issue face m te ts dt ov)
5034 (goto-char (point-min))
5035 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5036 (setq issue nil face def-face)
5037 (catch 'next
5038 (setq m (org-get-at-bol 'org-marker)
5039 te nil ts nil)
5040 (unless (and m (markerp m))
5041 (setq issue "No valid clock line") (throw 'next t))
5042 (org-with-point-at m
5043 (save-excursion
5044 (goto-char (point-at-bol))
5045 (unless (looking-at re)
5046 (error "No valid Clock line")
5047 (throw 'next t))
5048 (unless (match-end 3)
5049 (setq issue "No end time"
5050 face (or (plist-get pl :no-end-time-face) face))
5051 (throw 'next t))
5052 (setq ts (match-string 1)
5053 te (match-string 3)
5054 ts (org-float-time
5055 (apply 'encode-time (org-parse-time-string ts)))
5056 te (org-float-time
5057 (apply 'encode-time (org-parse-time-string te)))
5058 dt (- te ts))))
5059 (cond
5060 ((> dt (* 60 maxtime))
5061 ;; a very long clocking chunk
5062 (setq issue (format "Clocking interval is very long: %s"
5063 (org-minutes-to-hh:mm-string
5064 (floor (/ (float dt) 60.))))
5065 face (or (plist-get pl :long-face) face)))
5066 ((< dt (* 60 mintime))
5067 ;; a very short clocking chunk
5068 (setq issue (format "Clocking interval is very short: %s"
5069 (org-minutes-to-hh:mm-string
5070 (floor (/ (float dt) 60.))))
5071 face (or (plist-get pl :short-face) face)))
5072 ((and (> tlend 0) (< ts tlend))
5073 ;; Two clock entries are overlapping
5074 (setq issue (format "Clocking overlap: %d minutes"
5075 (/ (- tlend ts) 60))
5076 face (or (plist-get pl :overlap-face) face)))
5077 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5078 ;; There is a gap, lets see if we need to report it
5079 (unless (org-agenda-check-clock-gap tlend ts gapok)
5080 (setq issue (format "Clocking gap: %d minutes"
5081 (/ (- ts tlend) 60))
5082 face (or (plist-get pl :gap-face) face))))
5083 (t nil)))
5084 (setq tlend (or te tlend) tlstart (or ts tlstart))
5085 (when issue
5086 ;; OK, there was some issue, add an overlay to show the issue
5087 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5088 (overlay-put ov 'before-string
5089 (concat
5090 (org-add-props
5091 (format "%-43s" (concat " " issue))
5093 'face face)
5094 "\n"))
5095 (overlay-put ov 'evaporate t)))))
5097 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5098 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5099 (catch 'exit
5100 (unless ok-list
5101 ;; there are no OK times for gaps...
5102 (throw 'exit nil))
5103 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5104 ;; This is more than 24 hours, so it is OK.
5105 ;; because we have at least one OK time, that must be in the
5106 ;; 24 hour interval.
5107 (throw 'exit t))
5108 ;; We have a shorter gap.
5109 ;; Now we have to get the minute of the day when these times are
5110 (let* ((t1dec (decode-time (seconds-to-time t1)))
5111 (t2dec (decode-time (seconds-to-time t2)))
5112 ;; compute the minute on the day
5113 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5114 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5115 (when (< min2 min1)
5116 ;; if min2 is smaller than min1, this means it is on the next day.
5117 ;; Wrap it to after midnight.
5118 (setq min2 (+ min2 1440)))
5119 ;; Now check if any of the OK times is in the gap
5120 (mapc (lambda (x)
5121 ;; Wrap the time to after midnight if necessary
5122 (if (< x min1) (setq x (+ x 1440)))
5123 ;; Check if in interval
5124 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5125 ok-list)
5126 ;; Nope, this gap is not OK
5127 nil)))
5129 (defun org-agenda-get-deadlines ()
5130 "Return the deadline information for agenda display."
5131 (let* ((props (list 'mouse-face 'highlight
5132 'org-not-done-regexp org-not-done-regexp
5133 'org-todo-regexp org-todo-regexp
5134 'org-complex-heading-regexp org-complex-heading-regexp
5135 'help-echo
5136 (format "mouse-2 or RET jump to org file %s"
5137 (abbreviate-file-name buffer-file-name))))
5138 (regexp org-deadline-time-regexp)
5139 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5140 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5141 d2 diff dfrac wdays pos pos1 category tags
5142 suppress-prewarning
5143 ee txt head face s todo-state show-all upcomingp donep timestr)
5144 (goto-char (point-min))
5145 (while (re-search-forward regexp nil t)
5146 (setq suppress-prewarning nil)
5147 (catch :skip
5148 (org-agenda-skip)
5149 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5150 (save-match-data
5151 (string-match org-scheduled-time-regexp
5152 (buffer-substring (point-at-bol)
5153 (point-at-eol)))))
5154 (setq suppress-prewarning
5155 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5156 org-agenda-skip-deadline-prewarning-if-scheduled
5157 0)))
5158 (setq s (match-string 1)
5159 txt nil
5160 pos (1- (match-beginning 1))
5161 todo-state (save-match-data (org-get-todo-state))
5162 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5163 (member todo-state
5164 org-agenda-repeating-timestamp-show-all))
5165 d2 (org-time-string-to-absolute
5166 (match-string 1) d1 'past show-all)
5167 diff (- d2 d1)
5168 wdays (if suppress-prewarning
5169 (let ((org-deadline-warning-days suppress-prewarning))
5170 (org-get-wdays s))
5171 (org-get-wdays s))
5172 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
5173 upcomingp (and todayp (> diff 0)))
5174 ;; When to show a deadline in the calendar:
5175 ;; If the expiration is within wdays warning time.
5176 ;; Past-due deadlines are only shown on the current date
5177 (if (and (or (and (<= diff wdays)
5178 (and todayp (not org-agenda-only-exact-dates)))
5179 (= diff 0)))
5180 (save-excursion
5181 ;; (setq todo-state (org-get-todo-state))
5182 (setq donep (member todo-state org-done-keywords))
5183 (if (and donep
5184 (or org-agenda-skip-deadline-if-done
5185 (not (= diff 0))))
5186 (setq txt nil)
5187 (setq category (org-get-category))
5188 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5189 (setq txt org-agenda-no-heading-message)
5190 (goto-char (match-end 0))
5191 (setq pos1 (match-beginning 0))
5192 (setq tags (org-get-tags-at pos1))
5193 (setq head (buffer-substring-no-properties
5194 (point)
5195 (progn (skip-chars-forward "^\r\n")
5196 (point))))
5197 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5198 (setq timestr
5199 (concat (substring s (match-beginning 1)) " "))
5200 (setq timestr 'time))
5201 (setq txt (org-format-agenda-item
5202 (if (= diff 0)
5203 (car org-agenda-deadline-leaders)
5204 (if (functionp
5205 (nth 1 org-agenda-deadline-leaders))
5206 (funcall
5207 (nth 1 org-agenda-deadline-leaders)
5208 diff date)
5209 (format (nth 1 org-agenda-deadline-leaders)
5210 diff)))
5211 head category tags
5212 (if (not (= diff 0)) nil timestr)))))
5213 (when txt
5214 (setq face (org-agenda-deadline-face dfrac wdays))
5215 (org-add-props txt props
5216 'org-marker (org-agenda-new-marker pos)
5217 'org-hd-marker (org-agenda-new-marker pos1)
5218 'priority (+ (- diff)
5219 (org-get-priority txt))
5220 'org-category category
5221 'todo-state todo-state
5222 'type (if upcomingp "upcoming-deadline" "deadline")
5223 'date (if upcomingp date d2)
5224 'face (if donep 'org-agenda-done face)
5225 'undone-face face 'done-face 'org-agenda-done)
5226 (push txt ee))))))
5227 (nreverse ee)))
5229 (defun org-agenda-deadline-face (fraction &optional wdays)
5230 "Return the face to displaying a deadline item.
5231 FRACTION is what fraction of the head-warning time has passed."
5232 (if (equal wdays 0) (setq fraction 1.))
5233 (let ((faces org-agenda-deadline-faces) f)
5234 (catch 'exit
5235 (while (setq f (pop faces))
5236 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5238 (defun org-agenda-get-scheduled (&optional deadline-results)
5239 "Return the scheduled information for agenda display."
5240 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5241 'org-todo-regexp org-todo-regexp
5242 'org-complex-heading-regexp org-complex-heading-regexp
5243 'done-face 'org-agenda-done
5244 'mouse-face 'highlight
5245 'help-echo
5246 (format "mouse-2 or RET jump to org file %s"
5247 (abbreviate-file-name buffer-file-name))))
5248 (regexp org-scheduled-time-regexp)
5249 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5250 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5252 (deadline-position-alist
5253 (mapcar (lambda (a) (and (setq mm (get-text-property
5254 0 'org-hd-marker a))
5255 (cons (marker-position mm) a)))
5256 deadline-results))
5257 d2 diff pos pos1 category tags donep
5258 ee txt head pastschedp todo-state face timestr s habitp show-all)
5259 (goto-char (point-min))
5260 (while (re-search-forward regexp nil t)
5261 (catch :skip
5262 (org-agenda-skip)
5263 (setq s (match-string 1)
5264 txt nil
5265 pos (1- (match-beginning 1))
5266 todo-state (save-match-data (org-get-todo-state))
5267 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5268 (member todo-state
5269 org-agenda-repeating-timestamp-show-all))
5270 d2 (org-time-string-to-absolute
5271 (match-string 1) d1 'past show-all)
5272 diff (- d2 d1))
5273 (setq pastschedp (and todayp (< diff 0)))
5274 ;; When to show a scheduled item in the calendar:
5275 ;; If it is on or past the date.
5276 (when (or (and (< diff 0)
5277 (< (abs diff) org-scheduled-past-days)
5278 (and todayp (not org-agenda-only-exact-dates)))
5279 (= diff 0))
5280 (save-excursion
5281 (setq donep (member todo-state org-done-keywords))
5282 (if (and donep
5283 (or org-agenda-skip-scheduled-if-done
5284 (not (= diff 0))
5285 (and (functionp 'org-is-habit-p)
5286 (org-is-habit-p))))
5287 (setq txt nil)
5288 (setq habitp (and (functionp 'org-is-habit-p)
5289 (org-is-habit-p)))
5290 (setq category (org-get-category))
5291 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5292 (setq txt org-agenda-no-heading-message)
5293 (goto-char (match-end 0))
5294 (setq pos1 (match-beginning 0))
5295 (if habitp
5296 (if (or (not org-habit-show-habits)
5297 (and (not todayp)
5298 org-habit-show-habits-only-for-today))
5299 (throw :skip nil))
5300 (if (and
5301 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5302 (and org-agenda-skip-scheduled-if-deadline-is-shown
5303 pastschedp))
5304 (setq mm (assoc pos1 deadline-position-alist)))
5305 (throw :skip nil)))
5306 (setq tags (org-get-tags-at))
5307 (setq head (buffer-substring-no-properties
5308 (point)
5309 (progn (skip-chars-forward "^\r\n") (point))))
5310 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5311 (setq timestr
5312 (concat (substring s (match-beginning 1)) " "))
5313 (setq timestr 'time))
5314 (setq txt (org-format-agenda-item
5315 (if (= diff 0)
5316 (car org-agenda-scheduled-leaders)
5317 (format (nth 1 org-agenda-scheduled-leaders)
5318 (- 1 diff)))
5319 head category tags
5320 (if (not (= diff 0)) nil timestr)
5321 nil habitp))))
5322 (when txt
5323 (setq face
5324 (cond
5325 ((and (not habitp) pastschedp)
5326 'org-scheduled-previously)
5327 (todayp 'org-scheduled-today)
5328 (t 'org-scheduled))
5329 habitp (and habitp (org-habit-parse-todo)))
5330 (org-add-props txt props
5331 'undone-face face
5332 'face (if donep 'org-agenda-done face)
5333 'org-marker (org-agenda-new-marker pos)
5334 'org-hd-marker (org-agenda-new-marker pos1)
5335 'type (if pastschedp "past-scheduled" "scheduled")
5336 'date (if pastschedp d2 date)
5337 'priority (if habitp
5338 (org-habit-get-priority habitp)
5339 (+ 94 (- 5 diff) (org-get-priority txt)))
5340 'org-category category
5341 'org-habit-p habitp
5342 'todo-state todo-state)
5343 (push txt ee))))))
5344 (nreverse ee)))
5346 (defun org-agenda-get-blocks ()
5347 "Return the date-range information for agenda display."
5348 (let* ((props (list 'face nil
5349 'org-not-done-regexp org-not-done-regexp
5350 'org-todo-regexp org-todo-regexp
5351 'org-complex-heading-regexp org-complex-heading-regexp
5352 'mouse-face 'highlight
5353 'help-echo
5354 (format "mouse-2 or RET jump to org file %s"
5355 (abbreviate-file-name buffer-file-name))))
5356 (regexp org-tr-regexp)
5357 (d0 (calendar-absolute-from-gregorian date))
5358 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5359 head donep)
5360 (goto-char (point-min))
5361 (while (re-search-forward regexp nil t)
5362 (catch :skip
5363 (org-agenda-skip)
5364 (setq pos (point))
5365 (let ((start-time (match-string 1))
5366 (end-time (match-string 2)))
5367 (setq s1 (match-string 1)
5368 s2 (match-string 2)
5369 d1 (time-to-days (org-time-string-to-time s1))
5370 d2 (time-to-days (org-time-string-to-time s2)))
5371 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5372 ;; Only allow days between the limits, because the normal
5373 ;; date stamps will catch the limits.
5374 (save-excursion
5375 (setq todo-state (org-get-todo-state))
5376 (setq donep (member todo-state org-done-keywords))
5377 (if (and donep org-agenda-skip-timestamp-if-done)
5378 (throw :skip t))
5379 (setq marker (org-agenda-new-marker (point)))
5380 (setq category (org-get-category))
5381 (if (not (re-search-backward org-outline-regexp-bol nil t))
5382 (setq txt org-agenda-no-heading-message)
5383 (goto-char (match-beginning 0))
5384 (setq hdmarker (org-agenda-new-marker (point)))
5385 (setq tags (org-get-tags-at))
5386 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5387 (setq head (match-string 1))
5388 (let ((remove-re
5389 (if org-agenda-remove-timeranges-from-blocks
5390 (concat
5391 "<" (regexp-quote s1) ".*?>"
5392 "--"
5393 "<" (regexp-quote s2) ".*?>")
5394 nil)))
5395 (setq txt (org-format-agenda-item
5396 (format
5397 (nth (if (= d1 d2) 0 1)
5398 org-agenda-timerange-leaders)
5399 (1+ (- d0 d1)) (1+ (- d2 d1)))
5400 head category tags
5401 (cond ((= d1 d0)
5402 (concat "<" start-time ">"))
5403 ((= d2 d0)
5404 (concat "<" end-time ">"))
5405 (t nil))
5406 remove-re))))
5407 (org-add-props txt props
5408 'org-marker marker 'org-hd-marker hdmarker
5409 'type "block" 'date date
5410 'todo-state todo-state
5411 'priority (org-get-priority txt) 'org-category category)
5412 (push txt ee))))
5413 (goto-char pos)))
5414 ;; Sort the entries by expiration date.
5415 (nreverse ee)))
5417 ;;; Agenda presentation and sorting
5419 (defvar org-prefix-has-time nil
5420 "A flag, set by `org-compile-prefix-format'.
5421 The flag is set if the currently compiled format contains a `%t'.")
5422 (defvar org-prefix-has-tag nil
5423 "A flag, set by `org-compile-prefix-format'.
5424 The flag is set if the currently compiled format contains a `%T'.")
5425 (defvar org-prefix-has-effort nil
5426 "A flag, set by `org-compile-prefix-format'.
5427 The flag is set if the currently compiled format contains a `%e'.")
5428 (defvar org-prefix-category-length nil
5429 "Used by `org-compile-prefix-format' to remember the category field width.")
5430 (defvar org-prefix-category-max-length nil
5431 "Used by `org-compile-prefix-format' to remember the category field width.")
5433 (defun org-agenda-get-category-icon (category)
5434 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5435 (dolist (entry org-agenda-category-icon-alist)
5436 (when (org-string-match-p (car entry) category)
5437 (if (listp (cadr entry))
5438 (return (cadr entry))
5439 (return (apply 'create-image (cdr entry)))))))
5441 (defun org-format-agenda-item (extra txt &optional category tags dotime
5442 remove-re habitp)
5443 "Format TXT to be inserted into the agenda buffer.
5444 In particular, it adds the prefix and corresponding text properties. EXTRA
5445 must be a string and replaces the `%s' specifier in the prefix format.
5446 CATEGORY (string, symbol or nil) may be used to overrule the default
5447 category taken from local variable or file name. It will replace the `%c'
5448 specifier in the format. DOTIME, when non-nil, indicates that a
5449 time-of-day should be extracted from TXT for sorting of this entry, and for
5450 the `%t' specifier in the format. When DOTIME is a string, this string is
5451 searched for a time before TXT is. TAGS can be the tags of the headline.
5452 Any match of REMOVE-RE will be removed from TXT."
5453 (save-match-data
5454 ;; Diary entries sometimes have extra whitespace at the beginning
5455 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5457 ;; Fix the tags part in txt
5458 (setq txt (org-agenda-fix-displayed-tags
5459 txt tags
5460 org-agenda-show-inherited-tags
5461 org-agenda-hide-tags-regexp))
5462 (let* ((category (or category
5463 (if (stringp org-category)
5464 org-category
5465 (and org-category (symbol-name org-category)))
5466 (if buffer-file-name
5467 (file-name-sans-extension
5468 (file-name-nondirectory buffer-file-name))
5469 "")))
5470 (category-icon (org-agenda-get-category-icon category))
5471 (category-icon (if category-icon
5472 (propertize " " 'display category-icon)
5473 ""))
5474 ;; time, tag, effort are needed for the eval of the prefix format
5475 (tag (if tags (nth (1- (length tags)) tags) ""))
5476 time effort neffort
5477 (ts (if dotime (concat
5478 (if (stringp dotime) dotime "")
5479 (and org-agenda-search-headline-for-time txt))))
5480 (time-of-day (and dotime (org-get-time-of-day ts)))
5481 stamp plain s0 s1 s2 rtn srp l
5482 duration thecategory)
5483 (and (org-mode-p) buffer-file-name
5484 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5485 (when (and dotime time-of-day)
5486 ;; Extract starting and ending time and move them to prefix
5487 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5488 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5489 (setq s0 (match-string 0 ts)
5490 srp (and stamp (match-end 3))
5491 s1 (match-string (if plain 1 2) ts)
5492 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5494 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5495 ;; them, we might want to remove them there to avoid duplication.
5496 ;; The user can turn this off with a variable.
5497 (if (and org-prefix-has-time
5498 org-agenda-remove-times-when-in-prefix (or stamp plain)
5499 (string-match (concat (regexp-quote s0) " *") txt)
5500 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5501 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5502 (= (match-beginning 0) 0)
5504 (setq txt (replace-match "" nil nil txt))))
5505 ;; Normalize the time(s) to 24 hour
5506 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5507 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5509 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5510 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5511 (setq s2
5512 (org-minutes-to-hh:mm-string
5513 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5515 ;; Compute the duration
5516 (when s2
5517 (setq duration (- (org-hh:mm-string-to-minutes s2)
5518 (org-hh:mm-string-to-minutes s1)))))
5520 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5521 txt)
5522 ;; Tags are in the string
5523 (if (or (eq org-agenda-remove-tags t)
5524 (and org-agenda-remove-tags
5525 org-prefix-has-tag))
5526 (setq txt (replace-match "" t t txt))
5527 (setq txt (replace-match
5528 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5529 (match-string 2 txt))
5530 t t txt))))
5531 (when (org-mode-p)
5532 (setq effort
5533 (condition-case nil
5534 (org-get-effort
5535 (or (get-text-property 0 'org-hd-marker txt)
5536 (get-text-property 0 'org-marker txt)))
5537 (error nil)))
5538 (when effort
5539 (setq neffort (org-duration-string-to-minutes effort)
5540 effort (setq effort (concat "[" effort "]")))))
5541 ;; prevent erroring out with %e format when there is no effort
5542 (or effort (setq effort ""))
5544 (when remove-re
5545 (while (string-match remove-re txt)
5546 (setq txt (replace-match "" t t txt))))
5548 ;; Set org-heading property on `txt' to mark the start of the
5549 ;; heading.
5550 (add-text-properties 0 (length txt) '(org-heading t) txt)
5552 ;; Prepare the variables needed in the eval of the compiled format
5553 (setq time (cond (s2 (concat
5554 (org-agenda-time-of-day-to-ampm-maybe s1)
5555 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5556 (if org-agenda-timegrid-use-ampm " ")))
5557 (s1 (concat
5558 (org-agenda-time-of-day-to-ampm-maybe s1)
5559 (if org-agenda-timegrid-use-ampm
5560 "........ "
5561 "......")))
5562 (t ""))
5563 extra (or (and (not habitp) extra) "")
5564 category (if (symbolp category) (symbol-name category) category)
5565 thecategory (copy-sequence category))
5566 (if (string-match org-bracket-link-regexp category)
5567 (progn
5568 (setq l (if (match-end 3)
5569 (- (match-end 3) (match-beginning 3))
5570 (- (match-end 1) (match-beginning 1))))
5571 (when (< l (or org-prefix-category-length 0))
5572 (setq category (copy-sequence category))
5573 (org-add-props category nil
5574 'extra-space (make-string
5575 (- org-prefix-category-length l 1) ?\ ))))
5576 (if (and org-prefix-category-max-length
5577 (>= (length category) org-prefix-category-max-length))
5578 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5579 ;; Evaluate the compiled format
5580 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5582 ;; And finally add the text properties
5583 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5584 (org-add-props rtn nil
5585 'org-category (if thecategory (downcase thecategory) category)
5586 'tags (mapcar 'org-downcase-keep-props tags)
5587 'org-highest-priority org-highest-priority
5588 'org-lowest-priority org-lowest-priority
5589 'time-of-day time-of-day
5590 'duration duration
5591 'effort effort
5592 'effort-minutes neffort
5593 'txt txt
5594 'time time
5595 'extra extra
5596 'format org-prefix-format-compiled
5597 'dotime dotime))))
5599 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5600 "Remove tags string from TXT, and add a modified list of tags.
5601 The modified list may contain inherited tags, and tags matched by
5602 `org-agenda-hide-tags-regexp' will be removed."
5603 (when (or add-inherited hide-re)
5604 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5605 (setq txt (substring txt 0 (match-beginning 0))))
5606 (setq tags
5607 (delq nil
5608 (mapcar (lambda (tg)
5609 (if (or (and hide-re (string-match hide-re tg))
5610 (and (not add-inherited)
5611 (get-text-property 0 'inherited tg)))
5613 tg))
5614 tags)))
5615 (when tags
5616 (let ((have-i (get-text-property 0 'inherited (car tags)))
5618 (setq txt (concat txt " :"
5619 (mapconcat
5620 (lambda (x)
5621 (setq i (get-text-property 0 'inherited x))
5622 (if (and have-i (not i))
5623 (progn
5624 (setq have-i nil)
5625 (concat ":" x))
5627 tags ":")
5628 (if have-i "::" ":"))))))
5629 txt)
5631 (defun org-downcase-keep-props (s)
5632 (let ((props (text-properties-at 0 s)))
5633 (setq s (downcase s))
5634 (add-text-properties 0 (length s) props s)
5637 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5638 (defvar org-agenda-sorting-strategy-selected nil)
5640 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5641 (catch 'exit
5642 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5643 ((and todayp (member 'today (car org-agenda-time-grid))))
5644 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5645 ((member 'weekly (car org-agenda-time-grid)))
5646 (t (throw 'exit list)))
5647 (let* ((have (delq nil (mapcar
5648 (lambda (x) (get-text-property 1 'time-of-day x))
5649 list)))
5650 (string (nth 1 org-agenda-time-grid))
5651 (gridtimes (nth 2 org-agenda-time-grid))
5652 (req (car org-agenda-time-grid))
5653 (remove (member 'remove-match req))
5654 new time)
5655 (if (and (member 'require-timed req) (not have))
5656 ;; don't show empty grid
5657 (throw 'exit list))
5658 (while (setq time (pop gridtimes))
5659 (unless (and remove (member time have))
5660 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5661 (push (org-format-agenda-item
5662 nil string "" nil
5663 (concat (substring time 0 -2) ":" (substring time -2)))
5664 new)
5665 (put-text-property
5666 2 (length (car new)) 'face 'org-time-grid (car new))))
5667 (when (and todayp org-agenda-show-current-time-in-grid)
5668 (push (org-format-agenda-item
5670 org-agenda-current-time-string
5671 "" nil
5672 (format-time-string "%H:%M "))
5673 new)
5674 (put-text-property
5675 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5677 (if (member 'time-up org-agenda-sorting-strategy-selected)
5678 (append new list)
5679 (append list new)))))
5681 (defun org-compile-prefix-format (key)
5682 "Compile the prefix format into a Lisp form that can be evaluated.
5683 The resulting form is returned and stored in the variable
5684 `org-prefix-format-compiled'."
5685 (setq org-prefix-has-time nil org-prefix-has-tag nil
5686 org-prefix-category-length nil org-prefix-has-effort nil)
5687 (let ((s (cond
5688 ((stringp org-agenda-prefix-format)
5689 org-agenda-prefix-format)
5690 ((assq key org-agenda-prefix-format)
5691 (cdr (assq key org-agenda-prefix-format)))
5692 (t " %-12:c%?-12t% s")))
5693 (start 0)
5694 varform vars var e c f opt)
5695 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5696 s start)
5697 (setq var (or (cdr (assoc (match-string 4 s)
5698 '(("c" . category) ("t" . time) ("s" . extra)
5699 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5700 'eval)
5701 c (or (match-string 3 s) "")
5702 opt (match-beginning 1)
5703 start (1+ (match-beginning 0)))
5704 (if (equal var 'time) (setq org-prefix-has-time t))
5705 (if (equal var 'tag) (setq org-prefix-has-tag t))
5706 (if (equal var 'effort) (setq org-prefix-has-effort t))
5707 (setq f (concat "%" (match-string 2 s) "s"))
5708 (when (equal var 'category)
5709 (setq org-prefix-category-length
5710 (floor (abs (string-to-number (match-string 2 s)))))
5711 (setq org-prefix-category-max-length
5712 (let ((x (match-string 2 s)))
5713 (save-match-data
5714 (if (string-match "\\.[0-9]+" x)
5715 (string-to-number (substring (match-string 0 x) 1)))))))
5716 (if (eq var 'eval)
5717 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5718 (if opt
5719 (setq varform
5720 `(if (equal "" ,var)
5722 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5723 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5724 (setq s (replace-match "%s" t nil s))
5725 (push varform vars))
5726 (setq vars (nreverse vars))
5727 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5729 (defun org-set-sorting-strategy (key)
5730 (if (symbolp (car org-agenda-sorting-strategy))
5731 ;; the old format
5732 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5733 (setq org-agenda-sorting-strategy-selected
5734 (or (cdr (assq key org-agenda-sorting-strategy))
5735 (cdr (assq 'agenda org-agenda-sorting-strategy))
5736 '(time-up category-keep priority-down)))))
5738 (defun org-get-time-of-day (s &optional string mod24)
5739 "Check string S for a time of day.
5740 If found, return it as a military time number between 0 and 2400.
5741 If not found, return nil.
5742 The optional STRING argument forces conversion into a 5 character wide string
5743 HH:MM."
5744 (save-match-data
5745 (when
5746 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5747 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5748 (let* ((h (string-to-number (match-string 1 s)))
5749 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5750 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5751 (am-p (equal ampm "am"))
5752 (h1 (cond ((not ampm) h)
5753 ((= h 12) (if am-p 0 12))
5754 (t (+ h (if am-p 0 12)))))
5755 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5756 (mod h1 24) h1))
5757 (t0 (+ (* 100 h2) m))
5758 (t1 (concat (if (>= h1 24) "+" " ")
5759 (if (and org-agenda-time-leading-zero
5760 (< t0 1000)) "0" "")
5761 (if (< t0 100) "0" "")
5762 (if (< t0 10) "0" "")
5763 (int-to-string t0))))
5764 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5766 (defvar org-agenda-before-sorting-filter-function nil
5767 "Function to be applied to agenda items prior to sorting.
5768 Prior to sorting also means just before they are inserted into the agenda.
5770 To aid sorting, you may revisit the original entries and add more text
5771 properties which will later be used by the sorting functions.
5773 The function should take a string argument, an agenda line.
5774 It has access to the text properties in that line, which contain among
5775 other things, the property `org-hd-marker' that points to the entry
5776 where the line comes from. Note that not all lines going into the agenda
5777 have this property, only most.
5779 The function should return the modified string. It is probably best
5780 to ONLY change text properties.
5782 You can also use this function as a filter, by returning nil for lines
5783 you don't want to have in the agenda at all. For this application, you
5784 could bind the variable in the options section of a custom command.")
5786 (defun org-finalize-agenda-entries (list &optional nosort)
5787 "Sort and concatenate the agenda items."
5788 (setq list (mapcar 'org-agenda-highlight-todo list))
5789 (if nosort
5790 list
5791 (when org-agenda-before-sorting-filter-function
5792 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5793 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5795 (defun org-agenda-highlight-todo (x)
5796 (let ((org-done-keywords org-done-keywords-for-agenda)
5797 (case-fold-search nil)
5799 (if (eq x 'line)
5800 (save-excursion
5801 (beginning-of-line 1)
5802 (setq re (org-get-at-bol 'org-todo-regexp))
5803 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5804 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5805 (add-text-properties (match-beginning 0) (match-end 1)
5806 (list 'face (org-get-todo-face 1)))
5807 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5808 (delete-region (match-beginning 1) (1- (match-end 0)))
5809 (goto-char (match-beginning 1))
5810 (insert (format org-agenda-todo-keyword-format s)))))
5811 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5812 (setq re (get-text-property 0 'org-todo-regexp x))
5813 (when (and re
5814 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5815 x (or pl 0)) pl))
5816 (add-text-properties
5817 (or (match-end 1) (match-end 0)) (match-end 0)
5818 (list 'face (org-get-todo-face (match-string 2 x)))
5820 (when (match-end 1)
5821 (setq x (concat (substring x 0 (match-end 1))
5822 (format org-agenda-todo-keyword-format
5823 (match-string 2 x))
5824 (org-add-props " " (text-properties-at 0 x))
5825 (substring x (match-end 3)))))))
5826 x)))
5828 (defsubst org-cmp-priority (a b)
5829 "Compare the priorities of string A and B."
5830 (let ((pa (or (get-text-property 1 'priority a) 0))
5831 (pb (or (get-text-property 1 'priority b) 0)))
5832 (cond ((> pa pb) +1)
5833 ((< pa pb) -1)
5834 (t nil))))
5836 (defsubst org-cmp-effort (a b)
5837 "Compare the priorities of string A and B."
5838 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5839 (ea (or (get-text-property 1 'effort-minutes a) def))
5840 (eb (or (get-text-property 1 'effort-minutes b) def)))
5841 (cond ((> ea eb) +1)
5842 ((< ea eb) -1)
5843 (t nil))))
5845 (defsubst org-cmp-category (a b)
5846 "Compare the string values of categories of strings A and B."
5847 (let ((ca (or (get-text-property 1 'org-category a) ""))
5848 (cb (or (get-text-property 1 'org-category b) "")))
5849 (cond ((string-lessp ca cb) -1)
5850 ((string-lessp cb ca) +1)
5851 (t nil))))
5853 (defsubst org-cmp-todo-state (a b)
5854 "Compare the todo states of strings A and B."
5855 (let* ((ma (or (get-text-property 1 'org-marker a)
5856 (get-text-property 1 'org-hd-marker a)))
5857 (mb (or (get-text-property 1 'org-marker b)
5858 (get-text-property 1 'org-hd-marker b)))
5859 (fa (and ma (marker-buffer ma)))
5860 (fb (and mb (marker-buffer mb)))
5861 (todo-kwds
5862 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5863 (and fb (with-current-buffer fb org-todo-keywords-1))))
5864 (ta (or (get-text-property 1 'todo-state a) ""))
5865 (tb (or (get-text-property 1 'todo-state b) ""))
5866 (la (- (length (member ta todo-kwds))))
5867 (lb (- (length (member tb todo-kwds))))
5868 (donepa (member ta org-done-keywords-for-agenda))
5869 (donepb (member tb org-done-keywords-for-agenda)))
5870 (cond ((and donepa (not donepb)) -1)
5871 ((and (not donepa) donepb) +1)
5872 ((< la lb) -1)
5873 ((< lb la) +1)
5874 (t nil))))
5876 (defsubst org-cmp-alpha (a b)
5877 "Compare the headlines, alphabetically."
5878 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5879 (plb (text-property-any 0 (length b) 'org-heading t b))
5880 (ta (and pla (substring a pla)))
5881 (tb (and plb (substring b plb))))
5882 (when pla
5883 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5884 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5885 (setq ta (substring ta (match-end 0))))
5886 (setq ta (downcase ta)))
5887 (when plb
5888 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5889 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5890 (setq tb (substring tb (match-end 0))))
5891 (setq tb (downcase tb)))
5892 (cond ((not ta) +1)
5893 ((not tb) -1)
5894 ((string-lessp ta tb) -1)
5895 ((string-lessp tb ta) +1)
5896 (t nil))))
5898 (defsubst org-cmp-tag (a b)
5899 "Compare the string values of the first tags of A and B."
5900 (let ((ta (car (last (get-text-property 1 'tags a))))
5901 (tb (car (last (get-text-property 1 'tags b)))))
5902 (cond ((not ta) +1)
5903 ((not tb) -1)
5904 ((string-lessp ta tb) -1)
5905 ((string-lessp tb ta) +1)
5906 (t nil))))
5908 (defsubst org-cmp-time (a b)
5909 "Compare the time-of-day values of strings A and B."
5910 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5911 (ta (or (get-text-property 1 'time-of-day a) def))
5912 (tb (or (get-text-property 1 'time-of-day b) def)))
5913 (cond ((< ta tb) -1)
5914 ((< tb ta) +1)
5915 (t nil))))
5917 (defsubst org-cmp-habit-p (a b)
5918 "Compare the todo states of strings A and B."
5919 (let ((ha (get-text-property 1 'org-habit-p a))
5920 (hb (get-text-property 1 'org-habit-p b)))
5921 (cond ((and ha (not hb)) -1)
5922 ((and (not ha) hb) +1)
5923 (t nil))))
5925 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5927 (defun org-entries-lessp (a b)
5928 "Predicate for sorting agenda entries."
5929 ;; The following variables will be used when the form is evaluated.
5930 ;; So even though the compiler complains, keep them.
5931 (let* ((ss org-agenda-sorting-strategy-selected)
5932 (time-up (and (org-em 'time-up 'time-down ss)
5933 (org-cmp-time a b)))
5934 (time-down (if time-up (- time-up) nil))
5935 (priority-up (and (org-em 'priority-up 'priority-down ss)
5936 (org-cmp-priority a b)))
5937 (priority-down (if priority-up (- priority-up) nil))
5938 (effort-up (and (org-em 'effort-up 'effort-down ss)
5939 (org-cmp-effort a b)))
5940 (effort-down (if effort-up (- effort-up) nil))
5941 (category-up (and (or (org-em 'category-up 'category-down ss)
5942 (memq 'category-keep ss))
5943 (org-cmp-category a b)))
5944 (category-down (if category-up (- category-up) nil))
5945 (category-keep (if category-up +1 nil))
5946 (tag-up (and (org-em 'tag-up 'tag-down ss)
5947 (org-cmp-tag a b)))
5948 (tag-down (if tag-up (- tag-up) nil))
5949 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5950 (org-cmp-todo-state a b)))
5951 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5952 (habit-up (and (org-em 'habit-up 'habit-down ss)
5953 (org-cmp-habit-p a b)))
5954 (habit-down (if habit-up (- habit-up) nil))
5955 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5956 (org-cmp-alpha a b)))
5957 (alpha-down (if alpha-up (- alpha-up) nil))
5958 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5959 user-defined-up user-defined-down)
5960 (if (and need-user-cmp org-agenda-cmp-user-defined
5961 (functionp org-agenda-cmp-user-defined))
5962 (setq user-defined-up
5963 (funcall org-agenda-cmp-user-defined a b)
5964 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5965 (cdr (assoc
5966 (eval (cons 'or org-agenda-sorting-strategy-selected))
5967 '((-1 . t) (1 . nil) (nil . nil))))))
5969 ;;; Agenda restriction lock
5971 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5972 "Overlay to mark the headline to which agenda commands are restricted.")
5973 (overlay-put org-agenda-restriction-lock-overlay
5974 'face 'org-agenda-restriction-lock)
5975 (overlay-put org-agenda-restriction-lock-overlay
5976 'help-echo "Agendas are currently limited to this subtree.")
5977 (org-detach-overlay org-agenda-restriction-lock-overlay)
5979 (defun org-agenda-set-restriction-lock (&optional type)
5980 "Set restriction lock for agenda, to current subtree or file.
5981 Restriction will be the file if TYPE is `file', or if type is the
5982 universal prefix '(4), or if the cursor is before the first headline
5983 in the file. Otherwise, restriction will be to the current subtree."
5984 (interactive "P")
5985 (and (equal type '(4)) (setq type 'file))
5986 (setq type (cond
5987 (type type)
5988 ((org-at-heading-p) 'subtree)
5989 ((condition-case nil (org-back-to-heading t) (error nil))
5990 'subtree)
5991 (t 'file)))
5992 (if (eq type 'subtree)
5993 (progn
5994 (setq org-agenda-restrict t)
5995 (setq org-agenda-overriding-restriction 'subtree)
5996 (put 'org-agenda-files 'org-restrict
5997 (list (buffer-file-name (buffer-base-buffer))))
5998 (org-back-to-heading t)
5999 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6000 (move-marker org-agenda-restrict-begin (point))
6001 (move-marker org-agenda-restrict-end
6002 (save-excursion (org-end-of-subtree t)))
6003 (message "Locking agenda restriction to subtree"))
6004 (put 'org-agenda-files 'org-restrict
6005 (list (buffer-file-name (buffer-base-buffer))))
6006 (setq org-agenda-restrict nil)
6007 (setq org-agenda-overriding-restriction 'file)
6008 (move-marker org-agenda-restrict-begin nil)
6009 (move-marker org-agenda-restrict-end nil)
6010 (message "Locking agenda restriction to file"))
6011 (setq current-prefix-arg nil)
6012 (org-agenda-maybe-redo))
6014 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6015 "Remove the agenda restriction lock."
6016 (interactive "P")
6017 (org-detach-overlay org-agenda-restriction-lock-overlay)
6018 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6019 (setq org-agenda-overriding-restriction nil)
6020 (setq org-agenda-restrict nil)
6021 (put 'org-agenda-files 'org-restrict nil)
6022 (move-marker org-agenda-restrict-begin nil)
6023 (move-marker org-agenda-restrict-end nil)
6024 (setq current-prefix-arg nil)
6025 (message "Agenda restriction lock removed")
6026 (or noupdate (org-agenda-maybe-redo)))
6028 (defun org-agenda-maybe-redo ()
6029 "If there is any window showing the agenda view, update it."
6030 (let ((w (get-buffer-window org-agenda-buffer-name t))
6031 (w0 (selected-window)))
6032 (when w
6033 (select-window w)
6034 (org-agenda-redo)
6035 (select-window w0)
6036 (if org-agenda-overriding-restriction
6037 (message "Agenda view shifted to new %s restriction"
6038 org-agenda-overriding-restriction)
6039 (message "Agenda restriction lock removed")))))
6041 ;;; Agenda commands
6043 (defun org-agenda-check-type (error &rest types)
6044 "Check if agenda buffer is of allowed type.
6045 If ERROR is non-nil, throw an error, otherwise just return nil."
6046 (if (memq org-agenda-type types)
6048 (if error
6049 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6050 nil)))
6052 (defun org-agenda-quit ()
6053 "Exit agenda by removing the window or the buffer."
6054 (interactive)
6055 (if org-agenda-columns-active
6056 (org-columns-quit)
6057 (let ((buf (current-buffer)))
6058 (if (eq org-agenda-window-setup 'other-frame)
6059 (progn
6060 (kill-buffer buf)
6061 (org-agenda-reset-markers)
6062 (org-columns-remove-overlays)
6063 (setq org-agenda-archives-mode nil)
6064 (delete-frame))
6065 (and (not (eq org-agenda-window-setup 'current-window))
6066 (not (one-window-p))
6067 (delete-window))
6068 (kill-buffer buf)
6069 (org-agenda-reset-markers)
6070 (org-columns-remove-overlays)
6071 (setq org-agenda-archives-mode nil)))
6072 ;; Maybe restore the pre-agenda window configuration.
6073 (and org-agenda-restore-windows-after-quit
6074 (not (eq org-agenda-window-setup 'other-frame))
6075 org-pre-agenda-window-conf
6076 (set-window-configuration org-pre-agenda-window-conf))))
6078 (defun org-agenda-exit ()
6079 "Exit agenda by removing the window or the buffer.
6080 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6081 Org-mode buffers visited directly by the user will not be touched."
6082 (interactive)
6083 (org-release-buffers org-agenda-new-buffers)
6084 (setq org-agenda-new-buffers nil)
6085 (org-agenda-quit))
6087 (defun org-agenda-execute (arg)
6088 "Execute another agenda command, keeping same window.
6089 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6090 in the agenda."
6091 (interactive "P")
6092 (let ((org-agenda-window-setup 'current-window))
6093 (org-agenda arg)))
6095 (defun org-agenda-redo ()
6096 "Rebuild Agenda.
6097 When this is the global TODO list, a prefix argument will be interpreted."
6098 (interactive)
6099 (let* ((org-agenda-keep-modes t)
6100 (filter org-agenda-filter)
6101 (preset (get 'org-agenda-filter :preset-filter))
6102 (org-agenda-filter-while-redo (or filter preset))
6103 (cols org-agenda-columns-active)
6104 (line (org-current-line))
6105 (window-line (- line (org-current-line (window-start))))
6106 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6107 (put 'org-agenda-filter :preset-filter nil)
6108 (and cols (org-columns-quit))
6109 (message "Rebuilding agenda buffer...")
6110 (org-let lprops '(eval org-agenda-redo-command))
6111 (setq org-agenda-undo-list nil
6112 org-agenda-pending-undo-list nil)
6113 (message "Rebuilding agenda buffer...done")
6114 (put 'org-agenda-filter :preset-filter preset)
6115 (and (or filter preset) (org-agenda-filter-apply filter))
6116 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6117 (org-goto-line line)
6118 (recenter window-line)))
6121 (defvar org-global-tags-completion-table nil)
6122 (defvar org-agenda-filter-form nil)
6123 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6124 "Keep only those lines in the agenda buffer that have a specific tag.
6125 The tag is selected with its fast selection letter, as configured.
6126 With prefix argument STRIP, remove all lines that do have the tag.
6127 A lisp caller can specify CHAR. NARROW means that the new tag should be
6128 used to narrow the search - the interactive user can also press `-' or `+'
6129 to switch to narrowing."
6130 (interactive "P")
6131 (let* ((alist org-tag-alist-for-agenda)
6132 (tag-chars (mapconcat
6133 (lambda (x) (if (and (not (symbolp (car x)))
6134 (cdr x))
6135 (char-to-string (cdr x))
6136 ""))
6137 alist ""))
6138 (efforts (org-split-string
6139 (or (cdr (assoc (concat org-effort-property "_ALL")
6140 org-global-properties))
6141 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6142 "")))
6143 (effort-op org-agenda-filter-effort-default-operator)
6144 (effort-prompt "")
6145 (inhibit-read-only t)
6146 (current org-agenda-filter)
6147 maybe-refresh a n tag)
6148 (unless char
6149 (message
6150 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6151 (if narrow "Narrow" "Filter") tag-chars
6152 (if org-agenda-auto-exclude-function "[RET], " ""))
6153 (setq char (read-char)))
6154 (when (member char '(?+ ?-))
6155 ;; Narrowing down
6156 (cond ((equal char ?-) (setq strip t narrow t))
6157 ((equal char ?+) (setq strip nil narrow t)))
6158 (message
6159 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6160 (setq char (read-char)))
6161 (when (member char '(?< ?> ?= ??))
6162 ;; An effort operator
6163 (setq effort-op (char-to-string char))
6164 (setq alist nil) ; to make sure it will be interpreted as effort.
6165 (unless (equal char ??)
6166 (loop for i from 0 to 9 do
6167 (setq effort-prompt
6168 (concat
6169 effort-prompt " ["
6170 (if (= i 9) "0" (int-to-string (1+ i)))
6171 "]" (nth i efforts))))
6172 (message "Effort%s: %s " effort-op effort-prompt)
6173 (setq char (read-char))
6174 (when (or (< char ?0) (> char ?9))
6175 (error "Need 1-9,0 to select effort" ))))
6176 (when (equal char ?\t)
6177 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6178 (org-set-local 'org-global-tags-completion-table
6179 (org-global-tags-completion-table)))
6180 (let ((completion-ignore-case t))
6181 (setq tag (org-icompleting-read
6182 "Tag: " org-global-tags-completion-table))))
6183 (cond
6184 ((equal char ?\r)
6185 (org-agenda-filter-by-tag-show-all)
6186 (when org-agenda-auto-exclude-function
6187 (setq org-agenda-filter '())
6188 (dolist (tag (org-agenda-get-represented-tags))
6189 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6190 (if modifier
6191 (push modifier org-agenda-filter))))
6192 (if (not (null org-agenda-filter))
6193 (org-agenda-filter-apply org-agenda-filter)))
6194 (setq maybe-refresh t))
6195 ((equal char ?/)
6196 (org-agenda-filter-by-tag-show-all)
6197 (when (get 'org-agenda-filter :preset-filter)
6198 (org-agenda-filter-apply org-agenda-filter))
6199 (setq maybe-refresh t))
6200 ((or (equal char ?\ )
6201 (setq a (rassoc char alist))
6202 (and (>= char ?0) (<= char ?9)
6203 (setq n (if (= char ?0) 9 (- char ?0 1))
6204 tag (concat effort-op (nth n efforts))
6205 a (cons tag nil)))
6206 (and (= char ??)
6207 (setq tag "?eff")
6208 a (cons tag nil))
6209 (and tag (setq a (cons tag nil))))
6210 (org-agenda-filter-by-tag-show-all)
6211 (setq tag (car a))
6212 (setq org-agenda-filter
6213 (cons (concat (if strip "-" "+") tag)
6214 (if narrow current nil)))
6215 (org-agenda-filter-apply org-agenda-filter)
6216 (setq maybe-refresh t))
6217 (t (error "Invalid tag selection character %c" char)))
6218 (when (and maybe-refresh
6219 (eq org-agenda-clockreport-mode 'with-filter))
6220 (org-agenda-redo))))
6222 (defun org-agenda-get-represented-tags ()
6223 "Get a list of all tags currently represented in the agenda."
6224 (let (p tags)
6225 (save-excursion
6226 (goto-char (point-min))
6227 (while (setq p (next-single-property-change (point) 'tags))
6228 (goto-char p)
6229 (mapc (lambda (x) (add-to-list 'tags x))
6230 (get-text-property (point) 'tags))))
6231 tags))
6233 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6234 "Refine the current filter. See `org-agenda-filter-by-tag."
6235 (interactive "P")
6236 (org-agenda-filter-by-tag strip char 'refine))
6238 (defun org-agenda-filter-make-matcher ()
6239 "Create the form that tests a line for the agenda filter."
6240 (let (f f1)
6241 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6242 org-agenda-filter))
6243 (if (member x '("-" "+"))
6244 (setq f1 (if (equal x "-") 'tags '(not tags)))
6245 (if (string-match "[<=>?]" x)
6246 (setq f1 (org-agenda-filter-effort-form x))
6247 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6248 (if (equal (string-to-char x) ?-)
6249 (setq f1 (list 'not f1))))
6250 (push f1 f))
6251 (cons 'and (nreverse f))))
6253 (defun org-agenda-filter-effort-form (e)
6254 "Return the form to compare the effort of the current line with what E says.
6255 E looks like \"+<2:25\"."
6256 (let (op)
6257 (setq e (substring e 1))
6258 (setq op (string-to-char e) e (substring e 1))
6259 (setq op (cond ((equal op ?<) '<=)
6260 ((equal op ?>) '>=)
6261 ((equal op ??) op)
6262 (t '=)))
6263 (list 'org-agenda-compare-effort (list 'quote op)
6264 (org-duration-string-to-minutes e))))
6266 (defun org-agenda-compare-effort (op value)
6267 "Compare the effort of the current line with VALUE, using OP.
6268 If the line does not have an effort defined, return nil."
6269 (let ((eff (org-get-at-bol 'effort-minutes)))
6270 (if (equal op ??)
6271 (not eff)
6272 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6273 value))))
6275 (defun org-agenda-filter-apply (filter)
6276 "Set FILTER as the new agenda filter and apply it."
6277 (let (tags)
6278 (setq org-agenda-filter filter
6279 org-agenda-filter-form (org-agenda-filter-make-matcher))
6280 (org-agenda-set-mode-name)
6281 (save-excursion
6282 (goto-char (point-min))
6283 (while (not (eobp))
6284 (if (org-get-at-bol 'org-marker)
6285 (progn
6286 (setq tags (org-get-at-bol 'tags)) ; used in eval
6287 (if (not (eval org-agenda-filter-form))
6288 (org-agenda-filter-by-tag-hide-line))
6289 (beginning-of-line 2))
6290 (beginning-of-line 2))))
6291 (if (get-char-property (point) 'invisible)
6292 (org-agenda-previous-line))))
6294 (defun org-agenda-filter-by-tag-hide-line ()
6295 (let (ov)
6296 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6297 (point-at-eol)))
6298 (overlay-put ov 'invisible t)
6299 (overlay-put ov 'type 'tags-filter)
6300 (push ov org-agenda-filter-overlays)))
6302 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6303 (setq pos (or pos (point)))
6304 (save-excursion
6305 (dolist (ov (overlays-at pos))
6306 (when (and (overlay-get ov 'invisible)
6307 (eq (overlay-get ov 'type) 'tags-filter))
6308 (goto-char pos)
6309 (if (< (overlay-start ov) (point-at-eol))
6310 (move-overlay ov (point-at-eol)
6311 (overlay-end ov)))))))
6313 (defun org-agenda-filter-by-tag-show-all ()
6314 (mapc 'delete-overlay org-agenda-filter-overlays)
6315 (setq org-agenda-filter-overlays nil)
6316 (setq org-agenda-filter nil)
6317 (setq org-agenda-filter-form nil)
6318 (org-agenda-set-mode-name))
6320 (defun org-agenda-manipulate-query-add ()
6321 "Manipulate the query by adding a search term with positive selection.
6322 Positive selection means the term must be matched for selection of an entry."
6323 (interactive)
6324 (org-agenda-manipulate-query ?\[))
6325 (defun org-agenda-manipulate-query-subtract ()
6326 "Manipulate the query by adding a search term with negative selection.
6327 Negative selection means term must not be matched for selection of an entry."
6328 (interactive)
6329 (org-agenda-manipulate-query ?\]))
6330 (defun org-agenda-manipulate-query-add-re ()
6331 "Manipulate the query by adding a search regexp with positive selection.
6332 Positive selection means the regexp must match for selection of an entry."
6333 (interactive)
6334 (org-agenda-manipulate-query ?\{))
6335 (defun org-agenda-manipulate-query-subtract-re ()
6336 "Manipulate the query by adding a search regexp with negative selection.
6337 Negative selection means regexp must not match for selection of an entry."
6338 (interactive)
6339 (org-agenda-manipulate-query ?\}))
6340 (defun org-agenda-manipulate-query (char)
6341 (cond
6342 ((memq org-agenda-type '(timeline agenda))
6343 (let ((org-agenda-include-inactive-timestamps t))
6344 (org-agenda-redo))
6345 (message "Display now includes inactive timestamps as well"))
6346 ((eq org-agenda-type 'search)
6347 (org-add-to-string
6348 'org-agenda-query-string
6349 (if org-agenda-last-search-view-search-was-boolean
6350 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6351 (?\{ . " +{}") (?\} . " -{}"))))
6352 " "))
6353 (setq org-agenda-redo-command
6354 (list 'org-search-view
6355 org-todo-only
6356 org-agenda-query-string
6357 (+ (length org-agenda-query-string)
6358 (if (member char '(?\{ ?\})) 0 1))))
6359 (set-register org-agenda-query-register org-agenda-query-string)
6360 (org-agenda-redo))
6361 (t (error "Cannot manipulate query for %s-type agenda buffers"
6362 org-agenda-type))))
6364 (defun org-add-to-string (var string)
6365 (set var (concat (symbol-value var) string)))
6367 (defun org-agenda-goto-date (date)
6368 "Jump to DATE in agenda."
6369 (interactive (list (let ((org-read-date-prefer-future
6370 (eval org-agenda-jump-prefer-future)))
6371 (org-read-date))))
6372 (org-agenda-list nil date))
6374 (defun org-agenda-goto-today ()
6375 "Go to today."
6376 (interactive)
6377 (org-agenda-check-type t 'timeline 'agenda)
6378 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6379 (cond
6380 (tdpos (goto-char tdpos))
6381 ((eq org-agenda-type 'agenda)
6382 (let* ((sd (org-agenda-compute-starting-span
6383 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6384 (org-agenda-overriding-arguments org-agenda-last-arguments))
6385 (setf (nth 1 org-agenda-overriding-arguments) sd)
6386 (org-agenda-redo)
6387 (org-agenda-find-same-or-today-or-agenda)))
6388 (t (error "Cannot find today")))))
6390 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6391 (goto-char
6392 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6393 (text-property-any (point-min) (point-max) 'org-today t)
6394 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6395 (point-min))))
6397 (defun org-agenda-later (arg)
6398 "Go forward in time by thee current span.
6399 With prefix ARG, go forward that many times the current span."
6400 (interactive "p")
6401 (org-agenda-check-type t 'agenda)
6402 (let* ((span org-agenda-current-span)
6403 (sd org-starting-day)
6404 (greg (calendar-gregorian-from-absolute sd))
6405 (cnt (org-get-at-bol 'org-day-cnt))
6406 greg2)
6407 (cond
6408 ((eq span 'day)
6409 (setq sd (+ arg sd)))
6410 ((eq span 'week)
6411 (setq sd (+ (* 7 arg) sd)))
6412 ((eq span 'month)
6413 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6414 sd (calendar-absolute-from-gregorian greg2))
6415 (setcar greg2 (1+ (car greg2))))
6416 ((eq span 'year)
6417 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6418 sd (calendar-absolute-from-gregorian greg2))
6419 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6421 (setq sd (+ (* span arg) sd))))
6422 (let ((org-agenda-overriding-arguments
6423 (list (car org-agenda-last-arguments) sd span t)))
6424 (org-agenda-redo)
6425 (org-agenda-find-same-or-today-or-agenda cnt))))
6427 (defun org-agenda-earlier (arg)
6428 "Go backward in time by the current span.
6429 With prefix ARG, go backward that many times the current span."
6430 (interactive "p")
6431 (org-agenda-later (- arg)))
6433 (defun org-agenda-view-mode-dispatch ()
6434 "Call one of the view mode commands."
6435 (interactive)
6436 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6437 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6438 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6439 [E]ntryText")
6440 (let ((a (read-char-exclusive)))
6441 (case a
6442 (?\ (call-interactively 'org-agenda-reset-view))
6443 (?d (call-interactively 'org-agenda-day-view))
6444 (?w (call-interactively 'org-agenda-week-view))
6445 (?m (call-interactively 'org-agenda-month-view))
6446 (?y (call-interactively 'org-agenda-year-view))
6447 (?l (call-interactively 'org-agenda-log-mode))
6448 (?L (org-agenda-log-mode '(4)))
6449 (?c (org-agenda-log-mode 'clockcheck))
6450 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6451 (?a (call-interactively 'org-agenda-archives-mode))
6452 (?A (org-agenda-archives-mode 'files))
6453 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6454 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6455 (?G (call-interactively 'org-agenda-toggle-time-grid))
6456 (?D (call-interactively 'org-agenda-toggle-diary))
6457 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6458 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6459 (org-agenda-check-type t 'timeline 'agenda)
6460 (org-agenda-redo))
6461 (message "Display now includes inactive timestamps as well"))
6462 (?q (message "Abort"))
6463 (otherwise (error "Invalid key" )))))
6465 (defun org-agenda-reset-view ()
6466 "Switch to default view for agenda."
6467 (interactive)
6468 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6469 (defun org-agenda-day-view (&optional day-of-year)
6470 "Switch to daily view for agenda.
6471 With argument DAY-OF-YEAR, switch to that day of the year."
6472 (interactive "P")
6473 (org-agenda-change-time-span 'day day-of-year))
6474 (defun org-agenda-week-view (&optional iso-week)
6475 "Switch to daily view for agenda.
6476 With argument ISO-WEEK, switch to the corresponding ISO week.
6477 If ISO-WEEK has more then 2 digits, only the last two encode the
6478 week. Any digits before this encode a year. So 200712 means
6479 week 12 of year 2007. Years in the range 1938-2037 can also be
6480 written as 2-digit years."
6481 (interactive "P")
6482 (org-agenda-change-time-span 'week iso-week))
6483 (defun org-agenda-month-view (&optional month)
6484 "Switch to monthly view for agenda.
6485 With argument MONTH, switch to that month."
6486 (interactive "P")
6487 (org-agenda-change-time-span 'month month))
6488 (defun org-agenda-year-view (&optional year)
6489 "Switch to yearly view for agenda.
6490 With argument YEAR, switch to that year.
6491 If MONTH has more then 2 digits, only the last two encode the
6492 month. Any digits before this encode a year. So 200712 means
6493 December year 2007. Years in the range 1938-2037 can also be
6494 written as 2-digit years."
6495 (interactive "P")
6496 (when year
6497 (setq year (org-small-year-to-year year)))
6498 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6499 (org-agenda-change-time-span 'year year)
6500 (error "Abort")))
6502 (defun org-agenda-change-time-span (span &optional n)
6503 "Change the agenda view to SPAN.
6504 SPAN may be `day', `week', `month', `year'."
6505 (org-agenda-check-type t 'agenda)
6506 (if (and (not n) (equal org-agenda-current-span span))
6507 (error "Viewing span is already \"%s\"" span))
6508 (let* ((sd (or (org-get-at-bol 'day)
6509 org-starting-day))
6510 (sd (org-agenda-compute-starting-span sd span n))
6511 (org-agenda-overriding-arguments
6512 (or org-agenda-overriding-arguments
6513 (list (car org-agenda-last-arguments) sd span t))))
6514 (org-agenda-redo)
6515 (org-agenda-find-same-or-today-or-agenda))
6516 (org-agenda-set-mode-name)
6517 (message "Switched to %s view" span))
6519 (defun org-agenda-compute-starting-span (sd span &optional n)
6520 "Compute starting date for agenda.
6521 SPAN may be `day', `week', `month', `year'. The return value
6522 is a cons cell with the starting date and the number of days,
6523 so that the date SD will be in that range."
6524 (let* ((greg (calendar-gregorian-from-absolute sd))
6525 (dg (nth 1 greg))
6526 (mg (car greg))
6527 (yg (nth 2 greg)))
6528 (cond
6529 ((eq span 'day)
6530 (when n
6531 (setq sd (+ (calendar-absolute-from-gregorian
6532 (list mg 1 yg))
6533 n -1))))
6534 ((eq span 'week)
6535 (let* ((nt (calendar-day-of-week
6536 (calendar-gregorian-from-absolute sd)))
6537 (d (if org-agenda-start-on-weekday
6538 (- nt org-agenda-start-on-weekday)
6541 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6542 (when n
6543 (require 'cal-iso)
6544 (when (> n 99)
6545 (setq y1 (org-small-year-to-year (/ n 100))
6546 n (mod n 100)))
6547 (setq sd
6548 (calendar-absolute-from-iso
6549 (list n 1
6550 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6551 ((eq span 'month)
6552 (let (y1)
6553 (when (and n (> n 99))
6554 (setq y1 (org-small-year-to-year (/ n 100))
6555 n (mod n 100)))
6556 (setq sd (calendar-absolute-from-gregorian
6557 (list (or n mg) 1 (or y1 yg))))))
6558 ((eq span 'year)
6559 (setq sd (calendar-absolute-from-gregorian
6560 (list 1 1 (or n yg))))))
6561 sd))
6563 (defun org-agenda-next-date-line (&optional arg)
6564 "Jump to the next line indicating a date in agenda buffer."
6565 (interactive "p")
6566 (org-agenda-check-type t 'agenda 'timeline)
6567 (beginning-of-line 1)
6568 ;; This does not work if user makes date format that starts with a blank
6569 (if (looking-at "^\\S-") (forward-char 1))
6570 (if (not (re-search-forward "^\\S-" nil t arg))
6571 (progn
6572 (backward-char 1)
6573 (error "No next date after this line in this buffer")))
6574 (goto-char (match-beginning 0)))
6576 (defun org-agenda-previous-date-line (&optional arg)
6577 "Jump to the previous line indicating a date in agenda buffer."
6578 (interactive "p")
6579 (org-agenda-check-type t 'agenda 'timeline)
6580 (beginning-of-line 1)
6581 (if (not (re-search-backward "^\\S-" nil t arg))
6582 (error "No previous date before this line in this buffer")))
6584 ;; Initialize the highlight
6585 (defvar org-hl (make-overlay 1 1))
6586 (overlay-put org-hl 'face 'highlight)
6588 (defun org-highlight (begin end &optional buffer)
6589 "Highlight a region with overlay."
6590 (move-overlay org-hl begin end (or buffer (current-buffer))))
6592 (defun org-unhighlight ()
6593 "Detach overlay INDEX."
6594 (org-detach-overlay org-hl))
6596 ;; FIXME this is currently not used.
6597 (defun org-highlight-until-next-command (beg end &optional buffer)
6598 "Move the highlight overlay to BEG/END, remove it before the next command."
6599 (org-highlight beg end buffer)
6600 (add-hook 'pre-command-hook 'org-unhighlight-once))
6601 (defun org-unhighlight-once ()
6602 "Remove the highlight from its position, and this function from the hook."
6603 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6604 (org-unhighlight))
6606 (defun org-agenda-follow-mode ()
6607 "Toggle follow mode in an agenda buffer."
6608 (interactive)
6609 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6610 (org-agenda-set-mode-name)
6611 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6612 (org-agenda-show))
6613 (message "Follow mode is %s"
6614 (if org-agenda-follow-mode "on" "off")))
6616 (defun org-agenda-entry-text-mode (&optional arg)
6617 "Toggle entry text mode in an agenda buffer."
6618 (interactive "P")
6619 (setq org-agenda-entry-text-mode (or (integerp arg)
6620 (not org-agenda-entry-text-mode)))
6621 (org-agenda-entry-text-hide)
6622 (and org-agenda-entry-text-mode
6623 (let ((org-agenda-entry-text-maxlines
6624 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6625 (org-agenda-entry-text-show)))
6626 (org-agenda-set-mode-name)
6627 (message "Entry text mode is %s. Maximum number of lines is %d"
6628 (if org-agenda-entry-text-mode "on" "off")
6629 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6631 (defun org-agenda-clockreport-mode (&optional with-filter)
6632 "Toggle clocktable mode in an agenda buffer.
6633 With prefix arg WITH-FILTER, make the clocktable respect the current
6634 agenda filter."
6635 (interactive "P")
6636 (org-agenda-check-type t 'agenda)
6637 (if with-filter
6638 (setq org-agenda-clockreport-mode 'with-filter)
6639 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6640 (org-agenda-set-mode-name)
6641 (org-agenda-redo)
6642 (message "Clocktable mode is %s"
6643 (if org-agenda-clockreport-mode "on" "off")))
6645 (defun org-agenda-log-mode (&optional special)
6646 "Toggle log mode in an agenda buffer.
6647 With argument SPECIAL, show all possible log items, not only the ones
6648 configured in `org-agenda-log-mode-items'.
6649 With a double `C-u' prefix arg, show *only* log items, nothing else."
6650 (interactive "P")
6651 (org-agenda-check-type t 'agenda 'timeline)
6652 (setq org-agenda-show-log
6653 (cond
6654 ((equal special '(16)) 'only)
6655 ((eq special 'clockcheck)
6656 (if (eq org-agenda-show-log 'clockcheck)
6657 nil 'clockcheck))
6658 (special '(closed clock state))
6659 (t (not org-agenda-show-log))))
6660 (org-agenda-set-mode-name)
6661 (org-agenda-redo)
6662 (message "Log mode is %s"
6663 (if org-agenda-show-log "on" "off")))
6665 (defun org-agenda-archives-mode (&optional with-files)
6666 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6667 When called with a prefix argument, include all archive files as well."
6668 (interactive "P")
6669 (setq org-agenda-archives-mode
6670 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6671 (org-agenda-set-mode-name)
6672 (org-agenda-redo)
6673 (message
6674 "%s"
6675 (cond
6676 ((eq org-agenda-archives-mode nil)
6677 "No archives are included")
6678 ((eq org-agenda-archives-mode 'trees)
6679 (format "Trees with :%s: tag are included" org-archive-tag))
6680 ((eq org-agenda-archives-mode t)
6681 (format "Trees with :%s: tag and all active archive files are included"
6682 org-archive-tag)))))
6684 (defun org-agenda-toggle-diary ()
6685 "Toggle diary inclusion in an agenda buffer."
6686 (interactive)
6687 (org-agenda-check-type t 'agenda)
6688 (setq org-agenda-include-diary (not org-agenda-include-diary))
6689 (org-agenda-redo)
6690 (org-agenda-set-mode-name)
6691 (message "Diary inclusion turned %s"
6692 (if org-agenda-include-diary "on" "off")))
6694 (defun org-agenda-toggle-deadlines ()
6695 "Toggle inclusion of entries with a deadline in an agenda buffer."
6696 (interactive)
6697 (org-agenda-check-type t 'agenda)
6698 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6699 (org-agenda-redo)
6700 (org-agenda-set-mode-name)
6701 (message "Deadlines inclusion turned %s"
6702 (if org-agenda-include-deadlines "on" "off")))
6704 (defun org-agenda-toggle-time-grid ()
6705 "Toggle time grid in an agenda buffer."
6706 (interactive)
6707 (org-agenda-check-type t 'agenda)
6708 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6709 (org-agenda-redo)
6710 (org-agenda-set-mode-name)
6711 (message "Time-grid turned %s"
6712 (if org-agenda-use-time-grid "on" "off")))
6714 (defun org-agenda-set-mode-name ()
6715 "Set the mode name to indicate all the small mode settings."
6716 (setq mode-name
6717 (list "Org-Agenda"
6718 (if (get 'org-agenda-files 'org-restrict) " []" "")
6720 '(:eval (org-agenda-span-name org-agenda-current-span))
6721 (if org-agenda-follow-mode " Follow" "")
6722 (if org-agenda-entry-text-mode " ETxt" "")
6723 (if org-agenda-include-diary " Diary" "")
6724 (if org-agenda-include-deadlines " Ddl" "")
6725 (if org-agenda-use-time-grid " Grid" "")
6726 (if (and (boundp 'org-habit-show-habits)
6727 org-habit-show-habits) " Habit" "")
6728 (cond
6729 ((consp org-agenda-show-log) " LogAll")
6730 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6731 (org-agenda-show-log " Log")
6732 (t ""))
6733 (if (or org-agenda-filter (get 'org-agenda-filter
6734 :preset-filter))
6735 (concat " {" (mapconcat
6736 'identity
6737 (append (get 'org-agenda-filter
6738 :preset-filter)
6739 org-agenda-filter) "") "}")
6741 (if org-agenda-archives-mode
6742 (if (eq org-agenda-archives-mode t)
6743 " Archives"
6744 (format " :%s:" org-archive-tag))
6746 (if org-agenda-clockreport-mode
6747 (if (eq org-agenda-clockreport-mode 'with-filter)
6748 " Clock{}" " Clock")
6749 "")))
6750 (force-mode-line-update))
6752 (defun org-agenda-post-command-hook ()
6753 (setq org-agenda-type
6754 (or (get-text-property (point) 'org-agenda-type)
6755 (get-text-property (max (point-min) (1- (point)))
6756 'org-agenda-type))))
6758 (defun org-agenda-next-line ()
6759 "Move cursor to the next line, and show if follow mode is active."
6760 (interactive)
6761 (call-interactively 'next-line)
6762 (org-agenda-do-context-action))
6764 (defun org-agenda-previous-line ()
6765 "Move cursor to the previous line, and show if follow-mode is active."
6766 (interactive)
6767 (call-interactively 'previous-line)
6768 (org-agenda-do-context-action))
6770 (defun org-agenda-do-context-action ()
6771 "Show outline path and, maybe, follow mode window."
6772 (let ((m (org-get-at-bol 'org-marker)))
6773 (if (and org-agenda-follow-mode m)
6774 (org-agenda-show))
6775 (if (and m org-agenda-show-outline-path)
6776 (org-with-point-at m
6777 (org-display-outline-path t)))))
6779 (defun org-agenda-show-priority ()
6780 "Show the priority of the current item.
6781 This priority is composed of the main priority given with the [#A] cookies,
6782 and by additional input from the age of a schedules or deadline entry."
6783 (interactive)
6784 (let* ((pri (org-get-at-bol 'priority)))
6785 (message "Priority is %d" (if pri pri -1000))))
6787 (defun org-agenda-show-tags ()
6788 "Show the tags applicable to the current item."
6789 (interactive)
6790 (let* ((tags (org-get-at-bol 'tags)))
6791 (if tags
6792 (message "Tags are :%s:"
6793 (org-no-properties (mapconcat 'identity tags ":")))
6794 (message "No tags associated with this line"))))
6796 (defun org-agenda-goto (&optional highlight)
6797 "Go to the Org-mode file which contains the item at point."
6798 (interactive)
6799 (let* ((marker (or (org-get-at-bol 'org-marker)
6800 (org-agenda-error)))
6801 (buffer (marker-buffer marker))
6802 (pos (marker-position marker)))
6803 (switch-to-buffer-other-window buffer)
6804 (widen)
6805 (push-mark)
6806 (goto-char pos)
6807 (when (org-mode-p)
6808 (org-show-context 'agenda)
6809 (save-excursion
6810 (and (outline-next-heading)
6811 (org-flag-heading nil)))) ; show the next heading
6812 (when (outline-invisible-p)
6813 (show-entry)) ; display invisible text
6814 (recenter (/ (window-height) 2))
6815 (run-hooks 'org-agenda-after-show-hook)
6816 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6818 (defvar org-agenda-after-show-hook nil
6819 "Normal hook run after an item has been shown from the agenda.
6820 Point is in the buffer where the item originated.")
6822 (defun org-agenda-kill ()
6823 "Kill the entry or subtree belonging to the current agenda entry."
6824 (interactive)
6825 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6826 (let* ((marker (or (org-get-at-bol 'org-marker)
6827 (org-agenda-error)))
6828 (buffer (marker-buffer marker))
6829 (pos (marker-position marker))
6830 (type (org-get-at-bol 'type))
6831 dbeg dend (n 0) conf)
6832 (org-with-remote-undo buffer
6833 (with-current-buffer buffer
6834 (save-excursion
6835 (goto-char pos)
6836 (if (and (org-mode-p) (not (member type '("sexp"))))
6837 (setq dbeg (progn (org-back-to-heading t) (point))
6838 dend (org-end-of-subtree t t))
6839 (setq dbeg (point-at-bol)
6840 dend (min (point-max) (1+ (point-at-eol)))))
6841 (goto-char dbeg)
6842 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6843 (setq conf (or (eq t org-agenda-confirm-kill)
6844 (and (numberp org-agenda-confirm-kill)
6845 (> n org-agenda-confirm-kill))))
6846 (and conf
6847 (not (y-or-n-p
6848 (format "Delete entry with %d lines in buffer \"%s\"? "
6849 n (buffer-name buffer))))
6850 (error "Abort"))
6851 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6852 (with-current-buffer buffer (delete-region dbeg dend))
6853 (message "Agenda item and source killed"))))
6855 (defvar org-archive-default-command)
6856 (defun org-agenda-archive-default ()
6857 "Archive the entry or subtree belonging to the current agenda entry."
6858 (interactive)
6859 (require 'org-archive)
6860 (org-agenda-archive-with org-archive-default-command))
6862 (defun org-agenda-archive-default-with-confirmation ()
6863 "Archive the entry or subtree belonging to the current agenda entry."
6864 (interactive)
6865 (require 'org-archive)
6866 (org-agenda-archive-with org-archive-default-command 'confirm))
6868 (defun org-agenda-archive ()
6869 "Archive the entry or subtree belonging to the current agenda entry."
6870 (interactive)
6871 (org-agenda-archive-with 'org-archive-subtree))
6873 (defun org-agenda-archive-to-archive-sibling ()
6874 "Move the entry to the archive sibling."
6875 (interactive)
6876 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6878 (defun org-agenda-archive-with (cmd &optional confirm)
6879 "Move the entry to the archive sibling."
6880 (interactive)
6881 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6882 (let* ((marker (or (org-get-at-bol 'org-marker)
6883 (org-agenda-error)))
6884 (buffer (marker-buffer marker))
6885 (pos (marker-position marker)))
6886 (org-with-remote-undo buffer
6887 (with-current-buffer buffer
6888 (if (org-mode-p)
6889 (if (and confirm
6890 (not (y-or-n-p "Archive this subtree or entry? ")))
6891 (error "Abort")
6892 (save-excursion
6893 (goto-char pos)
6894 (org-remove-subtree-entries-from-agenda)
6895 (org-back-to-heading t)
6896 (funcall cmd)))
6897 (error "Archiving works only in Org-mode files"))))))
6899 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6900 "Remove all lines in the agenda that correspond to a given subtree.
6901 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6902 If this information is not given, the function uses the tree at point."
6903 (let ((buf (or buf (current-buffer))) m p)
6904 (save-excursion
6905 (unless (and beg end)
6906 (org-back-to-heading t)
6907 (setq beg (point))
6908 (org-end-of-subtree t)
6909 (setq end (point)))
6910 (set-buffer (get-buffer org-agenda-buffer-name))
6911 (save-excursion
6912 (goto-char (point-max))
6913 (beginning-of-line 1)
6914 (while (not (bobp))
6915 (when (and (setq m (org-get-at-bol 'org-marker))
6916 (equal buf (marker-buffer m))
6917 (setq p (marker-position m))
6918 (>= p beg)
6919 (< p end))
6920 (let ((inhibit-read-only t))
6921 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6922 (beginning-of-line 0))))))
6924 (defun org-agenda-refile (&optional goto rfloc no-update)
6925 "Refile the item at point."
6926 (interactive "P")
6927 (if (equal goto '(16))
6928 (org-refile-goto-last-stored)
6929 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6930 (org-agenda-error)))
6931 (buffer (marker-buffer marker))
6932 (pos (marker-position marker))
6933 (rfloc (or rfloc
6934 (org-refile-get-location
6935 (if goto "Goto" "Refile to") buffer
6936 org-refile-allow-creating-parent-nodes))))
6937 (with-current-buffer buffer
6938 (save-excursion
6939 (save-restriction
6940 (widen)
6941 (goto-char marker)
6942 (org-remove-subtree-entries-from-agenda)
6943 (org-refile goto buffer rfloc)))))
6944 (unless no-update (org-agenda-redo))))
6946 (defun org-agenda-open-link (&optional arg)
6947 "Follow the link in the current line, if any.
6948 This looks for a link in the displayed line in the agenda. It also looks
6949 at the text of the entry itself."
6950 (interactive "P")
6951 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6952 (org-get-at-bol 'org-marker)))
6953 (buffer (and marker (marker-buffer marker)))
6954 (prefix (buffer-substring
6955 (point-at-bol) (point-at-eol))))
6956 (cond
6957 (buffer
6958 (with-current-buffer buffer
6959 (save-excursion
6960 (save-restriction
6961 (widen)
6962 (goto-char marker)
6963 (org-offer-links-in-entry arg prefix)))))
6964 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6965 (save-excursion
6966 (beginning-of-line 1)
6967 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6968 (org-open-link-from-string (match-string 1)))
6969 (t (error "No link to open here")))))
6971 (defun org-agenda-copy-local-variable (var)
6972 "Get a variable from a referenced buffer and install it here."
6973 (let ((m (org-get-at-bol 'org-marker)))
6974 (when (and m (buffer-live-p (marker-buffer m)))
6975 (org-set-local var (with-current-buffer (marker-buffer m)
6976 (symbol-value var))))))
6978 (defun org-agenda-switch-to (&optional delete-other-windows)
6979 "Go to the Org-mode file which contains the item at point."
6980 (interactive)
6981 (if (and org-return-follows-link
6982 (not (org-get-at-bol 'org-marker))
6983 (org-in-regexp org-bracket-link-regexp))
6984 (org-open-link-from-string (match-string 0))
6985 (let* ((marker (or (org-get-at-bol 'org-marker)
6986 (org-agenda-error)))
6987 (buffer (marker-buffer marker))
6988 (pos (marker-position marker)))
6989 (org-pop-to-buffer-same-window buffer)
6990 (and delete-other-windows (delete-other-windows))
6991 (widen)
6992 (goto-char pos)
6993 (when (org-mode-p)
6994 (org-show-context 'agenda)
6995 (save-excursion
6996 (and (outline-next-heading)
6997 (org-flag-heading nil))) ; show the next heading
6998 (when (outline-invisible-p)
6999 (show-entry)))))) ; display invisible text
7001 (defun org-agenda-goto-mouse (ev)
7002 "Go to the Org-mode file which contains the item at the mouse click."
7003 (interactive "e")
7004 (mouse-set-point ev)
7005 (org-agenda-goto))
7007 (defun org-agenda-show (&optional full-entry)
7008 "Display the Org-mode file which contains the item at point.
7009 With prefix argument FULL-ENTRY, make the entire entry visible
7010 if it was hidden in the outline."
7011 (interactive "P")
7012 (let ((win (selected-window)))
7013 (if full-entry
7014 (let ((org-show-entry-below t))
7015 (org-agenda-goto t))
7016 (org-agenda-goto t))
7017 (select-window win)))
7019 (defvar org-agenda-show-window nil)
7020 (defun org-agenda-show-and-scroll-up ()
7021 "Display the Org-mode file which contains the item at point.
7022 When called repeatedly, scroll the window that is displaying the buffer."
7023 (interactive)
7024 (let ((win (selected-window)))
7025 (if (and (window-live-p org-agenda-show-window)
7026 (eq this-command last-command))
7027 (progn
7028 (select-window org-agenda-show-window)
7029 (ignore-errors (scroll-up)))
7030 (org-agenda-goto t)
7031 (show-subtree)
7032 (setq org-agenda-show-window (selected-window)))
7033 (select-window win)))
7035 (defun org-agenda-show-scroll-down ()
7036 "Scroll down the window showing the agenda."
7037 (interactive)
7038 (let ((win (selected-window)))
7039 (when (window-live-p org-agenda-show-window)
7040 (select-window org-agenda-show-window)
7041 (ignore-errors (scroll-down))
7042 (select-window win))))
7044 (defun org-agenda-show-1 (&optional more)
7045 "Display the Org-mode file which contains the item at point.
7046 The prefix arg selects the amount of information to display:
7048 0 hide the subtree
7049 1 just show the entry according to defaults.
7050 2 show the children view
7051 3 show the subtree view
7052 4 show the entire subtree and any LOGBOOK drawers
7053 5 show the entire subtree and any drawers
7054 With prefix argument FULL-ENTRY, make the entire entry visible
7055 if it was hidden in the outline."
7056 (interactive "p")
7057 (let ((win (selected-window)))
7058 (org-agenda-goto t)
7059 (org-recenter-heading 1)
7060 (cond
7061 ((= more 0)
7062 (hide-subtree)
7063 (save-excursion
7064 (org-back-to-heading)
7065 (run-hook-with-args 'org-cycle-hook 'folded))
7066 (message "Remote: FOLDED"))
7067 ((and (org-called-interactively-p 'any) (= more 1))
7068 (message "Remote: show with default settings"))
7069 ((= more 2)
7070 (show-entry)
7071 (show-children)
7072 (save-excursion
7073 (org-back-to-heading)
7074 (run-hook-with-args 'org-cycle-hook 'children))
7075 (message "Remote: CHILDREN"))
7076 ((= more 3)
7077 (show-subtree)
7078 (save-excursion
7079 (org-back-to-heading)
7080 (run-hook-with-args 'org-cycle-hook 'subtree))
7081 (message "Remote: SUBTREE"))
7082 ((= more 4)
7083 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7084 (org-drawer-regexp
7085 (concat "^[ \t]*:\\("
7086 (mapconcat 'regexp-quote org-drawers "\\|")
7087 "\\):[ \t]*$")))
7088 (show-subtree)
7089 (save-excursion
7090 (org-back-to-heading)
7091 (org-cycle-hide-drawers 'subtree)))
7092 (message "Remote: SUBTREE AND LOGBOOK"))
7093 ((> more 4)
7094 (show-subtree)
7095 (message "Remote: SUBTREE AND ALL DRAWERS")))
7096 (select-window win)))
7098 (defun org-recenter-heading (n)
7099 (save-excursion
7100 (org-back-to-heading)
7101 (recenter n)))
7103 (defvar org-agenda-cycle-counter nil)
7104 (defun org-agenda-cycle-show (&optional n)
7105 "Show the current entry in another window, with default settings.
7106 Default settings are taken from `org-show-hierarchy-above' and siblings.
7107 When use repeatedly in immediate succession, the remote entry will cycle
7108 through visibility
7110 children -> subtree -> folded
7112 When called with a numeric prefix arg, that arg will be passed through to
7113 `org-agenda-show-1'. For the interpretation of that argument, see the
7114 docstring of `org-agenda-show-1'."
7115 (interactive "P")
7116 (if (integerp n)
7117 (setq org-agenda-cycle-counter n)
7118 (if (not (eq last-command this-command))
7119 (setq org-agenda-cycle-counter 1)
7120 (if (equal org-agenda-cycle-counter 0)
7121 (setq org-agenda-cycle-counter 2)
7122 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7123 (if (> org-agenda-cycle-counter 3)
7124 (setq org-agenda-cycle-counter 0)))))
7125 (org-agenda-show-1 org-agenda-cycle-counter))
7127 (defun org-agenda-recenter (arg)
7128 "Display the Org-mode file which contains the item at point and recenter."
7129 (interactive "P")
7130 (let ((win (selected-window)))
7131 (org-agenda-goto t)
7132 (recenter arg)
7133 (select-window win)))
7135 (defun org-agenda-show-mouse (ev)
7136 "Display the Org-mode file which contains the item at the mouse click."
7137 (interactive "e")
7138 (mouse-set-point ev)
7139 (org-agenda-show))
7141 (defun org-agenda-check-no-diary ()
7142 "Check if the entry is a diary link and abort if yes."
7143 (if (org-get-at-bol 'org-agenda-diary-link)
7144 (org-agenda-error)))
7146 (defun org-agenda-error ()
7147 (error "Command not allowed in this line"))
7149 (defun org-agenda-tree-to-indirect-buffer ()
7150 "Show the subtree corresponding to the current entry in an indirect buffer.
7151 This calls the command `org-tree-to-indirect-buffer' from the original
7152 Org-mode buffer.
7153 With numerical prefix arg ARG, go up to this level and then take that tree.
7154 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7155 use the dedicated frame)."
7156 (interactive)
7157 (org-agenda-check-no-diary)
7158 (let* ((marker (or (org-get-at-bol 'org-marker)
7159 (org-agenda-error)))
7160 (buffer (marker-buffer marker))
7161 (pos (marker-position marker)))
7162 (with-current-buffer buffer
7163 (save-excursion
7164 (goto-char pos)
7165 (call-interactively 'org-tree-to-indirect-buffer)))))
7167 (defvar org-last-heading-marker (make-marker)
7168 "Marker pointing to the headline that last changed its TODO state
7169 by a remote command from the agenda.")
7171 (defun org-agenda-todo-nextset ()
7172 "Switch TODO entry to next sequence."
7173 (interactive)
7174 (org-agenda-todo 'nextset))
7176 (defun org-agenda-todo-previousset ()
7177 "Switch TODO entry to previous sequence."
7178 (interactive)
7179 (org-agenda-todo 'previousset))
7181 (defun org-agenda-todo (&optional arg)
7182 "Cycle TODO state of line at point, also in Org-mode file.
7183 This changes the line at point, all other lines in the agenda referring to
7184 the same tree node, and the headline of the tree node in the Org-mode file."
7185 (interactive "P")
7186 (org-agenda-check-no-diary)
7187 (let* ((col (current-column))
7188 (marker (or (org-get-at-bol 'org-marker)
7189 (org-agenda-error)))
7190 (buffer (marker-buffer marker))
7191 (pos (marker-position marker))
7192 (hdmarker (org-get-at-bol 'org-hd-marker))
7193 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7194 (inhibit-read-only t)
7195 org-agenda-headline-snapshot-before-repeat newhead just-one)
7196 (org-with-remote-undo buffer
7197 (with-current-buffer buffer
7198 (widen)
7199 (goto-char pos)
7200 (org-show-context 'agenda)
7201 (save-excursion
7202 (and (outline-next-heading)
7203 (org-flag-heading nil))) ; show the next heading
7204 (let ((current-prefix-arg arg))
7205 (call-interactively 'org-todo))
7206 (and (bolp) (forward-char 1))
7207 (setq newhead (org-get-heading))
7208 (when (and (org-bound-and-true-p
7209 org-agenda-headline-snapshot-before-repeat)
7210 (not (equal org-agenda-headline-snapshot-before-repeat
7211 newhead))
7212 todayp)
7213 (setq newhead org-agenda-headline-snapshot-before-repeat
7214 just-one t))
7215 (save-excursion
7216 (org-back-to-heading)
7217 (move-marker org-last-heading-marker (point))))
7218 (beginning-of-line 1)
7219 (save-excursion
7220 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7221 (org-move-to-column col))))
7223 (defun org-agenda-add-note (&optional arg)
7224 "Add a time-stamped note to the entry at point."
7225 (interactive "P")
7226 (org-agenda-check-no-diary)
7227 (let* ((marker (or (org-get-at-bol 'org-marker)
7228 (org-agenda-error)))
7229 (buffer (marker-buffer marker))
7230 (pos (marker-position marker))
7231 (hdmarker (org-get-at-bol 'org-hd-marker))
7232 (inhibit-read-only t))
7233 (with-current-buffer buffer
7234 (widen)
7235 (goto-char pos)
7236 (org-show-context 'agenda)
7237 (save-excursion
7238 (and (outline-next-heading)
7239 (org-flag-heading nil))) ; show the next heading
7240 (org-add-note))))
7242 (defun org-agenda-change-all-lines (newhead hdmarker
7243 &optional fixface just-this)
7244 "Change all lines in the agenda buffer which match HDMARKER.
7245 The new content of the line will be NEWHEAD (as modified by
7246 `org-format-agenda-item'). HDMARKER is checked with
7247 `equal' against all `org-hd-marker' text properties in the file.
7248 If FIXFACE is non-nil, the face of each item is modified according to
7249 the new TODO state.
7250 If JUST-THIS is non-nil, change just the current line, not all.
7251 If FORCE-TAGS is non nil, the car of it returns the new tags."
7252 (let* ((inhibit-read-only t)
7253 (line (org-current-line))
7254 (thetags (with-current-buffer (marker-buffer hdmarker)
7255 (save-excursion (save-restriction (widen)
7256 (goto-char hdmarker)
7257 (org-get-tags-at)))))
7258 props m pl undone-face done-face finish new dotime cat tags)
7259 (save-excursion
7260 (goto-char (point-max))
7261 (beginning-of-line 1)
7262 (while (not finish)
7263 (setq finish (bobp))
7264 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7265 (or (not just-this) (= (org-current-line) line))
7266 (equal m hdmarker))
7267 (setq props (text-properties-at (point))
7268 dotime (org-get-at-bol 'dotime)
7269 cat (org-get-at-bol 'org-category)
7270 tags thetags
7272 (let ((org-prefix-format-compiled
7273 (or (get-text-property (point) 'format)
7274 org-prefix-format-compiled)))
7275 (with-current-buffer (marker-buffer hdmarker)
7276 (save-excursion
7277 (save-restriction
7278 (widen)
7279 (org-format-agenda-item (org-get-at-bol 'extra)
7280 newhead cat tags dotime)))))
7281 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7282 undone-face (org-get-at-bol 'undone-face)
7283 done-face (org-get-at-bol 'done-face))
7284 (beginning-of-line 1)
7285 (cond
7286 ((equal new "")
7287 (and (looking-at ".*\n?") (replace-match "")))
7288 ((looking-at ".*")
7289 (replace-match new t t)
7290 (beginning-of-line 1)
7291 (add-text-properties (point-at-bol) (point-at-eol) props)
7292 (when fixface
7293 (add-text-properties
7294 (point-at-bol) (point-at-eol)
7295 (list 'face
7296 (if org-last-todo-state-is-todo
7297 undone-face done-face))))
7298 (org-agenda-highlight-todo 'line)
7299 (beginning-of-line 1))
7300 (t (error "Line update did not work"))))
7301 (beginning-of-line 0)))
7302 (org-finalize-agenda)))
7304 (defun org-agenda-align-tags (&optional line)
7305 "Align all tags in agenda items to `org-agenda-tags-column'."
7306 (let ((inhibit-read-only t) l c)
7307 (save-excursion
7308 (goto-char (if line (point-at-bol) (point-min)))
7309 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7310 (if line (point-at-eol) nil) t)
7311 (add-text-properties
7312 (match-beginning 2) (match-end 2)
7313 (list 'face (delq nil (let ((prop (get-text-property
7314 (match-beginning 2) 'face)))
7315 (or (listp prop) (setq prop (list prop)))
7316 (if (memq 'org-tag prop)
7317 prop
7318 (cons 'org-tag prop))))))
7319 (setq l (- (match-end 2) (match-beginning 2))
7320 c (if (< org-agenda-tags-column 0)
7321 (- (abs org-agenda-tags-column) l)
7322 org-agenda-tags-column))
7323 (delete-region (match-beginning 1) (match-end 1))
7324 (goto-char (match-beginning 1))
7325 (insert (org-add-props
7326 (make-string (max 1 (- c (current-column))) ?\ )
7327 (plist-put (copy-sequence (text-properties-at (point)))
7328 'face nil))))
7329 (goto-char (point-min))
7330 (org-font-lock-add-tag-faces (point-max)))))
7332 (defun org-agenda-priority-up ()
7333 "Increase the priority of line at point, also in Org-mode file."
7334 (interactive)
7335 (org-agenda-priority 'up))
7337 (defun org-agenda-priority-down ()
7338 "Decrease the priority of line at point, also in Org-mode file."
7339 (interactive)
7340 (org-agenda-priority 'down))
7342 (defun org-agenda-priority (&optional force-direction)
7343 "Set the priority of line at point, also in Org-mode file.
7344 This changes the line at point, all other lines in the agenda referring to
7345 the same tree node, and the headline of the tree node in the Org-mode file."
7346 (interactive)
7347 (unless org-enable-priority-commands
7348 (error "Priority commands are disabled"))
7349 (org-agenda-check-no-diary)
7350 (let* ((marker (or (org-get-at-bol 'org-marker)
7351 (org-agenda-error)))
7352 (hdmarker (org-get-at-bol 'org-hd-marker))
7353 (buffer (marker-buffer hdmarker))
7354 (pos (marker-position hdmarker))
7355 (inhibit-read-only t)
7356 newhead)
7357 (org-with-remote-undo buffer
7358 (with-current-buffer buffer
7359 (widen)
7360 (goto-char pos)
7361 (org-show-context 'agenda)
7362 (save-excursion
7363 (and (outline-next-heading)
7364 (org-flag-heading nil))) ; show the next heading
7365 (funcall 'org-priority force-direction)
7366 (end-of-line 1)
7367 (setq newhead (org-get-heading)))
7368 (org-agenda-change-all-lines newhead hdmarker)
7369 (beginning-of-line 1))))
7371 ;; FIXME: should fix the tags property of the agenda line.
7372 (defun org-agenda-set-tags (&optional tag onoff)
7373 "Set tags for the current headline."
7374 (interactive)
7375 (org-agenda-check-no-diary)
7376 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7377 (call-interactively 'org-change-tag-in-region)
7378 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7379 (org-agenda-error)))
7380 (buffer (marker-buffer hdmarker))
7381 (pos (marker-position hdmarker))
7382 (inhibit-read-only t)
7383 newhead)
7384 (org-with-remote-undo buffer
7385 (with-current-buffer buffer
7386 (widen)
7387 (goto-char pos)
7388 (save-excursion
7389 (org-show-context 'agenda))
7390 (save-excursion
7391 (and (outline-next-heading)
7392 (org-flag-heading nil))) ; show the next heading
7393 (goto-char pos)
7394 (if tag
7395 (org-toggle-tag tag onoff)
7396 (call-interactively 'org-set-tags))
7397 (end-of-line 1)
7398 (setq newhead (org-get-heading)))
7399 (org-agenda-change-all-lines newhead hdmarker)
7400 (beginning-of-line 1)))))
7402 (defun org-agenda-set-property ()
7403 "Set a property for the current headline."
7404 (interactive)
7405 (org-agenda-check-no-diary)
7406 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7407 (org-agenda-error)))
7408 (buffer (marker-buffer hdmarker))
7409 (pos (marker-position hdmarker))
7410 (inhibit-read-only t)
7411 newhead)
7412 (org-with-remote-undo buffer
7413 (with-current-buffer buffer
7414 (widen)
7415 (goto-char pos)
7416 (save-excursion
7417 (org-show-context 'agenda))
7418 (save-excursion
7419 (and (outline-next-heading)
7420 (org-flag-heading nil))) ; show the next heading
7421 (goto-char pos)
7422 (call-interactively 'org-set-property)))))
7424 (defun org-agenda-set-effort ()
7425 "Set the effort property for the current headline."
7426 (interactive)
7427 (org-agenda-check-no-diary)
7428 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7429 (org-agenda-error)))
7430 (buffer (marker-buffer hdmarker))
7431 (pos (marker-position hdmarker))
7432 (inhibit-read-only t)
7433 newhead)
7434 (org-with-remote-undo buffer
7435 (with-current-buffer buffer
7436 (widen)
7437 (goto-char pos)
7438 (save-excursion
7439 (org-show-context 'agenda))
7440 (save-excursion
7441 (and (outline-next-heading)
7442 (org-flag-heading nil))) ; show the next heading
7443 (goto-char pos)
7444 (call-interactively 'org-set-effort)
7445 (end-of-line 1)
7446 (setq newhead (org-get-heading)))
7447 (org-agenda-change-all-lines newhead hdmarker))))
7449 (defun org-agenda-toggle-archive-tag ()
7450 "Toggle the archive tag for the current entry."
7451 (interactive)
7452 (org-agenda-check-no-diary)
7453 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7454 (org-agenda-error)))
7455 (buffer (marker-buffer hdmarker))
7456 (pos (marker-position hdmarker))
7457 (inhibit-read-only t)
7458 newhead)
7459 (org-with-remote-undo buffer
7460 (with-current-buffer buffer
7461 (widen)
7462 (goto-char pos)
7463 (org-show-context 'agenda)
7464 (save-excursion
7465 (and (outline-next-heading)
7466 (org-flag-heading nil))) ; show the next heading
7467 (call-interactively 'org-toggle-archive-tag)
7468 (end-of-line 1)
7469 (setq newhead (org-get-heading)))
7470 (org-agenda-change-all-lines newhead hdmarker)
7471 (beginning-of-line 1))))
7473 (defun org-agenda-do-date-later (arg)
7474 (interactive "P")
7475 (cond
7476 ((or (equal arg '(16))
7477 (memq last-command
7478 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7479 (setq this-command 'org-agenda-date-later-minutes)
7480 (org-agenda-date-later-minutes 1))
7481 ((or (equal arg '(4))
7482 (memq last-command
7483 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7484 (setq this-command 'org-agenda-date-later-hours)
7485 (org-agenda-date-later-hours 1))
7487 (org-agenda-date-later (prefix-numeric-value arg)))))
7489 (defun org-agenda-do-date-earlier (arg)
7490 (interactive "P")
7491 (cond
7492 ((or (equal arg '(16))
7493 (memq last-command
7494 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7495 (setq this-command 'org-agenda-date-earlier-minutes)
7496 (org-agenda-date-earlier-minutes 1))
7497 ((or (equal arg '(4))
7498 (memq last-command
7499 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7500 (setq this-command 'org-agenda-date-earlier-hours)
7501 (org-agenda-date-earlier-hours 1))
7503 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7505 (defun org-agenda-date-later (arg &optional what)
7506 "Change the date of this item to ARG day(s) later."
7507 (interactive "p")
7508 (org-agenda-check-type t 'agenda 'timeline)
7509 (org-agenda-check-no-diary)
7510 (let* ((marker (or (org-get-at-bol 'org-marker)
7511 (org-agenda-error)))
7512 (buffer (marker-buffer marker))
7513 (pos (marker-position marker)))
7514 (org-with-remote-undo buffer
7515 (with-current-buffer buffer
7516 (widen)
7517 (goto-char pos)
7518 (if (not (org-at-timestamp-p))
7519 (error "Cannot find time stamp"))
7520 (org-timestamp-change arg (or what 'day)))
7521 (org-agenda-show-new-time marker org-last-changed-timestamp))
7522 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7524 (defun org-agenda-date-earlier (arg &optional what)
7525 "Change the date of this item to ARG day(s) earlier."
7526 (interactive "p")
7527 (org-agenda-date-later (- arg) what))
7529 (defun org-agenda-date-later-minutes (arg)
7530 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7531 (interactive "p")
7532 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7533 (org-agenda-date-later arg 'minute))
7535 (defun org-agenda-date-earlier-minutes (arg)
7536 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7537 (interactive "p")
7538 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7539 (org-agenda-date-earlier arg 'minute))
7541 (defun org-agenda-date-later-hours (arg)
7542 "Change the time of this item, in hour steps."
7543 (interactive "p")
7544 (org-agenda-date-later arg 'hour))
7546 (defun org-agenda-date-earlier-hours (arg)
7547 "Change the time of this item, in hour steps."
7548 (interactive "p")
7549 (org-agenda-date-earlier arg 'hour))
7551 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7552 "Show new date stamp via text properties."
7553 ;; We use text properties to make this undoable
7554 (let ((inhibit-read-only t)
7555 (buffer-invisibility-spec))
7556 (setq stamp (concat " " prefix " => " stamp))
7557 (save-excursion
7558 (goto-char (point-max))
7559 (while (not (bobp))
7560 (when (equal marker (org-get-at-bol 'org-marker))
7561 (org-move-to-column (- (window-width) (length stamp)) t)
7562 (org-agenda-fix-tags-filter-overlays-at (point))
7563 (if (featurep 'xemacs)
7564 ;; Use `duplicable' property to trigger undo recording
7565 (let ((ex (make-extent nil nil))
7566 (gl (make-glyph stamp)))
7567 (set-glyph-face gl 'secondary-selection)
7568 (set-extent-properties
7569 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7570 (insert-extent ex (1- (point)) (point-at-eol)))
7571 (add-text-properties
7572 (1- (point)) (point-at-eol)
7573 (list 'display (org-add-props stamp nil
7574 'face 'secondary-selection))))
7575 (beginning-of-line 1))
7576 (beginning-of-line 0)))))
7578 (defun org-agenda-date-prompt (arg)
7579 "Change the date of this item. Date is prompted for, with default today.
7580 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7581 be used to request time specification in the time stamp."
7582 (interactive "P")
7583 (org-agenda-check-type t 'agenda 'timeline)
7584 (org-agenda-check-no-diary)
7585 (let* ((marker (or (org-get-at-bol 'org-marker)
7586 (org-agenda-error)))
7587 (buffer (marker-buffer marker))
7588 (pos (marker-position marker)))
7589 (org-with-remote-undo buffer
7590 (with-current-buffer buffer
7591 (widen)
7592 (goto-char pos)
7593 (if (not (org-at-timestamp-p t))
7594 (error "Cannot find time stamp"))
7595 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7596 (org-agenda-show-new-time marker org-last-changed-timestamp))
7597 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7599 (defun org-agenda-schedule (arg &optional time)
7600 "Schedule the item at point.
7601 ARG is passed through to `org-schedule'."
7602 (interactive "P")
7603 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7604 (org-agenda-check-no-diary)
7605 (let* ((marker (or (org-get-at-bol 'org-marker)
7606 (org-agenda-error)))
7607 (type (marker-insertion-type marker))
7608 (buffer (marker-buffer marker))
7609 (pos (marker-position marker))
7610 (org-insert-labeled-timestamps-at-point nil)
7612 (set-marker-insertion-type marker t)
7613 (org-with-remote-undo buffer
7614 (with-current-buffer buffer
7615 (widen)
7616 (goto-char pos)
7617 (setq ts (org-schedule arg time)))
7618 (org-agenda-show-new-time marker ts "S"))
7619 (message "Item scheduled for %s" ts)))
7621 (defun org-agenda-deadline (arg &optional time)
7622 "Schedule the item at point.
7623 ARG is passed through to `org-deadline'."
7624 (interactive "P")
7625 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7626 (org-agenda-check-no-diary)
7627 (let* ((marker (or (org-get-at-bol 'org-marker)
7628 (org-agenda-error)))
7629 (buffer (marker-buffer marker))
7630 (pos (marker-position marker))
7631 (org-insert-labeled-timestamps-at-point nil)
7633 (org-with-remote-undo buffer
7634 (with-current-buffer buffer
7635 (widen)
7636 (goto-char pos)
7637 (setq ts (org-deadline arg time)))
7638 (org-agenda-show-new-time marker ts "D"))
7639 (message "Deadline for this item set to %s" ts)))
7641 (defun org-agenda-action ()
7642 "Select entry for agenda action, or execute an agenda action.
7643 This command prompts for another letter. Valid inputs are:
7645 m Mark the entry at point for an agenda action
7646 s Schedule the marked entry to the date at the cursor
7647 d Set the deadline of the marked entry to the date at the cursor
7648 r Call `org-remember' with cursor date as the default date
7649 c Call `org-capture' with cursor date as the default date
7650 SPC Show marked entry in other window
7651 TAB Visit marked entry in other window
7653 The cursor may be at a date in the calendar, or in the Org agenda."
7654 (interactive)
7655 (let (ans)
7656 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7657 (setq ans (read-char-exclusive))
7658 (cond
7659 ((equal ans ?m)
7660 ;; Mark this entry
7661 (if (eq major-mode 'org-agenda-mode)
7662 (let ((m (or (org-get-at-bol 'org-hd-marker)
7663 (org-get-at-bol 'org-marker))))
7664 (if m
7665 (progn
7666 (move-marker org-agenda-action-marker
7667 (marker-position m) (marker-buffer m))
7668 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7669 (error "Don't know which entry to mark")))
7670 (error "This command works only in the agenda")))
7671 ((equal ans ?s)
7672 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7673 ((equal ans ?d)
7674 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7675 ((equal ans ?r)
7676 (org-agenda-do-action '(org-remember) t))
7677 ((equal ans ?c)
7678 (org-agenda-do-action '(org-capture) t))
7679 ((equal ans ?\ )
7680 (let ((cw (selected-window)))
7681 (org-switch-to-buffer-other-window
7682 (marker-buffer org-agenda-action-marker))
7683 (goto-char org-agenda-action-marker)
7684 (org-show-context 'agenda)
7685 (select-window cw)))
7686 ((equal ans ?\C-i)
7687 (org-switch-to-buffer-other-window
7688 (marker-buffer org-agenda-action-marker))
7689 (goto-char org-agenda-action-marker)
7690 (org-show-context 'agenda))
7691 (t (error "Invalid agenda action %c" ans)))))
7693 (defun org-agenda-do-action (form &optional current-buffer)
7694 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7695 (let ((org-overriding-default-time (org-get-cursor-date)))
7696 (if current-buffer
7697 (eval form)
7698 (if (not (marker-buffer org-agenda-action-marker))
7699 (error "No entry has been selected for agenda action")
7700 (with-current-buffer (marker-buffer org-agenda-action-marker)
7701 (save-excursion
7702 (save-restriction
7703 (widen)
7704 (goto-char org-agenda-action-marker)
7705 (eval form))))))))
7707 (defun org-agenda-clock-in (&optional arg)
7708 "Start the clock on the currently selected item."
7709 (interactive "P")
7710 (org-agenda-check-no-diary)
7711 (if (equal arg '(4))
7712 (org-clock-in arg)
7713 (let* ((marker (or (org-get-at-bol 'org-marker)
7714 (org-agenda-error)))
7715 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7716 marker))
7717 (pos (marker-position marker))
7718 newhead)
7719 (org-with-remote-undo (marker-buffer marker)
7720 (with-current-buffer (marker-buffer marker)
7721 (widen)
7722 (goto-char pos)
7723 (org-show-context 'agenda)
7724 (org-show-entry)
7725 (org-cycle-hide-drawers 'children)
7726 (org-clock-in arg)
7727 (setq newhead (org-get-heading)))
7728 (org-agenda-change-all-lines newhead hdmarker)))))
7730 (defun org-agenda-clock-out ()
7731 "Stop the currently running clock."
7732 (interactive)
7733 (unless (marker-buffer org-clock-marker)
7734 (error "No running clock"))
7735 (let ((marker (make-marker)) newhead)
7736 (org-with-remote-undo (marker-buffer org-clock-marker)
7737 (with-current-buffer (marker-buffer org-clock-marker)
7738 (save-excursion
7739 (save-restriction
7740 (widen)
7741 (goto-char org-clock-marker)
7742 (org-back-to-heading t)
7743 (move-marker marker (point))
7744 (org-clock-out)
7745 (setq newhead (org-get-heading))))))
7746 (org-agenda-change-all-lines newhead marker)
7747 (move-marker marker nil)))
7749 (defun org-agenda-clock-cancel (&optional arg)
7750 "Cancel the currently running clock."
7751 (interactive "P")
7752 (unless (marker-buffer org-clock-marker)
7753 (error "No running clock"))
7754 (org-with-remote-undo (marker-buffer org-clock-marker)
7755 (org-clock-cancel)))
7757 (defun org-agenda-clock-goto ()
7758 "Jump to the currently clocked in task within the agenda.
7759 If the currently clocked in task is not listed in the agenda
7760 buffer, display it in another window."
7761 (interactive)
7762 (let (pos)
7763 (mapc (lambda (o)
7764 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7765 (setq pos (overlay-start o))))
7766 (overlays-in (point-min) (point-max)))
7767 (cond (pos (goto-char pos))
7768 ;; If the currently clocked entry is not in the agenda
7769 ;; buffer, we visit it in another window:
7770 (org-clock-current-task
7771 (org-switch-to-buffer-other-window (org-clock-goto)))
7772 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7774 (defun org-agenda-diary-entry-in-org-file ()
7775 "Make a diary entry in the file `org-agenda-diary-file'."
7776 (let (d1 d2 char (text "") dp1 dp2)
7777 (if (equal (buffer-name) "*Calendar*")
7778 (setq d1 (calendar-cursor-to-date t)
7779 d2 (car calendar-mark-ring))
7780 (setq dp1 (get-text-property (point-at-bol) 'day))
7781 (unless dp1 (error "No date defined in current line"))
7782 (setq d1 (calendar-gregorian-from-absolute dp1)
7783 d2 (and (ignore-errors (mark))
7784 (save-excursion
7785 (goto-char (mark))
7786 (setq dp2 (get-text-property (point-at-bol) 'day)))
7787 (calendar-gregorian-from-absolute dp2))))
7788 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7789 (setq char (read-char-exclusive))
7790 (cond
7791 ((equal char ?d)
7792 (setq text (read-string "Day entry: "))
7793 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7794 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7795 ((equal char ?a)
7796 (setq d1 (list (car d1) (nth 1 d1)
7797 (read-number (format "Reference year [%d]: " (nth 2 d1))
7798 (nth 2 d1))))
7799 (setq text (read-string "Anniversary (use %d to show years): "))
7800 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7801 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7802 ((equal char ?b)
7803 (setq text (read-string "Block entry: "))
7804 (unless (and d1 d2 (not (equal d1 d2)))
7805 (error "No block of days selected"))
7806 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7807 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7808 ((equal char ?j)
7809 (org-switch-to-buffer-other-window
7810 (find-file-noselect org-agenda-diary-file))
7811 (require 'org-datetree)
7812 (org-datetree-find-date-create d1)
7813 (org-reveal t))
7814 (t (error "Invalid selection character `%c'" char)))))
7816 (defcustom org-agenda-insert-diary-strategy 'date-tree
7817 "Where in `org-agenda-diary-file' should new entries be added?
7818 Valid values:
7820 date-tree in the date tree, as child of the date
7821 top-level as top-level entries at the end of the file."
7822 :group 'org-agenda
7823 :type '(choice
7824 (const :tag "in a date tree" date-tree)
7825 (const :tag "as top level at end of file" top-level)))
7827 (defcustom org-agenda-insert-diary-extract-time nil
7828 "Non-nil means extract any time specification from the diary entry."
7829 :group 'org-agenda
7830 :type 'boolean)
7832 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7833 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7834 If TEXT is not empty, it will become the headline of the new entry, and
7835 the resulting entry will not be shown. When TEXT is empty, switch to
7836 `org-agenda-diary-file' and let the user finish the entry there."
7837 (let ((cw (current-window-configuration)))
7838 (org-switch-to-buffer-other-window
7839 (find-file-noselect org-agenda-diary-file))
7840 (widen)
7841 (goto-char (point-min))
7842 (cond
7843 ((eq type 'anniversary)
7844 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7845 (progn
7846 (or (org-on-heading-p t)
7847 (progn
7848 (outline-next-heading)
7849 (insert "* Anniversaries\n\n")
7850 (beginning-of-line -1)))))
7851 (outline-next-heading)
7852 (org-back-over-empty-lines)
7853 (backward-char 1)
7854 (insert "\n")
7855 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7856 (nth 2 d1) (car d1) (nth 1 d1) text)))
7857 ((eq type 'day)
7858 (let ((org-prefix-has-time t)
7859 (org-agenda-time-leading-zero t)
7860 fmt time time2)
7861 (if org-agenda-insert-diary-extract-time
7862 ;; Use org-format-agenda-item to parse text for a time-range and
7863 ;; remove it. FIXME: This is a hack, we should refactor
7864 ;; that function to make time extraction available separately
7865 (setq fmt (org-format-agenda-item nil text nil nil t)
7866 time (get-text-property 0 'time fmt)
7867 time2 (if (> (length time) 0)
7868 ;; split-string removes trailing ...... if
7869 ;; no end time given. First space
7870 ;; separates time from date.
7871 (concat " " (car (split-string time "\\.")))
7872 nil)
7873 text (get-text-property 0 'txt fmt)))
7874 (if (eq org-agenda-insert-diary-strategy 'top-level)
7875 (org-agenda-insert-diary-as-top-level text)
7876 (require 'org-datetree)
7877 (org-datetree-find-date-create d1)
7878 (org-agenda-insert-diary-make-new-entry text))
7879 (org-insert-time-stamp (org-time-from-absolute
7880 (calendar-absolute-from-gregorian d1))
7881 nil nil nil nil time2))
7882 (end-of-line 0))
7883 ((eq type 'block)
7884 (if (> (calendar-absolute-from-gregorian d1)
7885 (calendar-absolute-from-gregorian d2))
7886 (setq d1 (prog1 d2 (setq d2 d1))))
7887 (if (eq org-agenda-insert-diary-strategy 'top-level)
7888 (org-agenda-insert-diary-as-top-level text)
7889 (require 'org-datetree)
7890 (org-datetree-find-date-create d1)
7891 (org-agenda-insert-diary-make-new-entry text))
7892 (org-insert-time-stamp (org-time-from-absolute
7893 (calendar-absolute-from-gregorian d1)))
7894 (insert "--")
7895 (org-insert-time-stamp (org-time-from-absolute
7896 (calendar-absolute-from-gregorian d2)))
7897 (end-of-line 0)))
7898 (if (string-match "\\S-" text)
7899 (progn
7900 (set-window-configuration cw)
7901 (message "%s entry added to %s"
7902 (capitalize (symbol-name type))
7903 (abbreviate-file-name org-agenda-diary-file)))
7904 (org-reveal t)
7905 (message "Please finish entry here"))))
7907 (defun org-agenda-insert-diary-as-top-level (text)
7908 "Make new entry as a top-level entry at the end of the file.
7909 Add TEXT as headline, and position the cursor in the second line so that
7910 a timestamp can be added there."
7911 (widen)
7912 (goto-char (point-max))
7913 (or (bolp) (insert "\n"))
7914 (insert "* " text "\n")
7915 (if org-adapt-indentation (org-indent-to-column 2)))
7917 (defun org-agenda-insert-diary-make-new-entry (text)
7918 "Make new entry as last child of current entry.
7919 Add TEXT as headline, and position the cursor in the second line so that
7920 a timestamp can be added there."
7921 (let ((org-show-following-heading t)
7922 (org-show-siblings t)
7923 (org-show-hierarchy-above t)
7924 (org-show-entry-below t)
7925 col)
7926 (outline-next-heading)
7927 (org-back-over-empty-lines)
7928 (or (looking-at "[ \t]*$")
7929 (progn (insert "\n") (backward-char 1)))
7930 (org-insert-heading nil t)
7931 (org-do-demote)
7932 (setq col (current-column))
7933 (insert text "\n")
7934 (if org-adapt-indentation (org-indent-to-column col))
7935 (let ((org-show-following-heading t)
7936 (org-show-siblings t)
7937 (org-show-hierarchy-above t)
7938 (org-show-entry-below t))
7939 (org-show-context))))
7941 (defun org-agenda-diary-entry ()
7942 "Make a diary entry, like the `i' command from the calendar.
7943 All the standard commands work: block, weekly etc.
7944 When `org-agenda-diary-file' points to a file,
7945 `org-agenda-diary-entry-in-org-file' is called instead to create
7946 entries in that Org-mode file."
7947 (interactive)
7948 (org-agenda-check-type t 'agenda 'timeline)
7949 (if (not (eq org-agenda-diary-file 'diary-file))
7950 (org-agenda-diary-entry-in-org-file)
7951 (require 'diary-lib)
7952 (let* ((char (progn
7953 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7954 (read-char-exclusive)))
7955 (cmd (cdr (assoc char
7956 '((?d . insert-diary-entry)
7957 (?w . insert-weekly-diary-entry)
7958 (?m . insert-monthly-diary-entry)
7959 (?y . insert-yearly-diary-entry)
7960 (?a . insert-anniversary-diary-entry)
7961 (?b . insert-block-diary-entry)
7962 (?c . insert-cyclic-diary-entry)))))
7963 (oldf (symbol-function 'calendar-cursor-to-date))
7964 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7965 (point (point))
7966 (mark (or (mark t) (point))))
7967 (unless cmd
7968 (error "No command associated with <%c>" char))
7969 (unless (and (get-text-property point 'day)
7970 (or (not (equal ?b char))
7971 (get-text-property mark 'day)))
7972 (error "Don't know which date to use for diary entry"))
7973 ;; We implement this by hacking the `calendar-cursor-to-date' function
7974 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7975 (let ((calendar-mark-ring
7976 (list (calendar-gregorian-from-absolute
7977 (or (get-text-property mark 'day)
7978 (get-text-property point 'day))))))
7979 (unwind-protect
7980 (progn
7981 (fset 'calendar-cursor-to-date
7982 (lambda (&optional error dummy)
7983 (calendar-gregorian-from-absolute
7984 (get-text-property point 'day))))
7985 (call-interactively cmd))
7986 (fset 'calendar-cursor-to-date oldf))))))
7988 (defun org-agenda-execute-calendar-command (cmd)
7989 "Execute a calendar command from the agenda, with the date associated to
7990 the cursor position."
7991 (org-agenda-check-type t 'agenda 'timeline)
7992 (require 'diary-lib)
7993 (unless (get-text-property (point) 'day)
7994 (error "Don't know which date to use for calendar command"))
7995 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7996 (point (point))
7997 (date (calendar-gregorian-from-absolute
7998 (get-text-property point 'day)))
7999 ;; the following 2 vars are needed in the calendar
8000 (displayed-month (car date))
8001 (displayed-year (nth 2 date)))
8002 (unwind-protect
8003 (progn
8004 (fset 'calendar-cursor-to-date
8005 (lambda (&optional error dummy)
8006 (calendar-gregorian-from-absolute
8007 (get-text-property point 'day))))
8008 (call-interactively cmd))
8009 (fset 'calendar-cursor-to-date oldf))))
8011 (defun org-agenda-phases-of-moon ()
8012 "Display the phases of the moon for the 3 months around the cursor date."
8013 (interactive)
8014 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8016 (defun org-agenda-holidays ()
8017 "Display the holidays for the 3 months around the cursor date."
8018 (interactive)
8019 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8021 (defvar calendar-longitude)
8022 (defvar calendar-latitude)
8023 (defvar calendar-location-name)
8025 (defun org-agenda-sunrise-sunset (arg)
8026 "Display sunrise and sunset for the cursor date.
8027 Latitude and longitude can be specified with the variables
8028 `calendar-latitude' and `calendar-longitude'. When called with prefix
8029 argument, latitude and longitude will be prompted for."
8030 (interactive "P")
8031 (require 'solar)
8032 (let ((calendar-longitude (if arg nil calendar-longitude))
8033 (calendar-latitude (if arg nil calendar-latitude))
8034 (calendar-location-name
8035 (if arg "the given coordinates" calendar-location-name)))
8036 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8038 (defun org-agenda-goto-calendar ()
8039 "Open the Emacs calendar with the date at the cursor."
8040 (interactive)
8041 (org-agenda-check-type t 'agenda 'timeline)
8042 (let* ((day (or (get-text-property (point) 'day)
8043 (error "Don't know which date to open in calendar")))
8044 (date (calendar-gregorian-from-absolute day))
8045 (calendar-move-hook nil)
8046 (calendar-view-holidays-initially-flag nil)
8047 (calendar-view-diary-initially-flag nil))
8048 (calendar)
8049 (calendar-goto-date date)))
8051 ;;;###autoload
8052 (defun org-calendar-goto-agenda ()
8053 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8054 This is a command that has to be installed in `calendar-mode-map'."
8055 (interactive)
8056 (org-agenda-list nil (calendar-absolute-from-gregorian
8057 (calendar-cursor-to-date))
8058 nil))
8060 (defun org-agenda-convert-date ()
8061 (interactive)
8062 (org-agenda-check-type t 'agenda 'timeline)
8063 (let ((day (get-text-property (point) 'day))
8064 date s)
8065 (unless day
8066 (error "Don't know which date to convert"))
8067 (setq date (calendar-gregorian-from-absolute day))
8068 (setq s (concat
8069 "Gregorian: " (calendar-date-string date) "\n"
8070 "ISO: " (calendar-iso-date-string date) "\n"
8071 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8072 "Julian: " (calendar-julian-date-string date) "\n"
8073 "Astron. JD: " (calendar-astro-date-string date)
8074 " (Julian date number at noon UTC)\n"
8075 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8076 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8077 "French: " (calendar-french-date-string date) "\n"
8078 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8079 "Mayan: " (calendar-mayan-date-string date) "\n"
8080 "Coptic: " (calendar-coptic-date-string date) "\n"
8081 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8082 "Persian: " (calendar-persian-date-string date) "\n"
8083 "Chinese: " (calendar-chinese-date-string date) "\n"))
8084 (with-output-to-temp-buffer "*Dates*"
8085 (princ s))
8086 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8088 ;;; Bulk commands
8090 (defvar org-agenda-bulk-marked-entries nil
8091 "List of markers that refer to marked entries in the agenda.")
8093 (defun org-agenda-bulk-marked-p ()
8094 (eq (get-char-property (point-at-bol) 'type)
8095 'org-marked-entry-overlay))
8097 (defun org-agenda-bulk-mark (&optional arg)
8098 "Mark the entry at point for future bulk action."
8099 (interactive "p")
8100 (dotimes (i (max arg 1))
8101 (unless (org-get-at-bol 'org-agenda-diary-link)
8102 (let* ((m (org-get-at-bol 'org-hd-marker))
8104 (unless (org-agenda-bulk-marked-p)
8105 (unless m (error "Nothing to mark at point"))
8106 (push m org-agenda-bulk-marked-entries)
8107 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8108 (org-overlay-display ov "> "
8109 (org-get-todo-face "TODO")
8110 'evaporate)
8111 (overlay-put ov 'type 'org-marked-entry-overlay))
8112 (beginning-of-line 2)
8113 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8114 (beginning-of-line 2))
8115 (message "%d entries marked for bulk action"
8116 (length org-agenda-bulk-marked-entries))))))
8118 (defun org-agenda-bulk-mark-regexp (regexp)
8119 "Mark entries match REGEXP."
8120 (interactive "sMark entries matching regexp: ")
8121 (let (entries-marked)
8122 (save-excursion
8123 (goto-char (point-min))
8124 (goto-char (next-single-property-change (point) 'txt))
8125 (while (re-search-forward regexp nil t)
8126 (when (string-match regexp (get-text-property (point) 'txt))
8127 (setq entries-marked (+ entries-marked 1))
8128 (call-interactively 'org-agenda-bulk-mark))))
8129 (if (not entries-marked)
8130 (message "No entry matching this regexp."))))
8132 (defun org-agenda-bulk-unmark ()
8133 "Unmark the entry at point for future bulk action."
8134 (interactive)
8135 (when (org-agenda-bulk-marked-p)
8136 (org-agenda-bulk-remove-overlays
8137 (point-at-bol) (+ 2 (point-at-bol)))
8138 (setq org-agenda-bulk-marked-entries
8139 (delete (org-get-at-bol 'org-hd-marker)
8140 org-agenda-bulk-marked-entries)))
8141 (beginning-of-line 2)
8142 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8143 (beginning-of-line 2))
8144 (message "%d entries marked for bulk action"
8145 (length org-agenda-bulk-marked-entries)))
8147 (defun org-agenda-bulk-toggle ()
8148 "Toggle marking the entry at point for bulk action."
8149 (interactive)
8150 (if (org-agenda-bulk-marked-p)
8151 (org-agenda-bulk-unmark)
8152 (org-agenda-bulk-mark)))
8154 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8155 "Remove the mark overlays between BEG and END in the agenda buffer.
8156 BEG and END default to the buffer limits.
8158 This only removes the overlays, it does not remove the markers
8159 from the list in `org-agenda-bulk-marked-entries'."
8160 (interactive)
8161 (mapc (lambda (ov)
8162 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8163 (delete-overlay ov)))
8164 (overlays-in (or beg (point-min)) (or end (point-max)))))
8166 (defun org-agenda-bulk-remove-all-marks ()
8167 "Remove all marks in the agenda buffer.
8168 This will remove the markers, and the overlays."
8169 (interactive)
8170 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8171 (setq org-agenda-bulk-marked-entries nil)
8172 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8174 (defun org-agenda-bulk-action (&optional arg)
8175 "Execute an remote-editing action on all marked entries.
8176 The prefix arg is passed through to the command if possible."
8177 (interactive "P")
8178 ;; Make sure we have markers, and only valid ones
8179 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8180 (mapc
8181 (lambda (m)
8182 (unless (and (markerp m)
8183 (marker-buffer m)
8184 (buffer-live-p (marker-buffer m))
8185 (marker-position m))
8186 (error "Marker %s for bulk command is invalid" m)))
8187 org-agenda-bulk-marked-entries)
8189 ;; Prompt for the bulk command
8190 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8191 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8192 (when org-agenda-bulk-custom-functions
8193 (concat " Custom: ["
8194 (mapconcat (lambda(f) (char-to-string (car f)))
8195 org-agenda-bulk-custom-functions "")
8196 "]"))))
8197 (let* ((action (read-char-exclusive))
8198 (org-log-refile (if org-log-refile 'time nil))
8199 (entries (reverse org-agenda-bulk-marked-entries))
8200 redo-at-end
8201 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8202 (cond
8203 ((equal action ?$)
8204 (setq cmd '(org-agenda-archive)))
8206 ((equal action ?A)
8207 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8209 ((member action '(?r ?w))
8210 (setq rfloc (org-refile-get-location
8211 "Refile to"
8212 (marker-buffer (car org-agenda-bulk-marked-entries))
8213 org-refile-allow-creating-parent-nodes))
8214 (if (nth 3 rfloc)
8215 (setcar (nthcdr 3 rfloc)
8216 (move-marker (make-marker) (nth 3 rfloc)
8217 (or (get-file-buffer (nth 1 rfloc))
8218 (find-buffer-visiting (nth 1 rfloc))
8219 (error "This should not happen")))))
8221 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8222 redo-at-end t))
8224 ((equal action ?t)
8225 (setq state (org-icompleting-read
8226 "Todo state: "
8227 (with-current-buffer (marker-buffer (car entries))
8228 (mapcar 'list org-todo-keywords-1))))
8229 (setq cmd `(let ((org-inhibit-blocking t)
8230 (org-inhibit-logging 'note))
8231 (org-agenda-todo ,state))))
8233 ((memq action '(?- ?+))
8234 (setq tag (org-icompleting-read
8235 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8236 (with-current-buffer (marker-buffer (car entries))
8237 (delq nil
8238 (mapcar (lambda (x)
8239 (if (stringp (car x)) x)) org-tag-alist)))))
8240 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8242 ((memq action '(?s ?d))
8243 (let* ((date (unless arg
8244 (org-read-date
8245 nil nil nil
8246 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8247 (ans (if arg nil org-read-date-final-answer))
8248 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8249 (setq cmd `(let* ((bound (fboundp 'read-string))
8250 (old (and bound (symbol-function 'read-string))))
8251 (unwind-protect
8252 (progn
8253 (fset 'read-string (lambda (&rest ignore) ,ans))
8254 (eval '(,c1 arg)))
8255 (if bound
8256 (fset 'read-string old)
8257 (fmakunbound 'read-string)))))))
8259 ((equal action ?S)
8260 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8261 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8262 (let ((days (read-number
8263 (format "Scatter tasks across how many %sdays: "
8264 (if arg "week" "")) 7)))
8265 (setq cmd
8266 `(let ((distance (1+ (random ,days))))
8267 (if arg
8268 (let ((dist distance)
8269 (day-of-week
8270 (calendar-day-of-week
8271 (calendar-gregorian-from-absolute (org-today)))))
8272 (dotimes (i (1+ dist))
8273 (while (member day-of-week org-agenda-weekend-days)
8274 (incf distance)
8275 (incf day-of-week)
8276 (if (= day-of-week 7)
8277 (setq day-of-week 0)))
8278 (incf day-of-week)
8279 (if (= day-of-week 7)
8280 (setq day-of-week 0)))))
8281 ;; silently fail when try to replan a sexp entry
8282 (condition-case nil
8283 (let* ((date (calendar-gregorian-from-absolute
8284 (+ (org-today) distance)))
8285 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8286 (nth 2 date))))
8287 (org-agenda-schedule nil time))
8288 (error nil)))))))
8290 ((assoc action org-agenda-bulk-custom-functions)
8291 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8292 redo-at-end t))
8294 ((equal action ?f)
8295 (setq cmd (list (intern
8296 (org-icompleting-read "Function: "
8297 obarray 'fboundp t nil nil)))))
8299 (t (error "Invalid bulk action")))
8301 ;; Sort the markers, to make sure that parents are handled before children
8302 (setq entries (sort entries
8303 (lambda (a b)
8304 (cond
8305 ((equal (marker-buffer a) (marker-buffer b))
8306 (< (marker-position a) (marker-position b)))
8308 (string< (buffer-name (marker-buffer a))
8309 (buffer-name (marker-buffer b))))))))
8311 ;; Now loop over all markers and apply cmd
8312 (while (setq e (pop entries))
8313 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8314 (if (not pos)
8315 (progn (message "Skipping removed entry at %s" e)
8316 (setq cntskip (1+ cntskip)))
8317 (goto-char pos)
8318 (eval cmd)
8319 (setq org-agenda-bulk-marked-entries
8320 (delete e org-agenda-bulk-marked-entries))
8321 (setq cnt (1+ cnt))))
8322 (setq org-agenda-bulk-marked-entries nil)
8323 (org-agenda-bulk-remove-all-marks)
8324 (when redo-at-end (org-agenda-redo))
8325 (message "Acted on %d entries%s"
8327 (if (= cntskip 0)
8329 (format ", skipped %d (disappeared before their turn)"
8330 cntskip)))))
8332 ;;; Flagging notes
8334 (defun org-agenda-show-the-flagging-note ()
8335 "Display the flagging note in the other window.
8336 When called a second time in direct sequence, offer to remove the FLAGGING
8337 tag and (if present) the flagging note."
8338 (interactive)
8339 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8340 (win (selected-window))
8341 note heading newhead)
8342 (unless hdmarker
8343 (error "No linked entry at point"))
8344 (if (and (eq this-command last-command)
8345 (y-or-n-p "Unflag and remove any flagging note? "))
8346 (progn
8347 (org-agenda-remove-flag hdmarker)
8348 (let ((win (get-buffer-window "*Flagging Note*")))
8349 (and win (delete-window win)))
8350 (message "Entry unflaged"))
8351 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8352 (unless note
8353 (error "No flagging note"))
8354 (org-kill-new note)
8355 (org-switch-to-buffer-other-window "*Flagging Note*")
8356 (erase-buffer)
8357 (insert note)
8358 (goto-char (point-min))
8359 (while (re-search-forward "\\\\n" nil t)
8360 (replace-match "\n" t t))
8361 (goto-char (point-min))
8362 (select-window win)
8363 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8365 (defun org-agenda-remove-flag (marker)
8366 "Remove the FLAGGED tag and any flagging note in the entry."
8367 (let (newhead)
8368 (org-with-point-at marker
8369 (org-toggle-tag "FLAGGED" 'off)
8370 (org-entry-delete nil "THEFLAGGINGNOTE")
8371 (setq newhead (org-get-heading)))
8372 (org-agenda-change-all-lines newhead marker)
8373 (message "Entry unflaged")))
8375 (defun org-agenda-get-any-marker (&optional pos)
8376 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8377 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8379 ;;; Appointment reminders
8381 (defvar appt-time-msg-list)
8383 ;;;###autoload
8384 (defun org-agenda-to-appt (&optional refresh filter)
8385 "Activate appointments found in `org-agenda-files'.
8386 With a \\[universal-argument] prefix, refresh the list of
8387 appointments.
8389 If FILTER is t, interactively prompt the user for a regular
8390 expression, and filter out entries that don't match it.
8392 If FILTER is a string, use this string as a regular expression
8393 for filtering entries out.
8395 FILTER can also be an alist with the car of each cell being
8396 either 'headline or 'category. For example:
8398 '((headline \"IMPORTANT\")
8399 (category \"Work\"))
8401 will only add headlines containing IMPORTANT or headlines
8402 belonging to the \"Work\" category."
8403 (interactive "P")
8404 (if refresh (setq appt-time-msg-list nil))
8405 (if (eq filter t)
8406 (setq filter (read-from-minibuffer "Regexp filter: ")))
8407 (let* ((cnt 0) ; count added events
8408 (org-agenda-new-buffers nil)
8409 (org-deadline-warning-days 0)
8410 ;; Do not use `org-today' here because appt only takes
8411 ;; time and without date as argument, so it may pass wrong
8412 ;; information otherwise
8413 (today (org-date-to-gregorian
8414 (time-to-days (current-time))))
8415 (org-agenda-restrict nil)
8416 (files (org-agenda-files 'unrestricted)) entries file)
8417 ;; Get all entries which may contain an appt
8418 (org-prepare-agenda-buffers files)
8419 (while (setq file (pop files))
8420 (setq entries
8421 (append entries
8422 (org-agenda-get-day-entries
8423 file today :timestamp :scheduled :deadline))))
8424 (setq entries (delq nil entries))
8425 ;; Map thru entries and find if we should filter them out
8426 (mapc
8427 (lambda(x)
8428 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8429 (cat (get-text-property 1 'org-category x))
8430 (tod (get-text-property 1 'time-of-day x))
8431 (ok (or (null filter)
8432 (and (stringp filter) (string-match filter evt))
8433 (and (listp filter)
8434 (or (string-match
8435 (cadr (assoc 'category filter)) cat)
8436 (string-match
8437 (cadr (assoc 'headline filter)) evt))))))
8438 ;; FIXME: Shall we remove text-properties for the appt text?
8439 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8440 (when (and ok tod)
8441 (setq tod (concat "00" (number-to-string tod))
8442 tod (when (string-match
8443 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8444 (concat (match-string 1 tod) ":"
8445 (match-string 2 tod))))
8446 (appt-add tod evt)
8447 (setq cnt (1+ cnt))))) entries)
8448 (org-release-buffers org-agenda-new-buffers)
8449 (if (eq cnt 0)
8450 (message "No event to add")
8451 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8453 (defun org-agenda-todayp (date)
8454 "Does DATE mean today, when considering `org-extend-today-until'?"
8455 (let ((today (org-today))
8456 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8457 date)))
8458 (eq date today)))
8460 (provide 'org-agenda)
8462 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
8464 ;;; org-agenda.el ends here