Merge from emacs-23; up to 2010-06-16T23:27:20Z!jay.p.belanger@gmail.com.
[bpt/emacs.git] / lisp / org / org-agenda.el
CommitLineData
b349f79f 1;;; org-agenda.el --- Dynamic task and appointment lists for Org
20908596 2
95df8112 3;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
20908596
CD
4
5;; Author: Carsten Dominik <carsten at orgmode dot org>
6;; Keywords: outlines, hypermedia, calendar, wp
7;; Homepage: http://orgmode.org
acedf35c 8;; Version: 7.4
20908596
CD
9;;
10;; This file is part of GNU Emacs.
11;;
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
20908596
CD
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
24;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25;;
26;;; Commentary:
27
28;; This file contains the code for creating and using the Agenda for Org-mode.
29
30;;; Code:
31
32(require 'org)
33(eval-when-compile
86fbb8ca 34 (require 'cl))
20908596 35
b349f79f 36(declare-function diary-add-to-list "diary-lib"
20908596
CD
37 (date string specifier &optional marker globcolor literal))
38(declare-function calendar-absolute-from-iso "cal-iso" (date))
39(declare-function calendar-astro-date-string "cal-julian" (&optional date))
40(declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
20908596
CD
41(declare-function calendar-chinese-date-string "cal-china" (&optional date))
42(declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
43(declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
44(declare-function calendar-french-date-string "cal-french" (&optional date))
45(declare-function calendar-goto-date "cal-move" (date))
46(declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
47(declare-function calendar-islamic-date-string "cal-islam" (&optional date))
48(declare-function calendar-iso-date-string "cal-iso" (&optional date))
f6aafbed 49(declare-function calendar-iso-from-absolute "cal-iso" (date))
20908596
CD
50(declare-function calendar-julian-date-string "cal-julian" (&optional date))
51(declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
52(declare-function calendar-persian-date-string "cal-persia" (&optional date))
68a1b090
GM
53(declare-function org-datetree-find-date-create "org-datetree"
54 (date &optional keep-restriction))
20908596 55(declare-function org-columns-quit "org-colview" ())
8bfe682a
CD
56(declare-function diary-date-display-form "diary-lib" (&optional type))
57(declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
58(declare-function org-habit-insert-consistency-graphs
59 "org-habit" (&optional line))
60(declare-function org-is-habit-p "org-habit" (&optional pom))
61(declare-function org-habit-parse-todo "org-habit" (&optional pom))
68a1b090 62(declare-function org-habit-get-priority "org-habit" (habit &optional moment))
20908596 63(defvar calendar-mode-map)
afe98dfa 64(defvar org-clock-current-task) ; defined in org-clock.el
8d642074 65(defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
8bfe682a
CD
66(defvar org-habit-show-habits)
67(defvar org-habit-show-habits-only-for-today)
20908596
CD
68
69;; Defined somewhere in this file, but used before definition.
70(defvar org-agenda-buffer-name)
71(defvar org-agenda-overriding-header)
8d642074 72(defvar org-agenda-title-append nil)
20908596
CD
73(defvar entry)
74(defvar date)
75(defvar org-agenda-undo-list)
76(defvar org-agenda-pending-undo-list)
77(defvar original-date) ; dynamically scoped, calendar.el does scope this
78
79(defcustom org-agenda-confirm-kill 1
80 "When set, remote killing from the agenda buffer needs confirmation.
81When t, a confirmation is always needed. When a number N, confirmation is
82only needed when the text to be killed contains more than N non-white lines."
83 :group 'org-agenda
84 :type '(choice
85 (const :tag "Never" nil)
86 (const :tag "Always" t)
c8d0cf5c 87 (integer :tag "When more than N lines")))
20908596
CD
88
89(defcustom org-agenda-compact-blocks nil
ed21c5c8 90 "Non-nil means make the block agenda more compact.
20908596
CD
91This is done by leaving out unnecessary lines."
92 :group 'org-agenda
93 :type 'boolean)
94
0bd48b37
CD
95(defcustom org-agenda-block-separator ?=
96 "The separator between blocks in the agenda.
97If this is a string, it will be used as the separator, with a newline added.
98If it is a character, it will be repeated to fill the window width."
99 :group 'org-agenda
100 :type '(choice
101 (character)
102 (string)))
103
20908596
CD
104(defgroup org-agenda-export nil
105 "Options concerning exporting agenda views in Org-mode."
106 :tag "Org Agenda Export"
107 :group 'org-agenda)
108
109(defcustom org-agenda-with-colors t
ed21c5c8 110 "Non-nil means use colors in agenda views."
20908596
CD
111 :group 'org-agenda-export
112 :type 'boolean)
113
114(defcustom org-agenda-exporter-settings nil
115 "Alist of variable/value pairs that should be active during agenda export.
c8d0cf5c
CD
116This is a good place to set options for ps-print and for htmlize.
117Note that the way this is implemented, the values will be evaluated
118before assigned to the variables. So make sure to quote values you do
119*not* want evaluated, for example
120
121 (setq org-agenda-exporter-settings
122 '((ps-print-color-p 'black-white)))"
20908596
CD
123 :group 'org-agenda-export
124 :type '(repeat
125 (list
126 (variable)
127 (sexp :tag "Value"))))
128
c8d0cf5c
CD
129(defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
130 "Hook run in temporary buffer before writing it to an export file.
131A useful function is `org-agenda-add-entry-text'."
132 :group 'org-agenda-export
133 :type 'hook
134 :options '(org-agenda-add-entry-text))
135
136(defcustom org-agenda-add-entry-text-maxlines 0
137 "Maximum number of entry text lines to be added to agenda.
138This is only relevant when `org-agenda-add-entry-text' is part of
139`org-agenda-before-write-hook', which it is by default.
140When this is 0, nothing will happen. When it is greater than 0, it
141specifies the maximum number of lines that will be added for each entry
54a0dee5
CD
142that is listed in the agenda view.
143
144Note that this variable is not used during display, only when exporting
86fbb8ca
CD
145the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
146and `org-agenda-entry-text-maxlines'."
c8d0cf5c
CD
147 :group 'org-agenda
148 :type 'integer)
149
150(defcustom org-agenda-add-entry-text-descriptive-links t
ed21c5c8 151 "Non-nil means export org-links as descriptive links in agenda added text.
c8d0cf5c
CD
152This variable applies to the text added to the agenda when
153`org-agenda-add-entry-text-maxlines' is larger than 0.
154When this variable nil, the URL will (also) be shown."
155 :group 'org-agenda
156 :type 'boolean)
157
20908596
CD
158(defcustom org-agenda-export-html-style ""
159 "The style specification for exported HTML Agenda files.
160If this variable contains a string, it will replace the default <style>
161section as produced by `htmlize'.
162Since there are different ways of setting style information, this variable
163needs to contain the full HTML structure to provide a style, including the
164surrounding HTML tags. The style specifications should include definitions
165the fonts used by the agenda, here is an example:
166
167 <style type=\"text/css\">
168 p { font-weight: normal; color: gray; }
169 .org-agenda-structure {
170 font-size: 110%;
171 color: #003399;
172 font-weight: 600;
173 }
174 .org-todo {
175 color: #cc6666;
176 font-weight: bold;
177 }
c8d0cf5c
CD
178 .org-agenda-done {
179 color: #339933;
180 }
20908596
CD
181 .org-done {
182 color: #339933;
183 }
184 .title { text-align: center; }
185 .todo, .deadline { color: red; }
186 .done { color: green; }
187 </style>
188
189or, if you want to keep the style in a file,
190
191 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
192
193As the value of this option simply gets inserted into the HTML <head> header,
194you can \"misuse\" it to also add other text to the header. However,
195<style>...</style> is required, if not present the variable will be ignored."
196 :group 'org-agenda-export
197 :group 'org-export-html
198 :type 'string)
199
86fbb8ca
CD
200(defcustom org-agenda-persistent-filter nil
201 "When set, keep filters from one agenda view to the next."
202 :group 'org-agenda
203 :type 'boolean)
204
20908596
CD
205(defgroup org-agenda-custom-commands nil
206 "Options concerning agenda views in Org-mode."
207 :tag "Org Agenda Custom Commands"
208 :group 'org-agenda)
209
210(defconst org-sorting-choice
211 '(choice
212 (const time-up) (const time-down)
213 (const category-keep) (const category-up) (const category-down)
214 (const tag-down) (const tag-up)
215 (const priority-up) (const priority-down)
621f83e4 216 (const todo-state-up) (const todo-state-down)
c8d0cf5c 217 (const effort-up) (const effort-down)
8bfe682a 218 (const habit-up) (const habit-down)
86fbb8ca 219 (const alpha-up) (const alpha-down)
c8d0cf5c 220 (const user-defined-up) (const user-defined-down))
20908596
CD
221 "Sorting choices.")
222
223(defconst org-agenda-custom-commands-local-options
224 `(repeat :tag "Local settings for this command. Remember to quote values"
225 (choice :tag "Setting"
c8d0cf5c
CD
226 (list :tag "Heading for this block"
227 (const org-agenda-overriding-header)
228 (string :tag "Headline"))
20908596
CD
229 (list :tag "Files to be searched"
230 (const org-agenda-files)
231 (list
232 (const :format "" quote)
c8d0cf5c 233 (repeat (file))))
20908596
CD
234 (list :tag "Sorting strategy"
235 (const org-agenda-sorting-strategy)
236 (list
237 (const :format "" quote)
238 (repeat
239 ,org-sorting-choice)))
240 (list :tag "Prefix format"
241 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
242 (string))
243 (list :tag "Number of days in agenda"
acedf35c
CD
244 (const org-agenda-span)
245 (choice (const :tag "Day" 'day)
246 (const :tag "Week" 'week)
247 (const :tag "Month" 'month)
248 (const :tag "Year" 'year)
249 (integer :tag "Custom")))
20908596
CD
250 (list :tag "Fixed starting date"
251 (const org-agenda-start-day)
252 (string :value "2007-11-01"))
253 (list :tag "Start on day of week"
254 (const org-agenda-start-on-weekday)
255 (choice :value 1
256 (const :tag "Today" nil)
c8d0cf5c 257 (integer :tag "Weekday No.")))
20908596
CD
258 (list :tag "Include data from diary"
259 (const org-agenda-include-diary)
260 (boolean))
261 (list :tag "Deadline Warning days"
262 (const org-deadline-warning-days)
263 (integer :value 1))
c8d0cf5c
CD
264 (list :tag "Tags filter preset"
265 (const org-agenda-filter-preset)
266 (list
267 (const :format "" quote)
268 (repeat
269 (string :tag "+tag or -tag"))))
ed21c5c8
CD
270 (list :tag "Set daily/weekly entry types"
271 (const org-agenda-entry-types)
272 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
273 (const :deadline)
274 (const :scheduled)
275 (const :timestamp)
276 (const :sexp)))
20908596
CD
277 (list :tag "Standard skipping condition"
278 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
279 (const org-agenda-skip-function)
280 (list
281 (const :format "" quote)
282 (list
283 (choice
33306645 284 :tag "Skipping range"
20908596
CD
285 (const :tag "Skip entry" org-agenda-skip-entry-if)
286 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
287 (repeat :inline t :tag "Conditions for skipping"
288 (choice
289 :tag "Condition type"
290 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
291 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
ed21c5c8
CD
292 (list :tag "TODO state is" :inline t
293 (const 'todo)
294 (choice
295 (const :tag "any not-done state" 'todo)
296 (const :tag "any done state" 'done)
297 (const :tag "any state" 'any)
298 (list :tag "Keyword list"
299 (const :format "" quote)
300 (repeat (string :tag "Keyword")))))
301 (list :tag "TODO state is not" :inline t
302 (const 'nottodo)
303 (choice
304 (const :tag "any not-done state" 'todo)
305 (const :tag "any done state" 'done)
306 (const :tag "any state" 'any)
307 (list :tag "Keyword list"
308 (const :format "" quote)
309 (repeat (string :tag "Keyword")))))
20908596
CD
310 (const :tag "scheduled" 'scheduled)
311 (const :tag "not scheduled" 'notscheduled)
312 (const :tag "deadline" 'deadline)
c8d0cf5c
CD
313 (const :tag "no deadline" 'notdeadline)
314 (const :tag "timestamp" 'timestamp)
315 (const :tag "no timestamp" 'nottimestamp))))))
20908596
CD
316 (list :tag "Non-standard skipping condition"
317 :value (org-agenda-skip-function)
2c3ad40d 318 (const org-agenda-skip-function)
c8d0cf5c
CD
319 (sexp :tag "Function or form (quoted!)"))
320 (list :tag "Any variable"
321 (variable :tag "Variable")
322 (sexp :tag "Value (sexp)"))))
20908596
CD
323 "Selection of examples for agenda command settings.
324This will be spliced into the custom type of
325`org-agenda-custom-commands'.")
326
327
328(defcustom org-agenda-custom-commands nil
329 "Custom commands for the agenda.
330These commands will be offered on the splash screen displayed by the
331agenda dispatcher \\[org-agenda]. Each entry is a list like this:
332
333 (key desc type match settings files)
334
335key The key (one or more characters as a string) to be associated
336 with the command.
337desc A description of the command, when omitted or nil, a default
338 description is built using MATCH.
339type The command type, any of the following symbols:
340 agenda The daily/weekly agenda.
341 todo Entries with a specific TODO keyword, in all agenda files.
342 search Entries containing search words entry or headline.
343 tags Tags/Property/TODO match in all agenda files.
344 tags-todo Tags/P/T match in all agenda files, TODO entries only.
345 todo-tree Sparse tree of specific TODO keyword in *current* file.
346 tags-tree Sparse tree with all tags matches in *current* file.
347 occur-tree Occur sparse tree for *current* file.
348 ... A user-defined function.
349match What to search for:
350 - a single keyword for TODO keyword searches
351 - a tags match expression for tags searches
352 - a word search expression for text searches.
353 - a regular expression for occur searches
354 For all other commands, this should be the empty string.
355settings A list of option settings, similar to that in a let form, so like
356 this: ((opt1 val1) (opt2 val2) ...). The values will be
357 evaluated at the moment of execution, so quote them when needed.
358files A list of files file to write the produced agenda buffer to
359 with the command `org-store-agenda-views'.
360 If a file name ends in \".html\", an HTML version of the buffer
361 is written out. If it ends in \".ps\", a postscript version is
33306645 362 produced. Otherwise, only the plain text is written to the file.
20908596
CD
363
364You can also define a set of commands, to create a composite agenda buffer.
365In this case, an entry looks like this:
366
367 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
368
369where
370
371desc A description string to be displayed in the dispatcher menu.
372cmd An agenda command, similar to the above. However, tree commands
373 are no allowed, but instead you can get agenda and global todo list.
374 So valid commands for a set are:
375 (agenda \"\" settings)
376 (alltodo \"\" settings)
377 (stuck \"\" settings)
378 (todo \"match\" settings files)
379 (search \"match\" settings files)
380 (tags \"match\" settings files)
381 (tags-todo \"match\" settings files)
382
383Each command can carry a list of options, and another set of options can be
384given for the whole set of commands. Individual command options take
385precedence over the general options.
386
387When using several characters as key to a command, the first characters
388are prefix commands. For the dispatcher to display useful information, you
389should provide a description for the prefix, like
390
391 (setq org-agenda-custom-commands
392 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
393 (\"hl\" tags \"+HOME+Lisa\")
394 (\"hp\" tags \"+HOME+Peter\")
395 (\"hk\" tags \"+HOME+Kim\")))"
396 :group 'org-agenda-custom-commands
397 :type `(repeat
398 (choice :value ("x" "Describe command here" tags "" nil)
399 (list :tag "Single command"
400 (string :tag "Access Key(s) ")
401 (option (string :tag "Description"))
402 (choice
403 (const :tag "Agenda" agenda)
404 (const :tag "TODO list" alltodo)
405 (const :tag "Search words" search)
406 (const :tag "Stuck projects" stuck)
c8d0cf5c
CD
407 (const :tag "Tags/Property match (all agenda files)" tags)
408 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
20908596
CD
409 (const :tag "TODO keyword search (all agenda files)" todo)
410 (const :tag "Tags sparse tree (current buffer)" tags-tree)
411 (const :tag "TODO keyword tree (current buffer)" todo-tree)
412 (const :tag "Occur tree (current buffer)" occur-tree)
413 (sexp :tag "Other, user-defined function"))
414 (string :tag "Match (only for some commands)")
415 ,org-agenda-custom-commands-local-options
416 (option (repeat :tag "Export" (file :tag "Export to"))))
417 (list :tag "Command series, all agenda files"
418 (string :tag "Access Key(s)")
419 (string :tag "Description ")
420 (repeat :tag "Component"
421 (choice
422 (list :tag "Agenda"
423 (const :format "" agenda)
424 (const :tag "" :format "" "")
425 ,org-agenda-custom-commands-local-options)
426 (list :tag "TODO list (all keywords)"
427 (const :format "" alltodo)
428 (const :tag "" :format "" "")
429 ,org-agenda-custom-commands-local-options)
430 (list :tag "Search words"
431 (const :format "" search)
432 (string :tag "Match")
433 ,org-agenda-custom-commands-local-options)
434 (list :tag "Stuck projects"
435 (const :format "" stuck)
436 (const :tag "" :format "" "")
437 ,org-agenda-custom-commands-local-options)
438 (list :tag "Tags search"
439 (const :format "" tags)
440 (string :tag "Match")
441 ,org-agenda-custom-commands-local-options)
442 (list :tag "Tags search, TODO entries only"
443 (const :format "" tags-todo)
444 (string :tag "Match")
445 ,org-agenda-custom-commands-local-options)
446 (list :tag "TODO keyword search"
447 (const :format "" todo)
448 (string :tag "Match")
449 ,org-agenda-custom-commands-local-options)
450 (list :tag "Other, user-defined function"
451 (symbol :tag "function")
452 (string :tag "Match")
453 ,org-agenda-custom-commands-local-options)))
454
455 (repeat :tag "Settings for entire command set"
456 (list (variable :tag "Any variable")
457 (sexp :tag "Value")))
458 (option (repeat :tag "Export" (file :tag "Export to"))))
459 (cons :tag "Prefix key documentation"
460 (string :tag "Access Key(s)")
461 (string :tag "Description ")))))
462
463(defcustom org-agenda-query-register ?o
464 "The register holding the current query string.
33306645 465The purpose of this is that if you construct a query string interactively,
20908596
CD
466you can then use it to define a custom command."
467 :group 'org-agenda-custom-commands
468 :type 'character)
469
470(defcustom org-stuck-projects
471 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
472 "How to identify stuck projects.
473This is a list of four items:
c8d0cf5c
CD
4741. A tags/todo/property matcher string that is used to identify a project.
475 See the manual for a description of tag and property searches.
20908596
CD
476 The entire tree below a headline matched by this is considered one project.
4772. A list of TODO keywords identifying non-stuck projects.
478 If the project subtree contains any headline with one of these todo
479 keywords, the project is considered to be not stuck. If you specify
480 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
4813. A list of tags identifying non-stuck projects.
482 If the project subtree contains any headline with one of these tags,
483 the project is considered to be not stuck. If you specify \"*\" as
c8d0cf5c
CD
484 a tag, any tag will mark the project unstuck. Note that this is about
485 the explicit presence of a tag somewhere in the subtree, inherited
486 tags to not count here. If inherited tags make a project not stuck,
487 use \"-TAG\" in the tags part of the matcher under (1.) above.
20908596
CD
4884. An arbitrary regular expression matching non-stuck projects.
489
c8d0cf5c
CD
490If the project turns out to be not stuck, search continues also in the
491subtree to see if any of the subtasks have project status.
492
493See also the variable `org-tags-match-list-sublevels' which applies
494to projects matched by this search as well.
495
20908596
CD
496After defining this variable, you may use \\[org-agenda-list-stuck-projects]
497or `C-c a #' to produce the list."
498 :group 'org-agenda-custom-commands
499 :type '(list
500 (string :tag "Tags/TODO match to identify a project")
501 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
502 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
c8d0cf5c 503 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
20908596 504
71d35b24
CD
505(defcustom org-agenda-filter-effort-default-operator "<"
506 "The default operator for effort estimate filtering.
93b62de8 507If you select an effort estimate limit without first pressing an operator,
71d35b24
CD
508this one will be used."
509 :group 'org-agenda-custom-commands
510 :type '(choice (const :tag "less or equal" "<")
511 (const :tag "greater or equal"">")
512 (const :tag "equal" "=")))
20908596
CD
513
514(defgroup org-agenda-skip nil
515 "Options concerning skipping parts of agenda files."
516 :tag "Org Agenda Skip"
517 :group 'org-agenda)
0bd48b37
CD
518(defgroup org-agenda-daily/weekly nil
519 "Options concerning the daily/weekly agenda."
520 :tag "Org Agenda Daily/Weekly"
521 :group 'org-agenda)
522(defgroup org-agenda-todo-list nil
523 "Options concerning the global todo list agenda view."
524 :tag "Org Agenda Todo List"
525 :group 'org-agenda)
526(defgroup org-agenda-match-view nil
527 "Options concerning the general tags/property/todo match agenda view."
528 :tag "Org Agenda Match View"
529 :group 'org-agenda)
8bfe682a
CD
530(defgroup org-agenda-search-view nil
531 "Options concerning the general tags/property/todo match agenda view."
532 :tag "Org Agenda Match View"
533 :group 'org-agenda)
20908596 534
2c3ad40d 535(defvar org-agenda-archives-mode nil
ed21c5c8 536 "Non-nil means the agenda will include archived items.
2c3ad40d
CD
537If this is the symbol `trees', trees in the selected agenda scope
538that are marked with the ARCHIVE tag will be included anyway. When this is
539t, also all archive files associated with the current selection of agenda
540files will be included.")
541
b349f79f 542(defcustom org-agenda-skip-comment-trees t
ed21c5c8 543 "Non-nil means skip trees that start with the COMMENT keyword.
33306645 544When nil, these trees are also scanned by agenda commands."
b349f79f
CD
545 :group 'org-agenda-skip
546 :type 'boolean)
547
20908596 548(defcustom org-agenda-todo-list-sublevels t
ed21c5c8 549 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
20908596
CD
550When nil, the sublevels of a TODO entry are not checked, resulting in
551potentially much shorter TODO lists."
552 :group 'org-agenda-skip
0bd48b37 553 :group 'org-agenda-todo-list
20908596
CD
554 :type 'boolean)
555
556(defcustom org-agenda-todo-ignore-with-date nil
ed21c5c8 557 "Non-nil means don't show entries with a date in the global todo list.
20908596
CD
558You can use this if you prefer to mark mere appointments with a TODO keyword,
559but don't want them to show up in the TODO list.
560When this is set, it also covers deadlines and scheduled items, the settings
561of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
c8d0cf5c
CD
562will be ignored.
563See also the variable `org-agenda-tags-todo-honor-ignore-options'."
20908596 564 :group 'org-agenda-skip
0bd48b37 565 :group 'org-agenda-todo-list
20908596
CD
566 :type 'boolean)
567
acedf35c
CD
568(defcustom org-agenda-todo-ignore-timestamp nil
569 "Non-nil means don't show entries with a timestamp.
570This applies when creating the global todo list.
571Valid values are:
572
573past Don't show entries for today or in the past.
574
575future Don't show entries with a timestamp in the future.
576 The idea behind this is that if it has a future
577 timestamp, you don't want to think about it until the
578 date.
579
580all Don't show any entries with a timestamp in the global todo list.
581 The idea behind this is that by setting a timestamp, you
582 have already \"taken care\" of this item.
583
584See also `org-agenda-todo-ignore-with-date'.
585See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
586to make his option also apply to the tags-todo list."
587 :group 'org-agenda-skip
588 :group 'org-agenda-todo-list
589 :type '(choice
590 (const :tag "Ignore future timestamp todos" future)
591 (const :tag "Ignore past or present timestamp todos" past)
592 (const :tag "Ignore all timestamp todos" all)
593 (const :tag "Show timestamp todos" nil)))
594
20908596 595(defcustom org-agenda-todo-ignore-scheduled nil
ed21c5c8
CD
596 "Non-nil means, ignore some scheduled TODO items when making TODO list.
597This applies when creating the global todo list.
598Valid values are:
599
600past Don't show entries scheduled today or in the past.
601
602future Don't show entries scheduled in the future.
603 The idea behind this is that by scheduling it, you don't want to
604 think about it until the scheduled date.
605
606all Don't show any scheduled entries in the global todo list.
607 The idea behind this is that by scheduling it, you have already
608 \"taken care\" of this item.
609
610t Same as `all', for backward compatibility.
611
c8d0cf5c 612See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
613See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
614to make his option also apply to the tags-todo list."
20908596 615 :group 'org-agenda-skip
0bd48b37 616 :group 'org-agenda-todo-list
ed21c5c8
CD
617 :type '(choice
618 (const :tag "Ignore future-scheduled todos" future)
619 (const :tag "Ignore past- or present-scheduled todos" past)
620 (const :tag "Ignore all scheduled todos" all)
621 (const :tag "Ignore all scheduled todos (compatibility)" t)
622 (const :tag "Show scheduled todos" nil)))
20908596
CD
623
624(defcustom org-agenda-todo-ignore-deadlines nil
ed21c5c8
CD
625 "Non-nil means ignore some deadlined TODO items when making TODO list.
626There are different motivations for using different values, please think
627carefully when configuring this variable.
628
86fbb8ca 629This applies when creating the global todo list.
ed21c5c8
CD
630Valid values are:
631
632near Don't show near deadline entries. A deadline is near when it is
633 closer than `org-deadline-warning-days' days. The idea behind this
634 is that such items will appear in the agenda anyway.
635
636far Don't show TODO entries where a deadline has been defined, but
637 the deadline is not near. This is useful if you don't want to
638 use the todo list to figure out what to do now.
639
640past Don't show entries with a deadline timestamp for today or in the past.
641
642future Don't show entries with a deadline timestamp in the future, not even
643 when they become `near' ones. Use it with caution.
644
645all Ignore all TODO entries that do have a deadline.
646
647t Same as `near', for backward compatibility.
648
c8d0cf5c 649See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
650See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
651to make his option also apply to the tags-todo list."
20908596 652 :group 'org-agenda-skip
0bd48b37 653 :group 'org-agenda-todo-list
ed21c5c8
CD
654 :type '(choice
655 (const :tag "Ignore near deadlines" near)
656 (const :tag "Ignore near deadlines (compatibility)" t)
657 (const :tag "Ignore far deadlines" far)
658 (const :tag "Ignore all TODOs with a deadlines" all)
659 (const :tag "Show all TODOs, even if they have a deadline" nil)))
0bd48b37
CD
660
661(defcustom org-agenda-tags-todo-honor-ignore-options nil
ed21c5c8 662 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
0bd48b37
CD
663The variables
664 `org-agenda-todo-ignore-with-date',
acedf35c
CD
665 `org-agenda-todo-ignore-timestamp',
666 `org-agenda-todo-ignore-scheduled',
0bd48b37
CD
667 `org-agenda-todo-ignore-deadlines'
668make the global TODO list skip entries that have time stamps of certain
669kinds. If this option is set, the same options will also apply for the
670tags-todo search, which is the general tags/property matcher
671restricted to unfinished TODO entries only."
672 :group 'org-agenda-skip
673 :group 'org-agenda-todo-list
674 :group 'org-agenda-match-view
20908596
CD
675 :type 'boolean)
676
677(defcustom org-agenda-skip-scheduled-if-done nil
678 "Non-nil means don't show scheduled items in agenda when they are done.
679This is relevant for the daily/weekly agenda, not for the TODO list. And
680it applies only to the actual date of the scheduling. Warnings about
681an item with a past scheduling dates are always turned off when the item
682is DONE."
683 :group 'org-agenda-skip
0bd48b37 684 :group 'org-agenda-daily/weekly
20908596
CD
685 :type 'boolean)
686
54a0dee5
CD
687(defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
688 "Non-nil means skip scheduling line if same entry shows because of deadline.
689In the agenda of today, an entry can show up multiple times because
690it is both scheduled and has a nearby deadline, and maybe a plain time
691stamp as well.
692When this variable is t, then only the deadline is shown and the fact that
693the entry is scheduled today or was scheduled previously is not shown.
694When this variable is nil, the entry will be shown several times. When
695the variable is the symbol `not-today', then skip scheduled previously,
696but not scheduled today."
697 :group 'org-agenda-skip
698 :group 'org-agenda-daily/weekly
699 :type '(choice
700 (const :tag "Never" nil)
701 (const :tag "Always" t)
702 (const :tag "Not when scheduled today" not-today)))
703
20908596 704(defcustom org-agenda-skip-deadline-if-done nil
33306645 705 "Non-nil means don't show deadlines when the corresponding item is done.
20908596
CD
706When nil, the deadline is still shown and should give you a happy feeling.
707This is relevant for the daily/weekly agenda. And it applied only to the
33306645 708actually date of the deadline. Warnings about approaching and past-due
20908596
CD
709deadlines are always turned off when the item is DONE."
710 :group 'org-agenda-skip
0bd48b37 711 :group 'org-agenda-daily/weekly
20908596
CD
712 :type 'boolean)
713
ed21c5c8
CD
714(defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
715 "Non-nil means skip deadline prewarning when entry is also scheduled.
716This will apply on all days where a prewarning for the deadline would
717be shown, but not at the day when the entry is actually due. On that day,
718the deadline will be shown anyway.
719This variable may be set to nil, t, or a number which will then give
720the number of days before the actual deadline when the prewarnings
721should resume.
722This can be used in a workflow where the first showing of the deadline will
723trigger you to schedule it, and then you don't want to be reminded of it
724because you will take care of it on the day when scheduled."
725 :group 'org-agenda-skip
726 :group 'org-agenda-daily/weekly
727 :type '(choice
728 (const :tag "Alwas show prewarning" nil)
729 (const :tag "Remove prewarning if entry is scheduled" t)
730 (integer :tag "Restart prewarning N days before deadline")))
731
c8d0cf5c
CD
732(defcustom org-agenda-skip-additional-timestamps-same-entry t
733 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
734When non-nil, after the search for timestamps has matched once in an
735entry, the rest of the entry will not be searched."
736 :group 'org-agenda-skip
737 :type 'boolean)
738
20908596
CD
739(defcustom org-agenda-skip-timestamp-if-done nil
740 "Non-nil means don't select item by timestamp or -range if it is DONE."
741 :group 'org-agenda-skip
0bd48b37 742 :group 'org-agenda-daily/weekly
20908596
CD
743 :type 'boolean)
744
d6685abc 745(defcustom org-agenda-dim-blocked-tasks t
ed21c5c8 746 "Non-nil means dim blocked tasks in the agenda display.
c8d0cf5c
CD
747This causes some overhead during agenda construction, but if you
748have turned on `org-enforce-todo-dependencies',
749`org-enforce-todo-checkbox-dependencies', or any other blocking
750mechanism, this will create useful feedback in the agenda.
751
8bfe682a 752Instead of t, this variable can also have the value `invisible'.
c8d0cf5c
CD
753Then blocked tasks will be invisible and only become visible when
754they become unblocked. An exemption to this behavior is when a task is
755blocked because of unchecked checkboxes below it. Since checkboxes do
756not show up in the agenda views, making this task invisible you remove any
757trace from agenda views that there is something to do. Therefore, a task
758that is blocked because of checkboxes will never be made invisible, it
759will only be dimmed."
d6685abc
CD
760 :group 'org-agenda-daily/weekly
761 :group 'org-agenda-todo-list
762 :type '(choice
763 (const :tag "Do not dim" nil)
764 (const :tag "Dim to a grey face" t)
8bfe682a 765 (const :tag "Make invisible" invisible)))
d6685abc 766
20908596 767(defcustom org-timeline-show-empty-dates 3
ed21c5c8 768 "Non-nil means `org-timeline' also shows dates without an entry.
20908596
CD
769When nil, only the days which actually have entries are shown.
770When t, all days between the first and the last date are shown.
771When an integer, show also empty dates, but if there is a gap of more than
772N days, just insert a special line indicating the size of the gap."
773 :group 'org-agenda-skip
774 :type '(choice
775 (const :tag "None" nil)
776 (const :tag "All" t)
c8d0cf5c 777 (integer :tag "at most")))
20908596 778
20908596
CD
779(defgroup org-agenda-startup nil
780 "Options concerning initial settings in the Agenda in Org Mode."
781 :tag "Org Agenda Startup"
782 :group 'org-agenda)
783
afe98dfa
CD
784(defcustom org-agenda-menu-show-matcher t
785 "Non-nil menas show the match string in the agenda dispatcher menu.
786When nil, the matcher string is not shown, but is put into the help-echo
787property so than moving the mouse over the command shows it.
788Setting it to nil is good if matcher strings are very long and/or if
789you wnat to use two-column display (see `org-agenda-menu-two-column')."
790 :group 'org-agenda
791 :type 'boolean)
792
793(defcustom org-agenda-menu-two-column nil
794 "Non-nil means, use two columns to show custom commands in the dispatcher.
795If you use this, you probably want to set `org-agenda-menu-show-matcher'
796to nil."
797 :group 'org-agenda
798 :type 'boolean)
799
20908596
CD
800(defcustom org-finalize-agenda-hook nil
801 "Hook run just before displaying an agenda buffer."
802 :group 'org-agenda-startup
803 :type 'hook)
804
805(defcustom org-agenda-mouse-1-follows-link nil
ed21c5c8 806 "Non-nil means mouse-1 on a link will follow the link in the agenda.
20908596
CD
807A longer mouse click will still set point. Does not work on XEmacs.
808Needs to be set before org.el is loaded."
809 :group 'org-agenda-startup
810 :type 'boolean)
811
812(defcustom org-agenda-start-with-follow-mode nil
86fbb8ca 813 "The initial value of follow mode in a newly created agenda window."
20908596
CD
814 :group 'org-agenda-startup
815 :type 'boolean)
816
1bcdebed 817(defcustom org-agenda-show-outline-path t
ed21c5c8 818 "Non-nil means show outline path in echo area after line motion."
1bcdebed
CD
819 :group 'org-agenda-startup
820 :type 'boolean)
821
54a0dee5
CD
822(defcustom org-agenda-start-with-entry-text-mode nil
823 "The initial value of entry-text-mode in a newly created agenda window."
824 :group 'org-agenda-startup
825 :type 'boolean)
826
827(defcustom org-agenda-entry-text-maxlines 5
8bfe682a 828 "Number of text lines to be added when `E' is pressed in the agenda.
54a0dee5
CD
829
830Note that this variable only used during agenda display. Add add entry text
831when exporting the agenda, configure the variable
832`org-agenda-add-entry-ext-maxlines'."
833 :group 'org-agenda
834 :type 'integer)
835
8d642074
CD
836(defcustom org-agenda-entry-text-exclude-regexps nil
837 "List of regular expressions to clean up entry text.
838The complete matches of all regular expressions in this list will be
839removed from entry text before it is shown in the agenda."
840 :group 'org-agenda
841 :type '(repeat (regexp)))
842
843(defvar org-agenda-entry-text-cleanup-hook nil
844 "Hook that is run after basic cleanup of entry text to be shown in agenda.
845This cleanup is done in a temporary buffer, so the function may inspect and
846change the entire buffer.
847Some default stuff like drawers and scheduling/deadline dates will already
848have been removed when this is called, as will any matches for regular
849expressions listed in `org-agenda-entry-text-exclude-regexps'.")
850
20908596 851(defvar org-agenda-include-inactive-timestamps nil
ed21c5c8 852 "Non-nil means include inactive time stamps in agenda and timeline.")
20908596
CD
853
854(defgroup org-agenda-windows nil
855 "Options concerning the windows used by the Agenda in Org Mode."
856 :tag "Org Agenda Windows"
857 :group 'org-agenda)
858
859(defcustom org-agenda-window-setup 'reorganize-frame
860 "How the agenda buffer should be displayed.
861Possible values for this option are:
862
863current-window Show agenda in the current window, keeping all other windows.
20908596
CD
864other-window Use `switch-to-buffer-other-window' to display agenda.
865reorganize-frame Show only two windows on the current frame, the current
866 window and the agenda.
8d642074
CD
867other-frame Use `switch-to-buffer-other-frame' to display agenda.
868 Also, when exiting the agenda, kill that frame.
20908596
CD
869See also the variable `org-agenda-restore-windows-after-quit'."
870 :group 'org-agenda-windows
871 :type '(choice
872 (const current-window)
873 (const other-frame)
874 (const other-window)
875 (const reorganize-frame)))
876
877(defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
878 "The min and max height of the agenda window as a fraction of frame height.
879The value of the variable is a cons cell with two numbers between 0 and 1.
880It only matters if `org-agenda-window-setup' is `reorganize-frame'."
881 :group 'org-agenda-windows
882 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
883
884(defcustom org-agenda-restore-windows-after-quit nil
ed21c5c8 885 "Non-nil means restore window configuration open exiting agenda.
20908596
CD
886Before the window configuration is changed for displaying the agenda,
887the current status is recorded. When the agenda is exited with
888`q' or `x' and this option is set, the old state is restored. If
889`org-agenda-window-setup' is `other-frame', the value of this
baf0cb84 890option will be ignored."
20908596
CD
891 :group 'org-agenda-windows
892 :type 'boolean)
893
acedf35c
CD
894(defcustom org-agenda-ndays nil
895 "Number of days to include in overview display.
c8d0cf5c 896Should be 1 or 7.
acedf35c
CD
897Obsolete, see `org-agenda-span'."
898 :group 'org-agenda-daily/weekly
899 :type 'integer)
900
901(make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
902
903(defcustom org-agenda-span 'week
904 "Number of days to include in overview display.
905Can be day, week, month, year, or any number of days.
c8d0cf5c 906Custom commands can set this variable in the options section."
20908596 907 :group 'org-agenda-daily/weekly
acedf35c
CD
908 :type '(choice (const :tag "Day" day)
909 (const :tag "Week" week)
910 (const :tag "Month" month)
911 (const :tag "Year" year)
912 (integer :tag "Custom")))
20908596
CD
913
914(defcustom org-agenda-start-on-weekday 1
ed21c5c8 915 "Non-nil means start the overview always on the specified weekday.
20908596 9160 denotes Sunday, 1 denotes Monday etc.
c8d0cf5c
CD
917When nil, always start on the current day.
918Custom commands can set this variable in the options section."
20908596
CD
919 :group 'org-agenda-daily/weekly
920 :type '(choice (const :tag "Today" nil)
c8d0cf5c 921 (integer :tag "Weekday No.")))
20908596
CD
922
923(defcustom org-agenda-show-all-dates t
ed21c5c8 924 "Non-nil means `org-agenda' shows every day in the selected range.
20908596
CD
925When nil, only the days which actually have entries are shown."
926 :group 'org-agenda-daily/weekly
927 :type 'boolean)
928
929(defcustom org-agenda-format-date 'org-agenda-format-date-aligned
930 "Format string for displaying dates in the agenda.
931Used by the daily/weekly agenda and by the timeline. This should be
932a format string understood by `format-time-string', or a function returning
933the formatted date as a string. The function must take a single argument,
934a calendar-style date list like (month day year)."
935 :group 'org-agenda-daily/weekly
936 :type '(choice
937 (string :tag "Format string")
938 (function :tag "Function")))
939
940(defun org-agenda-format-date-aligned (date)
941 "Format a date string for display in the daily/weekly agenda, or timeline.
942This function makes sure that dates are aligned for easy reading."
943 (require 'cal-iso)
944 (let* ((dayname (calendar-day-name date))
945 (day (cadr date))
946 (day-of-week (calendar-day-of-week date))
947 (month (car date))
948 (monthname (calendar-month-name month))
949 (year (nth 2 date))
950 (iso-week (org-days-to-iso-week
951 (calendar-absolute-from-gregorian date)))
952 (weekyear (cond ((and (= month 1) (>= iso-week 52))
953 (1- year))
954 ((and (= month 12) (<= iso-week 1))
955 (1+ year))
956 (t year)))
957 (weekstring (if (= day-of-week 1)
958 (format " W%02d" iso-week)
959 "")))
960 (format "%-10s %2d %s %4d%s"
961 dayname day monthname year weekstring)))
962
ed21c5c8
CD
963(defcustom org-agenda-time-leading-zero nil
964 "Non-nil means use leading zero for military times in agenda.
965For example, 9:30am would become 09:30 rather than 9:30."
966 :group 'org-agenda-daily/weekly
967 :type 'boolean)
968
acedf35c
CD
969(defcustom org-agenda-timegrid-use-ampm nil
970 "When set, show AM/PM style timestamps on the timegrid."
971 :group 'org-agenda
972 :type 'boolean)
973
974(defun org-agenda-time-of-day-to-ampm (time)
975 "Convert TIME of a string like '13:45' to an AM/PM style time string."
976 (let* ((hour-number (string-to-number (substring time 0 -3)))
977 (minute (substring time -2))
978 (ampm "am"))
979 (cond
980 ((equal hour-number 12)
981 (setq ampm "pm"))
982 ((> hour-number 12)
983 (setq ampm "pm")
984 (setq hour-number (- hour-number 12))))
985 (concat
986 (if org-agenda-time-leading-zero
987 (format "%02d" hour-number)
988 (format "%02s" (number-to-string hour-number)))
989 ":" minute ampm)))
990
991(defun org-agenda-time-of-day-to-ampm-maybe (time)
992 "Conditionally convert TIME to AM/PM format
993based on `org-agenda-timegrid-use-ampm'"
994 (if org-agenda-timegrid-use-ampm
995 (org-agenda-time-of-day-to-ampm time)
996 time))
997
20908596
CD
998(defcustom org-agenda-weekend-days '(6 0)
999 "Which days are weekend?
1000These days get the special face `org-agenda-date-weekend' in the agenda
1001and timeline buffers."
1002 :group 'org-agenda-daily/weekly
1003 :type '(set :greedy t
1004 (const :tag "Monday" 1)
1005 (const :tag "Tuesday" 2)
1006 (const :tag "Wednesday" 3)
1007 (const :tag "Thursday" 4)
1008 (const :tag "Friday" 5)
1009 (const :tag "Saturday" 6)
1010 (const :tag "Sunday" 0)))
1011
1012(defcustom org-agenda-include-diary nil
c8d0cf5c
CD
1013 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1014Custom commands can set this variable in the options section."
20908596
CD
1015 :group 'org-agenda-daily/weekly
1016 :type 'boolean)
1017
ed21c5c8
CD
1018(defcustom org-agenda-include-deadlines t
1019 "If non-nil, include entries within their deadline warning period.
1020Custom commands can set this variable in the options section."
1021 :group 'org-agenda-daily/weekly
1022 :type 'boolean)
1023
20908596
CD
1024(defcustom org-agenda-include-all-todo nil
1025 "Set means weekly/daily agenda will always contain all TODO entries.
1026The TODO entries will be listed at the top of the agenda, before
0bd48b37
CD
1027the entries for specific days.
1028This option is deprecated, it is better to define a block agenda instead."
20908596
CD
1029 :group 'org-agenda-daily/weekly
1030 :type 'boolean)
1031
1032(defcustom org-agenda-repeating-timestamp-show-all t
ed21c5c8 1033 "Non-nil means show all occurrences of a repeating stamp in the agenda.
33306645 1034When nil, only one occurrence is shown, either today or the
20908596
CD
1035nearest into the future."
1036 :group 'org-agenda-daily/weekly
1037 :type 'boolean)
1038
1039(defcustom org-scheduled-past-days 10000
1040 "No. of days to continue listing scheduled items that are not marked DONE.
1041When an item is scheduled on a date, it shows up in the agenda on this
1042day and will be listed until it is marked done for the number of days
1043given here."
1044 :group 'org-agenda-daily/weekly
c8d0cf5c 1045 :type 'integer)
20908596 1046
93b62de8
CD
1047(defcustom org-agenda-log-mode-items '(closed clock)
1048 "List of items that should be shown in agenda log mode.
1049This list may contain the following symbols:
1050
1051 closed Show entries that have been closed on that day.
1052 clock Show entries that have received clocked time on that day.
c8d0cf5c
CD
1053 state Show all logged state changes.
1054Note that instead of changing this variable, you can also press `C-u l' in
1055the agenda to display all available LOG items temporarily."
93b62de8
CD
1056 :group 'org-agenda-daily/weekly
1057 :type '(set :greedy t (const closed) (const clock) (const state)))
1058
c8d0cf5c 1059(defcustom org-agenda-log-mode-add-notes t
ed21c5c8 1060 "Non-nil means add first line of notes to log entries in agenda views.
c8d0cf5c
CD
1061If a log item like a state change or a clock entry is associated with
1062notes, the first line of these notes will be added to the entry in the
1063agenda display."
1064 :group 'org-agenda-daily/weekly
1065 :type 'boolean)
1066
1067(defcustom org-agenda-start-with-log-mode nil
1068 "The initial value of log-mode in a newly created agenda window."
1069 :group 'org-agenda-startup
1070 :group 'org-agenda-daily/weekly
1071 :type 'boolean)
1072
20908596
CD
1073(defcustom org-agenda-start-with-clockreport-mode nil
1074 "The initial value of clockreport-mode in a newly created agenda window."
1075 :group 'org-agenda-startup
1076 :group 'org-agenda-daily/weekly
1077 :type 'boolean)
1078
1079(defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1080 "Property list with parameters for the clocktable in clockreport mode.
1081This is the display mode that shows a clock table in the daily/weekly
1082agenda, the properties for this dynamic block can be set here.
1083The usual clocktable parameters are allowed here, but you cannot set
1084the properties :name, :tstart, :tend, :block, and :scope - these will
1085be overwritten to make sure the content accurately reflects the
1086current display in the agenda."
1087 :group 'org-agenda-daily/weekly
1088 :type 'plist)
1089
ed21c5c8
CD
1090(defcustom org-agenda-search-view-always-boolean nil
1091 "Non-nil means the search string is interpreted as individual parts.
1092
1093The search string for search view can either be interpreted as a phrase,
1094or as a list of snippets that define a boolean search for a number of
1095strings.
1096
1097When this is non-nil, the string will be split on whitespace, and each
1098snippet will be searched individually, and all must match in order to
1099select an entry. A snippet is then a single string of non-white
1100characters, or a string in double quotes, or a regexp in {} braces.
86fbb8ca 1101If a snippet is preceded by \"-\", the snippet must *not* match.
ed21c5c8
CD
1102\"+\" is syntactic sugar for positive selection. Each snippet may
1103be found as a full word or a partial word, but see the variable
1104`org-agenda-search-view-force-full-words'.
1105
1106When this is nil, search will look for the entire search phrase as one,
1107with each space character matching any amount of whitespace, including
1108line breaks.
1109
1110Even when this is nil, you can still switch to Boolean search dynamically
86fbb8ca 1111by preceding the first snippet with \"+\" or \"-\". If the first snippet
ed21c5c8
CD
1112is a regexp marked with braces like \"{abc}\", this will also switch to
1113boolean search."
1114 :group 'org-agenda-search-view
1115 :type 'boolean)
1116
1117(if (fboundp 'defvaralias)
1118 (defvaralias 'org-agenda-search-view-search-words-only
1119 'org-agenda-search-view-always-boolean))
1120
1121(defcustom org-agenda-search-view-force-full-words nil
86fbb8ca 1122 "Non-nil means, search words must be matches as complete words.
ed21c5c8 1123When nil, they may also match part of a word."
8bfe682a
CD
1124 :group 'org-agenda-search-view
1125 :type 'boolean)
20908596
CD
1126
1127(defgroup org-agenda-time-grid nil
1128 "Options concerning the time grid in the Org-mode Agenda."
1129 :tag "Org Agenda Time Grid"
1130 :group 'org-agenda)
1131
c8d0cf5c 1132(defcustom org-agenda-search-headline-for-time t
ed21c5c8 1133 "Non-nil means search headline for a time-of-day.
c8d0cf5c
CD
1134If the headline contains a time-of-day in one format or another, it will
1135be used to sort the entry into the time sequence of items for a day.
1136Some people have time stamps in the headline that refer to the creation
1137time or so, and then this produces an unwanted side effect. If this is
1138the case for your, use this variable to turn off searching the headline
1139for a time."
1140 :group 'org-agenda-time-grid
1141 :type 'boolean)
1142
20908596 1143(defcustom org-agenda-use-time-grid t
ed21c5c8 1144 "Non-nil means show a time grid in the agenda schedule.
20908596
CD
1145A time grid is a set of lines for specific times (like every two hours between
11468:00 and 20:00). The items scheduled for a day at specific times are
1147sorted in between these lines.
1148For details about when the grid will be shown, and what it will look like, see
1149the variable `org-agenda-time-grid'."
1150 :group 'org-agenda-time-grid
1151 :type 'boolean)
1152
1153(defcustom org-agenda-time-grid
1154 '((daily today require-timed)
1155 "----------------"
1156 (800 1000 1200 1400 1600 1800 2000))
1157
1158 "The settings for time grid for agenda display.
1159This is a list of three items. The first item is again a list. It contains
1160symbols specifying conditions when the grid should be displayed:
1161
1162 daily if the agenda shows a single day
1163 weekly if the agenda shows an entire week
1164 today show grid on current date, independent of daily/weekly display
1165 require-timed show grid only if at least one item has a time specification
1166
b349f79f 1167The second item is a string which will be placed behind the grid time.
20908596
CD
1168
1169The third item is a list of integers, indicating the times that should have
1170a grid line."
1171 :group 'org-agenda-time-grid
1172 :type
1173 '(list
1174 (set :greedy t :tag "Grid Display Options"
1175 (const :tag "Show grid in single day agenda display" daily)
1176 (const :tag "Show grid in weekly agenda display" weekly)
1177 (const :tag "Always show grid for today" today)
1178 (const :tag "Show grid only if any timed entries are present"
1179 require-timed)
1180 (const :tag "Skip grid times already present in an entry"
1181 remove-match))
1182 (string :tag "Grid String")
1183 (repeat :tag "Grid Times" (integer :tag "Time"))))
1184
1185(defgroup org-agenda-sorting nil
1186 "Options concerning sorting in the Org-mode Agenda."
1187 :tag "Org Agenda Sorting"
1188 :group 'org-agenda)
1189
1190(defcustom org-agenda-sorting-strategy
8bfe682a
CD
1191 '((agenda habit-down time-up priority-down category-keep)
1192 (todo priority-down category-keep)
1193 (tags priority-down category-keep)
20908596
CD
1194 (search category-keep))
1195 "Sorting structure for the agenda items of a single day.
1196This is a list of symbols which will be used in sequence to determine
1197if an entry should be listed before another entry. The following
1198symbols are recognized:
1199
c8d0cf5c
CD
1200time-up Put entries with time-of-day indications first, early first
1201time-down Put entries with time-of-day indications first, late first
1202category-keep Keep the default order of categories, corresponding to the
1203 sequence in `org-agenda-files'.
1204category-up Sort alphabetically by category, A-Z.
1205category-down Sort alphabetically by category, Z-A.
1206tag-up Sort alphabetically by last tag, A-Z.
1207tag-down Sort alphabetically by last tag, Z-A.
1208priority-up Sort numerically by priority, high priority last.
1209priority-down Sort numerically by priority, high priority first.
1210todo-state-up Sort by todo state, tasks that are done last.
1211todo-state-down Sort by todo state, tasks that are done first.
1212effort-up Sort numerically by estimated effort, high effort last.
1213effort-down Sort numerically by estimated effort, high effort first.
1214user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1215user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
8bfe682a
CD
1216habit-up Put entries that are habits first
1217habit-down Put entries that are habits last
86fbb8ca
CD
1218alpha-up Sort headlines alphabetically
1219alpha-down Sort headlines alphabetically, reversed
20908596
CD
1220
1221The different possibilities will be tried in sequence, and testing stops
1222if one comparison returns a \"not-equal\". For example, the default
1223 '(time-up category-keep priority-down)
1224means: Pull out all entries having a specified time of day and sort them,
1225in order to make a time schedule for the current day the first thing in the
1226agenda listing for the day. Of the entries without a time indication, keep
1227the grouped in categories, don't sort the categories, but keep them in
1228the sequence given in `org-agenda-files'. Within each category sort by
1229priority.
1230
1231Leaving out `category-keep' would mean that items will be sorted across
1232categories by priority.
1233
1234Instead of a single list, this can also be a set of list for specific
1235contents, with a context symbol in the car of the list, any of
8bfe682a 1236`agenda', `todo', `tags', `search' for the corresponding agenda views.
c8d0cf5c
CD
1237
1238Custom commands can bind this variable in the options section."
20908596
CD
1239 :group 'org-agenda-sorting
1240 :type `(choice
1241 (repeat :tag "General" ,org-sorting-choice)
1242 (list :tag "Individually"
1243 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1244 (repeat ,org-sorting-choice))
1245 (cons (const :tag "Strategy for TODO lists" todo)
1246 (repeat ,org-sorting-choice))
1247 (cons (const :tag "Strategy for Tags matches" tags)
8bfe682a
CD
1248 (repeat ,org-sorting-choice))
1249 (cons (const :tag "Strategy for search matches" search)
20908596
CD
1250 (repeat ,org-sorting-choice)))))
1251
c8d0cf5c
CD
1252(defcustom org-agenda-cmp-user-defined nil
1253 "A function to define the comparison `user-defined'.
1254This function must receive two arguments, agenda entry a and b.
1255If a>b, return +1. If a<b, return -1. If they are equal as seen by
1256the user comparison, return nil.
1257When this is defined, you can make `user-defined-up' and `user-defined-down'
1258part of an agenda sorting strategy."
1259 :group 'org-agenda-sorting
1260 :type 'symbol)
1261
20908596 1262(defcustom org-sort-agenda-notime-is-late t
ed21c5c8 1263 "Non-nil means items without time are considered late.
20908596
CD
1264This is only relevant for sorting. When t, items which have no explicit
1265time like 15:30 will be considered as 99:01, i.e. later than any items which
1266do have a time. When nil, the default time is before 0:00. You can use this
1267option to decide if the schedule for today should come before or after timeless
1268agenda entries."
1269 :group 'org-agenda-sorting
1270 :type 'boolean)
1271
1272(defcustom org-sort-agenda-noeffort-is-high t
ed21c5c8 1273 "Non-nil means items without effort estimate are sorted as high effort.
c8d0cf5c
CD
1274This also applies when filtering an agenda view with respect to the
1275< or > effort operator. Then, tasks with no effort defined will be treated
1276as tasks with high effort.
20908596
CD
1277When nil, such items are sorted as 0 minutes effort."
1278 :group 'org-agenda-sorting
1279 :type 'boolean)
1280
1281(defgroup org-agenda-line-format nil
1282 "Options concerning the entry prefix in the Org-mode agenda display."
1283 :tag "Org Agenda Line Format"
1284 :group 'org-agenda)
1285
1286(defcustom org-agenda-prefix-format
acedf35c 1287 '((agenda . " %i %-12:c%?-12t% s")
20908596 1288 (timeline . " % s")
acedf35c
CD
1289 (todo . " %i %-12:c")
1290 (tags . " %i %-12:c")
1291 (search . " %i %-12:c"))
20908596
CD
1292 "Format specifications for the prefix of items in the agenda views.
1293An alist with four entries, for the different agenda types. The keys to the
1294sublists are `agenda', `timeline', `todo', and `tags'. The values
1295are format strings.
1296This format works similar to a printf format, with the following meaning:
1297
1298 %c the category of the item, \"Diary\" for entries from the diary, or
1299 as given by the CATEGORY keyword or derived from the file name.
acedf35c
CD
1300 %i the icon category of the item, as give in
1301 `org-agenda-category-icon-alist'.
20908596
CD
1302 %T the *last* tag of the item. Last because inherited tags come
1303 first in the list.
1304 %t the time-of-day specification if one applies to the entry, in the
1305 format HH:MM
1306 %s Scheduling/Deadline information, a short string
1307
1308All specifiers work basically like the standard `%s' of printf, but may
1309contain two additional characters: A question mark just after the `%' and
1310a whitespace/punctuation character just before the final letter.
1311
1312If the first character after `%' is a question mark, the entire field
1313will only be included if the corresponding value applies to the
1314current entry. This is useful for fields which should have fixed
1315width when present, but zero width when absent. For example,
1316\"%?-12t\" will result in a 12 character time field if a time of the
1317day is specified, but will completely disappear in entries which do
1318not contain a time.
1319
1320If there is punctuation or whitespace character just before the final
1321format letter, this character will be appended to the field value if
1322the value is not empty. For example, the format \"%-12:c\" leads to
1323\"Diary: \" if the category is \"Diary\". If the category were be
8bfe682a 1324empty, no additional colon would be inserted.
20908596
CD
1325
1326The default value of this option is \" %-12:c%?-12t% s\", meaning:
1327- Indent the line with two space characters
1328- Give the category in a 12 chars wide field, padded with whitespace on
1329 the right (because of `-'). Append a colon if there is a category
1330 (because of `:').
1331- If there is a time-of-day, put it into a 12 chars wide field. If no
1332 time, don't put in an empty field, just skip it (because of '?').
1333- Finally, put the scheduling information and append a whitespace.
1334
1335As another example, if you don't want the time-of-day of entries in
1336the prefix, you could use:
1337
1338 (setq org-agenda-prefix-format \" %-11:c% s\")
1339
1340See also the variables `org-agenda-remove-times-when-in-prefix' and
c8d0cf5c
CD
1341`org-agenda-remove-tags'.
1342
1343Custom commands can set this variable in the options section."
20908596
CD
1344 :type '(choice
1345 (string :tag "General format")
1346 (list :greedy t :tag "View dependent"
1347 (cons (const agenda) (string :tag "Format"))
1348 (cons (const timeline) (string :tag "Format"))
1349 (cons (const todo) (string :tag "Format"))
1350 (cons (const tags) (string :tag "Format"))
1351 (cons (const search) (string :tag "Format"))))
1352 :group 'org-agenda-line-format)
1353
1354(defvar org-prefix-format-compiled nil
1355 "The compiled version of the most recently used prefix format.
1356See the variable `org-agenda-prefix-format'.")
1357
1358(defcustom org-agenda-todo-keyword-format "%-1s"
1359 "Format for the TODO keyword in agenda lines.
1360Set this to something like \"%-12s\" if you want all TODO keywords
1361to occupy a fixed space in the agenda display."
1362 :group 'org-agenda-line-format
1363 :type 'string)
1364
ce4fdcb9
CD
1365(defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1366 "Text preceding timerange entries in the agenda view.
1367This is a list with two strings. The first applies when the range
1368is entirely on one day. The second applies if the range spans several days.
1369The strings may have two \"%d\" format specifiers which will be filled
1370with the sequence number of the days, and the total number of days in the
1371range, respectively."
1372 :group 'org-agenda-line-format
1373 :type '(list
1374 (string :tag "Deadline today ")
1375 (choice :tag "Deadline relative"
1376 (string :tag "Format string")
1377 (function))))
1378
20908596 1379(defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
86fbb8ca 1380 "Text preceding scheduled items in the agenda view.
20908596
CD
1381This is a list with two strings. The first applies when the item is
1382scheduled on the current day. The second applies when it has been scheduled
b349f79f
CD
1383previously, it may contain a %d indicating that this is the nth time that
1384this item is scheduled, due to automatic rescheduling of unfinished items
1385for the following day. So this number is one larger than the number of days
1386that passed since this item was scheduled first."
20908596
CD
1387 :group 'org-agenda-line-format
1388 :type '(list
1389 (string :tag "Scheduled today ")
1390 (string :tag "Scheduled previously")))
1391
ed21c5c8 1392(defcustom org-agenda-inactive-leader "["
86fbb8ca 1393 "Text preceding item pulled into the agenda by inactive time stamps.
ed21c5c8
CD
1394These entries are added to the agenda when pressing \"[\"."
1395 :group 'org-agenda-line-format
1396 :type '(list
1397 (string :tag "Scheduled today ")
1398 (string :tag "Scheduled previously")))
1399
20908596 1400(defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
86fbb8ca 1401 "Text preceding deadline items in the agenda view.
20908596
CD
1402This is a list with two strings. The first applies when the item has its
1403deadline on the current day. The second applies when it is in the past or
1404in the future, it may contain %d to capture how many days away the deadline
1405is (was)."
1406 :group 'org-agenda-line-format
1407 :type '(list
1408 (string :tag "Deadline today ")
1409 (choice :tag "Deadline relative"
1410 (string :tag "Format string")
1411 (function))))
1412
1413(defcustom org-agenda-remove-times-when-in-prefix t
ed21c5c8 1414 "Non-nil means remove duplicate time specifications in agenda items.
20908596
CD
1415When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1416time-of-day specification in a headline or diary entry is extracted and
1417placed into the prefix. If this option is non-nil, the original specification
1418\(a timestamp or -range, or just a plain time(range) specification like
141911:30-4pm) will be removed for agenda display. This makes the agenda less
1420cluttered.
1421The option can be t or nil. It may also be the symbol `beg', indicating
86fbb8ca 1422that the time should only be removed when it is located at the beginning of
20908596
CD
1423the headline/diary entry."
1424 :group 'org-agenda-line-format
1425 :type '(choice
1426 (const :tag "Always" t)
1427 (const :tag "Never" nil)
1428 (const :tag "When at beginning of entry" beg)))
1429
86fbb8ca
CD
1430(defcustom org-agenda-remove-timeranges-from-blocks nil
1431 "Non-nil means remove time ranges specifications in agenda
1432items that span on several days."
1433 :group 'org-agenda-line-format
1434 :type 'boolean)
20908596
CD
1435
1436(defcustom org-agenda-default-appointment-duration nil
1437 "Default duration for appointments that only have a starting time.
1438When nil, no duration is specified in such cases.
1439When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1440 :group 'org-agenda-line-format
1441 :type '(choice
1442 (integer :tag "Minutes")
1443 (const :tag "No default duration")))
1444
ff4be292 1445(defcustom org-agenda-show-inherited-tags t
ed21c5c8 1446 "Non-nil means show inherited tags in each agenda line."
ff4be292
CD
1447 :group 'org-agenda-line-format
1448 :type 'boolean)
20908596 1449
5dec9555
CD
1450(defcustom org-agenda-hide-tags-regexp nil
1451 "Regular expression used to filter away specific tags in agenda views.
1452This means that these tags will be present, but not be shown in the agenda
86fbb8ca 1453line. Secondary filtering will still work on the hidden tags.
afe98dfa 1454Nil means don't hide any tags."
5dec9555
CD
1455 :group 'org-agenda-line-format
1456 :type '(choice
1457 (const :tag "Hide none" nil)
1458 (string :tag "Regexp ")))
1459
20908596 1460(defcustom org-agenda-remove-tags nil
ed21c5c8 1461 "Non-nil means remove the tags from the headline copy in the agenda.
20908596
CD
1462When this is the symbol `prefix', only remove tags when
1463`org-agenda-prefix-format' contains a `%T' specifier."
1464 :group 'org-agenda-line-format
1465 :type '(choice
1466 (const :tag "Always" t)
1467 (const :tag "Never" nil)
1468 (const :tag "When prefix format contains %T" prefix)))
1469
1470(if (fboundp 'defvaralias)
1471 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1472 'org-agenda-remove-tags))
1473
5ace2fe5 1474(defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
20908596
CD
1475 "Shift tags in agenda items to this column.
1476If this number is positive, it specifies the column. If it is negative,
1477it means that the tags should be flushright to that column. For example,
1478-80 works well for a normal 80 character screen."
1479 :group 'org-agenda-line-format
1480 :type 'integer)
1481
1482(if (fboundp 'defvaralias)
1483 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1484
c8d0cf5c 1485(defcustom org-agenda-fontify-priorities 'cookies
ed21c5c8 1486 "Non-nil means highlight low and high priorities in agenda.
20908596 1487When t, the highest priority entries are bold, lowest priority italic.
86fbb8ca 1488However, settings in `org-priority-faces' will overrule these faces.
c8d0cf5c
CD
1489When this variable is the symbol `cookies', only fontify the
1490cookies, not the entire task.
621f83e4
CD
1491This may also be an association list of priority faces, whose
1492keys are the character values of `org-highest-priority',
1493`org-default-priority', and `org-lowest-priority' (the default values
ed21c5c8
CD
1494are ?A, ?B, and ?C, respectively). The face may be a named face, a
1495color as a string, or a list like `(:background \"Red\")'.
1496If it is a color, the variable `org-faces-easy-properties'
1497determines if it is a foreground or a background color."
20908596
CD
1498 :group 'org-agenda-line-format
1499 :type '(choice
1500 (const :tag "Never" nil)
1501 (const :tag "Defaults" t)
c8d0cf5c 1502 (const :tag "Cookies only" cookies)
20908596
CD
1503 (repeat :tag "Specify"
1504 (list (character :tag "Priority" :value ?A)
ed21c5c8
CD
1505 (choice :tag "Face "
1506 (string :tag "Color")
1507 (sexp :tag "Face"))))))
20908596 1508
acedf35c
CD
1509(defcustom org-agenda-day-face-function nil
1510 "Function called to determine what face should be used to display a day.
1511The only argument passed to that function is the day. It should
1512returns a face, or nil if does not want to specify a face and let
1513the normal rules apply."
1514 :group 'org-agenda-line-format
1515 :type 'function)
1516
1517(defcustom org-agenda-category-icon-alist nil
1518 "Alist of category icon to be displayed in agenda views.
1519
1520Each entry should have the following format:
1521
1522 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1523
1524Where CATEGORY-REGEXP is a regexp matching the categories where
1525the icon should be displayed.
1526FILE-OR-DATA either a file path or a string containing image data.
1527
1528The other fields can be ommited safely if not needed:
1529TYPE indicates the image type.
1530DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1531image data.
1532PROPS are additional image attributes to assign to the image,
1533like, e.g. `:ascent center'.
1534
1535 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1536
1537If you want to set the display properties yourself, just put a
1538list as second element:
1539
1540 (CATEGORY-REGEXP (MY PROPERTY LIST))
1541
1542For example, to display a 16px horizontal space for Emacs
1543category, you can use:
1544
1545 (\"Emacs\" '(space . (:width (16))))"
1546 :group 'org-agenda-line-format
1547 :type '(alist :key-type (string :tag "Regexp matching category")
1548 :value-type (choice (list :tag "Icon"
1549 (string :tag "File or data")
1550 (symbol :tag "Type")
1551 (boolean :tag "Data?")
1552 (repeat :tag "Extra image properties" :inline t symbol))
1553 (list :tag "Display properties" sexp))))
1554
20908596
CD
1555(defgroup org-agenda-column-view nil
1556 "Options concerning column view in the agenda."
1557 :tag "Org Agenda Column View"
1558 :group 'org-agenda)
1559
1560(defcustom org-agenda-columns-show-summaries t
ed21c5c8 1561 "Non-nil means show summaries for columns displayed in the agenda view."
20908596
CD
1562 :group 'org-agenda-column-view
1563 :type 'boolean)
1564
b349f79f 1565(defcustom org-agenda-columns-remove-prefix-from-item t
ed21c5c8 1566 "Non-nil means remove the prefix from a headline for agenda column view.
b349f79f
CD
1567The special ITEM field in the columns format contains the current line, with
1568all information shown in other columns (like the TODO state or a tag).
1569When this variable is non-nil, also the agenda prefix will be removed from
1570the content of the ITEM field, to make sure as much as possible of the
1571headline can be shown in the limited width of the field."
1572 :group 'org-agenda
1573 :type 'boolean)
1574
20908596 1575(defcustom org-agenda-columns-compute-summary-properties t
ed21c5c8 1576 "Non-nil means recompute all summary properties before column view.
20908596
CD
1577When column view in the agenda is listing properties that have a summary
1578operator, it can go to all relevant buffers and recompute the summaries
1579there. This can mean overhead for the agenda column view, but is necessary
1580to have thing up to date.
1581As a special case, a CLOCKSUM property also makes sure that the clock
1582computations are current."
1583 :group 'org-agenda-column-view
1584 :type 'boolean)
1585
1586(defcustom org-agenda-columns-add-appointments-to-effort-sum nil
ed21c5c8 1587 "Non-nil means the duration of an appointment will add to day effort.
20908596
CD
1588The property to which appointment durations will be added is the one given
1589in the option `org-effort-property'. If an appointment does not have
1590an end time, `org-agenda-default-appointment-duration' will be used. If that
1591is not set, an appointment without end time will not contribute to the time
1592estimate."
1593 :group 'org-agenda-column-view
1594 :type 'boolean)
1595
8bfe682a
CD
1596(defcustom org-agenda-auto-exclude-function nil
1597 "A function called with a tag to decide if it is filtered on '/ RET'.
1598The sole argument to the function, which is called once for each
1599possible tag, is a string giving the name of the tag. The
1600function should return either nil if the tag should be included
ed21c5c8
CD
1601as normal, or \"-<TAG>\" to exclude the tag.
1602Note that for the purpose of tag filtering, only the lower-case version of
1603all tags will be considered, so that this function will only ever see
1604the lower-case version of all tags."
8bfe682a
CD
1605 :group 'org-agenda
1606 :type 'function)
1607
20908596
CD
1608(eval-when-compile
1609 (require 'cl))
1610(require 'org)
1611
afe98dfa
CD
1612(defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1613 "Execute BODY with point at location given by `org-hd-marker' property.
1614If STRING is non-nil, the text property will be fetched from position 0
1615in that string. If STRING is nil, it will be fetched from the beginning
1616of the current line."
1617 `(let ((marker (get-text-property (if string 0 (point-at-bol))
1618 'org-hd-marker string)))
1619 (with-current-buffer (marker-buffer marker)
1620 (save-excursion
1621 (goto-char marker)
1622 ,@body))))
1623
20908596
CD
1624(defun org-add-agenda-custom-command (entry)
1625 "Replace or add a command in `org-agenda-custom-commands'.
1626This is mostly for hacking and trying a new command - once the command
1627works you probably want to add it to `org-agenda-custom-commands' for good."
1628 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1629 (if ass
1630 (setcdr ass (cdr entry))
1631 (push entry org-agenda-custom-commands))))
1632
1633;;; Define the Org-agenda-mode
1634
1635(defvar org-agenda-mode-map (make-sparse-keymap)
1636 "Keymap for `org-agenda-mode'.")
8bfe682a
CD
1637(if (fboundp 'defvaralias)
1638 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
20908596
CD
1639
1640(defvar org-agenda-menu) ; defined later in this file.
c8d0cf5c 1641(defvar org-agenda-restrict) ; defined later in this file.
20908596 1642(defvar org-agenda-follow-mode nil)
54a0dee5 1643(defvar org-agenda-entry-text-mode nil)
20908596
CD
1644(defvar org-agenda-clockreport-mode nil)
1645(defvar org-agenda-show-log nil)
1646(defvar org-agenda-redo-command nil)
1647(defvar org-agenda-query-string nil)
0bd48b37 1648(defvar org-agenda-mode-hook nil
86fbb8ca 1649 "Hook for `org-agenda-mode', run after the mode is turned on.")
20908596
CD
1650(defvar org-agenda-type nil)
1651(defvar org-agenda-force-single-file nil)
c8d0cf5c 1652(defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
20908596
CD
1653
1654(defun org-agenda-mode ()
1655 "Mode for time-sorted view on action items in Org-mode files.
1656
1657The following commands are available:
1658
1659\\{org-agenda-mode-map}"
1660 (interactive)
1661 (kill-all-local-variables)
1662 (setq org-agenda-undo-list nil
c8d0cf5c
CD
1663 org-agenda-pending-undo-list nil
1664 org-agenda-bulk-marked-entries nil)
20908596
CD
1665 (setq major-mode 'org-agenda-mode)
1666 ;; Keep global-font-lock-mode from turning on font-lock-mode
1667 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1668 (setq mode-name "Org-Agenda")
1669 (use-local-map org-agenda-mode-map)
1670 (easy-menu-add org-agenda-menu)
1671 (if org-startup-truncated (setq truncate-lines t))
54a0dee5 1672 (org-set-local 'line-move-visual nil)
20908596
CD
1673 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1674 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1675 ;; Make sure properties are removed when copying text
1676 (when (boundp 'buffer-substring-filters)
1677 (org-set-local 'buffer-substring-filters
1678 (cons (lambda (x)
1679 (set-text-properties 0 (length x) nil x) x)
1680 buffer-substring-filters)))
1681 (unless org-agenda-keep-modes
1682 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
54a0dee5 1683 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
20908596 1684 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
c8d0cf5c
CD
1685 org-agenda-show-log org-agenda-start-with-log-mode))
1686
20908596
CD
1687 (easy-menu-change
1688 '("Agenda") "Agenda Files"
1689 (append
1690 (list
1691 (vector
1692 (if (get 'org-agenda-files 'org-restrict)
1693 "Restricted to single file"
1694 "Edit File List")
1695 '(org-edit-agenda-file-list)
1696 (not (get 'org-agenda-files 'org-restrict)))
1697 "--")
1698 (mapcar 'org-file-menu-entry (org-agenda-files))))
1699 (org-agenda-set-mode-name)
1700 (apply
1701 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1702 (list 'org-agenda-mode-hook)))
1703
1704(substitute-key-definition 'undo 'org-agenda-undo
1705 org-agenda-mode-map global-map)
1706(org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1707(org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1708(org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1709(org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
c8d0cf5c
CD
1710(org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1711(org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1712(org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1713(org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1714(org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1715(org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
8bfe682a
CD
1716(org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1717(org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1718(org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
54a0dee5 1719(org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
8bfe682a 1720(org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
20908596 1721(org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
20908596 1722(org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
8bfe682a
CD
1723(org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1724(org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1725(org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
20908596
CD
1726(org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1727(org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1728(org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
20908596
CD
1729(org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1730(org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
54a0dee5 1731(org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
20908596 1732(org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
8bfe682a 1733(org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
20908596 1734(org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
71d35b24 1735(org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
20908596
CD
1736(org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1737(org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1738(org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1739(org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
20908596
CD
1740(org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1741(org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1742(org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
b349f79f
CD
1743(org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1744(org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
c8d0cf5c
CD
1745(org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1746(org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1747(org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1748(org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
20908596
CD
1749
1750(org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1751(org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1752(org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1753(let ((l '(1 2 3 4 5 6 7 8 9 0)))
1754 (while l (org-defkey org-agenda-mode-map
1755 (int-to-string (pop l)) 'digit-argument)))
1756
54a0dee5 1757(org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
20908596 1758(org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
54a0dee5 1759(org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
20908596 1760(org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
c8d0cf5c 1761(org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
20908596 1762(org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
ed21c5c8 1763(org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
20908596
CD
1764(org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1765(org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1766(org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
54a0dee5
CD
1767(org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1768(org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1769(org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1770 'org-clock-modify-effort-estimate)
1771(org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
20908596
CD
1772(org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1773(org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1774(org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
20908596 1775(org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
c8d0cf5c 1776(org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
20908596
CD
1777(org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1778(org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
8bfe682a
CD
1779(org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1780(org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1781(substitute-key-definition 'next-line 'org-agenda-next-line
1782 org-agenda-mode-map global-map)
1783(substitute-key-definition 'previous-line 'org-agenda-previous-line
1784 org-agenda-mode-map global-map)
621f83e4 1785(org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
20908596
CD
1786(org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1787(org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1788(org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1789(org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1790(org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1791(org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1792(org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1793(org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1794(org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1795(org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1796(org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1797(org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1798(org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1799(org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1800(org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1801(org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1802(org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1803(org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
afe98dfa 1804(org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
20908596
CD
1805(org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1806(org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1807(org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1808(org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1809(org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1810(org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
54a0dee5
CD
1811(org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1812(org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
20908596 1813(org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
c8d0cf5c 1814(org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
20908596
CD
1815
1816(org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1817(org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1818(org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1819(org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
621f83e4 1820(org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
71d35b24 1821(org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
c8d0cf5c 1822(org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
8d642074
CD
1823(define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1824(org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1825(org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
20908596 1826
86fbb8ca
CD
1827(org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1828(org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
20908596 1829(when org-agenda-mouse-1-follows-link
8bfe682a 1830 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
20908596
CD
1831(easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1832 '("Agenda"
1833 ("Agenda Files")
1834 "--"
8d642074
CD
1835 ("Agenda Dates"
1836 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1837 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1838 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1839 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1840 "--"
1841 ("View"
1842 ["Day View" org-agenda-day-view
1843 :active (org-agenda-check-type nil 'agenda)
acedf35c 1844 :style radio :selected (eq org-agenda-current-span 'day)
8d642074
CD
1845 :keys "v d (or just d)"]
1846 ["Week View" org-agenda-week-view
1847 :active (org-agenda-check-type nil 'agenda)
acedf35c 1848 :style radio :selected (eq org-agenda-current-span 'week)
8d642074
CD
1849 :keys "v w (or just w)"]
1850 ["Month View" org-agenda-month-view
1851 :active (org-agenda-check-type nil 'agenda)
acedf35c 1852 :style radio :selected (eq org-agenda-current-span 'month)
8d642074
CD
1853 :keys "v m"]
1854 ["Year View" org-agenda-year-view
1855 :active (org-agenda-check-type nil 'agenda)
acedf35c 1856 :style radio :selected (eq org-agenda-current-span 'year)
8d642074
CD
1857 :keys "v y"]
1858 "--"
1859 ["Include Diary" org-agenda-toggle-diary
1860 :style toggle :selected org-agenda-include-diary
1861 :active (org-agenda-check-type nil 'agenda)]
ed21c5c8
CD
1862 ["Include Deadlines" org-agenda-toggle-deadlines
1863 :style toggle :selected org-agenda-include-deadlines
1864 :active (org-agenda-check-type nil 'agenda)]
8d642074
CD
1865 ["Use Time Grid" org-agenda-toggle-time-grid
1866 :style toggle :selected org-agenda-use-time-grid
1867 :active (org-agenda-check-type nil 'agenda)]
1868 "--"
1869 ["Show clock report" org-agenda-clockreport-mode
1870 :style toggle :selected org-agenda-clockreport-mode
1871 :active (org-agenda-check-type nil 'agenda)]
1872 ["Show some entry text" org-agenda-entry-text-mode
1873 :style toggle :selected org-agenda-entry-text-mode
1874 :active t]
1875 "--"
1876 ["Show Logbook entries" org-agenda-log-mode
1877 :style toggle :selected org-agenda-show-log
1878 :active (org-agenda-check-type nil 'agenda 'timeline)
1879 :keys "v l (or just l)"]
1880 ["Include archived trees" org-agenda-archives-mode
1881 :style toggle :selected org-agenda-archives-mode :active t
1882 :keys "v a"]
1883 ["Include archive files" (org-agenda-archives-mode t)
1884 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1885 :keys "v A"]
1886 "--"
1887 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1888 ["Write view to file" org-write-agenda t]
1889 ["Rebuild buffer" org-agenda-redo t]
1890 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1891 "--"
1892 ["Show original entry" org-agenda-show t]
20908596
CD
1893 ["Go To (other window)" org-agenda-goto t]
1894 ["Go To (this window)" org-agenda-switch-to t]
1895 ["Follow Mode" org-agenda-follow-mode
1896 :style toggle :selected org-agenda-follow-mode :active t]
8d642074 1897; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
20908596 1898 "--"
8d642074
CD
1899 ("TODO"
1900 ["Cycle TODO" org-agenda-todo t]
1901 ["Next TODO set" org-agenda-todo-nextset t]
1902 ["Previous TODO set" org-agenda-todo-previousset t]
1903 ["Add note" org-agenda-add-note t])
1904 ("Archive/Refile/Delete"
8bfe682a
CD
1905 ["Archive default" org-agenda-archive-default t]
1906 ["Archive default" org-agenda-archive-default-with-confirmation t]
20908596
CD
1907 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1908 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
c8d0cf5c 1909 ["Archive subtree" org-agenda-archive t]
8d642074
CD
1910 "--"
1911 ["Refile" org-agenda-refile t]
1912 "--"
1913 ["Delete subtree" org-agenda-kill t])
c8d0cf5c 1914 ("Bulk action"
8d642074
CD
1915 ["Mark entry" org-agenda-bulk-mark t]
1916 ["Unmark entry" org-agenda-bulk-unmark t]
c8d0cf5c
CD
1917 ["Act on all marked" org-agenda-bulk-action t]
1918 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
1919 "--"
20908596
CD
1920 ("Tags and Properties"
1921 ["Show all Tags" org-agenda-show-tags t]
1922 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1923 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1924 "--"
1925 ["Column View" org-columns t])
8d642074 1926 ("Deadline/Schedule"
20908596
CD
1927 ["Schedule" org-agenda-schedule t]
1928 ["Set Deadline" org-agenda-deadline t]
1929 "--"
b349f79f
CD
1930 ["Mark item" org-agenda-action :active t :keys "k m"]
1931 ["Show mark item" org-agenda-action :active t :keys "k v"]
1932 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1933 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1934 "--"
20908596
CD
1935 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1936 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
c8d0cf5c
CD
1937 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
1938 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
1939 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
1940 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
20908596 1941 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
54a0dee5 1942 ("Clock and Effort"
20908596
CD
1943 ["Clock in" org-agenda-clock-in t]
1944 ["Clock out" org-agenda-clock-out t]
1945 ["Clock cancel" org-agenda-clock-cancel t]
54a0dee5
CD
1946 ["Goto running clock" org-clock-goto t]
1947 "--"
1948 ["Set Effort" org-agenda-set-effort t]
1949 ["Change clocked effort" org-clock-modify-effort-estimate
1950 (org-clock-is-active)])
20908596
CD
1951 ("Priority"
1952 ["Set Priority" org-agenda-priority t]
1953 ["Increase Priority" org-agenda-priority-up t]
1954 ["Decrease Priority" org-agenda-priority-down t]
1955 ["Show Priority" org-agenda-show-priority t])
1956 ("Calendar/Diary"
1957 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1958 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1959 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1960 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1961 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1962 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1963 "--"
8d642074 1964 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
20908596 1965 "--"
8d642074 1966 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2c3ad40d 1967 "--"
8d642074
CD
1968 ("MobileOrg"
1969 ["Push Files and Views" org-mobile-push t]
1970 ["Get Captured and Flagged" org-mobile-pull t]
1971 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
1972 ["Show note / unflag" org-agenda-show-the-flagging-note t]
c8d0cf5c 1973 "--"
8d642074 1974 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
20908596
CD
1975 "--"
1976 ["Quit" org-agenda-quit t]
1977 ["Exit and Release Buffers" org-agenda-exit t]
1978 ))
1979
1980;;; Agenda undo
1981
1982(defvar org-agenda-allow-remote-undo t
ed21c5c8 1983 "Non-nil means allow remote undo from the agenda buffer.")
20908596
CD
1984(defvar org-agenda-undo-list nil
1985 "List of undoable operations in the agenda since last refresh.")
1986(defvar org-agenda-undo-has-started-in nil
1987 "Buffers that have already seen `undo-start' in the current undo sequence.")
1988(defvar org-agenda-pending-undo-list nil
33306645 1989 "In a series of undo commands, this is the list of remaining undo items.")
20908596
CD
1990
1991
1992(defun org-agenda-undo ()
1993 "Undo a remote editing step in the agenda.
1994This undoes changes both in the agenda buffer and in the remote buffer
1995that have been changed along."
1996 (interactive)
1997 (or org-agenda-allow-remote-undo
f924a367 1998 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
20908596
CD
1999 (if (not (eq this-command last-command))
2000 (setq org-agenda-undo-has-started-in nil
2001 org-agenda-pending-undo-list org-agenda-undo-list))
2002 (if (not org-agenda-pending-undo-list)
2003 (error "No further undo information"))
2004 (let* ((entry (pop org-agenda-pending-undo-list))
2005 buf line cmd rembuf)
2006 (setq cmd (pop entry) line (pop entry))
2007 (setq rembuf (nth 2 entry))
2008 (org-with-remote-undo rembuf
2009 (while (bufferp (setq buf (pop entry)))
2010 (if (pop entry)
2011 (with-current-buffer buf
2012 (let ((last-undo-buffer buf)
2013 (inhibit-read-only t))
2014 (unless (memq buf org-agenda-undo-has-started-in)
2015 (push buf org-agenda-undo-has-started-in)
2016 (make-local-variable 'pending-undo-list)
2017 (undo-start))
2018 (while (and pending-undo-list
2019 (listp pending-undo-list)
2020 (not (car pending-undo-list)))
2021 (pop pending-undo-list))
2022 (undo-more 1))))))
54a0dee5 2023 (org-goto-line line)
20908596
CD
2024 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2025
2026(defun org-verify-change-for-undo (l1 l2)
2027 "Verify that a real change occurred between the undo lists L1 and L2."
2028 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2029 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2030 (not (eq l1 l2)))
2031
2032;;; Agenda dispatch
2033
2034(defvar org-agenda-restrict nil)
2035(defvar org-agenda-restrict-begin (make-marker))
2036(defvar org-agenda-restrict-end (make-marker))
2037(defvar org-agenda-last-dispatch-buffer nil)
2038(defvar org-agenda-overriding-restriction nil)
2039
2040;;;###autoload
c8d0cf5c 2041(defun org-agenda (&optional arg keys restriction)
20908596
CD
2042 "Dispatch agenda commands to collect entries to the agenda buffer.
2043Prompts for a command to execute. Any prefix arg will be passed
2044on to the selected command. The default selections are:
2045
2046a Call `org-agenda-list' to display the agenda for current day or week.
2047t Call `org-todo-list' to display the global todo list.
2048T Call `org-todo-list' to display the global todo list, select only
2049 entries with a specific TODO keyword (the user gets a prompt).
2050m Call `org-tags-view' to display headlines with tags matching
2051 a condition (the user is prompted for the condition).
2052M Like `m', but select only TODO entries, no ordinary headlines.
2053L Create a timeline for the current buffer.
2054e Export views to associated files.
c8d0cf5c 2055s Search entries for keywords.
8bfe682a 2056/ Multi occur across all agenda files and also files listed
c8d0cf5c
CD
2057 in `org-agenda-text-search-extra-files'.
2058< Restrict agenda commands to buffer, subtree, or region.
2059 Press several times to get the desired effect.
2060> Remove a previous restriction.
2061# List \"stuck\" projects.
2062! Configure what \"stuck\" means.
2063C Configure custom agenda commands.
20908596
CD
2064
2065More commands can be added by configuring the variable
2066`org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2067searches can be pre-defined in this way.
2068
2069If the current buffer is in Org-mode and visiting a file, you can also
2070first press `<' once to indicate that the agenda should be temporarily
2071\(until the next use of \\[org-agenda]) restricted to the current file.
2072Pressing `<' twice means to restrict to the current subtree or region
2073\(if active)."
2074 (interactive "P")
2075 (catch 'exit
2076 (let* ((prefix-descriptions nil)
54a0dee5
CD
2077 (org-agenda-window-setup (if (equal (buffer-name)
2078 org-agenda-buffer-name)
2079 'current-window
2080 org-agenda-window-setup))
20908596
CD
2081 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2082 (org-agenda-custom-commands
2083 ;; normalize different versions
2084 (delq nil
2085 (mapcar
2086 (lambda (x)
2087 (cond ((stringp (cdr x))
2088 (push x prefix-descriptions)
2089 nil)
2090 ((stringp (nth 1 x)) x)
2091 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2092 (t (cons (car x) (cons "" (cdr x))))))
2093 org-agenda-custom-commands)))
2094 (buf (current-buffer))
2095 (bfn (buffer-file-name (buffer-base-buffer)))
2096 entry key type match lprops ans)
8d642074 2097 ;; Turn off restriction unless there is an overriding one,
20908596 2098 (unless org-agenda-overriding-restriction
8bfe682a 2099 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
8d642074
CD
2100 ;; There is a request to keep the file list in place
2101 (put 'org-agenda-files 'org-restrict nil))
20908596
CD
2102 (setq org-agenda-restrict nil)
2103 (move-marker org-agenda-restrict-begin nil)
2104 (move-marker org-agenda-restrict-end nil))
2105 ;; Delete old local properties
2106 (put 'org-agenda-redo-command 'org-lprops nil)
2107 ;; Remember where this call originated
2108 (setq org-agenda-last-dispatch-buffer (current-buffer))
2109 (unless keys
2110 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2111 keys (car ans)
2112 restriction (cdr ans)))
8bfe682a 2113 ;; Establish the restriction, if any
20908596
CD
2114 (when (and (not org-agenda-overriding-restriction) restriction)
2115 (put 'org-agenda-files 'org-restrict (list bfn))
2116 (cond
2117 ((eq restriction 'region)
2118 (setq org-agenda-restrict t)
2119 (move-marker org-agenda-restrict-begin (region-beginning))
2120 (move-marker org-agenda-restrict-end (region-end)))
2121 ((eq restriction 'subtree)
2122 (save-excursion
2123 (setq org-agenda-restrict t)
2124 (org-back-to-heading t)
2125 (move-marker org-agenda-restrict-begin (point))
2126 (move-marker org-agenda-restrict-end
2127 (progn (org-end-of-subtree t)))))))
2128
20908596
CD
2129 ;; For example the todo list should not need it (but does...)
2130 (cond
2131 ((setq entry (assoc keys org-agenda-custom-commands))
2132 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2133 (progn
8bfe682a
CD
2134 (setq type (nth 2 entry) match (eval (nth 3 entry))
2135 lprops (nth 4 entry))
20908596
CD
2136 (put 'org-agenda-redo-command 'org-lprops lprops)
2137 (cond
2138 ((eq type 'agenda)
2139 (org-let lprops '(org-agenda-list current-prefix-arg)))
2140 ((eq type 'alltodo)
2141 (org-let lprops '(org-todo-list current-prefix-arg)))
2142 ((eq type 'search)
2143 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2144 ((eq type 'stuck)
2145 (org-let lprops '(org-agenda-list-stuck-projects
2146 current-prefix-arg)))
2147 ((eq type 'tags)
2148 (org-let lprops '(org-tags-view current-prefix-arg match)))
2149 ((eq type 'tags-todo)
2150 (org-let lprops '(org-tags-view '(4) match)))
2151 ((eq type 'todo)
2152 (org-let lprops '(org-todo-list match)))
2153 ((eq type 'tags-tree)
2154 (org-check-for-org-mode)
c8d0cf5c 2155 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
20908596
CD
2156 ((eq type 'todo-tree)
2157 (org-check-for-org-mode)
2158 (org-let lprops
2159 '(org-occur (concat "^" outline-regexp "[ \t]*"
2160 (regexp-quote match) "\\>"))))
2161 ((eq type 'occur-tree)
2162 (org-check-for-org-mode)
2163 (org-let lprops '(org-occur match)))
2164 ((functionp type)
2165 (org-let lprops '(funcall type match)))
2166 ((fboundp type)
2167 (org-let lprops '(funcall type match)))
2168 (t (error "Invalid custom agenda command type %s" type))))
2169 (org-run-agenda-series (nth 1 entry) (cddr entry))))
2170 ((equal keys "C")
2171 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2172 (customize-variable 'org-agenda-custom-commands))
2173 ((equal keys "a") (call-interactively 'org-agenda-list))
2174 ((equal keys "s") (call-interactively 'org-search-view))
2175 ((equal keys "t") (call-interactively 'org-todo-list))
2176 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2177 ((equal keys "m") (call-interactively 'org-tags-view))
2178 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2179 ((equal keys "e") (call-interactively 'org-store-agenda-views))
8d642074
CD
2180 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2181 (org-add-hook
2182 'post-command-hook
2183 (lambda ()
2184 (unless (current-message)
2185 (let* ((m (org-agenda-get-any-marker))
2186 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2187 (when note
2188 (message (concat
2189 "FLAGGING-NOTE ([?] for more info): "
2190 (org-add-props
2191 (replace-regexp-in-string
2192 "\\\\n" "//"
2193 (copy-sequence note))
2194 nil 'face 'org-warning)))))))
2195 t t))
20908596
CD
2196 ((equal keys "L")
2197 (unless (org-mode-p)
2198 (error "This is not an Org-mode file"))
2199 (unless restriction
2200 (put 'org-agenda-files 'org-restrict (list bfn))
2201 (org-call-with-arg 'org-timeline arg)))
2202 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2203 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2204 ((equal keys "!") (customize-variable 'org-stuck-projects))
2205 (t (error "Invalid agenda key"))))))
2206
2207(defun org-agenda-normalize-custom-commands (cmds)
2208 (delq nil
2209 (mapcar
2210 (lambda (x)
2211 (cond ((stringp (cdr x)) nil)
2212 ((stringp (nth 1 x)) x)
2213 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2214 (t (cons (car x) (cons "" (cdr x))))))
2215 cmds)))
2216
2217(defun org-agenda-get-restriction-and-command (prefix-descriptions)
2218 "The user interface for selecting an agenda command."
2219 (catch 'exit
2220 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2221 (restrict-ok (and bfn (org-mode-p)))
2222 (region-p (org-region-active-p))
2223 (custom org-agenda-custom-commands)
2224 (selstring "")
2225 restriction second-time
afe98dfa
CD
2226 c entry key type match prefixes rmheader header-end custom1 desc
2227 line lines left right n n1)
20908596
CD
2228 (save-window-excursion
2229 (delete-other-windows)
2230 (org-switch-to-buffer-other-window " *Agenda Commands*")
2231 (erase-buffer)
2232 (insert (eval-when-compile
2233 (let ((header
2234"
2c3ad40d 2235Press key for an agenda command: < Buffer, subtree/region restriction
20908596
CD
2236-------------------------------- > Remove restriction
2237a Agenda for current week or day e Export agenda views
2238t List of all TODO entries T Entries with special TODO kwd
621f83e4 2239m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
20908596
CD
2240L Timeline for current buffer # List stuck projects (!=configure)
2241s Search for keywords C Configure custom agenda commands
8d642074 2242/ Multi-occur ? Find :FLAGGED: entries
20908596
CD
2243")
2244 (start 0))
2245 (while (string-match
2246 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2247 header start)
2248 (setq start (match-end 0))
2249 (add-text-properties (match-beginning 2) (match-end 2)
2250 '(face bold) header))
2251 header)))
2252 (setq header-end (move-marker (make-marker) (point)))
2253 (while t
2254 (setq custom1 custom)
2255 (when (eq rmheader t)
54a0dee5 2256 (org-goto-line 1)
20908596
CD
2257 (re-search-forward ":" nil t)
2258 (delete-region (match-end 0) (point-at-eol))
2259 (forward-char 1)
2260 (looking-at "-+")
2261 (delete-region (match-end 0) (point-at-eol))
2262 (move-marker header-end (match-end 0)))
2263 (goto-char header-end)
2264 (delete-region (point) (point-max))
afe98dfa
CD
2265
2266 ;; Produce all the lines that describe custom commands and prefixes
2267 (setq lines nil)
20908596
CD
2268 (while (setq entry (pop custom1))
2269 (setq key (car entry) desc (nth 1 entry)
54a0dee5
CD
2270 type (nth 2 entry)
2271 match (nth 3 entry))
20908596
CD
2272 (if (> (length key) 1)
2273 (add-to-list 'prefixes (string-to-char key))
afe98dfa
CD
2274 (setq line
2275 (format
2276 "%-4s%-14s"
2277 (org-add-props (copy-sequence key)
2278 '(face bold))
2279 (cond
2280 ((string-match "\\S-" desc) desc)
2281 ((eq type 'agenda) "Agenda for current week or day")
2282 ((eq type 'alltodo) "List of all TODO entries")
2283 ((eq type 'search) "Word search")
2284 ((eq type 'stuck) "List of stuck projects")
2285 ((eq type 'todo) "TODO keyword")
2286 ((eq type 'tags) "Tags query")
2287 ((eq type 'tags-todo) "Tags (TODO)")
2288 ((eq type 'tags-tree) "Tags tree")
2289 ((eq type 'todo-tree) "TODO kwd tree")
2290 ((eq type 'occur-tree) "Occur tree")
2291 ((functionp type) (if (symbolp type)
2292 (symbol-name type)
2293 "Lambda expression"))
2294 (t "???"))))
2295 (if org-agenda-menu-show-matcher
2296 (setq line
2297 (concat line ": "
2298 (cond
2299 ((stringp match)
2300 (setq match (copy-sequence match))
2301 (org-add-props match nil 'face 'org-warning))
2302 (match
2303 (format "set of %d commands" (length match)))
2304 (t ""))))
2305 (if (org-string-nw-p match)
2306 (add-text-properties
2307 0 (length line) (list 'help-echo
2308 (concat "Matcher: "match)) line)))
2309 (push line lines)))
2310 (setq lines (nreverse lines))
20908596
CD
2311 (when prefixes
2312 (mapc (lambda (x)
afe98dfa
CD
2313 (push
2314 (format "%s %s"
20908596 2315 (org-add-props (char-to-string x)
afe98dfa
CD
2316 nil 'face 'bold)
2317 (or (cdr (assoc (concat selstring
2318 (char-to-string x))
20908596 2319 prefix-descriptions))
afe98dfa
CD
2320 "Prefix key"))
2321 lines))
20908596 2322 prefixes))
afe98dfa
CD
2323
2324 ;; Check if we should display in two columns
2325 (if org-agenda-menu-two-column
2326 (progn
2327 (setq n (length lines)
2328 n1 (+ (/ n 2) (mod n 2))
2329 right (nthcdr n1 lines)
2330 left (copy-sequence lines))
2331 (setcdr (nthcdr (1- n1) left) nil))
2332 (setq left lines right nil))
2333 (while left
2334 (insert "\n" (pop left))
2335 (when right
2336 (if (< (current-column) 40)
2337 (move-to-column 40 t)
2338 (insert " "))
2339 (insert (pop right))))
2340
2341 ;; Make the window the right size
20908596 2342 (goto-char (point-min))
93b62de8
CD
2343 (if second-time
2344 (if (not (pos-visible-in-window-p (point-max)))
2345 (org-fit-window-to-buffer))
2346 (setq second-time t)
2347 (org-fit-window-to-buffer))
afe98dfa
CD
2348
2349 ;; Ask for selection
20908596
CD
2350 (message "Press key for agenda command%s:"
2351 (if (or restrict-ok org-agenda-overriding-restriction)
2352 (if org-agenda-overriding-restriction
2353 " (restriction lock active)"
2354 (if restriction
2355 (format " (restricted to %s)" restriction)
2356 " (unrestricted)"))
2357 ""))
2358 (setq c (read-char-exclusive))
2359 (message "")
2360 (cond
2361 ((assoc (char-to-string c) custom)
2362 (setq selstring (concat selstring (char-to-string c)))
2363 (throw 'exit (cons selstring restriction)))
2364 ((memq c prefixes)
2365 (setq selstring (concat selstring (char-to-string c))
2366 prefixes nil
2367 rmheader (or rmheader t)
2368 custom (delq nil (mapcar
2369 (lambda (x)
2370 (if (or (= (length (car x)) 1)
2371 (/= (string-to-char (car x)) c))
2372 nil
2373 (cons (substring (car x) 1) (cdr x))))
2374 custom))))
2375 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2376 (message "Restriction is only possible in Org-mode buffers")
2377 (ding) (sit-for 1))
2378 ((eq c ?1)
2379 (org-agenda-remove-restriction-lock 'noupdate)
2380 (setq restriction 'buffer))
2381 ((eq c ?0)
2382 (org-agenda-remove-restriction-lock 'noupdate)
2383 (setq restriction (if region-p 'region 'subtree)))
2384 ((eq c ?<)
2385 (org-agenda-remove-restriction-lock 'noupdate)
2386 (setq restriction
2387 (cond
2388 ((eq restriction 'buffer)
2389 (if region-p 'region 'subtree))
2390 ((memq restriction '(subtree region))
2391 nil)
2392 (t 'buffer))))
2393 ((eq c ?>)
2394 (org-agenda-remove-restriction-lock 'noupdate)
2395 (setq restriction nil))
8d642074 2396 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
20908596
CD
2397 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2398 ((and (> (length selstring) 0) (eq c ?\d))
2399 (delete-window)
2400 (org-agenda-get-restriction-and-command prefix-descriptions))
2401
2402 ((equal c ?q) (error "Abort"))
2403 (t (error "Invalid key %c" c))))))))
2404
2405(defun org-run-agenda-series (name series)
c8d0cf5c 2406 (org-let (nth 1 series) '(org-prepare-agenda name))
20908596
CD
2407 (let* ((org-agenda-multi t)
2408 (redo (list 'org-run-agenda-series name (list 'quote series)))
2409 (cmds (car series))
2410 (gprops (nth 1 series))
2411 match ;; The byte compiler incorrectly complains about this. Keep it!
2412 cmd type lprops)
2413 (while (setq cmd (pop cmds))
8bfe682a 2414 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
20908596
CD
2415 (cond
2416 ((eq type 'agenda)
2417 (org-let2 gprops lprops
2418 '(call-interactively 'org-agenda-list)))
2419 ((eq type 'alltodo)
2420 (org-let2 gprops lprops
2421 '(call-interactively 'org-todo-list)))
2422 ((eq type 'search)
2423 (org-let2 gprops lprops
2424 '(org-search-view current-prefix-arg match nil)))
2425 ((eq type 'stuck)
2426 (org-let2 gprops lprops
2427 '(call-interactively 'org-agenda-list-stuck-projects)))
2428 ((eq type 'tags)
2429 (org-let2 gprops lprops
2430 '(org-tags-view current-prefix-arg match)))
2431 ((eq type 'tags-todo)
2432 (org-let2 gprops lprops
2433 '(org-tags-view '(4) match)))
2434 ((eq type 'todo)
2435 (org-let2 gprops lprops
2436 '(org-todo-list match)))
2437 ((fboundp type)
2438 (org-let2 gprops lprops
2439 '(funcall type match)))
2440 (t (error "Invalid type in command series"))))
2441 (widen)
2442 (setq org-agenda-redo-command redo)
2443 (goto-char (point-min)))
c8d0cf5c 2444 (org-fit-agenda-window)
0bd48b37 2445 (org-let (nth 1 series) '(org-finalize-agenda)))
20908596
CD
2446
2447;;;###autoload
2448(defmacro org-batch-agenda (cmd-key &rest parameters)
2449 "Run an agenda command in batch mode and send the result to STDOUT.
2450If CMD-KEY is a string of length 1, it is used as a key in
2451`org-agenda-custom-commands' and triggers this command. If it is a
2452longer string it is used as a tags/todo match string.
86fbb8ca 2453Parameters are alternating variable names and values that will be bound
20908596
CD
2454before running the agenda command."
2455 (let (pars)
2456 (while parameters
2457 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2458 (if (> (length cmd-key) 2)
2459 (eval (list 'let (nreverse pars)
2460 (list 'org-tags-view nil cmd-key)))
2461 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2462 (set-buffer org-agenda-buffer-name)
2463 (princ (org-encode-for-stdout (buffer-string)))))
2464
8bfe682a
CD
2465;(defun org-encode-for-stdout (string)
2466; (if (fboundp 'encode-coding-string)
2467; (encode-coding-string string buffer-file-coding-system)
2468; string))
2469
20908596 2470(defun org-encode-for-stdout (string)
8bfe682a 2471 string)
20908596
CD
2472
2473(defvar org-agenda-info nil)
2474
2475;;;###autoload
2476(defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2477 "Run an agenda command in batch mode and send the result to STDOUT.
2478If CMD-KEY is a string of length 1, it is used as a key in
2479`org-agenda-custom-commands' and triggers this command. If it is a
2480longer string it is used as a tags/todo match string.
86fbb8ca 2481Parameters are alternating variable names and values that will be bound
20908596
CD
2482before running the agenda command.
2483
2484The output gives a line for each selected agenda item. Each
2485item is a list of comma-separated values, like this:
2486
2487category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2488
2489category The category of the item
2490head The headline, without TODO kwd, TAGS and PRIORITY
2491type The type of the agenda entry, can be
2492 todo selected in TODO match
2493 tagsmatch selected in tags match
2494 diary imported from diary
2495 deadline a deadline on given date
2496 scheduled scheduled on given date
2497 timestamp entry has timestamp on given date
2498 closed entry was closed on given date
2499 upcoming-deadline warning about deadline
2500 past-scheduled forwarded scheduled item
2501 block entry has date block including g. date
2502todo The todo keyword, if any
2503tags All tags including inherited ones, separated by colons
2504date The relevant date, like 2007-2-14
2505time The time, like 15:00-16:50
2506extra Sting with extra planning info
2507priority-l The priority letter if any was given
2508priority-n The computed numerical priority
2509agenda-day The day in the agenda where this is listed"
2510
2511 (let (pars)
2512 (while parameters
2513 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2514 (push (list 'org-agenda-remove-tags t) pars)
2515 (if (> (length cmd-key) 2)
2516 (eval (list 'let (nreverse pars)
2517 (list 'org-tags-view nil cmd-key)))
2518 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2519 (set-buffer org-agenda-buffer-name)
2520 (let* ((lines (org-split-string (buffer-string) "\n"))
2521 line)
2522 (while (setq line (pop lines))
2523 (catch 'next
2524 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2525 (setq org-agenda-info
2526 (org-fix-agenda-info (text-properties-at 0 line)))
2527 (princ
2528 (org-encode-for-stdout
2529 (mapconcat 'org-agenda-export-csv-mapper
ed21c5c8 2530 '(org-category txt type todo tags date time extra
20908596
CD
2531 priority-letter priority agenda-day)
2532 ",")))
2533 (princ "\n"))))))
2534
2535(defun org-fix-agenda-info (props)
86fbb8ca
CD
2536 "Make sure all properties on an agenda item have a canonical form.
2537This ensures the export commands can easily use it."
20908596
CD
2538 (let (tmp re)
2539 (when (setq tmp (plist-get props 'tags))
2540 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2541 (when (setq tmp (plist-get props 'date))
2542 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2543 (let ((calendar-date-display-form '(year "-" month "-" day)))
2544 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2545
2546 (setq tmp (calendar-date-string tmp)))
2547 (setq props (plist-put props 'date tmp)))
2548 (when (setq tmp (plist-get props 'day))
2549 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2550 (let ((calendar-date-display-form '(year "-" month "-" day)))
2551 (setq tmp (calendar-date-string tmp)))
2552 (setq props (plist-put props 'day tmp))
2553 (setq props (plist-put props 'agenda-day tmp)))
2554 (when (setq tmp (plist-get props 'txt))
2555 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2556 (plist-put props 'priority-letter (match-string 1 tmp))
2557 (setq tmp (replace-match "" t t tmp)))
2558 (when (and (setq re (plist-get props 'org-todo-regexp))
2559 (setq re (concat "\\`\\.*" re " ?"))
2560 (string-match re tmp))
2561 (plist-put props 'todo (match-string 1 tmp))
2562 (setq tmp (replace-match "" t t tmp)))
2563 (plist-put props 'txt tmp)))
2564 props)
2565
2566(defun org-agenda-export-csv-mapper (prop)
2567 (let ((res (plist-get org-agenda-info prop)))
2568 (setq res
2569 (cond
2570 ((not res) "")
2571 ((stringp res) res)
2572 (t (prin1-to-string res))))
2573 (while (string-match "," res)
2574 (setq res (replace-match ";" t t res)))
2575 (org-trim res)))
2576
2577
2578;;;###autoload
2579(defun org-store-agenda-views (&rest parameters)
2580 (interactive)
2581 (eval (list 'org-batch-store-agenda-views)))
2582
2583;; FIXME, why is this a macro?????
2584;;;###autoload
2585(defmacro org-batch-store-agenda-views (&rest parameters)
2586 "Run all custom agenda commands that have a file argument."
2587 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2588 (pop-up-frames nil)
2589 (dir default-directory)
2c3ad40d 2590 pars cmd thiscmdkey files opts cmd-or-set)
20908596
CD
2591 (while parameters
2592 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2593 (setq pars (reverse pars))
2594 (save-window-excursion
2595 (while cmds
2596 (setq cmd (pop cmds)
2597 thiscmdkey (car cmd)
2c3ad40d
CD
2598 cmd-or-set (nth 2 cmd)
2599 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2600 files (nth (if (listp cmd-or-set) 4 5) cmd))
20908596
CD
2601 (if (stringp files) (setq files (list files)))
2602 (when files
2603 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2604 (list 'org-agenda nil thiscmdkey)))
2605 (set-buffer org-agenda-buffer-name)
2606 (while files
2607 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2608 (list 'org-write-agenda
c8d0cf5c 2609 (expand-file-name (pop files) dir) nil t))))
20908596
CD
2610 (and (get-buffer org-agenda-buffer-name)
2611 (kill-buffer org-agenda-buffer-name)))))))
2612
8d642074
CD
2613(defun org-agenda-mark-header-line (pos)
2614 "Mark the line at POS as an agenda structure header."
2615 (save-excursion
2616 (goto-char pos)
2617 (put-text-property (point-at-bol) (point-at-eol)
2618 'org-agenda-structural-header t)
2619 (when org-agenda-title-append
2620 (put-text-property (point-at-bol) (point-at-eol)
2621 'org-agenda-title-append org-agenda-title-append))))
2622
8bfe682a 2623(defvar org-mobile-creating-agendas)
c8d0cf5c 2624(defun org-write-agenda (file &optional open nosettings)
20908596
CD
2625 "Write the current buffer (an agenda view) as a file.
2626Depending on the extension of the file name, plain text (.txt),
2627HTML (.html or .htm) or Postscript (.ps) is produced.
2628If the extension is .ics, run icalendar export over all files used
2629to construct the agenda and limit the export to entries listed in the
2630agenda now.
8bfe682a 2631With prefix argument OPEN, open the new file immediately.
20908596
CD
2632If NOSETTINGS is given, do not scope the settings of
2633`org-agenda-exporter-settings' into the export commands. This is used when
2634the settings have already been scoped and we do not wish to overrule other,
2635higher priority settings."
c8d0cf5c 2636 (interactive "FWrite agenda to file: \nP")
20908596
CD
2637 (if (not (file-writable-p file))
2638 (error "Cannot write agenda to file %s" file))
20908596 2639 (org-let (if nosettings nil org-agenda-exporter-settings)
afe98dfa 2640 '(save-excursion
20908596 2641 (save-window-excursion
93b62de8 2642 (org-agenda-mark-filtered-text)
8bfe682a 2643 (let ((bs (copy-sequence (buffer-string))) beg)
93b62de8
CD
2644 (org-agenda-unmark-filtered-text)
2645 (with-temp-buffer
afe98dfa
CD
2646 (rename-buffer "Agenda View" t)
2647 (set-buffer-modified-p nil)
20908596 2648 (insert bs)
93b62de8
CD
2649 (org-agenda-remove-marked-text 'org-filtered)
2650 (while (setq beg (text-property-any (point-min) (point-max)
2651 'org-filtered t))
2652 (delete-region
2653 beg (or (next-single-property-change beg 'org-filtered)
2654 (point-max))))
c8d0cf5c 2655 (run-hooks 'org-agenda-before-write-hook)
93b62de8 2656 (cond
8bfe682a
CD
2657 ((org-bound-and-true-p org-mobile-creating-agendas)
2658 (org-mobile-write-agenda-for-mobile file))
93b62de8 2659 ((string-match "\\.html?\\'" file)
afe98dfa 2660 (require 'htmlize)
93b62de8 2661 (set-buffer (htmlize-buffer (current-buffer)))
ff4be292 2662
93b62de8
CD
2663 (when (and org-agenda-export-html-style
2664 (string-match "<style>" org-agenda-export-html-style))
2665 ;; replace <style> section with org-agenda-export-html-style
2666 (goto-char (point-min))
2667 (kill-region (- (search-forward "<style") 6)
2668 (search-forward "</style>"))
2669 (insert org-agenda-export-html-style))
2670 (write-file file)
2671 (kill-buffer (current-buffer))
2672 (message "HTML written to %s" file))
2673 ((string-match "\\.ps\\'" file)
c8d0cf5c 2674 (require 'ps-print)
afe98dfa 2675 (ps-print-buffer-with-faces file)
93b62de8 2676 (message "Postscript written to %s" file))
c8d0cf5c
CD
2677 ((string-match "\\.pdf\\'" file)
2678 (require 'ps-print)
afe98dfa
CD
2679 (ps-print-buffer-with-faces
2680 (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c
CD
2681 (call-process "ps2pdf" nil nil nil
2682 (expand-file-name
2683 (concat (file-name-sans-extension file) ".ps"))
2684 (expand-file-name file))
afe98dfa 2685 (delete-file (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c 2686 (message "PDF written to %s" file))
93b62de8 2687 ((string-match "\\.ics\\'" file)
c8d0cf5c 2688 (require 'org-icalendar)
93b62de8
CD
2689 (let ((org-agenda-marker-table
2690 (org-create-marker-find-array
2691 (org-agenda-collect-markers)))
2692 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2693 (org-combined-agenda-icalendar-file file))
2694 (apply 'org-export-icalendar 'combine
2695 (org-agenda-files nil 'ifmode))))
2696 (t
2697 (let ((bs (buffer-string)))
2698 (find-file file)
2699 (erase-buffer)
2700 (insert bs)
2701 (save-buffer 0)
2702 (kill-buffer (current-buffer))
2703 (message "Plain text written to %s" file))))))))
c8d0cf5c
CD
2704 (set-buffer org-agenda-buffer-name))
2705 (when open (org-open-file file)))
2706
93b62de8
CD
2707(defvar org-agenda-filter-overlays nil)
2708
2709(defun org-agenda-mark-filtered-text ()
2710 "Mark all text hidden by filtering with a text property."
2711 (let ((inhibit-read-only t))
2712 (mapc
2713 (lambda (o)
86fbb8ca 2714 (when (equal (overlay-buffer o) (current-buffer))
93b62de8 2715 (put-text-property
86fbb8ca 2716 (overlay-start o) (overlay-end o)
93b62de8
CD
2717 'org-filtered t)))
2718 org-agenda-filter-overlays)))
2719
2720(defun org-agenda-unmark-filtered-text ()
2721 "Remove the filtering text property."
2722 (let ((inhibit-read-only t))
2723 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2724
2725(defun org-agenda-remove-marked-text (property &optional value)
2726 "Delete all text marked with VALUE of PROPERTY.
2727VALUE defaults to t."
2728 (let (beg)
2729 (setq value (or value t))
2730 (while (setq beg (text-property-any (point-min) (point-max)
2731 property value))
2732 (delete-region
2733 beg (or (next-single-property-change beg 'org-filtered)
2734 (point-max))))))
20908596 2735
c8d0cf5c
CD
2736(defun org-agenda-add-entry-text ()
2737 "Add entry text to agenda lines.
2738This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2739entry text following headings shown in the agenda.
2740Drawers will be excluded, also the line with scheduling/deadline info."
8bfe682a
CD
2741 (when (and (> org-agenda-add-entry-text-maxlines 0)
2742 (not (org-bound-and-true-p org-mobile-creating-agendas)))
54a0dee5 2743 (let (m txt)
c8d0cf5c
CD
2744 (goto-char (point-min))
2745 (while (not (eobp))
8d642074 2746 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
c8d0cf5c 2747 (beginning-of-line 2)
54a0dee5 2748 (setq txt (org-agenda-get-some-entry-text
8d642074 2749 m org-agenda-add-entry-text-maxlines " > "))
c8d0cf5c 2750 (end-of-line 1)
afe98dfa
CD
2751 (if (string-match "\\S-" txt)
2752 (insert "\n" txt)
2753 (or (eobp) (forward-char 1))))))))
c8d0cf5c 2754
8d642074
CD
2755(defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2756 &rest keep)
54a0dee5 2757 "Extract entry text from MARKER, at most N-LINES lines.
8d642074
CD
2758This will ignore drawers etc, just get the text.
2759If INDENT is given, prefix every line with this string. If KEEP is
8bfe682a 2760given, it is a list of symbols, defining stuff that should not be
8d642074 2761removed from the entry content. Currently only `planning' is allowed here."
54a0dee5
CD
2762 (let (txt drawer-re kwd-time-re ind)
2763 (save-excursion
2764 (with-current-buffer (marker-buffer marker)
2765 (if (not (org-mode-p))
2766 (setq txt "")
2767 (save-excursion
2768 (save-restriction
2769 (widen)
2770 (goto-char marker)
8d642074 2771 (end-of-line 1)
54a0dee5 2772 (setq txt (buffer-substring
8d642074 2773 (min (1+ (point)) (point-max))
54a0dee5
CD
2774 (progn (outline-next-heading) (point)))
2775 drawer-re org-drawer-regexp
2776 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2777 ".*\n?"))
2778 (with-temp-buffer
2779 (insert txt)
2780 (when org-agenda-add-entry-text-descriptive-links
2781 (goto-char (point-min))
2782 (while (org-activate-bracket-links (point-max))
2783 (add-text-properties (match-beginning 0) (match-end 0)
2784 '(face org-link))))
2785 (goto-char (point-min))
2786 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2787 (set-text-properties (match-beginning 0) (match-end 0)
2788 nil))
2789 (goto-char (point-min))
2790 (while (re-search-forward drawer-re nil t)
2791 (delete-region
2792 (match-beginning 0)
2793 (progn (re-search-forward
2794 "^[ \t]*:END:.*\n?" nil 'move)
2795 (point))))
8d642074
CD
2796 (unless (member 'planning keep)
2797 (goto-char (point-min))
2798 (while (re-search-forward kwd-time-re nil t)
2799 (replace-match "")))
54a0dee5 2800 (goto-char (point-min))
8d642074
CD
2801 (when org-agenda-entry-text-exclude-regexps
2802 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2803 (while (setq re (pop re-list))
2804 (goto-char (point-min))
2805 (while (re-search-forward re nil t)
2806 (replace-match "")))))
2807 (goto-char (point-max))
2808 (skip-chars-backward " \t\n")
2809 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2810
2811 ;; find and remove min common indentation
54a0dee5
CD
2812 (goto-char (point-min))
2813 (untabify (point-min) (point-max))
2814 (setq ind (org-get-indentation))
2815 (while (not (eobp))
2816 (unless (looking-at "[ \t]*$")
2817 (setq ind (min ind (org-get-indentation))))
2818 (beginning-of-line 2))
2819 (goto-char (point-min))
2820 (while (not (eobp))
2821 (unless (looking-at "[ \t]*$")
2822 (move-to-column ind)
2823 (delete-region (point-at-bol) (point)))
2824 (beginning-of-line 2))
8d642074
CD
2825
2826 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2827
54a0dee5 2828 (goto-char (point-min))
8d642074
CD
2829 (when indent
2830 (while (and (not (eobp)) (re-search-forward "^" nil t))
2831 (replace-match indent t t)))
54a0dee5
CD
2832 (goto-char (point-min))
2833 (while (looking-at "[ \t]*\n") (replace-match ""))
2834 (goto-char (point-max))
2835 (when (> (org-current-line)
2836 n-lines)
2837 (org-goto-line (1+ n-lines))
2838 (backward-char 1))
2839 (setq txt (buffer-substring (point-min) (point)))))))))
2840 txt))
2841
20908596
CD
2842(defun org-agenda-collect-markers ()
2843 "Collect the markers pointing to entries in the agenda buffer."
2844 (let (m markers)
2845 (save-excursion
2846 (goto-char (point-min))
2847 (while (not (eobp))
8d642074
CD
2848 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2849 (org-get-at-bol 'org-marker)))
20908596
CD
2850 (push m markers))
2851 (beginning-of-line 2)))
2852 (nreverse markers)))
2853
2854(defun org-create-marker-find-array (marker-list)
2855 "Create a alist of files names with all marker positions in that file."
2856 (let (f tbl m a p)
2857 (while (setq m (pop marker-list))
2858 (setq p (marker-position m)
2859 f (buffer-file-name (or (buffer-base-buffer
2860 (marker-buffer m))
2861 (marker-buffer m))))
2862 (if (setq a (assoc f tbl))
2863 (push (marker-position m) (cdr a))
2864 (push (list f p) tbl)))
2865 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2866 tbl)))
2867
33306645 2868(defvar org-agenda-marker-table nil) ; dynamically scoped parameter
20908596
CD
2869(defun org-check-agenda-marker-table ()
2870 "Check of the current entry is on the marker list."
2871 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2872 a)
2873 (and (setq a (assoc file org-agenda-marker-table))
2874 (save-match-data
2875 (save-excursion
2876 (org-back-to-heading t)
2877 (member (point) (cdr a)))))))
2878
2879(defun org-check-for-org-mode ()
2880 "Make sure current buffer is in org-mode. Error if not."
2881 (or (org-mode-p)
f924a367 2882 (error "Cannot execute org-mode agenda command on buffer in %s"
20908596
CD
2883 major-mode)))
2884
2885(defun org-fit-agenda-window ()
2886 "Fit the window to the buffer size."
2887 (and (memq org-agenda-window-setup '(reorganize-frame))
2888 (fboundp 'fit-window-to-buffer)
93b62de8 2889 (org-fit-window-to-buffer
20908596
CD
2890 nil
2891 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2892 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2893
2894;;; Agenda prepare and finalize
2895
33306645 2896(defvar org-agenda-multi nil) ; dynamically scoped
20908596
CD
2897(defvar org-agenda-buffer-name "*Org Agenda*")
2898(defvar org-pre-agenda-window-conf nil)
2899(defvar org-agenda-columns-active nil)
2900(defvar org-agenda-name nil)
71d35b24 2901(defvar org-agenda-filter nil)
acedf35c 2902(defvar org-agenda-filter-while-redo nil)
c8d0cf5c
CD
2903(defvar org-agenda-filter-preset nil
2904 "A preset of the tags filter used for secondary agenda filtering.
86fbb8ca 2905This must be a list of strings, each string must be a single tag preceded
c8d0cf5c
CD
2906by \"+\" or \"-\".
2907This variable should not be set directly, but agenda custom commands can
afe98dfa
CD
2908bind it in the options section. The preset filter is a global property of
2909the entire agenda view. In a block agenda, it will not work reliably to
2910define a filter for one of the individual blocks. You need to set it in
2911the global options and expect it to be applied to the entire view.")
c8d0cf5c 2912
20908596
CD
2913(defun org-prepare-agenda (&optional name)
2914 (setq org-todo-keywords-for-agenda nil)
2915 (setq org-done-keywords-for-agenda nil)
8d642074 2916 (setq org-drawers-for-agenda nil)
86fbb8ca
CD
2917 (unless org-agenda-persistent-filter
2918 (setq org-agenda-filter nil))
c8d0cf5c 2919 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
20908596
CD
2920 (if org-agenda-multi
2921 (progn
2922 (setq buffer-read-only nil)
2923 (goto-char (point-max))
2924 (unless (or (bobp) org-agenda-compact-blocks)
0bd48b37
CD
2925 (insert "\n"
2926 (if (stringp org-agenda-block-separator)
2927 org-agenda-block-separator
2928 (make-string (window-width) org-agenda-block-separator))
2929 "\n"))
20908596
CD
2930 (narrow-to-region (point) (point-max)))
2931 (org-agenda-reset-markers)
2932 (setq org-agenda-contributing-files nil)
2933 (setq org-agenda-columns-active nil)
2c3ad40d 2934 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
20908596
CD
2935 (setq org-todo-keywords-for-agenda
2936 (org-uniquify org-todo-keywords-for-agenda))
2937 (setq org-done-keywords-for-agenda
2938 (org-uniquify org-done-keywords-for-agenda))
8d642074 2939 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
20908596
CD
2940 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2941 (awin (get-buffer-window abuf)))
2942 (cond
2943 ((equal (current-buffer) abuf) nil)
2944 (awin (select-window awin))
2945 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2946 ((equal org-agenda-window-setup 'current-window)
2947 (switch-to-buffer abuf))
2948 ((equal org-agenda-window-setup 'other-window)
2949 (org-switch-to-buffer-other-window abuf))
2950 ((equal org-agenda-window-setup 'other-frame)
8d642074 2951 (switch-to-buffer-other-frame abuf))
20908596
CD
2952 ((equal org-agenda-window-setup 'reorganize-frame)
2953 (delete-other-windows)
afe98dfa
CD
2954 (org-switch-to-buffer-other-window abuf)))
2955 ;; additional test in case agenda is invoked from within agenda
2956 ;; buffer via elisp link
2957 (unless (equal (current-buffer) abuf)
2958 (switch-to-buffer abuf)))
20908596
CD
2959 (setq buffer-read-only nil)
2960 (let ((inhibit-read-only t)) (erase-buffer))
2961 (org-agenda-mode)
2962 (and name (not org-agenda-name)
2963 (org-set-local 'org-agenda-name name)))
2964 (setq buffer-read-only nil))
2965
2966(defun org-finalize-agenda ()
2967 "Finishing touch for the agenda buffer, called just before displaying it."
2968 (unless org-agenda-multi
2969 (save-excursion
2970 (let ((inhibit-read-only t))
2971 (goto-char (point-min))
2972 (while (org-activate-bracket-links (point-max))
2973 (add-text-properties (match-beginning 0) (match-end 0)
2974 '(face org-link)))
2975 (org-agenda-align-tags)
2976 (unless org-agenda-with-colors
2977 (remove-text-properties (point-min) (point-max) '(face nil))))
33306645
CD
2978 (if (and (boundp 'org-agenda-overriding-columns-format)
2979 org-agenda-overriding-columns-format)
2980 (org-set-local 'org-agenda-overriding-columns-format
2981 org-agenda-overriding-columns-format))
20908596
CD
2982 (if (and (boundp 'org-agenda-view-columns-initially)
2983 org-agenda-view-columns-initially)
2984 (org-agenda-columns))
2985 (when org-agenda-fontify-priorities
c8d0cf5c 2986 (org-agenda-fontify-priorities))
d6685abc
CD
2987 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2988 (org-agenda-dim-blocked-tasks))
54a0dee5
CD
2989 (org-agenda-mark-clocking-task)
2990 (when org-agenda-entry-text-mode
2991 (org-agenda-entry-text-hide)
f924a367 2992 (org-agenda-entry-text-show))
8bfe682a
CD
2993 (if (functionp 'org-habit-insert-consistency-graphs)
2994 (org-habit-insert-consistency-graphs))
20908596 2995 (run-hooks 'org-finalize-agenda-hook)
8d642074 2996 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
86fbb8ca 2997 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
c8d0cf5c 2998 (org-agenda-filter-apply org-agenda-filter))
20908596
CD
2999 )))
3000
54a0dee5
CD
3001(defun org-agenda-mark-clocking-task ()
3002 "Mark the current clock entry in the agenda if it is present."
3003 (mapc (lambda (o)
86fbb8ca
CD
3004 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3005 (delete-overlay o)))
3006 (overlays-in (point-min) (point-max)))
54a0dee5
CD
3007 (when (marker-buffer org-clock-hd-marker)
3008 (save-excursion
3009 (goto-char (point-min))
3010 (let (s ov)
3011 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3012 (goto-char s)
8d642074 3013 (when (equal (org-get-at-bol 'org-hd-marker)
54a0dee5 3014 org-clock-hd-marker)
86fbb8ca
CD
3015 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3016 (overlay-put ov 'type 'org-agenda-clocking)
3017 (overlay-put ov 'face 'org-agenda-clocking)
3018 (overlay-put ov 'help-echo
54a0dee5
CD
3019 "The clock is running in this item")))))))
3020
c8d0cf5c 3021(defun org-agenda-fontify-priorities ()
20908596
CD
3022 "Make highest priority lines bold, and lowest italic."
3023 (interactive)
86fbb8ca
CD
3024 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3025 (delete-overlay o)))
3026 (overlays-in (point-min) (point-max)))
20908596
CD
3027 (save-excursion
3028 (let ((inhibit-read-only t)
3029 b e p ov h l)
3030 (goto-char (point-min))
3031 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3032 (setq h (or (get-char-property (point) 'org-highest-priority)
3033 org-highest-priority)
3034 l (or (get-char-property (point) 'org-lowest-priority)
3035 org-lowest-priority)
3036 p (string-to-char (match-string 1))
c8d0cf5c
CD
3037 b (match-beginning 0)
3038 e (if (eq org-agenda-fontify-priorities 'cookies)
3039 (match-end 0)
3040 (point-at-eol))
86fbb8ca
CD
3041 ov (make-overlay b e))
3042 (overlay-put
20908596 3043 ov 'face
ed21c5c8
CD
3044 (cond ((org-face-from-face-or-color
3045 'priority nil
3046 (cdr (assoc p org-priority-faces))))
c8d0cf5c 3047 ((and (listp org-agenda-fontify-priorities)
ed21c5c8
CD
3048 (org-face-from-face-or-color
3049 'priority nil
3050 (cdr (assoc p org-agenda-fontify-priorities)))))
20908596
CD
3051 ((equal p l) 'italic)
3052 ((equal p h) 'bold)))
86fbb8ca 3053 (overlay-put ov 'org-type 'org-priority)))))
20908596 3054
d6685abc
CD
3055(defun org-agenda-dim-blocked-tasks ()
3056 "Dim currently blocked TODO's in the agenda display."
86fbb8ca
CD
3057 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3058 (delete-overlay o)))
3059 (overlays-in (point-min) (point-max)))
d6685abc
CD
3060 (save-excursion
3061 (let ((inhibit-read-only t)
72d06d81 3062 (org-depend-tag-blocked nil)
d6685abc 3063 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
c8d0cf5c
CD
3064 org-blocked-by-checkboxes
3065 invis1 b e p ov h l)
d6685abc
CD
3066 (goto-char (point-min))
3067 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3068 (and pos (goto-char (1+ pos))))
c8d0cf5c 3069 (setq org-blocked-by-checkboxes nil invis1 invis)
8d642074 3070 (let ((marker (org-get-at-bol 'org-hd-marker)))
d6685abc
CD
3071 (when (and marker
3072 (not (with-current-buffer (marker-buffer marker)
3073 (save-excursion
3074 (goto-char marker)
c8d0cf5c
CD
3075 (if (org-entry-get nil "NOBLOCKING")
3076 t ;; Never block this entry
3077 (run-hook-with-args-until-failure
3078 'org-blocker-hook
3079 (list :type 'todo-state-change
3080 :position marker
3081 :from 'todo
3082 :to 'done)))))))
3083 (if org-blocked-by-checkboxes (setq invis1 nil))
53e31a31
CD
3084 (setq b (if invis1
3085 (max (point-min) (1- (point-at-bol)))
3086 (point-at-bol))
d6685abc 3087 e (point-at-eol)
86fbb8ca 3088 ov (make-overlay b e))
c8d0cf5c 3089 (if invis1
86fbb8ca
CD
3090 (overlay-put ov 'invisible t)
3091 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3092 (overlay-put ov 'org-type 'org-blocked-todo)))))))
20908596
CD
3093
3094(defvar org-agenda-skip-function nil
3095 "Function to be called at each match during agenda construction.
3096If this function returns nil, the current match should not be skipped.
3097Otherwise, the function must return a position from where the search
3098should be continued.
3099This may also be a Lisp form, it will be evaluated.
3100Never set this variable using `setq' or so, because then it will apply
3101to all future agenda commands. Instead, bind it with `let' to scope
3102it dynamically into the agenda-constructing command. A good way to set
86fbb8ca 3103it is through options in `org-agenda-custom-commands'.")
20908596
CD
3104
3105(defun org-agenda-skip ()
3106 "Throw to `:skip' in places that should be skipped.
3107Also moves point to the end of the skipped region, so that search can
3108continue from there."
3109 (let ((p (point-at-bol)) to fp)
2c3ad40d 3110 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
20908596
CD
3111 (get-text-property p :org-archived)
3112 (org-end-of-subtree t)
3113 (throw :skip t))
b349f79f
CD
3114 (and org-agenda-skip-comment-trees
3115 (get-text-property p :org-comment)
20908596
CD
3116 (org-end-of-subtree t)
3117 (throw :skip t))
3118 (if (equal (char-after p) ?#) (throw :skip t))
3119 (when (and (or (setq fp (functionp org-agenda-skip-function))
3120 (consp org-agenda-skip-function))
3121 (setq to (save-excursion
3122 (save-match-data
3123 (if fp
3124 (funcall org-agenda-skip-function)
3125 (eval org-agenda-skip-function))))))
3126 (goto-char to)
3127 (throw :skip t))))
3128
3129(defvar org-agenda-markers nil
3130 "List of all currently active markers created by `org-agenda'.")
54a0dee5 3131(defvar org-agenda-last-marker-time (org-float-time)
20908596
CD
3132 "Creation time of the last agenda marker.")
3133
3134(defun org-agenda-new-marker (&optional pos)
3135 "Return a new agenda marker.
3136Org-mode keeps a list of these markers and resets them when they are
3137no longer in use."
3138 (let ((m (copy-marker (or pos (point)))))
54a0dee5 3139 (setq org-agenda-last-marker-time (org-float-time))
20908596
CD
3140 (push m org-agenda-markers)
3141 m))
3142
3143(defun org-agenda-reset-markers ()
3144 "Reset markers created by `org-agenda'."
3145 (while org-agenda-markers
3146 (move-marker (pop org-agenda-markers) nil)))
3147
b349f79f
CD
3148(defun org-agenda-save-markers-for-cut-and-paste (beg end)
3149 "Save relative positions of markers in region."
3150 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3151 org-agenda-markers))
3152
54a0dee5
CD
3153;;; Entry text mode
3154
3155(defun org-agenda-entry-text-show-here ()
8bfe682a 3156 "Add some text from the entry as context to the current line."
54a0dee5 3157 (let (m txt o)
8d642074 3158 (setq m (org-get-at-bol 'org-hd-marker))
54a0dee5
CD
3159 (unless (marker-buffer m)
3160 (error "No marker points to an entry here"))
3161 (setq txt (concat "\n" (org-no-properties
3162 (org-agenda-get-some-entry-text
8d642074 3163 m org-agenda-entry-text-maxlines " > "))))
54a0dee5 3164 (when (string-match "\\S-" txt)
86fbb8ca
CD
3165 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3166 (overlay-put o 'evaporate t)
3167 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3168 (overlay-put o 'after-string txt))))
54a0dee5
CD
3169
3170(defun org-agenda-entry-text-show ()
3171 "Add entry context for all agenda lines."
3172 (interactive)
3173 (save-excursion
3174 (goto-char (point-max))
3175 (beginning-of-line 1)
3176 (while (not (bobp))
8d642074 3177 (when (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
3178 (org-agenda-entry-text-show-here))
3179 (beginning-of-line 0))))
3180
3181(defun org-agenda-entry-text-hide ()
3182 "Remove any shown entry context."
3183 (delq nil
3184 (mapcar (lambda (o)
86fbb8ca 3185 (if (eq (overlay-get o 'org-overlay-type)
54a0dee5 3186 'agenda-entry-content)
86fbb8ca
CD
3187 (progn (delete-overlay o) t)))
3188 (overlays-in (point-min) (point-max)))))
54a0dee5 3189
acedf35c
CD
3190(defun org-agenda-get-day-face (date)
3191 "Return the face DATE should be displayed with."
3192 (or (and (functionp org-agenda-day-face-function)
3193 (funcall org-agenda-day-face-function date))
3194 (cond ((org-agenda-todayp date)
3195 'org-agenda-date-today)
3196 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3197 'org-agenda-date-weekend)
3198 (t 'org-agenda-date))))
3199
20908596
CD
3200;;; Agenda timeline
3201
3202(defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3203
3204(defun org-timeline (&optional include-all)
3205 "Show a time-sorted view of the entries in the current org file.
3206Only entries with a time stamp of today or later will be listed. With
3207\\[universal-argument] prefix, all unfinished TODO items will also be shown,
3208under the current date.
3209If the buffer contains an active region, only check the region for
3210dates."
3211 (interactive "P")
20908596
CD
3212 (org-compile-prefix-format 'timeline)
3213 (org-set-sorting-strategy 'timeline)
3214 (let* ((dopast t)
3215 (dotodo include-all)
3216 (doclosed org-agenda-show-log)
afe98dfa
CD
3217 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3218 (current-buffer))))
20908596
CD
3219 (date (calendar-current-date))
3220 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3221 (end (if (org-region-active-p) (region-end) (point-max)))
3222 (day-numbers (org-get-all-dates beg end 'no-ranges
3223 t doclosed ; always include today
3224 org-timeline-show-empty-dates))
3225 (org-deadline-warning-days 0)
3226 (org-agenda-only-exact-dates t)
acedf35c 3227 (today (org-today))
20908596
CD
3228 (past t)
3229 args
acedf35c 3230 s e rtn d emptyp)
20908596
CD
3231 (setq org-agenda-redo-command
3232 (list 'progn
3233 (list 'org-switch-to-buffer-other-window (current-buffer))
3234 (list 'org-timeline (list 'quote include-all))))
3235 (if (not dopast)
3236 ;; Remove past dates from the list of dates.
3237 (setq day-numbers (delq nil (mapcar (lambda(x)
3238 (if (>= x today) x nil))
3239 day-numbers))))
afe98dfa 3240 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
20908596
CD
3241 (if doclosed (push :closed args))
3242 (push :timestamp args)
3243 (push :deadline args)
3244 (push :scheduled args)
3245 (push :sexp args)
3246 (if dotodo (push :todo args))
8d642074
CD
3247 (insert "Timeline of file " entry "\n")
3248 (add-text-properties (point-min) (point)
3249 (list 'face 'org-agenda-structure))
3250 (org-agenda-mark-header-line (point-min))
20908596
CD
3251 (while (setq d (pop day-numbers))
3252 (if (and (listp d) (eq (car d) :omitted))
3253 (progn
3254 (setq s (point))
3255 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3256 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3257 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3258 (if (and (>= d today)
3259 dopast
3260 past)
3261 (progn
3262 (setq past nil)
3263 (insert (make-string 79 ?-) "\n")))
acedf35c 3264 (setq date (calendar-gregorian-from-absolute d))
20908596
CD
3265 (setq s (point))
3266 (setq rtn (and (not emptyp)
3267 (apply 'org-agenda-get-day-entries entry
3268 date args)))
3269 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3270 (progn
3271 (insert
3272 (if (stringp org-agenda-format-date)
3273 (format-time-string org-agenda-format-date
3274 (org-time-from-absolute date))
3275 (funcall org-agenda-format-date date))
3276 "\n")
3277 (put-text-property s (1- (point)) 'face
acedf35c 3278 (org-agenda-get-day-face date))
20908596 3279 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3280 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596
CD
3281 (if (equal d today)
3282 (put-text-property s (1- (point)) 'org-today t))
3283 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3284 (put-text-property s (1- (point)) 'day d)))))
3285 (goto-char (point-min))
3286 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3287 (point-min)))
3288 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3289 (org-finalize-agenda)
3290 (setq buffer-read-only t)))
3291
3292(defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3293 "Return a list of all relevant day numbers from BEG to END buffer positions.
3294If NO-RANGES is non-nil, include only the start and end dates of a range,
3295not every single day in the range. If FORCE-TODAY is non-nil, make
3296sure that TODAY is included in the list. If INACTIVE is non-nil, also
3297inactive time stamps (those in square brackets) are included.
3298When EMPTY is non-nil, also include days without any entries."
3299 (let ((re (concat
3300 (if pre-re pre-re "")
3301 (if inactive org-ts-regexp-both org-ts-regexp)))
3302 dates dates1 date day day1 day2 ts1 ts2)
3303 (if force-today
acedf35c 3304 (setq dates (list (org-today))))
20908596
CD
3305 (save-excursion
3306 (goto-char beg)
3307 (while (re-search-forward re end t)
3308 (setq day (time-to-days (org-time-string-to-time
3309 (substring (match-string 1) 0 10))))
3310 (or (memq day dates) (push day dates)))
3311 (unless no-ranges
3312 (goto-char beg)
3313 (while (re-search-forward org-tr-regexp end t)
3314 (setq ts1 (substring (match-string 1) 0 10)
3315 ts2 (substring (match-string 2) 0 10)
3316 day1 (time-to-days (org-time-string-to-time ts1))
3317 day2 (time-to-days (org-time-string-to-time ts2)))
3318 (while (< (setq day1 (1+ day1)) day2)
3319 (or (memq day1 dates) (push day1 dates)))))
3320 (setq dates (sort dates '<))
3321 (when empty
3322 (while (setq day (pop dates))
3323 (setq day2 (car dates))
3324 (push day dates1)
3325 (when (and day2 empty)
3326 (if (or (eq empty t)
3327 (and (numberp empty) (<= (- day2 day) empty)))
3328 (while (< (setq day (1+ day)) day2)
3329 (push (list day) dates1))
3330 (push (cons :omitted (- day2 day)) dates1))))
3331 (setq dates (nreverse dates1)))
3332 dates)))
3333
3334;;; Agenda Daily/Weekly
3335
3336(defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
c8d0cf5c
CD
3337(defvar org-agenda-start-day nil ; dynamically scoped parameter
3338"Custom commands can set this variable in the options section.")
20908596 3339(defvar org-agenda-last-arguments nil
86fbb8ca 3340 "The arguments of the previous call to `org-agenda'.")
20908596 3341(defvar org-starting-day nil) ; local variable in the agenda buffer
acedf35c
CD
3342(defvar org-agenda-current-span nil
3343 "The current span used in the agenda view.") ; local variable in the agenda buffer
20908596 3344(defvar org-include-all-loc nil) ; local variable
20908596 3345
ed21c5c8
CD
3346(defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3347 "List of types searched for when creating the daily/weekly agenda.
3348This variable is a list of symbols that controls the types of
3349items that appear in the daily/weekly agenda. Allowed symbols in this
3350list are are
3351
3352 :timestamp List items containing a date stamp or date range matching
3353 the selected date. This includes sexp entries in
3354 angular brackets.
3355
3356 :sexp List entries resulting from plain diary-like sexps.
3357
3358 :deadline List deadline due on that date. When the date is today,
3359 also list any deadlines past due, or due within
3360 `org-deadline-warning-days'. `:deadline' must appear before
3361 `:scheduled' if the setting of
3362 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3363 any effect.
3364
3365 :scheduled List all items which are scheduled for the given date.
3366 The diary for *today* also contains items which were
3367 scheduled earlier and are not yet marked DONE.
3368
3369By default, all four types are turned on.
3370
3371Never set this variable globally using `setq', because then it
3372will apply to all future agenda commands. Instead, bind it with
9b053e76 3373`let' to scope it dynamically into the agenda-constructing
ed21c5c8
CD
3374command. A good way to set it is through options in
3375`org-agenda-custom-commands'. For a more flexible (though
3376somewhat less efficient) way of determining what is included in
3377the daily/weekly agenda, see `org-agenda-skip-function'.")
3378
20908596 3379;;;###autoload
acedf35c 3380(defun org-agenda-list (&optional include-all start-day span)
20908596
CD
3381 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3382The view will be for the current day or week, but from the overview buffer
3383you will be able to go to other days/weeks.
3384
3385With one \\[universal-argument] prefix argument INCLUDE-ALL,
3386all unfinished TODO items will also be shown, before the agenda.
3387This feature is considered obsolete, please use the TODO list or a block
3388agenda instead.
3389
3390With a numeric prefix argument in an interactive call, the agenda will
acedf35c
CD
3391span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3392the number of days. SPAN defaults to `org-agenda-span'.
20908596
CD
3393
3394START-DAY defaults to TODAY, or to the most recent match for the weekday
3395given in `org-agenda-start-on-weekday'."
3396 (interactive "P")
3397 (if (and (integerp include-all) (> include-all 0))
acedf35c
CD
3398 (setq span include-all include-all nil))
3399 (setq start-day (or start-day org-agenda-start-day))
20908596
CD
3400 (if org-agenda-overriding-arguments
3401 (setq include-all (car org-agenda-overriding-arguments)
3402 start-day (nth 1 org-agenda-overriding-arguments)
acedf35c 3403 span (nth 2 org-agenda-overriding-arguments)))
20908596
CD
3404 (if (stringp start-day)
3405 ;; Convert to an absolute day number
3406 (setq start-day (time-to-days (org-read-date nil t start-day))))
acedf35c 3407 (setq org-agenda-last-arguments (list include-all start-day span))
20908596
CD
3408 (org-compile-prefix-format 'agenda)
3409 (org-set-sorting-strategy 'agenda)
acedf35c
CD
3410 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-ndays org-agenda-span)))
3411 (today (org-today))
3412 (sd (or start-day today))
3413 (ndays (org-agenda-span-to-ndays span sd))
3414 (org-agenda-start-on-weekday
3415 (if (eq ndays 7)
3416 org-agenda-start-on-weekday))
2c3ad40d 3417 (thefiles (org-agenda-files nil 'ifmode))
20908596 3418 (files thefiles)
20908596 3419 (start (if (or (null org-agenda-start-on-weekday)
acedf35c 3420 (< ndays 7))
20908596
CD
3421 sd
3422 (let* ((nt (calendar-day-of-week
3423 (calendar-gregorian-from-absolute sd)))
3424 (n1 org-agenda-start-on-weekday)
3425 (d (- nt n1)))
3426 (- sd (+ (if (< d 0) 7 0) d)))))
3427 (day-numbers (list start))
3428 (day-cnt 0)
3429 (inhibit-redisplay (not debug-on-error))
acedf35c
CD
3430 s e rtn rtnall file date d start-pos end-pos todayp
3431 clocktable-start clocktable-end filter)
20908596 3432 (setq org-agenda-redo-command
acedf35c
CD
3433 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3434 (dotimes (n (1- ndays))
3435 (push (1+ (car day-numbers)) day-numbers))
20908596
CD
3436 (setq day-numbers (nreverse day-numbers))
3437 (setq clocktable-start (car day-numbers)
3438 clocktable-end (1+ (or (org-last day-numbers) 0)))
3439 (org-prepare-agenda "Day/Week")
3440 (org-set-local 'org-starting-day (car day-numbers))
3441 (org-set-local 'org-include-all-loc include-all)
acedf35c 3442 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
20908596
CD
3443 (when (and (or include-all org-agenda-include-all-todo)
3444 (member today day-numbers))
3445 (setq files thefiles
3446 rtnall nil)
3447 (while (setq file (pop files))
3448 (catch 'nextfile
3449 (org-check-agenda-file file)
3450 (setq date (calendar-gregorian-from-absolute today)
3451 rtn (org-agenda-get-day-entries
3452 file date :todo))
3453 (setq rtnall (append rtnall rtn))))
3454 (when rtnall
8d642074 3455 (insert "All currently open TODO items:\n")
20908596 3456 (add-text-properties (point-min) (1- (point))
8d642074
CD
3457 (list 'face 'org-agenda-structure
3458 'short-heading "All TODO items"))
8bfe682a 3459 (org-agenda-mark-header-line (point-min))
20908596
CD
3460 (insert (org-finalize-agenda-entries rtnall) "\n")))
3461 (unless org-agenda-compact-blocks
3462 (let* ((d1 (car day-numbers))
3463 (d2 (org-last day-numbers))
3464 (w1 (org-days-to-iso-week d1))
3465 (w2 (org-days-to-iso-week d2)))
3466 (setq s (point))
c8d0cf5c
CD
3467 (if org-agenda-overriding-header
3468 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3469 nil 'face 'org-agenda-structure) "\n")
acedf35c 3470 (insert (org-agenda-span-name span)
c8d0cf5c
CD
3471 "-agenda"
3472 (if (< (- d2 d1) 350)
3473 (if (= w1 w2)
3474 (format " (W%02d)" w1)
3475 (format " (W%02d-W%02d)" w1 w2))
3476 "")
3477 ":\n")))
20908596 3478 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
8d642074
CD
3479 'org-date-line t))
3480 (org-agenda-mark-header-line s))
20908596
CD
3481 (while (setq d (pop day-numbers))
3482 (setq date (calendar-gregorian-from-absolute d)
20908596
CD
3483 s (point))
3484 (if (or (setq todayp (= d today))
3485 (and (not start-pos) (= d sd)))
3486 (setq start-pos (point))
3487 (if (and start-pos (not end-pos))
3488 (setq end-pos (point))))
3489 (setq files thefiles
3490 rtnall nil)
3491 (while (setq file (pop files))
3492 (catch 'nextfile
3493 (org-check-agenda-file file)
ed21c5c8
CD
3494 (let ((org-agenda-entry-types org-agenda-entry-types))
3495 (unless org-agenda-include-deadlines
3496 (setq org-agenda-entry-types
3497 (delq :deadline org-agenda-entry-types)))
3498 (cond
3499 ((eq org-agenda-show-log 'only)
3500 (setq rtn (org-agenda-get-day-entries
3501 file date :closed)))
3502 (org-agenda-show-log
3503 (setq rtn (apply 'org-agenda-get-day-entries
3504 file date
3505 (append '(:closed) org-agenda-entry-types))))
3506 (t
3507 (setq rtn (apply 'org-agenda-get-day-entries
3508 file date
3509 org-agenda-entry-types)))))
20908596
CD
3510 (setq rtnall (append rtnall rtn))))
3511 (if org-agenda-include-diary
c8d0cf5c 3512 (let ((org-agenda-search-headline-for-time t))
20908596
CD
3513 (require 'diary-lib)
3514 (setq rtn (org-get-entries-from-diary date))
3515 (setq rtnall (append rtnall rtn))))
3516 (if (or rtnall org-agenda-show-all-dates)
3517 (progn
3518 (setq day-cnt (1+ day-cnt))
3519 (insert
3520 (if (stringp org-agenda-format-date)
3521 (format-time-string org-agenda-format-date
3522 (org-time-from-absolute date))
3523 (funcall org-agenda-format-date date))
3524 "\n")
3525 (put-text-property s (1- (point)) 'face
acedf35c 3526 (org-agenda-get-day-face date))
20908596 3527 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3528 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596 3529 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
c8d0cf5c 3530 (when todayp
acedf35c 3531 (put-text-property s (1- (point)) 'org-today t))
20908596
CD
3532 (if rtnall (insert
3533 (org-finalize-agenda-entries
3534 (org-agenda-add-time-grid-maybe
acedf35c 3535 rtnall ndays todayp))
20908596
CD
3536 "\n"))
3537 (put-text-property s (1- (point)) 'day d)
3538 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3539 (when (and org-agenda-clockreport-mode clocktable-start)
2c3ad40d 3540 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
20908596
CD
3541 ;; the above line is to ensure the restricted range!
3542 (p org-agenda-clockreport-parameter-plist)
3543 tbl)
3544 (setq p (org-plist-delete p :block))
3545 (setq p (plist-put p :tstart clocktable-start))
3546 (setq p (plist-put p :tend clocktable-end))
3547 (setq p (plist-put p :scope 'agenda))
acedf35c
CD
3548 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3549 (setq filter (or org-agenda-filter-while-redo
3550 (get 'org-agenda-filter :preset-filter))))
3551 (setq p (plist-put p :tags (mapconcat (lambda (x)
3552 (if (string-match "[<>=]" x)
3553 ""
3554 x))
3555 filter ""))))
3556 (message "%s" (plist-get p :tags)) (sit-for 2)
20908596
CD
3557 (setq tbl (apply 'org-get-clocktable p))
3558 (insert tbl)))
3559 (goto-char (point-min))
c8d0cf5c 3560 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3561 (unless (and (pos-visible-in-window-p (point-min))
3562 (pos-visible-in-window-p (point-max)))
3563 (goto-char (1- (point-max)))
3564 (recenter -1)
3565 (if (not (pos-visible-in-window-p (or start-pos 1)))
3566 (progn
3567 (goto-char (or start-pos 1))
3568 (recenter 1))))
3569 (goto-char (or start-pos 1))
3570 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3571 (org-finalize-agenda)
3572 (setq buffer-read-only t)
3573 (message "")))
3574
3575(defun org-agenda-ndays-to-span (n)
acedf35c
CD
3576 "Return a span symbol for a span of N days, or N if none matches."
3577 (cond ((symbolp n) n)
3578 ((= n 1) 'day)
3579 ((= n 7) 'week)
3580 (t n)))
3581
3582(defun org-agenda-span-to-ndays (span start-day)
3583 "Return ndays from SPAN starting at START-DAY."
3584 (cond ((numberp span) span)
3585 ((eq span 'day) 1)
3586 ((eq span 'week) 7)
3587 ((eq span 'month)
3588 (let ((date (calendar-gregorian-from-absolute start-day)))
3589 (calendar-last-day-of-month (car date) (caddr date))))
3590 ((eq span 'year)
3591 (let ((date (calendar-gregorian-from-absolute start-day)))
3592 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3593
3594(defun org-agenda-span-name (span)
3595 "Return a SPAN name."
3596 (if (null span)
3597 ""
3598 (if (symbolp span)
3599 (capitalize (symbol-name span))
3600 (format "%d days" span))))
20908596
CD
3601
3602;;; Agenda word search
3603
3604(defvar org-agenda-search-history nil)
3605(defvar org-todo-only nil)
3606
3607(defvar org-search-syntax-table nil
3608 "Special syntax table for org-mode search.
3609In this table, we have single quotes not as word constituents, to
33306645 3610that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
20908596
CD
3611
3612(defun org-search-syntax-table ()
3613 (unless org-search-syntax-table
3614 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3615 (modify-syntax-entry ?' "." org-search-syntax-table)
3616 (modify-syntax-entry ?` "." org-search-syntax-table))
3617 org-search-syntax-table)
3618
ed21c5c8
CD
3619(defvar org-agenda-last-search-view-search-was-boolean nil)
3620
20908596
CD
3621;;;###autoload
3622(defun org-search-view (&optional todo-only string edit-at)
ed21c5c8 3623 "Show all entries that contain a phrase or words or regular expressions.
20908596
CD
3624
3625With optional prefix argument TODO-ONLY, only consider entries that are
3626TODO entries. The argument STRING can be used to pass a default search
3627string into this function. If EDIT-AT is non-nil, it means that the
3628user should get a chance to edit this string, with cursor at position
3629EDIT-AT.
3630
ed21c5c8
CD
3631The search string can be viewed either as a phrase that should be found as
3632is, or it can be broken into a number of snippets, each of which must match
3633in a Boolean way to select an entry. The default depends on the variable
3634`org-agenda-search-view-always-boolean'.
3635Even if this is turned off (the default) you can always switch to
86fbb8ca 3636Boolean search dynamically by preceding the first word with \"+\" or \"-\".
ed21c5c8
CD
3637
3638The default is a direct search of the whole phrase, where each space in
3639the search string can expand to an arbitrary amount of whitespace,
3640including newlines.
3641
3642If using a Boolean search, the search string is split on whitespace and
3643each snippet is searched separately, with logical AND to select an entry.
3644Words prefixed with a minus must *not* occur in the entry. Words without
3645a prefix or prefixed with a plus must occur in the entry. Matching is
3646case-insensitive. Words are enclosed by word delimiters (i.e. they must
3647match whole words, not parts of a word) if
3648`org-agenda-search-view-force-full-words' is set (default is nil).
3649
3650Boolean search snippets enclosed by curly braces are interpreted as
86fbb8ca 3651regular expressions that must or (when preceded with \"-\") must not
ed21c5c8 3652match in the entry. Snippets enclosed into double quotes will be taken
86fbb8ca 3653as a whole, to include whitespace.
ed21c5c8
CD
3654
3655- If the search string starts with an asterisk, search only in headlines.
3656- If (possibly after the leading star) the search string starts with an
3657 exclamation mark, this also means to look at TODO entries only, an effect
3658 that can also be achieved with a prefix argument.
3659- If (possibly after star and exclamation mark) the search string starts
3660 with a colon, this will mean that the (non-regexp) snippets of the
3661 Boolean search must match as full words.
20908596
CD
3662
3663This command searches the agenda files, and in addition the files listed
3664in `org-agenda-text-search-extra-files'."
3665 (interactive "P")
3666 (org-compile-prefix-format 'search)
3667 (org-set-sorting-strategy 'search)
3668 (org-prepare-agenda "SEARCH")
3669 (let* ((props (list 'face nil
c8d0cf5c 3670 'done-face 'org-agenda-done
20908596
CD
3671 'org-not-done-regexp org-not-done-regexp
3672 'org-todo-regexp org-todo-regexp
b349f79f 3673 'org-complex-heading-regexp org-complex-heading-regexp
20908596 3674 'mouse-face 'highlight
20908596 3675 'help-echo (format "mouse-2 or RET jump to location")))
ed21c5c8 3676 (full-words org-agenda-search-view-force-full-words)
86fbb8ca 3677 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
20908596 3678 regexp rtn rtnall files file pos
ed21c5c8 3679 marker category tags c neg re boolean
20908596
CD
3680 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3681 (unless (and (not edit-at)
3682 (stringp string)
3683 (string-match "\\S-" string))
ed21c5c8
CD
3684 (setq string (read-string
3685 (if org-agenda-search-view-always-boolean
3686 "[+-]Word/{Regexp} ...: "
3687 "Phrase, or [+-]Word/{Regexp} ...: ")
3688 (cond
3689 ((integerp edit-at) (cons string edit-at))
3690 (edit-at string))
3691 'org-agenda-search-history)))
20908596
CD
3692 (org-set-local 'org-todo-only todo-only)
3693 (setq org-agenda-redo-command
3694 (list 'org-search-view (if todo-only t nil) string
3695 '(if current-prefix-arg 1 nil)))
3696 (setq org-agenda-query-string string)
3697
3698 (if (equal (string-to-char string) ?*)
3699 (setq hdl-only t
3700 words (substring string 1))
3701 (setq words string))
3702 (when (equal (string-to-char words) ?!)
3703 (setq todo-only t
3704 words (substring words 1)))
ed21c5c8
CD
3705 (when (equal (string-to-char words) ?:)
3706 (setq full-words t
3707 words (substring words 1)))
3708 (if (or org-agenda-search-view-always-boolean
3709 (member (string-to-char words) '(?- ?+ ?\{)))
3710 (setq boolean t))
20908596 3711 (setq words (org-split-string words))
afe98dfa
CD
3712 (let (www w)
3713 (while (setq w (pop words))
3714 (while (and (string-match "\\\\\\'" w) words)
3715 (setq w (concat (substring w 0 -1) " " (pop words))))
3716 (push w www))
3717 (setq words (nreverse www) www nil)
3718 (while (setq w (pop words))
3719 (when (and (string-match "\\`[-+]?{" w)
3720 (not (string-match "}\\'" w)))
3721 (while (and words (not (string-match "}\\'" (car words))))
3722 (setq w (concat w " " (pop words))))
3723 (setq w (concat w " " (pop words))))
3724 (push w www))
3725 (setq words (nreverse www)))
ed21c5c8
CD
3726 (setq org-agenda-last-search-view-search-was-boolean boolean)
3727 (when boolean
3728 (let (wds w)
3729 (while (setq w (pop words))
3730 (if (or (equal (substring w 0 1) "\"")
3731 (and (> (length w) 1)
3732 (member (substring w 0 1) '("+" "-"))
3733 (equal (substring w 1 2) "\"")))
3734 (while (and words (not (equal (substring w -1) "\"")))
3735 (setq w (concat w " " (pop words)))))
3736 (and (string-match "\\`\\([-+]?\\)\"" w)
3737 (setq w (replace-match "\\1" nil nil w)))
3738 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3739 (push w wds))
3740 (setq words (nreverse wds))))
3741 (if boolean
8bfe682a
CD
3742 (mapc (lambda (w)
3743 (setq c (string-to-char w))
3744 (if (equal c ?-)
3745 (setq neg t w (substring w 1))
3746 (if (equal c ?+)
3747 (setq neg nil w (substring w 1))
ed21c5c8 3748 (setq neg nil)))
8bfe682a
CD
3749 (if (string-match "\\`{.*}\\'" w)
3750 (setq re (substring w 1 -1))
ed21c5c8
CD
3751 (if full-words
3752 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3753 (setq re (regexp-quote (downcase w)))))
8bfe682a
CD
3754 (if neg (push re regexps-) (push re regexps+)))
3755 words)
3756 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3757 regexps+))
20908596
CD
3758 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3759 (if (not regexps+)
3760 (setq regexp (concat "^" org-outline-regexp))
3761 (setq regexp (pop regexps+))
3762 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3763 regexp))))
2c3ad40d 3764 (setq files (org-agenda-files nil 'ifmode))
20908596
CD
3765 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3766 (pop org-agenda-text-search-extra-files)
3767 (setq files (org-add-archive-files files)))
3768 (setq files (append files org-agenda-text-search-extra-files)
3769 rtnall nil)
3770 (while (setq file (pop files))
3771 (setq ee nil)
3772 (catch 'nextfile
3773 (org-check-agenda-file file)
3774 (setq buffer (if (file-exists-p file)
3775 (org-get-agenda-file-buffer file)
3776 (error "No such file %s" file)))
3777 (if (not buffer)
3778 ;; If file does not exist, make sure an error message is sent
3779 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3780 file))))
3781 (with-current-buffer buffer
3782 (with-syntax-table (org-search-syntax-table)
3783 (unless (org-mode-p)
3784 (error "Agenda file %s is not in `org-mode'" file))
3785 (let ((case-fold-search t))
3786 (save-excursion
3787 (save-restriction
3788 (if org-agenda-restrict
3789 (narrow-to-region org-agenda-restrict-begin
3790 org-agenda-restrict-end)
3791 (widen))
3792 (goto-char (point-min))
3793 (unless (or (org-on-heading-p)
3794 (outline-next-heading))
3795 (throw 'nextfile t))
3796 (goto-char (max (point-min) (1- (point))))
3797 (while (re-search-forward regexp nil t)
3798 (org-back-to-heading t)
3799 (skip-chars-forward "* ")
3800 (setq beg (point-at-bol)
3801 beg1 (point)
3802 end (progn (outline-next-heading) (point)))
3803 (catch :skip
3804 (goto-char beg)
3805 (org-agenda-skip)
3806 (setq str (buffer-substring-no-properties
3807 (point-at-bol)
3808 (if hdl-only (point-at-eol) end)))
3809 (mapc (lambda (wr) (when (string-match wr str)
3810 (goto-char (1- end))
3811 (throw :skip t)))
3812 regexps-)
3813 (mapc (lambda (wr) (unless (string-match wr str)
3814 (goto-char (1- end))
3815 (throw :skip t)))
3816 (if todo-only
3817 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3818 regexps+)
3819 regexps+))
3820 (goto-char beg)
3821 (setq marker (org-agenda-new-marker (point))
3822 category (org-get-category)
3823 tags (org-get-tags-at (point))
3824 txt (org-format-agenda-item
3825 ""
3826 (buffer-substring-no-properties
3827 beg1 (point-at-eol))
3828 category tags))
3829 (org-add-props txt props
3830 'org-marker marker 'org-hd-marker marker
3831 'org-todo-regexp org-todo-regexp
b349f79f 3832 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
3833 'priority 1000 'org-category category
3834 'type "search")
3835 (push txt ee)
3836 (goto-char (1- end))))))))))
3837 (setq rtn (nreverse ee))
3838 (setq rtnall (append rtnall rtn)))
3839 (if org-agenda-overriding-header
3840 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3841 nil 'face 'org-agenda-structure) "\n")
3842 (insert "Search words: ")
3843 (add-text-properties (point-min) (1- (point))
3844 (list 'face 'org-agenda-structure))
3845 (setq pos (point))
3846 (insert string "\n")
3847 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3848 (setq pos (point))
3849 (unless org-agenda-multi
3850 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3851 (add-text-properties pos (1- (point))
3852 (list 'face 'org-agenda-structure))))
8d642074 3853 (org-agenda-mark-header-line (point-min))
20908596
CD
3854 (when rtnall
3855 (insert (org-finalize-agenda-entries rtnall) "\n"))
3856 (goto-char (point-min))
c8d0cf5c 3857 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3858 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3859 (org-finalize-agenda)
3860 (setq buffer-read-only t)))
3861
3862;;; Agenda TODO list
3863
3864(defvar org-select-this-todo-keyword nil)
3865(defvar org-last-arg nil)
3866
3867;;;###autoload
3868(defun org-todo-list (arg)
86fbb8ca 3869 "Show all (not done) TODO entries from all agenda file in a single list.
20908596
CD
3870The prefix arg can be used to select a specific TODO keyword and limit
3871the list to these. When using \\[universal-argument], you will be prompted
3872for a keyword. A numeric prefix directly selects the Nth keyword in
3873`org-todo-keywords-1'."
3874 (interactive "P")
20908596
CD
3875 (org-compile-prefix-format 'todo)
3876 (org-set-sorting-strategy 'todo)
3877 (org-prepare-agenda "TODO")
ed21c5c8 3878 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
acedf35c 3879 (let* ((today (org-today))
20908596
CD
3880 (date (calendar-gregorian-from-absolute today))
3881 (kwds org-todo-keywords-for-agenda)
3882 (completion-ignore-case t)
3883 (org-select-this-todo-keyword
3884 (if (stringp arg) arg
3885 (and arg (integerp arg) (> arg 0)
3886 (nth (1- arg) kwds))))
3887 rtn rtnall files file pos)
3888 (when (equal arg '(4))
3889 (setq org-select-this-todo-keyword
54a0dee5 3890 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
20908596
CD
3891 (mapcar 'list kwds) nil nil)))
3892 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3893 (org-set-local 'org-last-arg arg)
3894 (setq org-agenda-redo-command
3895 '(org-todo-list (or current-prefix-arg org-last-arg)))
2c3ad40d 3896 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
3897 rtnall nil)
3898 (while (setq file (pop files))
3899 (catch 'nextfile
3900 (org-check-agenda-file file)
3901 (setq rtn (org-agenda-get-day-entries file date :todo))
3902 (setq rtnall (append rtnall rtn))))
3903 (if org-agenda-overriding-header
3904 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3905 nil 'face 'org-agenda-structure) "\n")
3906 (insert "Global list of TODO items of type: ")
3907 (add-text-properties (point-min) (1- (point))
8d642074
CD
3908 (list 'face 'org-agenda-structure
3909 'short-heading
3910 (concat "ToDo: "
3911 (or org-select-this-todo-keyword "ALL"))))
3912 (org-agenda-mark-header-line (point-min))
20908596
CD
3913 (setq pos (point))
3914 (insert (or org-select-this-todo-keyword "ALL") "\n")
3915 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3916 (setq pos (point))
3917 (unless org-agenda-multi
3918 (insert "Available with `N r': (0)ALL")
3919 (let ((n 0) s)
3920 (mapc (lambda (x)
3921 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3922 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3923 (insert "\n "))
3924 (insert " " s))
3925 kwds))
3926 (insert "\n"))
3927 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 3928 (org-agenda-mark-header-line (point-min))
20908596
CD
3929 (when rtnall
3930 (insert (org-finalize-agenda-entries rtnall) "\n"))
3931 (goto-char (point-min))
c8d0cf5c 3932 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3933 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3934 (org-finalize-agenda)
3935 (setq buffer-read-only t)))
3936
3937;;; Agenda tags match
3938
3939;;;###autoload
3940(defun org-tags-view (&optional todo-only match)
3941 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3942The prefix arg TODO-ONLY limits the search to TODO entries."
3943 (interactive "P")
3944 (org-compile-prefix-format 'tags)
3945 (org-set-sorting-strategy 'tags)
3946 (let* ((org-tags-match-list-sublevels
c8d0cf5c 3947 org-tags-match-list-sublevels)
20908596
CD
3948 (completion-ignore-case t)
3949 rtn rtnall files file pos matcher
3950 buffer)
ed21c5c8
CD
3951 (when (and (stringp match) (not (string-match "\\S-" match)))
3952 (setq match nil))
20908596
CD
3953 (setq matcher (org-make-tags-matcher match)
3954 match (car matcher) matcher (cdr matcher))
3955 (org-prepare-agenda (concat "TAGS " match))
3956 (setq org-agenda-query-string match)
3957 (setq org-agenda-redo-command
3958 (list 'org-tags-view (list 'quote todo-only)
3959 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
2c3ad40d 3960 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
3961 rtnall nil)
3962 (while (setq file (pop files))
3963 (catch 'nextfile
3964 (org-check-agenda-file file)
3965 (setq buffer (if (file-exists-p file)
3966 (org-get-agenda-file-buffer file)
3967 (error "No such file %s" file)))
3968 (if (not buffer)
33306645 3969 ;; If file does not exist, error message to agenda
20908596
CD
3970 (setq rtn (list
3971 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3972 rtnall (append rtnall rtn))
3973 (with-current-buffer buffer
3974 (unless (org-mode-p)
3975 (error "Agenda file %s is not in `org-mode'" file))
3976 (save-excursion
3977 (save-restriction
3978 (if org-agenda-restrict
3979 (narrow-to-region org-agenda-restrict-begin
3980 org-agenda-restrict-end)
3981 (widen))
3982 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3983 (setq rtnall (append rtnall rtn))))))))
3984 (if org-agenda-overriding-header
3985 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3986 nil 'face 'org-agenda-structure) "\n")
3987 (insert "Headlines with TAGS match: ")
3988 (add-text-properties (point-min) (1- (point))
8d642074
CD
3989 (list 'face 'org-agenda-structure
3990 'short-heading
3991 (concat "Match: " match)))
20908596
CD
3992 (setq pos (point))
3993 (insert match "\n")
3994 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3995 (setq pos (point))
3996 (unless org-agenda-multi
3997 (insert "Press `C-u r' to search again with new search string\n"))
3998 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 3999 (org-agenda-mark-header-line (point-min))
20908596
CD
4000 (when rtnall
4001 (insert (org-finalize-agenda-entries rtnall) "\n"))
4002 (goto-char (point-min))
c8d0cf5c 4003 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
4004 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4005 (org-finalize-agenda)
4006 (setq buffer-read-only t)))
4007
4008;;; Agenda Finding stuck projects
4009
4010(defvar org-agenda-skip-regexp nil
4011 "Regular expression used in skipping subtrees for the agenda.
4012This is basically a temporary global variable that can be set and then
4013used by user-defined selections using `org-agenda-skip-function'.")
4014
4015(defvar org-agenda-overriding-header nil
c8d0cf5c
CD
4016 "When this is set during todo and tags searches, will replace header.
4017This variable should not be set directly, but custom commands can bind it
4018in the options section.")
4019
4020(defun org-agenda-skip-entry-when-regexp-matches ()
86fbb8ca 4021 "Check if the current entry contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4022If yes, it returns the end position of this entry, causing agenda commands
4023to skip the entry but continuing the search in the subtree. This is a
4024function that can be put into `org-agenda-skip-function' for the duration
4025of a command."
4026 (let ((end (save-excursion (org-end-of-subtree t)))
4027 skip)
4028 (save-excursion
4029 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4030 (and skip end)))
20908596
CD
4031
4032(defun org-agenda-skip-subtree-when-regexp-matches ()
86fbb8ca 4033 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
20908596
CD
4034If yes, it returns the end position of this tree, causing agenda commands
4035to skip this subtree. This is a function that can be put into
4036`org-agenda-skip-function' for the duration of a command."
4037 (let ((end (save-excursion (org-end-of-subtree t)))
4038 skip)
4039 (save-excursion
4040 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4041 (and skip end)))
4042
c8d0cf5c 4043(defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
86fbb8ca 4044 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4045If yes, it returns the end position of the current entry (NOT the tree),
4046causing agenda commands to skip the entry but continuing the search in
4047the subtree. This is a function that can be put into
4048`org-agenda-skip-function' for the duration of a command. An important
4049use of this function is for the stuck project list."
4050 (let ((end (save-excursion (org-end-of-subtree t)))
4051 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4052 skip)
4053 (save-excursion
4054 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4055 (and skip entry-end)))
4056
20908596
CD
4057(defun org-agenda-skip-entry-if (&rest conditions)
4058 "Skip entry if any of CONDITIONS is true.
4059See `org-agenda-skip-if' for details."
4060 (org-agenda-skip-if nil conditions))
4061
4062(defun org-agenda-skip-subtree-if (&rest conditions)
4063 "Skip entry if any of CONDITIONS is true.
4064See `org-agenda-skip-if' for details."
4065 (org-agenda-skip-if t conditions))
4066
4067(defun org-agenda-skip-if (subtree conditions)
4068 "Checks current entity for CONDITIONS.
4069If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4070the entry, i.e. the text before the next heading is checked.
4071
4072CONDITIONS is a list of symbols, boolean OR is used to combine the results
4073from different tests. Valid conditions are:
4074
4075scheduled Check if there is a scheduled cookie
4076notscheduled Check if there is no scheduled cookie
4077deadline Check if there is a deadline
4078notdeadline Check if there is no deadline
c8d0cf5c
CD
4079timestamp Check if there is a timestamp (also deadline or scheduled)
4080nottimestamp Check if there is no timestamp (also deadline or scheduled)
20908596
CD
4081regexp Check if regexp matches
4082notregexp Check if regexp does not match.
ed21c5c8
CD
4083todo Check if TODO keyword matches
4084nottodo Check if TODO keyword does not match
20908596
CD
4085
4086The regexp is taken from the conditions list, it must come right after
4087the `regexp' or `notregexp' element.
4088
ed21c5c8
CD
4089`todo' and `nottodo' accept as an argument a list of todo
4090keywords, which may include \"*\" to match any todo keyword.
4091
4092 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4093
4094would skip all entries with \"TODO\" or \"WAITING\" keywords.
4095
4096Instead of a list a keyword class may be given
4097
4098 (org-agenda-skip-entry-if 'nottodo 'done)
4099
4100would skip entries that haven't been marked with any of \"DONE\"
4101keywords. Possible classes are: `todo', `done', `any'.
4102
20908596
CD
4103If any of these conditions is met, this function returns the end point of
4104the entity, causing the search to continue from there. This is a function
4105that can be put into `org-agenda-skip-function' for the duration of a command."
4106 (let (beg end m)
4107 (org-back-to-heading t)
4108 (setq beg (point)
4109 end (if subtree
4110 (progn (org-end-of-subtree t) (point))
4111 (progn (outline-next-heading) (1- (point)))))
4112 (goto-char beg)
4113 (and
4114 (or
4115 (and (memq 'scheduled conditions)
4116 (re-search-forward org-scheduled-time-regexp end t))
4117 (and (memq 'notscheduled conditions)
4118 (not (re-search-forward org-scheduled-time-regexp end t)))
4119 (and (memq 'deadline conditions)
4120 (re-search-forward org-deadline-time-regexp end t))
4121 (and (memq 'notdeadline conditions)
4122 (not (re-search-forward org-deadline-time-regexp end t)))
c8d0cf5c
CD
4123 (and (memq 'timestamp conditions)
4124 (re-search-forward org-ts-regexp end t))
4125 (and (memq 'nottimestamp conditions)
4126 (not (re-search-forward org-ts-regexp end t)))
20908596
CD
4127 (and (setq m (memq 'regexp conditions))
4128 (stringp (nth 1 m))
4129 (re-search-forward (nth 1 m) end t))
4130 (and (setq m (memq 'notregexp conditions))
4131 (stringp (nth 1 m))
ed21c5c8
CD
4132 (not (re-search-forward (nth 1 m) end t)))
4133 (and (or
4134 (setq m (memq 'todo conditions))
4135 (setq m (memq 'nottodo conditions)))
4136 (org-agenda-skip-if-todo m end)))
20908596
CD
4137 end)))
4138
ed21c5c8
CD
4139(defun org-agenda-skip-if-todo (args end)
4140 "Helper function for `org-agenda-skip-if', do not use it directly.
4141ARGS is a list with first element either `todo' or `nottodo'.
4142The remainder is either a list of TODO keywords, or a state symbol
4143`todo' or `done' or `any'."
4144 (let ((kw (car args))
4145 (arg (cadr args))
4146 todo-wds todo-re)
4147 (setq todo-wds
4148 (org-uniquify
4149 (cond
4150 ((listp arg) ;; list of keywords
4151 (if (member "*" arg)
4152 (mapcar 'substring-no-properties org-todo-keywords-1)
4153 arg))
4154 ((symbolp arg) ;; keyword class name
4155 (cond
4156 ((eq arg 'todo)
4157 (org-delete-all org-done-keywords
4158 (mapcar 'substring-no-properties
4159 org-todo-keywords-1)))
4160 ((eq arg 'done) org-done-keywords)
4161 ((eq arg 'any)
4162 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4163 (setq todo-re
4164 (concat "^\\*+[ \t]+\\<\\("
4165 (mapconcat 'identity todo-wds "\\|")
4166 "\\)\\>"))
4167 (if (eq kw 'todo)
4168 (re-search-forward todo-re end t)
4169 (not (re-search-forward todo-re end t)))))
4170
20908596
CD
4171;;;###autoload
4172(defun org-agenda-list-stuck-projects (&rest ignore)
4173 "Create agenda view for projects that are stuck.
4174Stuck projects are project that have no next actions. For the definitions
4175of what a project is and how to check if it stuck, customize the variable
afe98dfa 4176`org-stuck-projects'."
20908596 4177 (interactive)
c8d0cf5c
CD
4178 (let* ((org-agenda-skip-function
4179 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
20908596 4180 ;; We could have used org-agenda-skip-if here.
c8d0cf5c
CD
4181 (org-agenda-overriding-header
4182 (or org-agenda-overriding-header "List of stuck projects: "))
20908596
CD
4183 (matcher (nth 0 org-stuck-projects))
4184 (todo (nth 1 org-stuck-projects))
4185 (todo-wds (if (member "*" todo)
4186 (progn
2c3ad40d
CD
4187 (org-prepare-agenda-buffers (org-agenda-files
4188 nil 'ifmode))
20908596
CD
4189 (org-delete-all
4190 org-done-keywords-for-agenda
4191 (copy-sequence org-todo-keywords-for-agenda)))
4192 todo))
4193 (todo-re (concat "^\\*+[ \t]+\\("
4194 (mapconcat 'identity todo-wds "\\|")
4195 "\\)\\>"))
4196 (tags (nth 2 org-stuck-projects))
4197 (tags-re (if (member "*" tags)
afe98dfa 4198 (org-re "^\\*+ .*:[[:alnum:]_@#%]+:[ \t]*$")
c8d0cf5c
CD
4199 (if tags
4200 (concat "^\\*+ .*:\\("
4201 (mapconcat 'identity tags "\\|")
afe98dfa 4202 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
20908596
CD
4203 (gen-re (nth 3 org-stuck-projects))
4204 (re-list
4205 (delq nil
4206 (list
4207 (if todo todo-re)
4208 (if tags tags-re)
4209 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4210 gen-re)))))
4211 (setq org-agenda-skip-regexp
4212 (if re-list
4213 (mapconcat 'identity re-list "\\|")
4214 (error "No information how to identify unstuck projects")))
4215 (org-tags-view nil matcher)
4216 (with-current-buffer org-agenda-buffer-name
4217 (setq org-agenda-redo-command
4218 '(org-agenda-list-stuck-projects
4219 (or current-prefix-arg org-last-arg))))))
4220
4221;;; Diary integration
4222
4223(defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4224(defvar list-diary-entries-hook)
4225
4226(defun org-get-entries-from-diary (date)
4227 "Get the (Emacs Calendar) diary entries for DATE."
4228 (require 'diary-lib)
4229 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
20908596 4230 (diary-display-hook '(fancy-diary-display))
ca8ef0dc 4231 (diary-display-function 'fancy-diary-display)
20908596
CD
4232 (pop-up-frames nil)
4233 (list-diary-entries-hook
4234 (cons 'org-diary-default-entry list-diary-entries-hook))
4235 (diary-file-name-prefix-function nil) ; turn this feature off
4236 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4237 entries
4238 (org-disable-agenda-to-diary t))
4239 (save-excursion
4240 (save-window-excursion
4241 (funcall (if (fboundp 'diary-list-entries)
4242 'diary-list-entries 'list-diary-entries)
4243 date 1)))
4244 (if (not (get-buffer diary-fancy-buffer))
4245 (setq entries nil)
4246 (with-current-buffer diary-fancy-buffer
4247 (setq buffer-read-only nil)
4248 (if (zerop (buffer-size))
4249 ;; No entries
4250 (setq entries nil)
4251 ;; Omit the date and other unnecessary stuff
4252 (org-agenda-cleanup-fancy-diary)
4253 ;; Add prefix to each line and extend the text properties
4254 (if (zerop (buffer-size))
4255 (setq entries nil)
4256 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
4257 (set-buffer-modified-p nil)
4258 (kill-buffer diary-fancy-buffer)))
4259 (when entries
4260 (setq entries (org-split-string entries "\n"))
4261 (setq entries
4262 (mapcar
4263 (lambda (x)
4264 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4265 ;; Extend the text properties to the beginning of the line
4266 (org-add-props x (text-properties-at (1- (length x)) x)
ed21c5c8 4267 'type "diary" 'date date 'face 'org-agenda-diary))
20908596
CD
4268 entries)))))
4269
c8d0cf5c
CD
4270(defvar org-agenda-cleanup-fancy-diary-hook nil
4271 "Hook run when the fancy diary buffer is cleaned up.")
4272
20908596
CD
4273(defun org-agenda-cleanup-fancy-diary ()
4274 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4275This gets rid of the date, the underline under the date, and
4276the dummy entry installed by `org-mode' to ensure non-empty diary for each
4277date. It also removes lines that contain only whitespace."
4278 (goto-char (point-min))
4279 (if (looking-at ".*?:[ \t]*")
4280 (progn
4281 (replace-match "")
4282 (re-search-forward "\n=+$" nil t)
4283 (replace-match "")
4284 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4285 (re-search-forward "\n=+$" nil t)
4286 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4287 (goto-char (point-min))
4288 (while (re-search-forward "^ +\n" nil t)
4289 (replace-match ""))
4290 (goto-char (point-min))
4291 (if (re-search-forward "^Org-mode dummy\n?" nil t)
c8d0cf5c
CD
4292 (replace-match ""))
4293 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
20908596
CD
4294
4295;; Make sure entries from the diary have the right text properties.
4296(eval-after-load "diary-lib"
4297 '(if (boundp 'diary-modify-entry-list-string-function)
4298 ;; We can rely on the hook, nothing to do
4299 nil
33306645 4300 ;; Hook not available, must use advice to make this work
20908596
CD
4301 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4302 "Make the position visible."
4303 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4304 (stringp string)
4305 buffer-file-name)
4306 (setq string (org-modify-diary-entry-string string))))))
4307
4308(defun org-modify-diary-entry-string (string)
4309 "Add text properties to string, allowing org-mode to act on it."
4310 (org-add-props string nil
4311 'mouse-face 'highlight
20908596
CD
4312 'help-echo (if buffer-file-name
4313 (format "mouse-2 or RET jump to diary file %s"
4314 (abbreviate-file-name buffer-file-name))
4315 "")
4316 'org-agenda-diary-link t
4317 'org-marker (org-agenda-new-marker (point-at-bol))))
4318
4319(defun org-diary-default-entry ()
4320 "Add a dummy entry to the diary.
4321Needed to avoid empty dates which mess up holiday display."
4322 ;; Catch the error if dealing with the new add-to-diary-alist
4323 (when org-disable-agenda-to-diary
4324 (condition-case nil
4325 (org-add-to-diary-list original-date "Org-mode dummy" "")
4326 (error
4327 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4328
4329(defun org-add-to-diary-list (&rest args)
4330 (if (fboundp 'diary-add-to-list)
4331 (apply 'diary-add-to-list args)
4332 (apply 'add-to-diary-list args)))
4333
ed21c5c8
CD
4334(defvar org-diary-last-run-time nil)
4335
20908596
CD
4336;;;###autoload
4337(defun org-diary (&rest args)
4338 "Return diary information from org-files.
4339This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4340It accesses org files and extracts information from those files to be
4341listed in the diary. The function accepts arguments specifying what
ed21c5c8
CD
4342items should be listed. For a list of arguments allowed here, see the
4343variable `org-agenda-entry-types'.
20908596
CD
4344
4345The call in the diary file should look like this:
4346
4347 &%%(org-diary) ~/path/to/some/orgfile.org
4348
4349Use a separate line for each org file to check. Or, if you omit the file name,
4350all files listed in `org-agenda-files' will be checked automatically:
4351
4352 &%%(org-diary)
4353
4354If you don't give any arguments (as in the example above), the default
4355arguments (:deadline :scheduled :timestamp :sexp) are used.
4356So the example above may also be written as
4357
4358 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4359
4360The function expects the lisp variables `entry' and `date' to be provided
4361by the caller, because this is how the calendar works. Don't use this
4362function from a program - use `org-agenda-get-day-entries' instead."
54a0dee5 4363 (when (> (- (org-float-time)
20908596
CD
4364 org-agenda-last-marker-time)
4365 5)
4366 (org-agenda-reset-markers))
4367 (org-compile-prefix-format 'agenda)
4368 (org-set-sorting-strategy 'agenda)
4369 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4370 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4371 (list entry)
4372 (org-agenda-files t)))
ed21c5c8 4373 (time (org-float-time))
20908596 4374 file rtn results)
ed21c5c8
CD
4375 (when (or (not org-diary-last-run-time)
4376 (> (- time
4377 org-diary-last-run-time)
4378 3))
4379 (org-prepare-agenda-buffers files))
4380 (setq org-diary-last-run-time time)
20908596
CD
4381 ;; If this is called during org-agenda, don't return any entries to
4382 ;; the calendar. Org Agenda will list these entries itself.
4383 (if org-disable-agenda-to-diary (setq files nil))
4384 (while (setq file (pop files))
4385 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4386 (setq results (append results rtn)))
4387 (if results
4388 (concat (org-finalize-agenda-entries results) "\n"))))
4389
4390;;; Agenda entry finders
4391
4392(defun org-agenda-get-day-entries (file date &rest args)
4393 "Does the work for `org-diary' and `org-agenda'.
4394FILE is the path to a file to be checked for entries. DATE is date like
4395the one returned by `calendar-current-date'. ARGS are symbols indicating
4396which kind of entries should be extracted. For details about these, see
4397the documentation of `org-diary'."
4398 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4399 (let* ((org-startup-folded nil)
4400 (org-startup-align-all-tables nil)
4401 (buffer (if (file-exists-p file)
4402 (org-get-agenda-file-buffer file)
4403 (error "No such file %s" file)))
54a0dee5 4404 arg results rtn deadline-results)
20908596
CD
4405 (if (not buffer)
4406 ;; If file does not exist, make sure an error message ends up in diary
4407 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4408 (with-current-buffer buffer
4409 (unless (org-mode-p)
4410 (error "Agenda file %s is not in `org-mode'" file))
4411 (let ((case-fold-search nil))
4412 (save-excursion
4413 (save-restriction
4414 (if org-agenda-restrict
4415 (narrow-to-region org-agenda-restrict-begin
4416 org-agenda-restrict-end)
4417 (widen))
4418 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4419 (while (setq arg (pop args))
4420 (cond
4421 ((and (eq arg :todo)
4422 (equal date (calendar-current-date)))
4423 (setq rtn (org-agenda-get-todos))
4424 (setq results (append results rtn)))
4425 ((eq arg :timestamp)
4426 (setq rtn (org-agenda-get-blocks))
4427 (setq results (append results rtn))
4428 (setq rtn (org-agenda-get-timestamps))
4429 (setq results (append results rtn)))
4430 ((eq arg :sexp)
4431 (setq rtn (org-agenda-get-sexps))
4432 (setq results (append results rtn)))
4433 ((eq arg :scheduled)
54a0dee5 4434 (setq rtn (org-agenda-get-scheduled deadline-results))
20908596
CD
4435 (setq results (append results rtn)))
4436 ((eq arg :closed)
93b62de8 4437 (setq rtn (org-agenda-get-progress))
20908596
CD
4438 (setq results (append results rtn)))
4439 ((eq arg :deadline)
4440 (setq rtn (org-agenda-get-deadlines))
54a0dee5 4441 (setq deadline-results (copy-sequence rtn))
20908596
CD
4442 (setq results (append results rtn))))))))
4443 results))))
4444
4445(defun org-agenda-get-todos ()
4446 "Return the TODO information for agenda display."
4447 (let* ((props (list 'face nil
c8d0cf5c 4448 'done-face 'org-agenda-done
20908596
CD
4449 'org-not-done-regexp org-not-done-regexp
4450 'org-todo-regexp org-todo-regexp
b349f79f 4451 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4452 'mouse-face 'highlight
20908596
CD
4453 'help-echo
4454 (format "mouse-2 or RET jump to org file %s"
4455 (abbreviate-file-name buffer-file-name))))
4456 (regexp (concat "^\\*+[ \t]+\\("
4457 (if org-select-this-todo-keyword
4458 (if (equal org-select-this-todo-keyword "*")
4459 org-todo-regexp
4460 (concat "\\<\\("
4461 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4462 "\\)\\>"))
4463 org-not-done-regexp)
4464 "[^\n\r]*\\)"))
621f83e4 4465 marker priority category tags todo-state
20908596
CD
4466 ee txt beg end)
4467 (goto-char (point-min))
4468 (while (re-search-forward regexp nil t)
4469 (catch :skip
4470 (save-match-data
4471 (beginning-of-line)
d6685abc 4472 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
0bd48b37 4473 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
20908596
CD
4474 (goto-char (1+ beg))
4475 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4476 (throw :skip nil)))
4477 (goto-char beg)
4478 (org-agenda-skip)
4479 (goto-char (match-beginning 1))
4480 (setq marker (org-agenda-new-marker (match-beginning 0))
4481 category (org-get-category)
c8d0cf5c 4482 txt (match-string 1)
20908596 4483 tags (org-get-tags-at (point))
c8d0cf5c 4484 txt (org-format-agenda-item "" txt category tags)
621f83e4
CD
4485 priority (1+ (org-get-priority txt))
4486 todo-state (org-get-todo-state))
20908596
CD
4487 (org-add-props txt props
4488 'org-marker marker 'org-hd-marker marker
4489 'priority priority 'org-category category
621f83e4 4490 'type "todo" 'todo-state todo-state)
20908596
CD
4491 (push txt ee)
4492 (if org-agenda-todo-list-sublevels
4493 (goto-char (match-end 1))
4494 (org-end-of-subtree 'invisible))))
4495 (nreverse ee)))
4496
0bd48b37 4497;;;###autoload
ed21c5c8
CD
4498(defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4499 (&optional end)
4500 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
0bd48b37
CD
4501 (when (or org-agenda-todo-ignore-with-date
4502 org-agenda-todo-ignore-scheduled
acedf35c
CD
4503 org-agenda-todo-ignore-deadlines
4504 org-agenda-todo-ignore-timestamp)
0bd48b37
CD
4505 (setq end (or end (save-excursion (outline-next-heading) (point))))
4506 (save-excursion
4507 (or (and org-agenda-todo-ignore-with-date
4508 (re-search-forward org-ts-regexp end t))
4509 (and org-agenda-todo-ignore-scheduled
ed21c5c8
CD
4510 (re-search-forward org-scheduled-time-regexp end t)
4511 (cond
4512 ((eq org-agenda-todo-ignore-scheduled 'future)
4513 (> (org-days-to-time (match-string 1)) 0))
4514 ((eq org-agenda-todo-ignore-scheduled 'past)
4515 (<= (org-days-to-time (match-string 1)) 0))
4516 (t)))
0bd48b37
CD
4517 (and org-agenda-todo-ignore-deadlines
4518 (re-search-forward org-deadline-time-regexp end t)
ed21c5c8
CD
4519 (cond
4520 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4521 ((eq org-agenda-todo-ignore-deadlines 'far)
4522 (not (org-deadline-close (match-string 1))))
4523 ((eq org-agenda-todo-ignore-deadlines 'future)
4524 (> (org-days-to-time (match-string 1)) 0))
4525 ((eq org-agenda-todo-ignore-deadlines 'past)
4526 (<= (org-days-to-time (match-string 1)) 0))
acedf35c
CD
4527 (t (org-deadline-close (match-string 1)))))
4528 (and org-agenda-todo-ignore-timestamp
4529 (let ((buffer (current-buffer))
4530 (regexp
4531 (concat
4532 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4533 (start (point)))
4534 ;; Copy current buffer into a temporary one
4535 (with-temp-buffer
4536 (insert-buffer-substring buffer start end)
4537 (goto-char (point-min))
4538 ;; Delete SCHEDULED and DEADLINE items
4539 (while (re-search-forward regexp end t)
4540 (delete-region (match-beginning 0) (match-end 0)))
4541 (goto-char (point-min))
4542 ;; No search for timestamp left
4543 (when (re-search-forward org-ts-regexp nil t)
4544 (cond
4545 ((eq org-agenda-todo-ignore-timestamp 'future)
4546 (> (org-days-to-time (match-string 1)) 0))
4547 ((eq org-agenda-todo-ignore-timestamp 'past)
4548 (<= (org-days-to-time (match-string 1)) 0))
4549 (t))))))))))
0bd48b37 4550
20908596
CD
4551(defconst org-agenda-no-heading-message
4552 "No heading for this item in buffer or region.")
4553
4554(defun org-agenda-get-timestamps ()
4555 "Return the date stamp information for agenda display."
4556 (let* ((props (list 'face nil
4557 'org-not-done-regexp org-not-done-regexp
4558 'org-todo-regexp org-todo-regexp
b349f79f 4559 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4560 'mouse-face 'highlight
20908596
CD
4561 'help-echo
4562 (format "mouse-2 or RET jump to org file %s"
4563 (abbreviate-file-name buffer-file-name))))
4564 (d1 (calendar-absolute-from-gregorian date))
4565 (remove-re
4566 (concat
4567 (regexp-quote
4568 (format-time-string
4569 "<%Y-%m-%d"
4570 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4571 ".*?>"))
4572 (regexp
4573 (concat
4574 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4575 (regexp-quote
4576 (substring
4577 (format-time-string
4578 (car org-time-stamp-formats)
4579 (apply 'encode-time ; DATE bound by calendar
4580 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4581 1 11))
4582 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4583 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4584 marker hdmarker deadlinep scheduledp clockp closedp inactivep
621f83e4 4585 donep tmp priority category ee txt timestr tags b0 b3 e3 head
c8d0cf5c 4586 todo-state end-of-match)
20908596 4587 (goto-char (point-min))
c8d0cf5c 4588 (while (setq end-of-match (re-search-forward regexp nil t))
20908596
CD
4589 (setq b0 (match-beginning 0)
4590 b3 (match-beginning 3) e3 (match-end 3))
4591 (catch :skip
4592 (and (org-at-date-range-p) (throw :skip nil))
4593 (org-agenda-skip)
4594 (if (and (match-end 1)
4595 (not (= d1 (org-time-string-to-absolute
4596 (match-string 1) d1 nil
4597 org-agenda-repeating-timestamp-show-all))))
4598 (throw :skip nil))
4599 (if (and e3
4600 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4601 (throw :skip nil))
c8d0cf5c 4602 (setq tmp (buffer-substring (max (point-min)
20908596
CD
4603 (- b0 org-ds-keyword-length))
4604 b0)
4605 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4606 inactivep (= (char-after b0) ?\[)
4607 deadlinep (string-match org-deadline-regexp tmp)
4608 scheduledp (string-match org-scheduled-regexp tmp)
4609 closedp (and org-agenda-include-inactive-timestamps
4610 (string-match org-closed-string tmp))
4611 clockp (and org-agenda-include-inactive-timestamps
4612 (or (string-match org-clock-string tmp)
4613 (string-match "]-+\\'" tmp)))
86fbb8ca 4614 todo-state (ignore-errors (org-get-todo-state))
621f83e4 4615 donep (member todo-state org-done-keywords))
c8d0cf5c
CD
4616 (if (or scheduledp deadlinep closedp clockp
4617 (and donep org-agenda-skip-timestamp-if-done))
20908596
CD
4618 (throw :skip t))
4619 (if (string-match ">" timestr)
4620 ;; substring should only run to end of time stamp
4621 (setq timestr (substring timestr 0 (match-end 0))))
c8d0cf5c
CD
4622 (setq marker (org-agenda-new-marker b0)
4623 category (org-get-category b0))
20908596 4624 (save-excursion
c8d0cf5c
CD
4625 (if (not (re-search-backward "^\\*+ " nil t))
4626 (setq txt org-agenda-no-heading-message)
4627 (goto-char (match-beginning 0))
4628 (setq hdmarker (org-agenda-new-marker)
4629 tags (org-get-tags-at))
4630 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4631 (setq head (match-string 1))
4632 (setq txt (org-format-agenda-item
ed21c5c8 4633 (if inactivep org-agenda-inactive-leader nil)
c8d0cf5c
CD
4634 head category tags timestr nil
4635 remove-re)))
20908596
CD
4636 (setq priority (org-get-priority txt))
4637 (org-add-props txt props
4638 'org-marker marker 'org-hd-marker hdmarker)
4639 (org-add-props txt nil 'priority priority
4640 'org-category category 'date date
621f83e4 4641 'todo-state todo-state
20908596
CD
4642 'type "timestamp")
4643 (push txt ee))
c8d0cf5c
CD
4644 (if org-agenda-skip-additional-timestamps-same-entry
4645 (outline-next-heading)
4646 (goto-char end-of-match))))
20908596
CD
4647 (nreverse ee)))
4648
4649(defun org-agenda-get-sexps ()
4650 "Return the sexp information for agenda display."
4651 (require 'diary-lib)
4652 (let* ((props (list 'face nil
4653 'mouse-face 'highlight
20908596
CD
4654 'help-echo
4655 (format "mouse-2 or RET jump to org file %s"
4656 (abbreviate-file-name buffer-file-name))))
4657 (regexp "^&?%%(")
c8d0cf5c
CD
4658 marker category ee txt tags entry result beg b sexp sexp-entry
4659 todo-state)
20908596
CD
4660 (goto-char (point-min))
4661 (while (re-search-forward regexp nil t)
4662 (catch :skip
4663 (org-agenda-skip)
4664 (setq beg (match-beginning 0))
4665 (goto-char (1- (match-end 0)))
4666 (setq b (point))
4667 (forward-sexp 1)
4668 (setq sexp (buffer-substring b (point)))
4669 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4670 (org-trim (match-string 1))
4671 ""))
4672 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4673 (when result
4674 (setq marker (org-agenda-new-marker beg)
c8d0cf5c
CD
4675 category (org-get-category beg)
4676 todo-state (org-get-todo-state))
20908596 4677
afe98dfa
CD
4678 (dolist (r (if (stringp result)
4679 (list result)
4680 result)) ;; we expect a list here
4681 (if (string-match "\\S-" r)
4682 (setq txt r)
4683 (setq txt "SEXP entry returned empty string"))
4684
4685 (setq txt (org-format-agenda-item
4686 "" txt category tags 'time))
4687 (org-add-props txt props 'org-marker marker)
4688 (org-add-props txt nil
4689 'org-category category 'date date 'todo-state todo-state
4690 'type "sexp")
4691 (push txt ee)))))
20908596
CD
4692 (nreverse ee)))
4693
ed21c5c8
CD
4694(defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4695 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4696The order of the first 2 times 3 arguments depends on the variable
4697`calendar-date-style' or, if that is not defined, on `european-calendar-style'.
86fbb8ca 4698So for American calendars, give this as MONTH DAY YEAR, for European as
ed21c5c8
CD
4699DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4700DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4701is any number of ISO weeks in the block period for which the item should
4702be skipped."
4703 (let* ((date1 (calendar-absolute-from-gregorian
4704 (org-order-calendar-date-args m1 d1 y1)))
4705 (date2 (calendar-absolute-from-gregorian
4706 (org-order-calendar-date-args m2 d2 y2)))
4707 (d (calendar-absolute-from-gregorian date)))
4708 (and
4709 (<= date1 d)
4710 (<= d date2)
4711 (= (calendar-day-of-week date) dayname)
4712 (or (not skip-weeks)
4713 (progn
4714 (require 'cal-iso)
4715 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4716 entry)))
4717
d60b1ba1 4718(defalias 'org-get-closed 'org-agenda-get-progress)
93b62de8 4719(defun org-agenda-get-progress ()
20908596
CD
4720 "Return the logged TODO entries for agenda display."
4721 (let* ((props (list 'mouse-face 'highlight
4722 'org-not-done-regexp org-not-done-regexp
4723 'org-todo-regexp org-todo-regexp
b349f79f 4724 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
4725 'help-echo
4726 (format "mouse-2 or RET jump to org file %s"
4727 (abbreviate-file-name buffer-file-name))))
93b62de8
CD
4728 (items (if (consp org-agenda-show-log)
4729 org-agenda-show-log
4730 org-agenda-log-mode-items))
ff4be292 4731 (parts
93b62de8
CD
4732 (delq nil
4733 (list
4734 (if (memq 'closed items) (concat "\\<" org-closed-string))
4735 (if (memq 'clock items) (concat "\\<" org-clock-string))
c8d0cf5c 4736 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
93b62de8
CD
4737 (parts-re (if parts (mapconcat 'identity parts "\\|")
4738 (error "`org-agenda-log-mode-items' is empty")))
20908596 4739 (regexp (concat
93b62de8
CD
4740 "\\(" parts-re "\\)"
4741 " *\\["
20908596
CD
4742 (regexp-quote
4743 (substring
4744 (format-time-string
4745 (car org-time-stamp-formats)
4746 (apply 'encode-time ; DATE bound by calendar
4747 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4748 1 11))))
c8d0cf5c
CD
4749 (org-agenda-search-headline-for-time nil)
4750 marker hdmarker priority category tags closedp statep clockp state
4751 ee txt extra timestr rest clocked)
20908596
CD
4752 (goto-char (point-min))
4753 (while (re-search-forward regexp nil t)
4754 (catch :skip
4755 (org-agenda-skip)
4756 (setq marker (org-agenda-new-marker (match-beginning 0))
4757 closedp (equal (match-string 1) org-closed-string)
93b62de8 4758 statep (equal (string-to-char (match-string 1)) ?-)
c8d0cf5c 4759 clockp (not (or closedp statep))
93b62de8 4760 state (and statep (match-string 2))
20908596
CD
4761 category (org-get-category (match-beginning 0))
4762 timestr (buffer-substring (match-beginning 0) (point-at-eol))
20908596 4763 )
b349f79f
CD
4764 (when (string-match "\\]" timestr)
4765 ;; substring should only run to end of time stamp
4766 (setq rest (substring timestr (match-end 0))
4767 timestr (substring timestr 0 (match-end 0)))
93b62de8 4768 (if (and (not closedp) (not statep)
c8d0cf5c 4769 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
621f83e4
CD
4770 (progn (setq timestr (concat (substring timestr 0 -1)
4771 "-" (match-string 1 rest) "]"))
4772 (setq clocked (match-string 2 rest)))
4773 (setq clocked "-")))
20908596 4774 (save-excursion
86fbb8ca 4775 (setq extra nil)
c8d0cf5c 4776 (cond
86fbb8ca 4777 ((not org-agenda-log-mode-add-notes))
c8d0cf5c
CD
4778 (statep
4779 (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4780 (setq extra (match-string 1))))
4781 (clockp
4782 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
86fbb8ca 4783 (setq extra (match-string 1)))))
c8d0cf5c
CD
4784 (if (not (re-search-backward "^\\*+ " nil t))
4785 (setq txt org-agenda-no-heading-message)
4786 (goto-char (match-beginning 0))
4787 (setq hdmarker (org-agenda-new-marker)
4788 tags (org-get-tags-at))
4789 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4790 (setq txt (match-string 1))
4791 (when extra
4792 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4793 (setq txt (concat (substring txt 0 (match-beginning 1))
4794 " - " extra " " (match-string 2 txt)))
4795 (setq txt (concat txt " - " extra))))
4796 (setq txt (org-format-agenda-item
4797 (cond
4798 (closedp "Closed: ")
93b62de8
CD
4799 (statep (concat "State: (" state ")"))
4800 (t (concat "Clocked: (" clocked ")")))
c8d0cf5c 4801 txt category tags timestr)))
20908596
CD
4802 (setq priority 100000)
4803 (org-add-props txt props
c8d0cf5c 4804 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
20908596
CD
4805 'priority priority 'org-category category
4806 'type "closed" 'date date
c8d0cf5c 4807 'undone-face 'org-warning 'done-face 'org-agenda-done)
20908596
CD
4808 (push txt ee))
4809 (goto-char (point-at-eol))))
4810 (nreverse ee)))
4811
4812(defun org-agenda-get-deadlines ()
4813 "Return the deadline information for agenda display."
4814 (let* ((props (list 'mouse-face 'highlight
4815 'org-not-done-regexp org-not-done-regexp
4816 'org-todo-regexp org-todo-regexp
b349f79f 4817 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
4818 'help-echo
4819 (format "mouse-2 or RET jump to org file %s"
4820 (abbreviate-file-name buffer-file-name))))
4821 (regexp org-deadline-time-regexp)
621f83e4 4822 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596
CD
4823 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4824 d2 diff dfrac wdays pos pos1 category tags
ed21c5c8 4825 suppress-prewarning
621f83e4 4826 ee txt head face s todo-state upcomingp donep timestr)
20908596
CD
4827 (goto-char (point-min))
4828 (while (re-search-forward regexp nil t)
ed21c5c8 4829 (setq suppress-prewarning nil)
20908596
CD
4830 (catch :skip
4831 (org-agenda-skip)
ed21c5c8
CD
4832 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
4833 (save-match-data
4834 (string-match org-scheduled-time-regexp
4835 (buffer-substring (point-at-bol)
4836 (point-at-eol)))))
4837 (setq suppress-prewarning
4838 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
4839 org-agenda-skip-deadline-prewarning-if-scheduled
4840 0)))
20908596 4841 (setq s (match-string 1)
c8d0cf5c 4842 txt nil
20908596
CD
4843 pos (1- (match-beginning 1))
4844 d2 (org-time-string-to-absolute
4845 (match-string 1) d1 'past
4846 org-agenda-repeating-timestamp-show-all)
4847 diff (- d2 d1)
ed21c5c8
CD
4848 wdays (if suppress-prewarning
4849 (let ((org-deadline-warning-days suppress-prewarning))
4850 (org-get-wdays s))
4851 (org-get-wdays s))
20908596
CD
4852 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
4853 upcomingp (and todayp (> diff 0)))
4854 ;; When to show a deadline in the calendar:
4855 ;; If the expiration is within wdays warning time.
4856 ;; Past-due deadlines are only shown on the current date
8bfe682a
CD
4857 (if (and (or (and (<= diff wdays)
4858 (and todayp (not org-agenda-only-exact-dates)))
4859 (= diff 0)))
20908596 4860 (save-excursion
621f83e4 4861 (setq todo-state (org-get-todo-state))
c8d0cf5c
CD
4862 (setq donep (member todo-state org-done-keywords))
4863 (if (and donep
4864 (or org-agenda-skip-deadline-if-done
4865 (not (= diff 0))))
4866 (setq txt nil)
4867 (setq category (org-get-category))
4868 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4869 (setq txt org-agenda-no-heading-message)
4870 (goto-char (match-end 0))
4871 (setq pos1 (match-beginning 0))
4872 (setq tags (org-get-tags-at pos1))
4873 (setq head (buffer-substring-no-properties
4874 (point)
4875 (progn (skip-chars-forward "^\r\n")
4876 (point))))
4877 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4878 (setq timestr
4879 (concat (substring s (match-beginning 1)) " "))
4880 (setq timestr 'time))
4881 (setq txt (org-format-agenda-item
4882 (if (= diff 0)
4883 (car org-agenda-deadline-leaders)
4884 (if (functionp
4885 (nth 1 org-agenda-deadline-leaders))
4886 (funcall
4887 (nth 1 org-agenda-deadline-leaders)
4888 diff date)
4889 (format (nth 1 org-agenda-deadline-leaders)
4890 diff)))
4891 head category tags
4892 (if (not (= diff 0)) nil timestr)))))
20908596
CD
4893 (when txt
4894 (setq face (org-agenda-deadline-face dfrac wdays))
4895 (org-add-props txt props
4896 'org-marker (org-agenda-new-marker pos)
4897 'org-hd-marker (org-agenda-new-marker pos1)
4898 'priority (+ (- diff)
4899 (org-get-priority txt))
4900 'org-category category
621f83e4 4901 'todo-state todo-state
20908596
CD
4902 'type (if upcomingp "upcoming-deadline" "deadline")
4903 'date (if upcomingp date d2)
c8d0cf5c
CD
4904 'face (if donep 'org-agenda-done face)
4905 'undone-face face 'done-face 'org-agenda-done)
20908596
CD
4906 (push txt ee))))))
4907 (nreverse ee)))
4908
4909(defun org-agenda-deadline-face (fraction &optional wdays)
4910 "Return the face to displaying a deadline item.
4911FRACTION is what fraction of the head-warning time has passed."
4912 (if (equal wdays 0) (setq fraction 1.))
4913 (let ((faces org-agenda-deadline-faces) f)
4914 (catch 'exit
4915 (while (setq f (pop faces))
4916 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
4917
54a0dee5 4918(defun org-agenda-get-scheduled (&optional deadline-results)
20908596
CD
4919 "Return the scheduled information for agenda display."
4920 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
4921 'org-todo-regexp org-todo-regexp
b349f79f 4922 'org-complex-heading-regexp org-complex-heading-regexp
c8d0cf5c 4923 'done-face 'org-agenda-done
20908596 4924 'mouse-face 'highlight
20908596
CD
4925 'help-echo
4926 (format "mouse-2 or RET jump to org file %s"
4927 (abbreviate-file-name buffer-file-name))))
4928 (regexp org-scheduled-time-regexp)
621f83e4 4929 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596 4930 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
54a0dee5
CD
4931 mm
4932 (deadline-position-alist
4933 (mapcar (lambda (a) (and (setq mm (get-text-property
8bfe682a
CD
4934 0 'org-hd-marker a))
4935 (cons (marker-position mm) a)))
54a0dee5 4936 deadline-results))
621f83e4 4937 d2 diff pos pos1 category tags donep
8bfe682a 4938 ee txt head pastschedp todo-state face timestr s habitp)
20908596
CD
4939 (goto-char (point-min))
4940 (while (re-search-forward regexp nil t)
4941 (catch :skip
4942 (org-agenda-skip)
4943 (setq s (match-string 1)
c8d0cf5c 4944 txt nil
20908596
CD
4945 pos (1- (match-beginning 1))
4946 d2 (org-time-string-to-absolute
4947 (match-string 1) d1 'past
4948 org-agenda-repeating-timestamp-show-all)
4949 diff (- d2 d1))
4950 (setq pastschedp (and todayp (< diff 0)))
4951 ;; When to show a scheduled item in the calendar:
4952 ;; If it is on or past the date.
8bfe682a
CD
4953 (when (or (and (< diff 0)
4954 (< (abs diff) org-scheduled-past-days)
4955 (and todayp (not org-agenda-only-exact-dates)))
4956 (= diff 0))
4957 (save-excursion
4958 (setq todo-state (org-get-todo-state))
4959 (setq donep (member todo-state org-done-keywords))
4960 (setq habitp (and (functionp 'org-is-habit-p)
4961 (org-is-habit-p)))
4962 (if (and donep
4963 (or habitp org-agenda-skip-scheduled-if-done
4964 (not (= diff 0))))
4965 (setq txt nil)
4966 (setq category (org-get-category))
4967 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4968 (setq txt org-agenda-no-heading-message)
4969 (goto-char (match-end 0))
4970 (setq pos1 (match-beginning 0))
4971 (if habitp
4972 (if (or (not org-habit-show-habits)
4973 (and (not todayp)
4974 org-habit-show-habits-only-for-today))
4975 (throw :skip nil))
54a0dee5
CD
4976 (if (and
4977 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
4978 (and org-agenda-skip-scheduled-if-deadline-is-shown
4979 pastschedp))
4980 (setq mm (assoc pos1 deadline-position-alist)))
8bfe682a
CD
4981 (throw :skip nil)))
4982 (setq tags (org-get-tags-at))
4983 (setq head (buffer-substring-no-properties
4984 (point)
4985 (progn (skip-chars-forward "^\r\n") (point))))
4986 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4987 (setq timestr
4988 (concat (substring s (match-beginning 1)) " "))
4989 (setq timestr 'time))
4990 (setq txt (org-format-agenda-item
4991 (if (= diff 0)
4992 (car org-agenda-scheduled-leaders)
4993 (format (nth 1 org-agenda-scheduled-leaders)
4994 (- 1 diff)))
4995 head category tags
4996 (if (not (= diff 0)) nil timestr)
4997 nil nil habitp))))
4998 (when txt
4999 (setq face
5000 (cond
5001 ((and (not habitp) pastschedp)
5002 'org-scheduled-previously)
5003 (todayp 'org-scheduled-today)
5004 (t 'org-scheduled))
5005 habitp (and habitp (org-habit-parse-todo)))
5006 (org-add-props txt props
5007 'undone-face face
5008 'face (if donep 'org-agenda-done face)
5009 'org-marker (org-agenda-new-marker pos)
5010 'org-hd-marker (org-agenda-new-marker pos1)
5011 'type (if pastschedp "past-scheduled" "scheduled")
5012 'date (if pastschedp d2 date)
5013 'priority (if habitp
5014 (org-habit-get-priority habitp)
5015 (+ 94 (- 5 diff) (org-get-priority txt)))
5016 'org-category category
5017 'org-habit-p habitp
5018 'todo-state todo-state)
5019 (push txt ee))))))
20908596
CD
5020 (nreverse ee)))
5021
5022(defun org-agenda-get-blocks ()
5023 "Return the date-range information for agenda display."
5024 (let* ((props (list 'face nil
5025 'org-not-done-regexp org-not-done-regexp
5026 'org-todo-regexp org-todo-regexp
b349f79f 5027 'org-complex-heading-regexp org-complex-heading-regexp
20908596 5028 'mouse-face 'highlight
20908596
CD
5029 'help-echo
5030 (format "mouse-2 or RET jump to org file %s"
5031 (abbreviate-file-name buffer-file-name))))
5032 (regexp org-tr-regexp)
5033 (d0 (calendar-absolute-from-gregorian date))
621f83e4 5034 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
c8d0cf5c 5035 head donep)
20908596
CD
5036 (goto-char (point-min))
5037 (while (re-search-forward regexp nil t)
5038 (catch :skip
5039 (org-agenda-skip)
5040 (setq pos (point))
5041 (setq timestr (match-string 0)
5042 s1 (match-string 1)
5043 s2 (match-string 2)
5044 d1 (time-to-days (org-time-string-to-time s1))
5045 d2 (time-to-days (org-time-string-to-time s2)))
5046 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5047 ;; Only allow days between the limits, because the normal
5048 ;; date stamps will catch the limits.
5049 (save-excursion
c8d0cf5c
CD
5050 (setq todo-state (org-get-todo-state))
5051 (setq donep (member todo-state org-done-keywords))
5052 (if (and donep org-agenda-skip-timestamp-if-done)
5053 (throw :skip t))
20908596
CD
5054 (setq marker (org-agenda-new-marker (point)))
5055 (setq category (org-get-category))
c8d0cf5c
CD
5056 (if (not (re-search-backward "^\\*+ " nil t))
5057 (setq txt org-agenda-no-heading-message)
5058 (goto-char (match-beginning 0))
5059 (setq hdmarker (org-agenda-new-marker (point)))
5060 (setq tags (org-get-tags-at))
5061 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5062 (setq head (match-string 1))
86fbb8ca
CD
5063 (let ((remove-re
5064 (if org-agenda-remove-timeranges-from-blocks
5065 (concat
5066 "<" (regexp-quote s1) ".*?>"
5067 "--"
5068 "<" (regexp-quote s2) ".*?>")
5069 nil)))
5070 (setq txt (org-format-agenda-item
5071 (format
5072 (nth (if (= d1 d2) 0 1)
5073 org-agenda-timerange-leaders)
5074 (1+ (- d0 d1)) (1+ (- d2 d1)))
5075 head category tags
5076 timestr nil remove-re))))
20908596
CD
5077 (org-add-props txt props
5078 'org-marker marker 'org-hd-marker hdmarker
5079 'type "block" 'date date
621f83e4 5080 'todo-state todo-state
20908596
CD
5081 'priority (org-get-priority txt) 'org-category category)
5082 (push txt ee)))
5083 (goto-char pos)))
5084 ;; Sort the entries by expiration date.
5085 (nreverse ee)))
5086
5087;;; Agenda presentation and sorting
5088
5089(defvar org-prefix-has-time nil
5090 "A flag, set by `org-compile-prefix-format'.
5091The flag is set if the currently compiled format contains a `%t'.")
5092(defvar org-prefix-has-tag nil
5093 "A flag, set by `org-compile-prefix-format'.
5094The flag is set if the currently compiled format contains a `%T'.")
5095(defvar org-prefix-has-effort nil
5096 "A flag, set by `org-compile-prefix-format'.
5097The flag is set if the currently compiled format contains a `%e'.")
8d642074 5098(defvar org-prefix-category-length nil
86fbb8ca 5099 "Used by `org-compile-prefix-format' to remember the category field width.")
8bfe682a 5100(defvar org-prefix-category-max-length nil
86fbb8ca 5101 "Used by `org-compile-prefix-format' to remember the category field width.")
20908596 5102
acedf35c
CD
5103(defun org-agenda-get-category-icon (category)
5104 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5105 (dolist (entry org-agenda-category-icon-alist)
5106 (when (org-string-match-p (car entry) category)
5107 (if (listp (cadr entry))
5108 (return (cadr entry))
5109 (return (apply 'create-image (cdr entry)))))))
5110
20908596 5111(defun org-format-agenda-item (extra txt &optional category tags dotime
8bfe682a 5112 noprefix remove-re habitp)
20908596
CD
5113 "Format TXT to be inserted into the agenda buffer.
5114In particular, it adds the prefix and corresponding text properties. EXTRA
5115must be a string and replaces the `%s' specifier in the prefix format.
5116CATEGORY (string, symbol or nil) may be used to overrule the default
5117category taken from local variable or file name. It will replace the `%c'
5118specifier in the format. DOTIME, when non-nil, indicates that a
5119time-of-day should be extracted from TXT for sorting of this entry, and for
5120the `%t' specifier in the format. When DOTIME is a string, this string is
5121searched for a time before TXT is. NOPREFIX is a flag and indicates that
5122only the correctly processes TXT should be returned - this is used by
5123`org-agenda-change-all-lines'. TAGS can be the tags of the headline.
5124Any match of REMOVE-RE will be removed from TXT."
5125 (save-match-data
5126 ;; Diary entries sometimes have extra whitespace at the beginning
5127 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5dec9555
CD
5128
5129 ;; Fix the tags part in txt
5130 (setq txt (org-agenda-fix-displayed-tags
5131 txt tags
5132 org-agenda-show-inherited-tags
5133 org-agenda-hide-tags-regexp))
20908596 5134 (let* ((category (or category
acedf35c
CD
5135 (if (stringp org-category)
5136 org-category
5137 (and org-category (symbol-name org-category)))
20908596
CD
5138 (if buffer-file-name
5139 (file-name-sans-extension
5140 (file-name-nondirectory buffer-file-name))
5141 "")))
acedf35c
CD
5142 (category-icon (org-agenda-get-category-icon category))
5143 (category-icon (if category-icon
5144 (propertize " " 'display category-icon)
5145 ""))
20908596
CD
5146 ;; time, tag, effort are needed for the eval of the prefix format
5147 (tag (if tags (nth (1- (length tags)) tags) ""))
5148 time effort neffort
c8d0cf5c
CD
5149 (ts (if dotime (concat
5150 (if (stringp dotime) dotime "")
5151 (and org-agenda-search-headline-for-time txt))))
20908596 5152 (time-of-day (and dotime (org-get-time-of-day ts)))
8d642074 5153 stamp plain s0 s1 s2 t1 t2 rtn srp l
8bfe682a 5154 duration thecategory)
20908596
CD
5155 (and (org-mode-p) buffer-file-name
5156 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5157 (when (and dotime time-of-day)
5158 ;; Extract starting and ending time and move them to prefix
5159 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5160 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5161 (setq s0 (match-string 0 ts)
5162 srp (and stamp (match-end 3))
5163 s1 (match-string (if plain 1 2) ts)
5164 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5165
5166 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5167 ;; them, we might want to remove them there to avoid duplication.
5168 ;; The user can turn this off with a variable.
5169 (if (and org-prefix-has-time
5170 org-agenda-remove-times-when-in-prefix (or stamp plain)
5171 (string-match (concat (regexp-quote s0) " *") txt)
5172 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5173 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5174 (= (match-beginning 0) 0)
5175 t))
5176 (setq txt (replace-match "" nil nil txt))))
5177 ;; Normalize the time(s) to 24 hour
5178 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5179 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5180 ;; Compute the duration
5181 (when s1
5182 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
5183 (string-to-number (substring s1 3)))
5184 t2 (cond
5185 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
5186 (string-to-number (substring s2 3))))
5187 (org-agenda-default-appointment-duration
5188 (+ t1 org-agenda-default-appointment-duration))
5189 (t nil)))
5190 (setq duration (if t2 (- t2 t1)))))
5191
5192 (when (and s1 (not s2) org-agenda-default-appointment-duration
5193 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
5194 (let ((m (+ (string-to-number (match-string 2 s1))
5195 (* 60 (string-to-number (match-string 1 s1)))
5196 org-agenda-default-appointment-duration))
5197 h)
5198 (setq h (/ m 60) m (- m (* h 60)))
5199 (setq s2 (format "%02d:%02d" h m))))
5200
afe98dfa 5201 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
20908596
CD
5202 txt)
5203 ;; Tags are in the string
5204 (if (or (eq org-agenda-remove-tags t)
5205 (and org-agenda-remove-tags
5206 org-prefix-has-tag))
5207 (setq txt (replace-match "" t t txt))
5208 (setq txt (replace-match
5209 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5210 (match-string 2 txt))
5211 t t txt))))
5212 (when (org-mode-p)
5213 (setq effort
5214 (condition-case nil
5215 (org-get-effort
5216 (or (get-text-property 0 'org-hd-marker txt)
5217 (get-text-property 0 'org-marker txt)))
5218 (error nil)))
5219 (when effort
5220 (setq neffort (org-hh:mm-string-to-minutes effort)
54a0dee5 5221 effort (setq effort (concat "[" effort "]" )))))
20908596
CD
5222
5223 (when remove-re
5224 (while (string-match remove-re txt)
5225 (setq txt (replace-match "" t t txt))))
5226
5227 ;; Create the final string
5228 (if noprefix
5229 (setq rtn txt)
5230 ;; Prepare the variables needed in the eval of the compiled format
acedf35c
CD
5231 (setq time (cond (s2 (concat
5232 (org-agenda-time-of-day-to-ampm-maybe s1)
5233 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5234 (if org-agenda-timegrid-use-ampm " ")))
5235 (s1 (concat
5236 (org-agenda-time-of-day-to-ampm-maybe s1)
5237 (if org-agenda-timegrid-use-ampm
5238 "........ "
5239 "......")))
20908596 5240 (t ""))
8bfe682a
CD
5241 extra (or (and (not habitp) extra) "")
5242 category (if (symbolp category) (symbol-name category) category)
5243 thecategory (copy-sequence category))
5244 (if (string-match org-bracket-link-regexp category)
5245 (progn
5246 (setq l (if (match-end 3)
5247 (- (match-end 3) (match-beginning 3))
5248 (- (match-end 1) (match-beginning 1))))
5249 (when (< l (or org-prefix-category-length 0))
5250 (setq category (copy-sequence category))
5251 (org-add-props category nil
5252 'extra-space (make-string
5253 (- org-prefix-category-length l 1) ?\ ))))
5254 (if (and org-prefix-category-max-length
5255 (>= (length category) org-prefix-category-max-length))
5256 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
20908596
CD
5257 ;; Evaluate the compiled format
5258 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
5259
5260 ;; And finally add the text properties
c8d0cf5c 5261 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
20908596 5262 (org-add-props rtn nil
8bfe682a 5263 'org-category (if thecategory (downcase thecategory) category)
ff4be292 5264 'tags (mapcar 'org-downcase-keep-props tags)
20908596
CD
5265 'org-highest-priority org-highest-priority
5266 'org-lowest-priority org-lowest-priority
5267 'prefix-length (- (length rtn) (length txt))
5268 'time-of-day time-of-day
5269 'duration duration
5270 'effort effort
5271 'effort-minutes neffort
5272 'txt txt
5273 'time time
5274 'extra extra
5275 'dotime dotime))))
5276
5dec9555
CD
5277(defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5278 "Remove tags string from TXT, and add a modified list of tags.
5279The modified list may contain inherited tags, and tags matched by
5280`org-agenda-hide-tags-regexp' will be removed."
5281 (when (or add-inherited hide-re)
afe98dfa 5282 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5dec9555 5283 (setq txt (substring txt 0 (match-beginning 0))))
ed21c5c8
CD
5284 (setq tags
5285 (delq nil
5286 (mapcar (lambda (tg)
5287 (if (or (and hide-re (string-match hide-re tg))
5288 (and (not add-inherited)
5289 (get-text-property 0 'inherited tg)))
5290 nil
5291 tg))
5292 tags)))
5dec9555 5293 (when tags
5dec9555
CD
5294 (let ((have-i (get-text-property 0 'inherited (car tags)))
5295 i)
5296 (setq txt (concat txt " :"
5297 (mapconcat
5298 (lambda (x)
5299 (setq i (get-text-property 0 'inherited x))
5300 (if (and have-i (not i))
5301 (progn
5302 (setq have-i nil)
5303 (concat ":" x))
5304 x))
5305 tags ":")
5306 (if have-i "::" ":"))))))
5307 txt)
ff4be292
CD
5308
5309(defun org-downcase-keep-props (s)
5310 (let ((props (text-properties-at 0 s)))
5311 (setq s (downcase s))
5312 (add-text-properties 0 (length s) props s)
5313 s))
5314
20908596
CD
5315(defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5316(defvar org-agenda-sorting-strategy-selected nil)
5317
5318(defun org-agenda-add-time-grid-maybe (list ndays todayp)
5319 (catch 'exit
5320 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5321 ((and todayp (member 'today (car org-agenda-time-grid))))
5322 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5323 ((member 'weekly (car org-agenda-time-grid)))
5324 (t (throw 'exit list)))
5325 (let* ((have (delq nil (mapcar
5326 (lambda (x) (get-text-property 1 'time-of-day x))
5327 list)))
5328 (string (nth 1 org-agenda-time-grid))
5329 (gridtimes (nth 2 org-agenda-time-grid))
5330 (req (car org-agenda-time-grid))
5331 (remove (member 'remove-match req))
5332 new time)
5333 (if (and (member 'require-timed req) (not have))
5334 ;; don't show empty grid
5335 (throw 'exit list))
5336 (while (setq time (pop gridtimes))
5337 (unless (and remove (member time have))
afe98dfa 5338 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
20908596
CD
5339 (push (org-format-agenda-item
5340 nil string "" nil
5341 (concat (substring time 0 -2) ":" (substring time -2)))
5342 new)
5343 (put-text-property
afe98dfa 5344 2 (length (car new)) 'face 'org-time-grid (car new))))
20908596
CD
5345 (if (member 'time-up org-agenda-sorting-strategy-selected)
5346 (append new list)
5347 (append list new)))))
5348
5349(defun org-compile-prefix-format (key)
5350 "Compile the prefix format into a Lisp form that can be evaluated.
5351The resulting form is returned and stored in the variable
5352`org-prefix-format-compiled'."
5353 (setq org-prefix-has-time nil org-prefix-has-tag nil
8d642074 5354 org-prefix-category-length nil org-prefix-has-effort nil)
20908596
CD
5355 (let ((s (cond
5356 ((stringp org-agenda-prefix-format)
5357 org-agenda-prefix-format)
5358 ((assq key org-agenda-prefix-format)
5359 (cdr (assq key org-agenda-prefix-format)))
5360 (t " %-12:c%?-12t% s")))
5361 (start 0)
5362 varform vars var e c f opt)
acedf35c 5363 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\)"
20908596
CD
5364 s start)
5365 (setq var (cdr (assoc (match-string 4 s)
5366 '(("c" . category) ("t" . time) ("s" . extra)
acedf35c 5367 ("i" . category-icon) ("T" . tag) ("e" . effort))))
20908596
CD
5368 c (or (match-string 3 s) "")
5369 opt (match-beginning 1)
5370 start (1+ (match-beginning 0)))
5371 (if (equal var 'time) (setq org-prefix-has-time t))
5372 (if (equal var 'tag) (setq org-prefix-has-tag t))
5373 (if (equal var 'effort) (setq org-prefix-has-effort t))
5374 (setq f (concat "%" (match-string 2 s) "s"))
8bfe682a
CD
5375 (when (equal var 'category)
5376 (setq org-prefix-category-length
5377 (floor (abs (string-to-number (match-string 2 s)))))
5378 (setq org-prefix-category-max-length
5379 (let ((x (match-string 2 s)))
5380 (save-match-data
5381 (if (string-match "\\.[0-9]+" x)
5382 (string-to-number (substring (match-string 0 x) 1)))))))
20908596
CD
5383 (if opt
5384 (setq varform
5385 `(if (equal "" ,var)
5386 ""
5387 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
8d642074 5388 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var))))))
20908596
CD
5389 (setq s (replace-match "%s" t nil s))
5390 (push varform vars))
5391 (setq vars (nreverse vars))
5392 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5393
5394(defun org-set-sorting-strategy (key)
5395 (if (symbolp (car org-agenda-sorting-strategy))
5396 ;; the old format
5397 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5398 (setq org-agenda-sorting-strategy-selected
5399 (or (cdr (assq key org-agenda-sorting-strategy))
5400 (cdr (assq 'agenda org-agenda-sorting-strategy))
5401 '(time-up category-keep priority-down)))))
5402
5403(defun org-get-time-of-day (s &optional string mod24)
5404 "Check string S for a time of day.
5405If found, return it as a military time number between 0 and 2400.
5406If not found, return nil.
5407The optional STRING argument forces conversion into a 5 character wide string
5408HH:MM."
5409 (save-match-data
5410 (when
5411 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5412 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5413 (let* ((h (string-to-number (match-string 1 s)))
5414 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5415 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5416 (am-p (equal ampm "am"))
5417 (h1 (cond ((not ampm) h)
5418 ((= h 12) (if am-p 0 12))
5419 (t (+ h (if am-p 0 12)))))
5420 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5421 (mod h1 24) h1))
5422 (t0 (+ (* 100 h2) m))
5423 (t1 (concat (if (>= h1 24) "+" " ")
ed21c5c8
CD
5424 (if (and org-agenda-time-leading-zero
5425 (< t0 1000)) "0" "")
20908596
CD
5426 (if (< t0 100) "0" "")
5427 (if (< t0 10) "0" "")
5428 (int-to-string t0))))
5429 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5430
afe98dfa
CD
5431(defvar org-agenda-before-sorting-filter-function nil
5432 "Function to be applied to agenda items prior to sorting.
5433Prior to sorting also means just before they are inserted into the agenda.
5434
5435To aid sorting, you may revisit the original entries and add more text
5436properties which will later be used by the sorting functions.
5437
5438The function should take a string argument, an agenda line.
5439It has access to the text properties in that line, which contain among
5440other things, the property `org-hd-marker' that points to the entry
5441where the line comes from. Note that not all lines going into the agenda
5442have this property, only most.
5443
5444The function should return the modified string. It is probably best
5445to ONLY change text properties.
5446
5447You can also use this function as a filter, by returning nil for lines
5448you don't want to have in the agenda at all. For this application, you
5449could bind the variable in the options section of a custom command.")
5450
20908596
CD
5451(defun org-finalize-agenda-entries (list &optional nosort)
5452 "Sort and concatenate the agenda items."
5453 (setq list (mapcar 'org-agenda-highlight-todo list))
5454 (if nosort
5455 list
afe98dfa
CD
5456 (when org-agenda-before-sorting-filter-function
5457 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
20908596
CD
5458 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5459
5460(defun org-agenda-highlight-todo (x)
621f83e4 5461 (let ((org-done-keywords org-done-keywords-for-agenda)
ed21c5c8 5462 (case-fold-search nil)
621f83e4 5463 re pl)
20908596
CD
5464 (if (eq x 'line)
5465 (save-excursion
5466 (beginning-of-line 1)
8d642074
CD
5467 (setq re (org-get-at-bol 'org-todo-regexp))
5468 (goto-char (+ (point) (or (org-get-at-bol 'prefix-length) 0)))
621f83e4 5469 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
c8d0cf5c 5470 (add-text-properties (match-beginning 0) (match-end 1)
621f83e4 5471 (list 'face (org-get-todo-face 1)))
20908596
CD
5472 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5473 (delete-region (match-beginning 1) (1- (match-end 0)))
5474 (goto-char (match-beginning 1))
5475 (insert (format org-agenda-todo-keyword-format s)))))
5476 (setq re (concat (get-text-property 0 'org-todo-regexp x))
5477 pl (get-text-property 0 'prefix-length x))
5478 (when (and re
5479 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5480 x (or pl 0)) pl))
5481 (add-text-properties
5482 (or (match-end 1) (match-end 0)) (match-end 0)
5483 (list 'face (org-get-todo-face (match-string 2 x)))
5484 x)
86fbb8ca
CD
5485 (when (match-end 1)
5486 (setq x (concat (substring x 0 (match-end 1))
5487 (format org-agenda-todo-keyword-format
5488 (match-string 2 x))
5489 (org-add-props " " (text-properties-at 0 x))
5490 (substring x (match-end 3))))))
20908596
CD
5491 x)))
5492
5493(defsubst org-cmp-priority (a b)
5494 "Compare the priorities of string A and B."
5495 (let ((pa (or (get-text-property 1 'priority a) 0))
5496 (pb (or (get-text-property 1 'priority b) 0)))
5497 (cond ((> pa pb) +1)
5498 ((< pa pb) -1)
5499 (t nil))))
5500
5501(defsubst org-cmp-effort (a b)
5502 "Compare the priorities of string A and B."
5503 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5504 (ea (or (get-text-property 1 'effort-minutes a) def))
5505 (eb (or (get-text-property 1 'effort-minutes b) def)))
5506 (cond ((> ea eb) +1)
5507 ((< ea eb) -1)
5508 (t nil))))
5509
5510(defsubst org-cmp-category (a b)
5511 "Compare the string values of categories of strings A and B."
5512 (let ((ca (or (get-text-property 1 'org-category a) ""))
5513 (cb (or (get-text-property 1 'org-category b) "")))
5514 (cond ((string-lessp ca cb) -1)
5515 ((string-lessp cb ca) +1)
5516 (t nil))))
5517
621f83e4
CD
5518(defsubst org-cmp-todo-state (a b)
5519 "Compare the todo states of strings A and B."
c8d0cf5c
CD
5520 (let* ((ma (or (get-text-property 1 'org-marker a)
5521 (get-text-property 1 'org-hd-marker a)))
5522 (mb (or (get-text-property 1 'org-marker b)
5523 (get-text-property 1 'org-hd-marker b)))
5524 (fa (and ma (marker-buffer ma)))
5525 (fb (and mb (marker-buffer mb)))
5526 (todo-kwds
5527 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5528 (and fb (with-current-buffer fb org-todo-keywords-1))))
5529 (ta (or (get-text-property 1 'todo-state a) ""))
621f83e4 5530 (tb (or (get-text-property 1 'todo-state b) ""))
c8d0cf5c
CD
5531 (la (- (length (member ta todo-kwds))))
5532 (lb (- (length (member tb todo-kwds))))
ff4be292 5533 (donepa (member ta org-done-keywords-for-agenda))
621f83e4
CD
5534 (donepb (member tb org-done-keywords-for-agenda)))
5535 (cond ((and donepa (not donepb)) -1)
5536 ((and (not donepa) donepb) +1)
5537 ((< la lb) -1)
5538 ((< lb la) +1)
5539 (t nil))))
5540
86fbb8ca
CD
5541(defsubst org-cmp-alpha (a b)
5542 "Compare the headlines, alphabetically."
5543 (let* ((pla (get-text-property 0 'prefix-length a))
5544 (plb (get-text-property 0 'prefix-length b))
5545 (ta (and pla (substring a pla)))
5546 (tb (and plb (substring b plb))))
5547 (when pla
5548 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5549 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5550 (setq ta (substring ta (match-end 0))))
5551 (setq ta (downcase ta)))
5552 (when plb
5553 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5554 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5555 (setq tb (substring tb (match-end 0))))
5556 (setq tb (downcase tb)))
5557 (cond ((not ta) +1)
5558 ((not tb) -1)
5559 ((string-lessp ta tb) -1)
5560 ((string-lessp tb ta) +1)
5561 (t nil))))
5562
20908596 5563(defsubst org-cmp-tag (a b)
71d35b24 5564 "Compare the string values of the first tags of A and B."
20908596
CD
5565 (let ((ta (car (last (get-text-property 1 'tags a))))
5566 (tb (car (last (get-text-property 1 'tags b)))))
5567 (cond ((not ta) +1)
5568 ((not tb) -1)
5569 ((string-lessp ta tb) -1)
5570 ((string-lessp tb ta) +1)
5571 (t nil))))
5572
5573(defsubst org-cmp-time (a b)
5574 "Compare the time-of-day values of strings A and B."
5575 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5576 (ta (or (get-text-property 1 'time-of-day a) def))
5577 (tb (or (get-text-property 1 'time-of-day b) def)))
5578 (cond ((< ta tb) -1)
5579 ((< tb ta) +1)
5580 (t nil))))
5581
8bfe682a
CD
5582(defsubst org-cmp-habit-p (a b)
5583 "Compare the todo states of strings A and B."
5584 (let ((ha (get-text-property 1 'org-habit-p a))
5585 (hb (get-text-property 1 'org-habit-p b)))
5586 (cond ((and ha (not hb)) -1)
5587 ((and (not ha) hb) +1)
5588 (t nil))))
5589
86fbb8ca
CD
5590(defsubst org-em (x y list) (or (memq x list) (memq y list)))
5591
20908596
CD
5592(defun org-entries-lessp (a b)
5593 "Predicate for sorting agenda entries."
5594 ;; The following variables will be used when the form is evaluated.
5595 ;; So even though the compiler complains, keep them.
86fbb8ca
CD
5596 (let* ((ss org-agenda-sorting-strategy-selected)
5597 (time-up (and (org-em 'time-up 'time-down ss)
5598 (org-cmp-time a b)))
5599 (time-down (if time-up (- time-up) nil))
5600 (priority-up (and (org-em 'priority-up 'priority-down ss)
5601 (org-cmp-priority a b)))
5602 (priority-down (if priority-up (- priority-up) nil))
5603 (effort-up (and (org-em 'effort-up 'effort-down ss)
5604 (org-cmp-effort a b)))
5605 (effort-down (if effort-up (- effort-up) nil))
5606 (category-up (and (or (org-em 'category-up 'category-down ss)
5607 (memq 'category-keep ss))
5608 (org-cmp-category a b)))
5609 (category-down (if category-up (- category-up) nil))
5610 (category-keep (if category-up +1 nil))
5611 (tag-up (and (org-em 'tag-up 'tag-down ss)
5612 (org-cmp-tag a b)))
5613 (tag-down (if tag-up (- tag-up) nil))
5614 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5615 (org-cmp-todo-state a b)))
c8d0cf5c 5616 (todo-state-down (if todo-state-up (- todo-state-up) nil))
86fbb8ca
CD
5617 (habit-up (and (org-em 'habit-up 'habit-down ss)
5618 (org-cmp-habit-p a b)))
5619 (habit-down (if habit-up (- habit-up) nil))
5620 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5621 (org-cmp-alpha a b)))
5622 (alpha-down (if alpha-up (- alpha-up) nil))
afe98dfa 5623 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
c8d0cf5c 5624 user-defined-up user-defined-down)
afe98dfa 5625 (if (and need-user-cmp org-agenda-cmp-user-defined
c8d0cf5c
CD
5626 (functionp org-agenda-cmp-user-defined))
5627 (setq user-defined-up
5628 (funcall org-agenda-cmp-user-defined a b)
5629 user-defined-down (if user-defined-up (- user-defined-up) nil)))
20908596
CD
5630 (cdr (assoc
5631 (eval (cons 'or org-agenda-sorting-strategy-selected))
5632 '((-1 . t) (1 . nil) (nil . nil))))))
5633
5634;;; Agenda restriction lock
5635
86fbb8ca 5636(defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
8bfe682a 5637 "Overlay to mark the headline to which agenda commands are restricted.")
86fbb8ca
CD
5638(overlay-put org-agenda-restriction-lock-overlay
5639 'face 'org-agenda-restriction-lock)
5640(overlay-put org-agenda-restriction-lock-overlay
5641 'help-echo "Agendas are currently limited to this subtree.")
20908596
CD
5642(org-detach-overlay org-agenda-restriction-lock-overlay)
5643
5644(defun org-agenda-set-restriction-lock (&optional type)
5645 "Set restriction lock for agenda, to current subtree or file.
5646Restriction will be the file if TYPE is `file', or if type is the
5647universal prefix '(4), or if the cursor is before the first headline
5648in the file. Otherwise, restriction will be to the current subtree."
5649 (interactive "P")
5650 (and (equal type '(4)) (setq type 'file))
5651 (setq type (cond
5652 (type type)
5653 ((org-at-heading-p) 'subtree)
5654 ((condition-case nil (org-back-to-heading t) (error nil))
5655 'subtree)
5656 (t 'file)))
5657 (if (eq type 'subtree)
5658 (progn
5659 (setq org-agenda-restrict t)
5660 (setq org-agenda-overriding-restriction 'subtree)
5661 (put 'org-agenda-files 'org-restrict
5662 (list (buffer-file-name (buffer-base-buffer))))
5663 (org-back-to-heading t)
86fbb8ca 5664 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
20908596
CD
5665 (move-marker org-agenda-restrict-begin (point))
5666 (move-marker org-agenda-restrict-end
5667 (save-excursion (org-end-of-subtree t)))
5668 (message "Locking agenda restriction to subtree"))
5669 (put 'org-agenda-files 'org-restrict
5670 (list (buffer-file-name (buffer-base-buffer))))
5671 (setq org-agenda-restrict nil)
5672 (setq org-agenda-overriding-restriction 'file)
5673 (move-marker org-agenda-restrict-begin nil)
5674 (move-marker org-agenda-restrict-end nil)
5675 (message "Locking agenda restriction to file"))
5676 (setq current-prefix-arg nil)
5677 (org-agenda-maybe-redo))
5678
5679(defun org-agenda-remove-restriction-lock (&optional noupdate)
5680 "Remove the agenda restriction lock."
5681 (interactive "P")
5682 (org-detach-overlay org-agenda-restriction-lock-overlay)
5683 (org-detach-overlay org-speedbar-restriction-lock-overlay)
5684 (setq org-agenda-overriding-restriction nil)
5685 (setq org-agenda-restrict nil)
5686 (put 'org-agenda-files 'org-restrict nil)
5687 (move-marker org-agenda-restrict-begin nil)
5688 (move-marker org-agenda-restrict-end nil)
5689 (setq current-prefix-arg nil)
5690 (message "Agenda restriction lock removed")
5691 (or noupdate (org-agenda-maybe-redo)))
5692
5693(defun org-agenda-maybe-redo ()
5694 "If there is any window showing the agenda view, update it."
5695 (let ((w (get-buffer-window org-agenda-buffer-name t))
5696 (w0 (selected-window)))
5697 (when w
5698 (select-window w)
5699 (org-agenda-redo)
5700 (select-window w0)
5701 (if org-agenda-overriding-restriction
5702 (message "Agenda view shifted to new %s restriction"
5703 org-agenda-overriding-restriction)
5704 (message "Agenda restriction lock removed")))))
5705
5706;;; Agenda commands
5707
5708(defun org-agenda-check-type (error &rest types)
5709 "Check if agenda buffer is of allowed type.
5710If ERROR is non-nil, throw an error, otherwise just return nil."
5711 (if (memq org-agenda-type types)
5712 t
5713 (if error
5714 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
5715 nil)))
5716
5717(defun org-agenda-quit ()
5718 "Exit agenda by removing the window or the buffer."
5719 (interactive)
5720 (if org-agenda-columns-active
5721 (org-columns-quit)
5722 (let ((buf (current-buffer)))
8d642074
CD
5723 (if (eq org-agenda-window-setup 'other-frame)
5724 (progn
5725 (kill-buffer buf)
5726 (org-agenda-reset-markers)
5727 (org-columns-remove-overlays)
5728 (setq org-agenda-archives-mode nil)
5729 (delete-frame))
5730 (and (not (eq org-agenda-window-setup 'current-window))
5731 (not (one-window-p))
5732 (delete-window))
5733 (kill-buffer buf)
5734 (org-agenda-reset-markers)
5735 (org-columns-remove-overlays)
5736 (setq org-agenda-archives-mode nil)))
20908596
CD
5737 ;; Maybe restore the pre-agenda window configuration.
5738 (and org-agenda-restore-windows-after-quit
5739 (not (eq org-agenda-window-setup 'other-frame))
5740 org-pre-agenda-window-conf
5741 (set-window-configuration org-pre-agenda-window-conf))))
5742
5743(defun org-agenda-exit ()
5744 "Exit agenda by removing the window or the buffer.
5745Also kill all Org-mode buffers which have been loaded by `org-agenda'.
5746Org-mode buffers visited directly by the user will not be touched."
5747 (interactive)
5748 (org-release-buffers org-agenda-new-buffers)
5749 (setq org-agenda-new-buffers nil)
5750 (org-agenda-quit))
5751
5752(defun org-agenda-execute (arg)
86fbb8ca
CD
5753 "Execute another agenda command, keeping same window.
5754So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
5755in the agenda."
20908596
CD
5756 (interactive "P")
5757 (let ((org-agenda-window-setup 'current-window))
5758 (org-agenda arg)))
5759
20908596
CD
5760(defun org-agenda-redo ()
5761 "Rebuild Agenda.
5762When this is the global TODO list, a prefix argument will be interpreted."
5763 (interactive)
5764 (let* ((org-agenda-keep-modes t)
71d35b24 5765 (filter org-agenda-filter)
c8d0cf5c 5766 (preset (get 'org-agenda-filter :preset-filter))
acedf35c 5767 (org-agenda-filter-while-redo (or filter preset))
20908596
CD
5768 (cols org-agenda-columns-active)
5769 (line (org-current-line))
5770 (window-line (- line (org-current-line (window-start))))
5771 (lprops (get 'org-agenda-redo-command 'org-lprops)))
c8d0cf5c 5772 (put 'org-agenda-filter :preset-filter nil)
20908596
CD
5773 (and cols (org-columns-quit))
5774 (message "Rebuilding agenda buffer...")
5775 (org-let lprops '(eval org-agenda-redo-command))
5776 (setq org-agenda-undo-list nil
5777 org-agenda-pending-undo-list nil)
5778 (message "Rebuilding agenda buffer...done")
c8d0cf5c
CD
5779 (put 'org-agenda-filter :preset-filter preset)
5780 (and (or filter preset) (org-agenda-filter-apply filter))
20908596 5781 (and cols (interactive-p) (org-agenda-columns))
54a0dee5 5782 (org-goto-line line)
20908596
CD
5783 (recenter window-line)))
5784
71d35b24 5785
621f83e4 5786(defvar org-global-tags-completion-table nil)
71d35b24
CD
5787(defvar org-agenda-filter-form nil)
5788(defun org-agenda-filter-by-tag (strip &optional char narrow)
621f83e4
CD
5789 "Keep only those lines in the agenda buffer that have a specific tag.
5790The tag is selected with its fast selection letter, as configured.
71d35b24
CD
5791With prefix argument STRIP, remove all lines that do have the tag.
5792A lisp caller can specify CHAR. NARROW means that the new tag should be
5793used to narrow the search - the interactive user can also press `-' or `+'
5794to switch to narrowing."
621f83e4 5795 (interactive "P")
71d35b24 5796 (let* ((alist org-tag-alist-for-agenda)
8bfe682a
CD
5797 (tag-chars (mapconcat
5798 (lambda (x) (if (and (not (symbolp (car x)))
5799 (cdr x))
5800 (char-to-string (cdr x))
5801 ""))
5802 alist ""))
5803 (efforts (org-split-string
5804 (or (cdr (assoc (concat org-effort-property "_ALL")
5805 org-global-properties))
5806 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
5807 (effort-op org-agenda-filter-effort-default-operator)
5808 (effort-prompt "")
5809 (inhibit-read-only t)
5810 (current org-agenda-filter)
ed21c5c8 5811 a n tag)
71d35b24 5812 (unless char
ff4be292 5813 (message
8bfe682a
CD
5814 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
5815 (if narrow "Narrow" "Filter") tag-chars
5816 (if org-agenda-auto-exclude-function "[RET], " ""))
71d35b24
CD
5817 (setq char (read-char)))
5818 (when (member char '(?+ ?-))
5819 ;; Narrowing down
5820 (cond ((equal char ?-) (setq strip t narrow t))
5821 ((equal char ?+) (setq strip nil narrow t)))
ff4be292 5822 (message
71d35b24
CD
5823 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
5824 (setq char (read-char)))
c8d0cf5c 5825 (when (member char '(?< ?> ?= ??))
71d35b24
CD
5826 ;; An effort operator
5827 (setq effort-op (char-to-string char))
71d35b24 5828 (setq alist nil) ; to make sure it will be interpreted as effort.
c8d0cf5c
CD
5829 (unless (equal char ??)
5830 (loop for i from 0 to 9 do
5831 (setq effort-prompt
5832 (concat
5833 effort-prompt " ["
5834 (if (= i 9) "0" (int-to-string (1+ i)))
5835 "]" (nth i efforts))))
5836 (message "Effort%s: %s " effort-op effort-prompt)
5837 (setq char (read-char))
5838 (when (or (< char ?0) (> char ?9))
5839 (error "Need 1-9,0 to select effort" ))))
71d35b24
CD
5840 (when (equal char ?\t)
5841 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
5842 (org-set-local 'org-global-tags-completion-table
5843 (org-global-tags-completion-table)))
5844 (let ((completion-ignore-case t))
54a0dee5 5845 (setq tag (org-icompleting-read
71d35b24
CD
5846 "Tag: " org-global-tags-completion-table))))
5847 (cond
8bfe682a
CD
5848 ((equal char ?\r)
5849 (org-agenda-filter-by-tag-show-all)
5850 (when org-agenda-auto-exclude-function
5851 (setq org-agenda-filter '())
ed21c5c8
CD
5852 (dolist (tag (org-agenda-get-represented-tags))
5853 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
8bfe682a
CD
5854 (if modifier
5855 (push modifier org-agenda-filter))))
5856 (if (not (null org-agenda-filter))
5857 (org-agenda-filter-apply org-agenda-filter))))
c8d0cf5c
CD
5858 ((equal char ?/)
5859 (org-agenda-filter-by-tag-show-all)
5860 (when (get 'org-agenda-filter :preset-filter)
5861 (org-agenda-filter-apply org-agenda-filter)))
71d35b24
CD
5862 ((or (equal char ?\ )
5863 (setq a (rassoc char alist))
5864 (and (>= char ?0) (<= char ?9)
5865 (setq n (if (= char ?0) 9 (- char ?0 1))
5866 tag (concat effort-op (nth n efforts))
5867 a (cons tag nil)))
c8d0cf5c
CD
5868 (and (= char ??)
5869 (setq tag "?eff")
5870 a (cons tag nil))
71d35b24
CD
5871 (and tag (setq a (cons tag nil))))
5872 (org-agenda-filter-by-tag-show-all)
5873 (setq tag (car a))
5874 (setq org-agenda-filter
5875 (cons (concat (if strip "-" "+") tag)
5876 (if narrow current nil)))
5877 (org-agenda-filter-apply org-agenda-filter))
5878 (t (error "Invalid tag selection character %c" char)))))
5879
ed21c5c8
CD
5880(defun org-agenda-get-represented-tags ()
5881 "Get a list of all tags currently represented in the agenda."
5882 (let (p tags)
5883 (save-excursion
5884 (goto-char (point-min))
5885 (while (setq p (next-single-property-change (point) 'tags))
5886 (goto-char p)
5887 (mapc (lambda (x) (add-to-list 'tags x))
5888 (get-text-property (point) 'tags))))
5889 tags))
5890
71d35b24
CD
5891(defun org-agenda-filter-by-tag-refine (strip &optional char)
5892 "Refine the current filter. See `org-agenda-filter-by-tag."
5893 (interactive "P")
5894 (org-agenda-filter-by-tag strip char 'refine))
5895
5896(defun org-agenda-filter-make-matcher ()
5897 "Create the form that tests a line for the agenda filter."
5898 (let (f f1)
c8d0cf5c
CD
5899 (dolist (x (append (get 'org-agenda-filter :preset-filter)
5900 org-agenda-filter))
71d35b24 5901 (if (member x '("-" "+"))
8bfe682a 5902 (setq f1 (if (equal x "-") 'tags '(not tags)))
c8d0cf5c 5903 (if (string-match "[<=>?]" x)
71d35b24
CD
5904 (setq f1 (org-agenda-filter-effort-form x))
5905 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
5906 (if (equal (string-to-char x) ?-)
5907 (setq f1 (list 'not f1))))
5908 (push f1 f))
5909 (cons 'and (nreverse f))))
5910
5911(defun org-agenda-filter-effort-form (e)
5912 "Return the form to compare the effort of the current line with what E says.
86fbb8ca 5913E looks like \"+<2:25\"."
71d35b24
CD
5914 (let (op)
5915 (setq e (substring e 1))
5916 (setq op (string-to-char e) e (substring e 1))
c8d0cf5c
CD
5917 (setq op (cond ((equal op ?<) '<=)
5918 ((equal op ?>) '>=)
5919 ((equal op ??) op)
5920 (t '=)))
71d35b24
CD
5921 (list 'org-agenda-compare-effort (list 'quote op)
5922 (org-hh:mm-string-to-minutes e))))
5923
5924(defun org-agenda-compare-effort (op value)
5925 "Compare the effort of the current line with VALUE, using OP.
5926If the line does not have an effort defined, return nil."
8d642074 5927 (let ((eff (org-get-at-bol 'effort-minutes)))
c8d0cf5c
CD
5928 (if (equal op ??)
5929 (not eff)
5930 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
5931 value))))
71d35b24
CD
5932
5933(defun org-agenda-filter-apply (filter)
5934 "Set FILTER as the new agenda filter and apply it."
5935 (let (tags)
5936 (setq org-agenda-filter filter
5937 org-agenda-filter-form (org-agenda-filter-make-matcher))
5938 (org-agenda-set-mode-name)
5939 (save-excursion
5940 (goto-char (point-min))
5941 (while (not (eobp))
8d642074 5942 (if (org-get-at-bol 'org-marker)
71d35b24 5943 (progn
8d642074 5944 (setq tags (org-get-at-bol 'tags)) ; used in eval
71d35b24
CD
5945 (if (not (eval org-agenda-filter-form))
5946 (org-agenda-filter-by-tag-hide-line))
5947 (beginning-of-line 2))
afe98dfa
CD
5948 (beginning-of-line 2))))
5949 (if (get-char-property (point) 'invisible)
5950 (org-agenda-previous-line))))
621f83e4 5951
621f83e4
CD
5952(defun org-agenda-filter-by-tag-hide-line ()
5953 (let (ov)
86fbb8ca 5954 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
621f83e4 5955 (point-at-eol)))
86fbb8ca
CD
5956 (overlay-put ov 'invisible t)
5957 (overlay-put ov 'type 'tags-filter)
621f83e4
CD
5958 (push ov org-agenda-filter-overlays)))
5959
71d35b24
CD
5960(defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
5961 (setq pos (or pos (point)))
5962 (save-excursion
86fbb8ca
CD
5963 (dolist (ov (overlays-at pos))
5964 (when (and (overlay-get ov 'invisible)
5965 (eq (overlay-get ov 'type) 'tags-filter))
71d35b24 5966 (goto-char pos)
86fbb8ca
CD
5967 (if (< (overlay-start ov) (point-at-eol))
5968 (move-overlay ov (point-at-eol)
5969 (overlay-end ov)))))))
71d35b24 5970
621f83e4 5971(defun org-agenda-filter-by-tag-show-all ()
86fbb8ca 5972 (mapc 'delete-overlay org-agenda-filter-overlays)
71d35b24
CD
5973 (setq org-agenda-filter-overlays nil)
5974 (setq org-agenda-filter nil)
5975 (setq org-agenda-filter-form nil)
5976 (org-agenda-set-mode-name))
621f83e4 5977
20908596
CD
5978(defun org-agenda-manipulate-query-add ()
5979 "Manipulate the query by adding a search term with positive selection.
ed21c5c8 5980Positive selection means the term must be matched for selection of an entry."
20908596
CD
5981 (interactive)
5982 (org-agenda-manipulate-query ?\[))
5983(defun org-agenda-manipulate-query-subtract ()
5984 "Manipulate the query by adding a search term with negative selection.
ed21c5c8 5985Negative selection means term must not be matched for selection of an entry."
20908596
CD
5986 (interactive)
5987 (org-agenda-manipulate-query ?\]))
5988(defun org-agenda-manipulate-query-add-re ()
5989 "Manipulate the query by adding a search regexp with positive selection.
ed21c5c8 5990Positive selection means the regexp must match for selection of an entry."
20908596
CD
5991 (interactive)
5992 (org-agenda-manipulate-query ?\{))
5993(defun org-agenda-manipulate-query-subtract-re ()
5994 "Manipulate the query by adding a search regexp with negative selection.
ed21c5c8 5995Negative selection means regexp must not match for selection of an entry."
20908596
CD
5996 (interactive)
5997 (org-agenda-manipulate-query ?\}))
5998(defun org-agenda-manipulate-query (char)
5999 (cond
6000 ((memq org-agenda-type '(timeline agenda))
54a0dee5
CD
6001 (let ((org-agenda-include-inactive-timestamps t))
6002 (org-agenda-redo))
6003 (message "Display now includes inactive timestamps as well"))
20908596
CD
6004 ((eq org-agenda-type 'search)
6005 (org-add-to-string
6006 'org-agenda-query-string
ed21c5c8
CD
6007 (if org-agenda-last-search-view-search-was-boolean
6008 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6009 (?\{ . " +{}") (?\} . " -{}"))))
6010 " "))
20908596
CD
6011 (setq org-agenda-redo-command
6012 (list 'org-search-view
6013 org-todo-only
6014 org-agenda-query-string
6015 (+ (length org-agenda-query-string)
6016 (if (member char '(?\{ ?\})) 0 1))))
6017 (set-register org-agenda-query-register org-agenda-query-string)
6018 (org-agenda-redo))
6019 (t (error "Cannot manipulate query for %s-type agenda buffers"
6020 org-agenda-type))))
6021
6022(defun org-add-to-string (var string)
6023 (set var (concat (symbol-value var) string)))
6024
6025(defun org-agenda-goto-date (date)
6026 "Jump to DATE in agenda."
afe98dfa
CD
6027 (interactive (list (let ((org-read-date-prefer-future
6028 (eval org-agenda-jump-prefer-future)))
6029 (org-read-date))))
20908596
CD
6030 (org-agenda-list nil date))
6031
6032(defun org-agenda-goto-today ()
6033 "Go to today."
6034 (interactive)
6035 (org-agenda-check-type t 'timeline 'agenda)
6036 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6037 (cond
6038 (tdpos (goto-char tdpos))
6039 ((eq org-agenda-type 'agenda)
acedf35c
CD
6040 (let* ((sd (org-agenda-compute-starting-span
6041 (org-today) (or org-agenda-ndays org-agenda-span)))
20908596 6042 (org-agenda-overriding-arguments org-agenda-last-arguments))
acedf35c 6043 (setf (nth 1 org-agenda-overriding-arguments) sd)
20908596
CD
6044 (org-agenda-redo)
6045 (org-agenda-find-same-or-today-or-agenda)))
6046 (t (error "Cannot find today")))))
6047
6048(defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6049 (goto-char
6050 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6051 (text-property-any (point-min) (point-max) 'org-today t)
6052 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6053 (point-min))))
6054
6055(defun org-agenda-later (arg)
6056 "Go forward in time by thee current span.
6057With prefix ARG, go forward that many times the current span."
6058 (interactive "p")
6059 (org-agenda-check-type t 'agenda)
acedf35c 6060 (let* ((span org-agenda-current-span)
20908596
CD
6061 (sd org-starting-day)
6062 (greg (calendar-gregorian-from-absolute sd))
8d642074 6063 (cnt (org-get-at-bol 'org-day-cnt))
acedf35c 6064 greg2)
20908596
CD
6065 (cond
6066 ((eq span 'day)
acedf35c 6067 (setq sd (+ arg sd)))
20908596 6068 ((eq span 'week)
acedf35c 6069 (setq sd (+ (* 7 arg) sd)))
20908596
CD
6070 ((eq span 'month)
6071 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6072 sd (calendar-absolute-from-gregorian greg2))
acedf35c 6073 (setcar greg2 (1+ (car greg2))))
20908596
CD
6074 ((eq span 'year)
6075 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6076 sd (calendar-absolute-from-gregorian greg2))
acedf35c
CD
6077 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6078 (t
6079 (setq sd (+ (* span arg) sd))))
20908596 6080 (let ((org-agenda-overriding-arguments
acedf35c 6081 (list (car org-agenda-last-arguments) sd span t)))
20908596
CD
6082 (org-agenda-redo)
6083 (org-agenda-find-same-or-today-or-agenda cnt))))
6084
6085(defun org-agenda-earlier (arg)
6086 "Go backward in time by the current span.
6087With prefix ARG, go backward that many times the current span."
6088 (interactive "p")
6089 (org-agenda-later (- arg)))
6090
c8d0cf5c
CD
6091(defun org-agenda-view-mode-dispatch ()
6092 "Call one of the view mode commands."
6093 (interactive)
ed21c5c8
CD
6094 (message "View: [d]ay [w]eek [m]onth [y]ear [q]uit/abort
6095 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [E]ntryText
6096 [a]rch-trees [A]rch-files clock[R]eport include[D]iary")
c8d0cf5c
CD
6097 (let ((a (read-char-exclusive)))
6098 (case a
6099 (?d (call-interactively 'org-agenda-day-view))
6100 (?w (call-interactively 'org-agenda-week-view))
6101 (?m (call-interactively 'org-agenda-month-view))
6102 (?y (call-interactively 'org-agenda-year-view))
6103 (?l (call-interactively 'org-agenda-log-mode))
ed21c5c8 6104 (?L (org-agenda-log-mode '(4)))
54a0dee5 6105 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
c8d0cf5c
CD
6106 (?a (call-interactively 'org-agenda-archives-mode))
6107 (?A (org-agenda-archives-mode 'files))
54a0dee5
CD
6108 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6109 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
c8d0cf5c
CD
6110 (?G (call-interactively 'org-agenda-toggle-time-grid))
6111 (?D (call-interactively 'org-agenda-toggle-diary))
ed21c5c8 6112 (?\! (call-interactively 'org-agenda-toggle-deadlines))
54a0dee5
CD
6113 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6114 (org-agenda-check-type t 'timeline 'agenda)
6115 (org-agenda-redo))
6116 (message "Display now includes inactive timestamps as well"))
c8d0cf5c
CD
6117 (?q (message "Abort"))
6118 (otherwise (error "Invalid key" )))))
6119
20908596
CD
6120(defun org-agenda-day-view (&optional day-of-year)
6121 "Switch to daily view for agenda.
6122With argument DAY-OF-YEAR, switch to that day of the year."
6123 (interactive "P")
20908596
CD
6124 (org-agenda-change-time-span 'day day-of-year))
6125(defun org-agenda-week-view (&optional iso-week)
6126 "Switch to daily view for agenda.
6127With argument ISO-WEEK, switch to the corresponding ISO week.
6128If ISO-WEEK has more then 2 digits, only the last two encode the
6129week. Any digits before this encode a year. So 200712 means
6130week 12 of year 2007. Years in the range 1938-2037 can also be
6131written as 2-digit years."
6132 (interactive "P")
20908596
CD
6133 (org-agenda-change-time-span 'week iso-week))
6134(defun org-agenda-month-view (&optional month)
b349f79f 6135 "Switch to monthly view for agenda.
20908596
CD
6136With argument MONTH, switch to that month."
6137 (interactive "P")
6138 (org-agenda-change-time-span 'month month))
6139(defun org-agenda-year-view (&optional year)
b349f79f 6140 "Switch to yearly view for agenda.
20908596
CD
6141With argument YEAR, switch to that year.
6142If MONTH has more then 2 digits, only the last two encode the
6143month. Any digits before this encode a year. So 200712 means
6144December year 2007. Years in the range 1938-2037 can also be
6145written as 2-digit years."
6146 (interactive "P")
6147 (when year
6148 (setq year (org-small-year-to-year year)))
6149 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6150 (org-agenda-change-time-span 'year year)
6151 (error "Abort")))
6152
6153(defun org-agenda-change-time-span (span &optional n)
6154 "Change the agenda view to SPAN.
6155SPAN may be `day', `week', `month', `year'."
6156 (org-agenda-check-type t 'agenda)
acedf35c 6157 (if (and (not n) (equal org-agenda-current-span span))
20908596 6158 (error "Viewing span is already \"%s\"" span))
8d642074 6159 (let* ((sd (or (org-get-at-bol 'day)
20908596 6160 org-starting-day))
acedf35c 6161 (sd (org-agenda-compute-starting-span sd span n))
20908596 6162 (org-agenda-overriding-arguments
acedf35c 6163 (list (car org-agenda-last-arguments) sd span t)))
20908596
CD
6164 (org-agenda-redo)
6165 (org-agenda-find-same-or-today-or-agenda))
6166 (org-agenda-set-mode-name)
6167 (message "Switched to %s view" span))
6168
acedf35c
CD
6169(defun org-agenda-compute-starting-span (sd span &optional n)
6170 "Compute starting date for agenda.
20908596
CD
6171SPAN may be `day', `week', `month', `year'. The return value
6172is a cons cell with the starting date and the number of days,
6173so that the date SD will be in that range."
6174 (let* ((greg (calendar-gregorian-from-absolute sd))
6175 (dg (nth 1 greg))
6176 (mg (car greg))
acedf35c 6177 (yg (nth 2 greg)))
20908596
CD
6178 (cond
6179 ((eq span 'day)
6180 (when n
6181 (setq sd (+ (calendar-absolute-from-gregorian
6182 (list mg 1 yg))
acedf35c 6183 n -1))))
20908596
CD
6184 ((eq span 'week)
6185 (let* ((nt (calendar-day-of-week
6186 (calendar-gregorian-from-absolute sd)))
6187 (d (if org-agenda-start-on-weekday
6188 (- nt org-agenda-start-on-weekday)
acedf35c
CD
6189 0))
6190 y1)
20908596
CD
6191 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6192 (when n
6193 (require 'cal-iso)
20908596
CD
6194 (when (> n 99)
6195 (setq y1 (org-small-year-to-year (/ n 100))
6196 n (mod n 100)))
6197 (setq sd
6198 (calendar-absolute-from-iso
6199 (list n 1
acedf35c 6200 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
20908596 6201 ((eq span 'month)
acedf35c
CD
6202 (let (y1)
6203 (when (and n (> n 99))
6204 (setq y1 (org-small-year-to-year (/ n 100))
6205 n (mod n 100)))
6206 (setq sd (calendar-absolute-from-gregorian
6207 (list (or n mg) 1 (or y1 yg))))))
20908596
CD
6208 ((eq span 'year)
6209 (setq sd (calendar-absolute-from-gregorian
acedf35c
CD
6210 (list 1 1 (or n yg))))))
6211 sd))
20908596
CD
6212
6213(defun org-agenda-next-date-line (&optional arg)
6214 "Jump to the next line indicating a date in agenda buffer."
6215 (interactive "p")
6216 (org-agenda-check-type t 'agenda 'timeline)
6217 (beginning-of-line 1)
6218 ;; This does not work if user makes date format that starts with a blank
6219 (if (looking-at "^\\S-") (forward-char 1))
6220 (if (not (re-search-forward "^\\S-" nil t arg))
6221 (progn
6222 (backward-char 1)
6223 (error "No next date after this line in this buffer")))
6224 (goto-char (match-beginning 0)))
6225
6226(defun org-agenda-previous-date-line (&optional arg)
6227 "Jump to the previous line indicating a date in agenda buffer."
6228 (interactive "p")
6229 (org-agenda-check-type t 'agenda 'timeline)
6230 (beginning-of-line 1)
6231 (if (not (re-search-backward "^\\S-" nil t arg))
6232 (error "No previous date before this line in this buffer")))
6233
6234;; Initialize the highlight
86fbb8ca
CD
6235(defvar org-hl (make-overlay 1 1))
6236(overlay-put org-hl 'face 'highlight)
20908596
CD
6237
6238(defun org-highlight (begin end &optional buffer)
6239 "Highlight a region with overlay."
86fbb8ca 6240 (move-overlay org-hl begin end (or buffer (current-buffer))))
20908596
CD
6241
6242(defun org-unhighlight ()
6243 "Detach overlay INDEX."
86fbb8ca 6244 (org-detach-overlay org-hl))
20908596
CD
6245
6246;; FIXME this is currently not used.
6247(defun org-highlight-until-next-command (beg end &optional buffer)
6248 "Move the highlight overlay to BEG/END, remove it before the next command."
6249 (org-highlight beg end buffer)
6250 (add-hook 'pre-command-hook 'org-unhighlight-once))
6251(defun org-unhighlight-once ()
6252 "Remove the highlight from its position, and this function from the hook."
6253 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6254 (org-unhighlight))
6255
6256(defun org-agenda-follow-mode ()
6257 "Toggle follow mode in an agenda buffer."
6258 (interactive)
6259 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6260 (org-agenda-set-mode-name)
8bfe682a
CD
6261 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6262 (org-agenda-show))
20908596
CD
6263 (message "Follow mode is %s"
6264 (if org-agenda-follow-mode "on" "off")))
6265
54a0dee5
CD
6266(defun org-agenda-entry-text-mode (&optional arg)
6267 "Toggle entry text mode in an agenda buffer."
6268 (interactive "P")
365f8d85
SM
6269 (setq org-agenda-entry-text-mode (or (integerp arg)
6270 (not org-agenda-entry-text-mode)))
54a0dee5
CD
6271 (org-agenda-entry-text-hide)
6272 (and org-agenda-entry-text-mode
6273 (let ((org-agenda-entry-text-maxlines
6274 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6275 (org-agenda-entry-text-show)))
6276 (org-agenda-set-mode-name)
6277 (message "Entry text mode is %s. Maximum number of lines is %d"
6278 (if org-agenda-entry-text-mode "on" "off")
6279 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6280
acedf35c
CD
6281(defun org-agenda-clockreport-mode (&optional with-filter)
6282 "Toggle clocktable mode in an agenda buffer.
6283With prefix arg WITH-FILTER, make the clocktable respect the current
6284agenda filter."
6285 (interactive "P")
20908596 6286 (org-agenda-check-type t 'agenda)
acedf35c
CD
6287 (if with-filter
6288 (setq org-agenda-clockreport-mode 'with-filter)
6289 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
20908596
CD
6290 (org-agenda-set-mode-name)
6291 (org-agenda-redo)
6292 (message "Clocktable mode is %s"
6293 (if org-agenda-clockreport-mode "on" "off")))
6294
93b62de8
CD
6295(defun org-agenda-log-mode (&optional special)
6296 "Toggle log mode in an agenda buffer.
6297With argument SPECIAL, show all possible log items, not only the ones
6298configured in `org-agenda-log-mode-items'.
6299With a double `C-u' prefix arg, show *only* log items, nothing else."
6300 (interactive "P")
20908596 6301 (org-agenda-check-type t 'agenda 'timeline)
93b62de8
CD
6302 (setq org-agenda-show-log
6303 (if (equal special '(16))
6304 'only
6305 (if special '(closed clock state)
6306 (not org-agenda-show-log))))
20908596
CD
6307 (org-agenda-set-mode-name)
6308 (org-agenda-redo)
6309 (message "Log mode is %s"
6310 (if org-agenda-show-log "on" "off")))
6311
2c3ad40d 6312(defun org-agenda-archives-mode (&optional with-files)
c8d0cf5c
CD
6313 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6314When called with a prefix argument, include all archive files as well."
2c3ad40d
CD
6315 (interactive "P")
6316 (setq org-agenda-archives-mode
6317 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6318 (org-agenda-set-mode-name)
6319 (org-agenda-redo)
6320 (message
6321 "%s"
6322 (cond
6323 ((eq org-agenda-archives-mode nil)
6324 "No archives are included")
6325 ((eq org-agenda-archives-mode 'trees)
6326 (format "Trees with :%s: tag are included" org-archive-tag))
6327 ((eq org-agenda-archives-mode t)
6328 (format "Trees with :%s: tag and all active archive files are included"
6329 org-archive-tag)))))
6330
20908596
CD
6331(defun org-agenda-toggle-diary ()
6332 "Toggle diary inclusion in an agenda buffer."
6333 (interactive)
6334 (org-agenda-check-type t 'agenda)
6335 (setq org-agenda-include-diary (not org-agenda-include-diary))
6336 (org-agenda-redo)
6337 (org-agenda-set-mode-name)
6338 (message "Diary inclusion turned %s"
6339 (if org-agenda-include-diary "on" "off")))
6340
ed21c5c8 6341(defun org-agenda-toggle-deadlines ()
acedf35c 6342 "Toggle inclusion of entries with a deadline in an agenda buffer."
ed21c5c8
CD
6343 (interactive)
6344 (org-agenda-check-type t 'agenda)
6345 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6346 (org-agenda-redo)
6347 (org-agenda-set-mode-name)
6348 (message "Deadlines inclusion turned %s"
6349 (if org-agenda-include-deadlines "on" "off")))
6350
20908596
CD
6351(defun org-agenda-toggle-time-grid ()
6352 "Toggle time grid in an agenda buffer."
6353 (interactive)
6354 (org-agenda-check-type t 'agenda)
6355 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6356 (org-agenda-redo)
6357 (org-agenda-set-mode-name)
6358 (message "Time-grid turned %s"
6359 (if org-agenda-use-time-grid "on" "off")))
6360
6361(defun org-agenda-set-mode-name ()
6362 "Set the mode name to indicate all the small mode settings."
6363 (setq mode-name
acedf35c
CD
6364 (list "Org-Agenda"
6365 (if (get 'org-agenda-files 'org-restrict) " []" "")
6366 " "
6367 '(:eval (org-agenda-span-name org-agenda-current-span))
6368 (if org-agenda-follow-mode " Follow" "")
6369 (if org-agenda-entry-text-mode " ETxt" "")
6370 (if org-agenda-include-diary " Diary" "")
6371 (if org-agenda-include-deadlines " Ddl" "")
6372 (if org-agenda-use-time-grid " Grid" "")
6373 (if (and (boundp 'org-habit-show-habits)
6374 org-habit-show-habits) " Habit" "")
6375 (if (consp org-agenda-show-log) " LogAll"
6376 (if org-agenda-show-log " Log" ""))
6377 (if (or org-agenda-filter (get 'org-agenda-filter
6378 :preset-filter))
6379 (concat " {" (mapconcat
6380 'identity
6381 (append (get 'org-agenda-filter
6382 :preset-filter)
6383 org-agenda-filter) "") "}")
6384 "")
6385 (if org-agenda-archives-mode
6386 (if (eq org-agenda-archives-mode t)
6387 " Archives"
6388 (format " :%s:" org-archive-tag))
6389 "")
6390 (if org-agenda-clockreport-mode
6391 (if (eq org-agenda-clockreport-mode 'with-filter)
6392 " Clock{}" " Clock")
6393 "")))
20908596
CD
6394 (force-mode-line-update))
6395
6396(defun org-agenda-post-command-hook ()
b349f79f
CD
6397 (setq org-agenda-type
6398 (or (get-text-property (point) 'org-agenda-type)
6399 (get-text-property (max (point-min) (1- (point)))
8bfe682a
CD
6400 'org-agenda-type))))
6401
6402(defun org-agenda-next-line ()
86fbb8ca 6403 "Move cursor to the next line, and show if follow mode is active."
8bfe682a
CD
6404 (interactive)
6405 (call-interactively 'next-line)
1bcdebed
CD
6406 (org-agenda-do-context-action))
6407
8bfe682a
CD
6408(defun org-agenda-previous-line ()
6409 "Move cursor to the previous line, and show if follow-mode is active."
8bfe682a
CD
6410 (interactive)
6411 (call-interactively 'previous-line)
1bcdebed
CD
6412 (org-agenda-do-context-action))
6413
6414(defun org-agenda-do-context-action ()
86fbb8ca 6415 "Show outline path and, maybe, follow mode window."
1bcdebed
CD
6416 (let ((m (org-get-at-bol 'org-marker)))
6417 (if (and org-agenda-follow-mode m)
6418 (org-agenda-show))
6419 (if (and m org-agenda-show-outline-path)
5dec9555
CD
6420 (org-with-point-at m
6421 (org-display-outline-path t)))))
20908596
CD
6422
6423(defun org-agenda-show-priority ()
6424 "Show the priority of the current item.
6425This priority is composed of the main priority given with the [#A] cookies,
6426and by additional input from the age of a schedules or deadline entry."
6427 (interactive)
8d642074 6428 (let* ((pri (org-get-at-bol 'priority)))
20908596
CD
6429 (message "Priority is %d" (if pri pri -1000))))
6430
6431(defun org-agenda-show-tags ()
6432 "Show the tags applicable to the current item."
6433 (interactive)
8d642074 6434 (let* ((tags (org-get-at-bol 'tags)))
20908596
CD
6435 (if tags
6436 (message "Tags are :%s:"
6437 (org-no-properties (mapconcat 'identity tags ":")))
6438 (message "No tags associated with this line"))))
6439
6440(defun org-agenda-goto (&optional highlight)
6441 "Go to the Org-mode file which contains the item at point."
6442 (interactive)
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 (switch-to-buffer-other-window buffer)
6448 (widen)
86fbb8ca 6449 (push-mark)
20908596
CD
6450 (goto-char pos)
6451 (when (org-mode-p)
6452 (org-show-context 'agenda)
6453 (save-excursion
6454 (and (outline-next-heading)
6455 (org-flag-heading nil)))) ; show the next heading
6456 (recenter (/ (window-height) 2))
6457 (run-hooks 'org-agenda-after-show-hook)
6458 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6459
6460(defvar org-agenda-after-show-hook nil
6461 "Normal hook run after an item has been shown from the agenda.
6462Point is in the buffer where the item originated.")
6463
6464(defun org-agenda-kill ()
6465 "Kill the entry or subtree belonging to the current agenda entry."
6466 (interactive)
6467 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 6468 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6469 (org-agenda-error)))
6470 (buffer (marker-buffer marker))
6471 (pos (marker-position marker))
8d642074 6472 (type (org-get-at-bol 'type))
20908596
CD
6473 dbeg dend (n 0) conf)
6474 (org-with-remote-undo buffer
6475 (with-current-buffer buffer
6476 (save-excursion
6477 (goto-char pos)
6478 (if (and (org-mode-p) (not (member type '("sexp"))))
6479 (setq dbeg (progn (org-back-to-heading t) (point))
6480 dend (org-end-of-subtree t t))
6481 (setq dbeg (point-at-bol)
6482 dend (min (point-max) (1+ (point-at-eol)))))
6483 (goto-char dbeg)
6484 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6485 (setq conf (or (eq t org-agenda-confirm-kill)
6486 (and (numberp org-agenda-confirm-kill)
6487 (> n org-agenda-confirm-kill))))
6488 (and conf
6489 (not (y-or-n-p
6490 (format "Delete entry with %d lines in buffer \"%s\"? "
6491 n (buffer-name buffer))))
6492 (error "Abort"))
6493 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6494 (with-current-buffer buffer (delete-region dbeg dend))
6495 (message "Agenda item and source killed"))))
6496
8bfe682a
CD
6497(defvar org-archive-default-command)
6498(defun org-agenda-archive-default ()
6499 "Archive the entry or subtree belonging to the current agenda entry."
6500 (interactive)
6501 (require 'org-archive)
6502 (org-agenda-archive-with org-archive-default-command))
6503
6504(defun org-agenda-archive-default-with-confirmation ()
6505 "Archive the entry or subtree belonging to the current agenda entry."
6506 (interactive)
6507 (require 'org-archive)
6508 (org-agenda-archive-with org-archive-default-command 'confirm))
6509
20908596
CD
6510(defun org-agenda-archive ()
6511 "Archive the entry or subtree belonging to the current agenda entry."
6512 (interactive)
8bfe682a 6513 (org-agenda-archive-with 'org-archive-subtree))
20908596
CD
6514
6515(defun org-agenda-archive-to-archive-sibling ()
8bfe682a
CD
6516 "Move the entry to the archive sibling."
6517 (interactive)
6518 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6519
6520(defun org-agenda-archive-with (cmd &optional confirm)
20908596
CD
6521 "Move the entry to the archive sibling."
6522 (interactive)
6523 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 6524 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6525 (org-agenda-error)))
6526 (buffer (marker-buffer marker))
6527 (pos (marker-position marker)))
6528 (org-with-remote-undo buffer
6529 (with-current-buffer buffer
6530 (if (org-mode-p)
8bfe682a
CD
6531 (if (and confirm
6532 (not (y-or-n-p "Archive this subtree or entry? ")))
6533 (error "Abort")
6534 (save-excursion
6535 (goto-char pos)
6536 (org-remove-subtree-entries-from-agenda)
6537 (org-back-to-heading t)
6538 (funcall cmd)))
20908596
CD
6539 (error "Archiving works only in Org-mode files"))))))
6540
6541(defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6542 "Remove all lines in the agenda that correspond to a given subtree.
6543The subtree is the one in buffer BUF, starting at BEG and ending at END.
6544If this information is not given, the function uses the tree at point."
6545 (let ((buf (or buf (current-buffer))) m p)
6546 (save-excursion
6547 (unless (and beg end)
6548 (org-back-to-heading t)
6549 (setq beg (point))
6550 (org-end-of-subtree t)
6551 (setq end (point)))
6552 (set-buffer (get-buffer org-agenda-buffer-name))
6553 (save-excursion
6554 (goto-char (point-max))
6555 (beginning-of-line 1)
6556 (while (not (bobp))
8d642074 6557 (when (and (setq m (org-get-at-bol 'org-marker))
20908596
CD
6558 (equal buf (marker-buffer m))
6559 (setq p (marker-position m))
6560 (>= p beg)
c8d0cf5c 6561 (< p end))
20908596
CD
6562 (let ((inhibit-read-only t))
6563 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6564 (beginning-of-line 0))))))
6565
86fbb8ca 6566(defun org-agenda-refile (&optional goto rfloc no-update)
c8d0cf5c 6567 "Refile the item at point."
54a0dee5
CD
6568 (interactive "P")
6569 (if (equal goto '(16))
6570 (org-refile-goto-last-stored)
8d642074 6571 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
6572 (org-agenda-error)))
6573 (buffer (marker-buffer marker))
6574 (pos (marker-position marker))
6575 (rfloc (or rfloc
6576 (org-refile-get-location
6577 (if goto "Goto: " "Refile to: ") buffer
6578 org-refile-allow-creating-parent-nodes))))
6579 (with-current-buffer buffer
6580 (save-excursion
6581 (save-restriction
6582 (widen)
6583 (goto-char marker)
6584 (org-remove-subtree-entries-from-agenda)
86fbb8ca
CD
6585 (org-refile goto buffer rfloc)))))
6586 (unless no-update (org-agenda-redo))))
54a0dee5
CD
6587
6588(defun org-agenda-open-link (&optional arg)
6589 "Follow the link in the current line, if any.
8bfe682a 6590This looks for a link in the displayed line in the agenda. It also looks
54a0dee5 6591at the text of the entry itself."
c8d0cf5c 6592 (interactive "P")
8d642074
CD
6593 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6594 (org-get-at-bol 'org-marker)))
6595 (buffer (and marker (marker-buffer marker)))
6596 (prefix (buffer-substring
6597 (point-at-bol)
6598 (+ (point-at-bol)
8bfe682a
CD
6599 (or (org-get-at-bol 'prefix-length) 0)))))
6600 (cond
6601 (buffer
6602 (with-current-buffer buffer
6603 (save-excursion
6604 (save-restriction
6605 (widen)
6606 (goto-char marker)
6607 (org-offer-links-in-entry arg prefix)))))
6608 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6609 (save-excursion
6610 (beginning-of-line 1)
6611 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6612 (org-open-link-from-string (match-string 1)))
6613 (t (error "No link to open here")))))
20908596
CD
6614
6615(defun org-agenda-copy-local-variable (var)
6616 "Get a variable from a referenced buffer and install it here."
8d642074 6617 (let ((m (org-get-at-bol 'org-marker)))
20908596
CD
6618 (when (and m (buffer-live-p (marker-buffer m)))
6619 (org-set-local var (with-current-buffer (marker-buffer m)
6620 (symbol-value var))))))
6621
6622(defun org-agenda-switch-to (&optional delete-other-windows)
6623 "Go to the Org-mode file which contains the item at point."
6624 (interactive)
8bfe682a
CD
6625 (if (and org-return-follows-link
6626 (not (org-get-at-bol 'org-marker))
6627 (org-in-regexp org-bracket-link-regexp))
6628 (org-open-link-from-string (match-string 0))
6629 (let* ((marker (or (org-get-at-bol 'org-marker)
6630 (org-agenda-error)))
6631 (buffer (marker-buffer marker))
6632 (pos (marker-position marker)))
6633 (switch-to-buffer buffer)
6634 (and delete-other-windows (delete-other-windows))
6635 (widen)
6636 (goto-char pos)
6637 (when (org-mode-p)
6638 (org-show-context 'agenda)
6639 (save-excursion
6640 (and (outline-next-heading)
6641 (org-flag-heading nil))))))) ; show the next heading
20908596
CD
6642
6643(defun org-agenda-goto-mouse (ev)
6644 "Go to the Org-mode file which contains the item at the mouse click."
6645 (interactive "e")
6646 (mouse-set-point ev)
6647 (org-agenda-goto))
6648
fdf730ed
CD
6649(defun org-agenda-show (&optional full-entry)
6650 "Display the Org-mode file which contains the item at point.
6651With prefix argument FULL-ENTRY, make the entire entry visible
6652if it was hidden in the outline."
6653 (interactive "P")
20908596 6654 (let ((win (selected-window)))
fdf730ed
CD
6655 (if full-entry
6656 (let ((org-show-entry-below t))
6657 (org-agenda-goto t))
6658 (org-agenda-goto t))
20908596
CD
6659 (select-window win)))
6660
8bfe682a
CD
6661(defvar org-agenda-show-window nil)
6662(defun org-agenda-show-and-scroll-up ()
6663 "Display the Org-mode file which contains the item at point.
6664When called repeatedly, scroll the window that is displaying the buffer."
6665 (interactive)
6666 (let ((win (selected-window)))
6667 (if (and (window-live-p org-agenda-show-window)
6668 (eq this-command last-command))
6669 (progn
6670 (select-window org-agenda-show-window)
6671 (ignore-errors (scroll-up)))
6672 (org-agenda-goto t)
6673 (show-subtree)
6674 (setq org-agenda-show-window (selected-window)))
6675 (select-window win)))
6676
6677(defun org-agenda-show-scroll-down ()
6678 "Scroll down the window showing the agenda."
6679 (interactive)
6680 (let ((win (selected-window)))
6681 (when (window-live-p org-agenda-show-window)
6682 (select-window org-agenda-show-window)
6683 (ignore-errors (scroll-down))
6684 (select-window win))))
6685
c8d0cf5c
CD
6686(defun org-agenda-show-1 (&optional more)
6687 "Display the Org-mode file which contains the item at point.
8bfe682a 6688The prefix arg selects the amount of information to display:
c8d0cf5c
CD
6689
66900 hide the subtree
66911 just show the entry according to defaults.
54a0dee5
CD
66922 show the children view
66933 show the subtree view
c8d0cf5c
CD
66944 show the entire subtree and any LOGBOOK drawers
66955 show the entire subtree and any drawers
6696With prefix argument FULL-ENTRY, make the entire entry visible
6697if it was hidden in the outline."
6698 (interactive "p")
6699 (let ((win (selected-window)))
6700 (org-agenda-goto t)
6701 (org-recenter-heading 1)
6702 (cond
6703 ((= more 0)
6704 (hide-subtree)
54a0dee5
CD
6705 (save-excursion
6706 (org-back-to-heading)
6707 (run-hook-with-args 'org-cycle-hook 'folded))
6708 (message "Remote: FOLDED"))
c8d0cf5c
CD
6709 ((and (interactive-p) (= more 1))
6710 (message "Remote: show with default settings"))
6711 ((= more 2)
6712 (show-entry)
54a0dee5 6713 (show-children)
c8d0cf5c
CD
6714 (save-excursion
6715 (org-back-to-heading)
54a0dee5
CD
6716 (run-hook-with-args 'org-cycle-hook 'children))
6717 (message "Remote: CHILDREN"))
c8d0cf5c
CD
6718 ((= more 3)
6719 (show-subtree)
6720 (save-excursion
6721 (org-back-to-heading)
54a0dee5
CD
6722 (run-hook-with-args 'org-cycle-hook 'subtree))
6723 (message "Remote: SUBTREE"))
c8d0cf5c
CD
6724 ((= more 4)
6725 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
6726 (org-drawer-regexp
6727 (concat "^[ \t]*:\\("
6728 (mapconcat 'regexp-quote org-drawers "\\|")
6729 "\\):[ \t]*$")))
6730 (show-subtree)
6731 (save-excursion
6732 (org-back-to-heading)
6733 (org-cycle-hide-drawers 'subtree)))
54a0dee5 6734 (message "Remote: SUBTREE AND LOGBOOK"))
c8d0cf5c
CD
6735 ((> more 4)
6736 (show-subtree)
54a0dee5 6737 (message "Remote: SUBTREE AND ALL DRAWERS")))
c8d0cf5c
CD
6738 (select-window win)))
6739
6740(defun org-recenter-heading (n)
6741 (save-excursion
6742 (org-back-to-heading)
6743 (recenter n)))
6744
6745(defvar org-agenda-cycle-counter nil)
54a0dee5 6746(defun org-agenda-cycle-show (&optional n)
c8d0cf5c
CD
6747 "Show the current entry in another window, with default settings.
6748Default settings are taken from `org-show-hierarchy-above' and siblings.
54a0dee5 6749When use repeatedly in immediate succession, the remote entry will cycle
c8d0cf5c
CD
6750through visibility
6751
54a0dee5
CD
6752children -> subtree -> folded
6753
6754When called with a numeric prefix arg, that arg will be passed through to
6755`org-agenda-show-1'. For the interpretation of that argument, see the
6756docstring of `org-agenda-show-1'."
6757 (interactive "P")
6758 (if (integerp n)
6759 (setq org-agenda-cycle-counter n)
6760 (if (not (eq last-command this-command))
6761 (setq org-agenda-cycle-counter 1)
6762 (if (equal org-agenda-cycle-counter 0)
6763 (setq org-agenda-cycle-counter 2)
6764 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
6765 (if (> org-agenda-cycle-counter 3)
6766 (setq org-agenda-cycle-counter 0)))))
c8d0cf5c
CD
6767 (org-agenda-show-1 org-agenda-cycle-counter))
6768
20908596
CD
6769(defun org-agenda-recenter (arg)
6770 "Display the Org-mode file which contains the item at point and recenter."
6771 (interactive "P")
6772 (let ((win (selected-window)))
6773 (org-agenda-goto t)
6774 (recenter arg)
6775 (select-window win)))
6776
6777(defun org-agenda-show-mouse (ev)
6778 "Display the Org-mode file which contains the item at the mouse click."
6779 (interactive "e")
6780 (mouse-set-point ev)
6781 (org-agenda-show))
6782
6783(defun org-agenda-check-no-diary ()
6784 "Check if the entry is a diary link and abort if yes."
8d642074 6785 (if (org-get-at-bol 'org-agenda-diary-link)
20908596
CD
6786 (org-agenda-error)))
6787
6788(defun org-agenda-error ()
6789 (error "Command not allowed in this line"))
6790
6791(defun org-agenda-tree-to-indirect-buffer ()
6792 "Show the subtree corresponding to the current entry in an indirect buffer.
6793This calls the command `org-tree-to-indirect-buffer' from the original
6794Org-mode buffer.
6795With numerical prefix arg ARG, go up to this level and then take that tree.
86fbb8ca
CD
6796With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
6797use the dedicated frame)."
20908596
CD
6798 (interactive)
6799 (org-agenda-check-no-diary)
8d642074 6800 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6801 (org-agenda-error)))
6802 (buffer (marker-buffer marker))
6803 (pos (marker-position marker)))
6804 (with-current-buffer buffer
6805 (save-excursion
6806 (goto-char pos)
6807 (call-interactively 'org-tree-to-indirect-buffer)))))
6808
6809(defvar org-last-heading-marker (make-marker)
6810 "Marker pointing to the headline that last changed its TODO state
6811by a remote command from the agenda.")
6812
6813(defun org-agenda-todo-nextset ()
6814 "Switch TODO entry to next sequence."
6815 (interactive)
6816 (org-agenda-todo 'nextset))
6817
6818(defun org-agenda-todo-previousset ()
6819 "Switch TODO entry to previous sequence."
6820 (interactive)
6821 (org-agenda-todo 'previousset))
6822
6823(defun org-agenda-todo (&optional arg)
6824 "Cycle TODO state of line at point, also in Org-mode file.
6825This changes the line at point, all other lines in the agenda referring to
6826the same tree node, and the headline of the tree node in the Org-mode file."
6827 (interactive "P")
6828 (org-agenda-check-no-diary)
6829 (let* ((col (current-column))
8d642074 6830 (marker (or (org-get-at-bol 'org-marker)
20908596
CD
6831 (org-agenda-error)))
6832 (buffer (marker-buffer marker))
6833 (pos (marker-position marker))
8d642074 6834 (hdmarker (org-get-at-bol 'org-hd-marker))
acedf35c 6835 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
20908596 6836 (inhibit-read-only t)
93b62de8 6837 org-agenda-headline-snapshot-before-repeat newhead just-one)
20908596
CD
6838 (org-with-remote-undo buffer
6839 (with-current-buffer buffer
6840 (widen)
6841 (goto-char pos)
6842 (org-show-context 'agenda)
6843 (save-excursion
6844 (and (outline-next-heading)
6845 (org-flag-heading nil))) ; show the next heading
a2a2e7fb
CD
6846 (let ((current-prefix-arg arg))
6847 (call-interactively 'org-todo))
20908596
CD
6848 (and (bolp) (forward-char 1))
6849 (setq newhead (org-get-heading))
93b62de8
CD
6850 (when (and (org-bound-and-true-p
6851 org-agenda-headline-snapshot-before-repeat)
6852 (not (equal org-agenda-headline-snapshot-before-repeat
6853 newhead))
6854 todayp)
6855 (setq newhead org-agenda-headline-snapshot-before-repeat
6856 just-one t))
20908596
CD
6857 (save-excursion
6858 (org-back-to-heading)
6859 (move-marker org-last-heading-marker (point))))
6860 (beginning-of-line 1)
6861 (save-excursion
93b62de8 6862 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
20908596
CD
6863 (org-move-to-column col))))
6864
6865(defun org-agenda-add-note (&optional arg)
6866 "Add a time-stamped note to the entry at point."
6867 (interactive "P")
6868 (org-agenda-check-no-diary)
8d642074 6869 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6870 (org-agenda-error)))
6871 (buffer (marker-buffer marker))
6872 (pos (marker-position marker))
8d642074 6873 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
6874 (inhibit-read-only t))
6875 (with-current-buffer buffer
6876 (widen)
6877 (goto-char pos)
6878 (org-show-context 'agenda)
6879 (save-excursion
6880 (and (outline-next-heading)
6881 (org-flag-heading nil))) ; show the next heading
6882 (org-add-note))))
6883
db55f368 6884(defun org-agenda-change-all-lines (newhead hdmarker
4ed008de 6885 &optional fixface just-this)
20908596
CD
6886 "Change all lines in the agenda buffer which match HDMARKER.
6887The new content of the line will be NEWHEAD (as modified by
6888`org-format-agenda-item'). HDMARKER is checked with
6889`equal' against all `org-hd-marker' text properties in the file.
33306645 6890If FIXFACE is non-nil, the face of each item is modified according to
db55f368
CD
6891the new TODO state.
6892If JUST-THIS is non-nil, change just the current line, not all.
33306645 6893If FORCE-TAGS is non nil, the car of it returns the new tags."
20908596 6894 (let* ((inhibit-read-only t)
93b62de8 6895 (line (org-current-line))
fdf730ed 6896 (thetags (with-current-buffer (marker-buffer hdmarker)
4ed008de
CD
6897 (save-excursion (save-restriction (widen)
6898 (goto-char hdmarker)
fdf730ed 6899 (org-get-tags-at)))))
20908596
CD
6900 props m pl undone-face done-face finish new dotime cat tags)
6901 (save-excursion
6902 (goto-char (point-max))
6903 (beginning-of-line 1)
6904 (while (not finish)
6905 (setq finish (bobp))
8d642074 6906 (when (and (setq m (org-get-at-bol 'org-hd-marker))
93b62de8 6907 (or (not just-this) (= (org-current-line) line))
20908596
CD
6908 (equal m hdmarker))
6909 (setq props (text-properties-at (point))
8d642074
CD
6910 dotime (org-get-at-bol 'dotime)
6911 cat (org-get-at-bol 'org-category)
4ed008de 6912 tags thetags
20908596 6913 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
8d642074
CD
6914 pl (org-get-at-bol 'prefix-length)
6915 undone-face (org-get-at-bol 'undone-face)
6916 done-face (org-get-at-bol 'done-face))
6917 (goto-char (+ (point) pl))
6918 ;; (org-move-to-column pl) FIXME: does the above line work correctly?
20908596
CD
6919 (cond
6920 ((equal new "")
6921 (beginning-of-line 1)
6922 (and (looking-at ".*\n?") (replace-match "")))
6923 ((looking-at ".*")
6924 (replace-match new t t)
6925 (beginning-of-line 1)
6926 (add-text-properties (point-at-bol) (point-at-eol) props)
6927 (when fixface
6928 (add-text-properties
6929 (point-at-bol) (point-at-eol)
6930 (list 'face
6931 (if org-last-todo-state-is-todo
6932 undone-face done-face))))
6933 (org-agenda-highlight-todo 'line)
6934 (beginning-of-line 1))
6935 (t (error "Line update did not work"))))
6936 (beginning-of-line 0)))
6937 (org-finalize-agenda)))
6938
6939(defun org-agenda-align-tags (&optional line)
6940 "Align all tags in agenda items to `org-agenda-tags-column'."
6941 (let ((inhibit-read-only t) l c)
6942 (save-excursion
6943 (goto-char (if line (point-at-bol) (point-min)))
afe98dfa 6944 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
20908596
CD
6945 (if line (point-at-eol) nil) t)
6946 (add-text-properties
6947 (match-beginning 2) (match-end 2)
30ab4580
GM
6948 (list 'face (delq nil (let ((prop (get-text-property
6949 (match-beginning 2) 'face)))
6950 (or (listp prop) (setq prop (list prop)))
6951 (if (memq 'org-tag prop)
6952 prop
6953 (cons 'org-tag prop))))))
20908596
CD
6954 (setq l (- (match-end 2) (match-beginning 2))
6955 c (if (< org-agenda-tags-column 0)
6956 (- (abs org-agenda-tags-column) l)
6957 org-agenda-tags-column))
6958 (delete-region (match-beginning 1) (match-end 1))
6959 (goto-char (match-beginning 1))
6960 (insert (org-add-props
6961 (make-string (max 1 (- c (current-column))) ?\ )
ed21c5c8
CD
6962 (plist-put (copy-sequence (text-properties-at (point)))
6963 'face nil))))
ff4be292
CD
6964 (goto-char (point-min))
6965 (org-font-lock-add-tag-faces (point-max)))))
20908596
CD
6966
6967(defun org-agenda-priority-up ()
6968 "Increase the priority of line at point, also in Org-mode file."
6969 (interactive)
6970 (org-agenda-priority 'up))
6971
6972(defun org-agenda-priority-down ()
6973 "Decrease the priority of line at point, also in Org-mode file."
6974 (interactive)
6975 (org-agenda-priority 'down))
6976
6977(defun org-agenda-priority (&optional force-direction)
6978 "Set the priority of line at point, also in Org-mode file.
6979This changes the line at point, all other lines in the agenda referring to
6980the same tree node, and the headline of the tree node in the Org-mode file."
6981 (interactive)
c8d0cf5c
CD
6982 (unless org-enable-priority-commands
6983 (error "Priority commands are disabled"))
20908596 6984 (org-agenda-check-no-diary)
8d642074 6985 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 6986 (org-agenda-error)))
8d642074 6987 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
6988 (buffer (marker-buffer hdmarker))
6989 (pos (marker-position hdmarker))
6990 (inhibit-read-only t)
6991 newhead)
6992 (org-with-remote-undo buffer
6993 (with-current-buffer buffer
6994 (widen)
6995 (goto-char pos)
6996 (org-show-context 'agenda)
6997 (save-excursion
6998 (and (outline-next-heading)
6999 (org-flag-heading nil))) ; show the next heading
7000 (funcall 'org-priority force-direction)
7001 (end-of-line 1)
7002 (setq newhead (org-get-heading)))
7003 (org-agenda-change-all-lines newhead hdmarker)
7004 (beginning-of-line 1))))
7005
7006;; FIXME: should fix the tags property of the agenda line.
c8d0cf5c 7007(defun org-agenda-set-tags (&optional tag onoff)
20908596
CD
7008 "Set tags for the current headline."
7009 (interactive)
7010 (org-agenda-check-no-diary)
7011 (if (and (org-region-active-p) (interactive-p))
7012 (call-interactively 'org-change-tag-in-region)
7013 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 7014 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
7015 (org-agenda-error)))
7016 (buffer (marker-buffer hdmarker))
7017 (pos (marker-position hdmarker))
7018 (inhibit-read-only t)
4ed008de 7019 newhead)
20908596
CD
7020 (org-with-remote-undo buffer
7021 (with-current-buffer buffer
7022 (widen)
7023 (goto-char pos)
7024 (save-excursion
7025 (org-show-context 'agenda))
7026 (save-excursion
7027 (and (outline-next-heading)
7028 (org-flag-heading nil))) ; show the next heading
7029 (goto-char pos)
c8d0cf5c
CD
7030 (if tag
7031 (org-toggle-tag tag onoff)
7032 (call-interactively 'org-set-tags))
20908596
CD
7033 (end-of-line 1)
7034 (setq newhead (org-get-heading)))
4ed008de 7035 (org-agenda-change-all-lines newhead hdmarker)
20908596
CD
7036 (beginning-of-line 1)))))
7037
54a0dee5
CD
7038(defun org-agenda-set-property ()
7039 "Set a property for the current headline."
7040 (interactive)
7041 (org-agenda-check-no-diary)
7042 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 7043 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
7044 (org-agenda-error)))
7045 (buffer (marker-buffer hdmarker))
7046 (pos (marker-position hdmarker))
7047 (inhibit-read-only t)
7048 newhead)
7049 (org-with-remote-undo buffer
7050 (with-current-buffer buffer
7051 (widen)
7052 (goto-char pos)
7053 (save-excursion
7054 (org-show-context 'agenda))
7055 (save-excursion
7056 (and (outline-next-heading)
7057 (org-flag-heading nil))) ; show the next heading
7058 (goto-char pos)
7059 (call-interactively 'org-set-property)))))
7060
7061(defun org-agenda-set-effort ()
7062 "Set the effort property for the current headline."
7063 (interactive)
7064 (org-agenda-check-no-diary)
7065 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 7066 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
7067 (org-agenda-error)))
7068 (buffer (marker-buffer hdmarker))
7069 (pos (marker-position hdmarker))
7070 (inhibit-read-only t)
7071 newhead)
7072 (org-with-remote-undo buffer
7073 (with-current-buffer buffer
7074 (widen)
7075 (goto-char pos)
7076 (save-excursion
7077 (org-show-context 'agenda))
7078 (save-excursion
7079 (and (outline-next-heading)
7080 (org-flag-heading nil))) ; show the next heading
7081 (goto-char pos)
7082 (call-interactively 'org-set-effort)
7083 (end-of-line 1)))))
7084
20908596
CD
7085(defun org-agenda-toggle-archive-tag ()
7086 "Toggle the archive tag for the current entry."
7087 (interactive)
7088 (org-agenda-check-no-diary)
7089 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 7090 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
7091 (org-agenda-error)))
7092 (buffer (marker-buffer hdmarker))
7093 (pos (marker-position hdmarker))
7094 (inhibit-read-only t)
7095 newhead)
7096 (org-with-remote-undo buffer
7097 (with-current-buffer buffer
7098 (widen)
7099 (goto-char pos)
7100 (org-show-context 'agenda)
7101 (save-excursion
7102 (and (outline-next-heading)
7103 (org-flag-heading nil))) ; show the next heading
7104 (call-interactively 'org-toggle-archive-tag)
7105 (end-of-line 1)
7106 (setq newhead (org-get-heading)))
7107 (org-agenda-change-all-lines newhead hdmarker)
7108 (beginning-of-line 1))))
7109
c8d0cf5c
CD
7110(defun org-agenda-do-date-later (arg)
7111 (interactive "P")
7112 (cond
7113 ((or (equal arg '(16))
7114 (memq last-command
7115 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7116 (setq this-command 'org-agenda-date-later-minutes)
7117 (org-agenda-date-later-minutes 1))
7118 ((or (equal arg '(4))
7119 (memq last-command
7120 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7121 (setq this-command 'org-agenda-date-later-hours)
7122 (org-agenda-date-later-hours 1))
7123 (t
7124 (org-agenda-date-later (prefix-numeric-value arg)))))
7125
7126(defun org-agenda-do-date-earlier (arg)
7127 (interactive "P")
7128 (cond
7129 ((or (equal arg '(16))
7130 (memq last-command
7131 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7132 (setq this-command 'org-agenda-date-earlier-minutes)
7133 (org-agenda-date-earlier-minutes 1))
7134 ((or (equal arg '(4))
7135 (memq last-command
7136 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7137 (setq this-command 'org-agenda-date-earlier-hours)
7138 (org-agenda-date-earlier-hours 1))
7139 (t
7140 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7141
20908596
CD
7142(defun org-agenda-date-later (arg &optional what)
7143 "Change the date of this item to one day later."
7144 (interactive "p")
7145 (org-agenda-check-type t 'agenda 'timeline)
7146 (org-agenda-check-no-diary)
8d642074 7147 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7148 (org-agenda-error)))
7149 (buffer (marker-buffer marker))
7150 (pos (marker-position marker)))
7151 (org-with-remote-undo buffer
7152 (with-current-buffer buffer
7153 (widen)
7154 (goto-char pos)
7155 (if (not (org-at-timestamp-p))
7156 (error "Cannot find time stamp"))
7157 (org-timestamp-change arg (or what 'day)))
7158 (org-agenda-show-new-time marker org-last-changed-timestamp))
7159 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7160
7161(defun org-agenda-date-earlier (arg &optional what)
7162 "Change the date of this item to one day earlier."
7163 (interactive "p")
7164 (org-agenda-date-later (- arg) what))
7165
c8d0cf5c
CD
7166(defun org-agenda-date-later-minutes (arg)
7167 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7168 (interactive "p")
7169 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7170 (org-agenda-date-later arg 'minute))
7171
7172(defun org-agenda-date-earlier-minutes (arg)
7173 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7174 (interactive "p")
7175 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7176 (org-agenda-date-earlier arg 'minute))
7177
7178(defun org-agenda-date-later-hours (arg)
7179 "Change the time of this item, in hour steps."
7180 (interactive "p")
7181 (org-agenda-date-later arg 'hour))
7182
7183(defun org-agenda-date-earlier-hours (arg)
7184 "Change the time of this item, in hour steps."
7185 (interactive "p")
7186 (org-agenda-date-earlier arg 'hour))
7187
20908596
CD
7188(defun org-agenda-show-new-time (marker stamp &optional prefix)
7189 "Show new date stamp via text properties."
7190 ;; We use text properties to make this undoable
71d35b24
CD
7191 (let ((inhibit-read-only t)
7192 (buffer-invisibility-spec))
20908596
CD
7193 (setq stamp (concat " " prefix " => " stamp))
7194 (save-excursion
7195 (goto-char (point-max))
7196 (while (not (bobp))
8d642074 7197 (when (equal marker (org-get-at-bol 'org-marker))
20908596 7198 (org-move-to-column (- (window-width) (length stamp)) t)
71d35b24 7199 (org-agenda-fix-tags-filter-overlays-at (point))
20908596
CD
7200 (if (featurep 'xemacs)
7201 ;; Use `duplicable' property to trigger undo recording
7202 (let ((ex (make-extent nil nil))
7203 (gl (make-glyph stamp)))
7204 (set-glyph-face gl 'secondary-selection)
7205 (set-extent-properties
7206 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7207 (insert-extent ex (1- (point)) (point-at-eol)))
7208 (add-text-properties
7209 (1- (point)) (point-at-eol)
7210 (list 'display (org-add-props stamp nil
7211 'face 'secondary-selection))))
7212 (beginning-of-line 1))
7213 (beginning-of-line 0)))))
7214
7215(defun org-agenda-date-prompt (arg)
7216 "Change the date of this item. Date is prompted for, with default today.
7217The prefix ARG is passed to the `org-time-stamp' command and can therefore
7218be used to request time specification in the time stamp."
7219 (interactive "P")
7220 (org-agenda-check-type t 'agenda 'timeline)
7221 (org-agenda-check-no-diary)
8d642074 7222 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7223 (org-agenda-error)))
7224 (buffer (marker-buffer marker))
7225 (pos (marker-position marker)))
7226 (org-with-remote-undo buffer
7227 (with-current-buffer buffer
7228 (widen)
7229 (goto-char pos)
ed21c5c8 7230 (if (not (org-at-timestamp-p t))
20908596 7231 (error "Cannot find time stamp"))
ed21c5c8 7232 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8d642074
CD
7233 (org-agenda-show-new-time marker org-last-changed-timestamp))
7234 (message "Time stamp changed to %s" org-last-changed-timestamp)))
20908596
CD
7235
7236(defun org-agenda-schedule (arg)
ed21c5c8
CD
7237 "Schedule the item at point.
7238Arg is passed through to `org-schedule'."
20908596
CD
7239 (interactive "P")
7240 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7241 (org-agenda-check-no-diary)
8d642074 7242 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7243 (org-agenda-error)))
7244 (type (marker-insertion-type marker))
7245 (buffer (marker-buffer marker))
7246 (pos (marker-position marker))
7247 (org-insert-labeled-timestamps-at-point nil)
7248 ts)
20908596
CD
7249 (set-marker-insertion-type marker t)
7250 (org-with-remote-undo buffer
7251 (with-current-buffer buffer
7252 (widen)
7253 (goto-char pos)
7254 (setq ts (org-schedule arg)))
7255 (org-agenda-show-new-time marker ts "S"))
7256 (message "Item scheduled for %s" ts)))
7257
7258(defun org-agenda-deadline (arg)
ed21c5c8
CD
7259 "Schedule the item at point.
7260Arg is passed through to `org-deadline'."
20908596
CD
7261 (interactive "P")
7262 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7263 (org-agenda-check-no-diary)
8d642074 7264 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7265 (org-agenda-error)))
7266 (buffer (marker-buffer marker))
7267 (pos (marker-position marker))
7268 (org-insert-labeled-timestamps-at-point nil)
7269 ts)
7270 (org-with-remote-undo buffer
7271 (with-current-buffer buffer
7272 (widen)
7273 (goto-char pos)
7274 (setq ts (org-deadline arg)))
8d642074 7275 (org-agenda-show-new-time marker ts "D"))
20908596
CD
7276 (message "Deadline for this item set to %s" ts)))
7277
b349f79f
CD
7278(defun org-agenda-action ()
7279 "Select entry for agenda action, or execute an agenda action.
7280This command prompts for another letter. Valid inputs are:
7281
7282m Mark the entry at point for an agenda action
7283s Schedule the marked entry to the date at the cursor
7284d Set the deadline of the marked entry to the date at the cursor
7285r Call `org-remember' with cursor date as the default date
86fbb8ca 7286c Call `org-capture' with cursor date as the default date
b349f79f
CD
7287SPC Show marked entry in other window
7288TAB Visit marked entry in other window
7289
7290The cursor may be at a date in the calendar, or in the Org agenda."
7291 (interactive)
65c439fd 7292 (let (ans)
86fbb8ca 7293 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
b349f79f
CD
7294 (setq ans (read-char-exclusive))
7295 (cond
7296 ((equal ans ?m)
7297 ;; Mark this entry
7298 (if (eq major-mode 'org-agenda-mode)
8d642074
CD
7299 (let ((m (or (org-get-at-bol 'org-hd-marker)
7300 (org-get-at-bol 'org-marker))))
b349f79f
CD
7301 (if m
7302 (progn
7303 (move-marker org-agenda-action-marker
7304 (marker-position m) (marker-buffer m))
7305 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7306 (error "Don't know which entry to mark")))
7307 (error "This command works only in the agenda")))
7308 ((equal ans ?s)
7309 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7310 ((equal ans ?d)
7311 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7312 ((equal ans ?r)
7313 (org-agenda-do-action '(org-remember) t))
86fbb8ca
CD
7314 ((equal ans ?c)
7315 (org-agenda-do-action '(org-capture) t))
b349f79f
CD
7316 ((equal ans ?\ )
7317 (let ((cw (selected-window)))
7318 (org-switch-to-buffer-other-window
7319 (marker-buffer org-agenda-action-marker))
7320 (goto-char org-agenda-action-marker)
7321 (org-show-context 'agenda)
7322 (select-window cw)))
7323 ((equal ans ?\C-i)
7324 (org-switch-to-buffer-other-window
7325 (marker-buffer org-agenda-action-marker))
7326 (goto-char org-agenda-action-marker)
7327 (org-show-context 'agenda))
7328 (t (error "Invalid agenda action %c" ans)))))
7329
7330(defun org-agenda-do-action (form &optional current-buffer)
7331 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7332 (let ((org-overriding-default-time (org-get-cursor-date)))
7333 (if current-buffer
7334 (eval form)
7335 (if (not (marker-buffer org-agenda-action-marker))
8bfe682a 7336 (error "No entry has been selected for agenda action")
b349f79f
CD
7337 (with-current-buffer (marker-buffer org-agenda-action-marker)
7338 (save-excursion
7339 (save-restriction
7340 (widen)
7341 (goto-char org-agenda-action-marker)
7342 (eval form))))))))
ff4be292 7343
20908596
CD
7344(defun org-agenda-clock-in (&optional arg)
7345 "Start the clock on the currently selected item."
7346 (interactive "P")
7347 (org-agenda-check-no-diary)
7348 (if (equal arg '(4))
7349 (org-clock-in arg)
8d642074 7350 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 7351 (org-agenda-error)))
8d642074 7352 (hdmarker (or (org-get-at-bol 'org-hd-marker)
b349f79f
CD
7353 marker))
7354 (pos (marker-position marker))
7355 newhead)
20908596
CD
7356 (org-with-remote-undo (marker-buffer marker)
7357 (with-current-buffer (marker-buffer marker)
7358 (widen)
7359 (goto-char pos)
b349f79f
CD
7360 (org-show-context 'agenda)
7361 (org-show-entry)
7362 (org-cycle-hide-drawers 'children)
7363 (org-clock-in arg)
7364 (setq newhead (org-get-heading)))
c8d0cf5c 7365 (org-agenda-change-all-lines newhead hdmarker)))))
20908596 7366
afe98dfa 7367(defun org-agenda-clock-out ()
20908596 7368 "Stop the currently running clock."
afe98dfa 7369 (interactive)
20908596
CD
7370 (unless (marker-buffer org-clock-marker)
7371 (error "No running clock"))
c8d0cf5c
CD
7372 (let ((marker (make-marker)) newhead)
7373 (org-with-remote-undo (marker-buffer org-clock-marker)
7374 (with-current-buffer (marker-buffer org-clock-marker)
7375 (save-excursion
7376 (save-restriction
7377 (widen)
7378 (goto-char org-clock-marker)
7379 (org-back-to-heading t)
7380 (move-marker marker (point))
7381 (org-clock-out)
7382 (setq newhead (org-get-heading))))))
7383 (org-agenda-change-all-lines newhead marker)
7384 (move-marker marker nil)))
20908596
CD
7385
7386(defun org-agenda-clock-cancel (&optional arg)
7387 "Cancel the currently running clock."
7388 (interactive "P")
7389 (unless (marker-buffer org-clock-marker)
7390 (error "No running clock"))
7391 (org-with-remote-undo (marker-buffer org-clock-marker)
7392 (org-clock-cancel)))
7393
afe98dfa
CD
7394(defun org-agenda-clock-goto ()
7395 "Jump to the currently clocked in task within the agenda.
7396If the currently clocked in task is not listed in the agenda
7397buffer, display it in another window."
7398 (interactive)
7399 (let (pos)
7400 (mapc (lambda (o)
7401 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7402 (setq pos (overlay-start o))))
7403 (overlays-in (point-min) (point-max)))
7404 (cond (pos (goto-char pos))
7405 ;; If the currently clocked entry is not in the agenda
7406 ;; buffer, we visit it in another window:
7407 (org-clock-current-task
7408 (org-switch-to-buffer-other-window (org-clock-goto)))
7409 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7410
8bfe682a
CD
7411(defun org-agenda-diary-entry-in-org-file ()
7412 "Make a diary entry in the file `org-agenda-diary-file'."
5dec9555 7413 (let (d1 d2 char (text "") dp1 dp2)
8bfe682a
CD
7414 (if (equal (buffer-name) "*Calendar*")
7415 (setq d1 (calendar-cursor-to-date t)
7416 d2 (car calendar-mark-ring))
5dec9555
CD
7417 (setq dp1 (get-text-property (point-at-bol) 'day))
7418 (unless dp1 (error "No date defined in current line"))
7419 (setq d1 (calendar-gregorian-from-absolute dp1)
7420 d2 (and (ignore-errors (mark))
7421 (save-excursion
7422 (goto-char (mark))
7423 (setq dp2 (get-text-property (point-at-bol) 'day)))
7424 (calendar-gregorian-from-absolute dp2))))
8bfe682a
CD
7425 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7426 (setq char (read-char-exclusive))
7427 (cond
7428 ((equal char ?d)
7429 (setq text (read-string "Day entry: "))
5dec9555
CD
7430 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7431 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7432 ((equal char ?a)
7433 (setq d1 (list (car d1) (nth 1 d1)
7434 (read-number (format "Reference year [%d]: " (nth 2 d1))
7435 (nth 2 d1))))
7436 (setq text (read-string "Anniversary (use %d to show years): "))
5dec9555
CD
7437 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7438 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7439 ((equal char ?b)
7440 (setq text (read-string "Block entry: "))
7441 (unless (and d1 d2 (not (equal d1 d2)))
7442 (error "No block of days selected"))
5dec9555
CD
7443 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7444 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7445 ((equal char ?j)
7446 (org-switch-to-buffer-other-window
7447 (find-file-noselect org-agenda-diary-file))
ed21c5c8 7448 (require 'org-datetree)
8bfe682a
CD
7449 (org-datetree-find-date-create d1)
7450 (org-reveal t))
7451 (t (error "Invalid selection character `%c'" char)))))
7452
5dec9555
CD
7453(defcustom org-agenda-insert-diary-strategy 'date-tree
7454 "Where in `org-agenda-diary-file' should new entries be added?
7455Valid values:
7456
7457date-tree in the date tree, as child of the date
7458top-level as top-level entries at the end of the file."
7459 :group 'org-agenda
7460 :type '(choice
7461 (const :tag "in a date tree" date-tree)
7462 (const :tag "as top level at end of file" top-level)))
7463
ed21c5c8
CD
7464(defcustom org-agenda-insert-diary-extract-time nil
7465 "Non-nil means extract any time specification from the diary entry."
7466 :group 'org-agenda
7467 :type 'boolean)
7468
8bfe682a
CD
7469(defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7470 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7471If TEXT is not empty, it will become the headline of the new entry, and
7472the resulting entry will not be shown. When TEXT is empty, switch to
7473`org-agenda-diary-file' and let the user finish the entry there."
7474 (let ((cw (current-window-configuration)))
7475 (org-switch-to-buffer-other-window
7476 (find-file-noselect org-agenda-diary-file))
7477 (widen)
7478 (goto-char (point-min))
7479 (cond
7480 ((eq type 'anniversary)
7481 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7482 (progn
7483 (or (org-on-heading-p t)
7484 (progn
7485 (outline-next-heading)
7486 (insert "* Anniversaries\n\n")
7487 (beginning-of-line -1)))))
7488 (outline-next-heading)
7489 (org-back-over-empty-lines)
7490 (backward-char 1)
7491 (insert "\n")
7492 (require 'diary-lib)
7493 (let ((calendar-date-display-form
7494 (if (if (boundp 'calendar-date-style)
7495 (eq calendar-date-style 'european)
afe98dfa
CD
7496 (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
7497 (org-bound-and-true-p european-calendar-style))) ; Emacs 22
8bfe682a
CD
7498 '(day " " month " " year)
7499 '(month " " day " " year))))
7500
7501 (insert (format "%%%%(diary-anniversary %s) %s"
7502 (calendar-date-string d1 nil t) text))))
7503 ((eq type 'day)
ed21c5c8
CD
7504 (let ((org-prefix-has-time t)
7505 (org-agenda-time-leading-zero t)
7506 fmt time time2)
7507 (if org-agenda-insert-diary-extract-time
7508 ;; Use org-format-agenda-item to parse text for a time-range and
7509 ;; remove it. FIXME: This is a hack, we should refactor
7510 ;; that function to make time extraction available separately
7511 (setq fmt (org-format-agenda-item nil text nil nil t)
7512 time (get-text-property 0 'time fmt)
7513 time2 (if (> (length time) 0)
7514 ;; split-string removes trailing ...... if
7515 ;; no end time given. First space
7516 ;; separates time from date.
7517 (concat " " (car (split-string time "\\.")))
7518 nil)
7519 text (get-text-property 0 'txt fmt)))
7520 (if (eq org-agenda-insert-diary-strategy 'top-level)
7521 (org-agenda-insert-diary-as-top-level text)
7522 (require 'org-datetree)
7523 (org-datetree-find-date-create d1)
7524 (org-agenda-insert-diary-make-new-entry text))
7525 (org-insert-time-stamp (org-time-from-absolute
7526 (calendar-absolute-from-gregorian d1))
7527 nil nil nil nil time2))
8bfe682a
CD
7528 (end-of-line 0))
7529 ((eq type 'block)
7530 (if (> (calendar-absolute-from-gregorian d1)
7531 (calendar-absolute-from-gregorian d2))
7532 (setq d1 (prog1 d2 (setq d2 d1))))
5dec9555
CD
7533 (if (eq org-agenda-insert-diary-strategy 'top-level)
7534 (org-agenda-insert-diary-as-top-level text)
7535 (require 'org-datetree)
7536 (org-datetree-find-date-create d1)
7537 (org-agenda-insert-diary-make-new-entry text))
8bfe682a
CD
7538 (org-insert-time-stamp (org-time-from-absolute
7539 (calendar-absolute-from-gregorian d1)))
7540 (insert "--")
7541 (org-insert-time-stamp (org-time-from-absolute
7542 (calendar-absolute-from-gregorian d2)))
7543 (end-of-line 0)))
7544 (if (string-match "\\S-" text)
7545 (progn
7546 (set-window-configuration cw)
7547 (message "%s entry added to %s"
7548 (capitalize (symbol-name type))
7549 (abbreviate-file-name org-agenda-diary-file)))
7550 (org-reveal t)
7551 (message "Please finish entry here"))))
7552
5dec9555
CD
7553(defun org-agenda-insert-diary-as-top-level (text)
7554 "Make new entry as a top-level entry at the end of the file.
7555Add TEXT as headline, and position the cursor in the second line so that
7556a timestamp can be added there."
7557 (widen)
7558 (goto-char (point-max))
7559 (or (bolp) (insert "\n"))
7560 (insert "* " text "\n")
7561 (if org-adapt-indentation (org-indent-to-column 2)))
7562
8bfe682a
CD
7563(defun org-agenda-insert-diary-make-new-entry (text)
7564 "Make new entry as last child of current entry.
7565Add TEXT as headline, and position the cursor in the second line so that
7566a timestamp can be added there."
7567 (let ((org-show-following-heading t)
7568 (org-show-siblings t)
7569 (org-show-hierarchy-above t)
7570 (org-show-entry-below t)
7571 col)
7572 (outline-next-heading)
7573 (org-back-over-empty-lines)
7574 (or (looking-at "[ \t]*$")
7575 (progn (insert "\n") (backward-char 1)))
ed21c5c8 7576 (org-insert-heading nil t)
8bfe682a
CD
7577 (org-do-demote)
7578 (setq col (current-column))
7579 (insert text "\n")
7580 (if org-adapt-indentation (org-indent-to-column col))
7581 (let ((org-show-following-heading t)
7582 (org-show-siblings t)
7583 (org-show-hierarchy-above t)
7584 (org-show-entry-below t))
7585 (org-show-context))))
7586
20908596
CD
7587(defun org-agenda-diary-entry ()
7588 "Make a diary entry, like the `i' command from the calendar.
8bfe682a
CD
7589All the standard commands work: block, weekly etc.
7590When `org-agenda-diary-file' points to a file,
7591`org-agenda-diary-entry-in-org-file' is called instead to create
7592entries in that Org-mode file."
20908596
CD
7593 (interactive)
7594 (org-agenda-check-type t 'agenda 'timeline)
8bfe682a
CD
7595 (if (not (eq org-agenda-diary-file 'diary-file))
7596 (org-agenda-diary-entry-in-org-file)
7597 (require 'diary-lib)
7598 (let* ((char (progn
7599 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7600 (read-char-exclusive)))
7601 (cmd (cdr (assoc char
7602 '((?d . insert-diary-entry)
7603 (?w . insert-weekly-diary-entry)
7604 (?m . insert-monthly-diary-entry)
7605 (?y . insert-yearly-diary-entry)
7606 (?a . insert-anniversary-diary-entry)
7607 (?b . insert-block-diary-entry)
7608 (?c . insert-cyclic-diary-entry)))))
7609 (oldf (symbol-function 'calendar-cursor-to-date))
7610 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7611 (point (point))
7612 (mark (or (mark t) (point))))
7613 (unless cmd
7614 (error "No command associated with <%c>" char))
7615 (unless (and (get-text-property point 'day)
7616 (or (not (equal ?b char))
7617 (get-text-property mark 'day)))
7618 (error "Don't know which date to use for diary entry"))
7619 ;; We implement this by hacking the `calendar-cursor-to-date' function
7620 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7621 (let ((calendar-mark-ring
7622 (list (calendar-gregorian-from-absolute
7623 (or (get-text-property mark 'day)
7624 (get-text-property point 'day))))))
7625 (unwind-protect
7626 (progn
7627 (fset 'calendar-cursor-to-date
7628 (lambda (&optional error dummy)
7629 (calendar-gregorian-from-absolute
7630 (get-text-property point 'day))))
20908596 7631 (call-interactively cmd))
8bfe682a 7632 (fset 'calendar-cursor-to-date oldf))))))
20908596 7633
20908596
CD
7634(defun org-agenda-execute-calendar-command (cmd)
7635 "Execute a calendar command from the agenda, with the date associated to
7636the cursor position."
7637 (org-agenda-check-type t 'agenda 'timeline)
7638 (require 'diary-lib)
7639 (unless (get-text-property (point) 'day)
7640 (error "Don't know which date to use for calendar command"))
7641 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7642 (point (point))
7643 (date (calendar-gregorian-from-absolute
7644 (get-text-property point 'day)))
7645 ;; the following 2 vars are needed in the calendar
7646 (displayed-month (car date))
7647 (displayed-year (nth 2 date)))
7648 (unwind-protect
7649 (progn
7650 (fset 'calendar-cursor-to-date
0627c265 7651 (lambda (&optional error dummy)
20908596
CD
7652 (calendar-gregorian-from-absolute
7653 (get-text-property point 'day))))
7654 (call-interactively cmd))
7655 (fset 'calendar-cursor-to-date oldf))))
7656
7657(defun org-agenda-phases-of-moon ()
7658 "Display the phases of the moon for the 3 months around the cursor date."
7659 (interactive)
7660 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
7661
7662(defun org-agenda-holidays ()
7663 "Display the holidays for the 3 months around the cursor date."
7664 (interactive)
7665 (org-agenda-execute-calendar-command 'list-calendar-holidays))
7666
7667(defvar calendar-longitude)
7668(defvar calendar-latitude)
7669(defvar calendar-location-name)
7670
7671(defun org-agenda-sunrise-sunset (arg)
7672 "Display sunrise and sunset for the cursor date.
7673Latitude and longitude can be specified with the variables
7674`calendar-latitude' and `calendar-longitude'. When called with prefix
7675argument, latitude and longitude will be prompted for."
7676 (interactive "P")
7677 (require 'solar)
7678 (let ((calendar-longitude (if arg nil calendar-longitude))
7679 (calendar-latitude (if arg nil calendar-latitude))
7680 (calendar-location-name
7681 (if arg "the given coordinates" calendar-location-name)))
7682 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
7683
7684(defun org-agenda-goto-calendar ()
7685 "Open the Emacs calendar with the date at the cursor."
7686 (interactive)
7687 (org-agenda-check-type t 'agenda 'timeline)
7688 (let* ((day (or (get-text-property (point) 'day)
7689 (error "Don't know which date to open in calendar")))
7690 (date (calendar-gregorian-from-absolute day))
7691 (calendar-move-hook nil)
7692 (calendar-view-holidays-initially-flag nil)
3820f429 7693 (calendar-view-diary-initially-flag nil))
20908596
CD
7694 (calendar)
7695 (calendar-goto-date date)))
7696
7697;;;###autoload
7698(defun org-calendar-goto-agenda ()
7699 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
7700This is a command that has to be installed in `calendar-mode-map'."
7701 (interactive)
7702 (org-agenda-list nil (calendar-absolute-from-gregorian
7703 (calendar-cursor-to-date))
7704 nil))
7705
7706(defun org-agenda-convert-date ()
7707 (interactive)
7708 (org-agenda-check-type t 'agenda 'timeline)
7709 (let ((day (get-text-property (point) 'day))
7710 date s)
7711 (unless day
7712 (error "Don't know which date to convert"))
7713 (setq date (calendar-gregorian-from-absolute day))
7714 (setq s (concat
7715 "Gregorian: " (calendar-date-string date) "\n"
7716 "ISO: " (calendar-iso-date-string date) "\n"
7717 "Day of Yr: " (calendar-day-of-year-string date) "\n"
7718 "Julian: " (calendar-julian-date-string date) "\n"
7719 "Astron. JD: " (calendar-astro-date-string date)
7720 " (Julian date number at noon UTC)\n"
7721 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
7722 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
7723 "French: " (calendar-french-date-string date) "\n"
7724 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
7725 "Mayan: " (calendar-mayan-date-string date) "\n"
7726 "Coptic: " (calendar-coptic-date-string date) "\n"
7727 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
7728 "Persian: " (calendar-persian-date-string date) "\n"
7729 "Chinese: " (calendar-chinese-date-string date) "\n"))
7730 (with-output-to-temp-buffer "*Dates*"
7731 (princ s))
93b62de8 7732 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
20908596 7733
c8d0cf5c
CD
7734;;; Bulk commands
7735
7736(defvar org-agenda-bulk-marked-entries nil
7737 "List of markers that refer to marked entries in the agenda.")
7738
54a0dee5
CD
7739(defun org-agenda-bulk-marked-p ()
7740 (eq (get-char-property (point-at-bol) 'type)
7741 'org-marked-entry-overlay))
7742
acedf35c 7743(defun org-agenda-bulk-mark (&optional arg)
c8d0cf5c 7744 "Mark the entry at point for future bulk action."
acedf35c
CD
7745 (interactive "p")
7746 (dotimes (i (max arg 1))
7747 (unless (org-get-at-bol 'org-agenda-diary-link)
7748 (let* ((m (org-get-at-bol 'org-hd-marker))
7749 ov)
7750 (unless (org-agenda-bulk-marked-p)
7751 (unless m (error "Nothing to mark at point"))
7752 (push m org-agenda-bulk-marked-entries)
7753 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
7754 (org-overlay-display ov "> "
7755 (org-get-todo-face "TODO")
7756 'evaporate)
7757 (overlay-put ov 'type 'org-marked-entry-overlay))
7758 (beginning-of-line 2)
7759 (while (and (get-char-property (point) 'invisible) (not (eobp)))
7760 (beginning-of-line 2))
7761 (message "%d entries marked for bulk action"
7762 (length org-agenda-bulk-marked-entries))))))
c8d0cf5c
CD
7763
7764(defun org-agenda-bulk-unmark ()
7765 "Unmark the entry at point for future bulk action."
7766 (interactive)
54a0dee5 7767 (when (org-agenda-bulk-marked-p)
c8d0cf5c
CD
7768 (org-agenda-bulk-remove-overlays
7769 (point-at-bol) (+ 2 (point-at-bol)))
7770 (setq org-agenda-bulk-marked-entries
8d642074 7771 (delete (org-get-at-bol 'org-hd-marker)
c8d0cf5c
CD
7772 org-agenda-bulk-marked-entries)))
7773 (beginning-of-line 2)
ed21c5c8
CD
7774 (while (and (get-char-property (point) 'invisible) (not (eobp)))
7775 (beginning-of-line 2))
c8d0cf5c
CD
7776 (message "%d entries marked for bulk action"
7777 (length org-agenda-bulk-marked-entries)))
7778
54a0dee5
CD
7779(defun org-agenda-bulk-toggle ()
7780 "Toggle marking the entry at point for bulk action."
7781 (interactive)
7782 (if (org-agenda-bulk-marked-p)
7783 (org-agenda-bulk-unmark)
7784 (org-agenda-bulk-mark)))
c8d0cf5c
CD
7785
7786(defun org-agenda-bulk-remove-overlays (&optional beg end)
7787 "Remove the mark overlays between BEG and END in the agenda buffer.
7788BEG and END default to the buffer limits.
7789
7790This only removes the overlays, it does not remove the markers
7791from the list in `org-agenda-bulk-marked-entries'."
7792 (interactive)
7793 (mapc (lambda (ov)
86fbb8ca
CD
7794 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
7795 (delete-overlay ov)))
7796 (overlays-in (or beg (point-min)) (or end (point-max)))))
c8d0cf5c
CD
7797
7798(defun org-agenda-bulk-remove-all-marks ()
7799 "Remove all marks in the agenda buffer.
7800This will remove the markers, and the overlays."
7801 (interactive)
7802 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
7803 (setq org-agenda-bulk-marked-entries nil)
7804 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
7805
ed21c5c8
CD
7806(defun org-agenda-bulk-action (&optional arg)
7807 "Execute an remote-editing action on all marked entries.
7808The prefix arg is passed through to the command if possible."
7809 (interactive "P")
c8d0cf5c
CD
7810 (unless org-agenda-bulk-marked-entries
7811 (error "No entries are marked"))
acedf35c 7812 (message "Bulk: [r]efile [$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [S]catter [d]eadline")
c8d0cf5c 7813 (let* ((action (read-char-exclusive))
ed21c5c8 7814 (org-log-refile (if org-log-refile 'time nil))
c8d0cf5c 7815 (entries (reverse org-agenda-bulk-marked-entries))
86fbb8ca 7816 redo-at-end
c8d0cf5c
CD
7817 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
7818 (cond
7819 ((equal action ?$)
7820 (setq cmd '(org-agenda-archive)))
7821
7822 ((equal action ?A)
7823 (setq cmd '(org-agenda-archive-to-archive-sibling)))
7824
7825 ((member action '(?r ?w))
7826 (setq rfloc (org-refile-get-location
7827 "Refile to: "
7828 (marker-buffer (car org-agenda-bulk-marked-entries))
7829 org-refile-allow-creating-parent-nodes))
86fbb8ca
CD
7830 (if (nth 3 rfloc)
7831 (setcar (nthcdr 3 rfloc)
7832 (move-marker (make-marker) (nth 3 rfloc)
7833 (or (get-file-buffer (nth 1 rfloc))
7834 (find-buffer-visiting (nth 1 rfloc))
7835 (error "This should not happen")))))
c8d0cf5c 7836
86fbb8ca
CD
7837 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
7838 redo-at-end t))
c8d0cf5c
CD
7839
7840 ((equal action ?t)
54a0dee5 7841 (setq state (org-icompleting-read
c8d0cf5c
CD
7842 "Todo state: "
7843 (with-current-buffer (marker-buffer (car entries))
7844 (mapcar 'list org-todo-keywords-1))))
7845 (setq cmd `(let ((org-inhibit-blocking t)
7846 (org-inhibit-logging 'note))
7847 (org-agenda-todo ,state))))
7848
7849 ((memq action '(?- ?+))
54a0dee5 7850 (setq tag (org-icompleting-read
c8d0cf5c
CD
7851 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
7852 (with-current-buffer (marker-buffer (car entries))
7853 (delq nil
7854 (mapcar (lambda (x)
7855 (if (stringp (car x)) x)) org-tag-alist)))))
7856 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
7857
7858 ((memq action '(?s ?d))
ed21c5c8
CD
7859 (let* ((date (unless arg
7860 (org-read-date
7861 nil nil nil
7862 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
7863 (ans (if arg nil org-read-date-final-answer))
c8d0cf5c
CD
7864 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
7865 (setq cmd `(let* ((bound (fboundp 'read-string))
7866 (old (and bound (symbol-function 'read-string))))
7867 (unwind-protect
7868 (progn
7869 (fset 'read-string (lambda (&rest ignore) ,ans))
ed21c5c8 7870 (eval '(,c1 arg)))
c8d0cf5c
CD
7871 (if bound
7872 (fset 'read-string old)
7873 (fmakunbound 'read-string)))))))
acedf35c
CD
7874
7875 ((eq action '?S)
7876 (let ((days (read-number
7877 (format "Scatter tasks across how many %sdays: "
7878 (if arg "week" "")) 7)))
7879 (setq cmd
7880 `(let ((distance (random ,(1+ days))))
7881 (if arg
7882 (let ((dist distance)
7883 (day-of-week
7884 (calendar-day-of-week
7885 (calendar-gregorian-from-absolute (org-today)))))
7886 (dotimes (i (1+ dist))
7887 (while (member day-of-week org-agenda-weekend-days)
7888 (incf distance)
7889 (incf day-of-week)
7890 (if (= day-of-week 7)
7891 (setq day-of-week 0)))
7892 (incf day-of-week)
7893 (if (= day-of-week 7)
7894 (setq day-of-week 0)))))
7895 (org-agenda-date-later distance)))))
7896
c8d0cf5c
CD
7897 (t (error "Invalid bulk action")))
7898
7899 ;; Sort the markers, to make sure that parents are handled before children
7900 (setq entries (sort entries
7901 (lambda (a b)
7902 (cond
7903 ((equal (marker-buffer a) (marker-buffer b))
7904 (< (marker-position a) (marker-position b)))
7905 (t
7906 (string< (buffer-name (marker-buffer a))
7907 (buffer-name (marker-buffer b))))))))
7908
7909 ;; Now loop over all markers and apply cmd
7910 (while (setq e (pop entries))
7911 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
7912 (if (not pos)
7913 (progn (message "Skipping removed entry at %s" e)
7914 (setq cntskip (1+ cntskip)))
7915 (goto-char pos)
7916 (eval cmd)
7917 (setq org-agenda-bulk-marked-entries
7918 (delete e org-agenda-bulk-marked-entries))
7919 (setq cnt (1+ cnt))))
7920 (setq org-agenda-bulk-marked-entries nil)
7921 (org-agenda-bulk-remove-all-marks)
86fbb8ca 7922 (when redo-at-end (org-agenda-redo))
c8d0cf5c
CD
7923 (message "Acted on %d entries%s"
7924 cnt
7925 (if (= cntskip 0)
7926 ""
7927 (format ", skipped %d (disappeared before their turn)"
7928 cntskip)))))
8d642074
CD
7929
7930;;; Flagging notes
7931
7932(defun org-agenda-show-the-flagging-note ()
7933 "Display the flagging note in the other window.
7934When called a second time in direct sequence, offer to remove the FLAGGING
7935tag and (if present) the flagging note."
7936 (interactive)
7937 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
7938 (win (selected-window))
7939 note heading newhead)
7940 (unless hdmarker
7941 (error "No linked entry at point"))
7942 (if (and (eq this-command last-command)
7943 (y-or-n-p "Unflag and remove any flagging note? "))
7944 (progn
7945 (org-agenda-remove-flag hdmarker)
7946 (let ((win (get-buffer-window "*Flagging Note*")))
7947 (and win (delete-window win)))
7948 (message "Entry unflaged"))
7949 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
7950 (unless note
7951 (error "No flagging note"))
7952 (org-kill-new note)
7953 (org-switch-to-buffer-other-window "*Flagging Note*")
7954 (erase-buffer)
7955 (insert note)
7956 (goto-char (point-min))
7957 (while (re-search-forward "\\\\n" nil t)
7958 (replace-match "\n" t t))
7959 (goto-char (point-min))
7960 (select-window win)
7961 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
7962
7963(defun org-agenda-remove-flag (marker)
8bfe682a 7964 "Remove the FLAGGED tag and any flagging note in the entry."
8d642074
CD
7965 (let (newhead)
7966 (org-with-point-at marker
7967 (org-toggle-tag "FLAGGED" 'off)
7968 (org-entry-delete nil "THEFLAGGINGNOTE")
7969 (setq newhead (org-get-heading)))
7970 (org-agenda-change-all-lines newhead marker)
7971 (message "Entry unflaged")))
7972
7973(defun org-agenda-get-any-marker (&optional pos)
7974 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
7975 (get-text-property (or pos (point-at-bol)) 'org-marker)))
c8d0cf5c 7976
20908596
CD
7977;;; Appointment reminders
7978
7979(defvar appt-time-msg-list)
7980
7981;;;###autoload
7982(defun org-agenda-to-appt (&optional refresh filter)
7983 "Activate appointments found in `org-agenda-files'.
7984With a \\[universal-argument] prefix, refresh the list of
33306645 7985appointments.
20908596
CD
7986
7987If FILTER is t, interactively prompt the user for a regular
7988expression, and filter out entries that don't match it.
7989
7990If FILTER is a string, use this string as a regular expression
7991for filtering entries out.
7992
7993FILTER can also be an alist with the car of each cell being
7994either 'headline or 'category. For example:
7995
7996 '((headline \"IMPORTANT\")
7997 (category \"Work\"))
7998
7999will only add headlines containing IMPORTANT or headlines
8000belonging to the \"Work\" category."
8001 (interactive "P")
20908596
CD
8002 (if refresh (setq appt-time-msg-list nil))
8003 (if (eq filter t)
8004 (setq filter (read-from-minibuffer "Regexp filter: ")))
8005 (let* ((cnt 0) ; count added events
8006 (org-agenda-new-buffers nil)
8007 (org-deadline-warning-days 0)
acedf35c
CD
8008 ;; Do not use `org-today' here because appt only takes
8009 ;; time and without date as argument, so it may pass wrong
8010 ;; information otherwise
20908596
CD
8011 (today (org-date-to-gregorian
8012 (time-to-days (current-time))))
c8d0cf5c 8013 (org-agenda-restrict nil)
621f83e4 8014 (files (org-agenda-files 'unrestricted)) entries file)
20908596 8015 ;; Get all entries which may contain an appt
db55f368 8016 (org-prepare-agenda-buffers files)
20908596
CD
8017 (while (setq file (pop files))
8018 (setq entries
8019 (append entries
8020 (org-agenda-get-day-entries
8021 file today :timestamp :scheduled :deadline))))
8022 (setq entries (delq nil entries))
8023 ;; Map thru entries and find if we should filter them out
8024 (mapc
8025 (lambda(x)
621f83e4 8026 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
20908596
CD
8027 (cat (get-text-property 1 'org-category x))
8028 (tod (get-text-property 1 'time-of-day x))
8029 (ok (or (null filter)
8030 (and (stringp filter) (string-match filter evt))
8031 (and (listp filter)
8032 (or (string-match
8033 (cadr (assoc 'category filter)) cat)
8034 (string-match
8035 (cadr (assoc 'headline filter)) evt))))))
8036 ;; FIXME: Shall we remove text-properties for the appt text?
8037 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8038 (when (and ok tod)
621f83e4 8039 (setq tod (concat "00" (number-to-string tod))
20908596 8040 tod (when (string-match
621f83e4 8041 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
20908596
CD
8042 (concat (match-string 1 tod) ":"
8043 (match-string 2 tod))))
8044 (appt-add tod evt)
8045 (setq cnt (1+ cnt))))) entries)
8046 (org-release-buffers org-agenda-new-buffers)
8047 (if (eq cnt 0)
8048 (message "No event to add")
8049 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8050
621f83e4
CD
8051(defun org-agenda-todayp (date)
8052 "Does DATE mean today, when considering `org-extend-today-until'?"
acedf35c
CD
8053 (let ((today (org-today))
8054 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8055 date)))
8056 (eq date today)))
621f83e4 8057
20908596
CD
8058(provide 'org-agenda)
8059
b349f79f 8060
20908596 8061;;; org-agenda.el ends here