Add 2010 to copyright years.
[bpt/emacs.git] / lisp / org / org-agenda.el
CommitLineData
b349f79f 1;;; org-agenda.el --- Dynamic task and appointment lists for Org
20908596 2
114f9c96 3;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
ca8ef0dc 4;; Free Software Foundation, Inc.
20908596
CD
5
6;; Author: Carsten Dominik <carsten at orgmode dot org>
7;; Keywords: outlines, hypermedia, calendar, wp
8;; Homepage: http://orgmode.org
5dec9555 9;; Version: 6.33x
20908596
CD
10;;
11;; This file is part of GNU Emacs.
12;;
b1fc2b50 13;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 14;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
20908596
CD
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
b1fc2b50 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
25;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26;;
27;;; Commentary:
28
29;; This file contains the code for creating and using the Agenda for Org-mode.
30
31;;; Code:
32
33(require 'org)
34(eval-when-compile
71d35b24 35 (require 'cl)
20908596
CD
36 (require 'calendar))
37
b349f79f 38(declare-function diary-add-to-list "diary-lib"
20908596
CD
39 (date string specifier &optional marker globcolor literal))
40(declare-function calendar-absolute-from-iso "cal-iso" (date))
41(declare-function calendar-astro-date-string "cal-julian" (&optional date))
42(declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
20908596
CD
43(declare-function calendar-chinese-date-string "cal-china" (&optional date))
44(declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
45(declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
46(declare-function calendar-french-date-string "cal-french" (&optional date))
47(declare-function calendar-goto-date "cal-move" (date))
48(declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
49(declare-function calendar-islamic-date-string "cal-islam" (&optional date))
50(declare-function calendar-iso-date-string "cal-iso" (&optional date))
f6aafbed 51(declare-function calendar-iso-from-absolute "cal-iso" (date))
20908596
CD
52(declare-function calendar-julian-date-string "cal-julian" (&optional date))
53(declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
54(declare-function calendar-persian-date-string "cal-persia" (&optional date))
68a1b090
GM
55(declare-function org-datetree-find-date-create "org-datetree"
56 (date &optional keep-restriction))
20908596 57(declare-function org-columns-quit "org-colview" ())
8bfe682a
CD
58(declare-function diary-date-display-form "diary-lib" (&optional type))
59(declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
60(declare-function org-habit-insert-consistency-graphs
61 "org-habit" (&optional line))
62(declare-function org-is-habit-p "org-habit" (&optional pom))
63(declare-function org-habit-parse-todo "org-habit" (&optional pom))
68a1b090 64(declare-function org-habit-get-priority "org-habit" (habit &optional moment))
20908596 65(defvar calendar-mode-map)
8d642074 66(defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
8bfe682a
CD
67(defvar org-habit-show-habits)
68(defvar org-habit-show-habits-only-for-today)
20908596
CD
69
70;; Defined somewhere in this file, but used before definition.
71(defvar org-agenda-buffer-name)
72(defvar org-agenda-overriding-header)
8d642074 73(defvar org-agenda-title-append nil)
20908596
CD
74(defvar entry)
75(defvar date)
76(defvar org-agenda-undo-list)
77(defvar org-agenda-pending-undo-list)
78(defvar original-date) ; dynamically scoped, calendar.el does scope this
79
80(defcustom org-agenda-confirm-kill 1
81 "When set, remote killing from the agenda buffer needs confirmation.
82When t, a confirmation is always needed. When a number N, confirmation is
83only needed when the text to be killed contains more than N non-white lines."
84 :group 'org-agenda
85 :type '(choice
86 (const :tag "Never" nil)
87 (const :tag "Always" t)
c8d0cf5c 88 (integer :tag "When more than N lines")))
20908596
CD
89
90(defcustom org-agenda-compact-blocks nil
91 "Non-nil means, make the block agenda more compact.
92This is done by leaving out unnecessary lines."
93 :group 'org-agenda
94 :type 'boolean)
95
0bd48b37
CD
96(defcustom org-agenda-block-separator ?=
97 "The separator between blocks in the agenda.
98If this is a string, it will be used as the separator, with a newline added.
99If it is a character, it will be repeated to fill the window width."
100 :group 'org-agenda
101 :type '(choice
102 (character)
103 (string)))
104
20908596
CD
105(defgroup org-agenda-export nil
106 "Options concerning exporting agenda views in Org-mode."
107 :tag "Org Agenda Export"
108 :group 'org-agenda)
109
110(defcustom org-agenda-with-colors t
111 "Non-nil means, use colors in agenda views."
112 :group 'org-agenda-export
113 :type 'boolean)
114
115(defcustom org-agenda-exporter-settings nil
116 "Alist of variable/value pairs that should be active during agenda export.
c8d0cf5c
CD
117This is a good place to set options for ps-print and for htmlize.
118Note that the way this is implemented, the values will be evaluated
119before assigned to the variables. So make sure to quote values you do
120*not* want evaluated, for example
121
122 (setq org-agenda-exporter-settings
123 '((ps-print-color-p 'black-white)))"
20908596
CD
124 :group 'org-agenda-export
125 :type '(repeat
126 (list
127 (variable)
128 (sexp :tag "Value"))))
129
c8d0cf5c
CD
130(defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
131 "Hook run in temporary buffer before writing it to an export file.
132A useful function is `org-agenda-add-entry-text'."
133 :group 'org-agenda-export
134 :type 'hook
135 :options '(org-agenda-add-entry-text))
136
137(defcustom org-agenda-add-entry-text-maxlines 0
138 "Maximum number of entry text lines to be added to agenda.
139This is only relevant when `org-agenda-add-entry-text' is part of
140`org-agenda-before-write-hook', which it is by default.
141When this is 0, nothing will happen. When it is greater than 0, it
142specifies the maximum number of lines that will be added for each entry
54a0dee5
CD
143that is listed in the agenda view.
144
145Note that this variable is not used during display, only when exporting
146the agenda. For agenda display, see org-agenda-entry-text-mode and the
147variable `org-agenda-entry-text-maxlines'."
c8d0cf5c
CD
148 :group 'org-agenda
149 :type 'integer)
150
151(defcustom org-agenda-add-entry-text-descriptive-links t
152 "Non-nil means, export org-links as descriptive links in agenda added text.
153This variable applies to the text added to the agenda when
154`org-agenda-add-entry-text-maxlines' is larger than 0.
155When this variable nil, the URL will (also) be shown."
156 :group 'org-agenda
157 :type 'boolean)
158
20908596
CD
159(defcustom org-agenda-export-html-style ""
160 "The style specification for exported HTML Agenda files.
161If this variable contains a string, it will replace the default <style>
162section as produced by `htmlize'.
163Since there are different ways of setting style information, this variable
164needs to contain the full HTML structure to provide a style, including the
165surrounding HTML tags. The style specifications should include definitions
166the fonts used by the agenda, here is an example:
167
168 <style type=\"text/css\">
169 p { font-weight: normal; color: gray; }
170 .org-agenda-structure {
171 font-size: 110%;
172 color: #003399;
173 font-weight: 600;
174 }
175 .org-todo {
176 color: #cc6666;
177 font-weight: bold;
178 }
c8d0cf5c
CD
179 .org-agenda-done {
180 color: #339933;
181 }
20908596
CD
182 .org-done {
183 color: #339933;
184 }
185 .title { text-align: center; }
186 .todo, .deadline { color: red; }
187 .done { color: green; }
188 </style>
189
190or, if you want to keep the style in a file,
191
192 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
193
194As the value of this option simply gets inserted into the HTML <head> header,
195you can \"misuse\" it to also add other text to the header. However,
196<style>...</style> is required, if not present the variable will be ignored."
197 :group 'org-agenda-export
198 :group 'org-export-html
199 :type 'string)
200
201(defgroup org-agenda-custom-commands nil
202 "Options concerning agenda views in Org-mode."
203 :tag "Org Agenda Custom Commands"
204 :group 'org-agenda)
205
206(defconst org-sorting-choice
207 '(choice
208 (const time-up) (const time-down)
209 (const category-keep) (const category-up) (const category-down)
210 (const tag-down) (const tag-up)
211 (const priority-up) (const priority-down)
621f83e4 212 (const todo-state-up) (const todo-state-down)
c8d0cf5c 213 (const effort-up) (const effort-down)
8bfe682a 214 (const habit-up) (const habit-down)
c8d0cf5c 215 (const user-defined-up) (const user-defined-down))
20908596
CD
216 "Sorting choices.")
217
218(defconst org-agenda-custom-commands-local-options
219 `(repeat :tag "Local settings for this command. Remember to quote values"
220 (choice :tag "Setting"
c8d0cf5c
CD
221 (list :tag "Heading for this block"
222 (const org-agenda-overriding-header)
223 (string :tag "Headline"))
20908596
CD
224 (list :tag "Files to be searched"
225 (const org-agenda-files)
226 (list
227 (const :format "" quote)
c8d0cf5c 228 (repeat (file))))
20908596
CD
229 (list :tag "Sorting strategy"
230 (const org-agenda-sorting-strategy)
231 (list
232 (const :format "" quote)
233 (repeat
234 ,org-sorting-choice)))
235 (list :tag "Prefix format"
236 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
237 (string))
238 (list :tag "Number of days in agenda"
239 (const org-agenda-ndays)
240 (integer :value 1))
241 (list :tag "Fixed starting date"
242 (const org-agenda-start-day)
243 (string :value "2007-11-01"))
244 (list :tag "Start on day of week"
245 (const org-agenda-start-on-weekday)
246 (choice :value 1
247 (const :tag "Today" nil)
c8d0cf5c 248 (integer :tag "Weekday No.")))
20908596
CD
249 (list :tag "Include data from diary"
250 (const org-agenda-include-diary)
251 (boolean))
252 (list :tag "Deadline Warning days"
253 (const org-deadline-warning-days)
254 (integer :value 1))
c8d0cf5c
CD
255 (list :tag "Tags filter preset"
256 (const org-agenda-filter-preset)
257 (list
258 (const :format "" quote)
259 (repeat
260 (string :tag "+tag or -tag"))))
20908596
CD
261 (list :tag "Standard skipping condition"
262 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
263 (const org-agenda-skip-function)
264 (list
265 (const :format "" quote)
266 (list
267 (choice
33306645 268 :tag "Skipping range"
20908596
CD
269 (const :tag "Skip entry" org-agenda-skip-entry-if)
270 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
271 (repeat :inline t :tag "Conditions for skipping"
272 (choice
273 :tag "Condition type"
274 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
275 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
276 (const :tag "scheduled" 'scheduled)
277 (const :tag "not scheduled" 'notscheduled)
278 (const :tag "deadline" 'deadline)
c8d0cf5c
CD
279 (const :tag "no deadline" 'notdeadline)
280 (const :tag "timestamp" 'timestamp)
281 (const :tag "no timestamp" 'nottimestamp))))))
20908596
CD
282 (list :tag "Non-standard skipping condition"
283 :value (org-agenda-skip-function)
2c3ad40d 284 (const org-agenda-skip-function)
c8d0cf5c
CD
285 (sexp :tag "Function or form (quoted!)"))
286 (list :tag "Any variable"
287 (variable :tag "Variable")
288 (sexp :tag "Value (sexp)"))))
20908596
CD
289 "Selection of examples for agenda command settings.
290This will be spliced into the custom type of
291`org-agenda-custom-commands'.")
292
293
294(defcustom org-agenda-custom-commands nil
295 "Custom commands for the agenda.
296These commands will be offered on the splash screen displayed by the
297agenda dispatcher \\[org-agenda]. Each entry is a list like this:
298
299 (key desc type match settings files)
300
301key The key (one or more characters as a string) to be associated
302 with the command.
303desc A description of the command, when omitted or nil, a default
304 description is built using MATCH.
305type The command type, any of the following symbols:
306 agenda The daily/weekly agenda.
307 todo Entries with a specific TODO keyword, in all agenda files.
308 search Entries containing search words entry or headline.
309 tags Tags/Property/TODO match in all agenda files.
310 tags-todo Tags/P/T match in all agenda files, TODO entries only.
311 todo-tree Sparse tree of specific TODO keyword in *current* file.
312 tags-tree Sparse tree with all tags matches in *current* file.
313 occur-tree Occur sparse tree for *current* file.
314 ... A user-defined function.
315match What to search for:
316 - a single keyword for TODO keyword searches
317 - a tags match expression for tags searches
318 - a word search expression for text searches.
319 - a regular expression for occur searches
320 For all other commands, this should be the empty string.
321settings A list of option settings, similar to that in a let form, so like
322 this: ((opt1 val1) (opt2 val2) ...). The values will be
323 evaluated at the moment of execution, so quote them when needed.
324files A list of files file to write the produced agenda buffer to
325 with the command `org-store-agenda-views'.
326 If a file name ends in \".html\", an HTML version of the buffer
327 is written out. If it ends in \".ps\", a postscript version is
33306645 328 produced. Otherwise, only the plain text is written to the file.
20908596
CD
329
330You can also define a set of commands, to create a composite agenda buffer.
331In this case, an entry looks like this:
332
333 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
334
335where
336
337desc A description string to be displayed in the dispatcher menu.
338cmd An agenda command, similar to the above. However, tree commands
339 are no allowed, but instead you can get agenda and global todo list.
340 So valid commands for a set are:
341 (agenda \"\" settings)
342 (alltodo \"\" settings)
343 (stuck \"\" settings)
344 (todo \"match\" settings files)
345 (search \"match\" settings files)
346 (tags \"match\" settings files)
347 (tags-todo \"match\" settings files)
348
349Each command can carry a list of options, and another set of options can be
350given for the whole set of commands. Individual command options take
351precedence over the general options.
352
353When using several characters as key to a command, the first characters
354are prefix commands. For the dispatcher to display useful information, you
355should provide a description for the prefix, like
356
357 (setq org-agenda-custom-commands
358 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
359 (\"hl\" tags \"+HOME+Lisa\")
360 (\"hp\" tags \"+HOME+Peter\")
361 (\"hk\" tags \"+HOME+Kim\")))"
362 :group 'org-agenda-custom-commands
363 :type `(repeat
364 (choice :value ("x" "Describe command here" tags "" nil)
365 (list :tag "Single command"
366 (string :tag "Access Key(s) ")
367 (option (string :tag "Description"))
368 (choice
369 (const :tag "Agenda" agenda)
370 (const :tag "TODO list" alltodo)
371 (const :tag "Search words" search)
372 (const :tag "Stuck projects" stuck)
c8d0cf5c
CD
373 (const :tag "Tags/Property match (all agenda files)" tags)
374 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
20908596
CD
375 (const :tag "TODO keyword search (all agenda files)" todo)
376 (const :tag "Tags sparse tree (current buffer)" tags-tree)
377 (const :tag "TODO keyword tree (current buffer)" todo-tree)
378 (const :tag "Occur tree (current buffer)" occur-tree)
379 (sexp :tag "Other, user-defined function"))
380 (string :tag "Match (only for some commands)")
381 ,org-agenda-custom-commands-local-options
382 (option (repeat :tag "Export" (file :tag "Export to"))))
383 (list :tag "Command series, all agenda files"
384 (string :tag "Access Key(s)")
385 (string :tag "Description ")
386 (repeat :tag "Component"
387 (choice
388 (list :tag "Agenda"
389 (const :format "" agenda)
390 (const :tag "" :format "" "")
391 ,org-agenda-custom-commands-local-options)
392 (list :tag "TODO list (all keywords)"
393 (const :format "" alltodo)
394 (const :tag "" :format "" "")
395 ,org-agenda-custom-commands-local-options)
396 (list :tag "Search words"
397 (const :format "" search)
398 (string :tag "Match")
399 ,org-agenda-custom-commands-local-options)
400 (list :tag "Stuck projects"
401 (const :format "" stuck)
402 (const :tag "" :format "" "")
403 ,org-agenda-custom-commands-local-options)
404 (list :tag "Tags search"
405 (const :format "" tags)
406 (string :tag "Match")
407 ,org-agenda-custom-commands-local-options)
408 (list :tag "Tags search, TODO entries only"
409 (const :format "" tags-todo)
410 (string :tag "Match")
411 ,org-agenda-custom-commands-local-options)
412 (list :tag "TODO keyword search"
413 (const :format "" todo)
414 (string :tag "Match")
415 ,org-agenda-custom-commands-local-options)
416 (list :tag "Other, user-defined function"
417 (symbol :tag "function")
418 (string :tag "Match")
419 ,org-agenda-custom-commands-local-options)))
420
421 (repeat :tag "Settings for entire command set"
422 (list (variable :tag "Any variable")
423 (sexp :tag "Value")))
424 (option (repeat :tag "Export" (file :tag "Export to"))))
425 (cons :tag "Prefix key documentation"
426 (string :tag "Access Key(s)")
427 (string :tag "Description ")))))
428
429(defcustom org-agenda-query-register ?o
430 "The register holding the current query string.
33306645 431The purpose of this is that if you construct a query string interactively,
20908596
CD
432you can then use it to define a custom command."
433 :group 'org-agenda-custom-commands
434 :type 'character)
435
436(defcustom org-stuck-projects
437 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
438 "How to identify stuck projects.
439This is a list of four items:
c8d0cf5c
CD
4401. A tags/todo/property matcher string that is used to identify a project.
441 See the manual for a description of tag and property searches.
20908596
CD
442 The entire tree below a headline matched by this is considered one project.
4432. A list of TODO keywords identifying non-stuck projects.
444 If the project subtree contains any headline with one of these todo
445 keywords, the project is considered to be not stuck. If you specify
446 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
4473. A list of tags identifying non-stuck projects.
448 If the project subtree contains any headline with one of these tags,
449 the project is considered to be not stuck. If you specify \"*\" as
c8d0cf5c
CD
450 a tag, any tag will mark the project unstuck. Note that this is about
451 the explicit presence of a tag somewhere in the subtree, inherited
452 tags to not count here. If inherited tags make a project not stuck,
453 use \"-TAG\" in the tags part of the matcher under (1.) above.
20908596
CD
4544. An arbitrary regular expression matching non-stuck projects.
455
c8d0cf5c
CD
456If the project turns out to be not stuck, search continues also in the
457subtree to see if any of the subtasks have project status.
458
459See also the variable `org-tags-match-list-sublevels' which applies
460to projects matched by this search as well.
461
20908596
CD
462After defining this variable, you may use \\[org-agenda-list-stuck-projects]
463or `C-c a #' to produce the list."
464 :group 'org-agenda-custom-commands
465 :type '(list
466 (string :tag "Tags/TODO match to identify a project")
467 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
468 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
c8d0cf5c 469 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
20908596 470
71d35b24
CD
471(defcustom org-agenda-filter-effort-default-operator "<"
472 "The default operator for effort estimate filtering.
93b62de8 473If you select an effort estimate limit without first pressing an operator,
71d35b24
CD
474this one will be used."
475 :group 'org-agenda-custom-commands
476 :type '(choice (const :tag "less or equal" "<")
477 (const :tag "greater or equal"">")
478 (const :tag "equal" "=")))
20908596
CD
479
480(defgroup org-agenda-skip nil
481 "Options concerning skipping parts of agenda files."
482 :tag "Org Agenda Skip"
483 :group 'org-agenda)
0bd48b37
CD
484(defgroup org-agenda-daily/weekly nil
485 "Options concerning the daily/weekly agenda."
486 :tag "Org Agenda Daily/Weekly"
487 :group 'org-agenda)
488(defgroup org-agenda-todo-list nil
489 "Options concerning the global todo list agenda view."
490 :tag "Org Agenda Todo List"
491 :group 'org-agenda)
492(defgroup org-agenda-match-view nil
493 "Options concerning the general tags/property/todo match agenda view."
494 :tag "Org Agenda Match View"
495 :group 'org-agenda)
8bfe682a
CD
496(defgroup org-agenda-search-view nil
497 "Options concerning the general tags/property/todo match agenda view."
498 :tag "Org Agenda Match View"
499 :group 'org-agenda)
20908596 500
2c3ad40d
CD
501(defvar org-agenda-archives-mode nil
502 "Non-nil means, the agenda will include archived items.
503If this is the symbol `trees', trees in the selected agenda scope
504that are marked with the ARCHIVE tag will be included anyway. When this is
505t, also all archive files associated with the current selection of agenda
506files will be included.")
507
b349f79f 508(defcustom org-agenda-skip-comment-trees t
93b62de8 509 "Non-nil means, skip trees that start with the COMMENT keyword.
33306645 510When nil, these trees are also scanned by agenda commands."
b349f79f
CD
511 :group 'org-agenda-skip
512 :type 'boolean)
513
20908596
CD
514(defcustom org-agenda-todo-list-sublevels t
515 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
516When nil, the sublevels of a TODO entry are not checked, resulting in
517potentially much shorter TODO lists."
518 :group 'org-agenda-skip
0bd48b37 519 :group 'org-agenda-todo-list
20908596
CD
520 :type 'boolean)
521
522(defcustom org-agenda-todo-ignore-with-date nil
523 "Non-nil means, don't show entries with a date in the global todo list.
524You can use this if you prefer to mark mere appointments with a TODO keyword,
525but don't want them to show up in the TODO list.
526When this is set, it also covers deadlines and scheduled items, the settings
527of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
c8d0cf5c
CD
528will be ignored.
529See also the variable `org-agenda-tags-todo-honor-ignore-options'."
20908596 530 :group 'org-agenda-skip
0bd48b37 531 :group 'org-agenda-todo-list
20908596
CD
532 :type 'boolean)
533
534(defcustom org-agenda-todo-ignore-scheduled nil
535 "Non-nil means, don't show scheduled entries in the global todo list.
536The idea behind this is that by scheduling it, you have already taken care
537of this item.
c8d0cf5c
CD
538See also `org-agenda-todo-ignore-with-date'.
539See also the variable `org-agenda-tags-todo-honor-ignore-options'."
20908596 540 :group 'org-agenda-skip
0bd48b37 541 :group 'org-agenda-todo-list
20908596
CD
542 :type 'boolean)
543
544(defcustom org-agenda-todo-ignore-deadlines nil
545 "Non-nil means, don't show near deadline entries in the global todo list.
546Near means closer than `org-deadline-warning-days' days.
547The idea behind this is that such items will appear in the agenda anyway.
c8d0cf5c
CD
548See also `org-agenda-todo-ignore-with-date'.
549See also the variable `org-agenda-tags-todo-honor-ignore-options'."
20908596 550 :group 'org-agenda-skip
0bd48b37
CD
551 :group 'org-agenda-todo-list
552 :type 'boolean)
553
554(defcustom org-agenda-tags-todo-honor-ignore-options nil
555 "Non-nil means, honor todo-list ...ignore options also in tags-todo search.
556The variables
557 `org-agenda-todo-ignore-with-date',
c8d0cf5c 558 `org-agenda-todo-ignore-scheduled'
0bd48b37
CD
559 `org-agenda-todo-ignore-deadlines'
560make the global TODO list skip entries that have time stamps of certain
561kinds. If this option is set, the same options will also apply for the
562tags-todo search, which is the general tags/property matcher
563restricted to unfinished TODO entries only."
564 :group 'org-agenda-skip
565 :group 'org-agenda-todo-list
566 :group 'org-agenda-match-view
20908596
CD
567 :type 'boolean)
568
569(defcustom org-agenda-skip-scheduled-if-done nil
570 "Non-nil means don't show scheduled items in agenda when they are done.
571This is relevant for the daily/weekly agenda, not for the TODO list. And
572it applies only to the actual date of the scheduling. Warnings about
573an item with a past scheduling dates are always turned off when the item
574is DONE."
575 :group 'org-agenda-skip
0bd48b37 576 :group 'org-agenda-daily/weekly
20908596
CD
577 :type 'boolean)
578
54a0dee5
CD
579(defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
580 "Non-nil means skip scheduling line if same entry shows because of deadline.
581In the agenda of today, an entry can show up multiple times because
582it is both scheduled and has a nearby deadline, and maybe a plain time
583stamp as well.
584When this variable is t, then only the deadline is shown and the fact that
585the entry is scheduled today or was scheduled previously is not shown.
586When this variable is nil, the entry will be shown several times. When
587the variable is the symbol `not-today', then skip scheduled previously,
588but not scheduled today."
589 :group 'org-agenda-skip
590 :group 'org-agenda-daily/weekly
591 :type '(choice
592 (const :tag "Never" nil)
593 (const :tag "Always" t)
594 (const :tag "Not when scheduled today" not-today)))
595
20908596 596(defcustom org-agenda-skip-deadline-if-done nil
33306645 597 "Non-nil means don't show deadlines when the corresponding item is done.
20908596
CD
598When nil, the deadline is still shown and should give you a happy feeling.
599This is relevant for the daily/weekly agenda. And it applied only to the
33306645 600actually date of the deadline. Warnings about approaching and past-due
20908596
CD
601deadlines are always turned off when the item is DONE."
602 :group 'org-agenda-skip
0bd48b37 603 :group 'org-agenda-daily/weekly
20908596
CD
604 :type 'boolean)
605
c8d0cf5c
CD
606(defcustom org-agenda-skip-additional-timestamps-same-entry t
607 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
608When non-nil, after the search for timestamps has matched once in an
609entry, the rest of the entry will not be searched."
610 :group 'org-agenda-skip
611 :type 'boolean)
612
20908596
CD
613(defcustom org-agenda-skip-timestamp-if-done nil
614 "Non-nil means don't select item by timestamp or -range if it is DONE."
615 :group 'org-agenda-skip
0bd48b37 616 :group 'org-agenda-daily/weekly
20908596
CD
617 :type 'boolean)
618
d6685abc
CD
619(defcustom org-agenda-dim-blocked-tasks t
620 "Non-nil means, dim blocked tasks in the agenda display.
c8d0cf5c
CD
621This causes some overhead during agenda construction, but if you
622have turned on `org-enforce-todo-dependencies',
623`org-enforce-todo-checkbox-dependencies', or any other blocking
624mechanism, this will create useful feedback in the agenda.
625
8bfe682a 626Instead of t, this variable can also have the value `invisible'.
c8d0cf5c
CD
627Then blocked tasks will be invisible and only become visible when
628they become unblocked. An exemption to this behavior is when a task is
629blocked because of unchecked checkboxes below it. Since checkboxes do
630not show up in the agenda views, making this task invisible you remove any
631trace from agenda views that there is something to do. Therefore, a task
632that is blocked because of checkboxes will never be made invisible, it
633will only be dimmed."
d6685abc
CD
634 :group 'org-agenda-daily/weekly
635 :group 'org-agenda-todo-list
636 :type '(choice
637 (const :tag "Do not dim" nil)
638 (const :tag "Dim to a grey face" t)
8bfe682a 639 (const :tag "Make invisible" invisible)))
d6685abc 640
20908596
CD
641(defcustom org-timeline-show-empty-dates 3
642 "Non-nil means, `org-timeline' also shows dates without an entry.
643When nil, only the days which actually have entries are shown.
644When t, all days between the first and the last date are shown.
645When an integer, show also empty dates, but if there is a gap of more than
646N days, just insert a special line indicating the size of the gap."
647 :group 'org-agenda-skip
648 :type '(choice
649 (const :tag "None" nil)
650 (const :tag "All" t)
c8d0cf5c 651 (integer :tag "at most")))
20908596 652
20908596
CD
653(defgroup org-agenda-startup nil
654 "Options concerning initial settings in the Agenda in Org Mode."
655 :tag "Org Agenda Startup"
656 :group 'org-agenda)
657
658(defcustom org-finalize-agenda-hook nil
659 "Hook run just before displaying an agenda buffer."
660 :group 'org-agenda-startup
661 :type 'hook)
662
663(defcustom org-agenda-mouse-1-follows-link nil
664 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
665A longer mouse click will still set point. Does not work on XEmacs.
666Needs to be set before org.el is loaded."
667 :group 'org-agenda-startup
668 :type 'boolean)
669
670(defcustom org-agenda-start-with-follow-mode nil
671 "The initial value of follow-mode in a newly created agenda window."
672 :group 'org-agenda-startup
673 :type 'boolean)
674
1bcdebed
CD
675(defcustom org-agenda-show-outline-path t
676 "Non-il means, show outline path in echo area after line motion."
677 :group 'org-agenda-startup
678 :type 'boolean)
679
54a0dee5
CD
680(defcustom org-agenda-start-with-entry-text-mode nil
681 "The initial value of entry-text-mode in a newly created agenda window."
682 :group 'org-agenda-startup
683 :type 'boolean)
684
685(defcustom org-agenda-entry-text-maxlines 5
8bfe682a 686 "Number of text lines to be added when `E' is pressed in the agenda.
54a0dee5
CD
687
688Note that this variable only used during agenda display. Add add entry text
689when exporting the agenda, configure the variable
690`org-agenda-add-entry-ext-maxlines'."
691 :group 'org-agenda
692 :type 'integer)
693
8d642074
CD
694(defcustom org-agenda-entry-text-exclude-regexps nil
695 "List of regular expressions to clean up entry text.
696The complete matches of all regular expressions in this list will be
697removed from entry text before it is shown in the agenda."
698 :group 'org-agenda
699 :type '(repeat (regexp)))
700
701(defvar org-agenda-entry-text-cleanup-hook nil
702 "Hook that is run after basic cleanup of entry text to be shown in agenda.
703This cleanup is done in a temporary buffer, so the function may inspect and
704change the entire buffer.
705Some default stuff like drawers and scheduling/deadline dates will already
706have been removed when this is called, as will any matches for regular
707expressions listed in `org-agenda-entry-text-exclude-regexps'.")
708
20908596
CD
709(defvar org-agenda-include-inactive-timestamps nil
710 "Non-nil means, include inactive time stamps in agenda and timeline.")
711
712(defgroup org-agenda-windows nil
713 "Options concerning the windows used by the Agenda in Org Mode."
714 :tag "Org Agenda Windows"
715 :group 'org-agenda)
716
717(defcustom org-agenda-window-setup 'reorganize-frame
718 "How the agenda buffer should be displayed.
719Possible values for this option are:
720
721current-window Show agenda in the current window, keeping all other windows.
20908596
CD
722other-window Use `switch-to-buffer-other-window' to display agenda.
723reorganize-frame Show only two windows on the current frame, the current
724 window and the agenda.
8d642074
CD
725other-frame Use `switch-to-buffer-other-frame' to display agenda.
726 Also, when exiting the agenda, kill that frame.
20908596
CD
727See also the variable `org-agenda-restore-windows-after-quit'."
728 :group 'org-agenda-windows
729 :type '(choice
730 (const current-window)
731 (const other-frame)
732 (const other-window)
733 (const reorganize-frame)))
734
735(defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
736 "The min and max height of the agenda window as a fraction of frame height.
737The value of the variable is a cons cell with two numbers between 0 and 1.
738It only matters if `org-agenda-window-setup' is `reorganize-frame'."
739 :group 'org-agenda-windows
740 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
741
742(defcustom org-agenda-restore-windows-after-quit nil
743 "Non-nil means, restore window configuration open exiting agenda.
744Before the window configuration is changed for displaying the agenda,
745the current status is recorded. When the agenda is exited with
746`q' or `x' and this option is set, the old state is restored. If
747`org-agenda-window-setup' is `other-frame', the value of this
baf0cb84 748option will be ignored."
20908596
CD
749 :group 'org-agenda-windows
750 :type 'boolean)
751
20908596
CD
752(defcustom org-agenda-ndays 7
753 "Number of days to include in overview display.
c8d0cf5c
CD
754Should be 1 or 7.
755Custom commands can set this variable in the options section."
20908596 756 :group 'org-agenda-daily/weekly
c8d0cf5c 757 :type 'integer)
20908596
CD
758
759(defcustom org-agenda-start-on-weekday 1
760 "Non-nil means, start the overview always on the specified weekday.
7610 denotes Sunday, 1 denotes Monday etc.
c8d0cf5c
CD
762When nil, always start on the current day.
763Custom commands can set this variable in the options section."
20908596
CD
764 :group 'org-agenda-daily/weekly
765 :type '(choice (const :tag "Today" nil)
c8d0cf5c 766 (integer :tag "Weekday No.")))
20908596
CD
767
768(defcustom org-agenda-show-all-dates t
769 "Non-nil means, `org-agenda' shows every day in the selected range.
770When nil, only the days which actually have entries are shown."
771 :group 'org-agenda-daily/weekly
772 :type 'boolean)
773
774(defcustom org-agenda-format-date 'org-agenda-format-date-aligned
775 "Format string for displaying dates in the agenda.
776Used by the daily/weekly agenda and by the timeline. This should be
777a format string understood by `format-time-string', or a function returning
778the formatted date as a string. The function must take a single argument,
779a calendar-style date list like (month day year)."
780 :group 'org-agenda-daily/weekly
781 :type '(choice
782 (string :tag "Format string")
783 (function :tag "Function")))
784
785(defun org-agenda-format-date-aligned (date)
786 "Format a date string for display in the daily/weekly agenda, or timeline.
787This function makes sure that dates are aligned for easy reading."
788 (require 'cal-iso)
789 (let* ((dayname (calendar-day-name date))
790 (day (cadr date))
791 (day-of-week (calendar-day-of-week date))
792 (month (car date))
793 (monthname (calendar-month-name month))
794 (year (nth 2 date))
795 (iso-week (org-days-to-iso-week
796 (calendar-absolute-from-gregorian date)))
797 (weekyear (cond ((and (= month 1) (>= iso-week 52))
798 (1- year))
799 ((and (= month 12) (<= iso-week 1))
800 (1+ year))
801 (t year)))
802 (weekstring (if (= day-of-week 1)
803 (format " W%02d" iso-week)
804 "")))
805 (format "%-10s %2d %s %4d%s"
806 dayname day monthname year weekstring)))
807
808(defcustom org-agenda-weekend-days '(6 0)
809 "Which days are weekend?
810These days get the special face `org-agenda-date-weekend' in the agenda
811and timeline buffers."
812 :group 'org-agenda-daily/weekly
813 :type '(set :greedy t
814 (const :tag "Monday" 1)
815 (const :tag "Tuesday" 2)
816 (const :tag "Wednesday" 3)
817 (const :tag "Thursday" 4)
818 (const :tag "Friday" 5)
819 (const :tag "Saturday" 6)
820 (const :tag "Sunday" 0)))
821
822(defcustom org-agenda-include-diary nil
c8d0cf5c
CD
823 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
824Custom commands can set this variable in the options section."
20908596
CD
825 :group 'org-agenda-daily/weekly
826 :type 'boolean)
827
828(defcustom org-agenda-include-all-todo nil
829 "Set means weekly/daily agenda will always contain all TODO entries.
830The TODO entries will be listed at the top of the agenda, before
0bd48b37
CD
831the entries for specific days.
832This option is deprecated, it is better to define a block agenda instead."
20908596
CD
833 :group 'org-agenda-daily/weekly
834 :type 'boolean)
835
836(defcustom org-agenda-repeating-timestamp-show-all t
33306645
CD
837 "Non-nil means, show all occurrences of a repeating stamp in the agenda.
838When nil, only one occurrence is shown, either today or the
20908596
CD
839nearest into the future."
840 :group 'org-agenda-daily/weekly
841 :type 'boolean)
842
843(defcustom org-scheduled-past-days 10000
844 "No. of days to continue listing scheduled items that are not marked DONE.
845When an item is scheduled on a date, it shows up in the agenda on this
846day and will be listed until it is marked done for the number of days
847given here."
848 :group 'org-agenda-daily/weekly
c8d0cf5c 849 :type 'integer)
20908596 850
93b62de8
CD
851(defcustom org-agenda-log-mode-items '(closed clock)
852 "List of items that should be shown in agenda log mode.
853This list may contain the following symbols:
854
855 closed Show entries that have been closed on that day.
856 clock Show entries that have received clocked time on that day.
c8d0cf5c
CD
857 state Show all logged state changes.
858Note that instead of changing this variable, you can also press `C-u l' in
859the agenda to display all available LOG items temporarily."
93b62de8
CD
860 :group 'org-agenda-daily/weekly
861 :type '(set :greedy t (const closed) (const clock) (const state)))
862
c8d0cf5c
CD
863(defcustom org-agenda-log-mode-add-notes t
864 "Non-nil means, add first line of notes to log entries in agenda views.
865If a log item like a state change or a clock entry is associated with
866notes, the first line of these notes will be added to the entry in the
867agenda display."
868 :group 'org-agenda-daily/weekly
869 :type 'boolean)
870
871(defcustom org-agenda-start-with-log-mode nil
872 "The initial value of log-mode in a newly created agenda window."
873 :group 'org-agenda-startup
874 :group 'org-agenda-daily/weekly
875 :type 'boolean)
876
20908596
CD
877(defcustom org-agenda-start-with-clockreport-mode nil
878 "The initial value of clockreport-mode in a newly created agenda window."
879 :group 'org-agenda-startup
880 :group 'org-agenda-daily/weekly
881 :type 'boolean)
882
883(defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
884 "Property list with parameters for the clocktable in clockreport mode.
885This is the display mode that shows a clock table in the daily/weekly
886agenda, the properties for this dynamic block can be set here.
887The usual clocktable parameters are allowed here, but you cannot set
888the properties :name, :tstart, :tend, :block, and :scope - these will
889be overwritten to make sure the content accurately reflects the
890current display in the agenda."
891 :group 'org-agenda-daily/weekly
892 :type 'plist)
893
8bfe682a
CD
894(defcustom org-agenda-search-view-search-words-only nil
895 "Non-nil means, the search string is interpreted as individual words
896The search then looks for each word separately in each entry and
897selects entries that have matches for all words.
898When nil, matching as loose words will only take place if the first
899word is preceded by + or -. If that is not the case, the search
900string will just be matched as a substring in the entry, but with
901each space character allowing for any whitespace, including newlines."
902 :group 'org-agenda-search-view
903 :type 'boolean)
20908596
CD
904
905(defgroup org-agenda-time-grid nil
906 "Options concerning the time grid in the Org-mode Agenda."
907 :tag "Org Agenda Time Grid"
908 :group 'org-agenda)
909
c8d0cf5c
CD
910(defcustom org-agenda-search-headline-for-time t
911 "Non-nil means, search headline for a time-of-day.
912If the headline contains a time-of-day in one format or another, it will
913be used to sort the entry into the time sequence of items for a day.
914Some people have time stamps in the headline that refer to the creation
915time or so, and then this produces an unwanted side effect. If this is
916the case for your, use this variable to turn off searching the headline
917for a time."
918 :group 'org-agenda-time-grid
919 :type 'boolean)
920
20908596
CD
921(defcustom org-agenda-use-time-grid t
922 "Non-nil means, show a time grid in the agenda schedule.
923A time grid is a set of lines for specific times (like every two hours between
9248:00 and 20:00). The items scheduled for a day at specific times are
925sorted in between these lines.
926For details about when the grid will be shown, and what it will look like, see
927the variable `org-agenda-time-grid'."
928 :group 'org-agenda-time-grid
929 :type 'boolean)
930
931(defcustom org-agenda-time-grid
932 '((daily today require-timed)
933 "----------------"
934 (800 1000 1200 1400 1600 1800 2000))
935
936 "The settings for time grid for agenda display.
937This is a list of three items. The first item is again a list. It contains
938symbols specifying conditions when the grid should be displayed:
939
940 daily if the agenda shows a single day
941 weekly if the agenda shows an entire week
942 today show grid on current date, independent of daily/weekly display
943 require-timed show grid only if at least one item has a time specification
944
b349f79f 945The second item is a string which will be placed behind the grid time.
20908596
CD
946
947The third item is a list of integers, indicating the times that should have
948a grid line."
949 :group 'org-agenda-time-grid
950 :type
951 '(list
952 (set :greedy t :tag "Grid Display Options"
953 (const :tag "Show grid in single day agenda display" daily)
954 (const :tag "Show grid in weekly agenda display" weekly)
955 (const :tag "Always show grid for today" today)
956 (const :tag "Show grid only if any timed entries are present"
957 require-timed)
958 (const :tag "Skip grid times already present in an entry"
959 remove-match))
960 (string :tag "Grid String")
961 (repeat :tag "Grid Times" (integer :tag "Time"))))
962
963(defgroup org-agenda-sorting nil
964 "Options concerning sorting in the Org-mode Agenda."
965 :tag "Org Agenda Sorting"
966 :group 'org-agenda)
967
968(defcustom org-agenda-sorting-strategy
8bfe682a
CD
969 '((agenda habit-down time-up priority-down category-keep)
970 (todo priority-down category-keep)
971 (tags priority-down category-keep)
20908596
CD
972 (search category-keep))
973 "Sorting structure for the agenda items of a single day.
974This is a list of symbols which will be used in sequence to determine
975if an entry should be listed before another entry. The following
976symbols are recognized:
977
c8d0cf5c
CD
978time-up Put entries with time-of-day indications first, early first
979time-down Put entries with time-of-day indications first, late first
980category-keep Keep the default order of categories, corresponding to the
981 sequence in `org-agenda-files'.
982category-up Sort alphabetically by category, A-Z.
983category-down Sort alphabetically by category, Z-A.
984tag-up Sort alphabetically by last tag, A-Z.
985tag-down Sort alphabetically by last tag, Z-A.
986priority-up Sort numerically by priority, high priority last.
987priority-down Sort numerically by priority, high priority first.
988todo-state-up Sort by todo state, tasks that are done last.
989todo-state-down Sort by todo state, tasks that are done first.
990effort-up Sort numerically by estimated effort, high effort last.
991effort-down Sort numerically by estimated effort, high effort first.
992user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
993user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
8bfe682a
CD
994habit-up Put entries that are habits first
995habit-down Put entries that are habits last
20908596
CD
996
997The different possibilities will be tried in sequence, and testing stops
998if one comparison returns a \"not-equal\". For example, the default
999 '(time-up category-keep priority-down)
1000means: Pull out all entries having a specified time of day and sort them,
1001in order to make a time schedule for the current day the first thing in the
1002agenda listing for the day. Of the entries without a time indication, keep
1003the grouped in categories, don't sort the categories, but keep them in
1004the sequence given in `org-agenda-files'. Within each category sort by
1005priority.
1006
1007Leaving out `category-keep' would mean that items will be sorted across
1008categories by priority.
1009
1010Instead of a single list, this can also be a set of list for specific
1011contents, with a context symbol in the car of the list, any of
8bfe682a 1012`agenda', `todo', `tags', `search' for the corresponding agenda views.
c8d0cf5c
CD
1013
1014Custom commands can bind this variable in the options section."
20908596
CD
1015 :group 'org-agenda-sorting
1016 :type `(choice
1017 (repeat :tag "General" ,org-sorting-choice)
1018 (list :tag "Individually"
1019 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1020 (repeat ,org-sorting-choice))
1021 (cons (const :tag "Strategy for TODO lists" todo)
1022 (repeat ,org-sorting-choice))
1023 (cons (const :tag "Strategy for Tags matches" tags)
8bfe682a
CD
1024 (repeat ,org-sorting-choice))
1025 (cons (const :tag "Strategy for search matches" search)
20908596
CD
1026 (repeat ,org-sorting-choice)))))
1027
c8d0cf5c
CD
1028(defcustom org-agenda-cmp-user-defined nil
1029 "A function to define the comparison `user-defined'.
1030This function must receive two arguments, agenda entry a and b.
1031If a>b, return +1. If a<b, return -1. If they are equal as seen by
1032the user comparison, return nil.
1033When this is defined, you can make `user-defined-up' and `user-defined-down'
1034part of an agenda sorting strategy."
1035 :group 'org-agenda-sorting
1036 :type 'symbol)
1037
20908596
CD
1038(defcustom org-sort-agenda-notime-is-late t
1039 "Non-nil means, items without time are considered late.
1040This is only relevant for sorting. When t, items which have no explicit
1041time like 15:30 will be considered as 99:01, i.e. later than any items which
1042do have a time. When nil, the default time is before 0:00. You can use this
1043option to decide if the schedule for today should come before or after timeless
1044agenda entries."
1045 :group 'org-agenda-sorting
1046 :type 'boolean)
1047
1048(defcustom org-sort-agenda-noeffort-is-high t
1049 "Non-nil means, items without effort estimate are sorted as high effort.
c8d0cf5c
CD
1050This also applies when filtering an agenda view with respect to the
1051< or > effort operator. Then, tasks with no effort defined will be treated
1052as tasks with high effort.
20908596
CD
1053When nil, such items are sorted as 0 minutes effort."
1054 :group 'org-agenda-sorting
1055 :type 'boolean)
1056
1057(defgroup org-agenda-line-format nil
1058 "Options concerning the entry prefix in the Org-mode agenda display."
1059 :tag "Org Agenda Line Format"
1060 :group 'org-agenda)
1061
1062(defcustom org-agenda-prefix-format
1063 '((agenda . " %-12:c%?-12t% s")
1064 (timeline . " % s")
1065 (todo . " %-12:c")
1066 (tags . " %-12:c")
1067 (search . " %-12:c"))
1068 "Format specifications for the prefix of items in the agenda views.
1069An alist with four entries, for the different agenda types. The keys to the
1070sublists are `agenda', `timeline', `todo', and `tags'. The values
1071are format strings.
1072This format works similar to a printf format, with the following meaning:
1073
1074 %c the category of the item, \"Diary\" for entries from the diary, or
1075 as given by the CATEGORY keyword or derived from the file name.
1076 %T the *last* tag of the item. Last because inherited tags come
1077 first in the list.
1078 %t the time-of-day specification if one applies to the entry, in the
1079 format HH:MM
1080 %s Scheduling/Deadline information, a short string
1081
1082All specifiers work basically like the standard `%s' of printf, but may
1083contain two additional characters: A question mark just after the `%' and
1084a whitespace/punctuation character just before the final letter.
1085
1086If the first character after `%' is a question mark, the entire field
1087will only be included if the corresponding value applies to the
1088current entry. This is useful for fields which should have fixed
1089width when present, but zero width when absent. For example,
1090\"%?-12t\" will result in a 12 character time field if a time of the
1091day is specified, but will completely disappear in entries which do
1092not contain a time.
1093
1094If there is punctuation or whitespace character just before the final
1095format letter, this character will be appended to the field value if
1096the value is not empty. For example, the format \"%-12:c\" leads to
1097\"Diary: \" if the category is \"Diary\". If the category were be
8bfe682a 1098empty, no additional colon would be inserted.
20908596
CD
1099
1100The default value of this option is \" %-12:c%?-12t% s\", meaning:
1101- Indent the line with two space characters
1102- Give the category in a 12 chars wide field, padded with whitespace on
1103 the right (because of `-'). Append a colon if there is a category
1104 (because of `:').
1105- If there is a time-of-day, put it into a 12 chars wide field. If no
1106 time, don't put in an empty field, just skip it (because of '?').
1107- Finally, put the scheduling information and append a whitespace.
1108
1109As another example, if you don't want the time-of-day of entries in
1110the prefix, you could use:
1111
1112 (setq org-agenda-prefix-format \" %-11:c% s\")
1113
1114See also the variables `org-agenda-remove-times-when-in-prefix' and
c8d0cf5c
CD
1115`org-agenda-remove-tags'.
1116
1117Custom commands can set this variable in the options section."
20908596
CD
1118 :type '(choice
1119 (string :tag "General format")
1120 (list :greedy t :tag "View dependent"
1121 (cons (const agenda) (string :tag "Format"))
1122 (cons (const timeline) (string :tag "Format"))
1123 (cons (const todo) (string :tag "Format"))
1124 (cons (const tags) (string :tag "Format"))
1125 (cons (const search) (string :tag "Format"))))
1126 :group 'org-agenda-line-format)
1127
1128(defvar org-prefix-format-compiled nil
1129 "The compiled version of the most recently used prefix format.
1130See the variable `org-agenda-prefix-format'.")
1131
1132(defcustom org-agenda-todo-keyword-format "%-1s"
1133 "Format for the TODO keyword in agenda lines.
1134Set this to something like \"%-12s\" if you want all TODO keywords
1135to occupy a fixed space in the agenda display."
1136 :group 'org-agenda-line-format
1137 :type 'string)
1138
ce4fdcb9
CD
1139(defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1140 "Text preceding timerange entries in the agenda view.
1141This is a list with two strings. The first applies when the range
1142is entirely on one day. The second applies if the range spans several days.
1143The strings may have two \"%d\" format specifiers which will be filled
1144with the sequence number of the days, and the total number of days in the
1145range, respectively."
1146 :group 'org-agenda-line-format
1147 :type '(list
1148 (string :tag "Deadline today ")
1149 (choice :tag "Deadline relative"
1150 (string :tag "Format string")
1151 (function))))
1152
20908596
CD
1153(defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1154 "Text preceeding scheduled items in the agenda view.
1155This is a list with two strings. The first applies when the item is
1156scheduled on the current day. The second applies when it has been scheduled
b349f79f
CD
1157previously, it may contain a %d indicating that this is the nth time that
1158this item is scheduled, due to automatic rescheduling of unfinished items
1159for the following day. So this number is one larger than the number of days
1160that passed since this item was scheduled first."
20908596
CD
1161 :group 'org-agenda-line-format
1162 :type '(list
1163 (string :tag "Scheduled today ")
1164 (string :tag "Scheduled previously")))
1165
1166(defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1167 "Text preceeding deadline items in the agenda view.
1168This is a list with two strings. The first applies when the item has its
1169deadline on the current day. The second applies when it is in the past or
1170in the future, it may contain %d to capture how many days away the deadline
1171is (was)."
1172 :group 'org-agenda-line-format
1173 :type '(list
1174 (string :tag "Deadline today ")
1175 (choice :tag "Deadline relative"
1176 (string :tag "Format string")
1177 (function))))
1178
1179(defcustom org-agenda-remove-times-when-in-prefix t
1180 "Non-nil means, remove duplicate time specifications in agenda items.
1181When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1182time-of-day specification in a headline or diary entry is extracted and
1183placed into the prefix. If this option is non-nil, the original specification
1184\(a timestamp or -range, or just a plain time(range) specification like
118511:30-4pm) will be removed for agenda display. This makes the agenda less
1186cluttered.
1187The option can be t or nil. It may also be the symbol `beg', indicating
1188that the time should only be removed what it is located at the beginning of
1189the headline/diary entry."
1190 :group 'org-agenda-line-format
1191 :type '(choice
1192 (const :tag "Always" t)
1193 (const :tag "Never" nil)
1194 (const :tag "When at beginning of entry" beg)))
1195
1196
1197(defcustom org-agenda-default-appointment-duration nil
1198 "Default duration for appointments that only have a starting time.
1199When nil, no duration is specified in such cases.
1200When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1201 :group 'org-agenda-line-format
1202 :type '(choice
1203 (integer :tag "Minutes")
1204 (const :tag "No default duration")))
1205
ff4be292
CD
1206(defcustom org-agenda-show-inherited-tags t
1207 "Non-nil means, show inherited tags in each agenda line."
1208 :group 'org-agenda-line-format
1209 :type 'boolean)
20908596 1210
5dec9555
CD
1211(defcustom org-agenda-hide-tags-regexp nil
1212 "Regular expression used to filter away specific tags in agenda views.
1213This means that these tags will be present, but not be shown in the agenda
1214line. Secondayt filltering will still work on the hidden tags.
1215Nil means don't hide any tags."
1216 :group 'org-agenda-line-format
1217 :type '(choice
1218 (const :tag "Hide none" nil)
1219 (string :tag "Regexp ")))
1220
20908596
CD
1221(defcustom org-agenda-remove-tags nil
1222 "Non-nil means, remove the tags from the headline copy in the agenda.
1223When this is the symbol `prefix', only remove tags when
1224`org-agenda-prefix-format' contains a `%T' specifier."
1225 :group 'org-agenda-line-format
1226 :type '(choice
1227 (const :tag "Always" t)
1228 (const :tag "Never" nil)
1229 (const :tag "When prefix format contains %T" prefix)))
1230
1231(if (fboundp 'defvaralias)
1232 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1233 'org-agenda-remove-tags))
1234
5ace2fe5 1235(defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
20908596
CD
1236 "Shift tags in agenda items to this column.
1237If this number is positive, it specifies the column. If it is negative,
1238it means that the tags should be flushright to that column. For example,
1239-80 works well for a normal 80 character screen."
1240 :group 'org-agenda-line-format
1241 :type 'integer)
1242
1243(if (fboundp 'defvaralias)
1244 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1245
c8d0cf5c 1246(defcustom org-agenda-fontify-priorities 'cookies
20908596
CD
1247 "Non-nil means, highlight low and high priorities in agenda.
1248When t, the highest priority entries are bold, lowest priority italic.
c8d0cf5c
CD
1249However, settings in org-priority-faces will overrule these faces.
1250When this variable is the symbol `cookies', only fontify the
1251cookies, not the entire task.
621f83e4
CD
1252This may also be an association list of priority faces, whose
1253keys are the character values of `org-highest-priority',
1254`org-default-priority', and `org-lowest-priority' (the default values
c8d0cf5c 1255are ?A, ?B, and ?C, respectively). The face may be a named face,
621f83e4 1256or a list like `(:background \"Red\")'."
20908596
CD
1257 :group 'org-agenda-line-format
1258 :type '(choice
1259 (const :tag "Never" nil)
1260 (const :tag "Defaults" t)
c8d0cf5c 1261 (const :tag "Cookies only" cookies)
20908596
CD
1262 (repeat :tag "Specify"
1263 (list (character :tag "Priority" :value ?A)
1264 (sexp :tag "face")))))
1265
20908596
CD
1266(defgroup org-agenda-column-view nil
1267 "Options concerning column view in the agenda."
1268 :tag "Org Agenda Column View"
1269 :group 'org-agenda)
1270
1271(defcustom org-agenda-columns-show-summaries t
1272 "Non-nil means, show summaries for columns displayed in the agenda view."
1273 :group 'org-agenda-column-view
1274 :type 'boolean)
1275
b349f79f
CD
1276(defcustom org-agenda-columns-remove-prefix-from-item t
1277 "Non-nil means, remove the prefix from a headline for agenda column view.
1278The special ITEM field in the columns format contains the current line, with
1279all information shown in other columns (like the TODO state or a tag).
1280When this variable is non-nil, also the agenda prefix will be removed from
1281the content of the ITEM field, to make sure as much as possible of the
1282headline can be shown in the limited width of the field."
1283 :group 'org-agenda
1284 :type 'boolean)
1285
20908596
CD
1286(defcustom org-agenda-columns-compute-summary-properties t
1287 "Non-nil means, recompute all summary properties before column view.
1288When column view in the agenda is listing properties that have a summary
1289operator, it can go to all relevant buffers and recompute the summaries
1290there. This can mean overhead for the agenda column view, but is necessary
1291to have thing up to date.
1292As a special case, a CLOCKSUM property also makes sure that the clock
1293computations are current."
1294 :group 'org-agenda-column-view
1295 :type 'boolean)
1296
1297(defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1298 "Non-nil means, the duration of an appointment will add to day effort.
1299The property to which appointment durations will be added is the one given
1300in the option `org-effort-property'. If an appointment does not have
1301an end time, `org-agenda-default-appointment-duration' will be used. If that
1302is not set, an appointment without end time will not contribute to the time
1303estimate."
1304 :group 'org-agenda-column-view
1305 :type 'boolean)
1306
8bfe682a
CD
1307(defcustom org-agenda-auto-exclude-function nil
1308 "A function called with a tag to decide if it is filtered on '/ RET'.
1309The sole argument to the function, which is called once for each
1310possible tag, is a string giving the name of the tag. The
1311function should return either nil if the tag should be included
1312as normal, or \"-<TAG>\" to exclude the tag."
1313 :group 'org-agenda
1314 :type 'function)
1315
20908596
CD
1316(eval-when-compile
1317 (require 'cl))
1318(require 'org)
1319
1320(defun org-add-agenda-custom-command (entry)
1321 "Replace or add a command in `org-agenda-custom-commands'.
1322This is mostly for hacking and trying a new command - once the command
1323works you probably want to add it to `org-agenda-custom-commands' for good."
1324 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1325 (if ass
1326 (setcdr ass (cdr entry))
1327 (push entry org-agenda-custom-commands))))
1328
1329;;; Define the Org-agenda-mode
1330
1331(defvar org-agenda-mode-map (make-sparse-keymap)
1332 "Keymap for `org-agenda-mode'.")
8bfe682a
CD
1333(if (fboundp 'defvaralias)
1334 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
20908596
CD
1335
1336(defvar org-agenda-menu) ; defined later in this file.
c8d0cf5c 1337(defvar org-agenda-restrict) ; defined later in this file.
20908596 1338(defvar org-agenda-follow-mode nil)
54a0dee5 1339(defvar org-agenda-entry-text-mode nil)
20908596
CD
1340(defvar org-agenda-clockreport-mode nil)
1341(defvar org-agenda-show-log nil)
1342(defvar org-agenda-redo-command nil)
1343(defvar org-agenda-query-string nil)
0bd48b37
CD
1344(defvar org-agenda-mode-hook nil
1345 "Hook for org-agenda-mode, run after the mode is turned on.")
20908596
CD
1346(defvar org-agenda-type nil)
1347(defvar org-agenda-force-single-file nil)
c8d0cf5c 1348(defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
20908596
CD
1349
1350(defun org-agenda-mode ()
1351 "Mode for time-sorted view on action items in Org-mode files.
1352
1353The following commands are available:
1354
1355\\{org-agenda-mode-map}"
1356 (interactive)
1357 (kill-all-local-variables)
1358 (setq org-agenda-undo-list nil
c8d0cf5c
CD
1359 org-agenda-pending-undo-list nil
1360 org-agenda-bulk-marked-entries nil)
20908596
CD
1361 (setq major-mode 'org-agenda-mode)
1362 ;; Keep global-font-lock-mode from turning on font-lock-mode
1363 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1364 (setq mode-name "Org-Agenda")
1365 (use-local-map org-agenda-mode-map)
1366 (easy-menu-add org-agenda-menu)
1367 (if org-startup-truncated (setq truncate-lines t))
54a0dee5 1368 (org-set-local 'line-move-visual nil)
20908596
CD
1369 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1370 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1371 ;; Make sure properties are removed when copying text
1372 (when (boundp 'buffer-substring-filters)
1373 (org-set-local 'buffer-substring-filters
1374 (cons (lambda (x)
1375 (set-text-properties 0 (length x) nil x) x)
1376 buffer-substring-filters)))
1377 (unless org-agenda-keep-modes
1378 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
54a0dee5 1379 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
20908596 1380 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
c8d0cf5c
CD
1381 org-agenda-show-log org-agenda-start-with-log-mode))
1382
20908596
CD
1383 (easy-menu-change
1384 '("Agenda") "Agenda Files"
1385 (append
1386 (list
1387 (vector
1388 (if (get 'org-agenda-files 'org-restrict)
1389 "Restricted to single file"
1390 "Edit File List")
1391 '(org-edit-agenda-file-list)
1392 (not (get 'org-agenda-files 'org-restrict)))
1393 "--")
1394 (mapcar 'org-file-menu-entry (org-agenda-files))))
1395 (org-agenda-set-mode-name)
1396 (apply
1397 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1398 (list 'org-agenda-mode-hook)))
1399
1400(substitute-key-definition 'undo 'org-agenda-undo
1401 org-agenda-mode-map global-map)
1402(org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1403(org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1404(org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1405(org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
c8d0cf5c
CD
1406(org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1407(org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1408(org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1409(org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1410(org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1411(org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
8bfe682a
CD
1412(org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1413(org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1414(org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
54a0dee5 1415(org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
8bfe682a 1416(org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
20908596 1417(org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
20908596 1418(org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
8bfe682a
CD
1419(org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1420(org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1421(org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
20908596
CD
1422(org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1423(org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1424(org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
20908596
CD
1425(org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1426(org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
54a0dee5 1427(org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
20908596 1428(org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
8bfe682a 1429(org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
20908596 1430(org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
71d35b24 1431(org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
20908596
CD
1432(org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1433(org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1434(org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1435(org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
20908596
CD
1436(org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1437(org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1438(org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
b349f79f
CD
1439(org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1440(org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
c8d0cf5c
CD
1441(org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1442(org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1443(org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1444(org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
20908596
CD
1445
1446(org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1447(org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1448(org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1449(let ((l '(1 2 3 4 5 6 7 8 9 0)))
1450 (while l (org-defkey org-agenda-mode-map
1451 (int-to-string (pop l)) 'digit-argument)))
1452
54a0dee5 1453(org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
20908596 1454(org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
54a0dee5 1455(org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
20908596 1456(org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
c8d0cf5c 1457(org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
20908596
CD
1458(org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1459(org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1460(org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1461(org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
54a0dee5
CD
1462(org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1463(org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1464(org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1465 'org-clock-modify-effort-estimate)
1466(org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
20908596
CD
1467(org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1468(org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1469(org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
20908596 1470(org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
c8d0cf5c 1471(org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
20908596
CD
1472(org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1473(org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
8bfe682a
CD
1474(org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1475(org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1476(substitute-key-definition 'next-line 'org-agenda-next-line
1477 org-agenda-mode-map global-map)
1478(substitute-key-definition 'previous-line 'org-agenda-previous-line
1479 org-agenda-mode-map global-map)
621f83e4 1480(org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
20908596
CD
1481(org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1482(org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1483(org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1484(org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1485(org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1486(org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1487(org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1488(org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1489(org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1490(org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1491(org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1492(org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1493(org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1494(org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1495(org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1496(org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1497(org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1498(org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1499(org-defkey org-agenda-mode-map "J" 'org-clock-goto)
1500(org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1501(org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1502(org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1503(org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1504(org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1505(org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
54a0dee5
CD
1506(org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1507(org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
20908596 1508(org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
c8d0cf5c 1509(org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
20908596
CD
1510
1511(org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1512(org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1513(org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1514(org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
621f83e4 1515(org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
71d35b24 1516(org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
c8d0cf5c 1517(org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
8d642074
CD
1518(define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1519(org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1520(org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
20908596 1521
8bfe682a 1522(org-defkey org-agenda-mode-map
20908596 1523 (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
8bfe682a 1524(org-defkey org-agenda-mode-map
20908596
CD
1525 (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
1526(when org-agenda-mouse-1-follows-link
8bfe682a 1527 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
20908596
CD
1528(easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1529 '("Agenda"
1530 ("Agenda Files")
1531 "--"
8d642074
CD
1532 ("Agenda Dates"
1533 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1534 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1535 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1536 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1537 "--"
1538 ("View"
1539 ["Day View" org-agenda-day-view
1540 :active (org-agenda-check-type nil 'agenda)
1541 :style radio :selected (equal org-agenda-ndays 1)
1542 :keys "v d (or just d)"]
1543 ["Week View" org-agenda-week-view
1544 :active (org-agenda-check-type nil 'agenda)
1545 :style radio :selected (equal org-agenda-ndays 7)
1546 :keys "v w (or just w)"]
1547 ["Month View" org-agenda-month-view
1548 :active (org-agenda-check-type nil 'agenda)
1549 :style radio :selected (member org-agenda-ndays '(28 29 30 31))
1550 :keys "v m"]
1551 ["Year View" org-agenda-year-view
1552 :active (org-agenda-check-type nil 'agenda)
1553 :style radio :selected (member org-agenda-ndays '(365 366))
1554 :keys "v y"]
1555 "--"
1556 ["Include Diary" org-agenda-toggle-diary
1557 :style toggle :selected org-agenda-include-diary
1558 :active (org-agenda-check-type nil 'agenda)]
1559 ["Use Time Grid" org-agenda-toggle-time-grid
1560 :style toggle :selected org-agenda-use-time-grid
1561 :active (org-agenda-check-type nil 'agenda)]
1562 "--"
1563 ["Show clock report" org-agenda-clockreport-mode
1564 :style toggle :selected org-agenda-clockreport-mode
1565 :active (org-agenda-check-type nil 'agenda)]
1566 ["Show some entry text" org-agenda-entry-text-mode
1567 :style toggle :selected org-agenda-entry-text-mode
1568 :active t]
1569 "--"
1570 ["Show Logbook entries" org-agenda-log-mode
1571 :style toggle :selected org-agenda-show-log
1572 :active (org-agenda-check-type nil 'agenda 'timeline)
1573 :keys "v l (or just l)"]
1574 ["Include archived trees" org-agenda-archives-mode
1575 :style toggle :selected org-agenda-archives-mode :active t
1576 :keys "v a"]
1577 ["Include archive files" (org-agenda-archives-mode t)
1578 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1579 :keys "v A"]
1580 "--"
1581 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1582 ["Write view to file" org-write-agenda t]
1583 ["Rebuild buffer" org-agenda-redo t]
1584 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1585 "--"
1586 ["Show original entry" org-agenda-show t]
20908596
CD
1587 ["Go To (other window)" org-agenda-goto t]
1588 ["Go To (this window)" org-agenda-switch-to t]
1589 ["Follow Mode" org-agenda-follow-mode
1590 :style toggle :selected org-agenda-follow-mode :active t]
8d642074 1591; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
20908596 1592 "--"
8d642074
CD
1593 ("TODO"
1594 ["Cycle TODO" org-agenda-todo t]
1595 ["Next TODO set" org-agenda-todo-nextset t]
1596 ["Previous TODO set" org-agenda-todo-previousset t]
1597 ["Add note" org-agenda-add-note t])
1598 ("Archive/Refile/Delete"
8bfe682a
CD
1599 ["Archive default" org-agenda-archive-default t]
1600 ["Archive default" org-agenda-archive-default-with-confirmation t]
20908596
CD
1601 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1602 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
c8d0cf5c 1603 ["Archive subtree" org-agenda-archive t]
8d642074
CD
1604 "--"
1605 ["Refile" org-agenda-refile t]
1606 "--"
1607 ["Delete subtree" org-agenda-kill t])
c8d0cf5c 1608 ("Bulk action"
8d642074
CD
1609 ["Mark entry" org-agenda-bulk-mark t]
1610 ["Unmark entry" org-agenda-bulk-unmark t]
c8d0cf5c
CD
1611 ["Act on all marked" org-agenda-bulk-action t]
1612 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
1613 "--"
20908596
CD
1614 ("Tags and Properties"
1615 ["Show all Tags" org-agenda-show-tags t]
1616 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1617 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1618 "--"
1619 ["Column View" org-columns t])
8d642074 1620 ("Deadline/Schedule"
20908596
CD
1621 ["Schedule" org-agenda-schedule t]
1622 ["Set Deadline" org-agenda-deadline t]
1623 "--"
b349f79f
CD
1624 ["Mark item" org-agenda-action :active t :keys "k m"]
1625 ["Show mark item" org-agenda-action :active t :keys "k v"]
1626 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1627 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1628 "--"
20908596
CD
1629 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1630 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
c8d0cf5c
CD
1631 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
1632 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
1633 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
1634 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
20908596 1635 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
54a0dee5 1636 ("Clock and Effort"
20908596
CD
1637 ["Clock in" org-agenda-clock-in t]
1638 ["Clock out" org-agenda-clock-out t]
1639 ["Clock cancel" org-agenda-clock-cancel t]
54a0dee5
CD
1640 ["Goto running clock" org-clock-goto t]
1641 "--"
1642 ["Set Effort" org-agenda-set-effort t]
1643 ["Change clocked effort" org-clock-modify-effort-estimate
1644 (org-clock-is-active)])
20908596
CD
1645 ("Priority"
1646 ["Set Priority" org-agenda-priority t]
1647 ["Increase Priority" org-agenda-priority-up t]
1648 ["Decrease Priority" org-agenda-priority-down t]
1649 ["Show Priority" org-agenda-show-priority t])
1650 ("Calendar/Diary"
1651 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1652 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1653 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1654 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1655 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1656 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1657 "--"
8d642074 1658 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
20908596 1659 "--"
8d642074 1660 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2c3ad40d 1661 "--"
8d642074
CD
1662 ("MobileOrg"
1663 ["Push Files and Views" org-mobile-push t]
1664 ["Get Captured and Flagged" org-mobile-pull t]
1665 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
1666 ["Show note / unflag" org-agenda-show-the-flagging-note t]
c8d0cf5c 1667 "--"
8d642074 1668 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
20908596
CD
1669 "--"
1670 ["Quit" org-agenda-quit t]
1671 ["Exit and Release Buffers" org-agenda-exit t]
1672 ))
1673
1674;;; Agenda undo
1675
1676(defvar org-agenda-allow-remote-undo t
1677 "Non-nil means, allow remote undo from the agenda buffer.")
1678(defvar org-agenda-undo-list nil
1679 "List of undoable operations in the agenda since last refresh.")
1680(defvar org-agenda-undo-has-started-in nil
1681 "Buffers that have already seen `undo-start' in the current undo sequence.")
1682(defvar org-agenda-pending-undo-list nil
33306645 1683 "In a series of undo commands, this is the list of remaining undo items.")
20908596
CD
1684
1685
1686(defun org-agenda-undo ()
1687 "Undo a remote editing step in the agenda.
1688This undoes changes both in the agenda buffer and in the remote buffer
1689that have been changed along."
1690 (interactive)
1691 (or org-agenda-allow-remote-undo
f924a367 1692 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
20908596
CD
1693 (if (not (eq this-command last-command))
1694 (setq org-agenda-undo-has-started-in nil
1695 org-agenda-pending-undo-list org-agenda-undo-list))
1696 (if (not org-agenda-pending-undo-list)
1697 (error "No further undo information"))
1698 (let* ((entry (pop org-agenda-pending-undo-list))
1699 buf line cmd rembuf)
1700 (setq cmd (pop entry) line (pop entry))
1701 (setq rembuf (nth 2 entry))
1702 (org-with-remote-undo rembuf
1703 (while (bufferp (setq buf (pop entry)))
1704 (if (pop entry)
1705 (with-current-buffer buf
1706 (let ((last-undo-buffer buf)
1707 (inhibit-read-only t))
1708 (unless (memq buf org-agenda-undo-has-started-in)
1709 (push buf org-agenda-undo-has-started-in)
1710 (make-local-variable 'pending-undo-list)
1711 (undo-start))
1712 (while (and pending-undo-list
1713 (listp pending-undo-list)
1714 (not (car pending-undo-list)))
1715 (pop pending-undo-list))
1716 (undo-more 1))))))
54a0dee5 1717 (org-goto-line line)
20908596
CD
1718 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
1719
1720(defun org-verify-change-for-undo (l1 l2)
1721 "Verify that a real change occurred between the undo lists L1 and L2."
1722 (while (and l1 (listp l1) (null (car l1))) (pop l1))
1723 (while (and l2 (listp l2) (null (car l2))) (pop l2))
1724 (not (eq l1 l2)))
1725
1726;;; Agenda dispatch
1727
1728(defvar org-agenda-restrict nil)
1729(defvar org-agenda-restrict-begin (make-marker))
1730(defvar org-agenda-restrict-end (make-marker))
1731(defvar org-agenda-last-dispatch-buffer nil)
1732(defvar org-agenda-overriding-restriction nil)
1733
1734;;;###autoload
c8d0cf5c 1735(defun org-agenda (&optional arg keys restriction)
20908596
CD
1736 "Dispatch agenda commands to collect entries to the agenda buffer.
1737Prompts for a command to execute. Any prefix arg will be passed
1738on to the selected command. The default selections are:
1739
1740a Call `org-agenda-list' to display the agenda for current day or week.
1741t Call `org-todo-list' to display the global todo list.
1742T Call `org-todo-list' to display the global todo list, select only
1743 entries with a specific TODO keyword (the user gets a prompt).
1744m Call `org-tags-view' to display headlines with tags matching
1745 a condition (the user is prompted for the condition).
1746M Like `m', but select only TODO entries, no ordinary headlines.
1747L Create a timeline for the current buffer.
1748e Export views to associated files.
c8d0cf5c 1749s Search entries for keywords.
8bfe682a 1750/ Multi occur across all agenda files and also files listed
c8d0cf5c
CD
1751 in `org-agenda-text-search-extra-files'.
1752< Restrict agenda commands to buffer, subtree, or region.
1753 Press several times to get the desired effect.
1754> Remove a previous restriction.
1755# List \"stuck\" projects.
1756! Configure what \"stuck\" means.
1757C Configure custom agenda commands.
20908596
CD
1758
1759More commands can be added by configuring the variable
1760`org-agenda-custom-commands'. In particular, specific tags and TODO keyword
1761searches can be pre-defined in this way.
1762
1763If the current buffer is in Org-mode and visiting a file, you can also
1764first press `<' once to indicate that the agenda should be temporarily
1765\(until the next use of \\[org-agenda]) restricted to the current file.
1766Pressing `<' twice means to restrict to the current subtree or region
1767\(if active)."
1768 (interactive "P")
1769 (catch 'exit
1770 (let* ((prefix-descriptions nil)
54a0dee5
CD
1771 (org-agenda-window-setup (if (equal (buffer-name)
1772 org-agenda-buffer-name)
1773 'current-window
1774 org-agenda-window-setup))
20908596
CD
1775 (org-agenda-custom-commands-orig org-agenda-custom-commands)
1776 (org-agenda-custom-commands
1777 ;; normalize different versions
1778 (delq nil
1779 (mapcar
1780 (lambda (x)
1781 (cond ((stringp (cdr x))
1782 (push x prefix-descriptions)
1783 nil)
1784 ((stringp (nth 1 x)) x)
1785 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1786 (t (cons (car x) (cons "" (cdr x))))))
1787 org-agenda-custom-commands)))
1788 (buf (current-buffer))
1789 (bfn (buffer-file-name (buffer-base-buffer)))
1790 entry key type match lprops ans)
8d642074 1791 ;; Turn off restriction unless there is an overriding one,
20908596 1792 (unless org-agenda-overriding-restriction
8bfe682a 1793 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
8d642074
CD
1794 ;; There is a request to keep the file list in place
1795 (put 'org-agenda-files 'org-restrict nil))
20908596
CD
1796 (setq org-agenda-restrict nil)
1797 (move-marker org-agenda-restrict-begin nil)
1798 (move-marker org-agenda-restrict-end nil))
1799 ;; Delete old local properties
1800 (put 'org-agenda-redo-command 'org-lprops nil)
1801 ;; Remember where this call originated
1802 (setq org-agenda-last-dispatch-buffer (current-buffer))
1803 (unless keys
1804 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
1805 keys (car ans)
1806 restriction (cdr ans)))
8bfe682a 1807 ;; Establish the restriction, if any
20908596
CD
1808 (when (and (not org-agenda-overriding-restriction) restriction)
1809 (put 'org-agenda-files 'org-restrict (list bfn))
1810 (cond
1811 ((eq restriction 'region)
1812 (setq org-agenda-restrict t)
1813 (move-marker org-agenda-restrict-begin (region-beginning))
1814 (move-marker org-agenda-restrict-end (region-end)))
1815 ((eq restriction 'subtree)
1816 (save-excursion
1817 (setq org-agenda-restrict t)
1818 (org-back-to-heading t)
1819 (move-marker org-agenda-restrict-begin (point))
1820 (move-marker org-agenda-restrict-end
1821 (progn (org-end-of-subtree t)))))))
1822
1823 (require 'calendar) ; FIXME: can we avoid this for some commands?
1824 ;; For example the todo list should not need it (but does...)
1825 (cond
1826 ((setq entry (assoc keys org-agenda-custom-commands))
1827 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
1828 (progn
8bfe682a
CD
1829 (setq type (nth 2 entry) match (eval (nth 3 entry))
1830 lprops (nth 4 entry))
20908596
CD
1831 (put 'org-agenda-redo-command 'org-lprops lprops)
1832 (cond
1833 ((eq type 'agenda)
1834 (org-let lprops '(org-agenda-list current-prefix-arg)))
1835 ((eq type 'alltodo)
1836 (org-let lprops '(org-todo-list current-prefix-arg)))
1837 ((eq type 'search)
1838 (org-let lprops '(org-search-view current-prefix-arg match nil)))
1839 ((eq type 'stuck)
1840 (org-let lprops '(org-agenda-list-stuck-projects
1841 current-prefix-arg)))
1842 ((eq type 'tags)
1843 (org-let lprops '(org-tags-view current-prefix-arg match)))
1844 ((eq type 'tags-todo)
1845 (org-let lprops '(org-tags-view '(4) match)))
1846 ((eq type 'todo)
1847 (org-let lprops '(org-todo-list match)))
1848 ((eq type 'tags-tree)
1849 (org-check-for-org-mode)
c8d0cf5c 1850 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
20908596
CD
1851 ((eq type 'todo-tree)
1852 (org-check-for-org-mode)
1853 (org-let lprops
1854 '(org-occur (concat "^" outline-regexp "[ \t]*"
1855 (regexp-quote match) "\\>"))))
1856 ((eq type 'occur-tree)
1857 (org-check-for-org-mode)
1858 (org-let lprops '(org-occur match)))
1859 ((functionp type)
1860 (org-let lprops '(funcall type match)))
1861 ((fboundp type)
1862 (org-let lprops '(funcall type match)))
1863 (t (error "Invalid custom agenda command type %s" type))))
1864 (org-run-agenda-series (nth 1 entry) (cddr entry))))
1865 ((equal keys "C")
1866 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
1867 (customize-variable 'org-agenda-custom-commands))
1868 ((equal keys "a") (call-interactively 'org-agenda-list))
1869 ((equal keys "s") (call-interactively 'org-search-view))
1870 ((equal keys "t") (call-interactively 'org-todo-list))
1871 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
1872 ((equal keys "m") (call-interactively 'org-tags-view))
1873 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
1874 ((equal keys "e") (call-interactively 'org-store-agenda-views))
8d642074
CD
1875 ((equal keys "?") (org-tags-view nil "+FLAGGED")
1876 (org-add-hook
1877 'post-command-hook
1878 (lambda ()
1879 (unless (current-message)
1880 (let* ((m (org-agenda-get-any-marker))
1881 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
1882 (when note
1883 (message (concat
1884 "FLAGGING-NOTE ([?] for more info): "
1885 (org-add-props
1886 (replace-regexp-in-string
1887 "\\\\n" "//"
1888 (copy-sequence note))
1889 nil 'face 'org-warning)))))))
1890 t t))
20908596
CD
1891 ((equal keys "L")
1892 (unless (org-mode-p)
1893 (error "This is not an Org-mode file"))
1894 (unless restriction
1895 (put 'org-agenda-files 'org-restrict (list bfn))
1896 (org-call-with-arg 'org-timeline arg)))
1897 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
1898 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
1899 ((equal keys "!") (customize-variable 'org-stuck-projects))
1900 (t (error "Invalid agenda key"))))))
1901
1902(defun org-agenda-normalize-custom-commands (cmds)
1903 (delq nil
1904 (mapcar
1905 (lambda (x)
1906 (cond ((stringp (cdr x)) nil)
1907 ((stringp (nth 1 x)) x)
1908 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1909 (t (cons (car x) (cons "" (cdr x))))))
1910 cmds)))
1911
1912(defun org-agenda-get-restriction-and-command (prefix-descriptions)
1913 "The user interface for selecting an agenda command."
1914 (catch 'exit
1915 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
1916 (restrict-ok (and bfn (org-mode-p)))
1917 (region-p (org-region-active-p))
1918 (custom org-agenda-custom-commands)
1919 (selstring "")
1920 restriction second-time
1921 c entry key type match prefixes rmheader header-end custom1 desc)
1922 (save-window-excursion
1923 (delete-other-windows)
1924 (org-switch-to-buffer-other-window " *Agenda Commands*")
1925 (erase-buffer)
1926 (insert (eval-when-compile
1927 (let ((header
1928"
2c3ad40d 1929Press key for an agenda command: < Buffer, subtree/region restriction
20908596
CD
1930-------------------------------- > Remove restriction
1931a Agenda for current week or day e Export agenda views
1932t List of all TODO entries T Entries with special TODO kwd
621f83e4 1933m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
20908596
CD
1934L Timeline for current buffer # List stuck projects (!=configure)
1935s Search for keywords C Configure custom agenda commands
8d642074 1936/ Multi-occur ? Find :FLAGGED: entries
20908596
CD
1937")
1938 (start 0))
1939 (while (string-match
1940 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
1941 header start)
1942 (setq start (match-end 0))
1943 (add-text-properties (match-beginning 2) (match-end 2)
1944 '(face bold) header))
1945 header)))
1946 (setq header-end (move-marker (make-marker) (point)))
1947 (while t
1948 (setq custom1 custom)
1949 (when (eq rmheader t)
54a0dee5 1950 (org-goto-line 1)
20908596
CD
1951 (re-search-forward ":" nil t)
1952 (delete-region (match-end 0) (point-at-eol))
1953 (forward-char 1)
1954 (looking-at "-+")
1955 (delete-region (match-end 0) (point-at-eol))
1956 (move-marker header-end (match-end 0)))
1957 (goto-char header-end)
1958 (delete-region (point) (point-max))
1959 (while (setq entry (pop custom1))
1960 (setq key (car entry) desc (nth 1 entry)
54a0dee5
CD
1961 type (nth 2 entry)
1962 match (nth 3 entry))
20908596
CD
1963 (if (> (length key) 1)
1964 (add-to-list 'prefixes (string-to-char key))
1965 (insert
1966 (format
1967 "\n%-4s%-14s: %s"
1968 (org-add-props (copy-sequence key)
1969 '(face bold))
1970 (cond
1971 ((string-match "\\S-" desc) desc)
1972 ((eq type 'agenda) "Agenda for current week or day")
1973 ((eq type 'alltodo) "List of all TODO entries")
1974 ((eq type 'search) "Word search")
1975 ((eq type 'stuck) "List of stuck projects")
1976 ((eq type 'todo) "TODO keyword")
1977 ((eq type 'tags) "Tags query")
1978 ((eq type 'tags-todo) "Tags (TODO)")
1979 ((eq type 'tags-tree) "Tags tree")
1980 ((eq type 'todo-tree) "TODO kwd tree")
1981 ((eq type 'occur-tree) "Occur tree")
1982 ((functionp type) (if (symbolp type)
1983 (symbol-name type)
1984 "Lambda expression"))
1985 (t "???"))
1986 (cond
1987 ((stringp match)
54a0dee5 1988 (setq match (copy-sequence match))
20908596
CD
1989 (org-add-props match nil 'face 'org-warning))
1990 (match
1991 (format "set of %d commands" (length match)))
1992 (t ""))))))
1993 (when prefixes
1994 (mapc (lambda (x)
1995 (insert
1996 (format "\n%s %s"
1997 (org-add-props (char-to-string x)
1998 nil 'face 'bold)
1999 (or (cdr (assoc (concat selstring (char-to-string x))
2000 prefix-descriptions))
2001 "Prefix key"))))
2002 prefixes))
2003 (goto-char (point-min))
93b62de8
CD
2004 (if second-time
2005 (if (not (pos-visible-in-window-p (point-max)))
2006 (org-fit-window-to-buffer))
2007 (setq second-time t)
2008 (org-fit-window-to-buffer))
20908596
CD
2009 (message "Press key for agenda command%s:"
2010 (if (or restrict-ok org-agenda-overriding-restriction)
2011 (if org-agenda-overriding-restriction
2012 " (restriction lock active)"
2013 (if restriction
2014 (format " (restricted to %s)" restriction)
2015 " (unrestricted)"))
2016 ""))
2017 (setq c (read-char-exclusive))
2018 (message "")
2019 (cond
2020 ((assoc (char-to-string c) custom)
2021 (setq selstring (concat selstring (char-to-string c)))
2022 (throw 'exit (cons selstring restriction)))
2023 ((memq c prefixes)
2024 (setq selstring (concat selstring (char-to-string c))
2025 prefixes nil
2026 rmheader (or rmheader t)
2027 custom (delq nil (mapcar
2028 (lambda (x)
2029 (if (or (= (length (car x)) 1)
2030 (/= (string-to-char (car x)) c))
2031 nil
2032 (cons (substring (car x) 1) (cdr x))))
2033 custom))))
2034 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2035 (message "Restriction is only possible in Org-mode buffers")
2036 (ding) (sit-for 1))
2037 ((eq c ?1)
2038 (org-agenda-remove-restriction-lock 'noupdate)
2039 (setq restriction 'buffer))
2040 ((eq c ?0)
2041 (org-agenda-remove-restriction-lock 'noupdate)
2042 (setq restriction (if region-p 'region 'subtree)))
2043 ((eq c ?<)
2044 (org-agenda-remove-restriction-lock 'noupdate)
2045 (setq restriction
2046 (cond
2047 ((eq restriction 'buffer)
2048 (if region-p 'region 'subtree))
2049 ((memq restriction '(subtree region))
2050 nil)
2051 (t 'buffer))))
2052 ((eq c ?>)
2053 (org-agenda-remove-restriction-lock 'noupdate)
2054 (setq restriction nil))
8d642074 2055 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
20908596
CD
2056 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2057 ((and (> (length selstring) 0) (eq c ?\d))
2058 (delete-window)
2059 (org-agenda-get-restriction-and-command prefix-descriptions))
2060
2061 ((equal c ?q) (error "Abort"))
2062 (t (error "Invalid key %c" c))))))))
2063
2064(defun org-run-agenda-series (name series)
c8d0cf5c 2065 (org-let (nth 1 series) '(org-prepare-agenda name))
20908596
CD
2066 (let* ((org-agenda-multi t)
2067 (redo (list 'org-run-agenda-series name (list 'quote series)))
2068 (cmds (car series))
2069 (gprops (nth 1 series))
2070 match ;; The byte compiler incorrectly complains about this. Keep it!
2071 cmd type lprops)
2072 (while (setq cmd (pop cmds))
8bfe682a 2073 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
20908596
CD
2074 (cond
2075 ((eq type 'agenda)
2076 (org-let2 gprops lprops
2077 '(call-interactively 'org-agenda-list)))
2078 ((eq type 'alltodo)
2079 (org-let2 gprops lprops
2080 '(call-interactively 'org-todo-list)))
2081 ((eq type 'search)
2082 (org-let2 gprops lprops
2083 '(org-search-view current-prefix-arg match nil)))
2084 ((eq type 'stuck)
2085 (org-let2 gprops lprops
2086 '(call-interactively 'org-agenda-list-stuck-projects)))
2087 ((eq type 'tags)
2088 (org-let2 gprops lprops
2089 '(org-tags-view current-prefix-arg match)))
2090 ((eq type 'tags-todo)
2091 (org-let2 gprops lprops
2092 '(org-tags-view '(4) match)))
2093 ((eq type 'todo)
2094 (org-let2 gprops lprops
2095 '(org-todo-list match)))
2096 ((fboundp type)
2097 (org-let2 gprops lprops
2098 '(funcall type match)))
2099 (t (error "Invalid type in command series"))))
2100 (widen)
2101 (setq org-agenda-redo-command redo)
2102 (goto-char (point-min)))
c8d0cf5c 2103 (org-fit-agenda-window)
0bd48b37 2104 (org-let (nth 1 series) '(org-finalize-agenda)))
20908596
CD
2105
2106;;;###autoload
2107(defmacro org-batch-agenda (cmd-key &rest parameters)
2108 "Run an agenda command in batch mode and send the result to STDOUT.
2109If CMD-KEY is a string of length 1, it is used as a key in
2110`org-agenda-custom-commands' and triggers this command. If it is a
2111longer string it is used as a tags/todo match string.
2112Paramters are alternating variable names and values that will be bound
2113before running the agenda command."
2114 (let (pars)
2115 (while parameters
2116 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2117 (if (> (length cmd-key) 2)
2118 (eval (list 'let (nreverse pars)
2119 (list 'org-tags-view nil cmd-key)))
2120 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2121 (set-buffer org-agenda-buffer-name)
2122 (princ (org-encode-for-stdout (buffer-string)))))
2123
8bfe682a
CD
2124;(defun org-encode-for-stdout (string)
2125; (if (fboundp 'encode-coding-string)
2126; (encode-coding-string string buffer-file-coding-system)
2127; string))
2128
20908596 2129(defun org-encode-for-stdout (string)
8bfe682a 2130 string)
20908596
CD
2131
2132(defvar org-agenda-info nil)
2133
2134;;;###autoload
2135(defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2136 "Run an agenda command in batch mode and send the result to STDOUT.
2137If CMD-KEY is a string of length 1, it is used as a key in
2138`org-agenda-custom-commands' and triggers this command. If it is a
2139longer string it is used as a tags/todo match string.
2140Paramters are alternating variable names and values that will be bound
2141before running the agenda command.
2142
2143The output gives a line for each selected agenda item. Each
2144item is a list of comma-separated values, like this:
2145
2146category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2147
2148category The category of the item
2149head The headline, without TODO kwd, TAGS and PRIORITY
2150type The type of the agenda entry, can be
2151 todo selected in TODO match
2152 tagsmatch selected in tags match
2153 diary imported from diary
2154 deadline a deadline on given date
2155 scheduled scheduled on given date
2156 timestamp entry has timestamp on given date
2157 closed entry was closed on given date
2158 upcoming-deadline warning about deadline
2159 past-scheduled forwarded scheduled item
2160 block entry has date block including g. date
2161todo The todo keyword, if any
2162tags All tags including inherited ones, separated by colons
2163date The relevant date, like 2007-2-14
2164time The time, like 15:00-16:50
2165extra Sting with extra planning info
2166priority-l The priority letter if any was given
2167priority-n The computed numerical priority
2168agenda-day The day in the agenda where this is listed"
2169
2170 (let (pars)
2171 (while parameters
2172 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2173 (push (list 'org-agenda-remove-tags t) pars)
2174 (if (> (length cmd-key) 2)
2175 (eval (list 'let (nreverse pars)
2176 (list 'org-tags-view nil cmd-key)))
2177 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2178 (set-buffer org-agenda-buffer-name)
2179 (let* ((lines (org-split-string (buffer-string) "\n"))
2180 line)
2181 (while (setq line (pop lines))
2182 (catch 'next
2183 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2184 (setq org-agenda-info
2185 (org-fix-agenda-info (text-properties-at 0 line)))
2186 (princ
2187 (org-encode-for-stdout
2188 (mapconcat 'org-agenda-export-csv-mapper
2189 '(org-category txt type todo tags date time-of-day extra
2190 priority-letter priority agenda-day)
2191 ",")))
2192 (princ "\n"))))))
2193
2194(defun org-fix-agenda-info (props)
2195 "Make sure all properties on an agenda item have a canonical form,
2196so the export commands can easily use it."
2197 (let (tmp re)
2198 (when (setq tmp (plist-get props 'tags))
2199 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2200 (when (setq tmp (plist-get props 'date))
2201 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2202 (let ((calendar-date-display-form '(year "-" month "-" day)))
2203 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2204
2205 (setq tmp (calendar-date-string tmp)))
2206 (setq props (plist-put props 'date tmp)))
2207 (when (setq tmp (plist-get props 'day))
2208 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2209 (let ((calendar-date-display-form '(year "-" month "-" day)))
2210 (setq tmp (calendar-date-string tmp)))
2211 (setq props (plist-put props 'day tmp))
2212 (setq props (plist-put props 'agenda-day tmp)))
2213 (when (setq tmp (plist-get props 'txt))
2214 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2215 (plist-put props 'priority-letter (match-string 1 tmp))
2216 (setq tmp (replace-match "" t t tmp)))
2217 (when (and (setq re (plist-get props 'org-todo-regexp))
2218 (setq re (concat "\\`\\.*" re " ?"))
2219 (string-match re tmp))
2220 (plist-put props 'todo (match-string 1 tmp))
2221 (setq tmp (replace-match "" t t tmp)))
2222 (plist-put props 'txt tmp)))
2223 props)
2224
2225(defun org-agenda-export-csv-mapper (prop)
2226 (let ((res (plist-get org-agenda-info prop)))
2227 (setq res
2228 (cond
2229 ((not res) "")
2230 ((stringp res) res)
2231 (t (prin1-to-string res))))
2232 (while (string-match "," res)
2233 (setq res (replace-match ";" t t res)))
2234 (org-trim res)))
2235
2236
2237;;;###autoload
2238(defun org-store-agenda-views (&rest parameters)
2239 (interactive)
2240 (eval (list 'org-batch-store-agenda-views)))
2241
2242;; FIXME, why is this a macro?????
2243;;;###autoload
2244(defmacro org-batch-store-agenda-views (&rest parameters)
2245 "Run all custom agenda commands that have a file argument."
2246 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2247 (pop-up-frames nil)
2248 (dir default-directory)
2c3ad40d 2249 pars cmd thiscmdkey files opts cmd-or-set)
20908596
CD
2250 (while parameters
2251 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2252 (setq pars (reverse pars))
2253 (save-window-excursion
2254 (while cmds
2255 (setq cmd (pop cmds)
2256 thiscmdkey (car cmd)
2c3ad40d
CD
2257 cmd-or-set (nth 2 cmd)
2258 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2259 files (nth (if (listp cmd-or-set) 4 5) cmd))
20908596
CD
2260 (if (stringp files) (setq files (list files)))
2261 (when files
2262 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2263 (list 'org-agenda nil thiscmdkey)))
2264 (set-buffer org-agenda-buffer-name)
2265 (while files
2266 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2267 (list 'org-write-agenda
c8d0cf5c 2268 (expand-file-name (pop files) dir) nil t))))
20908596
CD
2269 (and (get-buffer org-agenda-buffer-name)
2270 (kill-buffer org-agenda-buffer-name)))))))
2271
8d642074
CD
2272(defun org-agenda-mark-header-line (pos)
2273 "Mark the line at POS as an agenda structure header."
2274 (save-excursion
2275 (goto-char pos)
2276 (put-text-property (point-at-bol) (point-at-eol)
2277 'org-agenda-structural-header t)
2278 (when org-agenda-title-append
2279 (put-text-property (point-at-bol) (point-at-eol)
2280 'org-agenda-title-append org-agenda-title-append))))
2281
8bfe682a 2282(defvar org-mobile-creating-agendas)
c8d0cf5c 2283(defun org-write-agenda (file &optional open nosettings)
20908596
CD
2284 "Write the current buffer (an agenda view) as a file.
2285Depending on the extension of the file name, plain text (.txt),
2286HTML (.html or .htm) or Postscript (.ps) is produced.
2287If the extension is .ics, run icalendar export over all files used
2288to construct the agenda and limit the export to entries listed in the
2289agenda now.
8bfe682a 2290With prefix argument OPEN, open the new file immediately.
20908596
CD
2291If NOSETTINGS is given, do not scope the settings of
2292`org-agenda-exporter-settings' into the export commands. This is used when
2293the settings have already been scoped and we do not wish to overrule other,
2294higher priority settings."
c8d0cf5c 2295 (interactive "FWrite agenda to file: \nP")
20908596
CD
2296 (if (not (file-writable-p file))
2297 (error "Cannot write agenda to file %s" file))
2298 (cond
2299 ((string-match "\\.html?\\'" file) (require 'htmlize))
2300 ((string-match "\\.ps\\'" file) (require 'ps-print)))
2301 (org-let (if nosettings nil org-agenda-exporter-settings)
2302 '(save-excursion
2303 (save-window-excursion
93b62de8 2304 (org-agenda-mark-filtered-text)
8bfe682a 2305 (let ((bs (copy-sequence (buffer-string))) beg)
93b62de8
CD
2306 (org-agenda-unmark-filtered-text)
2307 (with-temp-buffer
20908596 2308 (insert bs)
93b62de8
CD
2309 (org-agenda-remove-marked-text 'org-filtered)
2310 (while (setq beg (text-property-any (point-min) (point-max)
2311 'org-filtered t))
2312 (delete-region
2313 beg (or (next-single-property-change beg 'org-filtered)
2314 (point-max))))
c8d0cf5c 2315 (run-hooks 'org-agenda-before-write-hook)
93b62de8 2316 (cond
8bfe682a
CD
2317 ((org-bound-and-true-p org-mobile-creating-agendas)
2318 (org-mobile-write-agenda-for-mobile file))
93b62de8
CD
2319 ((string-match "\\.html?\\'" file)
2320 (set-buffer (htmlize-buffer (current-buffer)))
ff4be292 2321
93b62de8
CD
2322 (when (and org-agenda-export-html-style
2323 (string-match "<style>" org-agenda-export-html-style))
2324 ;; replace <style> section with org-agenda-export-html-style
2325 (goto-char (point-min))
2326 (kill-region (- (search-forward "<style") 6)
2327 (search-forward "</style>"))
2328 (insert org-agenda-export-html-style))
2329 (write-file file)
2330 (kill-buffer (current-buffer))
2331 (message "HTML written to %s" file))
2332 ((string-match "\\.ps\\'" file)
c8d0cf5c
CD
2333 (require 'ps-print)
2334 (flet ((ps-get-buffer-name () "Agenda View"))
2335 (ps-print-buffer-with-faces file))
93b62de8 2336 (message "Postscript written to %s" file))
c8d0cf5c
CD
2337 ((string-match "\\.pdf\\'" file)
2338 (require 'ps-print)
2339 (flet ((ps-get-buffer-name () "Agenda View"))
2340 (ps-print-buffer-with-faces
2341 (concat (file-name-sans-extension file) ".ps")))
2342 (call-process "ps2pdf" nil nil nil
2343 (expand-file-name
2344 (concat (file-name-sans-extension file) ".ps"))
2345 (expand-file-name file))
2346 (message "PDF written to %s" file))
93b62de8 2347 ((string-match "\\.ics\\'" file)
c8d0cf5c 2348 (require 'org-icalendar)
93b62de8
CD
2349 (let ((org-agenda-marker-table
2350 (org-create-marker-find-array
2351 (org-agenda-collect-markers)))
2352 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2353 (org-combined-agenda-icalendar-file file))
2354 (apply 'org-export-icalendar 'combine
2355 (org-agenda-files nil 'ifmode))))
2356 (t
2357 (let ((bs (buffer-string)))
2358 (find-file file)
2359 (erase-buffer)
2360 (insert bs)
2361 (save-buffer 0)
2362 (kill-buffer (current-buffer))
2363 (message "Plain text written to %s" file))))))))
c8d0cf5c
CD
2364 (set-buffer org-agenda-buffer-name))
2365 (when open (org-open-file file)))
2366
93b62de8
CD
2367(defvar org-agenda-filter-overlays nil)
2368
2369(defun org-agenda-mark-filtered-text ()
2370 "Mark all text hidden by filtering with a text property."
2371 (let ((inhibit-read-only t))
2372 (mapc
2373 (lambda (o)
2374 (when (equal (org-overlay-buffer o) (current-buffer))
2375 (put-text-property
2376 (org-overlay-start o) (org-overlay-end o)
2377 'org-filtered t)))
2378 org-agenda-filter-overlays)))
2379
2380(defun org-agenda-unmark-filtered-text ()
2381 "Remove the filtering text property."
2382 (let ((inhibit-read-only t))
2383 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2384
2385(defun org-agenda-remove-marked-text (property &optional value)
2386 "Delete all text marked with VALUE of PROPERTY.
2387VALUE defaults to t."
2388 (let (beg)
2389 (setq value (or value t))
2390 (while (setq beg (text-property-any (point-min) (point-max)
2391 property value))
2392 (delete-region
2393 beg (or (next-single-property-change beg 'org-filtered)
2394 (point-max))))))
20908596 2395
c8d0cf5c
CD
2396(defun org-agenda-add-entry-text ()
2397 "Add entry text to agenda lines.
2398This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2399entry text following headings shown in the agenda.
2400Drawers will be excluded, also the line with scheduling/deadline info."
8bfe682a
CD
2401 (when (and (> org-agenda-add-entry-text-maxlines 0)
2402 (not (org-bound-and-true-p org-mobile-creating-agendas)))
54a0dee5 2403 (let (m txt)
c8d0cf5c
CD
2404 (goto-char (point-min))
2405 (while (not (eobp))
8d642074 2406 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
c8d0cf5c 2407 (beginning-of-line 2)
54a0dee5 2408 (setq txt (org-agenda-get-some-entry-text
8d642074 2409 m org-agenda-add-entry-text-maxlines " > "))
c8d0cf5c
CD
2410 (end-of-line 1)
2411 (if (string-match "\\S-" txt) (insert "\n" txt)))))))
2412
8d642074
CD
2413(defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2414 &rest keep)
54a0dee5 2415 "Extract entry text from MARKER, at most N-LINES lines.
8d642074
CD
2416This will ignore drawers etc, just get the text.
2417If INDENT is given, prefix every line with this string. If KEEP is
8bfe682a 2418given, it is a list of symbols, defining stuff that should not be
8d642074 2419removed from the entry content. Currently only `planning' is allowed here."
54a0dee5
CD
2420 (let (txt drawer-re kwd-time-re ind)
2421 (save-excursion
2422 (with-current-buffer (marker-buffer marker)
2423 (if (not (org-mode-p))
2424 (setq txt "")
2425 (save-excursion
2426 (save-restriction
2427 (widen)
2428 (goto-char marker)
8d642074 2429 (end-of-line 1)
54a0dee5 2430 (setq txt (buffer-substring
8d642074 2431 (min (1+ (point)) (point-max))
54a0dee5
CD
2432 (progn (outline-next-heading) (point)))
2433 drawer-re org-drawer-regexp
2434 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2435 ".*\n?"))
2436 (with-temp-buffer
2437 (insert txt)
2438 (when org-agenda-add-entry-text-descriptive-links
2439 (goto-char (point-min))
2440 (while (org-activate-bracket-links (point-max))
2441 (add-text-properties (match-beginning 0) (match-end 0)
2442 '(face org-link))))
2443 (goto-char (point-min))
2444 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2445 (set-text-properties (match-beginning 0) (match-end 0)
2446 nil))
2447 (goto-char (point-min))
2448 (while (re-search-forward drawer-re nil t)
2449 (delete-region
2450 (match-beginning 0)
2451 (progn (re-search-forward
2452 "^[ \t]*:END:.*\n?" nil 'move)
2453 (point))))
8d642074
CD
2454 (unless (member 'planning keep)
2455 (goto-char (point-min))
2456 (while (re-search-forward kwd-time-re nil t)
2457 (replace-match "")))
54a0dee5 2458 (goto-char (point-min))
8d642074
CD
2459 (when org-agenda-entry-text-exclude-regexps
2460 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2461 (while (setq re (pop re-list))
2462 (goto-char (point-min))
2463 (while (re-search-forward re nil t)
2464 (replace-match "")))))
2465 (goto-char (point-max))
2466 (skip-chars-backward " \t\n")
2467 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2468
2469 ;; find and remove min common indentation
54a0dee5
CD
2470 (goto-char (point-min))
2471 (untabify (point-min) (point-max))
2472 (setq ind (org-get-indentation))
2473 (while (not (eobp))
2474 (unless (looking-at "[ \t]*$")
2475 (setq ind (min ind (org-get-indentation))))
2476 (beginning-of-line 2))
2477 (goto-char (point-min))
2478 (while (not (eobp))
2479 (unless (looking-at "[ \t]*$")
2480 (move-to-column ind)
2481 (delete-region (point-at-bol) (point)))
2482 (beginning-of-line 2))
8d642074
CD
2483
2484 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2485
54a0dee5 2486 (goto-char (point-min))
8d642074
CD
2487 (when indent
2488 (while (and (not (eobp)) (re-search-forward "^" nil t))
2489 (replace-match indent t t)))
54a0dee5
CD
2490 (goto-char (point-min))
2491 (while (looking-at "[ \t]*\n") (replace-match ""))
2492 (goto-char (point-max))
2493 (when (> (org-current-line)
2494 n-lines)
2495 (org-goto-line (1+ n-lines))
2496 (backward-char 1))
2497 (setq txt (buffer-substring (point-min) (point)))))))))
2498 txt))
2499
20908596
CD
2500(defun org-agenda-collect-markers ()
2501 "Collect the markers pointing to entries in the agenda buffer."
2502 (let (m markers)
2503 (save-excursion
2504 (goto-char (point-min))
2505 (while (not (eobp))
8d642074
CD
2506 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2507 (org-get-at-bol 'org-marker)))
20908596
CD
2508 (push m markers))
2509 (beginning-of-line 2)))
2510 (nreverse markers)))
2511
2512(defun org-create-marker-find-array (marker-list)
2513 "Create a alist of files names with all marker positions in that file."
2514 (let (f tbl m a p)
2515 (while (setq m (pop marker-list))
2516 (setq p (marker-position m)
2517 f (buffer-file-name (or (buffer-base-buffer
2518 (marker-buffer m))
2519 (marker-buffer m))))
2520 (if (setq a (assoc f tbl))
2521 (push (marker-position m) (cdr a))
2522 (push (list f p) tbl)))
2523 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2524 tbl)))
2525
33306645 2526(defvar org-agenda-marker-table nil) ; dynamically scoped parameter
20908596
CD
2527(defun org-check-agenda-marker-table ()
2528 "Check of the current entry is on the marker list."
2529 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2530 a)
2531 (and (setq a (assoc file org-agenda-marker-table))
2532 (save-match-data
2533 (save-excursion
2534 (org-back-to-heading t)
2535 (member (point) (cdr a)))))))
2536
2537(defun org-check-for-org-mode ()
2538 "Make sure current buffer is in org-mode. Error if not."
2539 (or (org-mode-p)
f924a367 2540 (error "Cannot execute org-mode agenda command on buffer in %s"
20908596
CD
2541 major-mode)))
2542
2543(defun org-fit-agenda-window ()
2544 "Fit the window to the buffer size."
2545 (and (memq org-agenda-window-setup '(reorganize-frame))
2546 (fboundp 'fit-window-to-buffer)
93b62de8 2547 (org-fit-window-to-buffer
20908596
CD
2548 nil
2549 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2550 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2551
2552;;; Agenda prepare and finalize
2553
33306645 2554(defvar org-agenda-multi nil) ; dynamically scoped
20908596
CD
2555(defvar org-agenda-buffer-name "*Org Agenda*")
2556(defvar org-pre-agenda-window-conf nil)
2557(defvar org-agenda-columns-active nil)
2558(defvar org-agenda-name nil)
71d35b24 2559(defvar org-agenda-filter nil)
c8d0cf5c
CD
2560(defvar org-agenda-filter-preset nil
2561 "A preset of the tags filter used for secondary agenda filtering.
2562This must be a list of strings, each string must be a single tag preceeded
2563by \"+\" or \"-\".
2564This variable should not be set directly, but agenda custom commands can
2565bind it in the options section.")
2566
20908596
CD
2567(defun org-prepare-agenda (&optional name)
2568 (setq org-todo-keywords-for-agenda nil)
2569 (setq org-done-keywords-for-agenda nil)
8d642074 2570 (setq org-drawers-for-agenda nil)
71d35b24 2571 (setq org-agenda-filter nil)
c8d0cf5c 2572 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
20908596
CD
2573 (if org-agenda-multi
2574 (progn
2575 (setq buffer-read-only nil)
2576 (goto-char (point-max))
2577 (unless (or (bobp) org-agenda-compact-blocks)
0bd48b37
CD
2578 (insert "\n"
2579 (if (stringp org-agenda-block-separator)
2580 org-agenda-block-separator
2581 (make-string (window-width) org-agenda-block-separator))
2582 "\n"))
20908596
CD
2583 (narrow-to-region (point) (point-max)))
2584 (org-agenda-reset-markers)
2585 (setq org-agenda-contributing-files nil)
2586 (setq org-agenda-columns-active nil)
2c3ad40d 2587 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
20908596
CD
2588 (setq org-todo-keywords-for-agenda
2589 (org-uniquify org-todo-keywords-for-agenda))
2590 (setq org-done-keywords-for-agenda
2591 (org-uniquify org-done-keywords-for-agenda))
8d642074 2592 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
20908596
CD
2593 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2594 (awin (get-buffer-window abuf)))
2595 (cond
2596 ((equal (current-buffer) abuf) nil)
2597 (awin (select-window awin))
2598 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2599 ((equal org-agenda-window-setup 'current-window)
2600 (switch-to-buffer abuf))
2601 ((equal org-agenda-window-setup 'other-window)
2602 (org-switch-to-buffer-other-window abuf))
2603 ((equal org-agenda-window-setup 'other-frame)
8d642074 2604 (switch-to-buffer-other-frame abuf))
20908596
CD
2605 ((equal org-agenda-window-setup 'reorganize-frame)
2606 (delete-other-windows)
2607 (org-switch-to-buffer-other-window abuf))))
2608 (setq buffer-read-only nil)
2609 (let ((inhibit-read-only t)) (erase-buffer))
2610 (org-agenda-mode)
2611 (and name (not org-agenda-name)
2612 (org-set-local 'org-agenda-name name)))
2613 (setq buffer-read-only nil))
2614
2615(defun org-finalize-agenda ()
2616 "Finishing touch for the agenda buffer, called just before displaying it."
2617 (unless org-agenda-multi
2618 (save-excursion
2619 (let ((inhibit-read-only t))
2620 (goto-char (point-min))
2621 (while (org-activate-bracket-links (point-max))
2622 (add-text-properties (match-beginning 0) (match-end 0)
2623 '(face org-link)))
2624 (org-agenda-align-tags)
2625 (unless org-agenda-with-colors
2626 (remove-text-properties (point-min) (point-max) '(face nil))))
33306645
CD
2627 (if (and (boundp 'org-agenda-overriding-columns-format)
2628 org-agenda-overriding-columns-format)
2629 (org-set-local 'org-agenda-overriding-columns-format
2630 org-agenda-overriding-columns-format))
20908596
CD
2631 (if (and (boundp 'org-agenda-view-columns-initially)
2632 org-agenda-view-columns-initially)
2633 (org-agenda-columns))
2634 (when org-agenda-fontify-priorities
c8d0cf5c 2635 (org-agenda-fontify-priorities))
d6685abc
CD
2636 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2637 (org-agenda-dim-blocked-tasks))
54a0dee5
CD
2638 (org-agenda-mark-clocking-task)
2639 (when org-agenda-entry-text-mode
2640 (org-agenda-entry-text-hide)
f924a367 2641 (org-agenda-entry-text-show))
8bfe682a
CD
2642 (if (functionp 'org-habit-insert-consistency-graphs)
2643 (org-habit-insert-consistency-graphs))
20908596 2644 (run-hooks 'org-finalize-agenda-hook)
8d642074 2645 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
c8d0cf5c
CD
2646 (when (get 'org-agenda-filter :preset-filter)
2647 (org-agenda-filter-apply org-agenda-filter))
20908596
CD
2648 )))
2649
54a0dee5
CD
2650(defun org-agenda-mark-clocking-task ()
2651 "Mark the current clock entry in the agenda if it is present."
2652 (mapc (lambda (o)
2653 (if (eq (org-overlay-get o 'type) 'org-agenda-clocking)
2654 (org-delete-overlay o)))
2655 (org-overlays-in (point-min) (point-max)))
2656 (when (marker-buffer org-clock-hd-marker)
2657 (save-excursion
2658 (goto-char (point-min))
2659 (let (s ov)
2660 (while (setq s (next-single-property-change (point) 'org-hd-marker))
2661 (goto-char s)
8d642074 2662 (when (equal (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
2663 org-clock-hd-marker)
2664 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-eol))))
2665 (org-overlay-put ov 'type 'org-agenda-clocking)
2666 (org-overlay-put ov 'face 'org-agenda-clocking)
2667 (org-overlay-put ov 'help-echo
2668 "The clock is running in this item")))))))
2669
c8d0cf5c 2670(defun org-agenda-fontify-priorities ()
20908596
CD
2671 "Make highest priority lines bold, and lowest italic."
2672 (interactive)
2673 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
2674 (org-delete-overlay o)))
2675 (org-overlays-in (point-min) (point-max)))
2676 (save-excursion
2677 (let ((inhibit-read-only t)
2678 b e p ov h l)
2679 (goto-char (point-min))
2680 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
2681 (setq h (or (get-char-property (point) 'org-highest-priority)
2682 org-highest-priority)
2683 l (or (get-char-property (point) 'org-lowest-priority)
2684 org-lowest-priority)
2685 p (string-to-char (match-string 1))
c8d0cf5c
CD
2686 b (match-beginning 0)
2687 e (if (eq org-agenda-fontify-priorities 'cookies)
2688 (match-end 0)
2689 (point-at-eol))
20908596
CD
2690 ov (org-make-overlay b e))
2691 (org-overlay-put
2692 ov 'face
c8d0cf5c
CD
2693 (cond ((cdr (assoc p org-priority-faces)))
2694 ((and (listp org-agenda-fontify-priorities)
2695 (cdr (assoc p org-agenda-fontify-priorities))))
20908596
CD
2696 ((equal p l) 'italic)
2697 ((equal p h) 'bold)))
2698 (org-overlay-put ov 'org-type 'org-priority)))))
2699
d6685abc
CD
2700(defun org-agenda-dim-blocked-tasks ()
2701 "Dim currently blocked TODO's in the agenda display."
2702 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
2703 (org-delete-overlay o)))
2704 (org-overlays-in (point-min) (point-max)))
2705 (save-excursion
2706 (let ((inhibit-read-only t)
72d06d81 2707 (org-depend-tag-blocked nil)
d6685abc 2708 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
c8d0cf5c
CD
2709 org-blocked-by-checkboxes
2710 invis1 b e p ov h l)
d6685abc
CD
2711 (goto-char (point-min))
2712 (while (let ((pos (next-single-property-change (point) 'todo-state)))
2713 (and pos (goto-char (1+ pos))))
c8d0cf5c 2714 (setq org-blocked-by-checkboxes nil invis1 invis)
8d642074 2715 (let ((marker (org-get-at-bol 'org-hd-marker)))
d6685abc
CD
2716 (when (and marker
2717 (not (with-current-buffer (marker-buffer marker)
2718 (save-excursion
2719 (goto-char marker)
c8d0cf5c
CD
2720 (if (org-entry-get nil "NOBLOCKING")
2721 t ;; Never block this entry
2722 (run-hook-with-args-until-failure
2723 'org-blocker-hook
2724 (list :type 'todo-state-change
2725 :position marker
2726 :from 'todo
2727 :to 'done)))))))
2728 (if org-blocked-by-checkboxes (setq invis1 nil))
53e31a31
CD
2729 (setq b (if invis1
2730 (max (point-min) (1- (point-at-bol)))
2731 (point-at-bol))
d6685abc
CD
2732 e (point-at-eol)
2733 ov (org-make-overlay b e))
c8d0cf5c 2734 (if invis1
d6685abc
CD
2735 (org-overlay-put ov 'invisible t)
2736 (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
2737 (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
20908596
CD
2738
2739(defvar org-agenda-skip-function nil
2740 "Function to be called at each match during agenda construction.
2741If this function returns nil, the current match should not be skipped.
2742Otherwise, the function must return a position from where the search
2743should be continued.
2744This may also be a Lisp form, it will be evaluated.
2745Never set this variable using `setq' or so, because then it will apply
2746to all future agenda commands. Instead, bind it with `let' to scope
2747it dynamically into the agenda-constructing command. A good way to set
2748it is through options in org-agenda-custom-commands.")
2749
2750(defun org-agenda-skip ()
2751 "Throw to `:skip' in places that should be skipped.
2752Also moves point to the end of the skipped region, so that search can
2753continue from there."
2754 (let ((p (point-at-bol)) to fp)
2c3ad40d 2755 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
20908596
CD
2756 (get-text-property p :org-archived)
2757 (org-end-of-subtree t)
2758 (throw :skip t))
b349f79f
CD
2759 (and org-agenda-skip-comment-trees
2760 (get-text-property p :org-comment)
20908596
CD
2761 (org-end-of-subtree t)
2762 (throw :skip t))
2763 (if (equal (char-after p) ?#) (throw :skip t))
2764 (when (and (or (setq fp (functionp org-agenda-skip-function))
2765 (consp org-agenda-skip-function))
2766 (setq to (save-excursion
2767 (save-match-data
2768 (if fp
2769 (funcall org-agenda-skip-function)
2770 (eval org-agenda-skip-function))))))
2771 (goto-char to)
2772 (throw :skip t))))
2773
2774(defvar org-agenda-markers nil
2775 "List of all currently active markers created by `org-agenda'.")
54a0dee5 2776(defvar org-agenda-last-marker-time (org-float-time)
20908596
CD
2777 "Creation time of the last agenda marker.")
2778
2779(defun org-agenda-new-marker (&optional pos)
2780 "Return a new agenda marker.
2781Org-mode keeps a list of these markers and resets them when they are
2782no longer in use."
2783 (let ((m (copy-marker (or pos (point)))))
54a0dee5 2784 (setq org-agenda-last-marker-time (org-float-time))
20908596
CD
2785 (push m org-agenda-markers)
2786 m))
2787
2788(defun org-agenda-reset-markers ()
2789 "Reset markers created by `org-agenda'."
2790 (while org-agenda-markers
2791 (move-marker (pop org-agenda-markers) nil)))
2792
b349f79f
CD
2793(defun org-agenda-save-markers-for-cut-and-paste (beg end)
2794 "Save relative positions of markers in region."
2795 (mapc (lambda (m) (org-check-and-save-marker m beg end))
2796 org-agenda-markers))
2797
54a0dee5
CD
2798;;; Entry text mode
2799
2800(defun org-agenda-entry-text-show-here ()
8bfe682a 2801 "Add some text from the entry as context to the current line."
54a0dee5 2802 (let (m txt o)
8d642074 2803 (setq m (org-get-at-bol 'org-hd-marker))
54a0dee5
CD
2804 (unless (marker-buffer m)
2805 (error "No marker points to an entry here"))
2806 (setq txt (concat "\n" (org-no-properties
2807 (org-agenda-get-some-entry-text
8d642074 2808 m org-agenda-entry-text-maxlines " > "))))
54a0dee5
CD
2809 (when (string-match "\\S-" txt)
2810 (setq o (org-make-overlay (point-at-bol) (point-at-eol)))
2811 (org-overlay-put o 'evaporate t)
2812 (org-overlay-put o 'org-overlay-type 'agenda-entry-content)
2813 (org-overlay-put o 'after-string txt))))
2814
2815(defun org-agenda-entry-text-show ()
2816 "Add entry context for all agenda lines."
2817 (interactive)
2818 (save-excursion
2819 (goto-char (point-max))
2820 (beginning-of-line 1)
2821 (while (not (bobp))
8d642074 2822 (when (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
2823 (org-agenda-entry-text-show-here))
2824 (beginning-of-line 0))))
2825
2826(defun org-agenda-entry-text-hide ()
2827 "Remove any shown entry context."
2828 (delq nil
2829 (mapcar (lambda (o)
2830 (if (eq (org-overlay-get o 'org-overlay-type)
2831 'agenda-entry-content)
2832 (progn (org-delete-overlay o) t)))
2833 (org-overlays-in (point-min) (point-max)))))
2834
20908596
CD
2835;;; Agenda timeline
2836
2837(defvar org-agenda-only-exact-dates nil) ; dynamically scoped
2838
2839(defun org-timeline (&optional include-all)
2840 "Show a time-sorted view of the entries in the current org file.
2841Only entries with a time stamp of today or later will be listed. With
2842\\[universal-argument] prefix, all unfinished TODO items will also be shown,
2843under the current date.
2844If the buffer contains an active region, only check the region for
2845dates."
2846 (interactive "P")
2847 (require 'calendar)
2848 (org-compile-prefix-format 'timeline)
2849 (org-set-sorting-strategy 'timeline)
2850 (let* ((dopast t)
2851 (dotodo include-all)
2852 (doclosed org-agenda-show-log)
2853 (entry buffer-file-name)
2854 (date (calendar-current-date))
2855 (beg (if (org-region-active-p) (region-beginning) (point-min)))
2856 (end (if (org-region-active-p) (region-end) (point-max)))
2857 (day-numbers (org-get-all-dates beg end 'no-ranges
2858 t doclosed ; always include today
2859 org-timeline-show-empty-dates))
2860 (org-deadline-warning-days 0)
2861 (org-agenda-only-exact-dates t)
2862 (today (time-to-days (current-time)))
2863 (past t)
2864 args
2865 s e rtn d emptyp wd)
2866 (setq org-agenda-redo-command
2867 (list 'progn
2868 (list 'org-switch-to-buffer-other-window (current-buffer))
2869 (list 'org-timeline (list 'quote include-all))))
2870 (if (not dopast)
2871 ;; Remove past dates from the list of dates.
2872 (setq day-numbers (delq nil (mapcar (lambda(x)
2873 (if (>= x today) x nil))
2874 day-numbers))))
2875 (org-prepare-agenda (concat "Timeline "
2876 (file-name-nondirectory buffer-file-name)))
2877 (if doclosed (push :closed args))
2878 (push :timestamp args)
2879 (push :deadline args)
2880 (push :scheduled args)
2881 (push :sexp args)
2882 (if dotodo (push :todo args))
8d642074
CD
2883 (insert "Timeline of file " entry "\n")
2884 (add-text-properties (point-min) (point)
2885 (list 'face 'org-agenda-structure))
2886 (org-agenda-mark-header-line (point-min))
20908596
CD
2887 (while (setq d (pop day-numbers))
2888 (if (and (listp d) (eq (car d) :omitted))
2889 (progn
2890 (setq s (point))
2891 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
2892 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
2893 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
2894 (if (and (>= d today)
2895 dopast
2896 past)
2897 (progn
2898 (setq past nil)
2899 (insert (make-string 79 ?-) "\n")))
2900 (setq date (calendar-gregorian-from-absolute d)
2901 wd (calendar-day-of-week date))
2902 (setq s (point))
2903 (setq rtn (and (not emptyp)
2904 (apply 'org-agenda-get-day-entries entry
2905 date args)))
2906 (if (or rtn (equal d today) org-timeline-show-empty-dates)
2907 (progn
2908 (insert
2909 (if (stringp org-agenda-format-date)
2910 (format-time-string org-agenda-format-date
2911 (org-time-from-absolute date))
2912 (funcall org-agenda-format-date date))
2913 "\n")
2914 (put-text-property s (1- (point)) 'face
2915 (if (member wd org-agenda-weekend-days)
2916 'org-agenda-date-weekend
2917 'org-agenda-date))
2918 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 2919 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596
CD
2920 (if (equal d today)
2921 (put-text-property s (1- (point)) 'org-today t))
2922 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
2923 (put-text-property s (1- (point)) 'day d)))))
2924 (goto-char (point-min))
2925 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
2926 (point-min)))
2927 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
2928 (org-finalize-agenda)
2929 (setq buffer-read-only t)))
2930
2931(defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
2932 "Return a list of all relevant day numbers from BEG to END buffer positions.
2933If NO-RANGES is non-nil, include only the start and end dates of a range,
2934not every single day in the range. If FORCE-TODAY is non-nil, make
2935sure that TODAY is included in the list. If INACTIVE is non-nil, also
2936inactive time stamps (those in square brackets) are included.
2937When EMPTY is non-nil, also include days without any entries."
2938 (let ((re (concat
2939 (if pre-re pre-re "")
2940 (if inactive org-ts-regexp-both org-ts-regexp)))
2941 dates dates1 date day day1 day2 ts1 ts2)
2942 (if force-today
2943 (setq dates (list (time-to-days (current-time)))))
2944 (save-excursion
2945 (goto-char beg)
2946 (while (re-search-forward re end t)
2947 (setq day (time-to-days (org-time-string-to-time
2948 (substring (match-string 1) 0 10))))
2949 (or (memq day dates) (push day dates)))
2950 (unless no-ranges
2951 (goto-char beg)
2952 (while (re-search-forward org-tr-regexp end t)
2953 (setq ts1 (substring (match-string 1) 0 10)
2954 ts2 (substring (match-string 2) 0 10)
2955 day1 (time-to-days (org-time-string-to-time ts1))
2956 day2 (time-to-days (org-time-string-to-time ts2)))
2957 (while (< (setq day1 (1+ day1)) day2)
2958 (or (memq day1 dates) (push day1 dates)))))
2959 (setq dates (sort dates '<))
2960 (when empty
2961 (while (setq day (pop dates))
2962 (setq day2 (car dates))
2963 (push day dates1)
2964 (when (and day2 empty)
2965 (if (or (eq empty t)
2966 (and (numberp empty) (<= (- day2 day) empty)))
2967 (while (< (setq day (1+ day)) day2)
2968 (push (list day) dates1))
2969 (push (cons :omitted (- day2 day)) dates1))))
2970 (setq dates (nreverse dates1)))
2971 dates)))
2972
2973;;; Agenda Daily/Weekly
2974
2975(defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
c8d0cf5c
CD
2976(defvar org-agenda-start-day nil ; dynamically scoped parameter
2977"Custom commands can set this variable in the options section.")
20908596
CD
2978(defvar org-agenda-last-arguments nil
2979 "The arguments of the previous call to org-agenda")
2980(defvar org-starting-day nil) ; local variable in the agenda buffer
2981(defvar org-agenda-span nil) ; local variable in the agenda buffer
2982(defvar org-include-all-loc nil) ; local variable
20908596
CD
2983
2984;;;###autoload
2985(defun org-agenda-list (&optional include-all start-day ndays)
2986 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
2987The view will be for the current day or week, but from the overview buffer
2988you will be able to go to other days/weeks.
2989
2990With one \\[universal-argument] prefix argument INCLUDE-ALL,
2991all unfinished TODO items will also be shown, before the agenda.
2992This feature is considered obsolete, please use the TODO list or a block
2993agenda instead.
2994
2995With a numeric prefix argument in an interactive call, the agenda will
2996span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
2997the number of days. NDAYS defaults to `org-agenda-ndays'.
2998
2999START-DAY defaults to TODAY, or to the most recent match for the weekday
3000given in `org-agenda-start-on-weekday'."
3001 (interactive "P")
3002 (if (and (integerp include-all) (> include-all 0))
3003 (setq ndays include-all include-all nil))
3004 (setq ndays (or ndays org-agenda-ndays)
3005 start-day (or start-day org-agenda-start-day))
3006 (if org-agenda-overriding-arguments
3007 (setq include-all (car org-agenda-overriding-arguments)
3008 start-day (nth 1 org-agenda-overriding-arguments)
3009 ndays (nth 2 org-agenda-overriding-arguments)))
3010 (if (stringp start-day)
3011 ;; Convert to an absolute day number
3012 (setq start-day (time-to-days (org-read-date nil t start-day))))
3013 (setq org-agenda-last-arguments (list include-all start-day ndays))
3014 (org-compile-prefix-format 'agenda)
3015 (org-set-sorting-strategy 'agenda)
3016 (require 'calendar)
3017 (let* ((org-agenda-start-on-weekday
3018 (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
3019 org-agenda-start-on-weekday nil))
2c3ad40d 3020 (thefiles (org-agenda-files nil 'ifmode))
20908596
CD
3021 (files thefiles)
3022 (today (time-to-days
3023 (time-subtract (current-time)
3024 (list 0 (* 3600 org-extend-today-until) 0))))
3025 (sd (or start-day today))
3026 (start (if (or (null org-agenda-start-on-weekday)
3027 (< org-agenda-ndays 7))
3028 sd
3029 (let* ((nt (calendar-day-of-week
3030 (calendar-gregorian-from-absolute sd)))
3031 (n1 org-agenda-start-on-weekday)
3032 (d (- nt n1)))
3033 (- sd (+ (if (< d 0) 7 0) d)))))
3034 (day-numbers (list start))
3035 (day-cnt 0)
3036 (inhibit-redisplay (not debug-on-error))
3037 s e rtn rtnall file date d start-pos end-pos todayp nd wd
3038 clocktable-start clocktable-end)
3039 (setq org-agenda-redo-command
3040 (list 'org-agenda-list (list 'quote include-all) start-day ndays))
3041 ;; Make the list of days
3042 (setq ndays (or ndays org-agenda-ndays)
3043 nd ndays)
3044 (while (> ndays 1)
3045 (push (1+ (car day-numbers)) day-numbers)
3046 (setq ndays (1- ndays)))
3047 (setq day-numbers (nreverse day-numbers))
3048 (setq clocktable-start (car day-numbers)
3049 clocktable-end (1+ (or (org-last day-numbers) 0)))
3050 (org-prepare-agenda "Day/Week")
3051 (org-set-local 'org-starting-day (car day-numbers))
3052 (org-set-local 'org-include-all-loc include-all)
3053 (org-set-local 'org-agenda-span
3054 (org-agenda-ndays-to-span nd))
3055 (when (and (or include-all org-agenda-include-all-todo)
3056 (member today day-numbers))
3057 (setq files thefiles
3058 rtnall nil)
3059 (while (setq file (pop files))
3060 (catch 'nextfile
3061 (org-check-agenda-file file)
3062 (setq date (calendar-gregorian-from-absolute today)
3063 rtn (org-agenda-get-day-entries
3064 file date :todo))
3065 (setq rtnall (append rtnall rtn))))
3066 (when rtnall
8d642074 3067 (insert "All currently open TODO items:\n")
20908596 3068 (add-text-properties (point-min) (1- (point))
8d642074
CD
3069 (list 'face 'org-agenda-structure
3070 'short-heading "All TODO items"))
8bfe682a 3071 (org-agenda-mark-header-line (point-min))
20908596
CD
3072 (insert (org-finalize-agenda-entries rtnall) "\n")))
3073 (unless org-agenda-compact-blocks
3074 (let* ((d1 (car day-numbers))
3075 (d2 (org-last day-numbers))
3076 (w1 (org-days-to-iso-week d1))
3077 (w2 (org-days-to-iso-week d2)))
3078 (setq s (point))
c8d0cf5c
CD
3079 (if org-agenda-overriding-header
3080 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3081 nil 'face 'org-agenda-structure) "\n")
3082 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
3083 "-agenda"
3084 (if (< (- d2 d1) 350)
3085 (if (= w1 w2)
3086 (format " (W%02d)" w1)
3087 (format " (W%02d-W%02d)" w1 w2))
3088 "")
3089 ":\n")))
20908596 3090 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
8d642074
CD
3091 'org-date-line t))
3092 (org-agenda-mark-header-line s))
20908596
CD
3093 (while (setq d (pop day-numbers))
3094 (setq date (calendar-gregorian-from-absolute d)
3095 wd (calendar-day-of-week date)
3096 s (point))
3097 (if (or (setq todayp (= d today))
3098 (and (not start-pos) (= d sd)))
3099 (setq start-pos (point))
3100 (if (and start-pos (not end-pos))
3101 (setq end-pos (point))))
3102 (setq files thefiles
3103 rtnall nil)
3104 (while (setq file (pop files))
3105 (catch 'nextfile
3106 (org-check-agenda-file file)
93b62de8
CD
3107 (cond
3108 ((eq org-agenda-show-log 'only)
3109 (setq rtn (org-agenda-get-day-entries
3110 file date :closed)))
3111 (org-agenda-show-log
3112 (setq rtn (org-agenda-get-day-entries
3113 file date
3114 :deadline :scheduled :timestamp :sexp :closed)))
3115 (t
20908596
CD
3116 (setq rtn (org-agenda-get-day-entries
3117 file date
93b62de8 3118 :deadline :scheduled :sexp :timestamp))))
20908596
CD
3119 (setq rtnall (append rtnall rtn))))
3120 (if org-agenda-include-diary
c8d0cf5c 3121 (let ((org-agenda-search-headline-for-time t))
20908596
CD
3122 (require 'diary-lib)
3123 (setq rtn (org-get-entries-from-diary date))
3124 (setq rtnall (append rtnall rtn))))
3125 (if (or rtnall org-agenda-show-all-dates)
3126 (progn
3127 (setq day-cnt (1+ day-cnt))
3128 (insert
3129 (if (stringp org-agenda-format-date)
3130 (format-time-string org-agenda-format-date
3131 (org-time-from-absolute date))
3132 (funcall org-agenda-format-date date))
3133 "\n")
3134 (put-text-property s (1- (point)) 'face
3135 (if (member wd org-agenda-weekend-days)
3136 'org-agenda-date-weekend
3137 'org-agenda-date))
3138 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3139 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596 3140 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
c8d0cf5c
CD
3141 (when todayp
3142 (put-text-property s (1- (point)) 'org-today t)
3143 (put-text-property s (1- (point)) 'face 'org-agenda-date-today))
20908596
CD
3144 (if rtnall (insert
3145 (org-finalize-agenda-entries
3146 (org-agenda-add-time-grid-maybe
3147 rtnall nd todayp))
3148 "\n"))
3149 (put-text-property s (1- (point)) 'day d)
3150 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3151 (when (and org-agenda-clockreport-mode clocktable-start)
2c3ad40d 3152 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
20908596
CD
3153 ;; the above line is to ensure the restricted range!
3154 (p org-agenda-clockreport-parameter-plist)
3155 tbl)
3156 (setq p (org-plist-delete p :block))
3157 (setq p (plist-put p :tstart clocktable-start))
3158 (setq p (plist-put p :tend clocktable-end))
3159 (setq p (plist-put p :scope 'agenda))
3160 (setq tbl (apply 'org-get-clocktable p))
3161 (insert tbl)))
3162 (goto-char (point-min))
c8d0cf5c 3163 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3164 (unless (and (pos-visible-in-window-p (point-min))
3165 (pos-visible-in-window-p (point-max)))
3166 (goto-char (1- (point-max)))
3167 (recenter -1)
3168 (if (not (pos-visible-in-window-p (or start-pos 1)))
3169 (progn
3170 (goto-char (or start-pos 1))
3171 (recenter 1))))
3172 (goto-char (or start-pos 1))
3173 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3174 (org-finalize-agenda)
3175 (setq buffer-read-only t)
3176 (message "")))
3177
3178(defun org-agenda-ndays-to-span (n)
3179 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
3180
3181;;; Agenda word search
3182
3183(defvar org-agenda-search-history nil)
3184(defvar org-todo-only nil)
3185
3186(defvar org-search-syntax-table nil
3187 "Special syntax table for org-mode search.
3188In this table, we have single quotes not as word constituents, to
33306645 3189that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
20908596
CD
3190
3191(defun org-search-syntax-table ()
3192 (unless org-search-syntax-table
3193 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3194 (modify-syntax-entry ?' "." org-search-syntax-table)
3195 (modify-syntax-entry ?` "." org-search-syntax-table))
3196 org-search-syntax-table)
3197
3198;;;###autoload
3199(defun org-search-view (&optional todo-only string edit-at)
3200 "Show all entries that contain words or regular expressions.
3201If the first character of the search string is an asterisks,
3202search only the headlines.
3203
3204With optional prefix argument TODO-ONLY, only consider entries that are
3205TODO entries. The argument STRING can be used to pass a default search
3206string into this function. If EDIT-AT is non-nil, it means that the
3207user should get a chance to edit this string, with cursor at position
3208EDIT-AT.
3209
3210The search string is broken into \"words\" by splitting at whitespace.
8bfe682a
CD
3211Depending on the variable `org-agenda-search-view-search-words-only'
3212and on whether the first character in the search string is \"+\" or \"-\",
3213The string is then interpreted either as a substring with variable amounts
3214of whitespace, or as a list or individual words that should be matched.
3215
3216The default is a substring match, where each space in the search string
3217can expand to an arbitrary amount of whitespace, including newlines.
3218
3219If matching individual words, these words are then interpreted as a
3220boolean expression with logical AND. Words prefixed with a minus must
3221not occur in the entry. Words without a prefix or prefixed with a plus
3222must occur in the entry. Matching is case-insensitive and the words
3223are enclosed by word delimiters.
20908596
CD
3224
3225Words enclosed by curly braces are interpreted as regular expressions
3226that must or must not match in the entry.
3227
3228If the search string starts with an asterisk, search only in headlines.
3229If (possibly after the leading star) the search string starts with an
3230exclamation mark, this also means to look at TODO entries only, an effect
3231that can also be achieved with a prefix argument.
3232
3233This command searches the agenda files, and in addition the files listed
3234in `org-agenda-text-search-extra-files'."
3235 (interactive "P")
3236 (org-compile-prefix-format 'search)
3237 (org-set-sorting-strategy 'search)
3238 (org-prepare-agenda "SEARCH")
3239 (let* ((props (list 'face nil
c8d0cf5c 3240 'done-face 'org-agenda-done
20908596
CD
3241 'org-not-done-regexp org-not-done-regexp
3242 'org-todo-regexp org-todo-regexp
b349f79f 3243 'org-complex-heading-regexp org-complex-heading-regexp
20908596 3244 'mouse-face 'highlight
20908596
CD
3245 'help-echo (format "mouse-2 or RET jump to location")))
3246 regexp rtn rtnall files file pos
8bfe682a 3247 marker category tags c neg re as-words
20908596
CD
3248 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3249 (unless (and (not edit-at)
3250 (stringp string)
3251 (string-match "\\S-" string))
3252 (setq string (read-string "[+-]Word/{Regexp} ...: "
3253 (cond
3254 ((integerp edit-at) (cons string edit-at))
3255 (edit-at string))
3256 'org-agenda-search-history)))
3257 (org-set-local 'org-todo-only todo-only)
3258 (setq org-agenda-redo-command
3259 (list 'org-search-view (if todo-only t nil) string
3260 '(if current-prefix-arg 1 nil)))
3261 (setq org-agenda-query-string string)
3262
3263 (if (equal (string-to-char string) ?*)
3264 (setq hdl-only t
3265 words (substring string 1))
3266 (setq words string))
3267 (when (equal (string-to-char words) ?!)
3268 (setq todo-only t
3269 words (substring words 1)))
8bfe682a
CD
3270 (if (or org-agenda-search-view-search-words-only
3271 (member (string-to-char string) '(?- ?+)))
3272 (setq as-words t))
20908596 3273 (setq words (org-split-string words))
8bfe682a
CD
3274 (if as-words
3275 (mapc (lambda (w)
3276 (setq c (string-to-char w))
3277 (if (equal c ?-)
3278 (setq neg t w (substring w 1))
3279 (if (equal c ?+)
3280 (setq neg nil w (substring w 1))
3281 (setq neg nil)))
3282 (if (string-match "\\`{.*}\\'" w)
3283 (setq re (substring w 1 -1))
3284 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
3285 (if neg (push re regexps-) (push re regexps+)))
3286 words)
3287 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3288 regexps+))
20908596
CD
3289 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3290 (if (not regexps+)
3291 (setq regexp (concat "^" org-outline-regexp))
3292 (setq regexp (pop regexps+))
3293 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3294 regexp))))
2c3ad40d 3295 (setq files (org-agenda-files nil 'ifmode))
20908596
CD
3296 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3297 (pop org-agenda-text-search-extra-files)
3298 (setq files (org-add-archive-files files)))
3299 (setq files (append files org-agenda-text-search-extra-files)
3300 rtnall nil)
3301 (while (setq file (pop files))
3302 (setq ee nil)
3303 (catch 'nextfile
3304 (org-check-agenda-file file)
3305 (setq buffer (if (file-exists-p file)
3306 (org-get-agenda-file-buffer file)
3307 (error "No such file %s" file)))
3308 (if (not buffer)
3309 ;; If file does not exist, make sure an error message is sent
3310 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3311 file))))
3312 (with-current-buffer buffer
3313 (with-syntax-table (org-search-syntax-table)
3314 (unless (org-mode-p)
3315 (error "Agenda file %s is not in `org-mode'" file))
3316 (let ((case-fold-search t))
3317 (save-excursion
3318 (save-restriction
3319 (if org-agenda-restrict
3320 (narrow-to-region org-agenda-restrict-begin
3321 org-agenda-restrict-end)
3322 (widen))
3323 (goto-char (point-min))
3324 (unless (or (org-on-heading-p)
3325 (outline-next-heading))
3326 (throw 'nextfile t))
3327 (goto-char (max (point-min) (1- (point))))
3328 (while (re-search-forward regexp nil t)
3329 (org-back-to-heading t)
3330 (skip-chars-forward "* ")
3331 (setq beg (point-at-bol)
3332 beg1 (point)
3333 end (progn (outline-next-heading) (point)))
3334 (catch :skip
3335 (goto-char beg)
3336 (org-agenda-skip)
3337 (setq str (buffer-substring-no-properties
3338 (point-at-bol)
3339 (if hdl-only (point-at-eol) end)))
3340 (mapc (lambda (wr) (when (string-match wr str)
3341 (goto-char (1- end))
3342 (throw :skip t)))
3343 regexps-)
3344 (mapc (lambda (wr) (unless (string-match wr str)
3345 (goto-char (1- end))
3346 (throw :skip t)))
3347 (if todo-only
3348 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3349 regexps+)
3350 regexps+))
3351 (goto-char beg)
3352 (setq marker (org-agenda-new-marker (point))
3353 category (org-get-category)
3354 tags (org-get-tags-at (point))
3355 txt (org-format-agenda-item
3356 ""
3357 (buffer-substring-no-properties
3358 beg1 (point-at-eol))
3359 category tags))
3360 (org-add-props txt props
3361 'org-marker marker 'org-hd-marker marker
3362 'org-todo-regexp org-todo-regexp
b349f79f 3363 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
3364 'priority 1000 'org-category category
3365 'type "search")
3366 (push txt ee)
3367 (goto-char (1- end))))))))))
3368 (setq rtn (nreverse ee))
3369 (setq rtnall (append rtnall rtn)))
3370 (if org-agenda-overriding-header
3371 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3372 nil 'face 'org-agenda-structure) "\n")
3373 (insert "Search words: ")
3374 (add-text-properties (point-min) (1- (point))
3375 (list 'face 'org-agenda-structure))
3376 (setq pos (point))
3377 (insert string "\n")
3378 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3379 (setq pos (point))
3380 (unless org-agenda-multi
3381 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3382 (add-text-properties pos (1- (point))
3383 (list 'face 'org-agenda-structure))))
8d642074 3384 (org-agenda-mark-header-line (point-min))
20908596
CD
3385 (when rtnall
3386 (insert (org-finalize-agenda-entries rtnall) "\n"))
3387 (goto-char (point-min))
c8d0cf5c 3388 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3389 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3390 (org-finalize-agenda)
3391 (setq buffer-read-only t)))
3392
3393;;; Agenda TODO list
3394
3395(defvar org-select-this-todo-keyword nil)
3396(defvar org-last-arg nil)
3397
3398;;;###autoload
3399(defun org-todo-list (arg)
3400 "Show all TODO entries from all agenda file in a single list.
3401The prefix arg can be used to select a specific TODO keyword and limit
3402the list to these. When using \\[universal-argument], you will be prompted
3403for a keyword. A numeric prefix directly selects the Nth keyword in
3404`org-todo-keywords-1'."
3405 (interactive "P")
3406 (require 'calendar)
3407 (org-compile-prefix-format 'todo)
3408 (org-set-sorting-strategy 'todo)
3409 (org-prepare-agenda "TODO")
3410 (let* ((today (time-to-days (current-time)))
3411 (date (calendar-gregorian-from-absolute today))
3412 (kwds org-todo-keywords-for-agenda)
3413 (completion-ignore-case t)
3414 (org-select-this-todo-keyword
3415 (if (stringp arg) arg
3416 (and arg (integerp arg) (> arg 0)
3417 (nth (1- arg) kwds))))
3418 rtn rtnall files file pos)
3419 (when (equal arg '(4))
3420 (setq org-select-this-todo-keyword
54a0dee5 3421 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
20908596
CD
3422 (mapcar 'list kwds) nil nil)))
3423 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3424 (org-set-local 'org-last-arg arg)
3425 (setq org-agenda-redo-command
3426 '(org-todo-list (or current-prefix-arg org-last-arg)))
2c3ad40d 3427 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
3428 rtnall nil)
3429 (while (setq file (pop files))
3430 (catch 'nextfile
3431 (org-check-agenda-file file)
3432 (setq rtn (org-agenda-get-day-entries file date :todo))
3433 (setq rtnall (append rtnall rtn))))
3434 (if org-agenda-overriding-header
3435 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3436 nil 'face 'org-agenda-structure) "\n")
3437 (insert "Global list of TODO items of type: ")
3438 (add-text-properties (point-min) (1- (point))
8d642074
CD
3439 (list 'face 'org-agenda-structure
3440 'short-heading
3441 (concat "ToDo: "
3442 (or org-select-this-todo-keyword "ALL"))))
3443 (org-agenda-mark-header-line (point-min))
20908596
CD
3444 (setq pos (point))
3445 (insert (or org-select-this-todo-keyword "ALL") "\n")
3446 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3447 (setq pos (point))
3448 (unless org-agenda-multi
3449 (insert "Available with `N r': (0)ALL")
3450 (let ((n 0) s)
3451 (mapc (lambda (x)
3452 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3453 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3454 (insert "\n "))
3455 (insert " " s))
3456 kwds))
3457 (insert "\n"))
3458 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 3459 (org-agenda-mark-header-line (point-min))
20908596
CD
3460 (when rtnall
3461 (insert (org-finalize-agenda-entries rtnall) "\n"))
3462 (goto-char (point-min))
c8d0cf5c 3463 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3464 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3465 (org-finalize-agenda)
3466 (setq buffer-read-only t)))
3467
3468;;; Agenda tags match
3469
3470;;;###autoload
3471(defun org-tags-view (&optional todo-only match)
3472 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3473The prefix arg TODO-ONLY limits the search to TODO entries."
3474 (interactive "P")
3475 (org-compile-prefix-format 'tags)
3476 (org-set-sorting-strategy 'tags)
3477 (let* ((org-tags-match-list-sublevels
c8d0cf5c
CD
3478;?????? (if todo-only t org-tags-match-list-sublevels))
3479 org-tags-match-list-sublevels)
20908596
CD
3480 (completion-ignore-case t)
3481 rtn rtnall files file pos matcher
3482 buffer)
3483 (setq matcher (org-make-tags-matcher match)
3484 match (car matcher) matcher (cdr matcher))
3485 (org-prepare-agenda (concat "TAGS " match))
3486 (setq org-agenda-query-string match)
3487 (setq org-agenda-redo-command
3488 (list 'org-tags-view (list 'quote todo-only)
3489 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
2c3ad40d 3490 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
3491 rtnall nil)
3492 (while (setq file (pop files))
3493 (catch 'nextfile
3494 (org-check-agenda-file file)
3495 (setq buffer (if (file-exists-p file)
3496 (org-get-agenda-file-buffer file)
3497 (error "No such file %s" file)))
3498 (if (not buffer)
33306645 3499 ;; If file does not exist, error message to agenda
20908596
CD
3500 (setq rtn (list
3501 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3502 rtnall (append rtnall rtn))
3503 (with-current-buffer buffer
3504 (unless (org-mode-p)
3505 (error "Agenda file %s is not in `org-mode'" file))
3506 (save-excursion
3507 (save-restriction
3508 (if org-agenda-restrict
3509 (narrow-to-region org-agenda-restrict-begin
3510 org-agenda-restrict-end)
3511 (widen))
3512 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3513 (setq rtnall (append rtnall rtn))))))))
3514 (if org-agenda-overriding-header
3515 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3516 nil 'face 'org-agenda-structure) "\n")
3517 (insert "Headlines with TAGS match: ")
3518 (add-text-properties (point-min) (1- (point))
8d642074
CD
3519 (list 'face 'org-agenda-structure
3520 'short-heading
3521 (concat "Match: " match)))
20908596
CD
3522 (setq pos (point))
3523 (insert match "\n")
3524 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3525 (setq pos (point))
3526 (unless org-agenda-multi
3527 (insert "Press `C-u r' to search again with new search string\n"))
3528 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 3529 (org-agenda-mark-header-line (point-min))
20908596
CD
3530 (when rtnall
3531 (insert (org-finalize-agenda-entries rtnall) "\n"))
3532 (goto-char (point-min))
c8d0cf5c 3533 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3534 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
3535 (org-finalize-agenda)
3536 (setq buffer-read-only t)))
3537
3538;;; Agenda Finding stuck projects
3539
3540(defvar org-agenda-skip-regexp nil
3541 "Regular expression used in skipping subtrees for the agenda.
3542This is basically a temporary global variable that can be set and then
3543used by user-defined selections using `org-agenda-skip-function'.")
3544
3545(defvar org-agenda-overriding-header nil
c8d0cf5c
CD
3546 "When this is set during todo and tags searches, will replace header.
3547This variable should not be set directly, but custom commands can bind it
3548in the options section.")
3549
3550(defun org-agenda-skip-entry-when-regexp-matches ()
3551 "Checks if the current entry contains match for `org-agenda-skip-regexp'.
3552If yes, it returns the end position of this entry, causing agenda commands
3553to skip the entry but continuing the search in the subtree. This is a
3554function that can be put into `org-agenda-skip-function' for the duration
3555of a command."
3556 (let ((end (save-excursion (org-end-of-subtree t)))
3557 skip)
3558 (save-excursion
3559 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3560 (and skip end)))
20908596
CD
3561
3562(defun org-agenda-skip-subtree-when-regexp-matches ()
3563 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3564If yes, it returns the end position of this tree, causing agenda commands
3565to skip this subtree. This is a function that can be put into
3566`org-agenda-skip-function' for the duration of a command."
3567 (let ((end (save-excursion (org-end-of-subtree t)))
3568 skip)
3569 (save-excursion
3570 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3571 (and skip end)))
3572
c8d0cf5c
CD
3573(defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
3574 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3575If yes, it returns the end position of the current entry (NOT the tree),
3576causing agenda commands to skip the entry but continuing the search in
3577the subtree. This is a function that can be put into
3578`org-agenda-skip-function' for the duration of a command. An important
3579use of this function is for the stuck project list."
3580 (let ((end (save-excursion (org-end-of-subtree t)))
3581 (entry-end (save-excursion (outline-next-heading) (1- (point))))
3582 skip)
3583 (save-excursion
3584 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3585 (and skip entry-end)))
3586
20908596
CD
3587(defun org-agenda-skip-entry-if (&rest conditions)
3588 "Skip entry if any of CONDITIONS is true.
3589See `org-agenda-skip-if' for details."
3590 (org-agenda-skip-if nil conditions))
3591
3592(defun org-agenda-skip-subtree-if (&rest conditions)
3593 "Skip entry if any of CONDITIONS is true.
3594See `org-agenda-skip-if' for details."
3595 (org-agenda-skip-if t conditions))
3596
3597(defun org-agenda-skip-if (subtree conditions)
3598 "Checks current entity for CONDITIONS.
3599If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
3600the entry, i.e. the text before the next heading is checked.
3601
3602CONDITIONS is a list of symbols, boolean OR is used to combine the results
3603from different tests. Valid conditions are:
3604
3605scheduled Check if there is a scheduled cookie
3606notscheduled Check if there is no scheduled cookie
3607deadline Check if there is a deadline
3608notdeadline Check if there is no deadline
c8d0cf5c
CD
3609timestamp Check if there is a timestamp (also deadline or scheduled)
3610nottimestamp Check if there is no timestamp (also deadline or scheduled)
20908596
CD
3611regexp Check if regexp matches
3612notregexp Check if regexp does not match.
3613
3614The regexp is taken from the conditions list, it must come right after
3615the `regexp' or `notregexp' element.
3616
3617If any of these conditions is met, this function returns the end point of
3618the entity, causing the search to continue from there. This is a function
3619that can be put into `org-agenda-skip-function' for the duration of a command."
3620 (let (beg end m)
3621 (org-back-to-heading t)
3622 (setq beg (point)
3623 end (if subtree
3624 (progn (org-end-of-subtree t) (point))
3625 (progn (outline-next-heading) (1- (point)))))
3626 (goto-char beg)
3627 (and
3628 (or
3629 (and (memq 'scheduled conditions)
3630 (re-search-forward org-scheduled-time-regexp end t))
3631 (and (memq 'notscheduled conditions)
3632 (not (re-search-forward org-scheduled-time-regexp end t)))
3633 (and (memq 'deadline conditions)
3634 (re-search-forward org-deadline-time-regexp end t))
3635 (and (memq 'notdeadline conditions)
3636 (not (re-search-forward org-deadline-time-regexp end t)))
c8d0cf5c
CD
3637 (and (memq 'timestamp conditions)
3638 (re-search-forward org-ts-regexp end t))
3639 (and (memq 'nottimestamp conditions)
3640 (not (re-search-forward org-ts-regexp end t)))
20908596
CD
3641 (and (setq m (memq 'regexp conditions))
3642 (stringp (nth 1 m))
3643 (re-search-forward (nth 1 m) end t))
3644 (and (setq m (memq 'notregexp conditions))
3645 (stringp (nth 1 m))
3646 (not (re-search-forward (nth 1 m) end t))))
3647 end)))
3648
3649;;;###autoload
3650(defun org-agenda-list-stuck-projects (&rest ignore)
3651 "Create agenda view for projects that are stuck.
3652Stuck projects are project that have no next actions. For the definitions
3653of what a project is and how to check if it stuck, customize the variable
3654`org-stuck-projects'.
3655MATCH is being ignored."
3656 (interactive)
c8d0cf5c
CD
3657 (let* ((org-agenda-skip-function
3658 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
20908596 3659 ;; We could have used org-agenda-skip-if here.
c8d0cf5c
CD
3660 (org-agenda-overriding-header
3661 (or org-agenda-overriding-header "List of stuck projects: "))
20908596
CD
3662 (matcher (nth 0 org-stuck-projects))
3663 (todo (nth 1 org-stuck-projects))
3664 (todo-wds (if (member "*" todo)
3665 (progn
2c3ad40d
CD
3666 (org-prepare-agenda-buffers (org-agenda-files
3667 nil 'ifmode))
20908596
CD
3668 (org-delete-all
3669 org-done-keywords-for-agenda
3670 (copy-sequence org-todo-keywords-for-agenda)))
3671 todo))
3672 (todo-re (concat "^\\*+[ \t]+\\("
3673 (mapconcat 'identity todo-wds "\\|")
3674 "\\)\\>"))
3675 (tags (nth 2 org-stuck-projects))
3676 (tags-re (if (member "*" tags)
3677 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
c8d0cf5c
CD
3678 (if tags
3679 (concat "^\\*+ .*:\\("
3680 (mapconcat 'identity tags "\\|")
3681 (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
20908596
CD
3682 (gen-re (nth 3 org-stuck-projects))
3683 (re-list
3684 (delq nil
3685 (list
3686 (if todo todo-re)
3687 (if tags tags-re)
3688 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
3689 gen-re)))))
3690 (setq org-agenda-skip-regexp
3691 (if re-list
3692 (mapconcat 'identity re-list "\\|")
3693 (error "No information how to identify unstuck projects")))
3694 (org-tags-view nil matcher)
3695 (with-current-buffer org-agenda-buffer-name
3696 (setq org-agenda-redo-command
3697 '(org-agenda-list-stuck-projects
3698 (or current-prefix-arg org-last-arg))))))
3699
3700;;; Diary integration
3701
3702(defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
3703(defvar list-diary-entries-hook)
3704
3705(defun org-get-entries-from-diary (date)
3706 "Get the (Emacs Calendar) diary entries for DATE."
3707 (require 'diary-lib)
3708 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
ff4be292 3709 (fancy-diary-buffer diary-fancy-buffer)
20908596 3710 (diary-display-hook '(fancy-diary-display))
ca8ef0dc 3711 (diary-display-function 'fancy-diary-display)
20908596
CD
3712 (pop-up-frames nil)
3713 (list-diary-entries-hook
3714 (cons 'org-diary-default-entry list-diary-entries-hook))
3715 (diary-file-name-prefix-function nil) ; turn this feature off
3716 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
3717 entries
3718 (org-disable-agenda-to-diary t))
3719 (save-excursion
3720 (save-window-excursion
3721 (funcall (if (fboundp 'diary-list-entries)
3722 'diary-list-entries 'list-diary-entries)
3723 date 1)))
3724 (if (not (get-buffer diary-fancy-buffer))
3725 (setq entries nil)
3726 (with-current-buffer diary-fancy-buffer
3727 (setq buffer-read-only nil)
3728 (if (zerop (buffer-size))
3729 ;; No entries
3730 (setq entries nil)
3731 ;; Omit the date and other unnecessary stuff
3732 (org-agenda-cleanup-fancy-diary)
3733 ;; Add prefix to each line and extend the text properties
3734 (if (zerop (buffer-size))
3735 (setq entries nil)
3736 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3737 (set-buffer-modified-p nil)
3738 (kill-buffer diary-fancy-buffer)))
3739 (when entries
3740 (setq entries (org-split-string entries "\n"))
3741 (setq entries
3742 (mapcar
3743 (lambda (x)
3744 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
3745 ;; Extend the text properties to the beginning of the line
3746 (org-add-props x (text-properties-at (1- (length x)) x)
3747 'type "diary" 'date date))
3748 entries)))))
3749
c8d0cf5c
CD
3750(defvar org-agenda-cleanup-fancy-diary-hook nil
3751 "Hook run when the fancy diary buffer is cleaned up.")
3752
20908596
CD
3753(defun org-agenda-cleanup-fancy-diary ()
3754 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3755This gets rid of the date, the underline under the date, and
3756the dummy entry installed by `org-mode' to ensure non-empty diary for each
3757date. It also removes lines that contain only whitespace."
3758 (goto-char (point-min))
3759 (if (looking-at ".*?:[ \t]*")
3760 (progn
3761 (replace-match "")
3762 (re-search-forward "\n=+$" nil t)
3763 (replace-match "")
3764 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3765 (re-search-forward "\n=+$" nil t)
3766 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3767 (goto-char (point-min))
3768 (while (re-search-forward "^ +\n" nil t)
3769 (replace-match ""))
3770 (goto-char (point-min))
3771 (if (re-search-forward "^Org-mode dummy\n?" nil t)
c8d0cf5c
CD
3772 (replace-match ""))
3773 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
20908596
CD
3774
3775;; Make sure entries from the diary have the right text properties.
3776(eval-after-load "diary-lib"
3777 '(if (boundp 'diary-modify-entry-list-string-function)
3778 ;; We can rely on the hook, nothing to do
3779 nil
33306645 3780 ;; Hook not available, must use advice to make this work
20908596
CD
3781 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3782 "Make the position visible."
3783 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3784 (stringp string)
3785 buffer-file-name)
3786 (setq string (org-modify-diary-entry-string string))))))
3787
3788(defun org-modify-diary-entry-string (string)
3789 "Add text properties to string, allowing org-mode to act on it."
3790 (org-add-props string nil
3791 'mouse-face 'highlight
20908596
CD
3792 'help-echo (if buffer-file-name
3793 (format "mouse-2 or RET jump to diary file %s"
3794 (abbreviate-file-name buffer-file-name))
3795 "")
3796 'org-agenda-diary-link t
3797 'org-marker (org-agenda-new-marker (point-at-bol))))
3798
3799(defun org-diary-default-entry ()
3800 "Add a dummy entry to the diary.
3801Needed to avoid empty dates which mess up holiday display."
3802 ;; Catch the error if dealing with the new add-to-diary-alist
3803 (when org-disable-agenda-to-diary
3804 (condition-case nil
3805 (org-add-to-diary-list original-date "Org-mode dummy" "")
3806 (error
3807 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3808
3809(defun org-add-to-diary-list (&rest args)
3810 (if (fboundp 'diary-add-to-list)
3811 (apply 'diary-add-to-list args)
3812 (apply 'add-to-diary-list args)))
3813
3814;;;###autoload
3815(defun org-diary (&rest args)
3816 "Return diary information from org-files.
3817This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3818It accesses org files and extracts information from those files to be
3819listed in the diary. The function accepts arguments specifying what
3820items should be listed. The following arguments are allowed:
3821
3822 :timestamp List the headlines of items containing a date stamp or
3823 date range matching the selected date. Deadlines will
3824 also be listed, on the expiration day.
3825
3826 :sexp List entries resulting from diary-like sexps.
3827
3828 :deadline List any deadlines past due, or due within
3829 `org-deadline-warning-days'. The listing occurs only
3830 in the diary for *today*, not at any other date. If
3831 an entry is marked DONE, it is no longer listed.
3832
3833 :scheduled List all items which are scheduled for the given date.
3834 The diary for *today* also contains items which were
3835 scheduled earlier and are not yet marked DONE.
3836
3837 :todo List all TODO items from the org-file. This may be a
3838 long list - so this is not turned on by default.
3839 Like deadlines, these entries only show up in the
3840 diary for *today*, not at any other date.
3841
3842The call in the diary file should look like this:
3843
3844 &%%(org-diary) ~/path/to/some/orgfile.org
3845
3846Use a separate line for each org file to check. Or, if you omit the file name,
3847all files listed in `org-agenda-files' will be checked automatically:
3848
3849 &%%(org-diary)
3850
3851If you don't give any arguments (as in the example above), the default
3852arguments (:deadline :scheduled :timestamp :sexp) are used.
3853So the example above may also be written as
3854
3855 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3856
3857The function expects the lisp variables `entry' and `date' to be provided
3858by the caller, because this is how the calendar works. Don't use this
3859function from a program - use `org-agenda-get-day-entries' instead."
54a0dee5 3860 (when (> (- (org-float-time)
20908596
CD
3861 org-agenda-last-marker-time)
3862 5)
3863 (org-agenda-reset-markers))
3864 (org-compile-prefix-format 'agenda)
3865 (org-set-sorting-strategy 'agenda)
3866 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3867 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3868 (list entry)
3869 (org-agenda-files t)))
3870 file rtn results)
3871 (org-prepare-agenda-buffers files)
3872 ;; If this is called during org-agenda, don't return any entries to
3873 ;; the calendar. Org Agenda will list these entries itself.
3874 (if org-disable-agenda-to-diary (setq files nil))
3875 (while (setq file (pop files))
3876 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3877 (setq results (append results rtn)))
3878 (if results
3879 (concat (org-finalize-agenda-entries results) "\n"))))
3880
3881;;; Agenda entry finders
3882
3883(defun org-agenda-get-day-entries (file date &rest args)
3884 "Does the work for `org-diary' and `org-agenda'.
3885FILE is the path to a file to be checked for entries. DATE is date like
3886the one returned by `calendar-current-date'. ARGS are symbols indicating
3887which kind of entries should be extracted. For details about these, see
3888the documentation of `org-diary'."
3889 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3890 (let* ((org-startup-folded nil)
3891 (org-startup-align-all-tables nil)
3892 (buffer (if (file-exists-p file)
3893 (org-get-agenda-file-buffer file)
3894 (error "No such file %s" file)))
54a0dee5 3895 arg results rtn deadline-results)
20908596
CD
3896 (if (not buffer)
3897 ;; If file does not exist, make sure an error message ends up in diary
3898 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3899 (with-current-buffer buffer
3900 (unless (org-mode-p)
3901 (error "Agenda file %s is not in `org-mode'" file))
3902 (let ((case-fold-search nil))
3903 (save-excursion
3904 (save-restriction
3905 (if org-agenda-restrict
3906 (narrow-to-region org-agenda-restrict-begin
3907 org-agenda-restrict-end)
3908 (widen))
3909 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3910 (while (setq arg (pop args))
3911 (cond
3912 ((and (eq arg :todo)
3913 (equal date (calendar-current-date)))
3914 (setq rtn (org-agenda-get-todos))
3915 (setq results (append results rtn)))
3916 ((eq arg :timestamp)
3917 (setq rtn (org-agenda-get-blocks))
3918 (setq results (append results rtn))
3919 (setq rtn (org-agenda-get-timestamps))
3920 (setq results (append results rtn)))
3921 ((eq arg :sexp)
3922 (setq rtn (org-agenda-get-sexps))
3923 (setq results (append results rtn)))
3924 ((eq arg :scheduled)
54a0dee5 3925 (setq rtn (org-agenda-get-scheduled deadline-results))
20908596
CD
3926 (setq results (append results rtn)))
3927 ((eq arg :closed)
93b62de8 3928 (setq rtn (org-agenda-get-progress))
20908596
CD
3929 (setq results (append results rtn)))
3930 ((eq arg :deadline)
3931 (setq rtn (org-agenda-get-deadlines))
54a0dee5 3932 (setq deadline-results (copy-sequence rtn))
20908596
CD
3933 (setq results (append results rtn))))))))
3934 results))))
3935
3936(defun org-agenda-get-todos ()
3937 "Return the TODO information for agenda display."
3938 (let* ((props (list 'face nil
c8d0cf5c 3939 'done-face 'org-agenda-done
20908596
CD
3940 'org-not-done-regexp org-not-done-regexp
3941 'org-todo-regexp org-todo-regexp
b349f79f 3942 'org-complex-heading-regexp org-complex-heading-regexp
20908596 3943 'mouse-face 'highlight
20908596
CD
3944 'help-echo
3945 (format "mouse-2 or RET jump to org file %s"
3946 (abbreviate-file-name buffer-file-name))))
3947 (regexp (concat "^\\*+[ \t]+\\("
3948 (if org-select-this-todo-keyword
3949 (if (equal org-select-this-todo-keyword "*")
3950 org-todo-regexp
3951 (concat "\\<\\("
3952 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
3953 "\\)\\>"))
3954 org-not-done-regexp)
3955 "[^\n\r]*\\)"))
621f83e4 3956 marker priority category tags todo-state
20908596
CD
3957 ee txt beg end)
3958 (goto-char (point-min))
3959 (while (re-search-forward regexp nil t)
3960 (catch :skip
3961 (save-match-data
3962 (beginning-of-line)
d6685abc 3963 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
0bd48b37 3964 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
20908596
CD
3965 (goto-char (1+ beg))
3966 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
3967 (throw :skip nil)))
3968 (goto-char beg)
3969 (org-agenda-skip)
3970 (goto-char (match-beginning 1))
3971 (setq marker (org-agenda-new-marker (match-beginning 0))
3972 category (org-get-category)
c8d0cf5c 3973 txt (match-string 1)
20908596 3974 tags (org-get-tags-at (point))
c8d0cf5c 3975 txt (org-format-agenda-item "" txt category tags)
621f83e4
CD
3976 priority (1+ (org-get-priority txt))
3977 todo-state (org-get-todo-state))
20908596
CD
3978 (org-add-props txt props
3979 'org-marker marker 'org-hd-marker marker
3980 'priority priority 'org-category category
621f83e4 3981 'type "todo" 'todo-state todo-state)
20908596
CD
3982 (push txt ee)
3983 (if org-agenda-todo-list-sublevels
3984 (goto-char (match-end 1))
3985 (org-end-of-subtree 'invisible))))
3986 (nreverse ee)))
3987
0bd48b37
CD
3988;;;###autoload
3989(defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
3990 "Do we have a reason to ignore this todo entry because it has a time stamp?"
3991 (when (or org-agenda-todo-ignore-with-date
3992 org-agenda-todo-ignore-scheduled
3993 org-agenda-todo-ignore-deadlines)
3994 (setq end (or end (save-excursion (outline-next-heading) (point))))
3995 (save-excursion
3996 (or (and org-agenda-todo-ignore-with-date
3997 (re-search-forward org-ts-regexp end t))
3998 (and org-agenda-todo-ignore-scheduled
3999 (re-search-forward org-scheduled-time-regexp end t))
4000 (and org-agenda-todo-ignore-deadlines
4001 (re-search-forward org-deadline-time-regexp end t)
4002 (org-deadline-close (match-string 1)))))))
4003
20908596
CD
4004(defconst org-agenda-no-heading-message
4005 "No heading for this item in buffer or region.")
4006
4007(defun org-agenda-get-timestamps ()
4008 "Return the date stamp information for agenda display."
4009 (let* ((props (list 'face nil
4010 'org-not-done-regexp org-not-done-regexp
4011 'org-todo-regexp org-todo-regexp
b349f79f 4012 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4013 'mouse-face 'highlight
20908596
CD
4014 'help-echo
4015 (format "mouse-2 or RET jump to org file %s"
4016 (abbreviate-file-name buffer-file-name))))
4017 (d1 (calendar-absolute-from-gregorian date))
4018 (remove-re
4019 (concat
4020 (regexp-quote
4021 (format-time-string
4022 "<%Y-%m-%d"
4023 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4024 ".*?>"))
4025 (regexp
4026 (concat
4027 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4028 (regexp-quote
4029 (substring
4030 (format-time-string
4031 (car org-time-stamp-formats)
4032 (apply 'encode-time ; DATE bound by calendar
4033 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4034 1 11))
4035 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4036 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4037 marker hdmarker deadlinep scheduledp clockp closedp inactivep
621f83e4 4038 donep tmp priority category ee txt timestr tags b0 b3 e3 head
c8d0cf5c 4039 todo-state end-of-match)
20908596 4040 (goto-char (point-min))
c8d0cf5c 4041 (while (setq end-of-match (re-search-forward regexp nil t))
20908596
CD
4042 (setq b0 (match-beginning 0)
4043 b3 (match-beginning 3) e3 (match-end 3))
4044 (catch :skip
4045 (and (org-at-date-range-p) (throw :skip nil))
4046 (org-agenda-skip)
4047 (if (and (match-end 1)
4048 (not (= d1 (org-time-string-to-absolute
4049 (match-string 1) d1 nil
4050 org-agenda-repeating-timestamp-show-all))))
4051 (throw :skip nil))
4052 (if (and e3
4053 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4054 (throw :skip nil))
c8d0cf5c 4055 (setq tmp (buffer-substring (max (point-min)
20908596
CD
4056 (- b0 org-ds-keyword-length))
4057 b0)
4058 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4059 inactivep (= (char-after b0) ?\[)
4060 deadlinep (string-match org-deadline-regexp tmp)
4061 scheduledp (string-match org-scheduled-regexp tmp)
4062 closedp (and org-agenda-include-inactive-timestamps
4063 (string-match org-closed-string tmp))
4064 clockp (and org-agenda-include-inactive-timestamps
4065 (or (string-match org-clock-string tmp)
4066 (string-match "]-+\\'" tmp)))
621f83e4
CD
4067 todo-state (org-get-todo-state)
4068 donep (member todo-state org-done-keywords))
c8d0cf5c
CD
4069 (if (or scheduledp deadlinep closedp clockp
4070 (and donep org-agenda-skip-timestamp-if-done))
20908596
CD
4071 (throw :skip t))
4072 (if (string-match ">" timestr)
4073 ;; substring should only run to end of time stamp
4074 (setq timestr (substring timestr 0 (match-end 0))))
c8d0cf5c
CD
4075 (setq marker (org-agenda-new-marker b0)
4076 category (org-get-category b0))
20908596 4077 (save-excursion
c8d0cf5c
CD
4078 (if (not (re-search-backward "^\\*+ " nil t))
4079 (setq txt org-agenda-no-heading-message)
4080 (goto-char (match-beginning 0))
4081 (setq hdmarker (org-agenda-new-marker)
4082 tags (org-get-tags-at))
4083 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4084 (setq head (match-string 1))
4085 (setq txt (org-format-agenda-item
4086 (if inactivep "[" nil)
4087 head category tags timestr nil
4088 remove-re)))
20908596
CD
4089 (setq priority (org-get-priority txt))
4090 (org-add-props txt props
4091 'org-marker marker 'org-hd-marker hdmarker)
4092 (org-add-props txt nil 'priority priority
4093 'org-category category 'date date
621f83e4 4094 'todo-state todo-state
20908596
CD
4095 'type "timestamp")
4096 (push txt ee))
c8d0cf5c
CD
4097 (if org-agenda-skip-additional-timestamps-same-entry
4098 (outline-next-heading)
4099 (goto-char end-of-match))))
20908596
CD
4100 (nreverse ee)))
4101
4102(defun org-agenda-get-sexps ()
4103 "Return the sexp information for agenda display."
4104 (require 'diary-lib)
4105 (let* ((props (list 'face nil
4106 'mouse-face 'highlight
20908596
CD
4107 'help-echo
4108 (format "mouse-2 or RET jump to org file %s"
4109 (abbreviate-file-name buffer-file-name))))
4110 (regexp "^&?%%(")
c8d0cf5c
CD
4111 marker category ee txt tags entry result beg b sexp sexp-entry
4112 todo-state)
20908596
CD
4113 (goto-char (point-min))
4114 (while (re-search-forward regexp nil t)
4115 (catch :skip
4116 (org-agenda-skip)
4117 (setq beg (match-beginning 0))
4118 (goto-char (1- (match-end 0)))
4119 (setq b (point))
4120 (forward-sexp 1)
4121 (setq sexp (buffer-substring b (point)))
4122 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4123 (org-trim (match-string 1))
4124 ""))
4125 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4126 (when result
4127 (setq marker (org-agenda-new-marker beg)
c8d0cf5c
CD
4128 category (org-get-category beg)
4129 todo-state (org-get-todo-state))
20908596
CD
4130
4131 (if (string-match "\\S-" result)
4132 (setq txt result)
4133 (setq txt "SEXP entry returned empty string"))
4134
4135 (setq txt (org-format-agenda-item
4136 "" txt category tags 'time))
4137 (org-add-props txt props 'org-marker marker)
4138 (org-add-props txt nil
c8d0cf5c 4139 'org-category category 'date date 'todo-state todo-state
20908596
CD
4140 'type "sexp")
4141 (push txt ee))))
4142 (nreverse ee)))
4143
d60b1ba1 4144(defalias 'org-get-closed 'org-agenda-get-progress)
93b62de8 4145(defun org-agenda-get-progress ()
20908596
CD
4146 "Return the logged TODO entries for agenda display."
4147 (let* ((props (list 'mouse-face 'highlight
4148 'org-not-done-regexp org-not-done-regexp
4149 'org-todo-regexp org-todo-regexp
b349f79f 4150 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
4151 'help-echo
4152 (format "mouse-2 or RET jump to org file %s"
4153 (abbreviate-file-name buffer-file-name))))
93b62de8
CD
4154 (items (if (consp org-agenda-show-log)
4155 org-agenda-show-log
4156 org-agenda-log-mode-items))
ff4be292 4157 (parts
93b62de8
CD
4158 (delq nil
4159 (list
4160 (if (memq 'closed items) (concat "\\<" org-closed-string))
4161 (if (memq 'clock items) (concat "\\<" org-clock-string))
c8d0cf5c 4162 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
93b62de8
CD
4163 (parts-re (if parts (mapconcat 'identity parts "\\|")
4164 (error "`org-agenda-log-mode-items' is empty")))
20908596 4165 (regexp (concat
93b62de8
CD
4166 "\\(" parts-re "\\)"
4167 " *\\["
20908596
CD
4168 (regexp-quote
4169 (substring
4170 (format-time-string
4171 (car org-time-stamp-formats)
4172 (apply 'encode-time ; DATE bound by calendar
4173 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4174 1 11))))
c8d0cf5c
CD
4175 (org-agenda-search-headline-for-time nil)
4176 marker hdmarker priority category tags closedp statep clockp state
4177 ee txt extra timestr rest clocked)
20908596
CD
4178 (goto-char (point-min))
4179 (while (re-search-forward regexp nil t)
4180 (catch :skip
4181 (org-agenda-skip)
4182 (setq marker (org-agenda-new-marker (match-beginning 0))
4183 closedp (equal (match-string 1) org-closed-string)
93b62de8 4184 statep (equal (string-to-char (match-string 1)) ?-)
c8d0cf5c 4185 clockp (not (or closedp statep))
93b62de8 4186 state (and statep (match-string 2))
20908596
CD
4187 category (org-get-category (match-beginning 0))
4188 timestr (buffer-substring (match-beginning 0) (point-at-eol))
20908596 4189 )
b349f79f
CD
4190 (when (string-match "\\]" timestr)
4191 ;; substring should only run to end of time stamp
4192 (setq rest (substring timestr (match-end 0))
4193 timestr (substring timestr 0 (match-end 0)))
93b62de8 4194 (if (and (not closedp) (not statep)
c8d0cf5c 4195 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
621f83e4
CD
4196 (progn (setq timestr (concat (substring timestr 0 -1)
4197 "-" (match-string 1 rest) "]"))
4198 (setq clocked (match-string 2 rest)))
4199 (setq clocked "-")))
20908596 4200 (save-excursion
c8d0cf5c
CD
4201 (cond
4202 ((not org-agenda-log-mode-add-notes) (setq extra nil))
4203 (statep
4204 (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4205 (setq extra (match-string 1))))
4206 (clockp
4207 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4208 (setq extra (match-string 1))))
4209 (t (setq extra nil)))
4210 (if (not (re-search-backward "^\\*+ " nil t))
4211 (setq txt org-agenda-no-heading-message)
4212 (goto-char (match-beginning 0))
4213 (setq hdmarker (org-agenda-new-marker)
4214 tags (org-get-tags-at))
4215 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4216 (setq txt (match-string 1))
4217 (when extra
4218 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4219 (setq txt (concat (substring txt 0 (match-beginning 1))
4220 " - " extra " " (match-string 2 txt)))
4221 (setq txt (concat txt " - " extra))))
4222 (setq txt (org-format-agenda-item
4223 (cond
4224 (closedp "Closed: ")
93b62de8
CD
4225 (statep (concat "State: (" state ")"))
4226 (t (concat "Clocked: (" clocked ")")))
c8d0cf5c 4227 txt category tags timestr)))
20908596
CD
4228 (setq priority 100000)
4229 (org-add-props txt props
c8d0cf5c 4230 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
20908596
CD
4231 'priority priority 'org-category category
4232 'type "closed" 'date date
c8d0cf5c 4233 'undone-face 'org-warning 'done-face 'org-agenda-done)
20908596
CD
4234 (push txt ee))
4235 (goto-char (point-at-eol))))
4236 (nreverse ee)))
4237
4238(defun org-agenda-get-deadlines ()
4239 "Return the deadline information for agenda display."
4240 (let* ((props (list 'mouse-face 'highlight
4241 'org-not-done-regexp org-not-done-regexp
4242 'org-todo-regexp org-todo-regexp
b349f79f 4243 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
4244 'help-echo
4245 (format "mouse-2 or RET jump to org file %s"
4246 (abbreviate-file-name buffer-file-name))))
4247 (regexp org-deadline-time-regexp)
621f83e4 4248 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596
CD
4249 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4250 d2 diff dfrac wdays pos pos1 category tags
621f83e4 4251 ee txt head face s todo-state upcomingp donep timestr)
20908596
CD
4252 (goto-char (point-min))
4253 (while (re-search-forward regexp nil t)
4254 (catch :skip
4255 (org-agenda-skip)
4256 (setq s (match-string 1)
c8d0cf5c 4257 txt nil
20908596
CD
4258 pos (1- (match-beginning 1))
4259 d2 (org-time-string-to-absolute
4260 (match-string 1) d1 'past
4261 org-agenda-repeating-timestamp-show-all)
4262 diff (- d2 d1)
4263 wdays (org-get-wdays s)
4264 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
4265 upcomingp (and todayp (> diff 0)))
4266 ;; When to show a deadline in the calendar:
4267 ;; If the expiration is within wdays warning time.
4268 ;; Past-due deadlines are only shown on the current date
8bfe682a
CD
4269 (if (and (or (and (<= diff wdays)
4270 (and todayp (not org-agenda-only-exact-dates)))
4271 (= diff 0)))
20908596 4272 (save-excursion
621f83e4 4273 (setq todo-state (org-get-todo-state))
c8d0cf5c
CD
4274 (setq donep (member todo-state org-done-keywords))
4275 (if (and donep
4276 (or org-agenda-skip-deadline-if-done
4277 (not (= diff 0))))
4278 (setq txt nil)
4279 (setq category (org-get-category))
4280 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4281 (setq txt org-agenda-no-heading-message)
4282 (goto-char (match-end 0))
4283 (setq pos1 (match-beginning 0))
4284 (setq tags (org-get-tags-at pos1))
4285 (setq head (buffer-substring-no-properties
4286 (point)
4287 (progn (skip-chars-forward "^\r\n")
4288 (point))))
4289 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4290 (setq timestr
4291 (concat (substring s (match-beginning 1)) " "))
4292 (setq timestr 'time))
4293 (setq txt (org-format-agenda-item
4294 (if (= diff 0)
4295 (car org-agenda-deadline-leaders)
4296 (if (functionp
4297 (nth 1 org-agenda-deadline-leaders))
4298 (funcall
4299 (nth 1 org-agenda-deadline-leaders)
4300 diff date)
4301 (format (nth 1 org-agenda-deadline-leaders)
4302 diff)))
4303 head category tags
4304 (if (not (= diff 0)) nil timestr)))))
20908596
CD
4305 (when txt
4306 (setq face (org-agenda-deadline-face dfrac wdays))
4307 (org-add-props txt props
4308 'org-marker (org-agenda-new-marker pos)
4309 'org-hd-marker (org-agenda-new-marker pos1)
4310 'priority (+ (- diff)
4311 (org-get-priority txt))
4312 'org-category category
621f83e4 4313 'todo-state todo-state
20908596
CD
4314 'type (if upcomingp "upcoming-deadline" "deadline")
4315 'date (if upcomingp date d2)
c8d0cf5c
CD
4316 'face (if donep 'org-agenda-done face)
4317 'undone-face face 'done-face 'org-agenda-done)
20908596
CD
4318 (push txt ee))))))
4319 (nreverse ee)))
4320
4321(defun org-agenda-deadline-face (fraction &optional wdays)
4322 "Return the face to displaying a deadline item.
4323FRACTION is what fraction of the head-warning time has passed."
4324 (if (equal wdays 0) (setq fraction 1.))
4325 (let ((faces org-agenda-deadline-faces) f)
4326 (catch 'exit
4327 (while (setq f (pop faces))
4328 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
4329
54a0dee5 4330(defun org-agenda-get-scheduled (&optional deadline-results)
20908596
CD
4331 "Return the scheduled information for agenda display."
4332 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
4333 'org-todo-regexp org-todo-regexp
b349f79f 4334 'org-complex-heading-regexp org-complex-heading-regexp
c8d0cf5c 4335 'done-face 'org-agenda-done
20908596 4336 'mouse-face 'highlight
20908596
CD
4337 'help-echo
4338 (format "mouse-2 or RET jump to org file %s"
4339 (abbreviate-file-name buffer-file-name))))
4340 (regexp org-scheduled-time-regexp)
621f83e4 4341 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596 4342 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
54a0dee5
CD
4343 mm
4344 (deadline-position-alist
4345 (mapcar (lambda (a) (and (setq mm (get-text-property
8bfe682a
CD
4346 0 'org-hd-marker a))
4347 (cons (marker-position mm) a)))
54a0dee5 4348 deadline-results))
621f83e4 4349 d2 diff pos pos1 category tags donep
8bfe682a 4350 ee txt head pastschedp todo-state face timestr s habitp)
20908596
CD
4351 (goto-char (point-min))
4352 (while (re-search-forward regexp nil t)
4353 (catch :skip
4354 (org-agenda-skip)
4355 (setq s (match-string 1)
c8d0cf5c 4356 txt nil
20908596
CD
4357 pos (1- (match-beginning 1))
4358 d2 (org-time-string-to-absolute
4359 (match-string 1) d1 'past
4360 org-agenda-repeating-timestamp-show-all)
4361 diff (- d2 d1))
4362 (setq pastschedp (and todayp (< diff 0)))
4363 ;; When to show a scheduled item in the calendar:
4364 ;; If it is on or past the date.
8bfe682a
CD
4365 (when (or (and (< diff 0)
4366 (< (abs diff) org-scheduled-past-days)
4367 (and todayp (not org-agenda-only-exact-dates)))
4368 (= diff 0))
4369 (save-excursion
4370 (setq todo-state (org-get-todo-state))
4371 (setq donep (member todo-state org-done-keywords))
4372 (setq habitp (and (functionp 'org-is-habit-p)
4373 (org-is-habit-p)))
4374 (if (and donep
4375 (or habitp org-agenda-skip-scheduled-if-done
4376 (not (= diff 0))))
4377 (setq txt nil)
4378 (setq category (org-get-category))
4379 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4380 (setq txt org-agenda-no-heading-message)
4381 (goto-char (match-end 0))
4382 (setq pos1 (match-beginning 0))
4383 (if habitp
4384 (if (or (not org-habit-show-habits)
4385 (and (not todayp)
4386 org-habit-show-habits-only-for-today))
4387 (throw :skip nil))
54a0dee5
CD
4388 (if (and
4389 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
4390 (and org-agenda-skip-scheduled-if-deadline-is-shown
4391 pastschedp))
4392 (setq mm (assoc pos1 deadline-position-alist)))
8bfe682a
CD
4393 (throw :skip nil)))
4394 (setq tags (org-get-tags-at))
4395 (setq head (buffer-substring-no-properties
4396 (point)
4397 (progn (skip-chars-forward "^\r\n") (point))))
4398 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4399 (setq timestr
4400 (concat (substring s (match-beginning 1)) " "))
4401 (setq timestr 'time))
4402 (setq txt (org-format-agenda-item
4403 (if (= diff 0)
4404 (car org-agenda-scheduled-leaders)
4405 (format (nth 1 org-agenda-scheduled-leaders)
4406 (- 1 diff)))
4407 head category tags
4408 (if (not (= diff 0)) nil timestr)
4409 nil nil habitp))))
4410 (when txt
4411 (setq face
4412 (cond
4413 ((and (not habitp) pastschedp)
4414 'org-scheduled-previously)
4415 (todayp 'org-scheduled-today)
4416 (t 'org-scheduled))
4417 habitp (and habitp (org-habit-parse-todo)))
4418 (org-add-props txt props
4419 'undone-face face
4420 'face (if donep 'org-agenda-done face)
4421 'org-marker (org-agenda-new-marker pos)
4422 'org-hd-marker (org-agenda-new-marker pos1)
4423 'type (if pastschedp "past-scheduled" "scheduled")
4424 'date (if pastschedp d2 date)
4425 'priority (if habitp
4426 (org-habit-get-priority habitp)
4427 (+ 94 (- 5 diff) (org-get-priority txt)))
4428 'org-category category
4429 'org-habit-p habitp
4430 'todo-state todo-state)
4431 (push txt ee))))))
20908596
CD
4432 (nreverse ee)))
4433
4434(defun org-agenda-get-blocks ()
4435 "Return the date-range information for agenda display."
4436 (let* ((props (list 'face nil
4437 'org-not-done-regexp org-not-done-regexp
4438 'org-todo-regexp org-todo-regexp
b349f79f 4439 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4440 'mouse-face 'highlight
20908596
CD
4441 'help-echo
4442 (format "mouse-2 or RET jump to org file %s"
4443 (abbreviate-file-name buffer-file-name))))
4444 (regexp org-tr-regexp)
4445 (d0 (calendar-absolute-from-gregorian date))
621f83e4 4446 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
c8d0cf5c 4447 head donep)
20908596
CD
4448 (goto-char (point-min))
4449 (while (re-search-forward regexp nil t)
4450 (catch :skip
4451 (org-agenda-skip)
4452 (setq pos (point))
4453 (setq timestr (match-string 0)
4454 s1 (match-string 1)
4455 s2 (match-string 2)
4456 d1 (time-to-days (org-time-string-to-time s1))
4457 d2 (time-to-days (org-time-string-to-time s2)))
4458 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
4459 ;; Only allow days between the limits, because the normal
4460 ;; date stamps will catch the limits.
4461 (save-excursion
c8d0cf5c
CD
4462 (setq todo-state (org-get-todo-state))
4463 (setq donep (member todo-state org-done-keywords))
4464 (if (and donep org-agenda-skip-timestamp-if-done)
4465 (throw :skip t))
20908596
CD
4466 (setq marker (org-agenda-new-marker (point)))
4467 (setq category (org-get-category))
c8d0cf5c
CD
4468 (if (not (re-search-backward "^\\*+ " nil t))
4469 (setq txt org-agenda-no-heading-message)
4470 (goto-char (match-beginning 0))
4471 (setq hdmarker (org-agenda-new-marker (point)))
4472 (setq tags (org-get-tags-at))
4473 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4474 (setq head (match-string 1))
4475 (setq txt (org-format-agenda-item
4476 (format
4477 (nth (if (= d1 d2) 0 1)
4478 org-agenda-timerange-leaders)
4479 (1+ (- d0 d1)) (1+ (- d2 d1)))
4480 head category tags
4481 (if (= d0 d1) timestr))))
20908596
CD
4482 (org-add-props txt props
4483 'org-marker marker 'org-hd-marker hdmarker
4484 'type "block" 'date date
621f83e4 4485 'todo-state todo-state
20908596
CD
4486 'priority (org-get-priority txt) 'org-category category)
4487 (push txt ee)))
4488 (goto-char pos)))
4489 ;; Sort the entries by expiration date.
4490 (nreverse ee)))
4491
4492;;; Agenda presentation and sorting
4493
4494(defvar org-prefix-has-time nil
4495 "A flag, set by `org-compile-prefix-format'.
4496The flag is set if the currently compiled format contains a `%t'.")
4497(defvar org-prefix-has-tag nil
4498 "A flag, set by `org-compile-prefix-format'.
4499The flag is set if the currently compiled format contains a `%T'.")
4500(defvar org-prefix-has-effort nil
4501 "A flag, set by `org-compile-prefix-format'.
4502The flag is set if the currently compiled format contains a `%e'.")
8d642074
CD
4503(defvar org-prefix-category-length nil
4504 "Used by `org-compile-prefix-format' to remember the category field widh.")
8bfe682a
CD
4505(defvar org-prefix-category-max-length nil
4506 "Used by `org-compile-prefix-format' to remember the category field widh.")
20908596
CD
4507
4508(defun org-format-agenda-item (extra txt &optional category tags dotime
8bfe682a 4509 noprefix remove-re habitp)
20908596
CD
4510 "Format TXT to be inserted into the agenda buffer.
4511In particular, it adds the prefix and corresponding text properties. EXTRA
4512must be a string and replaces the `%s' specifier in the prefix format.
4513CATEGORY (string, symbol or nil) may be used to overrule the default
4514category taken from local variable or file name. It will replace the `%c'
4515specifier in the format. DOTIME, when non-nil, indicates that a
4516time-of-day should be extracted from TXT for sorting of this entry, and for
4517the `%t' specifier in the format. When DOTIME is a string, this string is
4518searched for a time before TXT is. NOPREFIX is a flag and indicates that
4519only the correctly processes TXT should be returned - this is used by
4520`org-agenda-change-all-lines'. TAGS can be the tags of the headline.
4521Any match of REMOVE-RE will be removed from TXT."
4522 (save-match-data
4523 ;; Diary entries sometimes have extra whitespace at the beginning
4524 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5dec9555
CD
4525
4526 ;; Fix the tags part in txt
4527 (setq txt (org-agenda-fix-displayed-tags
4528 txt tags
4529 org-agenda-show-inherited-tags
4530 org-agenda-hide-tags-regexp))
20908596
CD
4531 (let* ((category (or category
4532 org-category
4533 (if buffer-file-name
4534 (file-name-sans-extension
4535 (file-name-nondirectory buffer-file-name))
4536 "")))
4537 ;; time, tag, effort are needed for the eval of the prefix format
4538 (tag (if tags (nth (1- (length tags)) tags) ""))
4539 time effort neffort
c8d0cf5c
CD
4540 (ts (if dotime (concat
4541 (if (stringp dotime) dotime "")
4542 (and org-agenda-search-headline-for-time txt))))
20908596 4543 (time-of-day (and dotime (org-get-time-of-day ts)))
8d642074 4544 stamp plain s0 s1 s2 t1 t2 rtn srp l
8bfe682a 4545 duration thecategory)
20908596
CD
4546 (and (org-mode-p) buffer-file-name
4547 (add-to-list 'org-agenda-contributing-files buffer-file-name))
4548 (when (and dotime time-of-day)
4549 ;; Extract starting and ending time and move them to prefix
4550 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
4551 (setq plain (string-match org-plain-time-of-day-regexp ts)))
4552 (setq s0 (match-string 0 ts)
4553 srp (and stamp (match-end 3))
4554 s1 (match-string (if plain 1 2) ts)
4555 s2 (match-string (if plain 8 (if srp 4 6)) ts))
4556
4557 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
4558 ;; them, we might want to remove them there to avoid duplication.
4559 ;; The user can turn this off with a variable.
4560 (if (and org-prefix-has-time
4561 org-agenda-remove-times-when-in-prefix (or stamp plain)
4562 (string-match (concat (regexp-quote s0) " *") txt)
4563 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
4564 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
4565 (= (match-beginning 0) 0)
4566 t))
4567 (setq txt (replace-match "" nil nil txt))))
4568 ;; Normalize the time(s) to 24 hour
4569 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
4570 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
4571 ;; Compute the duration
4572 (when s1
4573 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
4574 (string-to-number (substring s1 3)))
4575 t2 (cond
4576 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
4577 (string-to-number (substring s2 3))))
4578 (org-agenda-default-appointment-duration
4579 (+ t1 org-agenda-default-appointment-duration))
4580 (t nil)))
4581 (setq duration (if t2 (- t2 t1)))))
4582
4583 (when (and s1 (not s2) org-agenda-default-appointment-duration
4584 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
4585 (let ((m (+ (string-to-number (match-string 2 s1))
4586 (* 60 (string-to-number (match-string 1 s1)))
4587 org-agenda-default-appointment-duration))
4588 h)
4589 (setq h (/ m 60) m (- m (* h 60)))
4590 (setq s2 (format "%02d:%02d" h m))))
4591
4592 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
4593 txt)
4594 ;; Tags are in the string
4595 (if (or (eq org-agenda-remove-tags t)
4596 (and org-agenda-remove-tags
4597 org-prefix-has-tag))
4598 (setq txt (replace-match "" t t txt))
4599 (setq txt (replace-match
4600 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
4601 (match-string 2 txt))
4602 t t txt))))
4603 (when (org-mode-p)
4604 (setq effort
4605 (condition-case nil
4606 (org-get-effort
4607 (or (get-text-property 0 'org-hd-marker txt)
4608 (get-text-property 0 'org-marker txt)))
4609 (error nil)))
4610 (when effort
4611 (setq neffort (org-hh:mm-string-to-minutes effort)
54a0dee5 4612 effort (setq effort (concat "[" effort "]" )))))
20908596
CD
4613
4614 (when remove-re
4615 (while (string-match remove-re txt)
4616 (setq txt (replace-match "" t t txt))))
4617
4618 ;; Create the final string
4619 (if noprefix
4620 (setq rtn txt)
4621 ;; Prepare the variables needed in the eval of the compiled format
4622 (setq time (cond (s2 (concat s1 "-" s2))
4623 (s1 (concat s1 "......"))
4624 (t ""))
8bfe682a
CD
4625 extra (or (and (not habitp) extra) "")
4626 category (if (symbolp category) (symbol-name category) category)
4627 thecategory (copy-sequence category))
4628 (if (string-match org-bracket-link-regexp category)
4629 (progn
4630 (setq l (if (match-end 3)
4631 (- (match-end 3) (match-beginning 3))
4632 (- (match-end 1) (match-beginning 1))))
4633 (when (< l (or org-prefix-category-length 0))
4634 (setq category (copy-sequence category))
4635 (org-add-props category nil
4636 'extra-space (make-string
4637 (- org-prefix-category-length l 1) ?\ ))))
4638 (if (and org-prefix-category-max-length
4639 (>= (length category) org-prefix-category-max-length))
4640 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
20908596
CD
4641 ;; Evaluate the compiled format
4642 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
4643
4644 ;; And finally add the text properties
c8d0cf5c 4645 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
20908596 4646 (org-add-props rtn nil
8bfe682a 4647 'org-category (if thecategory (downcase thecategory) category)
ff4be292 4648 'tags (mapcar 'org-downcase-keep-props tags)
20908596
CD
4649 'org-highest-priority org-highest-priority
4650 'org-lowest-priority org-lowest-priority
4651 'prefix-length (- (length rtn) (length txt))
4652 'time-of-day time-of-day
4653 'duration duration
4654 'effort effort
4655 'effort-minutes neffort
4656 'txt txt
4657 'time time
4658 'extra extra
4659 'dotime dotime))))
4660
5dec9555
CD
4661(defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
4662 "Remove tags string from TXT, and add a modified list of tags.
4663The modified list may contain inherited tags, and tags matched by
4664`org-agenda-hide-tags-regexp' will be removed."
4665 (when (or add-inherited hide-re)
4666 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
4667 (setq txt (substring txt 0 (match-beginning 0))))
4668 (when tags
4669 (setq tags
4670 (delq nil
4671 (mapcar (lambda (tg)
4672 (if (or (and hide-re (string-match hide-re tg))
4673 (and (not add-inherited)
4674 (get-text-property 0 'inherited tg)))
4675 nil
4676 tg))
4677 tags)))
4678 (let ((have-i (get-text-property 0 'inherited (car tags)))
4679 i)
4680 (setq txt (concat txt " :"
4681 (mapconcat
4682 (lambda (x)
4683 (setq i (get-text-property 0 'inherited x))
4684 (if (and have-i (not i))
4685 (progn
4686 (setq have-i nil)
4687 (concat ":" x))
4688 x))
4689 tags ":")
4690 (if have-i "::" ":"))))))
4691 txt)
ff4be292
CD
4692
4693(defun org-downcase-keep-props (s)
4694 (let ((props (text-properties-at 0 s)))
4695 (setq s (downcase s))
4696 (add-text-properties 0 (length s) props s)
4697 s))
4698
20908596
CD
4699(defvar org-agenda-sorting-strategy) ;; because the def is in a let form
4700(defvar org-agenda-sorting-strategy-selected nil)
4701
4702(defun org-agenda-add-time-grid-maybe (list ndays todayp)
4703 (catch 'exit
4704 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
4705 ((and todayp (member 'today (car org-agenda-time-grid))))
4706 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
4707 ((member 'weekly (car org-agenda-time-grid)))
4708 (t (throw 'exit list)))
4709 (let* ((have (delq nil (mapcar
4710 (lambda (x) (get-text-property 1 'time-of-day x))
4711 list)))
4712 (string (nth 1 org-agenda-time-grid))
4713 (gridtimes (nth 2 org-agenda-time-grid))
4714 (req (car org-agenda-time-grid))
4715 (remove (member 'remove-match req))
4716 new time)
4717 (if (and (member 'require-timed req) (not have))
4718 ;; don't show empty grid
4719 (throw 'exit list))
4720 (while (setq time (pop gridtimes))
4721 (unless (and remove (member time have))
4722 (setq time (int-to-string time))
4723 (push (org-format-agenda-item
4724 nil string "" nil
4725 (concat (substring time 0 -2) ":" (substring time -2)))
4726 new)
4727 (put-text-property
4728 1 (length (car new)) 'face 'org-time-grid (car new))))
4729 (if (member 'time-up org-agenda-sorting-strategy-selected)
4730 (append new list)
4731 (append list new)))))
4732
4733(defun org-compile-prefix-format (key)
4734 "Compile the prefix format into a Lisp form that can be evaluated.
4735The resulting form is returned and stored in the variable
4736`org-prefix-format-compiled'."
4737 (setq org-prefix-has-time nil org-prefix-has-tag nil
8d642074 4738 org-prefix-category-length nil org-prefix-has-effort nil)
20908596
CD
4739 (let ((s (cond
4740 ((stringp org-agenda-prefix-format)
4741 org-agenda-prefix-format)
4742 ((assq key org-agenda-prefix-format)
4743 (cdr (assq key org-agenda-prefix-format)))
4744 (t " %-12:c%?-12t% s")))
4745 (start 0)
4746 varform vars var e c f opt)
4747 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
4748 s start)
4749 (setq var (cdr (assoc (match-string 4 s)
4750 '(("c" . category) ("t" . time) ("s" . extra)
4751 ("T" . tag) ("e" . effort))))
4752 c (or (match-string 3 s) "")
4753 opt (match-beginning 1)
4754 start (1+ (match-beginning 0)))
4755 (if (equal var 'time) (setq org-prefix-has-time t))
4756 (if (equal var 'tag) (setq org-prefix-has-tag t))
4757 (if (equal var 'effort) (setq org-prefix-has-effort t))
4758 (setq f (concat "%" (match-string 2 s) "s"))
8bfe682a
CD
4759 (when (equal var 'category)
4760 (setq org-prefix-category-length
4761 (floor (abs (string-to-number (match-string 2 s)))))
4762 (setq org-prefix-category-max-length
4763 (let ((x (match-string 2 s)))
4764 (save-match-data
4765 (if (string-match "\\.[0-9]+" x)
4766 (string-to-number (substring (match-string 0 x) 1)))))))
20908596
CD
4767 (if opt
4768 (setq varform
4769 `(if (equal "" ,var)
4770 ""
4771 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
8d642074 4772 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var))))))
20908596
CD
4773 (setq s (replace-match "%s" t nil s))
4774 (push varform vars))
4775 (setq vars (nreverse vars))
4776 (setq org-prefix-format-compiled `(format ,s ,@vars))))
4777
4778(defun org-set-sorting-strategy (key)
4779 (if (symbolp (car org-agenda-sorting-strategy))
4780 ;; the old format
4781 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
4782 (setq org-agenda-sorting-strategy-selected
4783 (or (cdr (assq key org-agenda-sorting-strategy))
4784 (cdr (assq 'agenda org-agenda-sorting-strategy))
4785 '(time-up category-keep priority-down)))))
4786
4787(defun org-get-time-of-day (s &optional string mod24)
4788 "Check string S for a time of day.
4789If found, return it as a military time number between 0 and 2400.
4790If not found, return nil.
4791The optional STRING argument forces conversion into a 5 character wide string
4792HH:MM."
4793 (save-match-data
4794 (when
4795 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
4796 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
4797 (let* ((h (string-to-number (match-string 1 s)))
4798 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
4799 (ampm (if (match-end 4) (downcase (match-string 4 s))))
4800 (am-p (equal ampm "am"))
4801 (h1 (cond ((not ampm) h)
4802 ((= h 12) (if am-p 0 12))
4803 (t (+ h (if am-p 0 12)))))
4804 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
4805 (mod h1 24) h1))
4806 (t0 (+ (* 100 h2) m))
4807 (t1 (concat (if (>= h1 24) "+" " ")
4808 (if (< t0 100) "0" "")
4809 (if (< t0 10) "0" "")
4810 (int-to-string t0))))
4811 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
4812
4813(defun org-finalize-agenda-entries (list &optional nosort)
4814 "Sort and concatenate the agenda items."
4815 (setq list (mapcar 'org-agenda-highlight-todo list))
4816 (if nosort
4817 list
4818 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
4819
4820(defun org-agenda-highlight-todo (x)
621f83e4
CD
4821 (let ((org-done-keywords org-done-keywords-for-agenda)
4822 re pl)
20908596
CD
4823 (if (eq x 'line)
4824 (save-excursion
4825 (beginning-of-line 1)
8d642074
CD
4826 (setq re (org-get-at-bol 'org-todo-regexp))
4827 (goto-char (+ (point) (or (org-get-at-bol 'prefix-length) 0)))
621f83e4 4828 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
c8d0cf5c 4829 (add-text-properties (match-beginning 0) (match-end 1)
621f83e4 4830 (list 'face (org-get-todo-face 1)))
20908596
CD
4831 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
4832 (delete-region (match-beginning 1) (1- (match-end 0)))
4833 (goto-char (match-beginning 1))
4834 (insert (format org-agenda-todo-keyword-format s)))))
4835 (setq re (concat (get-text-property 0 'org-todo-regexp x))
4836 pl (get-text-property 0 'prefix-length x))
4837 (when (and re
4838 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
4839 x (or pl 0)) pl))
4840 (add-text-properties
4841 (or (match-end 1) (match-end 0)) (match-end 0)
4842 (list 'face (org-get-todo-face (match-string 2 x)))
4843 x)
4844 (setq x (concat (substring x 0 (match-end 1))
4845 (format org-agenda-todo-keyword-format
4846 (match-string 2 x))
8d642074 4847 (org-add-props " " (text-properties-at 0 x))
20908596
CD
4848 (substring x (match-end 3)))))
4849 x)))
4850
4851(defsubst org-cmp-priority (a b)
4852 "Compare the priorities of string A and B."
4853 (let ((pa (or (get-text-property 1 'priority a) 0))
4854 (pb (or (get-text-property 1 'priority b) 0)))
4855 (cond ((> pa pb) +1)
4856 ((< pa pb) -1)
4857 (t nil))))
4858
4859(defsubst org-cmp-effort (a b)
4860 "Compare the priorities of string A and B."
4861 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
4862 (ea (or (get-text-property 1 'effort-minutes a) def))
4863 (eb (or (get-text-property 1 'effort-minutes b) def)))
4864 (cond ((> ea eb) +1)
4865 ((< ea eb) -1)
4866 (t nil))))
4867
4868(defsubst org-cmp-category (a b)
4869 "Compare the string values of categories of strings A and B."
4870 (let ((ca (or (get-text-property 1 'org-category a) ""))
4871 (cb (or (get-text-property 1 'org-category b) "")))
4872 (cond ((string-lessp ca cb) -1)
4873 ((string-lessp cb ca) +1)
4874 (t nil))))
4875
621f83e4
CD
4876(defsubst org-cmp-todo-state (a b)
4877 "Compare the todo states of strings A and B."
c8d0cf5c
CD
4878 (let* ((ma (or (get-text-property 1 'org-marker a)
4879 (get-text-property 1 'org-hd-marker a)))
4880 (mb (or (get-text-property 1 'org-marker b)
4881 (get-text-property 1 'org-hd-marker b)))
4882 (fa (and ma (marker-buffer ma)))
4883 (fb (and mb (marker-buffer mb)))
4884 (todo-kwds
4885 (or (and fa (with-current-buffer fa org-todo-keywords-1))
4886 (and fb (with-current-buffer fb org-todo-keywords-1))))
4887 (ta (or (get-text-property 1 'todo-state a) ""))
621f83e4 4888 (tb (or (get-text-property 1 'todo-state b) ""))
c8d0cf5c
CD
4889 (la (- (length (member ta todo-kwds))))
4890 (lb (- (length (member tb todo-kwds))))
ff4be292 4891 (donepa (member ta org-done-keywords-for-agenda))
621f83e4
CD
4892 (donepb (member tb org-done-keywords-for-agenda)))
4893 (cond ((and donepa (not donepb)) -1)
4894 ((and (not donepa) donepb) +1)
4895 ((< la lb) -1)
4896 ((< lb la) +1)
4897 (t nil))))
4898
20908596 4899(defsubst org-cmp-tag (a b)
71d35b24 4900 "Compare the string values of the first tags of A and B."
20908596
CD
4901 (let ((ta (car (last (get-text-property 1 'tags a))))
4902 (tb (car (last (get-text-property 1 'tags b)))))
4903 (cond ((not ta) +1)
4904 ((not tb) -1)
4905 ((string-lessp ta tb) -1)
4906 ((string-lessp tb ta) +1)
4907 (t nil))))
4908
4909(defsubst org-cmp-time (a b)
4910 "Compare the time-of-day values of strings A and B."
4911 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
4912 (ta (or (get-text-property 1 'time-of-day a) def))
4913 (tb (or (get-text-property 1 'time-of-day b) def)))
4914 (cond ((< ta tb) -1)
4915 ((< tb ta) +1)
4916 (t nil))))
4917
8bfe682a
CD
4918(defsubst org-cmp-habit-p (a b)
4919 "Compare the todo states of strings A and B."
4920 (let ((ha (get-text-property 1 'org-habit-p a))
4921 (hb (get-text-property 1 'org-habit-p b)))
4922 (cond ((and ha (not hb)) -1)
4923 ((and (not ha) hb) +1)
4924 (t nil))))
4925
20908596
CD
4926(defun org-entries-lessp (a b)
4927 "Predicate for sorting agenda entries."
4928 ;; The following variables will be used when the form is evaluated.
4929 ;; So even though the compiler complains, keep them.
4930 (let* ((time-up (org-cmp-time a b))
4931 (time-down (if time-up (- time-up) nil))
4932 (priority-up (org-cmp-priority a b))
4933 (priority-down (if priority-up (- priority-up) nil))
4934 (effort-up (org-cmp-effort a b))
4935 (effort-down (if effort-up (- effort-up) nil))
4936 (category-up (org-cmp-category a b))
4937 (category-down (if category-up (- category-up) nil))
4938 (category-keep (if category-up +1 nil))
4939 (tag-up (org-cmp-tag a b))
621f83e4
CD
4940 (tag-down (if tag-up (- tag-up) nil))
4941 (todo-state-up (org-cmp-todo-state a b))
c8d0cf5c 4942 (todo-state-down (if todo-state-up (- todo-state-up) nil))
8bfe682a
CD
4943 (habit-up (org-cmp-habit-p a b))
4944 (habit-down (if habit-up (- habit-up) nil))
c8d0cf5c
CD
4945 user-defined-up user-defined-down)
4946 (if (and org-agenda-cmp-user-defined
4947 (functionp org-agenda-cmp-user-defined))
4948 (setq user-defined-up
4949 (funcall org-agenda-cmp-user-defined a b)
4950 user-defined-down (if user-defined-up (- user-defined-up) nil)))
20908596
CD
4951 (cdr (assoc
4952 (eval (cons 'or org-agenda-sorting-strategy-selected))
4953 '((-1 . t) (1 . nil) (nil . nil))))))
4954
4955;;; Agenda restriction lock
4956
4957(defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
8bfe682a 4958 "Overlay to mark the headline to which agenda commands are restricted.")
20908596
CD
4959(org-overlay-put org-agenda-restriction-lock-overlay
4960 'face 'org-agenda-restriction-lock)
4961(org-overlay-put org-agenda-restriction-lock-overlay
4962 'help-echo "Agendas are currently limited to this subtree.")
4963(org-detach-overlay org-agenda-restriction-lock-overlay)
4964
4965(defun org-agenda-set-restriction-lock (&optional type)
4966 "Set restriction lock for agenda, to current subtree or file.
4967Restriction will be the file if TYPE is `file', or if type is the
4968universal prefix '(4), or if the cursor is before the first headline
4969in the file. Otherwise, restriction will be to the current subtree."
4970 (interactive "P")
4971 (and (equal type '(4)) (setq type 'file))
4972 (setq type (cond
4973 (type type)
4974 ((org-at-heading-p) 'subtree)
4975 ((condition-case nil (org-back-to-heading t) (error nil))
4976 'subtree)
4977 (t 'file)))
4978 (if (eq type 'subtree)
4979 (progn
4980 (setq org-agenda-restrict t)
4981 (setq org-agenda-overriding-restriction 'subtree)
4982 (put 'org-agenda-files 'org-restrict
4983 (list (buffer-file-name (buffer-base-buffer))))
4984 (org-back-to-heading t)
4985 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
4986 (move-marker org-agenda-restrict-begin (point))
4987 (move-marker org-agenda-restrict-end
4988 (save-excursion (org-end-of-subtree t)))
4989 (message "Locking agenda restriction to subtree"))
4990 (put 'org-agenda-files 'org-restrict
4991 (list (buffer-file-name (buffer-base-buffer))))
4992 (setq org-agenda-restrict nil)
4993 (setq org-agenda-overriding-restriction 'file)
4994 (move-marker org-agenda-restrict-begin nil)
4995 (move-marker org-agenda-restrict-end nil)
4996 (message "Locking agenda restriction to file"))
4997 (setq current-prefix-arg nil)
4998 (org-agenda-maybe-redo))
4999
5000(defun org-agenda-remove-restriction-lock (&optional noupdate)
5001 "Remove the agenda restriction lock."
5002 (interactive "P")
5003 (org-detach-overlay org-agenda-restriction-lock-overlay)
5004 (org-detach-overlay org-speedbar-restriction-lock-overlay)
5005 (setq org-agenda-overriding-restriction nil)
5006 (setq org-agenda-restrict nil)
5007 (put 'org-agenda-files 'org-restrict nil)
5008 (move-marker org-agenda-restrict-begin nil)
5009 (move-marker org-agenda-restrict-end nil)
5010 (setq current-prefix-arg nil)
5011 (message "Agenda restriction lock removed")
5012 (or noupdate (org-agenda-maybe-redo)))
5013
5014(defun org-agenda-maybe-redo ()
5015 "If there is any window showing the agenda view, update it."
5016 (let ((w (get-buffer-window org-agenda-buffer-name t))
5017 (w0 (selected-window)))
5018 (when w
5019 (select-window w)
5020 (org-agenda-redo)
5021 (select-window w0)
5022 (if org-agenda-overriding-restriction
5023 (message "Agenda view shifted to new %s restriction"
5024 org-agenda-overriding-restriction)
5025 (message "Agenda restriction lock removed")))))
5026
5027;;; Agenda commands
5028
5029(defun org-agenda-check-type (error &rest types)
5030 "Check if agenda buffer is of allowed type.
5031If ERROR is non-nil, throw an error, otherwise just return nil."
5032 (if (memq org-agenda-type types)
5033 t
5034 (if error
5035 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
5036 nil)))
5037
5038(defun org-agenda-quit ()
5039 "Exit agenda by removing the window or the buffer."
5040 (interactive)
5041 (if org-agenda-columns-active
5042 (org-columns-quit)
5043 (let ((buf (current-buffer)))
8d642074
CD
5044 (if (eq org-agenda-window-setup 'other-frame)
5045 (progn
5046 (kill-buffer buf)
5047 (org-agenda-reset-markers)
5048 (org-columns-remove-overlays)
5049 (setq org-agenda-archives-mode nil)
5050 (delete-frame))
5051 (and (not (eq org-agenda-window-setup 'current-window))
5052 (not (one-window-p))
5053 (delete-window))
5054 (kill-buffer buf)
5055 (org-agenda-reset-markers)
5056 (org-columns-remove-overlays)
5057 (setq org-agenda-archives-mode nil)))
20908596
CD
5058 ;; Maybe restore the pre-agenda window configuration.
5059 (and org-agenda-restore-windows-after-quit
5060 (not (eq org-agenda-window-setup 'other-frame))
5061 org-pre-agenda-window-conf
5062 (set-window-configuration org-pre-agenda-window-conf))))
5063
5064(defun org-agenda-exit ()
5065 "Exit agenda by removing the window or the buffer.
5066Also kill all Org-mode buffers which have been loaded by `org-agenda'.
5067Org-mode buffers visited directly by the user will not be touched."
5068 (interactive)
5069 (org-release-buffers org-agenda-new-buffers)
5070 (setq org-agenda-new-buffers nil)
5071 (org-agenda-quit))
5072
5073(defun org-agenda-execute (arg)
5074 "Execute another agenda command, keeping same window.\\<global-map>
5075So this is just a shortcut for `\\[org-agenda]', available in the agenda."
5076 (interactive "P")
5077 (let ((org-agenda-window-setup 'current-window))
5078 (org-agenda arg)))
5079
20908596
CD
5080(defun org-agenda-redo ()
5081 "Rebuild Agenda.
5082When this is the global TODO list, a prefix argument will be interpreted."
5083 (interactive)
5084 (let* ((org-agenda-keep-modes t)
71d35b24 5085 (filter org-agenda-filter)
c8d0cf5c 5086 (preset (get 'org-agenda-filter :preset-filter))
20908596
CD
5087 (cols org-agenda-columns-active)
5088 (line (org-current-line))
5089 (window-line (- line (org-current-line (window-start))))
5090 (lprops (get 'org-agenda-redo-command 'org-lprops)))
c8d0cf5c 5091 (put 'org-agenda-filter :preset-filter nil)
20908596
CD
5092 (and cols (org-columns-quit))
5093 (message "Rebuilding agenda buffer...")
5094 (org-let lprops '(eval org-agenda-redo-command))
5095 (setq org-agenda-undo-list nil
5096 org-agenda-pending-undo-list nil)
5097 (message "Rebuilding agenda buffer...done")
c8d0cf5c
CD
5098 (put 'org-agenda-filter :preset-filter preset)
5099 (and (or filter preset) (org-agenda-filter-apply filter))
20908596 5100 (and cols (interactive-p) (org-agenda-columns))
54a0dee5 5101 (org-goto-line line)
20908596
CD
5102 (recenter window-line)))
5103
71d35b24 5104
621f83e4 5105(defvar org-global-tags-completion-table nil)
71d35b24
CD
5106(defvar org-agenda-filter-form nil)
5107(defun org-agenda-filter-by-tag (strip &optional char narrow)
621f83e4
CD
5108 "Keep only those lines in the agenda buffer that have a specific tag.
5109The tag is selected with its fast selection letter, as configured.
71d35b24
CD
5110With prefix argument STRIP, remove all lines that do have the tag.
5111A lisp caller can specify CHAR. NARROW means that the new tag should be
5112used to narrow the search - the interactive user can also press `-' or `+'
5113to switch to narrowing."
621f83e4 5114 (interactive "P")
71d35b24 5115 (let* ((alist org-tag-alist-for-agenda)
8bfe682a
CD
5116 (tag-chars (mapconcat
5117 (lambda (x) (if (and (not (symbolp (car x)))
5118 (cdr x))
5119 (char-to-string (cdr x))
5120 ""))
5121 alist ""))
5122 (efforts (org-split-string
5123 (or (cdr (assoc (concat org-effort-property "_ALL")
5124 org-global-properties))
5125 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
5126 (effort-op org-agenda-filter-effort-default-operator)
5127 (effort-prompt "")
5128 (inhibit-read-only t)
5129 (current org-agenda-filter)
5130 char a n tag)
71d35b24 5131 (unless char
ff4be292 5132 (message
8bfe682a
CD
5133 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
5134 (if narrow "Narrow" "Filter") tag-chars
5135 (if org-agenda-auto-exclude-function "[RET], " ""))
71d35b24
CD
5136 (setq char (read-char)))
5137 (when (member char '(?+ ?-))
5138 ;; Narrowing down
5139 (cond ((equal char ?-) (setq strip t narrow t))
5140 ((equal char ?+) (setq strip nil narrow t)))
ff4be292 5141 (message
71d35b24
CD
5142 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
5143 (setq char (read-char)))
c8d0cf5c 5144 (when (member char '(?< ?> ?= ??))
71d35b24
CD
5145 ;; An effort operator
5146 (setq effort-op (char-to-string char))
71d35b24 5147 (setq alist nil) ; to make sure it will be interpreted as effort.
c8d0cf5c
CD
5148 (unless (equal char ??)
5149 (loop for i from 0 to 9 do
5150 (setq effort-prompt
5151 (concat
5152 effort-prompt " ["
5153 (if (= i 9) "0" (int-to-string (1+ i)))
5154 "]" (nth i efforts))))
5155 (message "Effort%s: %s " effort-op effort-prompt)
5156 (setq char (read-char))
5157 (when (or (< char ?0) (> char ?9))
5158 (error "Need 1-9,0 to select effort" ))))
71d35b24
CD
5159 (when (equal char ?\t)
5160 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
5161 (org-set-local 'org-global-tags-completion-table
5162 (org-global-tags-completion-table)))
5163 (let ((completion-ignore-case t))
54a0dee5 5164 (setq tag (org-icompleting-read
71d35b24
CD
5165 "Tag: " org-global-tags-completion-table))))
5166 (cond
8bfe682a
CD
5167 ((equal char ?\r)
5168 (org-agenda-filter-by-tag-show-all)
5169 (when org-agenda-auto-exclude-function
5170 (setq org-agenda-filter '())
5171 (dolist (tag org-tag-alist-for-agenda)
5172 (let ((modifier (funcall org-agenda-auto-exclude-function
5173 (car tag))))
5174 (if modifier
5175 (push modifier org-agenda-filter))))
5176 (if (not (null org-agenda-filter))
5177 (org-agenda-filter-apply org-agenda-filter))))
c8d0cf5c
CD
5178 ((equal char ?/)
5179 (org-agenda-filter-by-tag-show-all)
5180 (when (get 'org-agenda-filter :preset-filter)
5181 (org-agenda-filter-apply org-agenda-filter)))
71d35b24
CD
5182 ((or (equal char ?\ )
5183 (setq a (rassoc char alist))
5184 (and (>= char ?0) (<= char ?9)
5185 (setq n (if (= char ?0) 9 (- char ?0 1))
5186 tag (concat effort-op (nth n efforts))
5187 a (cons tag nil)))
c8d0cf5c
CD
5188 (and (= char ??)
5189 (setq tag "?eff")
5190 a (cons tag nil))
71d35b24
CD
5191 (and tag (setq a (cons tag nil))))
5192 (org-agenda-filter-by-tag-show-all)
5193 (setq tag (car a))
5194 (setq org-agenda-filter
5195 (cons (concat (if strip "-" "+") tag)
5196 (if narrow current nil)))
5197 (org-agenda-filter-apply org-agenda-filter))
5198 (t (error "Invalid tag selection character %c" char)))))
5199
5200(defun org-agenda-filter-by-tag-refine (strip &optional char)
5201 "Refine the current filter. See `org-agenda-filter-by-tag."
5202 (interactive "P")
5203 (org-agenda-filter-by-tag strip char 'refine))
5204
5205(defun org-agenda-filter-make-matcher ()
5206 "Create the form that tests a line for the agenda filter."
5207 (let (f f1)
c8d0cf5c
CD
5208 (dolist (x (append (get 'org-agenda-filter :preset-filter)
5209 org-agenda-filter))
71d35b24 5210 (if (member x '("-" "+"))
8bfe682a 5211 (setq f1 (if (equal x "-") 'tags '(not tags)))
c8d0cf5c 5212 (if (string-match "[<=>?]" x)
71d35b24
CD
5213 (setq f1 (org-agenda-filter-effort-form x))
5214 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
5215 (if (equal (string-to-char x) ?-)
5216 (setq f1 (list 'not f1))))
5217 (push f1 f))
5218 (cons 'and (nreverse f))))
5219
5220(defun org-agenda-filter-effort-form (e)
5221 "Return the form to compare the effort of the current line with what E says.
5222E looks line \"+<2:25\"."
5223 (let (op)
5224 (setq e (substring e 1))
5225 (setq op (string-to-char e) e (substring e 1))
c8d0cf5c
CD
5226 (setq op (cond ((equal op ?<) '<=)
5227 ((equal op ?>) '>=)
5228 ((equal op ??) op)
5229 (t '=)))
71d35b24
CD
5230 (list 'org-agenda-compare-effort (list 'quote op)
5231 (org-hh:mm-string-to-minutes e))))
5232
5233(defun org-agenda-compare-effort (op value)
5234 "Compare the effort of the current line with VALUE, using OP.
5235If the line does not have an effort defined, return nil."
8d642074 5236 (let ((eff (org-get-at-bol 'effort-minutes)))
c8d0cf5c
CD
5237 (if (equal op ??)
5238 (not eff)
5239 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
5240 value))))
71d35b24
CD
5241
5242(defun org-agenda-filter-apply (filter)
5243 "Set FILTER as the new agenda filter and apply it."
5244 (let (tags)
5245 (setq org-agenda-filter filter
5246 org-agenda-filter-form (org-agenda-filter-make-matcher))
5247 (org-agenda-set-mode-name)
5248 (save-excursion
5249 (goto-char (point-min))
5250 (while (not (eobp))
8d642074 5251 (if (org-get-at-bol 'org-marker)
71d35b24 5252 (progn
8d642074 5253 (setq tags (org-get-at-bol 'tags)) ; used in eval
71d35b24
CD
5254 (if (not (eval org-agenda-filter-form))
5255 (org-agenda-filter-by-tag-hide-line))
5256 (beginning-of-line 2))
5257 (beginning-of-line 2))))))
621f83e4 5258
621f83e4
CD
5259(defun org-agenda-filter-by-tag-hide-line ()
5260 (let (ov)
5261 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
5262 (point-at-eol)))
5263 (org-overlay-put ov 'invisible t)
5264 (org-overlay-put ov 'type 'tags-filter)
5265 (push ov org-agenda-filter-overlays)))
5266
71d35b24
CD
5267(defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
5268 (setq pos (or pos (point)))
5269 (save-excursion
5270 (dolist (ov (org-overlays-at pos))
5271 (when (and (org-overlay-get ov 'invisible)
5272 (eq (org-overlay-get ov 'type) 'tags-filter))
5273 (goto-char pos)
5274 (if (< (org-overlay-start ov) (point-at-eol))
5275 (org-move-overlay ov (point-at-eol)
5276 (org-overlay-end ov)))))))
5277
621f83e4
CD
5278(defun org-agenda-filter-by-tag-show-all ()
5279 (mapc 'org-delete-overlay org-agenda-filter-overlays)
71d35b24
CD
5280 (setq org-agenda-filter-overlays nil)
5281 (setq org-agenda-filter nil)
5282 (setq org-agenda-filter-form nil)
5283 (org-agenda-set-mode-name))
621f83e4 5284
20908596
CD
5285(defun org-agenda-manipulate-query-add ()
5286 "Manipulate the query by adding a search term with positive selection.
5287Positive selection means, the term must be matched for selection of an entry."
5288 (interactive)
5289 (org-agenda-manipulate-query ?\[))
5290(defun org-agenda-manipulate-query-subtract ()
5291 "Manipulate the query by adding a search term with negative selection.
5292Negative selection means, term must not be matched for selection of an entry."
5293 (interactive)
5294 (org-agenda-manipulate-query ?\]))
5295(defun org-agenda-manipulate-query-add-re ()
5296 "Manipulate the query by adding a search regexp with positive selection.
5297Positive selection means, the regexp must match for selection of an entry."
5298 (interactive)
5299 (org-agenda-manipulate-query ?\{))
5300(defun org-agenda-manipulate-query-subtract-re ()
5301 "Manipulate the query by adding a search regexp with negative selection.
5302Negative selection means, regexp must not match for selection of an entry."
5303 (interactive)
5304 (org-agenda-manipulate-query ?\}))
5305(defun org-agenda-manipulate-query (char)
5306 (cond
5307 ((memq org-agenda-type '(timeline agenda))
54a0dee5
CD
5308 (let ((org-agenda-include-inactive-timestamps t))
5309 (org-agenda-redo))
5310 (message "Display now includes inactive timestamps as well"))
20908596
CD
5311 ((eq org-agenda-type 'search)
5312 (org-add-to-string
5313 'org-agenda-query-string
5314 (cdr (assoc char '((?\[ . " +") (?\] . " -")
5315 (?\{ . " +{}") (?\} . " -{}")))))
5316 (setq org-agenda-redo-command
5317 (list 'org-search-view
5318 org-todo-only
5319 org-agenda-query-string
5320 (+ (length org-agenda-query-string)
5321 (if (member char '(?\{ ?\})) 0 1))))
5322 (set-register org-agenda-query-register org-agenda-query-string)
5323 (org-agenda-redo))
5324 (t (error "Cannot manipulate query for %s-type agenda buffers"
5325 org-agenda-type))))
5326
5327(defun org-add-to-string (var string)
5328 (set var (concat (symbol-value var) string)))
5329
5330(defun org-agenda-goto-date (date)
5331 "Jump to DATE in agenda."
5332 (interactive (list (org-read-date)))
5333 (org-agenda-list nil date))
5334
5335(defun org-agenda-goto-today ()
5336 "Go to today."
5337 (interactive)
5338 (org-agenda-check-type t 'timeline 'agenda)
5339 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
5340 (cond
5341 (tdpos (goto-char tdpos))
5342 ((eq org-agenda-type 'agenda)
5343 (let* ((sd (time-to-days
5344 (time-subtract (current-time)
5345 (list 0 (* 3600 org-extend-today-until) 0))))
5346 (comp (org-agenda-compute-time-span sd org-agenda-span))
5347 (org-agenda-overriding-arguments org-agenda-last-arguments))
5348 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
5349 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
5350 (org-agenda-redo)
5351 (org-agenda-find-same-or-today-or-agenda)))
5352 (t (error "Cannot find today")))))
5353
5354(defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
5355 (goto-char
5356 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
5357 (text-property-any (point-min) (point-max) 'org-today t)
5358 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
5359 (point-min))))
5360
5361(defun org-agenda-later (arg)
5362 "Go forward in time by thee current span.
5363With prefix ARG, go forward that many times the current span."
5364 (interactive "p")
5365 (org-agenda-check-type t 'agenda)
5366 (let* ((span org-agenda-span)
5367 (sd org-starting-day)
5368 (greg (calendar-gregorian-from-absolute sd))
8d642074 5369 (cnt (org-get-at-bol 'org-day-cnt))
20908596
CD
5370 greg2 nd)
5371 (cond
5372 ((eq span 'day)
5373 (setq sd (+ arg sd) nd 1))
5374 ((eq span 'week)
5375 (setq sd (+ (* 7 arg) sd) nd 7))
5376 ((eq span 'month)
5377 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
5378 sd (calendar-absolute-from-gregorian greg2))
5379 (setcar greg2 (1+ (car greg2)))
5380 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
5381 ((eq span 'year)
5382 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
5383 sd (calendar-absolute-from-gregorian greg2))
5384 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
5385 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
5386 (let ((org-agenda-overriding-arguments
5387 (list (car org-agenda-last-arguments) sd nd t)))
5388 (org-agenda-redo)
5389 (org-agenda-find-same-or-today-or-agenda cnt))))
5390
5391(defun org-agenda-earlier (arg)
5392 "Go backward in time by the current span.
5393With prefix ARG, go backward that many times the current span."
5394 (interactive "p")
5395 (org-agenda-later (- arg)))
5396
c8d0cf5c
CD
5397(defun org-agenda-view-mode-dispatch ()
5398 "Call one of the view mode commands."
5399 (interactive)
5400 (message "View: [d]ay [w]eek [m]onth [y]ear [l]og [L]og-all [a]rch-trees [A]rch-files
54a0dee5 5401 clock[R]eport time[G]rid [[]inactive [E]ntryText include[D]iary")
c8d0cf5c
CD
5402 (let ((a (read-char-exclusive)))
5403 (case a
5404 (?d (call-interactively 'org-agenda-day-view))
5405 (?w (call-interactively 'org-agenda-week-view))
5406 (?m (call-interactively 'org-agenda-month-view))
5407 (?y (call-interactively 'org-agenda-year-view))
5408 (?l (call-interactively 'org-agenda-log-mode))
54a0dee5 5409 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
c8d0cf5c
CD
5410 (?a (call-interactively 'org-agenda-archives-mode))
5411 (?A (org-agenda-archives-mode 'files))
54a0dee5
CD
5412 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
5413 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
c8d0cf5c
CD
5414 (?G (call-interactively 'org-agenda-toggle-time-grid))
5415 (?D (call-interactively 'org-agenda-toggle-diary))
54a0dee5
CD
5416 (?\[ (let ((org-agenda-include-inactive-timestamps t))
5417 (org-agenda-check-type t 'timeline 'agenda)
5418 (org-agenda-redo))
5419 (message "Display now includes inactive timestamps as well"))
c8d0cf5c
CD
5420 (?q (message "Abort"))
5421 (otherwise (error "Invalid key" )))))
5422
20908596
CD
5423(defun org-agenda-day-view (&optional day-of-year)
5424 "Switch to daily view for agenda.
5425With argument DAY-OF-YEAR, switch to that day of the year."
5426 (interactive "P")
5427 (setq org-agenda-ndays 1)
5428 (org-agenda-change-time-span 'day day-of-year))
5429(defun org-agenda-week-view (&optional iso-week)
5430 "Switch to daily view for agenda.
5431With argument ISO-WEEK, switch to the corresponding ISO week.
5432If ISO-WEEK has more then 2 digits, only the last two encode the
5433week. Any digits before this encode a year. So 200712 means
5434week 12 of year 2007. Years in the range 1938-2037 can also be
5435written as 2-digit years."
5436 (interactive "P")
5437 (setq org-agenda-ndays 7)
5438 (org-agenda-change-time-span 'week iso-week))
5439(defun org-agenda-month-view (&optional month)
b349f79f 5440 "Switch to monthly view for agenda.
20908596
CD
5441With argument MONTH, switch to that month."
5442 (interactive "P")
5443 (org-agenda-change-time-span 'month month))
5444(defun org-agenda-year-view (&optional year)
b349f79f 5445 "Switch to yearly view for agenda.
20908596
CD
5446With argument YEAR, switch to that year.
5447If MONTH has more then 2 digits, only the last two encode the
5448month. Any digits before this encode a year. So 200712 means
5449December year 2007. Years in the range 1938-2037 can also be
5450written as 2-digit years."
5451 (interactive "P")
5452 (when year
5453 (setq year (org-small-year-to-year year)))
5454 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
5455 (org-agenda-change-time-span 'year year)
5456 (error "Abort")))
5457
5458(defun org-agenda-change-time-span (span &optional n)
5459 "Change the agenda view to SPAN.
5460SPAN may be `day', `week', `month', `year'."
5461 (org-agenda-check-type t 'agenda)
5462 (if (and (not n) (equal org-agenda-span span))
5463 (error "Viewing span is already \"%s\"" span))
8d642074 5464 (let* ((sd (or (org-get-at-bol 'day)
20908596
CD
5465 org-starting-day))
5466 (computed (org-agenda-compute-time-span sd span n))
5467 (org-agenda-overriding-arguments
5468 (list (car org-agenda-last-arguments)
5469 (car computed) (cdr computed) t)))
5470 (org-agenda-redo)
5471 (org-agenda-find-same-or-today-or-agenda))
5472 (org-agenda-set-mode-name)
5473 (message "Switched to %s view" span))
5474
5475(defun org-agenda-compute-time-span (sd span &optional n)
5476 "Compute starting date and number of days for agenda.
5477SPAN may be `day', `week', `month', `year'. The return value
5478is a cons cell with the starting date and the number of days,
5479so that the date SD will be in that range."
5480 (let* ((greg (calendar-gregorian-from-absolute sd))
5481 (dg (nth 1 greg))
5482 (mg (car greg))
5483 (yg (nth 2 greg))
5484 nd w1 y1 m1 thisweek)
5485 (cond
5486 ((eq span 'day)
5487 (when n
5488 (setq sd (+ (calendar-absolute-from-gregorian
5489 (list mg 1 yg))
5490 n -1)))
5491 (setq nd 1))
5492 ((eq span 'week)
5493 (let* ((nt (calendar-day-of-week
5494 (calendar-gregorian-from-absolute sd)))
5495 (d (if org-agenda-start-on-weekday
5496 (- nt org-agenda-start-on-weekday)
5497 0)))
5498 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
5499 (when n
5500 (require 'cal-iso)
5501 (setq thisweek (car (calendar-iso-from-absolute sd)))
5502 (when (> n 99)
5503 (setq y1 (org-small-year-to-year (/ n 100))
5504 n (mod n 100)))
5505 (setq sd
5506 (calendar-absolute-from-iso
5507 (list n 1
5508 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
5509 (setq nd 7)))
5510 ((eq span 'month)
5511 (when (and n (> n 99))
5512 (setq y1 (org-small-year-to-year (/ n 100))
5513 n (mod n 100)))
5514 (setq sd (calendar-absolute-from-gregorian
5515 (list (or n mg) 1 (or y1 yg)))
5516 nd (- (calendar-absolute-from-gregorian
5517 (list (1+ (or n mg)) 1 (or y1 yg)))
5518 sd)))
5519 ((eq span 'year)
5520 (setq sd (calendar-absolute-from-gregorian
5521 (list 1 1 (or n yg)))
5522 nd (- (calendar-absolute-from-gregorian
5523 (list 1 1 (1+ (or n yg))))
5524 sd))))
5525 (cons sd nd)))
5526
5527(defun org-agenda-next-date-line (&optional arg)
5528 "Jump to the next line indicating a date in agenda buffer."
5529 (interactive "p")
5530 (org-agenda-check-type t 'agenda 'timeline)
5531 (beginning-of-line 1)
5532 ;; This does not work if user makes date format that starts with a blank
5533 (if (looking-at "^\\S-") (forward-char 1))
5534 (if (not (re-search-forward "^\\S-" nil t arg))
5535 (progn
5536 (backward-char 1)
5537 (error "No next date after this line in this buffer")))
5538 (goto-char (match-beginning 0)))
5539
5540(defun org-agenda-previous-date-line (&optional arg)
5541 "Jump to the previous line indicating a date in agenda buffer."
5542 (interactive "p")
5543 (org-agenda-check-type t 'agenda 'timeline)
5544 (beginning-of-line 1)
5545 (if (not (re-search-backward "^\\S-" nil t arg))
5546 (error "No previous date before this line in this buffer")))
5547
5548;; Initialize the highlight
5549(defvar org-hl (org-make-overlay 1 1))
5550(org-overlay-put org-hl 'face 'highlight)
5551
5552(defun org-highlight (begin end &optional buffer)
5553 "Highlight a region with overlay."
5554 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
5555 org-hl begin end (or buffer (current-buffer))))
5556
5557(defun org-unhighlight ()
5558 "Detach overlay INDEX."
5559 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
5560
5561;; FIXME this is currently not used.
5562(defun org-highlight-until-next-command (beg end &optional buffer)
5563 "Move the highlight overlay to BEG/END, remove it before the next command."
5564 (org-highlight beg end buffer)
5565 (add-hook 'pre-command-hook 'org-unhighlight-once))
5566(defun org-unhighlight-once ()
5567 "Remove the highlight from its position, and this function from the hook."
5568 (remove-hook 'pre-command-hook 'org-unhighlight-once)
5569 (org-unhighlight))
5570
5571(defun org-agenda-follow-mode ()
5572 "Toggle follow mode in an agenda buffer."
5573 (interactive)
5574 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
5575 (org-agenda-set-mode-name)
8bfe682a
CD
5576 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
5577 (org-agenda-show))
20908596
CD
5578 (message "Follow mode is %s"
5579 (if org-agenda-follow-mode "on" "off")))
5580
54a0dee5
CD
5581(defun org-agenda-entry-text-mode (&optional arg)
5582 "Toggle entry text mode in an agenda buffer."
5583 (interactive "P")
5584 (if (integerp arg)
5585 (setq org-agenda-entry-text-mode t)
5586 (setq org-agenda-entry-text-mode (not org-agenda-entry-text-mode)))
5587 (org-agenda-entry-text-hide)
5588 (and org-agenda-entry-text-mode
5589 (let ((org-agenda-entry-text-maxlines
5590 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5591 (org-agenda-entry-text-show)))
5592 (org-agenda-set-mode-name)
5593 (message "Entry text mode is %s. Maximum number of lines is %d"
5594 (if org-agenda-entry-text-mode "on" "off")
5595 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5596
20908596
CD
5597(defun org-agenda-clockreport-mode ()
5598 "Toggle clocktable mode in an agenda buffer."
5599 (interactive)
5600 (org-agenda-check-type t 'agenda)
5601 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
5602 (org-agenda-set-mode-name)
5603 (org-agenda-redo)
5604 (message "Clocktable mode is %s"
5605 (if org-agenda-clockreport-mode "on" "off")))
5606
93b62de8
CD
5607(defun org-agenda-log-mode (&optional special)
5608 "Toggle log mode in an agenda buffer.
5609With argument SPECIAL, show all possible log items, not only the ones
5610configured in `org-agenda-log-mode-items'.
5611With a double `C-u' prefix arg, show *only* log items, nothing else."
5612 (interactive "P")
20908596 5613 (org-agenda-check-type t 'agenda 'timeline)
93b62de8
CD
5614 (setq org-agenda-show-log
5615 (if (equal special '(16))
5616 'only
5617 (if special '(closed clock state)
5618 (not org-agenda-show-log))))
20908596
CD
5619 (org-agenda-set-mode-name)
5620 (org-agenda-redo)
5621 (message "Log mode is %s"
5622 (if org-agenda-show-log "on" "off")))
5623
2c3ad40d 5624(defun org-agenda-archives-mode (&optional with-files)
c8d0cf5c
CD
5625 "Toggle inclusion of items in trees marked with :ARCHIVE:.
5626When called with a prefix argument, include all archive files as well."
2c3ad40d
CD
5627 (interactive "P")
5628 (setq org-agenda-archives-mode
5629 (if with-files t (if org-agenda-archives-mode nil 'trees)))
5630 (org-agenda-set-mode-name)
5631 (org-agenda-redo)
5632 (message
5633 "%s"
5634 (cond
5635 ((eq org-agenda-archives-mode nil)
5636 "No archives are included")
5637 ((eq org-agenda-archives-mode 'trees)
5638 (format "Trees with :%s: tag are included" org-archive-tag))
5639 ((eq org-agenda-archives-mode t)
5640 (format "Trees with :%s: tag and all active archive files are included"
5641 org-archive-tag)))))
5642
20908596
CD
5643(defun org-agenda-toggle-diary ()
5644 "Toggle diary inclusion in an agenda buffer."
5645 (interactive)
5646 (org-agenda-check-type t 'agenda)
5647 (setq org-agenda-include-diary (not org-agenda-include-diary))
5648 (org-agenda-redo)
5649 (org-agenda-set-mode-name)
5650 (message "Diary inclusion turned %s"
5651 (if org-agenda-include-diary "on" "off")))
5652
5653(defun org-agenda-toggle-time-grid ()
5654 "Toggle time grid in an agenda buffer."
5655 (interactive)
5656 (org-agenda-check-type t 'agenda)
5657 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
5658 (org-agenda-redo)
5659 (org-agenda-set-mode-name)
5660 (message "Time-grid turned %s"
5661 (if org-agenda-use-time-grid "on" "off")))
5662
5663(defun org-agenda-set-mode-name ()
5664 "Set the mode name to indicate all the small mode settings."
5665 (setq mode-name
5666 (concat "Org-Agenda"
5667 (if (equal org-agenda-ndays 1) " Day" "")
5668 (if (equal org-agenda-ndays 7) " Week" "")
5669 (if org-agenda-follow-mode " Follow" "")
54a0dee5 5670 (if org-agenda-entry-text-mode " ETxt" "")
20908596
CD
5671 (if org-agenda-include-diary " Diary" "")
5672 (if org-agenda-use-time-grid " Grid" "")
8bfe682a
CD
5673 (if (and (boundp 'org-habit-show-habits)
5674 org-habit-show-habits) " Habit" "")
93b62de8 5675 (if (consp org-agenda-show-log) " LogAll"
8bfe682a 5676 (if org-agenda-show-log " Log" ""))
c8d0cf5c
CD
5677 (if (or org-agenda-filter (get 'org-agenda-filter
5678 :preset-filter))
5679 (concat " {" (mapconcat
5680 'identity
5681 (append (get 'org-agenda-filter
5682 :preset-filter)
5683 org-agenda-filter) "") "}")
71d35b24 5684 "")
2c3ad40d
CD
5685 (if org-agenda-archives-mode
5686 (if (eq org-agenda-archives-mode t)
5687 " Archives"
5688 (format " :%s:" org-archive-tag))
5689 "")
20908596
CD
5690 (if org-agenda-clockreport-mode " Clock" "")))
5691 (force-mode-line-update))
5692
5693(defun org-agenda-post-command-hook ()
b349f79f
CD
5694 (setq org-agenda-type
5695 (or (get-text-property (point) 'org-agenda-type)
5696 (get-text-property (max (point-min) (1- (point)))
8bfe682a
CD
5697 'org-agenda-type))))
5698
5699(defun org-agenda-next-line ()
5700 "Move cursor to the next line, and show if follow-mode is active."
5701 (interactive)
5702 (call-interactively 'next-line)
1bcdebed
CD
5703 (org-agenda-do-context-action))
5704
8bfe682a
CD
5705(defun org-agenda-previous-line ()
5706 "Move cursor to the previous line, and show if follow-mode is active."
5707
5708 (interactive)
5709 (call-interactively 'previous-line)
1bcdebed
CD
5710 (org-agenda-do-context-action))
5711
5712(defun org-agenda-do-context-action ()
5713 "Show outline path and, maybe, follow-mode window."
5714 (let ((m (org-get-at-bol 'org-marker)))
5715 (if (and org-agenda-follow-mode m)
5716 (org-agenda-show))
5717 (if (and m org-agenda-show-outline-path)
5dec9555
CD
5718 (org-with-point-at m
5719 (org-display-outline-path t)))))
20908596
CD
5720
5721(defun org-agenda-show-priority ()
5722 "Show the priority of the current item.
5723This priority is composed of the main priority given with the [#A] cookies,
5724and by additional input from the age of a schedules or deadline entry."
5725 (interactive)
8d642074 5726 (let* ((pri (org-get-at-bol 'priority)))
20908596
CD
5727 (message "Priority is %d" (if pri pri -1000))))
5728
5729(defun org-agenda-show-tags ()
5730 "Show the tags applicable to the current item."
5731 (interactive)
8d642074 5732 (let* ((tags (org-get-at-bol 'tags)))
20908596
CD
5733 (if tags
5734 (message "Tags are :%s:"
5735 (org-no-properties (mapconcat 'identity tags ":")))
5736 (message "No tags associated with this line"))))
5737
5738(defun org-agenda-goto (&optional highlight)
5739 "Go to the Org-mode file which contains the item at point."
5740 (interactive)
8d642074 5741 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
5742 (org-agenda-error)))
5743 (buffer (marker-buffer marker))
5744 (pos (marker-position marker)))
5745 (switch-to-buffer-other-window buffer)
5746 (widen)
5747 (goto-char pos)
5748 (when (org-mode-p)
5749 (org-show-context 'agenda)
5750 (save-excursion
5751 (and (outline-next-heading)
5752 (org-flag-heading nil)))) ; show the next heading
5753 (recenter (/ (window-height) 2))
5754 (run-hooks 'org-agenda-after-show-hook)
5755 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
5756
5757(defvar org-agenda-after-show-hook nil
5758 "Normal hook run after an item has been shown from the agenda.
5759Point is in the buffer where the item originated.")
5760
5761(defun org-agenda-kill ()
5762 "Kill the entry or subtree belonging to the current agenda entry."
5763 (interactive)
5764 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 5765 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
5766 (org-agenda-error)))
5767 (buffer (marker-buffer marker))
5768 (pos (marker-position marker))
8d642074 5769 (type (org-get-at-bol 'type))
20908596
CD
5770 dbeg dend (n 0) conf)
5771 (org-with-remote-undo buffer
5772 (with-current-buffer buffer
5773 (save-excursion
5774 (goto-char pos)
5775 (if (and (org-mode-p) (not (member type '("sexp"))))
5776 (setq dbeg (progn (org-back-to-heading t) (point))
5777 dend (org-end-of-subtree t t))
5778 (setq dbeg (point-at-bol)
5779 dend (min (point-max) (1+ (point-at-eol)))))
5780 (goto-char dbeg)
5781 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
5782 (setq conf (or (eq t org-agenda-confirm-kill)
5783 (and (numberp org-agenda-confirm-kill)
5784 (> n org-agenda-confirm-kill))))
5785 (and conf
5786 (not (y-or-n-p
5787 (format "Delete entry with %d lines in buffer \"%s\"? "
5788 n (buffer-name buffer))))
5789 (error "Abort"))
5790 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
5791 (with-current-buffer buffer (delete-region dbeg dend))
5792 (message "Agenda item and source killed"))))
5793
8bfe682a
CD
5794(defvar org-archive-default-command)
5795(defun org-agenda-archive-default ()
5796 "Archive the entry or subtree belonging to the current agenda entry."
5797 (interactive)
5798 (require 'org-archive)
5799 (org-agenda-archive-with org-archive-default-command))
5800
5801(defun org-agenda-archive-default-with-confirmation ()
5802 "Archive the entry or subtree belonging to the current agenda entry."
5803 (interactive)
5804 (require 'org-archive)
5805 (org-agenda-archive-with org-archive-default-command 'confirm))
5806
20908596
CD
5807(defun org-agenda-archive ()
5808 "Archive the entry or subtree belonging to the current agenda entry."
5809 (interactive)
8bfe682a 5810 (org-agenda-archive-with 'org-archive-subtree))
20908596
CD
5811
5812(defun org-agenda-archive-to-archive-sibling ()
8bfe682a
CD
5813 "Move the entry to the archive sibling."
5814 (interactive)
5815 (org-agenda-archive-with 'org-archive-to-archive-sibling))
5816
5817(defun org-agenda-archive-with (cmd &optional confirm)
20908596
CD
5818 "Move the entry to the archive sibling."
5819 (interactive)
5820 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 5821 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
5822 (org-agenda-error)))
5823 (buffer (marker-buffer marker))
5824 (pos (marker-position marker)))
5825 (org-with-remote-undo buffer
5826 (with-current-buffer buffer
5827 (if (org-mode-p)
8bfe682a
CD
5828 (if (and confirm
5829 (not (y-or-n-p "Archive this subtree or entry? ")))
5830 (error "Abort")
5831 (save-excursion
5832 (goto-char pos)
5833 (org-remove-subtree-entries-from-agenda)
5834 (org-back-to-heading t)
5835 (funcall cmd)))
20908596
CD
5836 (error "Archiving works only in Org-mode files"))))))
5837
5838(defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
5839 "Remove all lines in the agenda that correspond to a given subtree.
5840The subtree is the one in buffer BUF, starting at BEG and ending at END.
5841If this information is not given, the function uses the tree at point."
5842 (let ((buf (or buf (current-buffer))) m p)
5843 (save-excursion
5844 (unless (and beg end)
5845 (org-back-to-heading t)
5846 (setq beg (point))
5847 (org-end-of-subtree t)
5848 (setq end (point)))
5849 (set-buffer (get-buffer org-agenda-buffer-name))
5850 (save-excursion
5851 (goto-char (point-max))
5852 (beginning-of-line 1)
5853 (while (not (bobp))
8d642074 5854 (when (and (setq m (org-get-at-bol 'org-marker))
20908596
CD
5855 (equal buf (marker-buffer m))
5856 (setq p (marker-position m))
5857 (>= p beg)
c8d0cf5c 5858 (< p end))
20908596
CD
5859 (let ((inhibit-read-only t))
5860 (delete-region (point-at-bol) (1+ (point-at-eol)))))
5861 (beginning-of-line 0))))))
5862
c8d0cf5c
CD
5863(defun org-agenda-refile (&optional goto rfloc)
5864 "Refile the item at point."
54a0dee5
CD
5865 (interactive "P")
5866 (if (equal goto '(16))
5867 (org-refile-goto-last-stored)
8d642074 5868 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
5869 (org-agenda-error)))
5870 (buffer (marker-buffer marker))
5871 (pos (marker-position marker))
5872 (rfloc (or rfloc
5873 (org-refile-get-location
5874 (if goto "Goto: " "Refile to: ") buffer
5875 org-refile-allow-creating-parent-nodes))))
5876 (with-current-buffer buffer
5877 (save-excursion
5878 (save-restriction
5879 (widen)
5880 (goto-char marker)
5881 (org-remove-subtree-entries-from-agenda)
5882 (org-refile goto buffer rfloc)))))))
5883
5884(defun org-agenda-open-link (&optional arg)
5885 "Follow the link in the current line, if any.
8bfe682a 5886This looks for a link in the displayed line in the agenda. It also looks
54a0dee5 5887at the text of the entry itself."
c8d0cf5c 5888 (interactive "P")
8d642074
CD
5889 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
5890 (org-get-at-bol 'org-marker)))
5891 (buffer (and marker (marker-buffer marker)))
5892 (prefix (buffer-substring
5893 (point-at-bol)
5894 (+ (point-at-bol)
8bfe682a
CD
5895 (or (org-get-at-bol 'prefix-length) 0)))))
5896 (cond
5897 (buffer
5898 (with-current-buffer buffer
5899 (save-excursion
5900 (save-restriction
5901 (widen)
5902 (goto-char marker)
5903 (org-offer-links-in-entry arg prefix)))))
5904 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
5905 (save-excursion
5906 (beginning-of-line 1)
5907 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
5908 (org-open-link-from-string (match-string 1)))
5909 (t (error "No link to open here")))))
20908596
CD
5910
5911(defun org-agenda-copy-local-variable (var)
5912 "Get a variable from a referenced buffer and install it here."
8d642074 5913 (let ((m (org-get-at-bol 'org-marker)))
20908596
CD
5914 (when (and m (buffer-live-p (marker-buffer m)))
5915 (org-set-local var (with-current-buffer (marker-buffer m)
5916 (symbol-value var))))))
5917
5918(defun org-agenda-switch-to (&optional delete-other-windows)
5919 "Go to the Org-mode file which contains the item at point."
5920 (interactive)
8bfe682a
CD
5921 (if (and org-return-follows-link
5922 (not (org-get-at-bol 'org-marker))
5923 (org-in-regexp org-bracket-link-regexp))
5924 (org-open-link-from-string (match-string 0))
5925 (let* ((marker (or (org-get-at-bol 'org-marker)
5926 (org-agenda-error)))
5927 (buffer (marker-buffer marker))
5928 (pos (marker-position marker)))
5929 (switch-to-buffer buffer)
5930 (and delete-other-windows (delete-other-windows))
5931 (widen)
5932 (goto-char pos)
5933 (when (org-mode-p)
5934 (org-show-context 'agenda)
5935 (save-excursion
5936 (and (outline-next-heading)
5937 (org-flag-heading nil))))))) ; show the next heading
20908596
CD
5938
5939(defun org-agenda-goto-mouse (ev)
5940 "Go to the Org-mode file which contains the item at the mouse click."
5941 (interactive "e")
5942 (mouse-set-point ev)
5943 (org-agenda-goto))
5944
fdf730ed
CD
5945(defun org-agenda-show (&optional full-entry)
5946 "Display the Org-mode file which contains the item at point.
5947With prefix argument FULL-ENTRY, make the entire entry visible
5948if it was hidden in the outline."
5949 (interactive "P")
20908596 5950 (let ((win (selected-window)))
fdf730ed
CD
5951 (if full-entry
5952 (let ((org-show-entry-below t))
5953 (org-agenda-goto t))
5954 (org-agenda-goto t))
20908596
CD
5955 (select-window win)))
5956
8bfe682a
CD
5957(defvar org-agenda-show-window nil)
5958(defun org-agenda-show-and-scroll-up ()
5959 "Display the Org-mode file which contains the item at point.
5960When called repeatedly, scroll the window that is displaying the buffer."
5961 (interactive)
5962 (let ((win (selected-window)))
5963 (if (and (window-live-p org-agenda-show-window)
5964 (eq this-command last-command))
5965 (progn
5966 (select-window org-agenda-show-window)
5967 (ignore-errors (scroll-up)))
5968 (org-agenda-goto t)
5969 (show-subtree)
5970 (setq org-agenda-show-window (selected-window)))
5971 (select-window win)))
5972
5973(defun org-agenda-show-scroll-down ()
5974 "Scroll down the window showing the agenda."
5975 (interactive)
5976 (let ((win (selected-window)))
5977 (when (window-live-p org-agenda-show-window)
5978 (select-window org-agenda-show-window)
5979 (ignore-errors (scroll-down))
5980 (select-window win))))
5981
c8d0cf5c
CD
5982(defun org-agenda-show-1 (&optional more)
5983 "Display the Org-mode file which contains the item at point.
8bfe682a 5984The prefix arg selects the amount of information to display:
c8d0cf5c
CD
5985
59860 hide the subtree
59871 just show the entry according to defaults.
54a0dee5
CD
59882 show the children view
59893 show the subtree view
c8d0cf5c
CD
59904 show the entire subtree and any LOGBOOK drawers
59915 show the entire subtree and any drawers
5992With prefix argument FULL-ENTRY, make the entire entry visible
5993if it was hidden in the outline."
5994 (interactive "p")
5995 (let ((win (selected-window)))
5996 (org-agenda-goto t)
5997 (org-recenter-heading 1)
5998 (cond
5999 ((= more 0)
6000 (hide-subtree)
54a0dee5
CD
6001 (save-excursion
6002 (org-back-to-heading)
6003 (run-hook-with-args 'org-cycle-hook 'folded))
6004 (message "Remote: FOLDED"))
c8d0cf5c
CD
6005 ((and (interactive-p) (= more 1))
6006 (message "Remote: show with default settings"))
6007 ((= more 2)
6008 (show-entry)
54a0dee5 6009 (show-children)
c8d0cf5c
CD
6010 (save-excursion
6011 (org-back-to-heading)
54a0dee5
CD
6012 (run-hook-with-args 'org-cycle-hook 'children))
6013 (message "Remote: CHILDREN"))
c8d0cf5c
CD
6014 ((= more 3)
6015 (show-subtree)
6016 (save-excursion
6017 (org-back-to-heading)
54a0dee5
CD
6018 (run-hook-with-args 'org-cycle-hook 'subtree))
6019 (message "Remote: SUBTREE"))
c8d0cf5c
CD
6020 ((= more 4)
6021 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
6022 (org-drawer-regexp
6023 (concat "^[ \t]*:\\("
6024 (mapconcat 'regexp-quote org-drawers "\\|")
6025 "\\):[ \t]*$")))
6026 (show-subtree)
6027 (save-excursion
6028 (org-back-to-heading)
6029 (org-cycle-hide-drawers 'subtree)))
54a0dee5 6030 (message "Remote: SUBTREE AND LOGBOOK"))
c8d0cf5c
CD
6031 ((> more 4)
6032 (show-subtree)
54a0dee5 6033 (message "Remote: SUBTREE AND ALL DRAWERS")))
c8d0cf5c
CD
6034 (select-window win)))
6035
6036(defun org-recenter-heading (n)
6037 (save-excursion
6038 (org-back-to-heading)
6039 (recenter n)))
6040
6041(defvar org-agenda-cycle-counter nil)
54a0dee5 6042(defun org-agenda-cycle-show (&optional n)
c8d0cf5c
CD
6043 "Show the current entry in another window, with default settings.
6044Default settings are taken from `org-show-hierarchy-above' and siblings.
54a0dee5 6045When use repeatedly in immediate succession, the remote entry will cycle
c8d0cf5c
CD
6046through visibility
6047
54a0dee5
CD
6048children -> subtree -> folded
6049
6050When called with a numeric prefix arg, that arg will be passed through to
6051`org-agenda-show-1'. For the interpretation of that argument, see the
6052docstring of `org-agenda-show-1'."
6053 (interactive "P")
6054 (if (integerp n)
6055 (setq org-agenda-cycle-counter n)
6056 (if (not (eq last-command this-command))
6057 (setq org-agenda-cycle-counter 1)
6058 (if (equal org-agenda-cycle-counter 0)
6059 (setq org-agenda-cycle-counter 2)
6060 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
6061 (if (> org-agenda-cycle-counter 3)
6062 (setq org-agenda-cycle-counter 0)))))
c8d0cf5c
CD
6063 (org-agenda-show-1 org-agenda-cycle-counter))
6064
20908596
CD
6065(defun org-agenda-recenter (arg)
6066 "Display the Org-mode file which contains the item at point and recenter."
6067 (interactive "P")
6068 (let ((win (selected-window)))
6069 (org-agenda-goto t)
6070 (recenter arg)
6071 (select-window win)))
6072
6073(defun org-agenda-show-mouse (ev)
6074 "Display the Org-mode file which contains the item at the mouse click."
6075 (interactive "e")
6076 (mouse-set-point ev)
6077 (org-agenda-show))
6078
6079(defun org-agenda-check-no-diary ()
6080 "Check if the entry is a diary link and abort if yes."
8d642074 6081 (if (org-get-at-bol 'org-agenda-diary-link)
20908596
CD
6082 (org-agenda-error)))
6083
6084(defun org-agenda-error ()
6085 (error "Command not allowed in this line"))
6086
6087(defun org-agenda-tree-to-indirect-buffer ()
6088 "Show the subtree corresponding to the current entry in an indirect buffer.
6089This calls the command `org-tree-to-indirect-buffer' from the original
6090Org-mode buffer.
6091With numerical prefix arg ARG, go up to this level and then take that tree.
6092With a C-u prefix, make a separate frame for this tree (i.e. don't use the
6093dedicated frame)."
6094 (interactive)
6095 (org-agenda-check-no-diary)
8d642074 6096 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6097 (org-agenda-error)))
6098 (buffer (marker-buffer marker))
6099 (pos (marker-position marker)))
6100 (with-current-buffer buffer
6101 (save-excursion
6102 (goto-char pos)
6103 (call-interactively 'org-tree-to-indirect-buffer)))))
6104
6105(defvar org-last-heading-marker (make-marker)
6106 "Marker pointing to the headline that last changed its TODO state
6107by a remote command from the agenda.")
6108
6109(defun org-agenda-todo-nextset ()
6110 "Switch TODO entry to next sequence."
6111 (interactive)
6112 (org-agenda-todo 'nextset))
6113
6114(defun org-agenda-todo-previousset ()
6115 "Switch TODO entry to previous sequence."
6116 (interactive)
6117 (org-agenda-todo 'previousset))
6118
6119(defun org-agenda-todo (&optional arg)
6120 "Cycle TODO state of line at point, also in Org-mode file.
6121This changes the line at point, all other lines in the agenda referring to
6122the same tree node, and the headline of the tree node in the Org-mode file."
6123 (interactive "P")
6124 (org-agenda-check-no-diary)
6125 (let* ((col (current-column))
8d642074 6126 (marker (or (org-get-at-bol 'org-marker)
20908596
CD
6127 (org-agenda-error)))
6128 (buffer (marker-buffer marker))
6129 (pos (marker-position marker))
8d642074
CD
6130 (hdmarker (org-get-at-bol 'org-hd-marker))
6131 (todayp (equal (org-get-at-bol 'day)
93b62de8 6132 (time-to-days (current-time))))
20908596 6133 (inhibit-read-only t)
93b62de8 6134 org-agenda-headline-snapshot-before-repeat newhead just-one)
20908596
CD
6135 (org-with-remote-undo buffer
6136 (with-current-buffer buffer
6137 (widen)
6138 (goto-char pos)
6139 (org-show-context 'agenda)
6140 (save-excursion
6141 (and (outline-next-heading)
6142 (org-flag-heading nil))) ; show the next heading
a2a2e7fb
CD
6143 (let ((current-prefix-arg arg))
6144 (call-interactively 'org-todo))
20908596
CD
6145 (and (bolp) (forward-char 1))
6146 (setq newhead (org-get-heading))
93b62de8
CD
6147 (when (and (org-bound-and-true-p
6148 org-agenda-headline-snapshot-before-repeat)
6149 (not (equal org-agenda-headline-snapshot-before-repeat
6150 newhead))
6151 todayp)
6152 (setq newhead org-agenda-headline-snapshot-before-repeat
6153 just-one t))
20908596
CD
6154 (save-excursion
6155 (org-back-to-heading)
6156 (move-marker org-last-heading-marker (point))))
6157 (beginning-of-line 1)
6158 (save-excursion
93b62de8 6159 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
20908596
CD
6160 (org-move-to-column col))))
6161
6162(defun org-agenda-add-note (&optional arg)
6163 "Add a time-stamped note to the entry at point."
6164 (interactive "P")
6165 (org-agenda-check-no-diary)
8d642074 6166 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6167 (org-agenda-error)))
6168 (buffer (marker-buffer marker))
6169 (pos (marker-position marker))
8d642074 6170 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
6171 (inhibit-read-only t))
6172 (with-current-buffer buffer
6173 (widen)
6174 (goto-char pos)
6175 (org-show-context 'agenda)
6176 (save-excursion
6177 (and (outline-next-heading)
6178 (org-flag-heading nil))) ; show the next heading
6179 (org-add-note))))
6180
db55f368 6181(defun org-agenda-change-all-lines (newhead hdmarker
4ed008de 6182 &optional fixface just-this)
20908596
CD
6183 "Change all lines in the agenda buffer which match HDMARKER.
6184The new content of the line will be NEWHEAD (as modified by
6185`org-format-agenda-item'). HDMARKER is checked with
6186`equal' against all `org-hd-marker' text properties in the file.
33306645 6187If FIXFACE is non-nil, the face of each item is modified according to
db55f368
CD
6188the new TODO state.
6189If JUST-THIS is non-nil, change just the current line, not all.
33306645 6190If FORCE-TAGS is non nil, the car of it returns the new tags."
20908596 6191 (let* ((inhibit-read-only t)
93b62de8 6192 (line (org-current-line))
fdf730ed 6193 (thetags (with-current-buffer (marker-buffer hdmarker)
4ed008de
CD
6194 (save-excursion (save-restriction (widen)
6195 (goto-char hdmarker)
fdf730ed 6196 (org-get-tags-at)))))
20908596
CD
6197 props m pl undone-face done-face finish new dotime cat tags)
6198 (save-excursion
6199 (goto-char (point-max))
6200 (beginning-of-line 1)
6201 (while (not finish)
6202 (setq finish (bobp))
8d642074 6203 (when (and (setq m (org-get-at-bol 'org-hd-marker))
93b62de8 6204 (or (not just-this) (= (org-current-line) line))
20908596
CD
6205 (equal m hdmarker))
6206 (setq props (text-properties-at (point))
8d642074
CD
6207 dotime (org-get-at-bol 'dotime)
6208 cat (org-get-at-bol 'org-category)
4ed008de 6209 tags thetags
20908596 6210 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
8d642074
CD
6211 pl (org-get-at-bol 'prefix-length)
6212 undone-face (org-get-at-bol 'undone-face)
6213 done-face (org-get-at-bol 'done-face))
6214 (goto-char (+ (point) pl))
6215 ;; (org-move-to-column pl) FIXME: does the above line work correctly?
20908596
CD
6216 (cond
6217 ((equal new "")
6218 (beginning-of-line 1)
6219 (and (looking-at ".*\n?") (replace-match "")))
6220 ((looking-at ".*")
6221 (replace-match new t t)
6222 (beginning-of-line 1)
6223 (add-text-properties (point-at-bol) (point-at-eol) props)
6224 (when fixface
6225 (add-text-properties
6226 (point-at-bol) (point-at-eol)
6227 (list 'face
6228 (if org-last-todo-state-is-todo
6229 undone-face done-face))))
6230 (org-agenda-highlight-todo 'line)
6231 (beginning-of-line 1))
6232 (t (error "Line update did not work"))))
6233 (beginning-of-line 0)))
6234 (org-finalize-agenda)))
6235
6236(defun org-agenda-align-tags (&optional line)
6237 "Align all tags in agenda items to `org-agenda-tags-column'."
6238 (let ((inhibit-read-only t) l c)
6239 (save-excursion
6240 (goto-char (if line (point-at-bol) (point-min)))
6241 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
6242 (if line (point-at-eol) nil) t)
6243 (add-text-properties
6244 (match-beginning 2) (match-end 2)
30ab4580
GM
6245 (list 'face (delq nil (let ((prop (get-text-property
6246 (match-beginning 2) 'face)))
6247 (or (listp prop) (setq prop (list prop)))
6248 (if (memq 'org-tag prop)
6249 prop
6250 (cons 'org-tag prop))))))
20908596
CD
6251 (setq l (- (match-end 2) (match-beginning 2))
6252 c (if (< org-agenda-tags-column 0)
6253 (- (abs org-agenda-tags-column) l)
6254 org-agenda-tags-column))
6255 (delete-region (match-beginning 1) (match-end 1))
6256 (goto-char (match-beginning 1))
6257 (insert (org-add-props
6258 (make-string (max 1 (- c (current-column))) ?\ )
ff4be292
CD
6259 (text-properties-at (point)))))
6260 (goto-char (point-min))
6261 (org-font-lock-add-tag-faces (point-max)))))
20908596
CD
6262
6263(defun org-agenda-priority-up ()
6264 "Increase the priority of line at point, also in Org-mode file."
6265 (interactive)
6266 (org-agenda-priority 'up))
6267
6268(defun org-agenda-priority-down ()
6269 "Decrease the priority of line at point, also in Org-mode file."
6270 (interactive)
6271 (org-agenda-priority 'down))
6272
6273(defun org-agenda-priority (&optional force-direction)
6274 "Set the priority of line at point, also in Org-mode file.
6275This changes the line at point, all other lines in the agenda referring to
6276the same tree node, and the headline of the tree node in the Org-mode file."
6277 (interactive)
c8d0cf5c
CD
6278 (unless org-enable-priority-commands
6279 (error "Priority commands are disabled"))
20908596 6280 (org-agenda-check-no-diary)
8d642074 6281 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 6282 (org-agenda-error)))
8d642074 6283 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
6284 (buffer (marker-buffer hdmarker))
6285 (pos (marker-position hdmarker))
6286 (inhibit-read-only t)
6287 newhead)
6288 (org-with-remote-undo buffer
6289 (with-current-buffer buffer
6290 (widen)
6291 (goto-char pos)
6292 (org-show-context 'agenda)
6293 (save-excursion
6294 (and (outline-next-heading)
6295 (org-flag-heading nil))) ; show the next heading
6296 (funcall 'org-priority force-direction)
6297 (end-of-line 1)
6298 (setq newhead (org-get-heading)))
6299 (org-agenda-change-all-lines newhead hdmarker)
6300 (beginning-of-line 1))))
6301
6302;; FIXME: should fix the tags property of the agenda line.
c8d0cf5c 6303(defun org-agenda-set-tags (&optional tag onoff)
20908596
CD
6304 "Set tags for the current headline."
6305 (interactive)
6306 (org-agenda-check-no-diary)
6307 (if (and (org-region-active-p) (interactive-p))
6308 (call-interactively 'org-change-tag-in-region)
6309 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 6310 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
6311 (org-agenda-error)))
6312 (buffer (marker-buffer hdmarker))
6313 (pos (marker-position hdmarker))
6314 (inhibit-read-only t)
4ed008de 6315 newhead)
20908596
CD
6316 (org-with-remote-undo buffer
6317 (with-current-buffer buffer
6318 (widen)
6319 (goto-char pos)
6320 (save-excursion
6321 (org-show-context 'agenda))
6322 (save-excursion
6323 (and (outline-next-heading)
6324 (org-flag-heading nil))) ; show the next heading
6325 (goto-char pos)
c8d0cf5c
CD
6326 (if tag
6327 (org-toggle-tag tag onoff)
6328 (call-interactively 'org-set-tags))
20908596
CD
6329 (end-of-line 1)
6330 (setq newhead (org-get-heading)))
4ed008de 6331 (org-agenda-change-all-lines newhead hdmarker)
20908596
CD
6332 (beginning-of-line 1)))))
6333
54a0dee5
CD
6334(defun org-agenda-set-property ()
6335 "Set a property for the current headline."
6336 (interactive)
6337 (org-agenda-check-no-diary)
6338 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 6339 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
6340 (org-agenda-error)))
6341 (buffer (marker-buffer hdmarker))
6342 (pos (marker-position hdmarker))
6343 (inhibit-read-only t)
6344 newhead)
6345 (org-with-remote-undo buffer
6346 (with-current-buffer buffer
6347 (widen)
6348 (goto-char pos)
6349 (save-excursion
6350 (org-show-context 'agenda))
6351 (save-excursion
6352 (and (outline-next-heading)
6353 (org-flag-heading nil))) ; show the next heading
6354 (goto-char pos)
6355 (call-interactively 'org-set-property)))))
6356
6357(defun org-agenda-set-effort ()
6358 "Set the effort property for the current headline."
6359 (interactive)
6360 (org-agenda-check-no-diary)
6361 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 6362 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
6363 (org-agenda-error)))
6364 (buffer (marker-buffer hdmarker))
6365 (pos (marker-position hdmarker))
6366 (inhibit-read-only t)
6367 newhead)
6368 (org-with-remote-undo buffer
6369 (with-current-buffer buffer
6370 (widen)
6371 (goto-char pos)
6372 (save-excursion
6373 (org-show-context 'agenda))
6374 (save-excursion
6375 (and (outline-next-heading)
6376 (org-flag-heading nil))) ; show the next heading
6377 (goto-char pos)
6378 (call-interactively 'org-set-effort)
6379 (end-of-line 1)))))
6380
20908596
CD
6381(defun org-agenda-toggle-archive-tag ()
6382 "Toggle the archive tag for the current entry."
6383 (interactive)
6384 (org-agenda-check-no-diary)
6385 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 6386 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
6387 (org-agenda-error)))
6388 (buffer (marker-buffer hdmarker))
6389 (pos (marker-position hdmarker))
6390 (inhibit-read-only t)
6391 newhead)
6392 (org-with-remote-undo buffer
6393 (with-current-buffer buffer
6394 (widen)
6395 (goto-char pos)
6396 (org-show-context 'agenda)
6397 (save-excursion
6398 (and (outline-next-heading)
6399 (org-flag-heading nil))) ; show the next heading
6400 (call-interactively 'org-toggle-archive-tag)
6401 (end-of-line 1)
6402 (setq newhead (org-get-heading)))
6403 (org-agenda-change-all-lines newhead hdmarker)
6404 (beginning-of-line 1))))
6405
c8d0cf5c
CD
6406(defun org-agenda-do-date-later (arg)
6407 (interactive "P")
6408 (cond
6409 ((or (equal arg '(16))
6410 (memq last-command
6411 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6412 (setq this-command 'org-agenda-date-later-minutes)
6413 (org-agenda-date-later-minutes 1))
6414 ((or (equal arg '(4))
6415 (memq last-command
6416 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6417 (setq this-command 'org-agenda-date-later-hours)
6418 (org-agenda-date-later-hours 1))
6419 (t
6420 (org-agenda-date-later (prefix-numeric-value arg)))))
6421
6422(defun org-agenda-do-date-earlier (arg)
6423 (interactive "P")
6424 (cond
6425 ((or (equal arg '(16))
6426 (memq last-command
6427 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6428 (setq this-command 'org-agenda-date-earlier-minutes)
6429 (org-agenda-date-earlier-minutes 1))
6430 ((or (equal arg '(4))
6431 (memq last-command
6432 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6433 (setq this-command 'org-agenda-date-earlier-hours)
6434 (org-agenda-date-earlier-hours 1))
6435 (t
6436 (org-agenda-date-earlier (prefix-numeric-value arg)))))
6437
20908596
CD
6438(defun org-agenda-date-later (arg &optional what)
6439 "Change the date of this item to one day later."
6440 (interactive "p")
6441 (org-agenda-check-type t 'agenda 'timeline)
6442 (org-agenda-check-no-diary)
8d642074 6443 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6444 (org-agenda-error)))
6445 (buffer (marker-buffer marker))
6446 (pos (marker-position marker)))
6447 (org-with-remote-undo buffer
6448 (with-current-buffer buffer
6449 (widen)
6450 (goto-char pos)
6451 (if (not (org-at-timestamp-p))
6452 (error "Cannot find time stamp"))
6453 (org-timestamp-change arg (or what 'day)))
6454 (org-agenda-show-new-time marker org-last-changed-timestamp))
6455 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6456
6457(defun org-agenda-date-earlier (arg &optional what)
6458 "Change the date of this item to one day earlier."
6459 (interactive "p")
6460 (org-agenda-date-later (- arg) what))
6461
c8d0cf5c
CD
6462(defun org-agenda-date-later-minutes (arg)
6463 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6464 (interactive "p")
6465 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6466 (org-agenda-date-later arg 'minute))
6467
6468(defun org-agenda-date-earlier-minutes (arg)
6469 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6470 (interactive "p")
6471 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6472 (org-agenda-date-earlier arg 'minute))
6473
6474(defun org-agenda-date-later-hours (arg)
6475 "Change the time of this item, in hour steps."
6476 (interactive "p")
6477 (org-agenda-date-later arg 'hour))
6478
6479(defun org-agenda-date-earlier-hours (arg)
6480 "Change the time of this item, in hour steps."
6481 (interactive "p")
6482 (org-agenda-date-earlier arg 'hour))
6483
20908596
CD
6484(defun org-agenda-show-new-time (marker stamp &optional prefix)
6485 "Show new date stamp via text properties."
6486 ;; We use text properties to make this undoable
71d35b24
CD
6487 (let ((inhibit-read-only t)
6488 (buffer-invisibility-spec))
20908596
CD
6489 (setq stamp (concat " " prefix " => " stamp))
6490 (save-excursion
6491 (goto-char (point-max))
6492 (while (not (bobp))
8d642074 6493 (when (equal marker (org-get-at-bol 'org-marker))
20908596 6494 (org-move-to-column (- (window-width) (length stamp)) t)
71d35b24 6495 (org-agenda-fix-tags-filter-overlays-at (point))
20908596
CD
6496 (if (featurep 'xemacs)
6497 ;; Use `duplicable' property to trigger undo recording
6498 (let ((ex (make-extent nil nil))
6499 (gl (make-glyph stamp)))
6500 (set-glyph-face gl 'secondary-selection)
6501 (set-extent-properties
6502 ex (list 'invisible t 'end-glyph gl 'duplicable t))
6503 (insert-extent ex (1- (point)) (point-at-eol)))
6504 (add-text-properties
6505 (1- (point)) (point-at-eol)
6506 (list 'display (org-add-props stamp nil
6507 'face 'secondary-selection))))
6508 (beginning-of-line 1))
6509 (beginning-of-line 0)))))
6510
6511(defun org-agenda-date-prompt (arg)
6512 "Change the date of this item. Date is prompted for, with default today.
6513The prefix ARG is passed to the `org-time-stamp' command and can therefore
6514be used to request time specification in the time stamp."
6515 (interactive "P")
6516 (org-agenda-check-type t 'agenda 'timeline)
6517 (org-agenda-check-no-diary)
8d642074 6518 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6519 (org-agenda-error)))
6520 (buffer (marker-buffer marker))
6521 (pos (marker-position marker)))
6522 (org-with-remote-undo buffer
6523 (with-current-buffer buffer
6524 (widen)
6525 (goto-char pos)
6526 (if (not (org-at-timestamp-p))
6527 (error "Cannot find time stamp"))
8d642074
CD
6528 (org-time-stamp arg))
6529 (org-agenda-show-new-time marker org-last-changed-timestamp))
6530 (message "Time stamp changed to %s" org-last-changed-timestamp)))
20908596
CD
6531
6532(defun org-agenda-schedule (arg)
6533 "Schedule the item at point."
6534 (interactive "P")
6535 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6536 (org-agenda-check-no-diary)
8d642074 6537 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6538 (org-agenda-error)))
6539 (type (marker-insertion-type marker))
6540 (buffer (marker-buffer marker))
6541 (pos (marker-position marker))
6542 (org-insert-labeled-timestamps-at-point nil)
6543 ts)
20908596
CD
6544 (set-marker-insertion-type marker t)
6545 (org-with-remote-undo buffer
6546 (with-current-buffer buffer
6547 (widen)
6548 (goto-char pos)
6549 (setq ts (org-schedule arg)))
6550 (org-agenda-show-new-time marker ts "S"))
6551 (message "Item scheduled for %s" ts)))
6552
6553(defun org-agenda-deadline (arg)
6554 "Schedule the item at point."
6555 (interactive "P")
6556 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6557 (org-agenda-check-no-diary)
8d642074 6558 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6559 (org-agenda-error)))
6560 (buffer (marker-buffer marker))
6561 (pos (marker-position marker))
6562 (org-insert-labeled-timestamps-at-point nil)
6563 ts)
6564 (org-with-remote-undo buffer
6565 (with-current-buffer buffer
6566 (widen)
6567 (goto-char pos)
6568 (setq ts (org-deadline arg)))
8d642074 6569 (org-agenda-show-new-time marker ts "D"))
20908596
CD
6570 (message "Deadline for this item set to %s" ts)))
6571
b349f79f
CD
6572(defun org-agenda-action ()
6573 "Select entry for agenda action, or execute an agenda action.
6574This command prompts for another letter. Valid inputs are:
6575
6576m Mark the entry at point for an agenda action
6577s Schedule the marked entry to the date at the cursor
6578d Set the deadline of the marked entry to the date at the cursor
6579r Call `org-remember' with cursor date as the default date
6580SPC Show marked entry in other window
6581TAB Visit marked entry in other window
6582
6583The cursor may be at a date in the calendar, or in the Org agenda."
6584 (interactive)
65c439fd 6585 (let (ans)
b349f79f
CD
6586 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
6587 (setq ans (read-char-exclusive))
6588 (cond
6589 ((equal ans ?m)
6590 ;; Mark this entry
6591 (if (eq major-mode 'org-agenda-mode)
8d642074
CD
6592 (let ((m (or (org-get-at-bol 'org-hd-marker)
6593 (org-get-at-bol 'org-marker))))
b349f79f
CD
6594 (if m
6595 (progn
6596 (move-marker org-agenda-action-marker
6597 (marker-position m) (marker-buffer m))
6598 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
6599 (error "Don't know which entry to mark")))
6600 (error "This command works only in the agenda")))
6601 ((equal ans ?s)
6602 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
6603 ((equal ans ?d)
6604 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
6605 ((equal ans ?r)
6606 (org-agenda-do-action '(org-remember) t))
6607 ((equal ans ?\ )
6608 (let ((cw (selected-window)))
6609 (org-switch-to-buffer-other-window
6610 (marker-buffer org-agenda-action-marker))
6611 (goto-char org-agenda-action-marker)
6612 (org-show-context 'agenda)
6613 (select-window cw)))
6614 ((equal ans ?\C-i)
6615 (org-switch-to-buffer-other-window
6616 (marker-buffer org-agenda-action-marker))
6617 (goto-char org-agenda-action-marker)
6618 (org-show-context 'agenda))
6619 (t (error "Invalid agenda action %c" ans)))))
6620
6621(defun org-agenda-do-action (form &optional current-buffer)
6622 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
6623 (let ((org-overriding-default-time (org-get-cursor-date)))
6624 (if current-buffer
6625 (eval form)
6626 (if (not (marker-buffer org-agenda-action-marker))
8bfe682a 6627 (error "No entry has been selected for agenda action")
b349f79f
CD
6628 (with-current-buffer (marker-buffer org-agenda-action-marker)
6629 (save-excursion
6630 (save-restriction
6631 (widen)
6632 (goto-char org-agenda-action-marker)
6633 (eval form))))))))
ff4be292 6634
20908596
CD
6635(defun org-agenda-clock-in (&optional arg)
6636 "Start the clock on the currently selected item."
6637 (interactive "P")
6638 (org-agenda-check-no-diary)
6639 (if (equal arg '(4))
6640 (org-clock-in arg)
8d642074 6641 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 6642 (org-agenda-error)))
8d642074 6643 (hdmarker (or (org-get-at-bol 'org-hd-marker)
b349f79f
CD
6644 marker))
6645 (pos (marker-position marker))
6646 newhead)
20908596
CD
6647 (org-with-remote-undo (marker-buffer marker)
6648 (with-current-buffer (marker-buffer marker)
6649 (widen)
6650 (goto-char pos)
b349f79f
CD
6651 (org-show-context 'agenda)
6652 (org-show-entry)
6653 (org-cycle-hide-drawers 'children)
6654 (org-clock-in arg)
6655 (setq newhead (org-get-heading)))
c8d0cf5c 6656 (org-agenda-change-all-lines newhead hdmarker)))))
20908596
CD
6657
6658(defun org-agenda-clock-out (&optional arg)
6659 "Stop the currently running clock."
6660 (interactive "P")
6661 (unless (marker-buffer org-clock-marker)
6662 (error "No running clock"))
c8d0cf5c
CD
6663 (let ((marker (make-marker)) newhead)
6664 (org-with-remote-undo (marker-buffer org-clock-marker)
6665 (with-current-buffer (marker-buffer org-clock-marker)
6666 (save-excursion
6667 (save-restriction
6668 (widen)
6669 (goto-char org-clock-marker)
6670 (org-back-to-heading t)
6671 (move-marker marker (point))
6672 (org-clock-out)
6673 (setq newhead (org-get-heading))))))
6674 (org-agenda-change-all-lines newhead marker)
6675 (move-marker marker nil)))
20908596
CD
6676
6677(defun org-agenda-clock-cancel (&optional arg)
6678 "Cancel the currently running clock."
6679 (interactive "P")
6680 (unless (marker-buffer org-clock-marker)
6681 (error "No running clock"))
6682 (org-with-remote-undo (marker-buffer org-clock-marker)
6683 (org-clock-cancel)))
6684
8bfe682a
CD
6685(defun org-agenda-diary-entry-in-org-file ()
6686 "Make a diary entry in the file `org-agenda-diary-file'."
5dec9555 6687 (let (d1 d2 char (text "") dp1 dp2)
8bfe682a
CD
6688 (if (equal (buffer-name) "*Calendar*")
6689 (setq d1 (calendar-cursor-to-date t)
6690 d2 (car calendar-mark-ring))
5dec9555
CD
6691 (setq dp1 (get-text-property (point-at-bol) 'day))
6692 (unless dp1 (error "No date defined in current line"))
6693 (setq d1 (calendar-gregorian-from-absolute dp1)
6694 d2 (and (ignore-errors (mark))
6695 (save-excursion
6696 (goto-char (mark))
6697 (setq dp2 (get-text-property (point-at-bol) 'day)))
6698 (calendar-gregorian-from-absolute dp2))))
8bfe682a
CD
6699 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
6700 (setq char (read-char-exclusive))
6701 (cond
6702 ((equal char ?d)
6703 (setq text (read-string "Day entry: "))
5dec9555
CD
6704 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
6705 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
6706 ((equal char ?a)
6707 (setq d1 (list (car d1) (nth 1 d1)
6708 (read-number (format "Reference year [%d]: " (nth 2 d1))
6709 (nth 2 d1))))
6710 (setq text (read-string "Anniversary (use %d to show years): "))
5dec9555
CD
6711 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
6712 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
6713 ((equal char ?b)
6714 (setq text (read-string "Block entry: "))
6715 (unless (and d1 d2 (not (equal d1 d2)))
6716 (error "No block of days selected"))
5dec9555
CD
6717 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
6718 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
6719 ((equal char ?j)
6720 (org-switch-to-buffer-other-window
6721 (find-file-noselect org-agenda-diary-file))
6722 (org-datetree-find-date-create d1)
6723 (org-reveal t))
6724 (t (error "Invalid selection character `%c'" char)))))
6725
5dec9555
CD
6726(defcustom org-agenda-insert-diary-strategy 'date-tree
6727 "Where in `org-agenda-diary-file' should new entries be added?
6728Valid values:
6729
6730date-tree in the date tree, as child of the date
6731top-level as top-level entries at the end of the file."
6732 :group 'org-agenda
6733 :type '(choice
6734 (const :tag "in a date tree" date-tree)
6735 (const :tag "as top level at end of file" top-level)))
6736
8bfe682a
CD
6737(defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
6738 "Add a diary entry with TYPE to `org-agenda-diary-file'.
6739If TEXT is not empty, it will become the headline of the new entry, and
6740the resulting entry will not be shown. When TEXT is empty, switch to
6741`org-agenda-diary-file' and let the user finish the entry there."
6742 (let ((cw (current-window-configuration)))
6743 (org-switch-to-buffer-other-window
6744 (find-file-noselect org-agenda-diary-file))
6745 (widen)
6746 (goto-char (point-min))
6747 (cond
6748 ((eq type 'anniversary)
6749 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
6750 (progn
6751 (or (org-on-heading-p t)
6752 (progn
6753 (outline-next-heading)
6754 (insert "* Anniversaries\n\n")
6755 (beginning-of-line -1)))))
6756 (outline-next-heading)
6757 (org-back-over-empty-lines)
6758 (backward-char 1)
6759 (insert "\n")
6760 (require 'diary-lib)
6761 (let ((calendar-date-display-form
6762 (if (if (boundp 'calendar-date-style)
6763 (eq calendar-date-style 'european)
6764 european-calendar-style) ; Emacs 22
6765 '(day " " month " " year)
6766 '(month " " day " " year))))
6767
6768 (insert (format "%%%%(diary-anniversary %s) %s"
6769 (calendar-date-string d1 nil t) text))))
6770 ((eq type 'day)
5dec9555
CD
6771 (if (eq org-agenda-insert-diary-strategy 'top-level)
6772 (org-agenda-insert-diary-as-top-level text)
6773 (require 'org-datetree)
6774 (org-datetree-find-date-create d1)
6775 (org-agenda-insert-diary-make-new-entry text))
8bfe682a
CD
6776 (org-insert-time-stamp (org-time-from-absolute
6777 (calendar-absolute-from-gregorian d1)))
6778 (end-of-line 0))
6779 ((eq type 'block)
6780 (if (> (calendar-absolute-from-gregorian d1)
6781 (calendar-absolute-from-gregorian d2))
6782 (setq d1 (prog1 d2 (setq d2 d1))))
5dec9555
CD
6783 (if (eq org-agenda-insert-diary-strategy 'top-level)
6784 (org-agenda-insert-diary-as-top-level text)
6785 (require 'org-datetree)
6786 (org-datetree-find-date-create d1)
6787 (org-agenda-insert-diary-make-new-entry text))
8bfe682a
CD
6788 (org-insert-time-stamp (org-time-from-absolute
6789 (calendar-absolute-from-gregorian d1)))
6790 (insert "--")
6791 (org-insert-time-stamp (org-time-from-absolute
6792 (calendar-absolute-from-gregorian d2)))
6793 (end-of-line 0)))
6794 (if (string-match "\\S-" text)
6795 (progn
6796 (set-window-configuration cw)
6797 (message "%s entry added to %s"
6798 (capitalize (symbol-name type))
6799 (abbreviate-file-name org-agenda-diary-file)))
6800 (org-reveal t)
6801 (message "Please finish entry here"))))
6802
5dec9555
CD
6803(defun org-agenda-insert-diary-as-top-level (text)
6804 "Make new entry as a top-level entry at the end of the file.
6805Add TEXT as headline, and position the cursor in the second line so that
6806a timestamp can be added there."
6807 (widen)
6808 (goto-char (point-max))
6809 (or (bolp) (insert "\n"))
6810 (insert "* " text "\n")
6811 (if org-adapt-indentation (org-indent-to-column 2)))
6812
8bfe682a
CD
6813(defun org-agenda-insert-diary-make-new-entry (text)
6814 "Make new entry as last child of current entry.
6815Add TEXT as headline, and position the cursor in the second line so that
6816a timestamp can be added there."
6817 (let ((org-show-following-heading t)
6818 (org-show-siblings t)
6819 (org-show-hierarchy-above t)
6820 (org-show-entry-below t)
6821 col)
6822 (outline-next-heading)
6823 (org-back-over-empty-lines)
6824 (or (looking-at "[ \t]*$")
6825 (progn (insert "\n") (backward-char 1)))
6826 (org-insert-heading)
6827 (org-do-demote)
6828 (setq col (current-column))
6829 (insert text "\n")
6830 (if org-adapt-indentation (org-indent-to-column col))
6831 (let ((org-show-following-heading t)
6832 (org-show-siblings t)
6833 (org-show-hierarchy-above t)
6834 (org-show-entry-below t))
6835 (org-show-context))))
6836
20908596
CD
6837(defun org-agenda-diary-entry ()
6838 "Make a diary entry, like the `i' command from the calendar.
8bfe682a
CD
6839All the standard commands work: block, weekly etc.
6840When `org-agenda-diary-file' points to a file,
6841`org-agenda-diary-entry-in-org-file' is called instead to create
6842entries in that Org-mode file."
20908596
CD
6843 (interactive)
6844 (org-agenda-check-type t 'agenda 'timeline)
8bfe682a
CD
6845 (if (not (eq org-agenda-diary-file 'diary-file))
6846 (org-agenda-diary-entry-in-org-file)
6847 (require 'diary-lib)
6848 (let* ((char (progn
6849 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
6850 (read-char-exclusive)))
6851 (cmd (cdr (assoc char
6852 '((?d . insert-diary-entry)
6853 (?w . insert-weekly-diary-entry)
6854 (?m . insert-monthly-diary-entry)
6855 (?y . insert-yearly-diary-entry)
6856 (?a . insert-anniversary-diary-entry)
6857 (?b . insert-block-diary-entry)
6858 (?c . insert-cyclic-diary-entry)))))
6859 (oldf (symbol-function 'calendar-cursor-to-date))
6860 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
6861 (point (point))
6862 (mark (or (mark t) (point))))
6863 (unless cmd
6864 (error "No command associated with <%c>" char))
6865 (unless (and (get-text-property point 'day)
6866 (or (not (equal ?b char))
6867 (get-text-property mark 'day)))
6868 (error "Don't know which date to use for diary entry"))
6869 ;; We implement this by hacking the `calendar-cursor-to-date' function
6870 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
6871 (let ((calendar-mark-ring
6872 (list (calendar-gregorian-from-absolute
6873 (or (get-text-property mark 'day)
6874 (get-text-property point 'day))))))
6875 (unwind-protect
6876 (progn
6877 (fset 'calendar-cursor-to-date
6878 (lambda (&optional error dummy)
6879 (calendar-gregorian-from-absolute
6880 (get-text-property point 'day))))
20908596 6881 (call-interactively cmd))
8bfe682a 6882 (fset 'calendar-cursor-to-date oldf))))))
20908596 6883
20908596
CD
6884(defun org-agenda-execute-calendar-command (cmd)
6885 "Execute a calendar command from the agenda, with the date associated to
6886the cursor position."
6887 (org-agenda-check-type t 'agenda 'timeline)
6888 (require 'diary-lib)
6889 (unless (get-text-property (point) 'day)
6890 (error "Don't know which date to use for calendar command"))
6891 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
6892 (point (point))
6893 (date (calendar-gregorian-from-absolute
6894 (get-text-property point 'day)))
6895 ;; the following 2 vars are needed in the calendar
6896 (displayed-month (car date))
6897 (displayed-year (nth 2 date)))
6898 (unwind-protect
6899 (progn
6900 (fset 'calendar-cursor-to-date
0627c265 6901 (lambda (&optional error dummy)
20908596
CD
6902 (calendar-gregorian-from-absolute
6903 (get-text-property point 'day))))
6904 (call-interactively cmd))
6905 (fset 'calendar-cursor-to-date oldf))))
6906
6907(defun org-agenda-phases-of-moon ()
6908 "Display the phases of the moon for the 3 months around the cursor date."
6909 (interactive)
6910 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
6911
6912(defun org-agenda-holidays ()
6913 "Display the holidays for the 3 months around the cursor date."
6914 (interactive)
6915 (org-agenda-execute-calendar-command 'list-calendar-holidays))
6916
6917(defvar calendar-longitude)
6918(defvar calendar-latitude)
6919(defvar calendar-location-name)
6920
6921(defun org-agenda-sunrise-sunset (arg)
6922 "Display sunrise and sunset for the cursor date.
6923Latitude and longitude can be specified with the variables
6924`calendar-latitude' and `calendar-longitude'. When called with prefix
6925argument, latitude and longitude will be prompted for."
6926 (interactive "P")
6927 (require 'solar)
6928 (let ((calendar-longitude (if arg nil calendar-longitude))
6929 (calendar-latitude (if arg nil calendar-latitude))
6930 (calendar-location-name
6931 (if arg "the given coordinates" calendar-location-name)))
6932 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
6933
6934(defun org-agenda-goto-calendar ()
6935 "Open the Emacs calendar with the date at the cursor."
6936 (interactive)
6937 (org-agenda-check-type t 'agenda 'timeline)
6938 (let* ((day (or (get-text-property (point) 'day)
6939 (error "Don't know which date to open in calendar")))
6940 (date (calendar-gregorian-from-absolute day))
6941 (calendar-move-hook nil)
6942 (calendar-view-holidays-initially-flag nil)
6943 (calendar-view-diary-initially-flag nil)
6944 (view-calendar-holidays-initially nil)
20908596
CD
6945 (view-diary-entries-initially nil))
6946 (calendar)
6947 (calendar-goto-date date)))
6948
6949;;;###autoload
6950(defun org-calendar-goto-agenda ()
6951 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
6952This is a command that has to be installed in `calendar-mode-map'."
6953 (interactive)
6954 (org-agenda-list nil (calendar-absolute-from-gregorian
6955 (calendar-cursor-to-date))
6956 nil))
6957
6958(defun org-agenda-convert-date ()
6959 (interactive)
6960 (org-agenda-check-type t 'agenda 'timeline)
6961 (let ((day (get-text-property (point) 'day))
6962 date s)
6963 (unless day
6964 (error "Don't know which date to convert"))
6965 (setq date (calendar-gregorian-from-absolute day))
6966 (setq s (concat
6967 "Gregorian: " (calendar-date-string date) "\n"
6968 "ISO: " (calendar-iso-date-string date) "\n"
6969 "Day of Yr: " (calendar-day-of-year-string date) "\n"
6970 "Julian: " (calendar-julian-date-string date) "\n"
6971 "Astron. JD: " (calendar-astro-date-string date)
6972 " (Julian date number at noon UTC)\n"
6973 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
6974 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
6975 "French: " (calendar-french-date-string date) "\n"
6976 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
6977 "Mayan: " (calendar-mayan-date-string date) "\n"
6978 "Coptic: " (calendar-coptic-date-string date) "\n"
6979 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
6980 "Persian: " (calendar-persian-date-string date) "\n"
6981 "Chinese: " (calendar-chinese-date-string date) "\n"))
6982 (with-output-to-temp-buffer "*Dates*"
6983 (princ s))
93b62de8 6984 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
20908596 6985
c8d0cf5c
CD
6986;;; Bulk commands
6987
6988(defvar org-agenda-bulk-marked-entries nil
6989 "List of markers that refer to marked entries in the agenda.")
6990
54a0dee5
CD
6991(defun org-agenda-bulk-marked-p ()
6992 (eq (get-char-property (point-at-bol) 'type)
6993 'org-marked-entry-overlay))
6994
c8d0cf5c
CD
6995(defun org-agenda-bulk-mark ()
6996 "Mark the entry at point for future bulk action."
6997 (interactive)
6998 (org-agenda-check-no-diary)
8d642074 6999 (let* ((m (org-get-at-bol 'org-hd-marker))
c8d0cf5c 7000 ov)
54a0dee5 7001 (unless (org-agenda-bulk-marked-p)
c8d0cf5c
CD
7002 (unless m (error "Nothing to mark at point"))
7003 (push m org-agenda-bulk-marked-entries)
7004 (setq ov (org-make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8d642074 7005 (org-overlay-display ov "> "
c8d0cf5c
CD
7006 (org-get-todo-face "TODO")
7007 'evaporate)
7008 (org-overlay-put ov 'type 'org-marked-entry-overlay))
7009 (beginning-of-line 2)
7010 (message "%d entries marked for bulk action"
7011 (length org-agenda-bulk-marked-entries))))
7012
7013(defun org-agenda-bulk-unmark ()
7014 "Unmark the entry at point for future bulk action."
7015 (interactive)
54a0dee5 7016 (when (org-agenda-bulk-marked-p)
c8d0cf5c
CD
7017 (org-agenda-bulk-remove-overlays
7018 (point-at-bol) (+ 2 (point-at-bol)))
7019 (setq org-agenda-bulk-marked-entries
8d642074 7020 (delete (org-get-at-bol 'org-hd-marker)
c8d0cf5c
CD
7021 org-agenda-bulk-marked-entries)))
7022 (beginning-of-line 2)
7023 (message "%d entries marked for bulk action"
7024 (length org-agenda-bulk-marked-entries)))
7025
54a0dee5
CD
7026(defun org-agenda-bulk-toggle ()
7027 "Toggle marking the entry at point for bulk action."
7028 (interactive)
7029 (if (org-agenda-bulk-marked-p)
7030 (org-agenda-bulk-unmark)
7031 (org-agenda-bulk-mark)))
c8d0cf5c
CD
7032
7033(defun org-agenda-bulk-remove-overlays (&optional beg end)
7034 "Remove the mark overlays between BEG and END in the agenda buffer.
7035BEG and END default to the buffer limits.
7036
7037This only removes the overlays, it does not remove the markers
7038from the list in `org-agenda-bulk-marked-entries'."
7039 (interactive)
7040 (mapc (lambda (ov)
7041 (and (eq (org-overlay-get ov 'type) 'org-marked-entry-overlay)
7042 (org-delete-overlay ov)))
7043 (org-overlays-in (or beg (point-min)) (or end (point-max)))))
7044
7045(defun org-agenda-bulk-remove-all-marks ()
7046 "Remove all marks in the agenda buffer.
7047This will remove the markers, and the overlays."
7048 (interactive)
7049 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
7050 (setq org-agenda-bulk-marked-entries nil)
7051 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
7052
7053(defun org-agenda-bulk-action ()
7054 "Execute an remote-editing action on all marked entries."
7055 (interactive)
7056 (unless org-agenda-bulk-marked-entries
7057 (error "No entries are marked"))
7058 (message "Bulk: [r]efile [$]archive [A]rch->sib [t]odo [+/-]tag [s]chedule [d]eadline")
7059 (let* ((action (read-char-exclusive))
7060 (entries (reverse org-agenda-bulk-marked-entries))
7061 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
7062 (cond
7063 ((equal action ?$)
7064 (setq cmd '(org-agenda-archive)))
7065
7066 ((equal action ?A)
7067 (setq cmd '(org-agenda-archive-to-archive-sibling)))
7068
7069 ((member action '(?r ?w))
7070 (setq rfloc (org-refile-get-location
7071 "Refile to: "
7072 (marker-buffer (car org-agenda-bulk-marked-entries))
7073 org-refile-allow-creating-parent-nodes))
7074 (setcar (nthcdr 3 rfloc)
7075 (move-marker (make-marker) (nth 3 rfloc)
7076 (or (get-file-buffer (nth 1 rfloc))
7077 (find-buffer-visiting (nth 1 rfloc))
7078 (error "This should not happen"))))
7079
7080 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc))))
7081
7082 ((equal action ?t)
54a0dee5 7083 (setq state (org-icompleting-read
c8d0cf5c
CD
7084 "Todo state: "
7085 (with-current-buffer (marker-buffer (car entries))
7086 (mapcar 'list org-todo-keywords-1))))
7087 (setq cmd `(let ((org-inhibit-blocking t)
7088 (org-inhibit-logging 'note))
7089 (org-agenda-todo ,state))))
7090
7091 ((memq action '(?- ?+))
54a0dee5 7092 (setq tag (org-icompleting-read
c8d0cf5c
CD
7093 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
7094 (with-current-buffer (marker-buffer (car entries))
7095 (delq nil
7096 (mapcar (lambda (x)
7097 (if (stringp (car x)) x)) org-tag-alist)))))
7098 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
7099
7100 ((memq action '(?s ?d))
7101 (let* ((date (org-read-date
7102 nil nil nil
7103 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to")))
7104 (ans org-read-date-final-answer)
7105 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
7106 (setq cmd `(let* ((bound (fboundp 'read-string))
7107 (old (and bound (symbol-function 'read-string))))
7108 (unwind-protect
7109 (progn
7110 (fset 'read-string (lambda (&rest ignore) ,ans))
7111 (call-interactively ',c1))
7112 (if bound
7113 (fset 'read-string old)
7114 (fmakunbound 'read-string)))))))
7115 (t (error "Invalid bulk action")))
7116
7117 ;; Sort the markers, to make sure that parents are handled before children
7118 (setq entries (sort entries
7119 (lambda (a b)
7120 (cond
7121 ((equal (marker-buffer a) (marker-buffer b))
7122 (< (marker-position a) (marker-position b)))
7123 (t
7124 (string< (buffer-name (marker-buffer a))
7125 (buffer-name (marker-buffer b))))))))
7126
7127 ;; Now loop over all markers and apply cmd
7128 (while (setq e (pop entries))
7129 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
7130 (if (not pos)
7131 (progn (message "Skipping removed entry at %s" e)
7132 (setq cntskip (1+ cntskip)))
7133 (goto-char pos)
7134 (eval cmd)
7135 (setq org-agenda-bulk-marked-entries
7136 (delete e org-agenda-bulk-marked-entries))
7137 (setq cnt (1+ cnt))))
7138 (setq org-agenda-bulk-marked-entries nil)
7139 (org-agenda-bulk-remove-all-marks)
7140 (message "Acted on %d entries%s"
7141 cnt
7142 (if (= cntskip 0)
7143 ""
7144 (format ", skipped %d (disappeared before their turn)"
7145 cntskip)))))
8d642074
CD
7146
7147;;; Flagging notes
7148
7149(defun org-agenda-show-the-flagging-note ()
7150 "Display the flagging note in the other window.
7151When called a second time in direct sequence, offer to remove the FLAGGING
7152tag and (if present) the flagging note."
7153 (interactive)
7154 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
7155 (win (selected-window))
7156 note heading newhead)
7157 (unless hdmarker
7158 (error "No linked entry at point"))
7159 (if (and (eq this-command last-command)
7160 (y-or-n-p "Unflag and remove any flagging note? "))
7161 (progn
7162 (org-agenda-remove-flag hdmarker)
7163 (let ((win (get-buffer-window "*Flagging Note*")))
7164 (and win (delete-window win)))
7165 (message "Entry unflaged"))
7166 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
7167 (unless note
7168 (error "No flagging note"))
7169 (org-kill-new note)
7170 (org-switch-to-buffer-other-window "*Flagging Note*")
7171 (erase-buffer)
7172 (insert note)
7173 (goto-char (point-min))
7174 (while (re-search-forward "\\\\n" nil t)
7175 (replace-match "\n" t t))
7176 (goto-char (point-min))
7177 (select-window win)
7178 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
7179
7180(defun org-agenda-remove-flag (marker)
8bfe682a 7181 "Remove the FLAGGED tag and any flagging note in the entry."
8d642074
CD
7182 (let (newhead)
7183 (org-with-point-at marker
7184 (org-toggle-tag "FLAGGED" 'off)
7185 (org-entry-delete nil "THEFLAGGINGNOTE")
7186 (setq newhead (org-get-heading)))
7187 (org-agenda-change-all-lines newhead marker)
7188 (message "Entry unflaged")))
7189
7190(defun org-agenda-get-any-marker (&optional pos)
7191 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
7192 (get-text-property (or pos (point-at-bol)) 'org-marker)))
c8d0cf5c 7193
20908596
CD
7194;;; Appointment reminders
7195
7196(defvar appt-time-msg-list)
7197
7198;;;###autoload
7199(defun org-agenda-to-appt (&optional refresh filter)
7200 "Activate appointments found in `org-agenda-files'.
7201With a \\[universal-argument] prefix, refresh the list of
33306645 7202appointments.
20908596
CD
7203
7204If FILTER is t, interactively prompt the user for a regular
7205expression, and filter out entries that don't match it.
7206
7207If FILTER is a string, use this string as a regular expression
7208for filtering entries out.
7209
7210FILTER can also be an alist with the car of each cell being
7211either 'headline or 'category. For example:
7212
7213 '((headline \"IMPORTANT\")
7214 (category \"Work\"))
7215
7216will only add headlines containing IMPORTANT or headlines
7217belonging to the \"Work\" category."
7218 (interactive "P")
7219 (require 'calendar)
7220 (if refresh (setq appt-time-msg-list nil))
7221 (if (eq filter t)
7222 (setq filter (read-from-minibuffer "Regexp filter: ")))
7223 (let* ((cnt 0) ; count added events
7224 (org-agenda-new-buffers nil)
7225 (org-deadline-warning-days 0)
7226 (today (org-date-to-gregorian
7227 (time-to-days (current-time))))
c8d0cf5c 7228 (org-agenda-restrict nil)
621f83e4 7229 (files (org-agenda-files 'unrestricted)) entries file)
20908596 7230 ;; Get all entries which may contain an appt
db55f368 7231 (org-prepare-agenda-buffers files)
20908596
CD
7232 (while (setq file (pop files))
7233 (setq entries
7234 (append entries
7235 (org-agenda-get-day-entries
7236 file today :timestamp :scheduled :deadline))))
7237 (setq entries (delq nil entries))
7238 ;; Map thru entries and find if we should filter them out
7239 (mapc
7240 (lambda(x)
621f83e4 7241 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
20908596
CD
7242 (cat (get-text-property 1 'org-category x))
7243 (tod (get-text-property 1 'time-of-day x))
7244 (ok (or (null filter)
7245 (and (stringp filter) (string-match filter evt))
7246 (and (listp filter)
7247 (or (string-match
7248 (cadr (assoc 'category filter)) cat)
7249 (string-match
7250 (cadr (assoc 'headline filter)) evt))))))
7251 ;; FIXME: Shall we remove text-properties for the appt text?
7252 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
7253 (when (and ok tod)
621f83e4 7254 (setq tod (concat "00" (number-to-string tod))
20908596 7255 tod (when (string-match
621f83e4 7256 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
20908596
CD
7257 (concat (match-string 1 tod) ":"
7258 (match-string 2 tod))))
7259 (appt-add tod evt)
7260 (setq cnt (1+ cnt))))) entries)
7261 (org-release-buffers org-agenda-new-buffers)
7262 (if (eq cnt 0)
7263 (message "No event to add")
7264 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
7265
621f83e4
CD
7266(defun org-agenda-todayp (date)
7267 "Does DATE mean today, when considering `org-extend-today-until'?"
7268 (let (today h)
7269 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
7270 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
7271 (setq h (nth 2 (decode-time (current-time))))
7272 (or (and (>= h org-extend-today-until)
7273 (= date today))
7274 (and (< h org-extend-today-until)
7275 (= date (1- today))))))
7276
20908596
CD
7277(provide 'org-agenda)
7278
b349f79f
CD
7279;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
7280
20908596 7281;;; org-agenda.el ends here