Fix Org ChangeLog entries and remove arch-tag.
[bpt/emacs.git] / lisp / org / org-agenda.el
CommitLineData
b349f79f 1;;; org-agenda.el --- Dynamic task and appointment lists for Org
20908596 2
3ab2c837
BG
3;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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
3ab2c837 9;; Version: 7.7
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))
3ab2c837
BG
64(declare-function org-pop-to-buffer-same-window "org-compat"
65 (&optional buffer-or-name norecord label))
66
20908596 67(defvar calendar-mode-map)
afe98dfa 68(defvar org-clock-current-task) ; defined in org-clock.el
8d642074 69(defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
8bfe682a
CD
70(defvar org-habit-show-habits)
71(defvar org-habit-show-habits-only-for-today)
20908596
CD
72
73;; Defined somewhere in this file, but used before definition.
74(defvar org-agenda-buffer-name)
75(defvar org-agenda-overriding-header)
8d642074 76(defvar org-agenda-title-append nil)
20908596
CD
77(defvar entry)
78(defvar date)
79(defvar org-agenda-undo-list)
80(defvar org-agenda-pending-undo-list)
81(defvar original-date) ; dynamically scoped, calendar.el does scope this
82
83(defcustom org-agenda-confirm-kill 1
84 "When set, remote killing from the agenda buffer needs confirmation.
85When t, a confirmation is always needed. When a number N, confirmation is
86only needed when the text to be killed contains more than N non-white lines."
87 :group 'org-agenda
88 :type '(choice
89 (const :tag "Never" nil)
90 (const :tag "Always" t)
c8d0cf5c 91 (integer :tag "When more than N lines")))
20908596
CD
92
93(defcustom org-agenda-compact-blocks nil
ed21c5c8 94 "Non-nil means make the block agenda more compact.
3ab2c837
BG
95This is done globally by leaving out lines like the agenda span
96name and week number or the separator lines."
20908596
CD
97 :group 'org-agenda
98 :type 'boolean)
99
0bd48b37
CD
100(defcustom org-agenda-block-separator ?=
101 "The separator between blocks in the agenda.
102If this is a string, it will be used as the separator, with a newline added.
3ab2c837
BG
103If it is a character, it will be repeated to fill the window width.
104If nil the separator is disabled. In `org-agenda-custom-commands' this
105addresses the separator between the current and the previous block."
0bd48b37
CD
106 :group 'org-agenda
107 :type '(choice
3ab2c837 108 (const :tag "Disabled" nil)
0bd48b37
CD
109 (character)
110 (string)))
111
20908596
CD
112(defgroup org-agenda-export nil
113 "Options concerning exporting agenda views in Org-mode."
114 :tag "Org Agenda Export"
115 :group 'org-agenda)
116
117(defcustom org-agenda-with-colors t
ed21c5c8 118 "Non-nil means use colors in agenda views."
20908596
CD
119 :group 'org-agenda-export
120 :type 'boolean)
121
122(defcustom org-agenda-exporter-settings nil
123 "Alist of variable/value pairs that should be active during agenda export.
c8d0cf5c
CD
124This is a good place to set options for ps-print and for htmlize.
125Note that the way this is implemented, the values will be evaluated
126before assigned to the variables. So make sure to quote values you do
127*not* want evaluated, for example
128
129 (setq org-agenda-exporter-settings
130 '((ps-print-color-p 'black-white)))"
20908596
CD
131 :group 'org-agenda-export
132 :type '(repeat
133 (list
134 (variable)
135 (sexp :tag "Value"))))
136
c8d0cf5c
CD
137(defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
138 "Hook run in temporary buffer before writing it to an export file.
139A useful function is `org-agenda-add-entry-text'."
140 :group 'org-agenda-export
141 :type 'hook
142 :options '(org-agenda-add-entry-text))
143
144(defcustom org-agenda-add-entry-text-maxlines 0
145 "Maximum number of entry text lines to be added to agenda.
146This is only relevant when `org-agenda-add-entry-text' is part of
147`org-agenda-before-write-hook', which it is by default.
148When this is 0, nothing will happen. When it is greater than 0, it
149specifies the maximum number of lines that will be added for each entry
54a0dee5
CD
150that is listed in the agenda view.
151
152Note that this variable is not used during display, only when exporting
86fbb8ca
CD
153the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
154and `org-agenda-entry-text-maxlines'."
c8d0cf5c
CD
155 :group 'org-agenda
156 :type 'integer)
157
158(defcustom org-agenda-add-entry-text-descriptive-links t
ed21c5c8 159 "Non-nil means export org-links as descriptive links in agenda added text.
c8d0cf5c
CD
160This variable applies to the text added to the agenda when
161`org-agenda-add-entry-text-maxlines' is larger than 0.
162When this variable nil, the URL will (also) be shown."
163 :group 'org-agenda
164 :type 'boolean)
165
20908596
CD
166(defcustom org-agenda-export-html-style ""
167 "The style specification for exported HTML Agenda files.
168If this variable contains a string, it will replace the default <style>
169section as produced by `htmlize'.
170Since there are different ways of setting style information, this variable
171needs to contain the full HTML structure to provide a style, including the
172surrounding HTML tags. The style specifications should include definitions
173the fonts used by the agenda, here is an example:
174
175 <style type=\"text/css\">
176 p { font-weight: normal; color: gray; }
177 .org-agenda-structure {
178 font-size: 110%;
179 color: #003399;
180 font-weight: 600;
181 }
182 .org-todo {
183 color: #cc6666;
184 font-weight: bold;
185 }
c8d0cf5c
CD
186 .org-agenda-done {
187 color: #339933;
188 }
20908596
CD
189 .org-done {
190 color: #339933;
191 }
192 .title { text-align: center; }
193 .todo, .deadline { color: red; }
194 .done { color: green; }
195 </style>
196
197or, if you want to keep the style in a file,
198
199 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
200
201As the value of this option simply gets inserted into the HTML <head> header,
202you can \"misuse\" it to also add other text to the header. However,
203<style>...</style> is required, if not present the variable will be ignored."
204 :group 'org-agenda-export
205 :group 'org-export-html
206 :type 'string)
207
86fbb8ca
CD
208(defcustom org-agenda-persistent-filter nil
209 "When set, keep filters from one agenda view to the next."
210 :group 'org-agenda
211 :type 'boolean)
212
20908596
CD
213(defgroup org-agenda-custom-commands nil
214 "Options concerning agenda views in Org-mode."
215 :tag "Org Agenda Custom Commands"
216 :group 'org-agenda)
217
218(defconst org-sorting-choice
219 '(choice
220 (const time-up) (const time-down)
221 (const category-keep) (const category-up) (const category-down)
222 (const tag-down) (const tag-up)
223 (const priority-up) (const priority-down)
621f83e4 224 (const todo-state-up) (const todo-state-down)
c8d0cf5c 225 (const effort-up) (const effort-down)
8bfe682a 226 (const habit-up) (const habit-down)
86fbb8ca 227 (const alpha-up) (const alpha-down)
c8d0cf5c 228 (const user-defined-up) (const user-defined-down))
20908596
CD
229 "Sorting choices.")
230
231(defconst org-agenda-custom-commands-local-options
232 `(repeat :tag "Local settings for this command. Remember to quote values"
233 (choice :tag "Setting"
c8d0cf5c
CD
234 (list :tag "Heading for this block"
235 (const org-agenda-overriding-header)
236 (string :tag "Headline"))
20908596
CD
237 (list :tag "Files to be searched"
238 (const org-agenda-files)
239 (list
240 (const :format "" quote)
c8d0cf5c 241 (repeat (file))))
20908596
CD
242 (list :tag "Sorting strategy"
243 (const org-agenda-sorting-strategy)
244 (list
245 (const :format "" quote)
246 (repeat
247 ,org-sorting-choice)))
248 (list :tag "Prefix format"
249 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
250 (string))
251 (list :tag "Number of days in agenda"
acedf35c
CD
252 (const org-agenda-span)
253 (choice (const :tag "Day" 'day)
254 (const :tag "Week" 'week)
255 (const :tag "Month" 'month)
256 (const :tag "Year" 'year)
257 (integer :tag "Custom")))
20908596
CD
258 (list :tag "Fixed starting date"
259 (const org-agenda-start-day)
260 (string :value "2007-11-01"))
261 (list :tag "Start on day of week"
262 (const org-agenda-start-on-weekday)
263 (choice :value 1
264 (const :tag "Today" nil)
c8d0cf5c 265 (integer :tag "Weekday No.")))
20908596
CD
266 (list :tag "Include data from diary"
267 (const org-agenda-include-diary)
268 (boolean))
269 (list :tag "Deadline Warning days"
270 (const org-deadline-warning-days)
271 (integer :value 1))
c8d0cf5c
CD
272 (list :tag "Tags filter preset"
273 (const org-agenda-filter-preset)
274 (list
275 (const :format "" quote)
276 (repeat
277 (string :tag "+tag or -tag"))))
ed21c5c8
CD
278 (list :tag "Set daily/weekly entry types"
279 (const org-agenda-entry-types)
280 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
281 (const :deadline)
282 (const :scheduled)
283 (const :timestamp)
284 (const :sexp)))
20908596
CD
285 (list :tag "Standard skipping condition"
286 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
287 (const org-agenda-skip-function)
288 (list
289 (const :format "" quote)
290 (list
291 (choice
33306645 292 :tag "Skipping range"
20908596
CD
293 (const :tag "Skip entry" org-agenda-skip-entry-if)
294 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
295 (repeat :inline t :tag "Conditions for skipping"
296 (choice
297 :tag "Condition type"
298 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
299 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
ed21c5c8
CD
300 (list :tag "TODO state is" :inline t
301 (const 'todo)
302 (choice
303 (const :tag "any not-done state" 'todo)
304 (const :tag "any done state" 'done)
305 (const :tag "any state" 'any)
306 (list :tag "Keyword list"
307 (const :format "" quote)
308 (repeat (string :tag "Keyword")))))
309 (list :tag "TODO state is not" :inline t
310 (const 'nottodo)
311 (choice
312 (const :tag "any not-done state" 'todo)
313 (const :tag "any done state" 'done)
314 (const :tag "any state" 'any)
315 (list :tag "Keyword list"
316 (const :format "" quote)
317 (repeat (string :tag "Keyword")))))
20908596
CD
318 (const :tag "scheduled" 'scheduled)
319 (const :tag "not scheduled" 'notscheduled)
320 (const :tag "deadline" 'deadline)
c8d0cf5c
CD
321 (const :tag "no deadline" 'notdeadline)
322 (const :tag "timestamp" 'timestamp)
323 (const :tag "no timestamp" 'nottimestamp))))))
20908596
CD
324 (list :tag "Non-standard skipping condition"
325 :value (org-agenda-skip-function)
2c3ad40d 326 (const org-agenda-skip-function)
c8d0cf5c
CD
327 (sexp :tag "Function or form (quoted!)"))
328 (list :tag "Any variable"
329 (variable :tag "Variable")
330 (sexp :tag "Value (sexp)"))))
20908596
CD
331 "Selection of examples for agenda command settings.
332This will be spliced into the custom type of
333`org-agenda-custom-commands'.")
334
335
336(defcustom org-agenda-custom-commands nil
337 "Custom commands for the agenda.
338These commands will be offered on the splash screen displayed by the
339agenda dispatcher \\[org-agenda]. Each entry is a list like this:
340
341 (key desc type match settings files)
342
343key The key (one or more characters as a string) to be associated
344 with the command.
345desc A description of the command, when omitted or nil, a default
346 description is built using MATCH.
347type The command type, any of the following symbols:
348 agenda The daily/weekly agenda.
349 todo Entries with a specific TODO keyword, in all agenda files.
350 search Entries containing search words entry or headline.
351 tags Tags/Property/TODO match in all agenda files.
352 tags-todo Tags/P/T match in all agenda files, TODO entries only.
353 todo-tree Sparse tree of specific TODO keyword in *current* file.
354 tags-tree Sparse tree with all tags matches in *current* file.
355 occur-tree Occur sparse tree for *current* file.
356 ... A user-defined function.
357match What to search for:
358 - a single keyword for TODO keyword searches
359 - a tags match expression for tags searches
360 - a word search expression for text searches.
361 - a regular expression for occur searches
362 For all other commands, this should be the empty string.
363settings A list of option settings, similar to that in a let form, so like
364 this: ((opt1 val1) (opt2 val2) ...). The values will be
365 evaluated at the moment of execution, so quote them when needed.
366files A list of files file to write the produced agenda buffer to
367 with the command `org-store-agenda-views'.
368 If a file name ends in \".html\", an HTML version of the buffer
369 is written out. If it ends in \".ps\", a postscript version is
33306645 370 produced. Otherwise, only the plain text is written to the file.
20908596
CD
371
372You can also define a set of commands, to create a composite agenda buffer.
373In this case, an entry looks like this:
374
375 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
376
377where
378
379desc A description string to be displayed in the dispatcher menu.
380cmd An agenda command, similar to the above. However, tree commands
381 are no allowed, but instead you can get agenda and global todo list.
382 So valid commands for a set are:
383 (agenda \"\" settings)
384 (alltodo \"\" settings)
385 (stuck \"\" settings)
386 (todo \"match\" settings files)
387 (search \"match\" settings files)
388 (tags \"match\" settings files)
389 (tags-todo \"match\" settings files)
390
391Each command can carry a list of options, and another set of options can be
392given for the whole set of commands. Individual command options take
393precedence over the general options.
394
395When using several characters as key to a command, the first characters
396are prefix commands. For the dispatcher to display useful information, you
397should provide a description for the prefix, like
398
399 (setq org-agenda-custom-commands
400 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
401 (\"hl\" tags \"+HOME+Lisa\")
402 (\"hp\" tags \"+HOME+Peter\")
403 (\"hk\" tags \"+HOME+Kim\")))"
404 :group 'org-agenda-custom-commands
405 :type `(repeat
406 (choice :value ("x" "Describe command here" tags "" nil)
407 (list :tag "Single command"
408 (string :tag "Access Key(s) ")
409 (option (string :tag "Description"))
410 (choice
411 (const :tag "Agenda" agenda)
412 (const :tag "TODO list" alltodo)
413 (const :tag "Search words" search)
414 (const :tag "Stuck projects" stuck)
c8d0cf5c
CD
415 (const :tag "Tags/Property match (all agenda files)" tags)
416 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
20908596
CD
417 (const :tag "TODO keyword search (all agenda files)" todo)
418 (const :tag "Tags sparse tree (current buffer)" tags-tree)
419 (const :tag "TODO keyword tree (current buffer)" todo-tree)
420 (const :tag "Occur tree (current buffer)" occur-tree)
421 (sexp :tag "Other, user-defined function"))
422 (string :tag "Match (only for some commands)")
423 ,org-agenda-custom-commands-local-options
424 (option (repeat :tag "Export" (file :tag "Export to"))))
425 (list :tag "Command series, all agenda files"
426 (string :tag "Access Key(s)")
427 (string :tag "Description ")
428 (repeat :tag "Component"
429 (choice
430 (list :tag "Agenda"
431 (const :format "" agenda)
432 (const :tag "" :format "" "")
433 ,org-agenda-custom-commands-local-options)
434 (list :tag "TODO list (all keywords)"
435 (const :format "" alltodo)
436 (const :tag "" :format "" "")
437 ,org-agenda-custom-commands-local-options)
438 (list :tag "Search words"
439 (const :format "" search)
440 (string :tag "Match")
441 ,org-agenda-custom-commands-local-options)
442 (list :tag "Stuck projects"
443 (const :format "" stuck)
444 (const :tag "" :format "" "")
445 ,org-agenda-custom-commands-local-options)
446 (list :tag "Tags search"
447 (const :format "" tags)
448 (string :tag "Match")
449 ,org-agenda-custom-commands-local-options)
450 (list :tag "Tags search, TODO entries only"
451 (const :format "" tags-todo)
452 (string :tag "Match")
453 ,org-agenda-custom-commands-local-options)
454 (list :tag "TODO keyword search"
455 (const :format "" todo)
456 (string :tag "Match")
457 ,org-agenda-custom-commands-local-options)
458 (list :tag "Other, user-defined function"
459 (symbol :tag "function")
460 (string :tag "Match")
461 ,org-agenda-custom-commands-local-options)))
462
463 (repeat :tag "Settings for entire command set"
464 (list (variable :tag "Any variable")
465 (sexp :tag "Value")))
466 (option (repeat :tag "Export" (file :tag "Export to"))))
467 (cons :tag "Prefix key documentation"
468 (string :tag "Access Key(s)")
469 (string :tag "Description ")))))
470
471(defcustom org-agenda-query-register ?o
472 "The register holding the current query string.
33306645 473The purpose of this is that if you construct a query string interactively,
20908596
CD
474you can then use it to define a custom command."
475 :group 'org-agenda-custom-commands
476 :type 'character)
477
478(defcustom org-stuck-projects
479 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
480 "How to identify stuck projects.
481This is a list of four items:
c8d0cf5c
CD
4821. A tags/todo/property matcher string that is used to identify a project.
483 See the manual for a description of tag and property searches.
20908596
CD
484 The entire tree below a headline matched by this is considered one project.
4852. A list of TODO keywords identifying non-stuck projects.
486 If the project subtree contains any headline with one of these todo
487 keywords, the project is considered to be not stuck. If you specify
488 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
4893. A list of tags identifying non-stuck projects.
490 If the project subtree contains any headline with one of these tags,
491 the project is considered to be not stuck. If you specify \"*\" as
c8d0cf5c
CD
492 a tag, any tag will mark the project unstuck. Note that this is about
493 the explicit presence of a tag somewhere in the subtree, inherited
494 tags to not count here. If inherited tags make a project not stuck,
495 use \"-TAG\" in the tags part of the matcher under (1.) above.
20908596
CD
4964. An arbitrary regular expression matching non-stuck projects.
497
c8d0cf5c
CD
498If the project turns out to be not stuck, search continues also in the
499subtree to see if any of the subtasks have project status.
500
501See also the variable `org-tags-match-list-sublevels' which applies
502to projects matched by this search as well.
503
20908596
CD
504After defining this variable, you may use \\[org-agenda-list-stuck-projects]
505or `C-c a #' to produce the list."
506 :group 'org-agenda-custom-commands
507 :type '(list
508 (string :tag "Tags/TODO match to identify a project")
509 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
510 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
c8d0cf5c 511 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
20908596 512
71d35b24
CD
513(defcustom org-agenda-filter-effort-default-operator "<"
514 "The default operator for effort estimate filtering.
93b62de8 515If you select an effort estimate limit without first pressing an operator,
71d35b24
CD
516this one will be used."
517 :group 'org-agenda-custom-commands
518 :type '(choice (const :tag "less or equal" "<")
519 (const :tag "greater or equal"">")
520 (const :tag "equal" "=")))
20908596
CD
521
522(defgroup org-agenda-skip nil
523 "Options concerning skipping parts of agenda files."
524 :tag "Org Agenda Skip"
525 :group 'org-agenda)
3ab2c837
BG
526
527(defcustom org-agenda-skip-function-global nil
528 "Function to be called at each match during agenda construction.
529If this function returns nil, the current match should not be skipped.
530If the function decided to skip an agenda match, is must return the
531buffer position from which the search should be continued.
532This may also be a Lisp form, which will be evaluated.
533
534This variable will be applied to every agenda match, including
535tags/property searches and TODO lists. So try to make the test function
536do its checking as efficiently as possible. To implement a skipping
537condition just for specific agenda commands, use the variable
538`org-agenda-skip-function' which can be set in the options section
539of custom agenda commands."
540 :group 'org-agenda-skip
541 :type 'sexp)
542
0bd48b37
CD
543(defgroup org-agenda-daily/weekly nil
544 "Options concerning the daily/weekly agenda."
545 :tag "Org Agenda Daily/Weekly"
546 :group 'org-agenda)
547(defgroup org-agenda-todo-list nil
548 "Options concerning the global todo list agenda view."
549 :tag "Org Agenda Todo List"
550 :group 'org-agenda)
551(defgroup org-agenda-match-view nil
552 "Options concerning the general tags/property/todo match agenda view."
553 :tag "Org Agenda Match View"
554 :group 'org-agenda)
8bfe682a
CD
555(defgroup org-agenda-search-view nil
556 "Options concerning the general tags/property/todo match agenda view."
557 :tag "Org Agenda Match View"
558 :group 'org-agenda)
20908596 559
2c3ad40d 560(defvar org-agenda-archives-mode nil
ed21c5c8 561 "Non-nil means the agenda will include archived items.
2c3ad40d
CD
562If this is the symbol `trees', trees in the selected agenda scope
563that are marked with the ARCHIVE tag will be included anyway. When this is
564t, also all archive files associated with the current selection of agenda
565files will be included.")
566
b349f79f 567(defcustom org-agenda-skip-comment-trees t
ed21c5c8 568 "Non-nil means skip trees that start with the COMMENT keyword.
33306645 569When nil, these trees are also scanned by agenda commands."
b349f79f
CD
570 :group 'org-agenda-skip
571 :type 'boolean)
572
20908596 573(defcustom org-agenda-todo-list-sublevels t
ed21c5c8 574 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
20908596
CD
575When nil, the sublevels of a TODO entry are not checked, resulting in
576potentially much shorter TODO lists."
577 :group 'org-agenda-skip
0bd48b37 578 :group 'org-agenda-todo-list
20908596
CD
579 :type 'boolean)
580
581(defcustom org-agenda-todo-ignore-with-date nil
ed21c5c8 582 "Non-nil means don't show entries with a date in the global todo list.
20908596
CD
583You can use this if you prefer to mark mere appointments with a TODO keyword,
584but don't want them to show up in the TODO list.
585When this is set, it also covers deadlines and scheduled items, the settings
586of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
c8d0cf5c
CD
587will be ignored.
588See also the variable `org-agenda-tags-todo-honor-ignore-options'."
20908596 589 :group 'org-agenda-skip
0bd48b37 590 :group 'org-agenda-todo-list
20908596
CD
591 :type 'boolean)
592
acedf35c
CD
593(defcustom org-agenda-todo-ignore-timestamp nil
594 "Non-nil means don't show entries with a timestamp.
595This applies when creating the global todo list.
596Valid values are:
597
598past Don't show entries for today or in the past.
599
600future Don't show entries with a timestamp in the future.
601 The idea behind this is that if it has a future
602 timestamp, you don't want to think about it until the
603 date.
604
605all Don't show any entries with a timestamp in the global todo list.
606 The idea behind this is that by setting a timestamp, you
607 have already \"taken care\" of this item.
608
3ab2c837
BG
609This variable can also have an integer as a value. If positive (N),
610todos with a timestamp N or more days in the future will be ignored. If
611negative (-N), todos with a timestamp N or more days in the past will be
612ignored. If 0, todos with a timestamp either today or in the future will
613be ignored. For example, a value of -1 will exclude todos with a
614timestamp in the past (yesterday or earlier), while a value of 7 will
615exclude todos with a timestamp a week or more in the future.
616
acedf35c
CD
617See also `org-agenda-todo-ignore-with-date'.
618See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
619to make his option also apply to the tags-todo list."
620 :group 'org-agenda-skip
621 :group 'org-agenda-todo-list
622 :type '(choice
623 (const :tag "Ignore future timestamp todos" future)
624 (const :tag "Ignore past or present timestamp todos" past)
625 (const :tag "Ignore all timestamp todos" all)
3ab2c837
BG
626 (const :tag "Show timestamp todos" nil)
627 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
acedf35c 628
20908596 629(defcustom org-agenda-todo-ignore-scheduled nil
ed21c5c8
CD
630 "Non-nil means, ignore some scheduled TODO items when making TODO list.
631This applies when creating the global todo list.
632Valid values are:
633
634past Don't show entries scheduled today or in the past.
635
636future Don't show entries scheduled in the future.
637 The idea behind this is that by scheduling it, you don't want to
638 think about it until the scheduled date.
639
640all Don't show any scheduled entries in the global todo list.
641 The idea behind this is that by scheduling it, you have already
642 \"taken care\" of this item.
643
644t Same as `all', for backward compatibility.
645
3ab2c837
BG
646This variable can also have an integer as a value. See
647`org-agenda-todo-ignore-timestamp' for more details.
648
c8d0cf5c 649See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
650See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
651to make his option also apply to the tags-todo list."
20908596 652 :group 'org-agenda-skip
0bd48b37 653 :group 'org-agenda-todo-list
ed21c5c8
CD
654 :type '(choice
655 (const :tag "Ignore future-scheduled todos" future)
656 (const :tag "Ignore past- or present-scheduled todos" past)
657 (const :tag "Ignore all scheduled todos" all)
658 (const :tag "Ignore all scheduled todos (compatibility)" t)
3ab2c837
BG
659 (const :tag "Show scheduled todos" nil)
660 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
20908596
CD
661
662(defcustom org-agenda-todo-ignore-deadlines nil
ed21c5c8
CD
663 "Non-nil means ignore some deadlined TODO items when making TODO list.
664There are different motivations for using different values, please think
665carefully when configuring this variable.
666
86fbb8ca 667This applies when creating the global todo list.
ed21c5c8
CD
668Valid values are:
669
670near Don't show near deadline entries. A deadline is near when it is
671 closer than `org-deadline-warning-days' days. The idea behind this
672 is that such items will appear in the agenda anyway.
673
674far Don't show TODO entries where a deadline has been defined, but
675 the deadline is not near. This is useful if you don't want to
676 use the todo list to figure out what to do now.
677
678past Don't show entries with a deadline timestamp for today or in the past.
679
680future Don't show entries with a deadline timestamp in the future, not even
681 when they become `near' ones. Use it with caution.
682
683all Ignore all TODO entries that do have a deadline.
684
685t Same as `near', for backward compatibility.
686
3ab2c837
BG
687This variable can also have an integer as a value. See
688`org-agenda-todo-ignore-timestamp' for more details.
689
c8d0cf5c 690See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
691See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
692to make his option also apply to the tags-todo list."
20908596 693 :group 'org-agenda-skip
0bd48b37 694 :group 'org-agenda-todo-list
ed21c5c8
CD
695 :type '(choice
696 (const :tag "Ignore near deadlines" near)
697 (const :tag "Ignore near deadlines (compatibility)" t)
698 (const :tag "Ignore far deadlines" far)
699 (const :tag "Ignore all TODOs with a deadlines" all)
3ab2c837
BG
700 (const :tag "Show all TODOs, even if they have a deadline" nil)
701 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
0bd48b37
CD
702
703(defcustom org-agenda-tags-todo-honor-ignore-options nil
ed21c5c8 704 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
0bd48b37
CD
705The variables
706 `org-agenda-todo-ignore-with-date',
acedf35c
CD
707 `org-agenda-todo-ignore-timestamp',
708 `org-agenda-todo-ignore-scheduled',
0bd48b37
CD
709 `org-agenda-todo-ignore-deadlines'
710make the global TODO list skip entries that have time stamps of certain
711kinds. If this option is set, the same options will also apply for the
712tags-todo search, which is the general tags/property matcher
713restricted to unfinished TODO entries only."
714 :group 'org-agenda-skip
715 :group 'org-agenda-todo-list
716 :group 'org-agenda-match-view
20908596
CD
717 :type 'boolean)
718
719(defcustom org-agenda-skip-scheduled-if-done nil
720 "Non-nil means don't show scheduled items in agenda when they are done.
721This is relevant for the daily/weekly agenda, not for the TODO list. And
722it applies only to the actual date of the scheduling. Warnings about
723an item with a past scheduling dates are always turned off when the item
724is DONE."
725 :group 'org-agenda-skip
0bd48b37 726 :group 'org-agenda-daily/weekly
20908596
CD
727 :type 'boolean)
728
54a0dee5
CD
729(defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
730 "Non-nil means skip scheduling line if same entry shows because of deadline.
731In the agenda of today, an entry can show up multiple times because
732it is both scheduled and has a nearby deadline, and maybe a plain time
733stamp as well.
734When this variable is t, then only the deadline is shown and the fact that
735the entry is scheduled today or was scheduled previously is not shown.
736When this variable is nil, the entry will be shown several times. When
737the variable is the symbol `not-today', then skip scheduled previously,
738but not scheduled today."
739 :group 'org-agenda-skip
740 :group 'org-agenda-daily/weekly
741 :type '(choice
742 (const :tag "Never" nil)
743 (const :tag "Always" t)
744 (const :tag "Not when scheduled today" not-today)))
745
20908596 746(defcustom org-agenda-skip-deadline-if-done nil
33306645 747 "Non-nil means don't show deadlines when the corresponding item is done.
20908596
CD
748When nil, the deadline is still shown and should give you a happy feeling.
749This is relevant for the daily/weekly agenda. And it applied only to the
33306645 750actually date of the deadline. Warnings about approaching and past-due
20908596
CD
751deadlines are always turned off when the item is DONE."
752 :group 'org-agenda-skip
0bd48b37 753 :group 'org-agenda-daily/weekly
20908596
CD
754 :type 'boolean)
755
ed21c5c8
CD
756(defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
757 "Non-nil means skip deadline prewarning when entry is also scheduled.
758This will apply on all days where a prewarning for the deadline would
759be shown, but not at the day when the entry is actually due. On that day,
760the deadline will be shown anyway.
761This variable may be set to nil, t, or a number which will then give
762the number of days before the actual deadline when the prewarnings
763should resume.
764This can be used in a workflow where the first showing of the deadline will
765trigger you to schedule it, and then you don't want to be reminded of it
766because you will take care of it on the day when scheduled."
767 :group 'org-agenda-skip
768 :group 'org-agenda-daily/weekly
769 :type '(choice
770 (const :tag "Alwas show prewarning" nil)
771 (const :tag "Remove prewarning if entry is scheduled" t)
772 (integer :tag "Restart prewarning N days before deadline")))
773
c8d0cf5c
CD
774(defcustom org-agenda-skip-additional-timestamps-same-entry t
775 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
776When non-nil, after the search for timestamps has matched once in an
777entry, the rest of the entry will not be searched."
778 :group 'org-agenda-skip
779 :type 'boolean)
780
20908596
CD
781(defcustom org-agenda-skip-timestamp-if-done nil
782 "Non-nil means don't select item by timestamp or -range if it is DONE."
783 :group 'org-agenda-skip
0bd48b37 784 :group 'org-agenda-daily/weekly
20908596
CD
785 :type 'boolean)
786
d6685abc 787(defcustom org-agenda-dim-blocked-tasks t
ed21c5c8 788 "Non-nil means dim blocked tasks in the agenda display.
c8d0cf5c
CD
789This causes some overhead during agenda construction, but if you
790have turned on `org-enforce-todo-dependencies',
791`org-enforce-todo-checkbox-dependencies', or any other blocking
792mechanism, this will create useful feedback in the agenda.
793
8bfe682a 794Instead of t, this variable can also have the value `invisible'.
c8d0cf5c
CD
795Then blocked tasks will be invisible and only become visible when
796they become unblocked. An exemption to this behavior is when a task is
797blocked because of unchecked checkboxes below it. Since checkboxes do
798not show up in the agenda views, making this task invisible you remove any
799trace from agenda views that there is something to do. Therefore, a task
800that is blocked because of checkboxes will never be made invisible, it
801will only be dimmed."
d6685abc
CD
802 :group 'org-agenda-daily/weekly
803 :group 'org-agenda-todo-list
804 :type '(choice
805 (const :tag "Do not dim" nil)
806 (const :tag "Dim to a grey face" t)
8bfe682a 807 (const :tag "Make invisible" invisible)))
d6685abc 808
20908596 809(defcustom org-timeline-show-empty-dates 3
ed21c5c8 810 "Non-nil means `org-timeline' also shows dates without an entry.
20908596
CD
811When nil, only the days which actually have entries are shown.
812When t, all days between the first and the last date are shown.
813When an integer, show also empty dates, but if there is a gap of more than
814N days, just insert a special line indicating the size of the gap."
815 :group 'org-agenda-skip
816 :type '(choice
817 (const :tag "None" nil)
818 (const :tag "All" t)
c8d0cf5c 819 (integer :tag "at most")))
20908596 820
20908596
CD
821(defgroup org-agenda-startup nil
822 "Options concerning initial settings in the Agenda in Org Mode."
823 :tag "Org Agenda Startup"
824 :group 'org-agenda)
825
afe98dfa 826(defcustom org-agenda-menu-show-matcher t
3ab2c837 827 "Non-nil means show the match string in the agenda dispatcher menu.
afe98dfa
CD
828When nil, the matcher string is not shown, but is put into the help-echo
829property so than moving the mouse over the command shows it.
830Setting it to nil is good if matcher strings are very long and/or if
3ab2c837 831you want to use two-column display (see `org-agenda-menu-two-column')."
afe98dfa
CD
832 :group 'org-agenda
833 :type 'boolean)
834
835(defcustom org-agenda-menu-two-column nil
836 "Non-nil means, use two columns to show custom commands in the dispatcher.
837If you use this, you probably want to set `org-agenda-menu-show-matcher'
838to nil."
839 :group 'org-agenda
840 :type 'boolean)
841
20908596
CD
842(defcustom org-finalize-agenda-hook nil
843 "Hook run just before displaying an agenda buffer."
844 :group 'org-agenda-startup
845 :type 'hook)
846
847(defcustom org-agenda-mouse-1-follows-link nil
ed21c5c8 848 "Non-nil means mouse-1 on a link will follow the link in the agenda.
20908596
CD
849A longer mouse click will still set point. Does not work on XEmacs.
850Needs to be set before org.el is loaded."
851 :group 'org-agenda-startup
852 :type 'boolean)
853
854(defcustom org-agenda-start-with-follow-mode nil
86fbb8ca 855 "The initial value of follow mode in a newly created agenda window."
20908596
CD
856 :group 'org-agenda-startup
857 :type 'boolean)
858
1bcdebed 859(defcustom org-agenda-show-outline-path t
ed21c5c8 860 "Non-nil means show outline path in echo area after line motion."
1bcdebed
CD
861 :group 'org-agenda-startup
862 :type 'boolean)
863
54a0dee5
CD
864(defcustom org-agenda-start-with-entry-text-mode nil
865 "The initial value of entry-text-mode in a newly created agenda window."
866 :group 'org-agenda-startup
867 :type 'boolean)
868
869(defcustom org-agenda-entry-text-maxlines 5
8bfe682a 870 "Number of text lines to be added when `E' is pressed in the agenda.
54a0dee5
CD
871
872Note that this variable only used during agenda display. Add add entry text
873when exporting the agenda, configure the variable
874`org-agenda-add-entry-ext-maxlines'."
875 :group 'org-agenda
876 :type 'integer)
877
8d642074
CD
878(defcustom org-agenda-entry-text-exclude-regexps nil
879 "List of regular expressions to clean up entry text.
880The complete matches of all regular expressions in this list will be
881removed from entry text before it is shown in the agenda."
882 :group 'org-agenda
883 :type '(repeat (regexp)))
884
885(defvar org-agenda-entry-text-cleanup-hook nil
886 "Hook that is run after basic cleanup of entry text to be shown in agenda.
887This cleanup is done in a temporary buffer, so the function may inspect and
888change the entire buffer.
889Some default stuff like drawers and scheduling/deadline dates will already
890have been removed when this is called, as will any matches for regular
891expressions listed in `org-agenda-entry-text-exclude-regexps'.")
892
20908596 893(defvar org-agenda-include-inactive-timestamps nil
ed21c5c8 894 "Non-nil means include inactive time stamps in agenda and timeline.")
20908596
CD
895
896(defgroup org-agenda-windows nil
897 "Options concerning the windows used by the Agenda in Org Mode."
898 :tag "Org Agenda Windows"
899 :group 'org-agenda)
900
901(defcustom org-agenda-window-setup 'reorganize-frame
902 "How the agenda buffer should be displayed.
903Possible values for this option are:
904
905current-window Show agenda in the current window, keeping all other windows.
20908596
CD
906other-window Use `switch-to-buffer-other-window' to display agenda.
907reorganize-frame Show only two windows on the current frame, the current
908 window and the agenda.
8d642074
CD
909other-frame Use `switch-to-buffer-other-frame' to display agenda.
910 Also, when exiting the agenda, kill that frame.
20908596
CD
911See also the variable `org-agenda-restore-windows-after-quit'."
912 :group 'org-agenda-windows
913 :type '(choice
914 (const current-window)
915 (const other-frame)
916 (const other-window)
917 (const reorganize-frame)))
918
919(defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
920 "The min and max height of the agenda window as a fraction of frame height.
921The value of the variable is a cons cell with two numbers between 0 and 1.
922It only matters if `org-agenda-window-setup' is `reorganize-frame'."
923 :group 'org-agenda-windows
924 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
925
926(defcustom org-agenda-restore-windows-after-quit nil
3ab2c837 927 "Non-nil means restore window configuration upon exiting agenda.
20908596
CD
928Before the window configuration is changed for displaying the agenda,
929the current status is recorded. When the agenda is exited with
930`q' or `x' and this option is set, the old state is restored. If
931`org-agenda-window-setup' is `other-frame', the value of this
baf0cb84 932option will be ignored."
20908596
CD
933 :group 'org-agenda-windows
934 :type 'boolean)
935
acedf35c
CD
936(defcustom org-agenda-ndays nil
937 "Number of days to include in overview display.
c8d0cf5c 938Should be 1 or 7.
acedf35c
CD
939Obsolete, see `org-agenda-span'."
940 :group 'org-agenda-daily/weekly
941 :type 'integer)
942
943(make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
944
945(defcustom org-agenda-span 'week
946 "Number of days to include in overview display.
947Can be day, week, month, year, or any number of days.
c8d0cf5c 948Custom commands can set this variable in the options section."
20908596 949 :group 'org-agenda-daily/weekly
acedf35c
CD
950 :type '(choice (const :tag "Day" day)
951 (const :tag "Week" week)
952 (const :tag "Month" month)
953 (const :tag "Year" year)
954 (integer :tag "Custom")))
20908596
CD
955
956(defcustom org-agenda-start-on-weekday 1
ed21c5c8 957 "Non-nil means start the overview always on the specified weekday.
20908596 9580 denotes Sunday, 1 denotes Monday etc.
c8d0cf5c
CD
959When nil, always start on the current day.
960Custom commands can set this variable in the options section."
20908596
CD
961 :group 'org-agenda-daily/weekly
962 :type '(choice (const :tag "Today" nil)
c8d0cf5c 963 (integer :tag "Weekday No.")))
20908596
CD
964
965(defcustom org-agenda-show-all-dates t
ed21c5c8 966 "Non-nil means `org-agenda' shows every day in the selected range.
20908596
CD
967When nil, only the days which actually have entries are shown."
968 :group 'org-agenda-daily/weekly
969 :type 'boolean)
970
971(defcustom org-agenda-format-date 'org-agenda-format-date-aligned
972 "Format string for displaying dates in the agenda.
973Used by the daily/weekly agenda and by the timeline. This should be
974a format string understood by `format-time-string', or a function returning
975the formatted date as a string. The function must take a single argument,
976a calendar-style date list like (month day year)."
977 :group 'org-agenda-daily/weekly
978 :type '(choice
979 (string :tag "Format string")
980 (function :tag "Function")))
981
982(defun org-agenda-format-date-aligned (date)
983 "Format a date string for display in the daily/weekly agenda, or timeline.
984This function makes sure that dates are aligned for easy reading."
985 (require 'cal-iso)
986 (let* ((dayname (calendar-day-name date))
987 (day (cadr date))
988 (day-of-week (calendar-day-of-week date))
989 (month (car date))
990 (monthname (calendar-month-name month))
991 (year (nth 2 date))
992 (iso-week (org-days-to-iso-week
993 (calendar-absolute-from-gregorian date)))
994 (weekyear (cond ((and (= month 1) (>= iso-week 52))
995 (1- year))
996 ((and (= month 12) (<= iso-week 1))
997 (1+ year))
998 (t year)))
999 (weekstring (if (= day-of-week 1)
1000 (format " W%02d" iso-week)
1001 "")))
1002 (format "%-10s %2d %s %4d%s"
1003 dayname day monthname year weekstring)))
1004
ed21c5c8
CD
1005(defcustom org-agenda-time-leading-zero nil
1006 "Non-nil means use leading zero for military times in agenda.
1007For example, 9:30am would become 09:30 rather than 9:30."
1008 :group 'org-agenda-daily/weekly
1009 :type 'boolean)
1010
acedf35c
CD
1011(defcustom org-agenda-timegrid-use-ampm nil
1012 "When set, show AM/PM style timestamps on the timegrid."
1013 :group 'org-agenda
1014 :type 'boolean)
1015
1016(defun org-agenda-time-of-day-to-ampm (time)
1017 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1018 (let* ((hour-number (string-to-number (substring time 0 -3)))
1019 (minute (substring time -2))
1020 (ampm "am"))
1021 (cond
1022 ((equal hour-number 12)
1023 (setq ampm "pm"))
1024 ((> hour-number 12)
1025 (setq ampm "pm")
1026 (setq hour-number (- hour-number 12))))
1027 (concat
1028 (if org-agenda-time-leading-zero
1029 (format "%02d" hour-number)
1030 (format "%02s" (number-to-string hour-number)))
1031 ":" minute ampm)))
1032
1033(defun org-agenda-time-of-day-to-ampm-maybe (time)
1034 "Conditionally convert TIME to AM/PM format
1035based on `org-agenda-timegrid-use-ampm'"
1036 (if org-agenda-timegrid-use-ampm
1037 (org-agenda-time-of-day-to-ampm time)
1038 time))
1039
20908596
CD
1040(defcustom org-agenda-weekend-days '(6 0)
1041 "Which days are weekend?
1042These days get the special face `org-agenda-date-weekend' in the agenda
1043and timeline buffers."
1044 :group 'org-agenda-daily/weekly
1045 :type '(set :greedy t
1046 (const :tag "Monday" 1)
1047 (const :tag "Tuesday" 2)
1048 (const :tag "Wednesday" 3)
1049 (const :tag "Thursday" 4)
1050 (const :tag "Friday" 5)
1051 (const :tag "Saturday" 6)
1052 (const :tag "Sunday" 0)))
1053
1054(defcustom org-agenda-include-diary nil
c8d0cf5c
CD
1055 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1056Custom commands can set this variable in the options section."
20908596
CD
1057 :group 'org-agenda-daily/weekly
1058 :type 'boolean)
1059
ed21c5c8
CD
1060(defcustom org-agenda-include-deadlines t
1061 "If non-nil, include entries within their deadline warning period.
1062Custom commands can set this variable in the options section."
1063 :group 'org-agenda-daily/weekly
1064 :type 'boolean)
1065
20908596
CD
1066(defcustom org-agenda-include-all-todo nil
1067 "Set means weekly/daily agenda will always contain all TODO entries.
1068The TODO entries will be listed at the top of the agenda, before
0bd48b37
CD
1069the entries for specific days.
1070This option is deprecated, it is better to define a block agenda instead."
20908596
CD
1071 :group 'org-agenda-daily/weekly
1072 :type 'boolean)
1073
1074(defcustom org-agenda-repeating-timestamp-show-all t
ed21c5c8 1075 "Non-nil means show all occurrences of a repeating stamp in the agenda.
3ab2c837
BG
1076When set to a list of strings, only show occurrences of repeating
1077stamps for these TODO keywords. When nil, only one occurrence is
1078shown, either today or the nearest into the future."
20908596 1079 :group 'org-agenda-daily/weekly
3ab2c837
BG
1080 :type '(choice
1081 (const :tag "Show repeating stamps" t)
1082 (repeat :tag "Show repeating stamps for these TODO keywords"
1083 (string :tag "TODO Keyword"))
1084 (const :tag "Don't show repeating stamps" nil)))
20908596
CD
1085
1086(defcustom org-scheduled-past-days 10000
1087 "No. of days to continue listing scheduled items that are not marked DONE.
1088When an item is scheduled on a date, it shows up in the agenda on this
1089day and will be listed until it is marked done for the number of days
1090given here."
1091 :group 'org-agenda-daily/weekly
c8d0cf5c 1092 :type 'integer)
20908596 1093
93b62de8
CD
1094(defcustom org-agenda-log-mode-items '(closed clock)
1095 "List of items that should be shown in agenda log mode.
1096This list may contain the following symbols:
1097
1098 closed Show entries that have been closed on that day.
1099 clock Show entries that have received clocked time on that day.
c8d0cf5c
CD
1100 state Show all logged state changes.
1101Note that instead of changing this variable, you can also press `C-u l' in
1102the agenda to display all available LOG items temporarily."
93b62de8
CD
1103 :group 'org-agenda-daily/weekly
1104 :type '(set :greedy t (const closed) (const clock) (const state)))
1105
3ab2c837
BG
1106(defcustom org-agenda-clock-consistency-checks
1107 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1108 :gap-ok-around ("4:00")
1109 :default-face ((:background "DarkRed") (:foreground "white"))
1110 :overlap-face nil :gap-face nil :no-end-time-face nil
1111 :long-face nil :short-face nil)
1112 "This is a property list, with the following keys:
1113
1114:max-duration Mark clocking chunks that are longer than this time.
1115 This is a time string like \"HH:MM\", or the number
1116 of minutes as an integer.
1117
1118:min-duration Mark clocking chunks that are shorter that this.
1119 This is a time string like \"HH:MM\", or the number
1120 of minutes as an integer.
1121
1122:max-gap Mark gaps between clocking chunks that are longer than
1123 this duration. A number of minutes, or a string
1124 like \"HH:MM\".
1125
1126:gap-ok-around List of times during the day which are usually not working
1127 times. When a gap is detected, but the gap contains any
1128 of these times, the gap is *not* reported. For example,
1129 if this is (\"4:00\" \"13:00\") then gaps that contain
1130 4:00 in the morning (i.e. the night) and 13:00
1131 (i.e. a typical lunch time) do not cause a warning.
1132 You should have at least one time during the night in this
1133 list, or otherwise the first task each morning will trigger
1134 a warning because it follows a long gap.
1135
1136Furthermore, the following properties can be used to define faces for
1137issue display.
1138
1139:default-face the default face, if the specific face is undefined
1140:overlap-face face for overlapping clocks
1141:gap-face face for gaps between clocks
1142:no-end-time-face face for incomplete clocks
1143:long-face face for clock intervals that are too long
1144:short-face face for clock intervals that are too short"
1145 :group 'org-agenda-daily/weekly
1146 :group 'org-clock
1147 :type 'plist)
1148
c8d0cf5c 1149(defcustom org-agenda-log-mode-add-notes t
ed21c5c8 1150 "Non-nil means add first line of notes to log entries in agenda views.
c8d0cf5c
CD
1151If a log item like a state change or a clock entry is associated with
1152notes, the first line of these notes will be added to the entry in the
1153agenda display."
1154 :group 'org-agenda-daily/weekly
1155 :type 'boolean)
1156
1157(defcustom org-agenda-start-with-log-mode nil
1158 "The initial value of log-mode in a newly created agenda window."
1159 :group 'org-agenda-startup
1160 :group 'org-agenda-daily/weekly
1161 :type 'boolean)
1162
20908596
CD
1163(defcustom org-agenda-start-with-clockreport-mode nil
1164 "The initial value of clockreport-mode in a newly created agenda window."
1165 :group 'org-agenda-startup
1166 :group 'org-agenda-daily/weekly
1167 :type 'boolean)
1168
1169(defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1170 "Property list with parameters for the clocktable in clockreport mode.
1171This is the display mode that shows a clock table in the daily/weekly
1172agenda, the properties for this dynamic block can be set here.
1173The usual clocktable parameters are allowed here, but you cannot set
1174the properties :name, :tstart, :tend, :block, and :scope - these will
1175be overwritten to make sure the content accurately reflects the
1176current display in the agenda."
1177 :group 'org-agenda-daily/weekly
1178 :type 'plist)
1179
ed21c5c8
CD
1180(defcustom org-agenda-search-view-always-boolean nil
1181 "Non-nil means the search string is interpreted as individual parts.
1182
1183The search string for search view can either be interpreted as a phrase,
1184or as a list of snippets that define a boolean search for a number of
1185strings.
1186
1187When this is non-nil, the string will be split on whitespace, and each
1188snippet will be searched individually, and all must match in order to
1189select an entry. A snippet is then a single string of non-white
1190characters, or a string in double quotes, or a regexp in {} braces.
86fbb8ca 1191If a snippet is preceded by \"-\", the snippet must *not* match.
ed21c5c8
CD
1192\"+\" is syntactic sugar for positive selection. Each snippet may
1193be found as a full word or a partial word, but see the variable
1194`org-agenda-search-view-force-full-words'.
1195
1196When this is nil, search will look for the entire search phrase as one,
1197with each space character matching any amount of whitespace, including
1198line breaks.
1199
1200Even when this is nil, you can still switch to Boolean search dynamically
86fbb8ca 1201by preceding the first snippet with \"+\" or \"-\". If the first snippet
ed21c5c8
CD
1202is a regexp marked with braces like \"{abc}\", this will also switch to
1203boolean search."
1204 :group 'org-agenda-search-view
1205 :type 'boolean)
1206
1207(if (fboundp 'defvaralias)
1208 (defvaralias 'org-agenda-search-view-search-words-only
1209 'org-agenda-search-view-always-boolean))
1210
1211(defcustom org-agenda-search-view-force-full-words nil
86fbb8ca 1212 "Non-nil means, search words must be matches as complete words.
ed21c5c8 1213When nil, they may also match part of a word."
8bfe682a
CD
1214 :group 'org-agenda-search-view
1215 :type 'boolean)
20908596
CD
1216
1217(defgroup org-agenda-time-grid nil
1218 "Options concerning the time grid in the Org-mode Agenda."
1219 :tag "Org Agenda Time Grid"
1220 :group 'org-agenda)
1221
c8d0cf5c 1222(defcustom org-agenda-search-headline-for-time t
ed21c5c8 1223 "Non-nil means search headline for a time-of-day.
c8d0cf5c
CD
1224If the headline contains a time-of-day in one format or another, it will
1225be used to sort the entry into the time sequence of items for a day.
1226Some people have time stamps in the headline that refer to the creation
1227time or so, and then this produces an unwanted side effect. If this is
1228the case for your, use this variable to turn off searching the headline
1229for a time."
1230 :group 'org-agenda-time-grid
1231 :type 'boolean)
1232
20908596 1233(defcustom org-agenda-use-time-grid t
ed21c5c8 1234 "Non-nil means show a time grid in the agenda schedule.
20908596
CD
1235A time grid is a set of lines for specific times (like every two hours between
12368:00 and 20:00). The items scheduled for a day at specific times are
1237sorted in between these lines.
1238For details about when the grid will be shown, and what it will look like, see
1239the variable `org-agenda-time-grid'."
1240 :group 'org-agenda-time-grid
1241 :type 'boolean)
1242
1243(defcustom org-agenda-time-grid
1244 '((daily today require-timed)
1245 "----------------"
1246 (800 1000 1200 1400 1600 1800 2000))
1247
1248 "The settings for time grid for agenda display.
1249This is a list of three items. The first item is again a list. It contains
1250symbols specifying conditions when the grid should be displayed:
1251
1252 daily if the agenda shows a single day
1253 weekly if the agenda shows an entire week
1254 today show grid on current date, independent of daily/weekly display
1255 require-timed show grid only if at least one item has a time specification
1256
b349f79f 1257The second item is a string which will be placed behind the grid time.
20908596
CD
1258
1259The third item is a list of integers, indicating the times that should have
1260a grid line."
1261 :group 'org-agenda-time-grid
1262 :type
1263 '(list
1264 (set :greedy t :tag "Grid Display Options"
1265 (const :tag "Show grid in single day agenda display" daily)
1266 (const :tag "Show grid in weekly agenda display" weekly)
1267 (const :tag "Always show grid for today" today)
1268 (const :tag "Show grid only if any timed entries are present"
1269 require-timed)
1270 (const :tag "Skip grid times already present in an entry"
1271 remove-match))
1272 (string :tag "Grid String")
1273 (repeat :tag "Grid Times" (integer :tag "Time"))))
1274
3ab2c837
BG
1275(defcustom org-agenda-show-current-time-in-grid t
1276 "Non-nil means show the current time in the time grid."
1277 :group 'org-agenda-time-grid
1278 :type 'boolean)
1279
1280(defcustom org-agenda-current-time-string
1281 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1282 "The string for the current time marker in the agenda."
1283 :group 'org-agenda-time-grid
1284 :type 'string)
1285
20908596
CD
1286(defgroup org-agenda-sorting nil
1287 "Options concerning sorting in the Org-mode Agenda."
1288 :tag "Org Agenda Sorting"
1289 :group 'org-agenda)
1290
1291(defcustom org-agenda-sorting-strategy
8bfe682a
CD
1292 '((agenda habit-down time-up priority-down category-keep)
1293 (todo priority-down category-keep)
1294 (tags priority-down category-keep)
20908596
CD
1295 (search category-keep))
1296 "Sorting structure for the agenda items of a single day.
1297This is a list of symbols which will be used in sequence to determine
1298if an entry should be listed before another entry. The following
1299symbols are recognized:
1300
c8d0cf5c
CD
1301time-up Put entries with time-of-day indications first, early first
1302time-down Put entries with time-of-day indications first, late first
1303category-keep Keep the default order of categories, corresponding to the
1304 sequence in `org-agenda-files'.
1305category-up Sort alphabetically by category, A-Z.
1306category-down Sort alphabetically by category, Z-A.
1307tag-up Sort alphabetically by last tag, A-Z.
1308tag-down Sort alphabetically by last tag, Z-A.
1309priority-up Sort numerically by priority, high priority last.
1310priority-down Sort numerically by priority, high priority first.
1311todo-state-up Sort by todo state, tasks that are done last.
1312todo-state-down Sort by todo state, tasks that are done first.
1313effort-up Sort numerically by estimated effort, high effort last.
1314effort-down Sort numerically by estimated effort, high effort first.
1315user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1316user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
8bfe682a
CD
1317habit-up Put entries that are habits first
1318habit-down Put entries that are habits last
86fbb8ca
CD
1319alpha-up Sort headlines alphabetically
1320alpha-down Sort headlines alphabetically, reversed
20908596
CD
1321
1322The different possibilities will be tried in sequence, and testing stops
1323if one comparison returns a \"not-equal\". For example, the default
1324 '(time-up category-keep priority-down)
1325means: Pull out all entries having a specified time of day and sort them,
1326in order to make a time schedule for the current day the first thing in the
1327agenda listing for the day. Of the entries without a time indication, keep
1328the grouped in categories, don't sort the categories, but keep them in
1329the sequence given in `org-agenda-files'. Within each category sort by
1330priority.
1331
1332Leaving out `category-keep' would mean that items will be sorted across
1333categories by priority.
1334
1335Instead of a single list, this can also be a set of list for specific
1336contents, with a context symbol in the car of the list, any of
8bfe682a 1337`agenda', `todo', `tags', `search' for the corresponding agenda views.
c8d0cf5c
CD
1338
1339Custom commands can bind this variable in the options section."
20908596
CD
1340 :group 'org-agenda-sorting
1341 :type `(choice
1342 (repeat :tag "General" ,org-sorting-choice)
1343 (list :tag "Individually"
1344 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1345 (repeat ,org-sorting-choice))
1346 (cons (const :tag "Strategy for TODO lists" todo)
1347 (repeat ,org-sorting-choice))
1348 (cons (const :tag "Strategy for Tags matches" tags)
8bfe682a
CD
1349 (repeat ,org-sorting-choice))
1350 (cons (const :tag "Strategy for search matches" search)
20908596
CD
1351 (repeat ,org-sorting-choice)))))
1352
c8d0cf5c
CD
1353(defcustom org-agenda-cmp-user-defined nil
1354 "A function to define the comparison `user-defined'.
1355This function must receive two arguments, agenda entry a and b.
1356If a>b, return +1. If a<b, return -1. If they are equal as seen by
1357the user comparison, return nil.
1358When this is defined, you can make `user-defined-up' and `user-defined-down'
1359part of an agenda sorting strategy."
1360 :group 'org-agenda-sorting
1361 :type 'symbol)
1362
20908596 1363(defcustom org-sort-agenda-notime-is-late t
ed21c5c8 1364 "Non-nil means items without time are considered late.
20908596
CD
1365This is only relevant for sorting. When t, items which have no explicit
1366time like 15:30 will be considered as 99:01, i.e. later than any items which
1367do have a time. When nil, the default time is before 0:00. You can use this
1368option to decide if the schedule for today should come before or after timeless
1369agenda entries."
1370 :group 'org-agenda-sorting
1371 :type 'boolean)
1372
1373(defcustom org-sort-agenda-noeffort-is-high t
ed21c5c8 1374 "Non-nil means items without effort estimate are sorted as high effort.
c8d0cf5c
CD
1375This also applies when filtering an agenda view with respect to the
1376< or > effort operator. Then, tasks with no effort defined will be treated
1377as tasks with high effort.
20908596
CD
1378When nil, such items are sorted as 0 minutes effort."
1379 :group 'org-agenda-sorting
1380 :type 'boolean)
1381
1382(defgroup org-agenda-line-format nil
1383 "Options concerning the entry prefix in the Org-mode agenda display."
1384 :tag "Org Agenda Line Format"
1385 :group 'org-agenda)
1386
1387(defcustom org-agenda-prefix-format
acedf35c 1388 '((agenda . " %i %-12:c%?-12t% s")
20908596 1389 (timeline . " % s")
acedf35c
CD
1390 (todo . " %i %-12:c")
1391 (tags . " %i %-12:c")
1392 (search . " %i %-12:c"))
20908596 1393 "Format specifications for the prefix of items in the agenda views.
3ab2c837
BG
1394An alist with five entries, each for the different agenda types. The
1395keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1396The values are format strings.
1397
20908596
CD
1398This format works similar to a printf format, with the following meaning:
1399
3ab2c837
BG
1400 %c the category of the item, \"Diary\" for entries from the diary,
1401 or as given by the CATEGORY keyword or derived from the file name
1402 %i the icon category of the item, see `org-agenda-category-icon-alist'
1403 %T the last tag of the item (ignore inherited tags, which come first)
1404 %t the HH:MM time-of-day specification if one applies to the entry
20908596 1405 %s Scheduling/Deadline information, a short string
3ab2c837
BG
1406 %(expression) Eval EXPRESSION and replace the control string
1407 by the result
20908596
CD
1408
1409All specifiers work basically like the standard `%s' of printf, but may
3ab2c837
BG
1410contain two additional characters: a question mark just after the `%'
1411and a whitespace/punctuation character just before the final letter.
20908596
CD
1412
1413If the first character after `%' is a question mark, the entire field
3ab2c837
BG
1414will only be included if the corresponding value applies to the current
1415entry. This is useful for fields which should have fixed width when
1416present, but zero width when absent. For example, \"%?-12t\" will
1417result in a 12 character time field if a time of the day is specified,
1418but will completely disappear in entries which do not contain a time.
20908596
CD
1419
1420If there is punctuation or whitespace character just before the final
1421format letter, this character will be appended to the field value if
1422the value is not empty. For example, the format \"%-12:c\" leads to
1423\"Diary: \" if the category is \"Diary\". If the category were be
8bfe682a 1424empty, no additional colon would be inserted.
20908596 1425
3ab2c837
BG
1426The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1427which means:
1428
20908596 1429- Indent the line with two space characters
3ab2c837 1430- Give the category a 12 chars wide field, padded with whitespace on
20908596
CD
1431 the right (because of `-'). Append a colon if there is a category
1432 (because of `:').
1433- If there is a time-of-day, put it into a 12 chars wide field. If no
1434 time, don't put in an empty field, just skip it (because of '?').
3ab2c837 1435- Finally, put the scheduling information.
20908596
CD
1436
1437See also the variables `org-agenda-remove-times-when-in-prefix' and
c8d0cf5c
CD
1438`org-agenda-remove-tags'.
1439
1440Custom commands can set this variable in the options section."
20908596
CD
1441 :type '(choice
1442 (string :tag "General format")
1443 (list :greedy t :tag "View dependent"
1444 (cons (const agenda) (string :tag "Format"))
1445 (cons (const timeline) (string :tag "Format"))
1446 (cons (const todo) (string :tag "Format"))
1447 (cons (const tags) (string :tag "Format"))
1448 (cons (const search) (string :tag "Format"))))
1449 :group 'org-agenda-line-format)
1450
1451(defvar org-prefix-format-compiled nil
1452 "The compiled version of the most recently used prefix format.
1453See the variable `org-agenda-prefix-format'.")
1454
1455(defcustom org-agenda-todo-keyword-format "%-1s"
1456 "Format for the TODO keyword in agenda lines.
1457Set this to something like \"%-12s\" if you want all TODO keywords
1458to occupy a fixed space in the agenda display."
1459 :group 'org-agenda-line-format
1460 :type 'string)
1461
ce4fdcb9
CD
1462(defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1463 "Text preceding timerange entries in the agenda view.
1464This is a list with two strings. The first applies when the range
1465is entirely on one day. The second applies if the range spans several days.
1466The strings may have two \"%d\" format specifiers which will be filled
1467with the sequence number of the days, and the total number of days in the
1468range, respectively."
1469 :group 'org-agenda-line-format
1470 :type '(list
1471 (string :tag "Deadline today ")
1472 (choice :tag "Deadline relative"
1473 (string :tag "Format string")
1474 (function))))
1475
20908596 1476(defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
86fbb8ca 1477 "Text preceding scheduled items in the agenda view.
20908596
CD
1478This is a list with two strings. The first applies when the item is
1479scheduled on the current day. The second applies when it has been scheduled
b349f79f
CD
1480previously, it may contain a %d indicating that this is the nth time that
1481this item is scheduled, due to automatic rescheduling of unfinished items
1482for the following day. So this number is one larger than the number of days
1483that passed since this item was scheduled first."
20908596
CD
1484 :group 'org-agenda-line-format
1485 :type '(list
1486 (string :tag "Scheduled today ")
1487 (string :tag "Scheduled previously")))
1488
ed21c5c8 1489(defcustom org-agenda-inactive-leader "["
86fbb8ca 1490 "Text preceding item pulled into the agenda by inactive time stamps.
ed21c5c8
CD
1491These entries are added to the agenda when pressing \"[\"."
1492 :group 'org-agenda-line-format
1493 :type '(list
1494 (string :tag "Scheduled today ")
1495 (string :tag "Scheduled previously")))
1496
20908596 1497(defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
86fbb8ca 1498 "Text preceding deadline items in the agenda view.
20908596
CD
1499This is a list with two strings. The first applies when the item has its
1500deadline on the current day. The second applies when it is in the past or
1501in the future, it may contain %d to capture how many days away the deadline
1502is (was)."
1503 :group 'org-agenda-line-format
1504 :type '(list
1505 (string :tag "Deadline today ")
1506 (choice :tag "Deadline relative"
1507 (string :tag "Format string")
1508 (function))))
1509
1510(defcustom org-agenda-remove-times-when-in-prefix t
ed21c5c8 1511 "Non-nil means remove duplicate time specifications in agenda items.
20908596
CD
1512When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1513time-of-day specification in a headline or diary entry is extracted and
1514placed into the prefix. If this option is non-nil, the original specification
1515\(a timestamp or -range, or just a plain time(range) specification like
151611:30-4pm) will be removed for agenda display. This makes the agenda less
1517cluttered.
1518The option can be t or nil. It may also be the symbol `beg', indicating
86fbb8ca 1519that the time should only be removed when it is located at the beginning of
20908596
CD
1520the headline/diary entry."
1521 :group 'org-agenda-line-format
1522 :type '(choice
1523 (const :tag "Always" t)
1524 (const :tag "Never" nil)
1525 (const :tag "When at beginning of entry" beg)))
1526
86fbb8ca
CD
1527(defcustom org-agenda-remove-timeranges-from-blocks nil
1528 "Non-nil means remove time ranges specifications in agenda
1529items that span on several days."
1530 :group 'org-agenda-line-format
1531 :type 'boolean)
20908596
CD
1532
1533(defcustom org-agenda-default-appointment-duration nil
1534 "Default duration for appointments that only have a starting time.
1535When nil, no duration is specified in such cases.
1536When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1537 :group 'org-agenda-line-format
1538 :type '(choice
1539 (integer :tag "Minutes")
1540 (const :tag "No default duration")))
1541
ff4be292 1542(defcustom org-agenda-show-inherited-tags t
ed21c5c8 1543 "Non-nil means show inherited tags in each agenda line."
ff4be292
CD
1544 :group 'org-agenda-line-format
1545 :type 'boolean)
20908596 1546
5dec9555
CD
1547(defcustom org-agenda-hide-tags-regexp nil
1548 "Regular expression used to filter away specific tags in agenda views.
1549This means that these tags will be present, but not be shown in the agenda
86fbb8ca 1550line. Secondary filtering will still work on the hidden tags.
afe98dfa 1551Nil means don't hide any tags."
5dec9555
CD
1552 :group 'org-agenda-line-format
1553 :type '(choice
1554 (const :tag "Hide none" nil)
1555 (string :tag "Regexp ")))
1556
20908596 1557(defcustom org-agenda-remove-tags nil
ed21c5c8 1558 "Non-nil means remove the tags from the headline copy in the agenda.
20908596
CD
1559When this is the symbol `prefix', only remove tags when
1560`org-agenda-prefix-format' contains a `%T' specifier."
1561 :group 'org-agenda-line-format
1562 :type '(choice
1563 (const :tag "Always" t)
1564 (const :tag "Never" nil)
1565 (const :tag "When prefix format contains %T" prefix)))
1566
1567(if (fboundp 'defvaralias)
1568 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1569 'org-agenda-remove-tags))
1570
5ace2fe5 1571(defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
20908596
CD
1572 "Shift tags in agenda items to this column.
1573If this number is positive, it specifies the column. If it is negative,
1574it means that the tags should be flushright to that column. For example,
1575-80 works well for a normal 80 character screen."
1576 :group 'org-agenda-line-format
1577 :type 'integer)
1578
1579(if (fboundp 'defvaralias)
1580 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1581
c8d0cf5c 1582(defcustom org-agenda-fontify-priorities 'cookies
ed21c5c8 1583 "Non-nil means highlight low and high priorities in agenda.
20908596 1584When t, the highest priority entries are bold, lowest priority italic.
86fbb8ca 1585However, settings in `org-priority-faces' will overrule these faces.
c8d0cf5c
CD
1586When this variable is the symbol `cookies', only fontify the
1587cookies, not the entire task.
621f83e4
CD
1588This may also be an association list of priority faces, whose
1589keys are the character values of `org-highest-priority',
1590`org-default-priority', and `org-lowest-priority' (the default values
ed21c5c8
CD
1591are ?A, ?B, and ?C, respectively). The face may be a named face, a
1592color as a string, or a list like `(:background \"Red\")'.
1593If it is a color, the variable `org-faces-easy-properties'
1594determines if it is a foreground or a background color."
20908596
CD
1595 :group 'org-agenda-line-format
1596 :type '(choice
1597 (const :tag "Never" nil)
1598 (const :tag "Defaults" t)
c8d0cf5c 1599 (const :tag "Cookies only" cookies)
20908596
CD
1600 (repeat :tag "Specify"
1601 (list (character :tag "Priority" :value ?A)
ed21c5c8
CD
1602 (choice :tag "Face "
1603 (string :tag "Color")
1604 (sexp :tag "Face"))))))
20908596 1605
acedf35c
CD
1606(defcustom org-agenda-day-face-function nil
1607 "Function called to determine what face should be used to display a day.
1608The only argument passed to that function is the day. It should
1609returns a face, or nil if does not want to specify a face and let
1610the normal rules apply."
1611 :group 'org-agenda-line-format
1612 :type 'function)
1613
1614(defcustom org-agenda-category-icon-alist nil
1615 "Alist of category icon to be displayed in agenda views.
1616
1617Each entry should have the following format:
1618
1619 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1620
1621Where CATEGORY-REGEXP is a regexp matching the categories where
1622the icon should be displayed.
1623FILE-OR-DATA either a file path or a string containing image data.
1624
3ab2c837 1625The other fields can be omited safely if not needed:
acedf35c
CD
1626TYPE indicates the image type.
1627DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1628image data.
1629PROPS are additional image attributes to assign to the image,
1630like, e.g. `:ascent center'.
1631
1632 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1633
1634If you want to set the display properties yourself, just put a
1635list as second element:
1636
1637 (CATEGORY-REGEXP (MY PROPERTY LIST))
1638
1639For example, to display a 16px horizontal space for Emacs
1640category, you can use:
1641
1642 (\"Emacs\" '(space . (:width (16))))"
1643 :group 'org-agenda-line-format
1644 :type '(alist :key-type (string :tag "Regexp matching category")
1645 :value-type (choice (list :tag "Icon"
1646 (string :tag "File or data")
1647 (symbol :tag "Type")
1648 (boolean :tag "Data?")
1649 (repeat :tag "Extra image properties" :inline t symbol))
1650 (list :tag "Display properties" sexp))))
1651
20908596
CD
1652(defgroup org-agenda-column-view nil
1653 "Options concerning column view in the agenda."
1654 :tag "Org Agenda Column View"
1655 :group 'org-agenda)
1656
1657(defcustom org-agenda-columns-show-summaries t
ed21c5c8 1658 "Non-nil means show summaries for columns displayed in the agenda view."
20908596
CD
1659 :group 'org-agenda-column-view
1660 :type 'boolean)
1661
1662(defcustom org-agenda-columns-compute-summary-properties t
ed21c5c8 1663 "Non-nil means recompute all summary properties before column view.
20908596
CD
1664When column view in the agenda is listing properties that have a summary
1665operator, it can go to all relevant buffers and recompute the summaries
1666there. This can mean overhead for the agenda column view, but is necessary
1667to have thing up to date.
1668As a special case, a CLOCKSUM property also makes sure that the clock
1669computations are current."
1670 :group 'org-agenda-column-view
1671 :type 'boolean)
1672
1673(defcustom org-agenda-columns-add-appointments-to-effort-sum nil
ed21c5c8 1674 "Non-nil means the duration of an appointment will add to day effort.
20908596
CD
1675The property to which appointment durations will be added is the one given
1676in the option `org-effort-property'. If an appointment does not have
1677an end time, `org-agenda-default-appointment-duration' will be used. If that
1678is not set, an appointment without end time will not contribute to the time
1679estimate."
1680 :group 'org-agenda-column-view
1681 :type 'boolean)
1682
8bfe682a
CD
1683(defcustom org-agenda-auto-exclude-function nil
1684 "A function called with a tag to decide if it is filtered on '/ RET'.
1685The sole argument to the function, which is called once for each
1686possible tag, is a string giving the name of the tag. The
1687function should return either nil if the tag should be included
ed21c5c8
CD
1688as normal, or \"-<TAG>\" to exclude the tag.
1689Note that for the purpose of tag filtering, only the lower-case version of
1690all tags will be considered, so that this function will only ever see
1691the lower-case version of all tags."
8bfe682a
CD
1692 :group 'org-agenda
1693 :type 'function)
1694
3ab2c837
BG
1695(defcustom org-agenda-bulk-custom-functions nil
1696 "Alist of characters and custom functions for bulk actions.
1697For example, this value makes those two functions available:
1698
1699 '((?R set-category)
1700 (?C bulk-cut))
1701
1702With selected entries in an agenda buffer, `B R' will call
1703the custom function `set-category' on the selected entries.
1704Note that functions in this alist don't need to be quoted."
1705 :type 'alist
1706 :group 'org-agenda)
1707
20908596
CD
1708(eval-when-compile
1709 (require 'cl))
1710(require 'org)
1711
afe98dfa
CD
1712(defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1713 "Execute BODY with point at location given by `org-hd-marker' property.
1714If STRING is non-nil, the text property will be fetched from position 0
1715in that string. If STRING is nil, it will be fetched from the beginning
1716of the current line."
1717 `(let ((marker (get-text-property (if string 0 (point-at-bol))
1718 'org-hd-marker string)))
1719 (with-current-buffer (marker-buffer marker)
1720 (save-excursion
1721 (goto-char marker)
1722 ,@body))))
1723
20908596
CD
1724(defun org-add-agenda-custom-command (entry)
1725 "Replace or add a command in `org-agenda-custom-commands'.
1726This is mostly for hacking and trying a new command - once the command
1727works you probably want to add it to `org-agenda-custom-commands' for good."
1728 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1729 (if ass
1730 (setcdr ass (cdr entry))
1731 (push entry org-agenda-custom-commands))))
1732
1733;;; Define the Org-agenda-mode
1734
1735(defvar org-agenda-mode-map (make-sparse-keymap)
1736 "Keymap for `org-agenda-mode'.")
8bfe682a
CD
1737(if (fboundp 'defvaralias)
1738 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
20908596
CD
1739
1740(defvar org-agenda-menu) ; defined later in this file.
c8d0cf5c 1741(defvar org-agenda-restrict) ; defined later in this file.
20908596 1742(defvar org-agenda-follow-mode nil)
54a0dee5 1743(defvar org-agenda-entry-text-mode nil)
20908596
CD
1744(defvar org-agenda-clockreport-mode nil)
1745(defvar org-agenda-show-log nil)
1746(defvar org-agenda-redo-command nil)
1747(defvar org-agenda-query-string nil)
0bd48b37 1748(defvar org-agenda-mode-hook nil
86fbb8ca 1749 "Hook for `org-agenda-mode', run after the mode is turned on.")
20908596
CD
1750(defvar org-agenda-type nil)
1751(defvar org-agenda-force-single-file nil)
c8d0cf5c 1752(defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
20908596
CD
1753
1754(defun org-agenda-mode ()
1755 "Mode for time-sorted view on action items in Org-mode files.
1756
1757The following commands are available:
1758
1759\\{org-agenda-mode-map}"
1760 (interactive)
1761 (kill-all-local-variables)
1762 (setq org-agenda-undo-list nil
c8d0cf5c
CD
1763 org-agenda-pending-undo-list nil
1764 org-agenda-bulk-marked-entries nil)
20908596
CD
1765 (setq major-mode 'org-agenda-mode)
1766 ;; Keep global-font-lock-mode from turning on font-lock-mode
1767 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1768 (setq mode-name "Org-Agenda")
1769 (use-local-map org-agenda-mode-map)
1770 (easy-menu-add org-agenda-menu)
1771 (if org-startup-truncated (setq truncate-lines t))
54a0dee5 1772 (org-set-local 'line-move-visual nil)
20908596
CD
1773 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1774 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1775 ;; Make sure properties are removed when copying text
1776 (when (boundp 'buffer-substring-filters)
1777 (org-set-local 'buffer-substring-filters
1778 (cons (lambda (x)
1779 (set-text-properties 0 (length x) nil x) x)
1780 buffer-substring-filters)))
1781 (unless org-agenda-keep-modes
1782 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
54a0dee5 1783 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
20908596 1784 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
c8d0cf5c
CD
1785 org-agenda-show-log org-agenda-start-with-log-mode))
1786
20908596
CD
1787 (easy-menu-change
1788 '("Agenda") "Agenda Files"
1789 (append
1790 (list
1791 (vector
1792 (if (get 'org-agenda-files 'org-restrict)
1793 "Restricted to single file"
1794 "Edit File List")
1795 '(org-edit-agenda-file-list)
1796 (not (get 'org-agenda-files 'org-restrict)))
1797 "--")
1798 (mapcar 'org-file-menu-entry (org-agenda-files))))
1799 (org-agenda-set-mode-name)
1800 (apply
1801 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1802 (list 'org-agenda-mode-hook)))
1803
1804(substitute-key-definition 'undo 'org-agenda-undo
1805 org-agenda-mode-map global-map)
1806(org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1807(org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1808(org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1809(org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
c8d0cf5c
CD
1810(org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1811(org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
3ab2c837 1812(org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
c8d0cf5c
CD
1813(org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1814(org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
3ab2c837 1815(org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
c8d0cf5c
CD
1816(org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1817(org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
8bfe682a
CD
1818(org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1819(org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1820(org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
54a0dee5 1821(org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
8bfe682a 1822(org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
20908596 1823(org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
20908596 1824(org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
8bfe682a
CD
1825(org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1826(org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1827(org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
20908596
CD
1828(org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1829(org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1830(org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
20908596
CD
1831(org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1832(org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
54a0dee5 1833(org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
20908596 1834(org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
8bfe682a 1835(org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
20908596 1836(org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
71d35b24 1837(org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
20908596
CD
1838(org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1839(org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1840(org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1841(org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
20908596
CD
1842(org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1843(org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1844(org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
b349f79f
CD
1845(org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1846(org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
c8d0cf5c
CD
1847(org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1848(org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1849(org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1850(org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
20908596
CD
1851
1852(org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1853(org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1854(org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1855(let ((l '(1 2 3 4 5 6 7 8 9 0)))
1856 (while l (org-defkey org-agenda-mode-map
1857 (int-to-string (pop l)) 'digit-argument)))
1858
54a0dee5 1859(org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
20908596 1860(org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
54a0dee5 1861(org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
20908596 1862(org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
c8d0cf5c 1863(org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
20908596 1864(org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
ed21c5c8 1865(org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
20908596
CD
1866(org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1867(org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1868(org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
54a0dee5
CD
1869(org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1870(org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1871(org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1872 'org-clock-modify-effort-estimate)
1873(org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
20908596
CD
1874(org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1875(org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1876(org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
20908596 1877(org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
c8d0cf5c 1878(org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
20908596
CD
1879(org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1880(org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
8bfe682a
CD
1881(org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1882(org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1883(substitute-key-definition 'next-line 'org-agenda-next-line
1884 org-agenda-mode-map global-map)
1885(substitute-key-definition 'previous-line 'org-agenda-previous-line
1886 org-agenda-mode-map global-map)
621f83e4 1887(org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
20908596
CD
1888(org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1889(org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1890(org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1891(org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1892(org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1893(org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1894(org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1895(org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1896(org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1897(org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1898(org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1899(org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1900(org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1901(org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1902(org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1903(org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1904(org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1905(org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
afe98dfa 1906(org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
20908596
CD
1907(org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1908(org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1909(org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1910(org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1911(org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1912(org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
54a0dee5
CD
1913(org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1914(org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
20908596 1915(org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
c8d0cf5c 1916(org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
20908596
CD
1917
1918(org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1919(org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1920(org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1921(org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
621f83e4 1922(org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
71d35b24 1923(org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
c8d0cf5c 1924(org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
8d642074
CD
1925(define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1926(org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1927(org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
20908596 1928
86fbb8ca
CD
1929(org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1930(org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
20908596 1931(when org-agenda-mouse-1-follows-link
8bfe682a 1932 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
20908596
CD
1933(easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1934 '("Agenda"
1935 ("Agenda Files")
1936 "--"
8d642074
CD
1937 ("Agenda Dates"
1938 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1939 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1940 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1941 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1942 "--"
1943 ("View"
1944 ["Day View" org-agenda-day-view
1945 :active (org-agenda-check-type nil 'agenda)
acedf35c 1946 :style radio :selected (eq org-agenda-current-span 'day)
8d642074
CD
1947 :keys "v d (or just d)"]
1948 ["Week View" org-agenda-week-view
1949 :active (org-agenda-check-type nil 'agenda)
acedf35c 1950 :style radio :selected (eq org-agenda-current-span 'week)
8d642074
CD
1951 :keys "v w (or just w)"]
1952 ["Month View" org-agenda-month-view
1953 :active (org-agenda-check-type nil 'agenda)
acedf35c 1954 :style radio :selected (eq org-agenda-current-span 'month)
8d642074
CD
1955 :keys "v m"]
1956 ["Year View" org-agenda-year-view
1957 :active (org-agenda-check-type nil 'agenda)
acedf35c 1958 :style radio :selected (eq org-agenda-current-span 'year)
8d642074
CD
1959 :keys "v y"]
1960 "--"
1961 ["Include Diary" org-agenda-toggle-diary
1962 :style toggle :selected org-agenda-include-diary
1963 :active (org-agenda-check-type nil 'agenda)]
ed21c5c8
CD
1964 ["Include Deadlines" org-agenda-toggle-deadlines
1965 :style toggle :selected org-agenda-include-deadlines
1966 :active (org-agenda-check-type nil 'agenda)]
8d642074
CD
1967 ["Use Time Grid" org-agenda-toggle-time-grid
1968 :style toggle :selected org-agenda-use-time-grid
1969 :active (org-agenda-check-type nil 'agenda)]
1970 "--"
1971 ["Show clock report" org-agenda-clockreport-mode
1972 :style toggle :selected org-agenda-clockreport-mode
1973 :active (org-agenda-check-type nil 'agenda)]
1974 ["Show some entry text" org-agenda-entry-text-mode
1975 :style toggle :selected org-agenda-entry-text-mode
1976 :active t]
1977 "--"
1978 ["Show Logbook entries" org-agenda-log-mode
1979 :style toggle :selected org-agenda-show-log
1980 :active (org-agenda-check-type nil 'agenda 'timeline)
1981 :keys "v l (or just l)"]
1982 ["Include archived trees" org-agenda-archives-mode
1983 :style toggle :selected org-agenda-archives-mode :active t
1984 :keys "v a"]
1985 ["Include archive files" (org-agenda-archives-mode t)
1986 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1987 :keys "v A"]
1988 "--"
1989 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1990 ["Write view to file" org-write-agenda t]
1991 ["Rebuild buffer" org-agenda-redo t]
1992 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1993 "--"
1994 ["Show original entry" org-agenda-show t]
20908596
CD
1995 ["Go To (other window)" org-agenda-goto t]
1996 ["Go To (this window)" org-agenda-switch-to t]
1997 ["Follow Mode" org-agenda-follow-mode
1998 :style toggle :selected org-agenda-follow-mode :active t]
8d642074 1999; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
20908596 2000 "--"
8d642074
CD
2001 ("TODO"
2002 ["Cycle TODO" org-agenda-todo t]
2003 ["Next TODO set" org-agenda-todo-nextset t]
2004 ["Previous TODO set" org-agenda-todo-previousset t]
2005 ["Add note" org-agenda-add-note t])
2006 ("Archive/Refile/Delete"
8bfe682a
CD
2007 ["Archive default" org-agenda-archive-default t]
2008 ["Archive default" org-agenda-archive-default-with-confirmation t]
20908596
CD
2009 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2010 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
c8d0cf5c 2011 ["Archive subtree" org-agenda-archive t]
8d642074
CD
2012 "--"
2013 ["Refile" org-agenda-refile t]
2014 "--"
2015 ["Delete subtree" org-agenda-kill t])
c8d0cf5c 2016 ("Bulk action"
8d642074 2017 ["Mark entry" org-agenda-bulk-mark t]
3ab2c837 2018 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
8d642074 2019 ["Unmark entry" org-agenda-bulk-unmark t]
c8d0cf5c 2020 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
3ab2c837 2021 ["Act on all marked" org-agenda-bulk-action t]
c8d0cf5c 2022 "--"
20908596
CD
2023 ("Tags and Properties"
2024 ["Show all Tags" org-agenda-show-tags t]
2025 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2026 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2027 "--"
2028 ["Column View" org-columns t])
8d642074 2029 ("Deadline/Schedule"
20908596
CD
2030 ["Schedule" org-agenda-schedule t]
2031 ["Set Deadline" org-agenda-deadline t]
2032 "--"
b349f79f
CD
2033 ["Mark item" org-agenda-action :active t :keys "k m"]
2034 ["Show mark item" org-agenda-action :active t :keys "k v"]
2035 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2036 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2037 "--"
20908596
CD
2038 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2039 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
c8d0cf5c
CD
2040 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2041 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2042 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2043 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
20908596 2044 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
54a0dee5 2045 ("Clock and Effort"
20908596
CD
2046 ["Clock in" org-agenda-clock-in t]
2047 ["Clock out" org-agenda-clock-out t]
2048 ["Clock cancel" org-agenda-clock-cancel t]
54a0dee5
CD
2049 ["Goto running clock" org-clock-goto t]
2050 "--"
2051 ["Set Effort" org-agenda-set-effort t]
2052 ["Change clocked effort" org-clock-modify-effort-estimate
2053 (org-clock-is-active)])
20908596
CD
2054 ("Priority"
2055 ["Set Priority" org-agenda-priority t]
2056 ["Increase Priority" org-agenda-priority-up t]
2057 ["Decrease Priority" org-agenda-priority-down t]
2058 ["Show Priority" org-agenda-show-priority t])
2059 ("Calendar/Diary"
2060 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2061 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2062 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2063 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2064 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2065 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2066 "--"
8d642074 2067 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
20908596 2068 "--"
8d642074 2069 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2c3ad40d 2070 "--"
8d642074
CD
2071 ("MobileOrg"
2072 ["Push Files and Views" org-mobile-push t]
2073 ["Get Captured and Flagged" org-mobile-pull t]
2074 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2075 ["Show note / unflag" org-agenda-show-the-flagging-note t]
c8d0cf5c 2076 "--"
8d642074 2077 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
20908596
CD
2078 "--"
2079 ["Quit" org-agenda-quit t]
2080 ["Exit and Release Buffers" org-agenda-exit t]
2081 ))
2082
2083;;; Agenda undo
2084
2085(defvar org-agenda-allow-remote-undo t
ed21c5c8 2086 "Non-nil means allow remote undo from the agenda buffer.")
20908596
CD
2087(defvar org-agenda-undo-list nil
2088 "List of undoable operations in the agenda since last refresh.")
2089(defvar org-agenda-undo-has-started-in nil
2090 "Buffers that have already seen `undo-start' in the current undo sequence.")
2091(defvar org-agenda-pending-undo-list nil
33306645 2092 "In a series of undo commands, this is the list of remaining undo items.")
20908596 2093
20908596
CD
2094(defun org-agenda-undo ()
2095 "Undo a remote editing step in the agenda.
2096This undoes changes both in the agenda buffer and in the remote buffer
2097that have been changed along."
2098 (interactive)
2099 (or org-agenda-allow-remote-undo
f924a367 2100 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
20908596
CD
2101 (if (not (eq this-command last-command))
2102 (setq org-agenda-undo-has-started-in nil
2103 org-agenda-pending-undo-list org-agenda-undo-list))
2104 (if (not org-agenda-pending-undo-list)
2105 (error "No further undo information"))
2106 (let* ((entry (pop org-agenda-pending-undo-list))
2107 buf line cmd rembuf)
2108 (setq cmd (pop entry) line (pop entry))
2109 (setq rembuf (nth 2 entry))
2110 (org-with-remote-undo rembuf
2111 (while (bufferp (setq buf (pop entry)))
2112 (if (pop entry)
2113 (with-current-buffer buf
2114 (let ((last-undo-buffer buf)
2115 (inhibit-read-only t))
2116 (unless (memq buf org-agenda-undo-has-started-in)
2117 (push buf org-agenda-undo-has-started-in)
2118 (make-local-variable 'pending-undo-list)
2119 (undo-start))
2120 (while (and pending-undo-list
2121 (listp pending-undo-list)
2122 (not (car pending-undo-list)))
2123 (pop pending-undo-list))
2124 (undo-more 1))))))
54a0dee5 2125 (org-goto-line line)
20908596
CD
2126 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2127
2128(defun org-verify-change-for-undo (l1 l2)
2129 "Verify that a real change occurred between the undo lists L1 and L2."
2130 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2131 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2132 (not (eq l1 l2)))
2133
2134;;; Agenda dispatch
2135
2136(defvar org-agenda-restrict nil)
2137(defvar org-agenda-restrict-begin (make-marker))
2138(defvar org-agenda-restrict-end (make-marker))
2139(defvar org-agenda-last-dispatch-buffer nil)
2140(defvar org-agenda-overriding-restriction nil)
2141
2142;;;###autoload
c8d0cf5c 2143(defun org-agenda (&optional arg keys restriction)
20908596
CD
2144 "Dispatch agenda commands to collect entries to the agenda buffer.
2145Prompts for a command to execute. Any prefix arg will be passed
2146on to the selected command. The default selections are:
2147
2148a Call `org-agenda-list' to display the agenda for current day or week.
2149t Call `org-todo-list' to display the global todo list.
2150T Call `org-todo-list' to display the global todo list, select only
2151 entries with a specific TODO keyword (the user gets a prompt).
2152m Call `org-tags-view' to display headlines with tags matching
2153 a condition (the user is prompted for the condition).
2154M Like `m', but select only TODO entries, no ordinary headlines.
2155L Create a timeline for the current buffer.
2156e Export views to associated files.
c8d0cf5c 2157s Search entries for keywords.
8bfe682a 2158/ Multi occur across all agenda files and also files listed
c8d0cf5c
CD
2159 in `org-agenda-text-search-extra-files'.
2160< Restrict agenda commands to buffer, subtree, or region.
2161 Press several times to get the desired effect.
2162> Remove a previous restriction.
2163# List \"stuck\" projects.
2164! Configure what \"stuck\" means.
2165C Configure custom agenda commands.
20908596
CD
2166
2167More commands can be added by configuring the variable
2168`org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2169searches can be pre-defined in this way.
2170
2171If the current buffer is in Org-mode and visiting a file, you can also
2172first press `<' once to indicate that the agenda should be temporarily
2173\(until the next use of \\[org-agenda]) restricted to the current file.
2174Pressing `<' twice means to restrict to the current subtree or region
2175\(if active)."
2176 (interactive "P")
2177 (catch 'exit
2178 (let* ((prefix-descriptions nil)
54a0dee5
CD
2179 (org-agenda-window-setup (if (equal (buffer-name)
2180 org-agenda-buffer-name)
2181 'current-window
2182 org-agenda-window-setup))
20908596
CD
2183 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2184 (org-agenda-custom-commands
2185 ;; normalize different versions
2186 (delq nil
2187 (mapcar
2188 (lambda (x)
2189 (cond ((stringp (cdr x))
2190 (push x prefix-descriptions)
2191 nil)
2192 ((stringp (nth 1 x)) x)
2193 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2194 (t (cons (car x) (cons "" (cdr x))))))
2195 org-agenda-custom-commands)))
2196 (buf (current-buffer))
2197 (bfn (buffer-file-name (buffer-base-buffer)))
2198 entry key type match lprops ans)
8d642074 2199 ;; Turn off restriction unless there is an overriding one,
20908596 2200 (unless org-agenda-overriding-restriction
8bfe682a 2201 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
8d642074
CD
2202 ;; There is a request to keep the file list in place
2203 (put 'org-agenda-files 'org-restrict nil))
20908596
CD
2204 (setq org-agenda-restrict nil)
2205 (move-marker org-agenda-restrict-begin nil)
2206 (move-marker org-agenda-restrict-end nil))
2207 ;; Delete old local properties
2208 (put 'org-agenda-redo-command 'org-lprops nil)
3ab2c837
BG
2209 ;; Delete previously set last-arguments
2210 (put 'org-agenda-redo-command 'last-args nil)
20908596
CD
2211 ;; Remember where this call originated
2212 (setq org-agenda-last-dispatch-buffer (current-buffer))
2213 (unless keys
2214 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2215 keys (car ans)
2216 restriction (cdr ans)))
8bfe682a 2217 ;; Establish the restriction, if any
20908596
CD
2218 (when (and (not org-agenda-overriding-restriction) restriction)
2219 (put 'org-agenda-files 'org-restrict (list bfn))
2220 (cond
2221 ((eq restriction 'region)
2222 (setq org-agenda-restrict t)
2223 (move-marker org-agenda-restrict-begin (region-beginning))
2224 (move-marker org-agenda-restrict-end (region-end)))
2225 ((eq restriction 'subtree)
2226 (save-excursion
2227 (setq org-agenda-restrict t)
2228 (org-back-to-heading t)
2229 (move-marker org-agenda-restrict-begin (point))
2230 (move-marker org-agenda-restrict-end
2231 (progn (org-end-of-subtree t)))))))
2232
20908596
CD
2233 ;; For example the todo list should not need it (but does...)
2234 (cond
2235 ((setq entry (assoc keys org-agenda-custom-commands))
2236 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2237 (progn
8bfe682a
CD
2238 (setq type (nth 2 entry) match (eval (nth 3 entry))
2239 lprops (nth 4 entry))
20908596
CD
2240 (put 'org-agenda-redo-command 'org-lprops lprops)
2241 (cond
2242 ((eq type 'agenda)
2243 (org-let lprops '(org-agenda-list current-prefix-arg)))
2244 ((eq type 'alltodo)
2245 (org-let lprops '(org-todo-list current-prefix-arg)))
2246 ((eq type 'search)
2247 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2248 ((eq type 'stuck)
2249 (org-let lprops '(org-agenda-list-stuck-projects
2250 current-prefix-arg)))
2251 ((eq type 'tags)
2252 (org-let lprops '(org-tags-view current-prefix-arg match)))
2253 ((eq type 'tags-todo)
2254 (org-let lprops '(org-tags-view '(4) match)))
2255 ((eq type 'todo)
2256 (org-let lprops '(org-todo-list match)))
2257 ((eq type 'tags-tree)
2258 (org-check-for-org-mode)
c8d0cf5c 2259 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
20908596
CD
2260 ((eq type 'todo-tree)
2261 (org-check-for-org-mode)
2262 (org-let lprops
3ab2c837 2263 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
20908596
CD
2264 (regexp-quote match) "\\>"))))
2265 ((eq type 'occur-tree)
2266 (org-check-for-org-mode)
2267 (org-let lprops '(org-occur match)))
2268 ((functionp type)
2269 (org-let lprops '(funcall type match)))
2270 ((fboundp type)
2271 (org-let lprops '(funcall type match)))
2272 (t (error "Invalid custom agenda command type %s" type))))
3ab2c837 2273 (org-agenda-run-series (nth 1 entry) (cddr entry))))
20908596
CD
2274 ((equal keys "C")
2275 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2276 (customize-variable 'org-agenda-custom-commands))
2277 ((equal keys "a") (call-interactively 'org-agenda-list))
2278 ((equal keys "s") (call-interactively 'org-search-view))
2279 ((equal keys "t") (call-interactively 'org-todo-list))
2280 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2281 ((equal keys "m") (call-interactively 'org-tags-view))
2282 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2283 ((equal keys "e") (call-interactively 'org-store-agenda-views))
8d642074
CD
2284 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2285 (org-add-hook
2286 'post-command-hook
2287 (lambda ()
2288 (unless (current-message)
2289 (let* ((m (org-agenda-get-any-marker))
2290 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2291 (when note
2292 (message (concat
2293 "FLAGGING-NOTE ([?] for more info): "
2294 (org-add-props
2295 (replace-regexp-in-string
2296 "\\\\n" "//"
2297 (copy-sequence note))
2298 nil 'face 'org-warning)))))))
2299 t t))
20908596
CD
2300 ((equal keys "L")
2301 (unless (org-mode-p)
2302 (error "This is not an Org-mode file"))
2303 (unless restriction
2304 (put 'org-agenda-files 'org-restrict (list bfn))
2305 (org-call-with-arg 'org-timeline arg)))
2306 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2307 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2308 ((equal keys "!") (customize-variable 'org-stuck-projects))
2309 (t (error "Invalid agenda key"))))))
2310
3ab2c837
BG
2311(defun org-agenda-append-agenda ()
2312 "Append another agenda view to the current one.
2313This function allows interactive building of block agendas.
2314Agenda views are separated by `org-agenda-block-separator'."
2315 (interactive)
2316 (unless (string= (buffer-name) org-agenda-buffer-name)
2317 (error "Can only append from within agenda buffer"))
2318 (let ((org-agenda-multi t))
2319 (org-agenda)
2320 (widen)))
2321
20908596
CD
2322(defun org-agenda-normalize-custom-commands (cmds)
2323 (delq nil
2324 (mapcar
2325 (lambda (x)
2326 (cond ((stringp (cdr x)) nil)
2327 ((stringp (nth 1 x)) x)
2328 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2329 (t (cons (car x) (cons "" (cdr x))))))
2330 cmds)))
2331
2332(defun org-agenda-get-restriction-and-command (prefix-descriptions)
2333 "The user interface for selecting an agenda command."
2334 (catch 'exit
2335 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2336 (restrict-ok (and bfn (org-mode-p)))
2337 (region-p (org-region-active-p))
2338 (custom org-agenda-custom-commands)
2339 (selstring "")
2340 restriction second-time
afe98dfa
CD
2341 c entry key type match prefixes rmheader header-end custom1 desc
2342 line lines left right n n1)
20908596
CD
2343 (save-window-excursion
2344 (delete-other-windows)
2345 (org-switch-to-buffer-other-window " *Agenda Commands*")
2346 (erase-buffer)
2347 (insert (eval-when-compile
2348 (let ((header
2349"
2c3ad40d 2350Press key for an agenda command: < Buffer, subtree/region restriction
20908596
CD
2351-------------------------------- > Remove restriction
2352a Agenda for current week or day e Export agenda views
2353t List of all TODO entries T Entries with special TODO kwd
621f83e4 2354m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
20908596
CD
2355L Timeline for current buffer # List stuck projects (!=configure)
2356s Search for keywords C Configure custom agenda commands
8d642074 2357/ Multi-occur ? Find :FLAGGED: entries
20908596
CD
2358")
2359 (start 0))
2360 (while (string-match
2361 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2362 header start)
2363 (setq start (match-end 0))
2364 (add-text-properties (match-beginning 2) (match-end 2)
2365 '(face bold) header))
2366 header)))
2367 (setq header-end (move-marker (make-marker) (point)))
2368 (while t
2369 (setq custom1 custom)
2370 (when (eq rmheader t)
54a0dee5 2371 (org-goto-line 1)
20908596
CD
2372 (re-search-forward ":" nil t)
2373 (delete-region (match-end 0) (point-at-eol))
2374 (forward-char 1)
2375 (looking-at "-+")
2376 (delete-region (match-end 0) (point-at-eol))
2377 (move-marker header-end (match-end 0)))
2378 (goto-char header-end)
2379 (delete-region (point) (point-max))
afe98dfa
CD
2380
2381 ;; Produce all the lines that describe custom commands and prefixes
2382 (setq lines nil)
20908596
CD
2383 (while (setq entry (pop custom1))
2384 (setq key (car entry) desc (nth 1 entry)
54a0dee5
CD
2385 type (nth 2 entry)
2386 match (nth 3 entry))
20908596
CD
2387 (if (> (length key) 1)
2388 (add-to-list 'prefixes (string-to-char key))
afe98dfa
CD
2389 (setq line
2390 (format
2391 "%-4s%-14s"
2392 (org-add-props (copy-sequence key)
2393 '(face bold))
2394 (cond
2395 ((string-match "\\S-" desc) desc)
2396 ((eq type 'agenda) "Agenda for current week or day")
2397 ((eq type 'alltodo) "List of all TODO entries")
2398 ((eq type 'search) "Word search")
2399 ((eq type 'stuck) "List of stuck projects")
2400 ((eq type 'todo) "TODO keyword")
2401 ((eq type 'tags) "Tags query")
2402 ((eq type 'tags-todo) "Tags (TODO)")
2403 ((eq type 'tags-tree) "Tags tree")
2404 ((eq type 'todo-tree) "TODO kwd tree")
2405 ((eq type 'occur-tree) "Occur tree")
2406 ((functionp type) (if (symbolp type)
2407 (symbol-name type)
2408 "Lambda expression"))
2409 (t "???"))))
2410 (if org-agenda-menu-show-matcher
2411 (setq line
2412 (concat line ": "
2413 (cond
2414 ((stringp match)
2415 (setq match (copy-sequence match))
2416 (org-add-props match nil 'face 'org-warning))
2417 (match
2418 (format "set of %d commands" (length match)))
2419 (t ""))))
2420 (if (org-string-nw-p match)
2421 (add-text-properties
2422 0 (length line) (list 'help-echo
2423 (concat "Matcher: "match)) line)))
2424 (push line lines)))
2425 (setq lines (nreverse lines))
20908596
CD
2426 (when prefixes
2427 (mapc (lambda (x)
afe98dfa
CD
2428 (push
2429 (format "%s %s"
20908596 2430 (org-add-props (char-to-string x)
afe98dfa
CD
2431 nil 'face 'bold)
2432 (or (cdr (assoc (concat selstring
2433 (char-to-string x))
20908596 2434 prefix-descriptions))
afe98dfa
CD
2435 "Prefix key"))
2436 lines))
20908596 2437 prefixes))
afe98dfa
CD
2438
2439 ;; Check if we should display in two columns
2440 (if org-agenda-menu-two-column
2441 (progn
2442 (setq n (length lines)
2443 n1 (+ (/ n 2) (mod n 2))
2444 right (nthcdr n1 lines)
2445 left (copy-sequence lines))
2446 (setcdr (nthcdr (1- n1) left) nil))
2447 (setq left lines right nil))
2448 (while left
2449 (insert "\n" (pop left))
2450 (when right
2451 (if (< (current-column) 40)
2452 (move-to-column 40 t)
2453 (insert " "))
2454 (insert (pop right))))
2455
2456 ;; Make the window the right size
20908596 2457 (goto-char (point-min))
93b62de8
CD
2458 (if second-time
2459 (if (not (pos-visible-in-window-p (point-max)))
2460 (org-fit-window-to-buffer))
2461 (setq second-time t)
2462 (org-fit-window-to-buffer))
afe98dfa
CD
2463
2464 ;; Ask for selection
20908596
CD
2465 (message "Press key for agenda command%s:"
2466 (if (or restrict-ok org-agenda-overriding-restriction)
2467 (if org-agenda-overriding-restriction
2468 " (restriction lock active)"
2469 (if restriction
2470 (format " (restricted to %s)" restriction)
2471 " (unrestricted)"))
2472 ""))
2473 (setq c (read-char-exclusive))
2474 (message "")
2475 (cond
2476 ((assoc (char-to-string c) custom)
2477 (setq selstring (concat selstring (char-to-string c)))
2478 (throw 'exit (cons selstring restriction)))
2479 ((memq c prefixes)
2480 (setq selstring (concat selstring (char-to-string c))
2481 prefixes nil
2482 rmheader (or rmheader t)
2483 custom (delq nil (mapcar
2484 (lambda (x)
2485 (if (or (= (length (car x)) 1)
2486 (/= (string-to-char (car x)) c))
2487 nil
2488 (cons (substring (car x) 1) (cdr x))))
2489 custom))))
2490 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2491 (message "Restriction is only possible in Org-mode buffers")
2492 (ding) (sit-for 1))
2493 ((eq c ?1)
2494 (org-agenda-remove-restriction-lock 'noupdate)
2495 (setq restriction 'buffer))
2496 ((eq c ?0)
2497 (org-agenda-remove-restriction-lock 'noupdate)
2498 (setq restriction (if region-p 'region 'subtree)))
2499 ((eq c ?<)
2500 (org-agenda-remove-restriction-lock 'noupdate)
2501 (setq restriction
2502 (cond
2503 ((eq restriction 'buffer)
2504 (if region-p 'region 'subtree))
2505 ((memq restriction '(subtree region))
2506 nil)
2507 (t 'buffer))))
2508 ((eq c ?>)
2509 (org-agenda-remove-restriction-lock 'noupdate)
2510 (setq restriction nil))
8d642074 2511 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
20908596
CD
2512 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2513 ((and (> (length selstring) 0) (eq c ?\d))
2514 (delete-window)
2515 (org-agenda-get-restriction-and-command prefix-descriptions))
2516
2517 ((equal c ?q) (error "Abort"))
2518 (t (error "Invalid key %c" c))))))))
2519
3ab2c837
BG
2520(defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2521(defvar org-agenda-last-arguments nil
2522 "The arguments of the previous call to `org-agenda'.")
2523(defun org-agenda-run-series (name series)
c8d0cf5c 2524 (org-let (nth 1 series) '(org-prepare-agenda name))
20908596 2525 (let* ((org-agenda-multi t)
3ab2c837
BG
2526 (redo (list 'org-agenda-run-series name (list 'quote series)))
2527 (org-agenda-overriding-arguments
2528 (or org-agenda-overriding-arguments
2529 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2530 (get 'org-agenda-redo-command 'last-args))))
20908596
CD
2531 (cmds (car series))
2532 (gprops (nth 1 series))
2533 match ;; The byte compiler incorrectly complains about this. Keep it!
2534 cmd type lprops)
2535 (while (setq cmd (pop cmds))
8bfe682a 2536 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
20908596
CD
2537 (cond
2538 ((eq type 'agenda)
2539 (org-let2 gprops lprops
2540 '(call-interactively 'org-agenda-list)))
2541 ((eq type 'alltodo)
2542 (org-let2 gprops lprops
2543 '(call-interactively 'org-todo-list)))
2544 ((eq type 'search)
2545 (org-let2 gprops lprops
2546 '(org-search-view current-prefix-arg match nil)))
2547 ((eq type 'stuck)
2548 (org-let2 gprops lprops
2549 '(call-interactively 'org-agenda-list-stuck-projects)))
2550 ((eq type 'tags)
2551 (org-let2 gprops lprops
2552 '(org-tags-view current-prefix-arg match)))
2553 ((eq type 'tags-todo)
2554 (org-let2 gprops lprops
2555 '(org-tags-view '(4) match)))
2556 ((eq type 'todo)
2557 (org-let2 gprops lprops
2558 '(org-todo-list match)))
2559 ((fboundp type)
2560 (org-let2 gprops lprops
2561 '(funcall type match)))
2562 (t (error "Invalid type in command series"))))
2563 (widen)
2564 (setq org-agenda-redo-command redo)
3ab2c837 2565 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
20908596 2566 (goto-char (point-min)))
c8d0cf5c 2567 (org-fit-agenda-window)
0bd48b37 2568 (org-let (nth 1 series) '(org-finalize-agenda)))
20908596
CD
2569
2570;;;###autoload
2571(defmacro org-batch-agenda (cmd-key &rest parameters)
2572 "Run an agenda command in batch mode and send the result to STDOUT.
2573If CMD-KEY is a string of length 1, it is used as a key in
2574`org-agenda-custom-commands' and triggers this command. If it is a
2575longer string it is used as a tags/todo match string.
86fbb8ca 2576Parameters are alternating variable names and values that will be bound
20908596
CD
2577before running the agenda command."
2578 (let (pars)
2579 (while parameters
2580 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2581 (if (> (length cmd-key) 2)
2582 (eval (list 'let (nreverse pars)
2583 (list 'org-tags-view nil cmd-key)))
2584 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2585 (set-buffer org-agenda-buffer-name)
2586 (princ (org-encode-for-stdout (buffer-string)))))
2587
8bfe682a
CD
2588;(defun org-encode-for-stdout (string)
2589; (if (fboundp 'encode-coding-string)
2590; (encode-coding-string string buffer-file-coding-system)
2591; string))
2592
20908596 2593(defun org-encode-for-stdout (string)
8bfe682a 2594 string)
20908596
CD
2595
2596(defvar org-agenda-info nil)
2597
2598;;;###autoload
2599(defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2600 "Run an agenda command in batch mode and send the result to STDOUT.
2601If CMD-KEY is a string of length 1, it is used as a key in
2602`org-agenda-custom-commands' and triggers this command. If it is a
2603longer string it is used as a tags/todo match string.
86fbb8ca 2604Parameters are alternating variable names and values that will be bound
20908596
CD
2605before running the agenda command.
2606
2607The output gives a line for each selected agenda item. Each
2608item is a list of comma-separated values, like this:
2609
2610category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2611
2612category The category of the item
2613head The headline, without TODO kwd, TAGS and PRIORITY
2614type The type of the agenda entry, can be
2615 todo selected in TODO match
2616 tagsmatch selected in tags match
2617 diary imported from diary
2618 deadline a deadline on given date
2619 scheduled scheduled on given date
2620 timestamp entry has timestamp on given date
2621 closed entry was closed on given date
2622 upcoming-deadline warning about deadline
2623 past-scheduled forwarded scheduled item
2624 block entry has date block including g. date
2625todo The todo keyword, if any
2626tags All tags including inherited ones, separated by colons
2627date The relevant date, like 2007-2-14
2628time The time, like 15:00-16:50
2629extra Sting with extra planning info
2630priority-l The priority letter if any was given
2631priority-n The computed numerical priority
2632agenda-day The day in the agenda where this is listed"
2633
2634 (let (pars)
2635 (while parameters
2636 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2637 (push (list 'org-agenda-remove-tags t) pars)
2638 (if (> (length cmd-key) 2)
2639 (eval (list 'let (nreverse pars)
2640 (list 'org-tags-view nil cmd-key)))
2641 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2642 (set-buffer org-agenda-buffer-name)
2643 (let* ((lines (org-split-string (buffer-string) "\n"))
2644 line)
2645 (while (setq line (pop lines))
2646 (catch 'next
2647 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2648 (setq org-agenda-info
2649 (org-fix-agenda-info (text-properties-at 0 line)))
2650 (princ
2651 (org-encode-for-stdout
2652 (mapconcat 'org-agenda-export-csv-mapper
ed21c5c8 2653 '(org-category txt type todo tags date time extra
20908596
CD
2654 priority-letter priority agenda-day)
2655 ",")))
2656 (princ "\n"))))))
2657
2658(defun org-fix-agenda-info (props)
86fbb8ca
CD
2659 "Make sure all properties on an agenda item have a canonical form.
2660This ensures the export commands can easily use it."
20908596
CD
2661 (let (tmp re)
2662 (when (setq tmp (plist-get props 'tags))
2663 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2664 (when (setq tmp (plist-get props 'date))
2665 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2666 (let ((calendar-date-display-form '(year "-" month "-" day)))
2667 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2668
2669 (setq tmp (calendar-date-string tmp)))
2670 (setq props (plist-put props 'date tmp)))
2671 (when (setq tmp (plist-get props 'day))
2672 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2673 (let ((calendar-date-display-form '(year "-" month "-" day)))
2674 (setq tmp (calendar-date-string tmp)))
2675 (setq props (plist-put props 'day tmp))
2676 (setq props (plist-put props 'agenda-day tmp)))
2677 (when (setq tmp (plist-get props 'txt))
2678 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2679 (plist-put props 'priority-letter (match-string 1 tmp))
2680 (setq tmp (replace-match "" t t tmp)))
2681 (when (and (setq re (plist-get props 'org-todo-regexp))
2682 (setq re (concat "\\`\\.*" re " ?"))
2683 (string-match re tmp))
2684 (plist-put props 'todo (match-string 1 tmp))
2685 (setq tmp (replace-match "" t t tmp)))
2686 (plist-put props 'txt tmp)))
2687 props)
2688
2689(defun org-agenda-export-csv-mapper (prop)
2690 (let ((res (plist-get org-agenda-info prop)))
2691 (setq res
2692 (cond
2693 ((not res) "")
2694 ((stringp res) res)
2695 (t (prin1-to-string res))))
2696 (while (string-match "," res)
2697 (setq res (replace-match ";" t t res)))
2698 (org-trim res)))
2699
2700
2701;;;###autoload
2702(defun org-store-agenda-views (&rest parameters)
2703 (interactive)
2704 (eval (list 'org-batch-store-agenda-views)))
2705
2706;; FIXME, why is this a macro?????
2707;;;###autoload
2708(defmacro org-batch-store-agenda-views (&rest parameters)
2709 "Run all custom agenda commands that have a file argument."
2710 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2711 (pop-up-frames nil)
2712 (dir default-directory)
2c3ad40d 2713 pars cmd thiscmdkey files opts cmd-or-set)
20908596
CD
2714 (while parameters
2715 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2716 (setq pars (reverse pars))
2717 (save-window-excursion
2718 (while cmds
2719 (setq cmd (pop cmds)
2720 thiscmdkey (car cmd)
2c3ad40d
CD
2721 cmd-or-set (nth 2 cmd)
2722 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2723 files (nth (if (listp cmd-or-set) 4 5) cmd))
20908596
CD
2724 (if (stringp files) (setq files (list files)))
2725 (when files
2726 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2727 (list 'org-agenda nil thiscmdkey)))
2728 (set-buffer org-agenda-buffer-name)
2729 (while files
2730 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2731 (list 'org-write-agenda
c8d0cf5c 2732 (expand-file-name (pop files) dir) nil t))))
20908596
CD
2733 (and (get-buffer org-agenda-buffer-name)
2734 (kill-buffer org-agenda-buffer-name)))))))
2735
8d642074
CD
2736(defun org-agenda-mark-header-line (pos)
2737 "Mark the line at POS as an agenda structure header."
2738 (save-excursion
2739 (goto-char pos)
2740 (put-text-property (point-at-bol) (point-at-eol)
2741 'org-agenda-structural-header t)
2742 (when org-agenda-title-append
2743 (put-text-property (point-at-bol) (point-at-eol)
2744 'org-agenda-title-append org-agenda-title-append))))
2745
8bfe682a 2746(defvar org-mobile-creating-agendas)
c8d0cf5c 2747(defun org-write-agenda (file &optional open nosettings)
20908596
CD
2748 "Write the current buffer (an agenda view) as a file.
2749Depending on the extension of the file name, plain text (.txt),
2750HTML (.html or .htm) or Postscript (.ps) is produced.
2751If the extension is .ics, run icalendar export over all files used
2752to construct the agenda and limit the export to entries listed in the
2753agenda now.
8bfe682a 2754With prefix argument OPEN, open the new file immediately.
20908596
CD
2755If NOSETTINGS is given, do not scope the settings of
2756`org-agenda-exporter-settings' into the export commands. This is used when
2757the settings have already been scoped and we do not wish to overrule other,
2758higher priority settings."
c8d0cf5c 2759 (interactive "FWrite agenda to file: \nP")
20908596
CD
2760 (if (not (file-writable-p file))
2761 (error "Cannot write agenda to file %s" file))
20908596 2762 (org-let (if nosettings nil org-agenda-exporter-settings)
afe98dfa 2763 '(save-excursion
20908596 2764 (save-window-excursion
93b62de8 2765 (org-agenda-mark-filtered-text)
8bfe682a 2766 (let ((bs (copy-sequence (buffer-string))) beg)
93b62de8
CD
2767 (org-agenda-unmark-filtered-text)
2768 (with-temp-buffer
afe98dfa
CD
2769 (rename-buffer "Agenda View" t)
2770 (set-buffer-modified-p nil)
20908596 2771 (insert bs)
93b62de8
CD
2772 (org-agenda-remove-marked-text 'org-filtered)
2773 (while (setq beg (text-property-any (point-min) (point-max)
2774 'org-filtered t))
2775 (delete-region
2776 beg (or (next-single-property-change beg 'org-filtered)
2777 (point-max))))
c8d0cf5c 2778 (run-hooks 'org-agenda-before-write-hook)
93b62de8 2779 (cond
8bfe682a
CD
2780 ((org-bound-and-true-p org-mobile-creating-agendas)
2781 (org-mobile-write-agenda-for-mobile file))
93b62de8 2782 ((string-match "\\.html?\\'" file)
afe98dfa 2783 (require 'htmlize)
93b62de8 2784 (set-buffer (htmlize-buffer (current-buffer)))
ff4be292 2785
93b62de8
CD
2786 (when (and org-agenda-export-html-style
2787 (string-match "<style>" org-agenda-export-html-style))
2788 ;; replace <style> section with org-agenda-export-html-style
2789 (goto-char (point-min))
2790 (kill-region (- (search-forward "<style") 6)
2791 (search-forward "</style>"))
2792 (insert org-agenda-export-html-style))
2793 (write-file file)
2794 (kill-buffer (current-buffer))
2795 (message "HTML written to %s" file))
2796 ((string-match "\\.ps\\'" file)
c8d0cf5c 2797 (require 'ps-print)
afe98dfa 2798 (ps-print-buffer-with-faces file)
93b62de8 2799 (message "Postscript written to %s" file))
c8d0cf5c
CD
2800 ((string-match "\\.pdf\\'" file)
2801 (require 'ps-print)
afe98dfa
CD
2802 (ps-print-buffer-with-faces
2803 (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c
CD
2804 (call-process "ps2pdf" nil nil nil
2805 (expand-file-name
2806 (concat (file-name-sans-extension file) ".ps"))
2807 (expand-file-name file))
afe98dfa 2808 (delete-file (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c 2809 (message "PDF written to %s" file))
93b62de8 2810 ((string-match "\\.ics\\'" file)
c8d0cf5c 2811 (require 'org-icalendar)
93b62de8
CD
2812 (let ((org-agenda-marker-table
2813 (org-create-marker-find-array
2814 (org-agenda-collect-markers)))
2815 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2816 (org-combined-agenda-icalendar-file file))
2817 (apply 'org-export-icalendar 'combine
2818 (org-agenda-files nil 'ifmode))))
2819 (t
2820 (let ((bs (buffer-string)))
2821 (find-file file)
2822 (erase-buffer)
2823 (insert bs)
2824 (save-buffer 0)
2825 (kill-buffer (current-buffer))
2826 (message "Plain text written to %s" file))))))))
c8d0cf5c
CD
2827 (set-buffer org-agenda-buffer-name))
2828 (when open (org-open-file file)))
2829
93b62de8
CD
2830(defvar org-agenda-filter-overlays nil)
2831
2832(defun org-agenda-mark-filtered-text ()
2833 "Mark all text hidden by filtering with a text property."
2834 (let ((inhibit-read-only t))
2835 (mapc
2836 (lambda (o)
86fbb8ca 2837 (when (equal (overlay-buffer o) (current-buffer))
93b62de8 2838 (put-text-property
86fbb8ca 2839 (overlay-start o) (overlay-end o)
93b62de8
CD
2840 'org-filtered t)))
2841 org-agenda-filter-overlays)))
2842
2843(defun org-agenda-unmark-filtered-text ()
2844 "Remove the filtering text property."
2845 (let ((inhibit-read-only t))
2846 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2847
2848(defun org-agenda-remove-marked-text (property &optional value)
2849 "Delete all text marked with VALUE of PROPERTY.
2850VALUE defaults to t."
2851 (let (beg)
2852 (setq value (or value t))
2853 (while (setq beg (text-property-any (point-min) (point-max)
2854 property value))
2855 (delete-region
2856 beg (or (next-single-property-change beg 'org-filtered)
2857 (point-max))))))
20908596 2858
c8d0cf5c
CD
2859(defun org-agenda-add-entry-text ()
2860 "Add entry text to agenda lines.
2861This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2862entry text following headings shown in the agenda.
2863Drawers will be excluded, also the line with scheduling/deadline info."
8bfe682a
CD
2864 (when (and (> org-agenda-add-entry-text-maxlines 0)
2865 (not (org-bound-and-true-p org-mobile-creating-agendas)))
54a0dee5 2866 (let (m txt)
c8d0cf5c
CD
2867 (goto-char (point-min))
2868 (while (not (eobp))
8d642074 2869 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
c8d0cf5c 2870 (beginning-of-line 2)
54a0dee5 2871 (setq txt (org-agenda-get-some-entry-text
8d642074 2872 m org-agenda-add-entry-text-maxlines " > "))
c8d0cf5c 2873 (end-of-line 1)
afe98dfa
CD
2874 (if (string-match "\\S-" txt)
2875 (insert "\n" txt)
2876 (or (eobp) (forward-char 1))))))))
c8d0cf5c 2877
8d642074
CD
2878(defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2879 &rest keep)
54a0dee5 2880 "Extract entry text from MARKER, at most N-LINES lines.
8d642074
CD
2881This will ignore drawers etc, just get the text.
2882If INDENT is given, prefix every line with this string. If KEEP is
8bfe682a 2883given, it is a list of symbols, defining stuff that should not be
8d642074 2884removed from the entry content. Currently only `planning' is allowed here."
54a0dee5
CD
2885 (let (txt drawer-re kwd-time-re ind)
2886 (save-excursion
2887 (with-current-buffer (marker-buffer marker)
2888 (if (not (org-mode-p))
2889 (setq txt "")
2890 (save-excursion
2891 (save-restriction
2892 (widen)
2893 (goto-char marker)
8d642074 2894 (end-of-line 1)
54a0dee5 2895 (setq txt (buffer-substring
8d642074 2896 (min (1+ (point)) (point-max))
54a0dee5
CD
2897 (progn (outline-next-heading) (point)))
2898 drawer-re org-drawer-regexp
2899 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2900 ".*\n?"))
2901 (with-temp-buffer
2902 (insert txt)
2903 (when org-agenda-add-entry-text-descriptive-links
2904 (goto-char (point-min))
2905 (while (org-activate-bracket-links (point-max))
2906 (add-text-properties (match-beginning 0) (match-end 0)
2907 '(face org-link))))
2908 (goto-char (point-min))
2909 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2910 (set-text-properties (match-beginning 0) (match-end 0)
2911 nil))
2912 (goto-char (point-min))
2913 (while (re-search-forward drawer-re nil t)
2914 (delete-region
2915 (match-beginning 0)
2916 (progn (re-search-forward
2917 "^[ \t]*:END:.*\n?" nil 'move)
2918 (point))))
8d642074
CD
2919 (unless (member 'planning keep)
2920 (goto-char (point-min))
2921 (while (re-search-forward kwd-time-re nil t)
2922 (replace-match "")))
54a0dee5 2923 (goto-char (point-min))
8d642074
CD
2924 (when org-agenda-entry-text-exclude-regexps
2925 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2926 (while (setq re (pop re-list))
2927 (goto-char (point-min))
2928 (while (re-search-forward re nil t)
2929 (replace-match "")))))
2930 (goto-char (point-max))
2931 (skip-chars-backward " \t\n")
2932 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2933
2934 ;; find and remove min common indentation
54a0dee5
CD
2935 (goto-char (point-min))
2936 (untabify (point-min) (point-max))
2937 (setq ind (org-get-indentation))
2938 (while (not (eobp))
2939 (unless (looking-at "[ \t]*$")
2940 (setq ind (min ind (org-get-indentation))))
2941 (beginning-of-line 2))
2942 (goto-char (point-min))
2943 (while (not (eobp))
2944 (unless (looking-at "[ \t]*$")
2945 (move-to-column ind)
2946 (delete-region (point-at-bol) (point)))
2947 (beginning-of-line 2))
8d642074
CD
2948
2949 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2950
54a0dee5 2951 (goto-char (point-min))
8d642074
CD
2952 (when indent
2953 (while (and (not (eobp)) (re-search-forward "^" nil t))
2954 (replace-match indent t t)))
54a0dee5
CD
2955 (goto-char (point-min))
2956 (while (looking-at "[ \t]*\n") (replace-match ""))
2957 (goto-char (point-max))
2958 (when (> (org-current-line)
2959 n-lines)
2960 (org-goto-line (1+ n-lines))
2961 (backward-char 1))
2962 (setq txt (buffer-substring (point-min) (point)))))))))
2963 txt))
2964
20908596
CD
2965(defun org-agenda-collect-markers ()
2966 "Collect the markers pointing to entries in the agenda buffer."
2967 (let (m markers)
2968 (save-excursion
2969 (goto-char (point-min))
2970 (while (not (eobp))
8d642074
CD
2971 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2972 (org-get-at-bol 'org-marker)))
20908596
CD
2973 (push m markers))
2974 (beginning-of-line 2)))
2975 (nreverse markers)))
2976
2977(defun org-create-marker-find-array (marker-list)
2978 "Create a alist of files names with all marker positions in that file."
2979 (let (f tbl m a p)
2980 (while (setq m (pop marker-list))
2981 (setq p (marker-position m)
2982 f (buffer-file-name (or (buffer-base-buffer
2983 (marker-buffer m))
2984 (marker-buffer m))))
2985 (if (setq a (assoc f tbl))
2986 (push (marker-position m) (cdr a))
2987 (push (list f p) tbl)))
2988 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2989 tbl)))
2990
33306645 2991(defvar org-agenda-marker-table nil) ; dynamically scoped parameter
20908596
CD
2992(defun org-check-agenda-marker-table ()
2993 "Check of the current entry is on the marker list."
2994 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2995 a)
2996 (and (setq a (assoc file org-agenda-marker-table))
2997 (save-match-data
2998 (save-excursion
2999 (org-back-to-heading t)
3000 (member (point) (cdr a)))))))
3001
3002(defun org-check-for-org-mode ()
3003 "Make sure current buffer is in org-mode. Error if not."
3004 (or (org-mode-p)
f924a367 3005 (error "Cannot execute org-mode agenda command on buffer in %s"
20908596
CD
3006 major-mode)))
3007
3008(defun org-fit-agenda-window ()
3009 "Fit the window to the buffer size."
3010 (and (memq org-agenda-window-setup '(reorganize-frame))
3011 (fboundp 'fit-window-to-buffer)
93b62de8 3012 (org-fit-window-to-buffer
20908596
CD
3013 nil
3014 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3015 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3016
3017;;; Agenda prepare and finalize
3018
33306645 3019(defvar org-agenda-multi nil) ; dynamically scoped
20908596
CD
3020(defvar org-agenda-buffer-name "*Org Agenda*")
3021(defvar org-pre-agenda-window-conf nil)
3022(defvar org-agenda-columns-active nil)
3023(defvar org-agenda-name nil)
71d35b24 3024(defvar org-agenda-filter nil)
acedf35c 3025(defvar org-agenda-filter-while-redo nil)
c8d0cf5c
CD
3026(defvar org-agenda-filter-preset nil
3027 "A preset of the tags filter used for secondary agenda filtering.
86fbb8ca 3028This must be a list of strings, each string must be a single tag preceded
c8d0cf5c
CD
3029by \"+\" or \"-\".
3030This variable should not be set directly, but agenda custom commands can
afe98dfa
CD
3031bind it in the options section. The preset filter is a global property of
3032the entire agenda view. In a block agenda, it will not work reliably to
3033define a filter for one of the individual blocks. You need to set it in
3034the global options and expect it to be applied to the entire view.")
c8d0cf5c 3035
20908596
CD
3036(defun org-prepare-agenda (&optional name)
3037 (setq org-todo-keywords-for-agenda nil)
3038 (setq org-done-keywords-for-agenda nil)
8d642074 3039 (setq org-drawers-for-agenda nil)
86fbb8ca
CD
3040 (unless org-agenda-persistent-filter
3041 (setq org-agenda-filter nil))
c8d0cf5c 3042 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
20908596
CD
3043 (if org-agenda-multi
3044 (progn
3045 (setq buffer-read-only nil)
3046 (goto-char (point-max))
3ab2c837
BG
3047 (unless (or (bobp) org-agenda-compact-blocks
3048 (not org-agenda-block-separator))
0bd48b37
CD
3049 (insert "\n"
3050 (if (stringp org-agenda-block-separator)
3051 org-agenda-block-separator
3052 (make-string (window-width) org-agenda-block-separator))
3053 "\n"))
20908596
CD
3054 (narrow-to-region (point) (point-max)))
3055 (org-agenda-reset-markers)
3056 (setq org-agenda-contributing-files nil)
3057 (setq org-agenda-columns-active nil)
2c3ad40d 3058 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
20908596
CD
3059 (setq org-todo-keywords-for-agenda
3060 (org-uniquify org-todo-keywords-for-agenda))
3061 (setq org-done-keywords-for-agenda
3062 (org-uniquify org-done-keywords-for-agenda))
8d642074 3063 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
20908596
CD
3064 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3065 (awin (get-buffer-window abuf)))
3066 (cond
3067 ((equal (current-buffer) abuf) nil)
3068 (awin (select-window awin))
3069 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3070 ((equal org-agenda-window-setup 'current-window)
3ab2c837 3071 (org-pop-to-buffer-same-window abuf))
20908596
CD
3072 ((equal org-agenda-window-setup 'other-window)
3073 (org-switch-to-buffer-other-window abuf))
3074 ((equal org-agenda-window-setup 'other-frame)
8d642074 3075 (switch-to-buffer-other-frame abuf))
20908596
CD
3076 ((equal org-agenda-window-setup 'reorganize-frame)
3077 (delete-other-windows)
afe98dfa
CD
3078 (org-switch-to-buffer-other-window abuf)))
3079 ;; additional test in case agenda is invoked from within agenda
3080 ;; buffer via elisp link
3081 (unless (equal (current-buffer) abuf)
3ab2c837 3082 (org-pop-to-buffer-same-window abuf)))
20908596
CD
3083 (setq buffer-read-only nil)
3084 (let ((inhibit-read-only t)) (erase-buffer))
3085 (org-agenda-mode)
3086 (and name (not org-agenda-name)
3087 (org-set-local 'org-agenda-name name)))
3088 (setq buffer-read-only nil))
3089
3090(defun org-finalize-agenda ()
3091 "Finishing touch for the agenda buffer, called just before displaying it."
3092 (unless org-agenda-multi
3093 (save-excursion
3094 (let ((inhibit-read-only t))
3095 (goto-char (point-min))
3096 (while (org-activate-bracket-links (point-max))
3097 (add-text-properties (match-beginning 0) (match-end 0)
3098 '(face org-link)))
3099 (org-agenda-align-tags)
3100 (unless org-agenda-with-colors
3101 (remove-text-properties (point-min) (point-max) '(face nil))))
33306645
CD
3102 (if (and (boundp 'org-agenda-overriding-columns-format)
3103 org-agenda-overriding-columns-format)
3104 (org-set-local 'org-agenda-overriding-columns-format
3105 org-agenda-overriding-columns-format))
20908596
CD
3106 (if (and (boundp 'org-agenda-view-columns-initially)
3107 org-agenda-view-columns-initially)
3108 (org-agenda-columns))
3109 (when org-agenda-fontify-priorities
c8d0cf5c 3110 (org-agenda-fontify-priorities))
d6685abc
CD
3111 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3112 (org-agenda-dim-blocked-tasks))
54a0dee5
CD
3113 (org-agenda-mark-clocking-task)
3114 (when org-agenda-entry-text-mode
3115 (org-agenda-entry-text-hide)
f924a367 3116 (org-agenda-entry-text-show))
8bfe682a
CD
3117 (if (functionp 'org-habit-insert-consistency-graphs)
3118 (org-habit-insert-consistency-graphs))
20908596 3119 (run-hooks 'org-finalize-agenda-hook)
8d642074 3120 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
86fbb8ca 3121 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
c8d0cf5c 3122 (org-agenda-filter-apply org-agenda-filter))
20908596
CD
3123 )))
3124
54a0dee5
CD
3125(defun org-agenda-mark-clocking-task ()
3126 "Mark the current clock entry in the agenda if it is present."
3127 (mapc (lambda (o)
86fbb8ca
CD
3128 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3129 (delete-overlay o)))
3130 (overlays-in (point-min) (point-max)))
54a0dee5
CD
3131 (when (marker-buffer org-clock-hd-marker)
3132 (save-excursion
3133 (goto-char (point-min))
3134 (let (s ov)
3135 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3136 (goto-char s)
8d642074 3137 (when (equal (org-get-at-bol 'org-hd-marker)
54a0dee5 3138 org-clock-hd-marker)
86fbb8ca
CD
3139 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3140 (overlay-put ov 'type 'org-agenda-clocking)
3141 (overlay-put ov 'face 'org-agenda-clocking)
3142 (overlay-put ov 'help-echo
54a0dee5
CD
3143 "The clock is running in this item")))))))
3144
c8d0cf5c 3145(defun org-agenda-fontify-priorities ()
20908596
CD
3146 "Make highest priority lines bold, and lowest italic."
3147 (interactive)
86fbb8ca
CD
3148 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3149 (delete-overlay o)))
3150 (overlays-in (point-min) (point-max)))
20908596
CD
3151 (save-excursion
3152 (let ((inhibit-read-only t)
3153 b e p ov h l)
3154 (goto-char (point-min))
3155 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3156 (setq h (or (get-char-property (point) 'org-highest-priority)
3157 org-highest-priority)
3158 l (or (get-char-property (point) 'org-lowest-priority)
3159 org-lowest-priority)
3160 p (string-to-char (match-string 1))
c8d0cf5c
CD
3161 b (match-beginning 0)
3162 e (if (eq org-agenda-fontify-priorities 'cookies)
3163 (match-end 0)
3164 (point-at-eol))
86fbb8ca
CD
3165 ov (make-overlay b e))
3166 (overlay-put
20908596 3167 ov 'face
ed21c5c8
CD
3168 (cond ((org-face-from-face-or-color
3169 'priority nil
3170 (cdr (assoc p org-priority-faces))))
c8d0cf5c 3171 ((and (listp org-agenda-fontify-priorities)
ed21c5c8
CD
3172 (org-face-from-face-or-color
3173 'priority nil
3174 (cdr (assoc p org-agenda-fontify-priorities)))))
20908596
CD
3175 ((equal p l) 'italic)
3176 ((equal p h) 'bold)))
86fbb8ca 3177 (overlay-put ov 'org-type 'org-priority)))))
20908596 3178
d6685abc
CD
3179(defun org-agenda-dim-blocked-tasks ()
3180 "Dim currently blocked TODO's in the agenda display."
86fbb8ca
CD
3181 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3182 (delete-overlay o)))
3183 (overlays-in (point-min) (point-max)))
d6685abc
CD
3184 (save-excursion
3185 (let ((inhibit-read-only t)
72d06d81 3186 (org-depend-tag-blocked nil)
d6685abc 3187 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
c8d0cf5c
CD
3188 org-blocked-by-checkboxes
3189 invis1 b e p ov h l)
d6685abc
CD
3190 (goto-char (point-min))
3191 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3192 (and pos (goto-char (1+ pos))))
c8d0cf5c 3193 (setq org-blocked-by-checkboxes nil invis1 invis)
8d642074 3194 (let ((marker (org-get-at-bol 'org-hd-marker)))
d6685abc
CD
3195 (when (and marker
3196 (not (with-current-buffer (marker-buffer marker)
3197 (save-excursion
3198 (goto-char marker)
c8d0cf5c
CD
3199 (if (org-entry-get nil "NOBLOCKING")
3200 t ;; Never block this entry
3201 (run-hook-with-args-until-failure
3202 'org-blocker-hook
3203 (list :type 'todo-state-change
3204 :position marker
3205 :from 'todo
3206 :to 'done)))))))
3207 (if org-blocked-by-checkboxes (setq invis1 nil))
53e31a31
CD
3208 (setq b (if invis1
3209 (max (point-min) (1- (point-at-bol)))
3210 (point-at-bol))
d6685abc 3211 e (point-at-eol)
86fbb8ca 3212 ov (make-overlay b e))
c8d0cf5c 3213 (if invis1
86fbb8ca
CD
3214 (overlay-put ov 'invisible t)
3215 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3216 (overlay-put ov 'org-type 'org-blocked-todo)))))))
20908596
CD
3217
3218(defvar org-agenda-skip-function nil
3219 "Function to be called at each match during agenda construction.
3220If this function returns nil, the current match should not be skipped.
3221Otherwise, the function must return a position from where the search
3222should be continued.
3223This may also be a Lisp form, it will be evaluated.
3224Never set this variable using `setq' or so, because then it will apply
3ab2c837
BG
3225to all future agenda commands. If you do want a global skipping condition,
3226use the option `org-agenda-skip-function-global' instead.
3227The correct usage for `org-agenda-skip-function' is to bind it with
3228`let' to scope it dynamically into the agenda-constructing command.
3229A good way to set it is through options in `org-agenda-custom-commands'.")
20908596
CD
3230
3231(defun org-agenda-skip ()
3232 "Throw to `:skip' in places that should be skipped.
3233Also moves point to the end of the skipped region, so that search can
3234continue from there."
3ab2c837 3235 (let ((p (point-at-bol)) to)
2c3ad40d 3236 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
20908596
CD
3237 (get-text-property p :org-archived)
3238 (org-end-of-subtree t)
3239 (throw :skip t))
b349f79f
CD
3240 (and org-agenda-skip-comment-trees
3241 (get-text-property p :org-comment)
20908596
CD
3242 (org-end-of-subtree t)
3243 (throw :skip t))
3244 (if (equal (char-after p) ?#) (throw :skip t))
3ab2c837
BG
3245 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3246 (org-agenda-skip-eval org-agenda-skip-function)))
20908596
CD
3247 (goto-char to)
3248 (throw :skip t))))
3249
3ab2c837
BG
3250(defun org-agenda-skip-eval (form)
3251 "If FORM is a function or a list, call (or eval) is and return result.
3252`save-excursion' and `save-match-data' are wrapped around the call, so point
3253and match data are returned to the previous state no matter what these
3254functions do."
3255 (let (fp)
3256 (and form
3257 (or (setq fp (functionp form))
3258 (consp form))
3259 (save-excursion
3260 (save-match-data
3261 (if fp
3262 (funcall form)
3263 (eval form)))))))
3264
20908596
CD
3265(defvar org-agenda-markers nil
3266 "List of all currently active markers created by `org-agenda'.")
54a0dee5 3267(defvar org-agenda-last-marker-time (org-float-time)
20908596
CD
3268 "Creation time of the last agenda marker.")
3269
3270(defun org-agenda-new-marker (&optional pos)
3271 "Return a new agenda marker.
3272Org-mode keeps a list of these markers and resets them when they are
3273no longer in use."
3274 (let ((m (copy-marker (or pos (point)))))
54a0dee5 3275 (setq org-agenda-last-marker-time (org-float-time))
20908596
CD
3276 (push m org-agenda-markers)
3277 m))
3278
3279(defun org-agenda-reset-markers ()
3280 "Reset markers created by `org-agenda'."
3281 (while org-agenda-markers
3282 (move-marker (pop org-agenda-markers) nil)))
3283
b349f79f
CD
3284(defun org-agenda-save-markers-for-cut-and-paste (beg end)
3285 "Save relative positions of markers in region."
3286 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3287 org-agenda-markers))
3288
54a0dee5
CD
3289;;; Entry text mode
3290
3291(defun org-agenda-entry-text-show-here ()
8bfe682a 3292 "Add some text from the entry as context to the current line."
54a0dee5 3293 (let (m txt o)
8d642074 3294 (setq m (org-get-at-bol 'org-hd-marker))
54a0dee5
CD
3295 (unless (marker-buffer m)
3296 (error "No marker points to an entry here"))
3297 (setq txt (concat "\n" (org-no-properties
3298 (org-agenda-get-some-entry-text
8d642074 3299 m org-agenda-entry-text-maxlines " > "))))
54a0dee5 3300 (when (string-match "\\S-" txt)
86fbb8ca
CD
3301 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3302 (overlay-put o 'evaporate t)
3303 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3304 (overlay-put o 'after-string txt))))
54a0dee5
CD
3305
3306(defun org-agenda-entry-text-show ()
3307 "Add entry context for all agenda lines."
3308 (interactive)
3309 (save-excursion
3310 (goto-char (point-max))
3311 (beginning-of-line 1)
3312 (while (not (bobp))
8d642074 3313 (when (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
3314 (org-agenda-entry-text-show-here))
3315 (beginning-of-line 0))))
3316
3317(defun org-agenda-entry-text-hide ()
3318 "Remove any shown entry context."
3319 (delq nil
3320 (mapcar (lambda (o)
86fbb8ca 3321 (if (eq (overlay-get o 'org-overlay-type)
54a0dee5 3322 'agenda-entry-content)
86fbb8ca
CD
3323 (progn (delete-overlay o) t)))
3324 (overlays-in (point-min) (point-max)))))
54a0dee5 3325
acedf35c
CD
3326(defun org-agenda-get-day-face (date)
3327 "Return the face DATE should be displayed with."
3328 (or (and (functionp org-agenda-day-face-function)
3329 (funcall org-agenda-day-face-function date))
3330 (cond ((org-agenda-todayp date)
3331 'org-agenda-date-today)
3332 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3333 'org-agenda-date-weekend)
3334 (t 'org-agenda-date))))
3335
20908596
CD
3336;;; Agenda timeline
3337
3338(defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3339
3340(defun org-timeline (&optional include-all)
3341 "Show a time-sorted view of the entries in the current org file.
3342Only entries with a time stamp of today or later will be listed. With
3343\\[universal-argument] prefix, all unfinished TODO items will also be shown,
3344under the current date.
3345If the buffer contains an active region, only check the region for
3346dates."
3347 (interactive "P")
20908596
CD
3348 (org-compile-prefix-format 'timeline)
3349 (org-set-sorting-strategy 'timeline)
3350 (let* ((dopast t)
3351 (dotodo include-all)
3352 (doclosed org-agenda-show-log)
afe98dfa
CD
3353 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3354 (current-buffer))))
20908596
CD
3355 (date (calendar-current-date))
3356 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3357 (end (if (org-region-active-p) (region-end) (point-max)))
3358 (day-numbers (org-get-all-dates beg end 'no-ranges
3359 t doclosed ; always include today
3360 org-timeline-show-empty-dates))
3361 (org-deadline-warning-days 0)
3362 (org-agenda-only-exact-dates t)
acedf35c 3363 (today (org-today))
20908596
CD
3364 (past t)
3365 args
acedf35c 3366 s e rtn d emptyp)
20908596
CD
3367 (setq org-agenda-redo-command
3368 (list 'progn
3369 (list 'org-switch-to-buffer-other-window (current-buffer))
3370 (list 'org-timeline (list 'quote include-all))))
3371 (if (not dopast)
3372 ;; Remove past dates from the list of dates.
3373 (setq day-numbers (delq nil (mapcar (lambda(x)
3374 (if (>= x today) x nil))
3375 day-numbers))))
afe98dfa 3376 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
20908596
CD
3377 (if doclosed (push :closed args))
3378 (push :timestamp args)
3379 (push :deadline args)
3380 (push :scheduled args)
3381 (push :sexp args)
3382 (if dotodo (push :todo args))
8d642074
CD
3383 (insert "Timeline of file " entry "\n")
3384 (add-text-properties (point-min) (point)
3385 (list 'face 'org-agenda-structure))
3386 (org-agenda-mark-header-line (point-min))
20908596
CD
3387 (while (setq d (pop day-numbers))
3388 (if (and (listp d) (eq (car d) :omitted))
3389 (progn
3390 (setq s (point))
3391 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3392 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3393 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3394 (if (and (>= d today)
3395 dopast
3396 past)
3397 (progn
3398 (setq past nil)
3399 (insert (make-string 79 ?-) "\n")))
acedf35c 3400 (setq date (calendar-gregorian-from-absolute d))
20908596
CD
3401 (setq s (point))
3402 (setq rtn (and (not emptyp)
3403 (apply 'org-agenda-get-day-entries entry
3404 date args)))
3405 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3406 (progn
3407 (insert
3408 (if (stringp org-agenda-format-date)
3409 (format-time-string org-agenda-format-date
3410 (org-time-from-absolute date))
3411 (funcall org-agenda-format-date date))
3412 "\n")
3413 (put-text-property s (1- (point)) 'face
acedf35c 3414 (org-agenda-get-day-face date))
20908596 3415 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3416 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596
CD
3417 (if (equal d today)
3418 (put-text-property s (1- (point)) 'org-today t))
3419 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3420 (put-text-property s (1- (point)) 'day d)))))
3421 (goto-char (point-min))
3422 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3423 (point-min)))
3424 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3425 (org-finalize-agenda)
3426 (setq buffer-read-only t)))
3427
3428(defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3429 "Return a list of all relevant day numbers from BEG to END buffer positions.
3430If NO-RANGES is non-nil, include only the start and end dates of a range,
3431not every single day in the range. If FORCE-TODAY is non-nil, make
3432sure that TODAY is included in the list. If INACTIVE is non-nil, also
3433inactive time stamps (those in square brackets) are included.
3434When EMPTY is non-nil, also include days without any entries."
3435 (let ((re (concat
3436 (if pre-re pre-re "")
3437 (if inactive org-ts-regexp-both org-ts-regexp)))
3438 dates dates1 date day day1 day2 ts1 ts2)
3439 (if force-today
acedf35c 3440 (setq dates (list (org-today))))
20908596
CD
3441 (save-excursion
3442 (goto-char beg)
3443 (while (re-search-forward re end t)
3444 (setq day (time-to-days (org-time-string-to-time
3445 (substring (match-string 1) 0 10))))
3446 (or (memq day dates) (push day dates)))
3447 (unless no-ranges
3448 (goto-char beg)
3449 (while (re-search-forward org-tr-regexp end t)
3450 (setq ts1 (substring (match-string 1) 0 10)
3451 ts2 (substring (match-string 2) 0 10)
3452 day1 (time-to-days (org-time-string-to-time ts1))
3453 day2 (time-to-days (org-time-string-to-time ts2)))
3454 (while (< (setq day1 (1+ day1)) day2)
3455 (or (memq day1 dates) (push day1 dates)))))
3456 (setq dates (sort dates '<))
3457 (when empty
3458 (while (setq day (pop dates))
3459 (setq day2 (car dates))
3460 (push day dates1)
3461 (when (and day2 empty)
3462 (if (or (eq empty t)
3463 (and (numberp empty) (<= (- day2 day) empty)))
3464 (while (< (setq day (1+ day)) day2)
3465 (push (list day) dates1))
3466 (push (cons :omitted (- day2 day)) dates1))))
3467 (setq dates (nreverse dates1)))
3468 dates)))
3469
3470;;; Agenda Daily/Weekly
3471
c8d0cf5c 3472(defvar org-agenda-start-day nil ; dynamically scoped parameter
3ab2c837
BG
3473"Start day for the agenda view.
3474Custom commands can set this variable in the options section.")
20908596 3475(defvar org-starting-day nil) ; local variable in the agenda buffer
acedf35c
CD
3476(defvar org-agenda-current-span nil
3477 "The current span used in the agenda view.") ; local variable in the agenda buffer
20908596 3478(defvar org-include-all-loc nil) ; local variable
20908596 3479
ed21c5c8
CD
3480(defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3481 "List of types searched for when creating the daily/weekly agenda.
3482This variable is a list of symbols that controls the types of
3483items that appear in the daily/weekly agenda. Allowed symbols in this
3484list are are
3485
3486 :timestamp List items containing a date stamp or date range matching
3487 the selected date. This includes sexp entries in
3488 angular brackets.
3489
3490 :sexp List entries resulting from plain diary-like sexps.
3491
3492 :deadline List deadline due on that date. When the date is today,
3493 also list any deadlines past due, or due within
3494 `org-deadline-warning-days'. `:deadline' must appear before
3495 `:scheduled' if the setting of
3496 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3497 any effect.
3498
3499 :scheduled List all items which are scheduled for the given date.
3500 The diary for *today* also contains items which were
3501 scheduled earlier and are not yet marked DONE.
3502
3503By default, all four types are turned on.
3504
3505Never set this variable globally using `setq', because then it
3506will apply to all future agenda commands. Instead, bind it with
9b053e76 3507`let' to scope it dynamically into the agenda-constructing
ed21c5c8
CD
3508command. A good way to set it is through options in
3509`org-agenda-custom-commands'. For a more flexible (though
3510somewhat less efficient) way of determining what is included in
3511the daily/weekly agenda, see `org-agenda-skip-function'.")
3512
20908596 3513;;;###autoload
acedf35c 3514(defun org-agenda-list (&optional include-all start-day span)
20908596
CD
3515 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3516The view will be for the current day or week, but from the overview buffer
3517you will be able to go to other days/weeks.
3518
20908596 3519With a numeric prefix argument in an interactive call, the agenda will
acedf35c
CD
3520span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3521the number of days. SPAN defaults to `org-agenda-span'.
20908596
CD
3522
3523START-DAY defaults to TODAY, or to the most recent match for the weekday
3524given in `org-agenda-start-on-weekday'."
3525 (interactive "P")
3526 (if (and (integerp include-all) (> include-all 0))
acedf35c
CD
3527 (setq span include-all include-all nil))
3528 (setq start-day (or start-day org-agenda-start-day))
20908596
CD
3529 (if org-agenda-overriding-arguments
3530 (setq include-all (car org-agenda-overriding-arguments)
3531 start-day (nth 1 org-agenda-overriding-arguments)
acedf35c 3532 span (nth 2 org-agenda-overriding-arguments)))
20908596
CD
3533 (if (stringp start-day)
3534 ;; Convert to an absolute day number
3535 (setq start-day (time-to-days (org-read-date nil t start-day))))
acedf35c 3536 (setq org-agenda-last-arguments (list include-all start-day span))
20908596
CD
3537 (org-compile-prefix-format 'agenda)
3538 (org-set-sorting-strategy 'agenda)
3ab2c837
BG
3539 (let* ((span (org-agenda-ndays-to-span
3540 (or span org-agenda-ndays org-agenda-span)))
acedf35c
CD
3541 (today (org-today))
3542 (sd (or start-day today))
3543 (ndays (org-agenda-span-to-ndays span sd))
3544 (org-agenda-start-on-weekday
3545 (if (eq ndays 7)
3546 org-agenda-start-on-weekday))
2c3ad40d 3547 (thefiles (org-agenda-files nil 'ifmode))
20908596 3548 (files thefiles)
20908596 3549 (start (if (or (null org-agenda-start-on-weekday)
acedf35c 3550 (< ndays 7))
20908596
CD
3551 sd
3552 (let* ((nt (calendar-day-of-week
3553 (calendar-gregorian-from-absolute sd)))
3554 (n1 org-agenda-start-on-weekday)
3555 (d (- nt n1)))
3556 (- sd (+ (if (< d 0) 7 0) d)))))
3557 (day-numbers (list start))
3558 (day-cnt 0)
3559 (inhibit-redisplay (not debug-on-error))
acedf35c
CD
3560 s e rtn rtnall file date d start-pos end-pos todayp
3561 clocktable-start clocktable-end filter)
20908596 3562 (setq org-agenda-redo-command
acedf35c
CD
3563 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3564 (dotimes (n (1- ndays))
3565 (push (1+ (car day-numbers)) day-numbers))
20908596
CD
3566 (setq day-numbers (nreverse day-numbers))
3567 (setq clocktable-start (car day-numbers)
3568 clocktable-end (1+ (or (org-last day-numbers) 0)))
3569 (org-prepare-agenda "Day/Week")
3570 (org-set-local 'org-starting-day (car day-numbers))
3571 (org-set-local 'org-include-all-loc include-all)
acedf35c 3572 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
20908596
CD
3573 (unless org-agenda-compact-blocks
3574 (let* ((d1 (car day-numbers))
3575 (d2 (org-last day-numbers))
3576 (w1 (org-days-to-iso-week d1))
3577 (w2 (org-days-to-iso-week d2)))
3578 (setq s (point))
c8d0cf5c
CD
3579 (if org-agenda-overriding-header
3580 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3581 nil 'face 'org-agenda-structure) "\n")
acedf35c 3582 (insert (org-agenda-span-name span)
c8d0cf5c
CD
3583 "-agenda"
3584 (if (< (- d2 d1) 350)
3585 (if (= w1 w2)
3586 (format " (W%02d)" w1)
3587 (format " (W%02d-W%02d)" w1 w2))
3588 "")
3589 ":\n")))
20908596 3590 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
8d642074
CD
3591 'org-date-line t))
3592 (org-agenda-mark-header-line s))
20908596
CD
3593 (while (setq d (pop day-numbers))
3594 (setq date (calendar-gregorian-from-absolute d)
20908596
CD
3595 s (point))
3596 (if (or (setq todayp (= d today))
3597 (and (not start-pos) (= d sd)))
3598 (setq start-pos (point))
3599 (if (and start-pos (not end-pos))
3600 (setq end-pos (point))))
3601 (setq files thefiles
3602 rtnall nil)
3603 (while (setq file (pop files))
3604 (catch 'nextfile
3605 (org-check-agenda-file file)
ed21c5c8
CD
3606 (let ((org-agenda-entry-types org-agenda-entry-types))
3607 (unless org-agenda-include-deadlines
3608 (setq org-agenda-entry-types
3609 (delq :deadline org-agenda-entry-types)))
3610 (cond
3ab2c837 3611 ((memq org-agenda-show-log '(only clockcheck))
ed21c5c8
CD
3612 (setq rtn (org-agenda-get-day-entries
3613 file date :closed)))
3614 (org-agenda-show-log
3615 (setq rtn (apply 'org-agenda-get-day-entries
3616 file date
3617 (append '(:closed) org-agenda-entry-types))))
3618 (t
3619 (setq rtn (apply 'org-agenda-get-day-entries
3620 file date
3621 org-agenda-entry-types)))))
20908596
CD
3622 (setq rtnall (append rtnall rtn))))
3623 (if org-agenda-include-diary
c8d0cf5c 3624 (let ((org-agenda-search-headline-for-time t))
20908596
CD
3625 (require 'diary-lib)
3626 (setq rtn (org-get-entries-from-diary date))
3627 (setq rtnall (append rtnall rtn))))
3628 (if (or rtnall org-agenda-show-all-dates)
3629 (progn
3630 (setq day-cnt (1+ day-cnt))
3631 (insert
3632 (if (stringp org-agenda-format-date)
3633 (format-time-string org-agenda-format-date
3634 (org-time-from-absolute date))
3635 (funcall org-agenda-format-date date))
3636 "\n")
3637 (put-text-property s (1- (point)) 'face
acedf35c 3638 (org-agenda-get-day-face date))
20908596 3639 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3640 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596 3641 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
c8d0cf5c 3642 (when todayp
acedf35c 3643 (put-text-property s (1- (point)) 'org-today t))
20908596
CD
3644 (if rtnall (insert
3645 (org-finalize-agenda-entries
3646 (org-agenda-add-time-grid-maybe
acedf35c 3647 rtnall ndays todayp))
20908596
CD
3648 "\n"))
3649 (put-text-property s (1- (point)) 'day d)
3650 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3651 (when (and org-agenda-clockreport-mode clocktable-start)
2c3ad40d 3652 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
20908596 3653 ;; the above line is to ensure the restricted range!
3ab2c837 3654 (p (copy-sequence org-agenda-clockreport-parameter-plist))
20908596
CD
3655 tbl)
3656 (setq p (org-plist-delete p :block))
3657 (setq p (plist-put p :tstart clocktable-start))
3658 (setq p (plist-put p :tend clocktable-end))
3659 (setq p (plist-put p :scope 'agenda))
acedf35c
CD
3660 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3661 (setq filter (or org-agenda-filter-while-redo
3662 (get 'org-agenda-filter :preset-filter))))
3663 (setq p (plist-put p :tags (mapconcat (lambda (x)
3664 (if (string-match "[<>=]" x)
3665 ""
3666 x))
3667 filter ""))))
20908596
CD
3668 (setq tbl (apply 'org-get-clocktable p))
3669 (insert tbl)))
3670 (goto-char (point-min))
c8d0cf5c 3671 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3672 (unless (and (pos-visible-in-window-p (point-min))
3673 (pos-visible-in-window-p (point-max)))
3674 (goto-char (1- (point-max)))
3675 (recenter -1)
3676 (if (not (pos-visible-in-window-p (or start-pos 1)))
3677 (progn
3678 (goto-char (or start-pos 1))
3679 (recenter 1))))
3680 (goto-char (or start-pos 1))
3681 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3ab2c837
BG
3682 (if (eq org-agenda-show-log 'clockcheck)
3683 (org-agenda-show-clocking-issues))
20908596
CD
3684 (org-finalize-agenda)
3685 (setq buffer-read-only t)
3686 (message "")))
3687
3688(defun org-agenda-ndays-to-span (n)
acedf35c
CD
3689 "Return a span symbol for a span of N days, or N if none matches."
3690 (cond ((symbolp n) n)
3691 ((= n 1) 'day)
3692 ((= n 7) 'week)
3693 (t n)))
3694
3695(defun org-agenda-span-to-ndays (span start-day)
3696 "Return ndays from SPAN starting at START-DAY."
3697 (cond ((numberp span) span)
3698 ((eq span 'day) 1)
3699 ((eq span 'week) 7)
3700 ((eq span 'month)
3701 (let ((date (calendar-gregorian-from-absolute start-day)))
3702 (calendar-last-day-of-month (car date) (caddr date))))
3703 ((eq span 'year)
3704 (let ((date (calendar-gregorian-from-absolute start-day)))
3705 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3706
3707(defun org-agenda-span-name (span)
3708 "Return a SPAN name."
3709 (if (null span)
3710 ""
3711 (if (symbolp span)
3712 (capitalize (symbol-name span))
3713 (format "%d days" span))))
20908596
CD
3714
3715;;; Agenda word search
3716
3717(defvar org-agenda-search-history nil)
3718(defvar org-todo-only nil)
3719
3720(defvar org-search-syntax-table nil
3721 "Special syntax table for org-mode search.
3722In this table, we have single quotes not as word constituents, to
33306645 3723that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
20908596
CD
3724
3725(defun org-search-syntax-table ()
3726 (unless org-search-syntax-table
3727 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3728 (modify-syntax-entry ?' "." org-search-syntax-table)
3729 (modify-syntax-entry ?` "." org-search-syntax-table))
3730 org-search-syntax-table)
3731
ed21c5c8
CD
3732(defvar org-agenda-last-search-view-search-was-boolean nil)
3733
20908596
CD
3734;;;###autoload
3735(defun org-search-view (&optional todo-only string edit-at)
ed21c5c8 3736 "Show all entries that contain a phrase or words or regular expressions.
20908596
CD
3737
3738With optional prefix argument TODO-ONLY, only consider entries that are
3739TODO entries. The argument STRING can be used to pass a default search
3740string into this function. If EDIT-AT is non-nil, it means that the
3741user should get a chance to edit this string, with cursor at position
3742EDIT-AT.
3743
ed21c5c8
CD
3744The search string can be viewed either as a phrase that should be found as
3745is, or it can be broken into a number of snippets, each of which must match
3746in a Boolean way to select an entry. The default depends on the variable
3747`org-agenda-search-view-always-boolean'.
3748Even if this is turned off (the default) you can always switch to
86fbb8ca 3749Boolean search dynamically by preceding the first word with \"+\" or \"-\".
ed21c5c8
CD
3750
3751The default is a direct search of the whole phrase, where each space in
3752the search string can expand to an arbitrary amount of whitespace,
3753including newlines.
3754
3755If using a Boolean search, the search string is split on whitespace and
3756each snippet is searched separately, with logical AND to select an entry.
3757Words prefixed with a minus must *not* occur in the entry. Words without
3758a prefix or prefixed with a plus must occur in the entry. Matching is
3759case-insensitive. Words are enclosed by word delimiters (i.e. they must
3760match whole words, not parts of a word) if
3761`org-agenda-search-view-force-full-words' is set (default is nil).
3762
3763Boolean search snippets enclosed by curly braces are interpreted as
86fbb8ca 3764regular expressions that must or (when preceded with \"-\") must not
ed21c5c8 3765match in the entry. Snippets enclosed into double quotes will be taken
86fbb8ca 3766as a whole, to include whitespace.
ed21c5c8
CD
3767
3768- If the search string starts with an asterisk, search only in headlines.
3769- If (possibly after the leading star) the search string starts with an
3770 exclamation mark, this also means to look at TODO entries only, an effect
3771 that can also be achieved with a prefix argument.
3772- If (possibly after star and exclamation mark) the search string starts
3773 with a colon, this will mean that the (non-regexp) snippets of the
3774 Boolean search must match as full words.
20908596
CD
3775
3776This command searches the agenda files, and in addition the files listed
3777in `org-agenda-text-search-extra-files'."
3778 (interactive "P")
3779 (org-compile-prefix-format 'search)
3780 (org-set-sorting-strategy 'search)
3781 (org-prepare-agenda "SEARCH")
3782 (let* ((props (list 'face nil
c8d0cf5c 3783 'done-face 'org-agenda-done
20908596
CD
3784 'org-not-done-regexp org-not-done-regexp
3785 'org-todo-regexp org-todo-regexp
b349f79f 3786 'org-complex-heading-regexp org-complex-heading-regexp
20908596 3787 'mouse-face 'highlight
20908596 3788 'help-echo (format "mouse-2 or RET jump to location")))
ed21c5c8 3789 (full-words org-agenda-search-view-force-full-words)
86fbb8ca 3790 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
20908596 3791 regexp rtn rtnall files file pos
ed21c5c8 3792 marker category tags c neg re boolean
20908596
CD
3793 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3794 (unless (and (not edit-at)
3795 (stringp string)
3796 (string-match "\\S-" string))
ed21c5c8
CD
3797 (setq string (read-string
3798 (if org-agenda-search-view-always-boolean
3799 "[+-]Word/{Regexp} ...: "
3800 "Phrase, or [+-]Word/{Regexp} ...: ")
3801 (cond
3802 ((integerp edit-at) (cons string edit-at))
3803 (edit-at string))
3804 'org-agenda-search-history)))
20908596
CD
3805 (org-set-local 'org-todo-only todo-only)
3806 (setq org-agenda-redo-command
3807 (list 'org-search-view (if todo-only t nil) string
3808 '(if current-prefix-arg 1 nil)))
3809 (setq org-agenda-query-string string)
3810
3811 (if (equal (string-to-char string) ?*)
3812 (setq hdl-only t
3813 words (substring string 1))
3814 (setq words string))
3815 (when (equal (string-to-char words) ?!)
3816 (setq todo-only t
3817 words (substring words 1)))
ed21c5c8
CD
3818 (when (equal (string-to-char words) ?:)
3819 (setq full-words t
3820 words (substring words 1)))
3821 (if (or org-agenda-search-view-always-boolean
3822 (member (string-to-char words) '(?- ?+ ?\{)))
3823 (setq boolean t))
20908596 3824 (setq words (org-split-string words))
afe98dfa
CD
3825 (let (www w)
3826 (while (setq w (pop words))
3827 (while (and (string-match "\\\\\\'" w) words)
3828 (setq w (concat (substring w 0 -1) " " (pop words))))
3829 (push w www))
3830 (setq words (nreverse www) www nil)
3831 (while (setq w (pop words))
3832 (when (and (string-match "\\`[-+]?{" w)
3833 (not (string-match "}\\'" w)))
3834 (while (and words (not (string-match "}\\'" (car words))))
3835 (setq w (concat w " " (pop words))))
3836 (setq w (concat w " " (pop words))))
3837 (push w www))
3838 (setq words (nreverse www)))
ed21c5c8
CD
3839 (setq org-agenda-last-search-view-search-was-boolean boolean)
3840 (when boolean
3841 (let (wds w)
3842 (while (setq w (pop words))
3843 (if (or (equal (substring w 0 1) "\"")
3844 (and (> (length w) 1)
3845 (member (substring w 0 1) '("+" "-"))
3846 (equal (substring w 1 2) "\"")))
3847 (while (and words (not (equal (substring w -1) "\"")))
3848 (setq w (concat w " " (pop words)))))
3849 (and (string-match "\\`\\([-+]?\\)\"" w)
3850 (setq w (replace-match "\\1" nil nil w)))
3851 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3852 (push w wds))
3853 (setq words (nreverse wds))))
3854 (if boolean
8bfe682a
CD
3855 (mapc (lambda (w)
3856 (setq c (string-to-char w))
3857 (if (equal c ?-)
3858 (setq neg t w (substring w 1))
3859 (if (equal c ?+)
3860 (setq neg nil w (substring w 1))
ed21c5c8 3861 (setq neg nil)))
8bfe682a
CD
3862 (if (string-match "\\`{.*}\\'" w)
3863 (setq re (substring w 1 -1))
ed21c5c8
CD
3864 (if full-words
3865 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3866 (setq re (regexp-quote (downcase w)))))
8bfe682a
CD
3867 (if neg (push re regexps-) (push re regexps+)))
3868 words)
3869 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3870 regexps+))
20908596
CD
3871 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3872 (if (not regexps+)
3ab2c837 3873 (setq regexp org-outline-regexp-bol)
20908596
CD
3874 (setq regexp (pop regexps+))
3875 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3876 regexp))))
2c3ad40d 3877 (setq files (org-agenda-files nil 'ifmode))
20908596
CD
3878 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3879 (pop org-agenda-text-search-extra-files)
3880 (setq files (org-add-archive-files files)))
3881 (setq files (append files org-agenda-text-search-extra-files)
3882 rtnall nil)
3883 (while (setq file (pop files))
3884 (setq ee nil)
3885 (catch 'nextfile
3886 (org-check-agenda-file file)
3887 (setq buffer (if (file-exists-p file)
3888 (org-get-agenda-file-buffer file)
3889 (error "No such file %s" file)))
3890 (if (not buffer)
3891 ;; If file does not exist, make sure an error message is sent
3892 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3893 file))))
3894 (with-current-buffer buffer
3895 (with-syntax-table (org-search-syntax-table)
3896 (unless (org-mode-p)
3897 (error "Agenda file %s is not in `org-mode'" file))
3898 (let ((case-fold-search t))
3899 (save-excursion
3900 (save-restriction
3901 (if org-agenda-restrict
3902 (narrow-to-region org-agenda-restrict-begin
3903 org-agenda-restrict-end)
3904 (widen))
3905 (goto-char (point-min))
3906 (unless (or (org-on-heading-p)
3907 (outline-next-heading))
3908 (throw 'nextfile t))
3909 (goto-char (max (point-min) (1- (point))))
3910 (while (re-search-forward regexp nil t)
3911 (org-back-to-heading t)
3912 (skip-chars-forward "* ")
3913 (setq beg (point-at-bol)
3914 beg1 (point)
3915 end (progn (outline-next-heading) (point)))
3916 (catch :skip
3917 (goto-char beg)
3918 (org-agenda-skip)
3919 (setq str (buffer-substring-no-properties
3920 (point-at-bol)
3921 (if hdl-only (point-at-eol) end)))
3922 (mapc (lambda (wr) (when (string-match wr str)
3923 (goto-char (1- end))
3924 (throw :skip t)))
3925 regexps-)
3926 (mapc (lambda (wr) (unless (string-match wr str)
3927 (goto-char (1- end))
3928 (throw :skip t)))
3929 (if todo-only
3930 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3931 regexps+)
3932 regexps+))
3933 (goto-char beg)
3934 (setq marker (org-agenda-new-marker (point))
3935 category (org-get-category)
3936 tags (org-get-tags-at (point))
3937 txt (org-format-agenda-item
3938 ""
3939 (buffer-substring-no-properties
3940 beg1 (point-at-eol))
3941 category tags))
3942 (org-add-props txt props
3943 'org-marker marker 'org-hd-marker marker
3944 'org-todo-regexp org-todo-regexp
b349f79f 3945 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
3946 'priority 1000 'org-category category
3947 'type "search")
3948 (push txt ee)
3949 (goto-char (1- end))))))))))
3950 (setq rtn (nreverse ee))
3951 (setq rtnall (append rtnall rtn)))
3952 (if org-agenda-overriding-header
3953 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3954 nil 'face 'org-agenda-structure) "\n")
3955 (insert "Search words: ")
3956 (add-text-properties (point-min) (1- (point))
3957 (list 'face 'org-agenda-structure))
3958 (setq pos (point))
3959 (insert string "\n")
3960 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3961 (setq pos (point))
3962 (unless org-agenda-multi
3963 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3964 (add-text-properties pos (1- (point))
3965 (list 'face 'org-agenda-structure))))
8d642074 3966 (org-agenda-mark-header-line (point-min))
20908596
CD
3967 (when rtnall
3968 (insert (org-finalize-agenda-entries rtnall) "\n"))
3969 (goto-char (point-min))
c8d0cf5c 3970 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3971 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3972 (org-finalize-agenda)
3973 (setq buffer-read-only t)))
3974
3975;;; Agenda TODO list
3976
3977(defvar org-select-this-todo-keyword nil)
3978(defvar org-last-arg nil)
3979
3980;;;###autoload
3981(defun org-todo-list (arg)
86fbb8ca 3982 "Show all (not done) TODO entries from all agenda file in a single list.
20908596
CD
3983The prefix arg can be used to select a specific TODO keyword and limit
3984the list to these. When using \\[universal-argument], you will be prompted
3985for a keyword. A numeric prefix directly selects the Nth keyword in
3986`org-todo-keywords-1'."
3987 (interactive "P")
20908596
CD
3988 (org-compile-prefix-format 'todo)
3989 (org-set-sorting-strategy 'todo)
3990 (org-prepare-agenda "TODO")
ed21c5c8 3991 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
acedf35c 3992 (let* ((today (org-today))
20908596
CD
3993 (date (calendar-gregorian-from-absolute today))
3994 (kwds org-todo-keywords-for-agenda)
3995 (completion-ignore-case t)
3996 (org-select-this-todo-keyword
3997 (if (stringp arg) arg
3998 (and arg (integerp arg) (> arg 0)
3999 (nth (1- arg) kwds))))
4000 rtn rtnall files file pos)
4001 (when (equal arg '(4))
4002 (setq org-select-this-todo-keyword
54a0dee5 4003 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
20908596
CD
4004 (mapcar 'list kwds) nil nil)))
4005 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4006 (org-set-local 'org-last-arg arg)
4007 (setq org-agenda-redo-command
4008 '(org-todo-list (or current-prefix-arg org-last-arg)))
2c3ad40d 4009 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
4010 rtnall nil)
4011 (while (setq file (pop files))
4012 (catch 'nextfile
4013 (org-check-agenda-file file)
4014 (setq rtn (org-agenda-get-day-entries file date :todo))
4015 (setq rtnall (append rtnall rtn))))
4016 (if org-agenda-overriding-header
4017 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4018 nil 'face 'org-agenda-structure) "\n")
4019 (insert "Global list of TODO items of type: ")
4020 (add-text-properties (point-min) (1- (point))
8d642074
CD
4021 (list 'face 'org-agenda-structure
4022 'short-heading
4023 (concat "ToDo: "
4024 (or org-select-this-todo-keyword "ALL"))))
4025 (org-agenda-mark-header-line (point-min))
20908596
CD
4026 (setq pos (point))
4027 (insert (or org-select-this-todo-keyword "ALL") "\n")
4028 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4029 (setq pos (point))
4030 (unless org-agenda-multi
4031 (insert "Available with `N r': (0)ALL")
4032 (let ((n 0) s)
4033 (mapc (lambda (x)
4034 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4035 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4036 (insert "\n "))
4037 (insert " " s))
4038 kwds))
4039 (insert "\n"))
4040 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 4041 (org-agenda-mark-header-line (point-min))
20908596
CD
4042 (when rtnall
4043 (insert (org-finalize-agenda-entries rtnall) "\n"))
4044 (goto-char (point-min))
c8d0cf5c 4045 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
4046 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4047 (org-finalize-agenda)
4048 (setq buffer-read-only t)))
4049
4050;;; Agenda tags match
4051
4052;;;###autoload
4053(defun org-tags-view (&optional todo-only match)
4054 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4055The prefix arg TODO-ONLY limits the search to TODO entries."
4056 (interactive "P")
4057 (org-compile-prefix-format 'tags)
4058 (org-set-sorting-strategy 'tags)
4059 (let* ((org-tags-match-list-sublevels
c8d0cf5c 4060 org-tags-match-list-sublevels)
20908596
CD
4061 (completion-ignore-case t)
4062 rtn rtnall files file pos matcher
4063 buffer)
ed21c5c8
CD
4064 (when (and (stringp match) (not (string-match "\\S-" match)))
4065 (setq match nil))
20908596
CD
4066 (setq matcher (org-make-tags-matcher match)
4067 match (car matcher) matcher (cdr matcher))
4068 (org-prepare-agenda (concat "TAGS " match))
4069 (setq org-agenda-query-string match)
4070 (setq org-agenda-redo-command
4071 (list 'org-tags-view (list 'quote todo-only)
4072 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
2c3ad40d 4073 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
4074 rtnall nil)
4075 (while (setq file (pop files))
4076 (catch 'nextfile
4077 (org-check-agenda-file file)
4078 (setq buffer (if (file-exists-p file)
4079 (org-get-agenda-file-buffer file)
4080 (error "No such file %s" file)))
4081 (if (not buffer)
33306645 4082 ;; If file does not exist, error message to agenda
20908596
CD
4083 (setq rtn (list
4084 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4085 rtnall (append rtnall rtn))
4086 (with-current-buffer buffer
4087 (unless (org-mode-p)
4088 (error "Agenda file %s is not in `org-mode'" file))
4089 (save-excursion
4090 (save-restriction
4091 (if org-agenda-restrict
4092 (narrow-to-region org-agenda-restrict-begin
4093 org-agenda-restrict-end)
4094 (widen))
4095 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4096 (setq rtnall (append rtnall rtn))))))))
4097 (if org-agenda-overriding-header
4098 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4099 nil 'face 'org-agenda-structure) "\n")
4100 (insert "Headlines with TAGS match: ")
4101 (add-text-properties (point-min) (1- (point))
8d642074
CD
4102 (list 'face 'org-agenda-structure
4103 'short-heading
4104 (concat "Match: " match)))
20908596
CD
4105 (setq pos (point))
4106 (insert match "\n")
4107 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4108 (setq pos (point))
4109 (unless org-agenda-multi
4110 (insert "Press `C-u r' to search again with new search string\n"))
4111 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 4112 (org-agenda-mark-header-line (point-min))
20908596
CD
4113 (when rtnall
4114 (insert (org-finalize-agenda-entries rtnall) "\n"))
4115 (goto-char (point-min))
c8d0cf5c 4116 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
4117 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4118 (org-finalize-agenda)
4119 (setq buffer-read-only t)))
4120
4121;;; Agenda Finding stuck projects
4122
4123(defvar org-agenda-skip-regexp nil
4124 "Regular expression used in skipping subtrees for the agenda.
4125This is basically a temporary global variable that can be set and then
4126used by user-defined selections using `org-agenda-skip-function'.")
4127
4128(defvar org-agenda-overriding-header nil
3ab2c837 4129 "When set during agenda, todo and tags searches it replaces the header.
c8d0cf5c
CD
4130This variable should not be set directly, but custom commands can bind it
4131in the options section.")
4132
4133(defun org-agenda-skip-entry-when-regexp-matches ()
86fbb8ca 4134 "Check if the current entry contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4135If yes, it returns the end position of this entry, causing agenda commands
4136to skip the entry but continuing the search in the subtree. This is a
4137function that can be put into `org-agenda-skip-function' for the duration
4138of a command."
4139 (let ((end (save-excursion (org-end-of-subtree t)))
4140 skip)
4141 (save-excursion
4142 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4143 (and skip end)))
20908596
CD
4144
4145(defun org-agenda-skip-subtree-when-regexp-matches ()
86fbb8ca 4146 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
20908596
CD
4147If yes, it returns the end position of this tree, causing agenda commands
4148to skip this subtree. This is a function that can be put into
4149`org-agenda-skip-function' for the duration of a command."
4150 (let ((end (save-excursion (org-end-of-subtree t)))
4151 skip)
4152 (save-excursion
4153 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4154 (and skip end)))
4155
c8d0cf5c 4156(defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
86fbb8ca 4157 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4158If yes, it returns the end position of the current entry (NOT the tree),
4159causing agenda commands to skip the entry but continuing the search in
4160the subtree. This is a function that can be put into
4161`org-agenda-skip-function' for the duration of a command. An important
4162use of this function is for the stuck project list."
4163 (let ((end (save-excursion (org-end-of-subtree t)))
4164 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4165 skip)
4166 (save-excursion
4167 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4168 (and skip entry-end)))
4169
20908596
CD
4170(defun org-agenda-skip-entry-if (&rest conditions)
4171 "Skip entry if any of CONDITIONS is true.
4172See `org-agenda-skip-if' for details."
4173 (org-agenda-skip-if nil conditions))
4174
4175(defun org-agenda-skip-subtree-if (&rest conditions)
4176 "Skip entry if any of CONDITIONS is true.
4177See `org-agenda-skip-if' for details."
4178 (org-agenda-skip-if t conditions))
4179
4180(defun org-agenda-skip-if (subtree conditions)
4181 "Checks current entity for CONDITIONS.
4182If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4183the entry, i.e. the text before the next heading is checked.
4184
4185CONDITIONS is a list of symbols, boolean OR is used to combine the results
4186from different tests. Valid conditions are:
4187
4188scheduled Check if there is a scheduled cookie
4189notscheduled Check if there is no scheduled cookie
4190deadline Check if there is a deadline
4191notdeadline Check if there is no deadline
c8d0cf5c
CD
4192timestamp Check if there is a timestamp (also deadline or scheduled)
4193nottimestamp Check if there is no timestamp (also deadline or scheduled)
20908596
CD
4194regexp Check if regexp matches
4195notregexp Check if regexp does not match.
ed21c5c8
CD
4196todo Check if TODO keyword matches
4197nottodo Check if TODO keyword does not match
20908596
CD
4198
4199The regexp is taken from the conditions list, it must come right after
4200the `regexp' or `notregexp' element.
4201
ed21c5c8
CD
4202`todo' and `nottodo' accept as an argument a list of todo
4203keywords, which may include \"*\" to match any todo keyword.
4204
4205 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4206
4207would skip all entries with \"TODO\" or \"WAITING\" keywords.
4208
4209Instead of a list a keyword class may be given
4210
4211 (org-agenda-skip-entry-if 'nottodo 'done)
4212
4213would skip entries that haven't been marked with any of \"DONE\"
4214keywords. Possible classes are: `todo', `done', `any'.
4215
20908596
CD
4216If any of these conditions is met, this function returns the end point of
4217the entity, causing the search to continue from there. This is a function
4218that can be put into `org-agenda-skip-function' for the duration of a command."
4219 (let (beg end m)
4220 (org-back-to-heading t)
4221 (setq beg (point)
4222 end (if subtree
4223 (progn (org-end-of-subtree t) (point))
4224 (progn (outline-next-heading) (1- (point)))))
4225 (goto-char beg)
4226 (and
4227 (or
4228 (and (memq 'scheduled conditions)
4229 (re-search-forward org-scheduled-time-regexp end t))
4230 (and (memq 'notscheduled conditions)
4231 (not (re-search-forward org-scheduled-time-regexp end t)))
4232 (and (memq 'deadline conditions)
4233 (re-search-forward org-deadline-time-regexp end t))
4234 (and (memq 'notdeadline conditions)
4235 (not (re-search-forward org-deadline-time-regexp end t)))
c8d0cf5c
CD
4236 (and (memq 'timestamp conditions)
4237 (re-search-forward org-ts-regexp end t))
4238 (and (memq 'nottimestamp conditions)
4239 (not (re-search-forward org-ts-regexp end t)))
20908596
CD
4240 (and (setq m (memq 'regexp conditions))
4241 (stringp (nth 1 m))
4242 (re-search-forward (nth 1 m) end t))
4243 (and (setq m (memq 'notregexp conditions))
4244 (stringp (nth 1 m))
ed21c5c8
CD
4245 (not (re-search-forward (nth 1 m) end t)))
4246 (and (or
4247 (setq m (memq 'todo conditions))
4248 (setq m (memq 'nottodo conditions)))
4249 (org-agenda-skip-if-todo m end)))
20908596
CD
4250 end)))
4251
ed21c5c8
CD
4252(defun org-agenda-skip-if-todo (args end)
4253 "Helper function for `org-agenda-skip-if', do not use it directly.
4254ARGS is a list with first element either `todo' or `nottodo'.
4255The remainder is either a list of TODO keywords, or a state symbol
4256`todo' or `done' or `any'."
4257 (let ((kw (car args))
4258 (arg (cadr args))
4259 todo-wds todo-re)
4260 (setq todo-wds
4261 (org-uniquify
4262 (cond
4263 ((listp arg) ;; list of keywords
4264 (if (member "*" arg)
4265 (mapcar 'substring-no-properties org-todo-keywords-1)
4266 arg))
4267 ((symbolp arg) ;; keyword class name
4268 (cond
4269 ((eq arg 'todo)
4270 (org-delete-all org-done-keywords
4271 (mapcar 'substring-no-properties
4272 org-todo-keywords-1)))
4273 ((eq arg 'done) org-done-keywords)
4274 ((eq arg 'any)
4275 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4276 (setq todo-re
4277 (concat "^\\*+[ \t]+\\<\\("
4278 (mapconcat 'identity todo-wds "\\|")
4279 "\\)\\>"))
4280 (if (eq kw 'todo)
4281 (re-search-forward todo-re end t)
4282 (not (re-search-forward todo-re end t)))))
4283
20908596
CD
4284;;;###autoload
4285(defun org-agenda-list-stuck-projects (&rest ignore)
4286 "Create agenda view for projects that are stuck.
4287Stuck projects are project that have no next actions. For the definitions
4288of what a project is and how to check if it stuck, customize the variable
afe98dfa 4289`org-stuck-projects'."
20908596 4290 (interactive)
c8d0cf5c
CD
4291 (let* ((org-agenda-skip-function
4292 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
20908596 4293 ;; We could have used org-agenda-skip-if here.
c8d0cf5c
CD
4294 (org-agenda-overriding-header
4295 (or org-agenda-overriding-header "List of stuck projects: "))
20908596
CD
4296 (matcher (nth 0 org-stuck-projects))
4297 (todo (nth 1 org-stuck-projects))
4298 (todo-wds (if (member "*" todo)
4299 (progn
2c3ad40d
CD
4300 (org-prepare-agenda-buffers (org-agenda-files
4301 nil 'ifmode))
20908596
CD
4302 (org-delete-all
4303 org-done-keywords-for-agenda
4304 (copy-sequence org-todo-keywords-for-agenda)))
4305 todo))
4306 (todo-re (concat "^\\*+[ \t]+\\("
4307 (mapconcat 'identity todo-wds "\\|")
4308 "\\)\\>"))
4309 (tags (nth 2 org-stuck-projects))
4310 (tags-re (if (member "*" tags)
3ab2c837
BG
4311 (org-re (concat org-outline-regexp-bol
4312 ".*:[[:alnum:]_@#%]+:[ \t]*$"))
c8d0cf5c 4313 (if tags
3ab2c837
BG
4314 (concat org-outline-regexp-bol
4315 ".*:\\("
c8d0cf5c 4316 (mapconcat 'identity tags "\\|")
afe98dfa 4317 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
20908596
CD
4318 (gen-re (nth 3 org-stuck-projects))
4319 (re-list
4320 (delq nil
4321 (list
4322 (if todo todo-re)
4323 (if tags tags-re)
4324 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4325 gen-re)))))
4326 (setq org-agenda-skip-regexp
4327 (if re-list
4328 (mapconcat 'identity re-list "\\|")
4329 (error "No information how to identify unstuck projects")))
4330 (org-tags-view nil matcher)
4331 (with-current-buffer org-agenda-buffer-name
4332 (setq org-agenda-redo-command
4333 '(org-agenda-list-stuck-projects
4334 (or current-prefix-arg org-last-arg))))))
4335
4336;;; Diary integration
4337
4338(defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4339(defvar list-diary-entries-hook)
3ab2c837 4340(defvar diary-time-regexp)
20908596
CD
4341(defun org-get-entries-from-diary (date)
4342 "Get the (Emacs Calendar) diary entries for DATE."
4343 (require 'diary-lib)
4344 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
20908596 4345 (diary-display-hook '(fancy-diary-display))
ca8ef0dc 4346 (diary-display-function 'fancy-diary-display)
20908596
CD
4347 (pop-up-frames nil)
4348 (list-diary-entries-hook
4349 (cons 'org-diary-default-entry list-diary-entries-hook))
4350 (diary-file-name-prefix-function nil) ; turn this feature off
4351 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4352 entries
4353 (org-disable-agenda-to-diary t))
4354 (save-excursion
4355 (save-window-excursion
4356 (funcall (if (fboundp 'diary-list-entries)
4357 'diary-list-entries 'list-diary-entries)
4358 date 1)))
4359 (if (not (get-buffer diary-fancy-buffer))
4360 (setq entries nil)
4361 (with-current-buffer diary-fancy-buffer
4362 (setq buffer-read-only nil)
4363 (if (zerop (buffer-size))
4364 ;; No entries
4365 (setq entries nil)
4366 ;; Omit the date and other unnecessary stuff
4367 (org-agenda-cleanup-fancy-diary)
4368 ;; Add prefix to each line and extend the text properties
4369 (if (zerop (buffer-size))
4370 (setq entries nil)
3ab2c837
BG
4371 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4372 (setq entries
4373 (with-temp-buffer
4374 (insert entries) (goto-char (point-min))
4375 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4376 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4377 (replace-match (concat "; " (match-string 1)))))
4378 (buffer-string)))))
20908596
CD
4379 (set-buffer-modified-p nil)
4380 (kill-buffer diary-fancy-buffer)))
4381 (when entries
4382 (setq entries (org-split-string entries "\n"))
4383 (setq entries
4384 (mapcar
4385 (lambda (x)
4386 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4387 ;; Extend the text properties to the beginning of the line
4388 (org-add-props x (text-properties-at (1- (length x)) x)
ed21c5c8 4389 'type "diary" 'date date 'face 'org-agenda-diary))
20908596
CD
4390 entries)))))
4391
c8d0cf5c
CD
4392(defvar org-agenda-cleanup-fancy-diary-hook nil
4393 "Hook run when the fancy diary buffer is cleaned up.")
4394
20908596
CD
4395(defun org-agenda-cleanup-fancy-diary ()
4396 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4397This gets rid of the date, the underline under the date, and
4398the dummy entry installed by `org-mode' to ensure non-empty diary for each
4399date. It also removes lines that contain only whitespace."
4400 (goto-char (point-min))
4401 (if (looking-at ".*?:[ \t]*")
4402 (progn
4403 (replace-match "")
4404 (re-search-forward "\n=+$" nil t)
4405 (replace-match "")
4406 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4407 (re-search-forward "\n=+$" nil t)
4408 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4409 (goto-char (point-min))
4410 (while (re-search-forward "^ +\n" nil t)
4411 (replace-match ""))
4412 (goto-char (point-min))
4413 (if (re-search-forward "^Org-mode dummy\n?" nil t)
c8d0cf5c
CD
4414 (replace-match ""))
4415 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
20908596
CD
4416
4417;; Make sure entries from the diary have the right text properties.
4418(eval-after-load "diary-lib"
4419 '(if (boundp 'diary-modify-entry-list-string-function)
4420 ;; We can rely on the hook, nothing to do
4421 nil
33306645 4422 ;; Hook not available, must use advice to make this work
20908596
CD
4423 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4424 "Make the position visible."
4425 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4426 (stringp string)
4427 buffer-file-name)
4428 (setq string (org-modify-diary-entry-string string))))))
4429
4430(defun org-modify-diary-entry-string (string)
4431 "Add text properties to string, allowing org-mode to act on it."
4432 (org-add-props string nil
4433 'mouse-face 'highlight
20908596
CD
4434 'help-echo (if buffer-file-name
4435 (format "mouse-2 or RET jump to diary file %s"
4436 (abbreviate-file-name buffer-file-name))
4437 "")
4438 'org-agenda-diary-link t
4439 'org-marker (org-agenda-new-marker (point-at-bol))))
4440
4441(defun org-diary-default-entry ()
4442 "Add a dummy entry to the diary.
4443Needed to avoid empty dates which mess up holiday display."
4444 ;; Catch the error if dealing with the new add-to-diary-alist
4445 (when org-disable-agenda-to-diary
4446 (condition-case nil
4447 (org-add-to-diary-list original-date "Org-mode dummy" "")
4448 (error
4449 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4450
4451(defun org-add-to-diary-list (&rest args)
4452 (if (fboundp 'diary-add-to-list)
4453 (apply 'diary-add-to-list args)
4454 (apply 'add-to-diary-list args)))
4455
ed21c5c8
CD
4456(defvar org-diary-last-run-time nil)
4457
20908596
CD
4458;;;###autoload
4459(defun org-diary (&rest args)
4460 "Return diary information from org-files.
4461This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4462It accesses org files and extracts information from those files to be
4463listed in the diary. The function accepts arguments specifying what
ed21c5c8
CD
4464items should be listed. For a list of arguments allowed here, see the
4465variable `org-agenda-entry-types'.
20908596
CD
4466
4467The call in the diary file should look like this:
4468
4469 &%%(org-diary) ~/path/to/some/orgfile.org
4470
4471Use a separate line for each org file to check. Or, if you omit the file name,
4472all files listed in `org-agenda-files' will be checked automatically:
4473
4474 &%%(org-diary)
4475
4476If you don't give any arguments (as in the example above), the default
4477arguments (:deadline :scheduled :timestamp :sexp) are used.
4478So the example above may also be written as
4479
4480 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4481
4482The function expects the lisp variables `entry' and `date' to be provided
4483by the caller, because this is how the calendar works. Don't use this
4484function from a program - use `org-agenda-get-day-entries' instead."
54a0dee5 4485 (when (> (- (org-float-time)
20908596
CD
4486 org-agenda-last-marker-time)
4487 5)
4488 (org-agenda-reset-markers))
4489 (org-compile-prefix-format 'agenda)
4490 (org-set-sorting-strategy 'agenda)
4491 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4492 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4493 (list entry)
4494 (org-agenda-files t)))
ed21c5c8 4495 (time (org-float-time))
20908596 4496 file rtn results)
ed21c5c8
CD
4497 (when (or (not org-diary-last-run-time)
4498 (> (- time
4499 org-diary-last-run-time)
4500 3))
4501 (org-prepare-agenda-buffers files))
4502 (setq org-diary-last-run-time time)
20908596
CD
4503 ;; If this is called during org-agenda, don't return any entries to
4504 ;; the calendar. Org Agenda will list these entries itself.
4505 (if org-disable-agenda-to-diary (setq files nil))
4506 (while (setq file (pop files))
4507 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4508 (setq results (append results rtn)))
4509 (if results
4510 (concat (org-finalize-agenda-entries results) "\n"))))
4511
4512;;; Agenda entry finders
4513
4514(defun org-agenda-get-day-entries (file date &rest args)
4515 "Does the work for `org-diary' and `org-agenda'.
4516FILE is the path to a file to be checked for entries. DATE is date like
4517the one returned by `calendar-current-date'. ARGS are symbols indicating
4518which kind of entries should be extracted. For details about these, see
4519the documentation of `org-diary'."
4520 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4521 (let* ((org-startup-folded nil)
4522 (org-startup-align-all-tables nil)
4523 (buffer (if (file-exists-p file)
4524 (org-get-agenda-file-buffer file)
4525 (error "No such file %s" file)))
54a0dee5 4526 arg results rtn deadline-results)
20908596
CD
4527 (if (not buffer)
4528 ;; If file does not exist, make sure an error message ends up in diary
4529 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4530 (with-current-buffer buffer
4531 (unless (org-mode-p)
4532 (error "Agenda file %s is not in `org-mode'" file))
4533 (let ((case-fold-search nil))
4534 (save-excursion
4535 (save-restriction
4536 (if org-agenda-restrict
4537 (narrow-to-region org-agenda-restrict-begin
4538 org-agenda-restrict-end)
4539 (widen))
4540 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4541 (while (setq arg (pop args))
4542 (cond
4543 ((and (eq arg :todo)
3ab2c837
BG
4544 (equal date (calendar-gregorian-from-absolute
4545 (org-today))))
20908596
CD
4546 (setq rtn (org-agenda-get-todos))
4547 (setq results (append results rtn)))
4548 ((eq arg :timestamp)
4549 (setq rtn (org-agenda-get-blocks))
4550 (setq results (append results rtn))
4551 (setq rtn (org-agenda-get-timestamps))
4552 (setq results (append results rtn)))
4553 ((eq arg :sexp)
4554 (setq rtn (org-agenda-get-sexps))
4555 (setq results (append results rtn)))
4556 ((eq arg :scheduled)
54a0dee5 4557 (setq rtn (org-agenda-get-scheduled deadline-results))
20908596
CD
4558 (setq results (append results rtn)))
4559 ((eq arg :closed)
93b62de8 4560 (setq rtn (org-agenda-get-progress))
20908596
CD
4561 (setq results (append results rtn)))
4562 ((eq arg :deadline)
4563 (setq rtn (org-agenda-get-deadlines))
54a0dee5 4564 (setq deadline-results (copy-sequence rtn))
20908596
CD
4565 (setq results (append results rtn))))))))
4566 results))))
4567
4568(defun org-agenda-get-todos ()
4569 "Return the TODO information for agenda display."
4570 (let* ((props (list 'face nil
c8d0cf5c 4571 'done-face 'org-agenda-done
20908596
CD
4572 'org-not-done-regexp org-not-done-regexp
4573 'org-todo-regexp org-todo-regexp
b349f79f 4574 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4575 'mouse-face 'highlight
20908596
CD
4576 'help-echo
4577 (format "mouse-2 or RET jump to org file %s"
4578 (abbreviate-file-name buffer-file-name))))
4579 (regexp (concat "^\\*+[ \t]+\\("
4580 (if org-select-this-todo-keyword
4581 (if (equal org-select-this-todo-keyword "*")
4582 org-todo-regexp
4583 (concat "\\<\\("
4584 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4585 "\\)\\>"))
4586 org-not-done-regexp)
4587 "[^\n\r]*\\)"))
621f83e4 4588 marker priority category tags todo-state
20908596
CD
4589 ee txt beg end)
4590 (goto-char (point-min))
4591 (while (re-search-forward regexp nil t)
4592 (catch :skip
4593 (save-match-data
4594 (beginning-of-line)
3ab2c837 4595 (org-agenda-skip)
d6685abc 4596 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
0bd48b37 4597 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
20908596
CD
4598 (goto-char (1+ beg))
4599 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4600 (throw :skip nil)))
20908596
CD
4601 (goto-char (match-beginning 1))
4602 (setq marker (org-agenda-new-marker (match-beginning 0))
4603 category (org-get-category)
c8d0cf5c 4604 txt (match-string 1)
20908596 4605 tags (org-get-tags-at (point))
c8d0cf5c 4606 txt (org-format-agenda-item "" txt category tags)
621f83e4
CD
4607 priority (1+ (org-get-priority txt))
4608 todo-state (org-get-todo-state))
20908596
CD
4609 (org-add-props txt props
4610 'org-marker marker 'org-hd-marker marker
4611 'priority priority 'org-category category
621f83e4 4612 'type "todo" 'todo-state todo-state)
20908596
CD
4613 (push txt ee)
4614 (if org-agenda-todo-list-sublevels
4615 (goto-char (match-end 1))
4616 (org-end-of-subtree 'invisible))))
4617 (nreverse ee)))
4618
3ab2c837
BG
4619(defun org-agenda-todo-custom-ignore-p (time n)
4620 "Check whether timestamp is farther away then n number of days.
4621This function is invoked if `org-agenda-todo-ignore-deadlines',
4622`org-agenda-todo-ignore-scheduled' or
4623`org-agenda-todo-ignore-timestamp' is set to an integer."
4624 (let ((days (org-days-to-time time)))
4625 (if (>= n 0)
4626 (>= days n)
4627 (<= days n))))
4628
0bd48b37 4629;;;###autoload
ed21c5c8
CD
4630(defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4631 (&optional end)
4632 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
0bd48b37
CD
4633 (when (or org-agenda-todo-ignore-with-date
4634 org-agenda-todo-ignore-scheduled
acedf35c
CD
4635 org-agenda-todo-ignore-deadlines
4636 org-agenda-todo-ignore-timestamp)
0bd48b37
CD
4637 (setq end (or end (save-excursion (outline-next-heading) (point))))
4638 (save-excursion
4639 (or (and org-agenda-todo-ignore-with-date
4640 (re-search-forward org-ts-regexp end t))
4641 (and org-agenda-todo-ignore-scheduled
ed21c5c8
CD
4642 (re-search-forward org-scheduled-time-regexp end t)
4643 (cond
4644 ((eq org-agenda-todo-ignore-scheduled 'future)
4645 (> (org-days-to-time (match-string 1)) 0))
4646 ((eq org-agenda-todo-ignore-scheduled 'past)
4647 (<= (org-days-to-time (match-string 1)) 0))
3ab2c837
BG
4648 ((numberp org-agenda-todo-ignore-scheduled)
4649 (org-agenda-todo-custom-ignore-p
4650 (match-string 1) org-agenda-todo-ignore-scheduled))
ed21c5c8 4651 (t)))
0bd48b37
CD
4652 (and org-agenda-todo-ignore-deadlines
4653 (re-search-forward org-deadline-time-regexp end t)
ed21c5c8
CD
4654 (cond
4655 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4656 ((eq org-agenda-todo-ignore-deadlines 'far)
4657 (not (org-deadline-close (match-string 1))))
4658 ((eq org-agenda-todo-ignore-deadlines 'future)
4659 (> (org-days-to-time (match-string 1)) 0))
4660 ((eq org-agenda-todo-ignore-deadlines 'past)
4661 (<= (org-days-to-time (match-string 1)) 0))
3ab2c837
BG
4662 ((numberp org-agenda-todo-ignore-deadlines)
4663 (org-agenda-todo-custom-ignore-p
4664 (match-string 1) org-agenda-todo-ignore-deadlines))
acedf35c
CD
4665 (t (org-deadline-close (match-string 1)))))
4666 (and org-agenda-todo-ignore-timestamp
4667 (let ((buffer (current-buffer))
4668 (regexp
4669 (concat
4670 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4671 (start (point)))
4672 ;; Copy current buffer into a temporary one
4673 (with-temp-buffer
4674 (insert-buffer-substring buffer start end)
4675 (goto-char (point-min))
4676 ;; Delete SCHEDULED and DEADLINE items
4677 (while (re-search-forward regexp end t)
4678 (delete-region (match-beginning 0) (match-end 0)))
4679 (goto-char (point-min))
4680 ;; No search for timestamp left
4681 (when (re-search-forward org-ts-regexp nil t)
4682 (cond
4683 ((eq org-agenda-todo-ignore-timestamp 'future)
4684 (> (org-days-to-time (match-string 1)) 0))
4685 ((eq org-agenda-todo-ignore-timestamp 'past)
4686 (<= (org-days-to-time (match-string 1)) 0))
3ab2c837
BG
4687 ((numberp org-agenda-todo-ignore-timestamp)
4688 (org-agenda-todo-custom-ignore-p
4689 (match-string 1) org-agenda-todo-ignore-timestamp))
acedf35c 4690 (t))))))))))
0bd48b37 4691
20908596
CD
4692(defconst org-agenda-no-heading-message
4693 "No heading for this item in buffer or region.")
4694
4695(defun org-agenda-get-timestamps ()
4696 "Return the date stamp information for agenda display."
4697 (let* ((props (list 'face nil
4698 'org-not-done-regexp org-not-done-regexp
4699 'org-todo-regexp org-todo-regexp
b349f79f 4700 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4701 'mouse-face 'highlight
20908596
CD
4702 'help-echo
4703 (format "mouse-2 or RET jump to org file %s"
4704 (abbreviate-file-name buffer-file-name))))
4705 (d1 (calendar-absolute-from-gregorian date))
4706 (remove-re
4707 (concat
4708 (regexp-quote
4709 (format-time-string
4710 "<%Y-%m-%d"
4711 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4712 ".*?>"))
4713 (regexp
4714 (concat
4715 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4716 (regexp-quote
4717 (substring
4718 (format-time-string
4719 (car org-time-stamp-formats)
4720 (apply 'encode-time ; DATE bound by calendar
4721 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4722 1 11))
4723 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4724 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4725 marker hdmarker deadlinep scheduledp clockp closedp inactivep
621f83e4 4726 donep tmp priority category ee txt timestr tags b0 b3 e3 head
3ab2c837 4727 todo-state end-of-match show-all)
20908596 4728 (goto-char (point-min))
c8d0cf5c 4729 (while (setq end-of-match (re-search-forward regexp nil t))
20908596 4730 (setq b0 (match-beginning 0)
3ab2c837
BG
4731 b3 (match-beginning 3) e3 (match-end 3)
4732 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4733 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4734 (member todo-state
4735 org-agenda-repeating-timestamp-show-all)))
20908596
CD
4736 (catch :skip
4737 (and (org-at-date-range-p) (throw :skip nil))
4738 (org-agenda-skip)
4739 (if (and (match-end 1)
4740 (not (= d1 (org-time-string-to-absolute
3ab2c837 4741 (match-string 1) d1 nil show-all))))
20908596
CD
4742 (throw :skip nil))
4743 (if (and e3
4744 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4745 (throw :skip nil))
c8d0cf5c 4746 (setq tmp (buffer-substring (max (point-min)
20908596
CD
4747 (- b0 org-ds-keyword-length))
4748 b0)
4749 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4750 inactivep (= (char-after b0) ?\[)
4751 deadlinep (string-match org-deadline-regexp tmp)
4752 scheduledp (string-match org-scheduled-regexp tmp)
4753 closedp (and org-agenda-include-inactive-timestamps
4754 (string-match org-closed-string tmp))
4755 clockp (and org-agenda-include-inactive-timestamps
4756 (or (string-match org-clock-string tmp)
4757 (string-match "]-+\\'" tmp)))
621f83e4 4758 donep (member todo-state org-done-keywords))
c8d0cf5c
CD
4759 (if (or scheduledp deadlinep closedp clockp
4760 (and donep org-agenda-skip-timestamp-if-done))
20908596
CD
4761 (throw :skip t))
4762 (if (string-match ">" timestr)
4763 ;; substring should only run to end of time stamp
4764 (setq timestr (substring timestr 0 (match-end 0))))
c8d0cf5c
CD
4765 (setq marker (org-agenda-new-marker b0)
4766 category (org-get-category b0))
20908596 4767 (save-excursion
3ab2c837 4768 (if (not (re-search-backward org-outline-regexp-bol nil t))
c8d0cf5c
CD
4769 (setq txt org-agenda-no-heading-message)
4770 (goto-char (match-beginning 0))
4771 (setq hdmarker (org-agenda-new-marker)
4772 tags (org-get-tags-at))
4773 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3ab2c837 4774 (setq head (or (match-string 1) ""))
c8d0cf5c 4775 (setq txt (org-format-agenda-item
ed21c5c8 4776 (if inactivep org-agenda-inactive-leader nil)
3ab2c837 4777 head category tags timestr
c8d0cf5c 4778 remove-re)))
20908596
CD
4779 (setq priority (org-get-priority txt))
4780 (org-add-props txt props
4781 'org-marker marker 'org-hd-marker hdmarker)
4782 (org-add-props txt nil 'priority priority
4783 'org-category category 'date date
621f83e4 4784 'todo-state todo-state
20908596
CD
4785 'type "timestamp")
4786 (push txt ee))
c8d0cf5c
CD
4787 (if org-agenda-skip-additional-timestamps-same-entry
4788 (outline-next-heading)
4789 (goto-char end-of-match))))
20908596
CD
4790 (nreverse ee)))
4791
4792(defun org-agenda-get-sexps ()
4793 "Return the sexp information for agenda display."
4794 (require 'diary-lib)
3ab2c837 4795 (let* ((props (list 'mouse-face 'highlight
20908596
CD
4796 'help-echo
4797 (format "mouse-2 or RET jump to org file %s"
4798 (abbreviate-file-name buffer-file-name))))
4799 (regexp "^&?%%(")
c8d0cf5c
CD
4800 marker category ee txt tags entry result beg b sexp sexp-entry
4801 todo-state)
20908596
CD
4802 (goto-char (point-min))
4803 (while (re-search-forward regexp nil t)
4804 (catch :skip
4805 (org-agenda-skip)
4806 (setq beg (match-beginning 0))
4807 (goto-char (1- (match-end 0)))
4808 (setq b (point))
4809 (forward-sexp 1)
4810 (setq sexp (buffer-substring b (point)))
4811 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4812 (org-trim (match-string 1))
4813 ""))
4814 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4815 (when result
4816 (setq marker (org-agenda-new-marker beg)
c8d0cf5c
CD
4817 category (org-get-category beg)
4818 todo-state (org-get-todo-state))
20908596 4819
afe98dfa
CD
4820 (dolist (r (if (stringp result)
4821 (list result)
4822 result)) ;; we expect a list here
4823 (if (string-match "\\S-" r)
4824 (setq txt r)
4825 (setq txt "SEXP entry returned empty string"))
4826
4827 (setq txt (org-format-agenda-item
4828 "" txt category tags 'time))
4829 (org-add-props txt props 'org-marker marker)
4830 (org-add-props txt nil
4831 'org-category category 'date date 'todo-state todo-state
4832 'type "sexp")
4833 (push txt ee)))))
20908596
CD
4834 (nreverse ee)))
4835
3ab2c837
BG
4836;; Calendar sanity: define some functions that are independent of
4837;; `calendar-date-style'.
4838;; Normally I would like to use ISO format when calling the diary functions,
4839;; but to make sure we still have Emacs 22 compatibility we bind
4840;; also `european-calendar-style' and use european format
4841(defun org-anniversary (year month day &optional mark)
4842 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4843 (org-no-warnings
4844 (let ((calendar-date-style 'european) (european-calendar-style t))
4845 (diary-anniversary day month year mark))))
4846(defun org-cyclic (N year month day &optional mark)
4847 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4848 (org-no-warnings
4849 (let ((calendar-date-style 'european) (european-calendar-style t))
4850 (diary-cyclic N day month year mark))))
4851(defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4852 "Like `diary-block', but with fixed (ISO) order of arguments."
4853 (org-no-warnings
4854 (let ((calendar-date-style 'european) (european-calendar-style t))
4855 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4856(defun org-date (year month day &optional mark)
4857 "Like `diary-date', but with fixed (ISO) order of arguments."
4858 (org-no-warnings
4859 (let ((calendar-date-style 'european) (european-calendar-style t))
4860 (diary-date day month year mark))))
4861(defalias 'org-float 'diary-float)
4862
4863;; Define the` org-class' function
4864(defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
ed21c5c8 4865 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
ed21c5c8
CD
4866DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4867is any number of ISO weeks in the block period for which the item should
4868be skipped."
3ab2c837
BG
4869 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4870 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
ed21c5c8
CD
4871 (d (calendar-absolute-from-gregorian date)))
4872 (and
4873 (<= date1 d)
4874 (<= d date2)
4875 (= (calendar-day-of-week date) dayname)
4876 (or (not skip-weeks)
4877 (progn
4878 (require 'cal-iso)
4879 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4880 entry)))
4881
3ab2c837
BG
4882(defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4883 "Like `org-class', but honor `calendar-date-style'.
4884The order of the first 2 times 3 arguments depends on the variable
4885`calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4886So for American calendars, give this as MONTH DAY YEAR, for European as
4887DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4888DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4889is any number of ISO weeks in the block period for which the item should
4890be skipped.
4891
4892This function is here only for backward compatibility and it is deprecated,
4893please use `org-class' instead."
4894 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4895 (date2 (org-order-calendar-date-args m2 d2 y2)))
4896 (org-class
4897 (nth 2 date1) (car date1) (nth 1 date1)
4898 (nth 2 date2) (car date2) (nth 1 date2)
4899 dayname skip-weeks)))
4900
d60b1ba1 4901(defalias 'org-get-closed 'org-agenda-get-progress)
93b62de8 4902(defun org-agenda-get-progress ()
20908596
CD
4903 "Return the logged TODO entries for agenda display."
4904 (let* ((props (list 'mouse-face 'highlight
4905 'org-not-done-regexp org-not-done-regexp
4906 'org-todo-regexp org-todo-regexp
b349f79f 4907 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
4908 'help-echo
4909 (format "mouse-2 or RET jump to org file %s"
4910 (abbreviate-file-name buffer-file-name))))
93b62de8
CD
4911 (items (if (consp org-agenda-show-log)
4912 org-agenda-show-log
3ab2c837
BG
4913 (if (eq org-agenda-show-log 'clockcheck)
4914 '(clock)
4915 org-agenda-log-mode-items)))
ff4be292 4916 (parts
93b62de8
CD
4917 (delq nil
4918 (list
4919 (if (memq 'closed items) (concat "\\<" org-closed-string))
4920 (if (memq 'clock items) (concat "\\<" org-clock-string))
c8d0cf5c 4921 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
93b62de8
CD
4922 (parts-re (if parts (mapconcat 'identity parts "\\|")
4923 (error "`org-agenda-log-mode-items' is empty")))
20908596 4924 (regexp (concat
93b62de8
CD
4925 "\\(" parts-re "\\)"
4926 " *\\["
20908596
CD
4927 (regexp-quote
4928 (substring
4929 (format-time-string
4930 (car org-time-stamp-formats)
4931 (apply 'encode-time ; DATE bound by calendar
4932 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4933 1 11))))
c8d0cf5c
CD
4934 (org-agenda-search-headline-for-time nil)
4935 marker hdmarker priority category tags closedp statep clockp state
4936 ee txt extra timestr rest clocked)
20908596
CD
4937 (goto-char (point-min))
4938 (while (re-search-forward regexp nil t)
4939 (catch :skip
4940 (org-agenda-skip)
4941 (setq marker (org-agenda-new-marker (match-beginning 0))
4942 closedp (equal (match-string 1) org-closed-string)
93b62de8 4943 statep (equal (string-to-char (match-string 1)) ?-)
c8d0cf5c 4944 clockp (not (or closedp statep))
93b62de8 4945 state (and statep (match-string 2))
20908596
CD
4946 category (org-get-category (match-beginning 0))
4947 timestr (buffer-substring (match-beginning 0) (point-at-eol))
20908596 4948 )
b349f79f
CD
4949 (when (string-match "\\]" timestr)
4950 ;; substring should only run to end of time stamp
4951 (setq rest (substring timestr (match-end 0))
4952 timestr (substring timestr 0 (match-end 0)))
93b62de8 4953 (if (and (not closedp) (not statep)
c8d0cf5c 4954 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
621f83e4
CD
4955 (progn (setq timestr (concat (substring timestr 0 -1)
4956 "-" (match-string 1 rest) "]"))
4957 (setq clocked (match-string 2 rest)))
4958 (setq clocked "-")))
20908596 4959 (save-excursion
3ab2c837
BG
4960 (setq extra
4961 (cond
4962 ((not org-agenda-log-mode-add-notes) nil)
4963 (statep
4964 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4965 (match-string 1)))
4966 (clockp
4967 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4968 (match-string 1)))))
4969 (if (not (re-search-backward org-outline-regexp-bol nil t))
c8d0cf5c
CD
4970 (setq txt org-agenda-no-heading-message)
4971 (goto-char (match-beginning 0))
4972 (setq hdmarker (org-agenda-new-marker)
4973 tags (org-get-tags-at))
4974 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4975 (setq txt (match-string 1))
4976 (when extra
4977 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4978 (setq txt (concat (substring txt 0 (match-beginning 1))
4979 " - " extra " " (match-string 2 txt)))
4980 (setq txt (concat txt " - " extra))))
4981 (setq txt (org-format-agenda-item
4982 (cond
4983 (closedp "Closed: ")
93b62de8
CD
4984 (statep (concat "State: (" state ")"))
4985 (t (concat "Clocked: (" clocked ")")))
c8d0cf5c 4986 txt category tags timestr)))
20908596
CD
4987 (setq priority 100000)
4988 (org-add-props txt props
c8d0cf5c 4989 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
20908596
CD
4990 'priority priority 'org-category category
4991 'type "closed" 'date date
c8d0cf5c 4992 'undone-face 'org-warning 'done-face 'org-agenda-done)
20908596
CD
4993 (push txt ee))
4994 (goto-char (point-at-eol))))
4995 (nreverse ee)))
4996
3ab2c837
BG
4997(defun org-agenda-show-clocking-issues ()
4998 "Add overlays, showing issues with clocking.
4999See also the user option `org-agenda-clock-consistency-checks'."
5000 (interactive)
5001 (let* ((pl org-agenda-clock-consistency-checks)
5002 (re (concat "^[ \t]*"
5003 org-clock-string
5004 "[ \t]+"
5005 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5006 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5007 (tlstart 0.)
5008 (tlend 0.)
5009 (maxtime (org-hh:mm-string-to-minutes
5010 (or (plist-get pl :max-duration) "24:00")))
5011 (mintime (org-hh:mm-string-to-minutes
5012 (or (plist-get pl :min-duration) 0)))
5013 (maxgap (org-hh:mm-string-to-minutes
5014 ;; default 30:00 means never complain
5015 (or (plist-get pl :max-gap) "30:00")))
5016 (gapok (mapcar 'org-hh:mm-string-to-minutes
5017 (plist-get pl :gap-ok-around)))
5018 (def-face (or (plist-get pl :default-face)
5019 '((:background "DarkRed") (:foreground "white"))))
5020 issue face m te ts dt ov)
5021 (goto-char (point-min))
5022 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5023 (setq issue nil face def-face)
5024 (catch 'next
5025 (setq m (org-get-at-bol 'org-marker)
5026 te nil ts nil)
5027 (unless (and m (markerp m))
5028 (setq issue "No valid clock line") (throw 'next t))
5029 (org-with-point-at m
5030 (save-excursion
5031 (goto-char (point-at-bol))
5032 (unless (looking-at re)
5033 (error "No valid Clock line")
5034 (throw 'next t))
5035 (unless (match-end 3)
5036 (setq issue "No end time"
5037 face (or (plist-get pl :no-end-time-face) face))
5038 (throw 'next t))
5039 (setq ts (match-string 1)
5040 te (match-string 3)
5041 ts (org-float-time
5042 (apply 'encode-time (org-parse-time-string ts)))
5043 te (org-float-time
5044 (apply 'encode-time (org-parse-time-string te)))
5045 dt (- te ts))))
5046 (cond
5047 ((> dt (* 60 maxtime))
5048 ;; a very long clocking chunk
5049 (setq issue (format "Clocking interval is very long: %s"
5050 (org-minutes-to-hh:mm-string
5051 (floor (/ (float dt) 60.))))
5052 face (or (plist-get pl :long-face) face)))
5053 ((< dt (* 60 mintime))
5054 ;; a very short clocking chunk
5055 (setq issue (format "Clocking interval is very short: %s"
5056 (org-minutes-to-hh:mm-string
5057 (floor (/ (float dt) 60.))))
5058 face (or (plist-get pl :short-face) face)))
5059 ((and (> tlend 0) (< ts tlend))
5060 ;; Two clock entries are overlapping
5061 (setq issue (format "Clocking overlap: %d minutes"
5062 (/ (- tlend ts) 60))
5063 face (or (plist-get pl :overlap-face) face)))
5064 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5065 ;; There is a gap, lets see if we need to report it
5066 (unless (org-agenda-check-clock-gap tlend ts gapok)
5067 (setq issue (format "Clocking gap: %d minutes"
5068 (/ (- ts tlend) 60))
5069 face (or (plist-get pl :gap-face) face))))
5070 (t nil)))
5071 (setq tlend (or te tlend) tlstart (or ts tlstart))
5072 (when issue
5073 ;; OK, there was some issue, add an overlay to show the issue
5074 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5075 (overlay-put ov 'before-string
5076 (concat
5077 (org-add-props
5078 (format "%-43s" (concat " " issue))
5079 nil
5080 'face face)
5081 "\n"))
5082 (overlay-put ov 'evaporate t)))))
5083
5084(defun org-agenda-check-clock-gap (t1 t2 ok-list)
5085 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5086 (catch 'exit
5087 (unless ok-list
5088 ;; there are no OK times for gaps...
5089 (throw 'exit nil))
5090 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5091 ;; This is more than 24 hours, so it is OK.
5092 ;; because we have at least one OK time, that must be in the
5093 ;; 24 hour interval.
5094 (throw 'exit t))
5095 ;; We have a shorter gap.
5096 ;; Now we have to get the minute of the day when these times are
5097 (let* ((t1dec (decode-time (seconds-to-time t1)))
5098 (t2dec (decode-time (seconds-to-time t2)))
5099 ;; compute the minute on the day
5100 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5101 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5102 (when (< min2 min1)
5103 ;; if min2 is smaller than min1, this means it is on the next day.
5104 ;; Wrap it to after midnight.
5105 (setq min2 (+ min2 1440)))
5106 ;; Now check if any of the OK times is in the gap
5107 (mapc (lambda (x)
5108 ;; Wrap the time to after midnight if necessary
5109 (if (< x min1) (setq x (+ x 1440)))
5110 ;; Check if in interval
5111 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5112 ok-list)
5113 ;; Nope, this gap is not OK
5114 nil)))
5115
20908596
CD
5116(defun org-agenda-get-deadlines ()
5117 "Return the deadline information for agenda display."
5118 (let* ((props (list 'mouse-face 'highlight
5119 'org-not-done-regexp org-not-done-regexp
5120 'org-todo-regexp org-todo-regexp
b349f79f 5121 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
5122 'help-echo
5123 (format "mouse-2 or RET jump to org file %s"
5124 (abbreviate-file-name buffer-file-name))))
5125 (regexp org-deadline-time-regexp)
621f83e4 5126 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596
CD
5127 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5128 d2 diff dfrac wdays pos pos1 category tags
ed21c5c8 5129 suppress-prewarning
3ab2c837 5130 ee txt head face s todo-state show-all upcomingp donep timestr)
20908596
CD
5131 (goto-char (point-min))
5132 (while (re-search-forward regexp nil t)
ed21c5c8 5133 (setq suppress-prewarning nil)
20908596
CD
5134 (catch :skip
5135 (org-agenda-skip)
ed21c5c8
CD
5136 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5137 (save-match-data
5138 (string-match org-scheduled-time-regexp
5139 (buffer-substring (point-at-bol)
5140 (point-at-eol)))))
5141 (setq suppress-prewarning
5142 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5143 org-agenda-skip-deadline-prewarning-if-scheduled
5144 0)))
20908596 5145 (setq s (match-string 1)
c8d0cf5c 5146 txt nil
20908596 5147 pos (1- (match-beginning 1))
3ab2c837
BG
5148 todo-state (save-match-data (org-get-todo-state))
5149 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5150 (member todo-state
5151 org-agenda-repeating-timestamp-show-all))
20908596 5152 d2 (org-time-string-to-absolute
3ab2c837 5153 (match-string 1) d1 'past show-all)
20908596 5154 diff (- d2 d1)
ed21c5c8
CD
5155 wdays (if suppress-prewarning
5156 (let ((org-deadline-warning-days suppress-prewarning))
5157 (org-get-wdays s))
5158 (org-get-wdays s))
20908596
CD
5159 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
5160 upcomingp (and todayp (> diff 0)))
5161 ;; When to show a deadline in the calendar:
5162 ;; If the expiration is within wdays warning time.
5163 ;; Past-due deadlines are only shown on the current date
8bfe682a
CD
5164 (if (and (or (and (<= diff wdays)
5165 (and todayp (not org-agenda-only-exact-dates)))
5166 (= diff 0)))
20908596 5167 (save-excursion
3ab2c837 5168 ;; (setq todo-state (org-get-todo-state))
c8d0cf5c
CD
5169 (setq donep (member todo-state org-done-keywords))
5170 (if (and donep
5171 (or org-agenda-skip-deadline-if-done
5172 (not (= diff 0))))
5173 (setq txt nil)
5174 (setq category (org-get-category))
5175 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5176 (setq txt org-agenda-no-heading-message)
5177 (goto-char (match-end 0))
5178 (setq pos1 (match-beginning 0))
5179 (setq tags (org-get-tags-at pos1))
5180 (setq head (buffer-substring-no-properties
5181 (point)
5182 (progn (skip-chars-forward "^\r\n")
5183 (point))))
5184 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5185 (setq timestr
5186 (concat (substring s (match-beginning 1)) " "))
5187 (setq timestr 'time))
5188 (setq txt (org-format-agenda-item
5189 (if (= diff 0)
5190 (car org-agenda-deadline-leaders)
5191 (if (functionp
5192 (nth 1 org-agenda-deadline-leaders))
5193 (funcall
5194 (nth 1 org-agenda-deadline-leaders)
5195 diff date)
5196 (format (nth 1 org-agenda-deadline-leaders)
5197 diff)))
5198 head category tags
5199 (if (not (= diff 0)) nil timestr)))))
20908596
CD
5200 (when txt
5201 (setq face (org-agenda-deadline-face dfrac wdays))
5202 (org-add-props txt props
5203 'org-marker (org-agenda-new-marker pos)
5204 'org-hd-marker (org-agenda-new-marker pos1)
5205 'priority (+ (- diff)
5206 (org-get-priority txt))
5207 'org-category category
621f83e4 5208 'todo-state todo-state
20908596
CD
5209 'type (if upcomingp "upcoming-deadline" "deadline")
5210 'date (if upcomingp date d2)
c8d0cf5c
CD
5211 'face (if donep 'org-agenda-done face)
5212 'undone-face face 'done-face 'org-agenda-done)
20908596
CD
5213 (push txt ee))))))
5214 (nreverse ee)))
5215
5216(defun org-agenda-deadline-face (fraction &optional wdays)
5217 "Return the face to displaying a deadline item.
5218FRACTION is what fraction of the head-warning time has passed."
5219 (if (equal wdays 0) (setq fraction 1.))
5220 (let ((faces org-agenda-deadline-faces) f)
5221 (catch 'exit
5222 (while (setq f (pop faces))
5223 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5224
54a0dee5 5225(defun org-agenda-get-scheduled (&optional deadline-results)
20908596
CD
5226 "Return the scheduled information for agenda display."
5227 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5228 'org-todo-regexp org-todo-regexp
b349f79f 5229 'org-complex-heading-regexp org-complex-heading-regexp
c8d0cf5c 5230 'done-face 'org-agenda-done
20908596 5231 'mouse-face 'highlight
20908596
CD
5232 'help-echo
5233 (format "mouse-2 or RET jump to org file %s"
5234 (abbreviate-file-name buffer-file-name))))
5235 (regexp org-scheduled-time-regexp)
621f83e4 5236 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596 5237 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
54a0dee5
CD
5238 mm
5239 (deadline-position-alist
5240 (mapcar (lambda (a) (and (setq mm (get-text-property
8bfe682a
CD
5241 0 'org-hd-marker a))
5242 (cons (marker-position mm) a)))
54a0dee5 5243 deadline-results))
621f83e4 5244 d2 diff pos pos1 category tags donep
3ab2c837 5245 ee txt head pastschedp todo-state face timestr s habitp show-all)
20908596
CD
5246 (goto-char (point-min))
5247 (while (re-search-forward regexp nil t)
5248 (catch :skip
5249 (org-agenda-skip)
5250 (setq s (match-string 1)
c8d0cf5c 5251 txt nil
20908596 5252 pos (1- (match-beginning 1))
3ab2c837
BG
5253 todo-state (save-match-data (org-get-todo-state))
5254 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5255 (member todo-state
5256 org-agenda-repeating-timestamp-show-all))
20908596 5257 d2 (org-time-string-to-absolute
3ab2c837 5258 (match-string 1) d1 'past show-all)
20908596
CD
5259 diff (- d2 d1))
5260 (setq pastschedp (and todayp (< diff 0)))
5261 ;; When to show a scheduled item in the calendar:
5262 ;; If it is on or past the date.
8bfe682a
CD
5263 (when (or (and (< diff 0)
5264 (< (abs diff) org-scheduled-past-days)
5265 (and todayp (not org-agenda-only-exact-dates)))
5266 (= diff 0))
5267 (save-excursion
8bfe682a 5268 (setq donep (member todo-state org-done-keywords))
8bfe682a 5269 (if (and donep
3ab2c837
BG
5270 (or org-agenda-skip-scheduled-if-done
5271 (not (= diff 0))
5272 (and (functionp 'org-is-habit-p)
5273 (org-is-habit-p))))
8bfe682a 5274 (setq txt nil)
3ab2c837
BG
5275 (setq habitp (and (functionp 'org-is-habit-p)
5276 (org-is-habit-p)))
8bfe682a
CD
5277 (setq category (org-get-category))
5278 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5279 (setq txt org-agenda-no-heading-message)
5280 (goto-char (match-end 0))
5281 (setq pos1 (match-beginning 0))
5282 (if habitp
5283 (if (or (not org-habit-show-habits)
5284 (and (not todayp)
5285 org-habit-show-habits-only-for-today))
5286 (throw :skip nil))
54a0dee5
CD
5287 (if (and
5288 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5289 (and org-agenda-skip-scheduled-if-deadline-is-shown
5290 pastschedp))
5291 (setq mm (assoc pos1 deadline-position-alist)))
8bfe682a
CD
5292 (throw :skip nil)))
5293 (setq tags (org-get-tags-at))
5294 (setq head (buffer-substring-no-properties
5295 (point)
5296 (progn (skip-chars-forward "^\r\n") (point))))
5297 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5298 (setq timestr
5299 (concat (substring s (match-beginning 1)) " "))
5300 (setq timestr 'time))
5301 (setq txt (org-format-agenda-item
5302 (if (= diff 0)
5303 (car org-agenda-scheduled-leaders)
5304 (format (nth 1 org-agenda-scheduled-leaders)
5305 (- 1 diff)))
5306 head category tags
5307 (if (not (= diff 0)) nil timestr)
3ab2c837 5308 nil habitp))))
8bfe682a
CD
5309 (when txt
5310 (setq face
5311 (cond
5312 ((and (not habitp) pastschedp)
5313 'org-scheduled-previously)
5314 (todayp 'org-scheduled-today)
5315 (t 'org-scheduled))
5316 habitp (and habitp (org-habit-parse-todo)))
5317 (org-add-props txt props
5318 'undone-face face
5319 'face (if donep 'org-agenda-done face)
5320 'org-marker (org-agenda-new-marker pos)
5321 'org-hd-marker (org-agenda-new-marker pos1)
5322 'type (if pastschedp "past-scheduled" "scheduled")
5323 'date (if pastschedp d2 date)
5324 'priority (if habitp
5325 (org-habit-get-priority habitp)
5326 (+ 94 (- 5 diff) (org-get-priority txt)))
5327 'org-category category
5328 'org-habit-p habitp
5329 'todo-state todo-state)
5330 (push txt ee))))))
20908596
CD
5331 (nreverse ee)))
5332
5333(defun org-agenda-get-blocks ()
5334 "Return the date-range information for agenda display."
5335 (let* ((props (list 'face nil
5336 'org-not-done-regexp org-not-done-regexp
5337 'org-todo-regexp org-todo-regexp
b349f79f 5338 'org-complex-heading-regexp org-complex-heading-regexp
20908596 5339 'mouse-face 'highlight
20908596
CD
5340 'help-echo
5341 (format "mouse-2 or RET jump to org file %s"
5342 (abbreviate-file-name buffer-file-name))))
5343 (regexp org-tr-regexp)
5344 (d0 (calendar-absolute-from-gregorian date))
3ab2c837 5345 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
c8d0cf5c 5346 head donep)
20908596
CD
5347 (goto-char (point-min))
5348 (while (re-search-forward regexp nil t)
5349 (catch :skip
5350 (org-agenda-skip)
5351 (setq pos (point))
3ab2c837
BG
5352 (let ((start-time (match-string 1))
5353 (end-time (match-string 2)))
5354 (setq s1 (match-string 1)
5355 s2 (match-string 2)
5356 d1 (time-to-days (org-time-string-to-time s1))
5357 d2 (time-to-days (org-time-string-to-time s2)))
5358 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5359 ;; Only allow days between the limits, because the normal
5360 ;; date stamps will catch the limits.
5361 (save-excursion
5362 (setq todo-state (org-get-todo-state))
5363 (setq donep (member todo-state org-done-keywords))
5364 (if (and donep org-agenda-skip-timestamp-if-done)
5365 (throw :skip t))
5366 (setq marker (org-agenda-new-marker (point)))
5367 (setq category (org-get-category))
5368 (if (not (re-search-backward org-outline-regexp-bol nil t))
5369 (setq txt org-agenda-no-heading-message)
5370 (goto-char (match-beginning 0))
5371 (setq hdmarker (org-agenda-new-marker (point)))
5372 (setq tags (org-get-tags-at))
5373 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5374 (setq head (match-string 1))
5375 (let ((remove-re
5376 (if org-agenda-remove-timeranges-from-blocks
5377 (concat
5378 "<" (regexp-quote s1) ".*?>"
5379 "--"
5380 "<" (regexp-quote s2) ".*?>")
5381 nil)))
5382 (setq txt (org-format-agenda-item
5383 (format
5384 (nth (if (= d1 d2) 0 1)
5385 org-agenda-timerange-leaders)
5386 (1+ (- d0 d1)) (1+ (- d2 d1)))
5387 head category tags
5388 (cond ((= d1 d0)
5389 (concat "<" start-time ">"))
5390 ((= d2 d0)
5391 (concat "<" end-time ">"))
5392 (t nil))
5393 remove-re))))
5394 (org-add-props txt props
5395 'org-marker marker 'org-hd-marker hdmarker
5396 'type "block" 'date date
5397 'todo-state todo-state
5398 'priority (org-get-priority txt) 'org-category category)
5399 (push txt ee))))
20908596
CD
5400 (goto-char pos)))
5401 ;; Sort the entries by expiration date.
5402 (nreverse ee)))
5403
5404;;; Agenda presentation and sorting
5405
5406(defvar org-prefix-has-time nil
5407 "A flag, set by `org-compile-prefix-format'.
5408The flag is set if the currently compiled format contains a `%t'.")
5409(defvar org-prefix-has-tag nil
5410 "A flag, set by `org-compile-prefix-format'.
5411The flag is set if the currently compiled format contains a `%T'.")
5412(defvar org-prefix-has-effort nil
5413 "A flag, set by `org-compile-prefix-format'.
5414The flag is set if the currently compiled format contains a `%e'.")
8d642074 5415(defvar org-prefix-category-length nil
86fbb8ca 5416 "Used by `org-compile-prefix-format' to remember the category field width.")
8bfe682a 5417(defvar org-prefix-category-max-length nil
86fbb8ca 5418 "Used by `org-compile-prefix-format' to remember the category field width.")
20908596 5419
acedf35c
CD
5420(defun org-agenda-get-category-icon (category)
5421 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5422 (dolist (entry org-agenda-category-icon-alist)
5423 (when (org-string-match-p (car entry) category)
5424 (if (listp (cadr entry))
5425 (return (cadr entry))
5426 (return (apply 'create-image (cdr entry)))))))
5427
20908596 5428(defun org-format-agenda-item (extra txt &optional category tags dotime
3ab2c837 5429 remove-re habitp)
20908596
CD
5430 "Format TXT to be inserted into the agenda buffer.
5431In particular, it adds the prefix and corresponding text properties. EXTRA
5432must be a string and replaces the `%s' specifier in the prefix format.
5433CATEGORY (string, symbol or nil) may be used to overrule the default
5434category taken from local variable or file name. It will replace the `%c'
5435specifier in the format. DOTIME, when non-nil, indicates that a
5436time-of-day should be extracted from TXT for sorting of this entry, and for
5437the `%t' specifier in the format. When DOTIME is a string, this string is
3ab2c837 5438searched for a time before TXT is. TAGS can be the tags of the headline.
20908596
CD
5439Any match of REMOVE-RE will be removed from TXT."
5440 (save-match-data
5441 ;; Diary entries sometimes have extra whitespace at the beginning
5442 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5dec9555
CD
5443
5444 ;; Fix the tags part in txt
5445 (setq txt (org-agenda-fix-displayed-tags
5446 txt tags
5447 org-agenda-show-inherited-tags
5448 org-agenda-hide-tags-regexp))
20908596 5449 (let* ((category (or category
acedf35c
CD
5450 (if (stringp org-category)
5451 org-category
5452 (and org-category (symbol-name org-category)))
20908596
CD
5453 (if buffer-file-name
5454 (file-name-sans-extension
5455 (file-name-nondirectory buffer-file-name))
5456 "")))
acedf35c
CD
5457 (category-icon (org-agenda-get-category-icon category))
5458 (category-icon (if category-icon
5459 (propertize " " 'display category-icon)
5460 ""))
20908596
CD
5461 ;; time, tag, effort are needed for the eval of the prefix format
5462 (tag (if tags (nth (1- (length tags)) tags) ""))
5463 time effort neffort
c8d0cf5c
CD
5464 (ts (if dotime (concat
5465 (if (stringp dotime) dotime "")
5466 (and org-agenda-search-headline-for-time txt))))
20908596 5467 (time-of-day (and dotime (org-get-time-of-day ts)))
3ab2c837 5468 stamp plain s0 s1 s2 rtn srp l
8bfe682a 5469 duration thecategory)
20908596
CD
5470 (and (org-mode-p) buffer-file-name
5471 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5472 (when (and dotime time-of-day)
5473 ;; Extract starting and ending time and move them to prefix
5474 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5475 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5476 (setq s0 (match-string 0 ts)
5477 srp (and stamp (match-end 3))
5478 s1 (match-string (if plain 1 2) ts)
5479 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5480
5481 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5482 ;; them, we might want to remove them there to avoid duplication.
5483 ;; The user can turn this off with a variable.
5484 (if (and org-prefix-has-time
5485 org-agenda-remove-times-when-in-prefix (or stamp plain)
5486 (string-match (concat (regexp-quote s0) " *") txt)
5487 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5488 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5489 (= (match-beginning 0) 0)
5490 t))
5491 (setq txt (replace-match "" nil nil txt))))
5492 ;; Normalize the time(s) to 24 hour
5493 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5494 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
3ab2c837
BG
5495
5496 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5497 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5498 (setq s2
5499 (org-minutes-to-hh:mm-string
5500 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5501
20908596 5502 ;; Compute the duration
3ab2c837
BG
5503 (when s2
5504 (setq duration (- (org-hh:mm-string-to-minutes s2)
5505 (org-hh:mm-string-to-minutes s1)))))
20908596 5506
afe98dfa 5507 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
20908596
CD
5508 txt)
5509 ;; Tags are in the string
5510 (if (or (eq org-agenda-remove-tags t)
5511 (and org-agenda-remove-tags
5512 org-prefix-has-tag))
5513 (setq txt (replace-match "" t t txt))
5514 (setq txt (replace-match
5515 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5516 (match-string 2 txt))
5517 t t txt))))
5518 (when (org-mode-p)
5519 (setq effort
5520 (condition-case nil
5521 (org-get-effort
5522 (or (get-text-property 0 'org-hd-marker txt)
5523 (get-text-property 0 'org-marker txt)))
5524 (error nil)))
5525 (when effort
3ab2c837
BG
5526 (setq neffort (org-duration-string-to-minutes effort)
5527 effort (setq effort (concat "[" effort "]")))))
5528 ;; prevent erroring out with %e format when there is no effort
5529 (or effort (setq effort ""))
20908596
CD
5530
5531 (when remove-re
5532 (while (string-match remove-re txt)
5533 (setq txt (replace-match "" t t txt))))
5534
3ab2c837
BG
5535 ;; Set org-heading property on `txt' to mark the start of the
5536 ;; heading.
5537 (add-text-properties 0 (length txt) '(org-heading t) txt)
5538
5539 ;; Prepare the variables needed in the eval of the compiled format
5540 (setq time (cond (s2 (concat
5541 (org-agenda-time-of-day-to-ampm-maybe s1)
5542 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5543 (if org-agenda-timegrid-use-ampm " ")))
5544 (s1 (concat
5545 (org-agenda-time-of-day-to-ampm-maybe s1)
5546 (if org-agenda-timegrid-use-ampm
5547 "........ "
5548 "......")))
5549 (t ""))
5550 extra (or (and (not habitp) extra) "")
5551 category (if (symbolp category) (symbol-name category) category)
5552 thecategory (copy-sequence category))
5553 (if (string-match org-bracket-link-regexp category)
5554 (progn
5555 (setq l (if (match-end 3)
5556 (- (match-end 3) (match-beginning 3))
5557 (- (match-end 1) (match-beginning 1))))
5558 (when (< l (or org-prefix-category-length 0))
5559 (setq category (copy-sequence category))
5560 (org-add-props category nil
5561 'extra-space (make-string
5562 (- org-prefix-category-length l 1) ?\ ))))
5563 (if (and org-prefix-category-max-length
5564 (>= (length category) org-prefix-category-max-length))
5565 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5566 ;; Evaluate the compiled format
5567 (setq rtn (concat (eval org-prefix-format-compiled) txt))
20908596
CD
5568
5569 ;; And finally add the text properties
c8d0cf5c 5570 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
20908596 5571 (org-add-props rtn nil
8bfe682a 5572 'org-category (if thecategory (downcase thecategory) category)
ff4be292 5573 'tags (mapcar 'org-downcase-keep-props tags)
20908596
CD
5574 'org-highest-priority org-highest-priority
5575 'org-lowest-priority org-lowest-priority
20908596
CD
5576 'time-of-day time-of-day
5577 'duration duration
5578 'effort effort
5579 'effort-minutes neffort
5580 'txt txt
5581 'time time
5582 'extra extra
3ab2c837 5583 'format org-prefix-format-compiled
20908596
CD
5584 'dotime dotime))))
5585
5dec9555
CD
5586(defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5587 "Remove tags string from TXT, and add a modified list of tags.
5588The modified list may contain inherited tags, and tags matched by
5589`org-agenda-hide-tags-regexp' will be removed."
5590 (when (or add-inherited hide-re)
afe98dfa 5591 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5dec9555 5592 (setq txt (substring txt 0 (match-beginning 0))))
ed21c5c8
CD
5593 (setq tags
5594 (delq nil
5595 (mapcar (lambda (tg)
5596 (if (or (and hide-re (string-match hide-re tg))
5597 (and (not add-inherited)
5598 (get-text-property 0 'inherited tg)))
5599 nil
5600 tg))
5601 tags)))
5dec9555 5602 (when tags
5dec9555
CD
5603 (let ((have-i (get-text-property 0 'inherited (car tags)))
5604 i)
5605 (setq txt (concat txt " :"
5606 (mapconcat
5607 (lambda (x)
5608 (setq i (get-text-property 0 'inherited x))
5609 (if (and have-i (not i))
5610 (progn
5611 (setq have-i nil)
5612 (concat ":" x))
5613 x))
5614 tags ":")
5615 (if have-i "::" ":"))))))
5616 txt)
ff4be292
CD
5617
5618(defun org-downcase-keep-props (s)
5619 (let ((props (text-properties-at 0 s)))
5620 (setq s (downcase s))
5621 (add-text-properties 0 (length s) props s)
5622 s))
5623
20908596
CD
5624(defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5625(defvar org-agenda-sorting-strategy-selected nil)
5626
5627(defun org-agenda-add-time-grid-maybe (list ndays todayp)
5628 (catch 'exit
5629 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5630 ((and todayp (member 'today (car org-agenda-time-grid))))
5631 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5632 ((member 'weekly (car org-agenda-time-grid)))
5633 (t (throw 'exit list)))
5634 (let* ((have (delq nil (mapcar
5635 (lambda (x) (get-text-property 1 'time-of-day x))
5636 list)))
5637 (string (nth 1 org-agenda-time-grid))
5638 (gridtimes (nth 2 org-agenda-time-grid))
5639 (req (car org-agenda-time-grid))
5640 (remove (member 'remove-match req))
5641 new time)
5642 (if (and (member 'require-timed req) (not have))
5643 ;; don't show empty grid
5644 (throw 'exit list))
5645 (while (setq time (pop gridtimes))
5646 (unless (and remove (member time have))
afe98dfa 5647 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
20908596
CD
5648 (push (org-format-agenda-item
5649 nil string "" nil
5650 (concat (substring time 0 -2) ":" (substring time -2)))
5651 new)
5652 (put-text-property
afe98dfa 5653 2 (length (car new)) 'face 'org-time-grid (car new))))
3ab2c837
BG
5654 (when (and todayp org-agenda-show-current-time-in-grid)
5655 (push (org-format-agenda-item
5656 nil
5657 org-agenda-current-time-string
5658 "" nil
5659 (format-time-string "%H:%M "))
5660 new)
5661 (put-text-property
5662 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5663
20908596
CD
5664 (if (member 'time-up org-agenda-sorting-strategy-selected)
5665 (append new list)
5666 (append list new)))))
5667
5668(defun org-compile-prefix-format (key)
5669 "Compile the prefix format into a Lisp form that can be evaluated.
5670The resulting form is returned and stored in the variable
5671`org-prefix-format-compiled'."
5672 (setq org-prefix-has-time nil org-prefix-has-tag nil
8d642074 5673 org-prefix-category-length nil org-prefix-has-effort nil)
20908596
CD
5674 (let ((s (cond
5675 ((stringp org-agenda-prefix-format)
5676 org-agenda-prefix-format)
5677 ((assq key org-agenda-prefix-format)
5678 (cdr (assq key org-agenda-prefix-format)))
5679 (t " %-12:c%?-12t% s")))
5680 (start 0)
5681 varform vars var e c f opt)
3ab2c837 5682 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
20908596 5683 s start)
3ab2c837
BG
5684 (setq var (or (cdr (assoc (match-string 4 s)
5685 '(("c" . category) ("t" . time) ("s" . extra)
5686 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5687 'eval)
20908596
CD
5688 c (or (match-string 3 s) "")
5689 opt (match-beginning 1)
5690 start (1+ (match-beginning 0)))
5691 (if (equal var 'time) (setq org-prefix-has-time t))
5692 (if (equal var 'tag) (setq org-prefix-has-tag t))
5693 (if (equal var 'effort) (setq org-prefix-has-effort t))
5694 (setq f (concat "%" (match-string 2 s) "s"))
8bfe682a
CD
5695 (when (equal var 'category)
5696 (setq org-prefix-category-length
5697 (floor (abs (string-to-number (match-string 2 s)))))
5698 (setq org-prefix-category-max-length
5699 (let ((x (match-string 2 s)))
5700 (save-match-data
5701 (if (string-match "\\.[0-9]+" x)
5702 (string-to-number (substring (match-string 0 x) 1)))))))
3ab2c837
BG
5703 (if (eq var 'eval)
5704 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5705 (if opt
5706 (setq varform
5707 `(if (equal "" ,var)
5708 ""
5709 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5710 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
20908596
CD
5711 (setq s (replace-match "%s" t nil s))
5712 (push varform vars))
5713 (setq vars (nreverse vars))
5714 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5715
5716(defun org-set-sorting-strategy (key)
5717 (if (symbolp (car org-agenda-sorting-strategy))
5718 ;; the old format
5719 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5720 (setq org-agenda-sorting-strategy-selected
5721 (or (cdr (assq key org-agenda-sorting-strategy))
5722 (cdr (assq 'agenda org-agenda-sorting-strategy))
5723 '(time-up category-keep priority-down)))))
5724
5725(defun org-get-time-of-day (s &optional string mod24)
5726 "Check string S for a time of day.
5727If found, return it as a military time number between 0 and 2400.
5728If not found, return nil.
5729The optional STRING argument forces conversion into a 5 character wide string
5730HH:MM."
5731 (save-match-data
5732 (when
5733 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5734 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5735 (let* ((h (string-to-number (match-string 1 s)))
5736 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5737 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5738 (am-p (equal ampm "am"))
5739 (h1 (cond ((not ampm) h)
5740 ((= h 12) (if am-p 0 12))
5741 (t (+ h (if am-p 0 12)))))
5742 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5743 (mod h1 24) h1))
5744 (t0 (+ (* 100 h2) m))
5745 (t1 (concat (if (>= h1 24) "+" " ")
ed21c5c8
CD
5746 (if (and org-agenda-time-leading-zero
5747 (< t0 1000)) "0" "")
20908596
CD
5748 (if (< t0 100) "0" "")
5749 (if (< t0 10) "0" "")
5750 (int-to-string t0))))
5751 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5752
afe98dfa
CD
5753(defvar org-agenda-before-sorting-filter-function nil
5754 "Function to be applied to agenda items prior to sorting.
5755Prior to sorting also means just before they are inserted into the agenda.
5756
5757To aid sorting, you may revisit the original entries and add more text
5758properties which will later be used by the sorting functions.
5759
5760The function should take a string argument, an agenda line.
5761It has access to the text properties in that line, which contain among
5762other things, the property `org-hd-marker' that points to the entry
5763where the line comes from. Note that not all lines going into the agenda
5764have this property, only most.
5765
5766The function should return the modified string. It is probably best
5767to ONLY change text properties.
5768
5769You can also use this function as a filter, by returning nil for lines
5770you don't want to have in the agenda at all. For this application, you
5771could bind the variable in the options section of a custom command.")
5772
20908596
CD
5773(defun org-finalize-agenda-entries (list &optional nosort)
5774 "Sort and concatenate the agenda items."
5775 (setq list (mapcar 'org-agenda-highlight-todo list))
5776 (if nosort
5777 list
afe98dfa
CD
5778 (when org-agenda-before-sorting-filter-function
5779 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
20908596
CD
5780 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5781
5782(defun org-agenda-highlight-todo (x)
621f83e4 5783 (let ((org-done-keywords org-done-keywords-for-agenda)
ed21c5c8 5784 (case-fold-search nil)
3ab2c837 5785 re)
20908596
CD
5786 (if (eq x 'line)
5787 (save-excursion
5788 (beginning-of-line 1)
8d642074 5789 (setq re (org-get-at-bol 'org-todo-regexp))
3ab2c837 5790 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
621f83e4 5791 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
c8d0cf5c 5792 (add-text-properties (match-beginning 0) (match-end 1)
621f83e4 5793 (list 'face (org-get-todo-face 1)))
20908596
CD
5794 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5795 (delete-region (match-beginning 1) (1- (match-end 0)))
5796 (goto-char (match-beginning 1))
5797 (insert (format org-agenda-todo-keyword-format s)))))
3ab2c837
BG
5798 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5799 (setq re (get-text-property 0 'org-todo-regexp x))
5800 (when (and re
5801 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5802 x (or pl 0)) pl))
5803 (add-text-properties
5804 (or (match-end 1) (match-end 0)) (match-end 0)
5805 (list 'face (org-get-todo-face (match-string 2 x)))
20908596 5806 x)
3ab2c837
BG
5807 (when (match-end 1)
5808 (setq x (concat (substring x 0 (match-end 1))
5809 (format org-agenda-todo-keyword-format
5810 (match-string 2 x))
86fbb8ca 5811 (org-add-props " " (text-properties-at 0 x))
3ab2c837 5812 (substring x (match-end 3)))))))
20908596
CD
5813 x)))
5814
5815(defsubst org-cmp-priority (a b)
5816 "Compare the priorities of string A and B."
5817 (let ((pa (or (get-text-property 1 'priority a) 0))
5818 (pb (or (get-text-property 1 'priority b) 0)))
5819 (cond ((> pa pb) +1)
5820 ((< pa pb) -1)
5821 (t nil))))
5822
5823(defsubst org-cmp-effort (a b)
5824 "Compare the priorities of string A and B."
5825 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5826 (ea (or (get-text-property 1 'effort-minutes a) def))
5827 (eb (or (get-text-property 1 'effort-minutes b) def)))
5828 (cond ((> ea eb) +1)
5829 ((< ea eb) -1)
5830 (t nil))))
5831
5832(defsubst org-cmp-category (a b)
5833 "Compare the string values of categories of strings A and B."
5834 (let ((ca (or (get-text-property 1 'org-category a) ""))
5835 (cb (or (get-text-property 1 'org-category b) "")))
5836 (cond ((string-lessp ca cb) -1)
5837 ((string-lessp cb ca) +1)
5838 (t nil))))
5839
621f83e4
CD
5840(defsubst org-cmp-todo-state (a b)
5841 "Compare the todo states of strings A and B."
c8d0cf5c
CD
5842 (let* ((ma (or (get-text-property 1 'org-marker a)
5843 (get-text-property 1 'org-hd-marker a)))
5844 (mb (or (get-text-property 1 'org-marker b)
5845 (get-text-property 1 'org-hd-marker b)))
5846 (fa (and ma (marker-buffer ma)))
5847 (fb (and mb (marker-buffer mb)))
5848 (todo-kwds
5849 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5850 (and fb (with-current-buffer fb org-todo-keywords-1))))
5851 (ta (or (get-text-property 1 'todo-state a) ""))
621f83e4 5852 (tb (or (get-text-property 1 'todo-state b) ""))
c8d0cf5c
CD
5853 (la (- (length (member ta todo-kwds))))
5854 (lb (- (length (member tb todo-kwds))))
ff4be292 5855 (donepa (member ta org-done-keywords-for-agenda))
621f83e4
CD
5856 (donepb (member tb org-done-keywords-for-agenda)))
5857 (cond ((and donepa (not donepb)) -1)
5858 ((and (not donepa) donepb) +1)
5859 ((< la lb) -1)
5860 ((< lb la) +1)
5861 (t nil))))
5862
86fbb8ca
CD
5863(defsubst org-cmp-alpha (a b)
5864 "Compare the headlines, alphabetically."
3ab2c837
BG
5865 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5866 (plb (text-property-any 0 (length b) 'org-heading t b))
86fbb8ca
CD
5867 (ta (and pla (substring a pla)))
5868 (tb (and plb (substring b plb))))
5869 (when pla
5870 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5871 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5872 (setq ta (substring ta (match-end 0))))
5873 (setq ta (downcase ta)))
5874 (when plb
5875 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5876 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5877 (setq tb (substring tb (match-end 0))))
5878 (setq tb (downcase tb)))
5879 (cond ((not ta) +1)
5880 ((not tb) -1)
5881 ((string-lessp ta tb) -1)
5882 ((string-lessp tb ta) +1)
5883 (t nil))))
5884
20908596 5885(defsubst org-cmp-tag (a b)
71d35b24 5886 "Compare the string values of the first tags of A and B."
20908596
CD
5887 (let ((ta (car (last (get-text-property 1 'tags a))))
5888 (tb (car (last (get-text-property 1 'tags b)))))
5889 (cond ((not ta) +1)
5890 ((not tb) -1)
5891 ((string-lessp ta tb) -1)
5892 ((string-lessp tb ta) +1)
5893 (t nil))))
5894
5895(defsubst org-cmp-time (a b)
5896 "Compare the time-of-day values of strings A and B."
5897 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5898 (ta (or (get-text-property 1 'time-of-day a) def))
5899 (tb (or (get-text-property 1 'time-of-day b) def)))
5900 (cond ((< ta tb) -1)
5901 ((< tb ta) +1)
5902 (t nil))))
5903
8bfe682a
CD
5904(defsubst org-cmp-habit-p (a b)
5905 "Compare the todo states of strings A and B."
5906 (let ((ha (get-text-property 1 'org-habit-p a))
5907 (hb (get-text-property 1 'org-habit-p b)))
5908 (cond ((and ha (not hb)) -1)
5909 ((and (not ha) hb) +1)
5910 (t nil))))
5911
86fbb8ca
CD
5912(defsubst org-em (x y list) (or (memq x list) (memq y list)))
5913
20908596
CD
5914(defun org-entries-lessp (a b)
5915 "Predicate for sorting agenda entries."
5916 ;; The following variables will be used when the form is evaluated.
5917 ;; So even though the compiler complains, keep them.
86fbb8ca
CD
5918 (let* ((ss org-agenda-sorting-strategy-selected)
5919 (time-up (and (org-em 'time-up 'time-down ss)
5920 (org-cmp-time a b)))
5921 (time-down (if time-up (- time-up) nil))
5922 (priority-up (and (org-em 'priority-up 'priority-down ss)
5923 (org-cmp-priority a b)))
5924 (priority-down (if priority-up (- priority-up) nil))
5925 (effort-up (and (org-em 'effort-up 'effort-down ss)
5926 (org-cmp-effort a b)))
5927 (effort-down (if effort-up (- effort-up) nil))
5928 (category-up (and (or (org-em 'category-up 'category-down ss)
5929 (memq 'category-keep ss))
5930 (org-cmp-category a b)))
5931 (category-down (if category-up (- category-up) nil))
5932 (category-keep (if category-up +1 nil))
5933 (tag-up (and (org-em 'tag-up 'tag-down ss)
5934 (org-cmp-tag a b)))
5935 (tag-down (if tag-up (- tag-up) nil))
5936 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5937 (org-cmp-todo-state a b)))
c8d0cf5c 5938 (todo-state-down (if todo-state-up (- todo-state-up) nil))
86fbb8ca
CD
5939 (habit-up (and (org-em 'habit-up 'habit-down ss)
5940 (org-cmp-habit-p a b)))
5941 (habit-down (if habit-up (- habit-up) nil))
5942 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5943 (org-cmp-alpha a b)))
5944 (alpha-down (if alpha-up (- alpha-up) nil))
afe98dfa 5945 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
c8d0cf5c 5946 user-defined-up user-defined-down)
afe98dfa 5947 (if (and need-user-cmp org-agenda-cmp-user-defined
c8d0cf5c
CD
5948 (functionp org-agenda-cmp-user-defined))
5949 (setq user-defined-up
5950 (funcall org-agenda-cmp-user-defined a b)
5951 user-defined-down (if user-defined-up (- user-defined-up) nil)))
20908596
CD
5952 (cdr (assoc
5953 (eval (cons 'or org-agenda-sorting-strategy-selected))
5954 '((-1 . t) (1 . nil) (nil . nil))))))
5955
5956;;; Agenda restriction lock
5957
86fbb8ca 5958(defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
8bfe682a 5959 "Overlay to mark the headline to which agenda commands are restricted.")
86fbb8ca
CD
5960(overlay-put org-agenda-restriction-lock-overlay
5961 'face 'org-agenda-restriction-lock)
5962(overlay-put org-agenda-restriction-lock-overlay
5963 'help-echo "Agendas are currently limited to this subtree.")
20908596
CD
5964(org-detach-overlay org-agenda-restriction-lock-overlay)
5965
5966(defun org-agenda-set-restriction-lock (&optional type)
5967 "Set restriction lock for agenda, to current subtree or file.
5968Restriction will be the file if TYPE is `file', or if type is the
5969universal prefix '(4), or if the cursor is before the first headline
5970in the file. Otherwise, restriction will be to the current subtree."
5971 (interactive "P")
5972 (and (equal type '(4)) (setq type 'file))
5973 (setq type (cond
5974 (type type)
5975 ((org-at-heading-p) 'subtree)
5976 ((condition-case nil (org-back-to-heading t) (error nil))
5977 'subtree)
5978 (t 'file)))
5979 (if (eq type 'subtree)
5980 (progn
5981 (setq org-agenda-restrict t)
5982 (setq org-agenda-overriding-restriction 'subtree)
5983 (put 'org-agenda-files 'org-restrict
5984 (list (buffer-file-name (buffer-base-buffer))))
5985 (org-back-to-heading t)
86fbb8ca 5986 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
20908596
CD
5987 (move-marker org-agenda-restrict-begin (point))
5988 (move-marker org-agenda-restrict-end
5989 (save-excursion (org-end-of-subtree t)))
5990 (message "Locking agenda restriction to subtree"))
5991 (put 'org-agenda-files 'org-restrict
5992 (list (buffer-file-name (buffer-base-buffer))))
5993 (setq org-agenda-restrict nil)
5994 (setq org-agenda-overriding-restriction 'file)
5995 (move-marker org-agenda-restrict-begin nil)
5996 (move-marker org-agenda-restrict-end nil)
5997 (message "Locking agenda restriction to file"))
5998 (setq current-prefix-arg nil)
5999 (org-agenda-maybe-redo))
6000
6001(defun org-agenda-remove-restriction-lock (&optional noupdate)
6002 "Remove the agenda restriction lock."
6003 (interactive "P")
6004 (org-detach-overlay org-agenda-restriction-lock-overlay)
6005 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6006 (setq org-agenda-overriding-restriction nil)
6007 (setq org-agenda-restrict nil)
6008 (put 'org-agenda-files 'org-restrict nil)
6009 (move-marker org-agenda-restrict-begin nil)
6010 (move-marker org-agenda-restrict-end nil)
6011 (setq current-prefix-arg nil)
6012 (message "Agenda restriction lock removed")
6013 (or noupdate (org-agenda-maybe-redo)))
6014
6015(defun org-agenda-maybe-redo ()
6016 "If there is any window showing the agenda view, update it."
6017 (let ((w (get-buffer-window org-agenda-buffer-name t))
6018 (w0 (selected-window)))
6019 (when w
6020 (select-window w)
6021 (org-agenda-redo)
6022 (select-window w0)
6023 (if org-agenda-overriding-restriction
6024 (message "Agenda view shifted to new %s restriction"
6025 org-agenda-overriding-restriction)
6026 (message "Agenda restriction lock removed")))))
6027
6028;;; Agenda commands
6029
6030(defun org-agenda-check-type (error &rest types)
6031 "Check if agenda buffer is of allowed type.
6032If ERROR is non-nil, throw an error, otherwise just return nil."
6033 (if (memq org-agenda-type types)
6034 t
6035 (if error
6036 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6037 nil)))
6038
6039(defun org-agenda-quit ()
6040 "Exit agenda by removing the window or the buffer."
6041 (interactive)
6042 (if org-agenda-columns-active
6043 (org-columns-quit)
6044 (let ((buf (current-buffer)))
8d642074
CD
6045 (if (eq org-agenda-window-setup 'other-frame)
6046 (progn
6047 (kill-buffer buf)
6048 (org-agenda-reset-markers)
6049 (org-columns-remove-overlays)
6050 (setq org-agenda-archives-mode nil)
6051 (delete-frame))
6052 (and (not (eq org-agenda-window-setup 'current-window))
6053 (not (one-window-p))
6054 (delete-window))
6055 (kill-buffer buf)
6056 (org-agenda-reset-markers)
6057 (org-columns-remove-overlays)
6058 (setq org-agenda-archives-mode nil)))
20908596
CD
6059 ;; Maybe restore the pre-agenda window configuration.
6060 (and org-agenda-restore-windows-after-quit
6061 (not (eq org-agenda-window-setup 'other-frame))
6062 org-pre-agenda-window-conf
6063 (set-window-configuration org-pre-agenda-window-conf))))
6064
6065(defun org-agenda-exit ()
6066 "Exit agenda by removing the window or the buffer.
6067Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6068Org-mode buffers visited directly by the user will not be touched."
6069 (interactive)
6070 (org-release-buffers org-agenda-new-buffers)
6071 (setq org-agenda-new-buffers nil)
6072 (org-agenda-quit))
6073
6074(defun org-agenda-execute (arg)
86fbb8ca
CD
6075 "Execute another agenda command, keeping same window.
6076So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6077in the agenda."
20908596
CD
6078 (interactive "P")
6079 (let ((org-agenda-window-setup 'current-window))
6080 (org-agenda arg)))
6081
20908596
CD
6082(defun org-agenda-redo ()
6083 "Rebuild Agenda.
6084When this is the global TODO list, a prefix argument will be interpreted."
6085 (interactive)
6086 (let* ((org-agenda-keep-modes t)
71d35b24 6087 (filter org-agenda-filter)
c8d0cf5c 6088 (preset (get 'org-agenda-filter :preset-filter))
acedf35c 6089 (org-agenda-filter-while-redo (or filter preset))
20908596
CD
6090 (cols org-agenda-columns-active)
6091 (line (org-current-line))
6092 (window-line (- line (org-current-line (window-start))))
6093 (lprops (get 'org-agenda-redo-command 'org-lprops)))
c8d0cf5c 6094 (put 'org-agenda-filter :preset-filter nil)
20908596
CD
6095 (and cols (org-columns-quit))
6096 (message "Rebuilding agenda buffer...")
6097 (org-let lprops '(eval org-agenda-redo-command))
6098 (setq org-agenda-undo-list nil
6099 org-agenda-pending-undo-list nil)
6100 (message "Rebuilding agenda buffer...done")
c8d0cf5c
CD
6101 (put 'org-agenda-filter :preset-filter preset)
6102 (and (or filter preset) (org-agenda-filter-apply filter))
3ab2c837 6103 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
54a0dee5 6104 (org-goto-line line)
20908596
CD
6105 (recenter window-line)))
6106
71d35b24 6107
621f83e4 6108(defvar org-global-tags-completion-table nil)
71d35b24
CD
6109(defvar org-agenda-filter-form nil)
6110(defun org-agenda-filter-by-tag (strip &optional char narrow)
621f83e4
CD
6111 "Keep only those lines in the agenda buffer that have a specific tag.
6112The tag is selected with its fast selection letter, as configured.
71d35b24
CD
6113With prefix argument STRIP, remove all lines that do have the tag.
6114A lisp caller can specify CHAR. NARROW means that the new tag should be
6115used to narrow the search - the interactive user can also press `-' or `+'
6116to switch to narrowing."
621f83e4 6117 (interactive "P")
71d35b24 6118 (let* ((alist org-tag-alist-for-agenda)
8bfe682a
CD
6119 (tag-chars (mapconcat
6120 (lambda (x) (if (and (not (symbolp (car x)))
6121 (cdr x))
6122 (char-to-string (cdr x))
6123 ""))
6124 alist ""))
6125 (efforts (org-split-string
6126 (or (cdr (assoc (concat org-effort-property "_ALL")
6127 org-global-properties))
3ab2c837
BG
6128 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6129 "")))
8bfe682a
CD
6130 (effort-op org-agenda-filter-effort-default-operator)
6131 (effort-prompt "")
6132 (inhibit-read-only t)
6133 (current org-agenda-filter)
3ab2c837 6134 maybe-refresh a n tag)
71d35b24 6135 (unless char
ff4be292 6136 (message
8bfe682a
CD
6137 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6138 (if narrow "Narrow" "Filter") tag-chars
6139 (if org-agenda-auto-exclude-function "[RET], " ""))
71d35b24
CD
6140 (setq char (read-char)))
6141 (when (member char '(?+ ?-))
6142 ;; Narrowing down
6143 (cond ((equal char ?-) (setq strip t narrow t))
6144 ((equal char ?+) (setq strip nil narrow t)))
ff4be292 6145 (message
71d35b24
CD
6146 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6147 (setq char (read-char)))
c8d0cf5c 6148 (when (member char '(?< ?> ?= ??))
71d35b24
CD
6149 ;; An effort operator
6150 (setq effort-op (char-to-string char))
71d35b24 6151 (setq alist nil) ; to make sure it will be interpreted as effort.
c8d0cf5c
CD
6152 (unless (equal char ??)
6153 (loop for i from 0 to 9 do
6154 (setq effort-prompt
6155 (concat
6156 effort-prompt " ["
6157 (if (= i 9) "0" (int-to-string (1+ i)))
6158 "]" (nth i efforts))))
6159 (message "Effort%s: %s " effort-op effort-prompt)
6160 (setq char (read-char))
6161 (when (or (< char ?0) (> char ?9))
6162 (error "Need 1-9,0 to select effort" ))))
71d35b24
CD
6163 (when (equal char ?\t)
6164 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6165 (org-set-local 'org-global-tags-completion-table
6166 (org-global-tags-completion-table)))
6167 (let ((completion-ignore-case t))
54a0dee5 6168 (setq tag (org-icompleting-read
71d35b24
CD
6169 "Tag: " org-global-tags-completion-table))))
6170 (cond
8bfe682a
CD
6171 ((equal char ?\r)
6172 (org-agenda-filter-by-tag-show-all)
6173 (when org-agenda-auto-exclude-function
6174 (setq org-agenda-filter '())
ed21c5c8
CD
6175 (dolist (tag (org-agenda-get-represented-tags))
6176 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
8bfe682a
CD
6177 (if modifier
6178 (push modifier org-agenda-filter))))
6179 (if (not (null org-agenda-filter))
3ab2c837
BG
6180 (org-agenda-filter-apply org-agenda-filter)))
6181 (setq maybe-refresh t))
c8d0cf5c
CD
6182 ((equal char ?/)
6183 (org-agenda-filter-by-tag-show-all)
6184 (when (get 'org-agenda-filter :preset-filter)
3ab2c837
BG
6185 (org-agenda-filter-apply org-agenda-filter))
6186 (setq maybe-refresh t))
71d35b24
CD
6187 ((or (equal char ?\ )
6188 (setq a (rassoc char alist))
6189 (and (>= char ?0) (<= char ?9)
6190 (setq n (if (= char ?0) 9 (- char ?0 1))
6191 tag (concat effort-op (nth n efforts))
6192 a (cons tag nil)))
c8d0cf5c
CD
6193 (and (= char ??)
6194 (setq tag "?eff")
6195 a (cons tag nil))
71d35b24
CD
6196 (and tag (setq a (cons tag nil))))
6197 (org-agenda-filter-by-tag-show-all)
6198 (setq tag (car a))
6199 (setq org-agenda-filter
6200 (cons (concat (if strip "-" "+") tag)
6201 (if narrow current nil)))
3ab2c837
BG
6202 (org-agenda-filter-apply org-agenda-filter)
6203 (setq maybe-refresh t))
6204 (t (error "Invalid tag selection character %c" char)))
58e9b49a
BG
6205 (when (and maybe-refresh
6206 (eq org-agenda-clockreport-mode 'with-filter))
3ab2c837 6207 (org-agenda-redo))))
71d35b24 6208
ed21c5c8
CD
6209(defun org-agenda-get-represented-tags ()
6210 "Get a list of all tags currently represented in the agenda."
6211 (let (p tags)
6212 (save-excursion
6213 (goto-char (point-min))
6214 (while (setq p (next-single-property-change (point) 'tags))
6215 (goto-char p)
6216 (mapc (lambda (x) (add-to-list 'tags x))
6217 (get-text-property (point) 'tags))))
6218 tags))
6219
71d35b24
CD
6220(defun org-agenda-filter-by-tag-refine (strip &optional char)
6221 "Refine the current filter. See `org-agenda-filter-by-tag."
6222 (interactive "P")
6223 (org-agenda-filter-by-tag strip char 'refine))
6224
6225(defun org-agenda-filter-make-matcher ()
6226 "Create the form that tests a line for the agenda filter."
6227 (let (f f1)
c8d0cf5c
CD
6228 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6229 org-agenda-filter))
71d35b24 6230 (if (member x '("-" "+"))
8bfe682a 6231 (setq f1 (if (equal x "-") 'tags '(not tags)))
c8d0cf5c 6232 (if (string-match "[<=>?]" x)
71d35b24
CD
6233 (setq f1 (org-agenda-filter-effort-form x))
6234 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6235 (if (equal (string-to-char x) ?-)
6236 (setq f1 (list 'not f1))))
6237 (push f1 f))
6238 (cons 'and (nreverse f))))
6239
6240(defun org-agenda-filter-effort-form (e)
6241 "Return the form to compare the effort of the current line with what E says.
86fbb8ca 6242E looks like \"+<2:25\"."
71d35b24
CD
6243 (let (op)
6244 (setq e (substring e 1))
6245 (setq op (string-to-char e) e (substring e 1))
c8d0cf5c
CD
6246 (setq op (cond ((equal op ?<) '<=)
6247 ((equal op ?>) '>=)
6248 ((equal op ??) op)
6249 (t '=)))
71d35b24 6250 (list 'org-agenda-compare-effort (list 'quote op)
3ab2c837 6251 (org-duration-string-to-minutes e))))
71d35b24
CD
6252
6253(defun org-agenda-compare-effort (op value)
6254 "Compare the effort of the current line with VALUE, using OP.
6255If the line does not have an effort defined, return nil."
8d642074 6256 (let ((eff (org-get-at-bol 'effort-minutes)))
c8d0cf5c
CD
6257 (if (equal op ??)
6258 (not eff)
6259 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6260 value))))
71d35b24
CD
6261
6262(defun org-agenda-filter-apply (filter)
6263 "Set FILTER as the new agenda filter and apply it."
6264 (let (tags)
6265 (setq org-agenda-filter filter
6266 org-agenda-filter-form (org-agenda-filter-make-matcher))
6267 (org-agenda-set-mode-name)
6268 (save-excursion
6269 (goto-char (point-min))
6270 (while (not (eobp))
8d642074 6271 (if (org-get-at-bol 'org-marker)
71d35b24 6272 (progn
8d642074 6273 (setq tags (org-get-at-bol 'tags)) ; used in eval
71d35b24
CD
6274 (if (not (eval org-agenda-filter-form))
6275 (org-agenda-filter-by-tag-hide-line))
6276 (beginning-of-line 2))
afe98dfa
CD
6277 (beginning-of-line 2))))
6278 (if (get-char-property (point) 'invisible)
6279 (org-agenda-previous-line))))
621f83e4 6280
621f83e4
CD
6281(defun org-agenda-filter-by-tag-hide-line ()
6282 (let (ov)
86fbb8ca 6283 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
621f83e4 6284 (point-at-eol)))
86fbb8ca
CD
6285 (overlay-put ov 'invisible t)
6286 (overlay-put ov 'type 'tags-filter)
621f83e4
CD
6287 (push ov org-agenda-filter-overlays)))
6288
71d35b24
CD
6289(defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6290 (setq pos (or pos (point)))
6291 (save-excursion
86fbb8ca
CD
6292 (dolist (ov (overlays-at pos))
6293 (when (and (overlay-get ov 'invisible)
6294 (eq (overlay-get ov 'type) 'tags-filter))
71d35b24 6295 (goto-char pos)
86fbb8ca
CD
6296 (if (< (overlay-start ov) (point-at-eol))
6297 (move-overlay ov (point-at-eol)
6298 (overlay-end ov)))))))
71d35b24 6299
621f83e4 6300(defun org-agenda-filter-by-tag-show-all ()
86fbb8ca 6301 (mapc 'delete-overlay org-agenda-filter-overlays)
71d35b24
CD
6302 (setq org-agenda-filter-overlays nil)
6303 (setq org-agenda-filter nil)
6304 (setq org-agenda-filter-form nil)
6305 (org-agenda-set-mode-name))
621f83e4 6306
20908596
CD
6307(defun org-agenda-manipulate-query-add ()
6308 "Manipulate the query by adding a search term with positive selection.
ed21c5c8 6309Positive selection means the term must be matched for selection of an entry."
20908596
CD
6310 (interactive)
6311 (org-agenda-manipulate-query ?\[))
6312(defun org-agenda-manipulate-query-subtract ()
6313 "Manipulate the query by adding a search term with negative selection.
ed21c5c8 6314Negative selection means term must not be matched for selection of an entry."
20908596
CD
6315 (interactive)
6316 (org-agenda-manipulate-query ?\]))
6317(defun org-agenda-manipulate-query-add-re ()
6318 "Manipulate the query by adding a search regexp with positive selection.
ed21c5c8 6319Positive selection means the regexp must match for selection of an entry."
20908596
CD
6320 (interactive)
6321 (org-agenda-manipulate-query ?\{))
6322(defun org-agenda-manipulate-query-subtract-re ()
6323 "Manipulate the query by adding a search regexp with negative selection.
ed21c5c8 6324Negative selection means regexp must not match for selection of an entry."
20908596
CD
6325 (interactive)
6326 (org-agenda-manipulate-query ?\}))
6327(defun org-agenda-manipulate-query (char)
6328 (cond
6329 ((memq org-agenda-type '(timeline agenda))
54a0dee5
CD
6330 (let ((org-agenda-include-inactive-timestamps t))
6331 (org-agenda-redo))
6332 (message "Display now includes inactive timestamps as well"))
20908596
CD
6333 ((eq org-agenda-type 'search)
6334 (org-add-to-string
6335 'org-agenda-query-string
ed21c5c8
CD
6336 (if org-agenda-last-search-view-search-was-boolean
6337 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6338 (?\{ . " +{}") (?\} . " -{}"))))
6339 " "))
20908596
CD
6340 (setq org-agenda-redo-command
6341 (list 'org-search-view
6342 org-todo-only
6343 org-agenda-query-string
6344 (+ (length org-agenda-query-string)
6345 (if (member char '(?\{ ?\})) 0 1))))
6346 (set-register org-agenda-query-register org-agenda-query-string)
6347 (org-agenda-redo))
6348 (t (error "Cannot manipulate query for %s-type agenda buffers"
6349 org-agenda-type))))
6350
6351(defun org-add-to-string (var string)
6352 (set var (concat (symbol-value var) string)))
6353
6354(defun org-agenda-goto-date (date)
6355 "Jump to DATE in agenda."
afe98dfa
CD
6356 (interactive (list (let ((org-read-date-prefer-future
6357 (eval org-agenda-jump-prefer-future)))
6358 (org-read-date))))
20908596
CD
6359 (org-agenda-list nil date))
6360
6361(defun org-agenda-goto-today ()
6362 "Go to today."
6363 (interactive)
6364 (org-agenda-check-type t 'timeline 'agenda)
6365 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6366 (cond
6367 (tdpos (goto-char tdpos))
6368 ((eq org-agenda-type 'agenda)
acedf35c 6369 (let* ((sd (org-agenda-compute-starting-span
3ab2c837 6370 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
20908596 6371 (org-agenda-overriding-arguments org-agenda-last-arguments))
acedf35c 6372 (setf (nth 1 org-agenda-overriding-arguments) sd)
20908596
CD
6373 (org-agenda-redo)
6374 (org-agenda-find-same-or-today-or-agenda)))
6375 (t (error "Cannot find today")))))
6376
6377(defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6378 (goto-char
6379 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6380 (text-property-any (point-min) (point-max) 'org-today t)
6381 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6382 (point-min))))
6383
6384(defun org-agenda-later (arg)
6385 "Go forward in time by thee current span.
6386With prefix ARG, go forward that many times the current span."
6387 (interactive "p")
6388 (org-agenda-check-type t 'agenda)
acedf35c 6389 (let* ((span org-agenda-current-span)
20908596
CD
6390 (sd org-starting-day)
6391 (greg (calendar-gregorian-from-absolute sd))
8d642074 6392 (cnt (org-get-at-bol 'org-day-cnt))
acedf35c 6393 greg2)
20908596
CD
6394 (cond
6395 ((eq span 'day)
acedf35c 6396 (setq sd (+ arg sd)))
20908596 6397 ((eq span 'week)
acedf35c 6398 (setq sd (+ (* 7 arg) sd)))
20908596
CD
6399 ((eq span 'month)
6400 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6401 sd (calendar-absolute-from-gregorian greg2))
acedf35c 6402 (setcar greg2 (1+ (car greg2))))
20908596
CD
6403 ((eq span 'year)
6404 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6405 sd (calendar-absolute-from-gregorian greg2))
acedf35c
CD
6406 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6407 (t
6408 (setq sd (+ (* span arg) sd))))
20908596 6409 (let ((org-agenda-overriding-arguments
acedf35c 6410 (list (car org-agenda-last-arguments) sd span t)))
20908596
CD
6411 (org-agenda-redo)
6412 (org-agenda-find-same-or-today-or-agenda cnt))))
6413
6414(defun org-agenda-earlier (arg)
6415 "Go backward in time by the current span.
6416With prefix ARG, go backward that many times the current span."
6417 (interactive "p")
6418 (org-agenda-later (- arg)))
6419
c8d0cf5c
CD
6420(defun org-agenda-view-mode-dispatch ()
6421 "Call one of the view mode commands."
6422 (interactive)
3ab2c837
BG
6423 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6424 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6425 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6426 [E]ntryText")
c8d0cf5c
CD
6427 (let ((a (read-char-exclusive)))
6428 (case a
3ab2c837 6429 (?\ (call-interactively 'org-agenda-reset-view))
c8d0cf5c
CD
6430 (?d (call-interactively 'org-agenda-day-view))
6431 (?w (call-interactively 'org-agenda-week-view))
6432 (?m (call-interactively 'org-agenda-month-view))
6433 (?y (call-interactively 'org-agenda-year-view))
6434 (?l (call-interactively 'org-agenda-log-mode))
ed21c5c8 6435 (?L (org-agenda-log-mode '(4)))
3ab2c837 6436 (?c (org-agenda-log-mode 'clockcheck))
54a0dee5 6437 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
c8d0cf5c
CD
6438 (?a (call-interactively 'org-agenda-archives-mode))
6439 (?A (org-agenda-archives-mode 'files))
54a0dee5
CD
6440 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6441 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
c8d0cf5c
CD
6442 (?G (call-interactively 'org-agenda-toggle-time-grid))
6443 (?D (call-interactively 'org-agenda-toggle-diary))
ed21c5c8 6444 (?\! (call-interactively 'org-agenda-toggle-deadlines))
54a0dee5
CD
6445 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6446 (org-agenda-check-type t 'timeline 'agenda)
6447 (org-agenda-redo))
6448 (message "Display now includes inactive timestamps as well"))
c8d0cf5c
CD
6449 (?q (message "Abort"))
6450 (otherwise (error "Invalid key" )))))
6451
3ab2c837
BG
6452(defun org-agenda-reset-view ()
6453 "Switch to default view for agenda."
6454 (interactive)
6455 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
20908596
CD
6456(defun org-agenda-day-view (&optional day-of-year)
6457 "Switch to daily view for agenda.
6458With argument DAY-OF-YEAR, switch to that day of the year."
6459 (interactive "P")
20908596
CD
6460 (org-agenda-change-time-span 'day day-of-year))
6461(defun org-agenda-week-view (&optional iso-week)
6462 "Switch to daily view for agenda.
6463With argument ISO-WEEK, switch to the corresponding ISO week.
6464If ISO-WEEK has more then 2 digits, only the last two encode the
6465week. Any digits before this encode a year. So 200712 means
6466week 12 of year 2007. Years in the range 1938-2037 can also be
6467written as 2-digit years."
6468 (interactive "P")
20908596
CD
6469 (org-agenda-change-time-span 'week iso-week))
6470(defun org-agenda-month-view (&optional month)
b349f79f 6471 "Switch to monthly view for agenda.
20908596
CD
6472With argument MONTH, switch to that month."
6473 (interactive "P")
6474 (org-agenda-change-time-span 'month month))
6475(defun org-agenda-year-view (&optional year)
b349f79f 6476 "Switch to yearly view for agenda.
20908596
CD
6477With argument YEAR, switch to that year.
6478If MONTH has more then 2 digits, only the last two encode the
6479month. Any digits before this encode a year. So 200712 means
6480December year 2007. Years in the range 1938-2037 can also be
6481written as 2-digit years."
6482 (interactive "P")
6483 (when year
6484 (setq year (org-small-year-to-year year)))
6485 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6486 (org-agenda-change-time-span 'year year)
6487 (error "Abort")))
6488
6489(defun org-agenda-change-time-span (span &optional n)
6490 "Change the agenda view to SPAN.
6491SPAN may be `day', `week', `month', `year'."
6492 (org-agenda-check-type t 'agenda)
acedf35c 6493 (if (and (not n) (equal org-agenda-current-span span))
20908596 6494 (error "Viewing span is already \"%s\"" span))
8d642074 6495 (let* ((sd (or (org-get-at-bol 'day)
20908596 6496 org-starting-day))
acedf35c 6497 (sd (org-agenda-compute-starting-span sd span n))
20908596 6498 (org-agenda-overriding-arguments
3ab2c837
BG
6499 (or org-agenda-overriding-arguments
6500 (list (car org-agenda-last-arguments) sd span t))))
20908596
CD
6501 (org-agenda-redo)
6502 (org-agenda-find-same-or-today-or-agenda))
6503 (org-agenda-set-mode-name)
6504 (message "Switched to %s view" span))
6505
acedf35c
CD
6506(defun org-agenda-compute-starting-span (sd span &optional n)
6507 "Compute starting date for agenda.
20908596
CD
6508SPAN may be `day', `week', `month', `year'. The return value
6509is a cons cell with the starting date and the number of days,
6510so that the date SD will be in that range."
6511 (let* ((greg (calendar-gregorian-from-absolute sd))
6512 (dg (nth 1 greg))
6513 (mg (car greg))
acedf35c 6514 (yg (nth 2 greg)))
20908596
CD
6515 (cond
6516 ((eq span 'day)
6517 (when n
6518 (setq sd (+ (calendar-absolute-from-gregorian
6519 (list mg 1 yg))
acedf35c 6520 n -1))))
20908596
CD
6521 ((eq span 'week)
6522 (let* ((nt (calendar-day-of-week
6523 (calendar-gregorian-from-absolute sd)))
6524 (d (if org-agenda-start-on-weekday
6525 (- nt org-agenda-start-on-weekday)
acedf35c
CD
6526 0))
6527 y1)
20908596
CD
6528 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6529 (when n
6530 (require 'cal-iso)
20908596
CD
6531 (when (> n 99)
6532 (setq y1 (org-small-year-to-year (/ n 100))
6533 n (mod n 100)))
6534 (setq sd
6535 (calendar-absolute-from-iso
6536 (list n 1
acedf35c 6537 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
20908596 6538 ((eq span 'month)
acedf35c
CD
6539 (let (y1)
6540 (when (and n (> n 99))
6541 (setq y1 (org-small-year-to-year (/ n 100))
6542 n (mod n 100)))
6543 (setq sd (calendar-absolute-from-gregorian
6544 (list (or n mg) 1 (or y1 yg))))))
20908596
CD
6545 ((eq span 'year)
6546 (setq sd (calendar-absolute-from-gregorian
acedf35c
CD
6547 (list 1 1 (or n yg))))))
6548 sd))
20908596
CD
6549
6550(defun org-agenda-next-date-line (&optional arg)
6551 "Jump to the next line indicating a date in agenda buffer."
6552 (interactive "p")
6553 (org-agenda-check-type t 'agenda 'timeline)
6554 (beginning-of-line 1)
6555 ;; This does not work if user makes date format that starts with a blank
6556 (if (looking-at "^\\S-") (forward-char 1))
6557 (if (not (re-search-forward "^\\S-" nil t arg))
6558 (progn
6559 (backward-char 1)
6560 (error "No next date after this line in this buffer")))
6561 (goto-char (match-beginning 0)))
6562
6563(defun org-agenda-previous-date-line (&optional arg)
6564 "Jump to the previous line indicating a date in agenda buffer."
6565 (interactive "p")
6566 (org-agenda-check-type t 'agenda 'timeline)
6567 (beginning-of-line 1)
6568 (if (not (re-search-backward "^\\S-" nil t arg))
6569 (error "No previous date before this line in this buffer")))
6570
6571;; Initialize the highlight
86fbb8ca
CD
6572(defvar org-hl (make-overlay 1 1))
6573(overlay-put org-hl 'face 'highlight)
20908596
CD
6574
6575(defun org-highlight (begin end &optional buffer)
6576 "Highlight a region with overlay."
86fbb8ca 6577 (move-overlay org-hl begin end (or buffer (current-buffer))))
20908596
CD
6578
6579(defun org-unhighlight ()
6580 "Detach overlay INDEX."
86fbb8ca 6581 (org-detach-overlay org-hl))
20908596
CD
6582
6583;; FIXME this is currently not used.
6584(defun org-highlight-until-next-command (beg end &optional buffer)
6585 "Move the highlight overlay to BEG/END, remove it before the next command."
6586 (org-highlight beg end buffer)
6587 (add-hook 'pre-command-hook 'org-unhighlight-once))
6588(defun org-unhighlight-once ()
6589 "Remove the highlight from its position, and this function from the hook."
6590 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6591 (org-unhighlight))
6592
6593(defun org-agenda-follow-mode ()
6594 "Toggle follow mode in an agenda buffer."
6595 (interactive)
6596 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6597 (org-agenda-set-mode-name)
8bfe682a
CD
6598 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6599 (org-agenda-show))
20908596
CD
6600 (message "Follow mode is %s"
6601 (if org-agenda-follow-mode "on" "off")))
6602
54a0dee5
CD
6603(defun org-agenda-entry-text-mode (&optional arg)
6604 "Toggle entry text mode in an agenda buffer."
6605 (interactive "P")
365f8d85
SM
6606 (setq org-agenda-entry-text-mode (or (integerp arg)
6607 (not org-agenda-entry-text-mode)))
54a0dee5
CD
6608 (org-agenda-entry-text-hide)
6609 (and org-agenda-entry-text-mode
6610 (let ((org-agenda-entry-text-maxlines
6611 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6612 (org-agenda-entry-text-show)))
6613 (org-agenda-set-mode-name)
6614 (message "Entry text mode is %s. Maximum number of lines is %d"
6615 (if org-agenda-entry-text-mode "on" "off")
6616 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6617
acedf35c
CD
6618(defun org-agenda-clockreport-mode (&optional with-filter)
6619 "Toggle clocktable mode in an agenda buffer.
6620With prefix arg WITH-FILTER, make the clocktable respect the current
6621agenda filter."
6622 (interactive "P")
20908596 6623 (org-agenda-check-type t 'agenda)
acedf35c
CD
6624 (if with-filter
6625 (setq org-agenda-clockreport-mode 'with-filter)
6626 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
20908596
CD
6627 (org-agenda-set-mode-name)
6628 (org-agenda-redo)
6629 (message "Clocktable mode is %s"
6630 (if org-agenda-clockreport-mode "on" "off")))
6631
93b62de8
CD
6632(defun org-agenda-log-mode (&optional special)
6633 "Toggle log mode in an agenda buffer.
6634With argument SPECIAL, show all possible log items, not only the ones
6635configured in `org-agenda-log-mode-items'.
6636With a double `C-u' prefix arg, show *only* log items, nothing else."
6637 (interactive "P")
20908596 6638 (org-agenda-check-type t 'agenda 'timeline)
93b62de8 6639 (setq org-agenda-show-log
3ab2c837
BG
6640 (cond
6641 ((equal special '(16)) 'only)
6642 ((eq special 'clockcheck)
6643 (if (eq org-agenda-show-log 'clockcheck)
6644 nil 'clockcheck))
6645 (special '(closed clock state))
6646 (t (not org-agenda-show-log))))
20908596
CD
6647 (org-agenda-set-mode-name)
6648 (org-agenda-redo)
6649 (message "Log mode is %s"
6650 (if org-agenda-show-log "on" "off")))
6651
2c3ad40d 6652(defun org-agenda-archives-mode (&optional with-files)
c8d0cf5c
CD
6653 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6654When called with a prefix argument, include all archive files as well."
2c3ad40d
CD
6655 (interactive "P")
6656 (setq org-agenda-archives-mode
6657 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6658 (org-agenda-set-mode-name)
6659 (org-agenda-redo)
6660 (message
6661 "%s"
6662 (cond
6663 ((eq org-agenda-archives-mode nil)
6664 "No archives are included")
6665 ((eq org-agenda-archives-mode 'trees)
6666 (format "Trees with :%s: tag are included" org-archive-tag))
6667 ((eq org-agenda-archives-mode t)
6668 (format "Trees with :%s: tag and all active archive files are included"
6669 org-archive-tag)))))
6670
20908596
CD
6671(defun org-agenda-toggle-diary ()
6672 "Toggle diary inclusion in an agenda buffer."
6673 (interactive)
6674 (org-agenda-check-type t 'agenda)
6675 (setq org-agenda-include-diary (not org-agenda-include-diary))
6676 (org-agenda-redo)
6677 (org-agenda-set-mode-name)
6678 (message "Diary inclusion turned %s"
6679 (if org-agenda-include-diary "on" "off")))
6680
ed21c5c8 6681(defun org-agenda-toggle-deadlines ()
acedf35c 6682 "Toggle inclusion of entries with a deadline in an agenda buffer."
ed21c5c8
CD
6683 (interactive)
6684 (org-agenda-check-type t 'agenda)
6685 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6686 (org-agenda-redo)
6687 (org-agenda-set-mode-name)
6688 (message "Deadlines inclusion turned %s"
6689 (if org-agenda-include-deadlines "on" "off")))
6690
20908596
CD
6691(defun org-agenda-toggle-time-grid ()
6692 "Toggle time grid in an agenda buffer."
6693 (interactive)
6694 (org-agenda-check-type t 'agenda)
6695 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6696 (org-agenda-redo)
6697 (org-agenda-set-mode-name)
6698 (message "Time-grid turned %s"
6699 (if org-agenda-use-time-grid "on" "off")))
6700
6701(defun org-agenda-set-mode-name ()
6702 "Set the mode name to indicate all the small mode settings."
6703 (setq mode-name
acedf35c
CD
6704 (list "Org-Agenda"
6705 (if (get 'org-agenda-files 'org-restrict) " []" "")
6706 " "
6707 '(:eval (org-agenda-span-name org-agenda-current-span))
6708 (if org-agenda-follow-mode " Follow" "")
6709 (if org-agenda-entry-text-mode " ETxt" "")
6710 (if org-agenda-include-diary " Diary" "")
6711 (if org-agenda-include-deadlines " Ddl" "")
6712 (if org-agenda-use-time-grid " Grid" "")
6713 (if (and (boundp 'org-habit-show-habits)
6714 org-habit-show-habits) " Habit" "")
3ab2c837
BG
6715 (cond
6716 ((consp org-agenda-show-log) " LogAll")
6717 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6718 (org-agenda-show-log " Log")
6719 (t ""))
acedf35c
CD
6720 (if (or org-agenda-filter (get 'org-agenda-filter
6721 :preset-filter))
6722 (concat " {" (mapconcat
6723 'identity
6724 (append (get 'org-agenda-filter
6725 :preset-filter)
6726 org-agenda-filter) "") "}")
6727 "")
6728 (if org-agenda-archives-mode
6729 (if (eq org-agenda-archives-mode t)
6730 " Archives"
6731 (format " :%s:" org-archive-tag))
6732 "")
6733 (if org-agenda-clockreport-mode
6734 (if (eq org-agenda-clockreport-mode 'with-filter)
6735 " Clock{}" " Clock")
6736 "")))
20908596
CD
6737 (force-mode-line-update))
6738
6739(defun org-agenda-post-command-hook ()
b349f79f
CD
6740 (setq org-agenda-type
6741 (or (get-text-property (point) 'org-agenda-type)
6742 (get-text-property (max (point-min) (1- (point)))
8bfe682a
CD
6743 'org-agenda-type))))
6744
6745(defun org-agenda-next-line ()
86fbb8ca 6746 "Move cursor to the next line, and show if follow mode is active."
8bfe682a
CD
6747 (interactive)
6748 (call-interactively 'next-line)
1bcdebed
CD
6749 (org-agenda-do-context-action))
6750
8bfe682a
CD
6751(defun org-agenda-previous-line ()
6752 "Move cursor to the previous line, and show if follow-mode is active."
8bfe682a
CD
6753 (interactive)
6754 (call-interactively 'previous-line)
1bcdebed
CD
6755 (org-agenda-do-context-action))
6756
6757(defun org-agenda-do-context-action ()
86fbb8ca 6758 "Show outline path and, maybe, follow mode window."
1bcdebed
CD
6759 (let ((m (org-get-at-bol 'org-marker)))
6760 (if (and org-agenda-follow-mode m)
6761 (org-agenda-show))
6762 (if (and m org-agenda-show-outline-path)
5dec9555
CD
6763 (org-with-point-at m
6764 (org-display-outline-path t)))))
20908596
CD
6765
6766(defun org-agenda-show-priority ()
6767 "Show the priority of the current item.
6768This priority is composed of the main priority given with the [#A] cookies,
6769and by additional input from the age of a schedules or deadline entry."
6770 (interactive)
8d642074 6771 (let* ((pri (org-get-at-bol 'priority)))
20908596
CD
6772 (message "Priority is %d" (if pri pri -1000))))
6773
6774(defun org-agenda-show-tags ()
6775 "Show the tags applicable to the current item."
6776 (interactive)
8d642074 6777 (let* ((tags (org-get-at-bol 'tags)))
20908596
CD
6778 (if tags
6779 (message "Tags are :%s:"
6780 (org-no-properties (mapconcat 'identity tags ":")))
6781 (message "No tags associated with this line"))))
6782
6783(defun org-agenda-goto (&optional highlight)
6784 "Go to the Org-mode file which contains the item at point."
6785 (interactive)
8d642074 6786 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6787 (org-agenda-error)))
6788 (buffer (marker-buffer marker))
6789 (pos (marker-position marker)))
6790 (switch-to-buffer-other-window buffer)
6791 (widen)
86fbb8ca 6792 (push-mark)
20908596
CD
6793 (goto-char pos)
6794 (when (org-mode-p)
6795 (org-show-context 'agenda)
6796 (save-excursion
6797 (and (outline-next-heading)
3ab2c837
BG
6798 (org-flag-heading nil)))) ; show the next heading
6799 (when (outline-invisible-p)
6800 (show-entry)) ; display invisible text
20908596
CD
6801 (recenter (/ (window-height) 2))
6802 (run-hooks 'org-agenda-after-show-hook)
6803 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6804
6805(defvar org-agenda-after-show-hook nil
6806 "Normal hook run after an item has been shown from the agenda.
6807Point is in the buffer where the item originated.")
6808
6809(defun org-agenda-kill ()
6810 "Kill the entry or subtree belonging to the current agenda entry."
6811 (interactive)
6812 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 6813 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6814 (org-agenda-error)))
6815 (buffer (marker-buffer marker))
6816 (pos (marker-position marker))
8d642074 6817 (type (org-get-at-bol 'type))
20908596
CD
6818 dbeg dend (n 0) conf)
6819 (org-with-remote-undo buffer
6820 (with-current-buffer buffer
6821 (save-excursion
6822 (goto-char pos)
6823 (if (and (org-mode-p) (not (member type '("sexp"))))
6824 (setq dbeg (progn (org-back-to-heading t) (point))
6825 dend (org-end-of-subtree t t))
6826 (setq dbeg (point-at-bol)
6827 dend (min (point-max) (1+ (point-at-eol)))))
6828 (goto-char dbeg)
6829 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6830 (setq conf (or (eq t org-agenda-confirm-kill)
6831 (and (numberp org-agenda-confirm-kill)
6832 (> n org-agenda-confirm-kill))))
6833 (and conf
6834 (not (y-or-n-p
6835 (format "Delete entry with %d lines in buffer \"%s\"? "
6836 n (buffer-name buffer))))
6837 (error "Abort"))
6838 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6839 (with-current-buffer buffer (delete-region dbeg dend))
6840 (message "Agenda item and source killed"))))
6841
8bfe682a
CD
6842(defvar org-archive-default-command)
6843(defun org-agenda-archive-default ()
6844 "Archive the entry or subtree belonging to the current agenda entry."
6845 (interactive)
6846 (require 'org-archive)
6847 (org-agenda-archive-with org-archive-default-command))
6848
6849(defun org-agenda-archive-default-with-confirmation ()
6850 "Archive the entry or subtree belonging to the current agenda entry."
6851 (interactive)
6852 (require 'org-archive)
6853 (org-agenda-archive-with org-archive-default-command 'confirm))
6854
20908596
CD
6855(defun org-agenda-archive ()
6856 "Archive the entry or subtree belonging to the current agenda entry."
6857 (interactive)
8bfe682a 6858 (org-agenda-archive-with 'org-archive-subtree))
20908596
CD
6859
6860(defun org-agenda-archive-to-archive-sibling ()
8bfe682a
CD
6861 "Move the entry to the archive sibling."
6862 (interactive)
6863 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6864
6865(defun org-agenda-archive-with (cmd &optional confirm)
20908596
CD
6866 "Move the entry to the archive sibling."
6867 (interactive)
6868 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 6869 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6870 (org-agenda-error)))
6871 (buffer (marker-buffer marker))
6872 (pos (marker-position marker)))
6873 (org-with-remote-undo buffer
6874 (with-current-buffer buffer
6875 (if (org-mode-p)
8bfe682a
CD
6876 (if (and confirm
6877 (not (y-or-n-p "Archive this subtree or entry? ")))
6878 (error "Abort")
6879 (save-excursion
6880 (goto-char pos)
6881 (org-remove-subtree-entries-from-agenda)
6882 (org-back-to-heading t)
6883 (funcall cmd)))
20908596
CD
6884 (error "Archiving works only in Org-mode files"))))))
6885
6886(defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6887 "Remove all lines in the agenda that correspond to a given subtree.
6888The subtree is the one in buffer BUF, starting at BEG and ending at END.
6889If this information is not given, the function uses the tree at point."
6890 (let ((buf (or buf (current-buffer))) m p)
6891 (save-excursion
6892 (unless (and beg end)
6893 (org-back-to-heading t)
6894 (setq beg (point))
6895 (org-end-of-subtree t)
6896 (setq end (point)))
6897 (set-buffer (get-buffer org-agenda-buffer-name))
6898 (save-excursion
6899 (goto-char (point-max))
6900 (beginning-of-line 1)
6901 (while (not (bobp))
8d642074 6902 (when (and (setq m (org-get-at-bol 'org-marker))
20908596
CD
6903 (equal buf (marker-buffer m))
6904 (setq p (marker-position m))
6905 (>= p beg)
c8d0cf5c 6906 (< p end))
20908596
CD
6907 (let ((inhibit-read-only t))
6908 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6909 (beginning-of-line 0))))))
6910
86fbb8ca 6911(defun org-agenda-refile (&optional goto rfloc no-update)
c8d0cf5c 6912 "Refile the item at point."
54a0dee5
CD
6913 (interactive "P")
6914 (if (equal goto '(16))
6915 (org-refile-goto-last-stored)
8d642074 6916 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
6917 (org-agenda-error)))
6918 (buffer (marker-buffer marker))
6919 (pos (marker-position marker))
6920 (rfloc (or rfloc
6921 (org-refile-get-location
3ab2c837 6922 (if goto "Goto" "Refile to") buffer
54a0dee5
CD
6923 org-refile-allow-creating-parent-nodes))))
6924 (with-current-buffer buffer
6925 (save-excursion
6926 (save-restriction
6927 (widen)
6928 (goto-char marker)
6929 (org-remove-subtree-entries-from-agenda)
86fbb8ca
CD
6930 (org-refile goto buffer rfloc)))))
6931 (unless no-update (org-agenda-redo))))
54a0dee5
CD
6932
6933(defun org-agenda-open-link (&optional arg)
6934 "Follow the link in the current line, if any.
8bfe682a 6935This looks for a link in the displayed line in the agenda. It also looks
54a0dee5 6936at the text of the entry itself."
c8d0cf5c 6937 (interactive "P")
8d642074
CD
6938 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6939 (org-get-at-bol 'org-marker)))
6940 (buffer (and marker (marker-buffer marker)))
6941 (prefix (buffer-substring
3ab2c837 6942 (point-at-bol) (point-at-eol))))
8bfe682a
CD
6943 (cond
6944 (buffer
6945 (with-current-buffer buffer
6946 (save-excursion
6947 (save-restriction
6948 (widen)
6949 (goto-char marker)
6950 (org-offer-links-in-entry arg prefix)))))
6951 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6952 (save-excursion
6953 (beginning-of-line 1)
6954 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6955 (org-open-link-from-string (match-string 1)))
6956 (t (error "No link to open here")))))
20908596
CD
6957
6958(defun org-agenda-copy-local-variable (var)
6959 "Get a variable from a referenced buffer and install it here."
8d642074 6960 (let ((m (org-get-at-bol 'org-marker)))
20908596
CD
6961 (when (and m (buffer-live-p (marker-buffer m)))
6962 (org-set-local var (with-current-buffer (marker-buffer m)
6963 (symbol-value var))))))
6964
6965(defun org-agenda-switch-to (&optional delete-other-windows)
6966 "Go to the Org-mode file which contains the item at point."
6967 (interactive)
8bfe682a
CD
6968 (if (and org-return-follows-link
6969 (not (org-get-at-bol 'org-marker))
6970 (org-in-regexp org-bracket-link-regexp))
6971 (org-open-link-from-string (match-string 0))
6972 (let* ((marker (or (org-get-at-bol 'org-marker)
6973 (org-agenda-error)))
6974 (buffer (marker-buffer marker))
6975 (pos (marker-position marker)))
3ab2c837 6976 (org-pop-to-buffer-same-window buffer)
8bfe682a
CD
6977 (and delete-other-windows (delete-other-windows))
6978 (widen)
6979 (goto-char pos)
6980 (when (org-mode-p)
6981 (org-show-context 'agenda)
6982 (save-excursion
6983 (and (outline-next-heading)
3ab2c837
BG
6984 (org-flag-heading nil))) ; show the next heading
6985 (when (outline-invisible-p)
6986 (show-entry)))))) ; display invisible text
20908596
CD
6987
6988(defun org-agenda-goto-mouse (ev)
6989 "Go to the Org-mode file which contains the item at the mouse click."
6990 (interactive "e")
6991 (mouse-set-point ev)
6992 (org-agenda-goto))
6993
fdf730ed
CD
6994(defun org-agenda-show (&optional full-entry)
6995 "Display the Org-mode file which contains the item at point.
6996With prefix argument FULL-ENTRY, make the entire entry visible
6997if it was hidden in the outline."
6998 (interactive "P")
20908596 6999 (let ((win (selected-window)))
fdf730ed
CD
7000 (if full-entry
7001 (let ((org-show-entry-below t))
7002 (org-agenda-goto t))
7003 (org-agenda-goto t))
20908596
CD
7004 (select-window win)))
7005
8bfe682a
CD
7006(defvar org-agenda-show-window nil)
7007(defun org-agenda-show-and-scroll-up ()
7008 "Display the Org-mode file which contains the item at point.
7009When called repeatedly, scroll the window that is displaying the buffer."
7010 (interactive)
7011 (let ((win (selected-window)))
7012 (if (and (window-live-p org-agenda-show-window)
7013 (eq this-command last-command))
7014 (progn
7015 (select-window org-agenda-show-window)
7016 (ignore-errors (scroll-up)))
7017 (org-agenda-goto t)
7018 (show-subtree)
7019 (setq org-agenda-show-window (selected-window)))
7020 (select-window win)))
7021
7022(defun org-agenda-show-scroll-down ()
7023 "Scroll down the window showing the agenda."
7024 (interactive)
7025 (let ((win (selected-window)))
7026 (when (window-live-p org-agenda-show-window)
7027 (select-window org-agenda-show-window)
7028 (ignore-errors (scroll-down))
7029 (select-window win))))
7030
c8d0cf5c
CD
7031(defun org-agenda-show-1 (&optional more)
7032 "Display the Org-mode file which contains the item at point.
8bfe682a 7033The prefix arg selects the amount of information to display:
c8d0cf5c
CD
7034
70350 hide the subtree
70361 just show the entry according to defaults.
54a0dee5
CD
70372 show the children view
70383 show the subtree view
c8d0cf5c
CD
70394 show the entire subtree and any LOGBOOK drawers
70405 show the entire subtree and any drawers
7041With prefix argument FULL-ENTRY, make the entire entry visible
7042if it was hidden in the outline."
7043 (interactive "p")
7044 (let ((win (selected-window)))
7045 (org-agenda-goto t)
7046 (org-recenter-heading 1)
7047 (cond
7048 ((= more 0)
7049 (hide-subtree)
54a0dee5
CD
7050 (save-excursion
7051 (org-back-to-heading)
7052 (run-hook-with-args 'org-cycle-hook 'folded))
7053 (message "Remote: FOLDED"))
3ab2c837 7054 ((and (org-called-interactively-p 'any) (= more 1))
c8d0cf5c
CD
7055 (message "Remote: show with default settings"))
7056 ((= more 2)
7057 (show-entry)
54a0dee5 7058 (show-children)
c8d0cf5c
CD
7059 (save-excursion
7060 (org-back-to-heading)
54a0dee5
CD
7061 (run-hook-with-args 'org-cycle-hook 'children))
7062 (message "Remote: CHILDREN"))
c8d0cf5c
CD
7063 ((= more 3)
7064 (show-subtree)
7065 (save-excursion
7066 (org-back-to-heading)
54a0dee5
CD
7067 (run-hook-with-args 'org-cycle-hook 'subtree))
7068 (message "Remote: SUBTREE"))
c8d0cf5c
CD
7069 ((= more 4)
7070 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7071 (org-drawer-regexp
7072 (concat "^[ \t]*:\\("
7073 (mapconcat 'regexp-quote org-drawers "\\|")
7074 "\\):[ \t]*$")))
7075 (show-subtree)
7076 (save-excursion
7077 (org-back-to-heading)
7078 (org-cycle-hide-drawers 'subtree)))
54a0dee5 7079 (message "Remote: SUBTREE AND LOGBOOK"))
c8d0cf5c
CD
7080 ((> more 4)
7081 (show-subtree)
54a0dee5 7082 (message "Remote: SUBTREE AND ALL DRAWERS")))
c8d0cf5c
CD
7083 (select-window win)))
7084
7085(defun org-recenter-heading (n)
7086 (save-excursion
7087 (org-back-to-heading)
7088 (recenter n)))
7089
7090(defvar org-agenda-cycle-counter nil)
54a0dee5 7091(defun org-agenda-cycle-show (&optional n)
c8d0cf5c
CD
7092 "Show the current entry in another window, with default settings.
7093Default settings are taken from `org-show-hierarchy-above' and siblings.
54a0dee5 7094When use repeatedly in immediate succession, the remote entry will cycle
c8d0cf5c
CD
7095through visibility
7096
54a0dee5
CD
7097children -> subtree -> folded
7098
7099When called with a numeric prefix arg, that arg will be passed through to
7100`org-agenda-show-1'. For the interpretation of that argument, see the
7101docstring of `org-agenda-show-1'."
7102 (interactive "P")
7103 (if (integerp n)
7104 (setq org-agenda-cycle-counter n)
7105 (if (not (eq last-command this-command))
7106 (setq org-agenda-cycle-counter 1)
7107 (if (equal org-agenda-cycle-counter 0)
7108 (setq org-agenda-cycle-counter 2)
7109 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7110 (if (> org-agenda-cycle-counter 3)
7111 (setq org-agenda-cycle-counter 0)))))
c8d0cf5c
CD
7112 (org-agenda-show-1 org-agenda-cycle-counter))
7113
20908596
CD
7114(defun org-agenda-recenter (arg)
7115 "Display the Org-mode file which contains the item at point and recenter."
7116 (interactive "P")
7117 (let ((win (selected-window)))
7118 (org-agenda-goto t)
7119 (recenter arg)
7120 (select-window win)))
7121
7122(defun org-agenda-show-mouse (ev)
7123 "Display the Org-mode file which contains the item at the mouse click."
7124 (interactive "e")
7125 (mouse-set-point ev)
7126 (org-agenda-show))
7127
7128(defun org-agenda-check-no-diary ()
7129 "Check if the entry is a diary link and abort if yes."
8d642074 7130 (if (org-get-at-bol 'org-agenda-diary-link)
20908596
CD
7131 (org-agenda-error)))
7132
7133(defun org-agenda-error ()
7134 (error "Command not allowed in this line"))
7135
7136(defun org-agenda-tree-to-indirect-buffer ()
7137 "Show the subtree corresponding to the current entry in an indirect buffer.
7138This calls the command `org-tree-to-indirect-buffer' from the original
7139Org-mode buffer.
7140With numerical prefix arg ARG, go up to this level and then take that tree.
86fbb8ca
CD
7141With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7142use the dedicated frame)."
20908596
CD
7143 (interactive)
7144 (org-agenda-check-no-diary)
8d642074 7145 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7146 (org-agenda-error)))
7147 (buffer (marker-buffer marker))
7148 (pos (marker-position marker)))
7149 (with-current-buffer buffer
7150 (save-excursion
7151 (goto-char pos)
7152 (call-interactively 'org-tree-to-indirect-buffer)))))
7153
7154(defvar org-last-heading-marker (make-marker)
7155 "Marker pointing to the headline that last changed its TODO state
7156by a remote command from the agenda.")
7157
7158(defun org-agenda-todo-nextset ()
7159 "Switch TODO entry to next sequence."
7160 (interactive)
7161 (org-agenda-todo 'nextset))
7162
7163(defun org-agenda-todo-previousset ()
7164 "Switch TODO entry to previous sequence."
7165 (interactive)
7166 (org-agenda-todo 'previousset))
7167
7168(defun org-agenda-todo (&optional arg)
7169 "Cycle TODO state of line at point, also in Org-mode file.
7170This changes the line at point, all other lines in the agenda referring to
7171the same tree node, and the headline of the tree node in the Org-mode file."
7172 (interactive "P")
7173 (org-agenda-check-no-diary)
7174 (let* ((col (current-column))
8d642074 7175 (marker (or (org-get-at-bol 'org-marker)
20908596
CD
7176 (org-agenda-error)))
7177 (buffer (marker-buffer marker))
7178 (pos (marker-position marker))
8d642074 7179 (hdmarker (org-get-at-bol 'org-hd-marker))
acedf35c 7180 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
20908596 7181 (inhibit-read-only t)
93b62de8 7182 org-agenda-headline-snapshot-before-repeat newhead just-one)
20908596
CD
7183 (org-with-remote-undo buffer
7184 (with-current-buffer buffer
7185 (widen)
7186 (goto-char pos)
7187 (org-show-context 'agenda)
7188 (save-excursion
7189 (and (outline-next-heading)
7190 (org-flag-heading nil))) ; show the next heading
a2a2e7fb
CD
7191 (let ((current-prefix-arg arg))
7192 (call-interactively 'org-todo))
20908596
CD
7193 (and (bolp) (forward-char 1))
7194 (setq newhead (org-get-heading))
93b62de8
CD
7195 (when (and (org-bound-and-true-p
7196 org-agenda-headline-snapshot-before-repeat)
7197 (not (equal org-agenda-headline-snapshot-before-repeat
7198 newhead))
7199 todayp)
7200 (setq newhead org-agenda-headline-snapshot-before-repeat
7201 just-one t))
20908596
CD
7202 (save-excursion
7203 (org-back-to-heading)
7204 (move-marker org-last-heading-marker (point))))
7205 (beginning-of-line 1)
7206 (save-excursion
93b62de8 7207 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
20908596
CD
7208 (org-move-to-column col))))
7209
7210(defun org-agenda-add-note (&optional arg)
7211 "Add a time-stamped note to the entry at point."
7212 (interactive "P")
7213 (org-agenda-check-no-diary)
8d642074 7214 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7215 (org-agenda-error)))
7216 (buffer (marker-buffer marker))
7217 (pos (marker-position marker))
8d642074 7218 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
7219 (inhibit-read-only t))
7220 (with-current-buffer buffer
7221 (widen)
7222 (goto-char pos)
7223 (org-show-context 'agenda)
7224 (save-excursion
7225 (and (outline-next-heading)
7226 (org-flag-heading nil))) ; show the next heading
7227 (org-add-note))))
7228
db55f368 7229(defun org-agenda-change-all-lines (newhead hdmarker
4ed008de 7230 &optional fixface just-this)
20908596
CD
7231 "Change all lines in the agenda buffer which match HDMARKER.
7232The new content of the line will be NEWHEAD (as modified by
7233`org-format-agenda-item'). HDMARKER is checked with
7234`equal' against all `org-hd-marker' text properties in the file.
33306645 7235If FIXFACE is non-nil, the face of each item is modified according to
db55f368
CD
7236the new TODO state.
7237If JUST-THIS is non-nil, change just the current line, not all.
33306645 7238If FORCE-TAGS is non nil, the car of it returns the new tags."
20908596 7239 (let* ((inhibit-read-only t)
93b62de8 7240 (line (org-current-line))
fdf730ed 7241 (thetags (with-current-buffer (marker-buffer hdmarker)
4ed008de
CD
7242 (save-excursion (save-restriction (widen)
7243 (goto-char hdmarker)
fdf730ed 7244 (org-get-tags-at)))))
20908596
CD
7245 props m pl undone-face done-face finish new dotime cat tags)
7246 (save-excursion
7247 (goto-char (point-max))
7248 (beginning-of-line 1)
7249 (while (not finish)
7250 (setq finish (bobp))
8d642074 7251 (when (and (setq m (org-get-at-bol 'org-hd-marker))
93b62de8 7252 (or (not just-this) (= (org-current-line) line))
20908596
CD
7253 (equal m hdmarker))
7254 (setq props (text-properties-at (point))
8d642074
CD
7255 dotime (org-get-at-bol 'dotime)
7256 cat (org-get-at-bol 'org-category)
4ed008de 7257 tags thetags
3ab2c837
BG
7258 new
7259 (let ((org-prefix-format-compiled
7260 (or (get-text-property (point) 'format)
7261 org-prefix-format-compiled)))
7262 (with-current-buffer (marker-buffer hdmarker)
7263 (save-excursion
7264 (save-restriction
7265 (widen)
7266 (org-format-agenda-item (org-get-at-bol 'extra)
7267 newhead cat tags dotime)))))
7268 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8d642074
CD
7269 undone-face (org-get-at-bol 'undone-face)
7270 done-face (org-get-at-bol 'done-face))
3ab2c837 7271 (beginning-of-line 1)
20908596
CD
7272 (cond
7273 ((equal new "")
20908596
CD
7274 (and (looking-at ".*\n?") (replace-match "")))
7275 ((looking-at ".*")
7276 (replace-match new t t)
7277 (beginning-of-line 1)
7278 (add-text-properties (point-at-bol) (point-at-eol) props)
7279 (when fixface
7280 (add-text-properties
7281 (point-at-bol) (point-at-eol)
7282 (list 'face
7283 (if org-last-todo-state-is-todo
7284 undone-face done-face))))
7285 (org-agenda-highlight-todo 'line)
7286 (beginning-of-line 1))
7287 (t (error "Line update did not work"))))
7288 (beginning-of-line 0)))
7289 (org-finalize-agenda)))
7290
7291(defun org-agenda-align-tags (&optional line)
7292 "Align all tags in agenda items to `org-agenda-tags-column'."
7293 (let ((inhibit-read-only t) l c)
7294 (save-excursion
7295 (goto-char (if line (point-at-bol) (point-min)))
afe98dfa 7296 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
20908596
CD
7297 (if line (point-at-eol) nil) t)
7298 (add-text-properties
7299 (match-beginning 2) (match-end 2)
30ab4580
GM
7300 (list 'face (delq nil (let ((prop (get-text-property
7301 (match-beginning 2) 'face)))
7302 (or (listp prop) (setq prop (list prop)))
7303 (if (memq 'org-tag prop)
7304 prop
7305 (cons 'org-tag prop))))))
20908596
CD
7306 (setq l (- (match-end 2) (match-beginning 2))
7307 c (if (< org-agenda-tags-column 0)
7308 (- (abs org-agenda-tags-column) l)
7309 org-agenda-tags-column))
7310 (delete-region (match-beginning 1) (match-end 1))
7311 (goto-char (match-beginning 1))
7312 (insert (org-add-props
7313 (make-string (max 1 (- c (current-column))) ?\ )
ed21c5c8
CD
7314 (plist-put (copy-sequence (text-properties-at (point)))
7315 'face nil))))
ff4be292
CD
7316 (goto-char (point-min))
7317 (org-font-lock-add-tag-faces (point-max)))))
20908596
CD
7318
7319(defun org-agenda-priority-up ()
7320 "Increase the priority of line at point, also in Org-mode file."
7321 (interactive)
7322 (org-agenda-priority 'up))
7323
7324(defun org-agenda-priority-down ()
7325 "Decrease the priority of line at point, also in Org-mode file."
7326 (interactive)
7327 (org-agenda-priority 'down))
7328
7329(defun org-agenda-priority (&optional force-direction)
7330 "Set the priority of line at point, also in Org-mode file.
7331This changes the line at point, all other lines in the agenda referring to
7332the same tree node, and the headline of the tree node in the Org-mode file."
7333 (interactive)
c8d0cf5c
CD
7334 (unless org-enable-priority-commands
7335 (error "Priority commands are disabled"))
20908596 7336 (org-agenda-check-no-diary)
8d642074 7337 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 7338 (org-agenda-error)))
8d642074 7339 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
7340 (buffer (marker-buffer hdmarker))
7341 (pos (marker-position hdmarker))
7342 (inhibit-read-only t)
7343 newhead)
7344 (org-with-remote-undo buffer
7345 (with-current-buffer buffer
7346 (widen)
7347 (goto-char pos)
7348 (org-show-context 'agenda)
7349 (save-excursion
7350 (and (outline-next-heading)
7351 (org-flag-heading nil))) ; show the next heading
7352 (funcall 'org-priority force-direction)
7353 (end-of-line 1)
7354 (setq newhead (org-get-heading)))
7355 (org-agenda-change-all-lines newhead hdmarker)
7356 (beginning-of-line 1))))
7357
7358;; FIXME: should fix the tags property of the agenda line.
c8d0cf5c 7359(defun org-agenda-set-tags (&optional tag onoff)
20908596
CD
7360 "Set tags for the current headline."
7361 (interactive)
7362 (org-agenda-check-no-diary)
3ab2c837 7363 (if (and (org-region-active-p) (org-called-interactively-p 'any))
20908596 7364 (call-interactively 'org-change-tag-in-region)
8d642074 7365 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
7366 (org-agenda-error)))
7367 (buffer (marker-buffer hdmarker))
7368 (pos (marker-position hdmarker))
7369 (inhibit-read-only t)
4ed008de 7370 newhead)
20908596
CD
7371 (org-with-remote-undo buffer
7372 (with-current-buffer buffer
7373 (widen)
7374 (goto-char pos)
7375 (save-excursion
7376 (org-show-context 'agenda))
7377 (save-excursion
7378 (and (outline-next-heading)
7379 (org-flag-heading nil))) ; show the next heading
7380 (goto-char pos)
c8d0cf5c
CD
7381 (if tag
7382 (org-toggle-tag tag onoff)
7383 (call-interactively 'org-set-tags))
20908596
CD
7384 (end-of-line 1)
7385 (setq newhead (org-get-heading)))
4ed008de 7386 (org-agenda-change-all-lines newhead hdmarker)
20908596
CD
7387 (beginning-of-line 1)))))
7388
54a0dee5
CD
7389(defun org-agenda-set-property ()
7390 "Set a property for the current headline."
7391 (interactive)
7392 (org-agenda-check-no-diary)
8d642074 7393 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
7394 (org-agenda-error)))
7395 (buffer (marker-buffer hdmarker))
7396 (pos (marker-position hdmarker))
7397 (inhibit-read-only t)
7398 newhead)
7399 (org-with-remote-undo buffer
7400 (with-current-buffer buffer
7401 (widen)
7402 (goto-char pos)
7403 (save-excursion
7404 (org-show-context 'agenda))
7405 (save-excursion
7406 (and (outline-next-heading)
7407 (org-flag-heading nil))) ; show the next heading
7408 (goto-char pos)
7409 (call-interactively 'org-set-property)))))
7410
7411(defun org-agenda-set-effort ()
7412 "Set the effort property for the current headline."
7413 (interactive)
7414 (org-agenda-check-no-diary)
8d642074 7415 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
7416 (org-agenda-error)))
7417 (buffer (marker-buffer hdmarker))
7418 (pos (marker-position hdmarker))
7419 (inhibit-read-only t)
7420 newhead)
7421 (org-with-remote-undo buffer
7422 (with-current-buffer buffer
7423 (widen)
7424 (goto-char pos)
7425 (save-excursion
7426 (org-show-context 'agenda))
7427 (save-excursion
7428 (and (outline-next-heading)
3ab2c837 7429 (org-flag-heading nil))) ; show the next heading
54a0dee5
CD
7430 (goto-char pos)
7431 (call-interactively 'org-set-effort)
3ab2c837
BG
7432 (end-of-line 1)
7433 (setq newhead (org-get-heading)))
7434 (org-agenda-change-all-lines newhead hdmarker))))
54a0dee5 7435
20908596
CD
7436(defun org-agenda-toggle-archive-tag ()
7437 "Toggle the archive tag for the current entry."
7438 (interactive)
7439 (org-agenda-check-no-diary)
8d642074 7440 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
7441 (org-agenda-error)))
7442 (buffer (marker-buffer hdmarker))
7443 (pos (marker-position hdmarker))
7444 (inhibit-read-only t)
7445 newhead)
7446 (org-with-remote-undo buffer
7447 (with-current-buffer buffer
7448 (widen)
7449 (goto-char pos)
7450 (org-show-context 'agenda)
7451 (save-excursion
7452 (and (outline-next-heading)
7453 (org-flag-heading nil))) ; show the next heading
7454 (call-interactively 'org-toggle-archive-tag)
7455 (end-of-line 1)
7456 (setq newhead (org-get-heading)))
7457 (org-agenda-change-all-lines newhead hdmarker)
7458 (beginning-of-line 1))))
7459
c8d0cf5c
CD
7460(defun org-agenda-do-date-later (arg)
7461 (interactive "P")
7462 (cond
7463 ((or (equal arg '(16))
7464 (memq last-command
7465 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7466 (setq this-command 'org-agenda-date-later-minutes)
7467 (org-agenda-date-later-minutes 1))
7468 ((or (equal arg '(4))
7469 (memq last-command
7470 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7471 (setq this-command 'org-agenda-date-later-hours)
7472 (org-agenda-date-later-hours 1))
7473 (t
7474 (org-agenda-date-later (prefix-numeric-value arg)))))
7475
7476(defun org-agenda-do-date-earlier (arg)
7477 (interactive "P")
7478 (cond
7479 ((or (equal arg '(16))
7480 (memq last-command
7481 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7482 (setq this-command 'org-agenda-date-earlier-minutes)
7483 (org-agenda-date-earlier-minutes 1))
7484 ((or (equal arg '(4))
7485 (memq last-command
7486 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7487 (setq this-command 'org-agenda-date-earlier-hours)
7488 (org-agenda-date-earlier-hours 1))
7489 (t
7490 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7491
20908596 7492(defun org-agenda-date-later (arg &optional what)
3ab2c837 7493 "Change the date of this item to ARG day(s) later."
20908596
CD
7494 (interactive "p")
7495 (org-agenda-check-type t 'agenda 'timeline)
7496 (org-agenda-check-no-diary)
8d642074 7497 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7498 (org-agenda-error)))
7499 (buffer (marker-buffer marker))
7500 (pos (marker-position marker)))
7501 (org-with-remote-undo buffer
7502 (with-current-buffer buffer
7503 (widen)
7504 (goto-char pos)
7505 (if (not (org-at-timestamp-p))
7506 (error "Cannot find time stamp"))
7507 (org-timestamp-change arg (or what 'day)))
7508 (org-agenda-show-new-time marker org-last-changed-timestamp))
7509 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7510
7511(defun org-agenda-date-earlier (arg &optional what)
3ab2c837 7512 "Change the date of this item to ARG day(s) earlier."
20908596
CD
7513 (interactive "p")
7514 (org-agenda-date-later (- arg) what))
7515
c8d0cf5c
CD
7516(defun org-agenda-date-later-minutes (arg)
7517 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7518 (interactive "p")
7519 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7520 (org-agenda-date-later arg 'minute))
7521
7522(defun org-agenda-date-earlier-minutes (arg)
7523 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7524 (interactive "p")
7525 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7526 (org-agenda-date-earlier arg 'minute))
7527
7528(defun org-agenda-date-later-hours (arg)
7529 "Change the time of this item, in hour steps."
7530 (interactive "p")
7531 (org-agenda-date-later arg 'hour))
7532
7533(defun org-agenda-date-earlier-hours (arg)
7534 "Change the time of this item, in hour steps."
7535 (interactive "p")
7536 (org-agenda-date-earlier arg 'hour))
7537
20908596
CD
7538(defun org-agenda-show-new-time (marker stamp &optional prefix)
7539 "Show new date stamp via text properties."
7540 ;; We use text properties to make this undoable
71d35b24
CD
7541 (let ((inhibit-read-only t)
7542 (buffer-invisibility-spec))
20908596
CD
7543 (setq stamp (concat " " prefix " => " stamp))
7544 (save-excursion
7545 (goto-char (point-max))
7546 (while (not (bobp))
8d642074 7547 (when (equal marker (org-get-at-bol 'org-marker))
20908596 7548 (org-move-to-column (- (window-width) (length stamp)) t)
71d35b24 7549 (org-agenda-fix-tags-filter-overlays-at (point))
20908596
CD
7550 (if (featurep 'xemacs)
7551 ;; Use `duplicable' property to trigger undo recording
7552 (let ((ex (make-extent nil nil))
7553 (gl (make-glyph stamp)))
7554 (set-glyph-face gl 'secondary-selection)
7555 (set-extent-properties
7556 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7557 (insert-extent ex (1- (point)) (point-at-eol)))
7558 (add-text-properties
7559 (1- (point)) (point-at-eol)
7560 (list 'display (org-add-props stamp nil
7561 'face 'secondary-selection))))
7562 (beginning-of-line 1))
7563 (beginning-of-line 0)))))
7564
7565(defun org-agenda-date-prompt (arg)
7566 "Change the date of this item. Date is prompted for, with default today.
7567The prefix ARG is passed to the `org-time-stamp' command and can therefore
7568be used to request time specification in the time stamp."
7569 (interactive "P")
7570 (org-agenda-check-type t 'agenda 'timeline)
7571 (org-agenda-check-no-diary)
8d642074 7572 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7573 (org-agenda-error)))
7574 (buffer (marker-buffer marker))
7575 (pos (marker-position marker)))
7576 (org-with-remote-undo buffer
7577 (with-current-buffer buffer
7578 (widen)
7579 (goto-char pos)
ed21c5c8 7580 (if (not (org-at-timestamp-p t))
20908596 7581 (error "Cannot find time stamp"))
ed21c5c8 7582 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8d642074
CD
7583 (org-agenda-show-new-time marker org-last-changed-timestamp))
7584 (message "Time stamp changed to %s" org-last-changed-timestamp)))
20908596 7585
3ab2c837 7586(defun org-agenda-schedule (arg &optional time)
ed21c5c8 7587 "Schedule the item at point.
3ab2c837 7588ARG is passed through to `org-schedule'."
20908596
CD
7589 (interactive "P")
7590 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7591 (org-agenda-check-no-diary)
8d642074 7592 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7593 (org-agenda-error)))
7594 (type (marker-insertion-type marker))
7595 (buffer (marker-buffer marker))
7596 (pos (marker-position marker))
7597 (org-insert-labeled-timestamps-at-point nil)
7598 ts)
20908596
CD
7599 (set-marker-insertion-type marker t)
7600 (org-with-remote-undo buffer
7601 (with-current-buffer buffer
7602 (widen)
7603 (goto-char pos)
3ab2c837 7604 (setq ts (org-schedule arg time)))
20908596
CD
7605 (org-agenda-show-new-time marker ts "S"))
7606 (message "Item scheduled for %s" ts)))
7607
3ab2c837 7608(defun org-agenda-deadline (arg &optional time)
ed21c5c8 7609 "Schedule the item at point.
3ab2c837 7610ARG is passed through to `org-deadline'."
20908596
CD
7611 (interactive "P")
7612 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7613 (org-agenda-check-no-diary)
8d642074 7614 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7615 (org-agenda-error)))
7616 (buffer (marker-buffer marker))
7617 (pos (marker-position marker))
7618 (org-insert-labeled-timestamps-at-point nil)
7619 ts)
7620 (org-with-remote-undo buffer
7621 (with-current-buffer buffer
7622 (widen)
7623 (goto-char pos)
3ab2c837 7624 (setq ts (org-deadline arg time)))
8d642074 7625 (org-agenda-show-new-time marker ts "D"))
20908596
CD
7626 (message "Deadline for this item set to %s" ts)))
7627
b349f79f
CD
7628(defun org-agenda-action ()
7629 "Select entry for agenda action, or execute an agenda action.
7630This command prompts for another letter. Valid inputs are:
7631
7632m Mark the entry at point for an agenda action
7633s Schedule the marked entry to the date at the cursor
7634d Set the deadline of the marked entry to the date at the cursor
7635r Call `org-remember' with cursor date as the default date
86fbb8ca 7636c Call `org-capture' with cursor date as the default date
b349f79f
CD
7637SPC Show marked entry in other window
7638TAB Visit marked entry in other window
7639
7640The cursor may be at a date in the calendar, or in the Org agenda."
7641 (interactive)
65c439fd 7642 (let (ans)
86fbb8ca 7643 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
b349f79f
CD
7644 (setq ans (read-char-exclusive))
7645 (cond
7646 ((equal ans ?m)
7647 ;; Mark this entry
7648 (if (eq major-mode 'org-agenda-mode)
8d642074
CD
7649 (let ((m (or (org-get-at-bol 'org-hd-marker)
7650 (org-get-at-bol 'org-marker))))
b349f79f
CD
7651 (if m
7652 (progn
7653 (move-marker org-agenda-action-marker
7654 (marker-position m) (marker-buffer m))
7655 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7656 (error "Don't know which entry to mark")))
7657 (error "This command works only in the agenda")))
7658 ((equal ans ?s)
7659 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7660 ((equal ans ?d)
7661 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7662 ((equal ans ?r)
7663 (org-agenda-do-action '(org-remember) t))
86fbb8ca
CD
7664 ((equal ans ?c)
7665 (org-agenda-do-action '(org-capture) t))
b349f79f
CD
7666 ((equal ans ?\ )
7667 (let ((cw (selected-window)))
7668 (org-switch-to-buffer-other-window
7669 (marker-buffer org-agenda-action-marker))
7670 (goto-char org-agenda-action-marker)
7671 (org-show-context 'agenda)
7672 (select-window cw)))
7673 ((equal ans ?\C-i)
7674 (org-switch-to-buffer-other-window
7675 (marker-buffer org-agenda-action-marker))
7676 (goto-char org-agenda-action-marker)
7677 (org-show-context 'agenda))
7678 (t (error "Invalid agenda action %c" ans)))))
7679
7680(defun org-agenda-do-action (form &optional current-buffer)
7681 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7682 (let ((org-overriding-default-time (org-get-cursor-date)))
7683 (if current-buffer
7684 (eval form)
7685 (if (not (marker-buffer org-agenda-action-marker))
8bfe682a 7686 (error "No entry has been selected for agenda action")
b349f79f
CD
7687 (with-current-buffer (marker-buffer org-agenda-action-marker)
7688 (save-excursion
7689 (save-restriction
7690 (widen)
7691 (goto-char org-agenda-action-marker)
7692 (eval form))))))))
ff4be292 7693
20908596
CD
7694(defun org-agenda-clock-in (&optional arg)
7695 "Start the clock on the currently selected item."
7696 (interactive "P")
7697 (org-agenda-check-no-diary)
7698 (if (equal arg '(4))
7699 (org-clock-in arg)
8d642074 7700 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 7701 (org-agenda-error)))
8d642074 7702 (hdmarker (or (org-get-at-bol 'org-hd-marker)
b349f79f
CD
7703 marker))
7704 (pos (marker-position marker))
7705 newhead)
20908596
CD
7706 (org-with-remote-undo (marker-buffer marker)
7707 (with-current-buffer (marker-buffer marker)
7708 (widen)
7709 (goto-char pos)
b349f79f
CD
7710 (org-show-context 'agenda)
7711 (org-show-entry)
7712 (org-cycle-hide-drawers 'children)
7713 (org-clock-in arg)
7714 (setq newhead (org-get-heading)))
c8d0cf5c 7715 (org-agenda-change-all-lines newhead hdmarker)))))
20908596 7716
afe98dfa 7717(defun org-agenda-clock-out ()
20908596 7718 "Stop the currently running clock."
afe98dfa 7719 (interactive)
20908596
CD
7720 (unless (marker-buffer org-clock-marker)
7721 (error "No running clock"))
c8d0cf5c
CD
7722 (let ((marker (make-marker)) newhead)
7723 (org-with-remote-undo (marker-buffer org-clock-marker)
7724 (with-current-buffer (marker-buffer org-clock-marker)
7725 (save-excursion
7726 (save-restriction
7727 (widen)
7728 (goto-char org-clock-marker)
7729 (org-back-to-heading t)
7730 (move-marker marker (point))
7731 (org-clock-out)
7732 (setq newhead (org-get-heading))))))
7733 (org-agenda-change-all-lines newhead marker)
7734 (move-marker marker nil)))
20908596
CD
7735
7736(defun org-agenda-clock-cancel (&optional arg)
7737 "Cancel the currently running clock."
7738 (interactive "P")
7739 (unless (marker-buffer org-clock-marker)
7740 (error "No running clock"))
7741 (org-with-remote-undo (marker-buffer org-clock-marker)
7742 (org-clock-cancel)))
7743
afe98dfa
CD
7744(defun org-agenda-clock-goto ()
7745 "Jump to the currently clocked in task within the agenda.
7746If the currently clocked in task is not listed in the agenda
7747buffer, display it in another window."
7748 (interactive)
7749 (let (pos)
7750 (mapc (lambda (o)
7751 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7752 (setq pos (overlay-start o))))
7753 (overlays-in (point-min) (point-max)))
7754 (cond (pos (goto-char pos))
7755 ;; If the currently clocked entry is not in the agenda
7756 ;; buffer, we visit it in another window:
7757 (org-clock-current-task
7758 (org-switch-to-buffer-other-window (org-clock-goto)))
7759 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7760
8bfe682a
CD
7761(defun org-agenda-diary-entry-in-org-file ()
7762 "Make a diary entry in the file `org-agenda-diary-file'."
5dec9555 7763 (let (d1 d2 char (text "") dp1 dp2)
8bfe682a
CD
7764 (if (equal (buffer-name) "*Calendar*")
7765 (setq d1 (calendar-cursor-to-date t)
7766 d2 (car calendar-mark-ring))
5dec9555
CD
7767 (setq dp1 (get-text-property (point-at-bol) 'day))
7768 (unless dp1 (error "No date defined in current line"))
7769 (setq d1 (calendar-gregorian-from-absolute dp1)
7770 d2 (and (ignore-errors (mark))
7771 (save-excursion
7772 (goto-char (mark))
7773 (setq dp2 (get-text-property (point-at-bol) 'day)))
7774 (calendar-gregorian-from-absolute dp2))))
8bfe682a
CD
7775 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7776 (setq char (read-char-exclusive))
7777 (cond
7778 ((equal char ?d)
7779 (setq text (read-string "Day entry: "))
5dec9555
CD
7780 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7781 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7782 ((equal char ?a)
7783 (setq d1 (list (car d1) (nth 1 d1)
7784 (read-number (format "Reference year [%d]: " (nth 2 d1))
7785 (nth 2 d1))))
7786 (setq text (read-string "Anniversary (use %d to show years): "))
5dec9555
CD
7787 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7788 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7789 ((equal char ?b)
7790 (setq text (read-string "Block entry: "))
7791 (unless (and d1 d2 (not (equal d1 d2)))
7792 (error "No block of days selected"))
5dec9555
CD
7793 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7794 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7795 ((equal char ?j)
7796 (org-switch-to-buffer-other-window
7797 (find-file-noselect org-agenda-diary-file))
ed21c5c8 7798 (require 'org-datetree)
8bfe682a
CD
7799 (org-datetree-find-date-create d1)
7800 (org-reveal t))
7801 (t (error "Invalid selection character `%c'" char)))))
7802
5dec9555
CD
7803(defcustom org-agenda-insert-diary-strategy 'date-tree
7804 "Where in `org-agenda-diary-file' should new entries be added?
7805Valid values:
7806
7807date-tree in the date tree, as child of the date
7808top-level as top-level entries at the end of the file."
7809 :group 'org-agenda
7810 :type '(choice
7811 (const :tag "in a date tree" date-tree)
7812 (const :tag "as top level at end of file" top-level)))
7813
ed21c5c8
CD
7814(defcustom org-agenda-insert-diary-extract-time nil
7815 "Non-nil means extract any time specification from the diary entry."
7816 :group 'org-agenda
7817 :type 'boolean)
7818
8bfe682a
CD
7819(defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7820 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7821If TEXT is not empty, it will become the headline of the new entry, and
7822the resulting entry will not be shown. When TEXT is empty, switch to
7823`org-agenda-diary-file' and let the user finish the entry there."
7824 (let ((cw (current-window-configuration)))
7825 (org-switch-to-buffer-other-window
7826 (find-file-noselect org-agenda-diary-file))
7827 (widen)
7828 (goto-char (point-min))
7829 (cond
7830 ((eq type 'anniversary)
7831 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7832 (progn
7833 (or (org-on-heading-p t)
7834 (progn
7835 (outline-next-heading)
7836 (insert "* Anniversaries\n\n")
7837 (beginning-of-line -1)))))
7838 (outline-next-heading)
7839 (org-back-over-empty-lines)
7840 (backward-char 1)
7841 (insert "\n")
3ab2c837
BG
7842 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7843 (nth 2 d1) (car d1) (nth 1 d1) text)))
8bfe682a 7844 ((eq type 'day)
ed21c5c8
CD
7845 (let ((org-prefix-has-time t)
7846 (org-agenda-time-leading-zero t)
7847 fmt time time2)
7848 (if org-agenda-insert-diary-extract-time
7849 ;; Use org-format-agenda-item to parse text for a time-range and
7850 ;; remove it. FIXME: This is a hack, we should refactor
7851 ;; that function to make time extraction available separately
7852 (setq fmt (org-format-agenda-item nil text nil nil t)
7853 time (get-text-property 0 'time fmt)
7854 time2 (if (> (length time) 0)
7855 ;; split-string removes trailing ...... if
7856 ;; no end time given. First space
7857 ;; separates time from date.
7858 (concat " " (car (split-string time "\\.")))
7859 nil)
7860 text (get-text-property 0 'txt fmt)))
7861 (if (eq org-agenda-insert-diary-strategy 'top-level)
7862 (org-agenda-insert-diary-as-top-level text)
7863 (require 'org-datetree)
7864 (org-datetree-find-date-create d1)
7865 (org-agenda-insert-diary-make-new-entry text))
7866 (org-insert-time-stamp (org-time-from-absolute
7867 (calendar-absolute-from-gregorian d1))
7868 nil nil nil nil time2))
8bfe682a
CD
7869 (end-of-line 0))
7870 ((eq type 'block)
7871 (if (> (calendar-absolute-from-gregorian d1)
7872 (calendar-absolute-from-gregorian d2))
7873 (setq d1 (prog1 d2 (setq d2 d1))))
5dec9555
CD
7874 (if (eq org-agenda-insert-diary-strategy 'top-level)
7875 (org-agenda-insert-diary-as-top-level text)
7876 (require 'org-datetree)
7877 (org-datetree-find-date-create d1)
7878 (org-agenda-insert-diary-make-new-entry text))
8bfe682a
CD
7879 (org-insert-time-stamp (org-time-from-absolute
7880 (calendar-absolute-from-gregorian d1)))
7881 (insert "--")
7882 (org-insert-time-stamp (org-time-from-absolute
7883 (calendar-absolute-from-gregorian d2)))
7884 (end-of-line 0)))
7885 (if (string-match "\\S-" text)
7886 (progn
7887 (set-window-configuration cw)
7888 (message "%s entry added to %s"
7889 (capitalize (symbol-name type))
7890 (abbreviate-file-name org-agenda-diary-file)))
7891 (org-reveal t)
7892 (message "Please finish entry here"))))
7893
5dec9555
CD
7894(defun org-agenda-insert-diary-as-top-level (text)
7895 "Make new entry as a top-level entry at the end of the file.
7896Add TEXT as headline, and position the cursor in the second line so that
7897a timestamp can be added there."
7898 (widen)
7899 (goto-char (point-max))
7900 (or (bolp) (insert "\n"))
7901 (insert "* " text "\n")
7902 (if org-adapt-indentation (org-indent-to-column 2)))
7903
8bfe682a
CD
7904(defun org-agenda-insert-diary-make-new-entry (text)
7905 "Make new entry as last child of current entry.
7906Add TEXT as headline, and position the cursor in the second line so that
7907a timestamp can be added there."
7908 (let ((org-show-following-heading t)
7909 (org-show-siblings t)
7910 (org-show-hierarchy-above t)
7911 (org-show-entry-below t)
7912 col)
7913 (outline-next-heading)
7914 (org-back-over-empty-lines)
7915 (or (looking-at "[ \t]*$")
7916 (progn (insert "\n") (backward-char 1)))
ed21c5c8 7917 (org-insert-heading nil t)
8bfe682a
CD
7918 (org-do-demote)
7919 (setq col (current-column))
7920 (insert text "\n")
7921 (if org-adapt-indentation (org-indent-to-column col))
7922 (let ((org-show-following-heading t)
7923 (org-show-siblings t)
7924 (org-show-hierarchy-above t)
7925 (org-show-entry-below t))
7926 (org-show-context))))
7927
20908596
CD
7928(defun org-agenda-diary-entry ()
7929 "Make a diary entry, like the `i' command from the calendar.
8bfe682a
CD
7930All the standard commands work: block, weekly etc.
7931When `org-agenda-diary-file' points to a file,
7932`org-agenda-diary-entry-in-org-file' is called instead to create
7933entries in that Org-mode file."
20908596
CD
7934 (interactive)
7935 (org-agenda-check-type t 'agenda 'timeline)
8bfe682a
CD
7936 (if (not (eq org-agenda-diary-file 'diary-file))
7937 (org-agenda-diary-entry-in-org-file)
7938 (require 'diary-lib)
7939 (let* ((char (progn
7940 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7941 (read-char-exclusive)))
7942 (cmd (cdr (assoc char
7943 '((?d . insert-diary-entry)
7944 (?w . insert-weekly-diary-entry)
7945 (?m . insert-monthly-diary-entry)
7946 (?y . insert-yearly-diary-entry)
7947 (?a . insert-anniversary-diary-entry)
7948 (?b . insert-block-diary-entry)
7949 (?c . insert-cyclic-diary-entry)))))
7950 (oldf (symbol-function 'calendar-cursor-to-date))
7951 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7952 (point (point))
7953 (mark (or (mark t) (point))))
7954 (unless cmd
7955 (error "No command associated with <%c>" char))
7956 (unless (and (get-text-property point 'day)
7957 (or (not (equal ?b char))
7958 (get-text-property mark 'day)))
7959 (error "Don't know which date to use for diary entry"))
7960 ;; We implement this by hacking the `calendar-cursor-to-date' function
7961 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7962 (let ((calendar-mark-ring
7963 (list (calendar-gregorian-from-absolute
7964 (or (get-text-property mark 'day)
7965 (get-text-property point 'day))))))
7966 (unwind-protect
7967 (progn
7968 (fset 'calendar-cursor-to-date
7969 (lambda (&optional error dummy)
7970 (calendar-gregorian-from-absolute
7971 (get-text-property point 'day))))
20908596 7972 (call-interactively cmd))
8bfe682a 7973 (fset 'calendar-cursor-to-date oldf))))))
20908596 7974
20908596
CD
7975(defun org-agenda-execute-calendar-command (cmd)
7976 "Execute a calendar command from the agenda, with the date associated to
7977the cursor position."
7978 (org-agenda-check-type t 'agenda 'timeline)
7979 (require 'diary-lib)
7980 (unless (get-text-property (point) 'day)
7981 (error "Don't know which date to use for calendar command"))
7982 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7983 (point (point))
7984 (date (calendar-gregorian-from-absolute
7985 (get-text-property point 'day)))
7986 ;; the following 2 vars are needed in the calendar
7987 (displayed-month (car date))
7988 (displayed-year (nth 2 date)))
7989 (unwind-protect
7990 (progn
7991 (fset 'calendar-cursor-to-date
0627c265 7992 (lambda (&optional error dummy)
20908596
CD
7993 (calendar-gregorian-from-absolute
7994 (get-text-property point 'day))))
7995 (call-interactively cmd))
7996 (fset 'calendar-cursor-to-date oldf))))
7997
7998(defun org-agenda-phases-of-moon ()
7999 "Display the phases of the moon for the 3 months around the cursor date."
8000 (interactive)
8001 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8002
8003(defun org-agenda-holidays ()
8004 "Display the holidays for the 3 months around the cursor date."
8005 (interactive)
8006 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8007
8008(defvar calendar-longitude)
8009(defvar calendar-latitude)
8010(defvar calendar-location-name)
8011
8012(defun org-agenda-sunrise-sunset (arg)
8013 "Display sunrise and sunset for the cursor date.
8014Latitude and longitude can be specified with the variables
8015`calendar-latitude' and `calendar-longitude'. When called with prefix
8016argument, latitude and longitude will be prompted for."
8017 (interactive "P")
8018 (require 'solar)
8019 (let ((calendar-longitude (if arg nil calendar-longitude))
8020 (calendar-latitude (if arg nil calendar-latitude))
8021 (calendar-location-name
8022 (if arg "the given coordinates" calendar-location-name)))
8023 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8024
8025(defun org-agenda-goto-calendar ()
8026 "Open the Emacs calendar with the date at the cursor."
8027 (interactive)
8028 (org-agenda-check-type t 'agenda 'timeline)
8029 (let* ((day (or (get-text-property (point) 'day)
8030 (error "Don't know which date to open in calendar")))
8031 (date (calendar-gregorian-from-absolute day))
8032 (calendar-move-hook nil)
8033 (calendar-view-holidays-initially-flag nil)
3820f429 8034 (calendar-view-diary-initially-flag nil))
20908596
CD
8035 (calendar)
8036 (calendar-goto-date date)))
8037
8038;;;###autoload
8039(defun org-calendar-goto-agenda ()
8040 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8041This is a command that has to be installed in `calendar-mode-map'."
8042 (interactive)
8043 (org-agenda-list nil (calendar-absolute-from-gregorian
8044 (calendar-cursor-to-date))
8045 nil))
8046
8047(defun org-agenda-convert-date ()
8048 (interactive)
8049 (org-agenda-check-type t 'agenda 'timeline)
8050 (let ((day (get-text-property (point) 'day))
8051 date s)
8052 (unless day
8053 (error "Don't know which date to convert"))
8054 (setq date (calendar-gregorian-from-absolute day))
8055 (setq s (concat
8056 "Gregorian: " (calendar-date-string date) "\n"
8057 "ISO: " (calendar-iso-date-string date) "\n"
8058 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8059 "Julian: " (calendar-julian-date-string date) "\n"
8060 "Astron. JD: " (calendar-astro-date-string date)
8061 " (Julian date number at noon UTC)\n"
8062 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8063 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8064 "French: " (calendar-french-date-string date) "\n"
8065 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8066 "Mayan: " (calendar-mayan-date-string date) "\n"
8067 "Coptic: " (calendar-coptic-date-string date) "\n"
8068 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8069 "Persian: " (calendar-persian-date-string date) "\n"
8070 "Chinese: " (calendar-chinese-date-string date) "\n"))
8071 (with-output-to-temp-buffer "*Dates*"
8072 (princ s))
93b62de8 8073 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
20908596 8074
c8d0cf5c
CD
8075;;; Bulk commands
8076
8077(defvar org-agenda-bulk-marked-entries nil
8078 "List of markers that refer to marked entries in the agenda.")
8079
54a0dee5
CD
8080(defun org-agenda-bulk-marked-p ()
8081 (eq (get-char-property (point-at-bol) 'type)
8082 'org-marked-entry-overlay))
8083
acedf35c 8084(defun org-agenda-bulk-mark (&optional arg)
c8d0cf5c 8085 "Mark the entry at point for future bulk action."
acedf35c
CD
8086 (interactive "p")
8087 (dotimes (i (max arg 1))
8088 (unless (org-get-at-bol 'org-agenda-diary-link)
8089 (let* ((m (org-get-at-bol 'org-hd-marker))
8090 ov)
8091 (unless (org-agenda-bulk-marked-p)
8092 (unless m (error "Nothing to mark at point"))
8093 (push m org-agenda-bulk-marked-entries)
8094 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8095 (org-overlay-display ov "> "
8096 (org-get-todo-face "TODO")
8097 'evaporate)
8098 (overlay-put ov 'type 'org-marked-entry-overlay))
8099 (beginning-of-line 2)
8100 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8101 (beginning-of-line 2))
8102 (message "%d entries marked for bulk action"
8103 (length org-agenda-bulk-marked-entries))))))
c8d0cf5c 8104
3ab2c837
BG
8105(defun org-agenda-bulk-mark-regexp (regexp)
8106 "Mark entries match REGEXP."
8107 (interactive "sMark entries matching regexp: ")
8108 (let (entries-marked)
8109 (save-excursion
8110 (goto-char (point-min))
8111 (goto-char (next-single-property-change (point) 'txt))
8112 (while (re-search-forward regexp nil t)
8113 (when (string-match regexp (get-text-property (point) 'txt))
8114 (setq entries-marked (+ entries-marked 1))
8115 (call-interactively 'org-agenda-bulk-mark))))
8116 (if (not entries-marked)
8117 (message "No entry matching this regexp."))))
8118
c8d0cf5c
CD
8119(defun org-agenda-bulk-unmark ()
8120 "Unmark the entry at point for future bulk action."
8121 (interactive)
54a0dee5 8122 (when (org-agenda-bulk-marked-p)
c8d0cf5c
CD
8123 (org-agenda-bulk-remove-overlays
8124 (point-at-bol) (+ 2 (point-at-bol)))
8125 (setq org-agenda-bulk-marked-entries
8d642074 8126 (delete (org-get-at-bol 'org-hd-marker)
c8d0cf5c
CD
8127 org-agenda-bulk-marked-entries)))
8128 (beginning-of-line 2)
ed21c5c8
CD
8129 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8130 (beginning-of-line 2))
c8d0cf5c
CD
8131 (message "%d entries marked for bulk action"
8132 (length org-agenda-bulk-marked-entries)))
8133
54a0dee5
CD
8134(defun org-agenda-bulk-toggle ()
8135 "Toggle marking the entry at point for bulk action."
8136 (interactive)
8137 (if (org-agenda-bulk-marked-p)
8138 (org-agenda-bulk-unmark)
8139 (org-agenda-bulk-mark)))
c8d0cf5c
CD
8140
8141(defun org-agenda-bulk-remove-overlays (&optional beg end)
8142 "Remove the mark overlays between BEG and END in the agenda buffer.
8143BEG and END default to the buffer limits.
8144
8145This only removes the overlays, it does not remove the markers
8146from the list in `org-agenda-bulk-marked-entries'."
8147 (interactive)
8148 (mapc (lambda (ov)
86fbb8ca
CD
8149 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8150 (delete-overlay ov)))
8151 (overlays-in (or beg (point-min)) (or end (point-max)))))
c8d0cf5c
CD
8152
8153(defun org-agenda-bulk-remove-all-marks ()
8154 "Remove all marks in the agenda buffer.
8155This will remove the markers, and the overlays."
8156 (interactive)
8157 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8158 (setq org-agenda-bulk-marked-entries nil)
8159 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8160
ed21c5c8
CD
8161(defun org-agenda-bulk-action (&optional arg)
8162 "Execute an remote-editing action on all marked entries.
8163The prefix arg is passed through to the command if possible."
8164 (interactive "P")
3ab2c837
BG
8165 ;; Make sure we have markers, and only valid ones
8166 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8167 (mapc
8168 (lambda (m)
8169 (unless (and (markerp m)
8170 (marker-buffer m)
8171 (buffer-live-p (marker-buffer m))
8172 (marker-position m))
8173 (error "Marker %s for bulk command is invalid" m)))
8174 org-agenda-bulk-marked-entries)
8175
8176 ;; Prompt for the bulk command
8177 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8178 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8179 (when org-agenda-bulk-custom-functions
8180 (concat " Custom: ["
8181 (mapconcat (lambda(f) (char-to-string (car f)))
8182 org-agenda-bulk-custom-functions "")
8183 "]"))))
c8d0cf5c 8184 (let* ((action (read-char-exclusive))
ed21c5c8 8185 (org-log-refile (if org-log-refile 'time nil))
c8d0cf5c 8186 (entries (reverse org-agenda-bulk-marked-entries))
86fbb8ca 8187 redo-at-end
c8d0cf5c
CD
8188 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8189 (cond
8190 ((equal action ?$)
8191 (setq cmd '(org-agenda-archive)))
8192
8193 ((equal action ?A)
8194 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8195
8196 ((member action '(?r ?w))
8197 (setq rfloc (org-refile-get-location
3ab2c837 8198 "Refile to"
c8d0cf5c
CD
8199 (marker-buffer (car org-agenda-bulk-marked-entries))
8200 org-refile-allow-creating-parent-nodes))
86fbb8ca
CD
8201 (if (nth 3 rfloc)
8202 (setcar (nthcdr 3 rfloc)
8203 (move-marker (make-marker) (nth 3 rfloc)
8204 (or (get-file-buffer (nth 1 rfloc))
8205 (find-buffer-visiting (nth 1 rfloc))
8206 (error "This should not happen")))))
c8d0cf5c 8207
86fbb8ca
CD
8208 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8209 redo-at-end t))
c8d0cf5c
CD
8210
8211 ((equal action ?t)
54a0dee5 8212 (setq state (org-icompleting-read
c8d0cf5c
CD
8213 "Todo state: "
8214 (with-current-buffer (marker-buffer (car entries))
8215 (mapcar 'list org-todo-keywords-1))))
8216 (setq cmd `(let ((org-inhibit-blocking t)
8217 (org-inhibit-logging 'note))
8218 (org-agenda-todo ,state))))
8219
8220 ((memq action '(?- ?+))
54a0dee5 8221 (setq tag (org-icompleting-read
c8d0cf5c
CD
8222 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8223 (with-current-buffer (marker-buffer (car entries))
8224 (delq nil
8225 (mapcar (lambda (x)
8226 (if (stringp (car x)) x)) org-tag-alist)))))
8227 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8228
8229 ((memq action '(?s ?d))
ed21c5c8
CD
8230 (let* ((date (unless arg
8231 (org-read-date
8232 nil nil nil
8233 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8234 (ans (if arg nil org-read-date-final-answer))
c8d0cf5c
CD
8235 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8236 (setq cmd `(let* ((bound (fboundp 'read-string))
8237 (old (and bound (symbol-function 'read-string))))
8238 (unwind-protect
8239 (progn
8240 (fset 'read-string (lambda (&rest ignore) ,ans))
ed21c5c8 8241 (eval '(,c1 arg)))
c8d0cf5c
CD
8242 (if bound
8243 (fset 'read-string old)
8244 (fmakunbound 'read-string)))))))
acedf35c 8245
3ab2c837
BG
8246 ((equal action ?S)
8247 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8248 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8249 (let ((days (read-number
8250 (format "Scatter tasks across how many %sdays: "
8251 (if arg "week" "")) 7)))
8252 (setq cmd
8253 `(let ((distance (1+ (random ,days))))
8254 (if arg
8255 (let ((dist distance)
8256 (day-of-week
8257 (calendar-day-of-week
8258 (calendar-gregorian-from-absolute (org-today)))))
8259 (dotimes (i (1+ dist))
8260 (while (member day-of-week org-agenda-weekend-days)
8261 (incf distance)
8262 (incf day-of-week)
8263 (if (= day-of-week 7)
8264 (setq day-of-week 0)))
acedf35c
CD
8265 (incf day-of-week)
8266 (if (= day-of-week 7)
3ab2c837
BG
8267 (setq day-of-week 0)))))
8268 ;; silently fail when try to replan a sexp entry
8269 (condition-case nil
8270 (let* ((date (calendar-gregorian-from-absolute
8271 (+ (org-today) distance)))
8272 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8273 (nth 2 date))))
8274 (org-agenda-schedule nil time))
8275 (error nil)))))))
8276
8277 ((assoc action org-agenda-bulk-custom-functions)
8278 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8279 redo-at-end t))
8280
8281 ((equal action ?f)
8282 (setq cmd (list (intern
8283 (org-icompleting-read "Function: "
8284 obarray 'fboundp t nil nil)))))
acedf35c 8285
c8d0cf5c
CD
8286 (t (error "Invalid bulk action")))
8287
8288 ;; Sort the markers, to make sure that parents are handled before children
8289 (setq entries (sort entries
8290 (lambda (a b)
8291 (cond
8292 ((equal (marker-buffer a) (marker-buffer b))
8293 (< (marker-position a) (marker-position b)))
8294 (t
8295 (string< (buffer-name (marker-buffer a))
8296 (buffer-name (marker-buffer b))))))))
8297
8298 ;; Now loop over all markers and apply cmd
8299 (while (setq e (pop entries))
8300 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8301 (if (not pos)
8302 (progn (message "Skipping removed entry at %s" e)
8303 (setq cntskip (1+ cntskip)))
8304 (goto-char pos)
8305 (eval cmd)
8306 (setq org-agenda-bulk-marked-entries
8307 (delete e org-agenda-bulk-marked-entries))
8308 (setq cnt (1+ cnt))))
8309 (setq org-agenda-bulk-marked-entries nil)
8310 (org-agenda-bulk-remove-all-marks)
86fbb8ca 8311 (when redo-at-end (org-agenda-redo))
c8d0cf5c
CD
8312 (message "Acted on %d entries%s"
8313 cnt
8314 (if (= cntskip 0)
8315 ""
8316 (format ", skipped %d (disappeared before their turn)"
8317 cntskip)))))
8d642074
CD
8318
8319;;; Flagging notes
8320
8321(defun org-agenda-show-the-flagging-note ()
8322 "Display the flagging note in the other window.
8323When called a second time in direct sequence, offer to remove the FLAGGING
8324tag and (if present) the flagging note."
8325 (interactive)
8326 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8327 (win (selected-window))
8328 note heading newhead)
8329 (unless hdmarker
8330 (error "No linked entry at point"))
8331 (if (and (eq this-command last-command)
8332 (y-or-n-p "Unflag and remove any flagging note? "))
8333 (progn
8334 (org-agenda-remove-flag hdmarker)
8335 (let ((win (get-buffer-window "*Flagging Note*")))
8336 (and win (delete-window win)))
8337 (message "Entry unflaged"))
8338 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8339 (unless note
8340 (error "No flagging note"))
8341 (org-kill-new note)
8342 (org-switch-to-buffer-other-window "*Flagging Note*")
8343 (erase-buffer)
8344 (insert note)
8345 (goto-char (point-min))
8346 (while (re-search-forward "\\\\n" nil t)
8347 (replace-match "\n" t t))
8348 (goto-char (point-min))
8349 (select-window win)
8350 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8351
8352(defun org-agenda-remove-flag (marker)
8bfe682a 8353 "Remove the FLAGGED tag and any flagging note in the entry."
8d642074
CD
8354 (let (newhead)
8355 (org-with-point-at marker
8356 (org-toggle-tag "FLAGGED" 'off)
8357 (org-entry-delete nil "THEFLAGGINGNOTE")
8358 (setq newhead (org-get-heading)))
8359 (org-agenda-change-all-lines newhead marker)
8360 (message "Entry unflaged")))
8361
8362(defun org-agenda-get-any-marker (&optional pos)
8363 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8364 (get-text-property (or pos (point-at-bol)) 'org-marker)))
c8d0cf5c 8365
20908596
CD
8366;;; Appointment reminders
8367
8368(defvar appt-time-msg-list)
8369
8370;;;###autoload
8371(defun org-agenda-to-appt (&optional refresh filter)
8372 "Activate appointments found in `org-agenda-files'.
8373With a \\[universal-argument] prefix, refresh the list of
33306645 8374appointments.
20908596
CD
8375
8376If FILTER is t, interactively prompt the user for a regular
8377expression, and filter out entries that don't match it.
8378
8379If FILTER is a string, use this string as a regular expression
8380for filtering entries out.
8381
8382FILTER can also be an alist with the car of each cell being
8383either 'headline or 'category. For example:
8384
8385 '((headline \"IMPORTANT\")
8386 (category \"Work\"))
8387
8388will only add headlines containing IMPORTANT or headlines
8389belonging to the \"Work\" category."
8390 (interactive "P")
20908596
CD
8391 (if refresh (setq appt-time-msg-list nil))
8392 (if (eq filter t)
8393 (setq filter (read-from-minibuffer "Regexp filter: ")))
8394 (let* ((cnt 0) ; count added events
8395 (org-agenda-new-buffers nil)
8396 (org-deadline-warning-days 0)
acedf35c
CD
8397 ;; Do not use `org-today' here because appt only takes
8398 ;; time and without date as argument, so it may pass wrong
8399 ;; information otherwise
20908596
CD
8400 (today (org-date-to-gregorian
8401 (time-to-days (current-time))))
c8d0cf5c 8402 (org-agenda-restrict nil)
621f83e4 8403 (files (org-agenda-files 'unrestricted)) entries file)
20908596 8404 ;; Get all entries which may contain an appt
db55f368 8405 (org-prepare-agenda-buffers files)
20908596
CD
8406 (while (setq file (pop files))
8407 (setq entries
8408 (append entries
8409 (org-agenda-get-day-entries
8410 file today :timestamp :scheduled :deadline))))
8411 (setq entries (delq nil entries))
8412 ;; Map thru entries and find if we should filter them out
8413 (mapc
8414 (lambda(x)
621f83e4 8415 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
20908596
CD
8416 (cat (get-text-property 1 'org-category x))
8417 (tod (get-text-property 1 'time-of-day x))
8418 (ok (or (null filter)
8419 (and (stringp filter) (string-match filter evt))
8420 (and (listp filter)
8421 (or (string-match
8422 (cadr (assoc 'category filter)) cat)
8423 (string-match
8424 (cadr (assoc 'headline filter)) evt))))))
8425 ;; FIXME: Shall we remove text-properties for the appt text?
8426 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8427 (when (and ok tod)
621f83e4 8428 (setq tod (concat "00" (number-to-string tod))
20908596 8429 tod (when (string-match
621f83e4 8430 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
20908596
CD
8431 (concat (match-string 1 tod) ":"
8432 (match-string 2 tod))))
8433 (appt-add tod evt)
8434 (setq cnt (1+ cnt))))) entries)
8435 (org-release-buffers org-agenda-new-buffers)
8436 (if (eq cnt 0)
8437 (message "No event to add")
8438 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8439
621f83e4
CD
8440(defun org-agenda-todayp (date)
8441 "Does DATE mean today, when considering `org-extend-today-until'?"
acedf35c
CD
8442 (let ((today (org-today))
8443 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8444 date)))
8445 (eq date today)))
621f83e4 8446
20908596
CD
8447(provide 'org-agenda)
8448
5b409b39 8449
b349f79f 8450
20908596 8451;;; org-agenda.el ends here