org.el (org-modules): odt is no longer a contrib module
[org-mode.git] / lisp / org-agenda.el
blobf8512d3b57ba036b31ec8181fc30b9f583556035
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 calendar-check-holidays "holidays" (date))
71 (declare-function org-datetree-find-date-create "org-datetree"
72 (date &optional keep-restriction))
73 (declare-function org-columns-quit "org-colview" ())
74 (declare-function diary-date-display-form "diary-lib" (&optional type))
75 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
76 (declare-function org-habit-insert-consistency-graphs
77 "org-habit" (&optional line))
78 (declare-function org-is-habit-p "org-habit" (&optional pom))
79 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
80 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
81 (declare-function org-pop-to-buffer-same-window "org-compat"
82 (&optional buffer-or-name norecord label))
84 (defvar calendar-mode-map)
85 (defvar org-clock-current-task) ; defined in org-clock.el
86 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
87 (defvar org-habit-show-habits)
88 (defvar org-habit-show-habits-only-for-today)
90 ;; Defined somewhere in this file, but used before definition.
91 (defvar org-agenda-buffer-name)
92 (defvar org-agenda-overriding-header)
93 (defvar org-agenda-title-append nil)
94 (defvar entry)
95 (defvar date)
96 (defvar org-agenda-undo-list)
97 (defvar org-agenda-pending-undo-list)
98 (defvar original-date) ; dynamically scoped, calendar.el does scope this
100 (defcustom org-agenda-confirm-kill 1
101 "When set, remote killing from the agenda buffer needs confirmation.
102 When t, a confirmation is always needed. When a number N, confirmation is
103 only needed when the text to be killed contains more than N non-white lines."
104 :group 'org-agenda
105 :type '(choice
106 (const :tag "Never" nil)
107 (const :tag "Always" t)
108 (integer :tag "When more than N lines")))
110 (defcustom org-agenda-compact-blocks nil
111 "Non-nil means make the block agenda more compact.
112 This is done globally by leaving out lines like the agenda span
113 name and week number or the separator lines."
114 :group 'org-agenda
115 :type 'boolean)
117 (defcustom org-agenda-block-separator ?=
118 "The separator between blocks in the agenda.
119 If this is a string, it will be used as the separator, with a newline added.
120 If it is a character, it will be repeated to fill the window width.
121 If nil the separator is disabled. In `org-agenda-custom-commands' this
122 addresses the separator between the current and the previous block."
123 :group 'org-agenda
124 :type '(choice
125 (const :tag "Disabled" nil)
126 (character)
127 (string)))
129 (defgroup org-agenda-export nil
130 "Options concerning exporting agenda views in Org-mode."
131 :tag "Org Agenda Export"
132 :group 'org-agenda)
134 (defcustom org-agenda-with-colors t
135 "Non-nil means use colors in agenda views."
136 :group 'org-agenda-export
137 :type 'boolean)
139 (defcustom org-agenda-exporter-settings nil
140 "Alist of variable/value pairs that should be active during agenda export.
141 This is a good place to set options for ps-print and for htmlize.
142 Note that the way this is implemented, the values will be evaluated
143 before assigned to the variables. So make sure to quote values you do
144 *not* want evaluated, for example
146 (setq org-agenda-exporter-settings
147 '((ps-print-color-p 'black-white)))"
148 :group 'org-agenda-export
149 :type '(repeat
150 (list
151 (variable)
152 (sexp :tag "Value"))))
154 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
155 "Hook run in temporary buffer before writing it to an export file.
156 A useful function is `org-agenda-add-entry-text'."
157 :group 'org-agenda-export
158 :type 'hook
159 :options '(org-agenda-add-entry-text))
161 (defcustom org-agenda-add-entry-text-maxlines 0
162 "Maximum number of entry text lines to be added to agenda.
163 This is only relevant when `org-agenda-add-entry-text' is part of
164 `org-agenda-before-write-hook', which it is by default.
165 When this is 0, nothing will happen. When it is greater than 0, it
166 specifies the maximum number of lines that will be added for each entry
167 that is listed in the agenda view.
169 Note that this variable is not used during display, only when exporting
170 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
171 and `org-agenda-entry-text-maxlines'."
172 :group 'org-agenda
173 :type 'integer)
175 (defcustom org-agenda-add-entry-text-descriptive-links t
176 "Non-nil means export org-links as descriptive links in agenda added text.
177 This variable applies to the text added to the agenda when
178 `org-agenda-add-entry-text-maxlines' is larger than 0.
179 When this variable nil, the URL will (also) be shown."
180 :group 'org-agenda
181 :type 'boolean)
183 (defcustom org-agenda-export-html-style ""
184 "The style specification for exported HTML Agenda files.
185 If this variable contains a string, it will replace the default <style>
186 section as produced by `htmlize'.
187 Since there are different ways of setting style information, this variable
188 needs to contain the full HTML structure to provide a style, including the
189 surrounding HTML tags. The style specifications should include definitions
190 the fonts used by the agenda, here is an example:
192 <style type=\"text/css\">
193 p { font-weight: normal; color: gray; }
194 .org-agenda-structure {
195 font-size: 110%;
196 color: #003399;
197 font-weight: 600;
199 .org-todo {
200 color: #cc6666;
201 font-weight: bold;
203 .org-agenda-done {
204 color: #339933;
206 .org-done {
207 color: #339933;
209 .title { text-align: center; }
210 .todo, .deadline { color: red; }
211 .done { color: green; }
212 </style>
214 or, if you want to keep the style in a file,
216 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
218 As the value of this option simply gets inserted into the HTML <head> header,
219 you can \"misuse\" it to also add other text to the header. However,
220 <style>...</style> is required, if not present the variable will be ignored."
221 :group 'org-agenda-export
222 :group 'org-export-html
223 :type 'string)
225 (defcustom org-agenda-persistent-filter nil
226 "When set, keep filters from one agenda view to the next."
227 :group 'org-agenda
228 :type 'boolean)
230 (defgroup org-agenda-custom-commands nil
231 "Options concerning agenda views in Org-mode."
232 :tag "Org Agenda Custom Commands"
233 :group 'org-agenda)
235 (defconst org-sorting-choice
236 '(choice
237 (const time-up) (const time-down)
238 (const category-keep) (const category-up) (const category-down)
239 (const tag-down) (const tag-up)
240 (const priority-up) (const priority-down)
241 (const todo-state-up) (const todo-state-down)
242 (const effort-up) (const effort-down)
243 (const habit-up) (const habit-down)
244 (const alpha-up) (const alpha-down)
245 (const user-defined-up) (const user-defined-down))
246 "Sorting choices.")
248 (defconst org-agenda-custom-commands-local-options
249 `(repeat :tag "Local settings for this command. Remember to quote values"
250 (choice :tag "Setting"
251 (list :tag "Heading for this block"
252 (const org-agenda-overriding-header)
253 (string :tag "Headline"))
254 (list :tag "Files to be searched"
255 (const org-agenda-files)
256 (list
257 (const :format "" quote)
258 (repeat (file))))
259 (list :tag "Sorting strategy"
260 (const org-agenda-sorting-strategy)
261 (list
262 (const :format "" quote)
263 (repeat
264 ,org-sorting-choice)))
265 (list :tag "Prefix format"
266 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
267 (string))
268 (list :tag "Number of days in agenda"
269 (const org-agenda-span)
270 (choice (const :tag "Day" 'day)
271 (const :tag "Week" 'week)
272 (const :tag "Month" 'month)
273 (const :tag "Year" 'year)
274 (integer :tag "Custom")))
275 (list :tag "Fixed starting date"
276 (const org-agenda-start-day)
277 (string :value "2007-11-01"))
278 (list :tag "Start on day of week"
279 (const org-agenda-start-on-weekday)
280 (choice :value 1
281 (const :tag "Today" nil)
282 (integer :tag "Weekday No.")))
283 (list :tag "Include data from diary"
284 (const org-agenda-include-diary)
285 (boolean))
286 (list :tag "Deadline Warning days"
287 (const org-deadline-warning-days)
288 (integer :value 1))
289 (list :tag "Tags filter preset"
290 (const org-agenda-filter-preset)
291 (list
292 (const :format "" quote)
293 (repeat
294 (string :tag "+tag or -tag"))))
295 (list :tag "Set daily/weekly entry types"
296 (const org-agenda-entry-types)
297 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
298 (const :deadline)
299 (const :scheduled)
300 (const :timestamp)
301 (const :sexp)))
302 (list :tag "Standard skipping condition"
303 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
304 (const org-agenda-skip-function)
305 (list
306 (const :format "" quote)
307 (list
308 (choice
309 :tag "Skipping range"
310 (const :tag "Skip entry" org-agenda-skip-entry-if)
311 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
312 (repeat :inline t :tag "Conditions for skipping"
313 (choice
314 :tag "Condition type"
315 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
316 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
317 (list :tag "TODO state is" :inline t
318 (const 'todo)
319 (choice
320 (const :tag "any not-done state" 'todo)
321 (const :tag "any done state" 'done)
322 (const :tag "any state" 'any)
323 (list :tag "Keyword list"
324 (const :format "" quote)
325 (repeat (string :tag "Keyword")))))
326 (list :tag "TODO state is not" :inline t
327 (const 'nottodo)
328 (choice
329 (const :tag "any not-done state" 'todo)
330 (const :tag "any done state" 'done)
331 (const :tag "any state" 'any)
332 (list :tag "Keyword list"
333 (const :format "" quote)
334 (repeat (string :tag "Keyword")))))
335 (const :tag "scheduled" 'scheduled)
336 (const :tag "not scheduled" 'notscheduled)
337 (const :tag "deadline" 'deadline)
338 (const :tag "no deadline" 'notdeadline)
339 (const :tag "timestamp" 'timestamp)
340 (const :tag "no timestamp" 'nottimestamp))))))
341 (list :tag "Non-standard skipping condition"
342 :value (org-agenda-skip-function)
343 (const org-agenda-skip-function)
344 (sexp :tag "Function or form (quoted!)"))
345 (list :tag "Any variable"
346 (variable :tag "Variable")
347 (sexp :tag "Value (sexp)"))))
348 "Selection of examples for agenda command settings.
349 This will be spliced into the custom type of
350 `org-agenda-custom-commands'.")
353 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
354 ((agenda "") (alltodo))))
355 "Custom commands for the agenda.
356 These commands will be offered on the splash screen displayed by the
357 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
359 (key desc type match settings files)
361 key The key (one or more characters as a string) to be associated
362 with the command.
363 desc A description of the command, when omitted or nil, a default
364 description is built using MATCH.
365 type The command type, any of the following symbols:
366 agenda The daily/weekly agenda.
367 todo Entries with a specific TODO keyword, in all agenda files.
368 search Entries containing search words entry or headline.
369 tags Tags/Property/TODO match in all agenda files.
370 tags-todo Tags/P/T match in all agenda files, TODO entries only.
371 todo-tree Sparse tree of specific TODO keyword in *current* file.
372 tags-tree Sparse tree with all tags matches in *current* file.
373 occur-tree Occur sparse tree for *current* file.
374 ... A user-defined function.
375 match What to search for:
376 - a single keyword for TODO keyword searches
377 - a tags match expression for tags searches
378 - a word search expression for text searches.
379 - a regular expression for occur searches
380 For all other commands, this should be the empty string.
381 settings A list of option settings, similar to that in a let form, so like
382 this: ((opt1 val1) (opt2 val2) ...). The values will be
383 evaluated at the moment of execution, so quote them when needed.
384 files A list of files file to write the produced agenda buffer to
385 with the command `org-store-agenda-views'.
386 If a file name ends in \".html\", an HTML version of the buffer
387 is written out. If it ends in \".ps\", a postscript version is
388 produced. Otherwise, only the plain text is written to the file.
390 You can also define a set of commands, to create a composite agenda buffer.
391 In this case, an entry looks like this:
393 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
395 where
397 desc A description string to be displayed in the dispatcher menu.
398 cmd An agenda command, similar to the above. However, tree commands
399 are no allowed, but instead you can get agenda and global todo list.
400 So valid commands for a set are:
401 (agenda \"\" settings)
402 (alltodo \"\" settings)
403 (stuck \"\" settings)
404 (todo \"match\" settings files)
405 (search \"match\" settings files)
406 (tags \"match\" settings files)
407 (tags-todo \"match\" settings files)
409 Each command can carry a list of options, and another set of options can be
410 given for the whole set of commands. Individual command options take
411 precedence over the general options.
413 When using several characters as key to a command, the first characters
414 are prefix commands. For the dispatcher to display useful information, you
415 should provide a description for the prefix, like
417 (setq org-agenda-custom-commands
418 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
419 (\"hl\" tags \"+HOME+Lisa\")
420 (\"hp\" tags \"+HOME+Peter\")
421 (\"hk\" tags \"+HOME+Kim\")))"
422 :group 'org-agenda-custom-commands
423 :type `(repeat
424 (choice :value ("x" "Describe command here" tags "" nil)
425 (list :tag "Single command"
426 (string :tag "Access Key(s) ")
427 (option (string :tag "Description"))
428 (choice
429 (const :tag "Agenda" agenda)
430 (const :tag "TODO list" alltodo)
431 (const :tag "Search words" search)
432 (const :tag "Stuck projects" stuck)
433 (const :tag "Tags/Property match (all agenda files)" tags)
434 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
435 (const :tag "TODO keyword search (all agenda files)" todo)
436 (const :tag "Tags sparse tree (current buffer)" tags-tree)
437 (const :tag "TODO keyword tree (current buffer)" todo-tree)
438 (const :tag "Occur tree (current buffer)" occur-tree)
439 (sexp :tag "Other, user-defined function"))
440 (string :tag "Match (only for some commands)")
441 ,org-agenda-custom-commands-local-options
442 (option (repeat :tag "Export" (file :tag "Export to"))))
443 (list :tag "Command series, all agenda files"
444 (string :tag "Access Key(s)")
445 (string :tag "Description ")
446 (repeat :tag "Component"
447 (choice
448 (list :tag "Agenda"
449 (const :format "" agenda)
450 (const :tag "" :format "" "")
451 ,org-agenda-custom-commands-local-options)
452 (list :tag "TODO list (all keywords)"
453 (const :format "" alltodo)
454 (const :tag "" :format "" "")
455 ,org-agenda-custom-commands-local-options)
456 (list :tag "Search words"
457 (const :format "" search)
458 (string :tag "Match")
459 ,org-agenda-custom-commands-local-options)
460 (list :tag "Stuck projects"
461 (const :format "" stuck)
462 (const :tag "" :format "" "")
463 ,org-agenda-custom-commands-local-options)
464 (list :tag "Tags search"
465 (const :format "" tags)
466 (string :tag "Match")
467 ,org-agenda-custom-commands-local-options)
468 (list :tag "Tags search, TODO entries only"
469 (const :format "" tags-todo)
470 (string :tag "Match")
471 ,org-agenda-custom-commands-local-options)
472 (list :tag "TODO keyword search"
473 (const :format "" todo)
474 (string :tag "Match")
475 ,org-agenda-custom-commands-local-options)
476 (list :tag "Other, user-defined function"
477 (symbol :tag "function")
478 (string :tag "Match")
479 ,org-agenda-custom-commands-local-options)))
481 (repeat :tag "Settings for entire command set"
482 (list (variable :tag "Any variable")
483 (sexp :tag "Value")))
484 (option (repeat :tag "Export" (file :tag "Export to"))))
485 (cons :tag "Prefix key documentation"
486 (string :tag "Access Key(s)")
487 (string :tag "Description ")))))
489 (defcustom org-agenda-query-register ?o
490 "The register holding the current query string.
491 The purpose of this is that if you construct a query string interactively,
492 you can then use it to define a custom command."
493 :group 'org-agenda-custom-commands
494 :type 'character)
496 (defcustom org-stuck-projects
497 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
498 "How to identify stuck projects.
499 This is a list of four items:
500 1. A tags/todo/property matcher string that is used to identify a project.
501 See the manual for a description of tag and property searches.
502 The entire tree below a headline matched by this is considered one project.
503 2. A list of TODO keywords identifying non-stuck projects.
504 If the project subtree contains any headline with one of these todo
505 keywords, the project is considered to be not stuck. If you specify
506 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
507 3. A list of tags identifying non-stuck projects.
508 If the project subtree contains any headline with one of these tags,
509 the project is considered to be not stuck. If you specify \"*\" as
510 a tag, any tag will mark the project unstuck. Note that this is about
511 the explicit presence of a tag somewhere in the subtree, inherited
512 tags to not count here. If inherited tags make a project not stuck,
513 use \"-TAG\" in the tags part of the matcher under (1.) above.
514 4. An arbitrary regular expression matching non-stuck projects.
516 If the project turns out to be not stuck, search continues also in the
517 subtree to see if any of the subtasks have project status.
519 See also the variable `org-tags-match-list-sublevels' which applies
520 to projects matched by this search as well.
522 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
523 or `C-c a #' to produce the list."
524 :group 'org-agenda-custom-commands
525 :type '(list
526 (string :tag "Tags/TODO match to identify a project")
527 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
528 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
529 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
531 (defcustom org-agenda-filter-effort-default-operator "<"
532 "The default operator for effort estimate filtering.
533 If you select an effort estimate limit without first pressing an operator,
534 this one will be used."
535 :group 'org-agenda-custom-commands
536 :type '(choice (const :tag "less or equal" "<")
537 (const :tag "greater or equal"">")
538 (const :tag "equal" "=")))
540 (defgroup org-agenda-skip nil
541 "Options concerning skipping parts of agenda files."
542 :tag "Org Agenda Skip"
543 :group 'org-agenda)
545 (defcustom org-agenda-skip-function-global nil
546 "Function to be called at each match during agenda construction.
547 If this function returns nil, the current match should not be skipped.
548 If the function decided to skip an agenda match, is must return the
549 buffer position from which the search should be continued.
550 This may also be a Lisp form, which will be evaluated.
552 This variable will be applied to every agenda match, including
553 tags/property searches and TODO lists. So try to make the test function
554 do its checking as efficiently as possible. To implement a skipping
555 condition just for specific agenda commands, use the variable
556 `org-agenda-skip-function' which can be set in the options section
557 of custom agenda commands."
558 :group 'org-agenda-skip
559 :type 'sexp)
561 (defgroup org-agenda-daily/weekly nil
562 "Options concerning the daily/weekly agenda."
563 :tag "Org Agenda Daily/Weekly"
564 :group 'org-agenda)
565 (defgroup org-agenda-todo-list nil
566 "Options concerning the global todo list agenda view."
567 :tag "Org Agenda Todo List"
568 :group 'org-agenda)
569 (defgroup org-agenda-match-view nil
570 "Options concerning the general tags/property/todo match agenda view."
571 :tag "Org Agenda Match View"
572 :group 'org-agenda)
573 (defgroup org-agenda-search-view nil
574 "Options concerning the general tags/property/todo match agenda view."
575 :tag "Org Agenda Match View"
576 :group 'org-agenda)
578 (defvar org-agenda-archives-mode nil
579 "Non-nil means the agenda will include archived items.
580 If this is the symbol `trees', trees in the selected agenda scope
581 that are marked with the ARCHIVE tag will be included anyway. When this is
582 t, also all archive files associated with the current selection of agenda
583 files will be included.")
585 (defcustom org-agenda-skip-comment-trees t
586 "Non-nil means skip trees that start with the COMMENT keyword.
587 When nil, these trees are also scanned by agenda commands."
588 :group 'org-agenda-skip
589 :type 'boolean)
591 (defcustom org-agenda-todo-list-sublevels t
592 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
593 When nil, the sublevels of a TODO entry are not checked, resulting in
594 potentially much shorter TODO lists."
595 :group 'org-agenda-skip
596 :group 'org-agenda-todo-list
597 :type 'boolean)
599 (defcustom org-agenda-todo-ignore-with-date nil
600 "Non-nil means don't show entries with a date in the global todo list.
601 You can use this if you prefer to mark mere appointments with a TODO keyword,
602 but don't want them to show up in the TODO list.
603 When this is set, it also covers deadlines and scheduled items, the settings
604 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
605 will be ignored.
606 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
607 :group 'org-agenda-skip
608 :group 'org-agenda-todo-list
609 :type 'boolean)
611 (defcustom org-agenda-todo-ignore-timestamp nil
612 "Non-nil means don't show entries with a timestamp.
613 This applies when creating the global todo list.
614 Valid values are:
616 past Don't show entries for today or in the past.
618 future Don't show entries with a timestamp in the future.
619 The idea behind this is that if it has a future
620 timestamp, you don't want to think about it until the
621 date.
623 all Don't show any entries with a timestamp in the global todo list.
624 The idea behind this is that by setting a timestamp, you
625 have already \"taken care\" of this item.
627 This variable can also have an integer as a value. If positive (N),
628 todos with a timestamp N or more days in the future will be ignored. If
629 negative (-N), todos with a timestamp N or more days in the past will be
630 ignored. If 0, todos with a timestamp either today or in the future will
631 be ignored. For example, a value of -1 will exclude todos with a
632 timestamp in the past (yesterday or earlier), while a value of 7 will
633 exclude todos with a timestamp a week or more in the future.
635 See also `org-agenda-todo-ignore-with-date'.
636 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
637 to make his option also apply to the tags-todo list."
638 :group 'org-agenda-skip
639 :group 'org-agenda-todo-list
640 :type '(choice
641 (const :tag "Ignore future timestamp todos" future)
642 (const :tag "Ignore past or present timestamp todos" past)
643 (const :tag "Ignore all timestamp todos" all)
644 (const :tag "Show timestamp todos" nil)
645 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
647 (defcustom org-agenda-todo-ignore-scheduled nil
648 "Non-nil means, ignore some scheduled TODO items when making TODO list.
649 This applies when creating the global todo list.
650 Valid values are:
652 past Don't show entries scheduled today or in the past.
654 future Don't show entries scheduled in the future.
655 The idea behind this is that by scheduling it, you don't want to
656 think about it until the scheduled date.
658 all Don't show any scheduled entries in the global todo list.
659 The idea behind this is that by scheduling it, you have already
660 \"taken care\" of this item.
662 t Same as `all', for backward compatibility.
664 This variable can also have an integer as a value. See
665 `org-agenda-todo-ignore-timestamp' for more details.
667 See also `org-agenda-todo-ignore-with-date'.
668 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
669 to make his option also apply to the tags-todo list."
670 :group 'org-agenda-skip
671 :group 'org-agenda-todo-list
672 :type '(choice
673 (const :tag "Ignore future-scheduled todos" future)
674 (const :tag "Ignore past- or present-scheduled todos" past)
675 (const :tag "Ignore all scheduled todos" all)
676 (const :tag "Ignore all scheduled todos (compatibility)" t)
677 (const :tag "Show scheduled todos" nil)
678 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
680 (defcustom org-agenda-todo-ignore-deadlines nil
681 "Non-nil means ignore some deadlined TODO items when making TODO list.
682 There are different motivations for using different values, please think
683 carefully when configuring this variable.
685 This applies when creating the global todo list.
686 Valid values are:
688 near Don't show near deadline entries. A deadline is near when it is
689 closer than `org-deadline-warning-days' days. The idea behind this
690 is that such items will appear in the agenda anyway.
692 far Don't show TODO entries where a deadline has been defined, but
693 the deadline is not near. This is useful if you don't want to
694 use the todo list to figure out what to do now.
696 past Don't show entries with a deadline timestamp for today or in the past.
698 future Don't show entries with a deadline timestamp in the future, not even
699 when they become `near' ones. Use it with caution.
701 all Ignore all TODO entries that do have a deadline.
703 t Same as `near', for backward compatibility.
705 This variable can also have an integer as a value. See
706 `org-agenda-todo-ignore-timestamp' for more details.
708 See also `org-agenda-todo-ignore-with-date'.
709 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
710 to make his option also apply to the tags-todo list."
711 :group 'org-agenda-skip
712 :group 'org-agenda-todo-list
713 :type '(choice
714 (const :tag "Ignore near deadlines" near)
715 (const :tag "Ignore near deadlines (compatibility)" t)
716 (const :tag "Ignore far deadlines" far)
717 (const :tag "Ignore all TODOs with a deadlines" all)
718 (const :tag "Show all TODOs, even if they have a deadline" nil)
719 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
721 (defcustom org-agenda-tags-todo-honor-ignore-options nil
722 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
723 The variables
724 `org-agenda-todo-ignore-with-date',
725 `org-agenda-todo-ignore-timestamp',
726 `org-agenda-todo-ignore-scheduled',
727 `org-agenda-todo-ignore-deadlines'
728 make the global TODO list skip entries that have time stamps of certain
729 kinds. If this option is set, the same options will also apply for the
730 tags-todo search, which is the general tags/property matcher
731 restricted to unfinished TODO entries only."
732 :group 'org-agenda-skip
733 :group 'org-agenda-todo-list
734 :group 'org-agenda-match-view
735 :type 'boolean)
737 (defcustom org-agenda-skip-scheduled-if-done nil
738 "Non-nil means don't show scheduled items in agenda when they are done.
739 This is relevant for the daily/weekly agenda, not for the TODO list. And
740 it applies only to the actual date of the scheduling. Warnings about
741 an item with a past scheduling dates are always turned off when the item
742 is DONE."
743 :group 'org-agenda-skip
744 :group 'org-agenda-daily/weekly
745 :type 'boolean)
747 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
748 "Non-nil means skip scheduling line if same entry shows because of deadline.
749 In the agenda of today, an entry can show up multiple times because
750 it is both scheduled and has a nearby deadline, and maybe a plain time
751 stamp as well.
752 When this variable is t, then only the deadline is shown and the fact that
753 the entry is scheduled today or was scheduled previously is not shown.
754 When this variable is nil, the entry will be shown several times. When
755 the variable is the symbol `not-today', then skip scheduled previously,
756 but not scheduled today."
757 :group 'org-agenda-skip
758 :group 'org-agenda-daily/weekly
759 :type '(choice
760 (const :tag "Never" nil)
761 (const :tag "Always" t)
762 (const :tag "Not when scheduled today" not-today)))
764 (defcustom org-agenda-skip-deadline-if-done nil
765 "Non-nil means don't show deadlines when the corresponding item is done.
766 When nil, the deadline is still shown and should give you a happy feeling.
767 This is relevant for the daily/weekly agenda. And it applied only to the
768 actually date of the deadline. Warnings about approaching and past-due
769 deadlines are always turned off when the item is DONE."
770 :group 'org-agenda-skip
771 :group 'org-agenda-daily/weekly
772 :type 'boolean)
774 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
775 "Non-nil means skip deadline prewarning when entry is also scheduled.
776 This will apply on all days where a prewarning for the deadline would
777 be shown, but not at the day when the entry is actually due. On that day,
778 the deadline will be shown anyway.
779 This variable may be set to nil, t, or a number which will then give
780 the number of days before the actual deadline when the prewarnings
781 should resume.
782 This can be used in a workflow where the first showing of the deadline will
783 trigger you to schedule it, and then you don't want to be reminded of it
784 because you will take care of it on the day when scheduled."
785 :group 'org-agenda-skip
786 :group 'org-agenda-daily/weekly
787 :type '(choice
788 (const :tag "Alwas show prewarning" nil)
789 (const :tag "Remove prewarning if entry is scheduled" t)
790 (integer :tag "Restart prewarning N days before deadline")))
792 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
793 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
794 When non-nil, after the search for timestamps has matched once in an
795 entry, the rest of the entry will not be searched."
796 :group 'org-agenda-skip
797 :type 'boolean)
799 (defcustom org-agenda-skip-timestamp-if-done nil
800 "Non-nil means don't select item by timestamp or -range if it is DONE."
801 :group 'org-agenda-skip
802 :group 'org-agenda-daily/weekly
803 :type 'boolean)
805 (defcustom org-agenda-dim-blocked-tasks t
806 "Non-nil means dim blocked tasks in the agenda display.
807 This causes some overhead during agenda construction, but if you
808 have turned on `org-enforce-todo-dependencies',
809 `org-enforce-todo-checkbox-dependencies', or any other blocking
810 mechanism, this will create useful feedback in the agenda.
812 Instead of t, this variable can also have the value `invisible'.
813 Then blocked tasks will be invisible and only become visible when
814 they become unblocked. An exemption to this behavior is when a task is
815 blocked because of unchecked checkboxes below it. Since checkboxes do
816 not show up in the agenda views, making this task invisible you remove any
817 trace from agenda views that there is something to do. Therefore, a task
818 that is blocked because of checkboxes will never be made invisible, it
819 will only be dimmed."
820 :group 'org-agenda-daily/weekly
821 :group 'org-agenda-todo-list
822 :type '(choice
823 (const :tag "Do not dim" nil)
824 (const :tag "Dim to a gray face" t)
825 (const :tag "Make invisible" invisible)))
827 (defcustom org-timeline-show-empty-dates 3
828 "Non-nil means `org-timeline' also shows dates without an entry.
829 When nil, only the days which actually have entries are shown.
830 When t, all days between the first and the last date are shown.
831 When an integer, show also empty dates, but if there is a gap of more than
832 N days, just insert a special line indicating the size of the gap."
833 :group 'org-agenda-skip
834 :type '(choice
835 (const :tag "None" nil)
836 (const :tag "All" t)
837 (integer :tag "at most")))
839 (defgroup org-agenda-startup nil
840 "Options concerning initial settings in the Agenda in Org Mode."
841 :tag "Org Agenda Startup"
842 :group 'org-agenda)
844 (defcustom org-agenda-menu-show-matcher t
845 "Non-nil means show the match string in the agenda dispatcher menu.
846 When nil, the matcher string is not shown, but is put into the help-echo
847 property so than moving the mouse over the command shows it.
848 Setting it to nil is good if matcher strings are very long and/or if
849 you want to use two-column display (see `org-agenda-menu-two-column')."
850 :group 'org-agenda
851 :type 'boolean)
853 (defcustom org-agenda-menu-two-column nil
854 "Non-nil means, use two columns to show custom commands in the dispatcher.
855 If you use this, you probably want to set `org-agenda-menu-show-matcher'
856 to nil."
857 :group 'org-agenda
858 :type 'boolean)
860 (defcustom org-finalize-agenda-hook nil
861 "Hook run just before displaying an agenda buffer."
862 :group 'org-agenda-startup
863 :type 'hook)
865 (defcustom org-agenda-mouse-1-follows-link nil
866 "Non-nil means mouse-1 on a link will follow the link in the agenda.
867 A longer mouse click will still set point. Does not work on XEmacs.
868 Needs to be set before org.el is loaded."
869 :group 'org-agenda-startup
870 :type 'boolean)
872 (defcustom org-agenda-start-with-follow-mode nil
873 "The initial value of follow mode in a newly created agenda window."
874 :group 'org-agenda-startup
875 :type 'boolean)
877 (defcustom org-agenda-follow-indirect nil
878 "Non-nil means `org-agenda-follow-mode' displays only the
879 current item's tree, in an indirect buffer."
880 :group 'org-agenda
881 :type 'boolean)
883 (defcustom org-agenda-show-outline-path t
884 "Non-nil means show outline path in echo area after line motion."
885 :group 'org-agenda-startup
886 :type 'boolean)
888 (defcustom org-agenda-start-with-entry-text-mode nil
889 "The initial value of entry-text-mode in a newly created agenda window."
890 :group 'org-agenda-startup
891 :type 'boolean)
893 (defcustom org-agenda-entry-text-maxlines 5
894 "Number of text lines to be added when `E' is pressed in the agenda.
896 Note that this variable only used during agenda display. Add add entry text
897 when exporting the agenda, configure the variable
898 `org-agenda-add-entry-ext-maxlines'."
899 :group 'org-agenda
900 :type 'integer)
902 (defcustom org-agenda-entry-text-exclude-regexps nil
903 "List of regular expressions to clean up entry text.
904 The complete matches of all regular expressions in this list will be
905 removed from entry text before it is shown in the agenda."
906 :group 'org-agenda
907 :type '(repeat (regexp)))
909 (defvar org-agenda-entry-text-cleanup-hook nil
910 "Hook that is run after basic cleanup of entry text to be shown in agenda.
911 This cleanup is done in a temporary buffer, so the function may inspect and
912 change the entire buffer.
913 Some default stuff like drawers and scheduling/deadline dates will already
914 have been removed when this is called, as will any matches for regular
915 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
917 (defvar org-agenda-include-inactive-timestamps nil
918 "Non-nil means include inactive time stamps in agenda and timeline.")
920 (defgroup org-agenda-windows nil
921 "Options concerning the windows used by the Agenda in Org Mode."
922 :tag "Org Agenda Windows"
923 :group 'org-agenda)
925 (defcustom org-agenda-window-setup 'reorganize-frame
926 "How the agenda buffer should be displayed.
927 Possible values for this option are:
929 current-window Show agenda in the current window, keeping all other windows.
930 other-window Use `switch-to-buffer-other-window' to display agenda.
931 reorganize-frame Show only two windows on the current frame, the current
932 window and the agenda.
933 other-frame Use `switch-to-buffer-other-frame' to display agenda.
934 Also, when exiting the agenda, kill that frame.
935 See also the variable `org-agenda-restore-windows-after-quit'."
936 :group 'org-agenda-windows
937 :type '(choice
938 (const current-window)
939 (const other-frame)
940 (const other-window)
941 (const reorganize-frame)))
943 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
944 "The min and max height of the agenda window as a fraction of frame height.
945 The value of the variable is a cons cell with two numbers between 0 and 1.
946 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
947 :group 'org-agenda-windows
948 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
950 (defcustom org-agenda-restore-windows-after-quit nil
951 "Non-nil means restore window configuration upon exiting agenda.
952 Before the window configuration is changed for displaying the agenda,
953 the current status is recorded. When the agenda is exited with
954 `q' or `x' and this option is set, the old state is restored. If
955 `org-agenda-window-setup' is `other-frame', the value of this
956 option will be ignored."
957 :group 'org-agenda-windows
958 :type 'boolean)
960 (defcustom org-agenda-ndays nil
961 "Number of days to include in overview display.
962 Should be 1 or 7.
963 Obsolete, see `org-agenda-span'."
964 :group 'org-agenda-daily/weekly
965 :type 'integer)
967 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
969 (defcustom org-agenda-span 'week
970 "Number of days to include in overview display.
971 Can be day, week, month, year, or any number of days.
972 Custom commands can set this variable in the options section."
973 :group 'org-agenda-daily/weekly
974 :type '(choice (const :tag "Day" day)
975 (const :tag "Week" week)
976 (const :tag "Month" month)
977 (const :tag "Year" year)
978 (integer :tag "Custom")))
980 (defcustom org-agenda-start-on-weekday 1
981 "Non-nil means start the overview always on the specified weekday.
982 0 denotes Sunday, 1 denotes Monday etc.
983 When nil, always start on the current day.
984 Custom commands can set this variable in the options section."
985 :group 'org-agenda-daily/weekly
986 :type '(choice (const :tag "Today" nil)
987 (integer :tag "Weekday No.")))
989 (defcustom org-agenda-show-all-dates t
990 "Non-nil means `org-agenda' shows every day in the selected range.
991 When nil, only the days which actually have entries are shown."
992 :group 'org-agenda-daily/weekly
993 :type 'boolean)
995 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
996 "Format string for displaying dates in the agenda.
997 Used by the daily/weekly agenda and by the timeline. This should be
998 a format string understood by `format-time-string', or a function returning
999 the formatted date as a string. The function must take a single argument,
1000 a calendar-style date list like (month day year)."
1001 :group 'org-agenda-daily/weekly
1002 :type '(choice
1003 (string :tag "Format string")
1004 (function :tag "Function")))
1006 (defun org-agenda-format-date-aligned (date)
1007 "Format a date string for display in the daily/weekly agenda, or timeline.
1008 This function makes sure that dates are aligned for easy reading."
1009 (require 'cal-iso)
1010 (let* ((dayname (calendar-day-name date))
1011 (day (cadr date))
1012 (day-of-week (calendar-day-of-week date))
1013 (month (car date))
1014 (monthname (calendar-month-name month))
1015 (year (nth 2 date))
1016 (iso-week (org-days-to-iso-week
1017 (calendar-absolute-from-gregorian date)))
1018 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1019 (1- year))
1020 ((and (= month 12) (<= iso-week 1))
1021 (1+ year))
1022 (t year)))
1023 (weekstring (if (= day-of-week 1)
1024 (format " W%02d" iso-week)
1025 "")))
1026 (format "%-10s %2d %s %4d%s"
1027 dayname day monthname year weekstring)))
1029 (defcustom org-agenda-time-leading-zero nil
1030 "Non-nil means use leading zero for military times in agenda.
1031 For example, 9:30am would become 09:30 rather than 9:30."
1032 :group 'org-agenda-daily/weekly
1033 :type 'boolean)
1035 (defcustom org-agenda-timegrid-use-ampm nil
1036 "When set, show AM/PM style timestamps on the timegrid."
1037 :group 'org-agenda
1038 :type 'boolean)
1040 (defun org-agenda-time-of-day-to-ampm (time)
1041 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1042 (let* ((hour-number (string-to-number (substring time 0 -3)))
1043 (minute (substring time -2))
1044 (ampm "am"))
1045 (cond
1046 ((equal hour-number 12)
1047 (setq ampm "pm"))
1048 ((> hour-number 12)
1049 (setq ampm "pm")
1050 (setq hour-number (- hour-number 12))))
1051 (concat
1052 (if org-agenda-time-leading-zero
1053 (format "%02d" hour-number)
1054 (format "%02s" (number-to-string hour-number)))
1055 ":" minute ampm)))
1057 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1058 "Conditionally convert TIME to AM/PM format
1059 based on `org-agenda-timegrid-use-ampm'"
1060 (if org-agenda-timegrid-use-ampm
1061 (org-agenda-time-of-day-to-ampm time)
1062 time))
1064 (defcustom org-agenda-weekend-days '(6 0)
1065 "Which days are weekend?
1066 These days get the special face `org-agenda-date-weekend' in the agenda
1067 and timeline buffers."
1068 :group 'org-agenda-daily/weekly
1069 :type '(set :greedy t
1070 (const :tag "Monday" 1)
1071 (const :tag "Tuesday" 2)
1072 (const :tag "Wednesday" 3)
1073 (const :tag "Thursday" 4)
1074 (const :tag "Friday" 5)
1075 (const :tag "Saturday" 6)
1076 (const :tag "Sunday" 0)))
1078 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1079 "Non-nil means jumpt to today when moving a past date forward in time.
1080 When using S-right in the agenda to move a a date forward, and the date
1081 stamp currently points to the past, the first key press will move it
1082 to today. WHen nil, just move one day forward even if the date stays
1083 in the past."
1084 :group 'org-agenda-daily/weekly
1085 :type 'boolean)
1087 (defcustom org-agenda-include-diary nil
1088 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1089 Custom commands can set this variable in the options section."
1090 :group 'org-agenda-daily/weekly
1091 :type 'boolean)
1093 (defcustom org-agenda-include-deadlines t
1094 "If non-nil, include entries within their deadline warning period.
1095 Custom commands can set this variable in the options section."
1096 :group 'org-agenda-daily/weekly
1097 :type 'boolean)
1099 (defcustom org-agenda-repeating-timestamp-show-all t
1100 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1101 When set to a list of strings, only show occurrences of repeating
1102 stamps for these TODO keywords. When nil, only one occurrence is
1103 shown, either today or the nearest into the future."
1104 :group 'org-agenda-daily/weekly
1105 :type '(choice
1106 (const :tag "Show repeating stamps" t)
1107 (repeat :tag "Show repeating stamps for these TODO keywords"
1108 (string :tag "TODO Keyword"))
1109 (const :tag "Don't show repeating stamps" nil)))
1111 (defcustom org-scheduled-past-days 10000
1112 "No. of days to continue listing scheduled items that are not marked DONE.
1113 When an item is scheduled on a date, it shows up in the agenda on this
1114 day and will be listed until it is marked done for the number of days
1115 given here."
1116 :group 'org-agenda-daily/weekly
1117 :type 'integer)
1119 (defcustom org-agenda-log-mode-items '(closed clock)
1120 "List of items that should be shown in agenda log mode.
1121 This list may contain the following symbols:
1123 closed Show entries that have been closed on that day.
1124 clock Show entries that have received clocked time on that day.
1125 state Show all logged state changes.
1126 Note that instead of changing this variable, you can also press `C-u l' in
1127 the agenda to display all available LOG items temporarily."
1128 :group 'org-agenda-daily/weekly
1129 :type '(set :greedy t (const closed) (const clock) (const state)))
1131 (defcustom org-agenda-clock-consistency-checks
1132 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1133 :gap-ok-around ("4:00")
1134 :default-face ((:background "DarkRed") (:foreground "white"))
1135 :overlap-face nil :gap-face nil :no-end-time-face nil
1136 :long-face nil :short-face nil)
1137 "This is a property list, with the following keys:
1139 :max-duration Mark clocking chunks that are longer than this time.
1140 This is a time string like \"HH:MM\", or the number
1141 of minutes as an integer.
1143 :min-duration Mark clocking chunks that are shorter that this.
1144 This is a time string like \"HH:MM\", or the number
1145 of minutes as an integer.
1147 :max-gap Mark gaps between clocking chunks that are longer than
1148 this duration. A number of minutes, or a string
1149 like \"HH:MM\".
1151 :gap-ok-around List of times during the day which are usually not working
1152 times. When a gap is detected, but the gap contains any
1153 of these times, the gap is *not* reported. For example,
1154 if this is (\"4:00\" \"13:00\") then gaps that contain
1155 4:00 in the morning (i.e. the night) and 13:00
1156 (i.e. a typical lunch time) do not cause a warning.
1157 You should have at least one time during the night in this
1158 list, or otherwise the first task each morning will trigger
1159 a warning because it follows a long gap.
1161 Furthermore, the following properties can be used to define faces for
1162 issue display.
1164 :default-face the default face, if the specific face is undefined
1165 :overlap-face face for overlapping clocks
1166 :gap-face face for gaps between clocks
1167 :no-end-time-face face for incomplete clocks
1168 :long-face face for clock intervals that are too long
1169 :short-face face for clock intervals that are too short"
1170 :group 'org-agenda-daily/weekly
1171 :group 'org-clock
1172 :type 'plist)
1174 (defcustom org-agenda-log-mode-add-notes t
1175 "Non-nil means add first line of notes to log entries in agenda views.
1176 If a log item like a state change or a clock entry is associated with
1177 notes, the first line of these notes will be added to the entry in the
1178 agenda display."
1179 :group 'org-agenda-daily/weekly
1180 :type 'boolean)
1182 (defcustom org-agenda-start-with-log-mode nil
1183 "The initial value of log-mode in a newly created agenda window."
1184 :group 'org-agenda-startup
1185 :group 'org-agenda-daily/weekly
1186 :type 'boolean)
1188 (defcustom org-agenda-start-with-clockreport-mode nil
1189 "The initial value of clockreport-mode in a newly created agenda window."
1190 :group 'org-agenda-startup
1191 :group 'org-agenda-daily/weekly
1192 :type 'boolean)
1194 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1195 "Property list with parameters for the clocktable in clockreport mode.
1196 This is the display mode that shows a clock table in the daily/weekly
1197 agenda, the properties for this dynamic block can be set here.
1198 The usual clocktable parameters are allowed here, but you cannot set
1199 the properties :name, :tstart, :tend, :block, and :scope - these will
1200 be overwritten to make sure the content accurately reflects the
1201 current display in the agenda."
1202 :group 'org-agenda-daily/weekly
1203 :type 'plist)
1205 (defcustom org-agenda-search-view-always-boolean nil
1206 "Non-nil means the search string is interpreted as individual parts.
1208 The search string for search view can either be interpreted as a phrase,
1209 or as a list of snippets that define a boolean search for a number of
1210 strings.
1212 When this is non-nil, the string will be split on whitespace, and each
1213 snippet will be searched individually, and all must match in order to
1214 select an entry. A snippet is then a single string of non-white
1215 characters, or a string in double quotes, or a regexp in {} braces.
1216 If a snippet is preceded by \"-\", the snippet must *not* match.
1217 \"+\" is syntactic sugar for positive selection. Each snippet may
1218 be found as a full word or a partial word, but see the variable
1219 `org-agenda-search-view-force-full-words'.
1221 When this is nil, search will look for the entire search phrase as one,
1222 with each space character matching any amount of whitespace, including
1223 line breaks.
1225 Even when this is nil, you can still switch to Boolean search dynamically
1226 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1227 is a regexp marked with braces like \"{abc}\", this will also switch to
1228 boolean search."
1229 :group 'org-agenda-search-view
1230 :type 'boolean)
1232 (if (fboundp 'defvaralias)
1233 (defvaralias 'org-agenda-search-view-search-words-only
1234 'org-agenda-search-view-always-boolean))
1236 (defcustom org-agenda-search-view-force-full-words nil
1237 "Non-nil means, search words must be matches as complete words.
1238 When nil, they may also match part of a word."
1239 :group 'org-agenda-search-view
1240 :type 'boolean)
1242 (defgroup org-agenda-time-grid nil
1243 "Options concerning the time grid in the Org-mode Agenda."
1244 :tag "Org Agenda Time Grid"
1245 :group 'org-agenda)
1247 (defcustom org-agenda-search-headline-for-time t
1248 "Non-nil means search headline for a time-of-day.
1249 If the headline contains a time-of-day in one format or another, it will
1250 be used to sort the entry into the time sequence of items for a day.
1251 Some people have time stamps in the headline that refer to the creation
1252 time or so, and then this produces an unwanted side effect. If this is
1253 the case for your, use this variable to turn off searching the headline
1254 for a time."
1255 :group 'org-agenda-time-grid
1256 :type 'boolean)
1258 (defcustom org-agenda-use-time-grid t
1259 "Non-nil means show a time grid in the agenda schedule.
1260 A time grid is a set of lines for specific times (like every two hours between
1261 8:00 and 20:00). The items scheduled for a day at specific times are
1262 sorted in between these lines.
1263 For details about when the grid will be shown, and what it will look like, see
1264 the variable `org-agenda-time-grid'."
1265 :group 'org-agenda-time-grid
1266 :type 'boolean)
1268 (defcustom org-agenda-time-grid
1269 '((daily today require-timed)
1270 "----------------"
1271 (800 1000 1200 1400 1600 1800 2000))
1273 "The settings for time grid for agenda display.
1274 This is a list of three items. The first item is again a list. It contains
1275 symbols specifying conditions when the grid should be displayed:
1277 daily if the agenda shows a single day
1278 weekly if the agenda shows an entire week
1279 today show grid on current date, independent of daily/weekly display
1280 require-timed show grid only if at least one item has a time specification
1282 The second item is a string which will be placed behind the grid time.
1284 The third item is a list of integers, indicating the times that should have
1285 a grid line."
1286 :group 'org-agenda-time-grid
1287 :type
1288 '(list
1289 (set :greedy t :tag "Grid Display Options"
1290 (const :tag "Show grid in single day agenda display" daily)
1291 (const :tag "Show grid in weekly agenda display" weekly)
1292 (const :tag "Always show grid for today" today)
1293 (const :tag "Show grid only if any timed entries are present"
1294 require-timed)
1295 (const :tag "Skip grid times already present in an entry"
1296 remove-match))
1297 (string :tag "Grid String")
1298 (repeat :tag "Grid Times" (integer :tag "Time"))))
1300 (defcustom org-agenda-show-current-time-in-grid t
1301 "Non-nil means show the current time in the time grid."
1302 :group 'org-agenda-time-grid
1303 :type 'boolean)
1305 (defcustom org-agenda-current-time-string
1306 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1307 "The string for the current time marker in the agenda."
1308 :group 'org-agenda-time-grid
1309 :type 'string)
1311 (defgroup org-agenda-sorting nil
1312 "Options concerning sorting in the Org-mode Agenda."
1313 :tag "Org Agenda Sorting"
1314 :group 'org-agenda)
1316 (defcustom org-agenda-sorting-strategy
1317 '((agenda habit-down time-up priority-down category-keep)
1318 (todo priority-down category-keep)
1319 (tags priority-down category-keep)
1320 (search category-keep))
1321 "Sorting structure for the agenda items of a single day.
1322 This is a list of symbols which will be used in sequence to determine
1323 if an entry should be listed before another entry. The following
1324 symbols are recognized:
1326 time-up Put entries with time-of-day indications first, early first
1327 time-down Put entries with time-of-day indications first, late first
1328 category-keep Keep the default order of categories, corresponding to the
1329 sequence in `org-agenda-files'.
1330 category-up Sort alphabetically by category, A-Z.
1331 category-down Sort alphabetically by category, Z-A.
1332 tag-up Sort alphabetically by last tag, A-Z.
1333 tag-down Sort alphabetically by last tag, Z-A.
1334 priority-up Sort numerically by priority, high priority last.
1335 priority-down Sort numerically by priority, high priority first.
1336 todo-state-up Sort by todo state, tasks that are done last.
1337 todo-state-down Sort by todo state, tasks that are done first.
1338 effort-up Sort numerically by estimated effort, high effort last.
1339 effort-down Sort numerically by estimated effort, high effort first.
1340 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1341 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1342 habit-up Put entries that are habits first
1343 habit-down Put entries that are habits last
1344 alpha-up Sort headlines alphabetically
1345 alpha-down Sort headlines alphabetically, reversed
1347 The different possibilities will be tried in sequence, and testing stops
1348 if one comparison returns a \"not-equal\". For example, the default
1349 '(time-up category-keep priority-down)
1350 means: Pull out all entries having a specified time of day and sort them,
1351 in order to make a time schedule for the current day the first thing in the
1352 agenda listing for the day. Of the entries without a time indication, keep
1353 the grouped in categories, don't sort the categories, but keep them in
1354 the sequence given in `org-agenda-files'. Within each category sort by
1355 priority.
1357 Leaving out `category-keep' would mean that items will be sorted across
1358 categories by priority.
1360 Instead of a single list, this can also be a set of list for specific
1361 contents, with a context symbol in the car of the list, any of
1362 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1364 Custom commands can bind this variable in the options section."
1365 :group 'org-agenda-sorting
1366 :type `(choice
1367 (repeat :tag "General" ,org-sorting-choice)
1368 (list :tag "Individually"
1369 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1370 (repeat ,org-sorting-choice))
1371 (cons (const :tag "Strategy for TODO lists" todo)
1372 (repeat ,org-sorting-choice))
1373 (cons (const :tag "Strategy for Tags matches" tags)
1374 (repeat ,org-sorting-choice))
1375 (cons (const :tag "Strategy for search matches" search)
1376 (repeat ,org-sorting-choice)))))
1378 (defcustom org-agenda-cmp-user-defined nil
1379 "A function to define the comparison `user-defined'.
1380 This function must receive two arguments, agenda entry a and b.
1381 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1382 the user comparison, return nil.
1383 When this is defined, you can make `user-defined-up' and `user-defined-down'
1384 part of an agenda sorting strategy."
1385 :group 'org-agenda-sorting
1386 :type 'symbol)
1388 (defcustom org-sort-agenda-notime-is-late t
1389 "Non-nil means items without time are considered late.
1390 This is only relevant for sorting. When t, items which have no explicit
1391 time like 15:30 will be considered as 99:01, i.e. later than any items which
1392 do have a time. When nil, the default time is before 0:00. You can use this
1393 option to decide if the schedule for today should come before or after timeless
1394 agenda entries."
1395 :group 'org-agenda-sorting
1396 :type 'boolean)
1398 (defcustom org-sort-agenda-noeffort-is-high t
1399 "Non-nil means items without effort estimate are sorted as high effort.
1400 This also applies when filtering an agenda view with respect to the
1401 < or > effort operator. Then, tasks with no effort defined will be treated
1402 as tasks with high effort.
1403 When nil, such items are sorted as 0 minutes effort."
1404 :group 'org-agenda-sorting
1405 :type 'boolean)
1407 (defgroup org-agenda-line-format nil
1408 "Options concerning the entry prefix in the Org-mode agenda display."
1409 :tag "Org Agenda Line Format"
1410 :group 'org-agenda)
1412 (defcustom org-agenda-prefix-format
1413 '((agenda . " %i %-12:c%?-12t% s")
1414 (timeline . " % s")
1415 (todo . " %i %-12:c")
1416 (tags . " %i %-12:c")
1417 (search . " %i %-12:c"))
1418 "Format specifications for the prefix of items in the agenda views.
1419 An alist with five entries, each for the different agenda types. The
1420 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1421 The values are format strings.
1423 This format works similar to a printf format, with the following meaning:
1425 %c the category of the item, \"Diary\" for entries from the diary,
1426 or as given by the CATEGORY keyword or derived from the file name
1427 %e the effort required by the item
1428 %i the icon category of the item, see `org-agenda-category-icon-alist'
1429 %T the last tag of the item (ignore inherited tags, which come first)
1430 %t the HH:MM time-of-day specification if one applies to the entry
1431 %s Scheduling/Deadline information, a short string
1432 %(expression) Eval EXPRESSION and replace the control string
1433 by the result
1435 All specifiers work basically like the standard `%s' of printf, but may
1436 contain two additional characters: a question mark just after the `%'
1437 and a whitespace/punctuation character just before the final letter.
1439 If the first character after `%' is a question mark, the entire field
1440 will only be included if the corresponding value applies to the current
1441 entry. This is useful for fields which should have fixed width when
1442 present, but zero width when absent. For example, \"%?-12t\" will
1443 result in a 12 character time field if a time of the day is specified,
1444 but will completely disappear in entries which do not contain a time.
1446 If there is punctuation or whitespace character just before the final
1447 format letter, this character will be appended to the field value if
1448 the value is not empty. For example, the format \"%-12:c\" leads to
1449 \"Diary: \" if the category is \"Diary\". If the category were be
1450 empty, no additional colon would be inserted.
1452 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1453 which means:
1455 - Indent the line with two space characters
1456 - Give the category a 12 chars wide field, padded with whitespace on
1457 the right (because of `-'). Append a colon if there is a category
1458 (because of `:').
1459 - If there is a time-of-day, put it into a 12 chars wide field. If no
1460 time, don't put in an empty field, just skip it (because of '?').
1461 - Finally, put the scheduling information.
1463 See also the variables `org-agenda-remove-times-when-in-prefix' and
1464 `org-agenda-remove-tags'.
1466 Custom commands can set this variable in the options section."
1467 :type '(choice
1468 (string :tag "General format")
1469 (list :greedy t :tag "View dependent"
1470 (cons (const agenda) (string :tag "Format"))
1471 (cons (const timeline) (string :tag "Format"))
1472 (cons (const todo) (string :tag "Format"))
1473 (cons (const tags) (string :tag "Format"))
1474 (cons (const search) (string :tag "Format"))))
1475 :group 'org-agenda-line-format)
1477 (defvar org-prefix-format-compiled nil
1478 "The compiled version of the most recently used prefix format.
1479 See the variable `org-agenda-prefix-format'.")
1481 (defcustom org-agenda-todo-keyword-format "%-1s"
1482 "Format for the TODO keyword in agenda lines.
1483 Set this to something like \"%-12s\" if you want all TODO keywords
1484 to occupy a fixed space in the agenda display."
1485 :group 'org-agenda-line-format
1486 :type 'string)
1488 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1489 "Text preceding timerange entries in the agenda view.
1490 This is a list with two strings. The first applies when the range
1491 is entirely on one day. The second applies if the range spans several days.
1492 The strings may have two \"%d\" format specifiers which will be filled
1493 with the sequence number of the days, and the total number of days in the
1494 range, respectively."
1495 :group 'org-agenda-line-format
1496 :type '(list
1497 (string :tag "Deadline today ")
1498 (choice :tag "Deadline relative"
1499 (string :tag "Format string")
1500 (function))))
1502 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1503 "Text preceding scheduled items in the agenda view.
1504 This is a list with two strings. The first applies when the item is
1505 scheduled on the current day. The second applies when it has been scheduled
1506 previously, it may contain a %d indicating that this is the nth time that
1507 this item is scheduled, due to automatic rescheduling of unfinished items
1508 for the following day. So this number is one larger than the number of days
1509 that passed since this item was scheduled first."
1510 :group 'org-agenda-line-format
1511 :type '(list
1512 (string :tag "Scheduled today ")
1513 (string :tag "Scheduled previously")))
1515 (defcustom org-agenda-inactive-leader "["
1516 "Text preceding item pulled into the agenda by inactive time stamps.
1517 These entries are added to the agenda when pressing \"[\"."
1518 :group 'org-agenda-line-format
1519 :type '(list
1520 (string :tag "Scheduled today ")
1521 (string :tag "Scheduled previously")))
1523 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1524 "Text preceding deadline items in the agenda view.
1525 This is a list with two strings. The first applies when the item has its
1526 deadline on the current day. The second applies when it is in the past or
1527 in the future, it may contain %d to capture how many days away the deadline
1528 is (was)."
1529 :group 'org-agenda-line-format
1530 :type '(list
1531 (string :tag "Deadline today ")
1532 (choice :tag "Deadline relative"
1533 (string :tag "Format string")
1534 (function))))
1536 (defcustom org-agenda-remove-times-when-in-prefix t
1537 "Non-nil means remove duplicate time specifications in agenda items.
1538 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1539 time-of-day specification in a headline or diary entry is extracted and
1540 placed into the prefix. If this option is non-nil, the original specification
1541 \(a timestamp or -range, or just a plain time(range) specification like
1542 11:30-4pm) will be removed for agenda display. This makes the agenda less
1543 cluttered.
1544 The option can be t or nil. It may also be the symbol `beg', indicating
1545 that the time should only be removed when it is located at the beginning of
1546 the headline/diary entry."
1547 :group 'org-agenda-line-format
1548 :type '(choice
1549 (const :tag "Always" t)
1550 (const :tag "Never" nil)
1551 (const :tag "When at beginning of entry" beg)))
1553 (defcustom org-agenda-remove-timeranges-from-blocks nil
1554 "Non-nil means remove time ranges specifications in agenda
1555 items that span on several days."
1556 :group 'org-agenda-line-format
1557 :type 'boolean)
1559 (defcustom org-agenda-default-appointment-duration nil
1560 "Default duration for appointments that only have a starting time.
1561 When nil, no duration is specified in such cases.
1562 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1563 :group 'org-agenda-line-format
1564 :type '(choice
1565 (integer :tag "Minutes")
1566 (const :tag "No default duration")))
1568 (defcustom org-agenda-show-inherited-tags t
1569 "Non-nil means show inherited tags in each agenda line."
1570 :group 'org-agenda-line-format
1571 :type 'boolean)
1573 (defcustom org-agenda-hide-tags-regexp nil
1574 "Regular expression used to filter away specific tags in agenda views.
1575 This means that these tags will be present, but not be shown in the agenda
1576 line. Secondary filtering will still work on the hidden tags.
1577 Nil means don't hide any tags."
1578 :group 'org-agenda-line-format
1579 :type '(choice
1580 (const :tag "Hide none" nil)
1581 (string :tag "Regexp ")))
1583 (defcustom org-agenda-remove-tags nil
1584 "Non-nil means remove the tags from the headline copy in the agenda.
1585 When this is the symbol `prefix', only remove tags when
1586 `org-agenda-prefix-format' contains a `%T' specifier."
1587 :group 'org-agenda-line-format
1588 :type '(choice
1589 (const :tag "Always" t)
1590 (const :tag "Never" nil)
1591 (const :tag "When prefix format contains %T" prefix)))
1593 (if (fboundp 'defvaralias)
1594 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1595 'org-agenda-remove-tags))
1597 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1598 "Shift tags in agenda items to this column.
1599 If this number is positive, it specifies the column. If it is negative,
1600 it means that the tags should be flushright to that column. For example,
1601 -80 works well for a normal 80 character screen."
1602 :group 'org-agenda-line-format
1603 :type 'integer)
1605 (if (fboundp 'defvaralias)
1606 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1608 (defcustom org-agenda-fontify-priorities 'cookies
1609 "Non-nil means highlight low and high priorities in agenda.
1610 When t, the highest priority entries are bold, lowest priority italic.
1611 However, settings in `org-priority-faces' will overrule these faces.
1612 When this variable is the symbol `cookies', only fontify the
1613 cookies, not the entire task.
1614 This may also be an association list of priority faces, whose
1615 keys are the character values of `org-highest-priority',
1616 `org-default-priority', and `org-lowest-priority' (the default values
1617 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1618 color as a string, or a list like `(:background \"Red\")'.
1619 If it is a color, the variable `org-faces-easy-properties'
1620 determines if it is a foreground or a background color."
1621 :group 'org-agenda-line-format
1622 :type '(choice
1623 (const :tag "Never" nil)
1624 (const :tag "Defaults" t)
1625 (const :tag "Cookies only" cookies)
1626 (repeat :tag "Specify"
1627 (list (character :tag "Priority" :value ?A)
1628 (choice :tag "Face "
1629 (string :tag "Color")
1630 (sexp :tag "Face"))))))
1632 (defcustom org-agenda-day-face-function nil
1633 "Function called to determine what face should be used to display a day.
1634 The only argument passed to that function is the day. It should
1635 returns a face, or nil if does not want to specify a face and let
1636 the normal rules apply."
1637 :group 'org-agenda-line-format
1638 :type 'function)
1640 (defcustom org-agenda-category-icon-alist nil
1641 "Alist of category icon to be displayed in agenda views.
1643 Each entry should have the following format:
1645 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1647 Where CATEGORY-REGEXP is a regexp matching the categories where
1648 the icon should be displayed.
1649 FILE-OR-DATA either a file path or a string containing image data.
1651 The other fields can be omited safely if not needed:
1652 TYPE indicates the image type.
1653 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1654 image data.
1655 PROPS are additional image attributes to assign to the image,
1656 like, e.g. `:ascent center'.
1658 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1660 If you want to set the display properties yourself, just put a
1661 list as second element:
1663 (CATEGORY-REGEXP (MY PROPERTY LIST))
1665 For example, to display a 16px horizontal space for Emacs
1666 category, you can use:
1668 (\"Emacs\" '(space . (:width (16))))"
1669 :group 'org-agenda-line-format
1670 :type '(alist :key-type (string :tag "Regexp matching category")
1671 :value-type (choice (list :tag "Icon"
1672 (string :tag "File or data")
1673 (symbol :tag "Type")
1674 (boolean :tag "Data?")
1675 (repeat :tag "Extra image properties" :inline t symbol))
1676 (list :tag "Display properties" sexp))))
1678 (defgroup org-agenda-column-view nil
1679 "Options concerning column view in the agenda."
1680 :tag "Org Agenda Column View"
1681 :group 'org-agenda)
1683 (defcustom org-agenda-columns-show-summaries t
1684 "Non-nil means show summaries for columns displayed in the agenda view."
1685 :group 'org-agenda-column-view
1686 :type 'boolean)
1688 (defcustom org-agenda-columns-compute-summary-properties t
1689 "Non-nil means recompute all summary properties before column view.
1690 When column view in the agenda is listing properties that have a summary
1691 operator, it can go to all relevant buffers and recompute the summaries
1692 there. This can mean overhead for the agenda column view, but is necessary
1693 to have thing up to date.
1694 As a special case, a CLOCKSUM property also makes sure that the clock
1695 computations are current."
1696 :group 'org-agenda-column-view
1697 :type 'boolean)
1699 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1700 "Non-nil means the duration of an appointment will add to day effort.
1701 The property to which appointment durations will be added is the one given
1702 in the option `org-effort-property'. If an appointment does not have
1703 an end time, `org-agenda-default-appointment-duration' will be used. If that
1704 is not set, an appointment without end time will not contribute to the time
1705 estimate."
1706 :group 'org-agenda-column-view
1707 :type 'boolean)
1709 (defcustom org-agenda-auto-exclude-function nil
1710 "A function called with a tag to decide if it is filtered on '/ RET'.
1711 The sole argument to the function, which is called once for each
1712 possible tag, is a string giving the name of the tag. The
1713 function should return either nil if the tag should be included
1714 as normal, or \"-<TAG>\" to exclude the tag.
1715 Note that for the purpose of tag filtering, only the lower-case version of
1716 all tags will be considered, so that this function will only ever see
1717 the lower-case version of all tags."
1718 :group 'org-agenda
1719 :type 'function)
1721 (defcustom org-agenda-bulk-custom-functions nil
1722 "Alist of characters and custom functions for bulk actions.
1723 For example, this value makes those two functions available:
1725 '((?R set-category)
1726 (?C bulk-cut))
1728 With selected entries in an agenda buffer, `B R' will call
1729 the custom function `set-category' on the selected entries.
1730 Note that functions in this alist don't need to be quoted."
1731 :type 'alist
1732 :group 'org-agenda)
1734 (eval-when-compile
1735 (require 'cl))
1736 (require 'org)
1738 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1739 "Execute BODY with point at location given by `org-hd-marker' property.
1740 If STRING is non-nil, the text property will be fetched from position 0
1741 in that string. If STRING is nil, it will be fetched from the beginning
1742 of the current line."
1743 (org-with-gensyms (marker)
1744 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1745 'org-hd-marker string)))
1746 (with-current-buffer (marker-buffer ,marker)
1747 (save-excursion
1748 (goto-char ,marker)
1749 ,@body)))))
1750 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1752 (defun org-add-agenda-custom-command (entry)
1753 "Replace or add a command in `org-agenda-custom-commands'.
1754 This is mostly for hacking and trying a new command - once the command
1755 works you probably want to add it to `org-agenda-custom-commands' for good."
1756 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1757 (if ass
1758 (setcdr ass (cdr entry))
1759 (push entry org-agenda-custom-commands))))
1761 ;;; Define the Org-agenda-mode
1763 (defvar org-agenda-mode-map (make-sparse-keymap)
1764 "Keymap for `org-agenda-mode'.")
1765 (if (fboundp 'defvaralias)
1766 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1768 (defvar org-agenda-menu) ; defined later in this file.
1769 (defvar org-agenda-restrict) ; defined later in this file.
1770 (defvar org-agenda-follow-mode nil)
1771 (defvar org-agenda-entry-text-mode nil)
1772 (defvar org-agenda-clockreport-mode nil)
1773 (defvar org-agenda-show-log nil)
1774 (defvar org-agenda-redo-command nil)
1775 (defvar org-agenda-query-string nil)
1776 (defvar org-agenda-mode-hook nil
1777 "Hook for `org-agenda-mode', run after the mode is turned on.")
1778 (defvar org-agenda-type nil)
1779 (defvar org-agenda-force-single-file nil)
1780 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1782 (defun org-agenda-mode ()
1783 "Mode for time-sorted view on action items in Org-mode files.
1785 The following commands are available:
1787 \\{org-agenda-mode-map}"
1788 (interactive)
1789 (kill-all-local-variables)
1790 (setq org-agenda-undo-list nil
1791 org-agenda-pending-undo-list nil
1792 org-agenda-bulk-marked-entries nil)
1793 (setq major-mode 'org-agenda-mode)
1794 ;; Keep global-font-lock-mode from turning on font-lock-mode
1795 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1796 (setq mode-name "Org-Agenda")
1797 (use-local-map org-agenda-mode-map)
1798 (easy-menu-add org-agenda-menu)
1799 (if org-startup-truncated (setq truncate-lines t))
1800 (org-set-local 'line-move-visual nil)
1801 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1802 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1803 ;; Make sure properties are removed when copying text
1804 (when (boundp 'buffer-substring-filters)
1805 (org-set-local 'buffer-substring-filters
1806 (cons (lambda (x)
1807 (set-text-properties 0 (length x) nil x) x)
1808 buffer-substring-filters)))
1809 (unless org-agenda-keep-modes
1810 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1811 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1812 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1813 org-agenda-show-log org-agenda-start-with-log-mode))
1815 (easy-menu-change
1816 '("Agenda") "Agenda Files"
1817 (append
1818 (list
1819 (vector
1820 (if (get 'org-agenda-files 'org-restrict)
1821 "Restricted to single file"
1822 "Edit File List")
1823 '(org-edit-agenda-file-list)
1824 (not (get 'org-agenda-files 'org-restrict)))
1825 "--")
1826 (mapcar 'org-file-menu-entry (org-agenda-files))))
1827 (org-agenda-set-mode-name)
1828 (apply
1829 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1830 (list 'org-agenda-mode-hook)))
1832 (substitute-key-definition 'undo 'org-agenda-undo
1833 org-agenda-mode-map global-map)
1834 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1835 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1836 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1837 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1838 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1839 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1840 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1841 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1842 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1843 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1844 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1845 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1846 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1847 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1848 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1849 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1850 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1851 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1852 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1853 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1854 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1855 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1856 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1857 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1858 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1859 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1860 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1861 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1862 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1863 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1864 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1865 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1866 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1867 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1868 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1869 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1870 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1871 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1872 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1873 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1874 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1875 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1876 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1877 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1878 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1880 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1881 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1882 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1883 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1884 (while l (org-defkey org-agenda-mode-map
1885 (int-to-string (pop l)) 'digit-argument)))
1887 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1888 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1889 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1890 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1891 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1892 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1893 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1894 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1895 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1896 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1897 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1898 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1899 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1900 'org-clock-modify-effort-estimate)
1901 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1902 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1903 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1904 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1905 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1906 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1907 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1908 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1909 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1910 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1911 (substitute-key-definition 'next-line 'org-agenda-next-line
1912 org-agenda-mode-map global-map)
1913 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1914 org-agenda-mode-map global-map)
1915 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1916 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1917 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1918 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1919 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1920 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1921 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1922 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1923 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1924 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1925 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1926 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1927 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1928 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1929 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1930 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1931 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1932 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1933 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1934 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1935 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1936 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1937 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1938 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1939 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1940 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1941 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1942 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1943 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1944 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1946 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1947 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1948 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1949 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1950 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1951 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1952 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1953 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1954 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1955 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1957 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1958 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1959 (when org-agenda-mouse-1-follows-link
1960 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1961 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1962 '("Agenda"
1963 ("Agenda Files")
1964 "--"
1965 ("Agenda Dates"
1966 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1967 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1968 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1969 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1970 "--"
1971 ("View"
1972 ["Day View" org-agenda-day-view
1973 :active (org-agenda-check-type nil 'agenda)
1974 :style radio :selected (eq org-agenda-current-span 'day)
1975 :keys "v d (or just d)"]
1976 ["Week View" org-agenda-week-view
1977 :active (org-agenda-check-type nil 'agenda)
1978 :style radio :selected (eq org-agenda-current-span 'week)
1979 :keys "v w (or just w)"]
1980 ["Month View" org-agenda-month-view
1981 :active (org-agenda-check-type nil 'agenda)
1982 :style radio :selected (eq org-agenda-current-span 'month)
1983 :keys "v m"]
1984 ["Year View" org-agenda-year-view
1985 :active (org-agenda-check-type nil 'agenda)
1986 :style radio :selected (eq org-agenda-current-span 'year)
1987 :keys "v y"]
1988 "--"
1989 ["Include Diary" org-agenda-toggle-diary
1990 :style toggle :selected org-agenda-include-diary
1991 :active (org-agenda-check-type nil 'agenda)]
1992 ["Include Deadlines" org-agenda-toggle-deadlines
1993 :style toggle :selected org-agenda-include-deadlines
1994 :active (org-agenda-check-type nil 'agenda)]
1995 ["Use Time Grid" org-agenda-toggle-time-grid
1996 :style toggle :selected org-agenda-use-time-grid
1997 :active (org-agenda-check-type nil 'agenda)]
1998 "--"
1999 ["Show clock report" org-agenda-clockreport-mode
2000 :style toggle :selected org-agenda-clockreport-mode
2001 :active (org-agenda-check-type nil 'agenda)]
2002 ["Show some entry text" org-agenda-entry-text-mode
2003 :style toggle :selected org-agenda-entry-text-mode
2004 :active t]
2005 "--"
2006 ["Show Logbook entries" org-agenda-log-mode
2007 :style toggle :selected org-agenda-show-log
2008 :active (org-agenda-check-type nil 'agenda 'timeline)
2009 :keys "v l (or just l)"]
2010 ["Include archived trees" org-agenda-archives-mode
2011 :style toggle :selected org-agenda-archives-mode :active t
2012 :keys "v a"]
2013 ["Include archive files" (org-agenda-archives-mode t)
2014 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2015 :keys "v A"]
2016 "--"
2017 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2018 ["Write view to file" org-write-agenda t]
2019 ["Rebuild buffer" org-agenda-redo t]
2020 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2021 "--"
2022 ["Show original entry" org-agenda-show t]
2023 ["Go To (other window)" org-agenda-goto t]
2024 ["Go To (this window)" org-agenda-switch-to t]
2025 ["Follow Mode" org-agenda-follow-mode
2026 :style toggle :selected org-agenda-follow-mode :active t]
2027 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2028 "--"
2029 ("TODO"
2030 ["Cycle TODO" org-agenda-todo t]
2031 ["Next TODO set" org-agenda-todo-nextset t]
2032 ["Previous TODO set" org-agenda-todo-previousset t]
2033 ["Add note" org-agenda-add-note t])
2034 ("Archive/Refile/Delete"
2035 ["Archive default" org-agenda-archive-default t]
2036 ["Archive default" org-agenda-archive-default-with-confirmation t]
2037 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2038 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2039 ["Archive subtree" org-agenda-archive t]
2040 "--"
2041 ["Refile" org-agenda-refile t]
2042 "--"
2043 ["Delete subtree" org-agenda-kill t])
2044 ("Bulk action"
2045 ["Mark entry" org-agenda-bulk-mark t]
2046 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2047 ["Unmark entry" org-agenda-bulk-unmark t]
2048 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2049 ["Act on all marked" org-agenda-bulk-action t]
2050 "--"
2051 ("Tags and Properties"
2052 ["Show all Tags" org-agenda-show-tags t]
2053 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2054 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2055 "--"
2056 ["Column View" org-columns t])
2057 ("Deadline/Schedule"
2058 ["Schedule" org-agenda-schedule t]
2059 ["Set Deadline" org-agenda-deadline t]
2060 "--"
2061 ["Mark item" org-agenda-action :active t :keys "k m"]
2062 ["Show mark item" org-agenda-action :active t :keys "k v"]
2063 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2064 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2065 "--"
2066 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2067 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2068 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2069 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2070 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2071 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2072 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2073 ("Clock and Effort"
2074 ["Clock in" org-agenda-clock-in t]
2075 ["Clock out" org-agenda-clock-out t]
2076 ["Clock cancel" org-agenda-clock-cancel t]
2077 ["Goto running clock" org-clock-goto t]
2078 "--"
2079 ["Set Effort" org-agenda-set-effort t]
2080 ["Change clocked effort" org-clock-modify-effort-estimate
2081 (org-clock-is-active)])
2082 ("Priority"
2083 ["Set Priority" org-agenda-priority t]
2084 ["Increase Priority" org-agenda-priority-up t]
2085 ["Decrease Priority" org-agenda-priority-down t]
2086 ["Show Priority" org-agenda-show-priority t])
2087 ("Calendar/Diary"
2088 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2089 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2090 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2091 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2092 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2093 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2094 "--"
2095 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2096 "--"
2097 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2098 "--"
2099 ("MobileOrg"
2100 ["Push Files and Views" org-mobile-push t]
2101 ["Get Captured and Flagged" org-mobile-pull t]
2102 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2103 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2104 "--"
2105 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2106 "--"
2107 ["Quit" org-agenda-quit t]
2108 ["Exit and Release Buffers" org-agenda-exit t]
2111 ;;; Agenda undo
2113 (defvar org-agenda-allow-remote-undo t
2114 "Non-nil means allow remote undo from the agenda buffer.")
2115 (defvar org-agenda-undo-list nil
2116 "List of undoable operations in the agenda since last refresh.")
2117 (defvar org-agenda-undo-has-started-in nil
2118 "Buffers that have already seen `undo-start' in the current undo sequence.")
2119 (defvar org-agenda-pending-undo-list nil
2120 "In a series of undo commands, this is the list of remaining undo items.")
2122 (defun org-agenda-undo ()
2123 "Undo a remote editing step in the agenda.
2124 This undoes changes both in the agenda buffer and in the remote buffer
2125 that have been changed along."
2126 (interactive)
2127 (or org-agenda-allow-remote-undo
2128 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2129 (if (not (eq this-command last-command))
2130 (setq org-agenda-undo-has-started-in nil
2131 org-agenda-pending-undo-list org-agenda-undo-list))
2132 (if (not org-agenda-pending-undo-list)
2133 (error "No further undo information"))
2134 (let* ((entry (pop org-agenda-pending-undo-list))
2135 buf line cmd rembuf)
2136 (setq cmd (pop entry) line (pop entry))
2137 (setq rembuf (nth 2 entry))
2138 (org-with-remote-undo rembuf
2139 (while (bufferp (setq buf (pop entry)))
2140 (if (pop entry)
2141 (with-current-buffer buf
2142 (let ((last-undo-buffer buf)
2143 (inhibit-read-only t))
2144 (unless (memq buf org-agenda-undo-has-started-in)
2145 (push buf org-agenda-undo-has-started-in)
2146 (make-local-variable 'pending-undo-list)
2147 (undo-start))
2148 (while (and pending-undo-list
2149 (listp pending-undo-list)
2150 (not (car pending-undo-list)))
2151 (pop pending-undo-list))
2152 (undo-more 1))))))
2153 (org-goto-line line)
2154 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2156 (defun org-verify-change-for-undo (l1 l2)
2157 "Verify that a real change occurred between the undo lists L1 and L2."
2158 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2159 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2160 (not (eq l1 l2)))
2162 ;;; Agenda dispatch
2164 (defvar org-agenda-restrict nil)
2165 (defvar org-agenda-restrict-begin (make-marker))
2166 (defvar org-agenda-restrict-end (make-marker))
2167 (defvar org-agenda-last-dispatch-buffer nil)
2168 (defvar org-agenda-overriding-restriction nil)
2170 ;;;###autoload
2171 (defun org-agenda (&optional arg keys restriction)
2172 "Dispatch agenda commands to collect entries to the agenda buffer.
2173 Prompts for a command to execute. Any prefix arg will be passed
2174 on to the selected command. The default selections are:
2176 a Call `org-agenda-list' to display the agenda for current day or week.
2177 t Call `org-todo-list' to display the global todo list.
2178 T Call `org-todo-list' to display the global todo list, select only
2179 entries with a specific TODO keyword (the user gets a prompt).
2180 m Call `org-tags-view' to display headlines with tags matching
2181 a condition (the user is prompted for the condition).
2182 M Like `m', but select only TODO entries, no ordinary headlines.
2183 L Create a timeline for the current buffer.
2184 e Export views to associated files.
2185 s Search entries for keywords.
2186 / Multi occur across all agenda files and also files listed
2187 in `org-agenda-text-search-extra-files'.
2188 < Restrict agenda commands to buffer, subtree, or region.
2189 Press several times to get the desired effect.
2190 > Remove a previous restriction.
2191 # List \"stuck\" projects.
2192 ! Configure what \"stuck\" means.
2193 C Configure custom agenda commands.
2195 More commands can be added by configuring the variable
2196 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2197 searches can be pre-defined in this way.
2199 If the current buffer is in Org-mode and visiting a file, you can also
2200 first press `<' once to indicate that the agenda should be temporarily
2201 \(until the next use of \\[org-agenda]) restricted to the current file.
2202 Pressing `<' twice means to restrict to the current subtree or region
2203 \(if active)."
2204 (interactive "P")
2205 (catch 'exit
2206 (let* ((prefix-descriptions nil)
2207 (org-agenda-window-setup (if (equal (buffer-name)
2208 org-agenda-buffer-name)
2209 'current-window
2210 org-agenda-window-setup))
2211 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2212 (org-agenda-custom-commands
2213 ;; normalize different versions
2214 (delq nil
2215 (mapcar
2216 (lambda (x)
2217 (cond ((stringp (cdr x))
2218 (push x prefix-descriptions)
2219 nil)
2220 ((stringp (nth 1 x)) x)
2221 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2222 (t (cons (car x) (cons "" (cdr x))))))
2223 org-agenda-custom-commands)))
2224 (buf (current-buffer))
2225 (bfn (buffer-file-name (buffer-base-buffer)))
2226 entry key type match lprops ans)
2227 ;; Turn off restriction unless there is an overriding one,
2228 (unless org-agenda-overriding-restriction
2229 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2230 ;; There is a request to keep the file list in place
2231 (put 'org-agenda-files 'org-restrict nil))
2232 (setq org-agenda-restrict nil)
2233 (move-marker org-agenda-restrict-begin nil)
2234 (move-marker org-agenda-restrict-end nil))
2235 ;; Delete old local properties
2236 (put 'org-agenda-redo-command 'org-lprops nil)
2237 ;; Delete previously set last-arguments
2238 (put 'org-agenda-redo-command 'last-args nil)
2239 ;; Remember where this call originated
2240 (setq org-agenda-last-dispatch-buffer (current-buffer))
2241 (unless keys
2242 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2243 keys (car ans)
2244 restriction (cdr ans)))
2245 ;; Establish the restriction, if any
2246 (when (and (not org-agenda-overriding-restriction) restriction)
2247 (put 'org-agenda-files 'org-restrict (list bfn))
2248 (cond
2249 ((eq restriction 'region)
2250 (setq org-agenda-restrict t)
2251 (move-marker org-agenda-restrict-begin (region-beginning))
2252 (move-marker org-agenda-restrict-end (region-end)))
2253 ((eq restriction 'subtree)
2254 (save-excursion
2255 (setq org-agenda-restrict t)
2256 (org-back-to-heading t)
2257 (move-marker org-agenda-restrict-begin (point))
2258 (move-marker org-agenda-restrict-end
2259 (progn (org-end-of-subtree t)))))))
2261 ;; For example the todo list should not need it (but does...)
2262 (cond
2263 ((setq entry (assoc keys org-agenda-custom-commands))
2264 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2265 (progn
2266 (setq type (nth 2 entry) match (eval (nth 3 entry))
2267 lprops (nth 4 entry))
2268 (put 'org-agenda-redo-command 'org-lprops lprops)
2269 (cond
2270 ((eq type 'agenda)
2271 (org-let lprops '(org-agenda-list current-prefix-arg)))
2272 ((eq type 'alltodo)
2273 (org-let lprops '(org-todo-list current-prefix-arg)))
2274 ((eq type 'search)
2275 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2276 ((eq type 'stuck)
2277 (org-let lprops '(org-agenda-list-stuck-projects
2278 current-prefix-arg)))
2279 ((eq type 'tags)
2280 (org-let lprops '(org-tags-view current-prefix-arg match)))
2281 ((eq type 'tags-todo)
2282 (org-let lprops '(org-tags-view '(4) match)))
2283 ((eq type 'todo)
2284 (org-let lprops '(org-todo-list match)))
2285 ((eq type 'tags-tree)
2286 (org-check-for-org-mode)
2287 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2288 ((eq type 'todo-tree)
2289 (org-check-for-org-mode)
2290 (org-let lprops
2291 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2292 (regexp-quote match) "\\>"))))
2293 ((eq type 'occur-tree)
2294 (org-check-for-org-mode)
2295 (org-let lprops '(org-occur match)))
2296 ((functionp type)
2297 (org-let lprops '(funcall type match)))
2298 ((fboundp type)
2299 (org-let lprops '(funcall type match)))
2300 (t (error "Invalid custom agenda command type %s" type))))
2301 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2302 ((equal keys "C")
2303 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2304 (customize-variable 'org-agenda-custom-commands))
2305 ((equal keys "a") (call-interactively 'org-agenda-list))
2306 ((equal keys "s") (call-interactively 'org-search-view))
2307 ((equal keys "t") (call-interactively 'org-todo-list))
2308 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2309 ((equal keys "m") (call-interactively 'org-tags-view))
2310 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2311 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2312 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2313 (org-add-hook
2314 'post-command-hook
2315 (lambda ()
2316 (unless (current-message)
2317 (let* ((m (org-agenda-get-any-marker))
2318 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2319 (when note
2320 (message (concat
2321 "FLAGGING-NOTE ([?] for more info): "
2322 (org-add-props
2323 (replace-regexp-in-string
2324 "\\\\n" "//"
2325 (copy-sequence note))
2326 nil 'face 'org-warning)))))))
2327 t t))
2328 ((equal keys "L")
2329 (unless (eq major-mode 'org-mode)
2330 (error "This is not an Org-mode file"))
2331 (unless restriction
2332 (put 'org-agenda-files 'org-restrict (list bfn))
2333 (org-call-with-arg 'org-timeline arg)))
2334 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2335 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2336 ((equal keys "!") (customize-variable 'org-stuck-projects))
2337 (t (error "Invalid agenda key"))))))
2339 (defun org-agenda-append-agenda ()
2340 "Append another agenda view to the current one.
2341 This function allows interactive building of block agendas.
2342 Agenda views are separated by `org-agenda-block-separator'."
2343 (interactive)
2344 (unless (string= (buffer-name) org-agenda-buffer-name)
2345 (error "Can only append from within agenda buffer"))
2346 (let ((org-agenda-multi t))
2347 (org-agenda)
2348 (widen)))
2350 (defun org-agenda-normalize-custom-commands (cmds)
2351 (delq nil
2352 (mapcar
2353 (lambda (x)
2354 (cond ((stringp (cdr x)) nil)
2355 ((stringp (nth 1 x)) x)
2356 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2357 (t (cons (car x) (cons "" (cdr x))))))
2358 cmds)))
2360 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2361 "The user interface for selecting an agenda command."
2362 (catch 'exit
2363 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2364 (restrict-ok (and bfn (eq major-mode 'org-mode)))
2365 (region-p (org-region-active-p))
2366 (custom org-agenda-custom-commands)
2367 (selstring "")
2368 restriction second-time
2369 c entry key type match prefixes rmheader header-end custom1 desc
2370 line lines left right n n1)
2371 (save-window-excursion
2372 (delete-other-windows)
2373 (org-switch-to-buffer-other-window " *Agenda Commands*")
2374 (erase-buffer)
2375 (insert (eval-when-compile
2376 (let ((header
2378 Press key for an agenda command: < Buffer, subtree/region restriction
2379 -------------------------------- > Remove restriction
2380 a Agenda for current week or day e Export agenda views
2381 t List of all TODO entries T Entries with special TODO kwd
2382 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2383 L Timeline for current buffer # List stuck projects (!=configure)
2384 s Search for keywords C Configure custom agenda commands
2385 / Multi-occur ? Find :FLAGGED: entries
2387 (start 0))
2388 (while (string-match
2389 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2390 header start)
2391 (setq start (match-end 0))
2392 (add-text-properties (match-beginning 2) (match-end 2)
2393 '(face bold) header))
2394 header)))
2395 (setq header-end (move-marker (make-marker) (point)))
2396 (while t
2397 (setq custom1 custom)
2398 (when (eq rmheader t)
2399 (org-goto-line 1)
2400 (re-search-forward ":" nil t)
2401 (delete-region (match-end 0) (point-at-eol))
2402 (forward-char 1)
2403 (looking-at "-+")
2404 (delete-region (match-end 0) (point-at-eol))
2405 (move-marker header-end (match-end 0)))
2406 (goto-char header-end)
2407 (delete-region (point) (point-max))
2409 ;; Produce all the lines that describe custom commands and prefixes
2410 (setq lines nil)
2411 (while (setq entry (pop custom1))
2412 (setq key (car entry) desc (nth 1 entry)
2413 type (nth 2 entry)
2414 match (nth 3 entry))
2415 (if (> (length key) 1)
2416 (add-to-list 'prefixes (string-to-char key))
2417 (setq line
2418 (format
2419 "%-4s%-14s"
2420 (org-add-props (copy-sequence key)
2421 '(face bold))
2422 (cond
2423 ((string-match "\\S-" desc) desc)
2424 ((eq type 'agenda) "Agenda for current week or day")
2425 ((eq type 'alltodo) "List of all TODO entries")
2426 ((eq type 'search) "Word search")
2427 ((eq type 'stuck) "List of stuck projects")
2428 ((eq type 'todo) "TODO keyword")
2429 ((eq type 'tags) "Tags query")
2430 ((eq type 'tags-todo) "Tags (TODO)")
2431 ((eq type 'tags-tree) "Tags tree")
2432 ((eq type 'todo-tree) "TODO kwd tree")
2433 ((eq type 'occur-tree) "Occur tree")
2434 ((functionp type) (if (symbolp type)
2435 (symbol-name type)
2436 "Lambda expression"))
2437 (t "???"))))
2438 (if org-agenda-menu-show-matcher
2439 (setq line
2440 (concat line ": "
2441 (cond
2442 ((stringp match)
2443 (setq match (copy-sequence match))
2444 (org-add-props match nil 'face 'org-warning))
2445 (match
2446 (format "set of %d commands" (length match)))
2447 (t ""))))
2448 (if (org-string-nw-p match)
2449 (add-text-properties
2450 0 (length line) (list 'help-echo
2451 (concat "Matcher: "match)) line)))
2452 (push line lines)))
2453 (setq lines (nreverse lines))
2454 (when prefixes
2455 (mapc (lambda (x)
2456 (push
2457 (format "%s %s"
2458 (org-add-props (char-to-string x)
2459 nil 'face 'bold)
2460 (or (cdr (assoc (concat selstring
2461 (char-to-string x))
2462 prefix-descriptions))
2463 "Prefix key"))
2464 lines))
2465 prefixes))
2467 ;; Check if we should display in two columns
2468 (if org-agenda-menu-two-column
2469 (progn
2470 (setq n (length lines)
2471 n1 (+ (/ n 2) (mod n 2))
2472 right (nthcdr n1 lines)
2473 left (copy-sequence lines))
2474 (setcdr (nthcdr (1- n1) left) nil))
2475 (setq left lines right nil))
2476 (while left
2477 (insert "\n" (pop left))
2478 (when right
2479 (if (< (current-column) 40)
2480 (move-to-column 40 t)
2481 (insert " "))
2482 (insert (pop right))))
2484 ;; Make the window the right size
2485 (goto-char (point-min))
2486 (if second-time
2487 (if (not (pos-visible-in-window-p (point-max)))
2488 (org-fit-window-to-buffer))
2489 (setq second-time t)
2490 (org-fit-window-to-buffer))
2492 ;; Ask for selection
2493 (message "Press key for agenda command%s:"
2494 (if (or restrict-ok org-agenda-overriding-restriction)
2495 (if org-agenda-overriding-restriction
2496 " (restriction lock active)"
2497 (if restriction
2498 (format " (restricted to %s)" restriction)
2499 " (unrestricted)"))
2500 ""))
2501 (setq c (read-char-exclusive))
2502 (message "")
2503 (cond
2504 ((assoc (char-to-string c) custom)
2505 (setq selstring (concat selstring (char-to-string c)))
2506 (throw 'exit (cons selstring restriction)))
2507 ((memq c prefixes)
2508 (setq selstring (concat selstring (char-to-string c))
2509 prefixes nil
2510 rmheader (or rmheader t)
2511 custom (delq nil (mapcar
2512 (lambda (x)
2513 (if (or (= (length (car x)) 1)
2514 (/= (string-to-char (car x)) c))
2516 (cons (substring (car x) 1) (cdr x))))
2517 custom))))
2518 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2519 (message "Restriction is only possible in Org-mode buffers")
2520 (ding) (sit-for 1))
2521 ((eq c ?1)
2522 (org-agenda-remove-restriction-lock 'noupdate)
2523 (setq restriction 'buffer))
2524 ((eq c ?0)
2525 (org-agenda-remove-restriction-lock 'noupdate)
2526 (setq restriction (if region-p 'region 'subtree)))
2527 ((eq c ?<)
2528 (org-agenda-remove-restriction-lock 'noupdate)
2529 (setq restriction
2530 (cond
2531 ((eq restriction 'buffer)
2532 (if region-p 'region 'subtree))
2533 ((memq restriction '(subtree region))
2534 nil)
2535 (t 'buffer))))
2536 ((eq c ?>)
2537 (org-agenda-remove-restriction-lock 'noupdate)
2538 (setq restriction nil))
2539 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2540 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2541 ((and (> (length selstring) 0) (eq c ?\d))
2542 (delete-window)
2543 (org-agenda-get-restriction-and-command prefix-descriptions))
2545 ((equal c ?q) (error "Abort"))
2546 (t (error "Invalid key %c" c))))))))
2548 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2549 (defvar org-agenda-last-arguments nil
2550 "The arguments of the previous call to `org-agenda'.")
2551 (defun org-agenda-run-series (name series)
2552 (org-let (nth 1 series) '(org-prepare-agenda name))
2553 (let* ((org-agenda-multi t)
2554 (redo (list 'org-agenda-run-series name (list 'quote series)))
2555 (org-agenda-overriding-arguments
2556 (or org-agenda-overriding-arguments
2557 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2558 (get 'org-agenda-redo-command 'last-args))))
2559 (cmds (car series))
2560 (gprops (nth 1 series))
2561 match ;; The byte compiler incorrectly complains about this. Keep it!
2562 cmd type lprops)
2563 (while (setq cmd (pop cmds))
2564 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2565 (cond
2566 ((eq type 'agenda)
2567 (org-let2 gprops lprops
2568 '(call-interactively 'org-agenda-list)))
2569 ((eq type 'alltodo)
2570 (org-let2 gprops lprops
2571 '(call-interactively 'org-todo-list)))
2572 ((eq type 'search)
2573 (org-let2 gprops lprops
2574 '(org-search-view current-prefix-arg match nil)))
2575 ((eq type 'stuck)
2576 (org-let2 gprops lprops
2577 '(call-interactively 'org-agenda-list-stuck-projects)))
2578 ((eq type 'tags)
2579 (org-let2 gprops lprops
2580 '(org-tags-view current-prefix-arg match)))
2581 ((eq type 'tags-todo)
2582 (org-let2 gprops lprops
2583 '(org-tags-view '(4) match)))
2584 ((eq type 'todo)
2585 (org-let2 gprops lprops
2586 '(org-todo-list match)))
2587 ((fboundp type)
2588 (org-let2 gprops lprops
2589 '(funcall type match)))
2590 (t (error "Invalid type in command series"))))
2591 (widen)
2592 (setq org-agenda-redo-command redo)
2593 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2594 (goto-char (point-min)))
2595 (org-fit-agenda-window)
2596 (org-let (nth 1 series) '(org-finalize-agenda)))
2598 ;;;###autoload
2599 (defmacro org-batch-agenda (cmd-key &rest parameters)
2600 "Run an agenda command in batch mode and send the result to STDOUT.
2601 If CMD-KEY is a string of length 1, it is used as a key in
2602 `org-agenda-custom-commands' and triggers this command. If it is a
2603 longer string it is used as a tags/todo match string.
2604 Parameters are alternating variable names and values that will be bound
2605 before running the agenda command."
2606 (org-eval-in-environment (org-make-parameter-alist parameters)
2607 (if (> (length cmd-key) 2)
2608 (org-tags-view nil cmd-key)
2609 (org-agenda nil cmd-key)))
2610 (set-buffer org-agenda-buffer-name)
2611 (princ (org-encode-for-stdout (buffer-string))))
2612 (def-edebug-spec org-batch-agenda (form &rest sexp))
2614 ;(defun org-encode-for-stdout (string)
2615 ; (if (fboundp 'encode-coding-string)
2616 ; (encode-coding-string string buffer-file-coding-system)
2617 ; string))
2619 (defun org-encode-for-stdout (string)
2620 string)
2622 (defvar org-agenda-info nil)
2624 ;;;###autoload
2625 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2626 "Run an agenda command in batch mode and send the result to STDOUT.
2627 If CMD-KEY is a string of length 1, it is used as a key in
2628 `org-agenda-custom-commands' and triggers this command. If it is a
2629 longer string it is used as a tags/todo match string.
2630 Parameters are alternating variable names and values that will be bound
2631 before running the agenda command.
2633 The output gives a line for each selected agenda item. Each
2634 item is a list of comma-separated values, like this:
2636 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2638 category The category of the item
2639 head The headline, without TODO kwd, TAGS and PRIORITY
2640 type The type of the agenda entry, can be
2641 todo selected in TODO match
2642 tagsmatch selected in tags match
2643 diary imported from diary
2644 deadline a deadline on given date
2645 scheduled scheduled on given date
2646 timestamp entry has timestamp on given date
2647 closed entry was closed on given date
2648 upcoming-deadline warning about deadline
2649 past-scheduled forwarded scheduled item
2650 block entry has date block including g. date
2651 todo The todo keyword, if any
2652 tags All tags including inherited ones, separated by colons
2653 date The relevant date, like 2007-2-14
2654 time The time, like 15:00-16:50
2655 extra Sting with extra planning info
2656 priority-l The priority letter if any was given
2657 priority-n The computed numerical priority
2658 agenda-day The day in the agenda where this is listed"
2659 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2660 (org-make-parameter-alist parameters))
2661 (if (> (length cmd-key) 2)
2662 (org-tags-view nil cmd-key)
2663 (org-agenda nil cmd-key)))
2664 (set-buffer org-agenda-buffer-name)
2665 (let* ((lines (org-split-string (buffer-string) "\n"))
2666 line)
2667 (while (setq line (pop lines))
2668 (catch 'next
2669 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2670 (setq org-agenda-info
2671 (org-fix-agenda-info (text-properties-at 0 line)))
2672 (princ
2673 (org-encode-for-stdout
2674 (mapconcat 'org-agenda-export-csv-mapper
2675 '(org-category txt type todo tags date time extra
2676 priority-letter priority agenda-day)
2677 ",")))
2678 (princ "\n")))))
2679 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2681 (defun org-fix-agenda-info (props)
2682 "Make sure all properties on an agenda item have a canonical form.
2683 This ensures the export commands can easily use it."
2684 (let (tmp re)
2685 (when (setq tmp (plist-get props 'tags))
2686 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2687 (when (setq tmp (plist-get props 'date))
2688 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2689 (let ((calendar-date-display-form '(year "-" month "-" day)))
2690 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2692 (setq tmp (calendar-date-string tmp)))
2693 (setq props (plist-put props 'date tmp)))
2694 (when (setq tmp (plist-get props 'day))
2695 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2696 (let ((calendar-date-display-form '(year "-" month "-" day)))
2697 (setq tmp (calendar-date-string tmp)))
2698 (setq props (plist-put props 'day tmp))
2699 (setq props (plist-put props 'agenda-day tmp)))
2700 (when (setq tmp (plist-get props 'txt))
2701 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2702 (plist-put props 'priority-letter (match-string 1 tmp))
2703 (setq tmp (replace-match "" t t tmp)))
2704 (when (and (setq re (plist-get props 'org-todo-regexp))
2705 (setq re (concat "\\`\\.*" re " ?"))
2706 (string-match re tmp))
2707 (plist-put props 'todo (match-string 1 tmp))
2708 (setq tmp (replace-match "" t t tmp)))
2709 (plist-put props 'txt tmp)))
2710 props)
2712 (defun org-agenda-export-csv-mapper (prop)
2713 (let ((res (plist-get org-agenda-info prop)))
2714 (setq res
2715 (cond
2716 ((not res) "")
2717 ((stringp res) res)
2718 (t (prin1-to-string res))))
2719 (while (string-match "," res)
2720 (setq res (replace-match ";" t t res)))
2721 (org-trim res)))
2724 ;;;###autoload
2725 (defun org-store-agenda-views (&rest parameters)
2726 (interactive)
2727 (eval (list 'org-batch-store-agenda-views)))
2729 ;;;###autoload
2730 (defmacro org-batch-store-agenda-views (&rest parameters)
2731 "Run all custom agenda commands that have a file argument."
2732 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2733 (pop-up-frames nil)
2734 (dir default-directory)
2735 (pars (org-make-parameter-alist parameters))
2736 cmd thiscmdkey files opts cmd-or-set)
2737 (save-window-excursion
2738 (while cmds
2739 (setq cmd (pop cmds)
2740 thiscmdkey (car cmd)
2741 cmd-or-set (nth 2 cmd)
2742 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2743 files (nth (if (listp cmd-or-set) 4 5) cmd))
2744 (if (stringp files) (setq files (list files)))
2745 (when files
2746 (org-eval-in-environment (append org-agenda-exporter-settings
2747 opts pars)
2748 (org-agenda nil thiscmdkey))
2749 (set-buffer org-agenda-buffer-name)
2750 (while files
2751 (org-eval-in-environment (append org-agenda-exporter-settings
2752 opts pars)
2753 (org-write-agenda (expand-file-name (pop files) dir) nil t)))
2754 (and (get-buffer org-agenda-buffer-name)
2755 (kill-buffer org-agenda-buffer-name)))))))
2756 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
2758 (defun org-agenda-mark-header-line (pos)
2759 "Mark the line at POS as an agenda structure header."
2760 (save-excursion
2761 (goto-char pos)
2762 (put-text-property (point-at-bol) (point-at-eol)
2763 'org-agenda-structural-header t)
2764 (when org-agenda-title-append
2765 (put-text-property (point-at-bol) (point-at-eol)
2766 'org-agenda-title-append org-agenda-title-append))))
2768 (defvar org-mobile-creating-agendas)
2769 (defun org-write-agenda (file &optional open nosettings)
2770 "Write the current buffer (an agenda view) as a file.
2771 Depending on the extension of the file name, plain text (.txt),
2772 HTML (.html or .htm) or Postscript (.ps) is produced.
2773 If the extension is .ics, run icalendar export over all files used
2774 to construct the agenda and limit the export to entries listed in the
2775 agenda now.
2776 With prefix argument OPEN, open the new file immediately.
2777 If NOSETTINGS is given, do not scope the settings of
2778 `org-agenda-exporter-settings' into the export commands. This is used when
2779 the settings have already been scoped and we do not wish to overrule other,
2780 higher priority settings."
2781 (interactive "FWrite agenda to file: \nP")
2782 (if (not (file-writable-p file))
2783 (error "Cannot write agenda to file %s" file))
2784 (org-let (if nosettings nil org-agenda-exporter-settings)
2785 '(save-excursion
2786 (save-window-excursion
2787 (org-agenda-mark-filtered-text)
2788 (let ((bs (copy-sequence (buffer-string))) beg)
2789 (org-agenda-unmark-filtered-text)
2790 (with-temp-buffer
2791 (rename-buffer "Agenda View" t)
2792 (set-buffer-modified-p nil)
2793 (insert bs)
2794 (org-agenda-remove-marked-text 'org-filtered)
2795 (while (setq beg (text-property-any (point-min) (point-max)
2796 'org-filtered t))
2797 (delete-region
2798 beg (or (next-single-property-change beg 'org-filtered)
2799 (point-max))))
2800 (run-hooks 'org-agenda-before-write-hook)
2801 (cond
2802 ((org-bound-and-true-p org-mobile-creating-agendas)
2803 (org-mobile-write-agenda-for-mobile file))
2804 ((string-match "\\.html?\\'" file)
2805 (require 'htmlize)
2806 (set-buffer (htmlize-buffer (current-buffer)))
2808 (when (and org-agenda-export-html-style
2809 (string-match "<style>" org-agenda-export-html-style))
2810 ;; replace <style> section with org-agenda-export-html-style
2811 (goto-char (point-min))
2812 (kill-region (- (search-forward "<style") 6)
2813 (search-forward "</style>"))
2814 (insert org-agenda-export-html-style))
2815 (write-file file)
2816 (kill-buffer (current-buffer))
2817 (message "HTML written to %s" file))
2818 ((string-match "\\.ps\\'" file)
2819 (require 'ps-print)
2820 (ps-print-buffer-with-faces file)
2821 (message "Postscript written to %s" file))
2822 ((string-match "\\.pdf\\'" file)
2823 (require 'ps-print)
2824 (ps-print-buffer-with-faces
2825 (concat (file-name-sans-extension file) ".ps"))
2826 (call-process "ps2pdf" nil nil nil
2827 (expand-file-name
2828 (concat (file-name-sans-extension file) ".ps"))
2829 (expand-file-name file))
2830 (delete-file (concat (file-name-sans-extension file) ".ps"))
2831 (message "PDF written to %s" file))
2832 ((string-match "\\.ics\\'" file)
2833 (require 'org-icalendar)
2834 (let ((org-agenda-marker-table
2835 (org-create-marker-find-array
2836 (org-agenda-collect-markers)))
2837 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2838 (org-combined-agenda-icalendar-file file))
2839 (apply 'org-export-icalendar 'combine
2840 (org-agenda-files nil 'ifmode))))
2842 (let ((bs (buffer-string)))
2843 (find-file file)
2844 (erase-buffer)
2845 (insert bs)
2846 (save-buffer 0)
2847 (kill-buffer (current-buffer))
2848 (message "Plain text written to %s" file))))))))
2849 (set-buffer org-agenda-buffer-name))
2850 (when open (org-open-file file)))
2852 (defvar org-agenda-filter-overlays nil)
2854 (defun org-agenda-mark-filtered-text ()
2855 "Mark all text hidden by filtering with a text property."
2856 (let ((inhibit-read-only t))
2857 (mapc
2858 (lambda (o)
2859 (when (equal (overlay-buffer o) (current-buffer))
2860 (put-text-property
2861 (overlay-start o) (overlay-end o)
2862 'org-filtered t)))
2863 org-agenda-filter-overlays)))
2865 (defun org-agenda-unmark-filtered-text ()
2866 "Remove the filtering text property."
2867 (let ((inhibit-read-only t))
2868 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2870 (defun org-agenda-remove-marked-text (property &optional value)
2871 "Delete all text marked with VALUE of PROPERTY.
2872 VALUE defaults to t."
2873 (let (beg)
2874 (setq value (or value t))
2875 (while (setq beg (text-property-any (point-min) (point-max)
2876 property value))
2877 (delete-region
2878 beg (or (next-single-property-change beg 'org-filtered)
2879 (point-max))))))
2881 (defun org-agenda-add-entry-text ()
2882 "Add entry text to agenda lines.
2883 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2884 entry text following headings shown in the agenda.
2885 Drawers will be excluded, also the line with scheduling/deadline info."
2886 (when (and (> org-agenda-add-entry-text-maxlines 0)
2887 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2888 (let (m txt)
2889 (goto-char (point-min))
2890 (while (not (eobp))
2891 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2892 (beginning-of-line 2)
2893 (setq txt (org-agenda-get-some-entry-text
2894 m org-agenda-add-entry-text-maxlines " > "))
2895 (end-of-line 1)
2896 (if (string-match "\\S-" txt)
2897 (insert "\n" txt)
2898 (or (eobp) (forward-char 1))))))))
2900 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2901 &rest keep)
2902 "Extract entry text from MARKER, at most N-LINES lines.
2903 This will ignore drawers etc, just get the text.
2904 If INDENT is given, prefix every line with this string. If KEEP is
2905 given, it is a list of symbols, defining stuff that should not be
2906 removed from the entry content. Currently only `planning' is allowed here."
2907 (let (txt drawer-re kwd-time-re ind)
2908 (save-excursion
2909 (with-current-buffer (marker-buffer marker)
2910 (if (not (eq major-mode 'org-mode))
2911 (setq txt "")
2912 (save-excursion
2913 (save-restriction
2914 (widen)
2915 (goto-char marker)
2916 (end-of-line 1)
2917 (setq txt (buffer-substring
2918 (min (1+ (point)) (point-max))
2919 (progn (outline-next-heading) (point)))
2920 drawer-re org-drawer-regexp
2921 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2922 ".*\n?"))
2923 (with-temp-buffer
2924 (insert txt)
2925 (when org-agenda-add-entry-text-descriptive-links
2926 (goto-char (point-min))
2927 (while (org-activate-bracket-links (point-max))
2928 (add-text-properties (match-beginning 0) (match-end 0)
2929 '(face org-link))))
2930 (goto-char (point-min))
2931 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2932 (set-text-properties (match-beginning 0) (match-end 0)
2933 nil))
2934 (goto-char (point-min))
2935 (while (re-search-forward drawer-re nil t)
2936 (delete-region
2937 (match-beginning 0)
2938 (progn (re-search-forward
2939 "^[ \t]*:END:.*\n?" nil 'move)
2940 (point))))
2941 (unless (member 'planning keep)
2942 (goto-char (point-min))
2943 (while (re-search-forward kwd-time-re nil t)
2944 (replace-match "")))
2945 (goto-char (point-min))
2946 (when org-agenda-entry-text-exclude-regexps
2947 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2948 (while (setq re (pop re-list))
2949 (goto-char (point-min))
2950 (while (re-search-forward re nil t)
2951 (replace-match "")))))
2952 (goto-char (point-max))
2953 (skip-chars-backward " \t\n")
2954 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2956 ;; find and remove min common indentation
2957 (goto-char (point-min))
2958 (untabify (point-min) (point-max))
2959 (setq ind (org-get-indentation))
2960 (while (not (eobp))
2961 (unless (looking-at "[ \t]*$")
2962 (setq ind (min ind (org-get-indentation))))
2963 (beginning-of-line 2))
2964 (goto-char (point-min))
2965 (while (not (eobp))
2966 (unless (looking-at "[ \t]*$")
2967 (move-to-column ind)
2968 (delete-region (point-at-bol) (point)))
2969 (beginning-of-line 2))
2971 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2973 (goto-char (point-min))
2974 (when indent
2975 (while (and (not (eobp)) (re-search-forward "^" nil t))
2976 (replace-match indent t t)))
2977 (goto-char (point-min))
2978 (while (looking-at "[ \t]*\n") (replace-match ""))
2979 (goto-char (point-max))
2980 (when (> (org-current-line)
2981 n-lines)
2982 (org-goto-line (1+ n-lines))
2983 (backward-char 1))
2984 (setq txt (buffer-substring (point-min) (point)))))))))
2985 txt))
2987 (defun org-agenda-collect-markers ()
2988 "Collect the markers pointing to entries in the agenda buffer."
2989 (let (m markers)
2990 (save-excursion
2991 (goto-char (point-min))
2992 (while (not (eobp))
2993 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2994 (org-get-at-bol 'org-marker)))
2995 (push m markers))
2996 (beginning-of-line 2)))
2997 (nreverse markers)))
2999 (defun org-create-marker-find-array (marker-list)
3000 "Create a alist of files names with all marker positions in that file."
3001 (let (f tbl m a p)
3002 (while (setq m (pop marker-list))
3003 (setq p (marker-position m)
3004 f (buffer-file-name (or (buffer-base-buffer
3005 (marker-buffer m))
3006 (marker-buffer m))))
3007 (if (setq a (assoc f tbl))
3008 (push (marker-position m) (cdr a))
3009 (push (list f p) tbl)))
3010 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3011 tbl)))
3013 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3014 (defun org-check-agenda-marker-table ()
3015 "Check of the current entry is on the marker list."
3016 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3018 (and (setq a (assoc file org-agenda-marker-table))
3019 (save-match-data
3020 (save-excursion
3021 (org-back-to-heading t)
3022 (member (point) (cdr a)))))))
3024 (defun org-check-for-org-mode ()
3025 "Make sure current buffer is in org-mode. Error if not."
3026 (or (eq major-mode 'org-mode)
3027 (error "Cannot execute org-mode agenda command on buffer in %s"
3028 major-mode)))
3030 (defun org-fit-agenda-window ()
3031 "Fit the window to the buffer size."
3032 (and (memq org-agenda-window-setup '(reorganize-frame))
3033 (fboundp 'fit-window-to-buffer)
3034 (org-fit-window-to-buffer
3036 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3037 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3039 ;;; Agenda prepare and finalize
3041 (defvar org-agenda-multi nil) ; dynamically scoped
3042 (defvar org-agenda-buffer-name "*Org Agenda*")
3043 (defvar org-pre-agenda-window-conf nil)
3044 (defvar org-agenda-columns-active nil)
3045 (defvar org-agenda-name nil)
3046 (defvar org-agenda-filter nil)
3047 (defvar org-agenda-filter-while-redo nil)
3048 (defvar org-agenda-filter-preset nil
3049 "A preset of the tags filter used for secondary agenda filtering.
3050 This must be a list of strings, each string must be a single tag preceded
3051 by \"+\" or \"-\".
3052 This variable should not be set directly, but agenda custom commands can
3053 bind it in the options section. The preset filter is a global property of
3054 the entire agenda view. In a block agenda, it will not work reliably to
3055 define a filter for one of the individual blocks. You need to set it in
3056 the global options and expect it to be applied to the entire view.")
3058 (defun org-prepare-agenda (&optional name)
3059 (setq org-todo-keywords-for-agenda nil)
3060 (setq org-done-keywords-for-agenda nil)
3061 (setq org-drawers-for-agenda nil)
3062 (unless org-agenda-persistent-filter
3063 (setq org-agenda-filter nil))
3064 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3065 (if org-agenda-multi
3066 (progn
3067 (setq buffer-read-only nil)
3068 (goto-char (point-max))
3069 (unless (or (bobp) org-agenda-compact-blocks
3070 (not org-agenda-block-separator))
3071 (insert "\n"
3072 (if (stringp org-agenda-block-separator)
3073 org-agenda-block-separator
3074 (make-string (window-width) org-agenda-block-separator))
3075 "\n"))
3076 (narrow-to-region (point) (point-max)))
3077 (org-agenda-reset-markers)
3078 (setq org-agenda-contributing-files nil)
3079 (setq org-agenda-columns-active nil)
3080 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3081 (setq org-todo-keywords-for-agenda
3082 (org-uniquify org-todo-keywords-for-agenda))
3083 (setq org-done-keywords-for-agenda
3084 (org-uniquify org-done-keywords-for-agenda))
3085 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3086 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3087 (awin (get-buffer-window abuf)))
3088 (cond
3089 ((equal (current-buffer) abuf) nil)
3090 (awin (select-window awin))
3091 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3092 ((equal org-agenda-window-setup 'current-window)
3093 (org-pop-to-buffer-same-window abuf))
3094 ((equal org-agenda-window-setup 'other-window)
3095 (org-switch-to-buffer-other-window abuf))
3096 ((equal org-agenda-window-setup 'other-frame)
3097 (switch-to-buffer-other-frame abuf))
3098 ((equal org-agenda-window-setup 'reorganize-frame)
3099 (delete-other-windows)
3100 (org-switch-to-buffer-other-window abuf)))
3101 ;; additional test in case agenda is invoked from within agenda
3102 ;; buffer via elisp link
3103 (unless (equal (current-buffer) abuf)
3104 (org-pop-to-buffer-same-window abuf)))
3105 (setq buffer-read-only nil)
3106 (let ((inhibit-read-only t)) (erase-buffer))
3107 (org-agenda-mode)
3108 (and name (not org-agenda-name)
3109 (org-set-local 'org-agenda-name name)))
3110 (setq buffer-read-only nil))
3112 (defun org-finalize-agenda ()
3113 "Finishing touch for the agenda buffer, called just before displaying it."
3114 (unless org-agenda-multi
3115 (save-excursion
3116 (let ((inhibit-read-only t))
3117 (goto-char (point-min))
3118 (while (org-activate-bracket-links (point-max))
3119 (add-text-properties (match-beginning 0) (match-end 0)
3120 '(face org-link)))
3121 (org-agenda-align-tags)
3122 (unless org-agenda-with-colors
3123 (remove-text-properties (point-min) (point-max) '(face nil))))
3124 (if (and (boundp 'org-agenda-overriding-columns-format)
3125 org-agenda-overriding-columns-format)
3126 (org-set-local 'org-agenda-overriding-columns-format
3127 org-agenda-overriding-columns-format))
3128 (if (and (boundp 'org-agenda-view-columns-initially)
3129 org-agenda-view-columns-initially)
3130 (org-agenda-columns))
3131 (when org-agenda-fontify-priorities
3132 (org-agenda-fontify-priorities))
3133 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3134 (org-agenda-dim-blocked-tasks))
3135 (org-agenda-mark-clocking-task)
3136 (when org-agenda-entry-text-mode
3137 (org-agenda-entry-text-hide)
3138 (org-agenda-entry-text-show))
3139 (if (functionp 'org-habit-insert-consistency-graphs)
3140 (org-habit-insert-consistency-graphs))
3141 (run-hooks 'org-finalize-agenda-hook)
3142 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3143 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3144 (org-agenda-filter-apply org-agenda-filter))
3147 (defun org-agenda-mark-clocking-task ()
3148 "Mark the current clock entry in the agenda if it is present."
3149 (mapc (lambda (o)
3150 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3151 (delete-overlay o)))
3152 (overlays-in (point-min) (point-max)))
3153 (when (marker-buffer org-clock-hd-marker)
3154 (save-excursion
3155 (goto-char (point-min))
3156 (let (s ov)
3157 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3158 (goto-char s)
3159 (when (equal (org-get-at-bol 'org-hd-marker)
3160 org-clock-hd-marker)
3161 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3162 (overlay-put ov 'type 'org-agenda-clocking)
3163 (overlay-put ov 'face 'org-agenda-clocking)
3164 (overlay-put ov 'help-echo
3165 "The clock is running in this item")))))))
3167 (defun org-agenda-fontify-priorities ()
3168 "Make highest priority lines bold, and lowest italic."
3169 (interactive)
3170 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3171 (delete-overlay o)))
3172 (overlays-in (point-min) (point-max)))
3173 (save-excursion
3174 (let ((inhibit-read-only t)
3175 b e p ov h l)
3176 (goto-char (point-min))
3177 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3178 (setq h (or (get-char-property (point) 'org-highest-priority)
3179 org-highest-priority)
3180 l (or (get-char-property (point) 'org-lowest-priority)
3181 org-lowest-priority)
3182 p (string-to-char (match-string 1))
3183 b (match-beginning 0)
3184 e (if (eq org-agenda-fontify-priorities 'cookies)
3185 (match-end 0)
3186 (point-at-eol))
3187 ov (make-overlay b e))
3188 (overlay-put
3189 ov 'face
3190 (cond ((org-face-from-face-or-color
3191 'priority nil
3192 (cdr (assoc p org-priority-faces))))
3193 ((and (listp org-agenda-fontify-priorities)
3194 (org-face-from-face-or-color
3195 'priority nil
3196 (cdr (assoc p org-agenda-fontify-priorities)))))
3197 ((equal p l) 'italic)
3198 ((equal p h) 'bold)))
3199 (overlay-put ov 'org-type 'org-priority)))))
3201 (defun org-agenda-dim-blocked-tasks ()
3202 "Dim currently blocked TODO's in the agenda display."
3203 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3204 (delete-overlay o)))
3205 (overlays-in (point-min) (point-max)))
3206 (save-excursion
3207 (let ((inhibit-read-only t)
3208 (org-depend-tag-blocked nil)
3209 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3210 org-blocked-by-checkboxes
3211 invis1 b e p ov h l)
3212 (goto-char (point-min))
3213 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3214 (and pos (goto-char (1+ pos))))
3215 (setq org-blocked-by-checkboxes nil invis1 invis)
3216 (let ((marker (org-get-at-bol 'org-hd-marker)))
3217 (when (and marker
3218 (with-current-buffer (marker-buffer marker)
3219 (save-excursion (goto-char marker)
3220 (org-entry-blocked-p))))
3221 (if org-blocked-by-checkboxes (setq invis1 nil))
3222 (setq b (if invis1
3223 (max (point-min) (1- (point-at-bol)))
3224 (point-at-bol))
3225 e (point-at-eol)
3226 ov (make-overlay b e))
3227 (if invis1
3228 (overlay-put ov 'invisible t)
3229 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3230 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3232 (defvar org-agenda-skip-function nil
3233 "Function to be called at each match during agenda construction.
3234 If this function returns nil, the current match should not be skipped.
3235 Otherwise, the function must return a position from where the search
3236 should be continued.
3237 This may also be a Lisp form, it will be evaluated.
3238 Never set this variable using `setq' or so, because then it will apply
3239 to all future agenda commands. If you do want a global skipping condition,
3240 use the option `org-agenda-skip-function-global' instead.
3241 The correct usage for `org-agenda-skip-function' is to bind it with
3242 `let' to scope it dynamically into the agenda-constructing command.
3243 A good way to set it is through options in `org-agenda-custom-commands'.")
3245 (defun org-agenda-skip ()
3246 "Throw to `:skip' in places that should be skipped.
3247 Also moves point to the end of the skipped region, so that search can
3248 continue from there."
3249 (let ((p (point-at-bol)) to)
3250 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3251 (get-text-property p :org-archived)
3252 (org-end-of-subtree t)
3253 (throw :skip t))
3254 (and org-agenda-skip-comment-trees
3255 (get-text-property p :org-comment)
3256 (org-end-of-subtree t)
3257 (throw :skip t))
3258 (if (equal (char-after p) ?#) (throw :skip t))
3259 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3260 (org-agenda-skip-eval org-agenda-skip-function)))
3261 (goto-char to)
3262 (throw :skip t))))
3264 (defun org-agenda-skip-eval (form)
3265 "If FORM is a function or a list, call (or eval) is and return result.
3266 `save-excursion' and `save-match-data' are wrapped around the call, so point
3267 and match data are returned to the previous state no matter what these
3268 functions do."
3269 (let (fp)
3270 (and form
3271 (or (setq fp (functionp form))
3272 (consp form))
3273 (save-excursion
3274 (save-match-data
3275 (if fp
3276 (funcall form)
3277 (eval form)))))))
3279 (defvar org-agenda-markers nil
3280 "List of all currently active markers created by `org-agenda'.")
3281 (defvar org-agenda-last-marker-time (org-float-time)
3282 "Creation time of the last agenda marker.")
3284 (defun org-agenda-new-marker (&optional pos)
3285 "Return a new agenda marker.
3286 Org-mode keeps a list of these markers and resets them when they are
3287 no longer in use."
3288 (let ((m (copy-marker (or pos (point)))))
3289 (setq org-agenda-last-marker-time (org-float-time))
3290 (push m org-agenda-markers)
3293 (defun org-agenda-reset-markers ()
3294 "Reset markers created by `org-agenda'."
3295 (while org-agenda-markers
3296 (move-marker (pop org-agenda-markers) nil)))
3298 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3299 "Save relative positions of markers in region."
3300 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3301 org-agenda-markers))
3303 ;;; Entry text mode
3305 (defun org-agenda-entry-text-show-here ()
3306 "Add some text from the entry as context to the current line."
3307 (let (m txt o)
3308 (setq m (org-get-at-bol 'org-hd-marker))
3309 (unless (marker-buffer m)
3310 (error "No marker points to an entry here"))
3311 (setq txt (concat "\n" (org-no-properties
3312 (org-agenda-get-some-entry-text
3313 m org-agenda-entry-text-maxlines " > "))))
3314 (when (string-match "\\S-" txt)
3315 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3316 (overlay-put o 'evaporate t)
3317 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3318 (overlay-put o 'after-string txt))))
3320 (defun org-agenda-entry-text-show ()
3321 "Add entry context for all agenda lines."
3322 (interactive)
3323 (save-excursion
3324 (goto-char (point-max))
3325 (beginning-of-line 1)
3326 (while (not (bobp))
3327 (when (org-get-at-bol 'org-hd-marker)
3328 (org-agenda-entry-text-show-here))
3329 (beginning-of-line 0))))
3331 (defun org-agenda-entry-text-hide ()
3332 "Remove any shown entry context."
3333 (delq nil
3334 (mapcar (lambda (o)
3335 (if (eq (overlay-get o 'org-overlay-type)
3336 'agenda-entry-content)
3337 (progn (delete-overlay o) t)))
3338 (overlays-in (point-min) (point-max)))))
3340 (defun org-agenda-get-day-face (date)
3341 "Return the face DATE should be displayed with."
3342 (or (and (functionp org-agenda-day-face-function)
3343 (funcall org-agenda-day-face-function date))
3344 (cond ((org-agenda-todayp date)
3345 'org-agenda-date-today)
3346 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3347 'org-agenda-date-weekend)
3348 (t 'org-agenda-date))))
3350 ;;; Agenda timeline
3352 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3354 (defun org-timeline (&optional dotodo)
3355 "Show a time-sorted view of the entries in the current org file.
3356 Only entries with a time stamp of today or later will be listed. With
3357 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3358 under the current date.
3359 If the buffer contains an active region, only check the region for
3360 dates."
3361 (interactive "P")
3362 (org-compile-prefix-format 'timeline)
3363 (org-set-sorting-strategy 'timeline)
3364 (let* ((dopast t)
3365 (doclosed org-agenda-show-log)
3366 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3367 (current-buffer))))
3368 (date (calendar-current-date))
3369 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3370 (end (if (org-region-active-p) (region-end) (point-max)))
3371 (day-numbers (org-get-all-dates beg end 'no-ranges
3372 t doclosed ; always include today
3373 org-timeline-show-empty-dates))
3374 (org-deadline-warning-days 0)
3375 (org-agenda-only-exact-dates t)
3376 (today (org-today))
3377 (past t)
3378 args
3379 s e rtn d emptyp)
3380 (setq org-agenda-redo-command
3381 (list 'progn
3382 (list 'org-switch-to-buffer-other-window (current-buffer))
3383 (list 'org-timeline (list 'quote dotodo))))
3384 (if (not dopast)
3385 ;; Remove past dates from the list of dates.
3386 (setq day-numbers (delq nil (mapcar (lambda(x)
3387 (if (>= x today) x nil))
3388 day-numbers))))
3389 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3390 (if doclosed (push :closed args))
3391 (push :timestamp args)
3392 (push :deadline args)
3393 (push :scheduled args)
3394 (push :sexp args)
3395 (if dotodo (push :todo args))
3396 (insert "Timeline of file " entry "\n")
3397 (add-text-properties (point-min) (point)
3398 (list 'face 'org-agenda-structure))
3399 (org-agenda-mark-header-line (point-min))
3400 (while (setq d (pop day-numbers))
3401 (if (and (listp d) (eq (car d) :omitted))
3402 (progn
3403 (setq s (point))
3404 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3405 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3406 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3407 (if (and (>= d today)
3408 dopast
3409 past)
3410 (progn
3411 (setq past nil)
3412 (insert (make-string 79 ?-) "\n")))
3413 (setq date (calendar-gregorian-from-absolute d))
3414 (setq s (point))
3415 (setq rtn (and (not emptyp)
3416 (apply 'org-agenda-get-day-entries entry
3417 date args)))
3418 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3419 (progn
3420 (insert
3421 (if (stringp org-agenda-format-date)
3422 (format-time-string org-agenda-format-date
3423 (org-time-from-absolute date))
3424 (funcall org-agenda-format-date date))
3425 "\n")
3426 (put-text-property s (1- (point)) 'face
3427 (org-agenda-get-day-face date))
3428 (put-text-property s (1- (point)) 'org-date-line t)
3429 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3430 (if (equal d today)
3431 (put-text-property s (1- (point)) 'org-today t))
3432 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3433 (put-text-property s (1- (point)) 'day d)))))
3434 (goto-char (point-min))
3435 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3436 (point-min)))
3437 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3438 (org-finalize-agenda)
3439 (setq buffer-read-only t)))
3441 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3442 "Return a list of all relevant day numbers from BEG to END buffer positions.
3443 If NO-RANGES is non-nil, include only the start and end dates of a range,
3444 not every single day in the range. If FORCE-TODAY is non-nil, make
3445 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3446 inactive time stamps (those in square brackets) are included.
3447 When EMPTY is non-nil, also include days without any entries."
3448 (let ((re (concat
3449 (if pre-re pre-re "")
3450 (if inactive org-ts-regexp-both org-ts-regexp)))
3451 dates dates1 date day day1 day2 ts1 ts2 pos)
3452 (if force-today
3453 (setq dates (list (org-today))))
3454 (save-excursion
3455 (goto-char beg)
3456 (while (re-search-forward re end t)
3457 (setq day (time-to-days (org-time-string-to-time
3458 (substring (match-string 1) 0 10)
3459 (current-buffer) (match-beginning 0))))
3460 (or (memq day dates) (push day dates)))
3461 (unless no-ranges
3462 (goto-char beg)
3463 (while (re-search-forward org-tr-regexp end t)
3464 (setq pos (match-beginning 0))
3465 (setq ts1 (substring (match-string 1) 0 10)
3466 ts2 (substring (match-string 2) 0 10)
3467 day1 (time-to-days (org-time-string-to-time
3468 ts1 (current-buffer) pos))
3469 day2 (time-to-days (org-time-string-to-time
3470 ts2 (current-buffer) pos)))
3471 (while (< (setq day1 (1+ day1)) day2)
3472 (or (memq day1 dates) (push day1 dates)))))
3473 (setq dates (sort dates '<))
3474 (when empty
3475 (while (setq day (pop dates))
3476 (setq day2 (car dates))
3477 (push day dates1)
3478 (when (and day2 empty)
3479 (if (or (eq empty t)
3480 (and (numberp empty) (<= (- day2 day) empty)))
3481 (while (< (setq day (1+ day)) day2)
3482 (push (list day) dates1))
3483 (push (cons :omitted (- day2 day)) dates1))))
3484 (setq dates (nreverse dates1)))
3485 dates)))
3487 ;;; Agenda Daily/Weekly
3489 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3490 "Start day for the agenda view.
3491 Custom commands can set this variable in the options section.")
3492 (defvar org-starting-day nil) ; local variable in the agenda buffer
3493 (defvar org-agenda-current-span nil
3494 "The current span used in the agenda view.") ; local variable in the agenda buffer
3495 (defvar org-arg-loc nil) ; local variable
3497 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3498 "List of types searched for when creating the daily/weekly agenda.
3499 This variable is a list of symbols that controls the types of
3500 items that appear in the daily/weekly agenda. Allowed symbols in this
3501 list are are
3503 :timestamp List items containing a date stamp or date range matching
3504 the selected date. This includes sexp entries in
3505 angular brackets.
3507 :sexp List entries resulting from plain diary-like sexps.
3509 :deadline List deadline due on that date. When the date is today,
3510 also list any deadlines past due, or due within
3511 `org-deadline-warning-days'. `:deadline' must appear before
3512 `:scheduled' if the setting of
3513 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3514 any effect.
3516 :scheduled List all items which are scheduled for the given date.
3517 The diary for *today* also contains items which were
3518 scheduled earlier and are not yet marked DONE.
3520 By default, all four types are turned on.
3522 Never set this variable globally using `setq', because then it
3523 will apply to all future agenda commands. Instead, bind it with
3524 `let' to scope it dynamically into the agenda-constructing
3525 command. A good way to set it is through options in
3526 `org-agenda-custom-commands'. For a more flexible (though
3527 somewhat less efficient) way of determining what is included in
3528 the daily/weekly agenda, see `org-agenda-skip-function'.")
3530 ;;;###autoload
3531 (defun org-agenda-list (&optional arg start-day span)
3532 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3533 The view will be for the current day or week, but from the overview buffer
3534 you will be able to go to other days/weeks.
3536 With a numeric prefix argument in an interactive call, the agenda will
3537 span ARG days. Lisp programs should instead specify SPAN to change
3538 the number of days. SPAN defaults to `org-agenda-span'.
3540 START-DAY defaults to TODAY, or to the most recent match for the weekday
3541 given in `org-agenda-start-on-weekday'."
3542 (interactive "P")
3543 (if (and (integerp arg) (> arg 0))
3544 (setq span arg arg nil))
3545 (setq start-day (or start-day org-agenda-start-day))
3546 (if org-agenda-overriding-arguments
3547 (setq arg (car org-agenda-overriding-arguments)
3548 start-day (nth 1 org-agenda-overriding-arguments)
3549 span (nth 2 org-agenda-overriding-arguments)))
3550 (if (stringp start-day)
3551 ;; Convert to an absolute day number
3552 (setq start-day (time-to-days (org-read-date nil t start-day))))
3553 (setq org-agenda-last-arguments (list arg start-day span))
3554 (org-compile-prefix-format 'agenda)
3555 (org-set-sorting-strategy 'agenda)
3556 (let* ((span (org-agenda-ndays-to-span
3557 (or span org-agenda-ndays org-agenda-span)))
3558 (today (org-today))
3559 (sd (or start-day today))
3560 (ndays (org-agenda-span-to-ndays span sd))
3561 (org-agenda-start-on-weekday
3562 (if (eq ndays 7)
3563 org-agenda-start-on-weekday))
3564 (thefiles (org-agenda-files nil 'ifmode))
3565 (files thefiles)
3566 (start (if (or (null org-agenda-start-on-weekday)
3567 (< ndays 7))
3569 (let* ((nt (calendar-day-of-week
3570 (calendar-gregorian-from-absolute sd)))
3571 (n1 org-agenda-start-on-weekday)
3572 (d (- nt n1)))
3573 (- sd (+ (if (< d 0) 7 0) d)))))
3574 (day-numbers (list start))
3575 (day-cnt 0)
3576 (inhibit-redisplay (not debug-on-error))
3577 s e rtn rtnall file date d start-pos end-pos todayp
3578 clocktable-start clocktable-end filter)
3579 (setq org-agenda-redo-command
3580 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3581 (dotimes (n (1- ndays))
3582 (push (1+ (car day-numbers)) day-numbers))
3583 (setq day-numbers (nreverse day-numbers))
3584 (setq clocktable-start (car day-numbers)
3585 clocktable-end (1+ (or (org-last day-numbers) 0)))
3586 (org-prepare-agenda "Day/Week")
3587 (org-set-local 'org-starting-day (car day-numbers))
3588 (org-set-local 'org-arg-loc arg)
3589 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3590 (unless org-agenda-compact-blocks
3591 (let* ((d1 (car day-numbers))
3592 (d2 (org-last day-numbers))
3593 (w1 (org-days-to-iso-week d1))
3594 (w2 (org-days-to-iso-week d2)))
3595 (setq s (point))
3596 (if org-agenda-overriding-header
3597 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3598 nil 'face 'org-agenda-structure) "\n")
3599 (insert (org-agenda-span-name span)
3600 "-agenda"
3601 (if (< (- d2 d1) 350)
3602 (if (= w1 w2)
3603 (format " (W%02d)" w1)
3604 (format " (W%02d-W%02d)" w1 w2))
3606 ":\n")))
3607 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3608 'org-date-line t))
3609 (org-agenda-mark-header-line s))
3610 (while (setq d (pop day-numbers))
3611 (setq date (calendar-gregorian-from-absolute d)
3612 s (point))
3613 (if (or (setq todayp (= d today))
3614 (and (not start-pos) (= d sd)))
3615 (setq start-pos (point))
3616 (if (and start-pos (not end-pos))
3617 (setq end-pos (point))))
3618 (setq files thefiles
3619 rtnall nil)
3620 (while (setq file (pop files))
3621 (catch 'nextfile
3622 (org-check-agenda-file file)
3623 (let ((org-agenda-entry-types org-agenda-entry-types))
3624 (unless org-agenda-include-deadlines
3625 (setq org-agenda-entry-types
3626 (delq :deadline org-agenda-entry-types)))
3627 (cond
3628 ((memq org-agenda-show-log '(only clockcheck))
3629 (setq rtn (org-agenda-get-day-entries
3630 file date :closed)))
3631 (org-agenda-show-log
3632 (setq rtn (apply 'org-agenda-get-day-entries
3633 file date
3634 (append '(:closed) org-agenda-entry-types))))
3636 (setq rtn (apply 'org-agenda-get-day-entries
3637 file date
3638 org-agenda-entry-types)))))
3639 (setq rtnall (append rtnall rtn))))
3640 (if org-agenda-include-diary
3641 (let ((org-agenda-search-headline-for-time t))
3642 (require 'diary-lib)
3643 (setq rtn (org-get-entries-from-diary date))
3644 (setq rtnall (append rtnall rtn))))
3645 (if (or rtnall org-agenda-show-all-dates)
3646 (progn
3647 (setq day-cnt (1+ day-cnt))
3648 (insert
3649 (if (stringp org-agenda-format-date)
3650 (format-time-string org-agenda-format-date
3651 (org-time-from-absolute date))
3652 (funcall org-agenda-format-date date))
3653 "\n")
3654 (put-text-property s (1- (point)) 'face
3655 (org-agenda-get-day-face date))
3656 (put-text-property s (1- (point)) 'org-date-line t)
3657 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3658 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3659 (when todayp
3660 (put-text-property s (1- (point)) 'org-today t))
3661 (if rtnall (insert
3662 (org-finalize-agenda-entries
3663 (org-agenda-add-time-grid-maybe
3664 rtnall ndays todayp))
3665 "\n"))
3666 (put-text-property s (1- (point)) 'day d)
3667 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3668 (when (and org-agenda-clockreport-mode clocktable-start)
3669 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3670 ;; the above line is to ensure the restricted range!
3671 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3672 tbl)
3673 (setq p (org-plist-delete p :block))
3674 (setq p (plist-put p :tstart clocktable-start))
3675 (setq p (plist-put p :tend clocktable-end))
3676 (setq p (plist-put p :scope 'agenda))
3677 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3678 (setq filter (or org-agenda-filter-while-redo
3679 (get 'org-agenda-filter :preset-filter))))
3680 (setq p (plist-put p :tags (mapconcat (lambda (x)
3681 (if (string-match "[<>=]" x)
3684 filter ""))))
3685 (setq tbl (apply 'org-get-clocktable p))
3686 (insert tbl)))
3687 (goto-char (point-min))
3688 (or org-agenda-multi (org-fit-agenda-window))
3689 (unless (and (pos-visible-in-window-p (point-min))
3690 (pos-visible-in-window-p (point-max)))
3691 (goto-char (1- (point-max)))
3692 (recenter -1)
3693 (if (not (pos-visible-in-window-p (or start-pos 1)))
3694 (progn
3695 (goto-char (or start-pos 1))
3696 (recenter 1))))
3697 (goto-char (or start-pos 1))
3698 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3699 (if (eq org-agenda-show-log 'clockcheck)
3700 (org-agenda-show-clocking-issues))
3701 (org-finalize-agenda)
3702 (setq buffer-read-only t)
3703 (message "")))
3705 (defun org-agenda-ndays-to-span (n)
3706 "Return a span symbol for a span of N days, or N if none matches."
3707 (cond ((symbolp n) n)
3708 ((= n 1) 'day)
3709 ((= n 7) 'week)
3710 (t n)))
3712 (defun org-agenda-span-to-ndays (span start-day)
3713 "Return ndays from SPAN starting at START-DAY."
3714 (cond ((numberp span) span)
3715 ((eq span 'day) 1)
3716 ((eq span 'week) 7)
3717 ((eq span 'month)
3718 (let ((date (calendar-gregorian-from-absolute start-day)))
3719 (calendar-last-day-of-month (car date) (caddr date))))
3720 ((eq span 'year)
3721 (let ((date (calendar-gregorian-from-absolute start-day)))
3722 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3724 (defun org-agenda-span-name (span)
3725 "Return a SPAN name."
3726 (if (null span)
3728 (if (symbolp span)
3729 (capitalize (symbol-name span))
3730 (format "%d days" span))))
3732 ;;; Agenda word search
3734 (defvar org-agenda-search-history nil)
3735 (defvar org-todo-only nil)
3737 (defvar org-search-syntax-table nil
3738 "Special syntax table for org-mode search.
3739 In this table, we have single quotes not as word constituents, to
3740 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3742 (defun org-search-syntax-table ()
3743 (unless org-search-syntax-table
3744 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3745 (modify-syntax-entry ?' "." org-search-syntax-table)
3746 (modify-syntax-entry ?` "." org-search-syntax-table))
3747 org-search-syntax-table)
3749 (defvar org-agenda-last-search-view-search-was-boolean nil)
3751 ;;;###autoload
3752 (defun org-search-view (&optional todo-only string edit-at)
3753 "Show all entries that contain a phrase or words or regular expressions.
3755 With optional prefix argument TODO-ONLY, only consider entries that are
3756 TODO entries. The argument STRING can be used to pass a default search
3757 string into this function. If EDIT-AT is non-nil, it means that the
3758 user should get a chance to edit this string, with cursor at position
3759 EDIT-AT.
3761 The search string can be viewed either as a phrase that should be found as
3762 is, or it can be broken into a number of snippets, each of which must match
3763 in a Boolean way to select an entry. The default depends on the variable
3764 `org-agenda-search-view-always-boolean'.
3765 Even if this is turned off (the default) you can always switch to
3766 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3768 The default is a direct search of the whole phrase, where each space in
3769 the search string can expand to an arbitrary amount of whitespace,
3770 including newlines.
3772 If using a Boolean search, the search string is split on whitespace and
3773 each snippet is searched separately, with logical AND to select an entry.
3774 Words prefixed with a minus must *not* occur in the entry. Words without
3775 a prefix or prefixed with a plus must occur in the entry. Matching is
3776 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3777 match whole words, not parts of a word) if
3778 `org-agenda-search-view-force-full-words' is set (default is nil).
3780 Boolean search snippets enclosed by curly braces are interpreted as
3781 regular expressions that must or (when preceded with \"-\") must not
3782 match in the entry. Snippets enclosed into double quotes will be taken
3783 as a whole, to include whitespace.
3785 - If the search string starts with an asterisk, search only in headlines.
3786 - If (possibly after the leading star) the search string starts with an
3787 exclamation mark, this also means to look at TODO entries only, an effect
3788 that can also be achieved with a prefix argument.
3789 - If (possibly after star and exclamation mark) the search string starts
3790 with a colon, this will mean that the (non-regexp) snippets of the
3791 Boolean search must match as full words.
3793 This command searches the agenda files, and in addition the files listed
3794 in `org-agenda-text-search-extra-files'."
3795 (interactive "P")
3796 (org-compile-prefix-format 'search)
3797 (org-set-sorting-strategy 'search)
3798 (org-prepare-agenda "SEARCH")
3799 (let* ((props (list 'face nil
3800 'done-face 'org-agenda-done
3801 'org-not-done-regexp org-not-done-regexp
3802 'org-todo-regexp org-todo-regexp
3803 'org-complex-heading-regexp org-complex-heading-regexp
3804 'mouse-face 'highlight
3805 'help-echo (format "mouse-2 or RET jump to location")))
3806 (full-words org-agenda-search-view-force-full-words)
3807 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3808 regexp rtn rtnall files file pos
3809 marker category org-category-pos tags c neg re boolean
3810 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3811 (unless (and (not edit-at)
3812 (stringp string)
3813 (string-match "\\S-" string))
3814 (setq string (read-string
3815 (if org-agenda-search-view-always-boolean
3816 "[+-]Word/{Regexp} ...: "
3817 "Phrase, or [+-]Word/{Regexp} ...: ")
3818 (cond
3819 ((integerp edit-at) (cons string edit-at))
3820 (edit-at string))
3821 'org-agenda-search-history)))
3822 (org-set-local 'org-todo-only todo-only)
3823 (setq org-agenda-redo-command
3824 (list 'org-search-view (if todo-only t nil) string
3825 '(if current-prefix-arg 1 nil)))
3826 (setq org-agenda-query-string string)
3828 (if (equal (string-to-char string) ?*)
3829 (setq hdl-only t
3830 words (substring string 1))
3831 (setq words string))
3832 (when (equal (string-to-char words) ?!)
3833 (setq todo-only t
3834 words (substring words 1)))
3835 (when (equal (string-to-char words) ?:)
3836 (setq full-words t
3837 words (substring words 1)))
3838 (if (or org-agenda-search-view-always-boolean
3839 (member (string-to-char words) '(?- ?+ ?\{)))
3840 (setq boolean t))
3841 (setq words (org-split-string words))
3842 (let (www w)
3843 (while (setq w (pop words))
3844 (while (and (string-match "\\\\\\'" w) words)
3845 (setq w (concat (substring w 0 -1) " " (pop words))))
3846 (push w www))
3847 (setq words (nreverse www) www nil)
3848 (while (setq w (pop words))
3849 (when (and (string-match "\\`[-+]?{" w)
3850 (not (string-match "}\\'" w)))
3851 (while (and words (not (string-match "}\\'" (car words))))
3852 (setq w (concat w " " (pop words))))
3853 (setq w (concat w " " (pop words))))
3854 (push w www))
3855 (setq words (nreverse www)))
3856 (setq org-agenda-last-search-view-search-was-boolean boolean)
3857 (when boolean
3858 (let (wds w)
3859 (while (setq w (pop words))
3860 (if (or (equal (substring w 0 1) "\"")
3861 (and (> (length w) 1)
3862 (member (substring w 0 1) '("+" "-"))
3863 (equal (substring w 1 2) "\"")))
3864 (while (and words (not (equal (substring w -1) "\"")))
3865 (setq w (concat w " " (pop words)))))
3866 (and (string-match "\\`\\([-+]?\\)\"" w)
3867 (setq w (replace-match "\\1" nil nil w)))
3868 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3869 (push w wds))
3870 (setq words (nreverse wds))))
3871 (if boolean
3872 (mapc (lambda (w)
3873 (setq c (string-to-char w))
3874 (if (equal c ?-)
3875 (setq neg t w (substring w 1))
3876 (if (equal c ?+)
3877 (setq neg nil w (substring w 1))
3878 (setq neg nil)))
3879 (if (string-match "\\`{.*}\\'" w)
3880 (setq re (substring w 1 -1))
3881 (if full-words
3882 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3883 (setq re (regexp-quote (downcase w)))))
3884 (if neg (push re regexps-) (push re regexps+)))
3885 words)
3886 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3887 regexps+))
3888 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3889 (if (not regexps+)
3890 (setq regexp org-outline-regexp-bol)
3891 (setq regexp (pop regexps+))
3892 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
3893 regexp))))
3894 (setq files (org-agenda-files nil 'ifmode))
3895 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3896 (pop org-agenda-text-search-extra-files)
3897 (setq files (org-add-archive-files files)))
3898 (setq files (append files org-agenda-text-search-extra-files)
3899 rtnall nil)
3900 (while (setq file (pop files))
3901 (setq ee nil)
3902 (catch 'nextfile
3903 (org-check-agenda-file file)
3904 (setq buffer (if (file-exists-p file)
3905 (org-get-agenda-file-buffer file)
3906 (error "No such file %s" file)))
3907 (if (not buffer)
3908 ;; If file does not exist, make sure an error message is sent
3909 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3910 file))))
3911 (with-current-buffer buffer
3912 (with-syntax-table (org-search-syntax-table)
3913 (unless (eq major-mode 'org-mode)
3914 (error "Agenda file %s is not in `org-mode'" file))
3915 (let ((case-fold-search t))
3916 (save-excursion
3917 (save-restriction
3918 (if org-agenda-restrict
3919 (narrow-to-region org-agenda-restrict-begin
3920 org-agenda-restrict-end)
3921 (widen))
3922 (goto-char (point-min))
3923 (unless (or (org-on-heading-p)
3924 (outline-next-heading))
3925 (throw 'nextfile t))
3926 (goto-char (max (point-min) (1- (point))))
3927 (while (re-search-forward regexp nil t)
3928 (org-back-to-heading t)
3929 (skip-chars-forward "* ")
3930 (setq beg (point-at-bol)
3931 beg1 (point)
3932 end (progn (outline-next-heading) (point)))
3933 (catch :skip
3934 (goto-char beg)
3935 (org-agenda-skip)
3936 (setq str (buffer-substring-no-properties
3937 (point-at-bol)
3938 (if hdl-only (point-at-eol) end)))
3939 (mapc (lambda (wr) (when (string-match wr str)
3940 (goto-char (1- end))
3941 (throw :skip t)))
3942 regexps-)
3943 (mapc (lambda (wr) (unless (string-match wr str)
3944 (goto-char (1- end))
3945 (throw :skip t)))
3946 (if todo-only
3947 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3948 regexps+)
3949 regexps+))
3950 (goto-char beg)
3951 (setq marker (org-agenda-new-marker (point))
3952 category (org-get-category)
3953 org-category-pos (get-text-property (point) 'org-category-position)
3954 tags (org-get-tags-at (point))
3955 txt (org-agenda-format-item
3957 (buffer-substring-no-properties
3958 beg1 (point-at-eol))
3959 category tags))
3960 (org-add-props txt props
3961 'org-marker marker 'org-hd-marker marker
3962 'org-todo-regexp org-todo-regexp
3963 'org-complex-heading-regexp org-complex-heading-regexp
3964 'priority 1000 'org-category category
3965 'org-category-position org-category-pos
3966 'type "search")
3967 (push txt ee)
3968 (goto-char (1- end))))))))))
3969 (setq rtn (nreverse ee))
3970 (setq rtnall (append rtnall rtn)))
3971 (if org-agenda-overriding-header
3972 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3973 nil 'face 'org-agenda-structure) "\n")
3974 (insert "Search words: ")
3975 (add-text-properties (point-min) (1- (point))
3976 (list 'face 'org-agenda-structure))
3977 (setq pos (point))
3978 (insert string "\n")
3979 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3980 (setq pos (point))
3981 (unless org-agenda-multi
3982 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3983 (add-text-properties pos (1- (point))
3984 (list 'face 'org-agenda-structure))))
3985 (org-agenda-mark-header-line (point-min))
3986 (when rtnall
3987 (insert (org-finalize-agenda-entries rtnall) "\n"))
3988 (goto-char (point-min))
3989 (or org-agenda-multi (org-fit-agenda-window))
3990 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3991 (org-finalize-agenda)
3992 (setq buffer-read-only t)))
3994 ;;; Agenda TODO list
3996 (defvar org-select-this-todo-keyword nil)
3997 (defvar org-last-arg nil)
3999 ;;;###autoload
4000 (defun org-todo-list (arg)
4001 "Show all (not done) TODO entries from all agenda file in a single list.
4002 The prefix arg can be used to select a specific TODO keyword and limit
4003 the list to these. When using \\[universal-argument], you will be prompted
4004 for a keyword. A numeric prefix directly selects the Nth keyword in
4005 `org-todo-keywords-1'."
4006 (interactive "P")
4007 (org-compile-prefix-format 'todo)
4008 (org-set-sorting-strategy 'todo)
4009 (org-prepare-agenda "TODO")
4010 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4011 (let* ((today (org-today))
4012 (date (calendar-gregorian-from-absolute today))
4013 (kwds org-todo-keywords-for-agenda)
4014 (completion-ignore-case t)
4015 (org-select-this-todo-keyword
4016 (if (stringp arg) arg
4017 (and arg (integerp arg) (> arg 0)
4018 (nth (1- arg) kwds))))
4019 rtn rtnall files file pos)
4020 (when (equal arg '(4))
4021 (setq org-select-this-todo-keyword
4022 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4023 (mapcar 'list kwds) nil nil)))
4024 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4025 (org-set-local 'org-last-arg arg)
4026 (setq org-agenda-redo-command
4027 '(org-todo-list (or current-prefix-arg org-last-arg)))
4028 (setq files (org-agenda-files nil 'ifmode)
4029 rtnall nil)
4030 (while (setq file (pop files))
4031 (catch 'nextfile
4032 (org-check-agenda-file file)
4033 (setq rtn (org-agenda-get-day-entries file date :todo))
4034 (setq rtnall (append rtnall rtn))))
4035 (if org-agenda-overriding-header
4036 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4037 nil 'face 'org-agenda-structure) "\n")
4038 (insert "Global list of TODO items of type: ")
4039 (add-text-properties (point-min) (1- (point))
4040 (list 'face 'org-agenda-structure
4041 'short-heading
4042 (concat "ToDo: "
4043 (or org-select-this-todo-keyword "ALL"))))
4044 (org-agenda-mark-header-line (point-min))
4045 (setq pos (point))
4046 (insert (or org-select-this-todo-keyword "ALL") "\n")
4047 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4048 (setq pos (point))
4049 (unless org-agenda-multi
4050 (insert "Available with `N r': (0)ALL")
4051 (let ((n 0) s)
4052 (mapc (lambda (x)
4053 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4054 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4055 (insert "\n "))
4056 (insert " " s))
4057 kwds))
4058 (insert "\n"))
4059 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4060 (org-agenda-mark-header-line (point-min))
4061 (when rtnall
4062 (insert (org-finalize-agenda-entries rtnall) "\n"))
4063 (goto-char (point-min))
4064 (or org-agenda-multi (org-fit-agenda-window))
4065 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4066 (org-finalize-agenda)
4067 (setq buffer-read-only t)))
4069 ;;; Agenda tags match
4071 ;;;###autoload
4072 (defun org-tags-view (&optional todo-only match)
4073 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4074 The prefix arg TODO-ONLY limits the search to TODO entries."
4075 (interactive "P")
4076 (org-compile-prefix-format 'tags)
4077 (org-set-sorting-strategy 'tags)
4078 (let* ((org-tags-match-list-sublevels
4079 org-tags-match-list-sublevels)
4080 (completion-ignore-case t)
4081 rtn rtnall files file pos matcher
4082 buffer)
4083 (when (and (stringp match) (not (string-match "\\S-" match)))
4084 (setq match nil))
4085 (setq matcher (org-make-tags-matcher match)
4086 match (car matcher) matcher (cdr matcher))
4087 (org-prepare-agenda (concat "TAGS " match))
4088 (setq org-agenda-query-string match)
4089 (setq org-agenda-redo-command
4090 (list 'org-tags-view (list 'quote todo-only)
4091 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4092 (setq files (org-agenda-files nil 'ifmode)
4093 rtnall nil)
4094 (while (setq file (pop files))
4095 (catch 'nextfile
4096 (org-check-agenda-file file)
4097 (setq buffer (if (file-exists-p file)
4098 (org-get-agenda-file-buffer file)
4099 (error "No such file %s" file)))
4100 (if (not buffer)
4101 ;; If file does not exist, error message to agenda
4102 (setq rtn (list
4103 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4104 rtnall (append rtnall rtn))
4105 (with-current-buffer buffer
4106 (unless (eq major-mode 'org-mode)
4107 (error "Agenda file %s is not in `org-mode'" file))
4108 (save-excursion
4109 (save-restriction
4110 (if org-agenda-restrict
4111 (narrow-to-region org-agenda-restrict-begin
4112 org-agenda-restrict-end)
4113 (widen))
4114 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4115 (setq rtnall (append rtnall rtn))))))))
4116 (if org-agenda-overriding-header
4117 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4118 nil 'face 'org-agenda-structure) "\n")
4119 (insert "Headlines with TAGS match: ")
4120 (add-text-properties (point-min) (1- (point))
4121 (list 'face 'org-agenda-structure
4122 'short-heading
4123 (concat "Match: " match)))
4124 (setq pos (point))
4125 (insert match "\n")
4126 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4127 (setq pos (point))
4128 (unless org-agenda-multi
4129 (insert "Press `C-u r' to search again with new search string\n"))
4130 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4131 (org-agenda-mark-header-line (point-min))
4132 (when rtnall
4133 (insert (org-finalize-agenda-entries rtnall) "\n"))
4134 (goto-char (point-min))
4135 (or org-agenda-multi (org-fit-agenda-window))
4136 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4137 (org-finalize-agenda)
4138 (setq buffer-read-only t)))
4140 ;;; Agenda Finding stuck projects
4142 (defvar org-agenda-skip-regexp nil
4143 "Regular expression used in skipping subtrees for the agenda.
4144 This is basically a temporary global variable that can be set and then
4145 used by user-defined selections using `org-agenda-skip-function'.")
4147 (defvar org-agenda-overriding-header nil
4148 "When set during agenda, todo and tags searches it replaces the header.
4149 This variable should not be set directly, but custom commands can bind it
4150 in the options section.")
4152 (defun org-agenda-skip-entry-when-regexp-matches ()
4153 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4154 If yes, it returns the end position of this entry, causing agenda commands
4155 to skip the entry but continuing the search in the subtree. This is a
4156 function that can be put into `org-agenda-skip-function' for the duration
4157 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-subtree-when-regexp-matches ()
4165 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4166 If yes, it returns the end position of this tree, causing agenda commands
4167 to skip this subtree. This is a function that can be put into
4168 `org-agenda-skip-function' for the duration of a command."
4169 (let ((end (save-excursion (org-end-of-subtree t)))
4170 skip)
4171 (save-excursion
4172 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4173 (and skip end)))
4175 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4176 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4177 If yes, it returns the end position of the current entry (NOT the tree),
4178 causing agenda commands to skip the entry but continuing the search in
4179 the subtree. This is a function that can be put into
4180 `org-agenda-skip-function' for the duration of a command. An important
4181 use of this function is for the stuck project list."
4182 (let ((end (save-excursion (org-end-of-subtree t)))
4183 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4184 skip)
4185 (save-excursion
4186 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4187 (and skip entry-end)))
4189 (defun org-agenda-skip-entry-if (&rest conditions)
4190 "Skip entry if any of CONDITIONS is true.
4191 See `org-agenda-skip-if' for details."
4192 (org-agenda-skip-if nil conditions))
4194 (defun org-agenda-skip-subtree-if (&rest conditions)
4195 "Skip entry if any of CONDITIONS is true.
4196 See `org-agenda-skip-if' for details."
4197 (org-agenda-skip-if t conditions))
4199 (defun org-agenda-skip-if (subtree conditions)
4200 "Checks current entity for CONDITIONS.
4201 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4202 the entry, i.e. the text before the next heading is checked.
4204 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4205 from different tests. Valid conditions are:
4207 scheduled Check if there is a scheduled cookie
4208 notscheduled Check if there is no scheduled cookie
4209 deadline Check if there is a deadline
4210 notdeadline Check if there is no deadline
4211 timestamp Check if there is a timestamp (also deadline or scheduled)
4212 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4213 regexp Check if regexp matches
4214 notregexp Check if regexp does not match.
4215 todo Check if TODO keyword matches
4216 nottodo Check if TODO keyword does not match
4218 The regexp is taken from the conditions list, it must come right after
4219 the `regexp' or `notregexp' element.
4221 `todo' and `nottodo' accept as an argument a list of todo
4222 keywords, which may include \"*\" to match any todo keyword.
4224 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4226 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4228 Instead of a list a keyword class may be given
4230 (org-agenda-skip-entry-if 'nottodo 'done)
4232 would skip entries that haven't been marked with any of \"DONE\"
4233 keywords. Possible classes are: `todo', `done', `any'.
4235 If any of these conditions is met, this function returns the end point of
4236 the entity, causing the search to continue from there. This is a function
4237 that can be put into `org-agenda-skip-function' for the duration of a command."
4238 (let (beg end m)
4239 (org-back-to-heading t)
4240 (setq beg (point)
4241 end (if subtree
4242 (progn (org-end-of-subtree t) (point))
4243 (progn (outline-next-heading) (1- (point)))))
4244 (goto-char beg)
4245 (and
4247 (and (memq 'scheduled conditions)
4248 (re-search-forward org-scheduled-time-regexp end t))
4249 (and (memq 'notscheduled conditions)
4250 (not (re-search-forward org-scheduled-time-regexp end t)))
4251 (and (memq 'deadline conditions)
4252 (re-search-forward org-deadline-time-regexp end t))
4253 (and (memq 'notdeadline conditions)
4254 (not (re-search-forward org-deadline-time-regexp end t)))
4255 (and (memq 'timestamp conditions)
4256 (re-search-forward org-ts-regexp end t))
4257 (and (memq 'nottimestamp conditions)
4258 (not (re-search-forward org-ts-regexp end t)))
4259 (and (setq m (memq 'regexp conditions))
4260 (stringp (nth 1 m))
4261 (re-search-forward (nth 1 m) end t))
4262 (and (setq m (memq 'notregexp conditions))
4263 (stringp (nth 1 m))
4264 (not (re-search-forward (nth 1 m) end t)))
4265 (and (or
4266 (setq m (memq 'todo conditions))
4267 (setq m (memq 'nottodo conditions)))
4268 (org-agenda-skip-if-todo m end)))
4269 end)))
4271 (defun org-agenda-skip-if-todo (args end)
4272 "Helper function for `org-agenda-skip-if', do not use it directly.
4273 ARGS is a list with first element either `todo' or `nottodo'.
4274 The remainder is either a list of TODO keywords, or a state symbol
4275 `todo' or `done' or `any'."
4276 (let ((kw (car args))
4277 (arg (cadr args))
4278 todo-wds todo-re)
4279 (setq todo-wds
4280 (org-uniquify
4281 (cond
4282 ((listp arg) ;; list of keywords
4283 (if (member "*" arg)
4284 (mapcar 'substring-no-properties org-todo-keywords-1)
4285 arg))
4286 ((symbolp arg) ;; keyword class name
4287 (cond
4288 ((eq arg 'todo)
4289 (org-delete-all org-done-keywords
4290 (mapcar 'substring-no-properties
4291 org-todo-keywords-1)))
4292 ((eq arg 'done) org-done-keywords)
4293 ((eq arg 'any)
4294 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4295 (setq todo-re
4296 (concat "^\\*+[ \t]+\\<\\("
4297 (mapconcat 'identity todo-wds "\\|")
4298 "\\)\\>"))
4299 (if (eq kw 'todo)
4300 (re-search-forward todo-re end t)
4301 (not (re-search-forward todo-re end t)))))
4303 ;;;###autoload
4304 (defun org-agenda-list-stuck-projects (&rest ignore)
4305 "Create agenda view for projects that are stuck.
4306 Stuck projects are project that have no next actions. For the definitions
4307 of what a project is and how to check if it stuck, customize the variable
4308 `org-stuck-projects'."
4309 (interactive)
4310 (let* ((org-agenda-skip-function
4311 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4312 ;; We could have used org-agenda-skip-if here.
4313 (org-agenda-overriding-header
4314 (or org-agenda-overriding-header "List of stuck projects: "))
4315 (matcher (nth 0 org-stuck-projects))
4316 (todo (nth 1 org-stuck-projects))
4317 (todo-wds (if (member "*" todo)
4318 (progn
4319 (org-prepare-agenda-buffers (org-agenda-files
4320 nil 'ifmode))
4321 (org-delete-all
4322 org-done-keywords-for-agenda
4323 (copy-sequence org-todo-keywords-for-agenda)))
4324 todo))
4325 (todo-re (concat "^\\*+[ \t]+\\("
4326 (mapconcat 'identity todo-wds "\\|")
4327 "\\)\\>"))
4328 (tags (nth 2 org-stuck-projects))
4329 (tags-re (if (member "*" tags)
4330 (concat org-outline-regexp-bol
4331 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4332 (if tags
4333 (concat org-outline-regexp-bol
4334 ".*:\\("
4335 (mapconcat 'identity tags "\\|")
4336 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4337 (gen-re (nth 3 org-stuck-projects))
4338 (re-list
4339 (delq nil
4340 (list
4341 (if todo todo-re)
4342 (if tags tags-re)
4343 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4344 gen-re)))))
4345 (setq org-agenda-skip-regexp
4346 (if re-list
4347 (mapconcat 'identity re-list "\\|")
4348 (error "No information how to identify unstuck projects")))
4349 (org-tags-view nil matcher)
4350 (with-current-buffer org-agenda-buffer-name
4351 (setq org-agenda-redo-command
4352 '(org-agenda-list-stuck-projects
4353 (or current-prefix-arg org-last-arg))))))
4355 ;;; Diary integration
4357 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4358 (defvar list-diary-entries-hook)
4359 (defvar diary-time-regexp)
4360 (defun org-get-entries-from-diary (date)
4361 "Get the (Emacs Calendar) diary entries for DATE."
4362 (require 'diary-lib)
4363 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4364 (diary-display-hook '(fancy-diary-display))
4365 (diary-display-function 'fancy-diary-display)
4366 (pop-up-frames nil)
4367 (list-diary-entries-hook
4368 (cons 'org-diary-default-entry list-diary-entries-hook))
4369 (diary-file-name-prefix-function nil) ; turn this feature off
4370 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4371 entries
4372 (org-disable-agenda-to-diary t))
4373 (save-excursion
4374 (save-window-excursion
4375 (funcall (if (fboundp 'diary-list-entries)
4376 'diary-list-entries 'list-diary-entries)
4377 date 1)))
4378 (if (not (get-buffer diary-fancy-buffer))
4379 (setq entries nil)
4380 (with-current-buffer diary-fancy-buffer
4381 (setq buffer-read-only nil)
4382 (if (zerop (buffer-size))
4383 ;; No entries
4384 (setq entries nil)
4385 ;; Omit the date and other unnecessary stuff
4386 (org-agenda-cleanup-fancy-diary)
4387 ;; Add prefix to each line and extend the text properties
4388 (if (zerop (buffer-size))
4389 (setq entries nil)
4390 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4391 (setq entries
4392 (with-temp-buffer
4393 (insert entries) (goto-char (point-min))
4394 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4395 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4396 (replace-match (concat "; " (match-string 1)))))
4397 (buffer-string)))))
4398 (set-buffer-modified-p nil)
4399 (kill-buffer diary-fancy-buffer)))
4400 (when entries
4401 (setq entries (org-split-string entries "\n"))
4402 (setq entries
4403 (mapcar
4404 (lambda (x)
4405 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4406 ;; Extend the text properties to the beginning of the line
4407 (org-add-props x (text-properties-at (1- (length x)) x)
4408 'type "diary" 'date date 'face 'org-agenda-diary))
4409 entries)))))
4411 (defvar org-agenda-cleanup-fancy-diary-hook nil
4412 "Hook run when the fancy diary buffer is cleaned up.")
4414 (defun org-agenda-cleanup-fancy-diary ()
4415 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4416 This gets rid of the date, the underline under the date, and
4417 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4418 date. It also removes lines that contain only whitespace."
4419 (goto-char (point-min))
4420 (if (looking-at ".*?:[ \t]*")
4421 (progn
4422 (replace-match "")
4423 (re-search-forward "\n=+$" nil t)
4424 (replace-match "")
4425 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4426 (re-search-forward "\n=+$" nil t)
4427 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4428 (goto-char (point-min))
4429 (while (re-search-forward "^ +\n" nil t)
4430 (replace-match ""))
4431 (goto-char (point-min))
4432 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4433 (replace-match ""))
4434 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4436 ;; Make sure entries from the diary have the right text properties.
4437 (eval-after-load "diary-lib"
4438 '(if (boundp 'diary-modify-entry-list-string-function)
4439 ;; We can rely on the hook, nothing to do
4441 ;; Hook not available, must use advice to make this work
4442 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4443 "Make the position visible."
4444 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4445 (stringp string)
4446 buffer-file-name)
4447 (setq string (org-modify-diary-entry-string string))))))
4449 (defun org-modify-diary-entry-string (string)
4450 "Add text properties to string, allowing org-mode to act on it."
4451 (org-add-props string nil
4452 'mouse-face 'highlight
4453 'help-echo (if buffer-file-name
4454 (format "mouse-2 or RET jump to diary file %s"
4455 (abbreviate-file-name buffer-file-name))
4457 'org-agenda-diary-link t
4458 'org-marker (org-agenda-new-marker (point-at-bol))))
4460 (defun org-diary-default-entry ()
4461 "Add a dummy entry to the diary.
4462 Needed to avoid empty dates which mess up holiday display."
4463 ;; Catch the error if dealing with the new add-to-diary-alist
4464 (when org-disable-agenda-to-diary
4465 (condition-case nil
4466 (org-add-to-diary-list original-date "Org-mode dummy" "")
4467 (error
4468 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4470 (defun org-add-to-diary-list (&rest args)
4471 (if (fboundp 'diary-add-to-list)
4472 (apply 'diary-add-to-list args)
4473 (apply 'add-to-diary-list args)))
4475 (defvar org-diary-last-run-time nil)
4477 ;;;###autoload
4478 (defun org-diary (&rest args)
4479 "Return diary information from org-files.
4480 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4481 It accesses org files and extracts information from those files to be
4482 listed in the diary. The function accepts arguments specifying what
4483 items should be listed. For a list of arguments allowed here, see the
4484 variable `org-agenda-entry-types'.
4486 The call in the diary file should look like this:
4488 &%%(org-diary) ~/path/to/some/orgfile.org
4490 Use a separate line for each org file to check. Or, if you omit the file name,
4491 all files listed in `org-agenda-files' will be checked automatically:
4493 &%%(org-diary)
4495 If you don't give any arguments (as in the example above), the default
4496 arguments (:deadline :scheduled :timestamp :sexp) are used.
4497 So the example above may also be written as
4499 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4501 The function expects the lisp variables `entry' and `date' to be provided
4502 by the caller, because this is how the calendar works. Don't use this
4503 function from a program - use `org-agenda-get-day-entries' instead."
4504 (when (> (- (org-float-time)
4505 org-agenda-last-marker-time)
4507 (org-agenda-reset-markers))
4508 (org-compile-prefix-format 'agenda)
4509 (org-set-sorting-strategy 'agenda)
4510 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4511 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4512 (list entry)
4513 (org-agenda-files t)))
4514 (time (org-float-time))
4515 file rtn results)
4516 (when (or (not org-diary-last-run-time)
4517 (> (- time
4518 org-diary-last-run-time)
4520 (org-prepare-agenda-buffers files))
4521 (setq org-diary-last-run-time time)
4522 ;; If this is called during org-agenda, don't return any entries to
4523 ;; the calendar. Org Agenda will list these entries itself.
4524 (if org-disable-agenda-to-diary (setq files nil))
4525 (while (setq file (pop files))
4526 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4527 (setq results (append results rtn)))
4528 (if results
4529 (concat (org-finalize-agenda-entries results) "\n"))))
4531 ;;; Agenda entry finders
4533 (defun org-agenda-get-day-entries (file date &rest args)
4534 "Does the work for `org-diary' and `org-agenda'.
4535 FILE is the path to a file to be checked for entries. DATE is date like
4536 the one returned by `calendar-current-date'. ARGS are symbols indicating
4537 which kind of entries should be extracted. For details about these, see
4538 the documentation of `org-diary'."
4539 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4540 (let* ((org-startup-folded nil)
4541 (org-startup-align-all-tables nil)
4542 (buffer (if (file-exists-p file)
4543 (org-get-agenda-file-buffer file)
4544 (error "No such file %s" file)))
4545 arg results rtn deadline-results)
4546 (if (not buffer)
4547 ;; If file does not exist, make sure an error message ends up in diary
4548 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4549 (with-current-buffer buffer
4550 (unless (eq major-mode 'org-mode)
4551 (error "Agenda file %s is not in `org-mode'" file))
4552 (let ((case-fold-search nil))
4553 (save-excursion
4554 (save-restriction
4555 (if org-agenda-restrict
4556 (narrow-to-region org-agenda-restrict-begin
4557 org-agenda-restrict-end)
4558 (widen))
4559 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4560 (while (setq arg (pop args))
4561 (cond
4562 ((and (eq arg :todo)
4563 (equal date (calendar-gregorian-from-absolute
4564 (org-today))))
4565 (setq rtn (org-agenda-get-todos))
4566 (setq results (append results rtn)))
4567 ((eq arg :timestamp)
4568 (setq rtn (org-agenda-get-blocks))
4569 (setq results (append results rtn))
4570 (setq rtn (org-agenda-get-timestamps))
4571 (setq results (append results rtn)))
4572 ((eq arg :sexp)
4573 (setq rtn (org-agenda-get-sexps))
4574 (setq results (append results rtn)))
4575 ((eq arg :scheduled)
4576 (setq rtn (org-agenda-get-scheduled deadline-results))
4577 (setq results (append results rtn)))
4578 ((eq arg :closed)
4579 (setq rtn (org-agenda-get-progress))
4580 (setq results (append results rtn)))
4581 ((eq arg :deadline)
4582 (setq rtn (org-agenda-get-deadlines))
4583 (setq deadline-results (copy-sequence rtn))
4584 (setq results (append results rtn))))))))
4585 results))))
4587 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4588 (defun org-agenda-get-todos ()
4589 "Return the TODO information for agenda display."
4590 (let* ((props (list 'face nil
4591 'done-face 'org-agenda-done
4592 'org-not-done-regexp org-not-done-regexp
4593 'org-todo-regexp org-todo-regexp
4594 'org-complex-heading-regexp org-complex-heading-regexp
4595 'mouse-face 'highlight
4596 'help-echo
4597 (format "mouse-2 or RET jump to org file %s"
4598 (abbreviate-file-name buffer-file-name))))
4599 (regexp (format org-heading-keyword-regexp-format
4600 (cond
4601 ((and org-select-this-todo-keyword
4602 (equal org-select-this-todo-keyword "*"))
4603 org-todo-regexp)
4604 (org-select-this-todo-keyword
4605 (concat "\\("
4606 (mapconcat 'identity
4607 (org-split-string
4608 org-select-this-todo-keyword
4609 "|")
4610 "\\|") "\\)"))
4611 (t org-not-done-regexp))))
4612 marker priority category org-category-pos tags todo-state
4613 ee txt beg end)
4614 (goto-char (point-min))
4615 (while (re-search-forward regexp nil t)
4616 (catch :skip
4617 (save-match-data
4618 (beginning-of-line)
4619 (org-agenda-skip)
4620 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4621 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4622 (goto-char (1+ beg))
4623 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4624 (throw :skip nil)))
4625 (goto-char (match-beginning 2))
4626 (setq marker (org-agenda-new-marker (match-beginning 0))
4627 category (org-get-category)
4628 org-category-pos (get-text-property (point) 'org-category-position)
4629 txt (org-trim
4630 (buffer-substring (match-beginning 2) (match-end 0)))
4631 tags (org-get-tags-at (point))
4632 txt (org-agenda-format-item "" txt category tags)
4633 priority (1+ (org-get-priority txt))
4634 todo-state (org-get-todo-state))
4635 (org-add-props txt props
4636 'org-marker marker 'org-hd-marker marker
4637 'priority priority 'org-category category
4638 'org-category-position org-category-pos
4639 'type "todo" 'todo-state todo-state)
4640 (push txt ee)
4641 (if org-agenda-todo-list-sublevels
4642 (goto-char (match-end 2))
4643 (org-end-of-subtree 'invisible))))
4644 (nreverse ee)))
4646 (defun org-agenda-todo-custom-ignore-p (time n)
4647 "Check whether timestamp is farther away then n number of days.
4648 This function is invoked if `org-agenda-todo-ignore-deadlines',
4649 `org-agenda-todo-ignore-scheduled' or
4650 `org-agenda-todo-ignore-timestamp' is set to an integer."
4651 (let ((days (org-days-to-time time)))
4652 (if (>= n 0)
4653 (>= days n)
4654 (<= days n))))
4656 ;;;###autoload
4657 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4658 (&optional end)
4659 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4660 (when (or org-agenda-todo-ignore-with-date
4661 org-agenda-todo-ignore-scheduled
4662 org-agenda-todo-ignore-deadlines
4663 org-agenda-todo-ignore-timestamp)
4664 (setq end (or end (save-excursion (outline-next-heading) (point))))
4665 (save-excursion
4666 (or (and org-agenda-todo-ignore-with-date
4667 (re-search-forward org-ts-regexp end t))
4668 (and org-agenda-todo-ignore-scheduled
4669 (re-search-forward org-scheduled-time-regexp end t)
4670 (cond
4671 ((eq org-agenda-todo-ignore-scheduled 'future)
4672 (> (org-days-to-time (match-string 1)) 0))
4673 ((eq org-agenda-todo-ignore-scheduled 'past)
4674 (<= (org-days-to-time (match-string 1)) 0))
4675 ((numberp org-agenda-todo-ignore-scheduled)
4676 (org-agenda-todo-custom-ignore-p
4677 (match-string 1) org-agenda-todo-ignore-scheduled))
4678 (t)))
4679 (and org-agenda-todo-ignore-deadlines
4680 (re-search-forward org-deadline-time-regexp end t)
4681 (cond
4682 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4683 ((eq org-agenda-todo-ignore-deadlines 'far)
4684 (not (org-deadline-close (match-string 1))))
4685 ((eq org-agenda-todo-ignore-deadlines 'future)
4686 (> (org-days-to-time (match-string 1)) 0))
4687 ((eq org-agenda-todo-ignore-deadlines 'past)
4688 (<= (org-days-to-time (match-string 1)) 0))
4689 ((numberp org-agenda-todo-ignore-deadlines)
4690 (org-agenda-todo-custom-ignore-p
4691 (match-string 1) org-agenda-todo-ignore-deadlines))
4692 (t (org-deadline-close (match-string 1)))))
4693 (and org-agenda-todo-ignore-timestamp
4694 (let ((buffer (current-buffer))
4695 (regexp
4696 (concat
4697 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4698 (start (point)))
4699 ;; Copy current buffer into a temporary one
4700 (with-temp-buffer
4701 (insert-buffer-substring buffer start end)
4702 (goto-char (point-min))
4703 ;; Delete SCHEDULED and DEADLINE items
4704 (while (re-search-forward regexp end t)
4705 (delete-region (match-beginning 0) (match-end 0)))
4706 (goto-char (point-min))
4707 ;; No search for timestamp left
4708 (when (re-search-forward org-ts-regexp nil t)
4709 (cond
4710 ((eq org-agenda-todo-ignore-timestamp 'future)
4711 (> (org-days-to-time (match-string 1)) 0))
4712 ((eq org-agenda-todo-ignore-timestamp 'past)
4713 (<= (org-days-to-time (match-string 1)) 0))
4714 ((numberp org-agenda-todo-ignore-timestamp)
4715 (org-agenda-todo-custom-ignore-p
4716 (match-string 1) org-agenda-todo-ignore-timestamp))
4717 (t))))))))))
4719 (defconst org-agenda-no-heading-message
4720 "No heading for this item in buffer or region.")
4722 (defun org-agenda-get-timestamps ()
4723 "Return the date stamp information for agenda display."
4724 (let* ((props (list 'face nil
4725 'org-not-done-regexp org-not-done-regexp
4726 'org-todo-regexp org-todo-regexp
4727 'org-complex-heading-regexp org-complex-heading-regexp
4728 'mouse-face 'highlight
4729 'help-echo
4730 (format "mouse-2 or RET jump to org file %s"
4731 (abbreviate-file-name buffer-file-name))))
4732 (d1 (calendar-absolute-from-gregorian date))
4733 (remove-re
4734 (concat
4735 (regexp-quote
4736 (format-time-string
4737 "<%Y-%m-%d"
4738 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4739 ".*?>"))
4740 (regexp
4741 (concat
4742 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4743 (regexp-quote
4744 (substring
4745 (format-time-string
4746 (car org-time-stamp-formats)
4747 (apply 'encode-time ; DATE bound by calendar
4748 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4749 1 11))
4750 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4751 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4752 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4753 donep tmp priority category org-category-pos ee txt timestr tags
4754 b0 b3 e3 head todo-state end-of-match show-all)
4755 (goto-char (point-min))
4756 (while (setq end-of-match (re-search-forward regexp nil t))
4757 (setq b0 (match-beginning 0)
4758 b3 (match-beginning 3) e3 (match-end 3)
4759 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4760 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4761 (member todo-state
4762 org-agenda-repeating-timestamp-show-all)))
4763 (catch :skip
4764 (and (org-at-date-range-p) (throw :skip nil))
4765 (org-agenda-skip)
4766 (if (and (match-end 1)
4767 (not (= d1 (org-time-string-to-absolute
4768 (match-string 1) d1 nil show-all
4769 (current-buffer) b0))))
4770 (throw :skip nil))
4771 (if (and e3
4772 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4773 (throw :skip nil))
4774 (setq tmp (buffer-substring (max (point-min)
4775 (- b0 org-ds-keyword-length))
4777 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4778 inactivep (= (char-after b0) ?\[)
4779 deadlinep (string-match org-deadline-regexp tmp)
4780 scheduledp (string-match org-scheduled-regexp tmp)
4781 closedp (and org-agenda-include-inactive-timestamps
4782 (string-match org-closed-string tmp))
4783 clockp (and org-agenda-include-inactive-timestamps
4784 (or (string-match org-clock-string tmp)
4785 (string-match "]-+\\'" tmp)))
4786 donep (member todo-state org-done-keywords))
4787 (if (or scheduledp deadlinep closedp clockp
4788 (and donep org-agenda-skip-timestamp-if-done))
4789 (throw :skip t))
4790 (if (string-match ">" timestr)
4791 ;; substring should only run to end of time stamp
4792 (setq timestr (substring timestr 0 (match-end 0))))
4793 (setq marker (org-agenda-new-marker b0)
4794 category (org-get-category b0)
4795 org-category-pos (get-text-property b0 'org-category-position))
4796 (save-excursion
4797 (if (not (re-search-backward org-outline-regexp-bol nil t))
4798 (setq txt org-agenda-no-heading-message)
4799 (goto-char (match-beginning 0))
4800 (setq hdmarker (org-agenda-new-marker)
4801 tags (org-get-tags-at))
4802 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4803 (setq head (or (match-string 1) ""))
4804 (setq txt (org-agenda-format-item
4805 (if inactivep org-agenda-inactive-leader nil)
4806 head category tags timestr
4807 remove-re)))
4808 (setq priority (org-get-priority txt))
4809 (org-add-props txt props
4810 'org-marker marker 'org-hd-marker hdmarker)
4811 (org-add-props txt nil 'priority priority
4812 'org-category category 'date date
4813 'org-category-position org-category-pos
4814 'todo-state todo-state
4815 'type "timestamp")
4816 (push txt ee))
4817 (if org-agenda-skip-additional-timestamps-same-entry
4818 (outline-next-heading)
4819 (goto-char end-of-match))))
4820 (nreverse ee)))
4822 (defun org-agenda-get-sexps ()
4823 "Return the sexp information for agenda display."
4824 (require 'diary-lib)
4825 (let* ((props (list 'mouse-face 'highlight
4826 'help-echo
4827 (format "mouse-2 or RET jump to org file %s"
4828 (abbreviate-file-name buffer-file-name))))
4829 (regexp "^&?%%(")
4830 marker category org-category-pos ee txt tags entry
4831 result beg b sexp sexp-entry todo-state)
4832 (goto-char (point-min))
4833 (while (re-search-forward regexp nil t)
4834 (catch :skip
4835 (org-agenda-skip)
4836 (setq beg (match-beginning 0))
4837 (goto-char (1- (match-end 0)))
4838 (setq b (point))
4839 (forward-sexp 1)
4840 (setq sexp (buffer-substring b (point)))
4841 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4842 (org-trim (match-string 1))
4843 ""))
4844 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4845 (when result
4846 (setq marker (org-agenda-new-marker beg)
4847 category (org-get-category beg)
4848 org-category-pos (get-text-property beg 'org-category-position)
4849 todo-state (org-get-todo-state))
4851 (dolist (r (if (stringp result)
4852 (list result)
4853 result)) ;; we expect a list here
4854 (if (string-match "\\S-" r)
4855 (setq txt r)
4856 (setq txt "SEXP entry returned empty string"))
4858 (setq txt (org-agenda-format-item
4859 "" txt category tags 'time))
4860 (org-add-props txt props 'org-marker marker)
4861 (org-add-props txt nil
4862 'org-category category 'date date 'todo-state todo-state
4863 'org-category-position org-category-pos
4864 'type "sexp")
4865 (push txt ee)))))
4866 (nreverse ee)))
4868 ;; Calendar sanity: define some functions that are independent of
4869 ;; `calendar-date-style'.
4870 ;; Normally I would like to use ISO format when calling the diary functions,
4871 ;; but to make sure we still have Emacs 22 compatibility we bind
4872 ;; also `european-calendar-style' and use european format
4873 (defun org-anniversary (year month day &optional mark)
4874 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4875 (org-no-warnings
4876 (let ((calendar-date-style 'european) (european-calendar-style t))
4877 (diary-anniversary day month year mark))))
4878 (defun org-cyclic (N year month day &optional mark)
4879 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4880 (org-no-warnings
4881 (let ((calendar-date-style 'european) (european-calendar-style t))
4882 (diary-cyclic N day month year mark))))
4883 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4884 "Like `diary-block', but with fixed (ISO) order of arguments."
4885 (org-no-warnings
4886 (let ((calendar-date-style 'european) (european-calendar-style t))
4887 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4888 (defun org-date (year month day &optional mark)
4889 "Like `diary-date', but with fixed (ISO) order of arguments."
4890 (org-no-warnings
4891 (let ((calendar-date-style 'european) (european-calendar-style t))
4892 (diary-date day month year mark))))
4893 (defalias 'org-float 'diary-float)
4895 ;; Define the` org-class' function
4896 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4897 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4898 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
4899 SKIP-WEEKS is any number of ISO weeks in the block period for which the
4900 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
4901 `holidays', then any date that is known by the Emacs calendar to be a
4902 holidy will also be skipped."
4903 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4904 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4905 (d (calendar-absolute-from-gregorian date)))
4906 (and
4907 (<= date1 d)
4908 (<= d date2)
4909 (= (calendar-day-of-week date) dayname)
4910 (or (not skip-weeks)
4911 (progn
4912 (require 'cal-iso)
4913 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4914 (not (and (memq 'holidays skip-weeks)
4915 (calendar-check-holidays date)))
4916 entry)))
4918 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4919 "Like `org-class', but honor `calendar-date-style'.
4920 The order of the first 2 times 3 arguments depends on the variable
4921 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4922 So for American calendars, give this as MONTH DAY YEAR, for European as
4923 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4924 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4925 is any number of ISO weeks in the block period for which the item should
4926 be skipped.
4928 This function is here only for backward compatibility and it is deprecated,
4929 please use `org-class' instead."
4930 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4931 (date2 (org-order-calendar-date-args m2 d2 y2)))
4932 (org-class
4933 (nth 2 date1) (car date1) (nth 1 date1)
4934 (nth 2 date2) (car date2) (nth 1 date2)
4935 dayname skip-weeks)))
4936 (make-obsolete 'org-diary-class 'org-class "")
4938 (defalias 'org-get-closed 'org-agenda-get-progress)
4939 (defun org-agenda-get-progress ()
4940 "Return the logged TODO entries for agenda display."
4941 (let* ((props (list 'mouse-face 'highlight
4942 'org-not-done-regexp org-not-done-regexp
4943 'org-todo-regexp org-todo-regexp
4944 'org-complex-heading-regexp org-complex-heading-regexp
4945 'help-echo
4946 (format "mouse-2 or RET jump to org file %s"
4947 (abbreviate-file-name buffer-file-name))))
4948 (items (if (consp org-agenda-show-log)
4949 org-agenda-show-log
4950 (if (eq org-agenda-show-log 'clockcheck)
4951 '(clock)
4952 org-agenda-log-mode-items)))
4953 (parts
4954 (delq nil
4955 (list
4956 (if (memq 'closed items) (concat "\\<" org-closed-string))
4957 (if (memq 'clock items) (concat "\\<" org-clock-string))
4958 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4959 (parts-re (if parts (mapconcat 'identity parts "\\|")
4960 (error "`org-agenda-log-mode-items' is empty")))
4961 (regexp (concat
4962 "\\(" parts-re "\\)"
4963 " *\\["
4964 (regexp-quote
4965 (substring
4966 (format-time-string
4967 (car org-time-stamp-formats)
4968 (apply 'encode-time ; DATE bound by calendar
4969 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4970 1 11))))
4971 (org-agenda-search-headline-for-time nil)
4972 marker hdmarker priority category org-category-pos tags closedp
4973 statep clockp state ee txt extra timestr rest clocked)
4974 (goto-char (point-min))
4975 (while (re-search-forward regexp nil t)
4976 (catch :skip
4977 (org-agenda-skip)
4978 (setq marker (org-agenda-new-marker (match-beginning 0))
4979 closedp (equal (match-string 1) org-closed-string)
4980 statep (equal (string-to-char (match-string 1)) ?-)
4981 clockp (not (or closedp statep))
4982 state (and statep (match-string 2))
4983 category (org-get-category (match-beginning 0))
4984 org-category-pos (get-text-property (match-beginning 0) 'org-category-position)
4985 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
4986 (when (string-match "\\]" timestr)
4987 ;; substring should only run to end of time stamp
4988 (setq rest (substring timestr (match-end 0))
4989 timestr (substring timestr 0 (match-end 0)))
4990 (if (and (not closedp) (not statep)
4991 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
4992 rest))
4993 (progn (setq timestr (concat (substring timestr 0 -1)
4994 "-" (match-string 1 rest) "]"))
4995 (setq clocked (match-string 2 rest)))
4996 (setq clocked "-")))
4997 (save-excursion
4998 (setq extra
4999 (cond
5000 ((not org-agenda-log-mode-add-notes) nil)
5001 (statep
5002 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5003 (match-string 1)))
5004 (clockp
5005 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5006 (match-string 1)))))
5007 (if (not (re-search-backward org-outline-regexp-bol nil t))
5008 (setq txt org-agenda-no-heading-message)
5009 (goto-char (match-beginning 0))
5010 (setq hdmarker (org-agenda-new-marker)
5011 tags (org-get-tags-at))
5012 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5013 (setq txt (match-string 1))
5014 (when extra
5015 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5016 (setq txt (concat (substring txt 0 (match-beginning 1))
5017 " - " extra " " (match-string 2 txt)))
5018 (setq txt (concat txt " - " extra))))
5019 (setq txt (org-agenda-format-item
5020 (cond
5021 (closedp "Closed: ")
5022 (statep (concat "State: (" state ")"))
5023 (t (concat "Clocked: (" clocked ")")))
5024 txt category tags timestr)))
5025 (setq priority 100000)
5026 (org-add-props txt props
5027 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5028 'priority priority 'org-category category
5029 'org-category-position org-category-pos
5030 'type "closed" 'date date
5031 'undone-face 'org-warning 'done-face 'org-agenda-done)
5032 (push txt ee))
5033 (goto-char (point-at-eol))))
5034 (nreverse ee)))
5036 (defun org-agenda-show-clocking-issues ()
5037 "Add overlays, showing issues with clocking.
5038 See also the user option `org-agenda-clock-consistency-checks'."
5039 (interactive)
5040 (let* ((pl org-agenda-clock-consistency-checks)
5041 (re (concat "^[ \t]*"
5042 org-clock-string
5043 "[ \t]+"
5044 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5045 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5046 (tlstart 0.)
5047 (tlend 0.)
5048 (maxtime (org-hh:mm-string-to-minutes
5049 (or (plist-get pl :max-duration) "24:00")))
5050 (mintime (org-hh:mm-string-to-minutes
5051 (or (plist-get pl :min-duration) 0)))
5052 (maxgap (org-hh:mm-string-to-minutes
5053 ;; default 30:00 means never complain
5054 (or (plist-get pl :max-gap) "30:00")))
5055 (gapok (mapcar 'org-hh:mm-string-to-minutes
5056 (plist-get pl :gap-ok-around)))
5057 (def-face (or (plist-get pl :default-face)
5058 '((:background "DarkRed") (:foreground "white"))))
5059 issue face m te ts dt ov)
5060 (goto-char (point-min))
5061 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5062 (setq issue nil face def-face)
5063 (catch 'next
5064 (setq m (org-get-at-bol 'org-marker)
5065 te nil ts nil)
5066 (unless (and m (markerp m))
5067 (setq issue "No valid clock line") (throw 'next t))
5068 (org-with-point-at m
5069 (save-excursion
5070 (goto-char (point-at-bol))
5071 (unless (looking-at re)
5072 (error "No valid Clock line")
5073 (throw 'next t))
5074 (unless (match-end 3)
5075 (setq issue "No end time"
5076 face (or (plist-get pl :no-end-time-face) face))
5077 (throw 'next t))
5078 (setq ts (match-string 1)
5079 te (match-string 3)
5080 ts (org-float-time
5081 (apply 'encode-time (org-parse-time-string ts)))
5082 te (org-float-time
5083 (apply 'encode-time (org-parse-time-string te)))
5084 dt (- te ts))))
5085 (cond
5086 ((> dt (* 60 maxtime))
5087 ;; a very long clocking chunk
5088 (setq issue (format "Clocking interval is very long: %s"
5089 (org-minutes-to-hh:mm-string
5090 (floor (/ (float dt) 60.))))
5091 face (or (plist-get pl :long-face) face)))
5092 ((< dt (* 60 mintime))
5093 ;; a very short clocking chunk
5094 (setq issue (format "Clocking interval is very short: %s"
5095 (org-minutes-to-hh:mm-string
5096 (floor (/ (float dt) 60.))))
5097 face (or (plist-get pl :short-face) face)))
5098 ((and (> tlend 0) (< ts tlend))
5099 ;; Two clock entries are overlapping
5100 (setq issue (format "Clocking overlap: %d minutes"
5101 (/ (- tlend ts) 60))
5102 face (or (plist-get pl :overlap-face) face)))
5103 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5104 ;; There is a gap, lets see if we need to report it
5105 (unless (org-agenda-check-clock-gap tlend ts gapok)
5106 (setq issue (format "Clocking gap: %d minutes"
5107 (/ (- ts tlend) 60))
5108 face (or (plist-get pl :gap-face) face))))
5109 (t nil)))
5110 (setq tlend (or te tlend) tlstart (or ts tlstart))
5111 (when issue
5112 ;; OK, there was some issue, add an overlay to show the issue
5113 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5114 (overlay-put ov 'before-string
5115 (concat
5116 (org-add-props
5117 (format "%-43s" (concat " " issue))
5119 'face face)
5120 "\n"))
5121 (overlay-put ov 'evaporate t)))))
5123 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5124 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5125 (catch 'exit
5126 (unless ok-list
5127 ;; there are no OK times for gaps...
5128 (throw 'exit nil))
5129 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5130 ;; This is more than 24 hours, so it is OK.
5131 ;; because we have at least one OK time, that must be in the
5132 ;; 24 hour interval.
5133 (throw 'exit t))
5134 ;; We have a shorter gap.
5135 ;; Now we have to get the minute of the day when these times are
5136 (let* ((t1dec (decode-time (seconds-to-time t1)))
5137 (t2dec (decode-time (seconds-to-time t2)))
5138 ;; compute the minute on the day
5139 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5140 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5141 (when (< min2 min1)
5142 ;; if min2 is smaller than min1, this means it is on the next day.
5143 ;; Wrap it to after midnight.
5144 (setq min2 (+ min2 1440)))
5145 ;; Now check if any of the OK times is in the gap
5146 (mapc (lambda (x)
5147 ;; Wrap the time to after midnight if necessary
5148 (if (< x min1) (setq x (+ x 1440)))
5149 ;; Check if in interval
5150 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5151 ok-list)
5152 ;; Nope, this gap is not OK
5153 nil)))
5155 (defun org-agenda-get-deadlines ()
5156 "Return the deadline information for agenda display."
5157 (let* ((props (list 'mouse-face 'highlight
5158 'org-not-done-regexp org-not-done-regexp
5159 'org-todo-regexp org-todo-regexp
5160 'org-complex-heading-regexp org-complex-heading-regexp
5161 'help-echo
5162 (format "mouse-2 or RET jump to org file %s"
5163 (abbreviate-file-name buffer-file-name))))
5164 (regexp org-deadline-time-regexp)
5165 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5166 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5167 d2 diff dfrac wdays pos pos1 category org-category-pos
5168 tags suppress-prewarning ee txt head face s todo-state
5169 show-all upcomingp donep timestr)
5170 (goto-char (point-min))
5171 (while (re-search-forward regexp nil t)
5172 (setq suppress-prewarning nil)
5173 (catch :skip
5174 (org-agenda-skip)
5175 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5176 (save-match-data
5177 (string-match org-scheduled-time-regexp
5178 (buffer-substring (point-at-bol)
5179 (point-at-eol)))))
5180 (setq suppress-prewarning
5181 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5182 org-agenda-skip-deadline-prewarning-if-scheduled
5183 0)))
5184 (setq s (match-string 1)
5185 txt nil
5186 pos (1- (match-beginning 1))
5187 todo-state (save-match-data (org-get-todo-state))
5188 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5189 (member todo-state
5190 org-agenda-repeating-timestamp-show-all))
5191 d2 (org-time-string-to-absolute
5192 (match-string 1) d1 'past show-all
5193 (current-buffer) pos)
5194 diff (- d2 d1)
5195 wdays (if suppress-prewarning
5196 (let ((org-deadline-warning-days suppress-prewarning))
5197 (org-get-wdays s))
5198 (org-get-wdays s))
5199 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5200 upcomingp (and todayp (> diff 0)))
5201 ;; When to show a deadline in the calendar:
5202 ;; If the expiration is within wdays warning time.
5203 ;; Past-due deadlines are only shown on the current date
5204 (if (and (or (and (<= diff wdays)
5205 (and todayp (not org-agenda-only-exact-dates)))
5206 (= diff 0)))
5207 (save-excursion
5208 ;; (setq todo-state (org-get-todo-state))
5209 (setq donep (member todo-state org-done-keywords))
5210 (if (and donep
5211 (or org-agenda-skip-deadline-if-done
5212 (not (= diff 0))))
5213 (setq txt nil)
5214 (setq category (org-get-category)
5215 org-category-pos (get-text-property (point) 'org-category-position))
5216 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5217 (setq txt org-agenda-no-heading-message)
5218 (goto-char (match-end 0))
5219 (setq pos1 (match-beginning 0))
5220 (setq tags (org-get-tags-at pos1))
5221 (setq head (buffer-substring-no-properties
5222 (point)
5223 (progn (skip-chars-forward "^\r\n")
5224 (point))))
5225 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5226 (setq timestr
5227 (concat (substring s (match-beginning 1)) " "))
5228 (setq timestr 'time))
5229 (setq txt (org-agenda-format-item
5230 (if (= diff 0)
5231 (car org-agenda-deadline-leaders)
5232 (if (functionp
5233 (nth 1 org-agenda-deadline-leaders))
5234 (funcall
5235 (nth 1 org-agenda-deadline-leaders)
5236 diff date)
5237 (format (nth 1 org-agenda-deadline-leaders)
5238 diff)))
5239 head category tags
5240 (if (not (= diff 0)) nil timestr)))))
5241 (when txt
5242 (setq face (org-agenda-deadline-face dfrac))
5243 (org-add-props txt props
5244 'org-marker (org-agenda-new-marker pos)
5245 'org-hd-marker (org-agenda-new-marker pos1)
5246 'priority (+ (- diff)
5247 (org-get-priority txt))
5248 'org-category category
5249 'org-category-position org-category-pos
5250 'todo-state todo-state
5251 'type (if upcomingp "upcoming-deadline" "deadline")
5252 'date (if upcomingp date d2)
5253 'face (if donep 'org-agenda-done face)
5254 'undone-face face 'done-face 'org-agenda-done)
5255 (push txt ee))))))
5256 (nreverse ee)))
5258 (defun org-agenda-deadline-face (fraction)
5259 "Return the face to displaying a deadline item.
5260 FRACTION is what fraction of the head-warning time has passed."
5261 (let ((faces org-agenda-deadline-faces) f)
5262 (catch 'exit
5263 (while (setq f (pop faces))
5264 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5266 (defun org-agenda-get-scheduled (&optional deadline-results)
5267 "Return the scheduled information for agenda display."
5268 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5269 'org-todo-regexp org-todo-regexp
5270 'org-complex-heading-regexp org-complex-heading-regexp
5271 'done-face 'org-agenda-done
5272 'mouse-face 'highlight
5273 'help-echo
5274 (format "mouse-2 or RET jump to org file %s"
5275 (abbreviate-file-name buffer-file-name))))
5276 (regexp org-scheduled-time-regexp)
5277 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5278 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5280 (deadline-position-alist
5281 (mapcar (lambda (a) (and (setq mm (get-text-property
5282 0 'org-hd-marker a))
5283 (cons (marker-position mm) a)))
5284 deadline-results))
5285 d2 diff pos pos1 category org-category-pos tags donep
5286 ee txt head pastschedp todo-state face timestr s habitp show-all)
5287 (goto-char (point-min))
5288 (while (re-search-forward regexp nil t)
5289 (catch :skip
5290 (org-agenda-skip)
5291 (setq s (match-string 1)
5292 txt nil
5293 pos (1- (match-beginning 1))
5294 todo-state (save-match-data (org-get-todo-state))
5295 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5296 (member todo-state
5297 org-agenda-repeating-timestamp-show-all))
5298 d2 (org-time-string-to-absolute
5299 (match-string 1) d1 'past show-all
5300 (current-buffer) pos)
5301 diff (- d2 d1))
5302 (setq pastschedp (and todayp (< diff 0)))
5303 ;; When to show a scheduled item in the calendar:
5304 ;; If it is on or past the date.
5305 (when (or (and (< diff 0)
5306 (< (abs diff) org-scheduled-past-days)
5307 (and todayp (not org-agenda-only-exact-dates)))
5308 (= diff 0))
5309 (save-excursion
5310 (setq donep (member todo-state org-done-keywords))
5311 (if (and donep
5312 (or org-agenda-skip-scheduled-if-done
5313 (not (= diff 0))
5314 (and (functionp 'org-is-habit-p)
5315 (org-is-habit-p))))
5316 (setq txt nil)
5317 (setq habitp (and (functionp 'org-is-habit-p)
5318 (org-is-habit-p)))
5319 (setq category (org-get-category)
5320 org-category-pos (get-text-property (point) 'org-category-position))
5321 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5322 (setq txt org-agenda-no-heading-message)
5323 (goto-char (match-end 0))
5324 (setq pos1 (match-beginning 0))
5325 (if habitp
5326 (if (or (not org-habit-show-habits)
5327 (and (not todayp)
5328 org-habit-show-habits-only-for-today))
5329 (throw :skip nil))
5330 (if (and
5331 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5332 (and org-agenda-skip-scheduled-if-deadline-is-shown
5333 pastschedp))
5334 (setq mm (assoc pos1 deadline-position-alist)))
5335 (throw :skip nil)))
5336 (setq tags (org-get-tags-at))
5337 (setq head (buffer-substring-no-properties
5338 (point)
5339 (progn (skip-chars-forward "^\r\n") (point))))
5340 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5341 (setq timestr
5342 (concat (substring s (match-beginning 1)) " "))
5343 (setq timestr 'time))
5344 (setq txt (org-agenda-format-item
5345 (if (= diff 0)
5346 (car org-agenda-scheduled-leaders)
5347 (format (nth 1 org-agenda-scheduled-leaders)
5348 (- 1 diff)))
5349 head category tags
5350 (if (not (= diff 0)) nil timestr)
5351 nil habitp))))
5352 (when txt
5353 (setq face
5354 (cond
5355 ((and (not habitp) pastschedp)
5356 'org-scheduled-previously)
5357 (todayp 'org-scheduled-today)
5358 (t 'org-scheduled))
5359 habitp (and habitp (org-habit-parse-todo)))
5360 (org-add-props txt props
5361 'undone-face face
5362 'face (if donep 'org-agenda-done face)
5363 'org-marker (org-agenda-new-marker pos)
5364 'org-hd-marker (org-agenda-new-marker pos1)
5365 'type (if pastschedp "past-scheduled" "scheduled")
5366 'date (if pastschedp d2 date)
5367 'priority (if habitp
5368 (org-habit-get-priority habitp)
5369 (+ 94 (- 5 diff) (org-get-priority txt)))
5370 'org-category category
5371 'org-category-position org-category-pos
5372 'org-habit-p habitp
5373 'todo-state todo-state)
5374 (push txt ee))))))
5375 (nreverse ee)))
5377 (defun org-agenda-get-blocks ()
5378 "Return the date-range information for agenda display."
5379 (let* ((props (list 'face nil
5380 'org-not-done-regexp org-not-done-regexp
5381 'org-todo-regexp org-todo-regexp
5382 'org-complex-heading-regexp org-complex-heading-regexp
5383 'mouse-face 'highlight
5384 'help-echo
5385 (format "mouse-2 or RET jump to org file %s"
5386 (abbreviate-file-name buffer-file-name))))
5387 (regexp org-tr-regexp)
5388 (d0 (calendar-absolute-from-gregorian date))
5389 marker hdmarker ee txt d1 d2 s1 s2 category org-category-pos
5390 todo-state tags pos head donep)
5391 (goto-char (point-min))
5392 (while (re-search-forward regexp nil t)
5393 (catch :skip
5394 (org-agenda-skip)
5395 (setq pos (point))
5396 (let ((start-time (match-string 1))
5397 (end-time (match-string 2)))
5398 (setq s1 (match-string 1)
5399 s2 (match-string 2)
5400 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5401 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5402 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5403 ;; Only allow days between the limits, because the normal
5404 ;; date stamps will catch the limits.
5405 (save-excursion
5406 (setq todo-state (org-get-todo-state))
5407 (setq donep (member todo-state org-done-keywords))
5408 (if (and donep org-agenda-skip-timestamp-if-done)
5409 (throw :skip t))
5410 (setq marker (org-agenda-new-marker (point)))
5411 (setq category (org-get-category)
5412 org-category-pos (get-text-property (point) 'org-category-position))
5413 (if (not (re-search-backward org-outline-regexp-bol nil t))
5414 (setq txt org-agenda-no-heading-message)
5415 (goto-char (match-beginning 0))
5416 (setq hdmarker (org-agenda-new-marker (point)))
5417 (setq tags (org-get-tags-at))
5418 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5419 (setq head (match-string 1))
5420 (let ((remove-re
5421 (if org-agenda-remove-timeranges-from-blocks
5422 (concat
5423 "<" (regexp-quote s1) ".*?>"
5424 "--"
5425 "<" (regexp-quote s2) ".*?>")
5426 nil)))
5427 (setq txt (org-agenda-format-item
5428 (format
5429 (nth (if (= d1 d2) 0 1)
5430 org-agenda-timerange-leaders)
5431 (1+ (- d0 d1)) (1+ (- d2 d1)))
5432 head category tags
5433 (cond ((and (= d1 d0) (= d2 d0))
5434 (concat "<" start-time ">--<" end-time ">"))
5435 ((= d1 d0)
5436 (concat "<" start-time ">"))
5437 ((= d2 d0)
5438 (concat "<" end-time ">"))
5439 (t nil))
5440 remove-re))))
5441 (org-add-props txt props
5442 'org-marker marker 'org-hd-marker hdmarker
5443 'type "block" 'date date
5444 'todo-state todo-state
5445 'priority (org-get-priority txt) 'org-category category
5446 'org-category-position org-category-pos)
5447 (push txt ee))))
5448 (goto-char pos)))
5449 ;; Sort the entries by expiration date.
5450 (nreverse ee)))
5452 ;;; Agenda presentation and sorting
5454 (defvar org-prefix-has-time nil
5455 "A flag, set by `org-compile-prefix-format'.
5456 The flag is set if the currently compiled format contains a `%t'.")
5457 (defvar org-prefix-has-tag nil
5458 "A flag, set by `org-compile-prefix-format'.
5459 The flag is set if the currently compiled format contains a `%T'.")
5460 (defvar org-prefix-has-effort nil
5461 "A flag, set by `org-compile-prefix-format'.
5462 The flag is set if the currently compiled format contains a `%e'.")
5463 (defvar org-prefix-category-length nil
5464 "Used by `org-compile-prefix-format' to remember the category field width.")
5465 (defvar org-prefix-category-max-length nil
5466 "Used by `org-compile-prefix-format' to remember the category field width.")
5468 (defun org-agenda-get-category-icon (category)
5469 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5470 (dolist (entry org-agenda-category-icon-alist)
5471 (when (org-string-match-p (car entry) category)
5472 (if (listp (cadr entry))
5473 (return (cadr entry))
5474 (return (apply 'create-image (cdr entry)))))))
5476 (defun org-agenda-format-item (extra txt &optional category tags dotime
5477 remove-re habitp)
5478 "Format TXT to be inserted into the agenda buffer.
5479 In particular, it adds the prefix and corresponding text properties. EXTRA
5480 must be a string and replaces the `%s' specifier in the prefix format.
5481 CATEGORY (string, symbol or nil) may be used to overrule the default
5482 category taken from local variable or file name. It will replace the `%c'
5483 specifier in the format. DOTIME, when non-nil, indicates that a
5484 time-of-day should be extracted from TXT for sorting of this entry, and for
5485 the `%t' specifier in the format. When DOTIME is a string, this string is
5486 searched for a time before TXT is. TAGS can be the tags of the headline.
5487 Any match of REMOVE-RE will be removed from TXT."
5488 (save-match-data
5489 ;; Diary entries sometimes have extra whitespace at the beginning
5490 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5492 ;; Fix the tags part in txt
5493 (setq txt (org-agenda-fix-displayed-tags
5494 txt tags
5495 org-agenda-show-inherited-tags
5496 org-agenda-hide-tags-regexp))
5497 (let* ((category (or category
5498 (if (stringp org-category)
5499 org-category
5500 (and org-category (symbol-name org-category)))
5501 (if buffer-file-name
5502 (file-name-sans-extension
5503 (file-name-nondirectory buffer-file-name))
5504 "")))
5505 (category-icon (org-agenda-get-category-icon category))
5506 (category-icon (if category-icon
5507 (propertize " " 'display category-icon)
5508 ""))
5509 ;; time, tag, effort are needed for the eval of the prefix format
5510 (tag (if tags (nth (1- (length tags)) tags) ""))
5511 time effort neffort
5512 (ts (if dotime (concat
5513 (if (stringp dotime) dotime "")
5514 (and org-agenda-search-headline-for-time txt))))
5515 (time-of-day (and dotime (org-get-time-of-day ts)))
5516 stamp plain s0 s1 s2 rtn srp l
5517 duration thecategory)
5518 (and (eq major-mode 'org-mode) buffer-file-name
5519 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5520 (when (and dotime time-of-day)
5521 ;; Extract starting and ending time and move them to prefix
5522 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5523 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5524 (setq s0 (match-string 0 ts)
5525 srp (and stamp (match-end 3))
5526 s1 (match-string (if plain 1 2) ts)
5527 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5529 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5530 ;; them, we might want to remove them there to avoid duplication.
5531 ;; The user can turn this off with a variable.
5532 (if (and org-prefix-has-time
5533 org-agenda-remove-times-when-in-prefix (or stamp plain)
5534 (string-match (concat (regexp-quote s0) " *") txt)
5535 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5536 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5537 (= (match-beginning 0) 0)
5539 (setq txt (replace-match "" nil nil txt))))
5540 ;; Normalize the time(s) to 24 hour
5541 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5542 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5544 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5545 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5546 (setq s2
5547 (org-minutes-to-hh:mm-string
5548 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5550 ;; Compute the duration
5551 (when s2
5552 (setq duration (- (org-hh:mm-string-to-minutes s2)
5553 (org-hh:mm-string-to-minutes s1)))))
5555 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5556 txt)
5557 ;; Tags are in the string
5558 (if (or (eq org-agenda-remove-tags t)
5559 (and org-agenda-remove-tags
5560 org-prefix-has-tag))
5561 (setq txt (replace-match "" t t txt))
5562 (setq txt (replace-match
5563 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5564 (match-string 2 txt))
5565 t t txt))))
5566 (when (eq major-mode 'org-mode)
5567 (setq effort
5568 (condition-case nil
5569 (org-get-effort
5570 (or (get-text-property 0 'org-hd-marker txt)
5571 (get-text-property 0 'org-marker txt)))
5572 (error nil)))
5573 (when effort
5574 (setq neffort (org-duration-string-to-minutes effort)
5575 effort (setq effort (concat "[" effort "]")))))
5576 ;; prevent erroring out with %e format when there is no effort
5577 (or effort (setq effort ""))
5579 (when remove-re
5580 (while (string-match remove-re txt)
5581 (setq txt (replace-match "" t t txt))))
5583 ;; Set org-heading property on `txt' to mark the start of the
5584 ;; heading.
5585 (add-text-properties 0 (length txt) '(org-heading t) txt)
5587 ;; Prepare the variables needed in the eval of the compiled format
5588 (setq time (cond (s2 (concat
5589 (org-agenda-time-of-day-to-ampm-maybe s1)
5590 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5591 (if org-agenda-timegrid-use-ampm " ")))
5592 (s1 (concat
5593 (org-agenda-time-of-day-to-ampm-maybe s1)
5594 (if org-agenda-timegrid-use-ampm
5595 "........ "
5596 "......")))
5597 (t ""))
5598 extra (or (and (not habitp) extra) "")
5599 category (if (symbolp category) (symbol-name category) category)
5600 thecategory (copy-sequence category))
5601 (if (string-match org-bracket-link-regexp category)
5602 (progn
5603 (setq l (if (match-end 3)
5604 (- (match-end 3) (match-beginning 3))
5605 (- (match-end 1) (match-beginning 1))))
5606 (when (< l (or org-prefix-category-length 0))
5607 (setq category (copy-sequence category))
5608 (org-add-props category nil
5609 'extra-space (make-string
5610 (- org-prefix-category-length l 1) ?\ ))))
5611 (if (and org-prefix-category-max-length
5612 (>= (length category) org-prefix-category-max-length))
5613 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5614 ;; Evaluate the compiled format
5615 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5617 ;; And finally add the text properties
5618 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5619 (org-add-props rtn nil
5620 'org-category (if thecategory (downcase thecategory) category)
5621 'tags (mapcar 'org-downcase-keep-props tags)
5622 'org-highest-priority org-highest-priority
5623 'org-lowest-priority org-lowest-priority
5624 'time-of-day time-of-day
5625 'duration duration
5626 'effort effort
5627 'effort-minutes neffort
5628 'txt txt
5629 'time time
5630 'extra extra
5631 'format org-prefix-format-compiled
5632 'dotime dotime))))
5634 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5635 "Remove tags string from TXT, and add a modified list of tags.
5636 The modified list may contain inherited tags, and tags matched by
5637 `org-agenda-hide-tags-regexp' will be removed."
5638 (when (or add-inherited hide-re)
5639 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5640 (setq txt (substring txt 0 (match-beginning 0))))
5641 (setq tags
5642 (delq nil
5643 (mapcar (lambda (tg)
5644 (if (or (and hide-re (string-match hide-re tg))
5645 (and (not add-inherited)
5646 (get-text-property 0 'inherited tg)))
5648 tg))
5649 tags)))
5650 (when tags
5651 (let ((have-i (get-text-property 0 'inherited (car tags)))
5653 (setq txt (concat txt " :"
5654 (mapconcat
5655 (lambda (x)
5656 (setq i (get-text-property 0 'inherited x))
5657 (if (and have-i (not i))
5658 (progn
5659 (setq have-i nil)
5660 (concat ":" x))
5662 tags ":")
5663 (if have-i "::" ":"))))))
5664 txt)
5666 (defun org-downcase-keep-props (s)
5667 (let ((props (text-properties-at 0 s)))
5668 (setq s (downcase s))
5669 (add-text-properties 0 (length s) props s)
5672 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5673 (defvar org-agenda-sorting-strategy-selected nil)
5675 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5676 (catch 'exit
5677 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5678 ((and todayp (member 'today (car org-agenda-time-grid))))
5679 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5680 ((member 'weekly (car org-agenda-time-grid)))
5681 (t (throw 'exit list)))
5682 (let* ((have (delq nil (mapcar
5683 (lambda (x) (get-text-property 1 'time-of-day x))
5684 list)))
5685 (string (nth 1 org-agenda-time-grid))
5686 (gridtimes (nth 2 org-agenda-time-grid))
5687 (req (car org-agenda-time-grid))
5688 (remove (member 'remove-match req))
5689 new time)
5690 (if (and (member 'require-timed req) (not have))
5691 ;; don't show empty grid
5692 (throw 'exit list))
5693 (while (setq time (pop gridtimes))
5694 (unless (and remove (member time have))
5695 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5696 (push (org-agenda-format-item
5697 nil string "" nil
5698 (concat (substring time 0 -2) ":" (substring time -2)))
5699 new)
5700 (put-text-property
5701 2 (length (car new)) 'face 'org-time-grid (car new))))
5702 (when (and todayp org-agenda-show-current-time-in-grid)
5703 (push (org-agenda-format-item
5705 org-agenda-current-time-string
5706 "" nil
5707 (format-time-string "%H:%M "))
5708 new)
5709 (put-text-property
5710 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5712 (if (member 'time-up org-agenda-sorting-strategy-selected)
5713 (append new list)
5714 (append list new)))))
5716 (defun org-compile-prefix-format (key)
5717 "Compile the prefix format into a Lisp form that can be evaluated.
5718 The resulting form is returned and stored in the variable
5719 `org-prefix-format-compiled'."
5720 (setq org-prefix-has-time nil org-prefix-has-tag nil
5721 org-prefix-category-length nil
5722 org-prefix-has-effort nil)
5723 (let ((s (cond
5724 ((stringp org-agenda-prefix-format)
5725 org-agenda-prefix-format)
5726 ((assq key org-agenda-prefix-format)
5727 (cdr (assq key org-agenda-prefix-format)))
5728 (t " %-12:c%?-12t% s")))
5729 (start 0)
5730 varform vars var e c f opt)
5731 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5732 s start)
5733 (setq var (or (cdr (assoc (match-string 4 s)
5734 '(("c" . category) ("t" . time) ("s" . extra)
5735 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5736 'eval)
5737 c (or (match-string 3 s) "")
5738 opt (match-beginning 1)
5739 start (1+ (match-beginning 0)))
5740 (if (equal var 'time) (setq org-prefix-has-time t))
5741 (if (equal var 'tag) (setq org-prefix-has-tag t))
5742 (if (equal var 'effort) (setq org-prefix-has-effort t))
5743 (setq f (concat "%" (match-string 2 s) "s"))
5744 (when (equal var 'category)
5745 (setq org-prefix-category-length
5746 (floor (abs (string-to-number (match-string 2 s)))))
5747 (setq org-prefix-category-max-length
5748 (let ((x (match-string 2 s)))
5749 (save-match-data
5750 (if (string-match "\\.[0-9]+" x)
5751 (string-to-number (substring (match-string 0 x) 1)))))))
5752 (if (eq var 'eval)
5753 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5754 (if opt
5755 (setq varform
5756 `(if (equal "" ,var)
5758 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5759 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5760 (setq s (replace-match "%s" t nil s))
5761 (push varform vars))
5762 (setq vars (nreverse vars))
5763 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5765 (defun org-set-sorting-strategy (key)
5766 (if (symbolp (car org-agenda-sorting-strategy))
5767 ;; the old format
5768 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5769 (setq org-agenda-sorting-strategy-selected
5770 (or (cdr (assq key org-agenda-sorting-strategy))
5771 (cdr (assq 'agenda org-agenda-sorting-strategy))
5772 '(time-up category-keep priority-down)))))
5774 (defun org-get-time-of-day (s &optional string mod24)
5775 "Check string S for a time of day.
5776 If found, return it as a military time number between 0 and 2400.
5777 If not found, return nil.
5778 The optional STRING argument forces conversion into a 5 character wide string
5779 HH:MM."
5780 (save-match-data
5781 (when
5782 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5783 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5784 (let* ((h (string-to-number (match-string 1 s)))
5785 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5786 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5787 (am-p (equal ampm "am"))
5788 (h1 (cond ((not ampm) h)
5789 ((= h 12) (if am-p 0 12))
5790 (t (+ h (if am-p 0 12)))))
5791 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5792 (mod h1 24) h1))
5793 (t0 (+ (* 100 h2) m))
5794 (t1 (concat (if (>= h1 24) "+" " ")
5795 (if (and org-agenda-time-leading-zero
5796 (< t0 1000)) "0" "")
5797 (if (< t0 100) "0" "")
5798 (if (< t0 10) "0" "")
5799 (int-to-string t0))))
5800 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5802 (defvar org-agenda-before-sorting-filter-function nil
5803 "Function to be applied to agenda items prior to sorting.
5804 Prior to sorting also means just before they are inserted into the agenda.
5806 To aid sorting, you may revisit the original entries and add more text
5807 properties which will later be used by the sorting functions.
5809 The function should take a string argument, an agenda line.
5810 It has access to the text properties in that line, which contain among
5811 other things, the property `org-hd-marker' that points to the entry
5812 where the line comes from. Note that not all lines going into the agenda
5813 have this property, only most.
5815 The function should return the modified string. It is probably best
5816 to ONLY change text properties.
5818 You can also use this function as a filter, by returning nil for lines
5819 you don't want to have in the agenda at all. For this application, you
5820 could bind the variable in the options section of a custom command.")
5822 (defun org-finalize-agenda-entries (list &optional nosort)
5823 "Sort and concatenate the agenda items."
5824 (setq list (mapcar 'org-agenda-highlight-todo list))
5825 (if nosort
5826 list
5827 (when org-agenda-before-sorting-filter-function
5828 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5829 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5831 (defun org-agenda-highlight-todo (x)
5832 (let ((org-done-keywords org-done-keywords-for-agenda)
5833 (case-fold-search nil)
5835 (if (eq x 'line)
5836 (save-excursion
5837 (beginning-of-line 1)
5838 (setq re (org-get-at-bol 'org-todo-regexp))
5839 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5840 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5841 (add-text-properties (match-beginning 0) (match-end 1)
5842 (list 'face (org-get-todo-face 1)))
5843 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5844 (delete-region (match-beginning 1) (1- (match-end 0)))
5845 (goto-char (match-beginning 1))
5846 (insert (format org-agenda-todo-keyword-format s)))))
5847 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5848 (setq re (get-text-property 0 'org-todo-regexp x))
5849 (when (and re
5850 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5851 x (or pl 0)) pl))
5852 (add-text-properties
5853 (or (match-end 1) (match-end 0)) (match-end 0)
5854 (list 'face (org-get-todo-face (match-string 2 x)))
5856 (when (match-end 1)
5857 (setq x (concat (substring x 0 (match-end 1))
5858 (format org-agenda-todo-keyword-format
5859 (match-string 2 x))
5860 (org-add-props " " (text-properties-at 0 x))
5861 (substring x (match-end 3)))))))
5862 x)))
5864 (defsubst org-cmp-priority (a b)
5865 "Compare the priorities of string A and B."
5866 (let ((pa (or (get-text-property 1 'priority a) 0))
5867 (pb (or (get-text-property 1 'priority b) 0)))
5868 (cond ((> pa pb) +1)
5869 ((< pa pb) -1)
5870 (t nil))))
5872 (defsubst org-cmp-effort (a b)
5873 "Compare the effort values of string A and B."
5874 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5875 (ea (or (get-text-property 1 'effort-minutes a) def))
5876 (eb (or (get-text-property 1 'effort-minutes b) def)))
5877 (cond ((> ea eb) +1)
5878 ((< ea eb) -1)
5879 (t nil))))
5881 (defsubst org-cmp-category (a b)
5882 "Compare the string values of categories of strings A and B."
5883 (let ((ca (or (get-text-property 1 'org-category a) ""))
5884 (cb (or (get-text-property 1 'org-category b) "")))
5885 (cond ((string-lessp ca cb) -1)
5886 ((string-lessp cb ca) +1)
5887 (t nil))))
5889 (defsubst org-cmp-todo-state (a b)
5890 "Compare the todo states of strings A and B."
5891 (let* ((ma (or (get-text-property 1 'org-marker a)
5892 (get-text-property 1 'org-hd-marker a)))
5893 (mb (or (get-text-property 1 'org-marker b)
5894 (get-text-property 1 'org-hd-marker b)))
5895 (fa (and ma (marker-buffer ma)))
5896 (fb (and mb (marker-buffer mb)))
5897 (todo-kwds
5898 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5899 (and fb (with-current-buffer fb org-todo-keywords-1))))
5900 (ta (or (get-text-property 1 'todo-state a) ""))
5901 (tb (or (get-text-property 1 'todo-state b) ""))
5902 (la (- (length (member ta todo-kwds))))
5903 (lb (- (length (member tb todo-kwds))))
5904 (donepa (member ta org-done-keywords-for-agenda))
5905 (donepb (member tb org-done-keywords-for-agenda)))
5906 (cond ((and donepa (not donepb)) -1)
5907 ((and (not donepa) donepb) +1)
5908 ((< la lb) -1)
5909 ((< lb la) +1)
5910 (t nil))))
5912 (defsubst org-cmp-alpha (a b)
5913 "Compare the headlines, alphabetically."
5914 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5915 (plb (text-property-any 0 (length b) 'org-heading t b))
5916 (ta (and pla (substring a pla)))
5917 (tb (and plb (substring b plb))))
5918 (when pla
5919 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5920 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5921 (setq ta (substring ta (match-end 0))))
5922 (setq ta (downcase ta)))
5923 (when plb
5924 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5925 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5926 (setq tb (substring tb (match-end 0))))
5927 (setq tb (downcase tb)))
5928 (cond ((not ta) +1)
5929 ((not tb) -1)
5930 ((string-lessp ta tb) -1)
5931 ((string-lessp tb ta) +1)
5932 (t nil))))
5934 (defsubst org-cmp-tag (a b)
5935 "Compare the string values of the first tags of A and B."
5936 (let ((ta (car (last (get-text-property 1 'tags a))))
5937 (tb (car (last (get-text-property 1 'tags b)))))
5938 (cond ((not ta) +1)
5939 ((not tb) -1)
5940 ((string-lessp ta tb) -1)
5941 ((string-lessp tb ta) +1)
5942 (t nil))))
5944 (defsubst org-cmp-time (a b)
5945 "Compare the time-of-day values of strings A and B."
5946 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5947 (ta (or (get-text-property 1 'time-of-day a) def))
5948 (tb (or (get-text-property 1 'time-of-day b) def)))
5949 (cond ((< ta tb) -1)
5950 ((< tb ta) +1)
5951 (t nil))))
5953 (defsubst org-cmp-habit-p (a b)
5954 "Compare the todo states of strings A and B."
5955 (let ((ha (get-text-property 1 'org-habit-p a))
5956 (hb (get-text-property 1 'org-habit-p b)))
5957 (cond ((and ha (not hb)) -1)
5958 ((and (not ha) hb) +1)
5959 (t nil))))
5961 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5963 (defun org-entries-lessp (a b)
5964 "Predicate for sorting agenda entries."
5965 ;; The following variables will be used when the form is evaluated.
5966 ;; So even though the compiler complains, keep them.
5967 (let* ((ss org-agenda-sorting-strategy-selected)
5968 (time-up (and (org-em 'time-up 'time-down ss)
5969 (org-cmp-time a b)))
5970 (time-down (if time-up (- time-up) nil))
5971 (priority-up (and (org-em 'priority-up 'priority-down ss)
5972 (org-cmp-priority a b)))
5973 (priority-down (if priority-up (- priority-up) nil))
5974 (effort-up (and (org-em 'effort-up 'effort-down ss)
5975 (org-cmp-effort a b)))
5976 (effort-down (if effort-up (- effort-up) nil))
5977 (category-up (and (or (org-em 'category-up 'category-down ss)
5978 (memq 'category-keep ss))
5979 (org-cmp-category a b)))
5980 (category-down (if category-up (- category-up) nil))
5981 (category-keep (if category-up +1 nil))
5982 (tag-up (and (org-em 'tag-up 'tag-down ss)
5983 (org-cmp-tag a b)))
5984 (tag-down (if tag-up (- tag-up) nil))
5985 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5986 (org-cmp-todo-state a b)))
5987 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5988 (habit-up (and (org-em 'habit-up 'habit-down ss)
5989 (org-cmp-habit-p a b)))
5990 (habit-down (if habit-up (- habit-up) nil))
5991 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5992 (org-cmp-alpha a b)))
5993 (alpha-down (if alpha-up (- alpha-up) nil))
5994 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5995 user-defined-up user-defined-down)
5996 (if (and need-user-cmp org-agenda-cmp-user-defined
5997 (functionp org-agenda-cmp-user-defined))
5998 (setq user-defined-up
5999 (funcall org-agenda-cmp-user-defined a b)
6000 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6001 (cdr (assoc
6002 (eval (cons 'or org-agenda-sorting-strategy-selected))
6003 '((-1 . t) (1 . nil) (nil . nil))))))
6005 ;;; Agenda restriction lock
6007 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6008 "Overlay to mark the headline to which agenda commands are restricted.")
6009 (overlay-put org-agenda-restriction-lock-overlay
6010 'face 'org-agenda-restriction-lock)
6011 (overlay-put org-agenda-restriction-lock-overlay
6012 'help-echo "Agendas are currently limited to this subtree.")
6013 (org-detach-overlay org-agenda-restriction-lock-overlay)
6015 (defun org-agenda-set-restriction-lock (&optional type)
6016 "Set restriction lock for agenda, to current subtree or file.
6017 Restriction will be the file if TYPE is `file', or if type is the
6018 universal prefix '(4), or if the cursor is before the first headline
6019 in the file. Otherwise, restriction will be to the current subtree."
6020 (interactive "P")
6021 (and (equal type '(4)) (setq type 'file))
6022 (setq type (cond
6023 (type type)
6024 ((org-at-heading-p) 'subtree)
6025 ((condition-case nil (org-back-to-heading t) (error nil))
6026 'subtree)
6027 (t 'file)))
6028 (if (eq type 'subtree)
6029 (progn
6030 (setq org-agenda-restrict t)
6031 (setq org-agenda-overriding-restriction 'subtree)
6032 (put 'org-agenda-files 'org-restrict
6033 (list (buffer-file-name (buffer-base-buffer))))
6034 (org-back-to-heading t)
6035 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6036 (move-marker org-agenda-restrict-begin (point))
6037 (move-marker org-agenda-restrict-end
6038 (save-excursion (org-end-of-subtree t)))
6039 (message "Locking agenda restriction to subtree"))
6040 (put 'org-agenda-files 'org-restrict
6041 (list (buffer-file-name (buffer-base-buffer))))
6042 (setq org-agenda-restrict nil)
6043 (setq org-agenda-overriding-restriction 'file)
6044 (move-marker org-agenda-restrict-begin nil)
6045 (move-marker org-agenda-restrict-end nil)
6046 (message "Locking agenda restriction to file"))
6047 (setq current-prefix-arg nil)
6048 (org-agenda-maybe-redo))
6050 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6051 "Remove the agenda restriction lock."
6052 (interactive "P")
6053 (org-detach-overlay org-agenda-restriction-lock-overlay)
6054 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6055 (setq org-agenda-overriding-restriction nil)
6056 (setq org-agenda-restrict nil)
6057 (put 'org-agenda-files 'org-restrict nil)
6058 (move-marker org-agenda-restrict-begin nil)
6059 (move-marker org-agenda-restrict-end nil)
6060 (setq current-prefix-arg nil)
6061 (message "Agenda restriction lock removed")
6062 (or noupdate (org-agenda-maybe-redo)))
6064 (defun org-agenda-maybe-redo ()
6065 "If there is any window showing the agenda view, update it."
6066 (let ((w (get-buffer-window org-agenda-buffer-name t))
6067 (w0 (selected-window)))
6068 (when w
6069 (select-window w)
6070 (org-agenda-redo)
6071 (select-window w0)
6072 (if org-agenda-overriding-restriction
6073 (message "Agenda view shifted to new %s restriction"
6074 org-agenda-overriding-restriction)
6075 (message "Agenda restriction lock removed")))))
6077 ;;; Agenda commands
6079 (defun org-agenda-check-type (error &rest types)
6080 "Check if agenda buffer is of allowed type.
6081 If ERROR is non-nil, throw an error, otherwise just return nil."
6082 (if (memq org-agenda-type types)
6084 (if error
6085 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6086 nil)))
6088 (defun org-agenda-quit ()
6089 "Exit agenda by removing the window or the buffer."
6090 (interactive)
6091 (if org-agenda-columns-active
6092 (org-columns-quit)
6093 (let ((buf (current-buffer)))
6094 (if (eq org-agenda-window-setup 'other-frame)
6095 (progn
6096 (kill-buffer buf)
6097 (org-agenda-reset-markers)
6098 (org-columns-remove-overlays)
6099 (setq org-agenda-archives-mode nil)
6100 (delete-frame))
6101 (and (not (eq org-agenda-window-setup 'current-window))
6102 (not (one-window-p))
6103 (delete-window))
6104 (kill-buffer buf)
6105 (org-agenda-reset-markers)
6106 (org-columns-remove-overlays)
6107 (setq org-agenda-archives-mode nil)))
6108 ;; Maybe restore the pre-agenda window configuration.
6109 (and org-agenda-restore-windows-after-quit
6110 (not (eq org-agenda-window-setup 'other-frame))
6111 org-pre-agenda-window-conf
6112 (set-window-configuration org-pre-agenda-window-conf))))
6114 (defun org-agenda-exit ()
6115 "Exit agenda by removing the window or the buffer.
6116 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6117 Org-mode buffers visited directly by the user will not be touched."
6118 (interactive)
6119 (org-release-buffers org-agenda-new-buffers)
6120 (setq org-agenda-new-buffers nil)
6121 (org-agenda-quit))
6123 (defun org-agenda-execute (arg)
6124 "Execute another agenda command, keeping same window.
6125 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6126 in the agenda."
6127 (interactive "P")
6128 (let ((org-agenda-window-setup 'current-window))
6129 (org-agenda arg)))
6131 (defun org-agenda-redo ()
6132 "Rebuild Agenda.
6133 When this is the global TODO list, a prefix argument will be interpreted."
6134 (interactive)
6135 (let* ((org-agenda-keep-modes t)
6136 (filter org-agenda-filter)
6137 (preset (get 'org-agenda-filter :preset-filter))
6138 (org-agenda-filter-while-redo (or filter preset))
6139 (cols org-agenda-columns-active)
6140 (line (org-current-line))
6141 (window-line (- line (org-current-line (window-start))))
6142 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6143 (put 'org-agenda-filter :preset-filter nil)
6144 (and cols (org-columns-quit))
6145 (message "Rebuilding agenda buffer...")
6146 (org-let lprops '(eval org-agenda-redo-command))
6147 (setq org-agenda-undo-list nil
6148 org-agenda-pending-undo-list nil)
6149 (message "Rebuilding agenda buffer...done")
6150 (put 'org-agenda-filter :preset-filter preset)
6151 (and (or filter preset) (org-agenda-filter-apply filter))
6152 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6153 (org-goto-line line)
6154 (recenter window-line)))
6157 (defvar org-global-tags-completion-table nil)
6158 (defvar org-agenda-filter-form nil)
6159 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6160 "Keep only those lines in the agenda buffer that have a specific tag.
6161 The tag is selected with its fast selection letter, as configured.
6162 With prefix argument STRIP, remove all lines that do have the tag.
6163 A lisp caller can specify CHAR. NARROW means that the new tag should be
6164 used to narrow the search - the interactive user can also press `-' or `+'
6165 to switch to narrowing."
6166 (interactive "P")
6167 (let* ((alist org-tag-alist-for-agenda)
6168 (tag-chars (mapconcat
6169 (lambda (x) (if (and (not (symbolp (car x)))
6170 (cdr x))
6171 (char-to-string (cdr x))
6172 ""))
6173 alist ""))
6174 (efforts (org-split-string
6175 (or (cdr (assoc (concat org-effort-property "_ALL")
6176 org-global-properties))
6177 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6178 "")))
6179 (effort-op org-agenda-filter-effort-default-operator)
6180 (effort-prompt "")
6181 (inhibit-read-only t)
6182 (current org-agenda-filter)
6183 maybe-refresh a n tag)
6184 (unless char
6185 (message
6186 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6187 (if narrow "Narrow" "Filter") tag-chars
6188 (if org-agenda-auto-exclude-function "[RET], " ""))
6189 (setq char (read-char)))
6190 (when (member char '(?+ ?-))
6191 ;; Narrowing down
6192 (cond ((equal char ?-) (setq strip t narrow t))
6193 ((equal char ?+) (setq strip nil narrow t)))
6194 (message
6195 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6196 (setq char (read-char)))
6197 (when (member char '(?< ?> ?= ??))
6198 ;; An effort operator
6199 (setq effort-op (char-to-string char))
6200 (setq alist nil) ; to make sure it will be interpreted as effort.
6201 (unless (equal char ??)
6202 (loop for i from 0 to 9 do
6203 (setq effort-prompt
6204 (concat
6205 effort-prompt " ["
6206 (if (= i 9) "0" (int-to-string (1+ i)))
6207 "]" (nth i efforts))))
6208 (message "Effort%s: %s " effort-op effort-prompt)
6209 (setq char (read-char))
6210 (when (or (< char ?0) (> char ?9))
6211 (error "Need 1-9,0 to select effort" ))))
6212 (when (equal char ?\t)
6213 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6214 (org-set-local 'org-global-tags-completion-table
6215 (org-global-tags-completion-table)))
6216 (let ((completion-ignore-case t))
6217 (setq tag (org-icompleting-read
6218 "Tag: " org-global-tags-completion-table))))
6219 (cond
6220 ((equal char ?\r)
6221 (org-agenda-filter-by-tag-show-all)
6222 (when org-agenda-auto-exclude-function
6223 (setq org-agenda-filter '())
6224 (dolist (tag (org-agenda-get-represented-tags))
6225 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6226 (if modifier
6227 (push modifier org-agenda-filter))))
6228 (if (not (null org-agenda-filter))
6229 (org-agenda-filter-apply org-agenda-filter)))
6230 (setq maybe-refresh t))
6231 ((equal char ?/)
6232 (org-agenda-filter-by-tag-show-all)
6233 (when (get 'org-agenda-filter :preset-filter)
6234 (org-agenda-filter-apply org-agenda-filter))
6235 (setq maybe-refresh t))
6236 ((or (equal char ?\ )
6237 (setq a (rassoc char alist))
6238 (and (>= char ?0) (<= char ?9)
6239 (setq n (if (= char ?0) 9 (- char ?0 1))
6240 tag (concat effort-op (nth n efforts))
6241 a (cons tag nil)))
6242 (and (= char ??)
6243 (setq tag "?eff")
6244 a (cons tag nil))
6245 (and tag (setq a (cons tag nil))))
6246 (org-agenda-filter-by-tag-show-all)
6247 (setq tag (car a))
6248 (setq org-agenda-filter
6249 (cons (concat (if strip "-" "+") tag)
6250 (if narrow current nil)))
6251 (org-agenda-filter-apply org-agenda-filter)
6252 (setq maybe-refresh t))
6253 (t (error "Invalid tag selection character %c" char)))
6254 (when (and maybe-refresh
6255 (eq org-agenda-clockreport-mode 'with-filter))
6256 (org-agenda-redo))))
6258 (defun org-agenda-get-represented-tags ()
6259 "Get a list of all tags currently represented in the agenda."
6260 (let (p tags)
6261 (save-excursion
6262 (goto-char (point-min))
6263 (while (setq p (next-single-property-change (point) 'tags))
6264 (goto-char p)
6265 (mapc (lambda (x) (add-to-list 'tags x))
6266 (get-text-property (point) 'tags))))
6267 tags))
6269 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6270 "Refine the current filter. See `org-agenda-filter-by-tag'."
6271 (interactive "P")
6272 (org-agenda-filter-by-tag strip char 'refine))
6274 (defun org-agenda-filter-make-matcher ()
6275 "Create the form that tests a line for the agenda filter."
6276 (let (f f1)
6277 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6278 org-agenda-filter))
6279 (if (member x '("-" "+"))
6280 (setq f1 (if (equal x "-") 'tags '(not tags)))
6281 (if (string-match "[<=>?]" x)
6282 (setq f1 (org-agenda-filter-effort-form x))
6283 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6284 (if (equal (string-to-char x) ?-)
6285 (setq f1 (list 'not f1))))
6286 (push f1 f))
6287 (cons 'and (nreverse f))))
6289 (defun org-agenda-filter-effort-form (e)
6290 "Return the form to compare the effort of the current line with what E says.
6291 E looks like \"+<2:25\"."
6292 (let (op)
6293 (setq e (substring e 1))
6294 (setq op (string-to-char e) e (substring e 1))
6295 (setq op (cond ((equal op ?<) '<=)
6296 ((equal op ?>) '>=)
6297 ((equal op ??) op)
6298 (t '=)))
6299 (list 'org-agenda-compare-effort (list 'quote op)
6300 (org-duration-string-to-minutes e))))
6302 (defun org-agenda-compare-effort (op value)
6303 "Compare the effort of the current line with VALUE, using OP.
6304 If the line does not have an effort defined, return nil."
6305 (let ((eff (org-get-at-bol 'effort-minutes)))
6306 (if (equal op ??)
6307 (not eff)
6308 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6309 value))))
6311 (defun org-agenda-filter-apply (filter)
6312 "Set FILTER as the new agenda filter and apply it."
6313 (let (tags)
6314 (setq org-agenda-filter filter
6315 org-agenda-filter-form (org-agenda-filter-make-matcher))
6316 (org-agenda-set-mode-name)
6317 (save-excursion
6318 (goto-char (point-min))
6319 (while (not (eobp))
6320 (if (org-get-at-bol 'org-marker)
6321 (progn
6322 (setq tags (org-get-at-bol 'tags)) ; used in eval
6323 (if (not (eval org-agenda-filter-form))
6324 (org-agenda-filter-by-tag-hide-line))
6325 (beginning-of-line 2))
6326 (beginning-of-line 2))))
6327 (if (get-char-property (point) 'invisible)
6328 (org-agenda-previous-line))))
6330 (defun org-agenda-filter-by-tag-hide-line ()
6331 (let (ov)
6332 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6333 (point-at-eol)))
6334 (overlay-put ov 'invisible t)
6335 (overlay-put ov 'type 'tags-filter)
6336 (push ov org-agenda-filter-overlays)))
6338 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6339 (setq pos (or pos (point)))
6340 (save-excursion
6341 (dolist (ov (overlays-at pos))
6342 (when (and (overlay-get ov 'invisible)
6343 (eq (overlay-get ov 'type) 'tags-filter))
6344 (goto-char pos)
6345 (if (< (overlay-start ov) (point-at-eol))
6346 (move-overlay ov (point-at-eol)
6347 (overlay-end ov)))))))
6349 (defun org-agenda-filter-by-tag-show-all ()
6350 (mapc 'delete-overlay org-agenda-filter-overlays)
6351 (setq org-agenda-filter-overlays nil)
6352 (setq org-agenda-filter nil)
6353 (setq org-agenda-filter-form nil)
6354 (org-agenda-set-mode-name))
6356 (defun org-agenda-manipulate-query-add ()
6357 "Manipulate the query by adding a search term with positive selection.
6358 Positive selection means the term must be matched for selection of an entry."
6359 (interactive)
6360 (org-agenda-manipulate-query ?\[))
6361 (defun org-agenda-manipulate-query-subtract ()
6362 "Manipulate the query by adding a search term with negative selection.
6363 Negative selection means term must not be matched for selection of an entry."
6364 (interactive)
6365 (org-agenda-manipulate-query ?\]))
6366 (defun org-agenda-manipulate-query-add-re ()
6367 "Manipulate the query by adding a search regexp with positive selection.
6368 Positive selection means the regexp must match for selection of an entry."
6369 (interactive)
6370 (org-agenda-manipulate-query ?\{))
6371 (defun org-agenda-manipulate-query-subtract-re ()
6372 "Manipulate the query by adding a search regexp with negative selection.
6373 Negative selection means regexp must not match for selection of an entry."
6374 (interactive)
6375 (org-agenda-manipulate-query ?\}))
6376 (defun org-agenda-manipulate-query (char)
6377 (cond
6378 ((memq org-agenda-type '(timeline agenda))
6379 (let ((org-agenda-include-inactive-timestamps t))
6380 (org-agenda-redo))
6381 (message "Display now includes inactive timestamps as well"))
6382 ((eq org-agenda-type 'search)
6383 (org-add-to-string
6384 'org-agenda-query-string
6385 (if org-agenda-last-search-view-search-was-boolean
6386 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6387 (?\{ . " +{}") (?\} . " -{}"))))
6388 " "))
6389 (setq org-agenda-redo-command
6390 (list 'org-search-view
6391 org-todo-only
6392 org-agenda-query-string
6393 (+ (length org-agenda-query-string)
6394 (if (member char '(?\{ ?\})) 0 1))))
6395 (set-register org-agenda-query-register org-agenda-query-string)
6396 (org-agenda-redo))
6397 (t (error "Cannot manipulate query for %s-type agenda buffers"
6398 org-agenda-type))))
6400 (defun org-add-to-string (var string)
6401 (set var (concat (symbol-value var) string)))
6403 (defun org-agenda-goto-date (date)
6404 "Jump to DATE in agenda."
6405 (interactive (list (let ((org-read-date-prefer-future
6406 (eval org-agenda-jump-prefer-future)))
6407 (org-read-date))))
6408 (org-agenda-list nil date))
6410 (defun org-agenda-goto-today ()
6411 "Go to today."
6412 (interactive)
6413 (org-agenda-check-type t 'timeline 'agenda)
6414 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6415 (cond
6416 (tdpos (goto-char tdpos))
6417 ((eq org-agenda-type 'agenda)
6418 (let* ((sd (org-agenda-compute-starting-span
6419 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6420 (org-agenda-overriding-arguments org-agenda-last-arguments))
6421 (setf (nth 1 org-agenda-overriding-arguments) sd)
6422 (org-agenda-redo)
6423 (org-agenda-find-same-or-today-or-agenda)))
6424 (t (error "Cannot find today")))))
6426 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6427 (goto-char
6428 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6429 (text-property-any (point-min) (point-max) 'org-today t)
6430 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6431 (point-min))))
6433 (defun org-agenda-later (arg)
6434 "Go forward in time by thee current span.
6435 With prefix ARG, go forward that many times the current span."
6436 (interactive "p")
6437 (org-agenda-check-type t 'agenda)
6438 (let* ((span org-agenda-current-span)
6439 (sd org-starting-day)
6440 (greg (calendar-gregorian-from-absolute sd))
6441 (cnt (org-get-at-bol 'org-day-cnt))
6442 greg2)
6443 (cond
6444 ((eq span 'day)
6445 (setq sd (+ arg sd)))
6446 ((eq span 'week)
6447 (setq sd (+ (* 7 arg) sd)))
6448 ((eq span 'month)
6449 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6450 sd (calendar-absolute-from-gregorian greg2))
6451 (setcar greg2 (1+ (car greg2))))
6452 ((eq span 'year)
6453 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6454 sd (calendar-absolute-from-gregorian greg2))
6455 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6457 (setq sd (+ (* span arg) sd))))
6458 (let ((org-agenda-overriding-arguments
6459 (list (car org-agenda-last-arguments) sd span t)))
6460 (org-agenda-redo)
6461 (org-agenda-find-same-or-today-or-agenda cnt))))
6463 (defun org-agenda-earlier (arg)
6464 "Go backward in time by the current span.
6465 With prefix ARG, go backward that many times the current span."
6466 (interactive "p")
6467 (org-agenda-later (- arg)))
6469 (defun org-agenda-view-mode-dispatch ()
6470 "Call one of the view mode commands."
6471 (interactive)
6472 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6473 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6474 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6475 [E]ntryText")
6476 (let ((a (read-char-exclusive)))
6477 (case a
6478 (?\ (call-interactively 'org-agenda-reset-view))
6479 (?d (call-interactively 'org-agenda-day-view))
6480 (?w (call-interactively 'org-agenda-week-view))
6481 (?m (call-interactively 'org-agenda-month-view))
6482 (?y (call-interactively 'org-agenda-year-view))
6483 (?l (call-interactively 'org-agenda-log-mode))
6484 (?L (org-agenda-log-mode '(4)))
6485 (?c (org-agenda-log-mode 'clockcheck))
6486 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6487 (?a (call-interactively 'org-agenda-archives-mode))
6488 (?A (org-agenda-archives-mode 'files))
6489 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6490 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6491 (?G (call-interactively 'org-agenda-toggle-time-grid))
6492 (?D (call-interactively 'org-agenda-toggle-diary))
6493 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6494 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6495 (org-agenda-check-type t 'timeline 'agenda)
6496 (org-agenda-redo))
6497 (message "Display now includes inactive timestamps as well"))
6498 (?q (message "Abort"))
6499 (otherwise (error "Invalid key" )))))
6501 (defun org-agenda-reset-view ()
6502 "Switch to default view for agenda."
6503 (interactive)
6504 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6505 (defun org-agenda-day-view (&optional day-of-year)
6506 "Switch to daily view for agenda.
6507 With argument DAY-OF-YEAR, switch to that day of the year."
6508 (interactive "P")
6509 (org-agenda-change-time-span 'day day-of-year))
6510 (defun org-agenda-week-view (&optional iso-week)
6511 "Switch to daily view for agenda.
6512 With argument ISO-WEEK, switch to the corresponding ISO week.
6513 If ISO-WEEK has more then 2 digits, only the last two encode the
6514 week. Any digits before this encode a year. So 200712 means
6515 week 12 of year 2007. Years in the range 1938-2037 can also be
6516 written as 2-digit years."
6517 (interactive "P")
6518 (org-agenda-change-time-span 'week iso-week))
6519 (defun org-agenda-month-view (&optional month)
6520 "Switch to monthly view for agenda.
6521 With argument MONTH, switch to that month."
6522 (interactive "P")
6523 (org-agenda-change-time-span 'month month))
6524 (defun org-agenda-year-view (&optional year)
6525 "Switch to yearly view for agenda.
6526 With argument YEAR, switch to that year.
6527 If MONTH has more then 2 digits, only the last two encode the
6528 month. Any digits before this encode a year. So 200712 means
6529 December year 2007. Years in the range 1938-2037 can also be
6530 written as 2-digit years."
6531 (interactive "P")
6532 (when year
6533 (setq year (org-small-year-to-year year)))
6534 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6535 (org-agenda-change-time-span 'year year)
6536 (error "Abort")))
6538 (defun org-agenda-change-time-span (span &optional n)
6539 "Change the agenda view to SPAN.
6540 SPAN may be `day', `week', `month', `year'."
6541 (org-agenda-check-type t 'agenda)
6542 (if (and (not n) (equal org-agenda-current-span span))
6543 (error "Viewing span is already \"%s\"" span))
6544 (let* ((sd (or (org-get-at-bol 'day)
6545 org-starting-day))
6546 (sd (org-agenda-compute-starting-span sd span n))
6547 (org-agenda-overriding-arguments
6548 (or org-agenda-overriding-arguments
6549 (list (car org-agenda-last-arguments) sd span t))))
6550 (org-agenda-redo)
6551 (org-agenda-find-same-or-today-or-agenda))
6552 (org-agenda-set-mode-name)
6553 (message "Switched to %s view" span))
6555 (defun org-agenda-compute-starting-span (sd span &optional n)
6556 "Compute starting date for agenda.
6557 SPAN may be `day', `week', `month', `year'. The return value
6558 is a cons cell with the starting date and the number of days,
6559 so that the date SD will be in that range."
6560 (let* ((greg (calendar-gregorian-from-absolute sd))
6561 (dg (nth 1 greg))
6562 (mg (car greg))
6563 (yg (nth 2 greg)))
6564 (cond
6565 ((eq span 'day)
6566 (when n
6567 (setq sd (+ (calendar-absolute-from-gregorian
6568 (list mg 1 yg))
6569 n -1))))
6570 ((eq span 'week)
6571 (let* ((nt (calendar-day-of-week
6572 (calendar-gregorian-from-absolute sd)))
6573 (d (if org-agenda-start-on-weekday
6574 (- nt org-agenda-start-on-weekday)
6577 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6578 (when n
6579 (require 'cal-iso)
6580 (when (> n 99)
6581 (setq y1 (org-small-year-to-year (/ n 100))
6582 n (mod n 100)))
6583 (setq sd
6584 (calendar-absolute-from-iso
6585 (list n 1
6586 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6587 ((eq span 'month)
6588 (let (y1)
6589 (when (and n (> n 99))
6590 (setq y1 (org-small-year-to-year (/ n 100))
6591 n (mod n 100)))
6592 (setq sd (calendar-absolute-from-gregorian
6593 (list (or n mg) 1 (or y1 yg))))))
6594 ((eq span 'year)
6595 (setq sd (calendar-absolute-from-gregorian
6596 (list 1 1 (or n yg))))))
6597 sd))
6599 (defun org-agenda-next-date-line (&optional arg)
6600 "Jump to the next line indicating a date in agenda buffer."
6601 (interactive "p")
6602 (org-agenda-check-type t 'agenda 'timeline)
6603 (beginning-of-line 1)
6604 ;; This does not work if user makes date format that starts with a blank
6605 (if (looking-at "^\\S-") (forward-char 1))
6606 (if (not (re-search-forward "^\\S-" nil t arg))
6607 (progn
6608 (backward-char 1)
6609 (error "No next date after this line in this buffer")))
6610 (goto-char (match-beginning 0)))
6612 (defun org-agenda-previous-date-line (&optional arg)
6613 "Jump to the previous line indicating a date in agenda buffer."
6614 (interactive "p")
6615 (org-agenda-check-type t 'agenda 'timeline)
6616 (beginning-of-line 1)
6617 (if (not (re-search-backward "^\\S-" nil t arg))
6618 (error "No previous date before this line in this buffer")))
6620 ;; Initialize the highlight
6621 (defvar org-hl (make-overlay 1 1))
6622 (overlay-put org-hl 'face 'highlight)
6624 (defun org-highlight (begin end &optional buffer)
6625 "Highlight a region with overlay."
6626 (move-overlay org-hl begin end (or buffer (current-buffer))))
6628 (defun org-unhighlight ()
6629 "Detach overlay INDEX."
6630 (org-detach-overlay org-hl))
6632 ;; FIXME this is currently not used.
6633 (defun org-highlight-until-next-command (beg end &optional buffer)
6634 "Move the highlight overlay to BEG/END, remove it before the next command."
6635 (org-highlight beg end buffer)
6636 (add-hook 'pre-command-hook 'org-unhighlight-once))
6637 (defun org-unhighlight-once ()
6638 "Remove the highlight from its position, and this function from the hook."
6639 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6640 (org-unhighlight))
6642 (defun org-agenda-follow-mode ()
6643 "Toggle follow mode in an agenda buffer."
6644 (interactive)
6645 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6646 (org-agenda-set-mode-name)
6647 (org-agenda-do-context-action)
6648 (message "Follow mode is %s"
6649 (if org-agenda-follow-mode "on" "off")))
6651 (defun org-agenda-entry-text-mode (&optional arg)
6652 "Toggle entry text mode in an agenda buffer."
6653 (interactive "P")
6654 (setq org-agenda-entry-text-mode (or (integerp arg)
6655 (not org-agenda-entry-text-mode)))
6656 (org-agenda-entry-text-hide)
6657 (and org-agenda-entry-text-mode
6658 (let ((org-agenda-entry-text-maxlines
6659 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6660 (org-agenda-entry-text-show)))
6661 (org-agenda-set-mode-name)
6662 (message "Entry text mode is %s. Maximum number of lines is %d"
6663 (if org-agenda-entry-text-mode "on" "off")
6664 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6666 (defun org-agenda-clockreport-mode (&optional with-filter)
6667 "Toggle clocktable mode in an agenda buffer.
6668 With prefix arg WITH-FILTER, make the clocktable respect the current
6669 agenda filter."
6670 (interactive "P")
6671 (org-agenda-check-type t 'agenda)
6672 (if with-filter
6673 (setq org-agenda-clockreport-mode 'with-filter)
6674 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6675 (org-agenda-set-mode-name)
6676 (org-agenda-redo)
6677 (message "Clocktable mode is %s"
6678 (if org-agenda-clockreport-mode "on" "off")))
6680 (defun org-agenda-log-mode (&optional special)
6681 "Toggle log mode in an agenda buffer.
6682 With argument SPECIAL, show all possible log items, not only the ones
6683 configured in `org-agenda-log-mode-items'.
6684 With a double `C-u' prefix arg, show *only* log items, nothing else."
6685 (interactive "P")
6686 (org-agenda-check-type t 'agenda 'timeline)
6687 (setq org-agenda-show-log
6688 (cond
6689 ((equal special '(16)) 'only)
6690 ((eq special 'clockcheck)
6691 (if (eq org-agenda-show-log 'clockcheck)
6692 nil 'clockcheck))
6693 (special '(closed clock state))
6694 (t (not org-agenda-show-log))))
6695 (org-agenda-set-mode-name)
6696 (org-agenda-redo)
6697 (message "Log mode is %s"
6698 (if org-agenda-show-log "on" "off")))
6700 (defun org-agenda-archives-mode (&optional with-files)
6701 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6702 When called with a prefix argument, include all archive files as well."
6703 (interactive "P")
6704 (setq org-agenda-archives-mode
6705 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6706 (org-agenda-set-mode-name)
6707 (org-agenda-redo)
6708 (message
6709 "%s"
6710 (cond
6711 ((eq org-agenda-archives-mode nil)
6712 "No archives are included")
6713 ((eq org-agenda-archives-mode 'trees)
6714 (format "Trees with :%s: tag are included" org-archive-tag))
6715 ((eq org-agenda-archives-mode t)
6716 (format "Trees with :%s: tag and all active archive files are included"
6717 org-archive-tag)))))
6719 (defun org-agenda-toggle-diary ()
6720 "Toggle diary inclusion in an agenda buffer."
6721 (interactive)
6722 (org-agenda-check-type t 'agenda)
6723 (setq org-agenda-include-diary (not org-agenda-include-diary))
6724 (org-agenda-redo)
6725 (org-agenda-set-mode-name)
6726 (message "Diary inclusion turned %s"
6727 (if org-agenda-include-diary "on" "off")))
6729 (defun org-agenda-toggle-deadlines ()
6730 "Toggle inclusion of entries with a deadline in an agenda buffer."
6731 (interactive)
6732 (org-agenda-check-type t 'agenda)
6733 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6734 (org-agenda-redo)
6735 (org-agenda-set-mode-name)
6736 (message "Deadlines inclusion turned %s"
6737 (if org-agenda-include-deadlines "on" "off")))
6739 (defun org-agenda-toggle-time-grid ()
6740 "Toggle time grid in an agenda buffer."
6741 (interactive)
6742 (org-agenda-check-type t 'agenda)
6743 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6744 (org-agenda-redo)
6745 (org-agenda-set-mode-name)
6746 (message "Time-grid turned %s"
6747 (if org-agenda-use-time-grid "on" "off")))
6749 (defun org-agenda-set-mode-name ()
6750 "Set the mode name to indicate all the small mode settings."
6751 (setq mode-name
6752 (list "Org-Agenda"
6753 (if (get 'org-agenda-files 'org-restrict) " []" "")
6755 '(:eval (org-agenda-span-name org-agenda-current-span))
6756 (if org-agenda-follow-mode " Follow" "")
6757 (if org-agenda-entry-text-mode " ETxt" "")
6758 (if org-agenda-include-diary " Diary" "")
6759 (if org-agenda-include-deadlines " Ddl" "")
6760 (if org-agenda-use-time-grid " Grid" "")
6761 (if (and (boundp 'org-habit-show-habits)
6762 org-habit-show-habits) " Habit" "")
6763 (cond
6764 ((consp org-agenda-show-log) " LogAll")
6765 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6766 (org-agenda-show-log " Log")
6767 (t ""))
6768 ;; show tags used for filtering in a custom face
6769 (if (or org-agenda-filter (get 'org-agenda-filter
6770 :preset-filter))
6771 '(:eval (org-propertize
6772 (concat " {"
6773 (mapconcat
6774 'identity
6775 (append
6776 (get 'org-agenda-filter :preset-filter)
6777 org-agenda-filter)
6779 "}")
6780 'face 'org-agenda-filter-tags
6781 'help-echo "Tags used in filtering"))
6783 (if org-agenda-archives-mode
6784 (if (eq org-agenda-archives-mode t)
6785 " Archives"
6786 (format " :%s:" org-archive-tag))
6788 (if org-agenda-clockreport-mode
6789 (if (eq org-agenda-clockreport-mode 'with-filter)
6790 " Clock{}" " Clock")
6791 "")))
6792 (force-mode-line-update))
6794 (defun org-agenda-post-command-hook ()
6795 (setq org-agenda-type
6796 (or (get-text-property (point) 'org-agenda-type)
6797 (get-text-property (max (point-min) (1- (point)))
6798 'org-agenda-type))))
6800 (defun org-agenda-next-line ()
6801 "Move cursor to the next line, and show if follow mode is active."
6802 (interactive)
6803 (call-interactively 'next-line)
6804 (org-agenda-do-context-action))
6806 (defun org-agenda-previous-line ()
6807 "Move cursor to the previous line, and show if follow-mode is active."
6808 (interactive)
6809 (call-interactively 'previous-line)
6810 (org-agenda-do-context-action))
6812 (defun org-agenda-do-context-action ()
6813 "Show outline path and, maybe, follow mode window."
6814 (let ((m (org-get-at-bol 'org-marker)))
6815 (when (and (markerp m) (marker-buffer m))
6816 (and org-agenda-follow-mode
6817 (if org-agenda-follow-indirect
6818 (org-agenda-tree-to-indirect-buffer)
6819 (org-agenda-show)))
6820 (and org-agenda-show-outline-path
6821 (org-with-point-at m (org-display-outline-path t))))))
6823 (defun org-agenda-show-priority ()
6824 "Show the priority of the current item.
6825 This priority is composed of the main priority given with the [#A] cookies,
6826 and by additional input from the age of a schedules or deadline entry."
6827 (interactive)
6828 (let* ((pri (org-get-at-bol 'priority)))
6829 (message "Priority is %d" (if pri pri -1000))))
6831 (defun org-agenda-show-tags ()
6832 "Show the tags applicable to the current item."
6833 (interactive)
6834 (let* ((tags (org-get-at-bol 'tags)))
6835 (if tags
6836 (message "Tags are :%s:"
6837 (org-no-properties (mapconcat 'identity tags ":")))
6838 (message "No tags associated with this line"))))
6840 (defun org-agenda-goto (&optional highlight)
6841 "Go to the Org-mode file which contains the item at point."
6842 (interactive)
6843 (let* ((marker (or (org-get-at-bol 'org-marker)
6844 (org-agenda-error)))
6845 (buffer (marker-buffer marker))
6846 (pos (marker-position marker)))
6847 (switch-to-buffer-other-window buffer)
6848 (widen)
6849 (push-mark)
6850 (goto-char pos)
6851 (when (eq major-mode 'org-mode)
6852 (org-show-context 'agenda)
6853 (save-excursion
6854 (and (outline-next-heading)
6855 (org-flag-heading nil)))) ; show the next heading
6856 (when (outline-invisible-p)
6857 (show-entry)) ; display invisible text
6858 (recenter (/ (window-height) 2))
6859 (run-hooks 'org-agenda-after-show-hook)
6860 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6862 (defvar org-agenda-after-show-hook nil
6863 "Normal hook run after an item has been shown from the agenda.
6864 Point is in the buffer where the item originated.")
6866 (defun org-agenda-kill ()
6867 "Kill the entry or subtree belonging to the current agenda entry."
6868 (interactive)
6869 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6870 (let* ((marker (or (org-get-at-bol 'org-marker)
6871 (org-agenda-error)))
6872 (buffer (marker-buffer marker))
6873 (pos (marker-position marker))
6874 (type (org-get-at-bol 'type))
6875 dbeg dend (n 0) conf)
6876 (org-with-remote-undo buffer
6877 (with-current-buffer buffer
6878 (save-excursion
6879 (goto-char pos)
6880 (if (and (eq major-mode 'org-mode) (not (member type '("sexp"))))
6881 (setq dbeg (progn (org-back-to-heading t) (point))
6882 dend (org-end-of-subtree t t))
6883 (setq dbeg (point-at-bol)
6884 dend (min (point-max) (1+ (point-at-eol)))))
6885 (goto-char dbeg)
6886 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6887 (setq conf (or (eq t org-agenda-confirm-kill)
6888 (and (numberp org-agenda-confirm-kill)
6889 (> n org-agenda-confirm-kill))))
6890 (and conf
6891 (not (y-or-n-p
6892 (format "Delete entry with %d lines in buffer \"%s\"? "
6893 n (buffer-name buffer))))
6894 (error "Abort"))
6895 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6896 (with-current-buffer buffer (delete-region dbeg dend))
6897 (message "Agenda item and source killed"))))
6899 (defvar org-archive-default-command)
6900 (defun org-agenda-archive-default ()
6901 "Archive the entry or subtree belonging to the current agenda entry."
6902 (interactive)
6903 (require 'org-archive)
6904 (org-agenda-archive-with org-archive-default-command))
6906 (defun org-agenda-archive-default-with-confirmation ()
6907 "Archive the entry or subtree belonging to the current agenda entry."
6908 (interactive)
6909 (require 'org-archive)
6910 (org-agenda-archive-with org-archive-default-command 'confirm))
6912 (defun org-agenda-archive ()
6913 "Archive the entry or subtree belonging to the current agenda entry."
6914 (interactive)
6915 (org-agenda-archive-with 'org-archive-subtree))
6917 (defun org-agenda-archive-to-archive-sibling ()
6918 "Move the entry to the archive sibling."
6919 (interactive)
6920 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6922 (defun org-agenda-archive-with (cmd &optional confirm)
6923 "Move the entry to the archive sibling."
6924 (interactive)
6925 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6926 (let* ((marker (or (org-get-at-bol 'org-marker)
6927 (org-agenda-error)))
6928 (buffer (marker-buffer marker))
6929 (pos (marker-position marker)))
6930 (org-with-remote-undo buffer
6931 (with-current-buffer buffer
6932 (if (eq major-mode 'org-mode)
6933 (if (and confirm
6934 (not (y-or-n-p "Archive this subtree or entry? ")))
6935 (error "Abort")
6936 (save-excursion
6937 (goto-char pos)
6938 (org-remove-subtree-entries-from-agenda)
6939 (org-back-to-heading t)
6940 (funcall cmd)))
6941 (error "Archiving works only in Org-mode files"))))))
6943 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6944 "Remove all lines in the agenda that correspond to a given subtree.
6945 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6946 If this information is not given, the function uses the tree at point."
6947 (let ((buf (or buf (current-buffer))) m p)
6948 (save-excursion
6949 (unless (and beg end)
6950 (org-back-to-heading t)
6951 (setq beg (point))
6952 (org-end-of-subtree t)
6953 (setq end (point)))
6954 (set-buffer (get-buffer org-agenda-buffer-name))
6955 (save-excursion
6956 (goto-char (point-max))
6957 (beginning-of-line 1)
6958 (while (not (bobp))
6959 (when (and (setq m (org-get-at-bol 'org-marker))
6960 (equal buf (marker-buffer m))
6961 (setq p (marker-position m))
6962 (>= p beg)
6963 (< p end))
6964 (let ((inhibit-read-only t))
6965 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6966 (beginning-of-line 0))))))
6968 (defun org-agenda-refile (&optional goto rfloc no-update)
6969 "Refile the item at point."
6970 (interactive "P")
6971 (if (equal goto '(16))
6972 (org-refile-goto-last-stored)
6973 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6974 (org-agenda-error)))
6975 (buffer (marker-buffer marker))
6976 (pos (marker-position marker))
6977 (rfloc (or rfloc
6978 (org-refile-get-location
6979 (if goto "Goto" "Refile to") buffer
6980 org-refile-allow-creating-parent-nodes))))
6981 (with-current-buffer buffer
6982 (save-excursion
6983 (save-restriction
6984 (widen)
6985 (goto-char marker)
6986 (org-remove-subtree-entries-from-agenda)
6987 (org-refile goto buffer rfloc)))))
6988 (unless no-update (org-agenda-redo))))
6990 (defun org-agenda-open-link (&optional arg)
6991 "Follow the link in the current line, if any.
6992 This looks for a link in the displayed line in the agenda. It also looks
6993 at the text of the entry itself."
6994 (interactive "P")
6995 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6996 (org-get-at-bol 'org-marker)))
6997 (buffer (and marker (marker-buffer marker)))
6998 (prefix (buffer-substring
6999 (point-at-bol) (point-at-eol))))
7000 (cond
7001 (buffer
7002 (with-current-buffer buffer
7003 (save-excursion
7004 (save-restriction
7005 (widen)
7006 (goto-char marker)
7007 (org-offer-links-in-entry arg prefix)))))
7008 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7009 (save-excursion
7010 (beginning-of-line 1)
7011 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7012 (org-open-link-from-string (match-string 1)))
7013 (t (error "No link to open here")))))
7015 (defun org-agenda-copy-local-variable (var)
7016 "Get a variable from a referenced buffer and install it here."
7017 (let ((m (org-get-at-bol 'org-marker)))
7018 (when (and m (buffer-live-p (marker-buffer m)))
7019 (org-set-local var (with-current-buffer (marker-buffer m)
7020 (symbol-value var))))))
7022 (defun org-agenda-switch-to (&optional delete-other-windows)
7023 "Go to the Org-mode file which contains the item at point."
7024 (interactive)
7025 (if (and org-return-follows-link
7026 (not (org-get-at-bol 'org-marker))
7027 (org-in-regexp org-bracket-link-regexp))
7028 (org-open-link-from-string (match-string 0))
7029 (let* ((marker (or (org-get-at-bol 'org-marker)
7030 (org-agenda-error)))
7031 (buffer (marker-buffer marker))
7032 (pos (marker-position marker)))
7033 (org-pop-to-buffer-same-window buffer)
7034 (and delete-other-windows (delete-other-windows))
7035 (widen)
7036 (goto-char pos)
7037 (when (eq major-mode 'org-mode)
7038 (org-show-context 'agenda)
7039 (save-excursion
7040 (and (outline-next-heading)
7041 (org-flag-heading nil))) ; show the next heading
7042 (when (outline-invisible-p)
7043 (show-entry)))))) ; display invisible text
7045 (defun org-agenda-goto-mouse (ev)
7046 "Go to the Org-mode file which contains the item at the mouse click."
7047 (interactive "e")
7048 (mouse-set-point ev)
7049 (org-agenda-goto))
7051 (defun org-agenda-show (&optional full-entry)
7052 "Display the Org-mode file which contains the item at point.
7053 With prefix argument FULL-ENTRY, make the entire entry visible
7054 if it was hidden in the outline."
7055 (interactive "P")
7056 (let ((win (selected-window)))
7057 (if full-entry
7058 (let ((org-show-entry-below t))
7059 (org-agenda-goto t))
7060 (org-agenda-goto t))
7061 (select-window win)))
7063 (defvar org-agenda-show-window nil)
7064 (defun org-agenda-show-and-scroll-up ()
7065 "Display the Org-mode file which contains the item at point.
7066 When called repeatedly, scroll the window that is displaying the buffer."
7067 (interactive)
7068 (let ((win (selected-window)))
7069 (if (and (window-live-p org-agenda-show-window)
7070 (eq this-command last-command))
7071 (progn
7072 (select-window org-agenda-show-window)
7073 (ignore-errors (scroll-up)))
7074 (org-agenda-goto t)
7075 (show-subtree)
7076 (setq org-agenda-show-window (selected-window)))
7077 (select-window win)))
7079 (defun org-agenda-show-scroll-down ()
7080 "Scroll down the window showing the agenda."
7081 (interactive)
7082 (let ((win (selected-window)))
7083 (when (window-live-p org-agenda-show-window)
7084 (select-window org-agenda-show-window)
7085 (ignore-errors (scroll-down))
7086 (select-window win))))
7088 (defun org-agenda-show-1 (&optional more)
7089 "Display the Org-mode file which contains the item at point.
7090 The prefix arg selects the amount of information to display:
7092 0 hide the subtree
7093 1 just show the entry according to defaults.
7094 2 show the children view
7095 3 show the subtree view
7096 4 show the entire subtree and any LOGBOOK drawers
7097 5 show the entire subtree and any drawers
7098 With prefix argument FULL-ENTRY, make the entire entry visible
7099 if it was hidden in the outline."
7100 (interactive "p")
7101 (let ((win (selected-window)))
7102 (org-agenda-goto t)
7103 (org-recenter-heading 1)
7104 (cond
7105 ((= more 0)
7106 (hide-subtree)
7107 (save-excursion
7108 (org-back-to-heading)
7109 (run-hook-with-args 'org-cycle-hook 'folded))
7110 (message "Remote: FOLDED"))
7111 ((and (org-called-interactively-p 'any) (= more 1))
7112 (message "Remote: show with default settings"))
7113 ((= more 2)
7114 (show-entry)
7115 (show-children)
7116 (save-excursion
7117 (org-back-to-heading)
7118 (run-hook-with-args 'org-cycle-hook 'children))
7119 (message "Remote: CHILDREN"))
7120 ((= more 3)
7121 (show-subtree)
7122 (save-excursion
7123 (org-back-to-heading)
7124 (run-hook-with-args 'org-cycle-hook 'subtree))
7125 (message "Remote: SUBTREE"))
7126 ((= more 4)
7127 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7128 (org-drawer-regexp
7129 (concat "^[ \t]*:\\("
7130 (mapconcat 'regexp-quote org-drawers "\\|")
7131 "\\):[ \t]*$")))
7132 (show-subtree)
7133 (save-excursion
7134 (org-back-to-heading)
7135 (org-cycle-hide-drawers 'subtree)))
7136 (message "Remote: SUBTREE AND LOGBOOK"))
7137 ((> more 4)
7138 (show-subtree)
7139 (message "Remote: SUBTREE AND ALL DRAWERS")))
7140 (select-window win)))
7142 (defun org-recenter-heading (n)
7143 (save-excursion
7144 (org-back-to-heading)
7145 (recenter n)))
7147 (defvar org-agenda-cycle-counter nil)
7148 (defun org-agenda-cycle-show (&optional n)
7149 "Show the current entry in another window, with default settings.
7150 Default settings are taken from `org-show-hierarchy-above' and siblings.
7151 When use repeatedly in immediate succession, the remote entry will cycle
7152 through visibility
7154 children -> subtree -> folded
7156 When called with a numeric prefix arg, that arg will be passed through to
7157 `org-agenda-show-1'. For the interpretation of that argument, see the
7158 docstring of `org-agenda-show-1'."
7159 (interactive "P")
7160 (if (integerp n)
7161 (setq org-agenda-cycle-counter n)
7162 (if (not (eq last-command this-command))
7163 (setq org-agenda-cycle-counter 1)
7164 (if (equal org-agenda-cycle-counter 0)
7165 (setq org-agenda-cycle-counter 2)
7166 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7167 (if (> org-agenda-cycle-counter 3)
7168 (setq org-agenda-cycle-counter 0)))))
7169 (org-agenda-show-1 org-agenda-cycle-counter))
7171 (defun org-agenda-recenter (arg)
7172 "Display the Org-mode file which contains the item at point and recenter."
7173 (interactive "P")
7174 (let ((win (selected-window)))
7175 (org-agenda-goto t)
7176 (recenter arg)
7177 (select-window win)))
7179 (defun org-agenda-show-mouse (ev)
7180 "Display the Org-mode file which contains the item at the mouse click."
7181 (interactive "e")
7182 (mouse-set-point ev)
7183 (org-agenda-show))
7185 (defun org-agenda-check-no-diary ()
7186 "Check if the entry is a diary link and abort if yes."
7187 (if (org-get-at-bol 'org-agenda-diary-link)
7188 (org-agenda-error)))
7190 (defun org-agenda-error ()
7191 (error "Command not allowed in this line"))
7193 (defun org-agenda-tree-to-indirect-buffer ()
7194 "Show the subtree corresponding to the current entry in an indirect buffer.
7195 This calls the command `org-tree-to-indirect-buffer' from the original
7196 Org-mode buffer.
7197 With numerical prefix arg ARG, go up to this level and then take that tree.
7198 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7199 use the dedicated frame)."
7200 (interactive)
7201 (org-agenda-check-no-diary)
7202 (let* ((marker (or (org-get-at-bol 'org-marker)
7203 (org-agenda-error)))
7204 (buffer (marker-buffer marker))
7205 (pos (marker-position marker)))
7206 (with-current-buffer buffer
7207 (save-excursion
7208 (goto-char pos)
7209 (call-interactively 'org-tree-to-indirect-buffer)))))
7211 (defvar org-last-heading-marker (make-marker)
7212 "Marker pointing to the headline that last changed its TODO state
7213 by a remote command from the agenda.")
7215 (defun org-agenda-todo-nextset ()
7216 "Switch TODO entry to next sequence."
7217 (interactive)
7218 (org-agenda-todo 'nextset))
7220 (defun org-agenda-todo-previousset ()
7221 "Switch TODO entry to previous sequence."
7222 (interactive)
7223 (org-agenda-todo 'previousset))
7225 (defun org-agenda-todo (&optional arg)
7226 "Cycle TODO state of line at point, also in Org-mode file.
7227 This changes the line at point, all other lines in the agenda referring to
7228 the same tree node, and the headline of the tree node in the Org-mode file."
7229 (interactive "P")
7230 (org-agenda-check-no-diary)
7231 (let* ((col (current-column))
7232 (marker (or (org-get-at-bol 'org-marker)
7233 (org-agenda-error)))
7234 (buffer (marker-buffer marker))
7235 (pos (marker-position marker))
7236 (hdmarker (org-get-at-bol 'org-hd-marker))
7237 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7238 (inhibit-read-only t)
7239 org-agenda-headline-snapshot-before-repeat newhead just-one)
7240 (org-with-remote-undo buffer
7241 (with-current-buffer buffer
7242 (widen)
7243 (goto-char pos)
7244 (org-show-context 'agenda)
7245 (save-excursion
7246 (and (outline-next-heading)
7247 (org-flag-heading nil))) ; show the next heading
7248 (let ((current-prefix-arg arg))
7249 (call-interactively 'org-todo))
7250 (and (bolp) (forward-char 1))
7251 (setq newhead (org-get-heading))
7252 (when (and (org-bound-and-true-p
7253 org-agenda-headline-snapshot-before-repeat)
7254 (not (equal org-agenda-headline-snapshot-before-repeat
7255 newhead))
7256 todayp)
7257 (setq newhead org-agenda-headline-snapshot-before-repeat
7258 just-one t))
7259 (save-excursion
7260 (org-back-to-heading)
7261 (move-marker org-last-heading-marker (point))))
7262 (beginning-of-line 1)
7263 (save-excursion
7264 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7265 (org-move-to-column col))))
7267 (defun org-agenda-add-note (&optional arg)
7268 "Add a time-stamped note to the entry at point."
7269 (interactive "P")
7270 (org-agenda-check-no-diary)
7271 (let* ((marker (or (org-get-at-bol 'org-marker)
7272 (org-agenda-error)))
7273 (buffer (marker-buffer marker))
7274 (pos (marker-position marker))
7275 (hdmarker (org-get-at-bol 'org-hd-marker))
7276 (inhibit-read-only t))
7277 (with-current-buffer buffer
7278 (widen)
7279 (goto-char pos)
7280 (org-show-context 'agenda)
7281 (save-excursion
7282 (and (outline-next-heading)
7283 (org-flag-heading nil))) ; show the next heading
7284 (org-add-note))))
7286 (defun org-agenda-change-all-lines (newhead hdmarker
7287 &optional fixface just-this)
7288 "Change all lines in the agenda buffer which match HDMARKER.
7289 The new content of the line will be NEWHEAD (as modified by
7290 `org-agenda-format-item'). HDMARKER is checked with
7291 `equal' against all `org-hd-marker' text properties in the file.
7292 If FIXFACE is non-nil, the face of each item is modified according to
7293 the new TODO state.
7294 If JUST-THIS is non-nil, change just the current line, not all.
7295 If FORCE-TAGS is non nil, the car of it returns the new tags."
7296 (let* ((inhibit-read-only t)
7297 (line (org-current-line))
7298 (thetags (with-current-buffer (marker-buffer hdmarker)
7299 (save-excursion (save-restriction (widen)
7300 (goto-char hdmarker)
7301 (org-get-tags-at)))))
7302 props m pl undone-face done-face finish new dotime cat tags)
7303 (save-excursion
7304 (goto-char (point-max))
7305 (beginning-of-line 1)
7306 (while (not finish)
7307 (setq finish (bobp))
7308 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7309 (or (not just-this) (= (org-current-line) line))
7310 (equal m hdmarker))
7311 (setq props (text-properties-at (point))
7312 dotime (org-get-at-bol 'dotime)
7313 cat (org-get-at-bol 'org-category)
7314 tags thetags
7316 (let ((org-prefix-format-compiled
7317 (or (get-text-property (point) 'format)
7318 org-prefix-format-compiled)))
7319 (with-current-buffer (marker-buffer hdmarker)
7320 (save-excursion
7321 (save-restriction
7322 (widen)
7323 (org-agenda-format-item (org-get-at-bol 'extra)
7324 newhead cat tags dotime)))))
7325 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7326 undone-face (org-get-at-bol 'undone-face)
7327 done-face (org-get-at-bol 'done-face))
7328 (beginning-of-line 1)
7329 (cond
7330 ((equal new "")
7331 (and (looking-at ".*\n?") (replace-match "")))
7332 ((looking-at ".*")
7333 (replace-match new t t)
7334 (beginning-of-line 1)
7335 (add-text-properties (point-at-bol) (point-at-eol) props)
7336 (when fixface
7337 (add-text-properties
7338 (point-at-bol) (point-at-eol)
7339 (list 'face
7340 (if org-last-todo-state-is-todo
7341 undone-face done-face))))
7342 (org-agenda-highlight-todo 'line)
7343 (beginning-of-line 1))
7344 (t (error "Line update did not work"))))
7345 (beginning-of-line 0)))
7346 (org-finalize-agenda)))
7348 (defun org-agenda-align-tags (&optional line)
7349 "Align all tags in agenda items to `org-agenda-tags-column'."
7350 (let ((inhibit-read-only t) l c)
7351 (save-excursion
7352 (goto-char (if line (point-at-bol) (point-min)))
7353 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7354 (if line (point-at-eol) nil) t)
7355 (add-text-properties
7356 (match-beginning 2) (match-end 2)
7357 (list 'face (delq nil (let ((prop (get-text-property
7358 (match-beginning 2) 'face)))
7359 (or (listp prop) (setq prop (list prop)))
7360 (if (memq 'org-tag prop)
7361 prop
7362 (cons 'org-tag prop))))))
7363 (setq l (- (match-end 2) (match-beginning 2))
7364 c (if (< org-agenda-tags-column 0)
7365 (- (abs org-agenda-tags-column) l)
7366 org-agenda-tags-column))
7367 (delete-region (match-beginning 1) (match-end 1))
7368 (goto-char (match-beginning 1))
7369 (insert (org-add-props
7370 (make-string (max 1 (- c (current-column))) ?\ )
7371 (plist-put (copy-sequence (text-properties-at (point)))
7372 'face nil))))
7373 (goto-char (point-min))
7374 (org-font-lock-add-tag-faces (point-max)))))
7376 (defun org-agenda-priority-up ()
7377 "Increase the priority of line at point, also in Org-mode file."
7378 (interactive)
7379 (org-agenda-priority 'up))
7381 (defun org-agenda-priority-down ()
7382 "Decrease the priority of line at point, also in Org-mode file."
7383 (interactive)
7384 (org-agenda-priority 'down))
7386 (defun org-agenda-priority (&optional force-direction)
7387 "Set the priority of line at point, also in Org-mode file.
7388 This changes the line at point, all other lines in the agenda referring to
7389 the same tree node, and the headline of the tree node in the Org-mode file."
7390 (interactive)
7391 (unless org-enable-priority-commands
7392 (error "Priority commands are disabled"))
7393 (org-agenda-check-no-diary)
7394 (let* ((marker (or (org-get-at-bol 'org-marker)
7395 (org-agenda-error)))
7396 (hdmarker (org-get-at-bol 'org-hd-marker))
7397 (buffer (marker-buffer hdmarker))
7398 (pos (marker-position hdmarker))
7399 (inhibit-read-only t)
7400 newhead)
7401 (org-with-remote-undo buffer
7402 (with-current-buffer buffer
7403 (widen)
7404 (goto-char pos)
7405 (org-show-context 'agenda)
7406 (save-excursion
7407 (and (outline-next-heading)
7408 (org-flag-heading nil))) ; show the next heading
7409 (funcall 'org-priority force-direction)
7410 (end-of-line 1)
7411 (setq newhead (org-get-heading)))
7412 (org-agenda-change-all-lines newhead hdmarker)
7413 (beginning-of-line 1))))
7415 ;; FIXME: should fix the tags property of the agenda line.
7416 (defun org-agenda-set-tags (&optional tag onoff)
7417 "Set tags for the current headline."
7418 (interactive)
7419 (org-agenda-check-no-diary)
7420 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7421 (call-interactively 'org-change-tag-in-region)
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 (if tag
7439 (org-toggle-tag tag onoff)
7440 (call-interactively 'org-set-tags))
7441 (end-of-line 1)
7442 (setq newhead (org-get-heading)))
7443 (org-agenda-change-all-lines newhead hdmarker)
7444 (beginning-of-line 1)))))
7446 (defun org-agenda-set-property ()
7447 "Set a property for the current headline."
7448 (interactive)
7449 (org-agenda-check-no-diary)
7450 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7451 (org-agenda-error)))
7452 (buffer (marker-buffer hdmarker))
7453 (pos (marker-position hdmarker))
7454 (inhibit-read-only t)
7455 newhead)
7456 (org-with-remote-undo buffer
7457 (with-current-buffer buffer
7458 (widen)
7459 (goto-char pos)
7460 (save-excursion
7461 (org-show-context 'agenda))
7462 (save-excursion
7463 (and (outline-next-heading)
7464 (org-flag-heading nil))) ; show the next heading
7465 (goto-char pos)
7466 (call-interactively 'org-set-property)))))
7468 (defun org-agenda-set-effort ()
7469 "Set the effort property for the current headline."
7470 (interactive)
7471 (org-agenda-check-no-diary)
7472 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7473 (org-agenda-error)))
7474 (buffer (marker-buffer hdmarker))
7475 (pos (marker-position hdmarker))
7476 (inhibit-read-only t)
7477 newhead)
7478 (org-with-remote-undo buffer
7479 (with-current-buffer buffer
7480 (widen)
7481 (goto-char pos)
7482 (save-excursion
7483 (org-show-context 'agenda))
7484 (save-excursion
7485 (and (outline-next-heading)
7486 (org-flag-heading nil))) ; show the next heading
7487 (goto-char pos)
7488 (call-interactively 'org-set-effort)
7489 (end-of-line 1)
7490 (setq newhead (org-get-heading)))
7491 (org-agenda-change-all-lines newhead hdmarker))))
7493 (defun org-agenda-toggle-archive-tag ()
7494 "Toggle the archive tag for the current entry."
7495 (interactive)
7496 (org-agenda-check-no-diary)
7497 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7498 (org-agenda-error)))
7499 (buffer (marker-buffer hdmarker))
7500 (pos (marker-position hdmarker))
7501 (inhibit-read-only t)
7502 newhead)
7503 (org-with-remote-undo buffer
7504 (with-current-buffer buffer
7505 (widen)
7506 (goto-char pos)
7507 (org-show-context 'agenda)
7508 (save-excursion
7509 (and (outline-next-heading)
7510 (org-flag-heading nil))) ; show the next heading
7511 (call-interactively 'org-toggle-archive-tag)
7512 (end-of-line 1)
7513 (setq newhead (org-get-heading)))
7514 (org-agenda-change-all-lines newhead hdmarker)
7515 (beginning-of-line 1))))
7517 (defun org-agenda-do-date-later (arg)
7518 (interactive "P")
7519 (cond
7520 ((or (equal arg '(16))
7521 (memq last-command
7522 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7523 (setq this-command 'org-agenda-date-later-minutes)
7524 (org-agenda-date-later-minutes 1))
7525 ((or (equal arg '(4))
7526 (memq last-command
7527 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7528 (setq this-command 'org-agenda-date-later-hours)
7529 (org-agenda-date-later-hours 1))
7531 (org-agenda-date-later (prefix-numeric-value arg)))))
7533 (defun org-agenda-do-date-earlier (arg)
7534 (interactive "P")
7535 (cond
7536 ((or (equal arg '(16))
7537 (memq last-command
7538 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7539 (setq this-command 'org-agenda-date-earlier-minutes)
7540 (org-agenda-date-earlier-minutes 1))
7541 ((or (equal arg '(4))
7542 (memq last-command
7543 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7544 (setq this-command 'org-agenda-date-earlier-hours)
7545 (org-agenda-date-earlier-hours 1))
7547 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7549 (defun org-agenda-date-later (arg &optional what)
7550 "Change the date of this item to ARG day(s) later."
7551 (interactive "p")
7552 (org-agenda-check-type t 'agenda 'timeline)
7553 (org-agenda-check-no-diary)
7554 (let* ((marker (or (org-get-at-bol 'org-marker)
7555 (org-agenda-error)))
7556 (buffer (marker-buffer marker))
7557 (pos (marker-position marker))
7558 cdate today)
7559 (org-with-remote-undo buffer
7560 (with-current-buffer buffer
7561 (widen)
7562 (goto-char pos)
7563 (if (not (org-at-timestamp-p))
7564 (error "Cannot find time stamp"))
7565 (when (and org-agenda-move-date-from-past-immediately-to-today
7566 (equal arg 1)
7567 (or (not what) (eq what 'day))
7568 (not (save-match-data (org-at-date-range-p))))
7569 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
7570 cdate (calendar-absolute-from-gregorian
7571 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
7572 today (org-today))
7573 (if (> today cdate)
7574 ;; immediately shift to today
7575 (setq arg (- today cdate))))
7576 (org-timestamp-change arg (or what 'day))
7577 (when (and (org-at-date-range-p)
7578 (re-search-backward org-tr-regexp-both (point-at-bol)))
7579 (let ((end org-last-changed-timestamp))
7580 (org-timestamp-change arg (or what 'day))
7581 (setq org-last-changed-timestamp
7582 (concat org-last-changed-timestamp "--" end)))))
7583 (org-agenda-show-new-time marker org-last-changed-timestamp))
7584 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7586 (defun org-agenda-date-earlier (arg &optional what)
7587 "Change the date of this item to ARG day(s) earlier."
7588 (interactive "p")
7589 (org-agenda-date-later (- arg) what))
7591 (defun org-agenda-date-later-minutes (arg)
7592 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7593 (interactive "p")
7594 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7595 (org-agenda-date-later arg 'minute))
7597 (defun org-agenda-date-earlier-minutes (arg)
7598 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7599 (interactive "p")
7600 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7601 (org-agenda-date-earlier arg 'minute))
7603 (defun org-agenda-date-later-hours (arg)
7604 "Change the time of this item, in hour steps."
7605 (interactive "p")
7606 (org-agenda-date-later arg 'hour))
7608 (defun org-agenda-date-earlier-hours (arg)
7609 "Change the time of this item, in hour steps."
7610 (interactive "p")
7611 (org-agenda-date-earlier arg 'hour))
7613 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7614 "Show new date stamp via text properties."
7615 ;; We use text properties to make this undoable
7616 (let ((inhibit-read-only t)
7617 (buffer-invisibility-spec))
7618 (setq stamp (concat " " prefix " => " stamp))
7619 (save-excursion
7620 (goto-char (point-max))
7621 (while (not (bobp))
7622 (when (equal marker (org-get-at-bol 'org-marker))
7623 (org-move-to-column (- (window-width) (length stamp)) t)
7624 (org-agenda-fix-tags-filter-overlays-at (point))
7625 (if (featurep 'xemacs)
7626 ;; Use `duplicable' property to trigger undo recording
7627 (let ((ex (make-extent nil nil))
7628 (gl (make-glyph stamp)))
7629 (set-glyph-face gl 'secondary-selection)
7630 (set-extent-properties
7631 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7632 (insert-extent ex (1- (point)) (point-at-eol)))
7633 (add-text-properties
7634 (1- (point)) (point-at-eol)
7635 (list 'display (org-add-props stamp nil
7636 'face 'secondary-selection))))
7637 (beginning-of-line 1))
7638 (beginning-of-line 0)))))
7640 (defun org-agenda-date-prompt (arg)
7641 "Change the date of this item. Date is prompted for, with default today.
7642 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7643 be used to request time specification in the time stamp."
7644 (interactive "P")
7645 (org-agenda-check-type t 'agenda 'timeline)
7646 (org-agenda-check-no-diary)
7647 (let* ((marker (or (org-get-at-bol 'org-marker)
7648 (org-agenda-error)))
7649 (buffer (marker-buffer marker))
7650 (pos (marker-position marker)))
7651 (org-with-remote-undo buffer
7652 (with-current-buffer buffer
7653 (widen)
7654 (goto-char pos)
7655 (if (not (org-at-timestamp-p t))
7656 (error "Cannot find time stamp"))
7657 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7658 (org-agenda-show-new-time marker org-last-changed-timestamp))
7659 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7661 (defun org-agenda-schedule (arg &optional time)
7662 "Schedule the item at point.
7663 ARG is passed through to `org-schedule'."
7664 (interactive "P")
7665 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7666 (org-agenda-check-no-diary)
7667 (let* ((marker (or (org-get-at-bol 'org-marker)
7668 (org-agenda-error)))
7669 (type (marker-insertion-type marker))
7670 (buffer (marker-buffer marker))
7671 (pos (marker-position marker))
7672 (org-insert-labeled-timestamps-at-point nil)
7674 (set-marker-insertion-type marker t)
7675 (org-with-remote-undo buffer
7676 (with-current-buffer buffer
7677 (widen)
7678 (goto-char pos)
7679 (setq ts (org-schedule arg time)))
7680 (org-agenda-show-new-time marker ts "S"))
7681 (message "Item scheduled for %s" ts)))
7683 (defun org-agenda-deadline (arg &optional time)
7684 "Schedule the item at point.
7685 ARG is passed through to `org-deadline'."
7686 (interactive "P")
7687 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7688 (org-agenda-check-no-diary)
7689 (let* ((marker (or (org-get-at-bol 'org-marker)
7690 (org-agenda-error)))
7691 (buffer (marker-buffer marker))
7692 (pos (marker-position marker))
7693 (org-insert-labeled-timestamps-at-point nil)
7695 (org-with-remote-undo buffer
7696 (with-current-buffer buffer
7697 (widen)
7698 (goto-char pos)
7699 (setq ts (org-deadline arg time)))
7700 (org-agenda-show-new-time marker ts "D"))
7701 (message "Deadline for this item set to %s" ts)))
7703 (defun org-agenda-action ()
7704 "Select entry for agenda action, or execute an agenda action.
7705 This command prompts for another letter. Valid inputs are:
7707 m Mark the entry at point for an agenda action
7708 s Schedule the marked entry to the date at the cursor
7709 d Set the deadline of the marked entry to the date at the cursor
7710 r Call `org-remember' with cursor date as the default date
7711 c Call `org-capture' with cursor date as the default date
7712 SPC Show marked entry in other window
7713 TAB Visit marked entry in other window
7715 The cursor may be at a date in the calendar, or in the Org agenda."
7716 (interactive)
7717 (let (ans)
7718 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7719 (setq ans (read-char-exclusive))
7720 (cond
7721 ((equal ans ?m)
7722 ;; Mark this entry
7723 (if (eq major-mode 'org-agenda-mode)
7724 (let ((m (or (org-get-at-bol 'org-hd-marker)
7725 (org-get-at-bol 'org-marker))))
7726 (if m
7727 (progn
7728 (move-marker org-agenda-action-marker
7729 (marker-position m) (marker-buffer m))
7730 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7731 (error "Don't know which entry to mark")))
7732 (error "This command works only in the agenda")))
7733 ((equal ans ?s)
7734 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7735 ((equal ans ?d)
7736 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7737 ((equal ans ?r)
7738 (org-agenda-do-action '(org-remember) t))
7739 ((equal ans ?c)
7740 (org-agenda-do-action '(org-capture) t))
7741 ((equal ans ?\ )
7742 (let ((cw (selected-window)))
7743 (org-switch-to-buffer-other-window
7744 (marker-buffer org-agenda-action-marker))
7745 (goto-char org-agenda-action-marker)
7746 (org-show-context 'agenda)
7747 (select-window cw)))
7748 ((equal ans ?\C-i)
7749 (org-switch-to-buffer-other-window
7750 (marker-buffer org-agenda-action-marker))
7751 (goto-char org-agenda-action-marker)
7752 (org-show-context 'agenda))
7753 (t (error "Invalid agenda action %c" ans)))))
7755 (defun org-agenda-do-action (form &optional current-buffer)
7756 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7757 (let ((org-overriding-default-time (org-get-cursor-date)))
7758 (if current-buffer
7759 (eval form)
7760 (if (not (marker-buffer org-agenda-action-marker))
7761 (error "No entry has been selected for agenda action")
7762 (with-current-buffer (marker-buffer org-agenda-action-marker)
7763 (save-excursion
7764 (save-restriction
7765 (widen)
7766 (goto-char org-agenda-action-marker)
7767 (eval form))))))))
7769 (defun org-agenda-clock-in (&optional arg)
7770 "Start the clock on the currently selected item."
7771 (interactive "P")
7772 (org-agenda-check-no-diary)
7773 (if (equal arg '(4))
7774 (org-clock-in arg)
7775 (let* ((marker (or (org-get-at-bol 'org-marker)
7776 (org-agenda-error)))
7777 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7778 marker))
7779 (pos (marker-position marker))
7780 newhead)
7781 (org-with-remote-undo (marker-buffer marker)
7782 (with-current-buffer (marker-buffer marker)
7783 (widen)
7784 (goto-char pos)
7785 (org-show-context 'agenda)
7786 (org-show-entry)
7787 (org-cycle-hide-drawers 'children)
7788 (org-clock-in arg)
7789 (setq newhead (org-get-heading)))
7790 (org-agenda-change-all-lines newhead hdmarker)))))
7792 (defun org-agenda-clock-out ()
7793 "Stop the currently running clock."
7794 (interactive)
7795 (unless (marker-buffer org-clock-marker)
7796 (error "No running clock"))
7797 (let ((marker (make-marker)) newhead)
7798 (org-with-remote-undo (marker-buffer org-clock-marker)
7799 (with-current-buffer (marker-buffer org-clock-marker)
7800 (save-excursion
7801 (save-restriction
7802 (widen)
7803 (goto-char org-clock-marker)
7804 (org-back-to-heading t)
7805 (move-marker marker (point))
7806 (org-clock-out)
7807 (setq newhead (org-get-heading))))))
7808 (org-agenda-change-all-lines newhead marker)
7809 (move-marker marker nil)))
7811 (defun org-agenda-clock-cancel (&optional arg)
7812 "Cancel the currently running clock."
7813 (interactive "P")
7814 (unless (marker-buffer org-clock-marker)
7815 (error "No running clock"))
7816 (org-with-remote-undo (marker-buffer org-clock-marker)
7817 (org-clock-cancel)))
7819 (defun org-agenda-clock-goto ()
7820 "Jump to the currently clocked in task within the agenda.
7821 If the currently clocked in task is not listed in the agenda
7822 buffer, display it in another window."
7823 (interactive)
7824 (let (pos)
7825 (mapc (lambda (o)
7826 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7827 (setq pos (overlay-start o))))
7828 (overlays-in (point-min) (point-max)))
7829 (cond (pos (goto-char pos))
7830 ;; If the currently clocked entry is not in the agenda
7831 ;; buffer, we visit it in another window:
7832 (org-clock-current-task
7833 (org-switch-to-buffer-other-window (org-clock-goto)))
7834 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7836 (defun org-agenda-diary-entry-in-org-file ()
7837 "Make a diary entry in the file `org-agenda-diary-file'."
7838 (let (d1 d2 char (text "") dp1 dp2)
7839 (if (equal (buffer-name) "*Calendar*")
7840 (setq d1 (calendar-cursor-to-date t)
7841 d2 (car calendar-mark-ring))
7842 (setq dp1 (get-text-property (point-at-bol) 'day))
7843 (unless dp1 (error "No date defined in current line"))
7844 (setq d1 (calendar-gregorian-from-absolute dp1)
7845 d2 (and (ignore-errors (mark))
7846 (save-excursion
7847 (goto-char (mark))
7848 (setq dp2 (get-text-property (point-at-bol) 'day)))
7849 (calendar-gregorian-from-absolute dp2))))
7850 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7851 (setq char (read-char-exclusive))
7852 (cond
7853 ((equal char ?d)
7854 (setq text (read-string "Day entry: "))
7855 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7856 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7857 ((equal char ?a)
7858 (setq d1 (list (car d1) (nth 1 d1)
7859 (read-number (format "Reference year [%d]: " (nth 2 d1))
7860 (nth 2 d1))))
7861 (setq text (read-string "Anniversary (use %d to show years): "))
7862 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7863 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7864 ((equal char ?b)
7865 (setq text (read-string "Block entry: "))
7866 (unless (and d1 d2 (not (equal d1 d2)))
7867 (error "No block of days selected"))
7868 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7869 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7870 ((equal char ?j)
7871 (org-switch-to-buffer-other-window
7872 (find-file-noselect org-agenda-diary-file))
7873 (require 'org-datetree)
7874 (org-datetree-find-date-create d1)
7875 (org-reveal t))
7876 (t (error "Invalid selection character `%c'" char)))))
7878 (defcustom org-agenda-insert-diary-strategy 'date-tree
7879 "Where in `org-agenda-diary-file' should new entries be added?
7880 Valid values:
7882 date-tree in the date tree, as child of the date
7883 top-level as top-level entries at the end of the file."
7884 :group 'org-agenda
7885 :type '(choice
7886 (const :tag "in a date tree" date-tree)
7887 (const :tag "as top level at end of file" top-level)))
7889 (defcustom org-agenda-insert-diary-extract-time nil
7890 "Non-nil means extract any time specification from the diary entry."
7891 :group 'org-agenda
7892 :type 'boolean)
7894 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7895 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7896 If TEXT is not empty, it will become the headline of the new entry, and
7897 the resulting entry will not be shown. When TEXT is empty, switch to
7898 `org-agenda-diary-file' and let the user finish the entry there."
7899 (let ((cw (current-window-configuration)))
7900 (org-switch-to-buffer-other-window
7901 (find-file-noselect org-agenda-diary-file))
7902 (widen)
7903 (goto-char (point-min))
7904 (cond
7905 ((eq type 'anniversary)
7906 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7907 (progn
7908 (or (org-on-heading-p t)
7909 (progn
7910 (outline-next-heading)
7911 (insert "* Anniversaries\n\n")
7912 (beginning-of-line -1)))))
7913 (outline-next-heading)
7914 (org-back-over-empty-lines)
7915 (backward-char 1)
7916 (insert "\n")
7917 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7918 (nth 2 d1) (car d1) (nth 1 d1) text)))
7919 ((eq type 'day)
7920 (let ((org-prefix-has-time t)
7921 (org-agenda-time-leading-zero t)
7922 fmt time time2)
7923 (if org-agenda-insert-diary-extract-time
7924 ;; Use org-agenda-format-item to parse text for a time-range and
7925 ;; remove it. FIXME: This is a hack, we should refactor
7926 ;; that function to make time extraction available separately
7927 (setq fmt (org-agenda-format-item nil text nil nil t)
7928 time (get-text-property 0 'time fmt)
7929 time2 (if (> (length time) 0)
7930 ;; split-string removes trailing ...... if
7931 ;; no end time given. First space
7932 ;; separates time from date.
7933 (concat " " (car (split-string time "\\.")))
7934 nil)
7935 text (get-text-property 0 'txt fmt)))
7936 (if (eq org-agenda-insert-diary-strategy 'top-level)
7937 (org-agenda-insert-diary-as-top-level text)
7938 (require 'org-datetree)
7939 (org-datetree-find-date-create d1)
7940 (org-agenda-insert-diary-make-new-entry text))
7941 (org-insert-time-stamp (org-time-from-absolute
7942 (calendar-absolute-from-gregorian d1))
7943 nil nil nil nil time2))
7944 (end-of-line 0))
7945 ((eq type 'block)
7946 (if (> (calendar-absolute-from-gregorian d1)
7947 (calendar-absolute-from-gregorian d2))
7948 (setq d1 (prog1 d2 (setq d2 d1))))
7949 (if (eq org-agenda-insert-diary-strategy 'top-level)
7950 (org-agenda-insert-diary-as-top-level text)
7951 (require 'org-datetree)
7952 (org-datetree-find-date-create d1)
7953 (org-agenda-insert-diary-make-new-entry text))
7954 (org-insert-time-stamp (org-time-from-absolute
7955 (calendar-absolute-from-gregorian d1)))
7956 (insert "--")
7957 (org-insert-time-stamp (org-time-from-absolute
7958 (calendar-absolute-from-gregorian d2)))
7959 (end-of-line 0)))
7960 (if (string-match "\\S-" text)
7961 (progn
7962 (set-window-configuration cw)
7963 (message "%s entry added to %s"
7964 (capitalize (symbol-name type))
7965 (abbreviate-file-name org-agenda-diary-file)))
7966 (org-reveal t)
7967 (message "Please finish entry here"))))
7969 (defun org-agenda-insert-diary-as-top-level (text)
7970 "Make new entry as a top-level entry at the end of the file.
7971 Add TEXT as headline, and position the cursor in the second line so that
7972 a timestamp can be added there."
7973 (widen)
7974 (goto-char (point-max))
7975 (or (bolp) (insert "\n"))
7976 (insert "* " text "\n")
7977 (if org-adapt-indentation (org-indent-to-column 2)))
7979 (defun org-agenda-insert-diary-make-new-entry (text)
7980 "Make new entry as last child of current entry.
7981 Add TEXT as headline, and position the cursor in the second line so that
7982 a timestamp can be added there."
7983 (let ((org-show-following-heading t)
7984 (org-show-siblings t)
7985 (org-show-hierarchy-above t)
7986 (org-show-entry-below t)
7987 col)
7988 (outline-next-heading)
7989 (org-back-over-empty-lines)
7990 (or (looking-at "[ \t]*$")
7991 (progn (insert "\n") (backward-char 1)))
7992 (org-insert-heading nil t)
7993 (org-do-demote)
7994 (setq col (current-column))
7995 (insert text "\n")
7996 (if org-adapt-indentation (org-indent-to-column col))
7997 (let ((org-show-following-heading t)
7998 (org-show-siblings t)
7999 (org-show-hierarchy-above t)
8000 (org-show-entry-below t))
8001 (org-show-context))))
8003 (defun org-agenda-diary-entry ()
8004 "Make a diary entry, like the `i' command from the calendar.
8005 All the standard commands work: block, weekly etc.
8006 When `org-agenda-diary-file' points to a file,
8007 `org-agenda-diary-entry-in-org-file' is called instead to create
8008 entries in that Org-mode file."
8009 (interactive)
8010 (org-agenda-check-type t 'agenda 'timeline)
8011 (if (not (eq org-agenda-diary-file 'diary-file))
8012 (org-agenda-diary-entry-in-org-file)
8013 (require 'diary-lib)
8014 (let* ((char (progn
8015 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8016 (read-char-exclusive)))
8017 (cmd (cdr (assoc char
8018 '((?d . insert-diary-entry)
8019 (?w . insert-weekly-diary-entry)
8020 (?m . insert-monthly-diary-entry)
8021 (?y . insert-yearly-diary-entry)
8022 (?a . insert-anniversary-diary-entry)
8023 (?b . insert-block-diary-entry)
8024 (?c . insert-cyclic-diary-entry)))))
8025 (oldf (symbol-function 'calendar-cursor-to-date))
8026 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8027 (point (point))
8028 (mark (or (mark t) (point))))
8029 (unless cmd
8030 (error "No command associated with <%c>" char))
8031 (unless (and (get-text-property point 'day)
8032 (or (not (equal ?b char))
8033 (get-text-property mark 'day)))
8034 (error "Don't know which date to use for diary entry"))
8035 ;; We implement this by hacking the `calendar-cursor-to-date' function
8036 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8037 (let ((calendar-mark-ring
8038 (list (calendar-gregorian-from-absolute
8039 (or (get-text-property mark 'day)
8040 (get-text-property point 'day))))))
8041 (unwind-protect
8042 (progn
8043 (fset 'calendar-cursor-to-date
8044 (lambda (&optional error dummy)
8045 (calendar-gregorian-from-absolute
8046 (get-text-property point 'day))))
8047 (call-interactively cmd))
8048 (fset 'calendar-cursor-to-date oldf))))))
8050 (defun org-agenda-execute-calendar-command (cmd)
8051 "Execute a calendar command from the agenda, with the date associated to
8052 the cursor position."
8053 (org-agenda-check-type t 'agenda 'timeline)
8054 (require 'diary-lib)
8055 (unless (get-text-property (point) 'day)
8056 (error "Don't know which date to use for calendar command"))
8057 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8058 (point (point))
8059 (date (calendar-gregorian-from-absolute
8060 (get-text-property point 'day)))
8061 ;; the following 2 vars are needed in the calendar
8062 (displayed-month (car date))
8063 (displayed-year (nth 2 date)))
8064 (unwind-protect
8065 (progn
8066 (fset 'calendar-cursor-to-date
8067 (lambda (&optional error dummy)
8068 (calendar-gregorian-from-absolute
8069 (get-text-property point 'day))))
8070 (call-interactively cmd))
8071 (fset 'calendar-cursor-to-date oldf))))
8073 (defun org-agenda-phases-of-moon ()
8074 "Display the phases of the moon for the 3 months around the cursor date."
8075 (interactive)
8076 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8078 (defun org-agenda-holidays ()
8079 "Display the holidays for the 3 months around the cursor date."
8080 (interactive)
8081 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8083 (defvar calendar-longitude)
8084 (defvar calendar-latitude)
8085 (defvar calendar-location-name)
8087 (defun org-agenda-sunrise-sunset (arg)
8088 "Display sunrise and sunset for the cursor date.
8089 Latitude and longitude can be specified with the variables
8090 `calendar-latitude' and `calendar-longitude'. When called with prefix
8091 argument, latitude and longitude will be prompted for."
8092 (interactive "P")
8093 (require 'solar)
8094 (let ((calendar-longitude (if arg nil calendar-longitude))
8095 (calendar-latitude (if arg nil calendar-latitude))
8096 (calendar-location-name
8097 (if arg "the given coordinates" calendar-location-name)))
8098 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8100 (defun org-agenda-goto-calendar ()
8101 "Open the Emacs calendar with the date at the cursor."
8102 (interactive)
8103 (org-agenda-check-type t 'agenda 'timeline)
8104 (let* ((day (or (get-text-property (point) 'day)
8105 (error "Don't know which date to open in calendar")))
8106 (date (calendar-gregorian-from-absolute day))
8107 (calendar-move-hook nil)
8108 (calendar-view-holidays-initially-flag nil)
8109 (calendar-view-diary-initially-flag nil))
8110 (calendar)
8111 (calendar-goto-date date)))
8113 ;;;###autoload
8114 (defun org-calendar-goto-agenda ()
8115 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8116 This is a command that has to be installed in `calendar-mode-map'."
8117 (interactive)
8118 (org-agenda-list nil (calendar-absolute-from-gregorian
8119 (calendar-cursor-to-date))
8120 nil))
8122 (defun org-agenda-convert-date ()
8123 (interactive)
8124 (org-agenda-check-type t 'agenda 'timeline)
8125 (let ((day (get-text-property (point) 'day))
8126 date s)
8127 (unless day
8128 (error "Don't know which date to convert"))
8129 (setq date (calendar-gregorian-from-absolute day))
8130 (setq s (concat
8131 "Gregorian: " (calendar-date-string date) "\n"
8132 "ISO: " (calendar-iso-date-string date) "\n"
8133 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8134 "Julian: " (calendar-julian-date-string date) "\n"
8135 "Astron. JD: " (calendar-astro-date-string date)
8136 " (Julian date number at noon UTC)\n"
8137 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8138 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8139 "French: " (calendar-french-date-string date) "\n"
8140 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8141 "Mayan: " (calendar-mayan-date-string date) "\n"
8142 "Coptic: " (calendar-coptic-date-string date) "\n"
8143 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8144 "Persian: " (calendar-persian-date-string date) "\n"
8145 "Chinese: " (calendar-chinese-date-string date) "\n"))
8146 (with-output-to-temp-buffer "*Dates*"
8147 (princ s))
8148 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8150 ;;; Bulk commands
8152 (defvar org-agenda-bulk-marked-entries nil
8153 "List of markers that refer to marked entries in the agenda.")
8155 (defun org-agenda-bulk-marked-p ()
8156 (eq (get-char-property (point-at-bol) 'type)
8157 'org-marked-entry-overlay))
8159 (defun org-agenda-bulk-mark (&optional arg)
8160 "Mark the entry at point for future bulk action."
8161 (interactive "p")
8162 (dotimes (i (max arg 1))
8163 (unless (org-get-at-bol 'org-agenda-diary-link)
8164 (let* ((m (org-get-at-bol 'org-hd-marker))
8166 (unless (org-agenda-bulk-marked-p)
8167 (unless m (error "Nothing to mark at point"))
8168 (push m org-agenda-bulk-marked-entries)
8169 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8170 (org-overlay-display ov "> "
8171 (org-get-todo-face "TODO")
8172 'evaporate)
8173 (overlay-put ov 'type 'org-marked-entry-overlay))
8174 (beginning-of-line 2)
8175 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8176 (beginning-of-line 2))
8177 (message "%d entries marked for bulk action"
8178 (length org-agenda-bulk-marked-entries))))))
8180 (defun org-agenda-bulk-mark-regexp (regexp)
8181 "Mark entries match REGEXP."
8182 (interactive "sMark entries matching regexp: ")
8183 (let (entries-marked)
8184 (save-excursion
8185 (goto-char (point-min))
8186 (goto-char (next-single-property-change (point) 'txt))
8187 (while (re-search-forward regexp nil t)
8188 (when (string-match regexp (get-text-property (point) 'txt))
8189 (setq entries-marked (+ entries-marked 1))
8190 (call-interactively 'org-agenda-bulk-mark))))
8191 (if (not entries-marked)
8192 (message "No entry matching this regexp."))))
8194 (defun org-agenda-bulk-unmark ()
8195 "Unmark the entry at point for future bulk action."
8196 (interactive)
8197 (when (org-agenda-bulk-marked-p)
8198 (org-agenda-bulk-remove-overlays
8199 (point-at-bol) (+ 2 (point-at-bol)))
8200 (setq org-agenda-bulk-marked-entries
8201 (delete (org-get-at-bol 'org-hd-marker)
8202 org-agenda-bulk-marked-entries)))
8203 (beginning-of-line 2)
8204 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8205 (beginning-of-line 2))
8206 (message "%d entries marked for bulk action"
8207 (length org-agenda-bulk-marked-entries)))
8209 (defun org-agenda-bulk-toggle ()
8210 "Toggle marking the entry at point for bulk action."
8211 (interactive)
8212 (if (org-agenda-bulk-marked-p)
8213 (org-agenda-bulk-unmark)
8214 (org-agenda-bulk-mark)))
8216 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8217 "Remove the mark overlays between BEG and END in the agenda buffer.
8218 BEG and END default to the buffer limits.
8220 This only removes the overlays, it does not remove the markers
8221 from the list in `org-agenda-bulk-marked-entries'."
8222 (interactive)
8223 (mapc (lambda (ov)
8224 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8225 (delete-overlay ov)))
8226 (overlays-in (or beg (point-min)) (or end (point-max)))))
8228 (defun org-agenda-bulk-remove-all-marks ()
8229 "Remove all marks in the agenda buffer.
8230 This will remove the markers, and the overlays."
8231 (interactive)
8232 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8233 (setq org-agenda-bulk-marked-entries nil)
8234 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8236 (defun org-agenda-bulk-action (&optional arg)
8237 "Execute an remote-editing action on all marked entries.
8238 The prefix arg is passed through to the command if possible."
8239 (interactive "P")
8240 ;; Make sure we have markers, and only valid ones
8241 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8242 (mapc
8243 (lambda (m)
8244 (unless (and (markerp m)
8245 (marker-buffer m)
8246 (buffer-live-p (marker-buffer m))
8247 (marker-position m))
8248 (error "Marker %s for bulk command is invalid" m)))
8249 org-agenda-bulk-marked-entries)
8251 ;; Prompt for the bulk command
8252 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8253 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8254 (when org-agenda-bulk-custom-functions
8255 (concat " Custom: ["
8256 (mapconcat (lambda(f) (char-to-string (car f)))
8257 org-agenda-bulk-custom-functions "")
8258 "]"))))
8259 (let* ((action (read-char-exclusive))
8260 (org-log-refile (if org-log-refile 'time nil))
8261 (entries (reverse org-agenda-bulk-marked-entries))
8262 redo-at-end
8263 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8264 (cond
8265 ((equal action ?$)
8266 (setq cmd '(org-agenda-archive)))
8268 ((equal action ?A)
8269 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8271 ((member action '(?r ?w))
8272 (setq rfloc (org-refile-get-location
8273 "Refile to"
8274 (marker-buffer (car org-agenda-bulk-marked-entries))
8275 org-refile-allow-creating-parent-nodes))
8276 (if (nth 3 rfloc)
8277 (setcar (nthcdr 3 rfloc)
8278 (move-marker (make-marker) (nth 3 rfloc)
8279 (or (get-file-buffer (nth 1 rfloc))
8280 (find-buffer-visiting (nth 1 rfloc))
8281 (error "This should not happen")))))
8283 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8284 redo-at-end t))
8286 ((equal action ?t)
8287 (setq state (org-icompleting-read
8288 "Todo state: "
8289 (with-current-buffer (marker-buffer (car entries))
8290 (mapcar 'list org-todo-keywords-1))))
8291 (setq cmd `(let ((org-inhibit-blocking t)
8292 (org-inhibit-logging 'note))
8293 (org-agenda-todo ,state))))
8295 ((memq action '(?- ?+))
8296 (setq tag (org-icompleting-read
8297 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8298 (with-current-buffer (marker-buffer (car entries))
8299 (delq nil
8300 (mapcar (lambda (x)
8301 (if (stringp (car x)) x)) org-tag-alist)))))
8302 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8304 ((memq action '(?s ?d))
8305 (let* ((date (unless arg
8306 (org-read-date
8307 nil nil nil
8308 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8309 (ans (if arg nil org-read-date-final-answer))
8310 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8311 (setq cmd `(let* ((bound (fboundp 'read-string))
8312 (old (and bound (symbol-function 'read-string))))
8313 (unwind-protect
8314 (progn
8315 (fset 'read-string (lambda (&rest ignore) ,ans))
8316 (eval '(,c1 arg)))
8317 (if bound
8318 (fset 'read-string old)
8319 (fmakunbound 'read-string)))))))
8321 ((equal action ?S)
8322 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8323 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8324 (let ((days (read-number
8325 (format "Scatter tasks across how many %sdays: "
8326 (if arg "week" "")) 7)))
8327 (setq cmd
8328 `(let ((distance (1+ (random ,days))))
8329 (if arg
8330 (let ((dist distance)
8331 (day-of-week
8332 (calendar-day-of-week
8333 (calendar-gregorian-from-absolute (org-today)))))
8334 (dotimes (i (1+ dist))
8335 (while (member day-of-week org-agenda-weekend-days)
8336 (incf distance)
8337 (incf day-of-week)
8338 (if (= day-of-week 7)
8339 (setq day-of-week 0)))
8340 (incf day-of-week)
8341 (if (= day-of-week 7)
8342 (setq day-of-week 0)))))
8343 ;; silently fail when try to replan a sexp entry
8344 (condition-case nil
8345 (let* ((date (calendar-gregorian-from-absolute
8346 (+ (org-today) distance)))
8347 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8348 (nth 2 date))))
8349 (org-agenda-schedule nil time))
8350 (error nil)))))))
8352 ((assoc action org-agenda-bulk-custom-functions)
8353 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8354 redo-at-end t))
8356 ((equal action ?f)
8357 (setq cmd (list (intern
8358 (org-icompleting-read "Function: "
8359 obarray 'fboundp t nil nil)))))
8361 (t (error "Invalid bulk action")))
8363 ;; Sort the markers, to make sure that parents are handled before children
8364 (setq entries (sort entries
8365 (lambda (a b)
8366 (cond
8367 ((equal (marker-buffer a) (marker-buffer b))
8368 (< (marker-position a) (marker-position b)))
8370 (string< (buffer-name (marker-buffer a))
8371 (buffer-name (marker-buffer b))))))))
8373 ;; Now loop over all markers and apply cmd
8374 (while (setq e (pop entries))
8375 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8376 (if (not pos)
8377 (progn (message "Skipping removed entry at %s" e)
8378 (setq cntskip (1+ cntskip)))
8379 (goto-char pos)
8380 (let (org-loop-over-headlines-in-active-region)
8381 (eval cmd))
8382 (setq org-agenda-bulk-marked-entries
8383 (delete e org-agenda-bulk-marked-entries))
8384 (setq cnt (1+ cnt))))
8385 (setq org-agenda-bulk-marked-entries nil)
8386 (org-agenda-bulk-remove-all-marks)
8387 (when redo-at-end (org-agenda-redo))
8388 (message "Acted on %d entries%s"
8390 (if (= cntskip 0)
8392 (format ", skipped %d (disappeared before their turn)"
8393 cntskip)))))
8395 ;;; Flagging notes
8397 (defun org-agenda-show-the-flagging-note ()
8398 "Display the flagging note in the other window.
8399 When called a second time in direct sequence, offer to remove the FLAGGING
8400 tag and (if present) the flagging note."
8401 (interactive)
8402 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8403 (win (selected-window))
8404 note heading newhead)
8405 (unless hdmarker
8406 (error "No linked entry at point"))
8407 (if (and (eq this-command last-command)
8408 (y-or-n-p "Unflag and remove any flagging note? "))
8409 (progn
8410 (org-agenda-remove-flag hdmarker)
8411 (let ((win (get-buffer-window "*Flagging Note*")))
8412 (and win (delete-window win)))
8413 (message "Entry unflaged"))
8414 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8415 (unless note
8416 (error "No flagging note"))
8417 (org-kill-new note)
8418 (org-switch-to-buffer-other-window "*Flagging Note*")
8419 (erase-buffer)
8420 (insert note)
8421 (goto-char (point-min))
8422 (while (re-search-forward "\\\\n" nil t)
8423 (replace-match "\n" t t))
8424 (goto-char (point-min))
8425 (select-window win)
8426 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8428 (defun org-agenda-remove-flag (marker)
8429 "Remove the FLAGGED tag and any flagging note in the entry."
8430 (let (newhead)
8431 (org-with-point-at marker
8432 (org-toggle-tag "FLAGGED" 'off)
8433 (org-entry-delete nil "THEFLAGGINGNOTE")
8434 (setq newhead (org-get-heading)))
8435 (org-agenda-change-all-lines newhead marker)
8436 (message "Entry unflaged")))
8438 (defun org-agenda-get-any-marker (&optional pos)
8439 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8440 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8442 ;;; Appointment reminders
8444 (defvar appt-time-msg-list)
8446 ;;;###autoload
8447 (defun org-agenda-to-appt (&optional refresh filter &rest args)
8448 "Activate appointments found in `org-agenda-files'.
8449 With a \\[universal-argument] prefix, refresh the list of
8450 appointments.
8452 If FILTER is t, interactively prompt the user for a regular
8453 expression, and filter out entries that don't match it.
8455 If FILTER is a string, use this string as a regular expression
8456 for filtering entries out.
8458 If FILTER is a function, filter out entries against which
8459 calling the function returns nil. This function takes one
8460 argument: an entry from `org-agenda-get-day-entries'.
8462 FILTER can also be an alist with the car of each cell being
8463 either 'headline or 'category. For example:
8465 '((headline \"IMPORTANT\")
8466 (category \"Work\"))
8468 will only add headlines containing IMPORTANT or headlines
8469 belonging to the \"Work\" category.
8471 ARGS are symbols indicating what kind of entries to consider.
8472 By default `org-agenda-to-appt' will use :deadline, :scheduled
8473 and :timestamp entries. See the docstring of `org-diary' for
8474 details and examples."
8475 (interactive "P")
8476 (if refresh (setq appt-time-msg-list nil))
8477 (if (eq filter t)
8478 (setq filter (read-from-minibuffer "Regexp filter: ")))
8479 (let* ((cnt 0) ; count added events
8480 (scope (or args '(:deadline :scheduled :timestamp)))
8481 (org-agenda-new-buffers nil)
8482 (org-deadline-warning-days 0)
8483 ;; Do not use `org-today' here because appt only takes
8484 ;; time and without date as argument, so it may pass wrong
8485 ;; information otherwise
8486 (today (org-date-to-gregorian
8487 (time-to-days (current-time))))
8488 (org-agenda-restrict nil)
8489 (files (org-agenda-files 'unrestricted)) entries file)
8490 ;; Get all entries which may contain an appt
8491 (org-prepare-agenda-buffers files)
8492 (while (setq file (pop files))
8493 (setq entries
8494 (delq nil
8495 (append entries
8496 (apply 'org-agenda-get-day-entries
8497 file today scope)))))
8498 ;; Map thru entries and find if we should filter them out
8499 (mapc
8500 (lambda(x)
8501 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8502 (cat (get-text-property 1 'org-category x))
8503 (tod (get-text-property 1 'time-of-day x))
8504 (ok (or (null filter)
8505 (and (stringp filter) (string-match filter evt))
8506 (and (functionp filter) (funcall filter x))
8507 (and (listp filter)
8508 (let ((cat-filter (cadr (assoc 'category filter)))
8509 (evt-filter (cadr (assoc 'headline filter))))
8510 (or (and (stringp cat-filter)
8511 (string-match cat-filter cat))
8512 (and (stringp evt-filter)
8513 (string-match evt-filter evt))))))))
8514 ;; FIXME: Shall we remove text-properties for the appt text?
8515 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8516 (when (and ok tod)
8517 (setq tod (concat "00" (number-to-string tod))
8518 tod (when (string-match
8519 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8520 (concat (match-string 1 tod) ":"
8521 (match-string 2 tod))))
8522 (appt-add tod evt)
8523 (setq cnt (1+ cnt))))) entries)
8524 (org-release-buffers org-agenda-new-buffers)
8525 (if (eq cnt 0)
8526 (message "No event to add")
8527 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8529 (defun org-agenda-todayp (date)
8530 "Does DATE mean today, when considering `org-extend-today-until'?"
8531 (let ((today (org-today))
8532 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8533 date)))
8534 (eq date today)))
8536 (defun org-agenda-todo-yesterday (&optional arg)
8537 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday"
8538 (interactive "P")
8539 (let* ((hour (third (decode-time
8540 (org-current-time))))
8541 (org-extend-today-until (1+ hour)))
8542 (org-agenda-todo arg)))
8544 (provide 'org-agenda)
8546 ;;; org-agenda.el ends here