Continue numbering from any previous numbered snippet with +n, even when previous...
[org-mode.git] / lisp / org-agenda.el
blobb208d1e72219d73044f1980c509c54d6e7f2fd2d
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a conveniant way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (eval-when-compile
50 (require 'cl))
52 (declare-function diary-add-to-list "diary-lib"
53 (date string specifier &optional marker globcolor literal))
54 (declare-function calendar-absolute-from-iso "cal-iso" (date))
55 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
56 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
57 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
58 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
59 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-french-date-string "cal-french" (&optional date))
61 (declare-function calendar-goto-date "cal-move" (date))
62 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
63 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
64 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
65 (declare-function calendar-iso-from-absolute "cal-iso" (date))
66 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
67 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
68 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
69 (declare-function org-datetree-find-date-create "org-datetree"
70 (date &optional keep-restriction))
71 (declare-function org-columns-quit "org-colview" ())
72 (declare-function diary-date-display-form "diary-lib" (&optional type))
73 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
74 (declare-function org-habit-insert-consistency-graphs
75 "org-habit" (&optional line))
76 (declare-function org-is-habit-p "org-habit" (&optional pom))
77 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
78 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
79 (declare-function org-pop-to-buffer-same-window "org-compat"
80 (&optional buffer-or-name norecord label))
82 (defvar calendar-mode-map)
83 (defvar org-clock-current-task) ; defined in org-clock.el
84 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
85 (defvar org-habit-show-habits)
86 (defvar org-habit-show-habits-only-for-today)
88 ;; Defined somewhere in this file, but used before definition.
89 (defvar org-agenda-buffer-name)
90 (defvar org-agenda-overriding-header)
91 (defvar org-agenda-title-append nil)
92 (defvar entry)
93 (defvar date)
94 (defvar org-agenda-undo-list)
95 (defvar org-agenda-pending-undo-list)
96 (defvar original-date) ; dynamically scoped, calendar.el does scope this
98 (defcustom org-agenda-confirm-kill 1
99 "When set, remote killing from the agenda buffer needs confirmation.
100 When t, a confirmation is always needed. When a number N, confirmation is
101 only needed when the text to be killed contains more than N non-white lines."
102 :group 'org-agenda
103 :type '(choice
104 (const :tag "Never" nil)
105 (const :tag "Always" t)
106 (integer :tag "When more than N lines")))
108 (defcustom org-agenda-compact-blocks nil
109 "Non-nil means make the block agenda more compact.
110 This is done globally by leaving out lines like the agenda span
111 name and week number or the separator lines."
112 :group 'org-agenda
113 :type 'boolean)
115 (defcustom org-agenda-block-separator ?=
116 "The separator between blocks in the agenda.
117 If this is a string, it will be used as the separator, with a newline added.
118 If it is a character, it will be repeated to fill the window width.
119 If nil the separator is disabled. In `org-agenda-custom-commands' this
120 addresses the separator between the current and the previous block."
121 :group 'org-agenda
122 :type '(choice
123 (const :tag "Disabled" nil)
124 (character)
125 (string)))
127 (defgroup org-agenda-export nil
128 "Options concerning exporting agenda views in Org-mode."
129 :tag "Org Agenda Export"
130 :group 'org-agenda)
132 (defcustom org-agenda-with-colors t
133 "Non-nil means use colors in agenda views."
134 :group 'org-agenda-export
135 :type 'boolean)
137 (defcustom org-agenda-exporter-settings nil
138 "Alist of variable/value pairs that should be active during agenda export.
139 This is a good place to set options for ps-print and for htmlize.
140 Note that the way this is implemented, the values will be evaluated
141 before assigned to the variables. So make sure to quote values you do
142 *not* want evaluated, for example
144 (setq org-agenda-exporter-settings
145 '((ps-print-color-p 'black-white)))"
146 :group 'org-agenda-export
147 :type '(repeat
148 (list
149 (variable)
150 (sexp :tag "Value"))))
152 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
153 "Hook run in temporary buffer before writing it to an export file.
154 A useful function is `org-agenda-add-entry-text'."
155 :group 'org-agenda-export
156 :type 'hook
157 :options '(org-agenda-add-entry-text))
159 (defcustom org-agenda-add-entry-text-maxlines 0
160 "Maximum number of entry text lines to be added to agenda.
161 This is only relevant when `org-agenda-add-entry-text' is part of
162 `org-agenda-before-write-hook', which it is by default.
163 When this is 0, nothing will happen. When it is greater than 0, it
164 specifies the maximum number of lines that will be added for each entry
165 that is listed in the agenda view.
167 Note that this variable is not used during display, only when exporting
168 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
169 and `org-agenda-entry-text-maxlines'."
170 :group 'org-agenda
171 :type 'integer)
173 (defcustom org-agenda-add-entry-text-descriptive-links t
174 "Non-nil means export org-links as descriptive links in agenda added text.
175 This variable applies to the text added to the agenda when
176 `org-agenda-add-entry-text-maxlines' is larger than 0.
177 When this variable nil, the URL will (also) be shown."
178 :group 'org-agenda
179 :type 'boolean)
181 (defcustom org-agenda-export-html-style ""
182 "The style specification for exported HTML Agenda files.
183 If this variable contains a string, it will replace the default <style>
184 section as produced by `htmlize'.
185 Since there are different ways of setting style information, this variable
186 needs to contain the full HTML structure to provide a style, including the
187 surrounding HTML tags. The style specifications should include definitions
188 the fonts used by the agenda, here is an example:
190 <style type=\"text/css\">
191 p { font-weight: normal; color: gray; }
192 .org-agenda-structure {
193 font-size: 110%;
194 color: #003399;
195 font-weight: 600;
197 .org-todo {
198 color: #cc6666;
199 font-weight: bold;
201 .org-agenda-done {
202 color: #339933;
204 .org-done {
205 color: #339933;
207 .title { text-align: center; }
208 .todo, .deadline { color: red; }
209 .done { color: green; }
210 </style>
212 or, if you want to keep the style in a file,
214 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
216 As the value of this option simply gets inserted into the HTML <head> header,
217 you can \"misuse\" it to also add other text to the header. However,
218 <style>...</style> is required, if not present the variable will be ignored."
219 :group 'org-agenda-export
220 :group 'org-export-html
221 :type 'string)
223 (defcustom org-agenda-persistent-filter nil
224 "When set, keep filters from one agenda view to the next."
225 :group 'org-agenda
226 :type 'boolean)
228 (defgroup org-agenda-custom-commands nil
229 "Options concerning agenda views in Org-mode."
230 :tag "Org Agenda Custom Commands"
231 :group 'org-agenda)
233 (defconst org-sorting-choice
234 '(choice
235 (const time-up) (const time-down)
236 (const category-keep) (const category-up) (const category-down)
237 (const tag-down) (const tag-up)
238 (const priority-up) (const priority-down)
239 (const todo-state-up) (const todo-state-down)
240 (const effort-up) (const effort-down)
241 (const habit-up) (const habit-down)
242 (const alpha-up) (const alpha-down)
243 (const user-defined-up) (const user-defined-down))
244 "Sorting choices.")
246 (defconst org-agenda-custom-commands-local-options
247 `(repeat :tag "Local settings for this command. Remember to quote values"
248 (choice :tag "Setting"
249 (list :tag "Heading for this block"
250 (const org-agenda-overriding-header)
251 (string :tag "Headline"))
252 (list :tag "Files to be searched"
253 (const org-agenda-files)
254 (list
255 (const :format "" quote)
256 (repeat (file))))
257 (list :tag "Sorting strategy"
258 (const org-agenda-sorting-strategy)
259 (list
260 (const :format "" quote)
261 (repeat
262 ,org-sorting-choice)))
263 (list :tag "Prefix format"
264 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
265 (string))
266 (list :tag "Number of days in agenda"
267 (const org-agenda-span)
268 (choice (const :tag "Day" 'day)
269 (const :tag "Week" 'week)
270 (const :tag "Month" 'month)
271 (const :tag "Year" 'year)
272 (integer :tag "Custom")))
273 (list :tag "Fixed starting date"
274 (const org-agenda-start-day)
275 (string :value "2007-11-01"))
276 (list :tag "Start on day of week"
277 (const org-agenda-start-on-weekday)
278 (choice :value 1
279 (const :tag "Today" nil)
280 (integer :tag "Weekday No.")))
281 (list :tag "Include data from diary"
282 (const org-agenda-include-diary)
283 (boolean))
284 (list :tag "Deadline Warning days"
285 (const org-deadline-warning-days)
286 (integer :value 1))
287 (list :tag "Tags filter preset"
288 (const org-agenda-filter-preset)
289 (list
290 (const :format "" quote)
291 (repeat
292 (string :tag "+tag or -tag"))))
293 (list :tag "Set daily/weekly entry types"
294 (const org-agenda-entry-types)
295 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
296 (const :deadline)
297 (const :scheduled)
298 (const :timestamp)
299 (const :sexp)))
300 (list :tag "Standard skipping condition"
301 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
302 (const org-agenda-skip-function)
303 (list
304 (const :format "" quote)
305 (list
306 (choice
307 :tag "Skipping range"
308 (const :tag "Skip entry" org-agenda-skip-entry-if)
309 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
310 (repeat :inline t :tag "Conditions for skipping"
311 (choice
312 :tag "Condition type"
313 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
314 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
315 (list :tag "TODO state is" :inline t
316 (const 'todo)
317 (choice
318 (const :tag "any not-done state" 'todo)
319 (const :tag "any done state" 'done)
320 (const :tag "any state" 'any)
321 (list :tag "Keyword list"
322 (const :format "" quote)
323 (repeat (string :tag "Keyword")))))
324 (list :tag "TODO state is not" :inline t
325 (const 'nottodo)
326 (choice
327 (const :tag "any not-done state" 'todo)
328 (const :tag "any done state" 'done)
329 (const :tag "any state" 'any)
330 (list :tag "Keyword list"
331 (const :format "" quote)
332 (repeat (string :tag "Keyword")))))
333 (const :tag "scheduled" 'scheduled)
334 (const :tag "not scheduled" 'notscheduled)
335 (const :tag "deadline" 'deadline)
336 (const :tag "no deadline" 'notdeadline)
337 (const :tag "timestamp" 'timestamp)
338 (const :tag "no timestamp" 'nottimestamp))))))
339 (list :tag "Non-standard skipping condition"
340 :value (org-agenda-skip-function)
341 (const org-agenda-skip-function)
342 (sexp :tag "Function or form (quoted!)"))
343 (list :tag "Any variable"
344 (variable :tag "Variable")
345 (sexp :tag "Value (sexp)"))))
346 "Selection of examples for agenda command settings.
347 This will be spliced into the custom type of
348 `org-agenda-custom-commands'.")
351 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
352 ((agenda "") (alltodo))))
353 "Custom commands for the agenda.
354 These commands will be offered on the splash screen displayed by the
355 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
357 (key desc type match settings files)
359 key The key (one or more characters as a string) to be associated
360 with the command.
361 desc A description of the command, when omitted or nil, a default
362 description is built using MATCH.
363 type The command type, any of the following symbols:
364 agenda The daily/weekly agenda.
365 todo Entries with a specific TODO keyword, in all agenda files.
366 search Entries containing search words entry or headline.
367 tags Tags/Property/TODO match in all agenda files.
368 tags-todo Tags/P/T match in all agenda files, TODO entries only.
369 todo-tree Sparse tree of specific TODO keyword in *current* file.
370 tags-tree Sparse tree with all tags matches in *current* file.
371 occur-tree Occur sparse tree for *current* file.
372 ... A user-defined function.
373 match What to search for:
374 - a single keyword for TODO keyword searches
375 - a tags match expression for tags searches
376 - a word search expression for text searches.
377 - a regular expression for occur searches
378 For all other commands, this should be the empty string.
379 settings A list of option settings, similar to that in a let form, so like
380 this: ((opt1 val1) (opt2 val2) ...). The values will be
381 evaluated at the moment of execution, so quote them when needed.
382 files A list of files file to write the produced agenda buffer to
383 with the command `org-store-agenda-views'.
384 If a file name ends in \".html\", an HTML version of the buffer
385 is written out. If it ends in \".ps\", a postscript version is
386 produced. Otherwise, only the plain text is written to the file.
388 You can also define a set of commands, to create a composite agenda buffer.
389 In this case, an entry looks like this:
391 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
393 where
395 desc A description string to be displayed in the dispatcher menu.
396 cmd An agenda command, similar to the above. However, tree commands
397 are no allowed, but instead you can get agenda and global todo list.
398 So valid commands for a set are:
399 (agenda \"\" settings)
400 (alltodo \"\" settings)
401 (stuck \"\" settings)
402 (todo \"match\" settings files)
403 (search \"match\" settings files)
404 (tags \"match\" settings files)
405 (tags-todo \"match\" settings files)
407 Each command can carry a list of options, and another set of options can be
408 given for the whole set of commands. Individual command options take
409 precedence over the general options.
411 When using several characters as key to a command, the first characters
412 are prefix commands. For the dispatcher to display useful information, you
413 should provide a description for the prefix, like
415 (setq org-agenda-custom-commands
416 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
417 (\"hl\" tags \"+HOME+Lisa\")
418 (\"hp\" tags \"+HOME+Peter\")
419 (\"hk\" tags \"+HOME+Kim\")))"
420 :group 'org-agenda-custom-commands
421 :type `(repeat
422 (choice :value ("x" "Describe command here" tags "" nil)
423 (list :tag "Single command"
424 (string :tag "Access Key(s) ")
425 (option (string :tag "Description"))
426 (choice
427 (const :tag "Agenda" agenda)
428 (const :tag "TODO list" alltodo)
429 (const :tag "Search words" search)
430 (const :tag "Stuck projects" stuck)
431 (const :tag "Tags/Property match (all agenda files)" tags)
432 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
433 (const :tag "TODO keyword search (all agenda files)" todo)
434 (const :tag "Tags sparse tree (current buffer)" tags-tree)
435 (const :tag "TODO keyword tree (current buffer)" todo-tree)
436 (const :tag "Occur tree (current buffer)" occur-tree)
437 (sexp :tag "Other, user-defined function"))
438 (string :tag "Match (only for some commands)")
439 ,org-agenda-custom-commands-local-options
440 (option (repeat :tag "Export" (file :tag "Export to"))))
441 (list :tag "Command series, all agenda files"
442 (string :tag "Access Key(s)")
443 (string :tag "Description ")
444 (repeat :tag "Component"
445 (choice
446 (list :tag "Agenda"
447 (const :format "" agenda)
448 (const :tag "" :format "" "")
449 ,org-agenda-custom-commands-local-options)
450 (list :tag "TODO list (all keywords)"
451 (const :format "" alltodo)
452 (const :tag "" :format "" "")
453 ,org-agenda-custom-commands-local-options)
454 (list :tag "Search words"
455 (const :format "" search)
456 (string :tag "Match")
457 ,org-agenda-custom-commands-local-options)
458 (list :tag "Stuck projects"
459 (const :format "" stuck)
460 (const :tag "" :format "" "")
461 ,org-agenda-custom-commands-local-options)
462 (list :tag "Tags search"
463 (const :format "" tags)
464 (string :tag "Match")
465 ,org-agenda-custom-commands-local-options)
466 (list :tag "Tags search, TODO entries only"
467 (const :format "" tags-todo)
468 (string :tag "Match")
469 ,org-agenda-custom-commands-local-options)
470 (list :tag "TODO keyword search"
471 (const :format "" todo)
472 (string :tag "Match")
473 ,org-agenda-custom-commands-local-options)
474 (list :tag "Other, user-defined function"
475 (symbol :tag "function")
476 (string :tag "Match")
477 ,org-agenda-custom-commands-local-options)))
479 (repeat :tag "Settings for entire command set"
480 (list (variable :tag "Any variable")
481 (sexp :tag "Value")))
482 (option (repeat :tag "Export" (file :tag "Export to"))))
483 (cons :tag "Prefix key documentation"
484 (string :tag "Access Key(s)")
485 (string :tag "Description ")))))
487 (defcustom org-agenda-query-register ?o
488 "The register holding the current query string.
489 The purpose of this is that if you construct a query string interactively,
490 you can then use it to define a custom command."
491 :group 'org-agenda-custom-commands
492 :type 'character)
494 (defcustom org-stuck-projects
495 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
496 "How to identify stuck projects.
497 This is a list of four items:
498 1. A tags/todo/property matcher string that is used to identify a project.
499 See the manual for a description of tag and property searches.
500 The entire tree below a headline matched by this is considered one project.
501 2. A list of TODO keywords identifying non-stuck projects.
502 If the project subtree contains any headline with one of these todo
503 keywords, the project is considered to be not stuck. If you specify
504 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
505 3. A list of tags identifying non-stuck projects.
506 If the project subtree contains any headline with one of these tags,
507 the project is considered to be not stuck. If you specify \"*\" as
508 a tag, any tag will mark the project unstuck. Note that this is about
509 the explicit presence of a tag somewhere in the subtree, inherited
510 tags to not count here. If inherited tags make a project not stuck,
511 use \"-TAG\" in the tags part of the matcher under (1.) above.
512 4. An arbitrary regular expression matching non-stuck projects.
514 If the project turns out to be not stuck, search continues also in the
515 subtree to see if any of the subtasks have project status.
517 See also the variable `org-tags-match-list-sublevels' which applies
518 to projects matched by this search as well.
520 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
521 or `C-c a #' to produce the list."
522 :group 'org-agenda-custom-commands
523 :type '(list
524 (string :tag "Tags/TODO match to identify a project")
525 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
526 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
527 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
529 (defcustom org-agenda-filter-effort-default-operator "<"
530 "The default operator for effort estimate filtering.
531 If you select an effort estimate limit without first pressing an operator,
532 this one will be used."
533 :group 'org-agenda-custom-commands
534 :type '(choice (const :tag "less or equal" "<")
535 (const :tag "greater or equal"">")
536 (const :tag "equal" "=")))
538 (defgroup org-agenda-skip nil
539 "Options concerning skipping parts of agenda files."
540 :tag "Org Agenda Skip"
541 :group 'org-agenda)
543 (defcustom org-agenda-skip-function-global nil
544 "Function to be called at each match during agenda construction.
545 If this function returns nil, the current match should not be skipped.
546 If the function decided to skip an agenda match, is must return the
547 buffer position from which the search should be continued.
548 This may also be a Lisp form, which will be evaluated.
550 This variable will be applied to every agenda match, including
551 tags/property searches and TODO lists. So try to make the test function
552 do its checking as efficiently as possible. To implement a skipping
553 condition just for specific agenda commands, use the variable
554 `org-agenda-skip-function' which can be set in the options section
555 of custom agenda commands."
556 :group 'org-agenda-skip
557 :type 'sexp)
559 (defgroup org-agenda-daily/weekly nil
560 "Options concerning the daily/weekly agenda."
561 :tag "Org Agenda Daily/Weekly"
562 :group 'org-agenda)
563 (defgroup org-agenda-todo-list nil
564 "Options concerning the global todo list agenda view."
565 :tag "Org Agenda Todo List"
566 :group 'org-agenda)
567 (defgroup org-agenda-match-view nil
568 "Options concerning the general tags/property/todo match agenda view."
569 :tag "Org Agenda Match View"
570 :group 'org-agenda)
571 (defgroup org-agenda-search-view nil
572 "Options concerning the general tags/property/todo match agenda view."
573 :tag "Org Agenda Match View"
574 :group 'org-agenda)
576 (defvar org-agenda-archives-mode nil
577 "Non-nil means the agenda will include archived items.
578 If this is the symbol `trees', trees in the selected agenda scope
579 that are marked with the ARCHIVE tag will be included anyway. When this is
580 t, also all archive files associated with the current selection of agenda
581 files will be included.")
583 (defcustom org-agenda-skip-comment-trees t
584 "Non-nil means skip trees that start with the COMMENT keyword.
585 When nil, these trees are also scanned by agenda commands."
586 :group 'org-agenda-skip
587 :type 'boolean)
589 (defcustom org-agenda-todo-list-sublevels t
590 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
591 When nil, the sublevels of a TODO entry are not checked, resulting in
592 potentially much shorter TODO lists."
593 :group 'org-agenda-skip
594 :group 'org-agenda-todo-list
595 :type 'boolean)
597 (defcustom org-agenda-todo-ignore-with-date nil
598 "Non-nil means don't show entries with a date in the global todo list.
599 You can use this if you prefer to mark mere appointments with a TODO keyword,
600 but don't want them to show up in the TODO list.
601 When this is set, it also covers deadlines and scheduled items, the settings
602 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
603 will be ignored.
604 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
605 :group 'org-agenda-skip
606 :group 'org-agenda-todo-list
607 :type 'boolean)
609 (defcustom org-agenda-todo-ignore-timestamp nil
610 "Non-nil means don't show entries with a timestamp.
611 This applies when creating the global todo list.
612 Valid values are:
614 past Don't show entries for today or in the past.
616 future Don't show entries with a timestamp in the future.
617 The idea behind this is that if it has a future
618 timestamp, you don't want to think about it until the
619 date.
621 all Don't show any entries with a timestamp in the global todo list.
622 The idea behind this is that by setting a timestamp, you
623 have already \"taken care\" of this item.
625 This variable can also have an integer as a value. If positive (N),
626 todos with a timestamp N or more days in the future will be ignored. If
627 negative (-N), todos with a timestamp N or more days in the past will be
628 ignored. If 0, todos with a timestamp either today or in the future will
629 be ignored. For example, a value of -1 will exclude todos with a
630 timestamp in the past (yesterday or earlier), while a value of 7 will
631 exclude todos with a timestamp a week or more in the future.
633 See also `org-agenda-todo-ignore-with-date'.
634 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
635 to make his option also apply to the tags-todo list."
636 :group 'org-agenda-skip
637 :group 'org-agenda-todo-list
638 :type '(choice
639 (const :tag "Ignore future timestamp todos" future)
640 (const :tag "Ignore past or present timestamp todos" past)
641 (const :tag "Ignore all timestamp todos" all)
642 (const :tag "Show timestamp todos" nil)
643 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
645 (defcustom org-agenda-todo-ignore-scheduled nil
646 "Non-nil means, ignore some scheduled TODO items when making TODO list.
647 This applies when creating the global todo list.
648 Valid values are:
650 past Don't show entries scheduled today or in the past.
652 future Don't show entries scheduled in the future.
653 The idea behind this is that by scheduling it, you don't want to
654 think about it until the scheduled date.
656 all Don't show any scheduled entries in the global todo list.
657 The idea behind this is that by scheduling it, you have already
658 \"taken care\" of this item.
660 t Same as `all', for backward compatibility.
662 This variable can also have an integer as a value. See
663 `org-agenda-todo-ignore-timestamp' for more details.
665 See also `org-agenda-todo-ignore-with-date'.
666 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
667 to make his option also apply to the tags-todo list."
668 :group 'org-agenda-skip
669 :group 'org-agenda-todo-list
670 :type '(choice
671 (const :tag "Ignore future-scheduled todos" future)
672 (const :tag "Ignore past- or present-scheduled todos" past)
673 (const :tag "Ignore all scheduled todos" all)
674 (const :tag "Ignore all scheduled todos (compatibility)" t)
675 (const :tag "Show scheduled todos" nil)
676 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
678 (defcustom org-agenda-todo-ignore-deadlines nil
679 "Non-nil means ignore some deadlined TODO items when making TODO list.
680 There are different motivations for using different values, please think
681 carefully when configuring this variable.
683 This applies when creating the global todo list.
684 Valid values are:
686 near Don't show near deadline entries. A deadline is near when it is
687 closer than `org-deadline-warning-days' days. The idea behind this
688 is that such items will appear in the agenda anyway.
690 far Don't show TODO entries where a deadline has been defined, but
691 the deadline is not near. This is useful if you don't want to
692 use the todo list to figure out what to do now.
694 past Don't show entries with a deadline timestamp for today or in the past.
696 future Don't show entries with a deadline timestamp in the future, not even
697 when they become `near' ones. Use it with caution.
699 all Ignore all TODO entries that do have a deadline.
701 t Same as `near', for backward compatibility.
703 This variable can also have an integer as a value. See
704 `org-agenda-todo-ignore-timestamp' for more details.
706 See also `org-agenda-todo-ignore-with-date'.
707 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
708 to make his option also apply to the tags-todo list."
709 :group 'org-agenda-skip
710 :group 'org-agenda-todo-list
711 :type '(choice
712 (const :tag "Ignore near deadlines" near)
713 (const :tag "Ignore near deadlines (compatibility)" t)
714 (const :tag "Ignore far deadlines" far)
715 (const :tag "Ignore all TODOs with a deadlines" all)
716 (const :tag "Show all TODOs, even if they have a deadline" nil)
717 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
719 (defcustom org-agenda-tags-todo-honor-ignore-options nil
720 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
721 The variables
722 `org-agenda-todo-ignore-with-date',
723 `org-agenda-todo-ignore-timestamp',
724 `org-agenda-todo-ignore-scheduled',
725 `org-agenda-todo-ignore-deadlines'
726 make the global TODO list skip entries that have time stamps of certain
727 kinds. If this option is set, the same options will also apply for the
728 tags-todo search, which is the general tags/property matcher
729 restricted to unfinished TODO entries only."
730 :group 'org-agenda-skip
731 :group 'org-agenda-todo-list
732 :group 'org-agenda-match-view
733 :type 'boolean)
735 (defcustom org-agenda-skip-scheduled-if-done nil
736 "Non-nil means don't show scheduled items in agenda when they are done.
737 This is relevant for the daily/weekly agenda, not for the TODO list. And
738 it applies only to the actual date of the scheduling. Warnings about
739 an item with a past scheduling dates are always turned off when the item
740 is DONE."
741 :group 'org-agenda-skip
742 :group 'org-agenda-daily/weekly
743 :type 'boolean)
745 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
746 "Non-nil means skip scheduling line if same entry shows because of deadline.
747 In the agenda of today, an entry can show up multiple times because
748 it is both scheduled and has a nearby deadline, and maybe a plain time
749 stamp as well.
750 When this variable is t, then only the deadline is shown and the fact that
751 the entry is scheduled today or was scheduled previously is not shown.
752 When this variable is nil, the entry will be shown several times. When
753 the variable is the symbol `not-today', then skip scheduled previously,
754 but not scheduled today."
755 :group 'org-agenda-skip
756 :group 'org-agenda-daily/weekly
757 :type '(choice
758 (const :tag "Never" nil)
759 (const :tag "Always" t)
760 (const :tag "Not when scheduled today" not-today)))
762 (defcustom org-agenda-skip-deadline-if-done nil
763 "Non-nil means don't show deadlines when the corresponding item is done.
764 When nil, the deadline is still shown and should give you a happy feeling.
765 This is relevant for the daily/weekly agenda. And it applied only to the
766 actually date of the deadline. Warnings about approaching and past-due
767 deadlines are always turned off when the item is DONE."
768 :group 'org-agenda-skip
769 :group 'org-agenda-daily/weekly
770 :type 'boolean)
772 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
773 "Non-nil means skip deadline prewarning when entry is also scheduled.
774 This will apply on all days where a prewarning for the deadline would
775 be shown, but not at the day when the entry is actually due. On that day,
776 the deadline will be shown anyway.
777 This variable may be set to nil, t, or a number which will then give
778 the number of days before the actual deadline when the prewarnings
779 should resume.
780 This can be used in a workflow where the first showing of the deadline will
781 trigger you to schedule it, and then you don't want to be reminded of it
782 because you will take care of it on the day when scheduled."
783 :group 'org-agenda-skip
784 :group 'org-agenda-daily/weekly
785 :type '(choice
786 (const :tag "Alwas show prewarning" nil)
787 (const :tag "Remove prewarning if entry is scheduled" t)
788 (integer :tag "Restart prewarning N days before deadline")))
790 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
791 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
792 When non-nil, after the search for timestamps has matched once in an
793 entry, the rest of the entry will not be searched."
794 :group 'org-agenda-skip
795 :type 'boolean)
797 (defcustom org-agenda-skip-timestamp-if-done nil
798 "Non-nil means don't select item by timestamp or -range if it is DONE."
799 :group 'org-agenda-skip
800 :group 'org-agenda-daily/weekly
801 :type 'boolean)
803 (defcustom org-agenda-dim-blocked-tasks t
804 "Non-nil means dim blocked tasks in the agenda display.
805 This causes some overhead during agenda construction, but if you
806 have turned on `org-enforce-todo-dependencies',
807 `org-enforce-todo-checkbox-dependencies', or any other blocking
808 mechanism, this will create useful feedback in the agenda.
810 Instead of t, this variable can also have the value `invisible'.
811 Then blocked tasks will be invisible and only become visible when
812 they become unblocked. An exemption to this behavior is when a task is
813 blocked because of unchecked checkboxes below it. Since checkboxes do
814 not show up in the agenda views, making this task invisible you remove any
815 trace from agenda views that there is something to do. Therefore, a task
816 that is blocked because of checkboxes will never be made invisible, it
817 will only be dimmed."
818 :group 'org-agenda-daily/weekly
819 :group 'org-agenda-todo-list
820 :type '(choice
821 (const :tag "Do not dim" nil)
822 (const :tag "Dim to a grey face" t)
823 (const :tag "Make invisible" invisible)))
825 (defcustom org-timeline-show-empty-dates 3
826 "Non-nil means `org-timeline' also shows dates without an entry.
827 When nil, only the days which actually have entries are shown.
828 When t, all days between the first and the last date are shown.
829 When an integer, show also empty dates, but if there is a gap of more than
830 N days, just insert a special line indicating the size of the gap."
831 :group 'org-agenda-skip
832 :type '(choice
833 (const :tag "None" nil)
834 (const :tag "All" t)
835 (integer :tag "at most")))
837 (defgroup org-agenda-startup nil
838 "Options concerning initial settings in the Agenda in Org Mode."
839 :tag "Org Agenda Startup"
840 :group 'org-agenda)
842 (defcustom org-agenda-menu-show-matcher t
843 "Non-nil means show the match string in the agenda dispatcher menu.
844 When nil, the matcher string is not shown, but is put into the help-echo
845 property so than moving the mouse over the command shows it.
846 Setting it to nil is good if matcher strings are very long and/or if
847 you want to use two-column display (see `org-agenda-menu-two-column')."
848 :group 'org-agenda
849 :type 'boolean)
851 (defcustom org-agenda-menu-two-column nil
852 "Non-nil means, use two columns to show custom commands in the dispatcher.
853 If you use this, you probably want to set `org-agenda-menu-show-matcher'
854 to nil."
855 :group 'org-agenda
856 :type 'boolean)
858 (defcustom org-finalize-agenda-hook nil
859 "Hook run just before displaying an agenda buffer."
860 :group 'org-agenda-startup
861 :type 'hook)
863 (defcustom org-agenda-mouse-1-follows-link nil
864 "Non-nil means mouse-1 on a link will follow the link in the agenda.
865 A longer mouse click will still set point. Does not work on XEmacs.
866 Needs to be set before org.el is loaded."
867 :group 'org-agenda-startup
868 :type 'boolean)
870 (defcustom org-agenda-start-with-follow-mode nil
871 "The initial value of follow mode in a newly created agenda window."
872 :group 'org-agenda-startup
873 :type 'boolean)
875 (defcustom org-agenda-follow-indirect nil
876 "Non-nil means `org-agenda-follow-mode' displays only the
877 current item's tree, in an indirect buffer."
878 :group 'org-agenda
879 :type 'boolean)
881 (defcustom org-agenda-show-outline-path t
882 "Non-nil means show outline path in echo area after line motion."
883 :group 'org-agenda-startup
884 :type 'boolean)
886 (defcustom org-agenda-start-with-entry-text-mode nil
887 "The initial value of entry-text-mode in a newly created agenda window."
888 :group 'org-agenda-startup
889 :type 'boolean)
891 (defcustom org-agenda-entry-text-maxlines 5
892 "Number of text lines to be added when `E' is pressed in the agenda.
894 Note that this variable only used during agenda display. Add add entry text
895 when exporting the agenda, configure the variable
896 `org-agenda-add-entry-ext-maxlines'."
897 :group 'org-agenda
898 :type 'integer)
900 (defcustom org-agenda-entry-text-exclude-regexps nil
901 "List of regular expressions to clean up entry text.
902 The complete matches of all regular expressions in this list will be
903 removed from entry text before it is shown in the agenda."
904 :group 'org-agenda
905 :type '(repeat (regexp)))
907 (defvar org-agenda-entry-text-cleanup-hook nil
908 "Hook that is run after basic cleanup of entry text to be shown in agenda.
909 This cleanup is done in a temporary buffer, so the function may inspect and
910 change the entire buffer.
911 Some default stuff like drawers and scheduling/deadline dates will already
912 have been removed when this is called, as will any matches for regular
913 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
915 (defvar org-agenda-include-inactive-timestamps nil
916 "Non-nil means include inactive time stamps in agenda and timeline.")
918 (defgroup org-agenda-windows nil
919 "Options concerning the windows used by the Agenda in Org Mode."
920 :tag "Org Agenda Windows"
921 :group 'org-agenda)
923 (defcustom org-agenda-window-setup 'reorganize-frame
924 "How the agenda buffer should be displayed.
925 Possible values for this option are:
927 current-window Show agenda in the current window, keeping all other windows.
928 other-window Use `switch-to-buffer-other-window' to display agenda.
929 reorganize-frame Show only two windows on the current frame, the current
930 window and the agenda.
931 other-frame Use `switch-to-buffer-other-frame' to display agenda.
932 Also, when exiting the agenda, kill that frame.
933 See also the variable `org-agenda-restore-windows-after-quit'."
934 :group 'org-agenda-windows
935 :type '(choice
936 (const current-window)
937 (const other-frame)
938 (const other-window)
939 (const reorganize-frame)))
941 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
942 "The min and max height of the agenda window as a fraction of frame height.
943 The value of the variable is a cons cell with two numbers between 0 and 1.
944 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
945 :group 'org-agenda-windows
946 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
948 (defcustom org-agenda-restore-windows-after-quit nil
949 "Non-nil means restore window configuration upon exiting agenda.
950 Before the window configuration is changed for displaying the agenda,
951 the current status is recorded. When the agenda is exited with
952 `q' or `x' and this option is set, the old state is restored. If
953 `org-agenda-window-setup' is `other-frame', the value of this
954 option will be ignored."
955 :group 'org-agenda-windows
956 :type 'boolean)
958 (defcustom org-agenda-ndays nil
959 "Number of days to include in overview display.
960 Should be 1 or 7.
961 Obsolete, see `org-agenda-span'."
962 :group 'org-agenda-daily/weekly
963 :type 'integer)
965 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
967 (defcustom org-agenda-span 'week
968 "Number of days to include in overview display.
969 Can be day, week, month, year, or any number of days.
970 Custom commands can set this variable in the options section."
971 :group 'org-agenda-daily/weekly
972 :type '(choice (const :tag "Day" day)
973 (const :tag "Week" week)
974 (const :tag "Month" month)
975 (const :tag "Year" year)
976 (integer :tag "Custom")))
978 (defcustom org-agenda-start-on-weekday 1
979 "Non-nil means start the overview always on the specified weekday.
980 0 denotes Sunday, 1 denotes Monday etc.
981 When nil, always start on the current day.
982 Custom commands can set this variable in the options section."
983 :group 'org-agenda-daily/weekly
984 :type '(choice (const :tag "Today" nil)
985 (integer :tag "Weekday No.")))
987 (defcustom org-agenda-show-all-dates t
988 "Non-nil means `org-agenda' shows every day in the selected range.
989 When nil, only the days which actually have entries are shown."
990 :group 'org-agenda-daily/weekly
991 :type 'boolean)
993 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
994 "Format string for displaying dates in the agenda.
995 Used by the daily/weekly agenda and by the timeline. This should be
996 a format string understood by `format-time-string', or a function returning
997 the formatted date as a string. The function must take a single argument,
998 a calendar-style date list like (month day year)."
999 :group 'org-agenda-daily/weekly
1000 :type '(choice
1001 (string :tag "Format string")
1002 (function :tag "Function")))
1004 (defun org-agenda-format-date-aligned (date)
1005 "Format a date string for display in the daily/weekly agenda, or timeline.
1006 This function makes sure that dates are aligned for easy reading."
1007 (require 'cal-iso)
1008 (let* ((dayname (calendar-day-name date))
1009 (day (cadr date))
1010 (day-of-week (calendar-day-of-week date))
1011 (month (car date))
1012 (monthname (calendar-month-name month))
1013 (year (nth 2 date))
1014 (iso-week (org-days-to-iso-week
1015 (calendar-absolute-from-gregorian date)))
1016 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1017 (1- year))
1018 ((and (= month 12) (<= iso-week 1))
1019 (1+ year))
1020 (t year)))
1021 (weekstring (if (= day-of-week 1)
1022 (format " W%02d" iso-week)
1023 "")))
1024 (format "%-10s %2d %s %4d%s"
1025 dayname day monthname year weekstring)))
1027 (defcustom org-agenda-time-leading-zero nil
1028 "Non-nil means use leading zero for military times in agenda.
1029 For example, 9:30am would become 09:30 rather than 9:30."
1030 :group 'org-agenda-daily/weekly
1031 :type 'boolean)
1033 (defcustom org-agenda-timegrid-use-ampm nil
1034 "When set, show AM/PM style timestamps on the timegrid."
1035 :group 'org-agenda
1036 :type 'boolean)
1038 (defun org-agenda-time-of-day-to-ampm (time)
1039 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1040 (let* ((hour-number (string-to-number (substring time 0 -3)))
1041 (minute (substring time -2))
1042 (ampm "am"))
1043 (cond
1044 ((equal hour-number 12)
1045 (setq ampm "pm"))
1046 ((> hour-number 12)
1047 (setq ampm "pm")
1048 (setq hour-number (- hour-number 12))))
1049 (concat
1050 (if org-agenda-time-leading-zero
1051 (format "%02d" hour-number)
1052 (format "%02s" (number-to-string hour-number)))
1053 ":" minute ampm)))
1055 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1056 "Conditionally convert TIME to AM/PM format
1057 based on `org-agenda-timegrid-use-ampm'"
1058 (if org-agenda-timegrid-use-ampm
1059 (org-agenda-time-of-day-to-ampm time)
1060 time))
1062 (defcustom org-agenda-weekend-days '(6 0)
1063 "Which days are weekend?
1064 These days get the special face `org-agenda-date-weekend' in the agenda
1065 and timeline buffers."
1066 :group 'org-agenda-daily/weekly
1067 :type '(set :greedy t
1068 (const :tag "Monday" 1)
1069 (const :tag "Tuesday" 2)
1070 (const :tag "Wednesday" 3)
1071 (const :tag "Thursday" 4)
1072 (const :tag "Friday" 5)
1073 (const :tag "Saturday" 6)
1074 (const :tag "Sunday" 0)))
1076 (defcustom org-agenda-include-diary nil
1077 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1078 Custom commands can set this variable in the options section."
1079 :group 'org-agenda-daily/weekly
1080 :type 'boolean)
1082 (defcustom org-agenda-include-deadlines t
1083 "If non-nil, include entries within their deadline warning period.
1084 Custom commands can set this variable in the options section."
1085 :group 'org-agenda-daily/weekly
1086 :type 'boolean)
1088 (defcustom org-agenda-repeating-timestamp-show-all t
1089 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1090 When set to a list of strings, only show occurrences of repeating
1091 stamps for these TODO keywords. When nil, only one occurrence is
1092 shown, either today or the nearest into the future."
1093 :group 'org-agenda-daily/weekly
1094 :type '(choice
1095 (const :tag "Show repeating stamps" t)
1096 (repeat :tag "Show repeating stamps for these TODO keywords"
1097 (string :tag "TODO Keyword"))
1098 (const :tag "Don't show repeating stamps" nil)))
1100 (defcustom org-scheduled-past-days 10000
1101 "No. of days to continue listing scheduled items that are not marked DONE.
1102 When an item is scheduled on a date, it shows up in the agenda on this
1103 day and will be listed until it is marked done for the number of days
1104 given here."
1105 :group 'org-agenda-daily/weekly
1106 :type 'integer)
1108 (defcustom org-agenda-log-mode-items '(closed clock)
1109 "List of items that should be shown in agenda log mode.
1110 This list may contain the following symbols:
1112 closed Show entries that have been closed on that day.
1113 clock Show entries that have received clocked time on that day.
1114 state Show all logged state changes.
1115 Note that instead of changing this variable, you can also press `C-u l' in
1116 the agenda to display all available LOG items temporarily."
1117 :group 'org-agenda-daily/weekly
1118 :type '(set :greedy t (const closed) (const clock) (const state)))
1120 (defcustom org-agenda-clock-consistency-checks
1121 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1122 :gap-ok-around ("4:00")
1123 :default-face ((:background "DarkRed") (:foreground "white"))
1124 :overlap-face nil :gap-face nil :no-end-time-face nil
1125 :long-face nil :short-face nil)
1126 "This is a property list, with the following keys:
1128 :max-duration Mark clocking chunks that are longer than this time.
1129 This is a time string like \"HH:MM\", or the number
1130 of minutes as an integer.
1132 :min-duration Mark clocking chunks that are shorter that this.
1133 This is a time string like \"HH:MM\", or the number
1134 of minutes as an integer.
1136 :max-gap Mark gaps between clocking chunks that are longer than
1137 this duration. A number of minutes, or a string
1138 like \"HH:MM\".
1140 :gap-ok-around List of times during the day which are usually not working
1141 times. When a gap is detected, but the gap contains any
1142 of these times, the gap is *not* reported. For example,
1143 if this is (\"4:00\" \"13:00\") then gaps that contain
1144 4:00 in the morning (i.e. the night) and 13:00
1145 (i.e. a typical lunch time) do not cause a warning.
1146 You should have at least one time during the night in this
1147 list, or otherwise the first task each morning will trigger
1148 a warning because it follows a long gap.
1150 Furthermore, the following properties can be used to define faces for
1151 issue display.
1153 :default-face the default face, if the specific face is undefined
1154 :overlap-face face for overlapping clocks
1155 :gap-face face for gaps between clocks
1156 :no-end-time-face face for incomplete clocks
1157 :long-face face for clock intervals that are too long
1158 :short-face face for clock intervals that are too short"
1159 :group 'org-agenda-daily/weekly
1160 :group 'org-clock
1161 :type 'plist)
1163 (defcustom org-agenda-log-mode-add-notes t
1164 "Non-nil means add first line of notes to log entries in agenda views.
1165 If a log item like a state change or a clock entry is associated with
1166 notes, the first line of these notes will be added to the entry in the
1167 agenda display."
1168 :group 'org-agenda-daily/weekly
1169 :type 'boolean)
1171 (defcustom org-agenda-start-with-log-mode nil
1172 "The initial value of log-mode in a newly created agenda window."
1173 :group 'org-agenda-startup
1174 :group 'org-agenda-daily/weekly
1175 :type 'boolean)
1177 (defcustom org-agenda-start-with-clockreport-mode nil
1178 "The initial value of clockreport-mode in a newly created agenda window."
1179 :group 'org-agenda-startup
1180 :group 'org-agenda-daily/weekly
1181 :type 'boolean)
1183 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1184 "Property list with parameters for the clocktable in clockreport mode.
1185 This is the display mode that shows a clock table in the daily/weekly
1186 agenda, the properties for this dynamic block can be set here.
1187 The usual clocktable parameters are allowed here, but you cannot set
1188 the properties :name, :tstart, :tend, :block, and :scope - these will
1189 be overwritten to make sure the content accurately reflects the
1190 current display in the agenda."
1191 :group 'org-agenda-daily/weekly
1192 :type 'plist)
1194 (defcustom org-agenda-search-view-always-boolean nil
1195 "Non-nil means the search string is interpreted as individual parts.
1197 The search string for search view can either be interpreted as a phrase,
1198 or as a list of snippets that define a boolean search for a number of
1199 strings.
1201 When this is non-nil, the string will be split on whitespace, and each
1202 snippet will be searched individually, and all must match in order to
1203 select an entry. A snippet is then a single string of non-white
1204 characters, or a string in double quotes, or a regexp in {} braces.
1205 If a snippet is preceded by \"-\", the snippet must *not* match.
1206 \"+\" is syntactic sugar for positive selection. Each snippet may
1207 be found as a full word or a partial word, but see the variable
1208 `org-agenda-search-view-force-full-words'.
1210 When this is nil, search will look for the entire search phrase as one,
1211 with each space character matching any amount of whitespace, including
1212 line breaks.
1214 Even when this is nil, you can still switch to Boolean search dynamically
1215 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1216 is a regexp marked with braces like \"{abc}\", this will also switch to
1217 boolean search."
1218 :group 'org-agenda-search-view
1219 :type 'boolean)
1221 (if (fboundp 'defvaralias)
1222 (defvaralias 'org-agenda-search-view-search-words-only
1223 'org-agenda-search-view-always-boolean))
1225 (defcustom org-agenda-search-view-force-full-words nil
1226 "Non-nil means, search words must be matches as complete words.
1227 When nil, they may also match part of a word."
1228 :group 'org-agenda-search-view
1229 :type 'boolean)
1231 (defgroup org-agenda-time-grid nil
1232 "Options concerning the time grid in the Org-mode Agenda."
1233 :tag "Org Agenda Time Grid"
1234 :group 'org-agenda)
1236 (defcustom org-agenda-search-headline-for-time t
1237 "Non-nil means search headline for a time-of-day.
1238 If the headline contains a time-of-day in one format or another, it will
1239 be used to sort the entry into the time sequence of items for a day.
1240 Some people have time stamps in the headline that refer to the creation
1241 time or so, and then this produces an unwanted side effect. If this is
1242 the case for your, use this variable to turn off searching the headline
1243 for a time."
1244 :group 'org-agenda-time-grid
1245 :type 'boolean)
1247 (defcustom org-agenda-use-time-grid t
1248 "Non-nil means show a time grid in the agenda schedule.
1249 A time grid is a set of lines for specific times (like every two hours between
1250 8:00 and 20:00). The items scheduled for a day at specific times are
1251 sorted in between these lines.
1252 For details about when the grid will be shown, and what it will look like, see
1253 the variable `org-agenda-time-grid'."
1254 :group 'org-agenda-time-grid
1255 :type 'boolean)
1257 (defcustom org-agenda-time-grid
1258 '((daily today require-timed)
1259 "----------------"
1260 (800 1000 1200 1400 1600 1800 2000))
1262 "The settings for time grid for agenda display.
1263 This is a list of three items. The first item is again a list. It contains
1264 symbols specifying conditions when the grid should be displayed:
1266 daily if the agenda shows a single day
1267 weekly if the agenda shows an entire week
1268 today show grid on current date, independent of daily/weekly display
1269 require-timed show grid only if at least one item has a time specification
1271 The second item is a string which will be placed behind the grid time.
1273 The third item is a list of integers, indicating the times that should have
1274 a grid line."
1275 :group 'org-agenda-time-grid
1276 :type
1277 '(list
1278 (set :greedy t :tag "Grid Display Options"
1279 (const :tag "Show grid in single day agenda display" daily)
1280 (const :tag "Show grid in weekly agenda display" weekly)
1281 (const :tag "Always show grid for today" today)
1282 (const :tag "Show grid only if any timed entries are present"
1283 require-timed)
1284 (const :tag "Skip grid times already present in an entry"
1285 remove-match))
1286 (string :tag "Grid String")
1287 (repeat :tag "Grid Times" (integer :tag "Time"))))
1289 (defcustom org-agenda-show-current-time-in-grid t
1290 "Non-nil means show the current time in the time grid."
1291 :group 'org-agenda-time-grid
1292 :type 'boolean)
1294 (defcustom org-agenda-current-time-string
1295 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1296 "The string for the current time marker in the agenda."
1297 :group 'org-agenda-time-grid
1298 :type 'string)
1300 (defgroup org-agenda-sorting nil
1301 "Options concerning sorting in the Org-mode Agenda."
1302 :tag "Org Agenda Sorting"
1303 :group 'org-agenda)
1305 (defcustom org-agenda-sorting-strategy
1306 '((agenda habit-down time-up priority-down category-keep)
1307 (todo priority-down category-keep)
1308 (tags priority-down category-keep)
1309 (search category-keep))
1310 "Sorting structure for the agenda items of a single day.
1311 This is a list of symbols which will be used in sequence to determine
1312 if an entry should be listed before another entry. The following
1313 symbols are recognized:
1315 time-up Put entries with time-of-day indications first, early first
1316 time-down Put entries with time-of-day indications first, late first
1317 category-keep Keep the default order of categories, corresponding to the
1318 sequence in `org-agenda-files'.
1319 category-up Sort alphabetically by category, A-Z.
1320 category-down Sort alphabetically by category, Z-A.
1321 tag-up Sort alphabetically by last tag, A-Z.
1322 tag-down Sort alphabetically by last tag, Z-A.
1323 priority-up Sort numerically by priority, high priority last.
1324 priority-down Sort numerically by priority, high priority first.
1325 todo-state-up Sort by todo state, tasks that are done last.
1326 todo-state-down Sort by todo state, tasks that are done first.
1327 effort-up Sort numerically by estimated effort, high effort last.
1328 effort-down Sort numerically by estimated effort, high effort first.
1329 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1330 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1331 habit-up Put entries that are habits first
1332 habit-down Put entries that are habits last
1333 alpha-up Sort headlines alphabetically
1334 alpha-down Sort headlines alphabetically, reversed
1336 The different possibilities will be tried in sequence, and testing stops
1337 if one comparison returns a \"not-equal\". For example, the default
1338 '(time-up category-keep priority-down)
1339 means: Pull out all entries having a specified time of day and sort them,
1340 in order to make a time schedule for the current day the first thing in the
1341 agenda listing for the day. Of the entries without a time indication, keep
1342 the grouped in categories, don't sort the categories, but keep them in
1343 the sequence given in `org-agenda-files'. Within each category sort by
1344 priority.
1346 Leaving out `category-keep' would mean that items will be sorted across
1347 categories by priority.
1349 Instead of a single list, this can also be a set of list for specific
1350 contents, with a context symbol in the car of the list, any of
1351 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1353 Custom commands can bind this variable in the options section."
1354 :group 'org-agenda-sorting
1355 :type `(choice
1356 (repeat :tag "General" ,org-sorting-choice)
1357 (list :tag "Individually"
1358 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1359 (repeat ,org-sorting-choice))
1360 (cons (const :tag "Strategy for TODO lists" todo)
1361 (repeat ,org-sorting-choice))
1362 (cons (const :tag "Strategy for Tags matches" tags)
1363 (repeat ,org-sorting-choice))
1364 (cons (const :tag "Strategy for search matches" search)
1365 (repeat ,org-sorting-choice)))))
1367 (defcustom org-agenda-cmp-user-defined nil
1368 "A function to define the comparison `user-defined'.
1369 This function must receive two arguments, agenda entry a and b.
1370 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1371 the user comparison, return nil.
1372 When this is defined, you can make `user-defined-up' and `user-defined-down'
1373 part of an agenda sorting strategy."
1374 :group 'org-agenda-sorting
1375 :type 'symbol)
1377 (defcustom org-sort-agenda-notime-is-late t
1378 "Non-nil means items without time are considered late.
1379 This is only relevant for sorting. When t, items which have no explicit
1380 time like 15:30 will be considered as 99:01, i.e. later than any items which
1381 do have a time. When nil, the default time is before 0:00. You can use this
1382 option to decide if the schedule for today should come before or after timeless
1383 agenda entries."
1384 :group 'org-agenda-sorting
1385 :type 'boolean)
1387 (defcustom org-sort-agenda-noeffort-is-high t
1388 "Non-nil means items without effort estimate are sorted as high effort.
1389 This also applies when filtering an agenda view with respect to the
1390 < or > effort operator. Then, tasks with no effort defined will be treated
1391 as tasks with high effort.
1392 When nil, such items are sorted as 0 minutes effort."
1393 :group 'org-agenda-sorting
1394 :type 'boolean)
1396 (defgroup org-agenda-line-format nil
1397 "Options concerning the entry prefix in the Org-mode agenda display."
1398 :tag "Org Agenda Line Format"
1399 :group 'org-agenda)
1401 (defcustom org-agenda-prefix-format
1402 '((agenda . " %i %-12:c%?-12t% s")
1403 (timeline . " % s")
1404 (todo . " %i %-12:c")
1405 (tags . " %i %-12:c")
1406 (search . " %i %-12:c"))
1407 "Format specifications for the prefix of items in the agenda views.
1408 An alist with five entries, each for the different agenda types. The
1409 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1410 The values are format strings.
1412 This format works similar to a printf format, with the following meaning:
1414 %c the category of the item, \"Diary\" for entries from the diary,
1415 or as given by the CATEGORY keyword or derived from the file name
1416 %e the effort required by the item
1417 %i the icon category of the item, see `org-agenda-category-icon-alist'
1418 %T the last tag of the item (ignore inherited tags, which come first)
1419 %t the HH:MM time-of-day specification if one applies to the entry
1420 %s Scheduling/Deadline information, a short string
1421 %(expression) Eval EXPRESSION and replace the control string
1422 by the result
1424 All specifiers work basically like the standard `%s' of printf, but may
1425 contain two additional characters: a question mark just after the `%'
1426 and a whitespace/punctuation character just before the final letter.
1428 If the first character after `%' is a question mark, the entire field
1429 will only be included if the corresponding value applies to the current
1430 entry. This is useful for fields which should have fixed width when
1431 present, but zero width when absent. For example, \"%?-12t\" will
1432 result in a 12 character time field if a time of the day is specified,
1433 but will completely disappear in entries which do not contain a time.
1435 If there is punctuation or whitespace character just before the final
1436 format letter, this character will be appended to the field value if
1437 the value is not empty. For example, the format \"%-12:c\" leads to
1438 \"Diary: \" if the category is \"Diary\". If the category were be
1439 empty, no additional colon would be inserted.
1441 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1442 which means:
1444 - Indent the line with two space characters
1445 - Give the category a 12 chars wide field, padded with whitespace on
1446 the right (because of `-'). Append a colon if there is a category
1447 (because of `:').
1448 - If there is a time-of-day, put it into a 12 chars wide field. If no
1449 time, don't put in an empty field, just skip it (because of '?').
1450 - Finally, put the scheduling information.
1452 See also the variables `org-agenda-remove-times-when-in-prefix' and
1453 `org-agenda-remove-tags'.
1455 Custom commands can set this variable in the options section."
1456 :type '(choice
1457 (string :tag "General format")
1458 (list :greedy t :tag "View dependent"
1459 (cons (const agenda) (string :tag "Format"))
1460 (cons (const timeline) (string :tag "Format"))
1461 (cons (const todo) (string :tag "Format"))
1462 (cons (const tags) (string :tag "Format"))
1463 (cons (const search) (string :tag "Format"))))
1464 :group 'org-agenda-line-format)
1466 (defvar org-prefix-format-compiled nil
1467 "The compiled version of the most recently used prefix format.
1468 See the variable `org-agenda-prefix-format'.")
1470 (defcustom org-agenda-todo-keyword-format "%-1s"
1471 "Format for the TODO keyword in agenda lines.
1472 Set this to something like \"%-12s\" if you want all TODO keywords
1473 to occupy a fixed space in the agenda display."
1474 :group 'org-agenda-line-format
1475 :type 'string)
1477 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1478 "Text preceding timerange entries in the agenda view.
1479 This is a list with two strings. The first applies when the range
1480 is entirely on one day. The second applies if the range spans several days.
1481 The strings may have two \"%d\" format specifiers which will be filled
1482 with the sequence number of the days, and the total number of days in the
1483 range, respectively."
1484 :group 'org-agenda-line-format
1485 :type '(list
1486 (string :tag "Deadline today ")
1487 (choice :tag "Deadline relative"
1488 (string :tag "Format string")
1489 (function))))
1491 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1492 "Text preceding scheduled items in the agenda view.
1493 This is a list with two strings. The first applies when the item is
1494 scheduled on the current day. The second applies when it has been scheduled
1495 previously, it may contain a %d indicating that this is the nth time that
1496 this item is scheduled, due to automatic rescheduling of unfinished items
1497 for the following day. So this number is one larger than the number of days
1498 that passed since this item was scheduled first."
1499 :group 'org-agenda-line-format
1500 :type '(list
1501 (string :tag "Scheduled today ")
1502 (string :tag "Scheduled previously")))
1504 (defcustom org-agenda-inactive-leader "["
1505 "Text preceding item pulled into the agenda by inactive time stamps.
1506 These entries are added to the agenda when pressing \"[\"."
1507 :group 'org-agenda-line-format
1508 :type '(list
1509 (string :tag "Scheduled today ")
1510 (string :tag "Scheduled previously")))
1512 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1513 "Text preceding deadline items in the agenda view.
1514 This is a list with two strings. The first applies when the item has its
1515 deadline on the current day. The second applies when it is in the past or
1516 in the future, it may contain %d to capture how many days away the deadline
1517 is (was)."
1518 :group 'org-agenda-line-format
1519 :type '(list
1520 (string :tag "Deadline today ")
1521 (choice :tag "Deadline relative"
1522 (string :tag "Format string")
1523 (function))))
1525 (defcustom org-agenda-remove-times-when-in-prefix t
1526 "Non-nil means remove duplicate time specifications in agenda items.
1527 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1528 time-of-day specification in a headline or diary entry is extracted and
1529 placed into the prefix. If this option is non-nil, the original specification
1530 \(a timestamp or -range, or just a plain time(range) specification like
1531 11:30-4pm) will be removed for agenda display. This makes the agenda less
1532 cluttered.
1533 The option can be t or nil. It may also be the symbol `beg', indicating
1534 that the time should only be removed when it is located at the beginning of
1535 the headline/diary entry."
1536 :group 'org-agenda-line-format
1537 :type '(choice
1538 (const :tag "Always" t)
1539 (const :tag "Never" nil)
1540 (const :tag "When at beginning of entry" beg)))
1542 (defcustom org-agenda-remove-timeranges-from-blocks nil
1543 "Non-nil means remove time ranges specifications in agenda
1544 items that span on several days."
1545 :group 'org-agenda-line-format
1546 :type 'boolean)
1548 (defcustom org-agenda-default-appointment-duration nil
1549 "Default duration for appointments that only have a starting time.
1550 When nil, no duration is specified in such cases.
1551 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1552 :group 'org-agenda-line-format
1553 :type '(choice
1554 (integer :tag "Minutes")
1555 (const :tag "No default duration")))
1557 (defcustom org-agenda-show-inherited-tags t
1558 "Non-nil means show inherited tags in each agenda line."
1559 :group 'org-agenda-line-format
1560 :type 'boolean)
1562 (defcustom org-agenda-hide-tags-regexp nil
1563 "Regular expression used to filter away specific tags in agenda views.
1564 This means that these tags will be present, but not be shown in the agenda
1565 line. Secondary filtering will still work on the hidden tags.
1566 Nil means don't hide any tags."
1567 :group 'org-agenda-line-format
1568 :type '(choice
1569 (const :tag "Hide none" nil)
1570 (string :tag "Regexp ")))
1572 (defcustom org-agenda-remove-tags nil
1573 "Non-nil means remove the tags from the headline copy in the agenda.
1574 When this is the symbol `prefix', only remove tags when
1575 `org-agenda-prefix-format' contains a `%T' specifier."
1576 :group 'org-agenda-line-format
1577 :type '(choice
1578 (const :tag "Always" t)
1579 (const :tag "Never" nil)
1580 (const :tag "When prefix format contains %T" prefix)))
1582 (if (fboundp 'defvaralias)
1583 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1584 'org-agenda-remove-tags))
1586 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1587 "Shift tags in agenda items to this column.
1588 If this number is positive, it specifies the column. If it is negative,
1589 it means that the tags should be flushright to that column. For example,
1590 -80 works well for a normal 80 character screen."
1591 :group 'org-agenda-line-format
1592 :type 'integer)
1594 (if (fboundp 'defvaralias)
1595 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1597 (defcustom org-agenda-fontify-priorities 'cookies
1598 "Non-nil means highlight low and high priorities in agenda.
1599 When t, the highest priority entries are bold, lowest priority italic.
1600 However, settings in `org-priority-faces' will overrule these faces.
1601 When this variable is the symbol `cookies', only fontify the
1602 cookies, not the entire task.
1603 This may also be an association list of priority faces, whose
1604 keys are the character values of `org-highest-priority',
1605 `org-default-priority', and `org-lowest-priority' (the default values
1606 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1607 color as a string, or a list like `(:background \"Red\")'.
1608 If it is a color, the variable `org-faces-easy-properties'
1609 determines if it is a foreground or a background color."
1610 :group 'org-agenda-line-format
1611 :type '(choice
1612 (const :tag "Never" nil)
1613 (const :tag "Defaults" t)
1614 (const :tag "Cookies only" cookies)
1615 (repeat :tag "Specify"
1616 (list (character :tag "Priority" :value ?A)
1617 (choice :tag "Face "
1618 (string :tag "Color")
1619 (sexp :tag "Face"))))))
1621 (defcustom org-agenda-day-face-function nil
1622 "Function called to determine what face should be used to display a day.
1623 The only argument passed to that function is the day. It should
1624 returns a face, or nil if does not want to specify a face and let
1625 the normal rules apply."
1626 :group 'org-agenda-line-format
1627 :type 'function)
1629 (defcustom org-agenda-category-icon-alist nil
1630 "Alist of category icon to be displayed in agenda views.
1632 Each entry should have the following format:
1634 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1636 Where CATEGORY-REGEXP is a regexp matching the categories where
1637 the icon should be displayed.
1638 FILE-OR-DATA either a file path or a string containing image data.
1640 The other fields can be omited safely if not needed:
1641 TYPE indicates the image type.
1642 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1643 image data.
1644 PROPS are additional image attributes to assign to the image,
1645 like, e.g. `:ascent center'.
1647 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1649 If you want to set the display properties yourself, just put a
1650 list as second element:
1652 (CATEGORY-REGEXP (MY PROPERTY LIST))
1654 For example, to display a 16px horizontal space for Emacs
1655 category, you can use:
1657 (\"Emacs\" '(space . (:width (16))))"
1658 :group 'org-agenda-line-format
1659 :type '(alist :key-type (string :tag "Regexp matching category")
1660 :value-type (choice (list :tag "Icon"
1661 (string :tag "File or data")
1662 (symbol :tag "Type")
1663 (boolean :tag "Data?")
1664 (repeat :tag "Extra image properties" :inline t symbol))
1665 (list :tag "Display properties" sexp))))
1667 (defgroup org-agenda-column-view nil
1668 "Options concerning column view in the agenda."
1669 :tag "Org Agenda Column View"
1670 :group 'org-agenda)
1672 (defcustom org-agenda-columns-show-summaries t
1673 "Non-nil means show summaries for columns displayed in the agenda view."
1674 :group 'org-agenda-column-view
1675 :type 'boolean)
1677 (defcustom org-agenda-columns-compute-summary-properties t
1678 "Non-nil means recompute all summary properties before column view.
1679 When column view in the agenda is listing properties that have a summary
1680 operator, it can go to all relevant buffers and recompute the summaries
1681 there. This can mean overhead for the agenda column view, but is necessary
1682 to have thing up to date.
1683 As a special case, a CLOCKSUM property also makes sure that the clock
1684 computations are current."
1685 :group 'org-agenda-column-view
1686 :type 'boolean)
1688 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1689 "Non-nil means the duration of an appointment will add to day effort.
1690 The property to which appointment durations will be added is the one given
1691 in the option `org-effort-property'. If an appointment does not have
1692 an end time, `org-agenda-default-appointment-duration' will be used. If that
1693 is not set, an appointment without end time will not contribute to the time
1694 estimate."
1695 :group 'org-agenda-column-view
1696 :type 'boolean)
1698 (defcustom org-agenda-auto-exclude-function nil
1699 "A function called with a tag to decide if it is filtered on '/ RET'.
1700 The sole argument to the function, which is called once for each
1701 possible tag, is a string giving the name of the tag. The
1702 function should return either nil if the tag should be included
1703 as normal, or \"-<TAG>\" to exclude the tag.
1704 Note that for the purpose of tag filtering, only the lower-case version of
1705 all tags will be considered, so that this function will only ever see
1706 the lower-case version of all tags."
1707 :group 'org-agenda
1708 :type 'function)
1710 (defcustom org-agenda-bulk-custom-functions nil
1711 "Alist of characters and custom functions for bulk actions.
1712 For example, this value makes those two functions available:
1714 '((?R set-category)
1715 (?C bulk-cut))
1717 With selected entries in an agenda buffer, `B R' will call
1718 the custom function `set-category' on the selected entries.
1719 Note that functions in this alist don't need to be quoted."
1720 :type 'alist
1721 :group 'org-agenda)
1723 (eval-when-compile
1724 (require 'cl))
1725 (require 'org)
1727 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1728 "Execute BODY with point at location given by `org-hd-marker' property.
1729 If STRING is non-nil, the text property will be fetched from position 0
1730 in that string. If STRING is nil, it will be fetched from the beginning
1731 of the current line."
1732 (org-with-gensyms (marker)
1733 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1734 'org-hd-marker string)))
1735 (with-current-buffer (marker-buffer ,marker)
1736 (save-excursion
1737 (goto-char ,marker)
1738 ,@body)))))
1739 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1741 (defun org-add-agenda-custom-command (entry)
1742 "Replace or add a command in `org-agenda-custom-commands'.
1743 This is mostly for hacking and trying a new command - once the command
1744 works you probably want to add it to `org-agenda-custom-commands' for good."
1745 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1746 (if ass
1747 (setcdr ass (cdr entry))
1748 (push entry org-agenda-custom-commands))))
1750 ;;; Define the Org-agenda-mode
1752 (defvar org-agenda-mode-map (make-sparse-keymap)
1753 "Keymap for `org-agenda-mode'.")
1754 (if (fboundp 'defvaralias)
1755 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1757 (defvar org-agenda-menu) ; defined later in this file.
1758 (defvar org-agenda-restrict) ; defined later in this file.
1759 (defvar org-agenda-follow-mode nil)
1760 (defvar org-agenda-entry-text-mode nil)
1761 (defvar org-agenda-clockreport-mode nil)
1762 (defvar org-agenda-show-log nil)
1763 (defvar org-agenda-redo-command nil)
1764 (defvar org-agenda-query-string nil)
1765 (defvar org-agenda-mode-hook nil
1766 "Hook for `org-agenda-mode', run after the mode is turned on.")
1767 (defvar org-agenda-type nil)
1768 (defvar org-agenda-force-single-file nil)
1769 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1771 (defun org-agenda-mode ()
1772 "Mode for time-sorted view on action items in Org-mode files.
1774 The following commands are available:
1776 \\{org-agenda-mode-map}"
1777 (interactive)
1778 (kill-all-local-variables)
1779 (setq org-agenda-undo-list nil
1780 org-agenda-pending-undo-list nil
1781 org-agenda-bulk-marked-entries nil)
1782 (setq major-mode 'org-agenda-mode)
1783 ;; Keep global-font-lock-mode from turning on font-lock-mode
1784 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1785 (setq mode-name "Org-Agenda")
1786 (use-local-map org-agenda-mode-map)
1787 (easy-menu-add org-agenda-menu)
1788 (if org-startup-truncated (setq truncate-lines t))
1789 (org-set-local 'line-move-visual nil)
1790 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1791 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1792 ;; Make sure properties are removed when copying text
1793 (when (boundp 'buffer-substring-filters)
1794 (org-set-local 'buffer-substring-filters
1795 (cons (lambda (x)
1796 (set-text-properties 0 (length x) nil x) x)
1797 buffer-substring-filters)))
1798 (unless org-agenda-keep-modes
1799 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1800 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1801 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1802 org-agenda-show-log org-agenda-start-with-log-mode))
1804 (easy-menu-change
1805 '("Agenda") "Agenda Files"
1806 (append
1807 (list
1808 (vector
1809 (if (get 'org-agenda-files 'org-restrict)
1810 "Restricted to single file"
1811 "Edit File List")
1812 '(org-edit-agenda-file-list)
1813 (not (get 'org-agenda-files 'org-restrict)))
1814 "--")
1815 (mapcar 'org-file-menu-entry (org-agenda-files))))
1816 (org-agenda-set-mode-name)
1817 (apply
1818 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1819 (list 'org-agenda-mode-hook)))
1821 (substitute-key-definition 'undo 'org-agenda-undo
1822 org-agenda-mode-map global-map)
1823 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1824 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1825 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1826 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1827 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1828 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1829 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1830 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1831 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1832 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1833 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1834 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1835 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1836 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1837 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1838 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1839 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1840 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1841 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1842 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1843 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1844 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1845 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1846 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1847 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1848 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1849 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1850 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1851 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1852 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1853 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1854 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1855 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1856 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1857 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1858 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1859 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1860 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1861 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1862 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1863 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1864 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1865 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1866 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1867 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1869 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1870 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1871 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1872 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1873 (while l (org-defkey org-agenda-mode-map
1874 (int-to-string (pop l)) 'digit-argument)))
1876 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1877 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1878 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1879 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1880 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1881 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1882 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1883 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1884 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1885 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1886 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1887 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1888 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1889 'org-clock-modify-effort-estimate)
1890 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1891 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1892 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1893 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1894 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1895 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1896 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1897 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1898 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1899 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1900 (substitute-key-definition 'next-line 'org-agenda-next-line
1901 org-agenda-mode-map global-map)
1902 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1903 org-agenda-mode-map global-map)
1904 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1905 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1906 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1907 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1908 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1909 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1910 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1911 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1912 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1913 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1914 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1915 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1916 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1917 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1918 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1919 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1920 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1921 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1922 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1923 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1924 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1925 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1926 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1927 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1928 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1929 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1930 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1931 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1932 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1933 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1935 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1936 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1937 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1938 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1939 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1940 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1941 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1942 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1943 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1944 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1946 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1947 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1948 (when org-agenda-mouse-1-follows-link
1949 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1950 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1951 '("Agenda"
1952 ("Agenda Files")
1953 "--"
1954 ("Agenda Dates"
1955 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1956 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1957 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1958 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1959 "--"
1960 ("View"
1961 ["Day View" org-agenda-day-view
1962 :active (org-agenda-check-type nil 'agenda)
1963 :style radio :selected (eq org-agenda-current-span 'day)
1964 :keys "v d (or just d)"]
1965 ["Week View" org-agenda-week-view
1966 :active (org-agenda-check-type nil 'agenda)
1967 :style radio :selected (eq org-agenda-current-span 'week)
1968 :keys "v w (or just w)"]
1969 ["Month View" org-agenda-month-view
1970 :active (org-agenda-check-type nil 'agenda)
1971 :style radio :selected (eq org-agenda-current-span 'month)
1972 :keys "v m"]
1973 ["Year View" org-agenda-year-view
1974 :active (org-agenda-check-type nil 'agenda)
1975 :style radio :selected (eq org-agenda-current-span 'year)
1976 :keys "v y"]
1977 "--"
1978 ["Include Diary" org-agenda-toggle-diary
1979 :style toggle :selected org-agenda-include-diary
1980 :active (org-agenda-check-type nil 'agenda)]
1981 ["Include Deadlines" org-agenda-toggle-deadlines
1982 :style toggle :selected org-agenda-include-deadlines
1983 :active (org-agenda-check-type nil 'agenda)]
1984 ["Use Time Grid" org-agenda-toggle-time-grid
1985 :style toggle :selected org-agenda-use-time-grid
1986 :active (org-agenda-check-type nil 'agenda)]
1987 "--"
1988 ["Show clock report" org-agenda-clockreport-mode
1989 :style toggle :selected org-agenda-clockreport-mode
1990 :active (org-agenda-check-type nil 'agenda)]
1991 ["Show some entry text" org-agenda-entry-text-mode
1992 :style toggle :selected org-agenda-entry-text-mode
1993 :active t]
1994 "--"
1995 ["Show Logbook entries" org-agenda-log-mode
1996 :style toggle :selected org-agenda-show-log
1997 :active (org-agenda-check-type nil 'agenda 'timeline)
1998 :keys "v l (or just l)"]
1999 ["Include archived trees" org-agenda-archives-mode
2000 :style toggle :selected org-agenda-archives-mode :active t
2001 :keys "v a"]
2002 ["Include archive files" (org-agenda-archives-mode t)
2003 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2004 :keys "v A"]
2005 "--"
2006 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2007 ["Write view to file" org-write-agenda t]
2008 ["Rebuild buffer" org-agenda-redo t]
2009 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2010 "--"
2011 ["Show original entry" org-agenda-show t]
2012 ["Go To (other window)" org-agenda-goto t]
2013 ["Go To (this window)" org-agenda-switch-to t]
2014 ["Follow Mode" org-agenda-follow-mode
2015 :style toggle :selected org-agenda-follow-mode :active t]
2016 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2017 "--"
2018 ("TODO"
2019 ["Cycle TODO" org-agenda-todo t]
2020 ["Next TODO set" org-agenda-todo-nextset t]
2021 ["Previous TODO set" org-agenda-todo-previousset t]
2022 ["Add note" org-agenda-add-note t])
2023 ("Archive/Refile/Delete"
2024 ["Archive default" org-agenda-archive-default t]
2025 ["Archive default" org-agenda-archive-default-with-confirmation t]
2026 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2027 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2028 ["Archive subtree" org-agenda-archive t]
2029 "--"
2030 ["Refile" org-agenda-refile t]
2031 "--"
2032 ["Delete subtree" org-agenda-kill t])
2033 ("Bulk action"
2034 ["Mark entry" org-agenda-bulk-mark t]
2035 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2036 ["Unmark entry" org-agenda-bulk-unmark t]
2037 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2038 ["Act on all marked" org-agenda-bulk-action t]
2039 "--"
2040 ("Tags and Properties"
2041 ["Show all Tags" org-agenda-show-tags t]
2042 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2043 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2044 "--"
2045 ["Column View" org-columns t])
2046 ("Deadline/Schedule"
2047 ["Schedule" org-agenda-schedule t]
2048 ["Set Deadline" org-agenda-deadline t]
2049 "--"
2050 ["Mark item" org-agenda-action :active t :keys "k m"]
2051 ["Show mark item" org-agenda-action :active t :keys "k v"]
2052 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2053 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2054 "--"
2055 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2056 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2057 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2058 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2059 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2060 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2061 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2062 ("Clock and Effort"
2063 ["Clock in" org-agenda-clock-in t]
2064 ["Clock out" org-agenda-clock-out t]
2065 ["Clock cancel" org-agenda-clock-cancel t]
2066 ["Goto running clock" org-clock-goto t]
2067 "--"
2068 ["Set Effort" org-agenda-set-effort t]
2069 ["Change clocked effort" org-clock-modify-effort-estimate
2070 (org-clock-is-active)])
2071 ("Priority"
2072 ["Set Priority" org-agenda-priority t]
2073 ["Increase Priority" org-agenda-priority-up t]
2074 ["Decrease Priority" org-agenda-priority-down t]
2075 ["Show Priority" org-agenda-show-priority t])
2076 ("Calendar/Diary"
2077 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2078 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2079 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2080 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2081 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2082 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2083 "--"
2084 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2085 "--"
2086 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2087 "--"
2088 ("MobileOrg"
2089 ["Push Files and Views" org-mobile-push t]
2090 ["Get Captured and Flagged" org-mobile-pull t]
2091 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2092 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2093 "--"
2094 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2095 "--"
2096 ["Quit" org-agenda-quit t]
2097 ["Exit and Release Buffers" org-agenda-exit t]
2100 ;;; Agenda undo
2102 (defvar org-agenda-allow-remote-undo t
2103 "Non-nil means allow remote undo from the agenda buffer.")
2104 (defvar org-agenda-undo-list nil
2105 "List of undoable operations in the agenda since last refresh.")
2106 (defvar org-agenda-undo-has-started-in nil
2107 "Buffers that have already seen `undo-start' in the current undo sequence.")
2108 (defvar org-agenda-pending-undo-list nil
2109 "In a series of undo commands, this is the list of remaining undo items.")
2111 (defun org-agenda-undo ()
2112 "Undo a remote editing step in the agenda.
2113 This undoes changes both in the agenda buffer and in the remote buffer
2114 that have been changed along."
2115 (interactive)
2116 (or org-agenda-allow-remote-undo
2117 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2118 (if (not (eq this-command last-command))
2119 (setq org-agenda-undo-has-started-in nil
2120 org-agenda-pending-undo-list org-agenda-undo-list))
2121 (if (not org-agenda-pending-undo-list)
2122 (error "No further undo information"))
2123 (let* ((entry (pop org-agenda-pending-undo-list))
2124 buf line cmd rembuf)
2125 (setq cmd (pop entry) line (pop entry))
2126 (setq rembuf (nth 2 entry))
2127 (org-with-remote-undo rembuf
2128 (while (bufferp (setq buf (pop entry)))
2129 (if (pop entry)
2130 (with-current-buffer buf
2131 (let ((last-undo-buffer buf)
2132 (inhibit-read-only t))
2133 (unless (memq buf org-agenda-undo-has-started-in)
2134 (push buf org-agenda-undo-has-started-in)
2135 (make-local-variable 'pending-undo-list)
2136 (undo-start))
2137 (while (and pending-undo-list
2138 (listp pending-undo-list)
2139 (not (car pending-undo-list)))
2140 (pop pending-undo-list))
2141 (undo-more 1))))))
2142 (org-goto-line line)
2143 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2145 (defun org-verify-change-for-undo (l1 l2)
2146 "Verify that a real change occurred between the undo lists L1 and L2."
2147 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2148 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2149 (not (eq l1 l2)))
2151 ;;; Agenda dispatch
2153 (defvar org-agenda-restrict nil)
2154 (defvar org-agenda-restrict-begin (make-marker))
2155 (defvar org-agenda-restrict-end (make-marker))
2156 (defvar org-agenda-last-dispatch-buffer nil)
2157 (defvar org-agenda-overriding-restriction nil)
2159 ;;;###autoload
2160 (defun org-agenda (&optional arg keys restriction)
2161 "Dispatch agenda commands to collect entries to the agenda buffer.
2162 Prompts for a command to execute. Any prefix arg will be passed
2163 on to the selected command. The default selections are:
2165 a Call `org-agenda-list' to display the agenda for current day or week.
2166 t Call `org-todo-list' to display the global todo list.
2167 T Call `org-todo-list' to display the global todo list, select only
2168 entries with a specific TODO keyword (the user gets a prompt).
2169 m Call `org-tags-view' to display headlines with tags matching
2170 a condition (the user is prompted for the condition).
2171 M Like `m', but select only TODO entries, no ordinary headlines.
2172 L Create a timeline for the current buffer.
2173 e Export views to associated files.
2174 s Search entries for keywords.
2175 / Multi occur across all agenda files and also files listed
2176 in `org-agenda-text-search-extra-files'.
2177 < Restrict agenda commands to buffer, subtree, or region.
2178 Press several times to get the desired effect.
2179 > Remove a previous restriction.
2180 # List \"stuck\" projects.
2181 ! Configure what \"stuck\" means.
2182 C Configure custom agenda commands.
2184 More commands can be added by configuring the variable
2185 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2186 searches can be pre-defined in this way.
2188 If the current buffer is in Org-mode and visiting a file, you can also
2189 first press `<' once to indicate that the agenda should be temporarily
2190 \(until the next use of \\[org-agenda]) restricted to the current file.
2191 Pressing `<' twice means to restrict to the current subtree or region
2192 \(if active)."
2193 (interactive "P")
2194 (catch 'exit
2195 (let* ((prefix-descriptions nil)
2196 (org-agenda-window-setup (if (equal (buffer-name)
2197 org-agenda-buffer-name)
2198 'current-window
2199 org-agenda-window-setup))
2200 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2201 (org-agenda-custom-commands
2202 ;; normalize different versions
2203 (delq nil
2204 (mapcar
2205 (lambda (x)
2206 (cond ((stringp (cdr x))
2207 (push x prefix-descriptions)
2208 nil)
2209 ((stringp (nth 1 x)) x)
2210 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2211 (t (cons (car x) (cons "" (cdr x))))))
2212 org-agenda-custom-commands)))
2213 (buf (current-buffer))
2214 (bfn (buffer-file-name (buffer-base-buffer)))
2215 entry key type match lprops ans)
2216 ;; Turn off restriction unless there is an overriding one,
2217 (unless org-agenda-overriding-restriction
2218 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2219 ;; There is a request to keep the file list in place
2220 (put 'org-agenda-files 'org-restrict nil))
2221 (setq org-agenda-restrict nil)
2222 (move-marker org-agenda-restrict-begin nil)
2223 (move-marker org-agenda-restrict-end nil))
2224 ;; Delete old local properties
2225 (put 'org-agenda-redo-command 'org-lprops nil)
2226 ;; Delete previously set last-arguments
2227 (put 'org-agenda-redo-command 'last-args nil)
2228 ;; Remember where this call originated
2229 (setq org-agenda-last-dispatch-buffer (current-buffer))
2230 (unless keys
2231 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2232 keys (car ans)
2233 restriction (cdr ans)))
2234 ;; Establish the restriction, if any
2235 (when (and (not org-agenda-overriding-restriction) restriction)
2236 (put 'org-agenda-files 'org-restrict (list bfn))
2237 (cond
2238 ((eq restriction 'region)
2239 (setq org-agenda-restrict t)
2240 (move-marker org-agenda-restrict-begin (region-beginning))
2241 (move-marker org-agenda-restrict-end (region-end)))
2242 ((eq restriction 'subtree)
2243 (save-excursion
2244 (setq org-agenda-restrict t)
2245 (org-back-to-heading t)
2246 (move-marker org-agenda-restrict-begin (point))
2247 (move-marker org-agenda-restrict-end
2248 (progn (org-end-of-subtree t)))))))
2250 ;; For example the todo list should not need it (but does...)
2251 (cond
2252 ((setq entry (assoc keys org-agenda-custom-commands))
2253 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2254 (progn
2255 (setq type (nth 2 entry) match (eval (nth 3 entry))
2256 lprops (nth 4 entry))
2257 (put 'org-agenda-redo-command 'org-lprops lprops)
2258 (cond
2259 ((eq type 'agenda)
2260 (org-let lprops '(org-agenda-list current-prefix-arg)))
2261 ((eq type 'alltodo)
2262 (org-let lprops '(org-todo-list current-prefix-arg)))
2263 ((eq type 'search)
2264 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2265 ((eq type 'stuck)
2266 (org-let lprops '(org-agenda-list-stuck-projects
2267 current-prefix-arg)))
2268 ((eq type 'tags)
2269 (org-let lprops '(org-tags-view current-prefix-arg match)))
2270 ((eq type 'tags-todo)
2271 (org-let lprops '(org-tags-view '(4) match)))
2272 ((eq type 'todo)
2273 (org-let lprops '(org-todo-list match)))
2274 ((eq type 'tags-tree)
2275 (org-check-for-org-mode)
2276 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2277 ((eq type 'todo-tree)
2278 (org-check-for-org-mode)
2279 (org-let lprops
2280 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2281 (regexp-quote match) "\\>"))))
2282 ((eq type 'occur-tree)
2283 (org-check-for-org-mode)
2284 (org-let lprops '(org-occur match)))
2285 ((functionp type)
2286 (org-let lprops '(funcall type match)))
2287 ((fboundp type)
2288 (org-let lprops '(funcall type match)))
2289 (t (error "Invalid custom agenda command type %s" type))))
2290 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2291 ((equal keys "C")
2292 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2293 (customize-variable 'org-agenda-custom-commands))
2294 ((equal keys "a") (call-interactively 'org-agenda-list))
2295 ((equal keys "s") (call-interactively 'org-search-view))
2296 ((equal keys "t") (call-interactively 'org-todo-list))
2297 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2298 ((equal keys "m") (call-interactively 'org-tags-view))
2299 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2300 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2301 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2302 (org-add-hook
2303 'post-command-hook
2304 (lambda ()
2305 (unless (current-message)
2306 (let* ((m (org-agenda-get-any-marker))
2307 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2308 (when note
2309 (message (concat
2310 "FLAGGING-NOTE ([?] for more info): "
2311 (org-add-props
2312 (replace-regexp-in-string
2313 "\\\\n" "//"
2314 (copy-sequence note))
2315 nil 'face 'org-warning)))))))
2316 t t))
2317 ((equal keys "L")
2318 (unless (org-mode-p)
2319 (error "This is not an Org-mode file"))
2320 (unless restriction
2321 (put 'org-agenda-files 'org-restrict (list bfn))
2322 (org-call-with-arg 'org-timeline arg)))
2323 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2324 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2325 ((equal keys "!") (customize-variable 'org-stuck-projects))
2326 (t (error "Invalid agenda key"))))))
2328 (defun org-agenda-append-agenda ()
2329 "Append another agenda view to the current one.
2330 This function allows interactive building of block agendas.
2331 Agenda views are separated by `org-agenda-block-separator'."
2332 (interactive)
2333 (unless (string= (buffer-name) org-agenda-buffer-name)
2334 (error "Can only append from within agenda buffer"))
2335 (let ((org-agenda-multi t))
2336 (org-agenda)
2337 (widen)))
2339 (defun org-agenda-normalize-custom-commands (cmds)
2340 (delq nil
2341 (mapcar
2342 (lambda (x)
2343 (cond ((stringp (cdr x)) nil)
2344 ((stringp (nth 1 x)) x)
2345 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2346 (t (cons (car x) (cons "" (cdr x))))))
2347 cmds)))
2349 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2350 "The user interface for selecting an agenda command."
2351 (catch 'exit
2352 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2353 (restrict-ok (and bfn (org-mode-p)))
2354 (region-p (org-region-active-p))
2355 (custom org-agenda-custom-commands)
2356 (selstring "")
2357 restriction second-time
2358 c entry key type match prefixes rmheader header-end custom1 desc
2359 line lines left right n n1)
2360 (save-window-excursion
2361 (delete-other-windows)
2362 (org-switch-to-buffer-other-window " *Agenda Commands*")
2363 (erase-buffer)
2364 (insert (eval-when-compile
2365 (let ((header
2367 Press key for an agenda command: < Buffer, subtree/region restriction
2368 -------------------------------- > Remove restriction
2369 a Agenda for current week or day e Export agenda views
2370 t List of all TODO entries T Entries with special TODO kwd
2371 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2372 L Timeline for current buffer # List stuck projects (!=configure)
2373 s Search for keywords C Configure custom agenda commands
2374 / Multi-occur ? Find :FLAGGED: entries
2376 (start 0))
2377 (while (string-match
2378 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2379 header start)
2380 (setq start (match-end 0))
2381 (add-text-properties (match-beginning 2) (match-end 2)
2382 '(face bold) header))
2383 header)))
2384 (setq header-end (move-marker (make-marker) (point)))
2385 (while t
2386 (setq custom1 custom)
2387 (when (eq rmheader t)
2388 (org-goto-line 1)
2389 (re-search-forward ":" nil t)
2390 (delete-region (match-end 0) (point-at-eol))
2391 (forward-char 1)
2392 (looking-at "-+")
2393 (delete-region (match-end 0) (point-at-eol))
2394 (move-marker header-end (match-end 0)))
2395 (goto-char header-end)
2396 (delete-region (point) (point-max))
2398 ;; Produce all the lines that describe custom commands and prefixes
2399 (setq lines nil)
2400 (while (setq entry (pop custom1))
2401 (setq key (car entry) desc (nth 1 entry)
2402 type (nth 2 entry)
2403 match (nth 3 entry))
2404 (if (> (length key) 1)
2405 (add-to-list 'prefixes (string-to-char key))
2406 (setq line
2407 (format
2408 "%-4s%-14s"
2409 (org-add-props (copy-sequence key)
2410 '(face bold))
2411 (cond
2412 ((string-match "\\S-" desc) desc)
2413 ((eq type 'agenda) "Agenda for current week or day")
2414 ((eq type 'alltodo) "List of all TODO entries")
2415 ((eq type 'search) "Word search")
2416 ((eq type 'stuck) "List of stuck projects")
2417 ((eq type 'todo) "TODO keyword")
2418 ((eq type 'tags) "Tags query")
2419 ((eq type 'tags-todo) "Tags (TODO)")
2420 ((eq type 'tags-tree) "Tags tree")
2421 ((eq type 'todo-tree) "TODO kwd tree")
2422 ((eq type 'occur-tree) "Occur tree")
2423 ((functionp type) (if (symbolp type)
2424 (symbol-name type)
2425 "Lambda expression"))
2426 (t "???"))))
2427 (if org-agenda-menu-show-matcher
2428 (setq line
2429 (concat line ": "
2430 (cond
2431 ((stringp match)
2432 (setq match (copy-sequence match))
2433 (org-add-props match nil 'face 'org-warning))
2434 (match
2435 (format "set of %d commands" (length match)))
2436 (t ""))))
2437 (if (org-string-nw-p match)
2438 (add-text-properties
2439 0 (length line) (list 'help-echo
2440 (concat "Matcher: "match)) line)))
2441 (push line lines)))
2442 (setq lines (nreverse lines))
2443 (when prefixes
2444 (mapc (lambda (x)
2445 (push
2446 (format "%s %s"
2447 (org-add-props (char-to-string x)
2448 nil 'face 'bold)
2449 (or (cdr (assoc (concat selstring
2450 (char-to-string x))
2451 prefix-descriptions))
2452 "Prefix key"))
2453 lines))
2454 prefixes))
2456 ;; Check if we should display in two columns
2457 (if org-agenda-menu-two-column
2458 (progn
2459 (setq n (length lines)
2460 n1 (+ (/ n 2) (mod n 2))
2461 right (nthcdr n1 lines)
2462 left (copy-sequence lines))
2463 (setcdr (nthcdr (1- n1) left) nil))
2464 (setq left lines right nil))
2465 (while left
2466 (insert "\n" (pop left))
2467 (when right
2468 (if (< (current-column) 40)
2469 (move-to-column 40 t)
2470 (insert " "))
2471 (insert (pop right))))
2473 ;; Make the window the right size
2474 (goto-char (point-min))
2475 (if second-time
2476 (if (not (pos-visible-in-window-p (point-max)))
2477 (org-fit-window-to-buffer))
2478 (setq second-time t)
2479 (org-fit-window-to-buffer))
2481 ;; Ask for selection
2482 (message "Press key for agenda command%s:"
2483 (if (or restrict-ok org-agenda-overriding-restriction)
2484 (if org-agenda-overriding-restriction
2485 " (restriction lock active)"
2486 (if restriction
2487 (format " (restricted to %s)" restriction)
2488 " (unrestricted)"))
2489 ""))
2490 (setq c (read-char-exclusive))
2491 (message "")
2492 (cond
2493 ((assoc (char-to-string c) custom)
2494 (setq selstring (concat selstring (char-to-string c)))
2495 (throw 'exit (cons selstring restriction)))
2496 ((memq c prefixes)
2497 (setq selstring (concat selstring (char-to-string c))
2498 prefixes nil
2499 rmheader (or rmheader t)
2500 custom (delq nil (mapcar
2501 (lambda (x)
2502 (if (or (= (length (car x)) 1)
2503 (/= (string-to-char (car x)) c))
2505 (cons (substring (car x) 1) (cdr x))))
2506 custom))))
2507 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2508 (message "Restriction is only possible in Org-mode buffers")
2509 (ding) (sit-for 1))
2510 ((eq c ?1)
2511 (org-agenda-remove-restriction-lock 'noupdate)
2512 (setq restriction 'buffer))
2513 ((eq c ?0)
2514 (org-agenda-remove-restriction-lock 'noupdate)
2515 (setq restriction (if region-p 'region 'subtree)))
2516 ((eq c ?<)
2517 (org-agenda-remove-restriction-lock 'noupdate)
2518 (setq restriction
2519 (cond
2520 ((eq restriction 'buffer)
2521 (if region-p 'region 'subtree))
2522 ((memq restriction '(subtree region))
2523 nil)
2524 (t 'buffer))))
2525 ((eq c ?>)
2526 (org-agenda-remove-restriction-lock 'noupdate)
2527 (setq restriction nil))
2528 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2529 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2530 ((and (> (length selstring) 0) (eq c ?\d))
2531 (delete-window)
2532 (org-agenda-get-restriction-and-command prefix-descriptions))
2534 ((equal c ?q) (error "Abort"))
2535 (t (error "Invalid key %c" c))))))))
2537 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2538 (defvar org-agenda-last-arguments nil
2539 "The arguments of the previous call to `org-agenda'.")
2540 (defun org-agenda-run-series (name series)
2541 (org-let (nth 1 series) '(org-prepare-agenda name))
2542 (let* ((org-agenda-multi t)
2543 (redo (list 'org-agenda-run-series name (list 'quote series)))
2544 (org-agenda-overriding-arguments
2545 (or org-agenda-overriding-arguments
2546 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2547 (get 'org-agenda-redo-command 'last-args))))
2548 (cmds (car series))
2549 (gprops (nth 1 series))
2550 match ;; The byte compiler incorrectly complains about this. Keep it!
2551 cmd type lprops)
2552 (while (setq cmd (pop cmds))
2553 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2554 (cond
2555 ((eq type 'agenda)
2556 (org-let2 gprops lprops
2557 '(call-interactively 'org-agenda-list)))
2558 ((eq type 'alltodo)
2559 (org-let2 gprops lprops
2560 '(call-interactively 'org-todo-list)))
2561 ((eq type 'search)
2562 (org-let2 gprops lprops
2563 '(org-search-view current-prefix-arg match nil)))
2564 ((eq type 'stuck)
2565 (org-let2 gprops lprops
2566 '(call-interactively 'org-agenda-list-stuck-projects)))
2567 ((eq type 'tags)
2568 (org-let2 gprops lprops
2569 '(org-tags-view current-prefix-arg match)))
2570 ((eq type 'tags-todo)
2571 (org-let2 gprops lprops
2572 '(org-tags-view '(4) match)))
2573 ((eq type 'todo)
2574 (org-let2 gprops lprops
2575 '(org-todo-list match)))
2576 ((fboundp type)
2577 (org-let2 gprops lprops
2578 '(funcall type match)))
2579 (t (error "Invalid type in command series"))))
2580 (widen)
2581 (setq org-agenda-redo-command redo)
2582 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2583 (goto-char (point-min)))
2584 (org-fit-agenda-window)
2585 (org-let (nth 1 series) '(org-finalize-agenda)))
2587 ;;;###autoload
2588 (defmacro org-batch-agenda (cmd-key &rest parameters)
2589 "Run an agenda command in batch mode and send the result to STDOUT.
2590 If CMD-KEY is a string of length 1, it is used as a key in
2591 `org-agenda-custom-commands' and triggers this command. If it is a
2592 longer string it is used as a tags/todo match string.
2593 Parameters are alternating variable names and values that will be bound
2594 before running the agenda command."
2595 (org-eval-in-environment (org-make-parameter-alist parameters)
2596 (if (> (length cmd-key) 2)
2597 (org-tags-view nil cmd-key)
2598 (org-agenda nil cmd-key)))
2599 (set-buffer org-agenda-buffer-name)
2600 (princ (org-encode-for-stdout (buffer-string))))
2601 (def-edebug-spec org-batch-agenda (form &rest sexp))
2603 ;(defun org-encode-for-stdout (string)
2604 ; (if (fboundp 'encode-coding-string)
2605 ; (encode-coding-string string buffer-file-coding-system)
2606 ; string))
2608 (defun org-encode-for-stdout (string)
2609 string)
2611 (defvar org-agenda-info nil)
2613 ;;;###autoload
2614 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2615 "Run an agenda command in batch mode and send the result to STDOUT.
2616 If CMD-KEY is a string of length 1, it is used as a key in
2617 `org-agenda-custom-commands' and triggers this command. If it is a
2618 longer string it is used as a tags/todo match string.
2619 Parameters are alternating variable names and values that will be bound
2620 before running the agenda command.
2622 The output gives a line for each selected agenda item. Each
2623 item is a list of comma-separated values, like this:
2625 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2627 category The category of the item
2628 head The headline, without TODO kwd, TAGS and PRIORITY
2629 type The type of the agenda entry, can be
2630 todo selected in TODO match
2631 tagsmatch selected in tags match
2632 diary imported from diary
2633 deadline a deadline on given date
2634 scheduled scheduled on given date
2635 timestamp entry has timestamp on given date
2636 closed entry was closed on given date
2637 upcoming-deadline warning about deadline
2638 past-scheduled forwarded scheduled item
2639 block entry has date block including g. date
2640 todo The todo keyword, if any
2641 tags All tags including inherited ones, separated by colons
2642 date The relevant date, like 2007-2-14
2643 time The time, like 15:00-16:50
2644 extra Sting with extra planning info
2645 priority-l The priority letter if any was given
2646 priority-n The computed numerical priority
2647 agenda-day The day in the agenda where this is listed"
2648 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2649 (org-make-parameter-alist parameters))
2650 (if (> (length cmd-key) 2)
2651 (org-tags-view nil cmd-key)
2652 (org-agenda nil cmd-key)))
2653 (set-buffer org-agenda-buffer-name)
2654 (let* ((lines (org-split-string (buffer-string) "\n"))
2655 line)
2656 (while (setq line (pop lines))
2657 (catch 'next
2658 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2659 (setq org-agenda-info
2660 (org-fix-agenda-info (text-properties-at 0 line)))
2661 (princ
2662 (org-encode-for-stdout
2663 (mapconcat 'org-agenda-export-csv-mapper
2664 '(org-category txt type todo tags date time extra
2665 priority-letter priority agenda-day)
2666 ",")))
2667 (princ "\n")))))
2668 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2670 (defun org-fix-agenda-info (props)
2671 "Make sure all properties on an agenda item have a canonical form.
2672 This ensures the export commands can easily use it."
2673 (let (tmp re)
2674 (when (setq tmp (plist-get props 'tags))
2675 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2676 (when (setq tmp (plist-get props 'date))
2677 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2678 (let ((calendar-date-display-form '(year "-" month "-" day)))
2679 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2681 (setq tmp (calendar-date-string tmp)))
2682 (setq props (plist-put props 'date tmp)))
2683 (when (setq tmp (plist-get props 'day))
2684 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2685 (let ((calendar-date-display-form '(year "-" month "-" day)))
2686 (setq tmp (calendar-date-string tmp)))
2687 (setq props (plist-put props 'day tmp))
2688 (setq props (plist-put props 'agenda-day tmp)))
2689 (when (setq tmp (plist-get props 'txt))
2690 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2691 (plist-put props 'priority-letter (match-string 1 tmp))
2692 (setq tmp (replace-match "" t t tmp)))
2693 (when (and (setq re (plist-get props 'org-todo-regexp))
2694 (setq re (concat "\\`\\.*" re " ?"))
2695 (string-match re tmp))
2696 (plist-put props 'todo (match-string 1 tmp))
2697 (setq tmp (replace-match "" t t tmp)))
2698 (plist-put props 'txt tmp)))
2699 props)
2701 (defun org-agenda-export-csv-mapper (prop)
2702 (let ((res (plist-get org-agenda-info prop)))
2703 (setq res
2704 (cond
2705 ((not res) "")
2706 ((stringp res) res)
2707 (t (prin1-to-string res))))
2708 (while (string-match "," res)
2709 (setq res (replace-match ";" t t res)))
2710 (org-trim res)))
2713 ;;;###autoload
2714 (defun org-store-agenda-views (&rest parameters)
2715 (interactive)
2716 (eval (list 'org-batch-store-agenda-views)))
2718 ;;;###autoload
2719 (defmacro org-batch-store-agenda-views (&rest parameters)
2720 "Run all custom agenda commands that have a file argument."
2721 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2722 (pop-up-frames nil)
2723 (dir default-directory)
2724 (pars (org-make-parameter-alist parameters))
2725 cmd thiscmdkey files opts cmd-or-set)
2726 (save-window-excursion
2727 (while cmds
2728 (setq cmd (pop cmds)
2729 thiscmdkey (car cmd)
2730 cmd-or-set (nth 2 cmd)
2731 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2732 files (nth (if (listp cmd-or-set) 4 5) cmd))
2733 (if (stringp files) (setq files (list files)))
2734 (when files
2735 (org-eval-in-environment (append org-agenda-exporter-settings
2736 opts pars)
2737 (org-agenda nil thiscmdkey))
2738 (set-buffer org-agenda-buffer-name)
2739 (while files
2740 (org-eval-in-environment (append org-agenda-exporter-settings
2741 opts pars)
2742 (org-write-agenda (expand-file-name (pop files) dir) nil t)))
2743 (and (get-buffer org-agenda-buffer-name)
2744 (kill-buffer org-agenda-buffer-name)))))))
2745 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2747 (defun org-agenda-mark-header-line (pos)
2748 "Mark the line at POS as an agenda structure header."
2749 (save-excursion
2750 (goto-char pos)
2751 (put-text-property (point-at-bol) (point-at-eol)
2752 'org-agenda-structural-header t)
2753 (when org-agenda-title-append
2754 (put-text-property (point-at-bol) (point-at-eol)
2755 'org-agenda-title-append org-agenda-title-append))))
2757 (defvar org-mobile-creating-agendas)
2758 (defun org-write-agenda (file &optional open nosettings)
2759 "Write the current buffer (an agenda view) as a file.
2760 Depending on the extension of the file name, plain text (.txt),
2761 HTML (.html or .htm) or Postscript (.ps) is produced.
2762 If the extension is .ics, run icalendar export over all files used
2763 to construct the agenda and limit the export to entries listed in the
2764 agenda now.
2765 With prefix argument OPEN, open the new file immediately.
2766 If NOSETTINGS is given, do not scope the settings of
2767 `org-agenda-exporter-settings' into the export commands. This is used when
2768 the settings have already been scoped and we do not wish to overrule other,
2769 higher priority settings."
2770 (interactive "FWrite agenda to file: \nP")
2771 (if (not (file-writable-p file))
2772 (error "Cannot write agenda to file %s" file))
2773 (org-let (if nosettings nil org-agenda-exporter-settings)
2774 '(save-excursion
2775 (save-window-excursion
2776 (org-agenda-mark-filtered-text)
2777 (let ((bs (copy-sequence (buffer-string))) beg)
2778 (org-agenda-unmark-filtered-text)
2779 (with-temp-buffer
2780 (rename-buffer "Agenda View" t)
2781 (set-buffer-modified-p nil)
2782 (insert bs)
2783 (org-agenda-remove-marked-text 'org-filtered)
2784 (while (setq beg (text-property-any (point-min) (point-max)
2785 'org-filtered t))
2786 (delete-region
2787 beg (or (next-single-property-change beg 'org-filtered)
2788 (point-max))))
2789 (run-hooks 'org-agenda-before-write-hook)
2790 (cond
2791 ((org-bound-and-true-p org-mobile-creating-agendas)
2792 (org-mobile-write-agenda-for-mobile file))
2793 ((string-match "\\.html?\\'" file)
2794 (require 'htmlize)
2795 (set-buffer (htmlize-buffer (current-buffer)))
2797 (when (and org-agenda-export-html-style
2798 (string-match "<style>" org-agenda-export-html-style))
2799 ;; replace <style> section with org-agenda-export-html-style
2800 (goto-char (point-min))
2801 (kill-region (- (search-forward "<style") 6)
2802 (search-forward "</style>"))
2803 (insert org-agenda-export-html-style))
2804 (write-file file)
2805 (kill-buffer (current-buffer))
2806 (message "HTML written to %s" file))
2807 ((string-match "\\.ps\\'" file)
2808 (require 'ps-print)
2809 (ps-print-buffer-with-faces file)
2810 (message "Postscript written to %s" file))
2811 ((string-match "\\.pdf\\'" file)
2812 (require 'ps-print)
2813 (ps-print-buffer-with-faces
2814 (concat (file-name-sans-extension file) ".ps"))
2815 (call-process "ps2pdf" nil nil nil
2816 (expand-file-name
2817 (concat (file-name-sans-extension file) ".ps"))
2818 (expand-file-name file))
2819 (delete-file (concat (file-name-sans-extension file) ".ps"))
2820 (message "PDF written to %s" file))
2821 ((string-match "\\.ics\\'" file)
2822 (require 'org-icalendar)
2823 (let ((org-agenda-marker-table
2824 (org-create-marker-find-array
2825 (org-agenda-collect-markers)))
2826 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2827 (org-combined-agenda-icalendar-file file))
2828 (apply 'org-export-icalendar 'combine
2829 (org-agenda-files nil 'ifmode))))
2831 (let ((bs (buffer-string)))
2832 (find-file file)
2833 (erase-buffer)
2834 (insert bs)
2835 (save-buffer 0)
2836 (kill-buffer (current-buffer))
2837 (message "Plain text written to %s" file))))))))
2838 (set-buffer org-agenda-buffer-name))
2839 (when open (org-open-file file)))
2841 (defvar org-agenda-filter-overlays nil)
2843 (defun org-agenda-mark-filtered-text ()
2844 "Mark all text hidden by filtering with a text property."
2845 (let ((inhibit-read-only t))
2846 (mapc
2847 (lambda (o)
2848 (when (equal (overlay-buffer o) (current-buffer))
2849 (put-text-property
2850 (overlay-start o) (overlay-end o)
2851 'org-filtered t)))
2852 org-agenda-filter-overlays)))
2854 (defun org-agenda-unmark-filtered-text ()
2855 "Remove the filtering text property."
2856 (let ((inhibit-read-only t))
2857 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2859 (defun org-agenda-remove-marked-text (property &optional value)
2860 "Delete all text marked with VALUE of PROPERTY.
2861 VALUE defaults to t."
2862 (let (beg)
2863 (setq value (or value t))
2864 (while (setq beg (text-property-any (point-min) (point-max)
2865 property value))
2866 (delete-region
2867 beg (or (next-single-property-change beg 'org-filtered)
2868 (point-max))))))
2870 (defun org-agenda-add-entry-text ()
2871 "Add entry text to agenda lines.
2872 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2873 entry text following headings shown in the agenda.
2874 Drawers will be excluded, also the line with scheduling/deadline info."
2875 (when (and (> org-agenda-add-entry-text-maxlines 0)
2876 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2877 (let (m txt)
2878 (goto-char (point-min))
2879 (while (not (eobp))
2880 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2881 (beginning-of-line 2)
2882 (setq txt (org-agenda-get-some-entry-text
2883 m org-agenda-add-entry-text-maxlines " > "))
2884 (end-of-line 1)
2885 (if (string-match "\\S-" txt)
2886 (insert "\n" txt)
2887 (or (eobp) (forward-char 1))))))))
2889 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2890 &rest keep)
2891 "Extract entry text from MARKER, at most N-LINES lines.
2892 This will ignore drawers etc, just get the text.
2893 If INDENT is given, prefix every line with this string. If KEEP is
2894 given, it is a list of symbols, defining stuff that should not be
2895 removed from the entry content. Currently only `planning' is allowed here."
2896 (let (txt drawer-re kwd-time-re ind)
2897 (save-excursion
2898 (with-current-buffer (marker-buffer marker)
2899 (if (not (org-mode-p))
2900 (setq txt "")
2901 (save-excursion
2902 (save-restriction
2903 (widen)
2904 (goto-char marker)
2905 (end-of-line 1)
2906 (setq txt (buffer-substring
2907 (min (1+ (point)) (point-max))
2908 (progn (outline-next-heading) (point)))
2909 drawer-re org-drawer-regexp
2910 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2911 ".*\n?"))
2912 (with-temp-buffer
2913 (insert txt)
2914 (when org-agenda-add-entry-text-descriptive-links
2915 (goto-char (point-min))
2916 (while (org-activate-bracket-links (point-max))
2917 (add-text-properties (match-beginning 0) (match-end 0)
2918 '(face org-link))))
2919 (goto-char (point-min))
2920 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2921 (set-text-properties (match-beginning 0) (match-end 0)
2922 nil))
2923 (goto-char (point-min))
2924 (while (re-search-forward drawer-re nil t)
2925 (delete-region
2926 (match-beginning 0)
2927 (progn (re-search-forward
2928 "^[ \t]*:END:.*\n?" nil 'move)
2929 (point))))
2930 (unless (member 'planning keep)
2931 (goto-char (point-min))
2932 (while (re-search-forward kwd-time-re nil t)
2933 (replace-match "")))
2934 (goto-char (point-min))
2935 (when org-agenda-entry-text-exclude-regexps
2936 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2937 (while (setq re (pop re-list))
2938 (goto-char (point-min))
2939 (while (re-search-forward re nil t)
2940 (replace-match "")))))
2941 (goto-char (point-max))
2942 (skip-chars-backward " \t\n")
2943 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2945 ;; find and remove min common indentation
2946 (goto-char (point-min))
2947 (untabify (point-min) (point-max))
2948 (setq ind (org-get-indentation))
2949 (while (not (eobp))
2950 (unless (looking-at "[ \t]*$")
2951 (setq ind (min ind (org-get-indentation))))
2952 (beginning-of-line 2))
2953 (goto-char (point-min))
2954 (while (not (eobp))
2955 (unless (looking-at "[ \t]*$")
2956 (move-to-column ind)
2957 (delete-region (point-at-bol) (point)))
2958 (beginning-of-line 2))
2960 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2962 (goto-char (point-min))
2963 (when indent
2964 (while (and (not (eobp)) (re-search-forward "^" nil t))
2965 (replace-match indent t t)))
2966 (goto-char (point-min))
2967 (while (looking-at "[ \t]*\n") (replace-match ""))
2968 (goto-char (point-max))
2969 (when (> (org-current-line)
2970 n-lines)
2971 (org-goto-line (1+ n-lines))
2972 (backward-char 1))
2973 (setq txt (buffer-substring (point-min) (point)))))))))
2974 txt))
2976 (defun org-agenda-collect-markers ()
2977 "Collect the markers pointing to entries in the agenda buffer."
2978 (let (m markers)
2979 (save-excursion
2980 (goto-char (point-min))
2981 (while (not (eobp))
2982 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2983 (org-get-at-bol 'org-marker)))
2984 (push m markers))
2985 (beginning-of-line 2)))
2986 (nreverse markers)))
2988 (defun org-create-marker-find-array (marker-list)
2989 "Create a alist of files names with all marker positions in that file."
2990 (let (f tbl m a p)
2991 (while (setq m (pop marker-list))
2992 (setq p (marker-position m)
2993 f (buffer-file-name (or (buffer-base-buffer
2994 (marker-buffer m))
2995 (marker-buffer m))))
2996 (if (setq a (assoc f tbl))
2997 (push (marker-position m) (cdr a))
2998 (push (list f p) tbl)))
2999 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3000 tbl)))
3002 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3003 (defun org-check-agenda-marker-table ()
3004 "Check of the current entry is on the marker list."
3005 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3007 (and (setq a (assoc file org-agenda-marker-table))
3008 (save-match-data
3009 (save-excursion
3010 (org-back-to-heading t)
3011 (member (point) (cdr a)))))))
3013 (defun org-check-for-org-mode ()
3014 "Make sure current buffer is in org-mode. Error if not."
3015 (or (org-mode-p)
3016 (error "Cannot execute org-mode agenda command on buffer in %s"
3017 major-mode)))
3019 (defun org-fit-agenda-window ()
3020 "Fit the window to the buffer size."
3021 (and (memq org-agenda-window-setup '(reorganize-frame))
3022 (fboundp 'fit-window-to-buffer)
3023 (org-fit-window-to-buffer
3025 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3026 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3028 ;;; Agenda prepare and finalize
3030 (defvar org-agenda-multi nil) ; dynamically scoped
3031 (defvar org-agenda-buffer-name "*Org Agenda*")
3032 (defvar org-pre-agenda-window-conf nil)
3033 (defvar org-agenda-columns-active nil)
3034 (defvar org-agenda-name nil)
3035 (defvar org-agenda-filter nil)
3036 (defvar org-agenda-filter-while-redo nil)
3037 (defvar org-agenda-filter-preset nil
3038 "A preset of the tags filter used for secondary agenda filtering.
3039 This must be a list of strings, each string must be a single tag preceded
3040 by \"+\" or \"-\".
3041 This variable should not be set directly, but agenda custom commands can
3042 bind it in the options section. The preset filter is a global property of
3043 the entire agenda view. In a block agenda, it will not work reliably to
3044 define a filter for one of the individual blocks. You need to set it in
3045 the global options and expect it to be applied to the entire view.")
3047 (defun org-prepare-agenda (&optional name)
3048 (setq org-todo-keywords-for-agenda nil)
3049 (setq org-done-keywords-for-agenda nil)
3050 (setq org-drawers-for-agenda nil)
3051 (unless org-agenda-persistent-filter
3052 (setq org-agenda-filter nil))
3053 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3054 (if org-agenda-multi
3055 (progn
3056 (setq buffer-read-only nil)
3057 (goto-char (point-max))
3058 (unless (or (bobp) org-agenda-compact-blocks
3059 (not org-agenda-block-separator))
3060 (insert "\n"
3061 (if (stringp org-agenda-block-separator)
3062 org-agenda-block-separator
3063 (make-string (window-width) org-agenda-block-separator))
3064 "\n"))
3065 (narrow-to-region (point) (point-max)))
3066 (org-agenda-reset-markers)
3067 (setq org-agenda-contributing-files nil)
3068 (setq org-agenda-columns-active nil)
3069 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3070 (setq org-todo-keywords-for-agenda
3071 (org-uniquify org-todo-keywords-for-agenda))
3072 (setq org-done-keywords-for-agenda
3073 (org-uniquify org-done-keywords-for-agenda))
3074 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3075 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3076 (awin (get-buffer-window abuf)))
3077 (cond
3078 ((equal (current-buffer) abuf) nil)
3079 (awin (select-window awin))
3080 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3081 ((equal org-agenda-window-setup 'current-window)
3082 (org-pop-to-buffer-same-window abuf))
3083 ((equal org-agenda-window-setup 'other-window)
3084 (org-switch-to-buffer-other-window abuf))
3085 ((equal org-agenda-window-setup 'other-frame)
3086 (switch-to-buffer-other-frame abuf))
3087 ((equal org-agenda-window-setup 'reorganize-frame)
3088 (delete-other-windows)
3089 (org-switch-to-buffer-other-window abuf)))
3090 ;; additional test in case agenda is invoked from within agenda
3091 ;; buffer via elisp link
3092 (unless (equal (current-buffer) abuf)
3093 (org-pop-to-buffer-same-window abuf)))
3094 (setq buffer-read-only nil)
3095 (let ((inhibit-read-only t)) (erase-buffer))
3096 (org-agenda-mode)
3097 (and name (not org-agenda-name)
3098 (org-set-local 'org-agenda-name name)))
3099 (setq buffer-read-only nil))
3101 (defun org-finalize-agenda ()
3102 "Finishing touch for the agenda buffer, called just before displaying it."
3103 (unless org-agenda-multi
3104 (save-excursion
3105 (let ((inhibit-read-only t))
3106 (goto-char (point-min))
3107 (while (org-activate-bracket-links (point-max))
3108 (add-text-properties (match-beginning 0) (match-end 0)
3109 '(face org-link)))
3110 (org-agenda-align-tags)
3111 (unless org-agenda-with-colors
3112 (remove-text-properties (point-min) (point-max) '(face nil))))
3113 (if (and (boundp 'org-agenda-overriding-columns-format)
3114 org-agenda-overriding-columns-format)
3115 (org-set-local 'org-agenda-overriding-columns-format
3116 org-agenda-overriding-columns-format))
3117 (if (and (boundp 'org-agenda-view-columns-initially)
3118 org-agenda-view-columns-initially)
3119 (org-agenda-columns))
3120 (when org-agenda-fontify-priorities
3121 (org-agenda-fontify-priorities))
3122 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3123 (org-agenda-dim-blocked-tasks))
3124 (org-agenda-mark-clocking-task)
3125 (when org-agenda-entry-text-mode
3126 (org-agenda-entry-text-hide)
3127 (org-agenda-entry-text-show))
3128 (if (functionp 'org-habit-insert-consistency-graphs)
3129 (org-habit-insert-consistency-graphs))
3130 (run-hooks 'org-finalize-agenda-hook)
3131 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3132 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3133 (org-agenda-filter-apply org-agenda-filter))
3136 (defun org-agenda-mark-clocking-task ()
3137 "Mark the current clock entry in the agenda if it is present."
3138 (mapc (lambda (o)
3139 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3140 (delete-overlay o)))
3141 (overlays-in (point-min) (point-max)))
3142 (when (marker-buffer org-clock-hd-marker)
3143 (save-excursion
3144 (goto-char (point-min))
3145 (let (s ov)
3146 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3147 (goto-char s)
3148 (when (equal (org-get-at-bol 'org-hd-marker)
3149 org-clock-hd-marker)
3150 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3151 (overlay-put ov 'type 'org-agenda-clocking)
3152 (overlay-put ov 'face 'org-agenda-clocking)
3153 (overlay-put ov 'help-echo
3154 "The clock is running in this item")))))))
3156 (defun org-agenda-fontify-priorities ()
3157 "Make highest priority lines bold, and lowest italic."
3158 (interactive)
3159 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3160 (delete-overlay o)))
3161 (overlays-in (point-min) (point-max)))
3162 (save-excursion
3163 (let ((inhibit-read-only t)
3164 b e p ov h l)
3165 (goto-char (point-min))
3166 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3167 (setq h (or (get-char-property (point) 'org-highest-priority)
3168 org-highest-priority)
3169 l (or (get-char-property (point) 'org-lowest-priority)
3170 org-lowest-priority)
3171 p (string-to-char (match-string 1))
3172 b (match-beginning 0)
3173 e (if (eq org-agenda-fontify-priorities 'cookies)
3174 (match-end 0)
3175 (point-at-eol))
3176 ov (make-overlay b e))
3177 (overlay-put
3178 ov 'face
3179 (cond ((org-face-from-face-or-color
3180 'priority nil
3181 (cdr (assoc p org-priority-faces))))
3182 ((and (listp org-agenda-fontify-priorities)
3183 (org-face-from-face-or-color
3184 'priority nil
3185 (cdr (assoc p org-agenda-fontify-priorities)))))
3186 ((equal p l) 'italic)
3187 ((equal p h) 'bold)))
3188 (overlay-put ov 'org-type 'org-priority)))))
3190 (defun org-agenda-dim-blocked-tasks ()
3191 "Dim currently blocked TODO's in the agenda display."
3192 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3193 (delete-overlay o)))
3194 (overlays-in (point-min) (point-max)))
3195 (save-excursion
3196 (let ((inhibit-read-only t)
3197 (org-depend-tag-blocked nil)
3198 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3199 org-blocked-by-checkboxes
3200 invis1 b e p ov h l)
3201 (goto-char (point-min))
3202 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3203 (and pos (goto-char (1+ pos))))
3204 (setq org-blocked-by-checkboxes nil invis1 invis)
3205 (let ((marker (org-get-at-bol 'org-hd-marker)))
3206 (when (and marker
3207 (with-current-buffer (marker-buffer marker)
3208 (save-excursion (goto-char marker)
3209 (org-entry-blocked-p))))
3210 (if org-blocked-by-checkboxes (setq invis1 nil))
3211 (setq b (if invis1
3212 (max (point-min) (1- (point-at-bol)))
3213 (point-at-bol))
3214 e (point-at-eol)
3215 ov (make-overlay b e))
3216 (if invis1
3217 (overlay-put ov 'invisible t)
3218 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3219 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3221 (defvar org-agenda-skip-function nil
3222 "Function to be called at each match during agenda construction.
3223 If this function returns nil, the current match should not be skipped.
3224 Otherwise, the function must return a position from where the search
3225 should be continued.
3226 This may also be a Lisp form, it will be evaluated.
3227 Never set this variable using `setq' or so, because then it will apply
3228 to all future agenda commands. If you do want a global skipping condition,
3229 use the option `org-agenda-skip-function-global' instead.
3230 The correct usage for `org-agenda-skip-function' is to bind it with
3231 `let' to scope it dynamically into the agenda-constructing command.
3232 A good way to set it is through options in `org-agenda-custom-commands'.")
3234 (defun org-agenda-skip ()
3235 "Throw to `:skip' in places that should be skipped.
3236 Also moves point to the end of the skipped region, so that search can
3237 continue from there."
3238 (let ((p (point-at-bol)) to)
3239 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3240 (get-text-property p :org-archived)
3241 (org-end-of-subtree t)
3242 (throw :skip t))
3243 (and org-agenda-skip-comment-trees
3244 (get-text-property p :org-comment)
3245 (org-end-of-subtree t)
3246 (throw :skip t))
3247 (if (equal (char-after p) ?#) (throw :skip t))
3248 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3249 (org-agenda-skip-eval org-agenda-skip-function)))
3250 (goto-char to)
3251 (throw :skip t))))
3253 (defun org-agenda-skip-eval (form)
3254 "If FORM is a function or a list, call (or eval) is and return result.
3255 `save-excursion' and `save-match-data' are wrapped around the call, so point
3256 and match data are returned to the previous state no matter what these
3257 functions do."
3258 (let (fp)
3259 (and form
3260 (or (setq fp (functionp form))
3261 (consp form))
3262 (save-excursion
3263 (save-match-data
3264 (if fp
3265 (funcall form)
3266 (eval form)))))))
3268 (defvar org-agenda-markers nil
3269 "List of all currently active markers created by `org-agenda'.")
3270 (defvar org-agenda-last-marker-time (org-float-time)
3271 "Creation time of the last agenda marker.")
3273 (defun org-agenda-new-marker (&optional pos)
3274 "Return a new agenda marker.
3275 Org-mode keeps a list of these markers and resets them when they are
3276 no longer in use."
3277 (let ((m (copy-marker (or pos (point)))))
3278 (setq org-agenda-last-marker-time (org-float-time))
3279 (push m org-agenda-markers)
3282 (defun org-agenda-reset-markers ()
3283 "Reset markers created by `org-agenda'."
3284 (while org-agenda-markers
3285 (move-marker (pop org-agenda-markers) nil)))
3287 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3288 "Save relative positions of markers in region."
3289 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3290 org-agenda-markers))
3292 ;;; Entry text mode
3294 (defun org-agenda-entry-text-show-here ()
3295 "Add some text from the entry as context to the current line."
3296 (let (m txt o)
3297 (setq m (org-get-at-bol 'org-hd-marker))
3298 (unless (marker-buffer m)
3299 (error "No marker points to an entry here"))
3300 (setq txt (concat "\n" (org-no-properties
3301 (org-agenda-get-some-entry-text
3302 m org-agenda-entry-text-maxlines " > "))))
3303 (when (string-match "\\S-" txt)
3304 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3305 (overlay-put o 'evaporate t)
3306 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3307 (overlay-put o 'after-string txt))))
3309 (defun org-agenda-entry-text-show ()
3310 "Add entry context for all agenda lines."
3311 (interactive)
3312 (save-excursion
3313 (goto-char (point-max))
3314 (beginning-of-line 1)
3315 (while (not (bobp))
3316 (when (org-get-at-bol 'org-hd-marker)
3317 (org-agenda-entry-text-show-here))
3318 (beginning-of-line 0))))
3320 (defun org-agenda-entry-text-hide ()
3321 "Remove any shown entry context."
3322 (delq nil
3323 (mapcar (lambda (o)
3324 (if (eq (overlay-get o 'org-overlay-type)
3325 'agenda-entry-content)
3326 (progn (delete-overlay o) t)))
3327 (overlays-in (point-min) (point-max)))))
3329 (defun org-agenda-get-day-face (date)
3330 "Return the face DATE should be displayed with."
3331 (or (and (functionp org-agenda-day-face-function)
3332 (funcall org-agenda-day-face-function date))
3333 (cond ((org-agenda-todayp date)
3334 'org-agenda-date-today)
3335 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3336 'org-agenda-date-weekend)
3337 (t 'org-agenda-date))))
3339 ;;; Agenda timeline
3341 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3343 (defun org-timeline (&optional dotodo)
3344 "Show a time-sorted view of the entries in the current org file.
3345 Only entries with a time stamp of today or later will be listed. With
3346 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3347 under the current date.
3348 If the buffer contains an active region, only check the region for
3349 dates."
3350 (interactive "P")
3351 (org-compile-prefix-format 'timeline)
3352 (org-set-sorting-strategy 'timeline)
3353 (let* ((dopast t)
3354 (doclosed org-agenda-show-log)
3355 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3356 (current-buffer))))
3357 (date (calendar-current-date))
3358 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3359 (end (if (org-region-active-p) (region-end) (point-max)))
3360 (day-numbers (org-get-all-dates beg end 'no-ranges
3361 t doclosed ; always include today
3362 org-timeline-show-empty-dates))
3363 (org-deadline-warning-days 0)
3364 (org-agenda-only-exact-dates t)
3365 (today (org-today))
3366 (past t)
3367 args
3368 s e rtn d emptyp)
3369 (setq org-agenda-redo-command
3370 (list 'progn
3371 (list 'org-switch-to-buffer-other-window (current-buffer))
3372 (list 'org-timeline (list 'quote dotodo))))
3373 (if (not dopast)
3374 ;; Remove past dates from the list of dates.
3375 (setq day-numbers (delq nil (mapcar (lambda(x)
3376 (if (>= x today) x nil))
3377 day-numbers))))
3378 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3379 (if doclosed (push :closed args))
3380 (push :timestamp args)
3381 (push :deadline args)
3382 (push :scheduled args)
3383 (push :sexp args)
3384 (if dotodo (push :todo args))
3385 (insert "Timeline of file " entry "\n")
3386 (add-text-properties (point-min) (point)
3387 (list 'face 'org-agenda-structure))
3388 (org-agenda-mark-header-line (point-min))
3389 (while (setq d (pop day-numbers))
3390 (if (and (listp d) (eq (car d) :omitted))
3391 (progn
3392 (setq s (point))
3393 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3394 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3395 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3396 (if (and (>= d today)
3397 dopast
3398 past)
3399 (progn
3400 (setq past nil)
3401 (insert (make-string 79 ?-) "\n")))
3402 (setq date (calendar-gregorian-from-absolute d))
3403 (setq s (point))
3404 (setq rtn (and (not emptyp)
3405 (apply 'org-agenda-get-day-entries entry
3406 date args)))
3407 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3408 (progn
3409 (insert
3410 (if (stringp org-agenda-format-date)
3411 (format-time-string org-agenda-format-date
3412 (org-time-from-absolute date))
3413 (funcall org-agenda-format-date date))
3414 "\n")
3415 (put-text-property s (1- (point)) 'face
3416 (org-agenda-get-day-face date))
3417 (put-text-property s (1- (point)) 'org-date-line t)
3418 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3419 (if (equal d today)
3420 (put-text-property s (1- (point)) 'org-today t))
3421 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3422 (put-text-property s (1- (point)) 'day d)))))
3423 (goto-char (point-min))
3424 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3425 (point-min)))
3426 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3427 (org-finalize-agenda)
3428 (setq buffer-read-only t)))
3430 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3431 "Return a list of all relevant day numbers from BEG to END buffer positions.
3432 If NO-RANGES is non-nil, include only the start and end dates of a range,
3433 not every single day in the range. If FORCE-TODAY is non-nil, make
3434 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3435 inactive time stamps (those in square brackets) are included.
3436 When EMPTY is non-nil, also include days without any entries."
3437 (let ((re (concat
3438 (if pre-re pre-re "")
3439 (if inactive org-ts-regexp-both org-ts-regexp)))
3440 dates dates1 date day day1 day2 ts1 ts2 pos)
3441 (if force-today
3442 (setq dates (list (org-today))))
3443 (save-excursion
3444 (goto-char beg)
3445 (while (re-search-forward re end t)
3446 (setq day (time-to-days (org-time-string-to-time
3447 (substring (match-string 1) 0 10)
3448 (current-buffer) (match-beginning 0))))
3449 (or (memq day dates) (push day dates)))
3450 (unless no-ranges
3451 (goto-char beg)
3452 (while (re-search-forward org-tr-regexp end t)
3453 (setq pos (match-beginning 0))
3454 (setq ts1 (substring (match-string 1) 0 10)
3455 ts2 (substring (match-string 2) 0 10)
3456 day1 (time-to-days (org-time-string-to-time
3457 ts1 (current-buffer) pos))
3458 day2 (time-to-days (org-time-string-to-time
3459 ts2 (current-buffer) pos)))
3460 (while (< (setq day1 (1+ day1)) day2)
3461 (or (memq day1 dates) (push day1 dates)))))
3462 (setq dates (sort dates '<))
3463 (when empty
3464 (while (setq day (pop dates))
3465 (setq day2 (car dates))
3466 (push day dates1)
3467 (when (and day2 empty)
3468 (if (or (eq empty t)
3469 (and (numberp empty) (<= (- day2 day) empty)))
3470 (while (< (setq day (1+ day)) day2)
3471 (push (list day) dates1))
3472 (push (cons :omitted (- day2 day)) dates1))))
3473 (setq dates (nreverse dates1)))
3474 dates)))
3476 ;;; Agenda Daily/Weekly
3478 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3479 "Start day for the agenda view.
3480 Custom commands can set this variable in the options section.")
3481 (defvar org-starting-day nil) ; local variable in the agenda buffer
3482 (defvar org-agenda-current-span nil
3483 "The current span used in the agenda view.") ; local variable in the agenda buffer
3484 (defvar org-arg-loc nil) ; local variable
3486 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3487 "List of types searched for when creating the daily/weekly agenda.
3488 This variable is a list of symbols that controls the types of
3489 items that appear in the daily/weekly agenda. Allowed symbols in this
3490 list are are
3492 :timestamp List items containing a date stamp or date range matching
3493 the selected date. This includes sexp entries in
3494 angular brackets.
3496 :sexp List entries resulting from plain diary-like sexps.
3498 :deadline List deadline due on that date. When the date is today,
3499 also list any deadlines past due, or due within
3500 `org-deadline-warning-days'. `:deadline' must appear before
3501 `:scheduled' if the setting of
3502 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3503 any effect.
3505 :scheduled List all items which are scheduled for the given date.
3506 The diary for *today* also contains items which were
3507 scheduled earlier and are not yet marked DONE.
3509 By default, all four types are turned on.
3511 Never set this variable globally using `setq', because then it
3512 will apply to all future agenda commands. Instead, bind it with
3513 `let' to scope it dynamically into the agenda-constructing
3514 command. A good way to set it is through options in
3515 `org-agenda-custom-commands'. For a more flexible (though
3516 somewhat less efficient) way of determining what is included in
3517 the daily/weekly agenda, see `org-agenda-skip-function'.")
3519 ;;;###autoload
3520 (defun org-agenda-list (&optional arg start-day span)
3521 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3522 The view will be for the current day or week, but from the overview buffer
3523 you will be able to go to other days/weeks.
3525 With a numeric prefix argument in an interactive call, the agenda will
3526 span ARG days. Lisp programs should instead specify SPAN to change
3527 the number of days. SPAN defaults to `org-agenda-span'.
3529 START-DAY defaults to TODAY, or to the most recent match for the weekday
3530 given in `org-agenda-start-on-weekday'."
3531 (interactive "P")
3532 (if (and (integerp arg) (> arg 0))
3533 (setq span arg arg nil))
3534 (setq start-day (or start-day org-agenda-start-day))
3535 (if org-agenda-overriding-arguments
3536 (setq arg (car org-agenda-overriding-arguments)
3537 start-day (nth 1 org-agenda-overriding-arguments)
3538 span (nth 2 org-agenda-overriding-arguments)))
3539 (if (stringp start-day)
3540 ;; Convert to an absolute day number
3541 (setq start-day (time-to-days (org-read-date nil t start-day))))
3542 (setq org-agenda-last-arguments (list arg start-day span))
3543 (org-compile-prefix-format 'agenda)
3544 (org-set-sorting-strategy 'agenda)
3545 (let* ((span (org-agenda-ndays-to-span
3546 (or span org-agenda-ndays org-agenda-span)))
3547 (today (org-today))
3548 (sd (or start-day today))
3549 (ndays (org-agenda-span-to-ndays span sd))
3550 (org-agenda-start-on-weekday
3551 (if (eq ndays 7)
3552 org-agenda-start-on-weekday))
3553 (thefiles (org-agenda-files nil 'ifmode))
3554 (files thefiles)
3555 (start (if (or (null org-agenda-start-on-weekday)
3556 (< ndays 7))
3558 (let* ((nt (calendar-day-of-week
3559 (calendar-gregorian-from-absolute sd)))
3560 (n1 org-agenda-start-on-weekday)
3561 (d (- nt n1)))
3562 (- sd (+ (if (< d 0) 7 0) d)))))
3563 (day-numbers (list start))
3564 (day-cnt 0)
3565 (inhibit-redisplay (not debug-on-error))
3566 s e rtn rtnall file date d start-pos end-pos todayp
3567 clocktable-start clocktable-end filter)
3568 (setq org-agenda-redo-command
3569 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3570 (dotimes (n (1- ndays))
3571 (push (1+ (car day-numbers)) day-numbers))
3572 (setq day-numbers (nreverse day-numbers))
3573 (setq clocktable-start (car day-numbers)
3574 clocktable-end (1+ (or (org-last day-numbers) 0)))
3575 (org-prepare-agenda "Day/Week")
3576 (org-set-local 'org-starting-day (car day-numbers))
3577 (org-set-local 'org-arg-loc arg)
3578 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3579 (unless org-agenda-compact-blocks
3580 (let* ((d1 (car day-numbers))
3581 (d2 (org-last day-numbers))
3582 (w1 (org-days-to-iso-week d1))
3583 (w2 (org-days-to-iso-week d2)))
3584 (setq s (point))
3585 (if org-agenda-overriding-header
3586 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3587 nil 'face 'org-agenda-structure) "\n")
3588 (insert (org-agenda-span-name span)
3589 "-agenda"
3590 (if (< (- d2 d1) 350)
3591 (if (= w1 w2)
3592 (format " (W%02d)" w1)
3593 (format " (W%02d-W%02d)" w1 w2))
3595 ":\n")))
3596 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3597 'org-date-line t))
3598 (org-agenda-mark-header-line s))
3599 (while (setq d (pop day-numbers))
3600 (setq date (calendar-gregorian-from-absolute d)
3601 s (point))
3602 (if (or (setq todayp (= d today))
3603 (and (not start-pos) (= d sd)))
3604 (setq start-pos (point))
3605 (if (and start-pos (not end-pos))
3606 (setq end-pos (point))))
3607 (setq files thefiles
3608 rtnall nil)
3609 (while (setq file (pop files))
3610 (catch 'nextfile
3611 (org-check-agenda-file file)
3612 (let ((org-agenda-entry-types org-agenda-entry-types))
3613 (unless org-agenda-include-deadlines
3614 (setq org-agenda-entry-types
3615 (delq :deadline org-agenda-entry-types)))
3616 (cond
3617 ((memq org-agenda-show-log '(only clockcheck))
3618 (setq rtn (org-agenda-get-day-entries
3619 file date :closed)))
3620 (org-agenda-show-log
3621 (setq rtn (apply 'org-agenda-get-day-entries
3622 file date
3623 (append '(:closed) org-agenda-entry-types))))
3625 (setq rtn (apply 'org-agenda-get-day-entries
3626 file date
3627 org-agenda-entry-types)))))
3628 (setq rtnall (append rtnall rtn))))
3629 (if org-agenda-include-diary
3630 (let ((org-agenda-search-headline-for-time t))
3631 (require 'diary-lib)
3632 (setq rtn (org-get-entries-from-diary date))
3633 (setq rtnall (append rtnall rtn))))
3634 (if (or rtnall org-agenda-show-all-dates)
3635 (progn
3636 (setq day-cnt (1+ day-cnt))
3637 (insert
3638 (if (stringp org-agenda-format-date)
3639 (format-time-string org-agenda-format-date
3640 (org-time-from-absolute date))
3641 (funcall org-agenda-format-date date))
3642 "\n")
3643 (put-text-property s (1- (point)) 'face
3644 (org-agenda-get-day-face date))
3645 (put-text-property s (1- (point)) 'org-date-line t)
3646 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3647 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3648 (when todayp
3649 (put-text-property s (1- (point)) 'org-today t))
3650 (if rtnall (insert
3651 (org-finalize-agenda-entries
3652 (org-agenda-add-time-grid-maybe
3653 rtnall ndays todayp))
3654 "\n"))
3655 (put-text-property s (1- (point)) 'day d)
3656 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3657 (when (and org-agenda-clockreport-mode clocktable-start)
3658 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3659 ;; the above line is to ensure the restricted range!
3660 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3661 tbl)
3662 (setq p (org-plist-delete p :block))
3663 (setq p (plist-put p :tstart clocktable-start))
3664 (setq p (plist-put p :tend clocktable-end))
3665 (setq p (plist-put p :scope 'agenda))
3666 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3667 (setq filter (or org-agenda-filter-while-redo
3668 (get 'org-agenda-filter :preset-filter))))
3669 (setq p (plist-put p :tags (mapconcat (lambda (x)
3670 (if (string-match "[<>=]" x)
3673 filter ""))))
3674 (setq tbl (apply 'org-get-clocktable p))
3675 (insert tbl)))
3676 (goto-char (point-min))
3677 (or org-agenda-multi (org-fit-agenda-window))
3678 (unless (and (pos-visible-in-window-p (point-min))
3679 (pos-visible-in-window-p (point-max)))
3680 (goto-char (1- (point-max)))
3681 (recenter -1)
3682 (if (not (pos-visible-in-window-p (or start-pos 1)))
3683 (progn
3684 (goto-char (or start-pos 1))
3685 (recenter 1))))
3686 (goto-char (or start-pos 1))
3687 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3688 (if (eq org-agenda-show-log 'clockcheck)
3689 (org-agenda-show-clocking-issues))
3690 (org-finalize-agenda)
3691 (setq buffer-read-only t)
3692 (message "")))
3694 (defun org-agenda-ndays-to-span (n)
3695 "Return a span symbol for a span of N days, or N if none matches."
3696 (cond ((symbolp n) n)
3697 ((= n 1) 'day)
3698 ((= n 7) 'week)
3699 (t n)))
3701 (defun org-agenda-span-to-ndays (span start-day)
3702 "Return ndays from SPAN starting at START-DAY."
3703 (cond ((numberp span) span)
3704 ((eq span 'day) 1)
3705 ((eq span 'week) 7)
3706 ((eq span 'month)
3707 (let ((date (calendar-gregorian-from-absolute start-day)))
3708 (calendar-last-day-of-month (car date) (caddr date))))
3709 ((eq span 'year)
3710 (let ((date (calendar-gregorian-from-absolute start-day)))
3711 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3713 (defun org-agenda-span-name (span)
3714 "Return a SPAN name."
3715 (if (null span)
3717 (if (symbolp span)
3718 (capitalize (symbol-name span))
3719 (format "%d days" span))))
3721 ;;; Agenda word search
3723 (defvar org-agenda-search-history nil)
3724 (defvar org-todo-only nil)
3726 (defvar org-search-syntax-table nil
3727 "Special syntax table for org-mode search.
3728 In this table, we have single quotes not as word constituents, to
3729 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3731 (defun org-search-syntax-table ()
3732 (unless org-search-syntax-table
3733 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3734 (modify-syntax-entry ?' "." org-search-syntax-table)
3735 (modify-syntax-entry ?` "." org-search-syntax-table))
3736 org-search-syntax-table)
3738 (defvar org-agenda-last-search-view-search-was-boolean nil)
3740 ;;;###autoload
3741 (defun org-search-view (&optional todo-only string edit-at)
3742 "Show all entries that contain a phrase or words or regular expressions.
3744 With optional prefix argument TODO-ONLY, only consider entries that are
3745 TODO entries. The argument STRING can be used to pass a default search
3746 string into this function. If EDIT-AT is non-nil, it means that the
3747 user should get a chance to edit this string, with cursor at position
3748 EDIT-AT.
3750 The search string can be viewed either as a phrase that should be found as
3751 is, or it can be broken into a number of snippets, each of which must match
3752 in a Boolean way to select an entry. The default depends on the variable
3753 `org-agenda-search-view-always-boolean'.
3754 Even if this is turned off (the default) you can always switch to
3755 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3757 The default is a direct search of the whole phrase, where each space in
3758 the search string can expand to an arbitrary amount of whitespace,
3759 including newlines.
3761 If using a Boolean search, the search string is split on whitespace and
3762 each snippet is searched separately, with logical AND to select an entry.
3763 Words prefixed with a minus must *not* occur in the entry. Words without
3764 a prefix or prefixed with a plus must occur in the entry. Matching is
3765 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3766 match whole words, not parts of a word) if
3767 `org-agenda-search-view-force-full-words' is set (default is nil).
3769 Boolean search snippets enclosed by curly braces are interpreted as
3770 regular expressions that must or (when preceded with \"-\") must not
3771 match in the entry. Snippets enclosed into double quotes will be taken
3772 as a whole, to include whitespace.
3774 - If the search string starts with an asterisk, search only in headlines.
3775 - If (possibly after the leading star) the search string starts with an
3776 exclamation mark, this also means to look at TODO entries only, an effect
3777 that can also be achieved with a prefix argument.
3778 - If (possibly after star and exclamation mark) the search string starts
3779 with a colon, this will mean that the (non-regexp) snippets of the
3780 Boolean search must match as full words.
3782 This command searches the agenda files, and in addition the files listed
3783 in `org-agenda-text-search-extra-files'."
3784 (interactive "P")
3785 (org-compile-prefix-format 'search)
3786 (org-set-sorting-strategy 'search)
3787 (org-prepare-agenda "SEARCH")
3788 (let* ((props (list 'face nil
3789 'done-face 'org-agenda-done
3790 'org-not-done-regexp org-not-done-regexp
3791 'org-todo-regexp org-todo-regexp
3792 'org-complex-heading-regexp org-complex-heading-regexp
3793 'mouse-face 'highlight
3794 'help-echo (format "mouse-2 or RET jump to location")))
3795 (full-words org-agenda-search-view-force-full-words)
3796 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3797 regexp rtn rtnall files file pos
3798 marker category category-pos tags c neg re boolean
3799 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3800 (unless (and (not edit-at)
3801 (stringp string)
3802 (string-match "\\S-" string))
3803 (setq string (read-string
3804 (if org-agenda-search-view-always-boolean
3805 "[+-]Word/{Regexp} ...: "
3806 "Phrase, or [+-]Word/{Regexp} ...: ")
3807 (cond
3808 ((integerp edit-at) (cons string edit-at))
3809 (edit-at string))
3810 'org-agenda-search-history)))
3811 (org-set-local 'org-todo-only todo-only)
3812 (setq org-agenda-redo-command
3813 (list 'org-search-view (if todo-only t nil) string
3814 '(if current-prefix-arg 1 nil)))
3815 (setq org-agenda-query-string string)
3817 (if (equal (string-to-char string) ?*)
3818 (setq hdl-only t
3819 words (substring string 1))
3820 (setq words string))
3821 (when (equal (string-to-char words) ?!)
3822 (setq todo-only t
3823 words (substring words 1)))
3824 (when (equal (string-to-char words) ?:)
3825 (setq full-words t
3826 words (substring words 1)))
3827 (if (or org-agenda-search-view-always-boolean
3828 (member (string-to-char words) '(?- ?+ ?\{)))
3829 (setq boolean t))
3830 (setq words (org-split-string words))
3831 (let (www w)
3832 (while (setq w (pop words))
3833 (while (and (string-match "\\\\\\'" w) words)
3834 (setq w (concat (substring w 0 -1) " " (pop words))))
3835 (push w www))
3836 (setq words (nreverse www) www nil)
3837 (while (setq w (pop words))
3838 (when (and (string-match "\\`[-+]?{" w)
3839 (not (string-match "}\\'" w)))
3840 (while (and words (not (string-match "}\\'" (car words))))
3841 (setq w (concat w " " (pop words))))
3842 (setq w (concat w " " (pop words))))
3843 (push w www))
3844 (setq words (nreverse www)))
3845 (setq org-agenda-last-search-view-search-was-boolean boolean)
3846 (when boolean
3847 (let (wds w)
3848 (while (setq w (pop words))
3849 (if (or (equal (substring w 0 1) "\"")
3850 (and (> (length w) 1)
3851 (member (substring w 0 1) '("+" "-"))
3852 (equal (substring w 1 2) "\"")))
3853 (while (and words (not (equal (substring w -1) "\"")))
3854 (setq w (concat w " " (pop words)))))
3855 (and (string-match "\\`\\([-+]?\\)\"" w)
3856 (setq w (replace-match "\\1" nil nil w)))
3857 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3858 (push w wds))
3859 (setq words (nreverse wds))))
3860 (if boolean
3861 (mapc (lambda (w)
3862 (setq c (string-to-char w))
3863 (if (equal c ?-)
3864 (setq neg t w (substring w 1))
3865 (if (equal c ?+)
3866 (setq neg nil w (substring w 1))
3867 (setq neg nil)))
3868 (if (string-match "\\`{.*}\\'" w)
3869 (setq re (substring w 1 -1))
3870 (if full-words
3871 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3872 (setq re (regexp-quote (downcase w)))))
3873 (if neg (push re regexps-) (push re regexps+)))
3874 words)
3875 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3876 regexps+))
3877 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3878 (if (not regexps+)
3879 (setq regexp org-outline-regexp-bol)
3880 (setq regexp (pop regexps+))
3881 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3882 regexp))))
3883 (setq files (org-agenda-files nil 'ifmode))
3884 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3885 (pop org-agenda-text-search-extra-files)
3886 (setq files (org-add-archive-files files)))
3887 (setq files (append files org-agenda-text-search-extra-files)
3888 rtnall nil)
3889 (while (setq file (pop files))
3890 (setq ee nil)
3891 (catch 'nextfile
3892 (org-check-agenda-file file)
3893 (setq buffer (if (file-exists-p file)
3894 (org-get-agenda-file-buffer file)
3895 (error "No such file %s" file)))
3896 (if (not buffer)
3897 ;; If file does not exist, make sure an error message is sent
3898 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3899 file))))
3900 (with-current-buffer buffer
3901 (with-syntax-table (org-search-syntax-table)
3902 (unless (org-mode-p)
3903 (error "Agenda file %s is not in `org-mode'" file))
3904 (let ((case-fold-search t))
3905 (save-excursion
3906 (save-restriction
3907 (if org-agenda-restrict
3908 (narrow-to-region org-agenda-restrict-begin
3909 org-agenda-restrict-end)
3910 (widen))
3911 (goto-char (point-min))
3912 (unless (or (org-on-heading-p)
3913 (outline-next-heading))
3914 (throw 'nextfile t))
3915 (goto-char (max (point-min) (1- (point))))
3916 (while (re-search-forward regexp nil t)
3917 (org-back-to-heading t)
3918 (skip-chars-forward "* ")
3919 (setq beg (point-at-bol)
3920 beg1 (point)
3921 end (progn (outline-next-heading) (point)))
3922 (catch :skip
3923 (goto-char beg)
3924 (org-agenda-skip)
3925 (setq str (buffer-substring-no-properties
3926 (point-at-bol)
3927 (if hdl-only (point-at-eol) end)))
3928 (mapc (lambda (wr) (when (string-match wr str)
3929 (goto-char (1- end))
3930 (throw :skip t)))
3931 regexps-)
3932 (mapc (lambda (wr) (unless (string-match wr str)
3933 (goto-char (1- end))
3934 (throw :skip t)))
3935 (if todo-only
3936 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3937 regexps+)
3938 regexps+))
3939 (goto-char beg)
3940 (setq marker (org-agenda-new-marker (point))
3941 category (org-get-category)
3942 category-pos (get-text-property (point) 'org-category-position)
3943 tags (org-get-tags-at (point))
3944 txt (org-format-agenda-item
3946 (buffer-substring-no-properties
3947 beg1 (point-at-eol))
3948 category tags))
3949 (org-add-props txt props
3950 'org-marker marker 'org-hd-marker marker
3951 'org-todo-regexp org-todo-regexp
3952 'org-complex-heading-regexp org-complex-heading-regexp
3953 'priority 1000 'org-category category
3954 'org-category-position category-pos
3955 'type "search")
3956 (push txt ee)
3957 (goto-char (1- end))))))))))
3958 (setq rtn (nreverse ee))
3959 (setq rtnall (append rtnall rtn)))
3960 (if org-agenda-overriding-header
3961 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3962 nil 'face 'org-agenda-structure) "\n")
3963 (insert "Search words: ")
3964 (add-text-properties (point-min) (1- (point))
3965 (list 'face 'org-agenda-structure))
3966 (setq pos (point))
3967 (insert string "\n")
3968 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3969 (setq pos (point))
3970 (unless org-agenda-multi
3971 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3972 (add-text-properties pos (1- (point))
3973 (list 'face 'org-agenda-structure))))
3974 (org-agenda-mark-header-line (point-min))
3975 (when rtnall
3976 (insert (org-finalize-agenda-entries rtnall) "\n"))
3977 (goto-char (point-min))
3978 (or org-agenda-multi (org-fit-agenda-window))
3979 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3980 (org-finalize-agenda)
3981 (setq buffer-read-only t)))
3983 ;;; Agenda TODO list
3985 (defvar org-select-this-todo-keyword nil)
3986 (defvar org-last-arg nil)
3988 ;;;###autoload
3989 (defun org-todo-list (arg)
3990 "Show all (not done) TODO entries from all agenda file in a single list.
3991 The prefix arg can be used to select a specific TODO keyword and limit
3992 the list to these. When using \\[universal-argument], you will be prompted
3993 for a keyword. A numeric prefix directly selects the Nth keyword in
3994 `org-todo-keywords-1'."
3995 (interactive "P")
3996 (org-compile-prefix-format 'todo)
3997 (org-set-sorting-strategy 'todo)
3998 (org-prepare-agenda "TODO")
3999 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4000 (let* ((today (org-today))
4001 (date (calendar-gregorian-from-absolute today))
4002 (kwds org-todo-keywords-for-agenda)
4003 (completion-ignore-case t)
4004 (org-select-this-todo-keyword
4005 (if (stringp arg) arg
4006 (and arg (integerp arg) (> arg 0)
4007 (nth (1- arg) kwds))))
4008 rtn rtnall files file pos)
4009 (when (equal arg '(4))
4010 (setq org-select-this-todo-keyword
4011 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4012 (mapcar 'list kwds) nil nil)))
4013 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4014 (org-set-local 'org-last-arg arg)
4015 (setq org-agenda-redo-command
4016 '(org-todo-list (or current-prefix-arg org-last-arg)))
4017 (setq files (org-agenda-files nil 'ifmode)
4018 rtnall nil)
4019 (while (setq file (pop files))
4020 (catch 'nextfile
4021 (org-check-agenda-file file)
4022 (setq rtn (org-agenda-get-day-entries file date :todo))
4023 (setq rtnall (append rtnall rtn))))
4024 (if org-agenda-overriding-header
4025 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4026 nil 'face 'org-agenda-structure) "\n")
4027 (insert "Global list of TODO items of type: ")
4028 (add-text-properties (point-min) (1- (point))
4029 (list 'face 'org-agenda-structure
4030 'short-heading
4031 (concat "ToDo: "
4032 (or org-select-this-todo-keyword "ALL"))))
4033 (org-agenda-mark-header-line (point-min))
4034 (setq pos (point))
4035 (insert (or org-select-this-todo-keyword "ALL") "\n")
4036 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4037 (setq pos (point))
4038 (unless org-agenda-multi
4039 (insert "Available with `N r': (0)ALL")
4040 (let ((n 0) s)
4041 (mapc (lambda (x)
4042 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4043 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4044 (insert "\n "))
4045 (insert " " s))
4046 kwds))
4047 (insert "\n"))
4048 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4049 (org-agenda-mark-header-line (point-min))
4050 (when rtnall
4051 (insert (org-finalize-agenda-entries rtnall) "\n"))
4052 (goto-char (point-min))
4053 (or org-agenda-multi (org-fit-agenda-window))
4054 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4055 (org-finalize-agenda)
4056 (setq buffer-read-only t)))
4058 ;;; Agenda tags match
4060 ;;;###autoload
4061 (defun org-tags-view (&optional todo-only match)
4062 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4063 The prefix arg TODO-ONLY limits the search to TODO entries."
4064 (interactive "P")
4065 (org-compile-prefix-format 'tags)
4066 (org-set-sorting-strategy 'tags)
4067 (let* ((org-tags-match-list-sublevels
4068 org-tags-match-list-sublevels)
4069 (completion-ignore-case t)
4070 rtn rtnall files file pos matcher
4071 buffer)
4072 (when (and (stringp match) (not (string-match "\\S-" match)))
4073 (setq match nil))
4074 (setq matcher (org-make-tags-matcher match)
4075 match (car matcher) matcher (cdr matcher))
4076 (org-prepare-agenda (concat "TAGS " match))
4077 (setq org-agenda-query-string match)
4078 (setq org-agenda-redo-command
4079 (list 'org-tags-view (list 'quote todo-only)
4080 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4081 (setq files (org-agenda-files nil 'ifmode)
4082 rtnall nil)
4083 (while (setq file (pop files))
4084 (catch 'nextfile
4085 (org-check-agenda-file file)
4086 (setq buffer (if (file-exists-p file)
4087 (org-get-agenda-file-buffer file)
4088 (error "No such file %s" file)))
4089 (if (not buffer)
4090 ;; If file does not exist, error message to agenda
4091 (setq rtn (list
4092 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4093 rtnall (append rtnall rtn))
4094 (with-current-buffer buffer
4095 (unless (org-mode-p)
4096 (error "Agenda file %s is not in `org-mode'" file))
4097 (save-excursion
4098 (save-restriction
4099 (if org-agenda-restrict
4100 (narrow-to-region org-agenda-restrict-begin
4101 org-agenda-restrict-end)
4102 (widen))
4103 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4104 (setq rtnall (append rtnall rtn))))))))
4105 (if org-agenda-overriding-header
4106 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4107 nil 'face 'org-agenda-structure) "\n")
4108 (insert "Headlines with TAGS match: ")
4109 (add-text-properties (point-min) (1- (point))
4110 (list 'face 'org-agenda-structure
4111 'short-heading
4112 (concat "Match: " match)))
4113 (setq pos (point))
4114 (insert match "\n")
4115 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4116 (setq pos (point))
4117 (unless org-agenda-multi
4118 (insert "Press `C-u r' to search again with new search string\n"))
4119 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4120 (org-agenda-mark-header-line (point-min))
4121 (when rtnall
4122 (insert (org-finalize-agenda-entries rtnall) "\n"))
4123 (goto-char (point-min))
4124 (or org-agenda-multi (org-fit-agenda-window))
4125 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4126 (org-finalize-agenda)
4127 (setq buffer-read-only t)))
4129 ;;; Agenda Finding stuck projects
4131 (defvar org-agenda-skip-regexp nil
4132 "Regular expression used in skipping subtrees for the agenda.
4133 This is basically a temporary global variable that can be set and then
4134 used by user-defined selections using `org-agenda-skip-function'.")
4136 (defvar org-agenda-overriding-header nil
4137 "When set during agenda, todo and tags searches it replaces the header.
4138 This variable should not be set directly, but custom commands can bind it
4139 in the options section.")
4141 (defun org-agenda-skip-entry-when-regexp-matches ()
4142 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4143 If yes, it returns the end position of this entry, causing agenda commands
4144 to skip the entry but continuing the search in the subtree. This is a
4145 function that can be put into `org-agenda-skip-function' for the duration
4146 of a command."
4147 (let ((end (save-excursion (org-end-of-subtree t)))
4148 skip)
4149 (save-excursion
4150 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4151 (and skip end)))
4153 (defun org-agenda-skip-subtree-when-regexp-matches ()
4154 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4155 If yes, it returns the end position of this tree, causing agenda commands
4156 to skip this subtree. This is a function that can be put into
4157 `org-agenda-skip-function' for the duration of a command."
4158 (let ((end (save-excursion (org-end-of-subtree t)))
4159 skip)
4160 (save-excursion
4161 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4162 (and skip end)))
4164 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4165 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4166 If yes, it returns the end position of the current entry (NOT the tree),
4167 causing agenda commands to skip the entry but continuing the search in
4168 the subtree. This is a function that can be put into
4169 `org-agenda-skip-function' for the duration of a command. An important
4170 use of this function is for the stuck project list."
4171 (let ((end (save-excursion (org-end-of-subtree t)))
4172 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4173 skip)
4174 (save-excursion
4175 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4176 (and skip entry-end)))
4178 (defun org-agenda-skip-entry-if (&rest conditions)
4179 "Skip entry if any of CONDITIONS is true.
4180 See `org-agenda-skip-if' for details."
4181 (org-agenda-skip-if nil conditions))
4183 (defun org-agenda-skip-subtree-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 t conditions))
4188 (defun org-agenda-skip-if (subtree conditions)
4189 "Checks current entity for CONDITIONS.
4190 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4191 the entry, i.e. the text before the next heading is checked.
4193 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4194 from different tests. Valid conditions are:
4196 scheduled Check if there is a scheduled cookie
4197 notscheduled Check if there is no scheduled cookie
4198 deadline Check if there is a deadline
4199 notdeadline Check if there is no deadline
4200 timestamp Check if there is a timestamp (also deadline or scheduled)
4201 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4202 regexp Check if regexp matches
4203 notregexp Check if regexp does not match.
4204 todo Check if TODO keyword matches
4205 nottodo Check if TODO keyword does not match
4207 The regexp is taken from the conditions list, it must come right after
4208 the `regexp' or `notregexp' element.
4210 `todo' and `nottodo' accept as an argument a list of todo
4211 keywords, which may include \"*\" to match any todo keyword.
4213 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4215 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4217 Instead of a list a keyword class may be given
4219 (org-agenda-skip-entry-if 'nottodo 'done)
4221 would skip entries that haven't been marked with any of \"DONE\"
4222 keywords. Possible classes are: `todo', `done', `any'.
4224 If any of these conditions is met, this function returns the end point of
4225 the entity, causing the search to continue from there. This is a function
4226 that can be put into `org-agenda-skip-function' for the duration of a command."
4227 (let (beg end m)
4228 (org-back-to-heading t)
4229 (setq beg (point)
4230 end (if subtree
4231 (progn (org-end-of-subtree t) (point))
4232 (progn (outline-next-heading) (1- (point)))))
4233 (goto-char beg)
4234 (and
4236 (and (memq 'scheduled conditions)
4237 (re-search-forward org-scheduled-time-regexp end t))
4238 (and (memq 'notscheduled conditions)
4239 (not (re-search-forward org-scheduled-time-regexp end t)))
4240 (and (memq 'deadline conditions)
4241 (re-search-forward org-deadline-time-regexp end t))
4242 (and (memq 'notdeadline conditions)
4243 (not (re-search-forward org-deadline-time-regexp end t)))
4244 (and (memq 'timestamp conditions)
4245 (re-search-forward org-ts-regexp end t))
4246 (and (memq 'nottimestamp conditions)
4247 (not (re-search-forward org-ts-regexp end t)))
4248 (and (setq m (memq 'regexp conditions))
4249 (stringp (nth 1 m))
4250 (re-search-forward (nth 1 m) end t))
4251 (and (setq m (memq 'notregexp conditions))
4252 (stringp (nth 1 m))
4253 (not (re-search-forward (nth 1 m) end t)))
4254 (and (or
4255 (setq m (memq 'todo conditions))
4256 (setq m (memq 'nottodo conditions)))
4257 (org-agenda-skip-if-todo m end)))
4258 end)))
4260 (defun org-agenda-skip-if-todo (args end)
4261 "Helper function for `org-agenda-skip-if', do not use it directly.
4262 ARGS is a list with first element either `todo' or `nottodo'.
4263 The remainder is either a list of TODO keywords, or a state symbol
4264 `todo' or `done' or `any'."
4265 (let ((kw (car args))
4266 (arg (cadr args))
4267 todo-wds todo-re)
4268 (setq todo-wds
4269 (org-uniquify
4270 (cond
4271 ((listp arg) ;; list of keywords
4272 (if (member "*" arg)
4273 (mapcar 'substring-no-properties org-todo-keywords-1)
4274 arg))
4275 ((symbolp arg) ;; keyword class name
4276 (cond
4277 ((eq arg 'todo)
4278 (org-delete-all org-done-keywords
4279 (mapcar 'substring-no-properties
4280 org-todo-keywords-1)))
4281 ((eq arg 'done) org-done-keywords)
4282 ((eq arg 'any)
4283 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4284 (setq todo-re
4285 (concat "^\\*+[ \t]+\\<\\("
4286 (mapconcat 'identity todo-wds "\\|")
4287 "\\)\\>"))
4288 (if (eq kw 'todo)
4289 (re-search-forward todo-re end t)
4290 (not (re-search-forward todo-re end t)))))
4292 ;;;###autoload
4293 (defun org-agenda-list-stuck-projects (&rest ignore)
4294 "Create agenda view for projects that are stuck.
4295 Stuck projects are project that have no next actions. For the definitions
4296 of what a project is and how to check if it stuck, customize the variable
4297 `org-stuck-projects'."
4298 (interactive)
4299 (let* ((org-agenda-skip-function
4300 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4301 ;; We could have used org-agenda-skip-if here.
4302 (org-agenda-overriding-header
4303 (or org-agenda-overriding-header "List of stuck projects: "))
4304 (matcher (nth 0 org-stuck-projects))
4305 (todo (nth 1 org-stuck-projects))
4306 (todo-wds (if (member "*" todo)
4307 (progn
4308 (org-prepare-agenda-buffers (org-agenda-files
4309 nil 'ifmode))
4310 (org-delete-all
4311 org-done-keywords-for-agenda
4312 (copy-sequence org-todo-keywords-for-agenda)))
4313 todo))
4314 (todo-re (concat "^\\*+[ \t]+\\("
4315 (mapconcat 'identity todo-wds "\\|")
4316 "\\)\\>"))
4317 (tags (nth 2 org-stuck-projects))
4318 (tags-re (if (member "*" tags)
4319 (org-re (concat org-outline-regexp-bol
4320 ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4321 (if tags
4322 (concat org-outline-regexp-bol
4323 ".*:\\("
4324 (mapconcat 'identity tags "\\|")
4325 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4326 (gen-re (nth 3 org-stuck-projects))
4327 (re-list
4328 (delq nil
4329 (list
4330 (if todo todo-re)
4331 (if tags tags-re)
4332 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4333 gen-re)))))
4334 (setq org-agenda-skip-regexp
4335 (if re-list
4336 (mapconcat 'identity re-list "\\|")
4337 (error "No information how to identify unstuck projects")))
4338 (org-tags-view nil matcher)
4339 (with-current-buffer org-agenda-buffer-name
4340 (setq org-agenda-redo-command
4341 '(org-agenda-list-stuck-projects
4342 (or current-prefix-arg org-last-arg))))))
4344 ;;; Diary integration
4346 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4347 (defvar list-diary-entries-hook)
4348 (defvar diary-time-regexp)
4349 (defun org-get-entries-from-diary (date)
4350 "Get the (Emacs Calendar) diary entries for DATE."
4351 (require 'diary-lib)
4352 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4353 (diary-display-hook '(fancy-diary-display))
4354 (diary-display-function 'fancy-diary-display)
4355 (pop-up-frames nil)
4356 (list-diary-entries-hook
4357 (cons 'org-diary-default-entry list-diary-entries-hook))
4358 (diary-file-name-prefix-function nil) ; turn this feature off
4359 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4360 entries
4361 (org-disable-agenda-to-diary t))
4362 (save-excursion
4363 (save-window-excursion
4364 (funcall (if (fboundp 'diary-list-entries)
4365 'diary-list-entries 'list-diary-entries)
4366 date 1)))
4367 (if (not (get-buffer diary-fancy-buffer))
4368 (setq entries nil)
4369 (with-current-buffer diary-fancy-buffer
4370 (setq buffer-read-only nil)
4371 (if (zerop (buffer-size))
4372 ;; No entries
4373 (setq entries nil)
4374 ;; Omit the date and other unnecessary stuff
4375 (org-agenda-cleanup-fancy-diary)
4376 ;; Add prefix to each line and extend the text properties
4377 (if (zerop (buffer-size))
4378 (setq entries nil)
4379 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4380 (setq entries
4381 (with-temp-buffer
4382 (insert entries) (goto-char (point-min))
4383 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4384 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4385 (replace-match (concat "; " (match-string 1)))))
4386 (buffer-string)))))
4387 (set-buffer-modified-p nil)
4388 (kill-buffer diary-fancy-buffer)))
4389 (when entries
4390 (setq entries (org-split-string entries "\n"))
4391 (setq entries
4392 (mapcar
4393 (lambda (x)
4394 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4395 ;; Extend the text properties to the beginning of the line
4396 (org-add-props x (text-properties-at (1- (length x)) x)
4397 'type "diary" 'date date 'face 'org-agenda-diary))
4398 entries)))))
4400 (defvar org-agenda-cleanup-fancy-diary-hook nil
4401 "Hook run when the fancy diary buffer is cleaned up.")
4403 (defun org-agenda-cleanup-fancy-diary ()
4404 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4405 This gets rid of the date, the underline under the date, and
4406 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4407 date. It also removes lines that contain only whitespace."
4408 (goto-char (point-min))
4409 (if (looking-at ".*?:[ \t]*")
4410 (progn
4411 (replace-match "")
4412 (re-search-forward "\n=+$" nil t)
4413 (replace-match "")
4414 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4415 (re-search-forward "\n=+$" nil t)
4416 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4417 (goto-char (point-min))
4418 (while (re-search-forward "^ +\n" nil t)
4419 (replace-match ""))
4420 (goto-char (point-min))
4421 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4422 (replace-match ""))
4423 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4425 ;; Make sure entries from the diary have the right text properties.
4426 (eval-after-load "diary-lib"
4427 '(if (boundp 'diary-modify-entry-list-string-function)
4428 ;; We can rely on the hook, nothing to do
4430 ;; Hook not available, must use advice to make this work
4431 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4432 "Make the position visible."
4433 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4434 (stringp string)
4435 buffer-file-name)
4436 (setq string (org-modify-diary-entry-string string))))))
4438 (defun org-modify-diary-entry-string (string)
4439 "Add text properties to string, allowing org-mode to act on it."
4440 (org-add-props string nil
4441 'mouse-face 'highlight
4442 'help-echo (if buffer-file-name
4443 (format "mouse-2 or RET jump to diary file %s"
4444 (abbreviate-file-name buffer-file-name))
4446 'org-agenda-diary-link t
4447 'org-marker (org-agenda-new-marker (point-at-bol))))
4449 (defun org-diary-default-entry ()
4450 "Add a dummy entry to the diary.
4451 Needed to avoid empty dates which mess up holiday display."
4452 ;; Catch the error if dealing with the new add-to-diary-alist
4453 (when org-disable-agenda-to-diary
4454 (condition-case nil
4455 (org-add-to-diary-list original-date "Org-mode dummy" "")
4456 (error
4457 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4459 (defun org-add-to-diary-list (&rest args)
4460 (if (fboundp 'diary-add-to-list)
4461 (apply 'diary-add-to-list args)
4462 (apply 'add-to-diary-list args)))
4464 (defvar org-diary-last-run-time nil)
4466 ;;;###autoload
4467 (defun org-diary (&rest args)
4468 "Return diary information from org-files.
4469 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4470 It accesses org files and extracts information from those files to be
4471 listed in the diary. The function accepts arguments specifying what
4472 items should be listed. For a list of arguments allowed here, see the
4473 variable `org-agenda-entry-types'.
4475 The call in the diary file should look like this:
4477 &%%(org-diary) ~/path/to/some/orgfile.org
4479 Use a separate line for each org file to check. Or, if you omit the file name,
4480 all files listed in `org-agenda-files' will be checked automatically:
4482 &%%(org-diary)
4484 If you don't give any arguments (as in the example above), the default
4485 arguments (:deadline :scheduled :timestamp :sexp) are used.
4486 So the example above may also be written as
4488 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4490 The function expects the lisp variables `entry' and `date' to be provided
4491 by the caller, because this is how the calendar works. Don't use this
4492 function from a program - use `org-agenda-get-day-entries' instead."
4493 (when (> (- (org-float-time)
4494 org-agenda-last-marker-time)
4496 (org-agenda-reset-markers))
4497 (org-compile-prefix-format 'agenda)
4498 (org-set-sorting-strategy 'agenda)
4499 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4500 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4501 (list entry)
4502 (org-agenda-files t)))
4503 (time (org-float-time))
4504 file rtn results)
4505 (when (or (not org-diary-last-run-time)
4506 (> (- time
4507 org-diary-last-run-time)
4509 (org-prepare-agenda-buffers files))
4510 (setq org-diary-last-run-time time)
4511 ;; If this is called during org-agenda, don't return any entries to
4512 ;; the calendar. Org Agenda will list these entries itself.
4513 (if org-disable-agenda-to-diary (setq files nil))
4514 (while (setq file (pop files))
4515 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4516 (setq results (append results rtn)))
4517 (if results
4518 (concat (org-finalize-agenda-entries results) "\n"))))
4520 ;;; Agenda entry finders
4522 (defun org-agenda-get-day-entries (file date &rest args)
4523 "Does the work for `org-diary' and `org-agenda'.
4524 FILE is the path to a file to be checked for entries. DATE is date like
4525 the one returned by `calendar-current-date'. ARGS are symbols indicating
4526 which kind of entries should be extracted. For details about these, see
4527 the documentation of `org-diary'."
4528 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4529 (let* ((org-startup-folded nil)
4530 (org-startup-align-all-tables nil)
4531 (buffer (if (file-exists-p file)
4532 (org-get-agenda-file-buffer file)
4533 (error "No such file %s" file)))
4534 arg results rtn deadline-results)
4535 (if (not buffer)
4536 ;; If file does not exist, make sure an error message ends up in diary
4537 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4538 (with-current-buffer buffer
4539 (unless (org-mode-p)
4540 (error "Agenda file %s is not in `org-mode'" file))
4541 (let ((case-fold-search nil))
4542 (save-excursion
4543 (save-restriction
4544 (if org-agenda-restrict
4545 (narrow-to-region org-agenda-restrict-begin
4546 org-agenda-restrict-end)
4547 (widen))
4548 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4549 (while (setq arg (pop args))
4550 (cond
4551 ((and (eq arg :todo)
4552 (equal date (calendar-gregorian-from-absolute
4553 (org-today))))
4554 (setq rtn (org-agenda-get-todos))
4555 (setq results (append results rtn)))
4556 ((eq arg :timestamp)
4557 (setq rtn (org-agenda-get-blocks))
4558 (setq results (append results rtn))
4559 (setq rtn (org-agenda-get-timestamps))
4560 (setq results (append results rtn)))
4561 ((eq arg :sexp)
4562 (setq rtn (org-agenda-get-sexps))
4563 (setq results (append results rtn)))
4564 ((eq arg :scheduled)
4565 (setq rtn (org-agenda-get-scheduled deadline-results))
4566 (setq results (append results rtn)))
4567 ((eq arg :closed)
4568 (setq rtn (org-agenda-get-progress))
4569 (setq results (append results rtn)))
4570 ((eq arg :deadline)
4571 (setq rtn (org-agenda-get-deadlines))
4572 (setq deadline-results (copy-sequence rtn))
4573 (setq results (append results rtn))))))))
4574 results))))
4576 (defun org-agenda-get-todos ()
4577 "Return the TODO information for agenda display."
4578 (let* ((props (list 'face nil
4579 'done-face 'org-agenda-done
4580 'org-not-done-regexp org-not-done-regexp
4581 'org-todo-regexp org-todo-regexp
4582 'org-complex-heading-regexp org-complex-heading-regexp
4583 'mouse-face 'highlight
4584 'help-echo
4585 (format "mouse-2 or RET jump to org file %s"
4586 (abbreviate-file-name buffer-file-name))))
4587 (regexp (concat "^\\*+[ \t]+\\("
4588 (if org-select-this-todo-keyword
4589 (if (equal org-select-this-todo-keyword "*")
4590 org-todo-regexp
4591 (concat "\\<\\("
4592 (mapconcat 'identity
4593 (org-split-string
4594 org-select-this-todo-keyword "|") "\\|")
4595 "\\)\\>"))
4596 org-not-done-regexp)
4597 "[^\n\r]*\\)"))
4598 marker priority category category-pos tags todo-state ee txt beg end)
4599 (goto-char (point-min))
4600 (while (re-search-forward regexp nil t)
4601 (catch :skip
4602 (save-match-data
4603 (beginning-of-line)
4604 (org-agenda-skip)
4605 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4606 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4607 (goto-char (1+ beg))
4608 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4609 (throw :skip nil)))
4610 (goto-char (match-beginning 1))
4611 (setq marker (org-agenda-new-marker (match-beginning 0))
4612 category (org-get-category)
4613 category-pos (get-text-property (point) 'org-category-position)
4614 txt (match-string 1)
4615 tags (org-get-tags-at (point))
4616 txt (org-format-agenda-item "" txt category tags)
4617 priority (1+ (org-get-priority txt))
4618 todo-state (org-get-todo-state))
4619 (org-add-props txt props
4620 'org-marker marker 'org-hd-marker marker
4621 'priority priority 'org-category category
4622 'org-category-position category-pos
4623 'type "todo" 'todo-state todo-state)
4624 (push txt ee)
4625 (if org-agenda-todo-list-sublevels
4626 (goto-char (match-end 1))
4627 (org-end-of-subtree 'invisible))))
4628 (nreverse ee)))
4630 (defun org-agenda-todo-custom-ignore-p (time n)
4631 "Check whether timestamp is farther away then n number of days.
4632 This function is invoked if `org-agenda-todo-ignore-deadlines',
4633 `org-agenda-todo-ignore-scheduled' or
4634 `org-agenda-todo-ignore-timestamp' is set to an integer."
4635 (let ((days (org-days-to-time time)))
4636 (if (>= n 0)
4637 (>= days n)
4638 (<= days n))))
4640 ;;;###autoload
4641 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4642 (&optional end)
4643 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4644 (when (or org-agenda-todo-ignore-with-date
4645 org-agenda-todo-ignore-scheduled
4646 org-agenda-todo-ignore-deadlines
4647 org-agenda-todo-ignore-timestamp)
4648 (setq end (or end (save-excursion (outline-next-heading) (point))))
4649 (save-excursion
4650 (or (and org-agenda-todo-ignore-with-date
4651 (re-search-forward org-ts-regexp end t))
4652 (and org-agenda-todo-ignore-scheduled
4653 (re-search-forward org-scheduled-time-regexp end t)
4654 (cond
4655 ((eq org-agenda-todo-ignore-scheduled 'future)
4656 (> (org-days-to-time (match-string 1)) 0))
4657 ((eq org-agenda-todo-ignore-scheduled 'past)
4658 (<= (org-days-to-time (match-string 1)) 0))
4659 ((numberp org-agenda-todo-ignore-scheduled)
4660 (org-agenda-todo-custom-ignore-p
4661 (match-string 1) org-agenda-todo-ignore-scheduled))
4662 (t)))
4663 (and org-agenda-todo-ignore-deadlines
4664 (re-search-forward org-deadline-time-regexp end t)
4665 (cond
4666 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4667 ((eq org-agenda-todo-ignore-deadlines 'far)
4668 (not (org-deadline-close (match-string 1))))
4669 ((eq org-agenda-todo-ignore-deadlines 'future)
4670 (> (org-days-to-time (match-string 1)) 0))
4671 ((eq org-agenda-todo-ignore-deadlines 'past)
4672 (<= (org-days-to-time (match-string 1)) 0))
4673 ((numberp org-agenda-todo-ignore-deadlines)
4674 (org-agenda-todo-custom-ignore-p
4675 (match-string 1) org-agenda-todo-ignore-deadlines))
4676 (t (org-deadline-close (match-string 1)))))
4677 (and org-agenda-todo-ignore-timestamp
4678 (let ((buffer (current-buffer))
4679 (regexp
4680 (concat
4681 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4682 (start (point)))
4683 ;; Copy current buffer into a temporary one
4684 (with-temp-buffer
4685 (insert-buffer-substring buffer start end)
4686 (goto-char (point-min))
4687 ;; Delete SCHEDULED and DEADLINE items
4688 (while (re-search-forward regexp end t)
4689 (delete-region (match-beginning 0) (match-end 0)))
4690 (goto-char (point-min))
4691 ;; No search for timestamp left
4692 (when (re-search-forward org-ts-regexp nil t)
4693 (cond
4694 ((eq org-agenda-todo-ignore-timestamp 'future)
4695 (> (org-days-to-time (match-string 1)) 0))
4696 ((eq org-agenda-todo-ignore-timestamp 'past)
4697 (<= (org-days-to-time (match-string 1)) 0))
4698 ((numberp org-agenda-todo-ignore-timestamp)
4699 (org-agenda-todo-custom-ignore-p
4700 (match-string 1) org-agenda-todo-ignore-timestamp))
4701 (t))))))))))
4703 (defconst org-agenda-no-heading-message
4704 "No heading for this item in buffer or region.")
4706 (defun org-agenda-get-timestamps ()
4707 "Return the date stamp information for agenda display."
4708 (let* ((props (list 'face nil
4709 'org-not-done-regexp org-not-done-regexp
4710 'org-todo-regexp org-todo-regexp
4711 'org-complex-heading-regexp org-complex-heading-regexp
4712 'mouse-face 'highlight
4713 'help-echo
4714 (format "mouse-2 or RET jump to org file %s"
4715 (abbreviate-file-name buffer-file-name))))
4716 (d1 (calendar-absolute-from-gregorian date))
4717 (remove-re
4718 (concat
4719 (regexp-quote
4720 (format-time-string
4721 "<%Y-%m-%d"
4722 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4723 ".*?>"))
4724 (regexp
4725 (concat
4726 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4727 (regexp-quote
4728 (substring
4729 (format-time-string
4730 (car org-time-stamp-formats)
4731 (apply 'encode-time ; DATE bound by calendar
4732 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4733 1 11))
4734 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4735 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4736 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4737 donep tmp priority category category-pos ee txt timestr tags
4738 b0 b3 e3 head todo-state end-of-match show-all)
4739 (goto-char (point-min))
4740 (while (setq end-of-match (re-search-forward regexp nil t))
4741 (setq b0 (match-beginning 0)
4742 b3 (match-beginning 3) e3 (match-end 3)
4743 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4744 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4745 (member todo-state
4746 org-agenda-repeating-timestamp-show-all)))
4747 (catch :skip
4748 (and (org-at-date-range-p) (throw :skip nil))
4749 (org-agenda-skip)
4750 (if (and (match-end 1)
4751 (not (= d1 (org-time-string-to-absolute
4752 (match-string 1) d1 nil show-all
4753 (current-buffer) b0))))
4754 (throw :skip nil))
4755 (if (and e3
4756 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4757 (throw :skip nil))
4758 (setq tmp (buffer-substring (max (point-min)
4759 (- b0 org-ds-keyword-length))
4761 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4762 inactivep (= (char-after b0) ?\[)
4763 deadlinep (string-match org-deadline-regexp tmp)
4764 scheduledp (string-match org-scheduled-regexp tmp)
4765 closedp (and org-agenda-include-inactive-timestamps
4766 (string-match org-closed-string tmp))
4767 clockp (and org-agenda-include-inactive-timestamps
4768 (or (string-match org-clock-string tmp)
4769 (string-match "]-+\\'" tmp)))
4770 donep (member todo-state org-done-keywords))
4771 (if (or scheduledp deadlinep closedp clockp
4772 (and donep org-agenda-skip-timestamp-if-done))
4773 (throw :skip t))
4774 (if (string-match ">" timestr)
4775 ;; substring should only run to end of time stamp
4776 (setq timestr (substring timestr 0 (match-end 0))))
4777 (setq marker (org-agenda-new-marker b0)
4778 category (org-get-category b0)
4779 category-pos (get-text-property b0 'org-category-position))
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 'org-category-position category-pos
4798 'todo-state todo-state
4799 'type "timestamp")
4800 (push txt ee))
4801 (if org-agenda-skip-additional-timestamps-same-entry
4802 (outline-next-heading)
4803 (goto-char end-of-match))))
4804 (nreverse ee)))
4806 (defun org-agenda-get-sexps ()
4807 "Return the sexp information for agenda display."
4808 (require 'diary-lib)
4809 (let* ((props (list 'mouse-face 'highlight
4810 'help-echo
4811 (format "mouse-2 or RET jump to org file %s"
4812 (abbreviate-file-name buffer-file-name))))
4813 (regexp "^&?%%(")
4814 marker category category-pos ee txt tags entry
4815 result beg b sexp sexp-entry todo-state)
4816 (goto-char (point-min))
4817 (while (re-search-forward regexp nil t)
4818 (catch :skip
4819 (org-agenda-skip)
4820 (setq beg (match-beginning 0))
4821 (goto-char (1- (match-end 0)))
4822 (setq b (point))
4823 (forward-sexp 1)
4824 (setq sexp (buffer-substring b (point)))
4825 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4826 (org-trim (match-string 1))
4827 ""))
4828 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4829 (when result
4830 (setq marker (org-agenda-new-marker beg)
4831 category (org-get-category beg)
4832 category-pos (get-text-property beg 'org-category-position)
4833 todo-state (org-get-todo-state))
4835 (dolist (r (if (stringp result)
4836 (list result)
4837 result)) ;; we expect a list here
4838 (if (string-match "\\S-" r)
4839 (setq txt r)
4840 (setq txt "SEXP entry returned empty string"))
4842 (setq txt (org-format-agenda-item
4843 "" txt category tags 'time))
4844 (org-add-props txt props 'org-marker marker)
4845 (org-add-props txt nil
4846 'org-category category 'date date 'todo-state todo-state
4847 'org-category-position category-pos
4848 'type "sexp")
4849 (push txt ee)))))
4850 (nreverse ee)))
4852 ;; Calendar sanity: define some functions that are independent of
4853 ;; `calendar-date-style'.
4854 ;; Normally I would like to use ISO format when calling the diary functions,
4855 ;; but to make sure we still have Emacs 22 compatibility we bind
4856 ;; also `european-calendar-style' and use european format
4857 (defun org-anniversary (year month day &optional mark)
4858 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4859 (org-no-warnings
4860 (let ((calendar-date-style 'european) (european-calendar-style t))
4861 (diary-anniversary day month year mark))))
4862 (defun org-cyclic (N year month day &optional mark)
4863 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4864 (org-no-warnings
4865 (let ((calendar-date-style 'european) (european-calendar-style t))
4866 (diary-cyclic N day month year mark))))
4867 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4868 "Like `diary-block', but with fixed (ISO) order of arguments."
4869 (org-no-warnings
4870 (let ((calendar-date-style 'european) (european-calendar-style t))
4871 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4872 (defun org-date (year month day &optional mark)
4873 "Like `diary-date', but with fixed (ISO) order of arguments."
4874 (org-no-warnings
4875 (let ((calendar-date-style 'european) (european-calendar-style t))
4876 (diary-date day month year mark))))
4877 (defalias 'org-float 'diary-float)
4879 ;; Define the` org-class' function
4880 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4881 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4882 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4883 is any number of ISO weeks in the block period for which the item should
4884 be skipped."
4885 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4886 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4887 (d (calendar-absolute-from-gregorian date)))
4888 (and
4889 (<= date1 d)
4890 (<= d date2)
4891 (= (calendar-day-of-week date) dayname)
4892 (or (not skip-weeks)
4893 (progn
4894 (require 'cal-iso)
4895 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4896 entry)))
4898 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4899 "Like `org-class', but honor `calendar-date-style'.
4900 The order of the first 2 times 3 arguments depends on the variable
4901 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4902 So for American calendars, give this as MONTH DAY YEAR, for European as
4903 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4904 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4905 is any number of ISO weeks in the block period for which the item should
4906 be skipped.
4908 This function is here only for backward compatibility and it is deprecated,
4909 please use `org-class' instead."
4910 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4911 (date2 (org-order-calendar-date-args m2 d2 y2)))
4912 (org-class
4913 (nth 2 date1) (car date1) (nth 1 date1)
4914 (nth 2 date2) (car date2) (nth 1 date2)
4915 dayname skip-weeks)))
4917 (defalias 'org-get-closed 'org-agenda-get-progress)
4918 (defun org-agenda-get-progress ()
4919 "Return the logged TODO entries for agenda display."
4920 (let* ((props (list 'mouse-face 'highlight
4921 'org-not-done-regexp org-not-done-regexp
4922 'org-todo-regexp org-todo-regexp
4923 'org-complex-heading-regexp org-complex-heading-regexp
4924 'help-echo
4925 (format "mouse-2 or RET jump to org file %s"
4926 (abbreviate-file-name buffer-file-name))))
4927 (items (if (consp org-agenda-show-log)
4928 org-agenda-show-log
4929 (if (eq org-agenda-show-log 'clockcheck)
4930 '(clock)
4931 org-agenda-log-mode-items)))
4932 (parts
4933 (delq nil
4934 (list
4935 (if (memq 'closed items) (concat "\\<" org-closed-string))
4936 (if (memq 'clock items) (concat "\\<" org-clock-string))
4937 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4938 (parts-re (if parts (mapconcat 'identity parts "\\|")
4939 (error "`org-agenda-log-mode-items' is empty")))
4940 (regexp (concat
4941 "\\(" parts-re "\\)"
4942 " *\\["
4943 (regexp-quote
4944 (substring
4945 (format-time-string
4946 (car org-time-stamp-formats)
4947 (apply 'encode-time ; DATE bound by calendar
4948 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4949 1 11))))
4950 (org-agenda-search-headline-for-time nil)
4951 marker hdmarker priority category category-pos tags closedp
4952 statep clockp state ee txt extra timestr rest clocked)
4953 (goto-char (point-min))
4954 (while (re-search-forward regexp nil t)
4955 (catch :skip
4956 (org-agenda-skip)
4957 (setq marker (org-agenda-new-marker (match-beginning 0))
4958 closedp (equal (match-string 1) org-closed-string)
4959 statep (equal (string-to-char (match-string 1)) ?-)
4960 clockp (not (or closedp statep))
4961 state (and statep (match-string 2))
4962 category (org-get-category (match-beginning 0))
4963 category-pos (get-text-property (match-beginning 0) 'org-category-position)
4964 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
4965 (when (string-match "\\]" timestr)
4966 ;; substring should only run to end of time stamp
4967 (setq rest (substring timestr (match-end 0))
4968 timestr (substring timestr 0 (match-end 0)))
4969 (if (and (not closedp) (not statep)
4970 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
4971 rest))
4972 (progn (setq timestr (concat (substring timestr 0 -1)
4973 "-" (match-string 1 rest) "]"))
4974 (setq clocked (match-string 2 rest)))
4975 (setq clocked "-")))
4976 (save-excursion
4977 (setq extra
4978 (cond
4979 ((not org-agenda-log-mode-add-notes) nil)
4980 (statep
4981 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4982 (match-string 1)))
4983 (clockp
4984 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4985 (match-string 1)))))
4986 (if (not (re-search-backward org-outline-regexp-bol nil t))
4987 (setq txt org-agenda-no-heading-message)
4988 (goto-char (match-beginning 0))
4989 (setq hdmarker (org-agenda-new-marker)
4990 tags (org-get-tags-at))
4991 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4992 (setq txt (match-string 1))
4993 (when extra
4994 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4995 (setq txt (concat (substring txt 0 (match-beginning 1))
4996 " - " extra " " (match-string 2 txt)))
4997 (setq txt (concat txt " - " extra))))
4998 (setq txt (org-format-agenda-item
4999 (cond
5000 (closedp "Closed: ")
5001 (statep (concat "State: (" state ")"))
5002 (t (concat "Clocked: (" clocked ")")))
5003 txt category tags timestr)))
5004 (setq priority 100000)
5005 (org-add-props txt props
5006 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5007 'priority priority 'org-category category
5008 'org-category-position category-pos
5009 'type "closed" 'date date
5010 'undone-face 'org-warning 'done-face 'org-agenda-done)
5011 (push txt ee))
5012 (goto-char (point-at-eol))))
5013 (nreverse ee)))
5015 (defun org-agenda-show-clocking-issues ()
5016 "Add overlays, showing issues with clocking.
5017 See also the user option `org-agenda-clock-consistency-checks'."
5018 (interactive)
5019 (let* ((pl org-agenda-clock-consistency-checks)
5020 (re (concat "^[ \t]*"
5021 org-clock-string
5022 "[ \t]+"
5023 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5024 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5025 (tlstart 0.)
5026 (tlend 0.)
5027 (maxtime (org-hh:mm-string-to-minutes
5028 (or (plist-get pl :max-duration) "24:00")))
5029 (mintime (org-hh:mm-string-to-minutes
5030 (or (plist-get pl :min-duration) 0)))
5031 (maxgap (org-hh:mm-string-to-minutes
5032 ;; default 30:00 means never complain
5033 (or (plist-get pl :max-gap) "30:00")))
5034 (gapok (mapcar 'org-hh:mm-string-to-minutes
5035 (plist-get pl :gap-ok-around)))
5036 (def-face (or (plist-get pl :default-face)
5037 '((:background "DarkRed") (:foreground "white"))))
5038 issue face m te ts dt ov)
5039 (goto-char (point-min))
5040 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5041 (setq issue nil face def-face)
5042 (catch 'next
5043 (setq m (org-get-at-bol 'org-marker)
5044 te nil ts nil)
5045 (unless (and m (markerp m))
5046 (setq issue "No valid clock line") (throw 'next t))
5047 (org-with-point-at m
5048 (save-excursion
5049 (goto-char (point-at-bol))
5050 (unless (looking-at re)
5051 (error "No valid Clock line")
5052 (throw 'next t))
5053 (unless (match-end 3)
5054 (setq issue "No end time"
5055 face (or (plist-get pl :no-end-time-face) face))
5056 (throw 'next t))
5057 (setq ts (match-string 1)
5058 te (match-string 3)
5059 ts (org-float-time
5060 (apply 'encode-time (org-parse-time-string ts)))
5061 te (org-float-time
5062 (apply 'encode-time (org-parse-time-string te)))
5063 dt (- te ts))))
5064 (cond
5065 ((> dt (* 60 maxtime))
5066 ;; a very long clocking chunk
5067 (setq issue (format "Clocking interval is very long: %s"
5068 (org-minutes-to-hh:mm-string
5069 (floor (/ (float dt) 60.))))
5070 face (or (plist-get pl :long-face) face)))
5071 ((< dt (* 60 mintime))
5072 ;; a very short clocking chunk
5073 (setq issue (format "Clocking interval is very short: %s"
5074 (org-minutes-to-hh:mm-string
5075 (floor (/ (float dt) 60.))))
5076 face (or (plist-get pl :short-face) face)))
5077 ((and (> tlend 0) (< ts tlend))
5078 ;; Two clock entries are overlapping
5079 (setq issue (format "Clocking overlap: %d minutes"
5080 (/ (- tlend ts) 60))
5081 face (or (plist-get pl :overlap-face) face)))
5082 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5083 ;; There is a gap, lets see if we need to report it
5084 (unless (org-agenda-check-clock-gap tlend ts gapok)
5085 (setq issue (format "Clocking gap: %d minutes"
5086 (/ (- ts tlend) 60))
5087 face (or (plist-get pl :gap-face) face))))
5088 (t nil)))
5089 (setq tlend (or te tlend) tlstart (or ts tlstart))
5090 (when issue
5091 ;; OK, there was some issue, add an overlay to show the issue
5092 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5093 (overlay-put ov 'before-string
5094 (concat
5095 (org-add-props
5096 (format "%-43s" (concat " " issue))
5098 'face face)
5099 "\n"))
5100 (overlay-put ov 'evaporate t)))))
5102 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5103 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5104 (catch 'exit
5105 (unless ok-list
5106 ;; there are no OK times for gaps...
5107 (throw 'exit nil))
5108 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5109 ;; This is more than 24 hours, so it is OK.
5110 ;; because we have at least one OK time, that must be in the
5111 ;; 24 hour interval.
5112 (throw 'exit t))
5113 ;; We have a shorter gap.
5114 ;; Now we have to get the minute of the day when these times are
5115 (let* ((t1dec (decode-time (seconds-to-time t1)))
5116 (t2dec (decode-time (seconds-to-time t2)))
5117 ;; compute the minute on the day
5118 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5119 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5120 (when (< min2 min1)
5121 ;; if min2 is smaller than min1, this means it is on the next day.
5122 ;; Wrap it to after midnight.
5123 (setq min2 (+ min2 1440)))
5124 ;; Now check if any of the OK times is in the gap
5125 (mapc (lambda (x)
5126 ;; Wrap the time to after midnight if necessary
5127 (if (< x min1) (setq x (+ x 1440)))
5128 ;; Check if in interval
5129 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5130 ok-list)
5131 ;; Nope, this gap is not OK
5132 nil)))
5134 (defun org-agenda-get-deadlines ()
5135 "Return the deadline information for agenda display."
5136 (let* ((props (list 'mouse-face 'highlight
5137 'org-not-done-regexp org-not-done-regexp
5138 'org-todo-regexp org-todo-regexp
5139 'org-complex-heading-regexp org-complex-heading-regexp
5140 'help-echo
5141 (format "mouse-2 or RET jump to org file %s"
5142 (abbreviate-file-name buffer-file-name))))
5143 (regexp org-deadline-time-regexp)
5144 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5145 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5146 d2 diff dfrac wdays pos pos1 category category-pos
5147 tags suppress-prewarning ee txt head face s todo-state
5148 show-all upcomingp donep timestr)
5149 (goto-char (point-min))
5150 (while (re-search-forward regexp nil t)
5151 (setq suppress-prewarning nil)
5152 (catch :skip
5153 (org-agenda-skip)
5154 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5155 (save-match-data
5156 (string-match org-scheduled-time-regexp
5157 (buffer-substring (point-at-bol)
5158 (point-at-eol)))))
5159 (setq suppress-prewarning
5160 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5161 org-agenda-skip-deadline-prewarning-if-scheduled
5162 0)))
5163 (setq s (match-string 1)
5164 txt nil
5165 pos (1- (match-beginning 1))
5166 todo-state (save-match-data (org-get-todo-state))
5167 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5168 (member todo-state
5169 org-agenda-repeating-timestamp-show-all))
5170 d2 (org-time-string-to-absolute
5171 (match-string 1) d1 'past show-all
5172 (current-buffer) pos)
5173 diff (- d2 d1)
5174 wdays (if suppress-prewarning
5175 (let ((org-deadline-warning-days suppress-prewarning))
5176 (org-get-wdays s))
5177 (org-get-wdays s))
5178 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5179 upcomingp (and todayp (> diff 0)))
5180 ;; When to show a deadline in the calendar:
5181 ;; If the expiration is within wdays warning time.
5182 ;; Past-due deadlines are only shown on the current date
5183 (if (and (or (and (<= diff wdays)
5184 (and todayp (not org-agenda-only-exact-dates)))
5185 (= diff 0)))
5186 (save-excursion
5187 ;; (setq todo-state (org-get-todo-state))
5188 (setq donep (member todo-state org-done-keywords))
5189 (if (and donep
5190 (or org-agenda-skip-deadline-if-done
5191 (not (= diff 0))))
5192 (setq txt nil)
5193 (setq category (org-get-category)
5194 category-pos (get-text-property (point) 'org-category-position))
5195 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5196 (setq txt org-agenda-no-heading-message)
5197 (goto-char (match-end 0))
5198 (setq pos1 (match-beginning 0))
5199 (setq tags (org-get-tags-at pos1))
5200 (setq head (buffer-substring-no-properties
5201 (point)
5202 (progn (skip-chars-forward "^\r\n")
5203 (point))))
5204 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5205 (setq timestr
5206 (concat (substring s (match-beginning 1)) " "))
5207 (setq timestr 'time))
5208 (setq txt (org-format-agenda-item
5209 (if (= diff 0)
5210 (car org-agenda-deadline-leaders)
5211 (if (functionp
5212 (nth 1 org-agenda-deadline-leaders))
5213 (funcall
5214 (nth 1 org-agenda-deadline-leaders)
5215 diff date)
5216 (format (nth 1 org-agenda-deadline-leaders)
5217 diff)))
5218 head category tags
5219 (if (not (= diff 0)) nil timestr)))))
5220 (when txt
5221 (setq face (org-agenda-deadline-face dfrac))
5222 (org-add-props txt props
5223 'org-marker (org-agenda-new-marker pos)
5224 'org-hd-marker (org-agenda-new-marker pos1)
5225 'priority (+ (- diff)
5226 (org-get-priority txt))
5227 'org-category category
5228 'org-category-position category-pos
5229 'todo-state todo-state
5230 'type (if upcomingp "upcoming-deadline" "deadline")
5231 'date (if upcomingp date d2)
5232 'face (if donep 'org-agenda-done face)
5233 'undone-face face 'done-face 'org-agenda-done)
5234 (push txt ee))))))
5235 (nreverse ee)))
5237 (defun org-agenda-deadline-face (fraction)
5238 "Return the face to displaying a deadline item.
5239 FRACTION is what fraction of the head-warning time has passed."
5240 (let ((faces org-agenda-deadline-faces) f)
5241 (catch 'exit
5242 (while (setq f (pop faces))
5243 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5245 (defun org-agenda-get-scheduled (&optional deadline-results)
5246 "Return the scheduled information for agenda display."
5247 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5248 'org-todo-regexp org-todo-regexp
5249 'org-complex-heading-regexp org-complex-heading-regexp
5250 'done-face 'org-agenda-done
5251 'mouse-face 'highlight
5252 'help-echo
5253 (format "mouse-2 or RET jump to org file %s"
5254 (abbreviate-file-name buffer-file-name))))
5255 (regexp org-scheduled-time-regexp)
5256 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5257 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5259 (deadline-position-alist
5260 (mapcar (lambda (a) (and (setq mm (get-text-property
5261 0 'org-hd-marker a))
5262 (cons (marker-position mm) a)))
5263 deadline-results))
5264 d2 diff pos pos1 category category-pos tags donep
5265 ee txt head pastschedp todo-state face timestr s habitp show-all)
5266 (goto-char (point-min))
5267 (while (re-search-forward regexp nil t)
5268 (catch :skip
5269 (org-agenda-skip)
5270 (setq s (match-string 1)
5271 txt nil
5272 pos (1- (match-beginning 1))
5273 todo-state (save-match-data (org-get-todo-state))
5274 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5275 (member todo-state
5276 org-agenda-repeating-timestamp-show-all))
5277 d2 (org-time-string-to-absolute
5278 (match-string 1) d1 'past show-all
5279 (current-buffer) pos)
5280 diff (- d2 d1))
5281 (setq pastschedp (and todayp (< diff 0)))
5282 ;; When to show a scheduled item in the calendar:
5283 ;; If it is on or past the date.
5284 (when (or (and (< diff 0)
5285 (< (abs diff) org-scheduled-past-days)
5286 (and todayp (not org-agenda-only-exact-dates)))
5287 (= diff 0))
5288 (save-excursion
5289 (setq donep (member todo-state org-done-keywords))
5290 (if (and donep
5291 (or org-agenda-skip-scheduled-if-done
5292 (not (= diff 0))
5293 (and (functionp 'org-is-habit-p)
5294 (org-is-habit-p))))
5295 (setq txt nil)
5296 (setq habitp (and (functionp 'org-is-habit-p)
5297 (org-is-habit-p)))
5298 (setq category (org-get-category)
5299 category-pos (get-text-property (point) 'org-category-position))
5300 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5301 (setq txt org-agenda-no-heading-message)
5302 (goto-char (match-end 0))
5303 (setq pos1 (match-beginning 0))
5304 (if habitp
5305 (if (or (not org-habit-show-habits)
5306 (and (not todayp)
5307 org-habit-show-habits-only-for-today))
5308 (throw :skip nil))
5309 (if (and
5310 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5311 (and org-agenda-skip-scheduled-if-deadline-is-shown
5312 pastschedp))
5313 (setq mm (assoc pos1 deadline-position-alist)))
5314 (throw :skip nil)))
5315 (setq tags (org-get-tags-at))
5316 (setq head (buffer-substring-no-properties
5317 (point)
5318 (progn (skip-chars-forward "^\r\n") (point))))
5319 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5320 (setq timestr
5321 (concat (substring s (match-beginning 1)) " "))
5322 (setq timestr 'time))
5323 (setq txt (org-format-agenda-item
5324 (if (= diff 0)
5325 (car org-agenda-scheduled-leaders)
5326 (format (nth 1 org-agenda-scheduled-leaders)
5327 (- 1 diff)))
5328 head category tags
5329 (if (not (= diff 0)) nil timestr)
5330 nil habitp))))
5331 (when txt
5332 (setq face
5333 (cond
5334 ((and (not habitp) pastschedp)
5335 'org-scheduled-previously)
5336 (todayp 'org-scheduled-today)
5337 (t 'org-scheduled))
5338 habitp (and habitp (org-habit-parse-todo)))
5339 (org-add-props txt props
5340 'undone-face face
5341 'face (if donep 'org-agenda-done face)
5342 'org-marker (org-agenda-new-marker pos)
5343 'org-hd-marker (org-agenda-new-marker pos1)
5344 'type (if pastschedp "past-scheduled" "scheduled")
5345 'date (if pastschedp d2 date)
5346 'priority (if habitp
5347 (org-habit-get-priority habitp)
5348 (+ 94 (- 5 diff) (org-get-priority txt)))
5349 'org-category category
5350 'org-category-position category-pos
5351 'org-habit-p habitp
5352 'todo-state todo-state)
5353 (push txt ee))))))
5354 (nreverse ee)))
5356 (defun org-agenda-get-blocks ()
5357 "Return the date-range information for agenda display."
5358 (let* ((props (list 'face nil
5359 'org-not-done-regexp org-not-done-regexp
5360 'org-todo-regexp org-todo-regexp
5361 'org-complex-heading-regexp org-complex-heading-regexp
5362 'mouse-face 'highlight
5363 'help-echo
5364 (format "mouse-2 or RET jump to org file %s"
5365 (abbreviate-file-name buffer-file-name))))
5366 (regexp org-tr-regexp)
5367 (d0 (calendar-absolute-from-gregorian date))
5368 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5369 head donep)
5370 (goto-char (point-min))
5371 (while (re-search-forward regexp nil t)
5372 (catch :skip
5373 (org-agenda-skip)
5374 (setq pos (point))
5375 (let ((start-time (match-string 1))
5376 (end-time (match-string 2)))
5377 (setq s1 (match-string 1)
5378 s2 (match-string 2)
5379 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5380 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5381 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5382 ;; Only allow days between the limits, because the normal
5383 ;; date stamps will catch the limits.
5384 (save-excursion
5385 (setq todo-state (org-get-todo-state))
5386 (setq donep (member todo-state org-done-keywords))
5387 (if (and donep org-agenda-skip-timestamp-if-done)
5388 (throw :skip t))
5389 (setq marker (org-agenda-new-marker (point)))
5390 (setq category (org-get-category)
5391 category-pos (get-text-property (point) 'org-category-position))
5392 (if (not (re-search-backward org-outline-regexp-bol nil t))
5393 (setq txt org-agenda-no-heading-message)
5394 (goto-char (match-beginning 0))
5395 (setq hdmarker (org-agenda-new-marker (point)))
5396 (setq tags (org-get-tags-at))
5397 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5398 (setq head (match-string 1))
5399 (let ((remove-re
5400 (if org-agenda-remove-timeranges-from-blocks
5401 (concat
5402 "<" (regexp-quote s1) ".*?>"
5403 "--"
5404 "<" (regexp-quote s2) ".*?>")
5405 nil)))
5406 (setq txt (org-format-agenda-item
5407 (format
5408 (nth (if (= d1 d2) 0 1)
5409 org-agenda-timerange-leaders)
5410 (1+ (- d0 d1)) (1+ (- d2 d1)))
5411 head category tags
5412 (cond ((and (= d1 d0) (= d2 d0))
5413 (concat "<" start-time ">--<" end-time ">"))
5414 ((= d1 d0)
5415 (concat "<" start-time ">"))
5416 ((= d2 d0)
5417 (concat "<" end-time ">"))
5418 (t nil))
5419 remove-re))))
5420 (org-add-props txt props
5421 'org-marker marker 'org-hd-marker hdmarker
5422 'type "block" 'date date
5423 'todo-state todo-state
5424 'priority (org-get-priority txt) 'org-category category
5425 'org-category-position category-pos)
5426 (push txt ee))))
5427 (goto-char pos)))
5428 ;; Sort the entries by expiration date.
5429 (nreverse ee)))
5431 ;;; Agenda presentation and sorting
5433 (defvar org-prefix-has-time nil
5434 "A flag, set by `org-compile-prefix-format'.
5435 The flag is set if the currently compiled format contains a `%t'.")
5436 (defvar org-prefix-has-tag nil
5437 "A flag, set by `org-compile-prefix-format'.
5438 The flag is set if the currently compiled format contains a `%T'.")
5439 (defvar org-prefix-has-effort nil
5440 "A flag, set by `org-compile-prefix-format'.
5441 The flag is set if the currently compiled format contains a `%e'.")
5442 (defvar org-prefix-category-length nil
5443 "Used by `org-compile-prefix-format' to remember the category field width.")
5444 (defvar org-prefix-category-max-length nil
5445 "Used by `org-compile-prefix-format' to remember the category field width.")
5447 (defun org-agenda-get-category-icon (category)
5448 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5449 (dolist (entry org-agenda-category-icon-alist)
5450 (when (org-string-match-p (car entry) category)
5451 (if (listp (cadr entry))
5452 (return (cadr entry))
5453 (return (apply 'create-image (cdr entry)))))))
5455 (defun org-format-agenda-item (extra txt &optional category tags dotime
5456 remove-re habitp)
5457 "Format TXT to be inserted into the agenda buffer.
5458 In particular, it adds the prefix and corresponding text properties. EXTRA
5459 must be a string and replaces the `%s' specifier in the prefix format.
5460 CATEGORY (string, symbol or nil) may be used to overrule the default
5461 category taken from local variable or file name. It will replace the `%c'
5462 specifier in the format. DOTIME, when non-nil, indicates that a
5463 time-of-day should be extracted from TXT for sorting of this entry, and for
5464 the `%t' specifier in the format. When DOTIME is a string, this string is
5465 searched for a time before TXT is. TAGS can be the tags of the headline.
5466 Any match of REMOVE-RE will be removed from TXT."
5467 (save-match-data
5468 ;; Diary entries sometimes have extra whitespace at the beginning
5469 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5471 ;; Fix the tags part in txt
5472 (setq txt (org-agenda-fix-displayed-tags
5473 txt tags
5474 org-agenda-show-inherited-tags
5475 org-agenda-hide-tags-regexp))
5476 (let* ((category (or category
5477 (if (stringp org-category)
5478 org-category
5479 (and org-category (symbol-name org-category)))
5480 (if buffer-file-name
5481 (file-name-sans-extension
5482 (file-name-nondirectory buffer-file-name))
5483 "")))
5484 (category-icon (org-agenda-get-category-icon category))
5485 (category-icon (if category-icon
5486 (propertize " " 'display category-icon)
5487 ""))
5488 ;; time, tag, effort are needed for the eval of the prefix format
5489 (tag (if tags (nth (1- (length tags)) tags) ""))
5490 time effort neffort
5491 (ts (if dotime (concat
5492 (if (stringp dotime) dotime "")
5493 (and org-agenda-search-headline-for-time txt))))
5494 (time-of-day (and dotime (org-get-time-of-day ts)))
5495 stamp plain s0 s1 s2 rtn srp l
5496 duration thecategory)
5497 (and (org-mode-p) buffer-file-name
5498 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5499 (when (and dotime time-of-day)
5500 ;; Extract starting and ending time and move them to prefix
5501 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5502 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5503 (setq s0 (match-string 0 ts)
5504 srp (and stamp (match-end 3))
5505 s1 (match-string (if plain 1 2) ts)
5506 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5508 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5509 ;; them, we might want to remove them there to avoid duplication.
5510 ;; The user can turn this off with a variable.
5511 (if (and org-prefix-has-time
5512 org-agenda-remove-times-when-in-prefix (or stamp plain)
5513 (string-match (concat (regexp-quote s0) " *") txt)
5514 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5515 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5516 (= (match-beginning 0) 0)
5518 (setq txt (replace-match "" nil nil txt))))
5519 ;; Normalize the time(s) to 24 hour
5520 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5521 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5523 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5524 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5525 (setq s2
5526 (org-minutes-to-hh:mm-string
5527 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5529 ;; Compute the duration
5530 (when s2
5531 (setq duration (- (org-hh:mm-string-to-minutes s2)
5532 (org-hh:mm-string-to-minutes s1)))))
5534 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5535 txt)
5536 ;; Tags are in the string
5537 (if (or (eq org-agenda-remove-tags t)
5538 (and org-agenda-remove-tags
5539 org-prefix-has-tag))
5540 (setq txt (replace-match "" t t txt))
5541 (setq txt (replace-match
5542 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5543 (match-string 2 txt))
5544 t t txt))))
5545 (when (org-mode-p)
5546 (setq effort
5547 (condition-case nil
5548 (org-get-effort
5549 (or (get-text-property 0 'org-hd-marker txt)
5550 (get-text-property 0 'org-marker txt)))
5551 (error nil)))
5552 (when effort
5553 (setq neffort (org-duration-string-to-minutes effort)
5554 effort (setq effort (concat "[" effort "]")))))
5555 ;; prevent erroring out with %e format when there is no effort
5556 (or effort (setq effort ""))
5558 (when remove-re
5559 (while (string-match remove-re txt)
5560 (setq txt (replace-match "" t t txt))))
5562 ;; Set org-heading property on `txt' to mark the start of the
5563 ;; heading.
5564 (add-text-properties 0 (length txt) '(org-heading t) txt)
5566 ;; Prepare the variables needed in the eval of the compiled format
5567 (setq time (cond (s2 (concat
5568 (org-agenda-time-of-day-to-ampm-maybe s1)
5569 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5570 (if org-agenda-timegrid-use-ampm " ")))
5571 (s1 (concat
5572 (org-agenda-time-of-day-to-ampm-maybe s1)
5573 (if org-agenda-timegrid-use-ampm
5574 "........ "
5575 "......")))
5576 (t ""))
5577 extra (or (and (not habitp) extra) "")
5578 category (if (symbolp category) (symbol-name category) category)
5579 thecategory (copy-sequence category))
5580 (if (string-match org-bracket-link-regexp category)
5581 (progn
5582 (setq l (if (match-end 3)
5583 (- (match-end 3) (match-beginning 3))
5584 (- (match-end 1) (match-beginning 1))))
5585 (when (< l (or org-prefix-category-length 0))
5586 (setq category (copy-sequence category))
5587 (org-add-props category nil
5588 'extra-space (make-string
5589 (- org-prefix-category-length l 1) ?\ ))))
5590 (if (and org-prefix-category-max-length
5591 (>= (length category) org-prefix-category-max-length))
5592 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5593 ;; Evaluate the compiled format
5594 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5596 ;; And finally add the text properties
5597 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5598 (org-add-props rtn nil
5599 'org-category (if thecategory (downcase thecategory) category)
5600 'tags (mapcar 'org-downcase-keep-props tags)
5601 'org-highest-priority org-highest-priority
5602 'org-lowest-priority org-lowest-priority
5603 'time-of-day time-of-day
5604 'duration duration
5605 'effort effort
5606 'effort-minutes neffort
5607 'txt txt
5608 'time time
5609 'extra extra
5610 'format org-prefix-format-compiled
5611 'dotime dotime))))
5613 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5614 "Remove tags string from TXT, and add a modified list of tags.
5615 The modified list may contain inherited tags, and tags matched by
5616 `org-agenda-hide-tags-regexp' will be removed."
5617 (when (or add-inherited hide-re)
5618 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5619 (setq txt (substring txt 0 (match-beginning 0))))
5620 (setq tags
5621 (delq nil
5622 (mapcar (lambda (tg)
5623 (if (or (and hide-re (string-match hide-re tg))
5624 (and (not add-inherited)
5625 (get-text-property 0 'inherited tg)))
5627 tg))
5628 tags)))
5629 (when tags
5630 (let ((have-i (get-text-property 0 'inherited (car tags)))
5632 (setq txt (concat txt " :"
5633 (mapconcat
5634 (lambda (x)
5635 (setq i (get-text-property 0 'inherited x))
5636 (if (and have-i (not i))
5637 (progn
5638 (setq have-i nil)
5639 (concat ":" x))
5641 tags ":")
5642 (if have-i "::" ":"))))))
5643 txt)
5645 (defun org-downcase-keep-props (s)
5646 (let ((props (text-properties-at 0 s)))
5647 (setq s (downcase s))
5648 (add-text-properties 0 (length s) props s)
5651 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5652 (defvar org-agenda-sorting-strategy-selected nil)
5654 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5655 (catch 'exit
5656 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5657 ((and todayp (member 'today (car org-agenda-time-grid))))
5658 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5659 ((member 'weekly (car org-agenda-time-grid)))
5660 (t (throw 'exit list)))
5661 (let* ((have (delq nil (mapcar
5662 (lambda (x) (get-text-property 1 'time-of-day x))
5663 list)))
5664 (string (nth 1 org-agenda-time-grid))
5665 (gridtimes (nth 2 org-agenda-time-grid))
5666 (req (car org-agenda-time-grid))
5667 (remove (member 'remove-match req))
5668 new time)
5669 (if (and (member 'require-timed req) (not have))
5670 ;; don't show empty grid
5671 (throw 'exit list))
5672 (while (setq time (pop gridtimes))
5673 (unless (and remove (member time have))
5674 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5675 (push (org-format-agenda-item
5676 nil string "" nil
5677 (concat (substring time 0 -2) ":" (substring time -2)))
5678 new)
5679 (put-text-property
5680 2 (length (car new)) 'face 'org-time-grid (car new))))
5681 (when (and todayp org-agenda-show-current-time-in-grid)
5682 (push (org-format-agenda-item
5684 org-agenda-current-time-string
5685 "" nil
5686 (format-time-string "%H:%M "))
5687 new)
5688 (put-text-property
5689 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5691 (if (member 'time-up org-agenda-sorting-strategy-selected)
5692 (append new list)
5693 (append list new)))))
5695 (defun org-compile-prefix-format (key)
5696 "Compile the prefix format into a Lisp form that can be evaluated.
5697 The resulting form is returned and stored in the variable
5698 `org-prefix-format-compiled'."
5699 (setq org-prefix-has-time nil org-prefix-has-tag nil
5700 org-prefix-category-length nil
5701 org-prefix-has-effort nil)
5702 (let ((s (cond
5703 ((stringp org-agenda-prefix-format)
5704 org-agenda-prefix-format)
5705 ((assq key org-agenda-prefix-format)
5706 (cdr (assq key org-agenda-prefix-format)))
5707 (t " %-12:c%?-12t% s")))
5708 (start 0)
5709 varform vars var e c f opt)
5710 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5711 s start)
5712 (setq var (or (cdr (assoc (match-string 4 s)
5713 '(("c" . category) ("t" . time) ("s" . extra)
5714 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5715 'eval)
5716 c (or (match-string 3 s) "")
5717 opt (match-beginning 1)
5718 start (1+ (match-beginning 0)))
5719 (if (equal var 'time) (setq org-prefix-has-time t))
5720 (if (equal var 'tag) (setq org-prefix-has-tag t))
5721 (if (equal var 'effort) (setq org-prefix-has-effort t))
5722 (setq f (concat "%" (match-string 2 s) "s"))
5723 (when (equal var 'category)
5724 (setq org-prefix-category-length
5725 (floor (abs (string-to-number (match-string 2 s)))))
5726 (setq org-prefix-category-max-length
5727 (let ((x (match-string 2 s)))
5728 (save-match-data
5729 (if (string-match "\\.[0-9]+" x)
5730 (string-to-number (substring (match-string 0 x) 1)))))))
5731 (if (eq var 'eval)
5732 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5733 (if opt
5734 (setq varform
5735 `(if (equal "" ,var)
5737 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5738 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5739 (setq s (replace-match "%s" t nil s))
5740 (push varform vars))
5741 (setq vars (nreverse vars))
5742 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5744 (defun org-set-sorting-strategy (key)
5745 (if (symbolp (car org-agenda-sorting-strategy))
5746 ;; the old format
5747 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5748 (setq org-agenda-sorting-strategy-selected
5749 (or (cdr (assq key org-agenda-sorting-strategy))
5750 (cdr (assq 'agenda org-agenda-sorting-strategy))
5751 '(time-up category-keep priority-down)))))
5753 (defun org-get-time-of-day (s &optional string mod24)
5754 "Check string S for a time of day.
5755 If found, return it as a military time number between 0 and 2400.
5756 If not found, return nil.
5757 The optional STRING argument forces conversion into a 5 character wide string
5758 HH:MM."
5759 (save-match-data
5760 (when
5761 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5762 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5763 (let* ((h (string-to-number (match-string 1 s)))
5764 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5765 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5766 (am-p (equal ampm "am"))
5767 (h1 (cond ((not ampm) h)
5768 ((= h 12) (if am-p 0 12))
5769 (t (+ h (if am-p 0 12)))))
5770 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5771 (mod h1 24) h1))
5772 (t0 (+ (* 100 h2) m))
5773 (t1 (concat (if (>= h1 24) "+" " ")
5774 (if (and org-agenda-time-leading-zero
5775 (< t0 1000)) "0" "")
5776 (if (< t0 100) "0" "")
5777 (if (< t0 10) "0" "")
5778 (int-to-string t0))))
5779 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5781 (defvar org-agenda-before-sorting-filter-function nil
5782 "Function to be applied to agenda items prior to sorting.
5783 Prior to sorting also means just before they are inserted into the agenda.
5785 To aid sorting, you may revisit the original entries and add more text
5786 properties which will later be used by the sorting functions.
5788 The function should take a string argument, an agenda line.
5789 It has access to the text properties in that line, which contain among
5790 other things, the property `org-hd-marker' that points to the entry
5791 where the line comes from. Note that not all lines going into the agenda
5792 have this property, only most.
5794 The function should return the modified string. It is probably best
5795 to ONLY change text properties.
5797 You can also use this function as a filter, by returning nil for lines
5798 you don't want to have in the agenda at all. For this application, you
5799 could bind the variable in the options section of a custom command.")
5801 (defun org-finalize-agenda-entries (list &optional nosort)
5802 "Sort and concatenate the agenda items."
5803 (setq list (mapcar 'org-agenda-highlight-todo list))
5804 (if nosort
5805 list
5806 (when org-agenda-before-sorting-filter-function
5807 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5808 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5810 (defun org-agenda-highlight-todo (x)
5811 (let ((org-done-keywords org-done-keywords-for-agenda)
5812 (case-fold-search nil)
5814 (if (eq x 'line)
5815 (save-excursion
5816 (beginning-of-line 1)
5817 (setq re (org-get-at-bol 'org-todo-regexp))
5818 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5819 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5820 (add-text-properties (match-beginning 0) (match-end 1)
5821 (list 'face (org-get-todo-face 1)))
5822 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5823 (delete-region (match-beginning 1) (1- (match-end 0)))
5824 (goto-char (match-beginning 1))
5825 (insert (format org-agenda-todo-keyword-format s)))))
5826 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5827 (setq re (get-text-property 0 'org-todo-regexp x))
5828 (when (and re
5829 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5830 x (or pl 0)) pl))
5831 (add-text-properties
5832 (or (match-end 1) (match-end 0)) (match-end 0)
5833 (list 'face (org-get-todo-face (match-string 2 x)))
5835 (when (match-end 1)
5836 (setq x (concat (substring x 0 (match-end 1))
5837 (format org-agenda-todo-keyword-format
5838 (match-string 2 x))
5839 (org-add-props " " (text-properties-at 0 x))
5840 (substring x (match-end 3)))))))
5841 x)))
5843 (defsubst org-cmp-priority (a b)
5844 "Compare the priorities of string A and B."
5845 (let ((pa (or (get-text-property 1 'priority a) 0))
5846 (pb (or (get-text-property 1 'priority b) 0)))
5847 (cond ((> pa pb) +1)
5848 ((< pa pb) -1)
5849 (t nil))))
5851 (defsubst org-cmp-effort (a b)
5852 "Compare the effort values of string A and B."
5853 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5854 (ea (or (get-text-property 1 'effort-minutes a) def))
5855 (eb (or (get-text-property 1 'effort-minutes b) def)))
5856 (cond ((> ea eb) +1)
5857 ((< ea eb) -1)
5858 (t nil))))
5860 (defsubst org-cmp-category (a b)
5861 "Compare the string values of categories of strings A and B."
5862 (let ((ca (or (get-text-property 1 'org-category a) ""))
5863 (cb (or (get-text-property 1 'org-category b) "")))
5864 (cond ((string-lessp ca cb) -1)
5865 ((string-lessp cb ca) +1)
5866 (t nil))))
5868 (defsubst org-cmp-todo-state (a b)
5869 "Compare the todo states of strings A and B."
5870 (let* ((ma (or (get-text-property 1 'org-marker a)
5871 (get-text-property 1 'org-hd-marker a)))
5872 (mb (or (get-text-property 1 'org-marker b)
5873 (get-text-property 1 'org-hd-marker b)))
5874 (fa (and ma (marker-buffer ma)))
5875 (fb (and mb (marker-buffer mb)))
5876 (todo-kwds
5877 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5878 (and fb (with-current-buffer fb org-todo-keywords-1))))
5879 (ta (or (get-text-property 1 'todo-state a) ""))
5880 (tb (or (get-text-property 1 'todo-state b) ""))
5881 (la (- (length (member ta todo-kwds))))
5882 (lb (- (length (member tb todo-kwds))))
5883 (donepa (member ta org-done-keywords-for-agenda))
5884 (donepb (member tb org-done-keywords-for-agenda)))
5885 (cond ((and donepa (not donepb)) -1)
5886 ((and (not donepa) donepb) +1)
5887 ((< la lb) -1)
5888 ((< lb la) +1)
5889 (t nil))))
5891 (defsubst org-cmp-alpha (a b)
5892 "Compare the headlines, alphabetically."
5893 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5894 (plb (text-property-any 0 (length b) 'org-heading t b))
5895 (ta (and pla (substring a pla)))
5896 (tb (and plb (substring b plb))))
5897 (when pla
5898 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5899 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5900 (setq ta (substring ta (match-end 0))))
5901 (setq ta (downcase ta)))
5902 (when plb
5903 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5904 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5905 (setq tb (substring tb (match-end 0))))
5906 (setq tb (downcase tb)))
5907 (cond ((not ta) +1)
5908 ((not tb) -1)
5909 ((string-lessp ta tb) -1)
5910 ((string-lessp tb ta) +1)
5911 (t nil))))
5913 (defsubst org-cmp-tag (a b)
5914 "Compare the string values of the first tags of A and B."
5915 (let ((ta (car (last (get-text-property 1 'tags a))))
5916 (tb (car (last (get-text-property 1 'tags b)))))
5917 (cond ((not ta) +1)
5918 ((not tb) -1)
5919 ((string-lessp ta tb) -1)
5920 ((string-lessp tb ta) +1)
5921 (t nil))))
5923 (defsubst org-cmp-time (a b)
5924 "Compare the time-of-day values of strings A and B."
5925 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5926 (ta (or (get-text-property 1 'time-of-day a) def))
5927 (tb (or (get-text-property 1 'time-of-day b) def)))
5928 (cond ((< ta tb) -1)
5929 ((< tb ta) +1)
5930 (t nil))))
5932 (defsubst org-cmp-habit-p (a b)
5933 "Compare the todo states of strings A and B."
5934 (let ((ha (get-text-property 1 'org-habit-p a))
5935 (hb (get-text-property 1 'org-habit-p b)))
5936 (cond ((and ha (not hb)) -1)
5937 ((and (not ha) hb) +1)
5938 (t nil))))
5940 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5942 (defun org-entries-lessp (a b)
5943 "Predicate for sorting agenda entries."
5944 ;; The following variables will be used when the form is evaluated.
5945 ;; So even though the compiler complains, keep them.
5946 (let* ((ss org-agenda-sorting-strategy-selected)
5947 (time-up (and (org-em 'time-up 'time-down ss)
5948 (org-cmp-time a b)))
5949 (time-down (if time-up (- time-up) nil))
5950 (priority-up (and (org-em 'priority-up 'priority-down ss)
5951 (org-cmp-priority a b)))
5952 (priority-down (if priority-up (- priority-up) nil))
5953 (effort-up (and (org-em 'effort-up 'effort-down ss)
5954 (org-cmp-effort a b)))
5955 (effort-down (if effort-up (- effort-up) nil))
5956 (category-up (and (or (org-em 'category-up 'category-down ss)
5957 (memq 'category-keep ss))
5958 (org-cmp-category a b)))
5959 (category-down (if category-up (- category-up) nil))
5960 (category-keep (if category-up +1 nil))
5961 (tag-up (and (org-em 'tag-up 'tag-down ss)
5962 (org-cmp-tag a b)))
5963 (tag-down (if tag-up (- tag-up) nil))
5964 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5965 (org-cmp-todo-state a b)))
5966 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5967 (habit-up (and (org-em 'habit-up 'habit-down ss)
5968 (org-cmp-habit-p a b)))
5969 (habit-down (if habit-up (- habit-up) nil))
5970 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5971 (org-cmp-alpha a b)))
5972 (alpha-down (if alpha-up (- alpha-up) nil))
5973 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5974 user-defined-up user-defined-down)
5975 (if (and need-user-cmp org-agenda-cmp-user-defined
5976 (functionp org-agenda-cmp-user-defined))
5977 (setq user-defined-up
5978 (funcall org-agenda-cmp-user-defined a b)
5979 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5980 (cdr (assoc
5981 (eval (cons 'or org-agenda-sorting-strategy-selected))
5982 '((-1 . t) (1 . nil) (nil . nil))))))
5984 ;;; Agenda restriction lock
5986 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5987 "Overlay to mark the headline to which agenda commands are restricted.")
5988 (overlay-put org-agenda-restriction-lock-overlay
5989 'face 'org-agenda-restriction-lock)
5990 (overlay-put org-agenda-restriction-lock-overlay
5991 'help-echo "Agendas are currently limited to this subtree.")
5992 (org-detach-overlay org-agenda-restriction-lock-overlay)
5994 (defun org-agenda-set-restriction-lock (&optional type)
5995 "Set restriction lock for agenda, to current subtree or file.
5996 Restriction will be the file if TYPE is `file', or if type is the
5997 universal prefix '(4), or if the cursor is before the first headline
5998 in the file. Otherwise, restriction will be to the current subtree."
5999 (interactive "P")
6000 (and (equal type '(4)) (setq type 'file))
6001 (setq type (cond
6002 (type type)
6003 ((org-at-heading-p) 'subtree)
6004 ((condition-case nil (org-back-to-heading t) (error nil))
6005 'subtree)
6006 (t 'file)))
6007 (if (eq type 'subtree)
6008 (progn
6009 (setq org-agenda-restrict t)
6010 (setq org-agenda-overriding-restriction 'subtree)
6011 (put 'org-agenda-files 'org-restrict
6012 (list (buffer-file-name (buffer-base-buffer))))
6013 (org-back-to-heading t)
6014 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6015 (move-marker org-agenda-restrict-begin (point))
6016 (move-marker org-agenda-restrict-end
6017 (save-excursion (org-end-of-subtree t)))
6018 (message "Locking agenda restriction to subtree"))
6019 (put 'org-agenda-files 'org-restrict
6020 (list (buffer-file-name (buffer-base-buffer))))
6021 (setq org-agenda-restrict nil)
6022 (setq org-agenda-overriding-restriction 'file)
6023 (move-marker org-agenda-restrict-begin nil)
6024 (move-marker org-agenda-restrict-end nil)
6025 (message "Locking agenda restriction to file"))
6026 (setq current-prefix-arg nil)
6027 (org-agenda-maybe-redo))
6029 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6030 "Remove the agenda restriction lock."
6031 (interactive "P")
6032 (org-detach-overlay org-agenda-restriction-lock-overlay)
6033 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6034 (setq org-agenda-overriding-restriction nil)
6035 (setq org-agenda-restrict nil)
6036 (put 'org-agenda-files 'org-restrict nil)
6037 (move-marker org-agenda-restrict-begin nil)
6038 (move-marker org-agenda-restrict-end nil)
6039 (setq current-prefix-arg nil)
6040 (message "Agenda restriction lock removed")
6041 (or noupdate (org-agenda-maybe-redo)))
6043 (defun org-agenda-maybe-redo ()
6044 "If there is any window showing the agenda view, update it."
6045 (let ((w (get-buffer-window org-agenda-buffer-name t))
6046 (w0 (selected-window)))
6047 (when w
6048 (select-window w)
6049 (org-agenda-redo)
6050 (select-window w0)
6051 (if org-agenda-overriding-restriction
6052 (message "Agenda view shifted to new %s restriction"
6053 org-agenda-overriding-restriction)
6054 (message "Agenda restriction lock removed")))))
6056 ;;; Agenda commands
6058 (defun org-agenda-check-type (error &rest types)
6059 "Check if agenda buffer is of allowed type.
6060 If ERROR is non-nil, throw an error, otherwise just return nil."
6061 (if (memq org-agenda-type types)
6063 (if error
6064 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6065 nil)))
6067 (defun org-agenda-quit ()
6068 "Exit agenda by removing the window or the buffer."
6069 (interactive)
6070 (if org-agenda-columns-active
6071 (org-columns-quit)
6072 (let ((buf (current-buffer)))
6073 (if (eq org-agenda-window-setup 'other-frame)
6074 (progn
6075 (kill-buffer buf)
6076 (org-agenda-reset-markers)
6077 (org-columns-remove-overlays)
6078 (setq org-agenda-archives-mode nil)
6079 (delete-frame))
6080 (and (not (eq org-agenda-window-setup 'current-window))
6081 (not (one-window-p))
6082 (delete-window))
6083 (kill-buffer buf)
6084 (org-agenda-reset-markers)
6085 (org-columns-remove-overlays)
6086 (setq org-agenda-archives-mode nil)))
6087 ;; Maybe restore the pre-agenda window configuration.
6088 (and org-agenda-restore-windows-after-quit
6089 (not (eq org-agenda-window-setup 'other-frame))
6090 org-pre-agenda-window-conf
6091 (set-window-configuration org-pre-agenda-window-conf))))
6093 (defun org-agenda-exit ()
6094 "Exit agenda by removing the window or the buffer.
6095 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6096 Org-mode buffers visited directly by the user will not be touched."
6097 (interactive)
6098 (org-release-buffers org-agenda-new-buffers)
6099 (setq org-agenda-new-buffers nil)
6100 (org-agenda-quit))
6102 (defun org-agenda-execute (arg)
6103 "Execute another agenda command, keeping same window.
6104 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6105 in the agenda."
6106 (interactive "P")
6107 (let ((org-agenda-window-setup 'current-window))
6108 (org-agenda arg)))
6110 (defun org-agenda-redo ()
6111 "Rebuild Agenda.
6112 When this is the global TODO list, a prefix argument will be interpreted."
6113 (interactive)
6114 (let* ((org-agenda-keep-modes t)
6115 (filter org-agenda-filter)
6116 (preset (get 'org-agenda-filter :preset-filter))
6117 (org-agenda-filter-while-redo (or filter preset))
6118 (cols org-agenda-columns-active)
6119 (line (org-current-line))
6120 (window-line (- line (org-current-line (window-start))))
6121 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6122 (put 'org-agenda-filter :preset-filter nil)
6123 (and cols (org-columns-quit))
6124 (message "Rebuilding agenda buffer...")
6125 (org-let lprops '(eval org-agenda-redo-command))
6126 (setq org-agenda-undo-list nil
6127 org-agenda-pending-undo-list nil)
6128 (message "Rebuilding agenda buffer...done")
6129 (put 'org-agenda-filter :preset-filter preset)
6130 (and (or filter preset) (org-agenda-filter-apply filter))
6131 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6132 (org-goto-line line)
6133 (recenter window-line)))
6136 (defvar org-global-tags-completion-table nil)
6137 (defvar org-agenda-filter-form nil)
6138 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6139 "Keep only those lines in the agenda buffer that have a specific tag.
6140 The tag is selected with its fast selection letter, as configured.
6141 With prefix argument STRIP, remove all lines that do have the tag.
6142 A lisp caller can specify CHAR. NARROW means that the new tag should be
6143 used to narrow the search - the interactive user can also press `-' or `+'
6144 to switch to narrowing."
6145 (interactive "P")
6146 (let* ((alist org-tag-alist-for-agenda)
6147 (tag-chars (mapconcat
6148 (lambda (x) (if (and (not (symbolp (car x)))
6149 (cdr x))
6150 (char-to-string (cdr x))
6151 ""))
6152 alist ""))
6153 (efforts (org-split-string
6154 (or (cdr (assoc (concat org-effort-property "_ALL")
6155 org-global-properties))
6156 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6157 "")))
6158 (effort-op org-agenda-filter-effort-default-operator)
6159 (effort-prompt "")
6160 (inhibit-read-only t)
6161 (current org-agenda-filter)
6162 maybe-refresh a n tag)
6163 (unless char
6164 (message
6165 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6166 (if narrow "Narrow" "Filter") tag-chars
6167 (if org-agenda-auto-exclude-function "[RET], " ""))
6168 (setq char (read-char)))
6169 (when (member char '(?+ ?-))
6170 ;; Narrowing down
6171 (cond ((equal char ?-) (setq strip t narrow t))
6172 ((equal char ?+) (setq strip nil narrow t)))
6173 (message
6174 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6175 (setq char (read-char)))
6176 (when (member char '(?< ?> ?= ??))
6177 ;; An effort operator
6178 (setq effort-op (char-to-string char))
6179 (setq alist nil) ; to make sure it will be interpreted as effort.
6180 (unless (equal char ??)
6181 (loop for i from 0 to 9 do
6182 (setq effort-prompt
6183 (concat
6184 effort-prompt " ["
6185 (if (= i 9) "0" (int-to-string (1+ i)))
6186 "]" (nth i efforts))))
6187 (message "Effort%s: %s " effort-op effort-prompt)
6188 (setq char (read-char))
6189 (when (or (< char ?0) (> char ?9))
6190 (error "Need 1-9,0 to select effort" ))))
6191 (when (equal char ?\t)
6192 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6193 (org-set-local 'org-global-tags-completion-table
6194 (org-global-tags-completion-table)))
6195 (let ((completion-ignore-case t))
6196 (setq tag (org-icompleting-read
6197 "Tag: " org-global-tags-completion-table))))
6198 (cond
6199 ((equal char ?\r)
6200 (org-agenda-filter-by-tag-show-all)
6201 (when org-agenda-auto-exclude-function
6202 (setq org-agenda-filter '())
6203 (dolist (tag (org-agenda-get-represented-tags))
6204 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6205 (if modifier
6206 (push modifier org-agenda-filter))))
6207 (if (not (null org-agenda-filter))
6208 (org-agenda-filter-apply org-agenda-filter)))
6209 (setq maybe-refresh t))
6210 ((equal char ?/)
6211 (org-agenda-filter-by-tag-show-all)
6212 (when (get 'org-agenda-filter :preset-filter)
6213 (org-agenda-filter-apply org-agenda-filter))
6214 (setq maybe-refresh t))
6215 ((or (equal char ?\ )
6216 (setq a (rassoc char alist))
6217 (and (>= char ?0) (<= char ?9)
6218 (setq n (if (= char ?0) 9 (- char ?0 1))
6219 tag (concat effort-op (nth n efforts))
6220 a (cons tag nil)))
6221 (and (= char ??)
6222 (setq tag "?eff")
6223 a (cons tag nil))
6224 (and tag (setq a (cons tag nil))))
6225 (org-agenda-filter-by-tag-show-all)
6226 (setq tag (car a))
6227 (setq org-agenda-filter
6228 (cons (concat (if strip "-" "+") tag)
6229 (if narrow current nil)))
6230 (org-agenda-filter-apply org-agenda-filter)
6231 (setq maybe-refresh t))
6232 (t (error "Invalid tag selection character %c" char)))
6233 (when (and maybe-refresh
6234 (eq org-agenda-clockreport-mode 'with-filter))
6235 (org-agenda-redo))))
6237 (defun org-agenda-get-represented-tags ()
6238 "Get a list of all tags currently represented in the agenda."
6239 (let (p tags)
6240 (save-excursion
6241 (goto-char (point-min))
6242 (while (setq p (next-single-property-change (point) 'tags))
6243 (goto-char p)
6244 (mapc (lambda (x) (add-to-list 'tags x))
6245 (get-text-property (point) 'tags))))
6246 tags))
6248 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6249 "Refine the current filter. See `org-agenda-filter-by-tag'."
6250 (interactive "P")
6251 (org-agenda-filter-by-tag strip char 'refine))
6253 (defun org-agenda-filter-make-matcher ()
6254 "Create the form that tests a line for the agenda filter."
6255 (let (f f1)
6256 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6257 org-agenda-filter))
6258 (if (member x '("-" "+"))
6259 (setq f1 (if (equal x "-") 'tags '(not tags)))
6260 (if (string-match "[<=>?]" x)
6261 (setq f1 (org-agenda-filter-effort-form x))
6262 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6263 (if (equal (string-to-char x) ?-)
6264 (setq f1 (list 'not f1))))
6265 (push f1 f))
6266 (cons 'and (nreverse f))))
6268 (defun org-agenda-filter-effort-form (e)
6269 "Return the form to compare the effort of the current line with what E says.
6270 E looks like \"+<2:25\"."
6271 (let (op)
6272 (setq e (substring e 1))
6273 (setq op (string-to-char e) e (substring e 1))
6274 (setq op (cond ((equal op ?<) '<=)
6275 ((equal op ?>) '>=)
6276 ((equal op ??) op)
6277 (t '=)))
6278 (list 'org-agenda-compare-effort (list 'quote op)
6279 (org-duration-string-to-minutes e))))
6281 (defun org-agenda-compare-effort (op value)
6282 "Compare the effort of the current line with VALUE, using OP.
6283 If the line does not have an effort defined, return nil."
6284 (let ((eff (org-get-at-bol 'effort-minutes)))
6285 (if (equal op ??)
6286 (not eff)
6287 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6288 value))))
6290 (defun org-agenda-filter-apply (filter)
6291 "Set FILTER as the new agenda filter and apply it."
6292 (let (tags)
6293 (setq org-agenda-filter filter
6294 org-agenda-filter-form (org-agenda-filter-make-matcher))
6295 (org-agenda-set-mode-name)
6296 (save-excursion
6297 (goto-char (point-min))
6298 (while (not (eobp))
6299 (if (org-get-at-bol 'org-marker)
6300 (progn
6301 (setq tags (org-get-at-bol 'tags)) ; used in eval
6302 (if (not (eval org-agenda-filter-form))
6303 (org-agenda-filter-by-tag-hide-line))
6304 (beginning-of-line 2))
6305 (beginning-of-line 2))))
6306 (if (get-char-property (point) 'invisible)
6307 (org-agenda-previous-line))))
6309 (defun org-agenda-filter-by-tag-hide-line ()
6310 (let (ov)
6311 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6312 (point-at-eol)))
6313 (overlay-put ov 'invisible t)
6314 (overlay-put ov 'type 'tags-filter)
6315 (push ov org-agenda-filter-overlays)))
6317 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6318 (setq pos (or pos (point)))
6319 (save-excursion
6320 (dolist (ov (overlays-at pos))
6321 (when (and (overlay-get ov 'invisible)
6322 (eq (overlay-get ov 'type) 'tags-filter))
6323 (goto-char pos)
6324 (if (< (overlay-start ov) (point-at-eol))
6325 (move-overlay ov (point-at-eol)
6326 (overlay-end ov)))))))
6328 (defun org-agenda-filter-by-tag-show-all ()
6329 (mapc 'delete-overlay org-agenda-filter-overlays)
6330 (setq org-agenda-filter-overlays nil)
6331 (setq org-agenda-filter nil)
6332 (setq org-agenda-filter-form nil)
6333 (org-agenda-set-mode-name))
6335 (defun org-agenda-manipulate-query-add ()
6336 "Manipulate the query by adding a search term with positive selection.
6337 Positive selection means the term must be matched for selection of an entry."
6338 (interactive)
6339 (org-agenda-manipulate-query ?\[))
6340 (defun org-agenda-manipulate-query-subtract ()
6341 "Manipulate the query by adding a search term with negative selection.
6342 Negative selection means term must not be matched for selection of an entry."
6343 (interactive)
6344 (org-agenda-manipulate-query ?\]))
6345 (defun org-agenda-manipulate-query-add-re ()
6346 "Manipulate the query by adding a search regexp with positive selection.
6347 Positive selection means the regexp must match for selection of an entry."
6348 (interactive)
6349 (org-agenda-manipulate-query ?\{))
6350 (defun org-agenda-manipulate-query-subtract-re ()
6351 "Manipulate the query by adding a search regexp with negative selection.
6352 Negative selection means regexp must not match for selection of an entry."
6353 (interactive)
6354 (org-agenda-manipulate-query ?\}))
6355 (defun org-agenda-manipulate-query (char)
6356 (cond
6357 ((memq org-agenda-type '(timeline agenda))
6358 (let ((org-agenda-include-inactive-timestamps t))
6359 (org-agenda-redo))
6360 (message "Display now includes inactive timestamps as well"))
6361 ((eq org-agenda-type 'search)
6362 (org-add-to-string
6363 'org-agenda-query-string
6364 (if org-agenda-last-search-view-search-was-boolean
6365 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6366 (?\{ . " +{}") (?\} . " -{}"))))
6367 " "))
6368 (setq org-agenda-redo-command
6369 (list 'org-search-view
6370 org-todo-only
6371 org-agenda-query-string
6372 (+ (length org-agenda-query-string)
6373 (if (member char '(?\{ ?\})) 0 1))))
6374 (set-register org-agenda-query-register org-agenda-query-string)
6375 (org-agenda-redo))
6376 (t (error "Cannot manipulate query for %s-type agenda buffers"
6377 org-agenda-type))))
6379 (defun org-add-to-string (var string)
6380 (set var (concat (symbol-value var) string)))
6382 (defun org-agenda-goto-date (date)
6383 "Jump to DATE in agenda."
6384 (interactive (list (let ((org-read-date-prefer-future
6385 (eval org-agenda-jump-prefer-future)))
6386 (org-read-date))))
6387 (org-agenda-list nil date))
6389 (defun org-agenda-goto-today ()
6390 "Go to today."
6391 (interactive)
6392 (org-agenda-check-type t 'timeline 'agenda)
6393 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6394 (cond
6395 (tdpos (goto-char tdpos))
6396 ((eq org-agenda-type 'agenda)
6397 (let* ((sd (org-agenda-compute-starting-span
6398 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6399 (org-agenda-overriding-arguments org-agenda-last-arguments))
6400 (setf (nth 1 org-agenda-overriding-arguments) sd)
6401 (org-agenda-redo)
6402 (org-agenda-find-same-or-today-or-agenda)))
6403 (t (error "Cannot find today")))))
6405 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6406 (goto-char
6407 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6408 (text-property-any (point-min) (point-max) 'org-today t)
6409 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6410 (point-min))))
6412 (defun org-agenda-later (arg)
6413 "Go forward in time by thee current span.
6414 With prefix ARG, go forward that many times the current span."
6415 (interactive "p")
6416 (org-agenda-check-type t 'agenda)
6417 (let* ((span org-agenda-current-span)
6418 (sd org-starting-day)
6419 (greg (calendar-gregorian-from-absolute sd))
6420 (cnt (org-get-at-bol 'org-day-cnt))
6421 greg2)
6422 (cond
6423 ((eq span 'day)
6424 (setq sd (+ arg sd)))
6425 ((eq span 'week)
6426 (setq sd (+ (* 7 arg) sd)))
6427 ((eq span 'month)
6428 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6429 sd (calendar-absolute-from-gregorian greg2))
6430 (setcar greg2 (1+ (car greg2))))
6431 ((eq span 'year)
6432 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6433 sd (calendar-absolute-from-gregorian greg2))
6434 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6436 (setq sd (+ (* span arg) sd))))
6437 (let ((org-agenda-overriding-arguments
6438 (list (car org-agenda-last-arguments) sd span t)))
6439 (org-agenda-redo)
6440 (org-agenda-find-same-or-today-or-agenda cnt))))
6442 (defun org-agenda-earlier (arg)
6443 "Go backward in time by the current span.
6444 With prefix ARG, go backward that many times the current span."
6445 (interactive "p")
6446 (org-agenda-later (- arg)))
6448 (defun org-agenda-view-mode-dispatch ()
6449 "Call one of the view mode commands."
6450 (interactive)
6451 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6452 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6453 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6454 [E]ntryText")
6455 (let ((a (read-char-exclusive)))
6456 (case a
6457 (?\ (call-interactively 'org-agenda-reset-view))
6458 (?d (call-interactively 'org-agenda-day-view))
6459 (?w (call-interactively 'org-agenda-week-view))
6460 (?m (call-interactively 'org-agenda-month-view))
6461 (?y (call-interactively 'org-agenda-year-view))
6462 (?l (call-interactively 'org-agenda-log-mode))
6463 (?L (org-agenda-log-mode '(4)))
6464 (?c (org-agenda-log-mode 'clockcheck))
6465 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6466 (?a (call-interactively 'org-agenda-archives-mode))
6467 (?A (org-agenda-archives-mode 'files))
6468 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6469 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6470 (?G (call-interactively 'org-agenda-toggle-time-grid))
6471 (?D (call-interactively 'org-agenda-toggle-diary))
6472 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6473 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6474 (org-agenda-check-type t 'timeline 'agenda)
6475 (org-agenda-redo))
6476 (message "Display now includes inactive timestamps as well"))
6477 (?q (message "Abort"))
6478 (otherwise (error "Invalid key" )))))
6480 (defun org-agenda-reset-view ()
6481 "Switch to default view for agenda."
6482 (interactive)
6483 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6484 (defun org-agenda-day-view (&optional day-of-year)
6485 "Switch to daily view for agenda.
6486 With argument DAY-OF-YEAR, switch to that day of the year."
6487 (interactive "P")
6488 (org-agenda-change-time-span 'day day-of-year))
6489 (defun org-agenda-week-view (&optional iso-week)
6490 "Switch to daily view for agenda.
6491 With argument ISO-WEEK, switch to the corresponding ISO week.
6492 If ISO-WEEK has more then 2 digits, only the last two encode the
6493 week. Any digits before this encode a year. So 200712 means
6494 week 12 of year 2007. Years in the range 1938-2037 can also be
6495 written as 2-digit years."
6496 (interactive "P")
6497 (org-agenda-change-time-span 'week iso-week))
6498 (defun org-agenda-month-view (&optional month)
6499 "Switch to monthly view for agenda.
6500 With argument MONTH, switch to that month."
6501 (interactive "P")
6502 (org-agenda-change-time-span 'month month))
6503 (defun org-agenda-year-view (&optional year)
6504 "Switch to yearly view for agenda.
6505 With argument YEAR, switch to that year.
6506 If MONTH has more then 2 digits, only the last two encode the
6507 month. Any digits before this encode a year. So 200712 means
6508 December year 2007. Years in the range 1938-2037 can also be
6509 written as 2-digit years."
6510 (interactive "P")
6511 (when year
6512 (setq year (org-small-year-to-year year)))
6513 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6514 (org-agenda-change-time-span 'year year)
6515 (error "Abort")))
6517 (defun org-agenda-change-time-span (span &optional n)
6518 "Change the agenda view to SPAN.
6519 SPAN may be `day', `week', `month', `year'."
6520 (org-agenda-check-type t 'agenda)
6521 (if (and (not n) (equal org-agenda-current-span span))
6522 (error "Viewing span is already \"%s\"" span))
6523 (let* ((sd (or (org-get-at-bol 'day)
6524 org-starting-day))
6525 (sd (org-agenda-compute-starting-span sd span n))
6526 (org-agenda-overriding-arguments
6527 (or org-agenda-overriding-arguments
6528 (list (car org-agenda-last-arguments) sd span t))))
6529 (org-agenda-redo)
6530 (org-agenda-find-same-or-today-or-agenda))
6531 (org-agenda-set-mode-name)
6532 (message "Switched to %s view" span))
6534 (defun org-agenda-compute-starting-span (sd span &optional n)
6535 "Compute starting date for agenda.
6536 SPAN may be `day', `week', `month', `year'. The return value
6537 is a cons cell with the starting date and the number of days,
6538 so that the date SD will be in that range."
6539 (let* ((greg (calendar-gregorian-from-absolute sd))
6540 (dg (nth 1 greg))
6541 (mg (car greg))
6542 (yg (nth 2 greg)))
6543 (cond
6544 ((eq span 'day)
6545 (when n
6546 (setq sd (+ (calendar-absolute-from-gregorian
6547 (list mg 1 yg))
6548 n -1))))
6549 ((eq span 'week)
6550 (let* ((nt (calendar-day-of-week
6551 (calendar-gregorian-from-absolute sd)))
6552 (d (if org-agenda-start-on-weekday
6553 (- nt org-agenda-start-on-weekday)
6556 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6557 (when n
6558 (require 'cal-iso)
6559 (when (> n 99)
6560 (setq y1 (org-small-year-to-year (/ n 100))
6561 n (mod n 100)))
6562 (setq sd
6563 (calendar-absolute-from-iso
6564 (list n 1
6565 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6566 ((eq span 'month)
6567 (let (y1)
6568 (when (and n (> n 99))
6569 (setq y1 (org-small-year-to-year (/ n 100))
6570 n (mod n 100)))
6571 (setq sd (calendar-absolute-from-gregorian
6572 (list (or n mg) 1 (or y1 yg))))))
6573 ((eq span 'year)
6574 (setq sd (calendar-absolute-from-gregorian
6575 (list 1 1 (or n yg))))))
6576 sd))
6578 (defun org-agenda-next-date-line (&optional arg)
6579 "Jump to the next line indicating a date in agenda buffer."
6580 (interactive "p")
6581 (org-agenda-check-type t 'agenda 'timeline)
6582 (beginning-of-line 1)
6583 ;; This does not work if user makes date format that starts with a blank
6584 (if (looking-at "^\\S-") (forward-char 1))
6585 (if (not (re-search-forward "^\\S-" nil t arg))
6586 (progn
6587 (backward-char 1)
6588 (error "No next date after this line in this buffer")))
6589 (goto-char (match-beginning 0)))
6591 (defun org-agenda-previous-date-line (&optional arg)
6592 "Jump to the previous line indicating a date in agenda buffer."
6593 (interactive "p")
6594 (org-agenda-check-type t 'agenda 'timeline)
6595 (beginning-of-line 1)
6596 (if (not (re-search-backward "^\\S-" nil t arg))
6597 (error "No previous date before this line in this buffer")))
6599 ;; Initialize the highlight
6600 (defvar org-hl (make-overlay 1 1))
6601 (overlay-put org-hl 'face 'highlight)
6603 (defun org-highlight (begin end &optional buffer)
6604 "Highlight a region with overlay."
6605 (move-overlay org-hl begin end (or buffer (current-buffer))))
6607 (defun org-unhighlight ()
6608 "Detach overlay INDEX."
6609 (org-detach-overlay org-hl))
6611 ;; FIXME this is currently not used.
6612 (defun org-highlight-until-next-command (beg end &optional buffer)
6613 "Move the highlight overlay to BEG/END, remove it before the next command."
6614 (org-highlight beg end buffer)
6615 (add-hook 'pre-command-hook 'org-unhighlight-once))
6616 (defun org-unhighlight-once ()
6617 "Remove the highlight from its position, and this function from the hook."
6618 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6619 (org-unhighlight))
6621 (defun org-agenda-follow-mode ()
6622 "Toggle follow mode in an agenda buffer."
6623 (interactive)
6624 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6625 (org-agenda-set-mode-name)
6626 (org-agenda-do-context-action)
6627 (message "Follow mode is %s"
6628 (if org-agenda-follow-mode "on" "off")))
6630 (defun org-agenda-entry-text-mode (&optional arg)
6631 "Toggle entry text mode in an agenda buffer."
6632 (interactive "P")
6633 (setq org-agenda-entry-text-mode (or (integerp arg)
6634 (not org-agenda-entry-text-mode)))
6635 (org-agenda-entry-text-hide)
6636 (and org-agenda-entry-text-mode
6637 (let ((org-agenda-entry-text-maxlines
6638 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6639 (org-agenda-entry-text-show)))
6640 (org-agenda-set-mode-name)
6641 (message "Entry text mode is %s. Maximum number of lines is %d"
6642 (if org-agenda-entry-text-mode "on" "off")
6643 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6645 (defun org-agenda-clockreport-mode (&optional with-filter)
6646 "Toggle clocktable mode in an agenda buffer.
6647 With prefix arg WITH-FILTER, make the clocktable respect the current
6648 agenda filter."
6649 (interactive "P")
6650 (org-agenda-check-type t 'agenda)
6651 (if with-filter
6652 (setq org-agenda-clockreport-mode 'with-filter)
6653 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6654 (org-agenda-set-mode-name)
6655 (org-agenda-redo)
6656 (message "Clocktable mode is %s"
6657 (if org-agenda-clockreport-mode "on" "off")))
6659 (defun org-agenda-log-mode (&optional special)
6660 "Toggle log mode in an agenda buffer.
6661 With argument SPECIAL, show all possible log items, not only the ones
6662 configured in `org-agenda-log-mode-items'.
6663 With a double `C-u' prefix arg, show *only* log items, nothing else."
6664 (interactive "P")
6665 (org-agenda-check-type t 'agenda 'timeline)
6666 (setq org-agenda-show-log
6667 (cond
6668 ((equal special '(16)) 'only)
6669 ((eq special 'clockcheck)
6670 (if (eq org-agenda-show-log 'clockcheck)
6671 nil 'clockcheck))
6672 (special '(closed clock state))
6673 (t (not org-agenda-show-log))))
6674 (org-agenda-set-mode-name)
6675 (org-agenda-redo)
6676 (message "Log mode is %s"
6677 (if org-agenda-show-log "on" "off")))
6679 (defun org-agenda-archives-mode (&optional with-files)
6680 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6681 When called with a prefix argument, include all archive files as well."
6682 (interactive "P")
6683 (setq org-agenda-archives-mode
6684 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6685 (org-agenda-set-mode-name)
6686 (org-agenda-redo)
6687 (message
6688 "%s"
6689 (cond
6690 ((eq org-agenda-archives-mode nil)
6691 "No archives are included")
6692 ((eq org-agenda-archives-mode 'trees)
6693 (format "Trees with :%s: tag are included" org-archive-tag))
6694 ((eq org-agenda-archives-mode t)
6695 (format "Trees with :%s: tag and all active archive files are included"
6696 org-archive-tag)))))
6698 (defun org-agenda-toggle-diary ()
6699 "Toggle diary inclusion in an agenda buffer."
6700 (interactive)
6701 (org-agenda-check-type t 'agenda)
6702 (setq org-agenda-include-diary (not org-agenda-include-diary))
6703 (org-agenda-redo)
6704 (org-agenda-set-mode-name)
6705 (message "Diary inclusion turned %s"
6706 (if org-agenda-include-diary "on" "off")))
6708 (defun org-agenda-toggle-deadlines ()
6709 "Toggle inclusion of entries with a deadline in an agenda buffer."
6710 (interactive)
6711 (org-agenda-check-type t 'agenda)
6712 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6713 (org-agenda-redo)
6714 (org-agenda-set-mode-name)
6715 (message "Deadlines inclusion turned %s"
6716 (if org-agenda-include-deadlines "on" "off")))
6718 (defun org-agenda-toggle-time-grid ()
6719 "Toggle time grid in an agenda buffer."
6720 (interactive)
6721 (org-agenda-check-type t 'agenda)
6722 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6723 (org-agenda-redo)
6724 (org-agenda-set-mode-name)
6725 (message "Time-grid turned %s"
6726 (if org-agenda-use-time-grid "on" "off")))
6728 (defun org-agenda-set-mode-name ()
6729 "Set the mode name to indicate all the small mode settings."
6730 (setq mode-name
6731 (list "Org-Agenda"
6732 (if (get 'org-agenda-files 'org-restrict) " []" "")
6734 '(:eval (org-agenda-span-name org-agenda-current-span))
6735 (if org-agenda-follow-mode " Follow" "")
6736 (if org-agenda-entry-text-mode " ETxt" "")
6737 (if org-agenda-include-diary " Diary" "")
6738 (if org-agenda-include-deadlines " Ddl" "")
6739 (if org-agenda-use-time-grid " Grid" "")
6740 (if (and (boundp 'org-habit-show-habits)
6741 org-habit-show-habits) " Habit" "")
6742 (cond
6743 ((consp org-agenda-show-log) " LogAll")
6744 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6745 (org-agenda-show-log " Log")
6746 (t ""))
6747 (if (or org-agenda-filter (get 'org-agenda-filter
6748 :preset-filter))
6749 (concat " {" (mapconcat
6750 'identity
6751 (append (get 'org-agenda-filter
6752 :preset-filter)
6753 org-agenda-filter) "") "}")
6755 (if org-agenda-archives-mode
6756 (if (eq org-agenda-archives-mode t)
6757 " Archives"
6758 (format " :%s:" org-archive-tag))
6760 (if org-agenda-clockreport-mode
6761 (if (eq org-agenda-clockreport-mode 'with-filter)
6762 " Clock{}" " Clock")
6763 "")))
6764 (force-mode-line-update))
6766 (defun org-agenda-post-command-hook ()
6767 (setq org-agenda-type
6768 (or (get-text-property (point) 'org-agenda-type)
6769 (get-text-property (max (point-min) (1- (point)))
6770 'org-agenda-type))))
6772 (defun org-agenda-next-line ()
6773 "Move cursor to the next line, and show if follow mode is active."
6774 (interactive)
6775 (call-interactively 'next-line)
6776 (org-agenda-do-context-action))
6778 (defun org-agenda-previous-line ()
6779 "Move cursor to the previous line, and show if follow-mode is active."
6780 (interactive)
6781 (call-interactively 'previous-line)
6782 (org-agenda-do-context-action))
6784 (defun org-agenda-do-context-action ()
6785 "Show outline path and, maybe, follow mode window."
6786 (let ((m (org-get-at-bol 'org-marker)))
6787 (if (and org-agenda-follow-mode m)
6788 (if org-agenda-follow-indirect
6789 (org-agenda-tree-to-indirect-buffer)
6790 (org-agenda-show)))
6791 (if (and m org-agenda-show-outline-path)
6792 (org-with-point-at m
6793 (org-display-outline-path t)))))
6795 (defun org-agenda-show-priority ()
6796 "Show the priority of the current item.
6797 This priority is composed of the main priority given with the [#A] cookies,
6798 and by additional input from the age of a schedules or deadline entry."
6799 (interactive)
6800 (let* ((pri (org-get-at-bol 'priority)))
6801 (message "Priority is %d" (if pri pri -1000))))
6803 (defun org-agenda-show-tags ()
6804 "Show the tags applicable to the current item."
6805 (interactive)
6806 (let* ((tags (org-get-at-bol 'tags)))
6807 (if tags
6808 (message "Tags are :%s:"
6809 (org-no-properties (mapconcat 'identity tags ":")))
6810 (message "No tags associated with this line"))))
6812 (defun org-agenda-goto (&optional highlight)
6813 "Go to the Org-mode file which contains the item at point."
6814 (interactive)
6815 (let* ((marker (or (org-get-at-bol 'org-marker)
6816 (org-agenda-error)))
6817 (buffer (marker-buffer marker))
6818 (pos (marker-position marker)))
6819 (switch-to-buffer-other-window buffer)
6820 (widen)
6821 (push-mark)
6822 (goto-char pos)
6823 (when (org-mode-p)
6824 (org-show-context 'agenda)
6825 (save-excursion
6826 (and (outline-next-heading)
6827 (org-flag-heading nil)))) ; show the next heading
6828 (when (outline-invisible-p)
6829 (show-entry)) ; display invisible text
6830 (recenter (/ (window-height) 2))
6831 (run-hooks 'org-agenda-after-show-hook)
6832 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6834 (defvar org-agenda-after-show-hook nil
6835 "Normal hook run after an item has been shown from the agenda.
6836 Point is in the buffer where the item originated.")
6838 (defun org-agenda-kill ()
6839 "Kill the entry or subtree belonging to the current agenda entry."
6840 (interactive)
6841 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6842 (let* ((marker (or (org-get-at-bol 'org-marker)
6843 (org-agenda-error)))
6844 (buffer (marker-buffer marker))
6845 (pos (marker-position marker))
6846 (type (org-get-at-bol 'type))
6847 dbeg dend (n 0) conf)
6848 (org-with-remote-undo buffer
6849 (with-current-buffer buffer
6850 (save-excursion
6851 (goto-char pos)
6852 (if (and (org-mode-p) (not (member type '("sexp"))))
6853 (setq dbeg (progn (org-back-to-heading t) (point))
6854 dend (org-end-of-subtree t t))
6855 (setq dbeg (point-at-bol)
6856 dend (min (point-max) (1+ (point-at-eol)))))
6857 (goto-char dbeg)
6858 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6859 (setq conf (or (eq t org-agenda-confirm-kill)
6860 (and (numberp org-agenda-confirm-kill)
6861 (> n org-agenda-confirm-kill))))
6862 (and conf
6863 (not (y-or-n-p
6864 (format "Delete entry with %d lines in buffer \"%s\"? "
6865 n (buffer-name buffer))))
6866 (error "Abort"))
6867 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6868 (with-current-buffer buffer (delete-region dbeg dend))
6869 (message "Agenda item and source killed"))))
6871 (defvar org-archive-default-command)
6872 (defun org-agenda-archive-default ()
6873 "Archive the entry or subtree belonging to the current agenda entry."
6874 (interactive)
6875 (require 'org-archive)
6876 (org-agenda-archive-with org-archive-default-command))
6878 (defun org-agenda-archive-default-with-confirmation ()
6879 "Archive the entry or subtree belonging to the current agenda entry."
6880 (interactive)
6881 (require 'org-archive)
6882 (org-agenda-archive-with org-archive-default-command 'confirm))
6884 (defun org-agenda-archive ()
6885 "Archive the entry or subtree belonging to the current agenda entry."
6886 (interactive)
6887 (org-agenda-archive-with 'org-archive-subtree))
6889 (defun org-agenda-archive-to-archive-sibling ()
6890 "Move the entry to the archive sibling."
6891 (interactive)
6892 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6894 (defun org-agenda-archive-with (cmd &optional confirm)
6895 "Move the entry to the archive sibling."
6896 (interactive)
6897 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6898 (let* ((marker (or (org-get-at-bol 'org-marker)
6899 (org-agenda-error)))
6900 (buffer (marker-buffer marker))
6901 (pos (marker-position marker)))
6902 (org-with-remote-undo buffer
6903 (with-current-buffer buffer
6904 (if (org-mode-p)
6905 (if (and confirm
6906 (not (y-or-n-p "Archive this subtree or entry? ")))
6907 (error "Abort")
6908 (save-excursion
6909 (goto-char pos)
6910 (org-remove-subtree-entries-from-agenda)
6911 (org-back-to-heading t)
6912 (funcall cmd)))
6913 (error "Archiving works only in Org-mode files"))))))
6915 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6916 "Remove all lines in the agenda that correspond to a given subtree.
6917 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6918 If this information is not given, the function uses the tree at point."
6919 (let ((buf (or buf (current-buffer))) m p)
6920 (save-excursion
6921 (unless (and beg end)
6922 (org-back-to-heading t)
6923 (setq beg (point))
6924 (org-end-of-subtree t)
6925 (setq end (point)))
6926 (set-buffer (get-buffer org-agenda-buffer-name))
6927 (save-excursion
6928 (goto-char (point-max))
6929 (beginning-of-line 1)
6930 (while (not (bobp))
6931 (when (and (setq m (org-get-at-bol 'org-marker))
6932 (equal buf (marker-buffer m))
6933 (setq p (marker-position m))
6934 (>= p beg)
6935 (< p end))
6936 (let ((inhibit-read-only t))
6937 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6938 (beginning-of-line 0))))))
6940 (defun org-agenda-refile (&optional goto rfloc no-update)
6941 "Refile the item at point."
6942 (interactive "P")
6943 (if (equal goto '(16))
6944 (org-refile-goto-last-stored)
6945 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6946 (org-agenda-error)))
6947 (buffer (marker-buffer marker))
6948 (pos (marker-position marker))
6949 (rfloc (or rfloc
6950 (org-refile-get-location
6951 (if goto "Goto" "Refile to") buffer
6952 org-refile-allow-creating-parent-nodes))))
6953 (with-current-buffer buffer
6954 (save-excursion
6955 (save-restriction
6956 (widen)
6957 (goto-char marker)
6958 (org-remove-subtree-entries-from-agenda)
6959 (org-refile goto buffer rfloc)))))
6960 (unless no-update (org-agenda-redo))))
6962 (defun org-agenda-open-link (&optional arg)
6963 "Follow the link in the current line, if any.
6964 This looks for a link in the displayed line in the agenda. It also looks
6965 at the text of the entry itself."
6966 (interactive "P")
6967 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6968 (org-get-at-bol 'org-marker)))
6969 (buffer (and marker (marker-buffer marker)))
6970 (prefix (buffer-substring
6971 (point-at-bol) (point-at-eol))))
6972 (cond
6973 (buffer
6974 (with-current-buffer buffer
6975 (save-excursion
6976 (save-restriction
6977 (widen)
6978 (goto-char marker)
6979 (org-offer-links-in-entry arg prefix)))))
6980 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6981 (save-excursion
6982 (beginning-of-line 1)
6983 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6984 (org-open-link-from-string (match-string 1)))
6985 (t (error "No link to open here")))))
6987 (defun org-agenda-copy-local-variable (var)
6988 "Get a variable from a referenced buffer and install it here."
6989 (let ((m (org-get-at-bol 'org-marker)))
6990 (when (and m (buffer-live-p (marker-buffer m)))
6991 (org-set-local var (with-current-buffer (marker-buffer m)
6992 (symbol-value var))))))
6994 (defun org-agenda-switch-to (&optional delete-other-windows)
6995 "Go to the Org-mode file which contains the item at point."
6996 (interactive)
6997 (if (and org-return-follows-link
6998 (not (org-get-at-bol 'org-marker))
6999 (org-in-regexp org-bracket-link-regexp))
7000 (org-open-link-from-string (match-string 0))
7001 (let* ((marker (or (org-get-at-bol 'org-marker)
7002 (org-agenda-error)))
7003 (buffer (marker-buffer marker))
7004 (pos (marker-position marker)))
7005 (org-pop-to-buffer-same-window buffer)
7006 (and delete-other-windows (delete-other-windows))
7007 (widen)
7008 (goto-char pos)
7009 (when (org-mode-p)
7010 (org-show-context 'agenda)
7011 (save-excursion
7012 (and (outline-next-heading)
7013 (org-flag-heading nil))) ; show the next heading
7014 (when (outline-invisible-p)
7015 (show-entry)))))) ; display invisible text
7017 (defun org-agenda-goto-mouse (ev)
7018 "Go to the Org-mode file which contains the item at the mouse click."
7019 (interactive "e")
7020 (mouse-set-point ev)
7021 (org-agenda-goto))
7023 (defun org-agenda-show (&optional full-entry)
7024 "Display the Org-mode file which contains the item at point.
7025 With prefix argument FULL-ENTRY, make the entire entry visible
7026 if it was hidden in the outline."
7027 (interactive "P")
7028 (let ((win (selected-window)))
7029 (if full-entry
7030 (let ((org-show-entry-below t))
7031 (org-agenda-goto t))
7032 (org-agenda-goto t))
7033 (select-window win)))
7035 (defvar org-agenda-show-window nil)
7036 (defun org-agenda-show-and-scroll-up ()
7037 "Display the Org-mode file which contains the item at point.
7038 When called repeatedly, scroll the window that is displaying the buffer."
7039 (interactive)
7040 (let ((win (selected-window)))
7041 (if (and (window-live-p org-agenda-show-window)
7042 (eq this-command last-command))
7043 (progn
7044 (select-window org-agenda-show-window)
7045 (ignore-errors (scroll-up)))
7046 (org-agenda-goto t)
7047 (show-subtree)
7048 (setq org-agenda-show-window (selected-window)))
7049 (select-window win)))
7051 (defun org-agenda-show-scroll-down ()
7052 "Scroll down the window showing the agenda."
7053 (interactive)
7054 (let ((win (selected-window)))
7055 (when (window-live-p org-agenda-show-window)
7056 (select-window org-agenda-show-window)
7057 (ignore-errors (scroll-down))
7058 (select-window win))))
7060 (defun org-agenda-show-1 (&optional more)
7061 "Display the Org-mode file which contains the item at point.
7062 The prefix arg selects the amount of information to display:
7064 0 hide the subtree
7065 1 just show the entry according to defaults.
7066 2 show the children view
7067 3 show the subtree view
7068 4 show the entire subtree and any LOGBOOK drawers
7069 5 show the entire subtree and any drawers
7070 With prefix argument FULL-ENTRY, make the entire entry visible
7071 if it was hidden in the outline."
7072 (interactive "p")
7073 (let ((win (selected-window)))
7074 (org-agenda-goto t)
7075 (org-recenter-heading 1)
7076 (cond
7077 ((= more 0)
7078 (hide-subtree)
7079 (save-excursion
7080 (org-back-to-heading)
7081 (run-hook-with-args 'org-cycle-hook 'folded))
7082 (message "Remote: FOLDED"))
7083 ((and (org-called-interactively-p 'any) (= more 1))
7084 (message "Remote: show with default settings"))
7085 ((= more 2)
7086 (show-entry)
7087 (show-children)
7088 (save-excursion
7089 (org-back-to-heading)
7090 (run-hook-with-args 'org-cycle-hook 'children))
7091 (message "Remote: CHILDREN"))
7092 ((= more 3)
7093 (show-subtree)
7094 (save-excursion
7095 (org-back-to-heading)
7096 (run-hook-with-args 'org-cycle-hook 'subtree))
7097 (message "Remote: SUBTREE"))
7098 ((= more 4)
7099 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7100 (org-drawer-regexp
7101 (concat "^[ \t]*:\\("
7102 (mapconcat 'regexp-quote org-drawers "\\|")
7103 "\\):[ \t]*$")))
7104 (show-subtree)
7105 (save-excursion
7106 (org-back-to-heading)
7107 (org-cycle-hide-drawers 'subtree)))
7108 (message "Remote: SUBTREE AND LOGBOOK"))
7109 ((> more 4)
7110 (show-subtree)
7111 (message "Remote: SUBTREE AND ALL DRAWERS")))
7112 (select-window win)))
7114 (defun org-recenter-heading (n)
7115 (save-excursion
7116 (org-back-to-heading)
7117 (recenter n)))
7119 (defvar org-agenda-cycle-counter nil)
7120 (defun org-agenda-cycle-show (&optional n)
7121 "Show the current entry in another window, with default settings.
7122 Default settings are taken from `org-show-hierarchy-above' and siblings.
7123 When use repeatedly in immediate succession, the remote entry will cycle
7124 through visibility
7126 children -> subtree -> folded
7128 When called with a numeric prefix arg, that arg will be passed through to
7129 `org-agenda-show-1'. For the interpretation of that argument, see the
7130 docstring of `org-agenda-show-1'."
7131 (interactive "P")
7132 (if (integerp n)
7133 (setq org-agenda-cycle-counter n)
7134 (if (not (eq last-command this-command))
7135 (setq org-agenda-cycle-counter 1)
7136 (if (equal org-agenda-cycle-counter 0)
7137 (setq org-agenda-cycle-counter 2)
7138 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7139 (if (> org-agenda-cycle-counter 3)
7140 (setq org-agenda-cycle-counter 0)))))
7141 (org-agenda-show-1 org-agenda-cycle-counter))
7143 (defun org-agenda-recenter (arg)
7144 "Display the Org-mode file which contains the item at point and recenter."
7145 (interactive "P")
7146 (let ((win (selected-window)))
7147 (org-agenda-goto t)
7148 (recenter arg)
7149 (select-window win)))
7151 (defun org-agenda-show-mouse (ev)
7152 "Display the Org-mode file which contains the item at the mouse click."
7153 (interactive "e")
7154 (mouse-set-point ev)
7155 (org-agenda-show))
7157 (defun org-agenda-check-no-diary ()
7158 "Check if the entry is a diary link and abort if yes."
7159 (if (org-get-at-bol 'org-agenda-diary-link)
7160 (org-agenda-error)))
7162 (defun org-agenda-error ()
7163 (error "Command not allowed in this line"))
7165 (defun org-agenda-tree-to-indirect-buffer ()
7166 "Show the subtree corresponding to the current entry in an indirect buffer.
7167 This calls the command `org-tree-to-indirect-buffer' from the original
7168 Org-mode buffer.
7169 With numerical prefix arg ARG, go up to this level and then take that tree.
7170 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7171 use the dedicated frame)."
7172 (interactive)
7173 (org-agenda-check-no-diary)
7174 (let* ((marker (or (org-get-at-bol 'org-marker)
7175 (org-agenda-error)))
7176 (buffer (marker-buffer marker))
7177 (pos (marker-position marker)))
7178 (with-current-buffer buffer
7179 (save-excursion
7180 (goto-char pos)
7181 (call-interactively 'org-tree-to-indirect-buffer)))))
7183 (defvar org-last-heading-marker (make-marker)
7184 "Marker pointing to the headline that last changed its TODO state
7185 by a remote command from the agenda.")
7187 (defun org-agenda-todo-nextset ()
7188 "Switch TODO entry to next sequence."
7189 (interactive)
7190 (org-agenda-todo 'nextset))
7192 (defun org-agenda-todo-previousset ()
7193 "Switch TODO entry to previous sequence."
7194 (interactive)
7195 (org-agenda-todo 'previousset))
7197 (defun org-agenda-todo (&optional arg)
7198 "Cycle TODO state of line at point, also in Org-mode file.
7199 This changes the line at point, all other lines in the agenda referring to
7200 the same tree node, and the headline of the tree node in the Org-mode file."
7201 (interactive "P")
7202 (org-agenda-check-no-diary)
7203 (let* ((col (current-column))
7204 (marker (or (org-get-at-bol 'org-marker)
7205 (org-agenda-error)))
7206 (buffer (marker-buffer marker))
7207 (pos (marker-position marker))
7208 (hdmarker (org-get-at-bol 'org-hd-marker))
7209 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7210 (inhibit-read-only t)
7211 org-agenda-headline-snapshot-before-repeat newhead just-one)
7212 (org-with-remote-undo buffer
7213 (with-current-buffer buffer
7214 (widen)
7215 (goto-char pos)
7216 (org-show-context 'agenda)
7217 (save-excursion
7218 (and (outline-next-heading)
7219 (org-flag-heading nil))) ; show the next heading
7220 (let ((current-prefix-arg arg))
7221 (call-interactively 'org-todo))
7222 (and (bolp) (forward-char 1))
7223 (setq newhead (org-get-heading))
7224 (when (and (org-bound-and-true-p
7225 org-agenda-headline-snapshot-before-repeat)
7226 (not (equal org-agenda-headline-snapshot-before-repeat
7227 newhead))
7228 todayp)
7229 (setq newhead org-agenda-headline-snapshot-before-repeat
7230 just-one t))
7231 (save-excursion
7232 (org-back-to-heading)
7233 (move-marker org-last-heading-marker (point))))
7234 (beginning-of-line 1)
7235 (save-excursion
7236 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7237 (org-move-to-column col))))
7239 (defun org-agenda-add-note (&optional arg)
7240 "Add a time-stamped note to the entry at point."
7241 (interactive "P")
7242 (org-agenda-check-no-diary)
7243 (let* ((marker (or (org-get-at-bol 'org-marker)
7244 (org-agenda-error)))
7245 (buffer (marker-buffer marker))
7246 (pos (marker-position marker))
7247 (hdmarker (org-get-at-bol 'org-hd-marker))
7248 (inhibit-read-only t))
7249 (with-current-buffer buffer
7250 (widen)
7251 (goto-char pos)
7252 (org-show-context 'agenda)
7253 (save-excursion
7254 (and (outline-next-heading)
7255 (org-flag-heading nil))) ; show the next heading
7256 (org-add-note))))
7258 (defun org-agenda-change-all-lines (newhead hdmarker
7259 &optional fixface just-this)
7260 "Change all lines in the agenda buffer which match HDMARKER.
7261 The new content of the line will be NEWHEAD (as modified by
7262 `org-format-agenda-item'). HDMARKER is checked with
7263 `equal' against all `org-hd-marker' text properties in the file.
7264 If FIXFACE is non-nil, the face of each item is modified according to
7265 the new TODO state.
7266 If JUST-THIS is non-nil, change just the current line, not all.
7267 If FORCE-TAGS is non nil, the car of it returns the new tags."
7268 (let* ((inhibit-read-only t)
7269 (line (org-current-line))
7270 (thetags (with-current-buffer (marker-buffer hdmarker)
7271 (save-excursion (save-restriction (widen)
7272 (goto-char hdmarker)
7273 (org-get-tags-at)))))
7274 props m pl undone-face done-face finish new dotime cat tags)
7275 (save-excursion
7276 (goto-char (point-max))
7277 (beginning-of-line 1)
7278 (while (not finish)
7279 (setq finish (bobp))
7280 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7281 (or (not just-this) (= (org-current-line) line))
7282 (equal m hdmarker))
7283 (setq props (text-properties-at (point))
7284 dotime (org-get-at-bol 'dotime)
7285 cat (org-get-at-bol 'org-category)
7286 tags thetags
7288 (let ((org-prefix-format-compiled
7289 (or (get-text-property (point) 'format)
7290 org-prefix-format-compiled)))
7291 (with-current-buffer (marker-buffer hdmarker)
7292 (save-excursion
7293 (save-restriction
7294 (widen)
7295 (org-format-agenda-item (org-get-at-bol 'extra)
7296 newhead cat tags dotime)))))
7297 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7298 undone-face (org-get-at-bol 'undone-face)
7299 done-face (org-get-at-bol 'done-face))
7300 (beginning-of-line 1)
7301 (cond
7302 ((equal new "")
7303 (and (looking-at ".*\n?") (replace-match "")))
7304 ((looking-at ".*")
7305 (replace-match new t t)
7306 (beginning-of-line 1)
7307 (add-text-properties (point-at-bol) (point-at-eol) props)
7308 (when fixface
7309 (add-text-properties
7310 (point-at-bol) (point-at-eol)
7311 (list 'face
7312 (if org-last-todo-state-is-todo
7313 undone-face done-face))))
7314 (org-agenda-highlight-todo 'line)
7315 (beginning-of-line 1))
7316 (t (error "Line update did not work"))))
7317 (beginning-of-line 0)))
7318 (org-finalize-agenda)))
7320 (defun org-agenda-align-tags (&optional line)
7321 "Align all tags in agenda items to `org-agenda-tags-column'."
7322 (let ((inhibit-read-only t) l c)
7323 (save-excursion
7324 (goto-char (if line (point-at-bol) (point-min)))
7325 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7326 (if line (point-at-eol) nil) t)
7327 (add-text-properties
7328 (match-beginning 2) (match-end 2)
7329 (list 'face (delq nil (let ((prop (get-text-property
7330 (match-beginning 2) 'face)))
7331 (or (listp prop) (setq prop (list prop)))
7332 (if (memq 'org-tag prop)
7333 prop
7334 (cons 'org-tag prop))))))
7335 (setq l (- (match-end 2) (match-beginning 2))
7336 c (if (< org-agenda-tags-column 0)
7337 (- (abs org-agenda-tags-column) l)
7338 org-agenda-tags-column))
7339 (delete-region (match-beginning 1) (match-end 1))
7340 (goto-char (match-beginning 1))
7341 (insert (org-add-props
7342 (make-string (max 1 (- c (current-column))) ?\ )
7343 (plist-put (copy-sequence (text-properties-at (point)))
7344 'face nil))))
7345 (goto-char (point-min))
7346 (org-font-lock-add-tag-faces (point-max)))))
7348 (defun org-agenda-priority-up ()
7349 "Increase the priority of line at point, also in Org-mode file."
7350 (interactive)
7351 (org-agenda-priority 'up))
7353 (defun org-agenda-priority-down ()
7354 "Decrease the priority of line at point, also in Org-mode file."
7355 (interactive)
7356 (org-agenda-priority 'down))
7358 (defun org-agenda-priority (&optional force-direction)
7359 "Set the priority of line at point, also in Org-mode file.
7360 This changes the line at point, all other lines in the agenda referring to
7361 the same tree node, and the headline of the tree node in the Org-mode file."
7362 (interactive)
7363 (unless org-enable-priority-commands
7364 (error "Priority commands are disabled"))
7365 (org-agenda-check-no-diary)
7366 (let* ((marker (or (org-get-at-bol 'org-marker)
7367 (org-agenda-error)))
7368 (hdmarker (org-get-at-bol 'org-hd-marker))
7369 (buffer (marker-buffer hdmarker))
7370 (pos (marker-position hdmarker))
7371 (inhibit-read-only t)
7372 newhead)
7373 (org-with-remote-undo buffer
7374 (with-current-buffer buffer
7375 (widen)
7376 (goto-char pos)
7377 (org-show-context 'agenda)
7378 (save-excursion
7379 (and (outline-next-heading)
7380 (org-flag-heading nil))) ; show the next heading
7381 (funcall 'org-priority force-direction)
7382 (end-of-line 1)
7383 (setq newhead (org-get-heading)))
7384 (org-agenda-change-all-lines newhead hdmarker)
7385 (beginning-of-line 1))))
7387 ;; FIXME: should fix the tags property of the agenda line.
7388 (defun org-agenda-set-tags (&optional tag onoff)
7389 "Set tags for the current headline."
7390 (interactive)
7391 (org-agenda-check-no-diary)
7392 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7393 (call-interactively 'org-change-tag-in-region)
7394 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7395 (org-agenda-error)))
7396 (buffer (marker-buffer hdmarker))
7397 (pos (marker-position hdmarker))
7398 (inhibit-read-only t)
7399 newhead)
7400 (org-with-remote-undo buffer
7401 (with-current-buffer buffer
7402 (widen)
7403 (goto-char pos)
7404 (save-excursion
7405 (org-show-context 'agenda))
7406 (save-excursion
7407 (and (outline-next-heading)
7408 (org-flag-heading nil))) ; show the next heading
7409 (goto-char pos)
7410 (if tag
7411 (org-toggle-tag tag onoff)
7412 (call-interactively 'org-set-tags))
7413 (end-of-line 1)
7414 (setq newhead (org-get-heading)))
7415 (org-agenda-change-all-lines newhead hdmarker)
7416 (beginning-of-line 1)))))
7418 (defun org-agenda-set-property ()
7419 "Set a property for the current headline."
7420 (interactive)
7421 (org-agenda-check-no-diary)
7422 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7423 (org-agenda-error)))
7424 (buffer (marker-buffer hdmarker))
7425 (pos (marker-position hdmarker))
7426 (inhibit-read-only t)
7427 newhead)
7428 (org-with-remote-undo buffer
7429 (with-current-buffer buffer
7430 (widen)
7431 (goto-char pos)
7432 (save-excursion
7433 (org-show-context 'agenda))
7434 (save-excursion
7435 (and (outline-next-heading)
7436 (org-flag-heading nil))) ; show the next heading
7437 (goto-char pos)
7438 (call-interactively 'org-set-property)))))
7440 (defun org-agenda-set-effort ()
7441 "Set the effort property for the current headline."
7442 (interactive)
7443 (org-agenda-check-no-diary)
7444 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7445 (org-agenda-error)))
7446 (buffer (marker-buffer hdmarker))
7447 (pos (marker-position hdmarker))
7448 (inhibit-read-only t)
7449 newhead)
7450 (org-with-remote-undo buffer
7451 (with-current-buffer buffer
7452 (widen)
7453 (goto-char pos)
7454 (save-excursion
7455 (org-show-context 'agenda))
7456 (save-excursion
7457 (and (outline-next-heading)
7458 (org-flag-heading nil))) ; show the next heading
7459 (goto-char pos)
7460 (call-interactively 'org-set-effort)
7461 (end-of-line 1)
7462 (setq newhead (org-get-heading)))
7463 (org-agenda-change-all-lines newhead hdmarker))))
7465 (defun org-agenda-toggle-archive-tag ()
7466 "Toggle the archive tag for the current entry."
7467 (interactive)
7468 (org-agenda-check-no-diary)
7469 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7470 (org-agenda-error)))
7471 (buffer (marker-buffer hdmarker))
7472 (pos (marker-position hdmarker))
7473 (inhibit-read-only t)
7474 newhead)
7475 (org-with-remote-undo buffer
7476 (with-current-buffer buffer
7477 (widen)
7478 (goto-char pos)
7479 (org-show-context 'agenda)
7480 (save-excursion
7481 (and (outline-next-heading)
7482 (org-flag-heading nil))) ; show the next heading
7483 (call-interactively 'org-toggle-archive-tag)
7484 (end-of-line 1)
7485 (setq newhead (org-get-heading)))
7486 (org-agenda-change-all-lines newhead hdmarker)
7487 (beginning-of-line 1))))
7489 (defun org-agenda-do-date-later (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-later-minutes)
7496 (org-agenda-date-later-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-later-hours)
7501 (org-agenda-date-later-hours 1))
7503 (org-agenda-date-later (prefix-numeric-value arg)))))
7505 (defun org-agenda-do-date-earlier (arg)
7506 (interactive "P")
7507 (cond
7508 ((or (equal arg '(16))
7509 (memq last-command
7510 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7511 (setq this-command 'org-agenda-date-earlier-minutes)
7512 (org-agenda-date-earlier-minutes 1))
7513 ((or (equal arg '(4))
7514 (memq last-command
7515 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7516 (setq this-command 'org-agenda-date-earlier-hours)
7517 (org-agenda-date-earlier-hours 1))
7519 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7521 (defun org-agenda-date-later (arg &optional what)
7522 "Change the date of this item to ARG day(s) later."
7523 (interactive "p")
7524 (org-agenda-check-type t 'agenda 'timeline)
7525 (org-agenda-check-no-diary)
7526 (let* ((marker (or (org-get-at-bol 'org-marker)
7527 (org-agenda-error)))
7528 (buffer (marker-buffer marker))
7529 (pos (marker-position marker)))
7530 (org-with-remote-undo buffer
7531 (with-current-buffer buffer
7532 (widen)
7533 (goto-char pos)
7534 (if (not (org-at-timestamp-p))
7535 (error "Cannot find time stamp"))
7536 (org-timestamp-change arg (or what 'day)))
7537 (org-agenda-show-new-time marker org-last-changed-timestamp))
7538 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7540 (defun org-agenda-date-earlier (arg &optional what)
7541 "Change the date of this item to ARG day(s) earlier."
7542 (interactive "p")
7543 (org-agenda-date-later (- arg) what))
7545 (defun org-agenda-date-later-minutes (arg)
7546 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7547 (interactive "p")
7548 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7549 (org-agenda-date-later arg 'minute))
7551 (defun org-agenda-date-earlier-minutes (arg)
7552 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7553 (interactive "p")
7554 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7555 (org-agenda-date-earlier arg 'minute))
7557 (defun org-agenda-date-later-hours (arg)
7558 "Change the time of this item, in hour steps."
7559 (interactive "p")
7560 (org-agenda-date-later arg 'hour))
7562 (defun org-agenda-date-earlier-hours (arg)
7563 "Change the time of this item, in hour steps."
7564 (interactive "p")
7565 (org-agenda-date-earlier arg 'hour))
7567 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7568 "Show new date stamp via text properties."
7569 ;; We use text properties to make this undoable
7570 (let ((inhibit-read-only t)
7571 (buffer-invisibility-spec))
7572 (setq stamp (concat " " prefix " => " stamp))
7573 (save-excursion
7574 (goto-char (point-max))
7575 (while (not (bobp))
7576 (when (equal marker (org-get-at-bol 'org-marker))
7577 (org-move-to-column (- (window-width) (length stamp)) t)
7578 (org-agenda-fix-tags-filter-overlays-at (point))
7579 (if (featurep 'xemacs)
7580 ;; Use `duplicable' property to trigger undo recording
7581 (let ((ex (make-extent nil nil))
7582 (gl (make-glyph stamp)))
7583 (set-glyph-face gl 'secondary-selection)
7584 (set-extent-properties
7585 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7586 (insert-extent ex (1- (point)) (point-at-eol)))
7587 (add-text-properties
7588 (1- (point)) (point-at-eol)
7589 (list 'display (org-add-props stamp nil
7590 'face 'secondary-selection))))
7591 (beginning-of-line 1))
7592 (beginning-of-line 0)))))
7594 (defun org-agenda-date-prompt (arg)
7595 "Change the date of this item. Date is prompted for, with default today.
7596 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7597 be used to request time specification in the time stamp."
7598 (interactive "P")
7599 (org-agenda-check-type t 'agenda 'timeline)
7600 (org-agenda-check-no-diary)
7601 (let* ((marker (or (org-get-at-bol 'org-marker)
7602 (org-agenda-error)))
7603 (buffer (marker-buffer marker))
7604 (pos (marker-position marker)))
7605 (org-with-remote-undo buffer
7606 (with-current-buffer buffer
7607 (widen)
7608 (goto-char pos)
7609 (if (not (org-at-timestamp-p t))
7610 (error "Cannot find time stamp"))
7611 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7612 (org-agenda-show-new-time marker org-last-changed-timestamp))
7613 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7615 (defun org-agenda-schedule (arg &optional time)
7616 "Schedule the item at point.
7617 ARG is passed through to `org-schedule'."
7618 (interactive "P")
7619 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7620 (org-agenda-check-no-diary)
7621 (let* ((marker (or (org-get-at-bol 'org-marker)
7622 (org-agenda-error)))
7623 (type (marker-insertion-type marker))
7624 (buffer (marker-buffer marker))
7625 (pos (marker-position marker))
7626 (org-insert-labeled-timestamps-at-point nil)
7628 (set-marker-insertion-type marker t)
7629 (org-with-remote-undo buffer
7630 (with-current-buffer buffer
7631 (widen)
7632 (goto-char pos)
7633 (setq ts (org-schedule arg time)))
7634 (org-agenda-show-new-time marker ts "S"))
7635 (message "Item scheduled for %s" ts)))
7637 (defun org-agenda-deadline (arg &optional time)
7638 "Schedule the item at point.
7639 ARG is passed through to `org-deadline'."
7640 (interactive "P")
7641 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7642 (org-agenda-check-no-diary)
7643 (let* ((marker (or (org-get-at-bol 'org-marker)
7644 (org-agenda-error)))
7645 (buffer (marker-buffer marker))
7646 (pos (marker-position marker))
7647 (org-insert-labeled-timestamps-at-point nil)
7649 (org-with-remote-undo buffer
7650 (with-current-buffer buffer
7651 (widen)
7652 (goto-char pos)
7653 (setq ts (org-deadline arg time)))
7654 (org-agenda-show-new-time marker ts "D"))
7655 (message "Deadline for this item set to %s" ts)))
7657 (defun org-agenda-action ()
7658 "Select entry for agenda action, or execute an agenda action.
7659 This command prompts for another letter. Valid inputs are:
7661 m Mark the entry at point for an agenda action
7662 s Schedule the marked entry to the date at the cursor
7663 d Set the deadline of the marked entry to the date at the cursor
7664 r Call `org-remember' with cursor date as the default date
7665 c Call `org-capture' with cursor date as the default date
7666 SPC Show marked entry in other window
7667 TAB Visit marked entry in other window
7669 The cursor may be at a date in the calendar, or in the Org agenda."
7670 (interactive)
7671 (let (ans)
7672 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7673 (setq ans (read-char-exclusive))
7674 (cond
7675 ((equal ans ?m)
7676 ;; Mark this entry
7677 (if (eq major-mode 'org-agenda-mode)
7678 (let ((m (or (org-get-at-bol 'org-hd-marker)
7679 (org-get-at-bol 'org-marker))))
7680 (if m
7681 (progn
7682 (move-marker org-agenda-action-marker
7683 (marker-position m) (marker-buffer m))
7684 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7685 (error "Don't know which entry to mark")))
7686 (error "This command works only in the agenda")))
7687 ((equal ans ?s)
7688 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7689 ((equal ans ?d)
7690 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7691 ((equal ans ?r)
7692 (org-agenda-do-action '(org-remember) t))
7693 ((equal ans ?c)
7694 (org-agenda-do-action '(org-capture) t))
7695 ((equal ans ?\ )
7696 (let ((cw (selected-window)))
7697 (org-switch-to-buffer-other-window
7698 (marker-buffer org-agenda-action-marker))
7699 (goto-char org-agenda-action-marker)
7700 (org-show-context 'agenda)
7701 (select-window cw)))
7702 ((equal ans ?\C-i)
7703 (org-switch-to-buffer-other-window
7704 (marker-buffer org-agenda-action-marker))
7705 (goto-char org-agenda-action-marker)
7706 (org-show-context 'agenda))
7707 (t (error "Invalid agenda action %c" ans)))))
7709 (defun org-agenda-do-action (form &optional current-buffer)
7710 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7711 (let ((org-overriding-default-time (org-get-cursor-date)))
7712 (if current-buffer
7713 (eval form)
7714 (if (not (marker-buffer org-agenda-action-marker))
7715 (error "No entry has been selected for agenda action")
7716 (with-current-buffer (marker-buffer org-agenda-action-marker)
7717 (save-excursion
7718 (save-restriction
7719 (widen)
7720 (goto-char org-agenda-action-marker)
7721 (eval form))))))))
7723 (defun org-agenda-clock-in (&optional arg)
7724 "Start the clock on the currently selected item."
7725 (interactive "P")
7726 (org-agenda-check-no-diary)
7727 (if (equal arg '(4))
7728 (org-clock-in arg)
7729 (let* ((marker (or (org-get-at-bol 'org-marker)
7730 (org-agenda-error)))
7731 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7732 marker))
7733 (pos (marker-position marker))
7734 newhead)
7735 (org-with-remote-undo (marker-buffer marker)
7736 (with-current-buffer (marker-buffer marker)
7737 (widen)
7738 (goto-char pos)
7739 (org-show-context 'agenda)
7740 (org-show-entry)
7741 (org-cycle-hide-drawers 'children)
7742 (org-clock-in arg)
7743 (setq newhead (org-get-heading)))
7744 (org-agenda-change-all-lines newhead hdmarker)))))
7746 (defun org-agenda-clock-out ()
7747 "Stop the currently running clock."
7748 (interactive)
7749 (unless (marker-buffer org-clock-marker)
7750 (error "No running clock"))
7751 (let ((marker (make-marker)) newhead)
7752 (org-with-remote-undo (marker-buffer org-clock-marker)
7753 (with-current-buffer (marker-buffer org-clock-marker)
7754 (save-excursion
7755 (save-restriction
7756 (widen)
7757 (goto-char org-clock-marker)
7758 (org-back-to-heading t)
7759 (move-marker marker (point))
7760 (org-clock-out)
7761 (setq newhead (org-get-heading))))))
7762 (org-agenda-change-all-lines newhead marker)
7763 (move-marker marker nil)))
7765 (defun org-agenda-clock-cancel (&optional arg)
7766 "Cancel the currently running clock."
7767 (interactive "P")
7768 (unless (marker-buffer org-clock-marker)
7769 (error "No running clock"))
7770 (org-with-remote-undo (marker-buffer org-clock-marker)
7771 (org-clock-cancel)))
7773 (defun org-agenda-clock-goto ()
7774 "Jump to the currently clocked in task within the agenda.
7775 If the currently clocked in task is not listed in the agenda
7776 buffer, display it in another window."
7777 (interactive)
7778 (let (pos)
7779 (mapc (lambda (o)
7780 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7781 (setq pos (overlay-start o))))
7782 (overlays-in (point-min) (point-max)))
7783 (cond (pos (goto-char pos))
7784 ;; If the currently clocked entry is not in the agenda
7785 ;; buffer, we visit it in another window:
7786 (org-clock-current-task
7787 (org-switch-to-buffer-other-window (org-clock-goto)))
7788 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7790 (defun org-agenda-diary-entry-in-org-file ()
7791 "Make a diary entry in the file `org-agenda-diary-file'."
7792 (let (d1 d2 char (text "") dp1 dp2)
7793 (if (equal (buffer-name) "*Calendar*")
7794 (setq d1 (calendar-cursor-to-date t)
7795 d2 (car calendar-mark-ring))
7796 (setq dp1 (get-text-property (point-at-bol) 'day))
7797 (unless dp1 (error "No date defined in current line"))
7798 (setq d1 (calendar-gregorian-from-absolute dp1)
7799 d2 (and (ignore-errors (mark))
7800 (save-excursion
7801 (goto-char (mark))
7802 (setq dp2 (get-text-property (point-at-bol) 'day)))
7803 (calendar-gregorian-from-absolute dp2))))
7804 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7805 (setq char (read-char-exclusive))
7806 (cond
7807 ((equal char ?d)
7808 (setq text (read-string "Day entry: "))
7809 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7810 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7811 ((equal char ?a)
7812 (setq d1 (list (car d1) (nth 1 d1)
7813 (read-number (format "Reference year [%d]: " (nth 2 d1))
7814 (nth 2 d1))))
7815 (setq text (read-string "Anniversary (use %d to show years): "))
7816 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7817 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7818 ((equal char ?b)
7819 (setq text (read-string "Block entry: "))
7820 (unless (and d1 d2 (not (equal d1 d2)))
7821 (error "No block of days selected"))
7822 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7823 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7824 ((equal char ?j)
7825 (org-switch-to-buffer-other-window
7826 (find-file-noselect org-agenda-diary-file))
7827 (require 'org-datetree)
7828 (org-datetree-find-date-create d1)
7829 (org-reveal t))
7830 (t (error "Invalid selection character `%c'" char)))))
7832 (defcustom org-agenda-insert-diary-strategy 'date-tree
7833 "Where in `org-agenda-diary-file' should new entries be added?
7834 Valid values:
7836 date-tree in the date tree, as child of the date
7837 top-level as top-level entries at the end of the file."
7838 :group 'org-agenda
7839 :type '(choice
7840 (const :tag "in a date tree" date-tree)
7841 (const :tag "as top level at end of file" top-level)))
7843 (defcustom org-agenda-insert-diary-extract-time nil
7844 "Non-nil means extract any time specification from the diary entry."
7845 :group 'org-agenda
7846 :type 'boolean)
7848 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7849 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7850 If TEXT is not empty, it will become the headline of the new entry, and
7851 the resulting entry will not be shown. When TEXT is empty, switch to
7852 `org-agenda-diary-file' and let the user finish the entry there."
7853 (let ((cw (current-window-configuration)))
7854 (org-switch-to-buffer-other-window
7855 (find-file-noselect org-agenda-diary-file))
7856 (widen)
7857 (goto-char (point-min))
7858 (cond
7859 ((eq type 'anniversary)
7860 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7861 (progn
7862 (or (org-on-heading-p t)
7863 (progn
7864 (outline-next-heading)
7865 (insert "* Anniversaries\n\n")
7866 (beginning-of-line -1)))))
7867 (outline-next-heading)
7868 (org-back-over-empty-lines)
7869 (backward-char 1)
7870 (insert "\n")
7871 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7872 (nth 2 d1) (car d1) (nth 1 d1) text)))
7873 ((eq type 'day)
7874 (let ((org-prefix-has-time t)
7875 (org-agenda-time-leading-zero t)
7876 fmt time time2)
7877 (if org-agenda-insert-diary-extract-time
7878 ;; Use org-format-agenda-item to parse text for a time-range and
7879 ;; remove it. FIXME: This is a hack, we should refactor
7880 ;; that function to make time extraction available separately
7881 (setq fmt (org-format-agenda-item nil text nil nil t)
7882 time (get-text-property 0 'time fmt)
7883 time2 (if (> (length time) 0)
7884 ;; split-string removes trailing ...... if
7885 ;; no end time given. First space
7886 ;; separates time from date.
7887 (concat " " (car (split-string time "\\.")))
7888 nil)
7889 text (get-text-property 0 'txt fmt)))
7890 (if (eq org-agenda-insert-diary-strategy 'top-level)
7891 (org-agenda-insert-diary-as-top-level text)
7892 (require 'org-datetree)
7893 (org-datetree-find-date-create d1)
7894 (org-agenda-insert-diary-make-new-entry text))
7895 (org-insert-time-stamp (org-time-from-absolute
7896 (calendar-absolute-from-gregorian d1))
7897 nil nil nil nil time2))
7898 (end-of-line 0))
7899 ((eq type 'block)
7900 (if (> (calendar-absolute-from-gregorian d1)
7901 (calendar-absolute-from-gregorian d2))
7902 (setq d1 (prog1 d2 (setq d2 d1))))
7903 (if (eq org-agenda-insert-diary-strategy 'top-level)
7904 (org-agenda-insert-diary-as-top-level text)
7905 (require 'org-datetree)
7906 (org-datetree-find-date-create d1)
7907 (org-agenda-insert-diary-make-new-entry text))
7908 (org-insert-time-stamp (org-time-from-absolute
7909 (calendar-absolute-from-gregorian d1)))
7910 (insert "--")
7911 (org-insert-time-stamp (org-time-from-absolute
7912 (calendar-absolute-from-gregorian d2)))
7913 (end-of-line 0)))
7914 (if (string-match "\\S-" text)
7915 (progn
7916 (set-window-configuration cw)
7917 (message "%s entry added to %s"
7918 (capitalize (symbol-name type))
7919 (abbreviate-file-name org-agenda-diary-file)))
7920 (org-reveal t)
7921 (message "Please finish entry here"))))
7923 (defun org-agenda-insert-diary-as-top-level (text)
7924 "Make new entry as a top-level entry at the end of the file.
7925 Add TEXT as headline, and position the cursor in the second line so that
7926 a timestamp can be added there."
7927 (widen)
7928 (goto-char (point-max))
7929 (or (bolp) (insert "\n"))
7930 (insert "* " text "\n")
7931 (if org-adapt-indentation (org-indent-to-column 2)))
7933 (defun org-agenda-insert-diary-make-new-entry (text)
7934 "Make new entry as last child of current entry.
7935 Add TEXT as headline, and position the cursor in the second line so that
7936 a timestamp can be added there."
7937 (let ((org-show-following-heading t)
7938 (org-show-siblings t)
7939 (org-show-hierarchy-above t)
7940 (org-show-entry-below t)
7941 col)
7942 (outline-next-heading)
7943 (org-back-over-empty-lines)
7944 (or (looking-at "[ \t]*$")
7945 (progn (insert "\n") (backward-char 1)))
7946 (org-insert-heading nil t)
7947 (org-do-demote)
7948 (setq col (current-column))
7949 (insert text "\n")
7950 (if org-adapt-indentation (org-indent-to-column col))
7951 (let ((org-show-following-heading t)
7952 (org-show-siblings t)
7953 (org-show-hierarchy-above t)
7954 (org-show-entry-below t))
7955 (org-show-context))))
7957 (defun org-agenda-diary-entry ()
7958 "Make a diary entry, like the `i' command from the calendar.
7959 All the standard commands work: block, weekly etc.
7960 When `org-agenda-diary-file' points to a file,
7961 `org-agenda-diary-entry-in-org-file' is called instead to create
7962 entries in that Org-mode file."
7963 (interactive)
7964 (org-agenda-check-type t 'agenda 'timeline)
7965 (if (not (eq org-agenda-diary-file 'diary-file))
7966 (org-agenda-diary-entry-in-org-file)
7967 (require 'diary-lib)
7968 (let* ((char (progn
7969 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7970 (read-char-exclusive)))
7971 (cmd (cdr (assoc char
7972 '((?d . insert-diary-entry)
7973 (?w . insert-weekly-diary-entry)
7974 (?m . insert-monthly-diary-entry)
7975 (?y . insert-yearly-diary-entry)
7976 (?a . insert-anniversary-diary-entry)
7977 (?b . insert-block-diary-entry)
7978 (?c . insert-cyclic-diary-entry)))))
7979 (oldf (symbol-function 'calendar-cursor-to-date))
7980 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7981 (point (point))
7982 (mark (or (mark t) (point))))
7983 (unless cmd
7984 (error "No command associated with <%c>" char))
7985 (unless (and (get-text-property point 'day)
7986 (or (not (equal ?b char))
7987 (get-text-property mark 'day)))
7988 (error "Don't know which date to use for diary entry"))
7989 ;; We implement this by hacking the `calendar-cursor-to-date' function
7990 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7991 (let ((calendar-mark-ring
7992 (list (calendar-gregorian-from-absolute
7993 (or (get-text-property mark 'day)
7994 (get-text-property point 'day))))))
7995 (unwind-protect
7996 (progn
7997 (fset 'calendar-cursor-to-date
7998 (lambda (&optional error dummy)
7999 (calendar-gregorian-from-absolute
8000 (get-text-property point 'day))))
8001 (call-interactively cmd))
8002 (fset 'calendar-cursor-to-date oldf))))))
8004 (defun org-agenda-execute-calendar-command (cmd)
8005 "Execute a calendar command from the agenda, with the date associated to
8006 the cursor position."
8007 (org-agenda-check-type t 'agenda 'timeline)
8008 (require 'diary-lib)
8009 (unless (get-text-property (point) 'day)
8010 (error "Don't know which date to use for calendar command"))
8011 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8012 (point (point))
8013 (date (calendar-gregorian-from-absolute
8014 (get-text-property point 'day)))
8015 ;; the following 2 vars are needed in the calendar
8016 (displayed-month (car date))
8017 (displayed-year (nth 2 date)))
8018 (unwind-protect
8019 (progn
8020 (fset 'calendar-cursor-to-date
8021 (lambda (&optional error dummy)
8022 (calendar-gregorian-from-absolute
8023 (get-text-property point 'day))))
8024 (call-interactively cmd))
8025 (fset 'calendar-cursor-to-date oldf))))
8027 (defun org-agenda-phases-of-moon ()
8028 "Display the phases of the moon for the 3 months around the cursor date."
8029 (interactive)
8030 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8032 (defun org-agenda-holidays ()
8033 "Display the holidays for the 3 months around the cursor date."
8034 (interactive)
8035 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8037 (defvar calendar-longitude)
8038 (defvar calendar-latitude)
8039 (defvar calendar-location-name)
8041 (defun org-agenda-sunrise-sunset (arg)
8042 "Display sunrise and sunset for the cursor date.
8043 Latitude and longitude can be specified with the variables
8044 `calendar-latitude' and `calendar-longitude'. When called with prefix
8045 argument, latitude and longitude will be prompted for."
8046 (interactive "P")
8047 (require 'solar)
8048 (let ((calendar-longitude (if arg nil calendar-longitude))
8049 (calendar-latitude (if arg nil calendar-latitude))
8050 (calendar-location-name
8051 (if arg "the given coordinates" calendar-location-name)))
8052 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8054 (defun org-agenda-goto-calendar ()
8055 "Open the Emacs calendar with the date at the cursor."
8056 (interactive)
8057 (org-agenda-check-type t 'agenda 'timeline)
8058 (let* ((day (or (get-text-property (point) 'day)
8059 (error "Don't know which date to open in calendar")))
8060 (date (calendar-gregorian-from-absolute day))
8061 (calendar-move-hook nil)
8062 (calendar-view-holidays-initially-flag nil)
8063 (calendar-view-diary-initially-flag nil))
8064 (calendar)
8065 (calendar-goto-date date)))
8067 ;;;###autoload
8068 (defun org-calendar-goto-agenda ()
8069 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8070 This is a command that has to be installed in `calendar-mode-map'."
8071 (interactive)
8072 (org-agenda-list nil (calendar-absolute-from-gregorian
8073 (calendar-cursor-to-date))
8074 nil))
8076 (defun org-agenda-convert-date ()
8077 (interactive)
8078 (org-agenda-check-type t 'agenda 'timeline)
8079 (let ((day (get-text-property (point) 'day))
8080 date s)
8081 (unless day
8082 (error "Don't know which date to convert"))
8083 (setq date (calendar-gregorian-from-absolute day))
8084 (setq s (concat
8085 "Gregorian: " (calendar-date-string date) "\n"
8086 "ISO: " (calendar-iso-date-string date) "\n"
8087 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8088 "Julian: " (calendar-julian-date-string date) "\n"
8089 "Astron. JD: " (calendar-astro-date-string date)
8090 " (Julian date number at noon UTC)\n"
8091 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8092 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8093 "French: " (calendar-french-date-string date) "\n"
8094 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8095 "Mayan: " (calendar-mayan-date-string date) "\n"
8096 "Coptic: " (calendar-coptic-date-string date) "\n"
8097 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8098 "Persian: " (calendar-persian-date-string date) "\n"
8099 "Chinese: " (calendar-chinese-date-string date) "\n"))
8100 (with-output-to-temp-buffer "*Dates*"
8101 (princ s))
8102 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8104 ;;; Bulk commands
8106 (defvar org-agenda-bulk-marked-entries nil
8107 "List of markers that refer to marked entries in the agenda.")
8109 (defun org-agenda-bulk-marked-p ()
8110 (eq (get-char-property (point-at-bol) 'type)
8111 'org-marked-entry-overlay))
8113 (defun org-agenda-bulk-mark (&optional arg)
8114 "Mark the entry at point for future bulk action."
8115 (interactive "p")
8116 (dotimes (i (max arg 1))
8117 (unless (org-get-at-bol 'org-agenda-diary-link)
8118 (let* ((m (org-get-at-bol 'org-hd-marker))
8120 (unless (org-agenda-bulk-marked-p)
8121 (unless m (error "Nothing to mark at point"))
8122 (push m org-agenda-bulk-marked-entries)
8123 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8124 (org-overlay-display ov "> "
8125 (org-get-todo-face "TODO")
8126 'evaporate)
8127 (overlay-put ov 'type 'org-marked-entry-overlay))
8128 (beginning-of-line 2)
8129 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8130 (beginning-of-line 2))
8131 (message "%d entries marked for bulk action"
8132 (length org-agenda-bulk-marked-entries))))))
8134 (defun org-agenda-bulk-mark-regexp (regexp)
8135 "Mark entries match REGEXP."
8136 (interactive "sMark entries matching regexp: ")
8137 (let (entries-marked)
8138 (save-excursion
8139 (goto-char (point-min))
8140 (goto-char (next-single-property-change (point) 'txt))
8141 (while (re-search-forward regexp nil t)
8142 (when (string-match regexp (get-text-property (point) 'txt))
8143 (setq entries-marked (+ entries-marked 1))
8144 (call-interactively 'org-agenda-bulk-mark))))
8145 (if (not entries-marked)
8146 (message "No entry matching this regexp."))))
8148 (defun org-agenda-bulk-unmark ()
8149 "Unmark the entry at point for future bulk action."
8150 (interactive)
8151 (when (org-agenda-bulk-marked-p)
8152 (org-agenda-bulk-remove-overlays
8153 (point-at-bol) (+ 2 (point-at-bol)))
8154 (setq org-agenda-bulk-marked-entries
8155 (delete (org-get-at-bol 'org-hd-marker)
8156 org-agenda-bulk-marked-entries)))
8157 (beginning-of-line 2)
8158 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8159 (beginning-of-line 2))
8160 (message "%d entries marked for bulk action"
8161 (length org-agenda-bulk-marked-entries)))
8163 (defun org-agenda-bulk-toggle ()
8164 "Toggle marking the entry at point for bulk action."
8165 (interactive)
8166 (if (org-agenda-bulk-marked-p)
8167 (org-agenda-bulk-unmark)
8168 (org-agenda-bulk-mark)))
8170 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8171 "Remove the mark overlays between BEG and END in the agenda buffer.
8172 BEG and END default to the buffer limits.
8174 This only removes the overlays, it does not remove the markers
8175 from the list in `org-agenda-bulk-marked-entries'."
8176 (interactive)
8177 (mapc (lambda (ov)
8178 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8179 (delete-overlay ov)))
8180 (overlays-in (or beg (point-min)) (or end (point-max)))))
8182 (defun org-agenda-bulk-remove-all-marks ()
8183 "Remove all marks in the agenda buffer.
8184 This will remove the markers, and the overlays."
8185 (interactive)
8186 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8187 (setq org-agenda-bulk-marked-entries nil)
8188 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8190 (defun org-agenda-bulk-action (&optional arg)
8191 "Execute an remote-editing action on all marked entries.
8192 The prefix arg is passed through to the command if possible."
8193 (interactive "P")
8194 ;; Make sure we have markers, and only valid ones
8195 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8196 (mapc
8197 (lambda (m)
8198 (unless (and (markerp m)
8199 (marker-buffer m)
8200 (buffer-live-p (marker-buffer m))
8201 (marker-position m))
8202 (error "Marker %s for bulk command is invalid" m)))
8203 org-agenda-bulk-marked-entries)
8205 ;; Prompt for the bulk command
8206 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8207 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8208 (when org-agenda-bulk-custom-functions
8209 (concat " Custom: ["
8210 (mapconcat (lambda(f) (char-to-string (car f)))
8211 org-agenda-bulk-custom-functions "")
8212 "]"))))
8213 (let* ((action (read-char-exclusive))
8214 (org-log-refile (if org-log-refile 'time nil))
8215 (entries (reverse org-agenda-bulk-marked-entries))
8216 redo-at-end
8217 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8218 (cond
8219 ((equal action ?$)
8220 (setq cmd '(org-agenda-archive)))
8222 ((equal action ?A)
8223 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8225 ((member action '(?r ?w))
8226 (setq rfloc (org-refile-get-location
8227 "Refile to"
8228 (marker-buffer (car org-agenda-bulk-marked-entries))
8229 org-refile-allow-creating-parent-nodes))
8230 (if (nth 3 rfloc)
8231 (setcar (nthcdr 3 rfloc)
8232 (move-marker (make-marker) (nth 3 rfloc)
8233 (or (get-file-buffer (nth 1 rfloc))
8234 (find-buffer-visiting (nth 1 rfloc))
8235 (error "This should not happen")))))
8237 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8238 redo-at-end t))
8240 ((equal action ?t)
8241 (setq state (org-icompleting-read
8242 "Todo state: "
8243 (with-current-buffer (marker-buffer (car entries))
8244 (mapcar 'list org-todo-keywords-1))))
8245 (setq cmd `(let ((org-inhibit-blocking t)
8246 (org-inhibit-logging 'note))
8247 (org-agenda-todo ,state))))
8249 ((memq action '(?- ?+))
8250 (setq tag (org-icompleting-read
8251 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8252 (with-current-buffer (marker-buffer (car entries))
8253 (delq nil
8254 (mapcar (lambda (x)
8255 (if (stringp (car x)) x)) org-tag-alist)))))
8256 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8258 ((memq action '(?s ?d))
8259 (let* ((date (unless arg
8260 (org-read-date
8261 nil nil nil
8262 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8263 (ans (if arg nil org-read-date-final-answer))
8264 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8265 (setq cmd `(let* ((bound (fboundp 'read-string))
8266 (old (and bound (symbol-function 'read-string))))
8267 (unwind-protect
8268 (progn
8269 (fset 'read-string (lambda (&rest ignore) ,ans))
8270 (eval '(,c1 arg)))
8271 (if bound
8272 (fset 'read-string old)
8273 (fmakunbound 'read-string)))))))
8275 ((equal action ?S)
8276 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8277 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8278 (let ((days (read-number
8279 (format "Scatter tasks across how many %sdays: "
8280 (if arg "week" "")) 7)))
8281 (setq cmd
8282 `(let ((distance (1+ (random ,days))))
8283 (if arg
8284 (let ((dist distance)
8285 (day-of-week
8286 (calendar-day-of-week
8287 (calendar-gregorian-from-absolute (org-today)))))
8288 (dotimes (i (1+ dist))
8289 (while (member day-of-week org-agenda-weekend-days)
8290 (incf distance)
8291 (incf day-of-week)
8292 (if (= day-of-week 7)
8293 (setq day-of-week 0)))
8294 (incf day-of-week)
8295 (if (= day-of-week 7)
8296 (setq day-of-week 0)))))
8297 ;; silently fail when try to replan a sexp entry
8298 (condition-case nil
8299 (let* ((date (calendar-gregorian-from-absolute
8300 (+ (org-today) distance)))
8301 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8302 (nth 2 date))))
8303 (org-agenda-schedule nil time))
8304 (error nil)))))))
8306 ((assoc action org-agenda-bulk-custom-functions)
8307 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8308 redo-at-end t))
8310 ((equal action ?f)
8311 (setq cmd (list (intern
8312 (org-icompleting-read "Function: "
8313 obarray 'fboundp t nil nil)))))
8315 (t (error "Invalid bulk action")))
8317 ;; Sort the markers, to make sure that parents are handled before children
8318 (setq entries (sort entries
8319 (lambda (a b)
8320 (cond
8321 ((equal (marker-buffer a) (marker-buffer b))
8322 (< (marker-position a) (marker-position b)))
8324 (string< (buffer-name (marker-buffer a))
8325 (buffer-name (marker-buffer b))))))))
8327 ;; Now loop over all markers and apply cmd
8328 (while (setq e (pop entries))
8329 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8330 (if (not pos)
8331 (progn (message "Skipping removed entry at %s" e)
8332 (setq cntskip (1+ cntskip)))
8333 (goto-char pos)
8334 (eval cmd)
8335 (setq org-agenda-bulk-marked-entries
8336 (delete e org-agenda-bulk-marked-entries))
8337 (setq cnt (1+ cnt))))
8338 (setq org-agenda-bulk-marked-entries nil)
8339 (org-agenda-bulk-remove-all-marks)
8340 (when redo-at-end (org-agenda-redo))
8341 (message "Acted on %d entries%s"
8343 (if (= cntskip 0)
8345 (format ", skipped %d (disappeared before their turn)"
8346 cntskip)))))
8348 ;;; Flagging notes
8350 (defun org-agenda-show-the-flagging-note ()
8351 "Display the flagging note in the other window.
8352 When called a second time in direct sequence, offer to remove the FLAGGING
8353 tag and (if present) the flagging note."
8354 (interactive)
8355 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8356 (win (selected-window))
8357 note heading newhead)
8358 (unless hdmarker
8359 (error "No linked entry at point"))
8360 (if (and (eq this-command last-command)
8361 (y-or-n-p "Unflag and remove any flagging note? "))
8362 (progn
8363 (org-agenda-remove-flag hdmarker)
8364 (let ((win (get-buffer-window "*Flagging Note*")))
8365 (and win (delete-window win)))
8366 (message "Entry unflaged"))
8367 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8368 (unless note
8369 (error "No flagging note"))
8370 (org-kill-new note)
8371 (org-switch-to-buffer-other-window "*Flagging Note*")
8372 (erase-buffer)
8373 (insert note)
8374 (goto-char (point-min))
8375 (while (re-search-forward "\\\\n" nil t)
8376 (replace-match "\n" t t))
8377 (goto-char (point-min))
8378 (select-window win)
8379 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8381 (defun org-agenda-remove-flag (marker)
8382 "Remove the FLAGGED tag and any flagging note in the entry."
8383 (let (newhead)
8384 (org-with-point-at marker
8385 (org-toggle-tag "FLAGGED" 'off)
8386 (org-entry-delete nil "THEFLAGGINGNOTE")
8387 (setq newhead (org-get-heading)))
8388 (org-agenda-change-all-lines newhead marker)
8389 (message "Entry unflaged")))
8391 (defun org-agenda-get-any-marker (&optional pos)
8392 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8393 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8395 ;;; Appointment reminders
8397 (defvar appt-time-msg-list)
8399 ;;;###autoload
8400 (defun org-agenda-to-appt (&optional refresh filter)
8401 "Activate appointments found in `org-agenda-files'.
8402 With a \\[universal-argument] prefix, refresh the list of
8403 appointments.
8405 If FILTER is t, interactively prompt the user for a regular
8406 expression, and filter out entries that don't match it.
8408 If FILTER is a string, use this string as a regular expression
8409 for filtering entries out.
8411 FILTER can also be an alist with the car of each cell being
8412 either 'headline or 'category. For example:
8414 '((headline \"IMPORTANT\")
8415 (category \"Work\"))
8417 will only add headlines containing IMPORTANT or headlines
8418 belonging to the \"Work\" category."
8419 (interactive "P")
8420 (if refresh (setq appt-time-msg-list nil))
8421 (if (eq filter t)
8422 (setq filter (read-from-minibuffer "Regexp filter: ")))
8423 (let* ((cnt 0) ; count added events
8424 (org-agenda-new-buffers nil)
8425 (org-deadline-warning-days 0)
8426 ;; Do not use `org-today' here because appt only takes
8427 ;; time and without date as argument, so it may pass wrong
8428 ;; information otherwise
8429 (today (org-date-to-gregorian
8430 (time-to-days (current-time))))
8431 (org-agenda-restrict nil)
8432 (files (org-agenda-files 'unrestricted)) entries file)
8433 ;; Get all entries which may contain an appt
8434 (org-prepare-agenda-buffers files)
8435 (while (setq file (pop files))
8436 (setq entries
8437 (append entries
8438 (org-agenda-get-day-entries
8439 file today :timestamp :scheduled :deadline))))
8440 (setq entries (delq nil entries))
8441 ;; Map thru entries and find if we should filter them out
8442 (mapc
8443 (lambda(x)
8444 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8445 (cat (get-text-property 1 'org-category x))
8446 (tod (get-text-property 1 'time-of-day x))
8447 (ok (or (null filter)
8448 (and (stringp filter) (string-match filter evt))
8449 (and (listp filter)
8450 (or (string-match
8451 (cadr (assoc 'category filter)) cat)
8452 (string-match
8453 (cadr (assoc 'headline filter)) evt))))))
8454 ;; FIXME: Shall we remove text-properties for the appt text?
8455 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8456 (when (and ok tod)
8457 (setq tod (concat "00" (number-to-string tod))
8458 tod (when (string-match
8459 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8460 (concat (match-string 1 tod) ":"
8461 (match-string 2 tod))))
8462 (appt-add tod evt)
8463 (setq cnt (1+ cnt))))) entries)
8464 (org-release-buffers org-agenda-new-buffers)
8465 (if (eq cnt 0)
8466 (message "No event to add")
8467 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8469 (defun org-agenda-todayp (date)
8470 "Does DATE mean today, when considering `org-extend-today-until'?"
8471 (let ((today (org-today))
8472 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8473 date)))
8474 (eq date today)))
8476 (defun org-agenda-todo-yesterday (&optional arg)
8477 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8478 (interactive "P")
8479 (let* ((hour (third (decode-time
8480 (org-current-time))))
8481 (org-extend-today-until (1+ hour)))
8482 (org-agenda-todo arg)))
8484 (provide 'org-agenda)
8486 ;;; org-agenda.el ends here