Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / org / org-agenda.el
CommitLineData
b349f79f 1;;; org-agenda.el --- Dynamic task and appointment lists for Org
20908596 2
73b0cd50 3;; Copyright (C) 2004-2011
ca8ef0dc 4;; Free Software Foundation, Inc.
20908596
CD
5
6;; Author: Carsten Dominik <carsten at orgmode dot org>
7;; Keywords: outlines, hypermedia, calendar, wp
8;; Homepage: http://orgmode.org
acedf35c 9;; Version: 7.4
20908596
CD
10;;
11;; This file is part of GNU Emacs.
12;;
b1fc2b50 13;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 14;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
20908596
CD
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
b1fc2b50 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
25;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26;;
27;;; Commentary:
28
29;; This file contains the code for creating and using the Agenda for Org-mode.
30
31;;; Code:
32
33(require 'org)
34(eval-when-compile
86fbb8ca 35 (require 'cl))
20908596 36
b349f79f 37(declare-function diary-add-to-list "diary-lib"
20908596
CD
38 (date string specifier &optional marker globcolor literal))
39(declare-function calendar-absolute-from-iso "cal-iso" (date))
40(declare-function calendar-astro-date-string "cal-julian" (&optional date))
41(declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
20908596
CD
42(declare-function calendar-chinese-date-string "cal-china" (&optional date))
43(declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
44(declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
45(declare-function calendar-french-date-string "cal-french" (&optional date))
46(declare-function calendar-goto-date "cal-move" (date))
47(declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
48(declare-function calendar-islamic-date-string "cal-islam" (&optional date))
49(declare-function calendar-iso-date-string "cal-iso" (&optional date))
f6aafbed 50(declare-function calendar-iso-from-absolute "cal-iso" (date))
20908596
CD
51(declare-function calendar-julian-date-string "cal-julian" (&optional date))
52(declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
53(declare-function calendar-persian-date-string "cal-persia" (&optional date))
68a1b090
GM
54(declare-function org-datetree-find-date-create "org-datetree"
55 (date &optional keep-restriction))
20908596 56(declare-function org-columns-quit "org-colview" ())
8bfe682a
CD
57(declare-function diary-date-display-form "diary-lib" (&optional type))
58(declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
59(declare-function org-habit-insert-consistency-graphs
60 "org-habit" (&optional line))
61(declare-function org-is-habit-p "org-habit" (&optional pom))
62(declare-function org-habit-parse-todo "org-habit" (&optional pom))
68a1b090 63(declare-function org-habit-get-priority "org-habit" (habit &optional moment))
20908596 64(defvar calendar-mode-map)
afe98dfa 65(defvar org-clock-current-task) ; defined in org-clock.el
8d642074 66(defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
8bfe682a
CD
67(defvar org-habit-show-habits)
68(defvar org-habit-show-habits-only-for-today)
20908596
CD
69
70;; Defined somewhere in this file, but used before definition.
71(defvar org-agenda-buffer-name)
72(defvar org-agenda-overriding-header)
8d642074 73(defvar org-agenda-title-append nil)
20908596
CD
74(defvar entry)
75(defvar date)
76(defvar org-agenda-undo-list)
77(defvar org-agenda-pending-undo-list)
78(defvar original-date) ; dynamically scoped, calendar.el does scope this
79
80(defcustom org-agenda-confirm-kill 1
81 "When set, remote killing from the agenda buffer needs confirmation.
82When t, a confirmation is always needed. When a number N, confirmation is
83only needed when the text to be killed contains more than N non-white lines."
84 :group 'org-agenda
85 :type '(choice
86 (const :tag "Never" nil)
87 (const :tag "Always" t)
c8d0cf5c 88 (integer :tag "When more than N lines")))
20908596
CD
89
90(defcustom org-agenda-compact-blocks nil
ed21c5c8 91 "Non-nil means make the block agenda more compact.
20908596
CD
92This is done by leaving out unnecessary lines."
93 :group 'org-agenda
94 :type 'boolean)
95
0bd48b37
CD
96(defcustom org-agenda-block-separator ?=
97 "The separator between blocks in the agenda.
98If this is a string, it will be used as the separator, with a newline added.
99If it is a character, it will be repeated to fill the window width."
100 :group 'org-agenda
101 :type '(choice
102 (character)
103 (string)))
104
20908596
CD
105(defgroup org-agenda-export nil
106 "Options concerning exporting agenda views in Org-mode."
107 :tag "Org Agenda Export"
108 :group 'org-agenda)
109
110(defcustom org-agenda-with-colors t
ed21c5c8 111 "Non-nil means use colors in agenda views."
20908596
CD
112 :group 'org-agenda-export
113 :type 'boolean)
114
115(defcustom org-agenda-exporter-settings nil
116 "Alist of variable/value pairs that should be active during agenda export.
c8d0cf5c
CD
117This is a good place to set options for ps-print and for htmlize.
118Note that the way this is implemented, the values will be evaluated
119before assigned to the variables. So make sure to quote values you do
120*not* want evaluated, for example
121
122 (setq org-agenda-exporter-settings
123 '((ps-print-color-p 'black-white)))"
20908596
CD
124 :group 'org-agenda-export
125 :type '(repeat
126 (list
127 (variable)
128 (sexp :tag "Value"))))
129
c8d0cf5c
CD
130(defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
131 "Hook run in temporary buffer before writing it to an export file.
132A useful function is `org-agenda-add-entry-text'."
133 :group 'org-agenda-export
134 :type 'hook
135 :options '(org-agenda-add-entry-text))
136
137(defcustom org-agenda-add-entry-text-maxlines 0
138 "Maximum number of entry text lines to be added to agenda.
139This is only relevant when `org-agenda-add-entry-text' is part of
140`org-agenda-before-write-hook', which it is by default.
141When this is 0, nothing will happen. When it is greater than 0, it
142specifies the maximum number of lines that will be added for each entry
54a0dee5
CD
143that is listed in the agenda view.
144
145Note that this variable is not used during display, only when exporting
86fbb8ca
CD
146the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
147and `org-agenda-entry-text-maxlines'."
c8d0cf5c
CD
148 :group 'org-agenda
149 :type 'integer)
150
151(defcustom org-agenda-add-entry-text-descriptive-links t
ed21c5c8 152 "Non-nil means export org-links as descriptive links in agenda added text.
c8d0cf5c
CD
153This variable applies to the text added to the agenda when
154`org-agenda-add-entry-text-maxlines' is larger than 0.
155When this variable nil, the URL will (also) be shown."
156 :group 'org-agenda
157 :type 'boolean)
158
20908596
CD
159(defcustom org-agenda-export-html-style ""
160 "The style specification for exported HTML Agenda files.
161If this variable contains a string, it will replace the default <style>
162section as produced by `htmlize'.
163Since there are different ways of setting style information, this variable
164needs to contain the full HTML structure to provide a style, including the
165surrounding HTML tags. The style specifications should include definitions
166the fonts used by the agenda, here is an example:
167
168 <style type=\"text/css\">
169 p { font-weight: normal; color: gray; }
170 .org-agenda-structure {
171 font-size: 110%;
172 color: #003399;
173 font-weight: 600;
174 }
175 .org-todo {
176 color: #cc6666;
177 font-weight: bold;
178 }
c8d0cf5c
CD
179 .org-agenda-done {
180 color: #339933;
181 }
20908596
CD
182 .org-done {
183 color: #339933;
184 }
185 .title { text-align: center; }
186 .todo, .deadline { color: red; }
187 .done { color: green; }
188 </style>
189
190or, if you want to keep the style in a file,
191
192 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
193
194As the value of this option simply gets inserted into the HTML <head> header,
195you can \"misuse\" it to also add other text to the header. However,
196<style>...</style> is required, if not present the variable will be ignored."
197 :group 'org-agenda-export
198 :group 'org-export-html
199 :type 'string)
200
86fbb8ca
CD
201(defcustom org-agenda-persistent-filter nil
202 "When set, keep filters from one agenda view to the next."
203 :group 'org-agenda
204 :type 'boolean)
205
20908596
CD
206(defgroup org-agenda-custom-commands nil
207 "Options concerning agenda views in Org-mode."
208 :tag "Org Agenda Custom Commands"
209 :group 'org-agenda)
210
211(defconst org-sorting-choice
212 '(choice
213 (const time-up) (const time-down)
214 (const category-keep) (const category-up) (const category-down)
215 (const tag-down) (const tag-up)
216 (const priority-up) (const priority-down)
621f83e4 217 (const todo-state-up) (const todo-state-down)
c8d0cf5c 218 (const effort-up) (const effort-down)
8bfe682a 219 (const habit-up) (const habit-down)
86fbb8ca 220 (const alpha-up) (const alpha-down)
c8d0cf5c 221 (const user-defined-up) (const user-defined-down))
20908596
CD
222 "Sorting choices.")
223
224(defconst org-agenda-custom-commands-local-options
225 `(repeat :tag "Local settings for this command. Remember to quote values"
226 (choice :tag "Setting"
c8d0cf5c
CD
227 (list :tag "Heading for this block"
228 (const org-agenda-overriding-header)
229 (string :tag "Headline"))
20908596
CD
230 (list :tag "Files to be searched"
231 (const org-agenda-files)
232 (list
233 (const :format "" quote)
c8d0cf5c 234 (repeat (file))))
20908596
CD
235 (list :tag "Sorting strategy"
236 (const org-agenda-sorting-strategy)
237 (list
238 (const :format "" quote)
239 (repeat
240 ,org-sorting-choice)))
241 (list :tag "Prefix format"
242 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
243 (string))
244 (list :tag "Number of days in agenda"
acedf35c
CD
245 (const org-agenda-span)
246 (choice (const :tag "Day" 'day)
247 (const :tag "Week" 'week)
248 (const :tag "Month" 'month)
249 (const :tag "Year" 'year)
250 (integer :tag "Custom")))
20908596
CD
251 (list :tag "Fixed starting date"
252 (const org-agenda-start-day)
253 (string :value "2007-11-01"))
254 (list :tag "Start on day of week"
255 (const org-agenda-start-on-weekday)
256 (choice :value 1
257 (const :tag "Today" nil)
c8d0cf5c 258 (integer :tag "Weekday No.")))
20908596
CD
259 (list :tag "Include data from diary"
260 (const org-agenda-include-diary)
261 (boolean))
262 (list :tag "Deadline Warning days"
263 (const org-deadline-warning-days)
264 (integer :value 1))
c8d0cf5c
CD
265 (list :tag "Tags filter preset"
266 (const org-agenda-filter-preset)
267 (list
268 (const :format "" quote)
269 (repeat
270 (string :tag "+tag or -tag"))))
ed21c5c8
CD
271 (list :tag "Set daily/weekly entry types"
272 (const org-agenda-entry-types)
273 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
274 (const :deadline)
275 (const :scheduled)
276 (const :timestamp)
277 (const :sexp)))
20908596
CD
278 (list :tag "Standard skipping condition"
279 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
280 (const org-agenda-skip-function)
281 (list
282 (const :format "" quote)
283 (list
284 (choice
33306645 285 :tag "Skipping range"
20908596
CD
286 (const :tag "Skip entry" org-agenda-skip-entry-if)
287 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
288 (repeat :inline t :tag "Conditions for skipping"
289 (choice
290 :tag "Condition type"
291 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
292 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
ed21c5c8
CD
293 (list :tag "TODO state is" :inline t
294 (const 'todo)
295 (choice
296 (const :tag "any not-done state" 'todo)
297 (const :tag "any done state" 'done)
298 (const :tag "any state" 'any)
299 (list :tag "Keyword list"
300 (const :format "" quote)
301 (repeat (string :tag "Keyword")))))
302 (list :tag "TODO state is not" :inline t
303 (const 'nottodo)
304 (choice
305 (const :tag "any not-done state" 'todo)
306 (const :tag "any done state" 'done)
307 (const :tag "any state" 'any)
308 (list :tag "Keyword list"
309 (const :format "" quote)
310 (repeat (string :tag "Keyword")))))
20908596
CD
311 (const :tag "scheduled" 'scheduled)
312 (const :tag "not scheduled" 'notscheduled)
313 (const :tag "deadline" 'deadline)
c8d0cf5c
CD
314 (const :tag "no deadline" 'notdeadline)
315 (const :tag "timestamp" 'timestamp)
316 (const :tag "no timestamp" 'nottimestamp))))))
20908596
CD
317 (list :tag "Non-standard skipping condition"
318 :value (org-agenda-skip-function)
2c3ad40d 319 (const org-agenda-skip-function)
c8d0cf5c
CD
320 (sexp :tag "Function or form (quoted!)"))
321 (list :tag "Any variable"
322 (variable :tag "Variable")
323 (sexp :tag "Value (sexp)"))))
20908596
CD
324 "Selection of examples for agenda command settings.
325This will be spliced into the custom type of
326`org-agenda-custom-commands'.")
327
328
329(defcustom org-agenda-custom-commands nil
330 "Custom commands for the agenda.
331These commands will be offered on the splash screen displayed by the
332agenda dispatcher \\[org-agenda]. Each entry is a list like this:
333
334 (key desc type match settings files)
335
336key The key (one or more characters as a string) to be associated
337 with the command.
338desc A description of the command, when omitted or nil, a default
339 description is built using MATCH.
340type The command type, any of the following symbols:
341 agenda The daily/weekly agenda.
342 todo Entries with a specific TODO keyword, in all agenda files.
343 search Entries containing search words entry or headline.
344 tags Tags/Property/TODO match in all agenda files.
345 tags-todo Tags/P/T match in all agenda files, TODO entries only.
346 todo-tree Sparse tree of specific TODO keyword in *current* file.
347 tags-tree Sparse tree with all tags matches in *current* file.
348 occur-tree Occur sparse tree for *current* file.
349 ... A user-defined function.
350match What to search for:
351 - a single keyword for TODO keyword searches
352 - a tags match expression for tags searches
353 - a word search expression for text searches.
354 - a regular expression for occur searches
355 For all other commands, this should be the empty string.
356settings A list of option settings, similar to that in a let form, so like
357 this: ((opt1 val1) (opt2 val2) ...). The values will be
358 evaluated at the moment of execution, so quote them when needed.
359files A list of files file to write the produced agenda buffer to
360 with the command `org-store-agenda-views'.
361 If a file name ends in \".html\", an HTML version of the buffer
362 is written out. If it ends in \".ps\", a postscript version is
33306645 363 produced. Otherwise, only the plain text is written to the file.
20908596
CD
364
365You can also define a set of commands, to create a composite agenda buffer.
366In this case, an entry looks like this:
367
368 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
369
370where
371
372desc A description string to be displayed in the dispatcher menu.
373cmd An agenda command, similar to the above. However, tree commands
374 are no allowed, but instead you can get agenda and global todo list.
375 So valid commands for a set are:
376 (agenda \"\" settings)
377 (alltodo \"\" settings)
378 (stuck \"\" settings)
379 (todo \"match\" settings files)
380 (search \"match\" settings files)
381 (tags \"match\" settings files)
382 (tags-todo \"match\" settings files)
383
384Each command can carry a list of options, and another set of options can be
385given for the whole set of commands. Individual command options take
386precedence over the general options.
387
388When using several characters as key to a command, the first characters
389are prefix commands. For the dispatcher to display useful information, you
390should provide a description for the prefix, like
391
392 (setq org-agenda-custom-commands
393 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
394 (\"hl\" tags \"+HOME+Lisa\")
395 (\"hp\" tags \"+HOME+Peter\")
396 (\"hk\" tags \"+HOME+Kim\")))"
397 :group 'org-agenda-custom-commands
398 :type `(repeat
399 (choice :value ("x" "Describe command here" tags "" nil)
400 (list :tag "Single command"
401 (string :tag "Access Key(s) ")
402 (option (string :tag "Description"))
403 (choice
404 (const :tag "Agenda" agenda)
405 (const :tag "TODO list" alltodo)
406 (const :tag "Search words" search)
407 (const :tag "Stuck projects" stuck)
c8d0cf5c
CD
408 (const :tag "Tags/Property match (all agenda files)" tags)
409 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
20908596
CD
410 (const :tag "TODO keyword search (all agenda files)" todo)
411 (const :tag "Tags sparse tree (current buffer)" tags-tree)
412 (const :tag "TODO keyword tree (current buffer)" todo-tree)
413 (const :tag "Occur tree (current buffer)" occur-tree)
414 (sexp :tag "Other, user-defined function"))
415 (string :tag "Match (only for some commands)")
416 ,org-agenda-custom-commands-local-options
417 (option (repeat :tag "Export" (file :tag "Export to"))))
418 (list :tag "Command series, all agenda files"
419 (string :tag "Access Key(s)")
420 (string :tag "Description ")
421 (repeat :tag "Component"
422 (choice
423 (list :tag "Agenda"
424 (const :format "" agenda)
425 (const :tag "" :format "" "")
426 ,org-agenda-custom-commands-local-options)
427 (list :tag "TODO list (all keywords)"
428 (const :format "" alltodo)
429 (const :tag "" :format "" "")
430 ,org-agenda-custom-commands-local-options)
431 (list :tag "Search words"
432 (const :format "" search)
433 (string :tag "Match")
434 ,org-agenda-custom-commands-local-options)
435 (list :tag "Stuck projects"
436 (const :format "" stuck)
437 (const :tag "" :format "" "")
438 ,org-agenda-custom-commands-local-options)
439 (list :tag "Tags search"
440 (const :format "" tags)
441 (string :tag "Match")
442 ,org-agenda-custom-commands-local-options)
443 (list :tag "Tags search, TODO entries only"
444 (const :format "" tags-todo)
445 (string :tag "Match")
446 ,org-agenda-custom-commands-local-options)
447 (list :tag "TODO keyword search"
448 (const :format "" todo)
449 (string :tag "Match")
450 ,org-agenda-custom-commands-local-options)
451 (list :tag "Other, user-defined function"
452 (symbol :tag "function")
453 (string :tag "Match")
454 ,org-agenda-custom-commands-local-options)))
455
456 (repeat :tag "Settings for entire command set"
457 (list (variable :tag "Any variable")
458 (sexp :tag "Value")))
459 (option (repeat :tag "Export" (file :tag "Export to"))))
460 (cons :tag "Prefix key documentation"
461 (string :tag "Access Key(s)")
462 (string :tag "Description ")))))
463
464(defcustom org-agenda-query-register ?o
465 "The register holding the current query string.
33306645 466The purpose of this is that if you construct a query string interactively,
20908596
CD
467you can then use it to define a custom command."
468 :group 'org-agenda-custom-commands
469 :type 'character)
470
471(defcustom org-stuck-projects
472 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
473 "How to identify stuck projects.
474This is a list of four items:
c8d0cf5c
CD
4751. A tags/todo/property matcher string that is used to identify a project.
476 See the manual for a description of tag and property searches.
20908596
CD
477 The entire tree below a headline matched by this is considered one project.
4782. A list of TODO keywords identifying non-stuck projects.
479 If the project subtree contains any headline with one of these todo
480 keywords, the project is considered to be not stuck. If you specify
481 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
4823. A list of tags identifying non-stuck projects.
483 If the project subtree contains any headline with one of these tags,
484 the project is considered to be not stuck. If you specify \"*\" as
c8d0cf5c
CD
485 a tag, any tag will mark the project unstuck. Note that this is about
486 the explicit presence of a tag somewhere in the subtree, inherited
487 tags to not count here. If inherited tags make a project not stuck,
488 use \"-TAG\" in the tags part of the matcher under (1.) above.
20908596
CD
4894. An arbitrary regular expression matching non-stuck projects.
490
c8d0cf5c
CD
491If the project turns out to be not stuck, search continues also in the
492subtree to see if any of the subtasks have project status.
493
494See also the variable `org-tags-match-list-sublevels' which applies
495to projects matched by this search as well.
496
20908596
CD
497After defining this variable, you may use \\[org-agenda-list-stuck-projects]
498or `C-c a #' to produce the list."
499 :group 'org-agenda-custom-commands
500 :type '(list
501 (string :tag "Tags/TODO match to identify a project")
502 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
503 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
c8d0cf5c 504 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
20908596 505
71d35b24
CD
506(defcustom org-agenda-filter-effort-default-operator "<"
507 "The default operator for effort estimate filtering.
93b62de8 508If you select an effort estimate limit without first pressing an operator,
71d35b24
CD
509this one will be used."
510 :group 'org-agenda-custom-commands
511 :type '(choice (const :tag "less or equal" "<")
512 (const :tag "greater or equal"">")
513 (const :tag "equal" "=")))
20908596
CD
514
515(defgroup org-agenda-skip nil
516 "Options concerning skipping parts of agenda files."
517 :tag "Org Agenda Skip"
518 :group 'org-agenda)
0bd48b37
CD
519(defgroup org-agenda-daily/weekly nil
520 "Options concerning the daily/weekly agenda."
521 :tag "Org Agenda Daily/Weekly"
522 :group 'org-agenda)
523(defgroup org-agenda-todo-list nil
524 "Options concerning the global todo list agenda view."
525 :tag "Org Agenda Todo List"
526 :group 'org-agenda)
527(defgroup org-agenda-match-view nil
528 "Options concerning the general tags/property/todo match agenda view."
529 :tag "Org Agenda Match View"
530 :group 'org-agenda)
8bfe682a
CD
531(defgroup org-agenda-search-view nil
532 "Options concerning the general tags/property/todo match agenda view."
533 :tag "Org Agenda Match View"
534 :group 'org-agenda)
20908596 535
2c3ad40d 536(defvar org-agenda-archives-mode nil
ed21c5c8 537 "Non-nil means the agenda will include archived items.
2c3ad40d
CD
538If this is the symbol `trees', trees in the selected agenda scope
539that are marked with the ARCHIVE tag will be included anyway. When this is
540t, also all archive files associated with the current selection of agenda
541files will be included.")
542
b349f79f 543(defcustom org-agenda-skip-comment-trees t
ed21c5c8 544 "Non-nil means skip trees that start with the COMMENT keyword.
33306645 545When nil, these trees are also scanned by agenda commands."
b349f79f
CD
546 :group 'org-agenda-skip
547 :type 'boolean)
548
20908596 549(defcustom org-agenda-todo-list-sublevels t
ed21c5c8 550 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
20908596
CD
551When nil, the sublevels of a TODO entry are not checked, resulting in
552potentially much shorter TODO lists."
553 :group 'org-agenda-skip
0bd48b37 554 :group 'org-agenda-todo-list
20908596
CD
555 :type 'boolean)
556
557(defcustom org-agenda-todo-ignore-with-date nil
ed21c5c8 558 "Non-nil means don't show entries with a date in the global todo list.
20908596
CD
559You can use this if you prefer to mark mere appointments with a TODO keyword,
560but don't want them to show up in the TODO list.
561When this is set, it also covers deadlines and scheduled items, the settings
562of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
c8d0cf5c
CD
563will be ignored.
564See also the variable `org-agenda-tags-todo-honor-ignore-options'."
20908596 565 :group 'org-agenda-skip
0bd48b37 566 :group 'org-agenda-todo-list
20908596
CD
567 :type 'boolean)
568
acedf35c
CD
569(defcustom org-agenda-todo-ignore-timestamp nil
570 "Non-nil means don't show entries with a timestamp.
571This applies when creating the global todo list.
572Valid values are:
573
574past Don't show entries for today or in the past.
575
576future Don't show entries with a timestamp in the future.
577 The idea behind this is that if it has a future
578 timestamp, you don't want to think about it until the
579 date.
580
581all Don't show any entries with a timestamp in the global todo list.
582 The idea behind this is that by setting a timestamp, you
583 have already \"taken care\" of this item.
584
585See also `org-agenda-todo-ignore-with-date'.
586See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
587to make his option also apply to the tags-todo list."
588 :group 'org-agenda-skip
589 :group 'org-agenda-todo-list
590 :type '(choice
591 (const :tag "Ignore future timestamp todos" future)
592 (const :tag "Ignore past or present timestamp todos" past)
593 (const :tag "Ignore all timestamp todos" all)
594 (const :tag "Show timestamp todos" nil)))
595
20908596 596(defcustom org-agenda-todo-ignore-scheduled nil
ed21c5c8
CD
597 "Non-nil means, ignore some scheduled TODO items when making TODO list.
598This applies when creating the global todo list.
599Valid values are:
600
601past Don't show entries scheduled today or in the past.
602
603future Don't show entries scheduled in the future.
604 The idea behind this is that by scheduling it, you don't want to
605 think about it until the scheduled date.
606
607all Don't show any scheduled entries in the global todo list.
608 The idea behind this is that by scheduling it, you have already
609 \"taken care\" of this item.
610
611t Same as `all', for backward compatibility.
612
c8d0cf5c 613See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
614See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
615to make his option also apply to the tags-todo list."
20908596 616 :group 'org-agenda-skip
0bd48b37 617 :group 'org-agenda-todo-list
ed21c5c8
CD
618 :type '(choice
619 (const :tag "Ignore future-scheduled todos" future)
620 (const :tag "Ignore past- or present-scheduled todos" past)
621 (const :tag "Ignore all scheduled todos" all)
622 (const :tag "Ignore all scheduled todos (compatibility)" t)
623 (const :tag "Show scheduled todos" nil)))
20908596
CD
624
625(defcustom org-agenda-todo-ignore-deadlines nil
ed21c5c8
CD
626 "Non-nil means ignore some deadlined TODO items when making TODO list.
627There are different motivations for using different values, please think
628carefully when configuring this variable.
629
86fbb8ca 630This applies when creating the global todo list.
ed21c5c8
CD
631Valid values are:
632
633near Don't show near deadline entries. A deadline is near when it is
634 closer than `org-deadline-warning-days' days. The idea behind this
635 is that such items will appear in the agenda anyway.
636
637far Don't show TODO entries where a deadline has been defined, but
638 the deadline is not near. This is useful if you don't want to
639 use the todo list to figure out what to do now.
640
641past Don't show entries with a deadline timestamp for today or in the past.
642
643future Don't show entries with a deadline timestamp in the future, not even
644 when they become `near' ones. Use it with caution.
645
646all Ignore all TODO entries that do have a deadline.
647
648t Same as `near', for backward compatibility.
649
c8d0cf5c 650See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
651See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
652to make his option also apply to the tags-todo list."
20908596 653 :group 'org-agenda-skip
0bd48b37 654 :group 'org-agenda-todo-list
ed21c5c8
CD
655 :type '(choice
656 (const :tag "Ignore near deadlines" near)
657 (const :tag "Ignore near deadlines (compatibility)" t)
658 (const :tag "Ignore far deadlines" far)
659 (const :tag "Ignore all TODOs with a deadlines" all)
660 (const :tag "Show all TODOs, even if they have a deadline" nil)))
0bd48b37
CD
661
662(defcustom org-agenda-tags-todo-honor-ignore-options nil
ed21c5c8 663 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
0bd48b37
CD
664The variables
665 `org-agenda-todo-ignore-with-date',
acedf35c
CD
666 `org-agenda-todo-ignore-timestamp',
667 `org-agenda-todo-ignore-scheduled',
0bd48b37
CD
668 `org-agenda-todo-ignore-deadlines'
669make the global TODO list skip entries that have time stamps of certain
670kinds. If this option is set, the same options will also apply for the
671tags-todo search, which is the general tags/property matcher
672restricted to unfinished TODO entries only."
673 :group 'org-agenda-skip
674 :group 'org-agenda-todo-list
675 :group 'org-agenda-match-view
20908596
CD
676 :type 'boolean)
677
678(defcustom org-agenda-skip-scheduled-if-done nil
679 "Non-nil means don't show scheduled items in agenda when they are done.
680This is relevant for the daily/weekly agenda, not for the TODO list. And
681it applies only to the actual date of the scheduling. Warnings about
682an item with a past scheduling dates are always turned off when the item
683is DONE."
684 :group 'org-agenda-skip
0bd48b37 685 :group 'org-agenda-daily/weekly
20908596
CD
686 :type 'boolean)
687
54a0dee5
CD
688(defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
689 "Non-nil means skip scheduling line if same entry shows because of deadline.
690In the agenda of today, an entry can show up multiple times because
691it is both scheduled and has a nearby deadline, and maybe a plain time
692stamp as well.
693When this variable is t, then only the deadline is shown and the fact that
694the entry is scheduled today or was scheduled previously is not shown.
695When this variable is nil, the entry will be shown several times. When
696the variable is the symbol `not-today', then skip scheduled previously,
697but not scheduled today."
698 :group 'org-agenda-skip
699 :group 'org-agenda-daily/weekly
700 :type '(choice
701 (const :tag "Never" nil)
702 (const :tag "Always" t)
703 (const :tag "Not when scheduled today" not-today)))
704
20908596 705(defcustom org-agenda-skip-deadline-if-done nil
33306645 706 "Non-nil means don't show deadlines when the corresponding item is done.
20908596
CD
707When nil, the deadline is still shown and should give you a happy feeling.
708This is relevant for the daily/weekly agenda. And it applied only to the
33306645 709actually date of the deadline. Warnings about approaching and past-due
20908596
CD
710deadlines are always turned off when the item is DONE."
711 :group 'org-agenda-skip
0bd48b37 712 :group 'org-agenda-daily/weekly
20908596
CD
713 :type 'boolean)
714
ed21c5c8
CD
715(defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
716 "Non-nil means skip deadline prewarning when entry is also scheduled.
717This will apply on all days where a prewarning for the deadline would
718be shown, but not at the day when the entry is actually due. On that day,
719the deadline will be shown anyway.
720This variable may be set to nil, t, or a number which will then give
721the number of days before the actual deadline when the prewarnings
722should resume.
723This can be used in a workflow where the first showing of the deadline will
724trigger you to schedule it, and then you don't want to be reminded of it
725because you will take care of it on the day when scheduled."
726 :group 'org-agenda-skip
727 :group 'org-agenda-daily/weekly
728 :type '(choice
729 (const :tag "Alwas show prewarning" nil)
730 (const :tag "Remove prewarning if entry is scheduled" t)
731 (integer :tag "Restart prewarning N days before deadline")))
732
c8d0cf5c
CD
733(defcustom org-agenda-skip-additional-timestamps-same-entry t
734 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
735When non-nil, after the search for timestamps has matched once in an
736entry, the rest of the entry will not be searched."
737 :group 'org-agenda-skip
738 :type 'boolean)
739
20908596
CD
740(defcustom org-agenda-skip-timestamp-if-done nil
741 "Non-nil means don't select item by timestamp or -range if it is DONE."
742 :group 'org-agenda-skip
0bd48b37 743 :group 'org-agenda-daily/weekly
20908596
CD
744 :type 'boolean)
745
d6685abc 746(defcustom org-agenda-dim-blocked-tasks t
ed21c5c8 747 "Non-nil means dim blocked tasks in the agenda display.
c8d0cf5c
CD
748This causes some overhead during agenda construction, but if you
749have turned on `org-enforce-todo-dependencies',
750`org-enforce-todo-checkbox-dependencies', or any other blocking
751mechanism, this will create useful feedback in the agenda.
752
8bfe682a 753Instead of t, this variable can also have the value `invisible'.
c8d0cf5c
CD
754Then blocked tasks will be invisible and only become visible when
755they become unblocked. An exemption to this behavior is when a task is
756blocked because of unchecked checkboxes below it. Since checkboxes do
757not show up in the agenda views, making this task invisible you remove any
758trace from agenda views that there is something to do. Therefore, a task
759that is blocked because of checkboxes will never be made invisible, it
760will only be dimmed."
d6685abc
CD
761 :group 'org-agenda-daily/weekly
762 :group 'org-agenda-todo-list
763 :type '(choice
764 (const :tag "Do not dim" nil)
765 (const :tag "Dim to a grey face" t)
8bfe682a 766 (const :tag "Make invisible" invisible)))
d6685abc 767
20908596 768(defcustom org-timeline-show-empty-dates 3
ed21c5c8 769 "Non-nil means `org-timeline' also shows dates without an entry.
20908596
CD
770When nil, only the days which actually have entries are shown.
771When t, all days between the first and the last date are shown.
772When an integer, show also empty dates, but if there is a gap of more than
773N days, just insert a special line indicating the size of the gap."
774 :group 'org-agenda-skip
775 :type '(choice
776 (const :tag "None" nil)
777 (const :tag "All" t)
c8d0cf5c 778 (integer :tag "at most")))
20908596 779
20908596
CD
780(defgroup org-agenda-startup nil
781 "Options concerning initial settings in the Agenda in Org Mode."
782 :tag "Org Agenda Startup"
783 :group 'org-agenda)
784
afe98dfa
CD
785(defcustom org-agenda-menu-show-matcher t
786 "Non-nil menas show the match string in the agenda dispatcher menu.
787When nil, the matcher string is not shown, but is put into the help-echo
788property so than moving the mouse over the command shows it.
789Setting it to nil is good if matcher strings are very long and/or if
790you wnat to use two-column display (see `org-agenda-menu-two-column')."
791 :group 'org-agenda
792 :type 'boolean)
793
794(defcustom org-agenda-menu-two-column nil
795 "Non-nil means, use two columns to show custom commands in the dispatcher.
796If you use this, you probably want to set `org-agenda-menu-show-matcher'
797to nil."
798 :group 'org-agenda
799 :type 'boolean)
800
20908596
CD
801(defcustom org-finalize-agenda-hook nil
802 "Hook run just before displaying an agenda buffer."
803 :group 'org-agenda-startup
804 :type 'hook)
805
806(defcustom org-agenda-mouse-1-follows-link nil
ed21c5c8 807 "Non-nil means mouse-1 on a link will follow the link in the agenda.
20908596
CD
808A longer mouse click will still set point. Does not work on XEmacs.
809Needs to be set before org.el is loaded."
810 :group 'org-agenda-startup
811 :type 'boolean)
812
813(defcustom org-agenda-start-with-follow-mode nil
86fbb8ca 814 "The initial value of follow mode in a newly created agenda window."
20908596
CD
815 :group 'org-agenda-startup
816 :type 'boolean)
817
1bcdebed 818(defcustom org-agenda-show-outline-path t
ed21c5c8 819 "Non-nil means show outline path in echo area after line motion."
1bcdebed
CD
820 :group 'org-agenda-startup
821 :type 'boolean)
822
54a0dee5
CD
823(defcustom org-agenda-start-with-entry-text-mode nil
824 "The initial value of entry-text-mode in a newly created agenda window."
825 :group 'org-agenda-startup
826 :type 'boolean)
827
828(defcustom org-agenda-entry-text-maxlines 5
8bfe682a 829 "Number of text lines to be added when `E' is pressed in the agenda.
54a0dee5
CD
830
831Note that this variable only used during agenda display. Add add entry text
832when exporting the agenda, configure the variable
833`org-agenda-add-entry-ext-maxlines'."
834 :group 'org-agenda
835 :type 'integer)
836
8d642074
CD
837(defcustom org-agenda-entry-text-exclude-regexps nil
838 "List of regular expressions to clean up entry text.
839The complete matches of all regular expressions in this list will be
840removed from entry text before it is shown in the agenda."
841 :group 'org-agenda
842 :type '(repeat (regexp)))
843
844(defvar org-agenda-entry-text-cleanup-hook nil
845 "Hook that is run after basic cleanup of entry text to be shown in agenda.
846This cleanup is done in a temporary buffer, so the function may inspect and
847change the entire buffer.
848Some default stuff like drawers and scheduling/deadline dates will already
849have been removed when this is called, as will any matches for regular
850expressions listed in `org-agenda-entry-text-exclude-regexps'.")
851
20908596 852(defvar org-agenda-include-inactive-timestamps nil
ed21c5c8 853 "Non-nil means include inactive time stamps in agenda and timeline.")
20908596
CD
854
855(defgroup org-agenda-windows nil
856 "Options concerning the windows used by the Agenda in Org Mode."
857 :tag "Org Agenda Windows"
858 :group 'org-agenda)
859
860(defcustom org-agenda-window-setup 'reorganize-frame
861 "How the agenda buffer should be displayed.
862Possible values for this option are:
863
864current-window Show agenda in the current window, keeping all other windows.
20908596
CD
865other-window Use `switch-to-buffer-other-window' to display agenda.
866reorganize-frame Show only two windows on the current frame, the current
867 window and the agenda.
8d642074
CD
868other-frame Use `switch-to-buffer-other-frame' to display agenda.
869 Also, when exiting the agenda, kill that frame.
20908596
CD
870See also the variable `org-agenda-restore-windows-after-quit'."
871 :group 'org-agenda-windows
872 :type '(choice
873 (const current-window)
874 (const other-frame)
875 (const other-window)
876 (const reorganize-frame)))
877
878(defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
879 "The min and max height of the agenda window as a fraction of frame height.
880The value of the variable is a cons cell with two numbers between 0 and 1.
881It only matters if `org-agenda-window-setup' is `reorganize-frame'."
882 :group 'org-agenda-windows
883 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
884
885(defcustom org-agenda-restore-windows-after-quit nil
ed21c5c8 886 "Non-nil means restore window configuration open exiting agenda.
20908596
CD
887Before the window configuration is changed for displaying the agenda,
888the current status is recorded. When the agenda is exited with
889`q' or `x' and this option is set, the old state is restored. If
890`org-agenda-window-setup' is `other-frame', the value of this
baf0cb84 891option will be ignored."
20908596
CD
892 :group 'org-agenda-windows
893 :type 'boolean)
894
acedf35c
CD
895(defcustom org-agenda-ndays nil
896 "Number of days to include in overview display.
c8d0cf5c 897Should be 1 or 7.
acedf35c
CD
898Obsolete, see `org-agenda-span'."
899 :group 'org-agenda-daily/weekly
900 :type 'integer)
901
902(make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
903
904(defcustom org-agenda-span 'week
905 "Number of days to include in overview display.
906Can be day, week, month, year, or any number of days.
c8d0cf5c 907Custom commands can set this variable in the options section."
20908596 908 :group 'org-agenda-daily/weekly
acedf35c
CD
909 :type '(choice (const :tag "Day" day)
910 (const :tag "Week" week)
911 (const :tag "Month" month)
912 (const :tag "Year" year)
913 (integer :tag "Custom")))
20908596
CD
914
915(defcustom org-agenda-start-on-weekday 1
ed21c5c8 916 "Non-nil means start the overview always on the specified weekday.
20908596 9170 denotes Sunday, 1 denotes Monday etc.
c8d0cf5c
CD
918When nil, always start on the current day.
919Custom commands can set this variable in the options section."
20908596
CD
920 :group 'org-agenda-daily/weekly
921 :type '(choice (const :tag "Today" nil)
c8d0cf5c 922 (integer :tag "Weekday No.")))
20908596
CD
923
924(defcustom org-agenda-show-all-dates t
ed21c5c8 925 "Non-nil means `org-agenda' shows every day in the selected range.
20908596
CD
926When nil, only the days which actually have entries are shown."
927 :group 'org-agenda-daily/weekly
928 :type 'boolean)
929
930(defcustom org-agenda-format-date 'org-agenda-format-date-aligned
931 "Format string for displaying dates in the agenda.
932Used by the daily/weekly agenda and by the timeline. This should be
933a format string understood by `format-time-string', or a function returning
934the formatted date as a string. The function must take a single argument,
935a calendar-style date list like (month day year)."
936 :group 'org-agenda-daily/weekly
937 :type '(choice
938 (string :tag "Format string")
939 (function :tag "Function")))
940
941(defun org-agenda-format-date-aligned (date)
942 "Format a date string for display in the daily/weekly agenda, or timeline.
943This function makes sure that dates are aligned for easy reading."
944 (require 'cal-iso)
945 (let* ((dayname (calendar-day-name date))
946 (day (cadr date))
947 (day-of-week (calendar-day-of-week date))
948 (month (car date))
949 (monthname (calendar-month-name month))
950 (year (nth 2 date))
951 (iso-week (org-days-to-iso-week
952 (calendar-absolute-from-gregorian date)))
953 (weekyear (cond ((and (= month 1) (>= iso-week 52))
954 (1- year))
955 ((and (= month 12) (<= iso-week 1))
956 (1+ year))
957 (t year)))
958 (weekstring (if (= day-of-week 1)
959 (format " W%02d" iso-week)
960 "")))
961 (format "%-10s %2d %s %4d%s"
962 dayname day monthname year weekstring)))
963
ed21c5c8
CD
964(defcustom org-agenda-time-leading-zero nil
965 "Non-nil means use leading zero for military times in agenda.
966For example, 9:30am would become 09:30 rather than 9:30."
967 :group 'org-agenda-daily/weekly
968 :type 'boolean)
969
acedf35c
CD
970(defcustom org-agenda-timegrid-use-ampm nil
971 "When set, show AM/PM style timestamps on the timegrid."
972 :group 'org-agenda
973 :type 'boolean)
974
975(defun org-agenda-time-of-day-to-ampm (time)
976 "Convert TIME of a string like '13:45' to an AM/PM style time string."
977 (let* ((hour-number (string-to-number (substring time 0 -3)))
978 (minute (substring time -2))
979 (ampm "am"))
980 (cond
981 ((equal hour-number 12)
982 (setq ampm "pm"))
983 ((> hour-number 12)
984 (setq ampm "pm")
985 (setq hour-number (- hour-number 12))))
986 (concat
987 (if org-agenda-time-leading-zero
988 (format "%02d" hour-number)
989 (format "%02s" (number-to-string hour-number)))
990 ":" minute ampm)))
991
992(defun org-agenda-time-of-day-to-ampm-maybe (time)
993 "Conditionally convert TIME to AM/PM format
994based on `org-agenda-timegrid-use-ampm'"
995 (if org-agenda-timegrid-use-ampm
996 (org-agenda-time-of-day-to-ampm time)
997 time))
998
20908596
CD
999(defcustom org-agenda-weekend-days '(6 0)
1000 "Which days are weekend?
1001These days get the special face `org-agenda-date-weekend' in the agenda
1002and timeline buffers."
1003 :group 'org-agenda-daily/weekly
1004 :type '(set :greedy t
1005 (const :tag "Monday" 1)
1006 (const :tag "Tuesday" 2)
1007 (const :tag "Wednesday" 3)
1008 (const :tag "Thursday" 4)
1009 (const :tag "Friday" 5)
1010 (const :tag "Saturday" 6)
1011 (const :tag "Sunday" 0)))
1012
1013(defcustom org-agenda-include-diary nil
c8d0cf5c
CD
1014 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1015Custom commands can set this variable in the options section."
20908596
CD
1016 :group 'org-agenda-daily/weekly
1017 :type 'boolean)
1018
ed21c5c8
CD
1019(defcustom org-agenda-include-deadlines t
1020 "If non-nil, include entries within their deadline warning period.
1021Custom commands can set this variable in the options section."
1022 :group 'org-agenda-daily/weekly
1023 :type 'boolean)
1024
20908596
CD
1025(defcustom org-agenda-include-all-todo nil
1026 "Set means weekly/daily agenda will always contain all TODO entries.
1027The TODO entries will be listed at the top of the agenda, before
0bd48b37
CD
1028the entries for specific days.
1029This option is deprecated, it is better to define a block agenda instead."
20908596
CD
1030 :group 'org-agenda-daily/weekly
1031 :type 'boolean)
1032
1033(defcustom org-agenda-repeating-timestamp-show-all t
ed21c5c8 1034 "Non-nil means show all occurrences of a repeating stamp in the agenda.
33306645 1035When nil, only one occurrence is shown, either today or the
20908596
CD
1036nearest into the future."
1037 :group 'org-agenda-daily/weekly
1038 :type 'boolean)
1039
1040(defcustom org-scheduled-past-days 10000
1041 "No. of days to continue listing scheduled items that are not marked DONE.
1042When an item is scheduled on a date, it shows up in the agenda on this
1043day and will be listed until it is marked done for the number of days
1044given here."
1045 :group 'org-agenda-daily/weekly
c8d0cf5c 1046 :type 'integer)
20908596 1047
93b62de8
CD
1048(defcustom org-agenda-log-mode-items '(closed clock)
1049 "List of items that should be shown in agenda log mode.
1050This list may contain the following symbols:
1051
1052 closed Show entries that have been closed on that day.
1053 clock Show entries that have received clocked time on that day.
c8d0cf5c
CD
1054 state Show all logged state changes.
1055Note that instead of changing this variable, you can also press `C-u l' in
1056the agenda to display all available LOG items temporarily."
93b62de8
CD
1057 :group 'org-agenda-daily/weekly
1058 :type '(set :greedy t (const closed) (const clock) (const state)))
1059
c8d0cf5c 1060(defcustom org-agenda-log-mode-add-notes t
ed21c5c8 1061 "Non-nil means add first line of notes to log entries in agenda views.
c8d0cf5c
CD
1062If a log item like a state change or a clock entry is associated with
1063notes, the first line of these notes will be added to the entry in the
1064agenda display."
1065 :group 'org-agenda-daily/weekly
1066 :type 'boolean)
1067
1068(defcustom org-agenda-start-with-log-mode nil
1069 "The initial value of log-mode in a newly created agenda window."
1070 :group 'org-agenda-startup
1071 :group 'org-agenda-daily/weekly
1072 :type 'boolean)
1073
20908596
CD
1074(defcustom org-agenda-start-with-clockreport-mode nil
1075 "The initial value of clockreport-mode in a newly created agenda window."
1076 :group 'org-agenda-startup
1077 :group 'org-agenda-daily/weekly
1078 :type 'boolean)
1079
1080(defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1081 "Property list with parameters for the clocktable in clockreport mode.
1082This is the display mode that shows a clock table in the daily/weekly
1083agenda, the properties for this dynamic block can be set here.
1084The usual clocktable parameters are allowed here, but you cannot set
1085the properties :name, :tstart, :tend, :block, and :scope - these will
1086be overwritten to make sure the content accurately reflects the
1087current display in the agenda."
1088 :group 'org-agenda-daily/weekly
1089 :type 'plist)
1090
ed21c5c8
CD
1091(defcustom org-agenda-search-view-always-boolean nil
1092 "Non-nil means the search string is interpreted as individual parts.
1093
1094The search string for search view can either be interpreted as a phrase,
1095or as a list of snippets that define a boolean search for a number of
1096strings.
1097
1098When this is non-nil, the string will be split on whitespace, and each
1099snippet will be searched individually, and all must match in order to
1100select an entry. A snippet is then a single string of non-white
1101characters, or a string in double quotes, or a regexp in {} braces.
86fbb8ca 1102If a snippet is preceded by \"-\", the snippet must *not* match.
ed21c5c8
CD
1103\"+\" is syntactic sugar for positive selection. Each snippet may
1104be found as a full word or a partial word, but see the variable
1105`org-agenda-search-view-force-full-words'.
1106
1107When this is nil, search will look for the entire search phrase as one,
1108with each space character matching any amount of whitespace, including
1109line breaks.
1110
1111Even when this is nil, you can still switch to Boolean search dynamically
86fbb8ca 1112by preceding the first snippet with \"+\" or \"-\". If the first snippet
ed21c5c8
CD
1113is a regexp marked with braces like \"{abc}\", this will also switch to
1114boolean search."
1115 :group 'org-agenda-search-view
1116 :type 'boolean)
1117
1118(if (fboundp 'defvaralias)
1119 (defvaralias 'org-agenda-search-view-search-words-only
1120 'org-agenda-search-view-always-boolean))
1121
1122(defcustom org-agenda-search-view-force-full-words nil
86fbb8ca 1123 "Non-nil means, search words must be matches as complete words.
ed21c5c8 1124When nil, they may also match part of a word."
8bfe682a
CD
1125 :group 'org-agenda-search-view
1126 :type 'boolean)
20908596
CD
1127
1128(defgroup org-agenda-time-grid nil
1129 "Options concerning the time grid in the Org-mode Agenda."
1130 :tag "Org Agenda Time Grid"
1131 :group 'org-agenda)
1132
c8d0cf5c 1133(defcustom org-agenda-search-headline-for-time t
ed21c5c8 1134 "Non-nil means search headline for a time-of-day.
c8d0cf5c
CD
1135If the headline contains a time-of-day in one format or another, it will
1136be used to sort the entry into the time sequence of items for a day.
1137Some people have time stamps in the headline that refer to the creation
1138time or so, and then this produces an unwanted side effect. If this is
1139the case for your, use this variable to turn off searching the headline
1140for a time."
1141 :group 'org-agenda-time-grid
1142 :type 'boolean)
1143
20908596 1144(defcustom org-agenda-use-time-grid t
ed21c5c8 1145 "Non-nil means show a time grid in the agenda schedule.
20908596
CD
1146A time grid is a set of lines for specific times (like every two hours between
11478:00 and 20:00). The items scheduled for a day at specific times are
1148sorted in between these lines.
1149For details about when the grid will be shown, and what it will look like, see
1150the variable `org-agenda-time-grid'."
1151 :group 'org-agenda-time-grid
1152 :type 'boolean)
1153
1154(defcustom org-agenda-time-grid
1155 '((daily today require-timed)
1156 "----------------"
1157 (800 1000 1200 1400 1600 1800 2000))
1158
1159 "The settings for time grid for agenda display.
1160This is a list of three items. The first item is again a list. It contains
1161symbols specifying conditions when the grid should be displayed:
1162
1163 daily if the agenda shows a single day
1164 weekly if the agenda shows an entire week
1165 today show grid on current date, independent of daily/weekly display
1166 require-timed show grid only if at least one item has a time specification
1167
b349f79f 1168The second item is a string which will be placed behind the grid time.
20908596
CD
1169
1170The third item is a list of integers, indicating the times that should have
1171a grid line."
1172 :group 'org-agenda-time-grid
1173 :type
1174 '(list
1175 (set :greedy t :tag "Grid Display Options"
1176 (const :tag "Show grid in single day agenda display" daily)
1177 (const :tag "Show grid in weekly agenda display" weekly)
1178 (const :tag "Always show grid for today" today)
1179 (const :tag "Show grid only if any timed entries are present"
1180 require-timed)
1181 (const :tag "Skip grid times already present in an entry"
1182 remove-match))
1183 (string :tag "Grid String")
1184 (repeat :tag "Grid Times" (integer :tag "Time"))))
1185
1186(defgroup org-agenda-sorting nil
1187 "Options concerning sorting in the Org-mode Agenda."
1188 :tag "Org Agenda Sorting"
1189 :group 'org-agenda)
1190
1191(defcustom org-agenda-sorting-strategy
8bfe682a
CD
1192 '((agenda habit-down time-up priority-down category-keep)
1193 (todo priority-down category-keep)
1194 (tags priority-down category-keep)
20908596
CD
1195 (search category-keep))
1196 "Sorting structure for the agenda items of a single day.
1197This is a list of symbols which will be used in sequence to determine
1198if an entry should be listed before another entry. The following
1199symbols are recognized:
1200
c8d0cf5c
CD
1201time-up Put entries with time-of-day indications first, early first
1202time-down Put entries with time-of-day indications first, late first
1203category-keep Keep the default order of categories, corresponding to the
1204 sequence in `org-agenda-files'.
1205category-up Sort alphabetically by category, A-Z.
1206category-down Sort alphabetically by category, Z-A.
1207tag-up Sort alphabetically by last tag, A-Z.
1208tag-down Sort alphabetically by last tag, Z-A.
1209priority-up Sort numerically by priority, high priority last.
1210priority-down Sort numerically by priority, high priority first.
1211todo-state-up Sort by todo state, tasks that are done last.
1212todo-state-down Sort by todo state, tasks that are done first.
1213effort-up Sort numerically by estimated effort, high effort last.
1214effort-down Sort numerically by estimated effort, high effort first.
1215user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1216user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
8bfe682a
CD
1217habit-up Put entries that are habits first
1218habit-down Put entries that are habits last
86fbb8ca
CD
1219alpha-up Sort headlines alphabetically
1220alpha-down Sort headlines alphabetically, reversed
20908596
CD
1221
1222The different possibilities will be tried in sequence, and testing stops
1223if one comparison returns a \"not-equal\". For example, the default
1224 '(time-up category-keep priority-down)
1225means: Pull out all entries having a specified time of day and sort them,
1226in order to make a time schedule for the current day the first thing in the
1227agenda listing for the day. Of the entries without a time indication, keep
1228the grouped in categories, don't sort the categories, but keep them in
1229the sequence given in `org-agenda-files'. Within each category sort by
1230priority.
1231
1232Leaving out `category-keep' would mean that items will be sorted across
1233categories by priority.
1234
1235Instead of a single list, this can also be a set of list for specific
1236contents, with a context symbol in the car of the list, any of
8bfe682a 1237`agenda', `todo', `tags', `search' for the corresponding agenda views.
c8d0cf5c
CD
1238
1239Custom commands can bind this variable in the options section."
20908596
CD
1240 :group 'org-agenda-sorting
1241 :type `(choice
1242 (repeat :tag "General" ,org-sorting-choice)
1243 (list :tag "Individually"
1244 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1245 (repeat ,org-sorting-choice))
1246 (cons (const :tag "Strategy for TODO lists" todo)
1247 (repeat ,org-sorting-choice))
1248 (cons (const :tag "Strategy for Tags matches" tags)
8bfe682a
CD
1249 (repeat ,org-sorting-choice))
1250 (cons (const :tag "Strategy for search matches" search)
20908596
CD
1251 (repeat ,org-sorting-choice)))))
1252
c8d0cf5c
CD
1253(defcustom org-agenda-cmp-user-defined nil
1254 "A function to define the comparison `user-defined'.
1255This function must receive two arguments, agenda entry a and b.
1256If a>b, return +1. If a<b, return -1. If they are equal as seen by
1257the user comparison, return nil.
1258When this is defined, you can make `user-defined-up' and `user-defined-down'
1259part of an agenda sorting strategy."
1260 :group 'org-agenda-sorting
1261 :type 'symbol)
1262
20908596 1263(defcustom org-sort-agenda-notime-is-late t
ed21c5c8 1264 "Non-nil means items without time are considered late.
20908596
CD
1265This is only relevant for sorting. When t, items which have no explicit
1266time like 15:30 will be considered as 99:01, i.e. later than any items which
1267do have a time. When nil, the default time is before 0:00. You can use this
1268option to decide if the schedule for today should come before or after timeless
1269agenda entries."
1270 :group 'org-agenda-sorting
1271 :type 'boolean)
1272
1273(defcustom org-sort-agenda-noeffort-is-high t
ed21c5c8 1274 "Non-nil means items without effort estimate are sorted as high effort.
c8d0cf5c
CD
1275This also applies when filtering an agenda view with respect to the
1276< or > effort operator. Then, tasks with no effort defined will be treated
1277as tasks with high effort.
20908596
CD
1278When nil, such items are sorted as 0 minutes effort."
1279 :group 'org-agenda-sorting
1280 :type 'boolean)
1281
1282(defgroup org-agenda-line-format nil
1283 "Options concerning the entry prefix in the Org-mode agenda display."
1284 :tag "Org Agenda Line Format"
1285 :group 'org-agenda)
1286
1287(defcustom org-agenda-prefix-format
acedf35c 1288 '((agenda . " %i %-12:c%?-12t% s")
20908596 1289 (timeline . " % s")
acedf35c
CD
1290 (todo . " %i %-12:c")
1291 (tags . " %i %-12:c")
1292 (search . " %i %-12:c"))
20908596
CD
1293 "Format specifications for the prefix of items in the agenda views.
1294An alist with four entries, for the different agenda types. The keys to the
1295sublists are `agenda', `timeline', `todo', and `tags'. The values
1296are format strings.
1297This format works similar to a printf format, with the following meaning:
1298
1299 %c the category of the item, \"Diary\" for entries from the diary, or
1300 as given by the CATEGORY keyword or derived from the file name.
acedf35c
CD
1301 %i the icon category of the item, as give in
1302 `org-agenda-category-icon-alist'.
20908596
CD
1303 %T the *last* tag of the item. Last because inherited tags come
1304 first in the list.
1305 %t the time-of-day specification if one applies to the entry, in the
1306 format HH:MM
1307 %s Scheduling/Deadline information, a short string
1308
1309All specifiers work basically like the standard `%s' of printf, but may
1310contain two additional characters: A question mark just after the `%' and
1311a whitespace/punctuation character just before the final letter.
1312
1313If the first character after `%' is a question mark, the entire field
1314will only be included if the corresponding value applies to the
1315current entry. This is useful for fields which should have fixed
1316width when present, but zero width when absent. For example,
1317\"%?-12t\" will result in a 12 character time field if a time of the
1318day is specified, but will completely disappear in entries which do
1319not contain a time.
1320
1321If there is punctuation or whitespace character just before the final
1322format letter, this character will be appended to the field value if
1323the value is not empty. For example, the format \"%-12:c\" leads to
1324\"Diary: \" if the category is \"Diary\". If the category were be
8bfe682a 1325empty, no additional colon would be inserted.
20908596
CD
1326
1327The default value of this option is \" %-12:c%?-12t% s\", meaning:
1328- Indent the line with two space characters
1329- Give the category in a 12 chars wide field, padded with whitespace on
1330 the right (because of `-'). Append a colon if there is a category
1331 (because of `:').
1332- If there is a time-of-day, put it into a 12 chars wide field. If no
1333 time, don't put in an empty field, just skip it (because of '?').
1334- Finally, put the scheduling information and append a whitespace.
1335
1336As another example, if you don't want the time-of-day of entries in
1337the prefix, you could use:
1338
1339 (setq org-agenda-prefix-format \" %-11:c% s\")
1340
1341See also the variables `org-agenda-remove-times-when-in-prefix' and
c8d0cf5c
CD
1342`org-agenda-remove-tags'.
1343
1344Custom commands can set this variable in the options section."
20908596
CD
1345 :type '(choice
1346 (string :tag "General format")
1347 (list :greedy t :tag "View dependent"
1348 (cons (const agenda) (string :tag "Format"))
1349 (cons (const timeline) (string :tag "Format"))
1350 (cons (const todo) (string :tag "Format"))
1351 (cons (const tags) (string :tag "Format"))
1352 (cons (const search) (string :tag "Format"))))
1353 :group 'org-agenda-line-format)
1354
1355(defvar org-prefix-format-compiled nil
1356 "The compiled version of the most recently used prefix format.
1357See the variable `org-agenda-prefix-format'.")
1358
1359(defcustom org-agenda-todo-keyword-format "%-1s"
1360 "Format for the TODO keyword in agenda lines.
1361Set this to something like \"%-12s\" if you want all TODO keywords
1362to occupy a fixed space in the agenda display."
1363 :group 'org-agenda-line-format
1364 :type 'string)
1365
ce4fdcb9
CD
1366(defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1367 "Text preceding timerange entries in the agenda view.
1368This is a list with two strings. The first applies when the range
1369is entirely on one day. The second applies if the range spans several days.
1370The strings may have two \"%d\" format specifiers which will be filled
1371with the sequence number of the days, and the total number of days in the
1372range, respectively."
1373 :group 'org-agenda-line-format
1374 :type '(list
1375 (string :tag "Deadline today ")
1376 (choice :tag "Deadline relative"
1377 (string :tag "Format string")
1378 (function))))
1379
20908596 1380(defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
86fbb8ca 1381 "Text preceding scheduled items in the agenda view.
20908596
CD
1382This is a list with two strings. The first applies when the item is
1383scheduled on the current day. The second applies when it has been scheduled
b349f79f
CD
1384previously, it may contain a %d indicating that this is the nth time that
1385this item is scheduled, due to automatic rescheduling of unfinished items
1386for the following day. So this number is one larger than the number of days
1387that passed since this item was scheduled first."
20908596
CD
1388 :group 'org-agenda-line-format
1389 :type '(list
1390 (string :tag "Scheduled today ")
1391 (string :tag "Scheduled previously")))
1392
ed21c5c8 1393(defcustom org-agenda-inactive-leader "["
86fbb8ca 1394 "Text preceding item pulled into the agenda by inactive time stamps.
ed21c5c8
CD
1395These entries are added to the agenda when pressing \"[\"."
1396 :group 'org-agenda-line-format
1397 :type '(list
1398 (string :tag "Scheduled today ")
1399 (string :tag "Scheduled previously")))
1400
20908596 1401(defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
86fbb8ca 1402 "Text preceding deadline items in the agenda view.
20908596
CD
1403This is a list with two strings. The first applies when the item has its
1404deadline on the current day. The second applies when it is in the past or
1405in the future, it may contain %d to capture how many days away the deadline
1406is (was)."
1407 :group 'org-agenda-line-format
1408 :type '(list
1409 (string :tag "Deadline today ")
1410 (choice :tag "Deadline relative"
1411 (string :tag "Format string")
1412 (function))))
1413
1414(defcustom org-agenda-remove-times-when-in-prefix t
ed21c5c8 1415 "Non-nil means remove duplicate time specifications in agenda items.
20908596
CD
1416When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1417time-of-day specification in a headline or diary entry is extracted and
1418placed into the prefix. If this option is non-nil, the original specification
1419\(a timestamp or -range, or just a plain time(range) specification like
142011:30-4pm) will be removed for agenda display. This makes the agenda less
1421cluttered.
1422The option can be t or nil. It may also be the symbol `beg', indicating
86fbb8ca 1423that the time should only be removed when it is located at the beginning of
20908596
CD
1424the headline/diary entry."
1425 :group 'org-agenda-line-format
1426 :type '(choice
1427 (const :tag "Always" t)
1428 (const :tag "Never" nil)
1429 (const :tag "When at beginning of entry" beg)))
1430
86fbb8ca
CD
1431(defcustom org-agenda-remove-timeranges-from-blocks nil
1432 "Non-nil means remove time ranges specifications in agenda
1433items that span on several days."
1434 :group 'org-agenda-line-format
1435 :type 'boolean)
20908596
CD
1436
1437(defcustom org-agenda-default-appointment-duration nil
1438 "Default duration for appointments that only have a starting time.
1439When nil, no duration is specified in such cases.
1440When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1441 :group 'org-agenda-line-format
1442 :type '(choice
1443 (integer :tag "Minutes")
1444 (const :tag "No default duration")))
1445
ff4be292 1446(defcustom org-agenda-show-inherited-tags t
ed21c5c8 1447 "Non-nil means show inherited tags in each agenda line."
ff4be292
CD
1448 :group 'org-agenda-line-format
1449 :type 'boolean)
20908596 1450
5dec9555
CD
1451(defcustom org-agenda-hide-tags-regexp nil
1452 "Regular expression used to filter away specific tags in agenda views.
1453This means that these tags will be present, but not be shown in the agenda
86fbb8ca 1454line. Secondary filtering will still work on the hidden tags.
afe98dfa 1455Nil means don't hide any tags."
5dec9555
CD
1456 :group 'org-agenda-line-format
1457 :type '(choice
1458 (const :tag "Hide none" nil)
1459 (string :tag "Regexp ")))
1460
20908596 1461(defcustom org-agenda-remove-tags nil
ed21c5c8 1462 "Non-nil means remove the tags from the headline copy in the agenda.
20908596
CD
1463When this is the symbol `prefix', only remove tags when
1464`org-agenda-prefix-format' contains a `%T' specifier."
1465 :group 'org-agenda-line-format
1466 :type '(choice
1467 (const :tag "Always" t)
1468 (const :tag "Never" nil)
1469 (const :tag "When prefix format contains %T" prefix)))
1470
1471(if (fboundp 'defvaralias)
1472 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1473 'org-agenda-remove-tags))
1474
5ace2fe5 1475(defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
20908596
CD
1476 "Shift tags in agenda items to this column.
1477If this number is positive, it specifies the column. If it is negative,
1478it means that the tags should be flushright to that column. For example,
1479-80 works well for a normal 80 character screen."
1480 :group 'org-agenda-line-format
1481 :type 'integer)
1482
1483(if (fboundp 'defvaralias)
1484 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1485
c8d0cf5c 1486(defcustom org-agenda-fontify-priorities 'cookies
ed21c5c8 1487 "Non-nil means highlight low and high priorities in agenda.
20908596 1488When t, the highest priority entries are bold, lowest priority italic.
86fbb8ca 1489However, settings in `org-priority-faces' will overrule these faces.
c8d0cf5c
CD
1490When this variable is the symbol `cookies', only fontify the
1491cookies, not the entire task.
621f83e4
CD
1492This may also be an association list of priority faces, whose
1493keys are the character values of `org-highest-priority',
1494`org-default-priority', and `org-lowest-priority' (the default values
ed21c5c8
CD
1495are ?A, ?B, and ?C, respectively). The face may be a named face, a
1496color as a string, or a list like `(:background \"Red\")'.
1497If it is a color, the variable `org-faces-easy-properties'
1498determines if it is a foreground or a background color."
20908596
CD
1499 :group 'org-agenda-line-format
1500 :type '(choice
1501 (const :tag "Never" nil)
1502 (const :tag "Defaults" t)
c8d0cf5c 1503 (const :tag "Cookies only" cookies)
20908596
CD
1504 (repeat :tag "Specify"
1505 (list (character :tag "Priority" :value ?A)
ed21c5c8
CD
1506 (choice :tag "Face "
1507 (string :tag "Color")
1508 (sexp :tag "Face"))))))
20908596 1509
acedf35c
CD
1510(defcustom org-agenda-day-face-function nil
1511 "Function called to determine what face should be used to display a day.
1512The only argument passed to that function is the day. It should
1513returns a face, or nil if does not want to specify a face and let
1514the normal rules apply."
1515 :group 'org-agenda-line-format
1516 :type 'function)
1517
1518(defcustom org-agenda-category-icon-alist nil
1519 "Alist of category icon to be displayed in agenda views.
1520
1521Each entry should have the following format:
1522
1523 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1524
1525Where CATEGORY-REGEXP is a regexp matching the categories where
1526the icon should be displayed.
1527FILE-OR-DATA either a file path or a string containing image data.
1528
1529The other fields can be ommited safely if not needed:
1530TYPE indicates the image type.
1531DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1532image data.
1533PROPS are additional image attributes to assign to the image,
1534like, e.g. `:ascent center'.
1535
1536 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1537
1538If you want to set the display properties yourself, just put a
1539list as second element:
1540
1541 (CATEGORY-REGEXP (MY PROPERTY LIST))
1542
1543For example, to display a 16px horizontal space for Emacs
1544category, you can use:
1545
1546 (\"Emacs\" '(space . (:width (16))))"
1547 :group 'org-agenda-line-format
1548 :type '(alist :key-type (string :tag "Regexp matching category")
1549 :value-type (choice (list :tag "Icon"
1550 (string :tag "File or data")
1551 (symbol :tag "Type")
1552 (boolean :tag "Data?")
1553 (repeat :tag "Extra image properties" :inline t symbol))
1554 (list :tag "Display properties" sexp))))
1555
20908596
CD
1556(defgroup org-agenda-column-view nil
1557 "Options concerning column view in the agenda."
1558 :tag "Org Agenda Column View"
1559 :group 'org-agenda)
1560
1561(defcustom org-agenda-columns-show-summaries t
ed21c5c8 1562 "Non-nil means show summaries for columns displayed in the agenda view."
20908596
CD
1563 :group 'org-agenda-column-view
1564 :type 'boolean)
1565
b349f79f 1566(defcustom org-agenda-columns-remove-prefix-from-item t
ed21c5c8 1567 "Non-nil means remove the prefix from a headline for agenda column view.
b349f79f
CD
1568The special ITEM field in the columns format contains the current line, with
1569all information shown in other columns (like the TODO state or a tag).
1570When this variable is non-nil, also the agenda prefix will be removed from
1571the content of the ITEM field, to make sure as much as possible of the
1572headline can be shown in the limited width of the field."
1573 :group 'org-agenda
1574 :type 'boolean)
1575
20908596 1576(defcustom org-agenda-columns-compute-summary-properties t
ed21c5c8 1577 "Non-nil means recompute all summary properties before column view.
20908596
CD
1578When column view in the agenda is listing properties that have a summary
1579operator, it can go to all relevant buffers and recompute the summaries
1580there. This can mean overhead for the agenda column view, but is necessary
1581to have thing up to date.
1582As a special case, a CLOCKSUM property also makes sure that the clock
1583computations are current."
1584 :group 'org-agenda-column-view
1585 :type 'boolean)
1586
1587(defcustom org-agenda-columns-add-appointments-to-effort-sum nil
ed21c5c8 1588 "Non-nil means the duration of an appointment will add to day effort.
20908596
CD
1589The property to which appointment durations will be added is the one given
1590in the option `org-effort-property'. If an appointment does not have
1591an end time, `org-agenda-default-appointment-duration' will be used. If that
1592is not set, an appointment without end time will not contribute to the time
1593estimate."
1594 :group 'org-agenda-column-view
1595 :type 'boolean)
1596
8bfe682a
CD
1597(defcustom org-agenda-auto-exclude-function nil
1598 "A function called with a tag to decide if it is filtered on '/ RET'.
1599The sole argument to the function, which is called once for each
1600possible tag, is a string giving the name of the tag. The
1601function should return either nil if the tag should be included
ed21c5c8
CD
1602as normal, or \"-<TAG>\" to exclude the tag.
1603Note that for the purpose of tag filtering, only the lower-case version of
1604all tags will be considered, so that this function will only ever see
1605the lower-case version of all tags."
8bfe682a
CD
1606 :group 'org-agenda
1607 :type 'function)
1608
20908596
CD
1609(eval-when-compile
1610 (require 'cl))
1611(require 'org)
1612
afe98dfa
CD
1613(defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1614 "Execute BODY with point at location given by `org-hd-marker' property.
1615If STRING is non-nil, the text property will be fetched from position 0
1616in that string. If STRING is nil, it will be fetched from the beginning
1617of the current line."
1618 `(let ((marker (get-text-property (if string 0 (point-at-bol))
1619 'org-hd-marker string)))
1620 (with-current-buffer (marker-buffer marker)
1621 (save-excursion
1622 (goto-char marker)
1623 ,@body))))
1624
20908596
CD
1625(defun org-add-agenda-custom-command (entry)
1626 "Replace or add a command in `org-agenda-custom-commands'.
1627This is mostly for hacking and trying a new command - once the command
1628works you probably want to add it to `org-agenda-custom-commands' for good."
1629 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1630 (if ass
1631 (setcdr ass (cdr entry))
1632 (push entry org-agenda-custom-commands))))
1633
1634;;; Define the Org-agenda-mode
1635
1636(defvar org-agenda-mode-map (make-sparse-keymap)
1637 "Keymap for `org-agenda-mode'.")
8bfe682a
CD
1638(if (fboundp 'defvaralias)
1639 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
20908596
CD
1640
1641(defvar org-agenda-menu) ; defined later in this file.
c8d0cf5c 1642(defvar org-agenda-restrict) ; defined later in this file.
20908596 1643(defvar org-agenda-follow-mode nil)
54a0dee5 1644(defvar org-agenda-entry-text-mode nil)
20908596
CD
1645(defvar org-agenda-clockreport-mode nil)
1646(defvar org-agenda-show-log nil)
1647(defvar org-agenda-redo-command nil)
1648(defvar org-agenda-query-string nil)
0bd48b37 1649(defvar org-agenda-mode-hook nil
86fbb8ca 1650 "Hook for `org-agenda-mode', run after the mode is turned on.")
20908596
CD
1651(defvar org-agenda-type nil)
1652(defvar org-agenda-force-single-file nil)
c8d0cf5c 1653(defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
20908596
CD
1654
1655(defun org-agenda-mode ()
1656 "Mode for time-sorted view on action items in Org-mode files.
1657
1658The following commands are available:
1659
1660\\{org-agenda-mode-map}"
1661 (interactive)
1662 (kill-all-local-variables)
1663 (setq org-agenda-undo-list nil
c8d0cf5c
CD
1664 org-agenda-pending-undo-list nil
1665 org-agenda-bulk-marked-entries nil)
20908596
CD
1666 (setq major-mode 'org-agenda-mode)
1667 ;; Keep global-font-lock-mode from turning on font-lock-mode
1668 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1669 (setq mode-name "Org-Agenda")
1670 (use-local-map org-agenda-mode-map)
1671 (easy-menu-add org-agenda-menu)
1672 (if org-startup-truncated (setq truncate-lines t))
54a0dee5 1673 (org-set-local 'line-move-visual nil)
20908596
CD
1674 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1675 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1676 ;; Make sure properties are removed when copying text
1677 (when (boundp 'buffer-substring-filters)
1678 (org-set-local 'buffer-substring-filters
1679 (cons (lambda (x)
1680 (set-text-properties 0 (length x) nil x) x)
1681 buffer-substring-filters)))
1682 (unless org-agenda-keep-modes
1683 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
54a0dee5 1684 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
20908596 1685 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
c8d0cf5c
CD
1686 org-agenda-show-log org-agenda-start-with-log-mode))
1687
20908596
CD
1688 (easy-menu-change
1689 '("Agenda") "Agenda Files"
1690 (append
1691 (list
1692 (vector
1693 (if (get 'org-agenda-files 'org-restrict)
1694 "Restricted to single file"
1695 "Edit File List")
1696 '(org-edit-agenda-file-list)
1697 (not (get 'org-agenda-files 'org-restrict)))
1698 "--")
1699 (mapcar 'org-file-menu-entry (org-agenda-files))))
1700 (org-agenda-set-mode-name)
1701 (apply
1702 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1703 (list 'org-agenda-mode-hook)))
1704
1705(substitute-key-definition 'undo 'org-agenda-undo
1706 org-agenda-mode-map global-map)
1707(org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1708(org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1709(org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1710(org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
c8d0cf5c
CD
1711(org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1712(org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1713(org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1714(org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1715(org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1716(org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
8bfe682a
CD
1717(org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1718(org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1719(org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
54a0dee5 1720(org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
8bfe682a 1721(org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
20908596 1722(org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
20908596 1723(org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
8bfe682a
CD
1724(org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1725(org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1726(org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
20908596
CD
1727(org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1728(org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1729(org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
20908596
CD
1730(org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1731(org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
54a0dee5 1732(org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
20908596 1733(org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
8bfe682a 1734(org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
20908596 1735(org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
71d35b24 1736(org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
20908596
CD
1737(org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1738(org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1739(org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1740(org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
20908596
CD
1741(org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1742(org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1743(org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
b349f79f
CD
1744(org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1745(org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
c8d0cf5c
CD
1746(org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1747(org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1748(org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1749(org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
20908596
CD
1750
1751(org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1752(org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1753(org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1754(let ((l '(1 2 3 4 5 6 7 8 9 0)))
1755 (while l (org-defkey org-agenda-mode-map
1756 (int-to-string (pop l)) 'digit-argument)))
1757
54a0dee5 1758(org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
20908596 1759(org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
54a0dee5 1760(org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
20908596 1761(org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
c8d0cf5c 1762(org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
20908596 1763(org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
ed21c5c8 1764(org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
20908596
CD
1765(org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1766(org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1767(org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
54a0dee5
CD
1768(org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1769(org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1770(org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1771 'org-clock-modify-effort-estimate)
1772(org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
20908596
CD
1773(org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1774(org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1775(org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
20908596 1776(org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
c8d0cf5c 1777(org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
20908596
CD
1778(org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1779(org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
8bfe682a
CD
1780(org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1781(org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1782(substitute-key-definition 'next-line 'org-agenda-next-line
1783 org-agenda-mode-map global-map)
1784(substitute-key-definition 'previous-line 'org-agenda-previous-line
1785 org-agenda-mode-map global-map)
621f83e4 1786(org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
20908596
CD
1787(org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1788(org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1789(org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1790(org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1791(org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1792(org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1793(org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1794(org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1795(org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1796(org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1797(org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1798(org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1799(org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1800(org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1801(org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1802(org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1803(org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1804(org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
afe98dfa 1805(org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
20908596
CD
1806(org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1807(org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1808(org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1809(org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1810(org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1811(org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
54a0dee5
CD
1812(org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1813(org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
20908596 1814(org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
c8d0cf5c 1815(org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
20908596
CD
1816
1817(org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1818(org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1819(org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1820(org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
621f83e4 1821(org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
71d35b24 1822(org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
c8d0cf5c 1823(org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
8d642074
CD
1824(define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1825(org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1826(org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
20908596 1827
86fbb8ca
CD
1828(org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1829(org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
20908596 1830(when org-agenda-mouse-1-follows-link
8bfe682a 1831 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
20908596
CD
1832(easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1833 '("Agenda"
1834 ("Agenda Files")
1835 "--"
8d642074
CD
1836 ("Agenda Dates"
1837 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1838 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1839 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1840 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1841 "--"
1842 ("View"
1843 ["Day View" org-agenda-day-view
1844 :active (org-agenda-check-type nil 'agenda)
acedf35c 1845 :style radio :selected (eq org-agenda-current-span 'day)
8d642074
CD
1846 :keys "v d (or just d)"]
1847 ["Week View" org-agenda-week-view
1848 :active (org-agenda-check-type nil 'agenda)
acedf35c 1849 :style radio :selected (eq org-agenda-current-span 'week)
8d642074
CD
1850 :keys "v w (or just w)"]
1851 ["Month View" org-agenda-month-view
1852 :active (org-agenda-check-type nil 'agenda)
acedf35c 1853 :style radio :selected (eq org-agenda-current-span 'month)
8d642074
CD
1854 :keys "v m"]
1855 ["Year View" org-agenda-year-view
1856 :active (org-agenda-check-type nil 'agenda)
acedf35c 1857 :style radio :selected (eq org-agenda-current-span 'year)
8d642074
CD
1858 :keys "v y"]
1859 "--"
1860 ["Include Diary" org-agenda-toggle-diary
1861 :style toggle :selected org-agenda-include-diary
1862 :active (org-agenda-check-type nil 'agenda)]
ed21c5c8
CD
1863 ["Include Deadlines" org-agenda-toggle-deadlines
1864 :style toggle :selected org-agenda-include-deadlines
1865 :active (org-agenda-check-type nil 'agenda)]
8d642074
CD
1866 ["Use Time Grid" org-agenda-toggle-time-grid
1867 :style toggle :selected org-agenda-use-time-grid
1868 :active (org-agenda-check-type nil 'agenda)]
1869 "--"
1870 ["Show clock report" org-agenda-clockreport-mode
1871 :style toggle :selected org-agenda-clockreport-mode
1872 :active (org-agenda-check-type nil 'agenda)]
1873 ["Show some entry text" org-agenda-entry-text-mode
1874 :style toggle :selected org-agenda-entry-text-mode
1875 :active t]
1876 "--"
1877 ["Show Logbook entries" org-agenda-log-mode
1878 :style toggle :selected org-agenda-show-log
1879 :active (org-agenda-check-type nil 'agenda 'timeline)
1880 :keys "v l (or just l)"]
1881 ["Include archived trees" org-agenda-archives-mode
1882 :style toggle :selected org-agenda-archives-mode :active t
1883 :keys "v a"]
1884 ["Include archive files" (org-agenda-archives-mode t)
1885 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1886 :keys "v A"]
1887 "--"
1888 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1889 ["Write view to file" org-write-agenda t]
1890 ["Rebuild buffer" org-agenda-redo t]
1891 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1892 "--"
1893 ["Show original entry" org-agenda-show t]
20908596
CD
1894 ["Go To (other window)" org-agenda-goto t]
1895 ["Go To (this window)" org-agenda-switch-to t]
1896 ["Follow Mode" org-agenda-follow-mode
1897 :style toggle :selected org-agenda-follow-mode :active t]
8d642074 1898; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
20908596 1899 "--"
8d642074
CD
1900 ("TODO"
1901 ["Cycle TODO" org-agenda-todo t]
1902 ["Next TODO set" org-agenda-todo-nextset t]
1903 ["Previous TODO set" org-agenda-todo-previousset t]
1904 ["Add note" org-agenda-add-note t])
1905 ("Archive/Refile/Delete"
8bfe682a
CD
1906 ["Archive default" org-agenda-archive-default t]
1907 ["Archive default" org-agenda-archive-default-with-confirmation t]
20908596
CD
1908 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1909 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
c8d0cf5c 1910 ["Archive subtree" org-agenda-archive t]
8d642074
CD
1911 "--"
1912 ["Refile" org-agenda-refile t]
1913 "--"
1914 ["Delete subtree" org-agenda-kill t])
c8d0cf5c 1915 ("Bulk action"
8d642074
CD
1916 ["Mark entry" org-agenda-bulk-mark t]
1917 ["Unmark entry" org-agenda-bulk-unmark t]
c8d0cf5c
CD
1918 ["Act on all marked" org-agenda-bulk-action t]
1919 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
1920 "--"
20908596
CD
1921 ("Tags and Properties"
1922 ["Show all Tags" org-agenda-show-tags t]
1923 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1924 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1925 "--"
1926 ["Column View" org-columns t])
8d642074 1927 ("Deadline/Schedule"
20908596
CD
1928 ["Schedule" org-agenda-schedule t]
1929 ["Set Deadline" org-agenda-deadline t]
1930 "--"
b349f79f
CD
1931 ["Mark item" org-agenda-action :active t :keys "k m"]
1932 ["Show mark item" org-agenda-action :active t :keys "k v"]
1933 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1934 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1935 "--"
20908596
CD
1936 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1937 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
c8d0cf5c
CD
1938 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
1939 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
1940 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
1941 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
20908596 1942 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
54a0dee5 1943 ("Clock and Effort"
20908596
CD
1944 ["Clock in" org-agenda-clock-in t]
1945 ["Clock out" org-agenda-clock-out t]
1946 ["Clock cancel" org-agenda-clock-cancel t]
54a0dee5
CD
1947 ["Goto running clock" org-clock-goto t]
1948 "--"
1949 ["Set Effort" org-agenda-set-effort t]
1950 ["Change clocked effort" org-clock-modify-effort-estimate
1951 (org-clock-is-active)])
20908596
CD
1952 ("Priority"
1953 ["Set Priority" org-agenda-priority t]
1954 ["Increase Priority" org-agenda-priority-up t]
1955 ["Decrease Priority" org-agenda-priority-down t]
1956 ["Show Priority" org-agenda-show-priority t])
1957 ("Calendar/Diary"
1958 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1959 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1960 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1961 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1962 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1963 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1964 "--"
8d642074 1965 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
20908596 1966 "--"
8d642074 1967 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2c3ad40d 1968 "--"
8d642074
CD
1969 ("MobileOrg"
1970 ["Push Files and Views" org-mobile-push t]
1971 ["Get Captured and Flagged" org-mobile-pull t]
1972 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
1973 ["Show note / unflag" org-agenda-show-the-flagging-note t]
c8d0cf5c 1974 "--"
8d642074 1975 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
20908596
CD
1976 "--"
1977 ["Quit" org-agenda-quit t]
1978 ["Exit and Release Buffers" org-agenda-exit t]
1979 ))
1980
1981;;; Agenda undo
1982
1983(defvar org-agenda-allow-remote-undo t
ed21c5c8 1984 "Non-nil means allow remote undo from the agenda buffer.")
20908596
CD
1985(defvar org-agenda-undo-list nil
1986 "List of undoable operations in the agenda since last refresh.")
1987(defvar org-agenda-undo-has-started-in nil
1988 "Buffers that have already seen `undo-start' in the current undo sequence.")
1989(defvar org-agenda-pending-undo-list nil
33306645 1990 "In a series of undo commands, this is the list of remaining undo items.")
20908596
CD
1991
1992
1993(defun org-agenda-undo ()
1994 "Undo a remote editing step in the agenda.
1995This undoes changes both in the agenda buffer and in the remote buffer
1996that have been changed along."
1997 (interactive)
1998 (or org-agenda-allow-remote-undo
f924a367 1999 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
20908596
CD
2000 (if (not (eq this-command last-command))
2001 (setq org-agenda-undo-has-started-in nil
2002 org-agenda-pending-undo-list org-agenda-undo-list))
2003 (if (not org-agenda-pending-undo-list)
2004 (error "No further undo information"))
2005 (let* ((entry (pop org-agenda-pending-undo-list))
2006 buf line cmd rembuf)
2007 (setq cmd (pop entry) line (pop entry))
2008 (setq rembuf (nth 2 entry))
2009 (org-with-remote-undo rembuf
2010 (while (bufferp (setq buf (pop entry)))
2011 (if (pop entry)
2012 (with-current-buffer buf
2013 (let ((last-undo-buffer buf)
2014 (inhibit-read-only t))
2015 (unless (memq buf org-agenda-undo-has-started-in)
2016 (push buf org-agenda-undo-has-started-in)
2017 (make-local-variable 'pending-undo-list)
2018 (undo-start))
2019 (while (and pending-undo-list
2020 (listp pending-undo-list)
2021 (not (car pending-undo-list)))
2022 (pop pending-undo-list))
2023 (undo-more 1))))))
54a0dee5 2024 (org-goto-line line)
20908596
CD
2025 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2026
2027(defun org-verify-change-for-undo (l1 l2)
2028 "Verify that a real change occurred between the undo lists L1 and L2."
2029 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2030 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2031 (not (eq l1 l2)))
2032
2033;;; Agenda dispatch
2034
2035(defvar org-agenda-restrict nil)
2036(defvar org-agenda-restrict-begin (make-marker))
2037(defvar org-agenda-restrict-end (make-marker))
2038(defvar org-agenda-last-dispatch-buffer nil)
2039(defvar org-agenda-overriding-restriction nil)
2040
2041;;;###autoload
c8d0cf5c 2042(defun org-agenda (&optional arg keys restriction)
20908596
CD
2043 "Dispatch agenda commands to collect entries to the agenda buffer.
2044Prompts for a command to execute. Any prefix arg will be passed
2045on to the selected command. The default selections are:
2046
2047a Call `org-agenda-list' to display the agenda for current day or week.
2048t Call `org-todo-list' to display the global todo list.
2049T Call `org-todo-list' to display the global todo list, select only
2050 entries with a specific TODO keyword (the user gets a prompt).
2051m Call `org-tags-view' to display headlines with tags matching
2052 a condition (the user is prompted for the condition).
2053M Like `m', but select only TODO entries, no ordinary headlines.
2054L Create a timeline for the current buffer.
2055e Export views to associated files.
c8d0cf5c 2056s Search entries for keywords.
8bfe682a 2057/ Multi occur across all agenda files and also files listed
c8d0cf5c
CD
2058 in `org-agenda-text-search-extra-files'.
2059< Restrict agenda commands to buffer, subtree, or region.
2060 Press several times to get the desired effect.
2061> Remove a previous restriction.
2062# List \"stuck\" projects.
2063! Configure what \"stuck\" means.
2064C Configure custom agenda commands.
20908596
CD
2065
2066More commands can be added by configuring the variable
2067`org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2068searches can be pre-defined in this way.
2069
2070If the current buffer is in Org-mode and visiting a file, you can also
2071first press `<' once to indicate that the agenda should be temporarily
2072\(until the next use of \\[org-agenda]) restricted to the current file.
2073Pressing `<' twice means to restrict to the current subtree or region
2074\(if active)."
2075 (interactive "P")
2076 (catch 'exit
2077 (let* ((prefix-descriptions nil)
54a0dee5
CD
2078 (org-agenda-window-setup (if (equal (buffer-name)
2079 org-agenda-buffer-name)
2080 'current-window
2081 org-agenda-window-setup))
20908596
CD
2082 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2083 (org-agenda-custom-commands
2084 ;; normalize different versions
2085 (delq nil
2086 (mapcar
2087 (lambda (x)
2088 (cond ((stringp (cdr x))
2089 (push x prefix-descriptions)
2090 nil)
2091 ((stringp (nth 1 x)) x)
2092 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2093 (t (cons (car x) (cons "" (cdr x))))))
2094 org-agenda-custom-commands)))
2095 (buf (current-buffer))
2096 (bfn (buffer-file-name (buffer-base-buffer)))
2097 entry key type match lprops ans)
8d642074 2098 ;; Turn off restriction unless there is an overriding one,
20908596 2099 (unless org-agenda-overriding-restriction
8bfe682a 2100 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
8d642074
CD
2101 ;; There is a request to keep the file list in place
2102 (put 'org-agenda-files 'org-restrict nil))
20908596
CD
2103 (setq org-agenda-restrict nil)
2104 (move-marker org-agenda-restrict-begin nil)
2105 (move-marker org-agenda-restrict-end nil))
2106 ;; Delete old local properties
2107 (put 'org-agenda-redo-command 'org-lprops nil)
2108 ;; Remember where this call originated
2109 (setq org-agenda-last-dispatch-buffer (current-buffer))
2110 (unless keys
2111 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2112 keys (car ans)
2113 restriction (cdr ans)))
8bfe682a 2114 ;; Establish the restriction, if any
20908596
CD
2115 (when (and (not org-agenda-overriding-restriction) restriction)
2116 (put 'org-agenda-files 'org-restrict (list bfn))
2117 (cond
2118 ((eq restriction 'region)
2119 (setq org-agenda-restrict t)
2120 (move-marker org-agenda-restrict-begin (region-beginning))
2121 (move-marker org-agenda-restrict-end (region-end)))
2122 ((eq restriction 'subtree)
2123 (save-excursion
2124 (setq org-agenda-restrict t)
2125 (org-back-to-heading t)
2126 (move-marker org-agenda-restrict-begin (point))
2127 (move-marker org-agenda-restrict-end
2128 (progn (org-end-of-subtree t)))))))
2129
20908596
CD
2130 ;; For example the todo list should not need it (but does...)
2131 (cond
2132 ((setq entry (assoc keys org-agenda-custom-commands))
2133 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2134 (progn
8bfe682a
CD
2135 (setq type (nth 2 entry) match (eval (nth 3 entry))
2136 lprops (nth 4 entry))
20908596
CD
2137 (put 'org-agenda-redo-command 'org-lprops lprops)
2138 (cond
2139 ((eq type 'agenda)
2140 (org-let lprops '(org-agenda-list current-prefix-arg)))
2141 ((eq type 'alltodo)
2142 (org-let lprops '(org-todo-list current-prefix-arg)))
2143 ((eq type 'search)
2144 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2145 ((eq type 'stuck)
2146 (org-let lprops '(org-agenda-list-stuck-projects
2147 current-prefix-arg)))
2148 ((eq type 'tags)
2149 (org-let lprops '(org-tags-view current-prefix-arg match)))
2150 ((eq type 'tags-todo)
2151 (org-let lprops '(org-tags-view '(4) match)))
2152 ((eq type 'todo)
2153 (org-let lprops '(org-todo-list match)))
2154 ((eq type 'tags-tree)
2155 (org-check-for-org-mode)
c8d0cf5c 2156 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
20908596
CD
2157 ((eq type 'todo-tree)
2158 (org-check-for-org-mode)
2159 (org-let lprops
2160 '(org-occur (concat "^" outline-regexp "[ \t]*"
2161 (regexp-quote match) "\\>"))))
2162 ((eq type 'occur-tree)
2163 (org-check-for-org-mode)
2164 (org-let lprops '(org-occur match)))
2165 ((functionp type)
2166 (org-let lprops '(funcall type match)))
2167 ((fboundp type)
2168 (org-let lprops '(funcall type match)))
2169 (t (error "Invalid custom agenda command type %s" type))))
2170 (org-run-agenda-series (nth 1 entry) (cddr entry))))
2171 ((equal keys "C")
2172 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2173 (customize-variable 'org-agenda-custom-commands))
2174 ((equal keys "a") (call-interactively 'org-agenda-list))
2175 ((equal keys "s") (call-interactively 'org-search-view))
2176 ((equal keys "t") (call-interactively 'org-todo-list))
2177 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2178 ((equal keys "m") (call-interactively 'org-tags-view))
2179 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2180 ((equal keys "e") (call-interactively 'org-store-agenda-views))
8d642074
CD
2181 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2182 (org-add-hook
2183 'post-command-hook
2184 (lambda ()
2185 (unless (current-message)
2186 (let* ((m (org-agenda-get-any-marker))
2187 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2188 (when note
2189 (message (concat
2190 "FLAGGING-NOTE ([?] for more info): "
2191 (org-add-props
2192 (replace-regexp-in-string
2193 "\\\\n" "//"
2194 (copy-sequence note))
2195 nil 'face 'org-warning)))))))
2196 t t))
20908596
CD
2197 ((equal keys "L")
2198 (unless (org-mode-p)
2199 (error "This is not an Org-mode file"))
2200 (unless restriction
2201 (put 'org-agenda-files 'org-restrict (list bfn))
2202 (org-call-with-arg 'org-timeline arg)))
2203 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2204 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2205 ((equal keys "!") (customize-variable 'org-stuck-projects))
2206 (t (error "Invalid agenda key"))))))
2207
2208(defun org-agenda-normalize-custom-commands (cmds)
2209 (delq nil
2210 (mapcar
2211 (lambda (x)
2212 (cond ((stringp (cdr x)) nil)
2213 ((stringp (nth 1 x)) x)
2214 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2215 (t (cons (car x) (cons "" (cdr x))))))
2216 cmds)))
2217
2218(defun org-agenda-get-restriction-and-command (prefix-descriptions)
2219 "The user interface for selecting an agenda command."
2220 (catch 'exit
2221 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2222 (restrict-ok (and bfn (org-mode-p)))
2223 (region-p (org-region-active-p))
2224 (custom org-agenda-custom-commands)
2225 (selstring "")
2226 restriction second-time
afe98dfa
CD
2227 c entry key type match prefixes rmheader header-end custom1 desc
2228 line lines left right n n1)
20908596
CD
2229 (save-window-excursion
2230 (delete-other-windows)
2231 (org-switch-to-buffer-other-window " *Agenda Commands*")
2232 (erase-buffer)
2233 (insert (eval-when-compile
2234 (let ((header
2235"
2c3ad40d 2236Press key for an agenda command: < Buffer, subtree/region restriction
20908596
CD
2237-------------------------------- > Remove restriction
2238a Agenda for current week or day e Export agenda views
2239t List of all TODO entries T Entries with special TODO kwd
621f83e4 2240m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
20908596
CD
2241L Timeline for current buffer # List stuck projects (!=configure)
2242s Search for keywords C Configure custom agenda commands
8d642074 2243/ Multi-occur ? Find :FLAGGED: entries
20908596
CD
2244")
2245 (start 0))
2246 (while (string-match
2247 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2248 header start)
2249 (setq start (match-end 0))
2250 (add-text-properties (match-beginning 2) (match-end 2)
2251 '(face bold) header))
2252 header)))
2253 (setq header-end (move-marker (make-marker) (point)))
2254 (while t
2255 (setq custom1 custom)
2256 (when (eq rmheader t)
54a0dee5 2257 (org-goto-line 1)
20908596
CD
2258 (re-search-forward ":" nil t)
2259 (delete-region (match-end 0) (point-at-eol))
2260 (forward-char 1)
2261 (looking-at "-+")
2262 (delete-region (match-end 0) (point-at-eol))
2263 (move-marker header-end (match-end 0)))
2264 (goto-char header-end)
2265 (delete-region (point) (point-max))
afe98dfa
CD
2266
2267 ;; Produce all the lines that describe custom commands and prefixes
2268 (setq lines nil)
20908596
CD
2269 (while (setq entry (pop custom1))
2270 (setq key (car entry) desc (nth 1 entry)
54a0dee5
CD
2271 type (nth 2 entry)
2272 match (nth 3 entry))
20908596
CD
2273 (if (> (length key) 1)
2274 (add-to-list 'prefixes (string-to-char key))
afe98dfa
CD
2275 (setq line
2276 (format
2277 "%-4s%-14s"
2278 (org-add-props (copy-sequence key)
2279 '(face bold))
2280 (cond
2281 ((string-match "\\S-" desc) desc)
2282 ((eq type 'agenda) "Agenda for current week or day")
2283 ((eq type 'alltodo) "List of all TODO entries")
2284 ((eq type 'search) "Word search")
2285 ((eq type 'stuck) "List of stuck projects")
2286 ((eq type 'todo) "TODO keyword")
2287 ((eq type 'tags) "Tags query")
2288 ((eq type 'tags-todo) "Tags (TODO)")
2289 ((eq type 'tags-tree) "Tags tree")
2290 ((eq type 'todo-tree) "TODO kwd tree")
2291 ((eq type 'occur-tree) "Occur tree")
2292 ((functionp type) (if (symbolp type)
2293 (symbol-name type)
2294 "Lambda expression"))
2295 (t "???"))))
2296 (if org-agenda-menu-show-matcher
2297 (setq line
2298 (concat line ": "
2299 (cond
2300 ((stringp match)
2301 (setq match (copy-sequence match))
2302 (org-add-props match nil 'face 'org-warning))
2303 (match
2304 (format "set of %d commands" (length match)))
2305 (t ""))))
2306 (if (org-string-nw-p match)
2307 (add-text-properties
2308 0 (length line) (list 'help-echo
2309 (concat "Matcher: "match)) line)))
2310 (push line lines)))
2311 (setq lines (nreverse lines))
20908596
CD
2312 (when prefixes
2313 (mapc (lambda (x)
afe98dfa
CD
2314 (push
2315 (format "%s %s"
20908596 2316 (org-add-props (char-to-string x)
afe98dfa
CD
2317 nil 'face 'bold)
2318 (or (cdr (assoc (concat selstring
2319 (char-to-string x))
20908596 2320 prefix-descriptions))
afe98dfa
CD
2321 "Prefix key"))
2322 lines))
20908596 2323 prefixes))
afe98dfa
CD
2324
2325 ;; Check if we should display in two columns
2326 (if org-agenda-menu-two-column
2327 (progn
2328 (setq n (length lines)
2329 n1 (+ (/ n 2) (mod n 2))
2330 right (nthcdr n1 lines)
2331 left (copy-sequence lines))
2332 (setcdr (nthcdr (1- n1) left) nil))
2333 (setq left lines right nil))
2334 (while left
2335 (insert "\n" (pop left))
2336 (when right
2337 (if (< (current-column) 40)
2338 (move-to-column 40 t)
2339 (insert " "))
2340 (insert (pop right))))
2341
2342 ;; Make the window the right size
20908596 2343 (goto-char (point-min))
93b62de8
CD
2344 (if second-time
2345 (if (not (pos-visible-in-window-p (point-max)))
2346 (org-fit-window-to-buffer))
2347 (setq second-time t)
2348 (org-fit-window-to-buffer))
afe98dfa
CD
2349
2350 ;; Ask for selection
20908596
CD
2351 (message "Press key for agenda command%s:"
2352 (if (or restrict-ok org-agenda-overriding-restriction)
2353 (if org-agenda-overriding-restriction
2354 " (restriction lock active)"
2355 (if restriction
2356 (format " (restricted to %s)" restriction)
2357 " (unrestricted)"))
2358 ""))
2359 (setq c (read-char-exclusive))
2360 (message "")
2361 (cond
2362 ((assoc (char-to-string c) custom)
2363 (setq selstring (concat selstring (char-to-string c)))
2364 (throw 'exit (cons selstring restriction)))
2365 ((memq c prefixes)
2366 (setq selstring (concat selstring (char-to-string c))
2367 prefixes nil
2368 rmheader (or rmheader t)
2369 custom (delq nil (mapcar
2370 (lambda (x)
2371 (if (or (= (length (car x)) 1)
2372 (/= (string-to-char (car x)) c))
2373 nil
2374 (cons (substring (car x) 1) (cdr x))))
2375 custom))))
2376 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2377 (message "Restriction is only possible in Org-mode buffers")
2378 (ding) (sit-for 1))
2379 ((eq c ?1)
2380 (org-agenda-remove-restriction-lock 'noupdate)
2381 (setq restriction 'buffer))
2382 ((eq c ?0)
2383 (org-agenda-remove-restriction-lock 'noupdate)
2384 (setq restriction (if region-p 'region 'subtree)))
2385 ((eq c ?<)
2386 (org-agenda-remove-restriction-lock 'noupdate)
2387 (setq restriction
2388 (cond
2389 ((eq restriction 'buffer)
2390 (if region-p 'region 'subtree))
2391 ((memq restriction '(subtree region))
2392 nil)
2393 (t 'buffer))))
2394 ((eq c ?>)
2395 (org-agenda-remove-restriction-lock 'noupdate)
2396 (setq restriction nil))
8d642074 2397 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
20908596
CD
2398 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2399 ((and (> (length selstring) 0) (eq c ?\d))
2400 (delete-window)
2401 (org-agenda-get-restriction-and-command prefix-descriptions))
2402
2403 ((equal c ?q) (error "Abort"))
2404 (t (error "Invalid key %c" c))))))))
2405
2406(defun org-run-agenda-series (name series)
c8d0cf5c 2407 (org-let (nth 1 series) '(org-prepare-agenda name))
20908596
CD
2408 (let* ((org-agenda-multi t)
2409 (redo (list 'org-run-agenda-series name (list 'quote series)))
2410 (cmds (car series))
2411 (gprops (nth 1 series))
2412 match ;; The byte compiler incorrectly complains about this. Keep it!
2413 cmd type lprops)
2414 (while (setq cmd (pop cmds))
8bfe682a 2415 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
20908596
CD
2416 (cond
2417 ((eq type 'agenda)
2418 (org-let2 gprops lprops
2419 '(call-interactively 'org-agenda-list)))
2420 ((eq type 'alltodo)
2421 (org-let2 gprops lprops
2422 '(call-interactively 'org-todo-list)))
2423 ((eq type 'search)
2424 (org-let2 gprops lprops
2425 '(org-search-view current-prefix-arg match nil)))
2426 ((eq type 'stuck)
2427 (org-let2 gprops lprops
2428 '(call-interactively 'org-agenda-list-stuck-projects)))
2429 ((eq type 'tags)
2430 (org-let2 gprops lprops
2431 '(org-tags-view current-prefix-arg match)))
2432 ((eq type 'tags-todo)
2433 (org-let2 gprops lprops
2434 '(org-tags-view '(4) match)))
2435 ((eq type 'todo)
2436 (org-let2 gprops lprops
2437 '(org-todo-list match)))
2438 ((fboundp type)
2439 (org-let2 gprops lprops
2440 '(funcall type match)))
2441 (t (error "Invalid type in command series"))))
2442 (widen)
2443 (setq org-agenda-redo-command redo)
2444 (goto-char (point-min)))
c8d0cf5c 2445 (org-fit-agenda-window)
0bd48b37 2446 (org-let (nth 1 series) '(org-finalize-agenda)))
20908596
CD
2447
2448;;;###autoload
2449(defmacro org-batch-agenda (cmd-key &rest parameters)
2450 "Run an agenda command in batch mode and send the result to STDOUT.
2451If CMD-KEY is a string of length 1, it is used as a key in
2452`org-agenda-custom-commands' and triggers this command. If it is a
2453longer string it is used as a tags/todo match string.
86fbb8ca 2454Parameters are alternating variable names and values that will be bound
20908596
CD
2455before running the agenda command."
2456 (let (pars)
2457 (while parameters
2458 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2459 (if (> (length cmd-key) 2)
2460 (eval (list 'let (nreverse pars)
2461 (list 'org-tags-view nil cmd-key)))
2462 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2463 (set-buffer org-agenda-buffer-name)
2464 (princ (org-encode-for-stdout (buffer-string)))))
2465
8bfe682a
CD
2466;(defun org-encode-for-stdout (string)
2467; (if (fboundp 'encode-coding-string)
2468; (encode-coding-string string buffer-file-coding-system)
2469; string))
2470
20908596 2471(defun org-encode-for-stdout (string)
8bfe682a 2472 string)
20908596
CD
2473
2474(defvar org-agenda-info nil)
2475
2476;;;###autoload
2477(defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2478 "Run an agenda command in batch mode and send the result to STDOUT.
2479If CMD-KEY is a string of length 1, it is used as a key in
2480`org-agenda-custom-commands' and triggers this command. If it is a
2481longer string it is used as a tags/todo match string.
86fbb8ca 2482Parameters are alternating variable names and values that will be bound
20908596
CD
2483before running the agenda command.
2484
2485The output gives a line for each selected agenda item. Each
2486item is a list of comma-separated values, like this:
2487
2488category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2489
2490category The category of the item
2491head The headline, without TODO kwd, TAGS and PRIORITY
2492type The type of the agenda entry, can be
2493 todo selected in TODO match
2494 tagsmatch selected in tags match
2495 diary imported from diary
2496 deadline a deadline on given date
2497 scheduled scheduled on given date
2498 timestamp entry has timestamp on given date
2499 closed entry was closed on given date
2500 upcoming-deadline warning about deadline
2501 past-scheduled forwarded scheduled item
2502 block entry has date block including g. date
2503todo The todo keyword, if any
2504tags All tags including inherited ones, separated by colons
2505date The relevant date, like 2007-2-14
2506time The time, like 15:00-16:50
2507extra Sting with extra planning info
2508priority-l The priority letter if any was given
2509priority-n The computed numerical priority
2510agenda-day The day in the agenda where this is listed"
2511
2512 (let (pars)
2513 (while parameters
2514 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2515 (push (list 'org-agenda-remove-tags t) pars)
2516 (if (> (length cmd-key) 2)
2517 (eval (list 'let (nreverse pars)
2518 (list 'org-tags-view nil cmd-key)))
2519 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2520 (set-buffer org-agenda-buffer-name)
2521 (let* ((lines (org-split-string (buffer-string) "\n"))
2522 line)
2523 (while (setq line (pop lines))
2524 (catch 'next
2525 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2526 (setq org-agenda-info
2527 (org-fix-agenda-info (text-properties-at 0 line)))
2528 (princ
2529 (org-encode-for-stdout
2530 (mapconcat 'org-agenda-export-csv-mapper
ed21c5c8 2531 '(org-category txt type todo tags date time extra
20908596
CD
2532 priority-letter priority agenda-day)
2533 ",")))
2534 (princ "\n"))))))
2535
2536(defun org-fix-agenda-info (props)
86fbb8ca
CD
2537 "Make sure all properties on an agenda item have a canonical form.
2538This ensures the export commands can easily use it."
20908596
CD
2539 (let (tmp re)
2540 (when (setq tmp (plist-get props 'tags))
2541 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2542 (when (setq tmp (plist-get props 'date))
2543 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2544 (let ((calendar-date-display-form '(year "-" month "-" day)))
2545 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2546
2547 (setq tmp (calendar-date-string tmp)))
2548 (setq props (plist-put props 'date tmp)))
2549 (when (setq tmp (plist-get props 'day))
2550 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2551 (let ((calendar-date-display-form '(year "-" month "-" day)))
2552 (setq tmp (calendar-date-string tmp)))
2553 (setq props (plist-put props 'day tmp))
2554 (setq props (plist-put props 'agenda-day tmp)))
2555 (when (setq tmp (plist-get props 'txt))
2556 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2557 (plist-put props 'priority-letter (match-string 1 tmp))
2558 (setq tmp (replace-match "" t t tmp)))
2559 (when (and (setq re (plist-get props 'org-todo-regexp))
2560 (setq re (concat "\\`\\.*" re " ?"))
2561 (string-match re tmp))
2562 (plist-put props 'todo (match-string 1 tmp))
2563 (setq tmp (replace-match "" t t tmp)))
2564 (plist-put props 'txt tmp)))
2565 props)
2566
2567(defun org-agenda-export-csv-mapper (prop)
2568 (let ((res (plist-get org-agenda-info prop)))
2569 (setq res
2570 (cond
2571 ((not res) "")
2572 ((stringp res) res)
2573 (t (prin1-to-string res))))
2574 (while (string-match "," res)
2575 (setq res (replace-match ";" t t res)))
2576 (org-trim res)))
2577
2578
2579;;;###autoload
2580(defun org-store-agenda-views (&rest parameters)
2581 (interactive)
2582 (eval (list 'org-batch-store-agenda-views)))
2583
2584;; FIXME, why is this a macro?????
2585;;;###autoload
2586(defmacro org-batch-store-agenda-views (&rest parameters)
2587 "Run all custom agenda commands that have a file argument."
2588 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2589 (pop-up-frames nil)
2590 (dir default-directory)
2c3ad40d 2591 pars cmd thiscmdkey files opts cmd-or-set)
20908596
CD
2592 (while parameters
2593 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2594 (setq pars (reverse pars))
2595 (save-window-excursion
2596 (while cmds
2597 (setq cmd (pop cmds)
2598 thiscmdkey (car cmd)
2c3ad40d
CD
2599 cmd-or-set (nth 2 cmd)
2600 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2601 files (nth (if (listp cmd-or-set) 4 5) cmd))
20908596
CD
2602 (if (stringp files) (setq files (list files)))
2603 (when files
2604 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2605 (list 'org-agenda nil thiscmdkey)))
2606 (set-buffer org-agenda-buffer-name)
2607 (while files
2608 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2609 (list 'org-write-agenda
c8d0cf5c 2610 (expand-file-name (pop files) dir) nil t))))
20908596
CD
2611 (and (get-buffer org-agenda-buffer-name)
2612 (kill-buffer org-agenda-buffer-name)))))))
2613
8d642074
CD
2614(defun org-agenda-mark-header-line (pos)
2615 "Mark the line at POS as an agenda structure header."
2616 (save-excursion
2617 (goto-char pos)
2618 (put-text-property (point-at-bol) (point-at-eol)
2619 'org-agenda-structural-header t)
2620 (when org-agenda-title-append
2621 (put-text-property (point-at-bol) (point-at-eol)
2622 'org-agenda-title-append org-agenda-title-append))))
2623
8bfe682a 2624(defvar org-mobile-creating-agendas)
c8d0cf5c 2625(defun org-write-agenda (file &optional open nosettings)
20908596
CD
2626 "Write the current buffer (an agenda view) as a file.
2627Depending on the extension of the file name, plain text (.txt),
2628HTML (.html or .htm) or Postscript (.ps) is produced.
2629If the extension is .ics, run icalendar export over all files used
2630to construct the agenda and limit the export to entries listed in the
2631agenda now.
8bfe682a 2632With prefix argument OPEN, open the new file immediately.
20908596
CD
2633If NOSETTINGS is given, do not scope the settings of
2634`org-agenda-exporter-settings' into the export commands. This is used when
2635the settings have already been scoped and we do not wish to overrule other,
2636higher priority settings."
c8d0cf5c 2637 (interactive "FWrite agenda to file: \nP")
20908596
CD
2638 (if (not (file-writable-p file))
2639 (error "Cannot write agenda to file %s" file))
20908596 2640 (org-let (if nosettings nil org-agenda-exporter-settings)
afe98dfa 2641 '(save-excursion
20908596 2642 (save-window-excursion
93b62de8 2643 (org-agenda-mark-filtered-text)
8bfe682a 2644 (let ((bs (copy-sequence (buffer-string))) beg)
93b62de8
CD
2645 (org-agenda-unmark-filtered-text)
2646 (with-temp-buffer
afe98dfa
CD
2647 (rename-buffer "Agenda View" t)
2648 (set-buffer-modified-p nil)
20908596 2649 (insert bs)
93b62de8
CD
2650 (org-agenda-remove-marked-text 'org-filtered)
2651 (while (setq beg (text-property-any (point-min) (point-max)
2652 'org-filtered t))
2653 (delete-region
2654 beg (or (next-single-property-change beg 'org-filtered)
2655 (point-max))))
c8d0cf5c 2656 (run-hooks 'org-agenda-before-write-hook)
93b62de8 2657 (cond
8bfe682a
CD
2658 ((org-bound-and-true-p org-mobile-creating-agendas)
2659 (org-mobile-write-agenda-for-mobile file))
93b62de8 2660 ((string-match "\\.html?\\'" file)
afe98dfa 2661 (require 'htmlize)
93b62de8 2662 (set-buffer (htmlize-buffer (current-buffer)))
ff4be292 2663
93b62de8
CD
2664 (when (and org-agenda-export-html-style
2665 (string-match "<style>" org-agenda-export-html-style))
2666 ;; replace <style> section with org-agenda-export-html-style
2667 (goto-char (point-min))
2668 (kill-region (- (search-forward "<style") 6)
2669 (search-forward "</style>"))
2670 (insert org-agenda-export-html-style))
2671 (write-file file)
2672 (kill-buffer (current-buffer))
2673 (message "HTML written to %s" file))
2674 ((string-match "\\.ps\\'" file)
c8d0cf5c 2675 (require 'ps-print)
afe98dfa 2676 (ps-print-buffer-with-faces file)
93b62de8 2677 (message "Postscript written to %s" file))
c8d0cf5c
CD
2678 ((string-match "\\.pdf\\'" file)
2679 (require 'ps-print)
afe98dfa
CD
2680 (ps-print-buffer-with-faces
2681 (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c
CD
2682 (call-process "ps2pdf" nil nil nil
2683 (expand-file-name
2684 (concat (file-name-sans-extension file) ".ps"))
2685 (expand-file-name file))
afe98dfa 2686 (delete-file (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c 2687 (message "PDF written to %s" file))
93b62de8 2688 ((string-match "\\.ics\\'" file)
c8d0cf5c 2689 (require 'org-icalendar)
93b62de8
CD
2690 (let ((org-agenda-marker-table
2691 (org-create-marker-find-array
2692 (org-agenda-collect-markers)))
2693 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2694 (org-combined-agenda-icalendar-file file))
2695 (apply 'org-export-icalendar 'combine
2696 (org-agenda-files nil 'ifmode))))
2697 (t
2698 (let ((bs (buffer-string)))
2699 (find-file file)
2700 (erase-buffer)
2701 (insert bs)
2702 (save-buffer 0)
2703 (kill-buffer (current-buffer))
2704 (message "Plain text written to %s" file))))))))
c8d0cf5c
CD
2705 (set-buffer org-agenda-buffer-name))
2706 (when open (org-open-file file)))
2707
93b62de8
CD
2708(defvar org-agenda-filter-overlays nil)
2709
2710(defun org-agenda-mark-filtered-text ()
2711 "Mark all text hidden by filtering with a text property."
2712 (let ((inhibit-read-only t))
2713 (mapc
2714 (lambda (o)
86fbb8ca 2715 (when (equal (overlay-buffer o) (current-buffer))
93b62de8 2716 (put-text-property
86fbb8ca 2717 (overlay-start o) (overlay-end o)
93b62de8
CD
2718 'org-filtered t)))
2719 org-agenda-filter-overlays)))
2720
2721(defun org-agenda-unmark-filtered-text ()
2722 "Remove the filtering text property."
2723 (let ((inhibit-read-only t))
2724 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2725
2726(defun org-agenda-remove-marked-text (property &optional value)
2727 "Delete all text marked with VALUE of PROPERTY.
2728VALUE defaults to t."
2729 (let (beg)
2730 (setq value (or value t))
2731 (while (setq beg (text-property-any (point-min) (point-max)
2732 property value))
2733 (delete-region
2734 beg (or (next-single-property-change beg 'org-filtered)
2735 (point-max))))))
20908596 2736
c8d0cf5c
CD
2737(defun org-agenda-add-entry-text ()
2738 "Add entry text to agenda lines.
2739This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2740entry text following headings shown in the agenda.
2741Drawers will be excluded, also the line with scheduling/deadline info."
8bfe682a
CD
2742 (when (and (> org-agenda-add-entry-text-maxlines 0)
2743 (not (org-bound-and-true-p org-mobile-creating-agendas)))
54a0dee5 2744 (let (m txt)
c8d0cf5c
CD
2745 (goto-char (point-min))
2746 (while (not (eobp))
8d642074 2747 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
c8d0cf5c 2748 (beginning-of-line 2)
54a0dee5 2749 (setq txt (org-agenda-get-some-entry-text
8d642074 2750 m org-agenda-add-entry-text-maxlines " > "))
c8d0cf5c 2751 (end-of-line 1)
afe98dfa
CD
2752 (if (string-match "\\S-" txt)
2753 (insert "\n" txt)
2754 (or (eobp) (forward-char 1))))))))
c8d0cf5c 2755
8d642074
CD
2756(defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2757 &rest keep)
54a0dee5 2758 "Extract entry text from MARKER, at most N-LINES lines.
8d642074
CD
2759This will ignore drawers etc, just get the text.
2760If INDENT is given, prefix every line with this string. If KEEP is
8bfe682a 2761given, it is a list of symbols, defining stuff that should not be
8d642074 2762removed from the entry content. Currently only `planning' is allowed here."
54a0dee5
CD
2763 (let (txt drawer-re kwd-time-re ind)
2764 (save-excursion
2765 (with-current-buffer (marker-buffer marker)
2766 (if (not (org-mode-p))
2767 (setq txt "")
2768 (save-excursion
2769 (save-restriction
2770 (widen)
2771 (goto-char marker)
8d642074 2772 (end-of-line 1)
54a0dee5 2773 (setq txt (buffer-substring
8d642074 2774 (min (1+ (point)) (point-max))
54a0dee5
CD
2775 (progn (outline-next-heading) (point)))
2776 drawer-re org-drawer-regexp
2777 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2778 ".*\n?"))
2779 (with-temp-buffer
2780 (insert txt)
2781 (when org-agenda-add-entry-text-descriptive-links
2782 (goto-char (point-min))
2783 (while (org-activate-bracket-links (point-max))
2784 (add-text-properties (match-beginning 0) (match-end 0)
2785 '(face org-link))))
2786 (goto-char (point-min))
2787 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2788 (set-text-properties (match-beginning 0) (match-end 0)
2789 nil))
2790 (goto-char (point-min))
2791 (while (re-search-forward drawer-re nil t)
2792 (delete-region
2793 (match-beginning 0)
2794 (progn (re-search-forward
2795 "^[ \t]*:END:.*\n?" nil 'move)
2796 (point))))
8d642074
CD
2797 (unless (member 'planning keep)
2798 (goto-char (point-min))
2799 (while (re-search-forward kwd-time-re nil t)
2800 (replace-match "")))
54a0dee5 2801 (goto-char (point-min))
8d642074
CD
2802 (when org-agenda-entry-text-exclude-regexps
2803 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2804 (while (setq re (pop re-list))
2805 (goto-char (point-min))
2806 (while (re-search-forward re nil t)
2807 (replace-match "")))))
2808 (goto-char (point-max))
2809 (skip-chars-backward " \t\n")
2810 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2811
2812 ;; find and remove min common indentation
54a0dee5
CD
2813 (goto-char (point-min))
2814 (untabify (point-min) (point-max))
2815 (setq ind (org-get-indentation))
2816 (while (not (eobp))
2817 (unless (looking-at "[ \t]*$")
2818 (setq ind (min ind (org-get-indentation))))
2819 (beginning-of-line 2))
2820 (goto-char (point-min))
2821 (while (not (eobp))
2822 (unless (looking-at "[ \t]*$")
2823 (move-to-column ind)
2824 (delete-region (point-at-bol) (point)))
2825 (beginning-of-line 2))
8d642074
CD
2826
2827 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2828
54a0dee5 2829 (goto-char (point-min))
8d642074
CD
2830 (when indent
2831 (while (and (not (eobp)) (re-search-forward "^" nil t))
2832 (replace-match indent t t)))
54a0dee5
CD
2833 (goto-char (point-min))
2834 (while (looking-at "[ \t]*\n") (replace-match ""))
2835 (goto-char (point-max))
2836 (when (> (org-current-line)
2837 n-lines)
2838 (org-goto-line (1+ n-lines))
2839 (backward-char 1))
2840 (setq txt (buffer-substring (point-min) (point)))))))))
2841 txt))
2842
20908596
CD
2843(defun org-agenda-collect-markers ()
2844 "Collect the markers pointing to entries in the agenda buffer."
2845 (let (m markers)
2846 (save-excursion
2847 (goto-char (point-min))
2848 (while (not (eobp))
8d642074
CD
2849 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2850 (org-get-at-bol 'org-marker)))
20908596
CD
2851 (push m markers))
2852 (beginning-of-line 2)))
2853 (nreverse markers)))
2854
2855(defun org-create-marker-find-array (marker-list)
2856 "Create a alist of files names with all marker positions in that file."
2857 (let (f tbl m a p)
2858 (while (setq m (pop marker-list))
2859 (setq p (marker-position m)
2860 f (buffer-file-name (or (buffer-base-buffer
2861 (marker-buffer m))
2862 (marker-buffer m))))
2863 (if (setq a (assoc f tbl))
2864 (push (marker-position m) (cdr a))
2865 (push (list f p) tbl)))
2866 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2867 tbl)))
2868
33306645 2869(defvar org-agenda-marker-table nil) ; dynamically scoped parameter
20908596
CD
2870(defun org-check-agenda-marker-table ()
2871 "Check of the current entry is on the marker list."
2872 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2873 a)
2874 (and (setq a (assoc file org-agenda-marker-table))
2875 (save-match-data
2876 (save-excursion
2877 (org-back-to-heading t)
2878 (member (point) (cdr a)))))))
2879
2880(defun org-check-for-org-mode ()
2881 "Make sure current buffer is in org-mode. Error if not."
2882 (or (org-mode-p)
f924a367 2883 (error "Cannot execute org-mode agenda command on buffer in %s"
20908596
CD
2884 major-mode)))
2885
2886(defun org-fit-agenda-window ()
2887 "Fit the window to the buffer size."
2888 (and (memq org-agenda-window-setup '(reorganize-frame))
2889 (fboundp 'fit-window-to-buffer)
93b62de8 2890 (org-fit-window-to-buffer
20908596
CD
2891 nil
2892 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2893 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2894
2895;;; Agenda prepare and finalize
2896
33306645 2897(defvar org-agenda-multi nil) ; dynamically scoped
20908596
CD
2898(defvar org-agenda-buffer-name "*Org Agenda*")
2899(defvar org-pre-agenda-window-conf nil)
2900(defvar org-agenda-columns-active nil)
2901(defvar org-agenda-name nil)
71d35b24 2902(defvar org-agenda-filter nil)
acedf35c 2903(defvar org-agenda-filter-while-redo nil)
c8d0cf5c
CD
2904(defvar org-agenda-filter-preset nil
2905 "A preset of the tags filter used for secondary agenda filtering.
86fbb8ca 2906This must be a list of strings, each string must be a single tag preceded
c8d0cf5c
CD
2907by \"+\" or \"-\".
2908This variable should not be set directly, but agenda custom commands can
afe98dfa
CD
2909bind it in the options section. The preset filter is a global property of
2910the entire agenda view. In a block agenda, it will not work reliably to
2911define a filter for one of the individual blocks. You need to set it in
2912the global options and expect it to be applied to the entire view.")
c8d0cf5c 2913
20908596
CD
2914(defun org-prepare-agenda (&optional name)
2915 (setq org-todo-keywords-for-agenda nil)
2916 (setq org-done-keywords-for-agenda nil)
8d642074 2917 (setq org-drawers-for-agenda nil)
86fbb8ca
CD
2918 (unless org-agenda-persistent-filter
2919 (setq org-agenda-filter nil))
c8d0cf5c 2920 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
20908596
CD
2921 (if org-agenda-multi
2922 (progn
2923 (setq buffer-read-only nil)
2924 (goto-char (point-max))
2925 (unless (or (bobp) org-agenda-compact-blocks)
0bd48b37
CD
2926 (insert "\n"
2927 (if (stringp org-agenda-block-separator)
2928 org-agenda-block-separator
2929 (make-string (window-width) org-agenda-block-separator))
2930 "\n"))
20908596
CD
2931 (narrow-to-region (point) (point-max)))
2932 (org-agenda-reset-markers)
2933 (setq org-agenda-contributing-files nil)
2934 (setq org-agenda-columns-active nil)
2c3ad40d 2935 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
20908596
CD
2936 (setq org-todo-keywords-for-agenda
2937 (org-uniquify org-todo-keywords-for-agenda))
2938 (setq org-done-keywords-for-agenda
2939 (org-uniquify org-done-keywords-for-agenda))
8d642074 2940 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
20908596
CD
2941 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2942 (awin (get-buffer-window abuf)))
2943 (cond
2944 ((equal (current-buffer) abuf) nil)
2945 (awin (select-window awin))
2946 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2947 ((equal org-agenda-window-setup 'current-window)
2948 (switch-to-buffer abuf))
2949 ((equal org-agenda-window-setup 'other-window)
2950 (org-switch-to-buffer-other-window abuf))
2951 ((equal org-agenda-window-setup 'other-frame)
8d642074 2952 (switch-to-buffer-other-frame abuf))
20908596
CD
2953 ((equal org-agenda-window-setup 'reorganize-frame)
2954 (delete-other-windows)
afe98dfa
CD
2955 (org-switch-to-buffer-other-window abuf)))
2956 ;; additional test in case agenda is invoked from within agenda
2957 ;; buffer via elisp link
2958 (unless (equal (current-buffer) abuf)
2959 (switch-to-buffer abuf)))
20908596
CD
2960 (setq buffer-read-only nil)
2961 (let ((inhibit-read-only t)) (erase-buffer))
2962 (org-agenda-mode)
2963 (and name (not org-agenda-name)
2964 (org-set-local 'org-agenda-name name)))
2965 (setq buffer-read-only nil))
2966
2967(defun org-finalize-agenda ()
2968 "Finishing touch for the agenda buffer, called just before displaying it."
2969 (unless org-agenda-multi
2970 (save-excursion
2971 (let ((inhibit-read-only t))
2972 (goto-char (point-min))
2973 (while (org-activate-bracket-links (point-max))
2974 (add-text-properties (match-beginning 0) (match-end 0)
2975 '(face org-link)))
2976 (org-agenda-align-tags)
2977 (unless org-agenda-with-colors
2978 (remove-text-properties (point-min) (point-max) '(face nil))))
33306645
CD
2979 (if (and (boundp 'org-agenda-overriding-columns-format)
2980 org-agenda-overriding-columns-format)
2981 (org-set-local 'org-agenda-overriding-columns-format
2982 org-agenda-overriding-columns-format))
20908596
CD
2983 (if (and (boundp 'org-agenda-view-columns-initially)
2984 org-agenda-view-columns-initially)
2985 (org-agenda-columns))
2986 (when org-agenda-fontify-priorities
c8d0cf5c 2987 (org-agenda-fontify-priorities))
d6685abc
CD
2988 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2989 (org-agenda-dim-blocked-tasks))
54a0dee5
CD
2990 (org-agenda-mark-clocking-task)
2991 (when org-agenda-entry-text-mode
2992 (org-agenda-entry-text-hide)
f924a367 2993 (org-agenda-entry-text-show))
8bfe682a
CD
2994 (if (functionp 'org-habit-insert-consistency-graphs)
2995 (org-habit-insert-consistency-graphs))
20908596 2996 (run-hooks 'org-finalize-agenda-hook)
8d642074 2997 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
86fbb8ca 2998 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
c8d0cf5c 2999 (org-agenda-filter-apply org-agenda-filter))
20908596
CD
3000 )))
3001
54a0dee5
CD
3002(defun org-agenda-mark-clocking-task ()
3003 "Mark the current clock entry in the agenda if it is present."
3004 (mapc (lambda (o)
86fbb8ca
CD
3005 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3006 (delete-overlay o)))
3007 (overlays-in (point-min) (point-max)))
54a0dee5
CD
3008 (when (marker-buffer org-clock-hd-marker)
3009 (save-excursion
3010 (goto-char (point-min))
3011 (let (s ov)
3012 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3013 (goto-char s)
8d642074 3014 (when (equal (org-get-at-bol 'org-hd-marker)
54a0dee5 3015 org-clock-hd-marker)
86fbb8ca
CD
3016 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3017 (overlay-put ov 'type 'org-agenda-clocking)
3018 (overlay-put ov 'face 'org-agenda-clocking)
3019 (overlay-put ov 'help-echo
54a0dee5
CD
3020 "The clock is running in this item")))))))
3021
c8d0cf5c 3022(defun org-agenda-fontify-priorities ()
20908596
CD
3023 "Make highest priority lines bold, and lowest italic."
3024 (interactive)
86fbb8ca
CD
3025 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3026 (delete-overlay o)))
3027 (overlays-in (point-min) (point-max)))
20908596
CD
3028 (save-excursion
3029 (let ((inhibit-read-only t)
3030 b e p ov h l)
3031 (goto-char (point-min))
3032 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3033 (setq h (or (get-char-property (point) 'org-highest-priority)
3034 org-highest-priority)
3035 l (or (get-char-property (point) 'org-lowest-priority)
3036 org-lowest-priority)
3037 p (string-to-char (match-string 1))
c8d0cf5c
CD
3038 b (match-beginning 0)
3039 e (if (eq org-agenda-fontify-priorities 'cookies)
3040 (match-end 0)
3041 (point-at-eol))
86fbb8ca
CD
3042 ov (make-overlay b e))
3043 (overlay-put
20908596 3044 ov 'face
ed21c5c8
CD
3045 (cond ((org-face-from-face-or-color
3046 'priority nil
3047 (cdr (assoc p org-priority-faces))))
c8d0cf5c 3048 ((and (listp org-agenda-fontify-priorities)
ed21c5c8
CD
3049 (org-face-from-face-or-color
3050 'priority nil
3051 (cdr (assoc p org-agenda-fontify-priorities)))))
20908596
CD
3052 ((equal p l) 'italic)
3053 ((equal p h) 'bold)))
86fbb8ca 3054 (overlay-put ov 'org-type 'org-priority)))))
20908596 3055
d6685abc
CD
3056(defun org-agenda-dim-blocked-tasks ()
3057 "Dim currently blocked TODO's in the agenda display."
86fbb8ca
CD
3058 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3059 (delete-overlay o)))
3060 (overlays-in (point-min) (point-max)))
d6685abc
CD
3061 (save-excursion
3062 (let ((inhibit-read-only t)
72d06d81 3063 (org-depend-tag-blocked nil)
d6685abc 3064 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
c8d0cf5c
CD
3065 org-blocked-by-checkboxes
3066 invis1 b e p ov h l)
d6685abc
CD
3067 (goto-char (point-min))
3068 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3069 (and pos (goto-char (1+ pos))))
c8d0cf5c 3070 (setq org-blocked-by-checkboxes nil invis1 invis)
8d642074 3071 (let ((marker (org-get-at-bol 'org-hd-marker)))
d6685abc
CD
3072 (when (and marker
3073 (not (with-current-buffer (marker-buffer marker)
3074 (save-excursion
3075 (goto-char marker)
c8d0cf5c
CD
3076 (if (org-entry-get nil "NOBLOCKING")
3077 t ;; Never block this entry
3078 (run-hook-with-args-until-failure
3079 'org-blocker-hook
3080 (list :type 'todo-state-change
3081 :position marker
3082 :from 'todo
3083 :to 'done)))))))
3084 (if org-blocked-by-checkboxes (setq invis1 nil))
53e31a31
CD
3085 (setq b (if invis1
3086 (max (point-min) (1- (point-at-bol)))
3087 (point-at-bol))
d6685abc 3088 e (point-at-eol)
86fbb8ca 3089 ov (make-overlay b e))
c8d0cf5c 3090 (if invis1
86fbb8ca
CD
3091 (overlay-put ov 'invisible t)
3092 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3093 (overlay-put ov 'org-type 'org-blocked-todo)))))))
20908596
CD
3094
3095(defvar org-agenda-skip-function nil
3096 "Function to be called at each match during agenda construction.
3097If this function returns nil, the current match should not be skipped.
3098Otherwise, the function must return a position from where the search
3099should be continued.
3100This may also be a Lisp form, it will be evaluated.
3101Never set this variable using `setq' or so, because then it will apply
3102to all future agenda commands. Instead, bind it with `let' to scope
3103it dynamically into the agenda-constructing command. A good way to set
86fbb8ca 3104it is through options in `org-agenda-custom-commands'.")
20908596
CD
3105
3106(defun org-agenda-skip ()
3107 "Throw to `:skip' in places that should be skipped.
3108Also moves point to the end of the skipped region, so that search can
3109continue from there."
3110 (let ((p (point-at-bol)) to fp)
2c3ad40d 3111 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
20908596
CD
3112 (get-text-property p :org-archived)
3113 (org-end-of-subtree t)
3114 (throw :skip t))
b349f79f
CD
3115 (and org-agenda-skip-comment-trees
3116 (get-text-property p :org-comment)
20908596
CD
3117 (org-end-of-subtree t)
3118 (throw :skip t))
3119 (if (equal (char-after p) ?#) (throw :skip t))
3120 (when (and (or (setq fp (functionp org-agenda-skip-function))
3121 (consp org-agenda-skip-function))
3122 (setq to (save-excursion
3123 (save-match-data
3124 (if fp
3125 (funcall org-agenda-skip-function)
3126 (eval org-agenda-skip-function))))))
3127 (goto-char to)
3128 (throw :skip t))))
3129
3130(defvar org-agenda-markers nil
3131 "List of all currently active markers created by `org-agenda'.")
54a0dee5 3132(defvar org-agenda-last-marker-time (org-float-time)
20908596
CD
3133 "Creation time of the last agenda marker.")
3134
3135(defun org-agenda-new-marker (&optional pos)
3136 "Return a new agenda marker.
3137Org-mode keeps a list of these markers and resets them when they are
3138no longer in use."
3139 (let ((m (copy-marker (or pos (point)))))
54a0dee5 3140 (setq org-agenda-last-marker-time (org-float-time))
20908596
CD
3141 (push m org-agenda-markers)
3142 m))
3143
3144(defun org-agenda-reset-markers ()
3145 "Reset markers created by `org-agenda'."
3146 (while org-agenda-markers
3147 (move-marker (pop org-agenda-markers) nil)))
3148
b349f79f
CD
3149(defun org-agenda-save-markers-for-cut-and-paste (beg end)
3150 "Save relative positions of markers in region."
3151 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3152 org-agenda-markers))
3153
54a0dee5
CD
3154;;; Entry text mode
3155
3156(defun org-agenda-entry-text-show-here ()
8bfe682a 3157 "Add some text from the entry as context to the current line."
54a0dee5 3158 (let (m txt o)
8d642074 3159 (setq m (org-get-at-bol 'org-hd-marker))
54a0dee5
CD
3160 (unless (marker-buffer m)
3161 (error "No marker points to an entry here"))
3162 (setq txt (concat "\n" (org-no-properties
3163 (org-agenda-get-some-entry-text
8d642074 3164 m org-agenda-entry-text-maxlines " > "))))
54a0dee5 3165 (when (string-match "\\S-" txt)
86fbb8ca
CD
3166 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3167 (overlay-put o 'evaporate t)
3168 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3169 (overlay-put o 'after-string txt))))
54a0dee5
CD
3170
3171(defun org-agenda-entry-text-show ()
3172 "Add entry context for all agenda lines."
3173 (interactive)
3174 (save-excursion
3175 (goto-char (point-max))
3176 (beginning-of-line 1)
3177 (while (not (bobp))
8d642074 3178 (when (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
3179 (org-agenda-entry-text-show-here))
3180 (beginning-of-line 0))))
3181
3182(defun org-agenda-entry-text-hide ()
3183 "Remove any shown entry context."
3184 (delq nil
3185 (mapcar (lambda (o)
86fbb8ca 3186 (if (eq (overlay-get o 'org-overlay-type)
54a0dee5 3187 'agenda-entry-content)
86fbb8ca
CD
3188 (progn (delete-overlay o) t)))
3189 (overlays-in (point-min) (point-max)))))
54a0dee5 3190
acedf35c
CD
3191(defun org-agenda-get-day-face (date)
3192 "Return the face DATE should be displayed with."
3193 (or (and (functionp org-agenda-day-face-function)
3194 (funcall org-agenda-day-face-function date))
3195 (cond ((org-agenda-todayp date)
3196 'org-agenda-date-today)
3197 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3198 'org-agenda-date-weekend)
3199 (t 'org-agenda-date))))
3200
20908596
CD
3201;;; Agenda timeline
3202
3203(defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3204
3205(defun org-timeline (&optional include-all)
3206 "Show a time-sorted view of the entries in the current org file.
3207Only entries with a time stamp of today or later will be listed. With
3208\\[universal-argument] prefix, all unfinished TODO items will also be shown,
3209under the current date.
3210If the buffer contains an active region, only check the region for
3211dates."
3212 (interactive "P")
20908596
CD
3213 (org-compile-prefix-format 'timeline)
3214 (org-set-sorting-strategy 'timeline)
3215 (let* ((dopast t)
3216 (dotodo include-all)
3217 (doclosed org-agenda-show-log)
afe98dfa
CD
3218 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3219 (current-buffer))))
20908596
CD
3220 (date (calendar-current-date))
3221 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3222 (end (if (org-region-active-p) (region-end) (point-max)))
3223 (day-numbers (org-get-all-dates beg end 'no-ranges
3224 t doclosed ; always include today
3225 org-timeline-show-empty-dates))
3226 (org-deadline-warning-days 0)
3227 (org-agenda-only-exact-dates t)
acedf35c 3228 (today (org-today))
20908596
CD
3229 (past t)
3230 args
acedf35c 3231 s e rtn d emptyp)
20908596
CD
3232 (setq org-agenda-redo-command
3233 (list 'progn
3234 (list 'org-switch-to-buffer-other-window (current-buffer))
3235 (list 'org-timeline (list 'quote include-all))))
3236 (if (not dopast)
3237 ;; Remove past dates from the list of dates.
3238 (setq day-numbers (delq nil (mapcar (lambda(x)
3239 (if (>= x today) x nil))
3240 day-numbers))))
afe98dfa 3241 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
20908596
CD
3242 (if doclosed (push :closed args))
3243 (push :timestamp args)
3244 (push :deadline args)
3245 (push :scheduled args)
3246 (push :sexp args)
3247 (if dotodo (push :todo args))
8d642074
CD
3248 (insert "Timeline of file " entry "\n")
3249 (add-text-properties (point-min) (point)
3250 (list 'face 'org-agenda-structure))
3251 (org-agenda-mark-header-line (point-min))
20908596
CD
3252 (while (setq d (pop day-numbers))
3253 (if (and (listp d) (eq (car d) :omitted))
3254 (progn
3255 (setq s (point))
3256 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3257 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3258 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3259 (if (and (>= d today)
3260 dopast
3261 past)
3262 (progn
3263 (setq past nil)
3264 (insert (make-string 79 ?-) "\n")))
acedf35c 3265 (setq date (calendar-gregorian-from-absolute d))
20908596
CD
3266 (setq s (point))
3267 (setq rtn (and (not emptyp)
3268 (apply 'org-agenda-get-day-entries entry
3269 date args)))
3270 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3271 (progn
3272 (insert
3273 (if (stringp org-agenda-format-date)
3274 (format-time-string org-agenda-format-date
3275 (org-time-from-absolute date))
3276 (funcall org-agenda-format-date date))
3277 "\n")
3278 (put-text-property s (1- (point)) 'face
acedf35c 3279 (org-agenda-get-day-face date))
20908596 3280 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3281 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596
CD
3282 (if (equal d today)
3283 (put-text-property s (1- (point)) 'org-today t))
3284 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3285 (put-text-property s (1- (point)) 'day d)))))
3286 (goto-char (point-min))
3287 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3288 (point-min)))
3289 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3290 (org-finalize-agenda)
3291 (setq buffer-read-only t)))
3292
3293(defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3294 "Return a list of all relevant day numbers from BEG to END buffer positions.
3295If NO-RANGES is non-nil, include only the start and end dates of a range,
3296not every single day in the range. If FORCE-TODAY is non-nil, make
3297sure that TODAY is included in the list. If INACTIVE is non-nil, also
3298inactive time stamps (those in square brackets) are included.
3299When EMPTY is non-nil, also include days without any entries."
3300 (let ((re (concat
3301 (if pre-re pre-re "")
3302 (if inactive org-ts-regexp-both org-ts-regexp)))
3303 dates dates1 date day day1 day2 ts1 ts2)
3304 (if force-today
acedf35c 3305 (setq dates (list (org-today))))
20908596
CD
3306 (save-excursion
3307 (goto-char beg)
3308 (while (re-search-forward re end t)
3309 (setq day (time-to-days (org-time-string-to-time
3310 (substring (match-string 1) 0 10))))
3311 (or (memq day dates) (push day dates)))
3312 (unless no-ranges
3313 (goto-char beg)
3314 (while (re-search-forward org-tr-regexp end t)
3315 (setq ts1 (substring (match-string 1) 0 10)
3316 ts2 (substring (match-string 2) 0 10)
3317 day1 (time-to-days (org-time-string-to-time ts1))
3318 day2 (time-to-days (org-time-string-to-time ts2)))
3319 (while (< (setq day1 (1+ day1)) day2)
3320 (or (memq day1 dates) (push day1 dates)))))
3321 (setq dates (sort dates '<))
3322 (when empty
3323 (while (setq day (pop dates))
3324 (setq day2 (car dates))
3325 (push day dates1)
3326 (when (and day2 empty)
3327 (if (or (eq empty t)
3328 (and (numberp empty) (<= (- day2 day) empty)))
3329 (while (< (setq day (1+ day)) day2)
3330 (push (list day) dates1))
3331 (push (cons :omitted (- day2 day)) dates1))))
3332 (setq dates (nreverse dates1)))
3333 dates)))
3334
3335;;; Agenda Daily/Weekly
3336
3337(defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
c8d0cf5c
CD
3338(defvar org-agenda-start-day nil ; dynamically scoped parameter
3339"Custom commands can set this variable in the options section.")
20908596 3340(defvar org-agenda-last-arguments nil
86fbb8ca 3341 "The arguments of the previous call to `org-agenda'.")
20908596 3342(defvar org-starting-day nil) ; local variable in the agenda buffer
acedf35c
CD
3343(defvar org-agenda-current-span nil
3344 "The current span used in the agenda view.") ; local variable in the agenda buffer
20908596 3345(defvar org-include-all-loc nil) ; local variable
20908596 3346
ed21c5c8
CD
3347(defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3348 "List of types searched for when creating the daily/weekly agenda.
3349This variable is a list of symbols that controls the types of
3350items that appear in the daily/weekly agenda. Allowed symbols in this
3351list are are
3352
3353 :timestamp List items containing a date stamp or date range matching
3354 the selected date. This includes sexp entries in
3355 angular brackets.
3356
3357 :sexp List entries resulting from plain diary-like sexps.
3358
3359 :deadline List deadline due on that date. When the date is today,
3360 also list any deadlines past due, or due within
3361 `org-deadline-warning-days'. `:deadline' must appear before
3362 `:scheduled' if the setting of
3363 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3364 any effect.
3365
3366 :scheduled List all items which are scheduled for the given date.
3367 The diary for *today* also contains items which were
3368 scheduled earlier and are not yet marked DONE.
3369
3370By default, all four types are turned on.
3371
3372Never set this variable globally using `setq', because then it
3373will apply to all future agenda commands. Instead, bind it with
3374`let' to scope it dynamically into the the agenda-constructing
3375command. A good way to set it is through options in
3376`org-agenda-custom-commands'. For a more flexible (though
3377somewhat less efficient) way of determining what is included in
3378the daily/weekly agenda, see `org-agenda-skip-function'.")
3379
20908596 3380;;;###autoload
acedf35c 3381(defun org-agenda-list (&optional include-all start-day span)
20908596
CD
3382 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3383The view will be for the current day or week, but from the overview buffer
3384you will be able to go to other days/weeks.
3385
3386With one \\[universal-argument] prefix argument INCLUDE-ALL,
3387all unfinished TODO items will also be shown, before the agenda.
3388This feature is considered obsolete, please use the TODO list or a block
3389agenda instead.
3390
3391With a numeric prefix argument in an interactive call, the agenda will
acedf35c
CD
3392span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3393the number of days. SPAN defaults to `org-agenda-span'.
20908596
CD
3394
3395START-DAY defaults to TODAY, or to the most recent match for the weekday
3396given in `org-agenda-start-on-weekday'."
3397 (interactive "P")
3398 (if (and (integerp include-all) (> include-all 0))
acedf35c
CD
3399 (setq span include-all include-all nil))
3400 (setq start-day (or start-day org-agenda-start-day))
20908596
CD
3401 (if org-agenda-overriding-arguments
3402 (setq include-all (car org-agenda-overriding-arguments)
3403 start-day (nth 1 org-agenda-overriding-arguments)
acedf35c 3404 span (nth 2 org-agenda-overriding-arguments)))
20908596
CD
3405 (if (stringp start-day)
3406 ;; Convert to an absolute day number
3407 (setq start-day (time-to-days (org-read-date nil t start-day))))
acedf35c 3408 (setq org-agenda-last-arguments (list include-all start-day span))
20908596
CD
3409 (org-compile-prefix-format 'agenda)
3410 (org-set-sorting-strategy 'agenda)
acedf35c
CD
3411 (let* ((span (org-agenda-ndays-to-span (or span org-agenda-ndays org-agenda-span)))
3412 (today (org-today))
3413 (sd (or start-day today))
3414 (ndays (org-agenda-span-to-ndays span sd))
3415 (org-agenda-start-on-weekday
3416 (if (eq ndays 7)
3417 org-agenda-start-on-weekday))
2c3ad40d 3418 (thefiles (org-agenda-files nil 'ifmode))
20908596 3419 (files thefiles)
20908596 3420 (start (if (or (null org-agenda-start-on-weekday)
acedf35c 3421 (< ndays 7))
20908596
CD
3422 sd
3423 (let* ((nt (calendar-day-of-week
3424 (calendar-gregorian-from-absolute sd)))
3425 (n1 org-agenda-start-on-weekday)
3426 (d (- nt n1)))
3427 (- sd (+ (if (< d 0) 7 0) d)))))
3428 (day-numbers (list start))
3429 (day-cnt 0)
3430 (inhibit-redisplay (not debug-on-error))
acedf35c
CD
3431 s e rtn rtnall file date d start-pos end-pos todayp
3432 clocktable-start clocktable-end filter)
20908596 3433 (setq org-agenda-redo-command
acedf35c
CD
3434 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3435 (dotimes (n (1- ndays))
3436 (push (1+ (car day-numbers)) day-numbers))
20908596
CD
3437 (setq day-numbers (nreverse day-numbers))
3438 (setq clocktable-start (car day-numbers)
3439 clocktable-end (1+ (or (org-last day-numbers) 0)))
3440 (org-prepare-agenda "Day/Week")
3441 (org-set-local 'org-starting-day (car day-numbers))
3442 (org-set-local 'org-include-all-loc include-all)
acedf35c 3443 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
20908596
CD
3444 (when (and (or include-all org-agenda-include-all-todo)
3445 (member today day-numbers))
3446 (setq files thefiles
3447 rtnall nil)
3448 (while (setq file (pop files))
3449 (catch 'nextfile
3450 (org-check-agenda-file file)
3451 (setq date (calendar-gregorian-from-absolute today)
3452 rtn (org-agenda-get-day-entries
3453 file date :todo))
3454 (setq rtnall (append rtnall rtn))))
3455 (when rtnall
8d642074 3456 (insert "All currently open TODO items:\n")
20908596 3457 (add-text-properties (point-min) (1- (point))
8d642074
CD
3458 (list 'face 'org-agenda-structure
3459 'short-heading "All TODO items"))
8bfe682a 3460 (org-agenda-mark-header-line (point-min))
20908596
CD
3461 (insert (org-finalize-agenda-entries rtnall) "\n")))
3462 (unless org-agenda-compact-blocks
3463 (let* ((d1 (car day-numbers))
3464 (d2 (org-last day-numbers))
3465 (w1 (org-days-to-iso-week d1))
3466 (w2 (org-days-to-iso-week d2)))
3467 (setq s (point))
c8d0cf5c
CD
3468 (if org-agenda-overriding-header
3469 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3470 nil 'face 'org-agenda-structure) "\n")
acedf35c 3471 (insert (org-agenda-span-name span)
c8d0cf5c
CD
3472 "-agenda"
3473 (if (< (- d2 d1) 350)
3474 (if (= w1 w2)
3475 (format " (W%02d)" w1)
3476 (format " (W%02d-W%02d)" w1 w2))
3477 "")
3478 ":\n")))
20908596 3479 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
8d642074
CD
3480 'org-date-line t))
3481 (org-agenda-mark-header-line s))
20908596
CD
3482 (while (setq d (pop day-numbers))
3483 (setq date (calendar-gregorian-from-absolute d)
20908596
CD
3484 s (point))
3485 (if (or (setq todayp (= d today))
3486 (and (not start-pos) (= d sd)))
3487 (setq start-pos (point))
3488 (if (and start-pos (not end-pos))
3489 (setq end-pos (point))))
3490 (setq files thefiles
3491 rtnall nil)
3492 (while (setq file (pop files))
3493 (catch 'nextfile
3494 (org-check-agenda-file file)
ed21c5c8
CD
3495 (let ((org-agenda-entry-types org-agenda-entry-types))
3496 (unless org-agenda-include-deadlines
3497 (setq org-agenda-entry-types
3498 (delq :deadline org-agenda-entry-types)))
3499 (cond
3500 ((eq org-agenda-show-log 'only)
3501 (setq rtn (org-agenda-get-day-entries
3502 file date :closed)))
3503 (org-agenda-show-log
3504 (setq rtn (apply 'org-agenda-get-day-entries
3505 file date
3506 (append '(:closed) org-agenda-entry-types))))
3507 (t
3508 (setq rtn (apply 'org-agenda-get-day-entries
3509 file date
3510 org-agenda-entry-types)))))
20908596
CD
3511 (setq rtnall (append rtnall rtn))))
3512 (if org-agenda-include-diary
c8d0cf5c 3513 (let ((org-agenda-search-headline-for-time t))
20908596
CD
3514 (require 'diary-lib)
3515 (setq rtn (org-get-entries-from-diary date))
3516 (setq rtnall (append rtnall rtn))))
3517 (if (or rtnall org-agenda-show-all-dates)
3518 (progn
3519 (setq day-cnt (1+ day-cnt))
3520 (insert
3521 (if (stringp org-agenda-format-date)
3522 (format-time-string org-agenda-format-date
3523 (org-time-from-absolute date))
3524 (funcall org-agenda-format-date date))
3525 "\n")
3526 (put-text-property s (1- (point)) 'face
acedf35c 3527 (org-agenda-get-day-face date))
20908596 3528 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3529 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596 3530 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
c8d0cf5c 3531 (when todayp
acedf35c 3532 (put-text-property s (1- (point)) 'org-today t))
20908596
CD
3533 (if rtnall (insert
3534 (org-finalize-agenda-entries
3535 (org-agenda-add-time-grid-maybe
acedf35c 3536 rtnall ndays todayp))
20908596
CD
3537 "\n"))
3538 (put-text-property s (1- (point)) 'day d)
3539 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3540 (when (and org-agenda-clockreport-mode clocktable-start)
2c3ad40d 3541 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
20908596
CD
3542 ;; the above line is to ensure the restricted range!
3543 (p org-agenda-clockreport-parameter-plist)
3544 tbl)
3545 (setq p (org-plist-delete p :block))
3546 (setq p (plist-put p :tstart clocktable-start))
3547 (setq p (plist-put p :tend clocktable-end))
3548 (setq p (plist-put p :scope 'agenda))
acedf35c
CD
3549 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3550 (setq filter (or org-agenda-filter-while-redo
3551 (get 'org-agenda-filter :preset-filter))))
3552 (setq p (plist-put p :tags (mapconcat (lambda (x)
3553 (if (string-match "[<>=]" x)
3554 ""
3555 x))
3556 filter ""))))
3557 (message "%s" (plist-get p :tags)) (sit-for 2)
20908596
CD
3558 (setq tbl (apply 'org-get-clocktable p))
3559 (insert tbl)))
3560 (goto-char (point-min))
c8d0cf5c 3561 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3562 (unless (and (pos-visible-in-window-p (point-min))
3563 (pos-visible-in-window-p (point-max)))
3564 (goto-char (1- (point-max)))
3565 (recenter -1)
3566 (if (not (pos-visible-in-window-p (or start-pos 1)))
3567 (progn
3568 (goto-char (or start-pos 1))
3569 (recenter 1))))
3570 (goto-char (or start-pos 1))
3571 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3572 (org-finalize-agenda)
3573 (setq buffer-read-only t)
3574 (message "")))
3575
3576(defun org-agenda-ndays-to-span (n)
acedf35c
CD
3577 "Return a span symbol for a span of N days, or N if none matches."
3578 (cond ((symbolp n) n)
3579 ((= n 1) 'day)
3580 ((= n 7) 'week)
3581 (t n)))
3582
3583(defun org-agenda-span-to-ndays (span start-day)
3584 "Return ndays from SPAN starting at START-DAY."
3585 (cond ((numberp span) span)
3586 ((eq span 'day) 1)
3587 ((eq span 'week) 7)
3588 ((eq span 'month)
3589 (let ((date (calendar-gregorian-from-absolute start-day)))
3590 (calendar-last-day-of-month (car date) (caddr date))))
3591 ((eq span 'year)
3592 (let ((date (calendar-gregorian-from-absolute start-day)))
3593 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3594
3595(defun org-agenda-span-name (span)
3596 "Return a SPAN name."
3597 (if (null span)
3598 ""
3599 (if (symbolp span)
3600 (capitalize (symbol-name span))
3601 (format "%d days" span))))
20908596
CD
3602
3603;;; Agenda word search
3604
3605(defvar org-agenda-search-history nil)
3606(defvar org-todo-only nil)
3607
3608(defvar org-search-syntax-table nil
3609 "Special syntax table for org-mode search.
3610In this table, we have single quotes not as word constituents, to
33306645 3611that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
20908596
CD
3612
3613(defun org-search-syntax-table ()
3614 (unless org-search-syntax-table
3615 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3616 (modify-syntax-entry ?' "." org-search-syntax-table)
3617 (modify-syntax-entry ?` "." org-search-syntax-table))
3618 org-search-syntax-table)
3619
ed21c5c8
CD
3620(defvar org-agenda-last-search-view-search-was-boolean nil)
3621
20908596
CD
3622;;;###autoload
3623(defun org-search-view (&optional todo-only string edit-at)
ed21c5c8 3624 "Show all entries that contain a phrase or words or regular expressions.
20908596
CD
3625
3626With optional prefix argument TODO-ONLY, only consider entries that are
3627TODO entries. The argument STRING can be used to pass a default search
3628string into this function. If EDIT-AT is non-nil, it means that the
3629user should get a chance to edit this string, with cursor at position
3630EDIT-AT.
3631
ed21c5c8
CD
3632The search string can be viewed either as a phrase that should be found as
3633is, or it can be broken into a number of snippets, each of which must match
3634in a Boolean way to select an entry. The default depends on the variable
3635`org-agenda-search-view-always-boolean'.
3636Even if this is turned off (the default) you can always switch to
86fbb8ca 3637Boolean search dynamically by preceding the first word with \"+\" or \"-\".
ed21c5c8
CD
3638
3639The default is a direct search of the whole phrase, where each space in
3640the search string can expand to an arbitrary amount of whitespace,
3641including newlines.
3642
3643If using a Boolean search, the search string is split on whitespace and
3644each snippet is searched separately, with logical AND to select an entry.
3645Words prefixed with a minus must *not* occur in the entry. Words without
3646a prefix or prefixed with a plus must occur in the entry. Matching is
3647case-insensitive. Words are enclosed by word delimiters (i.e. they must
3648match whole words, not parts of a word) if
3649`org-agenda-search-view-force-full-words' is set (default is nil).
3650
3651Boolean search snippets enclosed by curly braces are interpreted as
86fbb8ca 3652regular expressions that must or (when preceded with \"-\") must not
ed21c5c8 3653match in the entry. Snippets enclosed into double quotes will be taken
86fbb8ca 3654as a whole, to include whitespace.
ed21c5c8
CD
3655
3656- If the search string starts with an asterisk, search only in headlines.
3657- If (possibly after the leading star) the search string starts with an
3658 exclamation mark, this also means to look at TODO entries only, an effect
3659 that can also be achieved with a prefix argument.
3660- If (possibly after star and exclamation mark) the search string starts
3661 with a colon, this will mean that the (non-regexp) snippets of the
3662 Boolean search must match as full words.
20908596
CD
3663
3664This command searches the agenda files, and in addition the files listed
3665in `org-agenda-text-search-extra-files'."
3666 (interactive "P")
3667 (org-compile-prefix-format 'search)
3668 (org-set-sorting-strategy 'search)
3669 (org-prepare-agenda "SEARCH")
3670 (let* ((props (list 'face nil
c8d0cf5c 3671 'done-face 'org-agenda-done
20908596
CD
3672 'org-not-done-regexp org-not-done-regexp
3673 'org-todo-regexp org-todo-regexp
b349f79f 3674 'org-complex-heading-regexp org-complex-heading-regexp
20908596 3675 'mouse-face 'highlight
20908596 3676 'help-echo (format "mouse-2 or RET jump to location")))
ed21c5c8 3677 (full-words org-agenda-search-view-force-full-words)
86fbb8ca 3678 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
20908596 3679 regexp rtn rtnall files file pos
ed21c5c8 3680 marker category tags c neg re boolean
20908596
CD
3681 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3682 (unless (and (not edit-at)
3683 (stringp string)
3684 (string-match "\\S-" string))
ed21c5c8
CD
3685 (setq string (read-string
3686 (if org-agenda-search-view-always-boolean
3687 "[+-]Word/{Regexp} ...: "
3688 "Phrase, or [+-]Word/{Regexp} ...: ")
3689 (cond
3690 ((integerp edit-at) (cons string edit-at))
3691 (edit-at string))
3692 'org-agenda-search-history)))
20908596
CD
3693 (org-set-local 'org-todo-only todo-only)
3694 (setq org-agenda-redo-command
3695 (list 'org-search-view (if todo-only t nil) string
3696 '(if current-prefix-arg 1 nil)))
3697 (setq org-agenda-query-string string)
3698
3699 (if (equal (string-to-char string) ?*)
3700 (setq hdl-only t
3701 words (substring string 1))
3702 (setq words string))
3703 (when (equal (string-to-char words) ?!)
3704 (setq todo-only t
3705 words (substring words 1)))
ed21c5c8
CD
3706 (when (equal (string-to-char words) ?:)
3707 (setq full-words t
3708 words (substring words 1)))
3709 (if (or org-agenda-search-view-always-boolean
3710 (member (string-to-char words) '(?- ?+ ?\{)))
3711 (setq boolean t))
20908596 3712 (setq words (org-split-string words))
afe98dfa
CD
3713 (let (www w)
3714 (while (setq w (pop words))
3715 (while (and (string-match "\\\\\\'" w) words)
3716 (setq w (concat (substring w 0 -1) " " (pop words))))
3717 (push w www))
3718 (setq words (nreverse www) www nil)
3719 (while (setq w (pop words))
3720 (when (and (string-match "\\`[-+]?{" w)
3721 (not (string-match "}\\'" w)))
3722 (while (and words (not (string-match "}\\'" (car words))))
3723 (setq w (concat w " " (pop words))))
3724 (setq w (concat w " " (pop words))))
3725 (push w www))
3726 (setq words (nreverse www)))
ed21c5c8
CD
3727 (setq org-agenda-last-search-view-search-was-boolean boolean)
3728 (when boolean
3729 (let (wds w)
3730 (while (setq w (pop words))
3731 (if (or (equal (substring w 0 1) "\"")
3732 (and (> (length w) 1)
3733 (member (substring w 0 1) '("+" "-"))
3734 (equal (substring w 1 2) "\"")))
3735 (while (and words (not (equal (substring w -1) "\"")))
3736 (setq w (concat w " " (pop words)))))
3737 (and (string-match "\\`\\([-+]?\\)\"" w)
3738 (setq w (replace-match "\\1" nil nil w)))
3739 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3740 (push w wds))
3741 (setq words (nreverse wds))))
3742 (if boolean
8bfe682a
CD
3743 (mapc (lambda (w)
3744 (setq c (string-to-char w))
3745 (if (equal c ?-)
3746 (setq neg t w (substring w 1))
3747 (if (equal c ?+)
3748 (setq neg nil w (substring w 1))
ed21c5c8 3749 (setq neg nil)))
8bfe682a
CD
3750 (if (string-match "\\`{.*}\\'" w)
3751 (setq re (substring w 1 -1))
ed21c5c8
CD
3752 (if full-words
3753 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3754 (setq re (regexp-quote (downcase w)))))
8bfe682a
CD
3755 (if neg (push re regexps-) (push re regexps+)))
3756 words)
3757 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3758 regexps+))
20908596
CD
3759 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3760 (if (not regexps+)
3761 (setq regexp (concat "^" org-outline-regexp))
3762 (setq regexp (pop regexps+))
3763 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3764 regexp))))
2c3ad40d 3765 (setq files (org-agenda-files nil 'ifmode))
20908596
CD
3766 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3767 (pop org-agenda-text-search-extra-files)
3768 (setq files (org-add-archive-files files)))
3769 (setq files (append files org-agenda-text-search-extra-files)
3770 rtnall nil)
3771 (while (setq file (pop files))
3772 (setq ee nil)
3773 (catch 'nextfile
3774 (org-check-agenda-file file)
3775 (setq buffer (if (file-exists-p file)
3776 (org-get-agenda-file-buffer file)
3777 (error "No such file %s" file)))
3778 (if (not buffer)
3779 ;; If file does not exist, make sure an error message is sent
3780 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3781 file))))
3782 (with-current-buffer buffer
3783 (with-syntax-table (org-search-syntax-table)
3784 (unless (org-mode-p)
3785 (error "Agenda file %s is not in `org-mode'" file))
3786 (let ((case-fold-search t))
3787 (save-excursion
3788 (save-restriction
3789 (if org-agenda-restrict
3790 (narrow-to-region org-agenda-restrict-begin
3791 org-agenda-restrict-end)
3792 (widen))
3793 (goto-char (point-min))
3794 (unless (or (org-on-heading-p)
3795 (outline-next-heading))
3796 (throw 'nextfile t))
3797 (goto-char (max (point-min) (1- (point))))
3798 (while (re-search-forward regexp nil t)
3799 (org-back-to-heading t)
3800 (skip-chars-forward "* ")
3801 (setq beg (point-at-bol)
3802 beg1 (point)
3803 end (progn (outline-next-heading) (point)))
3804 (catch :skip
3805 (goto-char beg)
3806 (org-agenda-skip)
3807 (setq str (buffer-substring-no-properties
3808 (point-at-bol)
3809 (if hdl-only (point-at-eol) end)))
3810 (mapc (lambda (wr) (when (string-match wr str)
3811 (goto-char (1- end))
3812 (throw :skip t)))
3813 regexps-)
3814 (mapc (lambda (wr) (unless (string-match wr str)
3815 (goto-char (1- end))
3816 (throw :skip t)))
3817 (if todo-only
3818 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3819 regexps+)
3820 regexps+))
3821 (goto-char beg)
3822 (setq marker (org-agenda-new-marker (point))
3823 category (org-get-category)
3824 tags (org-get-tags-at (point))
3825 txt (org-format-agenda-item
3826 ""
3827 (buffer-substring-no-properties
3828 beg1 (point-at-eol))
3829 category tags))
3830 (org-add-props txt props
3831 'org-marker marker 'org-hd-marker marker
3832 'org-todo-regexp org-todo-regexp
b349f79f 3833 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
3834 'priority 1000 'org-category category
3835 'type "search")
3836 (push txt ee)
3837 (goto-char (1- end))))))))))
3838 (setq rtn (nreverse ee))
3839 (setq rtnall (append rtnall rtn)))
3840 (if org-agenda-overriding-header
3841 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3842 nil 'face 'org-agenda-structure) "\n")
3843 (insert "Search words: ")
3844 (add-text-properties (point-min) (1- (point))
3845 (list 'face 'org-agenda-structure))
3846 (setq pos (point))
3847 (insert string "\n")
3848 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3849 (setq pos (point))
3850 (unless org-agenda-multi
3851 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3852 (add-text-properties pos (1- (point))
3853 (list 'face 'org-agenda-structure))))
8d642074 3854 (org-agenda-mark-header-line (point-min))
20908596
CD
3855 (when rtnall
3856 (insert (org-finalize-agenda-entries rtnall) "\n"))
3857 (goto-char (point-min))
c8d0cf5c 3858 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3859 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3860 (org-finalize-agenda)
3861 (setq buffer-read-only t)))
3862
3863;;; Agenda TODO list
3864
3865(defvar org-select-this-todo-keyword nil)
3866(defvar org-last-arg nil)
3867
3868;;;###autoload
3869(defun org-todo-list (arg)
86fbb8ca 3870 "Show all (not done) TODO entries from all agenda file in a single list.
20908596
CD
3871The prefix arg can be used to select a specific TODO keyword and limit
3872the list to these. When using \\[universal-argument], you will be prompted
3873for a keyword. A numeric prefix directly selects the Nth keyword in
3874`org-todo-keywords-1'."
3875 (interactive "P")
20908596
CD
3876 (org-compile-prefix-format 'todo)
3877 (org-set-sorting-strategy 'todo)
3878 (org-prepare-agenda "TODO")
ed21c5c8 3879 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
acedf35c 3880 (let* ((today (org-today))
20908596
CD
3881 (date (calendar-gregorian-from-absolute today))
3882 (kwds org-todo-keywords-for-agenda)
3883 (completion-ignore-case t)
3884 (org-select-this-todo-keyword
3885 (if (stringp arg) arg
3886 (and arg (integerp arg) (> arg 0)
3887 (nth (1- arg) kwds))))
3888 rtn rtnall files file pos)
3889 (when (equal arg '(4))
3890 (setq org-select-this-todo-keyword
54a0dee5 3891 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
20908596
CD
3892 (mapcar 'list kwds) nil nil)))
3893 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3894 (org-set-local 'org-last-arg arg)
3895 (setq org-agenda-redo-command
3896 '(org-todo-list (or current-prefix-arg org-last-arg)))
2c3ad40d 3897 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
3898 rtnall nil)
3899 (while (setq file (pop files))
3900 (catch 'nextfile
3901 (org-check-agenda-file file)
3902 (setq rtn (org-agenda-get-day-entries file date :todo))
3903 (setq rtnall (append rtnall rtn))))
3904 (if org-agenda-overriding-header
3905 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3906 nil 'face 'org-agenda-structure) "\n")
3907 (insert "Global list of TODO items of type: ")
3908 (add-text-properties (point-min) (1- (point))
8d642074
CD
3909 (list 'face 'org-agenda-structure
3910 'short-heading
3911 (concat "ToDo: "
3912 (or org-select-this-todo-keyword "ALL"))))
3913 (org-agenda-mark-header-line (point-min))
20908596
CD
3914 (setq pos (point))
3915 (insert (or org-select-this-todo-keyword "ALL") "\n")
3916 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3917 (setq pos (point))
3918 (unless org-agenda-multi
3919 (insert "Available with `N r': (0)ALL")
3920 (let ((n 0) s)
3921 (mapc (lambda (x)
3922 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3923 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3924 (insert "\n "))
3925 (insert " " s))
3926 kwds))
3927 (insert "\n"))
3928 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 3929 (org-agenda-mark-header-line (point-min))
20908596
CD
3930 (when rtnall
3931 (insert (org-finalize-agenda-entries rtnall) "\n"))
3932 (goto-char (point-min))
c8d0cf5c 3933 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3934 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3935 (org-finalize-agenda)
3936 (setq buffer-read-only t)))
3937
3938;;; Agenda tags match
3939
3940;;;###autoload
3941(defun org-tags-view (&optional todo-only match)
3942 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3943The prefix arg TODO-ONLY limits the search to TODO entries."
3944 (interactive "P")
3945 (org-compile-prefix-format 'tags)
3946 (org-set-sorting-strategy 'tags)
3947 (let* ((org-tags-match-list-sublevels
c8d0cf5c 3948 org-tags-match-list-sublevels)
20908596
CD
3949 (completion-ignore-case t)
3950 rtn rtnall files file pos matcher
3951 buffer)
ed21c5c8
CD
3952 (when (and (stringp match) (not (string-match "\\S-" match)))
3953 (setq match nil))
20908596
CD
3954 (setq matcher (org-make-tags-matcher match)
3955 match (car matcher) matcher (cdr matcher))
3956 (org-prepare-agenda (concat "TAGS " match))
3957 (setq org-agenda-query-string match)
3958 (setq org-agenda-redo-command
3959 (list 'org-tags-view (list 'quote todo-only)
3960 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
2c3ad40d 3961 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
3962 rtnall nil)
3963 (while (setq file (pop files))
3964 (catch 'nextfile
3965 (org-check-agenda-file file)
3966 (setq buffer (if (file-exists-p file)
3967 (org-get-agenda-file-buffer file)
3968 (error "No such file %s" file)))
3969 (if (not buffer)
33306645 3970 ;; If file does not exist, error message to agenda
20908596
CD
3971 (setq rtn (list
3972 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3973 rtnall (append rtnall rtn))
3974 (with-current-buffer buffer
3975 (unless (org-mode-p)
3976 (error "Agenda file %s is not in `org-mode'" file))
3977 (save-excursion
3978 (save-restriction
3979 (if org-agenda-restrict
3980 (narrow-to-region org-agenda-restrict-begin
3981 org-agenda-restrict-end)
3982 (widen))
3983 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3984 (setq rtnall (append rtnall rtn))))))))
3985 (if org-agenda-overriding-header
3986 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3987 nil 'face 'org-agenda-structure) "\n")
3988 (insert "Headlines with TAGS match: ")
3989 (add-text-properties (point-min) (1- (point))
8d642074
CD
3990 (list 'face 'org-agenda-structure
3991 'short-heading
3992 (concat "Match: " match)))
20908596
CD
3993 (setq pos (point))
3994 (insert match "\n")
3995 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3996 (setq pos (point))
3997 (unless org-agenda-multi
3998 (insert "Press `C-u r' to search again with new search string\n"))
3999 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 4000 (org-agenda-mark-header-line (point-min))
20908596
CD
4001 (when rtnall
4002 (insert (org-finalize-agenda-entries rtnall) "\n"))
4003 (goto-char (point-min))
c8d0cf5c 4004 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
4005 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4006 (org-finalize-agenda)
4007 (setq buffer-read-only t)))
4008
4009;;; Agenda Finding stuck projects
4010
4011(defvar org-agenda-skip-regexp nil
4012 "Regular expression used in skipping subtrees for the agenda.
4013This is basically a temporary global variable that can be set and then
4014used by user-defined selections using `org-agenda-skip-function'.")
4015
4016(defvar org-agenda-overriding-header nil
c8d0cf5c
CD
4017 "When this is set during todo and tags searches, will replace header.
4018This variable should not be set directly, but custom commands can bind it
4019in the options section.")
4020
4021(defun org-agenda-skip-entry-when-regexp-matches ()
86fbb8ca 4022 "Check if the current entry contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4023If yes, it returns the end position of this entry, causing agenda commands
4024to skip the entry but continuing the search in the subtree. This is a
4025function that can be put into `org-agenda-skip-function' for the duration
4026of a command."
4027 (let ((end (save-excursion (org-end-of-subtree t)))
4028 skip)
4029 (save-excursion
4030 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4031 (and skip end)))
20908596
CD
4032
4033(defun org-agenda-skip-subtree-when-regexp-matches ()
86fbb8ca 4034 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
20908596
CD
4035If yes, it returns the end position of this tree, causing agenda commands
4036to skip this subtree. This is a function that can be put into
4037`org-agenda-skip-function' for the duration of a command."
4038 (let ((end (save-excursion (org-end-of-subtree t)))
4039 skip)
4040 (save-excursion
4041 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4042 (and skip end)))
4043
c8d0cf5c 4044(defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
86fbb8ca 4045 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4046If yes, it returns the end position of the current entry (NOT the tree),
4047causing agenda commands to skip the entry but continuing the search in
4048the subtree. This is a function that can be put into
4049`org-agenda-skip-function' for the duration of a command. An important
4050use of this function is for the stuck project list."
4051 (let ((end (save-excursion (org-end-of-subtree t)))
4052 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4053 skip)
4054 (save-excursion
4055 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4056 (and skip entry-end)))
4057
20908596
CD
4058(defun org-agenda-skip-entry-if (&rest conditions)
4059 "Skip entry if any of CONDITIONS is true.
4060See `org-agenda-skip-if' for details."
4061 (org-agenda-skip-if nil conditions))
4062
4063(defun org-agenda-skip-subtree-if (&rest conditions)
4064 "Skip entry if any of CONDITIONS is true.
4065See `org-agenda-skip-if' for details."
4066 (org-agenda-skip-if t conditions))
4067
4068(defun org-agenda-skip-if (subtree conditions)
4069 "Checks current entity for CONDITIONS.
4070If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4071the entry, i.e. the text before the next heading is checked.
4072
4073CONDITIONS is a list of symbols, boolean OR is used to combine the results
4074from different tests. Valid conditions are:
4075
4076scheduled Check if there is a scheduled cookie
4077notscheduled Check if there is no scheduled cookie
4078deadline Check if there is a deadline
4079notdeadline Check if there is no deadline
c8d0cf5c
CD
4080timestamp Check if there is a timestamp (also deadline or scheduled)
4081nottimestamp Check if there is no timestamp (also deadline or scheduled)
20908596
CD
4082regexp Check if regexp matches
4083notregexp Check if regexp does not match.
ed21c5c8
CD
4084todo Check if TODO keyword matches
4085nottodo Check if TODO keyword does not match
20908596
CD
4086
4087The regexp is taken from the conditions list, it must come right after
4088the `regexp' or `notregexp' element.
4089
ed21c5c8
CD
4090`todo' and `nottodo' accept as an argument a list of todo
4091keywords, which may include \"*\" to match any todo keyword.
4092
4093 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4094
4095would skip all entries with \"TODO\" or \"WAITING\" keywords.
4096
4097Instead of a list a keyword class may be given
4098
4099 (org-agenda-skip-entry-if 'nottodo 'done)
4100
4101would skip entries that haven't been marked with any of \"DONE\"
4102keywords. Possible classes are: `todo', `done', `any'.
4103
20908596
CD
4104If any of these conditions is met, this function returns the end point of
4105the entity, causing the search to continue from there. This is a function
4106that can be put into `org-agenda-skip-function' for the duration of a command."
4107 (let (beg end m)
4108 (org-back-to-heading t)
4109 (setq beg (point)
4110 end (if subtree
4111 (progn (org-end-of-subtree t) (point))
4112 (progn (outline-next-heading) (1- (point)))))
4113 (goto-char beg)
4114 (and
4115 (or
4116 (and (memq 'scheduled conditions)
4117 (re-search-forward org-scheduled-time-regexp end t))
4118 (and (memq 'notscheduled conditions)
4119 (not (re-search-forward org-scheduled-time-regexp end t)))
4120 (and (memq 'deadline conditions)
4121 (re-search-forward org-deadline-time-regexp end t))
4122 (and (memq 'notdeadline conditions)
4123 (not (re-search-forward org-deadline-time-regexp end t)))
c8d0cf5c
CD
4124 (and (memq 'timestamp conditions)
4125 (re-search-forward org-ts-regexp end t))
4126 (and (memq 'nottimestamp conditions)
4127 (not (re-search-forward org-ts-regexp end t)))
20908596
CD
4128 (and (setq m (memq 'regexp conditions))
4129 (stringp (nth 1 m))
4130 (re-search-forward (nth 1 m) end t))
4131 (and (setq m (memq 'notregexp conditions))
4132 (stringp (nth 1 m))
ed21c5c8
CD
4133 (not (re-search-forward (nth 1 m) end t)))
4134 (and (or
4135 (setq m (memq 'todo conditions))
4136 (setq m (memq 'nottodo conditions)))
4137 (org-agenda-skip-if-todo m end)))
20908596
CD
4138 end)))
4139
ed21c5c8
CD
4140(defun org-agenda-skip-if-todo (args end)
4141 "Helper function for `org-agenda-skip-if', do not use it directly.
4142ARGS is a list with first element either `todo' or `nottodo'.
4143The remainder is either a list of TODO keywords, or a state symbol
4144`todo' or `done' or `any'."
4145 (let ((kw (car args))
4146 (arg (cadr args))
4147 todo-wds todo-re)
4148 (setq todo-wds
4149 (org-uniquify
4150 (cond
4151 ((listp arg) ;; list of keywords
4152 (if (member "*" arg)
4153 (mapcar 'substring-no-properties org-todo-keywords-1)
4154 arg))
4155 ((symbolp arg) ;; keyword class name
4156 (cond
4157 ((eq arg 'todo)
4158 (org-delete-all org-done-keywords
4159 (mapcar 'substring-no-properties
4160 org-todo-keywords-1)))
4161 ((eq arg 'done) org-done-keywords)
4162 ((eq arg 'any)
4163 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4164 (setq todo-re
4165 (concat "^\\*+[ \t]+\\<\\("
4166 (mapconcat 'identity todo-wds "\\|")
4167 "\\)\\>"))
4168 (if (eq kw 'todo)
4169 (re-search-forward todo-re end t)
4170 (not (re-search-forward todo-re end t)))))
4171
20908596
CD
4172;;;###autoload
4173(defun org-agenda-list-stuck-projects (&rest ignore)
4174 "Create agenda view for projects that are stuck.
4175Stuck projects are project that have no next actions. For the definitions
4176of what a project is and how to check if it stuck, customize the variable
afe98dfa 4177`org-stuck-projects'."
20908596 4178 (interactive)
c8d0cf5c
CD
4179 (let* ((org-agenda-skip-function
4180 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
20908596 4181 ;; We could have used org-agenda-skip-if here.
c8d0cf5c
CD
4182 (org-agenda-overriding-header
4183 (or org-agenda-overriding-header "List of stuck projects: "))
20908596
CD
4184 (matcher (nth 0 org-stuck-projects))
4185 (todo (nth 1 org-stuck-projects))
4186 (todo-wds (if (member "*" todo)
4187 (progn
2c3ad40d
CD
4188 (org-prepare-agenda-buffers (org-agenda-files
4189 nil 'ifmode))
20908596
CD
4190 (org-delete-all
4191 org-done-keywords-for-agenda
4192 (copy-sequence org-todo-keywords-for-agenda)))
4193 todo))
4194 (todo-re (concat "^\\*+[ \t]+\\("
4195 (mapconcat 'identity todo-wds "\\|")
4196 "\\)\\>"))
4197 (tags (nth 2 org-stuck-projects))
4198 (tags-re (if (member "*" tags)
afe98dfa 4199 (org-re "^\\*+ .*:[[:alnum:]_@#%]+:[ \t]*$")
c8d0cf5c
CD
4200 (if tags
4201 (concat "^\\*+ .*:\\("
4202 (mapconcat 'identity tags "\\|")
afe98dfa 4203 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
20908596
CD
4204 (gen-re (nth 3 org-stuck-projects))
4205 (re-list
4206 (delq nil
4207 (list
4208 (if todo todo-re)
4209 (if tags tags-re)
4210 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4211 gen-re)))))
4212 (setq org-agenda-skip-regexp
4213 (if re-list
4214 (mapconcat 'identity re-list "\\|")
4215 (error "No information how to identify unstuck projects")))
4216 (org-tags-view nil matcher)
4217 (with-current-buffer org-agenda-buffer-name
4218 (setq org-agenda-redo-command
4219 '(org-agenda-list-stuck-projects
4220 (or current-prefix-arg org-last-arg))))))
4221
4222;;; Diary integration
4223
4224(defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4225(defvar list-diary-entries-hook)
4226
4227(defun org-get-entries-from-diary (date)
4228 "Get the (Emacs Calendar) diary entries for DATE."
4229 (require 'diary-lib)
4230 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
20908596 4231 (diary-display-hook '(fancy-diary-display))
ca8ef0dc 4232 (diary-display-function 'fancy-diary-display)
20908596
CD
4233 (pop-up-frames nil)
4234 (list-diary-entries-hook
4235 (cons 'org-diary-default-entry list-diary-entries-hook))
4236 (diary-file-name-prefix-function nil) ; turn this feature off
4237 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4238 entries
4239 (org-disable-agenda-to-diary t))
4240 (save-excursion
4241 (save-window-excursion
4242 (funcall (if (fboundp 'diary-list-entries)
4243 'diary-list-entries 'list-diary-entries)
4244 date 1)))
4245 (if (not (get-buffer diary-fancy-buffer))
4246 (setq entries nil)
4247 (with-current-buffer diary-fancy-buffer
4248 (setq buffer-read-only nil)
4249 (if (zerop (buffer-size))
4250 ;; No entries
4251 (setq entries nil)
4252 ;; Omit the date and other unnecessary stuff
4253 (org-agenda-cleanup-fancy-diary)
4254 ;; Add prefix to each line and extend the text properties
4255 (if (zerop (buffer-size))
4256 (setq entries nil)
4257 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
4258 (set-buffer-modified-p nil)
4259 (kill-buffer diary-fancy-buffer)))
4260 (when entries
4261 (setq entries (org-split-string entries "\n"))
4262 (setq entries
4263 (mapcar
4264 (lambda (x)
4265 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4266 ;; Extend the text properties to the beginning of the line
4267 (org-add-props x (text-properties-at (1- (length x)) x)
ed21c5c8 4268 'type "diary" 'date date 'face 'org-agenda-diary))
20908596
CD
4269 entries)))))
4270
c8d0cf5c
CD
4271(defvar org-agenda-cleanup-fancy-diary-hook nil
4272 "Hook run when the fancy diary buffer is cleaned up.")
4273
20908596
CD
4274(defun org-agenda-cleanup-fancy-diary ()
4275 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4276This gets rid of the date, the underline under the date, and
4277the dummy entry installed by `org-mode' to ensure non-empty diary for each
4278date. It also removes lines that contain only whitespace."
4279 (goto-char (point-min))
4280 (if (looking-at ".*?:[ \t]*")
4281 (progn
4282 (replace-match "")
4283 (re-search-forward "\n=+$" nil t)
4284 (replace-match "")
4285 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4286 (re-search-forward "\n=+$" nil t)
4287 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4288 (goto-char (point-min))
4289 (while (re-search-forward "^ +\n" nil t)
4290 (replace-match ""))
4291 (goto-char (point-min))
4292 (if (re-search-forward "^Org-mode dummy\n?" nil t)
c8d0cf5c
CD
4293 (replace-match ""))
4294 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
20908596
CD
4295
4296;; Make sure entries from the diary have the right text properties.
4297(eval-after-load "diary-lib"
4298 '(if (boundp 'diary-modify-entry-list-string-function)
4299 ;; We can rely on the hook, nothing to do
4300 nil
33306645 4301 ;; Hook not available, must use advice to make this work
20908596
CD
4302 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4303 "Make the position visible."
4304 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4305 (stringp string)
4306 buffer-file-name)
4307 (setq string (org-modify-diary-entry-string string))))))
4308
4309(defun org-modify-diary-entry-string (string)
4310 "Add text properties to string, allowing org-mode to act on it."
4311 (org-add-props string nil
4312 'mouse-face 'highlight
20908596
CD
4313 'help-echo (if buffer-file-name
4314 (format "mouse-2 or RET jump to diary file %s"
4315 (abbreviate-file-name buffer-file-name))
4316 "")
4317 'org-agenda-diary-link t
4318 'org-marker (org-agenda-new-marker (point-at-bol))))
4319
4320(defun org-diary-default-entry ()
4321 "Add a dummy entry to the diary.
4322Needed to avoid empty dates which mess up holiday display."
4323 ;; Catch the error if dealing with the new add-to-diary-alist
4324 (when org-disable-agenda-to-diary
4325 (condition-case nil
4326 (org-add-to-diary-list original-date "Org-mode dummy" "")
4327 (error
4328 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4329
4330(defun org-add-to-diary-list (&rest args)
4331 (if (fboundp 'diary-add-to-list)
4332 (apply 'diary-add-to-list args)
4333 (apply 'add-to-diary-list args)))
4334
ed21c5c8
CD
4335(defvar org-diary-last-run-time nil)
4336
20908596
CD
4337;;;###autoload
4338(defun org-diary (&rest args)
4339 "Return diary information from org-files.
4340This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4341It accesses org files and extracts information from those files to be
4342listed in the diary. The function accepts arguments specifying what
ed21c5c8
CD
4343items should be listed. For a list of arguments allowed here, see the
4344variable `org-agenda-entry-types'.
20908596
CD
4345
4346The call in the diary file should look like this:
4347
4348 &%%(org-diary) ~/path/to/some/orgfile.org
4349
4350Use a separate line for each org file to check. Or, if you omit the file name,
4351all files listed in `org-agenda-files' will be checked automatically:
4352
4353 &%%(org-diary)
4354
4355If you don't give any arguments (as in the example above), the default
4356arguments (:deadline :scheduled :timestamp :sexp) are used.
4357So the example above may also be written as
4358
4359 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4360
4361The function expects the lisp variables `entry' and `date' to be provided
4362by the caller, because this is how the calendar works. Don't use this
4363function from a program - use `org-agenda-get-day-entries' instead."
54a0dee5 4364 (when (> (- (org-float-time)
20908596
CD
4365 org-agenda-last-marker-time)
4366 5)
4367 (org-agenda-reset-markers))
4368 (org-compile-prefix-format 'agenda)
4369 (org-set-sorting-strategy 'agenda)
4370 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4371 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4372 (list entry)
4373 (org-agenda-files t)))
ed21c5c8 4374 (time (org-float-time))
20908596 4375 file rtn results)
ed21c5c8
CD
4376 (when (or (not org-diary-last-run-time)
4377 (> (- time
4378 org-diary-last-run-time)
4379 3))
4380 (org-prepare-agenda-buffers files))
4381 (setq org-diary-last-run-time time)
20908596
CD
4382 ;; If this is called during org-agenda, don't return any entries to
4383 ;; the calendar. Org Agenda will list these entries itself.
4384 (if org-disable-agenda-to-diary (setq files nil))
4385 (while (setq file (pop files))
4386 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4387 (setq results (append results rtn)))
4388 (if results
4389 (concat (org-finalize-agenda-entries results) "\n"))))
4390
4391;;; Agenda entry finders
4392
4393(defun org-agenda-get-day-entries (file date &rest args)
4394 "Does the work for `org-diary' and `org-agenda'.
4395FILE is the path to a file to be checked for entries. DATE is date like
4396the one returned by `calendar-current-date'. ARGS are symbols indicating
4397which kind of entries should be extracted. For details about these, see
4398the documentation of `org-diary'."
4399 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4400 (let* ((org-startup-folded nil)
4401 (org-startup-align-all-tables nil)
4402 (buffer (if (file-exists-p file)
4403 (org-get-agenda-file-buffer file)
4404 (error "No such file %s" file)))
54a0dee5 4405 arg results rtn deadline-results)
20908596
CD
4406 (if (not buffer)
4407 ;; If file does not exist, make sure an error message ends up in diary
4408 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4409 (with-current-buffer buffer
4410 (unless (org-mode-p)
4411 (error "Agenda file %s is not in `org-mode'" file))
4412 (let ((case-fold-search nil))
4413 (save-excursion
4414 (save-restriction
4415 (if org-agenda-restrict
4416 (narrow-to-region org-agenda-restrict-begin
4417 org-agenda-restrict-end)
4418 (widen))
4419 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4420 (while (setq arg (pop args))
4421 (cond
4422 ((and (eq arg :todo)
4423 (equal date (calendar-current-date)))
4424 (setq rtn (org-agenda-get-todos))
4425 (setq results (append results rtn)))
4426 ((eq arg :timestamp)
4427 (setq rtn (org-agenda-get-blocks))
4428 (setq results (append results rtn))
4429 (setq rtn (org-agenda-get-timestamps))
4430 (setq results (append results rtn)))
4431 ((eq arg :sexp)
4432 (setq rtn (org-agenda-get-sexps))
4433 (setq results (append results rtn)))
4434 ((eq arg :scheduled)
54a0dee5 4435 (setq rtn (org-agenda-get-scheduled deadline-results))
20908596
CD
4436 (setq results (append results rtn)))
4437 ((eq arg :closed)
93b62de8 4438 (setq rtn (org-agenda-get-progress))
20908596
CD
4439 (setq results (append results rtn)))
4440 ((eq arg :deadline)
4441 (setq rtn (org-agenda-get-deadlines))
54a0dee5 4442 (setq deadline-results (copy-sequence rtn))
20908596
CD
4443 (setq results (append results rtn))))))))
4444 results))))
4445
4446(defun org-agenda-get-todos ()
4447 "Return the TODO information for agenda display."
4448 (let* ((props (list 'face nil
c8d0cf5c 4449 'done-face 'org-agenda-done
20908596
CD
4450 'org-not-done-regexp org-not-done-regexp
4451 'org-todo-regexp org-todo-regexp
b349f79f 4452 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4453 'mouse-face 'highlight
20908596
CD
4454 'help-echo
4455 (format "mouse-2 or RET jump to org file %s"
4456 (abbreviate-file-name buffer-file-name))))
4457 (regexp (concat "^\\*+[ \t]+\\("
4458 (if org-select-this-todo-keyword
4459 (if (equal org-select-this-todo-keyword "*")
4460 org-todo-regexp
4461 (concat "\\<\\("
4462 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4463 "\\)\\>"))
4464 org-not-done-regexp)
4465 "[^\n\r]*\\)"))
621f83e4 4466 marker priority category tags todo-state
20908596
CD
4467 ee txt beg end)
4468 (goto-char (point-min))
4469 (while (re-search-forward regexp nil t)
4470 (catch :skip
4471 (save-match-data
4472 (beginning-of-line)
d6685abc 4473 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
0bd48b37 4474 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
20908596
CD
4475 (goto-char (1+ beg))
4476 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4477 (throw :skip nil)))
4478 (goto-char beg)
4479 (org-agenda-skip)
4480 (goto-char (match-beginning 1))
4481 (setq marker (org-agenda-new-marker (match-beginning 0))
4482 category (org-get-category)
c8d0cf5c 4483 txt (match-string 1)
20908596 4484 tags (org-get-tags-at (point))
c8d0cf5c 4485 txt (org-format-agenda-item "" txt category tags)
621f83e4
CD
4486 priority (1+ (org-get-priority txt))
4487 todo-state (org-get-todo-state))
20908596
CD
4488 (org-add-props txt props
4489 'org-marker marker 'org-hd-marker marker
4490 'priority priority 'org-category category
621f83e4 4491 'type "todo" 'todo-state todo-state)
20908596
CD
4492 (push txt ee)
4493 (if org-agenda-todo-list-sublevels
4494 (goto-char (match-end 1))
4495 (org-end-of-subtree 'invisible))))
4496 (nreverse ee)))
4497
0bd48b37 4498;;;###autoload
ed21c5c8
CD
4499(defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4500 (&optional end)
4501 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
0bd48b37
CD
4502 (when (or org-agenda-todo-ignore-with-date
4503 org-agenda-todo-ignore-scheduled
acedf35c
CD
4504 org-agenda-todo-ignore-deadlines
4505 org-agenda-todo-ignore-timestamp)
0bd48b37
CD
4506 (setq end (or end (save-excursion (outline-next-heading) (point))))
4507 (save-excursion
4508 (or (and org-agenda-todo-ignore-with-date
4509 (re-search-forward org-ts-regexp end t))
4510 (and org-agenda-todo-ignore-scheduled
ed21c5c8
CD
4511 (re-search-forward org-scheduled-time-regexp end t)
4512 (cond
4513 ((eq org-agenda-todo-ignore-scheduled 'future)
4514 (> (org-days-to-time (match-string 1)) 0))
4515 ((eq org-agenda-todo-ignore-scheduled 'past)
4516 (<= (org-days-to-time (match-string 1)) 0))
4517 (t)))
0bd48b37
CD
4518 (and org-agenda-todo-ignore-deadlines
4519 (re-search-forward org-deadline-time-regexp end t)
ed21c5c8
CD
4520 (cond
4521 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4522 ((eq org-agenda-todo-ignore-deadlines 'far)
4523 (not (org-deadline-close (match-string 1))))
4524 ((eq org-agenda-todo-ignore-deadlines 'future)
4525 (> (org-days-to-time (match-string 1)) 0))
4526 ((eq org-agenda-todo-ignore-deadlines 'past)
4527 (<= (org-days-to-time (match-string 1)) 0))
acedf35c
CD
4528 (t (org-deadline-close (match-string 1)))))
4529 (and org-agenda-todo-ignore-timestamp
4530 (let ((buffer (current-buffer))
4531 (regexp
4532 (concat
4533 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4534 (start (point)))
4535 ;; Copy current buffer into a temporary one
4536 (with-temp-buffer
4537 (insert-buffer-substring buffer start end)
4538 (goto-char (point-min))
4539 ;; Delete SCHEDULED and DEADLINE items
4540 (while (re-search-forward regexp end t)
4541 (delete-region (match-beginning 0) (match-end 0)))
4542 (goto-char (point-min))
4543 ;; No search for timestamp left
4544 (when (re-search-forward org-ts-regexp nil t)
4545 (cond
4546 ((eq org-agenda-todo-ignore-timestamp 'future)
4547 (> (org-days-to-time (match-string 1)) 0))
4548 ((eq org-agenda-todo-ignore-timestamp 'past)
4549 (<= (org-days-to-time (match-string 1)) 0))
4550 (t))))))))))
0bd48b37 4551
20908596
CD
4552(defconst org-agenda-no-heading-message
4553 "No heading for this item in buffer or region.")
4554
4555(defun org-agenda-get-timestamps ()
4556 "Return the date stamp information for agenda display."
4557 (let* ((props (list 'face nil
4558 'org-not-done-regexp org-not-done-regexp
4559 'org-todo-regexp org-todo-regexp
b349f79f 4560 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4561 'mouse-face 'highlight
20908596
CD
4562 'help-echo
4563 (format "mouse-2 or RET jump to org file %s"
4564 (abbreviate-file-name buffer-file-name))))
4565 (d1 (calendar-absolute-from-gregorian date))
4566 (remove-re
4567 (concat
4568 (regexp-quote
4569 (format-time-string
4570 "<%Y-%m-%d"
4571 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4572 ".*?>"))
4573 (regexp
4574 (concat
4575 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4576 (regexp-quote
4577 (substring
4578 (format-time-string
4579 (car org-time-stamp-formats)
4580 (apply 'encode-time ; DATE bound by calendar
4581 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4582 1 11))
4583 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4584 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4585 marker hdmarker deadlinep scheduledp clockp closedp inactivep
621f83e4 4586 donep tmp priority category ee txt timestr tags b0 b3 e3 head
c8d0cf5c 4587 todo-state end-of-match)
20908596 4588 (goto-char (point-min))
c8d0cf5c 4589 (while (setq end-of-match (re-search-forward regexp nil t))
20908596
CD
4590 (setq b0 (match-beginning 0)
4591 b3 (match-beginning 3) e3 (match-end 3))
4592 (catch :skip
4593 (and (org-at-date-range-p) (throw :skip nil))
4594 (org-agenda-skip)
4595 (if (and (match-end 1)
4596 (not (= d1 (org-time-string-to-absolute
4597 (match-string 1) d1 nil
4598 org-agenda-repeating-timestamp-show-all))))
4599 (throw :skip nil))
4600 (if (and e3
4601 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4602 (throw :skip nil))
c8d0cf5c 4603 (setq tmp (buffer-substring (max (point-min)
20908596
CD
4604 (- b0 org-ds-keyword-length))
4605 b0)
4606 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4607 inactivep (= (char-after b0) ?\[)
4608 deadlinep (string-match org-deadline-regexp tmp)
4609 scheduledp (string-match org-scheduled-regexp tmp)
4610 closedp (and org-agenda-include-inactive-timestamps
4611 (string-match org-closed-string tmp))
4612 clockp (and org-agenda-include-inactive-timestamps
4613 (or (string-match org-clock-string tmp)
4614 (string-match "]-+\\'" tmp)))
86fbb8ca 4615 todo-state (ignore-errors (org-get-todo-state))
621f83e4 4616 donep (member todo-state org-done-keywords))
c8d0cf5c
CD
4617 (if (or scheduledp deadlinep closedp clockp
4618 (and donep org-agenda-skip-timestamp-if-done))
20908596
CD
4619 (throw :skip t))
4620 (if (string-match ">" timestr)
4621 ;; substring should only run to end of time stamp
4622 (setq timestr (substring timestr 0 (match-end 0))))
c8d0cf5c
CD
4623 (setq marker (org-agenda-new-marker b0)
4624 category (org-get-category b0))
20908596 4625 (save-excursion
c8d0cf5c
CD
4626 (if (not (re-search-backward "^\\*+ " nil t))
4627 (setq txt org-agenda-no-heading-message)
4628 (goto-char (match-beginning 0))
4629 (setq hdmarker (org-agenda-new-marker)
4630 tags (org-get-tags-at))
4631 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4632 (setq head (match-string 1))
4633 (setq txt (org-format-agenda-item
ed21c5c8 4634 (if inactivep org-agenda-inactive-leader nil)
c8d0cf5c
CD
4635 head category tags timestr nil
4636 remove-re)))
20908596
CD
4637 (setq priority (org-get-priority txt))
4638 (org-add-props txt props
4639 'org-marker marker 'org-hd-marker hdmarker)
4640 (org-add-props txt nil 'priority priority
4641 'org-category category 'date date
621f83e4 4642 'todo-state todo-state
20908596
CD
4643 'type "timestamp")
4644 (push txt ee))
c8d0cf5c
CD
4645 (if org-agenda-skip-additional-timestamps-same-entry
4646 (outline-next-heading)
4647 (goto-char end-of-match))))
20908596
CD
4648 (nreverse ee)))
4649
4650(defun org-agenda-get-sexps ()
4651 "Return the sexp information for agenda display."
4652 (require 'diary-lib)
4653 (let* ((props (list 'face nil
4654 'mouse-face 'highlight
20908596
CD
4655 'help-echo
4656 (format "mouse-2 or RET jump to org file %s"
4657 (abbreviate-file-name buffer-file-name))))
4658 (regexp "^&?%%(")
c8d0cf5c
CD
4659 marker category ee txt tags entry result beg b sexp sexp-entry
4660 todo-state)
20908596
CD
4661 (goto-char (point-min))
4662 (while (re-search-forward regexp nil t)
4663 (catch :skip
4664 (org-agenda-skip)
4665 (setq beg (match-beginning 0))
4666 (goto-char (1- (match-end 0)))
4667 (setq b (point))
4668 (forward-sexp 1)
4669 (setq sexp (buffer-substring b (point)))
4670 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4671 (org-trim (match-string 1))
4672 ""))
4673 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4674 (when result
4675 (setq marker (org-agenda-new-marker beg)
c8d0cf5c
CD
4676 category (org-get-category beg)
4677 todo-state (org-get-todo-state))
20908596 4678
afe98dfa
CD
4679 (dolist (r (if (stringp result)
4680 (list result)
4681 result)) ;; we expect a list here
4682 (if (string-match "\\S-" r)
4683 (setq txt r)
4684 (setq txt "SEXP entry returned empty string"))
4685
4686 (setq txt (org-format-agenda-item
4687 "" txt category tags 'time))
4688 (org-add-props txt props 'org-marker marker)
4689 (org-add-props txt nil
4690 'org-category category 'date date 'todo-state todo-state
4691 'type "sexp")
4692 (push txt ee)))))
20908596
CD
4693 (nreverse ee)))
4694
ed21c5c8
CD
4695(defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4696 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4697The order of the first 2 times 3 arguments depends on the variable
4698`calendar-date-style' or, if that is not defined, on `european-calendar-style'.
86fbb8ca 4699So for American calendars, give this as MONTH DAY YEAR, for European as
ed21c5c8
CD
4700DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4701DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4702is any number of ISO weeks in the block period for which the item should
4703be skipped."
4704 (let* ((date1 (calendar-absolute-from-gregorian
4705 (org-order-calendar-date-args m1 d1 y1)))
4706 (date2 (calendar-absolute-from-gregorian
4707 (org-order-calendar-date-args m2 d2 y2)))
4708 (d (calendar-absolute-from-gregorian date)))
4709 (and
4710 (<= date1 d)
4711 (<= d date2)
4712 (= (calendar-day-of-week date) dayname)
4713 (or (not skip-weeks)
4714 (progn
4715 (require 'cal-iso)
4716 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4717 entry)))
4718
d60b1ba1 4719(defalias 'org-get-closed 'org-agenda-get-progress)
93b62de8 4720(defun org-agenda-get-progress ()
20908596
CD
4721 "Return the logged TODO entries for agenda display."
4722 (let* ((props (list 'mouse-face 'highlight
4723 'org-not-done-regexp org-not-done-regexp
4724 'org-todo-regexp org-todo-regexp
b349f79f 4725 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
4726 'help-echo
4727 (format "mouse-2 or RET jump to org file %s"
4728 (abbreviate-file-name buffer-file-name))))
93b62de8
CD
4729 (items (if (consp org-agenda-show-log)
4730 org-agenda-show-log
4731 org-agenda-log-mode-items))
ff4be292 4732 (parts
93b62de8
CD
4733 (delq nil
4734 (list
4735 (if (memq 'closed items) (concat "\\<" org-closed-string))
4736 (if (memq 'clock items) (concat "\\<" org-clock-string))
c8d0cf5c 4737 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
93b62de8
CD
4738 (parts-re (if parts (mapconcat 'identity parts "\\|")
4739 (error "`org-agenda-log-mode-items' is empty")))
20908596 4740 (regexp (concat
93b62de8
CD
4741 "\\(" parts-re "\\)"
4742 " *\\["
20908596
CD
4743 (regexp-quote
4744 (substring
4745 (format-time-string
4746 (car org-time-stamp-formats)
4747 (apply 'encode-time ; DATE bound by calendar
4748 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4749 1 11))))
c8d0cf5c
CD
4750 (org-agenda-search-headline-for-time nil)
4751 marker hdmarker priority category tags closedp statep clockp state
4752 ee txt extra timestr rest clocked)
20908596
CD
4753 (goto-char (point-min))
4754 (while (re-search-forward regexp nil t)
4755 (catch :skip
4756 (org-agenda-skip)
4757 (setq marker (org-agenda-new-marker (match-beginning 0))
4758 closedp (equal (match-string 1) org-closed-string)
93b62de8 4759 statep (equal (string-to-char (match-string 1)) ?-)
c8d0cf5c 4760 clockp (not (or closedp statep))
93b62de8 4761 state (and statep (match-string 2))
20908596
CD
4762 category (org-get-category (match-beginning 0))
4763 timestr (buffer-substring (match-beginning 0) (point-at-eol))
20908596 4764 )
b349f79f
CD
4765 (when (string-match "\\]" timestr)
4766 ;; substring should only run to end of time stamp
4767 (setq rest (substring timestr (match-end 0))
4768 timestr (substring timestr 0 (match-end 0)))
93b62de8 4769 (if (and (not closedp) (not statep)
c8d0cf5c 4770 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
621f83e4
CD
4771 (progn (setq timestr (concat (substring timestr 0 -1)
4772 "-" (match-string 1 rest) "]"))
4773 (setq clocked (match-string 2 rest)))
4774 (setq clocked "-")))
20908596 4775 (save-excursion
86fbb8ca 4776 (setq extra nil)
c8d0cf5c 4777 (cond
86fbb8ca 4778 ((not org-agenda-log-mode-add-notes))
c8d0cf5c
CD
4779 (statep
4780 (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4781 (setq extra (match-string 1))))
4782 (clockp
4783 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
86fbb8ca 4784 (setq extra (match-string 1)))))
c8d0cf5c
CD
4785 (if (not (re-search-backward "^\\*+ " nil t))
4786 (setq txt org-agenda-no-heading-message)
4787 (goto-char (match-beginning 0))
4788 (setq hdmarker (org-agenda-new-marker)
4789 tags (org-get-tags-at))
4790 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4791 (setq txt (match-string 1))
4792 (when extra
4793 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4794 (setq txt (concat (substring txt 0 (match-beginning 1))
4795 " - " extra " " (match-string 2 txt)))
4796 (setq txt (concat txt " - " extra))))
4797 (setq txt (org-format-agenda-item
4798 (cond
4799 (closedp "Closed: ")
93b62de8
CD
4800 (statep (concat "State: (" state ")"))
4801 (t (concat "Clocked: (" clocked ")")))
c8d0cf5c 4802 txt category tags timestr)))
20908596
CD
4803 (setq priority 100000)
4804 (org-add-props txt props
c8d0cf5c 4805 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
20908596
CD
4806 'priority priority 'org-category category
4807 'type "closed" 'date date
c8d0cf5c 4808 'undone-face 'org-warning 'done-face 'org-agenda-done)
20908596
CD
4809 (push txt ee))
4810 (goto-char (point-at-eol))))
4811 (nreverse ee)))
4812
4813(defun org-agenda-get-deadlines ()
4814 "Return the deadline information for agenda display."
4815 (let* ((props (list 'mouse-face 'highlight
4816 'org-not-done-regexp org-not-done-regexp
4817 'org-todo-regexp org-todo-regexp
b349f79f 4818 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
4819 'help-echo
4820 (format "mouse-2 or RET jump to org file %s"
4821 (abbreviate-file-name buffer-file-name))))
4822 (regexp org-deadline-time-regexp)
621f83e4 4823 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596
CD
4824 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4825 d2 diff dfrac wdays pos pos1 category tags
ed21c5c8 4826 suppress-prewarning
621f83e4 4827 ee txt head face s todo-state upcomingp donep timestr)
20908596
CD
4828 (goto-char (point-min))
4829 (while (re-search-forward regexp nil t)
ed21c5c8 4830 (setq suppress-prewarning nil)
20908596
CD
4831 (catch :skip
4832 (org-agenda-skip)
ed21c5c8
CD
4833 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
4834 (save-match-data
4835 (string-match org-scheduled-time-regexp
4836 (buffer-substring (point-at-bol)
4837 (point-at-eol)))))
4838 (setq suppress-prewarning
4839 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
4840 org-agenda-skip-deadline-prewarning-if-scheduled
4841 0)))
20908596 4842 (setq s (match-string 1)
c8d0cf5c 4843 txt nil
20908596
CD
4844 pos (1- (match-beginning 1))
4845 d2 (org-time-string-to-absolute
4846 (match-string 1) d1 'past
4847 org-agenda-repeating-timestamp-show-all)
4848 diff (- d2 d1)
ed21c5c8
CD
4849 wdays (if suppress-prewarning
4850 (let ((org-deadline-warning-days suppress-prewarning))
4851 (org-get-wdays s))
4852 (org-get-wdays s))
20908596
CD
4853 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
4854 upcomingp (and todayp (> diff 0)))
4855 ;; When to show a deadline in the calendar:
4856 ;; If the expiration is within wdays warning time.
4857 ;; Past-due deadlines are only shown on the current date
8bfe682a
CD
4858 (if (and (or (and (<= diff wdays)
4859 (and todayp (not org-agenda-only-exact-dates)))
4860 (= diff 0)))
20908596 4861 (save-excursion
621f83e4 4862 (setq todo-state (org-get-todo-state))
c8d0cf5c
CD
4863 (setq donep (member todo-state org-done-keywords))
4864 (if (and donep
4865 (or org-agenda-skip-deadline-if-done
4866 (not (= diff 0))))
4867 (setq txt nil)
4868 (setq category (org-get-category))
4869 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4870 (setq txt org-agenda-no-heading-message)
4871 (goto-char (match-end 0))
4872 (setq pos1 (match-beginning 0))
4873 (setq tags (org-get-tags-at pos1))
4874 (setq head (buffer-substring-no-properties
4875 (point)
4876 (progn (skip-chars-forward "^\r\n")
4877 (point))))
4878 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4879 (setq timestr
4880 (concat (substring s (match-beginning 1)) " "))
4881 (setq timestr 'time))
4882 (setq txt (org-format-agenda-item
4883 (if (= diff 0)
4884 (car org-agenda-deadline-leaders)
4885 (if (functionp
4886 (nth 1 org-agenda-deadline-leaders))
4887 (funcall
4888 (nth 1 org-agenda-deadline-leaders)
4889 diff date)
4890 (format (nth 1 org-agenda-deadline-leaders)
4891 diff)))
4892 head category tags
4893 (if (not (= diff 0)) nil timestr)))))
20908596
CD
4894 (when txt
4895 (setq face (org-agenda-deadline-face dfrac wdays))
4896 (org-add-props txt props
4897 'org-marker (org-agenda-new-marker pos)
4898 'org-hd-marker (org-agenda-new-marker pos1)
4899 'priority (+ (- diff)
4900 (org-get-priority txt))
4901 'org-category category
621f83e4 4902 'todo-state todo-state
20908596
CD
4903 'type (if upcomingp "upcoming-deadline" "deadline")
4904 'date (if upcomingp date d2)
c8d0cf5c
CD
4905 'face (if donep 'org-agenda-done face)
4906 'undone-face face 'done-face 'org-agenda-done)
20908596
CD
4907 (push txt ee))))))
4908 (nreverse ee)))
4909
4910(defun org-agenda-deadline-face (fraction &optional wdays)
4911 "Return the face to displaying a deadline item.
4912FRACTION is what fraction of the head-warning time has passed."
4913 (if (equal wdays 0) (setq fraction 1.))
4914 (let ((faces org-agenda-deadline-faces) f)
4915 (catch 'exit
4916 (while (setq f (pop faces))
4917 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
4918
54a0dee5 4919(defun org-agenda-get-scheduled (&optional deadline-results)
20908596
CD
4920 "Return the scheduled information for agenda display."
4921 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
4922 'org-todo-regexp org-todo-regexp
b349f79f 4923 'org-complex-heading-regexp org-complex-heading-regexp
c8d0cf5c 4924 'done-face 'org-agenda-done
20908596 4925 'mouse-face 'highlight
20908596
CD
4926 'help-echo
4927 (format "mouse-2 or RET jump to org file %s"
4928 (abbreviate-file-name buffer-file-name))))
4929 (regexp org-scheduled-time-regexp)
621f83e4 4930 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596 4931 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
54a0dee5
CD
4932 mm
4933 (deadline-position-alist
4934 (mapcar (lambda (a) (and (setq mm (get-text-property
8bfe682a
CD
4935 0 'org-hd-marker a))
4936 (cons (marker-position mm) a)))
54a0dee5 4937 deadline-results))
621f83e4 4938 d2 diff pos pos1 category tags donep
8bfe682a 4939 ee txt head pastschedp todo-state face timestr s habitp)
20908596
CD
4940 (goto-char (point-min))
4941 (while (re-search-forward regexp nil t)
4942 (catch :skip
4943 (org-agenda-skip)
4944 (setq s (match-string 1)
c8d0cf5c 4945 txt nil
20908596
CD
4946 pos (1- (match-beginning 1))
4947 d2 (org-time-string-to-absolute
4948 (match-string 1) d1 'past
4949 org-agenda-repeating-timestamp-show-all)
4950 diff (- d2 d1))
4951 (setq pastschedp (and todayp (< diff 0)))
4952 ;; When to show a scheduled item in the calendar:
4953 ;; If it is on or past the date.
8bfe682a
CD
4954 (when (or (and (< diff 0)
4955 (< (abs diff) org-scheduled-past-days)
4956 (and todayp (not org-agenda-only-exact-dates)))
4957 (= diff 0))
4958 (save-excursion
4959 (setq todo-state (org-get-todo-state))
4960 (setq donep (member todo-state org-done-keywords))
4961 (setq habitp (and (functionp 'org-is-habit-p)
4962 (org-is-habit-p)))
4963 (if (and donep
4964 (or habitp org-agenda-skip-scheduled-if-done
4965 (not (= diff 0))))
4966 (setq txt nil)
4967 (setq category (org-get-category))
4968 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4969 (setq txt org-agenda-no-heading-message)
4970 (goto-char (match-end 0))
4971 (setq pos1 (match-beginning 0))
4972 (if habitp
4973 (if (or (not org-habit-show-habits)
4974 (and (not todayp)
4975 org-habit-show-habits-only-for-today))
4976 (throw :skip nil))
54a0dee5
CD
4977 (if (and
4978 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
4979 (and org-agenda-skip-scheduled-if-deadline-is-shown
4980 pastschedp))
4981 (setq mm (assoc pos1 deadline-position-alist)))
8bfe682a
CD
4982 (throw :skip nil)))
4983 (setq tags (org-get-tags-at))
4984 (setq head (buffer-substring-no-properties
4985 (point)
4986 (progn (skip-chars-forward "^\r\n") (point))))
4987 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4988 (setq timestr
4989 (concat (substring s (match-beginning 1)) " "))
4990 (setq timestr 'time))
4991 (setq txt (org-format-agenda-item
4992 (if (= diff 0)
4993 (car org-agenda-scheduled-leaders)
4994 (format (nth 1 org-agenda-scheduled-leaders)
4995 (- 1 diff)))
4996 head category tags
4997 (if (not (= diff 0)) nil timestr)
4998 nil nil habitp))))
4999 (when txt
5000 (setq face
5001 (cond
5002 ((and (not habitp) pastschedp)
5003 'org-scheduled-previously)
5004 (todayp 'org-scheduled-today)
5005 (t 'org-scheduled))
5006 habitp (and habitp (org-habit-parse-todo)))
5007 (org-add-props txt props
5008 'undone-face face
5009 'face (if donep 'org-agenda-done face)
5010 'org-marker (org-agenda-new-marker pos)
5011 'org-hd-marker (org-agenda-new-marker pos1)
5012 'type (if pastschedp "past-scheduled" "scheduled")
5013 'date (if pastschedp d2 date)
5014 'priority (if habitp
5015 (org-habit-get-priority habitp)
5016 (+ 94 (- 5 diff) (org-get-priority txt)))
5017 'org-category category
5018 'org-habit-p habitp
5019 'todo-state todo-state)
5020 (push txt ee))))))
20908596
CD
5021 (nreverse ee)))
5022
5023(defun org-agenda-get-blocks ()
5024 "Return the date-range information for agenda display."
5025 (let* ((props (list 'face nil
5026 'org-not-done-regexp org-not-done-regexp
5027 'org-todo-regexp org-todo-regexp
b349f79f 5028 'org-complex-heading-regexp org-complex-heading-regexp
20908596 5029 'mouse-face 'highlight
20908596
CD
5030 'help-echo
5031 (format "mouse-2 or RET jump to org file %s"
5032 (abbreviate-file-name buffer-file-name))))
5033 (regexp org-tr-regexp)
5034 (d0 (calendar-absolute-from-gregorian date))
621f83e4 5035 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
c8d0cf5c 5036 head donep)
20908596
CD
5037 (goto-char (point-min))
5038 (while (re-search-forward regexp nil t)
5039 (catch :skip
5040 (org-agenda-skip)
5041 (setq pos (point))
5042 (setq timestr (match-string 0)
5043 s1 (match-string 1)
5044 s2 (match-string 2)
5045 d1 (time-to-days (org-time-string-to-time s1))
5046 d2 (time-to-days (org-time-string-to-time s2)))
5047 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5048 ;; Only allow days between the limits, because the normal
5049 ;; date stamps will catch the limits.
5050 (save-excursion
c8d0cf5c
CD
5051 (setq todo-state (org-get-todo-state))
5052 (setq donep (member todo-state org-done-keywords))
5053 (if (and donep org-agenda-skip-timestamp-if-done)
5054 (throw :skip t))
20908596
CD
5055 (setq marker (org-agenda-new-marker (point)))
5056 (setq category (org-get-category))
c8d0cf5c
CD
5057 (if (not (re-search-backward "^\\*+ " nil t))
5058 (setq txt org-agenda-no-heading-message)
5059 (goto-char (match-beginning 0))
5060 (setq hdmarker (org-agenda-new-marker (point)))
5061 (setq tags (org-get-tags-at))
5062 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5063 (setq head (match-string 1))
86fbb8ca
CD
5064 (let ((remove-re
5065 (if org-agenda-remove-timeranges-from-blocks
5066 (concat
5067 "<" (regexp-quote s1) ".*?>"
5068 "--"
5069 "<" (regexp-quote s2) ".*?>")
5070 nil)))
5071 (setq txt (org-format-agenda-item
5072 (format
5073 (nth (if (= d1 d2) 0 1)
5074 org-agenda-timerange-leaders)
5075 (1+ (- d0 d1)) (1+ (- d2 d1)))
5076 head category tags
5077 timestr nil remove-re))))
20908596
CD
5078 (org-add-props txt props
5079 'org-marker marker 'org-hd-marker hdmarker
5080 'type "block" 'date date
621f83e4 5081 'todo-state todo-state
20908596
CD
5082 'priority (org-get-priority txt) 'org-category category)
5083 (push txt ee)))
5084 (goto-char pos)))
5085 ;; Sort the entries by expiration date.
5086 (nreverse ee)))
5087
5088;;; Agenda presentation and sorting
5089
5090(defvar org-prefix-has-time nil
5091 "A flag, set by `org-compile-prefix-format'.
5092The flag is set if the currently compiled format contains a `%t'.")
5093(defvar org-prefix-has-tag nil
5094 "A flag, set by `org-compile-prefix-format'.
5095The flag is set if the currently compiled format contains a `%T'.")
5096(defvar org-prefix-has-effort nil
5097 "A flag, set by `org-compile-prefix-format'.
5098The flag is set if the currently compiled format contains a `%e'.")
8d642074 5099(defvar org-prefix-category-length nil
86fbb8ca 5100 "Used by `org-compile-prefix-format' to remember the category field width.")
8bfe682a 5101(defvar org-prefix-category-max-length nil
86fbb8ca 5102 "Used by `org-compile-prefix-format' to remember the category field width.")
20908596 5103
acedf35c
CD
5104(defun org-agenda-get-category-icon (category)
5105 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5106 (dolist (entry org-agenda-category-icon-alist)
5107 (when (org-string-match-p (car entry) category)
5108 (if (listp (cadr entry))
5109 (return (cadr entry))
5110 (return (apply 'create-image (cdr entry)))))))
5111
20908596 5112(defun org-format-agenda-item (extra txt &optional category tags dotime
8bfe682a 5113 noprefix remove-re habitp)
20908596
CD
5114 "Format TXT to be inserted into the agenda buffer.
5115In particular, it adds the prefix and corresponding text properties. EXTRA
5116must be a string and replaces the `%s' specifier in the prefix format.
5117CATEGORY (string, symbol or nil) may be used to overrule the default
5118category taken from local variable or file name. It will replace the `%c'
5119specifier in the format. DOTIME, when non-nil, indicates that a
5120time-of-day should be extracted from TXT for sorting of this entry, and for
5121the `%t' specifier in the format. When DOTIME is a string, this string is
5122searched for a time before TXT is. NOPREFIX is a flag and indicates that
5123only the correctly processes TXT should be returned - this is used by
5124`org-agenda-change-all-lines'. TAGS can be the tags of the headline.
5125Any match of REMOVE-RE will be removed from TXT."
5126 (save-match-data
5127 ;; Diary entries sometimes have extra whitespace at the beginning
5128 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5dec9555
CD
5129
5130 ;; Fix the tags part in txt
5131 (setq txt (org-agenda-fix-displayed-tags
5132 txt tags
5133 org-agenda-show-inherited-tags
5134 org-agenda-hide-tags-regexp))
20908596 5135 (let* ((category (or category
acedf35c
CD
5136 (if (stringp org-category)
5137 org-category
5138 (and org-category (symbol-name org-category)))
20908596
CD
5139 (if buffer-file-name
5140 (file-name-sans-extension
5141 (file-name-nondirectory buffer-file-name))
5142 "")))
acedf35c
CD
5143 (category-icon (org-agenda-get-category-icon category))
5144 (category-icon (if category-icon
5145 (propertize " " 'display category-icon)
5146 ""))
20908596
CD
5147 ;; time, tag, effort are needed for the eval of the prefix format
5148 (tag (if tags (nth (1- (length tags)) tags) ""))
5149 time effort neffort
c8d0cf5c
CD
5150 (ts (if dotime (concat
5151 (if (stringp dotime) dotime "")
5152 (and org-agenda-search-headline-for-time txt))))
20908596 5153 (time-of-day (and dotime (org-get-time-of-day ts)))
8d642074 5154 stamp plain s0 s1 s2 t1 t2 rtn srp l
8bfe682a 5155 duration thecategory)
20908596
CD
5156 (and (org-mode-p) buffer-file-name
5157 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5158 (when (and dotime time-of-day)
5159 ;; Extract starting and ending time and move them to prefix
5160 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5161 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5162 (setq s0 (match-string 0 ts)
5163 srp (and stamp (match-end 3))
5164 s1 (match-string (if plain 1 2) ts)
5165 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5166
5167 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5168 ;; them, we might want to remove them there to avoid duplication.
5169 ;; The user can turn this off with a variable.
5170 (if (and org-prefix-has-time
5171 org-agenda-remove-times-when-in-prefix (or stamp plain)
5172 (string-match (concat (regexp-quote s0) " *") txt)
5173 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5174 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5175 (= (match-beginning 0) 0)
5176 t))
5177 (setq txt (replace-match "" nil nil txt))))
5178 ;; Normalize the time(s) to 24 hour
5179 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5180 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5181 ;; Compute the duration
5182 (when s1
5183 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
5184 (string-to-number (substring s1 3)))
5185 t2 (cond
5186 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
5187 (string-to-number (substring s2 3))))
5188 (org-agenda-default-appointment-duration
5189 (+ t1 org-agenda-default-appointment-duration))
5190 (t nil)))
5191 (setq duration (if t2 (- t2 t1)))))
5192
5193 (when (and s1 (not s2) org-agenda-default-appointment-duration
5194 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
5195 (let ((m (+ (string-to-number (match-string 2 s1))
5196 (* 60 (string-to-number (match-string 1 s1)))
5197 org-agenda-default-appointment-duration))
5198 h)
5199 (setq h (/ m 60) m (- m (* h 60)))
5200 (setq s2 (format "%02d:%02d" h m))))
5201
afe98dfa 5202 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
20908596
CD
5203 txt)
5204 ;; Tags are in the string
5205 (if (or (eq org-agenda-remove-tags t)
5206 (and org-agenda-remove-tags
5207 org-prefix-has-tag))
5208 (setq txt (replace-match "" t t txt))
5209 (setq txt (replace-match
5210 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5211 (match-string 2 txt))
5212 t t txt))))
5213 (when (org-mode-p)
5214 (setq effort
5215 (condition-case nil
5216 (org-get-effort
5217 (or (get-text-property 0 'org-hd-marker txt)
5218 (get-text-property 0 'org-marker txt)))
5219 (error nil)))
5220 (when effort
5221 (setq neffort (org-hh:mm-string-to-minutes effort)
54a0dee5 5222 effort (setq effort (concat "[" effort "]" )))))
20908596
CD
5223
5224 (when remove-re
5225 (while (string-match remove-re txt)
5226 (setq txt (replace-match "" t t txt))))
5227
5228 ;; Create the final string
5229 (if noprefix
5230 (setq rtn txt)
5231 ;; Prepare the variables needed in the eval of the compiled format
acedf35c
CD
5232 (setq time (cond (s2 (concat
5233 (org-agenda-time-of-day-to-ampm-maybe s1)
5234 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5235 (if org-agenda-timegrid-use-ampm " ")))
5236 (s1 (concat
5237 (org-agenda-time-of-day-to-ampm-maybe s1)
5238 (if org-agenda-timegrid-use-ampm
5239 "........ "
5240 "......")))
20908596 5241 (t ""))
8bfe682a
CD
5242 extra (or (and (not habitp) extra) "")
5243 category (if (symbolp category) (symbol-name category) category)
5244 thecategory (copy-sequence category))
5245 (if (string-match org-bracket-link-regexp category)
5246 (progn
5247 (setq l (if (match-end 3)
5248 (- (match-end 3) (match-beginning 3))
5249 (- (match-end 1) (match-beginning 1))))
5250 (when (< l (or org-prefix-category-length 0))
5251 (setq category (copy-sequence category))
5252 (org-add-props category nil
5253 'extra-space (make-string
5254 (- org-prefix-category-length l 1) ?\ ))))
5255 (if (and org-prefix-category-max-length
5256 (>= (length category) org-prefix-category-max-length))
5257 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
20908596
CD
5258 ;; Evaluate the compiled format
5259 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
5260
5261 ;; And finally add the text properties
c8d0cf5c 5262 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
20908596 5263 (org-add-props rtn nil
8bfe682a 5264 'org-category (if thecategory (downcase thecategory) category)
ff4be292 5265 'tags (mapcar 'org-downcase-keep-props tags)
20908596
CD
5266 'org-highest-priority org-highest-priority
5267 'org-lowest-priority org-lowest-priority
5268 'prefix-length (- (length rtn) (length txt))
5269 'time-of-day time-of-day
5270 'duration duration
5271 'effort effort
5272 'effort-minutes neffort
5273 'txt txt
5274 'time time
5275 'extra extra
5276 'dotime dotime))))
5277
5dec9555
CD
5278(defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5279 "Remove tags string from TXT, and add a modified list of tags.
5280The modified list may contain inherited tags, and tags matched by
5281`org-agenda-hide-tags-regexp' will be removed."
5282 (when (or add-inherited hide-re)
afe98dfa 5283 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5dec9555 5284 (setq txt (substring txt 0 (match-beginning 0))))
ed21c5c8
CD
5285 (setq tags
5286 (delq nil
5287 (mapcar (lambda (tg)
5288 (if (or (and hide-re (string-match hide-re tg))
5289 (and (not add-inherited)
5290 (get-text-property 0 'inherited tg)))
5291 nil
5292 tg))
5293 tags)))
5dec9555 5294 (when tags
5dec9555
CD
5295 (let ((have-i (get-text-property 0 'inherited (car tags)))
5296 i)
5297 (setq txt (concat txt " :"
5298 (mapconcat
5299 (lambda (x)
5300 (setq i (get-text-property 0 'inherited x))
5301 (if (and have-i (not i))
5302 (progn
5303 (setq have-i nil)
5304 (concat ":" x))
5305 x))
5306 tags ":")
5307 (if have-i "::" ":"))))))
5308 txt)
ff4be292
CD
5309
5310(defun org-downcase-keep-props (s)
5311 (let ((props (text-properties-at 0 s)))
5312 (setq s (downcase s))
5313 (add-text-properties 0 (length s) props s)
5314 s))
5315
20908596
CD
5316(defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5317(defvar org-agenda-sorting-strategy-selected nil)
5318
5319(defun org-agenda-add-time-grid-maybe (list ndays todayp)
5320 (catch 'exit
5321 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5322 ((and todayp (member 'today (car org-agenda-time-grid))))
5323 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5324 ((member 'weekly (car org-agenda-time-grid)))
5325 (t (throw 'exit list)))
5326 (let* ((have (delq nil (mapcar
5327 (lambda (x) (get-text-property 1 'time-of-day x))
5328 list)))
5329 (string (nth 1 org-agenda-time-grid))
5330 (gridtimes (nth 2 org-agenda-time-grid))
5331 (req (car org-agenda-time-grid))
5332 (remove (member 'remove-match req))
5333 new time)
5334 (if (and (member 'require-timed req) (not have))
5335 ;; don't show empty grid
5336 (throw 'exit list))
5337 (while (setq time (pop gridtimes))
5338 (unless (and remove (member time have))
afe98dfa 5339 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
20908596
CD
5340 (push (org-format-agenda-item
5341 nil string "" nil
5342 (concat (substring time 0 -2) ":" (substring time -2)))
5343 new)
5344 (put-text-property
afe98dfa 5345 2 (length (car new)) 'face 'org-time-grid (car new))))
20908596
CD
5346 (if (member 'time-up org-agenda-sorting-strategy-selected)
5347 (append new list)
5348 (append list new)))))
5349
5350(defun org-compile-prefix-format (key)
5351 "Compile the prefix format into a Lisp form that can be evaluated.
5352The resulting form is returned and stored in the variable
5353`org-prefix-format-compiled'."
5354 (setq org-prefix-has-time nil org-prefix-has-tag nil
8d642074 5355 org-prefix-category-length nil org-prefix-has-effort nil)
20908596
CD
5356 (let ((s (cond
5357 ((stringp org-agenda-prefix-format)
5358 org-agenda-prefix-format)
5359 ((assq key org-agenda-prefix-format)
5360 (cdr (assq key org-agenda-prefix-format)))
5361 (t " %-12:c%?-12t% s")))
5362 (start 0)
5363 varform vars var e c f opt)
acedf35c 5364 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\)"
20908596
CD
5365 s start)
5366 (setq var (cdr (assoc (match-string 4 s)
5367 '(("c" . category) ("t" . time) ("s" . extra)
acedf35c 5368 ("i" . category-icon) ("T" . tag) ("e" . effort))))
20908596
CD
5369 c (or (match-string 3 s) "")
5370 opt (match-beginning 1)
5371 start (1+ (match-beginning 0)))
5372 (if (equal var 'time) (setq org-prefix-has-time t))
5373 (if (equal var 'tag) (setq org-prefix-has-tag t))
5374 (if (equal var 'effort) (setq org-prefix-has-effort t))
5375 (setq f (concat "%" (match-string 2 s) "s"))
8bfe682a
CD
5376 (when (equal var 'category)
5377 (setq org-prefix-category-length
5378 (floor (abs (string-to-number (match-string 2 s)))))
5379 (setq org-prefix-category-max-length
5380 (let ((x (match-string 2 s)))
5381 (save-match-data
5382 (if (string-match "\\.[0-9]+" x)
5383 (string-to-number (substring (match-string 0 x) 1)))))))
20908596
CD
5384 (if opt
5385 (setq varform
5386 `(if (equal "" ,var)
5387 ""
5388 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
8d642074 5389 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var))))))
20908596
CD
5390 (setq s (replace-match "%s" t nil s))
5391 (push varform vars))
5392 (setq vars (nreverse vars))
5393 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5394
5395(defun org-set-sorting-strategy (key)
5396 (if (symbolp (car org-agenda-sorting-strategy))
5397 ;; the old format
5398 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5399 (setq org-agenda-sorting-strategy-selected
5400 (or (cdr (assq key org-agenda-sorting-strategy))
5401 (cdr (assq 'agenda org-agenda-sorting-strategy))
5402 '(time-up category-keep priority-down)))))
5403
5404(defun org-get-time-of-day (s &optional string mod24)
5405 "Check string S for a time of day.
5406If found, return it as a military time number between 0 and 2400.
5407If not found, return nil.
5408The optional STRING argument forces conversion into a 5 character wide string
5409HH:MM."
5410 (save-match-data
5411 (when
5412 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5413 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5414 (let* ((h (string-to-number (match-string 1 s)))
5415 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5416 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5417 (am-p (equal ampm "am"))
5418 (h1 (cond ((not ampm) h)
5419 ((= h 12) (if am-p 0 12))
5420 (t (+ h (if am-p 0 12)))))
5421 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5422 (mod h1 24) h1))
5423 (t0 (+ (* 100 h2) m))
5424 (t1 (concat (if (>= h1 24) "+" " ")
ed21c5c8
CD
5425 (if (and org-agenda-time-leading-zero
5426 (< t0 1000)) "0" "")
20908596
CD
5427 (if (< t0 100) "0" "")
5428 (if (< t0 10) "0" "")
5429 (int-to-string t0))))
5430 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5431
afe98dfa
CD
5432(defvar org-agenda-before-sorting-filter-function nil
5433 "Function to be applied to agenda items prior to sorting.
5434Prior to sorting also means just before they are inserted into the agenda.
5435
5436To aid sorting, you may revisit the original entries and add more text
5437properties which will later be used by the sorting functions.
5438
5439The function should take a string argument, an agenda line.
5440It has access to the text properties in that line, which contain among
5441other things, the property `org-hd-marker' that points to the entry
5442where the line comes from. Note that not all lines going into the agenda
5443have this property, only most.
5444
5445The function should return the modified string. It is probably best
5446to ONLY change text properties.
5447
5448You can also use this function as a filter, by returning nil for lines
5449you don't want to have in the agenda at all. For this application, you
5450could bind the variable in the options section of a custom command.")
5451
20908596
CD
5452(defun org-finalize-agenda-entries (list &optional nosort)
5453 "Sort and concatenate the agenda items."
5454 (setq list (mapcar 'org-agenda-highlight-todo list))
5455 (if nosort
5456 list
afe98dfa
CD
5457 (when org-agenda-before-sorting-filter-function
5458 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
20908596
CD
5459 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5460
5461(defun org-agenda-highlight-todo (x)
621f83e4 5462 (let ((org-done-keywords org-done-keywords-for-agenda)
ed21c5c8 5463 (case-fold-search nil)
621f83e4 5464 re pl)
20908596
CD
5465 (if (eq x 'line)
5466 (save-excursion
5467 (beginning-of-line 1)
8d642074
CD
5468 (setq re (org-get-at-bol 'org-todo-regexp))
5469 (goto-char (+ (point) (or (org-get-at-bol 'prefix-length) 0)))
621f83e4 5470 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
c8d0cf5c 5471 (add-text-properties (match-beginning 0) (match-end 1)
621f83e4 5472 (list 'face (org-get-todo-face 1)))
20908596
CD
5473 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5474 (delete-region (match-beginning 1) (1- (match-end 0)))
5475 (goto-char (match-beginning 1))
5476 (insert (format org-agenda-todo-keyword-format s)))))
5477 (setq re (concat (get-text-property 0 'org-todo-regexp x))
5478 pl (get-text-property 0 'prefix-length x))
5479 (when (and re
5480 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5481 x (or pl 0)) pl))
5482 (add-text-properties
5483 (or (match-end 1) (match-end 0)) (match-end 0)
5484 (list 'face (org-get-todo-face (match-string 2 x)))
5485 x)
86fbb8ca
CD
5486 (when (match-end 1)
5487 (setq x (concat (substring x 0 (match-end 1))
5488 (format org-agenda-todo-keyword-format
5489 (match-string 2 x))
5490 (org-add-props " " (text-properties-at 0 x))
5491 (substring x (match-end 3))))))
20908596
CD
5492 x)))
5493
5494(defsubst org-cmp-priority (a b)
5495 "Compare the priorities of string A and B."
5496 (let ((pa (or (get-text-property 1 'priority a) 0))
5497 (pb (or (get-text-property 1 'priority b) 0)))
5498 (cond ((> pa pb) +1)
5499 ((< pa pb) -1)
5500 (t nil))))
5501
5502(defsubst org-cmp-effort (a b)
5503 "Compare the priorities of string A and B."
5504 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5505 (ea (or (get-text-property 1 'effort-minutes a) def))
5506 (eb (or (get-text-property 1 'effort-minutes b) def)))
5507 (cond ((> ea eb) +1)
5508 ((< ea eb) -1)
5509 (t nil))))
5510
5511(defsubst org-cmp-category (a b)
5512 "Compare the string values of categories of strings A and B."
5513 (let ((ca (or (get-text-property 1 'org-category a) ""))
5514 (cb (or (get-text-property 1 'org-category b) "")))
5515 (cond ((string-lessp ca cb) -1)
5516 ((string-lessp cb ca) +1)
5517 (t nil))))
5518
621f83e4
CD
5519(defsubst org-cmp-todo-state (a b)
5520 "Compare the todo states of strings A and B."
c8d0cf5c
CD
5521 (let* ((ma (or (get-text-property 1 'org-marker a)
5522 (get-text-property 1 'org-hd-marker a)))
5523 (mb (or (get-text-property 1 'org-marker b)
5524 (get-text-property 1 'org-hd-marker b)))
5525 (fa (and ma (marker-buffer ma)))
5526 (fb (and mb (marker-buffer mb)))
5527 (todo-kwds
5528 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5529 (and fb (with-current-buffer fb org-todo-keywords-1))))
5530 (ta (or (get-text-property 1 'todo-state a) ""))
621f83e4 5531 (tb (or (get-text-property 1 'todo-state b) ""))
c8d0cf5c
CD
5532 (la (- (length (member ta todo-kwds))))
5533 (lb (- (length (member tb todo-kwds))))
ff4be292 5534 (donepa (member ta org-done-keywords-for-agenda))
621f83e4
CD
5535 (donepb (member tb org-done-keywords-for-agenda)))
5536 (cond ((and donepa (not donepb)) -1)
5537 ((and (not donepa) donepb) +1)
5538 ((< la lb) -1)
5539 ((< lb la) +1)
5540 (t nil))))
5541
86fbb8ca
CD
5542(defsubst org-cmp-alpha (a b)
5543 "Compare the headlines, alphabetically."
5544 (let* ((pla (get-text-property 0 'prefix-length a))
5545 (plb (get-text-property 0 'prefix-length b))
5546 (ta (and pla (substring a pla)))
5547 (tb (and plb (substring b plb))))
5548 (when pla
5549 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5550 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5551 (setq ta (substring ta (match-end 0))))
5552 (setq ta (downcase ta)))
5553 (when plb
5554 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5555 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5556 (setq tb (substring tb (match-end 0))))
5557 (setq tb (downcase tb)))
5558 (cond ((not ta) +1)
5559 ((not tb) -1)
5560 ((string-lessp ta tb) -1)
5561 ((string-lessp tb ta) +1)
5562 (t nil))))
5563
20908596 5564(defsubst org-cmp-tag (a b)
71d35b24 5565 "Compare the string values of the first tags of A and B."
20908596
CD
5566 (let ((ta (car (last (get-text-property 1 'tags a))))
5567 (tb (car (last (get-text-property 1 'tags b)))))
5568 (cond ((not ta) +1)
5569 ((not tb) -1)
5570 ((string-lessp ta tb) -1)
5571 ((string-lessp tb ta) +1)
5572 (t nil))))
5573
5574(defsubst org-cmp-time (a b)
5575 "Compare the time-of-day values of strings A and B."
5576 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5577 (ta (or (get-text-property 1 'time-of-day a) def))
5578 (tb (or (get-text-property 1 'time-of-day b) def)))
5579 (cond ((< ta tb) -1)
5580 ((< tb ta) +1)
5581 (t nil))))
5582
8bfe682a
CD
5583(defsubst org-cmp-habit-p (a b)
5584 "Compare the todo states of strings A and B."
5585 (let ((ha (get-text-property 1 'org-habit-p a))
5586 (hb (get-text-property 1 'org-habit-p b)))
5587 (cond ((and ha (not hb)) -1)
5588 ((and (not ha) hb) +1)
5589 (t nil))))
5590
86fbb8ca
CD
5591(defsubst org-em (x y list) (or (memq x list) (memq y list)))
5592
20908596
CD
5593(defun org-entries-lessp (a b)
5594 "Predicate for sorting agenda entries."
5595 ;; The following variables will be used when the form is evaluated.
5596 ;; So even though the compiler complains, keep them.
86fbb8ca
CD
5597 (let* ((ss org-agenda-sorting-strategy-selected)
5598 (time-up (and (org-em 'time-up 'time-down ss)
5599 (org-cmp-time a b)))
5600 (time-down (if time-up (- time-up) nil))
5601 (priority-up (and (org-em 'priority-up 'priority-down ss)
5602 (org-cmp-priority a b)))
5603 (priority-down (if priority-up (- priority-up) nil))
5604 (effort-up (and (org-em 'effort-up 'effort-down ss)
5605 (org-cmp-effort a b)))
5606 (effort-down (if effort-up (- effort-up) nil))
5607 (category-up (and (or (org-em 'category-up 'category-down ss)
5608 (memq 'category-keep ss))
5609 (org-cmp-category a b)))
5610 (category-down (if category-up (- category-up) nil))
5611 (category-keep (if category-up +1 nil))
5612 (tag-up (and (org-em 'tag-up 'tag-down ss)
5613 (org-cmp-tag a b)))
5614 (tag-down (if tag-up (- tag-up) nil))
5615 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5616 (org-cmp-todo-state a b)))
c8d0cf5c 5617 (todo-state-down (if todo-state-up (- todo-state-up) nil))
86fbb8ca
CD
5618 (habit-up (and (org-em 'habit-up 'habit-down ss)
5619 (org-cmp-habit-p a b)))
5620 (habit-down (if habit-up (- habit-up) nil))
5621 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5622 (org-cmp-alpha a b)))
5623 (alpha-down (if alpha-up (- alpha-up) nil))
afe98dfa 5624 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
c8d0cf5c 5625 user-defined-up user-defined-down)
afe98dfa 5626 (if (and need-user-cmp org-agenda-cmp-user-defined
c8d0cf5c
CD
5627 (functionp org-agenda-cmp-user-defined))
5628 (setq user-defined-up
5629 (funcall org-agenda-cmp-user-defined a b)
5630 user-defined-down (if user-defined-up (- user-defined-up) nil)))
20908596
CD
5631 (cdr (assoc
5632 (eval (cons 'or org-agenda-sorting-strategy-selected))
5633 '((-1 . t) (1 . nil) (nil . nil))))))
5634
5635;;; Agenda restriction lock
5636
86fbb8ca 5637(defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
8bfe682a 5638 "Overlay to mark the headline to which agenda commands are restricted.")
86fbb8ca
CD
5639(overlay-put org-agenda-restriction-lock-overlay
5640 'face 'org-agenda-restriction-lock)
5641(overlay-put org-agenda-restriction-lock-overlay
5642 'help-echo "Agendas are currently limited to this subtree.")
20908596
CD
5643(org-detach-overlay org-agenda-restriction-lock-overlay)
5644
5645(defun org-agenda-set-restriction-lock (&optional type)
5646 "Set restriction lock for agenda, to current subtree or file.
5647Restriction will be the file if TYPE is `file', or if type is the
5648universal prefix '(4), or if the cursor is before the first headline
5649in the file. Otherwise, restriction will be to the current subtree."
5650 (interactive "P")
5651 (and (equal type '(4)) (setq type 'file))
5652 (setq type (cond
5653 (type type)
5654 ((org-at-heading-p) 'subtree)
5655 ((condition-case nil (org-back-to-heading t) (error nil))
5656 'subtree)
5657 (t 'file)))
5658 (if (eq type 'subtree)
5659 (progn
5660 (setq org-agenda-restrict t)
5661 (setq org-agenda-overriding-restriction 'subtree)
5662 (put 'org-agenda-files 'org-restrict
5663 (list (buffer-file-name (buffer-base-buffer))))
5664 (org-back-to-heading t)
86fbb8ca 5665 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
20908596
CD
5666 (move-marker org-agenda-restrict-begin (point))
5667 (move-marker org-agenda-restrict-end
5668 (save-excursion (org-end-of-subtree t)))
5669 (message "Locking agenda restriction to subtree"))
5670 (put 'org-agenda-files 'org-restrict
5671 (list (buffer-file-name (buffer-base-buffer))))
5672 (setq org-agenda-restrict nil)
5673 (setq org-agenda-overriding-restriction 'file)
5674 (move-marker org-agenda-restrict-begin nil)
5675 (move-marker org-agenda-restrict-end nil)
5676 (message "Locking agenda restriction to file"))
5677 (setq current-prefix-arg nil)
5678 (org-agenda-maybe-redo))
5679
5680(defun org-agenda-remove-restriction-lock (&optional noupdate)
5681 "Remove the agenda restriction lock."
5682 (interactive "P")
5683 (org-detach-overlay org-agenda-restriction-lock-overlay)
5684 (org-detach-overlay org-speedbar-restriction-lock-overlay)
5685 (setq org-agenda-overriding-restriction nil)
5686 (setq org-agenda-restrict nil)
5687 (put 'org-agenda-files 'org-restrict nil)
5688 (move-marker org-agenda-restrict-begin nil)
5689 (move-marker org-agenda-restrict-end nil)
5690 (setq current-prefix-arg nil)
5691 (message "Agenda restriction lock removed")
5692 (or noupdate (org-agenda-maybe-redo)))
5693
5694(defun org-agenda-maybe-redo ()
5695 "If there is any window showing the agenda view, update it."
5696 (let ((w (get-buffer-window org-agenda-buffer-name t))
5697 (w0 (selected-window)))
5698 (when w
5699 (select-window w)
5700 (org-agenda-redo)
5701 (select-window w0)
5702 (if org-agenda-overriding-restriction
5703 (message "Agenda view shifted to new %s restriction"
5704 org-agenda-overriding-restriction)
5705 (message "Agenda restriction lock removed")))))
5706
5707;;; Agenda commands
5708
5709(defun org-agenda-check-type (error &rest types)
5710 "Check if agenda buffer is of allowed type.
5711If ERROR is non-nil, throw an error, otherwise just return nil."
5712 (if (memq org-agenda-type types)
5713 t
5714 (if error
5715 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
5716 nil)))
5717
5718(defun org-agenda-quit ()
5719 "Exit agenda by removing the window or the buffer."
5720 (interactive)
5721 (if org-agenda-columns-active
5722 (org-columns-quit)
5723 (let ((buf (current-buffer)))
8d642074
CD
5724 (if (eq org-agenda-window-setup 'other-frame)
5725 (progn
5726 (kill-buffer buf)
5727 (org-agenda-reset-markers)
5728 (org-columns-remove-overlays)
5729 (setq org-agenda-archives-mode nil)
5730 (delete-frame))
5731 (and (not (eq org-agenda-window-setup 'current-window))
5732 (not (one-window-p))
5733 (delete-window))
5734 (kill-buffer buf)
5735 (org-agenda-reset-markers)
5736 (org-columns-remove-overlays)
5737 (setq org-agenda-archives-mode nil)))
20908596
CD
5738 ;; Maybe restore the pre-agenda window configuration.
5739 (and org-agenda-restore-windows-after-quit
5740 (not (eq org-agenda-window-setup 'other-frame))
5741 org-pre-agenda-window-conf
5742 (set-window-configuration org-pre-agenda-window-conf))))
5743
5744(defun org-agenda-exit ()
5745 "Exit agenda by removing the window or the buffer.
5746Also kill all Org-mode buffers which have been loaded by `org-agenda'.
5747Org-mode buffers visited directly by the user will not be touched."
5748 (interactive)
5749 (org-release-buffers org-agenda-new-buffers)
5750 (setq org-agenda-new-buffers nil)
5751 (org-agenda-quit))
5752
5753(defun org-agenda-execute (arg)
86fbb8ca
CD
5754 "Execute another agenda command, keeping same window.
5755So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
5756in the agenda."
20908596
CD
5757 (interactive "P")
5758 (let ((org-agenda-window-setup 'current-window))
5759 (org-agenda arg)))
5760
20908596
CD
5761(defun org-agenda-redo ()
5762 "Rebuild Agenda.
5763When this is the global TODO list, a prefix argument will be interpreted."
5764 (interactive)
5765 (let* ((org-agenda-keep-modes t)
71d35b24 5766 (filter org-agenda-filter)
c8d0cf5c 5767 (preset (get 'org-agenda-filter :preset-filter))
acedf35c 5768 (org-agenda-filter-while-redo (or filter preset))
20908596
CD
5769 (cols org-agenda-columns-active)
5770 (line (org-current-line))
5771 (window-line (- line (org-current-line (window-start))))
5772 (lprops (get 'org-agenda-redo-command 'org-lprops)))
c8d0cf5c 5773 (put 'org-agenda-filter :preset-filter nil)
20908596
CD
5774 (and cols (org-columns-quit))
5775 (message "Rebuilding agenda buffer...")
5776 (org-let lprops '(eval org-agenda-redo-command))
5777 (setq org-agenda-undo-list nil
5778 org-agenda-pending-undo-list nil)
5779 (message "Rebuilding agenda buffer...done")
c8d0cf5c
CD
5780 (put 'org-agenda-filter :preset-filter preset)
5781 (and (or filter preset) (org-agenda-filter-apply filter))
20908596 5782 (and cols (interactive-p) (org-agenda-columns))
54a0dee5 5783 (org-goto-line line)
20908596
CD
5784 (recenter window-line)))
5785
71d35b24 5786
621f83e4 5787(defvar org-global-tags-completion-table nil)
71d35b24
CD
5788(defvar org-agenda-filter-form nil)
5789(defun org-agenda-filter-by-tag (strip &optional char narrow)
621f83e4
CD
5790 "Keep only those lines in the agenda buffer that have a specific tag.
5791The tag is selected with its fast selection letter, as configured.
71d35b24
CD
5792With prefix argument STRIP, remove all lines that do have the tag.
5793A lisp caller can specify CHAR. NARROW means that the new tag should be
5794used to narrow the search - the interactive user can also press `-' or `+'
5795to switch to narrowing."
621f83e4 5796 (interactive "P")
71d35b24 5797 (let* ((alist org-tag-alist-for-agenda)
8bfe682a
CD
5798 (tag-chars (mapconcat
5799 (lambda (x) (if (and (not (symbolp (car x)))
5800 (cdr x))
5801 (char-to-string (cdr x))
5802 ""))
5803 alist ""))
5804 (efforts (org-split-string
5805 (or (cdr (assoc (concat org-effort-property "_ALL")
5806 org-global-properties))
5807 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
5808 (effort-op org-agenda-filter-effort-default-operator)
5809 (effort-prompt "")
5810 (inhibit-read-only t)
5811 (current org-agenda-filter)
ed21c5c8 5812 a n tag)
71d35b24 5813 (unless char
ff4be292 5814 (message
8bfe682a
CD
5815 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
5816 (if narrow "Narrow" "Filter") tag-chars
5817 (if org-agenda-auto-exclude-function "[RET], " ""))
71d35b24
CD
5818 (setq char (read-char)))
5819 (when (member char '(?+ ?-))
5820 ;; Narrowing down
5821 (cond ((equal char ?-) (setq strip t narrow t))
5822 ((equal char ?+) (setq strip nil narrow t)))
ff4be292 5823 (message
71d35b24
CD
5824 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
5825 (setq char (read-char)))
c8d0cf5c 5826 (when (member char '(?< ?> ?= ??))
71d35b24
CD
5827 ;; An effort operator
5828 (setq effort-op (char-to-string char))
71d35b24 5829 (setq alist nil) ; to make sure it will be interpreted as effort.
c8d0cf5c
CD
5830 (unless (equal char ??)
5831 (loop for i from 0 to 9 do
5832 (setq effort-prompt
5833 (concat
5834 effort-prompt " ["
5835 (if (= i 9) "0" (int-to-string (1+ i)))
5836 "]" (nth i efforts))))
5837 (message "Effort%s: %s " effort-op effort-prompt)
5838 (setq char (read-char))
5839 (when (or (< char ?0) (> char ?9))
5840 (error "Need 1-9,0 to select effort" ))))
71d35b24
CD
5841 (when (equal char ?\t)
5842 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
5843 (org-set-local 'org-global-tags-completion-table
5844 (org-global-tags-completion-table)))
5845 (let ((completion-ignore-case t))
54a0dee5 5846 (setq tag (org-icompleting-read
71d35b24
CD
5847 "Tag: " org-global-tags-completion-table))))
5848 (cond
8bfe682a
CD
5849 ((equal char ?\r)
5850 (org-agenda-filter-by-tag-show-all)
5851 (when org-agenda-auto-exclude-function
5852 (setq org-agenda-filter '())
ed21c5c8
CD
5853 (dolist (tag (org-agenda-get-represented-tags))
5854 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
8bfe682a
CD
5855 (if modifier
5856 (push modifier org-agenda-filter))))
5857 (if (not (null org-agenda-filter))
5858 (org-agenda-filter-apply org-agenda-filter))))
c8d0cf5c
CD
5859 ((equal char ?/)
5860 (org-agenda-filter-by-tag-show-all)
5861 (when (get 'org-agenda-filter :preset-filter)
5862 (org-agenda-filter-apply org-agenda-filter)))
71d35b24
CD
5863 ((or (equal char ?\ )
5864 (setq a (rassoc char alist))
5865 (and (>= char ?0) (<= char ?9)
5866 (setq n (if (= char ?0) 9 (- char ?0 1))
5867 tag (concat effort-op (nth n efforts))
5868 a (cons tag nil)))
c8d0cf5c
CD
5869 (and (= char ??)
5870 (setq tag "?eff")
5871 a (cons tag nil))
71d35b24
CD
5872 (and tag (setq a (cons tag nil))))
5873 (org-agenda-filter-by-tag-show-all)
5874 (setq tag (car a))
5875 (setq org-agenda-filter
5876 (cons (concat (if strip "-" "+") tag)
5877 (if narrow current nil)))
5878 (org-agenda-filter-apply org-agenda-filter))
5879 (t (error "Invalid tag selection character %c" char)))))
5880
ed21c5c8
CD
5881(defun org-agenda-get-represented-tags ()
5882 "Get a list of all tags currently represented in the agenda."
5883 (let (p tags)
5884 (save-excursion
5885 (goto-char (point-min))
5886 (while (setq p (next-single-property-change (point) 'tags))
5887 (goto-char p)
5888 (mapc (lambda (x) (add-to-list 'tags x))
5889 (get-text-property (point) 'tags))))
5890 tags))
5891
71d35b24
CD
5892(defun org-agenda-filter-by-tag-refine (strip &optional char)
5893 "Refine the current filter. See `org-agenda-filter-by-tag."
5894 (interactive "P")
5895 (org-agenda-filter-by-tag strip char 'refine))
5896
5897(defun org-agenda-filter-make-matcher ()
5898 "Create the form that tests a line for the agenda filter."
5899 (let (f f1)
c8d0cf5c
CD
5900 (dolist (x (append (get 'org-agenda-filter :preset-filter)
5901 org-agenda-filter))
71d35b24 5902 (if (member x '("-" "+"))
8bfe682a 5903 (setq f1 (if (equal x "-") 'tags '(not tags)))
c8d0cf5c 5904 (if (string-match "[<=>?]" x)
71d35b24
CD
5905 (setq f1 (org-agenda-filter-effort-form x))
5906 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
5907 (if (equal (string-to-char x) ?-)
5908 (setq f1 (list 'not f1))))
5909 (push f1 f))
5910 (cons 'and (nreverse f))))
5911
5912(defun org-agenda-filter-effort-form (e)
5913 "Return the form to compare the effort of the current line with what E says.
86fbb8ca 5914E looks like \"+<2:25\"."
71d35b24
CD
5915 (let (op)
5916 (setq e (substring e 1))
5917 (setq op (string-to-char e) e (substring e 1))
c8d0cf5c
CD
5918 (setq op (cond ((equal op ?<) '<=)
5919 ((equal op ?>) '>=)
5920 ((equal op ??) op)
5921 (t '=)))
71d35b24
CD
5922 (list 'org-agenda-compare-effort (list 'quote op)
5923 (org-hh:mm-string-to-minutes e))))
5924
5925(defun org-agenda-compare-effort (op value)
5926 "Compare the effort of the current line with VALUE, using OP.
5927If the line does not have an effort defined, return nil."
8d642074 5928 (let ((eff (org-get-at-bol 'effort-minutes)))
c8d0cf5c
CD
5929 (if (equal op ??)
5930 (not eff)
5931 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
5932 value))))
71d35b24
CD
5933
5934(defun org-agenda-filter-apply (filter)
5935 "Set FILTER as the new agenda filter and apply it."
5936 (let (tags)
5937 (setq org-agenda-filter filter
5938 org-agenda-filter-form (org-agenda-filter-make-matcher))
5939 (org-agenda-set-mode-name)
5940 (save-excursion
5941 (goto-char (point-min))
5942 (while (not (eobp))
8d642074 5943 (if (org-get-at-bol 'org-marker)
71d35b24 5944 (progn
8d642074 5945 (setq tags (org-get-at-bol 'tags)) ; used in eval
71d35b24
CD
5946 (if (not (eval org-agenda-filter-form))
5947 (org-agenda-filter-by-tag-hide-line))
5948 (beginning-of-line 2))
afe98dfa
CD
5949 (beginning-of-line 2))))
5950 (if (get-char-property (point) 'invisible)
5951 (org-agenda-previous-line))))
621f83e4 5952
621f83e4
CD
5953(defun org-agenda-filter-by-tag-hide-line ()
5954 (let (ov)
86fbb8ca 5955 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
621f83e4 5956 (point-at-eol)))
86fbb8ca
CD
5957 (overlay-put ov 'invisible t)
5958 (overlay-put ov 'type 'tags-filter)
621f83e4
CD
5959 (push ov org-agenda-filter-overlays)))
5960
71d35b24
CD
5961(defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
5962 (setq pos (or pos (point)))
5963 (save-excursion
86fbb8ca
CD
5964 (dolist (ov (overlays-at pos))
5965 (when (and (overlay-get ov 'invisible)
5966 (eq (overlay-get ov 'type) 'tags-filter))
71d35b24 5967 (goto-char pos)
86fbb8ca
CD
5968 (if (< (overlay-start ov) (point-at-eol))
5969 (move-overlay ov (point-at-eol)
5970 (overlay-end ov)))))))
71d35b24 5971
621f83e4 5972(defun org-agenda-filter-by-tag-show-all ()
86fbb8ca 5973 (mapc 'delete-overlay org-agenda-filter-overlays)
71d35b24
CD
5974 (setq org-agenda-filter-overlays nil)
5975 (setq org-agenda-filter nil)
5976 (setq org-agenda-filter-form nil)
5977 (org-agenda-set-mode-name))
621f83e4 5978
20908596
CD
5979(defun org-agenda-manipulate-query-add ()
5980 "Manipulate the query by adding a search term with positive selection.
ed21c5c8 5981Positive selection means the term must be matched for selection of an entry."
20908596
CD
5982 (interactive)
5983 (org-agenda-manipulate-query ?\[))
5984(defun org-agenda-manipulate-query-subtract ()
5985 "Manipulate the query by adding a search term with negative selection.
ed21c5c8 5986Negative selection means term must not be matched for selection of an entry."
20908596
CD
5987 (interactive)
5988 (org-agenda-manipulate-query ?\]))
5989(defun org-agenda-manipulate-query-add-re ()
5990 "Manipulate the query by adding a search regexp with positive selection.
ed21c5c8 5991Positive selection means the regexp must match for selection of an entry."
20908596
CD
5992 (interactive)
5993 (org-agenda-manipulate-query ?\{))
5994(defun org-agenda-manipulate-query-subtract-re ()
5995 "Manipulate the query by adding a search regexp with negative selection.
ed21c5c8 5996Negative selection means regexp must not match for selection of an entry."
20908596
CD
5997 (interactive)
5998 (org-agenda-manipulate-query ?\}))
5999(defun org-agenda-manipulate-query (char)
6000 (cond
6001 ((memq org-agenda-type '(timeline agenda))
54a0dee5
CD
6002 (let ((org-agenda-include-inactive-timestamps t))
6003 (org-agenda-redo))
6004 (message "Display now includes inactive timestamps as well"))
20908596
CD
6005 ((eq org-agenda-type 'search)
6006 (org-add-to-string
6007 'org-agenda-query-string
ed21c5c8
CD
6008 (if org-agenda-last-search-view-search-was-boolean
6009 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6010 (?\{ . " +{}") (?\} . " -{}"))))
6011 " "))
20908596
CD
6012 (setq org-agenda-redo-command
6013 (list 'org-search-view
6014 org-todo-only
6015 org-agenda-query-string
6016 (+ (length org-agenda-query-string)
6017 (if (member char '(?\{ ?\})) 0 1))))
6018 (set-register org-agenda-query-register org-agenda-query-string)
6019 (org-agenda-redo))
6020 (t (error "Cannot manipulate query for %s-type agenda buffers"
6021 org-agenda-type))))
6022
6023(defun org-add-to-string (var string)
6024 (set var (concat (symbol-value var) string)))
6025
6026(defun org-agenda-goto-date (date)
6027 "Jump to DATE in agenda."
afe98dfa
CD
6028 (interactive (list (let ((org-read-date-prefer-future
6029 (eval org-agenda-jump-prefer-future)))
6030 (org-read-date))))
20908596
CD
6031 (org-agenda-list nil date))
6032
6033(defun org-agenda-goto-today ()
6034 "Go to today."
6035 (interactive)
6036 (org-agenda-check-type t 'timeline 'agenda)
6037 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6038 (cond
6039 (tdpos (goto-char tdpos))
6040 ((eq org-agenda-type 'agenda)
acedf35c
CD
6041 (let* ((sd (org-agenda-compute-starting-span
6042 (org-today) (or org-agenda-ndays org-agenda-span)))
20908596 6043 (org-agenda-overriding-arguments org-agenda-last-arguments))
acedf35c 6044 (setf (nth 1 org-agenda-overriding-arguments) sd)
20908596
CD
6045 (org-agenda-redo)
6046 (org-agenda-find-same-or-today-or-agenda)))
6047 (t (error "Cannot find today")))))
6048
6049(defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6050 (goto-char
6051 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6052 (text-property-any (point-min) (point-max) 'org-today t)
6053 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6054 (point-min))))
6055
6056(defun org-agenda-later (arg)
6057 "Go forward in time by thee current span.
6058With prefix ARG, go forward that many times the current span."
6059 (interactive "p")
6060 (org-agenda-check-type t 'agenda)
acedf35c 6061 (let* ((span org-agenda-current-span)
20908596
CD
6062 (sd org-starting-day)
6063 (greg (calendar-gregorian-from-absolute sd))
8d642074 6064 (cnt (org-get-at-bol 'org-day-cnt))
acedf35c 6065 greg2)
20908596
CD
6066 (cond
6067 ((eq span 'day)
acedf35c 6068 (setq sd (+ arg sd)))
20908596 6069 ((eq span 'week)
acedf35c 6070 (setq sd (+ (* 7 arg) sd)))
20908596
CD
6071 ((eq span 'month)
6072 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6073 sd (calendar-absolute-from-gregorian greg2))
acedf35c 6074 (setcar greg2 (1+ (car greg2))))
20908596
CD
6075 ((eq span 'year)
6076 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6077 sd (calendar-absolute-from-gregorian greg2))
acedf35c
CD
6078 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6079 (t
6080 (setq sd (+ (* span arg) sd))))
20908596 6081 (let ((org-agenda-overriding-arguments
acedf35c 6082 (list (car org-agenda-last-arguments) sd span t)))
20908596
CD
6083 (org-agenda-redo)
6084 (org-agenda-find-same-or-today-or-agenda cnt))))
6085
6086(defun org-agenda-earlier (arg)
6087 "Go backward in time by the current span.
6088With prefix ARG, go backward that many times the current span."
6089 (interactive "p")
6090 (org-agenda-later (- arg)))
6091
c8d0cf5c
CD
6092(defun org-agenda-view-mode-dispatch ()
6093 "Call one of the view mode commands."
6094 (interactive)
ed21c5c8
CD
6095 (message "View: [d]ay [w]eek [m]onth [y]ear [q]uit/abort
6096 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [E]ntryText
6097 [a]rch-trees [A]rch-files clock[R]eport include[D]iary")
c8d0cf5c
CD
6098 (let ((a (read-char-exclusive)))
6099 (case a
6100 (?d (call-interactively 'org-agenda-day-view))
6101 (?w (call-interactively 'org-agenda-week-view))
6102 (?m (call-interactively 'org-agenda-month-view))
6103 (?y (call-interactively 'org-agenda-year-view))
6104 (?l (call-interactively 'org-agenda-log-mode))
ed21c5c8 6105 (?L (org-agenda-log-mode '(4)))
54a0dee5 6106 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
c8d0cf5c
CD
6107 (?a (call-interactively 'org-agenda-archives-mode))
6108 (?A (org-agenda-archives-mode 'files))
54a0dee5
CD
6109 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6110 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
c8d0cf5c
CD
6111 (?G (call-interactively 'org-agenda-toggle-time-grid))
6112 (?D (call-interactively 'org-agenda-toggle-diary))
ed21c5c8 6113 (?\! (call-interactively 'org-agenda-toggle-deadlines))
54a0dee5
CD
6114 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6115 (org-agenda-check-type t 'timeline 'agenda)
6116 (org-agenda-redo))
6117 (message "Display now includes inactive timestamps as well"))
c8d0cf5c
CD
6118 (?q (message "Abort"))
6119 (otherwise (error "Invalid key" )))))
6120
20908596
CD
6121(defun org-agenda-day-view (&optional day-of-year)
6122 "Switch to daily view for agenda.
6123With argument DAY-OF-YEAR, switch to that day of the year."
6124 (interactive "P")
20908596
CD
6125 (org-agenda-change-time-span 'day day-of-year))
6126(defun org-agenda-week-view (&optional iso-week)
6127 "Switch to daily view for agenda.
6128With argument ISO-WEEK, switch to the corresponding ISO week.
6129If ISO-WEEK has more then 2 digits, only the last two encode the
6130week. Any digits before this encode a year. So 200712 means
6131week 12 of year 2007. Years in the range 1938-2037 can also be
6132written as 2-digit years."
6133 (interactive "P")
20908596
CD
6134 (org-agenda-change-time-span 'week iso-week))
6135(defun org-agenda-month-view (&optional month)
b349f79f 6136 "Switch to monthly view for agenda.
20908596
CD
6137With argument MONTH, switch to that month."
6138 (interactive "P")
6139 (org-agenda-change-time-span 'month month))
6140(defun org-agenda-year-view (&optional year)
b349f79f 6141 "Switch to yearly view for agenda.
20908596
CD
6142With argument YEAR, switch to that year.
6143If MONTH has more then 2 digits, only the last two encode the
6144month. Any digits before this encode a year. So 200712 means
6145December year 2007. Years in the range 1938-2037 can also be
6146written as 2-digit years."
6147 (interactive "P")
6148 (when year
6149 (setq year (org-small-year-to-year year)))
6150 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6151 (org-agenda-change-time-span 'year year)
6152 (error "Abort")))
6153
6154(defun org-agenda-change-time-span (span &optional n)
6155 "Change the agenda view to SPAN.
6156SPAN may be `day', `week', `month', `year'."
6157 (org-agenda-check-type t 'agenda)
acedf35c 6158 (if (and (not n) (equal org-agenda-current-span span))
20908596 6159 (error "Viewing span is already \"%s\"" span))
8d642074 6160 (let* ((sd (or (org-get-at-bol 'day)
20908596 6161 org-starting-day))
acedf35c 6162 (sd (org-agenda-compute-starting-span sd span n))
20908596 6163 (org-agenda-overriding-arguments
acedf35c 6164 (list (car org-agenda-last-arguments) sd span t)))
20908596
CD
6165 (org-agenda-redo)
6166 (org-agenda-find-same-or-today-or-agenda))
6167 (org-agenda-set-mode-name)
6168 (message "Switched to %s view" span))
6169
acedf35c
CD
6170(defun org-agenda-compute-starting-span (sd span &optional n)
6171 "Compute starting date for agenda.
20908596
CD
6172SPAN may be `day', `week', `month', `year'. The return value
6173is a cons cell with the starting date and the number of days,
6174so that the date SD will be in that range."
6175 (let* ((greg (calendar-gregorian-from-absolute sd))
6176 (dg (nth 1 greg))
6177 (mg (car greg))
acedf35c 6178 (yg (nth 2 greg)))
20908596
CD
6179 (cond
6180 ((eq span 'day)
6181 (when n
6182 (setq sd (+ (calendar-absolute-from-gregorian
6183 (list mg 1 yg))
acedf35c 6184 n -1))))
20908596
CD
6185 ((eq span 'week)
6186 (let* ((nt (calendar-day-of-week
6187 (calendar-gregorian-from-absolute sd)))
6188 (d (if org-agenda-start-on-weekday
6189 (- nt org-agenda-start-on-weekday)
acedf35c
CD
6190 0))
6191 y1)
20908596
CD
6192 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6193 (when n
6194 (require 'cal-iso)
20908596
CD
6195 (when (> n 99)
6196 (setq y1 (org-small-year-to-year (/ n 100))
6197 n (mod n 100)))
6198 (setq sd
6199 (calendar-absolute-from-iso
6200 (list n 1
acedf35c 6201 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
20908596 6202 ((eq span 'month)
acedf35c
CD
6203 (let (y1)
6204 (when (and n (> n 99))
6205 (setq y1 (org-small-year-to-year (/ n 100))
6206 n (mod n 100)))
6207 (setq sd (calendar-absolute-from-gregorian
6208 (list (or n mg) 1 (or y1 yg))))))
20908596
CD
6209 ((eq span 'year)
6210 (setq sd (calendar-absolute-from-gregorian
acedf35c
CD
6211 (list 1 1 (or n yg))))))
6212 sd))
20908596
CD
6213
6214(defun org-agenda-next-date-line (&optional arg)
6215 "Jump to the next line indicating a date in agenda buffer."
6216 (interactive "p")
6217 (org-agenda-check-type t 'agenda 'timeline)
6218 (beginning-of-line 1)
6219 ;; This does not work if user makes date format that starts with a blank
6220 (if (looking-at "^\\S-") (forward-char 1))
6221 (if (not (re-search-forward "^\\S-" nil t arg))
6222 (progn
6223 (backward-char 1)
6224 (error "No next date after this line in this buffer")))
6225 (goto-char (match-beginning 0)))
6226
6227(defun org-agenda-previous-date-line (&optional arg)
6228 "Jump to the previous line indicating a date in agenda buffer."
6229 (interactive "p")
6230 (org-agenda-check-type t 'agenda 'timeline)
6231 (beginning-of-line 1)
6232 (if (not (re-search-backward "^\\S-" nil t arg))
6233 (error "No previous date before this line in this buffer")))
6234
6235;; Initialize the highlight
86fbb8ca
CD
6236(defvar org-hl (make-overlay 1 1))
6237(overlay-put org-hl 'face 'highlight)
20908596
CD
6238
6239(defun org-highlight (begin end &optional buffer)
6240 "Highlight a region with overlay."
86fbb8ca 6241 (move-overlay org-hl begin end (or buffer (current-buffer))))
20908596
CD
6242
6243(defun org-unhighlight ()
6244 "Detach overlay INDEX."
86fbb8ca 6245 (org-detach-overlay org-hl))
20908596
CD
6246
6247;; FIXME this is currently not used.
6248(defun org-highlight-until-next-command (beg end &optional buffer)
6249 "Move the highlight overlay to BEG/END, remove it before the next command."
6250 (org-highlight beg end buffer)
6251 (add-hook 'pre-command-hook 'org-unhighlight-once))
6252(defun org-unhighlight-once ()
6253 "Remove the highlight from its position, and this function from the hook."
6254 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6255 (org-unhighlight))
6256
6257(defun org-agenda-follow-mode ()
6258 "Toggle follow mode in an agenda buffer."
6259 (interactive)
6260 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6261 (org-agenda-set-mode-name)
8bfe682a
CD
6262 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6263 (org-agenda-show))
20908596
CD
6264 (message "Follow mode is %s"
6265 (if org-agenda-follow-mode "on" "off")))
6266
54a0dee5
CD
6267(defun org-agenda-entry-text-mode (&optional arg)
6268 "Toggle entry text mode in an agenda buffer."
6269 (interactive "P")
365f8d85
SM
6270 (setq org-agenda-entry-text-mode (or (integerp arg)
6271 (not org-agenda-entry-text-mode)))
54a0dee5
CD
6272 (org-agenda-entry-text-hide)
6273 (and org-agenda-entry-text-mode
6274 (let ((org-agenda-entry-text-maxlines
6275 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6276 (org-agenda-entry-text-show)))
6277 (org-agenda-set-mode-name)
6278 (message "Entry text mode is %s. Maximum number of lines is %d"
6279 (if org-agenda-entry-text-mode "on" "off")
6280 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6281
acedf35c
CD
6282(defun org-agenda-clockreport-mode (&optional with-filter)
6283 "Toggle clocktable mode in an agenda buffer.
6284With prefix arg WITH-FILTER, make the clocktable respect the current
6285agenda filter."
6286 (interactive "P")
20908596 6287 (org-agenda-check-type t 'agenda)
acedf35c
CD
6288 (if with-filter
6289 (setq org-agenda-clockreport-mode 'with-filter)
6290 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
20908596
CD
6291 (org-agenda-set-mode-name)
6292 (org-agenda-redo)
6293 (message "Clocktable mode is %s"
6294 (if org-agenda-clockreport-mode "on" "off")))
6295
93b62de8
CD
6296(defun org-agenda-log-mode (&optional special)
6297 "Toggle log mode in an agenda buffer.
6298With argument SPECIAL, show all possible log items, not only the ones
6299configured in `org-agenda-log-mode-items'.
6300With a double `C-u' prefix arg, show *only* log items, nothing else."
6301 (interactive "P")
20908596 6302 (org-agenda-check-type t 'agenda 'timeline)
93b62de8
CD
6303 (setq org-agenda-show-log
6304 (if (equal special '(16))
6305 'only
6306 (if special '(closed clock state)
6307 (not org-agenda-show-log))))
20908596
CD
6308 (org-agenda-set-mode-name)
6309 (org-agenda-redo)
6310 (message "Log mode is %s"
6311 (if org-agenda-show-log "on" "off")))
6312
2c3ad40d 6313(defun org-agenda-archives-mode (&optional with-files)
c8d0cf5c
CD
6314 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6315When called with a prefix argument, include all archive files as well."
2c3ad40d
CD
6316 (interactive "P")
6317 (setq org-agenda-archives-mode
6318 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6319 (org-agenda-set-mode-name)
6320 (org-agenda-redo)
6321 (message
6322 "%s"
6323 (cond
6324 ((eq org-agenda-archives-mode nil)
6325 "No archives are included")
6326 ((eq org-agenda-archives-mode 'trees)
6327 (format "Trees with :%s: tag are included" org-archive-tag))
6328 ((eq org-agenda-archives-mode t)
6329 (format "Trees with :%s: tag and all active archive files are included"
6330 org-archive-tag)))))
6331
20908596
CD
6332(defun org-agenda-toggle-diary ()
6333 "Toggle diary inclusion in an agenda buffer."
6334 (interactive)
6335 (org-agenda-check-type t 'agenda)
6336 (setq org-agenda-include-diary (not org-agenda-include-diary))
6337 (org-agenda-redo)
6338 (org-agenda-set-mode-name)
6339 (message "Diary inclusion turned %s"
6340 (if org-agenda-include-diary "on" "off")))
6341
ed21c5c8 6342(defun org-agenda-toggle-deadlines ()
acedf35c 6343 "Toggle inclusion of entries with a deadline in an agenda buffer."
ed21c5c8
CD
6344 (interactive)
6345 (org-agenda-check-type t 'agenda)
6346 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6347 (org-agenda-redo)
6348 (org-agenda-set-mode-name)
6349 (message "Deadlines inclusion turned %s"
6350 (if org-agenda-include-deadlines "on" "off")))
6351
20908596
CD
6352(defun org-agenda-toggle-time-grid ()
6353 "Toggle time grid in an agenda buffer."
6354 (interactive)
6355 (org-agenda-check-type t 'agenda)
6356 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6357 (org-agenda-redo)
6358 (org-agenda-set-mode-name)
6359 (message "Time-grid turned %s"
6360 (if org-agenda-use-time-grid "on" "off")))
6361
6362(defun org-agenda-set-mode-name ()
6363 "Set the mode name to indicate all the small mode settings."
6364 (setq mode-name
acedf35c
CD
6365 (list "Org-Agenda"
6366 (if (get 'org-agenda-files 'org-restrict) " []" "")
6367 " "
6368 '(:eval (org-agenda-span-name org-agenda-current-span))
6369 (if org-agenda-follow-mode " Follow" "")
6370 (if org-agenda-entry-text-mode " ETxt" "")
6371 (if org-agenda-include-diary " Diary" "")
6372 (if org-agenda-include-deadlines " Ddl" "")
6373 (if org-agenda-use-time-grid " Grid" "")
6374 (if (and (boundp 'org-habit-show-habits)
6375 org-habit-show-habits) " Habit" "")
6376 (if (consp org-agenda-show-log) " LogAll"
6377 (if org-agenda-show-log " Log" ""))
6378 (if (or org-agenda-filter (get 'org-agenda-filter
6379 :preset-filter))
6380 (concat " {" (mapconcat
6381 'identity
6382 (append (get 'org-agenda-filter
6383 :preset-filter)
6384 org-agenda-filter) "") "}")
6385 "")
6386 (if org-agenda-archives-mode
6387 (if (eq org-agenda-archives-mode t)
6388 " Archives"
6389 (format " :%s:" org-archive-tag))
6390 "")
6391 (if org-agenda-clockreport-mode
6392 (if (eq org-agenda-clockreport-mode 'with-filter)
6393 " Clock{}" " Clock")
6394 "")))
20908596
CD
6395 (force-mode-line-update))
6396
6397(defun org-agenda-post-command-hook ()
b349f79f
CD
6398 (setq org-agenda-type
6399 (or (get-text-property (point) 'org-agenda-type)
6400 (get-text-property (max (point-min) (1- (point)))
8bfe682a
CD
6401 'org-agenda-type))))
6402
6403(defun org-agenda-next-line ()
86fbb8ca 6404 "Move cursor to the next line, and show if follow mode is active."
8bfe682a
CD
6405 (interactive)
6406 (call-interactively 'next-line)
1bcdebed
CD
6407 (org-agenda-do-context-action))
6408
8bfe682a
CD
6409(defun org-agenda-previous-line ()
6410 "Move cursor to the previous line, and show if follow-mode is active."
8bfe682a
CD
6411 (interactive)
6412 (call-interactively 'previous-line)
1bcdebed
CD
6413 (org-agenda-do-context-action))
6414
6415(defun org-agenda-do-context-action ()
86fbb8ca 6416 "Show outline path and, maybe, follow mode window."
1bcdebed
CD
6417 (let ((m (org-get-at-bol 'org-marker)))
6418 (if (and org-agenda-follow-mode m)
6419 (org-agenda-show))
6420 (if (and m org-agenda-show-outline-path)
5dec9555
CD
6421 (org-with-point-at m
6422 (org-display-outline-path t)))))
20908596
CD
6423
6424(defun org-agenda-show-priority ()
6425 "Show the priority of the current item.
6426This priority is composed of the main priority given with the [#A] cookies,
6427and by additional input from the age of a schedules or deadline entry."
6428 (interactive)
8d642074 6429 (let* ((pri (org-get-at-bol 'priority)))
20908596
CD
6430 (message "Priority is %d" (if pri pri -1000))))
6431
6432(defun org-agenda-show-tags ()
6433 "Show the tags applicable to the current item."
6434 (interactive)
8d642074 6435 (let* ((tags (org-get-at-bol 'tags)))
20908596
CD
6436 (if tags
6437 (message "Tags are :%s:"
6438 (org-no-properties (mapconcat 'identity tags ":")))
6439 (message "No tags associated with this line"))))
6440
6441(defun org-agenda-goto (&optional highlight)
6442 "Go to the Org-mode file which contains the item at point."
6443 (interactive)
8d642074 6444 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6445 (org-agenda-error)))
6446 (buffer (marker-buffer marker))
6447 (pos (marker-position marker)))
6448 (switch-to-buffer-other-window buffer)
6449 (widen)
86fbb8ca 6450 (push-mark)
20908596
CD
6451 (goto-char pos)
6452 (when (org-mode-p)
6453 (org-show-context 'agenda)
6454 (save-excursion
6455 (and (outline-next-heading)
6456 (org-flag-heading nil)))) ; show the next heading
6457 (recenter (/ (window-height) 2))
6458 (run-hooks 'org-agenda-after-show-hook)
6459 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6460
6461(defvar org-agenda-after-show-hook nil
6462 "Normal hook run after an item has been shown from the agenda.
6463Point is in the buffer where the item originated.")
6464
6465(defun org-agenda-kill ()
6466 "Kill the entry or subtree belonging to the current agenda entry."
6467 (interactive)
6468 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 6469 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6470 (org-agenda-error)))
6471 (buffer (marker-buffer marker))
6472 (pos (marker-position marker))
8d642074 6473 (type (org-get-at-bol 'type))
20908596
CD
6474 dbeg dend (n 0) conf)
6475 (org-with-remote-undo buffer
6476 (with-current-buffer buffer
6477 (save-excursion
6478 (goto-char pos)
6479 (if (and (org-mode-p) (not (member type '("sexp"))))
6480 (setq dbeg (progn (org-back-to-heading t) (point))
6481 dend (org-end-of-subtree t t))
6482 (setq dbeg (point-at-bol)
6483 dend (min (point-max) (1+ (point-at-eol)))))
6484 (goto-char dbeg)
6485 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6486 (setq conf (or (eq t org-agenda-confirm-kill)
6487 (and (numberp org-agenda-confirm-kill)
6488 (> n org-agenda-confirm-kill))))
6489 (and conf
6490 (not (y-or-n-p
6491 (format "Delete entry with %d lines in buffer \"%s\"? "
6492 n (buffer-name buffer))))
6493 (error "Abort"))
6494 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6495 (with-current-buffer buffer (delete-region dbeg dend))
6496 (message "Agenda item and source killed"))))
6497
8bfe682a
CD
6498(defvar org-archive-default-command)
6499(defun org-agenda-archive-default ()
6500 "Archive the entry or subtree belonging to the current agenda entry."
6501 (interactive)
6502 (require 'org-archive)
6503 (org-agenda-archive-with org-archive-default-command))
6504
6505(defun org-agenda-archive-default-with-confirmation ()
6506 "Archive the entry or subtree belonging to the current agenda entry."
6507 (interactive)
6508 (require 'org-archive)
6509 (org-agenda-archive-with org-archive-default-command 'confirm))
6510
20908596
CD
6511(defun org-agenda-archive ()
6512 "Archive the entry or subtree belonging to the current agenda entry."
6513 (interactive)
8bfe682a 6514 (org-agenda-archive-with 'org-archive-subtree))
20908596
CD
6515
6516(defun org-agenda-archive-to-archive-sibling ()
8bfe682a
CD
6517 "Move the entry to the archive sibling."
6518 (interactive)
6519 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6520
6521(defun org-agenda-archive-with (cmd &optional confirm)
20908596
CD
6522 "Move the entry to the archive sibling."
6523 (interactive)
6524 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 6525 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6526 (org-agenda-error)))
6527 (buffer (marker-buffer marker))
6528 (pos (marker-position marker)))
6529 (org-with-remote-undo buffer
6530 (with-current-buffer buffer
6531 (if (org-mode-p)
8bfe682a
CD
6532 (if (and confirm
6533 (not (y-or-n-p "Archive this subtree or entry? ")))
6534 (error "Abort")
6535 (save-excursion
6536 (goto-char pos)
6537 (org-remove-subtree-entries-from-agenda)
6538 (org-back-to-heading t)
6539 (funcall cmd)))
20908596
CD
6540 (error "Archiving works only in Org-mode files"))))))
6541
6542(defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6543 "Remove all lines in the agenda that correspond to a given subtree.
6544The subtree is the one in buffer BUF, starting at BEG and ending at END.
6545If this information is not given, the function uses the tree at point."
6546 (let ((buf (or buf (current-buffer))) m p)
6547 (save-excursion
6548 (unless (and beg end)
6549 (org-back-to-heading t)
6550 (setq beg (point))
6551 (org-end-of-subtree t)
6552 (setq end (point)))
6553 (set-buffer (get-buffer org-agenda-buffer-name))
6554 (save-excursion
6555 (goto-char (point-max))
6556 (beginning-of-line 1)
6557 (while (not (bobp))
8d642074 6558 (when (and (setq m (org-get-at-bol 'org-marker))
20908596
CD
6559 (equal buf (marker-buffer m))
6560 (setq p (marker-position m))
6561 (>= p beg)
c8d0cf5c 6562 (< p end))
20908596
CD
6563 (let ((inhibit-read-only t))
6564 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6565 (beginning-of-line 0))))))
6566
86fbb8ca 6567(defun org-agenda-refile (&optional goto rfloc no-update)
c8d0cf5c 6568 "Refile the item at point."
54a0dee5
CD
6569 (interactive "P")
6570 (if (equal goto '(16))
6571 (org-refile-goto-last-stored)
8d642074 6572 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
6573 (org-agenda-error)))
6574 (buffer (marker-buffer marker))
6575 (pos (marker-position marker))
6576 (rfloc (or rfloc
6577 (org-refile-get-location
6578 (if goto "Goto: " "Refile to: ") buffer
6579 org-refile-allow-creating-parent-nodes))))
6580 (with-current-buffer buffer
6581 (save-excursion
6582 (save-restriction
6583 (widen)
6584 (goto-char marker)
6585 (org-remove-subtree-entries-from-agenda)
86fbb8ca
CD
6586 (org-refile goto buffer rfloc)))))
6587 (unless no-update (org-agenda-redo))))
54a0dee5
CD
6588
6589(defun org-agenda-open-link (&optional arg)
6590 "Follow the link in the current line, if any.
8bfe682a 6591This looks for a link in the displayed line in the agenda. It also looks
54a0dee5 6592at the text of the entry itself."
c8d0cf5c 6593 (interactive "P")
8d642074
CD
6594 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6595 (org-get-at-bol 'org-marker)))
6596 (buffer (and marker (marker-buffer marker)))
6597 (prefix (buffer-substring
6598 (point-at-bol)
6599 (+ (point-at-bol)
8bfe682a
CD
6600 (or (org-get-at-bol 'prefix-length) 0)))))
6601 (cond
6602 (buffer
6603 (with-current-buffer buffer
6604 (save-excursion
6605 (save-restriction
6606 (widen)
6607 (goto-char marker)
6608 (org-offer-links-in-entry arg prefix)))))
6609 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6610 (save-excursion
6611 (beginning-of-line 1)
6612 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6613 (org-open-link-from-string (match-string 1)))
6614 (t (error "No link to open here")))))
20908596
CD
6615
6616(defun org-agenda-copy-local-variable (var)
6617 "Get a variable from a referenced buffer and install it here."
8d642074 6618 (let ((m (org-get-at-bol 'org-marker)))
20908596
CD
6619 (when (and m (buffer-live-p (marker-buffer m)))
6620 (org-set-local var (with-current-buffer (marker-buffer m)
6621 (symbol-value var))))))
6622
6623(defun org-agenda-switch-to (&optional delete-other-windows)
6624 "Go to the Org-mode file which contains the item at point."
6625 (interactive)
8bfe682a
CD
6626 (if (and org-return-follows-link
6627 (not (org-get-at-bol 'org-marker))
6628 (org-in-regexp org-bracket-link-regexp))
6629 (org-open-link-from-string (match-string 0))
6630 (let* ((marker (or (org-get-at-bol 'org-marker)
6631 (org-agenda-error)))
6632 (buffer (marker-buffer marker))
6633 (pos (marker-position marker)))
6634 (switch-to-buffer buffer)
6635 (and delete-other-windows (delete-other-windows))
6636 (widen)
6637 (goto-char pos)
6638 (when (org-mode-p)
6639 (org-show-context 'agenda)
6640 (save-excursion
6641 (and (outline-next-heading)
6642 (org-flag-heading nil))))))) ; show the next heading
20908596
CD
6643
6644(defun org-agenda-goto-mouse (ev)
6645 "Go to the Org-mode file which contains the item at the mouse click."
6646 (interactive "e")
6647 (mouse-set-point ev)
6648 (org-agenda-goto))
6649
fdf730ed
CD
6650(defun org-agenda-show (&optional full-entry)
6651 "Display the Org-mode file which contains the item at point.
6652With prefix argument FULL-ENTRY, make the entire entry visible
6653if it was hidden in the outline."
6654 (interactive "P")
20908596 6655 (let ((win (selected-window)))
fdf730ed
CD
6656 (if full-entry
6657 (let ((org-show-entry-below t))
6658 (org-agenda-goto t))
6659 (org-agenda-goto t))
20908596
CD
6660 (select-window win)))
6661
8bfe682a
CD
6662(defvar org-agenda-show-window nil)
6663(defun org-agenda-show-and-scroll-up ()
6664 "Display the Org-mode file which contains the item at point.
6665When called repeatedly, scroll the window that is displaying the buffer."
6666 (interactive)
6667 (let ((win (selected-window)))
6668 (if (and (window-live-p org-agenda-show-window)
6669 (eq this-command last-command))
6670 (progn
6671 (select-window org-agenda-show-window)
6672 (ignore-errors (scroll-up)))
6673 (org-agenda-goto t)
6674 (show-subtree)
6675 (setq org-agenda-show-window (selected-window)))
6676 (select-window win)))
6677
6678(defun org-agenda-show-scroll-down ()
6679 "Scroll down the window showing the agenda."
6680 (interactive)
6681 (let ((win (selected-window)))
6682 (when (window-live-p org-agenda-show-window)
6683 (select-window org-agenda-show-window)
6684 (ignore-errors (scroll-down))
6685 (select-window win))))
6686
c8d0cf5c
CD
6687(defun org-agenda-show-1 (&optional more)
6688 "Display the Org-mode file which contains the item at point.
8bfe682a 6689The prefix arg selects the amount of information to display:
c8d0cf5c
CD
6690
66910 hide the subtree
66921 just show the entry according to defaults.
54a0dee5
CD
66932 show the children view
66943 show the subtree view
c8d0cf5c
CD
66954 show the entire subtree and any LOGBOOK drawers
66965 show the entire subtree and any drawers
6697With prefix argument FULL-ENTRY, make the entire entry visible
6698if it was hidden in the outline."
6699 (interactive "p")
6700 (let ((win (selected-window)))
6701 (org-agenda-goto t)
6702 (org-recenter-heading 1)
6703 (cond
6704 ((= more 0)
6705 (hide-subtree)
54a0dee5
CD
6706 (save-excursion
6707 (org-back-to-heading)
6708 (run-hook-with-args 'org-cycle-hook 'folded))
6709 (message "Remote: FOLDED"))
c8d0cf5c
CD
6710 ((and (interactive-p) (= more 1))
6711 (message "Remote: show with default settings"))
6712 ((= more 2)
6713 (show-entry)
54a0dee5 6714 (show-children)
c8d0cf5c
CD
6715 (save-excursion
6716 (org-back-to-heading)
54a0dee5
CD
6717 (run-hook-with-args 'org-cycle-hook 'children))
6718 (message "Remote: CHILDREN"))
c8d0cf5c
CD
6719 ((= more 3)
6720 (show-subtree)
6721 (save-excursion
6722 (org-back-to-heading)
54a0dee5
CD
6723 (run-hook-with-args 'org-cycle-hook 'subtree))
6724 (message "Remote: SUBTREE"))
c8d0cf5c
CD
6725 ((= more 4)
6726 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
6727 (org-drawer-regexp
6728 (concat "^[ \t]*:\\("
6729 (mapconcat 'regexp-quote org-drawers "\\|")
6730 "\\):[ \t]*$")))
6731 (show-subtree)
6732 (save-excursion
6733 (org-back-to-heading)
6734 (org-cycle-hide-drawers 'subtree)))
54a0dee5 6735 (message "Remote: SUBTREE AND LOGBOOK"))
c8d0cf5c
CD
6736 ((> more 4)
6737 (show-subtree)
54a0dee5 6738 (message "Remote: SUBTREE AND ALL DRAWERS")))
c8d0cf5c
CD
6739 (select-window win)))
6740
6741(defun org-recenter-heading (n)
6742 (save-excursion
6743 (org-back-to-heading)
6744 (recenter n)))
6745
6746(defvar org-agenda-cycle-counter nil)
54a0dee5 6747(defun org-agenda-cycle-show (&optional n)
c8d0cf5c
CD
6748 "Show the current entry in another window, with default settings.
6749Default settings are taken from `org-show-hierarchy-above' and siblings.
54a0dee5 6750When use repeatedly in immediate succession, the remote entry will cycle
c8d0cf5c
CD
6751through visibility
6752
54a0dee5
CD
6753children -> subtree -> folded
6754
6755When called with a numeric prefix arg, that arg will be passed through to
6756`org-agenda-show-1'. For the interpretation of that argument, see the
6757docstring of `org-agenda-show-1'."
6758 (interactive "P")
6759 (if (integerp n)
6760 (setq org-agenda-cycle-counter n)
6761 (if (not (eq last-command this-command))
6762 (setq org-agenda-cycle-counter 1)
6763 (if (equal org-agenda-cycle-counter 0)
6764 (setq org-agenda-cycle-counter 2)
6765 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
6766 (if (> org-agenda-cycle-counter 3)
6767 (setq org-agenda-cycle-counter 0)))))
c8d0cf5c
CD
6768 (org-agenda-show-1 org-agenda-cycle-counter))
6769
20908596
CD
6770(defun org-agenda-recenter (arg)
6771 "Display the Org-mode file which contains the item at point and recenter."
6772 (interactive "P")
6773 (let ((win (selected-window)))
6774 (org-agenda-goto t)
6775 (recenter arg)
6776 (select-window win)))
6777
6778(defun org-agenda-show-mouse (ev)
6779 "Display the Org-mode file which contains the item at the mouse click."
6780 (interactive "e")
6781 (mouse-set-point ev)
6782 (org-agenda-show))
6783
6784(defun org-agenda-check-no-diary ()
6785 "Check if the entry is a diary link and abort if yes."
8d642074 6786 (if (org-get-at-bol 'org-agenda-diary-link)
20908596
CD
6787 (org-agenda-error)))
6788
6789(defun org-agenda-error ()
6790 (error "Command not allowed in this line"))
6791
6792(defun org-agenda-tree-to-indirect-buffer ()
6793 "Show the subtree corresponding to the current entry in an indirect buffer.
6794This calls the command `org-tree-to-indirect-buffer' from the original
6795Org-mode buffer.
6796With numerical prefix arg ARG, go up to this level and then take that tree.
86fbb8ca
CD
6797With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
6798use the dedicated frame)."
20908596
CD
6799 (interactive)
6800 (org-agenda-check-no-diary)
8d642074 6801 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6802 (org-agenda-error)))
6803 (buffer (marker-buffer marker))
6804 (pos (marker-position marker)))
6805 (with-current-buffer buffer
6806 (save-excursion
6807 (goto-char pos)
6808 (call-interactively 'org-tree-to-indirect-buffer)))))
6809
6810(defvar org-last-heading-marker (make-marker)
6811 "Marker pointing to the headline that last changed its TODO state
6812by a remote command from the agenda.")
6813
6814(defun org-agenda-todo-nextset ()
6815 "Switch TODO entry to next sequence."
6816 (interactive)
6817 (org-agenda-todo 'nextset))
6818
6819(defun org-agenda-todo-previousset ()
6820 "Switch TODO entry to previous sequence."
6821 (interactive)
6822 (org-agenda-todo 'previousset))
6823
6824(defun org-agenda-todo (&optional arg)
6825 "Cycle TODO state of line at point, also in Org-mode file.
6826This changes the line at point, all other lines in the agenda referring to
6827the same tree node, and the headline of the tree node in the Org-mode file."
6828 (interactive "P")
6829 (org-agenda-check-no-diary)
6830 (let* ((col (current-column))
8d642074 6831 (marker (or (org-get-at-bol 'org-marker)
20908596
CD
6832 (org-agenda-error)))
6833 (buffer (marker-buffer marker))
6834 (pos (marker-position marker))
8d642074 6835 (hdmarker (org-get-at-bol 'org-hd-marker))
acedf35c 6836 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
20908596 6837 (inhibit-read-only t)
93b62de8 6838 org-agenda-headline-snapshot-before-repeat newhead just-one)
20908596
CD
6839 (org-with-remote-undo buffer
6840 (with-current-buffer buffer
6841 (widen)
6842 (goto-char pos)
6843 (org-show-context 'agenda)
6844 (save-excursion
6845 (and (outline-next-heading)
6846 (org-flag-heading nil))) ; show the next heading
a2a2e7fb
CD
6847 (let ((current-prefix-arg arg))
6848 (call-interactively 'org-todo))
20908596
CD
6849 (and (bolp) (forward-char 1))
6850 (setq newhead (org-get-heading))
93b62de8
CD
6851 (when (and (org-bound-and-true-p
6852 org-agenda-headline-snapshot-before-repeat)
6853 (not (equal org-agenda-headline-snapshot-before-repeat
6854 newhead))
6855 todayp)
6856 (setq newhead org-agenda-headline-snapshot-before-repeat
6857 just-one t))
20908596
CD
6858 (save-excursion
6859 (org-back-to-heading)
6860 (move-marker org-last-heading-marker (point))))
6861 (beginning-of-line 1)
6862 (save-excursion
93b62de8 6863 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
20908596
CD
6864 (org-move-to-column col))))
6865
6866(defun org-agenda-add-note (&optional arg)
6867 "Add a time-stamped note to the entry at point."
6868 (interactive "P")
6869 (org-agenda-check-no-diary)
8d642074 6870 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6871 (org-agenda-error)))
6872 (buffer (marker-buffer marker))
6873 (pos (marker-position marker))
8d642074 6874 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
6875 (inhibit-read-only t))
6876 (with-current-buffer buffer
6877 (widen)
6878 (goto-char pos)
6879 (org-show-context 'agenda)
6880 (save-excursion
6881 (and (outline-next-heading)
6882 (org-flag-heading nil))) ; show the next heading
6883 (org-add-note))))
6884
db55f368 6885(defun org-agenda-change-all-lines (newhead hdmarker
4ed008de 6886 &optional fixface just-this)
20908596
CD
6887 "Change all lines in the agenda buffer which match HDMARKER.
6888The new content of the line will be NEWHEAD (as modified by
6889`org-format-agenda-item'). HDMARKER is checked with
6890`equal' against all `org-hd-marker' text properties in the file.
33306645 6891If FIXFACE is non-nil, the face of each item is modified according to
db55f368
CD
6892the new TODO state.
6893If JUST-THIS is non-nil, change just the current line, not all.
33306645 6894If FORCE-TAGS is non nil, the car of it returns the new tags."
20908596 6895 (let* ((inhibit-read-only t)
93b62de8 6896 (line (org-current-line))
fdf730ed 6897 (thetags (with-current-buffer (marker-buffer hdmarker)
4ed008de
CD
6898 (save-excursion (save-restriction (widen)
6899 (goto-char hdmarker)
fdf730ed 6900 (org-get-tags-at)))))
20908596
CD
6901 props m pl undone-face done-face finish new dotime cat tags)
6902 (save-excursion
6903 (goto-char (point-max))
6904 (beginning-of-line 1)
6905 (while (not finish)
6906 (setq finish (bobp))
8d642074 6907 (when (and (setq m (org-get-at-bol 'org-hd-marker))
93b62de8 6908 (or (not just-this) (= (org-current-line) line))
20908596
CD
6909 (equal m hdmarker))
6910 (setq props (text-properties-at (point))
8d642074
CD
6911 dotime (org-get-at-bol 'dotime)
6912 cat (org-get-at-bol 'org-category)
4ed008de 6913 tags thetags
20908596 6914 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
8d642074
CD
6915 pl (org-get-at-bol 'prefix-length)
6916 undone-face (org-get-at-bol 'undone-face)
6917 done-face (org-get-at-bol 'done-face))
6918 (goto-char (+ (point) pl))
6919 ;; (org-move-to-column pl) FIXME: does the above line work correctly?
20908596
CD
6920 (cond
6921 ((equal new "")
6922 (beginning-of-line 1)
6923 (and (looking-at ".*\n?") (replace-match "")))
6924 ((looking-at ".*")
6925 (replace-match new t t)
6926 (beginning-of-line 1)
6927 (add-text-properties (point-at-bol) (point-at-eol) props)
6928 (when fixface
6929 (add-text-properties
6930 (point-at-bol) (point-at-eol)
6931 (list 'face
6932 (if org-last-todo-state-is-todo
6933 undone-face done-face))))
6934 (org-agenda-highlight-todo 'line)
6935 (beginning-of-line 1))
6936 (t (error "Line update did not work"))))
6937 (beginning-of-line 0)))
6938 (org-finalize-agenda)))
6939
6940(defun org-agenda-align-tags (&optional line)
6941 "Align all tags in agenda items to `org-agenda-tags-column'."
6942 (let ((inhibit-read-only t) l c)
6943 (save-excursion
6944 (goto-char (if line (point-at-bol) (point-min)))
afe98dfa 6945 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
20908596
CD
6946 (if line (point-at-eol) nil) t)
6947 (add-text-properties
6948 (match-beginning 2) (match-end 2)
30ab4580
GM
6949 (list 'face (delq nil (let ((prop (get-text-property
6950 (match-beginning 2) 'face)))
6951 (or (listp prop) (setq prop (list prop)))
6952 (if (memq 'org-tag prop)
6953 prop
6954 (cons 'org-tag prop))))))
20908596
CD
6955 (setq l (- (match-end 2) (match-beginning 2))
6956 c (if (< org-agenda-tags-column 0)
6957 (- (abs org-agenda-tags-column) l)
6958 org-agenda-tags-column))
6959 (delete-region (match-beginning 1) (match-end 1))
6960 (goto-char (match-beginning 1))
6961 (insert (org-add-props
6962 (make-string (max 1 (- c (current-column))) ?\ )
ed21c5c8
CD
6963 (plist-put (copy-sequence (text-properties-at (point)))
6964 'face nil))))
ff4be292
CD
6965 (goto-char (point-min))
6966 (org-font-lock-add-tag-faces (point-max)))))
20908596
CD
6967
6968(defun org-agenda-priority-up ()
6969 "Increase the priority of line at point, also in Org-mode file."
6970 (interactive)
6971 (org-agenda-priority 'up))
6972
6973(defun org-agenda-priority-down ()
6974 "Decrease the priority of line at point, also in Org-mode file."
6975 (interactive)
6976 (org-agenda-priority 'down))
6977
6978(defun org-agenda-priority (&optional force-direction)
6979 "Set the priority of line at point, also in Org-mode file.
6980This changes the line at point, all other lines in the agenda referring to
6981the same tree node, and the headline of the tree node in the Org-mode file."
6982 (interactive)
c8d0cf5c
CD
6983 (unless org-enable-priority-commands
6984 (error "Priority commands are disabled"))
20908596 6985 (org-agenda-check-no-diary)
8d642074 6986 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 6987 (org-agenda-error)))
8d642074 6988 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
6989 (buffer (marker-buffer hdmarker))
6990 (pos (marker-position hdmarker))
6991 (inhibit-read-only t)
6992 newhead)
6993 (org-with-remote-undo buffer
6994 (with-current-buffer buffer
6995 (widen)
6996 (goto-char pos)
6997 (org-show-context 'agenda)
6998 (save-excursion
6999 (and (outline-next-heading)
7000 (org-flag-heading nil))) ; show the next heading
7001 (funcall 'org-priority force-direction)
7002 (end-of-line 1)
7003 (setq newhead (org-get-heading)))
7004 (org-agenda-change-all-lines newhead hdmarker)
7005 (beginning-of-line 1))))
7006
7007;; FIXME: should fix the tags property of the agenda line.
c8d0cf5c 7008(defun org-agenda-set-tags (&optional tag onoff)
20908596
CD
7009 "Set tags for the current headline."
7010 (interactive)
7011 (org-agenda-check-no-diary)
7012 (if (and (org-region-active-p) (interactive-p))
7013 (call-interactively 'org-change-tag-in-region)
7014 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 7015 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
7016 (org-agenda-error)))
7017 (buffer (marker-buffer hdmarker))
7018 (pos (marker-position hdmarker))
7019 (inhibit-read-only t)
4ed008de 7020 newhead)
20908596
CD
7021 (org-with-remote-undo buffer
7022 (with-current-buffer buffer
7023 (widen)
7024 (goto-char pos)
7025 (save-excursion
7026 (org-show-context 'agenda))
7027 (save-excursion
7028 (and (outline-next-heading)
7029 (org-flag-heading nil))) ; show the next heading
7030 (goto-char pos)
c8d0cf5c
CD
7031 (if tag
7032 (org-toggle-tag tag onoff)
7033 (call-interactively 'org-set-tags))
20908596
CD
7034 (end-of-line 1)
7035 (setq newhead (org-get-heading)))
4ed008de 7036 (org-agenda-change-all-lines newhead hdmarker)
20908596
CD
7037 (beginning-of-line 1)))))
7038
54a0dee5
CD
7039(defun org-agenda-set-property ()
7040 "Set a property for the current headline."
7041 (interactive)
7042 (org-agenda-check-no-diary)
7043 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 7044 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
7045 (org-agenda-error)))
7046 (buffer (marker-buffer hdmarker))
7047 (pos (marker-position hdmarker))
7048 (inhibit-read-only t)
7049 newhead)
7050 (org-with-remote-undo buffer
7051 (with-current-buffer buffer
7052 (widen)
7053 (goto-char pos)
7054 (save-excursion
7055 (org-show-context 'agenda))
7056 (save-excursion
7057 (and (outline-next-heading)
7058 (org-flag-heading nil))) ; show the next heading
7059 (goto-char pos)
7060 (call-interactively 'org-set-property)))))
7061
7062(defun org-agenda-set-effort ()
7063 "Set the effort property for the current headline."
7064 (interactive)
7065 (org-agenda-check-no-diary)
7066 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 7067 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
7068 (org-agenda-error)))
7069 (buffer (marker-buffer hdmarker))
7070 (pos (marker-position hdmarker))
7071 (inhibit-read-only t)
7072 newhead)
7073 (org-with-remote-undo buffer
7074 (with-current-buffer buffer
7075 (widen)
7076 (goto-char pos)
7077 (save-excursion
7078 (org-show-context 'agenda))
7079 (save-excursion
7080 (and (outline-next-heading)
7081 (org-flag-heading nil))) ; show the next heading
7082 (goto-char pos)
7083 (call-interactively 'org-set-effort)
7084 (end-of-line 1)))))
7085
20908596
CD
7086(defun org-agenda-toggle-archive-tag ()
7087 "Toggle the archive tag for the current entry."
7088 (interactive)
7089 (org-agenda-check-no-diary)
7090 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
8d642074 7091 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
7092 (org-agenda-error)))
7093 (buffer (marker-buffer hdmarker))
7094 (pos (marker-position hdmarker))
7095 (inhibit-read-only t)
7096 newhead)
7097 (org-with-remote-undo buffer
7098 (with-current-buffer buffer
7099 (widen)
7100 (goto-char pos)
7101 (org-show-context 'agenda)
7102 (save-excursion
7103 (and (outline-next-heading)
7104 (org-flag-heading nil))) ; show the next heading
7105 (call-interactively 'org-toggle-archive-tag)
7106 (end-of-line 1)
7107 (setq newhead (org-get-heading)))
7108 (org-agenda-change-all-lines newhead hdmarker)
7109 (beginning-of-line 1))))
7110
c8d0cf5c
CD
7111(defun org-agenda-do-date-later (arg)
7112 (interactive "P")
7113 (cond
7114 ((or (equal arg '(16))
7115 (memq last-command
7116 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7117 (setq this-command 'org-agenda-date-later-minutes)
7118 (org-agenda-date-later-minutes 1))
7119 ((or (equal arg '(4))
7120 (memq last-command
7121 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7122 (setq this-command 'org-agenda-date-later-hours)
7123 (org-agenda-date-later-hours 1))
7124 (t
7125 (org-agenda-date-later (prefix-numeric-value arg)))))
7126
7127(defun org-agenda-do-date-earlier (arg)
7128 (interactive "P")
7129 (cond
7130 ((or (equal arg '(16))
7131 (memq last-command
7132 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7133 (setq this-command 'org-agenda-date-earlier-minutes)
7134 (org-agenda-date-earlier-minutes 1))
7135 ((or (equal arg '(4))
7136 (memq last-command
7137 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7138 (setq this-command 'org-agenda-date-earlier-hours)
7139 (org-agenda-date-earlier-hours 1))
7140 (t
7141 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7142
20908596
CD
7143(defun org-agenda-date-later (arg &optional what)
7144 "Change the date of this item to one day later."
7145 (interactive "p")
7146 (org-agenda-check-type t 'agenda 'timeline)
7147 (org-agenda-check-no-diary)
8d642074 7148 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7149 (org-agenda-error)))
7150 (buffer (marker-buffer marker))
7151 (pos (marker-position marker)))
7152 (org-with-remote-undo buffer
7153 (with-current-buffer buffer
7154 (widen)
7155 (goto-char pos)
7156 (if (not (org-at-timestamp-p))
7157 (error "Cannot find time stamp"))
7158 (org-timestamp-change arg (or what 'day)))
7159 (org-agenda-show-new-time marker org-last-changed-timestamp))
7160 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7161
7162(defun org-agenda-date-earlier (arg &optional what)
7163 "Change the date of this item to one day earlier."
7164 (interactive "p")
7165 (org-agenda-date-later (- arg) what))
7166
c8d0cf5c
CD
7167(defun org-agenda-date-later-minutes (arg)
7168 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7169 (interactive "p")
7170 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7171 (org-agenda-date-later arg 'minute))
7172
7173(defun org-agenda-date-earlier-minutes (arg)
7174 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7175 (interactive "p")
7176 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7177 (org-agenda-date-earlier arg 'minute))
7178
7179(defun org-agenda-date-later-hours (arg)
7180 "Change the time of this item, in hour steps."
7181 (interactive "p")
7182 (org-agenda-date-later arg 'hour))
7183
7184(defun org-agenda-date-earlier-hours (arg)
7185 "Change the time of this item, in hour steps."
7186 (interactive "p")
7187 (org-agenda-date-earlier arg 'hour))
7188
20908596
CD
7189(defun org-agenda-show-new-time (marker stamp &optional prefix)
7190 "Show new date stamp via text properties."
7191 ;; We use text properties to make this undoable
71d35b24
CD
7192 (let ((inhibit-read-only t)
7193 (buffer-invisibility-spec))
20908596
CD
7194 (setq stamp (concat " " prefix " => " stamp))
7195 (save-excursion
7196 (goto-char (point-max))
7197 (while (not (bobp))
8d642074 7198 (when (equal marker (org-get-at-bol 'org-marker))
20908596 7199 (org-move-to-column (- (window-width) (length stamp)) t)
71d35b24 7200 (org-agenda-fix-tags-filter-overlays-at (point))
20908596
CD
7201 (if (featurep 'xemacs)
7202 ;; Use `duplicable' property to trigger undo recording
7203 (let ((ex (make-extent nil nil))
7204 (gl (make-glyph stamp)))
7205 (set-glyph-face gl 'secondary-selection)
7206 (set-extent-properties
7207 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7208 (insert-extent ex (1- (point)) (point-at-eol)))
7209 (add-text-properties
7210 (1- (point)) (point-at-eol)
7211 (list 'display (org-add-props stamp nil
7212 'face 'secondary-selection))))
7213 (beginning-of-line 1))
7214 (beginning-of-line 0)))))
7215
7216(defun org-agenda-date-prompt (arg)
7217 "Change the date of this item. Date is prompted for, with default today.
7218The prefix ARG is passed to the `org-time-stamp' command and can therefore
7219be used to request time specification in the time stamp."
7220 (interactive "P")
7221 (org-agenda-check-type t 'agenda 'timeline)
7222 (org-agenda-check-no-diary)
8d642074 7223 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7224 (org-agenda-error)))
7225 (buffer (marker-buffer marker))
7226 (pos (marker-position marker)))
7227 (org-with-remote-undo buffer
7228 (with-current-buffer buffer
7229 (widen)
7230 (goto-char pos)
ed21c5c8 7231 (if (not (org-at-timestamp-p t))
20908596 7232 (error "Cannot find time stamp"))
ed21c5c8 7233 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8d642074
CD
7234 (org-agenda-show-new-time marker org-last-changed-timestamp))
7235 (message "Time stamp changed to %s" org-last-changed-timestamp)))
20908596
CD
7236
7237(defun org-agenda-schedule (arg)
ed21c5c8
CD
7238 "Schedule the item at point.
7239Arg is passed through to `org-schedule'."
20908596
CD
7240 (interactive "P")
7241 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7242 (org-agenda-check-no-diary)
8d642074 7243 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7244 (org-agenda-error)))
7245 (type (marker-insertion-type marker))
7246 (buffer (marker-buffer marker))
7247 (pos (marker-position marker))
7248 (org-insert-labeled-timestamps-at-point nil)
7249 ts)
20908596
CD
7250 (set-marker-insertion-type marker t)
7251 (org-with-remote-undo buffer
7252 (with-current-buffer buffer
7253 (widen)
7254 (goto-char pos)
7255 (setq ts (org-schedule arg)))
7256 (org-agenda-show-new-time marker ts "S"))
7257 (message "Item scheduled for %s" ts)))
7258
7259(defun org-agenda-deadline (arg)
ed21c5c8
CD
7260 "Schedule the item at point.
7261Arg is passed through to `org-deadline'."
20908596
CD
7262 (interactive "P")
7263 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7264 (org-agenda-check-no-diary)
8d642074 7265 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7266 (org-agenda-error)))
7267 (buffer (marker-buffer marker))
7268 (pos (marker-position marker))
7269 (org-insert-labeled-timestamps-at-point nil)
7270 ts)
7271 (org-with-remote-undo buffer
7272 (with-current-buffer buffer
7273 (widen)
7274 (goto-char pos)
7275 (setq ts (org-deadline arg)))
8d642074 7276 (org-agenda-show-new-time marker ts "D"))
20908596
CD
7277 (message "Deadline for this item set to %s" ts)))
7278
b349f79f
CD
7279(defun org-agenda-action ()
7280 "Select entry for agenda action, or execute an agenda action.
7281This command prompts for another letter. Valid inputs are:
7282
7283m Mark the entry at point for an agenda action
7284s Schedule the marked entry to the date at the cursor
7285d Set the deadline of the marked entry to the date at the cursor
7286r Call `org-remember' with cursor date as the default date
86fbb8ca 7287c Call `org-capture' with cursor date as the default date
b349f79f
CD
7288SPC Show marked entry in other window
7289TAB Visit marked entry in other window
7290
7291The cursor may be at a date in the calendar, or in the Org agenda."
7292 (interactive)
65c439fd 7293 (let (ans)
86fbb8ca 7294 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
b349f79f
CD
7295 (setq ans (read-char-exclusive))
7296 (cond
7297 ((equal ans ?m)
7298 ;; Mark this entry
7299 (if (eq major-mode 'org-agenda-mode)
8d642074
CD
7300 (let ((m (or (org-get-at-bol 'org-hd-marker)
7301 (org-get-at-bol 'org-marker))))
b349f79f
CD
7302 (if m
7303 (progn
7304 (move-marker org-agenda-action-marker
7305 (marker-position m) (marker-buffer m))
7306 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7307 (error "Don't know which entry to mark")))
7308 (error "This command works only in the agenda")))
7309 ((equal ans ?s)
7310 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7311 ((equal ans ?d)
7312 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7313 ((equal ans ?r)
7314 (org-agenda-do-action '(org-remember) t))
86fbb8ca
CD
7315 ((equal ans ?c)
7316 (org-agenda-do-action '(org-capture) t))
b349f79f
CD
7317 ((equal ans ?\ )
7318 (let ((cw (selected-window)))
7319 (org-switch-to-buffer-other-window
7320 (marker-buffer org-agenda-action-marker))
7321 (goto-char org-agenda-action-marker)
7322 (org-show-context 'agenda)
7323 (select-window cw)))
7324 ((equal ans ?\C-i)
7325 (org-switch-to-buffer-other-window
7326 (marker-buffer org-agenda-action-marker))
7327 (goto-char org-agenda-action-marker)
7328 (org-show-context 'agenda))
7329 (t (error "Invalid agenda action %c" ans)))))
7330
7331(defun org-agenda-do-action (form &optional current-buffer)
7332 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7333 (let ((org-overriding-default-time (org-get-cursor-date)))
7334 (if current-buffer
7335 (eval form)
7336 (if (not (marker-buffer org-agenda-action-marker))
8bfe682a 7337 (error "No entry has been selected for agenda action")
b349f79f
CD
7338 (with-current-buffer (marker-buffer org-agenda-action-marker)
7339 (save-excursion
7340 (save-restriction
7341 (widen)
7342 (goto-char org-agenda-action-marker)
7343 (eval form))))))))
ff4be292 7344
20908596
CD
7345(defun org-agenda-clock-in (&optional arg)
7346 "Start the clock on the currently selected item."
7347 (interactive "P")
7348 (org-agenda-check-no-diary)
7349 (if (equal arg '(4))
7350 (org-clock-in arg)
8d642074 7351 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 7352 (org-agenda-error)))
8d642074 7353 (hdmarker (or (org-get-at-bol 'org-hd-marker)
b349f79f
CD
7354 marker))
7355 (pos (marker-position marker))
7356 newhead)
20908596
CD
7357 (org-with-remote-undo (marker-buffer marker)
7358 (with-current-buffer (marker-buffer marker)
7359 (widen)
7360 (goto-char pos)
b349f79f
CD
7361 (org-show-context 'agenda)
7362 (org-show-entry)
7363 (org-cycle-hide-drawers 'children)
7364 (org-clock-in arg)
7365 (setq newhead (org-get-heading)))
c8d0cf5c 7366 (org-agenda-change-all-lines newhead hdmarker)))))
20908596 7367
afe98dfa 7368(defun org-agenda-clock-out ()
20908596 7369 "Stop the currently running clock."
afe98dfa 7370 (interactive)
20908596
CD
7371 (unless (marker-buffer org-clock-marker)
7372 (error "No running clock"))
c8d0cf5c
CD
7373 (let ((marker (make-marker)) newhead)
7374 (org-with-remote-undo (marker-buffer org-clock-marker)
7375 (with-current-buffer (marker-buffer org-clock-marker)
7376 (save-excursion
7377 (save-restriction
7378 (widen)
7379 (goto-char org-clock-marker)
7380 (org-back-to-heading t)
7381 (move-marker marker (point))
7382 (org-clock-out)
7383 (setq newhead (org-get-heading))))))
7384 (org-agenda-change-all-lines newhead marker)
7385 (move-marker marker nil)))
20908596
CD
7386
7387(defun org-agenda-clock-cancel (&optional arg)
7388 "Cancel the currently running clock."
7389 (interactive "P")
7390 (unless (marker-buffer org-clock-marker)
7391 (error "No running clock"))
7392 (org-with-remote-undo (marker-buffer org-clock-marker)
7393 (org-clock-cancel)))
7394
afe98dfa
CD
7395(defun org-agenda-clock-goto ()
7396 "Jump to the currently clocked in task within the agenda.
7397If the currently clocked in task is not listed in the agenda
7398buffer, display it in another window."
7399 (interactive)
7400 (let (pos)
7401 (mapc (lambda (o)
7402 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7403 (setq pos (overlay-start o))))
7404 (overlays-in (point-min) (point-max)))
7405 (cond (pos (goto-char pos))
7406 ;; If the currently clocked entry is not in the agenda
7407 ;; buffer, we visit it in another window:
7408 (org-clock-current-task
7409 (org-switch-to-buffer-other-window (org-clock-goto)))
7410 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7411
8bfe682a
CD
7412(defun org-agenda-diary-entry-in-org-file ()
7413 "Make a diary entry in the file `org-agenda-diary-file'."
5dec9555 7414 (let (d1 d2 char (text "") dp1 dp2)
8bfe682a
CD
7415 (if (equal (buffer-name) "*Calendar*")
7416 (setq d1 (calendar-cursor-to-date t)
7417 d2 (car calendar-mark-ring))
5dec9555
CD
7418 (setq dp1 (get-text-property (point-at-bol) 'day))
7419 (unless dp1 (error "No date defined in current line"))
7420 (setq d1 (calendar-gregorian-from-absolute dp1)
7421 d2 (and (ignore-errors (mark))
7422 (save-excursion
7423 (goto-char (mark))
7424 (setq dp2 (get-text-property (point-at-bol) 'day)))
7425 (calendar-gregorian-from-absolute dp2))))
8bfe682a
CD
7426 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7427 (setq char (read-char-exclusive))
7428 (cond
7429 ((equal char ?d)
7430 (setq text (read-string "Day entry: "))
5dec9555
CD
7431 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7432 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7433 ((equal char ?a)
7434 (setq d1 (list (car d1) (nth 1 d1)
7435 (read-number (format "Reference year [%d]: " (nth 2 d1))
7436 (nth 2 d1))))
7437 (setq text (read-string "Anniversary (use %d to show years): "))
5dec9555
CD
7438 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7439 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7440 ((equal char ?b)
7441 (setq text (read-string "Block entry: "))
7442 (unless (and d1 d2 (not (equal d1 d2)))
7443 (error "No block of days selected"))
5dec9555
CD
7444 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7445 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7446 ((equal char ?j)
7447 (org-switch-to-buffer-other-window
7448 (find-file-noselect org-agenda-diary-file))
ed21c5c8 7449 (require 'org-datetree)
8bfe682a
CD
7450 (org-datetree-find-date-create d1)
7451 (org-reveal t))
7452 (t (error "Invalid selection character `%c'" char)))))
7453
5dec9555
CD
7454(defcustom org-agenda-insert-diary-strategy 'date-tree
7455 "Where in `org-agenda-diary-file' should new entries be added?
7456Valid values:
7457
7458date-tree in the date tree, as child of the date
7459top-level as top-level entries at the end of the file."
7460 :group 'org-agenda
7461 :type '(choice
7462 (const :tag "in a date tree" date-tree)
7463 (const :tag "as top level at end of file" top-level)))
7464
ed21c5c8
CD
7465(defcustom org-agenda-insert-diary-extract-time nil
7466 "Non-nil means extract any time specification from the diary entry."
7467 :group 'org-agenda
7468 :type 'boolean)
7469
8bfe682a
CD
7470(defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7471 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7472If TEXT is not empty, it will become the headline of the new entry, and
7473the resulting entry will not be shown. When TEXT is empty, switch to
7474`org-agenda-diary-file' and let the user finish the entry there."
7475 (let ((cw (current-window-configuration)))
7476 (org-switch-to-buffer-other-window
7477 (find-file-noselect org-agenda-diary-file))
7478 (widen)
7479 (goto-char (point-min))
7480 (cond
7481 ((eq type 'anniversary)
7482 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7483 (progn
7484 (or (org-on-heading-p t)
7485 (progn
7486 (outline-next-heading)
7487 (insert "* Anniversaries\n\n")
7488 (beginning-of-line -1)))))
7489 (outline-next-heading)
7490 (org-back-over-empty-lines)
7491 (backward-char 1)
7492 (insert "\n")
7493 (require 'diary-lib)
7494 (let ((calendar-date-display-form
7495 (if (if (boundp 'calendar-date-style)
7496 (eq calendar-date-style 'european)
afe98dfa
CD
7497 (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
7498 (org-bound-and-true-p european-calendar-style))) ; Emacs 22
8bfe682a
CD
7499 '(day " " month " " year)
7500 '(month " " day " " year))))
7501
7502 (insert (format "%%%%(diary-anniversary %s) %s"
7503 (calendar-date-string d1 nil t) text))))
7504 ((eq type 'day)
ed21c5c8
CD
7505 (let ((org-prefix-has-time t)
7506 (org-agenda-time-leading-zero t)
7507 fmt time time2)
7508 (if org-agenda-insert-diary-extract-time
7509 ;; Use org-format-agenda-item to parse text for a time-range and
7510 ;; remove it. FIXME: This is a hack, we should refactor
7511 ;; that function to make time extraction available separately
7512 (setq fmt (org-format-agenda-item nil text nil nil t)
7513 time (get-text-property 0 'time fmt)
7514 time2 (if (> (length time) 0)
7515 ;; split-string removes trailing ...... if
7516 ;; no end time given. First space
7517 ;; separates time from date.
7518 (concat " " (car (split-string time "\\.")))
7519 nil)
7520 text (get-text-property 0 'txt fmt)))
7521 (if (eq org-agenda-insert-diary-strategy 'top-level)
7522 (org-agenda-insert-diary-as-top-level text)
7523 (require 'org-datetree)
7524 (org-datetree-find-date-create d1)
7525 (org-agenda-insert-diary-make-new-entry text))
7526 (org-insert-time-stamp (org-time-from-absolute
7527 (calendar-absolute-from-gregorian d1))
7528 nil nil nil nil time2))
8bfe682a
CD
7529 (end-of-line 0))
7530 ((eq type 'block)
7531 (if (> (calendar-absolute-from-gregorian d1)
7532 (calendar-absolute-from-gregorian d2))
7533 (setq d1 (prog1 d2 (setq d2 d1))))
5dec9555
CD
7534 (if (eq org-agenda-insert-diary-strategy 'top-level)
7535 (org-agenda-insert-diary-as-top-level text)
7536 (require 'org-datetree)
7537 (org-datetree-find-date-create d1)
7538 (org-agenda-insert-diary-make-new-entry text))
8bfe682a
CD
7539 (org-insert-time-stamp (org-time-from-absolute
7540 (calendar-absolute-from-gregorian d1)))
7541 (insert "--")
7542 (org-insert-time-stamp (org-time-from-absolute
7543 (calendar-absolute-from-gregorian d2)))
7544 (end-of-line 0)))
7545 (if (string-match "\\S-" text)
7546 (progn
7547 (set-window-configuration cw)
7548 (message "%s entry added to %s"
7549 (capitalize (symbol-name type))
7550 (abbreviate-file-name org-agenda-diary-file)))
7551 (org-reveal t)
7552 (message "Please finish entry here"))))
7553
5dec9555
CD
7554(defun org-agenda-insert-diary-as-top-level (text)
7555 "Make new entry as a top-level entry at the end of the file.
7556Add TEXT as headline, and position the cursor in the second line so that
7557a timestamp can be added there."
7558 (widen)
7559 (goto-char (point-max))
7560 (or (bolp) (insert "\n"))
7561 (insert "* " text "\n")
7562 (if org-adapt-indentation (org-indent-to-column 2)))
7563
8bfe682a
CD
7564(defun org-agenda-insert-diary-make-new-entry (text)
7565 "Make new entry as last child of current entry.
7566Add TEXT as headline, and position the cursor in the second line so that
7567a timestamp can be added there."
7568 (let ((org-show-following-heading t)
7569 (org-show-siblings t)
7570 (org-show-hierarchy-above t)
7571 (org-show-entry-below t)
7572 col)
7573 (outline-next-heading)
7574 (org-back-over-empty-lines)
7575 (or (looking-at "[ \t]*$")
7576 (progn (insert "\n") (backward-char 1)))
ed21c5c8 7577 (org-insert-heading nil t)
8bfe682a
CD
7578 (org-do-demote)
7579 (setq col (current-column))
7580 (insert text "\n")
7581 (if org-adapt-indentation (org-indent-to-column col))
7582 (let ((org-show-following-heading t)
7583 (org-show-siblings t)
7584 (org-show-hierarchy-above t)
7585 (org-show-entry-below t))
7586 (org-show-context))))
7587
20908596
CD
7588(defun org-agenda-diary-entry ()
7589 "Make a diary entry, like the `i' command from the calendar.
8bfe682a
CD
7590All the standard commands work: block, weekly etc.
7591When `org-agenda-diary-file' points to a file,
7592`org-agenda-diary-entry-in-org-file' is called instead to create
7593entries in that Org-mode file."
20908596
CD
7594 (interactive)
7595 (org-agenda-check-type t 'agenda 'timeline)
8bfe682a
CD
7596 (if (not (eq org-agenda-diary-file 'diary-file))
7597 (org-agenda-diary-entry-in-org-file)
7598 (require 'diary-lib)
7599 (let* ((char (progn
7600 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7601 (read-char-exclusive)))
7602 (cmd (cdr (assoc char
7603 '((?d . insert-diary-entry)
7604 (?w . insert-weekly-diary-entry)
7605 (?m . insert-monthly-diary-entry)
7606 (?y . insert-yearly-diary-entry)
7607 (?a . insert-anniversary-diary-entry)
7608 (?b . insert-block-diary-entry)
7609 (?c . insert-cyclic-diary-entry)))))
7610 (oldf (symbol-function 'calendar-cursor-to-date))
7611 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7612 (point (point))
7613 (mark (or (mark t) (point))))
7614 (unless cmd
7615 (error "No command associated with <%c>" char))
7616 (unless (and (get-text-property point 'day)
7617 (or (not (equal ?b char))
7618 (get-text-property mark 'day)))
7619 (error "Don't know which date to use for diary entry"))
7620 ;; We implement this by hacking the `calendar-cursor-to-date' function
7621 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7622 (let ((calendar-mark-ring
7623 (list (calendar-gregorian-from-absolute
7624 (or (get-text-property mark 'day)
7625 (get-text-property point 'day))))))
7626 (unwind-protect
7627 (progn
7628 (fset 'calendar-cursor-to-date
7629 (lambda (&optional error dummy)
7630 (calendar-gregorian-from-absolute
7631 (get-text-property point 'day))))
20908596 7632 (call-interactively cmd))
8bfe682a 7633 (fset 'calendar-cursor-to-date oldf))))))
20908596 7634
20908596
CD
7635(defun org-agenda-execute-calendar-command (cmd)
7636 "Execute a calendar command from the agenda, with the date associated to
7637the cursor position."
7638 (org-agenda-check-type t 'agenda 'timeline)
7639 (require 'diary-lib)
7640 (unless (get-text-property (point) 'day)
7641 (error "Don't know which date to use for calendar command"))
7642 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7643 (point (point))
7644 (date (calendar-gregorian-from-absolute
7645 (get-text-property point 'day)))
7646 ;; the following 2 vars are needed in the calendar
7647 (displayed-month (car date))
7648 (displayed-year (nth 2 date)))
7649 (unwind-protect
7650 (progn
7651 (fset 'calendar-cursor-to-date
0627c265 7652 (lambda (&optional error dummy)
20908596
CD
7653 (calendar-gregorian-from-absolute
7654 (get-text-property point 'day))))
7655 (call-interactively cmd))
7656 (fset 'calendar-cursor-to-date oldf))))
7657
7658(defun org-agenda-phases-of-moon ()
7659 "Display the phases of the moon for the 3 months around the cursor date."
7660 (interactive)
7661 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
7662
7663(defun org-agenda-holidays ()
7664 "Display the holidays for the 3 months around the cursor date."
7665 (interactive)
7666 (org-agenda-execute-calendar-command 'list-calendar-holidays))
7667
7668(defvar calendar-longitude)
7669(defvar calendar-latitude)
7670(defvar calendar-location-name)
7671
7672(defun org-agenda-sunrise-sunset (arg)
7673 "Display sunrise and sunset for the cursor date.
7674Latitude and longitude can be specified with the variables
7675`calendar-latitude' and `calendar-longitude'. When called with prefix
7676argument, latitude and longitude will be prompted for."
7677 (interactive "P")
7678 (require 'solar)
7679 (let ((calendar-longitude (if arg nil calendar-longitude))
7680 (calendar-latitude (if arg nil calendar-latitude))
7681 (calendar-location-name
7682 (if arg "the given coordinates" calendar-location-name)))
7683 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
7684
7685(defun org-agenda-goto-calendar ()
7686 "Open the Emacs calendar with the date at the cursor."
7687 (interactive)
7688 (org-agenda-check-type t 'agenda 'timeline)
7689 (let* ((day (or (get-text-property (point) 'day)
7690 (error "Don't know which date to open in calendar")))
7691 (date (calendar-gregorian-from-absolute day))
7692 (calendar-move-hook nil)
7693 (calendar-view-holidays-initially-flag nil)
3820f429 7694 (calendar-view-diary-initially-flag nil))
20908596
CD
7695 (calendar)
7696 (calendar-goto-date date)))
7697
7698;;;###autoload
7699(defun org-calendar-goto-agenda ()
7700 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
7701This is a command that has to be installed in `calendar-mode-map'."
7702 (interactive)
7703 (org-agenda-list nil (calendar-absolute-from-gregorian
7704 (calendar-cursor-to-date))
7705 nil))
7706
7707(defun org-agenda-convert-date ()
7708 (interactive)
7709 (org-agenda-check-type t 'agenda 'timeline)
7710 (let ((day (get-text-property (point) 'day))
7711 date s)
7712 (unless day
7713 (error "Don't know which date to convert"))
7714 (setq date (calendar-gregorian-from-absolute day))
7715 (setq s (concat
7716 "Gregorian: " (calendar-date-string date) "\n"
7717 "ISO: " (calendar-iso-date-string date) "\n"
7718 "Day of Yr: " (calendar-day-of-year-string date) "\n"
7719 "Julian: " (calendar-julian-date-string date) "\n"
7720 "Astron. JD: " (calendar-astro-date-string date)
7721 " (Julian date number at noon UTC)\n"
7722 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
7723 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
7724 "French: " (calendar-french-date-string date) "\n"
7725 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
7726 "Mayan: " (calendar-mayan-date-string date) "\n"
7727 "Coptic: " (calendar-coptic-date-string date) "\n"
7728 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
7729 "Persian: " (calendar-persian-date-string date) "\n"
7730 "Chinese: " (calendar-chinese-date-string date) "\n"))
7731 (with-output-to-temp-buffer "*Dates*"
7732 (princ s))
93b62de8 7733 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
20908596 7734
c8d0cf5c
CD
7735;;; Bulk commands
7736
7737(defvar org-agenda-bulk-marked-entries nil
7738 "List of markers that refer to marked entries in the agenda.")
7739
54a0dee5
CD
7740(defun org-agenda-bulk-marked-p ()
7741 (eq (get-char-property (point-at-bol) 'type)
7742 'org-marked-entry-overlay))
7743
acedf35c 7744(defun org-agenda-bulk-mark (&optional arg)
c8d0cf5c 7745 "Mark the entry at point for future bulk action."
acedf35c
CD
7746 (interactive "p")
7747 (dotimes (i (max arg 1))
7748 (unless (org-get-at-bol 'org-agenda-diary-link)
7749 (let* ((m (org-get-at-bol 'org-hd-marker))
7750 ov)
7751 (unless (org-agenda-bulk-marked-p)
7752 (unless m (error "Nothing to mark at point"))
7753 (push m org-agenda-bulk-marked-entries)
7754 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
7755 (org-overlay-display ov "> "
7756 (org-get-todo-face "TODO")
7757 'evaporate)
7758 (overlay-put ov 'type 'org-marked-entry-overlay))
7759 (beginning-of-line 2)
7760 (while (and (get-char-property (point) 'invisible) (not (eobp)))
7761 (beginning-of-line 2))
7762 (message "%d entries marked for bulk action"
7763 (length org-agenda-bulk-marked-entries))))))
c8d0cf5c
CD
7764
7765(defun org-agenda-bulk-unmark ()
7766 "Unmark the entry at point for future bulk action."
7767 (interactive)
54a0dee5 7768 (when (org-agenda-bulk-marked-p)
c8d0cf5c
CD
7769 (org-agenda-bulk-remove-overlays
7770 (point-at-bol) (+ 2 (point-at-bol)))
7771 (setq org-agenda-bulk-marked-entries
8d642074 7772 (delete (org-get-at-bol 'org-hd-marker)
c8d0cf5c
CD
7773 org-agenda-bulk-marked-entries)))
7774 (beginning-of-line 2)
ed21c5c8
CD
7775 (while (and (get-char-property (point) 'invisible) (not (eobp)))
7776 (beginning-of-line 2))
c8d0cf5c
CD
7777 (message "%d entries marked for bulk action"
7778 (length org-agenda-bulk-marked-entries)))
7779
54a0dee5
CD
7780(defun org-agenda-bulk-toggle ()
7781 "Toggle marking the entry at point for bulk action."
7782 (interactive)
7783 (if (org-agenda-bulk-marked-p)
7784 (org-agenda-bulk-unmark)
7785 (org-agenda-bulk-mark)))
c8d0cf5c
CD
7786
7787(defun org-agenda-bulk-remove-overlays (&optional beg end)
7788 "Remove the mark overlays between BEG and END in the agenda buffer.
7789BEG and END default to the buffer limits.
7790
7791This only removes the overlays, it does not remove the markers
7792from the list in `org-agenda-bulk-marked-entries'."
7793 (interactive)
7794 (mapc (lambda (ov)
86fbb8ca
CD
7795 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
7796 (delete-overlay ov)))
7797 (overlays-in (or beg (point-min)) (or end (point-max)))))
c8d0cf5c
CD
7798
7799(defun org-agenda-bulk-remove-all-marks ()
7800 "Remove all marks in the agenda buffer.
7801This will remove the markers, and the overlays."
7802 (interactive)
7803 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
7804 (setq org-agenda-bulk-marked-entries nil)
7805 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
7806
ed21c5c8
CD
7807(defun org-agenda-bulk-action (&optional arg)
7808 "Execute an remote-editing action on all marked entries.
7809The prefix arg is passed through to the command if possible."
7810 (interactive "P")
c8d0cf5c
CD
7811 (unless org-agenda-bulk-marked-entries
7812 (error "No entries are marked"))
acedf35c 7813 (message "Bulk: [r]efile [$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [S]catter [d]eadline")
c8d0cf5c 7814 (let* ((action (read-char-exclusive))
ed21c5c8 7815 (org-log-refile (if org-log-refile 'time nil))
c8d0cf5c 7816 (entries (reverse org-agenda-bulk-marked-entries))
86fbb8ca 7817 redo-at-end
c8d0cf5c
CD
7818 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
7819 (cond
7820 ((equal action ?$)
7821 (setq cmd '(org-agenda-archive)))
7822
7823 ((equal action ?A)
7824 (setq cmd '(org-agenda-archive-to-archive-sibling)))
7825
7826 ((member action '(?r ?w))
7827 (setq rfloc (org-refile-get-location
7828 "Refile to: "
7829 (marker-buffer (car org-agenda-bulk-marked-entries))
7830 org-refile-allow-creating-parent-nodes))
86fbb8ca
CD
7831 (if (nth 3 rfloc)
7832 (setcar (nthcdr 3 rfloc)
7833 (move-marker (make-marker) (nth 3 rfloc)
7834 (or (get-file-buffer (nth 1 rfloc))
7835 (find-buffer-visiting (nth 1 rfloc))
7836 (error "This should not happen")))))
c8d0cf5c 7837
86fbb8ca
CD
7838 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
7839 redo-at-end t))
c8d0cf5c
CD
7840
7841 ((equal action ?t)
54a0dee5 7842 (setq state (org-icompleting-read
c8d0cf5c
CD
7843 "Todo state: "
7844 (with-current-buffer (marker-buffer (car entries))
7845 (mapcar 'list org-todo-keywords-1))))
7846 (setq cmd `(let ((org-inhibit-blocking t)
7847 (org-inhibit-logging 'note))
7848 (org-agenda-todo ,state))))
7849
7850 ((memq action '(?- ?+))
54a0dee5 7851 (setq tag (org-icompleting-read
c8d0cf5c
CD
7852 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
7853 (with-current-buffer (marker-buffer (car entries))
7854 (delq nil
7855 (mapcar (lambda (x)
7856 (if (stringp (car x)) x)) org-tag-alist)))))
7857 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
7858
7859 ((memq action '(?s ?d))
ed21c5c8
CD
7860 (let* ((date (unless arg
7861 (org-read-date
7862 nil nil nil
7863 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
7864 (ans (if arg nil org-read-date-final-answer))
c8d0cf5c
CD
7865 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
7866 (setq cmd `(let* ((bound (fboundp 'read-string))
7867 (old (and bound (symbol-function 'read-string))))
7868 (unwind-protect
7869 (progn
7870 (fset 'read-string (lambda (&rest ignore) ,ans))
ed21c5c8 7871 (eval '(,c1 arg)))
c8d0cf5c
CD
7872 (if bound
7873 (fset 'read-string old)
7874 (fmakunbound 'read-string)))))))
acedf35c
CD
7875
7876 ((eq action '?S)
7877 (let ((days (read-number
7878 (format "Scatter tasks across how many %sdays: "
7879 (if arg "week" "")) 7)))
7880 (setq cmd
7881 `(let ((distance (random ,(1+ days))))
7882 (if arg
7883 (let ((dist distance)
7884 (day-of-week
7885 (calendar-day-of-week
7886 (calendar-gregorian-from-absolute (org-today)))))
7887 (dotimes (i (1+ dist))
7888 (while (member day-of-week org-agenda-weekend-days)
7889 (incf distance)
7890 (incf day-of-week)
7891 (if (= day-of-week 7)
7892 (setq day-of-week 0)))
7893 (incf day-of-week)
7894 (if (= day-of-week 7)
7895 (setq day-of-week 0)))))
7896 (org-agenda-date-later distance)))))
7897
c8d0cf5c
CD
7898 (t (error "Invalid bulk action")))
7899
7900 ;; Sort the markers, to make sure that parents are handled before children
7901 (setq entries (sort entries
7902 (lambda (a b)
7903 (cond
7904 ((equal (marker-buffer a) (marker-buffer b))
7905 (< (marker-position a) (marker-position b)))
7906 (t
7907 (string< (buffer-name (marker-buffer a))
7908 (buffer-name (marker-buffer b))))))))
7909
7910 ;; Now loop over all markers and apply cmd
7911 (while (setq e (pop entries))
7912 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
7913 (if (not pos)
7914 (progn (message "Skipping removed entry at %s" e)
7915 (setq cntskip (1+ cntskip)))
7916 (goto-char pos)
7917 (eval cmd)
7918 (setq org-agenda-bulk-marked-entries
7919 (delete e org-agenda-bulk-marked-entries))
7920 (setq cnt (1+ cnt))))
7921 (setq org-agenda-bulk-marked-entries nil)
7922 (org-agenda-bulk-remove-all-marks)
86fbb8ca 7923 (when redo-at-end (org-agenda-redo))
c8d0cf5c
CD
7924 (message "Acted on %d entries%s"
7925 cnt
7926 (if (= cntskip 0)
7927 ""
7928 (format ", skipped %d (disappeared before their turn)"
7929 cntskip)))))
8d642074
CD
7930
7931;;; Flagging notes
7932
7933(defun org-agenda-show-the-flagging-note ()
7934 "Display the flagging note in the other window.
7935When called a second time in direct sequence, offer to remove the FLAGGING
7936tag and (if present) the flagging note."
7937 (interactive)
7938 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
7939 (win (selected-window))
7940 note heading newhead)
7941 (unless hdmarker
7942 (error "No linked entry at point"))
7943 (if (and (eq this-command last-command)
7944 (y-or-n-p "Unflag and remove any flagging note? "))
7945 (progn
7946 (org-agenda-remove-flag hdmarker)
7947 (let ((win (get-buffer-window "*Flagging Note*")))
7948 (and win (delete-window win)))
7949 (message "Entry unflaged"))
7950 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
7951 (unless note
7952 (error "No flagging note"))
7953 (org-kill-new note)
7954 (org-switch-to-buffer-other-window "*Flagging Note*")
7955 (erase-buffer)
7956 (insert note)
7957 (goto-char (point-min))
7958 (while (re-search-forward "\\\\n" nil t)
7959 (replace-match "\n" t t))
7960 (goto-char (point-min))
7961 (select-window win)
7962 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
7963
7964(defun org-agenda-remove-flag (marker)
8bfe682a 7965 "Remove the FLAGGED tag and any flagging note in the entry."
8d642074
CD
7966 (let (newhead)
7967 (org-with-point-at marker
7968 (org-toggle-tag "FLAGGED" 'off)
7969 (org-entry-delete nil "THEFLAGGINGNOTE")
7970 (setq newhead (org-get-heading)))
7971 (org-agenda-change-all-lines newhead marker)
7972 (message "Entry unflaged")))
7973
7974(defun org-agenda-get-any-marker (&optional pos)
7975 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
7976 (get-text-property (or pos (point-at-bol)) 'org-marker)))
c8d0cf5c 7977
20908596
CD
7978;;; Appointment reminders
7979
7980(defvar appt-time-msg-list)
7981
7982;;;###autoload
7983(defun org-agenda-to-appt (&optional refresh filter)
7984 "Activate appointments found in `org-agenda-files'.
7985With a \\[universal-argument] prefix, refresh the list of
33306645 7986appointments.
20908596
CD
7987
7988If FILTER is t, interactively prompt the user for a regular
7989expression, and filter out entries that don't match it.
7990
7991If FILTER is a string, use this string as a regular expression
7992for filtering entries out.
7993
7994FILTER can also be an alist with the car of each cell being
7995either 'headline or 'category. For example:
7996
7997 '((headline \"IMPORTANT\")
7998 (category \"Work\"))
7999
8000will only add headlines containing IMPORTANT or headlines
8001belonging to the \"Work\" category."
8002 (interactive "P")
20908596
CD
8003 (if refresh (setq appt-time-msg-list nil))
8004 (if (eq filter t)
8005 (setq filter (read-from-minibuffer "Regexp filter: ")))
8006 (let* ((cnt 0) ; count added events
8007 (org-agenda-new-buffers nil)
8008 (org-deadline-warning-days 0)
acedf35c
CD
8009 ;; Do not use `org-today' here because appt only takes
8010 ;; time and without date as argument, so it may pass wrong
8011 ;; information otherwise
20908596
CD
8012 (today (org-date-to-gregorian
8013 (time-to-days (current-time))))
c8d0cf5c 8014 (org-agenda-restrict nil)
621f83e4 8015 (files (org-agenda-files 'unrestricted)) entries file)
20908596 8016 ;; Get all entries which may contain an appt
db55f368 8017 (org-prepare-agenda-buffers files)
20908596
CD
8018 (while (setq file (pop files))
8019 (setq entries
8020 (append entries
8021 (org-agenda-get-day-entries
8022 file today :timestamp :scheduled :deadline))))
8023 (setq entries (delq nil entries))
8024 ;; Map thru entries and find if we should filter them out
8025 (mapc
8026 (lambda(x)
621f83e4 8027 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
20908596
CD
8028 (cat (get-text-property 1 'org-category x))
8029 (tod (get-text-property 1 'time-of-day x))
8030 (ok (or (null filter)
8031 (and (stringp filter) (string-match filter evt))
8032 (and (listp filter)
8033 (or (string-match
8034 (cadr (assoc 'category filter)) cat)
8035 (string-match
8036 (cadr (assoc 'headline filter)) evt))))))
8037 ;; FIXME: Shall we remove text-properties for the appt text?
8038 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8039 (when (and ok tod)
621f83e4 8040 (setq tod (concat "00" (number-to-string tod))
20908596 8041 tod (when (string-match
621f83e4 8042 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
20908596
CD
8043 (concat (match-string 1 tod) ":"
8044 (match-string 2 tod))))
8045 (appt-add tod evt)
8046 (setq cnt (1+ cnt))))) entries)
8047 (org-release-buffers org-agenda-new-buffers)
8048 (if (eq cnt 0)
8049 (message "No event to add")
8050 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8051
621f83e4
CD
8052(defun org-agenda-todayp (date)
8053 "Does DATE mean today, when considering `org-extend-today-until'?"
acedf35c
CD
8054 (let ((today (org-today))
8055 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8056 date)))
8057 (eq date today)))
621f83e4 8058
20908596
CD
8059(provide 'org-agenda)
8060
b349f79f 8061
20908596 8062;;; org-agenda.el ends here