Merge Org 7.9.3 (commit 31c1aea)
[bpt/emacs.git] / lisp / org / org-agenda.el
CommitLineData
b349f79f 1;;; org-agenda.el --- Dynamic task and appointment lists for Org
20908596 2
ab422c4d 3;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
20908596
CD
4
5;; Author: Carsten Dominik <carsten at orgmode dot org>
6;; Keywords: outlines, hypermedia, calendar, wp
7;; Homepage: http://orgmode.org
20908596
CD
8;;
9;; This file is part of GNU Emacs.
10;;
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
20908596
CD
15
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.
20
21;; You should have received a copy of the GNU General Public License
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
23;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24;;
25;;; Commentary:
26
27;; This file contains the code for creating and using the Agenda for Org-mode.
e66ba1df
BG
28;;
29;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30;; `org-batch-store-agenda-views' are implemented as macros to provide
27e428e7 31;; a convenient way for extracting agenda information from the command
8223b1d2 32;; line. The Lisp does not evaluate parameters of a macro call; thus
e66ba1df 33;; it is not necessary to quote the parameters passed to one of those
8223b1d2 34;; functions. E.g. you can write:
e66ba1df
BG
35;;
36;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
37;;
8223b1d2 38;; To export an agenda spanning 7 days. If `org-batch-agenda' would
e66ba1df 39;; have been implemented as a regular function you'd have to quote the
8223b1d2 40;; symbol org-agenda-span. Moreover: To use a symbol as parameter
e66ba1df
BG
41;; value you would have to double quote the symbol.
42;;
43;; This is a hack, but it works even when running Org byte-compiled.
44;;
20908596
CD
45
46;;; Code:
47
48(require 'org)
8223b1d2 49(require 'org-macs)
20908596 50(eval-when-compile
86fbb8ca 51 (require 'cl))
20908596 52
b349f79f 53(declare-function diary-add-to-list "diary-lib"
20908596
CD
54 (date string specifier &optional marker globcolor literal))
55(declare-function calendar-absolute-from-iso "cal-iso" (date))
56(declare-function calendar-astro-date-string "cal-julian" (&optional date))
57(declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
20908596
CD
58(declare-function calendar-chinese-date-string "cal-china" (&optional date))
59(declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60(declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61(declare-function calendar-french-date-string "cal-french" (&optional date))
62(declare-function calendar-goto-date "cal-move" (date))
63(declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64(declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65(declare-function calendar-iso-date-string "cal-iso" (&optional date))
f6aafbed 66(declare-function calendar-iso-from-absolute "cal-iso" (date))
20908596
CD
67(declare-function calendar-julian-date-string "cal-julian" (&optional date))
68(declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69(declare-function calendar-persian-date-string "cal-persia" (&optional date))
e66ba1df
BG
70(declare-function calendar-check-holidays "holidays" (date))
71
68a1b090
GM
72(declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
20908596 74(declare-function org-columns-quit "org-colview" ())
8bfe682a
CD
75(declare-function diary-date-display-form "diary-lib" (&optional type))
76(declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77(declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79(declare-function org-is-habit-p "org-habit" (&optional pom))
80(declare-function org-habit-parse-todo "org-habit" (&optional pom))
68a1b090 81(declare-function org-habit-get-priority "org-habit" (habit &optional moment))
e66ba1df
BG
82(declare-function org-pop-to-buffer-same-window "org-compat"
83 (&optional buffer-or-name norecord label))
8223b1d2
BG
84(declare-function org-agenda-columns "org-colview" ())
85(declare-function org-add-archive-files "org-archive" (files))
86(declare-function org-capture "org-capture" (&optional goto keys))
87
88(defvar calendar-mode-map) ; defined in calendar.el
89(defvar org-clock-current-task nil) ; defined in org-clock.el
90(defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
91(defvar org-habit-show-habits) ; defined in org-habit.el
8bfe682a 92(defvar org-habit-show-habits-only-for-today)
8223b1d2 93(defvar org-habit-show-all-today)
20908596
CD
94
95;; Defined somewhere in this file, but used before definition.
8223b1d2
BG
96(defvar org-agenda-buffer-name "*Org Agenda*")
97(defvar org-agenda-overriding-header nil)
8d642074 98(defvar org-agenda-title-append nil)
8223b1d2
BG
99(org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
100(org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
20908596
CD
101(defvar original-date) ; dynamically scoped, calendar.el does scope this
102
8223b1d2
BG
103(defvar org-agenda-undo-list nil
104 "List of undoable operations in the agenda since last refresh.")
105(defvar org-agenda-pending-undo-list nil
106 "In a series of undo commands, this is the list of remaining undo items.")
107
20908596
CD
108(defcustom org-agenda-confirm-kill 1
109 "When set, remote killing from the agenda buffer needs confirmation.
110When t, a confirmation is always needed. When a number N, confirmation is
111only needed when the text to be killed contains more than N non-white lines."
112 :group 'org-agenda
113 :type '(choice
114 (const :tag "Never" nil)
115 (const :tag "Always" t)
c8d0cf5c 116 (integer :tag "When more than N lines")))
20908596
CD
117
118(defcustom org-agenda-compact-blocks nil
ed21c5c8 119 "Non-nil means make the block agenda more compact.
3ab2c837
BG
120This is done globally by leaving out lines like the agenda span
121name and week number or the separator lines."
20908596
CD
122 :group 'org-agenda
123 :type 'boolean)
124
0bd48b37
CD
125(defcustom org-agenda-block-separator ?=
126 "The separator between blocks in the agenda.
127If this is a string, it will be used as the separator, with a newline added.
3ab2c837
BG
128If it is a character, it will be repeated to fill the window width.
129If nil the separator is disabled. In `org-agenda-custom-commands' this
130addresses the separator between the current and the previous block."
0bd48b37
CD
131 :group 'org-agenda
132 :type '(choice
3ab2c837 133 (const :tag "Disabled" nil)
0bd48b37
CD
134 (character)
135 (string)))
136
20908596 137(defgroup org-agenda-export nil
8223b1d2
BG
138 "Options concerning exporting agenda views in Org-mode."
139 :tag "Org Agenda Export"
140 :group 'org-agenda)
20908596
CD
141
142(defcustom org-agenda-with-colors t
ed21c5c8 143 "Non-nil means use colors in agenda views."
20908596
CD
144 :group 'org-agenda-export
145 :type 'boolean)
146
147(defcustom org-agenda-exporter-settings nil
148 "Alist of variable/value pairs that should be active during agenda export.
c8d0cf5c
CD
149This is a good place to set options for ps-print and for htmlize.
150Note that the way this is implemented, the values will be evaluated
151before assigned to the variables. So make sure to quote values you do
152*not* want evaluated, for example
153
154 (setq org-agenda-exporter-settings
155 '((ps-print-color-p 'black-white)))"
20908596
CD
156 :group 'org-agenda-export
157 :type '(repeat
158 (list
159 (variable)
160 (sexp :tag "Value"))))
161
c8d0cf5c 162(defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
8223b1d2
BG
163 "Hook run in a temporary buffer before writing the agenda to an export file.
164A useful function for this hook is `org-agenda-add-entry-text'."
c8d0cf5c
CD
165 :group 'org-agenda-export
166 :type 'hook
167 :options '(org-agenda-add-entry-text))
168
169(defcustom org-agenda-add-entry-text-maxlines 0
170 "Maximum number of entry text lines to be added to agenda.
171This is only relevant when `org-agenda-add-entry-text' is part of
8223b1d2 172`org-agenda-before-write-hook', which is the default.
c8d0cf5c
CD
173When this is 0, nothing will happen. When it is greater than 0, it
174specifies the maximum number of lines that will be added for each entry
54a0dee5
CD
175that is listed in the agenda view.
176
177Note that this variable is not used during display, only when exporting
86fbb8ca
CD
178the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
179and `org-agenda-entry-text-maxlines'."
c8d0cf5c
CD
180 :group 'org-agenda
181 :type 'integer)
182
183(defcustom org-agenda-add-entry-text-descriptive-links t
ed21c5c8 184 "Non-nil means export org-links as descriptive links in agenda added text.
c8d0cf5c
CD
185This variable applies to the text added to the agenda when
186`org-agenda-add-entry-text-maxlines' is larger than 0.
e66ba1df 187When this variable nil, the URL will (also) be shown."
c8d0cf5c
CD
188 :group 'org-agenda
189 :type 'boolean)
190
8223b1d2 191(defcustom org-agenda-export-html-style nil
20908596
CD
192 "The style specification for exported HTML Agenda files.
193If this variable contains a string, it will replace the default <style>
194section as produced by `htmlize'.
195Since there are different ways of setting style information, this variable
196needs to contain the full HTML structure to provide a style, including the
197surrounding HTML tags. The style specifications should include definitions
198the fonts used by the agenda, here is an example:
199
200 <style type=\"text/css\">
201 p { font-weight: normal; color: gray; }
202 .org-agenda-structure {
203 font-size: 110%;
204 color: #003399;
205 font-weight: 600;
206 }
207 .org-todo {
208 color: #cc6666;
209 font-weight: bold;
210 }
c8d0cf5c
CD
211 .org-agenda-done {
212 color: #339933;
213 }
20908596
CD
214 .org-done {
215 color: #339933;
216 }
217 .title { text-align: center; }
218 .todo, .deadline { color: red; }
219 .done { color: green; }
220 </style>
221
222or, if you want to keep the style in a file,
223
224 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
225
226As the value of this option simply gets inserted into the HTML <head> header,
8223b1d2 227you can \"misuse\" it to also add other text to the header."
20908596
CD
228 :group 'org-agenda-export
229 :group 'org-export-html
230 :type 'string)
231
86fbb8ca
CD
232(defcustom org-agenda-persistent-filter nil
233 "When set, keep filters from one agenda view to the next."
234 :group 'org-agenda
235 :type 'boolean)
236
20908596 237(defgroup org-agenda-custom-commands nil
8223b1d2
BG
238 "Options concerning agenda views in Org-mode."
239 :tag "Org Agenda Custom Commands"
240 :group 'org-agenda)
20908596
CD
241
242(defconst org-sorting-choice
243 '(choice
244 (const time-up) (const time-down)
245 (const category-keep) (const category-up) (const category-down)
246 (const tag-down) (const tag-up)
247 (const priority-up) (const priority-down)
621f83e4 248 (const todo-state-up) (const todo-state-down)
c8d0cf5c 249 (const effort-up) (const effort-down)
8bfe682a 250 (const habit-up) (const habit-down)
86fbb8ca 251 (const alpha-up) (const alpha-down)
c8d0cf5c 252 (const user-defined-up) (const user-defined-down))
20908596
CD
253 "Sorting choices.")
254
e66ba1df
BG
255;; Keep custom values for `org-agenda-filter-preset' compatible with
256;; the new variable `org-agenda-tag-filter-preset'.
8223b1d2
BG
257(if (fboundp 'defvaralias)
258 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
259 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter))
e66ba1df 260
20908596 261(defconst org-agenda-custom-commands-local-options
8223b1d2 262 `(repeat :tag "Local settings for this command. Remember to quote values"
20908596 263 (choice :tag "Setting"
8223b1d2
BG
264 (list :tag "Heading for this block"
265 (const org-agenda-overriding-header)
266 (string :tag "Headline"))
267 (list :tag "Files to be searched"
268 (const org-agenda-files)
269 (list
270 (const :format "" quote)
271 (repeat (file))))
272 (list :tag "Sorting strategy"
273 (const org-agenda-sorting-strategy)
274 (list
275 (const :format "" quote)
276 (repeat
277 ,org-sorting-choice)))
278 (list :tag "Prefix format"
279 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
280 (string))
281 (list :tag "Number of days in agenda"
282 (const org-agenda-span)
283 (choice (const :tag "Day" 'day)
284 (const :tag "Week" 'week)
285 (const :tag "Month" 'month)
286 (const :tag "Year" 'year)
287 (integer :tag "Custom")))
288 (list :tag "Fixed starting date"
289 (const org-agenda-start-day)
290 (string :value "2007-11-01"))
291 (list :tag "Start on day of week"
292 (const org-agenda-start-on-weekday)
293 (choice :value 1
294 (const :tag "Today" nil)
295 (integer :tag "Weekday No.")))
296 (list :tag "Include data from diary"
297 (const org-agenda-include-diary)
298 (boolean))
299 (list :tag "Deadline Warning days"
300 (const org-deadline-warning-days)
301 (integer :value 1))
302 (list :tag "Category filter preset"
303 (const org-agenda-category-filter-preset)
304 (list
305 (const :format "" quote)
306 (repeat
307 (string :tag "+category or -category"))))
308 (list :tag "Tags filter preset"
309 (const org-agenda-tag-filter-preset)
310 (list
311 (const :format "" quote)
312 (repeat
313 (string :tag "+tag or -tag"))))
314 (list :tag "Set daily/weekly entry types"
315 (const org-agenda-entry-types)
316 (list
317 (const :format "" quote)
318 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
319 (const :deadline)
320 (const :scheduled)
321 (const :timestamp)
322 (const :sexp))))
323 (list :tag "Standard skipping condition"
324 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
325 (const org-agenda-skip-function)
326 (list
327 (const :format "" quote)
328 (list
329 (choice
330 :tag "Skipping range"
331 (const :tag "Skip entry" org-agenda-skip-entry-if)
332 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
333 (repeat :inline t :tag "Conditions for skipping"
ed21c5c8 334 (choice
8223b1d2
BG
335 :tag "Condition type"
336 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
337 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
338 (list :tag "TODO state is" :inline t
339 (const 'todo)
340 (choice
341 (const :tag "any not-done state" 'todo)
342 (const :tag "any done state" 'done)
343 (const :tag "any state" 'any)
344 (list :tag "Keyword list"
345 (const :format "" quote)
346 (repeat (string :tag "Keyword")))))
347 (list :tag "TODO state is not" :inline t
348 (const 'nottodo)
349 (choice
350 (const :tag "any not-done state" 'todo)
351 (const :tag "any done state" 'done)
352 (const :tag "any state" 'any)
353 (list :tag "Keyword list"
354 (const :format "" quote)
355 (repeat (string :tag "Keyword")))))
356 (const :tag "scheduled" 'scheduled)
357 (const :tag "not scheduled" 'notscheduled)
358 (const :tag "deadline" 'deadline)
359 (const :tag "no deadline" 'notdeadline)
360 (const :tag "timestamp" 'timestamp)
361 (const :tag "no timestamp" 'nottimestamp))))))
362 (list :tag "Non-standard skipping condition"
363 :value (org-agenda-skip-function)
364 (const org-agenda-skip-function)
365 (sexp :tag "Function or form (quoted!)"))
366 (list :tag "Any variable"
367 (variable :tag "Variable")
368 (sexp :tag "Value (sexp)"))))
20908596
CD
369 "Selection of examples for agenda command settings.
370This will be spliced into the custom type of
371`org-agenda-custom-commands'.")
372
373
e66ba1df
BG
374(defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
375 ((agenda "") (alltodo))))
20908596
CD
376 "Custom commands for the agenda.
377These commands will be offered on the splash screen displayed by the
378agenda dispatcher \\[org-agenda]. Each entry is a list like this:
379
380 (key desc type match settings files)
381
382key The key (one or more characters as a string) to be associated
383 with the command.
e66ba1df 384desc A description of the command, when omitted or nil, a default
20908596
CD
385 description is built using MATCH.
386type The command type, any of the following symbols:
387 agenda The daily/weekly agenda.
388 todo Entries with a specific TODO keyword, in all agenda files.
389 search Entries containing search words entry or headline.
390 tags Tags/Property/TODO match in all agenda files.
391 tags-todo Tags/P/T match in all agenda files, TODO entries only.
392 todo-tree Sparse tree of specific TODO keyword in *current* file.
393 tags-tree Sparse tree with all tags matches in *current* file.
394 occur-tree Occur sparse tree for *current* file.
395 ... A user-defined function.
396match What to search for:
397 - a single keyword for TODO keyword searches
398 - a tags match expression for tags searches
e66ba1df 399 - a word search expression for text searches.
20908596
CD
400 - a regular expression for occur searches
401 For all other commands, this should be the empty string.
402settings A list of option settings, similar to that in a let form, so like
403 this: ((opt1 val1) (opt2 val2) ...). The values will be
404 evaluated at the moment of execution, so quote them when needed.
405files A list of files file to write the produced agenda buffer to
406 with the command `org-store-agenda-views'.
407 If a file name ends in \".html\", an HTML version of the buffer
e66ba1df 408 is written out. If it ends in \".ps\", a postscript version is
33306645 409 produced. Otherwise, only the plain text is written to the file.
20908596
CD
410
411You can also define a set of commands, to create a composite agenda buffer.
412In this case, an entry looks like this:
413
414 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
415
416where
417
418desc A description string to be displayed in the dispatcher menu.
419cmd An agenda command, similar to the above. However, tree commands
153ae947 420 are not allowed, but instead you can get agenda and global todo list.
20908596
CD
421 So valid commands for a set are:
422 (agenda \"\" settings)
423 (alltodo \"\" settings)
424 (stuck \"\" settings)
425 (todo \"match\" settings files)
426 (search \"match\" settings files)
427 (tags \"match\" settings files)
428 (tags-todo \"match\" settings files)
429
430Each command can carry a list of options, and another set of options can be
431given for the whole set of commands. Individual command options take
432precedence over the general options.
433
434When using several characters as key to a command, the first characters
435are prefix commands. For the dispatcher to display useful information, you
436should provide a description for the prefix, like
437
438 (setq org-agenda-custom-commands
439 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
440 (\"hl\" tags \"+HOME+Lisa\")
441 (\"hp\" tags \"+HOME+Peter\")
442 (\"hk\" tags \"+HOME+Kim\")))"
443 :group 'org-agenda-custom-commands
444 :type `(repeat
445 (choice :value ("x" "Describe command here" tags "" nil)
8223b1d2
BG
446 (list :tag "Single command"
447 (string :tag "Access Key(s) ")
448 (option (string :tag "Description"))
449 (choice
450 (const :tag "Agenda" agenda)
451 (const :tag "TODO list" alltodo)
452 (const :tag "Search words" search)
453 (const :tag "Stuck projects" stuck)
454 (const :tag "Tags/Property match (all agenda files)" tags)
455 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
456 (const :tag "TODO keyword search (all agenda files)" todo)
457 (const :tag "Tags sparse tree (current buffer)" tags-tree)
458 (const :tag "TODO keyword tree (current buffer)" todo-tree)
459 (const :tag "Occur tree (current buffer)" occur-tree)
460 (sexp :tag "Other, user-defined function"))
461 (string :tag "Match (only for some commands)")
462 ,org-agenda-custom-commands-local-options
463 (option (repeat :tag "Export" (file :tag "Export to"))))
464 (list :tag "Command series, all agenda files"
465 (string :tag "Access Key(s)")
466 (string :tag "Description ")
467 (repeat :tag "Component"
468 (choice
469 (list :tag "Agenda"
470 (const :format "" agenda)
471 (const :tag "" :format "" "")
472 ,org-agenda-custom-commands-local-options)
473 (list :tag "TODO list (all keywords)"
474 (const :format "" alltodo)
475 (const :tag "" :format "" "")
476 ,org-agenda-custom-commands-local-options)
477 (list :tag "Search words"
478 (const :format "" search)
479 (string :tag "Match")
480 ,org-agenda-custom-commands-local-options)
481 (list :tag "Stuck projects"
482 (const :format "" stuck)
483 (const :tag "" :format "" "")
484 ,org-agenda-custom-commands-local-options)
485 (list :tag "Tags search"
486 (const :format "" tags)
487 (string :tag "Match")
488 ,org-agenda-custom-commands-local-options)
489 (list :tag "Tags search, TODO entries only"
490 (const :format "" tags-todo)
491 (string :tag "Match")
492 ,org-agenda-custom-commands-local-options)
493 (list :tag "TODO keyword search"
494 (const :format "" todo)
495 (string :tag "Match")
496 ,org-agenda-custom-commands-local-options)
497 (list :tag "Other, user-defined function"
498 (symbol :tag "function")
499 (string :tag "Match")
500 ,org-agenda-custom-commands-local-options)))
501
502 (repeat :tag "Settings for entire command set"
503 (list (variable :tag "Any variable")
504 (sexp :tag "Value")))
505 (option (repeat :tag "Export" (file :tag "Export to"))))
506 (cons :tag "Prefix key documentation"
507 (string :tag "Access Key(s)")
508 (string :tag "Description ")))))
20908596
CD
509
510(defcustom org-agenda-query-register ?o
511 "The register holding the current query string.
33306645 512The purpose of this is that if you construct a query string interactively,
20908596
CD
513you can then use it to define a custom command."
514 :group 'org-agenda-custom-commands
515 :type 'character)
516
517(defcustom org-stuck-projects
518 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
519 "How to identify stuck projects.
520This is a list of four items:
c8d0cf5c
CD
5211. A tags/todo/property matcher string that is used to identify a project.
522 See the manual for a description of tag and property searches.
20908596
CD
523 The entire tree below a headline matched by this is considered one project.
5242. A list of TODO keywords identifying non-stuck projects.
525 If the project subtree contains any headline with one of these todo
526 keywords, the project is considered to be not stuck. If you specify
527 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
5283. A list of tags identifying non-stuck projects.
529 If the project subtree contains any headline with one of these tags,
530 the project is considered to be not stuck. If you specify \"*\" as
c8d0cf5c
CD
531 a tag, any tag will mark the project unstuck. Note that this is about
532 the explicit presence of a tag somewhere in the subtree, inherited
533 tags to not count here. If inherited tags make a project not stuck,
534 use \"-TAG\" in the tags part of the matcher under (1.) above.
20908596
CD
5354. An arbitrary regular expression matching non-stuck projects.
536
c8d0cf5c
CD
537If the project turns out to be not stuck, search continues also in the
538subtree to see if any of the subtasks have project status.
539
540See also the variable `org-tags-match-list-sublevels' which applies
541to projects matched by this search as well.
542
20908596
CD
543After defining this variable, you may use \\[org-agenda-list-stuck-projects]
544or `C-c a #' to produce the list."
545 :group 'org-agenda-custom-commands
546 :type '(list
547 (string :tag "Tags/TODO match to identify a project")
548 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
549 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
c8d0cf5c 550 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
20908596 551
71d35b24
CD
552(defcustom org-agenda-filter-effort-default-operator "<"
553 "The default operator for effort estimate filtering.
93b62de8 554If you select an effort estimate limit without first pressing an operator,
71d35b24
CD
555this one will be used."
556 :group 'org-agenda-custom-commands
557 :type '(choice (const :tag "less or equal" "<")
558 (const :tag "greater or equal"">")
559 (const :tag "equal" "=")))
20908596
CD
560
561(defgroup org-agenda-skip nil
8223b1d2
BG
562 "Options concerning skipping parts of agenda files."
563 :tag "Org Agenda Skip"
564 :group 'org-agenda)
3ab2c837
BG
565
566(defcustom org-agenda-skip-function-global nil
567 "Function to be called at each match during agenda construction.
568If this function returns nil, the current match should not be skipped.
569If the function decided to skip an agenda match, is must return the
570buffer position from which the search should be continued.
571This may also be a Lisp form, which will be evaluated.
572
573This variable will be applied to every agenda match, including
574tags/property searches and TODO lists. So try to make the test function
575do its checking as efficiently as possible. To implement a skipping
576condition just for specific agenda commands, use the variable
577`org-agenda-skip-function' which can be set in the options section
578of custom agenda commands."
579 :group 'org-agenda-skip
580 :type 'sexp)
581
0bd48b37
CD
582(defgroup org-agenda-daily/weekly nil
583 "Options concerning the daily/weekly agenda."
584 :tag "Org Agenda Daily/Weekly"
585 :group 'org-agenda)
586(defgroup org-agenda-todo-list nil
587 "Options concerning the global todo list agenda view."
588 :tag "Org Agenda Todo List"
589 :group 'org-agenda)
590(defgroup org-agenda-match-view nil
591 "Options concerning the general tags/property/todo match agenda view."
592 :tag "Org Agenda Match View"
593 :group 'org-agenda)
8bfe682a
CD
594(defgroup org-agenda-search-view nil
595 "Options concerning the general tags/property/todo match agenda view."
596 :tag "Org Agenda Match View"
597 :group 'org-agenda)
20908596 598
2c3ad40d 599(defvar org-agenda-archives-mode nil
ed21c5c8 600 "Non-nil means the agenda will include archived items.
2c3ad40d
CD
601If this is the symbol `trees', trees in the selected agenda scope
602that are marked with the ARCHIVE tag will be included anyway. When this is
603t, also all archive files associated with the current selection of agenda
604files will be included.")
605
b349f79f 606(defcustom org-agenda-skip-comment-trees t
ed21c5c8 607 "Non-nil means skip trees that start with the COMMENT keyword.
33306645 608When nil, these trees are also scanned by agenda commands."
b349f79f
CD
609 :group 'org-agenda-skip
610 :type 'boolean)
611
20908596 612(defcustom org-agenda-todo-list-sublevels t
ed21c5c8 613 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
20908596
CD
614When nil, the sublevels of a TODO entry are not checked, resulting in
615potentially much shorter TODO lists."
616 :group 'org-agenda-skip
0bd48b37 617 :group 'org-agenda-todo-list
20908596
CD
618 :type 'boolean)
619
620(defcustom org-agenda-todo-ignore-with-date nil
ed21c5c8 621 "Non-nil means don't show entries with a date in the global todo list.
20908596
CD
622You can use this if you prefer to mark mere appointments with a TODO keyword,
623but don't want them to show up in the TODO list.
624When this is set, it also covers deadlines and scheduled items, the settings
625of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
c8d0cf5c
CD
626will be ignored.
627See also the variable `org-agenda-tags-todo-honor-ignore-options'."
20908596 628 :group 'org-agenda-skip
0bd48b37 629 :group 'org-agenda-todo-list
20908596
CD
630 :type 'boolean)
631
acedf35c
CD
632(defcustom org-agenda-todo-ignore-timestamp nil
633 "Non-nil means don't show entries with a timestamp.
634This applies when creating the global todo list.
635Valid values are:
636
637past Don't show entries for today or in the past.
638
639future Don't show entries with a timestamp in the future.
640 The idea behind this is that if it has a future
641 timestamp, you don't want to think about it until the
642 date.
643
644all Don't show any entries with a timestamp in the global todo list.
645 The idea behind this is that by setting a timestamp, you
646 have already \"taken care\" of this item.
647
8223b1d2
BG
648This variable can also have an integer as a value. If positive (N),
649todos with a timestamp N or more days in the future will be ignored. If
3ab2c837 650negative (-N), todos with a timestamp N or more days in the past will be
8223b1d2
BG
651ignored. If 0, todos with a timestamp either today or in the future will
652be ignored. For example, a value of -1 will exclude todos with a
3ab2c837
BG
653timestamp in the past (yesterday or earlier), while a value of 7 will
654exclude todos with a timestamp a week or more in the future.
655
acedf35c
CD
656See also `org-agenda-todo-ignore-with-date'.
657See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
658to make his option also apply to the tags-todo list."
659 :group 'org-agenda-skip
660 :group 'org-agenda-todo-list
372d7b21 661 :version "24.1"
acedf35c
CD
662 :type '(choice
663 (const :tag "Ignore future timestamp todos" future)
664 (const :tag "Ignore past or present timestamp todos" past)
665 (const :tag "Ignore all timestamp todos" all)
3ab2c837
BG
666 (const :tag "Show timestamp todos" nil)
667 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
acedf35c 668
20908596 669(defcustom org-agenda-todo-ignore-scheduled nil
ed21c5c8
CD
670 "Non-nil means, ignore some scheduled TODO items when making TODO list.
671This applies when creating the global todo list.
672Valid values are:
673
674past Don't show entries scheduled today or in the past.
675
676future Don't show entries scheduled in the future.
677 The idea behind this is that by scheduling it, you don't want to
678 think about it until the scheduled date.
679
680all Don't show any scheduled entries in the global todo list.
681 The idea behind this is that by scheduling it, you have already
682 \"taken care\" of this item.
683
684t Same as `all', for backward compatibility.
685
8223b1d2 686This variable can also have an integer as a value. See
3ab2c837
BG
687`org-agenda-todo-ignore-timestamp' for more details.
688
c8d0cf5c 689See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
690See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
691to make his option also apply to the tags-todo list."
20908596 692 :group 'org-agenda-skip
0bd48b37 693 :group 'org-agenda-todo-list
ed21c5c8
CD
694 :type '(choice
695 (const :tag "Ignore future-scheduled todos" future)
696 (const :tag "Ignore past- or present-scheduled todos" past)
697 (const :tag "Ignore all scheduled todos" all)
698 (const :tag "Ignore all scheduled todos (compatibility)" t)
3ab2c837
BG
699 (const :tag "Show scheduled todos" nil)
700 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
20908596
CD
701
702(defcustom org-agenda-todo-ignore-deadlines nil
ed21c5c8
CD
703 "Non-nil means ignore some deadlined TODO items when making TODO list.
704There are different motivations for using different values, please think
705carefully when configuring this variable.
706
86fbb8ca 707This applies when creating the global todo list.
ed21c5c8
CD
708Valid values are:
709
710near Don't show near deadline entries. A deadline is near when it is
711 closer than `org-deadline-warning-days' days. The idea behind this
712 is that such items will appear in the agenda anyway.
713
714far Don't show TODO entries where a deadline has been defined, but
715 the deadline is not near. This is useful if you don't want to
716 use the todo list to figure out what to do now.
717
718past Don't show entries with a deadline timestamp for today or in the past.
719
720future Don't show entries with a deadline timestamp in the future, not even
721 when they become `near' ones. Use it with caution.
722
723all Ignore all TODO entries that do have a deadline.
724
725t Same as `near', for backward compatibility.
726
8223b1d2 727This variable can also have an integer as a value. See
3ab2c837
BG
728`org-agenda-todo-ignore-timestamp' for more details.
729
c8d0cf5c 730See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
731See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
732to make his option also apply to the tags-todo list."
20908596 733 :group 'org-agenda-skip
0bd48b37 734 :group 'org-agenda-todo-list
ed21c5c8
CD
735 :type '(choice
736 (const :tag "Ignore near deadlines" near)
737 (const :tag "Ignore near deadlines (compatibility)" t)
738 (const :tag "Ignore far deadlines" far)
739 (const :tag "Ignore all TODOs with a deadlines" all)
3ab2c837
BG
740 (const :tag "Show all TODOs, even if they have a deadline" nil)
741 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
0bd48b37
CD
742
743(defcustom org-agenda-tags-todo-honor-ignore-options nil
ed21c5c8 744 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
0bd48b37
CD
745The variables
746 `org-agenda-todo-ignore-with-date',
acedf35c
CD
747 `org-agenda-todo-ignore-timestamp',
748 `org-agenda-todo-ignore-scheduled',
0bd48b37
CD
749 `org-agenda-todo-ignore-deadlines'
750make the global TODO list skip entries that have time stamps of certain
751kinds. If this option is set, the same options will also apply for the
752tags-todo search, which is the general tags/property matcher
753restricted to unfinished TODO entries only."
754 :group 'org-agenda-skip
755 :group 'org-agenda-todo-list
756 :group 'org-agenda-match-view
20908596
CD
757 :type 'boolean)
758
759(defcustom org-agenda-skip-scheduled-if-done nil
760 "Non-nil means don't show scheduled items in agenda when they are done.
761This is relevant for the daily/weekly agenda, not for the TODO list. And
762it applies only to the actual date of the scheduling. Warnings about
763an item with a past scheduling dates are always turned off when the item
764is DONE."
765 :group 'org-agenda-skip
0bd48b37 766 :group 'org-agenda-daily/weekly
20908596
CD
767 :type 'boolean)
768
54a0dee5
CD
769(defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
770 "Non-nil means skip scheduling line if same entry shows because of deadline.
771In the agenda of today, an entry can show up multiple times because
772it is both scheduled and has a nearby deadline, and maybe a plain time
773stamp as well.
774When this variable is t, then only the deadline is shown and the fact that
775the entry is scheduled today or was scheduled previously is not shown.
776When this variable is nil, the entry will be shown several times. When
777the variable is the symbol `not-today', then skip scheduled previously,
778but not scheduled today."
779 :group 'org-agenda-skip
780 :group 'org-agenda-daily/weekly
781 :type '(choice
782 (const :tag "Never" nil)
783 (const :tag "Always" t)
784 (const :tag "Not when scheduled today" not-today)))
785
8223b1d2
BG
786(defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
787 "Non-nil means skip timestamp line if same entry shows because of deadline.
788In the agenda of today, an entry can show up multiple times
789because it has both a plain timestamp and has a nearby deadline.
790When this variable is t, then only the deadline is shown and the
791fact that the entry has a timestamp for or including today is not
792shown. When this variable is nil, the entry will be shown
793several times."
794 :group 'org-agenda-skip
795 :group 'org-agenda-daily/weekly
796 :version "24.1"
797 :type '(choice
798 (const :tag "Never" nil)
799 (const :tag "Always" t)))
800
20908596 801(defcustom org-agenda-skip-deadline-if-done nil
33306645 802 "Non-nil means don't show deadlines when the corresponding item is done.
20908596
CD
803When nil, the deadline is still shown and should give you a happy feeling.
804This is relevant for the daily/weekly agenda. And it applied only to the
33306645 805actually date of the deadline. Warnings about approaching and past-due
20908596
CD
806deadlines are always turned off when the item is DONE."
807 :group 'org-agenda-skip
0bd48b37 808 :group 'org-agenda-daily/weekly
20908596
CD
809 :type 'boolean)
810
ed21c5c8
CD
811(defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
812 "Non-nil means skip deadline prewarning when entry is also scheduled.
813This will apply on all days where a prewarning for the deadline would
814be shown, but not at the day when the entry is actually due. On that day,
815the deadline will be shown anyway.
816This variable may be set to nil, t, or a number which will then give
817the number of days before the actual deadline when the prewarnings
818should resume.
819This can be used in a workflow where the first showing of the deadline will
820trigger you to schedule it, and then you don't want to be reminded of it
821because you will take care of it on the day when scheduled."
822 :group 'org-agenda-skip
823 :group 'org-agenda-daily/weekly
372d7b21 824 :version "24.1"
ed21c5c8 825 :type '(choice
735135f9 826 (const :tag "Always show prewarning" nil)
ed21c5c8
CD
827 (const :tag "Remove prewarning if entry is scheduled" t)
828 (integer :tag "Restart prewarning N days before deadline")))
829
e66ba1df 830(defcustom org-agenda-skip-additional-timestamps-same-entry nil
c8d0cf5c
CD
831 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
832When non-nil, after the search for timestamps has matched once in an
833entry, the rest of the entry will not be searched."
834 :group 'org-agenda-skip
835 :type 'boolean)
836
20908596
CD
837(defcustom org-agenda-skip-timestamp-if-done nil
838 "Non-nil means don't select item by timestamp or -range if it is DONE."
839 :group 'org-agenda-skip
0bd48b37 840 :group 'org-agenda-daily/weekly
20908596
CD
841 :type 'boolean)
842
c7cf0ebc 843(defcustom org-agenda-dim-blocked-tasks nil
ed21c5c8 844 "Non-nil means dim blocked tasks in the agenda display.
c8d0cf5c
CD
845This causes some overhead during agenda construction, but if you
846have turned on `org-enforce-todo-dependencies',
847`org-enforce-todo-checkbox-dependencies', or any other blocking
848mechanism, this will create useful feedback in the agenda.
849
8bfe682a 850Instead of t, this variable can also have the value `invisible'.
c8d0cf5c
CD
851Then blocked tasks will be invisible and only become visible when
852they become unblocked. An exemption to this behavior is when a task is
853blocked because of unchecked checkboxes below it. Since checkboxes do
854not show up in the agenda views, making this task invisible you remove any
855trace from agenda views that there is something to do. Therefore, a task
856that is blocked because of checkboxes will never be made invisible, it
857will only be dimmed."
d6685abc
CD
858 :group 'org-agenda-daily/weekly
859 :group 'org-agenda-todo-list
c7cf0ebc 860 :version "24.3"
d6685abc
CD
861 :type '(choice
862 (const :tag "Do not dim" nil)
e4769531 863 (const :tag "Dim to a gray face" t)
8bfe682a 864 (const :tag "Make invisible" invisible)))
d6685abc 865
20908596 866(defcustom org-timeline-show-empty-dates 3
ed21c5c8 867 "Non-nil means `org-timeline' also shows dates without an entry.
20908596
CD
868When nil, only the days which actually have entries are shown.
869When t, all days between the first and the last date are shown.
870When an integer, show also empty dates, but if there is a gap of more than
871N days, just insert a special line indicating the size of the gap."
872 :group 'org-agenda-skip
873 :type '(choice
874 (const :tag "None" nil)
875 (const :tag "All" t)
c8d0cf5c 876 (integer :tag "at most")))
20908596 877
20908596
CD
878(defgroup org-agenda-startup nil
879 "Options concerning initial settings in the Agenda in Org Mode."
880 :tag "Org Agenda Startup"
881 :group 'org-agenda)
882
afe98dfa 883(defcustom org-agenda-menu-show-matcher t
3ab2c837 884 "Non-nil means show the match string in the agenda dispatcher menu.
afe98dfa
CD
885When nil, the matcher string is not shown, but is put into the help-echo
886property so than moving the mouse over the command shows it.
887Setting it to nil is good if matcher strings are very long and/or if
8223b1d2 888you want to use two-columns display (see `org-agenda-menu-two-columns')."
afe98dfa 889 :group 'org-agenda
372d7b21 890 :version "24.1"
afe98dfa
CD
891 :type 'boolean)
892
c7cf0ebc 893(org-define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
8223b1d2
BG
894
895(defcustom org-agenda-menu-two-columns nil
afe98dfa
CD
896 "Non-nil means, use two columns to show custom commands in the dispatcher.
897If you use this, you probably want to set `org-agenda-menu-show-matcher'
898to nil."
899 :group 'org-agenda
372d7b21 900 :version "24.1"
afe98dfa
CD
901 :type 'boolean)
902
c7cf0ebc 903(org-define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
8223b1d2
BG
904(defcustom org-agenda-finalize-hook nil
905 "Hook run just before displaying an agenda buffer.
906The buffer is still writable when the hook is called.
907
908You can modify some of the buffer substrings but you should be
909extra careful not to modify the text properties of the agenda
910headlines as the agenda display heavily relies on them."
20908596
CD
911 :group 'org-agenda-startup
912 :type 'hook)
913
914(defcustom org-agenda-mouse-1-follows-link nil
ed21c5c8 915 "Non-nil means mouse-1 on a link will follow the link in the agenda.
20908596
CD
916A longer mouse click will still set point. Does not work on XEmacs.
917Needs to be set before org.el is loaded."
918 :group 'org-agenda-startup
919 :type 'boolean)
920
921(defcustom org-agenda-start-with-follow-mode nil
86fbb8ca 922 "The initial value of follow mode in a newly created agenda window."
20908596
CD
923 :group 'org-agenda-startup
924 :type 'boolean)
925
e66ba1df
BG
926(defcustom org-agenda-follow-indirect nil
927 "Non-nil means `org-agenda-follow-mode' displays only the
928current item's tree, in an indirect buffer."
929 :group 'org-agenda
372d7b21 930 :version "24.1"
e66ba1df
BG
931 :type 'boolean)
932
1bcdebed 933(defcustom org-agenda-show-outline-path t
ed21c5c8 934 "Non-nil means show outline path in echo area after line motion."
1bcdebed
CD
935 :group 'org-agenda-startup
936 :type 'boolean)
937
54a0dee5
CD
938(defcustom org-agenda-start-with-entry-text-mode nil
939 "The initial value of entry-text-mode in a newly created agenda window."
940 :group 'org-agenda-startup
941 :type 'boolean)
942
943(defcustom org-agenda-entry-text-maxlines 5
8bfe682a 944 "Number of text lines to be added when `E' is pressed in the agenda.
54a0dee5
CD
945
946Note that this variable only used during agenda display. Add add entry text
947when exporting the agenda, configure the variable
948`org-agenda-add-entry-ext-maxlines'."
949 :group 'org-agenda
950 :type 'integer)
951
8d642074
CD
952(defcustom org-agenda-entry-text-exclude-regexps nil
953 "List of regular expressions to clean up entry text.
954The complete matches of all regular expressions in this list will be
955removed from entry text before it is shown in the agenda."
956 :group 'org-agenda
957 :type '(repeat (regexp)))
958
959(defvar org-agenda-entry-text-cleanup-hook nil
960 "Hook that is run after basic cleanup of entry text to be shown in agenda.
961This cleanup is done in a temporary buffer, so the function may inspect and
962change the entire buffer.
963Some default stuff like drawers and scheduling/deadline dates will already
964have been removed when this is called, as will any matches for regular
965expressions listed in `org-agenda-entry-text-exclude-regexps'.")
966
20908596 967(defvar org-agenda-include-inactive-timestamps nil
8223b1d2
BG
968 "Non-nil means include inactive time stamps in agenda and timeline.
969Dynamically scoped.")
20908596
CD
970
971(defgroup org-agenda-windows nil
972 "Options concerning the windows used by the Agenda in Org Mode."
973 :tag "Org Agenda Windows"
974 :group 'org-agenda)
975
976(defcustom org-agenda-window-setup 'reorganize-frame
977 "How the agenda buffer should be displayed.
978Possible values for this option are:
979
980current-window Show agenda in the current window, keeping all other windows.
20908596
CD
981other-window Use `switch-to-buffer-other-window' to display agenda.
982reorganize-frame Show only two windows on the current frame, the current
983 window and the agenda.
8d642074
CD
984other-frame Use `switch-to-buffer-other-frame' to display agenda.
985 Also, when exiting the agenda, kill that frame.
20908596
CD
986See also the variable `org-agenda-restore-windows-after-quit'."
987 :group 'org-agenda-windows
988 :type '(choice
989 (const current-window)
990 (const other-frame)
991 (const other-window)
992 (const reorganize-frame)))
993
994(defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
995 "The min and max height of the agenda window as a fraction of frame height.
996The value of the variable is a cons cell with two numbers between 0 and 1.
997It only matters if `org-agenda-window-setup' is `reorganize-frame'."
998 :group 'org-agenda-windows
999 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1000
1001(defcustom org-agenda-restore-windows-after-quit nil
3ab2c837 1002 "Non-nil means restore window configuration upon exiting agenda.
20908596
CD
1003Before the window configuration is changed for displaying the agenda,
1004the current status is recorded. When the agenda is exited with
1005`q' or `x' and this option is set, the old state is restored. If
1006`org-agenda-window-setup' is `other-frame', the value of this
baf0cb84 1007option will be ignored."
20908596
CD
1008 :group 'org-agenda-windows
1009 :type 'boolean)
1010
acedf35c 1011(defcustom org-agenda-ndays nil
8223b1d2 1012 "Number of days to include in overview display.
c8d0cf5c 1013Should be 1 or 7.
acedf35c 1014Obsolete, see `org-agenda-span'."
8223b1d2
BG
1015 :group 'org-agenda-daily/weekly
1016 :type 'integer)
acedf35c
CD
1017
1018(make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1019
1020(defcustom org-agenda-span 'week
1021 "Number of days to include in overview display.
1022Can be day, week, month, year, or any number of days.
c8d0cf5c 1023Custom commands can set this variable in the options section."
20908596 1024 :group 'org-agenda-daily/weekly
acedf35c
CD
1025 :type '(choice (const :tag "Day" day)
1026 (const :tag "Week" week)
1027 (const :tag "Month" month)
1028 (const :tag "Year" year)
1029 (integer :tag "Custom")))
20908596
CD
1030
1031(defcustom org-agenda-start-on-weekday 1
ed21c5c8 1032 "Non-nil means start the overview always on the specified weekday.
20908596 10330 denotes Sunday, 1 denotes Monday etc.
c8d0cf5c
CD
1034When nil, always start on the current day.
1035Custom commands can set this variable in the options section."
20908596
CD
1036 :group 'org-agenda-daily/weekly
1037 :type '(choice (const :tag "Today" nil)
c8d0cf5c 1038 (integer :tag "Weekday No.")))
20908596
CD
1039
1040(defcustom org-agenda-show-all-dates t
ed21c5c8 1041 "Non-nil means `org-agenda' shows every day in the selected range.
20908596
CD
1042When nil, only the days which actually have entries are shown."
1043 :group 'org-agenda-daily/weekly
1044 :type 'boolean)
1045
1046(defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1047 "Format string for displaying dates in the agenda.
1048Used by the daily/weekly agenda and by the timeline. This should be
1049a format string understood by `format-time-string', or a function returning
1050the formatted date as a string. The function must take a single argument,
1051a calendar-style date list like (month day year)."
1052 :group 'org-agenda-daily/weekly
1053 :type '(choice
1054 (string :tag "Format string")
1055 (function :tag "Function")))
1056
1057(defun org-agenda-format-date-aligned (date)
1058 "Format a date string for display in the daily/weekly agenda, or timeline.
1059This function makes sure that dates are aligned for easy reading."
1060 (require 'cal-iso)
1061 (let* ((dayname (calendar-day-name date))
1062 (day (cadr date))
1063 (day-of-week (calendar-day-of-week date))
1064 (month (car date))
1065 (monthname (calendar-month-name month))
1066 (year (nth 2 date))
1067 (iso-week (org-days-to-iso-week
1068 (calendar-absolute-from-gregorian date)))
1069 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1070 (1- year))
1071 ((and (= month 12) (<= iso-week 1))
1072 (1+ year))
1073 (t year)))
1074 (weekstring (if (= day-of-week 1)
1075 (format " W%02d" iso-week)
1076 "")))
1077 (format "%-10s %2d %s %4d%s"
1078 dayname day monthname year weekstring)))
1079
ed21c5c8
CD
1080(defcustom org-agenda-time-leading-zero nil
1081 "Non-nil means use leading zero for military times in agenda.
1082For example, 9:30am would become 09:30 rather than 9:30."
1083 :group 'org-agenda-daily/weekly
372d7b21 1084 :version "24.1"
ed21c5c8
CD
1085 :type 'boolean)
1086
acedf35c
CD
1087(defcustom org-agenda-timegrid-use-ampm nil
1088 "When set, show AM/PM style timestamps on the timegrid."
1089 :group 'org-agenda
372d7b21 1090 :version "24.1"
acedf35c
CD
1091 :type 'boolean)
1092
1093(defun org-agenda-time-of-day-to-ampm (time)
1094 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1095 (let* ((hour-number (string-to-number (substring time 0 -3)))
1096 (minute (substring time -2))
1097 (ampm "am"))
1098 (cond
1099 ((equal hour-number 12)
1100 (setq ampm "pm"))
1101 ((> hour-number 12)
1102 (setq ampm "pm")
1103 (setq hour-number (- hour-number 12))))
1104 (concat
1105 (if org-agenda-time-leading-zero
1106 (format "%02d" hour-number)
1107 (format "%02s" (number-to-string hour-number)))
1108 ":" minute ampm)))
1109
1110(defun org-agenda-time-of-day-to-ampm-maybe (time)
1111 "Conditionally convert TIME to AM/PM format
1112based on `org-agenda-timegrid-use-ampm'"
1113 (if org-agenda-timegrid-use-ampm
1114 (org-agenda-time-of-day-to-ampm time)
1115 time))
1116
20908596
CD
1117(defcustom org-agenda-weekend-days '(6 0)
1118 "Which days are weekend?
1119These days get the special face `org-agenda-date-weekend' in the agenda
1120and timeline buffers."
1121 :group 'org-agenda-daily/weekly
1122 :type '(set :greedy t
1123 (const :tag "Monday" 1)
1124 (const :tag "Tuesday" 2)
1125 (const :tag "Wednesday" 3)
1126 (const :tag "Thursday" 4)
1127 (const :tag "Friday" 5)
1128 (const :tag "Saturday" 6)
1129 (const :tag "Sunday" 0)))
1130
e66ba1df 1131(defcustom org-agenda-move-date-from-past-immediately-to-today t
27e428e7 1132 "Non-nil means jump to today when moving a past date forward in time.
e66ba1df
BG
1133When using S-right in the agenda to move a a date forward, and the date
1134stamp currently points to the past, the first key press will move it
1135to today. WHen nil, just move one day forward even if the date stays
1136in the past."
1137 :group 'org-agenda-daily/weekly
372d7b21 1138 :version "24.1"
e66ba1df
BG
1139 :type 'boolean)
1140
20908596 1141(defcustom org-agenda-include-diary nil
c8d0cf5c
CD
1142 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1143Custom commands can set this variable in the options section."
20908596
CD
1144 :group 'org-agenda-daily/weekly
1145 :type 'boolean)
1146
ed21c5c8
CD
1147(defcustom org-agenda-include-deadlines t
1148 "If non-nil, include entries within their deadline warning period.
1149Custom commands can set this variable in the options section."
1150 :group 'org-agenda-daily/weekly
372d7b21 1151 :version "24.1"
ed21c5c8
CD
1152 :type 'boolean)
1153
20908596 1154(defcustom org-agenda-repeating-timestamp-show-all t
ed21c5c8 1155 "Non-nil means show all occurrences of a repeating stamp in the agenda.
3ab2c837
BG
1156When set to a list of strings, only show occurrences of repeating
1157stamps for these TODO keywords. When nil, only one occurrence is
1158shown, either today or the nearest into the future."
20908596 1159 :group 'org-agenda-daily/weekly
3ab2c837
BG
1160 :type '(choice
1161 (const :tag "Show repeating stamps" t)
1162 (repeat :tag "Show repeating stamps for these TODO keywords"
1163 (string :tag "TODO Keyword"))
1164 (const :tag "Don't show repeating stamps" nil)))
20908596
CD
1165
1166(defcustom org-scheduled-past-days 10000
1167 "No. of days to continue listing scheduled items that are not marked DONE.
1168When an item is scheduled on a date, it shows up in the agenda on this
1169day and will be listed until it is marked done for the number of days
1170given here."
1171 :group 'org-agenda-daily/weekly
c8d0cf5c 1172 :type 'integer)
20908596 1173
93b62de8
CD
1174(defcustom org-agenda-log-mode-items '(closed clock)
1175 "List of items that should be shown in agenda log mode.
1176This list may contain the following symbols:
1177
1178 closed Show entries that have been closed on that day.
1179 clock Show entries that have received clocked time on that day.
c8d0cf5c
CD
1180 state Show all logged state changes.
1181Note that instead of changing this variable, you can also press `C-u l' in
1182the agenda to display all available LOG items temporarily."
93b62de8
CD
1183 :group 'org-agenda-daily/weekly
1184 :type '(set :greedy t (const closed) (const clock) (const state)))
1185
3ab2c837
BG
1186(defcustom org-agenda-clock-consistency-checks
1187 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1188 :gap-ok-around ("4:00")
1189 :default-face ((:background "DarkRed") (:foreground "white"))
1190 :overlap-face nil :gap-face nil :no-end-time-face nil
1191 :long-face nil :short-face nil)
1192 "This is a property list, with the following keys:
1193
1194:max-duration Mark clocking chunks that are longer than this time.
1195 This is a time string like \"HH:MM\", or the number
1196 of minutes as an integer.
1197
1198:min-duration Mark clocking chunks that are shorter that this.
1199 This is a time string like \"HH:MM\", or the number
1200 of minutes as an integer.
1201
1202:max-gap Mark gaps between clocking chunks that are longer than
1203 this duration. A number of minutes, or a string
1204 like \"HH:MM\".
1205
1206:gap-ok-around List of times during the day which are usually not working
1207 times. When a gap is detected, but the gap contains any
1208 of these times, the gap is *not* reported. For example,
1209 if this is (\"4:00\" \"13:00\") then gaps that contain
1210 4:00 in the morning (i.e. the night) and 13:00
1211 (i.e. a typical lunch time) do not cause a warning.
1212 You should have at least one time during the night in this
1213 list, or otherwise the first task each morning will trigger
1214 a warning because it follows a long gap.
1215
1216Furthermore, the following properties can be used to define faces for
1217issue display.
1218
1219:default-face the default face, if the specific face is undefined
1220:overlap-face face for overlapping clocks
1221:gap-face face for gaps between clocks
1222:no-end-time-face face for incomplete clocks
1223:long-face face for clock intervals that are too long
1224:short-face face for clock intervals that are too short"
1225 :group 'org-agenda-daily/weekly
1226 :group 'org-clock
372d7b21 1227 :version "24.1"
3ab2c837
BG
1228 :type 'plist)
1229
c8d0cf5c 1230(defcustom org-agenda-log-mode-add-notes t
ed21c5c8 1231 "Non-nil means add first line of notes to log entries in agenda views.
c8d0cf5c
CD
1232If a log item like a state change or a clock entry is associated with
1233notes, the first line of these notes will be added to the entry in the
1234agenda display."
1235 :group 'org-agenda-daily/weekly
1236 :type 'boolean)
1237
1238(defcustom org-agenda-start-with-log-mode nil
8223b1d2
BG
1239 "The initial value of log-mode in a newly created agenda window.
1240See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1241explanations on the possible values."
c8d0cf5c
CD
1242 :group 'org-agenda-startup
1243 :group 'org-agenda-daily/weekly
8223b1d2
BG
1244 :type '(choice (const :tag "Don't show log items" nil)
1245 (const :tag "Show only log items" 'only)
1246 (const :tag "Show all possible log items" 'clockcheck)
1247 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1248 (choice (const :tag "Show closed log items" 'closed)
1249 (const :tag "Show clocked log items" 'clock)
1250 (const :tag "Show all logged state changes" 'state)))))
c8d0cf5c 1251
20908596
CD
1252(defcustom org-agenda-start-with-clockreport-mode nil
1253 "The initial value of clockreport-mode in a newly created agenda window."
1254 :group 'org-agenda-startup
1255 :group 'org-agenda-daily/weekly
1256 :type 'boolean)
1257
1258(defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1259 "Property list with parameters for the clocktable in clockreport mode.
1260This is the display mode that shows a clock table in the daily/weekly
1261agenda, the properties for this dynamic block can be set here.
1262The usual clocktable parameters are allowed here, but you cannot set
1263the properties :name, :tstart, :tend, :block, and :scope - these will
1264be overwritten to make sure the content accurately reflects the
1265current display in the agenda."
1266 :group 'org-agenda-daily/weekly
1267 :type 'plist)
1268
ed21c5c8
CD
1269(defcustom org-agenda-search-view-always-boolean nil
1270 "Non-nil means the search string is interpreted as individual parts.
1271
1272The search string for search view can either be interpreted as a phrase,
1273or as a list of snippets that define a boolean search for a number of
1274strings.
1275
1276When this is non-nil, the string will be split on whitespace, and each
1277snippet will be searched individually, and all must match in order to
1278select an entry. A snippet is then a single string of non-white
1279characters, or a string in double quotes, or a regexp in {} braces.
86fbb8ca 1280If a snippet is preceded by \"-\", the snippet must *not* match.
ed21c5c8
CD
1281\"+\" is syntactic sugar for positive selection. Each snippet may
1282be found as a full word or a partial word, but see the variable
1283`org-agenda-search-view-force-full-words'.
1284
1285When this is nil, search will look for the entire search phrase as one,
1286with each space character matching any amount of whitespace, including
1287line breaks.
1288
1289Even when this is nil, you can still switch to Boolean search dynamically
86fbb8ca 1290by preceding the first snippet with \"+\" or \"-\". If the first snippet
ed21c5c8
CD
1291is a regexp marked with braces like \"{abc}\", this will also switch to
1292boolean search."
1293 :group 'org-agenda-search-view
372d7b21 1294 :version "24.1"
ed21c5c8
CD
1295 :type 'boolean)
1296
1297(if (fboundp 'defvaralias)
1298 (defvaralias 'org-agenda-search-view-search-words-only
1299 'org-agenda-search-view-always-boolean))
1300
1301(defcustom org-agenda-search-view-force-full-words nil
86fbb8ca 1302 "Non-nil means, search words must be matches as complete words.
ed21c5c8 1303When nil, they may also match part of a word."
8bfe682a 1304 :group 'org-agenda-search-view
372d7b21 1305 :version "24.1"
8bfe682a 1306 :type 'boolean)
20908596
CD
1307
1308(defgroup org-agenda-time-grid nil
1309 "Options concerning the time grid in the Org-mode Agenda."
1310 :tag "Org Agenda Time Grid"
1311 :group 'org-agenda)
1312
c8d0cf5c 1313(defcustom org-agenda-search-headline-for-time t
ed21c5c8 1314 "Non-nil means search headline for a time-of-day.
c8d0cf5c
CD
1315If the headline contains a time-of-day in one format or another, it will
1316be used to sort the entry into the time sequence of items for a day.
1317Some people have time stamps in the headline that refer to the creation
1318time or so, and then this produces an unwanted side effect. If this is
1319the case for your, use this variable to turn off searching the headline
1320for a time."
1321 :group 'org-agenda-time-grid
1322 :type 'boolean)
1323
20908596 1324(defcustom org-agenda-use-time-grid t
ed21c5c8 1325 "Non-nil means show a time grid in the agenda schedule.
20908596
CD
1326A time grid is a set of lines for specific times (like every two hours between
13278:00 and 20:00). The items scheduled for a day at specific times are
1328sorted in between these lines.
1329For details about when the grid will be shown, and what it will look like, see
1330the variable `org-agenda-time-grid'."
1331 :group 'org-agenda-time-grid
1332 :type 'boolean)
1333
1334(defcustom org-agenda-time-grid
1335 '((daily today require-timed)
1336 "----------------"
1337 (800 1000 1200 1400 1600 1800 2000))
1338
1339 "The settings for time grid for agenda display.
1340This is a list of three items. The first item is again a list. It contains
1341symbols specifying conditions when the grid should be displayed:
1342
1343 daily if the agenda shows a single day
1344 weekly if the agenda shows an entire week
1345 today show grid on current date, independent of daily/weekly display
1346 require-timed show grid only if at least one item has a time specification
1347
b349f79f 1348The second item is a string which will be placed behind the grid time.
20908596
CD
1349
1350The third item is a list of integers, indicating the times that should have
1351a grid line."
1352 :group 'org-agenda-time-grid
1353 :type
1354 '(list
1355 (set :greedy t :tag "Grid Display Options"
1356 (const :tag "Show grid in single day agenda display" daily)
1357 (const :tag "Show grid in weekly agenda display" weekly)
1358 (const :tag "Always show grid for today" today)
1359 (const :tag "Show grid only if any timed entries are present"
1360 require-timed)
1361 (const :tag "Skip grid times already present in an entry"
1362 remove-match))
1363 (string :tag "Grid String")
1364 (repeat :tag "Grid Times" (integer :tag "Time"))))
1365
3ab2c837
BG
1366(defcustom org-agenda-show-current-time-in-grid t
1367 "Non-nil means show the current time in the time grid."
1368 :group 'org-agenda-time-grid
372d7b21 1369 :version "24.1"
3ab2c837
BG
1370 :type 'boolean)
1371
1372(defcustom org-agenda-current-time-string
1373 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1374 "The string for the current time marker in the agenda."
1375 :group 'org-agenda-time-grid
372d7b21 1376 :version "24.1"
3ab2c837
BG
1377 :type 'string)
1378
20908596
CD
1379(defgroup org-agenda-sorting nil
1380 "Options concerning sorting in the Org-mode Agenda."
1381 :tag "Org Agenda Sorting"
1382 :group 'org-agenda)
1383
1384(defcustom org-agenda-sorting-strategy
8bfe682a
CD
1385 '((agenda habit-down time-up priority-down category-keep)
1386 (todo priority-down category-keep)
1387 (tags priority-down category-keep)
20908596
CD
1388 (search category-keep))
1389 "Sorting structure for the agenda items of a single day.
1390This is a list of symbols which will be used in sequence to determine
1391if an entry should be listed before another entry. The following
1392symbols are recognized:
1393
c8d0cf5c
CD
1394time-up Put entries with time-of-day indications first, early first
1395time-down Put entries with time-of-day indications first, late first
1396category-keep Keep the default order of categories, corresponding to the
1397 sequence in `org-agenda-files'.
1398category-up Sort alphabetically by category, A-Z.
1399category-down Sort alphabetically by category, Z-A.
1400tag-up Sort alphabetically by last tag, A-Z.
1401tag-down Sort alphabetically by last tag, Z-A.
1402priority-up Sort numerically by priority, high priority last.
1403priority-down Sort numerically by priority, high priority first.
1404todo-state-up Sort by todo state, tasks that are done last.
1405todo-state-down Sort by todo state, tasks that are done first.
1406effort-up Sort numerically by estimated effort, high effort last.
1407effort-down Sort numerically by estimated effort, high effort first.
1408user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1409user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
8bfe682a
CD
1410habit-up Put entries that are habits first
1411habit-down Put entries that are habits last
86fbb8ca
CD
1412alpha-up Sort headlines alphabetically
1413alpha-down Sort headlines alphabetically, reversed
20908596
CD
1414
1415The different possibilities will be tried in sequence, and testing stops
1416if one comparison returns a \"not-equal\". For example, the default
1417 '(time-up category-keep priority-down)
1418means: Pull out all entries having a specified time of day and sort them,
1419in order to make a time schedule for the current day the first thing in the
1420agenda listing for the day. Of the entries without a time indication, keep
1421the grouped in categories, don't sort the categories, but keep them in
1422the sequence given in `org-agenda-files'. Within each category sort by
1423priority.
1424
1425Leaving out `category-keep' would mean that items will be sorted across
1426categories by priority.
1427
1428Instead of a single list, this can also be a set of list for specific
1429contents, with a context symbol in the car of the list, any of
8bfe682a 1430`agenda', `todo', `tags', `search' for the corresponding agenda views.
c8d0cf5c
CD
1431
1432Custom commands can bind this variable in the options section."
20908596
CD
1433 :group 'org-agenda-sorting
1434 :type `(choice
1435 (repeat :tag "General" ,org-sorting-choice)
1436 (list :tag "Individually"
1437 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1438 (repeat ,org-sorting-choice))
1439 (cons (const :tag "Strategy for TODO lists" todo)
1440 (repeat ,org-sorting-choice))
1441 (cons (const :tag "Strategy for Tags matches" tags)
8bfe682a
CD
1442 (repeat ,org-sorting-choice))
1443 (cons (const :tag "Strategy for search matches" search)
20908596
CD
1444 (repeat ,org-sorting-choice)))))
1445
c8d0cf5c
CD
1446(defcustom org-agenda-cmp-user-defined nil
1447 "A function to define the comparison `user-defined'.
1448This function must receive two arguments, agenda entry a and b.
1449If a>b, return +1. If a<b, return -1. If they are equal as seen by
1450the user comparison, return nil.
1451When this is defined, you can make `user-defined-up' and `user-defined-down'
1452part of an agenda sorting strategy."
1453 :group 'org-agenda-sorting
1454 :type 'symbol)
1455
20908596 1456(defcustom org-sort-agenda-notime-is-late t
ed21c5c8 1457 "Non-nil means items without time are considered late.
20908596
CD
1458This is only relevant for sorting. When t, items which have no explicit
1459time like 15:30 will be considered as 99:01, i.e. later than any items which
1460do have a time. When nil, the default time is before 0:00. You can use this
1461option to decide if the schedule for today should come before or after timeless
1462agenda entries."
1463 :group 'org-agenda-sorting
1464 :type 'boolean)
1465
1466(defcustom org-sort-agenda-noeffort-is-high t
ed21c5c8 1467 "Non-nil means items without effort estimate are sorted as high effort.
c8d0cf5c
CD
1468This also applies when filtering an agenda view with respect to the
1469< or > effort operator. Then, tasks with no effort defined will be treated
1470as tasks with high effort.
20908596
CD
1471When nil, such items are sorted as 0 minutes effort."
1472 :group 'org-agenda-sorting
1473 :type 'boolean)
1474
1475(defgroup org-agenda-line-format nil
1476 "Options concerning the entry prefix in the Org-mode agenda display."
1477 :tag "Org Agenda Line Format"
1478 :group 'org-agenda)
1479
1480(defcustom org-agenda-prefix-format
acedf35c 1481 '((agenda . " %i %-12:c%?-12t% s")
20908596 1482 (timeline . " % s")
acedf35c
CD
1483 (todo . " %i %-12:c")
1484 (tags . " %i %-12:c")
1485 (search . " %i %-12:c"))
20908596 1486 "Format specifications for the prefix of items in the agenda views.
fe3c5669
PE
1487An alist with five entries, each for the different agenda types. The
1488keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
3ab2c837
BG
1489The values are format strings.
1490
20908596
CD
1491This format works similar to a printf format, with the following meaning:
1492
fe3c5669 1493 %c the category of the item, \"Diary\" for entries from the diary,
3ab2c837 1494 or as given by the CATEGORY keyword or derived from the file name
e66ba1df 1495 %e the effort required by the item
3ab2c837
BG
1496 %i the icon category of the item, see `org-agenda-category-icon-alist'
1497 %T the last tag of the item (ignore inherited tags, which come first)
1498 %t the HH:MM time-of-day specification if one applies to the entry
20908596 1499 %s Scheduling/Deadline information, a short string
3ab2c837
BG
1500 %(expression) Eval EXPRESSION and replace the control string
1501 by the result
20908596
CD
1502
1503All specifiers work basically like the standard `%s' of printf, but may
3ab2c837
BG
1504contain two additional characters: a question mark just after the `%'
1505and a whitespace/punctuation character just before the final letter.
20908596
CD
1506
1507If the first character after `%' is a question mark, the entire field
fe3c5669
PE
1508will only be included if the corresponding value applies to the current
1509entry. This is useful for fields which should have fixed width when
1510present, but zero width when absent. For example, \"%?-12t\" will
1511result in a 12 character time field if a time of the day is specified,
3ab2c837 1512but will completely disappear in entries which do not contain a time.
20908596
CD
1513
1514If there is punctuation or whitespace character just before the final
1515format letter, this character will be appended to the field value if
1516the value is not empty. For example, the format \"%-12:c\" leads to
1517\"Diary: \" if the category is \"Diary\". If the category were be
8bfe682a 1518empty, no additional colon would be inserted.
20908596 1519
fe3c5669 1520The default value for the agenda sublist is \" %-12:c%?-12t% s\",
3ab2c837
BG
1521which means:
1522
20908596 1523- Indent the line with two space characters
3ab2c837 1524- Give the category a 12 chars wide field, padded with whitespace on
20908596
CD
1525 the right (because of `-'). Append a colon if there is a category
1526 (because of `:').
1527- If there is a time-of-day, put it into a 12 chars wide field. If no
1528 time, don't put in an empty field, just skip it (because of '?').
3ab2c837 1529- Finally, put the scheduling information.
20908596
CD
1530
1531See also the variables `org-agenda-remove-times-when-in-prefix' and
c8d0cf5c
CD
1532`org-agenda-remove-tags'.
1533
1534Custom commands can set this variable in the options section."
20908596
CD
1535 :type '(choice
1536 (string :tag "General format")
1537 (list :greedy t :tag "View dependent"
1538 (cons (const agenda) (string :tag "Format"))
1539 (cons (const timeline) (string :tag "Format"))
1540 (cons (const todo) (string :tag "Format"))
1541 (cons (const tags) (string :tag "Format"))
1542 (cons (const search) (string :tag "Format"))))
1543 :group 'org-agenda-line-format)
1544
1545(defvar org-prefix-format-compiled nil
8223b1d2
BG
1546 "The compiled prefix format and associated variables.
1547This is a list where first element is a list of variable bindings, and second
1548element is the compiled format expression. See the variable
1549`org-agenda-prefix-format'.")
20908596
CD
1550
1551(defcustom org-agenda-todo-keyword-format "%-1s"
1552 "Format for the TODO keyword in agenda lines.
1553Set this to something like \"%-12s\" if you want all TODO keywords
1554to occupy a fixed space in the agenda display."
1555 :group 'org-agenda-line-format
1556 :type 'string)
1557
8223b1d2
BG
1558(defcustom org-agenda-diary-sexp-prefix nil
1559 "A regexp that matches part of a diary sexp entry
1560which should be treated as scheduling/deadline information in
1561`org-agenda'.
1562
1563For example, you can use this to extract the `diary-remind-message' from
1564`diary-remind' entries."
1565 :group 'org-agenda-line-format
1566 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1567
ce4fdcb9
CD
1568(defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1569 "Text preceding timerange entries in the agenda view.
1570This is a list with two strings. The first applies when the range
1571is entirely on one day. The second applies if the range spans several days.
1572The strings may have two \"%d\" format specifiers which will be filled
1573with the sequence number of the days, and the total number of days in the
1574range, respectively."
1575 :group 'org-agenda-line-format
1576 :type '(list
1577 (string :tag "Deadline today ")
1578 (choice :tag "Deadline relative"
1579 (string :tag "Format string")
1580 (function))))
1581
20908596 1582(defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
86fbb8ca 1583 "Text preceding scheduled items in the agenda view.
20908596
CD
1584This is a list with two strings. The first applies when the item is
1585scheduled on the current day. The second applies when it has been scheduled
b349f79f
CD
1586previously, it may contain a %d indicating that this is the nth time that
1587this item is scheduled, due to automatic rescheduling of unfinished items
1588for the following day. So this number is one larger than the number of days
1589that passed since this item was scheduled first."
20908596
CD
1590 :group 'org-agenda-line-format
1591 :type '(list
1592 (string :tag "Scheduled today ")
1593 (string :tag "Scheduled previously")))
1594
ed21c5c8 1595(defcustom org-agenda-inactive-leader "["
86fbb8ca 1596 "Text preceding item pulled into the agenda by inactive time stamps.
ed21c5c8
CD
1597These entries are added to the agenda when pressing \"[\"."
1598 :group 'org-agenda-line-format
372d7b21 1599 :version "24.1"
ed21c5c8
CD
1600 :type '(list
1601 (string :tag "Scheduled today ")
1602 (string :tag "Scheduled previously")))
1603
20908596 1604(defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
86fbb8ca 1605 "Text preceding deadline items in the agenda view.
20908596
CD
1606This is a list with two strings. The first applies when the item has its
1607deadline on the current day. The second applies when it is in the past or
1608in the future, it may contain %d to capture how many days away the deadline
1609is (was)."
1610 :group 'org-agenda-line-format
1611 :type '(list
1612 (string :tag "Deadline today ")
1613 (choice :tag "Deadline relative"
1614 (string :tag "Format string")
1615 (function))))
1616
1617(defcustom org-agenda-remove-times-when-in-prefix t
ed21c5c8 1618 "Non-nil means remove duplicate time specifications in agenda items.
20908596
CD
1619When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1620time-of-day specification in a headline or diary entry is extracted and
1621placed into the prefix. If this option is non-nil, the original specification
1622\(a timestamp or -range, or just a plain time(range) specification like
162311:30-4pm) will be removed for agenda display. This makes the agenda less
1624cluttered.
1625The option can be t or nil. It may also be the symbol `beg', indicating
86fbb8ca 1626that the time should only be removed when it is located at the beginning of
20908596
CD
1627the headline/diary entry."
1628 :group 'org-agenda-line-format
1629 :type '(choice
1630 (const :tag "Always" t)
1631 (const :tag "Never" nil)
1632 (const :tag "When at beginning of entry" beg)))
1633
86fbb8ca
CD
1634(defcustom org-agenda-remove-timeranges-from-blocks nil
1635 "Non-nil means remove time ranges specifications in agenda
1636items that span on several days."
1637 :group 'org-agenda-line-format
372d7b21 1638 :version "24.1"
86fbb8ca 1639 :type 'boolean)
20908596
CD
1640
1641(defcustom org-agenda-default-appointment-duration nil
1642 "Default duration for appointments that only have a starting time.
1643When nil, no duration is specified in such cases.
1644When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1645 :group 'org-agenda-line-format
1646 :type '(choice
1647 (integer :tag "Minutes")
1648 (const :tag "No default duration")))
1649
ff4be292 1650(defcustom org-agenda-show-inherited-tags t
ed21c5c8 1651 "Non-nil means show inherited tags in each agenda line."
ff4be292
CD
1652 :group 'org-agenda-line-format
1653 :type 'boolean)
20908596 1654
c7cf0ebc
BG
1655(defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1656 "List of agenda view types where to use tag inheritance.
1657
1658In tags/tags-todo/tags-tree agenda views, tag inheritance is
1659controlled by `org-use-tag-inheritance'. In other agenda types,
1660`org-use-tag-inheritance' is not used when selecting the agenda
1661entries, but you may want the agenda to use the inherited tags
1662anyway, e.g. for later tag filtering.
1663
1664The default value reset tags in every agenda type. Setting this
1665option to nil will speed up non-tags agenda view a lot.
1666
1667Allowed value are 'todo, 'search, 'timeline and 'agenda."
1668 :version "24.3"
1669 :group 'org-agenda
1670 :type '(repeat (symbol :tag "Agenda type")))
1671
5dec9555
CD
1672(defcustom org-agenda-hide-tags-regexp nil
1673 "Regular expression used to filter away specific tags in agenda views.
1674This means that these tags will be present, but not be shown in the agenda
86fbb8ca 1675line. Secondary filtering will still work on the hidden tags.
afe98dfa 1676Nil means don't hide any tags."
5dec9555
CD
1677 :group 'org-agenda-line-format
1678 :type '(choice
1679 (const :tag "Hide none" nil)
1680 (string :tag "Regexp ")))
1681
20908596 1682(defcustom org-agenda-remove-tags nil
ed21c5c8 1683 "Non-nil means remove the tags from the headline copy in the agenda.
20908596
CD
1684When this is the symbol `prefix', only remove tags when
1685`org-agenda-prefix-format' contains a `%T' specifier."
1686 :group 'org-agenda-line-format
1687 :type '(choice
1688 (const :tag "Always" t)
1689 (const :tag "Never" nil)
1690 (const :tag "When prefix format contains %T" prefix)))
1691
1692(if (fboundp 'defvaralias)
1693 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1694 'org-agenda-remove-tags))
1695
5ace2fe5 1696(defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
20908596
CD
1697 "Shift tags in agenda items to this column.
1698If this number is positive, it specifies the column. If it is negative,
1699it means that the tags should be flushright to that column. For example,
1700-80 works well for a normal 80 character screen."
1701 :group 'org-agenda-line-format
1702 :type 'integer)
1703
1704(if (fboundp 'defvaralias)
1705 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1706
c8d0cf5c 1707(defcustom org-agenda-fontify-priorities 'cookies
ed21c5c8 1708 "Non-nil means highlight low and high priorities in agenda.
20908596 1709When t, the highest priority entries are bold, lowest priority italic.
86fbb8ca 1710However, settings in `org-priority-faces' will overrule these faces.
c8d0cf5c
CD
1711When this variable is the symbol `cookies', only fontify the
1712cookies, not the entire task.
621f83e4
CD
1713This may also be an association list of priority faces, whose
1714keys are the character values of `org-highest-priority',
1715`org-default-priority', and `org-lowest-priority' (the default values
ed21c5c8
CD
1716are ?A, ?B, and ?C, respectively). The face may be a named face, a
1717color as a string, or a list like `(:background \"Red\")'.
1718If it is a color, the variable `org-faces-easy-properties'
1719determines if it is a foreground or a background color."
20908596
CD
1720 :group 'org-agenda-line-format
1721 :type '(choice
1722 (const :tag "Never" nil)
1723 (const :tag "Defaults" t)
c8d0cf5c 1724 (const :tag "Cookies only" cookies)
20908596
CD
1725 (repeat :tag "Specify"
1726 (list (character :tag "Priority" :value ?A)
ed21c5c8
CD
1727 (choice :tag "Face "
1728 (string :tag "Color")
1729 (sexp :tag "Face"))))))
20908596 1730
acedf35c
CD
1731(defcustom org-agenda-day-face-function nil
1732 "Function called to determine what face should be used to display a day.
8223b1d2 1733The only argument passed to that function is the day. It should
acedf35c
CD
1734returns a face, or nil if does not want to specify a face and let
1735the normal rules apply."
1736 :group 'org-agenda-line-format
372d7b21 1737 :version "24.1"
acedf35c
CD
1738 :type 'function)
1739
1740(defcustom org-agenda-category-icon-alist nil
1741 "Alist of category icon to be displayed in agenda views.
1742
1743Each entry should have the following format:
1744
1745 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1746
1747Where CATEGORY-REGEXP is a regexp matching the categories where
1748the icon should be displayed.
1749FILE-OR-DATA either a file path or a string containing image data.
1750
27e428e7 1751The other fields can be omitted safely if not needed:
acedf35c
CD
1752TYPE indicates the image type.
1753DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1754image data.
1755PROPS are additional image attributes to assign to the image,
1756like, e.g. `:ascent center'.
1757
1758 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1759
1760If you want to set the display properties yourself, just put a
1761list as second element:
1762
1763 (CATEGORY-REGEXP (MY PROPERTY LIST))
1764
1765For example, to display a 16px horizontal space for Emacs
1766category, you can use:
1767
1768 (\"Emacs\" '(space . (:width (16))))"
1769 :group 'org-agenda-line-format
372d7b21 1770 :version "24.1"
acedf35c
CD
1771 :type '(alist :key-type (string :tag "Regexp matching category")
1772 :value-type (choice (list :tag "Icon"
1773 (string :tag "File or data")
1774 (symbol :tag "Type")
1775 (boolean :tag "Data?")
1776 (repeat :tag "Extra image properties" :inline t symbol))
1777 (list :tag "Display properties" sexp))))
1778
20908596
CD
1779(defgroup org-agenda-column-view nil
1780 "Options concerning column view in the agenda."
1781 :tag "Org Agenda Column View"
1782 :group 'org-agenda)
1783
1784(defcustom org-agenda-columns-show-summaries t
ed21c5c8 1785 "Non-nil means show summaries for columns displayed in the agenda view."
20908596
CD
1786 :group 'org-agenda-column-view
1787 :type 'boolean)
1788
1789(defcustom org-agenda-columns-compute-summary-properties t
ed21c5c8 1790 "Non-nil means recompute all summary properties before column view.
20908596
CD
1791When column view in the agenda is listing properties that have a summary
1792operator, it can go to all relevant buffers and recompute the summaries
1793there. This can mean overhead for the agenda column view, but is necessary
1794to have thing up to date.
1795As a special case, a CLOCKSUM property also makes sure that the clock
1796computations are current."
1797 :group 'org-agenda-column-view
1798 :type 'boolean)
1799
1800(defcustom org-agenda-columns-add-appointments-to-effort-sum nil
ed21c5c8 1801 "Non-nil means the duration of an appointment will add to day effort.
20908596
CD
1802The property to which appointment durations will be added is the one given
1803in the option `org-effort-property'. If an appointment does not have
1804an end time, `org-agenda-default-appointment-duration' will be used. If that
1805is not set, an appointment without end time will not contribute to the time
1806estimate."
1807 :group 'org-agenda-column-view
1808 :type 'boolean)
1809
8bfe682a
CD
1810(defcustom org-agenda-auto-exclude-function nil
1811 "A function called with a tag to decide if it is filtered on '/ RET'.
1812The sole argument to the function, which is called once for each
1813possible tag, is a string giving the name of the tag. The
1814function should return either nil if the tag should be included
ed21c5c8
CD
1815as normal, or \"-<TAG>\" to exclude the tag.
1816Note that for the purpose of tag filtering, only the lower-case version of
1817all tags will be considered, so that this function will only ever see
1818the lower-case version of all tags."
8bfe682a
CD
1819 :group 'org-agenda
1820 :type 'function)
1821
3ab2c837
BG
1822(defcustom org-agenda-bulk-custom-functions nil
1823 "Alist of characters and custom functions for bulk actions.
1824For example, this value makes those two functions available:
1825
1826 '((?R set-category)
1827 (?C bulk-cut))
1828
1829With selected entries in an agenda buffer, `B R' will call
fe3c5669 1830the custom function `set-category' on the selected entries.
3ab2c837
BG
1831Note that functions in this alist don't need to be quoted."
1832 :type 'alist
372d7b21 1833 :version "24.1"
3ab2c837
BG
1834 :group 'org-agenda)
1835
afe98dfa
CD
1836(defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1837 "Execute BODY with point at location given by `org-hd-marker' property.
1838If STRING is non-nil, the text property will be fetched from position 0
1839in that string. If STRING is nil, it will be fetched from the beginning
1840of the current line."
e66ba1df
BG
1841 (org-with-gensyms (marker)
1842 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1843 'org-hd-marker ,string)))
1844 (with-current-buffer (marker-buffer ,marker)
1845 (save-excursion
1846 (goto-char ,marker)
1847 ,@body)))))
1848(def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
afe98dfa 1849
20908596
CD
1850(defun org-add-agenda-custom-command (entry)
1851 "Replace or add a command in `org-agenda-custom-commands'.
1852This is mostly for hacking and trying a new command - once the command
1853works you probably want to add it to `org-agenda-custom-commands' for good."
1854 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1855 (if ass
1856 (setcdr ass (cdr entry))
1857 (push entry org-agenda-custom-commands))))
1858
8223b1d2 1859;;; Define the org-agenda-mode
20908596
CD
1860
1861(defvar org-agenda-mode-map (make-sparse-keymap)
1862 "Keymap for `org-agenda-mode'.")
8bfe682a
CD
1863(if (fboundp 'defvaralias)
1864 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
20908596
CD
1865
1866(defvar org-agenda-menu) ; defined later in this file.
8223b1d2 1867(defvar org-agenda-restrict nil) ; defined later in this file.
20908596 1868(defvar org-agenda-follow-mode nil)
54a0dee5 1869(defvar org-agenda-entry-text-mode nil)
20908596
CD
1870(defvar org-agenda-clockreport-mode nil)
1871(defvar org-agenda-show-log nil)
1872(defvar org-agenda-redo-command nil)
1873(defvar org-agenda-query-string nil)
0bd48b37 1874(defvar org-agenda-mode-hook nil
8223b1d2
BG
1875 "Hook run after `org-agenda-mode' is turned on.
1876The buffer is still writable when this hook is called.")
20908596
CD
1877(defvar org-agenda-type nil)
1878(defvar org-agenda-force-single-file nil)
8223b1d2
BG
1879(defvar org-agenda-bulk-marked-entries nil
1880 "List of markers that refer to marked entries in the agenda.")
1881
1882;;; Multiple agenda buffers support
1883
1884(defcustom org-agenda-sticky nil
1885 "Non-nil means agenda q key will bury agenda buffers.
1886Agenda commands will then show existing buffer instead of generating new ones.
1887When nil, `q' will kill the single agenda buffer."
1888 :group 'org-agenda
1889 :version "24.3"
1890 :type 'boolean)
1891
bdebdb64 1892\f
8223b1d2
BG
1893;;;###autoload
1894(defun org-toggle-sticky-agenda (&optional arg)
1895 "Toggle `org-agenda-sticky'."
1896 (interactive "P")
1897 (let ((new-value (if arg
1898 (> (prefix-numeric-value arg) 0)
1899 (not org-agenda-sticky))))
1900 (if (equal new-value org-agenda-sticky)
1901 (and (org-called-interactively-p 'interactive)
1902 (message "Sticky agenda was already %s"
1903 (if org-agenda-sticky "enabled" "disabled")))
1904 (setq org-agenda-sticky new-value)
1905 (org-agenda-kill-all-agenda-buffers)
1906 (and (org-called-interactively-p 'interactive)
1907 (message "Sticky agenda was %s"
1908 (if org-agenda-sticky "enabled" "disabled"))))))
1909
bdebdb64
BG
1910(autoload 'org-toggle-sticky-agenda "org-agenda" "\
1911Toggle `org-agenda-sticky'.
1912
1913\(fn &optional ARG)" t nil)
1914
8223b1d2
BG
1915(defvar org-agenda-buffer nil
1916 "Agenda buffer currently being generated.")
1917
1918(defvar org-agenda-last-prefix-arg nil)
1919(defvar org-agenda-this-buffer-name nil)
1920(defvar org-agenda-doing-sticky-redo nil)
1921(defvar org-agenda-this-buffer-is-sticky nil)
1922
1923(defconst org-agenda-local-vars
1924 '(org-agenda-this-buffer-name
1925 org-agenda-undo-list
1926 org-agenda-pending-undo-list
1927 org-agenda-follow-mode
1928 org-agenda-entry-text-mode
1929 org-agenda-clockreport-mode
1930 org-agenda-show-log
1931 org-agenda-redo-command
1932 org-agenda-query-string
1933 org-agenda-type
1934 org-agenda-bulk-marked-entries
1935 org-agenda-undo-has-started-in
1936 org-agenda-info
1937 org-agenda-tag-filter-overlays
1938 org-agenda-cat-filter-overlays
1939 org-agenda-pre-window-conf
1940 org-agenda-columns-active
1941 org-agenda-tag-filter
1942 org-agenda-category-filter
1943 org-agenda-markers
1944 org-agenda-last-search-view-search-was-boolean
1945 org-agenda-filtered-by-category
1946 org-agenda-filter-form
1947 org-agenda-show-window
1948 org-agenda-cycle-counter
1949 org-agenda-last-prefix-arg)
1950 "Variables that must be local in agenda buffers to allow multiple buffers.")
20908596
CD
1951
1952(defun org-agenda-mode ()
1953 "Mode for time-sorted view on action items in Org-mode files.
1954
1955The following commands are available:
1956
1957\\{org-agenda-mode-map}"
1958 (interactive)
8223b1d2
BG
1959 (cond (org-agenda-doing-sticky-redo
1960 ;; Refreshing sticky agenda-buffer
1961 ;;
1962 ;; Preserve the value of `org-agenda-local-vars' variables,
1963 ;; while letting `kill-all-local-variables' kill the rest
1964 (let ((save (buffer-local-variables)))
1965 (kill-all-local-variables)
1966 (mapc 'make-local-variable org-agenda-local-vars)
1967 (dolist (elem save)
1968 (let ((var (car elem))
1969 (val (cdr elem)))
1970 (when (and val
1971 (member var org-agenda-local-vars))
1972 (set var val)))))
1973 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1974 (org-agenda-sticky
1975 ;; Creating a sticky Agenda buffer for the first time
1976 (kill-all-local-variables)
1977 (mapc 'make-local-variable org-agenda-local-vars)
1978 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1979 (t
1980 ;; Creating a non-sticky agenda buffer
1981 (kill-all-local-variables)
1982 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
20908596 1983 (setq org-agenda-undo-list nil
c8d0cf5c
CD
1984 org-agenda-pending-undo-list nil
1985 org-agenda-bulk-marked-entries nil)
20908596
CD
1986 (setq major-mode 'org-agenda-mode)
1987 ;; Keep global-font-lock-mode from turning on font-lock-mode
1988 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1989 (setq mode-name "Org-Agenda")
1990 (use-local-map org-agenda-mode-map)
1991 (easy-menu-add org-agenda-menu)
1992 (if org-startup-truncated (setq truncate-lines t))
54a0dee5 1993 (org-set-local 'line-move-visual nil)
8223b1d2 1994 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
20908596
CD
1995 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1996 ;; Make sure properties are removed when copying text
8223b1d2
BG
1997 (make-local-variable 'filter-buffer-substring-functions)
1998 (add-hook 'filter-buffer-substring-functions
1999 (lambda (fun start end delete)
2000 (substring-no-properties (funcall fun start end delete))))
20908596
CD
2001 (unless org-agenda-keep-modes
2002 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
54a0dee5 2003 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
20908596 2004 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
c8d0cf5c
CD
2005 org-agenda-show-log org-agenda-start-with-log-mode))
2006
20908596
CD
2007 (easy-menu-change
2008 '("Agenda") "Agenda Files"
2009 (append
2010 (list
2011 (vector
2012 (if (get 'org-agenda-files 'org-restrict)
2013 "Restricted to single file"
2014 "Edit File List")
2015 '(org-edit-agenda-file-list)
2016 (not (get 'org-agenda-files 'org-restrict)))
2017 "--")
2018 (mapcar 'org-file-menu-entry (org-agenda-files))))
2019 (org-agenda-set-mode-name)
2020 (apply
2021 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2022 (list 'org-agenda-mode-hook)))
2023
2024(substitute-key-definition 'undo 'org-agenda-undo
2025 org-agenda-mode-map global-map)
2026(org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2027(org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2028(org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2029(org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
c8d0cf5c
CD
2030(org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2031(org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
8223b1d2 2032(org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
c7cf0ebc 2033(org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
3ab2c837 2034(org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
c8d0cf5c 2035(org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
8223b1d2 2036(org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
c8d0cf5c 2037(org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
8223b1d2
BG
2038(org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2039(org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
c8d0cf5c 2040(org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
8bfe682a
CD
2041(org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2042(org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2043(org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
54a0dee5 2044(org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
8bfe682a 2045(org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
20908596 2046(org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
20908596 2047(org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
8bfe682a
CD
2048(org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2049(org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2050(org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
20908596
CD
2051(org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2052(org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2053(org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
20908596
CD
2054(org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2055(org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
54a0dee5 2056(org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
20908596 2057(org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
8bfe682a 2058(org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
20908596 2059(org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
71d35b24 2060(org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
20908596
CD
2061(org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2062(org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2063(org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2064(org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
20908596
CD
2065(org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2066(org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2067(org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
c8d0cf5c
CD
2068(org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2069(org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2070(org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2071(org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
20908596
CD
2072
2073(org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2074(org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2075(org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2076(let ((l '(1 2 3 4 5 6 7 8 9 0)))
2077 (while l (org-defkey org-agenda-mode-map
8223b1d2 2078 (int-to-string (pop l)) 'digit-argument)))
20908596 2079
54a0dee5 2080(org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
20908596 2081(org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
54a0dee5 2082(org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
20908596 2083(org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
c8d0cf5c 2084(org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
20908596 2085(org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
ed21c5c8 2086(org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
20908596
CD
2087(org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2088(org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
8223b1d2 2089(org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
54a0dee5
CD
2090(org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2091(org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2092(org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2093 'org-clock-modify-effort-estimate)
2094(org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
20908596 2095(org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
8223b1d2 2096(org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
20908596 2097(org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
e66ba1df 2098(org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
20908596 2099(org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
c8d0cf5c 2100(org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
20908596 2101(org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
8bfe682a
CD
2102(org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2103(org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
8223b1d2
BG
2104(org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2105(org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
8bfe682a
CD
2106(substitute-key-definition 'next-line 'org-agenda-next-line
2107 org-agenda-mode-map global-map)
2108(substitute-key-definition 'previous-line 'org-agenda-previous-line
2109 org-agenda-mode-map global-map)
621f83e4 2110(org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
20908596
CD
2111(org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2112(org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
20908596 2113(org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
8223b1d2 2114(org-defkey org-agenda-mode-map "," 'org-agenda-priority)
20908596
CD
2115(org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2116(org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2117(org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2118(org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2119(org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2120(org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2121(org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2122(org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2123(org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2124(org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2125(org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2126(org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2127(org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2128(org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
afe98dfa 2129(org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
20908596
CD
2130(org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2131(org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2132(org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2133(org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2134(org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2135(org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
54a0dee5
CD
2136(org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2137(org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
20908596 2138(org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
c8d0cf5c 2139(org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
20908596
CD
2140
2141(org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2142(org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2143(org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2144(org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
621f83e4 2145(org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
71d35b24 2146(org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
e66ba1df 2147(org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
8223b1d2 2148(org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-category)
c8d0cf5c 2149(org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
8d642074
CD
2150(define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2151(org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2152(org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
20908596 2153
86fbb8ca
CD
2154(org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2155(org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
20908596 2156(when org-agenda-mouse-1-follows-link
8bfe682a 2157 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
20908596
CD
2158(easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2159 '("Agenda"
2160 ("Agenda Files")
2161 "--"
8d642074
CD
2162 ("Agenda Dates"
2163 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2164 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2165 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2166 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2167 "--"
2168 ("View"
2169 ["Day View" org-agenda-day-view
2170 :active (org-agenda-check-type nil 'agenda)
acedf35c 2171 :style radio :selected (eq org-agenda-current-span 'day)
8d642074
CD
2172 :keys "v d (or just d)"]
2173 ["Week View" org-agenda-week-view
2174 :active (org-agenda-check-type nil 'agenda)
acedf35c 2175 :style radio :selected (eq org-agenda-current-span 'week)
8d642074
CD
2176 :keys "v w (or just w)"]
2177 ["Month View" org-agenda-month-view
2178 :active (org-agenda-check-type nil 'agenda)
acedf35c 2179 :style radio :selected (eq org-agenda-current-span 'month)
8d642074
CD
2180 :keys "v m"]
2181 ["Year View" org-agenda-year-view
2182 :active (org-agenda-check-type nil 'agenda)
acedf35c 2183 :style radio :selected (eq org-agenda-current-span 'year)
8d642074
CD
2184 :keys "v y"]
2185 "--"
2186 ["Include Diary" org-agenda-toggle-diary
2187 :style toggle :selected org-agenda-include-diary
2188 :active (org-agenda-check-type nil 'agenda)]
ed21c5c8
CD
2189 ["Include Deadlines" org-agenda-toggle-deadlines
2190 :style toggle :selected org-agenda-include-deadlines
2191 :active (org-agenda-check-type nil 'agenda)]
8d642074
CD
2192 ["Use Time Grid" org-agenda-toggle-time-grid
2193 :style toggle :selected org-agenda-use-time-grid
2194 :active (org-agenda-check-type nil 'agenda)]
2195 "--"
2196 ["Show clock report" org-agenda-clockreport-mode
2197 :style toggle :selected org-agenda-clockreport-mode
2198 :active (org-agenda-check-type nil 'agenda)]
2199 ["Show some entry text" org-agenda-entry-text-mode
2200 :style toggle :selected org-agenda-entry-text-mode
2201 :active t]
8223b1d2 2202 "--"
8d642074
CD
2203 ["Show Logbook entries" org-agenda-log-mode
2204 :style toggle :selected org-agenda-show-log
2205 :active (org-agenda-check-type nil 'agenda 'timeline)
2206 :keys "v l (or just l)"]
2207 ["Include archived trees" org-agenda-archives-mode
2208 :style toggle :selected org-agenda-archives-mode :active t
2209 :keys "v a"]
2210 ["Include archive files" (org-agenda-archives-mode t)
2211 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2212 :keys "v A"]
2213 "--"
2214 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
e66ba1df 2215 ["Write view to file" org-agenda-write t]
8d642074
CD
2216 ["Rebuild buffer" org-agenda-redo t]
2217 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2218 "--"
2219 ["Show original entry" org-agenda-show t]
20908596
CD
2220 ["Go To (other window)" org-agenda-goto t]
2221 ["Go To (this window)" org-agenda-switch-to t]
8223b1d2 2222 ["Capture with cursor date" org-agenda-capture t]
20908596
CD
2223 ["Follow Mode" org-agenda-follow-mode
2224 :style toggle :selected org-agenda-follow-mode :active t]
8223b1d2 2225 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
20908596 2226 "--"
8d642074
CD
2227 ("TODO"
2228 ["Cycle TODO" org-agenda-todo t]
2229 ["Next TODO set" org-agenda-todo-nextset t]
2230 ["Previous TODO set" org-agenda-todo-previousset t]
2231 ["Add note" org-agenda-add-note t])
2232 ("Archive/Refile/Delete"
8bfe682a
CD
2233 ["Archive default" org-agenda-archive-default t]
2234 ["Archive default" org-agenda-archive-default-with-confirmation t]
20908596
CD
2235 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2236 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
c8d0cf5c 2237 ["Archive subtree" org-agenda-archive t]
8d642074
CD
2238 "--"
2239 ["Refile" org-agenda-refile t]
2240 "--"
2241 ["Delete subtree" org-agenda-kill t])
c8d0cf5c 2242 ("Bulk action"
8d642074 2243 ["Mark entry" org-agenda-bulk-mark t]
8223b1d2 2244 ["Mark all" org-agenda-bulk-mark-all t]
3ab2c837 2245 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
8d642074 2246 ["Unmark entry" org-agenda-bulk-unmark t]
8223b1d2
BG
2247 ["Unmark all entries" org-agenda-bulk-unmark-all :active t :keys "U"])
2248 ["Act on all marked" org-agenda-bulk-action t]
c8d0cf5c 2249 "--"
20908596
CD
2250 ("Tags and Properties"
2251 ["Show all Tags" org-agenda-show-tags t]
2252 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2253 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2254 "--"
2255 ["Column View" org-columns t])
8d642074 2256 ("Deadline/Schedule"
20908596
CD
2257 ["Schedule" org-agenda-schedule t]
2258 ["Set Deadline" org-agenda-deadline t]
2259 "--"
2260 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2261 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
c8d0cf5c
CD
2262 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2263 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2264 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2265 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
20908596 2266 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
54a0dee5 2267 ("Clock and Effort"
20908596
CD
2268 ["Clock in" org-agenda-clock-in t]
2269 ["Clock out" org-agenda-clock-out t]
2270 ["Clock cancel" org-agenda-clock-cancel t]
54a0dee5
CD
2271 ["Goto running clock" org-clock-goto t]
2272 "--"
2273 ["Set Effort" org-agenda-set-effort t]
2274 ["Change clocked effort" org-clock-modify-effort-estimate
2275 (org-clock-is-active)])
20908596
CD
2276 ("Priority"
2277 ["Set Priority" org-agenda-priority t]
2278 ["Increase Priority" org-agenda-priority-up t]
2279 ["Decrease Priority" org-agenda-priority-down t]
8223b1d2 2280 ["Show Priority" org-show-priority t])
20908596
CD
2281 ("Calendar/Diary"
2282 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2283 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2284 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2285 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2286 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2287 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2288 "--"
8d642074 2289 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
20908596 2290 "--"
8d642074 2291 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2c3ad40d 2292 "--"
8d642074
CD
2293 ("MobileOrg"
2294 ["Push Files and Views" org-mobile-push t]
2295 ["Get Captured and Flagged" org-mobile-pull t]
2296 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2297 ["Show note / unflag" org-agenda-show-the-flagging-note t]
c8d0cf5c 2298 "--"
8d642074 2299 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
20908596
CD
2300 "--"
2301 ["Quit" org-agenda-quit t]
2302 ["Exit and Release Buffers" org-agenda-exit t]
2303 ))
2304
2305;;; Agenda undo
2306
2307(defvar org-agenda-allow-remote-undo t
ed21c5c8 2308 "Non-nil means allow remote undo from the agenda buffer.")
20908596
CD
2309(defvar org-agenda-undo-has-started-in nil
2310 "Buffers that have already seen `undo-start' in the current undo sequence.")
20908596 2311
20908596
CD
2312(defun org-agenda-undo ()
2313 "Undo a remote editing step in the agenda.
2314This undoes changes both in the agenda buffer and in the remote buffer
2315that have been changed along."
2316 (interactive)
2317 (or org-agenda-allow-remote-undo
f924a367 2318 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
20908596
CD
2319 (if (not (eq this-command last-command))
2320 (setq org-agenda-undo-has-started-in nil
2321 org-agenda-pending-undo-list org-agenda-undo-list))
2322 (if (not org-agenda-pending-undo-list)
2323 (error "No further undo information"))
2324 (let* ((entry (pop org-agenda-pending-undo-list))
2325 buf line cmd rembuf)
2326 (setq cmd (pop entry) line (pop entry))
2327 (setq rembuf (nth 2 entry))
2328 (org-with-remote-undo rembuf
2329 (while (bufferp (setq buf (pop entry)))
2330 (if (pop entry)
2331 (with-current-buffer buf
2332 (let ((last-undo-buffer buf)
2333 (inhibit-read-only t))
2334 (unless (memq buf org-agenda-undo-has-started-in)
2335 (push buf org-agenda-undo-has-started-in)
2336 (make-local-variable 'pending-undo-list)
2337 (undo-start))
2338 (while (and pending-undo-list
2339 (listp pending-undo-list)
2340 (not (car pending-undo-list)))
2341 (pop pending-undo-list))
2342 (undo-more 1))))))
54a0dee5 2343 (org-goto-line line)
20908596
CD
2344 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2345
2346(defun org-verify-change-for-undo (l1 l2)
2347 "Verify that a real change occurred between the undo lists L1 and L2."
2348 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2349 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2350 (not (eq l1 l2)))
2351
2352;;; Agenda dispatch
2353
20908596
CD
2354(defvar org-agenda-restrict-begin (make-marker))
2355(defvar org-agenda-restrict-end (make-marker))
2356(defvar org-agenda-last-dispatch-buffer nil)
2357(defvar org-agenda-overriding-restriction nil)
2358
8223b1d2
BG
2359(defcustom org-agenda-custom-commands-contexts nil
2360 "Alist of custom agenda keys and contextual rules.
2361
2362For example, if you have a custom agenda command \"p\" and you
2363want this command to be accessible only from plain text files,
2364use this:
2365
2366 '((\"p\" (in-file . \"\\.txt\")))
2367
2368Here are the available contexts definitions:
2369
2370 in-file: command displayed only in matching files
2371 in-mode: command displayed only in matching modes
2372 not-in-file: command not displayed in matching files
2373 not-in-mode: command not displayed in matching modes
2374 [function]: a custom function taking no argument
2375
2376If you define several checks, the agenda command will be
2377accessible if there is at least one valid check.
2378
2379You can also bind a key to another agenda custom command
2380depending on contextual rules.
2381
2382 '((\"p\" \"q\" (in-file . \"\\.txt\")))
2383
2384Here it means: in .txt files, use \"p\" as the key for the
2385agenda command otherwise associated with \"q\". (The command
2386originally associated with \"q\" is not displayed to avoid
2387duplicates.)"
2388 :version "24.3"
2389 :group 'org-agenda-custom-commands
2390 :type '(repeat (list :tag "Rule"
2391 (string :tag " Agenda key")
2392 (string :tag "Replace by command")
2393 (repeat :tag "Available when"
2394 (choice
2395 (cons :tag "Condition"
2396 (choice
2397 (const :tag "In file" in-file)
2398 (const :tag "Not in file" not-in-file)
2399 (const :tag "In mode" in-mode)
2400 (const :tag "Not in mode" not-in-mode))
2401 (regexp))
2402 (function :tag "Custom function"))))))
2403
2404(defvar org-keys nil)
2405(defvar org-match nil)
20908596 2406;;;###autoload
8223b1d2 2407(defun org-agenda (&optional arg org-keys restriction)
20908596
CD
2408 "Dispatch agenda commands to collect entries to the agenda buffer.
2409Prompts for a command to execute. Any prefix arg will be passed
2410on to the selected command. The default selections are:
2411
2412a Call `org-agenda-list' to display the agenda for current day or week.
2413t Call `org-todo-list' to display the global todo list.
2414T Call `org-todo-list' to display the global todo list, select only
2415 entries with a specific TODO keyword (the user gets a prompt).
2416m Call `org-tags-view' to display headlines with tags matching
2417 a condition (the user is prompted for the condition).
2418M Like `m', but select only TODO entries, no ordinary headlines.
2419L Create a timeline for the current buffer.
2420e Export views to associated files.
c8d0cf5c 2421s Search entries for keywords.
8223b1d2 2422S Search entries for keywords, only with TODO keywords.
8bfe682a 2423/ Multi occur across all agenda files and also files listed
c8d0cf5c
CD
2424 in `org-agenda-text-search-extra-files'.
2425< Restrict agenda commands to buffer, subtree, or region.
2426 Press several times to get the desired effect.
2427> Remove a previous restriction.
2428# List \"stuck\" projects.
2429! Configure what \"stuck\" means.
2430C Configure custom agenda commands.
20908596
CD
2431
2432More commands can be added by configuring the variable
2433`org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2434searches can be pre-defined in this way.
2435
2436If the current buffer is in Org-mode and visiting a file, you can also
2437first press `<' once to indicate that the agenda should be temporarily
2438\(until the next use of \\[org-agenda]) restricted to the current file.
2439Pressing `<' twice means to restrict to the current subtree or region
2440\(if active)."
2441 (interactive "P")
2442 (catch 'exit
2443 (let* ((prefix-descriptions nil)
8223b1d2 2444 (org-agenda-buffer-name org-agenda-buffer-name)
54a0dee5
CD
2445 (org-agenda-window-setup (if (equal (buffer-name)
2446 org-agenda-buffer-name)
2447 'current-window
2448 org-agenda-window-setup))
20908596
CD
2449 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2450 (org-agenda-custom-commands
2451 ;; normalize different versions
2452 (delq nil
2453 (mapcar
2454 (lambda (x)
2455 (cond ((stringp (cdr x))
2456 (push x prefix-descriptions)
2457 nil)
2458 ((stringp (nth 1 x)) x)
2459 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2460 (t (cons (car x) (cons "" (cdr x))))))
2461 org-agenda-custom-commands)))
8223b1d2
BG
2462 (org-agenda-custom-commands
2463 (org-contextualize-keys
2464 org-agenda-custom-commands org-agenda-custom-commands-contexts))
20908596
CD
2465 (buf (current-buffer))
2466 (bfn (buffer-file-name (buffer-base-buffer)))
8223b1d2 2467 entry key type org-match lprops ans)
8d642074 2468 ;; Turn off restriction unless there is an overriding one,
20908596 2469 (unless org-agenda-overriding-restriction
8bfe682a 2470 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
8d642074
CD
2471 ;; There is a request to keep the file list in place
2472 (put 'org-agenda-files 'org-restrict nil))
20908596
CD
2473 (setq org-agenda-restrict nil)
2474 (move-marker org-agenda-restrict-begin nil)
2475 (move-marker org-agenda-restrict-end nil))
2476 ;; Delete old local properties
2477 (put 'org-agenda-redo-command 'org-lprops nil)
3ab2c837
BG
2478 ;; Delete previously set last-arguments
2479 (put 'org-agenda-redo-command 'last-args nil)
20908596
CD
2480 ;; Remember where this call originated
2481 (setq org-agenda-last-dispatch-buffer (current-buffer))
8223b1d2 2482 (unless org-keys
20908596 2483 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
8223b1d2 2484 org-keys (car ans)
20908596 2485 restriction (cdr ans)))
8223b1d2
BG
2486 ;; If we have sticky agenda buffers, set a name for the buffer,
2487 ;; depending on the invoking keys. The user may still set this
2488 ;; as a command option, which will overwrite what we do here.
2489 (if org-agenda-sticky
2490 (setq org-agenda-buffer-name
2491 (format "*Org Agenda(%s)*" org-keys)))
8bfe682a 2492 ;; Establish the restriction, if any
20908596
CD
2493 (when (and (not org-agenda-overriding-restriction) restriction)
2494 (put 'org-agenda-files 'org-restrict (list bfn))
2495 (cond
2496 ((eq restriction 'region)
2497 (setq org-agenda-restrict t)
2498 (move-marker org-agenda-restrict-begin (region-beginning))
2499 (move-marker org-agenda-restrict-end (region-end)))
2500 ((eq restriction 'subtree)
2501 (save-excursion
2502 (setq org-agenda-restrict t)
2503 (org-back-to-heading t)
2504 (move-marker org-agenda-restrict-begin (point))
2505 (move-marker org-agenda-restrict-end
2506 (progn (org-end-of-subtree t)))))))
2507
20908596
CD
2508 ;; For example the todo list should not need it (but does...)
2509 (cond
8223b1d2 2510 ((setq entry (assoc org-keys org-agenda-custom-commands))
20908596
CD
2511 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2512 (progn
8223b1d2 2513 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
8bfe682a 2514 lprops (nth 4 entry))
8223b1d2
BG
2515 (if org-agenda-sticky
2516 (setq org-agenda-buffer-name
2517 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2518 (format "*Org Agenda(%s)*" org-keys))))
20908596
CD
2519 (put 'org-agenda-redo-command 'org-lprops lprops)
2520 (cond
2521 ((eq type 'agenda)
2522 (org-let lprops '(org-agenda-list current-prefix-arg)))
2523 ((eq type 'alltodo)
2524 (org-let lprops '(org-todo-list current-prefix-arg)))
2525 ((eq type 'search)
8223b1d2 2526 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
20908596
CD
2527 ((eq type 'stuck)
2528 (org-let lprops '(org-agenda-list-stuck-projects
2529 current-prefix-arg)))
2530 ((eq type 'tags)
8223b1d2 2531 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
20908596 2532 ((eq type 'tags-todo)
8223b1d2 2533 (org-let lprops '(org-tags-view '(4) org-match)))
20908596 2534 ((eq type 'todo)
8223b1d2 2535 (org-let lprops '(org-todo-list org-match)))
20908596
CD
2536 ((eq type 'tags-tree)
2537 (org-check-for-org-mode)
8223b1d2 2538 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
20908596
CD
2539 ((eq type 'todo-tree)
2540 (org-check-for-org-mode)
2541 (org-let lprops
3ab2c837 2542 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
8223b1d2 2543 (regexp-quote org-match) "\\>"))))
20908596
CD
2544 ((eq type 'occur-tree)
2545 (org-check-for-org-mode)
8223b1d2 2546 (org-let lprops '(org-occur org-match)))
20908596 2547 ((functionp type)
8223b1d2 2548 (org-let lprops '(funcall type org-match)))
20908596 2549 ((fboundp type)
8223b1d2 2550 (org-let lprops '(funcall type org-match)))
20908596 2551 (t (error "Invalid custom agenda command type %s" type))))
3ab2c837 2552 (org-agenda-run-series (nth 1 entry) (cddr entry))))
8223b1d2 2553 ((equal org-keys "C")
20908596
CD
2554 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2555 (customize-variable 'org-agenda-custom-commands))
8223b1d2
BG
2556 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2557 ((equal org-keys "s") (call-interactively 'org-search-view))
2558 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2559 ((equal org-keys "t") (call-interactively 'org-todo-list))
2560 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2561 ((equal org-keys "m") (call-interactively 'org-tags-view))
2562 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2563 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2564 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
8d642074
CD
2565 (org-add-hook
2566 'post-command-hook
2567 (lambda ()
2568 (unless (current-message)
2569 (let* ((m (org-agenda-get-any-marker))
2570 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2571 (when note
2572 (message (concat
2573 "FLAGGING-NOTE ([?] for more info): "
2574 (org-add-props
2575 (replace-regexp-in-string
2576 "\\\\n" "//"
2577 (copy-sequence note))
2578 nil 'face 'org-warning)))))))
2579 t t))
8223b1d2
BG
2580 ((equal org-keys "L")
2581 (unless (derived-mode-p 'org-mode)
20908596
CD
2582 (error "This is not an Org-mode file"))
2583 (unless restriction
2584 (put 'org-agenda-files 'org-restrict (list bfn))
2585 (org-call-with-arg 'org-timeline arg)))
8223b1d2
BG
2586 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2587 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2588 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
20908596
CD
2589 (t (error "Invalid agenda key"))))))
2590
bdebdb64
BG
2591(autoload 'org-agenda "org-agenda" "\
2592Dispatch agenda commands to collect entries to the agenda buffer.
2593Prompts for a command to execute. Any prefix arg will be passed
2594on to the selected command. The default selections are:
2595
2596a Call `org-agenda-list' to display the agenda for current day or week.
2597t Call `org-todo-list' to display the global todo list.
2598T Call `org-todo-list' to display the global todo list, select only
2599 entries with a specific TODO keyword (the user gets a prompt).
2600m Call `org-tags-view' to display headlines with tags matching
2601 a condition (the user is prompted for the condition).
2602M Like `m', but select only TODO entries, no ordinary headlines.
2603L Create a timeline for the current buffer.
2604e Export views to associated files.
2605s Search entries for keywords.
2606S Search entries for keywords, only with TODO keywords.
2607/ Multi occur across all agenda files and also files listed
2608 in `org-agenda-text-search-extra-files'.
2609< Restrict agenda commands to buffer, subtree, or region.
2610 Press several times to get the desired effect.
2611> Remove a previous restriction.
2612# List \"stuck\" projects.
2613! Configure what \"stuck\" means.
2614C Configure custom agenda commands.
2615
2616More commands can be added by configuring the variable
2617`org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2618searches can be pre-defined in this way.
2619
2620If the current buffer is in Org-mode and visiting a file, you can also
2621first press `<' once to indicate that the agenda should be temporarily
2622\(until the next use of \\[org-agenda]) restricted to the current file.
2623Pressing `<' twice means to restrict to the current subtree or region
2624\(if active).
2625
2626\(fn &optional ARG ORG-KEYS RESTRICTION)" t nil)
2627
3ab2c837
BG
2628(defun org-agenda-append-agenda ()
2629 "Append another agenda view to the current one.
2630This function allows interactive building of block agendas.
2631Agenda views are separated by `org-agenda-block-separator'."
2632 (interactive)
8223b1d2 2633 (unless (derived-mode-p 'org-agenda-mode)
3ab2c837
BG
2634 (error "Can only append from within agenda buffer"))
2635 (let ((org-agenda-multi t))
2636 (org-agenda)
8223b1d2
BG
2637 (widen)
2638 (org-agenda-finalize)
2639 (org-agenda-fit-window-to-buffer)))
3ab2c837 2640
20908596
CD
2641(defun org-agenda-normalize-custom-commands (cmds)
2642 (delq nil
2643 (mapcar
2644 (lambda (x)
2645 (cond ((stringp (cdr x)) nil)
2646 ((stringp (nth 1 x)) x)
2647 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2648 (t (cons (car x) (cons "" (cdr x))))))
2649 cmds)))
2650
2651(defun org-agenda-get-restriction-and-command (prefix-descriptions)
2652 "The user interface for selecting an agenda command."
2653 (catch 'exit
2654 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
8223b1d2 2655 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
20908596
CD
2656 (region-p (org-region-active-p))
2657 (custom org-agenda-custom-commands)
2658 (selstring "")
2659 restriction second-time
afe98dfa
CD
2660 c entry key type match prefixes rmheader header-end custom1 desc
2661 line lines left right n n1)
20908596
CD
2662 (save-window-excursion
2663 (delete-other-windows)
2664 (org-switch-to-buffer-other-window " *Agenda Commands*")
2665 (erase-buffer)
2666 (insert (eval-when-compile
2667 (let ((header
8223b1d2 2668 "Press key for an agenda command: < Buffer, subtree/region restriction
20908596
CD
2669-------------------------------- > Remove restriction
2670a Agenda for current week or day e Export agenda views
2671t List of all TODO entries T Entries with special TODO kwd
621f83e4 2672m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
8223b1d2 2673s Search for keywords S Like s, but only TODO entries
20908596 2674L Timeline for current buffer # List stuck projects (!=configure)
8223b1d2
BG
2675/ Multi-occur C Configure custom agenda commands
2676? Find :FLAGGED: entries * Toggle sticky agenda views
20908596
CD
2677")
2678 (start 0))
2679 (while (string-match
2680 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2681 header start)
2682 (setq start (match-end 0))
2683 (add-text-properties (match-beginning 2) (match-end 2)
2684 '(face bold) header))
2685 header)))
c7cf0ebc 2686 (setq header-end (point-marker))
20908596
CD
2687 (while t
2688 (setq custom1 custom)
2689 (when (eq rmheader t)
54a0dee5 2690 (org-goto-line 1)
20908596
CD
2691 (re-search-forward ":" nil t)
2692 (delete-region (match-end 0) (point-at-eol))
2693 (forward-char 1)
2694 (looking-at "-+")
2695 (delete-region (match-end 0) (point-at-eol))
2696 (move-marker header-end (match-end 0)))
2697 (goto-char header-end)
2698 (delete-region (point) (point-max))
afe98dfa
CD
2699
2700 ;; Produce all the lines that describe custom commands and prefixes
2701 (setq lines nil)
20908596
CD
2702 (while (setq entry (pop custom1))
2703 (setq key (car entry) desc (nth 1 entry)
54a0dee5
CD
2704 type (nth 2 entry)
2705 match (nth 3 entry))
20908596
CD
2706 (if (> (length key) 1)
2707 (add-to-list 'prefixes (string-to-char key))
afe98dfa
CD
2708 (setq line
2709 (format
2710 "%-4s%-14s"
2711 (org-add-props (copy-sequence key)
2712 '(face bold))
2713 (cond
2714 ((string-match "\\S-" desc) desc)
2715 ((eq type 'agenda) "Agenda for current week or day")
2716 ((eq type 'alltodo) "List of all TODO entries")
2717 ((eq type 'search) "Word search")
2718 ((eq type 'stuck) "List of stuck projects")
2719 ((eq type 'todo) "TODO keyword")
2720 ((eq type 'tags) "Tags query")
2721 ((eq type 'tags-todo) "Tags (TODO)")
2722 ((eq type 'tags-tree) "Tags tree")
2723 ((eq type 'todo-tree) "TODO kwd tree")
2724 ((eq type 'occur-tree) "Occur tree")
2725 ((functionp type) (if (symbolp type)
2726 (symbol-name type)
2727 "Lambda expression"))
2728 (t "???"))))
2729 (if org-agenda-menu-show-matcher
2730 (setq line
2731 (concat line ": "
2732 (cond
2733 ((stringp match)
2734 (setq match (copy-sequence match))
2735 (org-add-props match nil 'face 'org-warning))
8223b1d2
BG
2736 ((listp type)
2737 (format "set of %d commands" (length type))))))
afe98dfa
CD
2738 (if (org-string-nw-p match)
2739 (add-text-properties
2740 0 (length line) (list 'help-echo
8223b1d2 2741 (concat "Matcher: " match)) line)))
afe98dfa
CD
2742 (push line lines)))
2743 (setq lines (nreverse lines))
20908596
CD
2744 (when prefixes
2745 (mapc (lambda (x)
afe98dfa
CD
2746 (push
2747 (format "%s %s"
20908596 2748 (org-add-props (char-to-string x)
afe98dfa
CD
2749 nil 'face 'bold)
2750 (or (cdr (assoc (concat selstring
2751 (char-to-string x))
20908596 2752 prefix-descriptions))
afe98dfa
CD
2753 "Prefix key"))
2754 lines))
20908596 2755 prefixes))
afe98dfa
CD
2756
2757 ;; Check if we should display in two columns
8223b1d2 2758 (if org-agenda-menu-two-columns
afe98dfa
CD
2759 (progn
2760 (setq n (length lines)
2761 n1 (+ (/ n 2) (mod n 2))
2762 right (nthcdr n1 lines)
2763 left (copy-sequence lines))
2764 (setcdr (nthcdr (1- n1) left) nil))
2765 (setq left lines right nil))
2766 (while left
2767 (insert "\n" (pop left))
2768 (when right
2769 (if (< (current-column) 40)
2770 (move-to-column 40 t)
2771 (insert " "))
2772 (insert (pop right))))
2773
2774 ;; Make the window the right size
20908596 2775 (goto-char (point-min))
93b62de8
CD
2776 (if second-time
2777 (if (not (pos-visible-in-window-p (point-max)))
2778 (org-fit-window-to-buffer))
2779 (setq second-time t)
2780 (org-fit-window-to-buffer))
afe98dfa
CD
2781
2782 ;; Ask for selection
20908596
CD
2783 (message "Press key for agenda command%s:"
2784 (if (or restrict-ok org-agenda-overriding-restriction)
2785 (if org-agenda-overriding-restriction
2786 " (restriction lock active)"
2787 (if restriction
2788 (format " (restricted to %s)" restriction)
2789 " (unrestricted)"))
2790 ""))
2791 (setq c (read-char-exclusive))
2792 (message "")
2793 (cond
2794 ((assoc (char-to-string c) custom)
2795 (setq selstring (concat selstring (char-to-string c)))
2796 (throw 'exit (cons selstring restriction)))
2797 ((memq c prefixes)
2798 (setq selstring (concat selstring (char-to-string c))
2799 prefixes nil
2800 rmheader (or rmheader t)
2801 custom (delq nil (mapcar
2802 (lambda (x)
2803 (if (or (= (length (car x)) 1)
2804 (/= (string-to-char (car x)) c))
2805 nil
2806 (cons (substring (car x) 1) (cdr x))))
2807 custom))))
8223b1d2
BG
2808 ((eq c ?*)
2809 (call-interactively 'org-toggle-sticky-agenda)
2810 (sit-for 2))
20908596
CD
2811 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2812 (message "Restriction is only possible in Org-mode buffers")
2813 (ding) (sit-for 1))
2814 ((eq c ?1)
2815 (org-agenda-remove-restriction-lock 'noupdate)
2816 (setq restriction 'buffer))
2817 ((eq c ?0)
2818 (org-agenda-remove-restriction-lock 'noupdate)
2819 (setq restriction (if region-p 'region 'subtree)))
2820 ((eq c ?<)
2821 (org-agenda-remove-restriction-lock 'noupdate)
2822 (setq restriction
2823 (cond
2824 ((eq restriction 'buffer)
2825 (if region-p 'region 'subtree))
2826 ((memq restriction '(subtree region))
2827 nil)
2828 (t 'buffer))))
2829 ((eq c ?>)
2830 (org-agenda-remove-restriction-lock 'noupdate)
2831 (setq restriction nil))
8223b1d2 2832 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
20908596
CD
2833 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2834 ((and (> (length selstring) 0) (eq c ?\d))
2835 (delete-window)
2836 (org-agenda-get-restriction-and-command prefix-descriptions))
2837
2838 ((equal c ?q) (error "Abort"))
2839 (t (error "Invalid key %c" c))))))))
2840
8223b1d2
BG
2841(defun org-agenda-fit-window-to-buffer ()
2842 "Fit the window to the buffer size."
2843 (and (memq org-agenda-window-setup '(reorganize-frame))
2844 (fboundp 'fit-window-to-buffer)
2845 (org-fit-window-to-buffer
2846 nil
2847 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2848 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2849
2850(defvar org-cmd nil)
2851(defvar org-agenda-overriding-cmd nil)
2852(defvar org-agenda-overriding-arguments nil)
2853(defvar org-agenda-overriding-cmd-arguments nil)
3ab2c837 2854(defun org-agenda-run-series (name series)
8223b1d2
BG
2855 (org-let (nth 1 series) '(org-agenda-prepare name))
2856 ;; We need to reset agenda markers here, because when constructing a
2857 ;; block agenda, the individual blocks do not do that.
2858 (org-agenda-reset-markers)
20908596 2859 (let* ((org-agenda-multi t)
3ab2c837 2860 (redo (list 'org-agenda-run-series name (list 'quote series)))
20908596
CD
2861 (cmds (car series))
2862 (gprops (nth 1 series))
2863 match ;; The byte compiler incorrectly complains about this. Keep it!
8223b1d2
BG
2864 org-cmd type lprops)
2865 (while (setq org-cmd (pop cmds))
2866 (setq type (car org-cmd)
2867 match (eval (nth 1 org-cmd))
2868 lprops (nth 2 org-cmd))
2869 (let ((org-agenda-overriding-arguments
2870 (if (eq org-agenda-overriding-cmd org-cmd)
2871 (or org-agenda-overriding-arguments
2872 org-agenda-overriding-cmd-arguments))))
2873 (cond
2874 ((eq type 'agenda)
2875 (org-let2 gprops lprops
2876 '(call-interactively 'org-agenda-list)))
2877 ((eq type 'alltodo)
2878 (org-let2 gprops lprops
2879 '(call-interactively 'org-todo-list)))
2880 ((eq type 'search)
2881 (org-let2 gprops lprops
2882 '(org-search-view current-prefix-arg match nil)))
2883 ((eq type 'stuck)
2884 (org-let2 gprops lprops
2885 '(call-interactively 'org-agenda-list-stuck-projects)))
2886 ((eq type 'tags)
2887 (org-let2 gprops lprops
2888 '(org-tags-view current-prefix-arg match)))
2889 ((eq type 'tags-todo)
2890 (org-let2 gprops lprops
2891 '(org-tags-view '(4) match)))
2892 ((eq type 'todo)
2893 (org-let2 gprops lprops
2894 '(org-todo-list match)))
2895 ((fboundp type)
2896 (org-let2 gprops lprops
2897 '(funcall type match)))
2898 (t (error "Invalid type in command series")))))
20908596 2899 (widen)
8223b1d2
BG
2900 (let ((inhibit-read-only t))
2901 (add-text-properties (point-min) (point-max)
735135f9 2902 `(org-series t org-series-redo-cmd ,redo)))
20908596
CD
2903 (setq org-agenda-redo-command redo)
2904 (goto-char (point-min)))
8223b1d2
BG
2905 (org-agenda-fit-window-to-buffer)
2906 (org-let (nth 1 series) '(org-agenda-finalize)))
20908596
CD
2907
2908;;;###autoload
2909(defmacro org-batch-agenda (cmd-key &rest parameters)
2910 "Run an agenda command in batch mode and send the result to STDOUT.
2911If CMD-KEY is a string of length 1, it is used as a key in
2912`org-agenda-custom-commands' and triggers this command. If it is a
2913longer string it is used as a tags/todo match string.
86fbb8ca 2914Parameters are alternating variable names and values that will be bound
20908596 2915before running the agenda command."
e66ba1df 2916 (org-eval-in-environment (org-make-parameter-alist parameters)
20908596 2917 (if (> (length cmd-key) 2)
e66ba1df
BG
2918 (org-tags-view nil cmd-key)
2919 (org-agenda nil cmd-key)))
2920 (set-buffer org-agenda-buffer-name)
2921 (princ (buffer-string)))
bdebdb64
BG
2922
2923(autoload 'org-batch-agenda "org-agenda" "\
2924Run an agenda command in batch mode and send the result to STDOUT.
2925If CMD-KEY is a string of length 1, it is used as a key in
2926`org-agenda-custom-commands' and triggers this command. If it is a
2927longer string it is used as a tags/todo match string.
2928Parameters are alternating variable names and values that will be bound
2929before running the agenda command.
2930
2931\(fn CMD-KEY &rest PARAMETERS)" nil t)
e66ba1df 2932(def-edebug-spec org-batch-agenda (form &rest sexp))
20908596
CD
2933
2934(defvar org-agenda-info nil)
2935
2936;;;###autoload
2937(defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2938 "Run an agenda command in batch mode and send the result to STDOUT.
2939If CMD-KEY is a string of length 1, it is used as a key in
2940`org-agenda-custom-commands' and triggers this command. If it is a
2941longer string it is used as a tags/todo match string.
86fbb8ca 2942Parameters are alternating variable names and values that will be bound
20908596
CD
2943before running the agenda command.
2944
2945The output gives a line for each selected agenda item. Each
2946item is a list of comma-separated values, like this:
2947
2948category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2949
2950category The category of the item
2951head The headline, without TODO kwd, TAGS and PRIORITY
2952type The type of the agenda entry, can be
2953 todo selected in TODO match
2954 tagsmatch selected in tags match
2955 diary imported from diary
2956 deadline a deadline on given date
2957 scheduled scheduled on given date
2958 timestamp entry has timestamp on given date
2959 closed entry was closed on given date
2960 upcoming-deadline warning about deadline
2961 past-scheduled forwarded scheduled item
2962 block entry has date block including g. date
2963todo The todo keyword, if any
2964tags All tags including inherited ones, separated by colons
2965date The relevant date, like 2007-2-14
2966time The time, like 15:00-16:50
2967extra Sting with extra planning info
2968priority-l The priority letter if any was given
2969priority-n The computed numerical priority
2970agenda-day The day in the agenda where this is listed"
e66ba1df
BG
2971 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2972 (org-make-parameter-alist parameters))
20908596 2973 (if (> (length cmd-key) 2)
e66ba1df
BG
2974 (org-tags-view nil cmd-key)
2975 (org-agenda nil cmd-key)))
2976 (set-buffer org-agenda-buffer-name)
2977 (let* ((lines (org-split-string (buffer-string) "\n"))
2978 line)
2979 (while (setq line (pop lines))
2980 (catch 'next
2981 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2982 (setq org-agenda-info
2983 (org-fix-agenda-info (text-properties-at 0 line)))
2984 (princ
2985 (mapconcat 'org-agenda-export-csv-mapper
2986 '(org-category txt type todo tags date time extra
2987 priority-letter priority agenda-day)
2988 ","))
2989 (princ "\n")))))
bdebdb64
BG
2990
2991(autoload 'org-batch-agenda-csv "org-agenda" "\
2992Run an agenda command in batch mode and send the result to STDOUT.
2993If CMD-KEY is a string of length 1, it is used as a key in
2994`org-agenda-custom-commands' and triggers this command. If it is a
2995longer string it is used as a tags/todo match string.
2996Parameters are alternating variable names and values that will be bound
2997before running the agenda command.
2998
2999The output gives a line for each selected agenda item. Each
3000item is a list of comma-separated values, like this:
3001
3002category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3003
3004category The category of the item
3005head The headline, without TODO kwd, TAGS and PRIORITY
3006type The type of the agenda entry, can be
3007 todo selected in TODO match
3008 tagsmatch selected in tags match
3009 diary imported from diary
3010 deadline a deadline on given date
3011 scheduled scheduled on given date
3012 timestamp entry has timestamp on given date
3013 closed entry was closed on given date
3014 upcoming-deadline warning about deadline
3015 past-scheduled forwarded scheduled item
3016 block entry has date block including g. date
3017todo The todo keyword, if any
3018tags All tags including inherited ones, separated by colons
3019date The relevant date, like 2007-2-14
3020time The time, like 15:00-16:50
3021extra Sting with extra planning info
3022priority-l The priority letter if any was given
3023priority-n The computed numerical priority
3024agenda-day The day in the agenda where this is listed
3025
3026\(fn CMD-KEY &rest PARAMETERS)" nil t)
e66ba1df 3027(def-edebug-spec org-batch-agenda-csv (form &rest sexp))
20908596
CD
3028
3029(defun org-fix-agenda-info (props)
86fbb8ca
CD
3030 "Make sure all properties on an agenda item have a canonical form.
3031This ensures the export commands can easily use it."
20908596
CD
3032 (let (tmp re)
3033 (when (setq tmp (plist-get props 'tags))
3034 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3035 (when (setq tmp (plist-get props 'date))
3036 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3037 (let ((calendar-date-display-form '(year "-" month "-" day)))
3038 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3039
3040 (setq tmp (calendar-date-string tmp)))
3041 (setq props (plist-put props 'date tmp)))
3042 (when (setq tmp (plist-get props 'day))
3043 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3044 (let ((calendar-date-display-form '(year "-" month "-" day)))
3045 (setq tmp (calendar-date-string tmp)))
3046 (setq props (plist-put props 'day tmp))
3047 (setq props (plist-put props 'agenda-day tmp)))
3048 (when (setq tmp (plist-get props 'txt))
3049 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3050 (plist-put props 'priority-letter (match-string 1 tmp))
3051 (setq tmp (replace-match "" t t tmp)))
3052 (when (and (setq re (plist-get props 'org-todo-regexp))
3053 (setq re (concat "\\`\\.*" re " ?"))
3054 (string-match re tmp))
3055 (plist-put props 'todo (match-string 1 tmp))
3056 (setq tmp (replace-match "" t t tmp)))
3057 (plist-put props 'txt tmp)))
3058 props)
3059
3060(defun org-agenda-export-csv-mapper (prop)
3061 (let ((res (plist-get org-agenda-info prop)))
3062 (setq res
3063 (cond
3064 ((not res) "")
3065 ((stringp res) res)
3066 (t (prin1-to-string res))))
3067 (while (string-match "," res)
3068 (setq res (replace-match ";" t t res)))
3069 (org-trim res)))
3070
20908596
CD
3071;;;###autoload
3072(defun org-store-agenda-views (&rest parameters)
3073 (interactive)
3074 (eval (list 'org-batch-store-agenda-views)))
3075
bdebdb64
BG
3076(autoload 'org-store-agenda-views "org-agenda" "\
3077
3078
3079\(fn &rest PARAMETERS)" t nil)
3080
20908596
CD
3081;;;###autoload
3082(defmacro org-batch-store-agenda-views (&rest parameters)
3083 "Run all custom agenda commands that have a file argument."
3084 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3085 (pop-up-frames nil)
3086 (dir default-directory)
e66ba1df 3087 (pars (org-make-parameter-alist parameters))
8223b1d2 3088 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
20908596
CD
3089 (save-window-excursion
3090 (while cmds
3091 (setq cmd (pop cmds)
3092 thiscmdkey (car cmd)
8223b1d2
BG
3093 thiscmdcmd (cdr cmd)
3094 match (nth 2 thiscmdcmd)
3095 bufname (if org-agenda-sticky
3096 (or (and (stringp match)
3097 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3098 (format "*Org Agenda(%s)*" thiscmdkey))
3099 org-agenda-buffer-name)
2c3ad40d
CD
3100 cmd-or-set (nth 2 cmd)
3101 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3102 files (nth (if (listp cmd-or-set) 4 5) cmd))
20908596
CD
3103 (if (stringp files) (setq files (list files)))
3104 (when files
e66ba1df
BG
3105 (org-eval-in-environment (append org-agenda-exporter-settings
3106 opts pars)
3107 (org-agenda nil thiscmdkey))
8223b1d2 3108 (set-buffer bufname)
20908596 3109 (while files
e66ba1df
BG
3110 (org-eval-in-environment (append org-agenda-exporter-settings
3111 opts pars)
8223b1d2
BG
3112 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3113 (and (get-buffer bufname)
3114 (kill-buffer bufname)))))))
bdebdb64
BG
3115
3116(autoload 'org-batch-store-agenda-views "org-agenda" "\
3117Run all custom agenda commands that have a file argument.
3118
3119\(fn &rest PARAMETERS)" nil t)
e66ba1df 3120(def-edebug-spec org-batch-store-agenda-views (&rest sexp))
20908596 3121
8223b1d2
BG
3122(defvar org-agenda-current-span nil
3123 "The current span used in the agenda view.") ; local variable in the agenda buffer
8d642074
CD
3124(defun org-agenda-mark-header-line (pos)
3125 "Mark the line at POS as an agenda structure header."
3126 (save-excursion
3127 (goto-char pos)
3128 (put-text-property (point-at-bol) (point-at-eol)
3129 'org-agenda-structural-header t)
3130 (when org-agenda-title-append
3131 (put-text-property (point-at-bol) (point-at-eol)
3132 'org-agenda-title-append org-agenda-title-append))))
3133
8223b1d2 3134(defvar org-mobile-creating-agendas) ; defined in org-mobile.el
e66ba1df 3135(defvar org-agenda-write-buffer-name "Agenda View")
8223b1d2 3136(defun org-agenda-write (file &optional open nosettings agenda-bufname)
20908596
CD
3137 "Write the current buffer (an agenda view) as a file.
3138Depending on the extension of the file name, plain text (.txt),
e66ba1df
BG
3139HTML (.html or .htm) or Postscript (.ps) is produced.
3140If the extension is .ics, run icalendar export over all files used
20908596
CD
3141to construct the agenda and limit the export to entries listed in the
3142agenda now.
8bfe682a 3143With prefix argument OPEN, open the new file immediately.
20908596
CD
3144If NOSETTINGS is given, do not scope the settings of
3145`org-agenda-exporter-settings' into the export commands. This is used when
3146the settings have already been scoped and we do not wish to overrule other,
8223b1d2
BG
3147higher priority settings.
3148If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
c8d0cf5c 3149 (interactive "FWrite agenda to file: \nP")
20908596
CD
3150 (if (not (file-writable-p file))
3151 (error "Cannot write agenda to file %s" file))
20908596 3152 (org-let (if nosettings nil org-agenda-exporter-settings)
afe98dfa 3153 '(save-excursion
20908596 3154 (save-window-excursion
93b62de8 3155 (org-agenda-mark-filtered-text)
8bfe682a 3156 (let ((bs (copy-sequence (buffer-string))) beg)
93b62de8
CD
3157 (org-agenda-unmark-filtered-text)
3158 (with-temp-buffer
e66ba1df 3159 (rename-buffer org-agenda-write-buffer-name t)
afe98dfa 3160 (set-buffer-modified-p nil)
20908596 3161 (insert bs)
93b62de8
CD
3162 (org-agenda-remove-marked-text 'org-filtered)
3163 (while (setq beg (text-property-any (point-min) (point-max)
3164 'org-filtered t))
3165 (delete-region
3166 beg (or (next-single-property-change beg 'org-filtered)
3167 (point-max))))
c8d0cf5c 3168 (run-hooks 'org-agenda-before-write-hook)
93b62de8 3169 (cond
8bfe682a
CD
3170 ((org-bound-and-true-p org-mobile-creating-agendas)
3171 (org-mobile-write-agenda-for-mobile file))
93b62de8 3172 ((string-match "\\.html?\\'" file)
afe98dfa 3173 (require 'htmlize)
93b62de8 3174 (set-buffer (htmlize-buffer (current-buffer)))
8223b1d2 3175 (when org-agenda-export-html-style
93b62de8
CD
3176 ;; replace <style> section with org-agenda-export-html-style
3177 (goto-char (point-min))
3178 (kill-region (- (search-forward "<style") 6)
3179 (search-forward "</style>"))
3180 (insert org-agenda-export-html-style))
3181 (write-file file)
3182 (kill-buffer (current-buffer))
3183 (message "HTML written to %s" file))
3184 ((string-match "\\.ps\\'" file)
c8d0cf5c 3185 (require 'ps-print)
afe98dfa 3186 (ps-print-buffer-with-faces file)
e66ba1df 3187 (message "Postscript written to %s" file))
c8d0cf5c
CD
3188 ((string-match "\\.pdf\\'" file)
3189 (require 'ps-print)
afe98dfa
CD
3190 (ps-print-buffer-with-faces
3191 (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c
CD
3192 (call-process "ps2pdf" nil nil nil
3193 (expand-file-name
3194 (concat (file-name-sans-extension file) ".ps"))
3195 (expand-file-name file))
afe98dfa 3196 (delete-file (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c 3197 (message "PDF written to %s" file))
93b62de8 3198 ((string-match "\\.ics\\'" file)
c8d0cf5c 3199 (require 'org-icalendar)
93b62de8
CD
3200 (let ((org-agenda-marker-table
3201 (org-create-marker-find-array
3202 (org-agenda-collect-markers)))
3203 (org-icalendar-verify-function 'org-check-agenda-marker-table)
3204 (org-combined-agenda-icalendar-file file))
3205 (apply 'org-export-icalendar 'combine
3206 (org-agenda-files nil 'ifmode))))
3207 (t
3208 (let ((bs (buffer-string)))
3209 (find-file file)
3210 (erase-buffer)
3211 (insert bs)
3212 (save-buffer 0)
3213 (kill-buffer (current-buffer))
3214 (message "Plain text written to %s" file))))))))
8223b1d2 3215 (set-buffer (or agenda-bufname
c7cf0ebc 3216 (and (org-called-interactively-p 'any) (buffer-name))
8223b1d2 3217 org-agenda-buffer-name)))
c8d0cf5c
CD
3218 (when open (org-open-file file)))
3219
e66ba1df
BG
3220(defvar org-agenda-tag-filter-overlays nil)
3221(defvar org-agenda-cat-filter-overlays nil)
93b62de8
CD
3222
3223(defun org-agenda-mark-filtered-text ()
3224 "Mark all text hidden by filtering with a text property."
3225 (let ((inhibit-read-only t))
3226 (mapc
3227 (lambda (o)
86fbb8ca 3228 (when (equal (overlay-buffer o) (current-buffer))
93b62de8 3229 (put-text-property
86fbb8ca 3230 (overlay-start o) (overlay-end o)
93b62de8 3231 'org-filtered t)))
e66ba1df
BG
3232 (append org-agenda-tag-filter-overlays
3233 org-agenda-cat-filter-overlays))))
93b62de8
CD
3234
3235(defun org-agenda-unmark-filtered-text ()
3236 "Remove the filtering text property."
3237 (let ((inhibit-read-only t))
3238 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3239
3240(defun org-agenda-remove-marked-text (property &optional value)
3241 "Delete all text marked with VALUE of PROPERTY.
3242VALUE defaults to t."
3243 (let (beg)
3244 (setq value (or value t))
3245 (while (setq beg (text-property-any (point-min) (point-max)
3246 property value))
3247 (delete-region
3248 beg (or (next-single-property-change beg 'org-filtered)
3249 (point-max))))))
20908596 3250
c8d0cf5c
CD
3251(defun org-agenda-add-entry-text ()
3252 "Add entry text to agenda lines.
3253This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3254entry text following headings shown in the agenda.
3255Drawers will be excluded, also the line with scheduling/deadline info."
8bfe682a
CD
3256 (when (and (> org-agenda-add-entry-text-maxlines 0)
3257 (not (org-bound-and-true-p org-mobile-creating-agendas)))
54a0dee5 3258 (let (m txt)
c8d0cf5c
CD
3259 (goto-char (point-min))
3260 (while (not (eobp))
8d642074 3261 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
c8d0cf5c 3262 (beginning-of-line 2)
54a0dee5 3263 (setq txt (org-agenda-get-some-entry-text
8d642074 3264 m org-agenda-add-entry-text-maxlines " > "))
c8d0cf5c 3265 (end-of-line 1)
afe98dfa
CD
3266 (if (string-match "\\S-" txt)
3267 (insert "\n" txt)
3268 (or (eobp) (forward-char 1))))))))
c8d0cf5c 3269
8d642074
CD
3270(defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3271 &rest keep)
54a0dee5 3272 "Extract entry text from MARKER, at most N-LINES lines.
8d642074
CD
3273This will ignore drawers etc, just get the text.
3274If INDENT is given, prefix every line with this string. If KEEP is
8bfe682a 3275given, it is a list of symbols, defining stuff that should not be
8d642074 3276removed from the entry content. Currently only `planning' is allowed here."
54a0dee5
CD
3277 (let (txt drawer-re kwd-time-re ind)
3278 (save-excursion
3279 (with-current-buffer (marker-buffer marker)
8223b1d2 3280 (if (not (derived-mode-p 'org-mode))
54a0dee5
CD
3281 (setq txt "")
3282 (save-excursion
3283 (save-restriction
3284 (widen)
3285 (goto-char marker)
8d642074 3286 (end-of-line 1)
54a0dee5 3287 (setq txt (buffer-substring
8d642074 3288 (min (1+ (point)) (point-max))
54a0dee5
CD
3289 (progn (outline-next-heading) (point)))
3290 drawer-re org-drawer-regexp
3291 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3292 ".*\n?"))
3293 (with-temp-buffer
3294 (insert txt)
3295 (when org-agenda-add-entry-text-descriptive-links
3296 (goto-char (point-min))
3297 (while (org-activate-bracket-links (point-max))
3298 (add-text-properties (match-beginning 0) (match-end 0)
3299 '(face org-link))))
3300 (goto-char (point-min))
3301 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3302 (set-text-properties (match-beginning 0) (match-end 0)
3303 nil))
3304 (goto-char (point-min))
3305 (while (re-search-forward drawer-re nil t)
3306 (delete-region
3307 (match-beginning 0)
3308 (progn (re-search-forward
3309 "^[ \t]*:END:.*\n?" nil 'move)
3310 (point))))
8d642074
CD
3311 (unless (member 'planning keep)
3312 (goto-char (point-min))
3313 (while (re-search-forward kwd-time-re nil t)
3314 (replace-match "")))
54a0dee5 3315 (goto-char (point-min))
8d642074
CD
3316 (when org-agenda-entry-text-exclude-regexps
3317 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3318 (while (setq re (pop re-list))
3319 (goto-char (point-min))
3320 (while (re-search-forward re nil t)
3321 (replace-match "")))))
3322 (goto-char (point-max))
3323 (skip-chars-backward " \t\n")
3324 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3325
3326 ;; find and remove min common indentation
54a0dee5
CD
3327 (goto-char (point-min))
3328 (untabify (point-min) (point-max))
3329 (setq ind (org-get-indentation))
3330 (while (not (eobp))
3331 (unless (looking-at "[ \t]*$")
3332 (setq ind (min ind (org-get-indentation))))
3333 (beginning-of-line 2))
3334 (goto-char (point-min))
3335 (while (not (eobp))
3336 (unless (looking-at "[ \t]*$")
3337 (move-to-column ind)
3338 (delete-region (point-at-bol) (point)))
3339 (beginning-of-line 2))
8d642074
CD
3340
3341 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3342
54a0dee5 3343 (goto-char (point-min))
8d642074
CD
3344 (when indent
3345 (while (and (not (eobp)) (re-search-forward "^" nil t))
3346 (replace-match indent t t)))
54a0dee5
CD
3347 (goto-char (point-min))
3348 (while (looking-at "[ \t]*\n") (replace-match ""))
3349 (goto-char (point-max))
3350 (when (> (org-current-line)
3351 n-lines)
3352 (org-goto-line (1+ n-lines))
3353 (backward-char 1))
3354 (setq txt (buffer-substring (point-min) (point)))))))))
3355 txt))
3356
20908596
CD
3357(defun org-agenda-collect-markers ()
3358 "Collect the markers pointing to entries in the agenda buffer."
3359 (let (m markers)
3360 (save-excursion
3361 (goto-char (point-min))
3362 (while (not (eobp))
8d642074
CD
3363 (when (setq m (or (org-get-at-bol 'org-hd-marker)
3364 (org-get-at-bol 'org-marker)))
20908596
CD
3365 (push m markers))
3366 (beginning-of-line 2)))
3367 (nreverse markers)))
3368
3369(defun org-create-marker-find-array (marker-list)
e66ba1df 3370 "Create a alist of files names with all marker positions in that file."
20908596
CD
3371 (let (f tbl m a p)
3372 (while (setq m (pop marker-list))
3373 (setq p (marker-position m)
3374 f (buffer-file-name (or (buffer-base-buffer
3375 (marker-buffer m))
3376 (marker-buffer m))))
3377 (if (setq a (assoc f tbl))
3378 (push (marker-position m) (cdr a))
3379 (push (list f p) tbl)))
3380 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3381 tbl)))
3382
33306645 3383(defvar org-agenda-marker-table nil) ; dynamically scoped parameter
20908596
CD
3384(defun org-check-agenda-marker-table ()
3385 "Check of the current entry is on the marker list."
3386 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3387 a)
3388 (and (setq a (assoc file org-agenda-marker-table))
3389 (save-match-data
3390 (save-excursion
3391 (org-back-to-heading t)
3392 (member (point) (cdr a)))))))
3393
3394(defun org-check-for-org-mode ()
e66ba1df 3395 "Make sure current buffer is in org-mode. Error if not."
8223b1d2 3396 (or (derived-mode-p 'org-mode)
f924a367 3397 (error "Cannot execute org-mode agenda command on buffer in %s"
20908596
CD
3398 major-mode)))
3399
20908596
CD
3400;;; Agenda prepare and finalize
3401
33306645 3402(defvar org-agenda-multi nil) ; dynamically scoped
8223b1d2 3403(defvar org-agenda-pre-window-conf nil)
20908596
CD
3404(defvar org-agenda-columns-active nil)
3405(defvar org-agenda-name nil)
e66ba1df
BG
3406(defvar org-agenda-tag-filter nil)
3407(defvar org-agenda-category-filter nil)
8223b1d2 3408(defvar org-agenda-top-category-filter nil)
e66ba1df
BG
3409(defvar org-agenda-tag-filter-while-redo nil)
3410(defvar org-agenda-tag-filter-preset nil
c8d0cf5c 3411 "A preset of the tags filter used for secondary agenda filtering.
86fbb8ca 3412This must be a list of strings, each string must be a single tag preceded
c8d0cf5c
CD
3413by \"+\" or \"-\".
3414This variable should not be set directly, but agenda custom commands can
afe98dfa
CD
3415bind it in the options section. The preset filter is a global property of
3416the entire agenda view. In a block agenda, it will not work reliably to
3417define a filter for one of the individual blocks. You need to set it in
3418the global options and expect it to be applied to the entire view.")
c8d0cf5c 3419
e66ba1df 3420(defvar org-agenda-category-filter-preset nil
27e428e7 3421 "A preset of the category filter used for secondary agenda filtering.
e66ba1df
BG
3422This must be a list of strings, each string must be a single category
3423preceded by \"+\" or \"-\".
3424This variable should not be set directly, but agenda custom commands can
3425bind it in the options section. The preset filter is a global property of
3426the entire agenda view. In a block agenda, it will not work reliably to
3427define a filter for one of the individual blocks. You need to set it in
3428the global options and expect it to be applied to the entire view.")
3429
8223b1d2
BG
3430
3431(defun org-agenda-use-sticky-p ()
3432 "Return non-nil if an agenda buffer named
3433`org-agenda-buffer-name' exists and should be shown instead of
3434generating a new one."
3435 (and
3436 ;; turned off by user
3437 org-agenda-sticky
3438 ;; For multi-agenda buffer already exists
3439 (not org-agenda-multi)
3440 ;; buffer found
3441 (get-buffer org-agenda-buffer-name)
3442 ;; C-u parameter is same as last call
3443 (with-current-buffer (get-buffer org-agenda-buffer-name)
3444 (and
3445 (equal current-prefix-arg
3446 org-agenda-last-prefix-arg)
3447 ;; In case user turned stickiness on, while having existing
3448 ;; Agenda buffer active, don't reuse that buffer, because it
3449 ;; does not have org variables local
3450 org-agenda-this-buffer-is-sticky))))
3451
3452(defun org-agenda-prepare-window (abuf)
3453 "Setup agenda buffer in the window."
3454 (let* ((awin (get-buffer-window abuf))
3455 wconf)
3456 (cond
3457 ((equal (current-buffer) abuf) nil)
3458 (awin (select-window awin))
3459 ((not (setq wconf (current-window-configuration))))
3460 ((equal org-agenda-window-setup 'current-window)
3461 (org-pop-to-buffer-same-window abuf))
3462 ((equal org-agenda-window-setup 'other-window)
3463 (org-switch-to-buffer-other-window abuf))
3464 ((equal org-agenda-window-setup 'other-frame)
3465 (switch-to-buffer-other-frame abuf))
3466 ((equal org-agenda-window-setup 'reorganize-frame)
3467 (delete-other-windows)
3468 (org-switch-to-buffer-other-window abuf)))
3469 ;; additional test in case agenda is invoked from within agenda
3470 ;; buffer via elisp link
3471 (unless (equal (current-buffer) abuf)
3472 (org-pop-to-buffer-same-window abuf))
3473 (setq org-agenda-pre-window-conf
3474 (or org-agenda-pre-window-conf wconf))))
3475
3476(defun org-agenda-prepare (&optional name)
3477 (if (org-agenda-use-sticky-p)
20908596 3478 (progn
8223b1d2
BG
3479 ;; Popup existing buffer
3480 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3481 (message "Sticky Agenda buffer, use `r' to refresh")
3482 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3483 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3484 (setq org-todo-keywords-for-agenda nil)
3485 (setq org-drawers-for-agenda nil)
3486 (unless org-agenda-persistent-filter
3487 (setq org-agenda-tag-filter nil
3488 org-agenda-category-filter nil))
3489 (put 'org-agenda-tag-filter :preset-filter
3490 org-agenda-tag-filter-preset)
3491 (put 'org-agenda-category-filter :preset-filter
3492 org-agenda-category-filter-preset)
3493 (if org-agenda-multi
3494 (progn
3495 (setq buffer-read-only nil)
3496 (goto-char (point-max))
3497 (unless (or (bobp) org-agenda-compact-blocks
3498 (not org-agenda-block-separator))
3499 (insert "\n"
3500 (if (stringp org-agenda-block-separator)
3501 org-agenda-block-separator
3502 (make-string (window-width) org-agenda-block-separator))
3503 "\n"))
3504 (narrow-to-region (point) (point-max)))
3505 (setq org-done-keywords-for-agenda nil)
3506
3507 ;; Setting any org variables that are in org-agenda-local-vars
3508 ;; list need to be done after the prepare call
3509 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3510 (setq buffer-read-only nil)
3511 (org-agenda-reset-markers)
3512 (let ((inhibit-read-only t)) (erase-buffer))
3513 (org-agenda-mode)
3514 (setq org-agenda-buffer (current-buffer))
3515 (setq org-agenda-contributing-files nil)
3516 (setq org-agenda-columns-active nil)
3517 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3518 (setq org-todo-keywords-for-agenda
3519 (org-uniquify org-todo-keywords-for-agenda))
3520 (setq org-done-keywords-for-agenda
3521 (org-uniquify org-done-keywords-for-agenda))
3522 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3523 (setq org-agenda-last-prefix-arg current-prefix-arg)
3524 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3525 (and name (not org-agenda-name)
3526 (org-set-local 'org-agenda-name name)))
3527 (setq buffer-read-only nil)))
3528
3529(defvar org-agenda-overriding-columns-format) ; From org-colview.el
3530(defun org-agenda-finalize ()
20908596
CD
3531 "Finishing touch for the agenda buffer, called just before displaying it."
3532 (unless org-agenda-multi
3533 (save-excursion
3534 (let ((inhibit-read-only t))
3535 (goto-char (point-min))
c7cf0ebc
BG
3536 (save-excursion
3537 (while (org-activate-bracket-links (point-max))
3538 (add-text-properties (match-beginning 0) (match-end 0)
3539 '(face org-link))))
3540 (save-excursion
3541 (while (org-activate-plain-links (point-max))
3542 (add-text-properties (match-beginning 0) (match-end 0)
3543 '(face org-link))))
3544 (unless (eq org-agenda-remove-tags t)
3545 (org-agenda-align-tags))
20908596
CD
3546 (unless org-agenda-with-colors
3547 (remove-text-properties (point-min) (point-max) '(face nil))))
33306645
CD
3548 (if (and (boundp 'org-agenda-overriding-columns-format)
3549 org-agenda-overriding-columns-format)
3550 (org-set-local 'org-agenda-overriding-columns-format
3551 org-agenda-overriding-columns-format))
20908596
CD
3552 (if (and (boundp 'org-agenda-view-columns-initially)
3553 org-agenda-view-columns-initially)
3554 (org-agenda-columns))
3555 (when org-agenda-fontify-priorities
c8d0cf5c 3556 (org-agenda-fontify-priorities))
d6685abc 3557 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
c7cf0ebc 3558 (org-agenda-dim-blocked-tasks))
63aa0982
BG
3559 ;; We need to widen when `org-agenda-finalize' is called from
3560 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
c7cf0ebc
BG
3561 (when org-clock-current-task
3562 (save-restriction
3563 (widen)
3564 (org-agenda-mark-clocking-task)))
54a0dee5
CD
3565 (when org-agenda-entry-text-mode
3566 (org-agenda-entry-text-hide)
f924a367 3567 (org-agenda-entry-text-show))
c7cf0ebc
BG
3568 (if (and (functionp 'org-habit-insert-consistency-graphs)
3569 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
8bfe682a 3570 (org-habit-insert-consistency-graphs))
c7cf0ebc
BG
3571 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3572 (when (delq nil (mapcar (lambda (tp) (org-agenda-check-type nil tp))
3573 org-agenda-use-tag-inheritance))
3574 (let (mrk)
3575 (save-excursion
3576 (goto-char (point-min))
3577 (while (equal (forward-line) 0)
3578 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3579 (get-text-property (point) 'org-hd-marker)))
3580 (put-text-property (point-at-bol) (point-at-eol)
3581 'tags (org-with-point-at mrk
3582 (delete-dups
3583 (mapcar 'downcase (org-get-tags-at))))))))))
8223b1d2
BG
3584 (let ((inhibit-read-only t))
3585 (run-hooks 'org-agenda-finalize-hook))
e66ba1df
BG
3586 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3587 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3588 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3589 (org-agenda-filter-apply org-agenda-category-filter 'category))
8223b1d2 3590 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local))))
20908596 3591
54a0dee5
CD
3592(defun org-agenda-mark-clocking-task ()
3593 "Mark the current clock entry in the agenda if it is present."
3594 (mapc (lambda (o)
86fbb8ca
CD
3595 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3596 (delete-overlay o)))
3597 (overlays-in (point-min) (point-max)))
54a0dee5
CD
3598 (when (marker-buffer org-clock-hd-marker)
3599 (save-excursion
3600 (goto-char (point-min))
3601 (let (s ov)
3602 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3603 (goto-char s)
8d642074 3604 (when (equal (org-get-at-bol 'org-hd-marker)
54a0dee5 3605 org-clock-hd-marker)
86fbb8ca
CD
3606 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3607 (overlay-put ov 'type 'org-agenda-clocking)
3608 (overlay-put ov 'face 'org-agenda-clocking)
3609 (overlay-put ov 'help-echo
8223b1d2 3610 "The clock is running in this item")))))))
54a0dee5 3611
c8d0cf5c 3612(defun org-agenda-fontify-priorities ()
20908596
CD
3613 "Make highest priority lines bold, and lowest italic."
3614 (interactive)
86fbb8ca
CD
3615 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3616 (delete-overlay o)))
3617 (overlays-in (point-min) (point-max)))
20908596
CD
3618 (save-excursion
3619 (let ((inhibit-read-only t)
3620 b e p ov h l)
3621 (goto-char (point-min))
3622 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3623 (setq h (or (get-char-property (point) 'org-highest-priority)
3624 org-highest-priority)
3625 l (or (get-char-property (point) 'org-lowest-priority)
3626 org-lowest-priority)
3627 p (string-to-char (match-string 1))
c8d0cf5c
CD
3628 b (match-beginning 0)
3629 e (if (eq org-agenda-fontify-priorities 'cookies)
3630 (match-end 0)
3631 (point-at-eol))
86fbb8ca
CD
3632 ov (make-overlay b e))
3633 (overlay-put
20908596 3634 ov 'face
ed21c5c8
CD
3635 (cond ((org-face-from-face-or-color
3636 'priority nil
3637 (cdr (assoc p org-priority-faces))))
c8d0cf5c 3638 ((and (listp org-agenda-fontify-priorities)
ed21c5c8
CD
3639 (org-face-from-face-or-color
3640 'priority nil
3641 (cdr (assoc p org-agenda-fontify-priorities)))))
20908596
CD
3642 ((equal p l) 'italic)
3643 ((equal p h) 'bold)))
86fbb8ca 3644 (overlay-put ov 'org-type 'org-priority)))))
20908596 3645
c7cf0ebc
BG
3646(defun org-agenda-dim-blocked-tasks (&optional invisible)
3647 (interactive "P")
d6685abc 3648 "Dim currently blocked TODO's in the agenda display."
c7cf0ebc 3649 (message "Dim or hide blocked tasks...")
86fbb8ca
CD
3650 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3651 (delete-overlay o)))
3652 (overlays-in (point-min) (point-max)))
d6685abc
CD
3653 (save-excursion
3654 (let ((inhibit-read-only t)
72d06d81 3655 (org-depend-tag-blocked nil)
c7cf0ebc
BG
3656 (invis (or (not (null invisible))
3657 (eq org-agenda-dim-blocked-tasks 'invisible)))
c8d0cf5c
CD
3658 org-blocked-by-checkboxes
3659 invis1 b e p ov h l)
d6685abc
CD
3660 (goto-char (point-min))
3661 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3662 (and pos (goto-char (1+ pos))))
c8d0cf5c 3663 (setq org-blocked-by-checkboxes nil invis1 invis)
8d642074 3664 (let ((marker (org-get-at-bol 'org-hd-marker)))
d6685abc 3665 (when (and marker
e66ba1df
BG
3666 (with-current-buffer (marker-buffer marker)
3667 (save-excursion (goto-char marker)
3668 (org-entry-blocked-p))))
c8d0cf5c 3669 (if org-blocked-by-checkboxes (setq invis1 nil))
53e31a31
CD
3670 (setq b (if invis1
3671 (max (point-min) (1- (point-at-bol)))
3672 (point-at-bol))
d6685abc 3673 e (point-at-eol)
86fbb8ca 3674 ov (make-overlay b e))
c8d0cf5c 3675 (if invis1
86fbb8ca
CD
3676 (overlay-put ov 'invisible t)
3677 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
c7cf0ebc
BG
3678 (overlay-put ov 'org-type 'org-blocked-todo))))))
3679 (message "Dim or hide blocked tasks...done"))
20908596
CD
3680
3681(defvar org-agenda-skip-function nil
3682 "Function to be called at each match during agenda construction.
3683If this function returns nil, the current match should not be skipped.
3684Otherwise, the function must return a position from where the search
3685should be continued.
3686This may also be a Lisp form, it will be evaluated.
3687Never set this variable using `setq' or so, because then it will apply
3ab2c837
BG
3688to all future agenda commands. If you do want a global skipping condition,
3689use the option `org-agenda-skip-function-global' instead.
3690The correct usage for `org-agenda-skip-function' is to bind it with
3691`let' to scope it dynamically into the agenda-constructing command.
3692A good way to set it is through options in `org-agenda-custom-commands'.")
20908596
CD
3693
3694(defun org-agenda-skip ()
3695 "Throw to `:skip' in places that should be skipped.
3696Also moves point to the end of the skipped region, so that search can
3697continue from there."
3ab2c837 3698 (let ((p (point-at-bol)) to)
c7cf0ebc 3699 (when (org-in-src-block-p t) (throw :skip t))
2c3ad40d 3700 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
20908596
CD
3701 (get-text-property p :org-archived)
3702 (org-end-of-subtree t)
3703 (throw :skip t))
b349f79f
CD
3704 (and org-agenda-skip-comment-trees
3705 (get-text-property p :org-comment)
20908596
CD
3706 (org-end-of-subtree t)
3707 (throw :skip t))
3708 (if (equal (char-after p) ?#) (throw :skip t))
3ab2c837
BG
3709 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3710 (org-agenda-skip-eval org-agenda-skip-function)))
20908596
CD
3711 (goto-char to)
3712 (throw :skip t))))
3713
3ab2c837
BG
3714(defun org-agenda-skip-eval (form)
3715 "If FORM is a function or a list, call (or eval) is and return result.
3716`save-excursion' and `save-match-data' are wrapped around the call, so point
3717and match data are returned to the previous state no matter what these
3718functions do."
3719 (let (fp)
3720 (and form
3721 (or (setq fp (functionp form))
3722 (consp form))
3723 (save-excursion
3724 (save-match-data
3725 (if fp
3726 (funcall form)
3727 (eval form)))))))
3728
20908596
CD
3729(defvar org-agenda-markers nil
3730 "List of all currently active markers created by `org-agenda'.")
54a0dee5 3731(defvar org-agenda-last-marker-time (org-float-time)
20908596
CD
3732 "Creation time of the last agenda marker.")
3733
3734(defun org-agenda-new-marker (&optional pos)
3735 "Return a new agenda marker.
3736Org-mode keeps a list of these markers and resets them when they are
3737no longer in use."
3738 (let ((m (copy-marker (or pos (point)))))
54a0dee5 3739 (setq org-agenda-last-marker-time (org-float-time))
8223b1d2
BG
3740 (if org-agenda-buffer
3741 (with-current-buffer org-agenda-buffer
3742 (push m org-agenda-markers))
3743 (push m org-agenda-markers))
20908596
CD
3744 m))
3745
3746(defun org-agenda-reset-markers ()
3747 "Reset markers created by `org-agenda'."
3748 (while org-agenda-markers
3749 (move-marker (pop org-agenda-markers) nil)))
3750
b349f79f 3751(defun org-agenda-save-markers-for-cut-and-paste (beg end)
8223b1d2
BG
3752 "Save relative positions of markers in region.
3753This check for agenda markers in all agenda buffers currently active."
3754 (dolist (buf (buffer-list))
3755 (with-current-buffer buf
3756 (when (eq major-mode 'org-agenda-mode)
3757 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3758 org-agenda-markers)))))
b349f79f 3759
54a0dee5
CD
3760;;; Entry text mode
3761
3762(defun org-agenda-entry-text-show-here ()
8bfe682a 3763 "Add some text from the entry as context to the current line."
54a0dee5 3764 (let (m txt o)
8d642074 3765 (setq m (org-get-at-bol 'org-hd-marker))
54a0dee5
CD
3766 (unless (marker-buffer m)
3767 (error "No marker points to an entry here"))
3768 (setq txt (concat "\n" (org-no-properties
3769 (org-agenda-get-some-entry-text
8d642074 3770 m org-agenda-entry-text-maxlines " > "))))
54a0dee5 3771 (when (string-match "\\S-" txt)
86fbb8ca
CD
3772 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3773 (overlay-put o 'evaporate t)
3774 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3775 (overlay-put o 'after-string txt))))
54a0dee5
CD
3776
3777(defun org-agenda-entry-text-show ()
3778 "Add entry context for all agenda lines."
3779 (interactive)
3780 (save-excursion
3781 (goto-char (point-max))
3782 (beginning-of-line 1)
3783 (while (not (bobp))
8d642074 3784 (when (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
3785 (org-agenda-entry-text-show-here))
3786 (beginning-of-line 0))))
3787
3788(defun org-agenda-entry-text-hide ()
3789 "Remove any shown entry context."
3790 (delq nil
3791 (mapcar (lambda (o)
86fbb8ca 3792 (if (eq (overlay-get o 'org-overlay-type)
54a0dee5 3793 'agenda-entry-content)
86fbb8ca
CD
3794 (progn (delete-overlay o) t)))
3795 (overlays-in (point-min) (point-max)))))
54a0dee5 3796
acedf35c
CD
3797(defun org-agenda-get-day-face (date)
3798 "Return the face DATE should be displayed with."
3799 (or (and (functionp org-agenda-day-face-function)
3800 (funcall org-agenda-day-face-function date))
3801 (cond ((org-agenda-todayp date)
3802 'org-agenda-date-today)
3803 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3804 'org-agenda-date-weekend)
3805 (t 'org-agenda-date))))
3806
20908596
CD
3807;;; Agenda timeline
3808
3809(defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3810
e66ba1df 3811(defun org-timeline (&optional dotodo)
20908596
CD
3812 "Show a time-sorted view of the entries in the current org file.
3813Only entries with a time stamp of today or later will be listed. With
3814\\[universal-argument] prefix, all unfinished TODO items will also be shown,
3815under the current date.
3816If the buffer contains an active region, only check the region for
3817dates."
3818 (interactive "P")
20908596 3819 (let* ((dopast t)
8223b1d2 3820 (org-agenda-show-log-scoped org-agenda-show-log)
afe98dfa
CD
3821 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3822 (current-buffer))))
20908596
CD
3823 (date (calendar-current-date))
3824 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3825 (end (if (org-region-active-p) (region-end) (point-max)))
8223b1d2
BG
3826 (day-numbers (org-get-all-dates
3827 beg end 'no-ranges
3828 t org-agenda-show-log-scoped ; always include today
3829 org-timeline-show-empty-dates))
20908596
CD
3830 (org-deadline-warning-days 0)
3831 (org-agenda-only-exact-dates t)
acedf35c 3832 (today (org-today))
20908596
CD
3833 (past t)
3834 args
acedf35c 3835 s e rtn d emptyp)
20908596
CD
3836 (setq org-agenda-redo-command
3837 (list 'progn
3838 (list 'org-switch-to-buffer-other-window (current-buffer))
e66ba1df 3839 (list 'org-timeline (list 'quote dotodo))))
20908596
CD
3840 (if (not dopast)
3841 ;; Remove past dates from the list of dates.
3842 (setq day-numbers (delq nil (mapcar (lambda(x)
3843 (if (>= x today) x nil))
3844 day-numbers))))
8223b1d2
BG
3845 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
3846 (org-compile-prefix-format 'timeline)
3847 (org-set-sorting-strategy 'timeline)
3848 (if org-agenda-show-log-scoped (push :closed args))
20908596
CD
3849 (push :timestamp args)
3850 (push :deadline args)
3851 (push :scheduled args)
3852 (push :sexp args)
3853 (if dotodo (push :todo args))
8d642074
CD
3854 (insert "Timeline of file " entry "\n")
3855 (add-text-properties (point-min) (point)
3856 (list 'face 'org-agenda-structure))
3857 (org-agenda-mark-header-line (point-min))
20908596
CD
3858 (while (setq d (pop day-numbers))
3859 (if (and (listp d) (eq (car d) :omitted))
3860 (progn
3861 (setq s (point))
3862 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3863 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3864 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3865 (if (and (>= d today)
3866 dopast
3867 past)
3868 (progn
3869 (setq past nil)
3870 (insert (make-string 79 ?-) "\n")))
acedf35c 3871 (setq date (calendar-gregorian-from-absolute d))
20908596
CD
3872 (setq s (point))
3873 (setq rtn (and (not emptyp)
3874 (apply 'org-agenda-get-day-entries entry
3875 date args)))
3876 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3877 (progn
3878 (insert
3879 (if (stringp org-agenda-format-date)
3880 (format-time-string org-agenda-format-date
3881 (org-time-from-absolute date))
3882 (funcall org-agenda-format-date date))
3883 "\n")
3884 (put-text-property s (1- (point)) 'face
acedf35c 3885 (org-agenda-get-day-face date))
20908596 3886 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3887 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596
CD
3888 (if (equal d today)
3889 (put-text-property s (1- (point)) 'org-today t))
8223b1d2 3890 (and rtn (insert (org-agenda-finalize-entries rtn) "\n"))
20908596
CD
3891 (put-text-property s (1- (point)) 'day d)))))
3892 (goto-char (point-min))
3893 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3894 (point-min)))
3895 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
8223b1d2 3896 (org-agenda-finalize)
20908596
CD
3897 (setq buffer-read-only t)))
3898
3899(defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3900 "Return a list of all relevant day numbers from BEG to END buffer positions.
3901If NO-RANGES is non-nil, include only the start and end dates of a range,
3902not every single day in the range. If FORCE-TODAY is non-nil, make
3903sure that TODAY is included in the list. If INACTIVE is non-nil, also
3904inactive time stamps (those in square brackets) are included.
3905When EMPTY is non-nil, also include days without any entries."
3906 (let ((re (concat
3907 (if pre-re pre-re "")
3908 (if inactive org-ts-regexp-both org-ts-regexp)))
8223b1d2 3909 dates dates1 date day day1 day2 ts1 ts2 pos)
20908596 3910 (if force-today
acedf35c 3911 (setq dates (list (org-today))))
20908596
CD
3912 (save-excursion
3913 (goto-char beg)
3914 (while (re-search-forward re end t)
3915 (setq day (time-to-days (org-time-string-to-time
e66ba1df
BG
3916 (substring (match-string 1) 0 10)
3917 (current-buffer) (match-beginning 0))))
20908596
CD
3918 (or (memq day dates) (push day dates)))
3919 (unless no-ranges
3920 (goto-char beg)
3921 (while (re-search-forward org-tr-regexp end t)
e66ba1df 3922 (setq pos (match-beginning 0))
20908596
CD
3923 (setq ts1 (substring (match-string 1) 0 10)
3924 ts2 (substring (match-string 2) 0 10)
e66ba1df
BG
3925 day1 (time-to-days (org-time-string-to-time
3926 ts1 (current-buffer) pos))
3927 day2 (time-to-days (org-time-string-to-time
3928 ts2 (current-buffer) pos)))
20908596
CD
3929 (while (< (setq day1 (1+ day1)) day2)
3930 (or (memq day1 dates) (push day1 dates)))))
3931 (setq dates (sort dates '<))
3932 (when empty
3933 (while (setq day (pop dates))
3934 (setq day2 (car dates))
3935 (push day dates1)
3936 (when (and day2 empty)
3937 (if (or (eq empty t)
3938 (and (numberp empty) (<= (- day2 day) empty)))
3939 (while (< (setq day (1+ day)) day2)
3940 (push (list day) dates1))
3941 (push (cons :omitted (- day2 day)) dates1))))
3942 (setq dates (nreverse dates1)))
3943 dates)))
3944
3945;;; Agenda Daily/Weekly
3946
c8d0cf5c 3947(defvar org-agenda-start-day nil ; dynamically scoped parameter
8223b1d2 3948 "Start day for the agenda view.
3ab2c837 3949Custom commands can set this variable in the options section.")
20908596 3950(defvar org-starting-day nil) ; local variable in the agenda buffer
e66ba1df 3951(defvar org-arg-loc nil) ; local variable
20908596 3952
ed21c5c8
CD
3953(defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3954 "List of types searched for when creating the daily/weekly agenda.
3955This variable is a list of symbols that controls the types of
3956items that appear in the daily/weekly agenda. Allowed symbols in this
3957list are are
3958
3959 :timestamp List items containing a date stamp or date range matching
3960 the selected date. This includes sexp entries in
3961 angular brackets.
3962
3963 :sexp List entries resulting from plain diary-like sexps.
3964
3965 :deadline List deadline due on that date. When the date is today,
3966 also list any deadlines past due, or due within
3967 `org-deadline-warning-days'. `:deadline' must appear before
3968 `:scheduled' if the setting of
3969 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3970 any effect.
3971
3972 :scheduled List all items which are scheduled for the given date.
3973 The diary for *today* also contains items which were
3974 scheduled earlier and are not yet marked DONE.
3975
3976By default, all four types are turned on.
3977
3978Never set this variable globally using `setq', because then it
3979will apply to all future agenda commands. Instead, bind it with
9b053e76 3980`let' to scope it dynamically into the agenda-constructing
ed21c5c8
CD
3981command. A good way to set it is through options in
3982`org-agenda-custom-commands'. For a more flexible (though
3983somewhat less efficient) way of determining what is included in
3984the daily/weekly agenda, see `org-agenda-skip-function'.")
3985
8223b1d2 3986(defvar org-agenda-buffer-tmp-name nil)
20908596 3987;;;###autoload
e66ba1df 3988(defun org-agenda-list (&optional arg start-day span)
20908596
CD
3989 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3990The view will be for the current day or week, but from the overview buffer
3991you will be able to go to other days/weeks.
3992
20908596 3993With a numeric prefix argument in an interactive call, the agenda will
e66ba1df 3994span ARG days. Lisp programs should instead specify SPAN to change
acedf35c 3995the number of days. SPAN defaults to `org-agenda-span'.
20908596
CD
3996
3997START-DAY defaults to TODAY, or to the most recent match for the weekday
3998given in `org-agenda-start-on-weekday'."
3999 (interactive "P")
20908596 4000 (if org-agenda-overriding-arguments
e66ba1df 4001 (setq arg (car org-agenda-overriding-arguments)
20908596 4002 start-day (nth 1 org-agenda-overriding-arguments)
acedf35c 4003 span (nth 2 org-agenda-overriding-arguments)))
8223b1d2
BG
4004 (if (and (integerp arg) (> arg 0))
4005 (setq span arg arg nil))
4006 (catch 'exit
4007 (setq org-agenda-buffer-name
4008 (or org-agenda-buffer-tmp-name
4009 (if org-agenda-sticky
4010 (cond ((and org-keys (stringp org-match))
4011 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4012 (org-keys
4013 (format "*Org Agenda(%s)*" org-keys))
4014 (t "*Org Agenda(a)*")))
4015 org-agenda-buffer-name))
4016 (org-agenda-prepare "Day/Week")
4017 (setq start-day (or start-day org-agenda-start-day))
4018 (if (stringp start-day)
4019 ;; Convert to an absolute day number
4020 (setq start-day (time-to-days (org-read-date nil t start-day))))
4021 (org-compile-prefix-format 'agenda)
4022 (org-set-sorting-strategy 'agenda)
4023 (let* ((span (org-agenda-ndays-to-span
4024 (or span org-agenda-ndays org-agenda-span)))
4025 (today (org-today))
4026 (sd (or start-day today))
4027 (ndays (org-agenda-span-to-ndays span sd))
4028 (org-agenda-start-on-weekday
4029 (if (eq ndays 7)
4030 org-agenda-start-on-weekday))
4031 (thefiles (org-agenda-files nil 'ifmode))
4032 (files thefiles)
4033 (start (if (or (null org-agenda-start-on-weekday)
4034 (< ndays 7))
4035 sd
4036 (let* ((nt (calendar-day-of-week
4037 (calendar-gregorian-from-absolute sd)))
4038 (n1 org-agenda-start-on-weekday)
4039 (d (- nt n1)))
4040 (- sd (+ (if (< d 0) 7 0) d)))))
4041 (day-numbers (list start))
4042 (day-cnt 0)
4043 (inhibit-redisplay (not debug-on-error))
4044 (org-agenda-show-log-scoped org-agenda-show-log)
4045 s e rtn rtnall file date d start-pos end-pos todayp
4046 clocktable-start clocktable-end filter)
4047 (setq org-agenda-redo-command
4048 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
4049 (dotimes (n (1- ndays))
4050 (push (1+ (car day-numbers)) day-numbers))
4051 (setq day-numbers (nreverse day-numbers))
4052 (setq clocktable-start (car day-numbers)
4053 clocktable-end (1+ (or (org-last day-numbers) 0)))
4054 (org-set-local 'org-starting-day (car day-numbers))
4055 (org-set-local 'org-arg-loc arg)
4056 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4057 (unless org-agenda-compact-blocks
4058 (let* ((d1 (car day-numbers))
4059 (d2 (org-last day-numbers))
4060 (w1 (org-days-to-iso-week d1))
4061 (w2 (org-days-to-iso-week d2)))
4062 (setq s (point))
4063 (if org-agenda-overriding-header
4064 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4065 nil 'face 'org-agenda-structure) "\n")
4066 (insert (org-agenda-span-name span)
4067 "-agenda"
4068 (if (< (- d2 d1) 350)
4069 (if (= w1 w2)
4070 (format " (W%02d)" w1)
4071 (format " (W%02d-W%02d)" w1 w2))
4072 "")
4073 ":\n")))
4074 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4075 'org-date-line t))
4076 (org-agenda-mark-header-line s))
4077 (while (setq d (pop day-numbers))
4078 (setq date (calendar-gregorian-from-absolute d)
4079 s (point))
4080 (if (or (setq todayp (= d today))
4081 (and (not start-pos) (= d sd)))
4082 (setq start-pos (point))
4083 (if (and start-pos (not end-pos))
4084 (setq end-pos (point))))
4085 (setq files thefiles
4086 rtnall nil)
4087 (while (setq file (pop files))
4088 (catch 'nextfile
4089 (org-check-agenda-file file)
4090 (let ((org-agenda-entry-types org-agenda-entry-types))
4091 (unless org-agenda-include-deadlines
4092 (setq org-agenda-entry-types
4093 (delq :deadline org-agenda-entry-types)))
4094 (cond
4095 ((memq org-agenda-show-log-scoped '(only clockcheck))
4096 (setq rtn (org-agenda-get-day-entries
4097 file date :closed)))
4098 (org-agenda-show-log-scoped
4099 (setq rtn (apply 'org-agenda-get-day-entries
4100 file date
4101 (append '(:closed) org-agenda-entry-types))))
4102 (t
4103 (setq rtn (apply 'org-agenda-get-day-entries
4104 file date
4105 org-agenda-entry-types)))))
4106 (setq rtnall (append rtnall rtn)))) ;; all entries
4107 (if org-agenda-include-diary
4108 (let ((org-agenda-search-headline-for-time t))
4109 (require 'diary-lib)
4110 (setq rtn (org-get-entries-from-diary date))
4111 (setq rtnall (append rtnall rtn))))
4112 (if (or rtnall org-agenda-show-all-dates)
4113 (progn
4114 (setq day-cnt (1+ day-cnt))
4115 (insert
4116 (if (stringp org-agenda-format-date)
4117 (format-time-string org-agenda-format-date
4118 (org-time-from-absolute date))
4119 (funcall org-agenda-format-date date))
4120 "\n")
4121 (put-text-property s (1- (point)) 'face
4122 (org-agenda-get-day-face date))
4123 (put-text-property s (1- (point)) 'org-date-line t)
4124 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4125 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4126 (when todayp
4127 (put-text-property s (1- (point)) 'org-today t))
4128 (setq rtnall
4129 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4130 (if rtnall (insert ;; all entries
4131 (org-agenda-finalize-entries rtnall)
4132 "\n"))
4133 (put-text-property s (1- (point)) 'day d)
4134 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4135 (when (and org-agenda-clockreport-mode clocktable-start)
4136 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4137 ;; the above line is to ensure the restricted range!
4138 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4139 tbl)
4140 (setq p (org-plist-delete p :block))
4141 (setq p (plist-put p :tstart clocktable-start))
4142 (setq p (plist-put p :tend clocktable-end))
4143 (setq p (plist-put p :scope 'agenda))
4144 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4145 (setq filter (or org-agenda-tag-filter-while-redo
4146 (get 'org-agenda-tag-filter :preset-filter))))
4147 (setq p (plist-put p :tags (mapconcat (lambda (x)
4148 (if (string-match "[<>=]" x)
4149 ""
4150 x))
4151 filter ""))))
bdebdb64 4152 (setq tbl (apply 'org-clock-get-clocktable p))
8223b1d2
BG
4153 (insert tbl)))
4154 (goto-char (point-min))
4155 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4156 (unless (and (pos-visible-in-window-p (point-min))
4157 (pos-visible-in-window-p (point-max)))
4158 (goto-char (1- (point-max)))
4159 (recenter -1)
4160 (if (not (pos-visible-in-window-p (or start-pos 1)))
4161 (progn
4162 (goto-char (or start-pos 1))
4163 (recenter 1))))
4164 (goto-char (or start-pos 1))
4165 (add-text-properties (point-min) (point-max)
4166 `(org-agenda-type agenda
4167 org-last-args (,arg ,start-day ,span)
4168 org-redo-cmd ,org-agenda-redo-command
735135f9 4169 org-series-cmd ,org-cmd))
8223b1d2
BG
4170 (if (eq org-agenda-show-log-scoped 'clockcheck)
4171 (org-agenda-show-clocking-issues))
4172 (org-agenda-finalize)
4173 (setq buffer-read-only t)
4174 (message ""))))
20908596 4175
bdebdb64
BG
4176(autoload 'org-agenda-list "org-agenda" "\
4177Produce a daily/weekly view from all files in variable `org-agenda-files'.
4178The view will be for the current day or week, but from the overview buffer
4179you will be able to go to other days/weeks.
4180
4181With a numeric prefix argument in an interactive call, the agenda will
4182span ARG days. Lisp programs should instead specify SPAN to change
4183the number of days. SPAN defaults to `org-agenda-span'.
4184
4185START-DAY defaults to TODAY, or to the most recent match for the weekday
4186given in `org-agenda-start-on-weekday'.
4187
4188\(fn &optional ARG START-DAY SPAN)" t nil)
4189
20908596 4190(defun org-agenda-ndays-to-span (n)
acedf35c
CD
4191 "Return a span symbol for a span of N days, or N if none matches."
4192 (cond ((symbolp n) n)
4193 ((= n 1) 'day)
4194 ((= n 7) 'week)
4195 (t n)))
4196
8223b1d2
BG
4197(defun org-agenda-span-to-ndays (span &optional start-day)
4198 "Return ndays from SPAN, possibly starting at START-DAY."
acedf35c
CD
4199 (cond ((numberp span) span)
4200 ((eq span 'day) 1)
4201 ((eq span 'week) 7)
4202 ((eq span 'month)
4203 (let ((date (calendar-gregorian-from-absolute start-day)))
4204 (calendar-last-day-of-month (car date) (caddr date))))
4205 ((eq span 'year)
4206 (let ((date (calendar-gregorian-from-absolute start-day)))
4207 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4208
4209(defun org-agenda-span-name (span)
4210 "Return a SPAN name."
4211 (if (null span)
4212 ""
4213 (if (symbolp span)
4214 (capitalize (symbol-name span))
4215 (format "%d days" span))))
20908596
CD
4216
4217;;; Agenda word search
4218
4219(defvar org-agenda-search-history nil)
20908596
CD
4220
4221(defvar org-search-syntax-table nil
e66ba1df
BG
4222 "Special syntax table for org-mode search.
4223In this table, we have single quotes not as word constituents, to
4224that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
20908596 4225
8223b1d2 4226(defvar org-mode-syntax-table) ; From org.el
20908596
CD
4227(defun org-search-syntax-table ()
4228 (unless org-search-syntax-table
4229 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4230 (modify-syntax-entry ?' "." org-search-syntax-table)
4231 (modify-syntax-entry ?` "." org-search-syntax-table))
4232 org-search-syntax-table)
4233
ed21c5c8
CD
4234(defvar org-agenda-last-search-view-search-was-boolean nil)
4235
20908596
CD
4236;;;###autoload
4237(defun org-search-view (&optional todo-only string edit-at)
ed21c5c8 4238 "Show all entries that contain a phrase or words or regular expressions.
20908596
CD
4239
4240With optional prefix argument TODO-ONLY, only consider entries that are
4241TODO entries. The argument STRING can be used to pass a default search
4242string into this function. If EDIT-AT is non-nil, it means that the
4243user should get a chance to edit this string, with cursor at position
4244EDIT-AT.
4245
ed21c5c8
CD
4246The search string can be viewed either as a phrase that should be found as
4247is, or it can be broken into a number of snippets, each of which must match
4248in a Boolean way to select an entry. The default depends on the variable
4249`org-agenda-search-view-always-boolean'.
4250Even if this is turned off (the default) you can always switch to
86fbb8ca 4251Boolean search dynamically by preceding the first word with \"+\" or \"-\".
ed21c5c8
CD
4252
4253The default is a direct search of the whole phrase, where each space in
4254the search string can expand to an arbitrary amount of whitespace,
4255including newlines.
4256
4257If using a Boolean search, the search string is split on whitespace and
4258each snippet is searched separately, with logical AND to select an entry.
4259Words prefixed with a minus must *not* occur in the entry. Words without
4260a prefix or prefixed with a plus must occur in the entry. Matching is
4261case-insensitive. Words are enclosed by word delimiters (i.e. they must
4262match whole words, not parts of a word) if
4263`org-agenda-search-view-force-full-words' is set (default is nil).
4264
4265Boolean search snippets enclosed by curly braces are interpreted as
86fbb8ca 4266regular expressions that must or (when preceded with \"-\") must not
ed21c5c8 4267match in the entry. Snippets enclosed into double quotes will be taken
86fbb8ca 4268as a whole, to include whitespace.
ed21c5c8
CD
4269
4270- If the search string starts with an asterisk, search only in headlines.
4271- If (possibly after the leading star) the search string starts with an
4272 exclamation mark, this also means to look at TODO entries only, an effect
4273 that can also be achieved with a prefix argument.
4274- If (possibly after star and exclamation mark) the search string starts
4275 with a colon, this will mean that the (non-regexp) snippets of the
4276 Boolean search must match as full words.
20908596
CD
4277
4278This command searches the agenda files, and in addition the files listed
4279in `org-agenda-text-search-extra-files'."
4280 (interactive "P")
8223b1d2
BG
4281 (if org-agenda-overriding-arguments
4282 (setq todo-only (car org-agenda-overriding-arguments)
4283 string (nth 1 org-agenda-overriding-arguments)
4284 edit-at (nth 2 org-agenda-overriding-arguments)))
20908596 4285 (let* ((props (list 'face nil
c8d0cf5c 4286 'done-face 'org-agenda-done
20908596
CD
4287 'org-not-done-regexp org-not-done-regexp
4288 'org-todo-regexp org-todo-regexp
b349f79f 4289 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4290 'mouse-face 'highlight
20908596 4291 'help-echo (format "mouse-2 or RET jump to location")))
ed21c5c8 4292 (full-words org-agenda-search-view-force-full-words)
86fbb8ca 4293 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
20908596 4294 regexp rtn rtnall files file pos
8223b1d2 4295 marker category category-pos tags c neg re boolean
20908596
CD
4296 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4297 (unless (and (not edit-at)
4298 (stringp string)
4299 (string-match "\\S-" string))
ed21c5c8
CD
4300 (setq string (read-string
4301 (if org-agenda-search-view-always-boolean
4302 "[+-]Word/{Regexp} ...: "
8223b1d2 4303 "Phrase or [+-]Word/{Regexp} ...: ")
ed21c5c8
CD
4304 (cond
4305 ((integerp edit-at) (cons string edit-at))
4306 (edit-at string))
4307 'org-agenda-search-history)))
8223b1d2
BG
4308 (catch 'exit
4309 (if org-agenda-sticky
4310 (setq org-agenda-buffer-name
4311 (if (stringp string)
4312 (format "*Org Agenda(%s:%s)*"
4313 (or org-keys (or (and todo-only "S") "s")) string)
4314 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4315 (org-agenda-prepare "SEARCH")
4316 (org-compile-prefix-format 'search)
4317 (org-set-sorting-strategy 'search)
4318 (setq org-agenda-redo-command
4319 (list 'org-search-view (if todo-only t nil)
4320 (list 'if 'current-prefix-arg nil string)))
4321 (setq org-agenda-query-string string)
4322 (if (equal (string-to-char string) ?*)
4323 (setq hdl-only t
4324 words (substring string 1))
4325 (setq words string))
4326 (when (equal (string-to-char words) ?!)
4327 (setq todo-only t
4328 words (substring words 1)))
4329 (when (equal (string-to-char words) ?:)
4330 (setq full-words t
4331 words (substring words 1)))
4332 (if (or org-agenda-search-view-always-boolean
4333 (member (string-to-char words) '(?- ?+ ?\{)))
4334 (setq boolean t))
4335 (setq words (org-split-string words))
4336 (let (www w)
ed21c5c8 4337 (while (setq w (pop words))
8223b1d2
BG
4338 (while (and (string-match "\\\\\\'" w) words)
4339 (setq w (concat (substring w 0 -1) " " (pop words))))
4340 (push w www))
4341 (setq words (nreverse www) www nil)
4342 (while (setq w (pop words))
4343 (when (and (string-match "\\`[-+]?{" w)
4344 (not (string-match "}\\'" w)))
4345 (while (and words (not (string-match "}\\'" (car words))))
4346 (setq w (concat w " " (pop words))))
4347 (setq w (concat w " " (pop words))))
4348 (push w www))
4349 (setq words (nreverse www)))
4350 (setq org-agenda-last-search-view-search-was-boolean boolean)
4351 (when boolean
4352 (let (wds w)
4353 (while (setq w (pop words))
4354 (if (or (equal (substring w 0 1) "\"")
4355 (and (> (length w) 1)
4356 (member (substring w 0 1) '("+" "-"))
4357 (equal (substring w 1 2) "\"")))
4358 (while (and words (not (equal (substring w -1) "\"")))
4359 (setq w (concat w " " (pop words)))))
4360 (and (string-match "\\`\\([-+]?\\)\"" w)
4361 (setq w (replace-match "\\1" nil nil w)))
4362 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4363 (push w wds))
4364 (setq words (nreverse wds))))
4365 (if boolean
4366 (mapc (lambda (w)
4367 (setq c (string-to-char w))
4368 (if (equal c ?-)
4369 (setq neg t w (substring w 1))
4370 (if (equal c ?+)
4371 (setq neg nil w (substring w 1))
4372 (setq neg nil)))
4373 (if (string-match "\\`{.*}\\'" w)
4374 (setq re (substring w 1 -1))
4375 (if full-words
4376 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4377 (setq re (regexp-quote (downcase w)))))
4378 (if neg (push re regexps-) (push re regexps+)))
4379 words)
4380 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4381 regexps+))
4382 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4383 (if (not regexps+)
4384 (setq regexp org-outline-regexp-bol)
4385 (setq regexp (pop regexps+))
4386 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4387 regexp))))
4388 (setq files (org-agenda-files nil 'ifmode))
4389 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4390 (pop org-agenda-text-search-extra-files)
4391 (setq files (org-add-archive-files files)))
4392 (setq files (append files org-agenda-text-search-extra-files)
4393 rtnall nil)
4394 (while (setq file (pop files))
4395 (setq ee nil)
4396 (catch 'nextfile
4397 (org-check-agenda-file file)
4398 (setq buffer (if (file-exists-p file)
4399 (org-get-agenda-file-buffer file)
4400 (error "No such file %s" file)))
4401 (if (not buffer)
4402 ;; If file does not exist, make sure an error message is sent
4403 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4404 file))))
4405 (with-current-buffer buffer
4406 (with-syntax-table (org-search-syntax-table)
4407 (unless (derived-mode-p 'org-mode)
4408 (error "Agenda file %s is not in `org-mode'" file))
4409 (let ((case-fold-search t))
4410 (save-excursion
4411 (save-restriction
4412 (if org-agenda-restrict
4413 (narrow-to-region org-agenda-restrict-begin
4414 org-agenda-restrict-end)
4415 (widen))
4416 (goto-char (point-min))
4417 (unless (or (org-at-heading-p)
4418 (outline-next-heading))
4419 (throw 'nextfile t))
4420 (goto-char (max (point-min) (1- (point))))
4421 (while (re-search-forward regexp nil t)
4422 (org-back-to-heading t)
4423 (skip-chars-forward "* ")
4424 (setq beg (point-at-bol)
4425 beg1 (point)
4426 end (progn (outline-next-heading) (point)))
4427 (catch :skip
4428 (goto-char beg)
4429 (org-agenda-skip)
4430 (setq str (buffer-substring-no-properties
4431 (point-at-bol)
4432 (if hdl-only (point-at-eol) end)))
4433 (mapc (lambda (wr) (when (string-match wr str)
4434 (goto-char (1- end))
4435 (throw :skip t)))
4436 regexps-)
4437 (mapc (lambda (wr) (unless (string-match wr str)
4438 (goto-char (1- end))
4439 (throw :skip t)))
4440 (if todo-only
4441 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4442 regexps+)
4443 regexps+))
4444 (goto-char beg)
4445 (setq marker (org-agenda-new-marker (point))
4446 category (org-get-category)
4447 category-pos (get-text-property (point) 'org-category-position)
c7cf0ebc 4448 tags (org-get-tags-at nil t)
8223b1d2
BG
4449 txt (org-agenda-format-item
4450 ""
4451 (buffer-substring-no-properties
4452 beg1 (point-at-eol))
4453 category tags t))
4454 (org-add-props txt props
4455 'org-marker marker 'org-hd-marker marker
4456 'org-todo-regexp org-todo-regexp
4457 'org-complex-heading-regexp org-complex-heading-regexp
4458 'priority 1000 'org-category category
4459 'org-category-position category-pos
4460 'type "search")
4461 (push txt ee)
4462 (goto-char (1- end))))))))))
4463 (setq rtn (nreverse ee))
4464 (setq rtnall (append rtnall rtn)))
4465 (if org-agenda-overriding-header
4466 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4467 nil 'face 'org-agenda-structure) "\n")
4468 (insert "Search words: ")
4469 (add-text-properties (point-min) (1- (point))
4470 (list 'face 'org-agenda-structure))
4471 (setq pos (point))
4472 (insert string "\n")
4473 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4474 (setq pos (point))
4475 (unless org-agenda-multi
4476 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4477 (add-text-properties pos (1- (point))
4478 (list 'face 'org-agenda-structure))))
4479 (org-agenda-mark-header-line (point-min))
4480 (when rtnall
4481 (insert (org-agenda-finalize-entries rtnall) "\n"))
4482 (goto-char (point-min))
4483 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4484 (add-text-properties (point-min) (point-max)
4485 `(org-agenda-type search
4486 org-last-args (,todo-only ,string ,edit-at)
4487 org-redo-cmd ,org-agenda-redo-command
735135f9 4488 org-series-cmd ,org-cmd))
8223b1d2
BG
4489 (org-agenda-finalize)
4490 (setq buffer-read-only t))))
20908596 4491
bdebdb64
BG
4492(autoload 'org-search-view "org-agenda" "\
4493Show all entries that contain a phrase or words or regular expressions.
4494
4495With optional prefix argument TODO-ONLY, only consider entries that are
4496TODO entries. The argument STRING can be used to pass a default search
4497string into this function. If EDIT-AT is non-nil, it means that the
4498user should get a chance to edit this string, with cursor at position
4499EDIT-AT.
4500
4501The search string can be viewed either as a phrase that should be found as
4502is, or it can be broken into a number of snippets, each of which must match
4503in a Boolean way to select an entry. The default depends on the variable
4504`org-agenda-search-view-always-boolean'.
4505Even if this is turned off (the default) you can always switch to
4506Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4507
4508The default is a direct search of the whole phrase, where each space in
4509the search string can expand to an arbitrary amount of whitespace,
4510including newlines.
4511
4512If using a Boolean search, the search string is split on whitespace and
4513each snippet is searched separately, with logical AND to select an entry.
4514Words prefixed with a minus must *not* occur in the entry. Words without
4515a prefix or prefixed with a plus must occur in the entry. Matching is
4516case-insensitive. Words are enclosed by word delimiters (i.e. they must
4517match whole words, not parts of a word) if
4518`org-agenda-search-view-force-full-words' is set (default is nil).
4519
4520Boolean search snippets enclosed by curly braces are interpreted as
4521regular expressions that must or (when preceded with \"-\") must not
4522match in the entry. Snippets enclosed into double quotes will be taken
4523as a whole, to include whitespace.
4524
4525- If the search string starts with an asterisk, search only in headlines.
4526- If (possibly after the leading star) the search string starts with an
4527 exclamation mark, this also means to look at TODO entries only, an effect
4528 that can also be achieved with a prefix argument.
4529- If (possibly after star and exclamation mark) the search string starts
4530 with a colon, this will mean that the (non-regexp) snippets of the
4531 Boolean search must match as full words.
4532
4533This command searches the agenda files, and in addition the files listed
4534in `org-agenda-text-search-extra-files'.
4535
4536\(fn &optional TODO-ONLY STRING EDIT-AT)" t nil)
4537
20908596
CD
4538;;; Agenda TODO list
4539
4540(defvar org-select-this-todo-keyword nil)
4541(defvar org-last-arg nil)
4542
4543;;;###autoload
8223b1d2 4544(defun org-todo-list (&optional arg)
86fbb8ca 4545 "Show all (not done) TODO entries from all agenda file in a single list.
20908596
CD
4546The prefix arg can be used to select a specific TODO keyword and limit
4547the list to these. When using \\[universal-argument], you will be prompted
4548for a keyword. A numeric prefix directly selects the Nth keyword in
4549`org-todo-keywords-1'."
4550 (interactive "P")
8223b1d2
BG
4551 (if org-agenda-overriding-arguments
4552 (setq arg org-agenda-overriding-arguments))
ed21c5c8 4553 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
acedf35c 4554 (let* ((today (org-today))
20908596
CD
4555 (date (calendar-gregorian-from-absolute today))
4556 (kwds org-todo-keywords-for-agenda)
4557 (completion-ignore-case t)
4558 (org-select-this-todo-keyword
4559 (if (stringp arg) arg
4560 (and arg (integerp arg) (> arg 0)
4561 (nth (1- arg) kwds))))
4562 rtn rtnall files file pos)
4563 (when (equal arg '(4))
4564 (setq org-select-this-todo-keyword
54a0dee5 4565 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
8223b1d2 4566 (mapcar 'list kwds) nil nil)))
20908596 4567 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
8223b1d2
BG
4568 (catch 'exit
4569 (if org-agenda-sticky
4570 (setq org-agenda-buffer-name
4571 (if (stringp org-select-this-todo-keyword)
4572 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4573 org-select-this-todo-keyword)
4574 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4575 (org-agenda-prepare "TODO")
4576 (org-compile-prefix-format 'todo)
4577 (org-set-sorting-strategy 'todo)
4578 (setq org-agenda-redo-command
4579 `(org-todo-list (or (and (numberp current-prefix-arg)
4580 current-prefix-arg)
4581 ,org-select-this-todo-keyword
4582 current-prefix-arg ,arg)))
4583 (setq files (org-agenda-files nil 'ifmode)
4584 rtnall nil)
4585 (while (setq file (pop files))
4586 (catch 'nextfile
4587 (org-check-agenda-file file)
4588 (setq rtn (org-agenda-get-day-entries file date :todo))
4589 (setq rtnall (append rtnall rtn))))
4590 (if org-agenda-overriding-header
4591 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4592 nil 'face 'org-agenda-structure) "\n")
4593 (insert "Global list of TODO items of type: ")
4594 (add-text-properties (point-min) (1- (point))
4595 (list 'face 'org-agenda-structure
4596 'short-heading
4597 (concat "ToDo: "
4598 (or org-select-this-todo-keyword "ALL"))))
4599 (org-agenda-mark-header-line (point-min))
4600 (setq pos (point))
4601 (insert (or org-select-this-todo-keyword "ALL") "\n")
4602 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4603 (setq pos (point))
4604 (unless org-agenda-multi
4605 (insert "Available with `N r': (0)[ALL]")
4606 (let ((n 0) s)
4607 (mapc (lambda (x)
4608 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4609 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4610 (insert "\n "))
4611 (insert " " s))
4612 kwds))
4613 (insert "\n"))
4614 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 4615 (org-agenda-mark-header-line (point-min))
8223b1d2
BG
4616 (when rtnall
4617 (insert (org-agenda-finalize-entries rtnall) "\n"))
4618 (goto-char (point-min))
4619 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4620 (add-text-properties (point-min) (point-max)
4621 `(org-agenda-type todo
4622 org-last-args ,arg
4623 org-redo-cmd ,org-agenda-redo-command
735135f9 4624 org-series-cmd ,org-cmd))
8223b1d2
BG
4625 (org-agenda-finalize)
4626 (setq buffer-read-only t))))
20908596 4627
bdebdb64
BG
4628(autoload 'org-todo-list "org-agenda" "\
4629Show all (not done) TODO entries from all agenda file in a single list.
4630The prefix arg can be used to select a specific TODO keyword and limit
4631the list to these. When using \\[universal-argument], you will be prompted
4632for a keyword. A numeric prefix directly selects the Nth keyword in
4633`org-todo-keywords-1'.
4634
4635\(fn &optional ARG)" t nil)
4636
20908596
CD
4637;;; Agenda tags match
4638
4639;;;###autoload
4640(defun org-tags-view (&optional todo-only match)
4641 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4642The prefix arg TODO-ONLY limits the search to TODO entries."
4643 (interactive "P")
8223b1d2
BG
4644 (if org-agenda-overriding-arguments
4645 (setq todo-only (car org-agenda-overriding-arguments)
4646 match (nth 1 org-agenda-overriding-arguments)))
20908596 4647 (let* ((org-tags-match-list-sublevels
c8d0cf5c 4648 org-tags-match-list-sublevels)
20908596
CD
4649 (completion-ignore-case t)
4650 rtn rtnall files file pos matcher
4651 buffer)
ed21c5c8
CD
4652 (when (and (stringp match) (not (string-match "\\S-" match)))
4653 (setq match nil))
20908596
CD
4654 (setq matcher (org-make-tags-matcher match)
4655 match (car matcher) matcher (cdr matcher))
8223b1d2
BG
4656 (catch 'exit
4657 (if org-agenda-sticky
4658 (setq org-agenda-buffer-name
4659 (if (stringp match)
4660 (format "*Org Agenda(%s:%s)*"
4661 (or org-keys (or (and todo-only "M") "m")) match)
4662 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4663 (org-agenda-prepare (concat "TAGS " match))
4664 (org-compile-prefix-format 'tags)
4665 (org-set-sorting-strategy 'tags)
4666 (setq org-agenda-query-string match)
4667 (setq org-agenda-redo-command
4668 (list 'org-tags-view `(quote ,todo-only)
4669 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4670 (setq files (org-agenda-files nil 'ifmode)
4671 rtnall nil)
4672 (while (setq file (pop files))
4673 (catch 'nextfile
4674 (org-check-agenda-file file)
4675 (setq buffer (if (file-exists-p file)
4676 (org-get-agenda-file-buffer file)
4677 (error "No such file %s" file)))
4678 (if (not buffer)
4679 ;; If file does not exist, error message to agenda
4680 (setq rtn (list
4681 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4682 rtnall (append rtnall rtn))
4683 (with-current-buffer buffer
4684 (unless (derived-mode-p 'org-mode)
4685 (error "Agenda file %s is not in `org-mode'" file))
4686 (save-excursion
4687 (save-restriction
4688 (if org-agenda-restrict
4689 (narrow-to-region org-agenda-restrict-begin
4690 org-agenda-restrict-end)
4691 (widen))
4692 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4693 (setq rtnall (append rtnall rtn))))))))
4694 (if org-agenda-overriding-header
4695 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4696 nil 'face 'org-agenda-structure) "\n")
4697 (insert "Headlines with TAGS match: ")
4698 (add-text-properties (point-min) (1- (point))
4699 (list 'face 'org-agenda-structure
4700 'short-heading
4701 (concat "Match: " match)))
4702 (setq pos (point))
4703 (insert match "\n")
4704 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4705 (setq pos (point))
4706 (unless org-agenda-multi
4707 (insert "Press `C-u r' to search again with new search string\n"))
4708 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4709 (org-agenda-mark-header-line (point-min))
4710 (when rtnall
4711 (insert (org-agenda-finalize-entries rtnall) "\n"))
4712 (goto-char (point-min))
4713 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4714 (add-text-properties (point-min) (point-max)
4715 `(org-agenda-type tags
4716 org-last-args (,todo-only ,match)
4717 org-redo-cmd ,org-agenda-redo-command
735135f9 4718 org-series-cmd ,org-cmd))
8223b1d2
BG
4719 (org-agenda-finalize)
4720 (setq buffer-read-only t))))
20908596 4721
bdebdb64
BG
4722(autoload 'org-tags-view "org-agenda" "\
4723Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4724The prefix arg TODO-ONLY limits the search to TODO entries.
4725
4726\(fn &optional TODO-ONLY MATCH)" t nil)
4727
20908596
CD
4728;;; Agenda Finding stuck projects
4729
4730(defvar org-agenda-skip-regexp nil
4731 "Regular expression used in skipping subtrees for the agenda.
4732This is basically a temporary global variable that can be set and then
4733used by user-defined selections using `org-agenda-skip-function'.")
4734
4735(defvar org-agenda-overriding-header nil
3ab2c837 4736 "When set during agenda, todo and tags searches it replaces the header.
c8d0cf5c
CD
4737This variable should not be set directly, but custom commands can bind it
4738in the options section.")
4739
4740(defun org-agenda-skip-entry-when-regexp-matches ()
86fbb8ca 4741 "Check if the current entry contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4742If yes, it returns the end position of this entry, causing agenda commands
4743to skip the entry but continuing the search in the subtree. This is a
4744function that can be put into `org-agenda-skip-function' for the duration
4745of a command."
4746 (let ((end (save-excursion (org-end-of-subtree t)))
4747 skip)
4748 (save-excursion
4749 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4750 (and skip end)))
20908596
CD
4751
4752(defun org-agenda-skip-subtree-when-regexp-matches ()
86fbb8ca 4753 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
20908596
CD
4754If yes, it returns the end position of this tree, causing agenda commands
4755to skip this subtree. This is a function that can be put into
4756`org-agenda-skip-function' for the duration of a command."
4757 (let ((end (save-excursion (org-end-of-subtree t)))
4758 skip)
4759 (save-excursion
4760 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4761 (and skip end)))
4762
c8d0cf5c 4763(defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
86fbb8ca 4764 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4765If yes, it returns the end position of the current entry (NOT the tree),
4766causing agenda commands to skip the entry but continuing the search in
4767the subtree. This is a function that can be put into
4768`org-agenda-skip-function' for the duration of a command. An important
4769use of this function is for the stuck project list."
4770 (let ((end (save-excursion (org-end-of-subtree t)))
4771 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4772 skip)
4773 (save-excursion
4774 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4775 (and skip entry-end)))
4776
20908596
CD
4777(defun org-agenda-skip-entry-if (&rest conditions)
4778 "Skip entry if any of CONDITIONS is true.
4779See `org-agenda-skip-if' for details."
4780 (org-agenda-skip-if nil conditions))
4781
4782(defun org-agenda-skip-subtree-if (&rest conditions)
4783 "Skip entry if any of CONDITIONS is true.
4784See `org-agenda-skip-if' for details."
4785 (org-agenda-skip-if t conditions))
4786
4787(defun org-agenda-skip-if (subtree conditions)
4788 "Checks current entity for CONDITIONS.
4789If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
153ae947 4790the entry (i.e. the text before the next heading) is checked.
20908596
CD
4791
4792CONDITIONS is a list of symbols, boolean OR is used to combine the results
4793from different tests. Valid conditions are:
4794
4795scheduled Check if there is a scheduled cookie
4796notscheduled Check if there is no scheduled cookie
4797deadline Check if there is a deadline
4798notdeadline Check if there is no deadline
c8d0cf5c
CD
4799timestamp Check if there is a timestamp (also deadline or scheduled)
4800nottimestamp Check if there is no timestamp (also deadline or scheduled)
20908596
CD
4801regexp Check if regexp matches
4802notregexp Check if regexp does not match.
ed21c5c8
CD
4803todo Check if TODO keyword matches
4804nottodo Check if TODO keyword does not match
20908596
CD
4805
4806The regexp is taken from the conditions list, it must come right after
4807the `regexp' or `notregexp' element.
4808
ed21c5c8
CD
4809`todo' and `nottodo' accept as an argument a list of todo
4810keywords, which may include \"*\" to match any todo keyword.
4811
4812 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4813
4814would skip all entries with \"TODO\" or \"WAITING\" keywords.
4815
153ae947 4816Instead of a list, a keyword class may be given. For example:
ed21c5c8
CD
4817
4818 (org-agenda-skip-entry-if 'nottodo 'done)
4819
4820would skip entries that haven't been marked with any of \"DONE\"
153ae947 4821keywords. Possible classes are: `todo', `done', `any'.
ed21c5c8 4822
20908596
CD
4823If any of these conditions is met, this function returns the end point of
4824the entity, causing the search to continue from there. This is a function
4825that can be put into `org-agenda-skip-function' for the duration of a command."
4826 (let (beg end m)
4827 (org-back-to-heading t)
4828 (setq beg (point)
4829 end (if subtree
4830 (progn (org-end-of-subtree t) (point))
4831 (progn (outline-next-heading) (1- (point)))))
4832 (goto-char beg)
4833 (and
4834 (or
4835 (and (memq 'scheduled conditions)
4836 (re-search-forward org-scheduled-time-regexp end t))
4837 (and (memq 'notscheduled conditions)
4838 (not (re-search-forward org-scheduled-time-regexp end t)))
4839 (and (memq 'deadline conditions)
4840 (re-search-forward org-deadline-time-regexp end t))
4841 (and (memq 'notdeadline conditions)
4842 (not (re-search-forward org-deadline-time-regexp end t)))
c8d0cf5c
CD
4843 (and (memq 'timestamp conditions)
4844 (re-search-forward org-ts-regexp end t))
4845 (and (memq 'nottimestamp conditions)
4846 (not (re-search-forward org-ts-regexp end t)))
20908596
CD
4847 (and (setq m (memq 'regexp conditions))
4848 (stringp (nth 1 m))
4849 (re-search-forward (nth 1 m) end t))
4850 (and (setq m (memq 'notregexp conditions))
4851 (stringp (nth 1 m))
ed21c5c8
CD
4852 (not (re-search-forward (nth 1 m) end t)))
4853 (and (or
153ae947 4854 (setq m (memq 'nottodo conditions))
8223b1d2
BG
4855 (setq m (memq 'todo-unblocked conditions))
4856 (setq m (memq 'nottodo-unblocked conditions))
153ae947 4857 (setq m (memq 'todo conditions)))
ed21c5c8 4858 (org-agenda-skip-if-todo m end)))
20908596
CD
4859 end)))
4860
ed21c5c8
CD
4861(defun org-agenda-skip-if-todo (args end)
4862 "Helper function for `org-agenda-skip-if', do not use it directly.
8223b1d2
BG
4863ARGS is a list with first element either `todo', `nottodo',
4864`todo-unblocked' or `nottodo-unblocked'. The remainder is either
4865a list of TODO keywords, or a state symbol `todo' or `done' or
4866`any'."
ed21c5c8
CD
4867 (let ((kw (car args))
4868 (arg (cadr args))
4869 todo-wds todo-re)
4870 (setq todo-wds
4871 (org-uniquify
4872 (cond
4873 ((listp arg) ;; list of keywords
4874 (if (member "*" arg)
4875 (mapcar 'substring-no-properties org-todo-keywords-1)
4876 arg))
4877 ((symbolp arg) ;; keyword class name
4878 (cond
4879 ((eq arg 'todo)
4880 (org-delete-all org-done-keywords
4881 (mapcar 'substring-no-properties
4882 org-todo-keywords-1)))
4883 ((eq arg 'done) org-done-keywords)
4884 ((eq arg 'any)
4885 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4886 (setq todo-re
4887 (concat "^\\*+[ \t]+\\<\\("
4888 (mapconcat 'identity todo-wds "\\|")
4889 "\\)\\>"))
8223b1d2
BG
4890 (cond
4891 ((eq kw 'todo) (re-search-forward todo-re end t))
4892 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4893 ((eq kw 'todo-unblocked)
4894 (catch 'unblocked
4895 (while (re-search-forward todo-re end t)
4896 (or (org-entry-blocked-p) (throw 'unblocked t)))
4897 nil))
4898 ((eq kw 'nottodo-unblocked)
4899 (catch 'unblocked
4900 (while (re-search-forward todo-re end t)
4901 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4902 t))
4903 )))
ed21c5c8 4904
20908596
CD
4905;;;###autoload
4906(defun org-agenda-list-stuck-projects (&rest ignore)
4907 "Create agenda view for projects that are stuck.
4908Stuck projects are project that have no next actions. For the definitions
4909of what a project is and how to check if it stuck, customize the variable
afe98dfa 4910`org-stuck-projects'."
20908596 4911 (interactive)
c8d0cf5c
CD
4912 (let* ((org-agenda-skip-function
4913 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
20908596 4914 ;; We could have used org-agenda-skip-if here.
c8d0cf5c
CD
4915 (org-agenda-overriding-header
4916 (or org-agenda-overriding-header "List of stuck projects: "))
20908596
CD
4917 (matcher (nth 0 org-stuck-projects))
4918 (todo (nth 1 org-stuck-projects))
4919 (todo-wds (if (member "*" todo)
4920 (progn
8223b1d2 4921 (org-agenda-prepare-buffers (org-agenda-files
2c3ad40d 4922 nil 'ifmode))
20908596
CD
4923 (org-delete-all
4924 org-done-keywords-for-agenda
4925 (copy-sequence org-todo-keywords-for-agenda)))
4926 todo))
4927 (todo-re (concat "^\\*+[ \t]+\\("
4928 (mapconcat 'identity todo-wds "\\|")
4929 "\\)\\>"))
4930 (tags (nth 2 org-stuck-projects))
4931 (tags-re (if (member "*" tags)
e66ba1df
BG
4932 (concat org-outline-regexp-bol
4933 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
c8d0cf5c 4934 (if tags
3ab2c837
BG
4935 (concat org-outline-regexp-bol
4936 ".*:\\("
c8d0cf5c 4937 (mapconcat 'identity tags "\\|")
afe98dfa 4938 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
20908596
CD
4939 (gen-re (nth 3 org-stuck-projects))
4940 (re-list
4941 (delq nil
4942 (list
4943 (if todo todo-re)
4944 (if tags tags-re)
4945 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4946 gen-re)))))
4947 (setq org-agenda-skip-regexp
4948 (if re-list
4949 (mapconcat 'identity re-list "\\|")
4950 (error "No information how to identify unstuck projects")))
4951 (org-tags-view nil matcher)
4952 (with-current-buffer org-agenda-buffer-name
4953 (setq org-agenda-redo-command
8223b1d2 4954 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
20908596 4955
bdebdb64
BG
4956(autoload 'org-agenda-list-stuck-projects "org-agenda" "\
4957Create agenda view for projects that are stuck.
4958Stuck projects are project that have no next actions. For the definitions
4959of what a project is and how to check if it stuck, customize the variable
4960`org-stuck-projects'.
4961
4962\(fn &rest IGNORE)" t nil)
4963
20908596
CD
4964;;; Diary integration
4965
4966(defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
8223b1d2 4967(defvar diary-list-entries-hook)
3ab2c837 4968(defvar diary-time-regexp)
20908596
CD
4969(defun org-get-entries-from-diary (date)
4970 "Get the (Emacs Calendar) diary entries for DATE."
4971 (require 'diary-lib)
4972 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
20908596 4973 (diary-display-hook '(fancy-diary-display))
ca8ef0dc 4974 (diary-display-function 'fancy-diary-display)
20908596 4975 (pop-up-frames nil)
8223b1d2
BG
4976 (diary-list-entries-hook
4977 (cons 'org-diary-default-entry diary-list-entries-hook))
20908596
CD
4978 (diary-file-name-prefix-function nil) ; turn this feature off
4979 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4980 entries
4981 (org-disable-agenda-to-diary t))
4982 (save-excursion
4983 (save-window-excursion
4984 (funcall (if (fboundp 'diary-list-entries)
4985 'diary-list-entries 'list-diary-entries)
4986 date 1)))
4987 (if (not (get-buffer diary-fancy-buffer))
4988 (setq entries nil)
4989 (with-current-buffer diary-fancy-buffer
4990 (setq buffer-read-only nil)
4991 (if (zerop (buffer-size))
4992 ;; No entries
4993 (setq entries nil)
4994 ;; Omit the date and other unnecessary stuff
4995 (org-agenda-cleanup-fancy-diary)
4996 ;; Add prefix to each line and extend the text properties
4997 (if (zerop (buffer-size))
4998 (setq entries nil)
3ab2c837
BG
4999 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5000 (setq entries
5001 (with-temp-buffer
5002 (insert entries) (goto-char (point-min))
5003 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5004 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5005 (replace-match (concat "; " (match-string 1)))))
5006 (buffer-string)))))
20908596
CD
5007 (set-buffer-modified-p nil)
5008 (kill-buffer diary-fancy-buffer)))
5009 (when entries
5010 (setq entries (org-split-string entries "\n"))
5011 (setq entries
5012 (mapcar
5013 (lambda (x)
e66ba1df 5014 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
20908596
CD
5015 ;; Extend the text properties to the beginning of the line
5016 (org-add-props x (text-properties-at (1- (length x)) x)
ed21c5c8 5017 'type "diary" 'date date 'face 'org-agenda-diary))
20908596
CD
5018 entries)))))
5019
c8d0cf5c
CD
5020(defvar org-agenda-cleanup-fancy-diary-hook nil
5021 "Hook run when the fancy diary buffer is cleaned up.")
5022
20908596
CD
5023(defun org-agenda-cleanup-fancy-diary ()
5024 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5025This gets rid of the date, the underline under the date, and
5026the dummy entry installed by `org-mode' to ensure non-empty diary for each
5027date. It also removes lines that contain only whitespace."
5028 (goto-char (point-min))
5029 (if (looking-at ".*?:[ \t]*")
5030 (progn
5031 (replace-match "")
5032 (re-search-forward "\n=+$" nil t)
5033 (replace-match "")
5034 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5035 (re-search-forward "\n=+$" nil t)
5036 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5037 (goto-char (point-min))
5038 (while (re-search-forward "^ +\n" nil t)
5039 (replace-match ""))
5040 (goto-char (point-min))
5041 (if (re-search-forward "^Org-mode dummy\n?" nil t)
c8d0cf5c
CD
5042 (replace-match ""))
5043 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
20908596
CD
5044
5045;; Make sure entries from the diary have the right text properties.
5046(eval-after-load "diary-lib"
5047 '(if (boundp 'diary-modify-entry-list-string-function)
5048 ;; We can rely on the hook, nothing to do
5049 nil
33306645 5050 ;; Hook not available, must use advice to make this work
20908596
CD
5051 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5052 "Make the position visible."
5053 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5054 (stringp string)
5055 buffer-file-name)
5056 (setq string (org-modify-diary-entry-string string))))))
5057
5058(defun org-modify-diary-entry-string (string)
e66ba1df 5059 "Add text properties to string, allowing org-mode to act on it."
20908596
CD
5060 (org-add-props string nil
5061 'mouse-face 'highlight
20908596
CD
5062 'help-echo (if buffer-file-name
5063 (format "mouse-2 or RET jump to diary file %s"
5064 (abbreviate-file-name buffer-file-name))
5065 "")
5066 'org-agenda-diary-link t
5067 'org-marker (org-agenda-new-marker (point-at-bol))))
5068
5069(defun org-diary-default-entry ()
5070 "Add a dummy entry to the diary.
5071Needed to avoid empty dates which mess up holiday display."
5072 ;; Catch the error if dealing with the new add-to-diary-alist
5073 (when org-disable-agenda-to-diary
5074 (condition-case nil
5075 (org-add-to-diary-list original-date "Org-mode dummy" "")
5076 (error
5077 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5078
5079(defun org-add-to-diary-list (&rest args)
5080 (if (fboundp 'diary-add-to-list)
5081 (apply 'diary-add-to-list args)
5082 (apply 'add-to-diary-list args)))
5083
ed21c5c8
CD
5084(defvar org-diary-last-run-time nil)
5085
20908596
CD
5086;;;###autoload
5087(defun org-diary (&rest args)
8223b1d2 5088 "Return diary information from org files.
20908596
CD
5089This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5090It accesses org files and extracts information from those files to be
5091listed in the diary. The function accepts arguments specifying what
ed21c5c8
CD
5092items should be listed. For a list of arguments allowed here, see the
5093variable `org-agenda-entry-types'.
20908596
CD
5094
5095The call in the diary file should look like this:
5096
5097 &%%(org-diary) ~/path/to/some/orgfile.org
5098
5099Use a separate line for each org file to check. Or, if you omit the file name,
5100all files listed in `org-agenda-files' will be checked automatically:
5101
5102 &%%(org-diary)
5103
5104If you don't give any arguments (as in the example above), the default
5105arguments (:deadline :scheduled :timestamp :sexp) are used.
5106So the example above may also be written as
5107
5108 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5109
5110The function expects the lisp variables `entry' and `date' to be provided
5111by the caller, because this is how the calendar works. Don't use this
5112function from a program - use `org-agenda-get-day-entries' instead."
54a0dee5 5113 (when (> (- (org-float-time)
20908596
CD
5114 org-agenda-last-marker-time)
5115 5)
8223b1d2
BG
5116 ;; I am not sure if this works with sticky agendas, because the marker
5117 ;; list is then no longer a global variable.
20908596
CD
5118 (org-agenda-reset-markers))
5119 (org-compile-prefix-format 'agenda)
5120 (org-set-sorting-strategy 'agenda)
5121 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
23f6720e
BG
5122 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5123 (list entry)
20908596 5124 (org-agenda-files t)))
ed21c5c8 5125 (time (org-float-time))
20908596 5126 file rtn results)
ed21c5c8
CD
5127 (when (or (not org-diary-last-run-time)
5128 (> (- time
5129 org-diary-last-run-time)
5130 3))
8223b1d2 5131 (org-agenda-prepare-buffers files))
ed21c5c8 5132 (setq org-diary-last-run-time time)
20908596
CD
5133 ;; If this is called during org-agenda, don't return any entries to
5134 ;; the calendar. Org Agenda will list these entries itself.
5135 (if org-disable-agenda-to-diary (setq files nil))
5136 (while (setq file (pop files))
5137 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5138 (setq results (append results rtn)))
5139 (if results
8223b1d2 5140 (concat (org-agenda-finalize-entries results) "\n"))))
20908596 5141
bdebdb64
BG
5142(autoload 'org-diary "org-agenda" "\
5143Return diary information from org files.
5144This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5145It accesses org files and extracts information from those files to be
5146listed in the diary. The function accepts arguments specifying what
5147items should be listed. For a list of arguments allowed here, see the
5148variable `org-agenda-entry-types'.
5149
5150The call in the diary file should look like this:
5151
5152 &%%(org-diary) ~/path/to/some/orgfile.org
5153
5154Use a separate line for each org file to check. Or, if you omit the file name,
5155all files listed in `org-agenda-files' will be checked automatically:
5156
5157 &%%(org-diary)
5158
5159If you don't give any arguments (as in the example above), the default
5160arguments (:deadline :scheduled :timestamp :sexp) are used.
5161So the example above may also be written as
5162
5163 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5164
5165The function expects the lisp variables `entry' and `date' to be provided
5166by the caller, because this is how the calendar works. Don't use this
5167function from a program - use `org-agenda-get-day-entries' instead.
5168
5169\(fn &rest ARGS)" nil nil)
5170
20908596
CD
5171;;; Agenda entry finders
5172
5173(defun org-agenda-get-day-entries (file date &rest args)
5174 "Does the work for `org-diary' and `org-agenda'.
5175FILE is the path to a file to be checked for entries. DATE is date like
5176the one returned by `calendar-current-date'. ARGS are symbols indicating
5177which kind of entries should be extracted. For details about these, see
5178the documentation of `org-diary'."
5179 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
5180 (let* ((org-startup-folded nil)
5181 (org-startup-align-all-tables nil)
5182 (buffer (if (file-exists-p file)
5183 (org-get-agenda-file-buffer file)
5184 (error "No such file %s" file)))
54a0dee5 5185 arg results rtn deadline-results)
20908596
CD
5186 (if (not buffer)
5187 ;; If file does not exist, make sure an error message ends up in diary
5188 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5189 (with-current-buffer buffer
8223b1d2 5190 (unless (derived-mode-p 'org-mode)
20908596 5191 (error "Agenda file %s is not in `org-mode'" file))
8223b1d2 5192 (setq org-agenda-buffer (or org-agenda-buffer buffer))
20908596
CD
5193 (let ((case-fold-search nil))
5194 (save-excursion
5195 (save-restriction
5196 (if org-agenda-restrict
5197 (narrow-to-region org-agenda-restrict-begin
5198 org-agenda-restrict-end)
5199 (widen))
5200 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5201 (while (setq arg (pop args))
5202 (cond
5203 ((and (eq arg :todo)
3ab2c837
BG
5204 (equal date (calendar-gregorian-from-absolute
5205 (org-today))))
20908596
CD
5206 (setq rtn (org-agenda-get-todos))
5207 (setq results (append results rtn)))
5208 ((eq arg :timestamp)
5209 (setq rtn (org-agenda-get-blocks))
5210 (setq results (append results rtn))
8223b1d2 5211 (setq rtn (org-agenda-get-timestamps deadline-results))
20908596
CD
5212 (setq results (append results rtn)))
5213 ((eq arg :sexp)
5214 (setq rtn (org-agenda-get-sexps))
5215 (setq results (append results rtn)))
5216 ((eq arg :scheduled)
54a0dee5 5217 (setq rtn (org-agenda-get-scheduled deadline-results))
20908596
CD
5218 (setq results (append results rtn)))
5219 ((eq arg :closed)
93b62de8 5220 (setq rtn (org-agenda-get-progress))
20908596
CD
5221 (setq results (append results rtn)))
5222 ((eq arg :deadline)
5223 (setq rtn (org-agenda-get-deadlines))
54a0dee5 5224 (setq deadline-results (copy-sequence rtn))
20908596
CD
5225 (setq results (append results rtn))))))))
5226 results))))
5227
e66ba1df 5228(defvar org-heading-keyword-regexp-format) ; defined in org.el
20908596
CD
5229(defun org-agenda-get-todos ()
5230 "Return the TODO information for agenda display."
5231 (let* ((props (list 'face nil
c8d0cf5c 5232 'done-face 'org-agenda-done
20908596
CD
5233 'org-not-done-regexp org-not-done-regexp
5234 'org-todo-regexp org-todo-regexp
b349f79f 5235 'org-complex-heading-regexp org-complex-heading-regexp
20908596 5236 'mouse-face 'highlight
20908596
CD
5237 'help-echo
5238 (format "mouse-2 or RET jump to org file %s"
5239 (abbreviate-file-name buffer-file-name))))
e66ba1df
BG
5240 (regexp (format org-heading-keyword-regexp-format
5241 (cond
5242 ((and org-select-this-todo-keyword
5243 (equal org-select-this-todo-keyword "*"))
5244 org-todo-regexp)
5245 (org-select-this-todo-keyword
5246 (concat "\\("
5247 (mapconcat 'identity
5248 (org-split-string
5249 org-select-this-todo-keyword
5250 "|")
5251 "\\|") "\\)"))
5252 (t org-not-done-regexp))))
8223b1d2 5253 marker priority category category-pos tags todo-state
20908596
CD
5254 ee txt beg end)
5255 (goto-char (point-min))
5256 (while (re-search-forward regexp nil t)
5257 (catch :skip
5258 (save-match-data
5259 (beginning-of-line)
3ab2c837 5260 (org-agenda-skip)
d6685abc 5261 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
0bd48b37 5262 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
20908596
CD
5263 (goto-char (1+ beg))
5264 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5265 (throw :skip nil)))
e66ba1df 5266 (goto-char (match-beginning 2))
20908596
CD
5267 (setq marker (org-agenda-new-marker (match-beginning 0))
5268 category (org-get-category)
8223b1d2 5269 category-pos (get-text-property (point) 'org-category-position)
e66ba1df
BG
5270 txt (org-trim
5271 (buffer-substring (match-beginning 2) (match-end 0)))
c7cf0ebc 5272 tags (org-get-tags-at nil t)
8223b1d2 5273 txt (org-agenda-format-item "" txt category tags t)
621f83e4
CD
5274 priority (1+ (org-get-priority txt))
5275 todo-state (org-get-todo-state))
20908596
CD
5276 (org-add-props txt props
5277 'org-marker marker 'org-hd-marker marker
5278 'priority priority 'org-category category
8223b1d2 5279 'org-category-position category-pos
621f83e4 5280 'type "todo" 'todo-state todo-state)
20908596
CD
5281 (push txt ee)
5282 (if org-agenda-todo-list-sublevels
e66ba1df 5283 (goto-char (match-end 2))
20908596
CD
5284 (org-end-of-subtree 'invisible))))
5285 (nreverse ee)))
5286
3ab2c837 5287(defun org-agenda-todo-custom-ignore-p (time n)
8223b1d2 5288 "Check whether timestamp is farther away than n number of days.
3ab2c837
BG
5289This function is invoked if `org-agenda-todo-ignore-deadlines',
5290`org-agenda-todo-ignore-scheduled' or
5291`org-agenda-todo-ignore-timestamp' is set to an integer."
5292 (let ((days (org-days-to-time time)))
5293 (if (>= n 0)
5294 (>= days n)
5295 (<= days n))))
5296
ed21c5c8
CD
5297(defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5298 (&optional end)
5299 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
0bd48b37
CD
5300 (when (or org-agenda-todo-ignore-with-date
5301 org-agenda-todo-ignore-scheduled
acedf35c
CD
5302 org-agenda-todo-ignore-deadlines
5303 org-agenda-todo-ignore-timestamp)
0bd48b37
CD
5304 (setq end (or end (save-excursion (outline-next-heading) (point))))
5305 (save-excursion
5306 (or (and org-agenda-todo-ignore-with-date
5307 (re-search-forward org-ts-regexp end t))
5308 (and org-agenda-todo-ignore-scheduled
ed21c5c8
CD
5309 (re-search-forward org-scheduled-time-regexp end t)
5310 (cond
5311 ((eq org-agenda-todo-ignore-scheduled 'future)
5312 (> (org-days-to-time (match-string 1)) 0))
5313 ((eq org-agenda-todo-ignore-scheduled 'past)
5314 (<= (org-days-to-time (match-string 1)) 0))
3ab2c837
BG
5315 ((numberp org-agenda-todo-ignore-scheduled)
5316 (org-agenda-todo-custom-ignore-p
5317 (match-string 1) org-agenda-todo-ignore-scheduled))
ed21c5c8 5318 (t)))
0bd48b37
CD
5319 (and org-agenda-todo-ignore-deadlines
5320 (re-search-forward org-deadline-time-regexp end t)
ed21c5c8
CD
5321 (cond
5322 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5323 ((eq org-agenda-todo-ignore-deadlines 'far)
5324 (not (org-deadline-close (match-string 1))))
5325 ((eq org-agenda-todo-ignore-deadlines 'future)
5326 (> (org-days-to-time (match-string 1)) 0))
5327 ((eq org-agenda-todo-ignore-deadlines 'past)
5328 (<= (org-days-to-time (match-string 1)) 0))
3ab2c837
BG
5329 ((numberp org-agenda-todo-ignore-deadlines)
5330 (org-agenda-todo-custom-ignore-p
5331 (match-string 1) org-agenda-todo-ignore-deadlines))
acedf35c
CD
5332 (t (org-deadline-close (match-string 1)))))
5333 (and org-agenda-todo-ignore-timestamp
5334 (let ((buffer (current-buffer))
5335 (regexp
5336 (concat
5337 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5338 (start (point)))
5339 ;; Copy current buffer into a temporary one
5340 (with-temp-buffer
5341 (insert-buffer-substring buffer start end)
5342 (goto-char (point-min))
5343 ;; Delete SCHEDULED and DEADLINE items
5344 (while (re-search-forward regexp end t)
5345 (delete-region (match-beginning 0) (match-end 0)))
5346 (goto-char (point-min))
5347 ;; No search for timestamp left
5348 (when (re-search-forward org-ts-regexp nil t)
5349 (cond
5350 ((eq org-agenda-todo-ignore-timestamp 'future)
5351 (> (org-days-to-time (match-string 1)) 0))
5352 ((eq org-agenda-todo-ignore-timestamp 'past)
5353 (<= (org-days-to-time (match-string 1)) 0))
3ab2c837
BG
5354 ((numberp org-agenda-todo-ignore-timestamp)
5355 (org-agenda-todo-custom-ignore-p
5356 (match-string 1) org-agenda-todo-ignore-timestamp))
acedf35c 5357 (t))))))))))
0bd48b37 5358
bdebdb64
BG
5359(autoload 'org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item "org-agenda" "\
5360Do we have a reason to ignore this TODO entry because it has a time stamp?
5361
5362\(fn &optional END)" nil nil)
5363
20908596
CD
5364(defconst org-agenda-no-heading-message
5365 "No heading for this item in buffer or region.")
5366
8223b1d2 5367(defun org-agenda-get-timestamps (&optional deadline-results)
20908596 5368 "Return the date stamp information for agenda display."
e66ba1df 5369 (let* ((props (list 'face 'org-agenda-calendar-event
20908596
CD
5370 'org-not-done-regexp org-not-done-regexp
5371 'org-todo-regexp org-todo-regexp
b349f79f 5372 'org-complex-heading-regexp org-complex-heading-regexp
20908596 5373 'mouse-face 'highlight
20908596
CD
5374 'help-echo
5375 (format "mouse-2 or RET jump to org file %s"
5376 (abbreviate-file-name buffer-file-name))))
5377 (d1 (calendar-absolute-from-gregorian date))
8223b1d2
BG
5378 mm
5379 (deadline-position-alist
5380 (mapcar (lambda (a) (and (setq mm (get-text-property
5381 0 'org-hd-marker a))
5382 (cons (marker-position mm) a)))
5383 deadline-results))
5384 (remove-re org-ts-regexp)
20908596
CD
5385 (regexp
5386 (concat
5387 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5388 (regexp-quote
5389 (substring
5390 (format-time-string
5391 (car org-time-stamp-formats)
5392 (apply 'encode-time ; DATE bound by calendar
5393 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5394 1 11))
8223b1d2 5395 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
20908596
CD
5396 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5397 marker hdmarker deadlinep scheduledp clockp closedp inactivep
8223b1d2 5398 donep tmp priority category category-pos ee txt timestr tags
bdebdb64 5399 b0 b3 e3 head todo-state end-of-match show-all warntime habitp)
20908596 5400 (goto-char (point-min))
c8d0cf5c 5401 (while (setq end-of-match (re-search-forward regexp nil t))
20908596 5402 (setq b0 (match-beginning 0)
3ab2c837
BG
5403 b3 (match-beginning 3) e3 (match-end 3)
5404 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
bdebdb64 5405 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
3ab2c837
BG
5406 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5407 (member todo-state
5408 org-agenda-repeating-timestamp-show-all)))
20908596
CD
5409 (catch :skip
5410 (and (org-at-date-range-p) (throw :skip nil))
5411 (org-agenda-skip)
5412 (if (and (match-end 1)
5413 (not (= d1 (org-time-string-to-absolute
e66ba1df
BG
5414 (match-string 1) d1 nil show-all
5415 (current-buffer) b0))))
20908596
CD
5416 (throw :skip nil))
5417 (if (and e3
5418 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5419 (throw :skip nil))
c8d0cf5c 5420 (setq tmp (buffer-substring (max (point-min)
20908596
CD
5421 (- b0 org-ds-keyword-length))
5422 b0)
5423 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5424 inactivep (= (char-after b0) ?\[)
5425 deadlinep (string-match org-deadline-regexp tmp)
5426 scheduledp (string-match org-scheduled-regexp tmp)
5427 closedp (and org-agenda-include-inactive-timestamps
5428 (string-match org-closed-string tmp))
5429 clockp (and org-agenda-include-inactive-timestamps
5430 (or (string-match org-clock-string tmp)
5431 (string-match "]-+\\'" tmp)))
c7cf0ebc 5432 warntime (get-text-property (point) 'org-appt-warntime)
621f83e4 5433 donep (member todo-state org-done-keywords))
c8d0cf5c
CD
5434 (if (or scheduledp deadlinep closedp clockp
5435 (and donep org-agenda-skip-timestamp-if-done))
20908596
CD
5436 (throw :skip t))
5437 (if (string-match ">" timestr)
5438 ;; substring should only run to end of time stamp
5439 (setq timestr (substring timestr 0 (match-end 0))))
c8d0cf5c 5440 (setq marker (org-agenda-new-marker b0)
e66ba1df 5441 category (org-get-category b0)
8223b1d2 5442 category-pos (get-text-property b0 'org-category-position))
20908596 5443 (save-excursion
3ab2c837 5444 (if (not (re-search-backward org-outline-regexp-bol nil t))
c8d0cf5c
CD
5445 (setq txt org-agenda-no-heading-message)
5446 (goto-char (match-beginning 0))
8223b1d2
BG
5447 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5448 (assoc (point) deadline-position-alist))
5449 (throw :skip nil))
c8d0cf5c 5450 (setq hdmarker (org-agenda-new-marker)
c7cf0ebc 5451 tags (org-get-tags-at nil t))
c8d0cf5c 5452 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3ab2c837 5453 (setq head (or (match-string 1) ""))
e66ba1df 5454 (setq txt (org-agenda-format-item
ed21c5c8 5455 (if inactivep org-agenda-inactive-leader nil)
3ab2c837 5456 head category tags timestr
bdebdb64 5457 remove-re habitp)))
20908596
CD
5458 (setq priority (org-get-priority txt))
5459 (org-add-props txt props
5460 'org-marker marker 'org-hd-marker hdmarker)
5461 (org-add-props txt nil 'priority priority
5462 'org-category category 'date date
8223b1d2 5463 'org-category-position category-pos
621f83e4 5464 'todo-state todo-state
8223b1d2 5465 'warntime warntime
20908596
CD
5466 'type "timestamp")
5467 (push txt ee))
c8d0cf5c
CD
5468 (if org-agenda-skip-additional-timestamps-same-entry
5469 (outline-next-heading)
5470 (goto-char end-of-match))))
20908596
CD
5471 (nreverse ee)))
5472
5473(defun org-agenda-get-sexps ()
5474 "Return the sexp information for agenda display."
5475 (require 'diary-lib)
e66ba1df
BG
5476 (let* ((props (list 'face 'org-agenda-calendar-sexp
5477 'mouse-face 'highlight
20908596
CD
5478 'help-echo
5479 (format "mouse-2 or RET jump to org file %s"
5480 (abbreviate-file-name buffer-file-name))))
5481 (regexp "^&?%%(")
8223b1d2
BG
5482 marker category extra category-pos ee txt tags entry
5483 result beg b sexp sexp-entry todo-state warntime)
20908596
CD
5484 (goto-char (point-min))
5485 (while (re-search-forward regexp nil t)
5486 (catch :skip
5487 (org-agenda-skip)
5488 (setq beg (match-beginning 0))
5489 (goto-char (1- (match-end 0)))
5490 (setq b (point))
5491 (forward-sexp 1)
5492 (setq sexp (buffer-substring b (point)))
5493 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5494 (org-trim (match-string 1))
5495 ""))
5496 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5497 (when result
5498 (setq marker (org-agenda-new-marker beg)
c8d0cf5c 5499 category (org-get-category beg)
8223b1d2 5500 category-pos (get-text-property beg 'org-category-position)
c7cf0ebc 5501 tags (save-excursion (org-back-to-heading t) (org-get-tags-at nil t))
8223b1d2 5502 todo-state (org-get-todo-state)
c7cf0ebc 5503 warntime (get-text-property (point) 'org-appt-warntime)
bdebdb64 5504 extra nil)
20908596 5505
afe98dfa
CD
5506 (dolist (r (if (stringp result)
5507 (list result)
5508 result)) ;; we expect a list here
8223b1d2
BG
5509 (when (and org-agenda-diary-sexp-prefix
5510 (string-match org-agenda-diary-sexp-prefix r))
5511 (setq extra (match-string 0 r)
5512 r (replace-match "" nil nil r)))
afe98dfa
CD
5513 (if (string-match "\\S-" r)
5514 (setq txt r)
5515 (setq txt "SEXP entry returned empty string"))
5516
e66ba1df 5517 (setq txt (org-agenda-format-item
8223b1d2 5518 extra txt category tags 'time))
afe98dfa
CD
5519 (org-add-props txt props 'org-marker marker)
5520 (org-add-props txt nil
5521 'org-category category 'date date 'todo-state todo-state
8223b1d2
BG
5522 'org-category-position category-pos 'tags tags
5523 'type "sexp" 'warntime warntime)
afe98dfa 5524 (push txt ee)))))
20908596
CD
5525 (nreverse ee)))
5526
3ab2c837
BG
5527;; Calendar sanity: define some functions that are independent of
5528;; `calendar-date-style'.
5529;; Normally I would like to use ISO format when calling the diary functions,
5530;; but to make sure we still have Emacs 22 compatibility we bind
5531;; also `european-calendar-style' and use european format
5532(defun org-anniversary (year month day &optional mark)
5533 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5534 (org-no-warnings
5535 (let ((calendar-date-style 'european) (european-calendar-style t))
5536 (diary-anniversary day month year mark))))
5537(defun org-cyclic (N year month day &optional mark)
5538 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5539 (org-no-warnings
5540 (let ((calendar-date-style 'european) (european-calendar-style t))
5541 (diary-cyclic N day month year mark))))
5542(defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5543 "Like `diary-block', but with fixed (ISO) order of arguments."
5544 (org-no-warnings
5545 (let ((calendar-date-style 'european) (european-calendar-style t))
5546 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5547(defun org-date (year month day &optional mark)
5548 "Like `diary-date', but with fixed (ISO) order of arguments."
5549 (org-no-warnings
5550 (let ((calendar-date-style 'european) (european-calendar-style t))
5551 (diary-date day month year mark))))
3ab2c837
BG
5552
5553;; Define the` org-class' function
5554(defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
ed21c5c8 5555 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
e66ba1df
BG
5556DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5557SKIP-WEEKS is any number of ISO weeks in the block period for which the
5558item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5559`holidays', then any date that is known by the Emacs calendar to be a
27e428e7 5560holiday will also be skipped."
3ab2c837
BG
5561 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5562 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
ed21c5c8
CD
5563 (d (calendar-absolute-from-gregorian date)))
5564 (and
5565 (<= date1 d)
5566 (<= d date2)
5567 (= (calendar-day-of-week date) dayname)
5568 (or (not skip-weeks)
5569 (progn
5570 (require 'cal-iso)
5571 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
e66ba1df
BG
5572 (not (and (memq 'holidays skip-weeks)
5573 (calendar-check-holidays date)))
23f6720e 5574 entry)))
ed21c5c8 5575
3ab2c837
BG
5576(defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5577 "Like `org-class', but honor `calendar-date-style'.
5578The order of the first 2 times 3 arguments depends on the variable
5579`calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5580So for American calendars, give this as MONTH DAY YEAR, for European as
5581DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5582DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5583is any number of ISO weeks in the block period for which the item should
5584be skipped.
5585
5586This function is here only for backward compatibility and it is deprecated,
5587please use `org-class' instead."
5588 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5589 (date2 (org-order-calendar-date-args m2 d2 y2)))
5590 (org-class
5591 (nth 2 date1) (car date1) (nth 1 date1)
5592 (nth 2 date2) (car date2) (nth 1 date2)
5593 dayname skip-weeks)))
e66ba1df 5594(make-obsolete 'org-diary-class 'org-class "")
3ab2c837 5595
8223b1d2 5596(defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
d60b1ba1 5597(defalias 'org-get-closed 'org-agenda-get-progress)
93b62de8 5598(defun org-agenda-get-progress ()
20908596
CD
5599 "Return the logged TODO entries for agenda display."
5600 (let* ((props (list 'mouse-face 'highlight
5601 'org-not-done-regexp org-not-done-regexp
5602 'org-todo-regexp org-todo-regexp
b349f79f 5603 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
5604 'help-echo
5605 (format "mouse-2 or RET jump to org file %s"
5606 (abbreviate-file-name buffer-file-name))))
8223b1d2
BG
5607 (items (if (consp org-agenda-show-log-scoped)
5608 org-agenda-show-log-scoped
5609 (if (eq org-agenda-show-log-scoped 'clockcheck)
3ab2c837
BG
5610 '(clock)
5611 org-agenda-log-mode-items)))
ff4be292 5612 (parts
93b62de8
CD
5613 (delq nil
5614 (list
5615 (if (memq 'closed items) (concat "\\<" org-closed-string))
5616 (if (memq 'clock items) (concat "\\<" org-clock-string))
c8d0cf5c 5617 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
93b62de8
CD
5618 (parts-re (if parts (mapconcat 'identity parts "\\|")
5619 (error "`org-agenda-log-mode-items' is empty")))
20908596 5620 (regexp (concat
93b62de8
CD
5621 "\\(" parts-re "\\)"
5622 " *\\["
20908596
CD
5623 (regexp-quote
5624 (substring
5625 (format-time-string
5626 (car org-time-stamp-formats)
5627 (apply 'encode-time ; DATE bound by calendar
5628 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5629 1 11))))
c8d0cf5c 5630 (org-agenda-search-headline-for-time nil)
8223b1d2 5631 marker hdmarker priority category category-pos tags closedp
e66ba1df 5632 statep clockp state ee txt extra timestr rest clocked)
20908596
CD
5633 (goto-char (point-min))
5634 (while (re-search-forward regexp nil t)
5635 (catch :skip
5636 (org-agenda-skip)
5637 (setq marker (org-agenda-new-marker (match-beginning 0))
5638 closedp (equal (match-string 1) org-closed-string)
93b62de8 5639 statep (equal (string-to-char (match-string 1)) ?-)
c8d0cf5c 5640 clockp (not (or closedp statep))
93b62de8 5641 state (and statep (match-string 2))
20908596 5642 category (org-get-category (match-beginning 0))
8223b1d2 5643 category-pos (get-text-property (match-beginning 0) 'org-category-position)
e66ba1df 5644 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
b349f79f
CD
5645 (when (string-match "\\]" timestr)
5646 ;; substring should only run to end of time stamp
5647 (setq rest (substring timestr (match-end 0))
5648 timestr (substring timestr 0 (match-end 0)))
93b62de8 5649 (if (and (not closedp) (not statep)
e66ba1df
BG
5650 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5651 rest))
621f83e4
CD
5652 (progn (setq timestr (concat (substring timestr 0 -1)
5653 "-" (match-string 1 rest) "]"))
5654 (setq clocked (match-string 2 rest)))
5655 (setq clocked "-")))
20908596 5656 (save-excursion
3ab2c837
BG
5657 (setq extra
5658 (cond
5659 ((not org-agenda-log-mode-add-notes) nil)
5660 (statep
5661 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5662 (match-string 1)))
5663 (clockp
5664 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5665 (match-string 1)))))
5666 (if (not (re-search-backward org-outline-regexp-bol nil t))
c8d0cf5c
CD
5667 (setq txt org-agenda-no-heading-message)
5668 (goto-char (match-beginning 0))
5669 (setq hdmarker (org-agenda-new-marker)
c7cf0ebc 5670 tags (org-get-tags-at nil t))
c8d0cf5c
CD
5671 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5672 (setq txt (match-string 1))
5673 (when extra
5674 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5675 (setq txt (concat (substring txt 0 (match-beginning 1))
5676 " - " extra " " (match-string 2 txt)))
5677 (setq txt (concat txt " - " extra))))
e66ba1df 5678 (setq txt (org-agenda-format-item
c8d0cf5c
CD
5679 (cond
5680 (closedp "Closed: ")
8223b1d2
BG
5681 (statep (concat "State: (" state ")"))
5682 (t (concat "Clocked: (" clocked ")")))
c8d0cf5c 5683 txt category tags timestr)))
20908596
CD
5684 (setq priority 100000)
5685 (org-add-props txt props
c8d0cf5c 5686 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
20908596 5687 'priority priority 'org-category category
8223b1d2 5688 'org-category-position category-pos
20908596 5689 'type "closed" 'date date
c8d0cf5c 5690 'undone-face 'org-warning 'done-face 'org-agenda-done)
20908596
CD
5691 (push txt ee))
5692 (goto-char (point-at-eol))))
5693 (nreverse ee)))
5694
3ab2c837
BG
5695(defun org-agenda-show-clocking-issues ()
5696 "Add overlays, showing issues with clocking.
5697See also the user option `org-agenda-clock-consistency-checks'."
5698 (interactive)
5699 (let* ((pl org-agenda-clock-consistency-checks)
5700 (re (concat "^[ \t]*"
5701 org-clock-string
5702 "[ \t]+"
5703 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5704 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5705 (tlstart 0.)
5706 (tlend 0.)
fe3c5669 5707 (maxtime (org-hh:mm-string-to-minutes
3ab2c837 5708 (or (plist-get pl :max-duration) "24:00")))
fe3c5669 5709 (mintime (org-hh:mm-string-to-minutes
3ab2c837
BG
5710 (or (plist-get pl :min-duration) 0)))
5711 (maxgap (org-hh:mm-string-to-minutes
5712 ;; default 30:00 means never complain
5713 (or (plist-get pl :max-gap) "30:00")))
5714 (gapok (mapcar 'org-hh:mm-string-to-minutes
5715 (plist-get pl :gap-ok-around)))
5716 (def-face (or (plist-get pl :default-face)
5717 '((:background "DarkRed") (:foreground "white"))))
5718 issue face m te ts dt ov)
5719 (goto-char (point-min))
5720 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5721 (setq issue nil face def-face)
5722 (catch 'next
5723 (setq m (org-get-at-bol 'org-marker)
5724 te nil ts nil)
5725 (unless (and m (markerp m))
5726 (setq issue "No valid clock line") (throw 'next t))
5727 (org-with-point-at m
5728 (save-excursion
5729 (goto-char (point-at-bol))
5730 (unless (looking-at re)
5731 (error "No valid Clock line")
5732 (throw 'next t))
5733 (unless (match-end 3)
5734 (setq issue "No end time"
5735 face (or (plist-get pl :no-end-time-face) face))
5736 (throw 'next t))
5737 (setq ts (match-string 1)
5738 te (match-string 3)
5739 ts (org-float-time
5740 (apply 'encode-time (org-parse-time-string ts)))
5741 te (org-float-time
5742 (apply 'encode-time (org-parse-time-string te)))
5743 dt (- te ts))))
5744 (cond
5745 ((> dt (* 60 maxtime))
5746 ;; a very long clocking chunk
5747 (setq issue (format "Clocking interval is very long: %s"
5748 (org-minutes-to-hh:mm-string
5749 (floor (/ (float dt) 60.))))
5750 face (or (plist-get pl :long-face) face)))
5751 ((< dt (* 60 mintime))
5752 ;; a very short clocking chunk
5753 (setq issue (format "Clocking interval is very short: %s"
5754 (org-minutes-to-hh:mm-string
5755 (floor (/ (float dt) 60.))))
5756 face (or (plist-get pl :short-face) face)))
5757 ((and (> tlend 0) (< ts tlend))
5758 ;; Two clock entries are overlapping
5759 (setq issue (format "Clocking overlap: %d minutes"
5760 (/ (- tlend ts) 60))
5761 face (or (plist-get pl :overlap-face) face)))
5762 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
e66ba1df 5763 ;; There is a gap, lets see if we need to report it
3ab2c837
BG
5764 (unless (org-agenda-check-clock-gap tlend ts gapok)
5765 (setq issue (format "Clocking gap: %d minutes"
8223b1d2 5766 (/ (- ts tlend) 60))
3ab2c837
BG
5767 face (or (plist-get pl :gap-face) face))))
5768 (t nil)))
5769 (setq tlend (or te tlend) tlstart (or ts tlstart))
5770 (when issue
5771 ;; OK, there was some issue, add an overlay to show the issue
5772 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5773 (overlay-put ov 'before-string
5774 (concat
5775 (org-add-props
5776 (format "%-43s" (concat " " issue))
5777 nil
5778 'face face)
5779 "\n"))
5780 (overlay-put ov 'evaporate t)))))
5781
5782(defun org-agenda-check-clock-gap (t1 t2 ok-list)
5783 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5784 (catch 'exit
5785 (unless ok-list
5786 ;; there are no OK times for gaps...
5787 (throw 'exit nil))
5788 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5789 ;; This is more than 24 hours, so it is OK.
5790 ;; because we have at least one OK time, that must be in the
5791 ;; 24 hour interval.
5792 (throw 'exit t))
5793 ;; We have a shorter gap.
5794 ;; Now we have to get the minute of the day when these times are
5795 (let* ((t1dec (decode-time (seconds-to-time t1)))
5796 (t2dec (decode-time (seconds-to-time t2)))
5797 ;; compute the minute on the day
5798 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5799 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5800 (when (< min2 min1)
5801 ;; if min2 is smaller than min1, this means it is on the next day.
5802 ;; Wrap it to after midnight.
5803 (setq min2 (+ min2 1440)))
5804 ;; Now check if any of the OK times is in the gap
5805 (mapc (lambda (x)
5806 ;; Wrap the time to after midnight if necessary
5807 (if (< x min1) (setq x (+ x 1440)))
5808 ;; Check if in interval
5809 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5810 ok-list)
5811 ;; Nope, this gap is not OK
5812 nil)))
5813
20908596
CD
5814(defun org-agenda-get-deadlines ()
5815 "Return the deadline information for agenda display."
5816 (let* ((props (list 'mouse-face 'highlight
5817 'org-not-done-regexp org-not-done-regexp
5818 'org-todo-regexp org-todo-regexp
b349f79f 5819 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
5820 'help-echo
5821 (format "mouse-2 or RET jump to org file %s"
5822 (abbreviate-file-name buffer-file-name))))
5823 (regexp org-deadline-time-regexp)
621f83e4 5824 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596 5825 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
8223b1d2 5826 d2 diff dfrac wdays pos pos1 category category-pos
e66ba1df 5827 tags suppress-prewarning ee txt head face s todo-state
8223b1d2 5828 show-all upcomingp donep timestr warntime)
20908596
CD
5829 (goto-char (point-min))
5830 (while (re-search-forward regexp nil t)
ed21c5c8 5831 (setq suppress-prewarning nil)
20908596
CD
5832 (catch :skip
5833 (org-agenda-skip)
ed21c5c8
CD
5834 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5835 (save-match-data
5836 (string-match org-scheduled-time-regexp
5837 (buffer-substring (point-at-bol)
5838 (point-at-eol)))))
5839 (setq suppress-prewarning
5840 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5841 org-agenda-skip-deadline-prewarning-if-scheduled
5842 0)))
20908596 5843 (setq s (match-string 1)
c8d0cf5c 5844 txt nil
20908596 5845 pos (1- (match-beginning 1))
3ab2c837
BG
5846 todo-state (save-match-data (org-get-todo-state))
5847 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5848 (member todo-state
8223b1d2 5849 org-agenda-repeating-timestamp-show-all))
20908596 5850 d2 (org-time-string-to-absolute
e66ba1df
BG
5851 (match-string 1) d1 'past show-all
5852 (current-buffer) pos)
20908596 5853 diff (- d2 d1)
ed21c5c8
CD
5854 wdays (if suppress-prewarning
5855 (let ((org-deadline-warning-days suppress-prewarning))
5856 (org-get-wdays s))
5857 (org-get-wdays s))
e66ba1df 5858 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
20908596
CD
5859 upcomingp (and todayp (> diff 0)))
5860 ;; When to show a deadline in the calendar:
5861 ;; If the expiration is within wdays warning time.
5862 ;; Past-due deadlines are only shown on the current date
8bfe682a
CD
5863 (if (and (or (and (<= diff wdays)
5864 (and todayp (not org-agenda-only-exact-dates)))
5865 (= diff 0)))
20908596 5866 (save-excursion
3ab2c837 5867 ;; (setq todo-state (org-get-todo-state))
c8d0cf5c
CD
5868 (setq donep (member todo-state org-done-keywords))
5869 (if (and donep
5870 (or org-agenda-skip-deadline-if-done
5871 (not (= diff 0))))
5872 (setq txt nil)
e66ba1df 5873 (setq category (org-get-category)
c7cf0ebc 5874 warntime (get-text-property (point) 'org-appt-warntime)
8223b1d2 5875 category-pos (get-text-property (point) 'org-category-position))
c8d0cf5c
CD
5876 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5877 (setq txt org-agenda-no-heading-message)
5878 (goto-char (match-end 0))
5879 (setq pos1 (match-beginning 0))
c7cf0ebc 5880 (setq tags (org-get-tags-at pos1 t))
c8d0cf5c
CD
5881 (setq head (buffer-substring-no-properties
5882 (point)
5883 (progn (skip-chars-forward "^\r\n")
5884 (point))))
5885 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5886 (setq timestr
5887 (concat (substring s (match-beginning 1)) " "))
5888 (setq timestr 'time))
e66ba1df 5889 (setq txt (org-agenda-format-item
c8d0cf5c
CD
5890 (if (= diff 0)
5891 (car org-agenda-deadline-leaders)
5892 (if (functionp
5893 (nth 1 org-agenda-deadline-leaders))
5894 (funcall
5895 (nth 1 org-agenda-deadline-leaders)
5896 diff date)
5897 (format (nth 1 org-agenda-deadline-leaders)
5898 diff)))
5899 head category tags
5900 (if (not (= diff 0)) nil timestr)))))
20908596 5901 (when txt
e66ba1df 5902 (setq face (org-agenda-deadline-face dfrac))
20908596
CD
5903 (org-add-props txt props
5904 'org-marker (org-agenda-new-marker pos)
8223b1d2 5905 'warntime warntime
20908596
CD
5906 'org-hd-marker (org-agenda-new-marker pos1)
5907 'priority (+ (- diff)
5908 (org-get-priority txt))
5909 'org-category category
8223b1d2 5910 'org-category-position category-pos
621f83e4 5911 'todo-state todo-state
20908596
CD
5912 'type (if upcomingp "upcoming-deadline" "deadline")
5913 'date (if upcomingp date d2)
c8d0cf5c
CD
5914 'face (if donep 'org-agenda-done face)
5915 'undone-face face 'done-face 'org-agenda-done)
20908596
CD
5916 (push txt ee))))))
5917 (nreverse ee)))
5918
e66ba1df 5919(defun org-agenda-deadline-face (fraction)
20908596
CD
5920 "Return the face to displaying a deadline item.
5921FRACTION is what fraction of the head-warning time has passed."
20908596
CD
5922 (let ((faces org-agenda-deadline-faces) f)
5923 (catch 'exit
5924 (while (setq f (pop faces))
5925 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5926
54a0dee5 5927(defun org-agenda-get-scheduled (&optional deadline-results)
20908596
CD
5928 "Return the scheduled information for agenda display."
5929 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5930 'org-todo-regexp org-todo-regexp
b349f79f 5931 'org-complex-heading-regexp org-complex-heading-regexp
c8d0cf5c 5932 'done-face 'org-agenda-done
20908596 5933 'mouse-face 'highlight
20908596
CD
5934 'help-echo
5935 (format "mouse-2 or RET jump to org file %s"
5936 (abbreviate-file-name buffer-file-name))))
5937 (regexp org-scheduled-time-regexp)
621f83e4 5938 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596 5939 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
54a0dee5
CD
5940 mm
5941 (deadline-position-alist
5942 (mapcar (lambda (a) (and (setq mm (get-text-property
8223b1d2
BG
5943 0 'org-hd-marker a))
5944 (cons (marker-position mm) a)))
54a0dee5 5945 deadline-results))
8223b1d2
BG
5946 d2 diff pos pos1 category category-pos tags donep
5947 ee txt head pastschedp todo-state face timestr s habitp show-all
5948 did-habit-check-p warntime)
20908596
CD
5949 (goto-char (point-min))
5950 (while (re-search-forward regexp nil t)
5951 (catch :skip
5952 (org-agenda-skip)
5953 (setq s (match-string 1)
c8d0cf5c 5954 txt nil
20908596 5955 pos (1- (match-beginning 1))
3ab2c837
BG
5956 todo-state (save-match-data (org-get-todo-state))
5957 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5958 (member todo-state
5959 org-agenda-repeating-timestamp-show-all))
20908596 5960 d2 (org-time-string-to-absolute
e66ba1df
BG
5961 (match-string 1) d1 'past show-all
5962 (current-buffer) pos)
8223b1d2 5963 diff (- d2 d1)
c7cf0ebc 5964 warntime (get-text-property (point) 'org-appt-warntime))
20908596 5965 (setq pastschedp (and todayp (< diff 0)))
8223b1d2 5966 (setq did-habit-check-p nil)
20908596
CD
5967 ;; When to show a scheduled item in the calendar:
5968 ;; If it is on or past the date.
8bfe682a
CD
5969 (when (or (and (< diff 0)
5970 (< (abs diff) org-scheduled-past-days)
5971 (and todayp (not org-agenda-only-exact-dates)))
8223b1d2
BG
5972 (= diff 0)
5973 ;; org-is-habit-p uses org-entry-get, which is expansive
5974 ;; so we go extra mile to only call it once
5975 (and todayp
5976 (boundp 'org-habit-show-all-today)
5977 org-habit-show-all-today
5978 (setq did-habit-check-p t)
5979 (setq habitp (and (functionp 'org-is-habit-p)
5980 (org-is-habit-p)))))
8bfe682a 5981 (save-excursion
8bfe682a 5982 (setq donep (member todo-state org-done-keywords))
8bfe682a 5983 (if (and donep
3ab2c837
BG
5984 (or org-agenda-skip-scheduled-if-done
5985 (not (= diff 0))
5986 (and (functionp 'org-is-habit-p)
5987 (org-is-habit-p))))
8bfe682a 5988 (setq txt nil)
8223b1d2
BG
5989 (setq habitp (if did-habit-check-p habitp
5990 (and (functionp 'org-is-habit-p)
5991 (org-is-habit-p))))
e66ba1df 5992 (setq category (org-get-category)
8223b1d2 5993 category-pos (get-text-property (point) 'org-category-position))
8bfe682a
CD
5994 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5995 (setq txt org-agenda-no-heading-message)
5996 (goto-char (match-end 0))
5997 (setq pos1 (match-beginning 0))
5998 (if habitp
5999 (if (or (not org-habit-show-habits)
6000 (and (not todayp)
8223b1d2 6001 (boundp 'org-habit-show-habits-only-for-today)
8bfe682a
CD
6002 org-habit-show-habits-only-for-today))
6003 (throw :skip nil))
54a0dee5
CD
6004 (if (and
6005 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6006 (and org-agenda-skip-scheduled-if-deadline-is-shown
6007 pastschedp))
6008 (setq mm (assoc pos1 deadline-position-alist)))
8bfe682a 6009 (throw :skip nil)))
c7cf0ebc 6010 (setq tags (org-get-tags-at nil t))
8bfe682a
CD
6011 (setq head (buffer-substring-no-properties
6012 (point)
6013 (progn (skip-chars-forward "^\r\n") (point))))
6014 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6015 (setq timestr
6016 (concat (substring s (match-beginning 1)) " "))
6017 (setq timestr 'time))
e66ba1df 6018 (setq txt (org-agenda-format-item
8bfe682a
CD
6019 (if (= diff 0)
6020 (car org-agenda-scheduled-leaders)
6021 (format (nth 1 org-agenda-scheduled-leaders)
6022 (- 1 diff)))
6023 head category tags
6024 (if (not (= diff 0)) nil timestr)
3ab2c837 6025 nil habitp))))
8bfe682a
CD
6026 (when txt
6027 (setq face
6028 (cond
6029 ((and (not habitp) pastschedp)
6030 'org-scheduled-previously)
6031 (todayp 'org-scheduled-today)
6032 (t 'org-scheduled))
6033 habitp (and habitp (org-habit-parse-todo)))
6034 (org-add-props txt props
6035 'undone-face face
6036 'face (if donep 'org-agenda-done face)
6037 'org-marker (org-agenda-new-marker pos)
6038 'org-hd-marker (org-agenda-new-marker pos1)
6039 'type (if pastschedp "past-scheduled" "scheduled")
6040 'date (if pastschedp d2 date)
8223b1d2 6041 'warntime warntime
8bfe682a
CD
6042 'priority (if habitp
6043 (org-habit-get-priority habitp)
6044 (+ 94 (- 5 diff) (org-get-priority txt)))
6045 'org-category category
8223b1d2 6046 'category-position category-pos
8bfe682a
CD
6047 'org-habit-p habitp
6048 'todo-state todo-state)
6049 (push txt ee))))))
20908596
CD
6050 (nreverse ee)))
6051
6052(defun org-agenda-get-blocks ()
6053 "Return the date-range information for agenda display."
6054 (let* ((props (list 'face nil
6055 'org-not-done-regexp org-not-done-regexp
6056 'org-todo-regexp org-todo-regexp
b349f79f 6057 'org-complex-heading-regexp org-complex-heading-regexp
20908596 6058 'mouse-face 'highlight
20908596
CD
6059 'help-echo
6060 (format "mouse-2 or RET jump to org file %s"
6061 (abbreviate-file-name buffer-file-name))))
6062 (regexp org-tr-regexp)
6063 (d0 (calendar-absolute-from-gregorian date))
8223b1d2 6064 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
e66ba1df 6065 todo-state tags pos head donep)
20908596
CD
6066 (goto-char (point-min))
6067 (while (re-search-forward regexp nil t)
6068 (catch :skip
6069 (org-agenda-skip)
6070 (setq pos (point))
3ab2c837
BG
6071 (let ((start-time (match-string 1))
6072 (end-time (match-string 2)))
6073 (setq s1 (match-string 1)
6074 s2 (match-string 2)
e66ba1df
BG
6075 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6076 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
3ab2c837
BG
6077 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6078 ;; Only allow days between the limits, because the normal
6079 ;; date stamps will catch the limits.
6080 (save-excursion
6081 (setq todo-state (org-get-todo-state))
6082 (setq donep (member todo-state org-done-keywords))
6083 (if (and donep org-agenda-skip-timestamp-if-done)
6084 (throw :skip t))
6085 (setq marker (org-agenda-new-marker (point)))
e66ba1df 6086 (setq category (org-get-category)
8223b1d2 6087 category-pos (get-text-property (point) 'org-category-position))
3ab2c837
BG
6088 (if (not (re-search-backward org-outline-regexp-bol nil t))
6089 (setq txt org-agenda-no-heading-message)
6090 (goto-char (match-beginning 0))
6091 (setq hdmarker (org-agenda-new-marker (point)))
c7cf0ebc 6092 (setq tags (org-get-tags-at nil t))
3ab2c837
BG
6093 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6094 (setq head (match-string 1))
6095 (let ((remove-re
6096 (if org-agenda-remove-timeranges-from-blocks
6097 (concat
6098 "<" (regexp-quote s1) ".*?>"
6099 "--"
6100 "<" (regexp-quote s2) ".*?>")
6101 nil)))
e66ba1df 6102 (setq txt (org-agenda-format-item
3ab2c837
BG
6103 (format
6104 (nth (if (= d1 d2) 0 1)
6105 org-agenda-timerange-leaders)
6106 (1+ (- d0 d1)) (1+ (- d2 d1)))
6107 head category tags
e66ba1df
BG
6108 (cond ((and (= d1 d0) (= d2 d0))
6109 (concat "<" start-time ">--<" end-time ">"))
6110 ((= d1 d0)
3ab2c837
BG
6111 (concat "<" start-time ">"))
6112 ((= d2 d0)
8223b1d2 6113 (concat "<" end-time ">")))
bdebdb64 6114 remove-re))))
3ab2c837
BG
6115 (org-add-props txt props
6116 'org-marker marker 'org-hd-marker hdmarker
6117 'type "block" 'date date
6118 'todo-state todo-state
e66ba1df 6119 'priority (org-get-priority txt) 'org-category category
8223b1d2 6120 'org-category-position category-pos)
3ab2c837 6121 (push txt ee))))
20908596
CD
6122 (goto-char pos)))
6123 ;; Sort the entries by expiration date.
6124 (nreverse ee)))
6125
6126;;; Agenda presentation and sorting
6127
6128(defvar org-prefix-has-time nil
6129 "A flag, set by `org-compile-prefix-format'.
6130The flag is set if the currently compiled format contains a `%t'.")
6131(defvar org-prefix-has-tag nil
6132 "A flag, set by `org-compile-prefix-format'.
6133The flag is set if the currently compiled format contains a `%T'.")
6134(defvar org-prefix-has-effort nil
6135 "A flag, set by `org-compile-prefix-format'.
6136The flag is set if the currently compiled format contains a `%e'.")
8d642074 6137(defvar org-prefix-category-length nil
86fbb8ca 6138 "Used by `org-compile-prefix-format' to remember the category field width.")
8bfe682a 6139(defvar org-prefix-category-max-length nil
86fbb8ca 6140 "Used by `org-compile-prefix-format' to remember the category field width.")
20908596 6141
acedf35c
CD
6142(defun org-agenda-get-category-icon (category)
6143 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6144 (dolist (entry org-agenda-category-icon-alist)
6145 (when (org-string-match-p (car entry) category)
6146 (if (listp (cadr entry))
6147 (return (cadr entry))
8223b1d2 6148 (return (apply 'create-image (cdr entry)))))))
acedf35c 6149
e66ba1df 6150(defun org-agenda-format-item (extra txt &optional category tags dotime
3ab2c837 6151 remove-re habitp)
20908596
CD
6152 "Format TXT to be inserted into the agenda buffer.
6153In particular, it adds the prefix and corresponding text properties. EXTRA
6154must be a string and replaces the `%s' specifier in the prefix format.
6155CATEGORY (string, symbol or nil) may be used to overrule the default
6156category taken from local variable or file name. It will replace the `%c'
6157specifier in the format. DOTIME, when non-nil, indicates that a
6158time-of-day should be extracted from TXT for sorting of this entry, and for
6159the `%t' specifier in the format. When DOTIME is a string, this string is
3ab2c837 6160searched for a time before TXT is. TAGS can be the tags of the headline.
20908596 6161Any match of REMOVE-RE will be removed from TXT."
8223b1d2
BG
6162 ;; We keep the org-prefix-* variable values along with a compiled
6163 ;; formatter, so that multiple agendas existing at the same time, do
6164 ;; not step on each other toes.
6165 ;;
6166 ;; It was inconvenient to make these variables buffer local in
6167 ;; Agenda buffers, because this function expects to be called with
6168 ;; the buffer where item comes from being current, and not agenda
6169 ;; buffer
6170 (let* ((bindings (car org-prefix-format-compiled))
6171 (formatter (cadr org-prefix-format-compiled)))
6172 (loop for (var value) in bindings
6173 do (set var value))
6174 (save-match-data
6175 ;; Diary entries sometimes have extra whitespace at the beginning
6176 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
6177
6178 ;; Fix the tags part in txt
6179 (setq txt (org-agenda-fix-displayed-tags
6180 txt tags
6181 org-agenda-show-inherited-tags
6182 org-agenda-hide-tags-regexp))
6183 (let* ((category (or category
6184 (if (stringp org-category)
6185 org-category
6186 (and org-category (symbol-name org-category)))
6187 (if buffer-file-name
6188 (file-name-sans-extension
6189 (file-name-nondirectory buffer-file-name))
6190 "")))
6191 (category-icon (org-agenda-get-category-icon category))
6192 (category-icon (if category-icon
6193 (propertize " " 'display category-icon)
6194 ""))
6195 ;; time, tag, effort are needed for the eval of the prefix format
6196 (tag (if tags (nth (1- (length tags)) tags) ""))
6197 time effort neffort
6198 (ts (if dotime (concat
6199 (if (stringp dotime) dotime "")
6200 (and org-agenda-search-headline-for-time txt))))
6201 (time-of-day (and dotime (org-get-time-of-day ts)))
6202 stamp plain s0 s1 s2 rtn srp l
6203 duration thecategory)
6204 (and (derived-mode-p 'org-mode) buffer-file-name
6205 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6206 (when (and dotime time-of-day)
6207 ;; Extract starting and ending time and move them to prefix
6208 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6209 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6210 (setq s0 (match-string 0 ts)
6211 srp (and stamp (match-end 3))
6212 s1 (match-string (if plain 1 2) ts)
6213 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6214
6215 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6216 ;; them, we might want to remove them there to avoid duplication.
6217 ;; The user can turn this off with a variable.
6218 (if (and org-prefix-has-time
6219 org-agenda-remove-times-when-in-prefix (or stamp plain)
6220 (string-match (concat (regexp-quote s0) " *") txt)
6221 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6222 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6223 (= (match-beginning 0) 0)
6224 t))
6225 (setq txt (replace-match "" nil nil txt))))
6226 ;; Normalize the time(s) to 24 hour
6227 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6228 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6229
6230 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6231 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6232 (setq s2
6233 (org-minutes-to-hh:mm-string
6234 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
6235
6236 ;; Compute the duration
6237 (when s2
6238 (setq duration (- (org-hh:mm-string-to-minutes s2)
6239 (org-hh:mm-string-to-minutes s1)))))
6240
6241 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6242 txt)
6243 ;; Tags are in the string
6244 (if (or (eq org-agenda-remove-tags t)
6245 (and org-agenda-remove-tags
6246 org-prefix-has-tag))
6247 (setq txt (replace-match "" t t txt))
6248 (setq txt (replace-match
6249 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6250 (match-string 2 txt))
6251 t t txt))))
6252 (when (derived-mode-p 'org-mode)
c7cf0ebc 6253 (setq effort (ignore-errors (get-text-property 0 'org-effort txt)))
8223b1d2
BG
6254 (when effort
6255 (setq neffort (org-duration-string-to-minutes effort)
6256 effort (setq effort (concat "[" effort "]")))))
6257 ;; prevent erroring out with %e format when there is no effort
6258 (or effort (setq effort ""))
6259
6260 (when remove-re
6261 (while (string-match remove-re txt)
6262 (setq txt (replace-match "" t t txt))))
6263
6264 ;; Set org-heading property on `txt' to mark the start of the
6265 ;; heading.
6266 (add-text-properties 0 (length txt) '(org-heading t) txt)
6267
6268 ;; Prepare the variables needed in the eval of the compiled format
6269 (setq time (cond (s2 (concat
6270 (org-agenda-time-of-day-to-ampm-maybe s1)
6271 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6272 (if org-agenda-timegrid-use-ampm " ")))
6273 (s1 (concat
6274 (org-agenda-time-of-day-to-ampm-maybe s1)
6275 (if org-agenda-timegrid-use-ampm
6276 "........ "
6277 "......")))
6278 (t ""))
6279 extra (or (and (not habitp) extra) "")
6280 category (if (symbolp category) (symbol-name category) category)
6281 thecategory (copy-sequence category))
6282 (if (string-match org-bracket-link-regexp category)
6283 (progn
6284 (setq l (if (match-end 3)
6285 (- (match-end 3) (match-beginning 3))
6286 (- (match-end 1) (match-beginning 1))))
6287 (when (< l (or org-prefix-category-length 0))
6288 (setq category (copy-sequence category))
6289 (org-add-props category nil
6290 'extra-space (make-string
6291 (- org-prefix-category-length l 1) ?\ ))))
6292 (if (and org-prefix-category-max-length
6293 (>= (length category) org-prefix-category-max-length))
6294 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6295 ;; Evaluate the compiled format
6296 (setq rtn (concat (eval formatter) txt))
6297
6298 ;; And finally add the text properties
6299 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6300 (org-add-props rtn nil
6301 'org-category (if thecategory (downcase thecategory) category)
6302 'tags (mapcar 'org-downcase-keep-props tags)
6303 'org-highest-priority org-highest-priority
6304 'org-lowest-priority org-lowest-priority
6305 'time-of-day time-of-day
6306 'duration duration
6307 'effort effort
6308 'effort-minutes neffort
6309 'txt txt
6310 'time time
6311 'extra extra
6312 'format org-prefix-format-compiled
6313 'dotime dotime)))))
20908596 6314
5dec9555
CD
6315(defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6316 "Remove tags string from TXT, and add a modified list of tags.
6317The modified list may contain inherited tags, and tags matched by
6318`org-agenda-hide-tags-regexp' will be removed."
6319 (when (or add-inherited hide-re)
afe98dfa 6320 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5dec9555 6321 (setq txt (substring txt 0 (match-beginning 0))))
ed21c5c8
CD
6322 (setq tags
6323 (delq nil
6324 (mapcar (lambda (tg)
6325 (if (or (and hide-re (string-match hide-re tg))
6326 (and (not add-inherited)
6327 (get-text-property 0 'inherited tg)))
6328 nil
6329 tg))
6330 tags)))
5dec9555 6331 (when tags
5dec9555
CD
6332 (let ((have-i (get-text-property 0 'inherited (car tags)))
6333 i)
6334 (setq txt (concat txt " :"
6335 (mapconcat
6336 (lambda (x)
6337 (setq i (get-text-property 0 'inherited x))
6338 (if (and have-i (not i))
6339 (progn
6340 (setq have-i nil)
6341 (concat ":" x))
6342 x))
6343 tags ":")
6344 (if have-i "::" ":"))))))
8223b1d2 6345 txt)
ff4be292
CD
6346
6347(defun org-downcase-keep-props (s)
6348 (let ((props (text-properties-at 0 s)))
6349 (setq s (downcase s))
6350 (add-text-properties 0 (length s) props s)
6351 s))
6352
20908596
CD
6353(defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6354(defvar org-agenda-sorting-strategy-selected nil)
6355
6356(defun org-agenda-add-time-grid-maybe (list ndays todayp)
6357 (catch 'exit
6358 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6359 ((and todayp (member 'today (car org-agenda-time-grid))))
6360 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6361 ((member 'weekly (car org-agenda-time-grid)))
6362 (t (throw 'exit list)))
6363 (let* ((have (delq nil (mapcar
6364 (lambda (x) (get-text-property 1 'time-of-day x))
6365 list)))
6366 (string (nth 1 org-agenda-time-grid))
6367 (gridtimes (nth 2 org-agenda-time-grid))
6368 (req (car org-agenda-time-grid))
6369 (remove (member 'remove-match req))
6370 new time)
6371 (if (and (member 'require-timed req) (not have))
6372 ;; don't show empty grid
6373 (throw 'exit list))
6374 (while (setq time (pop gridtimes))
6375 (unless (and remove (member time have))
afe98dfa 6376 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
e66ba1df 6377 (push (org-agenda-format-item
20908596
CD
6378 nil string "" nil
6379 (concat (substring time 0 -2) ":" (substring time -2)))
6380 new)
6381 (put-text-property
afe98dfa 6382 2 (length (car new)) 'face 'org-time-grid (car new))))
3ab2c837 6383 (when (and todayp org-agenda-show-current-time-in-grid)
e66ba1df 6384 (push (org-agenda-format-item
3ab2c837
BG
6385 nil
6386 org-agenda-current-time-string
6387 "" nil
6388 (format-time-string "%H:%M "))
6389 new)
6390 (put-text-property
6391 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6392
20908596
CD
6393 (if (member 'time-up org-agenda-sorting-strategy-selected)
6394 (append new list)
6395 (append list new)))))
6396
6397(defun org-compile-prefix-format (key)
6398 "Compile the prefix format into a Lisp form that can be evaluated.
8223b1d2
BG
6399The resulting form and associated variable bindings is returned
6400and stored in the variable `org-prefix-format-compiled'."
20908596 6401 (setq org-prefix-has-time nil org-prefix-has-tag nil
e66ba1df
BG
6402 org-prefix-category-length nil
6403 org-prefix-has-effort nil)
20908596
CD
6404 (let ((s (cond
6405 ((stringp org-agenda-prefix-format)
6406 org-agenda-prefix-format)
6407 ((assq key org-agenda-prefix-format)
6408 (cdr (assq key org-agenda-prefix-format)))
6409 (t " %-12:c%?-12t% s")))
6410 (start 0)
6411 varform vars var e c f opt)
3ab2c837 6412 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
20908596 6413 s start)
3ab2c837
BG
6414 (setq var (or (cdr (assoc (match-string 4 s)
6415 '(("c" . category) ("t" . time) ("s" . extra)
6416 ("i" . category-icon) ("T" . tag) ("e" . effort))))
6417 'eval)
20908596
CD
6418 c (or (match-string 3 s) "")
6419 opt (match-beginning 1)
6420 start (1+ (match-beginning 0)))
6421 (if (equal var 'time) (setq org-prefix-has-time t))
6422 (if (equal var 'tag) (setq org-prefix-has-tag t))
6423 (if (equal var 'effort) (setq org-prefix-has-effort t))
6424 (setq f (concat "%" (match-string 2 s) "s"))
8bfe682a
CD
6425 (when (equal var 'category)
6426 (setq org-prefix-category-length
6427 (floor (abs (string-to-number (match-string 2 s)))))
6428 (setq org-prefix-category-max-length
6429 (let ((x (match-string 2 s)))
6430 (save-match-data
6431 (if (string-match "\\.[0-9]+" x)
6432 (string-to-number (substring (match-string 0 x) 1)))))))
3ab2c837
BG
6433 (if (eq var 'eval)
6434 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6435 (if opt
6436 (setq varform
6437 `(if (equal "" ,var)
6438 ""
6439 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6440 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
20908596
CD
6441 (setq s (replace-match "%s" t nil s))
6442 (push varform vars))
6443 (setq vars (nreverse vars))
8223b1d2
BG
6444 (with-current-buffer (or org-agenda-buffer (current-buffer))
6445 (setq org-prefix-format-compiled
6446 (list
6447 `((org-prefix-has-time ,org-prefix-has-time)
6448 (org-prefix-has-tag ,org-prefix-has-tag)
6449 (org-prefix-category-length ,org-prefix-category-length)
6450 (org-prefix-has-effort ,org-prefix-has-effort))
6451 `(format ,s ,@vars))))))
20908596
CD
6452
6453(defun org-set-sorting-strategy (key)
6454 (if (symbolp (car org-agenda-sorting-strategy))
6455 ;; the old format
6456 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6457 (setq org-agenda-sorting-strategy-selected
6458 (or (cdr (assq key org-agenda-sorting-strategy))
6459 (cdr (assq 'agenda org-agenda-sorting-strategy))
6460 '(time-up category-keep priority-down)))))
6461
6462(defun org-get-time-of-day (s &optional string mod24)
6463 "Check string S for a time of day.
6464If found, return it as a military time number between 0 and 2400.
6465If not found, return nil.
6466The optional STRING argument forces conversion into a 5 character wide string
6467HH:MM."
6468 (save-match-data
6469 (when
6470 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6471 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
8223b1d2
BG
6472 (let* ((h (string-to-number (match-string 1 s)))
6473 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6474 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6475 (am-p (equal ampm "am"))
6476 (h1 (cond ((not ampm) h)
6477 ((= h 12) (if am-p 0 12))
6478 (t (+ h (if am-p 0 12)))))
6479 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6480 (mod h1 24) h1))
6481 (t0 (+ (* 100 h2) m))
6482 (t1 (concat (if (>= h1 24) "+" " ")
6483 (if (and org-agenda-time-leading-zero
6484 (< t0 1000)) "0" "")
6485 (if (< t0 100) "0" "")
6486 (if (< t0 10) "0" "")
6487 (int-to-string t0))))
6488 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
20908596 6489
afe98dfa
CD
6490(defvar org-agenda-before-sorting-filter-function nil
6491 "Function to be applied to agenda items prior to sorting.
6492Prior to sorting also means just before they are inserted into the agenda.
6493
6494To aid sorting, you may revisit the original entries and add more text
6495properties which will later be used by the sorting functions.
6496
6497The function should take a string argument, an agenda line.
6498It has access to the text properties in that line, which contain among
6499other things, the property `org-hd-marker' that points to the entry
6500where the line comes from. Note that not all lines going into the agenda
6501have this property, only most.
6502
6503The function should return the modified string. It is probably best
6504to ONLY change text properties.
6505
6506You can also use this function as a filter, by returning nil for lines
6507you don't want to have in the agenda at all. For this application, you
6508could bind the variable in the options section of a custom command.")
6509
8223b1d2 6510(defun org-agenda-finalize-entries (list &optional nosort)
20908596
CD
6511 "Sort and concatenate the agenda items."
6512 (setq list (mapcar 'org-agenda-highlight-todo list))
6513 (if nosort
6514 list
afe98dfa
CD
6515 (when org-agenda-before-sorting-filter-function
6516 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
20908596
CD
6517 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
6518
6519(defun org-agenda-highlight-todo (x)
621f83e4 6520 (let ((org-done-keywords org-done-keywords-for-agenda)
ed21c5c8 6521 (case-fold-search nil)
e66ba1df 6522 re)
20908596
CD
6523 (if (eq x 'line)
6524 (save-excursion
6525 (beginning-of-line 1)
8d642074 6526 (setq re (org-get-at-bol 'org-todo-regexp))
3ab2c837 6527 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
621f83e4 6528 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
c8d0cf5c 6529 (add-text-properties (match-beginning 0) (match-end 1)
621f83e4 6530 (list 'face (org-get-todo-face 1)))
20908596
CD
6531 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6532 (delete-region (match-beginning 1) (1- (match-end 0)))
6533 (goto-char (match-beginning 1))
6534 (insert (format org-agenda-todo-keyword-format s)))))
3ab2c837
BG
6535 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6536 (setq re (get-text-property 0 'org-todo-regexp x))
6537 (when (and re
153ae947
BG
6538 ;; Test `pl' because if there's no heading content,
6539 ;; there's no point matching to highlight. Note
6540 ;; that if we didn't test `pl' first, and there
6541 ;; happened to be no keyword from `org-todo-regexp'
6542 ;; on this heading line, then the `equal' comparison
6543 ;; afterwards would spuriously succeed in the case
6544 ;; where `pl' is nil -- causing an args-out-of-range
6545 ;; error when we try to add text properties to text
6546 ;; that isn't there.
6547 pl
3ab2c837 6548 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
153ae947 6549 x pl) pl))
3ab2c837
BG
6550 (add-text-properties
6551 (or (match-end 1) (match-end 0)) (match-end 0)
6552 (list 'face (org-get-todo-face (match-string 2 x)))
e66ba1df 6553 x)
3ab2c837
BG
6554 (when (match-end 1)
6555 (setq x (concat (substring x 0 (match-end 1))
6556 (format org-agenda-todo-keyword-format
6557 (match-string 2 x))
e66ba1df
BG
6558 (org-add-props " " (text-properties-at 0 x))
6559 (substring x (match-end 3)))))))
20908596
CD
6560 x)))
6561
6562(defsubst org-cmp-priority (a b)
6563 "Compare the priorities of string A and B."
6564 (let ((pa (or (get-text-property 1 'priority a) 0))
6565 (pb (or (get-text-property 1 'priority b) 0)))
6566 (cond ((> pa pb) +1)
8223b1d2 6567 ((< pa pb) -1))))
20908596
CD
6568
6569(defsubst org-cmp-effort (a b)
e66ba1df 6570 "Compare the effort values of string A and B."
20908596
CD
6571 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6572 (ea (or (get-text-property 1 'effort-minutes a) def))
6573 (eb (or (get-text-property 1 'effort-minutes b) def)))
6574 (cond ((> ea eb) +1)
8223b1d2 6575 ((< ea eb) -1))))
20908596
CD
6576
6577(defsubst org-cmp-category (a b)
6578 "Compare the string values of categories of strings A and B."
6579 (let ((ca (or (get-text-property 1 'org-category a) ""))
6580 (cb (or (get-text-property 1 'org-category b) "")))
6581 (cond ((string-lessp ca cb) -1)
8223b1d2 6582 ((string-lessp cb ca) +1))))
20908596 6583
621f83e4
CD
6584(defsubst org-cmp-todo-state (a b)
6585 "Compare the todo states of strings A and B."
c8d0cf5c
CD
6586 (let* ((ma (or (get-text-property 1 'org-marker a)
6587 (get-text-property 1 'org-hd-marker a)))
6588 (mb (or (get-text-property 1 'org-marker b)
6589 (get-text-property 1 'org-hd-marker b)))
6590 (fa (and ma (marker-buffer ma)))
6591 (fb (and mb (marker-buffer mb)))
6592 (todo-kwds
6593 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6594 (and fb (with-current-buffer fb org-todo-keywords-1))))
6595 (ta (or (get-text-property 1 'todo-state a) ""))
621f83e4 6596 (tb (or (get-text-property 1 'todo-state b) ""))
c8d0cf5c
CD
6597 (la (- (length (member ta todo-kwds))))
6598 (lb (- (length (member tb todo-kwds))))
ff4be292 6599 (donepa (member ta org-done-keywords-for-agenda))
621f83e4
CD
6600 (donepb (member tb org-done-keywords-for-agenda)))
6601 (cond ((and donepa (not donepb)) -1)
6602 ((and (not donepa) donepb) +1)
6603 ((< la lb) -1)
8223b1d2 6604 ((< lb la) +1))))
621f83e4 6605
86fbb8ca
CD
6606(defsubst org-cmp-alpha (a b)
6607 "Compare the headlines, alphabetically."
3ab2c837
BG
6608 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6609 (plb (text-property-any 0 (length b) 'org-heading t b))
86fbb8ca
CD
6610 (ta (and pla (substring a pla)))
6611 (tb (and plb (substring b plb))))
6612 (when pla
6613 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6614 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6615 (setq ta (substring ta (match-end 0))))
6616 (setq ta (downcase ta)))
6617 (when plb
6618 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6619 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6620 (setq tb (substring tb (match-end 0))))
6621 (setq tb (downcase tb)))
6622 (cond ((not ta) +1)
6623 ((not tb) -1)
6624 ((string-lessp ta tb) -1)
8223b1d2 6625 ((string-lessp tb ta) +1))))
86fbb8ca 6626
20908596 6627(defsubst org-cmp-tag (a b)
71d35b24 6628 "Compare the string values of the first tags of A and B."
20908596
CD
6629 (let ((ta (car (last (get-text-property 1 'tags a))))
6630 (tb (car (last (get-text-property 1 'tags b)))))
6631 (cond ((not ta) +1)
6632 ((not tb) -1)
6633 ((string-lessp ta tb) -1)
8223b1d2 6634 ((string-lessp tb ta) +1))))
20908596
CD
6635
6636(defsubst org-cmp-time (a b)
6637 "Compare the time-of-day values of strings A and B."
6638 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6639 (ta (or (get-text-property 1 'time-of-day a) def))
6640 (tb (or (get-text-property 1 'time-of-day b) def)))
6641 (cond ((< ta tb) -1)
8223b1d2 6642 ((< tb ta) +1))))
20908596 6643
8bfe682a
CD
6644(defsubst org-cmp-habit-p (a b)
6645 "Compare the todo states of strings A and B."
6646 (let ((ha (get-text-property 1 'org-habit-p a))
6647 (hb (get-text-property 1 'org-habit-p b)))
6648 (cond ((and ha (not hb)) -1)
8223b1d2 6649 ((and (not ha) hb) +1))))
8bfe682a 6650
86fbb8ca
CD
6651(defsubst org-em (x y list) (or (memq x list) (memq y list)))
6652
20908596
CD
6653(defun org-entries-lessp (a b)
6654 "Predicate for sorting agenda entries."
6655 ;; The following variables will be used when the form is evaluated.
6656 ;; So even though the compiler complains, keep them.
86fbb8ca
CD
6657 (let* ((ss org-agenda-sorting-strategy-selected)
6658 (time-up (and (org-em 'time-up 'time-down ss)
6659 (org-cmp-time a b)))
6660 (time-down (if time-up (- time-up) nil))
6661 (priority-up (and (org-em 'priority-up 'priority-down ss)
6662 (org-cmp-priority a b)))
6663 (priority-down (if priority-up (- priority-up) nil))
6664 (effort-up (and (org-em 'effort-up 'effort-down ss)
6665 (org-cmp-effort a b)))
6666 (effort-down (if effort-up (- effort-up) nil))
6667 (category-up (and (or (org-em 'category-up 'category-down ss)
6668 (memq 'category-keep ss))
6669 (org-cmp-category a b)))
6670 (category-down (if category-up (- category-up) nil))
6671 (category-keep (if category-up +1 nil))
6672 (tag-up (and (org-em 'tag-up 'tag-down ss)
6673 (org-cmp-tag a b)))
6674 (tag-down (if tag-up (- tag-up) nil))
6675 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6676 (org-cmp-todo-state a b)))
c8d0cf5c 6677 (todo-state-down (if todo-state-up (- todo-state-up) nil))
86fbb8ca
CD
6678 (habit-up (and (org-em 'habit-up 'habit-down ss)
6679 (org-cmp-habit-p a b)))
6680 (habit-down (if habit-up (- habit-up) nil))
6681 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6682 (org-cmp-alpha a b)))
6683 (alpha-down (if alpha-up (- alpha-up) nil))
afe98dfa 6684 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
c8d0cf5c 6685 user-defined-up user-defined-down)
afe98dfa 6686 (if (and need-user-cmp org-agenda-cmp-user-defined
c8d0cf5c
CD
6687 (functionp org-agenda-cmp-user-defined))
6688 (setq user-defined-up
6689 (funcall org-agenda-cmp-user-defined a b)
6690 user-defined-down (if user-defined-up (- user-defined-up) nil)))
20908596
CD
6691 (cdr (assoc
6692 (eval (cons 'or org-agenda-sorting-strategy-selected))
6693 '((-1 . t) (1 . nil) (nil . nil))))))
6694
6695;;; Agenda restriction lock
6696
86fbb8ca 6697(defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
8bfe682a 6698 "Overlay to mark the headline to which agenda commands are restricted.")
86fbb8ca
CD
6699(overlay-put org-agenda-restriction-lock-overlay
6700 'face 'org-agenda-restriction-lock)
6701(overlay-put org-agenda-restriction-lock-overlay
6702 'help-echo "Agendas are currently limited to this subtree.")
20908596
CD
6703(org-detach-overlay org-agenda-restriction-lock-overlay)
6704
6705(defun org-agenda-set-restriction-lock (&optional type)
6706 "Set restriction lock for agenda, to current subtree or file.
6707Restriction will be the file if TYPE is `file', or if type is the
6708universal prefix '(4), or if the cursor is before the first headline
6709in the file. Otherwise, restriction will be to the current subtree."
6710 (interactive "P")
6711 (and (equal type '(4)) (setq type 'file))
6712 (setq type (cond
6713 (type type)
6714 ((org-at-heading-p) 'subtree)
6715 ((condition-case nil (org-back-to-heading t) (error nil))
6716 'subtree)
6717 (t 'file)))
6718 (if (eq type 'subtree)
6719 (progn
6720 (setq org-agenda-restrict t)
6721 (setq org-agenda-overriding-restriction 'subtree)
6722 (put 'org-agenda-files 'org-restrict
6723 (list (buffer-file-name (buffer-base-buffer))))
6724 (org-back-to-heading t)
86fbb8ca 6725 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
20908596
CD
6726 (move-marker org-agenda-restrict-begin (point))
6727 (move-marker org-agenda-restrict-end
6728 (save-excursion (org-end-of-subtree t)))
6729 (message "Locking agenda restriction to subtree"))
6730 (put 'org-agenda-files 'org-restrict
6731 (list (buffer-file-name (buffer-base-buffer))))
6732 (setq org-agenda-restrict nil)
6733 (setq org-agenda-overriding-restriction 'file)
6734 (move-marker org-agenda-restrict-begin nil)
6735 (move-marker org-agenda-restrict-end nil)
6736 (message "Locking agenda restriction to file"))
6737 (setq current-prefix-arg nil)
6738 (org-agenda-maybe-redo))
6739
6740(defun org-agenda-remove-restriction-lock (&optional noupdate)
6741 "Remove the agenda restriction lock."
6742 (interactive "P")
6743 (org-detach-overlay org-agenda-restriction-lock-overlay)
6744 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6745 (setq org-agenda-overriding-restriction nil)
6746 (setq org-agenda-restrict nil)
6747 (put 'org-agenda-files 'org-restrict nil)
6748 (move-marker org-agenda-restrict-begin nil)
6749 (move-marker org-agenda-restrict-end nil)
6750 (setq current-prefix-arg nil)
6751 (message "Agenda restriction lock removed")
6752 (or noupdate (org-agenda-maybe-redo)))
6753
6754(defun org-agenda-maybe-redo ()
6755 "If there is any window showing the agenda view, update it."
6756 (let ((w (get-buffer-window org-agenda-buffer-name t))
6757 (w0 (selected-window)))
6758 (when w
6759 (select-window w)
6760 (org-agenda-redo)
6761 (select-window w0)
6762 (if org-agenda-overriding-restriction
6763 (message "Agenda view shifted to new %s restriction"
6764 org-agenda-overriding-restriction)
6765 (message "Agenda restriction lock removed")))))
6766
6767;;; Agenda commands
6768
6769(defun org-agenda-check-type (error &rest types)
6770 "Check if agenda buffer is of allowed type.
c7cf0ebc
BG
6771If ERROR is non-nil, throw an error, otherwise just return nil.
6772Allowed types are 'agenda 'timeline 'todo 'tags 'search."
8223b1d2
BG
6773 (if (not org-agenda-type)
6774 (error "No Org agenda currently displayed")
6775 (if (memq org-agenda-type types)
6776 t
6777 (if error
6778 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6779 nil))))
6780
6781(defun org-agenda-Quit (&optional arg)
20908596
CD
6782 "Exit agenda by removing the window or the buffer."
6783 (interactive)
6784 (if org-agenda-columns-active
6785 (org-columns-quit)
6786 (let ((buf (current-buffer)))
8d642074
CD
6787 (if (eq org-agenda-window-setup 'other-frame)
6788 (progn
8d642074 6789 (org-agenda-reset-markers)
8223b1d2 6790 (kill-buffer buf)
8d642074
CD
6791 (org-columns-remove-overlays)
6792 (setq org-agenda-archives-mode nil)
6793 (delete-frame))
6794 (and (not (eq org-agenda-window-setup 'current-window))
6795 (not (one-window-p))
6796 (delete-window))
8d642074 6797 (org-agenda-reset-markers)
8223b1d2 6798 (kill-buffer buf)
8d642074
CD
6799 (org-columns-remove-overlays)
6800 (setq org-agenda-archives-mode nil)))
20908596
CD
6801 ;; Maybe restore the pre-agenda window configuration.
6802 (and org-agenda-restore-windows-after-quit
6803 (not (eq org-agenda-window-setup 'other-frame))
8223b1d2
BG
6804 org-agenda-pre-window-conf
6805 (set-window-configuration org-agenda-pre-window-conf)
6806 (setq org-agenda-pre-window-conf nil))))
6807
6808(defun org-agenda-quit ()
6809 "Exit agenda by killing agenda buffer or burying it when
6810`org-agenda-sticky' is non-NIL"
6811 (interactive)
6812 (if (and (eq org-indirect-buffer-display 'other-window)
6813 org-last-indirect-buffer)
6814 (delete-window (get-buffer-window org-last-indirect-buffer)))
6815 (if org-agenda-columns-active
6816 (org-columns-quit)
6817 (if org-agenda-sticky
6818 (let ((buf (current-buffer)))
6819 (if (eq org-agenda-window-setup 'other-frame)
6820 (progn
6821 (delete-frame))
6822 (and (not (eq org-agenda-window-setup 'current-window))
6823 (not (one-window-p))
6824 (delete-window)))
6825 (with-current-buffer buf
6826 (bury-buffer)
6827 ;; Maybe restore the pre-agenda window configuration.
6828 (and org-agenda-restore-windows-after-quit
6829 (not (eq org-agenda-window-setup 'other-frame))
6830 org-agenda-pre-window-conf
6831 (set-window-configuration org-agenda-pre-window-conf)
6832 (setq org-agenda-pre-window-conf nil))))
6833 (org-agenda-Quit))))
20908596
CD
6834
6835(defun org-agenda-exit ()
6836 "Exit agenda by removing the window or the buffer.
6837Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6838Org-mode buffers visited directly by the user will not be touched."
6839 (interactive)
6840 (org-release-buffers org-agenda-new-buffers)
6841 (setq org-agenda-new-buffers nil)
8223b1d2
BG
6842 (org-agenda-Quit))
6843
6844(defun org-agenda-kill-all-agenda-buffers ()
735135f9 6845 "Kill all buffers in `org-agenda-mode'.
8223b1d2
BG
6846This is used when toggling sticky agendas. You can also explicitly invoke it
6847with `C-c a C-k'."
6848 (interactive)
6849 (let (blist)
6850 (dolist (buf (buffer-list))
6851 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
6852 (push buf blist)))
6853 (mapc 'kill-buffer blist)))
20908596
CD
6854
6855(defun org-agenda-execute (arg)
86fbb8ca
CD
6856 "Execute another agenda command, keeping same window.
6857So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6858in the agenda."
20908596
CD
6859 (interactive "P")
6860 (let ((org-agenda-window-setup 'current-window))
6861 (org-agenda arg)))
6862
8223b1d2
BG
6863(defun org-agenda-redo (&optional all)
6864 "Rebuild possibly ALL agenda view(s) in the current buffer."
6865 (interactive "P")
6866 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
6867 (cpa (unless (eq all t) current-prefix-arg))
6868 (org-agenda-doing-sticky-redo org-agenda-sticky)
6869 (org-agenda-sticky nil)
6870 (org-agenda-buffer-name (or org-agenda-this-buffer-name
6871 org-agenda-buffer-name))
6872 (org-agenda-keep-modes t)
e66ba1df
BG
6873 (tag-filter org-agenda-tag-filter)
6874 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
8223b1d2 6875 (top-cat-filter org-agenda-top-category-filter)
e66ba1df
BG
6876 (cat-filter org-agenda-category-filter)
6877 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6878 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
20908596
CD
6879 (cols org-agenda-columns-active)
6880 (line (org-current-line))
6881 (window-line (- line (org-current-line (window-start))))
8223b1d2
BG
6882 (lprops (get 'org-agenda-redo-command 'org-lprops))
6883 (redo-cmd (get-text-property p 'org-redo-cmd))
6884 (last-args (get-text-property p 'org-last-args))
735135f9 6885 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
8223b1d2
BG
6886 (org-agenda-overriding-cmd-arguments
6887 (unless (eq all t)
6888 (cond ((listp last-args)
6889 (cons (or cpa (car last-args)) (cdr last-args)))
6890 ((stringp last-args)
6891 last-args))))
735135f9 6892 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
e66ba1df
BG
6893 (put 'org-agenda-tag-filter :preset-filter nil)
6894 (put 'org-agenda-category-filter :preset-filter nil)
20908596
CD
6895 (and cols (org-columns-quit))
6896 (message "Rebuilding agenda buffer...")
735135f9
PE
6897 (if series-redo-cmd
6898 (eval series-redo-cmd)
8223b1d2 6899 (org-let lprops '(eval redo-cmd)))
20908596
CD
6900 (setq org-agenda-undo-list nil
6901 org-agenda-pending-undo-list nil)
6902 (message "Rebuilding agenda buffer...done")
e66ba1df
BG
6903 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6904 (put 'org-agenda-category-filter :preset-filter cat-preset)
6905 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6906 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
8223b1d2 6907 (and top-cat-filter (org-agenda-filter-top-category-apply top-cat-filter))
3ab2c837 6908 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
54a0dee5 6909 (org-goto-line line)
20908596
CD
6910 (recenter window-line)))
6911
621f83e4 6912(defvar org-global-tags-completion-table nil)
71d35b24 6913(defvar org-agenda-filter-form nil)
153ae947 6914(defvar org-agenda-filtered-by-category nil)
e66ba1df
BG
6915
6916(defun org-agenda-filter-by-category (strip)
6917 "Keep only those lines in the agenda buffer that have a specific category.
6918The category is that of the current line."
6919 (interactive "P")
8223b1d2
BG
6920 (if (and org-agenda-filtered-by-category
6921 org-agenda-category-filter)
e66ba1df
BG
6922 (org-agenda-filter-show-all-cat)
6923 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6924 (if cat (org-agenda-filter-apply
6925 (list (concat (if strip "-" "+") cat)) 'category)
6926 (error "No category at point")))))
6927
8223b1d2
BG
6928(defun org-find-top-category (&optional pos)
6929 (save-excursion
6930 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
6931 (if pos (goto-char pos))
6932 ;; Skip up to the topmost parent
6933 (while (ignore-errors (outline-up-heading 1) t))
6934 (ignore-errors
6935 (nth 4 (org-heading-components))))))
6936
6937(defvar org-agenda-filtered-by-top-category nil)
6938
6939(defun org-agenda-filter-by-top-category (strip)
6940 "Keep only those lines in the agenda buffer that have a specific category.
6941The category is that of the current line."
6942 (interactive "P")
6943 (if org-agenda-filtered-by-top-category
6944 (progn
6945 (setq org-agenda-filtered-by-top-category nil
6946 org-agenda-top-category-filter nil)
6947 (org-agenda-filter-show-all-cat))
6948 (let ((cat (org-find-top-category (org-get-at-bol 'org-hd-marker))))
6949 (if cat (org-agenda-filter-top-category-apply cat strip)
6950 (error "No top-level category at point")))))
6951
71d35b24 6952(defun org-agenda-filter-by-tag (strip &optional char narrow)
621f83e4
CD
6953 "Keep only those lines in the agenda buffer that have a specific tag.
6954The tag is selected with its fast selection letter, as configured.
71d35b24
CD
6955With prefix argument STRIP, remove all lines that do have the tag.
6956A lisp caller can specify CHAR. NARROW means that the new tag should be
6957used to narrow the search - the interactive user can also press `-' or `+'
6958to switch to narrowing."
621f83e4 6959 (interactive "P")
71d35b24 6960 (let* ((alist org-tag-alist-for-agenda)
8bfe682a
CD
6961 (tag-chars (mapconcat
6962 (lambda (x) (if (and (not (symbolp (car x)))
6963 (cdr x))
6964 (char-to-string (cdr x))
6965 ""))
6966 alist ""))
6967 (efforts (org-split-string
6968 (or (cdr (assoc (concat org-effort-property "_ALL")
6969 org-global-properties))
3ab2c837
BG
6970 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6971 "")))
8bfe682a
CD
6972 (effort-op org-agenda-filter-effort-default-operator)
6973 (effort-prompt "")
6974 (inhibit-read-only t)
e66ba1df 6975 (current org-agenda-tag-filter)
3ab2c837 6976 maybe-refresh a n tag)
71d35b24 6977 (unless char
ff4be292 6978 (message
8bfe682a
CD
6979 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6980 (if narrow "Narrow" "Filter") tag-chars
6981 (if org-agenda-auto-exclude-function "[RET], " ""))
e66ba1df 6982 (setq char (read-char-exclusive)))
71d35b24
CD
6983 (when (member char '(?+ ?-))
6984 ;; Narrowing down
6985 (cond ((equal char ?-) (setq strip t narrow t))
6986 ((equal char ?+) (setq strip nil narrow t)))
ff4be292 6987 (message
71d35b24 6988 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
e66ba1df 6989 (setq char (read-char-exclusive)))
c8d0cf5c 6990 (when (member char '(?< ?> ?= ??))
71d35b24
CD
6991 ;; An effort operator
6992 (setq effort-op (char-to-string char))
71d35b24 6993 (setq alist nil) ; to make sure it will be interpreted as effort.
c8d0cf5c
CD
6994 (unless (equal char ??)
6995 (loop for i from 0 to 9 do
6996 (setq effort-prompt
6997 (concat
6998 effort-prompt " ["
6999 (if (= i 9) "0" (int-to-string (1+ i)))
7000 "]" (nth i efforts))))
7001 (message "Effort%s: %s " effort-op effort-prompt)
e66ba1df 7002 (setq char (read-char-exclusive))
c8d0cf5c 7003 (when (or (< char ?0) (> char ?9))
8223b1d2 7004 (error "Need 1-9,0 to select effort"))))
71d35b24
CD
7005 (when (equal char ?\t)
7006 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7007 (org-set-local 'org-global-tags-completion-table
7008 (org-global-tags-completion-table)))
7009 (let ((completion-ignore-case t))
54a0dee5 7010 (setq tag (org-icompleting-read
71d35b24
CD
7011 "Tag: " org-global-tags-completion-table))))
7012 (cond
8bfe682a 7013 ((equal char ?\r)
e66ba1df 7014 (org-agenda-filter-show-all-tag)
8bfe682a 7015 (when org-agenda-auto-exclude-function
e66ba1df 7016 (setq org-agenda-tag-filter '())
ed21c5c8
CD
7017 (dolist (tag (org-agenda-get-represented-tags))
7018 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
8bfe682a 7019 (if modifier
e66ba1df
BG
7020 (push modifier org-agenda-tag-filter))))
7021 (if (not (null org-agenda-tag-filter))
7022 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
3ab2c837 7023 (setq maybe-refresh t))
c8d0cf5c 7024 ((equal char ?/)
e66ba1df
BG
7025 (org-agenda-filter-show-all-tag)
7026 (when (get 'org-agenda-tag-filter :preset-filter)
7027 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7028 (setq maybe-refresh t))
7029 ((equal char ?. )
7030 (setq org-agenda-tag-filter
7031 (mapcar (lambda(tag) (concat "+" tag))
7032 (org-get-at-bol 'tags)))
7033 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
3ab2c837 7034 (setq maybe-refresh t))
71d35b24
CD
7035 ((or (equal char ?\ )
7036 (setq a (rassoc char alist))
7037 (and (>= char ?0) (<= char ?9)
7038 (setq n (if (= char ?0) 9 (- char ?0 1))
7039 tag (concat effort-op (nth n efforts))
7040 a (cons tag nil)))
c8d0cf5c
CD
7041 (and (= char ??)
7042 (setq tag "?eff")
7043 a (cons tag nil))
71d35b24 7044 (and tag (setq a (cons tag nil))))
e66ba1df 7045 (org-agenda-filter-show-all-tag)
71d35b24 7046 (setq tag (car a))
e66ba1df 7047 (setq org-agenda-tag-filter
71d35b24
CD
7048 (cons (concat (if strip "-" "+") tag)
7049 (if narrow current nil)))
e66ba1df 7050 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
3ab2c837
BG
7051 (setq maybe-refresh t))
7052 (t (error "Invalid tag selection character %c" char)))
58e9b49a
BG
7053 (when (and maybe-refresh
7054 (eq org-agenda-clockreport-mode 'with-filter))
3ab2c837 7055 (org-agenda-redo))))
71d35b24 7056
ed21c5c8
CD
7057(defun org-agenda-get-represented-tags ()
7058 "Get a list of all tags currently represented in the agenda."
7059 (let (p tags)
7060 (save-excursion
7061 (goto-char (point-min))
7062 (while (setq p (next-single-property-change (point) 'tags))
7063 (goto-char p)
7064 (mapc (lambda (x) (add-to-list 'tags x))
7065 (get-text-property (point) 'tags))))
7066 tags))
7067
71d35b24 7068(defun org-agenda-filter-by-tag-refine (strip &optional char)
e66ba1df 7069 "Refine the current filter. See `org-agenda-filter-by-tag'."
71d35b24
CD
7070 (interactive "P")
7071 (org-agenda-filter-by-tag strip char 'refine))
7072
7073(defun org-agenda-filter-make-matcher ()
e66ba1df 7074 "Create the form that tests a line for agenda filter."
71d35b24 7075 (let (f f1)
e66ba1df
BG
7076 ;; first compute the tag-filter matcher
7077 (dolist (x (delete-dups
7078 (append (get 'org-agenda-tag-filter
7079 :preset-filter) org-agenda-tag-filter)))
71d35b24 7080 (if (member x '("-" "+"))
8bfe682a 7081 (setq f1 (if (equal x "-") 'tags '(not tags)))
c8d0cf5c 7082 (if (string-match "[<=>?]" x)
71d35b24
CD
7083 (setq f1 (org-agenda-filter-effort-form x))
7084 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
7085 (if (equal (string-to-char x) ?-)
7086 (setq f1 (list 'not f1))))
7087 (push f1 f))
e66ba1df
BG
7088 ;; then compute the category-filter matcher
7089 (dolist (x (delete-dups
7090 (append (get 'org-agenda-category-filter
7091 :preset-filter) org-agenda-category-filter)))
801a68c8
BG
7092 (if (equal "-" (substring x 0 1))
7093 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7094 (setq f1 (list 'equal (substring x 1) 'cat)))
e66ba1df 7095 (push f1 f))
71d35b24
CD
7096 (cons 'and (nreverse f))))
7097
7098(defun org-agenda-filter-effort-form (e)
7099 "Return the form to compare the effort of the current line with what E says.
86fbb8ca 7100E looks like \"+<2:25\"."
71d35b24
CD
7101 (let (op)
7102 (setq e (substring e 1))
7103 (setq op (string-to-char e) e (substring e 1))
c8d0cf5c
CD
7104 (setq op (cond ((equal op ?<) '<=)
7105 ((equal op ?>) '>=)
7106 ((equal op ??) op)
7107 (t '=)))
71d35b24 7108 (list 'org-agenda-compare-effort (list 'quote op)
3ab2c837 7109 (org-duration-string-to-minutes e))))
71d35b24
CD
7110
7111(defun org-agenda-compare-effort (op value)
7112 "Compare the effort of the current line with VALUE, using OP.
7113If the line does not have an effort defined, return nil."
8d642074 7114 (let ((eff (org-get-at-bol 'effort-minutes)))
c8d0cf5c
CD
7115 (if (equal op ??)
7116 (not eff)
7117 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7118 value))))
71d35b24 7119
e66ba1df 7120(defun org-agenda-filter-apply (filter type)
71d35b24 7121 "Set FILTER as the new agenda filter and apply it."
c74587e6 7122 (let (tags cat)
e66ba1df
BG
7123 (if (eq type 'tag)
7124 (setq org-agenda-tag-filter filter)
801a68c8 7125 (setq org-agenda-category-filter filter))
e66ba1df 7126 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
801a68c8
BG
7127 (if (and (eq type 'category)
7128 (not (equal (substring (car filter) 0 1) "-")))
7129 ;; Only set `org-agenda-filtered-by-category' to t
7130 ;; when a unique category is used as the filter
7131 (setq org-agenda-filtered-by-category t))
71d35b24
CD
7132 (org-agenda-set-mode-name)
7133 (save-excursion
7134 (goto-char (point-min))
7135 (while (not (eobp))
8d642074 7136 (if (org-get-at-bol 'org-marker)
71d35b24 7137 (progn
e66ba1df
BG
7138 (setq tags (org-get-at-bol 'tags) ; used in eval
7139 cat (get-text-property (point) 'org-category))
71d35b24 7140 (if (not (eval org-agenda-filter-form))
e66ba1df 7141 (org-agenda-filter-hide-line type))
71d35b24 7142 (beginning-of-line 2))
afe98dfa
CD
7143 (beginning-of-line 2))))
7144 (if (get-char-property (point) 'invisible)
801a68c8 7145 (ignore-errors (org-agenda-previous-line)))))
621f83e4 7146
8223b1d2
BG
7147(defun org-agenda-filter-top-category-apply (category &optional negative)
7148 "Set FILTER as the new agenda filter and apply it."
7149 (org-agenda-set-mode-name)
7150 (save-excursion
7151 (goto-char (point-min))
7152 (while (not (eobp))
7153 (let* ((pos (org-get-at-bol 'org-hd-marker))
7154 (topcat (and pos (org-find-top-category pos))))
7155 (if (and topcat (funcall (if negative 'identity 'not)
7156 (string= category topcat)))
7157 (org-agenda-filter-hide-line 'category)))
7158 (beginning-of-line 2)))
7159 (if (get-char-property (point) 'invisible)
7160 (org-agenda-previous-line))
7161 (setq org-agenda-top-category-filter category
7162 org-agenda-filtered-by-top-category t))
7163
e66ba1df 7164(defun org-agenda-filter-hide-line (type)
621f83e4 7165 (let (ov)
86fbb8ca 7166 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
8223b1d2 7167 (point-at-eol)))
86fbb8ca 7168 (overlay-put ov 'invisible t)
e66ba1df
BG
7169 (overlay-put ov 'type type)
7170 (if (eq type 'tag)
7171 (push ov org-agenda-tag-filter-overlays)
7172 (push ov org-agenda-cat-filter-overlays))))
621f83e4 7173
71d35b24
CD
7174(defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7175 (setq pos (or pos (point)))
7176 (save-excursion
86fbb8ca
CD
7177 (dolist (ov (overlays-at pos))
7178 (when (and (overlay-get ov 'invisible)
e66ba1df 7179 (eq (overlay-get ov 'type) 'tag))
71d35b24 7180 (goto-char pos)
86fbb8ca
CD
7181 (if (< (overlay-start ov) (point-at-eol))
7182 (move-overlay ov (point-at-eol)
8223b1d2 7183 (overlay-end ov)))))))
71d35b24 7184
e66ba1df
BG
7185(defun org-agenda-filter-show-all-tag nil
7186 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7187 (setq org-agenda-tag-filter-overlays nil
7188 org-agenda-tag-filter nil
7189 org-agenda-filter-form nil)
7190 (org-agenda-set-mode-name))
7191
7192(defun org-agenda-filter-show-all-cat nil
7193 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7194 (setq org-agenda-cat-filter-overlays nil
7195 org-agenda-filtered-by-category nil
7196 org-agenda-category-filter nil
7197 org-agenda-filter-form nil)
71d35b24 7198 (org-agenda-set-mode-name))
621f83e4 7199
20908596
CD
7200(defun org-agenda-manipulate-query-add ()
7201 "Manipulate the query by adding a search term with positive selection.
ed21c5c8 7202Positive selection means the term must be matched for selection of an entry."
20908596
CD
7203 (interactive)
7204 (org-agenda-manipulate-query ?\[))
7205(defun org-agenda-manipulate-query-subtract ()
7206 "Manipulate the query by adding a search term with negative selection.
ed21c5c8 7207Negative selection means term must not be matched for selection of an entry."
20908596
CD
7208 (interactive)
7209 (org-agenda-manipulate-query ?\]))
7210(defun org-agenda-manipulate-query-add-re ()
7211 "Manipulate the query by adding a search regexp with positive selection.
ed21c5c8 7212Positive selection means the regexp must match for selection of an entry."
20908596
CD
7213 (interactive)
7214 (org-agenda-manipulate-query ?\{))
7215(defun org-agenda-manipulate-query-subtract-re ()
7216 "Manipulate the query by adding a search regexp with negative selection.
ed21c5c8 7217Negative selection means regexp must not match for selection of an entry."
20908596
CD
7218 (interactive)
7219 (org-agenda-manipulate-query ?\}))
7220(defun org-agenda-manipulate-query (char)
7221 (cond
7222 ((memq org-agenda-type '(timeline agenda))
54a0dee5
CD
7223 (let ((org-agenda-include-inactive-timestamps t))
7224 (org-agenda-redo))
7225 (message "Display now includes inactive timestamps as well"))
20908596
CD
7226 ((eq org-agenda-type 'search)
7227 (org-add-to-string
7228 'org-agenda-query-string
ed21c5c8
CD
7229 (if org-agenda-last-search-view-search-was-boolean
7230 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7231 (?\{ . " +{}") (?\} . " -{}"))))
7232 " "))
20908596
CD
7233 (setq org-agenda-redo-command
7234 (list 'org-search-view
8223b1d2
BG
7235 (car (get-text-property (min (1- (point-max)) (point))
7236 'org-last-args))
20908596
CD
7237 org-agenda-query-string
7238 (+ (length org-agenda-query-string)
7239 (if (member char '(?\{ ?\})) 0 1))))
7240 (set-register org-agenda-query-register org-agenda-query-string)
8223b1d2
BG
7241 (let ((org-agenda-overriding-arguments
7242 (cdr org-agenda-redo-command)))
7243 (org-agenda-redo)))
20908596
CD
7244 (t (error "Cannot manipulate query for %s-type agenda buffers"
7245 org-agenda-type))))
7246
7247(defun org-add-to-string (var string)
7248 (set var (concat (symbol-value var) string)))
7249
8223b1d2 7250(defun org-agenda-goto-date (span)
20908596 7251 "Jump to DATE in agenda."
8223b1d2
BG
7252 (interactive "P")
7253 (let* ((org-read-date-prefer-future
7254 (eval org-agenda-jump-prefer-future))
7255 (date (org-read-date))
7256 (org-agenda-sticky-orig org-agenda-sticky)
7257 (org-agenda-buffer-tmp-name (buffer-name))
7258 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7259 (0-arg (or current-prefix-arg (car args)))
7260 (2-arg (nth 2 args))
7261 (newcmd (list 'org-agenda-list 0-arg date
7262 (org-agenda-span-to-ndays 2-arg)))
7263 (newargs (cdr newcmd))
7264 (inhibit-read-only t)
7265 org-agenda-sticky)
7266 (if (not (org-agenda-check-type t 'agenda))
7267 (error "Not available in non-agenda blocks")
7268 (add-text-properties (point-min) (point-max)
7269 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7270 (org-agenda-redo)
7271 (setq org-agenda-sticky org-agenda-sticky-orig
7272 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
20908596
CD
7273
7274(defun org-agenda-goto-today ()
7275 "Go to today."
7276 (interactive)
7277 (org-agenda-check-type t 'timeline 'agenda)
8223b1d2
BG
7278 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7279 (curspan (nth 2 args))
7280 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
20908596
CD
7281 (cond
7282 (tdpos (goto-char tdpos))
7283 ((eq org-agenda-type 'agenda)
acedf35c 7284 (let* ((sd (org-agenda-compute-starting-span
8223b1d2
BG
7285 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7286 (org-agenda-overriding-arguments args))
acedf35c 7287 (setf (nth 1 org-agenda-overriding-arguments) sd)
20908596
CD
7288 (org-agenda-redo)
7289 (org-agenda-find-same-or-today-or-agenda)))
7290 (t (error "Cannot find today")))))
7291
7292(defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7293 (goto-char
7294 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7295 (text-property-any (point-min) (point-max) 'org-today t)
7296 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
735135f9 7297 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
8223b1d2 7298 (org-agenda-goto-block-beginning))
20908596
CD
7299 (point-min))))
7300
8223b1d2
BG
7301(defun org-agenda-goto-block-beginning ()
7302 "Go the agenda block beginning."
7303 (interactive)
7304 (if (not (derived-mode-p 'org-agenda-mode))
7305 (error "Cannot execute this command outside of org-agenda-mode buffers")
7306 (let (dest)
7307 (save-excursion
7308 (unless (looking-at "\\'")
7309 (forward-char))
7310 (let* ((prop 'org-agenda-structural-header)
7311 (p (previous-single-property-change (point) prop))
7312 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7313 (1- (point))) prop)))
7314 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7315 (if (not dest)
7316 (error "Cannot find the beginning of the blog")
7317 (goto-char dest)
7318 (move-beginning-of-line 1)))))
7319
20908596 7320(defun org-agenda-later (arg)
bdebdb64 7321 "Go forward in time by the current span.
20908596
CD
7322With prefix ARG, go forward that many times the current span."
7323 (interactive "p")
7324 (org-agenda-check-type t 'agenda)
8223b1d2
BG
7325 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7326 (span (or (nth 2 args) org-agenda-current-span))
7327 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
20908596 7328 (greg (calendar-gregorian-from-absolute sd))
8d642074 7329 (cnt (org-get-at-bol 'org-day-cnt))
acedf35c 7330 greg2)
20908596 7331 (cond
8223b1d2 7332 ((numberp span)
bdebdb64 7333 (setq sd (+ (* span arg) sd)))
20908596 7334 ((eq span 'day)
acedf35c 7335 (setq sd (+ arg sd)))
20908596 7336 ((eq span 'week)
acedf35c 7337 (setq sd (+ (* 7 arg) sd)))
20908596
CD
7338 ((eq span 'month)
7339 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7340 sd (calendar-absolute-from-gregorian greg2))
acedf35c 7341 (setcar greg2 (1+ (car greg2))))
20908596
CD
7342 ((eq span 'year)
7343 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7344 sd (calendar-absolute-from-gregorian greg2))
acedf35c
CD
7345 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7346 (t
7347 (setq sd (+ (* span arg) sd))))
8223b1d2
BG
7348 (let ((org-agenda-overriding-cmd
7349 ;; `cmd' may have been set by `org-agenda-run-series' which
7350 ;; uses `org-agenda-overriding-cmd' to decide whether
7351 ;; overriding is allowed for `cmd'
735135f9 7352 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8223b1d2
BG
7353 (org-agenda-overriding-arguments
7354 (list (car args) sd span)))
20908596
CD
7355 (org-agenda-redo)
7356 (org-agenda-find-same-or-today-or-agenda cnt))))
7357
7358(defun org-agenda-earlier (arg)
7359 "Go backward in time by the current span.
7360With prefix ARG, go backward that many times the current span."
7361 (interactive "p")
7362 (org-agenda-later (- arg)))
7363
c8d0cf5c
CD
7364(defun org-agenda-view-mode-dispatch ()
7365 "Call one of the view mode commands."
7366 (interactive)
8223b1d2
BG
7367 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7368 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7369 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
c8d0cf5c
CD
7370 (let ((a (read-char-exclusive)))
7371 (case a
3ab2c837 7372 (?\ (call-interactively 'org-agenda-reset-view))
c8d0cf5c
CD
7373 (?d (call-interactively 'org-agenda-day-view))
7374 (?w (call-interactively 'org-agenda-week-view))
7375 (?m (call-interactively 'org-agenda-month-view))
7376 (?y (call-interactively 'org-agenda-year-view))
7377 (?l (call-interactively 'org-agenda-log-mode))
ed21c5c8 7378 (?L (org-agenda-log-mode '(4)))
3ab2c837 7379 (?c (org-agenda-log-mode 'clockcheck))
54a0dee5 7380 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
c8d0cf5c
CD
7381 (?a (call-interactively 'org-agenda-archives-mode))
7382 (?A (org-agenda-archives-mode 'files))
54a0dee5
CD
7383 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7384 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
c8d0cf5c
CD
7385 (?G (call-interactively 'org-agenda-toggle-time-grid))
7386 (?D (call-interactively 'org-agenda-toggle-diary))
ed21c5c8 7387 (?\! (call-interactively 'org-agenda-toggle-deadlines))
54a0dee5
CD
7388 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7389 (org-agenda-check-type t 'timeline 'agenda)
7390 (org-agenda-redo))
7391 (message "Display now includes inactive timestamps as well"))
c8d0cf5c
CD
7392 (?q (message "Abort"))
7393 (otherwise (error "Invalid key" )))))
7394
3ab2c837
BG
7395(defun org-agenda-reset-view ()
7396 "Switch to default view for agenda."
7397 (interactive)
7398 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
20908596
CD
7399(defun org-agenda-day-view (&optional day-of-year)
7400 "Switch to daily view for agenda.
7401With argument DAY-OF-YEAR, switch to that day of the year."
7402 (interactive "P")
20908596
CD
7403 (org-agenda-change-time-span 'day day-of-year))
7404(defun org-agenda-week-view (&optional iso-week)
7405 "Switch to daily view for agenda.
7406With argument ISO-WEEK, switch to the corresponding ISO week.
7407If ISO-WEEK has more then 2 digits, only the last two encode the
7408week. Any digits before this encode a year. So 200712 means
7409week 12 of year 2007. Years in the range 1938-2037 can also be
7410written as 2-digit years."
7411 (interactive "P")
20908596
CD
7412 (org-agenda-change-time-span 'week iso-week))
7413(defun org-agenda-month-view (&optional month)
b349f79f 7414 "Switch to monthly view for agenda.
20908596
CD
7415With argument MONTH, switch to that month."
7416 (interactive "P")
7417 (org-agenda-change-time-span 'month month))
7418(defun org-agenda-year-view (&optional year)
b349f79f 7419 "Switch to yearly view for agenda.
20908596
CD
7420With argument YEAR, switch to that year.
7421If MONTH has more then 2 digits, only the last two encode the
7422month. Any digits before this encode a year. So 200712 means
7423December year 2007. Years in the range 1938-2037 can also be
7424written as 2-digit years."
7425 (interactive "P")
7426 (when year
7427 (setq year (org-small-year-to-year year)))
7428 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7429 (org-agenda-change-time-span 'year year)
7430 (error "Abort")))
7431
7432(defun org-agenda-change-time-span (span &optional n)
7433 "Change the agenda view to SPAN.
7434SPAN may be `day', `week', `month', `year'."
7435 (org-agenda-check-type t 'agenda)
8223b1d2
BG
7436 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7437 (curspan (nth 2 args)))
7438 (if (and (not n) (equal curspan span))
7439 (error "Viewing span is already \"%s\"" span))
7440 (let* ((sd (or (org-get-at-bol 'day)
7441 (nth 1 args)
7442 org-starting-day))
7443 (sd (org-agenda-compute-starting-span sd span n))
7444 (org-agenda-overriding-cmd
735135f9 7445 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8223b1d2
BG
7446 (org-agenda-overriding-arguments
7447 (list (car args) sd span)))
7448 (org-agenda-redo)
7449 (org-agenda-find-same-or-today-or-agenda))
7450 (org-agenda-set-mode-name)
7451 (message "Switched to %s view" span)))
20908596 7452
acedf35c
CD
7453(defun org-agenda-compute-starting-span (sd span &optional n)
7454 "Compute starting date for agenda.
20908596
CD
7455SPAN may be `day', `week', `month', `year'. The return value
7456is a cons cell with the starting date and the number of days,
7457so that the date SD will be in that range."
7458 (let* ((greg (calendar-gregorian-from-absolute sd))
7459 (dg (nth 1 greg))
7460 (mg (car greg))
acedf35c 7461 (yg (nth 2 greg)))
20908596
CD
7462 (cond
7463 ((eq span 'day)
7464 (when n
7465 (setq sd (+ (calendar-absolute-from-gregorian
7466 (list mg 1 yg))
acedf35c 7467 n -1))))
20908596
CD
7468 ((eq span 'week)
7469 (let* ((nt (calendar-day-of-week
7470 (calendar-gregorian-from-absolute sd)))
7471 (d (if org-agenda-start-on-weekday
7472 (- nt org-agenda-start-on-weekday)
acedf35c
CD
7473 0))
7474 y1)
20908596
CD
7475 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7476 (when n
7477 (require 'cal-iso)
20908596
CD
7478 (when (> n 99)
7479 (setq y1 (org-small-year-to-year (/ n 100))
7480 n (mod n 100)))
7481 (setq sd
7482 (calendar-absolute-from-iso
7483 (list n 1
acedf35c 7484 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
20908596 7485 ((eq span 'month)
acedf35c
CD
7486 (let (y1)
7487 (when (and n (> n 99))
7488 (setq y1 (org-small-year-to-year (/ n 100))
7489 n (mod n 100)))
7490 (setq sd (calendar-absolute-from-gregorian
7491 (list (or n mg) 1 (or y1 yg))))))
20908596
CD
7492 ((eq span 'year)
7493 (setq sd (calendar-absolute-from-gregorian
acedf35c
CD
7494 (list 1 1 (or n yg))))))
7495 sd))
20908596
CD
7496
7497(defun org-agenda-next-date-line (&optional arg)
7498 "Jump to the next line indicating a date in agenda buffer."
7499 (interactive "p")
7500 (org-agenda-check-type t 'agenda 'timeline)
7501 (beginning-of-line 1)
7502 ;; This does not work if user makes date format that starts with a blank
7503 (if (looking-at "^\\S-") (forward-char 1))
7504 (if (not (re-search-forward "^\\S-" nil t arg))
7505 (progn
7506 (backward-char 1)
7507 (error "No next date after this line in this buffer")))
7508 (goto-char (match-beginning 0)))
7509
7510(defun org-agenda-previous-date-line (&optional arg)
7511 "Jump to the previous line indicating a date in agenda buffer."
7512 (interactive "p")
7513 (org-agenda-check-type t 'agenda 'timeline)
7514 (beginning-of-line 1)
7515 (if (not (re-search-backward "^\\S-" nil t arg))
7516 (error "No previous date before this line in this buffer")))
7517
7518;; Initialize the highlight
86fbb8ca
CD
7519(defvar org-hl (make-overlay 1 1))
7520(overlay-put org-hl 'face 'highlight)
20908596
CD
7521
7522(defun org-highlight (begin end &optional buffer)
7523 "Highlight a region with overlay."
86fbb8ca 7524 (move-overlay org-hl begin end (or buffer (current-buffer))))
20908596
CD
7525
7526(defun org-unhighlight ()
7527 "Detach overlay INDEX."
86fbb8ca 7528 (org-detach-overlay org-hl))
20908596 7529
20908596
CD
7530(defun org-unhighlight-once ()
7531 "Remove the highlight from its position, and this function from the hook."
7532 (remove-hook 'pre-command-hook 'org-unhighlight-once)
7533 (org-unhighlight))
7534
8223b1d2 7535(defvar org-agenda-pre-follow-window-conf nil)
20908596
CD
7536(defun org-agenda-follow-mode ()
7537 "Toggle follow mode in an agenda buffer."
7538 (interactive)
8223b1d2
BG
7539 (unless org-agenda-follow-mode
7540 (setq org-agenda-pre-follow-window-conf
7541 (current-window-configuration)))
20908596 7542 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8223b1d2
BG
7543 (unless org-agenda-follow-mode
7544 (set-window-configuration org-agenda-pre-follow-window-conf))
20908596 7545 (org-agenda-set-mode-name)
e66ba1df 7546 (org-agenda-do-context-action)
20908596
CD
7547 (message "Follow mode is %s"
7548 (if org-agenda-follow-mode "on" "off")))
7549
54a0dee5
CD
7550(defun org-agenda-entry-text-mode (&optional arg)
7551 "Toggle entry text mode in an agenda buffer."
7552 (interactive "P")
365f8d85
SM
7553 (setq org-agenda-entry-text-mode (or (integerp arg)
7554 (not org-agenda-entry-text-mode)))
54a0dee5
CD
7555 (org-agenda-entry-text-hide)
7556 (and org-agenda-entry-text-mode
7557 (let ((org-agenda-entry-text-maxlines
7558 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7559 (org-agenda-entry-text-show)))
7560 (org-agenda-set-mode-name)
7561 (message "Entry text mode is %s. Maximum number of lines is %d"
7562 (if org-agenda-entry-text-mode "on" "off")
7563 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7564
acedf35c
CD
7565(defun org-agenda-clockreport-mode (&optional with-filter)
7566 "Toggle clocktable mode in an agenda buffer.
7567With prefix arg WITH-FILTER, make the clocktable respect the current
7568agenda filter."
7569 (interactive "P")
20908596 7570 (org-agenda-check-type t 'agenda)
acedf35c
CD
7571 (if with-filter
7572 (setq org-agenda-clockreport-mode 'with-filter)
7573 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
20908596
CD
7574 (org-agenda-set-mode-name)
7575 (org-agenda-redo)
7576 (message "Clocktable mode is %s"
7577 (if org-agenda-clockreport-mode "on" "off")))
7578
93b62de8
CD
7579(defun org-agenda-log-mode (&optional special)
7580 "Toggle log mode in an agenda buffer.
7581With argument SPECIAL, show all possible log items, not only the ones
7582configured in `org-agenda-log-mode-items'.
7583With a double `C-u' prefix arg, show *only* log items, nothing else."
7584 (interactive "P")
20908596 7585 (org-agenda-check-type t 'agenda 'timeline)
93b62de8 7586 (setq org-agenda-show-log
3ab2c837
BG
7587 (cond
7588 ((equal special '(16)) 'only)
7589 ((eq special 'clockcheck)
7590 (if (eq org-agenda-show-log 'clockcheck)
7591 nil 'clockcheck))
7592 (special '(closed clock state))
7593 (t (not org-agenda-show-log))))
20908596
CD
7594 (org-agenda-set-mode-name)
7595 (org-agenda-redo)
7596 (message "Log mode is %s"
7597 (if org-agenda-show-log "on" "off")))
7598
2c3ad40d 7599(defun org-agenda-archives-mode (&optional with-files)
c8d0cf5c
CD
7600 "Toggle inclusion of items in trees marked with :ARCHIVE:.
7601When called with a prefix argument, include all archive files as well."
2c3ad40d
CD
7602 (interactive "P")
7603 (setq org-agenda-archives-mode
7604 (if with-files t (if org-agenda-archives-mode nil 'trees)))
7605 (org-agenda-set-mode-name)
7606 (org-agenda-redo)
7607 (message
7608 "%s"
7609 (cond
7610 ((eq org-agenda-archives-mode nil)
7611 "No archives are included")
7612 ((eq org-agenda-archives-mode 'trees)
7613 (format "Trees with :%s: tag are included" org-archive-tag))
7614 ((eq org-agenda-archives-mode t)
7615 (format "Trees with :%s: tag and all active archive files are included"
7616 org-archive-tag)))))
7617
20908596
CD
7618(defun org-agenda-toggle-diary ()
7619 "Toggle diary inclusion in an agenda buffer."
7620 (interactive)
7621 (org-agenda-check-type t 'agenda)
7622 (setq org-agenda-include-diary (not org-agenda-include-diary))
7623 (org-agenda-redo)
7624 (org-agenda-set-mode-name)
7625 (message "Diary inclusion turned %s"
7626 (if org-agenda-include-diary "on" "off")))
7627
ed21c5c8 7628(defun org-agenda-toggle-deadlines ()
acedf35c 7629 "Toggle inclusion of entries with a deadline in an agenda buffer."
ed21c5c8
CD
7630 (interactive)
7631 (org-agenda-check-type t 'agenda)
7632 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
7633 (org-agenda-redo)
7634 (org-agenda-set-mode-name)
7635 (message "Deadlines inclusion turned %s"
7636 (if org-agenda-include-deadlines "on" "off")))
7637
20908596
CD
7638(defun org-agenda-toggle-time-grid ()
7639 "Toggle time grid in an agenda buffer."
7640 (interactive)
7641 (org-agenda-check-type t 'agenda)
7642 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
7643 (org-agenda-redo)
7644 (org-agenda-set-mode-name)
7645 (message "Time-grid turned %s"
7646 (if org-agenda-use-time-grid "on" "off")))
7647
7648(defun org-agenda-set-mode-name ()
7649 "Set the mode name to indicate all the small mode settings."
7650 (setq mode-name
acedf35c
CD
7651 (list "Org-Agenda"
7652 (if (get 'org-agenda-files 'org-restrict) " []" "")
7653 " "
7654 '(:eval (org-agenda-span-name org-agenda-current-span))
7655 (if org-agenda-follow-mode " Follow" "")
7656 (if org-agenda-entry-text-mode " ETxt" "")
7657 (if org-agenda-include-diary " Diary" "")
7658 (if org-agenda-include-deadlines " Ddl" "")
7659 (if org-agenda-use-time-grid " Grid" "")
7660 (if (and (boundp 'org-habit-show-habits)
7661 org-habit-show-habits) " Habit" "")
3ab2c837
BG
7662 (cond
7663 ((consp org-agenda-show-log) " LogAll")
7664 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
7665 (org-agenda-show-log " Log")
7666 (t ""))
e66ba1df
BG
7667 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
7668 :preset-filter))
7669 '(:eval (org-propertize
7670 (concat " <"
7671 (mapconcat
7672 'identity
7673 (append
7674 (get 'org-agenda-category-filter :preset-filter)
7675 org-agenda-category-filter)
7676 "")
7677 ">")
7678 'face 'org-agenda-filter-category
7679 'help-echo "Category used in filtering"))
7680 "")
7681 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
8223b1d2 7682 :preset-filter))
e66ba1df
BG
7683 '(:eval (org-propertize
7684 (concat " {"
7685 (mapconcat
7686 'identity
7687 (append
7688 (get 'org-agenda-tag-filter :preset-filter)
7689 org-agenda-tag-filter)
7690 "")
7691 "}")
7692 'face 'org-agenda-filter-tags
7693 'help-echo "Tags used in filtering"))
acedf35c
CD
7694 "")
7695 (if org-agenda-archives-mode
7696 (if (eq org-agenda-archives-mode t)
7697 " Archives"
7698 (format " :%s:" org-archive-tag))
7699 "")
7700 (if org-agenda-clockreport-mode
7701 (if (eq org-agenda-clockreport-mode 'with-filter)
7702 " Clock{}" " Clock")
7703 "")))
20908596
CD
7704 (force-mode-line-update))
7705
c7cf0ebc 7706(org-define-obsolete-function-alias
8223b1d2
BG
7707 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
7708
7709(defun org-agenda-update-agenda-type ()
7710 "Update the agenda type after each command."
b349f79f
CD
7711 (setq org-agenda-type
7712 (or (get-text-property (point) 'org-agenda-type)
8223b1d2 7713 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8bfe682a
CD
7714
7715(defun org-agenda-next-line ()
86fbb8ca 7716 "Move cursor to the next line, and show if follow mode is active."
8bfe682a
CD
7717 (interactive)
7718 (call-interactively 'next-line)
1bcdebed
CD
7719 (org-agenda-do-context-action))
7720
8bfe682a
CD
7721(defun org-agenda-previous-line ()
7722 "Move cursor to the previous line, and show if follow-mode is active."
8bfe682a
CD
7723 (interactive)
7724 (call-interactively 'previous-line)
1bcdebed
CD
7725 (org-agenda-do-context-action))
7726
8223b1d2
BG
7727(defun org-agenda-next-item (n)
7728 "Move cursor to next agenda item."
7729 (interactive "p")
7730 (let ((col (current-column)))
7731 (dotimes (c n)
7732 (when (next-single-property-change (point-at-eol) 'org-marker)
7733 (move-end-of-line 1)
7734 (goto-char (next-single-property-change (point) 'org-marker))))
7735 (org-move-to-column col))
7736 (org-agenda-do-context-action))
7737
7738(defun org-agenda-previous-item (n)
7739 "Move cursor to next agenda item."
7740 (interactive "p")
7741 (dotimes (c n)
7742 (let ((col (current-column))
7743 (goto (save-excursion
7744 (move-end-of-line 0)
7745 (previous-single-property-change (point) 'org-marker))))
7746 (if goto (goto-char goto))
7747 (org-move-to-column col)))
7748 (org-agenda-do-context-action))
7749
1bcdebed 7750(defun org-agenda-do-context-action ()
86fbb8ca 7751 "Show outline path and, maybe, follow mode window."
1bcdebed 7752 (let ((m (org-get-at-bol 'org-marker)))
e66ba1df
BG
7753 (when (and (markerp m) (marker-buffer m))
7754 (and org-agenda-follow-mode
7755 (if org-agenda-follow-indirect
8223b1d2 7756 (org-agenda-tree-to-indirect-buffer nil)
e66ba1df
BG
7757 (org-agenda-show)))
7758 (and org-agenda-show-outline-path
7759 (org-with-point-at m (org-display-outline-path t))))))
20908596 7760
20908596
CD
7761(defun org-agenda-show-tags ()
7762 "Show the tags applicable to the current item."
7763 (interactive)
8d642074 7764 (let* ((tags (org-get-at-bol 'tags)))
20908596
CD
7765 (if tags
7766 (message "Tags are :%s:"
7767 (org-no-properties (mapconcat 'identity tags ":")))
7768 (message "No tags associated with this line"))))
7769
7770(defun org-agenda-goto (&optional highlight)
7771 "Go to the Org-mode file which contains the item at point."
7772 (interactive)
8d642074 7773 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7774 (org-agenda-error)))
7775 (buffer (marker-buffer marker))
7776 (pos (marker-position marker)))
7777 (switch-to-buffer-other-window buffer)
7778 (widen)
86fbb8ca 7779 (push-mark)
20908596 7780 (goto-char pos)
8223b1d2 7781 (when (derived-mode-p 'org-mode)
20908596
CD
7782 (org-show-context 'agenda)
7783 (save-excursion
7784 (and (outline-next-heading)
3ab2c837
BG
7785 (org-flag-heading nil)))) ; show the next heading
7786 (when (outline-invisible-p)
7787 (show-entry)) ; display invisible text
20908596
CD
7788 (recenter (/ (window-height) 2))
7789 (run-hooks 'org-agenda-after-show-hook)
7790 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
7791
7792(defvar org-agenda-after-show-hook nil
7793 "Normal hook run after an item has been shown from the agenda.
7794Point is in the buffer where the item originated.")
7795
7796(defun org-agenda-kill ()
7797 "Kill the entry or subtree belonging to the current agenda entry."
7798 (interactive)
7799 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8223b1d2
BG
7800 (let* ((bufname-orig (buffer-name))
7801 (marker (or (org-get-at-bol 'org-marker)
20908596
CD
7802 (org-agenda-error)))
7803 (buffer (marker-buffer marker))
7804 (pos (marker-position marker))
8d642074 7805 (type (org-get-at-bol 'type))
20908596
CD
7806 dbeg dend (n 0) conf)
7807 (org-with-remote-undo buffer
8223b1d2
BG
7808 (with-current-buffer buffer
7809 (save-excursion
7810 (goto-char pos)
7811 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
7812 (setq dbeg (progn (org-back-to-heading t) (point))
7813 dend (org-end-of-subtree t t))
7814 (setq dbeg (point-at-bol)
7815 dend (min (point-max) (1+ (point-at-eol)))))
7816 (goto-char dbeg)
7817 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
7818 (setq conf (or (eq t org-agenda-confirm-kill)
7819 (and (numberp org-agenda-confirm-kill)
7820 (> n org-agenda-confirm-kill))))
7821 (and conf
7822 (not (y-or-n-p
7823 (format "Delete entry with %d lines in buffer \"%s\"? "
7824 n (buffer-name buffer))))
7825 (error "Abort"))
7826 (let ((org-agenda-buffer-name bufname-orig))
7827 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
7828 (with-current-buffer buffer (delete-region dbeg dend))
7829 (message "Agenda item and source killed"))))
7830
7831(defvar org-archive-default-command) ; defined in org-archive.el
8bfe682a
CD
7832(defun org-agenda-archive-default ()
7833 "Archive the entry or subtree belonging to the current agenda entry."
7834 (interactive)
7835 (require 'org-archive)
7836 (org-agenda-archive-with org-archive-default-command))
7837
7838(defun org-agenda-archive-default-with-confirmation ()
7839 "Archive the entry or subtree belonging to the current agenda entry."
7840 (interactive)
7841 (require 'org-archive)
7842 (org-agenda-archive-with org-archive-default-command 'confirm))
7843
20908596
CD
7844(defun org-agenda-archive ()
7845 "Archive the entry or subtree belonging to the current agenda entry."
7846 (interactive)
8bfe682a 7847 (org-agenda-archive-with 'org-archive-subtree))
20908596
CD
7848
7849(defun org-agenda-archive-to-archive-sibling ()
8bfe682a
CD
7850 "Move the entry to the archive sibling."
7851 (interactive)
7852 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7853
7854(defun org-agenda-archive-with (cmd &optional confirm)
20908596
CD
7855 "Move the entry to the archive sibling."
7856 (interactive)
7857 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8223b1d2
BG
7858 (let* ((bufname-orig (buffer-name))
7859 (marker (or (org-get-at-bol 'org-marker)
20908596
CD
7860 (org-agenda-error)))
7861 (buffer (marker-buffer marker))
7862 (pos (marker-position marker)))
7863 (org-with-remote-undo buffer
7864 (with-current-buffer buffer
8223b1d2 7865 (if (derived-mode-p 'org-mode)
8bfe682a
CD
7866 (if (and confirm
7867 (not (y-or-n-p "Archive this subtree or entry? ")))
7868 (error "Abort")
7869 (save-excursion
7870 (goto-char pos)
8223b1d2
BG
7871 (let ((org-agenda-buffer-name bufname-orig))
7872 (org-remove-subtree-entries-from-agenda))
8bfe682a
CD
7873 (org-back-to-heading t)
7874 (funcall cmd)))
20908596
CD
7875 (error "Archiving works only in Org-mode files"))))))
7876
7877(defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7878 "Remove all lines in the agenda that correspond to a given subtree.
7879The subtree is the one in buffer BUF, starting at BEG and ending at END.
7880If this information is not given, the function uses the tree at point."
7881 (let ((buf (or buf (current-buffer))) m p)
7882 (save-excursion
7883 (unless (and beg end)
7884 (org-back-to-heading t)
7885 (setq beg (point))
7886 (org-end-of-subtree t)
7887 (setq end (point)))
7888 (set-buffer (get-buffer org-agenda-buffer-name))
7889 (save-excursion
7890 (goto-char (point-max))
7891 (beginning-of-line 1)
7892 (while (not (bobp))
8d642074 7893 (when (and (setq m (org-get-at-bol 'org-marker))
20908596
CD
7894 (equal buf (marker-buffer m))
7895 (setq p (marker-position m))
7896 (>= p beg)
c8d0cf5c 7897 (< p end))
20908596
CD
7898 (let ((inhibit-read-only t))
7899 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7900 (beginning-of-line 0))))))
7901
86fbb8ca 7902(defun org-agenda-refile (&optional goto rfloc no-update)
c8d0cf5c 7903 "Refile the item at point."
54a0dee5
CD
7904 (interactive "P")
7905 (if (equal goto '(16))
7906 (org-refile-goto-last-stored)
8223b1d2
BG
7907 (let* ((buffer-orig (buffer-name))
7908 (marker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
7909 (org-agenda-error)))
7910 (buffer (marker-buffer marker))
7911 (pos (marker-position marker))
7912 (rfloc (or rfloc
7913 (org-refile-get-location
3ab2c837 7914 (if goto "Goto" "Refile to") buffer
54a0dee5
CD
7915 org-refile-allow-creating-parent-nodes))))
7916 (with-current-buffer buffer
7917 (save-excursion
7918 (save-restriction
7919 (widen)
7920 (goto-char marker)
8223b1d2
BG
7921 (let ((org-agenda-buffer-name buffer-orig))
7922 (org-remove-subtree-entries-from-agenda))
86fbb8ca
CD
7923 (org-refile goto buffer rfloc)))))
7924 (unless no-update (org-agenda-redo))))
54a0dee5
CD
7925
7926(defun org-agenda-open-link (&optional arg)
c7cf0ebc
BG
7927 "Open the link(s) in the current entry, if any.
7928This looks for a link in the displayed line in the agenda.
7929It also looks at the text of the entry itself."
c8d0cf5c 7930 (interactive "P")
8d642074
CD
7931 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7932 (org-get-at-bol 'org-marker)))
7933 (buffer (and marker (marker-buffer marker)))
7934 (prefix (buffer-substring
c7cf0ebc
BG
7935 (point-at-bol) (point-at-eol)))
7936 (lkall (org-offer-links-in-entry buffer marker arg prefix))
7937 (lk (car lkall))
7938 (lkend (cdr lkall))
7939 trg)
8bfe682a 7940 (cond
c7cf0ebc 7941 ((and buffer (stringp lk))
8bfe682a 7942 (with-current-buffer buffer
c7cf0ebc
BG
7943 (setq trg (and (string-match org-bracket-link-regexp lk)
7944 (match-string 1 lk)))
7945 (if (or (not trg) (string-match org-any-link-re trg))
7946 (save-excursion
7947 (save-restriction
7948 (widen)
7949 (goto-char marker)
7950 (when (search-forward lk nil lkend)
7951 (goto-char (match-beginning 0))
7952 (org-open-at-point))))
7953 ;; This is an internal link, widen the buffer
7954 (switch-to-buffer-other-window buffer)
7955 (widen)
7956 (goto-char marker)
7957 (when (search-forward lk nil lkend)
7958 (goto-char (match-beginning 0))
7959 (org-open-at-point)))))
8bfe682a
CD
7960 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7961 (save-excursion
7962 (beginning-of-line 1)
7963 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7964 (org-open-link-from-string (match-string 1)))
c7cf0ebc 7965 (t (message "No link to open here")))))
20908596
CD
7966
7967(defun org-agenda-copy-local-variable (var)
7968 "Get a variable from a referenced buffer and install it here."
8d642074 7969 (let ((m (org-get-at-bol 'org-marker)))
20908596
CD
7970 (when (and m (buffer-live-p (marker-buffer m)))
7971 (org-set-local var (with-current-buffer (marker-buffer m)
7972 (symbol-value var))))))
7973
7974(defun org-agenda-switch-to (&optional delete-other-windows)
7975 "Go to the Org-mode file which contains the item at point."
7976 (interactive)
8bfe682a
CD
7977 (if (and org-return-follows-link
7978 (not (org-get-at-bol 'org-marker))
7979 (org-in-regexp org-bracket-link-regexp))
7980 (org-open-link-from-string (match-string 0))
7981 (let* ((marker (or (org-get-at-bol 'org-marker)
7982 (org-agenda-error)))
7983 (buffer (marker-buffer marker))
7984 (pos (marker-position marker)))
e66ba1df 7985 (org-pop-to-buffer-same-window buffer)
8bfe682a
CD
7986 (and delete-other-windows (delete-other-windows))
7987 (widen)
7988 (goto-char pos)
8223b1d2 7989 (when (derived-mode-p 'org-mode)
8bfe682a
CD
7990 (org-show-context 'agenda)
7991 (save-excursion
7992 (and (outline-next-heading)
3ab2c837
BG
7993 (org-flag-heading nil))) ; show the next heading
7994 (when (outline-invisible-p)
8223b1d2
BG
7995 (show-entry)) ; display invisible text
7996 (run-hooks 'org-agenda-after-show-hook)))))
20908596
CD
7997
7998(defun org-agenda-goto-mouse (ev)
7999 "Go to the Org-mode file which contains the item at the mouse click."
8000 (interactive "e")
8001 (mouse-set-point ev)
8002 (org-agenda-goto))
8003
fdf730ed
CD
8004(defun org-agenda-show (&optional full-entry)
8005 "Display the Org-mode file which contains the item at point.
8006With prefix argument FULL-ENTRY, make the entire entry visible
8007if it was hidden in the outline."
8008 (interactive "P")
20908596 8009 (let ((win (selected-window)))
fdf730ed
CD
8010 (if full-entry
8011 (let ((org-show-entry-below t))
8012 (org-agenda-goto t))
8013 (org-agenda-goto t))
20908596
CD
8014 (select-window win)))
8015
8bfe682a 8016(defvar org-agenda-show-window nil)
8223b1d2 8017(defun org-agenda-show-and-scroll-up (&optional arg)
8bfe682a 8018 "Display the Org-mode file which contains the item at point.
8223b1d2
BG
8019When called repeatedly, scroll the window that is displaying the buffer.
8020With a \\[universal-argument] prefix, use `org-show-entry' instead of
8021`show-subtree' to display the item, so that drawers and logbooks stay
8022folded."
8023 (interactive "P")
8bfe682a
CD
8024 (let ((win (selected-window)))
8025 (if (and (window-live-p org-agenda-show-window)
8026 (eq this-command last-command))
8027 (progn
8028 (select-window org-agenda-show-window)
8029 (ignore-errors (scroll-up)))
8030 (org-agenda-goto t)
8223b1d2 8031 (if arg (org-show-entry) (show-subtree))
8bfe682a
CD
8032 (setq org-agenda-show-window (selected-window)))
8033 (select-window win)))
8034
8035(defun org-agenda-show-scroll-down ()
8036 "Scroll down the window showing the agenda."
8037 (interactive)
8038 (let ((win (selected-window)))
8039 (when (window-live-p org-agenda-show-window)
8040 (select-window org-agenda-show-window)
8041 (ignore-errors (scroll-down))
8042 (select-window win))))
8043
c8d0cf5c
CD
8044(defun org-agenda-show-1 (&optional more)
8045 "Display the Org-mode file which contains the item at point.
8bfe682a 8046The prefix arg selects the amount of information to display:
c8d0cf5c
CD
8047
80480 hide the subtree
80491 just show the entry according to defaults.
54a0dee5
CD
80502 show the children view
80513 show the subtree view
c8d0cf5c
CD
80524 show the entire subtree and any LOGBOOK drawers
80535 show the entire subtree and any drawers
8054With prefix argument FULL-ENTRY, make the entire entry visible
8055if it was hidden in the outline."
8056 (interactive "p")
8057 (let ((win (selected-window)))
8058 (org-agenda-goto t)
8059 (org-recenter-heading 1)
8060 (cond
8061 ((= more 0)
8062 (hide-subtree)
54a0dee5
CD
8063 (save-excursion
8064 (org-back-to-heading)
8065 (run-hook-with-args 'org-cycle-hook 'folded))
8066 (message "Remote: FOLDED"))
3ab2c837 8067 ((and (org-called-interactively-p 'any) (= more 1))
c8d0cf5c
CD
8068 (message "Remote: show with default settings"))
8069 ((= more 2)
8070 (show-entry)
54a0dee5 8071 (show-children)
c8d0cf5c
CD
8072 (save-excursion
8073 (org-back-to-heading)
54a0dee5
CD
8074 (run-hook-with-args 'org-cycle-hook 'children))
8075 (message "Remote: CHILDREN"))
c8d0cf5c
CD
8076 ((= more 3)
8077 (show-subtree)
8078 (save-excursion
8079 (org-back-to-heading)
54a0dee5
CD
8080 (run-hook-with-args 'org-cycle-hook 'subtree))
8081 (message "Remote: SUBTREE"))
c8d0cf5c
CD
8082 ((= more 4)
8083 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8084 (org-drawer-regexp
8085 (concat "^[ \t]*:\\("
8086 (mapconcat 'regexp-quote org-drawers "\\|")
8087 "\\):[ \t]*$")))
8088 (show-subtree)
8089 (save-excursion
8090 (org-back-to-heading)
8091 (org-cycle-hide-drawers 'subtree)))
54a0dee5 8092 (message "Remote: SUBTREE AND LOGBOOK"))
c8d0cf5c
CD
8093 ((> more 4)
8094 (show-subtree)
54a0dee5 8095 (message "Remote: SUBTREE AND ALL DRAWERS")))
c8d0cf5c
CD
8096 (select-window win)))
8097
8098(defun org-recenter-heading (n)
8099 (save-excursion
8100 (org-back-to-heading)
8101 (recenter n)))
8102
8103(defvar org-agenda-cycle-counter nil)
54a0dee5 8104(defun org-agenda-cycle-show (&optional n)
c8d0cf5c
CD
8105 "Show the current entry in another window, with default settings.
8106Default settings are taken from `org-show-hierarchy-above' and siblings.
54a0dee5 8107When use repeatedly in immediate succession, the remote entry will cycle
c8d0cf5c
CD
8108through visibility
8109
54a0dee5
CD
8110children -> subtree -> folded
8111
8112When called with a numeric prefix arg, that arg will be passed through to
8113`org-agenda-show-1'. For the interpretation of that argument, see the
8114docstring of `org-agenda-show-1'."
8115 (interactive "P")
8116 (if (integerp n)
8117 (setq org-agenda-cycle-counter n)
8118 (if (not (eq last-command this-command))
8119 (setq org-agenda-cycle-counter 1)
8120 (if (equal org-agenda-cycle-counter 0)
8121 (setq org-agenda-cycle-counter 2)
8122 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8123 (if (> org-agenda-cycle-counter 3)
8124 (setq org-agenda-cycle-counter 0)))))
c8d0cf5c
CD
8125 (org-agenda-show-1 org-agenda-cycle-counter))
8126
20908596
CD
8127(defun org-agenda-recenter (arg)
8128 "Display the Org-mode file which contains the item at point and recenter."
8129 (interactive "P")
8130 (let ((win (selected-window)))
8131 (org-agenda-goto t)
8132 (recenter arg)
8133 (select-window win)))
8134
8135(defun org-agenda-show-mouse (ev)
8136 "Display the Org-mode file which contains the item at the mouse click."
8137 (interactive "e")
8138 (mouse-set-point ev)
8139 (org-agenda-show))
8140
8141(defun org-agenda-check-no-diary ()
8142 "Check if the entry is a diary link and abort if yes."
8d642074 8143 (if (org-get-at-bol 'org-agenda-diary-link)
20908596
CD
8144 (org-agenda-error)))
8145
8146(defun org-agenda-error ()
8147 (error "Command not allowed in this line"))
8148
8223b1d2 8149(defun org-agenda-tree-to-indirect-buffer (arg)
20908596 8150 "Show the subtree corresponding to the current entry in an indirect buffer.
8223b1d2
BG
8151This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8152
8153With a numerical prefix ARG, go up to this level and then take that tree.
8154With a negative numeric ARG, go up by this number of levels.
86fbb8ca
CD
8155With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8156use the dedicated frame)."
8223b1d2
BG
8157 (interactive "P")
8158 (if current-prefix-arg
8159 (org-agenda-do-tree-to-indirect-buffer arg)
8160 (let ((agenda-buffer (buffer-name))
8161 (agenda-window (selected-window))
153ae947
BG
8162 (indirect-window
8163 (and org-last-indirect-buffer
8164 (get-buffer-window org-last-indirect-buffer))))
8223b1d2
BG
8165 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8166 (unless (or (eq org-indirect-buffer-display 'new-frame)
8167 (eq org-indirect-buffer-display 'dedicated-frame))
8168 (unwind-protect
8169 (unless (and indirect-window (window-live-p indirect-window))
8170 (setq indirect-window (split-window agenda-window)))
8171 (and indirect-window (select-window indirect-window))
8172 (switch-to-buffer org-last-indirect-buffer :norecord)
8173 (fit-window-to-buffer indirect-window)))
8174 (select-window (get-buffer-window agenda-buffer)))))
8175
8176(defun org-agenda-do-tree-to-indirect-buffer (arg)
e66ba1df 8177 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
20908596 8178 (org-agenda-check-no-diary)
8d642074 8179 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
8180 (org-agenda-error)))
8181 (buffer (marker-buffer marker))
8182 (pos (marker-position marker)))
8183 (with-current-buffer buffer
8184 (save-excursion
8185 (goto-char pos)
8223b1d2 8186 (funcall 'org-tree-to-indirect-buffer arg)))))
20908596
CD
8187
8188(defvar org-last-heading-marker (make-marker)
8189 "Marker pointing to the headline that last changed its TODO state
8190by a remote command from the agenda.")
8191
8192(defun org-agenda-todo-nextset ()
8193 "Switch TODO entry to next sequence."
8194 (interactive)
8195 (org-agenda-todo 'nextset))
8196
8197(defun org-agenda-todo-previousset ()
8198 "Switch TODO entry to previous sequence."
8199 (interactive)
8200 (org-agenda-todo 'previousset))
8201
8202(defun org-agenda-todo (&optional arg)
8203 "Cycle TODO state of line at point, also in Org-mode file.
8204This changes the line at point, all other lines in the agenda referring to
8205the same tree node, and the headline of the tree node in the Org-mode file."
8206 (interactive "P")
8207 (org-agenda-check-no-diary)
8208 (let* ((col (current-column))
8d642074 8209 (marker (or (org-get-at-bol 'org-marker)
20908596
CD
8210 (org-agenda-error)))
8211 (buffer (marker-buffer marker))
8212 (pos (marker-position marker))
8d642074 8213 (hdmarker (org-get-at-bol 'org-hd-marker))
acedf35c 8214 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
20908596 8215 (inhibit-read-only t)
93b62de8 8216 org-agenda-headline-snapshot-before-repeat newhead just-one)
20908596
CD
8217 (org-with-remote-undo buffer
8218 (with-current-buffer buffer
8219 (widen)
8220 (goto-char pos)
8221 (org-show-context 'agenda)
8222 (save-excursion
8223 (and (outline-next-heading)
8224 (org-flag-heading nil))) ; show the next heading
a2a2e7fb
CD
8225 (let ((current-prefix-arg arg))
8226 (call-interactively 'org-todo))
20908596
CD
8227 (and (bolp) (forward-char 1))
8228 (setq newhead (org-get-heading))
93b62de8
CD
8229 (when (and (org-bound-and-true-p
8230 org-agenda-headline-snapshot-before-repeat)
8231 (not (equal org-agenda-headline-snapshot-before-repeat
8232 newhead))
8233 todayp)
8234 (setq newhead org-agenda-headline-snapshot-before-repeat
8235 just-one t))
20908596
CD
8236 (save-excursion
8237 (org-back-to-heading)
8238 (move-marker org-last-heading-marker (point))))
8239 (beginning-of-line 1)
8240 (save-excursion
93b62de8 8241 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
20908596
CD
8242 (org-move-to-column col))))
8243
8244(defun org-agenda-add-note (&optional arg)
8245 "Add a time-stamped note to the entry at point."
8246 (interactive "P")
8247 (org-agenda-check-no-diary)
8d642074 8248 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
8249 (org-agenda-error)))
8250 (buffer (marker-buffer marker))
8251 (pos (marker-position marker))
8d642074 8252 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
8253 (inhibit-read-only t))
8254 (with-current-buffer buffer
8255 (widen)
8256 (goto-char pos)
8257 (org-show-context 'agenda)
8258 (save-excursion
8259 (and (outline-next-heading)
8260 (org-flag-heading nil))) ; show the next heading
8261 (org-add-note))))
8262
db55f368 8263(defun org-agenda-change-all-lines (newhead hdmarker
4ed008de 8264 &optional fixface just-this)
20908596
CD
8265 "Change all lines in the agenda buffer which match HDMARKER.
8266The new content of the line will be NEWHEAD (as modified by
e66ba1df 8267`org-agenda-format-item'). HDMARKER is checked with
20908596 8268`equal' against all `org-hd-marker' text properties in the file.
33306645 8269If FIXFACE is non-nil, the face of each item is modified according to
db55f368
CD
8270the new TODO state.
8271If JUST-THIS is non-nil, change just the current line, not all.
33306645 8272If FORCE-TAGS is non nil, the car of it returns the new tags."
20908596 8273 (let* ((inhibit-read-only t)
93b62de8 8274 (line (org-current-line))
8223b1d2 8275 (org-agenda-buffer (current-buffer))
fdf730ed 8276 (thetags (with-current-buffer (marker-buffer hdmarker)
4ed008de
CD
8277 (save-excursion (save-restriction (widen)
8278 (goto-char hdmarker)
fdf730ed 8279 (org-get-tags-at)))))
20908596
CD
8280 props m pl undone-face done-face finish new dotime cat tags)
8281 (save-excursion
8282 (goto-char (point-max))
8283 (beginning-of-line 1)
8284 (while (not finish)
8285 (setq finish (bobp))
8d642074 8286 (when (and (setq m (org-get-at-bol 'org-hd-marker))
93b62de8 8287 (or (not just-this) (= (org-current-line) line))
20908596
CD
8288 (equal m hdmarker))
8289 (setq props (text-properties-at (point))
8d642074
CD
8290 dotime (org-get-at-bol 'dotime)
8291 cat (org-get-at-bol 'org-category)
4ed008de 8292 tags thetags
3ab2c837
BG
8293 new
8294 (let ((org-prefix-format-compiled
8223b1d2
BG
8295 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8296 org-prefix-format-compiled))
8297 (extra (org-get-at-bol 'extra)))
3ab2c837
BG
8298 (with-current-buffer (marker-buffer hdmarker)
8299 (save-excursion
8300 (save-restriction
8301 (widen)
8223b1d2 8302 (org-agenda-format-item extra newhead cat tags dotime)))))
3ab2c837 8303 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8d642074
CD
8304 undone-face (org-get-at-bol 'undone-face)
8305 done-face (org-get-at-bol 'done-face))
3ab2c837 8306 (beginning-of-line 1)
20908596
CD
8307 (cond
8308 ((equal new "")
20908596
CD
8309 (and (looking-at ".*\n?") (replace-match "")))
8310 ((looking-at ".*")
8311 (replace-match new t t)
8312 (beginning-of-line 1)
8313 (add-text-properties (point-at-bol) (point-at-eol) props)
8314 (when fixface
8315 (add-text-properties
8316 (point-at-bol) (point-at-eol)
8317 (list 'face
8318 (if org-last-todo-state-is-todo
8319 undone-face done-face))))
8320 (org-agenda-highlight-todo 'line)
8321 (beginning-of-line 1))
8223b1d2
BG
8322 (t (error "Line update did not work")))
8323 (save-restriction
8324 (narrow-to-region (point-at-bol) (point-at-eol))
8325 (org-agenda-finalize)))
8326 (beginning-of-line 0)))))
20908596
CD
8327
8328(defun org-agenda-align-tags (&optional line)
8329 "Align all tags in agenda items to `org-agenda-tags-column'."
8330 (let ((inhibit-read-only t) l c)
8331 (save-excursion
8332 (goto-char (if line (point-at-bol) (point-min)))
afe98dfa 8333 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
20908596
CD
8334 (if line (point-at-eol) nil) t)
8335 (add-text-properties
8336 (match-beginning 2) (match-end 2)
30ab4580
GM
8337 (list 'face (delq nil (let ((prop (get-text-property
8338 (match-beginning 2) 'face)))
8339 (or (listp prop) (setq prop (list prop)))
8340 (if (memq 'org-tag prop)
8341 prop
8342 (cons 'org-tag prop))))))
20908596
CD
8343 (setq l (- (match-end 2) (match-beginning 2))
8344 c (if (< org-agenda-tags-column 0)
8345 (- (abs org-agenda-tags-column) l)
8346 org-agenda-tags-column))
8347 (delete-region (match-beginning 1) (match-end 1))
8348 (goto-char (match-beginning 1))
8349 (insert (org-add-props
8350 (make-string (max 1 (- c (current-column))) ?\ )
ed21c5c8
CD
8351 (plist-put (copy-sequence (text-properties-at (point)))
8352 'face nil))))
ff4be292
CD
8353 (goto-char (point-min))
8354 (org-font-lock-add-tag-faces (point-max)))))
20908596
CD
8355
8356(defun org-agenda-priority-up ()
8357 "Increase the priority of line at point, also in Org-mode file."
8358 (interactive)
8359 (org-agenda-priority 'up))
8360
8361(defun org-agenda-priority-down ()
8362 "Decrease the priority of line at point, also in Org-mode file."
8363 (interactive)
8364 (org-agenda-priority 'down))
8365
c7cf0ebc 8366(defun org-agenda-priority (&optional force-direction)
20908596
CD
8367 "Set the priority of line at point, also in Org-mode file.
8368This changes the line at point, all other lines in the agenda referring to
c7cf0ebc
BG
8369the same tree node, and the headline of the tree node in the Org-mode file.
8370Called with a universal prefix arg, show the priority instead of setting it."
8223b1d2 8371 (interactive "P")
c7cf0ebc
BG
8372 (if (equal force-direction '(4))
8373 (org-show-priority)
8374 (unless org-enable-priority-commands
8375 (error "Priority commands are disabled"))
8376 (org-agenda-check-no-diary)
8377 (let* ((marker (or (org-get-at-bol 'org-marker)
8378 (org-agenda-error)))
8379 (hdmarker (org-get-at-bol 'org-hd-marker))
8380 (buffer (marker-buffer hdmarker))
8381 (pos (marker-position hdmarker))
8382 (inhibit-read-only t)
8383 newhead)
8384 (org-with-remote-undo buffer
8385 (with-current-buffer buffer
8386 (widen)
8387 (goto-char pos)
8388 (org-show-context 'agenda)
8389 (save-excursion
8390 (and (outline-next-heading)
8391 (org-flag-heading nil))) ; show the next heading
8392 (funcall 'org-priority force-direction)
8393 (end-of-line 1)
8394 (setq newhead (org-get-heading)))
8395 (org-agenda-change-all-lines newhead hdmarker)
8396 (beginning-of-line 1)))))
20908596
CD
8397
8398;; FIXME: should fix the tags property of the agenda line.
c8d0cf5c 8399(defun org-agenda-set-tags (&optional tag onoff)
20908596
CD
8400 "Set tags for the current headline."
8401 (interactive)
8402 (org-agenda-check-no-diary)
3ab2c837 8403 (if (and (org-region-active-p) (org-called-interactively-p 'any))
20908596 8404 (call-interactively 'org-change-tag-in-region)
8d642074 8405 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
8406 (org-agenda-error)))
8407 (buffer (marker-buffer hdmarker))
8408 (pos (marker-position hdmarker))
8409 (inhibit-read-only t)
4ed008de 8410 newhead)
20908596
CD
8411 (org-with-remote-undo buffer
8412 (with-current-buffer buffer
8413 (widen)
8414 (goto-char pos)
8415 (save-excursion
8416 (org-show-context 'agenda))
8417 (save-excursion
8418 (and (outline-next-heading)
8419 (org-flag-heading nil))) ; show the next heading
8420 (goto-char pos)
c8d0cf5c
CD
8421 (if tag
8422 (org-toggle-tag tag onoff)
8423 (call-interactively 'org-set-tags))
20908596
CD
8424 (end-of-line 1)
8425 (setq newhead (org-get-heading)))
4ed008de 8426 (org-agenda-change-all-lines newhead hdmarker)
20908596
CD
8427 (beginning-of-line 1)))))
8428
54a0dee5
CD
8429(defun org-agenda-set-property ()
8430 "Set a property for the current headline."
8431 (interactive)
8432 (org-agenda-check-no-diary)
8d642074 8433 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
8434 (org-agenda-error)))
8435 (buffer (marker-buffer hdmarker))
8436 (pos (marker-position hdmarker))
8437 (inhibit-read-only t)
8438 newhead)
8439 (org-with-remote-undo buffer
8440 (with-current-buffer buffer
8441 (widen)
8442 (goto-char pos)
8443 (save-excursion
8444 (org-show-context 'agenda))
8445 (save-excursion
8446 (and (outline-next-heading)
8447 (org-flag-heading nil))) ; show the next heading
8448 (goto-char pos)
8449 (call-interactively 'org-set-property)))))
8450
8451(defun org-agenda-set-effort ()
8452 "Set the effort property for the current headline."
8453 (interactive)
8454 (org-agenda-check-no-diary)
8d642074 8455 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
8456 (org-agenda-error)))
8457 (buffer (marker-buffer hdmarker))
8458 (pos (marker-position hdmarker))
8459 (inhibit-read-only t)
8460 newhead)
8461 (org-with-remote-undo buffer
8462 (with-current-buffer buffer
8463 (widen)
8464 (goto-char pos)
8465 (save-excursion
8466 (org-show-context 'agenda))
8467 (save-excursion
8468 (and (outline-next-heading)
3ab2c837 8469 (org-flag-heading nil))) ; show the next heading
54a0dee5
CD
8470 (goto-char pos)
8471 (call-interactively 'org-set-effort)
3ab2c837
BG
8472 (end-of-line 1)
8473 (setq newhead (org-get-heading)))
8474 (org-agenda-change-all-lines newhead hdmarker))))
54a0dee5 8475
20908596
CD
8476(defun org-agenda-toggle-archive-tag ()
8477 "Toggle the archive tag for the current entry."
8478 (interactive)
8479 (org-agenda-check-no-diary)
8d642074 8480 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
8481 (org-agenda-error)))
8482 (buffer (marker-buffer hdmarker))
8483 (pos (marker-position hdmarker))
8484 (inhibit-read-only t)
8485 newhead)
8486 (org-with-remote-undo buffer
8487 (with-current-buffer buffer
8488 (widen)
8489 (goto-char pos)
8490 (org-show-context 'agenda)
8491 (save-excursion
8492 (and (outline-next-heading)
8493 (org-flag-heading nil))) ; show the next heading
8494 (call-interactively 'org-toggle-archive-tag)
8495 (end-of-line 1)
8496 (setq newhead (org-get-heading)))
8497 (org-agenda-change-all-lines newhead hdmarker)
8498 (beginning-of-line 1))))
8499
c8d0cf5c
CD
8500(defun org-agenda-do-date-later (arg)
8501 (interactive "P")
8502 (cond
8503 ((or (equal arg '(16))
8504 (memq last-command
8505 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
8506 (setq this-command 'org-agenda-date-later-minutes)
8507 (org-agenda-date-later-minutes 1))
8508 ((or (equal arg '(4))
8509 (memq last-command
8510 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8511 (setq this-command 'org-agenda-date-later-hours)
8512 (org-agenda-date-later-hours 1))
8513 (t
8514 (org-agenda-date-later (prefix-numeric-value arg)))))
8515
8516(defun org-agenda-do-date-earlier (arg)
8517 (interactive "P")
8518 (cond
8519 ((or (equal arg '(16))
8520 (memq last-command
8521 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
8522 (setq this-command 'org-agenda-date-earlier-minutes)
8523 (org-agenda-date-earlier-minutes 1))
8524 ((or (equal arg '(4))
8525 (memq last-command
8526 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8527 (setq this-command 'org-agenda-date-earlier-hours)
8528 (org-agenda-date-earlier-hours 1))
8529 (t
8530 (org-agenda-date-earlier (prefix-numeric-value arg)))))
8531
20908596 8532(defun org-agenda-date-later (arg &optional what)
3ab2c837 8533 "Change the date of this item to ARG day(s) later."
20908596
CD
8534 (interactive "p")
8535 (org-agenda-check-type t 'agenda 'timeline)
8536 (org-agenda-check-no-diary)
8d642074 8537 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
8538 (org-agenda-error)))
8539 (buffer (marker-buffer marker))
e66ba1df
BG
8540 (pos (marker-position marker))
8541 cdate today)
20908596 8542 (org-with-remote-undo buffer
e66ba1df
BG
8543 (with-current-buffer buffer
8544 (widen)
8545 (goto-char pos)
8546 (if (not (org-at-timestamp-p))
8547 (error "Cannot find time stamp"))
8548 (when (and org-agenda-move-date-from-past-immediately-to-today
8549 (equal arg 1)
8550 (or (not what) (eq what 'day))
8551 (not (save-match-data (org-at-date-range-p))))
8552 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
8553 cdate (calendar-absolute-from-gregorian
8554 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
8555 today (org-today))
8556 (if (> today cdate)
8557 ;; immediately shift to today
8558 (setq arg (- today cdate))))
8559 (org-timestamp-change arg (or what 'day))
8560 (when (and (org-at-date-range-p)
8561 (re-search-backward org-tr-regexp-both (point-at-bol)))
8562 (let ((end org-last-changed-timestamp))
8563 (org-timestamp-change arg (or what 'day))
8564 (setq org-last-changed-timestamp
8565 (concat org-last-changed-timestamp "--" end)))))
8566 (org-agenda-show-new-time marker org-last-changed-timestamp))
20908596
CD
8567 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8568
8569(defun org-agenda-date-earlier (arg &optional what)
3ab2c837 8570 "Change the date of this item to ARG day(s) earlier."
20908596
CD
8571 (interactive "p")
8572 (org-agenda-date-later (- arg) what))
8573
c8d0cf5c
CD
8574(defun org-agenda-date-later-minutes (arg)
8575 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8576 (interactive "p")
8577 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8578 (org-agenda-date-later arg 'minute))
8579
8580(defun org-agenda-date-earlier-minutes (arg)
8581 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8582 (interactive "p")
8583 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8584 (org-agenda-date-earlier arg 'minute))
8585
8586(defun org-agenda-date-later-hours (arg)
8587 "Change the time of this item, in hour steps."
8588 (interactive "p")
8589 (org-agenda-date-later arg 'hour))
8590
8591(defun org-agenda-date-earlier-hours (arg)
8592 "Change the time of this item, in hour steps."
8593 (interactive "p")
8594 (org-agenda-date-earlier arg 'hour))
8595
20908596
CD
8596(defun org-agenda-show-new-time (marker stamp &optional prefix)
8597 "Show new date stamp via text properties."
8598 ;; We use text properties to make this undoable
71d35b24
CD
8599 (let ((inhibit-read-only t)
8600 (buffer-invisibility-spec))
20908596
CD
8601 (setq stamp (concat " " prefix " => " stamp))
8602 (save-excursion
8603 (goto-char (point-max))
8604 (while (not (bobp))
8d642074 8605 (when (equal marker (org-get-at-bol 'org-marker))
20908596 8606 (org-move-to-column (- (window-width) (length stamp)) t)
71d35b24 8607 (org-agenda-fix-tags-filter-overlays-at (point))
20908596
CD
8608 (if (featurep 'xemacs)
8609 ;; Use `duplicable' property to trigger undo recording
8610 (let ((ex (make-extent nil nil))
8611 (gl (make-glyph stamp)))
8612 (set-glyph-face gl 'secondary-selection)
8613 (set-extent-properties
8614 ex (list 'invisible t 'end-glyph gl 'duplicable t))
8615 (insert-extent ex (1- (point)) (point-at-eol)))
8616 (add-text-properties
8617 (1- (point)) (point-at-eol)
8618 (list 'display (org-add-props stamp nil
8619 'face 'secondary-selection))))
8620 (beginning-of-line 1))
8621 (beginning-of-line 0)))))
8622
8623(defun org-agenda-date-prompt (arg)
8624 "Change the date of this item. Date is prompted for, with default today.
8625The prefix ARG is passed to the `org-time-stamp' command and can therefore
8626be used to request time specification in the time stamp."
8627 (interactive "P")
8628 (org-agenda-check-type t 'agenda 'timeline)
8629 (org-agenda-check-no-diary)
8d642074 8630 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
8631 (org-agenda-error)))
8632 (buffer (marker-buffer marker))
8633 (pos (marker-position marker)))
8634 (org-with-remote-undo buffer
8635 (with-current-buffer buffer
8636 (widen)
8637 (goto-char pos)
ed21c5c8 8638 (if (not (org-at-timestamp-p t))
20908596 8639 (error "Cannot find time stamp"))
ed21c5c8 8640 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8d642074
CD
8641 (org-agenda-show-new-time marker org-last-changed-timestamp))
8642 (message "Time stamp changed to %s" org-last-changed-timestamp)))
20908596 8643
3ab2c837 8644(defun org-agenda-schedule (arg &optional time)
ed21c5c8 8645 "Schedule the item at point.
3ab2c837 8646ARG is passed through to `org-schedule'."
20908596
CD
8647 (interactive "P")
8648 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8649 (org-agenda-check-no-diary)
8d642074 8650 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
8651 (org-agenda-error)))
8652 (type (marker-insertion-type marker))
8653 (buffer (marker-buffer marker))
8654 (pos (marker-position marker))
8655 (org-insert-labeled-timestamps-at-point nil)
8656 ts)
20908596
CD
8657 (set-marker-insertion-type marker t)
8658 (org-with-remote-undo buffer
8659 (with-current-buffer buffer
8660 (widen)
8661 (goto-char pos)
3ab2c837 8662 (setq ts (org-schedule arg time)))
20908596 8663 (org-agenda-show-new-time marker ts "S"))
63aa0982 8664 (message "%s" ts)))
20908596 8665
3ab2c837 8666(defun org-agenda-deadline (arg &optional time)
ed21c5c8 8667 "Schedule the item at point.
3ab2c837 8668ARG is passed through to `org-deadline'."
20908596
CD
8669 (interactive "P")
8670 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8671 (org-agenda-check-no-diary)
8d642074 8672 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
8673 (org-agenda-error)))
8674 (buffer (marker-buffer marker))
8675 (pos (marker-position marker))
8676 (org-insert-labeled-timestamps-at-point nil)
8677 ts)
8678 (org-with-remote-undo buffer
8679 (with-current-buffer buffer
8680 (widen)
8681 (goto-char pos)
3ab2c837 8682 (setq ts (org-deadline arg time)))
8d642074 8683 (org-agenda-show-new-time marker ts "D"))
63aa0982 8684 (message "%s" ts)))
ff4be292 8685
20908596
CD
8686(defun org-agenda-clock-in (&optional arg)
8687 "Start the clock on the currently selected item."
8688 (interactive "P")
8689 (org-agenda-check-no-diary)
8690 (if (equal arg '(4))
8691 (org-clock-in arg)
8d642074 8692 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 8693 (org-agenda-error)))
8d642074 8694 (hdmarker (or (org-get-at-bol 'org-hd-marker)
b349f79f
CD
8695 marker))
8696 (pos (marker-position marker))
8697 newhead)
20908596
CD
8698 (org-with-remote-undo (marker-buffer marker)
8699 (with-current-buffer (marker-buffer marker)
8700 (widen)
8701 (goto-char pos)
b349f79f
CD
8702 (org-show-context 'agenda)
8703 (org-show-entry)
8704 (org-cycle-hide-drawers 'children)
8705 (org-clock-in arg)
8706 (setq newhead (org-get-heading)))
c8d0cf5c 8707 (org-agenda-change-all-lines newhead hdmarker)))))
20908596 8708
afe98dfa 8709(defun org-agenda-clock-out ()
20908596 8710 "Stop the currently running clock."
afe98dfa 8711 (interactive)
20908596
CD
8712 (unless (marker-buffer org-clock-marker)
8713 (error "No running clock"))
c8d0cf5c
CD
8714 (let ((marker (make-marker)) newhead)
8715 (org-with-remote-undo (marker-buffer org-clock-marker)
8716 (with-current-buffer (marker-buffer org-clock-marker)
8717 (save-excursion
8718 (save-restriction
8719 (widen)
8720 (goto-char org-clock-marker)
8721 (org-back-to-heading t)
8722 (move-marker marker (point))
8723 (org-clock-out)
8724 (setq newhead (org-get-heading))))))
8725 (org-agenda-change-all-lines newhead marker)
8726 (move-marker marker nil)))
20908596
CD
8727
8728(defun org-agenda-clock-cancel (&optional arg)
8729 "Cancel the currently running clock."
8730 (interactive "P")
8731 (unless (marker-buffer org-clock-marker)
8732 (error "No running clock"))
8733 (org-with-remote-undo (marker-buffer org-clock-marker)
8734 (org-clock-cancel)))
8735
afe98dfa
CD
8736(defun org-agenda-clock-goto ()
8737 "Jump to the currently clocked in task within the agenda.
8738If the currently clocked in task is not listed in the agenda
8739buffer, display it in another window."
8740 (interactive)
8741 (let (pos)
8742 (mapc (lambda (o)
8743 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
8744 (setq pos (overlay-start o))))
8745 (overlays-in (point-min) (point-max)))
8746 (cond (pos (goto-char pos))
8747 ;; If the currently clocked entry is not in the agenda
8748 ;; buffer, we visit it in another window:
8749 (org-clock-current-task
8750 (org-switch-to-buffer-other-window (org-clock-goto)))
8751 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
8752
8bfe682a
CD
8753(defun org-agenda-diary-entry-in-org-file ()
8754 "Make a diary entry in the file `org-agenda-diary-file'."
5dec9555 8755 (let (d1 d2 char (text "") dp1 dp2)
8bfe682a
CD
8756 (if (equal (buffer-name) "*Calendar*")
8757 (setq d1 (calendar-cursor-to-date t)
8758 d2 (car calendar-mark-ring))
5dec9555
CD
8759 (setq dp1 (get-text-property (point-at-bol) 'day))
8760 (unless dp1 (error "No date defined in current line"))
8761 (setq d1 (calendar-gregorian-from-absolute dp1)
8762 d2 (and (ignore-errors (mark))
8763 (save-excursion
8764 (goto-char (mark))
8765 (setq dp2 (get-text-property (point-at-bol) 'day)))
8766 (calendar-gregorian-from-absolute dp2))))
8bfe682a
CD
8767 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
8768 (setq char (read-char-exclusive))
8769 (cond
8770 ((equal char ?d)
8771 (setq text (read-string "Day entry: "))
5dec9555
CD
8772 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
8773 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
8774 ((equal char ?a)
8775 (setq d1 (list (car d1) (nth 1 d1)
8776 (read-number (format "Reference year [%d]: " (nth 2 d1))
8777 (nth 2 d1))))
8778 (setq text (read-string "Anniversary (use %d to show years): "))
5dec9555
CD
8779 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
8780 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
8781 ((equal char ?b)
8782 (setq text (read-string "Block entry: "))
8783 (unless (and d1 d2 (not (equal d1 d2)))
8784 (error "No block of days selected"))
5dec9555
CD
8785 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
8786 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
8787 ((equal char ?j)
8788 (org-switch-to-buffer-other-window
8789 (find-file-noselect org-agenda-diary-file))
ed21c5c8 8790 (require 'org-datetree)
8bfe682a
CD
8791 (org-datetree-find-date-create d1)
8792 (org-reveal t))
8793 (t (error "Invalid selection character `%c'" char)))))
8794
5dec9555
CD
8795(defcustom org-agenda-insert-diary-strategy 'date-tree
8796 "Where in `org-agenda-diary-file' should new entries be added?
8797Valid values:
8798
8799date-tree in the date tree, as child of the date
8800top-level as top-level entries at the end of the file."
8801 :group 'org-agenda
8802 :type '(choice
8803 (const :tag "in a date tree" date-tree)
8804 (const :tag "as top level at end of file" top-level)))
8805
ed21c5c8
CD
8806(defcustom org-agenda-insert-diary-extract-time nil
8807 "Non-nil means extract any time specification from the diary entry."
8808 :group 'org-agenda
372d7b21 8809 :version "24.1"
ed21c5c8
CD
8810 :type 'boolean)
8811
8223b1d2
BG
8812(defcustom org-agenda-bulk-mark-char ">"
8813 "A single-character string to be used as the bulk mark."
8814 :group 'org-agenda
8815 :version "24.1"
8816 :type 'string)
8817
8bfe682a
CD
8818(defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8819 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8820If TEXT is not empty, it will become the headline of the new entry, and
8821the resulting entry will not be shown. When TEXT is empty, switch to
8822`org-agenda-diary-file' and let the user finish the entry there."
8823 (let ((cw (current-window-configuration)))
8824 (org-switch-to-buffer-other-window
8825 (find-file-noselect org-agenda-diary-file))
8826 (widen)
8827 (goto-char (point-min))
8828 (cond
8829 ((eq type 'anniversary)
8830 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8223b1d2
BG
8831 (progn
8832 (or (org-at-heading-p t)
8833 (progn
8834 (outline-next-heading)
8835 (insert "* Anniversaries\n\n")
8836 (beginning-of-line -1)))))
8bfe682a
CD
8837 (outline-next-heading)
8838 (org-back-over-empty-lines)
8839 (backward-char 1)
8840 (insert "\n")
3ab2c837
BG
8841 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8842 (nth 2 d1) (car d1) (nth 1 d1) text)))
8bfe682a 8843 ((eq type 'day)
ed21c5c8
CD
8844 (let ((org-prefix-has-time t)
8845 (org-agenda-time-leading-zero t)
8846 fmt time time2)
8847 (if org-agenda-insert-diary-extract-time
e66ba1df 8848 ;; Use org-agenda-format-item to parse text for a time-range and
ed21c5c8
CD
8849 ;; remove it. FIXME: This is a hack, we should refactor
8850 ;; that function to make time extraction available separately
e66ba1df 8851 (setq fmt (org-agenda-format-item nil text nil nil t)
ed21c5c8
CD
8852 time (get-text-property 0 'time fmt)
8853 time2 (if (> (length time) 0)
8854 ;; split-string removes trailing ...... if
8855 ;; no end time given. First space
8856 ;; separates time from date.
8857 (concat " " (car (split-string time "\\.")))
8858 nil)
8859 text (get-text-property 0 'txt fmt)))
8860 (if (eq org-agenda-insert-diary-strategy 'top-level)
8861 (org-agenda-insert-diary-as-top-level text)
8862 (require 'org-datetree)
8863 (org-datetree-find-date-create d1)
8864 (org-agenda-insert-diary-make-new-entry text))
8865 (org-insert-time-stamp (org-time-from-absolute
8866 (calendar-absolute-from-gregorian d1))
8867 nil nil nil nil time2))
8bfe682a
CD
8868 (end-of-line 0))
8869 ((eq type 'block)
8870 (if (> (calendar-absolute-from-gregorian d1)
8871 (calendar-absolute-from-gregorian d2))
8872 (setq d1 (prog1 d2 (setq d2 d1))))
5dec9555
CD
8873 (if (eq org-agenda-insert-diary-strategy 'top-level)
8874 (org-agenda-insert-diary-as-top-level text)
8875 (require 'org-datetree)
8876 (org-datetree-find-date-create d1)
8877 (org-agenda-insert-diary-make-new-entry text))
8bfe682a
CD
8878 (org-insert-time-stamp (org-time-from-absolute
8879 (calendar-absolute-from-gregorian d1)))
8880 (insert "--")
8881 (org-insert-time-stamp (org-time-from-absolute
8882 (calendar-absolute-from-gregorian d2)))
8883 (end-of-line 0)))
8884 (if (string-match "\\S-" text)
8885 (progn
8886 (set-window-configuration cw)
8887 (message "%s entry added to %s"
8888 (capitalize (symbol-name type))
8889 (abbreviate-file-name org-agenda-diary-file)))
8890 (org-reveal t)
8891 (message "Please finish entry here"))))
8892
5dec9555
CD
8893(defun org-agenda-insert-diary-as-top-level (text)
8894 "Make new entry as a top-level entry at the end of the file.
8895Add TEXT as headline, and position the cursor in the second line so that
8896a timestamp can be added there."
8897 (widen)
8898 (goto-char (point-max))
8899 (or (bolp) (insert "\n"))
8900 (insert "* " text "\n")
8901 (if org-adapt-indentation (org-indent-to-column 2)))
8902
8bfe682a
CD
8903(defun org-agenda-insert-diary-make-new-entry (text)
8904 "Make new entry as last child of current entry.
8905Add TEXT as headline, and position the cursor in the second line so that
8906a timestamp can be added there."
8907 (let ((org-show-following-heading t)
8908 (org-show-siblings t)
8909 (org-show-hierarchy-above t)
8910 (org-show-entry-below t)
8911 col)
8912 (outline-next-heading)
8913 (org-back-over-empty-lines)
8914 (or (looking-at "[ \t]*$")
8915 (progn (insert "\n") (backward-char 1)))
ed21c5c8 8916 (org-insert-heading nil t)
8bfe682a
CD
8917 (org-do-demote)
8918 (setq col (current-column))
8919 (insert text "\n")
8920 (if org-adapt-indentation (org-indent-to-column col))
8921 (let ((org-show-following-heading t)
8922 (org-show-siblings t)
8923 (org-show-hierarchy-above t)
8924 (org-show-entry-below t))
8925 (org-show-context))))
8926
20908596
CD
8927(defun org-agenda-diary-entry ()
8928 "Make a diary entry, like the `i' command from the calendar.
8bfe682a
CD
8929All the standard commands work: block, weekly etc.
8930When `org-agenda-diary-file' points to a file,
8931`org-agenda-diary-entry-in-org-file' is called instead to create
8932entries in that Org-mode file."
20908596 8933 (interactive)
8bfe682a
CD
8934 (if (not (eq org-agenda-diary-file 'diary-file))
8935 (org-agenda-diary-entry-in-org-file)
8936 (require 'diary-lib)
8937 (let* ((char (progn
8938 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8939 (read-char-exclusive)))
8940 (cmd (cdr (assoc char
8941 '((?d . insert-diary-entry)
8942 (?w . insert-weekly-diary-entry)
8943 (?m . insert-monthly-diary-entry)
8944 (?y . insert-yearly-diary-entry)
8945 (?a . insert-anniversary-diary-entry)
8946 (?b . insert-block-diary-entry)
8947 (?c . insert-cyclic-diary-entry)))))
8948 (oldf (symbol-function 'calendar-cursor-to-date))
8949 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8950 (point (point))
8951 (mark (or (mark t) (point))))
8952 (unless cmd
8953 (error "No command associated with <%c>" char))
8954 (unless (and (get-text-property point 'day)
8955 (or (not (equal ?b char))
8956 (get-text-property mark 'day)))
8957 (error "Don't know which date to use for diary entry"))
8958 ;; We implement this by hacking the `calendar-cursor-to-date' function
8959 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8960 (let ((calendar-mark-ring
8961 (list (calendar-gregorian-from-absolute
8962 (or (get-text-property mark 'day)
8963 (get-text-property point 'day))))))
8964 (unwind-protect
8965 (progn
8966 (fset 'calendar-cursor-to-date
8967 (lambda (&optional error dummy)
8968 (calendar-gregorian-from-absolute
8969 (get-text-property point 'day))))
20908596 8970 (call-interactively cmd))
8bfe682a 8971 (fset 'calendar-cursor-to-date oldf))))))
20908596 8972
20908596 8973(defun org-agenda-execute-calendar-command (cmd)
8223b1d2 8974 "Execute a calendar command from the agenda with date from cursor."
20908596
CD
8975 (org-agenda-check-type t 'agenda 'timeline)
8976 (require 'diary-lib)
8223b1d2
BG
8977 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
8978 (error "Don't know which date to use for the calendar command"))
20908596
CD
8979 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8980 (point (point))
8981 (date (calendar-gregorian-from-absolute
8982 (get-text-property point 'day)))
8983 ;; the following 2 vars are needed in the calendar
8984 (displayed-month (car date))
8985 (displayed-year (nth 2 date)))
8223b1d2
BG
8986 (unwind-protect
8987 (progn
8988 (fset 'calendar-cursor-to-date
8989 (lambda (&optional error dummy)
8990 (calendar-gregorian-from-absolute
8991 (get-text-property point 'day))))
8992 (call-interactively cmd))
8993 (fset 'calendar-cursor-to-date oldf))))
20908596
CD
8994
8995(defun org-agenda-phases-of-moon ()
8996 "Display the phases of the moon for the 3 months around the cursor date."
8997 (interactive)
8998 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8999
9000(defun org-agenda-holidays ()
9001 "Display the holidays for the 3 months around the cursor date."
9002 (interactive)
9003 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9004
8223b1d2
BG
9005(defvar calendar-longitude) ; defined in calendar.el
9006(defvar calendar-latitude) ; defined in calendar.el
9007(defvar calendar-location-name) ; defined in calendar.el
20908596
CD
9008
9009(defun org-agenda-sunrise-sunset (arg)
9010 "Display sunrise and sunset for the cursor date.
9011Latitude and longitude can be specified with the variables
9012`calendar-latitude' and `calendar-longitude'. When called with prefix
9013argument, latitude and longitude will be prompted for."
9014 (interactive "P")
9015 (require 'solar)
9016 (let ((calendar-longitude (if arg nil calendar-longitude))
9017 (calendar-latitude (if arg nil calendar-latitude))
9018 (calendar-location-name
9019 (if arg "the given coordinates" calendar-location-name)))
9020 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9021
9022(defun org-agenda-goto-calendar ()
9023 "Open the Emacs calendar with the date at the cursor."
9024 (interactive)
9025 (org-agenda-check-type t 'agenda 'timeline)
8223b1d2 9026 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
20908596
CD
9027 (error "Don't know which date to open in calendar")))
9028 (date (calendar-gregorian-from-absolute day))
9029 (calendar-move-hook nil)
9030 (calendar-view-holidays-initially-flag nil)
3820f429 9031 (calendar-view-diary-initially-flag nil))
20908596
CD
9032 (calendar)
9033 (calendar-goto-date date)))
9034
9035;;;###autoload
9036(defun org-calendar-goto-agenda ()
9037 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9038This is a command that has to be installed in `calendar-mode-map'."
9039 (interactive)
9040 (org-agenda-list nil (calendar-absolute-from-gregorian
9041 (calendar-cursor-to-date))
9042 nil))
9043
bdebdb64
BG
9044(autoload 'org-calendar-goto-agenda "org-agenda" "\
9045Compute the Org-mode agenda for the calendar date displayed at the cursor.
9046This is a command that has to be installed in `calendar-mode-map'.
9047
9048\(fn)" t nil)
9049
20908596
CD
9050(defun org-agenda-convert-date ()
9051 (interactive)
9052 (org-agenda-check-type t 'agenda 'timeline)
8223b1d2 9053 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
20908596
CD
9054 date s)
9055 (unless day
9056 (error "Don't know which date to convert"))
9057 (setq date (calendar-gregorian-from-absolute day))
9058 (setq s (concat
9059 "Gregorian: " (calendar-date-string date) "\n"
9060 "ISO: " (calendar-iso-date-string date) "\n"
9061 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9062 "Julian: " (calendar-julian-date-string date) "\n"
9063 "Astron. JD: " (calendar-astro-date-string date)
9064 " (Julian date number at noon UTC)\n"
9065 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9066 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9067 "French: " (calendar-french-date-string date) "\n"
9068 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9069 "Mayan: " (calendar-mayan-date-string date) "\n"
9070 "Coptic: " (calendar-coptic-date-string date) "\n"
9071 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9072 "Persian: " (calendar-persian-date-string date) "\n"
9073 "Chinese: " (calendar-chinese-date-string date) "\n"))
9074 (with-output-to-temp-buffer "*Dates*"
9075 (princ s))
93b62de8 9076 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
20908596 9077
c8d0cf5c
CD
9078;;; Bulk commands
9079
54a0dee5
CD
9080(defun org-agenda-bulk-marked-p ()
9081 (eq (get-char-property (point-at-bol) 'type)
9082 'org-marked-entry-overlay))
9083
acedf35c 9084(defun org-agenda-bulk-mark (&optional arg)
c8d0cf5c 9085 "Mark the entry at point for future bulk action."
acedf35c 9086 (interactive "p")
2f885dca 9087 (dotimes (i (or arg 1))
acedf35c
CD
9088 (unless (org-get-at-bol 'org-agenda-diary-link)
9089 (let* ((m (org-get-at-bol 'org-hd-marker))
9090 ov)
9091 (unless (org-agenda-bulk-marked-p)
9092 (unless m (error "Nothing to mark at point"))
9093 (push m org-agenda-bulk-marked-entries)
9094 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8223b1d2 9095 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
acedf35c
CD
9096 (org-get-todo-face "TODO")
9097 'evaporate)
9098 (overlay-put ov 'type 'org-marked-entry-overlay))
9099 (beginning-of-line 2)
9100 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9101 (beginning-of-line 2))
9102 (message "%d entries marked for bulk action"
9103 (length org-agenda-bulk-marked-entries))))))
c8d0cf5c 9104
8223b1d2
BG
9105(defun org-agenda-bulk-mark-all ()
9106 "Mark all entries for future agenda bulk action."
9107 (interactive)
9108 (org-agenda-bulk-mark-regexp "."))
9109
3ab2c837 9110(defun org-agenda-bulk-mark-regexp (regexp)
8223b1d2 9111 "Mark entries matching REGEXP for future agenda bulk action."
3ab2c837 9112 (interactive "sMark entries matching regexp: ")
8c8b834f 9113 (let ((entries-marked 0))
3ab2c837
BG
9114 (save-excursion
9115 (goto-char (point-min))
9116 (goto-char (next-single-property-change (point) 'txt))
9117 (while (re-search-forward regexp nil t)
9118 (when (string-match regexp (get-text-property (point) 'txt))
8c8b834f 9119 (setq entries-marked (1+ entries-marked))
3ab2c837
BG
9120 (call-interactively 'org-agenda-bulk-mark))))
9121 (if (not entries-marked)
9122 (message "No entry matching this regexp."))))
9123
8223b1d2 9124(defun org-agenda-bulk-unmark (&optional arg)
c8d0cf5c 9125 "Unmark the entry at point for future bulk action."
8223b1d2
BG
9126 (interactive "P")
9127 (if arg
9128 (org-agenda-bulk-unmark-all)
9129 (cond ((org-agenda-bulk-marked-p)
9130 (org-agenda-bulk-remove-overlays
9131 (point-at-bol) (+ 2 (point-at-bol)))
9132 (setq org-agenda-bulk-marked-entries
9133 (delete (org-get-at-bol 'org-hd-marker)
9134 org-agenda-bulk-marked-entries))
9135 (beginning-of-line 2)
9136 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9137 (beginning-of-line 2))
9138 (message "%d entries left marked for bulk action"
9139 (length org-agenda-bulk-marked-entries)))
9140 (t (message "No entry to unmark here")))))
c8d0cf5c 9141
54a0dee5 9142(defun org-agenda-bulk-toggle ()
8223b1d2
BG
9143 "Toggle marking the entry at point for bulk action."
9144 (interactive)
9145 (if (org-agenda-bulk-marked-p)
9146 (org-agenda-bulk-unmark)
9147 (org-agenda-bulk-mark)))
c8d0cf5c
CD
9148
9149(defun org-agenda-bulk-remove-overlays (&optional beg end)
9150 "Remove the mark overlays between BEG and END in the agenda buffer.
9151BEG and END default to the buffer limits.
9152
9153This only removes the overlays, it does not remove the markers
9154from the list in `org-agenda-bulk-marked-entries'."
9155 (interactive)
9156 (mapc (lambda (ov)
86fbb8ca
CD
9157 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9158 (delete-overlay ov)))
9159 (overlays-in (or beg (point-min)) (or end (point-max)))))
c8d0cf5c 9160
8223b1d2 9161(defun org-agenda-bulk-unmark-all ()
c8d0cf5c 9162 "Remove all marks in the agenda buffer.
8223b1d2 9163This will remove the markers and the overlays."
c8d0cf5c 9164 (interactive)
8223b1d2
BG
9165 (if (null org-agenda-bulk-marked-entries)
9166 (message "No entry to unmark")
9167 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9168 (setq org-agenda-bulk-marked-entries nil)
9169 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9170
9171(defcustom org-agenda-persistent-marks nil
9172 "Non-nil means marked items will stay marked after a bulk action.
9173You can toggle this interactively by typing `p' when prompted for a
9174bulk action."
9175 :group 'org-agenda
9176 :version "24.1"
9177 :type 'boolean)
c8d0cf5c 9178
ed21c5c8
CD
9179(defun org-agenda-bulk-action (&optional arg)
9180 "Execute an remote-editing action on all marked entries.
9181The prefix arg is passed through to the command if possible."
9182 (interactive "P")
3ab2c837
BG
9183 ;; Make sure we have markers, and only valid ones
9184 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
9185 (mapc
9186 (lambda (m)
9187 (unless (and (markerp m)
9188 (marker-buffer m)
9189 (buffer-live-p (marker-buffer m))
9190 (marker-position m))
9191 (error "Marker %s for bulk command is invalid" m)))
9192 org-agenda-bulk-marked-entries)
9193
9194 ;; Prompt for the bulk command
8223b1d2
BG
9195 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9196 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9197 "[S]catter [f]unction "
9198 (when org-agenda-bulk-custom-functions
9199 (concat " Custom: ["
9200 (mapconcat (lambda(f) (char-to-string (car f)))
9201 org-agenda-bulk-custom-functions "")
9202 "]"))))
9203 (catch 'exit
9204 (let* ((action (read-char-exclusive))
9205 (org-log-refile (if org-log-refile 'time nil))
9206 (entries (reverse org-agenda-bulk-marked-entries))
9207 (org-overriding-default-time
9208 (if (get-text-property (point) 'org-agenda-date-header)
9209 (org-get-cursor-date)))
9210 redo-at-end
9211 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9212 (cond
9213 ((equal action ?p)
9214 (let ((org-agenda-persistent-marks
9215 (not org-agenda-persistent-marks)))
9216 (org-agenda-bulk-action)
9217 (throw 'exit nil)))
9218
9219 ((equal action ?$)
9220 (setq cmd '(org-agenda-archive)))
9221
9222 ((equal action ?A)
9223 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9224
9225 ((member action '(?r ?w))
9226 (setq rfloc (org-refile-get-location
9227 "Refile to"
9228 (marker-buffer (car entries))
9229 org-refile-allow-creating-parent-nodes))
9230 (if (nth 3 rfloc)
9231 (setcar (nthcdr 3 rfloc)
9232 (move-marker (make-marker) (nth 3 rfloc)
9233 (or (get-file-buffer (nth 1 rfloc))
9234 (find-buffer-visiting (nth 1 rfloc))
9235 (error "This should not happen")))))
9236
9237 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9238 redo-at-end t))
9239
9240 ((equal action ?t)
9241 (setq state (org-icompleting-read
9242 "Todo state: "
9243 (with-current-buffer (marker-buffer (car entries))
9244 (mapcar 'list org-todo-keywords-1))))
9245 (setq cmd `(let ((org-inhibit-blocking t)
9246 (org-inhibit-logging 'note))
9247 (org-agenda-todo ,state))))
9248
9249 ((memq action '(?- ?+))
9250 (setq tag (org-icompleting-read
9251 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9252 (with-current-buffer (marker-buffer (car entries))
9253 (delq nil
9254 (mapcar (lambda (x)
9255 (if (stringp (car x)) x)) org-tag-alist)))))
9256 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9257
9258 ((memq action '(?s ?d))
9259 (let* ((time
9260 (unless arg
9261 (org-read-date
9262 nil nil nil
9263 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9264 org-overriding-default-time)))
9265 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9266 (setq cmd `(eval '(,c1 arg ,time)))))
9267
9268 ((equal action ?S)
9269 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9270 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9271 (let ((days (read-number
9272 (format "Scatter tasks across how many %sdays: "
9273 (if arg "week" "")) 7)))
9274 (setq cmd
9275 `(let ((distance (1+ (random ,days))))
9276 (if arg
9277 (let ((dist distance)
9278 (day-of-week
9279 (calendar-day-of-week
9280 (calendar-gregorian-from-absolute (org-today)))))
9281 (dotimes (i (1+ dist))
9282 (while (member day-of-week org-agenda-weekend-days)
9283 (incf distance)
9284 (incf day-of-week)
9285 (if (= day-of-week 7)
9286 (setq day-of-week 0)))
9287 (incf day-of-week)
9288 (if (= day-of-week 7)
9289 (setq day-of-week 0)))))
9290 ;; silently fail when try to replan a sexp entry
9291 (condition-case nil
9292 (let* ((date (calendar-gregorian-from-absolute
9293 (+ (org-today) distance)))
9294 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9295 (nth 2 date))))
9296 (org-agenda-schedule nil time))
9297 (error nil)))))))
9298
9299 ((assoc action org-agenda-bulk-custom-functions)
9300 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9301 redo-at-end t))
9302
9303 ((equal action ?f)
9304 (setq cmd (list (intern
9305 (org-icompleting-read "Function: "
9306 obarray 'fboundp t nil nil)))))
9307
9308 (t (error "Invalid bulk action")))
9309
9310 ;; Sort the markers, to make sure that parents are handled before children
9311 (setq entries (sort entries
9312 (lambda (a b)
9313 (cond
9314 ((equal (marker-buffer a) (marker-buffer b))
9315 (< (marker-position a) (marker-position b)))
9316 (t
9317 (string< (buffer-name (marker-buffer a))
9318 (buffer-name (marker-buffer b))))))))
9319
9320 ;; Now loop over all markers and apply cmd
9321 (while (setq e (pop entries))
9322 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9323 (if (not pos)
9324 (progn (message "Skipping removed entry at %s" e)
9325 (setq cntskip (1+ cntskip)))
9326 (goto-char pos)
9327 (let (org-loop-over-headlines-in-active-region)
9328 (eval cmd))
9329 (setq cnt (1+ cnt))))
9330 (when redo-at-end (org-agenda-redo))
9331 (unless org-agenda-persistent-marks
9332 (org-agenda-bulk-unmark-all))
9333 (message "Acted on %d entries%s%s"
9334 cnt
9335 (if (= cntskip 0)
9336 ""
9337 (format ", skipped %d (disappeared before their turn)"
9338 cntskip))
9339 (if (not org-agenda-persistent-marks)
9340 "" " (kept marked)"))))))
9341
9342(defun org-agenda-capture ()
9343 "Call `org-capture' with the date at point."
9344 (interactive)
9345 (if (not (eq major-mode 'org-agenda-mode))
9346 (error "You cannot do this outside of agenda buffers")
9347 (let ((org-overriding-default-time
9348 (org-get-cursor-date)))
9349 (call-interactively 'org-capture))))
8d642074
CD
9350
9351;;; Flagging notes
9352
9353(defun org-agenda-show-the-flagging-note ()
9354 "Display the flagging note in the other window.
9355When called a second time in direct sequence, offer to remove the FLAGGING
9356tag and (if present) the flagging note."
9357 (interactive)
9358 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9359 (win (selected-window))
9360 note heading newhead)
9361 (unless hdmarker
9362 (error "No linked entry at point"))
9363 (if (and (eq this-command last-command)
9364 (y-or-n-p "Unflag and remove any flagging note? "))
9365 (progn
9366 (org-agenda-remove-flag hdmarker)
9367 (let ((win (get-buffer-window "*Flagging Note*")))
9368 (and win (delete-window win)))
27e428e7 9369 (message "Entry unflagged"))
8d642074
CD
9370 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9371 (unless note
9372 (error "No flagging note"))
9373 (org-kill-new note)
9374 (org-switch-to-buffer-other-window "*Flagging Note*")
9375 (erase-buffer)
9376 (insert note)
9377 (goto-char (point-min))
9378 (while (re-search-forward "\\\\n" nil t)
9379 (replace-match "\n" t t))
9380 (goto-char (point-min))
9381 (select-window win)
9382 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9383
9384(defun org-agenda-remove-flag (marker)
8bfe682a 9385 "Remove the FLAGGED tag and any flagging note in the entry."
8d642074
CD
9386 (let (newhead)
9387 (org-with-point-at marker
9388 (org-toggle-tag "FLAGGED" 'off)
9389 (org-entry-delete nil "THEFLAGGINGNOTE")
9390 (setq newhead (org-get-heading)))
9391 (org-agenda-change-all-lines newhead marker)
27e428e7 9392 (message "Entry unflagged")))
8d642074
CD
9393
9394(defun org-agenda-get-any-marker (&optional pos)
9395 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9396 (get-text-property (or pos (point-at-bol)) 'org-marker)))
c8d0cf5c 9397
20908596
CD
9398;;; Appointment reminders
9399
8223b1d2 9400(defvar appt-time-msg-list) ; defined in appt.el
20908596
CD
9401
9402;;;###autoload
e66ba1df 9403(defun org-agenda-to-appt (&optional refresh filter &rest args)
20908596
CD
9404 "Activate appointments found in `org-agenda-files'.
9405With a \\[universal-argument] prefix, refresh the list of
33306645 9406appointments.
20908596
CD
9407
9408If FILTER is t, interactively prompt the user for a regular
9409expression, and filter out entries that don't match it.
9410
9411If FILTER is a string, use this string as a regular expression
9412for filtering entries out.
9413
e66ba1df
BG
9414If FILTER is a function, filter out entries against which
9415calling the function returns nil. This function takes one
9416argument: an entry from `org-agenda-get-day-entries'.
9417
20908596
CD
9418FILTER can also be an alist with the car of each cell being
9419either 'headline or 'category. For example:
9420
9421 '((headline \"IMPORTANT\")
9422 (category \"Work\"))
9423
9424will only add headlines containing IMPORTANT or headlines
e66ba1df
BG
9425belonging to the \"Work\" category.
9426
9427ARGS are symbols indicating what kind of entries to consider.
9428By default `org-agenda-to-appt' will use :deadline, :scheduled
9429and :timestamp entries. See the docstring of `org-diary' for
8223b1d2
BG
9430details and examples.
9431
9432If an entry as a APPT_WARNTIME property, its value will be used
9433to override `appt-message-warning-time'."
20908596 9434 (interactive "P")
20908596
CD
9435 (if refresh (setq appt-time-msg-list nil))
9436 (if (eq filter t)
9437 (setq filter (read-from-minibuffer "Regexp filter: ")))
9438 (let* ((cnt 0) ; count added events
e66ba1df 9439 (scope (or args '(:deadline :scheduled :timestamp)))
20908596
CD
9440 (org-agenda-new-buffers nil)
9441 (org-deadline-warning-days 0)
acedf35c
CD
9442 ;; Do not use `org-today' here because appt only takes
9443 ;; time and without date as argument, so it may pass wrong
9444 ;; information otherwise
20908596
CD
9445 (today (org-date-to-gregorian
9446 (time-to-days (current-time))))
c8d0cf5c 9447 (org-agenda-restrict nil)
8223b1d2
BG
9448 (files (org-agenda-files 'unrestricted)) entries file
9449 (org-agenda-buffer nil))
20908596 9450 ;; Get all entries which may contain an appt
8223b1d2 9451 (org-agenda-prepare-buffers files)
20908596
CD
9452 (while (setq file (pop files))
9453 (setq entries
e66ba1df
BG
9454 (delq nil
9455 (append entries
9456 (apply 'org-agenda-get-day-entries
9457 file today scope)))))
20908596
CD
9458 ;; Map thru entries and find if we should filter them out
9459 (mapc
9460 (lambda(x)
621f83e4 9461 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
20908596
CD
9462 (cat (get-text-property 1 'org-category x))
9463 (tod (get-text-property 1 'time-of-day x))
9464 (ok (or (null filter)
9465 (and (stringp filter) (string-match filter evt))
e66ba1df 9466 (and (functionp filter) (funcall filter x))
20908596 9467 (and (listp filter)
e66ba1df
BG
9468 (let ((cat-filter (cadr (assoc 'category filter)))
9469 (evt-filter (cadr (assoc 'headline filter))))
9470 (or (and (stringp cat-filter)
9471 (string-match cat-filter cat))
9472 (and (stringp evt-filter)
8223b1d2
BG
9473 (string-match evt-filter evt)))))))
9474 (wrn (get-text-property 1 'warntime x)))
20908596
CD
9475 ;; FIXME: Shall we remove text-properties for the appt text?
9476 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
9477 (when (and ok tod)
621f83e4 9478 (setq tod (concat "00" (number-to-string tod))
20908596 9479 tod (when (string-match
621f83e4 9480 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
20908596
CD
9481 (concat (match-string 1 tod) ":"
9482 (match-string 2 tod))))
8223b1d2
BG
9483 (if (version< emacs-version "23.3")
9484 (appt-add tod evt)
9485 (appt-add tod evt wrn))
20908596
CD
9486 (setq cnt (1+ cnt))))) entries)
9487 (org-release-buffers org-agenda-new-buffers)
9488 (if (eq cnt 0)
9489 (message "No event to add")
9490 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
9491
bdebdb64
BG
9492(autoload 'org-agenda-to-appt "org-agenda" "\
9493Activate appointments found in `org-agenda-files'.
9494With a \\[universal-argument] prefix, refresh the list of
9495appointments.
9496
9497If FILTER is t, interactively prompt the user for a regular
9498expression, and filter out entries that don't match it.
9499
9500If FILTER is a string, use this string as a regular expression
9501for filtering entries out.
9502
9503If FILTER is a function, filter out entries against which
9504calling the function returns nil. This function takes one
9505argument: an entry from `org-agenda-get-day-entries'.
9506
9507FILTER can also be an alist with the car of each cell being
9508either 'headline or 'category. For example:
9509
9510 '((headline \"IMPORTANT\")
9511 (category \"Work\"))
9512
9513will only add headlines containing IMPORTANT or headlines
9514belonging to the \"Work\" category.
9515
9516ARGS are symbols indicating what kind of entries to consider.
9517By default `org-agenda-to-appt' will use :deadline, :scheduled
9518and :timestamp entries. See the docstring of `org-diary' for
9519details and examples.
9520
9521If an entry as a APPT_WARNTIME property, its value will be used
9522to override `appt-message-warning-time'.
9523
9524\(fn &optional REFRESH FILTER &rest ARGS)" t nil)
9525
621f83e4
CD
9526(defun org-agenda-todayp (date)
9527 "Does DATE mean today, when considering `org-extend-today-until'?"
acedf35c
CD
9528 (let ((today (org-today))
9529 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
9530 date)))
9531 (eq date today)))
621f83e4 9532
e66ba1df 9533(defun org-agenda-todo-yesterday (&optional arg)
8223b1d2 9534 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
e66ba1df
BG
9535 (interactive "P")
9536 (let* ((hour (third (decode-time
9537 (org-current-time))))
9538 (org-extend-today-until (1+ hour)))
9539 (org-agenda-todo arg)))
5b409b39 9540
e66ba1df 9541(provide 'org-agenda)
b349f79f 9542
20908596 9543;;; org-agenda.el ends here